Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Expr.cpp - Expression AST Node Implementation --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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/ASTContext.h" |
| 16 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 2fd1c65 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | d9ffbc9 | 2007-11-27 18:22:04 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // Primary Expressions. |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | StringLiteral::StringLiteral(const char *strData, unsigned byteLength, |
| 26 | bool Wide, QualType t, SourceLocation firstLoc, |
| 27 | SourceLocation lastLoc) : |
| 28 | Expr(StringLiteralClass, t) { |
| 29 | // OPTIMIZE: could allocate this appended to the StringLiteral. |
| 30 | char *AStrData = new char[byteLength]; |
| 31 | memcpy(AStrData, strData, byteLength); |
| 32 | StrData = AStrData; |
| 33 | ByteLength = byteLength; |
| 34 | IsWide = Wide; |
| 35 | firstTokLoc = firstLoc; |
| 36 | lastTokLoc = lastLoc; |
| 37 | } |
| 38 | |
| 39 | StringLiteral::~StringLiteral() { |
| 40 | delete[] StrData; |
| 41 | } |
| 42 | |
| 43 | bool UnaryOperator::isPostfix(Opcode Op) { |
| 44 | switch (Op) { |
| 45 | case PostInc: |
| 46 | case PostDec: |
| 47 | return true; |
| 48 | default: |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 54 | /// corresponds to, e.g. "sizeof" or "[pre]++". |
| 55 | const char *UnaryOperator::getOpcodeStr(Opcode Op) { |
| 56 | switch (Op) { |
| 57 | default: assert(0 && "Unknown unary operator"); |
| 58 | case PostInc: return "++"; |
| 59 | case PostDec: return "--"; |
| 60 | case PreInc: return "++"; |
| 61 | case PreDec: return "--"; |
| 62 | case AddrOf: return "&"; |
| 63 | case Deref: return "*"; |
| 64 | case Plus: return "+"; |
| 65 | case Minus: return "-"; |
| 66 | case Not: return "~"; |
| 67 | case LNot: return "!"; |
| 68 | case Real: return "__real"; |
| 69 | case Imag: return "__imag"; |
| 70 | case SizeOf: return "sizeof"; |
| 71 | case AlignOf: return "alignof"; |
| 72 | case Extension: return "__extension__"; |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 73 | case OffsetOf: return "__builtin_offsetof"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | // Postfix Operators. |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 82 | CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, |
| 83 | SourceLocation rparenloc) |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 84 | : Expr(CallExprClass, t), NumArgs(numargs) { |
| 85 | SubExprs = new Expr*[numargs+1]; |
| 86 | SubExprs[FN] = fn; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 87 | for (unsigned i = 0; i != numargs; ++i) |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 88 | SubExprs[i+ARGS_START] = args[i]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 89 | RParenLoc = rparenloc; |
| 90 | } |
| 91 | |
Chris Lattner | c257c0d | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 92 | /// setNumArgs - This changes the number of arguments present in this call. |
| 93 | /// Any orphaned expressions are deleted by this, and any new operands are set |
| 94 | /// to null. |
| 95 | void CallExpr::setNumArgs(unsigned NumArgs) { |
| 96 | // No change, just return. |
| 97 | if (NumArgs == getNumArgs()) return; |
| 98 | |
| 99 | // If shrinking # arguments, just delete the extras and forgot them. |
| 100 | if (NumArgs < getNumArgs()) { |
| 101 | for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i) |
| 102 | delete getArg(i); |
| 103 | this->NumArgs = NumArgs; |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | // Otherwise, we are growing the # arguments. New an bigger argument array. |
| 108 | Expr **NewSubExprs = new Expr*[NumArgs+1]; |
| 109 | // Copy over args. |
| 110 | for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i) |
| 111 | NewSubExprs[i] = SubExprs[i]; |
| 112 | // Null out new args. |
| 113 | for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i) |
| 114 | NewSubExprs[i] = 0; |
| 115 | |
| 116 | delete[] SubExprs; |
| 117 | SubExprs = NewSubExprs; |
| 118 | this->NumArgs = NumArgs; |
| 119 | } |
| 120 | |
Steve Naroff | 44aec4c | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 121 | bool CallExpr::isBuiltinConstantExpr() const { |
| 122 | // All simple function calls (e.g. func()) are implicitly cast to pointer to |
| 123 | // function. As a result, we try and obtain the DeclRefExpr from the |
| 124 | // ImplicitCastExpr. |
| 125 | const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); |
| 126 | if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). |
| 127 | return false; |
| 128 | |
| 129 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); |
| 130 | if (!DRE) |
| 131 | return false; |
| 132 | |
Anders Carlsson | 2ed959f | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 133 | const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 134 | if (!FDecl) |
| 135 | return false; |
| 136 | |
| 137 | unsigned builtinID = FDecl->getIdentifier()->getBuiltinID(); |
| 138 | if (!builtinID) |
| 139 | return false; |
| 140 | |
| 141 | // We have a builtin that is a constant expression |
| 142 | if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString) |
Steve Naroff | 44aec4c | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 143 | return true; |
| 144 | return false; |
| 145 | } |
Chris Lattner | c257c0d | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 146 | |
Steve Naroff | 8d3b170 | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 147 | bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const { |
| 148 | // The following enum mimics gcc's internal "typeclass.h" file. |
| 149 | enum gcc_type_class { |
| 150 | no_type_class = -1, |
| 151 | void_type_class, integer_type_class, char_type_class, |
| 152 | enumeral_type_class, boolean_type_class, |
| 153 | pointer_type_class, reference_type_class, offset_type_class, |
| 154 | real_type_class, complex_type_class, |
| 155 | function_type_class, method_type_class, |
| 156 | record_type_class, union_type_class, |
| 157 | array_type_class, string_type_class, |
| 158 | lang_type_class |
| 159 | }; |
| 160 | Result.setIsSigned(true); |
| 161 | |
| 162 | // All simple function calls (e.g. func()) are implicitly cast to pointer to |
| 163 | // function. As a result, we try and obtain the DeclRefExpr from the |
| 164 | // ImplicitCastExpr. |
| 165 | const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); |
| 166 | if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). |
| 167 | return false; |
| 168 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); |
| 169 | if (!DRE) |
| 170 | return false; |
| 171 | |
| 172 | // We have a DeclRefExpr. |
| 173 | if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) { |
| 174 | // If no argument was supplied, default to "no_type_class". This isn't |
| 175 | // ideal, however it's what gcc does. |
| 176 | Result = static_cast<uint64_t>(no_type_class); |
| 177 | if (NumArgs >= 1) { |
| 178 | QualType argType = getArg(0)->getType(); |
| 179 | |
| 180 | if (argType->isVoidType()) |
| 181 | Result = void_type_class; |
| 182 | else if (argType->isEnumeralType()) |
| 183 | Result = enumeral_type_class; |
| 184 | else if (argType->isBooleanType()) |
| 185 | Result = boolean_type_class; |
| 186 | else if (argType->isCharType()) |
| 187 | Result = string_type_class; // gcc doesn't appear to use char_type_class |
| 188 | else if (argType->isIntegerType()) |
| 189 | Result = integer_type_class; |
| 190 | else if (argType->isPointerType()) |
| 191 | Result = pointer_type_class; |
| 192 | else if (argType->isReferenceType()) |
| 193 | Result = reference_type_class; |
| 194 | else if (argType->isRealType()) |
| 195 | Result = real_type_class; |
| 196 | else if (argType->isComplexType()) |
| 197 | Result = complex_type_class; |
| 198 | else if (argType->isFunctionType()) |
| 199 | Result = function_type_class; |
| 200 | else if (argType->isStructureType()) |
| 201 | Result = record_type_class; |
| 202 | else if (argType->isUnionType()) |
| 203 | Result = union_type_class; |
| 204 | else if (argType->isArrayType()) |
| 205 | Result = array_type_class; |
| 206 | else if (argType->isUnionType()) |
| 207 | Result = union_type_class; |
| 208 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
Chris Lattner | 19b8f1a | 2007-11-08 17:56:40 +0000 | [diff] [blame] | 209 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Steve Naroff | 8d3b170 | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 216 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 217 | /// corresponds to, e.g. "<<=". |
| 218 | const char *BinaryOperator::getOpcodeStr(Opcode Op) { |
| 219 | switch (Op) { |
| 220 | default: assert(0 && "Unknown binary operator"); |
| 221 | case Mul: return "*"; |
| 222 | case Div: return "/"; |
| 223 | case Rem: return "%"; |
| 224 | case Add: return "+"; |
| 225 | case Sub: return "-"; |
| 226 | case Shl: return "<<"; |
| 227 | case Shr: return ">>"; |
| 228 | case LT: return "<"; |
| 229 | case GT: return ">"; |
| 230 | case LE: return "<="; |
| 231 | case GE: return ">="; |
| 232 | case EQ: return "=="; |
| 233 | case NE: return "!="; |
| 234 | case And: return "&"; |
| 235 | case Xor: return "^"; |
| 236 | case Or: return "|"; |
| 237 | case LAnd: return "&&"; |
| 238 | case LOr: return "||"; |
| 239 | case Assign: return "="; |
| 240 | case MulAssign: return "*="; |
| 241 | case DivAssign: return "/="; |
| 242 | case RemAssign: return "%="; |
| 243 | case AddAssign: return "+="; |
| 244 | case SubAssign: return "-="; |
| 245 | case ShlAssign: return "<<="; |
| 246 | case ShrAssign: return ">>="; |
| 247 | case AndAssign: return "&="; |
| 248 | case XorAssign: return "^="; |
| 249 | case OrAssign: return "|="; |
| 250 | case Comma: return ","; |
| 251 | } |
| 252 | } |
| 253 | |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 254 | InitListExpr::InitListExpr(SourceLocation lbraceloc, |
| 255 | Expr **initexprs, unsigned numinits, |
| 256 | SourceLocation rbraceloc) |
Steve Naroff | 2e33547 | 2008-05-01 02:04:18 +0000 | [diff] [blame] | 257 | : Expr(InitListExprClass, QualType()), |
| 258 | LBraceLoc(lbraceloc), RBraceLoc(rbraceloc) |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 259 | { |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 260 | for (unsigned i = 0; i != numinits; i++) |
Steve Naroff | 2e33547 | 2008-05-01 02:04:18 +0000 | [diff] [blame] | 261 | InitExprs.push_back(initexprs[i]); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 262 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 263 | |
| 264 | //===----------------------------------------------------------------------===// |
| 265 | // Generic Expression Routines |
| 266 | //===----------------------------------------------------------------------===// |
| 267 | |
| 268 | /// hasLocalSideEffect - Return true if this immediate expression has side |
| 269 | /// effects, not counting any sub-expressions. |
| 270 | bool Expr::hasLocalSideEffect() const { |
| 271 | switch (getStmtClass()) { |
| 272 | default: |
| 273 | return false; |
| 274 | case ParenExprClass: |
| 275 | return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect(); |
| 276 | case UnaryOperatorClass: { |
| 277 | const UnaryOperator *UO = cast<UnaryOperator>(this); |
| 278 | |
| 279 | switch (UO->getOpcode()) { |
| 280 | default: return false; |
| 281 | case UnaryOperator::PostInc: |
| 282 | case UnaryOperator::PostDec: |
| 283 | case UnaryOperator::PreInc: |
| 284 | case UnaryOperator::PreDec: |
| 285 | return true; // ++/-- |
| 286 | |
| 287 | case UnaryOperator::Deref: |
| 288 | // Dereferencing a volatile pointer is a side-effect. |
| 289 | return getType().isVolatileQualified(); |
| 290 | case UnaryOperator::Real: |
| 291 | case UnaryOperator::Imag: |
| 292 | // accessing a piece of a volatile complex is a side-effect. |
| 293 | return UO->getSubExpr()->getType().isVolatileQualified(); |
| 294 | |
| 295 | case UnaryOperator::Extension: |
| 296 | return UO->getSubExpr()->hasLocalSideEffect(); |
| 297 | } |
| 298 | } |
Chris Lattner | ef95ffd | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 299 | case BinaryOperatorClass: { |
| 300 | const BinaryOperator *BinOp = cast<BinaryOperator>(this); |
| 301 | // Consider comma to have side effects if the LHS and RHS both do. |
| 302 | if (BinOp->getOpcode() == BinaryOperator::Comma) |
| 303 | return BinOp->getLHS()->hasLocalSideEffect() && |
| 304 | BinOp->getRHS()->hasLocalSideEffect(); |
| 305 | |
| 306 | return BinOp->isAssignmentOp(); |
| 307 | } |
Chris Lattner | 06078d2 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 308 | case CompoundAssignOperatorClass: |
Chris Lattner | 9c0da3b | 2007-08-25 01:55:00 +0000 | [diff] [blame] | 309 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 310 | |
Fariborz Jahanian | 363c59b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 311 | case ConditionalOperatorClass: { |
| 312 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
| 313 | return Exp->getCond()->hasLocalSideEffect() |
| 314 | || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect()) |
| 315 | || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect()); |
| 316 | } |
| 317 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 318 | case MemberExprClass: |
| 319 | case ArraySubscriptExprClass: |
| 320 | // If the base pointer or element is to a volatile pointer/field, accessing |
| 321 | // if is a side effect. |
| 322 | return getType().isVolatileQualified(); |
| 323 | |
| 324 | case CallExprClass: |
| 325 | // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }" |
| 326 | // should warn. |
| 327 | return true; |
Chris Lattner | 99f5f0b | 2007-09-26 22:06:30 +0000 | [diff] [blame] | 328 | case ObjCMessageExprClass: |
| 329 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 330 | |
| 331 | case CastExprClass: |
| 332 | // If this is a cast to void, check the operand. Otherwise, the result of |
| 333 | // the cast is unused. |
| 334 | if (getType()->isVoidType()) |
| 335 | return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect(); |
| 336 | return false; |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 337 | |
| 338 | case CXXDefaultArgExprClass: |
| 339 | return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an |
| 344 | /// incomplete type other than void. Nonarray expressions that can be lvalues: |
| 345 | /// - name, where name must be a variable |
| 346 | /// - e[i] |
| 347 | /// - (e), where e must be an lvalue |
| 348 | /// - e.name, where e must be an lvalue |
| 349 | /// - e->name |
| 350 | /// - *e, the type of e cannot be a function type |
| 351 | /// - string-constant |
Chris Lattner | 5bf7202 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 352 | /// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 353 | /// - reference type [C++ [expr]] |
| 354 | /// |
| 355 | Expr::isLvalueResult Expr::isLvalue() const { |
| 356 | // first, check the type (C99 6.3.2.1) |
| 357 | if (TR->isFunctionType()) // from isObjectType() |
| 358 | return LV_NotObjectType; |
| 359 | |
Steve Naroff | ec7736d | 2008-02-10 01:39:04 +0000 | [diff] [blame] | 360 | // Allow qualified void which is an incomplete type other than void (yuck). |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 361 | if (TR->isVoidType() && !TR.getCanonicalType().getCVRQualifiers()) |
Steve Naroff | ec7736d | 2008-02-10 01:39:04 +0000 | [diff] [blame] | 362 | return LV_IncompleteVoidType; |
| 363 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 364 | if (TR->isReferenceType()) // C++ [expr] |
| 365 | return LV_Valid; |
| 366 | |
| 367 | // the type looks fine, now check the expression |
| 368 | switch (getStmtClass()) { |
| 369 | case StringLiteralClass: // C99 6.5.1p4 |
Anders Carlsson | 9e933a2 | 2007-11-30 22:47:59 +0000 | [diff] [blame] | 370 | return LV_Valid; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 371 | case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) |
| 372 | // For vectors, make sure base is an lvalue (i.e. not a function call). |
| 373 | if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) |
| 374 | return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(); |
| 375 | return LV_Valid; |
| 376 | case DeclRefExprClass: // C99 6.5.1p2 |
| 377 | if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) |
| 378 | return LV_Valid; |
| 379 | break; |
| 380 | case MemberExprClass: { // C99 6.5.2.3p4 |
| 381 | const MemberExpr *m = cast<MemberExpr>(this); |
| 382 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(); |
| 383 | } |
Chris Lattner | 5bf7202 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 384 | case UnaryOperatorClass: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 385 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) |
Chris Lattner | 5bf7202 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 386 | return LV_Valid; // C99 6.5.3p4 |
| 387 | |
| 388 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real || |
| 389 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag) |
| 390 | return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 391 | break; |
| 392 | case ParenExprClass: // C99 6.5.1p5 |
| 393 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(); |
Steve Naroff | c7c6653 | 2007-12-05 04:00:10 +0000 | [diff] [blame] | 394 | case CompoundLiteralExprClass: // C99 6.5.2.5p5 |
| 395 | return LV_Valid; |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 396 | case ExtVectorElementExprClass: |
| 397 | if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements()) |
Steve Naroff | ba67f69 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 398 | return LV_DuplicateVectorComponents; |
| 399 | return LV_Valid; |
Steve Naroff | 46f18f2 | 2007-11-12 14:34:27 +0000 | [diff] [blame] | 400 | case ObjCIvarRefExprClass: // ObjC instance variables are lvalues. |
| 401 | return LV_Valid; |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 402 | case PreDefinedExprClass: |
| 403 | return LV_Valid; |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 404 | case CXXDefaultArgExprClass: |
| 405 | return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 406 | default: |
| 407 | break; |
| 408 | } |
| 409 | return LV_InvalidExpression; |
| 410 | } |
| 411 | |
| 412 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 413 | /// does not have an incomplete type, does not have a const-qualified type, and |
| 414 | /// if it is a structure or union, does not have any member (including, |
| 415 | /// recursively, any member or element of all contained aggregates or unions) |
| 416 | /// with a const-qualified type. |
| 417 | Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const { |
| 418 | isLvalueResult lvalResult = isLvalue(); |
| 419 | |
| 420 | switch (lvalResult) { |
| 421 | case LV_Valid: break; |
| 422 | case LV_NotObjectType: return MLV_NotObjectType; |
| 423 | case LV_IncompleteVoidType: return MLV_IncompleteVoidType; |
Steve Naroff | ba67f69 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 424 | case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 425 | case LV_InvalidExpression: return MLV_InvalidExpression; |
| 426 | } |
| 427 | if (TR.isConstQualified()) |
| 428 | return MLV_ConstQualified; |
| 429 | if (TR->isArrayType()) |
| 430 | return MLV_ArrayType; |
| 431 | if (TR->isIncompleteType()) |
| 432 | return MLV_IncompleteType; |
| 433 | |
| 434 | if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) { |
| 435 | if (r->hasConstFields()) |
| 436 | return MLV_ConstQualified; |
| 437 | } |
| 438 | return MLV_Valid; |
| 439 | } |
| 440 | |
Ted Kremenek | 5778d62 | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 441 | /// hasGlobalStorage - Return true if this expression has static storage |
Chris Lattner | 743ec37 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 442 | /// duration. This means that the address of this expression is a link-time |
| 443 | /// constant. |
Ted Kremenek | 5778d62 | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 444 | bool Expr::hasGlobalStorage() const { |
Chris Lattner | ba3ddb2 | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 445 | switch (getStmtClass()) { |
| 446 | default: |
| 447 | return false; |
Chris Lattner | 743ec37 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 448 | case ParenExprClass: |
Ted Kremenek | 5778d62 | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 449 | return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage(); |
Chris Lattner | 743ec37 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 450 | case ImplicitCastExprClass: |
Ted Kremenek | 5778d62 | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 451 | return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage(); |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 452 | case CompoundLiteralExprClass: |
| 453 | return cast<CompoundLiteralExpr>(this)->isFileScope(); |
Chris Lattner | ba3ddb2 | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 454 | case DeclRefExprClass: { |
| 455 | const Decl *D = cast<DeclRefExpr>(this)->getDecl(); |
| 456 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
Ted Kremenek | 5778d62 | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 457 | return VD->hasGlobalStorage(); |
Seo Sanghyeon | e330ae7 | 2008-04-04 09:45:30 +0000 | [diff] [blame] | 458 | if (isa<FunctionDecl>(D)) |
| 459 | return true; |
Chris Lattner | ba3ddb2 | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 460 | return false; |
| 461 | } |
Chris Lattner | db586bf | 2007-11-28 04:30:09 +0000 | [diff] [blame] | 462 | case MemberExprClass: { |
Chris Lattner | ba3ddb2 | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 463 | const MemberExpr *M = cast<MemberExpr>(this); |
Ted Kremenek | 5778d62 | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 464 | return !M->isArrow() && M->getBase()->hasGlobalStorage(); |
Chris Lattner | db586bf | 2007-11-28 04:30:09 +0000 | [diff] [blame] | 465 | } |
Chris Lattner | 743ec37 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 466 | case ArraySubscriptExprClass: |
Ted Kremenek | 5778d62 | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 467 | return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage(); |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 468 | case PreDefinedExprClass: |
| 469 | return true; |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 470 | case CXXDefaultArgExprClass: |
| 471 | return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage(); |
Chris Lattner | ba3ddb2 | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Ted Kremenek | 87e30c5 | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 475 | Expr* Expr::IgnoreParens() { |
| 476 | Expr* E = this; |
| 477 | while (ParenExpr* P = dyn_cast<ParenExpr>(E)) |
| 478 | E = P->getSubExpr(); |
| 479 | |
| 480 | return E; |
| 481 | } |
| 482 | |
Chris Lattner | 7a48d9c | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 483 | /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr |
| 484 | /// or CastExprs or ImplicitCastExprs, returning their operand. |
| 485 | Expr *Expr::IgnoreParenCasts() { |
| 486 | Expr *E = this; |
| 487 | while (true) { |
| 488 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) |
| 489 | E = P->getSubExpr(); |
| 490 | else if (CastExpr *P = dyn_cast<CastExpr>(E)) |
| 491 | E = P->getSubExpr(); |
| 492 | else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) |
| 493 | E = P->getSubExpr(); |
| 494 | else |
| 495 | return E; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 500 | bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 501 | switch (getStmtClass()) { |
| 502 | default: |
| 503 | if (Loc) *Loc = getLocStart(); |
| 504 | return false; |
| 505 | case ParenExprClass: |
| 506 | return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc); |
| 507 | case StringLiteralClass: |
Steve Naroff | 4fdea13 | 2007-11-09 15:00:03 +0000 | [diff] [blame] | 508 | case ObjCStringLiteralClass: |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 509 | case FloatingLiteralClass: |
| 510 | case IntegerLiteralClass: |
| 511 | case CharacterLiteralClass: |
| 512 | case ImaginaryLiteralClass: |
Anders Carlsson | 855d78d | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 513 | case TypesCompatibleExprClass: |
| 514 | case CXXBoolLiteralExprClass: |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 515 | return true; |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 516 | case CallExprClass: { |
| 517 | const CallExpr *CE = cast<CallExpr>(this); |
| 518 | llvm::APSInt Result(32); |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 519 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 520 | if (CE->isBuiltinClassifyType(Result)) |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 521 | return true; |
Steve Naroff | 44aec4c | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 522 | if (CE->isBuiltinConstantExpr()) |
| 523 | return true; |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 524 | if (Loc) *Loc = getLocStart(); |
| 525 | return false; |
| 526 | } |
Chris Lattner | 42e86b6 | 2007-11-01 02:45:17 +0000 | [diff] [blame] | 527 | case DeclRefExprClass: { |
| 528 | const Decl *D = cast<DeclRefExpr>(this)->getDecl(); |
| 529 | // Accept address of function. |
| 530 | if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D)) |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 531 | return true; |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 532 | if (Loc) *Loc = getLocStart(); |
Chris Lattner | ba3ddb2 | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 533 | if (isa<VarDecl>(D)) |
| 534 | return TR->isArrayType(); |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 535 | return false; |
Chris Lattner | 42e86b6 | 2007-11-01 02:45:17 +0000 | [diff] [blame] | 536 | } |
Steve Naroff | f91f972 | 2008-01-09 00:05:37 +0000 | [diff] [blame] | 537 | case CompoundLiteralExprClass: |
| 538 | if (Loc) *Loc = getLocStart(); |
| 539 | // Allow "(int []){2,4}", since the array will be converted to a pointer. |
Nate Begeman | c4e28e4 | 2008-01-25 05:34:48 +0000 | [diff] [blame] | 540 | // Allow "(vector type){2,4}" since the elements are all constant. |
| 541 | return TR->isArrayType() || TR->isVectorType(); |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 542 | case UnaryOperatorClass: { |
| 543 | const UnaryOperator *Exp = cast<UnaryOperator>(this); |
| 544 | |
Chris Lattner | ba3ddb2 | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 545 | // C99 6.6p9 |
Chris Lattner | 35b662f | 2007-12-11 23:11:17 +0000 | [diff] [blame] | 546 | if (Exp->getOpcode() == UnaryOperator::AddrOf) { |
Ted Kremenek | 5778d62 | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 547 | if (!Exp->getSubExpr()->hasGlobalStorage()) { |
Chris Lattner | 35b662f | 2007-12-11 23:11:17 +0000 | [diff] [blame] | 548 | if (Loc) *Loc = getLocStart(); |
| 549 | return false; |
| 550 | } |
| 551 | return true; |
| 552 | } |
Chris Lattner | ba3ddb2 | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 553 | |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 554 | // Get the operand value. If this is sizeof/alignof, do not evalute the |
| 555 | // operand. This affects C99 6.6p3. |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 556 | if (!Exp->isSizeOfAlignOfOp() && |
| 557 | Exp->getOpcode() != UnaryOperator::OffsetOf && |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 558 | !Exp->getSubExpr()->isConstantExpr(Ctx, Loc)) |
| 559 | return false; |
| 560 | |
| 561 | switch (Exp->getOpcode()) { |
| 562 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 563 | // See C99 6.6p3. |
| 564 | default: |
| 565 | if (Loc) *Loc = Exp->getOperatorLoc(); |
| 566 | return false; |
| 567 | case UnaryOperator::Extension: |
| 568 | return true; // FIXME: this is wrong. |
| 569 | case UnaryOperator::SizeOf: |
| 570 | case UnaryOperator::AlignOf: |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 571 | case UnaryOperator::OffsetOf: |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 572 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Eli Friedman | 62f67fd | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 573 | if (!Exp->getSubExpr()->getType()->isConstantSizeType()) { |
Chris Lattner | b3ff082 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 574 | if (Loc) *Loc = Exp->getOperatorLoc(); |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 575 | return false; |
Chris Lattner | b3ff082 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 576 | } |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 577 | return true; |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 578 | case UnaryOperator::LNot: |
| 579 | case UnaryOperator::Plus: |
| 580 | case UnaryOperator::Minus: |
| 581 | case UnaryOperator::Not: |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 582 | return true; |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 583 | } |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 584 | } |
| 585 | case SizeOfAlignOfTypeExprClass: { |
| 586 | const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this); |
| 587 | // alignof always evaluates to a constant. |
Chris Lattner | 2051546 | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 588 | if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() && |
| 589 | !Exp->getArgumentType()->isConstantSizeType()) { |
Chris Lattner | b3ff082 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 590 | if (Loc) *Loc = Exp->getOperatorLoc(); |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 591 | return false; |
Chris Lattner | b3ff082 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 592 | } |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 593 | return true; |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 594 | } |
| 595 | case BinaryOperatorClass: { |
| 596 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
| 597 | |
| 598 | // The LHS of a constant expr is always evaluated and needed. |
| 599 | if (!Exp->getLHS()->isConstantExpr(Ctx, Loc)) |
| 600 | return false; |
| 601 | |
| 602 | if (!Exp->getRHS()->isConstantExpr(Ctx, Loc)) |
| 603 | return false; |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 604 | return true; |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 605 | } |
| 606 | case ImplicitCastExprClass: |
| 607 | case CastExprClass: { |
| 608 | const Expr *SubExpr; |
| 609 | SourceLocation CastLoc; |
| 610 | if (const CastExpr *C = dyn_cast<CastExpr>(this)) { |
| 611 | SubExpr = C->getSubExpr(); |
| 612 | CastLoc = C->getLParenLoc(); |
| 613 | } else { |
| 614 | SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr(); |
| 615 | CastLoc = getLocStart(); |
| 616 | } |
| 617 | if (!SubExpr->isConstantExpr(Ctx, Loc)) { |
| 618 | if (Loc) *Loc = SubExpr->getLocStart(); |
| 619 | return false; |
| 620 | } |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 621 | return true; |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 622 | } |
| 623 | case ConditionalOperatorClass: { |
| 624 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 625 | if (!Exp->getCond()->isConstantExpr(Ctx, Loc) || |
Anders Carlsson | 37365fc | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 626 | // Handle the GNU extension for missing LHS. |
| 627 | !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) || |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 628 | !Exp->getRHS()->isConstantExpr(Ctx, Loc)) |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 629 | return false; |
Chris Lattner | 06db613 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 630 | return true; |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 631 | } |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 632 | case InitListExprClass: { |
| 633 | const InitListExpr *Exp = cast<InitListExpr>(this); |
| 634 | unsigned numInits = Exp->getNumInits(); |
| 635 | for (unsigned i = 0; i < numInits; i++) { |
| 636 | if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) { |
| 637 | if (Loc) *Loc = Exp->getInit(i)->getLocStart(); |
| 638 | return false; |
| 639 | } |
| 640 | } |
| 641 | return true; |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 642 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 643 | case CXXDefaultArgExprClass: |
| 644 | return cast<CXXDefaultArgExpr>(this)->getExpr()->isConstantExpr(Ctx, Loc); |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 645 | } |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 648 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 649 | /// an integer constant expression. Note: With the introduction of VLA's in |
| 650 | /// C99 the result of the sizeof operator is no longer always a constant |
| 651 | /// expression. The generalization of the wording to include any subexpression |
| 652 | /// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions |
| 653 | /// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, |
| 654 | /// "0 || f()" can be treated as a constant expression. In C90 this expression, |
| 655 | /// occurring in a context requiring a constant, would have been a constraint |
| 656 | /// violation. FIXME: This routine currently implements C90 semantics. |
| 657 | /// To properly implement C99 semantics this routine will need to evaluate |
| 658 | /// expressions involving operators previously mentioned. |
| 659 | |
| 660 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 661 | /// comma, etc |
| 662 | /// |
| 663 | /// FIXME: This should ext-warn on overflow during evaluation! ISO C does not |
Chris Lattner | 9d020b3 | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 664 | /// permit this. This includes things like (int)1e1000 |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 665 | /// |
| 666 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 667 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 668 | /// cast+dereference. |
| 669 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 670 | SourceLocation *Loc, bool isEvaluated) const { |
| 671 | switch (getStmtClass()) { |
| 672 | default: |
| 673 | if (Loc) *Loc = getLocStart(); |
| 674 | return false; |
| 675 | case ParenExprClass: |
| 676 | return cast<ParenExpr>(this)->getSubExpr()-> |
| 677 | isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
| 678 | case IntegerLiteralClass: |
| 679 | Result = cast<IntegerLiteral>(this)->getValue(); |
| 680 | break; |
| 681 | case CharacterLiteralClass: { |
| 682 | const CharacterLiteral *CL = cast<CharacterLiteral>(this); |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 683 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 684 | Result = CL->getValue(); |
| 685 | Result.setIsUnsigned(!getType()->isSignedIntegerType()); |
| 686 | break; |
| 687 | } |
Steve Naroff | c6f0fd3 | 2007-08-02 04:09:23 +0000 | [diff] [blame] | 688 | case TypesCompatibleExprClass: { |
| 689 | const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this); |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 690 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 691 | Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2()); |
Steve Naroff | 1200b5a | 2007-08-02 00:13:27 +0000 | [diff] [blame] | 692 | break; |
Steve Naroff | c6f0fd3 | 2007-08-02 04:09:23 +0000 | [diff] [blame] | 693 | } |
Steve Naroff | 8d3b170 | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 694 | case CallExprClass: { |
| 695 | const CallExpr *CE = cast<CallExpr>(this); |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 696 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Steve Naroff | 8d3b170 | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 697 | if (CE->isBuiltinClassifyType(Result)) |
| 698 | break; |
| 699 | if (Loc) *Loc = getLocStart(); |
| 700 | return false; |
| 701 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 702 | case DeclRefExprClass: |
| 703 | if (const EnumConstantDecl *D = |
| 704 | dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { |
| 705 | Result = D->getInitVal(); |
| 706 | break; |
| 707 | } |
| 708 | if (Loc) *Loc = getLocStart(); |
| 709 | return false; |
| 710 | case UnaryOperatorClass: { |
| 711 | const UnaryOperator *Exp = cast<UnaryOperator>(this); |
| 712 | |
| 713 | // Get the operand value. If this is sizeof/alignof, do not evalute the |
| 714 | // operand. This affects C99 6.6p3. |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 715 | if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() && |
Chris Lattner | 5a9b624 | 2007-08-23 21:42:50 +0000 | [diff] [blame] | 716 | !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 717 | return false; |
| 718 | |
| 719 | switch (Exp->getOpcode()) { |
| 720 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 721 | // See C99 6.6p3. |
| 722 | default: |
| 723 | if (Loc) *Loc = Exp->getOperatorLoc(); |
| 724 | return false; |
| 725 | case UnaryOperator::Extension: |
| 726 | return true; // FIXME: this is wrong. |
| 727 | case UnaryOperator::SizeOf: |
| 728 | case UnaryOperator::AlignOf: |
Chris Lattner | 2051546 | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 729 | // Return the result in the right width. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 730 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Chris Lattner | 2051546 | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 731 | |
| 732 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
| 733 | if (Exp->getSubExpr()->getType()->isVoidType()) { |
| 734 | Result = 1; |
| 735 | break; |
| 736 | } |
| 737 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 738 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Eli Friedman | 62f67fd | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 739 | if (!Exp->getSubExpr()->getType()->isConstantSizeType()) { |
Chris Lattner | b3ff082 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 740 | if (Loc) *Loc = Exp->getOperatorLoc(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 741 | return false; |
Chris Lattner | b3ff082 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 742 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 743 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 744 | // Get information about the size or align. |
Chris Lattner | d4dc267 | 2008-01-02 21:54:09 +0000 | [diff] [blame] | 745 | if (Exp->getSubExpr()->getType()->isFunctionType()) { |
| 746 | // GCC extension: sizeof(function) = 1. |
| 747 | Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1; |
Chris Lattner | d9ffbc9 | 2007-11-27 18:22:04 +0000 | [diff] [blame] | 748 | } else { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 749 | unsigned CharSize = Ctx.Target.getCharWidth(); |
Anders Carlsson | 1f86b03 | 2008-02-18 07:10:45 +0000 | [diff] [blame] | 750 | if (Exp->getOpcode() == UnaryOperator::AlignOf) |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 751 | Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType()) / CharSize; |
Anders Carlsson | 1f86b03 | 2008-02-18 07:10:45 +0000 | [diff] [blame] | 752 | else |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 753 | Result = Ctx.getTypeSize(Exp->getSubExpr()->getType()) / CharSize; |
Chris Lattner | d9ffbc9 | 2007-11-27 18:22:04 +0000 | [diff] [blame] | 754 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 755 | break; |
| 756 | case UnaryOperator::LNot: { |
Chris Lattner | f00fdc0 | 2008-01-25 19:16:19 +0000 | [diff] [blame] | 757 | bool Val = Result == 0; |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 758 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 759 | Result = Val; |
| 760 | break; |
| 761 | } |
| 762 | case UnaryOperator::Plus: |
| 763 | break; |
| 764 | case UnaryOperator::Minus: |
| 765 | Result = -Result; |
| 766 | break; |
| 767 | case UnaryOperator::Not: |
| 768 | Result = ~Result; |
| 769 | break; |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 770 | case UnaryOperator::OffsetOf: |
| 771 | Result = Exp->evaluateOffsetOf(Ctx); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 772 | } |
| 773 | break; |
| 774 | } |
| 775 | case SizeOfAlignOfTypeExprClass: { |
| 776 | const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this); |
Chris Lattner | 2051546 | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 777 | |
| 778 | // Return the result in the right width. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 779 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Chris Lattner | 2051546 | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 780 | |
| 781 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
| 782 | if (Exp->getArgumentType()->isVoidType()) { |
| 783 | Result = 1; |
| 784 | break; |
| 785 | } |
| 786 | |
| 787 | // alignof always evaluates to a constant, sizeof does if arg is not VLA. |
Eli Friedman | 62f67fd | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 788 | if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) { |
Chris Lattner | b3ff082 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 789 | if (Loc) *Loc = Exp->getOperatorLoc(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 790 | return false; |
Chris Lattner | b3ff082 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 791 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 792 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 793 | // Get information about the size or align. |
Chris Lattner | d4dc267 | 2008-01-02 21:54:09 +0000 | [diff] [blame] | 794 | if (Exp->getArgumentType()->isFunctionType()) { |
| 795 | // GCC extension: sizeof(function) = 1. |
| 796 | Result = Exp->isSizeOf() ? 1 : 4; |
Anders Carlsson | 8d2b2b7 | 2008-02-16 01:20:23 +0000 | [diff] [blame] | 797 | } else { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 798 | unsigned CharSize = Ctx.Target.getCharWidth(); |
Anders Carlsson | 8d2b2b7 | 2008-02-16 01:20:23 +0000 | [diff] [blame] | 799 | if (Exp->isSizeOf()) |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 800 | Result = Ctx.getTypeSize(Exp->getArgumentType()) / CharSize; |
Anders Carlsson | 8d2b2b7 | 2008-02-16 01:20:23 +0000 | [diff] [blame] | 801 | else |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 802 | Result = Ctx.getTypeAlign(Exp->getArgumentType()) / CharSize; |
Ted Kremenek | a9d0fd9 | 2007-12-17 17:38:43 +0000 | [diff] [blame] | 803 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 804 | break; |
| 805 | } |
| 806 | case BinaryOperatorClass: { |
| 807 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
| 808 | |
| 809 | // The LHS of a constant expr is always evaluated and needed. |
| 810 | if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
| 811 | return false; |
| 812 | |
| 813 | llvm::APSInt RHS(Result); |
| 814 | |
| 815 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 816 | // RHS. Make sure to pass isEvaluated down correctly. |
| 817 | if (Exp->isLogicalOp()) { |
| 818 | bool RHSEval; |
| 819 | if (Exp->getOpcode() == BinaryOperator::LAnd) |
| 820 | RHSEval = Result != 0; |
| 821 | else { |
| 822 | assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); |
| 823 | RHSEval = Result == 0; |
| 824 | } |
| 825 | |
| 826 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, |
| 827 | isEvaluated & RHSEval)) |
| 828 | return false; |
| 829 | } else { |
| 830 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) |
| 831 | return false; |
| 832 | } |
| 833 | |
| 834 | switch (Exp->getOpcode()) { |
| 835 | default: |
| 836 | if (Loc) *Loc = getLocStart(); |
| 837 | return false; |
| 838 | case BinaryOperator::Mul: |
| 839 | Result *= RHS; |
| 840 | break; |
| 841 | case BinaryOperator::Div: |
| 842 | if (RHS == 0) { |
| 843 | if (!isEvaluated) break; |
| 844 | if (Loc) *Loc = getLocStart(); |
| 845 | return false; |
| 846 | } |
| 847 | Result /= RHS; |
| 848 | break; |
| 849 | case BinaryOperator::Rem: |
| 850 | if (RHS == 0) { |
| 851 | if (!isEvaluated) break; |
| 852 | if (Loc) *Loc = getLocStart(); |
| 853 | return false; |
| 854 | } |
| 855 | Result %= RHS; |
| 856 | break; |
| 857 | case BinaryOperator::Add: Result += RHS; break; |
| 858 | case BinaryOperator::Sub: Result -= RHS; break; |
| 859 | case BinaryOperator::Shl: |
Chris Lattner | 3496d52 | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 860 | Result <<= |
| 861 | static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 862 | break; |
| 863 | case BinaryOperator::Shr: |
Chris Lattner | 3496d52 | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 864 | Result >>= |
| 865 | static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 866 | break; |
| 867 | case BinaryOperator::LT: Result = Result < RHS; break; |
| 868 | case BinaryOperator::GT: Result = Result > RHS; break; |
| 869 | case BinaryOperator::LE: Result = Result <= RHS; break; |
| 870 | case BinaryOperator::GE: Result = Result >= RHS; break; |
| 871 | case BinaryOperator::EQ: Result = Result == RHS; break; |
| 872 | case BinaryOperator::NE: Result = Result != RHS; break; |
| 873 | case BinaryOperator::And: Result &= RHS; break; |
| 874 | case BinaryOperator::Xor: Result ^= RHS; break; |
| 875 | case BinaryOperator::Or: Result |= RHS; break; |
| 876 | case BinaryOperator::LAnd: |
| 877 | Result = Result != 0 && RHS != 0; |
| 878 | break; |
| 879 | case BinaryOperator::LOr: |
| 880 | Result = Result != 0 || RHS != 0; |
| 881 | break; |
| 882 | |
| 883 | case BinaryOperator::Comma: |
| 884 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 885 | // *except* when they are contained within a subexpression that is not |
| 886 | // evaluated". Note that Assignment can never happen due to constraints |
| 887 | // on the LHS subexpr, so we don't need to check it here. |
| 888 | if (isEvaluated) { |
| 889 | if (Loc) *Loc = getLocStart(); |
| 890 | return false; |
| 891 | } |
| 892 | |
| 893 | // The result of the constant expr is the RHS. |
| 894 | Result = RHS; |
| 895 | return true; |
| 896 | } |
| 897 | |
| 898 | assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); |
| 899 | break; |
| 900 | } |
| 901 | case ImplicitCastExprClass: |
| 902 | case CastExprClass: { |
| 903 | const Expr *SubExpr; |
| 904 | SourceLocation CastLoc; |
| 905 | if (const CastExpr *C = dyn_cast<CastExpr>(this)) { |
| 906 | SubExpr = C->getSubExpr(); |
| 907 | CastLoc = C->getLParenLoc(); |
| 908 | } else { |
| 909 | SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr(); |
| 910 | CastLoc = getLocStart(); |
| 911 | } |
| 912 | |
| 913 | // C99 6.6p6: shall only convert arithmetic types to integer types. |
| 914 | if (!SubExpr->getType()->isArithmeticType() || |
| 915 | !getType()->isIntegerType()) { |
| 916 | if (Loc) *Loc = SubExpr->getLocStart(); |
| 917 | return false; |
| 918 | } |
Chris Lattner | 76a0c1b | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 919 | |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 920 | uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType())); |
Chris Lattner | 76a0c1b | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 921 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 922 | // Handle simple integer->integer casts. |
| 923 | if (SubExpr->getType()->isIntegerType()) { |
| 924 | if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
| 925 | return false; |
| 926 | |
| 927 | // Figure out if this is a truncate, extend or noop cast. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 928 | // If the input is signed, do a sign extend, noop, or truncate. |
Chris Lattner | 000c410 | 2008-01-09 18:59:34 +0000 | [diff] [blame] | 929 | if (getType()->isBooleanType()) { |
| 930 | // Conversion to bool compares against zero. |
| 931 | Result = Result != 0; |
| 932 | Result.zextOrTrunc(DestWidth); |
| 933 | } else if (SubExpr->getType()->isSignedIntegerType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 934 | Result.sextOrTrunc(DestWidth); |
| 935 | else // If the input is unsigned, do a zero extend, noop, or truncate. |
| 936 | Result.zextOrTrunc(DestWidth); |
| 937 | break; |
| 938 | } |
| 939 | |
| 940 | // Allow floating constants that are the immediate operands of casts or that |
| 941 | // are parenthesized. |
| 942 | const Expr *Operand = SubExpr; |
| 943 | while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) |
| 944 | Operand = PE->getSubExpr(); |
Chris Lattner | 76a0c1b | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 945 | |
| 946 | // If this isn't a floating literal, we can't handle it. |
| 947 | const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand); |
| 948 | if (!FL) { |
| 949 | if (Loc) *Loc = Operand->getLocStart(); |
| 950 | return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 951 | } |
Chris Lattner | 000c410 | 2008-01-09 18:59:34 +0000 | [diff] [blame] | 952 | |
| 953 | // If the destination is boolean, compare against zero. |
| 954 | if (getType()->isBooleanType()) { |
| 955 | Result = !FL->getValue().isZero(); |
| 956 | Result.zextOrTrunc(DestWidth); |
| 957 | break; |
| 958 | } |
Chris Lattner | 76a0c1b | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 959 | |
| 960 | // Determine whether we are converting to unsigned or signed. |
| 961 | bool DestSigned = getType()->isSignedIntegerType(); |
Chris Lattner | 9d020b3 | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 962 | |
| 963 | // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can |
| 964 | // be called multiple times per AST. |
Chris Lattner | 76a0c1b | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 965 | uint64_t Space[4]; |
Chris Lattner | 9d020b3 | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 966 | (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned, |
| 967 | llvm::APFloat::rmTowardZero); |
Chris Lattner | 76a0c1b | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 968 | Result = llvm::APInt(DestWidth, 4, Space); |
| 969 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 970 | } |
| 971 | case ConditionalOperatorClass: { |
| 972 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
| 973 | |
| 974 | if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
| 975 | return false; |
| 976 | |
| 977 | const Expr *TrueExp = Exp->getLHS(); |
| 978 | const Expr *FalseExp = Exp->getRHS(); |
| 979 | if (Result == 0) std::swap(TrueExp, FalseExp); |
| 980 | |
| 981 | // Evaluate the false one first, discard the result. |
Anders Carlsson | 37365fc | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 982 | if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 983 | return false; |
| 984 | // Evalute the true one, capture the result. |
Anders Carlsson | 37365fc | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 985 | if (TrueExp && |
| 986 | !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 987 | return false; |
| 988 | break; |
| 989 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 990 | case CXXDefaultArgExprClass: |
| 991 | return cast<CXXDefaultArgExpr>(this) |
| 992 | ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | // Cases that are valid constant exprs fall through to here. |
| 996 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
| 997 | return true; |
| 998 | } |
| 999 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1000 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 1001 | /// integer constant expression with the value zero, or if this is one that is |
| 1002 | /// cast to void*. |
| 1003 | bool Expr::isNullPointerConstant(ASTContext &Ctx) const { |
Steve Naroff | a2e5322 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1004 | // Strip off a cast to void*, if it exists. |
| 1005 | if (const CastExpr *CE = dyn_cast<CastExpr>(this)) { |
| 1006 | // Check that it is a cast to void*. |
Eli Friedman | d899dbe | 2008-02-13 17:29:58 +0000 | [diff] [blame] | 1007 | if (const PointerType *PT = CE->getType()->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1008 | QualType Pointee = PT->getPointeeType(); |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1009 | if (Pointee.getCVRQualifiers() == 0 && |
| 1010 | Pointee->isVoidType() && // to void* |
Steve Naroff | a2e5322 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1011 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
| 1012 | return CE->getSubExpr()->isNullPointerConstant(Ctx); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1013 | } |
Steve Naroff | a2e5322 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1014 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
| 1015 | // Ignore the ImplicitCastExpr type entirely. |
| 1016 | return ICE->getSubExpr()->isNullPointerConstant(Ctx); |
| 1017 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 1018 | // Accept ((void*)0) as a null pointer constant, as many other |
| 1019 | // implementations do. |
| 1020 | return PE->getSubExpr()->isNullPointerConstant(Ctx); |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1021 | } else if (const CXXDefaultArgExpr *DefaultArg |
| 1022 | = dyn_cast<CXXDefaultArgExpr>(this)) { |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1023 | // See through default argument expressions |
| 1024 | return DefaultArg->getExpr()->isNullPointerConstant(Ctx); |
Steve Naroff | f33a985 | 2008-01-14 02:53:34 +0000 | [diff] [blame] | 1025 | } |
Steve Naroff | a2e5322 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1026 | |
| 1027 | // This expression must be an integer type. |
| 1028 | if (!getType()->isIntegerType()) |
| 1029 | return false; |
| 1030 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1031 | // If we have an integer constant expression, we need to *evaluate* it and |
| 1032 | // test for the value 0. |
| 1033 | llvm::APSInt Val(32); |
Steve Naroff | a2e5322 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1034 | return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1035 | } |
Steve Naroff | c11705f | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1036 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1037 | unsigned ExtVectorElementExpr::getNumElements() const { |
Chris Lattner | 5054785 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 1038 | return strlen(Accessor.getName()); |
| 1039 | } |
| 1040 | |
| 1041 | |
Chris Lattner | f4bf551 | 2007-08-02 21:47:28 +0000 | [diff] [blame] | 1042 | /// getComponentType - Determine whether the components of this access are |
| 1043 | /// "point" "color" or "texture" elements. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1044 | ExtVectorElementExpr::ElementType |
| 1045 | ExtVectorElementExpr::getElementType() const { |
Steve Naroff | c11705f | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1046 | // derive the component type, no need to waste space. |
| 1047 | const char *compStr = Accessor.getName(); |
Chris Lattner | abf25b3 | 2007-08-02 22:20:00 +0000 | [diff] [blame] | 1048 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1049 | if (ExtVectorType::getPointAccessorIdx(*compStr) != -1) return Point; |
| 1050 | if (ExtVectorType::getColorAccessorIdx(*compStr) != -1) return Color; |
Chris Lattner | abf25b3 | 2007-08-02 22:20:00 +0000 | [diff] [blame] | 1051 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1052 | assert(ExtVectorType::getTextureAccessorIdx(*compStr) != -1 && |
Chris Lattner | abf25b3 | 2007-08-02 22:20:00 +0000 | [diff] [blame] | 1053 | "getComponentType(): Illegal accessor"); |
| 1054 | return Texture; |
Steve Naroff | c11705f | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1055 | } |
Steve Naroff | ba67f69 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1056 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 1057 | /// containsDuplicateElements - Return true if any element access is |
Chris Lattner | f4bf551 | 2007-08-02 21:47:28 +0000 | [diff] [blame] | 1058 | /// repeated. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1059 | bool ExtVectorElementExpr::containsDuplicateElements() const { |
Steve Naroff | ba67f69 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1060 | const char *compStr = Accessor.getName(); |
| 1061 | unsigned length = strlen(compStr); |
| 1062 | |
| 1063 | for (unsigned i = 0; i < length-1; i++) { |
| 1064 | const char *s = compStr+i; |
| 1065 | for (const char c = *s++; *s; s++) |
| 1066 | if (c == *s) |
| 1067 | return true; |
| 1068 | } |
| 1069 | return false; |
| 1070 | } |
Chris Lattner | 42158e7 | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1071 | |
| 1072 | /// getEncodedElementAccess - We encode fields with two bits per component. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1073 | unsigned ExtVectorElementExpr::getEncodedElementAccess() const { |
Chris Lattner | 42158e7 | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1074 | const char *compStr = Accessor.getName(); |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 1075 | unsigned length = getNumElements(); |
Chris Lattner | 42158e7 | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1076 | |
| 1077 | unsigned Result = 0; |
| 1078 | |
| 1079 | while (length--) { |
| 1080 | Result <<= 2; |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1081 | int Idx = ExtVectorType::getAccessorIdx(compStr[length]); |
Chris Lattner | 42158e7 | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1082 | assert(Idx != -1 && "Invalid accessor letter"); |
| 1083 | Result |= Idx; |
| 1084 | } |
| 1085 | return Result; |
| 1086 | } |
| 1087 | |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1088 | // constructor for instance messages. |
Steve Naroff | 6cb1d36 | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1089 | ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1090 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | 1e1c391 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1091 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 9f176d1 | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1092 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | 1e1c391 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1093 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | d029a6f | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1094 | MethodProto(mproto) { |
Steve Naroff | 9f176d1 | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1095 | NumArgs = nargs; |
| 1096 | SubExprs = new Expr*[NumArgs+1]; |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1097 | SubExprs[RECEIVER] = receiver; |
Steve Naroff | 9f176d1 | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1098 | if (NumArgs) { |
| 1099 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1100 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1101 | } |
Steve Naroff | c39ca26 | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1102 | LBracloc = LBrac; |
| 1103 | RBracloc = RBrac; |
| 1104 | } |
| 1105 | |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1106 | // constructor for class messages. |
| 1107 | // FIXME: clsName should be typed to ObjCInterfaceType |
Steve Naroff | 6cb1d36 | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1108 | ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1109 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | 1e1c391 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1110 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 9f176d1 | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1111 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | 1e1c391 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1112 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | d029a6f | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1113 | MethodProto(mproto) { |
Steve Naroff | 9f176d1 | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1114 | NumArgs = nargs; |
| 1115 | SubExprs = new Expr*[NumArgs+1]; |
Ted Kremenek | d029a6f | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1116 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | 0x1); |
Steve Naroff | 9f176d1 | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1117 | if (NumArgs) { |
| 1118 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1119 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1120 | } |
Steve Naroff | c39ca26 | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1121 | LBracloc = LBrac; |
| 1122 | RBracloc = RBrac; |
| 1123 | } |
| 1124 | |
Chris Lattner | f624cd2 | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1125 | bool ChooseExpr::isConditionTrue(ASTContext &C) const { |
| 1126 | llvm::APSInt CondVal(32); |
| 1127 | bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C); |
| 1128 | assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst; |
| 1129 | return CondVal != 0; |
| 1130 | } |
| 1131 | |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1132 | static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) |
| 1133 | { |
| 1134 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1135 | QualType Ty = ME->getBase()->getType(); |
| 1136 | |
| 1137 | RecordDecl *RD = Ty->getAsRecordType()->getDecl(); |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1138 | const ASTRecordLayout &RL = C.getASTRecordLayout(RD); |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1139 | FieldDecl *FD = ME->getMemberDecl(); |
| 1140 | |
| 1141 | // FIXME: This is linear time. |
| 1142 | unsigned i = 0, e = 0; |
| 1143 | for (i = 0, e = RD->getNumMembers(); i != e; i++) { |
| 1144 | if (RD->getMember(i) == FD) |
| 1145 | break; |
| 1146 | } |
| 1147 | |
| 1148 | return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase()); |
| 1149 | } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 1150 | const Expr *Base = ASE->getBase(); |
| 1151 | llvm::APSInt Idx(32); |
| 1152 | bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C); |
| 1153 | assert(ICE && "Array index is not a constant integer!"); |
| 1154 | |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1155 | int64_t size = C.getTypeSize(ASE->getType()); |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1156 | size *= Idx.getSExtValue(); |
| 1157 | |
| 1158 | return size + evaluateOffsetOf(C, Base); |
| 1159 | } else if (isa<CompoundLiteralExpr>(E)) |
| 1160 | return 0; |
| 1161 | |
| 1162 | assert(0 && "Unknown offsetof subexpression!"); |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const |
| 1167 | { |
| 1168 | assert(Opc == OffsetOf && "Unary operator not offsetof!"); |
| 1169 | |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1170 | unsigned CharSize = C.Target.getCharWidth(); |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1171 | return ::evaluateOffsetOf(C, Val) / CharSize; |
| 1172 | } |
| 1173 | |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1174 | //===----------------------------------------------------------------------===// |
| 1175 | // Child Iterators for iterating over subexpressions/substatements |
| 1176 | //===----------------------------------------------------------------------===// |
| 1177 | |
| 1178 | // DeclRefExpr |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1179 | Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } |
| 1180 | Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1181 | |
Steve Naroff | 5eb2a4a | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 1182 | // ObjCIvarRefExpr |
Ted Kremenek | 07af3dd | 2008-05-02 18:40:22 +0000 | [diff] [blame] | 1183 | Stmt::child_iterator ObjCIvarRefExpr::child_begin() { |
| 1184 | return reinterpret_cast<Stmt**>(&Base); |
| 1185 | } |
| 1186 | |
| 1187 | Stmt::child_iterator ObjCIvarRefExpr::child_end() { |
| 1188 | return reinterpret_cast<Stmt**>(&Base)+1; |
| 1189 | } |
Steve Naroff | 5eb2a4a | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 1190 | |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1191 | // PreDefinedExpr |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1192 | Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); } |
| 1193 | Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); } |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1194 | |
| 1195 | // IntegerLiteral |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1196 | Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } |
| 1197 | Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1198 | |
| 1199 | // CharacterLiteral |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1200 | Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); } |
| 1201 | Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1202 | |
| 1203 | // FloatingLiteral |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1204 | Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } |
| 1205 | Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1206 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1207 | // ImaginaryLiteral |
| 1208 | Stmt::child_iterator ImaginaryLiteral::child_begin() { |
| 1209 | return reinterpret_cast<Stmt**>(&Val); |
| 1210 | } |
| 1211 | Stmt::child_iterator ImaginaryLiteral::child_end() { |
| 1212 | return reinterpret_cast<Stmt**>(&Val)+1; |
| 1213 | } |
| 1214 | |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1215 | // StringLiteral |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1216 | Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } |
| 1217 | Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1218 | |
| 1219 | // ParenExpr |
| 1220 | Stmt::child_iterator ParenExpr::child_begin() { |
| 1221 | return reinterpret_cast<Stmt**>(&Val); |
| 1222 | } |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1223 | Stmt::child_iterator ParenExpr::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1224 | return reinterpret_cast<Stmt**>(&Val)+1; |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | // UnaryOperator |
| 1228 | Stmt::child_iterator UnaryOperator::child_begin() { |
Ted Kremenek | 6d7ea52 | 2007-12-15 00:39:18 +0000 | [diff] [blame] | 1229 | return reinterpret_cast<Stmt**>(&Val); |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1230 | } |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1231 | Stmt::child_iterator UnaryOperator::child_end() { |
Ted Kremenek | 6d7ea52 | 2007-12-15 00:39:18 +0000 | [diff] [blame] | 1232 | return reinterpret_cast<Stmt**>(&Val+1); |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | // SizeOfAlignOfTypeExpr |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1236 | Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() { |
Ted Kremenek | b7cf095 | 2007-12-14 22:52:23 +0000 | [diff] [blame] | 1237 | // If the type is a VLA type (and not a typedef), the size expression of the |
| 1238 | // VLA needs to be treated as an executable expression. |
| 1239 | if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr())) |
| 1240 | return child_iterator(T); |
| 1241 | else |
| 1242 | return child_iterator(); |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1243 | } |
| 1244 | Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() { |
Ted Kremenek | b7cf095 | 2007-12-14 22:52:23 +0000 | [diff] [blame] | 1245 | return child_iterator(); |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1246 | } |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1247 | |
| 1248 | // ArraySubscriptExpr |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1249 | Stmt::child_iterator ArraySubscriptExpr::child_begin() { |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1250 | return reinterpret_cast<Stmt**>(&SubExprs); |
| 1251 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1252 | Stmt::child_iterator ArraySubscriptExpr::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1253 | return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR; |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | // CallExpr |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1257 | Stmt::child_iterator CallExpr::child_begin() { |
Ted Kremenek | 15ede50 | 2007-08-27 21:11:44 +0000 | [diff] [blame] | 1258 | return reinterpret_cast<Stmt**>(&SubExprs[0]); |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1259 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1260 | Stmt::child_iterator CallExpr::child_end() { |
Ted Kremenek | 15ede50 | 2007-08-27 21:11:44 +0000 | [diff] [blame] | 1261 | return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]); |
Ted Kremenek | e4acb9c | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1262 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1263 | |
| 1264 | // MemberExpr |
| 1265 | Stmt::child_iterator MemberExpr::child_begin() { |
| 1266 | return reinterpret_cast<Stmt**>(&Base); |
| 1267 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1268 | Stmt::child_iterator MemberExpr::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1269 | return reinterpret_cast<Stmt**>(&Base)+1; |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1272 | // ExtVectorElementExpr |
| 1273 | Stmt::child_iterator ExtVectorElementExpr::child_begin() { |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1274 | return reinterpret_cast<Stmt**>(&Base); |
| 1275 | } |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1276 | Stmt::child_iterator ExtVectorElementExpr::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1277 | return reinterpret_cast<Stmt**>(&Base)+1; |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | // CompoundLiteralExpr |
| 1281 | Stmt::child_iterator CompoundLiteralExpr::child_begin() { |
| 1282 | return reinterpret_cast<Stmt**>(&Init); |
| 1283 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1284 | Stmt::child_iterator CompoundLiteralExpr::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1285 | return reinterpret_cast<Stmt**>(&Init)+1; |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | // ImplicitCastExpr |
| 1289 | Stmt::child_iterator ImplicitCastExpr::child_begin() { |
| 1290 | return reinterpret_cast<Stmt**>(&Op); |
| 1291 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1292 | Stmt::child_iterator ImplicitCastExpr::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1293 | return reinterpret_cast<Stmt**>(&Op)+1; |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | // CastExpr |
| 1297 | Stmt::child_iterator CastExpr::child_begin() { |
| 1298 | return reinterpret_cast<Stmt**>(&Op); |
| 1299 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1300 | Stmt::child_iterator CastExpr::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1301 | return reinterpret_cast<Stmt**>(&Op)+1; |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | // BinaryOperator |
| 1305 | Stmt::child_iterator BinaryOperator::child_begin() { |
| 1306 | return reinterpret_cast<Stmt**>(&SubExprs); |
| 1307 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1308 | Stmt::child_iterator BinaryOperator::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1309 | return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR; |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | // ConditionalOperator |
| 1313 | Stmt::child_iterator ConditionalOperator::child_begin() { |
| 1314 | return reinterpret_cast<Stmt**>(&SubExprs); |
| 1315 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1316 | Stmt::child_iterator ConditionalOperator::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1317 | return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR; |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | // AddrLabelExpr |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1321 | Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } |
| 1322 | Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1323 | |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1324 | // StmtExpr |
| 1325 | Stmt::child_iterator StmtExpr::child_begin() { |
| 1326 | return reinterpret_cast<Stmt**>(&SubStmt); |
| 1327 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1328 | Stmt::child_iterator StmtExpr::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1329 | return reinterpret_cast<Stmt**>(&SubStmt)+1; |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | // TypesCompatibleExpr |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1333 | Stmt::child_iterator TypesCompatibleExpr::child_begin() { |
| 1334 | return child_iterator(); |
| 1335 | } |
| 1336 | |
| 1337 | Stmt::child_iterator TypesCompatibleExpr::child_end() { |
| 1338 | return child_iterator(); |
| 1339 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1340 | |
| 1341 | // ChooseExpr |
| 1342 | Stmt::child_iterator ChooseExpr::child_begin() { |
| 1343 | return reinterpret_cast<Stmt**>(&SubExprs); |
| 1344 | } |
| 1345 | |
| 1346 | Stmt::child_iterator ChooseExpr::child_end() { |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1347 | return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR; |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1350 | // OverloadExpr |
| 1351 | Stmt::child_iterator OverloadExpr::child_begin() { |
| 1352 | return reinterpret_cast<Stmt**>(&SubExprs[0]); |
| 1353 | } |
| 1354 | Stmt::child_iterator OverloadExpr::child_end() { |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 1355 | return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1358 | // VAArgExpr |
| 1359 | Stmt::child_iterator VAArgExpr::child_begin() { |
| 1360 | return reinterpret_cast<Stmt**>(&Val); |
| 1361 | } |
| 1362 | |
| 1363 | Stmt::child_iterator VAArgExpr::child_end() { |
| 1364 | return reinterpret_cast<Stmt**>(&Val)+1; |
| 1365 | } |
| 1366 | |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1367 | // InitListExpr |
| 1368 | Stmt::child_iterator InitListExpr::child_begin() { |
Steve Naroff | 84f75c2 | 2008-05-07 17:35:03 +0000 | [diff] [blame] | 1369 | return reinterpret_cast<Stmt**>(InitExprs.size() ? |
| 1370 | &InitExprs[0] : 0); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1371 | } |
| 1372 | Stmt::child_iterator InitListExpr::child_end() { |
Steve Naroff | 84f75c2 | 2008-05-07 17:35:03 +0000 | [diff] [blame] | 1373 | return reinterpret_cast<Stmt**>(InitExprs.size() ? |
| 1374 | &InitExprs[0] + InitExprs.size() : 0); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1377 | // ObjCStringLiteral |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1378 | Stmt::child_iterator ObjCStringLiteral::child_begin() { |
| 1379 | return child_iterator(); |
| 1380 | } |
| 1381 | Stmt::child_iterator ObjCStringLiteral::child_end() { |
| 1382 | return child_iterator(); |
| 1383 | } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1384 | |
| 1385 | // ObjCEncodeExpr |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1386 | Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } |
| 1387 | Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 9fdc8a9 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1388 | |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1389 | // ObjCSelectorExpr |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1390 | Stmt::child_iterator ObjCSelectorExpr::child_begin() { |
| 1391 | return child_iterator(); |
| 1392 | } |
| 1393 | Stmt::child_iterator ObjCSelectorExpr::child_end() { |
| 1394 | return child_iterator(); |
| 1395 | } |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1396 | |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1397 | // ObjCProtocolExpr |
Ted Kremenek | a647855 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1398 | Stmt::child_iterator ObjCProtocolExpr::child_begin() { |
| 1399 | return child_iterator(); |
| 1400 | } |
| 1401 | Stmt::child_iterator ObjCProtocolExpr::child_end() { |
| 1402 | return child_iterator(); |
| 1403 | } |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1404 | |
Steve Naroff | c39ca26 | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1405 | // ObjCMessageExpr |
Ted Kremenek | d029a6f | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1406 | Stmt::child_iterator ObjCMessageExpr::child_begin() { |
| 1407 | return reinterpret_cast<Stmt**>(&SubExprs[ getReceiver() ? 0 : ARGS_START ]); |
Steve Naroff | c39ca26 | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1408 | } |
| 1409 | Stmt::child_iterator ObjCMessageExpr::child_end() { |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1410 | return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]); |
Steve Naroff | c39ca26 | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |