Expr.cpp revision 21f77cd0c3da8a1dbaf6245cae43baf4c0b80ea4
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>
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
342b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// isKnownToHaveBooleanValue - Return true if this is an integer expression
352b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// that is known to return 0 or 1.  This happens for _Bool/bool expressions
362b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// but also int expressions which are produced by things like comparisons in
372b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// C.
382b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattnerbool Expr::isKnownToHaveBooleanValue() const {
39f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  const Expr *E = IgnoreParens();
40f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
412b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  // If this value has _Bool type, it is obvious 0/1.
42f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (E->getType()->isBooleanType()) return true;
43c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  // If this is a non-scalar-integer type, we don't care enough to try.
44f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (!E->getType()->isIntegralOrEnumerationType()) return false;
45c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
46f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
472b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    switch (UO->getOpcode()) {
482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Plus:
492b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return UO->getSubExpr()->isKnownToHaveBooleanValue();
502b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    default:
512b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return false;
522b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
532b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  }
54c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
556907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  // Only look through implicit casts.  If the user writes
566907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  // '(int) (a && b)' treat it as an arbitrary int.
57f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
582b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return CE->getSubExpr()->isKnownToHaveBooleanValue();
59c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
60f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
612b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    switch (BO->getOpcode()) {
622b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    default: return false;
632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:   // Relational operators.
642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:   // Equality operators.
682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LAnd: // AND operator.
702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LOr:  // Logical OR operator.
712b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return true;
72c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_And:  // Bitwise AND operator.
742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Xor:  // Bitwise XOR operator.
752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Or:   // Bitwise OR operator.
762b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      // Handle things like (x==2)|(y==12).
772b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return BO->getLHS()->isKnownToHaveBooleanValue() &&
782b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner             BO->getRHS()->isKnownToHaveBooleanValue();
79c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Comma:
812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Assign:
822b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return BO->getRHS()->isKnownToHaveBooleanValue();
832b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
842b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  }
85c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
86f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
872b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
882b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner           CO->getFalseExpr()->isKnownToHaveBooleanValue();
89c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
902b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  return false;
912b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner}
922b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner
9363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall// Amusing macro metaprogramming hack: check whether a class provides
9463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall// a more specific implementation of getExprLoc().
9563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallnamespace {
9663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// This implementation is used when a class provides a custom
9763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// implementation of getExprLoc.
9863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  template <class E, class T>
9963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  SourceLocation getExprLocImpl(const Expr *expr,
10063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                                SourceLocation (T::*v)() const) {
10163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return static_cast<const E*>(expr)->getExprLoc();
10263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
10363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
10463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// This implementation is used when a class doesn't provide
10563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// a custom implementation of getExprLoc.  Overload resolution
10663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// should pick it over the implementation above because it's
10763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// more specialized according to function template partial ordering.
10863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  template <class E>
10963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  SourceLocation getExprLocImpl(const Expr *expr,
11063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                                SourceLocation (Expr::*v)() const) {
11163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return static_cast<const E*>(expr)->getSourceRange().getBegin();
11263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
11363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall}
11463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
11563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallSourceLocation Expr::getExprLoc() const {
11663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  switch (getStmtClass()) {
11763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::NoStmtClass: llvm_unreachable("statement without class");
11863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(type)
11963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define STMT(type, base) \
12063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
12163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define EXPR(type, base) \
12263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
12363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#include "clang/AST/StmtNodes.inc"
12463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
12563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  llvm_unreachable("unknown statement kind");
12663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return SourceLocation();
12763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall}
12863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Expressions.
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
133561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor/// \brief Compute the type-, value-, and instantiation-dependence of a
134561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor/// declaration reference
135d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor/// based on the declaration being referenced.
136d967e31ee796efff24b84b704a063634f6b55627Douglas Gregorstatic void computeDeclRefDependence(NamedDecl *D, QualType T,
137d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor                                     bool &TypeDependent,
138561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                     bool &ValueDependent,
139561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                     bool &InstantiationDependent) {
140d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  TypeDependent = false;
141d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ValueDependent = false;
142561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  InstantiationDependent = false;
1430da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
1440da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  // (TD) C++ [temp.dep.expr]p3:
1450da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //   An id-expression is type-dependent if it contains:
1460da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //
147c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  // and
1480da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //
1490da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  // (VD) C++ [temp.dep.constexpr]p2:
1500da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  An identifier is value-dependent if it is:
151d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
1520da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - an identifier that was declared with dependent type
1530da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD)  - a name declared with a dependent type,
154d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (T->isDependentType()) {
155d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    TypeDependent = true;
156d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
157561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
158d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
159561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  } else if (T->isInstantiationDependentType()) {
160561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
1610da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
162d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
1630da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - a conversion-function-id that specifies a dependent type
164d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (D->getDeclName().getNameKind()
165561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                == DeclarationName::CXXConversionFunctionName) {
166561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    QualType T = D->getDeclName().getCXXNameType();
167561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (T->isDependentType()) {
168561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      TypeDependent = true;
169561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ValueDependent = true;
170561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
171561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      return;
172561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
173561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
174561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (T->isInstantiationDependentType())
175561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
1760da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
177561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
1780da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD)  - the name of a non-type template parameter,
179d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (isa<NonTypeTemplateParmDecl>(D)) {
180d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
181561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
182d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
183d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
184d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
1850da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD) - a constant with integral or enumeration type and is
1860da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //         initialized with an expression that is value-dependent.
187d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1882ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    if (Var->getType()->isIntegralOrEnumerationType() &&
189501edb6a54524555ad27fbf41a7920dc756b08c6Douglas Gregor        Var->getType().getCVRQualifiers() == Qualifiers::Const) {
19031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      if (const Expr *Init = Var->getAnyInitializer())
191561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        if (Init->isValueDependent()) {
192d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor          ValueDependent = true;
193561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          InstantiationDependent = true;
194561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        }
195bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    }
196d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
197bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    // (VD) - FIXME: Missing from the standard:
198bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    //      -  a member function or a static data member of the current
199bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    //         instantiation
200bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    else if (Var->isStaticDataMember() &&
201561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor             Var->getDeclContext()->isDependentContext()) {
202d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor      ValueDependent = true;
203561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
204561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
205d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
206d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
207d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
208d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
209bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  // (VD) - FIXME: Missing from the standard:
210bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  //      -  a member function or a static data member of the current
211bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  //         instantiation
212d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
213d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
214561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
215d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
216d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
217d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor}
218d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
219d967e31ee796efff24b84b704a063634f6b55627Douglas Gregorvoid DeclRefExpr::computeDependence() {
220d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool TypeDependent = false;
221d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool ValueDependent = false;
222561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  bool InstantiationDependent = false;
223561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
224561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                           InstantiationDependent);
225d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
226d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  // (TD) C++ [temp.dep.expr]p3:
227d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //   An id-expression is type-dependent if it contains:
228d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //
229d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  // and
230d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //
231d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  // (VD) C++ [temp.dep.constexpr]p2:
232d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //  An identifier is value-dependent if it is:
233d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (!TypeDependent && !ValueDependent &&
234d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor      hasExplicitTemplateArgs() &&
235d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor      TemplateSpecializationType::anyDependentTemplateArguments(
236d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor                                                            getTemplateArgs(),
237561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                                       getNumTemplateArgs(),
238561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                                      InstantiationDependent)) {
239d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    TypeDependent = true;
240d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
241561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
242d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
243d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
244d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.TypeDependent = TypeDependent;
245d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.ValueDependent = ValueDependent;
246561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  ExprBits.InstantiationDependent = InstantiationDependent;
247d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
24810738d36b150aa65206890c1c845cdba076e4200Douglas Gregor  // Is the declaration a parameter pack?
249d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (getDecl()->isParameterPack())
2501fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor    ExprBits.ContainsUnexpandedParameterPack = true;
2510da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor}
2520da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
2533aa8140bde5b9bedf13e46ec0a668daa54814196Chandler CarruthDeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
2542577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                         ValueDecl *D, const DeclarationNameInfo &NameInfo,
2553aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                         NamedDecl *FoundD,
2562577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                         const TemplateArgumentListInfo *TemplateArgs,
257f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                         QualType T, ExprValueKind VK)
258561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
259cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth    D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
260cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth  DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
2617e740bd36772aae16b5cc5e605998ccc5eaf26dbChandler Carruth  if (QualifierLoc)
2626857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    getInternalQualifierLoc() = QualifierLoc;
2633aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
2643aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (FoundD)
2653aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    getInternalFoundDecl() = FoundD;
266cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth  DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
267561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (TemplateArgs) {
268561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool Dependent = false;
269561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool InstantiationDependent = false;
270561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool ContainsUnexpandedParameterPack = false;
271561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
272561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                             InstantiationDependent,
273561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                             ContainsUnexpandedParameterPack);
274561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (InstantiationDependent)
275561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      setInstantiationDependent(true);
276561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
277b8da98a2182d2c69c72136b9a9ebabd4694f3d6bBenjamin Kramer  DeclRefExprBits.HadMultipleCandidates = 0;
278b8da98a2182d2c69c72136b9a9ebabd4694f3d6bBenjamin Kramer
2792577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  computeDependence();
2802577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
2812577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
282a2813cec2605ce7878d1b13471d685f689b251afDouglas GregorDeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
28340d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                                 NestedNameSpecifierLoc QualifierLoc,
284dbd872f273a8dbf22e089b3def6c09f0a460965dJohn McCall                                 ValueDecl *D,
285a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor                                 SourceLocation NameLoc,
2860da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                 QualType T,
287f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
2883aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                 NamedDecl *FoundD,
2890da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                 const TemplateArgumentListInfo *TemplateArgs) {
29040d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor  return Create(Context, QualifierLoc, D,
2912577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                DeclarationNameInfo(D->getDeclName(), NameLoc),
2923aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                T, VK, FoundD, TemplateArgs);
2932577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
2942577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2952577743c5650c646fb705df01403707e94f2df04Abramo BagnaraDeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
29640d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                                 NestedNameSpecifierLoc QualifierLoc,
2972577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 ValueDecl *D,
2982577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 const DeclarationNameInfo &NameInfo,
2992577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 QualType T,
300f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
3013aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                 NamedDecl *FoundD,
3022577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 const TemplateArgumentListInfo *TemplateArgs) {
3033aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  // Filter out cases where the found Decl is the same as the value refenenced.
3043aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (D == FoundD)
3053aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    FoundD = 0;
3063aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
307a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  std::size_t Size = sizeof(DeclRefExpr);
30840d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor  if (QualifierLoc != 0)
3096857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    Size += sizeof(NestedNameSpecifierLoc);
3103aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (FoundD)
3113aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    Size += sizeof(NamedDecl *);
312d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (TemplateArgs)
313b0c3e0909bb04af0bfb82ad01ab6909649d68ccaArgyrios Kyrtzidis    Size += ASTTemplateArgumentListInfo::sizeFor(*TemplateArgs);
3143aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
3153248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
3163aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
3173aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                               T, VK);
318a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor}
319a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
3203aa8140bde5b9bedf13e46ec0a668daa54814196Chandler CarruthDeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
321def0354384d9c4431f7b58b664b59896d4623028Douglas Gregor                                      bool HasQualifier,
3223aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                      bool HasFoundDecl,
323def0354384d9c4431f7b58b664b59896d4623028Douglas Gregor                                      bool HasExplicitTemplateArgs,
324663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis                                      unsigned NumTemplateArgs) {
325663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  std::size_t Size = sizeof(DeclRefExpr);
326663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  if (HasQualifier)
3276857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    Size += sizeof(NestedNameSpecifierLoc);
3283aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (HasFoundDecl)
3293aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    Size += sizeof(NamedDecl *);
330def0354384d9c4431f7b58b664b59896d4623028Douglas Gregor  if (HasExplicitTemplateArgs)
331b0c3e0909bb04af0bfb82ad01ab6909649d68ccaArgyrios Kyrtzidis    Size += ASTTemplateArgumentListInfo::sizeFor(NumTemplateArgs);
3323aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
3333248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
334663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  return new (Mem) DeclRefExpr(EmptyShell());
335663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis}
336663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis
337a2813cec2605ce7878d1b13471d685f689b251afDouglas GregorSourceRange DeclRefExpr::getSourceRange() const {
3382577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  SourceRange R = getNameInfo().getSourceRange();
339a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  if (hasQualifier())
34040d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    R.setBegin(getQualifierLoc().getBeginLoc());
341096832c5ed5b9106fa177ebc148489760c3bc496John McCall  if (hasExplicitTemplateArgs())
342a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor    R.setEnd(getRAngleLoc());
343a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  return R;
344a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor}
345a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
3463a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson// FIXME: Maybe this should use DeclPrinter with a special "print predefined
3473a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson// expr" policy instead.
348848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlssonstd::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
349848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson  ASTContext &Context = CurrentDecl->getASTContext();
350848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson
3513a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
352848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson    if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
3533a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      return FD->getNameAsString();
3543a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3553a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::SmallString<256> Name;
3563a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::raw_svector_ostream Out(Name);
3573a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3583a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
359848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson      if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
3603a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        Out << "virtual ";
3614eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (MD->isStatic())
3624eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Out << "static ";
3633a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    }
3643a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3653a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    PrintingPolicy Policy(Context.getLangOptions());
3663a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3673a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    std::string Proto = FD->getQualifiedNameAsString(Policy);
3683a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
369183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
3703a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    const FunctionProtoType *FT = 0;
3713a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (FD->hasWrittenPrototype())
3723a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      FT = dyn_cast<FunctionProtoType>(AFT);
3733a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3743a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Proto += "(";
3753a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (FT) {
3763a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      llvm::raw_string_ostream POut(Proto);
3773a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
3783a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        if (i) POut << ", ";
3793a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        std::string Param;
3803a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
3813a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        POut << Param;
3823a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      }
3833a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3843a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      if (FT->isVariadic()) {
3853a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        if (FD->getNumParams()) POut << ", ";
3863a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        POut << "...";
3873a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      }
3883a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    }
3893a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Proto += ")";
3903a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3914eadcc569223135e13353c9381b448986e3f7053Sam Weinig    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
3924eadcc569223135e13353c9381b448986e3f7053Sam Weinig      Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
3934eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (ThisQuals.hasConst())
3944eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Proto += " const";
3954eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (ThisQuals.hasVolatile())
3964eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Proto += " volatile";
3974eadcc569223135e13353c9381b448986e3f7053Sam Weinig    }
3984eadcc569223135e13353c9381b448986e3f7053Sam Weinig
3993a1ce1ed0f5686384e712837bad28c576622e442Sam Weinig    if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
4003a1ce1ed0f5686384e712837bad28c576622e442Sam Weinig      AFT->getResultType().getAsStringInternal(Proto, Policy);
4013a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4023a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << Proto;
4033a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4043a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out.flush();
4053a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return Name.str().str();
4063a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
4073a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
4083a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::SmallString<256> Name;
4093a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::raw_svector_ostream Out(Name);
4103a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << (MD->isInstanceMethod() ? '-' : '+');
4113a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << '[';
412b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek
413b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    // For incorrect code, there might not be an ObjCInterfaceDecl.  Do
414b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    // a null check to avoid a crash.
415b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
416b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer      Out << *ID;
417b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek
4183a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (const ObjCCategoryImplDecl *CID =
419900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer        dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
420900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer      Out << '(' << CID << ')';
421900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer
4223a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out <<  ' ';
4233a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << MD->getSelector().getAsString();
4243a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out <<  ']';
4253a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4263a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out.flush();
4273a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return Name.str().str();
4283a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
4293a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
4303a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
4313a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return "top level";
4323a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
4333a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return "";
4343a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
4353a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4369996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidisvoid APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
4379996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  if (hasAllocation())
4389996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    C.Deallocate(pVal);
4399996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4409996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  BitWidth = Val.getBitWidth();
4419996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  unsigned NumWords = Val.getNumWords();
4429996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  const uint64_t* Words = Val.getRawData();
4439996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  if (NumWords > 1) {
4449996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    pVal = new (C) uint64_t[NumWords];
4459996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    std::copy(Words, Words + NumWords, pVal);
4469996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  } else if (NumWords == 1)
4479996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    VAL = Words[0];
4489996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  else
4499996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    VAL = 0;
4509996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4519996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4529996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral *
4539996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
4549996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                       QualType type, SourceLocation l) {
4559996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) IntegerLiteral(C, V, type, l);
4569996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4579996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4589996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral *
4599996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
4609996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) IntegerLiteral(Empty);
4619996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4629996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4639996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral *
4649996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
4659996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                        bool isexact, QualType Type, SourceLocation L) {
4669996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) FloatingLiteral(C, V, isexact, Type, L);
4679996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4689996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
4699996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral *
4709996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
4719996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) FloatingLiteral(Empty);
4729996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
4739996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
474da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// getValueAsApproximateDouble - This returns the value as an inaccurate
475da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// double.  Note that this may cause loss of precision, but is useful for
476da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// debugging dumps, etc.
477da8249e57f3badecf925571881fe57243935c6c1Chris Lattnerdouble FloatingLiteral::getValueAsApproximateDouble() const {
478da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  llvm::APFloat V = getValue();
479ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  bool ignored;
480ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
481ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen            &ignored);
482da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  return V.convertToDouble();
483da8249e57f3badecf925571881fe57243935c6c1Chris Lattner}
484da8249e57f3badecf925571881fe57243935c6c1Chris Lattner
4855f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
4865cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                     StringKind Kind, bool Pascal, QualType Ty,
4871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     const SourceLocation *Loc,
488a135fb43eb94524a6529768596a4533eed9aa70dAnders Carlsson                                     unsigned NumStrs) {
4892085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // Allocate enough space for the StringLiteral plus an array of locations for
4902085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // any concatenated string tokens.
4912085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  void *Mem = C.Allocate(sizeof(StringLiteral)+
4922085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                         sizeof(SourceLocation)*(NumStrs-1),
4933248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner                         llvm::alignOf<StringLiteral>());
4942085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  StringLiteral *SL = new (Mem) StringLiteral(Ty);
4951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // OPTIMIZE: could allocate this appended to the StringLiteral.
49765aa6885818d4b4eea2e5a9d12085b2398148662Jay Foad  char *AStrData = new (C, 1) char[Str.size()];
49865aa6885818d4b4eea2e5a9d12085b2398148662Jay Foad  memcpy(AStrData, Str.data(), Str.size());
4992085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->StrData = AStrData;
50065aa6885818d4b4eea2e5a9d12085b2398148662Jay Foad  SL->ByteLength = Str.size();
5015cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  SL->Kind = Kind;
5023e2193ce5feb2feb092e5ae615e85148e06e9fd2Anders Carlsson  SL->IsPascal = Pascal;
5032085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->TokLocs[0] = Loc[0];
5042085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->NumConcatenated = NumStrs;
5055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
506726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner  if (NumStrs != 1)
5072085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner    memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
5082085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  return SL;
509726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner}
510726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner
511673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas GregorStringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
512673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  void *Mem = C.Allocate(sizeof(StringLiteral)+
513673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor                         sizeof(SourceLocation)*(NumStrs-1),
5143248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner                         llvm::alignOf<StringLiteral>());
515673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  StringLiteral *SL = new (Mem) StringLiteral(QualType());
516673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  SL->StrData = 0;
517673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  SL->ByteLength = 0;
518673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  SL->NumConcatenated = NumStrs;
519673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  return SL;
520673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor}
521673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor
5225f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid StringLiteral::setString(ASTContext &C, StringRef Str) {
523b648023da23e8b227cdda57a241db4c6f368726bDaniel Dunbar  char *AStrData = new (C, 1) char[Str.size()];
524b648023da23e8b227cdda57a241db4c6f368726bDaniel Dunbar  memcpy(AStrData, Str.data(), Str.size());
525673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  StrData = AStrData;
526b648023da23e8b227cdda57a241db4c6f368726bDaniel Dunbar  ByteLength = Str.size();
527673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor}
528673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor
52908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// getLocationOfByte - Return a source location that points to the specified
53008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// byte of this string literal.
53108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner///
53208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// Strings are amazingly complex.  They can be formed from multiple tokens and
53308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// can have escape sequences in them in addition to the usual trigraph and
53408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// escaped newline business.  This routine handles this complexity.
53508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner///
53608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris LattnerSourceLocation StringLiteral::
53708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris LattnergetLocationOfByte(unsigned ByteNo, const SourceManager &SM,
53808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner                  const LangOptions &Features, const TargetInfo &Target) const {
5395cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
5405cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
54108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  // Loop over all of the tokens in this string until we find the one that
54208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  // contains the byte we're looking for.
54308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  unsigned TokNo = 0;
54408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  while (1) {
54508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    assert(TokNo < getNumConcatenated() && "Invalid byte number!");
54608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
54708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
54808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Get the spelling of the string so that we can get the data that makes up
54908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // the string literal, not the identifier for the macro it is potentially
55008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // expanded through.
55108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
55208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
55308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Re-lex the token to get its length and original spelling.
55408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
55508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    bool Invalid = false;
5565f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
55708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    if (Invalid)
55808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      return StrTokSpellingLoc;
55908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
56008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    const char *StrData = Buffer.data()+LocInfo.second;
56108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
56208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Create a langops struct and enable trigraphs.  This is sufficient for
56308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // relexing tokens.
56408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    LangOptions LangOpts;
56508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    LangOpts.Trigraphs = true;
56608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
56708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Create a lexer starting at the beginning of this token.
56808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
56908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner                   Buffer.end());
57008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    Token TheTok;
57108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    TheLexer.LexFromRawLexer(TheTok);
57208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
57308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Use the StringLiteralParser to compute the length of the string in bytes.
57408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
57508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    unsigned TokNumBytes = SLP.GetStringLength();
57608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
57708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // If the byte is in this token, return the location of the byte.
57808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    if (ByteNo < TokNumBytes ||
579935a70c1e76d78985f20d422940280161b941299Hans Wennborg        (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
58008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
58108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
58208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      // Now that we know the offset of the token in the spelling, use the
58308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      // preprocessor to get the offset in the original source.
58408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
58508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    }
58608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
58708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Move to the next string token.
58808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    ++TokNo;
58908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    ByteNo -= TokNumBytes;
59008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  }
59108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner}
59208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
59308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
59408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "sizeof" or "[pre]++".
5975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *UnaryOperator::getOpcodeStr(Opcode Op) {
5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
599b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Unknown unary operator");
6002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostInc: return "++";
6012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostDec: return "--";
6022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PreInc:  return "++";
6032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PreDec:  return "--";
6042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_AddrOf:  return "&";
6052de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Deref:   return "*";
6062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:    return "+";
6072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus:   return "-";
6082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Not:     return "~";
6092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_LNot:    return "!";
6102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Real:    return "__real";
6112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Imag:    return "__imag";
6122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Extension: return "__extension__";
6135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCallUnaryOperatorKind
617bc736fceca6f0bca31d16003a7587857190408fbDouglas GregorUnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
618bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  switch (OO) {
619b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("No unary operator for overloaded function");
6202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PlusPlus:   return Postfix ? UO_PostInc : UO_PreInc;
6212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
6222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Amp:        return UO_AddrOf;
6232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Star:       return UO_Deref;
6242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Plus:       return UO_Plus;
6252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Minus:      return UO_Minus;
6262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Tilde:      return UO_Not;
6272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Exclaim:    return UO_LNot;
628bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  }
629bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor}
630bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
631bc736fceca6f0bca31d16003a7587857190408fbDouglas GregorOverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
632bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  switch (Opc) {
6332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
6342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
6352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_AddrOf: return OO_Amp;
6362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Deref: return OO_Star;
6372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus: return OO_Plus;
6382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus: return OO_Minus;
6392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Not: return OO_Tilde;
6402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_LNot: return OO_Exclaim;
641bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  default: return OO_None;
642bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  }
643bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor}
644bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
645bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
6465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
6475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Postfix Operators.
6485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
6495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
650cc324ad80ab940efca006b0064f7ca70a6181816Peter CollingbourneCallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
651cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne                   Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
652f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                   SourceLocation rparenloc)
653f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(SC, t, VK, OK_Ordinary,
654bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isTypeDependent(),
655bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isValueDependent(),
656561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         fn->isInstantiationDependent(),
657bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->containsUnexpandedParameterPack()),
658898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    NumArgs(numargs) {
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
660cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
661b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  SubExprs[FN] = fn;
662bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  for (unsigned i = 0; i != numargs; ++i) {
663bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
664bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
665bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
666bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
667561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
668561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
669bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
670bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
671bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
672cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
673bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
674668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
675cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = NumPreArgs;
676b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  RParenLoc = rparenloc;
677b4609806e9232593ece09ce08b630836e825865cDouglas Gregor}
678e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman
679668bf91d31265b6ea8c3eb854ba450857701f269Ted KremenekCallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
680f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                   QualType t, ExprValueKind VK, SourceLocation rparenloc)
681f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(CallExprClass, t, VK, OK_Ordinary,
682bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isTypeDependent(),
683bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isValueDependent(),
684561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         fn->isInstantiationDependent(),
685bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->containsUnexpandedParameterPack()),
686898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    NumArgs(numargs) {
687668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
688cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[numargs+PREARGS_START];
68977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek  SubExprs[FN] = fn;
690bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  for (unsigned i = 0; i != numargs; ++i) {
691bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
692bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
693bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
694bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
695561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
696561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
697bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
698bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
699bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
700cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    SubExprs[i+PREARGS_START] = args[i];
701bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
702668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
703cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = 0;
7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  RParenLoc = rparenloc;
7055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
7081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
709bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  // FIXME: Why do we allocate this?
710cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[PREARGS_START];
711cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = 0;
712cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne}
713cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne
714cc324ad80ab940efca006b0064f7ca70a6181816Peter CollingbourneCallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
715cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne                   EmptyShell Empty)
716cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
717cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  // FIXME: Why do we allocate this?
718cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
719cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = NumPreArgs;
7201f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor}
7211f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor
722d20254f2875d0004c57ee766f258dbcee29f4841Nuno LopesDecl *CallExpr::getCalleeDecl() {
723e8683d6d389def2fce229325435ba3c2b3f75492John McCall  Expr *CEE = getCallee()->IgnoreParenImpCasts();
7241ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor
7251ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor  while (SubstNonTypeTemplateParmExpr *NTTP
7261ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor                                = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
7271ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor    CEE = NTTP->getReplacement()->IgnoreParenCasts();
7281ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor  }
7291ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor
730200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  // If we're calling a dereference, look at the pointer instead.
731200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
732200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl    if (BO->isPtrMemOp())
733200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl      CEE = BO->getRHS()->IgnoreParenCasts();
734200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
735200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl    if (UO->getOpcode() == UO_Deref)
736200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl      CEE = UO->getSubExpr()->IgnoreParenCasts();
737200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  }
7386346f963145ed18b6edf50a78753b47db505e912Chris Lattner  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
739d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    return DRE->getDecl();
740cb1c77f90d4e747b83a0d0cc125dc01567378f82Nuno Lopes  if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
741cb1c77f90d4e747b83a0d0cc125dc01567378f82Nuno Lopes    return ME->getMemberDecl();
742a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu
743a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu  return 0;
744a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu}
745a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu
746d20254f2875d0004c57ee766f258dbcee29f4841Nuno LopesFunctionDecl *CallExpr::getDirectCallee() {
747caabf9bf331156e96dacb072385901fdfa057ec1Chris Lattner  return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
748d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes}
749d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes
750d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// setNumArgs - This changes the number of arguments present in this call.
751d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// Any orphaned expressions are deleted by this, and any new operands are set
752d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// to null.
7538189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenekvoid CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
754d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // No change, just return.
755d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs == getNumArgs()) return;
7561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
757d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // If shrinking # arguments, just delete the extras and forgot them.
758d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs < getNumArgs()) {
759d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    this->NumArgs = NumArgs;
760d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    return;
761d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  }
762d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
763d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Otherwise, we are growing the # arguments.  New an bigger argument array.
764cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  unsigned NumPreArgs = getNumPreArgs();
765cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
766d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Copy over args.
767cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
768d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = SubExprs[i];
769d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Null out new args.
770cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
771cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne       i != NumArgs+PREARGS_START+NumPreArgs; ++i)
772d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = 0;
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77488c9a46f0b84f1ee83e01917825346551ee540d0Douglas Gregor  if (SubExprs) C.Deallocate(SubExprs);
775d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  SubExprs = NewSubExprs;
776d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  this->NumArgs = NumArgs;
777d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner}
778d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
779cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// isBuiltinCall - If this is a call to a builtin, return the builtin ID.  If
780cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// not, return 0.
7814ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
782c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // All simple function calls (e.g. func()) are implicitly cast to pointer to
7831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // function. As a result, we try and obtain the DeclRefExpr from the
784c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // ImplicitCastExpr.
785c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
786c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
787cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
7881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
789c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
790c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!DRE)
791cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
7921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
793bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
794bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  if (!FDecl)
795cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
7961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7974fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor  if (!FDecl->getIdentifier())
7984fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor    return 0;
7994fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor
8007814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  return FDecl->getBuiltinID();
801cb888967400a03504c88acedd5248d6778a82f46Chris Lattner}
802bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson
8036dde78f744382a5627a04f984a97049e0c4b5e73Anders CarlssonQualType CallExpr::getCallReturnType() const {
8046dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson  QualType CalleeType = getCallee()->getType();
8056217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
8066dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson    CalleeType = FnTypePtr->getPointeeType();
8076217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
8086dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson    CalleeType = BPT->getPointeeType();
809864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
810864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    // This should never be overloaded and so should never return null.
811864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    CalleeType = Expr::findBoundMemberType(getCallee());
8125291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
813864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  const FunctionType *FnType = CalleeType->castAs<FunctionType>();
8146dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson  return FnType->getResultType();
8156dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson}
816cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
8172882eca5a184c78f793188083f6ce539740a5cf2John McCallSourceRange CallExpr::getSourceRange() const {
8182882eca5a184c78f793188083f6ce539740a5cf2John McCall  if (isa<CXXOperatorCallExpr>(this))
8192882eca5a184c78f793188083f6ce539740a5cf2John McCall    return cast<CXXOperatorCallExpr>(this)->getSourceRange();
8202882eca5a184c78f793188083f6ce539740a5cf2John McCall
8212882eca5a184c78f793188083f6ce539740a5cf2John McCall  SourceLocation begin = getCallee()->getLocStart();
8222882eca5a184c78f793188083f6ce539740a5cf2John McCall  if (begin.isInvalid() && getNumArgs() > 0)
8232882eca5a184c78f793188083f6ce539740a5cf2John McCall    begin = getArg(0)->getLocStart();
8242882eca5a184c78f793188083f6ce539740a5cf2John McCall  SourceLocation end = getRParenLoc();
8252882eca5a184c78f793188083f6ce539740a5cf2John McCall  if (end.isInvalid() && getNumArgs() > 0)
8262882eca5a184c78f793188083f6ce539740a5cf2John McCall    end = getArg(getNumArgs() - 1)->getLocEnd();
8272882eca5a184c78f793188083f6ce539740a5cf2John McCall  return SourceRange(begin, end);
8282882eca5a184c78f793188083f6ce539740a5cf2John McCall}
8292882eca5a184c78f793188083f6ce539740a5cf2John McCall
830c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntOffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
8318ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   SourceLocation OperatorLoc,
832c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                   TypeSourceInfo *tsi,
833c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                   OffsetOfNode* compsPtr, unsigned numComps,
8348ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   Expr** exprsPtr, unsigned numExprs,
8358ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   SourceLocation RParenLoc) {
8368ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
837c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                         sizeof(OffsetOfNode) * numComps +
8388ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(Expr*) * numExprs);
8398ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
8408ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
8418ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                exprsPtr, numExprs, RParenLoc);
8428ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
8438ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
8448ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas GregorOffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
8458ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                        unsigned numComps, unsigned numExprs) {
8468ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
8478ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(OffsetOfNode) * numComps +
8488ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(Expr*) * numExprs);
8498ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return new (Mem) OffsetOfExpr(numComps, numExprs);
8508ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
8518ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
852c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntOffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
8538ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           SourceLocation OperatorLoc, TypeSourceInfo *tsi,
854c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                           OffsetOfNode* compsPtr, unsigned numComps,
8558ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           Expr** exprsPtr, unsigned numExprs,
8568ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           SourceLocation RParenLoc)
857f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
858f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall         /*TypeDependent=*/false,
859bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*ValueDependent=*/tsi->getType()->isDependentType(),
860561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         tsi->getType()->isInstantiationDependentType(),
861bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         tsi->getType()->containsUnexpandedParameterPack()),
862c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
863c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    NumComps(numComps), NumExprs(numExprs)
8648ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor{
8658ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  for(unsigned i = 0; i < numComps; ++i) {
8668ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    setComponent(i, compsPtr[i]);
8678ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
868c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
8698ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  for(unsigned i = 0; i < numExprs; ++i) {
870bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
871bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
872bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprsPtr[i]->containsUnexpandedParameterPack())
873bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
874bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
8758ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    setIndexExpr(i, exprsPtr[i]);
8768ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
8778ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
8788ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
8798ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas GregorIdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
8808ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  assert(getKind() == Field || getKind() == Identifier);
8818ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  if (getKind() == Field)
8828ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    return getField()->getIdentifier();
883c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
8848ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
8858ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
8868ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpMemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
88840d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                               NestedNameSpecifierLoc QualifierLoc,
889f595cc41c4d95fe323f8a2b209523de9956f874dEli Friedman                               ValueDecl *memberdecl,
890161755a09898c95d21bfff33707da9ca41cd53c5John McCall                               DeclAccessPair founddecl,
8912577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                               DeclarationNameInfo nameinfo,
892d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                               const TemplateArgumentListInfo *targs,
893f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                               QualType ty,
894f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                               ExprValueKind vk,
895f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                               ExprObjectKind ok) {
89683f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  std::size_t Size = sizeof(MemberExpr);
8976bb8017bb9e828d118e15e59d71c66bba323c364John McCall
89840d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor  bool hasQualOrFound = (QualifierLoc ||
899161755a09898c95d21bfff33707da9ca41cd53c5John McCall                         founddecl.getDecl() != memberdecl ||
900161755a09898c95d21bfff33707da9ca41cd53c5John McCall                         founddecl.getAccess() != memberdecl->getAccess());
9016bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (hasQualOrFound)
9026bb8017bb9e828d118e15e59d71c66bba323c364John McCall    Size += sizeof(MemberNameQualifier);
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
904d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (targs)
905b0c3e0909bb04af0bfb82ad01ab6909649d68ccaArgyrios Kyrtzidis    Size += ASTTemplateArgumentListInfo::sizeFor(*targs);
9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9073248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
908f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
909f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                       ty, vk, ok);
9106bb8017bb9e828d118e15e59d71c66bba323c364John McCall
9116bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (hasQualOrFound) {
91240d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    // FIXME: Wrong. We should be looking at the member declaration we found.
91340d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
9146bb8017bb9e828d118e15e59d71c66bba323c364John McCall      E->setValueDependent(true);
9156bb8017bb9e828d118e15e59d71c66bba323c364John McCall      E->setTypeDependent(true);
916561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
917561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
918561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    else if (QualifierLoc &&
919561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor             QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
920561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
921561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
9226bb8017bb9e828d118e15e59d71c66bba323c364John McCall    E->HasQualifierOrFoundDecl = true;
9236bb8017bb9e828d118e15e59d71c66bba323c364John McCall
9246bb8017bb9e828d118e15e59d71c66bba323c364John McCall    MemberNameQualifier *NQ = E->getMemberQualifier();
92540d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    NQ->QualifierLoc = QualifierLoc;
9266bb8017bb9e828d118e15e59d71c66bba323c364John McCall    NQ->FoundDecl = founddecl;
9276bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
9286bb8017bb9e828d118e15e59d71c66bba323c364John McCall
9296bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (targs) {
930561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool Dependent = false;
931561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool InstantiationDependent = false;
932561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool ContainsUnexpandedParameterPack = false;
9336bb8017bb9e828d118e15e59d71c66bba323c364John McCall    E->HasExplicitTemplateArgumentList = true;
934561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
935561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                                InstantiationDependent,
936561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                              ContainsUnexpandedParameterPack);
937561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (InstantiationDependent)
938561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
9396bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
9406bb8017bb9e828d118e15e59d71c66bba323c364John McCall
9416bb8017bb9e828d118e15e59d71c66bba323c364John McCall  return E;
94283f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor}
94383f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor
94475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas GregorSourceRange MemberExpr::getSourceRange() const {
94575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  SourceLocation StartLoc;
94675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  if (isImplicitAccess()) {
94775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (hasQualifier())
94875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      StartLoc = getQualifierLoc().getBeginLoc();
94975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    else
95075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      StartLoc = MemberLoc;
95175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  } else {
95275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    // FIXME: We don't want this to happen. Rather, we should be able to
95375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    // detect all kinds of implicit accesses more cleanly.
95475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    StartLoc = getBase()->getLocStart();
95575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (StartLoc.isInvalid())
95675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      StartLoc = MemberLoc;
95775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  }
95875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
95975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  SourceLocation EndLoc =
96075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    HasExplicitTemplateArgumentList? getRAngleLoc()
96175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor                                   : getMemberNameInfo().getEndLoc();
96275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
96375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  return SourceRange(StartLoc, EndLoc);
96475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor}
96575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
9661d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCallvoid CastExpr::CheckCastConsistency() const {
9671d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  switch (getCastKind()) {
9681d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_DerivedToBase:
9691d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_UncheckedDerivedToBase:
9701d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_DerivedToBaseMemberPointer:
9711d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BaseToDerived:
9721d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BaseToDerivedMemberPointer:
9731d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(!path_empty() && "Cast kind should have a base path!");
9741d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    break;
9751d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
9761d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
9771d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isObjCObjectPointerType());
9781d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isPointerType());
9791d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
9801d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
9811d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
9821d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isObjCObjectPointerType());
9831d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isBlockPointerType());
9841d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
9851d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
9861d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BitCast:
9871d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // Arbitrary casts to C pointer types count as bitcasts.
9881d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // Otherwise, we should only have block and ObjC pointer casts
9891d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // here if they stay within the type kind.
9901d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    if (!getType()->isPointerType()) {
9911d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      assert(getType()->isObjCObjectPointerType() ==
9921d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall             getSubExpr()->getType()->isObjCObjectPointerType());
9931d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      assert(getType()->isBlockPointerType() ==
9941d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall             getSubExpr()->getType()->isBlockPointerType());
9951d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    }
9961d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
9971d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
9981d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_AnyPointerToBlockPointerCast:
9991d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isBlockPointerType());
10001d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isAnyPointerType() &&
10011d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall           !getSubExpr()->getType()->isBlockPointerType());
10021d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
10031d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
10041d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  // These should not have an inheritance path.
10051d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_Dynamic:
10061d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ToUnion:
10071d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ArrayToPointerDecay:
10081d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FunctionToPointerDecay:
10091d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NullToMemberPointer:
10101d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NullToPointer:
10111d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ConstructorConversion:
10121d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToPointer:
10131d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_PointerToIntegral:
10141d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ToVoid:
10151d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_VectorSplat:
10161d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralCast:
10171d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToFloating:
10181d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingToIntegral:
10191d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingCast:
10201d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ObjCObjectLValueCast:
10211d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingRealToComplex:
10221d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToReal:
10231d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexCast:
10241d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToIntegralComplex:
10251d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralRealToComplex:
10261d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToReal:
10271d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexCast:
10281d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToFloatingComplex:
102933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
103033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
103133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
103233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
10331d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(!getType()->isBooleanType() && "unheralded conversion to bool");
10341d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
10351d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
10361d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_Dependent:
10371d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_LValueToRValue:
10381d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_GetObjCProperty:
10391d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NoOp:
10401d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_PointerToBoolean:
10411d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToBoolean:
10421d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingToBoolean:
10431d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_MemberPointerToBoolean:
10441d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToBoolean:
10451d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToBoolean:
10461d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_LValueBitCast:            // -> bool&
10471d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_UserDefinedConversion:    // operator bool()
10481d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  CheckNoBasePath:
10491d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(path_empty() && "Cast kind should not have a base path!");
10501d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    break;
10511d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  }
10521d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall}
10531d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
1054f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlssonconst char *CastExpr::getCastKindName() const {
1055f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  switch (getCastKind()) {
1056daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_Dependent:
1057daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "Dependent";
10582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BitCast:
1059f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "BitCast";
10602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_LValueBitCast:
1061e39a3894513349908cdb3beba2614e53cb288e6cDouglas Gregor    return "LValueBitCast";
10620ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_LValueToRValue:
10630ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall    return "LValueToRValue";
1064f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  case CK_GetObjCProperty:
1065f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    return "GetObjCProperty";
10662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NoOp:
1067f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "NoOp";
10682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerived:
106911de6de25a0110cd7be97eef761ef3b189781da6Anders Carlsson    return "BaseToDerived";
10702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBase:
1071f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "DerivedToBase";
10722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UncheckedDerivedToBase:
107323cba801e11b03929c44f8cf54578305963a3476John McCall    return "UncheckedDerivedToBase";
10742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_Dynamic:
1075f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "Dynamic";
10762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToUnion:
1077f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ToUnion";
10782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ArrayToPointerDecay:
1079f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ArrayToPointerDecay";
10802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FunctionToPointerDecay:
1081f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "FunctionToPointerDecay";
10822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NullToMemberPointer:
1083f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "NullToMemberPointer";
1084404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall  case CK_NullToPointer:
1085404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall    return "NullToPointer";
10862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerivedMemberPointer:
1087f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "BaseToDerivedMemberPointer";
10882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBaseMemberPointer:
10891a31a18db9d657751f38c724adc0d62e86852bd7Anders Carlsson    return "DerivedToBaseMemberPointer";
10902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UserDefinedConversion:
1091f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "UserDefinedConversion";
10922de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ConstructorConversion:
1093f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ConstructorConversion";
10942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToPointer:
10957f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson    return "IntegralToPointer";
10962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_PointerToIntegral:
10977f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson    return "PointerToIntegral";
1098daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_PointerToBoolean:
1099daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "PointerToBoolean";
11002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToVoid:
1101ebeaf2031c968143c531bfe232d7507f20c57347Anders Carlsson    return "ToVoid";
11022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_VectorSplat:
110316a8904f3f5ed19158657e1da95e5902fbee66f7Anders Carlsson    return "VectorSplat";
11042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralCast:
110582debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "IntegralCast";
1106daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_IntegralToBoolean:
1107daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "IntegralToBoolean";
11082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToFloating:
110982debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "IntegralToFloating";
11102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FloatingToIntegral:
111182debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "FloatingToIntegral";
11122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FloatingCast:
1113c6b29163557d02da5d2a4a06f986f0480291f51fBenjamin Kramer    return "FloatingCast";
1114daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_FloatingToBoolean:
1115daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "FloatingToBoolean";
11162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_MemberPointerToBoolean:
1117bc0e0781da778bd5eb41a810419912893ae20448Anders Carlsson    return "MemberPointerToBoolean";
11181d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
11191d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return "CPointerToObjCPointerCast";
11201d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
11211d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return "BlockPointerToObjCPointerCast";
11222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_AnyPointerToBlockPointerCast:
11233b27f1a80e4e433b503efd344c909eeafaa9033cFariborz Jahanian    return "AnyPointerToBlockPointerCast";
11242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ObjCObjectLValueCast:
1125569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    return "ObjCObjectLValueCast";
11262bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_FloatingRealToComplex:
11272bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "FloatingRealToComplex";
1128f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToReal:
1129f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToReal";
1130f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToBoolean:
1131f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToBoolean";
11322bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_FloatingComplexCast:
11332bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "FloatingComplexCast";
1134f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToIntegralComplex:
1135f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToIntegralComplex";
11362bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_IntegralRealToComplex:
11372bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "IntegralRealToComplex";
1138f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToReal:
1139f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToReal";
1140f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToBoolean:
1141f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToBoolean";
11422bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_IntegralComplexCast:
11432bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "IntegralComplexCast";
1144f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToFloatingComplex:
1145f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToFloatingComplex";
114633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
114733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCConsumeObject";
114833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
114933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCProduceObject";
115033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
115133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCReclaimReturnedObject";
115233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
115333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCCExtendBlockObject";
1154f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  }
11551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11562bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  llvm_unreachable("Unhandled cast kind!");
1157f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  return 0;
1158f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson}
1159f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson
11606eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas GregorExpr *CastExpr::getSubExprAsWritten() {
11616eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  Expr *SubExpr = 0;
11626eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  CastExpr *E = this;
11636eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  do {
11646eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    SubExpr = E->getSubExpr();
116503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
116603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    // Skip through reference binding to temporary.
116703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
116803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                  = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
116903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      SubExpr = Materialize->GetTemporaryExpr();
117003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
11716eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Skip any temporary bindings; they're implicit.
11726eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
11736eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = Binder->getSubExpr();
1174c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
11756eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Conversions by constructor and conversion functions have a
11766eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // subexpression describing the call; strip it off.
11772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (E->getCastKind() == CK_ConstructorConversion)
11786eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
11792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    else if (E->getCastKind() == CK_UserDefinedConversion)
11806eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
1181c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
11826eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // If the subexpression we're left with is an implicit cast, look
11836eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // through that, too.
1184c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1185c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
11866eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return SubExpr;
11876eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
11886eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1189f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCXXBaseSpecifier **CastExpr::path_buffer() {
1190f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  switch (getStmtClass()) {
1191f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define ABSTRACT_STMT(x)
1192f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define CASTEXPR(Type, Base) \
1193f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  case Stmt::Type##Class: \
1194f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1195f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define STMT(Type, Base)
1196f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#include "clang/AST/StmtNodes.inc"
1197f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  default:
1198f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    llvm_unreachable("non-cast expressions not possible here");
1199f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
1200f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  }
1201f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1202f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1203f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallvoid CastExpr::setCastPath(const CXXCastPath &Path) {
1204f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  assert(Path.size() == path_size());
1205f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1206f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1207f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1208f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1209f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                           CastKind Kind, Expr *Operand,
1210f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                           const CXXCastPath *BasePath,
12115baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                           ExprValueKind VK) {
1212f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  unsigned PathSize = (BasePath ? BasePath->size() : 0);
1213f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1214f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1215f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  ImplicitCastExpr *E =
12165baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
1217f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (PathSize) E->setCastPath(*BasePath);
1218f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return E;
1219f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1220f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1221f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1222f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                                unsigned PathSize) {
1223f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1224f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1225f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1226f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1227f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1228f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1229f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
1230f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                       ExprValueKind VK, CastKind K, Expr *Op,
1231f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       const CXXCastPath *BasePath,
1232f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       TypeSourceInfo *WrittenTy,
1233f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       SourceLocation L, SourceLocation R) {
1234f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  unsigned PathSize = (BasePath ? BasePath->size() : 0);
1235f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1236f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1237f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CStyleCastExpr *E =
1238f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
1239f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (PathSize) E->setCastPath(*BasePath);
1240f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return E;
1241f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1242f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1243f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1244f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1245f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1246f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1247f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1248f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
12505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "<<=".
12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst char *BinaryOperator::getOpcodeStr(Opcode Op) {
12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
12532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_PtrMemD:   return ".*";
12542de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_PtrMemI:   return "->*";
12552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:       return "*";
12562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Div:       return "/";
12572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Rem:       return "%";
12582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:       return "+";
12592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:       return "-";
12602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shl:       return "<<";
12612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shr:       return ">>";
12622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LT:        return "<";
12632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_GT:        return ">";
12642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LE:        return "<=";
12652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_GE:        return ">=";
12662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_EQ:        return "==";
12672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_NE:        return "!=";
12682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_And:       return "&";
12692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Xor:       return "^";
12702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Or:        return "|";
12712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LAnd:      return "&&";
12722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LOr:       return "||";
12732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Assign:    return "=";
12742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_MulAssign: return "*=";
12752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_DivAssign: return "/=";
12762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_RemAssign: return "%=";
12772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_AddAssign: return "+=";
12782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_SubAssign: return "-=";
12792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_ShlAssign: return "<<=";
12802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_ShrAssign: return ">>=";
12812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_AndAssign: return "&=";
12822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_XorAssign: return "^=";
12832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_OrAssign:  return "|=";
12842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Comma:     return ",";
12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1286baf534875ed0a55c6342636ff3f4602b8ac22b69Douglas Gregor
1287baf534875ed0a55c6342636ff3f4602b8ac22b69Douglas Gregor  return "";
12885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCallBinaryOperatorKind
1291063daf6e196c51f162e0485478355d8e280eef5cDouglas GregorBinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1292063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  switch (OO) {
1293b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not an overloadable binary operator");
12942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Plus: return BO_Add;
12952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Minus: return BO_Sub;
12962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Star: return BO_Mul;
12972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Slash: return BO_Div;
12982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Percent: return BO_Rem;
12992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Caret: return BO_Xor;
13002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Amp: return BO_And;
13012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Pipe: return BO_Or;
13022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Equal: return BO_Assign;
13032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Less: return BO_LT;
13042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Greater: return BO_GT;
13052de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PlusEqual: return BO_AddAssign;
13062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_MinusEqual: return BO_SubAssign;
13072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_StarEqual: return BO_MulAssign;
13082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_SlashEqual: return BO_DivAssign;
13092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PercentEqual: return BO_RemAssign;
13102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_CaretEqual: return BO_XorAssign;
13112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_AmpEqual: return BO_AndAssign;
13122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PipeEqual: return BO_OrAssign;
13132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessLess: return BO_Shl;
13142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterGreater: return BO_Shr;
13152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessLessEqual: return BO_ShlAssign;
13162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterGreaterEqual: return BO_ShrAssign;
13172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_EqualEqual: return BO_EQ;
13182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_ExclaimEqual: return BO_NE;
13192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessEqual: return BO_LE;
13202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterEqual: return BO_GE;
13212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_AmpAmp: return BO_LAnd;
13222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PipePipe: return BO_LOr;
13232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Comma: return BO_Comma;
13242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_ArrowStar: return BO_PtrMemI;
1325063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  }
1326063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor}
1327063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor
1328063daf6e196c51f162e0485478355d8e280eef5cDouglas GregorOverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1329063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  static const OverloadedOperatorKind OverOps[] = {
1330063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1331063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Star, OO_Slash, OO_Percent,
1332063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Plus, OO_Minus,
1333063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_LessLess, OO_GreaterGreater,
1334063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1335063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_EqualEqual, OO_ExclaimEqual,
1336063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Amp,
1337063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Caret,
1338063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Pipe,
1339063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_AmpAmp,
1340063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PipePipe,
1341063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Equal, OO_StarEqual,
1342063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_SlashEqual, OO_PercentEqual,
1343063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PlusEqual, OO_MinusEqual,
1344063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_LessLessEqual, OO_GreaterGreaterEqual,
1345063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_AmpEqual, OO_CaretEqual,
1346063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PipeEqual,
1347063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Comma
1348063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  };
1349063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  return OverOps[Opc];
1350063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor}
1351063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor
1352709210feee317b8d6690dd1d15c2b74cfe55e261Ted KremenekInitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
1353418f6c7d142e5ff4607f70cd8431d008442bafe9Chris Lattner                           Expr **initExprs, unsigned numInits,
13544c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                           SourceLocation rbraceloc)
1355bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1356561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         false, false),
1357709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs(C, numInits),
13581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
13594423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis    HadArrayRangeDesignator(false)
1360c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt{
1361ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  for (unsigned I = 0; I != numInits; ++I) {
1362ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    if (initExprs[I]->isTypeDependent())
13638e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall      ExprBits.TypeDependent = true;
1364ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    if (initExprs[I]->isValueDependent())
13658e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall      ExprBits.ValueDependent = true;
1366561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (initExprs[I]->isInstantiationDependent())
1367561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
1368bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (initExprs[I]->containsUnexpandedParameterPack())
1369bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
137073460a32bc5299a5927d23d2e464d72af796eabfDouglas Gregor  }
1371c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1372709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek  InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
137366b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
13745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1375709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenekvoid InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
1376ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  if (NumInits > InitExprs.size())
1377709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs.reserve(C, NumInits);
1378fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor}
1379fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
1380709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenekvoid InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
1381709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek  InitExprs.resize(C, NumInits, 0);
13824c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
13834c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1384709210feee317b8d6690dd1d15c2b74cfe55e261Ted KremenekExpr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
1385ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  if (Init >= InitExprs.size()) {
1386709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
1387ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    InitExprs.back() = expr;
1388ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    return 0;
13894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
13901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13914c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
13924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitExprs[Init] = expr;
13934c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
13944c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
13954c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
13963e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidisvoid InitListExpr::setArrayFiller(Expr *filler) {
139721f77cd0c3da8a1dbaf6245cae43baf4c0b80ea4Argyrios Kyrtzidis  assert(!hasArrayFiller() && "Filler already set!");
13983e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  ArrayFillerOrUnionFieldInit = filler;
13993e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  // Fill out any "holes" in the array due to designated initializers.
14003e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  Expr **inits = getInits();
14013e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  for (unsigned i = 0, e = getNumInits(); i != e; ++i)
14023e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis    if (inits[i] == 0)
14033e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis      inits[i] = filler;
14043e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis}
14053e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis
1406c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted KremenekSourceRange InitListExpr::getSourceRange() const {
1407c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (SyntacticForm)
1408c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    return SyntacticForm->getSourceRange();
1409c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1410c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (Beg.isInvalid()) {
1411c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    // Find the first non-null initializer.
1412c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    for (InitExprsTy::const_iterator I = InitExprs.begin(),
1413c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek                                     E = InitExprs.end();
1414c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      I != E; ++I) {
1415c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      if (Stmt *S = *I) {
1416c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        Beg = S->getLocStart();
1417c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        break;
1418c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      }
1419c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    }
1420c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  }
1421c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (End.isInvalid()) {
1422c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    // Find the first non-null initializer from the end.
1423c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1424c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek                                             E = InitExprs.rend();
1425c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      I != E; ++I) {
1426c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      if (Stmt *S = *I) {
1427c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        End = S->getSourceRange().getEnd();
1428c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        break;
1429c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      }
1430c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    }
1431c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  }
1432c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  return SourceRange(Beg, End);
1433c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek}
1434c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek
1435bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block.
14364eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
14374eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroffconst FunctionType *BlockExpr::getFunctionType() const {
14386217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  return getType()->getAs<BlockPointerType>()->
1439183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                    getPointeeType()->getAs<FunctionType>();
14404eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
14414eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSourceLocation BlockExpr::getCaretLocation() const {
14431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getCaretLocation();
144456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
14451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst Stmt *BlockExpr::getBody() const {
14467297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor  return TheBlock->getBody();
14477297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
14481eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt *BlockExpr::getBody() {
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getBody();
14507297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
145156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
145256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines
14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1457026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// isUnusedResultAWarning - Return true if this immediate expression should
1458026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// be warned about if the result is unused.  If so, fill in Loc and Ranges
1459026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// with location to warn on and the source range[s] to report with the
1460026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// warning.
1461026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattnerbool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
1462df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump                                  SourceRange &R2, ASTContext &Ctx) const {
1463ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // Don't warn if the expr is type dependent. The type could end up
1464ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // instantiating to void.
1465ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  if (isTypeDependent())
1466ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson    return false;
14671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
14695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
14700faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
14710faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
1472026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = getExprLoc();
1473026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
1474026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass:
1476026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return cast<ParenExpr>(this)->getSubExpr()->
1477df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      isUnusedResultAWarning(Loc, R1, R2, Ctx);
1478f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
1479f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()->
1480f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      isUnusedResultAWarning(Loc, R1, R2, Ctx);
14815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UnaryOperatorClass: {
14825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const UnaryOperator *UO = cast<UnaryOperator>(this);
14831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (UO->getOpcode()) {
1485026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    default: break;
14862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostInc:
14872de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostDec:
14882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreInc:
14892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreDec:                 // ++/--
1490026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;  // Not a warning.
14912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Deref:
14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Dereferencing a volatile pointer is a side-effect.
1493df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1494026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
1495026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
14962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Real:
14972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Imag:
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // accessing a piece of a volatile complex is a side-effect.
1499df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1500df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump          .isVolatileQualified())
1501026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
1502026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
15032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
1504df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1506026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = UO->getOperatorLoc();
1507026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = UO->getSubExpr()->getSourceRange();
1508026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1510e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  case BinaryOperatorClass: {
1511026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const BinaryOperator *BO = cast<BinaryOperator>(this);
1512c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek    switch (BO->getOpcode()) {
1513c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek      default:
1514c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        break;
151525973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider the RHS of comma for side effects. LHS was checked by
151625973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Sema::CheckCommaOperands.
15172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_Comma:
1518c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // ((foo = <blah>), 0) is an idiom for hiding the result (and
1519c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // lvalue-ness) of an assignment written in a macro.
1520c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        if (IntegerLiteral *IE =
1521c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek              dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1522c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek          if (IE->getValue() == 0)
1523c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek            return false;
152425973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
152525973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider '||', '&&' to have side effects if the LHS or RHS does.
15262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LAnd:
15272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LOr:
152825973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
152925973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis            !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
153025973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis          return false;
153125973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        break;
1532bf0ee354163f87623a4b60412544243911332343John McCall    }
1533026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (BO->isAssignmentOp())
1534026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1535026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = BO->getOperatorLoc();
1536026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = BO->getLHS()->getSourceRange();
1537026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = BO->getRHS()->getSourceRange();
1538026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1539e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  }
1540eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner  case CompoundAssignOperatorClass:
1541c6dfe194f623b02c123759f235b504d4850fc178Douglas Gregor  case VAArgExprClass:
1542276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  case AtomicExprClass:
1543026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
15445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1545ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  case ConditionalOperatorClass: {
1546fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // If only one of the LHS or RHS is a warning, the operator might
1547fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // be being used for control flow. Only warn if both the LHS and
1548fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // RHS are warnings.
1549ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1550fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1551fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek      return false;
1552fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    if (!Exp->getLHS())
1553026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return true;
1554fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1555ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  }
1556ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian
15575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case MemberExprClass:
1558026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If the base pointer or element is to a volatile pointer/field, accessing
1559026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // it is a side effect.
1560df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1561026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1562026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<MemberExpr>(this)->getMemberLoc();
1563026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = SourceRange(Loc, Loc);
1564026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1565026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
15661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ArraySubscriptExprClass:
15685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If the base pointer or element is to a volatile pointer/field, accessing
1569026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // it is a side effect.
1570df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1571026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1572026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1573026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1574026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1575026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1576211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman
15779b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth  case CXXOperatorCallExprClass: {
15789b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // We warn about operator== and operator!= even when user-defined operator
15799b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // overloads as there is no reasonable way to define these such that they
15809b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // have non-trivial, desirable side-effects. See the -Wunused-comparison
15819b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // warning: these operators are commonly typo'ed, and so warning on them
15829b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // provides additional value as well. If this list is updated,
15839b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // DiagnoseUnusedComparison should be as well.
15849b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
15859b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    if (Op->getOperator() == OO_EqualEqual ||
15866e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay        Op->getOperator() == OO_ExclaimEqual) {
15876e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay      Loc = Op->getOperatorLoc();
15886e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay      R1 = Op->getSourceRange();
15899b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth      return true;
15906e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay    }
15919b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth
15929b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // Fallthrough for generic call handling.
15939b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth  }
15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case CallExprClass:
1595852871abbff45f1c1d3787755a27fce08365b166Eli Friedman  case CXXMemberCallExprClass: {
1596026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If this is a direct call, get the callee.
1597026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const CallExpr *CE = cast<CallExpr>(this);
1598d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    if (const Decl *FD = CE->getCalleeDecl()) {
1599026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // If the callee has attribute pure, const, or warn_unused_result, warn
1600026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // about it. void foo() { strlen("bar"); } should warn.
1601bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      //
1602bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1603bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // updated to match for QoI.
1604bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<WarnUnusedResultAttr>() ||
1605bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1606bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Loc = CE->getCallee()->getLocStart();
1607bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        R1 = CE->getCallee()->getSourceRange();
1608bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner
1609bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        if (unsigned NumArgs = CE->getNumArgs())
1610bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          R2 = SourceRange(CE->getArg(0)->getLocStart(),
1611bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner                           CE->getArg(NumArgs-1)->getLocEnd());
1612bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return true;
1613bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
1614026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    }
1615026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
1616026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner  }
161758beed91d468863b8c85bce43425422703838d27Anders Carlsson
161858beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXTemporaryObjectExprClass:
161958beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXConstructExprClass:
162058beed91d468863b8c85bce43425422703838d27Anders Carlsson    return false;
162158beed91d468863b8c85bce43425422703838d27Anders Carlsson
1622f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  case ObjCMessageExprClass: {
1623f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1624f85e193739c953358c865005855253af4f68a497John McCall    if (Ctx.getLangOptions().ObjCAutoRefCount &&
1625f85e193739c953358c865005855253af4f68a497John McCall        ME->isInstanceMessage() &&
1626f85e193739c953358c865005855253af4f68a497John McCall        !ME->getType()->isVoidType() &&
1627f85e193739c953358c865005855253af4f68a497John McCall        ME->getSelector().getIdentifierInfoForSlot(0) &&
1628f85e193739c953358c865005855253af4f68a497John McCall        ME->getSelector().getIdentifierInfoForSlot(0)
1629f85e193739c953358c865005855253af4f68a497John McCall                                               ->getName().startswith("init")) {
1630f85e193739c953358c865005855253af4f68a497John McCall      Loc = getExprLoc();
1631f85e193739c953358c865005855253af4f68a497John McCall      R1 = ME->getSourceRange();
1632f85e193739c953358c865005855253af4f68a497John McCall      return true;
1633f85e193739c953358c865005855253af4f68a497John McCall    }
1634f85e193739c953358c865005855253af4f68a497John McCall
1635f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMethodDecl *MD = ME->getMethodDecl();
1636f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1637f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      Loc = getExprLoc();
1638f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      return true;
1639f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    }
1640026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
1641f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  }
16421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
164312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  case ObjCPropertyRefExprClass:
16445e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    Loc = getExprLoc();
16455e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    R1 = getSourceRange();
1646a50089ec68a583d13718107c1b0c898f0903709eChris Lattner    return true;
164712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
1648611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  case StmtExprClass: {
1649611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // Statement exprs don't logically have side effects themselves, but are
1650611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // sometimes used in macros in ways that give them a type that is unused.
1651611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1652611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // however, if the result of the stmt expr is dead, we don't want to emit a
1653611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // warning.
1654611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1655d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    if (!CS->body_empty()) {
1656611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner      if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
1657df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump        return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1658d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis      if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1659d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis        if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1660d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis          return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1661d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    }
16621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16630faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
16640faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
1665026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<StmtExpr>(this)->getLParenLoc();
1666026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
1667026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1668611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  }
16696eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor  case CStyleCastExprClass:
1670fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner    // If this is an explicit cast to void, allow it.  People do this when they
1671fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner    // think they know what they're doing :).
1672026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (getType()->isVoidType())
1673fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner      return false;
1674026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1675026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1676026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
167758beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXFunctionalCastExprClass: {
16780faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
16790faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
168058beed91d468863b8c85bce43425422703838d27Anders Carlsson    const CastExpr *CE = cast<CastExpr>(this);
1681c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
168258beed91d468863b8c85bce43425422703838d27Anders Carlsson    // If this is a cast to void or a constructor conversion, check the operand.
168358beed91d468863b8c85bce43425422703838d27Anders Carlsson    // Otherwise, the result of the cast is unused.
16842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CE->getCastKind() == CK_ToVoid ||
16852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        CE->getCastKind() == CK_ConstructorConversion)
1686df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      return (cast<CastExpr>(this)->getSubExpr()
1687df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump              ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
1688026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1689026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1690026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
169158beed91d468863b8c85bce43425422703838d27Anders Carlsson  }
16921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16934be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman  case ImplicitCastExprClass:
16944be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman    // Check the operand, since implicit casts are inserted by Sema
1695df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<ImplicitCastExpr>(this)
1696df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
16974be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman
169804421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
1699df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXDefaultArgExpr>(this)
1700df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
17014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXNewExprClass:
17034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // FIXME: In theory, there might be new expressions that don't have side
17044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // effects (e.g. a placement new with an uninitialized POD).
17054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXDeleteExprClass:
1706026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
17072d46eb21eb2c904831b0e9f75ab3523384c70e66Anders Carlsson  case CXXBindTemporaryExprClass:
1708df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXBindTemporaryExpr>(this)
1709df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
17104765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case ExprWithCleanupsClass:
17114765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall    return (cast<ExprWithCleanups>(this)
1712df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
17134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
171644baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian/// isOBJCGCCandidate - Check if an expression is objc gc'able.
17177f4f86a2167abc116275e49c81350fc3225485e5Fariborz Jahanian/// returns true, if it is; false otherwise.
1718102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanianbool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
1719f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  const Expr *E = IgnoreParens();
1720f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  switch (E->getStmtClass()) {
172144baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  default:
172244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
172344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ObjCIvarRefExprClass:
172444baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return true;
1725207c5210eb0ac7b632609f0c006eb97ef2738948Fariborz Jahanian  case Expr::UnaryOperatorClass:
1726f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
172744baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ImplicitCastExprClass:
1728f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
172903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass:
173003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
173103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                                      ->isOBJCGCCandidate(Ctx);
173206b89124a9a5971a0528cc9da6817740bac43164Fariborz Jahanian  case CStyleCastExprClass:
1733f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
1734e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian  case BlockDeclRefExprClass:
1735a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  case DeclRefExprClass: {
1736e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian
1737e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian    const Decl *D;
1738e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian    if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1739e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian        D = BDRE->getDecl();
1740e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian    else
1741e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian        D = cast<DeclRefExpr>(E)->getDecl();
1742e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian
1743102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1744102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      if (VD->hasGlobalStorage())
1745102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian        return true;
1746102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      QualType T = VD->getType();
174759a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // dereferencing to a  pointer is always a gc'able candidate,
174859a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // unless it is __weak.
17497e88a60d38b36695520e4f8d9279766ef111a662Daniel Dunbar      return T->isPointerType() &&
17500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall             (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
1751102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    }
175244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
175344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
175483f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  case MemberExprClass: {
1755f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    const MemberExpr *M = cast<MemberExpr>(E);
1756102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return M->getBase()->isOBJCGCCandidate(Ctx);
175744baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
175844baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ArraySubscriptExprClass:
1759f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
176044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
176144baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian}
1762369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
176311ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidisbool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
176411ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis  if (isTypeDependent())
176511ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis    return false;
17667eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
176711ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis}
176811ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis
1769864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCallQualType Expr::findBoundMemberType(const Expr *expr) {
1770e0a22d06888c13989b3f72db319f1d498bf69153John McCall  assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
1771864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1772864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Bound member expressions are always one of these possibilities:
1773864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  //   x->m      x.m      x->*y      x.*y
1774864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // (possibly parenthesized)
1775864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1776864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  expr = expr->IgnoreParens();
1777864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1778864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1779864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return mem->getMemberDecl()->getType();
1780864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
1781864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1782864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1783864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1784864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall                      ->getPointeeType();
1785864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    assert(type->isFunctionType());
1786864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return type;
1787864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
1788864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1789864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  assert(isa<UnresolvedMemberExpr>(expr));
1790864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  return QualType();
1791864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall}
1792864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1793369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1794369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                          Expr::CanThrowResult CT2) {
1795369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // CanThrowResult constants are ordered so that the maximum is the correct
1796369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // merge result.
1797369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return CT1 > CT2 ? CT1 : CT2;
1798369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1799369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1800369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1801369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr *E = const_cast<Expr*>(CE);
1802369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr::CanThrowResult R = Expr::CT_Cannot;
18037502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
1804369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1805369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1806369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return R;
1807369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1808369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
18097a614d8380297fcd2bc23986241905d97222948cRichard Smithstatic Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
18107a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           const Decl *D,
1811369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                           bool NullThrows = true) {
1812369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!D)
1813369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1814369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1815369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // See if we can get a function type from the decl somehow.
1816369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1817369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!VD) // If we have no clue what we're calling, assume the worst.
1818369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Can;
1819369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
18205221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  // As an extension, we assume that __attribute__((nothrow)) functions don't
18215221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  // throw.
18225221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
18235221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl    return Expr::CT_Cannot;
18245221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl
1825369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  QualType T = VD->getType();
1826369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const FunctionProtoType *FT;
1827369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if ((FT = T->getAs<FunctionProtoType>())) {
1828369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  } else if (const PointerType *PT = T->getAs<PointerType>())
1829369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1830369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1831369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1832369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1833369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1834369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1835369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1836369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1837369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!FT)
1838369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Can;
1839369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
18407a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (FT->getExceptionSpecType() == EST_Delayed) {
18417a614d8380297fcd2bc23986241905d97222948cRichard Smith    assert(isa<CXXConstructorDecl>(D) &&
18427a614d8380297fcd2bc23986241905d97222948cRichard Smith           "only constructor exception specs can be unknown");
18437a614d8380297fcd2bc23986241905d97222948cRichard Smith    Ctx.getDiagnostics().Report(E->getLocStart(),
18447a614d8380297fcd2bc23986241905d97222948cRichard Smith                                diag::err_exception_spec_unknown)
18457a614d8380297fcd2bc23986241905d97222948cRichard Smith      << E->getSourceRange();
18467a614d8380297fcd2bc23986241905d97222948cRichard Smith    return Expr::CT_Can;
18477a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
18487a614d8380297fcd2bc23986241905d97222948cRichard Smith
18498026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
1850369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1851369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1852369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1853369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (DC->isTypeDependent())
1854369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Dependent;
1855369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1856295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  if (!DC->getTypeAsWritten()->isReferenceType())
1857295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl    return Expr::CT_Cannot;
1858295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl
1859be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman  if (DC->getSubExpr()->isTypeDependent())
1860be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    return Expr::CT_Dependent;
1861be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman
1862369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1863369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1864369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1865369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1866369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                           const CXXTypeidExpr *DC) {
1867369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (DC->isTypeOperand())
1868369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1869369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1870369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr *Op = DC->getExprOperand();
1871369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (Op->isTypeDependent())
1872369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Dependent;
1873369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1874369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const RecordType *RT = Op->getType()->getAs<RecordType>();
1875369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!RT)
1876369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1877369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1878369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1879369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1880369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1881369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (Op->Classify(C).isPRValue())
1882369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1883369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1884369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return Expr::CT_Can;
1885369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1886369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1887369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian RedlExpr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1888369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // C++ [expr.unary.noexcept]p3:
1889369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  //   [Can throw] if in a potentially-evaluated context the expression would
1890369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  //   contain:
1891369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  switch (getStmtClass()) {
1892369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXThrowExprClass:
1893369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated throw-expression
1894369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
1895369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1896369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDynamicCastExprClass: {
1897369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1898369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     where T is a reference type, that requires a run-time check
1899369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1900369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1901369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1902369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1903369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1904369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1905369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXTypeidExprClass:
1906369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated typeid expression applied to a glvalue
1907369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     expression whose type is a polymorphic class type
1908369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1909369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1910369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated call to a function, member function, function
1911369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     pointer, or member function pointer that does not have a non-throwing
1912369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     exception-specification
1913369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CallExprClass:
1914369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXOperatorCallExprClass:
1915369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXMemberCallExprClass: {
1916ebc93e176dad36fa8a28dd3a36c5b3dc7630d87dEli Friedman    const CallExpr *CE = cast<CallExpr>(this);
1917be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    CanThrowResult CT;
1918be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    if (isTypeDependent())
1919be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = CT_Dependent;
1920ebc93e176dad36fa8a28dd3a36c5b3dc7630d87dEli Friedman    else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1921ebc93e176dad36fa8a28dd3a36c5b3dc7630d87dEli Friedman      CT = CT_Cannot;
1922be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    else
19237a614d8380297fcd2bc23986241905d97222948cRichard Smith      CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
1924369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1925369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1926369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1927369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1928369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1929295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  case CXXConstructExprClass:
1930295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  case CXXTemporaryObjectExprClass: {
19317a614d8380297fcd2bc23986241905d97222948cRichard Smith    CanThrowResult CT = CanCalleeThrow(C, this,
1932369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl        cast<CXXConstructExpr>(this)->getConstructor());
1933369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1934369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1935369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1936369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1937369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1938369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXNewExprClass: {
1939be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    CanThrowResult CT;
1940be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    if (isTypeDependent())
1941be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = CT_Dependent;
1942be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    else
1943be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = MergeCanThrow(
19447a614d8380297fcd2bc23986241905d97222948cRichard Smith        CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
19457a614d8380297fcd2bc23986241905d97222948cRichard Smith        CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
1946369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                       /*NullThrows*/false));
1947369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1948369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1949369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1950369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1951369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1952369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDeleteExprClass: {
1953be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    CanThrowResult CT;
1954be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1955be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    if (DTy.isNull() || DTy->isDependentType()) {
1956be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = CT_Dependent;
1957be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    } else {
19587a614d8380297fcd2bc23986241905d97222948cRichard Smith      CT = CanCalleeThrow(C, this,
19597a614d8380297fcd2bc23986241905d97222948cRichard Smith                          cast<CXXDeleteExpr>(this)->getOperatorDelete());
1960be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      if (const RecordType *RT = DTy->getAs<RecordType>()) {
1961be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman        const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
19627a614d8380297fcd2bc23986241905d97222948cRichard Smith        CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
19630b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      }
1964be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      if (CT == CT_Can)
1965be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman        return CT;
19660b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    }
19670b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
19680b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl  }
19690b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl
19700b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl  case CXXBindTemporaryExprClass: {
19710b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    // The bound temporary has to be destroyed again, which might throw.
19727a614d8380297fcd2bc23986241905d97222948cRichard Smith    CanThrowResult CT = CanCalleeThrow(C, this,
19730b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
19740b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    if (CT == CT_Can)
19750b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      return CT;
1976369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1977369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1978369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1979369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // ObjC message sends are like function calls, but never have exception
1980369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // specs.
1981369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCMessageExprClass:
1982369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCPropertyRefExprClass:
1983369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
1984369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1985369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Many other things have subexpressions, so we have to test those.
1986369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some are simple:
1987369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ParenExprClass:
1988369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case MemberExprClass:
1989369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXReinterpretCastExprClass:
1990369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXConstCastExprClass:
1991369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ConditionalOperatorClass:
1992369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CompoundLiteralExprClass:
1993369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ExtVectorElementExprClass:
1994369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case InitListExprClass:
1995369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case DesignatedInitExprClass:
1996369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ParenListExprClass:
1997369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case VAArgExprClass:
1998369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDefaultArgExprClass:
19994765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case ExprWithCleanupsClass:
2000369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCIvarRefExprClass:
2001369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCIsaExprClass:
2002369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ShuffleVectorExprClass:
2003369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CanSubExprsThrow(C, this);
2004369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2005369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some might be dependent for other reasons.
2006369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case UnaryOperatorClass:
2007369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ArraySubscriptExprClass:
2008369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ImplicitCastExprClass:
2009369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CStyleCastExprClass:
2010369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXStaticCastExprClass:
2011369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXFunctionalCastExprClass:
2012369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case BinaryOperatorClass:
201303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case CompoundAssignOperatorClass:
201403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass: {
2015369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2016369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2017369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
2018369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2019369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2020369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case StmtExprClass:
2021369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
2022369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2023369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ChooseExprClass:
2024369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (isTypeDependent() || isValueDependent())
2025369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT_Dependent;
2026369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2027369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2028f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
2029f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (cast<GenericSelectionExpr>(this)->isResultDependent())
2030f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      return CT_Dependent;
2031f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2032f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
2033369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some expressions are always dependent.
2034369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case DependentScopeDeclRefExprClass:
2035369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXUnresolvedConstructExprClass:
2036369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDependentScopeMemberExprClass:
2037369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Dependent;
2038369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2039369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  default:
2040369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // All other expressions don't have subexpressions, or else they are
2041369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // unevaluated.
2042369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Cannot;
2043369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
2044369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
2045369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
20464e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() {
20474e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  Expr* E = this;
2048b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara  while (true) {
2049b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2050b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      E = P->getSubExpr();
2051b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2052b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2053b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2054b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2055b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2056b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2057b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2058b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2059f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2060f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2061f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2062f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2063f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2064f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
2065b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
2066b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara  }
20674e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek}
20684e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek
206956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
207056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand.
207156f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() {
207256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  Expr *E = this;
207356f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  while (true) {
2074b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
207556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
2076b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2077b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2078b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
207956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
2080b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2081b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2082b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2083b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2084b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2085b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2086b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2087b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2088f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2089f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2090f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2091f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2092f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2093f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
209403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
209503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
209603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
209703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
209803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
2099c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2100c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2101c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2102c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2103c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2104b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
210556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  }
210656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner}
210756f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner
21089c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
21099c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// casts.  This is intended purely as a temporary workaround for code
21109c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// that hasn't yet been rewritten to do the right thing about those
21119c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// casts, and may disappear along with the last internal use.
2112f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallExpr *Expr::IgnoreParenLValueCasts() {
2113f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  Expr *E = this;
21149c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall  while (true) {
2115f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2116f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      E = P->getSubExpr();
2117f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      continue;
21189c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall    } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2119f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      if (P->getCastKind() == CK_LValueToRValue) {
2120f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        E = P->getSubExpr();
2121f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        continue;
2122f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
21239c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall    } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
21249c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall      if (P->getOpcode() == UO_Extension) {
21259c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall        E = P->getSubExpr();
21269c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall        continue;
21279c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall      }
2128f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2129f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2130f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2131f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2132f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
213303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    } else if (MaterializeTemporaryExpr *Materialize
213403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
213503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
213603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
2137c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    } else if (SubstNonTypeTemplateParmExpr *NTTP
2138c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2139c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2140c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2141f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
2142f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
2143f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
2144f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return E;
2145f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
2146f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
21472fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCallExpr *Expr::IgnoreParenImpCasts() {
21482fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  Expr *E = this;
21492fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  while (true) {
2150b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
21512fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      E = P->getSubExpr();
2152b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2153b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2154b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
21552fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      E = P->getSubExpr();
2156b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2157b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2158b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2159b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2160b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2161b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2162b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2163b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2164f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2165f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2166f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2167f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2168f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2169f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
217003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
217103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
217203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
217303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
217403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
2175c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2176c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2177c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2178c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2179c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2180b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
21812fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  }
21822fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall}
21832fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall
21842f072b442879b8bba8c5dea11d7c61bedb1924aeHans WennborgExpr *Expr::IgnoreConversionOperator() {
21852f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
218614d251cd62942bf7d56bb87a267ba2ca2f7fae3eChandler Carruth    if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
21872f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg      return MCE->getImplicitObjectArgument();
21882f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  }
21892f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  return this;
21902f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg}
21912f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg
2192ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2193ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// value (including ptr->int casts of the same size).  Strip off any
2194ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// ParenExpr or CastExprs, returning their operand.
2195ecdd84147c0765caa999ddc22dde25b42712bb4dChris LattnerExpr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2196ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  Expr *E = this;
2197ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  while (true) {
2198ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2199ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      E = P->getSubExpr();
2200ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      continue;
2201ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    }
22021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2203ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2204ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      // We ignore integer <-> casts that are of the same width, ptr<->ptr and
22052ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor      // ptr<->int casts of the same width.  We also ignore all identity casts.
2206ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      Expr *SE = P->getSubExpr();
22071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2208ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2209ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
2210ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
2211ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
22121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2213b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if ((E->getType()->isPointerType() ||
22149d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           E->getType()->isIntegralType(Ctx)) &&
2215b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara          (SE->getType()->isPointerType() ||
22169d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           SE->getType()->isIntegralType(Ctx)) &&
2217ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner          Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2218ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
2219ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
2220ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
2221ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    }
22221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2223b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2224b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2225b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2226b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2227b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
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    }
2236f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
2237c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2238c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2239c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2240c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2241c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2242c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor
2243ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    return E;
2244ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  }
2245ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner}
2246ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
22476eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorbool Expr::isDefaultArgument() const {
22486eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  const Expr *E = this;
224903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
225003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = M->GetTemporaryExpr();
225103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
22526eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
22536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    E = ICE->getSubExprAsWritten();
2254c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
22556eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return isa<CXXDefaultArgExpr>(E);
22566eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2257ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
22582f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// \brief Skip over any no-op casts and any temporary-binding
22592f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// expressions.
2260f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlssonstatic const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
226103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
226203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = M->GetTemporaryExpr();
226303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
22642f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
22652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
22662f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
22672f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
22682f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
22692f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
22702f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
22712f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
22722f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    E = BE->getSubExpr();
22732f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
22742f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
22752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
22762f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
22772f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
22782f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
22792f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
2280f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson
2281f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson  return E->IgnoreParens();
22822f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
22832f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
2284558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// isTemporaryObject - Determines if this expression produces a
2285558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// temporary of the given class type.
2286558d2abc7f9fd6801cc7677200992313ae90b5d8John McCallbool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2287558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2288558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    return false;
22892f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
2290f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson  const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
22912f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
229258277b57f9492d0234748be89bcad48b322c5cf7John McCall  // Temporaries are by definition pr-values of class type.
2293db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian  if (!E->Classify(C).isPRValue()) {
2294db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian    // In this context, property reference is a message call and is pr-value.
229512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    if (!isa<ObjCPropertyRefExpr>(E))
2296db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian      return false;
2297db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian  }
22982f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
229919e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // Black-list a few cases which yield pr-values of class type that don't
230019e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // refer to temporaries of that type:
230119e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall
230219e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // - implicit derived-to-base conversions
2303558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (isa<ImplicitCastExpr>(E)) {
2304558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2305558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_DerivedToBase:
2306558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_UncheckedDerivedToBase:
2307558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      return false;
2308558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    default:
2309558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      break;
2310558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    }
23112f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
23122f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
231319e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // - member expressions (all)
231419e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  if (isa<MemberExpr>(E))
231519e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall    return false;
231619e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall
231756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // - opaque values (all)
231856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (isa<OpaqueValueExpr>(E))
231956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return false;
232056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
2321558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  return true;
23222f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
23232f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
232475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregorbool Expr::isImplicitCXXThis() const {
232575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  const Expr *E = this;
232675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
232775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  // Strip away parentheses and casts we don't care about.
232875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  while (true) {
232975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
233075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      E = Paren->getSubExpr();
233175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      continue;
233275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
233375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
233475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
233575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      if (ICE->getCastKind() == CK_NoOp ||
233675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_LValueToRValue ||
233775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_DerivedToBase ||
233875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_UncheckedDerivedToBase) {
233975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        E = ICE->getSubExpr();
234075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        continue;
234175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      }
234275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
234375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
234475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
234575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      if (UnOp->getOpcode() == UO_Extension) {
234675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        E = UnOp->getSubExpr();
234775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        continue;
234875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      }
234975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
235075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
235103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (const MaterializeTemporaryExpr *M
235203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
235303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = M->GetTemporaryExpr();
235403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
235503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
235603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
235775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    break;
235875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  }
235975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
236075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
236175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    return This->isImplicit();
236275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
236375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  return false;
236475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor}
236575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
2366898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyTypeDependentArguments - Determines if any of the expressions
2367898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is type-dependent.
2368898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2369898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
2370898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isTypeDependent())
2371898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
2372898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2373898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
2374898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
2375898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2376898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyValueDependentArguments - Determines if any of the expressions
2377898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is value-dependent.
2378898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2379898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
2380898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isValueDependent())
2381898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
2382898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2383898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
2384898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
2385898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
23864204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCallbool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
2387c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // This function is attempting whether an expression is an initializer
2388c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // which can be evaluated at compile-time.  isEvaluatable handles most
2389c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // of the cases, but it can't deal with some initializer-specific
2390c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // expressions, and it can't deal with aggregates; we deal with those here,
2391c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // and fall back to isEvaluatable for the other cases.
2392c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman
23934204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // If we ever capture reference-binding directly in the AST, we can
23944204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // kill the second parameter.
23954204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
23964204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  if (IsForRef) {
23974204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    EvalResult Result;
23984204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
23994204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  }
24001f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman
2401e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  switch (getStmtClass()) {
2402c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  default: break;
2403e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case StringLiteralClass:
240414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case ObjCStringLiteralClass:
2405eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  case ObjCEncodeExprClass:
2406e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    return true;
2407b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXTemporaryObjectExprClass:
2408b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXConstructExprClass: {
2409b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall    const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
24104204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
24114204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    // Only if it's
24124204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    // 1) an application of the trivial default constructor or
2413b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall    if (!CE->getConstructor()->isTrivial()) return false;
24144204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    if (!CE->getNumArgs()) return true;
24154204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
24164204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    // 2) an elidable trivial copy construction of an operand which is
24174204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    //    itself a constant initializer.  Note that we consider the
24184204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    //    operand on its own, *not* as a reference binding.
24194204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return CE->isElidable() &&
24204204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall           CE->getArg(0)->isConstantInitializer(Ctx, false);
2421b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  }
242259b5da6d853b4368b984700315adf7b37de05764Nate Begeman  case CompoundLiteralExprClass: {
24231f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // This handles gcc's extension that allows global initializers like
24241f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // "struct x {int x;} x = (struct x) {};".
24251f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This accepts other cases it shouldn't!
242659b5da6d853b4368b984700315adf7b37de05764Nate Begeman    const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
24274204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return Exp->isConstantInitializer(Ctx, false);
242859b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
2429e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case InitListExprClass: {
24301f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This doesn't deal with fields with reference types correctly.
24311f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This incorrectly allows pointers cast to integers to be assigned
24321f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // to bitfields.
2433e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    const InitListExpr *Exp = cast<InitListExpr>(this);
2434e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    unsigned numInits = Exp->getNumInits();
2435e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    for (unsigned i = 0; i < numInits; i++) {
24364204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
2437e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson        return false;
2438e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    }
2439c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    return true;
2440e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
24413498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  case ImplicitValueInitExprClass:
24423498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return true;
24433ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  case ParenExprClass:
24444204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return cast<ParenExpr>(this)->getSubExpr()
24454204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      ->isConstantInitializer(Ctx, IsForRef);
2446f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
2447f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (cast<GenericSelectionExpr>(this)->isResultDependent())
2448f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      return false;
2449f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()
2450f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      ->isConstantInitializer(Ctx, IsForRef);
24515cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara  case ChooseExprClass:
24525cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara    return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
24535cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara      ->isConstantInitializer(Ctx, IsForRef);
2454c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case UnaryOperatorClass: {
2455c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    const UnaryOperator* Exp = cast<UnaryOperator>(this);
24562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Exp->getOpcode() == UO_Extension)
24574204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
2458c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
2459c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  }
24603ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  case BinaryOperatorClass: {
24613ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    // Special case &&foo - &&bar.  It would be nice to generalize this somehow
24623ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    // but this handles the common case.
24633ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    const BinaryOperator *Exp = cast<BinaryOperator>(this);
24642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Exp->getOpcode() == BO_Sub &&
24653ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner        isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
24663ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner        isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
24673ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner      return true;
24683ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    break;
24693ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  }
24704204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  case CXXFunctionalCastExprClass:
2471b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXStaticCastExprClass:
247281045d8dcd967def69d8e0945566214a9fe9ffccChris Lattner  case ImplicitCastExprClass:
2473c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case CStyleCastExprClass:
2474c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // Handle casts with a destination that's a struct or union; this
2475c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // deals with both the gcc no-op struct cast extension and the
2476c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // cast-to-union extension.
2477c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    if (getType()->isRecordType())
24784204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return cast<CastExpr>(this)->getSubExpr()
24794204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall        ->isConstantInitializer(Ctx, false);
2480c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2481430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    // Integer->integer casts can be handled here, which is important for
2482430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    // things like (int)(&&x-&&y).  Scary but true.
2483430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    if (getType()->isIntegerType() &&
2484430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner        cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
24854204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return cast<CastExpr>(this)->getSubExpr()
24864204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall        ->isConstantInitializer(Ctx, false);
2487c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2488c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
248903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
249003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass:
24915f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
249203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                            ->isConstantInitializer(Ctx, false);
2493e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
2494c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  return isEvaluatable(Ctx);
249538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff}
249638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
249782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
249882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// pointer constant or not, as well as the specific kind of constant detected.
249982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// Null pointer constants can be integer constant expressions with the
250082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// value zero, casts of zero to void*, nullptr (C++0X), or __null
250182214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// (a GNU extension).
250282214a80c0163e01e4d8dec1426023c89277dbb4Chandler CarruthExpr::NullPointerConstantKind
250382214a80c0163e01e4d8dec1426023c89277dbb4Chandler CarruthExpr::isNullPointerConstant(ASTContext &Ctx,
250482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth                            NullPointerConstantValueDependence NPC) const {
2505ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  if (isValueDependent()) {
2506ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    switch (NPC) {
2507ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_NeverValueDependent:
2508b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unexpected value dependent expression!");
2509ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNull:
251082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      if (isTypeDependent() || getType()->isIntegralType(Ctx))
251182214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth        return NPCK_ZeroInteger;
251282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      else
251382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth        return NPCK_NotNull;
2514c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2515ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNotNull:
251682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      return NPCK_NotNull;
2517ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    }
2518ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  }
2519f515b2268f829adfbfdb751f54d102b53ed0285cDaniel Dunbar
25200777972d38a3125efed962b045704c30ae6965cfSebastian Redl  // Strip off a cast to void*, if it exists. Except in C++.
25210835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis  if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
25226215dee86c0e715b9f2b0d401ab2a5fcf629f1afSebastian Redl    if (!Ctx.getLangOptions().CPlusPlus) {
25230777972d38a3125efed962b045704c30ae6965cfSebastian Redl      // Check that it is a cast to void*.
25246217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
25250777972d38a3125efed962b045704c30ae6965cfSebastian Redl        QualType Pointee = PT->getPointeeType();
25260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        if (!Pointee.hasQualifiers() &&
25270777972d38a3125efed962b045704c30ae6965cfSebastian Redl            Pointee->isVoidType() &&                              // to void*
25280777972d38a3125efed962b045704c30ae6965cfSebastian Redl            CE->getSubExpr()->getType()->isIntegerType())         // from int.
2529ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor          return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
25300777972d38a3125efed962b045704c30ae6965cfSebastian Redl      }
25315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2532aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2533aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Ignore the ImplicitCastExpr type entirely.
2534ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
2535aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2536aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Accept ((void*)0) as a null pointer constant, as many other
2537aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // implementations do.
2538ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
2539f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  } else if (const GenericSelectionExpr *GE =
2540f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne               dyn_cast<GenericSelectionExpr>(this)) {
2541f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
25421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else if (const CXXDefaultArgExpr *DefaultArg
25438123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner               = dyn_cast<CXXDefaultArgExpr>(this)) {
254404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    // See through default argument expressions
2545ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
25462d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor  } else if (isa<GNUNullExpr>(this)) {
25472d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    // The GNU __null extension is always a null pointer constant.
254882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_GNUNull;
254903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  } else if (const MaterializeTemporaryExpr *M
255003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                   = dyn_cast<MaterializeTemporaryExpr>(this)) {
255103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
2552aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff  }
25532d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
25546e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // C++0x nullptr_t is always a null pointer constant.
25556e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  if (getType()->isNullPtrType())
255682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_CXX0X_nullptr;
25576e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2558ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian  if (const RecordType *UT = getType()->getAsUnionType())
2559ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian    if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2560ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2561ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian        const Expr *InitExpr = CLE->getInitializer();
2562ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian        if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2563ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian          return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2564ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      }
2565aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  // This expression must be an integer type.
2566c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  if (!getType()->isIntegerType() ||
256756fc0d18caf9c829647a5e3ce35197f0d7e0feeeFariborz Jahanian      (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
256882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_NotNull;
25691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have an integer constant expression, we need to *evaluate* it and
25715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // test for the value 0.
257209de1767990d4828bcaf0dd22033a5dddeecbe08Eli Friedman  llvm::APSInt Result;
257382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
257482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
257582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
25765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
257731a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff
2578f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// \brief If this expression is an l-value for an Objective C
2579f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// property, find the underlying property reference expression.
2580f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallconst ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2581f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  const Expr *E = this;
2582f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  while (true) {
2583f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    assert((E->getValueKind() == VK_LValue &&
2584f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall            E->getObjectKind() == OK_ObjCProperty) &&
2585f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall           "expression is not a property reference");
2586f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    E = E->IgnoreParenCasts();
2587f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2588f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      if (BO->getOpcode() == BO_Comma) {
2589f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        E = BO->getRHS();
2590f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        continue;
2591f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
2592f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
2593f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
2594f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
2595f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
2596f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
2597f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return cast<ObjCPropertyRefExpr>(E);
2598f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
2599f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
260033bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas GregorFieldDecl *Expr::getBitField() {
26016f4a69a3107e7ff1569c747f7c6bdf7cff8cbf55Douglas Gregor  Expr *E = this->IgnoreParens();
260233bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
2603de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2604f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (ICE->getCastKind() == CK_LValueToRValue ||
2605f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
2606de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      E = ICE->getSubExpr()->IgnoreParens();
2607de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    else
2608de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
2609de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  }
2610de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
261127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
261286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
261333bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor      if (Field->isBitField())
261433bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor        return Field;
261533bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
26160f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis  if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
26170f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis    if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
26180f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis      if (Field->isBitField())
26190f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis        return Field;
26200f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis
262142068e9d420889014ee26894e421fead336b239dEli Friedman  if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
262233bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor    if (BinOp->isAssignmentOp() && BinOp->getLHS())
262333bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor      return BinOp->getLHS()->getBitField();
262433bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
262542068e9d420889014ee26894e421fead336b239dEli Friedman    if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
262642068e9d420889014ee26894e421fead336b239dEli Friedman      return BinOp->getRHS()->getBitField();
262742068e9d420889014ee26894e421fead336b239dEli Friedman  }
262842068e9d420889014ee26894e421fead336b239dEli Friedman
262933bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor  return 0;
263027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
263127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
2632093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlssonbool Expr::refersToVectorElement() const {
2633093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  const Expr *E = this->IgnoreParens();
2634c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2635093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
26365baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    if (ICE->getValueKind() != VK_RValue &&
26372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        ICE->getCastKind() == CK_NoOp)
2638093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      E = ICE->getSubExpr()->IgnoreParens();
2639093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    else
2640093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      break;
2641093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  }
2642c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2643093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2644093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return ASE->getBase()->getType()->isVectorType();
2645093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
2646093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (isa<ExtVectorElementExpr>(E))
2647093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return true;
2648093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
2649093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  return false;
2650093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson}
2651093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
26522140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// isArrow - Return true if the base expression is a pointer to vector,
26532140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// return false if the base expression is a vector.
26542140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattnerbool ExtVectorElementExpr::isArrow() const {
26552140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner  return getBase()->getType()->isPointerType();
26562140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner}
26572140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner
2658213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const {
2659183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const VectorType *VT = getType()->getAs<VectorType>())
26608a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    return VT->getNumElements();
26618a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  return 1;
26624d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner}
26634d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner
26648a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated.
2665213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const {
2666a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // FIXME: Refactor this code to an accessor on the AST node which returns the
2667a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // "type" of component access, and share with code below and in Sema.
26685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Comp = Accessor->getName();
2669190d6a25393995b42e32086949a68285ee423fb9Nate Begeman
2670190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Halving swizzles do not contain duplicate elements.
2671150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
2672190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    return false;
26731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2674190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Advance past s-char prefix on hex swizzles.
2675150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
2676150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar    Comp = Comp.substr(1);
26771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2678150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  for (unsigned i = 0, e = Comp.size(); i != e; ++i)
26795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
2680fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff        return true;
2681150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar
2682fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  return false;
2683fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff}
2684b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
26858a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
26863b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess(
26875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  SmallVectorImpl<unsigned> &Elts) const {
26885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Comp = Accessor->getName();
26894b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
26904b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar    Comp = Comp.substr(1);
26911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26924b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isHi =   Comp == "hi";
26934b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isLo =   Comp == "lo";
26944b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isEven = Comp == "even";
26954b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isOdd  = Comp == "odd";
26961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26978a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
26988a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    uint64_t Index;
26991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27008a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    if (isHi)
27018a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = e + i;
27028a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isLo)
27038a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = i;
27048a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isEven)
27058a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i;
27068a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isOdd)
27078a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i + 1;
27088a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else
27094b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar      Index = ExtVectorType::getAccessorIdx(Comp[i]);
2710b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
27113b8d116703db8018f855cbb4733ace426422623bNate Begeman    Elts.push_back(Index);
2712b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner  }
27138a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman}
27148a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman
271504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
2716f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
271704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
271804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation SuperLoc,
271904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 bool IsInstanceSuper,
272004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 QualType SuperType,
2721c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
2722207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
2723207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
272404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
27258d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
272604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
2727f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
2728bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*TypeDependent=*/false, /*ValueDependent=*/false,
2729561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*InstantiationDependent=*/false,
2730bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*ContainsUnexpandedParameterPack=*/false),
273104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
273204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2733b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(IsInstanceSuper? SuperInstance : SuperClass),
2734b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
2735207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
2736c2350e553b853ad00914faf23fa731e5fc4a8a5cDouglas Gregor{
2737207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
273804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(SuperType.getAsOpaquePtr());
273904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
274004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
274104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
2742f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
274304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
274404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 TypeSourceInfo *Receiver,
2745f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                 Selector Sel,
2746207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
2747207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
274804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
27498d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
275004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
2751f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
2752561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         T->isDependentType(), T->isInstantiationDependentType(),
2753561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         T->containsUnexpandedParameterPack()),
275404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
275504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2756b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(Class),
2757b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false),
2758207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
275904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor{
2760207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
276104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(Receiver);
276204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
276304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
276404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
2765f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
276604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
276704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 Expr *Receiver,
2768c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
2769207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
2770207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
277104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
27728d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
277304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
2774f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
2775bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Receiver->isTypeDependent(),
2776561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Receiver->isInstantiationDependent(),
2777bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Receiver->containsUnexpandedParameterPack()),
277804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
277904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2780b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(Instance),
2781b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false),
2782207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
278304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor{
2784207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
278504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(Receiver);
2786207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2787207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2788207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidisvoid ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2789207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
2790207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                         SelectorLocationsKind SelLocsK) {
2791207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  setNumArgs(Args.size());
2792aa165f8458b51c546bebff947343e1a36f3594cbDouglas Gregor  Expr **MyArgs = getArgs();
27938d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis  for (unsigned I = 0; I != Args.size(); ++I) {
2794bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->isTypeDependent())
2795bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
2796bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->isValueDependent())
2797bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
2798561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[I]->isInstantiationDependent())
2799561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
2800bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->containsUnexpandedParameterPack())
2801bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
2802bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
2803bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    MyArgs[I] = Args[I];
2804bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
2805207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2806207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelLocsKind = SelLocsK;
2807207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  if (SelLocsK == SelLoc_NonStandard)
2808207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
280904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
281004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
281104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2812f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
281304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
281404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation SuperLoc,
281504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         bool IsInstanceSuper,
281604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         QualType SuperType,
2817c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
2818951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
281904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
28208d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
282104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2822207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelectorLocationsKind SelLocsK;
2823207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
2824f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
2825207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   SuperType, Sel, SelLocs, SelLocsK,
2826207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   Method, Args, RBracLoc);
282704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
282804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
282904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2830f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
283104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
283204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         TypeSourceInfo *Receiver,
2833c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
2834951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
283504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
28368d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
283704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2838207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelectorLocationsKind SelLocsK;
2839207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
2840951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
2841207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   SelLocs, SelLocsK, Method, Args, RBracLoc);
284204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
284304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
284404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2845f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
284604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
284704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         Expr *Receiver,
2848f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                         Selector Sel,
2849951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
285004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
28518d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
285204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2853207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelectorLocationsKind SelLocsK;
2854207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
2855951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
2856207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   SelLocs, SelLocsK, Method, Args, RBracLoc);
285704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
285804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
2859c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
2860207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                              unsigned NumArgs,
2861207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                              unsigned NumStoredSelLocs) {
2862207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
286304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
286404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
2865e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2866207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios KyrtzidisObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2867207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        ArrayRef<Expr *> Args,
2868207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        SourceLocation RBraceLoc,
2869207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        ArrayRef<SourceLocation> SelLocs,
2870207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        Selector Sel,
2871207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        SelectorLocationsKind &SelLocsK) {
2872207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2873207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2874207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                                               : 0;
2875207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  return alloc(C, Args.size(), NumStoredSelLocs);
2876207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2877207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2878207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios KyrtzidisObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2879207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        unsigned NumArgs,
2880207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        unsigned NumStoredSelLocs) {
2881207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2882207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2883207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  return (ObjCMessageExpr *)C.Allocate(Size,
2884207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                     llvm::AlignOf<ObjCMessageExpr>::Alignment);
2885207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2886207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2887207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidisvoid ObjCMessageExpr::getSelectorLocs(
2888207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                               SmallVectorImpl<SourceLocation> &SelLocs) const {
2889207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2890207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    SelLocs.push_back(getSelectorLoc(i));
2891207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2892207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2893e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios KyrtzidisSourceRange ObjCMessageExpr::getReceiverRange() const {
2894e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  switch (getReceiverKind()) {
2895e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case Instance:
2896e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getInstanceReceiver()->getSourceRange();
2897e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2898e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case Class:
2899e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2900e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2901e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case SuperInstance:
2902e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case SuperClass:
2903e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getSuperLoc();
2904e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  }
2905e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2906e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  return SourceLocation();
2907e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis}
2908e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
290904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorSelector ObjCMessageExpr::getSelector() const {
291004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (HasMethod)
291104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
291204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                               ->getSelector();
2913c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  return Selector(SelectorOrMethod);
291404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
291504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
291604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
291704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  switch (getReceiverKind()) {
291804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case Instance:
291904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    if (const ObjCObjectPointerType *Ptr
292004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
292104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor      return Ptr->getInterfaceDecl();
292204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
29234df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
292404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case Class:
2925c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (const ObjCObjectType *Ty
2926c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          = getClassReceiver()->getAs<ObjCObjectType>())
2927c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Ty->getInterface();
292804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
29294df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
293004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case SuperInstance:
293104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    if (const ObjCObjectPointerType *Ptr
293204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          = getSuperType()->getAs<ObjCObjectPointerType>())
293304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor      return Ptr->getInterfaceDecl();
293404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
2935c2350e553b853ad00914faf23fa731e5fc4a8a5cDouglas Gregor
293604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case SuperClass:
2937ee8a6cafe8b69c316dd4fa5f6ea4838ffe15621cArgyrios Kyrtzidis    if (const ObjCObjectType *Iface
2938ee8a6cafe8b69c316dd4fa5f6ea4838ffe15621cArgyrios Kyrtzidis          = getSuperType()->getAs<ObjCObjectType>())
2939ee8a6cafe8b69c316dd4fa5f6ea4838ffe15621cArgyrios Kyrtzidis      return Iface->getInterface();
294004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
294104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  }
29420389e6bd0159bfdd08f7c50a37543b6e3adf0c33Chris Lattner
294304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return 0;
2944eb3b324800598cc3d5385fbad95ae5cff2c79113Ted Kremenek}
29450389e6bd0159bfdd08f7c50a37543b6e3adf0c33Chris Lattner
29465f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef ObjCBridgedCastExpr::getBridgeKindName() const {
2947f85e193739c953358c865005855253af4f68a497John McCall  switch (getBridgeKind()) {
2948f85e193739c953358c865005855253af4f68a497John McCall  case OBC_Bridge:
2949f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge";
2950f85e193739c953358c865005855253af4f68a497John McCall  case OBC_BridgeTransfer:
2951f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge_transfer";
2952f85e193739c953358c865005855253af4f68a497John McCall  case OBC_BridgeRetained:
2953f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge_retained";
2954f85e193739c953358c865005855253af4f68a497John McCall  }
2955f85e193739c953358c865005855253af4f68a497John McCall
2956f85e193739c953358c865005855253af4f68a497John McCall  return "__bridge";
2957f85e193739c953358c865005855253af4f68a497John McCall}
2958f85e193739c953358c865005855253af4f68a497John McCall
29594ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ChooseExpr::isConditionTrue(const ASTContext &C) const {
2960a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  return getCond()->EvaluateKnownConstInt(C) != 0;
296127437caadea35f84d550cd29f024fcf3ea240eecChris Lattner}
296227437caadea35f84d550cd29f024fcf3ea240eecChris Lattner
2963bebbe0d9b7568ce43a464286bee49429489ef483Douglas GregorShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2964bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor                                     QualType Type, SourceLocation BLoc,
2965bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor                                     SourceLocation RP)
2966bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor   : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2967bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          Type->isDependentType(), Type->isDependentType(),
2968561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          Type->isInstantiationDependentType(),
2969bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          Type->containsUnexpandedParameterPack()),
2970bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor     BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2971bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor{
2972bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  SubExprs = new (C) Stmt*[nexpr];
2973bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  for (unsigned i = 0; i < nexpr; i++) {
2974bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
2975bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
2976bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
2977bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
2978561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
2979561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
2980bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
2981bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
2982bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
2983bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    SubExprs[i] = args[i];
2984bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
2985bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor}
2986bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
2987888376a2bbcfc2f047902249f8455918e2489ae1Nate Begemanvoid ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2988888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman                                 unsigned NumExprs) {
2989888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman  if (SubExprs) C.Deallocate(SubExprs);
2990888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
2991888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman  SubExprs = new (C) Stmt* [NumExprs];
299294cd5d1397bb1a8bcd109602aa38dd787b164c22Douglas Gregor  this->NumExprs = NumExprs;
299394cd5d1397bb1a8bcd109602aa38dd787b164c22Douglas Gregor  memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
29941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2995888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
2996f111d935722ed488144600cea5ed03a6b5069e8fPeter CollingbourneGenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2997f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation GenericLoc, Expr *ControllingExpr,
2998f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2999f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned NumAssocs, SourceLocation DefaultLoc,
3000f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation RParenLoc,
3001f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               bool ContainsUnexpandedParameterPack,
3002f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned ResultIndex)
3003f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  : Expr(GenericSelectionExprClass,
3004f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getType(),
3005f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getValueKind(),
3006f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getObjectKind(),
3007f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->isTypeDependent(),
3008f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->isValueDependent(),
3009561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         AssocExprs[ResultIndex]->isInstantiationDependent(),
3010f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         ContainsUnexpandedParameterPack),
3011f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3012f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3013f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3014f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    RParenLoc(RParenLoc) {
3015f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  SubExprs[CONTROLLING] = ControllingExpr;
3016f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3017f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3018f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne}
3019f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
3020f111d935722ed488144600cea5ed03a6b5069e8fPeter CollingbourneGenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3021f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation GenericLoc, Expr *ControllingExpr,
3022f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3023f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned NumAssocs, SourceLocation DefaultLoc,
3024f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation RParenLoc,
3025f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               bool ContainsUnexpandedParameterPack)
3026f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  : Expr(GenericSelectionExprClass,
3027f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         Context.DependentTy,
3028f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         VK_RValue,
3029f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         OK_Ordinary,
3030561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isTypeDependent=*/true,
3031561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isValueDependent=*/true,
3032561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isInstantiationDependent=*/true,
3033f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         ContainsUnexpandedParameterPack),
3034f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3035f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3036f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3037f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    RParenLoc(RParenLoc) {
3038f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  SubExprs[CONTROLLING] = ControllingExpr;
3039f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3040f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3041f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne}
3042f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
304377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
304405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//  DesignatedInitExpr
304505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
304605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3047b11382497a923b0d7009e85a1d8eb7bf93ec6d0dChandler CarruthIdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
304805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(Kind == FieldDesignator && "Only valid on a field designator");
304905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Field.NameOrField & 0x01)
305005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
305105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  else
305205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return getField()->getIdentifier();
305305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
305405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3055c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntDesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
3056319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                       unsigned NumDesignators,
3057ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       const Designator *Designators,
30581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       SourceLocation EqualOrColonLoc,
3059ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       bool GNUSyntax,
30601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       Expr **IndexExprs,
30619ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                       unsigned NumIndexExprs,
30629ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                       Expr *Init)
30631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(DesignatedInitExprClass, Ty,
3064f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall         Init->getValueKind(), Init->getObjectKind(),
3065bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Init->isTypeDependent(), Init->isValueDependent(),
3066561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Init->isInstantiationDependent(),
3067bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Init->containsUnexpandedParameterPack()),
30681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
30691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
3070319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  this->Designators = new (C) Designator[NumDesignators];
30719ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
30729ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Record the initializer itself.
30737502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  child_range Child = children();
30749ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  *Child++ = Init;
30759ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
30769ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Copy the designators and their subexpressions, computing
30779ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // value-dependence along the way.
30789ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  unsigned IndexIdx = 0;
30799ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  for (unsigned I = 0; I != NumDesignators; ++I) {
3080ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    this->Designators[I] = Designators[I];
30819ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
30829ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    if (this->Designators[I].isArrayDesignator()) {
30839ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
30849ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Index = IndexExprs[IndexIdx];
3085bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Index->isTypeDependent() || Index->isValueDependent())
3086bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ValueDependent = true;
3087561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      if (Index->isInstantiationDependent())
3088561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3089bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      // Propagate unexpanded parameter packs.
3090bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Index->containsUnexpandedParameterPack())
3091bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ContainsUnexpandedParameterPack = true;
30929ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
30939ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the index expressions into permanent storage.
30949ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
30959ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    } else if (this->Designators[I].isArrayRangeDesignator()) {
30969ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
30979ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Start = IndexExprs[IndexIdx];
30989ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *End = IndexExprs[IndexIdx + 1];
3099bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Start->isTypeDependent() || Start->isValueDependent() ||
3100561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          End->isTypeDependent() || End->isValueDependent()) {
3101bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ValueDependent = true;
3102561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3103561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      } else if (Start->isInstantiationDependent() ||
3104561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                 End->isInstantiationDependent()) {
3105561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3106561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      }
3107561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
3108bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      // Propagate unexpanded parameter packs.
3109bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Start->containsUnexpandedParameterPack() ||
3110bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          End->containsUnexpandedParameterPack())
3111bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ContainsUnexpandedParameterPack = true;
31129ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
31139ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the start/end expressions into permanent storage.
31149ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
31159ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
31169ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    }
31179ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  }
31189ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
31199ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
3120ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
3121ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
312205c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr *
31231eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
312405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           unsigned NumDesignators,
312505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           Expr **IndexExprs, unsigned NumIndexExprs,
312605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           SourceLocation ColonOrEqualLoc,
312705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           bool UsesColonSyntax, Expr *Init) {
3128c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3129c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3130319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
31319ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                      ColonOrEqualLoc, UsesColonSyntax,
31329ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                      IndexExprs, NumIndexExprs, Init);
313305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
313405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
31351eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
3136d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                                    unsigned NumIndexExprs) {
3137d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3138d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3139d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3140d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
3141d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
3142319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregorvoid DesignatedInitExpr::setDesignators(ASTContext &C,
3143319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                        const Designator *Desigs,
3144d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                        unsigned NumDesigs) {
3145319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  Designators = new (C) Designator[NumDesigs];
3146d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  NumDesignators = NumDesigs;
3147d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  for (unsigned I = 0; I != NumDesigs; ++I)
3148d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor    Designators[I] = Desigs[I];
3149d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
3150d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
315124f4674e697fb53587f0e8485e9c6592f7021ef2Abramo BagnaraSourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
315224f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
315324f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  if (size() == 1)
315424f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara    return DIE->getDesignator(0)->getSourceRange();
315524f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  return SourceRange(DIE->getDesignator(0)->getStartLocation(),
315624f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara                     DIE->getDesignator(size()-1)->getEndLocation());
315724f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara}
315824f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara
315905c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorSourceRange DesignatedInitExpr::getSourceRange() const {
316005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation StartLoc;
3161d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner  Designator &First =
3162d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    *const_cast<DesignatedInitExpr*>(this)->designators_begin();
316305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (First.isFieldDesignator()) {
3164eeae8f072748affce25ab4064982626361293390Douglas Gregor    if (GNUSyntax)
316505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
316605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else
316705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
316805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  } else
3169d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    StartLoc =
3170d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner      SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
317105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
317205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
317305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
317405c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
317505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
317605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
317705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
317805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
317905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
318005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
318105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
318205c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
31831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
318405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
318505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
318605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
318705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
318805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
318905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
319005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
319105c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
31921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
319305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
319405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
319505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
319605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
319705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
319805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
319905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3200ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// \brief Replaces the designator at index @p Idx with the series
3201ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// of designators in [First, Last).
3202319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregorvoid DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
32031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          const Designator *First,
3204ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                          const Designator *Last) {
3205ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  unsigned NumNewDesignators = Last - First;
3206ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  if (NumNewDesignators == 0) {
3207ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    std::copy_backward(Designators + Idx + 1,
3208ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + NumDesignators,
3209ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + Idx);
3210ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    --NumNewDesignators;
3211ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
3212ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  } else if (NumNewDesignators == 1) {
3213ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    Designators[Idx] = *First;
3214ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
3215ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
3216ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
32171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Designator *NewDesignators
3218319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor    = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
3219ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators, Designators + Idx, NewDesignators);
3220ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(First, Last, NewDesignators + Idx);
3221ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators + Idx + 1, Designators + NumDesignators,
3222ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor            NewDesignators + Idx + NumNewDesignators);
3223ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  Designators = NewDesignators;
3224ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  NumDesignators = NumDesignators - 1 + NumNewDesignators;
3225ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
3226ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
32271eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
32282ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                             Expr **exprs, unsigned nexprs,
32290d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek                             SourceLocation rparenloc, QualType T)
32300d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek  : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
3231561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         false, false, false, false),
3232bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
32330d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek  assert(!T.isNull() && "ParenListExpr must have a valid type");
32342ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman  Exprs = new (C) Stmt*[nexprs];
3235bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  for (unsigned i = 0; i != nexprs; ++i) {
3236bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->isTypeDependent())
3237bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
3238bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->isValueDependent())
3239bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
3240561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (exprs[i]->isInstantiationDependent())
3241561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
3242bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->containsUnexpandedParameterPack())
3243bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
3244bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
32452ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    Exprs[i] = exprs[i];
3246bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
32472ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman}
32482ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman
3249e996ffd240f20a1048179d7727a6ee3227261921John McCallconst OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3250e996ffd240f20a1048179d7727a6ee3227261921John McCall  if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3251e996ffd240f20a1048179d7727a6ee3227261921John McCall    e = ewc->getSubExpr();
325203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
325303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    e = m->GetTemporaryExpr();
3254e996ffd240f20a1048179d7727a6ee3227261921John McCall  e = cast<CXXConstructExpr>(e)->getArg(0);
3255e996ffd240f20a1048179d7727a6ee3227261921John McCall  while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3256e996ffd240f20a1048179d7727a6ee3227261921John McCall    e = ice->getSubExpr();
3257e996ffd240f20a1048179d7727a6ee3227261921John McCall  return cast<OpaqueValueExpr>(e);
3258e996ffd240f20a1048179d7727a6ee3227261921John McCall}
3259e996ffd240f20a1048179d7727a6ee3227261921John McCall
326005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
3261ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//  ExprIterator.
3262ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
3263ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
3264ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3265ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3266ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3267ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator[](size_t idx) const {
3268ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  return cast<Expr>(I[idx]);
3269ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek}
3270ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3271ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3272ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
3273ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
327477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//  Child Iterators for iterating over subexpressions/substatements
327577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
327677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
3277f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne// UnaryExprOrTypeTraitExpr
3278f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter CollingbourneStmt::child_range UnaryExprOrTypeTraitExpr::children() {
32790518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // If this is of a type and the type is a VLA type (and not a typedef), the
32800518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // size expression of the VLA needs to be treated as an executable expression.
32810518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // Why isn't this weirdness documented better in StmtIterator?
32820518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType()) {
3283f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
32840518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl                                   getArgumentType().getTypePtr()))
328563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      return child_range(child_iterator(T), child_iterator());
328663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return child_range();
32870518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  }
328863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return child_range(&Argument.Ex, &Argument.Ex + 1);
32899ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
3290390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
3291563477da25f824e37c535131695dc4dc9b68c465Steve Naroff// ObjCMessageExpr
329263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallStmt::child_range ObjCMessageExpr::children() {
329363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  Stmt **begin;
329404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (getReceiverKind() == Instance)
329563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    begin = reinterpret_cast<Stmt **>(this + 1);
329663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  else
329763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    begin = reinterpret_cast<Stmt **>(getArgs());
329863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return child_range(begin,
329963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                     reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
3300563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
3301563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
33024eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff// Blocks
33036b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallBlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
3304a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor                                   SourceLocation l, bool ByRef,
33056b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                                   bool constAdded)
3306561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
3307a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor         d->isParameterPack()),
33086b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
3309a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor{
3310d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool TypeDependent = false;
3311d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool ValueDependent = false;
3312561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  bool InstantiationDependent = false;
3313561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3314561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                           InstantiationDependent);
3315d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.TypeDependent = TypeDependent;
3316d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.ValueDependent = ValueDependent;
3317561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  ExprBits.InstantiationDependent = InstantiationDependent;
3318a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor}
3319dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman
3320dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman
3321dfa64ba45922e1c28e36341bdf34785fea74659bEli FriedmanAtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3322dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman                       QualType t, AtomicOp op, SourceLocation RP)
3323dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman  : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3324dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman         false, false, false, false),
3325dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3326dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman{
3327dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman  for (unsigned i = 0; i < nexpr; i++) {
3328dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isTypeDependent())
3329dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.TypeDependent = true;
3330dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isValueDependent())
3331dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.ValueDependent = true;
3332dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isInstantiationDependent())
3333dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.InstantiationDependent = true;
3334dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->containsUnexpandedParameterPack())
3335dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.ContainsUnexpandedParameterPack = true;
3336dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman
3337dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    SubExprs[i] = args[i];
3338dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman  }
3339dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman}
3340