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