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