Expr.cpp revision 4fcd399a52ae45ed8ebfdd3a25e01cfb76fa366d
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) 106b4609806e9232593ece09ce08b630836e825865cDouglas Gregor : Expr(SC, t), NumArgs(numargs) { 107b4609806e9232593ece09ce08b630836e825865cDouglas Gregor SubExprs = new Stmt*[numargs+1]; 108b4609806e9232593ece09ce08b630836e825865cDouglas Gregor SubExprs[FN] = fn; 109b4609806e9232593ece09ce08b630836e825865cDouglas Gregor for (unsigned i = 0; i != numargs; ++i) 110b4609806e9232593ece09ce08b630836e825865cDouglas Gregor SubExprs[i+ARGS_START] = args[i]; 111b4609806e9232593ece09ce08b630836e825865cDouglas Gregor RParenLoc = rparenloc; 112b4609806e9232593ece09ce08b630836e825865cDouglas Gregor} 113e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman 1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerCallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 1155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation rparenloc) 11677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek : Expr(CallExprClass, t), NumArgs(numargs) { 1175549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek SubExprs = new Stmt*[numargs+1]; 11877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek SubExprs[FN] = fn; 1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0; i != numargs; ++i) 12077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek SubExprs[i+ARGS_START] = args[i]; 1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer RParenLoc = rparenloc; 1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 124d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// setNumArgs - This changes the number of arguments present in this call. 125d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// Any orphaned expressions are deleted by this, and any new operands are set 126d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// to null. 127d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattnervoid CallExpr::setNumArgs(unsigned NumArgs) { 128d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // No change, just return. 129d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner if (NumArgs == getNumArgs()) return; 130d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 131d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // If shrinking # arguments, just delete the extras and forgot them. 132d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner if (NumArgs < getNumArgs()) { 133d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i) 134d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner delete getArg(i); 135d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner this->NumArgs = NumArgs; 136d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner return; 137d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner } 138d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 139d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // Otherwise, we are growing the # arguments. New an bigger argument array. 1405549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek Stmt **NewSubExprs = new Stmt*[NumArgs+1]; 141d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // Copy over args. 142d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i) 143d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner NewSubExprs[i] = SubExprs[i]; 144d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // Null out new args. 145d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i) 146d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner NewSubExprs[i] = 0; 147d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 148d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner delete[] SubExprs; 149d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner SubExprs = NewSubExprs; 150d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner this->NumArgs = NumArgs; 151d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner} 152d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 153cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 154cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// not, return 0. 155cb888967400a03504c88acedd5248d6778a82f46Chris Lattnerunsigned CallExpr::isBuiltinCall() const { 156c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff // All simple function calls (e.g. func()) are implicitly cast to pointer to 157c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff // function. As a result, we try and obtain the DeclRefExpr from the 158c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff // ImplicitCastExpr. 159c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); 160c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). 161cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return 0; 162cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 163c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); 164c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff if (!DRE) 165cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return 0; 166cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 167bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); 168bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson if (!FDecl) 169cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return 0; 170cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 1714fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor if (!FDecl->getIdentifier()) 1724fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor return 0; 1734fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor 174cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return FDecl->getIdentifier()->getBuiltinID(); 175cb888967400a03504c88acedd5248d6778a82f46Chris Lattner} 176bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson 177cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "<<=". 1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *BinaryOperator::getOpcodeStr(Opcode Op) { 1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Op) { 1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: assert(0 && "Unknown binary operator"); 1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Mul: return "*"; 1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Div: return "/"; 1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Rem: return "%"; 1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Add: return "+"; 1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Sub: return "-"; 1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Shl: return "<<"; 1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Shr: return ">>"; 1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LT: return "<"; 1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case GT: return ">"; 1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LE: return "<="; 1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case GE: return ">="; 1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case EQ: return "=="; 1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case NE: return "!="; 1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case And: return "&"; 1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Xor: return "^"; 1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Or: return "|"; 1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LAnd: return "&&"; 2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LOr: return "||"; 2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Assign: return "="; 2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case MulAssign: return "*="; 2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case DivAssign: return "/="; 2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case RemAssign: return "%="; 2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case AddAssign: return "+="; 2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case SubAssign: return "-="; 2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ShlAssign: return "<<="; 2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ShrAssign: return ">>="; 2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case AndAssign: return "&="; 2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case XorAssign: return "^="; 2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case OrAssign: return "|="; 2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Comma: return ","; 2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 21666b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonInitListExpr::InitListExpr(SourceLocation lbraceloc, 217418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner Expr **initExprs, unsigned numInits, 218418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner SourceLocation rbraceloc, bool hadDesignators) 219c5ae899b4bbf65488445316c63168079177db0edSteve Naroff : Expr(InitListExprClass, QualType()), 220418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), HadDesignators(hadDesignators) { 221418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner 222418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits); 22366b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson} 2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 225bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block. 2264eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff/// 2274eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroffconst FunctionType *BlockExpr::getFunctionType() const { 2284eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff return getType()->getAsBlockPointerType()-> 2294eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff getPointeeType()->getAsFunctionType(); 2304eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff} 2314eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff 23256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffSourceLocation BlockExpr::getCaretLocation() const { 23356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff return TheBlock->getCaretLocation(); 23456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff} 23556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroffconst Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); } 23656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt *BlockExpr::getBody() { return TheBlock->getBody(); } 23756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff 23856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff 2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines 2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// hasLocalSideEffect - Return true if this immediate expression has side 2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// effects, not counting any sub-expressions. 2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Expr::hasLocalSideEffect() const { 2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (getStmtClass()) { 2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 2495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ParenExprClass: 2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect(); 2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperatorClass: { 2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const UnaryOperator *UO = cast<UnaryOperator>(this); 2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (UO->getOpcode()) { 2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: return false; 2565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PostInc: 2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PostDec: 2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PreInc: 2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PreDec: 2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; // ++/-- 2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Deref: 2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Dereferencing a volatile pointer is a side-effect. 2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return getType().isVolatileQualified(); 2655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Real: 2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Imag: 2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // accessing a piece of a volatile complex is a side-effect. 2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return UO->getSubExpr()->getType().isVolatileQualified(); 2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Extension: 2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return UO->getSubExpr()->hasLocalSideEffect(); 2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 274e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner case BinaryOperatorClass: { 275e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner const BinaryOperator *BinOp = cast<BinaryOperator>(this); 276e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner // Consider comma to have side effects if the LHS and RHS both do. 277e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner if (BinOp->getOpcode() == BinaryOperator::Comma) 278e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner return BinOp->getLHS()->hasLocalSideEffect() && 279e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner BinOp->getRHS()->hasLocalSideEffect(); 280e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner 281e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner return BinOp->isAssignmentOp(); 282e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner } 283eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner case CompoundAssignOperatorClass: 2841f683e9cf2e855b32d3fb4b142aa121cd3cf1088Chris Lattner return true; 2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 286ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian case ConditionalOperatorClass: { 287ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian const ConditionalOperator *Exp = cast<ConditionalOperator>(this); 288ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian return Exp->getCond()->hasLocalSideEffect() 289ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect()) 290ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect()); 291ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian } 292ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian 2935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case MemberExprClass: 2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ArraySubscriptExprClass: 2955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the base pointer or element is to a volatile pointer/field, accessing 2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // if is a side effect. 2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return getType().isVolatileQualified(); 298211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman 2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case CallExprClass: 300b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CXXOperatorCallExprClass: 3015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }" 3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // should warn. 3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 304a9c01021724b9b546d282b8609cbe559734812ecChris Lattner case ObjCMessageExprClass: 305a9c01021724b9b546d282b8609cbe559734812ecChris Lattner return true; 306611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner case StmtExprClass: { 307611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // Statement exprs don't logically have side effects themselves, but are 308611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // sometimes used in macros in ways that give them a type that is unused. 309611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // For example ({ blah; foo(); }) will end up with a type if foo has a type. 310611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // however, if the result of the stmt expr is dead, we don't want to emit a 311611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // warning. 312611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); 313611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner if (!CS->body_empty()) 314611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner if (const Expr *E = dyn_cast<Expr>(CS->body_back())) 315611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner return E->hasLocalSideEffect(); 316611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner return false; 317611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner } 3186eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor case CStyleCastExprClass: 319987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case CXXFunctionalCastExprClass: 3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a cast to void, check the operand. Otherwise, the result of 3215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the cast is unused. 3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getType()->isVoidType()) 3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect(); 3245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 32504421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner 3264be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman case ImplicitCastExprClass: 3274be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman // Check the operand, since implicit casts are inserted by Sema 3284be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect(); 3294be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman 33004421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 33104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect(); 3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 335ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor/// DeclCanBeLvalue - Determine whether the given declaration can be 336ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor/// an lvalue. This is a helper routine for isLvalue. 337ba7e210a999275695f58be03ef402758cfec3635Douglas Gregorstatic bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) { 33890b7bc67518a408d397f2400ff6c40e5465b45e4Argyrios Kyrtzidis return isa<VarDecl>(Decl) || isa<CXXFieldDecl>(Decl) || 339ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor // C++ 3.10p2: An lvalue refers to an object or function. 340ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor (Ctx.getLangOptions().CPlusPlus && 341ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl))); 342ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor} 343ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor 3445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an 3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// incomplete type other than void. Nonarray expressions that can be lvalues: 3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - name, where name must be a variable 3475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - e[i] 3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - (e), where e must be an lvalue 3495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - e.name, where e must be an lvalue 3505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - e->name 3515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - *e, the type of e cannot be a function type 3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - string-constant 3537da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension] 35408ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling/// - reference type [C++ [expr]] 3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 35628be73f74c9e241a23ea24fe5756623de6bf1084Chris LattnerExpr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { 35798cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor // first, check the type (C99 6.3.2.1). Expressions with function 35898cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor // type in C are not lvalues, but they can be lvalues in C++. 35998cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) 3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_NotObjectType; 3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 362acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff // Allow qualified void which is an incomplete type other than void (yuck). 36328be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers()) 364acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff return LV_IncompleteVoidType; 365acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff 36698cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor /// FIXME: Expressions can't have reference type, so the following 36798cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor /// isn't needed. 368cb4f9a6d0be445d84af35ba81323be6127816f9eChris Lattner if (TR->isReferenceType()) // C++ [expr] 36908ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling return LV_Valid; 37008ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling 3715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the type looks fine, now check the expression 3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (getStmtClass()) { 3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case StringLiteralClass: // C99 6.5.1p4 3747323a6297edad643c202594dcf3d9a174de96ca6Anders Carlsson return LV_Valid; 3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) 3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // For vectors, make sure base is an lvalue (i.e. not a function call). 3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) 37828be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx); 3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_Valid; 3804111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner case DeclRefExprClass: { // C99 6.5.1p2 381ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl(); 382ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor if (DeclCanBeLvalue(RefdDecl, Ctx)) 3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_Valid; 3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3854111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner } 386dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff case BlockDeclRefExprClass: { 387dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); 3884f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff if (isa<VarDecl>(BDR->getDecl())) 389dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff return LV_Valid; 390dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff break; 391dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff } 392fdd75663fffeb2058a7847975e50837e61200593Anton Korobeynikov case MemberExprClass: { // C99 6.5.2.3p4 3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const MemberExpr *m = cast<MemberExpr>(this); 39428be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); 395fdd75663fffeb2058a7847975e50837e61200593Anton Korobeynikov } 3967da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner case UnaryOperatorClass: 3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) 3987da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner return LV_Valid; // C99 6.5.3p4 3997da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner 4007da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real || 401baf0d6678418e0dd9309438c3e50274253cfc7b2Chris Lattner cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag || 402baf0d6678418e0dd9309438c3e50274253cfc7b2Chris Lattner cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension) 40328be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU. 40474253736184c0717a0649922551bf9d8b6815651Douglas Gregor 40574253736184c0717a0649922551bf9d8b6815651Douglas Gregor if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1 40674253736184c0717a0649922551bf9d8b6815651Douglas Gregor (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc || 40774253736184c0717a0649922551bf9d8b6815651Douglas Gregor cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec)) 40874253736184c0717a0649922551bf9d8b6815651Douglas Gregor return LV_Valid; 4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 410eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor case ImplicitCastExprClass: 411eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid 412eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor : LV_InvalidExpression; 4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ParenExprClass: // C99 6.5.1p5 41428be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx); 415eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor case BinaryOperatorClass: 416eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor case CompoundAssignOperatorClass: { 417eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor const BinaryOperator *BinOp = cast<BinaryOperator>(this); 418337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 419337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1 420337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor BinOp->getOpcode() == BinaryOperator::Comma) 421337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor return BinOp->getRHS()->isLvalue(Ctx); 422337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 423bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor if (!BinOp->isAssignmentOp()) 424eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor return LV_InvalidExpression; 425eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor 426bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor if (Ctx.getLangOptions().CPlusPlus) 427bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor // C++ [expr.ass]p1: 428bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor // The result of an assignment operation [...] is an lvalue. 429bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor return LV_Valid; 430bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor 431bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor 432bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor // C99 6.5.16: 433bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor // An assignment expression [...] is not an lvalue. 434bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor return LV_InvalidExpression; 435eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor } 436b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CallExprClass: 437b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CXXOperatorCallExprClass: { 4389d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // C++ [expr.call]p10: 4399d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // A function call is an lvalue if and only if the result type 4409d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // is a reference. 44127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType(); 4429d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor if (const PointerType *FnTypePtr = CalleeType->getAsPointerType()) 4439d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor if (const FunctionType *FnType 4449d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor = FnTypePtr->getPointeeType()->getAsFunctionType()) 4459d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor if (FnType->getResultType()->isReferenceType()) 4469d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor return LV_Valid; 4479d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor 4489d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor break; 4499d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor } 450e6386394677ed4f77b20e2e3d5446a3a2f628e53Steve Naroff case CompoundLiteralExprClass: // C99 6.5.2.5p5 451e6386394677ed4f77b20e2e3d5446a3a2f628e53Steve Naroff return LV_Valid; 452213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman case ExtVectorElementExprClass: 453213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements()) 454fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return LV_DuplicateVectorComponents; 455fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return LV_Valid; 456027282d1c1ac151aa7b1b3b45babc918b8ad456aSteve Naroff case ObjCIvarRefExprClass: // ObjC instance variables are lvalues. 457027282d1c1ac151aa7b1b3b45babc918b8ad456aSteve Naroff return LV_Valid; 458799a6a6850af625946bb8d88ca960bb6604e3858Steve Naroff case ObjCPropertyRefExprClass: // FIXME: check if read-only property. 459799a6a6850af625946bb8d88ca960bb6604e3858Steve Naroff return LV_Valid; 460d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner case PredefinedExprClass: 461796da18402f286b897782a298ae3b20c459c102eDouglas Gregor return LV_Valid; 4629d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case VAArgExprClass: 4639d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor return LV_Valid; 46404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 46528be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx); 46624b41fa8239c63b9eb570d3e83c4a82840656a65Argyrios Kyrtzidis case CXXConditionDeclExprClass: 46724b41fa8239c63b9eb570d3e83c4a82840656a65Argyrios Kyrtzidis return LV_Valid; 4686eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor case CStyleCastExprClass: 4699d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case CXXFunctionalCastExprClass: 4709d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case CXXStaticCastExprClass: 4719d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case CXXDynamicCastExprClass: 4729d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case CXXReinterpretCastExprClass: 4739d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor case CXXConstCastExprClass: 4749d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // The result of an explicit cast is an lvalue if the type we are 4759d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // casting to is a reference type. See C++ [expr.cast]p1, 4769d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2, 4779d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1. 4789d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType()) 4799d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor return LV_Valid; 4809d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor break; 481c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl case CXXTypeidExprClass: 482c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ... 483c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl return LV_Valid; 484796da18402f286b897782a298ae3b20c459c102eDouglas Gregor case CXXThisExprClass: 485796da18402f286b897782a298ae3b20c459c102eDouglas Gregor return LV_InvalidExpression; 4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 4875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_InvalidExpression; 4905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// does not have an incomplete type, does not have a const-qualified type, and 4945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// if it is a structure or union, does not have any member (including, 4955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// recursively, any member or element of all contained aggregates or unions) 4965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// with a const-qualified type. 49728be73f74c9e241a23ea24fe5756623de6bf1084Chris LattnerExpr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const { 49828be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner isLvalueResult lvalResult = isLvalue(Ctx); 4995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 5005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (lvalResult) { 501ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor case LV_Valid: 502ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor // C++ 3.10p11: Functions cannot be modified, but pointers to 503ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor // functions can be modifiable. 504ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) 505ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor return MLV_NotObjectType; 506ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor break; 507ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor 5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LV_NotObjectType: return MLV_NotObjectType; 5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LV_IncompleteVoidType: return MLV_IncompleteVoidType; 510fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; 511ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner case LV_InvalidExpression: 512ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner // If the top level is a C-style cast, and the subexpression is a valid 513ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner // lvalue, then this is probably a use of the old-school "cast as lvalue" 514ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner // GCC extension. We don't support it, but we want to produce good 515ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner // diagnostics when it happens so that the user knows why. 516ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this)) 517ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) 518ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner return MLV_LValueCast; 519ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner return MLV_InvalidExpression; 5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 521c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 522c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType CT = Ctx.getCanonicalType(getType()); 523c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 524c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (CT.isConstQualified()) 5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_ConstQualified; 526c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (CT->isArrayType()) 5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_ArrayType; 528c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (CT->isIncompleteType()) 5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_IncompleteType; 5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 531c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const RecordType *r = CT->getAsRecordType()) { 5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (r->hasConstFields()) 5335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_ConstQualified; 5345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 5354f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // The following is illegal: 5364f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // void takeclosure(void (^C)(void)); 5374f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // void func() { int x = 1; takeclosure(^{ x = 7 }); } 5384f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // 5394f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff if (getStmtClass() == BlockDeclRefExprClass) { 5404f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); 5414f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl())) 5424f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff return MLV_NotBlockQualified; 5434f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff } 5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_Valid; 5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 5472e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek/// hasGlobalStorage - Return true if this expression has static storage 5484cc627111453b75519d5130b57e06256da7b00e8Chris Lattner/// duration. This means that the address of this expression is a link-time 5494cc627111453b75519d5130b57e06256da7b00e8Chris Lattner/// constant. 5502e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenekbool Expr::hasGlobalStorage() const { 5511d09ecca892185ba067e47ba879f424de59950efChris Lattner switch (getStmtClass()) { 5521d09ecca892185ba067e47ba879f424de59950efChris Lattner default: 5531d09ecca892185ba067e47ba879f424de59950efChris Lattner return false; 5544cc627111453b75519d5130b57e06256da7b00e8Chris Lattner case ParenExprClass: 5552e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage(); 5564cc627111453b75519d5130b57e06256da7b00e8Chris Lattner case ImplicitCastExprClass: 5572e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage(); 558e9b12198c4cc7f5687960100351b4af006c14469Steve Naroff case CompoundLiteralExprClass: 559e9b12198c4cc7f5687960100351b4af006c14469Steve Naroff return cast<CompoundLiteralExpr>(this)->isFileScope(); 5601d09ecca892185ba067e47ba879f424de59950efChris Lattner case DeclRefExprClass: { 5611d09ecca892185ba067e47ba879f424de59950efChris Lattner const Decl *D = cast<DeclRefExpr>(this)->getDecl(); 5621d09ecca892185ba067e47ba879f424de59950efChris Lattner if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 5632e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return VD->hasGlobalStorage(); 56463f067f5a75ae97b53dc7a6a6530e2c72c8bb7f8Seo Sanghyeon if (isa<FunctionDecl>(D)) 56563f067f5a75ae97b53dc7a6a6530e2c72c8bb7f8Seo Sanghyeon return true; 5661d09ecca892185ba067e47ba879f424de59950efChris Lattner return false; 5671d09ecca892185ba067e47ba879f424de59950efChris Lattner } 568fb7080663a0f0abd687a6f58ac917d9a76318b73Chris Lattner case MemberExprClass: { 5691d09ecca892185ba067e47ba879f424de59950efChris Lattner const MemberExpr *M = cast<MemberExpr>(this); 5702e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return !M->isArrow() && M->getBase()->hasGlobalStorage(); 571fb7080663a0f0abd687a6f58ac917d9a76318b73Chris Lattner } 5724cc627111453b75519d5130b57e06256da7b00e8Chris Lattner case ArraySubscriptExprClass: 5732e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage(); 574d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner case PredefinedExprClass: 575fa28b30d5a1e93e5263be33e343532b900d2c643Chris Lattner return true; 57604421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 57704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage(); 5781d09ecca892185ba067e47ba879f424de59950efChris Lattner } 5791d09ecca892185ba067e47ba879f424de59950efChris Lattner} 5801d09ecca892185ba067e47ba879f424de59950efChris Lattner 5814e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() { 5824e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek Expr* E = this; 5834e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek while (ParenExpr* P = dyn_cast<ParenExpr>(E)) 5844e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek E = P->getSubExpr(); 5854e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek 5864e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek return E; 5874e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek} 5884e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek 58956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 59056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand. 59156f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() { 59256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner Expr *E = this; 59356f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner while (true) { 59456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner if (ParenExpr *P = dyn_cast<ParenExpr>(E)) 59556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner E = P->getSubExpr(); 59656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner else if (CastExpr *P = dyn_cast<CastExpr>(E)) 59756f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner E = P->getSubExpr(); 59856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner else 59956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner return E; 60056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner } 60156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner} 60256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner 60356f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner 60438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroffbool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { 60538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff switch (getStmtClass()) { 60638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff default: 60738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (Loc) *Loc = getLocStart(); 60838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 60938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case ParenExprClass: 61038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc); 61138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case StringLiteralClass: 6125d37e32a5bfefacdd7b4d0664199d72c703d489dSteve Naroff case ObjCStringLiteralClass: 61338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case FloatingLiteralClass: 61438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case IntegerLiteralClass: 61538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case CharacterLiteralClass: 61638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case ImaginaryLiteralClass: 6171a86b33c1870fee08d281c9f07ac1280195a7faeAnders Carlsson case TypesCompatibleExprClass: 6181a86b33c1870fee08d281c9f07ac1280195a7faeAnders Carlsson case CXXBoolLiteralExprClass: 61915425f9d2d92e2a5f99b09b8e2fc00025631e593Anders Carlsson case AddrLabelExprClass: 6202777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 621b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CallExprClass: 622b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CXXOperatorCallExprClass: { 62338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff const CallExpr *CE = cast<CallExpr>(this); 62445b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner 62545b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner // Allow any constant foldable calls to builtins. 62645b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner if (CE->isBuiltinCall() && CE->isEvaluatable(Ctx)) 627c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff return true; 62845b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner 62938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (Loc) *Loc = getLocStart(); 63038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 63138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 6324ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner case DeclRefExprClass: { 6334ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner const Decl *D = cast<DeclRefExpr>(this)->getDecl(); 6344ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner // Accept address of function. 6354ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D)) 6362777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 63738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (Loc) *Loc = getLocStart(); 6381d09ecca892185ba067e47ba879f424de59950efChris Lattner if (isa<VarDecl>(D)) 6391d09ecca892185ba067e47ba879f424de59950efChris Lattner return TR->isArrayType(); 64038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 6414ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner } 642b8f13a8baff3df080fe024fec47746e9a79fe649Steve Naroff case CompoundLiteralExprClass: 643b8f13a8baff3df080fe024fec47746e9a79fe649Steve Naroff if (Loc) *Loc = getLocStart(); 644b8f13a8baff3df080fe024fec47746e9a79fe649Steve Naroff // Allow "(int []){2,4}", since the array will be converted to a pointer. 645d47d4f518e43eaa716e2d18359bd725a03b87f58Nate Begeman // Allow "(vector type){2,4}" since the elements are all constant. 646d47d4f518e43eaa716e2d18359bd725a03b87f58Nate Begeman return TR->isArrayType() || TR->isVectorType(); 64738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperatorClass: { 64838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff const UnaryOperator *Exp = cast<UnaryOperator>(this); 64938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 6501d09ecca892185ba067e47ba879f424de59950efChris Lattner // C99 6.6p9 651239c15e5b07a6cc84278ede87edf638922767520Chris Lattner if (Exp->getOpcode() == UnaryOperator::AddrOf) { 6522e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek if (!Exp->getSubExpr()->hasGlobalStorage()) { 653239c15e5b07a6cc84278ede87edf638922767520Chris Lattner if (Loc) *Loc = getLocStart(); 654239c15e5b07a6cc84278ede87edf638922767520Chris Lattner return false; 655239c15e5b07a6cc84278ede87edf638922767520Chris Lattner } 656239c15e5b07a6cc84278ede87edf638922767520Chris Lattner return true; 657239c15e5b07a6cc84278ede87edf638922767520Chris Lattner } 6581d09ecca892185ba067e47ba879f424de59950efChris Lattner 65938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // Get the operand value. If this is sizeof/alignof, do not evalute the 66038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // operand. This affects C99 6.6p3. 6610518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (Exp->getOpcode() != UnaryOperator::OffsetOf && 66238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff !Exp->getSubExpr()->isConstantExpr(Ctx, Loc)) 66338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 66438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 66538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff switch (Exp->getOpcode()) { 66638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. 66738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // See C99 6.6p3. 66838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff default: 66938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (Loc) *Loc = Exp->getOperatorLoc(); 67038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 67138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::Extension: 67238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return true; // FIXME: this is wrong. 673d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff case UnaryOperator::OffsetOf: 6743c2b3170041f69a92904e3bab9b6d654eaf260acEli Friedman if (!Exp->getSubExpr()->getType()->isConstantSizeType()) { 67565383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner if (Loc) *Loc = Exp->getOperatorLoc(); 67638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 67765383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner } 6782777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 67938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::LNot: 68038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::Plus: 68138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::Minus: 68238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::Not: 6832777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 68438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 68538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 6860518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl case SizeOfAlignOfExprClass: { 6870518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this); 68838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // alignof always evaluates to a constant. 6890518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (Exp->isSizeOf()) { 6900518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl QualType ArgTy = Exp->getTypeOfArgument(); 6910518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (!ArgTy->isVoidType() && !ArgTy->isConstantSizeType()) { 6920518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (Loc) *Loc = Exp->getOperatorLoc(); 6930518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl return false; 6940518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl } 69565383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner } 6962777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 69738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 69838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case BinaryOperatorClass: { 69938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff const BinaryOperator *Exp = cast<BinaryOperator>(this); 70038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 70138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // The LHS of a constant expr is always evaluated and needed. 70238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (!Exp->getLHS()->isConstantExpr(Ctx, Loc)) 70338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 70438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 70538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (!Exp->getRHS()->isConstantExpr(Ctx, Loc)) 70638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 7072777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 70838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 70938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case ImplicitCastExprClass: 7106eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor case CStyleCastExprClass: 711987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case CXXFunctionalCastExprClass: { 7120835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr(); 7130835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis SourceLocation CastLoc = getLocStart(); 71438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (!SubExpr->isConstantExpr(Ctx, Loc)) { 71538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (Loc) *Loc = SubExpr->getLocStart(); 71638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 71738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 7182777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 71938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 72038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case ConditionalOperatorClass: { 72138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff const ConditionalOperator *Exp = cast<ConditionalOperator>(this); 7222777e494c96bee69141a10a0174d5da16e7b927bChris Lattner if (!Exp->getCond()->isConstantExpr(Ctx, Loc) || 7233907323dd6665c0c4e383435cb145233f4533406Anders Carlsson // Handle the GNU extension for missing LHS. 7243907323dd6665c0c4e383435cb145233f4533406Anders Carlsson !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) || 7252777e494c96bee69141a10a0174d5da16e7b927bChris Lattner !Exp->getRHS()->isConstantExpr(Ctx, Loc)) 72638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 7272777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 72838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 729d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff case InitListExprClass: { 730d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff const InitListExpr *Exp = cast<InitListExpr>(this); 731d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff unsigned numInits = Exp->getNumInits(); 732d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff for (unsigned i = 0; i < numInits; i++) { 733d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) { 734d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff if (Loc) *Loc = Exp->getInit(i)->getLocStart(); 735d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff return false; 736d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff } 737d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff } 738d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff return true; 739d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff } 74004421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 74104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->isConstantExpr(Ctx, Loc); 74238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 74338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff} 74438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 7455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIntegerConstantExpr - this recursive routine will test if an expression is 7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an integer constant expression. Note: With the introduction of VLA's in 7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// C99 the result of the sizeof operator is no longer always a constant 7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expression. The generalization of the wording to include any subexpression 7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions 7505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, 7515f6b632391e24b08ead3e62ba2e2765e770382edNuno Lopes/// "0 || f()" can be treated as a constant expression. In C90 this expression, 7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// occurring in a context requiring a constant, would have been a constraint 7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// violation. FIXME: This routine currently implements C90 semantics. 7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// To properly implement C99 semantics this routine will need to evaluate 7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expressions involving operators previously mentioned. 7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, 7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// comma, etc 7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not 761ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner/// permit this. This includes things like (int)1e1000 762ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// 763ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof 764ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer 765ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// cast+dereference. 766590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattnerbool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 767590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner SourceLocation *Loc, bool isEvaluated) const { 768a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman // Pretest for integral type; some parts of the code crash for types that 769a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman // can't be sized. 770a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman if (!getType()->isIntegralType()) { 771a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman if (Loc) *Loc = getLocStart(); 772a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman return false; 773a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman } 7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (getStmtClass()) { 7755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 7765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 7775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 7785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ParenExprClass: 7795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return cast<ParenExpr>(this)->getSubExpr()-> 780590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); 7815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case IntegerLiteralClass: 7825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = cast<IntegerLiteral>(this)->getValue(); 7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 7842eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner case CharacterLiteralClass: { 7852eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner const CharacterLiteral *CL = cast<CharacterLiteral>(this); 78698be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 7872eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner Result = CL->getValue(); 788f0fbcb356bc4eb5f6467dc4fc4bc447e9a630102Chris Lattner Result.setIsUnsigned(!getType()->isSignedIntegerType()); 7895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 7902eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner } 791b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson case CXXBoolLiteralExprClass: { 792b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this); 793b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 794b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson Result = BL->getValue(); 795b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson Result.setIsUnsigned(!getType()->isSignedIntegerType()); 796b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson break; 797b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson } 7987267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis case CXXZeroInitValueExprClass: 7997267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis Result.clear(); 8007267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis break; 8017b658aa315784fa489a32daeacfe400512ce468dSteve Naroff case TypesCompatibleExprClass: { 8027b658aa315784fa489a32daeacfe400512ce468dSteve Naroff const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this); 80398be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 804ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar // Per gcc docs "this built-in function ignores top level 805ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar // qualifiers". We need to use the canonical version to properly 806ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar // be able to strip CRV qualifiers from the type. 807ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar QualType T0 = Ctx.getCanonicalType(TCE->getArgType1()); 808ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar QualType T1 = Ctx.getCanonicalType(TCE->getArgType2()); 809ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar Result = Ctx.typesAreCompatible(T0.getUnqualifiedType(), 810ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar T1.getUnqualifiedType()); 811389cecc83f33e93c8ba6bf2e8269b8690404416fSteve Naroff break; 8127b658aa315784fa489a32daeacfe400512ce468dSteve Naroff } 813b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CallExprClass: 814b4609806e9232593ece09ce08b630836e825865cDouglas Gregor case CXXOperatorCallExprClass: { 81513b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff const CallExpr *CE = cast<CallExpr>(this); 81698be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 817a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner 818a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // If this is a call to a builtin function, constant fold it otherwise 819a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // reject it. 820a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner if (CE->isBuiltinCall()) { 821a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner APValue ResultAP; 8226ee7aa154e8bbb21a21254293410b944f78b0bfeChris Lattner if (CE->Evaluate(ResultAP, Ctx)) { 823a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner Result = ResultAP.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: 8325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (const EnumConstantDecl *D = 8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { 8345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = D->getInitVal(); 8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperatorClass: { 8405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const UnaryOperator *Exp = cast<UnaryOperator>(this); 8415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8420518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // Get the operand value. If this is offsetof, do not evalute the 8435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // operand. This affects C99 6.6p3. 8440518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()-> 8450518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Exp->getOpcode()) { 8495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. 8505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // See C99 6.6p3. 8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = Exp->getOperatorLoc(); 8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Extension: 85576e773a443be9f006610f46529e07d4c8d857680Chris Lattner return true; // FIXME: this is wrong. 8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::LNot: { 857bf7553889594886213e05cd3f94a6d6f7747bf7bChris Lattner bool Val = Result == 0; 85898be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 8595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = Val; 8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Plus: 8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Minus: 8655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = -Result; 8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Not: 8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = ~Result; 8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8705a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson case UnaryOperator::OffsetOf: 871aa1f9f1d50adf294674b74510b62e863b68572bcDaniel Dunbar Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 8725a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson Result = Exp->evaluateOffsetOf(Ctx); 8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8760518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl case SizeOfAlignOfExprClass: { 8770518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this); 878a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 879a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // Return the result in the right width. 88098be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 881a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 8820518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl QualType ArgTy = Exp->getTypeOfArgument(); 883a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // sizeof(void) and __alignof__(void) = 1 as a gcc extension. 8840518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (ArgTy->isVoidType()) { 885a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner Result = 1; 886a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner break; 887a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner } 888a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 889a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // alignof always evaluates to a constant, sizeof does if arg is not VLA. 8900518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) { 89165383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner if (Loc) *Loc = Exp->getOperatorLoc(); 8925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 89365383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner } 8945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 89576e773a443be9f006610f46529e07d4c8d857680Chris Lattner // Get information about the size or align. 8960518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (ArgTy->isFunctionType()) { 897efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner // GCC extension: sizeof(function) = 1. 898efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner Result = Exp->isSizeOf() ? 1 : 4; 8996a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson } else { 90098be4943e8dc4f3905629a7102668960873cf863Chris Lattner unsigned CharSize = Ctx.Target.getCharWidth(); 9016a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson if (Exp->isSizeOf()) 9020518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl Result = Ctx.getTypeSize(ArgTy) / CharSize; 9036a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson else 9040518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl Result = Ctx.getTypeAlign(ArgTy) / CharSize; 905060e470e53b73167114284eb0b8c3e25bb3dad99Ted Kremenek } 9065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperatorClass: { 9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const BinaryOperator *Exp = cast<BinaryOperator>(this); 910e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar llvm::APSInt LHS, RHS; 911e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar 912e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar // Initialize result to have correct signedness and width. 913e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())), 914b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman !getType()->isSignedIntegerType()); 915b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman 9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The LHS of a constant expr is always evaluated and needed. 917e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated)) 9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The short-circuiting &&/|| operators don't necessarily evaluate their 9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // RHS. Make sure to pass isEvaluated down correctly. 9225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Exp->isLogicalOp()) { 9235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool RHSEval; 9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Exp->getOpcode() == BinaryOperator::LAnd) 925e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar RHSEval = LHS != 0; 9265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer else { 9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); 928e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar RHSEval = LHS == 0; 9295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 931590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, 9325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer isEvaluated & RHSEval)) 9335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 935590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) 9365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Exp->getOpcode()) { 9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 9415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 9425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Mul: 944e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS * RHS; 9455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Div: 9475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (RHS == 0) { 9485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isEvaluated) break; 9495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 9505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 952e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS / RHS; 9535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Rem: 9555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (RHS == 0) { 9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isEvaluated) break; 9575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 9585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 960e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS % RHS; 9615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 962e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Add: Result = LHS + RHS; break; 963e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Sub: Result = LHS - RHS; break; 9645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Shl: 965e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS << 966e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); 967e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar break; 9685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Shr: 969e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS >> 970e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); 9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 972e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::LT: Result = LHS < RHS; break; 973e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::GT: Result = LHS > RHS; break; 974e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::LE: Result = LHS <= RHS; break; 975e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::GE: Result = LHS >= RHS; break; 976e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::EQ: Result = LHS == RHS; break; 977e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::NE: Result = LHS != RHS; break; 978e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::And: Result = LHS & RHS; break; 979e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Xor: Result = LHS ^ RHS; break; 980e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Or: Result = LHS | RHS; break; 9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::LAnd: 982e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS != 0 && RHS != 0; 9835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::LOr: 985e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS != 0 || RHS != 0; 9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 987b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman 988b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman case BinaryOperator::Comma: 989b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman // C99 6.6p3: "shall not contain assignment, ..., or comma operators, 990b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman // *except* when they are contained within a subexpression that is not 991b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman // evaluated". Note that Assignment can never happen due to constraints 992b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman // on the LHS subexpr, so we don't need to check it here. 993b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman if (isEvaluated) { 994b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman if (Loc) *Loc = getLocStart(); 995b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman return false; 996b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman } 997b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman 998b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman // The result of the constant expr is the RHS. 999b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman Result = RHS; 1000b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman return true; 10015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); 10045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 100626dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner case ImplicitCastExprClass: 10076eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor case CStyleCastExprClass: 1008987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case CXXFunctionalCastExprClass: { 10090835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr(); 10100835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis SourceLocation CastLoc = getLocStart(); 101126dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner 10125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.6p6: shall only convert arithmetic types to integer types. 101326dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (!SubExpr->getType()->isArithmeticType() || 101426dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner !getType()->isIntegerType()) { 101526dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (Loc) *Loc = SubExpr->getLocStart(); 10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1018987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner 101998be4943e8dc4f3905629a7102668960873cf863Chris Lattner uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType())); 1020987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner 10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Handle simple integer->integer casts. 102226dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (SubExpr->getType()->isIntegerType()) { 102326dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 10245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 102526dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner 102626dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner // Figure out if this is a truncate, extend or noop cast. 102726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner // If the input is signed, do a sign extend, noop, or truncate. 1028c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner if (getType()->isBooleanType()) { 1029c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner // Conversion to bool compares against zero. 1030c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result = Result != 0; 1031c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result.zextOrTrunc(DestWidth); 1032c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner } else if (SubExpr->getType()->isSignedIntegerType()) 103326dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner Result.sextOrTrunc(DestWidth); 103426dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner else // If the input is unsigned, do a zero extend, noop, or truncate. 103526dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner Result.zextOrTrunc(DestWidth); 10365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 10375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Allow floating constants that are the immediate operands of casts or that 10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // are parenthesized. 104126dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner const Expr *Operand = SubExpr; 10425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) 10435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Operand = PE->getSubExpr(); 1044987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner 1045987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner // If this isn't a floating literal, we can't handle it. 1046987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand); 1047987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner if (!FL) { 1048987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner if (Loc) *Loc = Operand->getLocStart(); 1049987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner return false; 1050987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner } 1051c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner 1052c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner // If the destination is boolean, compare against zero. 1053c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner if (getType()->isBooleanType()) { 1054c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result = !FL->getValue().isZero(); 1055c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result.zextOrTrunc(DestWidth); 1056c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner break; 1057c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner } 10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1059987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner // Determine whether we are converting to unsigned or signed. 1060987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner bool DestSigned = getType()->isSignedIntegerType(); 1061ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner 1062ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can 1063ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner // be called multiple times per AST. 1064ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen uint64_t Space[4]; 1065ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen bool ignored; 1066ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned, 1067ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen llvm::APFloat::rmTowardZero, 1068ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen &ignored); 1069987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner Result = llvm::APInt(DestWidth, 4, Space); 1070987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner break; 10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ConditionalOperatorClass: { 10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const ConditionalOperator *Exp = cast<ConditionalOperator>(this); 10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1075590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const Expr *TrueExp = Exp->getLHS(); 10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const Expr *FalseExp = Exp->getRHS(); 10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Result == 0) std::swap(TrueExp, FalseExp); 10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Evaluate the false one first, discard the result. 10833907323dd6665c0c4e383435cb145233f4533406Anders Carlsson if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false)) 10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Evalute the true one, capture the result. 10863907323dd6665c0c4e383435cb145233f4533406Anders Carlsson if (TrueExp && 10873907323dd6665c0c4e383435cb145233f4533406Anders Carlsson !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 109104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 109204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this) 109304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); 10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Cases that are valid constant exprs fall through to here. 10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.setIsUnsigned(getType()->isUnsignedIntegerType()); 10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an 11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// integer constant expression with the value zero, or if this is one that is 11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// cast to void*. 1104590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattnerbool Expr::isNullPointerConstant(ASTContext &Ctx) const { 11050777972d38a3125efed962b045704c30ae6965cfSebastian Redl // Strip off a cast to void*, if it exists. Except in C++. 11060835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { 11076215dee86c0e715b9f2b0d401ab2a5fcf629f1afSebastian Redl if (!Ctx.getLangOptions().CPlusPlus) { 11080777972d38a3125efed962b045704c30ae6965cfSebastian Redl // Check that it is a cast to void*. 11090777972d38a3125efed962b045704c30ae6965cfSebastian Redl if (const PointerType *PT = CE->getType()->getAsPointerType()) { 11100777972d38a3125efed962b045704c30ae6965cfSebastian Redl QualType Pointee = PT->getPointeeType(); 11110777972d38a3125efed962b045704c30ae6965cfSebastian Redl if (Pointee.getCVRQualifiers() == 0 && 11120777972d38a3125efed962b045704c30ae6965cfSebastian Redl Pointee->isVoidType() && // to void* 11130777972d38a3125efed962b045704c30ae6965cfSebastian Redl CE->getSubExpr()->getType()->isIntegerType()) // from int. 11140777972d38a3125efed962b045704c30ae6965cfSebastian Redl return CE->getSubExpr()->isNullPointerConstant(Ctx); 11150777972d38a3125efed962b045704c30ae6965cfSebastian Redl } 11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1117aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { 1118aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // Ignore the ImplicitCastExpr type entirely. 1119aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff return ICE->getSubExpr()->isNullPointerConstant(Ctx); 1120aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { 1121aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // Accept ((void*)0) as a null pointer constant, as many other 1122aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // implementations do. 1123aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff return PE->getSubExpr()->isNullPointerConstant(Ctx); 11248123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner } else if (const CXXDefaultArgExpr *DefaultArg 11258123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner = dyn_cast<CXXDefaultArgExpr>(this)) { 112604421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner // See through default argument expressions 112704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return DefaultArg->getExpr()->isNullPointerConstant(Ctx); 1128aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff } 1129aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff 1130aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // This expression must be an integer type. 1131aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff if (!getType()->isIntegerType()) 1132aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff return false; 1133aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff 11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have an integer constant expression, we need to *evaluate* it and 11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // test for the value 0. 11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::APSInt Val(32); 1137aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0; 11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 113931a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff 114027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// isBitField - Return true if this expression is a bit-field. 114127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorbool Expr::isBitField() { 114227c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor Expr *E = this->IgnoreParenCasts(); 114327c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) 114427c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor return MemRef->getMemberDecl()->isBitField(); 114527c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor return false; 114627c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor} 114727c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor 1148213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const { 11498a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman if (const VectorType *VT = getType()->getAsVectorType()) 11508a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman return VT->getNumElements(); 11518a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman return 1; 11524d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner} 11534d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner 11548a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated. 1155213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const { 1156fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff const char *compStr = Accessor.getName(); 11577e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner unsigned length = Accessor.getLength(); 1158fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff 11597e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner for (unsigned i = 0; i != length-1; i++) { 1160fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff const char *s = compStr+i; 1161fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff for (const char c = *s++; *s; s++) 1162fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff if (c == *s) 1163fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return true; 1164fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff } 1165fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return false; 1166fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff} 1167b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner 11688a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. 11693b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess( 11703b8d116703db8018f855cbb4733ace426422623bNate Begeman llvm::SmallVectorImpl<unsigned> &Elts) const { 117192e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner bool isHi = Accessor.isStr("hi"); 117292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner bool isLo = Accessor.isStr("lo"); 117392e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner bool isEven = Accessor.isStr("e"); 117492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner bool isOdd = Accessor.isStr("o"); 11758a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman 11767e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner const char *compStr = Accessor.getName(); 11778a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman for (unsigned i = 0, e = getNumElements(); i != e; ++i) { 11788a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman uint64_t Index; 11798a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman 11808a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman if (isHi) 11818a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = e + i; 11828a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else if (isLo) 11838a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = i; 11848a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else if (isEven) 11858a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = 2 * i; 11868a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else if (isOdd) 11878a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = 2 * i + 1; 11888a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else 11898a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = ExtVectorType::getAccessorIdx(compStr[i]); 1190b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner 11913b8d116703db8018f855cbb4733ace426422623bNate Begeman Elts.push_back(Index); 1192b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner } 11938a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman} 11948a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman 119568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// constructor for instance messages. 1196bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve NaroffObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, 1197a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek QualType retType, ObjCMethodDecl *mproto, 1198db611d556f71f98b66b69514d45958d76e5727abSteve Naroff SourceLocation LBrac, SourceLocation RBrac, 119949f109c786f99eb7468dac3976db083a65493444Steve Naroff Expr **ArgExprs, unsigned nargs) 1200db611d556f71f98b66b69514d45958d76e5727abSteve Naroff : Expr(ObjCMessageExprClass, retType), SelName(selInfo), 1201ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted Kremenek MethodProto(mproto) { 120249f109c786f99eb7468dac3976db083a65493444Steve Naroff NumArgs = nargs; 12035549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek SubExprs = new Stmt*[NumArgs+1]; 120468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff SubExprs[RECEIVER] = receiver; 120549f109c786f99eb7468dac3976db083a65493444Steve Naroff if (NumArgs) { 120649f109c786f99eb7468dac3976db083a65493444Steve Naroff for (unsigned i = 0; i != NumArgs; ++i) 120768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 120868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff } 1209563477da25f824e37c535131695dc4dc9b68c465Steve Naroff LBracloc = LBrac; 1210563477da25f824e37c535131695dc4dc9b68c465Steve Naroff RBracloc = RBrac; 1211563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1212563477da25f824e37c535131695dc4dc9b68c465Steve Naroff 121368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// constructor for class messages. 121468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// FIXME: clsName should be typed to ObjCInterfaceType 1215bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve NaroffObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, 1216a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek QualType retType, ObjCMethodDecl *mproto, 1217db611d556f71f98b66b69514d45958d76e5727abSteve Naroff SourceLocation LBrac, SourceLocation RBrac, 121849f109c786f99eb7468dac3976db083a65493444Steve Naroff Expr **ArgExprs, unsigned nargs) 1219db611d556f71f98b66b69514d45958d76e5727abSteve Naroff : Expr(ObjCMessageExprClass, retType), SelName(selInfo), 1220ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted Kremenek MethodProto(mproto) { 122149f109c786f99eb7468dac3976db083a65493444Steve Naroff NumArgs = nargs; 12225549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek SubExprs = new Stmt*[NumArgs+1]; 12234df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown); 122449f109c786f99eb7468dac3976db083a65493444Steve Naroff if (NumArgs) { 122549f109c786f99eb7468dac3976db083a65493444Steve Naroff for (unsigned i = 0; i != NumArgs; ++i) 122668d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 122768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff } 1228563477da25f824e37c535131695dc4dc9b68c465Steve Naroff LBracloc = LBrac; 1229563477da25f824e37c535131695dc4dc9b68c465Steve Naroff RBracloc = RBrac; 1230563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1231563477da25f824e37c535131695dc4dc9b68c465Steve Naroff 12324df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek// constructor for class messages. 12334df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, 12344df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek QualType retType, ObjCMethodDecl *mproto, 12354df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SourceLocation LBrac, SourceLocation RBrac, 12364df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek Expr **ArgExprs, unsigned nargs) 12374df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek: Expr(ObjCMessageExprClass, retType), SelName(selInfo), 12384df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekMethodProto(mproto) { 12394df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek NumArgs = nargs; 12404df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs = new Stmt*[NumArgs+1]; 12414df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown); 12424df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek if (NumArgs) { 12434df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek for (unsigned i = 0; i != NumArgs; ++i) 12444df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 12454df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek } 12464df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek LBracloc = LBrac; 12474df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek RBracloc = RBrac; 12484df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek} 12494df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek 12504df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const { 12514df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; 12524df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek switch (x & Flags) { 12534df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek default: 12544df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek assert(false && "Invalid ObjCMessageExpr."); 12554df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek case IsInstMeth: 12564df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek return ClassInfo(0, 0); 12574df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek case IsClsMethDeclUnknown: 12584df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek return ClassInfo(0, (IdentifierInfo*) (x & ~Flags)); 12594df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek case IsClsMethDeclKnown: { 12604df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags); 12614df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek return ClassInfo(D, D->getIdentifier()); 12624df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek } 12634df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek } 12644df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek} 12654df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek 126627437caadea35f84d550cd29f024fcf3ea240eecChris Lattnerbool ChooseExpr::isConditionTrue(ASTContext &C) const { 126732442bbc98bafa512fa42d46fedf60ed7d79f574Daniel Dunbar return getCond()->getIntegerConstantExprValue(C) != 0; 126827437caadea35f84d550cd29f024fcf3ea240eecChris Lattner} 126927437caadea35f84d550cd29f024fcf3ea240eecChris Lattner 12705a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlssonstatic int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) 12715a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson{ 12725a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 12735a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson QualType Ty = ME->getBase()->getType(); 12745a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 12755a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson RecordDecl *RD = Ty->getAsRecordType()->getDecl(); 127698be4943e8dc4f3905629a7102668960873cf863Chris Lattner const ASTRecordLayout &RL = C.getASTRecordLayout(RD); 12775a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson FieldDecl *FD = ME->getMemberDecl(); 12785a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 12795a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson // FIXME: This is linear time. 12805a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson unsigned i = 0, e = 0; 12815a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson for (i = 0, e = RD->getNumMembers(); i != e; i++) { 12825a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson if (RD->getMember(i) == FD) 12835a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson break; 12845a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson } 12855a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 12865a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase()); 12875a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { 12885a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson const Expr *Base = ASE->getBase(); 12895a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 129098be4943e8dc4f3905629a7102668960873cf863Chris Lattner int64_t size = C.getTypeSize(ASE->getType()); 129132442bbc98bafa512fa42d46fedf60ed7d79f574Daniel Dunbar size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue(); 12925a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 12935a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return size + evaluateOffsetOf(C, Base); 12945a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson } else if (isa<CompoundLiteralExpr>(E)) 12955a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return 0; 12965a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 12975a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson assert(0 && "Unknown offsetof subexpression!"); 12985a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return 0; 12995a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson} 13005a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 13015a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlssonint64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const 13025a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson{ 13035a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson assert(Opc == OffsetOf && "Unary operator not offsetof!"); 13045a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 130598be4943e8dc4f3905629a7102668960873cf863Chris Lattner unsigned CharSize = C.Target.getCharWidth(); 13065549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize; 13075a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson} 13085a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 13090518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redlvoid SizeOfAlignOfExpr::Destroy(ASTContext& C) { 13100518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // Override default behavior of traversing children. If this has a type 13110518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // operand and the type is a variable-length array, the child iteration 13120518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // will iterate over the size expression. However, this expression belongs 13130518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // to the type, not to this, so we don't want to delete it. 13140518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // We still want to delete this expression. 13150518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // FIXME: Same as in Stmt::Destroy - will be eventually in ASTContext's 13160518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // pool allocator. 13170518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (isArgumentType()) 13180518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl delete this; 13190518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl else 13200518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl Expr::Destroy(C); 13219048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar} 13229048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar 132377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===// 1324ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek// ExprIterator. 1325ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===// 1326ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek 1327ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } 1328ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator*() const { return cast<Expr>(*I); } 1329ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator->() const { return cast<Expr>(*I); } 1330ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator[](size_t idx) const { 1331ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek return cast<Expr>(I[idx]); 1332ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek} 1333ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } 1334ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } 1335ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek 1336ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===// 133777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// Child Iterators for iterating over subexpressions/substatements 133877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===// 133977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 134077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// DeclRefExpr 13419ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } 13429ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } 134377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 13447779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff// ObjCIvarRefExpr 13455549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; } 13465549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } 13477779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff 1348e3e9add4fd788927df6f545570e7838db59c01d7Steve Naroff// ObjCPropertyRefExpr 13495549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; } 13505549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } 1351ae7840776d6cd31b4d7a4a345b61bcbb3744df6cSteve Naroff 1352cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas Gregor// ObjCSuperExpr 1353cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas GregorStmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); } 1354cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas GregorStmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); } 1355cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas Gregor 1356d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner// PredefinedExpr 1357d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } 1358d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } 135977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 136077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// IntegerLiteral 13619ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } 13629ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } 136377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 136477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CharacterLiteral 13659ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); } 13669ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } 136777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 136877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// FloatingLiteral 13699ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } 13709ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } 137177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 13725d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner// ImaginaryLiteral 13735549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; } 13745549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; } 13755d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner 137677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// StringLiteral 13779ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } 13789ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_end() { return child_iterator(); } 137977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 138077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ParenExpr 13815549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_begin() { return &Val; } 13825549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_end() { return &Val+1; } 138377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 138477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// UnaryOperator 13855549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_begin() { return &Val; } 13865549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_end() { return &Val+1; } 138777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 13880518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl// SizeOfAlignOfExpr 13890518999d3adcc289997bd974dce90cc97f5c1c44Sebastian RedlStmt::child_iterator SizeOfAlignOfExpr::child_begin() { 13900518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // If this is of a type and the type is a VLA type (and not a typedef), the 13910518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // size expression of the VLA needs to be treated as an executable expression. 13920518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl // Why isn't this weirdness documented better in StmtIterator? 13930518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (isArgumentType()) { 13940518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (VariableArrayType* T = dyn_cast<VariableArrayType>( 13950518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl getArgumentType().getTypePtr())) 13960518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl return child_iterator(T); 13970518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl return child_iterator(); 13980518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl } 13990518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl return child_iterator((Stmt**)&Argument); 14009ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 14010518999d3adcc289997bd974dce90cc97f5c1c44Sebastian RedlStmt::child_iterator SizeOfAlignOfExpr::child_end() { 14020518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl if (isArgumentType()) 14030518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl return child_iterator(); 14040518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl return child_iterator((Stmt**)&Argument + 1); 14059ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 140677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 140777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ArraySubscriptExpr 14081237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_begin() { 14095549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 141077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 14111237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_end() { 14125549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+END_EXPR; 141377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 141477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 141577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CallExpr 14161237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_begin() { 14175549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 141877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 14191237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_end() { 14205549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+NumArgs+ARGS_START; 142177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 14221237c673c07f9d827129ba02720108816abde562Ted Kremenek 14231237c673c07f9d827129ba02720108816abde562Ted Kremenek// MemberExpr 14245549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_begin() { return &Base; } 14255549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_end() { return &Base+1; } 14261237c673c07f9d827129ba02720108816abde562Ted Kremenek 1427213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman// ExtVectorElementExpr 14285549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; } 14295549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; } 14301237c673c07f9d827129ba02720108816abde562Ted Kremenek 14311237c673c07f9d827129ba02720108816abde562Ted Kremenek// CompoundLiteralExpr 14325549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; } 14335549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; } 14341237c673c07f9d827129ba02720108816abde562Ted Kremenek 14351237c673c07f9d827129ba02720108816abde562Ted Kremenek// CastExpr 14365549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_begin() { return &Op; } 14375549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_end() { return &Op+1; } 14381237c673c07f9d827129ba02720108816abde562Ted Kremenek 14391237c673c07f9d827129ba02720108816abde562Ted Kremenek// BinaryOperator 14401237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_begin() { 14415549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 14421237c673c07f9d827129ba02720108816abde562Ted Kremenek} 14431237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_end() { 14445549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+END_EXPR; 14451237c673c07f9d827129ba02720108816abde562Ted Kremenek} 14461237c673c07f9d827129ba02720108816abde562Ted Kremenek 14471237c673c07f9d827129ba02720108816abde562Ted Kremenek// ConditionalOperator 14481237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_begin() { 14495549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 14501237c673c07f9d827129ba02720108816abde562Ted Kremenek} 14511237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_end() { 14525549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+END_EXPR; 14531237c673c07f9d827129ba02720108816abde562Ted Kremenek} 14541237c673c07f9d827129ba02720108816abde562Ted Kremenek 14551237c673c07f9d827129ba02720108816abde562Ted Kremenek// AddrLabelExpr 14569ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } 14579ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } 14581237c673c07f9d827129ba02720108816abde562Ted Kremenek 14591237c673c07f9d827129ba02720108816abde562Ted Kremenek// StmtExpr 14605549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_begin() { return &SubStmt; } 14615549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; } 14621237c673c07f9d827129ba02720108816abde562Ted Kremenek 14631237c673c07f9d827129ba02720108816abde562Ted Kremenek// TypesCompatibleExpr 14649ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_begin() { 14659ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 14669ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 14679ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek 14689ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_end() { 14699ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 14709ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 14711237c673c07f9d827129ba02720108816abde562Ted Kremenek 14721237c673c07f9d827129ba02720108816abde562Ted Kremenek// ChooseExpr 14735549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; } 14745549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; } 14751237c673c07f9d827129ba02720108816abde562Ted Kremenek 1476e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman// OverloadExpr 14775549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; } 14785549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; } 1479e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman 1480d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman// ShuffleVectorExpr 1481d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_begin() { 14825549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 1483d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman} 1484d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_end() { 14855549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+NumExprs; 1486d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman} 1487d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman 14887c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson// VAArgExpr 14895549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_begin() { return &Val; } 14905549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_end() { return &Val+1; } 14917c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson 149266b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson// InitListExpr 149366b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonStmt::child_iterator InitListExpr::child_begin() { 14945549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return InitExprs.size() ? &InitExprs[0] : 0; 149566b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson} 149666b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonStmt::child_iterator InitListExpr::child_end() { 14975549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0; 149866b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson} 149966b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson 15001237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCStringLiteral 15019ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_begin() { 15029ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 15039ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 15049ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_end() { 15059ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 15069ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 15071237c673c07f9d827129ba02720108816abde562Ted Kremenek 15081237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCEncodeExpr 15099ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } 15109ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } 15111237c673c07f9d827129ba02720108816abde562Ted Kremenek 1512b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian// ObjCSelectorExpr 15139ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_begin() { 15149ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 15159ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 15169ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_end() { 15179ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 15189ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 1519b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian 1520390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian// ObjCProtocolExpr 15219ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_begin() { 15229ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 15239ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 15249ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_end() { 15259ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 15269ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 1527390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian 1528563477da25f824e37c535131695dc4dc9b68c465Steve Naroff// ObjCMessageExpr 1529ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted KremenekStmt::child_iterator ObjCMessageExpr::child_begin() { 15305549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START; 1531563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1532563477da25f824e37c535131695dc4dc9b68c465Steve NaroffStmt::child_iterator ObjCMessageExpr::child_end() { 15335549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+ARGS_START+getNumArgs(); 1534563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1535563477da25f824e37c535131695dc4dc9b68c465Steve Naroff 15364eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff// Blocks 153756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_begin() { return child_iterator(); } 153856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_end() { return child_iterator(); } 15394eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff 15409da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();} 15419da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); } 1542