Expr.cpp revision 98cd599ee8a9b259ed7388ee2921a20d97658864
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 SizeOf: return "sizeof"; 965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case AlignOf: return "alignof"; 975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Extension: return "__extension__"; 9873d0d4fac161ed12926e010dcf8b448a8de6a2ecChris Lattner case OffsetOf: return "__builtin_offsetof"; 995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Postfix Operators. 1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 106e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman 1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerCallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, 1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation rparenloc) 10977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek : Expr(CallExprClass, t), NumArgs(numargs) { 1105549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek SubExprs = new Stmt*[numargs+1]; 11177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek SubExprs[FN] = fn; 1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0; i != numargs; ++i) 11377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek SubExprs[i+ARGS_START] = args[i]; 1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer RParenLoc = rparenloc; 1155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 117d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// setNumArgs - This changes the number of arguments present in this call. 118d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// Any orphaned expressions are deleted by this, and any new operands are set 119d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// to null. 120d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattnervoid CallExpr::setNumArgs(unsigned NumArgs) { 121d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // No change, just return. 122d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner if (NumArgs == getNumArgs()) return; 123d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 124d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // If shrinking # arguments, just delete the extras and forgot them. 125d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner if (NumArgs < getNumArgs()) { 126d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i) 127d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner delete getArg(i); 128d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner this->NumArgs = NumArgs; 129d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner return; 130d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner } 131d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 132d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // Otherwise, we are growing the # arguments. New an bigger argument array. 1335549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek Stmt **NewSubExprs = new Stmt*[NumArgs+1]; 134d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // Copy over args. 135d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i) 136d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner NewSubExprs[i] = SubExprs[i]; 137d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner // Null out new args. 138d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i) 139d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner NewSubExprs[i] = 0; 140d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 141d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner delete[] SubExprs; 142d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner SubExprs = NewSubExprs; 143d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner this->NumArgs = NumArgs; 144d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner} 145d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner 146cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If 147cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// not, return 0. 148cb888967400a03504c88acedd5248d6778a82f46Chris Lattnerunsigned CallExpr::isBuiltinCall() const { 149c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff // All simple function calls (e.g. func()) are implicitly cast to pointer to 150c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff // function. As a result, we try and obtain the DeclRefExpr from the 151c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff // ImplicitCastExpr. 152c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); 153c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). 154cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return 0; 155cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 156c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); 157c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff if (!DRE) 158cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return 0; 159cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 160bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); 161bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson if (!FDecl) 162cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return 0; 163cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 164cb888967400a03504c88acedd5248d6778a82f46Chris Lattner return FDecl->getIdentifier()->getBuiltinID(); 165cb888967400a03504c88acedd5248d6778a82f46Chris Lattner} 166bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson 167cb888967400a03504c88acedd5248d6778a82f46Chris Lattner 1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it 1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "<<=". 1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *BinaryOperator::getOpcodeStr(Opcode Op) { 1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Op) { 1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: assert(0 && "Unknown binary operator"); 1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Mul: return "*"; 1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Div: return "/"; 1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Rem: return "%"; 1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Add: return "+"; 1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Sub: return "-"; 1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Shl: return "<<"; 1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Shr: return ">>"; 1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LT: return "<"; 1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case GT: return ">"; 1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LE: return "<="; 1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case GE: return ">="; 1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case EQ: return "=="; 1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case NE: return "!="; 1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case And: return "&"; 1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Xor: return "^"; 1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Or: return "|"; 1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LAnd: return "&&"; 1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LOr: return "||"; 1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Assign: return "="; 1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case MulAssign: return "*="; 1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case DivAssign: return "/="; 1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case RemAssign: return "%="; 1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case AddAssign: return "+="; 1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case SubAssign: return "-="; 1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ShlAssign: return "<<="; 1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ShrAssign: return ">>="; 1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case AndAssign: return "&="; 2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case XorAssign: return "^="; 2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case OrAssign: return "|="; 2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Comma: return ","; 2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20666b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonInitListExpr::InitListExpr(SourceLocation lbraceloc, 20766b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson Expr **initexprs, unsigned numinits, 20866b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson SourceLocation rbraceloc) 209c5ae899b4bbf65488445316c63168079177db0edSteve Naroff : Expr(InitListExprClass, QualType()), 210c5ae899b4bbf65488445316c63168079177db0edSteve Naroff LBraceLoc(lbraceloc), RBraceLoc(rbraceloc) 21166b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson{ 21266b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson for (unsigned i = 0; i != numinits; i++) 213c5ae899b4bbf65488445316c63168079177db0edSteve Naroff InitExprs.push_back(initexprs[i]); 21466b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson} 2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 216bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block. 2174eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff/// 2184eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroffconst FunctionType *BlockExpr::getFunctionType() const { 2194eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff return getType()->getAsBlockPointerType()-> 2204eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff getPointeeType()->getAsFunctionType(); 2214eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff} 2224eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff 22356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffSourceLocation BlockExpr::getCaretLocation() const { 22456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff return TheBlock->getCaretLocation(); 22556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff} 22656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroffconst Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); } 22756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt *BlockExpr::getBody() { return TheBlock->getBody(); } 22856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff 22956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff 2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines 2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// hasLocalSideEffect - Return true if this immediate expression has side 2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// effects, not counting any sub-expressions. 2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Expr::hasLocalSideEffect() const { 2375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (getStmtClass()) { 2385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ParenExprClass: 2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect(); 2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperatorClass: { 2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const UnaryOperator *UO = cast<UnaryOperator>(this); 2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (UO->getOpcode()) { 2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: return false; 2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PostInc: 2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PostDec: 2495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PreInc: 2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::PreDec: 2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; // ++/-- 2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Deref: 2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Dereferencing a volatile pointer is a side-effect. 2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return getType().isVolatileQualified(); 2565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Real: 2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Imag: 2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // accessing a piece of a volatile complex is a side-effect. 2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return UO->getSubExpr()->getType().isVolatileQualified(); 2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Extension: 2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return UO->getSubExpr()->hasLocalSideEffect(); 2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 265e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner case BinaryOperatorClass: { 266e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner const BinaryOperator *BinOp = cast<BinaryOperator>(this); 267e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner // Consider comma to have side effects if the LHS and RHS both do. 268e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner if (BinOp->getOpcode() == BinaryOperator::Comma) 269e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner return BinOp->getLHS()->hasLocalSideEffect() && 270e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner BinOp->getRHS()->hasLocalSideEffect(); 271e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner 272e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner return BinOp->isAssignmentOp(); 273e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner } 274eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner case CompoundAssignOperatorClass: 2751f683e9cf2e855b32d3fb4b142aa121cd3cf1088Chris Lattner return true; 2765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 277ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian case ConditionalOperatorClass: { 278ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian const ConditionalOperator *Exp = cast<ConditionalOperator>(this); 279ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian return Exp->getCond()->hasLocalSideEffect() 280ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect()) 281ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect()); 282ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian } 283ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian 2845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case MemberExprClass: 2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ArraySubscriptExprClass: 2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the base pointer or element is to a volatile pointer/field, accessing 2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // if is a side effect. 2885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return getType().isVolatileQualified(); 289211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman 2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case CallExprClass: 2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }" 2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // should warn. 2935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 294a9c01021724b9b546d282b8609cbe559734812ecChris Lattner case ObjCMessageExprClass: 295a9c01021724b9b546d282b8609cbe559734812ecChris Lattner return true; 296611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner case StmtExprClass: { 297611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // Statement exprs don't logically have side effects themselves, but are 298611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // sometimes used in macros in ways that give them a type that is unused. 299611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // For example ({ blah; foo(); }) will end up with a type if foo has a type. 300611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // however, if the result of the stmt expr is dead, we don't want to emit a 301611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner // warning. 302611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); 303611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner if (!CS->body_empty()) 304611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner if (const Expr *E = dyn_cast<Expr>(CS->body_back())) 305611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner return E->hasLocalSideEffect(); 306611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner return false; 307611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner } 3080835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis case ExplicitCastExprClass: 309987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case CXXFunctionalCastExprClass: 3105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a cast to void, check the operand. Otherwise, the result of 3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the cast is unused. 3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getType()->isVoidType()) 3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect(); 3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 31504421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner 3164be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman case ImplicitCastExprClass: 3174be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman // Check the operand, since implicit casts are inserted by Sema 3184be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect(); 3194be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman 32004421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 32104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect(); 3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an 3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// incomplete type other than void. Nonarray expressions that can be lvalues: 3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - name, where name must be a variable 3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - e[i] 3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - (e), where e must be an lvalue 3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - e.name, where e must be an lvalue 3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - e->name 3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - *e, the type of e cannot be a function type 3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - string-constant 3347da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension] 33508ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling/// - reference type [C++ [expr]] 3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 33728be73f74c9e241a23ea24fe5756623de6bf1084Chris LattnerExpr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { 33898cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor // first, check the type (C99 6.3.2.1). Expressions with function 33998cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor // type in C are not lvalues, but they can be lvalues in C++. 34098cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) 3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_NotObjectType; 3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 343acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff // Allow qualified void which is an incomplete type other than void (yuck). 34428be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers()) 345acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff return LV_IncompleteVoidType; 346acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff 34798cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor /// FIXME: Expressions can't have reference type, so the following 34898cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor /// isn't needed. 349cb4f9a6d0be445d84af35ba81323be6127816f9eChris Lattner if (TR->isReferenceType()) // C++ [expr] 35008ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling return LV_Valid; 35108ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling 3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the type looks fine, now check the expression 3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (getStmtClass()) { 3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case StringLiteralClass: // C99 6.5.1p4 3557323a6297edad643c202594dcf3d9a174de96ca6Anders Carlsson return LV_Valid; 3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) 3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // For vectors, make sure base is an lvalue (i.e. not a function call). 3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) 35928be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx); 3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_Valid; 3614111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner case DeclRefExprClass: { // C99 6.5.1p2 3624111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner const Decl *RefdDecl = cast<DeclRefExpr>(this)->getDecl(); 36398cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor if (isa<VarDecl>(RefdDecl) || 36498cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor isa<ImplicitParamDecl>(RefdDecl) || 36598cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor // C++ 3.10p2: An lvalue refers to an object or function. 36698cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor isa<FunctionDecl>(RefdDecl) || 36798cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor isa<OverloadedFunctionDecl>(RefdDecl)) 3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_Valid; 3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3704111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner } 371dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff case BlockDeclRefExprClass: { 372dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); 3734f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff if (isa<VarDecl>(BDR->getDecl())) 374dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff return LV_Valid; 375dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff break; 376dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff } 377fdd75663fffeb2058a7847975e50837e61200593Anton Korobeynikov case MemberExprClass: { // C99 6.5.2.3p4 3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const MemberExpr *m = cast<MemberExpr>(this); 37928be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); 380fdd75663fffeb2058a7847975e50837e61200593Anton Korobeynikov } 3817da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner case UnaryOperatorClass: 3825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) 3837da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner return LV_Valid; // C99 6.5.3p4 3847da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner 3857da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real || 386baf0d6678418e0dd9309438c3e50274253cfc7b2Chris Lattner cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag || 387baf0d6678418e0dd9309438c3e50274253cfc7b2Chris Lattner cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension) 38828be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU. 3895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ParenExprClass: // C99 6.5.1p5 39128be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx); 392e6386394677ed4f77b20e2e3d5446a3a2f628e53Steve Naroff case CompoundLiteralExprClass: // C99 6.5.2.5p5 393e6386394677ed4f77b20e2e3d5446a3a2f628e53Steve Naroff return LV_Valid; 394213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman case ExtVectorElementExprClass: 395213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements()) 396fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return LV_DuplicateVectorComponents; 397fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return LV_Valid; 398027282d1c1ac151aa7b1b3b45babc918b8ad456aSteve Naroff case ObjCIvarRefExprClass: // ObjC instance variables are lvalues. 399027282d1c1ac151aa7b1b3b45babc918b8ad456aSteve Naroff return LV_Valid; 400799a6a6850af625946bb8d88ca960bb6604e3858Steve Naroff case ObjCPropertyRefExprClass: // FIXME: check if read-only property. 401799a6a6850af625946bb8d88ca960bb6604e3858Steve Naroff return LV_Valid; 402d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner case PredefinedExprClass: 403d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner return (cast<PredefinedExpr>(this)->getIdentType() 404d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner == PredefinedExpr::CXXThis 4057c4a191d8be7eac565a64369de072b2a52bf6749Chris Lattner ? LV_InvalidExpression : LV_Valid); 40604421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 40728be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx); 40824b41fa8239c63b9eb570d3e83c4a82840656a65Argyrios Kyrtzidis case CXXConditionDeclExprClass: 40924b41fa8239c63b9eb570d3e83c4a82840656a65Argyrios Kyrtzidis return LV_Valid; 4105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 4115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LV_InvalidExpression; 4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, 4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// does not have an incomplete type, does not have a const-qualified type, and 4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// if it is a structure or union, does not have any member (including, 4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// recursively, any member or element of all contained aggregates or unions) 4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// with a const-qualified type. 42128be73f74c9e241a23ea24fe5756623de6bf1084Chris LattnerExpr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const { 42228be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner isLvalueResult lvalResult = isLvalue(Ctx); 4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (lvalResult) { 4255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LV_Valid: break; 4265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LV_NotObjectType: return MLV_NotObjectType; 4275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LV_IncompleteVoidType: return MLV_IncompleteVoidType; 428fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; 4295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case LV_InvalidExpression: return MLV_InvalidExpression; 4305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 431c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 432c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType CT = Ctx.getCanonicalType(getType()); 433c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 434c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (CT.isConstQualified()) 4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_ConstQualified; 436c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (CT->isArrayType()) 4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_ArrayType; 438c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (CT->isIncompleteType()) 4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_IncompleteType; 4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 441c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const RecordType *r = CT->getAsRecordType()) { 4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (r->hasConstFields()) 4435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_ConstQualified; 4445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4454f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // The following is illegal: 4464f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // void takeclosure(void (^C)(void)); 4474f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // void func() { int x = 1; takeclosure(^{ x = 7 }); } 4484f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff // 4494f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff if (getStmtClass() == BlockDeclRefExprClass) { 4504f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); 4514f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl())) 4524f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff return MLV_NotBlockQualified; 4534f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff } 4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return MLV_Valid; 4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4572e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek/// hasGlobalStorage - Return true if this expression has static storage 4584cc627111453b75519d5130b57e06256da7b00e8Chris Lattner/// duration. This means that the address of this expression is a link-time 4594cc627111453b75519d5130b57e06256da7b00e8Chris Lattner/// constant. 4602e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenekbool Expr::hasGlobalStorage() const { 4611d09ecca892185ba067e47ba879f424de59950efChris Lattner switch (getStmtClass()) { 4621d09ecca892185ba067e47ba879f424de59950efChris Lattner default: 4631d09ecca892185ba067e47ba879f424de59950efChris Lattner return false; 4644cc627111453b75519d5130b57e06256da7b00e8Chris Lattner case ParenExprClass: 4652e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage(); 4664cc627111453b75519d5130b57e06256da7b00e8Chris Lattner case ImplicitCastExprClass: 4672e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage(); 468e9b12198c4cc7f5687960100351b4af006c14469Steve Naroff case CompoundLiteralExprClass: 469e9b12198c4cc7f5687960100351b4af006c14469Steve Naroff return cast<CompoundLiteralExpr>(this)->isFileScope(); 4701d09ecca892185ba067e47ba879f424de59950efChris Lattner case DeclRefExprClass: { 4711d09ecca892185ba067e47ba879f424de59950efChris Lattner const Decl *D = cast<DeclRefExpr>(this)->getDecl(); 4721d09ecca892185ba067e47ba879f424de59950efChris Lattner if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 4732e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return VD->hasGlobalStorage(); 47463f067f5a75ae97b53dc7a6a6530e2c72c8bb7f8Seo Sanghyeon if (isa<FunctionDecl>(D)) 47563f067f5a75ae97b53dc7a6a6530e2c72c8bb7f8Seo Sanghyeon return true; 4761d09ecca892185ba067e47ba879f424de59950efChris Lattner return false; 4771d09ecca892185ba067e47ba879f424de59950efChris Lattner } 478fb7080663a0f0abd687a6f58ac917d9a76318b73Chris Lattner case MemberExprClass: { 4791d09ecca892185ba067e47ba879f424de59950efChris Lattner const MemberExpr *M = cast<MemberExpr>(this); 4802e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return !M->isArrow() && M->getBase()->hasGlobalStorage(); 481fb7080663a0f0abd687a6f58ac917d9a76318b73Chris Lattner } 4824cc627111453b75519d5130b57e06256da7b00e8Chris Lattner case ArraySubscriptExprClass: 4832e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage(); 484d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner case PredefinedExprClass: 485fa28b30d5a1e93e5263be33e343532b900d2c643Chris Lattner return true; 48604421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 48704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage(); 4881d09ecca892185ba067e47ba879f424de59950efChris Lattner } 4891d09ecca892185ba067e47ba879f424de59950efChris Lattner} 4901d09ecca892185ba067e47ba879f424de59950efChris Lattner 4914e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() { 4924e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek Expr* E = this; 4934e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek while (ParenExpr* P = dyn_cast<ParenExpr>(E)) 4944e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek E = P->getSubExpr(); 4954e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek 4964e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek return E; 4974e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek} 4984e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek 49956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr 50056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand. 50156f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() { 50256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner Expr *E = this; 50356f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner while (true) { 50456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner if (ParenExpr *P = dyn_cast<ParenExpr>(E)) 50556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner E = P->getSubExpr(); 50656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner else if (CastExpr *P = dyn_cast<CastExpr>(E)) 50756f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner E = P->getSubExpr(); 50856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner else 50956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner return E; 51056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner } 51156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner} 51256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner 51356f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner 51438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroffbool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { 51538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff switch (getStmtClass()) { 51638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff default: 51738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (Loc) *Loc = getLocStart(); 51838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 51938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case ParenExprClass: 52038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc); 52138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case StringLiteralClass: 5225d37e32a5bfefacdd7b4d0664199d72c703d489dSteve Naroff case ObjCStringLiteralClass: 52338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case FloatingLiteralClass: 52438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case IntegerLiteralClass: 52538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case CharacterLiteralClass: 52638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case ImaginaryLiteralClass: 5271a86b33c1870fee08d281c9f07ac1280195a7faeAnders Carlsson case TypesCompatibleExprClass: 5281a86b33c1870fee08d281c9f07ac1280195a7faeAnders Carlsson case CXXBoolLiteralExprClass: 52915425f9d2d92e2a5f99b09b8e2fc00025631e593Anders Carlsson case AddrLabelExprClass: 5302777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 53138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case CallExprClass: { 53238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff const CallExpr *CE = cast<CallExpr>(this); 53345b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner 53445b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner // Allow any constant foldable calls to builtins. 53545b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner if (CE->isBuiltinCall() && CE->isEvaluatable(Ctx)) 536c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff return true; 53745b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner 53838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (Loc) *Loc = getLocStart(); 53938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 54038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 5414ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner case DeclRefExprClass: { 5424ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner const Decl *D = cast<DeclRefExpr>(this)->getDecl(); 5434ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner // Accept address of function. 5444ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D)) 5452777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 54638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (Loc) *Loc = getLocStart(); 5471d09ecca892185ba067e47ba879f424de59950efChris Lattner if (isa<VarDecl>(D)) 5481d09ecca892185ba067e47ba879f424de59950efChris Lattner return TR->isArrayType(); 54938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 5504ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner } 551b8f13a8baff3df080fe024fec47746e9a79fe649Steve Naroff case CompoundLiteralExprClass: 552b8f13a8baff3df080fe024fec47746e9a79fe649Steve Naroff if (Loc) *Loc = getLocStart(); 553b8f13a8baff3df080fe024fec47746e9a79fe649Steve Naroff // Allow "(int []){2,4}", since the array will be converted to a pointer. 554d47d4f518e43eaa716e2d18359bd725a03b87f58Nate Begeman // Allow "(vector type){2,4}" since the elements are all constant. 555d47d4f518e43eaa716e2d18359bd725a03b87f58Nate Begeman return TR->isArrayType() || TR->isVectorType(); 55638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperatorClass: { 55738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff const UnaryOperator *Exp = cast<UnaryOperator>(this); 55838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 5591d09ecca892185ba067e47ba879f424de59950efChris Lattner // C99 6.6p9 560239c15e5b07a6cc84278ede87edf638922767520Chris Lattner if (Exp->getOpcode() == UnaryOperator::AddrOf) { 5612e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek if (!Exp->getSubExpr()->hasGlobalStorage()) { 562239c15e5b07a6cc84278ede87edf638922767520Chris Lattner if (Loc) *Loc = getLocStart(); 563239c15e5b07a6cc84278ede87edf638922767520Chris Lattner return false; 564239c15e5b07a6cc84278ede87edf638922767520Chris Lattner } 565239c15e5b07a6cc84278ede87edf638922767520Chris Lattner return true; 566239c15e5b07a6cc84278ede87edf638922767520Chris Lattner } 5671d09ecca892185ba067e47ba879f424de59950efChris Lattner 56838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // Get the operand value. If this is sizeof/alignof, do not evalute the 56938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // operand. This affects C99 6.6p3. 570d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff if (!Exp->isSizeOfAlignOfOp() && 571d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff Exp->getOpcode() != UnaryOperator::OffsetOf && 57238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff !Exp->getSubExpr()->isConstantExpr(Ctx, Loc)) 57338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 57438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 57538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff switch (Exp->getOpcode()) { 57638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. 57738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // See C99 6.6p3. 57838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff default: 57938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (Loc) *Loc = Exp->getOperatorLoc(); 58038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 58138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::Extension: 58238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return true; // FIXME: this is wrong. 58338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::SizeOf: 58438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::AlignOf: 585d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff case UnaryOperator::OffsetOf: 58638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. 5873c2b3170041f69a92904e3bab9b6d654eaf260acEli Friedman if (!Exp->getSubExpr()->getType()->isConstantSizeType()) { 58865383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner if (Loc) *Loc = Exp->getOperatorLoc(); 58938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 59065383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner } 5912777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 59238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::LNot: 59338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::Plus: 59438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::Minus: 59538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case UnaryOperator::Not: 5962777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 59738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 59838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 59938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case SizeOfAlignOfTypeExprClass: { 60038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this); 60138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // alignof always evaluates to a constant. 602a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() && 603a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner !Exp->getArgumentType()->isConstantSizeType()) { 60465383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner if (Loc) *Loc = Exp->getOperatorLoc(); 60538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 60665383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner } 6072777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 60838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 60938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case BinaryOperatorClass: { 61038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff const BinaryOperator *Exp = cast<BinaryOperator>(this); 61138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 61238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff // The LHS of a constant expr is always evaluated and needed. 61338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (!Exp->getLHS()->isConstantExpr(Ctx, Loc)) 61438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 61538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 61638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (!Exp->getRHS()->isConstantExpr(Ctx, Loc)) 61738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 6182777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 61938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 62038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case ImplicitCastExprClass: 621987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case ExplicitCastExprClass: 622987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case CXXFunctionalCastExprClass: { 6230835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr(); 6240835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis SourceLocation CastLoc = getLocStart(); 62538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (!SubExpr->isConstantExpr(Ctx, Loc)) { 62638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff if (Loc) *Loc = SubExpr->getLocStart(); 62738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 62838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 6292777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 63038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 63138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff case ConditionalOperatorClass: { 63238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff const ConditionalOperator *Exp = cast<ConditionalOperator>(this); 6332777e494c96bee69141a10a0174d5da16e7b927bChris Lattner if (!Exp->getCond()->isConstantExpr(Ctx, Loc) || 6343907323dd6665c0c4e383435cb145233f4533406Anders Carlsson // Handle the GNU extension for missing LHS. 6353907323dd6665c0c4e383435cb145233f4533406Anders Carlsson !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) || 6362777e494c96bee69141a10a0174d5da16e7b927bChris Lattner !Exp->getRHS()->isConstantExpr(Ctx, Loc)) 63738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff return false; 6382777e494c96bee69141a10a0174d5da16e7b927bChris Lattner return true; 63938374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 640d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff case InitListExprClass: { 641d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff const InitListExpr *Exp = cast<InitListExpr>(this); 642d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff unsigned numInits = Exp->getNumInits(); 643d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff for (unsigned i = 0; i < numInits; i++) { 644d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) { 645d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff if (Loc) *Loc = Exp->getInit(i)->getLocStart(); 646d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff return false; 647d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff } 648d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff } 649d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff return true; 650d0091aa3850a8c8be94c68710ed4ef1317729ba0Steve Naroff } 65104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 65204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this)->getExpr()->isConstantExpr(Ctx, Loc); 65338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff } 65438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff} 65538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff 6565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIntegerConstantExpr - this recursive routine will test if an expression is 6575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an integer constant expression. Note: With the introduction of VLA's in 6585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// C99 the result of the sizeof operator is no longer always a constant 6595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expression. The generalization of the wording to include any subexpression 6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions 6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, 6625f6b632391e24b08ead3e62ba2e2765e770382edNuno Lopes/// "0 || f()" can be treated as a constant expression. In C90 this expression, 6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// occurring in a context requiring a constant, would have been a constraint 6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// violation. FIXME: This routine currently implements C90 semantics. 6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// To properly implement C99 semantics this routine will need to evaluate 6665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expressions involving operators previously mentioned. 6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, 6695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// comma, etc 6705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 6715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not 672ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner/// permit this. This includes things like (int)1e1000 673ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// 674ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof 675ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer 676ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// cast+dereference. 677590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattnerbool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, 678590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner SourceLocation *Loc, bool isEvaluated) const { 6795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (getStmtClass()) { 6805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 6815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 6825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 6835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ParenExprClass: 6845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return cast<ParenExpr>(this)->getSubExpr()-> 685590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); 6865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case IntegerLiteralClass: 6875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = cast<IntegerLiteral>(this)->getValue(); 6885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 6892eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner case CharacterLiteralClass: { 6902eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner const CharacterLiteral *CL = cast<CharacterLiteral>(this); 69198be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 6922eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner Result = CL->getValue(); 693f0fbcb356bc4eb5f6467dc4fc4bc447e9a630102Chris Lattner Result.setIsUnsigned(!getType()->isSignedIntegerType()); 6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 6952eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner } 696b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson case CXXBoolLiteralExprClass: { 697b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this); 698b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 699b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson Result = BL->getValue(); 700b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson Result.setIsUnsigned(!getType()->isSignedIntegerType()); 701b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson break; 702b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson } 7037267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis case CXXZeroInitValueExprClass: 7047267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis Result.clear(); 7057267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis break; 7067b658aa315784fa489a32daeacfe400512ce468dSteve Naroff case TypesCompatibleExprClass: { 7077b658aa315784fa489a32daeacfe400512ce468dSteve Naroff const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this); 70898be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 709ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2()); 710389cecc83f33e93c8ba6bf2e8269b8690404416fSteve Naroff break; 7117b658aa315784fa489a32daeacfe400512ce468dSteve Naroff } 71213b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff case CallExprClass: { 71313b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff const CallExpr *CE = cast<CallExpr>(this); 71498be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 715a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner 716a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // If this is a call to a builtin function, constant fold it otherwise 717a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // reject it. 718a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner if (CE->isBuiltinCall()) { 719a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner APValue ResultAP; 720a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner if (CE->tryEvaluate(ResultAP, Ctx)) { 721a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner Result = ResultAP.getInt(); 722a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner break; // It is a constant, expand it. 723a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner } 724a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner } 725a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner 72613b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff if (Loc) *Loc = getLocStart(); 72713b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff return false; 72813b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff } 7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case DeclRefExprClass: 7305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (const EnumConstantDecl *D = 7315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { 7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = D->getInitVal(); 7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 7345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 7375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperatorClass: { 7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const UnaryOperator *Exp = cast<UnaryOperator>(this); 7395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the operand value. If this is sizeof/alignof, do not evalute the 7415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // operand. This affects C99 6.6p3. 7425a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() && 743602dafdea34b78c7bc1f4601f94062afa4139e76Chris Lattner !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated)) 7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 7455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Exp->getOpcode()) { 7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. 7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // See C99 6.6p3. 7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 7505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = Exp->getOperatorLoc(); 7515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Extension: 75376e773a443be9f006610f46529e07d4c8d857680Chris Lattner return true; // FIXME: this is wrong. 7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::SizeOf: 7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::AlignOf: 756a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // Return the result in the right width. 75798be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 758a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 759a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // sizeof(void) and __alignof__(void) = 1 as a gcc extension. 760a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner if (Exp->getSubExpr()->getType()->isVoidType()) { 761a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner Result = 1; 762a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner break; 763a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner } 764a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 7655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. 7663c2b3170041f69a92904e3bab9b6d654eaf260acEli Friedman if (!Exp->getSubExpr()->getType()->isConstantSizeType()) { 76765383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner if (Loc) *Loc = Exp->getOperatorLoc(); 7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 76965383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner } 7705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 77176e773a443be9f006610f46529e07d4c8d857680Chris Lattner // Get information about the size or align. 772efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner if (Exp->getSubExpr()->getType()->isFunctionType()) { 773efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner // GCC extension: sizeof(function) = 1. 774efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1; 775da5a6b6d9fd52899499d5b7b46273ec844dcaa6eChris Lattner } else { 77698be4943e8dc4f3905629a7102668960873cf863Chris Lattner unsigned CharSize = Ctx.Target.getCharWidth(); 77764a31ef8d46fcdf2bca38098e3206a45052b515dAnders Carlsson if (Exp->getOpcode() == UnaryOperator::AlignOf) 77898be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType()) / CharSize; 77964a31ef8d46fcdf2bca38098e3206a45052b515dAnders Carlsson else 78098be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result = Ctx.getTypeSize(Exp->getSubExpr()->getType()) / CharSize; 781da5a6b6d9fd52899499d5b7b46273ec844dcaa6eChris Lattner } 7825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::LNot: { 784bf7553889594886213e05cd3f94a6d6f7747bf7bChris Lattner bool Val = Result == 0; 78598be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = Val; 7875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 7885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Plus: 7905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 7915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Minus: 7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = -Result; 7935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 7945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case UnaryOperator::Not: 7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = ~Result; 7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 7975a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson case UnaryOperator::OffsetOf: 798aa1f9f1d50adf294674b74510b62e863b68572bcDaniel Dunbar Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 7995a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson Result = Exp->evaluateOffsetOf(Ctx); 8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case SizeOfAlignOfTypeExprClass: { 8045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this); 805a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 806a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // Return the result in the right width. 80798be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); 808a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 809a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // sizeof(void) and __alignof__(void) = 1 as a gcc extension. 810a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner if (Exp->getArgumentType()->isVoidType()) { 811a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner Result = 1; 812a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner break; 813a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner } 814a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner 815a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner // alignof always evaluates to a constant, sizeof does if arg is not VLA. 8163c2b3170041f69a92904e3bab9b6d654eaf260acEli Friedman if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) { 81765383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner if (Loc) *Loc = Exp->getOperatorLoc(); 8185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 81965383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner } 8205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 82176e773a443be9f006610f46529e07d4c8d857680Chris Lattner // Get information about the size or align. 822efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner if (Exp->getArgumentType()->isFunctionType()) { 823efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner // GCC extension: sizeof(function) = 1. 824efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner Result = Exp->isSizeOf() ? 1 : 4; 8256a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson } else { 82698be4943e8dc4f3905629a7102668960873cf863Chris Lattner unsigned CharSize = Ctx.Target.getCharWidth(); 8276a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson if (Exp->isSizeOf()) 82898be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result = Ctx.getTypeSize(Exp->getArgumentType()) / CharSize; 8296a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson else 83098be4943e8dc4f3905629a7102668960873cf863Chris Lattner Result = Ctx.getTypeAlign(Exp->getArgumentType()) / CharSize; 831060e470e53b73167114284eb0b8c3e25bb3dad99Ted Kremenek } 8325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperatorClass: { 8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const BinaryOperator *Exp = cast<BinaryOperator>(this); 836e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar llvm::APSInt LHS, RHS; 837e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar 838e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar // Initialize result to have correct signedness and width. 839e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())), 840e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar !getType()->isSignedIntegerType()); 8415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The LHS of a constant expr is always evaluated and needed. 843e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated)) 8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The short-circuiting &&/|| operators don't necessarily evaluate their 8475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // RHS. Make sure to pass isEvaluated down correctly. 8485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Exp->isLogicalOp()) { 8495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool RHSEval; 8505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Exp->getOpcode() == BinaryOperator::LAnd) 851e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar RHSEval = LHS != 0; 8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer else { 8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); 854e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar RHSEval = LHS == 0; 8555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 857590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, 8585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer isEvaluated & RHSEval)) 8595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 861590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) 8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Exp->getOpcode()) { 8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Mul: 870e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS * RHS; 8715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Div: 8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (RHS == 0) { 8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isEvaluated) break; 8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 878e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS / RHS; 8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Rem: 8815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (RHS == 0) { 8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isEvaluated) break; 8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 886e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS % RHS; 8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 888e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Add: Result = LHS + RHS; break; 889e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Sub: Result = LHS - RHS; break; 8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Shl: 891e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS << 892e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); 893e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar break; 8945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Shr: 895e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS >> 896e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); 8975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 898e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::LT: Result = LHS < RHS; break; 899e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::GT: Result = LHS > RHS; break; 900e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::LE: Result = LHS <= RHS; break; 901e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::GE: Result = LHS >= RHS; break; 902e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::EQ: Result = LHS == RHS; break; 903e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::NE: Result = LHS != RHS; break; 904e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::And: Result = LHS & RHS; break; 905e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Xor: Result = LHS ^ RHS; break; 906e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar case BinaryOperator::Or: Result = LHS | RHS; break; 9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::LAnd: 908e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS != 0 && RHS != 0; 9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::LOr: 911e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar Result = LHS != 0 || RHS != 0; 9125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BinaryOperator::Comma: 9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.6p3: "shall not contain assignment, ..., or comma operators, 9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // *except* when they are contained within a subexpression that is not 9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // evaluated". Note that Assignment can never happen due to constraints 9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // on the LHS subexpr, so we don't need to check it here. 9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isEvaluated) { 9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Loc) *Loc = getLocStart(); 9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The result of the constant expr is the RHS. 9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result = RHS; 9265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); 9305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 93226dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner case ImplicitCastExprClass: 933987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case ExplicitCastExprClass: 934987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis case CXXFunctionalCastExprClass: { 9350835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr(); 9360835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis SourceLocation CastLoc = getLocStart(); 93726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner 9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.6p6: shall only convert arithmetic types to integer types. 93926dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (!SubExpr->getType()->isArithmeticType() || 94026dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner !getType()->isIntegerType()) { 94126dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (Loc) *Loc = SubExpr->getLocStart(); 9425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 9435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 944987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner 94598be4943e8dc4f3905629a7102668960873cf863Chris Lattner uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType())); 946987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner 9475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Handle simple integer->integer casts. 94826dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (SubExpr->getType()->isIntegerType()) { 94926dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 9505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 95126dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner 95226dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner // Figure out if this is a truncate, extend or noop cast. 95326dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner // If the input is signed, do a sign extend, noop, or truncate. 954c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner if (getType()->isBooleanType()) { 955c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner // Conversion to bool compares against zero. 956c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result = Result != 0; 957c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result.zextOrTrunc(DestWidth); 958c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner } else if (SubExpr->getType()->isSignedIntegerType()) 95926dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner Result.sextOrTrunc(DestWidth); 96026dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner else // If the input is unsigned, do a zero extend, noop, or truncate. 96126dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner Result.zextOrTrunc(DestWidth); 9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Allow floating constants that are the immediate operands of casts or that 9665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // are parenthesized. 96726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner const Expr *Operand = SubExpr; 9685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) 9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Operand = PE->getSubExpr(); 970987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner 971987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner // If this isn't a floating literal, we can't handle it. 972987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand); 973987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner if (!FL) { 974987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner if (Loc) *Loc = Operand->getLocStart(); 975987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner return false; 976987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner } 977c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner 978c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner // If the destination is boolean, compare against zero. 979c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner if (getType()->isBooleanType()) { 980c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result = !FL->getValue().isZero(); 981c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner Result.zextOrTrunc(DestWidth); 982c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner break; 983c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner } 9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 985987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner // Determine whether we are converting to unsigned or signed. 986987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner bool DestSigned = getType()->isSignedIntegerType(); 987ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner 988ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can 989ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner // be called multiple times per AST. 990ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen uint64_t Space[4]; 991ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen bool ignored; 992ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned, 993ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen llvm::APFloat::rmTowardZero, 994ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen &ignored); 995987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner Result = llvm::APInt(DestWidth, 4, Space); 996987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner break; 9975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ConditionalOperatorClass: { 9995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const ConditionalOperator *Exp = cast<ConditionalOperator>(this); 10005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1001590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 10025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const Expr *TrueExp = Exp->getLHS(); 10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const Expr *FalseExp = Exp->getRHS(); 10065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Result == 0) std::swap(TrueExp, FalseExp); 10075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Evaluate the false one first, discard the result. 10093907323dd6665c0c4e383435cb145233f4533406Anders Carlsson if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false)) 10105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Evalute the true one, capture the result. 10123907323dd6665c0c4e383435cb145233f4533406Anders Carlsson if (TrueExp && 10133907323dd6665c0c4e383435cb145233f4533406Anders Carlsson !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) 10145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 101704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner case CXXDefaultArgExprClass: 101804421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return cast<CXXDefaultArgExpr>(this) 101904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); 10205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Cases that are valid constant exprs fall through to here. 10235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.setIsUnsigned(getType()->isUnsignedIntegerType()); 10245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an 10285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// integer constant expression with the value zero, or if this is one that is 10295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// cast to void*. 1030590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattnerbool Expr::isNullPointerConstant(ASTContext &Ctx) const { 1031aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // Strip off a cast to void*, if it exists. 10320835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { 1033aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // Check that it is a cast to void*. 10344b3f9b367c16e494c181d6f03d53497ae1275fbeEli Friedman if (const PointerType *PT = CE->getType()->getAsPointerType()) { 10355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Pointee = PT->getPointeeType(); 1036f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner if (Pointee.getCVRQualifiers() == 0 && 1037f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner Pointee->isVoidType() && // to void* 1038aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff CE->getSubExpr()->getType()->isIntegerType()) // from int. 1039aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff return CE->getSubExpr()->isNullPointerConstant(Ctx); 10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1041aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { 1042aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // Ignore the ImplicitCastExpr type entirely. 1043aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff return ICE->getSubExpr()->isNullPointerConstant(Ctx); 1044aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { 1045aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // Accept ((void*)0) as a null pointer constant, as many other 1046aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // implementations do. 1047aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff return PE->getSubExpr()->isNullPointerConstant(Ctx); 10488123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner } else if (const CXXDefaultArgExpr *DefaultArg 10498123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner = dyn_cast<CXXDefaultArgExpr>(this)) { 105004421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner // See through default argument expressions 105104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner return DefaultArg->getExpr()->isNullPointerConstant(Ctx); 1052aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff } 1053aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff 1054aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff // This expression must be an integer type. 1055aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff if (!getType()->isIntegerType()) 1056aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff return false; 1057aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff 10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have an integer constant expression, we need to *evaluate* it and 10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // test for the value 0. 10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::APSInt Val(32); 1061aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0; 10625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 106331a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff 1064213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const { 10658a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman if (const VectorType *VT = getType()->getAsVectorType()) 10668a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman return VT->getNumElements(); 10678a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman return 1; 10684d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner} 10694d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner 10708a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated. 1071213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const { 1072fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff const char *compStr = Accessor.getName(); 1073fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff unsigned length = strlen(compStr); 1074fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff 1075fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff for (unsigned i = 0; i < length-1; i++) { 1076fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff const char *s = compStr+i; 1077fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff for (const char c = *s++; *s; s++) 1078fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff if (c == *s) 1079fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return true; 1080fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff } 1081fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff return false; 1082fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff} 1083b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner 10848a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. 10853b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess( 10863b8d116703db8018f855cbb4733ace426422623bNate Begeman llvm::SmallVectorImpl<unsigned> &Elts) const { 1087b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner const char *compStr = Accessor.getName(); 10888a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman 10898a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman bool isHi = !strcmp(compStr, "hi"); 10908a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman bool isLo = !strcmp(compStr, "lo"); 10918a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman bool isEven = !strcmp(compStr, "e"); 10928a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman bool isOdd = !strcmp(compStr, "o"); 10938a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman 10948a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman for (unsigned i = 0, e = getNumElements(); i != e; ++i) { 10958a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman uint64_t Index; 10968a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman 10978a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman if (isHi) 10988a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = e + i; 10998a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else if (isLo) 11008a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = i; 11018a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else if (isEven) 11028a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = 2 * i; 11038a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else if (isOdd) 11048a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = 2 * i + 1; 11058a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman else 11068a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman Index = ExtVectorType::getAccessorIdx(compStr[i]); 1107b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner 11083b8d116703db8018f855cbb4733ace426422623bNate Begeman Elts.push_back(Index); 1109b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner } 11108a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman} 11118a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman 111268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// constructor for instance messages. 1113bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve NaroffObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, 1114a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek QualType retType, ObjCMethodDecl *mproto, 1115db611d556f71f98b66b69514d45958d76e5727abSteve Naroff SourceLocation LBrac, SourceLocation RBrac, 111649f109c786f99eb7468dac3976db083a65493444Steve Naroff Expr **ArgExprs, unsigned nargs) 1117db611d556f71f98b66b69514d45958d76e5727abSteve Naroff : Expr(ObjCMessageExprClass, retType), SelName(selInfo), 1118ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted Kremenek MethodProto(mproto) { 111949f109c786f99eb7468dac3976db083a65493444Steve Naroff NumArgs = nargs; 11205549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek SubExprs = new Stmt*[NumArgs+1]; 112168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff SubExprs[RECEIVER] = receiver; 112249f109c786f99eb7468dac3976db083a65493444Steve Naroff if (NumArgs) { 112349f109c786f99eb7468dac3976db083a65493444Steve Naroff for (unsigned i = 0; i != NumArgs; ++i) 112468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 112568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff } 1126563477da25f824e37c535131695dc4dc9b68c465Steve Naroff LBracloc = LBrac; 1127563477da25f824e37c535131695dc4dc9b68c465Steve Naroff RBracloc = RBrac; 1128563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1129563477da25f824e37c535131695dc4dc9b68c465Steve Naroff 113068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// constructor for class messages. 113168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// FIXME: clsName should be typed to ObjCInterfaceType 1132bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve NaroffObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, 1133a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek QualType retType, ObjCMethodDecl *mproto, 1134db611d556f71f98b66b69514d45958d76e5727abSteve Naroff SourceLocation LBrac, SourceLocation RBrac, 113549f109c786f99eb7468dac3976db083a65493444Steve Naroff Expr **ArgExprs, unsigned nargs) 1136db611d556f71f98b66b69514d45958d76e5727abSteve Naroff : Expr(ObjCMessageExprClass, retType), SelName(selInfo), 1137ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted Kremenek MethodProto(mproto) { 113849f109c786f99eb7468dac3976db083a65493444Steve Naroff NumArgs = nargs; 11395549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek SubExprs = new Stmt*[NumArgs+1]; 11404df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown); 114149f109c786f99eb7468dac3976db083a65493444Steve Naroff if (NumArgs) { 114249f109c786f99eb7468dac3976db083a65493444Steve Naroff for (unsigned i = 0; i != NumArgs; ++i) 114368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 114468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff } 1145563477da25f824e37c535131695dc4dc9b68c465Steve Naroff LBracloc = LBrac; 1146563477da25f824e37c535131695dc4dc9b68c465Steve Naroff RBracloc = RBrac; 1147563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1148563477da25f824e37c535131695dc4dc9b68c465Steve Naroff 11494df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek// constructor for class messages. 11504df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, 11514df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek QualType retType, ObjCMethodDecl *mproto, 11524df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SourceLocation LBrac, SourceLocation RBrac, 11534df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek Expr **ArgExprs, unsigned nargs) 11544df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek: Expr(ObjCMessageExprClass, retType), SelName(selInfo), 11554df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekMethodProto(mproto) { 11564df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek NumArgs = nargs; 11574df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs = new Stmt*[NumArgs+1]; 11584df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown); 11594df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek if (NumArgs) { 11604df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek for (unsigned i = 0; i != NumArgs; ++i) 11614df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); 11624df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek } 11634df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek LBracloc = LBrac; 11644df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek RBracloc = RBrac; 11654df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek} 11664df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek 11674df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const { 11684df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; 11694df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek switch (x & Flags) { 11704df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek default: 11714df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek assert(false && "Invalid ObjCMessageExpr."); 11724df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek case IsInstMeth: 11734df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek return ClassInfo(0, 0); 11744df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek case IsClsMethDeclUnknown: 11754df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek return ClassInfo(0, (IdentifierInfo*) (x & ~Flags)); 11764df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek case IsClsMethDeclKnown: { 11774df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags); 11784df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek return ClassInfo(D, D->getIdentifier()); 11794df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek } 11804df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek } 11814df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek} 11824df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek 118327437caadea35f84d550cd29f024fcf3ea240eecChris Lattnerbool ChooseExpr::isConditionTrue(ASTContext &C) const { 118432442bbc98bafa512fa42d46fedf60ed7d79f574Daniel Dunbar return getCond()->getIntegerConstantExprValue(C) != 0; 118527437caadea35f84d550cd29f024fcf3ea240eecChris Lattner} 118627437caadea35f84d550cd29f024fcf3ea240eecChris Lattner 11875a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlssonstatic int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) 11885a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson{ 11895a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 11905a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson QualType Ty = ME->getBase()->getType(); 11915a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 11925a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson RecordDecl *RD = Ty->getAsRecordType()->getDecl(); 119398be4943e8dc4f3905629a7102668960873cf863Chris Lattner const ASTRecordLayout &RL = C.getASTRecordLayout(RD); 11945a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson FieldDecl *FD = ME->getMemberDecl(); 11955a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 11965a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson // FIXME: This is linear time. 11975a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson unsigned i = 0, e = 0; 11985a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson for (i = 0, e = RD->getNumMembers(); i != e; i++) { 11995a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson if (RD->getMember(i) == FD) 12005a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson break; 12015a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson } 12025a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 12035a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase()); 12045a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { 12055a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson const Expr *Base = ASE->getBase(); 12065a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 120798be4943e8dc4f3905629a7102668960873cf863Chris Lattner int64_t size = C.getTypeSize(ASE->getType()); 120832442bbc98bafa512fa42d46fedf60ed7d79f574Daniel Dunbar size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue(); 12095a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 12105a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return size + evaluateOffsetOf(C, Base); 12115a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson } else if (isa<CompoundLiteralExpr>(E)) 12125a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return 0; 12135a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 12145a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson assert(0 && "Unknown offsetof subexpression!"); 12155a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson return 0; 12165a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson} 12175a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 12185a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlssonint64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const 12195a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson{ 12205a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson assert(Opc == OffsetOf && "Unary operator not offsetof!"); 12215a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 122298be4943e8dc4f3905629a7102668960873cf863Chris Lattner unsigned CharSize = C.Target.getCharWidth(); 12235549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize; 12245a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson} 12255a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson 12269048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbarvoid SizeOfAlignOfTypeExpr::Destroy(ASTContext& C) { 12279048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar // Override default behavior of traversing children. We do not want 12289048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar // to delete the type. 12299048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar} 12309048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar 123177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===// 123277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// Child Iterators for iterating over subexpressions/substatements 123377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===// 123477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 123577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// DeclRefExpr 12369ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } 12379ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } 123877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 12397779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff// ObjCIvarRefExpr 12405549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; } 12415549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } 12427779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff 1243e3e9add4fd788927df6f545570e7838db59c01d7Steve Naroff// ObjCPropertyRefExpr 12445549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; } 12455549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } 1246ae7840776d6cd31b4d7a4a345b61bcbb3744df6cSteve Naroff 1247d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner// PredefinedExpr 1248d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } 1249d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } 125077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 125177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// IntegerLiteral 12529ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } 12539ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } 125477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 125577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CharacterLiteral 12569ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); } 12579ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } 125877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 125977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// FloatingLiteral 12609ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } 12619ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } 126277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 12635d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner// ImaginaryLiteral 12645549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; } 12655549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; } 12665d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner 126777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// StringLiteral 12689ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } 12699ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_end() { return child_iterator(); } 127077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 127177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ParenExpr 12725549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_begin() { return &Val; } 12735549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_end() { return &Val+1; } 127477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 127577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// UnaryOperator 12765549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_begin() { return &Val; } 12775549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_end() { return &Val+1; } 127877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 127977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// SizeOfAlignOfTypeExpr 12809ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() { 1281699e9fbeae34cdd651b8e6308649c8939f090cbdTed Kremenek // If the type is a VLA type (and not a typedef), the size expression of the 1282699e9fbeae34cdd651b8e6308649c8939f090cbdTed Kremenek // VLA needs to be treated as an executable expression. 1283699e9fbeae34cdd651b8e6308649c8939f090cbdTed Kremenek if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr())) 1284699e9fbeae34cdd651b8e6308649c8939f090cbdTed Kremenek return child_iterator(T); 1285699e9fbeae34cdd651b8e6308649c8939f090cbdTed Kremenek else 12868a213de1a86e5f347c6b591f52712c2fd6177cceTed Kremenek return child_iterator(); 12879ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 12889ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator SizeOfAlignOfTypeExpr::child_end() { 12898a213de1a86e5f347c6b591f52712c2fd6177cceTed Kremenek return child_iterator(); 12909ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 129177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 129277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ArraySubscriptExpr 12931237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_begin() { 12945549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 129577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 12961237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_end() { 12975549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+END_EXPR; 129877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 129977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek 130077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CallExpr 13011237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_begin() { 13025549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 130377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 13041237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_end() { 13055549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+NumArgs+ARGS_START; 130677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek} 13071237c673c07f9d827129ba02720108816abde562Ted Kremenek 13081237c673c07f9d827129ba02720108816abde562Ted Kremenek// MemberExpr 13095549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_begin() { return &Base; } 13105549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_end() { return &Base+1; } 13111237c673c07f9d827129ba02720108816abde562Ted Kremenek 1312213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman// ExtVectorElementExpr 13135549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; } 13145549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; } 13151237c673c07f9d827129ba02720108816abde562Ted Kremenek 13161237c673c07f9d827129ba02720108816abde562Ted Kremenek// CompoundLiteralExpr 13175549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; } 13185549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; } 13191237c673c07f9d827129ba02720108816abde562Ted Kremenek 13201237c673c07f9d827129ba02720108816abde562Ted Kremenek// CastExpr 13215549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_begin() { return &Op; } 13225549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_end() { return &Op+1; } 13231237c673c07f9d827129ba02720108816abde562Ted Kremenek 13241237c673c07f9d827129ba02720108816abde562Ted Kremenek// BinaryOperator 13251237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_begin() { 13265549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 13271237c673c07f9d827129ba02720108816abde562Ted Kremenek} 13281237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_end() { 13295549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+END_EXPR; 13301237c673c07f9d827129ba02720108816abde562Ted Kremenek} 13311237c673c07f9d827129ba02720108816abde562Ted Kremenek 13321237c673c07f9d827129ba02720108816abde562Ted Kremenek// ConditionalOperator 13331237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_begin() { 13345549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 13351237c673c07f9d827129ba02720108816abde562Ted Kremenek} 13361237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_end() { 13375549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+END_EXPR; 13381237c673c07f9d827129ba02720108816abde562Ted Kremenek} 13391237c673c07f9d827129ba02720108816abde562Ted Kremenek 13401237c673c07f9d827129ba02720108816abde562Ted Kremenek// AddrLabelExpr 13419ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } 13429ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } 13431237c673c07f9d827129ba02720108816abde562Ted Kremenek 13441237c673c07f9d827129ba02720108816abde562Ted Kremenek// StmtExpr 13455549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_begin() { return &SubStmt; } 13465549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; } 13471237c673c07f9d827129ba02720108816abde562Ted Kremenek 13481237c673c07f9d827129ba02720108816abde562Ted Kremenek// TypesCompatibleExpr 13499ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_begin() { 13509ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 13519ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 13529ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek 13539ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_end() { 13549ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 13559ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 13561237c673c07f9d827129ba02720108816abde562Ted Kremenek 13571237c673c07f9d827129ba02720108816abde562Ted Kremenek// ChooseExpr 13585549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; } 13595549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; } 13601237c673c07f9d827129ba02720108816abde562Ted Kremenek 1361e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman// OverloadExpr 13625549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; } 13635549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; } 1364e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman 1365d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman// ShuffleVectorExpr 1366d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_begin() { 13675549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]; 1368d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman} 1369d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_end() { 13705549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+NumExprs; 1371d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman} 1372d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman 13737c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson// VAArgExpr 13745549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_begin() { return &Val; } 13755549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_end() { return &Val+1; } 13767c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson 137766b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson// InitListExpr 137866b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonStmt::child_iterator InitListExpr::child_begin() { 13795549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return InitExprs.size() ? &InitExprs[0] : 0; 138066b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson} 138166b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonStmt::child_iterator InitListExpr::child_end() { 13825549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0; 138366b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson} 138466b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson 13851237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCStringLiteral 13869ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_begin() { 13879ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 13889ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 13899ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_end() { 13909ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 13919ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 13921237c673c07f9d827129ba02720108816abde562Ted Kremenek 13931237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCEncodeExpr 13949ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } 13959ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } 13961237c673c07f9d827129ba02720108816abde562Ted Kremenek 1397b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian// ObjCSelectorExpr 13989ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_begin() { 13999ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 14009ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 14019ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_end() { 14029ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 14039ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 1404b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian 1405390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian// ObjCProtocolExpr 14069ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_begin() { 14079ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 14089ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 14099ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_end() { 14109ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek return child_iterator(); 14119ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek} 1412390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian 1413563477da25f824e37c535131695dc4dc9b68c465Steve Naroff// ObjCMessageExpr 1414ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted KremenekStmt::child_iterator ObjCMessageExpr::child_begin() { 14155549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START; 1416563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1417563477da25f824e37c535131695dc4dc9b68c465Steve NaroffStmt::child_iterator ObjCMessageExpr::child_end() { 14185549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek return &SubExprs[0]+ARGS_START+getNumArgs(); 1419563477da25f824e37c535131695dc4dc9b68c465Steve Naroff} 1420563477da25f824e37c535131695dc4dc9b68c465Steve Naroff 14214eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff// Blocks 142256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_begin() { return child_iterator(); } 142356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_end() { return child_iterator(); } 14244eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff 14259da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();} 14269da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); } 1427