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
14a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner#include "clang/AST/APValue.h"
152eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner#include "clang/AST/ASTContext.h"
162fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/Attr.h"
1798cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor#include "clang/AST/DeclCXX.h"
182fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/DeclObjC.h"
19aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
2025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor#include "clang/AST/EvaluatedExprVisitor.h"
212fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/Expr.h"
222fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/ExprCXX.h"
23bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer#include "clang/AST/Mangle.h"
2419cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/StmtVisitor.h"
261b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
273f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose#include "clang/Basic/CharInfo.h"
2808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner#include "clang/Basic/SourceManager.h"
29da5a6b6d9fd52899499d5b7b46273ec844dcaa6eChris Lattner#include "clang/Basic/TargetInfo.h"
302fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/Lex/Lexer.h"
312fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/Lex/LiteralSupport.h"
322fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/Sema/SemaDiagnostic.h"
33cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor#include "llvm/Support/ErrorHandling.h"
343a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson#include "llvm/Support/raw_ostream.h"
35ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor#include <algorithm>
3664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman#include <cstring>
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
398d852e35adb46e0799538dfc9c80d44f27cd3597Rafael Espindolaconst CXXRecordDecl *Expr::getBestDynamicClassType() const {
40632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola  const Expr *E = ignoreParenBaseCasts();
410b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola
420b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  QualType DerivedType = E->getType();
430b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  if (const PointerType *PTy = DerivedType->getAs<PointerType>())
440b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola    DerivedType = PTy->getPointeeType();
450b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola
46251c449b280eb845017a6c022ed7189d17c63d49Rafael Espindola  if (DerivedType->isDependentType())
476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
48251c449b280eb845017a6c022ed7189d17c63d49Rafael Espindola
490b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  const RecordType *Ty = DerivedType->castAs<RecordType>();
500b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  Decl *D = Ty->getDecl();
510b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  return cast<CXXRecordDecl>(D);
520b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola}
530b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola
544e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smithconst Expr *Expr::skipRValueSubobjectAdjustments(
554e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith    SmallVectorImpl<const Expr *> &CommaLHSs,
564e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith    SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
570a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  const Expr *E = this;
580a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  while (true) {
590a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    E = E->IgnoreParens();
600a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
610a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
620a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      if ((CE->getCastKind() == CK_DerivedToBase ||
630a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola           CE->getCastKind() == CK_UncheckedDerivedToBase) &&
640a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola          E->getType()->isRecordType()) {
650a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        E = CE->getSubExpr();
660a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        CXXRecordDecl *Derived
670a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola          = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
680a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        Adjustments.push_back(SubobjectAdjustment(CE, Derived));
690a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        continue;
700a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      }
710a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
720a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      if (CE->getCastKind() == CK_NoOp) {
730a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        E = CE->getSubExpr();
740a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        continue;
750a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      }
760a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
77d6b698739ab157348acafcec5b06a05d3d35377dRichard Smith      if (!ME->isArrow()) {
780a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        assert(ME->getBase()->getType()->isRecordType());
790a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
80d6b698739ab157348acafcec5b06a05d3d35377dRichard Smith          if (!Field->isBitField() && !Field->getType()->isReferenceType()) {
81d1b55dcf6bd61d4350b01e15590bde9bb5a334bbRichard Smith            E = ME->getBase();
82d1b55dcf6bd61d4350b01e15590bde9bb5a334bbRichard Smith            Adjustments.push_back(SubobjectAdjustment(Field));
83d1b55dcf6bd61d4350b01e15590bde9bb5a334bbRichard Smith            continue;
84d1b55dcf6bd61d4350b01e15590bde9bb5a334bbRichard Smith          }
850a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        }
860a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      }
870a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
880a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      if (BO->isPtrMemOp()) {
89ef4b666e841e3917385892713612888ec6c3a056Rafael Espindola        assert(BO->getRHS()->isRValue());
900a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        E = BO->getLHS();
910a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        const MemberPointerType *MPT =
920a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola          BO->getRHS()->getType()->getAs<MemberPointerType>();
930a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
944e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith        continue;
954e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith      } else if (BO->getOpcode() == BO_Comma) {
964e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith        CommaLHSs.push_back(BO->getLHS());
974e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith        E = BO->getRHS();
984e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith        continue;
990a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      }
1000a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    }
1010a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
1020a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    // Nothing changed.
1030a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    break;
1040a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  }
1050a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  return E;
1060a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola}
1070a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
1082b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// isKnownToHaveBooleanValue - Return true if this is an integer expression
1092b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// that is known to return 0 or 1.  This happens for _Bool/bool expressions
1102b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// but also int expressions which are produced by things like comparisons in
1112b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// C.
1122b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattnerbool Expr::isKnownToHaveBooleanValue() const {
113f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  const Expr *E = IgnoreParens();
114f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
1152b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  // If this value has _Bool type, it is obvious 0/1.
116f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (E->getType()->isBooleanType()) return true;
117c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  // If this is a non-scalar-integer type, we don't care enough to try.
118f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (!E->getType()->isIntegralOrEnumerationType()) return false;
119c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
120f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1212b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    switch (UO->getOpcode()) {
1222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Plus:
1232b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return UO->getSubExpr()->isKnownToHaveBooleanValue();
1246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    case UO_LNot:
1256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return true;
1262b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    default:
1272b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return false;
1282b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
1292b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  }
130c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1316907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  // Only look through implicit casts.  If the user writes
1326907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  // '(int) (a && b)' treat it as an arbitrary int.
133f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
1342b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return CE->getSubExpr()->isKnownToHaveBooleanValue();
135c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
136f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
1372b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    switch (BO->getOpcode()) {
1382b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    default: return false;
1392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:   // Relational operators.
1402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
1412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
1422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
1432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:   // Equality operators.
1442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
1452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LAnd: // AND operator.
1462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LOr:  // Logical OR operator.
1472b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return true;
148c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_And:  // Bitwise AND operator.
1502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Xor:  // Bitwise XOR operator.
1512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Or:   // Bitwise OR operator.
1522b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      // Handle things like (x==2)|(y==12).
1532b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return BO->getLHS()->isKnownToHaveBooleanValue() &&
1542b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner             BO->getRHS()->isKnownToHaveBooleanValue();
155c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Comma:
1572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Assign:
1582b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return BO->getRHS()->isKnownToHaveBooleanValue();
1592b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
1602b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  }
161c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
162f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
1632b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
1642b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner           CO->getFalseExpr()->isKnownToHaveBooleanValue();
165c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1662b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  return false;
1672b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner}
1682b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner
16963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall// Amusing macro metaprogramming hack: check whether a class provides
17063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall// a more specific implementation of getExprLoc().
17190e25a8b2cc5d006e4ced052bcdb40c8af999456Daniel Dunbar//
17290e25a8b2cc5d006e4ced052bcdb40c8af999456Daniel Dunbar// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
17363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallnamespace {
17463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// This implementation is used when a class provides a custom
17563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// implementation of getExprLoc.
17663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  template <class E, class T>
17763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  SourceLocation getExprLocImpl(const Expr *expr,
17863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                                SourceLocation (T::*v)() const) {
17963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return static_cast<const E*>(expr)->getExprLoc();
18063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
18163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
18263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// This implementation is used when a class doesn't provide
18363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// a custom implementation of getExprLoc.  Overload resolution
18463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// should pick it over the implementation above because it's
18563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// more specialized according to function template partial ordering.
18663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  template <class E>
18763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  SourceLocation getExprLocImpl(const Expr *expr,
18863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                                SourceLocation (Expr::*v)() const) {
18990e25a8b2cc5d006e4ced052bcdb40c8af999456Daniel Dunbar    return static_cast<const E*>(expr)->getLocStart();
19063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
19163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall}
19263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
19363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallSourceLocation Expr::getExprLoc() const {
19463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  switch (getStmtClass()) {
19563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::NoStmtClass: llvm_unreachable("statement without class");
19663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(type)
19763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define STMT(type, base) \
198176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case Stmt::type##Class: break;
19963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define EXPR(type, base) \
20063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
20163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#include "clang/AST/StmtNodes.inc"
20263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
203176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  llvm_unreachable("unknown expression kind");
20463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall}
20563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Expressions.
2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
210561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor/// \brief Compute the type-, value-, and instantiation-dependence of a
211561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor/// declaration reference
212d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor/// based on the declaration being referenced.
2139db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig Topperstatic void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
2149db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig Topper                                     QualType T, bool &TypeDependent,
215561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                     bool &ValueDependent,
216561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                     bool &InstantiationDependent) {
217d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  TypeDependent = false;
218d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ValueDependent = false;
219561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  InstantiationDependent = false;
2200da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
2210da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  // (TD) C++ [temp.dep.expr]p3:
2220da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //   An id-expression is type-dependent if it contains:
2230da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //
224176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // and
2250da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //
2260da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  // (VD) C++ [temp.dep.constexpr]p2:
2270da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  An identifier is value-dependent if it is:
228176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
2290da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - an identifier that was declared with dependent type
2300da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD)  - a name declared with a dependent type,
231d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (T->isDependentType()) {
232d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    TypeDependent = true;
233d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
234561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
235d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
236561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  } else if (T->isInstantiationDependentType()) {
237561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
2380da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
239d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
2400da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - a conversion-function-id that specifies a dependent type
241d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (D->getDeclName().getNameKind()
242561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                == DeclarationName::CXXConversionFunctionName) {
243561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    QualType T = D->getDeclName().getCXXNameType();
244561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (T->isDependentType()) {
245561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      TypeDependent = true;
246561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ValueDependent = true;
247561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
248561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      return;
249561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
250561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
251561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (T->isInstantiationDependentType())
252561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
2530da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
254561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
2550da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD)  - the name of a non-type template parameter,
256d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (isa<NonTypeTemplateParmDecl>(D)) {
257d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
258561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
259d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
260d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
261d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
2620da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD) - a constant with integral or enumeration type and is
2630da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //         initialized with an expression that is value-dependent.
264db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //  (VD) - a constant with literal type and is initialized with an
265db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //         expression that is value-dependent [C++11].
266db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //  (VD) - FIXME: Missing from the standard:
267db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //       -  an entity with reference type and is initialized with an
268db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //          expression that is value-dependent [C++11]
269d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
27080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if ((Ctx.getLangOpts().CPlusPlus11 ?
271a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith           Var->getType()->isLiteralType(Ctx) :
272db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith           Var->getType()->isIntegralOrEnumerationType()) &&
2734ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie        (Var->getType().isConstQualified() ||
274db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith         Var->getType()->isReferenceType())) {
27531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      if (const Expr *Init = Var->getAnyInitializer())
276561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        if (Init->isValueDependent()) {
277d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor          ValueDependent = true;
278561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          InstantiationDependent = true;
279561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        }
280db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith    }
281db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith
282bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    // (VD) - FIXME: Missing from the standard:
283bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    //      -  a member function or a static data member of the current
284bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    //         instantiation
285db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith    if (Var->isStaticDataMember() &&
286db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith        Var->getDeclContext()->isDependentContext()) {
287d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor      ValueDependent = true;
288561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
289418220b209ed67a5cc6802d367a9a994d7225e04Richard Smith      TypeSourceInfo *TInfo = Var->getFirstDecl()->getTypeSourceInfo();
290418220b209ed67a5cc6802d367a9a994d7225e04Richard Smith      if (TInfo->getType()->isIncompleteArrayType())
291418220b209ed67a5cc6802d367a9a994d7225e04Richard Smith        TypeDependent = true;
292561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
293d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
294d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
295d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
296d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
297bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  // (VD) - FIXME: Missing from the standard:
298bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  //      -  a member function or a static data member of the current
299bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  //         instantiation
300d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
301d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
302561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
303db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  }
304d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor}
305d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
3069db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig Toppervoid DeclRefExpr::computeDependence(const ASTContext &Ctx) {
307d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool TypeDependent = false;
308d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool ValueDependent = false;
309561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  bool InstantiationDependent = false;
3103d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar  computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
3113d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar                           ValueDependent, InstantiationDependent);
312176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
313176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ExprBits.TypeDependent |= TypeDependent;
314176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ExprBits.ValueDependent |= ValueDependent;
315176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ExprBits.InstantiationDependent |= InstantiationDependent;
316176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
31710738d36b150aa65206890c1c845cdba076e4200Douglas Gregor  // Is the declaration a parameter pack?
318d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (getDecl()->isParameterPack())
3191fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor    ExprBits.ContainsUnexpandedParameterPack = true;
3200da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor}
3210da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
3229db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperDeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
3233d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar                         NestedNameSpecifierLoc QualifierLoc,
324e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         SourceLocation TemplateKWLoc,
3250e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                         ValueDecl *D, bool RefersToEnclosingVariableOrCapture,
326f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                         const DeclarationNameInfo &NameInfo,
3273aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                         NamedDecl *FoundD,
3282577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                         const TemplateArgumentListInfo *TemplateArgs,
329f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                         QualType T, ExprValueKind VK)
330561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
331cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth    D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
332cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth  DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
333176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (QualifierLoc) {
3346857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    getInternalQualifierLoc() = QualifierLoc;
335176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    auto *NNS = QualifierLoc.getNestedNameSpecifier();
336176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (NNS->isInstantiationDependent())
337176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      ExprBits.InstantiationDependent = true;
338176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (NNS->containsUnexpandedParameterPack())
339176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      ExprBits.ContainsUnexpandedParameterPack = true;
340176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
3413aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
3423aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (FoundD)
3433aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    getInternalFoundDecl() = FoundD;
344e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  DeclRefExprBits.HasTemplateKWAndArgsInfo
345e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
3460e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  DeclRefExprBits.RefersToEnclosingVariableOrCapture =
3470e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      RefersToEnclosingVariableOrCapture;
348561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (TemplateArgs) {
349561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool Dependent = false;
350561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool InstantiationDependent = false;
351561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool ContainsUnexpandedParameterPack = false;
352e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
353e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               Dependent,
354e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               InstantiationDependent,
355e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               ContainsUnexpandedParameterPack);
356176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    assert(!Dependent && "built a DeclRefExpr with dependent template args");
357176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ExprBits.InstantiationDependent |= InstantiationDependent;
358176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ExprBits.ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack;
359e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  } else if (TemplateKWLoc.isValid()) {
360e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
361561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
362b8da98a2182d2c69c72136b9a9ebabd4694f3d6bBenjamin Kramer  DeclRefExprBits.HadMultipleCandidates = 0;
363b8da98a2182d2c69c72136b9a9ebabd4694f3d6bBenjamin Kramer
3643d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar  computeDependence(Ctx);
3652577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
3662577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
3679db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperDeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
36840d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                                 NestedNameSpecifierLoc QualifierLoc,
369e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                 SourceLocation TemplateKWLoc,
370dbd872f273a8dbf22e089b3def6c09f0a460965dJohn McCall                                 ValueDecl *D,
3710e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                 bool RefersToEnclosingVariableOrCapture,
372a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor                                 SourceLocation NameLoc,
3730da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                 QualType T,
374f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
3753aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                 NamedDecl *FoundD,
3760da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                 const TemplateArgumentListInfo *TemplateArgs) {
377e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  return Create(Context, QualifierLoc, TemplateKWLoc, D,
3780e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                RefersToEnclosingVariableOrCapture,
3792577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                DeclarationNameInfo(D->getDeclName(), NameLoc),
3803aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                T, VK, FoundD, TemplateArgs);
3812577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
3822577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
3839db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperDeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
38440d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                                 NestedNameSpecifierLoc QualifierLoc,
385e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                 SourceLocation TemplateKWLoc,
3862577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 ValueDecl *D,
3870e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                 bool RefersToEnclosingVariableOrCapture,
3882577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 const DeclarationNameInfo &NameInfo,
3892577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 QualType T,
390f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
3913aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                 NamedDecl *FoundD,
3922577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 const TemplateArgumentListInfo *TemplateArgs) {
3933aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  // Filter out cases where the found Decl is the same as the value refenenced.
3943aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (D == FoundD)
3956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    FoundD = nullptr;
3963aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
397a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  std::size_t Size = sizeof(DeclRefExpr);
3987247c88d1e41514a41085f83ebf03dd5220e054aDavid Blaikie  if (QualifierLoc)
3996857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    Size += sizeof(NestedNameSpecifierLoc);
4003aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (FoundD)
4013aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    Size += sizeof(NamedDecl *);
402a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  if (TemplateArgs) {
403a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    Size = llvm::RoundUpToAlignment(Size,
404a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                                    llvm::alignOf<ASTTemplateKWAndArgsInfo>());
405e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
406a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  } else if (TemplateKWLoc.isValid()) {
407a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    Size = llvm::RoundUpToAlignment(Size,
408a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                                    llvm::alignOf<ASTTemplateKWAndArgsInfo>());
409e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
410a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  }
4113aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
4123248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
4133d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar  return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
4140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                               RefersToEnclosingVariableOrCapture,
4153d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar                               NameInfo, FoundD, TemplateArgs, T, VK);
416a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor}
417a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
4189db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperDeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
419def0354384d9c4431f7b58b664b59896d4623028Douglas Gregor                                      bool HasQualifier,
4203aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                      bool HasFoundDecl,
421e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      bool HasTemplateKWAndArgsInfo,
422663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis                                      unsigned NumTemplateArgs) {
423663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  std::size_t Size = sizeof(DeclRefExpr);
424663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  if (HasQualifier)
4256857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    Size += sizeof(NestedNameSpecifierLoc);
4263aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (HasFoundDecl)
4273aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    Size += sizeof(NamedDecl *);
428a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  if (HasTemplateKWAndArgsInfo) {
429a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    Size = llvm::RoundUpToAlignment(Size,
430a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                                    llvm::alignOf<ASTTemplateKWAndArgsInfo>());
431e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
432a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  }
4333aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
4343248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
435663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  return new (Mem) DeclRefExpr(EmptyShell());
436663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis}
437663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis
438396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel DunbarSourceLocation DeclRefExpr::getLocStart() const {
439396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  if (hasQualifier())
440396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar    return getQualifierLoc().getBeginLoc();
441396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  return getNameInfo().getLocStart();
442396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar}
443396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel DunbarSourceLocation DeclRefExpr::getLocEnd() const {
444396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  if (hasExplicitTemplateArgs())
445396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar    return getRAngleLoc();
446396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  return getNameInfo().getLocEnd();
447396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar}
448a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
449176edba5311f6eff0cad2631449885ddf4fbc9eaStephen HinesPredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy, IdentType IT,
450176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                               StringLiteral *SL)
451176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    : Expr(PredefinedExprClass, FNTy, VK_LValue, OK_Ordinary,
452176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines           FNTy->isDependentType(), FNTy->isDependentType(),
453176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines           FNTy->isInstantiationDependentType(),
454176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines           /*ContainsUnexpandedParameterPack=*/false),
455176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Loc(L), Type(IT), FnName(SL) {}
456176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
457176edba5311f6eff0cad2631449885ddf4fbc9eaStephen HinesStringLiteral *PredefinedExpr::getFunctionName() {
458176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  return cast_or_null<StringLiteral>(FnName);
459176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines}
460176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
461176edba5311f6eff0cad2631449885ddf4fbc9eaStephen HinesStringRef PredefinedExpr::getIdentTypeName(PredefinedExpr::IdentType IT) {
462176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  switch (IT) {
463176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case Func:
464176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return "__func__";
465176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case Function:
466176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return "__FUNCTION__";
467176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case FuncDName:
468176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return "__FUNCDNAME__";
469176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case LFunction:
470176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return "L__FUNCTION__";
471176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case PrettyFunction:
472176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return "__PRETTY_FUNCTION__";
473176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case FuncSig:
474176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return "__FUNCSIG__";
475176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case PrettyFunctionNoVirtual:
476176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    break;
477176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
478176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  llvm_unreachable("Unknown ident type for PredefinedExpr");
479176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines}
480176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
4813a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson// FIXME: Maybe this should use DeclPrinter with a special "print predefined
4823a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson// expr" policy instead.
483848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlssonstd::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
484848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson  ASTContext &Context = CurrentDecl->getASTContext();
485848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson
486bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer  if (IT == PredefinedExpr::FuncDName) {
487bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer    if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
488651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      std::unique_ptr<MangleContext> MC;
489bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer      MC.reset(Context.createMangleContext());
490bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer
491bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer      if (MC->shouldMangleDeclName(ND)) {
492bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer        SmallString<256> Buffer;
493bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer        llvm::raw_svector_ostream Out(Buffer);
494bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer        if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
495bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer          MC->mangleCXXCtor(CD, Ctor_Base, Out);
496bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer        else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
497bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer          MC->mangleCXXDtor(DD, Dtor_Base, Out);
498bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer        else
499bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer          MC->mangleName(ND, Out);
500bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer
501bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer        if (!Buffer.empty() && Buffer.front() == '\01')
502bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer          return Buffer.substr(1);
503bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer        return Buffer.str();
504bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer      } else
505bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer        return ND->getIdentifier()->getName();
506bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer    }
507bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer    return "";
508bafa74f360cb3ec82fa8c688845330f491d167fdDavid Majnemer  }
509176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (auto *BD = dyn_cast<BlockDecl>(CurrentDecl)) {
510176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    std::unique_ptr<MangleContext> MC;
511176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    MC.reset(Context.createMangleContext());
512176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    SmallString<256> Buffer;
513176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    llvm::raw_svector_ostream Out(Buffer);
514176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    auto DC = CurrentDecl->getDeclContext();
515176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (DC->isFileContext())
516176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      MC->mangleGlobalBlock(BD, /*ID*/ nullptr, Out);
517176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    else if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
518176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      MC->mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
519176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
520176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      MC->mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
521176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    else
522176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      MC->mangleBlock(DC, BD, Out);
523176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return Out.str();
524176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
5253a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
5266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual && IT != FuncSig)
5273a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      return FD->getNameAsString();
5283a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
529f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith    SmallString<256> Name;
5303a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::raw_svector_ostream Out(Name);
5313a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
5323a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
533848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson      if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
5343a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        Out << "virtual ";
5354eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (MD->isStatic())
5364eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Out << "static ";
5373a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    }
5383a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
5394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    PrintingPolicy Policy(Context.getLangOpts());
540b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer    std::string Proto;
541abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    llvm::raw_string_ostream POut(Proto);
5423a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
543abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    const FunctionDecl *Decl = FD;
544abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
545abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      Decl = Pattern;
546abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
5476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    const FunctionProtoType *FT = nullptr;
5483a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (FD->hasWrittenPrototype())
5493a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      FT = dyn_cast<FunctionProtoType>(AFT);
5503a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
5516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (IT == FuncSig) {
5526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      switch (FT->getCallConv()) {
5536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      case CC_C: POut << "__cdecl "; break;
5546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      case CC_X86StdCall: POut << "__stdcall "; break;
5556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      case CC_X86FastCall: POut << "__fastcall "; break;
5566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      case CC_X86ThisCall: POut << "__thiscall "; break;
557176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      case CC_X86VectorCall: POut << "__vectorcall "; break;
5586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // Only bother printing the conventions that MSVC knows about.
5596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      default: break;
5606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      }
5616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
5626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
5636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    FD->printQualifiedName(POut, Policy);
5646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
565abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    POut << "(";
5663a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (FT) {
567abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
5683a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        if (i) POut << ", ";
5697ad5c996e9519ed4e9afd1f0166be1cd2be8415aArgyrios Kyrtzidis        POut << Decl->getParamDecl(i)->getType().stream(Policy);
5703a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      }
5713a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
5723a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      if (FT->isVariadic()) {
5733a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        if (FD->getNumParams()) POut << ", ";
5743a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        POut << "...";
5753a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      }
5763a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    }
577abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    POut << ")";
5783a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
5794eadcc569223135e13353c9381b448986e3f7053Sam Weinig    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
5804ae711b5ef292378d5b88cede3d4b708174053e2Argyrios Kyrtzidis      const FunctionType *FT = MD->getType()->castAs<FunctionType>();
5814ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie      if (FT->isConst())
582abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        POut << " const";
5834ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie      if (FT->isVolatile())
584abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        POut << " volatile";
585abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      RefQualifierKind Ref = MD->getRefQualifier();
586abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      if (Ref == RQ_LValue)
587abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        POut << " &";
588abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      else if (Ref == RQ_RValue)
589abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        POut << " &&";
590abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    }
591abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor
592abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
593abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    SpecsTy Specs;
594abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    const DeclContext *Ctx = FD->getDeclContext();
595abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    while (Ctx && isa<NamedDecl>(Ctx)) {
596abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      const ClassTemplateSpecializationDecl *Spec
597abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor                               = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
598abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      if (Spec && !Spec->isExplicitSpecialization())
599abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        Specs.push_back(Spec);
600abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      Ctx = Ctx->getParent();
601abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    }
602abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor
603abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    std::string TemplateParams;
604abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    llvm::raw_string_ostream TOut(TemplateParams);
605abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
606abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor         I != E; ++I) {
607abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      const TemplateParameterList *Params
608abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor                  = (*I)->getSpecializedTemplate()->getTemplateParameters();
609abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      const TemplateArgumentList &Args = (*I)->getTemplateArgs();
610abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      assert(Params->size() == Args.size());
611abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
612abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        StringRef Param = Params->getParam(i)->getName();
613abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        if (Param.empty()) continue;
614abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        TOut << Param << " = ";
615abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        Args.get(i).print(Policy, TOut);
616abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        TOut << ", ";
617abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      }
6184eadcc569223135e13353c9381b448986e3f7053Sam Weinig    }
6194eadcc569223135e13353c9381b448986e3f7053Sam Weinig
620abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    FunctionTemplateSpecializationInfo *FSI
621abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor                                          = FD->getTemplateSpecializationInfo();
622abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    if (FSI && !FSI->isExplicitSpecialization()) {
623abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      const TemplateParameterList* Params
624abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor                                  = FSI->getTemplate()->getTemplateParameters();
625abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      const TemplateArgumentList* Args = FSI->TemplateArguments;
626abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      assert(Params->size() == Args->size());
627abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      for (unsigned i = 0, e = Params->size(); i != e; ++i) {
628abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        StringRef Param = Params->getParam(i)->getName();
629abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        if (Param.empty()) continue;
630abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        TOut << Param << " = ";
631abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        Args->get(i).print(Policy, TOut);
632abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        TOut << ", ";
633abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      }
634abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    }
635abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor
636abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    TOut.flush();
637abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    if (!TemplateParams.empty()) {
638abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      // remove the trailing comma and space
639abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      TemplateParams.resize(TemplateParams.size() - 2);
640abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      POut << " [" << TemplateParams << "]";
641abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    }
642abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor
643abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    POut.flush();
644abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor
64528bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    // Print "auto" for all deduced return types. This includes C++1y return
64628bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    // type deduction and lambdas. For trailing return types resolve the
64728bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    // decltype expression. Otherwise print the real type when this is
64828bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    // not a constructor or destructor.
649176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (isa<CXXMethodDecl>(FD) &&
650176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines         cast<CXXMethodDecl>(FD)->getParent()->isLambda())
65128bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer      Proto = "auto " + Proto;
652651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    else if (FT && FT->getReturnType()->getAs<DecltypeType>())
653651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      FT->getReturnType()
654651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          ->getAs<DecltypeType>()
655651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          ->getUnderlyingType()
65628bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer          .getAsStringInternal(Proto, Policy);
65728bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
658651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      AFT->getReturnType().getAsStringInternal(Proto, Policy);
6593a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
6603a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << Proto;
6613a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
6623a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return Name.str().str();
6633a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
66415b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan  if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
66515b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan    for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
66615b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan      // Skip to its enclosing function or method, but not its enclosing
66715b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan      // CapturedDecl.
66815b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan      if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
66915b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan        const Decl *D = Decl::castFromDeclContext(DC);
67015b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan        return ComputeName(IT, D);
67115b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan      }
67215b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan    llvm_unreachable("CapturedDecl not inside a function or method");
67315b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan  }
6743a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
675f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith    SmallString<256> Name;
6763a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::raw_svector_ostream Out(Name);
6773a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << (MD->isInstanceMethod() ? '-' : '+');
6783a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << '[';
679b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek
680b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    // For incorrect code, there might not be an ObjCInterfaceDecl.  Do
681b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    // a null check to avoid a crash.
682b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
683b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer      Out << *ID;
684b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek
6853a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (const ObjCCategoryImplDecl *CID =
686900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer        dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
687f978059b82db8c0d849c5f992036210b5ca53200Benjamin Kramer      Out << '(' << *CID << ')';
688900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer
6893a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out <<  ' ';
690651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    MD->getSelector().print(Out);
6913a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out <<  ']';
6923a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
6933a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return Name.str().str();
6943a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
6953a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
6963a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
6973a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return "top level";
6983a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
6993a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return "";
7003a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
7013a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
70205ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid APNumericStorage::setIntValue(const ASTContext &C,
70305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                                   const llvm::APInt &Val) {
7049996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  if (hasAllocation())
7059996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    C.Deallocate(pVal);
7069996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
7079996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  BitWidth = Val.getBitWidth();
7089996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  unsigned NumWords = Val.getNumWords();
7099996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  const uint64_t* Words = Val.getRawData();
7109996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  if (NumWords > 1) {
7119996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    pVal = new (C) uint64_t[NumWords];
7129996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    std::copy(Words, Words + NumWords, pVal);
7139996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  } else if (NumWords == 1)
7149996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    VAL = Words[0];
7159996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  else
7169996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    VAL = 0;
7179996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
7189996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
71905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperIntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
720478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer                               QualType type, SourceLocation l)
721478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
722478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer         false, false),
723478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer    Loc(l) {
724478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
725478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  assert(V.getBitWidth() == C.getIntWidth(type) &&
726478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer         "Integer type is not the correct size for constant.");
727478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  setValue(C, V);
728478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer}
729478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer
7309996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral *
73105ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperIntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
7329996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                       QualType type, SourceLocation l) {
7339996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) IntegerLiteral(C, V, type, l);
7349996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
7359996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
7369996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral *
73705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperIntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
7389996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) IntegerLiteral(Empty);
7399996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
7409996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
74105ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperFloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
742478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer                                 bool isexact, QualType Type, SourceLocation L)
743478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
744478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer         false, false), Loc(L) {
7459ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  setSemantics(V.getSemantics());
746478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  FloatingLiteralBits.IsExact = isexact;
747478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  setValue(C, V);
748478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer}
749478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer
75005ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperFloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
751478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  : Expr(FloatingLiteralClass, Empty) {
7529ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  setRawSemantics(IEEEhalf);
753478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  FloatingLiteralBits.IsExact = false;
754478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer}
755478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer
7569996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral *
75705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperFloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
7589996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                        bool isexact, QualType Type, SourceLocation L) {
7599996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) FloatingLiteral(C, V, isexact, Type, L);
7609996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
7619996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
7629996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral *
76305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperFloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
76431dfd642d5ac33c4ee0cfe1d7a1d4da455dcc7a4Akira Hatanaka  return new (C) FloatingLiteral(C, Empty);
7659996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
7669996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
7679ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northoverconst llvm::fltSemantics &FloatingLiteral::getSemantics() const {
7689ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  switch(FloatingLiteralBits.Semantics) {
7699ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case IEEEhalf:
7709ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::IEEEhalf;
7719ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case IEEEsingle:
7729ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::IEEEsingle;
7739ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case IEEEdouble:
7749ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::IEEEdouble;
7759ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case x87DoubleExtended:
7769ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::x87DoubleExtended;
7779ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case IEEEquad:
7789ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::IEEEquad;
7799ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case PPCDoubleDouble:
7809ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::PPCDoubleDouble;
7819ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  }
7829ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  llvm_unreachable("Unrecognised floating semantics");
7839ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover}
7849ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover
7859ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northovervoid FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
7869ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  if (&Sem == &llvm::APFloat::IEEEhalf)
7879ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = IEEEhalf;
7889ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else if (&Sem == &llvm::APFloat::IEEEsingle)
7899ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = IEEEsingle;
7909ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else if (&Sem == &llvm::APFloat::IEEEdouble)
7919ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = IEEEdouble;
7929ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else if (&Sem == &llvm::APFloat::x87DoubleExtended)
7939ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = x87DoubleExtended;
7949ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else if (&Sem == &llvm::APFloat::IEEEquad)
7959ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = IEEEquad;
7969ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
7979ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = PPCDoubleDouble;
7989ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else
7999ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    llvm_unreachable("Unknown floating semantics");
8009ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover}
8019ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover
802da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// getValueAsApproximateDouble - This returns the value as an inaccurate
803da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// double.  Note that this may cause loss of precision, but is useful for
804da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// debugging dumps, etc.
805da8249e57f3badecf925571881fe57243935c6c1Chris Lattnerdouble FloatingLiteral::getValueAsApproximateDouble() const {
806da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  llvm::APFloat V = getValue();
807ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  bool ignored;
808ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
809ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen            &ignored);
810da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  return V.convertToDouble();
811da8249e57f3badecf925571881fe57243935c6c1Chris Lattner}
812da8249e57f3badecf925571881fe57243935c6c1Chris Lattner
8130fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewyckyint StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
814fd819783aafa39b3bfdfcc095270352074ef4734Eli Friedman  int CharByteWidth = 0;
8150fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky  switch(k) {
81664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case Ascii:
81764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case UTF8:
8180fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky      CharByteWidth = target.getCharWidth();
81964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
82064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case Wide:
8210fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky      CharByteWidth = target.getWCharWidth();
82264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
82364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case UTF16:
8240fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky      CharByteWidth = target.getChar16Width();
82564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
82664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case UTF32:
8270fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky      CharByteWidth = target.getChar32Width();
828fd819783aafa39b3bfdfcc095270352074ef4734Eli Friedman      break;
82964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  }
83064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
83164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  CharByteWidth /= 8;
8320fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky  assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
83364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman         && "character byte widths supported are 1, 2, and 4 only");
83464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  return CharByteWidth;
83564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman}
83664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
83705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperStringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
8385cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                     StringKind Kind, bool Pascal, QualType Ty,
8391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     const SourceLocation *Loc,
840a135fb43eb94524a6529768596a4533eed9aa70dAnders Carlsson                                     unsigned NumStrs) {
841651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  assert(C.getAsConstantArrayType(Ty) &&
842651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines         "StringLiteral must be of constant array type!");
843651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
8442085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // Allocate enough space for the StringLiteral plus an array of locations for
8452085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // any concatenated string tokens.
8462085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  void *Mem = C.Allocate(sizeof(StringLiteral)+
8472085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                         sizeof(SourceLocation)*(NumStrs-1),
8483248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner                         llvm::alignOf<StringLiteral>());
8492085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  StringLiteral *SL = new (Mem) StringLiteral(Ty);
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // OPTIMIZE: could allocate this appended to the StringLiteral.
85264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  SL->setString(C,Str,Kind,Pascal);
85364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
8542085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->TokLocs[0] = Loc[0];
8552085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->NumConcatenated = NumStrs;
8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
857726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner  if (NumStrs != 1)
8582085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner    memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
8592085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  return SL;
860726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner}
861726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner
86205ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperStringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
86305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                                          unsigned NumStrs) {
864673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  void *Mem = C.Allocate(sizeof(StringLiteral)+
865673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor                         sizeof(SourceLocation)*(NumStrs-1),
8663248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner                         llvm::alignOf<StringLiteral>());
867673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  StringLiteral *SL = new (Mem) StringLiteral(QualType());
86864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  SL->CharByteWidth = 0;
86964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  SL->Length = 0;
870673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  SL->NumConcatenated = NumStrs;
871673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  return SL;
872673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor}
873673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor
874ae54121c15cdd38f415f6fdda48215ab5e4dcee1Alexander Kornienkovoid StringLiteral::outputString(raw_ostream &OS) const {
8758ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  switch (getKind()) {
8768ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  case Ascii: break; // no prefix.
8778ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  case Wide:  OS << 'L'; break;
8788ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  case UTF8:  OS << "u8"; break;
8798ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  case UTF16: OS << 'u'; break;
8808ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  case UTF32: OS << 'U'; break;
8818ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  }
8828ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  OS << '"';
8838ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  static const char Hex[] = "0123456789ABCDEF";
8848ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
8858ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  unsigned LastSlashX = getLength();
8868ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  for (unsigned I = 0, N = getLength(); I != N; ++I) {
8878ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    switch (uint32_t Char = getCodeUnit(I)) {
8888ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    default:
8898ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      // FIXME: Convert UTF-8 back to codepoints before rendering.
8908ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
8918ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      // Convert UTF-16 surrogate pairs back to codepoints before rendering.
8928ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      // Leave invalid surrogates alone; we'll use \x for those.
8938ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
8948ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          Char <= 0xdbff) {
8958ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        uint32_t Trail = getCodeUnit(I + 1);
8968ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        if (Trail >= 0xdc00 && Trail <= 0xdfff) {
8978ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
8988ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          ++I;
8998ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        }
9008ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      }
9018ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
9028ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      if (Char > 0xff) {
9038ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        // If this is a wide string, output characters over 0xff using \x
9048ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
9058ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        // codepoint: use \x escapes for invalid codepoints.
9068ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        if (getKind() == Wide ||
9078ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu            (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
9088ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          // FIXME: Is this the best way to print wchar_t?
9098ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          OS << "\\x";
9108ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          int Shift = 28;
9118ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          while ((Char >> Shift) == 0)
9128ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu            Shift -= 4;
9138ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          for (/**/; Shift >= 0; Shift -= 4)
9148ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu            OS << Hex[(Char >> Shift) & 15];
9158ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          LastSlashX = I;
9168ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          break;
9178ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        }
9188ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
9198ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        if (Char > 0xffff)
9208ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          OS << "\\U00"
9218ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu             << Hex[(Char >> 20) & 15]
9228ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu             << Hex[(Char >> 16) & 15];
9238ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        else
9248ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          OS << "\\u";
9258ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        OS << Hex[(Char >> 12) & 15]
9268ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << Hex[(Char >>  8) & 15]
9278ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << Hex[(Char >>  4) & 15]
9288ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << Hex[(Char >>  0) & 15];
9298ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        break;
9308ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      }
9318ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
9328ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      // If we used \x... for the previous character, and this character is a
9338ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      // hexadecimal digit, prevent it being slurped as part of the \x.
9348ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      if (LastSlashX + 1 == I) {
9358ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        switch (Char) {
9368ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          case '0': case '1': case '2': case '3': case '4':
9378ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          case '5': case '6': case '7': case '8': case '9':
9388ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
9398ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
9408ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu            OS << "\"\"";
9418ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        }
9428ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      }
9438ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
9448ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      assert(Char <= 0xff &&
9458ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu             "Characters above 0xff should already have been handled.");
9468ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
9473f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose      if (isPrintable(Char))
9488ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        OS << (char)Char;
9498ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      else  // Output anything hard as an octal escape.
9508ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        OS << '\\'
9518ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << (char)('0' + ((Char >> 6) & 7))
9528ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << (char)('0' + ((Char >> 3) & 7))
9538ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << (char)('0' + ((Char >> 0) & 7));
9548ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      break;
9558ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    // Handle some common non-printable cases to make dumps prettier.
9568ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '\\': OS << "\\\\"; break;
9578ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '"': OS << "\\\""; break;
9588ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '\n': OS << "\\n"; break;
9598ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '\t': OS << "\\t"; break;
9608ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '\a': OS << "\\a"; break;
9618ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '\b': OS << "\\b"; break;
9628ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    }
9638ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  }
9648ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  OS << '"';
9658ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu}
9668ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
96705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid StringLiteral::setString(const ASTContext &C, StringRef Str,
96864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman                              StringKind Kind, bool IsPascal) {
96964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  //FIXME: we assume that the string data comes from a target that uses the same
97064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  // code unit size and endianess for the type of string.
97164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  this->Kind = Kind;
97264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  this->IsPascal = IsPascal;
97364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
9740fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky  CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
97564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  assert((Str.size()%CharByteWidth == 0)
97664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman         && "size of data must be multiple of CharByteWidth");
97764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  Length = Str.size()/CharByteWidth;
97864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
97964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  switch(CharByteWidth) {
98064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case 1: {
98164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      char *AStrData = new (C) char[Length];
98266dfef17b8da7c89e20f32d4f0f4a04691b79768Argyrios Kyrtzidis      std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
98364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      StrData.asChar = AStrData;
98464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
98564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    }
98664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case 2: {
98764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      uint16_t *AStrData = new (C) uint16_t[Length];
98866dfef17b8da7c89e20f32d4f0f4a04691b79768Argyrios Kyrtzidis      std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
98964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      StrData.asUInt16 = AStrData;
99064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
99164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    }
99264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case 4: {
99364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      uint32_t *AStrData = new (C) uint32_t[Length];
99466dfef17b8da7c89e20f32d4f0f4a04691b79768Argyrios Kyrtzidis      std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
99564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      StrData.asUInt32 = AStrData;
99664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
99764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    }
99864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    default:
99964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      assert(false && "unsupported CharByteWidth");
100064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  }
1001673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor}
1002673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor
100308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// getLocationOfByte - Return a source location that points to the specified
100408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// byte of this string literal.
100508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner///
100608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// Strings are amazingly complex.  They can be formed from multiple tokens and
100708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// can have escape sequences in them in addition to the usual trigraph and
100808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// escaped newline business.  This routine handles this complexity.
100908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner///
1010a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar/// The *StartToken sets the first token to be searched in this function and
1011a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar/// the *StartTokenByteOffset is the byte offset of the first token. Before
1012a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar/// returning, it updates the *StartToken to the TokNo of the token being found
1013a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar/// and sets *StartTokenByteOffset to the byte offset of the token in the
1014a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar/// string.
1015a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar/// Using these two parameters can reduce the time complexity from O(n^2) to
1016a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar/// O(n) if one wants to get the location of byte for all the tokens in a
1017a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar/// string.
1018a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar///
1019a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga NainarSourceLocation
1020a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga NainarStringLiteral::getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
1021a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                                 const LangOptions &Features,
1022a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                                 const TargetInfo &Target, unsigned *StartToken,
1023a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                                 unsigned *StartTokenByteOffset) const {
1024df9ef1bc8c3780307ab2ed81bf5e31c23310b936Richard Smith  assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
1025df9ef1bc8c3780307ab2ed81bf5e31c23310b936Richard Smith         "Only narrow string literals are currently supported");
10265cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
102708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  // Loop over all of the tokens in this string until we find the one that
102808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  // contains the byte we're looking for.
102908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  unsigned TokNo = 0;
1030a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  unsigned StringOffset = 0;
1031a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  if (StartToken)
1032a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    TokNo = *StartToken;
1033a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  if (StartTokenByteOffset) {
1034a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    StringOffset = *StartTokenByteOffset;
1035a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    ByteNo -= StringOffset;
1036a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  }
103708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  while (1) {
103808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    assert(TokNo < getNumConcatenated() && "Invalid byte number!");
103908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
104008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
104108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Get the spelling of the string so that we can get the data that makes up
104208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // the string literal, not the identifier for the macro it is potentially
104308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // expanded through.
104408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
1045a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
104608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Re-lex the token to get its length and original spelling.
1047a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    std::pair<FileID, unsigned> LocInfo =
1048a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        SM.getDecomposedLoc(StrTokSpellingLoc);
104908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    bool Invalid = false;
10505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
1051a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    if (Invalid) {
1052a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      if (StartTokenByteOffset != nullptr)
1053a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        *StartTokenByteOffset = StringOffset;
1054a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      if (StartToken != nullptr)
1055a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        *StartToken = TokNo;
105608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      return StrTokSpellingLoc;
1057a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    }
1058a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
105908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    const char *StrData = Buffer.data()+LocInfo.second;
106008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
106108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Create a lexer starting at the beginning of this token.
1062df8755884e039d3f313ee0fea42b955257b5e240Argyrios Kyrtzidis    Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
1063df8755884e039d3f313ee0fea42b955257b5e240Argyrios Kyrtzidis                   Buffer.begin(), StrData, Buffer.end());
106408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    Token TheTok;
106508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    TheLexer.LexFromRawLexer(TheTok);
106608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
106708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Use the StringLiteralParser to compute the length of the string in bytes.
1068c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    StringLiteralParser SLP(TheTok, SM, Features, Target);
106908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    unsigned TokNumBytes = SLP.GetStringLength();
107008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
107108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // If the byte is in this token, return the location of the byte.
107208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    if (ByteNo < TokNumBytes ||
1073935a70c1e76d78985f20d422940280161b941299Hans Wennborg        (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
1074a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1075a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
107608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      // Now that we know the offset of the token in the spelling, use the
107708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      // preprocessor to get the offset in the original source.
1078a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      if (StartTokenByteOffset != nullptr)
1079a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        *StartTokenByteOffset = StringOffset;
1080a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      if (StartToken != nullptr)
1081a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        *StartToken = TokNo;
108208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
108308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    }
1084a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
108508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Move to the next string token.
1086a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    StringOffset += TokNumBytes;
108708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    ++TokNo;
108808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    ByteNo -= TokNumBytes;
108908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  }
109008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner}
109108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
109208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
109308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "sizeof" or "[pre]++".
10960bea86307eb8c16339315a1e261fc490eb505c5bDavid BlaikieStringRef UnaryOperator::getOpcodeStr(Opcode Op) {
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
10982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostInc: return "++";
10992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostDec: return "--";
11002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PreInc:  return "++";
11012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PreDec:  return "--";
11022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_AddrOf:  return "&";
11032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Deref:   return "*";
11042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:    return "+";
11052de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus:   return "-";
11062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Not:     return "~";
11072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_LNot:    return "!";
11082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Real:    return "__real";
11092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Imag:    return "__imag";
11102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Extension: return "__extension__";
1111a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  case UO_Coawait: return "co_await";
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1113561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie  llvm_unreachable("Unknown unary operator");
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCallUnaryOperatorKind
1117bc736fceca6f0bca31d16003a7587857190408fbDouglas GregorUnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1118bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  switch (OO) {
1119b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("No unary operator for overloaded function");
11202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PlusPlus:   return Postfix ? UO_PostInc : UO_PreInc;
11212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
11222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Amp:        return UO_AddrOf;
11232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Star:       return UO_Deref;
11242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Plus:       return UO_Plus;
11252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Minus:      return UO_Minus;
11262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Tilde:      return UO_Not;
11272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Exclaim:    return UO_LNot;
1128a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  case OO_Coawait:    return UO_Coawait;
1129bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  }
1130bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor}
1131bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
1132bc736fceca6f0bca31d16003a7587857190408fbDouglas GregorOverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1133bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  switch (Opc) {
11342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
11352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
11362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_AddrOf: return OO_Amp;
11372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Deref: return OO_Star;
11382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus: return OO_Plus;
11392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus: return OO_Minus;
11402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Not: return OO_Tilde;
11412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_LNot: return OO_Exclaim;
1142a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  case UO_Coawait: return OO_Coawait;
1143bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  default: return OO_None;
1144bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  }
1145bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor}
1146bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
1147bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Postfix Operators.
11505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
115205ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
115305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                   unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
115405ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                   ExprValueKind VK, SourceLocation rparenloc)
1155f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(SC, t, VK, OK_Ordinary,
1156bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isTypeDependent(),
1157bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isValueDependent(),
1158561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         fn->isInstantiationDependent(),
1159bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->containsUnexpandedParameterPack()),
11603b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumArgs(args.size()) {
11611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11623b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
1163b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  SubExprs[FN] = fn;
11643b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != args.size(); ++i) {
1165bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
1166bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
1167bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
1168bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
1169561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
1170561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
1171bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
1172bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
1173bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
1174cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
1175bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
1176668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
1177cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = NumPreArgs;
1178b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  RParenLoc = rparenloc;
1179b4609806e9232593ece09ce08b630836e825865cDouglas Gregor}
1180e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman
11813ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga NainarCallExpr::CallExpr(const ASTContext &C, Expr *fn, ArrayRef<Expr *> args,
1182f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                   QualType t, ExprValueKind VK, SourceLocation rparenloc)
11833ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    : CallExpr(C, CallExprClass, fn, /*NumPreArgs=*/0, args, t, VK, rparenloc) {
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
118605ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
11873ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    : CallExpr(C, SC, /*NumPreArgs=*/0, Empty) {}
1188cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne
118905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
1190cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne                   EmptyShell Empty)
11916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
1192cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  // FIXME: Why do we allocate this?
1193cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1194cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = NumPreArgs;
11951f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor}
11961f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor
1197d20254f2875d0004c57ee766f258dbcee29f4841Nuno LopesDecl *CallExpr::getCalleeDecl() {
1198e8683d6d389def2fce229325435ba3c2b3f75492John McCall  Expr *CEE = getCallee()->IgnoreParenImpCasts();
11991ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor
12001ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor  while (SubstNonTypeTemplateParmExpr *NTTP
12011ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor                                = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
12021ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor    CEE = NTTP->getReplacement()->IgnoreParenCasts();
12031ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor  }
12041ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor
1205200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  // If we're calling a dereference, look at the pointer instead.
1206200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1207200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl    if (BO->isPtrMemOp())
1208200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl      CEE = BO->getRHS()->IgnoreParenCasts();
1209200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1210200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl    if (UO->getOpcode() == UO_Deref)
1211200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl      CEE = UO->getSubExpr()->IgnoreParenCasts();
1212200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  }
12136346f963145ed18b6edf50a78753b47db505e912Chris Lattner  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
1214d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    return DRE->getDecl();
1215cb1c77f90d4e747b83a0d0cc125dc01567378f82Nuno Lopes  if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1216cb1c77f90d4e747b83a0d0cc125dc01567378f82Nuno Lopes    return ME->getMemberDecl();
1217a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu
12186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
1219a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu}
1220a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu
1221d20254f2875d0004c57ee766f258dbcee29f4841Nuno LopesFunctionDecl *CallExpr::getDirectCallee() {
1222caabf9bf331156e96dacb072385901fdfa057ec1Chris Lattner  return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
1223d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes}
1224d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes
1225d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// setNumArgs - This changes the number of arguments present in this call.
1226d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// Any orphaned expressions are deleted by this, and any new operands are set
1227d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// to null.
122805ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
1229d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // No change, just return.
1230d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs == getNumArgs()) return;
12311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1232d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // If shrinking # arguments, just delete the extras and forgot them.
1233d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs < getNumArgs()) {
1234d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    this->NumArgs = NumArgs;
1235d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    return;
1236d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  }
1237d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
1238d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Otherwise, we are growing the # arguments.  New an bigger argument array.
1239cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  unsigned NumPreArgs = getNumPreArgs();
1240cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
1241d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Copy over args.
1242cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
1243d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = SubExprs[i];
1244d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Null out new args.
1245cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1246cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne       i != NumArgs+PREARGS_START+NumPreArgs; ++i)
12476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    NewSubExprs[i] = nullptr;
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124988c9a46f0b84f1ee83e01917825346551ee540d0Douglas Gregor  if (SubExprs) C.Deallocate(SubExprs);
1250d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  SubExprs = NewSubExprs;
1251d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  this->NumArgs = NumArgs;
1252d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner}
1253d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
1254651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
1255cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// not, return 0.
1256651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesunsigned CallExpr::getBuiltinCallee() const {
1257c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // All simple function calls (e.g. func()) are implicitly cast to pointer to
12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // function. As a result, we try and obtain the DeclRefExpr from the
1259c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // ImplicitCastExpr.
1260c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1261c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
1262cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
12631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1264c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1265c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!DRE)
1266cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
12671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1268bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1269bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  if (!FDecl)
1270cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
12711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12724fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor  if (!FDecl->getIdentifier())
12734fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor    return 0;
12744fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor
12757814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  return FDecl->getBuiltinID();
1276cb888967400a03504c88acedd5248d6778a82f46Chris Lattner}
1277bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson
1278b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainarbool CallExpr::isUnevaluatedBuiltinCall(const ASTContext &Ctx) const {
1279651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (unsigned BI = getBuiltinCallee())
1280ba57183965f117279342903edec19766e478c9a8Richard Smith    return Ctx.BuiltinInfo.isUnevaluated(BI);
1281ba57183965f117279342903edec19766e478c9a8Richard Smith  return false;
1282ba57183965f117279342903edec19766e478c9a8Richard Smith}
1283ba57183965f117279342903edec19766e478c9a8Richard Smith
12840e2c34f92f00628d48968dfea096d36381f494cbStephen HinesQualType CallExpr::getCallReturnType(const ASTContext &Ctx) const {
12850e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  const Expr *Callee = getCallee();
12860e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  QualType CalleeType = Callee->getType();
12870e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (const auto *FnTypePtr = CalleeType->getAs<PointerType>()) {
12886dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson    CalleeType = FnTypePtr->getPointeeType();
12890e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  } else if (const auto *BPT = CalleeType->getAs<BlockPointerType>()) {
12906dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson    CalleeType = BPT->getPointeeType();
12910e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  } else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember)) {
12920e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (isa<CXXPseudoDestructorExpr>(Callee->IgnoreParens()))
12930e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      return Ctx.VoidTy;
12940e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1295864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    // This should never be overloaded and so should never return null.
12960e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    CalleeType = Expr::findBoundMemberType(Callee);
12970e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
12980e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1299864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  const FunctionType *FnType = CalleeType->castAs<FunctionType>();
1300651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return FnType->getReturnType();
13016dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson}
1302cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
13038fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel DunbarSourceLocation CallExpr::getLocStart() const {
13048fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  if (isa<CXXOperatorCallExpr>(this))
130565d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen    return cast<CXXOperatorCallExpr>(this)->getLocStart();
13068fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar
13078fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  SourceLocation begin = getCallee()->getLocStart();
1308176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (begin.isInvalid() && getNumArgs() > 0 && getArg(0))
13098fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar    begin = getArg(0)->getLocStart();
13108fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  return begin;
13118fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar}
13128fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel DunbarSourceLocation CallExpr::getLocEnd() const {
13138fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  if (isa<CXXOperatorCallExpr>(this))
131465d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen    return cast<CXXOperatorCallExpr>(this)->getLocEnd();
13158fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar
13168fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  SourceLocation end = getRParenLoc();
1317176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (end.isInvalid() && getNumArgs() > 0 && getArg(getNumArgs() - 1))
13188fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar    end = getArg(getNumArgs() - 1)->getLocEnd();
13198fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  return end;
13208fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar}
13212882eca5a184c78f793188083f6ce539740a5cf2John McCall
132205ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperOffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
13238ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   SourceLocation OperatorLoc,
1324c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                   TypeSourceInfo *tsi,
13253b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                   ArrayRef<OffsetOfNode> comps,
13263b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                   ArrayRef<Expr*> exprs,
13278ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   SourceLocation RParenLoc) {
13288ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
13293b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                         sizeof(OffsetOfNode) * comps.size() +
13303b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                         sizeof(Expr*) * exprs.size());
13318ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
13323b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
13333b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                RParenLoc);
13348ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
13358ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
133605ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperOffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
13378ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                        unsigned numComps, unsigned numExprs) {
13388ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
13398ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(OffsetOfNode) * numComps +
13408ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(Expr*) * numExprs);
13418ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return new (Mem) OffsetOfExpr(numComps, numExprs);
13428ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
13438ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
134405ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperOffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
13458ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           SourceLocation OperatorLoc, TypeSourceInfo *tsi,
13463b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                           ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
13478ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           SourceLocation RParenLoc)
1348f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1349f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall         /*TypeDependent=*/false,
1350bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*ValueDependent=*/tsi->getType()->isDependentType(),
1351561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         tsi->getType()->isInstantiationDependentType(),
1352bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         tsi->getType()->containsUnexpandedParameterPack()),
1353c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
13543b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumComps(comps.size()), NumExprs(exprs.size())
13558ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor{
13563b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != comps.size(); ++i) {
13573b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    setComponent(i, comps[i]);
13588ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
1359c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
13603b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != exprs.size(); ++i) {
13613b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
1362bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
13633b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    if (exprs[i]->containsUnexpandedParameterPack())
1364bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
1365bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
13663b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    setIndexExpr(i, exprs[i]);
13678ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
13688ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
13698ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
13708ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas GregorIdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
13718ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  assert(getKind() == Field || getKind() == Identifier);
13728ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  if (getKind() == Field)
13738ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    return getField()->getIdentifier();
1374c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
13758ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
13768ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
13778ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
13780e2c34f92f00628d48968dfea096d36381f494cbStephen HinesUnaryExprOrTypeTraitExpr::UnaryExprOrTypeTraitExpr(
13790e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    UnaryExprOrTypeTrait ExprKind, Expr *E, QualType resultType,
13800e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    SourceLocation op, SourceLocation rp)
13810e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    : Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary,
13820e2c34f92f00628d48968dfea096d36381f494cbStephen Hines           false, // Never type-dependent (C++ [temp.dep.expr]p3).
13830e2c34f92f00628d48968dfea096d36381f494cbStephen Hines           // Value-dependent if the argument is type-dependent.
13840e2c34f92f00628d48968dfea096d36381f494cbStephen Hines           E->isTypeDependent(), E->isInstantiationDependent(),
13850e2c34f92f00628d48968dfea096d36381f494cbStephen Hines           E->containsUnexpandedParameterPack()),
13860e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      OpLoc(op), RParenLoc(rp) {
13870e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  UnaryExprOrTypeTraitExprBits.Kind = ExprKind;
13880e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  UnaryExprOrTypeTraitExprBits.IsType = false;
13890e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  Argument.Ex = E;
13900e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
13910e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // Check to see if we are in the situation where alignof(decl) should be
13920e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // dependent because decl's alignment is dependent.
13930e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (ExprKind == UETT_AlignOf) {
13940e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (!isValueDependent() || !isInstantiationDependent()) {
13950e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      E = E->IgnoreParens();
13960e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
13970e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      const ValueDecl *D = nullptr;
13980e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
13990e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        D = DRE->getDecl();
14000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      else if (const auto *ME = dyn_cast<MemberExpr>(E))
14010e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        D = ME->getMemberDecl();
14020e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
14030e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (D) {
14040e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        for (const auto *I : D->specific_attrs<AlignedAttr>()) {
14050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines          if (I->isAlignmentDependent()) {
14060e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            setValueDependent(true);
14070e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            setInstantiationDependent(true);
14080e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            break;
14090e2c34f92f00628d48968dfea096d36381f494cbStephen Hines          }
14100e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        }
14110e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      }
14120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    }
14130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
14140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines}
14150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
14163ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga NainarMemberExpr *MemberExpr::Create(
14173ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    const ASTContext &C, Expr *base, bool isarrow, SourceLocation OperatorLoc,
14183ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
14193ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    ValueDecl *memberdecl, DeclAccessPair founddecl,
14203ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    DeclarationNameInfo nameinfo, const TemplateArgumentListInfo *targs,
14213ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    QualType ty, ExprValueKind vk, ExprObjectKind ok) {
142283f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  std::size_t Size = sizeof(MemberExpr);
14236bb8017bb9e828d118e15e59d71c66bba323c364John McCall
142440d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor  bool hasQualOrFound = (QualifierLoc ||
1425161755a09898c95d21bfff33707da9ca41cd53c5John McCall                         founddecl.getDecl() != memberdecl ||
1426161755a09898c95d21bfff33707da9ca41cd53c5John McCall                         founddecl.getAccess() != memberdecl->getAccess());
14276bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (hasQualOrFound)
14286bb8017bb9e828d118e15e59d71c66bba323c364John McCall    Size += sizeof(MemberNameQualifier);
14291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1430d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (targs)
1431e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1432e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  else if (TemplateKWLoc.isValid())
1433e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
14341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14353248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
14363ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  MemberExpr *E = new (Mem)
14373ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      MemberExpr(base, isarrow, OperatorLoc, memberdecl, nameinfo, ty, vk, ok);
14386bb8017bb9e828d118e15e59d71c66bba323c364John McCall
14396bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (hasQualOrFound) {
144040d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    // FIXME: Wrong. We should be looking at the member declaration we found.
144140d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
14426bb8017bb9e828d118e15e59d71c66bba323c364John McCall      E->setValueDependent(true);
14436bb8017bb9e828d118e15e59d71c66bba323c364John McCall      E->setTypeDependent(true);
1444561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
1445561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
1446561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    else if (QualifierLoc &&
1447561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor             QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1448561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
1449561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
14506bb8017bb9e828d118e15e59d71c66bba323c364John McCall    E->HasQualifierOrFoundDecl = true;
14516bb8017bb9e828d118e15e59d71c66bba323c364John McCall
14526bb8017bb9e828d118e15e59d71c66bba323c364John McCall    MemberNameQualifier *NQ = E->getMemberQualifier();
145340d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    NQ->QualifierLoc = QualifierLoc;
14546bb8017bb9e828d118e15e59d71c66bba323c364John McCall    NQ->FoundDecl = founddecl;
14556bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
14566bb8017bb9e828d118e15e59d71c66bba323c364John McCall
1457e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1458e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
14596bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (targs) {
1460561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool Dependent = false;
1461561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool InstantiationDependent = false;
1462561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool ContainsUnexpandedParameterPack = false;
1463e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1464e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                  Dependent,
1465e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                  InstantiationDependent,
1466e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                             ContainsUnexpandedParameterPack);
1467561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (InstantiationDependent)
1468561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
1469e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  } else if (TemplateKWLoc.isValid()) {
1470e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
14716bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
14726bb8017bb9e828d118e15e59d71c66bba323c364John McCall
14736bb8017bb9e828d118e15e59d71c66bba323c364John McCall  return E;
147483f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor}
147583f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor
1476396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel DunbarSourceLocation MemberExpr::getLocStart() const {
147775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  if (isImplicitAccess()) {
147875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (hasQualifier())
1479396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar      return getQualifierLoc().getBeginLoc();
1480396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar    return MemberLoc;
148175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  }
1482e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
1483396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  // FIXME: We don't want this to happen. Rather, we should be able to
1484396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  // detect all kinds of implicit accesses more cleanly.
1485396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  SourceLocation BaseStartLoc = getBase()->getLocStart();
1486396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  if (BaseStartLoc.isValid())
1487396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar    return BaseStartLoc;
1488396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  return MemberLoc;
1489396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar}
1490396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel DunbarSourceLocation MemberExpr::getLocEnd() const {
149113fd684ee2c7578177b7c741e8eeec91ae628fcfAbramo Bagnara  SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
1492396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  if (hasExplicitTemplateArgs())
149313fd684ee2c7578177b7c741e8eeec91ae628fcfAbramo Bagnara    EndLoc = getRAngleLoc();
149413fd684ee2c7578177b7c741e8eeec91ae628fcfAbramo Bagnara  else if (EndLoc.isInvalid())
149513fd684ee2c7578177b7c741e8eeec91ae628fcfAbramo Bagnara    EndLoc = getBase()->getLocEnd();
149613fd684ee2c7578177b7c741e8eeec91ae628fcfAbramo Bagnara  return EndLoc;
149775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor}
149875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
1499651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesbool CastExpr::CastConsistency() const {
15001d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  switch (getCastKind()) {
15011d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_DerivedToBase:
15021d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_UncheckedDerivedToBase:
15031d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_DerivedToBaseMemberPointer:
15041d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BaseToDerived:
15051d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BaseToDerivedMemberPointer:
15061d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(!path_empty() && "Cast kind should have a base path!");
15071d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    break;
15081d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
15091d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
15101d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isObjCObjectPointerType());
15111d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isPointerType());
15121d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
15131d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
15141d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
15151d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isObjCObjectPointerType());
15161d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isBlockPointerType());
15171d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
15181d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
15194d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
15204d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    assert(getType()->isMemberPointerType());
15214d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    assert(getSubExpr()->getType()->isMemberPointerType());
15224d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    goto CheckNoBasePath;
15234d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
15241d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BitCast:
15251d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // Arbitrary casts to C pointer types count as bitcasts.
15261d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // Otherwise, we should only have block and ObjC pointer casts
15271d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // here if they stay within the type kind.
15281d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    if (!getType()->isPointerType()) {
15291d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      assert(getType()->isObjCObjectPointerType() ==
15301d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall             getSubExpr()->getType()->isObjCObjectPointerType());
15311d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      assert(getType()->isBlockPointerType() ==
15321d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall             getSubExpr()->getType()->isBlockPointerType());
15331d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    }
15341d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
15351d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
15361d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_AnyPointerToBlockPointerCast:
15371d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isBlockPointerType());
15381d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isAnyPointerType() &&
15391d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall           !getSubExpr()->getType()->isBlockPointerType());
15401d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
15411d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
1542ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
1543ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor    assert(getType()->isBlockPointerType());
1544ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor    assert(getSubExpr()->getType()->isBlockPointerType());
1545ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor    goto CheckNoBasePath;
1546a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman
1547a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  case CK_FunctionToPointerDecay:
1548a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman    assert(getType()->isPointerType());
1549a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman    assert(getSubExpr()->getType()->isFunctionType());
1550a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman    goto CheckNoBasePath;
1551a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman
1552651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  case CK_AddressSpaceConversion:
1553651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    assert(getType()->isPointerType());
1554651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    assert(getSubExpr()->getType()->isPointerType());
1555651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    assert(getType()->getPointeeType().getAddressSpace() !=
1556651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines           getSubExpr()->getType()->getPointeeType().getAddressSpace());
15571d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  // These should not have an inheritance path.
15581d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_Dynamic:
15591d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ToUnion:
15601d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ArrayToPointerDecay:
15611d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NullToMemberPointer:
15621d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NullToPointer:
15631d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ConstructorConversion:
15641d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToPointer:
15651d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_PointerToIntegral:
15661d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ToVoid:
15671d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_VectorSplat:
15681d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralCast:
15691d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToFloating:
15701d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingToIntegral:
15711d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingCast:
15721d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ObjCObjectLValueCast:
15731d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingRealToComplex:
15741d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToReal:
15751d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexCast:
15761d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToIntegralComplex:
15771d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralRealToComplex:
15781d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToReal:
15791d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexCast:
15801d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToFloatingComplex:
158133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
158233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
158333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
158433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
1585e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case CK_ZeroToOCLEvent:
15861d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(!getType()->isBooleanType() && "unheralded conversion to bool");
15871d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
15881d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
15891d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_Dependent:
15901d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_LValueToRValue:
15911d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NoOp:
15927a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
15937a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
15941d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_PointerToBoolean:
15951d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToBoolean:
15961d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingToBoolean:
15971d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_MemberPointerToBoolean:
15981d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToBoolean:
15991d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToBoolean:
16001d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_LValueBitCast:            // -> bool&
16011d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_UserDefinedConversion:    // operator bool()
1602a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  case CK_BuiltinFnToFnPtr:
16031d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  CheckNoBasePath:
16041d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(path_empty() && "Cast kind should not have a base path!");
16051d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    break;
16061d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  }
1607651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return true;
16081d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall}
16091d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
1610f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlssonconst char *CastExpr::getCastKindName() const {
1611f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  switch (getCastKind()) {
1612daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_Dependent:
1613daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "Dependent";
16142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BitCast:
1615f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "BitCast";
16162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_LValueBitCast:
1617e39a3894513349908cdb3beba2614e53cb288e6cDouglas Gregor    return "LValueBitCast";
16180ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_LValueToRValue:
16190ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall    return "LValueToRValue";
16202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NoOp:
1621f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "NoOp";
16222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerived:
162311de6de25a0110cd7be97eef761ef3b189781da6Anders Carlsson    return "BaseToDerived";
16242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBase:
1625f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "DerivedToBase";
16262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UncheckedDerivedToBase:
162723cba801e11b03929c44f8cf54578305963a3476John McCall    return "UncheckedDerivedToBase";
16282de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_Dynamic:
1629f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "Dynamic";
16302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToUnion:
1631f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ToUnion";
16322de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ArrayToPointerDecay:
1633f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ArrayToPointerDecay";
16342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FunctionToPointerDecay:
1635f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "FunctionToPointerDecay";
16362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NullToMemberPointer:
1637f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "NullToMemberPointer";
1638404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall  case CK_NullToPointer:
1639404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall    return "NullToPointer";
16402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerivedMemberPointer:
1641f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "BaseToDerivedMemberPointer";
16422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBaseMemberPointer:
16431a31a18db9d657751f38c724adc0d62e86852bd7Anders Carlsson    return "DerivedToBaseMemberPointer";
16444d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
16454d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    return "ReinterpretMemberPointer";
16462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UserDefinedConversion:
1647f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "UserDefinedConversion";
16482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ConstructorConversion:
1649f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ConstructorConversion";
16502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToPointer:
16517f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson    return "IntegralToPointer";
16522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_PointerToIntegral:
16537f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson    return "PointerToIntegral";
1654daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_PointerToBoolean:
1655daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "PointerToBoolean";
16562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToVoid:
1657ebeaf2031c968143c531bfe232d7507f20c57347Anders Carlsson    return "ToVoid";
16582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_VectorSplat:
165916a8904f3f5ed19158657e1da95e5902fbee66f7Anders Carlsson    return "VectorSplat";
16602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralCast:
166182debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "IntegralCast";
1662daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_IntegralToBoolean:
1663daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "IntegralToBoolean";
16642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToFloating:
166582debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "IntegralToFloating";
16662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FloatingToIntegral:
166782debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "FloatingToIntegral";
16682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FloatingCast:
1669c6b29163557d02da5d2a4a06f986f0480291f51fBenjamin Kramer    return "FloatingCast";
1670daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_FloatingToBoolean:
1671daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "FloatingToBoolean";
16722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_MemberPointerToBoolean:
1673bc0e0781da778bd5eb41a810419912893ae20448Anders Carlsson    return "MemberPointerToBoolean";
16741d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
16751d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return "CPointerToObjCPointerCast";
16761d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
16771d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return "BlockPointerToObjCPointerCast";
16782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_AnyPointerToBlockPointerCast:
16793b27f1a80e4e433b503efd344c909eeafaa9033cFariborz Jahanian    return "AnyPointerToBlockPointerCast";
16802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ObjCObjectLValueCast:
1681569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    return "ObjCObjectLValueCast";
16822bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_FloatingRealToComplex:
16832bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "FloatingRealToComplex";
1684f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToReal:
1685f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToReal";
1686f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToBoolean:
1687f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToBoolean";
16882bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_FloatingComplexCast:
16892bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "FloatingComplexCast";
1690f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToIntegralComplex:
1691f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToIntegralComplex";
16922bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_IntegralRealToComplex:
16932bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "IntegralRealToComplex";
1694f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToReal:
1695f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToReal";
1696f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToBoolean:
1697f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToBoolean";
16982bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_IntegralComplexCast:
16992bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "IntegralComplexCast";
1700f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToFloatingComplex:
1701f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToFloatingComplex";
170233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
170333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCConsumeObject";
170433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
170533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCProduceObject";
170633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
170733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCReclaimReturnedObject";
170833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
1709651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return "ARCExtendBlockObject";
17107a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
17117a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    return "AtomicToNonAtomic";
17127a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
17137a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    return "NonAtomicToAtomic";
1714ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
1715ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor    return "CopyAndAutoreleaseBlockObject";
1716a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  case CK_BuiltinFnToFnPtr:
1717a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman    return "BuiltinFnToFnPtr";
1718e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case CK_ZeroToOCLEvent:
1719e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    return "ZeroToOCLEvent";
1720651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  case CK_AddressSpaceConversion:
1721651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return "AddressSpaceConversion";
1722f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  }
17231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17242bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  llvm_unreachable("Unhandled cast kind!");
1725f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson}
1726f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson
17276eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas GregorExpr *CastExpr::getSubExprAsWritten() {
17286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Expr *SubExpr = nullptr;
17296eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  CastExpr *E = this;
17306eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  do {
17316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    SubExpr = E->getSubExpr();
173203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
173303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    // Skip through reference binding to temporary.
173403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
173503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                  = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
173603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      SubExpr = Materialize->GetTemporaryExpr();
173703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
17386eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Skip any temporary bindings; they're implicit.
17396eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
17406eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = Binder->getSubExpr();
1741c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
17426eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Conversions by constructor and conversion functions have a
17436eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // subexpression describing the call; strip it off.
17442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (E->getCastKind() == CK_ConstructorConversion)
17456eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
17462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    else if (E->getCastKind() == CK_UserDefinedConversion)
17476eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
1748c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
17496eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // If the subexpression we're left with is an implicit cast, look
17506eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // through that, too.
1751c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1752c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
17536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return SubExpr;
17546eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
17556eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1756f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCXXBaseSpecifier **CastExpr::path_buffer() {
1757f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  switch (getStmtClass()) {
1758f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define ABSTRACT_STMT(x)
1759f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define CASTEXPR(Type, Base) \
1760f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  case Stmt::Type##Class: \
1761f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1762f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define STMT(Type, Base)
1763f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#include "clang/AST/StmtNodes.inc"
1764f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  default:
1765f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    llvm_unreachable("non-cast expressions not possible here");
1766f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  }
1767f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1768f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1769f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallvoid CastExpr::setCastPath(const CXXCastPath &Path) {
1770f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  assert(Path.size() == path_size());
1771f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1772f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1773f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
177405ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
1775f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                           CastKind Kind, Expr *Operand,
1776f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                           const CXXCastPath *BasePath,
17775baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                           ExprValueKind VK) {
1778f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  unsigned PathSize = (BasePath ? BasePath->size() : 0);
1779f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1780f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1781f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  ImplicitCastExpr *E =
17825baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
1783f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (PathSize) E->setCastPath(*BasePath);
1784f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return E;
1785f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1786f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
178705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
1788f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                                unsigned PathSize) {
1789f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1790f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1791f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1792f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1793f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1794f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
179505ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
1796f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                       ExprValueKind VK, CastKind K, Expr *Op,
1797f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       const CXXCastPath *BasePath,
1798f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       TypeSourceInfo *WrittenTy,
1799f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       SourceLocation L, SourceLocation R) {
1800f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  unsigned PathSize = (BasePath ? BasePath->size() : 0);
1801f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1802f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1803f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CStyleCastExpr *E =
1804f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
1805f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (PathSize) E->setCastPath(*BasePath);
1806f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return E;
1807f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1808f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
180905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
181005ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                                            unsigned PathSize) {
1811f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1812f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1813f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1814f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1815f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
18165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
18175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "<<=".
18180bea86307eb8c16339315a1e261fc490eb505c5bDavid BlaikieStringRef BinaryOperator::getOpcodeStr(Opcode Op) {
18195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
18202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_PtrMemD:   return ".*";
18212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_PtrMemI:   return "->*";
18222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:       return "*";
18232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Div:       return "/";
18242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Rem:       return "%";
18252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:       return "+";
18262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:       return "-";
18272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shl:       return "<<";
18282de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shr:       return ">>";
18292de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LT:        return "<";
18302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_GT:        return ">";
18312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LE:        return "<=";
18322de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_GE:        return ">=";
18332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_EQ:        return "==";
18342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_NE:        return "!=";
18352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_And:       return "&";
18362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Xor:       return "^";
18372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Or:        return "|";
18382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LAnd:      return "&&";
18392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LOr:       return "||";
18402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Assign:    return "=";
18412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_MulAssign: return "*=";
18422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_DivAssign: return "/=";
18432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_RemAssign: return "%=";
18442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_AddAssign: return "+=";
18452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_SubAssign: return "-=";
18462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_ShlAssign: return "<<=";
18472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_ShrAssign: return ">>=";
18482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_AndAssign: return "&=";
18492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_XorAssign: return "^=";
18502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_OrAssign:  return "|=";
18512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Comma:     return ",";
18525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1853baf534875ed0a55c6342636ff3f4602b8ac22b69Douglas Gregor
18543026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid OpCode!");
18555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCallBinaryOperatorKind
1858063daf6e196c51f162e0485478355d8e280eef5cDouglas GregorBinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1859063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  switch (OO) {
1860b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not an overloadable binary operator");
18612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Plus: return BO_Add;
18622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Minus: return BO_Sub;
18632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Star: return BO_Mul;
18642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Slash: return BO_Div;
18652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Percent: return BO_Rem;
18662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Caret: return BO_Xor;
18672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Amp: return BO_And;
18682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Pipe: return BO_Or;
18692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Equal: return BO_Assign;
18702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Less: return BO_LT;
18712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Greater: return BO_GT;
18722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PlusEqual: return BO_AddAssign;
18732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_MinusEqual: return BO_SubAssign;
18742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_StarEqual: return BO_MulAssign;
18752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_SlashEqual: return BO_DivAssign;
18762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PercentEqual: return BO_RemAssign;
18772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_CaretEqual: return BO_XorAssign;
18782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_AmpEqual: return BO_AndAssign;
18792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PipeEqual: return BO_OrAssign;
18802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessLess: return BO_Shl;
18812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterGreater: return BO_Shr;
18822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessLessEqual: return BO_ShlAssign;
18832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterGreaterEqual: return BO_ShrAssign;
18842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_EqualEqual: return BO_EQ;
18852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_ExclaimEqual: return BO_NE;
18862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessEqual: return BO_LE;
18872de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterEqual: return BO_GE;
18882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_AmpAmp: return BO_LAnd;
18892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PipePipe: return BO_LOr;
18902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Comma: return BO_Comma;
18912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_ArrowStar: return BO_PtrMemI;
1892063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  }
1893063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor}
1894063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor
1895063daf6e196c51f162e0485478355d8e280eef5cDouglas GregorOverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1896063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  static const OverloadedOperatorKind OverOps[] = {
1897063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1898063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Star, OO_Slash, OO_Percent,
1899063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Plus, OO_Minus,
1900063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_LessLess, OO_GreaterGreater,
1901063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1902063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_EqualEqual, OO_ExclaimEqual,
1903063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Amp,
1904063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Caret,
1905063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Pipe,
1906063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_AmpAmp,
1907063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PipePipe,
1908063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Equal, OO_StarEqual,
1909063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_SlashEqual, OO_PercentEqual,
1910063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PlusEqual, OO_MinusEqual,
1911063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_LessLessEqual, OO_GreaterGreaterEqual,
1912063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_AmpEqual, OO_CaretEqual,
1913063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PipeEqual,
1914063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Comma
1915063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  };
1916063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  return OverOps[Opc];
1917063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor}
1918063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor
191905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperInitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
19203b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                           ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
1921bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1922561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         false, false),
19233b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    InitExprs(C, initExprs.size()),
19246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(nullptr, true)
192532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl{
192632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  sawArrayRangeDesignator(false);
19273b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned I = 0; I != initExprs.size(); ++I) {
1928ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    if (initExprs[I]->isTypeDependent())
19298e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall      ExprBits.TypeDependent = true;
1930ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    if (initExprs[I]->isValueDependent())
19318e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall      ExprBits.ValueDependent = true;
1932561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (initExprs[I]->isInstantiationDependent())
1933561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
1934bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (initExprs[I]->containsUnexpandedParameterPack())
1935bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
193673460a32bc5299a5927d23d2e464d72af796eabfDouglas Gregor  }
1937c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
19383b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
193966b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
19405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
194105ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
1942ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  if (NumInits > InitExprs.size())
1943709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs.reserve(C, NumInits);
1944fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor}
1945fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
194605ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
19476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  InitExprs.resize(C, NumInits, nullptr);
19484c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
19494c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
195005ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperExpr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
1951ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  if (Init >= InitExprs.size()) {
19526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, nullptr);
1953651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    setInit(Init, expr);
19546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
19554c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
19561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19574c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1958651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  setInit(Init, expr);
19594c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
19604c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
19614c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
19623e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidisvoid InitListExpr::setArrayFiller(Expr *filler) {
196321f77cd0c3da8a1dbaf6245cae43baf4c0b80ea4Argyrios Kyrtzidis  assert(!hasArrayFiller() && "Filler already set!");
19643e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  ArrayFillerOrUnionFieldInit = filler;
19653e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  // Fill out any "holes" in the array due to designated initializers.
19663e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  Expr **inits = getInits();
19673e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  for (unsigned i = 0, e = getNumInits(); i != e; ++i)
19686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (inits[i] == nullptr)
19693e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis      inits[i] = filler;
19703e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis}
19713e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis
1972fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smithbool InitListExpr::isStringLiteralInit() const {
1973fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith  if (getNumInits() != 1)
1974fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith    return false;
1975f0a26499c0ef62c7940b596be092315922a46ab7Eli Friedman  const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1976f0a26499c0ef62c7940b596be092315922a46ab7Eli Friedman  if (!AT || !AT->getElementType()->isIntegerType())
1977fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith    return false;
1978651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // It is possible for getInit() to return null.
1979651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const Expr *Init = getInit(0);
1980651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!Init)
1981651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return false;
1982651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Init = Init->IgnoreParens();
1983fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith  return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1984fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith}
1985fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith
198665d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik VerbruggenSourceLocation InitListExpr::getLocStart() const {
198723700f083fb72f5c6792e253f203a43aba3cef86Abramo Bagnara  if (InitListExpr *SyntacticForm = getSyntacticForm())
198865d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen    return SyntacticForm->getLocStart();
198965d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  SourceLocation Beg = LBraceLoc;
1990c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (Beg.isInvalid()) {
1991c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    // Find the first non-null initializer.
1992c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    for (InitExprsTy::const_iterator I = InitExprs.begin(),
1993c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek                                     E = InitExprs.end();
1994c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      I != E; ++I) {
1995c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      if (Stmt *S = *I) {
1996c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        Beg = S->getLocStart();
1997c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        break;
1998c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      }
1999c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    }
2000c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  }
200165d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  return Beg;
200265d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen}
200365d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen
200465d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik VerbruggenSourceLocation InitListExpr::getLocEnd() const {
200565d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  if (InitListExpr *SyntacticForm = getSyntacticForm())
200665d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen    return SyntacticForm->getLocEnd();
200765d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  SourceLocation End = RBraceLoc;
2008c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (End.isInvalid()) {
2009c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    // Find the first non-null initializer from the end.
2010c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
201165d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen         E = InitExprs.rend();
201265d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen         I != E; ++I) {
2013c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      if (Stmt *S = *I) {
201465d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen        End = S->getLocEnd();
2015c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        break;
201665d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen      }
2017c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    }
2018c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  }
201965d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  return End;
2020c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek}
2021c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek
2022bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block.
20234eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
2024a345edb668380691fc92d4e4aa0a5ffec366ca6aJohn McCallconst FunctionProtoType *BlockExpr::getFunctionType() const {
2025a345edb668380691fc92d4e4aa0a5ffec366ca6aJohn McCall  // The block pointer is never sugared, but the function type might be.
2026a345edb668380691fc92d4e4aa0a5ffec366ca6aJohn McCall  return cast<BlockPointerType>(getType())
2027a345edb668380691fc92d4e4aa0a5ffec366ca6aJohn McCall           ->getPointeeType()->castAs<FunctionProtoType>();
20284eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
20294eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
20301eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSourceLocation BlockExpr::getCaretLocation() const {
20311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getCaretLocation();
203256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
20331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst Stmt *BlockExpr::getBody() const {
20347297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor  return TheBlock->getBody();
20357297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
20361eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt *BlockExpr::getBody() {
20371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getBody();
20387297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
203956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
204056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
20415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
20425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines
20435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
20445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2045026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// isUnusedResultAWarning - Return true if this immediate expression should
2046026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// be warned about if the result is unused.  If so, fill in Loc and Ranges
2047026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// with location to warn on and the source range[s] to report with the
2048026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// warning.
2049a6115068cde719142eb394db88612c185cabd05bEli Friedmanbool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
2050a6115068cde719142eb394db88612c185cabd05bEli Friedman                                  SourceRange &R1, SourceRange &R2,
2051a6115068cde719142eb394db88612c185cabd05bEli Friedman                                  ASTContext &Ctx) const {
2052ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // Don't warn if the expr is type dependent. The type could end up
2053ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // instantiating to void.
2054ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  if (isTypeDependent())
2055ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson    return false;
20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
20585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
20590faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
20600faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
2061a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
2062026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = getExprLoc();
2063026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
2064026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
20655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass:
2066026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return cast<ParenExpr>(this)->getSubExpr()->
2067a6115068cde719142eb394db88612c185cabd05bEli Friedman      isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2068f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
2069f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()->
2070a6115068cde719142eb394db88612c185cabd05bEli Friedman      isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2071a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman  case ChooseExprClass:
2072a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    return cast<ChooseExpr>(this)->getChosenSubExpr()->
2073a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
20745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UnaryOperatorClass: {
20755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const UnaryOperator *UO = cast<UnaryOperator>(this);
20761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (UO->getOpcode()) {
2078a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_Plus:
2079a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_Minus:
2080a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_AddrOf:
2081a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_Not:
2082a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_LNot:
2083a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_Deref:
2084a6115068cde719142eb394db88612c185cabd05bEli Friedman      break;
2085a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    case UO_Coawait:
2086a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      // This is just the 'operator co_await' call inside the guts of a
2087a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      // dependent co_await call.
20882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostInc:
20892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostDec:
20902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreInc:
20912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreDec:                 // ++/--
2092026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;  // Not a warning.
20932de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Real:
20942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Imag:
20955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // accessing a piece of a volatile complex is a side-effect.
2096df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2097df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump          .isVolatileQualified())
2098026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
2099026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
21002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
2101a6115068cde719142eb394db88612c185cabd05bEli Friedman      return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
21025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2103a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
2104026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = UO->getOperatorLoc();
2105026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = UO->getSubExpr()->getSourceRange();
2106026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2108e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  case BinaryOperatorClass: {
2109026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const BinaryOperator *BO = cast<BinaryOperator>(this);
2110c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek    switch (BO->getOpcode()) {
2111c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek      default:
2112c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        break;
211325973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider the RHS of comma for side effects. LHS was checked by
211425973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Sema::CheckCommaOperands.
21152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_Comma:
2116c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // ((foo = <blah>), 0) is an idiom for hiding the result (and
2117c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // lvalue-ness) of an assignment written in a macro.
2118c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        if (IntegerLiteral *IE =
2119c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek              dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2120c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek          if (IE->getValue() == 0)
2121c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek            return false;
2122a6115068cde719142eb394db88612c185cabd05bEli Friedman        return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
212325973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider '||', '&&' to have side effects if the LHS or RHS does.
21242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LAnd:
21252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LOr:
2126a6115068cde719142eb394db88612c185cabd05bEli Friedman        if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2127a6115068cde719142eb394db88612c185cabd05bEli Friedman            !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
212825973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis          return false;
212925973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        break;
2130bf0ee354163f87623a4b60412544243911332343John McCall    }
2131026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (BO->isAssignmentOp())
2132026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
2133a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
2134026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = BO->getOperatorLoc();
2135026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = BO->getLHS()->getSourceRange();
2136026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = BO->getRHS()->getSourceRange();
2137026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
2138e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  }
2139eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner  case CompoundAssignOperatorClass:
2140c6dfe194f623b02c123759f235b504d4850fc178Douglas Gregor  case VAArgExprClass:
2141276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  case AtomicExprClass:
2142026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
21435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2144ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  case ConditionalOperatorClass: {
2145fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // If only one of the LHS or RHS is a warning, the operator might
2146fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // be being used for control flow. Only warn if both the LHS and
2147fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // RHS are warnings.
2148ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
2149a6115068cde719142eb394db88612c185cabd05bEli Friedman    if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
2150fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek      return false;
2151fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    if (!Exp->getLHS())
2152026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return true;
2153a6115068cde719142eb394db88612c185cabd05bEli Friedman    return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2154ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  }
2155ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian
21565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case MemberExprClass:
2157a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
2158026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<MemberExpr>(this)->getMemberLoc();
2159026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = SourceRange(Loc, Loc);
2160026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2161026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
21621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ArraySubscriptExprClass:
2164a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
2165026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2166026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2167026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2168026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
2169211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman
21709b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth  case CXXOperatorCallExprClass: {
2171651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator
21729b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // overloads as there is no reasonable way to define these such that they
21739b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // have non-trivial, desirable side-effects. See the -Wunused-comparison
2174651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // warning: operators == and != are commonly typo'ed, and so warning on them
21759b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // provides additional value as well. If this list is updated,
21769b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // DiagnoseUnusedComparison should be as well.
21779b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2178651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    switch (Op->getOperator()) {
2179651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    default:
2180651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      break;
2181651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case OO_EqualEqual:
2182651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case OO_ExclaimEqual:
2183651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case OO_Less:
2184651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case OO_Greater:
2185651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case OO_GreaterEqual:
2186651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case OO_LessEqual:
21870e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (Op->getCallReturnType(Ctx)->isReferenceType() ||
21880e2c34f92f00628d48968dfea096d36381f494cbStephen Hines          Op->getCallReturnType(Ctx)->isVoidType())
21896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        break;
2190a6115068cde719142eb394db88612c185cabd05bEli Friedman      WarnE = this;
21916e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay      Loc = Op->getOperatorLoc();
21926e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay      R1 = Op->getSourceRange();
21939b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth      return true;
21946e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay    }
21959b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth
21969b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // Fallthrough for generic call handling.
21979b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth  }
21985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case CallExprClass:
21999fcce65e7e1307b5b8da9be13e4092d6bb94dc1dRichard Smith  case CXXMemberCallExprClass:
22009fcce65e7e1307b5b8da9be13e4092d6bb94dc1dRichard Smith  case UserDefinedLiteralClass: {
2201026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If this is a direct call, get the callee.
2202026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const CallExpr *CE = cast<CallExpr>(this);
2203d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    if (const Decl *FD = CE->getCalleeDecl()) {
220458878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar      const FunctionDecl *Func = dyn_cast<FunctionDecl>(FD);
220558878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar      bool HasWarnUnusedResultAttr = Func ? Func->hasUnusedResultAttr()
220658878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar                                          : FD->hasAttr<WarnUnusedResultAttr>();
220758878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar
2208026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // If the callee has attribute pure, const, or warn_unused_result, warn
2209026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // about it. void foo() { strlen("bar"); } should warn.
2210bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      //
2211bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2212bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // updated to match for QoI.
221358878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar      if (HasWarnUnusedResultAttr ||
2214651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
2215a6115068cde719142eb394db88612c185cabd05bEli Friedman        WarnE = this;
2216bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Loc = CE->getCallee()->getLocStart();
2217bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        R1 = CE->getCallee()->getSourceRange();
2218bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner
2219bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        if (unsigned NumArgs = CE->getNumArgs())
2220bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          R2 = SourceRange(CE->getArg(0)->getLocStart(),
2221bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner                           CE->getArg(NumArgs-1)->getLocEnd());
2222bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return true;
2223bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
2224026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    }
2225026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
2226026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner  }
222758beed91d468863b8c85bce43425422703838d27Anders Carlsson
222884c3b9745f813a784b5d8ce77f2785750523d9ebMatt Beaumont-Gay  // If we don't know precisely what we're looking at, let's not warn.
222984c3b9745f813a784b5d8ce77f2785750523d9ebMatt Beaumont-Gay  case UnresolvedLookupExprClass:
223084c3b9745f813a784b5d8ce77f2785750523d9ebMatt Beaumont-Gay  case CXXUnresolvedConstructExprClass:
223184c3b9745f813a784b5d8ce77f2785750523d9ebMatt Beaumont-Gay    return false;
223284c3b9745f813a784b5d8ce77f2785750523d9ebMatt Beaumont-Gay
223358beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXTemporaryObjectExprClass:
223481e4549fede62835235a53c024909c3fab6b73acLubos Lunak  case CXXConstructExprClass: {
223581e4549fede62835235a53c024909c3fab6b73acLubos Lunak    if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
223681e4549fede62835235a53c024909c3fab6b73acLubos Lunak      if (Type->hasAttr<WarnUnusedAttr>()) {
223781e4549fede62835235a53c024909c3fab6b73acLubos Lunak        WarnE = this;
223881e4549fede62835235a53c024909c3fab6b73acLubos Lunak        Loc = getLocStart();
223981e4549fede62835235a53c024909c3fab6b73acLubos Lunak        R1 = getSourceRange();
224081e4549fede62835235a53c024909c3fab6b73acLubos Lunak        return true;
224181e4549fede62835235a53c024909c3fab6b73acLubos Lunak      }
224281e4549fede62835235a53c024909c3fab6b73acLubos Lunak    }
224358beed91d468863b8c85bce43425422703838d27Anders Carlsson    return false;
224481e4549fede62835235a53c024909c3fab6b73acLubos Lunak  }
224558beed91d468863b8c85bce43425422703838d27Anders Carlsson
2246f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  case ObjCMessageExprClass: {
2247f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
22484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Ctx.getLangOpts().ObjCAutoRefCount &&
2249f85e193739c953358c865005855253af4f68a497John McCall        ME->isInstanceMessage() &&
2250f85e193739c953358c865005855253af4f68a497John McCall        !ME->getType()->isVoidType() &&
22514bdb602d1a70ea432aad909825eb4748a2aca768Jean-Daniel Dupas        ME->getMethodFamily() == OMF_init) {
2252a6115068cde719142eb394db88612c185cabd05bEli Friedman      WarnE = this;
2253f85e193739c953358c865005855253af4f68a497John McCall      Loc = getExprLoc();
2254f85e193739c953358c865005855253af4f68a497John McCall      R1 = ME->getSourceRange();
2255f85e193739c953358c865005855253af4f68a497John McCall      return true;
2256f85e193739c953358c865005855253af4f68a497John McCall    }
2257f85e193739c953358c865005855253af4f68a497John McCall
2258176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (const ObjCMethodDecl *MD = ME->getMethodDecl())
22590e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (MD->hasAttr<WarnUnusedResultAttr>()) {
2260176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        WarnE = this;
2261176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        Loc = getExprLoc();
2262176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        return true;
2263176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
2264176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
2265026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
2266f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  }
22671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
226812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  case ObjCPropertyRefExprClass:
2269a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
22705e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    Loc = getExprLoc();
22715e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    R1 = getSourceRange();
2272a50089ec68a583d13718107c1b0c898f0903709eChris Lattner    return true;
227312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
22744b9c2d235fb9449e249d74f48ecfec601650de93John McCall  case PseudoObjectExprClass: {
22754b9c2d235fb9449e249d74f48ecfec601650de93John McCall    const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
22764b9c2d235fb9449e249d74f48ecfec601650de93John McCall
22774b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // Only complain about things that have the form of a getter.
22784b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
22794b9c2d235fb9449e249d74f48ecfec601650de93John McCall        isa<BinaryOperator>(PO->getSyntacticForm()))
22804b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return false;
22814b9c2d235fb9449e249d74f48ecfec601650de93John McCall
2282a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
22834b9c2d235fb9449e249d74f48ecfec601650de93John McCall    Loc = getExprLoc();
22844b9c2d235fb9449e249d74f48ecfec601650de93John McCall    R1 = getSourceRange();
22854b9c2d235fb9449e249d74f48ecfec601650de93John McCall    return true;
22864b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
22874b9c2d235fb9449e249d74f48ecfec601650de93John McCall
2288611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  case StmtExprClass: {
2289611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // Statement exprs don't logically have side effects themselves, but are
2290611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // sometimes used in macros in ways that give them a type that is unused.
2291611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2292611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // however, if the result of the stmt expr is dead, we don't want to emit a
2293611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // warning.
2294611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
2295d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    if (!CS->body_empty()) {
2296611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner      if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
2297a6115068cde719142eb394db88612c185cabd05bEli Friedman        return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2298d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis      if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2299d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis        if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
2300a6115068cde719142eb394db88612c185cabd05bEli Friedman          return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2301d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    }
23021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23030faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
23040faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
2305a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
2306026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<StmtExpr>(this)->getLParenLoc();
2307026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
2308026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
2309611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  }
23106319917b5021e9602389b49ca4f245d235e9b90aEli Friedman  case CXXFunctionalCastExprClass:
2311a6115068cde719142eb394db88612c185cabd05bEli Friedman  case CStyleCastExprClass: {
23124059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman    // Ignore an explicit cast to void unless the operand is a non-trivial
2313a6115068cde719142eb394db88612c185cabd05bEli Friedman    // volatile lvalue.
23144059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman    const CastExpr *CE = cast<CastExpr>(this);
2315a6115068cde719142eb394db88612c185cabd05bEli Friedman    if (CE->getCastKind() == CK_ToVoid) {
2316a6115068cde719142eb394db88612c185cabd05bEli Friedman      if (CE->getSubExpr()->isGLValue() &&
23174059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman          CE->getSubExpr()->getType().isVolatileQualified()) {
23184059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman        const DeclRefExpr *DRE =
23194059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman            dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
23204059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman        if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
23214059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman              cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
23224059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman          return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
23234059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman                                                          R1, R2, Ctx);
23244059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman        }
23254059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman      }
23260faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
2327a6115068cde719142eb394db88612c185cabd05bEli Friedman    }
23284059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman
2329a6115068cde719142eb394db88612c185cabd05bEli Friedman    // If this is a cast to a constructor conversion, check the operand.
233058beed91d468863b8c85bce43425422703838d27Anders Carlsson    // Otherwise, the result of the cast is unused.
2331a6115068cde719142eb394db88612c185cabd05bEli Friedman    if (CE->getCastKind() == CK_ConstructorConversion)
2332a6115068cde719142eb394db88612c185cabd05bEli Friedman      return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
23334059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman
2334a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
23354059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman    if (const CXXFunctionalCastExpr *CXXCE =
23364059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman            dyn_cast<CXXFunctionalCastExpr>(this)) {
2337cdd4b78583120222b82148626119b3e80ae1d291Eli Friedman      Loc = CXXCE->getLocStart();
23384059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman      R1 = CXXCE->getSubExpr()->getSourceRange();
23394059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman    } else {
23404059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman      const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
23414059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman      Loc = CStyleCE->getLParenLoc();
23424059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman      R1 = CStyleCE->getSubExpr()->getSourceRange();
23434059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman    }
2344026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
234558beed91d468863b8c85bce43425422703838d27Anders Carlsson  }
2346a6115068cde719142eb394db88612c185cabd05bEli Friedman  case ImplicitCastExprClass: {
2347a6115068cde719142eb394db88612c185cabd05bEli Friedman    const CastExpr *ICE = cast<ImplicitCastExpr>(this);
2348a6115068cde719142eb394db88612c185cabd05bEli Friedman
2349a6115068cde719142eb394db88612c185cabd05bEli Friedman    // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2350a6115068cde719142eb394db88612c185cabd05bEli Friedman    if (ICE->getCastKind() == CK_LValueToRValue &&
2351a6115068cde719142eb394db88612c185cabd05bEli Friedman        ICE->getSubExpr()->getType().isVolatileQualified())
2352a6115068cde719142eb394db88612c185cabd05bEli Friedman      return false;
23534be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman
2354a6115068cde719142eb394db88612c185cabd05bEli Friedman    return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2355a6115068cde719142eb394db88612c185cabd05bEli Friedman  }
235604421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
2357df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXDefaultArgExpr>(this)
2358a6115068cde719142eb394db88612c185cabd05bEli Friedman            ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
2359c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  case CXXDefaultInitExprClass:
2360c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    return (cast<CXXDefaultInitExpr>(this)
2361c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith            ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
23624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
23634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXNewExprClass:
23644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // FIXME: In theory, there might be new expressions that don't have side
23654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // effects (e.g. a placement new with an uninitialized POD).
23664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXDeleteExprClass:
2367026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
23682d46eb21eb2c904831b0e9f75ab3523384c70e66Anders Carlsson  case CXXBindTemporaryExprClass:
2369df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXBindTemporaryExpr>(this)
2370a6115068cde719142eb394db88612c185cabd05bEli Friedman            ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
23714765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case ExprWithCleanupsClass:
23724765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall    return (cast<ExprWithCleanups>(this)
2373a6115068cde719142eb394db88612c185cabd05bEli Friedman            ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
23744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
23755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
23765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
237744baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian/// isOBJCGCCandidate - Check if an expression is objc gc'able.
23787f4f86a2167abc116275e49c81350fc3225485e5Fariborz Jahanian/// returns true, if it is; false otherwise.
2379102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanianbool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
2380f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  const Expr *E = IgnoreParens();
2381f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  switch (E->getStmtClass()) {
238244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  default:
238344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
238444baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ObjCIvarRefExprClass:
238544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return true;
2386207c5210eb0ac7b632609f0c006eb97ef2738948Fariborz Jahanian  case Expr::UnaryOperatorClass:
2387f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
238844baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ImplicitCastExprClass:
2389f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
239003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass:
239103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
239203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                                      ->isOBJCGCCandidate(Ctx);
239306b89124a9a5971a0528cc9da6817740bac43164Fariborz Jahanian  case CStyleCastExprClass:
2394f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
2395a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  case DeclRefExprClass: {
2396f4b88a45902af1802a1cb42ba48b1c474474f228John McCall    const Decl *D = cast<DeclRefExpr>(E)->getDecl();
2397e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian
2398102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2399102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      if (VD->hasGlobalStorage())
2400102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian        return true;
2401102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      QualType T = VD->getType();
240259a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // dereferencing to a  pointer is always a gc'able candidate,
240359a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // unless it is __weak.
24047e88a60d38b36695520e4f8d9279766ef111a662Daniel Dunbar      return T->isPointerType() &&
24050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall             (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
2406102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    }
240744baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
240844baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
240983f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  case MemberExprClass: {
2410f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    const MemberExpr *M = cast<MemberExpr>(E);
2411102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return M->getBase()->isOBJCGCCandidate(Ctx);
241244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
241344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ArraySubscriptExprClass:
2414f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
241544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
241644baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian}
2417369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
241811ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidisbool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
241911ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis  if (isTypeDependent())
242011ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis    return false;
24217eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
242211ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis}
242311ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis
2424864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCallQualType Expr::findBoundMemberType(const Expr *expr) {
2425e0a22d06888c13989b3f72db319f1d498bf69153John McCall  assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
2426864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
2427864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Bound member expressions are always one of these possibilities:
2428864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  //   x->m      x.m      x->*y      x.*y
2429864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // (possibly parenthesized)
2430864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
2431864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  expr = expr->IgnoreParens();
2432864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2433864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2434864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return mem->getMemberDecl()->getType();
2435864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
2436864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
2437864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2438864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2439864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall                      ->getPointeeType();
2440864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    assert(type->isFunctionType());
2441864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return type;
2442864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
2443864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
24440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  assert(isa<UnresolvedMemberExpr>(expr) || isa<CXXPseudoDestructorExpr>(expr));
2445864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  return QualType();
2446864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall}
2447864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
24484e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() {
24494e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  Expr* E = this;
2450b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara  while (true) {
2451b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2452b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      E = P->getSubExpr();
2453b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2454b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2455b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2456b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2457b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2458b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2459b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2460b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2461f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2462f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2463f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2464f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2465f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2466f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
2467a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2468a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      if (!P->isConditionDependent()) {
2469a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman        E = P->getChosenSubExpr();
2470a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman        continue;
2471a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      }
2472a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    }
2473b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
2474b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara  }
24754e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek}
24764e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek
247756f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
247856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand.
247956f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() {
248056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  Expr *E = this;
248156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  while (true) {
2482a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    E = E->IgnoreParens();
2483b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
248456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
2485b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2486b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
248703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
248803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
248903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
249003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
249103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
2492c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2493c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2494c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2495c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2496c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2497b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
249856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  }
249956f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner}
250056f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner
25016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesExpr *Expr::IgnoreCasts() {
25026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Expr *E = this;
25036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  while (true) {
25046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
25056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      E = P->getSubExpr();
25066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      continue;
25076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
25086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (MaterializeTemporaryExpr *Materialize
25096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        = dyn_cast<MaterializeTemporaryExpr>(E)) {
25106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      E = Materialize->GetTemporaryExpr();
25116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      continue;
25126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
25136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (SubstNonTypeTemplateParmExpr *NTTP
25146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
25156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      E = NTTP->getReplacement();
25166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      continue;
25176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
25186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return E;
25196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
25206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
25216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
25229c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
25239c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// casts.  This is intended purely as a temporary workaround for code
25249c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// that hasn't yet been rewritten to do the right thing about those
25259c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// casts, and may disappear along with the last internal use.
2526f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallExpr *Expr::IgnoreParenLValueCasts() {
2527f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  Expr *E = this;
25289c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall  while (true) {
2529a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    E = E->IgnoreParens();
2530a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2531f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      if (P->getCastKind() == CK_LValueToRValue) {
2532f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        E = P->getSubExpr();
2533f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        continue;
2534f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
253503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    } else if (MaterializeTemporaryExpr *Materialize
253603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
253703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
253803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
2539c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    } else if (SubstNonTypeTemplateParmExpr *NTTP
2540c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2541c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2542c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2543f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
2544f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
2545f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
2546f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return E;
2547f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
2548632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola
2549632fbaa22fbed7c090eb83775731bfff786c2198Rafael EspindolaExpr *Expr::ignoreParenBaseCasts() {
2550632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola  Expr *E = this;
2551632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola  while (true) {
2552a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    E = E->IgnoreParens();
2553632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola    if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2554632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola      if (CE->getCastKind() == CK_DerivedToBase ||
2555632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola          CE->getCastKind() == CK_UncheckedDerivedToBase ||
2556632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola          CE->getCastKind() == CK_NoOp) {
2557632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola        E = CE->getSubExpr();
2558632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola        continue;
2559632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola      }
2560632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola    }
2561632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola
2562632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola    return E;
2563632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola  }
2564632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola}
2565632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola
25662fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCallExpr *Expr::IgnoreParenImpCasts() {
25672fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  Expr *E = this;
25682fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  while (true) {
2569a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    E = E->IgnoreParens();
2570b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
25712fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      E = P->getSubExpr();
2572b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2573b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
257403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
257503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
257603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
257703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
257803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
2579c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2580c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2581c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2582c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2583c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2584b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
25852fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  }
25862fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall}
25872fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall
25882f072b442879b8bba8c5dea11d7c61bedb1924aeHans WennborgExpr *Expr::IgnoreConversionOperator() {
25892f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
259014d251cd62942bf7d56bb87a267ba2ca2f7fae3eChandler Carruth    if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
25912f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg      return MCE->getImplicitObjectArgument();
25922f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  }
25932f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  return this;
25942f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg}
25952f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg
2596ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2597ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// value (including ptr->int casts of the same size).  Strip off any
2598ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// ParenExpr or CastExprs, returning their operand.
2599ecdd84147c0765caa999ddc22dde25b42712bb4dChris LattnerExpr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2600ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  Expr *E = this;
2601ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  while (true) {
2602a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    E = E->IgnoreParens();
26031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2604ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2605ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      // We ignore integer <-> casts that are of the same width, ptr<->ptr and
26062ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor      // ptr<->int casts of the same width.  We also ignore all identity casts.
2607ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      Expr *SE = P->getSubExpr();
26081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2609ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2610ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
2611ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
2612ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
26131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2614b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if ((E->getType()->isPointerType() ||
26159d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           E->getType()->isIntegralType(Ctx)) &&
2616b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara          (SE->getType()->isPointerType() ||
26179d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           SE->getType()->isIntegralType(Ctx)) &&
2618ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner          Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2619ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
2620ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
2621ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
2622ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    }
26231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2624c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2625c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2626c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2627c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2628c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2629c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor
2630ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    return E;
2631ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  }
2632ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner}
2633ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
26346eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorbool Expr::isDefaultArgument() const {
26356eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  const Expr *E = this;
263603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
263703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = M->GetTemporaryExpr();
263803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
26396eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
26406eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    E = ICE->getSubExprAsWritten();
2641c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
26426eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return isa<CXXDefaultArgExpr>(E);
26436eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2644ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
26452f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// \brief Skip over any no-op casts and any temporary-binding
26462f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// expressions.
2647f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlssonstatic const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
264803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
264903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = M->GetTemporaryExpr();
265003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
26512f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
26522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
26532f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
26542f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
26552f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
26562f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
26572f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
26582f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
26592f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    E = BE->getSubExpr();
26602f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
26612f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
26622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
26632f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
26642f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
26652f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
26662f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
2667f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson
2668f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson  return E->IgnoreParens();
26692f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
26702f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
2671558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// isTemporaryObject - Determines if this expression produces a
2672558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// temporary of the given class type.
2673558d2abc7f9fd6801cc7677200992313ae90b5d8John McCallbool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2674558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2675558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    return false;
26762f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
2677f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson  const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
26782f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
267958277b57f9492d0234748be89bcad48b322c5cf7John McCall  // Temporaries are by definition pr-values of class type.
2680db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian  if (!E->Classify(C).isPRValue()) {
2681db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian    // In this context, property reference is a message call and is pr-value.
268212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    if (!isa<ObjCPropertyRefExpr>(E))
2683db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian      return false;
2684db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian  }
26852f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
268619e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // Black-list a few cases which yield pr-values of class type that don't
268719e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // refer to temporaries of that type:
268819e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall
268919e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // - implicit derived-to-base conversions
2690558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (isa<ImplicitCastExpr>(E)) {
2691558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2692558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_DerivedToBase:
2693558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_UncheckedDerivedToBase:
2694558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      return false;
2695558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    default:
2696558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      break;
2697558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    }
26982f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
26992f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
270019e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // - member expressions (all)
270119e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  if (isa<MemberExpr>(E))
270219e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall    return false;
270319e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall
270432f498a675df990901e6659d610dc740f9423228Eli Friedman  if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
270532f498a675df990901e6659d610dc740f9423228Eli Friedman    if (BO->isPtrMemOp())
270632f498a675df990901e6659d610dc740f9423228Eli Friedman      return false;
270732f498a675df990901e6659d610dc740f9423228Eli Friedman
270856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // - opaque values (all)
270956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (isa<OpaqueValueExpr>(E))
271056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return false;
271156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
2712558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  return true;
27132f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
27142f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
271575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregorbool Expr::isImplicitCXXThis() const {
271675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  const Expr *E = this;
271775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
271875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  // Strip away parentheses and casts we don't care about.
271975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  while (true) {
272075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
272175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      E = Paren->getSubExpr();
272275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      continue;
272375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
272475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
272575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
272675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      if (ICE->getCastKind() == CK_NoOp ||
272775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_LValueToRValue ||
272875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_DerivedToBase ||
272975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_UncheckedDerivedToBase) {
273075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        E = ICE->getSubExpr();
273175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        continue;
273275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      }
273375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
273475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
273575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
273675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      if (UnOp->getOpcode() == UO_Extension) {
273775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        E = UnOp->getSubExpr();
273875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        continue;
273975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      }
274075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
274175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
274203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (const MaterializeTemporaryExpr *M
274303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
274403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = M->GetTemporaryExpr();
274503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
274603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
274703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
274875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    break;
274975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  }
275075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
275175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
275275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    return This->isImplicit();
275375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
275475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  return false;
275575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor}
275675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
2757898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyTypeDependentArguments - Determines if any of the expressions
2758898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is type-dependent.
2759cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenkobool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
276013a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles  for (unsigned I = 0; I < Exprs.size(); ++I)
2761898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isTypeDependent())
2762898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
2763898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2764898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
2765898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
2766898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
27676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesbool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef,
27686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 const Expr **Culprit) const {
2769c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // This function is attempting whether an expression is an initializer
277021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  // which can be evaluated at compile-time. It very closely parallels
277121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
277221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  // will lead to unexpected results.  Like ConstExprEmitter, it falls back
277321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  // to isEvaluatable most of the time.
277421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  //
27754204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // If we ever capture reference-binding directly in the AST, we can
27764204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // kill the second parameter.
27774204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
27784204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  if (IsForRef) {
27794204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    EvalResult Result;
27806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects)
27816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return true;
27826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (Culprit)
27836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      *Culprit = this;
27846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return false;
27854204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  }
27861f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman
2787e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  switch (getStmtClass()) {
2788c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  default: break;
2789e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case StringLiteralClass:
2790eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  case ObjCEncodeExprClass:
2791e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    return true;
2792b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXTemporaryObjectExprClass:
2793b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXConstructExprClass: {
2794b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall    const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
27954204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
279621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    if (CE->getConstructor()->isTrivial() &&
279721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        CE->getConstructor()->getParent()->hasTrivialDestructor()) {
279821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      // Trivial default constructor
2799180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!CE->getNumArgs()) return true;
2800180f47959a066795cc0f409433023af448bb0328Richard Smith
280121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      // Trivial copy constructor
280221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
28036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return CE->getArg(0)->isConstantInitializer(Ctx, false, Culprit);
2804180f47959a066795cc0f409433023af448bb0328Richard Smith    }
2805180f47959a066795cc0f409433023af448bb0328Richard Smith
2806180f47959a066795cc0f409433023af448bb0328Richard Smith    break;
2807b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  }
280859b5da6d853b4368b984700315adf7b37de05764Nate Begeman  case CompoundLiteralExprClass: {
28091f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // This handles gcc's extension that allows global initializers like
28101f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // "struct x {int x;} x = (struct x) {};".
28111f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This accepts other cases it shouldn't!
281259b5da6d853b4368b984700315adf7b37de05764Nate Begeman    const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
28136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return Exp->isConstantInitializer(Ctx, false, Culprit);
281459b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
2815b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  case DesignatedInitUpdateExprClass: {
2816b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    const DesignatedInitUpdateExpr *DIUE = cast<DesignatedInitUpdateExpr>(this);
2817b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    return DIUE->getBase()->isConstantInitializer(Ctx, false, Culprit) &&
2818b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar           DIUE->getUpdater()->isConstantInitializer(Ctx, false, Culprit);
2819b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  }
2820e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case InitListExprClass: {
282121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    const InitListExpr *ILE = cast<InitListExpr>(this);
282221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    if (ILE->getType()->isArrayType()) {
282321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      unsigned numInits = ILE->getNumInits();
282421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      for (unsigned i = 0; i < numInits; i++) {
28256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (!ILE->getInit(i)->isConstantInitializer(Ctx, false, Culprit))
282621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          return false;
282721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      }
282821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      return true;
2829e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    }
283021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman
283121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    if (ILE->getType()->isRecordType()) {
283221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      unsigned ElementNo = 0;
283321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2834176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      for (const auto *Field : RD->fields()) {
283521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        // If this is a union, skip all the fields that aren't being initialized.
2836176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        if (RD->isUnion() && ILE->getInitializedFieldInUnion() != Field)
283721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          continue;
283821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman
283921cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        // Don't emit anonymous bitfields, they just affect layout.
284021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        if (Field->isUnnamedBitfield())
284121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          continue;
284221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman
284321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        if (ElementNo < ILE->getNumInits()) {
284421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          const Expr *Elt = ILE->getInit(ElementNo++);
284521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          if (Field->isBitField()) {
284621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman            // Bitfields have to evaluate to an integer.
284721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman            llvm::APSInt ResultTmp;
28486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            if (!Elt->EvaluateAsInt(ResultTmp, Ctx)) {
28496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              if (Culprit)
28506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                *Culprit = Elt;
285121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman              return false;
28526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            }
285321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          } else {
285421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman            bool RefType = Field->getType()->isReferenceType();
28556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            if (!Elt->isConstantInitializer(Ctx, RefType, Culprit))
285621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman              return false;
285721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          }
285821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        }
285921cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      }
286021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      return true;
286121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    }
286221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman
286321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    break;
2864e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
28653498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  case ImplicitValueInitExprClass:
2866b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  case NoInitExprClass:
28673498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return true;
28683ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  case ParenExprClass:
28694204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return cast<ParenExpr>(this)->getSubExpr()
28706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ->isConstantInitializer(Ctx, IsForRef, Culprit);
2871f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
2872f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()
28736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ->isConstantInitializer(Ctx, IsForRef, Culprit);
28745cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara  case ChooseExprClass:
28756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (cast<ChooseExpr>(this)->isConditionDependent()) {
28766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (Culprit)
28776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        *Culprit = this;
2878a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      return false;
28796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
2880a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    return cast<ChooseExpr>(this)->getChosenSubExpr()
28816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ->isConstantInitializer(Ctx, IsForRef, Culprit);
2882c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case UnaryOperatorClass: {
2883c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    const UnaryOperator* Exp = cast<UnaryOperator>(this);
28842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Exp->getOpcode() == UO_Extension)
28856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return Exp->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
2886c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
2887c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  }
28884204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  case CXXFunctionalCastExprClass:
2889b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXStaticCastExprClass:
289081045d8dcd967def69d8e0945566214a9fe9ffccChris Lattner  case ImplicitCastExprClass:
289121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CStyleCastExprClass:
289221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case ObjCBridgedCastExprClass:
289321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CXXDynamicCastExprClass:
289421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CXXReinterpretCastExprClass:
289521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CXXConstCastExprClass: {
2896d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith    const CastExpr *CE = cast<CastExpr>(this);
2897d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith
28986bd9719fd97abac1b8126eae866e96db88be4ac8Eli Friedman    // Handle misc casts we want to ignore.
28996bd9719fd97abac1b8126eae866e96db88be4ac8Eli Friedman    if (CE->getCastKind() == CK_NoOp ||
29006bd9719fd97abac1b8126eae866e96db88be4ac8Eli Friedman        CE->getCastKind() == CK_LValueToRValue ||
29016bd9719fd97abac1b8126eae866e96db88be4ac8Eli Friedman        CE->getCastKind() == CK_ToUnion ||
290221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        CE->getCastKind() == CK_ConstructorConversion ||
290321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        CE->getCastKind() == CK_NonAtomicToAtomic ||
290421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        CE->getCastKind() == CK_AtomicToNonAtomic)
29056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return CE->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
2906d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith
2907c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
2908d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith  }
290903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass:
29105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
29116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ->isConstantInitializer(Ctx, false, Culprit);
291221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman
291321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case SubstNonTypeTemplateParmExprClass:
291421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
29156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ->isConstantInitializer(Ctx, false, Culprit);
291621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CXXDefaultArgExprClass:
291721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    return cast<CXXDefaultArgExpr>(this)->getExpr()
29186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ->isConstantInitializer(Ctx, false, Culprit);
291921cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CXXDefaultInitExprClass:
292021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    return cast<CXXDefaultInitExpr>(this)->getExpr()
29216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ->isConstantInitializer(Ctx, false, Culprit);
2922e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
2923a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  // Allow certain forms of UB in constant initializers: signed integer
2924a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  // overflow and floating-point division by zero. We'll give a warning on
2925a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  // these, but they're common enough that we have to accept them.
2926a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  if (isEvaluatable(Ctx, SE_AllowUndefinedBehavior))
29276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return true;
29286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Culprit)
29296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    *Culprit = this;
29306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return false;
293138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff}
293238374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
2933b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainarnamespace {
2934b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  /// \brief Look for any side effects within a Stmt.
2935b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  class SideEffectFinder : public ConstEvaluatedExprVisitor<SideEffectFinder> {
2936b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    typedef ConstEvaluatedExprVisitor<SideEffectFinder> Inherited;
2937b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    const bool IncludePossibleEffects;
2938b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    bool HasSideEffects;
2939b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
2940b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  public:
2941b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    explicit SideEffectFinder(const ASTContext &Context, bool IncludePossible)
2942b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      : Inherited(Context),
2943b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar        IncludePossibleEffects(IncludePossible), HasSideEffects(false) { }
2944b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
2945b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    bool hasSideEffects() const { return HasSideEffects; }
2946b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
2947b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    void VisitExpr(const Expr *E) {
2948b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      if (!HasSideEffects &&
2949b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          E->HasSideEffects(Context, IncludePossibleEffects))
2950b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar        HasSideEffects = true;
2951b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    }
2952b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  };
2953b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar}
2954b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
29550e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesbool Expr::HasSideEffects(const ASTContext &Ctx,
29560e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                          bool IncludePossibleEffects) const {
29570e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // In circumstances where we care about definite side effects instead of
29580e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // potential side effects, we want to ignore expressions that are part of a
29590e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // macro expansion as a potential side effect.
29600e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (!IncludePossibleEffects && getExprLoc().isMacroID())
29610e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return false;
29620e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
29638ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  if (isInstantiationDependent())
29640e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return IncludePossibleEffects;
29658ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
29668ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  switch (getStmtClass()) {
29678ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case NoStmtClass:
29688ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  #define ABSTRACT_STMT(Type)
29698ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  #define STMT(Type, Base) case Type##Class:
29708ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  #define EXPR(Type, Base)
29718ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  #include "clang/AST/StmtNodes.inc"
29728ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    llvm_unreachable("unexpected Expr kind");
29738ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
29748ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case DependentScopeDeclRefExprClass:
29758ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXUnresolvedConstructExprClass:
29768ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXDependentScopeMemberExprClass:
29778ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case UnresolvedLookupExprClass:
29788ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case UnresolvedMemberExprClass:
29798ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case PackExpansionExprClass:
29808ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case SubstNonTypeTemplateParmPackExprClass:
29819a4db032ecd991626d236a502e770126db32bd31Richard Smith  case FunctionParmPackExprClass:
2982176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case TypoExprClass:
2983176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case CXXFoldExprClass:
29848ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    llvm_unreachable("shouldn't see dependent / unresolved nodes here");
29858ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
298660b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case DeclRefExprClass:
298760b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case ObjCIvarRefExprClass:
29888ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case PredefinedExprClass:
29898ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case IntegerLiteralClass:
29908ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case FloatingLiteralClass:
29918ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ImaginaryLiteralClass:
29928ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case StringLiteralClass:
29938ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CharacterLiteralClass:
29948ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case OffsetOfExprClass:
29958ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ImplicitValueInitExprClass:
29968ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case UnaryExprOrTypeTraitExprClass:
29978ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case AddrLabelExprClass:
29988ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case GNUNullExprClass:
2999b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  case NoInitExprClass:
30008ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXBoolLiteralExprClass:
30018ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXNullPtrLiteralExprClass:
30028ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXThisExprClass:
30038ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXScalarValueInitExprClass:
30048ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case TypeTraitExprClass:
30058ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ArrayTypeTraitExprClass:
30068ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ExpressionTraitExprClass:
30078ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXNoexceptExprClass:
30088ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case SizeOfPackExprClass:
30098ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCStringLiteralClass:
30108ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCEncodeExprClass:
30118ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCBoolLiteralExprClass:
30128ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXUuidofExprClass:
30138ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case OpaqueValueExprClass:
30148ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // These never have a side-effect.
30158ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return false;
30168ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
30178ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CallExprClass:
30180e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case CXXOperatorCallExprClass:
30190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case CXXMemberCallExprClass:
30200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case CUDAKernelCallExprClass:
302158878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar  case UserDefinedLiteralClass: {
302258878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar    // We don't know a call definitely has side effects, except for calls
302358878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar    // to pure/const functions that definitely don't.
302458878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar    // If the call itself is considered side-effect free, check the operands.
302558878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar    const Decl *FD = cast<CallExpr>(this)->getCalleeDecl();
302658878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar    bool IsPure = FD && (FD->hasAttr<ConstAttr>() || FD->hasAttr<PureAttr>());
302758878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar    if (IsPure || !IncludePossibleEffects)
302858878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar      break;
302958878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar    return true;
303058878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar  }
303158878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar
30320e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case BlockExprClass:
30330e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case CXXBindTemporaryExprClass:
30340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (!IncludePossibleEffects)
30350e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      break;
30360e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return true;
30370e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
303876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  case MSPropertyRefExprClass:
3039a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  case MSPropertySubscriptExprClass:
30408ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CompoundAssignOperatorClass:
30418ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case VAArgExprClass:
30428ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case AtomicExprClass:
30438ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXThrowExprClass:
30448ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXNewExprClass:
30458ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXDeleteExprClass:
30468ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ExprWithCleanupsClass:
3047a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  case CoawaitExprClass:
3048a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  case CoyieldExprClass:
30498ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // These always have a side-effect.
30508ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return true;
30518ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
3052b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  case StmtExprClass: {
3053b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    // StmtExprs have a side-effect if any substatement does.
3054b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    SideEffectFinder Finder(Ctx, IncludePossibleEffects);
3055b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    Finder.Visit(cast<StmtExpr>(this)->getSubStmt());
3056b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    return Finder.hasSideEffects();
3057b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  }
3058b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
30598ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ParenExprClass:
30608ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ArraySubscriptExprClass:
3061a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  case OMPArraySectionExprClass:
30628ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case MemberExprClass:
30638ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ConditionalOperatorClass:
30648ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case BinaryConditionalOperatorClass:
30658ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CompoundLiteralExprClass:
30668ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ExtVectorElementExprClass:
30678ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case DesignatedInitExprClass:
3068b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  case DesignatedInitUpdateExprClass:
30698ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ParenListExprClass:
30708ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXPseudoDestructorExprClass:
30717c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith  case CXXStdInitializerListExprClass:
30728ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case SubstNonTypeTemplateParmExprClass:
30738ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case MaterializeTemporaryExprClass:
30748ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ShuffleVectorExprClass:
3075414a1bdbdaf250e0488589f12865c8961831b65dHal Finkel  case ConvertVectorExprClass:
30768ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case AsTypeExprClass:
30778ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // These have a side-effect if any subexpression does.
30788ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
30798ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
308060b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case UnaryOperatorClass:
308160b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
30828ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      return true;
30838ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
30848ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
30858ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case BinaryOperatorClass:
30868ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    if (cast<BinaryOperator>(this)->isAssignmentOp())
30878ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      return true;
30888ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
30898ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
30908ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case InitListExprClass:
30918ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // FIXME: The children for an InitListExpr doesn't include the array filler.
30928ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
30930e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (E->HasSideEffects(Ctx, IncludePossibleEffects))
30948ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        return true;
30958ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
30968ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
30978ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case GenericSelectionExprClass:
30988ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return cast<GenericSelectionExpr>(this)->getResultExpr()->
30990e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        HasSideEffects(Ctx, IncludePossibleEffects);
31008ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
31018ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ChooseExprClass:
31020e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(
31030e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        Ctx, IncludePossibleEffects);
31048ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
31058ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXDefaultArgExprClass:
31060e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(
31070e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        Ctx, IncludePossibleEffects);
31088ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
3109176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case CXXDefaultInitExprClass: {
3110176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    const FieldDecl *FD = cast<CXXDefaultInitExpr>(this)->getField();
3111176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (const Expr *E = FD->getInClassInitializer())
31120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      return E->HasSideEffects(Ctx, IncludePossibleEffects);
3113c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    // If we've not yet parsed the initializer, assume it has side-effects.
3114c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    return true;
3115176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
3116c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith
31178ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXDynamicCastExprClass: {
31188ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // A dynamic_cast expression has side-effects if it can throw.
31198ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
31208ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    if (DCE->getTypeAsWritten()->isReferenceType() &&
31218ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        DCE->getCastKind() == CK_Dynamic)
31228ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      return true;
312360b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  } // Fall through.
312460b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case ImplicitCastExprClass:
312560b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case CStyleCastExprClass:
312660b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case CXXStaticCastExprClass:
312760b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case CXXReinterpretCastExprClass:
312860b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case CXXConstCastExprClass:
312960b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case CXXFunctionalCastExprClass: {
31300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // While volatile reads are side-effecting in both C and C++, we treat them
31310e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // as having possible (not definite) side-effects. This allows idiomatic
31320e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // code to behave without warning, such as sizeof(*v) for a volatile-
31330e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // qualified pointer.
31340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (!IncludePossibleEffects)
31350e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      break;
31360e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
313760b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    const CastExpr *CE = cast<CastExpr>(this);
313860b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    if (CE->getCastKind() == CK_LValueToRValue &&
313960b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith        CE->getSubExpr()->getType().isVolatileQualified())
314060b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith      return true;
31418ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
31428ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  }
31438ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
31440d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith  case CXXTypeidExprClass:
31450d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith    // typeid might throw if its subexpression is potentially-evaluated, so has
31460d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith    // side-effects in that case whether or not its subexpression does.
31470d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith    return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
31488ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
31498ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXConstructExprClass:
31508ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXTemporaryObjectExprClass: {
31518ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
31520e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (!CE->getConstructor()->isTrivial() && IncludePossibleEffects)
31538ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      return true;
315460b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    // A trivial constructor does not add any side-effects of its own. Just look
315560b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    // at its arguments.
31568ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
31578ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  }
31588ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
31598ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case LambdaExprClass: {
31608ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    const LambdaExpr *LE = cast<LambdaExpr>(this);
31618ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    for (LambdaExpr::capture_iterator I = LE->capture_begin(),
31628ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith                                      E = LE->capture_end(); I != E; ++I)
31638ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      if (I->getCaptureKind() == LCK_ByCopy)
31648ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        // FIXME: Only has a side-effect if the variable is volatile or if
31658ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        // the copy would invoke a non-trivial copy constructor.
31668ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        return true;
31678ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return false;
31688ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  }
31698ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
31708ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case PseudoObjectExprClass: {
31718ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // Only look for side-effects in the semantic form, and look past
31728ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // OpaqueValueExpr bindings in that form.
31738ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
31748ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
31758ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith                                                    E = PO->semantics_end();
31768ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith         I != E; ++I) {
31778ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      const Expr *Subexpr = *I;
31788ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
31798ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        Subexpr = OVE->getSourceExpr();
31800e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (Subexpr->HasSideEffects(Ctx, IncludePossibleEffects))
31818ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        return true;
31828ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    }
31838ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return false;
31848ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  }
31858ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
31868ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCBoxedExprClass:
31878ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCArrayLiteralClass:
31888ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCDictionaryLiteralClass:
31898ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCSelectorExprClass:
31908ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCProtocolExprClass:
31918ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCIsaExprClass:
31928ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCIndirectCopyRestoreExprClass:
31938ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCSubscriptRefExprClass:
31948ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCBridgedCastExprClass:
31950e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case ObjCMessageExprClass:
31960e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case ObjCPropertyRefExprClass:
31970e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // FIXME: Classify these cases better.
31980e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (IncludePossibleEffects)
31990e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      return true;
32000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    break;
32018ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  }
32028ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
32038ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  // Recurse to children.
3204a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  for (const Stmt *SubStmt : children())
3205a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    if (SubStmt &&
3206a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        cast<Expr>(SubStmt)->HasSideEffects(Ctx, IncludePossibleEffects))
3207a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      return true;
32088ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
32098ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  return false;
32108ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith}
32118ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
321225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregornamespace {
321325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  /// \brief Look for a call to a non-trivial function within an expression.
3214b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  class NonTrivialCallFinder : public ConstEvaluatedExprVisitor<NonTrivialCallFinder>
321525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  {
3216b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    typedef ConstEvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
3217b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
321825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    bool NonTrivial;
321925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
322025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  public:
3221b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    explicit NonTrivialCallFinder(const ASTContext &Context)
3222b11e52540166a2958439cfe562c25931e4460759Douglas Gregor      : Inherited(Context), NonTrivial(false) { }
322325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
322425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    bool hasNonTrivialCall() const { return NonTrivial; }
3225b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
3226b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    void VisitCallExpr(const CallExpr *E) {
3227b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      if (const CXXMethodDecl *Method
3228b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          = dyn_cast_or_null<const CXXMethodDecl>(E->getCalleeDecl())) {
322925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        if (Method->isTrivial()) {
323025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor          // Recurse to children of the call.
323125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor          Inherited::VisitStmt(E);
323225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor          return;
323325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        }
323425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      }
323525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
323625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      NonTrivial = true;
323725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    }
3238b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
3239b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    void VisitCXXConstructExpr(const CXXConstructExpr *E) {
324025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      if (E->getConstructor()->isTrivial()) {
324125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        // Recurse to children of the call.
324225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        Inherited::VisitStmt(E);
324325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        return;
324425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      }
324525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
324625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      NonTrivial = true;
324725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    }
3248b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
3249b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) {
325025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      if (E->getTemporary()->getDestructor()->isTrivial()) {
325125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        Inherited::VisitStmt(E);
325225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        return;
325325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      }
325425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
325525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      NonTrivial = true;
325625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    }
325725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  };
325825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor}
325925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
3260b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainarbool Expr::hasNonTrivialCall(const ASTContext &Ctx) const {
326125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  NonTrivialCallFinder Finder(Ctx);
326225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  Finder.Visit(this);
326325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  return Finder.hasNonTrivialCall();
326425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor}
326525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
326682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
326782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// pointer constant or not, as well as the specific kind of constant detected.
326882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// Null pointer constants can be integer constant expressions with the
326982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// value zero, casts of zero to void*, nullptr (C++0X), or __null
327082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// (a GNU extension).
327182214a80c0163e01e4d8dec1426023c89277dbb4Chandler CarruthExpr::NullPointerConstantKind
327282214a80c0163e01e4d8dec1426023c89277dbb4Chandler CarruthExpr::isNullPointerConstant(ASTContext &Ctx,
327382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth                            NullPointerConstantValueDependence NPC) const {
3274e0ac9bfbf588e269fefc8758e715ee5804affb18Reid Kleckner  if (isValueDependent() &&
3275651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
3276ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    switch (NPC) {
3277ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_NeverValueDependent:
3278b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unexpected value dependent expression!");
3279ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNull:
328082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      if (isTypeDependent() || getType()->isIntegralType(Ctx))
328150800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie        return NPCK_ZeroExpression;
328282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      else
328382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth        return NPCK_NotNull;
3284c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
3285ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNotNull:
328682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      return NPCK_NotNull;
3287ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    }
3288ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  }
3289f515b2268f829adfbfdb751f54d102b53ed0285cDaniel Dunbar
32900777972d38a3125efed962b045704c30ae6965cfSebastian Redl  // Strip off a cast to void*, if it exists. Except in C++.
32910835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis  if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
32924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!Ctx.getLangOpts().CPlusPlus) {
32930777972d38a3125efed962b045704c30ae6965cfSebastian Redl      // Check that it is a cast to void*.
32946217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
32950777972d38a3125efed962b045704c30ae6965cfSebastian Redl        QualType Pointee = PT->getPointeeType();
3296a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        Qualifiers Q = Pointee.getQualifiers();
3297a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        // In OpenCL v2.0 generic address space acts as a placeholder
3298a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        // and should be ignored.
3299a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        bool IsASValid = true;
3300a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        if (Ctx.getLangOpts().OpenCLVersion >= 200) {
3301a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar          if (Pointee.getAddressSpace() == LangAS::opencl_generic)
3302a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar            Q.removeAddressSpace();
3303a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar          else
3304a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar            IsASValid = false;
3305a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        }
3306a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
3307a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        if (IsASValid && !Q.hasQualifiers() &&
3308a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar            Pointee->isVoidType() &&                      // to void*
3309a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar            CE->getSubExpr()->getType()->isIntegerType()) // from int.
3310ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor          return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
33110777972d38a3125efed962b045704c30ae6965cfSebastian Redl      }
33125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3313aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3314aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Ignore the ImplicitCastExpr type entirely.
3315ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
3316aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3317aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Accept ((void*)0) as a null pointer constant, as many other
3318aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // implementations do.
3319ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
3320f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  } else if (const GenericSelectionExpr *GE =
3321f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne               dyn_cast<GenericSelectionExpr>(this)) {
3322a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    if (GE->isResultDependent())
3323a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      return NPCK_NotNull;
3324f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
3325a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman  } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3326a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    if (CE->isConditionDependent())
3327a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      return NPCK_NotNull;
3328a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
33291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else if (const CXXDefaultArgExpr *DefaultArg
33308123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner               = dyn_cast<CXXDefaultArgExpr>(this)) {
3331c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    // See through default argument expressions.
3332ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
3333c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  } else if (const CXXDefaultInitExpr *DefaultInit
3334c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith               = dyn_cast<CXXDefaultInitExpr>(this)) {
3335c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    // See through default initializer expressions.
3336c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
33372d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor  } else if (isa<GNUNullExpr>(this)) {
33382d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    // The GNU __null extension is always a null pointer constant.
333982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_GNUNull;
334003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  } else if (const MaterializeTemporaryExpr *M
334103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                   = dyn_cast<MaterializeTemporaryExpr>(this)) {
334203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
33434b9c2d235fb9449e249d74f48ecfec601650de93John McCall  } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
33444b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (const Expr *Source = OVE->getSourceExpr())
33454b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return Source->isNullPointerConstant(Ctx, NPC);
3346aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff  }
33472d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
33484e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  // C++11 nullptr_t is always a null pointer constant.
33496e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  if (getType()->isNullPtrType())
33504e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    return NPCK_CXX11_nullptr;
33516e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
3352ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian  if (const RecordType *UT = getType()->getAsUnionType())
3353f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    if (!Ctx.getLangOpts().CPlusPlus11 &&
3354f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith        UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
3355ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3356ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian        const Expr *InitExpr = CLE->getInitializer();
3357ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian        if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3358ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian          return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3359ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      }
3360aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  // This expression must be an integer type.
3361c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  if (!getType()->isIntegerType() ||
33624e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
336382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_NotNull;
33641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
336580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (Ctx.getLangOpts().CPlusPlus11) {
3366f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3367f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    // value zero or a prvalue of type std::nullptr_t.
3368e0ac9bfbf588e269fefc8758e715ee5804affb18Reid Kleckner    // Microsoft mode permits C++98 rules reflecting MSVC behavior.
3369f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
3370e0ac9bfbf588e269fefc8758e715ee5804affb18Reid Kleckner    if (Lit && !Lit->getValue())
3371e0ac9bfbf588e269fefc8758e715ee5804affb18Reid Kleckner      return NPCK_ZeroLiteral;
3372651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
3373e0ac9bfbf588e269fefc8758e715ee5804affb18Reid Kleckner      return NPCK_NotNull;
337470488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  } else {
3375f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    // If we have an integer constant expression, we need to *evaluate* it and
3376f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    // test for the value 0.
337770488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith    if (!isIntegerConstantExpr(Ctx))
337870488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith      return NPCK_NotNull;
337970488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  }
338082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
338150800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie  if (EvaluateKnownConstInt(Ctx) != 0)
338250800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    return NPCK_NotNull;
338350800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie
338450800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie  if (isa<IntegerLiteral>(this))
338550800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    return NPCK_ZeroLiteral;
338650800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie  return NPCK_ZeroExpression;
33875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
338831a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff
3389f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// \brief If this expression is an l-value for an Objective C
3390f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// property, find the underlying property reference expression.
3391f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallconst ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3392f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  const Expr *E = this;
3393f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  while (true) {
3394f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    assert((E->getValueKind() == VK_LValue &&
3395f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall            E->getObjectKind() == OK_ObjCProperty) &&
3396f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall           "expression is not a property reference");
3397f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    E = E->IgnoreParenCasts();
3398f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3399f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      if (BO->getOpcode() == BO_Comma) {
3400f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        E = BO->getRHS();
3401f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        continue;
3402f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
3403f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
3404f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
3405f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
3406f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
3407f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
3408f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return cast<ObjCPropertyRefExpr>(E);
3409f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
3410f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
3411bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaksbool Expr::isObjCSelfExpr() const {
3412bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  const Expr *E = IgnoreParenImpCasts();
3413bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
3414bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3415bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  if (!DRE)
3416bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks    return false;
3417bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
3418bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3419bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  if (!Param)
3420bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks    return false;
3421bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
3422bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3423bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  if (!M)
3424bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks    return false;
3425bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
3426bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  return M->getSelfDecl() == Param;
3427bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks}
3428bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
3429993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCallFieldDecl *Expr::getSourceBitField() {
34306f4a69a3107e7ff1569c747f7c6bdf7cff8cbf55Douglas Gregor  Expr *E = this->IgnoreParens();
343133bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
3432de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
3433f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (ICE->getCastKind() == CK_LValueToRValue ||
3434f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
3435de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      E = ICE->getSubExpr()->IgnoreParens();
3436de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    else
3437de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
3438de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  }
3439de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
344027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
344186f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
344233bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor      if (Field->isBitField())
344333bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor        return Field;
344433bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
3445993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3446993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall    if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3447993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall      if (Ivar->isBitField())
3448993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall        return Ivar;
3449993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall
34500f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis  if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
34510f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis    if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
34520f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis      if (Field->isBitField())
34530f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis        return Field;
34540f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis
345542068e9d420889014ee26894e421fead336b239dEli Friedman  if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
345633bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor    if (BinOp->isAssignmentOp() && BinOp->getLHS())
3457993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall      return BinOp->getLHS()->getSourceBitField();
345833bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
345942068e9d420889014ee26894e421fead336b239dEli Friedman    if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
3460993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall      return BinOp->getRHS()->getSourceBitField();
346142068e9d420889014ee26894e421fead336b239dEli Friedman  }
346242068e9d420889014ee26894e421fead336b239dEli Friedman
3463176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E))
3464176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (UnOp->isPrefix() && UnOp->isIncrementDecrementOp())
3465176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      return UnOp->getSubExpr()->getSourceBitField();
3466176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
34676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
346827c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
346927c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
3470093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlssonbool Expr::refersToVectorElement() const {
3471093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  const Expr *E = this->IgnoreParens();
3472c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
3473093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
34745baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    if (ICE->getValueKind() != VK_RValue &&
34752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        ICE->getCastKind() == CK_NoOp)
3476093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      E = ICE->getSubExpr()->IgnoreParens();
3477093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    else
3478093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      break;
3479093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  }
3480c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
3481093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3482093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return ASE->getBase()->getType()->isVectorType();
3483093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
3484093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (isa<ExtVectorElementExpr>(E))
3485093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return true;
3486093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
3487093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  return false;
3488093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson}
3489093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
3490a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarbool Expr::refersToGlobalRegisterVar() const {
3491a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  const Expr *E = this->IgnoreParenImpCasts();
3492a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
3493a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
3494a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
3495a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      if (VD->getStorageClass() == SC_Register &&
3496a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar          VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())
3497a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        return true;
3498a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
3499a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  return false;
3500a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar}
3501a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
35022140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// isArrow - Return true if the base expression is a pointer to vector,
35032140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// return false if the base expression is a vector.
35042140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattnerbool ExtVectorElementExpr::isArrow() const {
35052140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner  return getBase()->getType()->isPointerType();
35062140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner}
35072140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner
3508213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const {
3509183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const VectorType *VT = getType()->getAs<VectorType>())
35108a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    return VT->getNumElements();
35118a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  return 1;
35124d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner}
35134d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner
35148a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated.
3515213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const {
3516a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // FIXME: Refactor this code to an accessor on the AST node which returns the
3517a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // "type" of component access, and share with code below and in Sema.
35185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Comp = Accessor->getName();
3519190d6a25393995b42e32086949a68285ee423fb9Nate Begeman
3520190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Halving swizzles do not contain duplicate elements.
3521150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
3522190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    return false;
35231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3524190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Advance past s-char prefix on hex swizzles.
3525150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
3526150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar    Comp = Comp.substr(1);
35271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3528150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  for (unsigned i = 0, e = Comp.size(); i != e; ++i)
35295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
3530fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff        return true;
3531150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar
3532fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  return false;
3533fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff}
3534b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
35358a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
35363b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess(
3537a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    SmallVectorImpl<uint32_t> &Elts) const {
35385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Comp = Accessor->getName();
35394b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
35404b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar    Comp = Comp.substr(1);
35411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35424b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isHi =   Comp == "hi";
35434b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isLo =   Comp == "lo";
35444b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isEven = Comp == "even";
35454b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isOdd  = Comp == "odd";
35461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35478a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
35488a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    uint64_t Index;
35491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35508a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    if (isHi)
35518a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = e + i;
35528a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isLo)
35538a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = i;
35548a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isEven)
35558a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i;
35568a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isOdd)
35578a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i + 1;
35588a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else
35594b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar      Index = ExtVectorType::getAccessorIdx(Comp[i]);
3560b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
35613b8d116703db8018f855cbb4733ace426422623bNate Begeman    Elts.push_back(Index);
3562b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner  }
35638a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman}
35648a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman
356505ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
3566bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor                                     QualType Type, SourceLocation BLoc,
3567bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor                                     SourceLocation RP)
3568bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor   : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3569bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          Type->isDependentType(), Type->isDependentType(),
3570561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          Type->isInstantiationDependentType(),
3571bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          Type->containsUnexpandedParameterPack()),
35723b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer     BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
3573bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor{
35743b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  SubExprs = new (C) Stmt*[args.size()];
35753b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != args.size(); i++) {
3576bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
3577bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
3578bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
3579bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
3580561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
3581561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
3582bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
3583bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
3584bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
3585bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    SubExprs[i] = args[i];
3586bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
3587bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor}
3588bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
358905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
3590888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman  if (SubExprs) C.Deallocate(SubExprs);
3591888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
359227365ee830cec7cf52c088b400867ff67b0b3b23Dmitri Gribenko  this->NumExprs = Exprs.size();
35932ad77cdd028b3c96f21d7bc6daee8fde6cf0cd86Dmitri Gribenko  SubExprs = new (C) Stmt*[NumExprs];
359427365ee830cec7cf52c088b400867ff67b0b3b23Dmitri Gribenko  memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
35951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
3596888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
359705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperGenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
3598f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation GenericLoc, Expr *ControllingExpr,
35993b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               ArrayRef<TypeSourceInfo*> AssocTypes,
36003b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               ArrayRef<Expr*> AssocExprs,
36013b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               SourceLocation DefaultLoc,
3602f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation RParenLoc,
3603f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               bool ContainsUnexpandedParameterPack,
3604f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned ResultIndex)
3605f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  : Expr(GenericSelectionExprClass,
3606f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getType(),
3607f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getValueKind(),
3608f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getObjectKind(),
3609f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->isTypeDependent(),
3610f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->isValueDependent(),
3611561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         AssocExprs[ResultIndex]->isInstantiationDependent(),
3612f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         ContainsUnexpandedParameterPack),
36133b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
36143b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
36153b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
36163b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
3617f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  SubExprs[CONTROLLING] = ControllingExpr;
36183b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  assert(AssocTypes.size() == AssocExprs.size());
36193b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
36203b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
3621f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne}
3622f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
362305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperGenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
3624f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation GenericLoc, Expr *ControllingExpr,
36253b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               ArrayRef<TypeSourceInfo*> AssocTypes,
36263b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               ArrayRef<Expr*> AssocExprs,
36273b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               SourceLocation DefaultLoc,
3628f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation RParenLoc,
3629f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               bool ContainsUnexpandedParameterPack)
3630f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  : Expr(GenericSelectionExprClass,
3631f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         Context.DependentTy,
3632f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         VK_RValue,
3633f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         OK_Ordinary,
3634561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isTypeDependent=*/true,
3635561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isValueDependent=*/true,
3636561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isInstantiationDependent=*/true,
3637f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         ContainsUnexpandedParameterPack),
36383b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
36393b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
36403b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
36413b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
3642f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  SubExprs[CONTROLLING] = ControllingExpr;
36433b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  assert(AssocTypes.size() == AssocExprs.size());
36443b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
36453b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
3646f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne}
3647f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
364877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
364905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//  DesignatedInitExpr
365005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
365105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3652b11382497a923b0d7009e85a1d8eb7bf93ec6d0dChandler CarruthIdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
365305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(Kind == FieldDesignator && "Only valid on a field designator");
365405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Field.NameOrField & 0x01)
365505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
365605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  else
365705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return getField()->getIdentifier();
365805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
365905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
366005ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperDesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
3661319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                       unsigned NumDesignators,
3662ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       const Designator *Designators,
36631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       SourceLocation EqualOrColonLoc,
3664ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       bool GNUSyntax,
36653b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                       ArrayRef<Expr*> IndexExprs,
36669ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                       Expr *Init)
36671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(DesignatedInitExprClass, Ty,
3668f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall         Init->getValueKind(), Init->getObjectKind(),
3669bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Init->isTypeDependent(), Init->isValueDependent(),
3670561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Init->isInstantiationDependent(),
3671bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Init->containsUnexpandedParameterPack()),
36721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
36733b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
3674319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  this->Designators = new (C) Designator[NumDesignators];
36759ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
36769ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Record the initializer itself.
3677a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  child_iterator Child = child_begin();
36789ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  *Child++ = Init;
36799ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
36809ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Copy the designators and their subexpressions, computing
36819ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // value-dependence along the way.
36829ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  unsigned IndexIdx = 0;
36839ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  for (unsigned I = 0; I != NumDesignators; ++I) {
3684ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    this->Designators[I] = Designators[I];
36859ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
36869ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    if (this->Designators[I].isArrayDesignator()) {
36879ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
36889ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Index = IndexExprs[IndexIdx];
3689bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Index->isTypeDependent() || Index->isValueDependent())
36900e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        ExprBits.TypeDependent = ExprBits.ValueDependent = true;
3691561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      if (Index->isInstantiationDependent())
3692561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3693bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      // Propagate unexpanded parameter packs.
3694bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Index->containsUnexpandedParameterPack())
3695bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ContainsUnexpandedParameterPack = true;
36969ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
36979ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the index expressions into permanent storage.
36989ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
36999ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    } else if (this->Designators[I].isArrayRangeDesignator()) {
37009ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
37019ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Start = IndexExprs[IndexIdx];
37029ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *End = IndexExprs[IndexIdx + 1];
3703bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Start->isTypeDependent() || Start->isValueDependent() ||
3704561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          End->isTypeDependent() || End->isValueDependent()) {
37050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        ExprBits.TypeDependent = ExprBits.ValueDependent = true;
3706561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3707561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      } else if (Start->isInstantiationDependent() ||
3708561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                 End->isInstantiationDependent()) {
3709561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3710561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      }
3711561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
3712bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      // Propagate unexpanded parameter packs.
3713bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Start->containsUnexpandedParameterPack() ||
3714bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          End->containsUnexpandedParameterPack())
3715bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ContainsUnexpandedParameterPack = true;
37169ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
37179ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the start/end expressions into permanent storage.
37189ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
37199ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
37209ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    }
37219ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  }
37229ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
37233b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
3724ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
3725ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
372605c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr *
372705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperDesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
372805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           unsigned NumDesignators,
37293b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                           ArrayRef<Expr*> IndexExprs,
373005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           SourceLocation ColonOrEqualLoc,
373105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           bool UsesColonSyntax, Expr *Init) {
3732c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3733a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                             sizeof(Stmt *) * (IndexExprs.size() + 1),
3734a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar                         llvm::alignOf<DesignatedInitExpr>());
3735319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
37369ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                      ColonOrEqualLoc, UsesColonSyntax,
37373b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                      IndexExprs, Init);
373805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
373905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
374005ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperDesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
3741d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                                    unsigned NumIndexExprs) {
3742d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3743d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3744d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3745d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
3746d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
374705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid DesignatedInitExpr::setDesignators(const ASTContext &C,
3748319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                        const Designator *Desigs,
3749d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                        unsigned NumDesigs) {
3750319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  Designators = new (C) Designator[NumDesigs];
3751d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  NumDesignators = NumDesigs;
3752d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  for (unsigned I = 0; I != NumDesigs; ++I)
3753d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor    Designators[I] = Desigs[I];
3754d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
3755d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
375624f4674e697fb53587f0e8485e9c6592f7021ef2Abramo BagnaraSourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
375724f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
375824f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  if (size() == 1)
375924f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara    return DIE->getDesignator(0)->getSourceRange();
376065d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  return SourceRange(DIE->getDesignator(0)->getLocStart(),
376165d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen                     DIE->getDesignator(size()-1)->getLocEnd());
376224f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara}
376324f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara
376465d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik VerbruggenSourceLocation DesignatedInitExpr::getLocStart() const {
376505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation StartLoc;
3766d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner  Designator &First =
3767d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    *const_cast<DesignatedInitExpr*>(this)->designators_begin();
376805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (First.isFieldDesignator()) {
3769eeae8f072748affce25ab4064982626361293390Douglas Gregor    if (GNUSyntax)
377005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
377105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else
377205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
377305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  } else
3774d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    StartLoc =
3775d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner      SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
377665d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  return StartLoc;
377765d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen}
377865d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen
377965d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik VerbruggenSourceLocation DesignatedInitExpr::getLocEnd() const {
378065d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  return getInit()->getLocEnd();
378105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
378205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3783d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri GribenkoExpr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
378405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3785651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
378605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
378705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
378805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3789d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri GribenkoExpr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
37901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
379105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
3792651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
379305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
379405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
379505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3796d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri GribenkoExpr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
37971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
379805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
3799651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
380005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
380105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
380205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3803ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// \brief Replaces the designator at index @p Idx with the series
3804ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// of designators in [First, Last).
380505ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
38061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          const Designator *First,
3807ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                          const Designator *Last) {
3808ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  unsigned NumNewDesignators = Last - First;
3809ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  if (NumNewDesignators == 0) {
3810ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    std::copy_backward(Designators + Idx + 1,
3811ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + NumDesignators,
3812ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + Idx);
3813ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    --NumNewDesignators;
3814ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
3815ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  } else if (NumNewDesignators == 1) {
3816ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    Designators[Idx] = *First;
3817ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
3818ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
3819ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
38201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Designator *NewDesignators
3821319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor    = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
3822ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators, Designators + Idx, NewDesignators);
3823ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(First, Last, NewDesignators + Idx);
3824ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators + Idx + 1, Designators + NumDesignators,
3825ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor            NewDesignators + Idx + NumNewDesignators);
3826ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  Designators = NewDesignators;
3827ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  NumDesignators = NumDesignators - 1 + NumNewDesignators;
3828ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
3829ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
3830b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga NainarDesignatedInitUpdateExpr::DesignatedInitUpdateExpr(const ASTContext &C,
3831b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    SourceLocation lBraceLoc, Expr *baseExpr, SourceLocation rBraceLoc)
3832b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  : Expr(DesignatedInitUpdateExprClass, baseExpr->getType(), VK_RValue,
3833b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar         OK_Ordinary, false, false, false, false) {
3834b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  BaseAndUpdaterExprs[0] = baseExpr;
3835b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
3836b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  InitListExpr *ILE = new (C) InitListExpr(C, lBraceLoc, None, rBraceLoc);
3837b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  ILE->setType(baseExpr->getType());
3838b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  BaseAndUpdaterExprs[1] = ILE;
3839b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar}
3840b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
3841b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga NainarSourceLocation DesignatedInitUpdateExpr::getLocStart() const {
3842b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  return getBase()->getLocStart();
3843b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar}
3844b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
3845b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga NainarSourceLocation DesignatedInitUpdateExpr::getLocEnd() const {
3846b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  return getBase()->getLocEnd();
3847b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar}
3848b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
384905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
38503b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                             ArrayRef<Expr*> exprs,
38515b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                             SourceLocation rparenloc)
38525b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
3853561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         false, false, false, false),
38543b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
38553b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  Exprs = new (C) Stmt*[exprs.size()];
38563b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != exprs.size(); ++i) {
3857bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->isTypeDependent())
3858bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
3859bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->isValueDependent())
3860bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
3861561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (exprs[i]->isInstantiationDependent())
3862561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
3863bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->containsUnexpandedParameterPack())
3864bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
3865bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
38662ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    Exprs[i] = exprs[i];
3867bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
38682ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman}
38692ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman
3870e996ffd240f20a1048179d7727a6ee3227261921John McCallconst OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3871e996ffd240f20a1048179d7727a6ee3227261921John McCall  if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3872e996ffd240f20a1048179d7727a6ee3227261921John McCall    e = ewc->getSubExpr();
387303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
387403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    e = m->GetTemporaryExpr();
3875e996ffd240f20a1048179d7727a6ee3227261921John McCall  e = cast<CXXConstructExpr>(e)->getArg(0);
3876e996ffd240f20a1048179d7727a6ee3227261921John McCall  while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3877e996ffd240f20a1048179d7727a6ee3227261921John McCall    e = ice->getSubExpr();
3878e996ffd240f20a1048179d7727a6ee3227261921John McCall  return cast<OpaqueValueExpr>(e);
3879e996ffd240f20a1048179d7727a6ee3227261921John McCall}
3880e996ffd240f20a1048179d7727a6ee3227261921John McCall
388105ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperPseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
388205ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                                           EmptyShell sh,
38834b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                           unsigned numSemanticExprs) {
38844b9c2d235fb9449e249d74f48ecfec601650de93John McCall  void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
38854b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                    (1 + numSemanticExprs) * sizeof(Expr*),
38864b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                  llvm::alignOf<PseudoObjectExpr>());
38874b9c2d235fb9449e249d74f48ecfec601650de93John McCall  return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
38884b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
38894b9c2d235fb9449e249d74f48ecfec601650de93John McCall
38904b9c2d235fb9449e249d74f48ecfec601650de93John McCallPseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
38914b9c2d235fb9449e249d74f48ecfec601650de93John McCall  : Expr(PseudoObjectExprClass, shell) {
38924b9c2d235fb9449e249d74f48ecfec601650de93John McCall  PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
38934b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
38944b9c2d235fb9449e249d74f48ecfec601650de93John McCall
389505ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperPseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
38964b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                           ArrayRef<Expr*> semantics,
38974b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                           unsigned resultIndex) {
38984b9c2d235fb9449e249d74f48ecfec601650de93John McCall  assert(syntax && "no syntactic expression!");
38994b9c2d235fb9449e249d74f48ecfec601650de93John McCall  assert(semantics.size() && "no semantic expressions!");
39004b9c2d235fb9449e249d74f48ecfec601650de93John McCall
39014b9c2d235fb9449e249d74f48ecfec601650de93John McCall  QualType type;
39024b9c2d235fb9449e249d74f48ecfec601650de93John McCall  ExprValueKind VK;
39034b9c2d235fb9449e249d74f48ecfec601650de93John McCall  if (resultIndex == NoResult) {
39044b9c2d235fb9449e249d74f48ecfec601650de93John McCall    type = C.VoidTy;
39054b9c2d235fb9449e249d74f48ecfec601650de93John McCall    VK = VK_RValue;
39064b9c2d235fb9449e249d74f48ecfec601650de93John McCall  } else {
39074b9c2d235fb9449e249d74f48ecfec601650de93John McCall    assert(resultIndex < semantics.size());
39084b9c2d235fb9449e249d74f48ecfec601650de93John McCall    type = semantics[resultIndex]->getType();
39094b9c2d235fb9449e249d74f48ecfec601650de93John McCall    VK = semantics[resultIndex]->getValueKind();
39104b9c2d235fb9449e249d74f48ecfec601650de93John McCall    assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
39114b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
39124b9c2d235fb9449e249d74f48ecfec601650de93John McCall
39134b9c2d235fb9449e249d74f48ecfec601650de93John McCall  void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
39144b9c2d235fb9449e249d74f48ecfec601650de93John McCall                              (1 + semantics.size()) * sizeof(Expr*),
39154b9c2d235fb9449e249d74f48ecfec601650de93John McCall                            llvm::alignOf<PseudoObjectExpr>());
39164b9c2d235fb9449e249d74f48ecfec601650de93John McCall  return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
39174b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                      resultIndex);
39184b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
39194b9c2d235fb9449e249d74f48ecfec601650de93John McCall
39204b9c2d235fb9449e249d74f48ecfec601650de93John McCallPseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
39214b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                   Expr *syntax, ArrayRef<Expr*> semantics,
39224b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                   unsigned resultIndex)
39234b9c2d235fb9449e249d74f48ecfec601650de93John McCall  : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
39244b9c2d235fb9449e249d74f48ecfec601650de93John McCall         /*filled in at end of ctor*/ false, false, false, false) {
39254b9c2d235fb9449e249d74f48ecfec601650de93John McCall  PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
39264b9c2d235fb9449e249d74f48ecfec601650de93John McCall  PseudoObjectExprBits.ResultIndex = resultIndex + 1;
39274b9c2d235fb9449e249d74f48ecfec601650de93John McCall
39284b9c2d235fb9449e249d74f48ecfec601650de93John McCall  for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
39294b9c2d235fb9449e249d74f48ecfec601650de93John McCall    Expr *E = (i == 0 ? syntax : semantics[i-1]);
39304b9c2d235fb9449e249d74f48ecfec601650de93John McCall    getSubExprsBuffer()[i] = E;
39314b9c2d235fb9449e249d74f48ecfec601650de93John McCall
39324b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->isTypeDependent())
39334b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.TypeDependent = true;
39344b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->isValueDependent())
39354b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.ValueDependent = true;
39364b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->isInstantiationDependent())
39374b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.InstantiationDependent = true;
39384b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->containsUnexpandedParameterPack())
39394b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.ContainsUnexpandedParameterPack = true;
39404b9c2d235fb9449e249d74f48ecfec601650de93John McCall
39414b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (isa<OpaqueValueExpr>(E))
39426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != nullptr &&
39434b9c2d235fb9449e249d74f48ecfec601650de93John McCall             "opaque-value semantic expressions for pseudo-object "
39444b9c2d235fb9449e249d74f48ecfec601650de93John McCall             "operations must have sources");
39454b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
39464b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
39474b9c2d235fb9449e249d74f48ecfec601650de93John McCall
394805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
394977ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//  Child Iterators for iterating over subexpressions/substatements
395077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
395177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
3952f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne// UnaryExprOrTypeTraitExpr
3953f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter CollingbourneStmt::child_range UnaryExprOrTypeTraitExpr::children() {
39540518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // If this is of a type and the type is a VLA type (and not a typedef), the
39550518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // size expression of the VLA needs to be treated as an executable expression.
39560518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // Why isn't this weirdness documented better in StmtIterator?
39570518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType()) {
3958f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
39590518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl                                   getArgumentType().getTypePtr()))
396063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      return child_range(child_iterator(T), child_iterator());
3961a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    return child_range(child_iterator(), child_iterator());
39620518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  }
396363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return child_range(&Argument.Ex, &Argument.Ex + 1);
39649ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
3965390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
39663b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin KramerAtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
3967dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman                       QualType t, AtomicOp op, SourceLocation RP)
3968dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman  : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3969dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman         false, false, false, false),
39703b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3971dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman{
39723b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
39733b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != args.size(); i++) {
3974dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isTypeDependent())
3975dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.TypeDependent = true;
3976dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isValueDependent())
3977dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.ValueDependent = true;
3978dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isInstantiationDependent())
3979dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.InstantiationDependent = true;
3980dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->containsUnexpandedParameterPack())
3981dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.ContainsUnexpandedParameterPack = true;
3982dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman
3983dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    SubExprs[i] = args[i];
3984dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman  }
3985dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman}
3986e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith
3987e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smithunsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
3988e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith  switch (Op) {
3989ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_init:
3990ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_load:
3991ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_load_n:
3992e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith    return 2;
3993ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith
3994ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_store:
3995ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_exchange:
3996ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_load:
3997ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_store:
3998ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_store_n:
3999ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_exchange_n:
4000ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_fetch_add:
4001ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_fetch_sub:
4002ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_fetch_and:
4003ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_fetch_or:
4004ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_fetch_xor:
4005ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_fetch_add:
4006ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_fetch_sub:
4007ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_fetch_and:
4008ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_fetch_or:
4009ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_fetch_xor:
401051b92401c9f95023a2ef27064fd5a60fd99175f5Richard Smith  case AO__atomic_fetch_nand:
4011ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_add_fetch:
4012ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_sub_fetch:
4013ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_and_fetch:
4014ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_or_fetch:
4015ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_xor_fetch:
401651b92401c9f95023a2ef27064fd5a60fd99175f5Richard Smith  case AO__atomic_nand_fetch:
4017e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith    return 3;
4018ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith
4019ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_exchange:
4020ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith    return 4;
4021ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith
4022ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_compare_exchange_strong:
4023ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_compare_exchange_weak:
4024e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith    return 5;
4025ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith
4026ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_compare_exchange:
4027ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_compare_exchange_n:
4028ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith    return 6;
4029e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith  }
4030e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith  llvm_unreachable("unknown atomic op");
4031e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith}
4032a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
4033a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga NainarQualType OMPArraySectionExpr::getBaseOriginalType(Expr *Base) {
4034a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  unsigned ArraySectionCount = 0;
4035a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  while (auto *OASE = dyn_cast<OMPArraySectionExpr>(Base->IgnoreParens())) {
4036a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    Base = OASE->getBase();
4037a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    ++ArraySectionCount;
4038a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  }
4039a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  while (auto *ASE = dyn_cast<ArraySubscriptExpr>(Base->IgnoreParens())) {
4040a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    Base = ASE->getBase();
4041a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    ++ArraySectionCount;
4042a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  }
4043a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  auto OriginalTy = Base->getType();
4044a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  if (auto *DRE = dyn_cast<DeclRefExpr>(Base))
4045a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
4046a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      OriginalTy = PVD->getOriginalType().getNonReferenceType();
4047a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
4048a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  for (unsigned Cnt = 0; Cnt < ArraySectionCount; ++Cnt) {
4049a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    if (OriginalTy->isAnyPointerType())
4050a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      OriginalTy = OriginalTy->getPointeeType();
4051a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    else {
4052a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      assert (OriginalTy->isArrayType());
4053a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      OriginalTy = OriginalTy->castAsArrayTypeUnsafe()->getElementType();
4054a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    }
4055a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  }
4056a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  return OriginalTy;
4057a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar}
4058a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
4059