blob: 17f474c19486cea7466e5e267318b8019fc13efa [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000016#include "clang/AST/StmtVisitor.h"
Chris Lattnere165d942006-08-24 04:40:38 +000017#include "clang/Lex/IdentifierTable.h"
Chris Lattner1b926492006-08-23 06:42:10 +000018using namespace clang;
19
Chris Lattner0eedafe2006-08-24 04:56:27 +000020//===----------------------------------------------------------------------===//
21// Primary Expressions.
22//===----------------------------------------------------------------------===//
23
Steve Naroff408451b2007-02-26 22:17:12 +000024StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
Steve Naroff53f07dc2007-05-17 21:49:33 +000025 bool Wide, QualType t, SourceLocation firstLoc,
26 SourceLocation lastLoc) :
Steve Narofff1e53692007-03-23 22:27:02 +000027 Expr(StringLiteralClass, t) {
Steve Naroffdf7855b2007-02-21 23:46:25 +000028 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerd3e98952006-10-06 05:22:26 +000029 char *AStrData = new char[byteLength];
30 memcpy(AStrData, strData, byteLength);
31 StrData = AStrData;
32 ByteLength = byteLength;
Chris Lattner882f7882006-11-04 18:52:07 +000033 IsWide = Wide;
Steve Naroff53f07dc2007-05-17 21:49:33 +000034 firstTokLoc = firstLoc;
35 lastTokLoc = lastLoc;
Chris Lattnerd3e98952006-10-06 05:22:26 +000036}
37
Steve Naroffdf7855b2007-02-21 23:46:25 +000038StringLiteral::~StringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +000039 delete[] StrData;
40}
41
Chris Lattner15768702006-11-05 23:54:51 +000042bool UnaryOperator::isPostfix(Opcode Op) {
43 switch (Op) {
44 case PostInc:
45 case PostDec:
46 return true;
47 default:
48 return false;
49 }
50}
51
Chris Lattner1b926492006-08-23 06:42:10 +000052/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
53/// corresponds to, e.g. "sizeof" or "[pre]++".
54const char *UnaryOperator::getOpcodeStr(Opcode Op) {
55 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +000056 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +000057 case PostInc: return "++";
58 case PostDec: return "--";
59 case PreInc: return "++";
60 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +000061 case AddrOf: return "&";
62 case Deref: return "*";
63 case Plus: return "+";
64 case Minus: return "-";
65 case Not: return "~";
66 case LNot: return "!";
67 case Real: return "__real";
68 case Imag: return "__imag";
Chris Lattner26115ac2006-08-24 06:10:04 +000069 case SizeOf: return "sizeof";
70 case AlignOf: return "alignof";
Chris Lattnerc52b1182006-10-25 05:45:55 +000071 case Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +000072 }
73}
74
Chris Lattner0eedafe2006-08-24 04:56:27 +000075//===----------------------------------------------------------------------===//
76// Postfix Operators.
77//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +000078
Steve Naroff509fe022007-05-17 01:16:00 +000079CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
Chris Lattner9b3b9a12007-06-27 06:08:24 +000080 SourceLocation rparenloc)
Steve Naroffae4143e2007-04-26 20:39:23 +000081 : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) {
Chris Lattnere165d942006-08-24 04:40:38 +000082 Args = new Expr*[numargs];
83 for (unsigned i = 0; i != numargs; ++i)
84 Args[i] = args[i];
Chris Lattner9b3b9a12007-06-27 06:08:24 +000085 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +000086}
87
Chris Lattner1b926492006-08-23 06:42:10 +000088/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
89/// corresponds to, e.g. "<<=".
90const char *BinaryOperator::getOpcodeStr(Opcode Op) {
91 switch (Op) {
92 default: assert(0 && "Unknown binary operator");
93 case Mul: return "*";
94 case Div: return "/";
95 case Rem: return "%";
96 case Add: return "+";
97 case Sub: return "-";
98 case Shl: return "<<";
99 case Shr: return ">>";
100 case LT: return "<";
101 case GT: return ">";
102 case LE: return "<=";
103 case GE: return ">=";
104 case EQ: return "==";
105 case NE: return "!=";
106 case And: return "&";
107 case Xor: return "^";
108 case Or: return "|";
109 case LAnd: return "&&";
110 case LOr: return "||";
111 case Assign: return "=";
112 case MulAssign: return "*=";
113 case DivAssign: return "/=";
114 case RemAssign: return "%=";
115 case AddAssign: return "+=";
116 case SubAssign: return "-=";
117 case ShlAssign: return "<<=";
118 case ShrAssign: return ">>=";
119 case AndAssign: return "&=";
120 case XorAssign: return "^=";
121 case OrAssign: return "|=";
122 case Comma: return ",";
123 }
124}
Steve Naroff47500512007-04-19 23:00:49 +0000125
Chris Lattner1ec5f562007-06-27 05:38:08 +0000126
127//===----------------------------------------------------------------------===//
128// Generic Expression Routines
129//===----------------------------------------------------------------------===//
130
131/// hasLocalSideEffect - Return true if this immediate expression has side
132/// effects, not counting any sub-expressions.
133bool Expr::hasLocalSideEffect() const {
134 switch (getStmtClass()) {
135 default:
136 return false;
137 case ParenExprClass:
138 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
139 case UnaryOperatorClass: {
140 const UnaryOperator *UO = cast<UnaryOperator>(this);
141
142 switch (UO->getOpcode()) {
143 default: return false;
144 case UnaryOperator::PostInc:
145 case UnaryOperator::PostDec:
146 case UnaryOperator::PreInc:
147 case UnaryOperator::PreDec:
148 return true; // ++/--
149
Chris Lattnera44d1162007-06-27 05:58:59 +0000150 case UnaryOperator::Deref:
151 // Dereferencing a volatile pointer is a side-effect.
152 return getType().isVolatileQualified();
153 case UnaryOperator::Real:
154 case UnaryOperator::Imag:
155 // accessing a piece of a volatile complex is a side-effect.
156 return UO->getSubExpr()->getType().isVolatileQualified();
157
Chris Lattner1ec5f562007-06-27 05:38:08 +0000158 case UnaryOperator::Extension:
159 return UO->getSubExpr()->hasLocalSideEffect();
160 }
161 }
162 case BinaryOperatorClass:
163 return cast<BinaryOperator>(this)->isAssignmentOp();
164
Chris Lattnera44d1162007-06-27 05:58:59 +0000165 case MemberExprClass:
Chris Lattner1ec5f562007-06-27 05:38:08 +0000166 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000167 // If the base pointer or element is to a volatile pointer/field, accessing
168 // if is a side effect.
169 return getType().isVolatileQualified();
Chris Lattner1ec5f562007-06-27 05:38:08 +0000170
171 case CallExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000172 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
173 // should warn.
Chris Lattner1ec5f562007-06-27 05:38:08 +0000174 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000175
176 case CastExprClass:
177 // If this is a cast to void, check the operand. Otherwise, the result of
178 // the cast is unused.
179 if (getType()->isVoidType())
180 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
181 return false;
182 }
183}
184
Steve Naroff475cca02007-05-14 17:19:29 +0000185/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
186/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000187/// - name, where name must be a variable
188/// - e[i]
189/// - (e), where e must be an lvalue
190/// - e.name, where e must be an lvalue
191/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000192/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000193/// - string-constant
Bill Wendlingdfc81072007-07-17 03:52:31 +0000194/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +0000195///
Bill Wendling457d1342007-07-16 07:07:56 +0000196Expr::isLvalueResult Expr::isLvalue() const {
Steve Naroff475cca02007-05-14 17:19:29 +0000197 // first, check the type (C99 6.3.2.1)
Steve Naroff9358c712007-05-27 23:58:33 +0000198 if (isa<FunctionType>(TR.getCanonicalType())) // from isObjectType()
199 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +0000200
Steve Naroff475cca02007-05-14 17:19:29 +0000201 if (TR->isIncompleteType() && TR->isVoidType())
Steve Naroff9358c712007-05-27 23:58:33 +0000202 return LV_IncompleteVoidType;
Bill Wendlingdfc81072007-07-17 03:52:31 +0000203
204 if (isa<ReferenceType>(TR.getCanonicalType())) // C++ [expr]
205 return LV_Valid;
206
Steve Naroff5dd642e2007-05-14 18:14:51 +0000207 // the type looks fine, now check the expression
Steve Naroff47500512007-04-19 23:00:49 +0000208 switch (getStmtClass()) {
Steve Naroff475cca02007-05-14 17:19:29 +0000209 case StringLiteralClass: // C99 6.5.1p4
Steve Naroff5dd642e2007-05-14 18:14:51 +0000210 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +0000211 // For vectors, make sure base is an lvalue (i.e. not a function call).
212 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
213 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
Steve Naroff9358c712007-05-27 23:58:33 +0000214 return LV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000215 case DeclRefExprClass: // C99 6.5.1p2
Steve Naroff9358c712007-05-27 23:58:33 +0000216 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
217 return LV_Valid;
218 break;
Anton Korobeynikovb76cda02007-07-12 15:26:50 +0000219 case MemberExprClass: { // C99 6.5.2.3p4
Steve Naroff47500512007-04-19 23:00:49 +0000220 const MemberExpr *m = cast<MemberExpr>(this);
Steve Naroff9358c712007-05-27 23:58:33 +0000221 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovb76cda02007-07-12 15:26:50 +0000222 }
Steve Naroff475cca02007-05-14 17:19:29 +0000223 case UnaryOperatorClass: // C99 6.5.3p4
Steve Naroff9358c712007-05-27 23:58:33 +0000224 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
225 return LV_Valid;
226 break;
Steve Naroff475cca02007-05-14 17:19:29 +0000227 case ParenExprClass: // C99 6.5.1p5
228 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroff9358c712007-05-27 23:58:33 +0000229 default:
230 break;
Steve Naroff47500512007-04-19 23:00:49 +0000231 }
Steve Naroff9358c712007-05-27 23:58:33 +0000232 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +0000233}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000234
Steve Naroff475cca02007-05-14 17:19:29 +0000235/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
236/// does not have an incomplete type, does not have a const-qualified type, and
237/// if it is a structure or union, does not have any member (including,
238/// recursively, any member or element of all contained aggregates or unions)
239/// with a const-qualified type.
Bill Wendling457d1342007-07-16 07:07:56 +0000240Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Steve Naroff9358c712007-05-27 23:58:33 +0000241 isLvalueResult lvalResult = isLvalue();
242
243 switch (lvalResult) {
Chris Lattner1ec5f562007-06-27 05:38:08 +0000244 case LV_Valid: break;
245 case LV_NotObjectType: return MLV_NotObjectType;
246 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
247 case LV_InvalidExpression: return MLV_InvalidExpression;
Steve Naroff9358c712007-05-27 23:58:33 +0000248 }
Steve Naroff475cca02007-05-14 17:19:29 +0000249 if (TR.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +0000250 return MLV_ConstQualified;
Steve Naroff475cca02007-05-14 17:19:29 +0000251 if (TR->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +0000252 return MLV_ArrayType;
Steve Naroff475cca02007-05-14 17:19:29 +0000253 if (TR->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +0000254 return MLV_IncompleteType;
255
256 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
257 if (r->hasConstFields())
258 return MLV_ConstQualified;
259 }
260 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000261}
262
Chris Lattner1f4479e2007-06-05 04:15:44 +0000263/// isIntegerConstantExpr - this recursive routine will test if an expression is
264/// an integer constant expression. Note: With the introduction of VLA's in
Steve Narofff8a28c52007-05-15 20:29:32 +0000265/// C99 the result of the sizeof operator is no longer always a constant
266/// expression. The generalization of the wording to include any subexpression
267/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
268/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
269/// "0 || f()" can be treated as a constant expression. In C90 this expression,
270/// occurring in a context requiring a constant, would have been a constraint
271/// violation. FIXME: This routine currently implements C90 semantics.
272/// To properly implement C99 semantics this routine will need to evaluate
273/// expressions involving operators previously mentioned.
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000274
275/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
276/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +0000277///
278/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
279/// permit this.
Chris Lattnerd7372ba2007-07-18 05:21:20 +0000280///
281/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
282/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
283/// cast+dereference.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000284bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
285 SourceLocation *Loc, bool isEvaluated) const {
Steve Naroff8eeeb132007-05-08 21:09:37 +0000286 switch (getStmtClass()) {
Steve Naroff72cada02007-05-18 00:18:52 +0000287 default:
Chris Lattner238cbc52007-06-02 04:48:48 +0000288 if (Loc) *Loc = getLocStart();
Steve Naroff8eeeb132007-05-08 21:09:37 +0000289 return false;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000290 case ParenExprClass:
291 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner0e9d6222007-07-15 23:26:56 +0000292 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000293 case IntegerLiteralClass:
294 Result = cast<IntegerLiteral>(this)->getValue();
295 break;
Chris Lattner5c4664e2007-07-15 23:32:58 +0000296 case CharacterLiteralClass: {
297 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
298 Result.zextOrTrunc(Ctx.getTypeSize(getType(), CL->getLoc()));
299 Result = CL->getValue();
Chris Lattner7d138432007-07-16 21:04:56 +0000300 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000301 break;
Chris Lattner5c4664e2007-07-15 23:32:58 +0000302 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000303 case DeclRefExprClass:
304 if (const EnumConstantDecl *D =
305 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
Chris Lattner9fba0282007-06-11 03:47:05 +0000306 Result = D->getInitVal();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000307 break;
308 }
309 if (Loc) *Loc = getLocStart();
310 return false;
311 case UnaryOperatorClass: {
312 const UnaryOperator *Exp = cast<UnaryOperator>(this);
313
314 // Get the operand value. If this is sizeof/alignof, do not evalute the
315 // operand. This affects C99 6.6p3.
316 if (Exp->isSizeOfAlignOfOp()) isEvaluated = false;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000317 if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx,Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000318 return false;
319
320 switch (Exp->getOpcode()) {
321 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
322 // See C99 6.6p3.
323 default:
324 if (Loc) *Loc = Exp->getOperatorLoc();
325 return false;
326 case UnaryOperator::Extension:
327 return true;
328 case UnaryOperator::SizeOf:
329 case UnaryOperator::AlignOf:
330 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000331 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000332 return false;
333
334 // FIXME: Evaluate sizeof/alignof.
335 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
336 Result = 1; // FIXME: Obviously bogus
337 break;
338 case UnaryOperator::LNot: {
339 bool Val = Result != 0;
340 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
341 Result = Val;
342 break;
343 }
344 case UnaryOperator::Plus:
345 // FIXME: Do usual unary promotions here!
346 break;
347 case UnaryOperator::Minus:
348 // FIXME: Do usual unary promotions here!
349 Result = -Result;
350 break;
351 case UnaryOperator::Not:
352 // FIXME: Do usual unary promotions here!
353 Result = ~Result;
354 break;
355 }
356 break;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000357 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000358 case SizeOfAlignOfTypeExprClass: {
359 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
360 // alignof always evaluates to a constant.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000361 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000362 return false;
363
364 // FIXME: Evaluate sizeof/alignof.
365 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
366 Result = 1; // FIXME: Obviously bogus
367 break;
368 }
369 case BinaryOperatorClass: {
370 const BinaryOperator *Exp = cast<BinaryOperator>(this);
371
372 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000373 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000374 return false;
375
Chris Lattner23b7eb62007-06-15 23:05:46 +0000376 llvm::APSInt RHS(Result);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000377
378 // The short-circuiting &&/|| operators don't necessarily evaluate their
379 // RHS. Make sure to pass isEvaluated down correctly.
380 if (Exp->isLogicalOp()) {
381 bool RHSEval;
382 if (Exp->getOpcode() == BinaryOperator::LAnd)
383 RHSEval = Result != 0;
384 else {
385 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
386 RHSEval = Result == 0;
387 }
388
Chris Lattner0e9d6222007-07-15 23:26:56 +0000389 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000390 isEvaluated & RHSEval))
391 return false;
392 } else {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000393 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000394 return false;
395 }
396
397 // FIXME: These should all do the standard promotions, etc.
398 switch (Exp->getOpcode()) {
399 default:
400 if (Loc) *Loc = getLocStart();
401 return false;
402 case BinaryOperator::Mul:
403 Result *= RHS;
404 break;
405 case BinaryOperator::Div:
406 if (RHS == 0) {
407 if (!isEvaluated) break;
408 if (Loc) *Loc = getLocStart();
409 return false;
410 }
411 Result /= RHS;
412 break;
413 case BinaryOperator::Rem:
414 if (RHS == 0) {
415 if (!isEvaluated) break;
416 if (Loc) *Loc = getLocStart();
417 return false;
418 }
419 Result %= RHS;
420 break;
421 case BinaryOperator::Add: Result += RHS; break;
422 case BinaryOperator::Sub: Result -= RHS; break;
Chris Lattner901ae1f2007-06-08 21:54:26 +0000423 case BinaryOperator::Shl:
424 Result <<= RHS.getLimitedValue(Result.getBitWidth()-1);
425 break;
426 case BinaryOperator::Shr:
427 Result >>= RHS.getLimitedValue(Result.getBitWidth()-1);
428 break;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000429 case BinaryOperator::LT: Result = Result < RHS; break;
430 case BinaryOperator::GT: Result = Result > RHS; break;
431 case BinaryOperator::LE: Result = Result <= RHS; break;
432 case BinaryOperator::GE: Result = Result >= RHS; break;
433 case BinaryOperator::EQ: Result = Result == RHS; break;
434 case BinaryOperator::NE: Result = Result != RHS; break;
435 case BinaryOperator::And: Result &= RHS; break;
436 case BinaryOperator::Xor: Result ^= RHS; break;
437 case BinaryOperator::Or: Result |= RHS; break;
438 case BinaryOperator::LAnd:
439 Result = Result != 0 && RHS != 0;
440 break;
441 case BinaryOperator::LOr:
442 Result = Result != 0 || RHS != 0;
443 break;
444
445 case BinaryOperator::Comma:
446 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
447 // *except* when they are contained within a subexpression that is not
448 // evaluated". Note that Assignment can never happen due to constraints
449 // on the LHS subexpr, so we don't need to check it here.
450 if (isEvaluated) {
451 if (Loc) *Loc = getLocStart();
452 return false;
453 }
454
455 // The result of the constant expr is the RHS.
456 Result = RHS;
457 return true;
458 }
459
460 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
461 break;
462 }
Chris Lattner51aff8b2007-07-15 23:54:50 +0000463 case ImplicitCastExprClass:
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000464 case CastExprClass: {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000465 const Expr *SubExpr;
466 SourceLocation CastLoc;
467 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
468 SubExpr = C->getSubExpr();
469 CastLoc = C->getLParenLoc();
470 } else {
471 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
472 CastLoc = getLocStart();
473 }
474
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000475 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner51aff8b2007-07-15 23:54:50 +0000476 if (!SubExpr->getType()->isArithmeticType() ||
477 !getType()->isIntegerType()) {
478 if (Loc) *Loc = SubExpr->getLocStart();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000479 return false;
480 }
481
482 // Handle simple integer->integer casts.
Chris Lattner51aff8b2007-07-15 23:54:50 +0000483 if (SubExpr->getType()->isIntegerType()) {
484 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000485 return false;
Chris Lattner51aff8b2007-07-15 23:54:50 +0000486
487 // Figure out if this is a truncate, extend or noop cast.
488 unsigned DestWidth = Ctx.getTypeSize(getType(), CastLoc);
489
490 // If the input is signed, do a sign extend, noop, or truncate.
491 if (SubExpr->getType()->isSignedIntegerType())
492 Result.sextOrTrunc(DestWidth);
493 else // If the input is unsigned, do a zero extend, noop, or truncate.
494 Result.zextOrTrunc(DestWidth);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000495 break;
496 }
497
498 // Allow floating constants that are the immediate operands of casts or that
499 // are parenthesized.
Chris Lattner51aff8b2007-07-15 23:54:50 +0000500 const Expr *Operand = SubExpr;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000501 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
502 Operand = PE->getSubExpr();
503
504 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) {
505 // FIXME: Evaluate this correctly!
506 Result = (int)FL->getValue();
507 break;
508 }
Chris Lattner6046e002007-06-05 06:39:23 +0000509 if (Loc) *Loc = Operand->getLocStart();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000510 return false;
511 }
512 case ConditionalOperatorClass: {
513 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
514
Chris Lattner0e9d6222007-07-15 23:26:56 +0000515 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000516 return false;
517
518 const Expr *TrueExp = Exp->getLHS();
519 const Expr *FalseExp = Exp->getRHS();
520 if (Result == 0) std::swap(TrueExp, FalseExp);
521
522 // Evaluate the false one first, discard the result.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000523 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000524 return false;
525 // Evalute the true one, capture the result.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000526 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000527 return false;
528 // FIXME: promotions on result.
529 break;
530 }
531 }
532
533 // Cases that are valid constant exprs fall through to here.
534 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
535 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000536}
537
Chris Lattner1f4479e2007-06-05 04:15:44 +0000538
Chris Lattner7eef9192007-05-24 01:23:49 +0000539/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
540/// integer constant expression with the value zero, or if this is one that is
541/// cast to void*.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000542bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Chris Lattner7eef9192007-05-24 01:23:49 +0000543 // Strip off a cast to void*, if it exists.
544 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
545 // Check that it is a cast to void*.
546 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
547 QualType Pointee = PT->getPointeeType();
548 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
549 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000550 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffada7d422007-05-20 17:54:12 +0000551 }
Chris Lattner7eef9192007-05-24 01:23:49 +0000552 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
553 // Accept ((void*)0) as a null pointer constant, as many other
554 // implementations do.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000555 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattnere6b0f622007-05-21 05:58:35 +0000556 }
557
Chris Lattner7eef9192007-05-24 01:23:49 +0000558 // This expression must be an integer type.
559 if (!getType()->isIntegerType())
560 return false;
561
Chris Lattner1abbd412007-06-08 17:58:43 +0000562 // If we have an integer constant expression, we need to *evaluate* it and
563 // test for the value 0.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000564 llvm::APSInt Val(32);
Chris Lattner0e9d6222007-07-15 23:26:56 +0000565 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000566}