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