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 |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 194 | /// - reference type [C++ [expr]] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | /// |
Bill Wendling | ca51c97 | 2007-07-16 07:07:56 +0000 | [diff] [blame] | 196 | Expr::isLvalueResult Expr::isLvalue() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | // first, check the type (C99 6.3.2.1) |
Chris Lattner | cb4f9a6 | 2007-07-21 05:33:26 +0000 | [diff] [blame] | 198 | if (TR->isFunctionType()) // from isObjectType() |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | return LV_NotObjectType; |
| 200 | |
Steve Naroff | 731ec57 | 2007-07-21 13:32:03 +0000 | [diff] [blame] | 201 | if (TR->isVoidType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | return LV_IncompleteVoidType; |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 203 | |
Chris Lattner | cb4f9a6 | 2007-07-21 05:33:26 +0000 | [diff] [blame] | 204 | if (TR->isReferenceType()) // C++ [expr] |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 205 | return LV_Valid; |
| 206 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | // the type looks fine, now check the expression |
| 208 | switch (getStmtClass()) { |
| 209 | case StringLiteralClass: // C99 6.5.1p4 |
| 210 | case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) |
| 211 | // For vectors, make sure base is an lvalue (i.e. not a function call). |
| 212 | if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) |
| 213 | return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(); |
| 214 | return LV_Valid; |
| 215 | case DeclRefExprClass: // C99 6.5.1p2 |
| 216 | if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) |
| 217 | return LV_Valid; |
| 218 | break; |
Anton Korobeynikov | fdd7566 | 2007-07-12 15:26:50 +0000 | [diff] [blame] | 219 | case MemberExprClass: { // C99 6.5.2.3p4 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 220 | const MemberExpr *m = cast<MemberExpr>(this); |
| 221 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(); |
Anton Korobeynikov | fdd7566 | 2007-07-12 15:26:50 +0000 | [diff] [blame] | 222 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | case UnaryOperatorClass: // C99 6.5.3p4 |
| 224 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) |
| 225 | return LV_Valid; |
| 226 | break; |
| 227 | case ParenExprClass: // C99 6.5.1p5 |
| 228 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(); |
| 229 | default: |
| 230 | break; |
| 231 | } |
| 232 | return LV_InvalidExpression; |
| 233 | } |
| 234 | |
| 235 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 236 | /// does not have an incomplete type, does not have a const-qualified type, and |
| 237 | /// if it is a structure or union, does not have any member (including, |
| 238 | /// recursively, any member or element of all contained aggregates or unions) |
| 239 | /// with a const-qualified type. |
Bill Wendling | ca51c97 | 2007-07-16 07:07:56 +0000 | [diff] [blame] | 240 | Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | isLvalueResult lvalResult = isLvalue(); |
| 242 | |
| 243 | switch (lvalResult) { |
| 244 | case LV_Valid: break; |
| 245 | case LV_NotObjectType: return MLV_NotObjectType; |
| 246 | case LV_IncompleteVoidType: return MLV_IncompleteVoidType; |
| 247 | case LV_InvalidExpression: return MLV_InvalidExpression; |
| 248 | } |
| 249 | if (TR.isConstQualified()) |
| 250 | return MLV_ConstQualified; |
| 251 | if (TR->isArrayType()) |
| 252 | return MLV_ArrayType; |
| 253 | if (TR->isIncompleteType()) |
| 254 | return MLV_IncompleteType; |
| 255 | |
| 256 | if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) { |
| 257 | if (r->hasConstFields()) |
| 258 | return MLV_ConstQualified; |
| 259 | } |
| 260 | return MLV_Valid; |
| 261 | } |
| 262 | |
| 263 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 264 | /// an integer constant expression. Note: With the introduction of VLA's in |
| 265 | /// C99 the result of the sizeof operator is no longer always a constant |
| 266 | /// expression. The generalization of the wording to include any subexpression |
| 267 | /// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions |
| 268 | /// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, |
| 269 | /// "0 || f()" can be treated as a constant expression. In C90 this expression, |
| 270 | /// occurring in a context requiring a constant, would have been a constraint |
| 271 | /// violation. FIXME: This routine currently implements C90 semantics. |
| 272 | /// To properly implement C99 semantics this routine will need to evaluate |
| 273 | /// expressions involving operators previously mentioned. |
| 274 | |
| 275 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 276 | /// comma, etc |
| 277 | /// |
| 278 | /// FIXME: This should ext-warn on overflow during evaluation! ISO C does not |
| 279 | /// permit this. |
Chris Lattner | ce0afc0 | 2007-07-18 05:21:20 +0000 | [diff] [blame] | 280 | /// |
| 281 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 282 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 283 | /// cast+dereference. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 284 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 285 | SourceLocation *Loc, bool isEvaluated) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | switch (getStmtClass()) { |
| 287 | default: |
| 288 | if (Loc) *Loc = getLocStart(); |
| 289 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | case ParenExprClass: |
| 291 | return cast<ParenExpr>(this)->getSubExpr()-> |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 292 | isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 293 | case IntegerLiteralClass: |
| 294 | Result = cast<IntegerLiteral>(this)->getValue(); |
| 295 | break; |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 296 | case CharacterLiteralClass: { |
| 297 | const CharacterLiteral *CL = cast<CharacterLiteral>(this); |
| 298 | Result.zextOrTrunc(Ctx.getTypeSize(getType(), CL->getLoc())); |
| 299 | Result = CL->getValue(); |
Chris Lattner | f0fbcb3 | 2007-07-16 21:04:56 +0000 | [diff] [blame] | 300 | Result.setIsUnsigned(!getType()->isSignedIntegerType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 301 | break; |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 302 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 303 | case DeclRefExprClass: |
| 304 | if (const EnumConstantDecl *D = |
| 305 | dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { |
| 306 | Result = D->getInitVal(); |
| 307 | break; |
| 308 | } |
| 309 | if (Loc) *Loc = getLocStart(); |
| 310 | return false; |
| 311 | case UnaryOperatorClass: { |
| 312 | const UnaryOperator *Exp = cast<UnaryOperator>(this); |
| 313 | |
| 314 | // Get the operand value. If this is sizeof/alignof, do not evalute the |
| 315 | // operand. This affects C99 6.6p3. |
| 316 | if (Exp->isSizeOfAlignOfOp()) isEvaluated = false; |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 317 | if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx,Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 318 | return false; |
| 319 | |
| 320 | switch (Exp->getOpcode()) { |
| 321 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 322 | // See C99 6.6p3. |
| 323 | default: |
| 324 | if (Loc) *Loc = Exp->getOperatorLoc(); |
| 325 | return false; |
| 326 | case UnaryOperator::Extension: |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 327 | return true; // FIXME: this is wrong. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | case UnaryOperator::SizeOf: |
| 329 | case UnaryOperator::AlignOf: |
| 330 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 331 | if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 332 | return false; |
| 333 | |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 334 | // Return the result in the right width. |
| 335 | Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())); |
| 336 | |
| 337 | // Get information about the size or align. |
| 338 | if (Exp->getOpcode() == UnaryOperator::SizeOf) |
| 339 | Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(), |
| 340 | Exp->getOperatorLoc()); |
| 341 | else |
| 342 | Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(), |
| 343 | Exp->getOperatorLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 344 | break; |
| 345 | case UnaryOperator::LNot: { |
| 346 | bool Val = Result != 0; |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 347 | Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 348 | Result = Val; |
| 349 | break; |
| 350 | } |
| 351 | case UnaryOperator::Plus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | break; |
| 353 | case UnaryOperator::Minus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 354 | Result = -Result; |
| 355 | break; |
| 356 | case UnaryOperator::Not: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 357 | Result = ~Result; |
| 358 | break; |
| 359 | } |
| 360 | break; |
| 361 | } |
| 362 | case SizeOfAlignOfTypeExprClass: { |
| 363 | const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this); |
| 364 | // alignof always evaluates to a constant. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 365 | if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | return false; |
| 367 | |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 368 | // Return the result in the right width. |
| 369 | Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())); |
| 370 | |
| 371 | // Get information about the size or align. |
| 372 | if (Exp->isSizeOf()) |
| 373 | Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc()); |
| 374 | else |
| 375 | Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | case BinaryOperatorClass: { |
| 379 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
| 380 | |
| 381 | // The LHS of a constant expr is always evaluated and needed. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 382 | if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | return false; |
| 384 | |
| 385 | llvm::APSInt RHS(Result); |
| 386 | |
| 387 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 388 | // RHS. Make sure to pass isEvaluated down correctly. |
| 389 | if (Exp->isLogicalOp()) { |
| 390 | bool RHSEval; |
| 391 | if (Exp->getOpcode() == BinaryOperator::LAnd) |
| 392 | RHSEval = Result != 0; |
| 393 | else { |
| 394 | assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); |
| 395 | RHSEval = Result == 0; |
| 396 | } |
| 397 | |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 398 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 399 | isEvaluated & RHSEval)) |
| 400 | return false; |
| 401 | } else { |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 402 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 403 | return false; |
| 404 | } |
| 405 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | switch (Exp->getOpcode()) { |
| 407 | default: |
| 408 | if (Loc) *Loc = getLocStart(); |
| 409 | return false; |
| 410 | case BinaryOperator::Mul: |
| 411 | Result *= RHS; |
| 412 | break; |
| 413 | case BinaryOperator::Div: |
| 414 | if (RHS == 0) { |
| 415 | if (!isEvaluated) break; |
| 416 | if (Loc) *Loc = getLocStart(); |
| 417 | return false; |
| 418 | } |
| 419 | Result /= RHS; |
| 420 | break; |
| 421 | case BinaryOperator::Rem: |
| 422 | if (RHS == 0) { |
| 423 | if (!isEvaluated) break; |
| 424 | if (Loc) *Loc = getLocStart(); |
| 425 | return false; |
| 426 | } |
| 427 | Result %= RHS; |
| 428 | break; |
| 429 | case BinaryOperator::Add: Result += RHS; break; |
| 430 | case BinaryOperator::Sub: Result -= RHS; break; |
| 431 | case BinaryOperator::Shl: |
| 432 | Result <<= RHS.getLimitedValue(Result.getBitWidth()-1); |
| 433 | break; |
| 434 | case BinaryOperator::Shr: |
| 435 | Result >>= RHS.getLimitedValue(Result.getBitWidth()-1); |
| 436 | break; |
| 437 | case BinaryOperator::LT: Result = Result < RHS; break; |
| 438 | case BinaryOperator::GT: Result = Result > RHS; break; |
| 439 | case BinaryOperator::LE: Result = Result <= RHS; break; |
| 440 | case BinaryOperator::GE: Result = Result >= RHS; break; |
| 441 | case BinaryOperator::EQ: Result = Result == RHS; break; |
| 442 | case BinaryOperator::NE: Result = Result != RHS; break; |
| 443 | case BinaryOperator::And: Result &= RHS; break; |
| 444 | case BinaryOperator::Xor: Result ^= RHS; break; |
| 445 | case BinaryOperator::Or: Result |= RHS; break; |
| 446 | case BinaryOperator::LAnd: |
| 447 | Result = Result != 0 && RHS != 0; |
| 448 | break; |
| 449 | case BinaryOperator::LOr: |
| 450 | Result = Result != 0 || RHS != 0; |
| 451 | break; |
| 452 | |
| 453 | case BinaryOperator::Comma: |
| 454 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 455 | // *except* when they are contained within a subexpression that is not |
| 456 | // evaluated". Note that Assignment can never happen due to constraints |
| 457 | // on the LHS subexpr, so we don't need to check it here. |
| 458 | if (isEvaluated) { |
| 459 | if (Loc) *Loc = getLocStart(); |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | // The result of the constant expr is the RHS. |
| 464 | Result = RHS; |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); |
| 469 | break; |
| 470 | } |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 471 | case ImplicitCastExprClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 472 | case CastExprClass: { |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 473 | const Expr *SubExpr; |
| 474 | SourceLocation CastLoc; |
| 475 | if (const CastExpr *C = dyn_cast<CastExpr>(this)) { |
| 476 | SubExpr = C->getSubExpr(); |
| 477 | CastLoc = C->getLParenLoc(); |
| 478 | } else { |
| 479 | SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr(); |
| 480 | CastLoc = getLocStart(); |
| 481 | } |
| 482 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 483 | // C99 6.6p6: shall only convert arithmetic types to integer types. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 484 | if (!SubExpr->getType()->isArithmeticType() || |
| 485 | !getType()->isIntegerType()) { |
| 486 | if (Loc) *Loc = SubExpr->getLocStart(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | |
| 490 | // Handle simple integer->integer casts. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 491 | if (SubExpr->getType()->isIntegerType()) { |
| 492 | if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 493 | return false; |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 494 | |
| 495 | // Figure out if this is a truncate, extend or noop cast. |
| 496 | unsigned DestWidth = Ctx.getTypeSize(getType(), CastLoc); |
| 497 | |
| 498 | // If the input is signed, do a sign extend, noop, or truncate. |
| 499 | if (SubExpr->getType()->isSignedIntegerType()) |
| 500 | Result.sextOrTrunc(DestWidth); |
| 501 | else // If the input is unsigned, do a zero extend, noop, or truncate. |
| 502 | Result.zextOrTrunc(DestWidth); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | |
| 506 | // Allow floating constants that are the immediate operands of casts or that |
| 507 | // are parenthesized. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 508 | const Expr *Operand = SubExpr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 509 | while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) |
| 510 | Operand = PE->getSubExpr(); |
| 511 | |
| 512 | if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) { |
| 513 | // FIXME: Evaluate this correctly! |
| 514 | Result = (int)FL->getValue(); |
| 515 | break; |
| 516 | } |
| 517 | if (Loc) *Loc = Operand->getLocStart(); |
| 518 | return false; |
| 519 | } |
| 520 | case ConditionalOperatorClass: { |
| 521 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
| 522 | |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 523 | if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 524 | return false; |
| 525 | |
| 526 | const Expr *TrueExp = Exp->getLHS(); |
| 527 | const Expr *FalseExp = Exp->getRHS(); |
| 528 | if (Result == 0) std::swap(TrueExp, FalseExp); |
| 529 | |
| 530 | // Evaluate the false one first, discard the result. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 531 | if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | return false; |
| 533 | // Evalute the true one, capture the result. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 534 | if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 535 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // Cases that are valid constant exprs fall through to here. |
| 541 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 547 | /// integer constant expression with the value zero, or if this is one that is |
| 548 | /// cast to void*. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 549 | bool Expr::isNullPointerConstant(ASTContext &Ctx) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 550 | // Strip off a cast to void*, if it exists. |
| 551 | if (const CastExpr *CE = dyn_cast<CastExpr>(this)) { |
| 552 | // Check that it is a cast to void*. |
| 553 | if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) { |
| 554 | QualType Pointee = PT->getPointeeType(); |
| 555 | if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void* |
| 556 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 557 | return CE->getSubExpr()->isNullPointerConstant(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 558 | } |
| 559 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 560 | // Accept ((void*)0) as a null pointer constant, as many other |
| 561 | // implementations do. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 562 | return PE->getSubExpr()->isNullPointerConstant(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // This expression must be an integer type. |
| 566 | if (!getType()->isIntegerType()) |
| 567 | return false; |
| 568 | |
| 569 | // If we have an integer constant expression, we need to *evaluate* it and |
| 570 | // test for the value 0. |
| 571 | llvm::APSInt Val(32); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 572 | return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 573 | } |