Expr.cpp revision 2085fd6cd22ec5c268175251db10d7c60caf7aaa
1//===--- Expr.cpp - Expression AST Node Implementation --------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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/APValue.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/DeclObjC.h" 18#include "clang/AST/DeclCXX.h" 19#include "clang/AST/DeclTemplate.h" 20#include "clang/AST/RecordLayout.h" 21#include "clang/AST/StmtVisitor.h" 22#include "clang/Basic/TargetInfo.h" 23using namespace clang; 24 25//===----------------------------------------------------------------------===// 26// Primary Expressions. 27//===----------------------------------------------------------------------===// 28 29/// getValueAsApproximateDouble - This returns the value as an inaccurate 30/// double. Note that this may cause loss of precision, but is useful for 31/// debugging dumps, etc. 32double FloatingLiteral::getValueAsApproximateDouble() const { 33 llvm::APFloat V = getValue(); 34 bool ignored; 35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, 36 &ignored); 37 return V.convertToDouble(); 38} 39 40StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData, 41 unsigned ByteLength, bool Wide, 42 QualType Ty, 43 SourceLocation *Loc, unsigned NumStrs) { 44 // Allocate enough space for the StringLiteral plus an array of locations for 45 // any concatenated string tokens. 46 void *Mem = C.Allocate(sizeof(StringLiteral)+ 47 sizeof(SourceLocation)*(NumStrs-1), 48 llvm::alignof<StringLiteral>()); 49 StringLiteral *SL = new (Mem) StringLiteral(Ty); 50 51 // OPTIMIZE: could allocate this appended to the StringLiteral. 52 char *AStrData = new (C, 1) char[ByteLength]; 53 memcpy(AStrData, StrData, ByteLength); 54 SL->StrData = AStrData; 55 SL->ByteLength = ByteLength; 56 SL->IsWide = Wide; 57 SL->TokLocs[0] = Loc[0]; 58 SL->NumConcatenated = NumStrs; 59 60 if (NumStrs != 1) 61 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1)); 62 return SL; 63} 64 65 66void StringLiteral::Destroy(ASTContext &C) { 67 C.Deallocate(const_cast<char*>(StrData)); 68 this->~StringLiteral(); 69 C.Deallocate(this); 70} 71 72/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 73/// corresponds to, e.g. "sizeof" or "[pre]++". 74const char *UnaryOperator::getOpcodeStr(Opcode Op) { 75 switch (Op) { 76 default: assert(0 && "Unknown unary operator"); 77 case PostInc: return "++"; 78 case PostDec: return "--"; 79 case PreInc: return "++"; 80 case PreDec: return "--"; 81 case AddrOf: return "&"; 82 case Deref: return "*"; 83 case Plus: return "+"; 84 case Minus: return "-"; 85 case Not: return "~"; 86 case LNot: return "!"; 87 case Real: return "__real"; 88 case Imag: return "__imag"; 89 case Extension: return "__extension__"; 90 case OffsetOf: return "__builtin_offsetof"; 91 } 92} 93 94//===----------------------------------------------------------------------===// 95// Postfix Operators. 96//===----------------------------------------------------------------------===// 97 98CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, 99 unsigned numargs, QualType t, SourceLocation rparenloc) 100 : Expr(SC, t, 101 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), 102 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)), 103 NumArgs(numargs) { 104 105 SubExprs = new (C) Stmt*[numargs+1]; 106 SubExprs[FN] = fn; 107 for (unsigned i = 0; i != numargs; ++i) 108 SubExprs[i+ARGS_START] = args[i]; 109 110 RParenLoc = rparenloc; 111} 112 113CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, 114 QualType t, SourceLocation rparenloc) 115 : Expr(CallExprClass, t, 116 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), 117 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)), 118 NumArgs(numargs) { 119 120 SubExprs = new (C) Stmt*[numargs+1]; 121 SubExprs[FN] = fn; 122 for (unsigned i = 0; i != numargs; ++i) 123 SubExprs[i+ARGS_START] = args[i]; 124 125 RParenLoc = rparenloc; 126} 127 128void CallExpr::Destroy(ASTContext& C) { 129 DestroyChildren(C); 130 if (SubExprs) C.Deallocate(SubExprs); 131 this->~CallExpr(); 132 C.Deallocate(this); 133} 134 135/// setNumArgs - This changes the number of arguments present in this call. 136/// Any orphaned expressions are deleted by this, and any new operands are set 137/// to null. 138void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) { 139 // No change, just return. 140 if (NumArgs == getNumArgs()) return; 141 142 // If shrinking # arguments, just delete the extras and forgot them. 143 if (NumArgs < getNumArgs()) { 144 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i) 145 getArg(i)->Destroy(C); 146 this->NumArgs = NumArgs; 147 return; 148 } 149 150 // Otherwise, we are growing the # arguments. New an bigger argument array. 151 Stmt **NewSubExprs = new Stmt*[NumArgs+1]; 152 // Copy over args. 153 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i) 154 NewSubExprs[i] = SubExprs[i]; 155 // Null out new args. 156 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i) 157 NewSubExprs[i] = 0; 158 159 delete [] SubExprs; 160 SubExprs = NewSubExprs; 161 this->NumArgs = NumArgs; 162} 163 164/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 165/// not, return 0. 166unsigned CallExpr::isBuiltinCall(ASTContext &Context) const { 167 // All simple function calls (e.g. func()) are implicitly cast to pointer to 168 // function. As a result, we try and obtain the DeclRefExpr from the 169 // ImplicitCastExpr. 170 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); 171 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). 172 return 0; 173 174 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); 175 if (!DRE) 176 return 0; 177 178 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); 179 if (!FDecl) 180 return 0; 181 182 if (!FDecl->getIdentifier()) 183 return 0; 184 185 return FDecl->getBuiltinID(Context); 186} 187 188 189/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 190/// corresponds to, e.g. "<<=". 191const char *BinaryOperator::getOpcodeStr(Opcode Op) { 192 switch (Op) { 193 default: assert(0 && "Unknown binary operator"); 194 case Mul: return "*"; 195 case Div: return "/"; 196 case Rem: return "%"; 197 case Add: return "+"; 198 case Sub: return "-"; 199 case Shl: return "<<"; 200 case Shr: return ">>"; 201 case LT: return "<"; 202 case GT: return ">"; 203 case LE: return "<="; 204 case GE: return ">="; 205 case EQ: return "=="; 206 case NE: return "!="; 207 case And: return "&"; 208 case Xor: return "^"; 209 case Or: return "|"; 210 case LAnd: return "&&"; 211 case LOr: return "||"; 212 case Assign: return "="; 213 case MulAssign: return "*="; 214 case DivAssign: return "/="; 215 case RemAssign: return "%="; 216 case AddAssign: return "+="; 217 case SubAssign: return "-="; 218 case ShlAssign: return "<<="; 219 case ShrAssign: return ">>="; 220 case AndAssign: return "&="; 221 case XorAssign: return "^="; 222 case OrAssign: return "|="; 223 case Comma: return ","; 224 } 225} 226 227InitListExpr::InitListExpr(SourceLocation lbraceloc, 228 Expr **initExprs, unsigned numInits, 229 SourceLocation rbraceloc) 230 : Expr(InitListExprClass, QualType()), 231 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0), 232 UnionFieldInit(0), HadArrayRangeDesignator(false) { 233 234 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits); 235} 236 237void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) { 238 for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); 239 Idx < LastIdx; ++Idx) 240 delete InitExprs[Idx]; 241 InitExprs.resize(NumInits, 0); 242} 243 244Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) { 245 if (Init >= InitExprs.size()) { 246 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0); 247 InitExprs.back() = expr; 248 return 0; 249 } 250 251 Expr *Result = cast_or_null<Expr>(InitExprs[Init]); 252 InitExprs[Init] = expr; 253 return Result; 254} 255 256/// getFunctionType - Return the underlying function type for this block. 257/// 258const FunctionType *BlockExpr::getFunctionType() const { 259 return getType()->getAsBlockPointerType()-> 260 getPointeeType()->getAsFunctionType(); 261} 262 263SourceLocation BlockExpr::getCaretLocation() const { 264 return TheBlock->getCaretLocation(); 265} 266const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); } 267Stmt *BlockExpr::getBody() { return TheBlock->getBody(); } 268 269 270//===----------------------------------------------------------------------===// 271// Generic Expression Routines 272//===----------------------------------------------------------------------===// 273 274/// isUnusedResultAWarning - Return true if this immediate expression should 275/// be warned about if the result is unused. If so, fill in Loc and Ranges 276/// with location to warn on and the source range[s] to report with the 277/// warning. 278bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, 279 SourceRange &R2) const { 280 switch (getStmtClass()) { 281 default: 282 Loc = getExprLoc(); 283 R1 = getSourceRange(); 284 return true; 285 case ParenExprClass: 286 return cast<ParenExpr>(this)->getSubExpr()-> 287 isUnusedResultAWarning(Loc, R1, R2); 288 case UnaryOperatorClass: { 289 const UnaryOperator *UO = cast<UnaryOperator>(this); 290 291 switch (UO->getOpcode()) { 292 default: break; 293 case UnaryOperator::PostInc: 294 case UnaryOperator::PostDec: 295 case UnaryOperator::PreInc: 296 case UnaryOperator::PreDec: // ++/-- 297 return false; // Not a warning. 298 case UnaryOperator::Deref: 299 // Dereferencing a volatile pointer is a side-effect. 300 if (getType().isVolatileQualified()) 301 return false; 302 break; 303 case UnaryOperator::Real: 304 case UnaryOperator::Imag: 305 // accessing a piece of a volatile complex is a side-effect. 306 if (UO->getSubExpr()->getType().isVolatileQualified()) 307 return false; 308 break; 309 case UnaryOperator::Extension: 310 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2); 311 } 312 Loc = UO->getOperatorLoc(); 313 R1 = UO->getSubExpr()->getSourceRange(); 314 return true; 315 } 316 case BinaryOperatorClass: { 317 const BinaryOperator *BO = cast<BinaryOperator>(this); 318 // Consider comma to have side effects if the LHS or RHS does. 319 if (BO->getOpcode() == BinaryOperator::Comma) 320 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) || 321 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2); 322 323 if (BO->isAssignmentOp()) 324 return false; 325 Loc = BO->getOperatorLoc(); 326 R1 = BO->getLHS()->getSourceRange(); 327 R2 = BO->getRHS()->getSourceRange(); 328 return true; 329 } 330 case CompoundAssignOperatorClass: 331 return false; 332 333 case ConditionalOperatorClass: { 334 // The condition must be evaluated, but if either the LHS or RHS is a 335 // warning, warn about them. 336 const ConditionalOperator *Exp = cast<ConditionalOperator>(this); 337 if (Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2)) 338 return true; 339 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2); 340 } 341 342 case MemberExprClass: 343 // If the base pointer or element is to a volatile pointer/field, accessing 344 // it is a side effect. 345 if (getType().isVolatileQualified()) 346 return false; 347 Loc = cast<MemberExpr>(this)->getMemberLoc(); 348 R1 = SourceRange(Loc, Loc); 349 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange(); 350 return true; 351 352 case ArraySubscriptExprClass: 353 // If the base pointer or element is to a volatile pointer/field, accessing 354 // it is a side effect. 355 if (getType().isVolatileQualified()) 356 return false; 357 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc(); 358 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange(); 359 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange(); 360 return true; 361 362 case CallExprClass: 363 case CXXOperatorCallExprClass: { 364 // If this is a direct call, get the callee. 365 const CallExpr *CE = cast<CallExpr>(this); 366 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts(); 367 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) { 368 // If the callee has attribute pure, const, or warn_unused_result, warn 369 // about it. void foo() { strlen("bar"); } should warn. 370 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl())) 371 if (FD->getAttr<WarnUnusedResultAttr>() || 372 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) { 373 Loc = CE->getCallee()->getLocStart(); 374 R1 = CE->getCallee()->getSourceRange(); 375 376 if (unsigned NumArgs = CE->getNumArgs()) 377 R2 = SourceRange(CE->getArg(0)->getLocStart(), 378 CE->getArg(NumArgs-1)->getLocEnd()); 379 return true; 380 } 381 } 382 return false; 383 } 384 case ObjCMessageExprClass: 385 return false; 386 case StmtExprClass: { 387 // Statement exprs don't logically have side effects themselves, but are 388 // sometimes used in macros in ways that give them a type that is unused. 389 // For example ({ blah; foo(); }) will end up with a type if foo has a type. 390 // however, if the result of the stmt expr is dead, we don't want to emit a 391 // warning. 392 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); 393 if (!CS->body_empty()) 394 if (const Expr *E = dyn_cast<Expr>(CS->body_back())) 395 return E->isUnusedResultAWarning(Loc, R1, R2); 396 397 Loc = cast<StmtExpr>(this)->getLParenLoc(); 398 R1 = getSourceRange(); 399 return true; 400 } 401 case CStyleCastExprClass: 402 // If this is a cast to void, check the operand. Otherwise, the result of 403 // the cast is unused. 404 if (getType()->isVoidType()) 405 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc, 406 R1, R2); 407 Loc = cast<CStyleCastExpr>(this)->getLParenLoc(); 408 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange(); 409 return true; 410 case CXXFunctionalCastExprClass: 411 // If this is a cast to void, check the operand. Otherwise, the result of 412 // the cast is unused. 413 if (getType()->isVoidType()) 414 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc, 415 R1, R2); 416 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc(); 417 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange(); 418 return true; 419 420 case ImplicitCastExprClass: 421 // Check the operand, since implicit casts are inserted by Sema 422 return cast<ImplicitCastExpr>(this) 423 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2); 424 425 case CXXDefaultArgExprClass: 426 return cast<CXXDefaultArgExpr>(this) 427 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2); 428 429 case CXXNewExprClass: 430 // FIXME: In theory, there might be new expressions that don't have side 431 // effects (e.g. a placement new with an uninitialized POD). 432 case CXXDeleteExprClass: 433 return false; 434 } 435} 436 437/// DeclCanBeLvalue - Determine whether the given declaration can be 438/// an lvalue. This is a helper routine for isLvalue. 439static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) { 440 // C++ [temp.param]p6: 441 // A non-type non-reference template-parameter is not an lvalue. 442 if (const NonTypeTemplateParmDecl *NTTParm 443 = dyn_cast<NonTypeTemplateParmDecl>(Decl)) 444 return NTTParm->getType()->isReferenceType(); 445 446 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) || 447 // C++ 3.10p2: An lvalue refers to an object or function. 448 (Ctx.getLangOptions().CPlusPlus && 449 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl))); 450} 451 452/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an 453/// incomplete type other than void. Nonarray expressions that can be lvalues: 454/// - name, where name must be a variable 455/// - e[i] 456/// - (e), where e must be an lvalue 457/// - e.name, where e must be an lvalue 458/// - e->name 459/// - *e, the type of e cannot be a function type 460/// - string-constant 461/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension] 462/// - reference type [C++ [expr]] 463/// 464Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { 465 // first, check the type (C99 6.3.2.1). Expressions with function 466 // type in C are not lvalues, but they can be lvalues in C++. 467 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) 468 return LV_NotObjectType; 469 470 // Allow qualified void which is an incomplete type other than void (yuck). 471 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers()) 472 return LV_IncompleteVoidType; 473 474 /// FIXME: Expressions can't have reference type, so the following 475 /// isn't needed. 476 if (TR->isReferenceType()) // C++ [expr] 477 return LV_Valid; 478 479 // the type looks fine, now check the expression 480 switch (getStmtClass()) { 481 case StringLiteralClass: // C99 6.5.1p4 482 return LV_Valid; 483 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) 484 // For vectors, make sure base is an lvalue (i.e. not a function call). 485 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) 486 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx); 487 return LV_Valid; 488 case DeclRefExprClass: 489 case QualifiedDeclRefExprClass: { // C99 6.5.1p2 490 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl(); 491 if (DeclCanBeLvalue(RefdDecl, Ctx)) 492 return LV_Valid; 493 break; 494 } 495 case BlockDeclRefExprClass: { 496 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); 497 if (isa<VarDecl>(BDR->getDecl())) 498 return LV_Valid; 499 break; 500 } 501 case MemberExprClass: { 502 const MemberExpr *m = cast<MemberExpr>(this); 503 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4: 504 NamedDecl *Member = m->getMemberDecl(); 505 // C++ [expr.ref]p4: 506 // If E2 is declared to have type "reference to T", then E1.E2 507 // is an lvalue. 508 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) 509 if (Value->getType()->isReferenceType()) 510 return LV_Valid; 511 512 // -- If E2 is a static data member [...] then E1.E2 is an lvalue. 513 if (isa<CXXClassVarDecl>(Member)) 514 return LV_Valid; 515 516 // -- If E2 is a non-static data member [...]. If E1 is an 517 // lvalue, then E1.E2 is an lvalue. 518 if (isa<FieldDecl>(Member)) 519 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); 520 521 // -- If it refers to a static member function [...], then 522 // E1.E2 is an lvalue. 523 // -- Otherwise, if E1.E2 refers to a non-static member 524 // function [...], then E1.E2 is not an lvalue. 525 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) 526 return Method->isStatic()? LV_Valid : LV_MemberFunction; 527 528 // -- If E2 is a member enumerator [...], the expression E1.E2 529 // is not an lvalue. 530 if (isa<EnumConstantDecl>(Member)) 531 return LV_InvalidExpression; 532 533 // Not an lvalue. 534 return LV_InvalidExpression; 535 } 536 537 // C99 6.5.2.3p4 538 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); 539 } 540 case UnaryOperatorClass: 541 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) 542 return LV_Valid; // C99 6.5.3p4 543 544 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real || 545 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag || 546 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension) 547 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU. 548 549 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1 550 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc || 551 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec)) 552 return LV_Valid; 553 break; 554 case ImplicitCastExprClass: 555 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid 556 : LV_InvalidExpression; 557 case ParenExprClass: // C99 6.5.1p5 558 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx); 559 case BinaryOperatorClass: 560 case CompoundAssignOperatorClass: { 561 const BinaryOperator *BinOp = cast<BinaryOperator>(this); 562 563 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1 564 BinOp->getOpcode() == BinaryOperator::Comma) 565 return BinOp->getRHS()->isLvalue(Ctx); 566 567 // C++ [expr.mptr.oper]p6 568 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD || 569 BinOp->getOpcode() == BinaryOperator::PtrMemI) && 570 !BinOp->getType()->isFunctionType()) 571 return BinOp->getLHS()->isLvalue(Ctx); 572 573 if (!BinOp->isAssignmentOp()) 574 return LV_InvalidExpression; 575 576 if (Ctx.getLangOptions().CPlusPlus) 577 // C++ [expr.ass]p1: 578 // The result of an assignment operation [...] is an lvalue. 579 return LV_Valid; 580 581 582 // C99 6.5.16: 583 // An assignment expression [...] is not an lvalue. 584 return LV_InvalidExpression; 585 } 586 // FIXME: OverloadExprClass 587 case CallExprClass: 588 case CXXOperatorCallExprClass: 589 case CXXMemberCallExprClass: { 590 // C++ [expr.call]p10: 591 // A function call is an lvalue if and only if the result type 592 // is a reference. 593 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType(); 594 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType()) 595 CalleeType = FnTypePtr->getPointeeType(); 596 if (const FunctionType *FnType = CalleeType->getAsFunctionType()) 597 if (FnType->getResultType()->isReferenceType()) 598 return LV_Valid; 599 600 break; 601 } 602 case CompoundLiteralExprClass: // C99 6.5.2.5p5 603 return LV_Valid; 604 case ChooseExprClass: 605 // __builtin_choose_expr is an lvalue if the selected operand is. 606 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx)) 607 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx); 608 else 609 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx); 610 611 case ExtVectorElementExprClass: 612 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements()) 613 return LV_DuplicateVectorComponents; 614 return LV_Valid; 615 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues. 616 return LV_Valid; 617 case ObjCPropertyRefExprClass: // FIXME: check if read-only property. 618 return LV_Valid; 619 case ObjCKVCRefExprClass: // FIXME: check if read-only property. 620 return LV_Valid; 621 case PredefinedExprClass: 622 return LV_Valid; 623 case VAArgExprClass: 624 return LV_NotObjectType; 625 case CXXDefaultArgExprClass: 626 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx); 627 case CXXConditionDeclExprClass: 628 return LV_Valid; 629 case CStyleCastExprClass: 630 case CXXFunctionalCastExprClass: 631 case CXXStaticCastExprClass: 632 case CXXDynamicCastExprClass: 633 case CXXReinterpretCastExprClass: 634 case CXXConstCastExprClass: 635 // The result of an explicit cast is an lvalue if the type we are 636 // casting to is a reference type. See C++ [expr.cast]p1, 637 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2, 638 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1. 639 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType()) 640 return LV_Valid; 641 break; 642 case CXXTypeidExprClass: 643 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ... 644 return LV_Valid; 645 default: 646 break; 647 } 648 return LV_InvalidExpression; 649} 650 651/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 652/// does not have an incomplete type, does not have a const-qualified type, and 653/// if it is a structure or union, does not have any member (including, 654/// recursively, any member or element of all contained aggregates or unions) 655/// with a const-qualified type. 656Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const { 657 isLvalueResult lvalResult = isLvalue(Ctx); 658 659 switch (lvalResult) { 660 case LV_Valid: 661 // C++ 3.10p11: Functions cannot be modified, but pointers to 662 // functions can be modifiable. 663 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) 664 return MLV_NotObjectType; 665 break; 666 667 case LV_NotObjectType: return MLV_NotObjectType; 668 case LV_IncompleteVoidType: return MLV_IncompleteVoidType; 669 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; 670 case LV_InvalidExpression: 671 // If the top level is a C-style cast, and the subexpression is a valid 672 // lvalue, then this is probably a use of the old-school "cast as lvalue" 673 // GCC extension. We don't support it, but we want to produce good 674 // diagnostics when it happens so that the user knows why. 675 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this)) 676 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) 677 return MLV_LValueCast; 678 return MLV_InvalidExpression; 679 case LV_MemberFunction: return MLV_MemberFunction; 680 } 681 682 QualType CT = Ctx.getCanonicalType(getType()); 683 684 if (CT.isConstQualified()) 685 return MLV_ConstQualified; 686 if (CT->isArrayType()) 687 return MLV_ArrayType; 688 if (CT->isIncompleteType()) 689 return MLV_IncompleteType; 690 691 if (const RecordType *r = CT->getAsRecordType()) { 692 if (r->hasConstFields()) 693 return MLV_ConstQualified; 694 } 695 // The following is illegal: 696 // void takeclosure(void (^C)(void)); 697 // void func() { int x = 1; takeclosure(^{ x = 7 }); } 698 // 699 if (getStmtClass() == BlockDeclRefExprClass) { 700 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); 701 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl())) 702 return MLV_NotBlockQualified; 703 } 704 705 // Assigning to an 'implicit' property? 706 else if (getStmtClass() == ObjCKVCRefExprClass) { 707 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this); 708 if (KVCExpr->getSetterMethod() == 0) 709 return MLV_NoSetterProperty; 710 } 711 return MLV_Valid; 712} 713 714/// hasGlobalStorage - Return true if this expression has static storage 715/// duration. This means that the address of this expression is a link-time 716/// constant. 717bool Expr::hasGlobalStorage() const { 718 switch (getStmtClass()) { 719 default: 720 return false; 721 case ParenExprClass: 722 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage(); 723 case ImplicitCastExprClass: 724 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage(); 725 case CompoundLiteralExprClass: 726 return cast<CompoundLiteralExpr>(this)->isFileScope(); 727 case DeclRefExprClass: 728 case QualifiedDeclRefExprClass: { 729 const Decl *D = cast<DeclRefExpr>(this)->getDecl(); 730 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 731 return VD->hasGlobalStorage(); 732 if (isa<FunctionDecl>(D)) 733 return true; 734 return false; 735 } 736 case MemberExprClass: { 737 const MemberExpr *M = cast<MemberExpr>(this); 738 return !M->isArrow() && M->getBase()->hasGlobalStorage(); 739 } 740 case ArraySubscriptExprClass: 741 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage(); 742 case PredefinedExprClass: 743 return true; 744 case CXXDefaultArgExprClass: 745 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage(); 746 } 747} 748 749Expr* Expr::IgnoreParens() { 750 Expr* E = this; 751 while (ParenExpr* P = dyn_cast<ParenExpr>(E)) 752 E = P->getSubExpr(); 753 754 return E; 755} 756 757/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 758/// or CastExprs or ImplicitCastExprs, returning their operand. 759Expr *Expr::IgnoreParenCasts() { 760 Expr *E = this; 761 while (true) { 762 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) 763 E = P->getSubExpr(); 764 else if (CastExpr *P = dyn_cast<CastExpr>(E)) 765 E = P->getSubExpr(); 766 else 767 return E; 768 } 769} 770 771/// hasAnyTypeDependentArguments - Determines if any of the expressions 772/// in Exprs is type-dependent. 773bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) { 774 for (unsigned I = 0; I < NumExprs; ++I) 775 if (Exprs[I]->isTypeDependent()) 776 return true; 777 778 return false; 779} 780 781/// hasAnyValueDependentArguments - Determines if any of the expressions 782/// in Exprs is value-dependent. 783bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) { 784 for (unsigned I = 0; I < NumExprs; ++I) 785 if (Exprs[I]->isValueDependent()) 786 return true; 787 788 return false; 789} 790 791bool Expr::isConstantInitializer(ASTContext &Ctx) const { 792 // This function is attempting whether an expression is an initializer 793 // which can be evaluated at compile-time. isEvaluatable handles most 794 // of the cases, but it can't deal with some initializer-specific 795 // expressions, and it can't deal with aggregates; we deal with those here, 796 // and fall back to isEvaluatable for the other cases. 797 798 switch (getStmtClass()) { 799 default: break; 800 case StringLiteralClass: 801 return true; 802 case CompoundLiteralExprClass: { 803 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer(); 804 return Exp->isConstantInitializer(Ctx); 805 } 806 case InitListExprClass: { 807 const InitListExpr *Exp = cast<InitListExpr>(this); 808 unsigned numInits = Exp->getNumInits(); 809 for (unsigned i = 0; i < numInits; i++) { 810 if (!Exp->getInit(i)->isConstantInitializer(Ctx)) 811 return false; 812 } 813 return true; 814 } 815 case ImplicitValueInitExprClass: 816 return true; 817 case ParenExprClass: { 818 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); 819 } 820 case UnaryOperatorClass: { 821 const UnaryOperator* Exp = cast<UnaryOperator>(this); 822 if (Exp->getOpcode() == UnaryOperator::Extension) 823 return Exp->getSubExpr()->isConstantInitializer(Ctx); 824 break; 825 } 826 case CStyleCastExprClass: 827 // Handle casts with a destination that's a struct or union; this 828 // deals with both the gcc no-op struct cast extension and the 829 // cast-to-union extension. 830 if (getType()->isRecordType()) 831 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); 832 break; 833 case DesignatedInitExprClass: 834 return cast<DesignatedInitExpr>(this)-> 835 getInit()->isConstantInitializer(Ctx); 836 } 837 838 return isEvaluatable(Ctx); 839} 840 841/// isIntegerConstantExpr - this recursive routine will test if an expression is 842/// an integer constant expression. Note: With the introduction of VLA's in 843/// C99 the result of the sizeof operator is no longer always a constant 844/// expression. The generalization of the wording to include any subexpression 845/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions 846/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, 847/// "0 || f()" can be treated as a constant expression. In C90 this expression, 848/// occurring in a context requiring a constant, would have been a constraint 849/// violation. FIXME: This routine currently implements C90 semantics. 850/// To properly implement C99 semantics this routine will need to evaluate 851/// expressions involving operators previously mentioned. 852 853/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, 854/// comma, etc 855/// 856/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not 857/// permit this. This includes things like (int)1e1000 858/// 859/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof 860/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer 861/// cast+dereference. 862bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 863 SourceLocation *Loc, bool isEvaluated) const { 864 if (!isIntegerConstantExprInternal(Result, Ctx, Loc, isEvaluated)) 865 return false; 866 assert(Result == EvaluateAsInt(Ctx) && "Inconsistent Evaluate() result!"); 867 return true; 868} 869 870bool Expr::isIntegerConstantExprInternal(llvm::APSInt &Result, ASTContext &Ctx, 871 SourceLocation *Loc, bool isEvaluated) const { 872 873 // Pretest for integral type; some parts of the code crash for types that 874 // can't be sized. 875 if (!getType()->isIntegralType()) { 876 if (Loc) *Loc = getLocStart(); 877 return false; 878 } 879 switch (getStmtClass()) { 880 default: 881 if (Loc) *Loc = getLocStart(); 882 return false; 883 case ParenExprClass: 884 return cast<ParenExpr>(this)->getSubExpr()-> 885 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); 886 case IntegerLiteralClass: 887 // NOTE: getValue() returns an APInt, we must set sign. 888 Result = cast<IntegerLiteral>(this)->getValue(); 889 Result.setIsUnsigned(getType()->isUnsignedIntegerType()); 890 break; 891 case CharacterLiteralClass: { 892 const CharacterLiteral *CL = cast<CharacterLiteral>(this); 893 Result = Ctx.MakeIntValue(CL->getValue(), getType()); 894 break; 895 } 896 case CXXBoolLiteralExprClass: { 897 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this); 898 Result = Ctx.MakeIntValue(BL->getValue(), getType()); 899 break; 900 } 901 case CXXZeroInitValueExprClass: 902 Result = Ctx.MakeIntValue(0, getType()); 903 break; 904 case TypesCompatibleExprClass: { 905 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this); 906 // Per gcc docs "this built-in function ignores top level 907 // qualifiers". We need to use the canonical version to properly 908 // be able to strip CRV qualifiers from the type. 909 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1()); 910 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2()); 911 Result = Ctx.MakeIntValue(Ctx.typesAreCompatible(T0.getUnqualifiedType(), 912 T1.getUnqualifiedType()), 913 getType()); 914 break; 915 } 916 case CallExprClass: 917 case CXXOperatorCallExprClass: { 918 const CallExpr *CE = cast<CallExpr>(this); 919 920 // If this is a call to a builtin function, constant fold it otherwise 921 // reject it. 922 if (CE->isBuiltinCall(Ctx)) { 923 EvalResult EvalResult; 924 if (CE->Evaluate(EvalResult, Ctx)) { 925 assert(!EvalResult.HasSideEffects && 926 "Foldable builtin call should not have side effects!"); 927 Result = EvalResult.Val.getInt(); 928 break; // It is a constant, expand it. 929 } 930 } 931 932 if (Loc) *Loc = getLocStart(); 933 return false; 934 } 935 case DeclRefExprClass: 936 case QualifiedDeclRefExprClass: 937 if (const EnumConstantDecl *D = 938 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { 939 Result = D->getInitVal(); 940 break; 941 } 942 if (Ctx.getLangOptions().CPlusPlus && 943 getType().getCVRQualifiers() == QualType::Const) { 944 // C++ 7.1.5.1p2 945 // A variable of non-volatile const-qualified integral or enumeration 946 // type initialized by an ICE can be used in ICEs. 947 if (const VarDecl *Dcl = 948 dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) { 949 if (const Expr *Init = Dcl->getInit()) 950 return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); 951 } 952 } 953 if (Loc) *Loc = getLocStart(); 954 return false; 955 case UnaryOperatorClass: { 956 const UnaryOperator *Exp = cast<UnaryOperator>(this); 957 958 // Get the operand value. If this is offsetof, do not evalute the 959 // operand. This affects C99 6.6p3. 960 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()-> 961 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 962 return false; 963 964 switch (Exp->getOpcode()) { 965 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. 966 // See C99 6.6p3. 967 default: 968 if (Loc) *Loc = Exp->getOperatorLoc(); 969 return false; 970 case UnaryOperator::Extension: 971 return true; // FIXME: this is wrong. 972 case UnaryOperator::LNot: { 973 Result = Ctx.MakeIntValue(Result == 0, getType()); 974 break; 975 } 976 case UnaryOperator::Plus: 977 break; 978 case UnaryOperator::Minus: 979 Result = -Result; 980 break; 981 case UnaryOperator::Not: 982 Result = ~Result; 983 break; 984 case UnaryOperator::OffsetOf: 985 Result = Ctx.MakeIntValue(Exp->evaluateOffsetOf(Ctx), getType()); 986 break; 987 } 988 break; 989 } 990 case SizeOfAlignOfExprClass: { 991 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this); 992 993 QualType ArgTy = Exp->getTypeOfArgument(); 994 // sizeof(void) and __alignof__(void) = 1 as a gcc extension. 995 if (ArgTy->isVoidType()) { 996 Result = Ctx.MakeIntValue(1, getType()); 997 break; 998 } 999 1000 // alignof always evaluates to a constant, sizeof does if arg is not VLA. 1001 if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) { 1002 if (Loc) *Loc = Exp->getOperatorLoc(); 1003 return false; 1004 } 1005 1006 // Get information about the size or align. 1007 if (ArgTy->isFunctionType()) { 1008 // GCC extension: sizeof(function) = 1. 1009 Result = Ctx.MakeIntValue(Exp->isSizeOf() ? 1 : 4, getType()); 1010 } else { 1011 unsigned CharSize = Ctx.Target.getCharWidth(); 1012 if (Exp->isSizeOf()) 1013 Result = Ctx.MakeIntValue(Ctx.getTypeSize(ArgTy)/CharSize, getType()); 1014 else 1015 Result = Ctx.MakeIntValue(Ctx.getTypeAlign(ArgTy)/CharSize, getType()); 1016 } 1017 break; 1018 } 1019 case BinaryOperatorClass: { 1020 const BinaryOperator *Exp = cast<BinaryOperator>(this); 1021 llvm::APSInt LHS, RHS; 1022 1023 // Initialize result to have correct signedness and width. 1024 Result = Ctx.MakeIntValue(0, getType()); 1025 1026 // The LHS of a constant expr is always evaluated and needed. 1027 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated)) 1028 return false; 1029 1030 // The short-circuiting &&/|| operators don't necessarily evaluate their 1031 // RHS. Make sure to pass isEvaluated down correctly. 1032 if (Exp->isLogicalOp()) { 1033 bool RHSEval; 1034 if (Exp->getOpcode() == BinaryOperator::LAnd) 1035 RHSEval = LHS != 0; 1036 else { 1037 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); 1038 RHSEval = LHS == 0; 1039 } 1040 1041 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, 1042 isEvaluated & RHSEval)) 1043 return false; 1044 } else { 1045 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) 1046 return false; 1047 } 1048 1049 switch (Exp->getOpcode()) { 1050 default: 1051 if (Loc) *Loc = getLocStart(); 1052 return false; 1053 case BinaryOperator::Mul: 1054 Result = LHS * RHS; 1055 break; 1056 case BinaryOperator::Div: 1057 if (RHS == 0) { 1058 if (!isEvaluated) break; 1059 if (Loc) *Loc = getLocStart(); 1060 return false; 1061 } 1062 Result = LHS / RHS; 1063 break; 1064 case BinaryOperator::Rem: 1065 if (RHS == 0) { 1066 if (!isEvaluated) break; 1067 if (Loc) *Loc = getLocStart(); 1068 return false; 1069 } 1070 Result = LHS % RHS; 1071 break; 1072 case BinaryOperator::Add: Result = LHS + RHS; break; 1073 case BinaryOperator::Sub: Result = LHS - RHS; break; 1074 case BinaryOperator::Shl: 1075 Result = LHS << 1076 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); 1077 break; 1078 case BinaryOperator::Shr: 1079 Result = LHS >> 1080 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); 1081 break; 1082 case BinaryOperator::LT: Result = LHS < RHS; break; 1083 case BinaryOperator::GT: Result = LHS > RHS; break; 1084 case BinaryOperator::LE: Result = LHS <= RHS; break; 1085 case BinaryOperator::GE: Result = LHS >= RHS; break; 1086 case BinaryOperator::EQ: Result = LHS == RHS; break; 1087 case BinaryOperator::NE: Result = LHS != RHS; break; 1088 case BinaryOperator::And: Result = LHS & RHS; break; 1089 case BinaryOperator::Xor: Result = LHS ^ RHS; break; 1090 case BinaryOperator::Or: Result = LHS | RHS; break; 1091 case BinaryOperator::LAnd: 1092 Result = LHS != 0 && RHS != 0; 1093 break; 1094 case BinaryOperator::LOr: 1095 Result = LHS != 0 || RHS != 0; 1096 break; 1097 1098 case BinaryOperator::Comma: 1099 // C99 6.6p3: "shall not contain assignment, ..., or comma operators, 1100 // *except* when they are contained within a subexpression that is not 1101 // evaluated". Note that Assignment can never happen due to constraints 1102 // on the LHS subexpr, so we don't need to check it here. 1103 if (isEvaluated) { 1104 if (Loc) *Loc = getLocStart(); 1105 return false; 1106 } 1107 1108 // The result of the constant expr is the RHS. 1109 Result = RHS; 1110 break; 1111 } 1112 1113 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); 1114 break; 1115 } 1116 case ImplicitCastExprClass: 1117 case CStyleCastExprClass: 1118 case CXXFunctionalCastExprClass: { 1119 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr(); 1120 SourceLocation CastLoc = getLocStart(); 1121 1122 // C99 6.6p6: shall only convert arithmetic types to integer types. 1123 if (!SubExpr->getType()->isArithmeticType() || 1124 !getType()->isIntegerType()) { 1125 if (Loc) *Loc = SubExpr->getLocStart(); 1126 return false; 1127 } 1128 1129 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType())); 1130 1131 // Handle simple integer->integer casts. 1132 if (SubExpr->getType()->isIntegerType()) { 1133 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 1134 return false; 1135 1136 // Figure out if this is a truncate, extend or noop cast. 1137 // If the input is signed, do a sign extend, noop, or truncate. 1138 if (getType()->isBooleanType()) { 1139 // Conversion to bool compares against zero. 1140 Result = Ctx.MakeIntValue(Result != 0, getType()); 1141 } else if (SubExpr->getType()->isSignedIntegerType()) { 1142 Result.sextOrTrunc(DestWidth); 1143 Result.setIsUnsigned(getType()->isUnsignedIntegerType()); 1144 } else { // If the input is unsigned, do a zero extend, noop, 1145 // or truncate. 1146 Result.zextOrTrunc(DestWidth); 1147 Result.setIsUnsigned(getType()->isUnsignedIntegerType()); 1148 } 1149 break; 1150 } 1151 1152 // Allow floating constants that are the immediate operands of casts or that 1153 // are parenthesized. 1154 const Expr *Operand = SubExpr->IgnoreParens(); 1155 1156 // If this isn't a floating literal, we can't handle it. 1157 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand); 1158 if (!FL) { 1159 if (Loc) *Loc = Operand->getLocStart(); 1160 return false; 1161 } 1162 1163 // If the destination is boolean, compare against zero. 1164 if (getType()->isBooleanType()) { 1165 Result = Ctx.MakeIntValue(!FL->getValue().isZero(), getType()); 1166 break; 1167 } 1168 1169 // Determine whether we are converting to unsigned or signed. 1170 bool DestSigned = getType()->isSignedIntegerType(); 1171 1172 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can 1173 // be called multiple times per AST. 1174 uint64_t Space[4]; 1175 bool ignored; 1176 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned, 1177 llvm::APFloat::rmTowardZero, 1178 &ignored); 1179 Result = llvm::APInt(DestWidth, 4, Space); 1180 Result.setIsUnsigned(getType()->isUnsignedIntegerType()); 1181 break; 1182 } 1183 case ConditionalOperatorClass: { 1184 const ConditionalOperator *Exp = cast<ConditionalOperator>(this); 1185 1186 const Expr *Cond = Exp->getCond(); 1187 1188 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 1189 return false; 1190 1191 const Expr *TrueExp = Exp->getLHS(); 1192 const Expr *FalseExp = Exp->getRHS(); 1193 if (Result == 0) std::swap(TrueExp, FalseExp); 1194 1195 // If the condition (ignoring parens) is a __builtin_constant_p call, 1196 // then only the true side is actually considered in an integer constant 1197 // expression, and it is fully evaluated. This is an important GNU 1198 // extension. See GCC PR38377 for discussion. 1199 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts())) 1200 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) { 1201 EvalResult EVResult; 1202 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects) 1203 return false; 1204 assert(EVResult.Val.isInt() && "FP conditional expr not expected"); 1205 Result = EVResult.Val.getInt(); 1206 if (Loc) *Loc = EVResult.DiagLoc; 1207 return true; 1208 } 1209 1210 // Evaluate the false one first, discard the result. 1211 llvm::APSInt Tmp; 1212 if (FalseExp && !FalseExp->isIntegerConstantExpr(Tmp, Ctx, Loc, false)) 1213 return false; 1214 // Evalute the true one, capture the result. Note that if TrueExp 1215 // is False then this is an instant of the gcc missing LHS 1216 // extension, and we will just reuse Result. 1217 if (TrueExp && 1218 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 1219 return false; 1220 break; 1221 } 1222 case CXXDefaultArgExprClass: 1223 return cast<CXXDefaultArgExpr>(this) 1224 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); 1225 1226 case UnaryTypeTraitExprClass: 1227 Result = Ctx.MakeIntValue(cast<UnaryTypeTraitExpr>(this)->EvaluateTrait(), 1228 getType()); 1229 break; 1230 } 1231 1232 return true; 1233} 1234 1235/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an 1236/// integer constant expression with the value zero, or if this is one that is 1237/// cast to void*. 1238bool Expr::isNullPointerConstant(ASTContext &Ctx) const 1239{ 1240 // Strip off a cast to void*, if it exists. Except in C++. 1241 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { 1242 if (!Ctx.getLangOptions().CPlusPlus) { 1243 // Check that it is a cast to void*. 1244 if (const PointerType *PT = CE->getType()->getAsPointerType()) { 1245 QualType Pointee = PT->getPointeeType(); 1246 if (Pointee.getCVRQualifiers() == 0 && 1247 Pointee->isVoidType() && // to void* 1248 CE->getSubExpr()->getType()->isIntegerType()) // from int. 1249 return CE->getSubExpr()->isNullPointerConstant(Ctx); 1250 } 1251 } 1252 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { 1253 // Ignore the ImplicitCastExpr type entirely. 1254 return ICE->getSubExpr()->isNullPointerConstant(Ctx); 1255 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { 1256 // Accept ((void*)0) as a null pointer constant, as many other 1257 // implementations do. 1258 return PE->getSubExpr()->isNullPointerConstant(Ctx); 1259 } else if (const CXXDefaultArgExpr *DefaultArg 1260 = dyn_cast<CXXDefaultArgExpr>(this)) { 1261 // See through default argument expressions 1262 return DefaultArg->getExpr()->isNullPointerConstant(Ctx); 1263 } else if (isa<GNUNullExpr>(this)) { 1264 // The GNU __null extension is always a null pointer constant. 1265 return true; 1266 } 1267 1268 // This expression must be an integer type. 1269 if (!getType()->isIntegerType()) 1270 return false; 1271 1272 // If we have an integer constant expression, we need to *evaluate* it and 1273 // test for the value 0. 1274 // FIXME: We should probably return false if we're compiling in strict mode 1275 // and Diag is not null (this indicates that the value was foldable but not 1276 // an ICE. 1277 EvalResult Result; 1278 return Evaluate(Result, Ctx) && !Result.HasSideEffects && 1279 Result.Val.isInt() && Result.Val.getInt() == 0; 1280} 1281 1282/// isBitField - Return true if this expression is a bit-field. 1283bool Expr::isBitField() { 1284 Expr *E = this->IgnoreParenCasts(); 1285 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) 1286 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) 1287 return Field->isBitField(); 1288 return false; 1289} 1290 1291/// isArrow - Return true if the base expression is a pointer to vector, 1292/// return false if the base expression is a vector. 1293bool ExtVectorElementExpr::isArrow() const { 1294 return getBase()->getType()->isPointerType(); 1295} 1296 1297unsigned ExtVectorElementExpr::getNumElements() const { 1298 if (const VectorType *VT = getType()->getAsVectorType()) 1299 return VT->getNumElements(); 1300 return 1; 1301} 1302 1303/// containsDuplicateElements - Return true if any element access is repeated. 1304bool ExtVectorElementExpr::containsDuplicateElements() const { 1305 const char *compStr = Accessor.getName(); 1306 unsigned length = Accessor.getLength(); 1307 1308 // Halving swizzles do not contain duplicate elements. 1309 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || 1310 !strcmp(compStr, "even") || !strcmp(compStr, "odd")) 1311 return false; 1312 1313 // Advance past s-char prefix on hex swizzles. 1314 if (*compStr == 's') { 1315 compStr++; 1316 length--; 1317 } 1318 1319 for (unsigned i = 0; i != length-1; i++) { 1320 const char *s = compStr+i; 1321 for (const char c = *s++; *s; s++) 1322 if (c == *s) 1323 return true; 1324 } 1325 return false; 1326} 1327 1328/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. 1329void ExtVectorElementExpr::getEncodedElementAccess( 1330 llvm::SmallVectorImpl<unsigned> &Elts) const { 1331 const char *compStr = Accessor.getName(); 1332 if (*compStr == 's') 1333 compStr++; 1334 1335 bool isHi = !strcmp(compStr, "hi"); 1336 bool isLo = !strcmp(compStr, "lo"); 1337 bool isEven = !strcmp(compStr, "even"); 1338 bool isOdd = !strcmp(compStr, "odd"); 1339 1340 for (unsigned i = 0, e = getNumElements(); i != e; ++i) { 1341 uint64_t Index; 1342 1343 if (isHi) 1344 Index = e + i; 1345 else if (isLo) 1346 Index = i; 1347 else if (isEven) 1348 Index = 2 * i; 1349 else if (isOdd) 1350 Index = 2 * i + 1; 1351 else 1352 Index = ExtVectorType::getAccessorIdx(compStr[i]); 1353 1354 Elts.push_back(Index); 1355 } 1356} 1357 1358// constructor for instance messages. 1359ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, 1360 QualType retType, ObjCMethodDecl *mproto, 1361 SourceLocation LBrac, SourceLocation RBrac, 1362 Expr **ArgExprs, unsigned nargs) 1363 : Expr(ObjCMessageExprClass, retType), SelName(selInfo), 1364 MethodProto(mproto) { 1365 NumArgs = nargs; 1366 SubExprs = new Stmt*[NumArgs+1]; 1367 SubExprs[RECEIVER] = receiver; 1368 if (NumArgs) { 1369 for (unsigned i = 0; i != NumArgs; ++i) 1370 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 1371 } 1372 LBracloc = LBrac; 1373 RBracloc = RBrac; 1374} 1375 1376// constructor for class messages. 1377// FIXME: clsName should be typed to ObjCInterfaceType 1378ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, 1379 QualType retType, ObjCMethodDecl *mproto, 1380 SourceLocation LBrac, SourceLocation RBrac, 1381 Expr **ArgExprs, unsigned nargs) 1382 : Expr(ObjCMessageExprClass, retType), SelName(selInfo), 1383 MethodProto(mproto) { 1384 NumArgs = nargs; 1385 SubExprs = new Stmt*[NumArgs+1]; 1386 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown); 1387 if (NumArgs) { 1388 for (unsigned i = 0; i != NumArgs; ++i) 1389 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 1390 } 1391 LBracloc = LBrac; 1392 RBracloc = RBrac; 1393} 1394 1395// constructor for class messages. 1396ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, 1397 QualType retType, ObjCMethodDecl *mproto, 1398 SourceLocation LBrac, SourceLocation RBrac, 1399 Expr **ArgExprs, unsigned nargs) 1400: Expr(ObjCMessageExprClass, retType), SelName(selInfo), 1401MethodProto(mproto) { 1402 NumArgs = nargs; 1403 SubExprs = new Stmt*[NumArgs+1]; 1404 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown); 1405 if (NumArgs) { 1406 for (unsigned i = 0; i != NumArgs; ++i) 1407 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 1408 } 1409 LBracloc = LBrac; 1410 RBracloc = RBrac; 1411} 1412 1413ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const { 1414 uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; 1415 switch (x & Flags) { 1416 default: 1417 assert(false && "Invalid ObjCMessageExpr."); 1418 case IsInstMeth: 1419 return ClassInfo(0, 0); 1420 case IsClsMethDeclUnknown: 1421 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags)); 1422 case IsClsMethDeclKnown: { 1423 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags); 1424 return ClassInfo(D, D->getIdentifier()); 1425 } 1426 } 1427} 1428 1429bool ChooseExpr::isConditionTrue(ASTContext &C) const { 1430 return getCond()->getIntegerConstantExprValue(C) != 0; 1431} 1432 1433static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) { 1434 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 1435 QualType Ty = ME->getBase()->getType(); 1436 1437 RecordDecl *RD = Ty->getAsRecordType()->getDecl(); 1438 const ASTRecordLayout &RL = C.getASTRecordLayout(RD); 1439 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { 1440 // FIXME: This is linear time. And the fact that we're indexing 1441 // into the layout by position in the record means that we're 1442 // either stuck numbering the fields in the AST or we have to keep 1443 // the linear search (yuck and yuck). 1444 unsigned i = 0; 1445 for (RecordDecl::field_iterator Field = RD->field_begin(), 1446 FieldEnd = RD->field_end(); 1447 Field != FieldEnd; (void)++Field, ++i) { 1448 if (*Field == FD) 1449 break; 1450 } 1451 1452 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase()); 1453 } 1454 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { 1455 const Expr *Base = ASE->getBase(); 1456 1457 int64_t size = C.getTypeSize(ASE->getType()); 1458 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue(); 1459 1460 return size + evaluateOffsetOf(C, Base); 1461 } else if (isa<CompoundLiteralExpr>(E)) 1462 return 0; 1463 1464 assert(0 && "Unknown offsetof subexpression!"); 1465 return 0; 1466} 1467 1468int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const 1469{ 1470 assert(Opc == OffsetOf && "Unary operator not offsetof!"); 1471 1472 unsigned CharSize = C.Target.getCharWidth(); 1473 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize; 1474} 1475 1476void SizeOfAlignOfExpr::Destroy(ASTContext& C) { 1477 // Override default behavior of traversing children. If this has a type 1478 // operand and the type is a variable-length array, the child iteration 1479 // will iterate over the size expression. However, this expression belongs 1480 // to the type, not to this, so we don't want to delete it. 1481 // We still want to delete this expression. 1482 if (isArgumentType()) { 1483 this->~SizeOfAlignOfExpr(); 1484 C.Deallocate(this); 1485 } 1486 else 1487 Expr::Destroy(C); 1488} 1489 1490void OverloadExpr::Destroy(ASTContext& C) { 1491 DestroyChildren(C); 1492 C.Deallocate(SubExprs); 1493 this->~OverloadExpr(); 1494 C.Deallocate(this); 1495} 1496 1497//===----------------------------------------------------------------------===// 1498// DesignatedInitExpr 1499//===----------------------------------------------------------------------===// 1500 1501IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() { 1502 assert(Kind == FieldDesignator && "Only valid on a field designator"); 1503 if (Field.NameOrField & 0x01) 1504 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); 1505 else 1506 return getField()->getIdentifier(); 1507} 1508 1509DesignatedInitExpr * 1510DesignatedInitExpr::Create(ASTContext &C, Designator *Designators, 1511 unsigned NumDesignators, 1512 Expr **IndexExprs, unsigned NumIndexExprs, 1513 SourceLocation ColonOrEqualLoc, 1514 bool UsesColonSyntax, Expr *Init) { 1515 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + 1516 sizeof(Designator) * NumDesignators + 1517 sizeof(Stmt *) * (NumIndexExprs + 1), 8); 1518 DesignatedInitExpr *DIE 1519 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, 1520 ColonOrEqualLoc, UsesColonSyntax, 1521 NumIndexExprs + 1); 1522 1523 // Fill in the designators 1524 unsigned ExpectedNumSubExprs = 0; 1525 designators_iterator Desig = DIE->designators_begin(); 1526 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) { 1527 new (static_cast<void*>(Desig)) Designator(Designators[Idx]); 1528 if (Designators[Idx].isArrayDesignator()) 1529 ++ExpectedNumSubExprs; 1530 else if (Designators[Idx].isArrayRangeDesignator()) 1531 ExpectedNumSubExprs += 2; 1532 } 1533 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!"); 1534 1535 // Fill in the subexpressions, including the initializer expression. 1536 child_iterator Child = DIE->child_begin(); 1537 *Child++ = Init; 1538 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child) 1539 *Child = IndexExprs[Idx]; 1540 1541 return DIE; 1542} 1543 1544SourceRange DesignatedInitExpr::getSourceRange() const { 1545 SourceLocation StartLoc; 1546 Designator &First = 1547 *const_cast<DesignatedInitExpr*>(this)->designators_begin(); 1548 if (First.isFieldDesignator()) { 1549 if (UsesColonSyntax) 1550 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); 1551 else 1552 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); 1553 } else 1554 StartLoc = 1555 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); 1556 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd()); 1557} 1558 1559DesignatedInitExpr::designators_iterator 1560DesignatedInitExpr::designators_begin() { 1561 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 1562 Ptr += sizeof(DesignatedInitExpr); 1563 return static_cast<Designator*>(static_cast<void*>(Ptr)); 1564} 1565 1566DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() { 1567 return designators_begin() + NumDesignators; 1568} 1569 1570Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) { 1571 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); 1572 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 1573 Ptr += sizeof(DesignatedInitExpr); 1574 Ptr += sizeof(Designator) * NumDesignators; 1575 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); 1576 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); 1577} 1578 1579Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) { 1580 assert(D.Kind == Designator::ArrayRangeDesignator && 1581 "Requires array range designator"); 1582 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 1583 Ptr += sizeof(DesignatedInitExpr); 1584 Ptr += sizeof(Designator) * NumDesignators; 1585 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); 1586 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); 1587} 1588 1589Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { 1590 assert(D.Kind == Designator::ArrayRangeDesignator && 1591 "Requires array range designator"); 1592 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 1593 Ptr += sizeof(DesignatedInitExpr); 1594 Ptr += sizeof(Designator) * NumDesignators; 1595 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); 1596 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2)); 1597} 1598 1599//===----------------------------------------------------------------------===// 1600// ExprIterator. 1601//===----------------------------------------------------------------------===// 1602 1603Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } 1604Expr* ExprIterator::operator*() const { return cast<Expr>(*I); } 1605Expr* ExprIterator::operator->() const { return cast<Expr>(*I); } 1606const Expr* ConstExprIterator::operator[](size_t idx) const { 1607 return cast<Expr>(I[idx]); 1608} 1609const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } 1610const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } 1611 1612//===----------------------------------------------------------------------===// 1613// Child Iterators for iterating over subexpressions/substatements 1614//===----------------------------------------------------------------------===// 1615 1616// DeclRefExpr 1617Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } 1618Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } 1619 1620// ObjCIvarRefExpr 1621Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; } 1622Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } 1623 1624// ObjCPropertyRefExpr 1625Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; } 1626Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } 1627 1628// ObjCKVCRefExpr 1629Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; } 1630Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; } 1631 1632// ObjCSuperExpr 1633Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); } 1634Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); } 1635 1636// PredefinedExpr 1637Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } 1638Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } 1639 1640// IntegerLiteral 1641Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } 1642Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } 1643 1644// CharacterLiteral 1645Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();} 1646Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } 1647 1648// FloatingLiteral 1649Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } 1650Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } 1651 1652// ImaginaryLiteral 1653Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; } 1654Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; } 1655 1656// StringLiteral 1657Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } 1658Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); } 1659 1660// ParenExpr 1661Stmt::child_iterator ParenExpr::child_begin() { return &Val; } 1662Stmt::child_iterator ParenExpr::child_end() { return &Val+1; } 1663 1664// UnaryOperator 1665Stmt::child_iterator UnaryOperator::child_begin() { return &Val; } 1666Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; } 1667 1668// SizeOfAlignOfExpr 1669Stmt::child_iterator SizeOfAlignOfExpr::child_begin() { 1670 // If this is of a type and the type is a VLA type (and not a typedef), the 1671 // size expression of the VLA needs to be treated as an executable expression. 1672 // Why isn't this weirdness documented better in StmtIterator? 1673 if (isArgumentType()) { 1674 if (VariableArrayType* T = dyn_cast<VariableArrayType>( 1675 getArgumentType().getTypePtr())) 1676 return child_iterator(T); 1677 return child_iterator(); 1678 } 1679 return child_iterator(&Argument.Ex); 1680} 1681Stmt::child_iterator SizeOfAlignOfExpr::child_end() { 1682 if (isArgumentType()) 1683 return child_iterator(); 1684 return child_iterator(&Argument.Ex + 1); 1685} 1686 1687// ArraySubscriptExpr 1688Stmt::child_iterator ArraySubscriptExpr::child_begin() { 1689 return &SubExprs[0]; 1690} 1691Stmt::child_iterator ArraySubscriptExpr::child_end() { 1692 return &SubExprs[0]+END_EXPR; 1693} 1694 1695// CallExpr 1696Stmt::child_iterator CallExpr::child_begin() { 1697 return &SubExprs[0]; 1698} 1699Stmt::child_iterator CallExpr::child_end() { 1700 return &SubExprs[0]+NumArgs+ARGS_START; 1701} 1702 1703// MemberExpr 1704Stmt::child_iterator MemberExpr::child_begin() { return &Base; } 1705Stmt::child_iterator MemberExpr::child_end() { return &Base+1; } 1706 1707// ExtVectorElementExpr 1708Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; } 1709Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; } 1710 1711// CompoundLiteralExpr 1712Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; } 1713Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; } 1714 1715// CastExpr 1716Stmt::child_iterator CastExpr::child_begin() { return &Op; } 1717Stmt::child_iterator CastExpr::child_end() { return &Op+1; } 1718 1719// BinaryOperator 1720Stmt::child_iterator BinaryOperator::child_begin() { 1721 return &SubExprs[0]; 1722} 1723Stmt::child_iterator BinaryOperator::child_end() { 1724 return &SubExprs[0]+END_EXPR; 1725} 1726 1727// ConditionalOperator 1728Stmt::child_iterator ConditionalOperator::child_begin() { 1729 return &SubExprs[0]; 1730} 1731Stmt::child_iterator ConditionalOperator::child_end() { 1732 return &SubExprs[0]+END_EXPR; 1733} 1734 1735// AddrLabelExpr 1736Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } 1737Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } 1738 1739// StmtExpr 1740Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; } 1741Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; } 1742 1743// TypesCompatibleExpr 1744Stmt::child_iterator TypesCompatibleExpr::child_begin() { 1745 return child_iterator(); 1746} 1747 1748Stmt::child_iterator TypesCompatibleExpr::child_end() { 1749 return child_iterator(); 1750} 1751 1752// ChooseExpr 1753Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; } 1754Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; } 1755 1756// GNUNullExpr 1757Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); } 1758Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); } 1759 1760// OverloadExpr 1761Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; } 1762Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; } 1763 1764// ShuffleVectorExpr 1765Stmt::child_iterator ShuffleVectorExpr::child_begin() { 1766 return &SubExprs[0]; 1767} 1768Stmt::child_iterator ShuffleVectorExpr::child_end() { 1769 return &SubExprs[0]+NumExprs; 1770} 1771 1772// VAArgExpr 1773Stmt::child_iterator VAArgExpr::child_begin() { return &Val; } 1774Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; } 1775 1776// InitListExpr 1777Stmt::child_iterator InitListExpr::child_begin() { 1778 return InitExprs.size() ? &InitExprs[0] : 0; 1779} 1780Stmt::child_iterator InitListExpr::child_end() { 1781 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0; 1782} 1783 1784// DesignatedInitExpr 1785Stmt::child_iterator DesignatedInitExpr::child_begin() { 1786 char* Ptr = static_cast<char*>(static_cast<void *>(this)); 1787 Ptr += sizeof(DesignatedInitExpr); 1788 Ptr += sizeof(Designator) * NumDesignators; 1789 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); 1790} 1791Stmt::child_iterator DesignatedInitExpr::child_end() { 1792 return child_iterator(&*child_begin() + NumSubExprs); 1793} 1794 1795// ImplicitValueInitExpr 1796Stmt::child_iterator ImplicitValueInitExpr::child_begin() { 1797 return child_iterator(); 1798} 1799 1800Stmt::child_iterator ImplicitValueInitExpr::child_end() { 1801 return child_iterator(); 1802} 1803 1804// ObjCStringLiteral 1805Stmt::child_iterator ObjCStringLiteral::child_begin() { 1806 return child_iterator(); 1807} 1808Stmt::child_iterator ObjCStringLiteral::child_end() { 1809 return child_iterator(); 1810} 1811 1812// ObjCEncodeExpr 1813Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } 1814Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } 1815 1816// ObjCSelectorExpr 1817Stmt::child_iterator ObjCSelectorExpr::child_begin() { 1818 return child_iterator(); 1819} 1820Stmt::child_iterator ObjCSelectorExpr::child_end() { 1821 return child_iterator(); 1822} 1823 1824// ObjCProtocolExpr 1825Stmt::child_iterator ObjCProtocolExpr::child_begin() { 1826 return child_iterator(); 1827} 1828Stmt::child_iterator ObjCProtocolExpr::child_end() { 1829 return child_iterator(); 1830} 1831 1832// ObjCMessageExpr 1833Stmt::child_iterator ObjCMessageExpr::child_begin() { 1834 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START; 1835} 1836Stmt::child_iterator ObjCMessageExpr::child_end() { 1837 return &SubExprs[0]+ARGS_START+getNumArgs(); 1838} 1839 1840// Blocks 1841Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); } 1842Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); } 1843 1844Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();} 1845Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); } 1846