Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- 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" |
| 15 | #include "clang/AST/StmtVisitor.h" |
| 16 | #include "clang/Lex/IdentifierTable.h" |
| 17 | using namespace clang; |
| 18 | |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | // Primary Expressions. |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | StringLiteral::StringLiteral(const char *strData, unsigned byteLength, |
| 24 | bool Wide, QualType t, SourceLocation firstLoc, |
| 25 | SourceLocation lastLoc) : |
| 26 | Expr(StringLiteralClass, t) { |
| 27 | // OPTIMIZE: could allocate this appended to the StringLiteral. |
| 28 | char *AStrData = new char[byteLength]; |
| 29 | memcpy(AStrData, strData, byteLength); |
| 30 | StrData = AStrData; |
| 31 | ByteLength = byteLength; |
| 32 | IsWide = Wide; |
| 33 | firstTokLoc = firstLoc; |
| 34 | lastTokLoc = lastLoc; |
| 35 | } |
| 36 | |
| 37 | StringLiteral::~StringLiteral() { |
| 38 | delete[] StrData; |
| 39 | } |
| 40 | |
| 41 | bool UnaryOperator::isPostfix(Opcode Op) { |
| 42 | switch (Op) { |
| 43 | case PostInc: |
| 44 | case PostDec: |
| 45 | return true; |
| 46 | default: |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 52 | /// corresponds to, e.g. "sizeof" or "[pre]++". |
| 53 | const char *UnaryOperator::getOpcodeStr(Opcode Op) { |
| 54 | switch (Op) { |
| 55 | default: assert(0 && "Unknown unary operator"); |
| 56 | case PostInc: return "++"; |
| 57 | case PostDec: return "--"; |
| 58 | case PreInc: return "++"; |
| 59 | case PreDec: return "--"; |
| 60 | 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"; |
| 68 | case SizeOf: return "sizeof"; |
| 69 | case AlignOf: return "alignof"; |
| 70 | case Extension: return "__extension__"; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | // Postfix Operators. |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | |
| 78 | CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, |
| 79 | SourceLocation rparenloc) |
| 80 | : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) { |
| 81 | Args = new Expr*[numargs]; |
| 82 | for (unsigned i = 0; i != numargs; ++i) |
| 83 | Args[i] = args[i]; |
| 84 | RParenLoc = rparenloc; |
| 85 | } |
| 86 | |
| 87 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 88 | /// corresponds to, e.g. "<<=". |
| 89 | const 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 | } |
| 124 | |
| 125 | |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | // Generic Expression Routines |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
| 130 | /// hasLocalSideEffect - Return true if this immediate expression has side |
| 131 | /// effects, not counting any sub-expressions. |
| 132 | bool 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 | 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 | |
| 157 | case UnaryOperator::Extension: |
| 158 | return UO->getSubExpr()->hasLocalSideEffect(); |
| 159 | } |
| 160 | } |
| 161 | case BinaryOperatorClass: |
| 162 | return cast<BinaryOperator>(this)->isAssignmentOp(); |
| 163 | |
| 164 | case MemberExprClass: |
| 165 | case ArraySubscriptExprClass: |
| 166 | // If the base pointer or element is to a volatile pointer/field, accessing |
| 167 | // if is a side effect. |
| 168 | return getType().isVolatileQualified(); |
| 169 | |
| 170 | case CallExprClass: |
| 171 | // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }" |
| 172 | // should warn. |
| 173 | return true; |
| 174 | |
| 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 | |
| 184 | /// 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: |
| 186 | /// - 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 |
| 191 | /// - *e, the type of e cannot be a function type |
| 192 | /// - string-constant |
| 193 | /// |
| 194 | Expr::isLvalueResult Expr::isLvalue() { |
| 195 | // first, check the type (C99 6.3.2.1) |
| 196 | if (isa<FunctionType>(TR.getCanonicalType())) // from isObjectType() |
| 197 | return LV_NotObjectType; |
| 198 | |
| 199 | if (TR->isIncompleteType() && TR->isVoidType()) |
| 200 | return LV_IncompleteVoidType; |
| 201 | |
| 202 | // the type looks fine, now check the expression |
| 203 | switch (getStmtClass()) { |
| 204 | case StringLiteralClass: // C99 6.5.1p4 |
| 205 | case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) |
| 206 | // For vectors, make sure base is an lvalue (i.e. not a function call). |
| 207 | if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) |
| 208 | return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(); |
| 209 | return LV_Valid; |
| 210 | case DeclRefExprClass: // C99 6.5.1p2 |
| 211 | if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) |
| 212 | return LV_Valid; |
| 213 | break; |
Anton Korobeynikov | fdd7566 | 2007-07-12 15:26:50 +0000 | [diff] [blame] | 214 | case MemberExprClass: { // C99 6.5.2.3p4 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 215 | const MemberExpr *m = cast<MemberExpr>(this); |
| 216 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(); |
Anton Korobeynikov | fdd7566 | 2007-07-12 15:26:50 +0000 | [diff] [blame] | 217 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 218 | case UnaryOperatorClass: // C99 6.5.3p4 |
| 219 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) |
| 220 | return LV_Valid; |
| 221 | break; |
| 222 | case ParenExprClass: // C99 6.5.1p5 |
| 223 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(); |
| 224 | default: |
| 225 | break; |
| 226 | } |
| 227 | return LV_InvalidExpression; |
| 228 | } |
| 229 | |
| 230 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 231 | /// does not have an incomplete type, does not have a const-qualified type, and |
| 232 | /// if it is a structure or union, does not have any member (including, |
| 233 | /// recursively, any member or element of all contained aggregates or unions) |
| 234 | /// with a const-qualified type. |
| 235 | Expr::isModifiableLvalueResult Expr::isModifiableLvalue() { |
| 236 | isLvalueResult lvalResult = isLvalue(); |
| 237 | |
| 238 | switch (lvalResult) { |
| 239 | case LV_Valid: break; |
| 240 | case LV_NotObjectType: return MLV_NotObjectType; |
| 241 | case LV_IncompleteVoidType: return MLV_IncompleteVoidType; |
| 242 | case LV_InvalidExpression: return MLV_InvalidExpression; |
| 243 | } |
| 244 | if (TR.isConstQualified()) |
| 245 | return MLV_ConstQualified; |
| 246 | if (TR->isArrayType()) |
| 247 | return MLV_ArrayType; |
| 248 | if (TR->isIncompleteType()) |
| 249 | return MLV_IncompleteType; |
| 250 | |
| 251 | if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) { |
| 252 | if (r->hasConstFields()) |
| 253 | return MLV_ConstQualified; |
| 254 | } |
| 255 | return MLV_Valid; |
| 256 | } |
| 257 | |
| 258 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 259 | /// an integer constant expression. Note: With the introduction of VLA's in |
| 260 | /// C99 the result of the sizeof operator is no longer always a constant |
| 261 | /// expression. The generalization of the wording to include any subexpression |
| 262 | /// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions |
| 263 | /// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, |
| 264 | /// "0 || f()" can be treated as a constant expression. In C90 this expression, |
| 265 | /// occurring in a context requiring a constant, would have been a constraint |
| 266 | /// violation. FIXME: This routine currently implements C90 semantics. |
| 267 | /// To properly implement C99 semantics this routine will need to evaluate |
| 268 | /// expressions involving operators previously mentioned. |
| 269 | |
| 270 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 271 | /// comma, etc |
| 272 | /// |
| 273 | /// FIXME: This should ext-warn on overflow during evaluation! ISO C does not |
| 274 | /// permit this. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 275 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 276 | SourceLocation *Loc, bool isEvaluated) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | switch (getStmtClass()) { |
| 278 | default: |
| 279 | if (Loc) *Loc = getLocStart(); |
| 280 | return false; |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 281 | case ImplicitCastExprClass: |
| 282 | return cast<ImplicitCastExpr>(this)->getSubExpr()-> |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 283 | isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | case ParenExprClass: |
| 285 | return cast<ParenExpr>(this)->getSubExpr()-> |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 286 | isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 287 | case IntegerLiteralClass: |
| 288 | Result = cast<IntegerLiteral>(this)->getValue(); |
| 289 | break; |
| 290 | case CharacterLiteralClass: |
| 291 | // FIXME: This doesn't set the right width etc. |
| 292 | Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL. |
| 293 | Result = cast<CharacterLiteral>(this)->getValue(); |
| 294 | break; |
| 295 | case DeclRefExprClass: |
| 296 | if (const EnumConstantDecl *D = |
| 297 | dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { |
| 298 | Result = D->getInitVal(); |
| 299 | break; |
| 300 | } |
| 301 | if (Loc) *Loc = getLocStart(); |
| 302 | return false; |
| 303 | case UnaryOperatorClass: { |
| 304 | const UnaryOperator *Exp = cast<UnaryOperator>(this); |
| 305 | |
| 306 | // Get the operand value. If this is sizeof/alignof, do not evalute the |
| 307 | // operand. This affects C99 6.6p3. |
| 308 | if (Exp->isSizeOfAlignOfOp()) isEvaluated = false; |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 309 | if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx,Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | return false; |
| 311 | |
| 312 | switch (Exp->getOpcode()) { |
| 313 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 314 | // See C99 6.6p3. |
| 315 | default: |
| 316 | if (Loc) *Loc = Exp->getOperatorLoc(); |
| 317 | return false; |
| 318 | case UnaryOperator::Extension: |
| 319 | return true; |
| 320 | case UnaryOperator::SizeOf: |
| 321 | case UnaryOperator::AlignOf: |
| 322 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 323 | if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | return false; |
| 325 | |
| 326 | // FIXME: Evaluate sizeof/alignof. |
| 327 | Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL. |
| 328 | Result = 1; // FIXME: Obviously bogus |
| 329 | break; |
| 330 | case UnaryOperator::LNot: { |
| 331 | bool Val = Result != 0; |
| 332 | Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL. |
| 333 | Result = Val; |
| 334 | break; |
| 335 | } |
| 336 | case UnaryOperator::Plus: |
| 337 | // FIXME: Do usual unary promotions here! |
| 338 | break; |
| 339 | case UnaryOperator::Minus: |
| 340 | // FIXME: Do usual unary promotions here! |
| 341 | Result = -Result; |
| 342 | break; |
| 343 | case UnaryOperator::Not: |
| 344 | // FIXME: Do usual unary promotions here! |
| 345 | Result = ~Result; |
| 346 | break; |
| 347 | } |
| 348 | break; |
| 349 | } |
| 350 | case SizeOfAlignOfTypeExprClass: { |
| 351 | const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this); |
| 352 | // alignof always evaluates to a constant. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 353 | if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 354 | return false; |
| 355 | |
| 356 | // FIXME: Evaluate sizeof/alignof. |
| 357 | Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL. |
| 358 | Result = 1; // FIXME: Obviously bogus |
| 359 | break; |
| 360 | } |
| 361 | case BinaryOperatorClass: { |
| 362 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
| 363 | |
| 364 | // The LHS of a constant expr is always evaluated and needed. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 365 | if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | return false; |
| 367 | |
| 368 | llvm::APSInt RHS(Result); |
| 369 | |
| 370 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 371 | // RHS. Make sure to pass isEvaluated down correctly. |
| 372 | if (Exp->isLogicalOp()) { |
| 373 | bool RHSEval; |
| 374 | if (Exp->getOpcode() == BinaryOperator::LAnd) |
| 375 | RHSEval = Result != 0; |
| 376 | else { |
| 377 | assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); |
| 378 | RHSEval = Result == 0; |
| 379 | } |
| 380 | |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 381 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 382 | isEvaluated & RHSEval)) |
| 383 | return false; |
| 384 | } else { |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 385 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
| 389 | // FIXME: These should all do the standard promotions, etc. |
| 390 | switch (Exp->getOpcode()) { |
| 391 | default: |
| 392 | if (Loc) *Loc = getLocStart(); |
| 393 | return false; |
| 394 | case BinaryOperator::Mul: |
| 395 | Result *= RHS; |
| 396 | break; |
| 397 | case BinaryOperator::Div: |
| 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::Rem: |
| 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::Add: Result += RHS; break; |
| 414 | case BinaryOperator::Sub: Result -= RHS; break; |
| 415 | case BinaryOperator::Shl: |
| 416 | Result <<= RHS.getLimitedValue(Result.getBitWidth()-1); |
| 417 | break; |
| 418 | case BinaryOperator::Shr: |
| 419 | Result >>= RHS.getLimitedValue(Result.getBitWidth()-1); |
| 420 | break; |
| 421 | case BinaryOperator::LT: Result = Result < RHS; break; |
| 422 | case BinaryOperator::GT: Result = Result > RHS; break; |
| 423 | case BinaryOperator::LE: Result = Result <= RHS; break; |
| 424 | case BinaryOperator::GE: Result = Result >= RHS; break; |
| 425 | case BinaryOperator::EQ: Result = Result == RHS; break; |
| 426 | case BinaryOperator::NE: Result = Result != RHS; break; |
| 427 | case BinaryOperator::And: Result &= RHS; break; |
| 428 | case BinaryOperator::Xor: Result ^= RHS; break; |
| 429 | case BinaryOperator::Or: Result |= RHS; break; |
| 430 | case BinaryOperator::LAnd: |
| 431 | Result = Result != 0 && RHS != 0; |
| 432 | break; |
| 433 | case BinaryOperator::LOr: |
| 434 | Result = Result != 0 || RHS != 0; |
| 435 | break; |
| 436 | |
| 437 | case BinaryOperator::Comma: |
| 438 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 439 | // *except* when they are contained within a subexpression that is not |
| 440 | // evaluated". Note that Assignment can never happen due to constraints |
| 441 | // on the LHS subexpr, so we don't need to check it here. |
| 442 | if (isEvaluated) { |
| 443 | if (Loc) *Loc = getLocStart(); |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | // The result of the constant expr is the RHS. |
| 448 | Result = RHS; |
| 449 | return true; |
| 450 | } |
| 451 | |
| 452 | assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); |
| 453 | break; |
| 454 | } |
| 455 | case CastExprClass: { |
| 456 | const CastExpr *Exp = cast<CastExpr>(this); |
| 457 | // C99 6.6p6: shall only convert arithmetic types to integer types. |
| 458 | if (!Exp->getSubExpr()->getType()->isArithmeticType() || |
| 459 | !Exp->getDestType()->isIntegerType()) { |
| 460 | if (Loc) *Loc = Exp->getSubExpr()->getLocStart(); |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | // Handle simple integer->integer casts. |
| 465 | if (Exp->getSubExpr()->getType()->isIntegerType()) { |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 466 | if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, |
| 467 | Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 468 | return false; |
| 469 | // FIXME: do the conversion on Result. |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | // Allow floating constants that are the immediate operands of casts or that |
| 474 | // are parenthesized. |
| 475 | const Expr *Operand = Exp->getSubExpr(); |
| 476 | while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) |
| 477 | Operand = PE->getSubExpr(); |
| 478 | |
| 479 | if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) { |
| 480 | // FIXME: Evaluate this correctly! |
| 481 | Result = (int)FL->getValue(); |
| 482 | break; |
| 483 | } |
| 484 | if (Loc) *Loc = Operand->getLocStart(); |
| 485 | return false; |
| 486 | } |
| 487 | case ConditionalOperatorClass: { |
| 488 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
| 489 | |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 490 | if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 491 | return false; |
| 492 | |
| 493 | const Expr *TrueExp = Exp->getLHS(); |
| 494 | const Expr *FalseExp = Exp->getRHS(); |
| 495 | if (Result == 0) std::swap(TrueExp, FalseExp); |
| 496 | |
| 497 | // Evaluate the false one first, discard the result. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 498 | if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 499 | return false; |
| 500 | // Evalute the true one, capture the result. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 501 | if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 502 | return false; |
| 503 | // FIXME: promotions on result. |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // Cases that are valid constant exprs fall through to here. |
| 509 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
| 510 | return true; |
| 511 | } |
| 512 | |
| 513 | |
| 514 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 515 | /// integer constant expression with the value zero, or if this is one that is |
| 516 | /// cast to void*. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 517 | bool Expr::isNullPointerConstant(ASTContext &Ctx) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 518 | // Strip off a cast to void*, if it exists. |
| 519 | if (const CastExpr *CE = dyn_cast<CastExpr>(this)) { |
| 520 | // Check that it is a cast to void*. |
| 521 | if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) { |
| 522 | QualType Pointee = PT->getPointeeType(); |
| 523 | if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void* |
| 524 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 525 | return CE->getSubExpr()->isNullPointerConstant(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 526 | } |
| 527 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 528 | // Accept ((void*)0) as a null pointer constant, as many other |
| 529 | // implementations do. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 530 | return PE->getSubExpr()->isNullPointerConstant(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // This expression must be an integer type. |
| 534 | if (!getType()->isIntegerType()) |
| 535 | return false; |
| 536 | |
| 537 | // If we have an integer constant expression, we need to *evaluate* it and |
| 538 | // test for the value 0. |
| 539 | llvm::APSInt Val(32); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame^] | 540 | return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 541 | } |