2006-11-10 05:03:26 +00:00
|
|
|
//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for expressions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
2006-11-10 06:20:45 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2006-11-20 04:58:19 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
2006-11-10 05:03:26 +00:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2007-03-09 23:16:33 +00:00
|
|
|
#include "clang/Lex/LiteralSupport.h"
|
2007-03-13 20:29:44 +00:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2006-11-10 05:03:26 +00:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2006-11-20 06:49:47 +00:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2006-11-10 05:03:26 +00:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2007-05-03 21:03:48 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2006-11-10 05:03:26 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2007-04-27 18:30:00 +00:00
|
|
|
// Sema.h avoids including Expr.h. As a result, all the Check* functions take
|
|
|
|
// an unsigned which is really an enum. These typedefs provide a short hand
|
|
|
|
// notiation for casting (to keep the lines within 80 columns:-)
|
|
|
|
typedef BinaryOperator::Opcode BOP;
|
|
|
|
typedef UnaryOperator::Opcode UOP;
|
|
|
|
|
2007-02-21 23:46:25 +00:00
|
|
|
/// ParseStringLiteral - The specified tokens were lexed as pasted string
|
2006-11-10 05:03:26 +00:00
|
|
|
/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
|
|
|
|
/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
|
|
|
|
/// multiple tokens. However, the common case is that StringToks points to one
|
|
|
|
/// string.
|
|
|
|
///
|
|
|
|
Action::ExprResult
|
2007-02-21 23:46:25 +00:00
|
|
|
Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) {
|
2006-11-10 05:03:26 +00:00
|
|
|
assert(NumStringToks && "Must have at least one string!");
|
|
|
|
|
2007-03-13 22:37:02 +00:00
|
|
|
StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
|
|
|
|
if (Literal.hadError)
|
|
|
|
return ExprResult(true);
|
2006-11-10 05:03:26 +00:00
|
|
|
|
|
|
|
SmallVector<SourceLocation, 4> StringTokLocs;
|
|
|
|
for (unsigned i = 0; i != NumStringToks; ++i)
|
|
|
|
StringTokLocs.push_back(StringToks[i].getLocation());
|
2007-03-23 22:27:02 +00:00
|
|
|
|
|
|
|
// FIXME: handle wchar_t
|
2007-04-05 22:36:20 +00:00
|
|
|
QualType t = Context.getPointerType(Context.CharTy);
|
2007-03-23 22:27:02 +00:00
|
|
|
|
2006-11-10 05:03:26 +00:00
|
|
|
// FIXME: use factory.
|
|
|
|
// Pass &StringTokLocs[0], StringTokLocs.size() to factory!
|
2007-03-13 22:37:02 +00:00
|
|
|
return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
|
2007-03-23 22:27:02 +00:00
|
|
|
Literal.AnyWide, t);
|
2006-11-10 05:03:26 +00:00
|
|
|
}
|
|
|
|
|
2006-11-10 05:29:30 +00:00
|
|
|
|
2006-11-20 06:49:47 +00:00
|
|
|
/// ParseIdentifierExpr - The parser read an identifier in expression context,
|
|
|
|
/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
|
|
|
|
/// identifier is used in an function call context.
|
|
|
|
Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
|
|
|
|
IdentifierInfo &II,
|
|
|
|
bool HasTrailingLParen) {
|
2006-11-20 04:58:19 +00:00
|
|
|
// Could be enum-constant or decl.
|
2007-01-28 08:20:04 +00:00
|
|
|
Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
|
2006-11-20 04:58:19 +00:00
|
|
|
if (D == 0) {
|
2007-02-13 01:51:42 +00:00
|
|
|
// Otherwise, this could be an implicitly declared function reference (legal
|
2007-01-28 08:20:04 +00:00
|
|
|
// in C90, extension in C99).
|
2006-11-20 06:49:47 +00:00
|
|
|
if (HasTrailingLParen &&
|
|
|
|
// Not in C++.
|
2007-03-23 22:27:02 +00:00
|
|
|
!getLangOptions().CPlusPlus)
|
2006-11-20 06:49:47 +00:00
|
|
|
D = ImplicitlyDefineFunction(Loc, II, S);
|
2007-04-02 22:35:25 +00:00
|
|
|
else {
|
2006-11-20 06:49:47 +00:00
|
|
|
// If this name wasn't predeclared and if this is not a function call,
|
|
|
|
// diagnose the problem.
|
2007-03-23 22:27:02 +00:00
|
|
|
return Diag(Loc, diag::err_undeclared_var_use, II.getName());
|
2007-04-02 22:35:25 +00:00
|
|
|
}
|
2006-11-20 04:58:19 +00:00
|
|
|
}
|
|
|
|
|
2007-04-03 23:13:13 +00:00
|
|
|
if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
|
|
|
|
return new DeclRefExpr(VD, VD->getType());
|
|
|
|
if (isa<TypedefDecl>(D))
|
2007-03-23 22:27:02 +00:00
|
|
|
return Diag(Loc, diag::err_unexpected_typedef, II.getName());
|
|
|
|
|
|
|
|
assert(0 && "Invalid decl");
|
2006-11-20 04:58:19 +00:00
|
|
|
}
|
2006-11-10 05:29:30 +00:00
|
|
|
|
2006-11-20 04:58:19 +00:00
|
|
|
Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
|
|
|
|
tok::TokenKind Kind) {
|
2006-11-10 05:29:30 +00:00
|
|
|
switch (Kind) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown simple primary expr!");
|
2007-04-27 18:30:00 +00:00
|
|
|
// TODO: MOVE this to be some other callback.
|
2006-11-10 05:29:30 +00:00
|
|
|
case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
|
|
|
|
case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
|
|
|
|
case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
|
2006-11-20 04:58:19 +00:00
|
|
|
return 0;
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-26 20:39:23 +00:00
|
|
|
Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
|
|
|
|
SmallString<16> CharBuffer;
|
|
|
|
CharBuffer.resize(Tok.getLength());
|
|
|
|
const char *ThisTokBegin = &CharBuffer[0];
|
|
|
|
unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
|
|
|
|
|
|
|
|
CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
|
|
|
|
Tok.getLocation(), PP);
|
|
|
|
if (Literal.hadError())
|
|
|
|
return ExprResult(true);
|
|
|
|
return new CharacterLiteral(Literal.getValue(), Context.IntTy);
|
|
|
|
}
|
|
|
|
|
2007-03-06 01:09:46 +00:00
|
|
|
Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
|
2007-03-13 20:29:44 +00:00
|
|
|
// fast path for a single digit (which is quite common). A single digit
|
|
|
|
// cannot have a trigraph, escaped newline, radix prefix, or type suffix.
|
|
|
|
if (Tok.getLength() == 1) {
|
|
|
|
const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
|
|
|
|
return ExprResult(new IntegerLiteral(*t-'0', Context.IntTy));
|
|
|
|
}
|
2007-03-06 01:09:46 +00:00
|
|
|
SmallString<512> IntegerBuffer;
|
|
|
|
IntegerBuffer.resize(Tok.getLength());
|
|
|
|
const char *ThisTokBegin = &IntegerBuffer[0];
|
|
|
|
|
|
|
|
// Get the spelling of the token, which eliminates trigraphs, etc. Notes:
|
|
|
|
// - We know that ThisTokBuf points to a buffer that is big enough for the
|
|
|
|
// whole token and 'spelled' tokens can only shrink.
|
|
|
|
// - In practice, the local buffer is only used when the spelling doesn't
|
|
|
|
// match the original token (which is rare). The common case simply returns
|
|
|
|
// a pointer to a *constant* buffer (avoiding a copy).
|
|
|
|
|
|
|
|
unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
|
2007-03-09 23:16:33 +00:00
|
|
|
NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
|
2007-03-12 23:22:38 +00:00
|
|
|
Tok.getLocation(), PP);
|
2007-03-13 20:29:44 +00:00
|
|
|
if (Literal.hadError)
|
|
|
|
return ExprResult(true);
|
|
|
|
|
2007-03-09 23:16:33 +00:00
|
|
|
if (Literal.isIntegerLiteral()) {
|
2007-04-05 22:36:20 +00:00
|
|
|
QualType t;
|
2007-03-09 23:16:33 +00:00
|
|
|
if (Literal.hasSuffix()) {
|
|
|
|
if (Literal.isLong)
|
|
|
|
t = Literal.isUnsigned ? Context.UnsignedLongTy : Context.LongTy;
|
|
|
|
else if (Literal.isLongLong)
|
|
|
|
t = Literal.isUnsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
|
|
|
|
else
|
|
|
|
t = Context.UnsignedIntTy;
|
2007-03-06 01:09:46 +00:00
|
|
|
} else {
|
2007-03-09 23:16:33 +00:00
|
|
|
t = Context.IntTy; // implicit type is "int"
|
2007-03-07 01:21:37 +00:00
|
|
|
}
|
2007-03-12 23:22:38 +00:00
|
|
|
uintmax_t val;
|
|
|
|
if (Literal.GetIntegerValue(val)) {
|
2007-03-13 20:29:44 +00:00
|
|
|
return new IntegerLiteral(val, t);
|
2007-03-07 01:21:37 +00:00
|
|
|
}
|
2007-03-09 23:16:33 +00:00
|
|
|
} else if (Literal.isFloatingLiteral()) {
|
2007-03-23 22:27:02 +00:00
|
|
|
// FIXME: fill in the value and compute the real type...
|
|
|
|
return new FloatingLiteral(7.7, Context.FloatTy);
|
2007-03-06 01:09:46 +00:00
|
|
|
}
|
2007-03-13 20:29:44 +00:00
|
|
|
return ExprResult(true);
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
|
|
|
|
ExprTy *Val) {
|
2007-04-26 20:39:23 +00:00
|
|
|
Expr *e = (Expr *)Val;
|
|
|
|
assert((e != 0) && "ParseParenExpr() missing expr");
|
|
|
|
return e;
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::ExprResult Sema::
|
|
|
|
ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
|
|
|
|
SourceLocation LParenLoc, TypeTy *Ty,
|
|
|
|
SourceLocation RParenLoc) {
|
2006-11-20 04:34:45 +00:00
|
|
|
// If error parsing type, ignore.
|
|
|
|
if (Ty == 0) return true;
|
2007-01-23 22:29:49 +00:00
|
|
|
|
|
|
|
// Verify that this is a valid expression.
|
2007-04-05 22:36:20 +00:00
|
|
|
QualType ArgTy = QualType::getFromOpaquePtr(Ty);
|
2007-01-23 22:29:49 +00:00
|
|
|
|
|
|
|
if (isa<FunctionType>(ArgTy) && isSizeof) {
|
|
|
|
// alignof(function) is allowed.
|
|
|
|
Diag(OpLoc, diag::ext_sizeof_function_type);
|
2007-03-21 21:08:52 +00:00
|
|
|
return new IntegerLiteral(1, Context.IntTy);
|
2007-01-23 22:29:49 +00:00
|
|
|
} else if (ArgTy->isVoidType()) {
|
|
|
|
Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
|
|
|
|
} else if (ArgTy->isIncompleteType()) {
|
|
|
|
std::string TypeName;
|
|
|
|
ArgTy->getAsString(TypeName);
|
|
|
|
Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
|
|
|
|
diag::err_alignof_incomplete_type, TypeName);
|
2007-03-21 21:08:52 +00:00
|
|
|
return new IntegerLiteral(0, Context.IntTy);
|
2007-01-23 22:29:49 +00:00
|
|
|
}
|
2007-04-02 22:35:25 +00:00
|
|
|
// C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
|
|
|
|
return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, Context.getSizeType());
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
|
|
|
|
tok::TokenKind Kind,
|
|
|
|
ExprTy *Input) {
|
|
|
|
UnaryOperator::Opcode Opc;
|
|
|
|
switch (Kind) {
|
|
|
|
default: assert(0 && "Unknown unary op!");
|
|
|
|
case tok::plusplus: Opc = UnaryOperator::PostInc; break;
|
|
|
|
case tok::minusminus: Opc = UnaryOperator::PostDec; break;
|
|
|
|
}
|
2007-05-07 00:24:15 +00:00
|
|
|
QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
|
|
|
|
if (result.isNull())
|
|
|
|
return true;
|
|
|
|
return new UnaryOperator((Expr *)Input, Opc, result);
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::ExprResult Sema::
|
|
|
|
ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
|
|
|
|
ExprTy *Idx, SourceLocation RLoc) {
|
2007-04-05 22:36:20 +00:00
|
|
|
QualType t1 = ((Expr *)Base)->getType();
|
|
|
|
QualType t2 = ((Expr *)Idx)->getType();
|
2007-03-23 22:27:02 +00:00
|
|
|
|
|
|
|
assert(!t1.isNull() && "no type for array base expression");
|
2007-03-28 21:49:40 +00:00
|
|
|
assert(!t2.isNull() && "no type for array index expression");
|
2007-03-23 22:27:02 +00:00
|
|
|
|
2007-04-05 22:36:20 +00:00
|
|
|
QualType canonT1 = t1.getCanonicalType();
|
|
|
|
QualType canonT2 = t2.getCanonicalType();
|
2007-04-05 21:15:20 +00:00
|
|
|
|
2007-03-28 21:49:40 +00:00
|
|
|
// C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
|
|
|
|
// to the expression *((e1)+(e2)). This means the array "Base" may actually be
|
2007-03-23 22:27:02 +00:00
|
|
|
// in the subscript position. As a result, we need to derive the array base
|
|
|
|
// and index from the expression types.
|
|
|
|
|
2007-04-05 22:36:20 +00:00
|
|
|
QualType baseType, indexType;
|
2007-04-05 21:15:20 +00:00
|
|
|
if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
|
|
|
|
baseType = canonT1;
|
|
|
|
indexType = canonT2;
|
|
|
|
} else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
|
|
|
|
baseType = canonT2;
|
|
|
|
indexType = canonT1;
|
2007-03-23 22:27:02 +00:00
|
|
|
} else
|
|
|
|
return Diag(LLoc, diag::err_typecheck_subscript_value);
|
|
|
|
|
2007-03-28 21:49:40 +00:00
|
|
|
// C99 6.5.2.1p1
|
2007-04-24 00:23:05 +00:00
|
|
|
if (!indexType->isIntegerType())
|
2007-03-23 22:27:02 +00:00
|
|
|
return Diag(LLoc, diag::err_typecheck_subscript);
|
2007-03-28 21:49:40 +00:00
|
|
|
|
2007-03-30 23:47:58 +00:00
|
|
|
// FIXME: need to deal with const...
|
2007-04-05 22:36:20 +00:00
|
|
|
QualType resultType;
|
2007-03-28 21:49:40 +00:00
|
|
|
if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
|
|
|
|
resultType = ary->getElementType();
|
|
|
|
} else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
|
|
|
|
resultType = ary->getPointeeType();
|
|
|
|
// in practice, the following check catches trying to index a pointer
|
|
|
|
// to a function (e.g. void (*)(int)). Functions are not objects in c99.
|
2007-03-30 20:09:34 +00:00
|
|
|
if (!resultType->isObjectType())
|
|
|
|
return Diag(LLoc, diag::err_typecheck_subscript_not_object, baseType);
|
2007-03-28 21:49:40 +00:00
|
|
|
}
|
|
|
|
return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType);
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::ExprResult Sema::
|
|
|
|
ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
|
|
|
|
tok::TokenKind OpKind, SourceLocation MemberLoc,
|
|
|
|
IdentifierInfo &Member) {
|
2007-04-05 22:36:20 +00:00
|
|
|
QualType qualifiedType = ((Expr *)Base)->getType();
|
2007-04-01 01:41:35 +00:00
|
|
|
|
|
|
|
assert(!qualifiedType.isNull() && "no type for member expression");
|
|
|
|
|
2007-04-05 22:36:20 +00:00
|
|
|
QualType canonType = qualifiedType.getCanonicalType();
|
2007-03-23 22:27:02 +00:00
|
|
|
|
|
|
|
if (OpKind == tok::arrow) {
|
2007-04-01 01:41:35 +00:00
|
|
|
if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
|
|
|
|
qualifiedType = PT->getPointeeType();
|
2007-04-05 21:15:20 +00:00
|
|
|
canonType = qualifiedType.getCanonicalType();
|
2007-04-01 01:41:35 +00:00
|
|
|
} else
|
2007-03-23 22:27:02 +00:00
|
|
|
return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
|
|
|
|
}
|
2007-04-02 22:35:25 +00:00
|
|
|
if (!isa<RecordType>(canonType))
|
|
|
|
return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
|
|
|
|
|
|
|
|
// get the struct/union definition from the type.
|
|
|
|
RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
|
2007-03-26 23:09:51 +00:00
|
|
|
|
2007-04-02 22:35:25 +00:00
|
|
|
if (canonType->isIncompleteType())
|
|
|
|
return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
|
2007-03-26 23:09:51 +00:00
|
|
|
|
2007-04-02 22:35:25 +00:00
|
|
|
FieldDecl *MemberDecl = RD->getMember(&Member);
|
|
|
|
if (!MemberDecl)
|
|
|
|
return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
|
|
|
|
|
|
|
|
return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
|
|
|
|
/// This provides the location of the left/right parens and a list of comma
|
|
|
|
/// locations.
|
|
|
|
Action::ExprResult Sema::
|
|
|
|
ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
|
|
|
|
ExprTy **Args, unsigned NumArgs,
|
|
|
|
SourceLocation *CommaLocs, SourceLocation RParenLoc) {
|
2007-04-26 20:39:23 +00:00
|
|
|
QualType qType = ((Expr *)Fn)->getType();
|
|
|
|
|
|
|
|
assert(!qType.isNull() && "no type for function call expression");
|
|
|
|
|
2007-05-03 21:03:48 +00:00
|
|
|
const FunctionType *funcT = dyn_cast<FunctionType>(qType.getCanonicalType());
|
2007-04-26 20:39:23 +00:00
|
|
|
|
2007-05-03 21:03:48 +00:00
|
|
|
assert(funcT && "ParseCallExpr(): not a function type");
|
|
|
|
|
|
|
|
// If a prototype isn't declared, the parser implicitly defines a func decl
|
|
|
|
QualType resultType = funcT->getResultType();
|
|
|
|
|
|
|
|
if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
|
|
|
|
// C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
|
|
|
|
// assignment, to the types of the corresponding parameter, ...
|
|
|
|
|
|
|
|
unsigned NumArgsInProto = proto->getNumArgs();
|
|
|
|
unsigned n = NumArgs;
|
|
|
|
|
|
|
|
if (NumArgs < NumArgsInProto)
|
|
|
|
Diag(LParenLoc, diag::ext_typecheck_call_too_few_args);
|
|
|
|
else if (NumArgs > NumArgsInProto) { // FIXME: check isVariadic()...
|
|
|
|
Diag(LParenLoc, diag::ext_typecheck_call_too_many_args);
|
|
|
|
n = NumArgsInProto;
|
|
|
|
}
|
|
|
|
// Continue to check argument types (even if we have too few/many args).
|
|
|
|
for (unsigned i = 0; i < n; i++) {
|
|
|
|
QualType lhsType = proto->getArgType(i);
|
|
|
|
QualType rhsType = ((Expr **)Args)[i]->getType();
|
|
|
|
|
|
|
|
if (lhsType == rhsType) // common case, fast path...
|
|
|
|
continue;
|
|
|
|
AssignmentConversionResult result;
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
UsualAssignmentConversions(lhsType, rhsType, result);
|
2007-05-03 21:03:48 +00:00
|
|
|
|
|
|
|
SourceLocation l = (i == 0) ? LParenLoc : CommaLocs[i-1];
|
|
|
|
|
|
|
|
// decode the result (notice that AST's are still created for extensions).
|
|
|
|
// FIXME: consider fancier error diagnostics (since this is quite common).
|
|
|
|
// #1: emit the actual prototype arg...requires adding source loc info.
|
|
|
|
// #2: pass Diag the offending argument type...requires hacking Diag.
|
|
|
|
switch (result) {
|
|
|
|
case Compatible:
|
|
|
|
break;
|
|
|
|
case PointerFromInt:
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
// check for null pointer constant (C99 6.3.2.3p3)
|
|
|
|
if (!((Expr **)Args)[i]->isNullPointerConstant())
|
|
|
|
Diag(l, diag::ext_typecheck_passing_pointer_from_int, utostr(i+1));
|
2007-05-03 21:03:48 +00:00
|
|
|
break;
|
|
|
|
case IntFromPointer:
|
|
|
|
Diag(l, diag::ext_typecheck_passing_int_from_pointer, utostr(i+1));
|
|
|
|
break;
|
|
|
|
case IncompatiblePointer:
|
|
|
|
Diag(l, diag::ext_typecheck_passing_incompatible_pointer, utostr(i+1));
|
|
|
|
break;
|
|
|
|
case Incompatible:
|
|
|
|
return Diag(l, diag::err_typecheck_passing_incompatible, utostr(i+1));
|
|
|
|
}
|
|
|
|
}
|
2007-04-26 20:39:23 +00:00
|
|
|
}
|
|
|
|
return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs, resultType);
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::ExprResult Sema::
|
|
|
|
ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
|
|
|
|
SourceLocation RParenLoc, ExprTy *Op) {
|
2006-11-20 04:34:45 +00:00
|
|
|
// If error parsing type, ignore.
|
2007-04-26 20:39:23 +00:00
|
|
|
assert((Ty != 0) && "ParseCastExpr(): missing type");
|
2007-04-05 22:36:20 +00:00
|
|
|
return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op);
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
|
|
|
|
/// in the case of a the GNU conditional expr extension.
|
|
|
|
Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
|
|
|
|
SourceLocation ColonLoc,
|
|
|
|
ExprTy *Cond, ExprTy *LHS,
|
|
|
|
ExprTy *RHS) {
|
2007-04-26 20:39:23 +00:00
|
|
|
QualType lhs = ((Expr *)LHS)->getType();
|
|
|
|
QualType rhs = ((Expr *)RHS)->getType();
|
|
|
|
|
|
|
|
assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
|
|
|
|
assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
|
|
|
|
|
2007-04-27 18:30:00 +00:00
|
|
|
QualType canonType = rhs.getCanonicalType(); // FIXME
|
2007-04-26 20:39:23 +00:00
|
|
|
return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, canonType);
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
2007-04-24 00:23:05 +00:00
|
|
|
/// UsualUnaryConversion - Performs various conversions that are common to most
|
|
|
|
/// operators (C99 6.3). The conversions of array and function types are
|
|
|
|
/// sometimes surpressed. For example, the array->pointer conversion doesn't
|
|
|
|
/// apply if the array is an argument to the sizeof or address (&) operators.
|
|
|
|
/// In these instances, this routine should *not* be called.
|
|
|
|
QualType Sema::UsualUnaryConversion(QualType t) {
|
|
|
|
assert(!t.isNull() && "UsualUnaryConversion - missing type");
|
2007-04-20 22:26:17 +00:00
|
|
|
|
2007-04-24 00:23:05 +00:00
|
|
|
if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
|
|
|
|
return Context.IntTy;
|
|
|
|
else if (t->isFunctionType()) // C99 6.3.2.1p4
|
|
|
|
return Context.getPointerType(t);
|
|
|
|
else if (t->isArrayType()) // C99 6.3.2.1p3
|
|
|
|
return Context.getPointerType(cast<ArrayType>(t)->getElementType());
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// UsualArithmeticConversions - Performs various conversions that are common to
|
|
|
|
/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
|
|
|
|
/// routine returns the first non-arithmetic type found. The client is
|
|
|
|
/// responsible for emitting appropriate error diagnostics.
|
|
|
|
QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
|
2007-04-25 01:22:31 +00:00
|
|
|
QualType lhs = UsualUnaryConversion(t1);
|
|
|
|
QualType rhs = UsualUnaryConversion(t2);
|
2007-04-24 00:23:05 +00:00
|
|
|
|
|
|
|
// if either operand is not of arithmetic type, no conversion is possible.
|
2007-04-25 01:22:31 +00:00
|
|
|
if (!lhs->isArithmeticType())
|
|
|
|
return lhs;
|
2007-04-25 19:01:39 +00:00
|
|
|
if (!rhs->isArithmeticType())
|
2007-04-25 01:22:31 +00:00
|
|
|
return rhs;
|
2007-04-24 00:23:05 +00:00
|
|
|
|
2007-04-25 19:01:39 +00:00
|
|
|
// if both arithmetic types are identical, no conversion is needed.
|
2007-04-25 01:22:31 +00:00
|
|
|
if (lhs == rhs)
|
|
|
|
return lhs;
|
2007-04-24 00:23:05 +00:00
|
|
|
|
2007-04-25 01:22:31 +00:00
|
|
|
// at this point, we have two different arithmetic types.
|
|
|
|
|
|
|
|
// Handle complex types first (C99 6.3.1.8p1).
|
|
|
|
if (lhs->isComplexType() || rhs->isComplexType()) {
|
|
|
|
// if we have an integer operand, the result is the complex type.
|
|
|
|
if (rhs->isIntegerType())
|
|
|
|
return lhs;
|
|
|
|
if (lhs->isIntegerType())
|
|
|
|
return rhs;
|
|
|
|
|
2007-04-27 18:30:00 +00:00
|
|
|
return Context.maxComplexType(lhs, rhs);
|
2007-04-25 01:22:31 +00:00
|
|
|
}
|
|
|
|
// Now handle "real" floating types (i.e. float, double, long double).
|
|
|
|
if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
|
|
|
|
// if we have an integer operand, the result is the real floating type.
|
|
|
|
if (rhs->isIntegerType())
|
|
|
|
return lhs;
|
|
|
|
if (lhs->isIntegerType())
|
|
|
|
return rhs;
|
|
|
|
|
|
|
|
// we have two real floating types, float/complex combos were handled above.
|
2007-04-27 18:30:00 +00:00
|
|
|
return Context.maxFloatingType(lhs, rhs);
|
2007-04-24 20:56:26 +00:00
|
|
|
}
|
2007-04-27 18:30:00 +00:00
|
|
|
return Context.maxIntegerType(lhs, rhs);
|
2007-03-23 22:27:02 +00:00
|
|
|
}
|
|
|
|
|
2007-05-03 21:03:48 +00:00
|
|
|
/// UsualAssignmentConversions (C99 6.5.16) - This routine currently
|
|
|
|
/// has code to accommodate several GCC extensions when type checking
|
|
|
|
/// pointers. Here are some objectionable examples that GCC considers warnings:
|
|
|
|
///
|
|
|
|
/// int a, *pint;
|
|
|
|
/// short *pshort;
|
|
|
|
/// struct foo *pfoo;
|
|
|
|
///
|
|
|
|
/// pint = pshort; // warning: assignment from incompatible pointer type
|
|
|
|
/// a = pint; // warning: assignment makes integer from pointer without a cast
|
|
|
|
/// pint = a; // warning: assignment makes pointer from integer without a cast
|
|
|
|
/// pint = pfoo; // warning: assignment from incompatible pointer type
|
|
|
|
///
|
|
|
|
/// As a result, the code for dealing with pointers is more complex than the
|
|
|
|
/// C99 spec dictates.
|
|
|
|
/// Note: the warning above turn into errors when -pedantic-errors is enabled.
|
|
|
|
///
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType,
|
2007-05-03 21:03:48 +00:00
|
|
|
AssignmentConversionResult &r) {
|
2007-05-02 23:51:10 +00:00
|
|
|
// this check seems unnatural, however it necessary to insure the proper
|
|
|
|
// conversion of functions/arrays. If the conversion where done for all
|
|
|
|
// DeclExpr's (created by ParseIdentifierExpr), it would mess up the
|
|
|
|
// unary expressions that surpress this implicit conversion (&, sizeof).
|
|
|
|
if (rhsType->isFunctionType() || rhsType->isArrayType())
|
|
|
|
rhsType = UsualUnaryConversion(rhsType);
|
|
|
|
|
2007-05-03 21:03:48 +00:00
|
|
|
r = Compatible;
|
2007-05-02 21:58:15 +00:00
|
|
|
if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
|
|
|
|
return lhsType;
|
|
|
|
else if (lhsType->isPointerType()) {
|
|
|
|
if (rhsType->isIntegerType()) {
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
r = PointerFromInt;
|
2007-05-02 23:51:10 +00:00
|
|
|
return rhsType;
|
2007-05-02 21:58:15 +00:00
|
|
|
}
|
|
|
|
// FIXME: make sure the qualifier are matching
|
|
|
|
if (rhsType->isPointerType()) {
|
|
|
|
if (!Type::pointerTypesAreCompatible(lhsType, rhsType))
|
2007-05-03 21:03:48 +00:00
|
|
|
r = IncompatiblePointer;
|
2007-05-02 23:51:10 +00:00
|
|
|
return rhsType;
|
2007-05-02 21:58:15 +00:00
|
|
|
}
|
|
|
|
} else if (rhsType->isPointerType()) {
|
|
|
|
if (lhsType->isIntegerType()) {
|
|
|
|
// C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
|
|
|
|
if (lhsType != Context.BoolTy)
|
2007-05-03 21:03:48 +00:00
|
|
|
r = IntFromPointer;
|
2007-05-02 23:51:10 +00:00
|
|
|
return rhsType;
|
2007-05-02 21:58:15 +00:00
|
|
|
}
|
|
|
|
// - both operands are pointers to qualified or unqualified versions of
|
|
|
|
// compatible types, and the type pointed to by the left has *all* the
|
|
|
|
// qualifiers of the type pointed to by the right;
|
|
|
|
if (lhsType->isPointerType()) {
|
|
|
|
if (!Type::pointerTypesAreCompatible(lhsType, rhsType))
|
2007-05-03 21:03:48 +00:00
|
|
|
r = IncompatiblePointer;
|
2007-05-02 23:51:10 +00:00
|
|
|
return rhsType;
|
2007-05-02 21:58:15 +00:00
|
|
|
}
|
|
|
|
} else if (lhsType->isStructureType() && rhsType->isStructureType()) {
|
|
|
|
if (Type::structureTypesAreCompatible(lhsType, rhsType))
|
2007-05-02 23:51:10 +00:00
|
|
|
return rhsType;
|
2007-05-02 21:58:15 +00:00
|
|
|
} else if (lhsType->isUnionType() && rhsType->isUnionType()) {
|
|
|
|
if (Type::unionTypesAreCompatible(lhsType, rhsType))
|
2007-05-02 23:51:10 +00:00
|
|
|
return rhsType;
|
2007-05-02 21:58:15 +00:00
|
|
|
}
|
2007-05-03 21:03:48 +00:00
|
|
|
r = Incompatible;
|
|
|
|
return QualType();
|
2007-05-02 21:58:15 +00:00
|
|
|
}
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
inline QualType Sema::CheckMultiplyDivideOperands(
|
|
|
|
Expr *lex, Expr *rex, SourceLocation loc)
|
2007-04-20 23:42:24 +00:00
|
|
|
{
|
2007-04-24 00:23:05 +00:00
|
|
|
QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
|
2007-04-20 23:42:24 +00:00
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
if (resType->isArithmeticType())
|
|
|
|
return resType;
|
|
|
|
Diag(loc, diag::err_typecheck_invalid_operands);
|
|
|
|
return QualType();
|
2007-03-30 23:47:58 +00:00
|
|
|
}
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
inline QualType Sema::CheckRemainderOperands(
|
|
|
|
Expr *lex, Expr *rex, SourceLocation loc)
|
|
|
|
{
|
|
|
|
QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
|
|
|
|
|
|
|
|
if (resType->isIntegerType())
|
|
|
|
return resType;
|
|
|
|
Diag(loc, diag::err_typecheck_invalid_operands);
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
|
|
|
|
Expr *lex, Expr *rex, SourceLocation loc)
|
2007-04-24 00:23:05 +00:00
|
|
|
{
|
2007-04-27 18:30:00 +00:00
|
|
|
QualType lhsType = lex->getType(), rhsType = rex->getType();
|
|
|
|
QualType resType = UsualArithmeticConversions(lhsType, rhsType);
|
|
|
|
|
|
|
|
// handle the common case first (both operands are arithmetic).
|
|
|
|
if (resType->isArithmeticType())
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
return resType;
|
|
|
|
|
|
|
|
if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
|
|
|
|
(lhsType->isIntegerType() && rhsType->isPointerType()))
|
|
|
|
return resType;
|
|
|
|
Diag(loc, diag::err_typecheck_invalid_operands);
|
|
|
|
return QualType();
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
|
|
|
|
Expr *lex, Expr *rex, SourceLocation loc)
|
2007-04-24 00:23:05 +00:00
|
|
|
{
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
QualType lhsType = lex->getType(), rhsType = rex->getType();
|
|
|
|
QualType resType = UsualArithmeticConversions(lhsType, rhsType);
|
2007-04-24 00:23:05 +00:00
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
// handle the common case first (both operands are arithmetic).
|
|
|
|
if (resType->isArithmeticType())
|
|
|
|
return resType;
|
|
|
|
if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
|
|
|
|
(lhsType->isPointerType() && rhsType->isPointerType()))
|
|
|
|
return resType;
|
|
|
|
Diag(loc, diag::err_typecheck_invalid_operands);
|
|
|
|
return QualType();
|
|
|
|
}
|
2007-04-24 00:23:05 +00:00
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
inline QualType Sema::CheckShiftOperands( // C99 6.5.7
|
|
|
|
Expr *lex, Expr *rex, SourceLocation loc)
|
|
|
|
{
|
|
|
|
QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
|
|
|
|
|
|
|
|
if (resType->isIntegerType())
|
|
|
|
return resType;
|
|
|
|
Diag(loc, diag::err_typecheck_invalid_operands);
|
|
|
|
return QualType();
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
|
|
|
|
Expr *lex, Expr *rex, SourceLocation loc)
|
2007-04-24 00:23:05 +00:00
|
|
|
{
|
|
|
|
QualType lType = lex->getType(), rType = rex->getType();
|
|
|
|
|
|
|
|
if (lType->isRealType() && rType->isRealType())
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
return Context.IntTy;
|
2007-04-27 18:30:00 +00:00
|
|
|
|
|
|
|
if (lType->isPointerType() && rType->isPointerType())
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
return Context.IntTy;
|
2007-04-27 18:30:00 +00:00
|
|
|
|
|
|
|
if (lType->isIntegerType() || rType->isIntegerType()) // GCC extension.
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
|
|
|
|
else
|
|
|
|
Diag(loc, diag::err_typecheck_invalid_operands);
|
|
|
|
return QualType();
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
|
|
|
|
Expr *lex, Expr *rex, SourceLocation loc)
|
2007-04-24 00:23:05 +00:00
|
|
|
{
|
|
|
|
QualType lType = lex->getType(), rType = rex->getType();
|
|
|
|
|
|
|
|
if (lType->isArithmeticType() && rType->isArithmeticType())
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
return Context.IntTy;
|
2007-04-27 18:30:00 +00:00
|
|
|
if (lType->isPointerType() && rType->isPointerType())
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
return Context.IntTy;
|
|
|
|
|
2007-04-27 18:30:00 +00:00
|
|
|
if (lType->isIntegerType() || rType->isIntegerType()) // GCC extension.
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
|
|
|
|
else
|
|
|
|
Diag(loc, diag::err_typecheck_invalid_operands);
|
|
|
|
return QualType();
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
inline QualType Sema::CheckBitwiseOperands(
|
|
|
|
Expr *lex, Expr *rex, SourceLocation loc)
|
2007-04-24 00:23:05 +00:00
|
|
|
{
|
|
|
|
QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
if (resType->isIntegerType())
|
|
|
|
return resType;
|
|
|
|
Diag(loc, diag::err_typecheck_invalid_operands);
|
|
|
|
return QualType();
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
|
|
|
|
Expr *lex, Expr *rex, SourceLocation loc)
|
2007-04-24 00:23:05 +00:00
|
|
|
{
|
2007-04-27 18:30:00 +00:00
|
|
|
QualType lhsType = UsualUnaryConversion(lex->getType());
|
|
|
|
QualType rhsType = UsualUnaryConversion(rex->getType());
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
if (lhsType->isScalarType() || rhsType->isScalarType())
|
|
|
|
return Context.IntTy;
|
|
|
|
Diag(loc, diag::err_typecheck_invalid_operands);
|
|
|
|
return QualType();
|
2007-04-26 20:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-05-07 00:24:15 +00:00
|
|
|
inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
|
|
|
|
Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
|
2007-04-26 20:39:23 +00:00
|
|
|
{
|
2007-04-27 21:51:21 +00:00
|
|
|
QualType lhsType = lex->getType();
|
2007-05-07 00:24:15 +00:00
|
|
|
QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
|
2007-04-27 21:51:21 +00:00
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
// FIXME: consider hacking isModifiableLvalue to return an enum that
|
|
|
|
// communicates why the expression/type wasn't a modifiableLvalue.
|
|
|
|
|
|
|
|
// this check is done first to give a more precise diagnostic.
|
|
|
|
if (lhsType.isConstQualified()) {
|
|
|
|
Diag(loc, diag::err_typecheck_assign_const);
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
if (!lex->isModifiableLvalue()) { // this includes checking for "const"
|
|
|
|
Diag(loc, diag::ext_typecheck_assign_non_lvalue);
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
if (lhsType == rhsType) // common case, fast path...
|
|
|
|
return lhsType;
|
|
|
|
|
|
|
|
AssignmentConversionResult result;
|
|
|
|
QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
|
|
|
|
|
|
|
|
// decode the result (notice that extensions still return a type).
|
|
|
|
switch (result) {
|
|
|
|
case Compatible:
|
|
|
|
return resType;
|
|
|
|
case Incompatible:
|
|
|
|
Diag(loc, diag::err_typecheck_assign_incompatible);
|
|
|
|
return QualType();
|
|
|
|
case PointerFromInt:
|
|
|
|
// check for null pointer constant (C99 6.3.2.3p3)
|
2007-05-07 00:24:15 +00:00
|
|
|
if (compoundType.isNull() && !rex->isNullPointerConstant())
|
2007-05-03 21:03:48 +00:00
|
|
|
Diag(loc, diag::ext_typecheck_assign_pointer_from_int);
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
return resType;
|
|
|
|
case IntFromPointer:
|
|
|
|
Diag(loc, diag::ext_typecheck_assign_int_from_pointer);
|
|
|
|
return resType;
|
|
|
|
case IncompatiblePointer:
|
|
|
|
Diag(loc, diag::ext_typecheck_assign_incompatible_pointer);
|
|
|
|
return resType;
|
|
|
|
}
|
|
|
|
assert(0 && "should never get here");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QualType Sema::CheckCommaOperands( // C99 6.5.17
|
2007-04-26 20:39:23 +00:00
|
|
|
Expr *lex, Expr *rex, SourceLocation loc)
|
|
|
|
{
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
return UsualUnaryConversion(rex->getType());
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
2007-05-07 00:24:15 +00:00
|
|
|
QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
|
|
|
|
QualType resType = UsualArithmeticConversions(op->getType(),
|
|
|
|
constantOne->getType());
|
|
|
|
assert(!resType.isNull() && "no type for increment/decrement expression");
|
2007-04-05 21:15:20 +00:00
|
|
|
|
2007-04-03 23:13:13 +00:00
|
|
|
// C99 6.5.2.4p1
|
2007-05-07 00:24:15 +00:00
|
|
|
if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
|
|
|
|
if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
|
|
|
|
Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, resType);
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
} else if (!resType->isRealType()) {
|
2007-03-30 23:47:58 +00:00
|
|
|
// FIXME: Allow Complex as a GCC extension.
|
2007-05-07 00:24:15 +00:00
|
|
|
Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, resType);
|
|
|
|
return QualType();
|
2007-04-03 23:13:13 +00:00
|
|
|
}
|
2007-03-30 23:47:58 +00:00
|
|
|
// At this point, we know we have a real or pointer type. As a result, the
|
|
|
|
// following predicate is overkill (i.e. it will check for types we know we
|
|
|
|
// don't have in this context). Nevertheless, we model the C99 spec closely.
|
2007-05-07 00:24:15 +00:00
|
|
|
if (!resType.isModifiableLvalue()) {
|
|
|
|
Diag(OpLoc, diag::err_typecheck_not_modifiable, resType);
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
return resType;
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
2007-04-24 00:23:05 +00:00
|
|
|
/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
|
2007-04-19 23:00:49 +00:00
|
|
|
/// This routine allows us to typecheck complex/recursive expressions
|
|
|
|
/// where the declaration is needed for type checking. Here are some
|
|
|
|
/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
|
2007-04-24 00:23:05 +00:00
|
|
|
static Decl *getPrimaryDeclaration(Expr *e) {
|
2007-04-19 23:00:49 +00:00
|
|
|
switch (e->getStmtClass()) {
|
|
|
|
case Stmt::DeclRefExprClass:
|
|
|
|
return cast<DeclRefExpr>(e)->getDecl();
|
|
|
|
case Stmt::MemberExprClass:
|
2007-04-24 00:23:05 +00:00
|
|
|
return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
|
2007-04-19 23:00:49 +00:00
|
|
|
case Stmt::ArraySubscriptExprClass:
|
2007-04-24 00:23:05 +00:00
|
|
|
return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
|
2007-04-19 23:00:49 +00:00
|
|
|
case Stmt::CallExprClass:
|
2007-04-24 00:23:05 +00:00
|
|
|
return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
|
2007-04-19 23:00:49 +00:00
|
|
|
case Stmt::UnaryOperatorClass:
|
2007-04-24 00:23:05 +00:00
|
|
|
return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
|
2007-04-19 23:00:49 +00:00
|
|
|
case Stmt::ParenExprClass:
|
2007-04-24 00:23:05 +00:00
|
|
|
return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
|
2007-04-19 23:00:49 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckAddressOfOperand - The operand of & must be either a function
|
|
|
|
/// designator or an lvalue designating an object. If it is an lvalue, the
|
|
|
|
/// object cannot be declared with storage class register or be a bit field.
|
|
|
|
/// Note: The usual conversions are *not* applied to the operand of the &
|
|
|
|
/// operator, and its result is never an lvalue.
|
2007-05-07 00:24:15 +00:00
|
|
|
QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
|
2007-04-24 00:23:05 +00:00
|
|
|
Decl *dcl = getPrimaryDeclaration(op);
|
2007-04-19 23:00:49 +00:00
|
|
|
|
2007-05-03 21:03:48 +00:00
|
|
|
if (!op->isModifiableLvalue()) {
|
2007-04-19 23:00:49 +00:00
|
|
|
if (dcl && isa<FunctionDecl>(dcl))
|
|
|
|
; // C99 6.5.3.2p1: Allow function designators.
|
2007-05-07 00:24:15 +00:00
|
|
|
else {
|
|
|
|
Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof);
|
|
|
|
return QualType();
|
|
|
|
}
|
2007-04-19 23:00:49 +00:00
|
|
|
} else if (dcl) {
|
|
|
|
// We have an lvalue with a decl. Make sure the decl is not declared
|
|
|
|
// with the register storage-class specifier.
|
|
|
|
if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
|
2007-05-07 00:24:15 +00:00
|
|
|
if (vd->getStorageClass() == VarDecl::Register) {
|
|
|
|
Diag(OpLoc, diag::err_typecheck_address_of_register);
|
|
|
|
return QualType();
|
|
|
|
}
|
2007-04-25 19:01:39 +00:00
|
|
|
} else
|
|
|
|
assert(0 && "Unknown/unexpected decl type");
|
|
|
|
|
2007-04-19 23:00:49 +00:00
|
|
|
// FIXME: add check for bitfields!
|
|
|
|
}
|
|
|
|
// If the operand has type "type", the result has type "pointer to type".
|
2007-05-07 00:24:15 +00:00
|
|
|
return Context.getPointerType(op->getType());
|
2007-04-19 23:00:49 +00:00
|
|
|
}
|
|
|
|
|
2007-05-07 00:24:15 +00:00
|
|
|
QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
|
|
|
|
QualType qType = UsualUnaryConversion(op->getType());
|
2007-04-19 23:00:49 +00:00
|
|
|
|
2007-05-07 00:24:15 +00:00
|
|
|
assert(!qType.isNull() && "no type for * expression");
|
2007-04-24 00:23:05 +00:00
|
|
|
|
2007-05-07 00:24:15 +00:00
|
|
|
if (PointerType *PT = dyn_cast<PointerType>(qType))
|
|
|
|
return PT->getPointeeType();
|
|
|
|
Diag(OpLoc, diag::err_typecheck_unary_expr, qType);
|
|
|
|
return QualType();
|
2007-04-24 00:23:05 +00:00
|
|
|
}
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
|
|
|
|
static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
|
|
|
|
tok::TokenKind Kind) {
|
|
|
|
BinaryOperator::Opcode Opc;
|
|
|
|
switch (Kind) {
|
|
|
|
default: assert(0 && "Unknown binop!");
|
|
|
|
case tok::star: Opc = BinaryOperator::Mul; break;
|
|
|
|
case tok::slash: Opc = BinaryOperator::Div; break;
|
|
|
|
case tok::percent: Opc = BinaryOperator::Rem; break;
|
|
|
|
case tok::plus: Opc = BinaryOperator::Add; break;
|
|
|
|
case tok::minus: Opc = BinaryOperator::Sub; break;
|
|
|
|
case tok::lessless: Opc = BinaryOperator::Shl; break;
|
|
|
|
case tok::greatergreater: Opc = BinaryOperator::Shr; break;
|
|
|
|
case tok::lessequal: Opc = BinaryOperator::LE; break;
|
|
|
|
case tok::less: Opc = BinaryOperator::LT; break;
|
|
|
|
case tok::greaterequal: Opc = BinaryOperator::GE; break;
|
|
|
|
case tok::greater: Opc = BinaryOperator::GT; break;
|
|
|
|
case tok::exclaimequal: Opc = BinaryOperator::NE; break;
|
|
|
|
case tok::equalequal: Opc = BinaryOperator::EQ; break;
|
|
|
|
case tok::amp: Opc = BinaryOperator::And; break;
|
|
|
|
case tok::caret: Opc = BinaryOperator::Xor; break;
|
|
|
|
case tok::pipe: Opc = BinaryOperator::Or; break;
|
|
|
|
case tok::ampamp: Opc = BinaryOperator::LAnd; break;
|
|
|
|
case tok::pipepipe: Opc = BinaryOperator::LOr; break;
|
|
|
|
case tok::equal: Opc = BinaryOperator::Assign; break;
|
|
|
|
case tok::starequal: Opc = BinaryOperator::MulAssign; break;
|
|
|
|
case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
|
|
|
|
case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
|
|
|
|
case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
|
|
|
|
case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
|
|
|
|
case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
|
|
|
|
case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
|
|
|
|
case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
|
|
|
|
case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
|
|
|
|
case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
|
|
|
|
case tok::comma: Opc = BinaryOperator::Comma; break;
|
|
|
|
}
|
|
|
|
return Opc;
|
|
|
|
}
|
|
|
|
|
2007-05-07 00:24:15 +00:00
|
|
|
static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
|
|
|
|
tok::TokenKind Kind) {
|
|
|
|
UnaryOperator::Opcode Opc;
|
|
|
|
switch (Kind) {
|
|
|
|
default: assert(0 && "Unknown unary op!");
|
|
|
|
case tok::plusplus: Opc = UnaryOperator::PreInc; break;
|
|
|
|
case tok::minusminus: Opc = UnaryOperator::PreDec; break;
|
|
|
|
case tok::amp: Opc = UnaryOperator::AddrOf; break;
|
|
|
|
case tok::star: Opc = UnaryOperator::Deref; break;
|
|
|
|
case tok::plus: Opc = UnaryOperator::Plus; break;
|
|
|
|
case tok::minus: Opc = UnaryOperator::Minus; break;
|
|
|
|
case tok::tilde: Opc = UnaryOperator::Not; break;
|
|
|
|
case tok::exclaim: Opc = UnaryOperator::LNot; break;
|
|
|
|
case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
|
|
|
|
case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
|
|
|
|
case tok::kw___real: Opc = UnaryOperator::Real; break;
|
|
|
|
case tok::kw___imag: Opc = UnaryOperator::Imag; break;
|
|
|
|
case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
|
|
|
|
// FIXME: case tok::kw___extension__:
|
|
|
|
}
|
|
|
|
return Opc;
|
|
|
|
}
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
// Binary Operators. 'Tok' is the token for the operator.
|
|
|
|
Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
|
|
|
|
ExprTy *LHS, ExprTy *RHS) {
|
|
|
|
BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
|
|
|
|
Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
|
|
|
|
|
|
|
|
assert((lhs != 0) && "ParseBinOp(): missing left expression");
|
|
|
|
assert((rhs != 0) && "ParseBinOp(): missing right expression");
|
|
|
|
|
|
|
|
QualType result;
|
|
|
|
|
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown binary expr!");
|
|
|
|
case BinaryOperator::Assign:
|
2007-05-07 00:24:15 +00:00
|
|
|
result = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
break;
|
|
|
|
case BinaryOperator::Mul:
|
|
|
|
case BinaryOperator::Div:
|
|
|
|
result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
|
|
|
|
break;
|
|
|
|
case BinaryOperator::Rem:
|
|
|
|
result = CheckRemainderOperands(lhs, rhs, TokLoc);
|
|
|
|
break;
|
|
|
|
case BinaryOperator::Add:
|
|
|
|
result = CheckAdditionOperands(lhs, rhs, TokLoc);
|
|
|
|
break;
|
|
|
|
case BinaryOperator::Sub:
|
|
|
|
result = CheckSubtractionOperands(lhs, rhs, TokLoc);
|
|
|
|
break;
|
|
|
|
case BinaryOperator::Shl:
|
|
|
|
case BinaryOperator::Shr:
|
|
|
|
result = CheckShiftOperands(lhs, rhs, TokLoc);
|
|
|
|
break;
|
|
|
|
case BinaryOperator::LE:
|
|
|
|
case BinaryOperator::LT:
|
|
|
|
case BinaryOperator::GE:
|
|
|
|
case BinaryOperator::GT:
|
|
|
|
result = CheckRelationalOperands(lhs, rhs, TokLoc);
|
|
|
|
break;
|
|
|
|
case BinaryOperator::EQ:
|
|
|
|
case BinaryOperator::NE:
|
|
|
|
result = CheckEqualityOperands(lhs, rhs, TokLoc);
|
|
|
|
break;
|
|
|
|
case BinaryOperator::And:
|
|
|
|
case BinaryOperator::Xor:
|
|
|
|
case BinaryOperator::Or:
|
|
|
|
result = CheckBitwiseOperands(lhs, rhs, TokLoc);
|
|
|
|
break;
|
|
|
|
case BinaryOperator::LAnd:
|
|
|
|
case BinaryOperator::LOr:
|
|
|
|
result = CheckLogicalOperands(lhs, rhs, TokLoc);
|
|
|
|
break;
|
|
|
|
case BinaryOperator::MulAssign:
|
|
|
|
case BinaryOperator::DivAssign:
|
|
|
|
result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
|
|
|
|
if (result.isNull())
|
|
|
|
return true;
|
2007-05-07 00:24:15 +00:00
|
|
|
result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
break;
|
|
|
|
case BinaryOperator::RemAssign:
|
|
|
|
result = CheckRemainderOperands(lhs, rhs, TokLoc);
|
|
|
|
if (result.isNull())
|
|
|
|
return true;
|
2007-05-07 00:24:15 +00:00
|
|
|
result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
break;
|
|
|
|
case BinaryOperator::AddAssign:
|
|
|
|
result = CheckAdditionOperands(lhs, rhs, TokLoc);
|
|
|
|
if (result.isNull())
|
|
|
|
return true;
|
2007-05-07 00:24:15 +00:00
|
|
|
result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
break;
|
|
|
|
case BinaryOperator::SubAssign:
|
|
|
|
result = CheckSubtractionOperands(lhs, rhs, TokLoc);
|
|
|
|
if (result.isNull())
|
|
|
|
return true;
|
2007-05-07 00:24:15 +00:00
|
|
|
result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
break;
|
|
|
|
case BinaryOperator::ShlAssign:
|
|
|
|
case BinaryOperator::ShrAssign:
|
|
|
|
result = CheckShiftOperands(lhs, rhs, TokLoc);
|
|
|
|
if (result.isNull())
|
|
|
|
return true;
|
2007-05-07 00:24:15 +00:00
|
|
|
result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
break;
|
|
|
|
case BinaryOperator::AndAssign:
|
|
|
|
case BinaryOperator::XorAssign:
|
|
|
|
case BinaryOperator::OrAssign:
|
|
|
|
result = CheckBitwiseOperands(lhs, rhs, TokLoc);
|
|
|
|
if (result.isNull())
|
|
|
|
return true;
|
2007-05-07 00:24:15 +00:00
|
|
|
result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
break;
|
|
|
|
case BinaryOperator::Comma:
|
|
|
|
result = CheckCommaOperands(lhs, rhs, TokLoc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (result.isNull())
|
|
|
|
return true;
|
|
|
|
return new BinaryOperator(lhs, rhs, Opc, result);
|
|
|
|
}
|
|
|
|
|
2007-05-07 00:24:15 +00:00
|
|
|
// Unary Operators. 'Tok' is the token for the operator.
|
|
|
|
Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
|
|
|
|
ExprTy *Input) {
|
|
|
|
UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
|
|
|
|
QualType resultType;
|
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Unimplemented unary expr!");
|
|
|
|
case UnaryOperator::PreInc:
|
|
|
|
case UnaryOperator::PreDec:
|
|
|
|
resultType = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
|
|
|
|
break;
|
|
|
|
case UnaryOperator::AddrOf:
|
|
|
|
resultType = CheckAddressOfOperand((Expr *)Input, OpLoc);
|
|
|
|
break;
|
|
|
|
case UnaryOperator::Deref:
|
|
|
|
resultType = CheckIndirectionOperand((Expr *)Input, OpLoc);
|
|
|
|
break;
|
|
|
|
case UnaryOperator::Plus:
|
|
|
|
case UnaryOperator::Minus:
|
|
|
|
resultType = UsualUnaryConversion(((Expr *)Input)->getType());
|
|
|
|
if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
|
|
|
|
return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
|
|
|
|
break;
|
|
|
|
case UnaryOperator::Not: // bitwise complement
|
|
|
|
resultType = UsualUnaryConversion(((Expr *)Input)->getType());
|
|
|
|
if (!resultType->isIntegerType()) // C99 6.5.3.3p1
|
|
|
|
return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
|
|
|
|
break;
|
|
|
|
case UnaryOperator::LNot: // logical negation
|
|
|
|
resultType = UsualUnaryConversion(((Expr *)Input)->getType());
|
|
|
|
if (!resultType->isScalarType()) // C99 6.5.3.3p1
|
|
|
|
return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
|
|
|
|
break;
|
|
|
|
case UnaryOperator::SizeOf:
|
|
|
|
case UnaryOperator::AlignOf:
|
|
|
|
assert(0 && "need to implement type checking for sizeof/alignof");
|
|
|
|
}
|
|
|
|
if (resultType.isNull())
|
|
|
|
return true;
|
|
|
|
return new UnaryOperator((Expr *)Input, Opc, resultType);
|
|
|
|
}
|