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