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