Expr.cpp revision d62ca370b03b8c6ad58002d3399383baf744e32b
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"
2308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner#include "clang/Lex/LiteralSupport.h"
2408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner#include "clang/Lex/Lexer.h"
257a614d8380297fcd2bc23986241905d97222948cRichard Smith#include "clang/Sema/SemaDiagnostic.h"
261b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
2708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner#include "clang/Basic/SourceManager.h"
28da5a6b6d9fd52899499d5b7b46273ec844dcaa6eChris Lattner#include "clang/Basic/TargetInfo.h"
29cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor#include "llvm/Support/ErrorHandling.h"
303a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson#include "llvm/Support/raw_ostream.h"
31ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor#include <algorithm>
3264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman#include <cstring>
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
352b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// isKnownToHaveBooleanValue - Return true if this is an integer expression
362b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// that is known to return 0 or 1.  This happens for _Bool/bool expressions
372b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// but also int expressions which are produced by things like comparisons in
382b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// C.
392b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattnerbool Expr::isKnownToHaveBooleanValue() const {
40f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  const Expr *E = IgnoreParens();
41f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
422b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  // If this value has _Bool type, it is obvious 0/1.
43f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (E->getType()->isBooleanType()) return true;
44c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  // If this is a non-scalar-integer type, we don't care enough to try.
45f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (!E->getType()->isIntegralOrEnumerationType()) return false;
46c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
47f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
482b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    switch (UO->getOpcode()) {
492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Plus:
502b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return UO->getSubExpr()->isKnownToHaveBooleanValue();
512b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    default:
522b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return false;
532b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
542b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  }
55c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
566907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  // Only look through implicit casts.  If the user writes
576907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  // '(int) (a && b)' treat it as an arbitrary int.
58f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
592b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return CE->getSubExpr()->isKnownToHaveBooleanValue();
60c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
61f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
622b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    switch (BO->getOpcode()) {
632b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    default: return false;
642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:   // Relational operators.
652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:   // Equality operators.
692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LAnd: // AND operator.
712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LOr:  // Logical OR operator.
722b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return true;
73c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_And:  // Bitwise AND operator.
752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Xor:  // Bitwise XOR operator.
762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Or:   // Bitwise OR operator.
772b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      // Handle things like (x==2)|(y==12).
782b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return BO->getLHS()->isKnownToHaveBooleanValue() &&
792b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner             BO->getRHS()->isKnownToHaveBooleanValue();
80c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Comma:
822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Assign:
832b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return BO->getRHS()->isKnownToHaveBooleanValue();
842b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
852b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  }
86c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
87f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
882b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
892b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner           CO->getFalseExpr()->isKnownToHaveBooleanValue();
90c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
912b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  return false;
922b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner}
932b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner
9463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall// Amusing macro metaprogramming hack: check whether a class provides
9563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall// a more specific implementation of getExprLoc().
9663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallnamespace {
9763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// This implementation is used when a class provides a custom
9863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// implementation of getExprLoc.
9963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  template <class E, class T>
10063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  SourceLocation getExprLocImpl(const Expr *expr,
10163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                                SourceLocation (T::*v)() const) {
10263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return static_cast<const E*>(expr)->getExprLoc();
10363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
10463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
10563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// This implementation is used when a class doesn't provide
10663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// a custom implementation of getExprLoc.  Overload resolution
10763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// should pick it over the implementation above because it's
10863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// more specialized according to function template partial ordering.
10963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  template <class E>
11063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  SourceLocation getExprLocImpl(const Expr *expr,
11163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                                SourceLocation (Expr::*v)() const) {
11263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return static_cast<const E*>(expr)->getSourceRange().getBegin();
11363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
11463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall}
11563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
11663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallSourceLocation Expr::getExprLoc() const {
11763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  switch (getStmtClass()) {
11863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::NoStmtClass: llvm_unreachable("statement without class");
11963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(type)
12063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define STMT(type, base) \
12163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
12263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define EXPR(type, base) \
12363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
12463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#include "clang/AST/StmtNodes.inc"
12563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
12663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  llvm_unreachable("unknown statement kind");
12763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return SourceLocation();
12863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall}
12963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Expressions.
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
134561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor/// \brief Compute the type-, value-, and instantiation-dependence of a
135561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor/// declaration reference
136d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor/// based on the declaration being referenced.
137d967e31ee796efff24b84b704a063634f6b55627Douglas Gregorstatic void computeDeclRefDependence(NamedDecl *D, QualType T,
138d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor                                     bool &TypeDependent,
139561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                     bool &ValueDependent,
140561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                     bool &InstantiationDependent) {
141d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  TypeDependent = false;
142d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ValueDependent = false;
143561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  InstantiationDependent = false;
1440da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
1450da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  // (TD) C++ [temp.dep.expr]p3:
1460da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //   An id-expression is type-dependent if it contains:
1470da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //
148c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  // and
1490da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //
1500da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  // (VD) C++ [temp.dep.constexpr]p2:
1510da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  An identifier is value-dependent if it is:
152d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
1530da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - an identifier that was declared with dependent type
1540da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD)  - a name declared with a dependent type,
155d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (T->isDependentType()) {
156d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    TypeDependent = true;
157d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
158561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
159d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
160561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  } else if (T->isInstantiationDependentType()) {
161561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
1620da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
163d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
1640da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - a conversion-function-id that specifies a dependent type
165d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (D->getDeclName().getNameKind()
166561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                == DeclarationName::CXXConversionFunctionName) {
167561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    QualType T = D->getDeclName().getCXXNameType();
168561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (T->isDependentType()) {
169561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      TypeDependent = true;
170561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ValueDependent = true;
171561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
172561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      return;
173561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
174561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
175561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (T->isInstantiationDependentType())
176561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
1770da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
178561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
1790da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD)  - the name of a non-type template parameter,
180d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (isa<NonTypeTemplateParmDecl>(D)) {
181d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
182561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
183d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
184d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
185d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
1860da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD) - a constant with integral or enumeration type and is
1870da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //         initialized with an expression that is value-dependent.
188db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //  (VD) - a constant with literal type and is initialized with an
189db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //         expression that is value-dependent [C++11].
190db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //  (VD) - FIXME: Missing from the standard:
191db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //       -  an entity with reference type and is initialized with an
192db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //          expression that is value-dependent [C++11]
193d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
194db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith    if ((D->getASTContext().getLangOptions().CPlusPlus0x ?
195db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith           Var->getType()->isLiteralType() :
196db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith           Var->getType()->isIntegralOrEnumerationType()) &&
197db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith        (Var->getType().getCVRQualifiers() == Qualifiers::Const ||
198db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith         Var->getType()->isReferenceType())) {
19931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      if (const Expr *Init = Var->getAnyInitializer())
200561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        if (Init->isValueDependent()) {
201d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor          ValueDependent = true;
202561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          InstantiationDependent = true;
203561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        }
204db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith    }
205db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith
206bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    // (VD) - FIXME: Missing from the standard:
207bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    //      -  a member function or a static data member of the current
208bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    //         instantiation
209db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith    if (Var->isStaticDataMember() &&
210db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith        Var->getDeclContext()->isDependentContext()) {
211d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor      ValueDependent = true;
212561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
213561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
214d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
215d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
216d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
217d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
218bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  // (VD) - FIXME: Missing from the standard:
219bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  //      -  a member function or a static data member of the current
220bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  //         instantiation
221d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
222d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
223561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
224db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  }
225d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor}
226d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
227d967e31ee796efff24b84b704a063634f6b55627Douglas Gregorvoid DeclRefExpr::computeDependence() {
228d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool TypeDependent = false;
229d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool ValueDependent = false;
230561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  bool InstantiationDependent = false;
231561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
232561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                           InstantiationDependent);
233d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
234d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  // (TD) C++ [temp.dep.expr]p3:
235d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //   An id-expression is type-dependent if it contains:
236d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //
237d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  // and
238d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //
239d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  // (VD) C++ [temp.dep.constexpr]p2:
240d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //  An identifier is value-dependent if it is:
241d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (!TypeDependent && !ValueDependent &&
242d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor      hasExplicitTemplateArgs() &&
243d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor      TemplateSpecializationType::anyDependentTemplateArguments(
244d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor                                                            getTemplateArgs(),
245561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                                       getNumTemplateArgs(),
246561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                                      InstantiationDependent)) {
247d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    TypeDependent = true;
248d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
249561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
250d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
251d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
252d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.TypeDependent = TypeDependent;
253d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.ValueDependent = ValueDependent;
254561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  ExprBits.InstantiationDependent = InstantiationDependent;
255d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
25610738d36b150aa65206890c1c845cdba076e4200Douglas Gregor  // Is the declaration a parameter pack?
257d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (getDecl()->isParameterPack())
2581fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor    ExprBits.ContainsUnexpandedParameterPack = true;
2590da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor}
2600da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
2613aa8140bde5b9bedf13e46ec0a668daa54814196Chandler CarruthDeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
2622577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                         ValueDecl *D, const DeclarationNameInfo &NameInfo,
2633aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                         NamedDecl *FoundD,
2642577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                         const TemplateArgumentListInfo *TemplateArgs,
265f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                         QualType T, ExprValueKind VK)
266561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
267cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth    D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
268cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth  DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
2697e740bd36772aae16b5cc5e605998ccc5eaf26dbChandler Carruth  if (QualifierLoc)
2706857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    getInternalQualifierLoc() = QualifierLoc;
2713aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
2723aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (FoundD)
2733aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    getInternalFoundDecl() = FoundD;
274cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth  DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
275561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (TemplateArgs) {
276561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool Dependent = false;
277561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool InstantiationDependent = false;
278561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool ContainsUnexpandedParameterPack = false;
279561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
280561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                             InstantiationDependent,
281561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                             ContainsUnexpandedParameterPack);
282561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (InstantiationDependent)
283561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      setInstantiationDependent(true);
284561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
285b8da98a2182d2c69c72136b9a9ebabd4694f3d6bBenjamin Kramer  DeclRefExprBits.HadMultipleCandidates = 0;
286b8da98a2182d2c69c72136b9a9ebabd4694f3d6bBenjamin Kramer
2872577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  computeDependence();
2882577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
2892577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
290a2813cec2605ce7878d1b13471d685f689b251afDouglas GregorDeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
29140d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                                 NestedNameSpecifierLoc QualifierLoc,
292dbd872f273a8dbf22e089b3def6c09f0a460965dJohn McCall                                 ValueDecl *D,
293a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor                                 SourceLocation NameLoc,
2940da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                 QualType T,
295f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
2963aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                 NamedDecl *FoundD,
2970da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                 const TemplateArgumentListInfo *TemplateArgs) {
29840d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor  return Create(Context, QualifierLoc, D,
2992577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                DeclarationNameInfo(D->getDeclName(), NameLoc),
3003aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                T, VK, FoundD, TemplateArgs);
3012577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
3022577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
3032577743c5650c646fb705df01403707e94f2df04Abramo BagnaraDeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
30440d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                                 NestedNameSpecifierLoc QualifierLoc,
3052577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 ValueDecl *D,
3062577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 const DeclarationNameInfo &NameInfo,
3072577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 QualType T,
308f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
3093aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                 NamedDecl *FoundD,
3102577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 const TemplateArgumentListInfo *TemplateArgs) {
3113aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  // Filter out cases where the found Decl is the same as the value refenenced.
3123aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (D == FoundD)
3133aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    FoundD = 0;
3143aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
315a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  std::size_t Size = sizeof(DeclRefExpr);
31640d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor  if (QualifierLoc != 0)
3176857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    Size += sizeof(NestedNameSpecifierLoc);
3183aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (FoundD)
3193aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    Size += sizeof(NamedDecl *);
320d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (TemplateArgs)
321b0c3e0909bb04af0bfb82ad01ab6909649d68ccaArgyrios Kyrtzidis    Size += ASTTemplateArgumentListInfo::sizeFor(*TemplateArgs);
3223aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
3233248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
3243aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
3253aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                               T, VK);
326a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor}
327a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
3283aa8140bde5b9bedf13e46ec0a668daa54814196Chandler CarruthDeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
329def0354384d9c4431f7b58b664b59896d4623028Douglas Gregor                                      bool HasQualifier,
3303aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                      bool HasFoundDecl,
331def0354384d9c4431f7b58b664b59896d4623028Douglas Gregor                                      bool HasExplicitTemplateArgs,
332663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis                                      unsigned NumTemplateArgs) {
333663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  std::size_t Size = sizeof(DeclRefExpr);
334663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  if (HasQualifier)
3356857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    Size += sizeof(NestedNameSpecifierLoc);
3363aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (HasFoundDecl)
3373aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    Size += sizeof(NamedDecl *);
338def0354384d9c4431f7b58b664b59896d4623028Douglas Gregor  if (HasExplicitTemplateArgs)
339b0c3e0909bb04af0bfb82ad01ab6909649d68ccaArgyrios Kyrtzidis    Size += ASTTemplateArgumentListInfo::sizeFor(NumTemplateArgs);
3403aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
3413248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
342663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  return new (Mem) DeclRefExpr(EmptyShell());
343663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis}
344663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis
345a2813cec2605ce7878d1b13471d685f689b251afDouglas GregorSourceRange DeclRefExpr::getSourceRange() const {
3462577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  SourceRange R = getNameInfo().getSourceRange();
347a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  if (hasQualifier())
34840d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    R.setBegin(getQualifierLoc().getBeginLoc());
349096832c5ed5b9106fa177ebc148489760c3bc496John McCall  if (hasExplicitTemplateArgs())
350a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor    R.setEnd(getRAngleLoc());
351a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  return R;
352a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor}
353a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
3543a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson// FIXME: Maybe this should use DeclPrinter with a special "print predefined
3553a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson// expr" policy instead.
356848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlssonstd::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
357848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson  ASTContext &Context = CurrentDecl->getASTContext();
358848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson
3593a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
360848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson    if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
3613a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      return FD->getNameAsString();
3623a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3633a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::SmallString<256> Name;
3643a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::raw_svector_ostream Out(Name);
3653a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3663a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
367848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson      if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
3683a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        Out << "virtual ";
3694eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (MD->isStatic())
3704eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Out << "static ";
3713a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    }
3723a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3733a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    PrintingPolicy Policy(Context.getLangOptions());
3743a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3753a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    std::string Proto = FD->getQualifiedNameAsString(Policy);
3763a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
377183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
3783a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    const FunctionProtoType *FT = 0;
3793a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (FD->hasWrittenPrototype())
3803a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      FT = dyn_cast<FunctionProtoType>(AFT);
3813a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3823a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Proto += "(";
3833a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (FT) {
3843a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      llvm::raw_string_ostream POut(Proto);
3853a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
3863a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        if (i) POut << ", ";
3873a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        std::string Param;
3883a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
3893a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        POut << Param;
3903a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      }
3913a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3923a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      if (FT->isVariadic()) {
3933a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        if (FD->getNumParams()) POut << ", ";
3943a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        POut << "...";
3953a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      }
3963a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    }
3973a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Proto += ")";
3983a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3994eadcc569223135e13353c9381b448986e3f7053Sam Weinig    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
4004eadcc569223135e13353c9381b448986e3f7053Sam Weinig      Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
4014eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (ThisQuals.hasConst())
4024eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Proto += " const";
4034eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (ThisQuals.hasVolatile())
4044eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Proto += " volatile";
4054eadcc569223135e13353c9381b448986e3f7053Sam Weinig    }
4064eadcc569223135e13353c9381b448986e3f7053Sam Weinig
4073a1ce1ed0f5686384e712837bad28c576622e442Sam Weinig    if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
4083a1ce1ed0f5686384e712837bad28c576622e442Sam Weinig      AFT->getResultType().getAsStringInternal(Proto, Policy);
4093a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4103a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << Proto;
4113a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4123a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out.flush();
4133a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return Name.str().str();
4143a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
4153a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
4163a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::SmallString<256> Name;
4173a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::raw_svector_ostream Out(Name);
4183a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << (MD->isInstanceMethod() ? '-' : '+');
4193a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << '[';
420b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek
421b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    // For incorrect code, there might not be an ObjCInterfaceDecl.  Do
422b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    // a null check to avoid a crash.
423b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
424b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer      Out << *ID;
425b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek
4263a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (const ObjCCategoryImplDecl *CID =
427900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer        dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
428900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer      Out << '(' << CID << ')';
429900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer
4303a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out <<  ' ';
4313a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << MD->getSelector().getAsString();
4323a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out <<  ']';
4333a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4343a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out.flush();
4353a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return Name.str().str();
4363a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
4373a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
4383a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
4393a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return "top level";
4403a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
4413a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return "";
4423a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
4433a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4449996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidisvoid APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
4459996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  if (hasAllocation())
4469996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    C.Deallocate(pVal);
4479996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4489996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  BitWidth = Val.getBitWidth();
4499996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  unsigned NumWords = Val.getNumWords();
4509996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  const uint64_t* Words = Val.getRawData();
4519996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  if (NumWords > 1) {
4529996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    pVal = new (C) uint64_t[NumWords];
4539996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    std::copy(Words, Words + NumWords, pVal);
4549996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  } else if (NumWords == 1)
4559996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    VAL = Words[0];
4569996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  else
4579996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    VAL = 0;
4589996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4599996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4609996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral *
4619996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
4629996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                       QualType type, SourceLocation l) {
4639996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) IntegerLiteral(C, V, type, l);
4649996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4659996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4669996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral *
4679996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
4689996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) IntegerLiteral(Empty);
4699996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4709996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4719996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral *
4729996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
4739996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                        bool isexact, QualType Type, SourceLocation L) {
4749996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) FloatingLiteral(C, V, isexact, Type, L);
4759996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4769996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4779996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral *
4789996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
4799996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) FloatingLiteral(Empty);
4809996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4819996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
482da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// getValueAsApproximateDouble - This returns the value as an inaccurate
483da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// double.  Note that this may cause loss of precision, but is useful for
484da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// debugging dumps, etc.
485da8249e57f3badecf925571881fe57243935c6c1Chris Lattnerdouble FloatingLiteral::getValueAsApproximateDouble() const {
486da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  llvm::APFloat V = getValue();
487ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  bool ignored;
488ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
489ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen            &ignored);
490da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  return V.convertToDouble();
491da8249e57f3badecf925571881fe57243935c6c1Chris Lattner}
492da8249e57f3badecf925571881fe57243935c6c1Chris Lattner
49364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedmanint StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
49464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  int CharByteWidth;
49564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  switch(k) {
49664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case Ascii:
49764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case UTF8:
49864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      CharByteWidth = target.getCharWidth();
49964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
50064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case Wide:
50164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      CharByteWidth = target.getWCharWidth();
50264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
50364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case UTF16:
50464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      CharByteWidth = target.getChar16Width();
50564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
50664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case UTF32:
50764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      CharByteWidth = target.getChar32Width();
50864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  }
50964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
51064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  CharByteWidth /= 8;
51164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
51264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman         && "character byte widths supported are 1, 2, and 4 only");
51364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  return CharByteWidth;
51464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman}
51564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
5165f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
5175cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                     StringKind Kind, bool Pascal, QualType Ty,
5181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     const SourceLocation *Loc,
519a135fb43eb94524a6529768596a4533eed9aa70dAnders Carlsson                                     unsigned NumStrs) {
5202085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // Allocate enough space for the StringLiteral plus an array of locations for
5212085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // any concatenated string tokens.
5222085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  void *Mem = C.Allocate(sizeof(StringLiteral)+
5232085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                         sizeof(SourceLocation)*(NumStrs-1),
5243248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner                         llvm::alignOf<StringLiteral>());
5252085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  StringLiteral *SL = new (Mem) StringLiteral(Ty);
5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // OPTIMIZE: could allocate this appended to the StringLiteral.
52864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  SL->setString(C,Str,Kind,Pascal);
52964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
5302085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->TokLocs[0] = Loc[0];
5312085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->NumConcatenated = NumStrs;
5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
533726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner  if (NumStrs != 1)
5342085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner    memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
5352085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  return SL;
536726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner}
537726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner
538673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas GregorStringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
539673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  void *Mem = C.Allocate(sizeof(StringLiteral)+
540673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor                         sizeof(SourceLocation)*(NumStrs-1),
5413248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner                         llvm::alignOf<StringLiteral>());
542673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  StringLiteral *SL = new (Mem) StringLiteral(QualType());
54364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  SL->CharByteWidth = 0;
54464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  SL->Length = 0;
545673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  SL->NumConcatenated = NumStrs;
546673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  return SL;
547673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor}
548673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor
54964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedmanvoid StringLiteral::setString(ASTContext &C, StringRef Str,
55064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman                              StringKind Kind, bool IsPascal) {
55164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  //FIXME: we assume that the string data comes from a target that uses the same
55264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  // code unit size and endianess for the type of string.
55364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  this->Kind = Kind;
55464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  this->IsPascal = IsPascal;
55564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
55664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
55764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  assert((Str.size()%CharByteWidth == 0)
55864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman         && "size of data must be multiple of CharByteWidth");
55964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  Length = Str.size()/CharByteWidth;
56064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
56164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  switch(CharByteWidth) {
56264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case 1: {
56364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      char *AStrData = new (C) char[Length];
56464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      std::memcpy(AStrData,Str.data(),Str.size());
56564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      StrData.asChar = AStrData;
56664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
56764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    }
56864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case 2: {
56964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      uint16_t *AStrData = new (C) uint16_t[Length];
57064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      std::memcpy(AStrData,Str.data(),Str.size());
57164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      StrData.asUInt16 = AStrData;
57264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
57364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    }
57464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case 4: {
57564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      uint32_t *AStrData = new (C) uint32_t[Length];
57664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      std::memcpy(AStrData,Str.data(),Str.size());
57764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      StrData.asUInt32 = AStrData;
57864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
57964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    }
58064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    default:
58164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      assert(false && "unsupported CharByteWidth");
58264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  }
583673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor}
584673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor
58508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// getLocationOfByte - Return a source location that points to the specified
58608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// byte of this string literal.
58708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner///
58808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// Strings are amazingly complex.  They can be formed from multiple tokens and
58908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// can have escape sequences in them in addition to the usual trigraph and
59008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// escaped newline business.  This routine handles this complexity.
59108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner///
59208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris LattnerSourceLocation StringLiteral::
59308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris LattnergetLocationOfByte(unsigned ByteNo, const SourceManager &SM,
59408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner                  const LangOptions &Features, const TargetInfo &Target) const {
5955cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
5965cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
59708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  // Loop over all of the tokens in this string until we find the one that
59808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  // contains the byte we're looking for.
59908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  unsigned TokNo = 0;
60008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  while (1) {
60108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    assert(TokNo < getNumConcatenated() && "Invalid byte number!");
60208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
60308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
60408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Get the spelling of the string so that we can get the data that makes up
60508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // the string literal, not the identifier for the macro it is potentially
60608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // expanded through.
60708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
60808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
60908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Re-lex the token to get its length and original spelling.
61008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
61108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    bool Invalid = false;
6125f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
61308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    if (Invalid)
61408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      return StrTokSpellingLoc;
61508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
61608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    const char *StrData = Buffer.data()+LocInfo.second;
61708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
61808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Create a langops struct and enable trigraphs.  This is sufficient for
61908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // relexing tokens.
62008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    LangOptions LangOpts;
62108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    LangOpts.Trigraphs = true;
62208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
62308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Create a lexer starting at the beginning of this token.
62408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
62508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner                   Buffer.end());
62608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    Token TheTok;
62708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    TheLexer.LexFromRawLexer(TheTok);
62808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
62908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Use the StringLiteralParser to compute the length of the string in bytes.
63008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
63108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    unsigned TokNumBytes = SLP.GetStringLength();
63208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
63308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // If the byte is in this token, return the location of the byte.
63408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    if (ByteNo < TokNumBytes ||
635935a70c1e76d78985f20d422940280161b941299Hans Wennborg        (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
63608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
63708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
63808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      // Now that we know the offset of the token in the spelling, use the
63908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      // preprocessor to get the offset in the original source.
64008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
64108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    }
64208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
64308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Move to the next string token.
64408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    ++TokNo;
64508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    ByteNo -= TokNumBytes;
64608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  }
64708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner}
64808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
64908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
65008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
6515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
6525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "sizeof" or "[pre]++".
6535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *UnaryOperator::getOpcodeStr(Opcode Op) {
6545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
655b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Unknown unary operator");
6562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostInc: return "++";
6572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostDec: return "--";
6582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PreInc:  return "++";
6592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PreDec:  return "--";
6602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_AddrOf:  return "&";
6612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Deref:   return "*";
6622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:    return "+";
6632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus:   return "-";
6642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Not:     return "~";
6652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_LNot:    return "!";
6662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Real:    return "__real";
6672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Imag:    return "__imag";
6682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Extension: return "__extension__";
6695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCallUnaryOperatorKind
673bc736fceca6f0bca31d16003a7587857190408fbDouglas GregorUnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
674bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  switch (OO) {
675b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("No unary operator for overloaded function");
6762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PlusPlus:   return Postfix ? UO_PostInc : UO_PreInc;
6772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
6782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Amp:        return UO_AddrOf;
6792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Star:       return UO_Deref;
6802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Plus:       return UO_Plus;
6812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Minus:      return UO_Minus;
6822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Tilde:      return UO_Not;
6832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Exclaim:    return UO_LNot;
684bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  }
685bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor}
686bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
687bc736fceca6f0bca31d16003a7587857190408fbDouglas GregorOverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
688bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  switch (Opc) {
6892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
6902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
6912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_AddrOf: return OO_Amp;
6922de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Deref: return OO_Star;
6932de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus: return OO_Plus;
6942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus: return OO_Minus;
6952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Not: return OO_Tilde;
6962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_LNot: return OO_Exclaim;
697bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  default: return OO_None;
698bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  }
699bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor}
700bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
701bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
7025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Postfix Operators.
7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
7055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
706cc324ad80ab940efca006b0064f7ca70a6181816Peter CollingbourneCallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
707cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne                   Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
708f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                   SourceLocation rparenloc)
709f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(SC, t, VK, OK_Ordinary,
710bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isTypeDependent(),
711bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isValueDependent(),
712561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         fn->isInstantiationDependent(),
713bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->containsUnexpandedParameterPack()),
714898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    NumArgs(numargs) {
7151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
716cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
717b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  SubExprs[FN] = fn;
718bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  for (unsigned i = 0; i != numargs; ++i) {
719bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
720bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
721bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
722bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
723561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
724561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
725bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
726bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
727bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
728cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
729bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
730668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
731cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = NumPreArgs;
732b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  RParenLoc = rparenloc;
733b4609806e9232593ece09ce08b630836e825865cDouglas Gregor}
734e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman
735668bf91d31265b6ea8c3eb854ba450857701f269Ted KremenekCallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
736f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                   QualType t, ExprValueKind VK, SourceLocation rparenloc)
737f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(CallExprClass, t, VK, OK_Ordinary,
738bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isTypeDependent(),
739bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isValueDependent(),
740561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         fn->isInstantiationDependent(),
741bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->containsUnexpandedParameterPack()),
742898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    NumArgs(numargs) {
743668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
744cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[numargs+PREARGS_START];
74577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek  SubExprs[FN] = fn;
746bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  for (unsigned i = 0; i != numargs; ++i) {
747bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
748bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
749bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
750bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
751561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
752561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
753bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
754bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
755bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
756cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    SubExprs[i+PREARGS_START] = args[i];
757bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
758668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
759cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = 0;
7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  RParenLoc = rparenloc;
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
7641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
765bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  // FIXME: Why do we allocate this?
766cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[PREARGS_START];
767cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = 0;
768cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne}
769cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne
770cc324ad80ab940efca006b0064f7ca70a6181816Peter CollingbourneCallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
771cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne                   EmptyShell Empty)
772cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
773cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  // FIXME: Why do we allocate this?
774cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
775cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = NumPreArgs;
7761f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor}
7771f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor
778d20254f2875d0004c57ee766f258dbcee29f4841Nuno LopesDecl *CallExpr::getCalleeDecl() {
779e8683d6d389def2fce229325435ba3c2b3f75492John McCall  Expr *CEE = getCallee()->IgnoreParenImpCasts();
7801ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor
7811ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor  while (SubstNonTypeTemplateParmExpr *NTTP
7821ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor                                = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
7831ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor    CEE = NTTP->getReplacement()->IgnoreParenCasts();
7841ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor  }
7851ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor
786200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  // If we're calling a dereference, look at the pointer instead.
787200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
788200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl    if (BO->isPtrMemOp())
789200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl      CEE = BO->getRHS()->IgnoreParenCasts();
790200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
791200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl    if (UO->getOpcode() == UO_Deref)
792200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl      CEE = UO->getSubExpr()->IgnoreParenCasts();
793200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  }
7946346f963145ed18b6edf50a78753b47db505e912Chris Lattner  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
795d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    return DRE->getDecl();
796cb1c77f90d4e747b83a0d0cc125dc01567378f82Nuno Lopes  if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
797cb1c77f90d4e747b83a0d0cc125dc01567378f82Nuno Lopes    return ME->getMemberDecl();
798a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu
799a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu  return 0;
800a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu}
801a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu
802d20254f2875d0004c57ee766f258dbcee29f4841Nuno LopesFunctionDecl *CallExpr::getDirectCallee() {
803caabf9bf331156e96dacb072385901fdfa057ec1Chris Lattner  return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
804d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes}
805d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes
806d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// setNumArgs - This changes the number of arguments present in this call.
807d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// Any orphaned expressions are deleted by this, and any new operands are set
808d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// to null.
8098189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenekvoid CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
810d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // No change, just return.
811d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs == getNumArgs()) return;
8121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
813d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // If shrinking # arguments, just delete the extras and forgot them.
814d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs < getNumArgs()) {
815d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    this->NumArgs = NumArgs;
816d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    return;
817d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  }
818d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
819d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Otherwise, we are growing the # arguments.  New an bigger argument array.
820cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  unsigned NumPreArgs = getNumPreArgs();
821cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
822d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Copy over args.
823cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
824d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = SubExprs[i];
825d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Null out new args.
826cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
827cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne       i != NumArgs+PREARGS_START+NumPreArgs; ++i)
828d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = 0;
8291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83088c9a46f0b84f1ee83e01917825346551ee540d0Douglas Gregor  if (SubExprs) C.Deallocate(SubExprs);
831d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  SubExprs = NewSubExprs;
832d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  this->NumArgs = NumArgs;
833d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner}
834d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
835cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// isBuiltinCall - If this is a call to a builtin, return the builtin ID.  If
836cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// not, return 0.
837180f47959a066795cc0f409433023af448bb0328Richard Smithunsigned CallExpr::isBuiltinCall() const {
838c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // All simple function calls (e.g. func()) are implicitly cast to pointer to
8391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // function. As a result, we try and obtain the DeclRefExpr from the
840c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // ImplicitCastExpr.
841c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
842c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
843cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
8441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
845c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
846c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!DRE)
847cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
8481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
849bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
850bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  if (!FDecl)
851cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
8521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8534fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor  if (!FDecl->getIdentifier())
8544fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor    return 0;
8554fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor
8567814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  return FDecl->getBuiltinID();
857cb888967400a03504c88acedd5248d6778a82f46Chris Lattner}
858bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson
8596dde78f744382a5627a04f984a97049e0c4b5e73Anders CarlssonQualType CallExpr::getCallReturnType() const {
8606dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson  QualType CalleeType = getCallee()->getType();
8616217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
8626dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson    CalleeType = FnTypePtr->getPointeeType();
8636217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
8646dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson    CalleeType = BPT->getPointeeType();
865864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
866864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    // This should never be overloaded and so should never return null.
867864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    CalleeType = Expr::findBoundMemberType(getCallee());
8685291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
869864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  const FunctionType *FnType = CalleeType->castAs<FunctionType>();
8706dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson  return FnType->getResultType();
8716dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson}
872cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
8732882eca5a184c78f793188083f6ce539740a5cf2John McCallSourceRange CallExpr::getSourceRange() const {
8742882eca5a184c78f793188083f6ce539740a5cf2John McCall  if (isa<CXXOperatorCallExpr>(this))
8752882eca5a184c78f793188083f6ce539740a5cf2John McCall    return cast<CXXOperatorCallExpr>(this)->getSourceRange();
8762882eca5a184c78f793188083f6ce539740a5cf2John McCall
8772882eca5a184c78f793188083f6ce539740a5cf2John McCall  SourceLocation begin = getCallee()->getLocStart();
8782882eca5a184c78f793188083f6ce539740a5cf2John McCall  if (begin.isInvalid() && getNumArgs() > 0)
8792882eca5a184c78f793188083f6ce539740a5cf2John McCall    begin = getArg(0)->getLocStart();
8802882eca5a184c78f793188083f6ce539740a5cf2John McCall  SourceLocation end = getRParenLoc();
8812882eca5a184c78f793188083f6ce539740a5cf2John McCall  if (end.isInvalid() && getNumArgs() > 0)
8822882eca5a184c78f793188083f6ce539740a5cf2John McCall    end = getArg(getNumArgs() - 1)->getLocEnd();
8832882eca5a184c78f793188083f6ce539740a5cf2John McCall  return SourceRange(begin, end);
8842882eca5a184c78f793188083f6ce539740a5cf2John McCall}
8852882eca5a184c78f793188083f6ce539740a5cf2John McCall
886c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntOffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
8878ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   SourceLocation OperatorLoc,
888c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                   TypeSourceInfo *tsi,
889c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                   OffsetOfNode* compsPtr, unsigned numComps,
8908ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   Expr** exprsPtr, unsigned numExprs,
8918ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   SourceLocation RParenLoc) {
8928ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
893c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                         sizeof(OffsetOfNode) * numComps +
8948ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(Expr*) * numExprs);
8958ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
8968ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
8978ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                exprsPtr, numExprs, RParenLoc);
8988ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
8998ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
9008ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas GregorOffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
9018ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                        unsigned numComps, unsigned numExprs) {
9028ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
9038ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(OffsetOfNode) * numComps +
9048ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(Expr*) * numExprs);
9058ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return new (Mem) OffsetOfExpr(numComps, numExprs);
9068ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
9078ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
908c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntOffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
9098ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           SourceLocation OperatorLoc, TypeSourceInfo *tsi,
910c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                           OffsetOfNode* compsPtr, unsigned numComps,
9118ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           Expr** exprsPtr, unsigned numExprs,
9128ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           SourceLocation RParenLoc)
913f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
914f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall         /*TypeDependent=*/false,
915bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*ValueDependent=*/tsi->getType()->isDependentType(),
916561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         tsi->getType()->isInstantiationDependentType(),
917bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         tsi->getType()->containsUnexpandedParameterPack()),
918c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
919c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    NumComps(numComps), NumExprs(numExprs)
9208ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor{
9218ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  for(unsigned i = 0; i < numComps; ++i) {
9228ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    setComponent(i, compsPtr[i]);
9238ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
924c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
9258ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  for(unsigned i = 0; i < numExprs; ++i) {
926bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
927bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
928bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprsPtr[i]->containsUnexpandedParameterPack())
929bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
930bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
9318ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    setIndexExpr(i, exprsPtr[i]);
9328ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
9338ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
9348ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
9358ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas GregorIdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
9368ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  assert(getKind() == Field || getKind() == Identifier);
9378ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  if (getKind() == Field)
9388ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    return getField()->getIdentifier();
939c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
9408ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
9418ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
9428ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
9431eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpMemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
94440d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                               NestedNameSpecifierLoc QualifierLoc,
945f595cc41c4d95fe323f8a2b209523de9956f874dEli Friedman                               ValueDecl *memberdecl,
946161755a09898c95d21bfff33707da9ca41cd53c5John McCall                               DeclAccessPair founddecl,
9472577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                               DeclarationNameInfo nameinfo,
948d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                               const TemplateArgumentListInfo *targs,
949f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                               QualType ty,
950f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                               ExprValueKind vk,
951f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                               ExprObjectKind ok) {
95283f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  std::size_t Size = sizeof(MemberExpr);
9536bb8017bb9e828d118e15e59d71c66bba323c364John McCall
95440d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor  bool hasQualOrFound = (QualifierLoc ||
955161755a09898c95d21bfff33707da9ca41cd53c5John McCall                         founddecl.getDecl() != memberdecl ||
956161755a09898c95d21bfff33707da9ca41cd53c5John McCall                         founddecl.getAccess() != memberdecl->getAccess());
9576bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (hasQualOrFound)
9586bb8017bb9e828d118e15e59d71c66bba323c364John McCall    Size += sizeof(MemberNameQualifier);
9591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
960d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (targs)
961b0c3e0909bb04af0bfb82ad01ab6909649d68ccaArgyrios Kyrtzidis    Size += ASTTemplateArgumentListInfo::sizeFor(*targs);
9621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9633248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
964f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
965f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                       ty, vk, ok);
9666bb8017bb9e828d118e15e59d71c66bba323c364John McCall
9676bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (hasQualOrFound) {
96840d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    // FIXME: Wrong. We should be looking at the member declaration we found.
96940d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
9706bb8017bb9e828d118e15e59d71c66bba323c364John McCall      E->setValueDependent(true);
9716bb8017bb9e828d118e15e59d71c66bba323c364John McCall      E->setTypeDependent(true);
972561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
973561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
974561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    else if (QualifierLoc &&
975561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor             QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
976561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
977561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
9786bb8017bb9e828d118e15e59d71c66bba323c364John McCall    E->HasQualifierOrFoundDecl = true;
9796bb8017bb9e828d118e15e59d71c66bba323c364John McCall
9806bb8017bb9e828d118e15e59d71c66bba323c364John McCall    MemberNameQualifier *NQ = E->getMemberQualifier();
98140d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    NQ->QualifierLoc = QualifierLoc;
9826bb8017bb9e828d118e15e59d71c66bba323c364John McCall    NQ->FoundDecl = founddecl;
9836bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
9846bb8017bb9e828d118e15e59d71c66bba323c364John McCall
9856bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (targs) {
986561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool Dependent = false;
987561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool InstantiationDependent = false;
988561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool ContainsUnexpandedParameterPack = false;
9896bb8017bb9e828d118e15e59d71c66bba323c364John McCall    E->HasExplicitTemplateArgumentList = true;
990561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
991561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                                InstantiationDependent,
992561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                              ContainsUnexpandedParameterPack);
993561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (InstantiationDependent)
994561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
9956bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
9966bb8017bb9e828d118e15e59d71c66bba323c364John McCall
9976bb8017bb9e828d118e15e59d71c66bba323c364John McCall  return E;
99883f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor}
99983f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor
100075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas GregorSourceRange MemberExpr::getSourceRange() const {
100175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  SourceLocation StartLoc;
100275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  if (isImplicitAccess()) {
100375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (hasQualifier())
100475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      StartLoc = getQualifierLoc().getBeginLoc();
100575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    else
100675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      StartLoc = MemberLoc;
100775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  } else {
100875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    // FIXME: We don't want this to happen. Rather, we should be able to
100975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    // detect all kinds of implicit accesses more cleanly.
101075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    StartLoc = getBase()->getLocStart();
101175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (StartLoc.isInvalid())
101275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      StartLoc = MemberLoc;
101375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  }
101475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
101575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  SourceLocation EndLoc =
101675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    HasExplicitTemplateArgumentList? getRAngleLoc()
101775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor                                   : getMemberNameInfo().getEndLoc();
101875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
101975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  return SourceRange(StartLoc, EndLoc);
102075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor}
102175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
10221d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCallvoid CastExpr::CheckCastConsistency() const {
10231d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  switch (getCastKind()) {
10241d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_DerivedToBase:
10251d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_UncheckedDerivedToBase:
10261d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_DerivedToBaseMemberPointer:
10271d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BaseToDerived:
10281d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BaseToDerivedMemberPointer:
10291d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(!path_empty() && "Cast kind should have a base path!");
10301d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    break;
10311d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
10321d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
10331d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isObjCObjectPointerType());
10341d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isPointerType());
10351d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
10361d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
10371d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
10381d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isObjCObjectPointerType());
10391d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isBlockPointerType());
10401d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
10411d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
10421d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BitCast:
10431d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // Arbitrary casts to C pointer types count as bitcasts.
10441d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // Otherwise, we should only have block and ObjC pointer casts
10451d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // here if they stay within the type kind.
10461d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    if (!getType()->isPointerType()) {
10471d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      assert(getType()->isObjCObjectPointerType() ==
10481d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall             getSubExpr()->getType()->isObjCObjectPointerType());
10491d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      assert(getType()->isBlockPointerType() ==
10501d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall             getSubExpr()->getType()->isBlockPointerType());
10511d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    }
10521d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
10531d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
10541d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_AnyPointerToBlockPointerCast:
10551d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isBlockPointerType());
10561d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isAnyPointerType() &&
10571d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall           !getSubExpr()->getType()->isBlockPointerType());
10581d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
10591d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
10601d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  // These should not have an inheritance path.
10611d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_Dynamic:
10621d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ToUnion:
10631d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ArrayToPointerDecay:
10641d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FunctionToPointerDecay:
10651d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NullToMemberPointer:
10661d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NullToPointer:
10671d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ConstructorConversion:
10681d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToPointer:
10691d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_PointerToIntegral:
10701d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ToVoid:
10711d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_VectorSplat:
10721d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralCast:
10731d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToFloating:
10741d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingToIntegral:
10751d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingCast:
10761d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ObjCObjectLValueCast:
10771d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingRealToComplex:
10781d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToReal:
10791d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexCast:
10801d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToIntegralComplex:
10811d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralRealToComplex:
10821d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToReal:
10831d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexCast:
10841d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToFloatingComplex:
108533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
108633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
108733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
108833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
10891d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(!getType()->isBooleanType() && "unheralded conversion to bool");
10901d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
10911d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
10921d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_Dependent:
10931d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_LValueToRValue:
10941d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NoOp:
10951d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_PointerToBoolean:
10961d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToBoolean:
10971d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingToBoolean:
10981d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_MemberPointerToBoolean:
10991d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToBoolean:
11001d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToBoolean:
11011d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_LValueBitCast:            // -> bool&
11021d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_UserDefinedConversion:    // operator bool()
11031d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  CheckNoBasePath:
11041d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(path_empty() && "Cast kind should not have a base path!");
11051d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    break;
11061d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  }
11071d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall}
11081d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
1109f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlssonconst char *CastExpr::getCastKindName() const {
1110f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  switch (getCastKind()) {
1111daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_Dependent:
1112daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "Dependent";
11132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BitCast:
1114f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "BitCast";
11152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_LValueBitCast:
1116e39a3894513349908cdb3beba2614e53cb288e6cDouglas Gregor    return "LValueBitCast";
11170ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_LValueToRValue:
11180ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall    return "LValueToRValue";
11192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NoOp:
1120f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "NoOp";
11212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerived:
112211de6de25a0110cd7be97eef761ef3b189781da6Anders Carlsson    return "BaseToDerived";
11232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBase:
1124f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "DerivedToBase";
11252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UncheckedDerivedToBase:
112623cba801e11b03929c44f8cf54578305963a3476John McCall    return "UncheckedDerivedToBase";
11272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_Dynamic:
1128f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "Dynamic";
11292de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToUnion:
1130f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ToUnion";
11312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ArrayToPointerDecay:
1132f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ArrayToPointerDecay";
11332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FunctionToPointerDecay:
1134f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "FunctionToPointerDecay";
11352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NullToMemberPointer:
1136f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "NullToMemberPointer";
1137404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall  case CK_NullToPointer:
1138404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall    return "NullToPointer";
11392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerivedMemberPointer:
1140f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "BaseToDerivedMemberPointer";
11412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBaseMemberPointer:
11421a31a18db9d657751f38c724adc0d62e86852bd7Anders Carlsson    return "DerivedToBaseMemberPointer";
11432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UserDefinedConversion:
1144f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "UserDefinedConversion";
11452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ConstructorConversion:
1146f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ConstructorConversion";
11472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToPointer:
11487f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson    return "IntegralToPointer";
11492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_PointerToIntegral:
11507f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson    return "PointerToIntegral";
1151daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_PointerToBoolean:
1152daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "PointerToBoolean";
11532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToVoid:
1154ebeaf2031c968143c531bfe232d7507f20c57347Anders Carlsson    return "ToVoid";
11552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_VectorSplat:
115616a8904f3f5ed19158657e1da95e5902fbee66f7Anders Carlsson    return "VectorSplat";
11572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralCast:
115882debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "IntegralCast";
1159daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_IntegralToBoolean:
1160daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "IntegralToBoolean";
11612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToFloating:
116282debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "IntegralToFloating";
11632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FloatingToIntegral:
116482debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "FloatingToIntegral";
11652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FloatingCast:
1166c6b29163557d02da5d2a4a06f986f0480291f51fBenjamin Kramer    return "FloatingCast";
1167daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_FloatingToBoolean:
1168daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "FloatingToBoolean";
11692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_MemberPointerToBoolean:
1170bc0e0781da778bd5eb41a810419912893ae20448Anders Carlsson    return "MemberPointerToBoolean";
11711d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
11721d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return "CPointerToObjCPointerCast";
11731d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
11741d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return "BlockPointerToObjCPointerCast";
11752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_AnyPointerToBlockPointerCast:
11763b27f1a80e4e433b503efd344c909eeafaa9033cFariborz Jahanian    return "AnyPointerToBlockPointerCast";
11772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ObjCObjectLValueCast:
1178569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    return "ObjCObjectLValueCast";
11792bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_FloatingRealToComplex:
11802bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "FloatingRealToComplex";
1181f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToReal:
1182f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToReal";
1183f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToBoolean:
1184f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToBoolean";
11852bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_FloatingComplexCast:
11862bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "FloatingComplexCast";
1187f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToIntegralComplex:
1188f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToIntegralComplex";
11892bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_IntegralRealToComplex:
11902bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "IntegralRealToComplex";
1191f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToReal:
1192f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToReal";
1193f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToBoolean:
1194f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToBoolean";
11952bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_IntegralComplexCast:
11962bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "IntegralComplexCast";
1197f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToFloatingComplex:
1198f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToFloatingComplex";
119933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
120033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCConsumeObject";
120133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
120233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCProduceObject";
120333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
120433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCReclaimReturnedObject";
120533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
120633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCCExtendBlockObject";
1207f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  }
12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12092bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  llvm_unreachable("Unhandled cast kind!");
1210f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  return 0;
1211f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson}
1212f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson
12136eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas GregorExpr *CastExpr::getSubExprAsWritten() {
12146eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  Expr *SubExpr = 0;
12156eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  CastExpr *E = this;
12166eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  do {
12176eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    SubExpr = E->getSubExpr();
121803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
121903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    // Skip through reference binding to temporary.
122003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
122103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                  = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
122203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      SubExpr = Materialize->GetTemporaryExpr();
122303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
12246eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Skip any temporary bindings; they're implicit.
12256eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
12266eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = Binder->getSubExpr();
1227c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
12286eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Conversions by constructor and conversion functions have a
12296eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // subexpression describing the call; strip it off.
12302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (E->getCastKind() == CK_ConstructorConversion)
12316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
12322de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    else if (E->getCastKind() == CK_UserDefinedConversion)
12336eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
1234c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
12356eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // If the subexpression we're left with is an implicit cast, look
12366eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // through that, too.
1237c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1238c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
12396eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return SubExpr;
12406eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
12416eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1242f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCXXBaseSpecifier **CastExpr::path_buffer() {
1243f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  switch (getStmtClass()) {
1244f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define ABSTRACT_STMT(x)
1245f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define CASTEXPR(Type, Base) \
1246f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  case Stmt::Type##Class: \
1247f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1248f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define STMT(Type, Base)
1249f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#include "clang/AST/StmtNodes.inc"
1250f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  default:
1251f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    llvm_unreachable("non-cast expressions not possible here");
1252f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
1253f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  }
1254f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1255f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1256f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallvoid CastExpr::setCastPath(const CXXCastPath &Path) {
1257f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  assert(Path.size() == path_size());
1258f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1259f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1260f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1261f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1262f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                           CastKind Kind, Expr *Operand,
1263f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                           const CXXCastPath *BasePath,
12645baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                           ExprValueKind VK) {
1265f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  unsigned PathSize = (BasePath ? BasePath->size() : 0);
1266f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1267f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1268f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  ImplicitCastExpr *E =
12695baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
1270f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (PathSize) E->setCastPath(*BasePath);
1271f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return E;
1272f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1273f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1274f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1275f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                                unsigned PathSize) {
1276f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1277f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1278f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1279f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1280f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1281f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1282f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
1283f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                       ExprValueKind VK, CastKind K, Expr *Op,
1284f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       const CXXCastPath *BasePath,
1285f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       TypeSourceInfo *WrittenTy,
1286f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       SourceLocation L, SourceLocation R) {
1287f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  unsigned PathSize = (BasePath ? BasePath->size() : 0);
1288f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1289f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1290f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CStyleCastExpr *E =
1291f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
1292f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (PathSize) E->setCastPath(*BasePath);
1293f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return E;
1294f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1295f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1296f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1297f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1298f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1299f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1300f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1301f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
13025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
13035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "<<=".
13045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *BinaryOperator::getOpcodeStr(Opcode Op) {
13055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
13062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_PtrMemD:   return ".*";
13072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_PtrMemI:   return "->*";
13082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:       return "*";
13092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Div:       return "/";
13102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Rem:       return "%";
13112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:       return "+";
13122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:       return "-";
13132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shl:       return "<<";
13142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shr:       return ">>";
13152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LT:        return "<";
13162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_GT:        return ">";
13172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LE:        return "<=";
13182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_GE:        return ">=";
13192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_EQ:        return "==";
13202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_NE:        return "!=";
13212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_And:       return "&";
13222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Xor:       return "^";
13232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Or:        return "|";
13242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LAnd:      return "&&";
13252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LOr:       return "||";
13262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Assign:    return "=";
13272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_MulAssign: return "*=";
13282de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_DivAssign: return "/=";
13292de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_RemAssign: return "%=";
13302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_AddAssign: return "+=";
13312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_SubAssign: return "-=";
13322de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_ShlAssign: return "<<=";
13332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_ShrAssign: return ">>=";
13342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_AndAssign: return "&=";
13352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_XorAssign: return "^=";
13362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_OrAssign:  return "|=";
13372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Comma:     return ",";
13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1339baf534875ed0a55c6342636ff3f4602b8ac22b69Douglas Gregor
1340baf534875ed0a55c6342636ff3f4602b8ac22b69Douglas Gregor  return "";
13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCallBinaryOperatorKind
1344063daf6e196c51f162e0485478355d8e280eef5cDouglas GregorBinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1345063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  switch (OO) {
1346b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not an overloadable binary operator");
13472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Plus: return BO_Add;
13482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Minus: return BO_Sub;
13492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Star: return BO_Mul;
13502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Slash: return BO_Div;
13512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Percent: return BO_Rem;
13522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Caret: return BO_Xor;
13532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Amp: return BO_And;
13542de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Pipe: return BO_Or;
13552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Equal: return BO_Assign;
13562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Less: return BO_LT;
13572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Greater: return BO_GT;
13582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PlusEqual: return BO_AddAssign;
13592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_MinusEqual: return BO_SubAssign;
13602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_StarEqual: return BO_MulAssign;
13612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_SlashEqual: return BO_DivAssign;
13622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PercentEqual: return BO_RemAssign;
13632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_CaretEqual: return BO_XorAssign;
13642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_AmpEqual: return BO_AndAssign;
13652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PipeEqual: return BO_OrAssign;
13662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessLess: return BO_Shl;
13672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterGreater: return BO_Shr;
13682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessLessEqual: return BO_ShlAssign;
13692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterGreaterEqual: return BO_ShrAssign;
13702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_EqualEqual: return BO_EQ;
13712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_ExclaimEqual: return BO_NE;
13722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessEqual: return BO_LE;
13732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterEqual: return BO_GE;
13742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_AmpAmp: return BO_LAnd;
13752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PipePipe: return BO_LOr;
13762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Comma: return BO_Comma;
13772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_ArrowStar: return BO_PtrMemI;
1378063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  }
1379063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor}
1380063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor
1381063daf6e196c51f162e0485478355d8e280eef5cDouglas GregorOverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1382063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  static const OverloadedOperatorKind OverOps[] = {
1383063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1384063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Star, OO_Slash, OO_Percent,
1385063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Plus, OO_Minus,
1386063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_LessLess, OO_GreaterGreater,
1387063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1388063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_EqualEqual, OO_ExclaimEqual,
1389063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Amp,
1390063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Caret,
1391063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Pipe,
1392063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_AmpAmp,
1393063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PipePipe,
1394063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Equal, OO_StarEqual,
1395063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_SlashEqual, OO_PercentEqual,
1396063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PlusEqual, OO_MinusEqual,
1397063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_LessLessEqual, OO_GreaterGreaterEqual,
1398063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_AmpEqual, OO_CaretEqual,
1399063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PipeEqual,
1400063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Comma
1401063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  };
1402063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  return OverOps[Opc];
1403063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor}
1404063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor
1405709210feee317b8d6690dd1d15c2b74cfe55e261Ted KremenekInitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
1406418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner                           Expr **initExprs, unsigned numInits,
14074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                           SourceLocation rbraceloc)
1408bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1409561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         false, false),
1410709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs(C, numInits),
14111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
14124423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis    HadArrayRangeDesignator(false)
1413c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt{
1414ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  for (unsigned I = 0; I != numInits; ++I) {
1415ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    if (initExprs[I]->isTypeDependent())
14168e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall      ExprBits.TypeDependent = true;
1417ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    if (initExprs[I]->isValueDependent())
14188e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall      ExprBits.ValueDependent = true;
1419561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (initExprs[I]->isInstantiationDependent())
1420561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
1421bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (initExprs[I]->containsUnexpandedParameterPack())
1422bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
142373460a32bc5299a5927d23d2e464d72af796eabfDouglas Gregor  }
1424c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1425709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek  InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
142666b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1428709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenekvoid InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
1429ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  if (NumInits > InitExprs.size())
1430709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs.reserve(C, NumInits);
1431fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor}
1432fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
1433709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenekvoid InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
1434709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek  InitExprs.resize(C, NumInits, 0);
14354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
14364c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1437709210feee317b8d6690dd1d15c2b74cfe55e261Ted KremenekExpr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
1438ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  if (Init >= InitExprs.size()) {
1439709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
1440ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    InitExprs.back() = expr;
1441ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    return 0;
14424c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
14431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14444c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
14454c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitExprs[Init] = expr;
14464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
14474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
14484c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
14493e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidisvoid InitListExpr::setArrayFiller(Expr *filler) {
145021f77cd0c3da8a1dbaf6245cae43baf4c0b80ea4Argyrios Kyrtzidis  assert(!hasArrayFiller() && "Filler already set!");
14513e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  ArrayFillerOrUnionFieldInit = filler;
14523e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  // Fill out any "holes" in the array due to designated initializers.
14533e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  Expr **inits = getInits();
14543e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  for (unsigned i = 0, e = getNumInits(); i != e; ++i)
14553e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis    if (inits[i] == 0)
14563e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis      inits[i] = filler;
14573e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis}
14583e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis
1459c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted KremenekSourceRange InitListExpr::getSourceRange() const {
1460c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (SyntacticForm)
1461c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    return SyntacticForm->getSourceRange();
1462c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1463c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (Beg.isInvalid()) {
1464c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    // Find the first non-null initializer.
1465c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    for (InitExprsTy::const_iterator I = InitExprs.begin(),
1466c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek                                     E = InitExprs.end();
1467c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      I != E; ++I) {
1468c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      if (Stmt *S = *I) {
1469c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        Beg = S->getLocStart();
1470c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        break;
1471c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      }
1472c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    }
1473c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  }
1474c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (End.isInvalid()) {
1475c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    // Find the first non-null initializer from the end.
1476c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1477c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek                                             E = InitExprs.rend();
1478c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      I != E; ++I) {
1479c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      if (Stmt *S = *I) {
1480c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        End = S->getSourceRange().getEnd();
1481c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        break;
1482c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      }
1483c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    }
1484c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  }
1485c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  return SourceRange(Beg, End);
1486c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek}
1487c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek
1488bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block.
14894eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
14904eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroffconst FunctionType *BlockExpr::getFunctionType() const {
14916217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  return getType()->getAs<BlockPointerType>()->
1492183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                    getPointeeType()->getAs<FunctionType>();
14934eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
14944eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
14951eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSourceLocation BlockExpr::getCaretLocation() const {
14961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getCaretLocation();
149756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
14981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst Stmt *BlockExpr::getBody() const {
14997297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor  return TheBlock->getBody();
15007297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
15011eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt *BlockExpr::getBody() {
15021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getBody();
15037297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
150456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
150556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
15065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines
15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1510026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// isUnusedResultAWarning - Return true if this immediate expression should
1511026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// be warned about if the result is unused.  If so, fill in Loc and Ranges
1512026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// with location to warn on and the source range[s] to report with the
1513026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// warning.
1514026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattnerbool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
1515df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump                                  SourceRange &R2, ASTContext &Ctx) const {
1516ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // Don't warn if the expr is type dependent. The type could end up
1517ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // instantiating to void.
1518ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  if (isTypeDependent())
1519ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson    return false;
15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
15230faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
15240faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
1525026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = getExprLoc();
1526026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
1527026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
15285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass:
1529026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return cast<ParenExpr>(this)->getSubExpr()->
1530df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      isUnusedResultAWarning(Loc, R1, R2, Ctx);
1531f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
1532f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()->
1533f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      isUnusedResultAWarning(Loc, R1, R2, Ctx);
15345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UnaryOperatorClass: {
15355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const UnaryOperator *UO = cast<UnaryOperator>(this);
15361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (UO->getOpcode()) {
1538026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    default: break;
15392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostInc:
15402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostDec:
15412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreInc:
15422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreDec:                 // ++/--
1543026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;  // Not a warning.
15442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Deref:
15455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Dereferencing a volatile pointer is a side-effect.
1546df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1547026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
1548026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
15492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Real:
15502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Imag:
15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // accessing a piece of a volatile complex is a side-effect.
1552df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1553df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump          .isVolatileQualified())
1554026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
1555026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
15562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
1557df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
15585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1559026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = UO->getOperatorLoc();
1560026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = UO->getSubExpr()->getSourceRange();
1561026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
15625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1563e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  case BinaryOperatorClass: {
1564026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const BinaryOperator *BO = cast<BinaryOperator>(this);
1565c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek    switch (BO->getOpcode()) {
1566c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek      default:
1567c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        break;
156825973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider the RHS of comma for side effects. LHS was checked by
156925973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Sema::CheckCommaOperands.
15702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_Comma:
1571c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // ((foo = <blah>), 0) is an idiom for hiding the result (and
1572c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // lvalue-ness) of an assignment written in a macro.
1573c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        if (IntegerLiteral *IE =
1574c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek              dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1575c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek          if (IE->getValue() == 0)
1576c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek            return false;
157725973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
157825973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider '||', '&&' to have side effects if the LHS or RHS does.
15792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LAnd:
15802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LOr:
158125973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
158225973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis            !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
158325973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis          return false;
158425973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        break;
1585bf0ee354163f87623a4b60412544243911332343John McCall    }
1586026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (BO->isAssignmentOp())
1587026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1588026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = BO->getOperatorLoc();
1589026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = BO->getLHS()->getSourceRange();
1590026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = BO->getRHS()->getSourceRange();
1591026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1592e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  }
1593eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner  case CompoundAssignOperatorClass:
1594c6dfe194f623b02c123759f235b504d4850fc178Douglas Gregor  case VAArgExprClass:
1595276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  case AtomicExprClass:
1596026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
15975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1598ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  case ConditionalOperatorClass: {
1599fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // If only one of the LHS or RHS is a warning, the operator might
1600fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // be being used for control flow. Only warn if both the LHS and
1601fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // RHS are warnings.
1602ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1603fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1604fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek      return false;
1605fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    if (!Exp->getLHS())
1606026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return true;
1607fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1608ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  }
1609ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian
16105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case MemberExprClass:
1611026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If the base pointer or element is to a volatile pointer/field, accessing
1612026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // it is a side effect.
1613df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1614026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1615026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<MemberExpr>(this)->getMemberLoc();
1616026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = SourceRange(Loc, Loc);
1617026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1618026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
16191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ArraySubscriptExprClass:
16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If the base pointer or element is to a volatile pointer/field, accessing
1622026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // it is a side effect.
1623df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1624026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1625026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1626026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1627026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1628026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1629211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman
16309b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth  case CXXOperatorCallExprClass: {
16319b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // We warn about operator== and operator!= even when user-defined operator
16329b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // overloads as there is no reasonable way to define these such that they
16339b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // have non-trivial, desirable side-effects. See the -Wunused-comparison
16349b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // warning: these operators are commonly typo'ed, and so warning on them
16359b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // provides additional value as well. If this list is updated,
16369b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // DiagnoseUnusedComparison should be as well.
16379b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
16389b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    if (Op->getOperator() == OO_EqualEqual ||
16396e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay        Op->getOperator() == OO_ExclaimEqual) {
16406e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay      Loc = Op->getOperatorLoc();
16416e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay      R1 = Op->getSourceRange();
16429b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth      return true;
16436e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay    }
16449b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth
16459b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // Fallthrough for generic call handling.
16469b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth  }
16475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case CallExprClass:
1648852871abbff45f1c1d3787755a27fce08365b166Eli Friedman  case CXXMemberCallExprClass: {
1649026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If this is a direct call, get the callee.
1650026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const CallExpr *CE = cast<CallExpr>(this);
1651d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    if (const Decl *FD = CE->getCalleeDecl()) {
1652026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // If the callee has attribute pure, const, or warn_unused_result, warn
1653026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // about it. void foo() { strlen("bar"); } should warn.
1654bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      //
1655bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1656bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // updated to match for QoI.
1657bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<WarnUnusedResultAttr>() ||
1658bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1659bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Loc = CE->getCallee()->getLocStart();
1660bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        R1 = CE->getCallee()->getSourceRange();
1661bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner
1662bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        if (unsigned NumArgs = CE->getNumArgs())
1663bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          R2 = SourceRange(CE->getArg(0)->getLocStart(),
1664bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner                           CE->getArg(NumArgs-1)->getLocEnd());
1665bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return true;
1666bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
1667026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    }
1668026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
1669026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner  }
167058beed91d468863b8c85bce43425422703838d27Anders Carlsson
167158beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXTemporaryObjectExprClass:
167258beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXConstructExprClass:
167358beed91d468863b8c85bce43425422703838d27Anders Carlsson    return false;
167458beed91d468863b8c85bce43425422703838d27Anders Carlsson
1675f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  case ObjCMessageExprClass: {
1676f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1677f85e193739c953358c865005855253af4f68a497John McCall    if (Ctx.getLangOptions().ObjCAutoRefCount &&
1678f85e193739c953358c865005855253af4f68a497John McCall        ME->isInstanceMessage() &&
1679f85e193739c953358c865005855253af4f68a497John McCall        !ME->getType()->isVoidType() &&
1680f85e193739c953358c865005855253af4f68a497John McCall        ME->getSelector().getIdentifierInfoForSlot(0) &&
1681f85e193739c953358c865005855253af4f68a497John McCall        ME->getSelector().getIdentifierInfoForSlot(0)
1682f85e193739c953358c865005855253af4f68a497John McCall                                               ->getName().startswith("init")) {
1683f85e193739c953358c865005855253af4f68a497John McCall      Loc = getExprLoc();
1684f85e193739c953358c865005855253af4f68a497John McCall      R1 = ME->getSourceRange();
1685f85e193739c953358c865005855253af4f68a497John McCall      return true;
1686f85e193739c953358c865005855253af4f68a497John McCall    }
1687f85e193739c953358c865005855253af4f68a497John McCall
1688f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMethodDecl *MD = ME->getMethodDecl();
1689f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1690f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      Loc = getExprLoc();
1691f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      return true;
1692f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    }
1693026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
1694f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  }
16951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
169612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  case ObjCPropertyRefExprClass:
16975e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    Loc = getExprLoc();
16985e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    R1 = getSourceRange();
1699a50089ec68a583d13718107c1b0c898f0903709eChris Lattner    return true;
170012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
17014b9c2d235fb9449e249d74f48ecfec601650de93John McCall  case PseudoObjectExprClass: {
17024b9c2d235fb9449e249d74f48ecfec601650de93John McCall    const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
17034b9c2d235fb9449e249d74f48ecfec601650de93John McCall
17044b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // Only complain about things that have the form of a getter.
17054b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
17064b9c2d235fb9449e249d74f48ecfec601650de93John McCall        isa<BinaryOperator>(PO->getSyntacticForm()))
17074b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return false;
17084b9c2d235fb9449e249d74f48ecfec601650de93John McCall
17094b9c2d235fb9449e249d74f48ecfec601650de93John McCall    Loc = getExprLoc();
17104b9c2d235fb9449e249d74f48ecfec601650de93John McCall    R1 = getSourceRange();
17114b9c2d235fb9449e249d74f48ecfec601650de93John McCall    return true;
17124b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
17134b9c2d235fb9449e249d74f48ecfec601650de93John McCall
1714611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  case StmtExprClass: {
1715611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // Statement exprs don't logically have side effects themselves, but are
1716611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // sometimes used in macros in ways that give them a type that is unused.
1717611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1718611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // however, if the result of the stmt expr is dead, we don't want to emit a
1719611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // warning.
1720611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1721d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    if (!CS->body_empty()) {
1722611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner      if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
1723df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump        return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1724d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis      if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1725d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis        if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1726d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis          return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1727d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    }
17281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17290faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
17300faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
1731026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<StmtExpr>(this)->getLParenLoc();
1732026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
1733026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1734611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  }
17356eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor  case CStyleCastExprClass:
1736fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner    // If this is an explicit cast to void, allow it.  People do this when they
1737fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner    // think they know what they're doing :).
1738026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (getType()->isVoidType())
1739fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner      return false;
1740026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1741026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1742026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
174358beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXFunctionalCastExprClass: {
17440faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
17450faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
174658beed91d468863b8c85bce43425422703838d27Anders Carlsson    const CastExpr *CE = cast<CastExpr>(this);
1747c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
174858beed91d468863b8c85bce43425422703838d27Anders Carlsson    // If this is a cast to void or a constructor conversion, check the operand.
174958beed91d468863b8c85bce43425422703838d27Anders Carlsson    // Otherwise, the result of the cast is unused.
17502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CE->getCastKind() == CK_ToVoid ||
17512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        CE->getCastKind() == CK_ConstructorConversion)
1752df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      return (cast<CastExpr>(this)->getSubExpr()
1753df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump              ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
1754026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1755026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1756026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
175758beed91d468863b8c85bce43425422703838d27Anders Carlsson  }
17581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17594be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman  case ImplicitCastExprClass:
17604be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman    // Check the operand, since implicit casts are inserted by Sema
1761df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<ImplicitCastExpr>(this)
1762df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
17634be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman
176404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
1765df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXDefaultArgExpr>(this)
1766df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
17674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXNewExprClass:
17694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // FIXME: In theory, there might be new expressions that don't have side
17704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // effects (e.g. a placement new with an uninitialized POD).
17714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXDeleteExprClass:
1772026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
17732d46eb21eb2c904831b0e9f75ab3523384c70e66Anders Carlsson  case CXXBindTemporaryExprClass:
1774df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXBindTemporaryExpr>(this)
1775df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
17764765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case ExprWithCleanupsClass:
17774765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall    return (cast<ExprWithCleanups>(this)
1778df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
17794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
17805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
178244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian/// isOBJCGCCandidate - Check if an expression is objc gc'able.
17837f4f86a2167abc116275e49c81350fc3225485e5Fariborz Jahanian/// returns true, if it is; false otherwise.
1784102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanianbool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
1785f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  const Expr *E = IgnoreParens();
1786f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  switch (E->getStmtClass()) {
178744baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  default:
178844baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
178944baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ObjCIvarRefExprClass:
179044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return true;
1791207c5210eb0ac7b632609f0c006eb97ef2738948Fariborz Jahanian  case Expr::UnaryOperatorClass:
1792f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
179344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ImplicitCastExprClass:
1794f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
179503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass:
179603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
179703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                                      ->isOBJCGCCandidate(Ctx);
179806b89124a9a5971a0528cc9da6817740bac43164Fariborz Jahanian  case CStyleCastExprClass:
1799f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
1800e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian  case BlockDeclRefExprClass:
1801a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  case DeclRefExprClass: {
1802e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian
1803e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian    const Decl *D;
1804e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian    if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1805e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian        D = BDRE->getDecl();
1806e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian    else
1807e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian        D = cast<DeclRefExpr>(E)->getDecl();
1808e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian
1809102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1810102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      if (VD->hasGlobalStorage())
1811102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian        return true;
1812102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      QualType T = VD->getType();
181359a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // dereferencing to a  pointer is always a gc'able candidate,
181459a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // unless it is __weak.
18157e88a60d38b36695520e4f8d9279766ef111a662Daniel Dunbar      return T->isPointerType() &&
18160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall             (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
1817102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    }
181844baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
181944baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
182083f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  case MemberExprClass: {
1821f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    const MemberExpr *M = cast<MemberExpr>(E);
1822102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return M->getBase()->isOBJCGCCandidate(Ctx);
182344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
182444baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ArraySubscriptExprClass:
1825f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
182644baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
182744baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian}
1828369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
182911ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidisbool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
183011ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis  if (isTypeDependent())
183111ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis    return false;
18327eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
183311ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis}
183411ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis
1835864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCallQualType Expr::findBoundMemberType(const Expr *expr) {
1836e0a22d06888c13989b3f72db319f1d498bf69153John McCall  assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
1837864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1838864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Bound member expressions are always one of these possibilities:
1839864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  //   x->m      x.m      x->*y      x.*y
1840864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // (possibly parenthesized)
1841864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1842864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  expr = expr->IgnoreParens();
1843864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1844864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1845864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return mem->getMemberDecl()->getType();
1846864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
1847864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1848864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1849864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1850864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall                      ->getPointeeType();
1851864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    assert(type->isFunctionType());
1852864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return type;
1853864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
1854864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1855864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  assert(isa<UnresolvedMemberExpr>(expr));
1856864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  return QualType();
1857864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall}
1858864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1859369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1860369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                          Expr::CanThrowResult CT2) {
1861369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // CanThrowResult constants are ordered so that the maximum is the correct
1862369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // merge result.
1863369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return CT1 > CT2 ? CT1 : CT2;
1864369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1865369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1866369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1867369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr *E = const_cast<Expr*>(CE);
1868369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr::CanThrowResult R = Expr::CT_Cannot;
18697502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
1870369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1871369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1872369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return R;
1873369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1874369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
18757a614d8380297fcd2bc23986241905d97222948cRichard Smithstatic Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
18767a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           const Decl *D,
1877369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                           bool NullThrows = true) {
1878369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!D)
1879369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1880369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1881369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // See if we can get a function type from the decl somehow.
1882369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1883369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!VD) // If we have no clue what we're calling, assume the worst.
1884369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Can;
1885369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
18865221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  // As an extension, we assume that __attribute__((nothrow)) functions don't
18875221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  // throw.
18885221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
18895221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl    return Expr::CT_Cannot;
18905221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl
1891369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  QualType T = VD->getType();
1892369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const FunctionProtoType *FT;
1893369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if ((FT = T->getAs<FunctionProtoType>())) {
1894369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  } else if (const PointerType *PT = T->getAs<PointerType>())
1895369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1896369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1897369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1898369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1899369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1900369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1901369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1902369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1903369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!FT)
1904369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Can;
1905369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
19067a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (FT->getExceptionSpecType() == EST_Delayed) {
19077a614d8380297fcd2bc23986241905d97222948cRichard Smith    assert(isa<CXXConstructorDecl>(D) &&
19087a614d8380297fcd2bc23986241905d97222948cRichard Smith           "only constructor exception specs can be unknown");
19097a614d8380297fcd2bc23986241905d97222948cRichard Smith    Ctx.getDiagnostics().Report(E->getLocStart(),
19107a614d8380297fcd2bc23986241905d97222948cRichard Smith                                diag::err_exception_spec_unknown)
19117a614d8380297fcd2bc23986241905d97222948cRichard Smith      << E->getSourceRange();
19127a614d8380297fcd2bc23986241905d97222948cRichard Smith    return Expr::CT_Can;
19137a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
19147a614d8380297fcd2bc23986241905d97222948cRichard Smith
19158026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
1916369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1917369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1918369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1919369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (DC->isTypeDependent())
1920369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Dependent;
1921369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1922295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  if (!DC->getTypeAsWritten()->isReferenceType())
1923295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl    return Expr::CT_Cannot;
1924295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl
1925be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman  if (DC->getSubExpr()->isTypeDependent())
1926be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    return Expr::CT_Dependent;
1927be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman
1928369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1929369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1930369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1931369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1932369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                           const CXXTypeidExpr *DC) {
1933369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (DC->isTypeOperand())
1934369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1935369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1936369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr *Op = DC->getExprOperand();
1937369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (Op->isTypeDependent())
1938369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Dependent;
1939369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1940369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const RecordType *RT = Op->getType()->getAs<RecordType>();
1941369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!RT)
1942369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1943369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1944369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1945369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1946369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1947369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (Op->Classify(C).isPRValue())
1948369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1949369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1950369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return Expr::CT_Can;
1951369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1952369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1953369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian RedlExpr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1954369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // C++ [expr.unary.noexcept]p3:
1955369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  //   [Can throw] if in a potentially-evaluated context the expression would
1956369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  //   contain:
1957369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  switch (getStmtClass()) {
1958369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXThrowExprClass:
1959369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated throw-expression
1960369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
1961369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1962369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDynamicCastExprClass: {
1963369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1964369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     where T is a reference type, that requires a run-time check
1965369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1966369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1967369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1968369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1969369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1970369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1971369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXTypeidExprClass:
1972369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated typeid expression applied to a glvalue
1973369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     expression whose type is a polymorphic class type
1974369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1975369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1976369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated call to a function, member function, function
1977369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     pointer, or member function pointer that does not have a non-throwing
1978369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     exception-specification
1979369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CallExprClass:
1980369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXOperatorCallExprClass:
1981369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXMemberCallExprClass: {
1982ebc93e176dad36fa8a28dd3a36c5b3dc7630d87dEli Friedman    const CallExpr *CE = cast<CallExpr>(this);
1983be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    CanThrowResult CT;
1984be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    if (isTypeDependent())
1985be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = CT_Dependent;
1986ebc93e176dad36fa8a28dd3a36c5b3dc7630d87dEli Friedman    else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1987ebc93e176dad36fa8a28dd3a36c5b3dc7630d87dEli Friedman      CT = CT_Cannot;
1988be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    else
19897a614d8380297fcd2bc23986241905d97222948cRichard Smith      CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
1990369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1991369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1992369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1993369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1994369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1995295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  case CXXConstructExprClass:
1996295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  case CXXTemporaryObjectExprClass: {
19977a614d8380297fcd2bc23986241905d97222948cRichard Smith    CanThrowResult CT = CanCalleeThrow(C, this,
1998369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl        cast<CXXConstructExpr>(this)->getConstructor());
1999369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
2000369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
2001369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2002369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
2003369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2004369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXNewExprClass: {
2005be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    CanThrowResult CT;
2006be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    if (isTypeDependent())
2007be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = CT_Dependent;
2008be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    else
2009be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = MergeCanThrow(
20107a614d8380297fcd2bc23986241905d97222948cRichard Smith        CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
20117a614d8380297fcd2bc23986241905d97222948cRichard Smith        CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
2012369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                       /*NullThrows*/false));
2013369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
2014369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
2015369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2016369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
2017369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2018369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDeleteExprClass: {
2019be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    CanThrowResult CT;
2020be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2021be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    if (DTy.isNull() || DTy->isDependentType()) {
2022be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = CT_Dependent;
2023be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    } else {
20247a614d8380297fcd2bc23986241905d97222948cRichard Smith      CT = CanCalleeThrow(C, this,
20257a614d8380297fcd2bc23986241905d97222948cRichard Smith                          cast<CXXDeleteExpr>(this)->getOperatorDelete());
2026be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      if (const RecordType *RT = DTy->getAs<RecordType>()) {
2027be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman        const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
20287a614d8380297fcd2bc23986241905d97222948cRichard Smith        CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
20290b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      }
2030be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      if (CT == CT_Can)
2031be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman        return CT;
20320b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    }
20330b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
20340b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl  }
20350b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl
20360b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl  case CXXBindTemporaryExprClass: {
20370b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    // The bound temporary has to be destroyed again, which might throw.
20387a614d8380297fcd2bc23986241905d97222948cRichard Smith    CanThrowResult CT = CanCalleeThrow(C, this,
20390b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
20400b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    if (CT == CT_Can)
20410b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      return CT;
2042369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2043369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
2044369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2045369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // ObjC message sends are like function calls, but never have exception
2046369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // specs.
2047369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCMessageExprClass:
2048369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCPropertyRefExprClass:
2049369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
2050369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2051369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Many other things have subexpressions, so we have to test those.
2052369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some are simple:
2053369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ParenExprClass:
2054369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case MemberExprClass:
2055369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXReinterpretCastExprClass:
2056369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXConstCastExprClass:
2057369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ConditionalOperatorClass:
2058369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CompoundLiteralExprClass:
2059369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ExtVectorElementExprClass:
2060369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case InitListExprClass:
2061369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case DesignatedInitExprClass:
2062369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ParenListExprClass:
2063369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case VAArgExprClass:
2064369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDefaultArgExprClass:
20654765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case ExprWithCleanupsClass:
2066369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCIvarRefExprClass:
2067369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCIsaExprClass:
2068369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ShuffleVectorExprClass:
2069369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CanSubExprsThrow(C, this);
2070369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2071369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some might be dependent for other reasons.
2072369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case UnaryOperatorClass:
2073369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ArraySubscriptExprClass:
2074369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ImplicitCastExprClass:
2075369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CStyleCastExprClass:
2076369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXStaticCastExprClass:
2077369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXFunctionalCastExprClass:
2078369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case BinaryOperatorClass:
207903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case CompoundAssignOperatorClass:
208003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass: {
2081369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2082369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2083369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
2084369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2085369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2086369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case StmtExprClass:
2087369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
2088369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2089369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ChooseExprClass:
2090369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (isTypeDependent() || isValueDependent())
2091369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT_Dependent;
2092369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2093369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2094f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
2095f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (cast<GenericSelectionExpr>(this)->isResultDependent())
2096f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      return CT_Dependent;
2097f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2098f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
2099369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some expressions are always dependent.
2100369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case DependentScopeDeclRefExprClass:
2101369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXUnresolvedConstructExprClass:
2102369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDependentScopeMemberExprClass:
2103369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Dependent;
2104369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2105369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  default:
2106369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // All other expressions don't have subexpressions, or else they are
2107369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // unevaluated.
2108369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Cannot;
2109369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
2110369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
2111369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
21124e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() {
21134e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  Expr* E = this;
2114b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara  while (true) {
2115b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2116b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      E = P->getSubExpr();
2117b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2118b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2119b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2120b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2121b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2122b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2123b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2124b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2125f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2126f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2127f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2128f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2129f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2130f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
2131b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
2132b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara  }
21334e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek}
21344e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek
213556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
213656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand.
213756f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() {
213856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  Expr *E = this;
213956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  while (true) {
2140b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
214156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
2142b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2143b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2144b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
214556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
2146b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2147b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2148b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2149b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2150b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2151b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2152b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2153b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2154f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2155f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2156f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2157f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2158f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2159f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
216003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
216103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
216203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
216303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
216403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
2165c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2166c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2167c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2168c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2169c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2170b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
217156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  }
217256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner}
217356f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner
21749c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
21759c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// casts.  This is intended purely as a temporary workaround for code
21769c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// that hasn't yet been rewritten to do the right thing about those
21779c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// casts, and may disappear along with the last internal use.
2178f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallExpr *Expr::IgnoreParenLValueCasts() {
2179f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  Expr *E = this;
21809c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall  while (true) {
2181f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2182f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      E = P->getSubExpr();
2183f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      continue;
21849c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall    } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2185f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      if (P->getCastKind() == CK_LValueToRValue) {
2186f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        E = P->getSubExpr();
2187f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        continue;
2188f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
21899c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall    } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
21909c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall      if (P->getOpcode() == UO_Extension) {
21919c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall        E = P->getSubExpr();
21929c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall        continue;
21939c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall      }
2194f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2195f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2196f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2197f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2198f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
219903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    } else if (MaterializeTemporaryExpr *Materialize
220003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
220103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
220203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
2203c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    } else if (SubstNonTypeTemplateParmExpr *NTTP
2204c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2205c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2206c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2207f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
2208f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
2209f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
2210f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return E;
2211f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
2212f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
22132fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCallExpr *Expr::IgnoreParenImpCasts() {
22142fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  Expr *E = this;
22152fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  while (true) {
2216b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
22172fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      E = P->getSubExpr();
2218b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2219b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2220b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
22212fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      E = P->getSubExpr();
2222b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2223b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2224b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2225b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2226b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2227b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2228b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2229b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2230f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2231f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2232f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2233f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2234f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2235f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
223603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
223703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
223803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
223903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
224003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
2241c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2242c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2243c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2244c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2245c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2246b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
22472fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  }
22482fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall}
22492fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall
22502f072b442879b8bba8c5dea11d7c61bedb1924aeHans WennborgExpr *Expr::IgnoreConversionOperator() {
22512f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
225214d251cd62942bf7d56bb87a267ba2ca2f7fae3eChandler Carruth    if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
22532f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg      return MCE->getImplicitObjectArgument();
22542f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  }
22552f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  return this;
22562f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg}
22572f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg
2258ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2259ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// value (including ptr->int casts of the same size).  Strip off any
2260ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// ParenExpr or CastExprs, returning their operand.
2261ecdd84147c0765caa999ddc22dde25b42712bb4dChris LattnerExpr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2262ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  Expr *E = this;
2263ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  while (true) {
2264ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2265ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      E = P->getSubExpr();
2266ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      continue;
2267ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    }
22681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2269ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2270ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      // We ignore integer <-> casts that are of the same width, ptr<->ptr and
22712ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor      // ptr<->int casts of the same width.  We also ignore all identity casts.
2272ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      Expr *SE = P->getSubExpr();
22731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2274ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2275ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
2276ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
2277ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
22781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2279b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if ((E->getType()->isPointerType() ||
22809d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           E->getType()->isIntegralType(Ctx)) &&
2281b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara          (SE->getType()->isPointerType() ||
22829d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           SE->getType()->isIntegralType(Ctx)) &&
2283ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner          Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2284ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
2285ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
2286ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
2287ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    }
22881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2289b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2290b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2291b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2292b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2293b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2294b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2295b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara
2296f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2297f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2298f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2299f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2300f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2301f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
2302f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
2303c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2304c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2305c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2306c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2307c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2308c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor
2309ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    return E;
2310ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  }
2311ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner}
2312ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
23136eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorbool Expr::isDefaultArgument() const {
23146eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  const Expr *E = this;
231503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
231603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = M->GetTemporaryExpr();
231703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
23186eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
23196eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    E = ICE->getSubExprAsWritten();
2320c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
23216eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return isa<CXXDefaultArgExpr>(E);
23226eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2323ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
23242f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// \brief Skip over any no-op casts and any temporary-binding
23252f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// expressions.
2326f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlssonstatic const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
232703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
232803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = M->GetTemporaryExpr();
232903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
23302f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
23312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
23322f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
23332f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
23342f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
23352f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
23362f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
23372f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
23382f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    E = BE->getSubExpr();
23392f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
23402f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
23412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
23422f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
23432f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
23442f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
23452f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
2346f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson
2347f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson  return E->IgnoreParens();
23482f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
23492f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
2350558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// isTemporaryObject - Determines if this expression produces a
2351558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// temporary of the given class type.
2352558d2abc7f9fd6801cc7677200992313ae90b5d8John McCallbool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2353558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2354558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    return false;
23552f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
2356f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson  const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
23572f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
235858277b57f9492d0234748be89bcad48b322c5cf7John McCall  // Temporaries are by definition pr-values of class type.
2359db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian  if (!E->Classify(C).isPRValue()) {
2360db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian    // In this context, property reference is a message call and is pr-value.
236112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    if (!isa<ObjCPropertyRefExpr>(E))
2362db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian      return false;
2363db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian  }
23642f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
236519e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // Black-list a few cases which yield pr-values of class type that don't
236619e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // refer to temporaries of that type:
236719e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall
236819e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // - implicit derived-to-base conversions
2369558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (isa<ImplicitCastExpr>(E)) {
2370558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2371558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_DerivedToBase:
2372558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_UncheckedDerivedToBase:
2373558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      return false;
2374558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    default:
2375558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      break;
2376558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    }
23772f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
23782f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
237919e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // - member expressions (all)
238019e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  if (isa<MemberExpr>(E))
238119e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall    return false;
238219e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall
238356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // - opaque values (all)
238456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (isa<OpaqueValueExpr>(E))
238556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return false;
238656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
2387558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  return true;
23882f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
23892f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
239075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregorbool Expr::isImplicitCXXThis() const {
239175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  const Expr *E = this;
239275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
239375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  // Strip away parentheses and casts we don't care about.
239475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  while (true) {
239575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
239675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      E = Paren->getSubExpr();
239775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      continue;
239875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
239975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
240075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
240175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      if (ICE->getCastKind() == CK_NoOp ||
240275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_LValueToRValue ||
240375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_DerivedToBase ||
240475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_UncheckedDerivedToBase) {
240575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        E = ICE->getSubExpr();
240675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        continue;
240775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      }
240875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
240975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
241075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
241175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      if (UnOp->getOpcode() == UO_Extension) {
241275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        E = UnOp->getSubExpr();
241375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        continue;
241475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      }
241575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
241675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
241703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (const MaterializeTemporaryExpr *M
241803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
241903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = M->GetTemporaryExpr();
242003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
242103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
242203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
242375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    break;
242475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  }
242575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
242675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
242775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    return This->isImplicit();
242875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
242975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  return false;
243075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor}
243175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
2432898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyTypeDependentArguments - Determines if any of the expressions
2433898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is type-dependent.
2434898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2435898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
2436898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isTypeDependent())
2437898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
2438898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2439898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
2440898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
2441898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2442898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyValueDependentArguments - Determines if any of the expressions
2443898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is value-dependent.
2444898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2445898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
2446898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isValueDependent())
2447898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
2448898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2449898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
2450898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
2451898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
24524204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCallbool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
2453c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // This function is attempting whether an expression is an initializer
2454c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // which can be evaluated at compile-time.  isEvaluatable handles most
2455c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // of the cases, but it can't deal with some initializer-specific
2456c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // expressions, and it can't deal with aggregates; we deal with those here,
2457c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // and fall back to isEvaluatable for the other cases.
2458c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman
24594204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // If we ever capture reference-binding directly in the AST, we can
24604204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // kill the second parameter.
24614204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
24624204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  if (IsForRef) {
24634204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    EvalResult Result;
24644204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
24654204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  }
24661f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman
2467e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  switch (getStmtClass()) {
2468c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  default: break;
2469e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case StringLiteralClass:
247014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case ObjCStringLiteralClass:
2471eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  case ObjCEncodeExprClass:
2472e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    return true;
2473b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXTemporaryObjectExprClass:
2474b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXConstructExprClass: {
2475b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall    const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
24764204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
24774204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    // Only if it's
2478180f47959a066795cc0f409433023af448bb0328Richard Smith    if (CE->getConstructor()->isTrivial()) {
2479180f47959a066795cc0f409433023af448bb0328Richard Smith      // 1) an application of the trivial default constructor or
2480180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!CE->getNumArgs()) return true;
2481180f47959a066795cc0f409433023af448bb0328Richard Smith
2482180f47959a066795cc0f409433023af448bb0328Richard Smith      // 2) an elidable trivial copy construction of an operand which is
2483180f47959a066795cc0f409433023af448bb0328Richard Smith      //    itself a constant initializer.  Note that we consider the
2484180f47959a066795cc0f409433023af448bb0328Richard Smith      //    operand on its own, *not* as a reference binding.
2485180f47959a066795cc0f409433023af448bb0328Richard Smith      if (CE->isElidable() &&
2486180f47959a066795cc0f409433023af448bb0328Richard Smith          CE->getArg(0)->isConstantInitializer(Ctx, false))
2487180f47959a066795cc0f409433023af448bb0328Richard Smith        return true;
2488180f47959a066795cc0f409433023af448bb0328Richard Smith    }
2489180f47959a066795cc0f409433023af448bb0328Richard Smith
2490180f47959a066795cc0f409433023af448bb0328Richard Smith    // 3) a foldable constexpr constructor.
2491180f47959a066795cc0f409433023af448bb0328Richard Smith    break;
2492b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  }
249359b5da6d853b4368b984700315adf7b37de05764Nate Begeman  case CompoundLiteralExprClass: {
24941f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // This handles gcc's extension that allows global initializers like
24951f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // "struct x {int x;} x = (struct x) {};".
24961f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This accepts other cases it shouldn't!
249759b5da6d853b4368b984700315adf7b37de05764Nate Begeman    const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
24984204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return Exp->isConstantInitializer(Ctx, false);
249959b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
2500e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case InitListExprClass: {
25011f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This doesn't deal with fields with reference types correctly.
25021f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This incorrectly allows pointers cast to integers to be assigned
25031f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // to bitfields.
2504e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    const InitListExpr *Exp = cast<InitListExpr>(this);
2505e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    unsigned numInits = Exp->getNumInits();
2506e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    for (unsigned i = 0; i < numInits; i++) {
25074204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
2508e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson        return false;
2509e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    }
2510c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    return true;
2511e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
25123498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  case ImplicitValueInitExprClass:
25133498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return true;
25143ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  case ParenExprClass:
25154204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return cast<ParenExpr>(this)->getSubExpr()
25164204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      ->isConstantInitializer(Ctx, IsForRef);
2517f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
2518f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (cast<GenericSelectionExpr>(this)->isResultDependent())
2519f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      return false;
2520f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()
2521f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      ->isConstantInitializer(Ctx, IsForRef);
25225cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara  case ChooseExprClass:
25235cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara    return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
25245cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara      ->isConstantInitializer(Ctx, IsForRef);
2525c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case UnaryOperatorClass: {
2526c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    const UnaryOperator* Exp = cast<UnaryOperator>(this);
25272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Exp->getOpcode() == UO_Extension)
25284204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
2529c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
2530c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  }
25313ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  case BinaryOperatorClass: {
25323ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    // Special case &&foo - &&bar.  It would be nice to generalize this somehow
25333ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    // but this handles the common case.
25343ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    const BinaryOperator *Exp = cast<BinaryOperator>(this);
25352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Exp->getOpcode() == BO_Sub &&
25363ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner        isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
25373ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner        isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
25383ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner      return true;
25393ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    break;
25403ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  }
25414204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  case CXXFunctionalCastExprClass:
2542b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXStaticCastExprClass:
254381045d8dcd967def69d8e0945566214a9fe9ffccChris Lattner  case ImplicitCastExprClass:
2544d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith  case CStyleCastExprClass: {
2545d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith    const CastExpr *CE = cast<CastExpr>(this);
2546d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith
2547d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith    // Handle bitcasts of vector constants.
2548d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith    if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2549d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith      return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2550d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith
2551c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // Handle casts with a destination that's a struct or union; this
2552c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // deals with both the gcc no-op struct cast extension and the
2553c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // cast-to-union extension.
2554c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    if (getType()->isRecordType())
2555d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith      return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2556d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith
2557430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    // Integer->integer casts can be handled here, which is important for
2558430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    // things like (int)(&&x-&&y).  Scary but true.
2559430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    if (getType()->isIntegerType() &&
2560d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith        CE->getSubExpr()->getType()->isIntegerType())
2561d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith      return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2562d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith
2563c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
2564d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith  }
256503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass:
25665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
256703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                            ->isConstantInitializer(Ctx, false);
2568e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
2569c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  return isEvaluatable(Ctx);
257038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff}
257138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
257282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
257382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// pointer constant or not, as well as the specific kind of constant detected.
257482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// Null pointer constants can be integer constant expressions with the
257582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// value zero, casts of zero to void*, nullptr (C++0X), or __null
257682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// (a GNU extension).
257782214a80c0163e01e4d8dec1426023c89277dbb4Chandler CarruthExpr::NullPointerConstantKind
257882214a80c0163e01e4d8dec1426023c89277dbb4Chandler CarruthExpr::isNullPointerConstant(ASTContext &Ctx,
257982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth                            NullPointerConstantValueDependence NPC) const {
2580ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  if (isValueDependent()) {
2581ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    switch (NPC) {
2582ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_NeverValueDependent:
2583b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unexpected value dependent expression!");
2584ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNull:
258582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      if (isTypeDependent() || getType()->isIntegralType(Ctx))
258682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth        return NPCK_ZeroInteger;
258782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      else
258882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth        return NPCK_NotNull;
2589c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2590ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNotNull:
259182214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      return NPCK_NotNull;
2592ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    }
2593ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  }
2594f515b2268f829adfbfdb751f54d102b53ed0285cDaniel Dunbar
25950777972d38a3125efed962b045704c30ae6965cfSebastian Redl  // Strip off a cast to void*, if it exists. Except in C++.
25960835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis  if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
25976215dee86c0e715b9f2b0d401ab2a5fcf629f1afSebastian Redl    if (!Ctx.getLangOptions().CPlusPlus) {
25980777972d38a3125efed962b045704c30ae6965cfSebastian Redl      // Check that it is a cast to void*.
25996217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
26000777972d38a3125efed962b045704c30ae6965cfSebastian Redl        QualType Pointee = PT->getPointeeType();
26010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        if (!Pointee.hasQualifiers() &&
26020777972d38a3125efed962b045704c30ae6965cfSebastian Redl            Pointee->isVoidType() &&                              // to void*
26030777972d38a3125efed962b045704c30ae6965cfSebastian Redl            CE->getSubExpr()->getType()->isIntegerType())         // from int.
2604ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor          return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
26050777972d38a3125efed962b045704c30ae6965cfSebastian Redl      }
26065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2607aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2608aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Ignore the ImplicitCastExpr type entirely.
2609ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
2610aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2611aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Accept ((void*)0) as a null pointer constant, as many other
2612aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // implementations do.
2613ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
2614f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  } else if (const GenericSelectionExpr *GE =
2615f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne               dyn_cast<GenericSelectionExpr>(this)) {
2616f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
26171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else if (const CXXDefaultArgExpr *DefaultArg
26188123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner               = dyn_cast<CXXDefaultArgExpr>(this)) {
261904421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    // See through default argument expressions
2620ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
26212d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor  } else if (isa<GNUNullExpr>(this)) {
26222d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    // The GNU __null extension is always a null pointer constant.
262382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_GNUNull;
262403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  } else if (const MaterializeTemporaryExpr *M
262503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                   = dyn_cast<MaterializeTemporaryExpr>(this)) {
262603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
26274b9c2d235fb9449e249d74f48ecfec601650de93John McCall  } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
26284b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (const Expr *Source = OVE->getSourceExpr())
26294b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return Source->isNullPointerConstant(Ctx, NPC);
2630aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff  }
26312d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
26326e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // C++0x nullptr_t is always a null pointer constant.
26336e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  if (getType()->isNullPtrType())
263482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_CXX0X_nullptr;
26356e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2636ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian  if (const RecordType *UT = getType()->getAsUnionType())
2637ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian    if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2638ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2639ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian        const Expr *InitExpr = CLE->getInitializer();
2640ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian        if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2641ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian          return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2642ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      }
2643aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  // This expression must be an integer type.
2644c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  if (!getType()->isIntegerType() ||
264556fc0d18caf9c829647a5e3ce35197f0d7e0feeeFariborz Jahanian      (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
264682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_NotNull;
26471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have an integer constant expression, we need to *evaluate* it and
26495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // test for the value 0.
265009de1767990d4828bcaf0dd22033a5dddeecbe08Eli Friedman  llvm::APSInt Result;
265182214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
265282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
265382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
26545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
265531a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff
2656f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// \brief If this expression is an l-value for an Objective C
2657f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// property, find the underlying property reference expression.
2658f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallconst ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2659f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  const Expr *E = this;
2660f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  while (true) {
2661f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    assert((E->getValueKind() == VK_LValue &&
2662f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall            E->getObjectKind() == OK_ObjCProperty) &&
2663f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall           "expression is not a property reference");
2664f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    E = E->IgnoreParenCasts();
2665f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2666f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      if (BO->getOpcode() == BO_Comma) {
2667f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        E = BO->getRHS();
2668f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        continue;
2669f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
2670f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
2671f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
2672f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
2673f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
2674f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
2675f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return cast<ObjCPropertyRefExpr>(E);
2676f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
2677f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
267833bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas GregorFieldDecl *Expr::getBitField() {
26796f4a69a3107e7ff1569c747f7c6bdf7cff8cbf55Douglas Gregor  Expr *E = this->IgnoreParens();
268033bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
2681de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2682f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (ICE->getCastKind() == CK_LValueToRValue ||
2683f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
2684de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      E = ICE->getSubExpr()->IgnoreParens();
2685de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    else
2686de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
2687de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  }
2688de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
268927c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
269086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
269133bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor      if (Field->isBitField())
269233bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor        return Field;
269333bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
26940f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis  if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
26950f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis    if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
26960f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis      if (Field->isBitField())
26970f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis        return Field;
26980f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis
269942068e9d420889014ee26894e421fead336b239dEli Friedman  if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
270033bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor    if (BinOp->isAssignmentOp() && BinOp->getLHS())
270133bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor      return BinOp->getLHS()->getBitField();
270233bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
270342068e9d420889014ee26894e421fead336b239dEli Friedman    if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
270442068e9d420889014ee26894e421fead336b239dEli Friedman      return BinOp->getRHS()->getBitField();
270542068e9d420889014ee26894e421fead336b239dEli Friedman  }
270642068e9d420889014ee26894e421fead336b239dEli Friedman
270733bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor  return 0;
270827c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
270927c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
2710093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlssonbool Expr::refersToVectorElement() const {
2711093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  const Expr *E = this->IgnoreParens();
2712c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2713093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
27145baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    if (ICE->getValueKind() != VK_RValue &&
27152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        ICE->getCastKind() == CK_NoOp)
2716093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      E = ICE->getSubExpr()->IgnoreParens();
2717093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    else
2718093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      break;
2719093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  }
2720c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2721093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2722093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return ASE->getBase()->getType()->isVectorType();
2723093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
2724093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (isa<ExtVectorElementExpr>(E))
2725093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return true;
2726093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
2727093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  return false;
2728093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson}
2729093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
27302140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// isArrow - Return true if the base expression is a pointer to vector,
27312140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// return false if the base expression is a vector.
27322140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattnerbool ExtVectorElementExpr::isArrow() const {
27332140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner  return getBase()->getType()->isPointerType();
27342140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner}
27352140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner
2736213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const {
2737183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const VectorType *VT = getType()->getAs<VectorType>())
27388a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    return VT->getNumElements();
27398a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  return 1;
27404d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner}
27414d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner
27428a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated.
2743213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const {
2744a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // FIXME: Refactor this code to an accessor on the AST node which returns the
2745a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // "type" of component access, and share with code below and in Sema.
27465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Comp = Accessor->getName();
2747190d6a25393995b42e32086949a68285ee423fb9Nate Begeman
2748190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Halving swizzles do not contain duplicate elements.
2749150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
2750190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    return false;
27511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2752190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Advance past s-char prefix on hex swizzles.
2753150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
2754150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar    Comp = Comp.substr(1);
27551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2756150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  for (unsigned i = 0, e = Comp.size(); i != e; ++i)
27575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
2758fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff        return true;
2759150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar
2760fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  return false;
2761fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff}
2762b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
27638a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
27643b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess(
27655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  SmallVectorImpl<unsigned> &Elts) const {
27665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Comp = Accessor->getName();
27674b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
27684b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar    Comp = Comp.substr(1);
27691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27704b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isHi =   Comp == "hi";
27714b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isLo =   Comp == "lo";
27724b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isEven = Comp == "even";
27734b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isOdd  = Comp == "odd";
27741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27758a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
27768a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    uint64_t Index;
27771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27788a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    if (isHi)
27798a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = e + i;
27808a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isLo)
27818a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = i;
27828a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isEven)
27838a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i;
27848a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isOdd)
27858a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i + 1;
27868a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else
27874b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar      Index = ExtVectorType::getAccessorIdx(Comp[i]);
2788b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
27893b8d116703db8018f855cbb4733ace426422623bNate Begeman    Elts.push_back(Index);
2790b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner  }
27918a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman}
27928a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman
279304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
2794f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
279504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
279604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation SuperLoc,
279704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 bool IsInstanceSuper,
279804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 QualType SuperType,
2799c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
2800207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
2801207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
280204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
28038d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
280404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
2805f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
2806bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*TypeDependent=*/false, /*ValueDependent=*/false,
2807561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*InstantiationDependent=*/false,
2808bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*ContainsUnexpandedParameterPack=*/false),
280904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
281004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2811b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(IsInstanceSuper? SuperInstance : SuperClass),
2812b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
2813207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
2814c2350e553b853ad00914faf23fa731e5fc4a8a5cDouglas Gregor{
2815207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
281604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(SuperType.getAsOpaquePtr());
281704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
281804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
281904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
2820f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
282104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
282204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 TypeSourceInfo *Receiver,
2823f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                 Selector Sel,
2824207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
2825207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
282604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
28278d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
282804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
2829f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
2830561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         T->isDependentType(), T->isInstantiationDependentType(),
2831561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         T->containsUnexpandedParameterPack()),
283204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
283304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2834b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(Class),
2835b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false),
2836207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
283704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor{
2838207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
283904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(Receiver);
284004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
284104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
284204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
2843f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
284404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
284504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 Expr *Receiver,
2846c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
2847207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
2848207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
284904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
28508d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
285104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
2852f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
2853bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Receiver->isTypeDependent(),
2854561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Receiver->isInstantiationDependent(),
2855bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Receiver->containsUnexpandedParameterPack()),
285604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
285704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2858b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(Instance),
2859b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false),
2860207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
286104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor{
2862207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
286304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(Receiver);
2864207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2865207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2866207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidisvoid ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2867207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
2868207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                         SelectorLocationsKind SelLocsK) {
2869207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  setNumArgs(Args.size());
2870aa165f8458b51c546bebff947343e1a36f3594cbDouglas Gregor  Expr **MyArgs = getArgs();
28718d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis  for (unsigned I = 0; I != Args.size(); ++I) {
2872bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->isTypeDependent())
2873bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
2874bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->isValueDependent())
2875bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
2876561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[I]->isInstantiationDependent())
2877561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
2878bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->containsUnexpandedParameterPack())
2879bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
2880bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
2881bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    MyArgs[I] = Args[I];
2882bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
2883207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2884207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelLocsKind = SelLocsK;
2885207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  if (SelLocsK == SelLoc_NonStandard)
2886207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
288704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
288804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
288904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2890f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
289104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
289204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation SuperLoc,
289304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         bool IsInstanceSuper,
289404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         QualType SuperType,
2895c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
2896951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
289704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
28988d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
289904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2900207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelectorLocationsKind SelLocsK;
2901207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
2902f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
2903207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   SuperType, Sel, SelLocs, SelLocsK,
2904207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   Method, Args, RBracLoc);
290504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
290604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
290704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2908f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
290904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
291004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         TypeSourceInfo *Receiver,
2911c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
2912951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
291304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
29148d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
291504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2916207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelectorLocationsKind SelLocsK;
2917207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
2918951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
2919207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   SelLocs, SelLocsK, Method, Args, RBracLoc);
292004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
292104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
292204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2923f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
292404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
292504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         Expr *Receiver,
2926f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                         Selector Sel,
2927951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
292804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
29298d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
293004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2931207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelectorLocationsKind SelLocsK;
2932207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
2933951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
2934207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   SelLocs, SelLocsK, Method, Args, RBracLoc);
293504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
293604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
2937c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
2938207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                              unsigned NumArgs,
2939207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                              unsigned NumStoredSelLocs) {
2940207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
294104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
294204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
2943e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2944207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios KyrtzidisObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2945207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        ArrayRef<Expr *> Args,
2946207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        SourceLocation RBraceLoc,
2947207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        ArrayRef<SourceLocation> SelLocs,
2948207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        Selector Sel,
2949207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        SelectorLocationsKind &SelLocsK) {
2950207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2951207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2952207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                                               : 0;
2953207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  return alloc(C, Args.size(), NumStoredSelLocs);
2954207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2955207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2956207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios KyrtzidisObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2957207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        unsigned NumArgs,
2958207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        unsigned NumStoredSelLocs) {
2959207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2960207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2961207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  return (ObjCMessageExpr *)C.Allocate(Size,
2962207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                     llvm::AlignOf<ObjCMessageExpr>::Alignment);
2963207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2964207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2965207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidisvoid ObjCMessageExpr::getSelectorLocs(
2966207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                               SmallVectorImpl<SourceLocation> &SelLocs) const {
2967207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2968207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    SelLocs.push_back(getSelectorLoc(i));
2969207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2970207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2971e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios KyrtzidisSourceRange ObjCMessageExpr::getReceiverRange() const {
2972e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  switch (getReceiverKind()) {
2973e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case Instance:
2974e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getInstanceReceiver()->getSourceRange();
2975e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2976e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case Class:
2977e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2978e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2979e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case SuperInstance:
2980e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case SuperClass:
2981e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getSuperLoc();
2982e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  }
2983e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2984e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  return SourceLocation();
2985e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis}
2986e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
298704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorSelector ObjCMessageExpr::getSelector() const {
298804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (HasMethod)
298904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
299004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                               ->getSelector();
2991c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  return Selector(SelectorOrMethod);
299204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
299304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
299404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
299504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  switch (getReceiverKind()) {
299604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case Instance:
299704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    if (const ObjCObjectPointerType *Ptr
299804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
299904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor      return Ptr->getInterfaceDecl();
300004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
30014df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
300204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case Class:
3003c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (const ObjCObjectType *Ty
3004c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          = getClassReceiver()->getAs<ObjCObjectType>())
3005c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Ty->getInterface();
300604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
30074df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
300804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case SuperInstance:
300904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    if (const ObjCObjectPointerType *Ptr
301004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          = getSuperType()->getAs<ObjCObjectPointerType>())
301104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor      return Ptr->getInterfaceDecl();
301204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
3013c2350e553b853ad00914faf23fa731e5fc4a8a5cDouglas Gregor
301404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case SuperClass:
3015ee8a6cafe8b69c316dd4fa5f6ea4838ffe15621cArgyrios Kyrtzidis    if (const ObjCObjectType *Iface
3016ee8a6cafe8b69c316dd4fa5f6ea4838ffe15621cArgyrios Kyrtzidis          = getSuperType()->getAs<ObjCObjectType>())
3017ee8a6cafe8b69c316dd4fa5f6ea4838ffe15621cArgyrios Kyrtzidis      return Iface->getInterface();
301804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
301904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  }
30200389e6bd0159bfdd08f7c50a37543b6e3adf0c33Chris Lattner
302104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return 0;
3022eb3b324800598cc3d5385fbad95ae5cff2c79113Ted Kremenek}
30230389e6bd0159bfdd08f7c50a37543b6e3adf0c33Chris Lattner
30245f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef ObjCBridgedCastExpr::getBridgeKindName() const {
3025f85e193739c953358c865005855253af4f68a497John McCall  switch (getBridgeKind()) {
3026f85e193739c953358c865005855253af4f68a497John McCall  case OBC_Bridge:
3027f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge";
3028f85e193739c953358c865005855253af4f68a497John McCall  case OBC_BridgeTransfer:
3029f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge_transfer";
3030f85e193739c953358c865005855253af4f68a497John McCall  case OBC_BridgeRetained:
3031f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge_retained";
3032f85e193739c953358c865005855253af4f68a497John McCall  }
3033f85e193739c953358c865005855253af4f68a497John McCall
3034f85e193739c953358c865005855253af4f68a497John McCall  return "__bridge";
3035f85e193739c953358c865005855253af4f68a497John McCall}
3036f85e193739c953358c865005855253af4f68a497John McCall
30374ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ChooseExpr::isConditionTrue(const ASTContext &C) const {
3038a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  return getCond()->EvaluateKnownConstInt(C) != 0;
303927437caadea35f84d550cd29f024fcf3ea240eecChris Lattner}
304027437caadea35f84d550cd29f024fcf3ea240eecChris Lattner
3041bebbe0d9b7568ce43a464286bee49429489ef483Douglas GregorShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3042bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor                                     QualType Type, SourceLocation BLoc,
3043bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor                                     SourceLocation RP)
3044bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor   : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3045bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          Type->isDependentType(), Type->isDependentType(),
3046561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          Type->isInstantiationDependentType(),
3047bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          Type->containsUnexpandedParameterPack()),
3048bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor     BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3049bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor{
3050bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  SubExprs = new (C) Stmt*[nexpr];
3051bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  for (unsigned i = 0; i < nexpr; i++) {
3052bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
3053bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
3054bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
3055bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
3056561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
3057561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
3058bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
3059bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
3060bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
3061bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    SubExprs[i] = args[i];
3062bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
3063bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor}
3064bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
3065888376a2bbcfc2f047902249f8455918e2489ae1Nate Begemanvoid ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3066888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman                                 unsigned NumExprs) {
3067888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman  if (SubExprs) C.Deallocate(SubExprs);
3068888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
3069888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman  SubExprs = new (C) Stmt* [NumExprs];
307094cd5d1397bb1a8bcd109602aa38dd787b164c22Douglas Gregor  this->NumExprs = NumExprs;
307194cd5d1397bb1a8bcd109602aa38dd787b164c22Douglas Gregor  memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
30721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
3073888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
3074f111d935722ed488144600cea5ed03a6b5069e8fPeter CollingbourneGenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3075f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation GenericLoc, Expr *ControllingExpr,
3076f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3077f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned NumAssocs, SourceLocation DefaultLoc,
3078f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation RParenLoc,
3079f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               bool ContainsUnexpandedParameterPack,
3080f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned ResultIndex)
3081f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  : Expr(GenericSelectionExprClass,
3082f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getType(),
3083f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getValueKind(),
3084f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getObjectKind(),
3085f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->isTypeDependent(),
3086f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->isValueDependent(),
3087561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         AssocExprs[ResultIndex]->isInstantiationDependent(),
3088f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         ContainsUnexpandedParameterPack),
3089f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3090f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3091f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3092f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    RParenLoc(RParenLoc) {
3093f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  SubExprs[CONTROLLING] = ControllingExpr;
3094f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3095f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3096f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne}
3097f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
3098f111d935722ed488144600cea5ed03a6b5069e8fPeter CollingbourneGenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3099f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation GenericLoc, Expr *ControllingExpr,
3100f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3101f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned NumAssocs, SourceLocation DefaultLoc,
3102f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation RParenLoc,
3103f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               bool ContainsUnexpandedParameterPack)
3104f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  : Expr(GenericSelectionExprClass,
3105f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         Context.DependentTy,
3106f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         VK_RValue,
3107f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         OK_Ordinary,
3108561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isTypeDependent=*/true,
3109561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isValueDependent=*/true,
3110561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isInstantiationDependent=*/true,
3111f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         ContainsUnexpandedParameterPack),
3112f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3113f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3114f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3115f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    RParenLoc(RParenLoc) {
3116f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  SubExprs[CONTROLLING] = ControllingExpr;
3117f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3118f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3119f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne}
3120f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
312177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
312205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//  DesignatedInitExpr
312305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
312405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3125b11382497a923b0d7009e85a1d8eb7bf93ec6d0dChandler CarruthIdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
312605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(Kind == FieldDesignator && "Only valid on a field designator");
312705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Field.NameOrField & 0x01)
312805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
312905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  else
313005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return getField()->getIdentifier();
313105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
313205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3133c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntDesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
3134319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                       unsigned NumDesignators,
3135ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       const Designator *Designators,
31361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       SourceLocation EqualOrColonLoc,
3137ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       bool GNUSyntax,
31381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       Expr **IndexExprs,
31399ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                       unsigned NumIndexExprs,
31409ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                       Expr *Init)
31411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(DesignatedInitExprClass, Ty,
3142f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall         Init->getValueKind(), Init->getObjectKind(),
3143bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Init->isTypeDependent(), Init->isValueDependent(),
3144561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Init->isInstantiationDependent(),
3145bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Init->containsUnexpandedParameterPack()),
31461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
31471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
3148319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  this->Designators = new (C) Designator[NumDesignators];
31499ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
31509ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Record the initializer itself.
31517502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  child_range Child = children();
31529ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  *Child++ = Init;
31539ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
31549ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Copy the designators and their subexpressions, computing
31559ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // value-dependence along the way.
31569ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  unsigned IndexIdx = 0;
31579ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  for (unsigned I = 0; I != NumDesignators; ++I) {
3158ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    this->Designators[I] = Designators[I];
31599ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
31609ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    if (this->Designators[I].isArrayDesignator()) {
31619ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
31629ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Index = IndexExprs[IndexIdx];
3163bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Index->isTypeDependent() || Index->isValueDependent())
3164bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ValueDependent = true;
3165561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      if (Index->isInstantiationDependent())
3166561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3167bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      // Propagate unexpanded parameter packs.
3168bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Index->containsUnexpandedParameterPack())
3169bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ContainsUnexpandedParameterPack = true;
31709ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
31719ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the index expressions into permanent storage.
31729ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
31739ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    } else if (this->Designators[I].isArrayRangeDesignator()) {
31749ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
31759ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Start = IndexExprs[IndexIdx];
31769ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *End = IndexExprs[IndexIdx + 1];
3177bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Start->isTypeDependent() || Start->isValueDependent() ||
3178561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          End->isTypeDependent() || End->isValueDependent()) {
3179bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ValueDependent = true;
3180561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3181561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      } else if (Start->isInstantiationDependent() ||
3182561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                 End->isInstantiationDependent()) {
3183561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3184561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      }
3185561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
3186bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      // Propagate unexpanded parameter packs.
3187bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Start->containsUnexpandedParameterPack() ||
3188bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          End->containsUnexpandedParameterPack())
3189bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ContainsUnexpandedParameterPack = true;
31909ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
31919ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the start/end expressions into permanent storage.
31929ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
31939ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
31949ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    }
31959ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  }
31969ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
31979ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
3198ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
3199ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
320005c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr *
32011eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
320205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           unsigned NumDesignators,
320305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           Expr **IndexExprs, unsigned NumIndexExprs,
320405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           SourceLocation ColonOrEqualLoc,
320505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           bool UsesColonSyntax, Expr *Init) {
3206c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3207c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3208319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
32099ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                      ColonOrEqualLoc, UsesColonSyntax,
32109ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                      IndexExprs, NumIndexExprs, Init);
321105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
321205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
32131eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
3214d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                                    unsigned NumIndexExprs) {
3215d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3216d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3217d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3218d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
3219d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
3220319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregorvoid DesignatedInitExpr::setDesignators(ASTContext &C,
3221319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                        const Designator *Desigs,
3222d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                        unsigned NumDesigs) {
3223319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  Designators = new (C) Designator[NumDesigs];
3224d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  NumDesignators = NumDesigs;
3225d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  for (unsigned I = 0; I != NumDesigs; ++I)
3226d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor    Designators[I] = Desigs[I];
3227d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
3228d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
322924f4674e697fb53587f0e8485e9c6592f7021ef2Abramo BagnaraSourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
323024f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
323124f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  if (size() == 1)
323224f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara    return DIE->getDesignator(0)->getSourceRange();
323324f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  return SourceRange(DIE->getDesignator(0)->getStartLocation(),
323424f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara                     DIE->getDesignator(size()-1)->getEndLocation());
323524f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara}
323624f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara
323705c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorSourceRange DesignatedInitExpr::getSourceRange() const {
323805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation StartLoc;
3239d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner  Designator &First =
3240d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    *const_cast<DesignatedInitExpr*>(this)->designators_begin();
324105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (First.isFieldDesignator()) {
3242eeae8f072748affce25ab4064982626361293390Douglas Gregor    if (GNUSyntax)
324305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
324405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else
324505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
324605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  } else
3247d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    StartLoc =
3248d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner      SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
324905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
325005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
325105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
325205c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
325305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
325405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
325505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
325605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
325705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
325805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
325905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
326005c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
32611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
326205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
326305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
326405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
326505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
326605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
326705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
326805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
326905c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
32701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
327105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
327205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
327305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
327405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
327505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
327605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
327705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3278ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// \brief Replaces the designator at index @p Idx with the series
3279ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// of designators in [First, Last).
3280319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregorvoid DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
32811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          const Designator *First,
3282ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                          const Designator *Last) {
3283ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  unsigned NumNewDesignators = Last - First;
3284ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  if (NumNewDesignators == 0) {
3285ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    std::copy_backward(Designators + Idx + 1,
3286ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + NumDesignators,
3287ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + Idx);
3288ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    --NumNewDesignators;
3289ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
3290ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  } else if (NumNewDesignators == 1) {
3291ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    Designators[Idx] = *First;
3292ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
3293ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
3294ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
32951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Designator *NewDesignators
3296319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor    = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
3297ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators, Designators + Idx, NewDesignators);
3298ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(First, Last, NewDesignators + Idx);
3299ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators + Idx + 1, Designators + NumDesignators,
3300ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor            NewDesignators + Idx + NumNewDesignators);
3301ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  Designators = NewDesignators;
3302ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  NumDesignators = NumDesignators - 1 + NumNewDesignators;
3303ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
3304ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
33051eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
33062ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                             Expr **exprs, unsigned nexprs,
33070d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek                             SourceLocation rparenloc, QualType T)
33080d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek  : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
3309561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         false, false, false, false),
3310bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
33110d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek  assert(!T.isNull() && "ParenListExpr must have a valid type");
33122ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman  Exprs = new (C) Stmt*[nexprs];
3313bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  for (unsigned i = 0; i != nexprs; ++i) {
3314bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->isTypeDependent())
3315bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
3316bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->isValueDependent())
3317bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
3318561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (exprs[i]->isInstantiationDependent())
3319561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
3320bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->containsUnexpandedParameterPack())
3321bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
3322bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
33232ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    Exprs[i] = exprs[i];
3324bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
33252ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman}
33262ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman
3327e996ffd240f20a1048179d7727a6ee3227261921John McCallconst OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3328e996ffd240f20a1048179d7727a6ee3227261921John McCall  if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3329e996ffd240f20a1048179d7727a6ee3227261921John McCall    e = ewc->getSubExpr();
333003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
333103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    e = m->GetTemporaryExpr();
3332e996ffd240f20a1048179d7727a6ee3227261921John McCall  e = cast<CXXConstructExpr>(e)->getArg(0);
3333e996ffd240f20a1048179d7727a6ee3227261921John McCall  while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3334e996ffd240f20a1048179d7727a6ee3227261921John McCall    e = ice->getSubExpr();
3335e996ffd240f20a1048179d7727a6ee3227261921John McCall  return cast<OpaqueValueExpr>(e);
3336e996ffd240f20a1048179d7727a6ee3227261921John McCall}
3337e996ffd240f20a1048179d7727a6ee3227261921John McCall
33384b9c2d235fb9449e249d74f48ecfec601650de93John McCallPseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
33394b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                           unsigned numSemanticExprs) {
33404b9c2d235fb9449e249d74f48ecfec601650de93John McCall  void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
33414b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                    (1 + numSemanticExprs) * sizeof(Expr*),
33424b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                  llvm::alignOf<PseudoObjectExpr>());
33434b9c2d235fb9449e249d74f48ecfec601650de93John McCall  return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
33444b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
33454b9c2d235fb9449e249d74f48ecfec601650de93John McCall
33464b9c2d235fb9449e249d74f48ecfec601650de93John McCallPseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
33474b9c2d235fb9449e249d74f48ecfec601650de93John McCall  : Expr(PseudoObjectExprClass, shell) {
33484b9c2d235fb9449e249d74f48ecfec601650de93John McCall  PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
33494b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
33504b9c2d235fb9449e249d74f48ecfec601650de93John McCall
33514b9c2d235fb9449e249d74f48ecfec601650de93John McCallPseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
33524b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                           ArrayRef<Expr*> semantics,
33534b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                           unsigned resultIndex) {
33544b9c2d235fb9449e249d74f48ecfec601650de93John McCall  assert(syntax && "no syntactic expression!");
33554b9c2d235fb9449e249d74f48ecfec601650de93John McCall  assert(semantics.size() && "no semantic expressions!");
33564b9c2d235fb9449e249d74f48ecfec601650de93John McCall
33574b9c2d235fb9449e249d74f48ecfec601650de93John McCall  QualType type;
33584b9c2d235fb9449e249d74f48ecfec601650de93John McCall  ExprValueKind VK;
33594b9c2d235fb9449e249d74f48ecfec601650de93John McCall  if (resultIndex == NoResult) {
33604b9c2d235fb9449e249d74f48ecfec601650de93John McCall    type = C.VoidTy;
33614b9c2d235fb9449e249d74f48ecfec601650de93John McCall    VK = VK_RValue;
33624b9c2d235fb9449e249d74f48ecfec601650de93John McCall  } else {
33634b9c2d235fb9449e249d74f48ecfec601650de93John McCall    assert(resultIndex < semantics.size());
33644b9c2d235fb9449e249d74f48ecfec601650de93John McCall    type = semantics[resultIndex]->getType();
33654b9c2d235fb9449e249d74f48ecfec601650de93John McCall    VK = semantics[resultIndex]->getValueKind();
33664b9c2d235fb9449e249d74f48ecfec601650de93John McCall    assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
33674b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
33684b9c2d235fb9449e249d74f48ecfec601650de93John McCall
33694b9c2d235fb9449e249d74f48ecfec601650de93John McCall  void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
33704b9c2d235fb9449e249d74f48ecfec601650de93John McCall                              (1 + semantics.size()) * sizeof(Expr*),
33714b9c2d235fb9449e249d74f48ecfec601650de93John McCall                            llvm::alignOf<PseudoObjectExpr>());
33724b9c2d235fb9449e249d74f48ecfec601650de93John McCall  return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
33734b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                      resultIndex);
33744b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
33754b9c2d235fb9449e249d74f48ecfec601650de93John McCall
33764b9c2d235fb9449e249d74f48ecfec601650de93John McCallPseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
33774b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                   Expr *syntax, ArrayRef<Expr*> semantics,
33784b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                   unsigned resultIndex)
33794b9c2d235fb9449e249d74f48ecfec601650de93John McCall  : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
33804b9c2d235fb9449e249d74f48ecfec601650de93John McCall         /*filled in at end of ctor*/ false, false, false, false) {
33814b9c2d235fb9449e249d74f48ecfec601650de93John McCall  PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
33824b9c2d235fb9449e249d74f48ecfec601650de93John McCall  PseudoObjectExprBits.ResultIndex = resultIndex + 1;
33834b9c2d235fb9449e249d74f48ecfec601650de93John McCall
33844b9c2d235fb9449e249d74f48ecfec601650de93John McCall  for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
33854b9c2d235fb9449e249d74f48ecfec601650de93John McCall    Expr *E = (i == 0 ? syntax : semantics[i-1]);
33864b9c2d235fb9449e249d74f48ecfec601650de93John McCall    getSubExprsBuffer()[i] = E;
33874b9c2d235fb9449e249d74f48ecfec601650de93John McCall
33884b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->isTypeDependent())
33894b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.TypeDependent = true;
33904b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->isValueDependent())
33914b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.ValueDependent = true;
33924b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->isInstantiationDependent())
33934b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.InstantiationDependent = true;
33944b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->containsUnexpandedParameterPack())
33954b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.ContainsUnexpandedParameterPack = true;
33964b9c2d235fb9449e249d74f48ecfec601650de93John McCall
33974b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (isa<OpaqueValueExpr>(E))
33984b9c2d235fb9449e249d74f48ecfec601650de93John McCall      assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
33994b9c2d235fb9449e249d74f48ecfec601650de93John McCall             "opaque-value semantic expressions for pseudo-object "
34004b9c2d235fb9449e249d74f48ecfec601650de93John McCall             "operations must have sources");
34014b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
34024b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
34034b9c2d235fb9449e249d74f48ecfec601650de93John McCall
340405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
3405ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//  ExprIterator.
3406ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
3407ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
3408ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3409ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3410ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3411ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator[](size_t idx) const {
3412ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  return cast<Expr>(I[idx]);
3413ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek}
3414ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3415ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3416ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
3417ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
341877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//  Child Iterators for iterating over subexpressions/substatements
341977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
342077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
3421f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne// UnaryExprOrTypeTraitExpr
3422f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter CollingbourneStmt::child_range UnaryExprOrTypeTraitExpr::children() {
34230518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // If this is of a type and the type is a VLA type (and not a typedef), the
34240518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // size expression of the VLA needs to be treated as an executable expression.
34250518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // Why isn't this weirdness documented better in StmtIterator?
34260518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType()) {
3427f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
34280518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl                                   getArgumentType().getTypePtr()))
342963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      return child_range(child_iterator(T), child_iterator());
343063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return child_range();
34310518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  }
343263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return child_range(&Argument.Ex, &Argument.Ex + 1);
34339ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
3434390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
3435563477da25f824e37c535131695dc4dc9b68c465Steve Naroff// ObjCMessageExpr
343663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallStmt::child_range ObjCMessageExpr::children() {
343763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  Stmt **begin;
343804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (getReceiverKind() == Instance)
343963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    begin = reinterpret_cast<Stmt **>(this + 1);
344063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  else
344163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    begin = reinterpret_cast<Stmt **>(getArgs());
344263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return child_range(begin,
344363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                     reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
3444563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
3445563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
34464eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff// Blocks
34476b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallBlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
3448a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor                                   SourceLocation l, bool ByRef,
34496b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                                   bool constAdded)
3450561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
3451a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor         d->isParameterPack()),
34526b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
3453a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor{
3454d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool TypeDependent = false;
3455d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool ValueDependent = false;
3456561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  bool InstantiationDependent = false;
3457561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3458561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                           InstantiationDependent);
3459d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.TypeDependent = TypeDependent;
3460d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.ValueDependent = ValueDependent;
3461561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  ExprBits.InstantiationDependent = InstantiationDependent;
3462a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor}
3463dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman
3464dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman
3465dfa64ba45922e1c28e36341bdf34785fea74659bEli FriedmanAtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3466dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman                       QualType t, AtomicOp op, SourceLocation RP)
3467dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman  : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3468dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman         false, false, false, false),
3469dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3470dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman{
3471dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman  for (unsigned i = 0; i < nexpr; i++) {
3472dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isTypeDependent())
3473dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.TypeDependent = true;
3474dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isValueDependent())
3475dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.ValueDependent = true;
3476dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isInstantiationDependent())
3477dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.InstantiationDependent = true;
3478dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->containsUnexpandedParameterPack())
3479dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.ContainsUnexpandedParameterPack = true;
3480dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman
3481dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    SubExprs[i] = args[i];
3482dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman  }
3483dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman}
3484