Expr.cpp revision 207c5210eb0ac7b632609f0c006eb97ef2738948
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
402085fd6cd22ec5c268175251db10d7c60caf7aaaChris LattnerStringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
412085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                                     unsigned ByteLength, bool Wide,
422085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                                     QualType Ty,
432085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                                     SourceLocation *Loc, unsigned NumStrs) {
442085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // Allocate enough space for the StringLiteral plus an array of locations for
452085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // any concatenated string tokens.
462085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  void *Mem = C.Allocate(sizeof(StringLiteral)+
472085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                         sizeof(SourceLocation)*(NumStrs-1),
482085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                         llvm::alignof<StringLiteral>());
492085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  StringLiteral *SL = new (Mem) StringLiteral(Ty);
502085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // OPTIMIZE: could allocate this appended to the StringLiteral.
522085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  char *AStrData = new (C, 1) char[ByteLength];
532085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  memcpy(AStrData, StrData, ByteLength);
542085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->StrData = AStrData;
552085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->ByteLength = ByteLength;
562085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->IsWide = Wide;
572085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->TokLocs[0] = Loc[0];
582085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->NumConcatenated = NumStrs;
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
60726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner  if (NumStrs != 1)
612085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner    memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
622085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  return SL;
63726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner}
64726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner
65726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner
666e94ef5696cfb005d3fc7bbac8dcf7690b64f0a5Ted Kremenekvoid StringLiteral::Destroy(ASTContext &C) {
678189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  C.Deallocate(const_cast<char*>(StrData));
68353ffceafc6bcebd5592cb9d93ea3f9242e5370aTed Kremenek  this->~StringLiteral();
69353ffceafc6bcebd5592cb9d93ea3f9242e5370aTed Kremenek  C.Deallocate(this);
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "sizeof" or "[pre]++".
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *UnaryOperator::getOpcodeStr(Opcode Op) {
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown unary operator");
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case PostInc: return "++";
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case PostDec: return "--";
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case PreInc:  return "++";
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case PreDec:  return "--";
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case AddrOf:  return "&";
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Deref:   return "*";
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Plus:    return "+";
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Minus:   return "-";
855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Not:     return "~";
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LNot:    return "!";
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Real:    return "__real";
885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Imag:    return "__imag";
895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Extension: return "__extension__";
9073d0d4fac161ed12926e010dcf8b448a8de6a2ecChris Lattner  case OffsetOf: return "__builtin_offsetof";
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Postfix Operators.
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
98668bf91d31265b6ea8c3eb854ba450857701f269Ted KremenekCallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
998189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                   unsigned numargs, QualType t, SourceLocation rparenloc)
100898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  : Expr(SC, t,
101898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
102d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner         fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
103898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    NumArgs(numargs) {
104668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
105668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  SubExprs = new (C) Stmt*[numargs+1];
106b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  SubExprs[FN] = fn;
107b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  for (unsigned i = 0; i != numargs; ++i)
108b4609806e9232593ece09ce08b630836e825865cDouglas Gregor    SubExprs[i+ARGS_START] = args[i];
109668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
110b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  RParenLoc = rparenloc;
111b4609806e9232593ece09ce08b630836e825865cDouglas Gregor}
112e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman
113668bf91d31265b6ea8c3eb854ba450857701f269Ted KremenekCallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
114668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek                   QualType t, SourceLocation rparenloc)
115898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  : Expr(CallExprClass, t,
116898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
117d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner         fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
118898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    NumArgs(numargs) {
119668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
120668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  SubExprs = new (C) Stmt*[numargs+1];
12177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek  SubExprs[FN] = fn;
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != numargs; ++i)
12377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek    SubExprs[i+ARGS_START] = args[i];
124668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  RParenLoc = rparenloc;
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
128668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenekvoid CallExpr::Destroy(ASTContext& C) {
129668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  DestroyChildren(C);
130668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  if (SubExprs) C.Deallocate(SubExprs);
131668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  this->~CallExpr();
132668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  C.Deallocate(this);
133668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek}
134668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
135d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// setNumArgs - This changes the number of arguments present in this call.
136d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// Any orphaned expressions are deleted by this, and any new operands are set
137d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// to null.
1388189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenekvoid CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
139d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // No change, just return.
140d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs == getNumArgs()) return;
141d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
142d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // If shrinking # arguments, just delete the extras and forgot them.
143d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs < getNumArgs()) {
144d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
1458189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek      getArg(i)->Destroy(C);
146d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    this->NumArgs = NumArgs;
147d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    return;
148d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  }
149d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
150d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Otherwise, we are growing the # arguments.  New an bigger argument array.
1515549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  Stmt **NewSubExprs = new Stmt*[NumArgs+1];
152d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Copy over args.
153d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
154d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = SubExprs[i];
155d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Null out new args.
156d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
157d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = 0;
158d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
1598189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  delete [] SubExprs;
160d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  SubExprs = NewSubExprs;
161d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  this->NumArgs = NumArgs;
162d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner}
163d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
164cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// isBuiltinCall - If this is a call to a builtin, return the builtin ID.  If
165cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// not, return 0.
1663c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregorunsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
167c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // All simple function calls (e.g. func()) are implicitly cast to pointer to
168c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // function. As a result, we try and obtain the DeclRefExpr from the
169c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // ImplicitCastExpr.
170c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
171c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
172cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
173cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
174c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
175c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!DRE)
176cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
177cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
178bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
179bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  if (!FDecl)
180cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
181cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
1824fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor  if (!FDecl->getIdentifier())
1834fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor    return 0;
1844fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor
1853c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  return FDecl->getBuiltinID(Context);
186cb888967400a03504c88acedd5248d6778a82f46Chris Lattner}
187bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson
188cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "<<=".
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *BinaryOperator::getOpcodeStr(Opcode Op) {
1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown binary operator");
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Mul:       return "*";
1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Div:       return "/";
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Rem:       return "%";
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Add:       return "+";
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Sub:       return "-";
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Shl:       return "<<";
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Shr:       return ">>";
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LT:        return "<";
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case GT:        return ">";
2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LE:        return "<=";
2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case GE:        return ">=";
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case EQ:        return "==";
2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case NE:        return "!=";
2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case And:       return "&";
2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Xor:       return "^";
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Or:        return "|";
2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LAnd:      return "&&";
2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LOr:       return "||";
2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Assign:    return "=";
2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case MulAssign: return "*=";
2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DivAssign: return "/=";
2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case RemAssign: return "%=";
2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case AddAssign: return "+=";
2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case SubAssign: return "-=";
2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ShlAssign: return "<<=";
2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ShrAssign: return ">>=";
2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case AndAssign: return "&=";
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case XorAssign: return "^=";
2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case OrAssign:  return "|=";
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Comma:     return ",";
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22766b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonInitListExpr::InitListExpr(SourceLocation lbraceloc,
228418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner                           Expr **initExprs, unsigned numInits,
2294c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                           SourceLocation rbraceloc)
230c5ae899b4bbf65488445316c63168079177db0edSteve Naroff  : Expr(InitListExprClass, QualType()),
2310bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
232a9c878086036de36482cc21e35a33cabe9699b0aDouglas Gregor    UnionFieldInit(0), HadArrayRangeDesignator(false) {
233418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner
234418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner  InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
23566b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregorvoid InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
238d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner  for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
239f592c929bd1f083abcc8199b24bb825fdd28df1cDaniel Dunbar       Idx < LastIdx; ++Idx)
2404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    delete InitExprs[Idx];
2414c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitExprs.resize(NumInits, 0);
2424c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
2434c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
2444c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas GregorExpr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
2454c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (Init >= InitExprs.size()) {
2464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
2474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    InitExprs.back() = expr;
2484c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return 0;
2494c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
2504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
2514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
2524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitExprs[Init] = expr;
2534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
2544c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
2554c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
256bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block.
2574eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
2584eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroffconst FunctionType *BlockExpr::getFunctionType() const {
2594eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  return getType()->getAsBlockPointerType()->
2604eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff                    getPointeeType()->getAsFunctionType();
2614eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
2624eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
26356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffSourceLocation BlockExpr::getCaretLocation() const {
26456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  return TheBlock->getCaretLocation();
26556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
26656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroffconst Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
26756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt *BlockExpr::getBody() { return TheBlock->getBody(); }
26856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
26956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
274026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// isUnusedResultAWarning - Return true if this immediate expression should
275026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// be warned about if the result is unused.  If so, fill in Loc and Ranges
276026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// with location to warn on and the source range[s] to report with the
277026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// warning.
278026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattnerbool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
279026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner                                  SourceRange &R2) const {
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
282026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = getExprLoc();
283026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
284026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass:
286026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return cast<ParenExpr>(this)->getSubExpr()->
287026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      isUnusedResultAWarning(Loc, R1, R2);
2885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UnaryOperatorClass: {
2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const UnaryOperator *UO = cast<UnaryOperator>(this);
2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (UO->getOpcode()) {
292026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    default: break;
2935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::PostInc:
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::PostDec:
2955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::PreInc:
296026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    case UnaryOperator::PreDec:                 // ++/--
297026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;  // Not a warning.
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Deref:
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Dereferencing a volatile pointer is a side-effect.
300026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      if (getType().isVolatileQualified())
301026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
302026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Real:
3045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Imag:
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // accessing a piece of a volatile complex is a side-effect.
306026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      if (UO->getSubExpr()->getType().isVolatileQualified())
307026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
308026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Extension:
310026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
312026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = UO->getOperatorLoc();
313026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = UO->getSubExpr()->getSourceRange();
314026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
316e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  case BinaryOperatorClass: {
317026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const BinaryOperator *BO = cast<BinaryOperator>(this);
318026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // Consider comma to have side effects if the LHS or RHS does.
319026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (BO->getOpcode() == BinaryOperator::Comma)
320026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
321026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner             BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
322e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner
323026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (BO->isAssignmentOp())
324026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
325026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = BO->getOperatorLoc();
326026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = BO->getLHS()->getSourceRange();
327026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = BO->getRHS()->getSourceRange();
328026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
329e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  }
330eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner  case CompoundAssignOperatorClass:
331026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
333ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  case ConditionalOperatorClass: {
334026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // The condition must be evaluated, but if either the LHS or RHS is a
335026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // warning, warn about them.
336ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
337026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
338026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return true;
339026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
340ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  }
341ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case MemberExprClass:
343026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If the base pointer or element is to a volatile pointer/field, accessing
344026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // it is a side effect.
345026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (getType().isVolatileQualified())
346026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
347026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<MemberExpr>(this)->getMemberLoc();
348026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = SourceRange(Loc, Loc);
349026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
350026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
351026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ArraySubscriptExprClass:
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If the base pointer or element is to a volatile pointer/field, accessing
354026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // it is a side effect.
355026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (getType().isVolatileQualified())
356026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
357026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
358026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
359026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
360026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
361211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case CallExprClass:
363026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner  case CXXOperatorCallExprClass: {
364026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If this is a direct call, get the callee.
365026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const CallExpr *CE = cast<CallExpr>(this);
366026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
367026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
368026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // If the callee has attribute pure, const, or warn_unused_result, warn
369026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // about it. void foo() { strlen("bar"); } should warn.
370026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
371026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        if (FD->getAttr<WarnUnusedResultAttr>() ||
372026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner            FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
373026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner          Loc = CE->getCallee()->getLocStart();
374026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner          R1 = CE->getCallee()->getSourceRange();
375026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner
376026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner          if (unsigned NumArgs = CE->getNumArgs())
377026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner            R2 = SourceRange(CE->getArg(0)->getLocStart(),
378026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner                             CE->getArg(NumArgs-1)->getLocEnd());
379026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner          return true;
380026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        }
381026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    }
382026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
383026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner  }
384a9c01021724b9b546d282b8609cbe559734812ecChris Lattner  case ObjCMessageExprClass:
385026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
386611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  case StmtExprClass: {
387611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // Statement exprs don't logically have side effects themselves, but are
388611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // sometimes used in macros in ways that give them a type that is unused.
389611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // For example ({ blah; foo(); }) will end up with a type if foo has a type.
390611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // however, if the result of the stmt expr is dead, we don't want to emit a
391611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // warning.
392611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
393611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    if (!CS->body_empty())
394611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner      if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
395026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return E->isUnusedResultAWarning(Loc, R1, R2);
396026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner
397026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<StmtExpr>(this)->getLParenLoc();
398026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
399026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
400611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  }
4016eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor  case CStyleCastExprClass:
402026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If this is a cast to void, check the operand.  Otherwise, the result of
403026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // the cast is unused.
404026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (getType()->isVoidType())
405026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
406026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner                                                                        R1, R2);
407026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
408026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
409026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
410987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case CXXFunctionalCastExprClass:
4115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is a cast to void, check the operand.  Otherwise, the result of
4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // the cast is unused.
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getType()->isVoidType())
414026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
415026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner                                                                        R1, R2);
416026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
417026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
418026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
419026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner
4204be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman  case ImplicitCastExprClass:
4214be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman    // Check the operand, since implicit casts are inserted by Sema
422026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return cast<ImplicitCastExpr>(this)
423026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
4244be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman
42504421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
426026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return cast<CXXDefaultArgExpr>(this)
427026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
4284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
4294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXNewExprClass:
4304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // FIXME: In theory, there might be new expressions that don't have side
4314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // effects (e.g. a placement new with an uninitialized POD).
4324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXDeleteExprClass:
433026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
4344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
437ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor/// DeclCanBeLvalue - Determine whether the given declaration can be
438ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor/// an lvalue. This is a helper routine for isLvalue.
439ba7e210a999275695f58be03ef402758cfec3635Douglas Gregorstatic bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
44072c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor  // C++ [temp.param]p6:
44172c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor  //   A non-type non-reference template-parameter is not an lvalue.
44272c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor  if (const NonTypeTemplateParmDecl *NTTParm
44372c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor        = dyn_cast<NonTypeTemplateParmDecl>(Decl))
44472c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor    return NTTParm->getType()->isReferenceType();
44572c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor
44644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
447ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor    // C++ 3.10p2: An lvalue refers to an object or function.
448ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor    (Ctx.getLangOptions().CPlusPlus &&
449ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor     (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
450ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor}
451ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor
4525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
4535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// incomplete type other than void. Nonarray expressions that can be lvalues:
4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - name, where name must be a variable
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - e[i]
4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - (e), where e must be an lvalue
4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - e.name, where e must be an lvalue
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - e->name
4595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - *e, the type of e cannot be a function type
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///  - string-constant
4617da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner///  - (__real__ e) and (__imag__ e) where e is an lvalue  [GNU extension]
46208ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling///  - reference type [C++ [expr]]
4635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
46428be73f74c9e241a23ea24fe5756623de6bf1084Chris LattnerExpr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
46598cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor  // first, check the type (C99 6.3.2.1). Expressions with function
46698cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor  // type in C are not lvalues, but they can be lvalues in C++.
46798cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor  if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
4685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LV_NotObjectType;
4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
470acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff  // Allow qualified void which is an incomplete type other than void (yuck).
47128be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner  if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
472acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff    return LV_IncompleteVoidType;
473acb818a4f7d9b608826171094d6b5a555a8fe694Steve Naroff
47498cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor  /// FIXME: Expressions can't have reference type, so the following
47598cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor  /// isn't needed.
476cb4f9a6d0be445d84af35ba81323be6127816f9eChris Lattner  if (TR->isReferenceType()) // C++ [expr]
47708ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling    return LV_Valid;
47808ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling
4795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the type looks fine, now check the expression
4805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
4815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case StringLiteralClass: // C99 6.5.1p4
4827323a6297edad643c202594dcf3d9a174de96ca6Anders Carlsson    return LV_Valid;
4835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
4845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // For vectors, make sure base is an lvalue (i.e. not a function call).
4855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
48628be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner      return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
4875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LV_Valid;
4881a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor  case DeclRefExprClass:
4891a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor  case QualifiedDeclRefExprClass: { // C99 6.5.1p2
490ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor    const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
491ba7e210a999275695f58be03ef402758cfec3635Douglas Gregor    if (DeclCanBeLvalue(RefdDecl, Ctx))
4925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LV_Valid;
4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
4944111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  }
495dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff  case BlockDeclRefExprClass: {
496dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff    const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
4974f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff    if (isa<VarDecl>(BDR->getDecl()))
498dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff      return LV_Valid;
499dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff    break;
500dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff  }
50186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor  case MemberExprClass: {
5025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const MemberExpr *m = cast<MemberExpr>(this);
50386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
50486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      NamedDecl *Member = m->getMemberDecl();
50586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      // C++ [expr.ref]p4:
50686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   If E2 is declared to have type "reference to T", then E1.E2
50786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   is an lvalue.
50886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
50986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        if (Value->getType()->isReferenceType())
51086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor          return LV_Valid;
51186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
51286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   -- If E2 is a static data member [...] then E1.E2 is an lvalue.
51386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      if (isa<CXXClassVarDecl>(Member))
51486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        return LV_Valid;
51586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
51686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   -- If E2 is a non-static data member [...]. If E1 is an
51786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //      lvalue, then E1.E2 is an lvalue.
51886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      if (isa<FieldDecl>(Member))
51986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
52086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
52186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   -- If it refers to a static member function [...], then
52286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //      E1.E2 is an lvalue.
52386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   -- Otherwise, if E1.E2 refers to a non-static member
52486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //      function [...], then E1.E2 is not an lvalue.
52586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
52686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        return Method->isStatic()? LV_Valid : LV_MemberFunction;
52786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
52886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //   -- If E2 is a member enumerator [...], the expression E1.E2
52986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      //      is not an lvalue.
53086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      if (isa<EnumConstantDecl>(Member))
53186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        return LV_InvalidExpression;
53286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
53386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        // Not an lvalue.
53486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      return LV_InvalidExpression;
53586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    }
53686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
53786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    // C99 6.5.2.3p4
53828be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner    return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
539fdd75663fffeb2058a7847975e50837e61200593Anton Korobeynikov  }
5407da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner  case UnaryOperatorClass:
5415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
5427da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner      return LV_Valid; // C99 6.5.3p4
5437da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner
5447da36f642e907ff5a5ba4b18b5bfebfabf36ecc7Chris Lattner    if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
545baf0d6678418e0dd9309438c3e50274253cfc7b2Chris Lattner        cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
546baf0d6678418e0dd9309438c3e50274253cfc7b2Chris Lattner        cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
54728be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner      return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx);  // GNU.
54874253736184c0717a0649922551bf9d8b6815651Douglas Gregor
54974253736184c0717a0649922551bf9d8b6815651Douglas Gregor    if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
55074253736184c0717a0649922551bf9d8b6815651Douglas Gregor        (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
55174253736184c0717a0649922551bf9d8b6815651Douglas Gregor         cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
55274253736184c0717a0649922551bf9d8b6815651Douglas Gregor      return LV_Valid;
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
554eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  case ImplicitCastExprClass:
555eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor    return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
556eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor                                                       : LV_InvalidExpression;
5575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass: // C99 6.5.1p5
55828be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner    return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
559eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  case BinaryOperatorClass:
560eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  case CompoundAssignOperatorClass: {
561eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor    const BinaryOperator *BinOp = cast<BinaryOperator>(this);
562337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor
563337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor    if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
564337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor        BinOp->getOpcode() == BinaryOperator::Comma)
565337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor      return BinOp->getRHS()->isLvalue(Ctx);
566337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor
567224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl    // C++ [expr.mptr.oper]p6
568224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl    if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
569224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl         BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
570224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl        !BinOp->getType()->isFunctionType())
571224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl      return BinOp->getLHS()->isLvalue(Ctx);
572224605064a4ef87d1c3d35ad1cb363f8b534012bSebastian Redl
573bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor    if (!BinOp->isAssignmentOp())
574eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor      return LV_InvalidExpression;
575eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor
576bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor    if (Ctx.getLangOptions().CPlusPlus)
577bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor      // C++ [expr.ass]p1:
578bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor      //   The result of an assignment operation [...] is an lvalue.
579bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor      return LV_Valid;
580bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor
581bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor
582bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor    // C99 6.5.16:
583bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor    //   An assignment expression [...] is not an lvalue.
584bf3af056289893f58d37b05a2c80970708781d61Douglas Gregor    return LV_InvalidExpression;
585eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  }
586b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  case CallExprClass:
58788a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor  case CXXOperatorCallExprClass:
58888a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor  case CXXMemberCallExprClass: {
5899d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // C++ [expr.call]p10:
5909d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    //   A function call is an lvalue if and only if the result type
5919d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    //   is a reference.
59227c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor    QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
5939d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
59488a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor      CalleeType = FnTypePtr->getPointeeType();
59588a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor    if (const FunctionType *FnType = CalleeType->getAsFunctionType())
59688a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor      if (FnType->getResultType()->isReferenceType())
59788a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor        return LV_Valid;
5989d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor
5999d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    break;
6009d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  }
601e6386394677ed4f77b20e2e3d5446a3a2f628e53Steve Naroff  case CompoundLiteralExprClass: // C99 6.5.2.5p5
602e6386394677ed4f77b20e2e3d5446a3a2f628e53Steve Naroff    return LV_Valid;
603670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner  case ChooseExprClass:
604670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner    // __builtin_choose_expr is an lvalue if the selected operand is.
605670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner    if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
606670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner      return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
607670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner    else
608670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner      return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
609670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner
610213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case ExtVectorElementExprClass:
611213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
612fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff      return LV_DuplicateVectorComponents;
613fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff    return LV_Valid;
614027282d1c1ac151aa7b1b3b45babc918b8ad456aSteve Naroff  case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
615027282d1c1ac151aa7b1b3b45babc918b8ad456aSteve Naroff    return LV_Valid;
616799a6a6850af625946bb8d88ca960bb6604e3858Steve Naroff  case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
617799a6a6850af625946bb8d88ca960bb6604e3858Steve Naroff    return LV_Valid;
6185daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian  case ObjCKVCRefExprClass: // FIXME: check if read-only property.
619670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattner    return LV_Valid;
620d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner  case PredefinedExprClass:
621796da18402f286b897782a298ae3b20c459c102eDouglas Gregor    return LV_Valid;
6229d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case VAArgExprClass:
623adadd8db2aba32f689e7057d8b7ce004be30685bDaniel Dunbar    return LV_NotObjectType;
62404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
62528be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner    return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
62624b41fa8239c63b9eb570d3e83c4a82840656a65Argyrios Kyrtzidis  case CXXConditionDeclExprClass:
62724b41fa8239c63b9eb570d3e83c4a82840656a65Argyrios Kyrtzidis    return LV_Valid;
6286eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor  case CStyleCastExprClass:
6299d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case CXXFunctionalCastExprClass:
6309d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case CXXStaticCastExprClass:
6319d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case CXXDynamicCastExprClass:
6329d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case CXXReinterpretCastExprClass:
6339d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  case CXXConstCastExprClass:
6349d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // The result of an explicit cast is an lvalue if the type we are
6359d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // casting to is a reference type. See C++ [expr.cast]p1,
6369d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
6379d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
6389d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
6399d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor      return LV_Valid;
6409d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    break;
641c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl  case CXXTypeidExprClass:
642c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
643c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl    return LV_Valid;
6445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
6455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
6465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return LV_InvalidExpression;
6485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
6515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// does not have an incomplete type, does not have a const-qualified type, and
6525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// if it is a structure or union, does not have any member (including,
6535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// recursively, any member or element of all contained aggregates or unions)
6545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// with a const-qualified type.
65528be73f74c9e241a23ea24fe5756623de6bf1084Chris LattnerExpr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
65628be73f74c9e241a23ea24fe5756623de6bf1084Chris Lattner  isLvalueResult lvalResult = isLvalue(Ctx);
6575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (lvalResult) {
659ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor  case LV_Valid:
660ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor    // C++ 3.10p11: Functions cannot be modified, but pointers to
661ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor    // functions can be modifiable.
662ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor    if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
663ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor      return MLV_NotObjectType;
664ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor    break;
665ae8d467e75a4e72b19e1eca199bf93dfaab47acfDouglas Gregor
6665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LV_NotObjectType: return MLV_NotObjectType;
6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
668fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
669ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner  case LV_InvalidExpression:
670ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    // If the top level is a C-style cast, and the subexpression is a valid
671ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    // lvalue, then this is probably a use of the old-school "cast as lvalue"
672ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    // GCC extension.  We don't support it, but we want to produce good
673ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    // diagnostics when it happens so that the user knows why.
674ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
675ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner      if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
676ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner        return MLV_LValueCast;
677ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    return MLV_InvalidExpression;
67886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor  case LV_MemberFunction: return MLV_MemberFunction;
6795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
680c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
681c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType CT = Ctx.getCanonicalType(getType());
682c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
683c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (CT.isConstQualified())
6845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return MLV_ConstQualified;
685c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (CT->isArrayType())
6865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return MLV_ArrayType;
687c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (CT->isIncompleteType())
6885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return MLV_IncompleteType;
6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
690c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const RecordType *r = CT->getAsRecordType()) {
6915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (r->hasConstFields())
6925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return MLV_ConstQualified;
6935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6944f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  // The following is illegal:
6954f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  //   void takeclosure(void (^C)(void));
6964f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  //   void func() { int x = 1; takeclosure(^{ x = 7 }); }
6974f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  //
6984f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  if (getStmtClass() == BlockDeclRefExprClass) {
6994f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff    const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
7004f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff    if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
7014f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff      return MLV_NotBlockQualified;
7024f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff  }
703d1fa6449e9dbdd667466e9e1e971aa17c9793e8aFariborz Jahanian
704ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian  // Assigning to an 'implicit' property?
7056669db9d80d77d10f101aa9f8e488bbd2d98f76cFariborz Jahanian  else if (getStmtClass() == ObjCKVCRefExprClass) {
706ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian    const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
707ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian    if (KVCExpr->getSetterMethod() == 0)
708ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian      return MLV_NoSetterProperty;
709ba8d2d684e74a20bef03828c21c991d222c7e9e5Fariborz Jahanian  }
7105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return MLV_Valid;
7115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7132e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek/// hasGlobalStorage - Return true if this expression has static storage
7144cc627111453b75519d5130b57e06256da7b00e8Chris Lattner/// duration.  This means that the address of this expression is a link-time
7154cc627111453b75519d5130b57e06256da7b00e8Chris Lattner/// constant.
7162e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenekbool Expr::hasGlobalStorage() const {
7171d09ecca892185ba067e47ba879f424de59950efChris Lattner  switch (getStmtClass()) {
7181d09ecca892185ba067e47ba879f424de59950efChris Lattner  default:
7191d09ecca892185ba067e47ba879f424de59950efChris Lattner    return false;
7204cc627111453b75519d5130b57e06256da7b00e8Chris Lattner  case ParenExprClass:
7212e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek    return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
7224cc627111453b75519d5130b57e06256da7b00e8Chris Lattner  case ImplicitCastExprClass:
7232e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek    return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
724e9b12198c4cc7f5687960100351b4af006c14469Steve Naroff  case CompoundLiteralExprClass:
725e9b12198c4cc7f5687960100351b4af006c14469Steve Naroff    return cast<CompoundLiteralExpr>(this)->isFileScope();
7261a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor  case DeclRefExprClass:
7271a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor  case QualifiedDeclRefExprClass: {
7281d09ecca892185ba067e47ba879f424de59950efChris Lattner    const Decl *D = cast<DeclRefExpr>(this)->getDecl();
7291d09ecca892185ba067e47ba879f424de59950efChris Lattner    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
7302e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek      return VD->hasGlobalStorage();
73163f067f5a75ae97b53dc7a6a6530e2c72c8bb7f8Seo Sanghyeon    if (isa<FunctionDecl>(D))
73263f067f5a75ae97b53dc7a6a6530e2c72c8bb7f8Seo Sanghyeon      return true;
7331d09ecca892185ba067e47ba879f424de59950efChris Lattner    return false;
7341d09ecca892185ba067e47ba879f424de59950efChris Lattner  }
735fb7080663a0f0abd687a6f58ac917d9a76318b73Chris Lattner  case MemberExprClass: {
7361d09ecca892185ba067e47ba879f424de59950efChris Lattner    const MemberExpr *M = cast<MemberExpr>(this);
7372e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek    return !M->isArrow() && M->getBase()->hasGlobalStorage();
738fb7080663a0f0abd687a6f58ac917d9a76318b73Chris Lattner  }
7394cc627111453b75519d5130b57e06256da7b00e8Chris Lattner  case ArraySubscriptExprClass:
7402e5f54aa1dd15a62c34a9d1d24a5a0692f43934aTed Kremenek    return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
741d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner  case PredefinedExprClass:
742fa28b30d5a1e93e5263be33e343532b900d2c643Chris Lattner    return true;
74304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
74404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
7451d09ecca892185ba067e47ba879f424de59950efChris Lattner  }
7461d09ecca892185ba067e47ba879f424de59950efChris Lattner}
7471d09ecca892185ba067e47ba879f424de59950efChris Lattner
74844baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian/// isOBJCGCCandidate - Check if an expression is objc gc'able.
74944baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian///
75044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanianbool Expr::isOBJCGCCandidate() const {
75144baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  switch (getStmtClass()) {
75244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  default:
75344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
75444baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ObjCIvarRefExprClass:
75544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return true;
756207c5210eb0ac7b632609f0c006eb97ef2738948Fariborz Jahanian  case Expr::UnaryOperatorClass:
757207c5210eb0ac7b632609f0c006eb97ef2738948Fariborz Jahanian    return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
75844baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ParenExprClass:
75944baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
76044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ImplicitCastExprClass:
76144baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
76244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case DeclRefExprClass:
76344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case QualifiedDeclRefExprClass: {
76444baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    const Decl *D = cast<DeclRefExpr>(this)->getDecl();
76544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
76644baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian      return VD->hasGlobalStorage();
76744baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
76844baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
76944baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case MemberExprClass: {
77044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    const MemberExpr *M = cast<MemberExpr>(this);
77144baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
77244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
77344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ArraySubscriptExprClass:
77444baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
77544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
77644baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian}
7774e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() {
7784e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  Expr* E = this;
7794e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  while (ParenExpr* P = dyn_cast<ParenExpr>(E))
7804e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek    E = P->getSubExpr();
7814e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek
7824e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  return E;
7834e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek}
7844e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek
78556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
78656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand.
78756f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() {
78856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  Expr *E = this;
78956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  while (true) {
79056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner    if (ParenExpr *P = dyn_cast<ParenExpr>(E))
79156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
79256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner    else if (CastExpr *P = dyn_cast<CastExpr>(E))
79356f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
79456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner    else
79556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      return E;
79656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  }
79756f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner}
79856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner
799898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyTypeDependentArguments - Determines if any of the expressions
800898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is type-dependent.
801898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
802898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
803898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isTypeDependent())
804898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
805898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
806898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
807898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
808898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
809898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyValueDependentArguments - Determines if any of the expressions
810898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is value-dependent.
811898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
812898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
813898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isValueDependent())
814898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
815898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
816898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
817898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
818898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
819c9e8f606787b0bc0c3b08e566b87cc1751694168Eli Friedmanbool Expr::isConstantInitializer(ASTContext &Ctx) const {
820c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // This function is attempting whether an expression is an initializer
821c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // which can be evaluated at compile-time.  isEvaluatable handles most
822c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // of the cases, but it can't deal with some initializer-specific
823c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // expressions, and it can't deal with aggregates; we deal with those here,
824c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // and fall back to isEvaluatable for the other cases.
825c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman
8261f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman  // FIXME: This function assumes the variable being assigned to
8271f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman  // isn't a reference type!
8281f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman
829e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  switch (getStmtClass()) {
830c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  default: break;
831e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case StringLiteralClass:
832e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    return true;
83359b5da6d853b4368b984700315adf7b37de05764Nate Begeman  case CompoundLiteralExprClass: {
8341f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // This handles gcc's extension that allows global initializers like
8351f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // "struct x {int x;} x = (struct x) {};".
8361f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This accepts other cases it shouldn't!
83759b5da6d853b4368b984700315adf7b37de05764Nate Begeman    const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
838c9e8f606787b0bc0c3b08e566b87cc1751694168Eli Friedman    return Exp->isConstantInitializer(Ctx);
83959b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
840e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case InitListExprClass: {
8411f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This doesn't deal with fields with reference types correctly.
8421f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This incorrectly allows pointers cast to integers to be assigned
8431f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // to bitfields.
844e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    const InitListExpr *Exp = cast<InitListExpr>(this);
845e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    unsigned numInits = Exp->getNumInits();
846e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    for (unsigned i = 0; i < numInits; i++) {
847c9e8f606787b0bc0c3b08e566b87cc1751694168Eli Friedman      if (!Exp->getInit(i)->isConstantInitializer(Ctx))
848e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson        return false;
849e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    }
850c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    return true;
851e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
8523498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  case ImplicitValueInitExprClass:
8533498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return true;
854c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case ParenExprClass: {
855c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
856c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  }
857c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case UnaryOperatorClass: {
858c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    const UnaryOperator* Exp = cast<UnaryOperator>(this);
859c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    if (Exp->getOpcode() == UnaryOperator::Extension)
860c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman      return Exp->getSubExpr()->isConstantInitializer(Ctx);
861c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
862c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  }
863c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case CStyleCastExprClass:
864c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // Handle casts with a destination that's a struct or union; this
865c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // deals with both the gcc no-op struct cast extension and the
866c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // cast-to-union extension.
867c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    if (getType()->isRecordType())
868c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman      return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
869c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
870e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
871e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson
872c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  return isEvaluatable(Ctx);
87338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff}
87438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIntegerConstantExpr - this recursive routine will test if an expression is
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an integer constant expression. Note: With the introduction of VLA's in
8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// C99 the result of the sizeof operator is no longer always a constant
8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expression. The generalization of the wording to include any subexpression
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
8805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
8815f6b632391e24b08ead3e62ba2e2765e770382edNuno Lopes/// "0 || f()" can be treated as a constant expression. In C90 this expression,
8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// occurring in a context requiring a constant, would have been a constraint
8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// violation. FIXME: This routine currently implements C90 semantics.
8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// To properly implement C99 semantics this routine will need to evaluate
8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expressions involving operators previously mentioned.
8865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// comma, etc
8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// FIXME: This should ext-warn on overflow during evaluation!  ISO C does not
891ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner/// permit this.  This includes things like (int)1e1000
892ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner///
893ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// FIXME: Handle offsetof.  Two things to do:  Handle GCC's __builtin_offsetof
894ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// to support gcc 4.0+  and handle the idiom GCC recognizes with a null pointer
895ce0afc0b09accbf7370b2ba87574b2c4be7fb032Chris Lattner/// cast+dereference.
896590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattnerbool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
897590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner                                 SourceLocation *Loc, bool isEvaluated) const {
8982d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar  if (!isIntegerConstantExprInternal(Result, Ctx, Loc, isEvaluated))
8992d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar    return false;
9002d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar  assert(Result == EvaluateAsInt(Ctx) && "Inconsistent Evaluate() result!");
9012d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar  return true;
9022d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar}
9032d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar
9042d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbarbool Expr::isIntegerConstantExprInternal(llvm::APSInt &Result, ASTContext &Ctx,
9052d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar                                 SourceLocation *Loc, bool isEvaluated) const {
9062d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar
907a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  // Pretest for integral type; some parts of the code crash for types that
908a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  // can't be sized.
909a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  if (!getType()->isIntegralType()) {
910a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman    if (Loc) *Loc = getLocStart();
911a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman    return false;
912a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  }
9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Loc) *Loc = getLocStart();
9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass:
9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return cast<ParenExpr>(this)->getSubExpr()->
919590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner                     isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case IntegerLiteralClass:
9212d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar    // NOTE: getValue() returns an APInt, we must set sign.
9225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Result = cast<IntegerLiteral>(this)->getValue();
923a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar    Result.setIsUnsigned(getType()->isUnsignedIntegerType());
9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
9252eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner  case CharacterLiteralClass: {
9262eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner    const CharacterLiteral *CL = cast<CharacterLiteral>(this);
927a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar    Result = Ctx.MakeIntValue(CL->getValue(), getType());
9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
9292eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner  }
930b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson  case CXXBoolLiteralExprClass: {
931b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson    const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
932a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar    Result = Ctx.MakeIntValue(BL->getValue(), getType());
933b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson    break;
934b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson  }
9357267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis  case CXXZeroInitValueExprClass:
936a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar    Result = Ctx.MakeIntValue(0, getType());
9377267f7832e5f0c7f951765e201c5a2650eb1637bArgyrios Kyrtzidis    break;
9387b658aa315784fa489a32daeacfe400512ce468dSteve Naroff  case TypesCompatibleExprClass: {
9397b658aa315784fa489a32daeacfe400512ce468dSteve Naroff    const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
940ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    // Per gcc docs "this built-in function ignores top level
941ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    // qualifiers".  We need to use the canonical version to properly
942ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    // be able to strip CRV qualifiers from the type.
943ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
944ac620decb68aad1a2cf6c0c191b56d78981d9aaaDaniel Dunbar    QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
945a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar    Result = Ctx.MakeIntValue(Ctx.typesAreCompatible(T0.getUnqualifiedType(),
946a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar                                                     T1.getUnqualifiedType()),
947a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar                              getType());
948389cecc83f33e93c8ba6bf2e8269b8690404416fSteve Naroff    break;
9497b658aa315784fa489a32daeacfe400512ce468dSteve Naroff  }
950b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  case CallExprClass:
951b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  case CXXOperatorCallExprClass: {
95213b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff    const CallExpr *CE = cast<CallExpr>(this);
953a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner
954a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    // If this is a call to a builtin function, constant fold it otherwise
955a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    // reject it.
9563c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    if (CE->isBuiltinCall(Ctx)) {
9571c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson      EvalResult EvalResult;
9581c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson      if (CE->Evaluate(EvalResult, Ctx)) {
9591c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson        assert(!EvalResult.HasSideEffects &&
9601c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson               "Foldable builtin call should not have side effects!");
9611c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson        Result = EvalResult.Val.getInt();
962a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner        break;  // It is a constant, expand it.
963a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner      }
964a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    }
965a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner
96613b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff    if (Loc) *Loc = getLocStart();
96713b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff    return false;
96813b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff  }
9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclRefExprClass:
9701a49af9681c350fef58e677f85ccb9a77e8e9d0aDouglas Gregor  case QualifiedDeclRefExprClass:
9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (const EnumConstantDecl *D =
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result = D->getInitVal();
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
9764a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl    if (Ctx.getLangOptions().CPlusPlus &&
9774a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl        getType().getCVRQualifiers() == QualType::Const) {
9784a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl      // C++ 7.1.5.1p2
9794a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl      //   A variable of non-volatile const-qualified integral or enumeration
9804a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl      //   type initialized by an ICE can be used in ICEs.
9814a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl      if (const VarDecl *Dcl =
9824a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl              dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
9834a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl        if (const Expr *Init = Dcl->getInit())
9844a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl          return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
9854a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl      }
9864a4251b9e719415f30db0f5170abf31296a62225Sebastian Redl    }
9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Loc) *Loc = getLocStart();
9885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UnaryOperatorClass: {
9905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const UnaryOperator *Exp = cast<UnaryOperator>(this);
9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9920518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    // Get the operand value.  If this is offsetof, do not evalute the
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // operand.  This affects C99 6.6p3.
9940518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
9950518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl                        isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
9975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Exp->getOpcode()) {
9995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
10005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // See C99 6.6p3.
10015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
10025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Loc) *Loc = Exp->getOperatorLoc();
10035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
10045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Extension:
100576e773a443be9f006610f46529e07d4c8d857680Chris Lattner      return true;  // FIXME: this is wrong.
10065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::LNot: {
1007a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar      Result = Ctx.MakeIntValue(Result == 0, getType());
10085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Plus:
10115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Minus:
10135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result = -Result;
10145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case UnaryOperator::Not:
10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result = ~Result;
10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10185a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    case UnaryOperator::OffsetOf:
1019a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar      Result = Ctx.MakeIntValue(Exp->evaluateOffsetOf(Ctx), getType());
1020a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar      break;
10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
10235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10240518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  case SizeOfAlignOfExprClass: {
10250518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
10260518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    QualType ArgTy = Exp->getTypeOfArgument();
1027dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
1028dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    // alignof is always an ICE; sizeof is an ICE if and only if
1029dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    // the operand isn't a VLA
1030dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    if (Exp->isSizeOf() && ArgTy->isVariableArrayType()) {
103165383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner      if (Loc) *Loc = Exp->getOperatorLoc();
10325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
103365383479cb2caf0f136f58fecdbdbaf9c497b7a1Chris Lattner    }
10345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1035dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    // Use the Evaluate logic to calculate the value, since the
1036dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    // calculation is non-trivial.
1037dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    Result = EvaluateAsInt(Ctx);
10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BinaryOperatorClass: {
10415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const BinaryOperator *Exp = cast<BinaryOperator>(this);
1042e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    llvm::APSInt LHS, RHS;
1043e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar
1044e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    // Initialize result to have correct signedness and width.
1045a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar    Result = Ctx.MakeIntValue(0, getType());
1046b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The LHS of a constant expr is always evaluated and needed.
1048e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
10505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The short-circuiting &&/|| operators don't necessarily evaluate their
10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // RHS.  Make sure to pass isEvaluated down correctly.
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Exp->isLogicalOp()) {
10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      bool RHSEval;
10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Exp->getOpcode() == BinaryOperator::LAnd)
1056e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar        RHSEval = LHS != 0;
10575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else {
10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
1059e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar        RHSEval = LHS == 0;
10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
10615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1062590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner      if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
10635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                                isEvaluated & RHSEval))
10645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
10655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
1066590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner      if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Exp->getOpcode()) {
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Loc) *Loc = getLocStart();
10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::Mul:
1075e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS * RHS;
10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::Div:
10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (RHS == 0) {
10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (!isEvaluated) break;
10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (Loc) *Loc = getLocStart();
10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1083e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS / RHS;
10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::Rem:
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (RHS == 0) {
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (!isEvaluated) break;
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (Loc) *Loc = getLocStart();
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1091e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS % RHS;
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1093e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::Add: Result = LHS + RHS; break;
1094e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::Sub: Result = LHS - RHS; break;
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::Shl:
1096e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS <<
1097e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar        static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1098e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    break;
10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::Shr:
1100e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS >>
1101e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar        static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1103e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::LT:  Result = LHS < RHS; break;
1104e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::GT:  Result = LHS > RHS; break;
1105e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::LE:  Result = LHS <= RHS; break;
1106e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::GE:  Result = LHS >= RHS; break;
1107e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::EQ:  Result = LHS == RHS; break;
1108e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::NE:  Result = LHS != RHS; break;
1109e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::And: Result = LHS & RHS; break;
1110e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1111e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar    case BinaryOperator::Or:  Result = LHS | RHS; break;
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::LAnd:
1113e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS != 0 && RHS != 0;
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BinaryOperator::LOr:
1116e1226d24d23510e422160eb2413d9bb90de9b144Daniel Dunbar      Result = LHS != 0 || RHS != 0;
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1118b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman
1119b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman    case BinaryOperator::Comma:
1120b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1121b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      // *except* when they are contained within a subexpression that is not
1122b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      // evaluated".  Note that Assignment can never happen due to constraints
1123b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      // on the LHS subexpr, so we don't need to check it here.
1124b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      if (isEvaluated) {
1125b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman        if (Loc) *Loc = getLocStart();
1126b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman        return false;
1127b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      }
1128b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman
1129b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      // The result of the constant expr is the RHS.
1130b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman      Result = RHS;
11312d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar      break;
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1133a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
113726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner  case ImplicitCastExprClass:
11386eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor  case CStyleCastExprClass:
1139987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  case CXXFunctionalCastExprClass: {
11400835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis    const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
11410835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis    SourceLocation CastLoc = getLocStart();
114226dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner
11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.6p6: shall only convert arithmetic types to integer types.
114426dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner    if (!SubExpr->getType()->isArithmeticType() ||
114526dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner        !getType()->isIntegerType()) {
114626dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner      if (Loc) *Loc = SubExpr->getLocStart();
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1149987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner
115098be4943e8dc4f3905629a7102668960873cf863Chris Lattner    uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
1151987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner
11525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Handle simple integer->integer casts.
115326dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner    if (SubExpr->getType()->isIntegerType()) {
115426dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner      if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return false;
115626dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner
115726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner      // Figure out if this is a truncate, extend or noop cast.
115826dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner      // If the input is signed, do a sign extend, noop, or truncate.
1159c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner      if (getType()->isBooleanType()) {
1160c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner        // Conversion to bool compares against zero.
1161a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar        Result = Ctx.MakeIntValue(Result != 0, getType());
1162a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar      } else if (SubExpr->getType()->isSignedIntegerType()) {
116326dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner        Result.sextOrTrunc(DestWidth);
1164a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar        Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1165a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar      } else {  // If the input is unsigned, do a zero extend, noop,
1166a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar                // or truncate.
116726dc7b39267b7d29b35a7887a5b8e54697192192Chris Lattner        Result.zextOrTrunc(DestWidth);
1168a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar        Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1169a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar      }
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Allow floating constants that are the immediate operands of casts or that
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // are parenthesized.
1175a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar    const Expr *Operand = SubExpr->IgnoreParens();
1176987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner
1177987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    // If this isn't a floating literal, we can't handle it.
1178987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1179987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    if (!FL) {
1180987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner      if (Loc) *Loc = Operand->getLocStart();
1181987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner      return false;
1182987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    }
1183c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner
1184c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner    // If the destination is boolean, compare against zero.
1185c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner    if (getType()->isBooleanType()) {
1186a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar      Result = Ctx.MakeIntValue(!FL->getValue().isZero(), getType());
1187c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner      break;
1188c0a356b4fa35bf3f7d6031c229c071eef1a8f7a9Chris Lattner    }
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1190987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    // Determine whether we are converting to unsigned or signed.
1191987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    bool DestSigned = getType()->isSignedIntegerType();
1192ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner
1193ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner    // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1194ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner    // be called multiple times per AST.
1195ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen    uint64_t Space[4];
1196ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen    bool ignored;
1197ccc213f5d4034bb6fcda14cad112d9029a508b66Chris Lattner    (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
1198ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen                                          llvm::APFloat::rmTowardZero,
1199ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen                                          &ignored);
1200987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    Result = llvm::APInt(DestWidth, 4, Space);
1201a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar    Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1202987b15db39745cd7fb2e634ba1a4b85469ac9131Chris Lattner    break;
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ConditionalOperatorClass: {
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
120728daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner    const Expr *Cond = Exp->getCond();
120828daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner
120928daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner    if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
12115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const Expr *TrueExp  = Exp->getLHS();
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const Expr *FalseExp = Exp->getRHS();
12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Result == 0) std::swap(TrueExp, FalseExp);
12155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
121628daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner    // If the condition (ignoring parens) is a __builtin_constant_p call,
121728daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner    // then only the true side is actually considered in an integer constant
121842b83dde7c700b34f9435ad746984169888ae705Chris Lattner    // expression, and it is fully evaluated.  This is an important GNU
121942b83dde7c700b34f9435ad746984169888ae705Chris Lattner    // extension.  See GCC PR38377 for discussion.
122028daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner    if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
12213c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
122242b83dde7c700b34f9435ad746984169888ae705Chris Lattner        EvalResult EVResult;
122342b83dde7c700b34f9435ad746984169888ae705Chris Lattner        if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
122442b83dde7c700b34f9435ad746984169888ae705Chris Lattner          return false;
122542b83dde7c700b34f9435ad746984169888ae705Chris Lattner        assert(EVResult.Val.isInt() && "FP conditional expr not expected");
122642b83dde7c700b34f9435ad746984169888ae705Chris Lattner        Result = EVResult.Val.getInt();
122742b83dde7c700b34f9435ad746984169888ae705Chris Lattner        if (Loc) *Loc = EVResult.DiagLoc;
122842b83dde7c700b34f9435ad746984169888ae705Chris Lattner        return true;
122942b83dde7c700b34f9435ad746984169888ae705Chris Lattner      }
123028daa53db73341b7ee7f269924ccfca1c6d179acChris Lattner
12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Evaluate the false one first, discard the result.
12322d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar    llvm::APSInt Tmp;
12332d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar    if (FalseExp && !FalseExp->isIntegerConstantExpr(Tmp, Ctx, Loc, false))
12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
12352d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar    // Evalute the true one, capture the result. Note that if TrueExp
12362d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar    // is False then this is an instant of the gcc missing LHS
12372d6744ff04c1690a1485178d550d2fab84a0270bDaniel Dunbar    // extension, and we will just reuse Result.
12383907323dd6665c0c4e383435cb145233f4533406Anders Carlsson    if (TrueExp &&
12393907323dd6665c0c4e383435cb145233f4533406Anders Carlsson        !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
12415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
124304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
124404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    return cast<CXXDefaultArgExpr>(this)
124504421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner             ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
124664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
124764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  case UnaryTypeTraitExprClass:
1248a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar    Result = Ctx.MakeIntValue(cast<UnaryTypeTraitExpr>(this)->EvaluateTrait(),
1249a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar                              getType());
1250a135975d4257ccc83f13b607b29d882bb00c80b6Daniel Dunbar    break;
12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
12545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNullPointerConstant - C99 6.3.2.3p3 -  Return true if this is either an
12575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// integer constant expression with the value zero, or if this is one that is
12585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// cast to void*.
1259efa9b3877ef298bcb792600ac33521827e1f7fafAnders Carlssonbool Expr::isNullPointerConstant(ASTContext &Ctx) const
1260efa9b3877ef298bcb792600ac33521827e1f7fafAnders Carlsson{
12610777972d38a3125efed962b045704c30ae6965cfSebastian Redl  // Strip off a cast to void*, if it exists. Except in C++.
12620835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis  if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
12636215dee86c0e715b9f2b0d401ab2a5fcf629f1afSebastian Redl    if (!Ctx.getLangOptions().CPlusPlus) {
12640777972d38a3125efed962b045704c30ae6965cfSebastian Redl      // Check that it is a cast to void*.
12650777972d38a3125efed962b045704c30ae6965cfSebastian Redl      if (const PointerType *PT = CE->getType()->getAsPointerType()) {
12660777972d38a3125efed962b045704c30ae6965cfSebastian Redl        QualType Pointee = PT->getPointeeType();
12670777972d38a3125efed962b045704c30ae6965cfSebastian Redl        if (Pointee.getCVRQualifiers() == 0 &&
12680777972d38a3125efed962b045704c30ae6965cfSebastian Redl            Pointee->isVoidType() &&                              // to void*
12690777972d38a3125efed962b045704c30ae6965cfSebastian Redl            CE->getSubExpr()->getType()->isIntegerType())         // from int.
1270d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson          return CE->getSubExpr()->isNullPointerConstant(Ctx);
12710777972d38a3125efed962b045704c30ae6965cfSebastian Redl      }
12725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1273aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1274aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Ignore the ImplicitCastExpr type entirely.
1275d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson    return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1276aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1277aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Accept ((void*)0) as a null pointer constant, as many other
1278aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // implementations do.
1279d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson    return PE->getSubExpr()->isNullPointerConstant(Ctx);
12808123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  } else if (const CXXDefaultArgExpr *DefaultArg
12818123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner               = dyn_cast<CXXDefaultArgExpr>(this)) {
128204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    // See through default argument expressions
1283d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson    return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
12842d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor  } else if (isa<GNUNullExpr>(this)) {
12852d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    // The GNU __null extension is always a null pointer constant.
12862d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    return true;
1287aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff  }
12882d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
1289aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  // This expression must be an integer type.
1290aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  if (!getType()->isIntegerType())
1291aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    return false;
1292aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff
12935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have an integer constant expression, we need to *evaluate* it and
12945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // test for the value 0.
1295d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson  // FIXME: We should probably return false if we're compiling in strict mode
1296d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson  // and Diag is not null (this indicates that the value was foldable but not
1297d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson  // an ICE.
1298d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson  EvalResult Result;
1299efa9b3877ef298bcb792600ac33521827e1f7fafAnders Carlsson  return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
1300d26527708b2b2f3b1d747f570efd10149d48364eAnders Carlsson        Result.Val.isInt() && Result.Val.getInt() == 0;
13015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
130231a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff
130327c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// isBitField - Return true if this expression is a bit-field.
130427c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorbool Expr::isBitField() {
130527c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  Expr *E = this->IgnoreParenCasts();
130627c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
130786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
130886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        return Field->isBitField();
130927c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  return false;
131027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
131127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
13122140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// isArrow - Return true if the base expression is a pointer to vector,
13132140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// return false if the base expression is a vector.
13142140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattnerbool ExtVectorElementExpr::isArrow() const {
13152140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner  return getBase()->getType()->isPointerType();
13162140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner}
13172140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner
1318213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const {
13198a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  if (const VectorType *VT = getType()->getAsVectorType())
13208a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    return VT->getNumElements();
13218a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  return 1;
13224d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner}
13234d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner
13248a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated.
1325213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const {
1326fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  const char *compStr = Accessor.getName();
13277e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner  unsigned length = Accessor.getLength();
1328190d6a25393995b42e32086949a68285ee423fb9Nate Begeman
1329190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Halving swizzles do not contain duplicate elements.
1330190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1331190d6a25393995b42e32086949a68285ee423fb9Nate Begeman      !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1332190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    return false;
1333190d6a25393995b42e32086949a68285ee423fb9Nate Begeman
1334190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Advance past s-char prefix on hex swizzles.
1335190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  if (*compStr == 's') {
1336190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    compStr++;
1337190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    length--;
1338190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  }
1339fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff
13407e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner  for (unsigned i = 0; i != length-1; i++) {
1341fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff    const char *s = compStr+i;
1342fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff    for (const char c = *s++; *s; s++)
1343fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff      if (c == *s)
1344fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff        return true;
1345fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  }
1346fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  return false;
1347fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff}
1348b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
13498a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
13503b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess(
13513b8d116703db8018f855cbb4733ace426422623bNate Begeman                                  llvm::SmallVectorImpl<unsigned> &Elts) const {
13527e3e9b152e06edf329ceb32190d3255f248d4d5fChris Lattner  const char *compStr = Accessor.getName();
1353353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman  if (*compStr == 's')
1354353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman    compStr++;
1355353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman
1356353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman  bool isHi =   !strcmp(compStr, "hi");
1357353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman  bool isLo =   !strcmp(compStr, "lo");
1358353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman  bool isEven = !strcmp(compStr, "even");
1359353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman  bool isOdd  = !strcmp(compStr, "odd");
1360353417af9d254d4fd0eb7d0a3ff71c4d8594ac58Nate Begeman
13618a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
13628a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    uint64_t Index;
13638a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman
13648a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    if (isHi)
13658a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = e + i;
13668a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isLo)
13678a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = i;
13688a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isEven)
13698a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i;
13708a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isOdd)
13718a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i + 1;
13728a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else
13738a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = ExtVectorType::getAccessorIdx(compStr[i]);
1374b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
13753b8d116703db8018f855cbb4733ace426422623bNate Begeman    Elts.push_back(Index);
1376b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner  }
13778a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman}
13788a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman
137968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// constructor for instance messages.
1380bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve NaroffObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
1381a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                QualType retType, ObjCMethodDecl *mproto,
1382db611d556f71f98b66b69514d45958d76e5727abSteve Naroff                SourceLocation LBrac, SourceLocation RBrac,
138349f109c786f99eb7468dac3976db083a65493444Steve Naroff                Expr **ArgExprs, unsigned nargs)
1384db611d556f71f98b66b69514d45958d76e5727abSteve Naroff  : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1385ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted Kremenek    MethodProto(mproto) {
138649f109c786f99eb7468dac3976db083a65493444Steve Naroff  NumArgs = nargs;
13875549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  SubExprs = new Stmt*[NumArgs+1];
138868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  SubExprs[RECEIVER] = receiver;
138949f109c786f99eb7468dac3976db083a65493444Steve Naroff  if (NumArgs) {
139049f109c786f99eb7468dac3976db083a65493444Steve Naroff    for (unsigned i = 0; i != NumArgs; ++i)
139168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
139268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  }
1393563477da25f824e37c535131695dc4dc9b68c465Steve Naroff  LBracloc = LBrac;
1394563477da25f824e37c535131695dc4dc9b68c465Steve Naroff  RBracloc = RBrac;
1395563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
1396563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
139768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// constructor for class messages.
139868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff// FIXME: clsName should be typed to ObjCInterfaceType
1399bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve NaroffObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
1400a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                QualType retType, ObjCMethodDecl *mproto,
1401db611d556f71f98b66b69514d45958d76e5727abSteve Naroff                SourceLocation LBrac, SourceLocation RBrac,
140249f109c786f99eb7468dac3976db083a65493444Steve Naroff                Expr **ArgExprs, unsigned nargs)
1403db611d556f71f98b66b69514d45958d76e5727abSteve Naroff  : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1404ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted Kremenek    MethodProto(mproto) {
140549f109c786f99eb7468dac3976db083a65493444Steve Naroff  NumArgs = nargs;
14065549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  SubExprs = new Stmt*[NumArgs+1];
14074df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
140849f109c786f99eb7468dac3976db083a65493444Steve Naroff  if (NumArgs) {
140949f109c786f99eb7468dac3976db083a65493444Steve Naroff    for (unsigned i = 0; i != NumArgs; ++i)
141068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
141168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  }
1412563477da25f824e37c535131695dc4dc9b68c465Steve Naroff  LBracloc = LBrac;
1413563477da25f824e37c535131695dc4dc9b68c465Steve Naroff  RBracloc = RBrac;
1414563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
1415563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
14164df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek// constructor for class messages.
14174df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
14184df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek                                 QualType retType, ObjCMethodDecl *mproto,
14194df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek                                 SourceLocation LBrac, SourceLocation RBrac,
14204df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek                                 Expr **ArgExprs, unsigned nargs)
14214df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
14224df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekMethodProto(mproto) {
14234df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  NumArgs = nargs;
14244df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  SubExprs = new Stmt*[NumArgs+1];
14254df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
14264df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  if (NumArgs) {
14274df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    for (unsigned i = 0; i != NumArgs; ++i)
14284df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
14294df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  }
14304df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  LBracloc = LBrac;
14314df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  RBracloc = RBrac;
14324df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek}
14334df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
14344df728e368fa1f65ffc57572fed613dcca5b4fe8Ted KremenekObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
14354df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
14364df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  switch (x & Flags) {
14374df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    default:
14384df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      assert(false && "Invalid ObjCMessageExpr.");
14394df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    case IsInstMeth:
14404df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      return ClassInfo(0, 0);
14414df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    case IsClsMethDeclUnknown:
14424df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
14434df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    case IsClsMethDeclKnown: {
14444df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
14454df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek      return ClassInfo(D, D->getIdentifier());
14464df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek    }
14474df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek  }
14484df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek}
14494df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
145027437caadea35f84d550cd29f024fcf3ea240eecChris Lattnerbool ChooseExpr::isConditionTrue(ASTContext &C) const {
145132442bbc98bafa512fa42d46fedf60ed7d79f574Daniel Dunbar  return getCond()->getIntegerConstantExprValue(C) != 0;
145227437caadea35f84d550cd29f024fcf3ea240eecChris Lattner}
145327437caadea35f84d550cd29f024fcf3ea240eecChris Lattner
1454670a62cd1d51042ea076cda5e93f26a1d8327fb3Chris Lattnerstatic int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
14555a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
14565a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    QualType Ty = ME->getBase()->getType();
14575a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
14585a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    RecordDecl *RD = Ty->getAsRecordType()->getDecl();
145998be4943e8dc4f3905629a7102668960873cf863Chris Lattner    const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
146086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
146186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      // FIXME: This is linear time. And the fact that we're indexing
146286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      // into the layout by position in the record means that we're
146386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      // either stuck numbering the fields in the AST or we have to keep
146486f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      // the linear search (yuck and yuck).
146586f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      unsigned i = 0;
146686f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      for (RecordDecl::field_iterator Field = RD->field_begin(),
146786f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor                                   FieldEnd = RD->field_end();
146886f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor           Field != FieldEnd; (void)++Field, ++i) {
146986f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor        if (*Field == FD)
147086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor          break;
147186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      }
147286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor
147386f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor      return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
14745a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    }
14755a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
14765a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    const Expr *Base = ASE->getBase();
14775a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
147898be4943e8dc4f3905629a7102668960873cf863Chris Lattner    int64_t size = C.getTypeSize(ASE->getType());
147932442bbc98bafa512fa42d46fedf60ed7d79f574Daniel Dunbar    size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
14805a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
14815a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    return size + evaluateOffsetOf(C, Base);
14825a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  } else if (isa<CompoundLiteralExpr>(E))
14835a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson    return 0;
14845a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
14855a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  assert(0 && "Unknown offsetof subexpression!");
14865a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  return 0;
14875a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson}
14885a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
14895a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlssonint64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
14905a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson{
14915a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson  assert(Opc == OffsetOf && "Unary operator not offsetof!");
14925a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
149398be4943e8dc4f3905629a7102668960873cf863Chris Lattner  unsigned CharSize = C.Target.getCharWidth();
14945549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
14955a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson}
14965a1deb8d9c0722beae28d693fa137bbb942bd11fAnders Carlsson
14970518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redlvoid SizeOfAlignOfExpr::Destroy(ASTContext& C) {
14980518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // Override default behavior of traversing children. If this has a type
14990518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // operand and the type is a variable-length array, the child iteration
15000518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // will iterate over the size expression. However, this expression belongs
15010518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // to the type, not to this, so we don't want to delete it.
15020518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // We still want to delete this expression.
15038189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  if (isArgumentType()) {
15048189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    this->~SizeOfAlignOfExpr();
15058189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    C.Deallocate(this);
15068189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  }
15070518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  else
15080518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    Expr::Destroy(C);
15099048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar}
15109048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar
151177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
151205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//  DesignatedInitExpr
151305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
151405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
151505c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorIdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
151605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(Kind == FieldDesignator && "Only valid on a field designator");
151705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Field.NameOrField & 0x01)
151805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
151905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  else
152005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return getField()->getIdentifier();
152105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
152205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
152305c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr *
152405c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
152505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           unsigned NumDesignators,
152605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           Expr **IndexExprs, unsigned NumIndexExprs,
152705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           SourceLocation ColonOrEqualLoc,
152805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           bool UsesColonSyntax, Expr *Init) {
1529c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1530c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff                         sizeof(Designator) * NumDesignators +
1531c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
153205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  DesignatedInitExpr *DIE
153305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
153405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                   ColonOrEqualLoc, UsesColonSyntax,
153505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                   NumIndexExprs + 1);
153605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
153705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Fill in the designators
153805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  unsigned ExpectedNumSubExprs = 0;
153905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  designators_iterator Desig = DIE->designators_begin();
154005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
154105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
154205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (Designators[Idx].isArrayDesignator())
154305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ++ExpectedNumSubExprs;
154405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else if (Designators[Idx].isArrayRangeDesignator())
154505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ExpectedNumSubExprs += 2;
154605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
154705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
154805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
154905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Fill in the subexpressions, including the initializer expression.
155005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  child_iterator Child = DIE->child_begin();
155105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  *Child++ = Init;
155205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
155305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    *Child = IndexExprs[Idx];
155405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
155505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return DIE;
155605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
155705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
155805c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorSourceRange DesignatedInitExpr::getSourceRange() const {
155905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation StartLoc;
1560d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner  Designator &First =
1561d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    *const_cast<DesignatedInitExpr*>(this)->designators_begin();
156205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (First.isFieldDesignator()) {
156305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (UsesColonSyntax)
156405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
156505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else
156605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
156705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  } else
1568d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    StartLoc =
1569d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner      SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
157005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
157105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
157205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
1573d603eaa682cecac2c10771a700cb83aa301653b4Chris LattnerDesignatedInitExpr::designators_iterator
1574d603eaa682cecac2c10771a700cb83aa301653b4Chris LattnerDesignatedInitExpr::designators_begin() {
157505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
157605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
157705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return static_cast<Designator*>(static_cast<void*>(Ptr));
157805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
157905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
158005c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
158105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return designators_begin() + NumDesignators;
158205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
158305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
158405c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
158505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
158605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
158705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
158805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(Designator) * NumDesignators;
158905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
159005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
159105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
159205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
159305c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
159405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayRangeDesignator &&
159505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
159605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
159705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
159805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(Designator) * NumDesignators;
159905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
160005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
160105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
160205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
160305c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
160405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayRangeDesignator &&
160505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
160605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
160705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
160805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(Designator) * NumDesignators;
160905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
161005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
161105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
161205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
161305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
1614ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//  ExprIterator.
1615ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
1616ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
1617ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1618ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1619ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1620ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator[](size_t idx) const {
1621ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  return cast<Expr>(I[idx]);
1622ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek}
1623ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1624ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1625ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
1626ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
162777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//  Child Iterators for iterating over subexpressions/substatements
162877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
162977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
163077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// DeclRefExpr
16319ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
16329ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
163377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
16347779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff// ObjCIvarRefExpr
16355549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
16365549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
16377779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff
1638e3e9add4fd788927df6f545570e7838db59c01d7Steve Naroff// ObjCPropertyRefExpr
16395549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
16405549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
1641ae7840776d6cd31b4d7a4a345b61bcbb3744df6cSteve Naroff
16425daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian// ObjCKVCRefExpr
16435daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz JahanianStmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
16445daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz JahanianStmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
16455daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian
1646cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas Gregor// ObjCSuperExpr
1647cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas GregorStmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1648cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas GregorStmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1649cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas Gregor
1650d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner// PredefinedExpr
1651d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1652d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
165377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
165477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// IntegerLiteral
16559ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
16569ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
165777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
165877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CharacterLiteral
1659d603eaa682cecac2c10771a700cb83aa301653b4Chris LattnerStmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
16609ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
166177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
166277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// FloatingLiteral
16639ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
16649ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
166577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
16665d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner// ImaginaryLiteral
16675549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
16685549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
16695d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner
167077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// StringLiteral
16719ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
16729ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
167377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
167477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ParenExpr
16755549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_begin() { return &Val; }
16765549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_end() { return &Val+1; }
167777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
167877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// UnaryOperator
16795549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_begin() { return &Val; }
16805549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
168177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
16820518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl// SizeOfAlignOfExpr
16830518999d3adcc289997bd974dce90cc97f5c1c44Sebastian RedlStmt::child_iterator SizeOfAlignOfExpr::child_begin() {
16840518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // If this is of a type and the type is a VLA type (and not a typedef), the
16850518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // size expression of the VLA needs to be treated as an executable expression.
16860518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // Why isn't this weirdness documented better in StmtIterator?
16870518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType()) {
16880518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    if (VariableArrayType* T = dyn_cast<VariableArrayType>(
16890518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl                                   getArgumentType().getTypePtr()))
16900518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl      return child_iterator(T);
16910518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    return child_iterator();
16920518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  }
1693d457589fc69dc7a9c80cd74d317c0b81a35a27c9Sebastian Redl  return child_iterator(&Argument.Ex);
16949ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
16950518999d3adcc289997bd974dce90cc97f5c1c44Sebastian RedlStmt::child_iterator SizeOfAlignOfExpr::child_end() {
16960518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType())
16970518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    return child_iterator();
1698d457589fc69dc7a9c80cd74d317c0b81a35a27c9Sebastian Redl  return child_iterator(&Argument.Ex + 1);
16999ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
170077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
170177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ArraySubscriptExpr
17021237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_begin() {
17035549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
170477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
17051237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_end() {
17065549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+END_EXPR;
170777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
170877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
170977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CallExpr
17101237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_begin() {
17115549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
171277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
17131237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_end() {
17145549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+NumArgs+ARGS_START;
171577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
17161237c673c07f9d827129ba02720108816abde562Ted Kremenek
17171237c673c07f9d827129ba02720108816abde562Ted Kremenek// MemberExpr
17185549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_begin() { return &Base; }
17195549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_end() { return &Base+1; }
17201237c673c07f9d827129ba02720108816abde562Ted Kremenek
1721213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman// ExtVectorElementExpr
17225549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
17235549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
17241237c673c07f9d827129ba02720108816abde562Ted Kremenek
17251237c673c07f9d827129ba02720108816abde562Ted Kremenek// CompoundLiteralExpr
17265549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
17275549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
17281237c673c07f9d827129ba02720108816abde562Ted Kremenek
17291237c673c07f9d827129ba02720108816abde562Ted Kremenek// CastExpr
17305549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_begin() { return &Op; }
17315549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_end() { return &Op+1; }
17321237c673c07f9d827129ba02720108816abde562Ted Kremenek
17331237c673c07f9d827129ba02720108816abde562Ted Kremenek// BinaryOperator
17341237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_begin() {
17355549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
17361237c673c07f9d827129ba02720108816abde562Ted Kremenek}
17371237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_end() {
17385549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+END_EXPR;
17391237c673c07f9d827129ba02720108816abde562Ted Kremenek}
17401237c673c07f9d827129ba02720108816abde562Ted Kremenek
17411237c673c07f9d827129ba02720108816abde562Ted Kremenek// ConditionalOperator
17421237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_begin() {
17435549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
17441237c673c07f9d827129ba02720108816abde562Ted Kremenek}
17451237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_end() {
17465549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+END_EXPR;
17471237c673c07f9d827129ba02720108816abde562Ted Kremenek}
17481237c673c07f9d827129ba02720108816abde562Ted Kremenek
17491237c673c07f9d827129ba02720108816abde562Ted Kremenek// AddrLabelExpr
17509ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
17519ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
17521237c673c07f9d827129ba02720108816abde562Ted Kremenek
17531237c673c07f9d827129ba02720108816abde562Ted Kremenek// StmtExpr
17545549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
17555549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
17561237c673c07f9d827129ba02720108816abde562Ted Kremenek
17571237c673c07f9d827129ba02720108816abde562Ted Kremenek// TypesCompatibleExpr
17589ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_begin() {
17599ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
17609ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
17619ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek
17629ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_end() {
17639ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
17649ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
17651237c673c07f9d827129ba02720108816abde562Ted Kremenek
17661237c673c07f9d827129ba02720108816abde562Ted Kremenek// ChooseExpr
17675549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
17685549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
17692d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
17702d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor// GNUNullExpr
17712d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas GregorStmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
17722d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas GregorStmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
17731237c673c07f9d827129ba02720108816abde562Ted Kremenek
1774d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman// ShuffleVectorExpr
1775d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_begin() {
17765549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
1777d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman}
1778d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_end() {
17795549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+NumExprs;
1780d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman}
1781d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman
17827c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson// VAArgExpr
17835549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_begin() { return &Val; }
17845549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
17857c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson
178666b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson// InitListExpr
178766b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonStmt::child_iterator InitListExpr::child_begin() {
17885549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return InitExprs.size() ? &InitExprs[0] : 0;
178966b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
179066b5a8a39088598c01a9fa6f032dc908612dc8ecAnders CarlssonStmt::child_iterator InitListExpr::child_end() {
17915549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
179266b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
179366b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson
17943498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor// DesignatedInitExpr
179505c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorStmt::child_iterator DesignatedInitExpr::child_begin() {
179605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
179705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
179805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(Designator) * NumDesignators;
179905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
180005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
180105c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorStmt::child_iterator DesignatedInitExpr::child_end() {
180205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return child_iterator(&*child_begin() + NumSubExprs);
180305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
180405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
18053498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor// ImplicitValueInitExpr
18063498bdb9e9cb300de74c7b51c92608e2902b2348Douglas GregorStmt::child_iterator ImplicitValueInitExpr::child_begin() {
18073498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  return child_iterator();
18083498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor}
18093498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
18103498bdb9e9cb300de74c7b51c92608e2902b2348Douglas GregorStmt::child_iterator ImplicitValueInitExpr::child_end() {
18113498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  return child_iterator();
18123498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor}
18133498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
18141237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCStringLiteral
18159ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_begin() {
1816c6c16af963eddc3e9b75b5d2614d069e1162fe27Chris Lattner  return &String;
18179ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
18189ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_end() {
1819c6c16af963eddc3e9b75b5d2614d069e1162fe27Chris Lattner  return &String+1;
18209ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
18211237c673c07f9d827129ba02720108816abde562Ted Kremenek
18221237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCEncodeExpr
18239ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
18249ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
18251237c673c07f9d827129ba02720108816abde562Ted Kremenek
1826b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian// ObjCSelectorExpr
18279ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_begin() {
18289ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
18299ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
18309ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_end() {
18319ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
18329ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
1833b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
1834390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian// ObjCProtocolExpr
18359ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_begin() {
18369ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
18379ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
18389ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_end() {
18399ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
18409ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
1841390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
1842563477da25f824e37c535131695dc4dc9b68c465Steve Naroff// ObjCMessageExpr
1843ea958e57a370b641c5a69347b75e9f8e3b5a41a2Ted KremenekStmt::child_iterator ObjCMessageExpr::child_begin() {
18445549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
1845563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
1846563477da25f824e37c535131695dc4dc9b68c465Steve NaroffStmt::child_iterator ObjCMessageExpr::child_end() {
18475549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+ARGS_START+getNumArgs();
1848563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
1849563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
18504eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff// Blocks
185156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
185256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
18534eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
18549da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
18559da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }
1856