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