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