Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Expr.cpp - Expression AST Node Implementation --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr class and subclasses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Expr.h" |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | da5a6b6 | 2007-11-27 18:22:04 +0000 | [diff] [blame^] | 18 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 73 | case OffsetOf: return "__builtin_offsetof"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 77ed8e4 | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | for (unsigned i = 0; i != numargs; ++i) |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 87 | SubExprs[i+ARGS_START] = args[i]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | RParenLoc = rparenloc; |
| 89 | } |
| 90 | |
Steve Naroff | 13b7c5f | 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 | 3ef5bc0 | 2007-11-08 17:56:40 +0000 | [diff] [blame] | 153 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 66b5a8a | 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 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | } |
| 246 | case BinaryOperatorClass: |
| 247 | return cast<BinaryOperator>(this)->isAssignmentOp(); |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 248 | case CompoundAssignOperatorClass: |
Chris Lattner | 1f683e9 | 2007-08-25 01:55:00 +0000 | [diff] [blame] | 249 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | |
| 251 | case MemberExprClass: |
| 252 | case ArraySubscriptExprClass: |
| 253 | // If the base pointer or element is to a volatile pointer/field, accessing |
| 254 | // if is a side effect. |
| 255 | return getType().isVolatileQualified(); |
| 256 | |
| 257 | case CallExprClass: |
| 258 | // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }" |
| 259 | // should warn. |
| 260 | return true; |
Chris Lattner | a9c0102 | 2007-09-26 22:06:30 +0000 | [diff] [blame] | 261 | case ObjCMessageExprClass: |
| 262 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | |
| 264 | case CastExprClass: |
| 265 | // If this is a cast to void, check the operand. Otherwise, the result of |
| 266 | // the cast is unused. |
| 267 | if (getType()->isVoidType()) |
| 268 | return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect(); |
| 269 | return false; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an |
| 274 | /// incomplete type other than void. Nonarray expressions that can be lvalues: |
| 275 | /// - name, where name must be a variable |
| 276 | /// - e[i] |
| 277 | /// - (e), where e must be an lvalue |
| 278 | /// - e.name, where e must be an lvalue |
| 279 | /// - e->name |
| 280 | /// - *e, the type of e cannot be a function type |
| 281 | /// - string-constant |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 282 | /// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension] |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 283 | /// - reference type [C++ [expr]] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | /// |
Bill Wendling | ca51c97 | 2007-07-16 07:07:56 +0000 | [diff] [blame] | 285 | Expr::isLvalueResult Expr::isLvalue() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | // first, check the type (C99 6.3.2.1) |
Chris Lattner | cb4f9a6 | 2007-07-21 05:33:26 +0000 | [diff] [blame] | 287 | if (TR->isFunctionType()) // from isObjectType() |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | return LV_NotObjectType; |
| 289 | |
Steve Naroff | 731ec57 | 2007-07-21 13:32:03 +0000 | [diff] [blame] | 290 | if (TR->isVoidType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | return LV_IncompleteVoidType; |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 292 | |
Chris Lattner | cb4f9a6 | 2007-07-21 05:33:26 +0000 | [diff] [blame] | 293 | if (TR->isReferenceType()) // C++ [expr] |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 294 | return LV_Valid; |
| 295 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | // the type looks fine, now check the expression |
| 297 | switch (getStmtClass()) { |
| 298 | case StringLiteralClass: // C99 6.5.1p4 |
| 299 | case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) |
| 300 | // For vectors, make sure base is an lvalue (i.e. not a function call). |
| 301 | if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) |
| 302 | return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(); |
| 303 | return LV_Valid; |
| 304 | case DeclRefExprClass: // C99 6.5.1p2 |
| 305 | if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) |
| 306 | return LV_Valid; |
| 307 | break; |
Anton Korobeynikov | fdd7566 | 2007-07-12 15:26:50 +0000 | [diff] [blame] | 308 | case MemberExprClass: { // C99 6.5.2.3p4 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | const MemberExpr *m = cast<MemberExpr>(this); |
| 310 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(); |
Anton Korobeynikov | fdd7566 | 2007-07-12 15:26:50 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 312 | case UnaryOperatorClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 313 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 314 | return LV_Valid; // C99 6.5.3p4 |
| 315 | |
| 316 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real || |
| 317 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag) |
| 318 | return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | break; |
| 320 | case ParenExprClass: // C99 6.5.1p5 |
| 321 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(); |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 322 | case OCUVectorElementExprClass: |
| 323 | if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements()) |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 324 | return LV_DuplicateVectorComponents; |
| 325 | return LV_Valid; |
Steve Naroff | 027282d | 2007-11-12 14:34:27 +0000 | [diff] [blame] | 326 | case ObjCIvarRefExprClass: // ObjC instance variables are lvalues. |
| 327 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | default: |
| 329 | break; |
| 330 | } |
| 331 | return LV_InvalidExpression; |
| 332 | } |
| 333 | |
| 334 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 335 | /// does not have an incomplete type, does not have a const-qualified type, and |
| 336 | /// if it is a structure or union, does not have any member (including, |
| 337 | /// recursively, any member or element of all contained aggregates or unions) |
| 338 | /// with a const-qualified type. |
Bill Wendling | ca51c97 | 2007-07-16 07:07:56 +0000 | [diff] [blame] | 339 | Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | isLvalueResult lvalResult = isLvalue(); |
| 341 | |
| 342 | switch (lvalResult) { |
| 343 | case LV_Valid: break; |
| 344 | case LV_NotObjectType: return MLV_NotObjectType; |
| 345 | case LV_IncompleteVoidType: return MLV_IncompleteVoidType; |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 346 | case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | case LV_InvalidExpression: return MLV_InvalidExpression; |
| 348 | } |
| 349 | if (TR.isConstQualified()) |
| 350 | return MLV_ConstQualified; |
| 351 | if (TR->isArrayType()) |
| 352 | return MLV_ArrayType; |
| 353 | if (TR->isIncompleteType()) |
| 354 | return MLV_IncompleteType; |
| 355 | |
| 356 | if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) { |
| 357 | if (r->hasConstFields()) |
| 358 | return MLV_ConstQualified; |
| 359 | } |
| 360 | return MLV_Valid; |
| 361 | } |
| 362 | |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 363 | bool Expr::hasStaticStorage() const { |
| 364 | switch (getStmtClass()) { |
| 365 | default: |
| 366 | return false; |
| 367 | case DeclRefExprClass: { |
| 368 | const Decl *D = cast<DeclRefExpr>(this)->getDecl(); |
| 369 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 370 | return VD->hasStaticStorage(); |
| 371 | return false; |
| 372 | } |
| 373 | case MemberExprClass: |
| 374 | const MemberExpr *M = cast<MemberExpr>(this); |
| 375 | return !M->isArrow() && M->getBase()->hasStaticStorage(); |
| 376 | } |
| 377 | } |
| 378 | |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 379 | bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 380 | switch (getStmtClass()) { |
| 381 | default: |
| 382 | if (Loc) *Loc = getLocStart(); |
| 383 | return false; |
| 384 | case ParenExprClass: |
| 385 | return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc); |
| 386 | case StringLiteralClass: |
Steve Naroff | 5d37e32 | 2007-11-09 15:00:03 +0000 | [diff] [blame] | 387 | case ObjCStringLiteralClass: |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 388 | case FloatingLiteralClass: |
| 389 | case IntegerLiteralClass: |
| 390 | case CharacterLiteralClass: |
| 391 | case ImaginaryLiteralClass: |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 392 | case TypesCompatibleExprClass: |
| 393 | case CXXBoolLiteralExprClass: |
Chris Lattner | 2777e49 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 394 | return true; |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 395 | case CallExprClass: { |
| 396 | const CallExpr *CE = cast<CallExpr>(this); |
| 397 | llvm::APSInt Result(32); |
Hartmut Kaiser | 86fd355 | 2007-09-16 21:35:35 +0000 | [diff] [blame] | 398 | Result.zextOrTrunc( |
| 399 | static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart()))); |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 400 | if (CE->isBuiltinClassifyType(Result)) |
Chris Lattner | 2777e49 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 401 | return true; |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 402 | if (Loc) *Loc = getLocStart(); |
| 403 | return false; |
| 404 | } |
Chris Lattner | 4ef8dd6 | 2007-11-01 02:45:17 +0000 | [diff] [blame] | 405 | case DeclRefExprClass: { |
| 406 | const Decl *D = cast<DeclRefExpr>(this)->getDecl(); |
| 407 | // Accept address of function. |
| 408 | if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D)) |
Chris Lattner | 2777e49 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 409 | return true; |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 410 | if (Loc) *Loc = getLocStart(); |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 411 | if (isa<VarDecl>(D)) |
| 412 | return TR->isArrayType(); |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 413 | return false; |
Chris Lattner | 4ef8dd6 | 2007-11-01 02:45:17 +0000 | [diff] [blame] | 414 | } |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 415 | case UnaryOperatorClass: { |
| 416 | const UnaryOperator *Exp = cast<UnaryOperator>(this); |
| 417 | |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 418 | // C99 6.6p9 |
| 419 | if (Exp->getOpcode() == UnaryOperator::AddrOf) |
| 420 | return Exp->getSubExpr()->hasStaticStorage(); |
| 421 | |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 422 | // Get the operand value. If this is sizeof/alignof, do not evalute the |
| 423 | // operand. This affects C99 6.6p3. |
| 424 | if (!Exp->isSizeOfAlignOfOp() && |
| 425 | !Exp->getSubExpr()->isConstantExpr(Ctx, Loc)) |
| 426 | return false; |
| 427 | |
| 428 | switch (Exp->getOpcode()) { |
| 429 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 430 | // See C99 6.6p3. |
| 431 | default: |
| 432 | if (Loc) *Loc = Exp->getOperatorLoc(); |
| 433 | return false; |
| 434 | case UnaryOperator::Extension: |
| 435 | return true; // FIXME: this is wrong. |
| 436 | case UnaryOperator::SizeOf: |
| 437 | case UnaryOperator::AlignOf: |
| 438 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 439 | if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc)) |
| 440 | return false; |
Chris Lattner | 2777e49 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 441 | return true; |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 442 | case UnaryOperator::LNot: |
| 443 | case UnaryOperator::Plus: |
| 444 | case UnaryOperator::Minus: |
| 445 | case UnaryOperator::Not: |
Chris Lattner | 2777e49 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 446 | return true; |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 447 | } |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 448 | } |
| 449 | case SizeOfAlignOfTypeExprClass: { |
| 450 | const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this); |
| 451 | // alignof always evaluates to a constant. |
| 452 | if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc)) |
| 453 | return false; |
Chris Lattner | 2777e49 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 454 | return true; |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 455 | } |
| 456 | case BinaryOperatorClass: { |
| 457 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
| 458 | |
| 459 | // The LHS of a constant expr is always evaluated and needed. |
| 460 | if (!Exp->getLHS()->isConstantExpr(Ctx, Loc)) |
| 461 | return false; |
| 462 | |
| 463 | if (!Exp->getRHS()->isConstantExpr(Ctx, Loc)) |
| 464 | return false; |
Chris Lattner | 2777e49 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 465 | return true; |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 466 | } |
| 467 | case ImplicitCastExprClass: |
| 468 | case CastExprClass: { |
| 469 | const Expr *SubExpr; |
| 470 | SourceLocation CastLoc; |
| 471 | if (const CastExpr *C = dyn_cast<CastExpr>(this)) { |
| 472 | SubExpr = C->getSubExpr(); |
| 473 | CastLoc = C->getLParenLoc(); |
| 474 | } else { |
| 475 | SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr(); |
| 476 | CastLoc = getLocStart(); |
| 477 | } |
| 478 | if (!SubExpr->isConstantExpr(Ctx, Loc)) { |
| 479 | if (Loc) *Loc = SubExpr->getLocStart(); |
| 480 | return false; |
| 481 | } |
Chris Lattner | 2777e49 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 482 | return true; |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 483 | } |
| 484 | case ConditionalOperatorClass: { |
| 485 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
Chris Lattner | 2777e49 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 486 | if (!Exp->getCond()->isConstantExpr(Ctx, Loc) || |
| 487 | !Exp->getLHS()->isConstantExpr(Ctx, Loc) || |
| 488 | !Exp->getRHS()->isConstantExpr(Ctx, Loc)) |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 489 | return false; |
Chris Lattner | 2777e49 | 2007-10-18 00:20:32 +0000 | [diff] [blame] | 490 | return true; |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | return true; |
| 495 | } |
| 496 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 497 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 498 | /// an integer constant expression. Note: With the introduction of VLA's in |
| 499 | /// C99 the result of the sizeof operator is no longer always a constant |
| 500 | /// expression. The generalization of the wording to include any subexpression |
| 501 | /// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions |
| 502 | /// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, |
| 503 | /// "0 || f()" can be treated as a constant expression. In C90 this expression, |
| 504 | /// occurring in a context requiring a constant, would have been a constraint |
| 505 | /// violation. FIXME: This routine currently implements C90 semantics. |
| 506 | /// To properly implement C99 semantics this routine will need to evaluate |
| 507 | /// expressions involving operators previously mentioned. |
| 508 | |
| 509 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 510 | /// comma, etc |
| 511 | /// |
| 512 | /// FIXME: This should ext-warn on overflow during evaluation! ISO C does not |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 513 | /// permit this. This includes things like (int)1e1000 |
Chris Lattner | ce0afc0 | 2007-07-18 05:21:20 +0000 | [diff] [blame] | 514 | /// |
| 515 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 516 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 517 | /// cast+dereference. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 518 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 519 | SourceLocation *Loc, bool isEvaluated) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 520 | switch (getStmtClass()) { |
| 521 | default: |
| 522 | if (Loc) *Loc = getLocStart(); |
| 523 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 524 | case ParenExprClass: |
| 525 | return cast<ParenExpr>(this)->getSubExpr()-> |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 526 | isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 527 | case IntegerLiteralClass: |
| 528 | Result = cast<IntegerLiteral>(this)->getValue(); |
| 529 | break; |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 530 | case CharacterLiteralClass: { |
| 531 | const CharacterLiteral *CL = cast<CharacterLiteral>(this); |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 532 | Result.zextOrTrunc( |
| 533 | static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc()))); |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 534 | Result = CL->getValue(); |
Chris Lattner | f0fbcb3 | 2007-07-16 21:04:56 +0000 | [diff] [blame] | 535 | Result.setIsUnsigned(!getType()->isSignedIntegerType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 536 | break; |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 537 | } |
Steve Naroff | 7b658aa | 2007-08-02 04:09:23 +0000 | [diff] [blame] | 538 | case TypesCompatibleExprClass: { |
| 539 | const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this); |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 540 | Result.zextOrTrunc( |
| 541 | static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart()))); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 542 | Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2()); |
Steve Naroff | 389cecc | 2007-08-02 00:13:27 +0000 | [diff] [blame] | 543 | break; |
Steve Naroff | 7b658aa | 2007-08-02 04:09:23 +0000 | [diff] [blame] | 544 | } |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 545 | case CallExprClass: { |
| 546 | const CallExpr *CE = cast<CallExpr>(this); |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 547 | Result.zextOrTrunc( |
| 548 | static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart()))); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 549 | if (CE->isBuiltinClassifyType(Result)) |
| 550 | break; |
| 551 | if (Loc) *Loc = getLocStart(); |
| 552 | return false; |
| 553 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 554 | case DeclRefExprClass: |
| 555 | if (const EnumConstantDecl *D = |
| 556 | dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { |
| 557 | Result = D->getInitVal(); |
| 558 | break; |
| 559 | } |
| 560 | if (Loc) *Loc = getLocStart(); |
| 561 | return false; |
| 562 | case UnaryOperatorClass: { |
| 563 | const UnaryOperator *Exp = cast<UnaryOperator>(this); |
| 564 | |
| 565 | // Get the operand value. If this is sizeof/alignof, do not evalute the |
| 566 | // operand. This affects C99 6.6p3. |
Chris Lattner | 602dafd | 2007-08-23 21:42:50 +0000 | [diff] [blame] | 567 | if (!Exp->isSizeOfAlignOfOp() && |
| 568 | !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 569 | return false; |
| 570 | |
| 571 | switch (Exp->getOpcode()) { |
| 572 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 573 | // See C99 6.6p3. |
| 574 | default: |
| 575 | if (Loc) *Loc = Exp->getOperatorLoc(); |
| 576 | return false; |
| 577 | case UnaryOperator::Extension: |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 578 | return true; // FIXME: this is wrong. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 579 | case UnaryOperator::SizeOf: |
| 580 | case UnaryOperator::AlignOf: |
| 581 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 582 | if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 583 | return false; |
| 584 | |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 585 | // Return the result in the right width. |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 586 | Result.zextOrTrunc( |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 587 | static_cast<uint32_t>(Ctx.getTypeSize(getType(), |
| 588 | Exp->getOperatorLoc()))); |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 589 | |
| 590 | // Get information about the size or align. |
Chris Lattner | da5a6b6 | 2007-11-27 18:22:04 +0000 | [diff] [blame^] | 591 | if (Exp->getOpcode() == UnaryOperator::AlignOf) { |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 592 | Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(), |
| 593 | Exp->getOperatorLoc()); |
Chris Lattner | da5a6b6 | 2007-11-27 18:22:04 +0000 | [diff] [blame^] | 594 | } else { |
| 595 | unsigned CharSize = Ctx.Target.getCharWidth(Exp->getOperatorLoc()); |
| 596 | Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(), |
| 597 | Exp->getOperatorLoc()) / CharSize; |
| 598 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 599 | break; |
| 600 | case UnaryOperator::LNot: { |
| 601 | bool Val = Result != 0; |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 602 | Result.zextOrTrunc( |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 603 | static_cast<uint32_t>(Ctx.getTypeSize(getType(), |
| 604 | Exp->getOperatorLoc()))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 605 | Result = Val; |
| 606 | break; |
| 607 | } |
| 608 | case UnaryOperator::Plus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 609 | break; |
| 610 | case UnaryOperator::Minus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 611 | Result = -Result; |
| 612 | break; |
| 613 | case UnaryOperator::Not: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 614 | Result = ~Result; |
| 615 | break; |
| 616 | } |
| 617 | break; |
| 618 | } |
| 619 | case SizeOfAlignOfTypeExprClass: { |
| 620 | const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this); |
| 621 | // alignof always evaluates to a constant. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 622 | if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 623 | return false; |
| 624 | |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 625 | // Return the result in the right width. |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 626 | Result.zextOrTrunc( |
| 627 | static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()))); |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 628 | |
| 629 | // Get information about the size or align. |
| 630 | if (Exp->isSizeOf()) |
| 631 | Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc()); |
| 632 | else |
| 633 | Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | case BinaryOperatorClass: { |
| 637 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
| 638 | |
| 639 | // The LHS of a constant expr is always evaluated and needed. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 640 | if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 641 | return false; |
| 642 | |
| 643 | llvm::APSInt RHS(Result); |
| 644 | |
| 645 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 646 | // RHS. Make sure to pass isEvaluated down correctly. |
| 647 | if (Exp->isLogicalOp()) { |
| 648 | bool RHSEval; |
| 649 | if (Exp->getOpcode() == BinaryOperator::LAnd) |
| 650 | RHSEval = Result != 0; |
| 651 | else { |
| 652 | assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); |
| 653 | RHSEval = Result == 0; |
| 654 | } |
| 655 | |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 656 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | isEvaluated & RHSEval)) |
| 658 | return false; |
| 659 | } else { |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 660 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 661 | return false; |
| 662 | } |
| 663 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 664 | switch (Exp->getOpcode()) { |
| 665 | default: |
| 666 | if (Loc) *Loc = getLocStart(); |
| 667 | return false; |
| 668 | case BinaryOperator::Mul: |
| 669 | Result *= RHS; |
| 670 | break; |
| 671 | case BinaryOperator::Div: |
| 672 | if (RHS == 0) { |
| 673 | if (!isEvaluated) break; |
| 674 | if (Loc) *Loc = getLocStart(); |
| 675 | return false; |
| 676 | } |
| 677 | Result /= RHS; |
| 678 | break; |
| 679 | case BinaryOperator::Rem: |
| 680 | if (RHS == 0) { |
| 681 | if (!isEvaluated) break; |
| 682 | if (Loc) *Loc = getLocStart(); |
| 683 | return false; |
| 684 | } |
| 685 | Result %= RHS; |
| 686 | break; |
| 687 | case BinaryOperator::Add: Result += RHS; break; |
| 688 | case BinaryOperator::Sub: Result -= RHS; break; |
| 689 | case BinaryOperator::Shl: |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 690 | Result <<= |
| 691 | static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 692 | break; |
| 693 | case BinaryOperator::Shr: |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 694 | Result >>= |
| 695 | static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 696 | break; |
| 697 | case BinaryOperator::LT: Result = Result < RHS; break; |
| 698 | case BinaryOperator::GT: Result = Result > RHS; break; |
| 699 | case BinaryOperator::LE: Result = Result <= RHS; break; |
| 700 | case BinaryOperator::GE: Result = Result >= RHS; break; |
| 701 | case BinaryOperator::EQ: Result = Result == RHS; break; |
| 702 | case BinaryOperator::NE: Result = Result != RHS; break; |
| 703 | case BinaryOperator::And: Result &= RHS; break; |
| 704 | case BinaryOperator::Xor: Result ^= RHS; break; |
| 705 | case BinaryOperator::Or: Result |= RHS; break; |
| 706 | case BinaryOperator::LAnd: |
| 707 | Result = Result != 0 && RHS != 0; |
| 708 | break; |
| 709 | case BinaryOperator::LOr: |
| 710 | Result = Result != 0 || RHS != 0; |
| 711 | break; |
| 712 | |
| 713 | case BinaryOperator::Comma: |
| 714 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 715 | // *except* when they are contained within a subexpression that is not |
| 716 | // evaluated". Note that Assignment can never happen due to constraints |
| 717 | // on the LHS subexpr, so we don't need to check it here. |
| 718 | if (isEvaluated) { |
| 719 | if (Loc) *Loc = getLocStart(); |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | // The result of the constant expr is the RHS. |
| 724 | Result = RHS; |
| 725 | return true; |
| 726 | } |
| 727 | |
| 728 | assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); |
| 729 | break; |
| 730 | } |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 731 | case ImplicitCastExprClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 732 | case CastExprClass: { |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 733 | const Expr *SubExpr; |
| 734 | SourceLocation CastLoc; |
| 735 | if (const CastExpr *C = dyn_cast<CastExpr>(this)) { |
| 736 | SubExpr = C->getSubExpr(); |
| 737 | CastLoc = C->getLParenLoc(); |
| 738 | } else { |
| 739 | SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr(); |
| 740 | CastLoc = getLocStart(); |
| 741 | } |
| 742 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 743 | // C99 6.6p6: shall only convert arithmetic types to integer types. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 744 | if (!SubExpr->getType()->isArithmeticType() || |
| 745 | !getType()->isIntegerType()) { |
| 746 | if (Loc) *Loc = SubExpr->getLocStart(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 747 | return false; |
| 748 | } |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 749 | |
| 750 | uint32_t DestWidth = |
| 751 | static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc)); |
| 752 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 753 | // Handle simple integer->integer casts. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 754 | if (SubExpr->getType()->isIntegerType()) { |
| 755 | if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 756 | return false; |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 757 | |
| 758 | // Figure out if this is a truncate, extend or noop cast. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 759 | // If the input is signed, do a sign extend, noop, or truncate. |
| 760 | if (SubExpr->getType()->isSignedIntegerType()) |
| 761 | Result.sextOrTrunc(DestWidth); |
| 762 | else // If the input is unsigned, do a zero extend, noop, or truncate. |
| 763 | Result.zextOrTrunc(DestWidth); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 764 | break; |
| 765 | } |
| 766 | |
| 767 | // Allow floating constants that are the immediate operands of casts or that |
| 768 | // are parenthesized. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 769 | const Expr *Operand = SubExpr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 770 | while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) |
| 771 | Operand = PE->getSubExpr(); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 772 | |
| 773 | // If this isn't a floating literal, we can't handle it. |
| 774 | const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand); |
| 775 | if (!FL) { |
| 776 | if (Loc) *Loc = Operand->getLocStart(); |
| 777 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 778 | } |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 779 | |
| 780 | // Determine whether we are converting to unsigned or signed. |
| 781 | bool DestSigned = getType()->isSignedIntegerType(); |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 782 | |
| 783 | // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can |
| 784 | // be called multiple times per AST. |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 785 | uint64_t Space[4]; |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 786 | (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned, |
| 787 | llvm::APFloat::rmTowardZero); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 788 | Result = llvm::APInt(DestWidth, 4, Space); |
| 789 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 790 | } |
| 791 | case ConditionalOperatorClass: { |
| 792 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
| 793 | |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 794 | if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | return false; |
| 796 | |
| 797 | const Expr *TrueExp = Exp->getLHS(); |
| 798 | const Expr *FalseExp = Exp->getRHS(); |
| 799 | if (Result == 0) std::swap(TrueExp, FalseExp); |
| 800 | |
| 801 | // Evaluate the false one first, discard the result. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 802 | if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 803 | return false; |
| 804 | // Evalute the true one, capture the result. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 805 | if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 806 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 807 | break; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // Cases that are valid constant exprs fall through to here. |
| 812 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
| 813 | return true; |
| 814 | } |
| 815 | |
| 816 | |
| 817 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 818 | /// integer constant expression with the value zero, or if this is one that is |
| 819 | /// cast to void*. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 820 | bool Expr::isNullPointerConstant(ASTContext &Ctx) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 821 | // Strip off a cast to void*, if it exists. |
| 822 | if (const CastExpr *CE = dyn_cast<CastExpr>(this)) { |
| 823 | // Check that it is a cast to void*. |
| 824 | if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) { |
| 825 | QualType Pointee = PT->getPointeeType(); |
| 826 | if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void* |
| 827 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 828 | return CE->getSubExpr()->isNullPointerConstant(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 829 | } |
Steve Naroff | 7269f2d | 2007-08-28 21:20:34 +0000 | [diff] [blame] | 830 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
Steve Naroff | 19a6ebd | 2007-08-29 00:00:02 +0000 | [diff] [blame] | 831 | // Ignore the ImplicitCastExpr type entirely. |
| 832 | return ICE->getSubExpr()->isNullPointerConstant(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 833 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 834 | // Accept ((void*)0) as a null pointer constant, as many other |
| 835 | // implementations do. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 836 | return PE->getSubExpr()->isNullPointerConstant(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | // This expression must be an integer type. |
| 840 | if (!getType()->isIntegerType()) |
| 841 | return false; |
| 842 | |
| 843 | // If we have an integer constant expression, we need to *evaluate* it and |
| 844 | // test for the value 0. |
| 845 | llvm::APSInt Val(32); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 846 | return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 847 | } |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 848 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 849 | unsigned OCUVectorElementExpr::getNumElements() const { |
Chris Lattner | 4d0ac88 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 850 | return strlen(Accessor.getName()); |
| 851 | } |
| 852 | |
| 853 | |
Chris Lattner | cb92a11 | 2007-08-02 21:47:28 +0000 | [diff] [blame] | 854 | /// getComponentType - Determine whether the components of this access are |
| 855 | /// "point" "color" or "texture" elements. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 856 | OCUVectorElementExpr::ElementType |
| 857 | OCUVectorElementExpr::getElementType() const { |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 858 | // derive the component type, no need to waste space. |
| 859 | const char *compStr = Accessor.getName(); |
Chris Lattner | b4878f4 | 2007-08-02 22:20:00 +0000 | [diff] [blame] | 860 | |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 861 | if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point; |
| 862 | if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color; |
Chris Lattner | b4878f4 | 2007-08-02 22:20:00 +0000 | [diff] [blame] | 863 | |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 864 | assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 && |
Chris Lattner | b4878f4 | 2007-08-02 22:20:00 +0000 | [diff] [blame] | 865 | "getComponentType(): Illegal accessor"); |
| 866 | return Texture; |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 867 | } |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 868 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 869 | /// containsDuplicateElements - Return true if any element access is |
Chris Lattner | cb92a11 | 2007-08-02 21:47:28 +0000 | [diff] [blame] | 870 | /// repeated. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 871 | bool OCUVectorElementExpr::containsDuplicateElements() const { |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 872 | const char *compStr = Accessor.getName(); |
| 873 | unsigned length = strlen(compStr); |
| 874 | |
| 875 | for (unsigned i = 0; i < length-1; i++) { |
| 876 | const char *s = compStr+i; |
| 877 | for (const char c = *s++; *s; s++) |
| 878 | if (c == *s) |
| 879 | return true; |
| 880 | } |
| 881 | return false; |
| 882 | } |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 883 | |
| 884 | /// getEncodedElementAccess - We encode fields with two bits per component. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 885 | unsigned OCUVectorElementExpr::getEncodedElementAccess() const { |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 886 | const char *compStr = Accessor.getName(); |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 887 | unsigned length = getNumElements(); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 888 | |
| 889 | unsigned Result = 0; |
| 890 | |
| 891 | while (length--) { |
| 892 | Result <<= 2; |
| 893 | int Idx = OCUVectorType::getAccessorIdx(compStr[length]); |
| 894 | assert(Idx != -1 && "Invalid accessor letter"); |
| 895 | Result |= Idx; |
| 896 | } |
| 897 | return Result; |
| 898 | } |
| 899 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 900 | // constructor for instance messages. |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 901 | ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 902 | QualType retType, ObjcMethodDecl *mproto, |
| 903 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 904 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 905 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
| 906 | MethodProto(mproto), ClassName(0) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 907 | NumArgs = nargs; |
| 908 | SubExprs = new Expr*[NumArgs+1]; |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 909 | SubExprs[RECEIVER] = receiver; |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 910 | if (NumArgs) { |
| 911 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 912 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 913 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 914 | LBracloc = LBrac; |
| 915 | RBracloc = RBrac; |
| 916 | } |
| 917 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 918 | // constructor for class messages. |
| 919 | // FIXME: clsName should be typed to ObjCInterfaceType |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 920 | ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 921 | QualType retType, ObjcMethodDecl *mproto, |
| 922 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 923 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 924 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
| 925 | MethodProto(mproto), ClassName(clsName) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 926 | NumArgs = nargs; |
| 927 | SubExprs = new Expr*[NumArgs+1]; |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 928 | SubExprs[RECEIVER] = 0; |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 929 | if (NumArgs) { |
| 930 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 931 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 932 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 933 | LBracloc = LBrac; |
| 934 | RBracloc = RBrac; |
| 935 | } |
| 936 | |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 937 | |
| 938 | bool ChooseExpr::isConditionTrue(ASTContext &C) const { |
| 939 | llvm::APSInt CondVal(32); |
| 940 | bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C); |
| 941 | assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst; |
| 942 | return CondVal != 0; |
| 943 | } |
| 944 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 945 | //===----------------------------------------------------------------------===// |
| 946 | // Child Iterators for iterating over subexpressions/substatements |
| 947 | //===----------------------------------------------------------------------===// |
| 948 | |
| 949 | // DeclRefExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 950 | Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } |
| 951 | Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 952 | |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 953 | // ObjCIvarRefExpr |
| 954 | Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); } |
| 955 | Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); } |
| 956 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 957 | // PreDefinedExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 958 | Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); } |
| 959 | Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 960 | |
| 961 | // IntegerLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 962 | Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } |
| 963 | Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 964 | |
| 965 | // CharacterLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 966 | Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); } |
| 967 | Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 968 | |
| 969 | // FloatingLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 970 | Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } |
| 971 | Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 972 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 973 | // ImaginaryLiteral |
| 974 | Stmt::child_iterator ImaginaryLiteral::child_begin() { |
| 975 | return reinterpret_cast<Stmt**>(&Val); |
| 976 | } |
| 977 | Stmt::child_iterator ImaginaryLiteral::child_end() { |
| 978 | return reinterpret_cast<Stmt**>(&Val)+1; |
| 979 | } |
| 980 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 981 | // StringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 982 | Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } |
| 983 | Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 984 | |
| 985 | // ParenExpr |
| 986 | Stmt::child_iterator ParenExpr::child_begin() { |
| 987 | return reinterpret_cast<Stmt**>(&Val); |
| 988 | } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 989 | Stmt::child_iterator ParenExpr::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 990 | return reinterpret_cast<Stmt**>(&Val)+1; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | // UnaryOperator |
| 994 | Stmt::child_iterator UnaryOperator::child_begin() { |
| 995 | return reinterpret_cast<Stmt**>(&Val); |
| 996 | } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 997 | Stmt::child_iterator UnaryOperator::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 998 | return reinterpret_cast<Stmt**>(&Val)+1; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | // SizeOfAlignOfTypeExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1002 | Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() { |
| 1003 | return child_iterator(); |
| 1004 | } |
| 1005 | Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() { |
| 1006 | return child_iterator(); |
| 1007 | } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1008 | |
| 1009 | // ArraySubscriptExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1010 | Stmt::child_iterator ArraySubscriptExpr::child_begin() { |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1011 | return reinterpret_cast<Stmt**>(&SubExprs); |
| 1012 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1013 | Stmt::child_iterator ArraySubscriptExpr::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1014 | return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | // CallExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1018 | Stmt::child_iterator CallExpr::child_begin() { |
Ted Kremenek | 42a2977 | 2007-08-27 21:11:44 +0000 | [diff] [blame] | 1019 | return reinterpret_cast<Stmt**>(&SubExprs[0]); |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1020 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1021 | Stmt::child_iterator CallExpr::child_end() { |
Ted Kremenek | 42a2977 | 2007-08-27 21:11:44 +0000 | [diff] [blame] | 1022 | return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]); |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1023 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1024 | |
| 1025 | // MemberExpr |
| 1026 | Stmt::child_iterator MemberExpr::child_begin() { |
| 1027 | return reinterpret_cast<Stmt**>(&Base); |
| 1028 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1029 | Stmt::child_iterator MemberExpr::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1030 | return reinterpret_cast<Stmt**>(&Base)+1; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | // OCUVectorElementExpr |
| 1034 | Stmt::child_iterator OCUVectorElementExpr::child_begin() { |
| 1035 | return reinterpret_cast<Stmt**>(&Base); |
| 1036 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1037 | Stmt::child_iterator OCUVectorElementExpr::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1038 | return reinterpret_cast<Stmt**>(&Base)+1; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | // CompoundLiteralExpr |
| 1042 | Stmt::child_iterator CompoundLiteralExpr::child_begin() { |
| 1043 | return reinterpret_cast<Stmt**>(&Init); |
| 1044 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1045 | Stmt::child_iterator CompoundLiteralExpr::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1046 | return reinterpret_cast<Stmt**>(&Init)+1; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | // ImplicitCastExpr |
| 1050 | Stmt::child_iterator ImplicitCastExpr::child_begin() { |
| 1051 | return reinterpret_cast<Stmt**>(&Op); |
| 1052 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1053 | Stmt::child_iterator ImplicitCastExpr::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1054 | return reinterpret_cast<Stmt**>(&Op)+1; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | // CastExpr |
| 1058 | Stmt::child_iterator CastExpr::child_begin() { |
| 1059 | return reinterpret_cast<Stmt**>(&Op); |
| 1060 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1061 | Stmt::child_iterator CastExpr::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1062 | return reinterpret_cast<Stmt**>(&Op)+1; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | // BinaryOperator |
| 1066 | Stmt::child_iterator BinaryOperator::child_begin() { |
| 1067 | return reinterpret_cast<Stmt**>(&SubExprs); |
| 1068 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1069 | Stmt::child_iterator BinaryOperator::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1070 | return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | // ConditionalOperator |
| 1074 | Stmt::child_iterator ConditionalOperator::child_begin() { |
| 1075 | return reinterpret_cast<Stmt**>(&SubExprs); |
| 1076 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1077 | Stmt::child_iterator ConditionalOperator::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1078 | return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | // AddrLabelExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1082 | Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } |
| 1083 | Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1084 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1085 | // StmtExpr |
| 1086 | Stmt::child_iterator StmtExpr::child_begin() { |
| 1087 | return reinterpret_cast<Stmt**>(&SubStmt); |
| 1088 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1089 | Stmt::child_iterator StmtExpr::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1090 | return reinterpret_cast<Stmt**>(&SubStmt)+1; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | // TypesCompatibleExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1094 | Stmt::child_iterator TypesCompatibleExpr::child_begin() { |
| 1095 | return child_iterator(); |
| 1096 | } |
| 1097 | |
| 1098 | Stmt::child_iterator TypesCompatibleExpr::child_end() { |
| 1099 | return child_iterator(); |
| 1100 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1101 | |
| 1102 | // ChooseExpr |
| 1103 | Stmt::child_iterator ChooseExpr::child_begin() { |
| 1104 | return reinterpret_cast<Stmt**>(&SubExprs); |
| 1105 | } |
| 1106 | |
| 1107 | Stmt::child_iterator ChooseExpr::child_end() { |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1108 | return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1111 | // VAArgExpr |
| 1112 | Stmt::child_iterator VAArgExpr::child_begin() { |
| 1113 | return reinterpret_cast<Stmt**>(&Val); |
| 1114 | } |
| 1115 | |
| 1116 | Stmt::child_iterator VAArgExpr::child_end() { |
| 1117 | return reinterpret_cast<Stmt**>(&Val)+1; |
| 1118 | } |
| 1119 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1120 | // InitListExpr |
| 1121 | Stmt::child_iterator InitListExpr::child_begin() { |
| 1122 | return reinterpret_cast<Stmt**>(&InitExprs[0]); |
| 1123 | } |
| 1124 | Stmt::child_iterator InitListExpr::child_end() { |
| 1125 | return reinterpret_cast<Stmt**>(&InitExprs[NumInits]); |
| 1126 | } |
| 1127 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1128 | // ObjCStringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1129 | Stmt::child_iterator ObjCStringLiteral::child_begin() { |
| 1130 | return child_iterator(); |
| 1131 | } |
| 1132 | Stmt::child_iterator ObjCStringLiteral::child_end() { |
| 1133 | return child_iterator(); |
| 1134 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1135 | |
| 1136 | // ObjCEncodeExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1137 | Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } |
| 1138 | Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1139 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1140 | // ObjCSelectorExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1141 | Stmt::child_iterator ObjCSelectorExpr::child_begin() { |
| 1142 | return child_iterator(); |
| 1143 | } |
| 1144 | Stmt::child_iterator ObjCSelectorExpr::child_end() { |
| 1145 | return child_iterator(); |
| 1146 | } |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1147 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1148 | // ObjCProtocolExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1149 | Stmt::child_iterator ObjCProtocolExpr::child_begin() { |
| 1150 | return child_iterator(); |
| 1151 | } |
| 1152 | Stmt::child_iterator ObjCProtocolExpr::child_end() { |
| 1153 | return child_iterator(); |
| 1154 | } |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1155 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1156 | // ObjCMessageExpr |
| 1157 | Stmt::child_iterator ObjCMessageExpr::child_begin() { |
| 1158 | return reinterpret_cast<Stmt**>(&SubExprs[0]); |
| 1159 | } |
| 1160 | Stmt::child_iterator ObjCMessageExpr::child_end() { |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1161 | return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |