blob: b704d9654b615bac18229ac5a3464c04c8fa8ca1 [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 Lattner5e9a8782006-11-04 06:21:51 +000015#include "clang/AST/StmtVisitor.h"
Chris Lattnere165d942006-08-24 04:40:38 +000016#include "clang/Lex/IdentifierTable.h"
Chris Lattner1b926492006-08-23 06:42:10 +000017using namespace clang;
18
Chris Lattner0eedafe2006-08-24 04:56:27 +000019//===----------------------------------------------------------------------===//
20// Primary Expressions.
21//===----------------------------------------------------------------------===//
22
Steve Naroff408451b2007-02-26 22:17:12 +000023StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
Steve Naroff53f07dc2007-05-17 21:49:33 +000024 bool Wide, QualType t, SourceLocation firstLoc,
25 SourceLocation lastLoc) :
Steve Narofff1e53692007-03-23 22:27:02 +000026 Expr(StringLiteralClass, t) {
Steve Naroffdf7855b2007-02-21 23:46:25 +000027 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerd3e98952006-10-06 05:22:26 +000028 char *AStrData = new char[byteLength];
29 memcpy(AStrData, strData, byteLength);
30 StrData = AStrData;
31 ByteLength = byteLength;
Chris Lattner882f7882006-11-04 18:52:07 +000032 IsWide = Wide;
Steve Naroff53f07dc2007-05-17 21:49:33 +000033 firstTokLoc = firstLoc;
34 lastTokLoc = lastLoc;
Chris Lattnerd3e98952006-10-06 05:22:26 +000035}
36
Steve Naroffdf7855b2007-02-21 23:46:25 +000037StringLiteral::~StringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +000038 delete[] StrData;
39}
40
Chris Lattner15768702006-11-05 23:54:51 +000041bool UnaryOperator::isPostfix(Opcode Op) {
42 switch (Op) {
43 case PostInc:
44 case PostDec:
45 return true;
46 default:
47 return false;
48 }
49}
50
Chris Lattner1b926492006-08-23 06:42:10 +000051/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
52/// corresponds to, e.g. "sizeof" or "[pre]++".
53const char *UnaryOperator::getOpcodeStr(Opcode Op) {
54 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +000055 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +000056 case PostInc: return "++";
57 case PostDec: return "--";
58 case PreInc: return "++";
59 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +000060 case AddrOf: return "&";
61 case Deref: return "*";
62 case Plus: return "+";
63 case Minus: return "-";
64 case Not: return "~";
65 case LNot: return "!";
66 case Real: return "__real";
67 case Imag: return "__imag";
Chris Lattner26115ac2006-08-24 06:10:04 +000068 case SizeOf: return "sizeof";
69 case AlignOf: return "alignof";
Chris Lattnerc52b1182006-10-25 05:45:55 +000070 case Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +000071 }
72}
73
Chris Lattner0eedafe2006-08-24 04:56:27 +000074//===----------------------------------------------------------------------===//
75// Postfix Operators.
76//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +000077
Steve Naroff509fe022007-05-17 01:16:00 +000078CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
Chris Lattner9b3b9a12007-06-27 06:08:24 +000079 SourceLocation rparenloc)
Steve Naroffae4143e2007-04-26 20:39:23 +000080 : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) {
Chris Lattnere165d942006-08-24 04:40:38 +000081 Args = new Expr*[numargs];
82 for (unsigned i = 0; i != numargs; ++i)
83 Args[i] = args[i];
Chris Lattner9b3b9a12007-06-27 06:08:24 +000084 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +000085}
86
Chris Lattner1b926492006-08-23 06:42:10 +000087/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
88/// corresponds to, e.g. "<<=".
89const char *BinaryOperator::getOpcodeStr(Opcode Op) {
90 switch (Op) {
91 default: assert(0 && "Unknown binary operator");
92 case Mul: return "*";
93 case Div: return "/";
94 case Rem: return "%";
95 case Add: return "+";
96 case Sub: return "-";
97 case Shl: return "<<";
98 case Shr: return ">>";
99 case LT: return "<";
100 case GT: return ">";
101 case LE: return "<=";
102 case GE: return ">=";
103 case EQ: return "==";
104 case NE: return "!=";
105 case And: return "&";
106 case Xor: return "^";
107 case Or: return "|";
108 case LAnd: return "&&";
109 case LOr: return "||";
110 case Assign: return "=";
111 case MulAssign: return "*=";
112 case DivAssign: return "/=";
113 case RemAssign: return "%=";
114 case AddAssign: return "+=";
115 case SubAssign: return "-=";
116 case ShlAssign: return "<<=";
117 case ShrAssign: return ">>=";
118 case AndAssign: return "&=";
119 case XorAssign: return "^=";
120 case OrAssign: return "|=";
121 case Comma: return ",";
122 }
123}
Steve Naroff47500512007-04-19 23:00:49 +0000124
Chris Lattner1ec5f562007-06-27 05:38:08 +0000125
126//===----------------------------------------------------------------------===//
127// Generic Expression Routines
128//===----------------------------------------------------------------------===//
129
130/// hasLocalSideEffect - Return true if this immediate expression has side
131/// effects, not counting any sub-expressions.
132bool Expr::hasLocalSideEffect() const {
133 switch (getStmtClass()) {
134 default:
135 return false;
136 case ParenExprClass:
137 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
138 case UnaryOperatorClass: {
139 const UnaryOperator *UO = cast<UnaryOperator>(this);
140
141 switch (UO->getOpcode()) {
142 default: return false;
143 case UnaryOperator::PostInc:
144 case UnaryOperator::PostDec:
145 case UnaryOperator::PreInc:
146 case UnaryOperator::PreDec:
147 return true; // ++/--
148
Chris Lattnera44d1162007-06-27 05:58:59 +0000149 case UnaryOperator::Deref:
150 // Dereferencing a volatile pointer is a side-effect.
151 return getType().isVolatileQualified();
152 case UnaryOperator::Real:
153 case UnaryOperator::Imag:
154 // accessing a piece of a volatile complex is a side-effect.
155 return UO->getSubExpr()->getType().isVolatileQualified();
156
Chris Lattner1ec5f562007-06-27 05:38:08 +0000157 case UnaryOperator::Extension:
158 return UO->getSubExpr()->hasLocalSideEffect();
159 }
160 }
161 case BinaryOperatorClass:
162 return cast<BinaryOperator>(this)->isAssignmentOp();
163
Chris Lattnera44d1162007-06-27 05:58:59 +0000164 case MemberExprClass:
Chris Lattner1ec5f562007-06-27 05:38:08 +0000165 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000166 // If the base pointer or element is to a volatile pointer/field, accessing
167 // if is a side effect.
168 return getType().isVolatileQualified();
Chris Lattner1ec5f562007-06-27 05:38:08 +0000169
170 case CallExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000171 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
172 // should warn.
Chris Lattner1ec5f562007-06-27 05:38:08 +0000173 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000174
175 case CastExprClass:
176 // If this is a cast to void, check the operand. Otherwise, the result of
177 // the cast is unused.
178 if (getType()->isVoidType())
179 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
180 return false;
181 }
182}
183
Steve Naroff475cca02007-05-14 17:19:29 +0000184/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
185/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000186/// - name, where name must be a variable
187/// - e[i]
188/// - (e), where e must be an lvalue
189/// - e.name, where e must be an lvalue
190/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000191/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000192/// - string-constant
193///
Steve Naroff9358c712007-05-27 23:58:33 +0000194Expr::isLvalueResult Expr::isLvalue() {
Steve Naroff475cca02007-05-14 17:19:29 +0000195 // first, check the type (C99 6.3.2.1)
Steve Naroff9358c712007-05-27 23:58:33 +0000196 if (isa<FunctionType>(TR.getCanonicalType())) // from isObjectType()
197 return LV_NotObjectType;
Steve Naroff475cca02007-05-14 17:19:29 +0000198 if (TR->isIncompleteType() && TR->isVoidType())
Steve Naroff9358c712007-05-27 23:58:33 +0000199 return LV_IncompleteVoidType;
Steve Naroff475cca02007-05-14 17:19:29 +0000200
Steve Naroff5dd642e2007-05-14 18:14:51 +0000201 // the type looks fine, now check the expression
Steve Naroff47500512007-04-19 23:00:49 +0000202 switch (getStmtClass()) {
Steve Naroff475cca02007-05-14 17:19:29 +0000203 case StringLiteralClass: // C99 6.5.1p4
Steve Naroff5dd642e2007-05-14 18:14:51 +0000204 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroff9358c712007-05-27 23:58:33 +0000205 return LV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000206 case DeclRefExprClass: // C99 6.5.1p2
Steve Naroff9358c712007-05-27 23:58:33 +0000207 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
208 return LV_Valid;
209 break;
Steve Naroff475cca02007-05-14 17:19:29 +0000210 case MemberExprClass: // C99 6.5.2.3p4
Steve Naroff47500512007-04-19 23:00:49 +0000211 const MemberExpr *m = cast<MemberExpr>(this);
Steve Naroff9358c712007-05-27 23:58:33 +0000212 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Steve Naroff475cca02007-05-14 17:19:29 +0000213 case UnaryOperatorClass: // C99 6.5.3p4
Steve Naroff9358c712007-05-27 23:58:33 +0000214 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
215 return LV_Valid;
216 break;
Steve Naroff475cca02007-05-14 17:19:29 +0000217 case ParenExprClass: // C99 6.5.1p5
218 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroff9358c712007-05-27 23:58:33 +0000219 default:
220 break;
Steve Naroff47500512007-04-19 23:00:49 +0000221 }
Steve Naroff9358c712007-05-27 23:58:33 +0000222 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +0000223}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000224
Steve Naroff475cca02007-05-14 17:19:29 +0000225/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
226/// does not have an incomplete type, does not have a const-qualified type, and
227/// if it is a structure or union, does not have any member (including,
228/// recursively, any member or element of all contained aggregates or unions)
229/// with a const-qualified type.
Steve Naroff9358c712007-05-27 23:58:33 +0000230Expr::isModifiableLvalueResult Expr::isModifiableLvalue() {
231 isLvalueResult lvalResult = isLvalue();
232
233 switch (lvalResult) {
Chris Lattner1ec5f562007-06-27 05:38:08 +0000234 case LV_Valid: break;
235 case LV_NotObjectType: return MLV_NotObjectType;
236 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
237 case LV_InvalidExpression: return MLV_InvalidExpression;
Steve Naroff9358c712007-05-27 23:58:33 +0000238 }
Steve Naroff475cca02007-05-14 17:19:29 +0000239 if (TR.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +0000240 return MLV_ConstQualified;
Steve Naroff475cca02007-05-14 17:19:29 +0000241 if (TR->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +0000242 return MLV_ArrayType;
Steve Naroff475cca02007-05-14 17:19:29 +0000243 if (TR->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +0000244 return MLV_IncompleteType;
245
246 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
247 if (r->hasConstFields())
248 return MLV_ConstQualified;
249 }
250 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000251}
252
Chris Lattner1f4479e2007-06-05 04:15:44 +0000253/// isIntegerConstantExpr - this recursive routine will test if an expression is
254/// an integer constant expression. Note: With the introduction of VLA's in
Steve Narofff8a28c52007-05-15 20:29:32 +0000255/// C99 the result of the sizeof operator is no longer always a constant
256/// expression. The generalization of the wording to include any subexpression
257/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
258/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
259/// "0 || f()" can be treated as a constant expression. In C90 this expression,
260/// occurring in a context requiring a constant, would have been a constraint
261/// violation. FIXME: This routine currently implements C90 semantics.
262/// To properly implement C99 semantics this routine will need to evaluate
263/// expressions involving operators previously mentioned.
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000264
265/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
266/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +0000267///
268/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
269/// permit this.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000270bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, SourceLocation *Loc,
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000271 bool isEvaluated) const {
Steve Naroff8eeeb132007-05-08 21:09:37 +0000272 switch (getStmtClass()) {
Steve Naroff72cada02007-05-18 00:18:52 +0000273 default:
Chris Lattner238cbc52007-06-02 04:48:48 +0000274 if (Loc) *Loc = getLocStart();
Steve Naroff8eeeb132007-05-08 21:09:37 +0000275 return false;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000276 case ParenExprClass:
277 return cast<ParenExpr>(this)->getSubExpr()->
278 isIntegerConstantExpr(Result, Loc, isEvaluated);
279 case IntegerLiteralClass:
280 Result = cast<IntegerLiteral>(this)->getValue();
281 break;
282 case CharacterLiteralClass:
283 // FIXME: This doesn't set the right width etc.
284 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
Chris Lattner4cd73fd2007-06-08 21:51:02 +0000285 Result = cast<CharacterLiteral>(this)->getValue();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000286 break;
287 case DeclRefExprClass:
288 if (const EnumConstantDecl *D =
289 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
Chris Lattner9fba0282007-06-11 03:47:05 +0000290 Result = D->getInitVal();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000291 break;
292 }
293 if (Loc) *Loc = getLocStart();
294 return false;
295 case UnaryOperatorClass: {
296 const UnaryOperator *Exp = cast<UnaryOperator>(this);
297
298 // Get the operand value. If this is sizeof/alignof, do not evalute the
299 // operand. This affects C99 6.6p3.
300 if (Exp->isSizeOfAlignOfOp()) isEvaluated = false;
301 if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Loc, isEvaluated))
302 return false;
303
304 switch (Exp->getOpcode()) {
305 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
306 // See C99 6.6p3.
307 default:
308 if (Loc) *Loc = Exp->getOperatorLoc();
309 return false;
310 case UnaryOperator::Extension:
311 return true;
312 case UnaryOperator::SizeOf:
313 case UnaryOperator::AlignOf:
314 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
315 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Loc))
316 return false;
317
318 // FIXME: Evaluate sizeof/alignof.
319 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
320 Result = 1; // FIXME: Obviously bogus
321 break;
322 case UnaryOperator::LNot: {
323 bool Val = Result != 0;
324 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
325 Result = Val;
326 break;
327 }
328 case UnaryOperator::Plus:
329 // FIXME: Do usual unary promotions here!
330 break;
331 case UnaryOperator::Minus:
332 // FIXME: Do usual unary promotions here!
333 Result = -Result;
334 break;
335 case UnaryOperator::Not:
336 // FIXME: Do usual unary promotions here!
337 Result = ~Result;
338 break;
339 }
340 break;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000341 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000342 case SizeOfAlignOfTypeExprClass: {
343 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
344 // alignof always evaluates to a constant.
345 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Loc))
346 return false;
347
348 // FIXME: Evaluate sizeof/alignof.
349 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
350 Result = 1; // FIXME: Obviously bogus
351 break;
352 }
353 case BinaryOperatorClass: {
354 const BinaryOperator *Exp = cast<BinaryOperator>(this);
355
356 // The LHS of a constant expr is always evaluated and needed.
357 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Loc, isEvaluated))
358 return false;
359
Chris Lattner23b7eb62007-06-15 23:05:46 +0000360 llvm::APSInt RHS(Result);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000361
362 // The short-circuiting &&/|| operators don't necessarily evaluate their
363 // RHS. Make sure to pass isEvaluated down correctly.
364 if (Exp->isLogicalOp()) {
365 bool RHSEval;
366 if (Exp->getOpcode() == BinaryOperator::LAnd)
367 RHSEval = Result != 0;
368 else {
369 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
370 RHSEval = Result == 0;
371 }
372
373 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Loc,
374 isEvaluated & RHSEval))
375 return false;
376 } else {
377 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Loc, isEvaluated))
378 return false;
379 }
380
381 // FIXME: These should all do the standard promotions, etc.
382 switch (Exp->getOpcode()) {
383 default:
384 if (Loc) *Loc = getLocStart();
385 return false;
386 case BinaryOperator::Mul:
387 Result *= RHS;
388 break;
389 case BinaryOperator::Div:
390 if (RHS == 0) {
391 if (!isEvaluated) break;
392 if (Loc) *Loc = getLocStart();
393 return false;
394 }
395 Result /= RHS;
396 break;
397 case BinaryOperator::Rem:
398 if (RHS == 0) {
399 if (!isEvaluated) break;
400 if (Loc) *Loc = getLocStart();
401 return false;
402 }
403 Result %= RHS;
404 break;
405 case BinaryOperator::Add: Result += RHS; break;
406 case BinaryOperator::Sub: Result -= RHS; break;
Chris Lattner901ae1f2007-06-08 21:54:26 +0000407 case BinaryOperator::Shl:
408 Result <<= RHS.getLimitedValue(Result.getBitWidth()-1);
409 break;
410 case BinaryOperator::Shr:
411 Result >>= RHS.getLimitedValue(Result.getBitWidth()-1);
412 break;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000413 case BinaryOperator::LT: Result = Result < RHS; break;
414 case BinaryOperator::GT: Result = Result > RHS; break;
415 case BinaryOperator::LE: Result = Result <= RHS; break;
416 case BinaryOperator::GE: Result = Result >= RHS; break;
417 case BinaryOperator::EQ: Result = Result == RHS; break;
418 case BinaryOperator::NE: Result = Result != RHS; break;
419 case BinaryOperator::And: Result &= RHS; break;
420 case BinaryOperator::Xor: Result ^= RHS; break;
421 case BinaryOperator::Or: Result |= RHS; break;
422 case BinaryOperator::LAnd:
423 Result = Result != 0 && RHS != 0;
424 break;
425 case BinaryOperator::LOr:
426 Result = Result != 0 || RHS != 0;
427 break;
428
429 case BinaryOperator::Comma:
430 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
431 // *except* when they are contained within a subexpression that is not
432 // evaluated". Note that Assignment can never happen due to constraints
433 // on the LHS subexpr, so we don't need to check it here.
434 if (isEvaluated) {
435 if (Loc) *Loc = getLocStart();
436 return false;
437 }
438
439 // The result of the constant expr is the RHS.
440 Result = RHS;
441 return true;
442 }
443
444 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
445 break;
446 }
447 case CastExprClass: {
448 const CastExpr *Exp = cast<CastExpr>(this);
449 // C99 6.6p6: shall only convert arithmetic types to integer types.
450 if (!Exp->getSubExpr()->getType()->isArithmeticType() ||
451 !Exp->getDestType()->isIntegerType()) {
452 if (Loc) *Loc = Exp->getSubExpr()->getLocStart();
453 return false;
454 }
455
456 // Handle simple integer->integer casts.
457 if (Exp->getSubExpr()->getType()->isIntegerType()) {
458 if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Loc, isEvaluated))
459 return false;
460 // FIXME: do the conversion on Result.
461 break;
462 }
463
464 // Allow floating constants that are the immediate operands of casts or that
465 // are parenthesized.
466 const Expr *Operand = Exp->getSubExpr();
467 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
468 Operand = PE->getSubExpr();
469
470 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) {
471 // FIXME: Evaluate this correctly!
472 Result = (int)FL->getValue();
473 break;
474 }
Chris Lattner6046e002007-06-05 06:39:23 +0000475 if (Loc) *Loc = Operand->getLocStart();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000476 return false;
477 }
478 case ConditionalOperatorClass: {
479 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
480
481 if (!Exp->getCond()->isIntegerConstantExpr(Result, Loc, isEvaluated))
482 return false;
483
484 const Expr *TrueExp = Exp->getLHS();
485 const Expr *FalseExp = Exp->getRHS();
486 if (Result == 0) std::swap(TrueExp, FalseExp);
487
488 // Evaluate the false one first, discard the result.
489 if (!FalseExp->isIntegerConstantExpr(Result, Loc, false))
490 return false;
491 // Evalute the true one, capture the result.
492 if (!TrueExp->isIntegerConstantExpr(Result, Loc, isEvaluated))
493 return false;
494 // FIXME: promotions on result.
495 break;
496 }
497 }
498
499 // Cases that are valid constant exprs fall through to here.
500 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
501 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000502}
503
Chris Lattner1f4479e2007-06-05 04:15:44 +0000504
Chris Lattner7eef9192007-05-24 01:23:49 +0000505/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
506/// integer constant expression with the value zero, or if this is one that is
507/// cast to void*.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000508bool Expr::isNullPointerConstant() const {
Chris Lattner7eef9192007-05-24 01:23:49 +0000509 // Strip off a cast to void*, if it exists.
510 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
511 // Check that it is a cast to void*.
512 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
513 QualType Pointee = PT->getPointeeType();
514 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
515 CE->getSubExpr()->getType()->isIntegerType()) // from int.
516 return CE->getSubExpr()->isNullPointerConstant();
Steve Naroffada7d422007-05-20 17:54:12 +0000517 }
Chris Lattner7eef9192007-05-24 01:23:49 +0000518 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
519 // Accept ((void*)0) as a null pointer constant, as many other
520 // implementations do.
521 return PE->getSubExpr()->isNullPointerConstant();
Chris Lattnere6b0f622007-05-21 05:58:35 +0000522 }
523
Chris Lattner7eef9192007-05-24 01:23:49 +0000524 // This expression must be an integer type.
525 if (!getType()->isIntegerType())
526 return false;
527
Chris Lattner1abbd412007-06-08 17:58:43 +0000528 // If we have an integer constant expression, we need to *evaluate* it and
529 // test for the value 0.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000530 llvm::APSInt Val(32);
Chris Lattner1abbd412007-06-08 17:58:43 +0000531 return isIntegerConstantExpr(Val, 0, true) && Val == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000532}