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"
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
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
|
|
|
|
TypeRef t = Context.getPointerType(Context.CharTy);
|
|
|
|
|
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-03-23 22:27:02 +00:00
|
|
|
if (ObjectDecl *OD = dyn_cast<ObjectDecl>(D)) {
|
|
|
|
return new DeclRefExpr(OD);
|
|
|
|
} else if (isa<TypedefDecl>(D))
|
|
|
|
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!");
|
|
|
|
case tok::char_constant: // constant: character-constant
|
2006-11-20 04:58:19 +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-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()) {
|
|
|
|
TypeRef t;
|
|
|
|
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) {
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Unary Operators. 'Tok' is the token for the operator.
|
|
|
|
Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
|
|
|
|
ExprTy *Input) {
|
|
|
|
UnaryOperator::Opcode Opc;
|
|
|
|
switch (Op) {
|
|
|
|
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;
|
|
|
|
case tok::kw___extension__:
|
|
|
|
return Input;
|
|
|
|
//Opc = UnaryOperator::Extension;
|
|
|
|
//break;
|
|
|
|
}
|
2007-03-30 23:47:58 +00:00
|
|
|
if (Opc == UnaryOperator::PreInc || Opc == UnaryOperator::PreDec)
|
|
|
|
return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc);
|
|
|
|
|
|
|
|
// when all the check functions are written, this will go away...
|
|
|
|
return new UnaryOperator((Expr*)Input, Opc, 0);
|
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.
|
|
|
|
TypeRef ArgTy = TypeRef::getFromOpaquePtr(Ty);
|
|
|
|
|
|
|
|
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-03-30 23:47:58 +00:00
|
|
|
return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc);
|
2006-11-10 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::ExprResult Sema::
|
|
|
|
ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
|
|
|
|
ExprTy *Idx, SourceLocation RLoc) {
|
2007-03-23 22:27:02 +00:00
|
|
|
TypeRef t1 = ((Expr *)Base)->getTypeRef();
|
|
|
|
TypeRef t2 = ((Expr *)Idx)->getTypeRef();
|
|
|
|
|
|
|
|
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-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.
|
|
|
|
|
|
|
|
TypeRef baseType, indexType;
|
|
|
|
if (isa<ArrayType>(t1) || isa<PointerType>(t1)) {
|
|
|
|
baseType = t1;
|
|
|
|
indexType = t2;
|
|
|
|
} else if (isa<ArrayType>(t2) || isa<PointerType>(t2)) { // uncommon case
|
|
|
|
baseType = t2;
|
|
|
|
indexType = t1;
|
|
|
|
} else
|
|
|
|
return Diag(LLoc, diag::err_typecheck_subscript_value);
|
|
|
|
|
2007-03-28 21:49:40 +00:00
|
|
|
// C99 6.5.2.1p1
|
|
|
|
if (!indexType->isIntegralType())
|
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-03-28 21:49:40 +00:00
|
|
|
TypeRef resultType;
|
|
|
|
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-01 01:41:35 +00:00
|
|
|
TypeRef qualifiedType = ((Expr *)Base)->getTypeRef();
|
|
|
|
|
|
|
|
assert(!qualifiedType.isNull() && "no type for member expression");
|
|
|
|
|
|
|
|
Type *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-02 22:35:25 +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) {
|
|
|
|
return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (Ty == 0) return true;
|
|
|
|
return new CastExpr(TypeRef::getFromOpaquePtr(Ty), (Expr*)Op);
|
2006-11-10 05:29:30 +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;
|
|
|
|
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;
|
|
|
|
}
|
2007-03-23 22:27:02 +00:00
|
|
|
|
|
|
|
// perform implicit conversions (C99 6.3)
|
|
|
|
Expr *e1 = ImplicitConversion((Expr*)LHS);
|
|
|
|
Expr *e2 = ImplicitConversion((Expr*)RHS);
|
2006-11-10 05:29:30 +00:00
|
|
|
|
2007-03-21 21:08:52 +00:00
|
|
|
if (BinaryOperator::isMultiplicativeOp(Opc))
|
|
|
|
CheckMultiplicativeOperands((Expr*)LHS, (Expr*)RHS);
|
|
|
|
else if (BinaryOperator::isAdditiveOp(Opc))
|
|
|
|
CheckAdditiveOperands((Expr*)LHS, (Expr*)RHS);
|
|
|
|
else if (BinaryOperator::isShiftOp(Opc))
|
|
|
|
CheckShiftOperands((Expr*)LHS, (Expr*)RHS);
|
|
|
|
else if (BinaryOperator::isRelationalOp(Opc))
|
|
|
|
CheckRelationalOperands((Expr*)LHS, (Expr*)RHS);
|
|
|
|
else if (BinaryOperator::isEqualityOp(Opc))
|
|
|
|
CheckEqualityOperands((Expr*)LHS, (Expr*)RHS);
|
|
|
|
else if (BinaryOperator::isBitwiseOp(Opc))
|
|
|
|
CheckBitwiseOperands((Expr*)LHS, (Expr*)RHS);
|
|
|
|
else if (BinaryOperator::isLogicalOp(Opc))
|
|
|
|
CheckLogicalOperands((Expr*)LHS, (Expr*)RHS);
|
|
|
|
|
2006-11-10 05:29:30 +00:00
|
|
|
return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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) {
|
|
|
|
return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
|
|
|
|
}
|
|
|
|
|
2007-03-23 22:27:02 +00:00
|
|
|
Expr *Sema::ImplicitConversion(Expr *E) {
|
|
|
|
#if 0
|
|
|
|
TypeRef t = E->getTypeRef();
|
|
|
|
if (t != 0) t.dump();
|
|
|
|
else printf("no type for expr %s\n", E->getStmtClassName());
|
|
|
|
#endif
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
2007-03-30 23:47:58 +00:00
|
|
|
Action::ExprResult Sema::CheckMultiplicativeOperands(Expr *op1, Expr *op2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Action::ExprResult Sema::CheckAdditiveOperands(Expr *op1, Expr *op2) {
|
|
|
|
return false;
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 23:47:58 +00:00
|
|
|
Action::ExprResult Sema::CheckShiftOperands(Expr *op1, Expr *op2) {
|
|
|
|
return false;
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 23:47:58 +00:00
|
|
|
Action::ExprResult Sema::CheckRelationalOperands(Expr *op1, Expr *op2) {
|
|
|
|
return false;
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 23:47:58 +00:00
|
|
|
Action::ExprResult Sema::CheckEqualityOperands(Expr *op1, Expr *op2) {
|
|
|
|
return false;
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 23:47:58 +00:00
|
|
|
Action::ExprResult Sema::CheckBitwiseOperands(Expr *op1, Expr *op2) {
|
|
|
|
return false;
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 23:47:58 +00:00
|
|
|
Action::ExprResult Sema::CheckLogicalOperands(Expr *op1, Expr *op2) {
|
|
|
|
return false;
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 23:47:58 +00:00
|
|
|
Action::ExprResult
|
|
|
|
Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc,
|
|
|
|
UnaryOperator::Opcode OpCode) {
|
|
|
|
TypeRef type = op->getTypeRef();
|
|
|
|
|
|
|
|
assert(!type.isNull() && "no type for increment/decrement expression");
|
|
|
|
|
|
|
|
if (const PointerType *pt = dyn_cast<PointerType>(type)) {
|
|
|
|
if (!pt->getPointeeType()->isObjectType()) // C99 6.5.6p2
|
|
|
|
return Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, type);
|
|
|
|
} else if (!type->isRealType()) // C99 6.5.2.4: isRealType excludes complex.
|
|
|
|
// FIXME: Allow Complex as a GCC extension.
|
|
|
|
return Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, type);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (!type.isModifiableLvalue())
|
|
|
|
return Diag(OpLoc, diag::err_typecheck_not_modifiable, type);
|
|
|
|
|
|
|
|
return new UnaryOperator(op, OpCode, type);
|
2007-03-21 21:08:52 +00:00
|
|
|
}
|
|
|
|
|