Expr.cpp revision 276b061970939293f1abaf694bd3ef05b2cbda79
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())
416900fc6388e803868a34b9483510c345e9b49d7ebBenjamin 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) {
13973e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  ArrayFillerOrUnionFieldInit = filler;
13983e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  // Fill out any "holes" in the array due to designated initializers.
13993e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  Expr **inits = getInits();
14003e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  for (unsigned i = 0, e = getNumInits(); i != e; ++i)
14013e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis    if (inits[i] == 0)
14023e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis      inits[i] = filler;
14033e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis}
14043e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis
1405c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted KremenekSourceRange InitListExpr::getSourceRange() const {
1406c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (SyntacticForm)
1407c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    return SyntacticForm->getSourceRange();
1408c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1409c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (Beg.isInvalid()) {
1410c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    // Find the first non-null initializer.
1411c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    for (InitExprsTy::const_iterator I = InitExprs.begin(),
1412c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek                                     E = InitExprs.end();
1413c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      I != E; ++I) {
1414c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      if (Stmt *S = *I) {
1415c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        Beg = S->getLocStart();
1416c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        break;
1417c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      }
1418c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    }
1419c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  }
1420c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (End.isInvalid()) {
1421c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    // Find the first non-null initializer from the end.
1422c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1423c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek                                             E = InitExprs.rend();
1424c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      I != E; ++I) {
1425c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      if (Stmt *S = *I) {
1426c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        End = S->getSourceRange().getEnd();
1427c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        break;
1428c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      }
1429c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    }
1430c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  }
1431c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  return SourceRange(Beg, End);
1432c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek}
1433c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek
1434bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block.
14354eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
14364eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroffconst FunctionType *BlockExpr::getFunctionType() const {
14376217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  return getType()->getAs<BlockPointerType>()->
1438183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                    getPointeeType()->getAs<FunctionType>();
14394eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
14404eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
14411eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSourceLocation BlockExpr::getCaretLocation() const {
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getCaretLocation();
144356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst Stmt *BlockExpr::getBody() const {
14457297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor  return TheBlock->getBody();
14467297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
14471eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt *BlockExpr::getBody() {
14481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getBody();
14497297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
145056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
145156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
14525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines
14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1456026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// isUnusedResultAWarning - Return true if this immediate expression should
1457026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// be warned about if the result is unused.  If so, fill in Loc and Ranges
1458026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// with location to warn on and the source range[s] to report with the
1459026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// warning.
1460026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattnerbool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
1461df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump                                  SourceRange &R2, ASTContext &Ctx) const {
1462ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // Don't warn if the expr is type dependent. The type could end up
1463ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // instantiating to void.
1464ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  if (isTypeDependent())
1465ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson    return false;
14661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
14685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
14690faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
14700faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
1471026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = getExprLoc();
1472026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
1473026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
14745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass:
1475026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return cast<ParenExpr>(this)->getSubExpr()->
1476df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      isUnusedResultAWarning(Loc, R1, R2, Ctx);
1477f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
1478f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()->
1479f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      isUnusedResultAWarning(Loc, R1, R2, Ctx);
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UnaryOperatorClass: {
14815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const UnaryOperator *UO = cast<UnaryOperator>(this);
14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (UO->getOpcode()) {
1484026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    default: break;
14852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostInc:
14862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostDec:
14872de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreInc:
14882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreDec:                 // ++/--
1489026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;  // Not a warning.
14902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Deref:
14915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Dereferencing a volatile pointer is a side-effect.
1492df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1493026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
1494026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
14952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Real:
14962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Imag:
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // accessing a piece of a volatile complex is a side-effect.
1498df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1499df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump          .isVolatileQualified())
1500026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
1501026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
15022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
1503df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
15045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1505026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = UO->getOperatorLoc();
1506026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = UO->getSubExpr()->getSourceRange();
1507026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1509e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  case BinaryOperatorClass: {
1510026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const BinaryOperator *BO = cast<BinaryOperator>(this);
1511c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek    switch (BO->getOpcode()) {
1512c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek      default:
1513c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        break;
151425973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider the RHS of comma for side effects. LHS was checked by
151525973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Sema::CheckCommaOperands.
15162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_Comma:
1517c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // ((foo = <blah>), 0) is an idiom for hiding the result (and
1518c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // lvalue-ness) of an assignment written in a macro.
1519c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        if (IntegerLiteral *IE =
1520c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek              dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1521c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek          if (IE->getValue() == 0)
1522c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek            return false;
152325973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
152425973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider '||', '&&' to have side effects if the LHS or RHS does.
15252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LAnd:
15262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LOr:
152725973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
152825973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis            !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
152925973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis          return false;
153025973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        break;
1531bf0ee354163f87623a4b60412544243911332343John McCall    }
1532026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (BO->isAssignmentOp())
1533026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1534026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = BO->getOperatorLoc();
1535026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = BO->getLHS()->getSourceRange();
1536026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = BO->getRHS()->getSourceRange();
1537026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1538e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  }
1539eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner  case CompoundAssignOperatorClass:
1540c6dfe194f623b02c123759f235b504d4850fc178Douglas Gregor  case VAArgExprClass:
1541276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  case AtomicExprClass:
1542026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
15435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1544ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  case ConditionalOperatorClass: {
1545fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // If only one of the LHS or RHS is a warning, the operator might
1546fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // be being used for control flow. Only warn if both the LHS and
1547fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // RHS are warnings.
1548ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1549fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1550fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek      return false;
1551fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    if (!Exp->getLHS())
1552026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return true;
1553fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1554ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  }
1555ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian
15565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case MemberExprClass:
1557026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If the base pointer or element is to a volatile pointer/field, accessing
1558026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // it is a side effect.
1559df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1560026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1561026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<MemberExpr>(this)->getMemberLoc();
1562026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = SourceRange(Loc, Loc);
1563026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1564026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ArraySubscriptExprClass:
15675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If the base pointer or element is to a volatile pointer/field, accessing
1568026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // it is a side effect.
1569df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    if (Ctx.getCanonicalType(getType()).isVolatileQualified())
1570026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
1571026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1572026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1573026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1574026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1575211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman
15769b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth  case CXXOperatorCallExprClass: {
15779b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // We warn about operator== and operator!= even when user-defined operator
15789b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // overloads as there is no reasonable way to define these such that they
15799b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // have non-trivial, desirable side-effects. See the -Wunused-comparison
15809b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // warning: these operators are commonly typo'ed, and so warning on them
15819b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // provides additional value as well. If this list is updated,
15829b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // DiagnoseUnusedComparison should be as well.
15839b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
15849b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    if (Op->getOperator() == OO_EqualEqual ||
15856e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay        Op->getOperator() == OO_ExclaimEqual) {
15866e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay      Loc = Op->getOperatorLoc();
15876e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay      R1 = Op->getSourceRange();
15889b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth      return true;
15896e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay    }
15909b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth
15919b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // Fallthrough for generic call handling.
15929b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth  }
15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case CallExprClass:
1594852871abbff45f1c1d3787755a27fce08365b166Eli Friedman  case CXXMemberCallExprClass: {
1595026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If this is a direct call, get the callee.
1596026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const CallExpr *CE = cast<CallExpr>(this);
1597d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    if (const Decl *FD = CE->getCalleeDecl()) {
1598026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // If the callee has attribute pure, const, or warn_unused_result, warn
1599026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // about it. void foo() { strlen("bar"); } should warn.
1600bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      //
1601bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1602bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // updated to match for QoI.
1603bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<WarnUnusedResultAttr>() ||
1604bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1605bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Loc = CE->getCallee()->getLocStart();
1606bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        R1 = CE->getCallee()->getSourceRange();
1607bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner
1608bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        if (unsigned NumArgs = CE->getNumArgs())
1609bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          R2 = SourceRange(CE->getArg(0)->getLocStart(),
1610bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner                           CE->getArg(NumArgs-1)->getLocEnd());
1611bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return true;
1612bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
1613026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    }
1614026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
1615026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner  }
161658beed91d468863b8c85bce43425422703838d27Anders Carlsson
161758beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXTemporaryObjectExprClass:
161858beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXConstructExprClass:
161958beed91d468863b8c85bce43425422703838d27Anders Carlsson    return false;
162058beed91d468863b8c85bce43425422703838d27Anders Carlsson
1621f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  case ObjCMessageExprClass: {
1622f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1623f85e193739c953358c865005855253af4f68a497John McCall    if (Ctx.getLangOptions().ObjCAutoRefCount &&
1624f85e193739c953358c865005855253af4f68a497John McCall        ME->isInstanceMessage() &&
1625f85e193739c953358c865005855253af4f68a497John McCall        !ME->getType()->isVoidType() &&
1626f85e193739c953358c865005855253af4f68a497John McCall        ME->getSelector().getIdentifierInfoForSlot(0) &&
1627f85e193739c953358c865005855253af4f68a497John McCall        ME->getSelector().getIdentifierInfoForSlot(0)
1628f85e193739c953358c865005855253af4f68a497John McCall                                               ->getName().startswith("init")) {
1629f85e193739c953358c865005855253af4f68a497John McCall      Loc = getExprLoc();
1630f85e193739c953358c865005855253af4f68a497John McCall      R1 = ME->getSourceRange();
1631f85e193739c953358c865005855253af4f68a497John McCall      return true;
1632f85e193739c953358c865005855253af4f68a497John McCall    }
1633f85e193739c953358c865005855253af4f68a497John McCall
1634f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMethodDecl *MD = ME->getMethodDecl();
1635f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1636f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      Loc = getExprLoc();
1637f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      return true;
1638f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    }
1639026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
1640f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  }
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
164212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  case ObjCPropertyRefExprClass:
16435e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    Loc = getExprLoc();
16445e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    R1 = getSourceRange();
1645a50089ec68a583d13718107c1b0c898f0903709eChris Lattner    return true;
164612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
1647611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  case StmtExprClass: {
1648611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // Statement exprs don't logically have side effects themselves, but are
1649611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // sometimes used in macros in ways that give them a type that is unused.
1650611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1651611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // however, if the result of the stmt expr is dead, we don't want to emit a
1652611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // warning.
1653611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1654d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    if (!CS->body_empty()) {
1655611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner      if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
1656df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump        return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1657d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis      if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1658d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis        if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1659d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis          return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1660d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    }
16611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16620faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
16630faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
1664026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<StmtExpr>(this)->getLParenLoc();
1665026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
1666026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
1667611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  }
16686eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor  case CStyleCastExprClass:
1669fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner    // If this is an explicit cast to void, allow it.  People do this when they
1670fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner    // think they know what they're doing :).
1671026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (getType()->isVoidType())
1672fb84664349ca6f37f5ec4df440f6c362cca62470Chris Lattner      return false;
1673026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1674026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1675026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
167658beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXFunctionalCastExprClass: {
16770faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
16780faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
167958beed91d468863b8c85bce43425422703838d27Anders Carlsson    const CastExpr *CE = cast<CastExpr>(this);
1680c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
168158beed91d468863b8c85bce43425422703838d27Anders Carlsson    // If this is a cast to void or a constructor conversion, check the operand.
168258beed91d468863b8c85bce43425422703838d27Anders Carlsson    // Otherwise, the result of the cast is unused.
16832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CE->getCastKind() == CK_ToVoid ||
16842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        CE->getCastKind() == CK_ConstructorConversion)
1685df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      return (cast<CastExpr>(this)->getSubExpr()
1686df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump              ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
1687026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1688026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1689026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
169058beed91d468863b8c85bce43425422703838d27Anders Carlsson  }
16911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16924be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman  case ImplicitCastExprClass:
16934be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman    // Check the operand, since implicit casts are inserted by Sema
1694df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<ImplicitCastExpr>(this)
1695df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
16964be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman
169704421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
1698df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXDefaultArgExpr>(this)
1699df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
17004c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
17014c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXNewExprClass:
17024c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // FIXME: In theory, there might be new expressions that don't have side
17034c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // effects (e.g. a placement new with an uninitialized POD).
17044c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXDeleteExprClass:
1705026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
17062d46eb21eb2c904831b0e9f75ab3523384c70e66Anders Carlsson  case CXXBindTemporaryExprClass:
1707df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXBindTemporaryExpr>(this)
1708df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
17094765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case ExprWithCleanupsClass:
17104765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall    return (cast<ExprWithCleanups>(this)
1711df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump            ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
17124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
17135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
171544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian/// isOBJCGCCandidate - Check if an expression is objc gc'able.
17167f4f86a2167abc116275e49c81350fc3225485e5Fariborz Jahanian/// returns true, if it is; false otherwise.
1717102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanianbool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
1718f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  const Expr *E = IgnoreParens();
1719f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  switch (E->getStmtClass()) {
172044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  default:
172144baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
172244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ObjCIvarRefExprClass:
172344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return true;
1724207c5210eb0ac7b632609f0c006eb97ef2738948Fariborz Jahanian  case Expr::UnaryOperatorClass:
1725f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
172644baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ImplicitCastExprClass:
1727f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
172803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass:
172903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
173003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                                      ->isOBJCGCCandidate(Ctx);
173106b89124a9a5971a0528cc9da6817740bac43164Fariborz Jahanian  case CStyleCastExprClass:
1732f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
1733e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian  case BlockDeclRefExprClass:
1734a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  case DeclRefExprClass: {
1735e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian
1736e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian    const Decl *D;
1737e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian    if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1738e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian        D = BDRE->getDecl();
1739e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian    else
1740e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian        D = cast<DeclRefExpr>(E)->getDecl();
1741e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian
1742102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1743102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      if (VD->hasGlobalStorage())
1744102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian        return true;
1745102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      QualType T = VD->getType();
174659a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // dereferencing to a  pointer is always a gc'able candidate,
174759a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // unless it is __weak.
17487e88a60d38b36695520e4f8d9279766ef111a662Daniel Dunbar      return T->isPointerType() &&
17490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall             (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
1750102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    }
175144baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
175244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
175383f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  case MemberExprClass: {
1754f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    const MemberExpr *M = cast<MemberExpr>(E);
1755102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return M->getBase()->isOBJCGCCandidate(Ctx);
175644baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
175744baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ArraySubscriptExprClass:
1758f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
175944baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
176044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian}
1761369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
176211ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidisbool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
176311ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis  if (isTypeDependent())
176411ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis    return false;
17657eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
176611ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis}
176711ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis
1768864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCallQualType Expr::findBoundMemberType(const Expr *expr) {
1769864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1770864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1771864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Bound member expressions are always one of these possibilities:
1772864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  //   x->m      x.m      x->*y      x.*y
1773864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // (possibly parenthesized)
1774864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1775864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  expr = expr->IgnoreParens();
1776864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1777864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1778864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return mem->getMemberDecl()->getType();
1779864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
1780864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1781864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1782864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1783864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall                      ->getPointeeType();
1784864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    assert(type->isFunctionType());
1785864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return type;
1786864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
1787864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1788864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  assert(isa<UnresolvedMemberExpr>(expr));
1789864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  return QualType();
1790864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall}
1791864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
1792369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1793369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                          Expr::CanThrowResult CT2) {
1794369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // CanThrowResult constants are ordered so that the maximum is the correct
1795369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // merge result.
1796369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return CT1 > CT2 ? CT1 : CT2;
1797369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1798369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1799369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1800369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr *E = const_cast<Expr*>(CE);
1801369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr::CanThrowResult R = Expr::CT_Cannot;
18027502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
1803369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1804369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1805369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return R;
1806369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1807369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
18087a614d8380297fcd2bc23986241905d97222948cRichard Smithstatic Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
18097a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           const Decl *D,
1810369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                           bool NullThrows = true) {
1811369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!D)
1812369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1813369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1814369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // See if we can get a function type from the decl somehow.
1815369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1816369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!VD) // If we have no clue what we're calling, assume the worst.
1817369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Can;
1818369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
18195221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  // As an extension, we assume that __attribute__((nothrow)) functions don't
18205221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  // throw.
18215221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl  if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
18225221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl    return Expr::CT_Cannot;
18235221d8f2da008689f7ff9476e6522bb2b63ec1a3Sebastian Redl
1824369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  QualType T = VD->getType();
1825369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const FunctionProtoType *FT;
1826369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if ((FT = T->getAs<FunctionProtoType>())) {
1827369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  } else if (const PointerType *PT = T->getAs<PointerType>())
1828369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1829369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1830369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1831369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1832369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1833369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1834369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1835369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1836369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!FT)
1837369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Can;
1838369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
18397a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (FT->getExceptionSpecType() == EST_Delayed) {
18407a614d8380297fcd2bc23986241905d97222948cRichard Smith    assert(isa<CXXConstructorDecl>(D) &&
18417a614d8380297fcd2bc23986241905d97222948cRichard Smith           "only constructor exception specs can be unknown");
18427a614d8380297fcd2bc23986241905d97222948cRichard Smith    Ctx.getDiagnostics().Report(E->getLocStart(),
18437a614d8380297fcd2bc23986241905d97222948cRichard Smith                                diag::err_exception_spec_unknown)
18447a614d8380297fcd2bc23986241905d97222948cRichard Smith      << E->getSourceRange();
18457a614d8380297fcd2bc23986241905d97222948cRichard Smith    return Expr::CT_Can;
18467a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
18477a614d8380297fcd2bc23986241905d97222948cRichard Smith
18488026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
1849369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1850369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1851369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1852369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (DC->isTypeDependent())
1853369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Dependent;
1854369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1855295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  if (!DC->getTypeAsWritten()->isReferenceType())
1856295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl    return Expr::CT_Cannot;
1857295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl
1858be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman  if (DC->getSubExpr()->isTypeDependent())
1859be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    return Expr::CT_Dependent;
1860be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman
1861369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1862369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1863369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1864369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redlstatic Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1865369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                                           const CXXTypeidExpr *DC) {
1866369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (DC->isTypeOperand())
1867369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1868369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1869369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  Expr *Op = DC->getExprOperand();
1870369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (Op->isTypeDependent())
1871369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Dependent;
1872369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1873369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  const RecordType *RT = Op->getType()->getAs<RecordType>();
1874369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!RT)
1875369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1876369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1877369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1878369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1879369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1880369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  if (Op->Classify(C).isPRValue())
1881369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return Expr::CT_Cannot;
1882369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1883369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  return Expr::CT_Can;
1884369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
1885369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1886369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian RedlExpr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1887369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  // C++ [expr.unary.noexcept]p3:
1888369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  //   [Can throw] if in a potentially-evaluated context the expression would
1889369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  //   contain:
1890369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  switch (getStmtClass()) {
1891369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXThrowExprClass:
1892369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated throw-expression
1893369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
1894369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1895369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDynamicCastExprClass: {
1896369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1897369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     where T is a reference type, that requires a run-time check
1898369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1899369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1900369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1901369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1902369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1903369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1904369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXTypeidExprClass:
1905369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated typeid expression applied to a glvalue
1906369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     expression whose type is a polymorphic class type
1907369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1908369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1909369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //   - a potentially evaluated call to a function, member function, function
1910369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     pointer, or member function pointer that does not have a non-throwing
1911369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    //     exception-specification
1912369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CallExprClass:
1913369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXOperatorCallExprClass:
1914369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXMemberCallExprClass: {
1915ebc93e176dad36fa8a28dd3a36c5b3dc7630d87dEli Friedman    const CallExpr *CE = cast<CallExpr>(this);
1916be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    CanThrowResult CT;
1917be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    if (isTypeDependent())
1918be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = CT_Dependent;
1919ebc93e176dad36fa8a28dd3a36c5b3dc7630d87dEli Friedman    else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1920ebc93e176dad36fa8a28dd3a36c5b3dc7630d87dEli Friedman      CT = CT_Cannot;
1921be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    else
19227a614d8380297fcd2bc23986241905d97222948cRichard Smith      CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
1923369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1924369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1925369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1926369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1927369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1928295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  case CXXConstructExprClass:
1929295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  case CXXTemporaryObjectExprClass: {
19307a614d8380297fcd2bc23986241905d97222948cRichard Smith    CanThrowResult CT = CanCalleeThrow(C, this,
1931369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl        cast<CXXConstructExpr>(this)->getConstructor());
1932369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1933369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1934369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1935369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1936369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1937369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXNewExprClass: {
1938be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    CanThrowResult CT;
1939be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    if (isTypeDependent())
1940be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = CT_Dependent;
1941be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    else
1942be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = MergeCanThrow(
19437a614d8380297fcd2bc23986241905d97222948cRichard Smith        CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
19447a614d8380297fcd2bc23986241905d97222948cRichard Smith        CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
1945369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl                       /*NullThrows*/false));
1946369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (CT == CT_Can)
1947369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT;
1948369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1949369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1950369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1951369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDeleteExprClass: {
1952be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    CanThrowResult CT;
1953be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1954be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    if (DTy.isNull() || DTy->isDependentType()) {
1955be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      CT = CT_Dependent;
1956be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman    } else {
19577a614d8380297fcd2bc23986241905d97222948cRichard Smith      CT = CanCalleeThrow(C, this,
19587a614d8380297fcd2bc23986241905d97222948cRichard Smith                          cast<CXXDeleteExpr>(this)->getOperatorDelete());
1959be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      if (const RecordType *RT = DTy->getAs<RecordType>()) {
1960be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman        const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
19617a614d8380297fcd2bc23986241905d97222948cRichard Smith        CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
19620b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      }
1963be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman      if (CT == CT_Can)
1964be57cf41fb55b48e3f889787960b3ac2eb5e4dbdEli Friedman        return CT;
19650b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    }
19660b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
19670b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl  }
19680b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl
19690b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl  case CXXBindTemporaryExprClass: {
19700b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    // The bound temporary has to be destroyed again, which might throw.
19717a614d8380297fcd2bc23986241905d97222948cRichard Smith    CanThrowResult CT = CanCalleeThrow(C, this,
19720b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
19730b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl    if (CT == CT_Can)
19740b34cf7399e61ef33dc5a3af405351822eeb5f3eSebastian Redl      return CT;
1975369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1976369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
1977369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1978369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // ObjC message sends are like function calls, but never have exception
1979369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // specs.
1980369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCMessageExprClass:
1981369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCPropertyRefExprClass:
1982369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
1983369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
1984369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Many other things have subexpressions, so we have to test those.
1985369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some are simple:
1986369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ParenExprClass:
1987369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case MemberExprClass:
1988369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXReinterpretCastExprClass:
1989369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXConstCastExprClass:
1990369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ConditionalOperatorClass:
1991369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CompoundLiteralExprClass:
1992369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ExtVectorElementExprClass:
1993369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case InitListExprClass:
1994369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case DesignatedInitExprClass:
1995369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ParenListExprClass:
1996369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case VAArgExprClass:
1997369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDefaultArgExprClass:
19984765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case ExprWithCleanupsClass:
1999369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCIvarRefExprClass:
2000369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ObjCIsaExprClass:
2001369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ShuffleVectorExprClass:
2002369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CanSubExprsThrow(C, this);
2003369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2004369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some might be dependent for other reasons.
2005369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case UnaryOperatorClass:
2006369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ArraySubscriptExprClass:
2007369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ImplicitCastExprClass:
2008369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CStyleCastExprClass:
2009369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXStaticCastExprClass:
2010369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXFunctionalCastExprClass:
2011369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case BinaryOperatorClass:
201203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case CompoundAssignOperatorClass:
201303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass: {
2014369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2015369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2016369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
2017369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2018369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2019369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case StmtExprClass:
2020369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Can;
2021369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2022369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case ChooseExprClass:
2023369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    if (isTypeDependent() || isValueDependent())
2024369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl      return CT_Dependent;
2025369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2026369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2027f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
2028f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (cast<GenericSelectionExpr>(this)->isResultDependent())
2029f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      return CT_Dependent;
2030f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2031f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
2032369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // Some expressions are always dependent.
2033369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case DependentScopeDeclRefExprClass:
2034369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXUnresolvedConstructExprClass:
2035369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  case CXXDependentScopeMemberExprClass:
2036369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Dependent;
2037369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
2038369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  default:
2039369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // All other expressions don't have subexpressions, or else they are
2040369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    // unevaluated.
2041369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl    return CT_Cannot;
2042369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  }
2043369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl}
2044369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
20454e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() {
20464e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  Expr* E = this;
2047b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara  while (true) {
2048b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2049b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      E = P->getSubExpr();
2050b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2051b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2052b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2053b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2054b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2055b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2056b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2057b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2058f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2059f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2060f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2061f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2062f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2063f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
2064b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
2065b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara  }
20664e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek}
20674e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek
206856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
206956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand.
207056f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() {
207156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  Expr *E = this;
207256f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  while (true) {
2073b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
207456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
2075b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2076b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2077b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
207856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
2079b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2080b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2081b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2082b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2083b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2084b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2085b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2086b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2087f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2088f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2089f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2090f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2091f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2092f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
209303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
209403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
209503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
209603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
209703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
2098c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2099c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2100c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2101c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2102c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2103b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
210456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  }
210556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner}
210656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner
21079c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
21089c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// casts.  This is intended purely as a temporary workaround for code
21099c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// that hasn't yet been rewritten to do the right thing about those
21109c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// casts, and may disappear along with the last internal use.
2111f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallExpr *Expr::IgnoreParenLValueCasts() {
2112f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  Expr *E = this;
21139c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall  while (true) {
2114f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2115f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      E = P->getSubExpr();
2116f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      continue;
21179c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall    } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2118f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      if (P->getCastKind() == CK_LValueToRValue) {
2119f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        E = P->getSubExpr();
2120f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        continue;
2121f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
21229c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall    } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
21239c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall      if (P->getOpcode() == UO_Extension) {
21249c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall        E = P->getSubExpr();
21259c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall        continue;
21269c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall      }
2127f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2128f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2129f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2130f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2131f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
213203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    } else if (MaterializeTemporaryExpr *Materialize
213303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
213403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
213503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
2136c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    } else if (SubstNonTypeTemplateParmExpr *NTTP
2137c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2138c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2139c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2140f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
2141f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
2142f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
2143f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return E;
2144f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
2145f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
21462fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCallExpr *Expr::IgnoreParenImpCasts() {
21472fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  Expr *E = this;
21482fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  while (true) {
2149b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
21502fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      E = P->getSubExpr();
2151b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2152b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2153b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
21542fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      E = P->getSubExpr();
2155b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2156b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2157b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2158b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2159b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2160b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2161b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2162b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2163f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2164f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2165f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2166f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2167f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2168f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
216903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
217003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
217103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
217203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
217303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
2174c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2175c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2176c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2177c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2178c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2179b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
21802fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  }
21812fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall}
21822fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall
21832f072b442879b8bba8c5dea11d7c61bedb1924aeHans WennborgExpr *Expr::IgnoreConversionOperator() {
21842f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
218514d251cd62942bf7d56bb87a267ba2ca2f7fae3eChandler Carruth    if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
21862f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg      return MCE->getImplicitObjectArgument();
21872f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  }
21882f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  return this;
21892f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg}
21902f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg
2191ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2192ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// value (including ptr->int casts of the same size).  Strip off any
2193ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// ParenExpr or CastExprs, returning their operand.
2194ecdd84147c0765caa999ddc22dde25b42712bb4dChris LattnerExpr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2195ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  Expr *E = this;
2196ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  while (true) {
2197ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2198ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      E = P->getSubExpr();
2199ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      continue;
2200ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    }
22011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2202ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2203ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      // We ignore integer <-> casts that are of the same width, ptr<->ptr and
22042ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor      // ptr<->int casts of the same width.  We also ignore all identity casts.
2205ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      Expr *SE = P->getSubExpr();
22061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2207ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2208ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
2209ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
2210ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
22111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2212b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if ((E->getType()->isPointerType() ||
22139d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           E->getType()->isIntegralType(Ctx)) &&
2214b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara          (SE->getType()->isPointerType() ||
22159d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           SE->getType()->isIntegralType(Ctx)) &&
2216ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner          Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2217ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
2218ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
2219ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
2220ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    }
22211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2222b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2223b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2224b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2225b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2226b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2227b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2228b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara
2229f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2230f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2231f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2232f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2233f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2234f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
2235f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
2236c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2237c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2238c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2239c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2240c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2241c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor
2242ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    return E;
2243ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  }
2244ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner}
2245ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
22466eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorbool Expr::isDefaultArgument() const {
22476eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  const Expr *E = this;
224803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
224903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = M->GetTemporaryExpr();
225003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
22516eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
22526eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    E = ICE->getSubExprAsWritten();
2253c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
22546eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return isa<CXXDefaultArgExpr>(E);
22556eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2256ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
22572f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// \brief Skip over any no-op casts and any temporary-binding
22582f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// expressions.
2259f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlssonstatic const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
226003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
226103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = M->GetTemporaryExpr();
226203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
22632f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
22642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
22652f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
22662f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
22672f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
22682f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
22692f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
22702f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
22712f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    E = BE->getSubExpr();
22722f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
22732f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
22742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
22752f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
22762f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
22772f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
22782f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
2279f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson
2280f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson  return E->IgnoreParens();
22812f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
22822f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
2283558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// isTemporaryObject - Determines if this expression produces a
2284558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// temporary of the given class type.
2285558d2abc7f9fd6801cc7677200992313ae90b5d8John McCallbool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2286558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2287558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    return false;
22882f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
2289f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson  const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
22902f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
229158277b57f9492d0234748be89bcad48b322c5cf7John McCall  // Temporaries are by definition pr-values of class type.
2292db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian  if (!E->Classify(C).isPRValue()) {
2293db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian    // In this context, property reference is a message call and is pr-value.
229412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    if (!isa<ObjCPropertyRefExpr>(E))
2295db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian      return false;
2296db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian  }
22972f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
229819e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // Black-list a few cases which yield pr-values of class type that don't
229919e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // refer to temporaries of that type:
230019e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall
230119e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // - implicit derived-to-base conversions
2302558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (isa<ImplicitCastExpr>(E)) {
2303558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2304558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_DerivedToBase:
2305558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_UncheckedDerivedToBase:
2306558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      return false;
2307558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    default:
2308558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      break;
2309558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    }
23102f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
23112f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
231219e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // - member expressions (all)
231319e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  if (isa<MemberExpr>(E))
231419e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall    return false;
231519e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall
231656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // - opaque values (all)
231756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (isa<OpaqueValueExpr>(E))
231856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return false;
231956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
2320558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  return true;
23212f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
23222f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
232375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregorbool Expr::isImplicitCXXThis() const {
232475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  const Expr *E = this;
232575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
232675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  // Strip away parentheses and casts we don't care about.
232775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  while (true) {
232875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
232975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      E = Paren->getSubExpr();
233075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      continue;
233175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
233275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
233375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
233475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      if (ICE->getCastKind() == CK_NoOp ||
233575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_LValueToRValue ||
233675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_DerivedToBase ||
233775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_UncheckedDerivedToBase) {
233875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        E = ICE->getSubExpr();
233975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        continue;
234075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      }
234175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
234275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
234375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
234475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      if (UnOp->getOpcode() == UO_Extension) {
234575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        E = UnOp->getSubExpr();
234675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        continue;
234775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      }
234875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
234975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
235003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (const MaterializeTemporaryExpr *M
235103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
235203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = M->GetTemporaryExpr();
235303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
235403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
235503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
235675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    break;
235775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  }
235875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
235975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
236075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    return This->isImplicit();
236175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
236275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  return false;
236375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor}
236475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
2365898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyTypeDependentArguments - Determines if any of the expressions
2366898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is type-dependent.
2367898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2368898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
2369898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isTypeDependent())
2370898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
2371898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2372898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
2373898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
2374898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2375898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyValueDependentArguments - Determines if any of the expressions
2376898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is value-dependent.
2377898574e7496ba8fd76290079d3a9d06954992734Douglas Gregorbool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2378898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  for (unsigned I = 0; I < NumExprs; ++I)
2379898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isValueDependent())
2380898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
2381898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2382898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
2383898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
2384898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
23854204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCallbool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
2386c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // This function is attempting whether an expression is an initializer
2387c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // which can be evaluated at compile-time.  isEvaluatable handles most
2388c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // of the cases, but it can't deal with some initializer-specific
2389c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // expressions, and it can't deal with aggregates; we deal with those here,
2390c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // and fall back to isEvaluatable for the other cases.
2391c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman
23924204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // If we ever capture reference-binding directly in the AST, we can
23934204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // kill the second parameter.
23944204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
23954204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  if (IsForRef) {
23964204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    EvalResult Result;
23974204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
23984204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  }
23991f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman
2400e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  switch (getStmtClass()) {
2401c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  default: break;
2402e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case StringLiteralClass:
240314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case ObjCStringLiteralClass:
2404eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  case ObjCEncodeExprClass:
2405e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    return true;
2406b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXTemporaryObjectExprClass:
2407b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXConstructExprClass: {
2408b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall    const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
24094204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
24104204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    // Only if it's
24114204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    // 1) an application of the trivial default constructor or
2412b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall    if (!CE->getConstructor()->isTrivial()) return false;
24134204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    if (!CE->getNumArgs()) return true;
24144204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
24154204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    // 2) an elidable trivial copy construction of an operand which is
24164204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    //    itself a constant initializer.  Note that we consider the
24174204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    //    operand on its own, *not* as a reference binding.
24184204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return CE->isElidable() &&
24194204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall           CE->getArg(0)->isConstantInitializer(Ctx, false);
2420b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  }
242159b5da6d853b4368b984700315adf7b37de05764Nate Begeman  case CompoundLiteralExprClass: {
24221f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // This handles gcc's extension that allows global initializers like
24231f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // "struct x {int x;} x = (struct x) {};".
24241f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This accepts other cases it shouldn't!
242559b5da6d853b4368b984700315adf7b37de05764Nate Begeman    const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
24264204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return Exp->isConstantInitializer(Ctx, false);
242759b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
2428e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case InitListExprClass: {
24291f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This doesn't deal with fields with reference types correctly.
24301f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This incorrectly allows pointers cast to integers to be assigned
24311f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // to bitfields.
2432e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    const InitListExpr *Exp = cast<InitListExpr>(this);
2433e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    unsigned numInits = Exp->getNumInits();
2434e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    for (unsigned i = 0; i < numInits; i++) {
24354204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
2436e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson        return false;
2437e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    }
2438c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    return true;
2439e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
24403498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  case ImplicitValueInitExprClass:
24413498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return true;
24423ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  case ParenExprClass:
24434204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return cast<ParenExpr>(this)->getSubExpr()
24444204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      ->isConstantInitializer(Ctx, IsForRef);
2445f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
2446f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (cast<GenericSelectionExpr>(this)->isResultDependent())
2447f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      return false;
2448f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()
2449f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      ->isConstantInitializer(Ctx, IsForRef);
24505cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara  case ChooseExprClass:
24515cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara    return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
24525cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara      ->isConstantInitializer(Ctx, IsForRef);
2453c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case UnaryOperatorClass: {
2454c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    const UnaryOperator* Exp = cast<UnaryOperator>(this);
24552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Exp->getOpcode() == UO_Extension)
24564204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
2457c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
2458c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  }
24593ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  case BinaryOperatorClass: {
24603ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    // Special case &&foo - &&bar.  It would be nice to generalize this somehow
24613ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    // but this handles the common case.
24623ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    const BinaryOperator *Exp = cast<BinaryOperator>(this);
24632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Exp->getOpcode() == BO_Sub &&
24643ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner        isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
24653ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner        isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
24663ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner      return true;
24673ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner    break;
24683ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  }
24694204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  case CXXFunctionalCastExprClass:
2470b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXStaticCastExprClass:
247181045d8dcd967def69d8e0945566214a9fe9ffccChris Lattner  case ImplicitCastExprClass:
2472c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case CStyleCastExprClass:
2473c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // Handle casts with a destination that's a struct or union; this
2474c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // deals with both the gcc no-op struct cast extension and the
2475c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    // cast-to-union extension.
2476c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    if (getType()->isRecordType())
24774204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return cast<CastExpr>(this)->getSubExpr()
24784204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall        ->isConstantInitializer(Ctx, false);
2479c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2480430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    // Integer->integer casts can be handled here, which is important for
2481430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    // things like (int)(&&x-&&y).  Scary but true.
2482430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner    if (getType()->isIntegerType() &&
2483430656e1c392dcd9f17fe91a495421d69fca1bc8Chris Lattner        cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
24844204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return cast<CastExpr>(this)->getSubExpr()
24854204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall        ->isConstantInitializer(Ctx, false);
2486c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2487c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
248803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
248903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass:
24905f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
249103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                            ->isConstantInitializer(Ctx, false);
2492e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
2493c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  return isEvaluatable(Ctx);
249438374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff}
249538374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
249682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
249782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// pointer constant or not, as well as the specific kind of constant detected.
249882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// Null pointer constants can be integer constant expressions with the
249982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// value zero, casts of zero to void*, nullptr (C++0X), or __null
250082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// (a GNU extension).
250182214a80c0163e01e4d8dec1426023c89277dbb4Chandler CarruthExpr::NullPointerConstantKind
250282214a80c0163e01e4d8dec1426023c89277dbb4Chandler CarruthExpr::isNullPointerConstant(ASTContext &Ctx,
250382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth                            NullPointerConstantValueDependence NPC) const {
2504ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  if (isValueDependent()) {
2505ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    switch (NPC) {
2506ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_NeverValueDependent:
2507b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unexpected value dependent expression!");
2508ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNull:
250982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      if (isTypeDependent() || getType()->isIntegralType(Ctx))
251082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth        return NPCK_ZeroInteger;
251182214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      else
251282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth        return NPCK_NotNull;
2513c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2514ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNotNull:
251582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      return NPCK_NotNull;
2516ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    }
2517ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  }
2518f515b2268f829adfbfdb751f54d102b53ed0285cDaniel Dunbar
25190777972d38a3125efed962b045704c30ae6965cfSebastian Redl  // Strip off a cast to void*, if it exists. Except in C++.
25200835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis  if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
25216215dee86c0e715b9f2b0d401ab2a5fcf629f1afSebastian Redl    if (!Ctx.getLangOptions().CPlusPlus) {
25220777972d38a3125efed962b045704c30ae6965cfSebastian Redl      // Check that it is a cast to void*.
25236217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
25240777972d38a3125efed962b045704c30ae6965cfSebastian Redl        QualType Pointee = PT->getPointeeType();
25250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        if (!Pointee.hasQualifiers() &&
25260777972d38a3125efed962b045704c30ae6965cfSebastian Redl            Pointee->isVoidType() &&                              // to void*
25270777972d38a3125efed962b045704c30ae6965cfSebastian Redl            CE->getSubExpr()->getType()->isIntegerType())         // from int.
2528ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor          return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
25290777972d38a3125efed962b045704c30ae6965cfSebastian Redl      }
25305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2531aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2532aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Ignore the ImplicitCastExpr type entirely.
2533ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
2534aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2535aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Accept ((void*)0) as a null pointer constant, as many other
2536aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // implementations do.
2537ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
2538f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  } else if (const GenericSelectionExpr *GE =
2539f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne               dyn_cast<GenericSelectionExpr>(this)) {
2540f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
25411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else if (const CXXDefaultArgExpr *DefaultArg
25428123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner               = dyn_cast<CXXDefaultArgExpr>(this)) {
254304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    // See through default argument expressions
2544ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
25452d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor  } else if (isa<GNUNullExpr>(this)) {
25462d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    // The GNU __null extension is always a null pointer constant.
254782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_GNUNull;
254803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  } else if (const MaterializeTemporaryExpr *M
254903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                   = dyn_cast<MaterializeTemporaryExpr>(this)) {
255003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
2551aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff  }
25522d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
25536e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // C++0x nullptr_t is always a null pointer constant.
25546e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  if (getType()->isNullPtrType())
255582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_CXX0X_nullptr;
25566e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2557ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian  if (const RecordType *UT = getType()->getAsUnionType())
2558ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian    if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2559ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2560ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian        const Expr *InitExpr = CLE->getInitializer();
2561ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian        if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2562ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian          return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2563ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      }
2564aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  // This expression must be an integer type.
2565c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  if (!getType()->isIntegerType() ||
256656fc0d18caf9c829647a5e3ce35197f0d7e0feeeFariborz Jahanian      (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
256782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_NotNull;
25681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have an integer constant expression, we need to *evaluate* it and
25705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // test for the value 0.
257109de1767990d4828bcaf0dd22033a5dddeecbe08Eli Friedman  llvm::APSInt Result;
257282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
257382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
257482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
25755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
257631a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff
2577f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// \brief If this expression is an l-value for an Objective C
2578f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// property, find the underlying property reference expression.
2579f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallconst ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2580f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  const Expr *E = this;
2581f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  while (true) {
2582f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    assert((E->getValueKind() == VK_LValue &&
2583f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall            E->getObjectKind() == OK_ObjCProperty) &&
2584f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall           "expression is not a property reference");
2585f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    E = E->IgnoreParenCasts();
2586f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2587f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      if (BO->getOpcode() == BO_Comma) {
2588f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        E = BO->getRHS();
2589f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        continue;
2590f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
2591f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
2592f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
2593f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
2594f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
2595f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
2596f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return cast<ObjCPropertyRefExpr>(E);
2597f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
2598f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
259933bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas GregorFieldDecl *Expr::getBitField() {
26006f4a69a3107e7ff1569c747f7c6bdf7cff8cbf55Douglas Gregor  Expr *E = this->IgnoreParens();
260133bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
2602de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2603f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (ICE->getCastKind() == CK_LValueToRValue ||
2604f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
2605de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      E = ICE->getSubExpr()->IgnoreParens();
2606de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    else
2607de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
2608de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  }
2609de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
261027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
261186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
261233bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor      if (Field->isBitField())
261333bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor        return Field;
261433bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
26150f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis  if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
26160f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis    if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
26170f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis      if (Field->isBitField())
26180f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis        return Field;
26190f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis
262042068e9d420889014ee26894e421fead336b239dEli Friedman  if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
262133bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor    if (BinOp->isAssignmentOp() && BinOp->getLHS())
262233bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor      return BinOp->getLHS()->getBitField();
262333bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
262442068e9d420889014ee26894e421fead336b239dEli Friedman    if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
262542068e9d420889014ee26894e421fead336b239dEli Friedman      return BinOp->getRHS()->getBitField();
262642068e9d420889014ee26894e421fead336b239dEli Friedman  }
262742068e9d420889014ee26894e421fead336b239dEli Friedman
262833bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor  return 0;
262927c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
263027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
2631093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlssonbool Expr::refersToVectorElement() const {
2632093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  const Expr *E = this->IgnoreParens();
2633c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2634093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
26355baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    if (ICE->getValueKind() != VK_RValue &&
26362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        ICE->getCastKind() == CK_NoOp)
2637093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      E = ICE->getSubExpr()->IgnoreParens();
2638093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    else
2639093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      break;
2640093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  }
2641c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
2642093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2643093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return ASE->getBase()->getType()->isVectorType();
2644093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
2645093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (isa<ExtVectorElementExpr>(E))
2646093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return true;
2647093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
2648093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  return false;
2649093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson}
2650093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
26512140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// isArrow - Return true if the base expression is a pointer to vector,
26522140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// return false if the base expression is a vector.
26532140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattnerbool ExtVectorElementExpr::isArrow() const {
26542140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner  return getBase()->getType()->isPointerType();
26552140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner}
26562140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner
2657213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const {
2658183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const VectorType *VT = getType()->getAs<VectorType>())
26598a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    return VT->getNumElements();
26608a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  return 1;
26614d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner}
26624d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner
26638a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated.
2664213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const {
2665a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // FIXME: Refactor this code to an accessor on the AST node which returns the
2666a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // "type" of component access, and share with code below and in Sema.
26675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Comp = Accessor->getName();
2668190d6a25393995b42e32086949a68285ee423fb9Nate Begeman
2669190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Halving swizzles do not contain duplicate elements.
2670150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
2671190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    return false;
26721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2673190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Advance past s-char prefix on hex swizzles.
2674150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
2675150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar    Comp = Comp.substr(1);
26761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2677150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  for (unsigned i = 0, e = Comp.size(); i != e; ++i)
26785f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
2679fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff        return true;
2680150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar
2681fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  return false;
2682fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff}
2683b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
26848a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
26853b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess(
26865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  SmallVectorImpl<unsigned> &Elts) const {
26875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Comp = Accessor->getName();
26884b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
26894b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar    Comp = Comp.substr(1);
26901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26914b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isHi =   Comp == "hi";
26924b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isLo =   Comp == "lo";
26934b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isEven = Comp == "even";
26944b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isOdd  = Comp == "odd";
26951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26968a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
26978a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    uint64_t Index;
26981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26998a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    if (isHi)
27008a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = e + i;
27018a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isLo)
27028a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = i;
27038a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isEven)
27048a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i;
27058a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isOdd)
27068a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i + 1;
27078a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else
27084b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar      Index = ExtVectorType::getAccessorIdx(Comp[i]);
2709b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
27103b8d116703db8018f855cbb4733ace426422623bNate Begeman    Elts.push_back(Index);
2711b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner  }
27128a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman}
27138a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman
271404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
2715f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
271604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
271704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation SuperLoc,
271804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 bool IsInstanceSuper,
271904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 QualType SuperType,
2720c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
2721207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
2722207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
272304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
27248d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
272504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
2726f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
2727bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*TypeDependent=*/false, /*ValueDependent=*/false,
2728561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*InstantiationDependent=*/false,
2729bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*ContainsUnexpandedParameterPack=*/false),
273004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
273104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2732b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(IsInstanceSuper? SuperInstance : SuperClass),
2733b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
2734207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
2735c2350e553b853ad00914faf23fa731e5fc4a8a5cDouglas Gregor{
2736207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
273704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(SuperType.getAsOpaquePtr());
273804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
273904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
274004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
2741f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
274204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
274304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 TypeSourceInfo *Receiver,
2744f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                 Selector Sel,
2745207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
2746207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
274704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
27488d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
274904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
2750f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
2751561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         T->isDependentType(), T->isInstantiationDependentType(),
2752561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         T->containsUnexpandedParameterPack()),
275304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
275404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2755b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(Class),
2756b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false),
2757207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
275804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor{
2759207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
276004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(Receiver);
276104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
276204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
276304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
2764f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
276504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
276604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 Expr *Receiver,
2767c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
2768207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
2769207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
277004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
27718d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
277204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation RBracLoc)
2773f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
2774bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Receiver->isTypeDependent(),
2775561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Receiver->isInstantiationDependent(),
2776bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Receiver->containsUnexpandedParameterPack()),
277704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
277804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
2779b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(Instance),
2780b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false),
2781207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
278204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor{
2783207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
278404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(Receiver);
2785207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2786207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2787207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidisvoid ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2788207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
2789207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                         SelectorLocationsKind SelLocsK) {
2790207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  setNumArgs(Args.size());
2791aa165f8458b51c546bebff947343e1a36f3594cbDouglas Gregor  Expr **MyArgs = getArgs();
27928d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis  for (unsigned I = 0; I != Args.size(); ++I) {
2793bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->isTypeDependent())
2794bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
2795bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->isValueDependent())
2796bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
2797561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[I]->isInstantiationDependent())
2798561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
2799bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->containsUnexpandedParameterPack())
2800bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
2801bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
2802bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    MyArgs[I] = Args[I];
2803bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
2804207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2805207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelLocsKind = SelLocsK;
2806207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  if (SelLocsK == SelLoc_NonStandard)
2807207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
280804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
280904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
281004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2811f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
281204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
281304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation SuperLoc,
281404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         bool IsInstanceSuper,
281504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         QualType SuperType,
2816c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
2817951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
281804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
28198d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
282004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2821207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelectorLocationsKind SelLocsK;
2822207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
2823f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
2824207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   SuperType, Sel, SelLocs, SelLocsK,
2825207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   Method, Args, RBracLoc);
282604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
282704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
282804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2829f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
283004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
283104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         TypeSourceInfo *Receiver,
2832c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
2833951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
283404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
28358d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
283604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2837207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelectorLocationsKind SelLocsK;
2838207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
2839951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
2840207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   SelLocs, SelLocsK, Method, Args, RBracLoc);
284104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
284204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
284304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2844f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
284504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
284604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         Expr *Receiver,
2847f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                         Selector Sel,
2848951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
284904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
28508d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
285104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation RBracLoc) {
2852207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelectorLocationsKind SelLocsK;
2853207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
2854951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
2855207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   SelLocs, SelLocsK, Method, Args, RBracLoc);
285604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
285704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
2858c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
2859207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                              unsigned NumArgs,
2860207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                              unsigned NumStoredSelLocs) {
2861207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
286204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
286304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
2864e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2865207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios KyrtzidisObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2866207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        ArrayRef<Expr *> Args,
2867207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        SourceLocation RBraceLoc,
2868207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        ArrayRef<SourceLocation> SelLocs,
2869207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        Selector Sel,
2870207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        SelectorLocationsKind &SelLocsK) {
2871207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2872207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2873207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                                               : 0;
2874207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  return alloc(C, Args.size(), NumStoredSelLocs);
2875207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2876207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2877207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios KyrtzidisObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2878207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        unsigned NumArgs,
2879207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        unsigned NumStoredSelLocs) {
2880207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2881207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2882207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  return (ObjCMessageExpr *)C.Allocate(Size,
2883207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                     llvm::AlignOf<ObjCMessageExpr>::Alignment);
2884207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2885207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2886207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidisvoid ObjCMessageExpr::getSelectorLocs(
2887207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                               SmallVectorImpl<SourceLocation> &SelLocs) const {
2888207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2889207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    SelLocs.push_back(getSelectorLoc(i));
2890207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
2891207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
2892e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios KyrtzidisSourceRange ObjCMessageExpr::getReceiverRange() const {
2893e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  switch (getReceiverKind()) {
2894e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case Instance:
2895e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getInstanceReceiver()->getSourceRange();
2896e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2897e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case Class:
2898e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2899e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2900e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case SuperInstance:
2901e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case SuperClass:
2902e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getSuperLoc();
2903e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  }
2904e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
2905e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  return SourceLocation();
2906e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis}
2907e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
290804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorSelector ObjCMessageExpr::getSelector() const {
290904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (HasMethod)
291004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
291104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                               ->getSelector();
2912c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  return Selector(SelectorOrMethod);
291304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
291404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
291504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
291604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  switch (getReceiverKind()) {
291704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case Instance:
291804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    if (const ObjCObjectPointerType *Ptr
291904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
292004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor      return Ptr->getInterfaceDecl();
292104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
29224df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
292304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case Class:
2924c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (const ObjCObjectType *Ty
2925c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          = getClassReceiver()->getAs<ObjCObjectType>())
2926c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Ty->getInterface();
292704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
29284df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
292904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case SuperInstance:
293004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    if (const ObjCObjectPointerType *Ptr
293104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          = getSuperType()->getAs<ObjCObjectPointerType>())
293204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor      return Ptr->getInterfaceDecl();
293304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
2934c2350e553b853ad00914faf23fa731e5fc4a8a5cDouglas Gregor
293504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case SuperClass:
2936ee8a6cafe8b69c316dd4fa5f6ea4838ffe15621cArgyrios Kyrtzidis    if (const ObjCObjectType *Iface
2937ee8a6cafe8b69c316dd4fa5f6ea4838ffe15621cArgyrios Kyrtzidis          = getSuperType()->getAs<ObjCObjectType>())
2938ee8a6cafe8b69c316dd4fa5f6ea4838ffe15621cArgyrios Kyrtzidis      return Iface->getInterface();
293904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    break;
294004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  }
29410389e6bd0159bfdd08f7c50a37543b6e3adf0c33Chris Lattner
294204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return 0;
2943eb3b324800598cc3d5385fbad95ae5cff2c79113Ted Kremenek}
29440389e6bd0159bfdd08f7c50a37543b6e3adf0c33Chris Lattner
29455f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef ObjCBridgedCastExpr::getBridgeKindName() const {
2946f85e193739c953358c865005855253af4f68a497John McCall  switch (getBridgeKind()) {
2947f85e193739c953358c865005855253af4f68a497John McCall  case OBC_Bridge:
2948f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge";
2949f85e193739c953358c865005855253af4f68a497John McCall  case OBC_BridgeTransfer:
2950f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge_transfer";
2951f85e193739c953358c865005855253af4f68a497John McCall  case OBC_BridgeRetained:
2952f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge_retained";
2953f85e193739c953358c865005855253af4f68a497John McCall  }
2954f85e193739c953358c865005855253af4f68a497John McCall
2955f85e193739c953358c865005855253af4f68a497John McCall  return "__bridge";
2956f85e193739c953358c865005855253af4f68a497John McCall}
2957f85e193739c953358c865005855253af4f68a497John McCall
29584ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ChooseExpr::isConditionTrue(const ASTContext &C) const {
2959a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  return getCond()->EvaluateKnownConstInt(C) != 0;
296027437caadea35f84d550cd29f024fcf3ea240eecChris Lattner}
296127437caadea35f84d550cd29f024fcf3ea240eecChris Lattner
2962bebbe0d9b7568ce43a464286bee49429489ef483Douglas GregorShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2963bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor                                     QualType Type, SourceLocation BLoc,
2964bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor                                     SourceLocation RP)
2965bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor   : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2966bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          Type->isDependentType(), Type->isDependentType(),
2967561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          Type->isInstantiationDependentType(),
2968bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          Type->containsUnexpandedParameterPack()),
2969bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor     BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2970bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor{
2971bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  SubExprs = new (C) Stmt*[nexpr];
2972bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  for (unsigned i = 0; i < nexpr; i++) {
2973bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
2974bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
2975bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
2976bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
2977561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
2978561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
2979bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
2980bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
2981bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
2982bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    SubExprs[i] = args[i];
2983bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
2984bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor}
2985bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
2986888376a2bbcfc2f047902249f8455918e2489ae1Nate Begemanvoid ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2987888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman                                 unsigned NumExprs) {
2988888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman  if (SubExprs) C.Deallocate(SubExprs);
2989888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
2990888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman  SubExprs = new (C) Stmt* [NumExprs];
299194cd5d1397bb1a8bcd109602aa38dd787b164c22Douglas Gregor  this->NumExprs = NumExprs;
299294cd5d1397bb1a8bcd109602aa38dd787b164c22Douglas Gregor  memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
29931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2994888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
2995f111d935722ed488144600cea5ed03a6b5069e8fPeter CollingbourneGenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2996f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation GenericLoc, Expr *ControllingExpr,
2997f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2998f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned NumAssocs, SourceLocation DefaultLoc,
2999f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation RParenLoc,
3000f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               bool ContainsUnexpandedParameterPack,
3001f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned ResultIndex)
3002f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  : Expr(GenericSelectionExprClass,
3003f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getType(),
3004f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getValueKind(),
3005f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getObjectKind(),
3006f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->isTypeDependent(),
3007f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->isValueDependent(),
3008561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         AssocExprs[ResultIndex]->isInstantiationDependent(),
3009f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         ContainsUnexpandedParameterPack),
3010f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3011f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3012f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3013f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    RParenLoc(RParenLoc) {
3014f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  SubExprs[CONTROLLING] = ControllingExpr;
3015f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3016f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3017f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne}
3018f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
3019f111d935722ed488144600cea5ed03a6b5069e8fPeter CollingbourneGenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3020f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation GenericLoc, Expr *ControllingExpr,
3021f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3022f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned NumAssocs, SourceLocation DefaultLoc,
3023f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation RParenLoc,
3024f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               bool ContainsUnexpandedParameterPack)
3025f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  : Expr(GenericSelectionExprClass,
3026f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         Context.DependentTy,
3027f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         VK_RValue,
3028f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         OK_Ordinary,
3029561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isTypeDependent=*/true,
3030561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isValueDependent=*/true,
3031561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isInstantiationDependent=*/true,
3032f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         ContainsUnexpandedParameterPack),
3033f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3034f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3035f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3036f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    RParenLoc(RParenLoc) {
3037f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  SubExprs[CONTROLLING] = ControllingExpr;
3038f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3039f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3040f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne}
3041f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
304277ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
304305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//  DesignatedInitExpr
304405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
304505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3046b11382497a923b0d7009e85a1d8eb7bf93ec6d0dChandler CarruthIdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
304705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(Kind == FieldDesignator && "Only valid on a field designator");
304805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Field.NameOrField & 0x01)
304905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
305005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  else
305105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return getField()->getIdentifier();
305205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
305305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3054c302113179a1c2b1254224ea9b6f5316ceeb375cSean HuntDesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
3055319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                       unsigned NumDesignators,
3056ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       const Designator *Designators,
30571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       SourceLocation EqualOrColonLoc,
3058ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       bool GNUSyntax,
30591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       Expr **IndexExprs,
30609ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                       unsigned NumIndexExprs,
30619ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                       Expr *Init)
30621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(DesignatedInitExprClass, Ty,
3063f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall         Init->getValueKind(), Init->getObjectKind(),
3064bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Init->isTypeDependent(), Init->isValueDependent(),
3065561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Init->isInstantiationDependent(),
3066bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Init->containsUnexpandedParameterPack()),
30671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
30681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
3069319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  this->Designators = new (C) Designator[NumDesignators];
30709ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
30719ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Record the initializer itself.
30727502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  child_range Child = children();
30739ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  *Child++ = Init;
30749ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
30759ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Copy the designators and their subexpressions, computing
30769ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // value-dependence along the way.
30779ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  unsigned IndexIdx = 0;
30789ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  for (unsigned I = 0; I != NumDesignators; ++I) {
3079ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    this->Designators[I] = Designators[I];
30809ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
30819ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    if (this->Designators[I].isArrayDesignator()) {
30829ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
30839ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Index = IndexExprs[IndexIdx];
3084bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Index->isTypeDependent() || Index->isValueDependent())
3085bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ValueDependent = true;
3086561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      if (Index->isInstantiationDependent())
3087561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3088bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      // Propagate unexpanded parameter packs.
3089bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Index->containsUnexpandedParameterPack())
3090bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ContainsUnexpandedParameterPack = true;
30919ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
30929ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the index expressions into permanent storage.
30939ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
30949ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    } else if (this->Designators[I].isArrayRangeDesignator()) {
30959ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
30969ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Start = IndexExprs[IndexIdx];
30979ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *End = IndexExprs[IndexIdx + 1];
3098bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Start->isTypeDependent() || Start->isValueDependent() ||
3099561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          End->isTypeDependent() || End->isValueDependent()) {
3100bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ValueDependent = true;
3101561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3102561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      } else if (Start->isInstantiationDependent() ||
3103561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                 End->isInstantiationDependent()) {
3104561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3105561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      }
3106561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
3107bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      // Propagate unexpanded parameter packs.
3108bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Start->containsUnexpandedParameterPack() ||
3109bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          End->containsUnexpandedParameterPack())
3110bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ContainsUnexpandedParameterPack = true;
31119ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
31129ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the start/end expressions into permanent storage.
31139ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
31149ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
31159ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    }
31169ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  }
31179ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
31189ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
3119ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
3120ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
312105c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr *
31221eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
312305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           unsigned NumDesignators,
312405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           Expr **IndexExprs, unsigned NumIndexExprs,
312505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           SourceLocation ColonOrEqualLoc,
312605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           bool UsesColonSyntax, Expr *Init) {
3127c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3128c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3129319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
31309ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                      ColonOrEqualLoc, UsesColonSyntax,
31319ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                      IndexExprs, NumIndexExprs, Init);
313205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
313305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
31341eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
3135d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                                    unsigned NumIndexExprs) {
3136d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3137d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3138d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3139d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
3140d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
3141319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregorvoid DesignatedInitExpr::setDesignators(ASTContext &C,
3142319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                        const Designator *Desigs,
3143d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                        unsigned NumDesigs) {
3144319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  Designators = new (C) Designator[NumDesigs];
3145d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  NumDesignators = NumDesigs;
3146d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  for (unsigned I = 0; I != NumDesigs; ++I)
3147d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor    Designators[I] = Desigs[I];
3148d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
3149d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
315024f4674e697fb53587f0e8485e9c6592f7021ef2Abramo BagnaraSourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
315124f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
315224f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  if (size() == 1)
315324f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara    return DIE->getDesignator(0)->getSourceRange();
315424f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  return SourceRange(DIE->getDesignator(0)->getStartLocation(),
315524f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara                     DIE->getDesignator(size()-1)->getEndLocation());
315624f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara}
315724f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara
315805c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorSourceRange DesignatedInitExpr::getSourceRange() const {
315905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation StartLoc;
3160d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner  Designator &First =
3161d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    *const_cast<DesignatedInitExpr*>(this)->designators_begin();
316205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (First.isFieldDesignator()) {
3163eeae8f072748affce25ab4064982626361293390Douglas Gregor    if (GNUSyntax)
316405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
316505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else
316605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
316705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  } else
3168d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    StartLoc =
3169d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner      SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
317005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
317105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
317205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
317305c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
317405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
317505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
317605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
317705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
317805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
317905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
318005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
318105c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
31821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
318305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
318405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
318505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
318605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
318705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
318805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
318905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
319005c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorExpr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
31911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
319205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
319305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  char* Ptr = static_cast<char*>(static_cast<void *>(this));
319405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
319505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
319605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
319705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
319805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3199ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// \brief Replaces the designator at index @p Idx with the series
3200ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// of designators in [First, Last).
3201319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregorvoid DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
32021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          const Designator *First,
3203ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                          const Designator *Last) {
3204ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  unsigned NumNewDesignators = Last - First;
3205ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  if (NumNewDesignators == 0) {
3206ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    std::copy_backward(Designators + Idx + 1,
3207ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + NumDesignators,
3208ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + Idx);
3209ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    --NumNewDesignators;
3210ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
3211ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  } else if (NumNewDesignators == 1) {
3212ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    Designators[Idx] = *First;
3213ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
3214ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
3215ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
32161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Designator *NewDesignators
3217319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor    = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
3218ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators, Designators + Idx, NewDesignators);
3219ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(First, Last, NewDesignators + Idx);
3220ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators + Idx + 1, Designators + NumDesignators,
3221ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor            NewDesignators + Idx + NumNewDesignators);
3222ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  Designators = NewDesignators;
3223ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  NumDesignators = NumDesignators - 1 + NumNewDesignators;
3224ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
3225ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
32261eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
32272ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman                             Expr **exprs, unsigned nexprs,
32280d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek                             SourceLocation rparenloc, QualType T)
32290d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek  : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
3230561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         false, false, false, false),
3231bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
32320d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek  assert(!T.isNull() && "ParenListExpr must have a valid type");
32332ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman  Exprs = new (C) Stmt*[nexprs];
3234bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  for (unsigned i = 0; i != nexprs; ++i) {
3235bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->isTypeDependent())
3236bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
3237bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->isValueDependent())
3238bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
3239561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (exprs[i]->isInstantiationDependent())
3240561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
3241bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->containsUnexpandedParameterPack())
3242bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
3243bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
32442ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    Exprs[i] = exprs[i];
3245bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
32462ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman}
32472ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman
3248e996ffd240f20a1048179d7727a6ee3227261921John McCallconst OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3249e996ffd240f20a1048179d7727a6ee3227261921John McCall  if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3250e996ffd240f20a1048179d7727a6ee3227261921John McCall    e = ewc->getSubExpr();
325103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
325203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    e = m->GetTemporaryExpr();
3253e996ffd240f20a1048179d7727a6ee3227261921John McCall  e = cast<CXXConstructExpr>(e)->getArg(0);
3254e996ffd240f20a1048179d7727a6ee3227261921John McCall  while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3255e996ffd240f20a1048179d7727a6ee3227261921John McCall    e = ice->getSubExpr();
3256e996ffd240f20a1048179d7727a6ee3227261921John McCall  return cast<OpaqueValueExpr>(e);
3257e996ffd240f20a1048179d7727a6ee3227261921John McCall}
3258e996ffd240f20a1048179d7727a6ee3227261921John McCall
325905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
3260ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//  ExprIterator.
3261ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
3262ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
3263ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3264ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3265ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3266ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator[](size_t idx) const {
3267ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  return cast<Expr>(I[idx]);
3268ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek}
3269ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3270ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3271ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
3272ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
327377ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//  Child Iterators for iterating over subexpressions/substatements
327477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
327577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
3276f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne// UnaryExprOrTypeTraitExpr
3277f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter CollingbourneStmt::child_range UnaryExprOrTypeTraitExpr::children() {
32780518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // If this is of a type and the type is a VLA type (and not a typedef), the
32790518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // size expression of the VLA needs to be treated as an executable expression.
32800518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // Why isn't this weirdness documented better in StmtIterator?
32810518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType()) {
3282f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
32830518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl                                   getArgumentType().getTypePtr()))
328463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      return child_range(child_iterator(T), child_iterator());
328563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return child_range();
32860518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  }
328763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return child_range(&Argument.Ex, &Argument.Ex + 1);
32889ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
3289390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
3290563477da25f824e37c535131695dc4dc9b68c465Steve Naroff// ObjCMessageExpr
329163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallStmt::child_range ObjCMessageExpr::children() {
329263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  Stmt **begin;
329304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (getReceiverKind() == Instance)
329463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    begin = reinterpret_cast<Stmt **>(this + 1);
329563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  else
329663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    begin = reinterpret_cast<Stmt **>(getArgs());
329763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return child_range(begin,
329863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                     reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
3299563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
3300563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
33014eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff// Blocks
33026b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallBlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
3303a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor                                   SourceLocation l, bool ByRef,
33046b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                                   bool constAdded)
3305561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
3306a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor         d->isParameterPack()),
33076b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
3308a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor{
3309d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool TypeDependent = false;
3310d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool ValueDependent = false;
3311561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  bool InstantiationDependent = false;
3312561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3313561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                           InstantiationDependent);
3314d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.TypeDependent = TypeDependent;
3315d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.ValueDependent = ValueDependent;
3316561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  ExprBits.InstantiationDependent = InstantiationDependent;
3317a779d9ca2fdf1247f65de0e6acf2870d8be53ccdDouglas Gregor}
3318