blob: 5d109203cae218948def6dc81a18667af50482b5 [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,
79 SourceLocation l)
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];
Steve Naroff509fe022007-05-17 01:16:00 +000084 Loc = l;
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
149 // FIXME: real/imag volatile
150 // deref volatile;
151
152 case UnaryOperator::Extension:
153 return UO->getSubExpr()->hasLocalSideEffect();
154 }
155 }
156 case BinaryOperatorClass:
157 return cast<BinaryOperator>(this)->isAssignmentOp();
158
159 case ArraySubscriptExprClass:
160 // volatile
161 return false;
162
163 case CallExprClass:
164 // TODO: check attributes for pure/const.
165 return true;
166
167 case MemberExprClass:
168 // volatile load.
169 return false;
170
171 case CastExprClass:
172 // If this is a cast to void, check the operand. Otherwise, the result of
173 // the cast is unused.
174 if (getType()->isVoidType())
175 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
176 return false;
177 }
178}
179
Steve Naroff475cca02007-05-14 17:19:29 +0000180/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
181/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000182/// - name, where name must be a variable
183/// - e[i]
184/// - (e), where e must be an lvalue
185/// - e.name, where e must be an lvalue
186/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000187/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000188/// - string-constant
189///
Steve Naroff9358c712007-05-27 23:58:33 +0000190Expr::isLvalueResult Expr::isLvalue() {
Steve Naroff475cca02007-05-14 17:19:29 +0000191 // first, check the type (C99 6.3.2.1)
Steve Naroff9358c712007-05-27 23:58:33 +0000192 if (isa<FunctionType>(TR.getCanonicalType())) // from isObjectType()
193 return LV_NotObjectType;
Steve Naroff475cca02007-05-14 17:19:29 +0000194 if (TR->isIncompleteType() && TR->isVoidType())
Steve Naroff9358c712007-05-27 23:58:33 +0000195 return LV_IncompleteVoidType;
Steve Naroff475cca02007-05-14 17:19:29 +0000196
Steve Naroff5dd642e2007-05-14 18:14:51 +0000197 // the type looks fine, now check the expression
Steve Naroff47500512007-04-19 23:00:49 +0000198 switch (getStmtClass()) {
Steve Naroff475cca02007-05-14 17:19:29 +0000199 case StringLiteralClass: // C99 6.5.1p4
Steve Naroff5dd642e2007-05-14 18:14:51 +0000200 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroff9358c712007-05-27 23:58:33 +0000201 return LV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000202 case DeclRefExprClass: // C99 6.5.1p2
Steve Naroff9358c712007-05-27 23:58:33 +0000203 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
204 return LV_Valid;
205 break;
Steve Naroff475cca02007-05-14 17:19:29 +0000206 case MemberExprClass: // C99 6.5.2.3p4
Steve Naroff47500512007-04-19 23:00:49 +0000207 const MemberExpr *m = cast<MemberExpr>(this);
Steve Naroff9358c712007-05-27 23:58:33 +0000208 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Steve Naroff475cca02007-05-14 17:19:29 +0000209 case UnaryOperatorClass: // C99 6.5.3p4
Steve Naroff9358c712007-05-27 23:58:33 +0000210 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
211 return LV_Valid;
212 break;
Steve Naroff475cca02007-05-14 17:19:29 +0000213 case ParenExprClass: // C99 6.5.1p5
214 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroff9358c712007-05-27 23:58:33 +0000215 default:
216 break;
Steve Naroff47500512007-04-19 23:00:49 +0000217 }
Steve Naroff9358c712007-05-27 23:58:33 +0000218 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +0000219}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000220
Steve Naroff475cca02007-05-14 17:19:29 +0000221/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
222/// does not have an incomplete type, does not have a const-qualified type, and
223/// if it is a structure or union, does not have any member (including,
224/// recursively, any member or element of all contained aggregates or unions)
225/// with a const-qualified type.
Steve Naroff9358c712007-05-27 23:58:33 +0000226Expr::isModifiableLvalueResult Expr::isModifiableLvalue() {
227 isLvalueResult lvalResult = isLvalue();
228
229 switch (lvalResult) {
Chris Lattner1ec5f562007-06-27 05:38:08 +0000230 case LV_Valid: break;
231 case LV_NotObjectType: return MLV_NotObjectType;
232 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
233 case LV_InvalidExpression: return MLV_InvalidExpression;
Steve Naroff9358c712007-05-27 23:58:33 +0000234 }
Steve Naroff475cca02007-05-14 17:19:29 +0000235 if (TR.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +0000236 return MLV_ConstQualified;
Steve Naroff475cca02007-05-14 17:19:29 +0000237 if (TR->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +0000238 return MLV_ArrayType;
Steve Naroff475cca02007-05-14 17:19:29 +0000239 if (TR->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +0000240 return MLV_IncompleteType;
241
242 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
243 if (r->hasConstFields())
244 return MLV_ConstQualified;
245 }
246 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000247}
248
Chris Lattner1f4479e2007-06-05 04:15:44 +0000249/// isIntegerConstantExpr - this recursive routine will test if an expression is
250/// an integer constant expression. Note: With the introduction of VLA's in
Steve Narofff8a28c52007-05-15 20:29:32 +0000251/// C99 the result of the sizeof operator is no longer always a constant
252/// expression. The generalization of the wording to include any subexpression
253/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
254/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
255/// "0 || f()" can be treated as a constant expression. In C90 this expression,
256/// occurring in a context requiring a constant, would have been a constraint
257/// violation. FIXME: This routine currently implements C90 semantics.
258/// To properly implement C99 semantics this routine will need to evaluate
259/// expressions involving operators previously mentioned.
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000260
261/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
262/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +0000263///
264/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
265/// permit this.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000266bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, SourceLocation *Loc,
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000267 bool isEvaluated) const {
Steve Naroff8eeeb132007-05-08 21:09:37 +0000268 switch (getStmtClass()) {
Steve Naroff72cada02007-05-18 00:18:52 +0000269 default:
Chris Lattner238cbc52007-06-02 04:48:48 +0000270 if (Loc) *Loc = getLocStart();
Steve Naroff8eeeb132007-05-08 21:09:37 +0000271 return false;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000272 case ParenExprClass:
273 return cast<ParenExpr>(this)->getSubExpr()->
274 isIntegerConstantExpr(Result, Loc, isEvaluated);
275 case IntegerLiteralClass:
276 Result = cast<IntegerLiteral>(this)->getValue();
277 break;
278 case CharacterLiteralClass:
279 // FIXME: This doesn't set the right width etc.
280 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
Chris Lattner4cd73fd2007-06-08 21:51:02 +0000281 Result = cast<CharacterLiteral>(this)->getValue();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000282 break;
283 case DeclRefExprClass:
284 if (const EnumConstantDecl *D =
285 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
Chris Lattner9fba0282007-06-11 03:47:05 +0000286 Result = D->getInitVal();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000287 break;
288 }
289 if (Loc) *Loc = getLocStart();
290 return false;
291 case UnaryOperatorClass: {
292 const UnaryOperator *Exp = cast<UnaryOperator>(this);
293
294 // Get the operand value. If this is sizeof/alignof, do not evalute the
295 // operand. This affects C99 6.6p3.
296 if (Exp->isSizeOfAlignOfOp()) isEvaluated = false;
297 if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Loc, isEvaluated))
298 return false;
299
300 switch (Exp->getOpcode()) {
301 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
302 // See C99 6.6p3.
303 default:
304 if (Loc) *Loc = Exp->getOperatorLoc();
305 return false;
306 case UnaryOperator::Extension:
307 return true;
308 case UnaryOperator::SizeOf:
309 case UnaryOperator::AlignOf:
310 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
311 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Loc))
312 return false;
313
314 // FIXME: Evaluate sizeof/alignof.
315 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
316 Result = 1; // FIXME: Obviously bogus
317 break;
318 case UnaryOperator::LNot: {
319 bool Val = Result != 0;
320 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
321 Result = Val;
322 break;
323 }
324 case UnaryOperator::Plus:
325 // FIXME: Do usual unary promotions here!
326 break;
327 case UnaryOperator::Minus:
328 // FIXME: Do usual unary promotions here!
329 Result = -Result;
330 break;
331 case UnaryOperator::Not:
332 // FIXME: Do usual unary promotions here!
333 Result = ~Result;
334 break;
335 }
336 break;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000337 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000338 case SizeOfAlignOfTypeExprClass: {
339 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
340 // alignof always evaluates to a constant.
341 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Loc))
342 return false;
343
344 // FIXME: Evaluate sizeof/alignof.
345 Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL.
346 Result = 1; // FIXME: Obviously bogus
347 break;
348 }
349 case BinaryOperatorClass: {
350 const BinaryOperator *Exp = cast<BinaryOperator>(this);
351
352 // The LHS of a constant expr is always evaluated and needed.
353 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Loc, isEvaluated))
354 return false;
355
Chris Lattner23b7eb62007-06-15 23:05:46 +0000356 llvm::APSInt RHS(Result);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000357
358 // The short-circuiting &&/|| operators don't necessarily evaluate their
359 // RHS. Make sure to pass isEvaluated down correctly.
360 if (Exp->isLogicalOp()) {
361 bool RHSEval;
362 if (Exp->getOpcode() == BinaryOperator::LAnd)
363 RHSEval = Result != 0;
364 else {
365 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
366 RHSEval = Result == 0;
367 }
368
369 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Loc,
370 isEvaluated & RHSEval))
371 return false;
372 } else {
373 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Loc, isEvaluated))
374 return false;
375 }
376
377 // FIXME: These should all do the standard promotions, etc.
378 switch (Exp->getOpcode()) {
379 default:
380 if (Loc) *Loc = getLocStart();
381 return false;
382 case BinaryOperator::Mul:
383 Result *= RHS;
384 break;
385 case BinaryOperator::Div:
386 if (RHS == 0) {
387 if (!isEvaluated) break;
388 if (Loc) *Loc = getLocStart();
389 return false;
390 }
391 Result /= RHS;
392 break;
393 case BinaryOperator::Rem:
394 if (RHS == 0) {
395 if (!isEvaluated) break;
396 if (Loc) *Loc = getLocStart();
397 return false;
398 }
399 Result %= RHS;
400 break;
401 case BinaryOperator::Add: Result += RHS; break;
402 case BinaryOperator::Sub: Result -= RHS; break;
Chris Lattner901ae1f2007-06-08 21:54:26 +0000403 case BinaryOperator::Shl:
404 Result <<= RHS.getLimitedValue(Result.getBitWidth()-1);
405 break;
406 case BinaryOperator::Shr:
407 Result >>= RHS.getLimitedValue(Result.getBitWidth()-1);
408 break;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000409 case BinaryOperator::LT: Result = Result < RHS; break;
410 case BinaryOperator::GT: Result = Result > RHS; break;
411 case BinaryOperator::LE: Result = Result <= RHS; break;
412 case BinaryOperator::GE: Result = Result >= RHS; break;
413 case BinaryOperator::EQ: Result = Result == RHS; break;
414 case BinaryOperator::NE: Result = Result != RHS; break;
415 case BinaryOperator::And: Result &= RHS; break;
416 case BinaryOperator::Xor: Result ^= RHS; break;
417 case BinaryOperator::Or: Result |= RHS; break;
418 case BinaryOperator::LAnd:
419 Result = Result != 0 && RHS != 0;
420 break;
421 case BinaryOperator::LOr:
422 Result = Result != 0 || RHS != 0;
423 break;
424
425 case BinaryOperator::Comma:
426 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
427 // *except* when they are contained within a subexpression that is not
428 // evaluated". Note that Assignment can never happen due to constraints
429 // on the LHS subexpr, so we don't need to check it here.
430 if (isEvaluated) {
431 if (Loc) *Loc = getLocStart();
432 return false;
433 }
434
435 // The result of the constant expr is the RHS.
436 Result = RHS;
437 return true;
438 }
439
440 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
441 break;
442 }
443 case CastExprClass: {
444 const CastExpr *Exp = cast<CastExpr>(this);
445 // C99 6.6p6: shall only convert arithmetic types to integer types.
446 if (!Exp->getSubExpr()->getType()->isArithmeticType() ||
447 !Exp->getDestType()->isIntegerType()) {
448 if (Loc) *Loc = Exp->getSubExpr()->getLocStart();
449 return false;
450 }
451
452 // Handle simple integer->integer casts.
453 if (Exp->getSubExpr()->getType()->isIntegerType()) {
454 if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Loc, isEvaluated))
455 return false;
456 // FIXME: do the conversion on Result.
457 break;
458 }
459
460 // Allow floating constants that are the immediate operands of casts or that
461 // are parenthesized.
462 const Expr *Operand = Exp->getSubExpr();
463 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
464 Operand = PE->getSubExpr();
465
466 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) {
467 // FIXME: Evaluate this correctly!
468 Result = (int)FL->getValue();
469 break;
470 }
Chris Lattner6046e002007-06-05 06:39:23 +0000471 if (Loc) *Loc = Operand->getLocStart();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000472 return false;
473 }
474 case ConditionalOperatorClass: {
475 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
476
477 if (!Exp->getCond()->isIntegerConstantExpr(Result, Loc, isEvaluated))
478 return false;
479
480 const Expr *TrueExp = Exp->getLHS();
481 const Expr *FalseExp = Exp->getRHS();
482 if (Result == 0) std::swap(TrueExp, FalseExp);
483
484 // Evaluate the false one first, discard the result.
485 if (!FalseExp->isIntegerConstantExpr(Result, Loc, false))
486 return false;
487 // Evalute the true one, capture the result.
488 if (!TrueExp->isIntegerConstantExpr(Result, Loc, isEvaluated))
489 return false;
490 // FIXME: promotions on result.
491 break;
492 }
493 }
494
495 // Cases that are valid constant exprs fall through to here.
496 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
497 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000498}
499
Chris Lattner1f4479e2007-06-05 04:15:44 +0000500
Chris Lattner7eef9192007-05-24 01:23:49 +0000501/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
502/// integer constant expression with the value zero, or if this is one that is
503/// cast to void*.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000504bool Expr::isNullPointerConstant() const {
Chris Lattner7eef9192007-05-24 01:23:49 +0000505 // Strip off a cast to void*, if it exists.
506 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
507 // Check that it is a cast to void*.
508 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
509 QualType Pointee = PT->getPointeeType();
510 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
511 CE->getSubExpr()->getType()->isIntegerType()) // from int.
512 return CE->getSubExpr()->isNullPointerConstant();
Steve Naroffada7d422007-05-20 17:54:12 +0000513 }
Chris Lattner7eef9192007-05-24 01:23:49 +0000514 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
515 // Accept ((void*)0) as a null pointer constant, as many other
516 // implementations do.
517 return PE->getSubExpr()->isNullPointerConstant();
Chris Lattnere6b0f622007-05-21 05:58:35 +0000518 }
519
Chris Lattner7eef9192007-05-24 01:23:49 +0000520 // This expression must be an integer type.
521 if (!getType()->isIntegerType())
522 return false;
523
Chris Lattner1abbd412007-06-08 17:58:43 +0000524 // If we have an integer constant expression, we need to *evaluate* it and
525 // test for the value 0.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000526 llvm::APSInt Val(32);
Chris Lattner1abbd412007-06-08 17:58:43 +0000527 return isIntegerConstantExpr(Val, 0, true) && Val == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000528}