Expr.cpp revision 668bf91d31265b6ea8c3eb854ba450857701f269
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"
19aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/RecordLayout.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/StmtVisitor.h"
22da5a6b6d9fd52899499d5b7b46273ec844dcaa6eChris Lattner#include "clang/Basic/TargetInfo.h"
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Expressions.
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
29da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// getValueAsApproximateDouble - This returns the value as an inaccurate
30da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// double.  Note that this may cause loss of precision, but is useful for
31da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// debugging dumps, etc.
32da8249e57f3badecf925571881fe57243935c6c1Chris Lattnerdouble FloatingLiteral::getValueAsApproximateDouble() const {
33da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  llvm::APFloat V = getValue();
34ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  bool ignored;
35ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen            &ignored);
37da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  return V.convertToDouble();
38da8249e57f3badecf925571881fe57243935c6c1Chris Lattner}
39da8249e57f3badecf925571881fe57243935c6c1Chris Lattner
40da8249e57f3badecf925571881fe57243935c6c1Chris Lattner
416e94ef5696cfb005d3fc7bbac8dcf7690b64f0a5Ted KremenekStringLiteral::StringLiteral(ASTContext& C, const char *strData,
426e94ef5696cfb005d3fc7bbac8dcf7690b64f0a5Ted Kremenek                             unsigned byteLength, bool Wide, QualType t,
436e94ef5696cfb005d3fc7bbac8dcf7690b64f0a5Ted Kremenek                             SourceLocation firstLoc,
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                             SourceLocation lastLoc) :
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Expr(StringLiteralClass, t) {
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // OPTIMIZE: could allocate this appended to the StringLiteral.
476e94ef5696cfb005d3fc7bbac8dcf7690b64f0a5Ted Kremenek  char *AStrData = new (C, 1) char[byteLength];
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  memcpy(AStrData, strData, byteLength);
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  StrData = AStrData;
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ByteLength = byteLength;
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IsWide = Wide;
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  firstTokLoc = firstLoc;
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  lastTokLoc = lastLoc;
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
566e94ef5696cfb005d3fc7bbac8dcf7690b64f0a5Ted Kremenekvoid StringLiteral::Destroy(ASTContext &C) {
578189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  C.Deallocate(const_cast<char*>(StrData));
58353ffceafc6bcebd5592cb9d93ea3f9242e5370aTed Kremenek  this->~StringLiteral();
59353ffceafc6bcebd5592cb9d93ea3f9242e5370aTed Kremenek  C.Deallocate(this);
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool UnaryOperator::isPostfix(Opcode Op) {
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case PostInc:
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case PostDec:
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return true;
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
725a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenekbool UnaryOperator::isPrefix(Opcode Op) {
735a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek  switch (Op) {
745a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek    case PreInc:
755a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek    case PreDec:
765a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek      return true;
775a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek    default:
785a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek      return false;
795a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek  }
805a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek}
815a56ac30d04e8f0431a08980885662a47a6308aaTed Kremenek
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "sizeof" or "[pre]++".
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *UnaryOperator::getOpcodeStr(Opcode Op) {
855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown unary operator");
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case PostInc: return "++";
885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case PostDec: return "--";
895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case PreInc:  return "++";
905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case PreDec:  return "--";
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case AddrOf:  return "&";
925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Deref:   return "*";
935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Plus:    return "+";
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Minus:   return "-";
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Not:     return "~";
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LNot:    return "!";
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Real:    return "__real";
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Imag:    return "__imag";
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Extension: return "__extension__";
10073d0d4fac161ed12926e010dcf8b448a8de6a2ecChris Lattner  case OffsetOf: return "__builtin_offsetof";
1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Postfix Operators.
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
108668bf91d31265b6ea8c3eb854ba450857701f269Ted KremenekCallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
1098189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                   unsigned numargs, QualType t, SourceLocation rparenloc)
110898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  : Expr(SC, t,
111898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
112898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
113898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    NumArgs(numargs) {
114668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
115668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  SubExprs = new (C) Stmt*[numargs+1];
116b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  SubExprs[FN] = fn;
117b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  for (unsigned i = 0; i != numargs; ++i)
118b4609806e9232593ece09ce08b630836e825865cDouglas Gregor    SubExprs[i+ARGS_START] = args[i];
119668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
120b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  RParenLoc = rparenloc;
121b4609806e9232593ece09ce08b630836e825865cDouglas Gregor}
122e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman
123668bf91d31265b6ea8c3eb854ba450857701f269Ted KremenekCallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
124668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek                   QualType t, SourceLocation rparenloc)
125898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  : Expr(CallExprClass, t,
126898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
127898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
128898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    NumArgs(numargs) {
129668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
130668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  SubExprs = new (C) Stmt*[numargs+1];
13177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek  SubExprs[FN] = fn;
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != numargs; ++i)
13377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek    SubExprs[i+ARGS_START] = args[i];
134668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  RParenLoc = rparenloc;
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
138668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenekvoid CallExpr::Destroy(ASTContext& C) {
139668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  DestroyChildren(C);
140668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  if (SubExprs) C.Deallocate(SubExprs);
141668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  this->~CallExpr();
142668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  C.Deallocate(this);
143668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek}
144668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
145d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// setNumArgs - This changes the number of arguments present in this call.
146d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// Any orphaned expressions are deleted by this, and any new operands are set
147d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// to null.
1488189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenekvoid CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
149d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // No change, just return.
150d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs == getNumArgs()) return;
151d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
152d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // If shrinking # arguments, just delete the extras and forgot them.
153d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs < getNumArgs()) {
154d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
1558189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek      getArg(i)->Destroy(C);
156d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    this->NumArgs = NumArgs;
157d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    return;
158d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  }
159d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
160d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Otherwise, we are growing the # arguments.  New an bigger argument array.
1615549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  Stmt **NewSubExprs = new Stmt*[NumArgs+1];
162d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Copy over args.
163d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
164d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = SubExprs[i];
165d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Null out new args.
166d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
167d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = 0;
168d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
1698189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  delete [] SubExprs;
170d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  SubExprs = NewSubExprs;
171d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  this->NumArgs = NumArgs;
172d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner}
173d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
174cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// isBuiltinCall - If this is a call to a builtin, return the builtin ID.  If
175cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// not, return 0.
176cb888967400a03504c88acedd5248d6778a82f46Chris Lattnerunsigned CallExpr::isBuiltinCall() const {
177c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // All simple function calls (e.g. func()) are implicitly cast to pointer to
178c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // function. As a result, we try and obtain the DeclRefExpr from the
179c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // ImplicitCastExpr.
180c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
181c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
182cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
183cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
184c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
185c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!DRE)
186cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
187cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
188bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
189bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  if (!FDecl)
190cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
191cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
1924fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor  if (!FDecl->getIdentifier())
1934fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor    return 0;
1944fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor
195cb888967400a03504c88acedd5248d6778a82f46Chris Lattner  return FDecl->getIdentifier()->getBuiltinID();
196cb888967400a03504c88acedd5248d6778a82f46Chris Lattner}
197bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson
198cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "<<=".
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *BinaryOperator::getOpcodeStr(Opcode Op) {
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown binary operator");
2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Mul:       return "*";
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Div:       return "/";
2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Rem:       return "%";
2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Add:       return "+";
2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Sub:       return "-";
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Shl:       return "<<";
2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Shr:       return ">>";
2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LT:        return "<";
2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case GT:        return ">";
2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LE:        return "<=";
2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case GE:        return ">=";
2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case EQ:        return "==";
2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case NE:        return "!=";
2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case And:       return "&";
2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Xor:       return "^";
2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Or:        return "|";
2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LAnd:      return "&&";
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LOr:       return "||";
2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Assign:    return "=";
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case MulAssign: return "*=";
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DivAssign: return "/=";
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case RemAssign: return "%=";
2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case AddAssign: return "+=";
2275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case SubAssign: return "-=";
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ShlAssign: return "<<=";
2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ShrAssign: return ">>=";
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case AndAssign: return "&=";
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case XorAssign: return "^=";
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case OrAssign:  return "|=";
2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Comma:     return ",";
2345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23766b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonInitListExpr::InitListExpr(SourceLocation lbraceloc,
238418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner                           Expr **initExprs, unsigned numInits,
2394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                           SourceLocation rbraceloc)
240c5ae899b4bbf65488445316c63168079177db0edSteve Naroff  : Expr(InitListExprClass, QualType()),
2410bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
242a9c878086036de36482cc21e35a33cabe9699b0aDouglas Gregor    UnionFieldInit(0), HadArrayRangeDesignator(false) {
243418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner
244418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner  InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
24566b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregorvoid InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
2484c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); Idx < LastIdx; ++Idx)
2494c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    delete InitExprs[Idx];
2504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitExprs.resize(NumInits, 0);
2514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
2524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
2534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas GregorExpr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
2544c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (Init >= InitExprs.size()) {
2554c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
2564c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    InitExprs.back() = expr;
2574c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return 0;
2584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
2594c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
2604c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
2614c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitExprs[Init] = expr;
2624c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
2634c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
2644c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
265bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block.
2664eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
2674eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroffconst FunctionType *BlockExpr::getFunctionType() const {
2684eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  return getType()->getAsBlockPointerType()->
2694eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff                    getPointeeType()->getAsFunctionType();
2704eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
2714eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
27256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffSourceLocation BlockExpr::getCaretLocation() const {
27356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  return TheBlock->getCaretLocation();
27456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
27556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroffconst Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
27656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt *BlockExpr::getBody() { return TheBlock->getBody(); }
27756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
27856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// hasLocalSideEffect - Return true if this immediate expression has side
2845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// effects, not counting any sub-expressions.
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Expr::hasLocalSideEffect() const {
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
2885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass:
2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UnaryOperatorClass: {
2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const UnaryOperator *UO = cast<UnaryOperator>(this);
2935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (UO->getOpcode()) {
2955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: return false;
2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::PostInc:
2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::PostDec:
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::PreInc:
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::PreDec:
3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return true;                     // ++/--
3015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Deref:
3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Dereferencing a volatile pointer is a side-effect.
3045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return getType().isVolatileQualified();
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Real:
3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Imag:
3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // accessing a piece of a volatile complex is a side-effect.
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return UO->getSubExpr()->getType().isVolatileQualified();
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Extension:
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return UO->getSubExpr()->hasLocalSideEffect();
3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
314e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  case BinaryOperatorClass: {
315e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner    const BinaryOperator *BinOp = cast<BinaryOperator>(this);
316e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner    // Consider comma to have side effects if the LHS and RHS both do.
317e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner    if (BinOp->getOpcode() == BinaryOperator::Comma)
318e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner      return BinOp->getLHS()->hasLocalSideEffect() &&
319e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner             BinOp->getRHS()->hasLocalSideEffect();
320e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner
321e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner    return BinOp->isAssignmentOp();
322e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  }
323eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner  case CompoundAssignOperatorClass:
3241f683e9cf2e855b32d3fb4b142aa121cd3cf1088Chris Lattner    return true;
3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
326ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  case ConditionalOperatorClass: {
327ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
328ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian    return Exp->getCond()->hasLocalSideEffect()
329ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian           || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
330ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian           || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
331ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  }
332ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case MemberExprClass:
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ArraySubscriptExprClass:
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If the base pointer or element is to a volatile pointer/field, accessing
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // if is a side effect.
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getType().isVolatileQualified();
338211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case CallExprClass:
340b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  case CXXOperatorCallExprClass:
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TODO: check attributes for pure/const.   "void foo() { strlen("bar"); }"
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // should warn.
3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return true;
344a9c01021724b9b546d282b8609cbe559734812ecChris Lattner  case ObjCMessageExprClass:
345a9c01021724b9b546d282b8609cbe559734812ecChris Lattner    return true;
346611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  case StmtExprClass: {
347611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // Statement exprs don't logically have side effects themselves, but are
348611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // sometimes used in macros in ways that give them a type that is unused.
349611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // For example ({ blah; foo(); }) will end up with a type if foo has a type.
350611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // however, if the result of the stmt expr is dead, we don't want to emit a
351611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // warning.
352611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
353611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    if (!CS->body_empty())
354611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner      if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
355611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner        return E->hasLocalSideEffect();
356611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    return false;
357611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  }
3586eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor  case CStyleCastExprClass:
359987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case CXXFunctionalCastExprClass:
3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is a cast to void, check the operand.  Otherwise, the result of
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // the cast is unused.
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getType()->isVoidType())
3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
36504421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner
3664be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman  case ImplicitCastExprClass:
3674be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman    // Check the operand, since implicit casts are inserted by Sema
3684be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman    return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect();
3694be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman
37004421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
37104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect();
3724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
3734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXNewExprClass:
3744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // FIXME: In theory, there might be new expressions that don't have side
3754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // effects (e.g. a placement new with an uninitialized POD).
3764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXDeleteExprClass:
3774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    return true;
3784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
381ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor/// DeclCanBeLvalue - Determine whether the given declaration can be
382ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor/// an lvalue. This is a helper routine for isLvalue.
383ba7e210a999275695f58be03ef402758cfec3635Douglas Gregorstatic bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
38472c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor  // C++ [temp.param]p6:
38572c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor  //   A non-type non-reference template-parameter is not an lvalue.
38672c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor  if (const NonTypeTemplateParmDecl *NTTParm
38772c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor        = dyn_cast<NonTypeTemplateParmDecl>(Decl))
38872c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor    return NTTParm->getType()->isReferenceType();
38972c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor
39044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
391ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor    // C++ 3.10p2: An lvalue refers to an object or function.
392ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor    (Ctx.getLangOptions().CPlusPlus &&
393ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor     (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
394ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor}
395ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor
3965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// incomplete type other than void. Nonarray expressions that can be lvalues:
3985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - name, where name must be a variable
3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - e[i]
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - (e), where e must be an lvalue
4015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - e.name, where e must be an lvalue
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - e->name
4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - *e, the type of e cannot be a function type
4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - string-constant
4057da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner///  - (__real__ e) and (__imag__ e) where e is an lvalue  [GNU extension]
40608ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling///  - reference type [C++ [expr]]
4075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
40828be73f74c9e241a23ea24fe5756623de6bf1084Chris LattnerExpr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
40998cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor  // first, check the type (C99 6.3.2.1). Expressions with function
41098cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor  // type in C are not lvalues, but they can be lvalues in C++.
41198cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor  if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LV_NotObjectType;
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
414acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff  // Allow qualified void which is an incomplete type other than void (yuck).
41528be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner  if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
416acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff    return LV_IncompleteVoidType;
417acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff
41898cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor  /// FIXME: Expressions can't have reference type, so the following
41998cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor  /// isn't needed.
420cb4f9a6d0be445d84af35ba81323be6127816f9eChris Lattner  if (TR->isReferenceType()) // C++ [expr]
42108ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling    return LV_Valid;
42208ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling
4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the type looks fine, now check the expression
4245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
4255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case StringLiteralClass: // C99 6.5.1p4
4267323a6297edad643c202594dcf3d9a174de96ca6Anders Carlsson    return LV_Valid;
4275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // For vectors, make sure base is an lvalue (i.e. not a function call).
4295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
43028be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner      return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
4315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LV_Valid;
4321a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor  case DeclRefExprClass:
4331a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor  case QualifiedDeclRefExprClass: { // C99 6.5.1p2
434ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor    const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
435ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor    if (DeclCanBeLvalue(RefdDecl, Ctx))
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LV_Valid;
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
4384111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  }
439dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff  case BlockDeclRefExprClass: {
440dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff    const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
4414f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff    if (isa<VarDecl>(BDR->getDecl()))
442dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff      return LV_Valid;
443dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff    break;
444dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff  }
44586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor  case MemberExprClass: {
4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const MemberExpr *m = cast<MemberExpr>(this);
44786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
44886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      NamedDecl *Member = m->getMemberDecl();
44986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      // C++ [expr.ref]p4:
45086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   If E2 is declared to have type "reference to T", then E1.E2
45186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   is an lvalue.
45286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
45386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        if (Value->getType()->isReferenceType())
45486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor          return LV_Valid;
45586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
45686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   -- If E2 is a static data member [...] then E1.E2 is an lvalue.
45786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      if (isa<CXXClassVarDecl>(Member))
45886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        return LV_Valid;
45986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
46086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   -- If E2 is a non-static data member [...]. If E1 is an
46186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //      lvalue, then E1.E2 is an lvalue.
46286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      if (isa<FieldDecl>(Member))
46386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
46486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
46586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   -- If it refers to a static member function [...], then
46686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //      E1.E2 is an lvalue.
46786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   -- Otherwise, if E1.E2 refers to a non-static member
46886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //      function [...], then E1.E2 is not an lvalue.
46986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
47086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        return Method->isStatic()? LV_Valid : LV_MemberFunction;
47186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
47286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   -- If E2 is a member enumerator [...], the expression E1.E2
47386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //      is not an lvalue.
47486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      if (isa<EnumConstantDecl>(Member))
47586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        return LV_InvalidExpression;
47686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
47786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        // Not an lvalue.
47886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      return LV_InvalidExpression;
47986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    }
48086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
48186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    // C99 6.5.2.3p4
48228be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner    return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
483fdd75663fffeb2058a7847975e50837e61200593Anton Korobeynikov  }
4847da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner  case UnaryOperatorClass:
4855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
4867da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner      return LV_Valid; // C99 6.5.3p4
4877da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner
4887da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner    if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
489baf0d6678418e0dd9309438c3e50274253cfc7b2Chris Lattner        cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
490baf0d6678418e0dd9309438c3e50274253cfc7b2Chris Lattner        cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
49128be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner      return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx);  // GNU.
49274253736184c0717a0649922551bf9d8b6815651Douglas Gregor
49374253736184c0717a0649922551bf9d8b6815651Douglas Gregor    if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
49474253736184c0717a0649922551bf9d8b6815651Douglas Gregor        (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
49574253736184c0717a0649922551bf9d8b6815651Douglas Gregor         cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
49674253736184c0717a0649922551bf9d8b6815651Douglas Gregor      return LV_Valid;
4975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
498eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  case ImplicitCastExprClass:
499eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor    return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
500eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor                                                       : LV_InvalidExpression;
5015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass: // C99 6.5.1p5
50228be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner    return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
503eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  case BinaryOperatorClass:
504eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  case CompoundAssignOperatorClass: {
505eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor    const BinaryOperator *BinOp = cast<BinaryOperator>(this);
506337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor
507337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor    if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
508337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor        BinOp->getOpcode() == BinaryOperator::Comma)
509337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor      return BinOp->getRHS()->isLvalue(Ctx);
510337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor
511224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl    // C++ [expr.mptr.oper]p6
512224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl    if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
513224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl         BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
514224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl        !BinOp->getType()->isFunctionType())
515224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl      return BinOp->getLHS()->isLvalue(Ctx);
516224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl
517bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor    if (!BinOp->isAssignmentOp())
518eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor      return LV_InvalidExpression;
519eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor
520bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor    if (Ctx.getLangOptions().CPlusPlus)
521bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor      // C++ [expr.ass]p1:
522bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor      //   The result of an assignment operation [...] is an lvalue.
523bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor      return LV_Valid;
524bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor
525bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor
526bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor    // C99 6.5.16:
527bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor    //   An assignment expression [...] is not an lvalue.
528bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor    return LV_InvalidExpression;
529eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  }
53059b5da6d853b4368b984700315adf7b37de05764Nate Begeman  // FIXME: OverloadExprClass
531b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  case CallExprClass:
53288a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor  case CXXOperatorCallExprClass:
53388a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor  case CXXMemberCallExprClass: {
5349d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // C++ [expr.call]p10:
5359d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    //   A function call is an lvalue if and only if the result type
5369d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    //   is a reference.
53727c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor    QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
5389d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
53988a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor      CalleeType = FnTypePtr->getPointeeType();
54088a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor    if (const FunctionType *FnType = CalleeType->getAsFunctionType())
54188a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor      if (FnType->getResultType()->isReferenceType())
54288a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor        return LV_Valid;
5439d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor
5449d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    break;
5459d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  }
546e6386394677ed4f77b20e2e3d5446a3a2f628e53Steve Naroff  case CompoundLiteralExprClass: // C99 6.5.2.5p5
547e6386394677ed4f77b20e2e3d5446a3a2f628e53Steve Naroff    return LV_Valid;
548670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner  case ChooseExprClass:
549670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner    // __builtin_choose_expr is an lvalue if the selected operand is.
550670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner    if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
551670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner      return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
552670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner    else
553670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner      return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
554670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner
555213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case ExtVectorElementExprClass:
556213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
557fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff      return LV_DuplicateVectorComponents;
558fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff    return LV_Valid;
559027282d1c1ac151aa7b1b3b45babc918b8ad456aSteve Naroff  case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
560027282d1c1ac151aa7b1b3b45babc918b8ad456aSteve Naroff    return LV_Valid;
561799a6a6850af625946bb8d88ca960bb6604e3858Steve Naroff  case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
562799a6a6850af625946bb8d88ca960bb6604e3858Steve Naroff    return LV_Valid;
5635daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian  case ObjCKVCRefExprClass: // FIXME: check if read-only property.
564670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner    return LV_Valid;
565d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner  case PredefinedExprClass:
566796da18402f286b897782a298ae3b20c459c102eDouglas Gregor    return LV_Valid;
5679d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case VAArgExprClass:
5689d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    return LV_Valid;
56904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
57028be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner    return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
57124b41fa8239c63b9eb570d3e83c4a82840656a65Argyrios Kyrtzidis  case CXXConditionDeclExprClass:
57224b41fa8239c63b9eb570d3e83c4a82840656a65Argyrios Kyrtzidis    return LV_Valid;
5736eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor  case CStyleCastExprClass:
5749d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case CXXFunctionalCastExprClass:
5759d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case CXXStaticCastExprClass:
5769d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case CXXDynamicCastExprClass:
5779d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case CXXReinterpretCastExprClass:
5789d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case CXXConstCastExprClass:
5799d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // The result of an explicit cast is an lvalue if the type we are
5809d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // casting to is a reference type. See C++ [expr.cast]p1,
5819d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
5829d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
5839d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
5849d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor      return LV_Valid;
5859d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    break;
586c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  case CXXTypeidExprClass:
587c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
588c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    return LV_Valid;
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return LV_InvalidExpression;
5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// does not have an incomplete type, does not have a const-qualified type, and
5975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// if it is a structure or union, does not have any member (including,
5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// recursively, any member or element of all contained aggregates or unions)
5995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// with a const-qualified type.
60028be73f74c9e241a23ea24fe5756623de6bf1084Chris LattnerExpr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
60128be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner  isLvalueResult lvalResult = isLvalue(Ctx);
6025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (lvalResult) {
604ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor  case LV_Valid:
605ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor    // C++ 3.10p11: Functions cannot be modified, but pointers to
606ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor    // functions can be modifiable.
607ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor    if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
608ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor      return MLV_NotObjectType;
609ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor    break;
610ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor
6115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LV_NotObjectType: return MLV_NotObjectType;
6125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
613fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
614ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner  case LV_InvalidExpression:
615ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    // If the top level is a C-style cast, and the subexpression is a valid
616ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    // lvalue, then this is probably a use of the old-school "cast as lvalue"
617ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    // GCC extension.  We don't support it, but we want to produce good
618ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    // diagnostics when it happens so that the user knows why.
619ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
620ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner      if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
621ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner        return MLV_LValueCast;
622ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    return MLV_InvalidExpression;
62386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor  case LV_MemberFunction: return MLV_MemberFunction;
6245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
625c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
626c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType CT = Ctx.getCanonicalType(getType());
627c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
628c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (CT.isConstQualified())
6295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return MLV_ConstQualified;
630c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (CT->isArrayType())
6315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return MLV_ArrayType;
632c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (CT->isIncompleteType())
6335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return MLV_IncompleteType;
6345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
635c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const RecordType *r = CT->getAsRecordType()) {
6365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (r->hasConstFields())
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return MLV_ConstQualified;
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6394f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  // The following is illegal:
6404f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  //   void takeclosure(void (^C)(void));
6414f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  //   void func() { int x = 1; takeclosure(^{ x = 7 }); }
6424f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  //
6434f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  if (getStmtClass() == BlockDeclRefExprClass) {
6444f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff    const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
6454f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff    if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
6464f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff      return MLV_NotBlockQualified;
6474f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  }
648d1fa6449e9dbdd667466e9e1e971aa17c9793e8aFariborz Jahanian
649ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian  // Assigning to an 'implicit' property?
6506669db9d80d77d10f101aa9f8e488bbd2d98f76cFariborz Jahanian  else if (getStmtClass() == ObjCKVCRefExprClass) {
651ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian    const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
652ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian    if (KVCExpr->getSetterMethod() == 0)
653ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian      return MLV_NoSetterProperty;
654ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian  }
6555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return MLV_Valid;
6565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6582e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek/// hasGlobalStorage - Return true if this expression has static storage
6594cc627111453b75519d5130b57e06256da7b00e8Chris Lattner/// duration.  This means that the address of this expression is a link-time
6604cc627111453b75519d5130b57e06256da7b00e8Chris Lattner/// constant.
6612e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenekbool Expr::hasGlobalStorage() const {
6621d09ecca892185ba067e47ba879f424de59950efChris Lattner  switch (getStmtClass()) {
6631d09ecca892185ba067e47ba879f424de59950efChris Lattner  default:
6641d09ecca892185ba067e47ba879f424de59950efChris Lattner    return false;
6654cc627111453b75519d5130b57e06256da7b00e8Chris Lattner  case ParenExprClass:
6662e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek    return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
6674cc627111453b75519d5130b57e06256da7b00e8Chris Lattner  case ImplicitCastExprClass:
6682e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek    return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
669e9b12198c4cc7f5687960100351b4af006c14469Steve Naroff  case CompoundLiteralExprClass:
670e9b12198c4cc7f5687960100351b4af006c14469Steve Naroff    return cast<CompoundLiteralExpr>(this)->isFileScope();
6711a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor  case DeclRefExprClass:
6721a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor  case QualifiedDeclRefExprClass: {
6731d09ecca892185ba067e47ba879f424de59950efChris Lattner    const Decl *D = cast<DeclRefExpr>(this)->getDecl();
6741d09ecca892185ba067e47ba879f424de59950efChris Lattner    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
6752e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek      return VD->hasGlobalStorage();
67663f067f5a75ae97b53dc7a6a6530e2c72c8bb7f8Seo Sanghyeon    if (isa<FunctionDecl>(D))
67763f067f5a75ae97b53dc7a6a6530e2c72c8bb7f8Seo Sanghyeon      return true;
6781d09ecca892185ba067e47ba879f424de59950efChris Lattner    return false;
6791d09ecca892185ba067e47ba879f424de59950efChris Lattner  }
680fb7080663a0f0abd687a6f58ac917d9a76318b73Chris Lattner  case MemberExprClass: {
6811d09ecca892185ba067e47ba879f424de59950efChris Lattner    const MemberExpr *M = cast<MemberExpr>(this);
6822e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek    return !M->isArrow() && M->getBase()->hasGlobalStorage();
683fb7080663a0f0abd687a6f58ac917d9a76318b73Chris Lattner  }
6844cc627111453b75519d5130b57e06256da7b00e8Chris Lattner  case ArraySubscriptExprClass:
6852e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek    return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
686d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner  case PredefinedExprClass:
687fa28b30d5a1e93e5263be33e343532b900d2c643Chris Lattner    return true;
68804421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
68904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
6901d09ecca892185ba067e47ba879f424de59950efChris Lattner  }
6911d09ecca892185ba067e47ba879f424de59950efChris Lattner}
6921d09ecca892185ba067e47ba879f424de59950efChris Lattner
6934e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() {
6944e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  Expr* E = this;
6954e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  while (ParenExpr* P = dyn_cast<ParenExpr>(E))
6964e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek    E = P->getSubExpr();
6974e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek
6984e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  return E;
6994e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek}
7004e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek
70156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
70256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand.
70356f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() {
70456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  Expr *E = this;
70556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  while (true) {
70656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner    if (ParenExpr *P = dyn_cast<ParenExpr>(E))
70756f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
70856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner    else if (CastExpr *P = dyn_cast<CastExpr>(E))
70956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
71056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner    else
71156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      return E;
71256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  }
71356f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner}
71456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner
715898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyTypeDependentArguments - Determines if any of the expressions
716898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is type-dependent.
717898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
718898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
719898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isTypeDependent())
720898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
721898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
722898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
723898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
724898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
725898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyValueDependentArguments - Determines if any of the expressions
726898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is value-dependent.
727898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
728898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
729898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isValueDependent())
730898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
731898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
732898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
733898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
734898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
735c9e8f606787b0bc0c3b08e566b87cc1751694168Eli Friedmanbool Expr::isConstantInitializer(ASTContext &Ctx) const {
736c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // This function is attempting whether an expression is an initializer
737c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // which can be evaluated at compile-time.  isEvaluatable handles most
738c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // of the cases, but it can't deal with some initializer-specific
739c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // expressions, and it can't deal with aggregates; we deal with those here,
740c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // and fall back to isEvaluatable for the other cases.
741c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman
742e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  switch (getStmtClass()) {
743c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  default: break;
744e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case StringLiteralClass:
745e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    return true;
74659b5da6d853b4368b984700315adf7b37de05764Nate Begeman  case CompoundLiteralExprClass: {
74759b5da6d853b4368b984700315adf7b37de05764Nate Begeman    const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
748c9e8f606787b0bc0c3b08e566b87cc1751694168Eli Friedman    return Exp->isConstantInitializer(Ctx);
74959b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
750e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case InitListExprClass: {
751e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    const InitListExpr *Exp = cast<InitListExpr>(this);
752e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    unsigned numInits = Exp->getNumInits();
753e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    for (unsigned i = 0; i < numInits; i++) {
754c9e8f606787b0bc0c3b08e566b87cc1751694168Eli Friedman      if (!Exp->getInit(i)->isConstantInitializer(Ctx))
755e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson        return false;
756e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    }
757c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    return true;
758e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
7593498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  case ImplicitValueInitExprClass:
7603498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return true;
761c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case ParenExprClass: {
762c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
763c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  }
764c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case UnaryOperatorClass: {
765c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    const UnaryOperator* Exp = cast<UnaryOperator>(this);
766c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    if (Exp->getOpcode() == UnaryOperator::Extension)
767c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman      return Exp->getSubExpr()->isConstantInitializer(Ctx);
768c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
769c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  }
770c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case CStyleCastExprClass:
771c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // Handle casts with a destination that's a struct or union; this
772c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // deals with both the gcc no-op struct cast extension and the
773c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // cast-to-union extension.
774c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    if (getType()->isRecordType())
775c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman      return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
776c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
77732a311e276cd4bcafddd38db679aff9804e66bd4Eli Friedman  case DesignatedInitExprClass:
7784e716e07d90a18ac83dd94d157ec676530bc78f9Sebastian Redl    return cast<DesignatedInitExpr>(this)->
7794e716e07d90a18ac83dd94d157ec676530bc78f9Sebastian Redl            getInit()->isConstantInitializer(Ctx);
780e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
781e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson
782c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  return isEvaluatable(Ctx);
78338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff}
78438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIntegerConstantExpr - this recursive routine will test if an expression is
7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an integer constant expression. Note: With the introduction of VLA's in
7875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// C99 the result of the sizeof operator is no longer always a constant
7885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expression. The generalization of the wording to include any subexpression
7895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
7905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
7915f6b632391e24b08ead3e62ba2e2765e770382edNuno Lopes/// "0 || f()" can be treated as a constant expression. In C90 this expression,
7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// occurring in a context requiring a constant, would have been a constraint
7935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// violation. FIXME: This routine currently implements C90 semantics.
7945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// To properly implement C99 semantics this routine will need to evaluate
7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expressions involving operators previously mentioned.
7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
7985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// comma, etc
7995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// FIXME: This should ext-warn on overflow during evaluation!  ISO C does not
801ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner/// permit this.  This includes things like (int)1e1000
802ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner///
803ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// FIXME: Handle offsetof.  Two things to do:  Handle GCC's __builtin_offsetof
804ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// to support gcc 4.0+  and handle the idiom GCC recognizes with a null pointer
805ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// cast+dereference.
806590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattnerbool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
807590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner                                 SourceLocation *Loc, bool isEvaluated) const {
808a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  // Pretest for integral type; some parts of the code crash for types that
809a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  // can't be sized.
810a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  if (!getType()->isIntegralType()) {
811a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman    if (Loc) *Loc = getLocStart();
812a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman    return false;
813a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  }
8145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
8155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Loc) *Loc = getLocStart();
8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
8185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass:
8195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return cast<ParenExpr>(this)->getSubExpr()->
820590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner                     isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case IntegerLiteralClass:
8225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Result = cast<IntegerLiteral>(this)->getValue();
8235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
8242eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner  case CharacterLiteralClass: {
8252eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner    const CharacterLiteral *CL = cast<CharacterLiteral>(this);
82698be4943e8dc4f3905629a7102668960873cf863Chris Lattner    Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
8272eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner    Result = CL->getValue();
828f0fbcb356bc4eb5f6467dc4fc4bc447e9a630102Chris Lattner    Result.setIsUnsigned(!getType()->isSignedIntegerType());
8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
8302eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner  }
831b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson  case CXXBoolLiteralExprClass: {
832b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson    const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
833b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson    Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
834b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson    Result = BL->getValue();
835b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson    Result.setIsUnsigned(!getType()->isSignedIntegerType());
836b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson    break;
837b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson  }
8387267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis  case CXXZeroInitValueExprClass:
8397267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis    Result.clear();
8407267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis    break;
8417b658aa315784fa489a32daeacfe400512ce468dSteve Naroff  case TypesCompatibleExprClass: {
8427b658aa315784fa489a32daeacfe400512ce468dSteve Naroff    const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
84398be4943e8dc4f3905629a7102668960873cf863Chris Lattner    Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
844ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    // Per gcc docs "this built-in function ignores top level
845ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    // qualifiers".  We need to use the canonical version to properly
846ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    // be able to strip CRV qualifiers from the type.
847ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
848ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
849ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    Result = Ctx.typesAreCompatible(T0.getUnqualifiedType(),
850ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar                                    T1.getUnqualifiedType());
851389cecc83f33e93c8ba6bf2e8269b8690404416fSteve Naroff    break;
8527b658aa315784fa489a32daeacfe400512ce468dSteve Naroff  }
853b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  case CallExprClass:
854b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  case CXXOperatorCallExprClass: {
85513b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff    const CallExpr *CE = cast<CallExpr>(this);
85698be4943e8dc4f3905629a7102668960873cf863Chris Lattner    Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
857a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner
858a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    // If this is a call to a builtin function, constant fold it otherwise
859a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    // reject it.
860a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    if (CE->isBuiltinCall()) {
8611c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson      EvalResult EvalResult;
8621c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson      if (CE->Evaluate(EvalResult, Ctx)) {
8631c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson        assert(!EvalResult.HasSideEffects &&
8641c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson               "Foldable builtin call should not have side effects!");
8651c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson        Result = EvalResult.Val.getInt();
866a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner        break;  // It is a constant, expand it.
867a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner      }
868a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    }
869a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner
87013b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff    if (Loc) *Loc = getLocStart();
87113b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff    return false;
87213b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff  }
8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclRefExprClass:
8741a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor  case QualifiedDeclRefExprClass:
8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (const EnumConstantDecl *D =
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result = D->getInitVal();
8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
8804a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl    if (Ctx.getLangOptions().CPlusPlus &&
8814a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl        getType().getCVRQualifiers() == QualType::Const) {
8824a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl      // C++ 7.1.5.1p2
8834a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl      //   A variable of non-volatile const-qualified integral or enumeration
8844a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl      //   type initialized by an ICE can be used in ICEs.
8854a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl      if (const VarDecl *Dcl =
8864a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl              dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
8874a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl        if (const Expr *Init = Dcl->getInit())
8884a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl          return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
8894a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl      }
8904a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl    }
8915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Loc) *Loc = getLocStart();
8925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
8935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UnaryOperatorClass: {
8945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const UnaryOperator *Exp = cast<UnaryOperator>(this);
8955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8960518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    // Get the operand value.  If this is offsetof, do not evalute the
8975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // operand.  This affects C99 6.6p3.
8980518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
8990518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl                        isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
9005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
9015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Exp->getOpcode()) {
9035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
9045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // See C99 6.6p3.
9055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
9065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Loc) *Loc = Exp->getOperatorLoc();
9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Extension:
90976e773a443be9f006610f46529e07d4c8d857680Chris Lattner      return true;  // FIXME: this is wrong.
9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::LNot: {
911bf7553889594886213e05cd3f94a6d6f7747bf7bChris Lattner      bool Val = Result == 0;
91298be4943e8dc4f3905629a7102668960873cf863Chris Lattner      Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result = Val;
9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Plus:
9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Minus:
9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result = -Result;
9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Not:
9225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result = ~Result;
9235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
9245a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    case UnaryOperator::OffsetOf:
925aa1f9f1d50adf294674b74510b62e863b68572bcDaniel Dunbar      Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
9265a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson      Result = Exp->evaluateOffsetOf(Ctx);
9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
9295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9300518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  case SizeOfAlignOfExprClass: {
9310518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
932a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner
933a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner    // Return the result in the right width.
93498be4943e8dc4f3905629a7102668960873cf863Chris Lattner    Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
935a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner
9360518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    QualType ArgTy = Exp->getTypeOfArgument();
937a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner    // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
9380518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    if (ArgTy->isVoidType()) {
939a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner      Result = 1;
940a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner      break;
941a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner    }
942a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner
943a269ebfd91c4fa47e051fa1fa904833a022fe025Chris Lattner    // alignof always evaluates to a constant, sizeof does if arg is not VLA.
9440518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
94565383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner      if (Loc) *Loc = Exp->getOperatorLoc();
9465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
94765383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner    }
9485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
94976e773a443be9f006610f46529e07d4c8d857680Chris Lattner    // Get information about the size or align.
9500518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    if (ArgTy->isFunctionType()) {
951efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner      // GCC extension: sizeof(function) = 1.
952efdd1574e3cca00ef0410aee8b7ee36eda5a2f71Chris Lattner      Result = Exp->isSizeOf() ? 1 : 4;
9536a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson    } else {
95498be4943e8dc4f3905629a7102668960873cf863Chris Lattner      unsigned CharSize = Ctx.Target.getCharWidth();
9556a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson      if (Exp->isSizeOf())
9560518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl        Result = Ctx.getTypeSize(ArgTy) / CharSize;
9576a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson      else
9580518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl        Result = Ctx.getTypeAlign(ArgTy) / CharSize;
959060e470e53b73167114284eb0b8c3e25bb3dad99Ted Kremenek    }
9605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
9615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BinaryOperatorClass: {
9635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const BinaryOperator *Exp = cast<BinaryOperator>(this);
964e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    llvm::APSInt LHS, RHS;
965e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar
966e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    // Initialize result to have correct signedness and width.
967e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())),
968b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman                          !getType()->isSignedIntegerType());
969b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman
9705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The LHS of a constant expr is always evaluated and needed.
971e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The short-circuiting &&/|| operators don't necessarily evaluate their
9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // RHS.  Make sure to pass isEvaluated down correctly.
9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Exp->isLogicalOp()) {
9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      bool RHSEval;
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Exp->getOpcode() == BinaryOperator::LAnd)
979e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar        RHSEval = LHS != 0;
9805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else {
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
982e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar        RHSEval = LHS == 0;
9835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
985590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner      if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                                isEvaluated & RHSEval))
9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
9885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
989590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner      if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
9905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
9925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Exp->getOpcode()) {
9945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
9955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Loc) *Loc = getLocStart();
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
9975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::Mul:
998e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS * RHS;
9995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::Div:
10015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (RHS == 0) {
10025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (!isEvaluated) break;
10035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (Loc) *Loc = getLocStart();
10045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1006e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS / RHS;
10075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::Rem:
10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (RHS == 0) {
10105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (!isEvaluated) break;
10115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (Loc) *Loc = getLocStart();
10125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
10135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1014e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS % RHS;
10155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1016e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::Add: Result = LHS + RHS; break;
1017e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::Sub: Result = LHS - RHS; break;
10185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::Shl:
1019e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS <<
1020e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar        static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1021e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    break;
10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::Shr:
1023e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS >>
1024e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar        static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1026e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::LT:  Result = LHS < RHS; break;
1027e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::GT:  Result = LHS > RHS; break;
1028e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::LE:  Result = LHS <= RHS; break;
1029e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::GE:  Result = LHS >= RHS; break;
1030e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::EQ:  Result = LHS == RHS; break;
1031e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::NE:  Result = LHS != RHS; break;
1032e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::And: Result = LHS & RHS; break;
1033e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1034e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::Or:  Result = LHS | RHS; break;
10355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::LAnd:
1036e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS != 0 && RHS != 0;
10375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::LOr:
1039e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS != 0 || RHS != 0;
10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1041b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman
1042b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman    case BinaryOperator::Comma:
1043b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1044b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      // *except* when they are contained within a subexpression that is not
1045b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      // evaluated".  Note that Assignment can never happen due to constraints
1046b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      // on the LHS subexpr, so we don't need to check it here.
1047b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      if (isEvaluated) {
1048b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman        if (Loc) *Loc = getLocStart();
1049b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman        return false;
1050b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      }
1051b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman
1052b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      // The result of the constant expr is the RHS.
1053b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      Result = RHS;
1054b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      return true;
10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
106026dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner  case ImplicitCastExprClass:
10616eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor  case CStyleCastExprClass:
1062987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case CXXFunctionalCastExprClass: {
10630835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis    const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
10640835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis    SourceLocation CastLoc = getLocStart();
106526dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner
10665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.6p6: shall only convert arithmetic types to integer types.
106726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner    if (!SubExpr->getType()->isArithmeticType() ||
106826dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner        !getType()->isIntegerType()) {
106926dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner      if (Loc) *Loc = SubExpr->getLocStart();
10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1072987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner
107398be4943e8dc4f3905629a7102668960873cf863Chris Lattner    uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
1074987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Handle simple integer->integer casts.
107626dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner    if (SubExpr->getType()->isIntegerType()) {
107726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner      if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
107926dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner
108026dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner      // Figure out if this is a truncate, extend or noop cast.
108126dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner      // If the input is signed, do a sign extend, noop, or truncate.
1082c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner      if (getType()->isBooleanType()) {
1083c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner        // Conversion to bool compares against zero.
1084c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner        Result = Result != 0;
1085c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner        Result.zextOrTrunc(DestWidth);
1086c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner      } else if (SubExpr->getType()->isSignedIntegerType())
108726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner        Result.sextOrTrunc(DestWidth);
108826dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner      else  // If the input is unsigned, do a zero extend, noop, or truncate.
108926dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner        Result.zextOrTrunc(DestWidth);
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Allow floating constants that are the immediate operands of casts or that
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // are parenthesized.
109526dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner    const Expr *Operand = SubExpr;
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Operand = PE->getSubExpr();
1098987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner
1099987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    // If this isn't a floating literal, we can't handle it.
1100987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1101987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    if (!FL) {
1102987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner      if (Loc) *Loc = Operand->getLocStart();
1103987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner      return false;
1104987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    }
1105c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner
1106c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner    // If the destination is boolean, compare against zero.
1107c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner    if (getType()->isBooleanType()) {
1108c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner      Result = !FL->getValue().isZero();
1109c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner      Result.zextOrTrunc(DestWidth);
1110c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner      break;
1111c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner    }
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1113987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    // Determine whether we are converting to unsigned or signed.
1114987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    bool DestSigned = getType()->isSignedIntegerType();
1115ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner
1116ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner    // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1117ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner    // be called multiple times per AST.
1118ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen    uint64_t Space[4];
1119ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen    bool ignored;
1120ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner    (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
1121ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen                                          llvm::APFloat::rmTowardZero,
1122ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen                                          &ignored);
1123987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    Result = llvm::APInt(DestWidth, 4, Space);
1124987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    break;
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ConditionalOperatorClass: {
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
112928daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner    const Expr *Cond = Exp->getCond();
113028daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner
113128daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner    if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const Expr *TrueExp  = Exp->getLHS();
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const Expr *FalseExp = Exp->getRHS();
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Result == 0) std::swap(TrueExp, FalseExp);
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
113828daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner    // If the condition (ignoring parens) is a __builtin_constant_p call,
113928daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner    // then only the true side is actually considered in an integer constant
114042b83dde7c700b34f9435ad746984169888ae705Chris Lattner    // expression, and it is fully evaluated.  This is an important GNU
114142b83dde7c700b34f9435ad746984169888ae705Chris Lattner    // extension.  See GCC PR38377 for discussion.
114228daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner    if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
114342b83dde7c700b34f9435ad746984169888ae705Chris Lattner      if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) {
114442b83dde7c700b34f9435ad746984169888ae705Chris Lattner        EvalResult EVResult;
114542b83dde7c700b34f9435ad746984169888ae705Chris Lattner        if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
114642b83dde7c700b34f9435ad746984169888ae705Chris Lattner          return false;
114742b83dde7c700b34f9435ad746984169888ae705Chris Lattner        assert(EVResult.Val.isInt() && "FP conditional expr not expected");
114842b83dde7c700b34f9435ad746984169888ae705Chris Lattner        Result = EVResult.Val.getInt();
114942b83dde7c700b34f9435ad746984169888ae705Chris Lattner        if (Loc) *Loc = EVResult.DiagLoc;
115042b83dde7c700b34f9435ad746984169888ae705Chris Lattner        return true;
115142b83dde7c700b34f9435ad746984169888ae705Chris Lattner      }
115228daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner
11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Evaluate the false one first, discard the result.
11543907323dd6665c0c4e383435cb145233f4533406Anders Carlsson    if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Evalute the true one, capture the result.
11573907323dd6665c0c4e383435cb145233f4533406Anders Carlsson    if (TrueExp &&
11583907323dd6665c0c4e383435cb145233f4533406Anders Carlsson        !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
116204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
116304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    return cast<CXXDefaultArgExpr>(this)
116404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner             ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
116564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
116664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case UnaryTypeTraitExprClass:
116764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    Result = cast<UnaryTypeTraitExpr>(this)->Evaluate();
116864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return true;
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Cases that are valid constant exprs fall through to here.
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Result.setIsUnsigned(getType()->isUnsignedIntegerType());
11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNullPointerConstant - C99 6.3.2.3p3 -  Return true if this is either an
11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// integer constant expression with the value zero, or if this is one that is
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// cast to void*.
1179efa9b3877ef298bcb792600ac33521827e1f7fafAnders Carlssonbool Expr::isNullPointerConstant(ASTContext &Ctx) const
1180efa9b3877ef298bcb792600ac33521827e1f7fafAnders Carlsson{
11810777972d38a3125efed962b045704c30ae6965cfSebastian Redl  // Strip off a cast to void*, if it exists. Except in C++.
11820835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis  if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
11836215dee86c0e715b9f2b0d401ab2a5fcf629f1afSebastian Redl    if (!Ctx.getLangOptions().CPlusPlus) {
11840777972d38a3125efed962b045704c30ae6965cfSebastian Redl      // Check that it is a cast to void*.
11850777972d38a3125efed962b045704c30ae6965cfSebastian Redl      if (const PointerType *PT = CE->getType()->getAsPointerType()) {
11860777972d38a3125efed962b045704c30ae6965cfSebastian Redl        QualType Pointee = PT->getPointeeType();
11870777972d38a3125efed962b045704c30ae6965cfSebastian Redl        if (Pointee.getCVRQualifiers() == 0 &&
11880777972d38a3125efed962b045704c30ae6965cfSebastian Redl            Pointee->isVoidType() &&                              // to void*
11890777972d38a3125efed962b045704c30ae6965cfSebastian Redl            CE->getSubExpr()->getType()->isIntegerType())         // from int.
1190d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson          return CE->getSubExpr()->isNullPointerConstant(Ctx);
11910777972d38a3125efed962b045704c30ae6965cfSebastian Redl      }
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1193aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1194aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Ignore the ImplicitCastExpr type entirely.
1195d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson    return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1196aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1197aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Accept ((void*)0) as a null pointer constant, as many other
1198aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // implementations do.
1199d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson    return PE->getSubExpr()->isNullPointerConstant(Ctx);
12008123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  } else if (const CXXDefaultArgExpr *DefaultArg
12018123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner               = dyn_cast<CXXDefaultArgExpr>(this)) {
120204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    // See through default argument expressions
1203d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson    return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
12042d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor  } else if (isa<GNUNullExpr>(this)) {
12052d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    // The GNU __null extension is always a null pointer constant.
12062d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    return true;
1207aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff  }
12082d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
1209aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  // This expression must be an integer type.
1210aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  if (!getType()->isIntegerType())
1211aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    return false;
1212aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have an integer constant expression, we need to *evaluate* it and
12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // test for the value 0.
1215d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson  // FIXME: We should probably return false if we're compiling in strict mode
1216d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson  // and Diag is not null (this indicates that the value was foldable but not
1217d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson  // an ICE.
1218d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson  EvalResult Result;
1219efa9b3877ef298bcb792600ac33521827e1f7fafAnders Carlsson  return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
1220d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson        Result.Val.isInt() && Result.Val.getInt() == 0;
12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
122231a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff
122327c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// isBitField - Return true if this expression is a bit-field.
122427c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorbool Expr::isBitField() {
122527c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  Expr *E = this->IgnoreParenCasts();
122627c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
122786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
122886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        return Field->isBitField();
122927c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  return false;
123027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
123127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
1232213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const {
12338a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  if (const VectorType *VT = getType()->getAsVectorType())
12348a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    return VT->getNumElements();
12358a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  return 1;
12364d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner}
12374d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner
12388a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated.
1239213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const {
1240fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  const char *compStr = Accessor.getName();
12417e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner  unsigned length = Accessor.getLength();
1242190d6a25393995b42e32086949a68285ee423fb9Nate Begeman
1243190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Halving swizzles do not contain duplicate elements.
1244190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1245190d6a25393995b42e32086949a68285ee423fb9Nate Begeman      !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1246190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    return false;
1247190d6a25393995b42e32086949a68285ee423fb9Nate Begeman
1248190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Advance past s-char prefix on hex swizzles.
1249190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  if (*compStr == 's') {
1250190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    compStr++;
1251190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    length--;
1252190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  }
1253fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff
12547e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner  for (unsigned i = 0; i != length-1; i++) {
1255fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff    const char *s = compStr+i;
1256fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff    for (const char c = *s++; *s; s++)
1257fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff      if (c == *s)
1258fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff        return true;
1259fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  }
1260fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  return false;
1261fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff}
1262b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
12638a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
12643b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess(
12653b8d116703db8018f855cbb4733ace426422623bNate Begeman                                  llvm::SmallVectorImpl<unsigned> &Elts) const {
12667e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner  const char *compStr = Accessor.getName();
1267353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman  if (*compStr == 's')
1268353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman    compStr++;
1269353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman
1270353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman  bool isHi =   !strcmp(compStr, "hi");
1271353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman  bool isLo =   !strcmp(compStr, "lo");
1272353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman  bool isEven = !strcmp(compStr, "even");
1273353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman  bool isOdd  = !strcmp(compStr, "odd");
1274353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman
12758a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
12768a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    uint64_t Index;
12778a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman
12788a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    if (isHi)
12798a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = e + i;
12808a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isLo)
12818a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = i;
12828a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isEven)
12838a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i;
12848a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isOdd)
12858a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i + 1;
12868a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else
12878a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = ExtVectorType::getAccessorIdx(compStr[i]);
1288b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
12893b8d116703db8018f855cbb4733ace426422623bNate Begeman    Elts.push_back(Index);
1290b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner  }
12918a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman}
12928a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman
129368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// constructor for instance messages.
1294bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve NaroffObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
1295a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                QualType retType, ObjCMethodDecl *mproto,
1296db611d556f71f98b66b69514d45958d76e5727abSteve Naroff                SourceLocation LBrac, SourceLocation RBrac,
129749f109c786f99eb7468dac3976db083a65493444Steve Naroff                Expr **ArgExprs, unsigned nargs)
1298db611d556f71f98b66b69514d45958d76e5727abSteve Naroff  : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1299ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted Kremenek    MethodProto(mproto) {
130049f109c786f99eb7468dac3976db083a65493444Steve Naroff  NumArgs = nargs;
13015549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  SubExprs = new Stmt*[NumArgs+1];
130268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  SubExprs[RECEIVER] = receiver;
130349f109c786f99eb7468dac3976db083a65493444Steve Naroff  if (NumArgs) {
130449f109c786f99eb7468dac3976db083a65493444Steve Naroff    for (unsigned i = 0; i != NumArgs; ++i)
130568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
130668d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  }
1307563477da25f824e37c535131695dc4dc9b68c465Steve Naroff  LBracloc = LBrac;
1308563477da25f824e37c535131695dc4dc9b68c465Steve Naroff  RBracloc = RBrac;
1309563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
1310563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
131168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// constructor for class messages.
131268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// FIXME: clsName should be typed to ObjCInterfaceType
1313bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve NaroffObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
1314a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                QualType retType, ObjCMethodDecl *mproto,
1315db611d556f71f98b66b69514d45958d76e5727abSteve Naroff                SourceLocation LBrac, SourceLocation RBrac,
131649f109c786f99eb7468dac3976db083a65493444Steve Naroff                Expr **ArgExprs, unsigned nargs)
1317db611d556f71f98b66b69514d45958d76e5727abSteve Naroff  : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1318ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted Kremenek    MethodProto(mproto) {
131949f109c786f99eb7468dac3976db083a65493444Steve Naroff  NumArgs = nargs;
13205549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  SubExprs = new Stmt*[NumArgs+1];
13214df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
132249f109c786f99eb7468dac3976db083a65493444Steve Naroff  if (NumArgs) {
132349f109c786f99eb7468dac3976db083a65493444Steve Naroff    for (unsigned i = 0; i != NumArgs; ++i)
132468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
132568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  }
1326563477da25f824e37c535131695dc4dc9b68c465Steve Naroff  LBracloc = LBrac;
1327563477da25f824e37c535131695dc4dc9b68c465Steve Naroff  RBracloc = RBrac;
1328563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
1329563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
13304df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek// constructor for class messages.
13314df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
13324df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek                                 QualType retType, ObjCMethodDecl *mproto,
13334df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek                                 SourceLocation LBrac, SourceLocation RBrac,
13344df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek                                 Expr **ArgExprs, unsigned nargs)
13354df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
13364df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekMethodProto(mproto) {
13374df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  NumArgs = nargs;
13384df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  SubExprs = new Stmt*[NumArgs+1];
13394df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
13404df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  if (NumArgs) {
13414df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    for (unsigned i = 0; i != NumArgs; ++i)
13424df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
13434df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  }
13444df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  LBracloc = LBrac;
13454df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  RBracloc = RBrac;
13464df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek}
13474df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
13484df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
13494df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
13504df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  switch (x & Flags) {
13514df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    default:
13524df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      assert(false && "Invalid ObjCMessageExpr.");
13534df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    case IsInstMeth:
13544df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      return ClassInfo(0, 0);
13554df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    case IsClsMethDeclUnknown:
13564df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
13574df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    case IsClsMethDeclKnown: {
13584df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
13594df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      return ClassInfo(D, D->getIdentifier());
13604df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    }
13614df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  }
13624df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek}
13634df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
136427437caadea35f84d550cd29f024fcf3ea240eecChris Lattnerbool ChooseExpr::isConditionTrue(ASTContext &C) const {
136532442bbc98bafa512fa42d46fedf60ed7d79f574Daniel Dunbar  return getCond()->getIntegerConstantExprValue(C) != 0;
136627437caadea35f84d550cd29f024fcf3ea240eecChris Lattner}
136727437caadea35f84d550cd29f024fcf3ea240eecChris Lattner
1368670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattnerstatic int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
13695a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
13705a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    QualType Ty = ME->getBase()->getType();
13715a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
13725a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    RecordDecl *RD = Ty->getAsRecordType()->getDecl();
137398be4943e8dc4f3905629a7102668960873cf863Chris Lattner    const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
137486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
137586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      // FIXME: This is linear time. And the fact that we're indexing
137686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      // into the layout by position in the record means that we're
137786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      // either stuck numbering the fields in the AST or we have to keep
137886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      // the linear search (yuck and yuck).
137986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      unsigned i = 0;
138086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      for (RecordDecl::field_iterator Field = RD->field_begin(),
138186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor                                   FieldEnd = RD->field_end();
138286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor           Field != FieldEnd; (void)++Field, ++i) {
138386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        if (*Field == FD)
138486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor          break;
138586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      }
138686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
138786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
13885a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    }
13895a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
13905a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    const Expr *Base = ASE->getBase();
13915a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
139298be4943e8dc4f3905629a7102668960873cf863Chris Lattner    int64_t size = C.getTypeSize(ASE->getType());
139332442bbc98bafa512fa42d46fedf60ed7d79f574Daniel Dunbar    size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
13945a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
13955a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    return size + evaluateOffsetOf(C, Base);
13965a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  } else if (isa<CompoundLiteralExpr>(E))
13975a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    return 0;
13985a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
13995a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  assert(0 && "Unknown offsetof subexpression!");
14005a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  return 0;
14015a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson}
14025a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
14035a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlssonint64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
14045a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson{
14055a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  assert(Opc == OffsetOf && "Unary operator not offsetof!");
14065a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
140798be4943e8dc4f3905629a7102668960873cf863Chris Lattner  unsigned CharSize = C.Target.getCharWidth();
14085549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
14095a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson}
14105a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
14110518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redlvoid SizeOfAlignOfExpr::Destroy(ASTContext& C) {
14120518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // Override default behavior of traversing children. If this has a type
14130518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // operand and the type is a variable-length array, the child iteration
14140518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // will iterate over the size expression. However, this expression belongs
14150518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // to the type, not to this, so we don't want to delete it.
14160518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // We still want to delete this expression.
14178189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  if (isArgumentType()) {
14188189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    this->~SizeOfAlignOfExpr();
14198189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    C.Deallocate(this);
14208189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  }
14210518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  else
14220518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    Expr::Destroy(C);
14239048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar}
14249048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar
1425fb7413f12636cefa9ebec9abf95804f82c305b11Ted Kremenekvoid OverloadExpr::Destroy(ASTContext& C) {
1426fb7413f12636cefa9ebec9abf95804f82c305b11Ted Kremenek  DestroyChildren(C);
1427fb7413f12636cefa9ebec9abf95804f82c305b11Ted Kremenek  C.Deallocate(SubExprs);
1428fb7413f12636cefa9ebec9abf95804f82c305b11Ted Kremenek  this->~OverloadExpr();
1429fb7413f12636cefa9ebec9abf95804f82c305b11Ted Kremenek  C.Deallocate(this);
1430fb7413f12636cefa9ebec9abf95804f82c305b11Ted Kremenek}
1431fb7413f12636cefa9ebec9abf95804f82c305b11Ted Kremenek
143277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
143305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//  DesignatedInitExpr
143405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
143505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
143605c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorIdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
143705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(Kind == FieldDesignator && "Only valid on a field designator");
143805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Field.NameOrField & 0x01)
143905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
144005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  else
144105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return getField()->getIdentifier();
144205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
144305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
144405c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr *
144505c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
144605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           unsigned NumDesignators,
144705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           Expr **IndexExprs, unsigned NumIndexExprs,
144805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           SourceLocation ColonOrEqualLoc,
144905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           bool UsesColonSyntax, Expr *Init) {
1450c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1451c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff                         sizeof(Designator) * NumDesignators +
1452c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
145305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  DesignatedInitExpr *DIE
145405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
145505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                   ColonOrEqualLoc, UsesColonSyntax,
145605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                   NumIndexExprs + 1);
145705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
145805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Fill in the designators
145905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  unsigned ExpectedNumSubExprs = 0;
146005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  designators_iterator Desig = DIE->designators_begin();
146105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
146205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
146305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (Designators[Idx].isArrayDesignator())
146405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ++ExpectedNumSubExprs;
146505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else if (Designators[Idx].isArrayRangeDesignator())
146605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ExpectedNumSubExprs += 2;
146705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
146805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
146905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
147005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Fill in the subexpressions, including the initializer expression.
147105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  child_iterator Child = DIE->child_begin();
147205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  *Child++ = Init;
147305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
147405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    *Child = IndexExprs[Idx];
147505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
147605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return DIE;
147705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
147805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
147905c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorSourceRange DesignatedInitExpr::getSourceRange() const {
148005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation StartLoc;
148105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Designator &First = *const_cast<DesignatedInitExpr*>(this)->designators_begin();
148205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (First.isFieldDesignator()) {
148305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (UsesColonSyntax)
148405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
148505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else
148605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
148705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  } else
148805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    StartLoc = SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
148905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
149005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
149105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
149205c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_begin() {
149305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
149405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
149505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return static_cast<Designator*>(static_cast<void*>(Ptr));
149605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
149705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
149805c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
149905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return designators_begin() + NumDesignators;
150005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
150105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
150205c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
150305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
150405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
150505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
150605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(Designator) * NumDesignators;
150705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
150805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
150905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
151005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
151105c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
151205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayRangeDesignator &&
151305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
151405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
151505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
151605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(Designator) * NumDesignators;
151705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
151805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
151905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
152005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
152105c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
152205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayRangeDesignator &&
152305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
152405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
152505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
152605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(Designator) * NumDesignators;
152705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
152805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
152905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
153005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
153105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
1532ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//  ExprIterator.
1533ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
1534ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
1535ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1536ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1537ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1538ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator[](size_t idx) const {
1539ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  return cast<Expr>(I[idx]);
1540ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek}
1541ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1542ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1543ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
1544ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
154577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//  Child Iterators for iterating over subexpressions/substatements
154677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
154777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
154877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// DeclRefExpr
15499ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
15509ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
155177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
15527779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff// ObjCIvarRefExpr
15535549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
15545549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
15557779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff
1556e3e9add4fd788927df6f545570e7838db59c01d7Steve Naroff// ObjCPropertyRefExpr
15575549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
15585549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
1559ae7840776d6cd31b4d7a4a345b61bcbb3744df6cSteve Naroff
15605daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian// ObjCKVCRefExpr
15615daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz JahanianStmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
15625daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz JahanianStmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
15635daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian
1564cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas Gregor// ObjCSuperExpr
1565cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas GregorStmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1566cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas GregorStmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1567cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas Gregor
1568d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner// PredefinedExpr
1569d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1570d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
157177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
157277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// IntegerLiteral
15739ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
15749ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
157577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
157677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CharacterLiteral
15779ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
15789ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
157977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
158077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// FloatingLiteral
15819ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
15829ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
158377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
15845d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner// ImaginaryLiteral
15855549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
15865549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
15875d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner
158877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// StringLiteral
15899ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
15909ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
159177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
159277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ParenExpr
15935549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_begin() { return &Val; }
15945549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_end() { return &Val+1; }
159577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
159677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// UnaryOperator
15975549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_begin() { return &Val; }
15985549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
159977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
16000518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl// SizeOfAlignOfExpr
16010518999d3adcc289997bd974dce90cc97f5c1c44Sebastian RedlStmt::child_iterator SizeOfAlignOfExpr::child_begin() {
16020518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // If this is of a type and the type is a VLA type (and not a typedef), the
16030518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // size expression of the VLA needs to be treated as an executable expression.
16040518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // Why isn't this weirdness documented better in StmtIterator?
16050518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType()) {
16060518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    if (VariableArrayType* T = dyn_cast<VariableArrayType>(
16070518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl                                   getArgumentType().getTypePtr()))
16080518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl      return child_iterator(T);
16090518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    return child_iterator();
16100518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  }
1611d457589fc69dc7a9c80cd74d317c0b81a35a27c9Sebastian Redl  return child_iterator(&Argument.Ex);
16129ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
16130518999d3adcc289997bd974dce90cc97f5c1c44Sebastian RedlStmt::child_iterator SizeOfAlignOfExpr::child_end() {
16140518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType())
16150518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    return child_iterator();
1616d457589fc69dc7a9c80cd74d317c0b81a35a27c9Sebastian Redl  return child_iterator(&Argument.Ex + 1);
16179ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
161877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
161977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ArraySubscriptExpr
16201237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_begin() {
16215549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
162277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
16231237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_end() {
16245549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+END_EXPR;
162577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
162677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
162777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CallExpr
16281237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_begin() {
16295549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
163077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
16311237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_end() {
16325549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+NumArgs+ARGS_START;
163377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
16341237c673c07f9d827129ba02720108816abde562Ted Kremenek
16351237c673c07f9d827129ba02720108816abde562Ted Kremenek// MemberExpr
16365549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_begin() { return &Base; }
16375549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_end() { return &Base+1; }
16381237c673c07f9d827129ba02720108816abde562Ted Kremenek
1639213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman// ExtVectorElementExpr
16405549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
16415549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
16421237c673c07f9d827129ba02720108816abde562Ted Kremenek
16431237c673c07f9d827129ba02720108816abde562Ted Kremenek// CompoundLiteralExpr
16445549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
16455549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
16461237c673c07f9d827129ba02720108816abde562Ted Kremenek
16471237c673c07f9d827129ba02720108816abde562Ted Kremenek// CastExpr
16485549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_begin() { return &Op; }
16495549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_end() { return &Op+1; }
16501237c673c07f9d827129ba02720108816abde562Ted Kremenek
16511237c673c07f9d827129ba02720108816abde562Ted Kremenek// BinaryOperator
16521237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_begin() {
16535549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
16541237c673c07f9d827129ba02720108816abde562Ted Kremenek}
16551237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_end() {
16565549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+END_EXPR;
16571237c673c07f9d827129ba02720108816abde562Ted Kremenek}
16581237c673c07f9d827129ba02720108816abde562Ted Kremenek
16591237c673c07f9d827129ba02720108816abde562Ted Kremenek// ConditionalOperator
16601237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_begin() {
16615549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
16621237c673c07f9d827129ba02720108816abde562Ted Kremenek}
16631237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_end() {
16645549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+END_EXPR;
16651237c673c07f9d827129ba02720108816abde562Ted Kremenek}
16661237c673c07f9d827129ba02720108816abde562Ted Kremenek
16671237c673c07f9d827129ba02720108816abde562Ted Kremenek// AddrLabelExpr
16689ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
16699ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
16701237c673c07f9d827129ba02720108816abde562Ted Kremenek
16711237c673c07f9d827129ba02720108816abde562Ted Kremenek// StmtExpr
16725549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
16735549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
16741237c673c07f9d827129ba02720108816abde562Ted Kremenek
16751237c673c07f9d827129ba02720108816abde562Ted Kremenek// TypesCompatibleExpr
16769ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_begin() {
16779ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
16789ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
16799ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek
16809ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_end() {
16819ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
16829ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
16831237c673c07f9d827129ba02720108816abde562Ted Kremenek
16841237c673c07f9d827129ba02720108816abde562Ted Kremenek// ChooseExpr
16855549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
16865549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
16872d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
16882d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor// GNUNullExpr
16892d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas GregorStmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
16902d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas GregorStmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
16911237c673c07f9d827129ba02720108816abde562Ted Kremenek
1692e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman// OverloadExpr
16935549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
16945549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
1695e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman
1696d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman// ShuffleVectorExpr
1697d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_begin() {
16985549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
1699d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman}
1700d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_end() {
17015549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+NumExprs;
1702d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman}
1703d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman
17047c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson// VAArgExpr
17055549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_begin() { return &Val; }
17065549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
17077c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson
170866b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson// InitListExpr
170966b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonStmt::child_iterator InitListExpr::child_begin() {
17105549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return InitExprs.size() ? &InitExprs[0] : 0;
171166b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
171266b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonStmt::child_iterator InitListExpr::child_end() {
17135549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
171466b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
171566b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson
17163498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor// DesignatedInitExpr
171705c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorStmt::child_iterator DesignatedInitExpr::child_begin() {
171805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
171905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
172005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(Designator) * NumDesignators;
172105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
172205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
172305c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorStmt::child_iterator DesignatedInitExpr::child_end() {
172405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return child_iterator(&*child_begin() + NumSubExprs);
172505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
172605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
17273498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor// ImplicitValueInitExpr
17283498bdb9e9cb300de74c7b51c92608e2902b2348Douglas GregorStmt::child_iterator ImplicitValueInitExpr::child_begin() {
17293498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  return child_iterator();
17303498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor}
17313498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
17323498bdb9e9cb300de74c7b51c92608e2902b2348Douglas GregorStmt::child_iterator ImplicitValueInitExpr::child_end() {
17333498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  return child_iterator();
17343498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor}
17353498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
17361237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCStringLiteral
17379ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_begin() {
17389ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
17399ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
17409ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_end() {
17419ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
17429ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
17431237c673c07f9d827129ba02720108816abde562Ted Kremenek
17441237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCEncodeExpr
17459ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
17469ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
17471237c673c07f9d827129ba02720108816abde562Ted Kremenek
1748b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian// ObjCSelectorExpr
17499ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_begin() {
17509ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
17519ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
17529ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_end() {
17539ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
17549ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
1755b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
1756390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian// ObjCProtocolExpr
17579ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_begin() {
17589ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
17599ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
17609ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_end() {
17619ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
17629ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
1763390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
1764563477da25f824e37c535131695dc4dc9b68c465Steve Naroff// ObjCMessageExpr
1765ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted KremenekStmt::child_iterator ObjCMessageExpr::child_begin() {
17665549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
1767563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
1768563477da25f824e37c535131695dc4dc9b68c465Steve NaroffStmt::child_iterator ObjCMessageExpr::child_end() {
17695549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+ARGS_START+getNumArgs();
1770563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
1771563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
17724eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff// Blocks
177356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
177456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
17754eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
17769da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
17779da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }
1778