Expr.cpp revision 15b2674896371ac2a0fe707b538a1a29dec9d8e4
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"
2319cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/StmtVisitor.h"
251b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
263f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose#include "clang/Basic/CharInfo.h"
2708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner#include "clang/Basic/SourceManager.h"
28da5a6b6d9fd52899499d5b7b46273ec844dcaa6eChris Lattner#include "clang/Basic/TargetInfo.h"
292fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/Lex/Lexer.h"
302fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/Lex/LiteralSupport.h"
312fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/Sema/SemaDiagnostic.h"
32cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor#include "llvm/Support/ErrorHandling.h"
333a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson#include "llvm/Support/raw_ostream.h"
34ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor#include <algorithm>
3564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman#include <cstring>
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
388d852e35adb46e0799538dfc9c80d44f27cd3597Rafael Espindolaconst CXXRecordDecl *Expr::getBestDynamicClassType() const {
39632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola  const Expr *E = ignoreParenBaseCasts();
400b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola
410b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  QualType DerivedType = E->getType();
420b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  if (const PointerType *PTy = DerivedType->getAs<PointerType>())
430b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola    DerivedType = PTy->getPointeeType();
440b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola
45251c449b280eb845017a6c022ed7189d17c63d49Rafael Espindola  if (DerivedType->isDependentType())
46251c449b280eb845017a6c022ed7189d17c63d49Rafael Espindola    return NULL;
47251c449b280eb845017a6c022ed7189d17c63d49Rafael Espindola
480b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  const RecordType *Ty = DerivedType->castAs<RecordType>();
490b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  Decl *D = Ty->getDecl();
500b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  return cast<CXXRecordDecl>(D);
510b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola}
520b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola
534e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smithconst Expr *Expr::skipRValueSubobjectAdjustments(
544e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith    SmallVectorImpl<const Expr *> &CommaLHSs,
554e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith    SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
560a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  const Expr *E = this;
570a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  while (true) {
580a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    E = E->IgnoreParens();
590a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
600a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
610a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      if ((CE->getCastKind() == CK_DerivedToBase ||
620a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola           CE->getCastKind() == CK_UncheckedDerivedToBase) &&
630a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola          E->getType()->isRecordType()) {
640a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        E = CE->getSubExpr();
650a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        CXXRecordDecl *Derived
660a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola          = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
670a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        Adjustments.push_back(SubobjectAdjustment(CE, Derived));
680a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        continue;
690a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      }
700a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
710a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      if (CE->getCastKind() == CK_NoOp) {
720a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        E = CE->getSubExpr();
730a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        continue;
740a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      }
750a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
76d6b698739ab157348acafcec5b06a05d3d35377dRichard Smith      if (!ME->isArrow()) {
770a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        assert(ME->getBase()->getType()->isRecordType());
780a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
79d6b698739ab157348acafcec5b06a05d3d35377dRichard Smith          if (!Field->isBitField() && !Field->getType()->isReferenceType()) {
80d1b55dcf6bd61d4350b01e15590bde9bb5a334bbRichard Smith            E = ME->getBase();
81d1b55dcf6bd61d4350b01e15590bde9bb5a334bbRichard Smith            Adjustments.push_back(SubobjectAdjustment(Field));
82d1b55dcf6bd61d4350b01e15590bde9bb5a334bbRichard Smith            continue;
83d1b55dcf6bd61d4350b01e15590bde9bb5a334bbRichard Smith          }
840a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        }
850a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      }
860a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
870a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      if (BO->isPtrMemOp()) {
88ef4b666e841e3917385892713612888ec6c3a056Rafael Espindola        assert(BO->getRHS()->isRValue());
890a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        E = BO->getLHS();
900a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        const MemberPointerType *MPT =
910a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola          BO->getRHS()->getType()->getAs<MemberPointerType>();
920a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola        Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
934e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith        continue;
944e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith      } else if (BO->getOpcode() == BO_Comma) {
954e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith        CommaLHSs.push_back(BO->getLHS());
964e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith        E = BO->getRHS();
974e43dec4df9d7dd8e07b47bb15967f1b733a9bc6Richard Smith        continue;
980a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      }
990a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    }
1000a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
1010a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    // Nothing changed.
1020a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    break;
1030a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  }
1040a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  return E;
1050a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola}
1060a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
1070a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindolaconst Expr *
1080a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael EspindolaExpr::findMaterializedTemporary(const MaterializeTemporaryExpr *&MTE) const {
1090a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  const Expr *E = this;
110c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith
111c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  // This might be a default initializer for a reference member. Walk over the
112c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  // wrapper node for that.
113c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
114c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    E = DAE->getExpr();
115c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith
1160a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  // Look through single-element init lists that claim to be lvalues. They're
1170a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  // just syntactic wrappers in this case.
1180a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E)) {
119c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    if (ILE->getNumInits() == 1 && ILE->isGLValue()) {
1200a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      E = ILE->getInit(0);
121c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith      if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
122c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith        E = DAE->getExpr();
123c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    }
1240a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  }
1250a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
1260a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  // Look through expressions for materialized temporaries (for now).
1270a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  if (const MaterializeTemporaryExpr *M
1280a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola      = dyn_cast<MaterializeTemporaryExpr>(E)) {
1290a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    MTE = M;
1300a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    E = M->GetTemporaryExpr();
1310a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  }
1320a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
1330a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
1340a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    E = DAE->getExpr();
1350a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  return E;
1360a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola}
1370a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
1382b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// isKnownToHaveBooleanValue - Return true if this is an integer expression
1392b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// that is known to return 0 or 1.  This happens for _Bool/bool expressions
1402b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// but also int expressions which are produced by things like comparisons in
1412b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner/// C.
1422b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattnerbool Expr::isKnownToHaveBooleanValue() const {
143f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  const Expr *E = IgnoreParens();
144f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
1452b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  // If this value has _Bool type, it is obvious 0/1.
146f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (E->getType()->isBooleanType()) return true;
147c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  // If this is a non-scalar-integer type, we don't care enough to try.
148f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (!E->getType()->isIntegralOrEnumerationType()) return false;
149c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
150f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1512b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    switch (UO->getOpcode()) {
1522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Plus:
1532b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return UO->getSubExpr()->isKnownToHaveBooleanValue();
1542b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    default:
1552b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return false;
1562b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
1572b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  }
158c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1596907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  // Only look through implicit casts.  If the user writes
1606907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  // '(int) (a && b)' treat it as an arbitrary int.
161f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
1622b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return CE->getSubExpr()->isKnownToHaveBooleanValue();
163c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
164f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
1652b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    switch (BO->getOpcode()) {
1662b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    default: return false;
1672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:   // Relational operators.
1682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
1692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
1702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
1712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:   // Equality operators.
1722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
1732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LAnd: // AND operator.
1742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LOr:  // Logical OR operator.
1752b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return true;
176c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_And:  // Bitwise AND operator.
1782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Xor:  // Bitwise XOR operator.
1792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Or:   // Bitwise OR operator.
1802b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      // Handle things like (x==2)|(y==12).
1812b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return BO->getLHS()->isKnownToHaveBooleanValue() &&
1822b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner             BO->getRHS()->isKnownToHaveBooleanValue();
183c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Comma:
1852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Assign:
1862b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      return BO->getRHS()->isKnownToHaveBooleanValue();
1872b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
1882b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  }
189c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
190f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
1912b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
1922b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner           CO->getFalseExpr()->isKnownToHaveBooleanValue();
193c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
1942b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  return false;
1952b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner}
1962b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner
19763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall// Amusing macro metaprogramming hack: check whether a class provides
19863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall// a more specific implementation of getExprLoc().
19990e25a8b2cc5d006e4ced052bcdb40c8af999456Daniel Dunbar//
20090e25a8b2cc5d006e4ced052bcdb40c8af999456Daniel Dunbar// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
20163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallnamespace {
20263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// This implementation is used when a class provides a custom
20363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// implementation of getExprLoc.
20463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  template <class E, class T>
20563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  SourceLocation getExprLocImpl(const Expr *expr,
20663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                                SourceLocation (T::*v)() const) {
20763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return static_cast<const E*>(expr)->getExprLoc();
20863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
20963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
21063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// This implementation is used when a class doesn't provide
21163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// a custom implementation of getExprLoc.  Overload resolution
21263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// should pick it over the implementation above because it's
21363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// more specialized according to function template partial ordering.
21463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  template <class E>
21563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  SourceLocation getExprLocImpl(const Expr *expr,
21663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                                SourceLocation (Expr::*v)() const) {
21790e25a8b2cc5d006e4ced052bcdb40c8af999456Daniel Dunbar    return static_cast<const E*>(expr)->getLocStart();
21863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
21963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall}
22063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
22163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallSourceLocation Expr::getExprLoc() const {
22263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  switch (getStmtClass()) {
22363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::NoStmtClass: llvm_unreachable("statement without class");
22463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(type)
22563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define STMT(type, base) \
22663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
22763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define EXPR(type, base) \
22863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
22963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#include "clang/AST/StmtNodes.inc"
23063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
23163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  llvm_unreachable("unknown statement kind");
23263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall}
23363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
2345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Expressions.
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
238561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor/// \brief Compute the type-, value-, and instantiation-dependence of a
239561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor/// declaration reference
240d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor/// based on the declaration being referenced.
2419db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig Topperstatic void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
2429db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig Topper                                     QualType T, bool &TypeDependent,
243561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                     bool &ValueDependent,
244561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                     bool &InstantiationDependent) {
245d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  TypeDependent = false;
246d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ValueDependent = false;
247561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  InstantiationDependent = false;
2480da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
2490da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  // (TD) C++ [temp.dep.expr]p3:
2500da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //   An id-expression is type-dependent if it contains:
2510da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //
252c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  // and
2530da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //
2540da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  // (VD) C++ [temp.dep.constexpr]p2:
2550da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  An identifier is value-dependent if it is:
256d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
2570da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - an identifier that was declared with dependent type
2580da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD)  - a name declared with a dependent type,
259d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (T->isDependentType()) {
260d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    TypeDependent = true;
261d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
262561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
263d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
264561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  } else if (T->isInstantiationDependentType()) {
265561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
2660da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
267d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
2680da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (TD)  - a conversion-function-id that specifies a dependent type
269d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (D->getDeclName().getNameKind()
270561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                == DeclarationName::CXXConversionFunctionName) {
271561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    QualType T = D->getDeclName().getCXXNameType();
272561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (T->isDependentType()) {
273561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      TypeDependent = true;
274561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ValueDependent = true;
275561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
276561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      return;
277561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
278561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
279561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (T->isInstantiationDependentType())
280561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
2810da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  }
282561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
2830da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD)  - the name of a non-type template parameter,
284d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (isa<NonTypeTemplateParmDecl>(D)) {
285d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
286561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
287d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
288d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
289d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
2900da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //  (VD) - a constant with integral or enumeration type and is
2910da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor  //         initialized with an expression that is value-dependent.
292db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //  (VD) - a constant with literal type and is initialized with an
293db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //         expression that is value-dependent [C++11].
294db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //  (VD) - FIXME: Missing from the standard:
295db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //       -  an entity with reference type and is initialized with an
296db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  //          expression that is value-dependent [C++11]
297d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
29880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if ((Ctx.getLangOpts().CPlusPlus11 ?
299a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith           Var->getType()->isLiteralType(Ctx) :
300db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith           Var->getType()->isIntegralOrEnumerationType()) &&
3014ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie        (Var->getType().isConstQualified() ||
302db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith         Var->getType()->isReferenceType())) {
30331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      if (const Expr *Init = Var->getAnyInitializer())
304561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        if (Init->isValueDependent()) {
305d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor          ValueDependent = true;
306561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          InstantiationDependent = true;
307561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        }
308db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith    }
309db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith
310bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    // (VD) - FIXME: Missing from the standard:
311bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    //      -  a member function or a static data member of the current
312bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor    //         instantiation
313db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith    if (Var->isStaticDataMember() &&
314db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith        Var->getDeclContext()->isDependentContext()) {
315d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor      ValueDependent = true;
316561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
317561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
318d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
319d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    return;
320d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
321d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
322bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  // (VD) - FIXME: Missing from the standard:
323bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  //      -  a member function or a static data member of the current
324bb6e73fcf60fa5a4cc36c14744dc366b658443b5Douglas Gregor  //         instantiation
325d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
326d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
327561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
328db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith  }
329d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor}
330d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
3319db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig Toppervoid DeclRefExpr::computeDependence(const ASTContext &Ctx) {
332d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool TypeDependent = false;
333d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  bool ValueDependent = false;
334561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  bool InstantiationDependent = false;
3353d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar  computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
3363d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar                           ValueDependent, InstantiationDependent);
337d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
338d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  // (TD) C++ [temp.dep.expr]p3:
339d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //   An id-expression is type-dependent if it contains:
340d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //
341d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  // and
342d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //
343d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  // (VD) C++ [temp.dep.constexpr]p2:
344d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  //  An identifier is value-dependent if it is:
345d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (!TypeDependent && !ValueDependent &&
346d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor      hasExplicitTemplateArgs() &&
347d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor      TemplateSpecializationType::anyDependentTemplateArguments(
348d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor                                                            getTemplateArgs(),
349561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                                       getNumTemplateArgs(),
350561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                                      InstantiationDependent)) {
351d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    TypeDependent = true;
352d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    ValueDependent = true;
353561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    InstantiationDependent = true;
354d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  }
355d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
356d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.TypeDependent = TypeDependent;
357d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  ExprBits.ValueDependent = ValueDependent;
358561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  ExprBits.InstantiationDependent = InstantiationDependent;
359d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor
36010738d36b150aa65206890c1c845cdba076e4200Douglas Gregor  // Is the declaration a parameter pack?
361d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor  if (getDecl()->isParameterPack())
3621fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor    ExprBits.ContainsUnexpandedParameterPack = true;
3630da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor}
3640da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor
3659db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperDeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
3663d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar                         NestedNameSpecifierLoc QualifierLoc,
367e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                         SourceLocation TemplateKWLoc,
368f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                         ValueDecl *D, bool RefersToEnclosingLocal,
369f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                         const DeclarationNameInfo &NameInfo,
3703aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                         NamedDecl *FoundD,
3712577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                         const TemplateArgumentListInfo *TemplateArgs,
372f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                         QualType T, ExprValueKind VK)
373561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
374cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth    D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
375cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth  DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
3767e740bd36772aae16b5cc5e605998ccc5eaf26dbChandler Carruth  if (QualifierLoc)
3776857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    getInternalQualifierLoc() = QualifierLoc;
3783aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
3793aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (FoundD)
3803aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    getInternalFoundDecl() = FoundD;
381e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  DeclRefExprBits.HasTemplateKWAndArgsInfo
382e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
383f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
384561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (TemplateArgs) {
385561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool Dependent = false;
386561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool InstantiationDependent = false;
387561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool ContainsUnexpandedParameterPack = false;
388e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
389e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               Dependent,
390e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               InstantiationDependent,
391e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                               ContainsUnexpandedParameterPack);
392561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (InstantiationDependent)
393561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      setInstantiationDependent(true);
394e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  } else if (TemplateKWLoc.isValid()) {
395e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
396561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
397b8da98a2182d2c69c72136b9a9ebabd4694f3d6bBenjamin Kramer  DeclRefExprBits.HadMultipleCandidates = 0;
398b8da98a2182d2c69c72136b9a9ebabd4694f3d6bBenjamin Kramer
3993d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar  computeDependence(Ctx);
4002577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
4012577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
4029db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperDeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
40340d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                                 NestedNameSpecifierLoc QualifierLoc,
404e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                 SourceLocation TemplateKWLoc,
405dbd872f273a8dbf22e089b3def6c09f0a460965dJohn McCall                                 ValueDecl *D,
406f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                                 bool RefersToEnclosingLocal,
407a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor                                 SourceLocation NameLoc,
4080da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                 QualType T,
409f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
4103aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                 NamedDecl *FoundD,
4110da76df9218d7c27b471b0a4d83a5b29fe24e5b4Douglas Gregor                                 const TemplateArgumentListInfo *TemplateArgs) {
412e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  return Create(Context, QualifierLoc, TemplateKWLoc, D,
413f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                RefersToEnclosingLocal,
4142577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                DeclarationNameInfo(D->getDeclName(), NameLoc),
4153aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                T, VK, FoundD, TemplateArgs);
4162577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
4172577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
4189db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperDeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
41940d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                                 NestedNameSpecifierLoc QualifierLoc,
420e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                 SourceLocation TemplateKWLoc,
4212577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 ValueDecl *D,
422f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                                 bool RefersToEnclosingLocal,
4232577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 const DeclarationNameInfo &NameInfo,
4242577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 QualType T,
425f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
4263aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                 NamedDecl *FoundD,
4272577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                 const TemplateArgumentListInfo *TemplateArgs) {
4283aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  // Filter out cases where the found Decl is the same as the value refenenced.
4293aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (D == FoundD)
4303aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    FoundD = 0;
4313aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
432a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  std::size_t Size = sizeof(DeclRefExpr);
4337247c88d1e41514a41085f83ebf03dd5220e054aDavid Blaikie  if (QualifierLoc)
4346857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    Size += sizeof(NestedNameSpecifierLoc);
4353aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (FoundD)
4363aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    Size += sizeof(NamedDecl *);
437d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (TemplateArgs)
438e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
439e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  else if (TemplateKWLoc.isValid())
440e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
4413aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
4423248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
4433d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar  return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
444f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                               RefersToEnclosingLocal,
4453d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar                               NameInfo, FoundD, TemplateArgs, T, VK);
446a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor}
447a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
4489db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperDeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
449def0354384d9c4431f7b58b664b59896d4623028Douglas Gregor                                      bool HasQualifier,
4503aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                                      bool HasFoundDecl,
451e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      bool HasTemplateKWAndArgsInfo,
452663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis                                      unsigned NumTemplateArgs) {
453663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  std::size_t Size = sizeof(DeclRefExpr);
454663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  if (HasQualifier)
4556857c3e12c86fd0271eb46baab5b18756a94f4cbChandler Carruth    Size += sizeof(NestedNameSpecifierLoc);
4563aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (HasFoundDecl)
4573aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    Size += sizeof(NamedDecl *);
458e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  if (HasTemplateKWAndArgsInfo)
459e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
4603aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
4613248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
462663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis  return new (Mem) DeclRefExpr(EmptyShell());
463663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis}
464663e380d7b2de2bbf20e886e05371195bea9adc4Argyrios Kyrtzidis
465396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel DunbarSourceLocation DeclRefExpr::getLocStart() const {
466396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  if (hasQualifier())
467396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar    return getQualifierLoc().getBeginLoc();
468396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  return getNameInfo().getLocStart();
469396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar}
470396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel DunbarSourceLocation DeclRefExpr::getLocEnd() const {
471396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  if (hasExplicitTemplateArgs())
472396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar    return getRAngleLoc();
473396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  return getNameInfo().getLocEnd();
474396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar}
475a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor
4763a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson// FIXME: Maybe this should use DeclPrinter with a special "print predefined
4773a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson// expr" policy instead.
478848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlssonstd::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
479848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson  ASTContext &Context = CurrentDecl->getASTContext();
480848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson
4813a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
482848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson    if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
4833a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      return FD->getNameAsString();
4843a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
485f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith    SmallString<256> Name;
4863a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::raw_svector_ostream Out(Name);
4873a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4883a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
489848fa64143fbe5ae62a601ad61277f741e54dfabAnders Carlsson      if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
4903a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        Out << "virtual ";
4914eadcc569223135e13353c9381b448986e3f7053Sam Weinig      if (MD->isStatic())
4924eadcc569223135e13353c9381b448986e3f7053Sam Weinig        Out << "static ";
4933a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    }
4943a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    PrintingPolicy Policy(Context.getLangOpts());
496b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer    std::string Proto;
497abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    llvm::raw_string_ostream POut(Proto);
498b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer    FD->printQualifiedName(POut, Policy);
4993a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
500abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    const FunctionDecl *Decl = FD;
501abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
502abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      Decl = Pattern;
503abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
5043a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    const FunctionProtoType *FT = 0;
5053a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (FD->hasWrittenPrototype())
5063a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      FT = dyn_cast<FunctionProtoType>(AFT);
5073a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
508abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    POut << "(";
5093a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (FT) {
510abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
5113a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        if (i) POut << ", ";
5127ad5c996e9519ed4e9afd1f0166be1cd2be8415aArgyrios Kyrtzidis        POut << Decl->getParamDecl(i)->getType().stream(Policy);
5133a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      }
5143a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
5153a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      if (FT->isVariadic()) {
5163a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        if (FD->getNumParams()) POut << ", ";
5173a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson        POut << "...";
5183a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson      }
5193a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    }
520abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    POut << ")";
5213a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
5224eadcc569223135e13353c9381b448986e3f7053Sam Weinig    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
5234ae711b5ef292378d5b88cede3d4b708174053e2Argyrios Kyrtzidis      const FunctionType *FT = MD->getType()->castAs<FunctionType>();
5244ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie      if (FT->isConst())
525abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        POut << " const";
5264ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie      if (FT->isVolatile())
527abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        POut << " volatile";
528abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      RefQualifierKind Ref = MD->getRefQualifier();
529abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      if (Ref == RQ_LValue)
530abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        POut << " &";
531abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      else if (Ref == RQ_RValue)
532abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        POut << " &&";
533abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    }
534abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor
535abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
536abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    SpecsTy Specs;
537abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    const DeclContext *Ctx = FD->getDeclContext();
538abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    while (Ctx && isa<NamedDecl>(Ctx)) {
539abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      const ClassTemplateSpecializationDecl *Spec
540abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor                               = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
541abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      if (Spec && !Spec->isExplicitSpecialization())
542abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        Specs.push_back(Spec);
543abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      Ctx = Ctx->getParent();
544abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    }
545abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor
546abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    std::string TemplateParams;
547abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    llvm::raw_string_ostream TOut(TemplateParams);
548abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
549abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor         I != E; ++I) {
550abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      const TemplateParameterList *Params
551abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor                  = (*I)->getSpecializedTemplate()->getTemplateParameters();
552abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      const TemplateArgumentList &Args = (*I)->getTemplateArgs();
553abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      assert(Params->size() == Args.size());
554abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
555abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        StringRef Param = Params->getParam(i)->getName();
556abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        if (Param.empty()) continue;
557abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        TOut << Param << " = ";
558abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        Args.get(i).print(Policy, TOut);
559abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        TOut << ", ";
560abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      }
5614eadcc569223135e13353c9381b448986e3f7053Sam Weinig    }
5624eadcc569223135e13353c9381b448986e3f7053Sam Weinig
563abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    FunctionTemplateSpecializationInfo *FSI
564abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor                                          = FD->getTemplateSpecializationInfo();
565abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    if (FSI && !FSI->isExplicitSpecialization()) {
566abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      const TemplateParameterList* Params
567abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor                                  = FSI->getTemplate()->getTemplateParameters();
568abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      const TemplateArgumentList* Args = FSI->TemplateArguments;
569abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      assert(Params->size() == Args->size());
570abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      for (unsigned i = 0, e = Params->size(); i != e; ++i) {
571abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        StringRef Param = Params->getParam(i)->getName();
572abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        if (Param.empty()) continue;
573abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        TOut << Param << " = ";
574abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        Args->get(i).print(Policy, TOut);
575abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor        TOut << ", ";
576abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      }
577abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    }
578abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor
579abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    TOut.flush();
580abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    if (!TemplateParams.empty()) {
581abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      // remove the trailing comma and space
582abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      TemplateParams.resize(TemplateParams.size() - 2);
583abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor      POut << " [" << TemplateParams << "]";
584abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    }
585abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor
586abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor    POut.flush();
587abf65ce5ddfb8db0d34280d30a91253930732f4fDouglas Gregor
58828bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    // Print "auto" for all deduced return types. This includes C++1y return
58928bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    // type deduction and lambdas. For trailing return types resolve the
59028bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    // decltype expression. Otherwise print the real type when this is
59128bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    // not a constructor or destructor.
59228bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    if ((isa<CXXMethodDecl>(FD) &&
59328bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer         cast<CXXMethodDecl>(FD)->getParent()->isLambda()) ||
59428bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer        (FT && FT->getResultType()->getAs<AutoType>()))
59528bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer      Proto = "auto " + Proto;
59628bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    else if (FT && FT->getResultType()->getAs<DecltypeType>())
59728bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer      FT->getResultType()->getAs<DecltypeType>()->getUnderlyingType()
59828bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer          .getAsStringInternal(Proto, Policy);
59928bdbf0861fefb33474fddbda1d3c66ee29be2afBenjamin Kramer    else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
6003a1ce1ed0f5686384e712837bad28c576622e442Sam Weinig      AFT->getResultType().getAsStringInternal(Proto, Policy);
6013a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
6023a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << Proto;
6033a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
6043a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out.flush();
6053a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return Name.str().str();
6063a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
60715b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan  if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
60815b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan    for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
60915b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan      // Skip to its enclosing function or method, but not its enclosing
61015b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan      // CapturedDecl.
61115b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan      if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
61215b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan        const Decl *D = Decl::castFromDeclContext(DC);
61315b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan        return ComputeName(IT, D);
61415b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan      }
61515b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan    llvm_unreachable("CapturedDecl not inside a function or method");
61615b2674896371ac2a0fe707b538a1a29dec9d8e4Wei Pan  }
6173a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
618f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith    SmallString<256> Name;
6193a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    llvm::raw_svector_ostream Out(Name);
6203a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << (MD->isInstanceMethod() ? '-' : '+');
6213a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << '[';
622b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek
623b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    // For incorrect code, there might not be an ObjCInterfaceDecl.  Do
624b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    // a null check to avoid a crash.
625b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek    if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
626b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer      Out << *ID;
627b03d33edaf24af2893a50caee4d2c99839242c44Ted Kremenek
6283a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    if (const ObjCCategoryImplDecl *CID =
629900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer        dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
630f978059b82db8c0d849c5f992036210b5ca53200Benjamin Kramer      Out << '(' << *CID << ')';
631900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer
6323a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out <<  ' ';
6333a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out << MD->getSelector().getAsString();
6343a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out <<  ']';
6353a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
6363a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    Out.flush();
6373a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return Name.str().str();
6383a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
6393a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
6403a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
6413a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson    return "top level";
6423a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  }
6433a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return "";
6443a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
6453a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
64605ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid APNumericStorage::setIntValue(const ASTContext &C,
64705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                                   const llvm::APInt &Val) {
6489996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  if (hasAllocation())
6499996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    C.Deallocate(pVal);
6509996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
6519996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  BitWidth = Val.getBitWidth();
6529996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  unsigned NumWords = Val.getNumWords();
6539996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  const uint64_t* Words = Val.getRawData();
6549996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  if (NumWords > 1) {
6559996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    pVal = new (C) uint64_t[NumWords];
6569996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    std::copy(Words, Words + NumWords, pVal);
6579996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  } else if (NumWords == 1)
6589996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    VAL = Words[0];
6599996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  else
6609996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis    VAL = 0;
6619996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
6629996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
66305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperIntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
664478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer                               QualType type, SourceLocation l)
665478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
666478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer         false, false),
667478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer    Loc(l) {
668478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
669478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  assert(V.getBitWidth() == C.getIntWidth(type) &&
670478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer         "Integer type is not the correct size for constant.");
671478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  setValue(C, V);
672478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer}
673478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer
6749996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral *
67505ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperIntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
6769996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                       QualType type, SourceLocation l) {
6779996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) IntegerLiteral(C, V, type, l);
6789996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
6799996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
6809996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisIntegerLiteral *
68105ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperIntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
6829996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) IntegerLiteral(Empty);
6839996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
6849996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
68505ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperFloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
686478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer                                 bool isexact, QualType Type, SourceLocation L)
687478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
688478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer         false, false), Loc(L) {
6899ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  setSemantics(V.getSemantics());
690478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  FloatingLiteralBits.IsExact = isexact;
691478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  setValue(C, V);
692478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer}
693478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer
69405ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperFloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
695478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  : Expr(FloatingLiteralClass, Empty) {
6969ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  setRawSemantics(IEEEhalf);
697478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  FloatingLiteralBits.IsExact = false;
698478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer}
699478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer
7009996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral *
70105ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperFloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
7029996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                        bool isexact, QualType Type, SourceLocation L) {
7039996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return new (C) FloatingLiteral(C, V, isexact, Type, L);
7049996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
7059996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
7069996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios KyrtzidisFloatingLiteral *
70705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperFloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
70831dfd642d5ac33c4ee0cfe1d7a1d4da455dcc7a4Akira Hatanaka  return new (C) FloatingLiteral(C, Empty);
7099996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis}
7109996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis
7119ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northoverconst llvm::fltSemantics &FloatingLiteral::getSemantics() const {
7129ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  switch(FloatingLiteralBits.Semantics) {
7139ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case IEEEhalf:
7149ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::IEEEhalf;
7159ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case IEEEsingle:
7169ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::IEEEsingle;
7179ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case IEEEdouble:
7189ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::IEEEdouble;
7199ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case x87DoubleExtended:
7209ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::x87DoubleExtended;
7219ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case IEEEquad:
7229ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::IEEEquad;
7239ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  case PPCDoubleDouble:
7249ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    return llvm::APFloat::PPCDoubleDouble;
7259ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  }
7269ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  llvm_unreachable("Unrecognised floating semantics");
7279ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover}
7289ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover
7299ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northovervoid FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
7309ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  if (&Sem == &llvm::APFloat::IEEEhalf)
7319ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = IEEEhalf;
7329ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else if (&Sem == &llvm::APFloat::IEEEsingle)
7339ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = IEEEsingle;
7349ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else if (&Sem == &llvm::APFloat::IEEEdouble)
7359ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = IEEEdouble;
7369ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else if (&Sem == &llvm::APFloat::x87DoubleExtended)
7379ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = x87DoubleExtended;
7389ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else if (&Sem == &llvm::APFloat::IEEEquad)
7399ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = IEEEquad;
7409ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
7419ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    FloatingLiteralBits.Semantics = PPCDoubleDouble;
7429ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  else
7439ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    llvm_unreachable("Unknown floating semantics");
7449ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover}
7459ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover
746da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// getValueAsApproximateDouble - This returns the value as an inaccurate
747da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// double.  Note that this may cause loss of precision, but is useful for
748da8249e57f3badecf925571881fe57243935c6c1Chris Lattner/// debugging dumps, etc.
749da8249e57f3badecf925571881fe57243935c6c1Chris Lattnerdouble FloatingLiteral::getValueAsApproximateDouble() const {
750da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  llvm::APFloat V = getValue();
751ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  bool ignored;
752ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen  V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
753ee5a700af3fe9ae1a639c271f093f40677dddc04Dale Johannesen            &ignored);
754da8249e57f3badecf925571881fe57243935c6c1Chris Lattner  return V.convertToDouble();
755da8249e57f3badecf925571881fe57243935c6c1Chris Lattner}
756da8249e57f3badecf925571881fe57243935c6c1Chris Lattner
7570fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewyckyint StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
758fd819783aafa39b3bfdfcc095270352074ef4734Eli Friedman  int CharByteWidth = 0;
7590fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky  switch(k) {
76064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case Ascii:
76164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case UTF8:
7620fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky      CharByteWidth = target.getCharWidth();
76364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
76464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case Wide:
7650fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky      CharByteWidth = target.getWCharWidth();
76664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
76764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case UTF16:
7680fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky      CharByteWidth = target.getChar16Width();
76964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
77064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case UTF32:
7710fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky      CharByteWidth = target.getChar32Width();
772fd819783aafa39b3bfdfcc095270352074ef4734Eli Friedman      break;
77364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  }
77464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
77564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  CharByteWidth /= 8;
7760fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky  assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
77764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman         && "character byte widths supported are 1, 2, and 4 only");
77864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  return CharByteWidth;
77964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman}
78064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
78105ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperStringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
7825cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                     StringKind Kind, bool Pascal, QualType Ty,
7831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     const SourceLocation *Loc,
784a135fb43eb94524a6529768596a4533eed9aa70dAnders Carlsson                                     unsigned NumStrs) {
7852085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // Allocate enough space for the StringLiteral plus an array of locations for
7862085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  // any concatenated string tokens.
7872085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  void *Mem = C.Allocate(sizeof(StringLiteral)+
7882085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner                         sizeof(SourceLocation)*(NumStrs-1),
7893248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner                         llvm::alignOf<StringLiteral>());
7902085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  StringLiteral *SL = new (Mem) StringLiteral(Ty);
7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // OPTIMIZE: could allocate this appended to the StringLiteral.
79364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  SL->setString(C,Str,Kind,Pascal);
79464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
7952085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->TokLocs[0] = Loc[0];
7962085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  SL->NumConcatenated = NumStrs;
7975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
798726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner  if (NumStrs != 1)
7992085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner    memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
8002085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner  return SL;
801726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner}
802726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner
80305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperStringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
80405ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                                          unsigned NumStrs) {
805673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  void *Mem = C.Allocate(sizeof(StringLiteral)+
806673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor                         sizeof(SourceLocation)*(NumStrs-1),
8073248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner                         llvm::alignOf<StringLiteral>());
808673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  StringLiteral *SL = new (Mem) StringLiteral(QualType());
80964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  SL->CharByteWidth = 0;
81064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  SL->Length = 0;
811673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  SL->NumConcatenated = NumStrs;
812673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor  return SL;
813673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor}
814673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor
815ae54121c15cdd38f415f6fdda48215ab5e4dcee1Alexander Kornienkovoid StringLiteral::outputString(raw_ostream &OS) const {
8168ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  switch (getKind()) {
8178ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  case Ascii: break; // no prefix.
8188ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  case Wide:  OS << 'L'; break;
8198ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  case UTF8:  OS << "u8"; break;
8208ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  case UTF16: OS << 'u'; break;
8218ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  case UTF32: OS << 'U'; break;
8228ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  }
8238ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  OS << '"';
8248ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  static const char Hex[] = "0123456789ABCDEF";
8258ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
8268ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  unsigned LastSlashX = getLength();
8278ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  for (unsigned I = 0, N = getLength(); I != N; ++I) {
8288ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    switch (uint32_t Char = getCodeUnit(I)) {
8298ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    default:
8308ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      // FIXME: Convert UTF-8 back to codepoints before rendering.
8318ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
8328ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      // Convert UTF-16 surrogate pairs back to codepoints before rendering.
8338ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      // Leave invalid surrogates alone; we'll use \x for those.
8348ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
8358ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          Char <= 0xdbff) {
8368ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        uint32_t Trail = getCodeUnit(I + 1);
8378ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        if (Trail >= 0xdc00 && Trail <= 0xdfff) {
8388ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
8398ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          ++I;
8408ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        }
8418ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      }
8428ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
8438ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      if (Char > 0xff) {
8448ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        // If this is a wide string, output characters over 0xff using \x
8458ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
8468ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        // codepoint: use \x escapes for invalid codepoints.
8478ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        if (getKind() == Wide ||
8488ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu            (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
8498ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          // FIXME: Is this the best way to print wchar_t?
8508ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          OS << "\\x";
8518ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          int Shift = 28;
8528ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          while ((Char >> Shift) == 0)
8538ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu            Shift -= 4;
8548ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          for (/**/; Shift >= 0; Shift -= 4)
8558ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu            OS << Hex[(Char >> Shift) & 15];
8568ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          LastSlashX = I;
8578ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          break;
8588ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        }
8598ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
8608ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        if (Char > 0xffff)
8618ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          OS << "\\U00"
8628ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu             << Hex[(Char >> 20) & 15]
8638ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu             << Hex[(Char >> 16) & 15];
8648ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        else
8658ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          OS << "\\u";
8668ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        OS << Hex[(Char >> 12) & 15]
8678ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << Hex[(Char >>  8) & 15]
8688ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << Hex[(Char >>  4) & 15]
8698ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << Hex[(Char >>  0) & 15];
8708ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        break;
8718ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      }
8728ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
8738ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      // If we used \x... for the previous character, and this character is a
8748ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      // hexadecimal digit, prevent it being slurped as part of the \x.
8758ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      if (LastSlashX + 1 == I) {
8768ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        switch (Char) {
8778ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          case '0': case '1': case '2': case '3': case '4':
8788ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          case '5': case '6': case '7': case '8': case '9':
8798ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
8808ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu          case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
8818ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu            OS << "\"\"";
8828ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        }
8838ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      }
8848ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
8858ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      assert(Char <= 0xff &&
8868ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu             "Characters above 0xff should already have been handled.");
8878ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
8883f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose      if (isPrintable(Char))
8898ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        OS << (char)Char;
8908ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      else  // Output anything hard as an octal escape.
8918ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu        OS << '\\'
8928ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << (char)('0' + ((Char >> 6) & 7))
8938ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << (char)('0' + ((Char >> 3) & 7))
8948ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu           << (char)('0' + ((Char >> 0) & 7));
8958ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu      break;
8968ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    // Handle some common non-printable cases to make dumps prettier.
8978ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '\\': OS << "\\\\"; break;
8988ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '"': OS << "\\\""; break;
8998ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '\n': OS << "\\n"; break;
9008ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '\t': OS << "\\t"; break;
9018ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '\a': OS << "\\a"; break;
9028ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    case '\b': OS << "\\b"; break;
9038ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu    }
9048ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  }
9058ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu  OS << '"';
9068ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu}
9078ab09da1faaa33b9fa78de59cc4e191bfe9907b5Richard Trieu
90805ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid StringLiteral::setString(const ASTContext &C, StringRef Str,
90964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman                              StringKind Kind, bool IsPascal) {
91064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  //FIXME: we assume that the string data comes from a target that uses the same
91164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  // code unit size and endianess for the type of string.
91264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  this->Kind = Kind;
91364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  this->IsPascal = IsPascal;
91464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
9150fd7f4db69ea75bde3d5fd7365165c6f43d78be5Nick Lewycky  CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
91664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  assert((Str.size()%CharByteWidth == 0)
91764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman         && "size of data must be multiple of CharByteWidth");
91864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  Length = Str.size()/CharByteWidth;
91964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
92064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  switch(CharByteWidth) {
92164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case 1: {
92264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      char *AStrData = new (C) char[Length];
92366dfef17b8da7c89e20f32d4f0f4a04691b79768Argyrios Kyrtzidis      std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
92464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      StrData.asChar = AStrData;
92564f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
92664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    }
92764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case 2: {
92864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      uint16_t *AStrData = new (C) uint16_t[Length];
92966dfef17b8da7c89e20f32d4f0f4a04691b79768Argyrios Kyrtzidis      std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
93064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      StrData.asUInt16 = AStrData;
93164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
93264f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    }
93364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    case 4: {
93464f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      uint32_t *AStrData = new (C) uint32_t[Length];
93566dfef17b8da7c89e20f32d4f0f4a04691b79768Argyrios Kyrtzidis      std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
93664f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      StrData.asUInt32 = AStrData;
93764f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      break;
93864f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    }
93964f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman    default:
94064f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman      assert(false && "unsupported CharByteWidth");
94164f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman  }
942673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor}
943673ecd6a4a9f7c12fb6f76f84f654dbdcdc89e76Douglas Gregor
94408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// getLocationOfByte - Return a source location that points to the specified
94508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// byte of this string literal.
94608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner///
94708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// Strings are amazingly complex.  They can be formed from multiple tokens and
94808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// can have escape sequences in them in addition to the usual trigraph and
94908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner/// escaped newline business.  This routine handles this complexity.
95008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner///
95108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris LattnerSourceLocation StringLiteral::
95208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris LattnergetLocationOfByte(unsigned ByteNo, const SourceManager &SM,
95308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner                  const LangOptions &Features, const TargetInfo &Target) const {
954df9ef1bc8c3780307ab2ed81bf5e31c23310b936Richard Smith  assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
955df9ef1bc8c3780307ab2ed81bf5e31c23310b936Richard Smith         "Only narrow string literals are currently supported");
9565cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
95708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  // Loop over all of the tokens in this string until we find the one that
95808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  // contains the byte we're looking for.
95908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  unsigned TokNo = 0;
96008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  while (1) {
96108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    assert(TokNo < getNumConcatenated() && "Invalid byte number!");
96208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
96308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
96408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Get the spelling of the string so that we can get the data that makes up
96508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // the string literal, not the identifier for the macro it is potentially
96608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // expanded through.
96708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
96808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
96908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Re-lex the token to get its length and original spelling.
97008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
97108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    bool Invalid = false;
9725f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
97308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    if (Invalid)
97408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      return StrTokSpellingLoc;
97508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
97608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    const char *StrData = Buffer.data()+LocInfo.second;
97708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
97808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Create a lexer starting at the beginning of this token.
979df8755884e039d3f313ee0fea42b955257b5e240Argyrios Kyrtzidis    Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
980df8755884e039d3f313ee0fea42b955257b5e240Argyrios Kyrtzidis                   Buffer.begin(), StrData, Buffer.end());
98108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    Token TheTok;
98208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    TheLexer.LexFromRawLexer(TheTok);
98308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
98408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Use the StringLiteralParser to compute the length of the string in bytes.
98508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
98608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    unsigned TokNumBytes = SLP.GetStringLength();
98708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
98808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // If the byte is in this token, return the location of the byte.
98908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    if (ByteNo < TokNumBytes ||
990935a70c1e76d78985f20d422940280161b941299Hans Wennborg        (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
99108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
99208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
99308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      // Now that we know the offset of the token in the spelling, use the
99408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      // preprocessor to get the offset in the original source.
99508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner      return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
99608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    }
99708f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
99808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    // Move to the next string token.
99908f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    ++TokNo;
100008f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner    ByteNo -= TokNumBytes;
100108f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner  }
100208f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner}
100308f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
100408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
100508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner
10065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
10075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "sizeof" or "[pre]++".
10080bea86307eb8c16339315a1e261fc490eb505c5bDavid BlaikieStringRef UnaryOperator::getOpcodeStr(Opcode Op) {
10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
10102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostInc: return "++";
10112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostDec: return "--";
10122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PreInc:  return "++";
10132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PreDec:  return "--";
10142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_AddrOf:  return "&";
10152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Deref:   return "*";
10162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:    return "+";
10172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus:   return "-";
10182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Not:     return "~";
10192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_LNot:    return "!";
10202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Real:    return "__real";
10212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Imag:    return "__imag";
10222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Extension: return "__extension__";
10235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1024561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie  llvm_unreachable("Unknown unary operator");
10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCallUnaryOperatorKind
1028bc736fceca6f0bca31d16003a7587857190408fbDouglas GregorUnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1029bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  switch (OO) {
1030b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("No unary operator for overloaded function");
10312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PlusPlus:   return Postfix ? UO_PostInc : UO_PreInc;
10322de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
10332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Amp:        return UO_AddrOf;
10342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Star:       return UO_Deref;
10352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Plus:       return UO_Plus;
10362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Minus:      return UO_Minus;
10372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Tilde:      return UO_Not;
10382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Exclaim:    return UO_LNot;
1039bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  }
1040bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor}
1041bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
1042bc736fceca6f0bca31d16003a7587857190408fbDouglas GregorOverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1043bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  switch (Opc) {
10442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
10452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
10462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_AddrOf: return OO_Amp;
10472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Deref: return OO_Star;
10482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus: return OO_Plus;
10492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus: return OO_Minus;
10502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Not: return OO_Tilde;
10512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_LNot: return OO_Exclaim;
1052bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  default: return OO_None;
1053bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor  }
1054bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor}
1055bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
1056bc736fceca6f0bca31d16003a7587857190408fbDouglas Gregor
10575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Postfix Operators.
10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
106105ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
106205ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                   unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
106305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                   ExprValueKind VK, SourceLocation rparenloc)
1064f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(SC, t, VK, OK_Ordinary,
1065bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isTypeDependent(),
1066bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isValueDependent(),
1067561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         fn->isInstantiationDependent(),
1068bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->containsUnexpandedParameterPack()),
10693b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumArgs(args.size()) {
10701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10713b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
1072b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  SubExprs[FN] = fn;
10733b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != args.size(); ++i) {
1074bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
1075bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
1076bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
1077bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
1078561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
1079561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
1080bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
1081bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
1082bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
1083cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
1084bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
1085668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
1086cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = NumPreArgs;
1087b4609806e9232593ece09ce08b630836e825865cDouglas Gregor  RParenLoc = rparenloc;
1088b4609806e9232593ece09ce08b630836e825865cDouglas Gregor}
1089e2ce1d9440186cf3332368291cd884a6e3ae8946Nate Begeman
109005ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
1091f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                   QualType t, ExprValueKind VK, SourceLocation rparenloc)
1092f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(CallExprClass, t, VK, OK_Ordinary,
1093bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isTypeDependent(),
1094bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->isValueDependent(),
1095561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         fn->isInstantiationDependent(),
1096bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         fn->containsUnexpandedParameterPack()),
10973b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumArgs(args.size()) {
1098668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
10993b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
110077ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek  SubExprs[FN] = fn;
11013b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != args.size(); ++i) {
1102bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
1103bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
1104bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
1105bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
1106561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
1107561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
1108bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
1109bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
1110bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
1111cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    SubExprs[i+PREARGS_START] = args[i];
1112bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
1113668bf91d31265b6ea8c3eb854ba450857701f269Ted Kremenek
1114cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = 0;
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  RParenLoc = rparenloc;
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
111805ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
11191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1120bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  // FIXME: Why do we allocate this?
1121cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[PREARGS_START];
1122cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = 0;
1123cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne}
1124cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne
112505ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
1126cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne                   EmptyShell Empty)
1127cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1128cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  // FIXME: Why do we allocate this?
1129cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1130cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  CallExprBits.NumPreArgs = NumPreArgs;
11311f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor}
11321f0d0133b0e8d1f01f63951ee04927796b34740dDouglas Gregor
1133d20254f2875d0004c57ee766f258dbcee29f4841Nuno LopesDecl *CallExpr::getCalleeDecl() {
1134e8683d6d389def2fce229325435ba3c2b3f75492John McCall  Expr *CEE = getCallee()->IgnoreParenImpCasts();
11351ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor
11361ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor  while (SubstNonTypeTemplateParmExpr *NTTP
11371ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor                                = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
11381ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor    CEE = NTTP->getReplacement()->IgnoreParenCasts();
11391ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor  }
11401ddc9c4674b7c737ec65241a531df8c64e40b0a9Douglas Gregor
1141200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  // If we're calling a dereference, look at the pointer instead.
1142200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1143200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl    if (BO->isPtrMemOp())
1144200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl      CEE = BO->getRHS()->IgnoreParenCasts();
1145200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1146200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl    if (UO->getOpcode() == UO_Deref)
1147200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl      CEE = UO->getSubExpr()->IgnoreParenCasts();
1148200121569dc6cff10a1fb6ed7500098770b9dd25Sebastian Redl  }
11496346f963145ed18b6edf50a78753b47db505e912Chris Lattner  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
1150d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    return DRE->getDecl();
1151cb1c77f90d4e747b83a0d0cc125dc01567378f82Nuno Lopes  if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1152cb1c77f90d4e747b83a0d0cc125dc01567378f82Nuno Lopes    return ME->getMemberDecl();
1153a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu
1154a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu  return 0;
1155a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu}
1156a00425414e8c209cabc25d1826b200aeb94259afZhongxing Xu
1157d20254f2875d0004c57ee766f258dbcee29f4841Nuno LopesFunctionDecl *CallExpr::getDirectCallee() {
1158caabf9bf331156e96dacb072385901fdfa057ec1Chris Lattner  return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
1159d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes}
1160d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes
1161d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// setNumArgs - This changes the number of arguments present in this call.
1162d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// Any orphaned expressions are deleted by this, and any new operands are set
1163d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner/// to null.
116405ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
1165d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // No change, just return.
1166d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs == getNumArgs()) return;
11671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1168d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // If shrinking # arguments, just delete the extras and forgot them.
1169d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  if (NumArgs < getNumArgs()) {
1170d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    this->NumArgs = NumArgs;
1171d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    return;
1172d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  }
1173d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
1174d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Otherwise, we are growing the # arguments.  New an bigger argument array.
1175cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  unsigned NumPreArgs = getNumPreArgs();
1176cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
1177d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Copy over args.
1178cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
1179d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = SubExprs[i];
1180d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  // Null out new args.
1181cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1182cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne       i != NumArgs+PREARGS_START+NumPreArgs; ++i)
1183d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner    NewSubExprs[i] = 0;
11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
118588c9a46f0b84f1ee83e01917825346551ee540d0Douglas Gregor  if (SubExprs) C.Deallocate(SubExprs);
1186d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  SubExprs = NewSubExprs;
1187d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner  this->NumArgs = NumArgs;
1188d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner}
1189d18b3299debb7b0dbd9d34d9369189dc98c87f53Chris Lattner
1190cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// isBuiltinCall - If this is a call to a builtin, return the builtin ID.  If
1191cb888967400a03504c88acedd5248d6778a82f46Chris Lattner/// not, return 0.
1192180f47959a066795cc0f409433023af448bb0328Richard Smithunsigned CallExpr::isBuiltinCall() const {
1193c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // All simple function calls (e.g. func()) are implicitly cast to pointer to
11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // function. As a result, we try and obtain the DeclRefExpr from the
1195c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  // ImplicitCastExpr.
1196c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1197c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
1198cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1200c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1201c4f8e8b645b8e0e685c089dde0674c6f29a24168Steve Naroff  if (!DRE)
1202cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1204bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1205bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson  if (!FDecl)
1206cb888967400a03504c88acedd5248d6778a82f46Chris Lattner    return 0;
12071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12084fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor  if (!FDecl->getIdentifier())
12094fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor    return 0;
12104fcd399a52ae45ed8ebfdd3a25e01cfb76fa366dDouglas Gregor
12117814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  return FDecl->getBuiltinID();
1212cb888967400a03504c88acedd5248d6778a82f46Chris Lattner}
1213bcba201a1118d7852b8b97187d495ae2a4f49519Anders Carlsson
1214ba57183965f117279342903edec19766e478c9a8Richard Smithbool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1215ba57183965f117279342903edec19766e478c9a8Richard Smith  if (unsigned BI = isBuiltinCall())
1216ba57183965f117279342903edec19766e478c9a8Richard Smith    return Ctx.BuiltinInfo.isUnevaluated(BI);
1217ba57183965f117279342903edec19766e478c9a8Richard Smith  return false;
1218ba57183965f117279342903edec19766e478c9a8Richard Smith}
1219ba57183965f117279342903edec19766e478c9a8Richard Smith
12206dde78f744382a5627a04f984a97049e0c4b5e73Anders CarlssonQualType CallExpr::getCallReturnType() const {
12216dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson  QualType CalleeType = getCallee()->getType();
12226217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
12236dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson    CalleeType = FnTypePtr->getPointeeType();
12246217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
12256dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson    CalleeType = BPT->getPointeeType();
1226864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1227864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    // This should never be overloaded and so should never return null.
1228864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    CalleeType = Expr::findBoundMemberType(getCallee());
12295291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
1230864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  const FunctionType *FnType = CalleeType->castAs<FunctionType>();
12316dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson  return FnType->getResultType();
12326dde78f744382a5627a04f984a97049e0c4b5e73Anders Carlsson}
1233cb888967400a03504c88acedd5248d6778a82f46Chris Lattner
12348fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel DunbarSourceLocation CallExpr::getLocStart() const {
12358fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  if (isa<CXXOperatorCallExpr>(this))
123665d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen    return cast<CXXOperatorCallExpr>(this)->getLocStart();
12378fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar
12388fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  SourceLocation begin = getCallee()->getLocStart();
12398fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  if (begin.isInvalid() && getNumArgs() > 0)
12408fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar    begin = getArg(0)->getLocStart();
12418fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  return begin;
12428fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar}
12438fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel DunbarSourceLocation CallExpr::getLocEnd() const {
12448fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  if (isa<CXXOperatorCallExpr>(this))
124565d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen    return cast<CXXOperatorCallExpr>(this)->getLocEnd();
12468fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar
12478fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  SourceLocation end = getRParenLoc();
12488fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  if (end.isInvalid() && getNumArgs() > 0)
12498fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar    end = getArg(getNumArgs() - 1)->getLocEnd();
12508fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar  return end;
12518fbc6d23d61213750ba1fdcb0b4b9d3d54bbc32fDaniel Dunbar}
12522882eca5a184c78f793188083f6ce539740a5cf2John McCall
125305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperOffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
12548ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   SourceLocation OperatorLoc,
1255c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                   TypeSourceInfo *tsi,
12563b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                   ArrayRef<OffsetOfNode> comps,
12573b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                   ArrayRef<Expr*> exprs,
12588ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                   SourceLocation RParenLoc) {
12598ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
12603b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                         sizeof(OffsetOfNode) * comps.size() +
12613b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                         sizeof(Expr*) * exprs.size());
12628ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
12633b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
12643b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                RParenLoc);
12658ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
12668ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
126705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperOffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
12688ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                                        unsigned numComps, unsigned numExprs) {
12698ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
12708ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(OffsetOfNode) * numComps +
12718ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                         sizeof(Expr*) * numExprs);
12728ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return new (Mem) OffsetOfExpr(numComps, numExprs);
12738ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
12748ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
127505ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperOffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
12768ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           SourceLocation OperatorLoc, TypeSourceInfo *tsi,
12773b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                           ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
12788ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor                           SourceLocation RParenLoc)
1279f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1280f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall         /*TypeDependent=*/false,
1281bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*ValueDependent=*/tsi->getType()->isDependentType(),
1282561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         tsi->getType()->isInstantiationDependentType(),
1283bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         tsi->getType()->containsUnexpandedParameterPack()),
1284c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
12853b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumComps(comps.size()), NumExprs(exprs.size())
12868ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor{
12873b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != comps.size(); ++i) {
12883b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    setComponent(i, comps[i]);
12898ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
1290c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
12913b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != exprs.size(); ++i) {
12923b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
1293bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
12943b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    if (exprs[i]->containsUnexpandedParameterPack())
1295bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
1296bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
12973b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    setIndexExpr(i, exprs[i]);
12988ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
12998ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
13008ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
13018ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas GregorIdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
13028ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  assert(getKind() == Field || getKind() == Identifier);
13038ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  if (getKind() == Field)
13048ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    return getField()->getIdentifier();
1305c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
13068ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
13078ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
13088ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
130905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperMemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
131040d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                               NestedNameSpecifierLoc QualifierLoc,
1311e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                               SourceLocation TemplateKWLoc,
1312f595cc41c4d95fe323f8a2b209523de9956f874dEli Friedman                               ValueDecl *memberdecl,
1313161755a09898c95d21bfff33707da9ca41cd53c5John McCall                               DeclAccessPair founddecl,
13142577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                               DeclarationNameInfo nameinfo,
1315d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                               const TemplateArgumentListInfo *targs,
1316f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                               QualType ty,
1317f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                               ExprValueKind vk,
1318f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                               ExprObjectKind ok) {
131983f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  std::size_t Size = sizeof(MemberExpr);
13206bb8017bb9e828d118e15e59d71c66bba323c364John McCall
132140d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor  bool hasQualOrFound = (QualifierLoc ||
1322161755a09898c95d21bfff33707da9ca41cd53c5John McCall                         founddecl.getDecl() != memberdecl ||
1323161755a09898c95d21bfff33707da9ca41cd53c5John McCall                         founddecl.getAccess() != memberdecl->getAccess());
13246bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (hasQualOrFound)
13256bb8017bb9e828d118e15e59d71c66bba323c364John McCall    Size += sizeof(MemberNameQualifier);
13261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1327d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (targs)
1328e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1329e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  else if (TemplateKWLoc.isValid())
1330e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
13311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13323248854a5d16e1de17c58e05f726bdef9f042df2Chris Lattner  void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
1333f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1334f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                       ty, vk, ok);
13356bb8017bb9e828d118e15e59d71c66bba323c364John McCall
13366bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (hasQualOrFound) {
133740d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    // FIXME: Wrong. We should be looking at the member declaration we found.
133840d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
13396bb8017bb9e828d118e15e59d71c66bba323c364John McCall      E->setValueDependent(true);
13406bb8017bb9e828d118e15e59d71c66bba323c364John McCall      E->setTypeDependent(true);
1341561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
1342561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
1343561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    else if (QualifierLoc &&
1344561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor             QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1345561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
1346561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
13476bb8017bb9e828d118e15e59d71c66bba323c364John McCall    E->HasQualifierOrFoundDecl = true;
13486bb8017bb9e828d118e15e59d71c66bba323c364John McCall
13496bb8017bb9e828d118e15e59d71c66bba323c364John McCall    MemberNameQualifier *NQ = E->getMemberQualifier();
135040d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor    NQ->QualifierLoc = QualifierLoc;
13516bb8017bb9e828d118e15e59d71c66bba323c364John McCall    NQ->FoundDecl = founddecl;
13526bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
13536bb8017bb9e828d118e15e59d71c66bba323c364John McCall
1354e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1355e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
13566bb8017bb9e828d118e15e59d71c66bba323c364John McCall  if (targs) {
1357561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool Dependent = false;
1358561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool InstantiationDependent = false;
1359561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    bool ContainsUnexpandedParameterPack = false;
1360e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1361e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                  Dependent,
1362e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                  InstantiationDependent,
1363e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                             ContainsUnexpandedParameterPack);
1364561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (InstantiationDependent)
1365561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      E->setInstantiationDependent(true);
1366e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara  } else if (TemplateKWLoc.isValid()) {
1367e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
13686bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
13696bb8017bb9e828d118e15e59d71c66bba323c364John McCall
13706bb8017bb9e828d118e15e59d71c66bba323c364John McCall  return E;
137183f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor}
137283f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor
1373396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel DunbarSourceLocation MemberExpr::getLocStart() const {
137475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  if (isImplicitAccess()) {
137575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (hasQualifier())
1376396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar      return getQualifierLoc().getBeginLoc();
1377396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar    return MemberLoc;
137875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  }
1379e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
1380396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  // FIXME: We don't want this to happen. Rather, we should be able to
1381396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  // detect all kinds of implicit accesses more cleanly.
1382396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  SourceLocation BaseStartLoc = getBase()->getLocStart();
1383396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  if (BaseStartLoc.isValid())
1384396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar    return BaseStartLoc;
1385396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  return MemberLoc;
1386396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar}
1387396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel DunbarSourceLocation MemberExpr::getLocEnd() const {
138813fd684ee2c7578177b7c741e8eeec91ae628fcfAbramo Bagnara  SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
1389396ec676b7a39665fa3a3f86f8e0520f8d7e93a8Daniel Dunbar  if (hasExplicitTemplateArgs())
139013fd684ee2c7578177b7c741e8eeec91ae628fcfAbramo Bagnara    EndLoc = getRAngleLoc();
139113fd684ee2c7578177b7c741e8eeec91ae628fcfAbramo Bagnara  else if (EndLoc.isInvalid())
139213fd684ee2c7578177b7c741e8eeec91ae628fcfAbramo Bagnara    EndLoc = getBase()->getLocEnd();
139313fd684ee2c7578177b7c741e8eeec91ae628fcfAbramo Bagnara  return EndLoc;
139475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor}
139575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
13961d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCallvoid CastExpr::CheckCastConsistency() const {
13971d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  switch (getCastKind()) {
13981d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_DerivedToBase:
13991d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_UncheckedDerivedToBase:
14001d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_DerivedToBaseMemberPointer:
14011d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BaseToDerived:
14021d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BaseToDerivedMemberPointer:
14031d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(!path_empty() && "Cast kind should have a base path!");
14041d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    break;
14051d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
14061d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
14071d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isObjCObjectPointerType());
14081d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isPointerType());
14091d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
14101d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
14111d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
14121d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isObjCObjectPointerType());
14131d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isBlockPointerType());
14141d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
14151d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
14164d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
14174d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    assert(getType()->isMemberPointerType());
14184d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    assert(getSubExpr()->getType()->isMemberPointerType());
14194d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    goto CheckNoBasePath;
14204d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
14211d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BitCast:
14221d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // Arbitrary casts to C pointer types count as bitcasts.
14231d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // Otherwise, we should only have block and ObjC pointer casts
14241d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    // here if they stay within the type kind.
14251d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    if (!getType()->isPointerType()) {
14261d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      assert(getType()->isObjCObjectPointerType() ==
14271d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall             getSubExpr()->getType()->isObjCObjectPointerType());
14281d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall      assert(getType()->isBlockPointerType() ==
14291d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall             getSubExpr()->getType()->isBlockPointerType());
14301d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    }
14311d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
14321d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
14331d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_AnyPointerToBlockPointerCast:
14341d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getType()->isBlockPointerType());
14351d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(getSubExpr()->getType()->isAnyPointerType() &&
14361d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall           !getSubExpr()->getType()->isBlockPointerType());
14371d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
14381d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
1439ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
1440ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor    assert(getType()->isBlockPointerType());
1441ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor    assert(getSubExpr()->getType()->isBlockPointerType());
1442ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor    goto CheckNoBasePath;
1443a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman
1444a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  case CK_FunctionToPointerDecay:
1445a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman    assert(getType()->isPointerType());
1446a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman    assert(getSubExpr()->getType()->isFunctionType());
1447a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman    goto CheckNoBasePath;
1448a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman
14491d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  // These should not have an inheritance path.
14501d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_Dynamic:
14511d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ToUnion:
14521d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ArrayToPointerDecay:
14531d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NullToMemberPointer:
14541d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NullToPointer:
14551d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ConstructorConversion:
14561d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToPointer:
14571d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_PointerToIntegral:
14581d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ToVoid:
14591d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_VectorSplat:
14601d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralCast:
14611d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToFloating:
14621d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingToIntegral:
14631d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingCast:
14641d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_ObjCObjectLValueCast:
14651d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingRealToComplex:
14661d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToReal:
14671d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexCast:
14681d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToIntegralComplex:
14691d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralRealToComplex:
14701d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToReal:
14711d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexCast:
14721d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToFloatingComplex:
147333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
147433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
147533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
147633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
1477e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case CK_ZeroToOCLEvent:
14781d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(!getType()->isBooleanType() && "unheralded conversion to bool");
14791d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    goto CheckNoBasePath;
14801d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
14811d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_Dependent:
14821d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_LValueToRValue:
14831d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_NoOp:
14847a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
14857a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
14861d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_PointerToBoolean:
14871d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralToBoolean:
14881d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingToBoolean:
14891d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_MemberPointerToBoolean:
14901d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_FloatingComplexToBoolean:
14911d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_IntegralComplexToBoolean:
14921d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_LValueBitCast:            // -> bool&
14931d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_UserDefinedConversion:    // operator bool()
1494a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  case CK_BuiltinFnToFnPtr:
14951d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  CheckNoBasePath:
14961d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    assert(path_empty() && "Cast kind should not have a base path!");
14971d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    break;
14981d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  }
14991d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall}
15001d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall
1501f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlssonconst char *CastExpr::getCastKindName() const {
1502f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  switch (getCastKind()) {
1503daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_Dependent:
1504daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "Dependent";
15052de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BitCast:
1506f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "BitCast";
15072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_LValueBitCast:
1508e39a3894513349908cdb3beba2614e53cb288e6cDouglas Gregor    return "LValueBitCast";
15090ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_LValueToRValue:
15100ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall    return "LValueToRValue";
15112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NoOp:
1512f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "NoOp";
15132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerived:
151411de6de25a0110cd7be97eef761ef3b189781da6Anders Carlsson    return "BaseToDerived";
15152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBase:
1516f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "DerivedToBase";
15172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UncheckedDerivedToBase:
151823cba801e11b03929c44f8cf54578305963a3476John McCall    return "UncheckedDerivedToBase";
15192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_Dynamic:
1520f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "Dynamic";
15212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToUnion:
1522f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ToUnion";
15232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ArrayToPointerDecay:
1524f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ArrayToPointerDecay";
15252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FunctionToPointerDecay:
1526f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "FunctionToPointerDecay";
15272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NullToMemberPointer:
1528f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "NullToMemberPointer";
1529404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall  case CK_NullToPointer:
1530404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall    return "NullToPointer";
15312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerivedMemberPointer:
1532f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "BaseToDerivedMemberPointer";
15332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBaseMemberPointer:
15341a31a18db9d657751f38c724adc0d62e86852bd7Anders Carlsson    return "DerivedToBaseMemberPointer";
15354d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
15364d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    return "ReinterpretMemberPointer";
15372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UserDefinedConversion:
1538f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "UserDefinedConversion";
15392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ConstructorConversion:
1540f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson    return "ConstructorConversion";
15412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToPointer:
15427f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson    return "IntegralToPointer";
15432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_PointerToIntegral:
15447f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson    return "PointerToIntegral";
1545daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_PointerToBoolean:
1546daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "PointerToBoolean";
15472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToVoid:
1548ebeaf2031c968143c531bfe232d7507f20c57347Anders Carlsson    return "ToVoid";
15492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_VectorSplat:
155016a8904f3f5ed19158657e1da95e5902fbee66f7Anders Carlsson    return "VectorSplat";
15512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralCast:
155282debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "IntegralCast";
1553daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_IntegralToBoolean:
1554daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "IntegralToBoolean";
15552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToFloating:
155682debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "IntegralToFloating";
15572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FloatingToIntegral:
155882debc7d282e723e58d183bfa89ddc2500a8daafAnders Carlsson    return "FloatingToIntegral";
15592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FloatingCast:
1560c6b29163557d02da5d2a4a06f986f0480291f51fBenjamin Kramer    return "FloatingCast";
1561daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case CK_FloatingToBoolean:
1562daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return "FloatingToBoolean";
15632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_MemberPointerToBoolean:
1564bc0e0781da778bd5eb41a810419912893ae20448Anders Carlsson    return "MemberPointerToBoolean";
15651d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
15661d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return "CPointerToObjCPointerCast";
15671d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
15681d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return "BlockPointerToObjCPointerCast";
15692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_AnyPointerToBlockPointerCast:
15703b27f1a80e4e433b503efd344c909eeafaa9033cFariborz Jahanian    return "AnyPointerToBlockPointerCast";
15712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ObjCObjectLValueCast:
1572569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    return "ObjCObjectLValueCast";
15732bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_FloatingRealToComplex:
15742bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "FloatingRealToComplex";
1575f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToReal:
1576f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToReal";
1577f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToBoolean:
1578f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToBoolean";
15792bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_FloatingComplexCast:
15802bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "FloatingComplexCast";
1581f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_FloatingComplexToIntegralComplex:
1582f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "FloatingComplexToIntegralComplex";
15832bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_IntegralRealToComplex:
15842bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "IntegralRealToComplex";
1585f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToReal:
1586f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToReal";
1587f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToBoolean:
1588f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToBoolean";
15892bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  case CK_IntegralComplexCast:
15902bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    return "IntegralComplexCast";
1591f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  case CK_IntegralComplexToFloatingComplex:
1592f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return "IntegralComplexToFloatingComplex";
159333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
159433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCConsumeObject";
159533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
159633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCProduceObject";
159733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
159833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCReclaimReturnedObject";
159933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
160033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    return "ARCCExtendBlockObject";
16017a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
16027a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    return "AtomicToNonAtomic";
16037a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
16047a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    return "NonAtomicToAtomic";
1605ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
1606ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor    return "CopyAndAutoreleaseBlockObject";
1607a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  case CK_BuiltinFnToFnPtr:
1608a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman    return "BuiltinFnToFnPtr";
1609e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case CK_ZeroToOCLEvent:
1610e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    return "ZeroToOCLEvent";
1611f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson  }
16121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16132bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall  llvm_unreachable("Unhandled cast kind!");
1614f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson}
1615f8ec55a104e55961f8666f773dce99bbc628298fAnders Carlsson
16166eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas GregorExpr *CastExpr::getSubExprAsWritten() {
16176eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  Expr *SubExpr = 0;
16186eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  CastExpr *E = this;
16196eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  do {
16206eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    SubExpr = E->getSubExpr();
162103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
162203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    // Skip through reference binding to temporary.
162303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
162403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                  = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
162503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      SubExpr = Materialize->GetTemporaryExpr();
162603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
16276eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Skip any temporary bindings; they're implicit.
16286eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
16296eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = Binder->getSubExpr();
1630c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
16316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Conversions by constructor and conversion functions have a
16326eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // subexpression describing the call; strip it off.
16332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (E->getCastKind() == CK_ConstructorConversion)
16346eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
16352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    else if (E->getCastKind() == CK_UserDefinedConversion)
16366eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
1637c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
16386eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // If the subexpression we're left with is an implicit cast, look
16396eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // through that, too.
1640c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1641c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
16426eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return SubExpr;
16436eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
16446eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1645f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallCXXBaseSpecifier **CastExpr::path_buffer() {
1646f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  switch (getStmtClass()) {
1647f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define ABSTRACT_STMT(x)
1648f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define CASTEXPR(Type, Base) \
1649f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  case Stmt::Type##Class: \
1650f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1651f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#define STMT(Type, Base)
1652f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall#include "clang/AST/StmtNodes.inc"
1653f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  default:
1654f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    llvm_unreachable("non-cast expressions not possible here");
1655f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  }
1656f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1657f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1658f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallvoid CastExpr::setCastPath(const CXXCastPath &Path) {
1659f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  assert(Path.size() == path_size());
1660f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1661f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1662f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
166305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
1664f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                           CastKind Kind, Expr *Operand,
1665f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                           const CXXCastPath *BasePath,
16665baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                           ExprValueKind VK) {
1667f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  unsigned PathSize = (BasePath ? BasePath->size() : 0);
1668f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1669f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1670f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  ImplicitCastExpr *E =
16715baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
1672f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (PathSize) E->setCastPath(*BasePath);
1673f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return E;
1674f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1675f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
167605ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
1677f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                                unsigned PathSize) {
1678f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1679f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1680f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1681f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1682f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
1683f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
168405ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
1685f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                       ExprValueKind VK, CastKind K, Expr *Op,
1686f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       const CXXCastPath *BasePath,
1687f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       TypeSourceInfo *WrittenTy,
1688f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                       SourceLocation L, SourceLocation R) {
1689f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  unsigned PathSize = (BasePath ? BasePath->size() : 0);
1690f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1691f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1692f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CStyleCastExpr *E =
1693f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
1694f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (PathSize) E->setCastPath(*BasePath);
1695f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return E;
1696f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1697f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
169805ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperCStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
169905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                                            unsigned PathSize) {
1700f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  void *Buffer =
1701f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1702f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1703f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
1704f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
17055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
17065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// corresponds to, e.g. "<<=".
17070bea86307eb8c16339315a1e261fc490eb505c5bDavid BlaikieStringRef BinaryOperator::getOpcodeStr(Opcode Op) {
17085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Op) {
17092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_PtrMemD:   return ".*";
17102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_PtrMemI:   return "->*";
17112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:       return "*";
17122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Div:       return "/";
17132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Rem:       return "%";
17142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:       return "+";
17152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:       return "-";
17162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shl:       return "<<";
17172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shr:       return ">>";
17182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LT:        return "<";
17192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_GT:        return ">";
17202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LE:        return "<=";
17212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_GE:        return ">=";
17222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_EQ:        return "==";
17232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_NE:        return "!=";
17242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_And:       return "&";
17252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Xor:       return "^";
17262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Or:        return "|";
17272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LAnd:      return "&&";
17282de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_LOr:       return "||";
17292de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Assign:    return "=";
17302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_MulAssign: return "*=";
17312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_DivAssign: return "/=";
17322de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_RemAssign: return "%=";
17332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_AddAssign: return "+=";
17342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_SubAssign: return "-=";
17352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_ShlAssign: return "<<=";
17362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_ShrAssign: return ">>=";
17372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_AndAssign: return "&=";
17382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_XorAssign: return "^=";
17392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_OrAssign:  return "|=";
17402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Comma:     return ",";
17415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1742baf534875ed0a55c6342636ff3f4602b8ac22b69Douglas Gregor
17433026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid OpCode!");
17445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCallBinaryOperatorKind
1747063daf6e196c51f162e0485478355d8e280eef5cDouglas GregorBinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1748063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  switch (OO) {
1749b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not an overloadable binary operator");
17502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Plus: return BO_Add;
17512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Minus: return BO_Sub;
17522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Star: return BO_Mul;
17532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Slash: return BO_Div;
17542de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Percent: return BO_Rem;
17552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Caret: return BO_Xor;
17562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Amp: return BO_And;
17572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Pipe: return BO_Or;
17582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Equal: return BO_Assign;
17592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Less: return BO_LT;
17602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Greater: return BO_GT;
17612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PlusEqual: return BO_AddAssign;
17622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_MinusEqual: return BO_SubAssign;
17632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_StarEqual: return BO_MulAssign;
17642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_SlashEqual: return BO_DivAssign;
17652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PercentEqual: return BO_RemAssign;
17662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_CaretEqual: return BO_XorAssign;
17672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_AmpEqual: return BO_AndAssign;
17682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PipeEqual: return BO_OrAssign;
17692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessLess: return BO_Shl;
17702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterGreater: return BO_Shr;
17712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessLessEqual: return BO_ShlAssign;
17722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterGreaterEqual: return BO_ShrAssign;
17732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_EqualEqual: return BO_EQ;
17742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_ExclaimEqual: return BO_NE;
17752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_LessEqual: return BO_LE;
17762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_GreaterEqual: return BO_GE;
17772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_AmpAmp: return BO_LAnd;
17782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_PipePipe: return BO_LOr;
17792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_Comma: return BO_Comma;
17802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case OO_ArrowStar: return BO_PtrMemI;
1781063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  }
1782063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor}
1783063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor
1784063daf6e196c51f162e0485478355d8e280eef5cDouglas GregorOverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1785063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  static const OverloadedOperatorKind OverOps[] = {
1786063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1787063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Star, OO_Slash, OO_Percent,
1788063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Plus, OO_Minus,
1789063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_LessLess, OO_GreaterGreater,
1790063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1791063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_EqualEqual, OO_ExclaimEqual,
1792063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Amp,
1793063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Caret,
1794063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Pipe,
1795063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_AmpAmp,
1796063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PipePipe,
1797063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Equal, OO_StarEqual,
1798063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_SlashEqual, OO_PercentEqual,
1799063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PlusEqual, OO_MinusEqual,
1800063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_LessLessEqual, OO_GreaterGreaterEqual,
1801063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_AmpEqual, OO_CaretEqual,
1802063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_PipeEqual,
1803063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor    OO_Comma
1804063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  };
1805063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor  return OverOps[Opc];
1806063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor}
1807063daf6e196c51f162e0485478355d8e280eef5cDouglas Gregor
180805ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperInitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
18093b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                           ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
1810bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1811561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         false, false),
18123b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    InitExprs(C, initExprs.size()),
181323700f083fb72f5c6792e253f203a43aba3cef86Abramo Bagnara    LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
181432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl{
181532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  sawArrayRangeDesignator(false);
18163b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned I = 0; I != initExprs.size(); ++I) {
1817ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    if (initExprs[I]->isTypeDependent())
18188e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall      ExprBits.TypeDependent = true;
1819ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    if (initExprs[I]->isValueDependent())
18208e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall      ExprBits.ValueDependent = true;
1821561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (initExprs[I]->isInstantiationDependent())
1822561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
1823bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (initExprs[I]->containsUnexpandedParameterPack())
1824bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
182573460a32bc5299a5927d23d2e464d72af796eabfDouglas Gregor  }
1826c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
18273b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
182866b5a8a39088598c01a9fa6f032dc908612dc8ecAnders Carlsson}
18295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
183005ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
1831ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  if (NumInits > InitExprs.size())
1832709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs.reserve(C, NumInits);
1833fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor}
1834fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
183505ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
1836709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek  InitExprs.resize(C, NumInits, 0);
18374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
18384c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
183905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperExpr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
1840ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek  if (Init >= InitExprs.size()) {
1841709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
1842ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    InitExprs.back() = expr;
1843ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek    return 0;
18444c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
18451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
18474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitExprs[Init] = expr;
18484c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
18494c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
18504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
18513e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidisvoid InitListExpr::setArrayFiller(Expr *filler) {
185221f77cd0c3da8a1dbaf6245cae43baf4c0b80ea4Argyrios Kyrtzidis  assert(!hasArrayFiller() && "Filler already set!");
18533e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  ArrayFillerOrUnionFieldInit = filler;
18543e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  // Fill out any "holes" in the array due to designated initializers.
18553e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  Expr **inits = getInits();
18563e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis  for (unsigned i = 0, e = getNumInits(); i != e; ++i)
18573e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis    if (inits[i] == 0)
18583e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis      inits[i] = filler;
18593e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis}
18603e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis
1861fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smithbool InitListExpr::isStringLiteralInit() const {
1862fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith  if (getNumInits() != 1)
1863fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith    return false;
1864f0a26499c0ef62c7940b596be092315922a46ab7Eli Friedman  const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1865f0a26499c0ef62c7940b596be092315922a46ab7Eli Friedman  if (!AT || !AT->getElementType()->isIntegerType())
1866fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith    return false;
1867f0a26499c0ef62c7940b596be092315922a46ab7Eli Friedman  const Expr *Init = getInit(0)->IgnoreParens();
1868fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith  return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1869fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith}
1870fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith
187165d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik VerbruggenSourceLocation InitListExpr::getLocStart() const {
187223700f083fb72f5c6792e253f203a43aba3cef86Abramo Bagnara  if (InitListExpr *SyntacticForm = getSyntacticForm())
187365d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen    return SyntacticForm->getLocStart();
187465d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  SourceLocation Beg = LBraceLoc;
1875c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (Beg.isInvalid()) {
1876c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    // Find the first non-null initializer.
1877c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    for (InitExprsTy::const_iterator I = InitExprs.begin(),
1878c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek                                     E = InitExprs.end();
1879c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      I != E; ++I) {
1880c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      if (Stmt *S = *I) {
1881c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        Beg = S->getLocStart();
1882c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        break;
1883c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      }
1884c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    }
1885c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  }
188665d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  return Beg;
188765d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen}
188865d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen
188965d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik VerbruggenSourceLocation InitListExpr::getLocEnd() const {
189065d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  if (InitListExpr *SyntacticForm = getSyntacticForm())
189165d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen    return SyntacticForm->getLocEnd();
189265d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  SourceLocation End = RBraceLoc;
1893c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  if (End.isInvalid()) {
1894c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    // Find the first non-null initializer from the end.
1895c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
189665d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen         E = InitExprs.rend();
189765d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen         I != E; ++I) {
1898c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek      if (Stmt *S = *I) {
189965d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen        End = S->getLocEnd();
1900c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek        break;
190165d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen      }
1902c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek    }
1903c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek  }
190465d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  return End;
1905c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek}
1906c4ba51f365a3cd3374b3ef87272a9b3e517cd5d3Ted Kremenek
1907bfdcae678d44906293e21c0cddc6537f3ee8b5a4Steve Naroff/// getFunctionType - Return the underlying function type for this block.
19084eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
1909a345edb668380691fc92d4e4aa0a5ffec366ca6aJohn McCallconst FunctionProtoType *BlockExpr::getFunctionType() const {
1910a345edb668380691fc92d4e4aa0a5ffec366ca6aJohn McCall  // The block pointer is never sugared, but the function type might be.
1911a345edb668380691fc92d4e4aa0a5ffec366ca6aJohn McCall  return cast<BlockPointerType>(getType())
1912a345edb668380691fc92d4e4aa0a5ffec366ca6aJohn McCall           ->getPointeeType()->castAs<FunctionProtoType>();
19134eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
19144eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
19151eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSourceLocation BlockExpr::getCaretLocation() const {
19161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getCaretLocation();
191756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
19181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst Stmt *BlockExpr::getBody() const {
19197297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor  return TheBlock->getBody();
19207297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
19211eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpStmt *BlockExpr::getBody() {
19221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TheBlock->getBody();
19237297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor}
192456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
192556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
19265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
19275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Generic Expression Routines
19285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
19295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1930026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// isUnusedResultAWarning - Return true if this immediate expression should
1931026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// be warned about if the result is unused.  If so, fill in Loc and Ranges
1932026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// with location to warn on and the source range[s] to report with the
1933026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner/// warning.
1934a6115068cde719142eb394db88612c185cabd05bEli Friedmanbool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1935a6115068cde719142eb394db88612c185cabd05bEli Friedman                                  SourceRange &R1, SourceRange &R2,
1936a6115068cde719142eb394db88612c185cabd05bEli Friedman                                  ASTContext &Ctx) const {
1937ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // Don't warn if the expr is type dependent. The type could end up
1938ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  // instantiating to void.
1939ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson  if (isTypeDependent())
1940ffce2df6ae280d354d51371282a579df1eb86876Anders Carlsson    return false;
19411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getStmtClass()) {
19435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
19440faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
19450faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
1946a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
1947026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = getExprLoc();
1948026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
1949026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
19505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ParenExprClass:
1951026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return cast<ParenExpr>(this)->getSubExpr()->
1952a6115068cde719142eb394db88612c185cabd05bEli Friedman      isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
1953f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
1954f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()->
1955a6115068cde719142eb394db88612c185cabd05bEli Friedman      isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
1956a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman  case ChooseExprClass:
1957a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    return cast<ChooseExpr>(this)->getChosenSubExpr()->
1958a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
19595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UnaryOperatorClass: {
19605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const UnaryOperator *UO = cast<UnaryOperator>(this);
19611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (UO->getOpcode()) {
1963a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_Plus:
1964a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_Minus:
1965a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_AddrOf:
1966a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_Not:
1967a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_LNot:
1968a6115068cde719142eb394db88612c185cabd05bEli Friedman    case UO_Deref:
1969a6115068cde719142eb394db88612c185cabd05bEli Friedman      break;
19702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostInc:
19712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostDec:
19722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreInc:
19732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreDec:                 // ++/--
1974026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;  // Not a warning.
19752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Real:
19762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Imag:
19775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // accessing a piece of a volatile complex is a side-effect.
1978df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump      if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1979df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump          .isVolatileQualified())
1980026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner        return false;
1981026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      break;
19822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
1983a6115068cde719142eb394db88612c185cabd05bEli Friedman      return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
19845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1985a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
1986026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = UO->getOperatorLoc();
1987026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = UO->getSubExpr()->getSourceRange();
1988026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
19895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1990e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  case BinaryOperatorClass: {
1991026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const BinaryOperator *BO = cast<BinaryOperator>(this);
1992c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek    switch (BO->getOpcode()) {
1993c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek      default:
1994c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        break;
199525973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider the RHS of comma for side effects. LHS was checked by
199625973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Sema::CheckCommaOperands.
19972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_Comma:
1998c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // ((foo = <blah>), 0) is an idiom for hiding the result (and
1999c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        // lvalue-ness) of an assignment written in a macro.
2000c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek        if (IntegerLiteral *IE =
2001c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek              dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2002c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek          if (IE->getValue() == 0)
2003c46a246f5fb00bf8448a1081e7d7e73bb6dbfbf5Ted Kremenek            return false;
2004a6115068cde719142eb394db88612c185cabd05bEli Friedman        return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
200525973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis      // Consider '||', '&&' to have side effects if the LHS or RHS does.
20062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LAnd:
20072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      case BO_LOr:
2008a6115068cde719142eb394db88612c185cabd05bEli Friedman        if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2009a6115068cde719142eb394db88612c185cabd05bEli Friedman            !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
201025973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis          return false;
201125973455aed1cdc9c40b208c792b5db4f8f1297dArgyrios Kyrtzidis        break;
2012bf0ee354163f87623a4b60412544243911332343John McCall    }
2013026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    if (BO->isAssignmentOp())
2014026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return false;
2015a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
2016026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = BO->getOperatorLoc();
2017026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = BO->getLHS()->getSourceRange();
2018026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = BO->getRHS()->getSourceRange();
2019026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
2020e7716e6133e23e4a89248a65a388bc840d8c130cChris Lattner  }
2021eb14fe839ec24c2ca14e5f094be147a34e3d3339Chris Lattner  case CompoundAssignOperatorClass:
2022c6dfe194f623b02c123759f235b504d4850fc178Douglas Gregor  case VAArgExprClass:
2023276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  case AtomicExprClass:
2024026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
20255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2026ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  case ConditionalOperatorClass: {
2027fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // If only one of the LHS or RHS is a warning, the operator might
2028fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // be being used for control flow. Only warn if both the LHS and
2029fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    // RHS are warnings.
2030ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian    const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
2031a6115068cde719142eb394db88612c185cabd05bEli Friedman    if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
2032fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek      return false;
2033fb7cb35fa0a8131853b1b049ca7be77980e144f5Ted Kremenek    if (!Exp->getLHS())
2034026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      return true;
2035a6115068cde719142eb394db88612c185cabd05bEli Friedman    return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2036ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian  }
2037ab38e4b50268633f037a10841fdfb612513f8d33Fariborz Jahanian
20385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case MemberExprClass:
2039a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
2040026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<MemberExpr>(this)->getMemberLoc();
2041026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = SourceRange(Loc, Loc);
2042026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2043026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
20441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ArraySubscriptExprClass:
2046a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
2047026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2048026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2049026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2050026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
2051211f6adf1301a1461015fb6cb08a05f0a35b65f3Eli Friedman
20529b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth  case CXXOperatorCallExprClass: {
20539b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // We warn about operator== and operator!= even when user-defined operator
20549b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // overloads as there is no reasonable way to define these such that they
20559b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // have non-trivial, desirable side-effects. See the -Wunused-comparison
20569b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // warning: these operators are commonly typo'ed, and so warning on them
20579b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // provides additional value as well. If this list is updated,
20589b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // DiagnoseUnusedComparison should be as well.
20599b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
20609b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    if (Op->getOperator() == OO_EqualEqual ||
20616e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay        Op->getOperator() == OO_ExclaimEqual) {
2062a6115068cde719142eb394db88612c185cabd05bEli Friedman      WarnE = this;
20636e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay      Loc = Op->getOperatorLoc();
20646e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay      R1 = Op->getSourceRange();
20659b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth      return true;
20666e5218367c513fe02d1c7210023d40739ecb1572Matt Beaumont-Gay    }
20679b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth
20689b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth    // Fallthrough for generic call handling.
20699b10683a3f222d8dc6e139073ca96b97b439747bChandler Carruth  }
20705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case CallExprClass:
20719fcce65e7e1307b5b8da9be13e4092d6bb94dc1dRichard Smith  case CXXMemberCallExprClass:
20729fcce65e7e1307b5b8da9be13e4092d6bb94dc1dRichard Smith  case UserDefinedLiteralClass: {
2073026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // If this is a direct call, get the callee.
2074026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    const CallExpr *CE = cast<CallExpr>(this);
2075d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    if (const Decl *FD = CE->getCalleeDecl()) {
2076026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // If the callee has attribute pure, const, or warn_unused_result, warn
2077026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      // about it. void foo() { strlen("bar"); } should warn.
2078bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      //
2079bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2080bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      // updated to match for QoI.
2081bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<WarnUnusedResultAttr>() ||
2082bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
2083a6115068cde719142eb394db88612c185cabd05bEli Friedman        WarnE = this;
2084bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Loc = CE->getCallee()->getLocStart();
2085bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        R1 = CE->getCallee()->getSourceRange();
2086bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner
2087bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        if (unsigned NumArgs = CE->getNumArgs())
2088bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner          R2 = SourceRange(CE->getArg(0)->getLocStart(),
2089bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner                           CE->getArg(NumArgs-1)->getLocEnd());
2090bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return true;
2091bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
2092026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    }
2093026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
2094026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner  }
209558beed91d468863b8c85bce43425422703838d27Anders Carlsson
209684c3b9745f813a784b5d8ce77f2785750523d9ebMatt Beaumont-Gay  // If we don't know precisely what we're looking at, let's not warn.
209784c3b9745f813a784b5d8ce77f2785750523d9ebMatt Beaumont-Gay  case UnresolvedLookupExprClass:
209884c3b9745f813a784b5d8ce77f2785750523d9ebMatt Beaumont-Gay  case CXXUnresolvedConstructExprClass:
209984c3b9745f813a784b5d8ce77f2785750523d9ebMatt Beaumont-Gay    return false;
210084c3b9745f813a784b5d8ce77f2785750523d9ebMatt Beaumont-Gay
210158beed91d468863b8c85bce43425422703838d27Anders Carlsson  case CXXTemporaryObjectExprClass:
210281e4549fede62835235a53c024909c3fab6b73acLubos Lunak  case CXXConstructExprClass: {
210381e4549fede62835235a53c024909c3fab6b73acLubos Lunak    if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
210481e4549fede62835235a53c024909c3fab6b73acLubos Lunak      if (Type->hasAttr<WarnUnusedAttr>()) {
210581e4549fede62835235a53c024909c3fab6b73acLubos Lunak        WarnE = this;
210681e4549fede62835235a53c024909c3fab6b73acLubos Lunak        Loc = getLocStart();
210781e4549fede62835235a53c024909c3fab6b73acLubos Lunak        R1 = getSourceRange();
210881e4549fede62835235a53c024909c3fab6b73acLubos Lunak        return true;
210981e4549fede62835235a53c024909c3fab6b73acLubos Lunak      }
211081e4549fede62835235a53c024909c3fab6b73acLubos Lunak    }
211158beed91d468863b8c85bce43425422703838d27Anders Carlsson    return false;
211281e4549fede62835235a53c024909c3fab6b73acLubos Lunak  }
211358beed91d468863b8c85bce43425422703838d27Anders Carlsson
2114f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  case ObjCMessageExprClass: {
2115f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
21164e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Ctx.getLangOpts().ObjCAutoRefCount &&
2117f85e193739c953358c865005855253af4f68a497John McCall        ME->isInstanceMessage() &&
2118f85e193739c953358c865005855253af4f68a497John McCall        !ME->getType()->isVoidType() &&
21194bdb602d1a70ea432aad909825eb4748a2aca768Jean-Daniel Dupas        ME->getMethodFamily() == OMF_init) {
2120a6115068cde719142eb394db88612c185cabd05bEli Friedman      WarnE = this;
2121f85e193739c953358c865005855253af4f68a497John McCall      Loc = getExprLoc();
2122f85e193739c953358c865005855253af4f68a497John McCall      R1 = ME->getSourceRange();
2123f85e193739c953358c865005855253af4f68a497John McCall      return true;
2124f85e193739c953358c865005855253af4f68a497John McCall    }
2125f85e193739c953358c865005855253af4f68a497John McCall
2126f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMethodDecl *MD = ME->getMethodDecl();
2127f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
2128a6115068cde719142eb394db88612c185cabd05bEli Friedman      WarnE = this;
2129f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      Loc = getExprLoc();
2130f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      return true;
2131f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    }
2132026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
2133f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  }
21341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
213512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  case ObjCPropertyRefExprClass:
2136a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
21375e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    Loc = getExprLoc();
21385e94a0d82b1f49be41c35a73106b219e3f588c8cChris Lattner    R1 = getSourceRange();
2139a50089ec68a583d13718107c1b0c898f0903709eChris Lattner    return true;
214012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
21414b9c2d235fb9449e249d74f48ecfec601650de93John McCall  case PseudoObjectExprClass: {
21424b9c2d235fb9449e249d74f48ecfec601650de93John McCall    const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
21434b9c2d235fb9449e249d74f48ecfec601650de93John McCall
21444b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // Only complain about things that have the form of a getter.
21454b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
21464b9c2d235fb9449e249d74f48ecfec601650de93John McCall        isa<BinaryOperator>(PO->getSyntacticForm()))
21474b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return false;
21484b9c2d235fb9449e249d74f48ecfec601650de93John McCall
2149a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
21504b9c2d235fb9449e249d74f48ecfec601650de93John McCall    Loc = getExprLoc();
21514b9c2d235fb9449e249d74f48ecfec601650de93John McCall    R1 = getSourceRange();
21524b9c2d235fb9449e249d74f48ecfec601650de93John McCall    return true;
21534b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
21544b9c2d235fb9449e249d74f48ecfec601650de93John McCall
2155611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  case StmtExprClass: {
2156611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // Statement exprs don't logically have side effects themselves, but are
2157611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // sometimes used in macros in ways that give them a type that is unused.
2158611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2159611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // however, if the result of the stmt expr is dead, we don't want to emit a
2160611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    // warning.
2161611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner    const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
2162d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    if (!CS->body_empty()) {
2163611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner      if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
2164a6115068cde719142eb394db88612c185cabd05bEli Friedman        return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2165d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis      if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2166d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis        if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
2167a6115068cde719142eb394db88612c185cabd05bEli Friedman          return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2168d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    }
21691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21700faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (getType()->isVoidType())
21710faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
2172a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
2173026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Loc = cast<StmtExpr>(this)->getLParenLoc();
2174026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    R1 = getSourceRange();
2175026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
2176611b2eccaf3869f32de51ecc02985426d1c0aaefChris Lattner  }
21776319917b5021e9602389b49ca4f245d235e9b90aEli Friedman  case CXXFunctionalCastExprClass:
2178a6115068cde719142eb394db88612c185cabd05bEli Friedman  case CStyleCastExprClass: {
21794059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman    // Ignore an explicit cast to void unless the operand is a non-trivial
2180a6115068cde719142eb394db88612c185cabd05bEli Friedman    // volatile lvalue.
21814059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman    const CastExpr *CE = cast<CastExpr>(this);
2182a6115068cde719142eb394db88612c185cabd05bEli Friedman    if (CE->getCastKind() == CK_ToVoid) {
2183a6115068cde719142eb394db88612c185cabd05bEli Friedman      if (CE->getSubExpr()->isGLValue() &&
21844059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman          CE->getSubExpr()->getType().isVolatileQualified()) {
21854059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman        const DeclRefExpr *DRE =
21864059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman            dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
21874059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman        if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
21884059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman              cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
21894059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman          return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
21904059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman                                                          R1, R2, Ctx);
21914059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman        }
21924059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman      }
21930faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return false;
2194a6115068cde719142eb394db88612c185cabd05bEli Friedman    }
21954059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman
2196a6115068cde719142eb394db88612c185cabd05bEli Friedman    // If this is a cast to a constructor conversion, check the operand.
219758beed91d468863b8c85bce43425422703838d27Anders Carlsson    // Otherwise, the result of the cast is unused.
2198a6115068cde719142eb394db88612c185cabd05bEli Friedman    if (CE->getCastKind() == CK_ConstructorConversion)
2199a6115068cde719142eb394db88612c185cabd05bEli Friedman      return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
22004059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman
2201a6115068cde719142eb394db88612c185cabd05bEli Friedman    WarnE = this;
22024059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman    if (const CXXFunctionalCastExpr *CXXCE =
22034059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman            dyn_cast<CXXFunctionalCastExpr>(this)) {
2204cdd4b78583120222b82148626119b3e80ae1d291Eli Friedman      Loc = CXXCE->getLocStart();
22054059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman      R1 = CXXCE->getSubExpr()->getSourceRange();
22064059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman    } else {
22074059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman      const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
22084059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman      Loc = CStyleCE->getLParenLoc();
22094059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman      R1 = CStyleCE->getSubExpr()->getSourceRange();
22104059da87fa2fe9f415c9656dc63e75d5d4a489efEli Friedman    }
2211026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return true;
221258beed91d468863b8c85bce43425422703838d27Anders Carlsson  }
2213a6115068cde719142eb394db88612c185cabd05bEli Friedman  case ImplicitCastExprClass: {
2214a6115068cde719142eb394db88612c185cabd05bEli Friedman    const CastExpr *ICE = cast<ImplicitCastExpr>(this);
2215a6115068cde719142eb394db88612c185cabd05bEli Friedman
2216a6115068cde719142eb394db88612c185cabd05bEli Friedman    // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2217a6115068cde719142eb394db88612c185cabd05bEli Friedman    if (ICE->getCastKind() == CK_LValueToRValue &&
2218a6115068cde719142eb394db88612c185cabd05bEli Friedman        ICE->getSubExpr()->getType().isVolatileQualified())
2219a6115068cde719142eb394db88612c185cabd05bEli Friedman      return false;
22204be1f47de20525ad90f02ba8682a7e2cbd3205d1Eli Friedman
2221a6115068cde719142eb394db88612c185cabd05bEli Friedman    return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2222a6115068cde719142eb394db88612c185cabd05bEli Friedman  }
222304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  case CXXDefaultArgExprClass:
2224df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXDefaultArgExpr>(this)
2225a6115068cde719142eb394db88612c185cabd05bEli Friedman            ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
2226c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  case CXXDefaultInitExprClass:
2227c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    return (cast<CXXDefaultInitExpr>(this)
2228c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith            ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
22294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXNewExprClass:
22314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // FIXME: In theory, there might be new expressions that don't have side
22324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    // effects (e.g. a placement new with an uninitialized POD).
22334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  case CXXDeleteExprClass:
2234026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return false;
22352d46eb21eb2c904831b0e9f75ab3523384c70e66Anders Carlsson  case CXXBindTemporaryExprClass:
2236df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump    return (cast<CXXBindTemporaryExpr>(this)
2237a6115068cde719142eb394db88612c185cabd05bEli Friedman            ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
22384765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case ExprWithCleanupsClass:
22394765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall    return (cast<ExprWithCleanups>(this)
2240a6115068cde719142eb394db88612c185cabd05bEli Friedman            ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
22414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
22425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
224444baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian/// isOBJCGCCandidate - Check if an expression is objc gc'able.
22457f4f86a2167abc116275e49c81350fc3225485e5Fariborz Jahanian/// returns true, if it is; false otherwise.
2246102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanianbool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
2247f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  const Expr *E = IgnoreParens();
2248f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  switch (E->getStmtClass()) {
224944baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  default:
225044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
225144baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ObjCIvarRefExprClass:
225244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return true;
2253207c5210eb0ac7b632609f0c006eb97ef2738948Fariborz Jahanian  case Expr::UnaryOperatorClass:
2254f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
225544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ImplicitCastExprClass:
2256f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
225703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass:
225803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
225903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                                      ->isOBJCGCCandidate(Ctx);
226006b89124a9a5971a0528cc9da6817740bac43164Fariborz Jahanian  case CStyleCastExprClass:
2261f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
2262a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor  case DeclRefExprClass: {
2263f4b88a45902af1802a1cb42ba48b1c474474f228John McCall    const Decl *D = cast<DeclRefExpr>(E)->getDecl();
2264e3f834950801f1334f1b3f3f7e9a34062905fe1dFariborz Jahanian
2265102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2266102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      if (VD->hasGlobalStorage())
2267102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian        return true;
2268102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian      QualType T = VD->getType();
226959a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // dereferencing to a  pointer is always a gc'able candidate,
227059a53fa3f8ea73bae52ea36d0038f76e9f10729cFariborz Jahanian      // unless it is __weak.
22717e88a60d38b36695520e4f8d9279766ef111a662Daniel Dunbar      return T->isPointerType() &&
22720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall             (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
2273102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    }
227444baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian    return false;
227544baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
227683f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor  case MemberExprClass: {
2277f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    const MemberExpr *M = cast<MemberExpr>(E);
2278102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian    return M->getBase()->isOBJCGCCandidate(Ctx);
227944baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
228044baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  case ArraySubscriptExprClass:
2281f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
228244baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  }
228344baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian}
2284369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl
228511ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidisbool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
228611ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis  if (isTypeDependent())
228711ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis    return false;
22887eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
228911ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis}
229011ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis
2291864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCallQualType Expr::findBoundMemberType(const Expr *expr) {
2292e0a22d06888c13989b3f72db319f1d498bf69153John McCall  assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
2293864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
2294864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Bound member expressions are always one of these possibilities:
2295864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  //   x->m      x.m      x->*y      x.*y
2296864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // (possibly parenthesized)
2297864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
2298864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  expr = expr->IgnoreParens();
2299864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2300864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2301864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return mem->getMemberDecl()->getType();
2302864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
2303864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
2304864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2305864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2306864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall                      ->getPointeeType();
2307864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    assert(type->isFunctionType());
2308864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return type;
2309864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
2310864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
2311864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  assert(isa<UnresolvedMemberExpr>(expr));
2312864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  return QualType();
2313864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall}
2314864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
23154e99a5fc3b203397a91136c6e695e405fb8fc606Ted KremenekExpr* Expr::IgnoreParens() {
23164e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek  Expr* E = this;
2317b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara  while (true) {
2318b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2319b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      E = P->getSubExpr();
2320b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2321b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2322b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2323b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if (P->getOpcode() == UO_Extension) {
2324b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        E = P->getSubExpr();
2325b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara        continue;
2326b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      }
2327b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
2328f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2329f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      if (!P->isResultDependent()) {
2330f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        E = P->getResultExpr();
2331f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne        continue;
2332f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      }
2333f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
2334a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2335a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      if (!P->isConditionDependent()) {
2336a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman        E = P->getChosenSubExpr();
2337a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman        continue;
2338a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      }
2339a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    }
2340b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
2341b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara  }
23424e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek}
23434e99a5fc3b203397a91136c6e695e405fb8fc606Ted Kremenek
234456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
234556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner/// or CastExprs or ImplicitCastExprs, returning their operand.
234656f349400c5932a196509c0480ff6f99a9a0b48fChris LattnerExpr *Expr::IgnoreParenCasts() {
234756f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  Expr *E = this;
234856f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  while (true) {
2349a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    E = E->IgnoreParens();
2350b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
235156f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner      E = P->getSubExpr();
2352b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2353b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
235403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
235503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
235603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
235703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
235803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
2359c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2360c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2361c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2362c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2363c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2364b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
236556f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner  }
236656f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner}
236756f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner
23689c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
23699c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// casts.  This is intended purely as a temporary workaround for code
23709c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// that hasn't yet been rewritten to do the right thing about those
23719c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall/// casts, and may disappear along with the last internal use.
2372f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallExpr *Expr::IgnoreParenLValueCasts() {
2373f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  Expr *E = this;
23749c5d70cee1fab3f988f9cd40316071b088a3f19dJohn McCall  while (true) {
2375a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    E = E->IgnoreParens();
2376a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2377f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      if (P->getCastKind() == CK_LValueToRValue) {
2378f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        E = P->getSubExpr();
2379f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        continue;
2380f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
238103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    } else if (MaterializeTemporaryExpr *Materialize
238203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
238303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
238403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
2385c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    } else if (SubstNonTypeTemplateParmExpr *NTTP
2386c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2387c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2388c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2389f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
2390f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
2391f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
2392f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return E;
2393f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
2394632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola
2395632fbaa22fbed7c090eb83775731bfff786c2198Rafael EspindolaExpr *Expr::ignoreParenBaseCasts() {
2396632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola  Expr *E = this;
2397632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola  while (true) {
2398a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    E = E->IgnoreParens();
2399632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola    if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2400632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola      if (CE->getCastKind() == CK_DerivedToBase ||
2401632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola          CE->getCastKind() == CK_UncheckedDerivedToBase ||
2402632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola          CE->getCastKind() == CK_NoOp) {
2403632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola        E = CE->getSubExpr();
2404632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola        continue;
2405632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola      }
2406632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola    }
2407632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola
2408632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola    return E;
2409632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola  }
2410632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola}
2411632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola
24122fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCallExpr *Expr::IgnoreParenImpCasts() {
24132fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  Expr *E = this;
24142fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  while (true) {
2415a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    E = E->IgnoreParens();
2416b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
24172fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall      E = P->getSubExpr();
2418b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      continue;
2419b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    }
242003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (MaterializeTemporaryExpr *Materialize
242103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
242203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = Materialize->GetTemporaryExpr();
242303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
242403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
2425c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2426c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2427c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2428c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2429c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2430b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara    return E;
24312fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall  }
24322fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall}
24332fc46bf1a9bc31d50f82de37c70ea257d3cded27John McCall
24342f072b442879b8bba8c5dea11d7c61bedb1924aeHans WennborgExpr *Expr::IgnoreConversionOperator() {
24352f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
243614d251cd62942bf7d56bb87a267ba2ca2f7fae3eChandler Carruth    if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
24372f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg      return MCE->getImplicitObjectArgument();
24382f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  }
24392f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg  return this;
24402f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg}
24412f072b442879b8bba8c5dea11d7c61bedb1924aeHans Wennborg
2442ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2443ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// value (including ptr->int casts of the same size).  Strip off any
2444ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner/// ParenExpr or CastExprs, returning their operand.
2445ecdd84147c0765caa999ddc22dde25b42712bb4dChris LattnerExpr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2446ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  Expr *E = this;
2447ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  while (true) {
2448a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    E = E->IgnoreParens();
24491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2450ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2451ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      // We ignore integer <-> casts that are of the same width, ptr<->ptr and
24522ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor      // ptr<->int casts of the same width.  We also ignore all identity casts.
2453ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      Expr *SE = P->getSubExpr();
24541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2455ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2456ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
2457ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
2458ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
24591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2460b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara      if ((E->getType()->isPointerType() ||
24619d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           E->getType()->isIntegralType(Ctx)) &&
2462b9eb35ced8369c8c8479efc17712faaf34e16c56Abramo Bagnara          (SE->getType()->isPointerType() ||
24639d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor           SE->getType()->isIntegralType(Ctx)) &&
2464ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner          Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2465ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        E = SE;
2466ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner        continue;
2467ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner      }
2468ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    }
24691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2470c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    if (SubstNonTypeTemplateParmExpr *NTTP
2471c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor                                  = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2472c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      E = NTTP->getReplacement();
2473c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor      continue;
2474c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor    }
2475c0244c5c351db18c6c35c1594872dde072b90df9Douglas Gregor
2476ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner    return E;
2477ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner  }
2478ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner}
2479ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
24806eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorbool Expr::isDefaultArgument() const {
24816eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  const Expr *E = this;
248203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
248303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = M->GetTemporaryExpr();
248403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
24856eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
24866eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    E = ICE->getSubExprAsWritten();
2487c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
24886eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return isa<CXXDefaultArgExpr>(E);
24896eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2490ecdd84147c0765caa999ddc22dde25b42712bb4dChris Lattner
24912f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// \brief Skip over any no-op casts and any temporary-binding
24922f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor/// expressions.
2493f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlssonstatic const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
249403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
249503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = M->GetTemporaryExpr();
249603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
24972f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
24982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
24992f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
25002f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
25012f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
25022f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
25032f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
25042f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
25052f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    E = BE->getSubExpr();
25062f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
25072f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
25082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (ICE->getCastKind() == CK_NoOp)
25092f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      E = ICE->getSubExpr();
25102f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    else
25112f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      break;
25122f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
2513f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson
2514f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson  return E->IgnoreParens();
25152f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
25162f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
2517558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// isTemporaryObject - Determines if this expression produces a
2518558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall/// temporary of the given class type.
2519558d2abc7f9fd6801cc7677200992313ae90b5d8John McCallbool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2520558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2521558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    return false;
25222f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
2523f8b3015060096a946d91578d0f3b65d3097a4ebbAnders Carlsson  const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
25242f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
252558277b57f9492d0234748be89bcad48b322c5cf7John McCall  // Temporaries are by definition pr-values of class type.
2526db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian  if (!E->Classify(C).isPRValue()) {
2527db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian    // In this context, property reference is a message call and is pr-value.
252812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    if (!isa<ObjCPropertyRefExpr>(E))
2529db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian      return false;
2530db148be93c9af45da1f3aa9302c577618a56e6eaFariborz Jahanian  }
25312f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
253219e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // Black-list a few cases which yield pr-values of class type that don't
253319e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // refer to temporaries of that type:
253419e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall
253519e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // - implicit derived-to-base conversions
2536558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  if (isa<ImplicitCastExpr>(E)) {
2537558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2538558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_DerivedToBase:
2539558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    case CK_UncheckedDerivedToBase:
2540558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      return false;
2541558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    default:
2542558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      break;
2543558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    }
25442f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  }
25452f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
254619e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  // - member expressions (all)
254719e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall  if (isa<MemberExpr>(E))
254819e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall    return false;
254919e60ad937af50843a653fa19c8be6a83a64f24fJohn McCall
255032f498a675df990901e6659d610dc740f9423228Eli Friedman  if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
255132f498a675df990901e6659d610dc740f9423228Eli Friedman    if (BO->isPtrMemOp())
255232f498a675df990901e6659d610dc740f9423228Eli Friedman      return false;
255332f498a675df990901e6659d610dc740f9423228Eli Friedman
255456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // - opaque values (all)
255556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (isa<OpaqueValueExpr>(E))
255656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return false;
255756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
2558558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  return true;
25592f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor}
25602f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
256175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregorbool Expr::isImplicitCXXThis() const {
256275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  const Expr *E = this;
256375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
256475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  // Strip away parentheses and casts we don't care about.
256575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  while (true) {
256675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
256775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      E = Paren->getSubExpr();
256875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      continue;
256975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
257075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
257175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
257275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      if (ICE->getCastKind() == CK_NoOp ||
257375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_LValueToRValue ||
257475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_DerivedToBase ||
257575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor          ICE->getCastKind() == CK_UncheckedDerivedToBase) {
257675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        E = ICE->getSubExpr();
257775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        continue;
257875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      }
257975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
258075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
258175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
258275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      if (UnOp->getOpcode() == UO_Extension) {
258375e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        E = UnOp->getSubExpr();
258475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor        continue;
258575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor      }
258675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    }
258775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
258803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    if (const MaterializeTemporaryExpr *M
258903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                      = dyn_cast<MaterializeTemporaryExpr>(E)) {
259003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      E = M->GetTemporaryExpr();
259103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      continue;
259203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    }
259303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
259475e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    break;
259575e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  }
259675e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
259775e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
259875e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor    return This->isImplicit();
259975e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
260075e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor  return false;
260175e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor}
260275e85048e73fcde2ce9d8a48dfdb1220e132eb59Douglas Gregor
2603898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// hasAnyTypeDependentArguments - Determines if any of the expressions
2604898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// in Exprs is type-dependent.
2605cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenkobool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
260613a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles  for (unsigned I = 0; I < Exprs.size(); ++I)
2607898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (Exprs[I]->isTypeDependent())
2608898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      return true;
2609898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2610898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return false;
2611898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
2612898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
26134204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCallbool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
2614c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  // This function is attempting whether an expression is an initializer
261521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  // which can be evaluated at compile-time. It very closely parallels
261621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
261721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  // will lead to unexpected results.  Like ConstExprEmitter, it falls back
261821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  // to isEvaluatable most of the time.
261921cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  //
26204204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // If we ever capture reference-binding directly in the AST, we can
26214204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  // kill the second parameter.
26224204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
26234204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  if (IsForRef) {
26244204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    EvalResult Result;
26254204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
26264204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  }
26271f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman
2628e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  switch (getStmtClass()) {
2629c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  default: break;
2630e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case StringLiteralClass:
2631eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  case ObjCEncodeExprClass:
2632e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    return true;
2633b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXTemporaryObjectExprClass:
2634b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXConstructExprClass: {
2635b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall    const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
26364204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall
263721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    if (CE->getConstructor()->isTrivial() &&
263821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        CE->getConstructor()->getParent()->hasTrivialDestructor()) {
263921cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      // Trivial default constructor
2640180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!CE->getNumArgs()) return true;
2641180f47959a066795cc0f409433023af448bb0328Richard Smith
264221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      // Trivial copy constructor
264321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
264421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      return CE->getArg(0)->isConstantInitializer(Ctx, false);
2645180f47959a066795cc0f409433023af448bb0328Richard Smith    }
2646180f47959a066795cc0f409433023af448bb0328Richard Smith
2647180f47959a066795cc0f409433023af448bb0328Richard Smith    break;
2648b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  }
264959b5da6d853b4368b984700315adf7b37de05764Nate Begeman  case CompoundLiteralExprClass: {
26501f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // This handles gcc's extension that allows global initializers like
26511f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // "struct x {int x;} x = (struct x) {};".
26521f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This accepts other cases it shouldn't!
265359b5da6d853b4368b984700315adf7b37de05764Nate Begeman    const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
26544204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return Exp->isConstantInitializer(Ctx, false);
265559b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
2656e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  case InitListExprClass: {
26571f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This doesn't deal with fields with reference types correctly.
26581f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // FIXME: This incorrectly allows pointers cast to integers to be assigned
26591f4a6db271f389d6ab3cb1bc28cb5c23a7828602Eli Friedman    // to bitfields.
266021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    const InitListExpr *ILE = cast<InitListExpr>(this);
266121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    if (ILE->getType()->isArrayType()) {
266221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      unsigned numInits = ILE->getNumInits();
266321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      for (unsigned i = 0; i < numInits; i++) {
266421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
266521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          return false;
266621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      }
266721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      return true;
2668e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson    }
266921cde050b64eefbb5094af67985752eee42d00e2Eli Friedman
267021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    if (ILE->getType()->isRecordType()) {
267121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      unsigned ElementNo = 0;
267221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
267321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      for (RecordDecl::field_iterator Field = RD->field_begin(),
267421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman           FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
267521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        // If this is a union, skip all the fields that aren't being initialized.
267621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
267721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          continue;
267821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman
267921cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        // Don't emit anonymous bitfields, they just affect layout.
268021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        if (Field->isUnnamedBitfield())
268121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          continue;
268221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman
268321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        if (ElementNo < ILE->getNumInits()) {
268421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          const Expr *Elt = ILE->getInit(ElementNo++);
268521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          if (Field->isBitField()) {
268621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman            // Bitfields have to evaluate to an integer.
268721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman            llvm::APSInt ResultTmp;
268821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman            if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
268921cde050b64eefbb5094af67985752eee42d00e2Eli Friedman              return false;
269021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          } else {
269121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman            bool RefType = Field->getType()->isReferenceType();
269221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman            if (!Elt->isConstantInitializer(Ctx, RefType))
269321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman              return false;
269421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman          }
269521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        }
269621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      }
269721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman      return true;
269821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    }
269921cde050b64eefbb5094af67985752eee42d00e2Eli Friedman
270021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    break;
2701e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
27023498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  case ImplicitValueInitExprClass:
27033498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return true;
27043ae9f48ae0d07a5aa352bf03c944f557a5ac4c95Chris Lattner  case ParenExprClass:
27054204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall    return cast<ParenExpr>(this)->getSubExpr()
27064204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      ->isConstantInitializer(Ctx, IsForRef);
2707f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case GenericSelectionExprClass:
2708f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return cast<GenericSelectionExpr>(this)->getResultExpr()
2709f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne      ->isConstantInitializer(Ctx, IsForRef);
27105cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara  case ChooseExprClass:
2711a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    if (cast<ChooseExpr>(this)->isConditionDependent())
2712a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      return false;
2713a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    return cast<ChooseExpr>(this)->getChosenSubExpr()
27145cadfab3f2e7660453211eb0e00314bd40d93014Abramo Bagnara      ->isConstantInitializer(Ctx, IsForRef);
2715c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  case UnaryOperatorClass: {
2716c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    const UnaryOperator* Exp = cast<UnaryOperator>(this);
27172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Exp->getOpcode() == UO_Extension)
27184204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall      return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
2719c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
2720c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  }
27214204f07fc8bffe6d320b2de95fea274ccf37a17bJohn McCall  case CXXFunctionalCastExprClass:
2722b4b9b15c597a923a03ad0a33cdc49b67e5cc4450John McCall  case CXXStaticCastExprClass:
272381045d8dcd967def69d8e0945566214a9fe9ffccChris Lattner  case ImplicitCastExprClass:
272421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CStyleCastExprClass:
272521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case ObjCBridgedCastExprClass:
272621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CXXDynamicCastExprClass:
272721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CXXReinterpretCastExprClass:
272821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CXXConstCastExprClass: {
2729d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith    const CastExpr *CE = cast<CastExpr>(this);
2730d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith
27316bd9719fd97abac1b8126eae866e96db88be4ac8Eli Friedman    // Handle misc casts we want to ignore.
27326bd9719fd97abac1b8126eae866e96db88be4ac8Eli Friedman    if (CE->getCastKind() == CK_NoOp ||
27336bd9719fd97abac1b8126eae866e96db88be4ac8Eli Friedman        CE->getCastKind() == CK_LValueToRValue ||
27346bd9719fd97abac1b8126eae866e96db88be4ac8Eli Friedman        CE->getCastKind() == CK_ToUnion ||
273521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        CE->getCastKind() == CK_ConstructorConversion ||
273621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        CE->getCastKind() == CK_NonAtomicToAtomic ||
273721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman        CE->getCastKind() == CK_AtomicToNonAtomic)
2738d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith      return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2739d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith
2740c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman    break;
2741d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith  }
274203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case MaterializeTemporaryExprClass:
27435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
274403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                            ->isConstantInitializer(Ctx, false);
274521cde050b64eefbb5094af67985752eee42d00e2Eli Friedman
274621cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case SubstNonTypeTemplateParmExprClass:
274721cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
274821cde050b64eefbb5094af67985752eee42d00e2Eli Friedman                                            ->isConstantInitializer(Ctx, false);
274921cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CXXDefaultArgExprClass:
275021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    return cast<CXXDefaultArgExpr>(this)->getExpr()
275121cde050b64eefbb5094af67985752eee42d00e2Eli Friedman                                            ->isConstantInitializer(Ctx, false);
275221cde050b64eefbb5094af67985752eee42d00e2Eli Friedman  case CXXDefaultInitExprClass:
275321cde050b64eefbb5094af67985752eee42d00e2Eli Friedman    return cast<CXXDefaultInitExpr>(this)->getExpr()
275421cde050b64eefbb5094af67985752eee42d00e2Eli Friedman                                            ->isConstantInitializer(Ctx, false);
2755e8a32b855ce4e8580a191f8d29d2f3f459834302Anders Carlsson  }
2756c39dc9a25a9d74a5302e8567a4d3fc008212024cEli Friedman  return isEvaluatable(Ctx);
275738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff}
275838374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
27598ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smithbool Expr::HasSideEffects(const ASTContext &Ctx) const {
27608ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  if (isInstantiationDependent())
27618ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return true;
27628ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
27638ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  switch (getStmtClass()) {
27648ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case NoStmtClass:
27658ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  #define ABSTRACT_STMT(Type)
27668ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  #define STMT(Type, Base) case Type##Class:
27678ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  #define EXPR(Type, Base)
27688ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  #include "clang/AST/StmtNodes.inc"
27698ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    llvm_unreachable("unexpected Expr kind");
27708ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
27718ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case DependentScopeDeclRefExprClass:
27728ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXUnresolvedConstructExprClass:
27738ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXDependentScopeMemberExprClass:
27748ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case UnresolvedLookupExprClass:
27758ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case UnresolvedMemberExprClass:
27768ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case PackExpansionExprClass:
27778ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case SubstNonTypeTemplateParmPackExprClass:
27789a4db032ecd991626d236a502e770126db32bd31Richard Smith  case FunctionParmPackExprClass:
27798ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    llvm_unreachable("shouldn't see dependent / unresolved nodes here");
27808ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
278160b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case DeclRefExprClass:
278260b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case ObjCIvarRefExprClass:
27838ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case PredefinedExprClass:
27848ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case IntegerLiteralClass:
27858ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case FloatingLiteralClass:
27868ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ImaginaryLiteralClass:
27878ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case StringLiteralClass:
27888ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CharacterLiteralClass:
27898ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case OffsetOfExprClass:
27908ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ImplicitValueInitExprClass:
27918ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case UnaryExprOrTypeTraitExprClass:
27928ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case AddrLabelExprClass:
27938ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case GNUNullExprClass:
27948ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXBoolLiteralExprClass:
27958ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXNullPtrLiteralExprClass:
27968ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXThisExprClass:
27978ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXScalarValueInitExprClass:
27988ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case TypeTraitExprClass:
27998ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case UnaryTypeTraitExprClass:
28008ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case BinaryTypeTraitExprClass:
28018ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ArrayTypeTraitExprClass:
28028ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ExpressionTraitExprClass:
28038ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXNoexceptExprClass:
28048ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case SizeOfPackExprClass:
28058ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCStringLiteralClass:
28068ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCEncodeExprClass:
28078ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCBoolLiteralExprClass:
28088ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXUuidofExprClass:
28098ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case OpaqueValueExprClass:
28108ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // These never have a side-effect.
28118ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return false;
28128ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
28138ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CallExprClass:
281476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  case MSPropertyRefExprClass:
28158ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CompoundAssignOperatorClass:
28168ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case VAArgExprClass:
28178ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case AtomicExprClass:
28188ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case StmtExprClass:
28198ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXOperatorCallExprClass:
28208ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXMemberCallExprClass:
28218ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case UserDefinedLiteralClass:
28228ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXThrowExprClass:
28238ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXNewExprClass:
28248ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXDeleteExprClass:
28258ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ExprWithCleanupsClass:
28268ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXBindTemporaryExprClass:
28278ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case BlockExprClass:
28288ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CUDAKernelCallExprClass:
28298ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // These always have a side-effect.
28308ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return true;
28318ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
28328ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ParenExprClass:
28338ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ArraySubscriptExprClass:
28348ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case MemberExprClass:
28358ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ConditionalOperatorClass:
28368ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case BinaryConditionalOperatorClass:
28378ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CompoundLiteralExprClass:
28388ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ExtVectorElementExprClass:
28398ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case DesignatedInitExprClass:
28408ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ParenListExprClass:
28418ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXPseudoDestructorExprClass:
28427c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith  case CXXStdInitializerListExprClass:
28438ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case SubstNonTypeTemplateParmExprClass:
28448ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case MaterializeTemporaryExprClass:
28458ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ShuffleVectorExprClass:
28468ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case AsTypeExprClass:
28478ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // These have a side-effect if any subexpression does.
28488ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
28498ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
285060b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case UnaryOperatorClass:
285160b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
28528ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      return true;
28538ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
28548ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
28558ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case BinaryOperatorClass:
28568ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    if (cast<BinaryOperator>(this)->isAssignmentOp())
28578ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      return true;
28588ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
28598ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
28608ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case InitListExprClass:
28618ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // FIXME: The children for an InitListExpr doesn't include the array filler.
28628ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
28638ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      if (E->HasSideEffects(Ctx))
28648ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        return true;
28658ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
28668ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
28678ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case GenericSelectionExprClass:
28688ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return cast<GenericSelectionExpr>(this)->getResultExpr()->
28698ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        HasSideEffects(Ctx);
28708ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
28718ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ChooseExprClass:
2872a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
28738ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
28748ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXDefaultArgExprClass:
28758ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
28768ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
2877c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  case CXXDefaultInitExprClass:
2878c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2879c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith      return E->HasSideEffects(Ctx);
2880c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    // If we've not yet parsed the initializer, assume it has side-effects.
2881c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    return true;
2882c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith
28838ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXDynamicCastExprClass: {
28848ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // A dynamic_cast expression has side-effects if it can throw.
28858ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
28868ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    if (DCE->getTypeAsWritten()->isReferenceType() &&
28878ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        DCE->getCastKind() == CK_Dynamic)
28888ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      return true;
288960b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  } // Fall through.
289060b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case ImplicitCastExprClass:
289160b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case CStyleCastExprClass:
289260b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case CXXStaticCastExprClass:
289360b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case CXXReinterpretCastExprClass:
289460b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case CXXConstCastExprClass:
289560b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  case CXXFunctionalCastExprClass: {
289660b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    const CastExpr *CE = cast<CastExpr>(this);
289760b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    if (CE->getCastKind() == CK_LValueToRValue &&
289860b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith        CE->getSubExpr()->getType().isVolatileQualified())
289960b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith      return true;
29008ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
29018ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  }
29028ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
29030d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith  case CXXTypeidExprClass:
29040d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith    // typeid might throw if its subexpression is potentially-evaluated, so has
29050d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith    // side-effects in that case whether or not its subexpression does.
29060d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith    return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
29078ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
29088ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXConstructExprClass:
29098ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case CXXTemporaryObjectExprClass: {
29108ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
291160b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    if (!CE->getConstructor()->isTrivial())
29128ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      return true;
291360b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    // A trivial constructor does not add any side-effects of its own. Just look
291460b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith    // at its arguments.
29158ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    break;
29168ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  }
29178ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
29188ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case LambdaExprClass: {
29198ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    const LambdaExpr *LE = cast<LambdaExpr>(this);
29208ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    for (LambdaExpr::capture_iterator I = LE->capture_begin(),
29218ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith                                      E = LE->capture_end(); I != E; ++I)
29228ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      if (I->getCaptureKind() == LCK_ByCopy)
29238ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        // FIXME: Only has a side-effect if the variable is volatile or if
29248ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        // the copy would invoke a non-trivial copy constructor.
29258ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        return true;
29268ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return false;
29278ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  }
29288ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
29298ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case PseudoObjectExprClass: {
29308ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // Only look for side-effects in the semantic form, and look past
29318ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // OpaqueValueExpr bindings in that form.
29328ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
29338ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
29348ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith                                                    E = PO->semantics_end();
29358ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith         I != E; ++I) {
29368ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      const Expr *Subexpr = *I;
29378ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
29388ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        Subexpr = OVE->getSourceExpr();
29398ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      if (Subexpr->HasSideEffects(Ctx))
29408ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        return true;
29418ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    }
29428ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return false;
29438ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  }
29448ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
29458ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCBoxedExprClass:
29468ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCArrayLiteralClass:
29478ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCDictionaryLiteralClass:
29488ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCMessageExprClass:
29498ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCSelectorExprClass:
29508ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCProtocolExprClass:
29518ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCPropertyRefExprClass:
29528ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCIsaExprClass:
29538ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCIndirectCopyRestoreExprClass:
29548ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCSubscriptRefExprClass:
29558ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  case ObjCBridgedCastExprClass:
29568ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // FIXME: Classify these cases better.
29578ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    return true;
29588ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  }
29598ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
29608ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  // Recurse to children.
29618ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
29628ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    if (const Stmt *S = *SubStmts)
29638ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith      if (cast<Expr>(S)->HasSideEffects(Ctx))
29648ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith        return true;
29658ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
29668ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith  return false;
29678ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith}
29688ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith
296925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregornamespace {
297025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  /// \brief Look for a call to a non-trivial function within an expression.
297125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
297225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  {
297325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
297425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
297525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    bool NonTrivial;
297625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
297725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  public:
297825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    explicit NonTrivialCallFinder(ASTContext &Context)
2979b11e52540166a2958439cfe562c25931e4460759Douglas Gregor      : Inherited(Context), NonTrivial(false) { }
298025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
298125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    bool hasNonTrivialCall() const { return NonTrivial; }
298225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
298325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    void VisitCallExpr(CallExpr *E) {
298425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      if (CXXMethodDecl *Method
298525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor          = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
298625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        if (Method->isTrivial()) {
298725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor          // Recurse to children of the call.
298825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor          Inherited::VisitStmt(E);
298925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor          return;
299025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        }
299125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      }
299225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
299325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      NonTrivial = true;
299425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    }
299525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
299625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    void VisitCXXConstructExpr(CXXConstructExpr *E) {
299725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      if (E->getConstructor()->isTrivial()) {
299825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        // Recurse to children of the call.
299925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        Inherited::VisitStmt(E);
300025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        return;
300125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      }
300225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
300325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      NonTrivial = true;
300425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    }
300525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
300625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
300725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      if (E->getTemporary()->getDestructor()->isTrivial()) {
300825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        Inherited::VisitStmt(E);
300925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        return;
301025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      }
301125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
301225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      NonTrivial = true;
301325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    }
301425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  };
301525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor}
301625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
301725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregorbool Expr::hasNonTrivialCall(ASTContext &Ctx) {
301825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  NonTrivialCallFinder Finder(Ctx);
301925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  Finder.Visit(this);
302025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  return Finder.hasNonTrivialCall();
302125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor}
302225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
302382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
302482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// pointer constant or not, as well as the specific kind of constant detected.
302582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// Null pointer constants can be integer constant expressions with the
302682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// value zero, casts of zero to void*, nullptr (C++0X), or __null
302782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth/// (a GNU extension).
302882214a80c0163e01e4d8dec1426023c89277dbb4Chandler CarruthExpr::NullPointerConstantKind
302982214a80c0163e01e4d8dec1426023c89277dbb4Chandler CarruthExpr::isNullPointerConstant(ASTContext &Ctx,
303082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth                            NullPointerConstantValueDependence NPC) const {
3031f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith  if (isValueDependent() && !Ctx.getLangOpts().CPlusPlus11) {
3032ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    switch (NPC) {
3033ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_NeverValueDependent:
3034b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unexpected value dependent expression!");
3035ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNull:
303682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      if (isTypeDependent() || getType()->isIntegralType(Ctx))
303750800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie        return NPCK_ZeroExpression;
303882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      else
303982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth        return NPCK_NotNull;
3040c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
3041ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    case NPC_ValueDependentIsNotNull:
304282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      return NPCK_NotNull;
3043ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    }
3044ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  }
3045f515b2268f829adfbfdb751f54d102b53ed0285cDaniel Dunbar
30460777972d38a3125efed962b045704c30ae6965cfSebastian Redl  // Strip off a cast to void*, if it exists. Except in C++.
30470835a3cdeefe714b4959d31127ea155e56393125Argyrios Kyrtzidis  if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
30484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!Ctx.getLangOpts().CPlusPlus) {
30490777972d38a3125efed962b045704c30ae6965cfSebastian Redl      // Check that it is a cast to void*.
30506217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
30510777972d38a3125efed962b045704c30ae6965cfSebastian Redl        QualType Pointee = PT->getPointeeType();
30520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        if (!Pointee.hasQualifiers() &&
30530777972d38a3125efed962b045704c30ae6965cfSebastian Redl            Pointee->isVoidType() &&                              // to void*
30540777972d38a3125efed962b045704c30ae6965cfSebastian Redl            CE->getSubExpr()->getType()->isIntegerType())         // from int.
3055ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor          return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
30560777972d38a3125efed962b045704c30ae6965cfSebastian Redl      }
30575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3058aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3059aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Ignore the ImplicitCastExpr type entirely.
3060ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
3061aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3062aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // Accept ((void*)0) as a null pointer constant, as many other
3063aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff    // implementations do.
3064ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
3065f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  } else if (const GenericSelectionExpr *GE =
3066f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne               dyn_cast<GenericSelectionExpr>(this)) {
3067a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    if (GE->isResultDependent())
3068a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      return NPCK_NotNull;
3069f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
3070a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman  } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3071a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    if (CE->isConditionDependent())
3072a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman      return NPCK_NotNull;
3073a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman    return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
30741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else if (const CXXDefaultArgExpr *DefaultArg
30758123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner               = dyn_cast<CXXDefaultArgExpr>(this)) {
3076c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    // See through default argument expressions.
3077ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
3078c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  } else if (const CXXDefaultInitExpr *DefaultInit
3079c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith               = dyn_cast<CXXDefaultInitExpr>(this)) {
3080c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    // See through default initializer expressions.
3081c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
30822d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor  } else if (isa<GNUNullExpr>(this)) {
30832d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor    // The GNU __null extension is always a null pointer constant.
308482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_GNUNull;
308503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  } else if (const MaterializeTemporaryExpr *M
308603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                   = dyn_cast<MaterializeTemporaryExpr>(this)) {
308703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
30884b9c2d235fb9449e249d74f48ecfec601650de93John McCall  } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
30894b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (const Expr *Source = OVE->getSourceExpr())
30904b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return Source->isNullPointerConstant(Ctx, NPC);
3091aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff  }
30922d8b273470684a9cd47f0ce24743cc1f71ef7cbcDouglas Gregor
30934e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  // C++11 nullptr_t is always a null pointer constant.
30946e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  if (getType()->isNullPtrType())
30954e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    return NPCK_CXX11_nullptr;
30966e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
3097ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian  if (const RecordType *UT = getType()->getAsUnionType())
3098f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    if (!Ctx.getLangOpts().CPlusPlus11 &&
3099f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith        UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
3100ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3101ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian        const Expr *InitExpr = CLE->getInitializer();
3102ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian        if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3103ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian          return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3104ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      }
3105aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  // This expression must be an integer type.
3106c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  if (!getType()->isIntegerType() ||
31074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
310882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    return NPCK_NotNull;
31091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
311080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (Ctx.getLangOpts().CPlusPlus11) {
3111f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3112f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    // value zero or a prvalue of type std::nullptr_t.
3113f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
3114f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    return (Lit && !Lit->getValue()) ? NPCK_ZeroLiteral : NPCK_NotNull;
311570488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  } else {
3116f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    // If we have an integer constant expression, we need to *evaluate* it and
3117f050d2445ed75569ccfe0740caa900403d2ce3b3Richard Smith    // test for the value 0.
311870488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith    if (!isIntegerConstantExpr(Ctx))
311970488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith      return NPCK_NotNull;
312070488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  }
312182214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
312250800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie  if (EvaluateKnownConstInt(Ctx) != 0)
312350800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    return NPCK_NotNull;
312450800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie
312550800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie  if (isa<IntegerLiteral>(this))
312650800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    return NPCK_ZeroLiteral;
312750800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie  return NPCK_ZeroExpression;
31285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
312931a458462c6cf417a84e0c47852b18fb22d79acbSteve Naroff
3130f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// \brief If this expression is an l-value for an Objective C
3131f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// property, find the underlying property reference expression.
3132f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallconst ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3133f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  const Expr *E = this;
3134f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  while (true) {
3135f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    assert((E->getValueKind() == VK_LValue &&
3136f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall            E->getObjectKind() == OK_ObjCProperty) &&
3137f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall           "expression is not a property reference");
3138f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    E = E->IgnoreParenCasts();
3139f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3140f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      if (BO->getOpcode() == BO_Comma) {
3141f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        E = BO->getRHS();
3142f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        continue;
3143f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
3144f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
3145f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
3146f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
3147f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
3148f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
3149f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return cast<ObjCPropertyRefExpr>(E);
3150f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
3151f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
3152bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaksbool Expr::isObjCSelfExpr() const {
3153bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  const Expr *E = IgnoreParenImpCasts();
3154bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
3155bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3156bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  if (!DRE)
3157bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks    return false;
3158bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
3159bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3160bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  if (!Param)
3161bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks    return false;
3162bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
3163bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3164bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  if (!M)
3165bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks    return false;
3166bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
3167bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  return M->getSelfDecl() == Param;
3168bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks}
3169bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
3170993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCallFieldDecl *Expr::getSourceBitField() {
31716f4a69a3107e7ff1569c747f7c6bdf7cff8cbf55Douglas Gregor  Expr *E = this->IgnoreParens();
317233bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
3173de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
3174f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (ICE->getCastKind() == CK_LValueToRValue ||
3175f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
3176de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      E = ICE->getSubExpr()->IgnoreParens();
3177de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    else
3178de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
3179de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  }
3180de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
318127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
318286f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
318333bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor      if (Field->isBitField())
318433bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor        return Field;
318533bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
3186993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3187993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall    if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3188993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall      if (Ivar->isBitField())
3189993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall        return Ivar;
3190993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall
31910f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis  if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
31920f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis    if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
31930f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis      if (Field->isBitField())
31940f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis        return Field;
31950f279e756e3df69f9e071c572805e5d3e89123a2Argyrios Kyrtzidis
319642068e9d420889014ee26894e421fead336b239dEli Friedman  if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
319733bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor    if (BinOp->isAssignmentOp() && BinOp->getLHS())
3198993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall      return BinOp->getLHS()->getSourceBitField();
319933bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor
320042068e9d420889014ee26894e421fead336b239dEli Friedman    if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
3201993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall      return BinOp->getRHS()->getSourceBitField();
320242068e9d420889014ee26894e421fead336b239dEli Friedman  }
320342068e9d420889014ee26894e421fead336b239dEli Friedman
320433bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor  return 0;
320527c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
320627c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
3207093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlssonbool Expr::refersToVectorElement() const {
3208093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  const Expr *E = this->IgnoreParens();
3209c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
3210093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
32115baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    if (ICE->getValueKind() != VK_RValue &&
32122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        ICE->getCastKind() == CK_NoOp)
3213093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      E = ICE->getSubExpr()->IgnoreParens();
3214093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    else
3215093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      break;
3216093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  }
3217c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
3218093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3219093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return ASE->getBase()->getType()->isVectorType();
3220093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
3221093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  if (isa<ExtVectorElementExpr>(E))
3222093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson    return true;
3223093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
3224093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  return false;
3225093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson}
3226093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
32272140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// isArrow - Return true if the base expression is a pointer to vector,
32282140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner/// return false if the base expression is a vector.
32292140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattnerbool ExtVectorElementExpr::isArrow() const {
32302140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner  return getBase()->getType()->isPointerType();
32312140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner}
32322140e904dbe53657339cb5b1cc13de563ca0d1fcChris Lattner
3233213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanunsigned ExtVectorElementExpr::getNumElements() const {
3234183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const VectorType *VT = getType()->getAs<VectorType>())
32358a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    return VT->getNumElements();
32368a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  return 1;
32374d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner}
32384d0ac88428b3ed7c6f3a2f4e758ea5424ecd70aeChris Lattner
32398a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// containsDuplicateElements - Return true if any element access is repeated.
3240213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begemanbool ExtVectorElementExpr::containsDuplicateElements() const {
3241a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // FIXME: Refactor this code to an accessor on the AST node which returns the
3242a2b34eb7d19d1d199a244da20afe12353e3593acDaniel Dunbar  // "type" of component access, and share with code below and in Sema.
32435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Comp = Accessor->getName();
3244190d6a25393995b42e32086949a68285ee423fb9Nate Begeman
3245190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Halving swizzles do not contain duplicate elements.
3246150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
3247190d6a25393995b42e32086949a68285ee423fb9Nate Begeman    return false;
32481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3249190d6a25393995b42e32086949a68285ee423fb9Nate Begeman  // Advance past s-char prefix on hex swizzles.
3250150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
3251150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar    Comp = Comp.substr(1);
32521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3253150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar  for (unsigned i = 0, e = Comp.size(); i != e; ++i)
32545f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
3255fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff        return true;
3256150274299b0bc2efda45783f99bef3f9f6e807acDaniel Dunbar
3257fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff  return false;
3258fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff}
3259b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
32608a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
32613b8d116703db8018f855cbb4733ace426422623bNate Begemanvoid ExtVectorElementExpr::getEncodedElementAccess(
32625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  SmallVectorImpl<unsigned> &Elts) const {
32635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Comp = Accessor->getName();
32644b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  if (Comp[0] == 's' || Comp[0] == 'S')
32654b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar    Comp = Comp.substr(1);
32661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32674b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isHi =   Comp == "hi";
32684b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isLo =   Comp == "lo";
32694b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isEven = Comp == "even";
32704b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar  bool isOdd  = Comp == "odd";
32711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32728a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman  for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
32738a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    uint64_t Index;
32741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32758a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    if (isHi)
32768a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = e + i;
32778a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isLo)
32788a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = i;
32798a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isEven)
32808a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i;
32818a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else if (isOdd)
32828a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman      Index = 2 * i + 1;
32838a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman    else
32844b55b24410a2739c589c4b9e84a364161c9a17e5Daniel Dunbar      Index = ExtVectorType::getAccessorIdx(Comp[i]);
3285b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner
32863b8d116703db8018f855cbb4733ace426422623bNate Begeman    Elts.push_back(Index);
3287b8f849da3cedee2f61ad98389115ddd04e439d60Chris Lattner  }
32888a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman}
32898a99764f9b778a54e7440b1ee06a1e48f25d76d8Nate Begeman
329004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
3291f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
329204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
329304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation SuperLoc,
329404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 bool IsInstanceSuper,
329504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 QualType SuperType,
3296c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
3297207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
3298207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
329904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
33008d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
3301746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                 SourceLocation RBracLoc,
3302746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                 bool isImplicit)
3303f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
3304bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*TypeDependent=*/false, /*ValueDependent=*/false,
3305561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*InstantiationDependent=*/false,
3306bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         /*ContainsUnexpandedParameterPack=*/false),
330704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
330804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
3309b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(IsInstanceSuper? SuperInstance : SuperClass),
3310746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3311746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis    SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
3312c2350e553b853ad00914faf23fa731e5fc4a8a5cDouglas Gregor{
3313207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
331404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(SuperType.getAsOpaquePtr());
331504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
331604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
331704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
3318f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
331904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
332004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 TypeSourceInfo *Receiver,
3321f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                 Selector Sel,
3322207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
3323207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
332404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
33258d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
3326746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                 SourceLocation RBracLoc,
3327746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                 bool isImplicit)
3328f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
3329561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         T->isDependentType(), T->isInstantiationDependentType(),
3330561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         T->containsUnexpandedParameterPack()),
333104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
333204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
3333b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(Class),
3334746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3335207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
333604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor{
3337207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
333804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(Receiver);
333904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
334004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
334104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorObjCMessageExpr::ObjCMessageExpr(QualType T,
3342f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                 ExprValueKind VK,
334304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 SourceLocation LBracLoc,
334404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 Expr *Receiver,
3345c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                 Selector Sel,
3346207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 ArrayRef<SourceLocation> SelLocs,
3347207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                 SelectorLocationsKind SelLocsK,
334804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                 ObjCMethodDecl *Method,
33498d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                 ArrayRef<Expr *> Args,
3350746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                 SourceLocation RBracLoc,
3351746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                 bool isImplicit)
3352f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
3353bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Receiver->isTypeDependent(),
3354561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Receiver->isInstantiationDependent(),
3355bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Receiver->containsUnexpandedParameterPack()),
335604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
335704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                       : Sel.getAsOpaquePtr())),
3358b994e6c7d57b00e3e0f69d152065e2cf85d1de33Argyrios Kyrtzidis    Kind(Instance),
3359746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis    HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3360207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    LBracLoc(LBracLoc), RBracLoc(RBracLoc)
336104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor{
3362207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
336304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  setReceiverPointer(Receiver);
3364207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
3365207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
3366207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidisvoid ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3367207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
3368207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                         SelectorLocationsKind SelLocsK) {
3369207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  setNumArgs(Args.size());
3370aa165f8458b51c546bebff947343e1a36f3594cbDouglas Gregor  Expr **MyArgs = getArgs();
33718d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis  for (unsigned I = 0; I != Args.size(); ++I) {
3372bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->isTypeDependent())
3373bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
3374bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->isValueDependent())
3375bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
3376561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[I]->isInstantiationDependent())
3377561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
3378bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (Args[I]->containsUnexpandedParameterPack())
3379bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
3380bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
3381bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    MyArgs[I] = Args[I];
3382bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
3383207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
338419562c97669532084a71895197f4444305dd432aBenjamin Kramer  SelLocsKind = SelLocsK;
33850c6b8e3fa718a0a67292340e5cff6fe7cbd15c14Argyrios Kyrtzidis  if (!isImplicit()) {
33860c6b8e3fa718a0a67292340e5cff6fe7cbd15c14Argyrios Kyrtzidis    if (SelLocsK == SelLoc_NonStandard)
33870c6b8e3fa718a0a67292340e5cff6fe7cbd15c14Argyrios Kyrtzidis      std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
33880c6b8e3fa718a0a67292340e5cff6fe7cbd15c14Argyrios Kyrtzidis  }
338904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
339004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
33919db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
3392f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
339304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
339404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation SuperLoc,
339504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         bool IsInstanceSuper,
339604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         QualType SuperType,
3397c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
3398951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
339904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
34008d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
3401746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                         SourceLocation RBracLoc,
3402746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                         bool isImplicit) {
3403746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  assert((!SelLocs.empty() || isImplicit) &&
3404746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis         "No selector locs for non-implicit message");
3405746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  ObjCMessageExpr *Mem;
3406746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3407746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  if (isImplicit)
3408746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis    Mem = alloc(Context, Args.size(), 0);
3409746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  else
3410746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis    Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
3411f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
3412207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                   SuperType, Sel, SelLocs, SelLocsK,
3413746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                   Method, Args, RBracLoc, isImplicit);
341404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
341504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
34169db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
3417f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
341804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
341904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         TypeSourceInfo *Receiver,
3420c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt                                         Selector Sel,
3421951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
342204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
34238d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
3424746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                         SourceLocation RBracLoc,
3425746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                         bool isImplicit) {
3426746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  assert((!SelLocs.empty() || isImplicit) &&
3427746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis         "No selector locs for non-implicit message");
3428746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  ObjCMessageExpr *Mem;
3429746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3430746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  if (isImplicit)
3431746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis    Mem = alloc(Context, Args.size(), 0);
3432746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  else
3433746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis    Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
3434951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
3435746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                   SelLocs, SelLocsK, Method, Args, RBracLoc,
3436746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                   isImplicit);
343704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
343804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
34399db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
3440f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         ExprValueKind VK,
344104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         SourceLocation LBracLoc,
344204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         Expr *Receiver,
3443f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                         Selector Sel,
3444951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                         ArrayRef<SourceLocation> SelLocs,
344504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                         ObjCMethodDecl *Method,
34468d9ed7980405e91a12e33338a78fb99620adf553Argyrios Kyrtzidis                                         ArrayRef<Expr *> Args,
3447746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                         SourceLocation RBracLoc,
3448746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                         bool isImplicit) {
3449746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  assert((!SelLocs.empty() || isImplicit) &&
3450746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis         "No selector locs for non-implicit message");
3451746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  ObjCMessageExpr *Mem;
3452746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3453746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  if (isImplicit)
3454746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis    Mem = alloc(Context, Args.size(), 0);
3455746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis  else
3456746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis    Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
3457951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
3458746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                   SelLocs, SelLocsK, Method, Args, RBracLoc,
3459746f5bcbfde5b25269169c63c66492311673b67dArgyrios Kyrtzidis                                   isImplicit);
346004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
346104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
34629db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
3463207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                              unsigned NumArgs,
3464207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                              unsigned NumStoredSelLocs) {
3465207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
346604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
346704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
3468e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
34699db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
3470207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        ArrayRef<Expr *> Args,
3471207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        SourceLocation RBraceLoc,
3472207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        ArrayRef<SourceLocation> SelLocs,
3473207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        Selector Sel,
3474207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        SelectorLocationsKind &SelLocsK) {
3475207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3476207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3477207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                                               : 0;
3478207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  return alloc(C, Args.size(), NumStoredSelLocs);
3479207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
3480207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
34819db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
3482207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        unsigned NumArgs,
3483207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                        unsigned NumStoredSelLocs) {
3484207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3485207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3486207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  return (ObjCMessageExpr *)C.Allocate(Size,
3487207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                                     llvm::AlignOf<ObjCMessageExpr>::Alignment);
3488207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
3489207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
3490207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidisvoid ObjCMessageExpr::getSelectorLocs(
3491207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis                               SmallVectorImpl<SourceLocation> &SelLocs) const {
3492207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis  for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3493207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis    SelLocs.push_back(getSelectorLoc(i));
3494207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis}
3495207180802c836fda8acbedb47a92f9d2bdca59c3Argyrios Kyrtzidis
3496e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios KyrtzidisSourceRange ObjCMessageExpr::getReceiverRange() const {
3497e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  switch (getReceiverKind()) {
3498e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case Instance:
3499e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getInstanceReceiver()->getSourceRange();
3500e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
3501e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case Class:
3502e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3503e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
3504e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case SuperInstance:
3505e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  case SuperClass:
3506e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis    return getSuperLoc();
3507e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis  }
3508e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
35093026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid ReceiverKind!");
3510e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis}
3511e005d19456e6fb73ace33f25e02ac10e22dd063fArgyrios Kyrtzidis
351204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas GregorSelector ObjCMessageExpr::getSelector() const {
351304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (HasMethod)
351404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
351504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor                                                               ->getSelector();
3516c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt  return Selector(SelectorOrMethod);
351704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor}
351804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
3519e4a990f34904eb572c8d6aa1deef19465214359cArgyrios KyrtzidisQualType ObjCMessageExpr::getReceiverType() const {
352004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  switch (getReceiverKind()) {
352104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case Instance:
3522e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis    return getInstanceReceiver()->getType();
352304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case Class:
3524e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis    return getClassReceiver();
352504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case SuperInstance:
352604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  case SuperClass:
3527e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis    return getSuperType();
352804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  }
35290389e6bd0159bfdd08f7c50a37543b6e3adf0c33Chris Lattner
3530e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis  llvm_unreachable("unexpected receiver kind");
3531e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis}
3532e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis
3533e4a990f34904eb572c8d6aa1deef19465214359cArgyrios KyrtzidisObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3534e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis  QualType T = getReceiverType();
3535e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis
3536e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis  if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3537e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis    return Ptr->getInterfaceDecl();
3538e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis
3539e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis  if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3540e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis    return Ty->getInterface();
3541e4a990f34904eb572c8d6aa1deef19465214359cArgyrios Kyrtzidis
354204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  return 0;
3543eb3b324800598cc3d5385fbad95ae5cff2c79113Ted Kremenek}
35440389e6bd0159bfdd08f7c50a37543b6e3adf0c33Chris Lattner
35455f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef ObjCBridgedCastExpr::getBridgeKindName() const {
3546f85e193739c953358c865005855253af4f68a497John McCall  switch (getBridgeKind()) {
3547f85e193739c953358c865005855253af4f68a497John McCall  case OBC_Bridge:
3548f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge";
3549f85e193739c953358c865005855253af4f68a497John McCall  case OBC_BridgeTransfer:
3550f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge_transfer";
3551f85e193739c953358c865005855253af4f68a497John McCall  case OBC_BridgeRetained:
3552f85e193739c953358c865005855253af4f68a497John McCall    return "__bridge_retained";
3553f85e193739c953358c865005855253af4f68a497John McCall  }
35543026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie
35553026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid BridgeKind!");
3556f85e193739c953358c865005855253af4f68a497John McCall}
3557f85e193739c953358c865005855253af4f68a497John McCall
355805ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
3559bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor                                     QualType Type, SourceLocation BLoc,
3560bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor                                     SourceLocation RP)
3561bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor   : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3562bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          Type->isDependentType(), Type->isDependentType(),
3563561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          Type->isInstantiationDependentType(),
3564bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          Type->containsUnexpandedParameterPack()),
35653b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer     BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
3566bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor{
35673b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  SubExprs = new (C) Stmt*[args.size()];
35683b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != args.size(); i++) {
3569bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isTypeDependent())
3570bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
3571bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->isValueDependent())
3572bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
3573561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (args[i]->isInstantiationDependent())
3574561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
3575bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (args[i]->containsUnexpandedParameterPack())
3576bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
3577bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
3578bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    SubExprs[i] = args[i];
3579bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
3580bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor}
3581bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
358205ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
3583888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman  if (SubExprs) C.Deallocate(SubExprs);
3584888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
358527365ee830cec7cf52c088b400867ff67b0b3b23Dmitri Gribenko  this->NumExprs = Exprs.size();
35862ad77cdd028b3c96f21d7bc6daee8fde6cf0cd86Dmitri Gribenko  SubExprs = new (C) Stmt*[NumExprs];
358727365ee830cec7cf52c088b400867ff67b0b3b23Dmitri Gribenko  memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
35881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
3589888376a2bbcfc2f047902249f8455918e2489ae1Nate Begeman
359005ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperGenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
3591f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation GenericLoc, Expr *ControllingExpr,
35923b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               ArrayRef<TypeSourceInfo*> AssocTypes,
35933b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               ArrayRef<Expr*> AssocExprs,
35943b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               SourceLocation DefaultLoc,
3595f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation RParenLoc,
3596f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               bool ContainsUnexpandedParameterPack,
3597f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               unsigned ResultIndex)
3598f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  : Expr(GenericSelectionExprClass,
3599f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getType(),
3600f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getValueKind(),
3601f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->getObjectKind(),
3602f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->isTypeDependent(),
3603f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         AssocExprs[ResultIndex]->isValueDependent(),
3604561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         AssocExprs[ResultIndex]->isInstantiationDependent(),
3605f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         ContainsUnexpandedParameterPack),
36063b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
36073b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
36083b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
36093b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
3610f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  SubExprs[CONTROLLING] = ControllingExpr;
36113b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  assert(AssocTypes.size() == AssocExprs.size());
36123b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
36133b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
3614f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne}
3615f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
361605ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperGenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
3617f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation GenericLoc, Expr *ControllingExpr,
36183b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               ArrayRef<TypeSourceInfo*> AssocTypes,
36193b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               ArrayRef<Expr*> AssocExprs,
36203b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                               SourceLocation DefaultLoc,
3621f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               SourceLocation RParenLoc,
3622f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne                               bool ContainsUnexpandedParameterPack)
3623f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  : Expr(GenericSelectionExprClass,
3624f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         Context.DependentTy,
3625f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         VK_RValue,
3626f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         OK_Ordinary,
3627561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isTypeDependent=*/true,
3628561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isValueDependent=*/true,
3629561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*isInstantiationDependent=*/true,
3630f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne         ContainsUnexpandedParameterPack),
36313b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
36323b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
36333b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
36343b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
3635f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  SubExprs[CONTROLLING] = ControllingExpr;
36363b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  assert(AssocTypes.size() == AssocExprs.size());
36373b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
36383b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
3639f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne}
3640f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
364177ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
364205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//  DesignatedInitExpr
364305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
364405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3645b11382497a923b0d7009e85a1d8eb7bf93ec6d0dChandler CarruthIdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
364605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(Kind == FieldDesignator && "Only valid on a field designator");
364705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Field.NameOrField & 0x01)
364805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
364905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  else
365005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return getField()->getIdentifier();
365105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
365205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
365305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperDesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
3654319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                       unsigned NumDesignators,
3655ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       const Designator *Designators,
36561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       SourceLocation EqualOrColonLoc,
3657ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                       bool GNUSyntax,
36583b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                       ArrayRef<Expr*> IndexExprs,
36599ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                       Expr *Init)
36601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Expr(DesignatedInitExprClass, Ty,
3661f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall         Init->getValueKind(), Init->getObjectKind(),
3662bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Init->isTypeDependent(), Init->isValueDependent(),
3663561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Init->isInstantiationDependent(),
3664bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor         Init->containsUnexpandedParameterPack()),
36651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
36663b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
3667319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  this->Designators = new (C) Designator[NumDesignators];
36689ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
36699ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Record the initializer itself.
36707502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  child_range Child = children();
36719ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  *Child++ = Init;
36729ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
36739ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // Copy the designators and their subexpressions, computing
36749ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  // value-dependence along the way.
36759ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  unsigned IndexIdx = 0;
36769ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  for (unsigned I = 0; I != NumDesignators; ++I) {
3677ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    this->Designators[I] = Designators[I];
36789ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
36799ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    if (this->Designators[I].isArrayDesignator()) {
36809ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
36819ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Index = IndexExprs[IndexIdx];
3682bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Index->isTypeDependent() || Index->isValueDependent())
3683bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ValueDependent = true;
3684561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      if (Index->isInstantiationDependent())
3685561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3686bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      // Propagate unexpanded parameter packs.
3687bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Index->containsUnexpandedParameterPack())
3688bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ContainsUnexpandedParameterPack = true;
36899ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
36909ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the index expressions into permanent storage.
36919ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
36929ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    } else if (this->Designators[I].isArrayRangeDesignator()) {
36939ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Compute type- and value-dependence.
36949ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *Start = IndexExprs[IndexIdx];
36959ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      Expr *End = IndexExprs[IndexIdx + 1];
3696bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Start->isTypeDependent() || Start->isValueDependent() ||
3697561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          End->isTypeDependent() || End->isValueDependent()) {
3698bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ValueDependent = true;
3699561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3700561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      } else if (Start->isInstantiationDependent() ||
3701561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                 End->isInstantiationDependent()) {
3702561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        ExprBits.InstantiationDependent = true;
3703561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      }
3704561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
3705bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      // Propagate unexpanded parameter packs.
3706bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      if (Start->containsUnexpandedParameterPack() ||
3707bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor          End->containsUnexpandedParameterPack())
3708bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor        ExprBits.ContainsUnexpandedParameterPack = true;
37099ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
37109ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      // Copy the start/end expressions into permanent storage.
37119ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
37129ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      *Child++ = IndexExprs[IndexIdx++];
37139ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor    }
37149ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor  }
37159ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor
37163b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
3717ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
3718ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
371905c13a3411782108d65aab3c77b1a231a4963bc0Douglas GregorDesignatedInitExpr *
372005ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperDesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
372105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           unsigned NumDesignators,
37223b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                           ArrayRef<Expr*> IndexExprs,
372305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           SourceLocation ColonOrEqualLoc,
372405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                           bool UsesColonSyntax, Expr *Init) {
3725c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
37263b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                         sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
3727319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
37289ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                                      ColonOrEqualLoc, UsesColonSyntax,
37293b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                      IndexExprs, Init);
373005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
373105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
373205ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperDesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
3733d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                                    unsigned NumIndexExprs) {
3734d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3735d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                         sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3736d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3737d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
3738d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
373905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid DesignatedInitExpr::setDesignators(const ASTContext &C,
3740319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor                                        const Designator *Desigs,
3741d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor                                        unsigned NumDesigs) {
3742319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  Designators = new (C) Designator[NumDesigs];
3743d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  NumDesignators = NumDesigs;
3744d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor  for (unsigned I = 0; I != NumDesigs; ++I)
3745d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor    Designators[I] = Desigs[I];
3746d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor}
3747d077d759d0c7fceee98f4e77b6423a3f11cfc849Douglas Gregor
374824f4674e697fb53587f0e8485e9c6592f7021ef2Abramo BagnaraSourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
374924f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
375024f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara  if (size() == 1)
375124f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara    return DIE->getDesignator(0)->getSourceRange();
375265d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  return SourceRange(DIE->getDesignator(0)->getLocStart(),
375365d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen                     DIE->getDesignator(size()-1)->getLocEnd());
375424f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara}
375524f4674e697fb53587f0e8485e9c6592f7021ef2Abramo Bagnara
375665d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik VerbruggenSourceLocation DesignatedInitExpr::getLocStart() const {
375705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  SourceLocation StartLoc;
3758d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner  Designator &First =
3759d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    *const_cast<DesignatedInitExpr*>(this)->designators_begin();
376005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (First.isFieldDesignator()) {
3761eeae8f072748affce25ab4064982626361293390Douglas Gregor    if (GNUSyntax)
376205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
376305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    else
376405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
376505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  } else
3766d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner    StartLoc =
3767d603eaa682cecac2c10771a700cb83aa301653b4Chris Lattner      SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
376865d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  return StartLoc;
376965d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen}
377065d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen
377165d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik VerbruggenSourceLocation DesignatedInitExpr::getLocEnd() const {
377265d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  return getInit()->getLocEnd();
377305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
377405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3775d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri GribenkoExpr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
377605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3777d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri Gribenko  char *Ptr = static_cast<char *>(
3778d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri Gribenko                  const_cast<void *>(static_cast<const void *>(this)));
377905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
378005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
378105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
378205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
378305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3784d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri GribenkoExpr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
37851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
378605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
3787d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri Gribenko  char *Ptr = static_cast<char *>(
3788d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri Gribenko                  const_cast<void *>(static_cast<const void *>(this)));
378905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
379005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
379105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
379205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
379305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3794d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri GribenkoExpr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
37951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(D.Kind == Designator::ArrayRangeDesignator &&
379605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor         "Requires array range designator");
3797d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri Gribenko  char *Ptr = static_cast<char *>(
3798d615f88e3c24f91a65c25a4f7e66e0f97b18cc15Dmitri Gribenko                  const_cast<void *>(static_cast<const void *>(this)));
379905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Ptr += sizeof(DesignatedInitExpr);
380005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
380105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
380205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
380305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
3804ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// \brief Replaces the designator at index @p Idx with the series
3805ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// of designators in [First, Last).
380605ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Toppervoid DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
38071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          const Designator *First,
3808ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                          const Designator *Last) {
3809ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  unsigned NumNewDesignators = Last - First;
3810ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  if (NumNewDesignators == 0) {
3811ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    std::copy_backward(Designators + Idx + 1,
3812ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + NumDesignators,
3813ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                       Designators + Idx);
3814ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    --NumNewDesignators;
3815ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
3816ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  } else if (NumNewDesignators == 1) {
3817ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    Designators[Idx] = *First;
3818ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    return;
3819ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
3820ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
38211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Designator *NewDesignators
3822319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor    = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
3823ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators, Designators + Idx, NewDesignators);
3824ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(First, Last, NewDesignators + Idx);
3825ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  std::copy(Designators + Idx + 1, Designators + NumDesignators,
3826ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor            NewDesignators + Idx + NumNewDesignators);
3827ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  Designators = NewDesignators;
3828ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  NumDesignators = NumDesignators - 1 + NumNewDesignators;
3829ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
3830ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
383105ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
38323b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                             ArrayRef<Expr*> exprs,
38335b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                             SourceLocation rparenloc)
38345b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
3835561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         false, false, false, false),
38363b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
38373b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  Exprs = new (C) Stmt*[exprs.size()];
38383b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != exprs.size(); ++i) {
3839bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->isTypeDependent())
3840bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.TypeDependent = true;
3841bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->isValueDependent())
3842bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ValueDependent = true;
3843561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (exprs[i]->isInstantiationDependent())
3844561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
3845bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    if (exprs[i]->containsUnexpandedParameterPack())
3846bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor      ExprBits.ContainsUnexpandedParameterPack = true;
3847bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
38482ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman    Exprs[i] = exprs[i];
3849bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
38502ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman}
38512ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman
3852e996ffd240f20a1048179d7727a6ee3227261921John McCallconst OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3853e996ffd240f20a1048179d7727a6ee3227261921John McCall  if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3854e996ffd240f20a1048179d7727a6ee3227261921John McCall    e = ewc->getSubExpr();
385503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
385603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    e = m->GetTemporaryExpr();
3857e996ffd240f20a1048179d7727a6ee3227261921John McCall  e = cast<CXXConstructExpr>(e)->getArg(0);
3858e996ffd240f20a1048179d7727a6ee3227261921John McCall  while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3859e996ffd240f20a1048179d7727a6ee3227261921John McCall    e = ice->getSubExpr();
3860e996ffd240f20a1048179d7727a6ee3227261921John McCall  return cast<OpaqueValueExpr>(e);
3861e996ffd240f20a1048179d7727a6ee3227261921John McCall}
3862e996ffd240f20a1048179d7727a6ee3227261921John McCall
386305ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperPseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
386405ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper                                           EmptyShell sh,
38654b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                           unsigned numSemanticExprs) {
38664b9c2d235fb9449e249d74f48ecfec601650de93John McCall  void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
38674b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                    (1 + numSemanticExprs) * sizeof(Expr*),
38684b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                  llvm::alignOf<PseudoObjectExpr>());
38694b9c2d235fb9449e249d74f48ecfec601650de93John McCall  return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
38704b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
38714b9c2d235fb9449e249d74f48ecfec601650de93John McCall
38724b9c2d235fb9449e249d74f48ecfec601650de93John McCallPseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
38734b9c2d235fb9449e249d74f48ecfec601650de93John McCall  : Expr(PseudoObjectExprClass, shell) {
38744b9c2d235fb9449e249d74f48ecfec601650de93John McCall  PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
38754b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
38764b9c2d235fb9449e249d74f48ecfec601650de93John McCall
387705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig TopperPseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
38784b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                           ArrayRef<Expr*> semantics,
38794b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                           unsigned resultIndex) {
38804b9c2d235fb9449e249d74f48ecfec601650de93John McCall  assert(syntax && "no syntactic expression!");
38814b9c2d235fb9449e249d74f48ecfec601650de93John McCall  assert(semantics.size() && "no semantic expressions!");
38824b9c2d235fb9449e249d74f48ecfec601650de93John McCall
38834b9c2d235fb9449e249d74f48ecfec601650de93John McCall  QualType type;
38844b9c2d235fb9449e249d74f48ecfec601650de93John McCall  ExprValueKind VK;
38854b9c2d235fb9449e249d74f48ecfec601650de93John McCall  if (resultIndex == NoResult) {
38864b9c2d235fb9449e249d74f48ecfec601650de93John McCall    type = C.VoidTy;
38874b9c2d235fb9449e249d74f48ecfec601650de93John McCall    VK = VK_RValue;
38884b9c2d235fb9449e249d74f48ecfec601650de93John McCall  } else {
38894b9c2d235fb9449e249d74f48ecfec601650de93John McCall    assert(resultIndex < semantics.size());
38904b9c2d235fb9449e249d74f48ecfec601650de93John McCall    type = semantics[resultIndex]->getType();
38914b9c2d235fb9449e249d74f48ecfec601650de93John McCall    VK = semantics[resultIndex]->getValueKind();
38924b9c2d235fb9449e249d74f48ecfec601650de93John McCall    assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
38934b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
38944b9c2d235fb9449e249d74f48ecfec601650de93John McCall
38954b9c2d235fb9449e249d74f48ecfec601650de93John McCall  void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
38964b9c2d235fb9449e249d74f48ecfec601650de93John McCall                              (1 + semantics.size()) * sizeof(Expr*),
38974b9c2d235fb9449e249d74f48ecfec601650de93John McCall                            llvm::alignOf<PseudoObjectExpr>());
38984b9c2d235fb9449e249d74f48ecfec601650de93John McCall  return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
38994b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                      resultIndex);
39004b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
39014b9c2d235fb9449e249d74f48ecfec601650de93John McCall
39024b9c2d235fb9449e249d74f48ecfec601650de93John McCallPseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
39034b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                   Expr *syntax, ArrayRef<Expr*> semantics,
39044b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                   unsigned resultIndex)
39054b9c2d235fb9449e249d74f48ecfec601650de93John McCall  : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
39064b9c2d235fb9449e249d74f48ecfec601650de93John McCall         /*filled in at end of ctor*/ false, false, false, false) {
39074b9c2d235fb9449e249d74f48ecfec601650de93John McCall  PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
39084b9c2d235fb9449e249d74f48ecfec601650de93John McCall  PseudoObjectExprBits.ResultIndex = resultIndex + 1;
39094b9c2d235fb9449e249d74f48ecfec601650de93John McCall
39104b9c2d235fb9449e249d74f48ecfec601650de93John McCall  for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
39114b9c2d235fb9449e249d74f48ecfec601650de93John McCall    Expr *E = (i == 0 ? syntax : semantics[i-1]);
39124b9c2d235fb9449e249d74f48ecfec601650de93John McCall    getSubExprsBuffer()[i] = E;
39134b9c2d235fb9449e249d74f48ecfec601650de93John McCall
39144b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->isTypeDependent())
39154b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.TypeDependent = true;
39164b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->isValueDependent())
39174b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.ValueDependent = true;
39184b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->isInstantiationDependent())
39194b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.InstantiationDependent = true;
39204b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->containsUnexpandedParameterPack())
39214b9c2d235fb9449e249d74f48ecfec601650de93John McCall      ExprBits.ContainsUnexpandedParameterPack = true;
39224b9c2d235fb9449e249d74f48ecfec601650de93John McCall
39234b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (isa<OpaqueValueExpr>(E))
39244b9c2d235fb9449e249d74f48ecfec601650de93John McCall      assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
39254b9c2d235fb9449e249d74f48ecfec601650de93John McCall             "opaque-value semantic expressions for pseudo-object "
39264b9c2d235fb9449e249d74f48ecfec601650de93John McCall             "operations must have sources");
39274b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
39284b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
39294b9c2d235fb9449e249d74f48ecfec601650de93John McCall
393005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor//===----------------------------------------------------------------------===//
3931ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//  ExprIterator.
3932ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
3933ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
3934ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3935ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3936ce2fc3a343ea6098a96d587071cee7299f11957aTed KremenekExpr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3937ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator[](size_t idx) const {
3938ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  return cast<Expr>(I[idx]);
3939ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek}
3940ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3941ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenekconst Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3942ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek
3943ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
394477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//  Child Iterators for iterating over subexpressions/substatements
394577ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek//===----------------------------------------------------------------------===//
394677ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
3947f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne// UnaryExprOrTypeTraitExpr
3948f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter CollingbourneStmt::child_range UnaryExprOrTypeTraitExpr::children() {
39490518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // If this is of a type and the type is a VLA type (and not a typedef), the
39500518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // size expression of the VLA needs to be treated as an executable expression.
39510518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  // Why isn't this weirdness documented better in StmtIterator?
39520518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  if (isArgumentType()) {
3953f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
39540518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl                                   getArgumentType().getTypePtr()))
395563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      return child_range(child_iterator(T), child_iterator());
395663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return child_range();
39570518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl  }
395863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return child_range(&Argument.Ex, &Argument.Ex + 1);
39599ac5928abeb3a47592201e1c30fe2930c20a507eTed Kremenek}
3960390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
3961563477da25f824e37c535131695dc4dc9b68c465Steve Naroff// ObjCMessageExpr
396263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCallStmt::child_range ObjCMessageExpr::children() {
396363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  Stmt **begin;
396404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor  if (getReceiverKind() == Instance)
396563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    begin = reinterpret_cast<Stmt **>(this + 1);
396663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  else
396763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    begin = reinterpret_cast<Stmt **>(getArgs());
396863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  return child_range(begin,
396963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                     reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
3970563477da25f824e37c535131695dc4dc9b68c465Steve Naroff}
3971563477da25f824e37c535131695dc4dc9b68c465Steve Naroff
3972cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri GribenkoObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
3973ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                   QualType T, ObjCMethodDecl *Method,
3974ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                   SourceRange SR)
3975ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3976ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek         false, false, false, false),
3977ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3978ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek{
3979ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  Expr **SaveElements = getElements();
3980ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3981ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3982ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ExprBits.ValueDependent = true;
3983ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Elements[I]->isInstantiationDependent())
3984ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ExprBits.InstantiationDependent = true;
3985ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Elements[I]->containsUnexpandedParameterPack())
3986ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ExprBits.ContainsUnexpandedParameterPack = true;
3987ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
3988ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    SaveElements[I] = Elements[I];
3989ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
3990ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
3991ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
39929db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
3993cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                                           ArrayRef<Expr *> Elements,
3994ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                           QualType T, ObjCMethodDecl * Method,
3995ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                           SourceRange SR) {
3996ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3997ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                         + Elements.size() * sizeof(Expr *));
3998ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3999ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
4000ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
40019db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
4002ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                                unsigned NumElements) {
4003ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
4004ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4005ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                         + NumElements * sizeof(Expr *));
4006ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4007ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
4008ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
4009ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekObjCDictionaryLiteral::ObjCDictionaryLiteral(
4010ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                             ArrayRef<ObjCDictionaryElement> VK,
4011ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                             bool HasPackExpansions,
4012ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                             QualType T, ObjCMethodDecl *method,
4013ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                             SourceRange SR)
4014ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4015ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek         false, false),
4016ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4017ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    DictWithObjectsMethod(method)
4018ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek{
4019ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  KeyValuePair *KeyValues = getKeyValues();
4020ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ExpansionData *Expansions = getExpansionData();
4021ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  for (unsigned I = 0; I < NumElements; I++) {
4022ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4023ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4024ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ExprBits.ValueDependent = true;
4025ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (VK[I].Key->isInstantiationDependent() ||
4026ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        VK[I].Value->isInstantiationDependent())
4027ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ExprBits.InstantiationDependent = true;
4028ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (VK[I].EllipsisLoc.isInvalid() &&
4029ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        (VK[I].Key->containsUnexpandedParameterPack() ||
4030ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek         VK[I].Value->containsUnexpandedParameterPack()))
4031ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ExprBits.ContainsUnexpandedParameterPack = true;
4032ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
4033ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    KeyValues[I].Key = VK[I].Key;
4034ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    KeyValues[I].Value = VK[I].Value;
4035ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Expansions) {
4036ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4037ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      if (VK[I].NumExpansions)
4038ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4039ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      else
4040ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        Expansions[I].NumExpansionsPlusOne = 0;
4041ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
4042ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
4043ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
4044ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
4045ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekObjCDictionaryLiteral *
40469db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCDictionaryLiteral::Create(const ASTContext &C,
4047ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                              ArrayRef<ObjCDictionaryElement> VK,
4048ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                              bool HasPackExpansions,
4049ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                              QualType T, ObjCMethodDecl *method,
4050ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                              SourceRange SR) {
4051ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  unsigned ExpansionsSize = 0;
4052ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  if (HasPackExpansions)
4053ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ExpansionsSize = sizeof(ExpansionData) * VK.size();
4054ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
4055ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4056ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                         sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4057ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4058ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
4059ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
4060ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekObjCDictionaryLiteral *
40619db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
4062ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                   bool HasPackExpansions) {
4063ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  unsigned ExpansionsSize = 0;
4064ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  if (HasPackExpansions)
4065ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ExpansionsSize = sizeof(ExpansionData) * NumElements;
4066ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4067ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                         sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4068ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4069ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                         HasPackExpansions);
4070ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
4071ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
40729db7a7eb4e23758e041752c9c0c0ec1663d5a0afCraig TopperObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
4073ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                                   Expr *base,
4074ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                                   Expr *key, QualType T,
4075ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                                   ObjCMethodDecl *getMethod,
4076ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                                   ObjCMethodDecl *setMethod,
4077ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                                   SourceLocation RB) {
4078ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4079ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4080ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                        OK_ObjCSubscript,
4081ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                        getMethod, setMethod, RB);
4082ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
4083dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman
40843b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin KramerAtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
4085dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman                       QualType t, AtomicOp op, SourceLocation RP)
4086dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman  : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4087dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman         false, false, false, false),
40883b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
4089dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman{
40903b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
40913b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  for (unsigned i = 0; i != args.size(); i++) {
4092dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isTypeDependent())
4093dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.TypeDependent = true;
4094dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isValueDependent())
4095dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.ValueDependent = true;
4096dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->isInstantiationDependent())
4097dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.InstantiationDependent = true;
4098dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    if (args[i]->containsUnexpandedParameterPack())
4099dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman      ExprBits.ContainsUnexpandedParameterPack = true;
4100dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman
4101dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    SubExprs[i] = args[i];
4102dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman  }
4103dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman}
4104e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith
4105e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smithunsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4106e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith  switch (Op) {
4107ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_init:
4108ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_load:
4109ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_load_n:
4110e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith    return 2;
4111ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith
4112ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_store:
4113ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_exchange:
4114ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_load:
4115ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_store:
4116ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_store_n:
4117ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_exchange_n:
4118ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_fetch_add:
4119ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_fetch_sub:
4120ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_fetch_and:
4121ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_fetch_or:
4122ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_fetch_xor:
4123ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_fetch_add:
4124ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_fetch_sub:
4125ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_fetch_and:
4126ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_fetch_or:
4127ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_fetch_xor:
412851b92401c9f95023a2ef27064fd5a60fd99175f5Richard Smith  case AO__atomic_fetch_nand:
4129ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_add_fetch:
4130ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_sub_fetch:
4131ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_and_fetch:
4132ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_or_fetch:
4133ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_xor_fetch:
413451b92401c9f95023a2ef27064fd5a60fd99175f5Richard Smith  case AO__atomic_nand_fetch:
4135e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith    return 3;
4136ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith
4137ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_exchange:
4138ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith    return 4;
4139ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith
4140ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_compare_exchange_strong:
4141ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__c11_atomic_compare_exchange_weak:
4142e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith    return 5;
4143ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith
4144ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_compare_exchange:
4145ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith  case AO__atomic_compare_exchange_n:
4146ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith    return 6;
4147e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith  }
4148e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith  llvm_unreachable("unknown atomic op");
4149e1b2abc2ed3f2c98985b06b4ad01c977bd584020Richard Smith}
4150