Expr.cpp revision c0ac4923f08b25ae973a8ee7942cf3eb89da57b7
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Expr.cpp - Expression AST Node Implementation --------------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements the Expr class and subclasses. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Expr.h" 15a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner#include "clang/AST/APValue.h" 162eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner#include "clang/AST/ASTContext.h" 17a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner#include "clang/AST/DeclObjC.h" 1898cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor#include "clang/AST/DeclCXX.h" 19e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/RecordLayout.h" 205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/StmtVisitor.h" 21da5a6b6d9fd52899499d5b7b46273ec844dcaa6eChris Lattner#include "clang/Basic/TargetInfo.h" 225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Expressions. 265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 28da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// getValueAsApproximateDouble - This returns the value as an inaccurate 29da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// double. Note that this may cause loss of precision, but is useful for 30da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// debugging dumps, etc. 31da8249e57f3badecf925571881fe57243935c6c1Chris Lattnerdouble FloatingLiteral::getValueAsApproximateDouble() const { 32da8249e57f3badecf925571881fe57243935c6c1Chris Lattner llvm::APFloat V = getValue(); 33ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen bool ignored; 34ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, 35ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen &ignored); 36da8249e57f3badecf925571881fe57243935c6c1Chris Lattner return V.convertToDouble(); 37da8249e57f3badecf925571881fe57243935c6c1Chris Lattner} 38da8249e57f3badecf925571881fe57243935c6c1Chris Lattner 39da8249e57f3badecf925571881fe57243935c6c1Chris Lattner 405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerStringLiteral::StringLiteral(const char *strData, unsigned byteLength, 415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool Wide, QualType t, SourceLocation firstLoc, 425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation lastLoc) : 435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Expr(StringLiteralClass, t) { 445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // OPTIMIZE: could allocate this appended to the StringLiteral. 455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char *AStrData = new char[byteLength]; 465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer memcpy(AStrData, strData, byteLength); 475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer StrData = AStrData; 485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ByteLength = byteLength; 495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsWide = Wide; 505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer firstTokLoc = firstLoc; 515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer lastTokLoc = lastLoc; 525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerStringLiteral::~StringLiteral() { 555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer delete[] StrData; 565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool UnaryOperator::isPostfix(Opcode Op) { 595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Op) { 605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case PostInc: 615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case PostDec: 625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 685a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenekbool UnaryOperator::isPrefix(Opcode Op) { 695a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek switch (Op) { 705a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek case PreInc: 715a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek case PreDec: 725a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek return true; 735a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek default: 745a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek return false; 755a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek } 765a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek} 775a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek 785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "sizeof" or "[pre]++". 805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *UnaryOperator::getOpcodeStr(Opcode Op) { 815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Op) { 825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: assert(0 && "Unknown unary operator"); 835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case PostInc: return "++"; 845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case PostDec: return "--"; 855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case PreInc: return "++"; 865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case PreDec: return "--"; 875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case AddrOf: return "&"; 885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Deref: return "*"; 895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Plus: return "+"; 905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Minus: return "-"; 915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Not: return "~"; 925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LNot: return "!"; 935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Real: return "__real"; 945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Imag: return "__imag"; 955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Extension: return "__extension__"; 9673d0d4fac161ed12926e010dcf8b448a8de6a2ecChris Lattner case OffsetOf: return "__builtin_offsetof"; 975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Postfix Operators. 1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 104b4609806e9232593ece09ce08b630836e825865cDouglas GregorCallExpr::CallExpr(StmtClass SC, Expr *fn, Expr **args, unsigned numargs, 105b4609806e9232593ece09ce08b630836e825865cDouglas Gregor QualType t, SourceLocation rparenloc) 106898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor : Expr(SC, t, 107898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), 108898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)), 109898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor NumArgs(numargs) { 110b4609806e9232593ece09ce08b630836e825865cDouglas Gregor SubExprs = new Stmt*[numargs+1]; 111b4609806e9232593ece09ce08b630836e825865cDouglas Gregor SubExprs[FN] = fn; 112b4609806e9232593ece09ce08b630836e825865cDouglas Gregor for (unsigned i = 0; i != numargs; ++i) 113b4609806e9232593ece09ce08b630836e825865cDouglas Gregor SubExprs[i+ARGS_START] = args[i]; 114b4609806e9232593ece09ce08b630836e825865cDouglas Gregor RParenLoc = rparenloc; 115b4609806e9232593ece09ce08b630836e825865cDouglas Gregor} 116e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman 1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerCallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 1185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation rparenloc) 119898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor : Expr(CallExprClass, t, 120898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), 121898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)), 122898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor NumArgs(numargs) { 1235549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek SubExprs = new Stmt*[numargs+1]; 12477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek SubExprs[FN] = fn; 1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0; i != numargs; ++i) 12677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek SubExprs[i+ARGS_START] = args[i]; 1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer RParenLoc = rparenloc; 1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 130d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// setNumArgs - This changes the number of arguments present in this call. 131d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// Any orphaned expressions are deleted by this, and any new operands are set 132d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// to null. 133d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattnervoid CallExpr::setNumArgs(unsigned NumArgs) { 134d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // No change, just return. 135d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner if (NumArgs == getNumArgs()) return; 136d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 137d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // If shrinking # arguments, just delete the extras and forgot them. 138d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner if (NumArgs < getNumArgs()) { 139d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i) 140d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner delete getArg(i); 141d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner this->NumArgs = NumArgs; 142d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner return; 143d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner } 144d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 145d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // Otherwise, we are growing the # arguments. New an bigger argument array. 1465549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek Stmt **NewSubExprs = new Stmt*[NumArgs+1]; 147d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // Copy over args. 148d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i) 149d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner NewSubExprs[i] = SubExprs[i]; 150d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // Null out new args. 151d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i) 152d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner NewSubExprs[i] = 0; 153d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 154d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner delete[] SubExprs; 155d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner SubExprs = NewSubExprs; 156d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner this->NumArgs = NumArgs; 157d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner} 158d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 159cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 160cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// not, return 0. 161cb888967400a03504c88acedd5248d6778a82f46Chris Lattnerunsigned CallExpr::isBuiltinCall() const { 162c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff // All simple function calls (e.g. func()) are implicitly cast to pointer to 163c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff // function. As a result, we try and obtain the DeclRefExpr from the 164c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff // ImplicitCastExpr. 165c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); 166c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). 167cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return 0; 168cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 169c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); 170c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff if (!DRE) 171cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return 0; 172cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 173bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); 174bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson if (!FDecl) 175cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return 0; 176cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 1774fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor if (!FDecl->getIdentifier()) 1784fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor return 0; 1794fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor 180cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return FDecl->getIdentifier()->getBuiltinID(); 181cb888967400a03504c88acedd5248d6778a82f46Chris Lattner} 182bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson 183cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "<<=". 1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *BinaryOperator::getOpcodeStr(Opcode Op) { 1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Op) { 1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: assert(0 && "Unknown binary operator"); 1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Mul: return "*"; 1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Div: return "/"; 1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Rem: return "%"; 1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Add: return "+"; 1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Sub: return "-"; 1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Shl: return "<<"; 1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Shr: return ">>"; 1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LT: return "<"; 1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case GT: return ">"; 1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LE: return "<="; 1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case GE: return ">="; 2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case EQ: return "=="; 2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case NE: return "!="; 2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case And: return "&"; 2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Xor: return "^"; 2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Or: return "|"; 2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LAnd: return "&&"; 2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LOr: return "||"; 2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Assign: return "="; 2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case MulAssign: return "*="; 2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case DivAssign: return "/="; 2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case RemAssign: return "%="; 2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case AddAssign: return "+="; 2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case SubAssign: return "-="; 2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ShlAssign: return "<<="; 2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ShrAssign: return ">>="; 2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case AndAssign: return "&="; 2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case XorAssign: return "^="; 2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case OrAssign: return "|="; 2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Comma: return ","; 2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22266b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonInitListExpr::InitListExpr(SourceLocation lbraceloc, 223418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner Expr **initExprs, unsigned numInits, 224418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner SourceLocation rbraceloc, bool hadDesignators) 225c5ae899b4bbf65488445316c63168079177db0edSteve Naroff : Expr(InitListExprClass, QualType()), 226418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), HadDesignators(hadDesignators) { 227418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner 228418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits); 22966b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson} 2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 231bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block. 2324eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff/// 2334eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroffconst FunctionType *BlockExpr::getFunctionType() const { 2344eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff return getType()->getAsBlockPointerType()-> 2354eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff getPointeeType()->getAsFunctionType(); 2364eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff} 2374eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff 23856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffSourceLocation BlockExpr::getCaretLocation() const { 23956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff return TheBlock->getCaretLocation(); 24056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff} 24156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroffconst Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); } 24256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt *BlockExpr::getBody() { return TheBlock->getBody(); } 24356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff 24456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff 2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines 2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// hasLocalSideEffect - Return true if this immediate expression has side 2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// effects, not counting any sub-expressions. 2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Expr::hasLocalSideEffect() const { 2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (getStmtClass()) { 2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ParenExprClass: 2565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect(); 2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperatorClass: { 2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const UnaryOperator *UO = cast<UnaryOperator>(this); 2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (UO->getOpcode()) { 2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: return false; 2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PostInc: 2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PostDec: 2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PreInc: 2655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PreDec: 2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; // ++/-- 2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Deref: 2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Dereferencing a volatile pointer is a side-effect. 2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return getType().isVolatileQualified(); 2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Real: 2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Imag: 2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // accessing a piece of a volatile complex is a side-effect. 2745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return UO->getSubExpr()->getType().isVolatileQualified(); 2755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Extension: 2775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return UO->getSubExpr()->hasLocalSideEffect(); 2785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 280e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner case BinaryOperatorClass: { 281e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner const BinaryOperator *BinOp = cast<BinaryOperator>(this); 282e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner // Consider comma to have side effects if the LHS and RHS both do. 283e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner if (BinOp->getOpcode() == BinaryOperator::Comma) 284e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner return BinOp->getLHS()->hasLocalSideEffect() && 285e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner BinOp->getRHS()->hasLocalSideEffect(); 286e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner 287e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner return BinOp->isAssignmentOp(); 288e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner } 289eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner case CompoundAssignOperatorClass: 2901f683e9cf2e855b32d3fb4b142aa121cd3cf1088Chris Lattner return true; 2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 292ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian case ConditionalOperatorClass: { 293ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian const ConditionalOperator *Exp = cast<ConditionalOperator>(this); 294ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian return Exp->getCond()->hasLocalSideEffect() 295ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect()) 296ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect()); 297ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian } 298ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian 2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case MemberExprClass: 3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ArraySubscriptExprClass: 3015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the base pointer or element is to a volatile pointer/field, accessing 3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // if is a side effect. 3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return getType().isVolatileQualified(); 304211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman 3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case CallExprClass: 306b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CXXOperatorCallExprClass: 3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }" 3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // should warn. 3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 310a9c01021724b9b546d282b8609cbe559734812ecChris Lattner case ObjCMessageExprClass: 311a9c01021724b9b546d282b8609cbe559734812ecChris Lattner return true; 312611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner case StmtExprClass: { 313611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // Statement exprs don't logically have side effects themselves, but are 314611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // sometimes used in macros in ways that give them a type that is unused. 315611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // For example ({ blah; foo(); }) will end up with a type if foo has a type. 316611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // however, if the result of the stmt expr is dead, we don't want to emit a 317611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // warning. 318611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); 319611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner if (!CS->body_empty()) 320611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner if (const Expr *E = dyn_cast<Expr>(CS->body_back())) 321611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner return E->hasLocalSideEffect(); 322611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner return false; 323611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner } 3246eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor case CStyleCastExprClass: 325987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case CXXFunctionalCastExprClass: 3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a cast to void, check the operand. Otherwise, the result of 3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the cast is unused. 3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getType()->isVoidType()) 3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect(); 3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 33104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner 3324be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman case ImplicitCastExprClass: 3334be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman // Check the operand, since implicit casts are inserted by Sema 3344be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect(); 3354be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman 33604421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 33704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect(); 3384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 3394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl case CXXNewExprClass: 3404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // FIXME: In theory, there might be new expressions that don't have side 3414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // effects (e.g. a placement new with an uninitialized POD). 3424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl case CXXDeleteExprClass: 3434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl return true; 3444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl } 3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 347ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor/// DeclCanBeLvalue - Determine whether the given declaration can be 348ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor/// an lvalue. This is a helper routine for isLvalue. 349ba7e210a999275695f58be03ef402758cfec3635Douglas Gregorstatic bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) { 35072c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor // C++ [temp.param]p6: 35172c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor // A non-type non-reference template-parameter is not an lvalue. 35272c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor if (const NonTypeTemplateParmDecl *NTTParm 35372c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(Decl)) 35472c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor return NTTParm->getType()->isReferenceType(); 35572c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor 35644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) || 357ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor // C++ 3.10p2: An lvalue refers to an object or function. 358ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor (Ctx.getLangOptions().CPlusPlus && 359ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl))); 360ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor} 361ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor 3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an 3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// incomplete type other than void. Nonarray expressions that can be lvalues: 3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - name, where name must be a variable 3655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - e[i] 3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - (e), where e must be an lvalue 3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - e.name, where e must be an lvalue 3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - e->name 3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - *e, the type of e cannot be a function type 3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - string-constant 3717da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension] 37208ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling/// - reference type [C++ [expr]] 3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 37428be73f74c9e241a23ea24fe5756623de6bf1084Chris LattnerExpr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { 37598cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor // first, check the type (C99 6.3.2.1). Expressions with function 37698cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor // type in C are not lvalues, but they can be lvalues in C++. 37798cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) 3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_NotObjectType; 3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 380acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff // Allow qualified void which is an incomplete type other than void (yuck). 38128be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers()) 382acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff return LV_IncompleteVoidType; 383acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff 38498cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor /// FIXME: Expressions can't have reference type, so the following 38598cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor /// isn't needed. 386cb4f9a6d0be445d84af35ba81323be6127816f9eChris Lattner if (TR->isReferenceType()) // C++ [expr] 38708ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling return LV_Valid; 38808ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling 3895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the type looks fine, now check the expression 3905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (getStmtClass()) { 3915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case StringLiteralClass: // C99 6.5.1p4 3927323a6297edad643c202594dcf3d9a174de96ca6Anders Carlsson return LV_Valid; 3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) 3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // For vectors, make sure base is an lvalue (i.e. not a function call). 3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) 39628be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx); 3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_Valid; 3981a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor case DeclRefExprClass: 3991a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor case QualifiedDeclRefExprClass: { // C99 6.5.1p2 400ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl(); 401ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor if (DeclCanBeLvalue(RefdDecl, Ctx)) 4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_Valid; 4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 4044111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner } 405dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff case BlockDeclRefExprClass: { 406dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); 4074f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff if (isa<VarDecl>(BDR->getDecl())) 408dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff return LV_Valid; 409dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff break; 410dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff } 41186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor case MemberExprClass: { 4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const MemberExpr *m = cast<MemberExpr>(this); 41386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4: 41486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor NamedDecl *Member = m->getMemberDecl(); 41586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // C++ [expr.ref]p4: 41686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // If E2 is declared to have type "reference to T", then E1.E2 41786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // is an lvalue. 41886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) 41986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor if (Value->getType()->isReferenceType()) 42086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor return LV_Valid; 42186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor 42286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // -- If E2 is a static data member [...] then E1.E2 is an lvalue. 42386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor if (isa<CXXClassVarDecl>(Member)) 42486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor return LV_Valid; 42586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor 42686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // -- If E2 is a non-static data member [...]. If E1 is an 42786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // lvalue, then E1.E2 is an lvalue. 42886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor if (isa<FieldDecl>(Member)) 42986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); 43086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor 43186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // -- If it refers to a static member function [...], then 43286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // E1.E2 is an lvalue. 43386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // -- Otherwise, if E1.E2 refers to a non-static member 43486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // function [...], then E1.E2 is not an lvalue. 43586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) 43686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor return Method->isStatic()? LV_Valid : LV_MemberFunction; 43786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor 43886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // -- If E2 is a member enumerator [...], the expression E1.E2 43986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // is not an lvalue. 44086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor if (isa<EnumConstantDecl>(Member)) 44186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor return LV_InvalidExpression; 44286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor 44386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // Not an lvalue. 44486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor return LV_InvalidExpression; 44586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor } 44686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor 44786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // C99 6.5.2.3p4 44828be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); 449fdd75663fffeb2058a7847975e50837e61200593Anton Korobeynikov } 4507da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner case UnaryOperatorClass: 4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) 4527da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner return LV_Valid; // C99 6.5.3p4 4537da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner 4547da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real || 455baf0d6678418e0dd9309438c3e50274253cfc7b2Chris Lattner cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag || 456baf0d6678418e0dd9309438c3e50274253cfc7b2Chris Lattner cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension) 45728be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU. 45874253736184c0717a0649922551bf9d8b6815651Douglas Gregor 45974253736184c0717a0649922551bf9d8b6815651Douglas Gregor if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1 46074253736184c0717a0649922551bf9d8b6815651Douglas Gregor (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc || 46174253736184c0717a0649922551bf9d8b6815651Douglas Gregor cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec)) 46274253736184c0717a0649922551bf9d8b6815651Douglas Gregor return LV_Valid; 4635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 464eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor case ImplicitCastExprClass: 465eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid 466eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor : LV_InvalidExpression; 4675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ParenExprClass: // C99 6.5.1p5 46828be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx); 469eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor case BinaryOperatorClass: 470eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor case CompoundAssignOperatorClass: { 471eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor const BinaryOperator *BinOp = cast<BinaryOperator>(this); 472337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 473337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1 474337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor BinOp->getOpcode() == BinaryOperator::Comma) 475337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor return BinOp->getRHS()->isLvalue(Ctx); 476337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 477bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor if (!BinOp->isAssignmentOp()) 478eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor return LV_InvalidExpression; 479eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor 480bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor if (Ctx.getLangOptions().CPlusPlus) 481bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor // C++ [expr.ass]p1: 482bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor // The result of an assignment operation [...] is an lvalue. 483bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor return LV_Valid; 484bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor 485bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor 486bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor // C99 6.5.16: 487bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor // An assignment expression [...] is not an lvalue. 488bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor return LV_InvalidExpression; 489eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor } 49059b5da6d853b4368b984700315adf7b37de05764Nate Begeman // FIXME: OverloadExprClass 491b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CallExprClass: 49288a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor case CXXOperatorCallExprClass: 49388a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor case CXXMemberCallExprClass: { 4949d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // C++ [expr.call]p10: 4959d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // A function call is an lvalue if and only if the result type 4969d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // is a reference. 49727c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType(); 4989d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor if (const PointerType *FnTypePtr = CalleeType->getAsPointerType()) 49988a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor CalleeType = FnTypePtr->getPointeeType(); 50088a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor if (const FunctionType *FnType = CalleeType->getAsFunctionType()) 50188a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor if (FnType->getResultType()->isReferenceType()) 50288a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor return LV_Valid; 5039d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor 5049d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor break; 5059d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor } 506e6386394677ed4f77b20e2e3d5446a3a2f628e53Steve Naroff case CompoundLiteralExprClass: // C99 6.5.2.5p5 507e6386394677ed4f77b20e2e3d5446a3a2f628e53Steve Naroff return LV_Valid; 508670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner case ChooseExprClass: 509670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner // __builtin_choose_expr is an lvalue if the selected operand is. 510670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner if (cast<ChooseExpr>(this)->isConditionTrue(Ctx)) 511670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx); 512670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner else 513670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx); 514670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner 515213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman case ExtVectorElementExprClass: 516213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements()) 517fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return LV_DuplicateVectorComponents; 518fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return LV_Valid; 519027282d1c1ac151aa7b1b3b45babc918b8ad456aSteve Naroff case ObjCIvarRefExprClass: // ObjC instance variables are lvalues. 520027282d1c1ac151aa7b1b3b45babc918b8ad456aSteve Naroff return LV_Valid; 521799a6a6850af625946bb8d88ca960bb6604e3858Steve Naroff case ObjCPropertyRefExprClass: // FIXME: check if read-only property. 522799a6a6850af625946bb8d88ca960bb6604e3858Steve Naroff return LV_Valid; 5235daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian case ObjCKVCRefExprClass: // FIXME: check if read-only property. 524670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner return LV_Valid; 525d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner case PredefinedExprClass: 526796da18402f286b897782a298ae3b20c459c102eDouglas Gregor return LV_Valid; 5279d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case VAArgExprClass: 5289d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor return LV_Valid; 52904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 53028be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx); 53124b41fa8239c63b9eb570d3e83c4a82840656a65Argyrios Kyrtzidis case CXXConditionDeclExprClass: 53224b41fa8239c63b9eb570d3e83c4a82840656a65Argyrios Kyrtzidis return LV_Valid; 5336eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor case CStyleCastExprClass: 5349d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case CXXFunctionalCastExprClass: 5359d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case CXXStaticCastExprClass: 5369d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case CXXDynamicCastExprClass: 5379d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case CXXReinterpretCastExprClass: 5389d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case CXXConstCastExprClass: 5399d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // The result of an explicit cast is an lvalue if the type we are 5409d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // casting to is a reference type. See C++ [expr.cast]p1, 5419d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2, 5429d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1. 5439d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType()) 5449d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor return LV_Valid; 5459d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor break; 546c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl case CXXTypeidExprClass: 547c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ... 548c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl return LV_Valid; 5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 5505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_InvalidExpression; 5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 5545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// does not have an incomplete type, does not have a const-qualified type, and 5575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// if it is a structure or union, does not have any member (including, 5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// recursively, any member or element of all contained aggregates or unions) 5595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// with a const-qualified type. 56028be73f74c9e241a23ea24fe5756623de6bf1084Chris LattnerExpr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const { 56128be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner isLvalueResult lvalResult = isLvalue(Ctx); 5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (lvalResult) { 564ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor case LV_Valid: 565ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor // C++ 3.10p11: Functions cannot be modified, but pointers to 566ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor // functions can be modifiable. 567ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) 568ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor return MLV_NotObjectType; 569ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor break; 570ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor 5715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LV_NotObjectType: return MLV_NotObjectType; 5725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LV_IncompleteVoidType: return MLV_IncompleteVoidType; 573fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; 574ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner case LV_InvalidExpression: 575ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner // If the top level is a C-style cast, and the subexpression is a valid 576ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner // lvalue, then this is probably a use of the old-school "cast as lvalue" 577ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner // GCC extension. We don't support it, but we want to produce good 578ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner // diagnostics when it happens so that the user knows why. 579ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this)) 580ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) 581ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner return MLV_LValueCast; 582ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner return MLV_InvalidExpression; 58386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor case LV_MemberFunction: return MLV_MemberFunction; 5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 585c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 586c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType CT = Ctx.getCanonicalType(getType()); 587c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 588c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (CT.isConstQualified()) 5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_ConstQualified; 590c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (CT->isArrayType()) 5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_ArrayType; 592c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (CT->isIncompleteType()) 5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_IncompleteType; 5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 595c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const RecordType *r = CT->getAsRecordType()) { 5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (r->hasConstFields()) 5975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_ConstQualified; 5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 5994f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // The following is illegal: 6004f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // void takeclosure(void (^C)(void)); 6014f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // void func() { int x = 1; takeclosure(^{ x = 7 }); } 6024f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // 6034f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff if (getStmtClass() == BlockDeclRefExprClass) { 6044f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); 6054f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl())) 6064f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff return MLV_NotBlockQualified; 6074f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff } 608d1fa6449e9dbdd667466e9e1e971aa17c9793e8aFariborz Jahanian 609ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian // Assigning to an 'implicit' property? 6106669db9d80d77d10f101aa9f8e488bbd2d98f76cFariborz Jahanian else if (getStmtClass() == ObjCKVCRefExprClass) { 611ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this); 612ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian if (KVCExpr->getSetterMethod() == 0) 613ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian return MLV_NoSetterProperty; 614ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian } 6155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_Valid; 6165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6182e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek/// hasGlobalStorage - Return true if this expression has static storage 6194cc627111453b75519d5130b57e06256da7b00e8Chris Lattner/// duration. This means that the address of this expression is a link-time 6204cc627111453b75519d5130b57e06256da7b00e8Chris Lattner/// constant. 6212e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenekbool Expr::hasGlobalStorage() const { 6221d09ecca892185ba067e47ba879f424de59950efChris Lattner switch (getStmtClass()) { 6231d09ecca892185ba067e47ba879f424de59950efChris Lattner default: 6241d09ecca892185ba067e47ba879f424de59950efChris Lattner return false; 6254cc627111453b75519d5130b57e06256da7b00e8Chris Lattner case ParenExprClass: 6262e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage(); 6274cc627111453b75519d5130b57e06256da7b00e8Chris Lattner case ImplicitCastExprClass: 6282e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage(); 629e9b12198c4cc7f5687960100351b4af006c14469Steve Naroff case CompoundLiteralExprClass: 630e9b12198c4cc7f5687960100351b4af006c14469Steve Naroff return cast<CompoundLiteralExpr>(this)->isFileScope(); 6311a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor case DeclRefExprClass: 6321a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor case QualifiedDeclRefExprClass: { 6331d09ecca892185ba067e47ba879f424de59950efChris Lattner const Decl *D = cast<DeclRefExpr>(this)->getDecl(); 6341d09ecca892185ba067e47ba879f424de59950efChris Lattner if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 6352e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return VD->hasGlobalStorage(); 63663f067f5a75ae97b53dc7a6a6530e2c72c8bb7f8Seo Sanghyeon if (isa<FunctionDecl>(D)) 63763f067f5a75ae97b53dc7a6a6530e2c72c8bb7f8Seo Sanghyeon return true; 6381d09ecca892185ba067e47ba879f424de59950efChris Lattner return false; 6391d09ecca892185ba067e47ba879f424de59950efChris Lattner } 640fb7080663a0f0abd687a6f58ac917d9a76318b73Chris Lattner case MemberExprClass: { 6411d09ecca892185ba067e47ba879f424de59950efChris Lattner const MemberExpr *M = cast<MemberExpr>(this); 6422e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return !M->isArrow() && M->getBase()->hasGlobalStorage(); 643fb7080663a0f0abd687a6f58ac917d9a76318b73Chris Lattner } 6444cc627111453b75519d5130b57e06256da7b00e8Chris Lattner case ArraySubscriptExprClass: 6452e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage(); 646d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner case PredefinedExprClass: 647fa28b30d5a1e93e5263be33e343532b900d2c643Chris Lattner return true; 64804421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 64904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage(); 6501d09ecca892185ba067e47ba879f424de59950efChris Lattner } 6511d09ecca892185ba067e47ba879f424de59950efChris Lattner} 6521d09ecca892185ba067e47ba879f424de59950efChris Lattner 6534e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() { 6544e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek Expr* E = this; 6554e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek while (ParenExpr* P = dyn_cast<ParenExpr>(E)) 6564e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek E = P->getSubExpr(); 6574e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek 6584e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek return E; 6594e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek} 6604e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek 66156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 66256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand. 66356f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() { 66456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner Expr *E = this; 66556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner while (true) { 66656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner if (ParenExpr *P = dyn_cast<ParenExpr>(E)) 66756f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner E = P->getSubExpr(); 66856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner else if (CastExpr *P = dyn_cast<CastExpr>(E)) 66956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner E = P->getSubExpr(); 67056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner else 67156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner return E; 67256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner } 67356f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner} 67456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner 675898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyTypeDependentArguments - Determines if any of the expressions 676898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is type-dependent. 677898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) { 678898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor for (unsigned I = 0; I < NumExprs; ++I) 679898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor if (Exprs[I]->isTypeDependent()) 680898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor return true; 681898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 682898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor return false; 683898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor} 684898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 685898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyValueDependentArguments - Determines if any of the expressions 686898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is value-dependent. 687898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) { 688898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor for (unsigned I = 0; I < NumExprs; ++I) 689898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor if (Exprs[I]->isValueDependent()) 690898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor return true; 691898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 692898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor return false; 693898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor} 694898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 695c9e8f606787b0bc0c3b08e566b87cc1751694168Eli Friedmanbool Expr::isConstantInitializer(ASTContext &Ctx) const { 696c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman // This function is attempting whether an expression is an initializer 697c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman // which can be evaluated at compile-time. isEvaluatable handles most 698c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman // of the cases, but it can't deal with some initializer-specific 699c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman // expressions, and it can't deal with aggregates; we deal with those here, 700c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman // and fall back to isEvaluatable for the other cases. 701c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman 702e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson switch (getStmtClass()) { 703c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman default: break; 704e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson case StringLiteralClass: 705e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson return true; 70659b5da6d853b4368b984700315adf7b37de05764Nate Begeman case CompoundLiteralExprClass: { 70759b5da6d853b4368b984700315adf7b37de05764Nate Begeman const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer(); 708c9e8f606787b0bc0c3b08e566b87cc1751694168Eli Friedman return Exp->isConstantInitializer(Ctx); 70959b5da6d853b4368b984700315adf7b37de05764Nate Begeman } 710e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson case InitListExprClass: { 711e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson const InitListExpr *Exp = cast<InitListExpr>(this); 712e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson unsigned numInits = Exp->getNumInits(); 713e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson for (unsigned i = 0; i < numInits; i++) { 714c9e8f606787b0bc0c3b08e566b87cc1751694168Eli Friedman if (!Exp->getInit(i)->isConstantInitializer(Ctx)) 715e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson return false; 716e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson } 717c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman return true; 718e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson } 719c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman case ParenExprClass: { 720c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); 721c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman } 722c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman case UnaryOperatorClass: { 723c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman const UnaryOperator* Exp = cast<UnaryOperator>(this); 724c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman if (Exp->getOpcode() == UnaryOperator::Extension) 725c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman return Exp->getSubExpr()->isConstantInitializer(Ctx); 726c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman break; 727c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman } 728c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman case CStyleCastExprClass: 729c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman // Handle casts with a destination that's a struct or union; this 730c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman // deals with both the gcc no-op struct cast extension and the 731c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman // cast-to-union extension. 732c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman if (getType()->isRecordType()) 733c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); 734c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman break; 73532a311e276cd4bcafddd38db679aff9804e66bd4Eli Friedman case DesignatedInitExprClass: 7364e716e07d90a18ac83dd94d157ec676530bc78f9Sebastian Redl return cast<DesignatedInitExpr>(this)-> 7374e716e07d90a18ac83dd94d157ec676530bc78f9Sebastian Redl getInit()->isConstantInitializer(Ctx); 738e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson } 739e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson 740c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman return isEvaluatable(Ctx); 74138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff} 74238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIntegerConstantExpr - this recursive routine will test if an expression is 7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an integer constant expression. Note: With the introduction of VLA's in 7455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// C99 the result of the sizeof operator is no longer always a constant 7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expression. The generalization of the wording to include any subexpression 7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions 7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, 7495f6b632391e24b08ead3e62ba2e2765e770382edNuno Lopes/// "0 || f()" can be treated as a constant expression. In C90 this expression, 7505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// occurring in a context requiring a constant, would have been a constraint 7515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// violation. FIXME: This routine currently implements C90 semantics. 7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// To properly implement C99 semantics this routine will need to evaluate 7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expressions involving operators previously mentioned. 7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, 7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// comma, etc 7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not 759ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner/// permit this. This includes things like (int)1e1000 760ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// 761ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof 762ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer 763ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// cast+dereference. 764590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattnerbool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 765590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner SourceLocation *Loc, bool isEvaluated) const { 766a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman // Pretest for integral type; some parts of the code crash for types that 767a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman // can't be sized. 768a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman if (!getType()->isIntegralType()) { 769a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman if (Loc) *Loc = getLocStart(); 770a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman return false; 771a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman } 7725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (getStmtClass()) { 7735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 7755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 7765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ParenExprClass: 7775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return cast<ParenExpr>(this)->getSubExpr()-> 778590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); 7795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case IntegerLiteralClass: 7805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = cast<IntegerLiteral>(this)->getValue(); 7815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 7822eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner case CharacterLiteralClass: { 7832eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner const CharacterLiteral *CL = cast<CharacterLiteral>(this); 78498be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 7852eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner Result = CL->getValue(); 786f0fbcb356bc4eb5f6467dc4fc4bc447e9a630102Chris Lattner Result.setIsUnsigned(!getType()->isSignedIntegerType()); 7875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 7882eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner } 789b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson case CXXBoolLiteralExprClass: { 790b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this); 791b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 792b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson Result = BL->getValue(); 793b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson Result.setIsUnsigned(!getType()->isSignedIntegerType()); 794b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson break; 795b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson } 7967267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis case CXXZeroInitValueExprClass: 7977267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis Result.clear(); 7987267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis break; 7997b658aa315784fa489a32daeacfe400512ce468dSteve Naroff case TypesCompatibleExprClass: { 8007b658aa315784fa489a32daeacfe400512ce468dSteve Naroff const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this); 80198be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 802ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar // Per gcc docs "this built-in function ignores top level 803ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar // qualifiers". We need to use the canonical version to properly 804ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar // be able to strip CRV qualifiers from the type. 805ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar QualType T0 = Ctx.getCanonicalType(TCE->getArgType1()); 806ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar QualType T1 = Ctx.getCanonicalType(TCE->getArgType2()); 807ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar Result = Ctx.typesAreCompatible(T0.getUnqualifiedType(), 808ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar T1.getUnqualifiedType()); 809389cecc83f33e93c8ba6bf2e8269b8690404416fSteve Naroff break; 8107b658aa315784fa489a32daeacfe400512ce468dSteve Naroff } 811b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CallExprClass: 812b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CXXOperatorCallExprClass: { 81313b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff const CallExpr *CE = cast<CallExpr>(this); 81498be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 815a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner 816a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // If this is a call to a builtin function, constant fold it otherwise 817a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // reject it. 818a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner if (CE->isBuiltinCall()) { 8191c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson EvalResult EvalResult; 8201c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson if (CE->Evaluate(EvalResult, Ctx)) { 8211c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson assert(!EvalResult.HasSideEffects && 8221c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson "Foldable builtin call should not have side effects!"); 8231c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson Result = EvalResult.Val.getInt(); 824a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner break; // It is a constant, expand it. 825a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner } 826a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner } 827a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner 82813b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff if (Loc) *Loc = getLocStart(); 82913b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff return false; 83013b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff } 8315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case DeclRefExprClass: 8321a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor case QualifiedDeclRefExprClass: 8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (const EnumConstantDecl *D = 8345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { 8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = D->getInitVal(); 8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 8395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperatorClass: { 8415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const UnaryOperator *Exp = cast<UnaryOperator>(this); 8425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8430518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // Get the operand value. If this is offsetof, do not evalute the 8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // operand. This affects C99 6.6p3. 8450518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()-> 8460518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 8475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Exp->getOpcode()) { 8505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. 8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // See C99 6.6p3. 8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = Exp->getOperatorLoc(); 8545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Extension: 85676e773a443be9f006610f46529e07d4c8d857680Chris Lattner return true; // FIXME: this is wrong. 8575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::LNot: { 858bf7553889594886213e05cd3f94a6d6f7747bf7bChris Lattner bool Val = Result == 0; 85998be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = Val; 8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Plus: 8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Minus: 8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = -Result; 8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Not: 8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = ~Result; 8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8715a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson case UnaryOperator::OffsetOf: 872aa1f9f1d50adf294674b74510b62e863b68572bcDaniel Dunbar Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 8735a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson Result = Exp->evaluateOffsetOf(Ctx); 8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8770518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl case SizeOfAlignOfExprClass: { 8780518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this); 879a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 880a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // Return the result in the right width. 88198be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 882a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 8830518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl QualType ArgTy = Exp->getTypeOfArgument(); 884a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // sizeof(void) and __alignof__(void) = 1 as a gcc extension. 8850518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (ArgTy->isVoidType()) { 886a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner Result = 1; 887a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner break; 888a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner } 889a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 890a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // alignof always evaluates to a constant, sizeof does if arg is not VLA. 8910518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) { 89265383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner if (Loc) *Loc = Exp->getOperatorLoc(); 8935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 89465383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner } 8955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 89676e773a443be9f006610f46529e07d4c8d857680Chris Lattner // Get information about the size or align. 8970518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (ArgTy->isFunctionType()) { 898efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner // GCC extension: sizeof(function) = 1. 899efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner Result = Exp->isSizeOf() ? 1 : 4; 9006a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson } else { 90198be4943e8dc4f3905629a7102668960873cf863Chris Lattner unsigned CharSize = Ctx.Target.getCharWidth(); 9026a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson if (Exp->isSizeOf()) 9030518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl Result = Ctx.getTypeSize(ArgTy) / CharSize; 9046a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson else 9050518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl Result = Ctx.getTypeAlign(ArgTy) / CharSize; 906060e470e53b73167114284eb0b8c3e25bb3dad99Ted Kremenek } 9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperatorClass: { 9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const BinaryOperator *Exp = cast<BinaryOperator>(this); 911e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar llvm::APSInt LHS, RHS; 912e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar 913e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar // Initialize result to have correct signedness and width. 914e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())), 915b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman !getType()->isSignedIntegerType()); 916b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman 9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The LHS of a constant expr is always evaluated and needed. 918e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated)) 9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The short-circuiting &&/|| operators don't necessarily evaluate their 9225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // RHS. Make sure to pass isEvaluated down correctly. 9235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Exp->isLogicalOp()) { 9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool RHSEval; 9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Exp->getOpcode() == BinaryOperator::LAnd) 926e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar RHSEval = LHS != 0; 9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer else { 9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); 929e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar RHSEval = LHS == 0; 9305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 932590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, 9335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer isEvaluated & RHSEval)) 9345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 936590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) 9375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Exp->getOpcode()) { 9415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 9425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 9435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Mul: 945e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS * RHS; 9465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Div: 9485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (RHS == 0) { 9495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isEvaluated) break; 9505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 9515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 953e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS / RHS; 9545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Rem: 9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (RHS == 0) { 9575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isEvaluated) break; 9585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 9595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 961e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS % RHS; 9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 963e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Add: Result = LHS + RHS; break; 964e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Sub: Result = LHS - RHS; break; 9655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Shl: 966e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS << 967e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); 968e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar break; 9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Shr: 970e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS >> 971e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); 9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 973e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::LT: Result = LHS < RHS; break; 974e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::GT: Result = LHS > RHS; break; 975e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::LE: Result = LHS <= RHS; break; 976e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::GE: Result = LHS >= RHS; break; 977e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::EQ: Result = LHS == RHS; break; 978e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::NE: Result = LHS != RHS; break; 979e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::And: Result = LHS & RHS; break; 980e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Xor: Result = LHS ^ RHS; break; 981e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Or: Result = LHS | RHS; break; 9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::LAnd: 983e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS != 0 && RHS != 0; 9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::LOr: 986e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS != 0 || RHS != 0; 9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 988b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman 989b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman case BinaryOperator::Comma: 990b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman // C99 6.6p3: "shall not contain assignment, ..., or comma operators, 991b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman // *except* when they are contained within a subexpression that is not 992b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman // evaluated". Note that Assignment can never happen due to constraints 993b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman // on the LHS subexpr, so we don't need to check it here. 994b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman if (isEvaluated) { 995b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman if (Loc) *Loc = getLocStart(); 996b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman return false; 997b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman } 998b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman 999b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman // The result of the constant expr is the RHS. 1000b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman Result = RHS; 1001b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman return true; 10025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); 10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 10065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 100726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner case ImplicitCastExprClass: 10086eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor case CStyleCastExprClass: 1009987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case CXXFunctionalCastExprClass: { 10100835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr(); 10110835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis SourceLocation CastLoc = getLocStart(); 101226dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner 10135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.6p6: shall only convert arithmetic types to integer types. 101426dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (!SubExpr->getType()->isArithmeticType() || 101526dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner !getType()->isIntegerType()) { 101626dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (Loc) *Loc = SubExpr->getLocStart(); 10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1019987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner 102098be4943e8dc4f3905629a7102668960873cf863Chris Lattner uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType())); 1021987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner 10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Handle simple integer->integer casts. 102326dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (SubExpr->getType()->isIntegerType()) { 102426dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 102626dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner 102726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner // Figure out if this is a truncate, extend or noop cast. 102826dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner // If the input is signed, do a sign extend, noop, or truncate. 1029c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner if (getType()->isBooleanType()) { 1030c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner // Conversion to bool compares against zero. 1031c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result = Result != 0; 1032c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result.zextOrTrunc(DestWidth); 1033c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner } else if (SubExpr->getType()->isSignedIntegerType()) 103426dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner Result.sextOrTrunc(DestWidth); 103526dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner else // If the input is unsigned, do a zero extend, noop, or truncate. 103626dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner Result.zextOrTrunc(DestWidth); 10375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Allow floating constants that are the immediate operands of casts or that 10415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // are parenthesized. 104226dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner const Expr *Operand = SubExpr; 10435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) 10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Operand = PE->getSubExpr(); 1045987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner 1046987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner // If this isn't a floating literal, we can't handle it. 1047987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand); 1048987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner if (!FL) { 1049987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner if (Loc) *Loc = Operand->getLocStart(); 1050987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner return false; 1051987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner } 1052c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner 1053c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner // If the destination is boolean, compare against zero. 1054c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner if (getType()->isBooleanType()) { 1055c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result = !FL->getValue().isZero(); 1056c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result.zextOrTrunc(DestWidth); 1057c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner break; 1058c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner } 10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1060987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner // Determine whether we are converting to unsigned or signed. 1061987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner bool DestSigned = getType()->isSignedIntegerType(); 1062ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner 1063ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can 1064ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner // be called multiple times per AST. 1065ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen uint64_t Space[4]; 1066ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen bool ignored; 1067ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned, 1068ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen llvm::APFloat::rmTowardZero, 1069ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen &ignored); 1070987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner Result = llvm::APInt(DestWidth, 4, Space); 1071987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner break; 10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ConditionalOperatorClass: { 10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const ConditionalOperator *Exp = cast<ConditionalOperator>(this); 10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 107628daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner const Expr *Cond = Exp->getCond(); 107728daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner 107828daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const Expr *TrueExp = Exp->getLHS(); 10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const Expr *FalseExp = Exp->getRHS(); 10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Result == 0) std::swap(TrueExp, FalseExp); 10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 108528daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner // If the condition (ignoring parens) is a __builtin_constant_p call, 108628daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner // then only the true side is actually considered in an integer constant 108742b83dde7c700b34f9435ad746984169888ae705Chris Lattner // expression, and it is fully evaluated. This is an important GNU 108842b83dde7c700b34f9435ad746984169888ae705Chris Lattner // extension. See GCC PR38377 for discussion. 108928daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts())) 109042b83dde7c700b34f9435ad746984169888ae705Chris Lattner if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) { 109142b83dde7c700b34f9435ad746984169888ae705Chris Lattner EvalResult EVResult; 109242b83dde7c700b34f9435ad746984169888ae705Chris Lattner if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects) 109342b83dde7c700b34f9435ad746984169888ae705Chris Lattner return false; 109442b83dde7c700b34f9435ad746984169888ae705Chris Lattner assert(EVResult.Val.isInt() && "FP conditional expr not expected"); 109542b83dde7c700b34f9435ad746984169888ae705Chris Lattner Result = EVResult.Val.getInt(); 109642b83dde7c700b34f9435ad746984169888ae705Chris Lattner if (Loc) *Loc = EVResult.DiagLoc; 109742b83dde7c700b34f9435ad746984169888ae705Chris Lattner return true; 109842b83dde7c700b34f9435ad746984169888ae705Chris Lattner } 109928daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner 11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Evaluate the false one first, discard the result. 11013907323dd6665c0c4e383435cb145233f4533406Anders Carlsson if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false)) 11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Evalute the true one, capture the result. 11043907323dd6665c0c4e383435cb145233f4533406Anders Carlsson if (TrueExp && 11053907323dd6665c0c4e383435cb145233f4533406Anders Carlsson !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 110904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 111004421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this) 111104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); 111264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 111364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl case UnaryTypeTraitExprClass: 111464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl Result = cast<UnaryTypeTraitExpr>(this)->Evaluate(); 111564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl return true; 11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Cases that are valid constant exprs fall through to here. 11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.setIsUnsigned(getType()->isUnsignedIntegerType()); 11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an 11245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// integer constant expression with the value zero, or if this is one that is 11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// cast to void*. 1126efa9b3877ef298bcb792600ac33521827e1f7fafAnders Carlssonbool Expr::isNullPointerConstant(ASTContext &Ctx) const 1127efa9b3877ef298bcb792600ac33521827e1f7fafAnders Carlsson{ 11280777972d38a3125efed962b045704c30ae6965cfSebastian Redl // Strip off a cast to void*, if it exists. Except in C++. 11290835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { 11306215dee86c0e715b9f2b0d401ab2a5fcf629f1afSebastian Redl if (!Ctx.getLangOptions().CPlusPlus) { 11310777972d38a3125efed962b045704c30ae6965cfSebastian Redl // Check that it is a cast to void*. 11320777972d38a3125efed962b045704c30ae6965cfSebastian Redl if (const PointerType *PT = CE->getType()->getAsPointerType()) { 11330777972d38a3125efed962b045704c30ae6965cfSebastian Redl QualType Pointee = PT->getPointeeType(); 11340777972d38a3125efed962b045704c30ae6965cfSebastian Redl if (Pointee.getCVRQualifiers() == 0 && 11350777972d38a3125efed962b045704c30ae6965cfSebastian Redl Pointee->isVoidType() && // to void* 11360777972d38a3125efed962b045704c30ae6965cfSebastian Redl CE->getSubExpr()->getType()->isIntegerType()) // from int. 1137d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson return CE->getSubExpr()->isNullPointerConstant(Ctx); 11380777972d38a3125efed962b045704c30ae6965cfSebastian Redl } 11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1140aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { 1141aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // Ignore the ImplicitCastExpr type entirely. 1142d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson return ICE->getSubExpr()->isNullPointerConstant(Ctx); 1143aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { 1144aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // Accept ((void*)0) as a null pointer constant, as many other 1145aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // implementations do. 1146d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson return PE->getSubExpr()->isNullPointerConstant(Ctx); 11478123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner } else if (const CXXDefaultArgExpr *DefaultArg 11488123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner = dyn_cast<CXXDefaultArgExpr>(this)) { 114904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner // See through default argument expressions 1150d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson return DefaultArg->getExpr()->isNullPointerConstant(Ctx); 11512d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor } else if (isa<GNUNullExpr>(this)) { 11522d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor // The GNU __null extension is always a null pointer constant. 11532d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor return true; 1154aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff } 11552d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor 1156aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // This expression must be an integer type. 1157aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff if (!getType()->isIntegerType()) 1158aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff return false; 1159aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff 11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have an integer constant expression, we need to *evaluate* it and 11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // test for the value 0. 1162d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson // FIXME: We should probably return false if we're compiling in strict mode 1163d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson // and Diag is not null (this indicates that the value was foldable but not 1164d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson // an ICE. 1165d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson EvalResult Result; 1166efa9b3877ef298bcb792600ac33521827e1f7fafAnders Carlsson return Evaluate(Result, Ctx) && !Result.HasSideEffects && 1167d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson Result.Val.isInt() && Result.Val.getInt() == 0; 11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 116931a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff 117027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// isBitField - Return true if this expression is a bit-field. 117127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorbool Expr::isBitField() { 117227c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor Expr *E = this->IgnoreParenCasts(); 117327c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) 117486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) 117586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor return Field->isBitField(); 117627c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor return false; 117727c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor} 117827c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor 1179213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const { 11808a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman if (const VectorType *VT = getType()->getAsVectorType()) 11818a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman return VT->getNumElements(); 11828a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman return 1; 11834d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner} 11844d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner 11858a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated. 1186213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const { 1187fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff const char *compStr = Accessor.getName(); 11887e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner unsigned length = Accessor.getLength(); 1189190d6a25393995b42e32086949a68285ee423fb9Nate Begeman 1190190d6a25393995b42e32086949a68285ee423fb9Nate Begeman // Halving swizzles do not contain duplicate elements. 1191190d6a25393995b42e32086949a68285ee423fb9Nate Begeman if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || 1192190d6a25393995b42e32086949a68285ee423fb9Nate Begeman !strcmp(compStr, "even") || !strcmp(compStr, "odd")) 1193190d6a25393995b42e32086949a68285ee423fb9Nate Begeman return false; 1194190d6a25393995b42e32086949a68285ee423fb9Nate Begeman 1195190d6a25393995b42e32086949a68285ee423fb9Nate Begeman // Advance past s-char prefix on hex swizzles. 1196190d6a25393995b42e32086949a68285ee423fb9Nate Begeman if (*compStr == 's') { 1197190d6a25393995b42e32086949a68285ee423fb9Nate Begeman compStr++; 1198190d6a25393995b42e32086949a68285ee423fb9Nate Begeman length--; 1199190d6a25393995b42e32086949a68285ee423fb9Nate Begeman } 1200fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff 12017e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner for (unsigned i = 0; i != length-1; i++) { 1202fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff const char *s = compStr+i; 1203fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff for (const char c = *s++; *s; s++) 1204fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff if (c == *s) 1205fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return true; 1206fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff } 1207fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return false; 1208fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff} 1209b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner 12108a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. 12113b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess( 12123b8d116703db8018f855cbb4733ace426422623bNate Begeman llvm::SmallVectorImpl<unsigned> &Elts) const { 12137e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner const char *compStr = Accessor.getName(); 1214353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman if (*compStr == 's') 1215353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman compStr++; 1216353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman 1217353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman bool isHi = !strcmp(compStr, "hi"); 1218353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman bool isLo = !strcmp(compStr, "lo"); 1219353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman bool isEven = !strcmp(compStr, "even"); 1220353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman bool isOdd = !strcmp(compStr, "odd"); 1221353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman 12228a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman for (unsigned i = 0, e = getNumElements(); i != e; ++i) { 12238a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman uint64_t Index; 12248a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman 12258a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman if (isHi) 12268a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = e + i; 12278a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else if (isLo) 12288a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = i; 12298a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else if (isEven) 12308a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = 2 * i; 12318a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else if (isOdd) 12328a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = 2 * i + 1; 12338a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else 12348a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = ExtVectorType::getAccessorIdx(compStr[i]); 1235b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner 12363b8d116703db8018f855cbb4733ace426422623bNate Begeman Elts.push_back(Index); 1237b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner } 12388a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman} 12398a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman 124068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// constructor for instance messages. 1241bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve NaroffObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, 1242a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek QualType retType, ObjCMethodDecl *mproto, 1243db611d556f71f98b66b69514d45958d76e5727abSteve Naroff SourceLocation LBrac, SourceLocation RBrac, 124449f109c786f99eb7468dac3976db083a65493444Steve Naroff Expr **ArgExprs, unsigned nargs) 1245db611d556f71f98b66b69514d45958d76e5727abSteve Naroff : Expr(ObjCMessageExprClass, retType), SelName(selInfo), 1246ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted Kremenek MethodProto(mproto) { 124749f109c786f99eb7468dac3976db083a65493444Steve Naroff NumArgs = nargs; 12485549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek SubExprs = new Stmt*[NumArgs+1]; 124968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff SubExprs[RECEIVER] = receiver; 125049f109c786f99eb7468dac3976db083a65493444Steve Naroff if (NumArgs) { 125149f109c786f99eb7468dac3976db083a65493444Steve Naroff for (unsigned i = 0; i != NumArgs; ++i) 125268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 125368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff } 1254563477da25f824e37c535131695dc4dc9b68c465Steve Naroff LBracloc = LBrac; 1255563477da25f824e37c535131695dc4dc9b68c465Steve Naroff RBracloc = RBrac; 1256563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1257563477da25f824e37c535131695dc4dc9b68c465Steve Naroff 125868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// constructor for class messages. 125968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// FIXME: clsName should be typed to ObjCInterfaceType 1260bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve NaroffObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, 1261a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek QualType retType, ObjCMethodDecl *mproto, 1262db611d556f71f98b66b69514d45958d76e5727abSteve Naroff SourceLocation LBrac, SourceLocation RBrac, 126349f109c786f99eb7468dac3976db083a65493444Steve Naroff Expr **ArgExprs, unsigned nargs) 1264db611d556f71f98b66b69514d45958d76e5727abSteve Naroff : Expr(ObjCMessageExprClass, retType), SelName(selInfo), 1265ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted Kremenek MethodProto(mproto) { 126649f109c786f99eb7468dac3976db083a65493444Steve Naroff NumArgs = nargs; 12675549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek SubExprs = new Stmt*[NumArgs+1]; 12684df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown); 126949f109c786f99eb7468dac3976db083a65493444Steve Naroff if (NumArgs) { 127049f109c786f99eb7468dac3976db083a65493444Steve Naroff for (unsigned i = 0; i != NumArgs; ++i) 127168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 127268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff } 1273563477da25f824e37c535131695dc4dc9b68c465Steve Naroff LBracloc = LBrac; 1274563477da25f824e37c535131695dc4dc9b68c465Steve Naroff RBracloc = RBrac; 1275563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1276563477da25f824e37c535131695dc4dc9b68c465Steve Naroff 12774df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek// constructor for class messages. 12784df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, 12794df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek QualType retType, ObjCMethodDecl *mproto, 12804df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SourceLocation LBrac, SourceLocation RBrac, 12814df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek Expr **ArgExprs, unsigned nargs) 12824df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek: Expr(ObjCMessageExprClass, retType), SelName(selInfo), 12834df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekMethodProto(mproto) { 12844df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek NumArgs = nargs; 12854df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs = new Stmt*[NumArgs+1]; 12864df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown); 12874df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek if (NumArgs) { 12884df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek for (unsigned i = 0; i != NumArgs; ++i) 12894df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 12904df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek } 12914df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek LBracloc = LBrac; 12924df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek RBracloc = RBrac; 12934df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek} 12944df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek 12954df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const { 12964df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; 12974df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek switch (x & Flags) { 12984df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek default: 12994df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek assert(false && "Invalid ObjCMessageExpr."); 13004df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek case IsInstMeth: 13014df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek return ClassInfo(0, 0); 13024df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek case IsClsMethDeclUnknown: 13034df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek return ClassInfo(0, (IdentifierInfo*) (x & ~Flags)); 13044df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek case IsClsMethDeclKnown: { 13054df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags); 13064df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek return ClassInfo(D, D->getIdentifier()); 13074df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek } 13084df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek } 13094df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek} 13104df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek 131127437caadea35f84d550cd29f024fcf3ea240eecChris Lattnerbool ChooseExpr::isConditionTrue(ASTContext &C) const { 131232442bbc98bafa512fa42d46fedf60ed7d79f574Daniel Dunbar return getCond()->getIntegerConstantExprValue(C) != 0; 131327437caadea35f84d550cd29f024fcf3ea240eecChris Lattner} 131427437caadea35f84d550cd29f024fcf3ea240eecChris Lattner 1315670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattnerstatic int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) { 13165a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 13175a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson QualType Ty = ME->getBase()->getType(); 13185a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 13195a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson RecordDecl *RD = Ty->getAsRecordType()->getDecl(); 132098be4943e8dc4f3905629a7102668960873cf863Chris Lattner const ASTRecordLayout &RL = C.getASTRecordLayout(RD); 132186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { 132286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // FIXME: This is linear time. And the fact that we're indexing 132386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // into the layout by position in the record means that we're 132486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // either stuck numbering the fields in the AST or we have to keep 132586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor // the linear search (yuck and yuck). 132686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor unsigned i = 0; 132786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor for (RecordDecl::field_iterator Field = RD->field_begin(), 132886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor FieldEnd = RD->field_end(); 132986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor Field != FieldEnd; (void)++Field, ++i) { 133086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor if (*Field == FD) 133186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor break; 133286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor } 133386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor 133486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase()); 13355a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson } 13365a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { 13375a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson const Expr *Base = ASE->getBase(); 13385a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 133998be4943e8dc4f3905629a7102668960873cf863Chris Lattner int64_t size = C.getTypeSize(ASE->getType()); 134032442bbc98bafa512fa42d46fedf60ed7d79f574Daniel Dunbar size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue(); 13415a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 13425a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return size + evaluateOffsetOf(C, Base); 13435a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson } else if (isa<CompoundLiteralExpr>(E)) 13445a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return 0; 13455a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 13465a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson assert(0 && "Unknown offsetof subexpression!"); 13475a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return 0; 13485a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson} 13495a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 13505a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlssonint64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const 13515a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson{ 13525a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson assert(Opc == OffsetOf && "Unary operator not offsetof!"); 13535a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 135498be4943e8dc4f3905629a7102668960873cf863Chris Lattner unsigned CharSize = C.Target.getCharWidth(); 13555549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize; 13565a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson} 13575a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 13580518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redlvoid SizeOfAlignOfExpr::Destroy(ASTContext& C) { 13590518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // Override default behavior of traversing children. If this has a type 13600518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // operand and the type is a variable-length array, the child iteration 13610518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // will iterate over the size expression. However, this expression belongs 13620518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // to the type, not to this, so we don't want to delete it. 13630518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // We still want to delete this expression. 13640518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // FIXME: Same as in Stmt::Destroy - will be eventually in ASTContext's 13650518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // pool allocator. 13660518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (isArgumentType()) 13670518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl delete this; 13680518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl else 13690518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl Expr::Destroy(C); 13709048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar} 13719048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar 137277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===// 137305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor// DesignatedInitExpr 137405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===// 137505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 137605c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorIdentifierInfo *DesignatedInitExpr::Designator::getFieldName() { 137705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor assert(Kind == FieldDesignator && "Only valid on a field designator"); 137805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor if (Field.NameOrField & 0x01) 137905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); 138005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor else 138105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return getField()->getIdentifier(); 138205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor} 138305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 138405c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr * 138505c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr::Create(ASTContext &C, Designator *Designators, 138605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor unsigned NumDesignators, 138705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Expr **IndexExprs, unsigned NumIndexExprs, 138805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor SourceLocation ColonOrEqualLoc, 138905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor bool UsesColonSyntax, Expr *Init) { 1390c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + 1391c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff sizeof(Designator) * NumDesignators + 1392c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff sizeof(Stmt *) * (NumIndexExprs + 1), 8); 139305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor DesignatedInitExpr *DIE 139405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, 139505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor ColonOrEqualLoc, UsesColonSyntax, 139605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor NumIndexExprs + 1); 139705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 139805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor // Fill in the designators 139905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor unsigned ExpectedNumSubExprs = 0; 140005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor designators_iterator Desig = DIE->designators_begin(); 140105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) { 140205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor new (static_cast<void*>(Desig)) Designator(Designators[Idx]); 140305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor if (Designators[Idx].isArrayDesignator()) 140405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor ++ExpectedNumSubExprs; 140505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor else if (Designators[Idx].isArrayRangeDesignator()) 140605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor ExpectedNumSubExprs += 2; 140705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor } 140805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!"); 140905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 141005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor // Fill in the subexpressions, including the initializer expression. 141105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor child_iterator Child = DIE->child_begin(); 141205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor *Child++ = Init; 141305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child) 141405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor *Child = IndexExprs[Idx]; 141505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 141605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return DIE; 141705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor} 141805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 141905c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorSourceRange DesignatedInitExpr::getSourceRange() const { 142005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor SourceLocation StartLoc; 142105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Designator &First = *const_cast<DesignatedInitExpr*>(this)->designators_begin(); 142205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor if (First.isFieldDesignator()) { 142305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor if (UsesColonSyntax) 142405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); 142505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor else 142605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); 142705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor } else 142805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor StartLoc = SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); 142905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return SourceRange(StartLoc, getInit()->getSourceRange().getEnd()); 143005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor} 143105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 143205c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_begin() { 143305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor char* Ptr = static_cast<char*>(static_cast<void *>(this)); 143405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Ptr += sizeof(DesignatedInitExpr); 143505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return static_cast<Designator*>(static_cast<void*>(Ptr)); 143605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor} 143705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 143805c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() { 143905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return designators_begin() + NumDesignators; 144005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor} 144105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 144205c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayIndex(const Designator& D) { 144305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); 144405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor char* Ptr = static_cast<char*>(static_cast<void *>(this)); 144505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Ptr += sizeof(DesignatedInitExpr); 144605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Ptr += sizeof(Designator) * NumDesignators; 144705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); 144805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); 144905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor} 145005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 145105c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) { 145205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor assert(D.Kind == Designator::ArrayRangeDesignator && 145305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor "Requires array range designator"); 145405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor char* Ptr = static_cast<char*>(static_cast<void *>(this)); 145505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Ptr += sizeof(DesignatedInitExpr); 145605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Ptr += sizeof(Designator) * NumDesignators; 145705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); 145805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); 145905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor} 146005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 146105c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { 146205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor assert(D.Kind == Designator::ArrayRangeDesignator && 146305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor "Requires array range designator"); 146405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor char* Ptr = static_cast<char*>(static_cast<void *>(this)); 146505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Ptr += sizeof(DesignatedInitExpr); 146605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Ptr += sizeof(Designator) * NumDesignators; 146705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); 146805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2)); 146905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor} 147005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 147105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===// 1472ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek// ExprIterator. 1473ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===// 1474ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek 1475ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } 1476ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator*() const { return cast<Expr>(*I); } 1477ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator->() const { return cast<Expr>(*I); } 1478ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator[](size_t idx) const { 1479ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek return cast<Expr>(I[idx]); 1480ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek} 1481ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } 1482ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } 1483ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek 1484ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===// 148577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// Child Iterators for iterating over subexpressions/substatements 148677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===// 148777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 148877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// DeclRefExpr 14899ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } 14909ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } 149177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 14927779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff// ObjCIvarRefExpr 14935549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; } 14945549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } 14957779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff 1496e3e9add4fd788927df6f545570e7838db59c01d7Steve Naroff// ObjCPropertyRefExpr 14975549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; } 14985549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } 1499ae7840776d6cd31b4d7a4a345b61bcbb3744df6cSteve Naroff 15005daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian// ObjCKVCRefExpr 15015daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz JahanianStmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; } 15025daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz JahanianStmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; } 15035daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian 1504cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas Gregor// ObjCSuperExpr 1505cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas GregorStmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); } 1506cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas GregorStmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); } 1507cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas Gregor 1508d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner// PredefinedExpr 1509d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } 1510d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } 151177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 151277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// IntegerLiteral 15139ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } 15149ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } 151577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 151677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CharacterLiteral 15179ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); } 15189ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } 151977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 152077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// FloatingLiteral 15219ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } 15229ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } 152377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 15245d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner// ImaginaryLiteral 15255549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; } 15265549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; } 15275d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner 152877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// StringLiteral 15299ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } 15309ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_end() { return child_iterator(); } 153177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 153277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ParenExpr 15335549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_begin() { return &Val; } 15345549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_end() { return &Val+1; } 153577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 153677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// UnaryOperator 15375549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_begin() { return &Val; } 15385549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_end() { return &Val+1; } 153977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 15400518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl// SizeOfAlignOfExpr 15410518999d3adcc289997bd974dce90cc97f5c1c44Sebastian RedlStmt::child_iterator SizeOfAlignOfExpr::child_begin() { 15420518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // If this is of a type and the type is a VLA type (and not a typedef), the 15430518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // size expression of the VLA needs to be treated as an executable expression. 15440518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // Why isn't this weirdness documented better in StmtIterator? 15450518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (isArgumentType()) { 15460518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (VariableArrayType* T = dyn_cast<VariableArrayType>( 15470518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl getArgumentType().getTypePtr())) 15480518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl return child_iterator(T); 15490518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl return child_iterator(); 15500518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl } 1551d457589fc69dc7a9c80cd74d317c0b81a35a27c9Sebastian Redl return child_iterator(&Argument.Ex); 15529ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 15530518999d3adcc289997bd974dce90cc97f5c1c44Sebastian RedlStmt::child_iterator SizeOfAlignOfExpr::child_end() { 15540518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (isArgumentType()) 15550518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl return child_iterator(); 1556d457589fc69dc7a9c80cd74d317c0b81a35a27c9Sebastian Redl return child_iterator(&Argument.Ex + 1); 15579ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 155877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 155977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ArraySubscriptExpr 15601237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_begin() { 15615549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 156277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 15631237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_end() { 15645549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+END_EXPR; 156577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 156677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 156777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CallExpr 15681237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_begin() { 15695549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 157077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 15711237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_end() { 15725549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+NumArgs+ARGS_START; 157377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 15741237c673c07f9d827129ba02720108816abde562Ted Kremenek 15751237c673c07f9d827129ba02720108816abde562Ted Kremenek// MemberExpr 15765549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_begin() { return &Base; } 15775549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_end() { return &Base+1; } 15781237c673c07f9d827129ba02720108816abde562Ted Kremenek 1579213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman// ExtVectorElementExpr 15805549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; } 15815549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; } 15821237c673c07f9d827129ba02720108816abde562Ted Kremenek 15831237c673c07f9d827129ba02720108816abde562Ted Kremenek// CompoundLiteralExpr 15845549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; } 15855549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; } 15861237c673c07f9d827129ba02720108816abde562Ted Kremenek 15871237c673c07f9d827129ba02720108816abde562Ted Kremenek// CastExpr 15885549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_begin() { return &Op; } 15895549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_end() { return &Op+1; } 15901237c673c07f9d827129ba02720108816abde562Ted Kremenek 15911237c673c07f9d827129ba02720108816abde562Ted Kremenek// BinaryOperator 15921237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_begin() { 15935549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 15941237c673c07f9d827129ba02720108816abde562Ted Kremenek} 15951237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_end() { 15965549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+END_EXPR; 15971237c673c07f9d827129ba02720108816abde562Ted Kremenek} 15981237c673c07f9d827129ba02720108816abde562Ted Kremenek 15991237c673c07f9d827129ba02720108816abde562Ted Kremenek// ConditionalOperator 16001237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_begin() { 16015549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 16021237c673c07f9d827129ba02720108816abde562Ted Kremenek} 16031237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_end() { 16045549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+END_EXPR; 16051237c673c07f9d827129ba02720108816abde562Ted Kremenek} 16061237c673c07f9d827129ba02720108816abde562Ted Kremenek 16071237c673c07f9d827129ba02720108816abde562Ted Kremenek// AddrLabelExpr 16089ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } 16099ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } 16101237c673c07f9d827129ba02720108816abde562Ted Kremenek 16111237c673c07f9d827129ba02720108816abde562Ted Kremenek// StmtExpr 16125549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_begin() { return &SubStmt; } 16135549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; } 16141237c673c07f9d827129ba02720108816abde562Ted Kremenek 16151237c673c07f9d827129ba02720108816abde562Ted Kremenek// TypesCompatibleExpr 16169ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_begin() { 16179ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 16189ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 16199ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek 16209ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_end() { 16219ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 16229ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 16231237c673c07f9d827129ba02720108816abde562Ted Kremenek 16241237c673c07f9d827129ba02720108816abde562Ted Kremenek// ChooseExpr 16255549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; } 16265549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; } 16272d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor 16282d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor// GNUNullExpr 16292d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas GregorStmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); } 16302d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas GregorStmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); } 16311237c673c07f9d827129ba02720108816abde562Ted Kremenek 1632e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman// OverloadExpr 16335549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; } 16345549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; } 1635e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman 1636d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman// ShuffleVectorExpr 1637d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_begin() { 16385549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 1639d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman} 1640d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_end() { 16415549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+NumExprs; 1642d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman} 1643d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman 16447c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson// VAArgExpr 16455549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_begin() { return &Val; } 16465549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_end() { return &Val+1; } 16477c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson 164866b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson// InitListExpr 164966b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonStmt::child_iterator InitListExpr::child_begin() { 16505549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return InitExprs.size() ? &InitExprs[0] : 0; 165166b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson} 165266b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonStmt::child_iterator InitListExpr::child_end() { 16535549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0; 165466b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson} 165566b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson 165605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// DesignatedInitExpr 165705c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorStmt::child_iterator DesignatedInitExpr::child_begin() { 165805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor char* Ptr = static_cast<char*>(static_cast<void *>(this)); 165905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Ptr += sizeof(DesignatedInitExpr); 166005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor Ptr += sizeof(Designator) * NumDesignators; 166105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); 166205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor} 166305c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorStmt::child_iterator DesignatedInitExpr::child_end() { 166405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor return child_iterator(&*child_begin() + NumSubExprs); 166505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor} 166605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor 16671237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCStringLiteral 16689ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_begin() { 16699ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 16709ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 16719ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_end() { 16729ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 16739ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 16741237c673c07f9d827129ba02720108816abde562Ted Kremenek 16751237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCEncodeExpr 16769ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } 16779ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } 16781237c673c07f9d827129ba02720108816abde562Ted Kremenek 1679b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian// ObjCSelectorExpr 16809ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_begin() { 16819ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 16829ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 16839ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_end() { 16849ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 16859ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 1686b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian 1687390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian// ObjCProtocolExpr 16889ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_begin() { 16899ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 16909ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 16919ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_end() { 16929ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 16939ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 1694390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian 1695563477da25f824e37c535131695dc4dc9b68c465Steve Naroff// ObjCMessageExpr 1696ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted KremenekStmt::child_iterator ObjCMessageExpr::child_begin() { 16975549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START; 1698563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1699563477da25f824e37c535131695dc4dc9b68c465Steve NaroffStmt::child_iterator ObjCMessageExpr::child_end() { 17005549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+ARGS_START+getNumArgs(); 1701563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1702563477da25f824e37c535131695dc4dc9b68c465Steve Naroff 17034eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff// Blocks 170456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_begin() { return child_iterator(); } 170556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_end() { return child_iterator(); } 17064eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff 17079da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();} 17089da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); } 1709