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