Expr.cpp revision 58277b57f9492d0234748be89bcad48b322c5cf7
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"
150979c805475d1ba49b5d6ef93c4d2ce6d2eab6edDouglas Gregor#include "clang/AST/ExprCXX.h"
16a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner#include "clang/AST/APValue.h"
172eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner#include "clang/AST/ASTContext.h"
18a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner#include "clang/AST/DeclObjC.h"
1998cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor#include "clang/AST/DeclCXX.h"
20aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
2119cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/StmtVisitor.h"
231b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
24da5a6b6d9fd52899499d5b7b46273ec844dcaa6eChris Lattner#include "clang/Basic/TargetInfo.h"
25cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor#include "llvm/Support/ErrorHandling.h"
263a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson#include "llvm/Support/raw_ostream.h"
27ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor#include <algorithm>
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
30bef0efd11bc4430a3ee437a3213cec5c18af855aChris Lattnervoid Expr::ANCHOR() {} // key function for Expr class.
31bef0efd11bc4430a3ee437a3213cec5c18af855aChris Lattner
322b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// isKnownToHaveBooleanValue - Return true if this is an integer expression
332b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// that is known to return 0 or 1.  This happens for _Bool/bool expressions
342b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// but also int expressions which are produced by things like comparisons in
352b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// C.
362b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattnerbool Expr::isKnownToHaveBooleanValue() const {
372b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  // If this value has _Bool type, it is obvious 0/1.
382b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  if (getType()->isBooleanType()) return true;
39c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  // If this is a non-scalar-integer type, we don't care enough to try.
402ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (!getType()->isIntegralOrEnumerationType()) return false;
41c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
422b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
432b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return PE->getSubExpr()->isKnownToHaveBooleanValue();
44c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
452b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
462b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    switch (UO->getOpcode()) {
472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Plus:
482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
492b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return UO->getSubExpr()->isKnownToHaveBooleanValue();
502b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    default:
512b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return false;
522b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
532b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  }
54c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
556907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  // Only look through implicit casts.  If the user writes
566907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  // '(int) (a && b)' treat it as an arbitrary int.
576907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(this))
582b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return CE->getSubExpr()->isKnownToHaveBooleanValue();
59c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
602b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
612b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    switch (BO->getOpcode()) {
622b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    default: return false;
632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:   // Relational operators.
642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:   // Equality operators.
682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LAnd: // AND operator.
702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LOr:  // Logical OR operator.
712b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return true;
72c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_And:  // Bitwise AND operator.
742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Xor:  // Bitwise XOR operator.
752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Or:   // Bitwise OR operator.
762b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      // Handle things like (x==2)|(y==12).
772b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return BO->getLHS()->isKnownToHaveBooleanValue() &&
782b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner             BO->getRHS()->isKnownToHaveBooleanValue();
79c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Comma:
812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Assign:
822b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return BO->getRHS()->isKnownToHaveBooleanValue();
832b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
842b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  }
85c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
862b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
872b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
882b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner           CO->getFalseExpr()->isKnownToHaveBooleanValue();
89c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
902b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  return false;
912b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner}
922b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner
935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Expressions.
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
97d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCallvoid ExplicitTemplateArgumentList::initializeFrom(
98d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                      const TemplateArgumentListInfo &Info) {
99d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  LAngleLoc = Info.getLAngleLoc();
100d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  RAngleLoc = Info.getRAngleLoc();
101d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  NumTemplateArgs = Info.size();
102d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
103d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
104d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  for (unsigned i = 0; i != NumTemplateArgs; ++i)
105d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
106d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall}
107d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
108d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCallvoid ExplicitTemplateArgumentList::copyInto(
109d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                      TemplateArgumentListInfo &Info) const {
110d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  Info.setLAngleLoc(LAngleLoc);
111d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  Info.setRAngleLoc(RAngleLoc);
112d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  for (unsigned I = 0; I != NumTemplateArgs; ++I)
113d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    Info.addArgument(getTemplateArgs()[I]);
114d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall}
115d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1168dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidisstd::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
1178dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis  return sizeof(ExplicitTemplateArgumentList) +
1188dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis         sizeof(TemplateArgumentLoc) * NumTemplateArgs;
1198dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis}
1208dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis
121d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCallstd::size_t ExplicitTemplateArgumentList::sizeFor(
122d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                      const TemplateArgumentListInfo &Info) {
1238dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis  return sizeFor(Info.size());
124d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall}
125d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1260da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregorvoid DeclRefExpr::computeDependence() {
1270da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  TypeDependent = false;
1280da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  ValueDependent = false;
129c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1300da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  NamedDecl *D = getDecl();
1310da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
1320da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  // (TD) C++ [temp.dep.expr]p3:
1330da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //   An id-expression is type-dependent if it contains:
1340da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //
135c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  // and
1360da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //
1370da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  // (VD) C++ [temp.dep.constexpr]p2:
1380da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  An identifier is value-dependent if it is:
1390da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
1400da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - an identifier that was declared with dependent type
1410da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD)  - a name declared with a dependent type,
1420da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  if (getType()->isDependentType()) {
1430da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor    TypeDependent = true;
1440da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor    ValueDependent = true;
1450da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
1460da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - a conversion-function-id that specifies a dependent type
147c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  else if (D->getDeclName().getNameKind()
1480da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                               == DeclarationName::CXXConversionFunctionName &&
1490da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor           D->getDeclName().getCXXNameType()->isDependentType()) {
1500da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor    TypeDependent = true;
1510da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor    ValueDependent = true;
1520da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
1530da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - a template-id that is dependent,
154096832c5ed5b9106fa177ebc148489760c3bc496John McCall  else if (hasExplicitTemplateArgs() &&
1550da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor           TemplateSpecializationType::anyDependentTemplateArguments(
156c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                                       getTemplateArgs(),
1570da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                                       getNumTemplateArgs())) {
1580da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor    TypeDependent = true;
1590da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor    ValueDependent = true;
1600da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
1610da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD)  - the name of a non-type template parameter,
1620da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  else if (isa<NonTypeTemplateParmDecl>(D))
1630da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor    ValueDependent = true;
1640da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD) - a constant with integral or enumeration type and is
1650da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //         initialized with an expression that is value-dependent.
1660da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1672ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    if (Var->getType()->isIntegralOrEnumerationType() &&
168501edb6a54524555ad27fbf41a7920dc756b08c6Douglas Gregor        Var->getType().getCVRQualifiers() == Qualifiers::Const) {
16931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      if (const Expr *Init = Var->getAnyInitializer())
170501edb6a54524555ad27fbf41a7920dc756b08c6Douglas Gregor        if (Init->isValueDependent())
171501edb6a54524555ad27fbf41a7920dc756b08c6Douglas Gregor          ValueDependent = true;
172bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    }
173bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    // (VD) - FIXME: Missing from the standard:
174bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    //      -  a member function or a static data member of the current
175bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    //         instantiation
176bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    else if (Var->isStaticDataMember() &&
1777ed5bd3e27a6f2b37ee0449aa818116cbd03306eDouglas Gregor             Var->getDeclContext()->isDependentContext())
178bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor      ValueDependent = true;
179bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  }
180bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  // (VD) - FIXME: Missing from the standard:
181bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  //      -  a member function or a static data member of the current
182bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  //         instantiation
183bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  else if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext())
184bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    ValueDependent = true;
1850da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - a nested-name-specifier or a qualified-id that names a
1860da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //          member of an unknown specialization.
1870da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //        (handled by DependentScopeDeclRefExpr)
1880da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor}
1890da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
190c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntDeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
191a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor                         SourceRange QualifierRange,
192dbd872f273a8dbf22e089b3def6c09f0a460965dJohn McCall                         ValueDecl *D, SourceLocation NameLoc,
193d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                         const TemplateArgumentListInfo *TemplateArgs,
1940da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                         QualType T)
1950da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  : Expr(DeclRefExprClass, T, false, false),
196a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor    DecoratedD(D,
197a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor               (Qualifier? HasQualifierFlag : 0) |
198d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall               (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
199a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor    Loc(NameLoc) {
200a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  if (Qualifier) {
201a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor    NameQualifier *NQ = getNameQualifier();
202a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor    NQ->NNS = Qualifier;
203a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor    NQ->Range = QualifierRange;
204a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  }
205c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
206d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (TemplateArgs)
207096832c5ed5b9106fa177ebc148489760c3bc496John McCall    getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
208a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
2090da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  computeDependence();
210a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor}
211a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
2122577743c5650c646fb705df01403707e94f2df04Abramo BagnaraDeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
2132577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                         SourceRange QualifierRange,
2142577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                         ValueDecl *D, const DeclarationNameInfo &NameInfo,
2152577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                         const TemplateArgumentListInfo *TemplateArgs,
2162577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                         QualType T)
2172577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  : Expr(DeclRefExprClass, T, false, false),
2182577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    DecoratedD(D,
2192577743c5650c646fb705df01403707e94f2df04Abramo Bagnara               (Qualifier? HasQualifierFlag : 0) |
2202577743c5650c646fb705df01403707e94f2df04Abramo Bagnara               (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
2212577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
2222577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  if (Qualifier) {
2232577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    NameQualifier *NQ = getNameQualifier();
2242577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    NQ->NNS = Qualifier;
2252577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    NQ->Range = QualifierRange;
2262577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
2272577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2282577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  if (TemplateArgs)
229096832c5ed5b9106fa177ebc148489760c3bc496John McCall    getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
2302577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2312577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  computeDependence();
2322577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
2332577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
234a2813cec2605ce7878d1b13471d685f689b251afDouglas GregorDeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
235a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor                                 NestedNameSpecifier *Qualifier,
236a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor                                 SourceRange QualifierRange,
237dbd872f273a8dbf22e089b3def6c09f0a460965dJohn McCall                                 ValueDecl *D,
238a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor                                 SourceLocation NameLoc,
2390da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                 QualType T,
2400da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                 const TemplateArgumentListInfo *TemplateArgs) {
2412577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  return Create(Context, Qualifier, QualifierRange, D,
2422577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                DeclarationNameInfo(D->getDeclName(), NameLoc),
2432577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                T, TemplateArgs);
2442577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
2452577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2462577743c5650c646fb705df01403707e94f2df04Abramo BagnaraDeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
2472577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 NestedNameSpecifier *Qualifier,
2482577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 SourceRange QualifierRange,
2492577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 ValueDecl *D,
2502577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 const DeclarationNameInfo &NameInfo,
2512577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 QualType T,
2522577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 const TemplateArgumentListInfo *TemplateArgs) {
253a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  std::size_t Size = sizeof(DeclRefExpr);
254a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  if (Qualifier != 0)
255a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor    Size += sizeof(NameQualifier);
256c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
257d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (TemplateArgs)
258d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
259c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
260a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
2612577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameInfo,
2620da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                               TemplateArgs, T);
263a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor}
264a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
265663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios KyrtzidisDeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context, bool HasQualifier,
266663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis                                      unsigned NumTemplateArgs) {
267663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  std::size_t Size = sizeof(DeclRefExpr);
268663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  if (HasQualifier)
269663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis    Size += sizeof(NameQualifier);
270663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis
271663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  if (NumTemplateArgs)
272663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis    Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
273663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis
274663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
275663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  return new (Mem) DeclRefExpr(EmptyShell());
276663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis}
277663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis
278a2813cec2605ce7878d1b13471d685f689b251afDouglas GregorSourceRange DeclRefExpr::getSourceRange() const {
2792577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  SourceRange R = getNameInfo().getSourceRange();
280a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  if (hasQualifier())
281a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor    R.setBegin(getQualifierRange().getBegin());
282096832c5ed5b9106fa177ebc148489760c3bc496John McCall  if (hasExplicitTemplateArgs())
283a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor    R.setEnd(getRAngleLoc());
284a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  return R;
285a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor}
286a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
2873a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson// FIXME: Maybe this should use DeclPrinter with a special "print predefined
2883a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson// expr" policy instead.
289848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlssonstd::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
290848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson  ASTContext &Context = CurrentDecl->getASTContext();
291848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson
2923a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
293848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson    if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
2943a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      return FD->getNameAsString();
2953a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
2963a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::SmallString<256> Name;
2973a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::raw_svector_ostream Out(Name);
2983a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
2993a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
300848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson      if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
3013a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        Out << "virtual ";
3024eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (MD->isStatic())
3034eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Out << "static ";
3043a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    }
3053a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3063a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    PrintingPolicy Policy(Context.getLangOptions());
3073a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3083a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    std::string Proto = FD->getQualifiedNameAsString(Policy);
3093a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
310183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
3113a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    const FunctionProtoType *FT = 0;
3123a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (FD->hasWrittenPrototype())
3133a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      FT = dyn_cast<FunctionProtoType>(AFT);
3143a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3153a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Proto += "(";
3163a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (FT) {
3173a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      llvm::raw_string_ostream POut(Proto);
3183a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
3193a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        if (i) POut << ", ";
3203a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        std::string Param;
3213a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
3223a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        POut << Param;
3233a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      }
3243a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3253a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      if (FT->isVariadic()) {
3263a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        if (FD->getNumParams()) POut << ", ";
3273a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        POut << "...";
3283a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      }
3293a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    }
3303a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Proto += ")";
3313a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3324eadcc569223135e13353c9381b448986e3f7053Sam Weinig    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
3334eadcc569223135e13353c9381b448986e3f7053Sam Weinig      Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
3344eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (ThisQuals.hasConst())
3354eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Proto += " const";
3364eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (ThisQuals.hasVolatile())
3374eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Proto += " volatile";
3384eadcc569223135e13353c9381b448986e3f7053Sam Weinig    }
3394eadcc569223135e13353c9381b448986e3f7053Sam Weinig
3403a1ce1ed0f5686384e712837bad28c576622e442Sam Weinig    if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
3413a1ce1ed0f5686384e712837bad28c576622e442Sam Weinig      AFT->getResultType().getAsStringInternal(Proto, Policy);
3423a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3433a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << Proto;
3443a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3453a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out.flush();
3463a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return Name.str().str();
3473a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
3483a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
3493a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::SmallString<256> Name;
3503a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::raw_svector_ostream Out(Name);
3513a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << (MD->isInstanceMethod() ? '-' : '+');
3523a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << '[';
353b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek
354b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    // For incorrect code, there might not be an ObjCInterfaceDecl.  Do
355b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    // a null check to avoid a crash.
356b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
357900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer      Out << ID;
358b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek
3593a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (const ObjCCategoryImplDecl *CID =
360900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer        dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
361900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer      Out << '(' << CID << ')';
362900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer
3633a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out <<  ' ';
3643a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << MD->getSelector().getAsString();
3653a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out <<  ']';
3663a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3673a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out.flush();
3683a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return Name.str().str();
3693a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
3703a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
3713a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
3723a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return "top level";
3733a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
3743a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return "";
3753a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
3763a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3779996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidisvoid APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
3789996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  if (hasAllocation())
3799996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    C.Deallocate(pVal);
3809996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
3819996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  BitWidth = Val.getBitWidth();
3829996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  unsigned NumWords = Val.getNumWords();
3839996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  const uint64_t* Words = Val.getRawData();
3849996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  if (NumWords > 1) {
3859996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    pVal = new (C) uint64_t[NumWords];
3869996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    std::copy(Words, Words + NumWords, pVal);
3879996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  } else if (NumWords == 1)
3889996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    VAL = Words[0];
3899996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  else
3909996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    VAL = 0;
3919996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
3929996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
3939996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral *
3949996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
3959996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                       QualType type, SourceLocation l) {
3969996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) IntegerLiteral(C, V, type, l);
3979996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
3989996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
3999996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral *
4009996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
4019996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) IntegerLiteral(Empty);
4029996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4039996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4049996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral *
4059996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
4069996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                        bool isexact, QualType Type, SourceLocation L) {
4079996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) FloatingLiteral(C, V, isexact, Type, L);
4089996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4099996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4109996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral *
4119996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
4129996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) FloatingLiteral(Empty);
4139996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4149996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
415da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// getValueAsApproximateDouble - This returns the value as an inaccurate
416da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// double.  Note that this may cause loss of precision, but is useful for
417da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// debugging dumps, etc.
418da8249e57f3badecf925571881fe57243935c6c1Chris Lattnerdouble FloatingLiteral::getValueAsApproximateDouble() const {
419da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  llvm::APFloat V = getValue();
420ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  bool ignored;
421ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
422ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen            &ignored);
423da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  return V.convertToDouble();
424da8249e57f3badecf925571881fe57243935c6c1Chris Lattner}
425da8249e57f3badecf925571881fe57243935c6c1Chris Lattner
4262085fd6cd22ec5c268175251db10d7c60caf7aaaChris LattnerStringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
4272085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                                     unsigned ByteLength, bool Wide,
4282085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                                     QualType Ty,
4291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     const SourceLocation *Loc,
430a135fb43eb94524a6529768596a4533eed9aa70dAnders Carlsson                                     unsigned NumStrs) {
4312085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // Allocate enough space for the StringLiteral plus an array of locations for
4322085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // any concatenated string tokens.
4332085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  void *Mem = C.Allocate(sizeof(StringLiteral)+
4342085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                         sizeof(SourceLocation)*(NumStrs-1),
4352085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                         llvm::alignof<StringLiteral>());
4362085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  StringLiteral *SL = new (Mem) StringLiteral(Ty);
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // OPTIMIZE: could allocate this appended to the StringLiteral.
4392085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  char *AStrData = new (C, 1) char[ByteLength];
4402085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  memcpy(AStrData, StrData, ByteLength);
4412085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->StrData = AStrData;
4422085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->ByteLength = ByteLength;
4432085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->IsWide = Wide;
4442085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->TokLocs[0] = Loc[0];
4452085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->NumConcatenated = NumStrs;
4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
447726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner  if (NumStrs != 1)
4482085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner    memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
4492085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  return SL;
450726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner}
451726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner
452673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas GregorStringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
453673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  void *Mem = C.Allocate(sizeof(StringLiteral)+
454673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor                         sizeof(SourceLocation)*(NumStrs-1),
455673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor                         llvm::alignof<StringLiteral>());
456673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  StringLiteral *SL = new (Mem) StringLiteral(QualType());
457673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  SL->StrData = 0;
458673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  SL->ByteLength = 0;
459673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  SL->NumConcatenated = NumStrs;
460673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  return SL;
461673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor}
462673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor
463b648023da23e8b227cdda57a241db4c6f368726bDaniel Dunbarvoid StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
464b648023da23e8b227cdda57a241db4c6f368726bDaniel Dunbar  char *AStrData = new (C, 1) char[Str.size()];
465b648023da23e8b227cdda57a241db4c6f368726bDaniel Dunbar  memcpy(AStrData, Str.data(), Str.size());
466673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  StrData = AStrData;
467b648023da23e8b227cdda57a241db4c6f368726bDaniel Dunbar  ByteLength = Str.size();
468673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor}
469673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor
4705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
4715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "sizeof" or "[pre]++".
4725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *UnaryOperator::getOpcodeStr(Opcode Op) {
4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown unary operator");
4752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostInc: return "++";
4762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostDec: return "--";
4772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PreInc:  return "++";
4782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PreDec:  return "--";
4792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_AddrOf:  return "&";
4802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Deref:   return "*";
4812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:    return "+";
4822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus:   return "-";
4832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Not:     return "~";
4842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_LNot:    return "!";
4852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Real:    return "__real";
4862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Imag:    return "__imag";
4872de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Extension: return "__extension__";
4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCallUnaryOperatorKind
492bc736fceca6f0bca31d16003a7587857190408fbDouglas GregorUnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
493bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  switch (OO) {
494bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  default: assert(false && "No unary operator for overloaded function");
4952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PlusPlus:   return Postfix ? UO_PostInc : UO_PreInc;
4962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
4972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Amp:        return UO_AddrOf;
4982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Star:       return UO_Deref;
4992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Plus:       return UO_Plus;
5002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Minus:      return UO_Minus;
5012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Tilde:      return UO_Not;
5022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Exclaim:    return UO_LNot;
503bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  }
504bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor}
505bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
506bc736fceca6f0bca31d16003a7587857190408fbDouglas GregorOverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
507bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  switch (Opc) {
5082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
5092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
5102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_AddrOf: return OO_Amp;
5112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Deref: return OO_Star;
5122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus: return OO_Plus;
5132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus: return OO_Minus;
5142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Not: return OO_Tilde;
5152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_LNot: return OO_Exclaim;
516bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  default: return OO_None;
517bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  }
518bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor}
519bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
520bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
5215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Postfix Operators.
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
525668bf91d31265b6ea8c3eb854ba450857701f269Ted KremenekCallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
5268189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                   unsigned numargs, QualType t, SourceLocation rparenloc)
5271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(SC, t,
528898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
529d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner         fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
530898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    NumArgs(numargs) {
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
532668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  SubExprs = new (C) Stmt*[numargs+1];
533b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  SubExprs[FN] = fn;
534b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  for (unsigned i = 0; i != numargs; ++i)
535b4609806e9232593ece09ce08b630836e825865cDouglas Gregor    SubExprs[i+ARGS_START] = args[i];
536668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
537b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  RParenLoc = rparenloc;
538b4609806e9232593ece09ce08b630836e825865cDouglas Gregor}
539e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman
540668bf91d31265b6ea8c3eb854ba450857701f269Ted KremenekCallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
541668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek                   QualType t, SourceLocation rparenloc)
542898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  : Expr(CallExprClass, t,
543898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
544d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner         fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
545898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    NumArgs(numargs) {
546668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
547668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek  SubExprs = new (C) Stmt*[numargs+1];
54877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek  SubExprs[FN] = fn;
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != numargs; ++i)
55077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek    SubExprs[i+ARGS_START] = args[i];
551668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  RParenLoc = rparenloc;
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5551eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
5571f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor  SubExprs = new (C) Stmt*[1];
5581f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor}
5591f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor
560d20254f2875d0004c57ee766f258dbcee29f4841Nuno LopesDecl *CallExpr::getCalleeDecl() {
561a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu  Expr *CEE = getCallee()->IgnoreParenCasts();
562200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  // If we're calling a dereference, look at the pointer instead.
563200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
564200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl    if (BO->isPtrMemOp())
565200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl      CEE = BO->getRHS()->IgnoreParenCasts();
566200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
567200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl    if (UO->getOpcode() == UO_Deref)
568200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl      CEE = UO->getSubExpr()->IgnoreParenCasts();
569200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  }
5706346f963145ed18b6edf50a78753b47db505e912Chris Lattner  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
571d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    return DRE->getDecl();
572cb1c77f90d4e747b83a0d0cc125dc01567378f82Nuno Lopes  if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
573cb1c77f90d4e747b83a0d0cc125dc01567378f82Nuno Lopes    return ME->getMemberDecl();
574a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu
575a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu  return 0;
576a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu}
577a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu
578d20254f2875d0004c57ee766f258dbcee29f4841Nuno LopesFunctionDecl *CallExpr::getDirectCallee() {
579caabf9bf331156e96dacb072385901fdfa057ec1Chris Lattner  return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
580d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes}
581d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes
582d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// setNumArgs - This changes the number of arguments present in this call.
583d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// Any orphaned expressions are deleted by this, and any new operands are set
584d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// to null.
5858189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenekvoid CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
586d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // No change, just return.
587d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs == getNumArgs()) return;
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
589d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // If shrinking # arguments, just delete the extras and forgot them.
590d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs < getNumArgs()) {
591d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    this->NumArgs = NumArgs;
592d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    return;
593d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  }
594d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
595d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Otherwise, we are growing the # arguments.  New an bigger argument array.
59668a049cab6015a7437bec5661601b7d37d23c70cDaniel Dunbar  Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
597d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Copy over args.
598d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
599d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = SubExprs[i];
600d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Null out new args.
601d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
602d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = 0;
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60488c9a46f0b84f1ee83e01917825346551ee540d0Douglas Gregor  if (SubExprs) C.Deallocate(SubExprs);
605d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  SubExprs = NewSubExprs;
606d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  this->NumArgs = NumArgs;
607d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner}
608d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
609cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// isBuiltinCall - If this is a call to a builtin, return the builtin ID.  If
610cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// not, return 0.
6113c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregorunsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
612c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // All simple function calls (e.g. func()) are implicitly cast to pointer to
6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // function. As a result, we try and obtain the DeclRefExpr from the
614c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // ImplicitCastExpr.
615c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
616c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
617cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
6181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
619c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
620c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!DRE)
621cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
623bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
624bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  if (!FDecl)
625cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
6261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6274fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor  if (!FDecl->getIdentifier())
6284fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor    return 0;
6294fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor
6307814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  return FDecl->getBuiltinID();
631cb888967400a03504c88acedd5248d6778a82f46Chris Lattner}
632bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson
6336dde78f744382a5627a04f984a97049e0c4b5e73Anders CarlssonQualType CallExpr::getCallReturnType() const {
6346dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson  QualType CalleeType = getCallee()->getType();
6356217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
6366dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson    CalleeType = FnTypePtr->getPointeeType();
6376217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
6386dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson    CalleeType = BPT->getPointeeType();
6395291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  else if (const MemberPointerType *MPT
6405291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor                                      = CalleeType->getAs<MemberPointerType>())
6415291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor    CalleeType = MPT->getPointeeType();
6425291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
643183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FnType = CalleeType->getAs<FunctionType>();
6446dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson  return FnType->getResultType();
6456dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson}
646cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
647c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntOffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
6488ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   SourceLocation OperatorLoc,
649c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                   TypeSourceInfo *tsi,
650c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                   OffsetOfNode* compsPtr, unsigned numComps,
6518ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   Expr** exprsPtr, unsigned numExprs,
6528ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   SourceLocation RParenLoc) {
6538ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
654c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                         sizeof(OffsetOfNode) * numComps +
6558ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(Expr*) * numExprs);
6568ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
6578ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
6588ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                exprsPtr, numExprs, RParenLoc);
6598ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
6608ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
6618ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas GregorOffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
6628ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                        unsigned numComps, unsigned numExprs) {
6638ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
6648ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(OffsetOfNode) * numComps +
6658ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(Expr*) * numExprs);
6668ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return new (Mem) OffsetOfExpr(numComps, numExprs);
6678ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
6688ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
669c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntOffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
6708ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           SourceLocation OperatorLoc, TypeSourceInfo *tsi,
671c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                           OffsetOfNode* compsPtr, unsigned numComps,
6728ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           Expr** exprsPtr, unsigned numExprs,
6738ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           SourceLocation RParenLoc)
674c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
6758ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor         /*ValueDependent=*/tsi->getType()->isDependentType() ||
6768ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor         hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
6778ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor         hasAnyValueDependentArguments(exprsPtr, numExprs)),
678c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
679c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    NumComps(numComps), NumExprs(numExprs)
6808ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor{
6818ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  for(unsigned i = 0; i < numComps; ++i) {
6828ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    setComponent(i, compsPtr[i]);
6838ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
684c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
6858ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  for(unsigned i = 0; i < numExprs; ++i) {
6868ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    setIndexExpr(i, exprsPtr[i]);
6878ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
6888ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
6898ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
6908ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas GregorIdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
6918ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  assert(getKind() == Field || getKind() == Identifier);
6928ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  if (getKind() == Field)
6938ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    return getField()->getIdentifier();
694c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
6958ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
6968ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
6978ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
6981eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpMemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                               NestedNameSpecifier *qual,
70083f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor                               SourceRange qualrange,
701f595cc41c4d95fe323f8a2b209523de9956f874dEli Friedman                               ValueDecl *memberdecl,
702161755a09898c95d21bfff33707da9ca41cd53c5John McCall                               DeclAccessPair founddecl,
7032577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                               DeclarationNameInfo nameinfo,
704d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                               const TemplateArgumentListInfo *targs,
705c4bf26fbdff42967d660f505a83f75a4df2cc752Douglas Gregor                               QualType ty) {
70683f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  std::size_t Size = sizeof(MemberExpr);
7076bb8017bb9e828d118e15e59d71c66bba323c364John McCall
708161755a09898c95d21bfff33707da9ca41cd53c5John McCall  bool hasQualOrFound = (qual != 0 ||
709161755a09898c95d21bfff33707da9ca41cd53c5John McCall                         founddecl.getDecl() != memberdecl ||
710161755a09898c95d21bfff33707da9ca41cd53c5John McCall                         founddecl.getAccess() != memberdecl->getAccess());
7116bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (hasQualOrFound)
7126bb8017bb9e828d118e15e59d71c66bba323c364John McCall    Size += sizeof(MemberNameQualifier);
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
714d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (targs)
715d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    Size += ExplicitTemplateArgumentList::sizeFor(*targs);
7161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
71783f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
7182577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo, ty);
7196bb8017bb9e828d118e15e59d71c66bba323c364John McCall
7206bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (hasQualOrFound) {
7216bb8017bb9e828d118e15e59d71c66bba323c364John McCall    if (qual && qual->isDependent()) {
7226bb8017bb9e828d118e15e59d71c66bba323c364John McCall      E->setValueDependent(true);
7236bb8017bb9e828d118e15e59d71c66bba323c364John McCall      E->setTypeDependent(true);
7246bb8017bb9e828d118e15e59d71c66bba323c364John McCall    }
7256bb8017bb9e828d118e15e59d71c66bba323c364John McCall    E->HasQualifierOrFoundDecl = true;
7266bb8017bb9e828d118e15e59d71c66bba323c364John McCall
7276bb8017bb9e828d118e15e59d71c66bba323c364John McCall    MemberNameQualifier *NQ = E->getMemberQualifier();
7286bb8017bb9e828d118e15e59d71c66bba323c364John McCall    NQ->NNS = qual;
7296bb8017bb9e828d118e15e59d71c66bba323c364John McCall    NQ->Range = qualrange;
7306bb8017bb9e828d118e15e59d71c66bba323c364John McCall    NQ->FoundDecl = founddecl;
7316bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
7326bb8017bb9e828d118e15e59d71c66bba323c364John McCall
7336bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (targs) {
7346bb8017bb9e828d118e15e59d71c66bba323c364John McCall    E->HasExplicitTemplateArgumentList = true;
735096832c5ed5b9106fa177ebc148489760c3bc496John McCall    E->getExplicitTemplateArgs().initializeFrom(*targs);
7366bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
7376bb8017bb9e828d118e15e59d71c66bba323c364John McCall
7386bb8017bb9e828d118e15e59d71c66bba323c364John McCall  return E;
73983f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor}
74083f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor
741f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlssonconst char *CastExpr::getCastKindName() const {
742f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  switch (getCastKind()) {
7432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_Unknown:
744f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "Unknown";
7452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BitCast:
746f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "BitCast";
7472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_LValueBitCast:
748e39a3894513349908cdb3beba2614e53cb288e6cDouglas Gregor    return "LValueBitCast";
7492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NoOp:
750f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "NoOp";
7512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerived:
75211de6de25a0110cd7be97eef761ef3b189781da6Anders Carlsson    return "BaseToDerived";
7532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBase:
754f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "DerivedToBase";
7552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UncheckedDerivedToBase:
75623cba801e11b03929c44f8cf54578305963a3476John McCall    return "UncheckedDerivedToBase";
7572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_Dynamic:
758f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "Dynamic";
7592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToUnion:
760f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ToUnion";
7612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ArrayToPointerDecay:
762f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ArrayToPointerDecay";
7632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FunctionToPointerDecay:
764f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "FunctionToPointerDecay";
7652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NullToMemberPointer:
766f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "NullToMemberPointer";
7672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerivedMemberPointer:
768f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "BaseToDerivedMemberPointer";
7692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBaseMemberPointer:
7701a31a18db9d657751f38c724adc0d62e86852bd7Anders Carlsson    return "DerivedToBaseMemberPointer";
7712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UserDefinedConversion:
772f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "UserDefinedConversion";
7732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ConstructorConversion:
774f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ConstructorConversion";
7752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToPointer:
7767f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson    return "IntegralToPointer";
7772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_PointerToIntegral:
7787f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson    return "PointerToIntegral";
7792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToVoid:
780ebeaf2031c968143c531bfe232d7507f20c57347Anders Carlsson    return "ToVoid";
7812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_VectorSplat:
78216a8904f3f5ed19158657e1da95e5902fbee66f7Anders Carlsson    return "VectorSplat";
7832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralCast:
78482debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "IntegralCast";
7852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToFloating:
78682debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "IntegralToFloating";
7872de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FloatingToIntegral:
78882debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "FloatingToIntegral";
7892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FloatingCast:
790c6b29163557d02da5d2a4a06f986f0480291f51fBenjamin Kramer    return "FloatingCast";
7912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_MemberPointerToBoolean:
792bc0e0781da778bd5eb41a810419912893ae20448Anders Carlsson    return "MemberPointerToBoolean";
7932de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_AnyPointerToObjCPointerCast:
7944cbf9d43cc47bb7a070c5c5026521d7d6a8f73c7Fariborz Jahanian    return "AnyPointerToObjCPointerCast";
7952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_AnyPointerToBlockPointerCast:
7963b27f1a80e4e433b503efd344c909eeafaa9033cFariborz Jahanian    return "AnyPointerToBlockPointerCast";
7972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ObjCObjectLValueCast:
798569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    return "ObjCObjectLValueCast";
799f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  }
8001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
801f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  assert(0 && "Unhandled cast kind!");
802f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  return 0;
803f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson}
804f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson
8056eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas GregorExpr *CastExpr::getSubExprAsWritten() {
8066eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  Expr *SubExpr = 0;
8076eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  CastExpr *E = this;
8086eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  do {
8096eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    SubExpr = E->getSubExpr();
810c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
8116eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Skip any temporary bindings; they're implicit.
8126eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
8136eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = Binder->getSubExpr();
814c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
8156eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Conversions by constructor and conversion functions have a
8166eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // subexpression describing the call; strip it off.
8172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (E->getCastKind() == CK_ConstructorConversion)
8186eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
8192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    else if (E->getCastKind() == CK_UserDefinedConversion)
8206eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
821c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
8226eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // If the subexpression we're left with is an implicit cast, look
8236eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // through that, too.
824c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
825c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
8266eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return SubExpr;
8276eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
8286eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
829f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCXXBaseSpecifier **CastExpr::path_buffer() {
830f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  switch (getStmtClass()) {
831f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define ABSTRACT_STMT(x)
832f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define CASTEXPR(Type, Base) \
833f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  case Stmt::Type##Class: \
834f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
835f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define STMT(Type, Base)
836f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#include "clang/AST/StmtNodes.inc"
837f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  default:
838f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    llvm_unreachable("non-cast expressions not possible here");
839f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
840f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  }
841f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
842f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
843f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallvoid CastExpr::setCastPath(const CXXCastPath &Path) {
844f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  assert(Path.size() == path_size());
845f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
846f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
847f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
848f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
849f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                           CastKind Kind, Expr *Operand,
850f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                           const CXXCastPath *BasePath,
8515baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                           ExprValueKind VK) {
852f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  unsigned PathSize = (BasePath ? BasePath->size() : 0);
853f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
854f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
855f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  ImplicitCastExpr *E =
8565baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
857f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (PathSize) E->setCastPath(*BasePath);
858f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return E;
859f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
860f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
861f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
862f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                                unsigned PathSize) {
863f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
864f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
865f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
866f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
867f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
868f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
869f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
870f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       CastKind K, Expr *Op,
871f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       const CXXCastPath *BasePath,
872f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       TypeSourceInfo *WrittenTy,
873f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       SourceLocation L, SourceLocation R) {
874f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  unsigned PathSize = (BasePath ? BasePath->size() : 0);
875f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
876f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
877f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CStyleCastExpr *E =
878f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    new (Buffer) CStyleCastExpr(T, K, Op, PathSize, WrittenTy, L, R);
879f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (PathSize) E->setCastPath(*BasePath);
880f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return E;
881f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
882f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
883f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
884f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
885f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
886f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
887f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
888f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "<<=".
8915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *BinaryOperator::getOpcodeStr(Opcode Op) {
8925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
8932de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_PtrMemD:   return ".*";
8942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_PtrMemI:   return "->*";
8952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:       return "*";
8962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Div:       return "/";
8972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Rem:       return "%";
8982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:       return "+";
8992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:       return "-";
9002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shl:       return "<<";
9012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shr:       return ">>";
9022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LT:        return "<";
9032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_GT:        return ">";
9042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LE:        return "<=";
9052de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_GE:        return ">=";
9062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_EQ:        return "==";
9072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_NE:        return "!=";
9082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_And:       return "&";
9092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Xor:       return "^";
9102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Or:        return "|";
9112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LAnd:      return "&&";
9122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LOr:       return "||";
9132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Assign:    return "=";
9142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_MulAssign: return "*=";
9152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_DivAssign: return "/=";
9162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_RemAssign: return "%=";
9172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_AddAssign: return "+=";
9182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_SubAssign: return "-=";
9192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_ShlAssign: return "<<=";
9202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_ShrAssign: return ">>=";
9212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_AndAssign: return "&=";
9222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_XorAssign: return "^=";
9232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_OrAssign:  return "|=";
9242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Comma:     return ",";
9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
926baf534875ed0a55c6342636ff3f4602b8ac22b69Douglas Gregor
927baf534875ed0a55c6342636ff3f4602b8ac22b69Douglas Gregor  return "";
9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCallBinaryOperatorKind
931063daf6e196c51f162e0485478355d8e280eef5cDouglas GregorBinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
932063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  switch (OO) {
933b7beee9f2b52f34a3b0800a5f0038f0e4295b260Chris Lattner  default: assert(false && "Not an overloadable binary operator");
9342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Plus: return BO_Add;
9352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Minus: return BO_Sub;
9362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Star: return BO_Mul;
9372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Slash: return BO_Div;
9382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Percent: return BO_Rem;
9392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Caret: return BO_Xor;
9402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Amp: return BO_And;
9412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Pipe: return BO_Or;
9422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Equal: return BO_Assign;
9432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Less: return BO_LT;
9442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Greater: return BO_GT;
9452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PlusEqual: return BO_AddAssign;
9462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_MinusEqual: return BO_SubAssign;
9472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_StarEqual: return BO_MulAssign;
9482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_SlashEqual: return BO_DivAssign;
9492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PercentEqual: return BO_RemAssign;
9502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_CaretEqual: return BO_XorAssign;
9512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_AmpEqual: return BO_AndAssign;
9522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PipeEqual: return BO_OrAssign;
9532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessLess: return BO_Shl;
9542de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterGreater: return BO_Shr;
9552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessLessEqual: return BO_ShlAssign;
9562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterGreaterEqual: return BO_ShrAssign;
9572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_EqualEqual: return BO_EQ;
9582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_ExclaimEqual: return BO_NE;
9592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessEqual: return BO_LE;
9602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterEqual: return BO_GE;
9612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_AmpAmp: return BO_LAnd;
9622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PipePipe: return BO_LOr;
9632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Comma: return BO_Comma;
9642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_ArrowStar: return BO_PtrMemI;
965063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  }
966063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor}
967063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor
968063daf6e196c51f162e0485478355d8e280eef5cDouglas GregorOverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
969063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  static const OverloadedOperatorKind OverOps[] = {
970063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
971063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Star, OO_Slash, OO_Percent,
972063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Plus, OO_Minus,
973063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_LessLess, OO_GreaterGreater,
974063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
975063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_EqualEqual, OO_ExclaimEqual,
976063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Amp,
977063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Caret,
978063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Pipe,
979063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_AmpAmp,
980063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PipePipe,
981063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Equal, OO_StarEqual,
982063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_SlashEqual, OO_PercentEqual,
983063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PlusEqual, OO_MinusEqual,
984063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_LessLessEqual, OO_GreaterGreaterEqual,
985063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_AmpEqual, OO_CaretEqual,
986063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PipeEqual,
987063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Comma
988063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  };
989063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  return OverOps[Opc];
990063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor}
991063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor
992709210feee317b8d6690dd1d15c2b74cfe55e261Ted KremenekInitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
993418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner                           Expr **initExprs, unsigned numInits,
9944c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                           SourceLocation rbraceloc)
99573460a32bc5299a5927d23d2e464d72af796eabfDouglas Gregor  : Expr(InitListExprClass, QualType(), false, false),
996709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs(C, numInits),
9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
998c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    UnionFieldInit(0), HadArrayRangeDesignator(false)
999c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt{
1000ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  for (unsigned I = 0; I != numInits; ++I) {
1001ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    if (initExprs[I]->isTypeDependent())
100273460a32bc5299a5927d23d2e464d72af796eabfDouglas Gregor      TypeDependent = true;
1003ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    if (initExprs[I]->isValueDependent())
100473460a32bc5299a5927d23d2e464d72af796eabfDouglas Gregor      ValueDependent = true;
100573460a32bc5299a5927d23d2e464d72af796eabfDouglas Gregor  }
1006c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1007709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek  InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
100866b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1010709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenekvoid InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
1011ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  if (NumInits > InitExprs.size())
1012709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs.reserve(C, NumInits);
1013fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor}
1014fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
1015709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenekvoid InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
1016709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek  InitExprs.resize(C, NumInits, 0);
10174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
10184c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1019709210feee317b8d6690dd1d15c2b74cfe55e261Ted KremenekExpr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
1020ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  if (Init >= InitExprs.size()) {
1021709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
1022ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    InitExprs.back() = expr;
1023ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    return 0;
10244c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
10251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
10274c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitExprs[Init] = expr;
10284c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
10294c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
10304c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1031bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block.
10324eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
10334eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroffconst FunctionType *BlockExpr::getFunctionType() const {
10346217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  return getType()->getAs<BlockPointerType>()->
1035183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                    getPointeeType()->getAs<FunctionType>();
10364eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
10374eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
10381eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSourceLocation BlockExpr::getCaretLocation() const {
10391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getCaretLocation();
104056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
10411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst Stmt *BlockExpr::getBody() const {
10427297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor  return TheBlock->getBody();
10437297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
10441eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt *BlockExpr::getBody() {
10451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getBody();
10467297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
104756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
104856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
10505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines
10515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1053026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// isUnusedResultAWarning - Return true if this immediate expression should
1054026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// be warned about if the result is unused.  If so, fill in Loc and Ranges
1055026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// with location to warn on and the source range[s] to report with the
1056026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// warning.
1057026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattnerbool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
1058df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump                                  SourceRange &R2, ASTContext &Ctx) const {
1059ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // Don't warn if the expr is type dependent. The type could end up
1060ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // instantiating to void.
1061ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  if (isTypeDependent())
1062ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson    return false;
10631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
10655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
10660faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
10670faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
1068026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = getExprLoc();
1069026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
1070026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass:
1072026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return cast<ParenExpr>(this)->getSubExpr()->
1073df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      isUnusedResultAWarning(Loc, R1, R2, Ctx);
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UnaryOperatorClass: {
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const UnaryOperator *UO = cast<UnaryOperator>(this);
10761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (UO->getOpcode()) {
1078026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    default: break;
10792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostInc:
10802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostDec:
10812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreInc:
10822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreDec:                 // ++/--
1083026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;  // Not a warning.
10842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Deref:
10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Dereferencing a volatile pointer is a side-effect.
1086df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1087026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
1088026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
10892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Real:
10902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Imag:
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // accessing a piece of a volatile complex is a side-effect.
1092df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1093df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump          .isVolatileQualified())
1094026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
1095026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
10962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
1097df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1099026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = UO->getOperatorLoc();
1100026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = UO->getSubExpr()->getSourceRange();
1101026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1103e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  case BinaryOperatorClass: {
1104026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const BinaryOperator *BO = cast<BinaryOperator>(this);
1105c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek    switch (BO->getOpcode()) {
1106c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek      default:
1107c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        break;
110825973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider the RHS of comma for side effects. LHS was checked by
110925973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Sema::CheckCommaOperands.
11102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_Comma:
1111c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // ((foo = <blah>), 0) is an idiom for hiding the result (and
1112c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // lvalue-ness) of an assignment written in a macro.
1113c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        if (IntegerLiteral *IE =
1114c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek              dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1115c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek          if (IE->getValue() == 0)
1116c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek            return false;
111725973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
111825973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider '||', '&&' to have side effects if the LHS or RHS does.
11192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LAnd:
11202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LOr:
112125973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
112225973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis            !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
112325973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis          return false;
112425973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        break;
1125bf0ee354163f87623a4b60412544243911332343John McCall    }
1126026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (BO->isAssignmentOp())
1127026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1128026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = BO->getOperatorLoc();
1129026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = BO->getLHS()->getSourceRange();
1130026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = BO->getRHS()->getSourceRange();
1131026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1132e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  }
1133eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner  case CompoundAssignOperatorClass:
1134c6dfe194f623b02c123759f235b504d4850fc178Douglas Gregor  case VAArgExprClass:
1135026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1137ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  case ConditionalOperatorClass: {
1138026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // The condition must be evaluated, but if either the LHS or RHS is a
1139026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // warning, warn about them.
1140ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
11411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (Exp->getLHS() &&
1142df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump        Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1143026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return true;
1144df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1145ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  }
1146ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case MemberExprClass:
1148026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If the base pointer or element is to a volatile pointer/field, accessing
1149026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // it is a side effect.
1150df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1151026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1152026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<MemberExpr>(this)->getMemberLoc();
1153026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = SourceRange(Loc, Loc);
1154026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1155026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ArraySubscriptExprClass:
11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If the base pointer or element is to a volatile pointer/field, accessing
1159026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // it is a side effect.
1160df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1161026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1162026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1163026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1164026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1165026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1166211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman
11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case CallExprClass:
1168852871abbff45f1c1d3787755a27fce08365b166Eli Friedman  case CXXOperatorCallExprClass:
1169852871abbff45f1c1d3787755a27fce08365b166Eli Friedman  case CXXMemberCallExprClass: {
1170026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If this is a direct call, get the callee.
1171026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const CallExpr *CE = cast<CallExpr>(this);
1172d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    if (const Decl *FD = CE->getCalleeDecl()) {
1173026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // If the callee has attribute pure, const, or warn_unused_result, warn
1174026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // about it. void foo() { strlen("bar"); } should warn.
1175bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      //
1176bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1177bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // updated to match for QoI.
1178bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<WarnUnusedResultAttr>() ||
1179bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1180bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Loc = CE->getCallee()->getLocStart();
1181bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        R1 = CE->getCallee()->getSourceRange();
1182bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner
1183bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        if (unsigned NumArgs = CE->getNumArgs())
1184bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          R2 = SourceRange(CE->getArg(0)->getLocStart(),
1185bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner                           CE->getArg(NumArgs-1)->getLocEnd());
1186bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return true;
1187bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
1188026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    }
1189026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
1190026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner  }
119158beed91d468863b8c85bce43425422703838d27Anders Carlsson
119258beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXTemporaryObjectExprClass:
119358beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXConstructExprClass:
119458beed91d468863b8c85bce43425422703838d27Anders Carlsson    return false;
119558beed91d468863b8c85bce43425422703838d27Anders Carlsson
1196f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  case ObjCMessageExprClass: {
1197f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1198f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMethodDecl *MD = ME->getMethodDecl();
1199f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1200f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      Loc = getExprLoc();
1201f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      return true;
1202f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    }
1203026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
1204f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  }
12051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
120609105f52b1f28cbb1374c27c3c70f5517e2c465dFariborz Jahanian  case ObjCImplicitSetterGetterRefExprClass: {   // Dot syntax for message send.
1207a50089ec68a583d13718107c1b0c898f0903709eChris Lattner#if 0
12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const ObjCImplicitSetterGetterRefExpr *Ref =
120909105f52b1f28cbb1374c27c3c70f5517e2c465dFariborz Jahanian      cast<ObjCImplicitSetterGetterRefExpr>(this);
1210a50089ec68a583d13718107c1b0c898f0903709eChris Lattner    // FIXME: We really want the location of the '.' here.
1211154440e6a8fa6ac5bca395876d79b530b39a2c1cFariborz Jahanian    Loc = Ref->getLocation();
1212154440e6a8fa6ac5bca395876d79b530b39a2c1cFariborz Jahanian    R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1213154440e6a8fa6ac5bca395876d79b530b39a2c1cFariborz Jahanian    if (Ref->getBase())
1214154440e6a8fa6ac5bca395876d79b530b39a2c1cFariborz Jahanian      R2 = Ref->getBase()->getSourceRange();
12155e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner#else
12165e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    Loc = getExprLoc();
12175e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    R1 = getSourceRange();
1218a50089ec68a583d13718107c1b0c898f0903709eChris Lattner#endif
1219a50089ec68a583d13718107c1b0c898f0903709eChris Lattner    return true;
1220a50089ec68a583d13718107c1b0c898f0903709eChris Lattner  }
1221611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  case StmtExprClass: {
1222611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // Statement exprs don't logically have side effects themselves, but are
1223611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // sometimes used in macros in ways that give them a type that is unused.
1224611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1225611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // however, if the result of the stmt expr is dead, we don't want to emit a
1226611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // warning.
1227611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1228611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    if (!CS->body_empty())
1229611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner      if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
1230df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump        return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
12311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12320faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
12330faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
1234026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<StmtExpr>(this)->getLParenLoc();
1235026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
1236026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1237611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  }
12386eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor  case CStyleCastExprClass:
1239fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner    // If this is an explicit cast to void, allow it.  People do this when they
1240fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner    // think they know what they're doing :).
1241026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (getType()->isVoidType())
1242fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner      return false;
1243026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1244026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1245026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
124658beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXFunctionalCastExprClass: {
12470faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
12480faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
124958beed91d468863b8c85bce43425422703838d27Anders Carlsson    const CastExpr *CE = cast<CastExpr>(this);
1250c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
125158beed91d468863b8c85bce43425422703838d27Anders Carlsson    // If this is a cast to void or a constructor conversion, check the operand.
125258beed91d468863b8c85bce43425422703838d27Anders Carlsson    // Otherwise, the result of the cast is unused.
12532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CE->getCastKind() == CK_ToVoid ||
12542de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        CE->getCastKind() == CK_ConstructorConversion)
1255df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      return (cast<CastExpr>(this)->getSubExpr()
1256df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump              ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
1257026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1258026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1259026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
126058beed91d468863b8c85bce43425422703838d27Anders Carlsson  }
12611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12624be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman  case ImplicitCastExprClass:
12634be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman    // Check the operand, since implicit casts are inserted by Sema
1264df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<ImplicitCastExpr>(this)
1265df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
12664be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman
126704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
1268df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXDefaultArgExpr>(this)
1269df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
12704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
12714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXNewExprClass:
12724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // FIXME: In theory, there might be new expressions that don't have side
12734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // effects (e.g. a placement new with an uninitialized POD).
12744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXDeleteExprClass:
1275026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
12762d46eb21eb2c904831b0e9f75ab3523384c70e66Anders Carlsson  case CXXBindTemporaryExprClass:
1277df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXBindTemporaryExpr>(this)
1278df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
12796b1d283fe879fb11d7ce7a69feecf66e77b0eaf3Anders Carlsson  case CXXExprWithTemporariesClass:
1280df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXExprWithTemporaries>(this)
1281df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
12824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
12835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
128544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian/// isOBJCGCCandidate - Check if an expression is objc gc'able.
12867f4f86a2167abc116275e49c81350fc3225485e5Fariborz Jahanian/// returns true, if it is; false otherwise.
1287102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanianbool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
128844baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  switch (getStmtClass()) {
128944baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  default:
129044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
129144baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ObjCIvarRefExprClass:
129244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return true;
1293207c5210eb0ac7b632609f0c006eb97ef2738948Fariborz Jahanian  case Expr::UnaryOperatorClass:
1294102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
129544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ParenExprClass:
1296102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
129744baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ImplicitCastExprClass:
1298102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
129906b89124a9a5971a0528cc9da6817740bac43164Fariborz Jahanian  case CStyleCastExprClass:
1300102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
1301a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  case DeclRefExprClass: {
130244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    const Decl *D = cast<DeclRefExpr>(this)->getDecl();
1303102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1304102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      if (VD->hasGlobalStorage())
1305102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian        return true;
1306102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      QualType T = VD->getType();
130759a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // dereferencing to a  pointer is always a gc'able candidate,
130859a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // unless it is __weak.
13097e88a60d38b36695520e4f8d9279766ef111a662Daniel Dunbar      return T->isPointerType() &&
13100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall             (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
1311102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    }
131244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
131344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
131483f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  case MemberExprClass: {
131544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    const MemberExpr *M = cast<MemberExpr>(this);
1316102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return M->getBase()->isOBJCGCCandidate(Ctx);
131744baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
131844baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ArraySubscriptExprClass:
1319102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
132044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
132144baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian}
1322369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1323369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1324369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                          Expr::CanThrowResult CT2) {
1325369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // CanThrowResult constants are ordered so that the maximum is the correct
1326369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // merge result.
1327369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return CT1 > CT2 ? CT1 : CT2;
1328369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1329369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1330369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1331369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr *E = const_cast<Expr*>(CE);
1332369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr::CanThrowResult R = Expr::CT_Cannot;
1333369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  for (Expr::child_iterator I = E->child_begin(), IE = E->child_end();
1334369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl       I != IE && R != Expr::CT_Can; ++I) {
1335369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1336369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1337369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return R;
1338369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1339369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1340369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1341369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                           bool NullThrows = true) {
1342369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!D)
1343369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1344369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1345369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // See if we can get a function type from the decl somehow.
1346369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1347369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!VD) // If we have no clue what we're calling, assume the worst.
1348369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Can;
1349369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
13505221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  // As an extension, we assume that __attribute__((nothrow)) functions don't
13515221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  // throw.
13525221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
13535221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl    return Expr::CT_Cannot;
13545221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl
1355369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  QualType T = VD->getType();
1356369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const FunctionProtoType *FT;
1357369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if ((FT = T->getAs<FunctionProtoType>())) {
1358369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  } else if (const PointerType *PT = T->getAs<PointerType>())
1359369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1360369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1361369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1362369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1363369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1364369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1365369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1366369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1367369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!FT)
1368369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Can;
1369369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1370369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1371369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1372369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1373369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1374369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (DC->isTypeDependent())
1375369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Dependent;
1376369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1377295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  if (!DC->getTypeAsWritten()->isReferenceType())
1378295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl    return Expr::CT_Cannot;
1379295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl
1380369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1381369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1382369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1383369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1384369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                           const CXXTypeidExpr *DC) {
1385369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (DC->isTypeOperand())
1386369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1387369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1388369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr *Op = DC->getExprOperand();
1389369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (Op->isTypeDependent())
1390369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Dependent;
1391369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1392369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const RecordType *RT = Op->getType()->getAs<RecordType>();
1393369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!RT)
1394369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1395369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1396369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1397369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1398369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1399369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (Op->Classify(C).isPRValue())
1400369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1401369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1402369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return Expr::CT_Can;
1403369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1404369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1405369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian RedlExpr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1406369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // C++ [expr.unary.noexcept]p3:
1407369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  //   [Can throw] if in a potentially-evaluated context the expression would
1408369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  //   contain:
1409369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  switch (getStmtClass()) {
1410369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXThrowExprClass:
1411369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated throw-expression
1412369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
1413369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1414369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDynamicCastExprClass: {
1415369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1416369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     where T is a reference type, that requires a run-time check
1417369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1418369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1419369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1420369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1421369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1422369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1423369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXTypeidExprClass:
1424369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated typeid expression applied to a glvalue
1425369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     expression whose type is a polymorphic class type
1426369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1427369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1428369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated call to a function, member function, function
1429369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     pointer, or member function pointer that does not have a non-throwing
1430369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     exception-specification
1431369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CallExprClass:
1432369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXOperatorCallExprClass:
1433369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXMemberCallExprClass: {
1434369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1435369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1436369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1437369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1438369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1439369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1440295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  case CXXConstructExprClass:
1441295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  case CXXTemporaryObjectExprClass: {
1442369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = CanCalleeThrow(
1443369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl        cast<CXXConstructExpr>(this)->getConstructor());
1444369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1445369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1446369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1447369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1448369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1449369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXNewExprClass: {
1450369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = MergeCanThrow(
1451369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl        CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1452369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl        CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1453369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                       /*NullThrows*/false));
1454369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1455369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1456369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1457369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1458369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1459369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDeleteExprClass: {
1460369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = CanCalleeThrow(
1461369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl        cast<CXXDeleteExpr>(this)->getOperatorDelete());
1462369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1463369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
14640b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
14650b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    // Unwrap exactly one implicit cast, which converts all pointers to void*.
14660b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
14670b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      Arg = Cast->getSubExpr();
14680b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
14690b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
14700b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl        CanThrowResult CT2 = CanCalleeThrow(
14710b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl            cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
14720b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl        if (CT2 == CT_Can)
14730b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl          return CT2;
14740b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl        CT = MergeCanThrow(CT, CT2);
14750b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      }
14760b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    }
14770b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
14780b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl  }
14790b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl
14800b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl  case CXXBindTemporaryExprClass: {
14810b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    // The bound temporary has to be destroyed again, which might throw.
14820b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    CanThrowResult CT = CanCalleeThrow(
14830b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
14840b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    if (CT == CT_Can)
14850b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      return CT;
1486369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1487369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1488369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1489369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // ObjC message sends are like function calls, but never have exception
1490369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // specs.
1491369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCMessageExprClass:
1492369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCPropertyRefExprClass:
1493369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCImplicitSetterGetterRefExprClass:
1494369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
1495369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1496369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Many other things have subexpressions, so we have to test those.
1497369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some are simple:
1498369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ParenExprClass:
1499369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case MemberExprClass:
1500369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXReinterpretCastExprClass:
1501369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXConstCastExprClass:
1502369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ConditionalOperatorClass:
1503369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CompoundLiteralExprClass:
1504369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ExtVectorElementExprClass:
1505369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case InitListExprClass:
1506369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case DesignatedInitExprClass:
1507369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ParenListExprClass:
1508369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case VAArgExprClass:
1509369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDefaultArgExprClass:
15100b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl  case CXXExprWithTemporariesClass:
1511369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCIvarRefExprClass:
1512369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCIsaExprClass:
1513369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ShuffleVectorExprClass:
1514369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CanSubExprsThrow(C, this);
1515369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1516369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some might be dependent for other reasons.
1517369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case UnaryOperatorClass:
1518369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ArraySubscriptExprClass:
1519369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ImplicitCastExprClass:
1520369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CStyleCastExprClass:
1521369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXStaticCastExprClass:
1522369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXFunctionalCastExprClass:
1523369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case BinaryOperatorClass:
1524369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CompoundAssignOperatorClass: {
1525369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1526369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1527369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1528369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1529369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1530369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case StmtExprClass:
1531369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
1532369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1533369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ChooseExprClass:
1534369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (isTypeDependent() || isValueDependent())
1535369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT_Dependent;
1536369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1537369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1538369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some expressions are always dependent.
1539369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case DependentScopeDeclRefExprClass:
1540369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXUnresolvedConstructExprClass:
1541369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDependentScopeMemberExprClass:
1542369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Dependent;
1543369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1544369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  default:
1545369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // All other expressions don't have subexpressions, or else they are
1546369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // unevaluated.
1547369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Cannot;
1548369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1549369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1550369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
15514e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() {
15524e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  Expr* E = this;
15534e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  while (ParenExpr* P = dyn_cast<ParenExpr>(E))
15544e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek    E = P->getSubExpr();
15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15564e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  return E;
15574e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek}
15584e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek
155956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
156056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand.
156156f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() {
156256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  Expr *E = this;
156356f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  while (true) {
156456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner    if (ParenExpr *P = dyn_cast<ParenExpr>(E))
156556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
156656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner    else if (CastExpr *P = dyn_cast<CastExpr>(E))
156756f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
156856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner    else
156956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      return E;
157056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  }
157156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner}
157256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner
15732fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCallExpr *Expr::IgnoreParenImpCasts() {
15742fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  Expr *E = this;
15752fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  while (true) {
15762fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall    if (ParenExpr *P = dyn_cast<ParenExpr>(E))
15772fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      E = P->getSubExpr();
15782fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall    else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
15792fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      E = P->getSubExpr();
15802fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall    else
15812fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      return E;
15822fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  }
15832fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall}
15842fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall
1585ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1586ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// value (including ptr->int casts of the same size).  Strip off any
1587ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// ParenExpr or CastExprs, returning their operand.
1588ecdd84147c0765caa999ddc22dde25b42712bb4dChris LattnerExpr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1589ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  Expr *E = this;
1590ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  while (true) {
1591ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1592ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      E = P->getSubExpr();
1593ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      continue;
1594ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    }
15951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1596ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1597ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      // We ignore integer <-> casts that are of the same width, ptr<->ptr and
15982ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor      // ptr<->int casts of the same width.  We also ignore all identity casts.
1599ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      Expr *SE = P->getSubExpr();
16001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1601ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1602ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
1603ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
1604ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
16051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16069d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor      if ((E->getType()->isPointerType() ||
16079d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           E->getType()->isIntegralType(Ctx)) &&
16089d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor          (SE->getType()->isPointerType() ||
16099d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           SE->getType()->isIntegralType(Ctx)) &&
1610ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner          Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1611ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
1612ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
1613ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
1614ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    }
16151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1616ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    return E;
1617ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  }
1618ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner}
1619ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
16206eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorbool Expr::isDefaultArgument() const {
16216eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  const Expr *E = this;
16226eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
16236eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    E = ICE->getSubExprAsWritten();
1624c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
16256eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return isa<CXXDefaultArgExpr>(E);
16266eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
1627ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
16282f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// \brief Skip over any no-op casts and any temporary-binding
16292f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// expressions.
16302f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregorstatic const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
16312f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
16322de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
16332f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
16342f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
16352f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
16362f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
16372f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
16382f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
16392f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    E = BE->getSubExpr();
16402f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
16412f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
16422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
16432f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
16442f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
16452f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
16462f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
1647c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
16482f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  return E;
16492f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
16502f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
1651558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// isTemporaryObject - Determines if this expression produces a
1652558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// temporary of the given class type.
1653558d2abc7f9fd6801cc7677200992313ae90b5d8John McCallbool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1654558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1655558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    return false;
16562f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
1657558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
16582f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
165958277b57f9492d0234748be89bcad48b322c5cf7John McCall  // Temporaries are by definition pr-values of class type.
1660558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (!E->Classify(C).isPRValue()) return false;
16612f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
1662558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  // Black-list implicit derived-to-base conversions, which are the
1663558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  // only way we can get a pr-value of class type that doesn't refer
1664558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  // to a temporary of that type.
1665558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (isa<ImplicitCastExpr>(E)) {
1666558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1667558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_DerivedToBase:
1668558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_UncheckedDerivedToBase:
1669558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      return false;
1670558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    default:
1671558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      break;
1672558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    }
16732f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
16742f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
1675558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  return true;
16762f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
16772f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
1678898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyTypeDependentArguments - Determines if any of the expressions
1679898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is type-dependent.
1680898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1681898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
1682898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isTypeDependent())
1683898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
1684898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1685898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
1686898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1687898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1688898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyValueDependentArguments - Determines if any of the expressions
1689898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is value-dependent.
1690898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1691898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
1692898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isValueDependent())
1693898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
1694898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1695898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
1696898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1697898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
16984204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCallbool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
1699c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // This function is attempting whether an expression is an initializer
1700c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // which can be evaluated at compile-time.  isEvaluatable handles most
1701c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // of the cases, but it can't deal with some initializer-specific
1702c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // expressions, and it can't deal with aggregates; we deal with those here,
1703c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // and fall back to isEvaluatable for the other cases.
1704c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman
17054204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // If we ever capture reference-binding directly in the AST, we can
17064204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // kill the second parameter.
17074204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
17084204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  if (IsForRef) {
17094204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    EvalResult Result;
17104204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
17114204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  }
17121f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman
1713e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  switch (getStmtClass()) {
1714c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  default: break;
1715e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case StringLiteralClass:
171614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case ObjCStringLiteralClass:
1717eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  case ObjCEncodeExprClass:
1718e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    return true;
1719b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXTemporaryObjectExprClass:
1720b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXConstructExprClass: {
1721b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall    const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
17224204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
17234204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    // Only if it's
17244204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    // 1) an application of the trivial default constructor or
1725b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall    if (!CE->getConstructor()->isTrivial()) return false;
17264204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    if (!CE->getNumArgs()) return true;
17274204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
17284204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    // 2) an elidable trivial copy construction of an operand which is
17294204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    //    itself a constant initializer.  Note that we consider the
17304204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    //    operand on its own, *not* as a reference binding.
17314204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return CE->isElidable() &&
17324204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall           CE->getArg(0)->isConstantInitializer(Ctx, false);
1733b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  }
173459b5da6d853b4368b984700315adf7b37de05764Nate Begeman  case CompoundLiteralExprClass: {
17351f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // This handles gcc's extension that allows global initializers like
17361f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // "struct x {int x;} x = (struct x) {};".
17371f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This accepts other cases it shouldn't!
173859b5da6d853b4368b984700315adf7b37de05764Nate Begeman    const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
17394204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return Exp->isConstantInitializer(Ctx, false);
174059b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
1741e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case InitListExprClass: {
17421f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This doesn't deal with fields with reference types correctly.
17431f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This incorrectly allows pointers cast to integers to be assigned
17441f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // to bitfields.
1745e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    const InitListExpr *Exp = cast<InitListExpr>(this);
1746e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    unsigned numInits = Exp->getNumInits();
1747e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    for (unsigned i = 0; i < numInits; i++) {
17484204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
1749e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson        return false;
1750e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    }
1751c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    return true;
1752e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
17533498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  case ImplicitValueInitExprClass:
17543498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return true;
17553ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  case ParenExprClass:
17564204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return cast<ParenExpr>(this)->getSubExpr()
17574204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      ->isConstantInitializer(Ctx, IsForRef);
1758c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case UnaryOperatorClass: {
1759c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    const UnaryOperator* Exp = cast<UnaryOperator>(this);
17602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Exp->getOpcode() == UO_Extension)
17614204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
1762c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
1763c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  }
17643ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  case BinaryOperatorClass: {
17653ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    // Special case &&foo - &&bar.  It would be nice to generalize this somehow
17663ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    // but this handles the common case.
17673ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    const BinaryOperator *Exp = cast<BinaryOperator>(this);
17682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Exp->getOpcode() == BO_Sub &&
17693ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner        isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
17703ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner        isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
17713ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner      return true;
17723ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    break;
17733ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  }
17744204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  case CXXFunctionalCastExprClass:
1775b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXStaticCastExprClass:
177681045d8dcd967def69d8e0945566214a9fe9ffccChris Lattner  case ImplicitCastExprClass:
1777c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case CStyleCastExprClass:
1778c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // Handle casts with a destination that's a struct or union; this
1779c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // deals with both the gcc no-op struct cast extension and the
1780c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // cast-to-union extension.
1781c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    if (getType()->isRecordType())
17824204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return cast<CastExpr>(this)->getSubExpr()
17834204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall        ->isConstantInitializer(Ctx, false);
1784c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1785430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    // Integer->integer casts can be handled here, which is important for
1786430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    // things like (int)(&&x-&&y).  Scary but true.
1787430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    if (getType()->isIntegerType() &&
1788430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner        cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
17894204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return cast<CastExpr>(this)->getSubExpr()
17904204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall        ->isConstantInitializer(Ctx, false);
1791c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1792c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
1793e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
1794c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  return isEvaluatable(Ctx);
179538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff}
179638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
17975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNullPointerConstant - C99 6.3.2.3p3 -  Return true if this is either an
17985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// integer constant expression with the value zero, or if this is one that is
17995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// cast to void*.
1800ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregorbool Expr::isNullPointerConstant(ASTContext &Ctx,
1801ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor                                 NullPointerConstantValueDependence NPC) const {
1802ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  if (isValueDependent()) {
1803ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    switch (NPC) {
1804ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_NeverValueDependent:
1805ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor      assert(false && "Unexpected value dependent expression!");
1806ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor      // If the unthinkable happens, fall through to the safest alternative.
1807c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1808ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNull:
18099d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor      return isTypeDependent() || getType()->isIntegralType(Ctx);
1810c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1811ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNotNull:
1812ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor      return false;
1813ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    }
1814ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  }
1815f515b2268f829adfbfdb751f54d102b53ed0285cDaniel Dunbar
18160777972d38a3125efed962b045704c30ae6965cfSebastian Redl  // Strip off a cast to void*, if it exists. Except in C++.
18170835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis  if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
18186215dee86c0e715b9f2b0d401ab2a5fcf629f1afSebastian Redl    if (!Ctx.getLangOptions().CPlusPlus) {
18190777972d38a3125efed962b045704c30ae6965cfSebastian Redl      // Check that it is a cast to void*.
18206217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
18210777972d38a3125efed962b045704c30ae6965cfSebastian Redl        QualType Pointee = PT->getPointeeType();
18220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        if (!Pointee.hasQualifiers() &&
18230777972d38a3125efed962b045704c30ae6965cfSebastian Redl            Pointee->isVoidType() &&                              // to void*
18240777972d38a3125efed962b045704c30ae6965cfSebastian Redl            CE->getSubExpr()->getType()->isIntegerType())         // from int.
1825ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor          return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
18260777972d38a3125efed962b045704c30ae6965cfSebastian Redl      }
18275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1828aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1829aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Ignore the ImplicitCastExpr type entirely.
1830ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
1831aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1832aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Accept ((void*)0) as a null pointer constant, as many other
1833aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // implementations do.
1834ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
18351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else if (const CXXDefaultArgExpr *DefaultArg
18368123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner               = dyn_cast<CXXDefaultArgExpr>(this)) {
183704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    // See through default argument expressions
1838ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
18392d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor  } else if (isa<GNUNullExpr>(this)) {
18402d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    // The GNU __null extension is always a null pointer constant.
18412d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    return true;
1842aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff  }
18432d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
18446e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // C++0x nullptr_t is always a null pointer constant.
18456e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  if (getType()->isNullPtrType())
18466e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    return true;
18476e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
1848aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  // This expression must be an integer type.
1849c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  if (!getType()->isIntegerType() ||
185056fc0d18caf9c829647a5e3ce35197f0d7e0feeeFariborz Jahanian      (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
1851aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    return false;
18521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have an integer constant expression, we need to *evaluate* it and
18545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // test for the value 0.
185509de1767990d4828bcaf0dd22033a5dddeecbe08Eli Friedman  llvm::APSInt Result;
185609de1767990d4828bcaf0dd22033a5dddeecbe08Eli Friedman  return isIntegerConstantExpr(Result, Ctx) && Result == 0;
18575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
185831a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff
185933bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas GregorFieldDecl *Expr::getBitField() {
18606f4a69a3107e7ff1569c747f7c6bdf7cff8cbf55Douglas Gregor  Expr *E = this->IgnoreParens();
186133bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
1862de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
18635baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    if (ICE->getValueKind() != VK_RValue &&
18642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        ICE->getCastKind() == CK_NoOp)
1865de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      E = ICE->getSubExpr()->IgnoreParens();
1866de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    else
1867de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
1868de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  }
1869de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
187027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
187186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
187233bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor      if (Field->isBitField())
187333bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor        return Field;
187433bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
187533bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor  if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
187633bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor    if (BinOp->isAssignmentOp() && BinOp->getLHS())
187733bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor      return BinOp->getLHS()->getBitField();
187833bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
187933bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor  return 0;
188027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
188127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
1882093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlssonbool Expr::refersToVectorElement() const {
1883093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  const Expr *E = this->IgnoreParens();
1884c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1885093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
18865baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    if (ICE->getValueKind() != VK_RValue &&
18872de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        ICE->getCastKind() == CK_NoOp)
1888093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      E = ICE->getSubExpr()->IgnoreParens();
1889093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    else
1890093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      break;
1891093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  }
1892c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1893093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1894093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return ASE->getBase()->getType()->isVectorType();
1895093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
1896093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (isa<ExtVectorElementExpr>(E))
1897093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return true;
1898093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
1899093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  return false;
1900093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson}
1901093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
19022140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// isArrow - Return true if the base expression is a pointer to vector,
19032140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// return false if the base expression is a vector.
19042140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattnerbool ExtVectorElementExpr::isArrow() const {
19052140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner  return getBase()->getType()->isPointerType();
19062140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner}
19072140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner
1908213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const {
1909183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const VectorType *VT = getType()->getAs<VectorType>())
19108a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    return VT->getNumElements();
19118a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  return 1;
19124d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner}
19134d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner
19148a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated.
1915213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const {
1916a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // FIXME: Refactor this code to an accessor on the AST node which returns the
1917a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // "type" of component access, and share with code below and in Sema.
191801eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar  llvm::StringRef Comp = Accessor->getName();
1919190d6a25393995b42e32086949a68285ee423fb9Nate Begeman
1920190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Halving swizzles do not contain duplicate elements.
1921150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
1922190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    return false;
19231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1924190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Advance past s-char prefix on hex swizzles.
1925150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
1926150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar    Comp = Comp.substr(1);
19271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1928150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1929150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar    if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
1930fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff        return true;
1931150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar
1932fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  return false;
1933fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff}
1934b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
19358a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
19363b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess(
19373b8d116703db8018f855cbb4733ace426422623bNate Begeman                                  llvm::SmallVectorImpl<unsigned> &Elts) const {
19384b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  llvm::StringRef Comp = Accessor->getName();
19394b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
19404b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar    Comp = Comp.substr(1);
19411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19424b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isHi =   Comp == "hi";
19434b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isLo =   Comp == "lo";
19444b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isEven = Comp == "even";
19454b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isOdd  = Comp == "odd";
19461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19478a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
19488a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    uint64_t Index;
19491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19508a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    if (isHi)
19518a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = e + i;
19528a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isLo)
19538a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = i;
19548a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isEven)
19558a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i;
19568a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isOdd)
19578a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i + 1;
19588a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else
19594b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar      Index = ExtVectorType::getAccessorIdx(Comp[i]);
1960b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
19613b8d116703db8018f855cbb4733ace426422623bNate Begeman    Elts.push_back(Index);
1962b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner  }
19638a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman}
19648a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman
196504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
196604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
196704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation SuperLoc,
196804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 bool IsInstanceSuper,
196904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 QualType SuperType,
1970c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
197104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
197204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 Expr **Args, unsigned NumArgs,
197304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
197404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
197592e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor         /*ValueDependent=*/false),
197604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
197704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    HasMethod(Method != 0), SuperLoc(SuperLoc),
197804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
197904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
1980c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
1981c2350e553b853ad00914faf23fa731e5fc4a8a5cDouglas Gregor{
198204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(SuperType.getAsOpaquePtr());
198304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (NumArgs)
198404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
198504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
198604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
198704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
198804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
198904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 TypeSourceInfo *Receiver,
1990c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
199104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
199204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 Expr **Args, unsigned NumArgs,
199304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
199404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  : Expr(ObjCMessageExprClass, T, T->isDependentType(),
1995c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt         (T->isDependentType() ||
199604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          hasAnyValueDependentArguments(Args, NumArgs))),
199704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
199804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
199904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2000c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
200104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor{
200204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(Receiver);
200304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (NumArgs)
200404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
200504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
200604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
200704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
200804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
200904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 Expr *Receiver,
2010c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
201104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
201204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 Expr **Args, unsigned NumArgs,
201304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
201492e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor  : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
2015c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt         (Receiver->isTypeDependent() ||
201604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          hasAnyValueDependentArguments(Args, NumArgs))),
201704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
201804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
201904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2020c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
202104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor{
202204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(Receiver);
202304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (NumArgs)
202404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
202504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
202604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
202704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
202804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
202904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation SuperLoc,
203004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         bool IsInstanceSuper,
203104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         QualType SuperType,
2032c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
203304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
203404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         Expr **Args, unsigned NumArgs,
203504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2036c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
203704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    NumArgs * sizeof(Expr *);
203804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
203904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
2040c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                   SuperType, Sel, Method, Args, NumArgs,
204104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                   RBracLoc);
204204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
204304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
204404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
204504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
204604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         TypeSourceInfo *Receiver,
2047c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
204804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
204904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         Expr **Args, unsigned NumArgs,
205004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2051c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
205204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    NumArgs * sizeof(Expr *);
205304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2054c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
205504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                   NumArgs, RBracLoc);
205604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
205704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
205804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
205904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
206004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         Expr *Receiver,
2061c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
206204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
206304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         Expr **Args, unsigned NumArgs,
206404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2065c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
206604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    NumArgs * sizeof(Expr *);
206704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2068c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
206904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                   NumArgs, RBracLoc);
207004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
207104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
2072c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
207304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                              unsigned NumArgs) {
2074c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
207504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    NumArgs * sizeof(Expr *);
207604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
207704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
207804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
2079c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
208004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorSelector ObjCMessageExpr::getSelector() const {
208104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (HasMethod)
208204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
208304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                               ->getSelector();
2084c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  return Selector(SelectorOrMethod);
208504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
208604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
208704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
208804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  switch (getReceiverKind()) {
208904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case Instance:
209004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    if (const ObjCObjectPointerType *Ptr
209104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
209204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor      return Ptr->getInterfaceDecl();
209304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
20944df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
209504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case Class:
2096c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (const ObjCObjectType *Ty
2097c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          = getClassReceiver()->getAs<ObjCObjectType>())
2098c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Ty->getInterface();
209904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
21004df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
210104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case SuperInstance:
210204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    if (const ObjCObjectPointerType *Ptr
210304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          = getSuperType()->getAs<ObjCObjectPointerType>())
210404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor      return Ptr->getInterfaceDecl();
210504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
2106c2350e553b853ad00914faf23fa731e5fc4a8a5cDouglas Gregor
210704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case SuperClass:
210804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    if (const ObjCObjectPointerType *Iface
210904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                       = getSuperType()->getAs<ObjCObjectPointerType>())
211004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor      return Iface->getInterfaceDecl();
211104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
211204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  }
21130389e6bd0159bfdd08f7c50a37543b6e3adf0c33Chris Lattner
211404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return 0;
2115eb3b324800598cc3d5385fbad95ae5cff2c79113Ted Kremenek}
21160389e6bd0159bfdd08f7c50a37543b6e3adf0c33Chris Lattner
211727437caadea35f84d550cd29f024fcf3ea240eecChris Lattnerbool ChooseExpr::isConditionTrue(ASTContext &C) const {
21189a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman  return getCond()->EvaluateAsInt(C) != 0;
211927437caadea35f84d550cd29f024fcf3ea240eecChris Lattner}
212027437caadea35f84d550cd29f024fcf3ea240eecChris Lattner
2121888376a2bbcfc2f047902249f8455918e2489ae1Nate Begemanvoid ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2122888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman                                 unsigned NumExprs) {
2123888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman  if (SubExprs) C.Deallocate(SubExprs);
2124888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
2125888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman  SubExprs = new (C) Stmt* [NumExprs];
212694cd5d1397bb1a8bcd109602aa38dd787b164c22Douglas Gregor  this->NumExprs = NumExprs;
212794cd5d1397bb1a8bcd109602aa38dd787b164c22Douglas Gregor  memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
21281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2129888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
213077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
213105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//  DesignatedInitExpr
213205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
213305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
213405c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorIdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
213505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(Kind == FieldDesignator && "Only valid on a field designator");
213605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Field.NameOrField & 0x01)
213705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
213805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  else
213905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return getField()->getIdentifier();
214005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
214105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
2142c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntDesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2143319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                       unsigned NumDesignators,
2144ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       const Designator *Designators,
21451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       SourceLocation EqualOrColonLoc,
2146ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       bool GNUSyntax,
21471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       Expr **IndexExprs,
21489ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                       unsigned NumIndexExprs,
21499ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                       Expr *Init)
21501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(DesignatedInitExprClass, Ty,
21519ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor         Init->isTypeDependent(), Init->isValueDependent()),
21521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
21531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
2154319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  this->Designators = new (C) Designator[NumDesignators];
21559ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
21569ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Record the initializer itself.
21579ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  child_iterator Child = child_begin();
21589ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  *Child++ = Init;
21599ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
21609ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Copy the designators and their subexpressions, computing
21619ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // value-dependence along the way.
21629ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  unsigned IndexIdx = 0;
21639ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  for (unsigned I = 0; I != NumDesignators; ++I) {
2164ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    this->Designators[I] = Designators[I];
21659ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
21669ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    if (this->Designators[I].isArrayDesignator()) {
21679ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
21689ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Index = IndexExprs[IndexIdx];
21691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ValueDependent = ValueDependent ||
21709ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor        Index->isTypeDependent() || Index->isValueDependent();
21719ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
21729ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the index expressions into permanent storage.
21739ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
21749ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    } else if (this->Designators[I].isArrayRangeDesignator()) {
21759ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
21769ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Start = IndexExprs[IndexIdx];
21779ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *End = IndexExprs[IndexIdx + 1];
21781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ValueDependent = ValueDependent ||
21799ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor        Start->isTypeDependent() || Start->isValueDependent() ||
21809ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor        End->isTypeDependent() || End->isValueDependent();
21819ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
21829ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the start/end expressions into permanent storage.
21839ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
21849ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
21859ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    }
21869ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  }
21879ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
21889ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
2189ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
2190ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
219105c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr *
21921eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
219305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           unsigned NumDesignators,
219405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           Expr **IndexExprs, unsigned NumIndexExprs,
219505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           SourceLocation ColonOrEqualLoc,
219605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           bool UsesColonSyntax, Expr *Init) {
2197c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2198c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2199319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
22009ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                      ColonOrEqualLoc, UsesColonSyntax,
22019ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                      IndexExprs, NumIndexExprs, Init);
220205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
220305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
22041eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
2205d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                                    unsigned NumIndexExprs) {
2206d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2207d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2208d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2209d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
2210d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
2211319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregorvoid DesignatedInitExpr::setDesignators(ASTContext &C,
2212319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                        const Designator *Desigs,
2213d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                        unsigned NumDesigs) {
2214319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  Designators = new (C) Designator[NumDesigs];
2215d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  NumDesignators = NumDesigs;
2216d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  for (unsigned I = 0; I != NumDesigs; ++I)
2217d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor    Designators[I] = Desigs[I];
2218d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
2219d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
222005c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorSourceRange DesignatedInitExpr::getSourceRange() const {
222105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation StartLoc;
2222d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner  Designator &First =
2223d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    *const_cast<DesignatedInitExpr*>(this)->designators_begin();
222405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (First.isFieldDesignator()) {
2225eeae8f072748affce25ab4064982626361293390Douglas Gregor    if (GNUSyntax)
222605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
222705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else
222805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
222905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  } else
2230d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    StartLoc =
2231d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner      SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
223205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
223305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
223405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
223505c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
223605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
223705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
223805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
223905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
224005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
224105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
224205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
224305c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
22441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
224505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
224605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
224705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
224805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
224905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
225005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
225105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
225205c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
22531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
225405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
225505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
225605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
225705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
225805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
225905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
226005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
2261ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// \brief Replaces the designator at index @p Idx with the series
2262ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// of designators in [First, Last).
2263319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregorvoid DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
22641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          const Designator *First,
2265ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                          const Designator *Last) {
2266ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  unsigned NumNewDesignators = Last - First;
2267ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  if (NumNewDesignators == 0) {
2268ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    std::copy_backward(Designators + Idx + 1,
2269ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + NumDesignators,
2270ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + Idx);
2271ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    --NumNewDesignators;
2272ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
2273ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  } else if (NumNewDesignators == 1) {
2274ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    Designators[Idx] = *First;
2275ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
2276ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
2277ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
22781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Designator *NewDesignators
2279319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor    = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
2280ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators, Designators + Idx, NewDesignators);
2281ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(First, Last, NewDesignators + Idx);
2282ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators + Idx + 1, Designators + NumDesignators,
2283ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor            NewDesignators + Idx + NumNewDesignators);
2284ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  Designators = NewDesignators;
2285ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  NumDesignators = NumDesignators - 1 + NumNewDesignators;
2286ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
2287ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
22881eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
22892ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                             Expr **exprs, unsigned nexprs,
22902ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                             SourceLocation rparenloc)
22912ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman: Expr(ParenListExprClass, QualType(),
22922ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman       hasAnyTypeDependentArguments(exprs, nexprs),
22931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump       hasAnyValueDependentArguments(exprs, nexprs)),
22942ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman  NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
22951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22962ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman  Exprs = new (C) Stmt*[nexprs];
22972ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman  for (unsigned i = 0; i != nexprs; ++i)
22982ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    Exprs[i] = exprs[i];
22992ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman}
23002ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman
230105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
2302ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//  ExprIterator.
2303ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
2304ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
2305ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2306ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2307ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2308ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator[](size_t idx) const {
2309ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  return cast<Expr>(I[idx]);
2310ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek}
2311ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2312ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2313ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
2314ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
231577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//  Child Iterators for iterating over subexpressions/substatements
231677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
231777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
231877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// DeclRefExpr
23199ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
23209ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
232177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
23227779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff// ObjCIvarRefExpr
23235549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
23245549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
23257779db42c94405ecbd6ee45efb293483fa6cbeffSteve Naroff
2326e3e9add4fd788927df6f545570e7838db59c01d7Steve Naroff// ObjCPropertyRefExpr
23275549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
23285549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
2329ae7840776d6cd31b4d7a4a345b61bcbb3744df6cSteve Naroff
233009105f52b1f28cbb1374c27c3c70f5517e2c465dFariborz Jahanian// ObjCImplicitSetterGetterRefExpr
23311eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
23322fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  // If this is accessing a class member, skip that entry.
23332fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  if (Base) return &Base;
23342fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  return &Base+1;
2335154440e6a8fa6ac5bca395876d79b530b39a2c1cFariborz Jahanian}
23361eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
23371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return &Base+1;
2338154440e6a8fa6ac5bca395876d79b530b39a2c1cFariborz Jahanian}
23395daf570d0ce027e18ed5f9d66e6b2a14a40b720dFariborz Jahanian
2340cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas Gregor// ObjCSuperExpr
2341cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas GregorStmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2342cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas GregorStmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2343cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9Douglas Gregor
2344f242b1b0c4e998911cb96b2ba7e27ab4a5abaed3Steve Naroff// ObjCIsaExpr
2345f242b1b0c4e998911cb96b2ba7e27ab4a5abaed3Steve NaroffStmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2346f242b1b0c4e998911cb96b2ba7e27ab4a5abaed3Steve NaroffStmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2347f242b1b0c4e998911cb96b2ba7e27ab4a5abaed3Steve Naroff
2348d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner// PredefinedExpr
2349d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2350d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris LattnerStmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
235177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
235277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// IntegerLiteral
23539ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
23549ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
235577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
235677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CharacterLiteral
2357d603eaa682cecac2c10771a700cb83aa301653b4Chris LattnerStmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
23589ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
235977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
236077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// FloatingLiteral
23619ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
23629ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
236377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
23645d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner// ImaginaryLiteral
23655549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
23665549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
23675d66145eed1c319df5a69977cb8ff74f597ea544Chris Lattner
236877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// StringLiteral
23699ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
23709ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
237177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
237277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ParenExpr
23735549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_begin() { return &Val; }
23745549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ParenExpr::child_end() { return &Val+1; }
237577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
237677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// UnaryOperator
23775549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_begin() { return &Val; }
23785549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
237977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
23808ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor// OffsetOfExpr
23818ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas GregorStmt::child_iterator OffsetOfExpr::child_begin() {
23828ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
23838ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                      + NumComps);
23848ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
23858ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas GregorStmt::child_iterator OffsetOfExpr::child_end() {
23868ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return child_iterator(&*child_begin() + NumExprs);
23878ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
23888ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
23890518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl// SizeOfAlignOfExpr
23901eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt::child_iterator SizeOfAlignOfExpr::child_begin() {
23910518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // If this is of a type and the type is a VLA type (and not a typedef), the
23920518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // size expression of the VLA needs to be treated as an executable expression.
23930518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // Why isn't this weirdness documented better in StmtIterator?
23940518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType()) {
23950518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    if (VariableArrayType* T = dyn_cast<VariableArrayType>(
23960518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl                                   getArgumentType().getTypePtr()))
23970518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl      return child_iterator(T);
23980518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    return child_iterator();
23990518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  }
2400d457589fc69dc7a9c80cd74d317c0b81a35a27c9Sebastian Redl  return child_iterator(&Argument.Ex);
24019ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
24020518999d3adcc289997bd974dce90cc97f5c1c44Sebastian RedlStmt::child_iterator SizeOfAlignOfExpr::child_end() {
24030518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType())
24040518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl    return child_iterator();
2405d457589fc69dc7a9c80cd74d317c0b81a35a27c9Sebastian Redl  return child_iterator(&Argument.Ex + 1);
24069ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
240777ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
240877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// ArraySubscriptExpr
24091237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_begin() {
24105549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
241177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
24121237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ArraySubscriptExpr::child_end() {
24135549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+END_EXPR;
241477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
241577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
241677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek// CallExpr
24171237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_begin() {
24185549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
241977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
24201237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator CallExpr::child_end() {
24215549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+NumArgs+ARGS_START;
242277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek}
24231237c673c07f9d827129ba02720108816abde562Ted Kremenek
24241237c673c07f9d827129ba02720108816abde562Ted Kremenek// MemberExpr
24255549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_begin() { return &Base; }
24265549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator MemberExpr::child_end() { return &Base+1; }
24271237c673c07f9d827129ba02720108816abde562Ted Kremenek
2428213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman// ExtVectorElementExpr
24295549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
24305549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
24311237c673c07f9d827129ba02720108816abde562Ted Kremenek
24321237c673c07f9d827129ba02720108816abde562Ted Kremenek// CompoundLiteralExpr
24335549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
24345549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
24351237c673c07f9d827129ba02720108816abde562Ted Kremenek
24361237c673c07f9d827129ba02720108816abde562Ted Kremenek// CastExpr
24375549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_begin() { return &Op; }
24385549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator CastExpr::child_end() { return &Op+1; }
24391237c673c07f9d827129ba02720108816abde562Ted Kremenek
24401237c673c07f9d827129ba02720108816abde562Ted Kremenek// BinaryOperator
24411237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_begin() {
24425549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
24431237c673c07f9d827129ba02720108816abde562Ted Kremenek}
24441237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator BinaryOperator::child_end() {
24455549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+END_EXPR;
24461237c673c07f9d827129ba02720108816abde562Ted Kremenek}
24471237c673c07f9d827129ba02720108816abde562Ted Kremenek
24481237c673c07f9d827129ba02720108816abde562Ted Kremenek// ConditionalOperator
24491237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_begin() {
24505549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
24511237c673c07f9d827129ba02720108816abde562Ted Kremenek}
24521237c673c07f9d827129ba02720108816abde562Ted KremenekStmt::child_iterator ConditionalOperator::child_end() {
24535549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+END_EXPR;
24541237c673c07f9d827129ba02720108816abde562Ted Kremenek}
24551237c673c07f9d827129ba02720108816abde562Ted Kremenek
24561237c673c07f9d827129ba02720108816abde562Ted Kremenek// AddrLabelExpr
24579ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
24589ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
24591237c673c07f9d827129ba02720108816abde562Ted Kremenek
24601237c673c07f9d827129ba02720108816abde562Ted Kremenek// StmtExpr
24615549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
24625549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
24631237c673c07f9d827129ba02720108816abde562Ted Kremenek
24641237c673c07f9d827129ba02720108816abde562Ted Kremenek// TypesCompatibleExpr
24659ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_begin() {
24669ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
24679ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
24689ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek
24699ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator TypesCompatibleExpr::child_end() {
24709ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
24719ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
24721237c673c07f9d827129ba02720108816abde562Ted Kremenek
24731237c673c07f9d827129ba02720108816abde562Ted Kremenek// ChooseExpr
24745549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
24755549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
24762d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
24772d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor// GNUNullExpr
24782d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas GregorStmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
24792d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas GregorStmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
24801237c673c07f9d827129ba02720108816abde562Ted Kremenek
2481d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman// ShuffleVectorExpr
2482d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_begin() {
24835549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0];
2484d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman}
2485d38617c8a50f9729c254ab76cd359af797c6739bEli FriedmanStmt::child_iterator ShuffleVectorExpr::child_end() {
24865549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek  return &SubExprs[0]+NumExprs;
2487d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman}
2488d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman
24897c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson// VAArgExpr
24905549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_begin() { return &Val; }
24915549976193e34417d4474a5f4a514268ef6666c7Ted KremenekStmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
24927c50aca2fe36f6daa9bf1c8c428f30e72f96470aAnders Carlsson
249366b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson// InitListExpr
2494ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed KremenekStmt::child_iterator InitListExpr::child_begin() {
2495ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  return InitExprs.size() ? &InitExprs[0] : 0;
2496ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek}
2497ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed KremenekStmt::child_iterator InitListExpr::child_end() {
2498ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2499ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek}
250066b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson
25013498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor// DesignatedInitExpr
250205c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorStmt::child_iterator DesignatedInitExpr::child_begin() {
250305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
250405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
250505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
250605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
250705c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorStmt::child_iterator DesignatedInitExpr::child_end() {
250805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return child_iterator(&*child_begin() + NumSubExprs);
250905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
251005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
25113498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor// ImplicitValueInitExpr
25121eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt::child_iterator ImplicitValueInitExpr::child_begin() {
25131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return child_iterator();
25143498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor}
25153498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
25161eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt::child_iterator ImplicitValueInitExpr::child_end() {
25171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return child_iterator();
25183498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor}
25193498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
25202ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman// ParenListExpr
25212ef13e5abef0570a9f567b4671367275c05d4d34Nate BegemanStmt::child_iterator ParenListExpr::child_begin() {
25222ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman  return &Exprs[0];
25232ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman}
25242ef13e5abef0570a9f567b4671367275c05d4d34Nate BegemanStmt::child_iterator ParenListExpr::child_end() {
25252ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman  return &Exprs[0]+NumExprs;
25262ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman}
25272ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman
25281237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCStringLiteral
25291eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt::child_iterator ObjCStringLiteral::child_begin() {
2530c6c16af963eddc3e9b75b5d2614d069e1162fe27Chris Lattner  return &String;
25319ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
25329ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCStringLiteral::child_end() {
2533c6c16af963eddc3e9b75b5d2614d069e1162fe27Chris Lattner  return &String+1;
25349ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
25351237c673c07f9d827129ba02720108816abde562Ted Kremenek
25361237c673c07f9d827129ba02720108816abde562Ted Kremenek// ObjCEncodeExpr
25379ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
25389ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
25391237c673c07f9d827129ba02720108816abde562Ted Kremenek
2540b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian// ObjCSelectorExpr
25411eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt::child_iterator ObjCSelectorExpr::child_begin() {
25429ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
25439ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
25449ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCSelectorExpr::child_end() {
25459ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
25469ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
2547b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
2548390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian// ObjCProtocolExpr
25499ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_begin() {
25509ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
25519ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
25529ac5928abeb3a47592201e1c30fe2930c20a507eTed KremenekStmt::child_iterator ObjCProtocolExpr::child_end() {
25539ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek  return child_iterator();
25549ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
2555390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
2556563477da25f824e37c535131695dc4dc9b68c465Steve Naroff// ObjCMessageExpr
25571eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt::child_iterator ObjCMessageExpr::child_begin() {
255804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (getReceiverKind() == Instance)
255904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    return reinterpret_cast<Stmt **>(this + 1);
256004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return getArgs();
2561563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
2562563477da25f824e37c535131695dc4dc9b68c465Steve NaroffStmt::child_iterator ObjCMessageExpr::child_end() {
256304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return getArgs() + getNumArgs();
2564563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
2565563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
25664eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff// Blocks
256756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
256856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffStmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
25694eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
25709da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
25719da13f9ddb2567e36f4bbee7b3c32f54aeb76d5bTed KremenekStmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }
2572