15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===//
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//===----------------------------------------------------------------------===//
9306f1791d002e84c0a58de5868d8bca0f48e37e4James Dennett///
10306f1791d002e84c0a58de5868d8bca0f48e37e4James Dennett/// \file
11306f1791d002e84c0a58de5868d8bca0f48e37e4James Dennett/// \brief Implements semantic analysis for C++ expressions.
12306f1791d002e84c0a58de5868d8bca0f48e37e4James Dennett///
13306f1791d002e84c0a58de5868d8bca0f48e37e4James Dennett//===----------------------------------------------------------------------===//
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
152d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
162a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall#include "clang/Sema/DeclSpec.h"
17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Initialization.h"
18e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Lookup.h"
192a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall#include "clang/Sema/ParsedTemplate.h"
20469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall#include "clang/Sema/ScopeInfo.h"
217a614d8380297fcd2bc23986241905d97222948cRichard Smith#include "clang/Sema/Scope.h"
222a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall#include "clang/Sema/TemplateDeduction.h"
23210679cacc47640107e46090de1a138977a0431eSteve Naroff#include "clang/AST/ASTContext.h"
24fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky#include "clang/AST/CharUnits.h"
25a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h"
267cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/AST/DeclObjC.h"
27d497ba7ca5e52cd4523822055abd5e89dfec1800Anders Carlsson#include "clang/AST/ExprCXX.h"
28d4266629ed62e3a0de5e2cb2dfb8e806f9bdc5f6Fariborz Jahanian#include "clang/AST/ExprObjC.h"
29b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor#include "clang/AST/TypeLoc.h"
30d497ba7ca5e52cd4523822055abd5e89dfec1800Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
31b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl#include "clang/Basic/TargetInfo.h"
32d497ba7ca5e52cd4523822055abd5e89dfec1800Anders Carlsson#include "clang/Lex/Preprocessor.h"
3391ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie#include "TypeLocBuilder.h"
34bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl#include "llvm/ADT/APInt.h"
353fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor#include "llvm/ADT/STLExtras.h"
3673e0a91c9130a048dfb0928643b9cde1f838b87dChandler Carruth#include "llvm/Support/ErrorHandling.h"
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
382a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCallusing namespace sema;
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
40b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::getDestructorName(SourceLocation TildeLoc,
41dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                   IdentifierInfo &II,
42b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   SourceLocation NameLoc,
43b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   Scope *S, CXXScopeSpec &SS,
44b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   ParsedType ObjectTypePtr,
45b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   bool EnteringContext) {
46124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  // Determine where to perform name lookup.
47124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
48124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  // FIXME: This area of the standard is very messy, and the current
49124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  // wording is rather unclear about which scopes we search for the
50124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  // destructor name; see core issues 399 and 555. Issue 399 in
51124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  // particular shows where the current description of destructor name
52124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  // lookup is completely out of line with existing practice, e.g.,
53124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  // this appears to be ill-formed:
54124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //
55124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //   namespace N {
56124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //     template <typename T> struct S {
57124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //       ~S();
58124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //     };
59124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //   }
60124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //
61124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //   void f(N::S<int>* s) {
62124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //     s->N::S<int>::~S();
63124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //   }
64124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  //
6593649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor  // See also PR6358 and PR6359.
66c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl  // For this reason, we're currently only doing the C++03 version of this
67c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl  // code; the C++0x version has to wait until we get a proper spec.
68124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  QualType SearchType;
69124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  DeclContext *LookupCtx = 0;
70124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  bool isDependent = false;
71124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  bool LookInScope = false;
72124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
73124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  // If we have an object type, it's because we are in a
74124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  // pseudo-destructor-expression or a member access expression, and
75124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  // we know what type we're looking for.
76124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  if (ObjectTypePtr)
77124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    SearchType = GetTypeFromParser(ObjectTypePtr);
78124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
79124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  if (SS.isSet()) {
8093649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor    NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
81dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8293649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor    bool AlreadySearched = false;
8393649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor    bool LookAtPrefix = true;
84c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    // C++ [basic.lookup.qual]p6:
85dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If a pseudo-destructor-name (5.2.4) contains a nested-name-specifier,
86c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    //   the type-names are looked up as types in the scope designated by the
87c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    //   nested-name-specifier. In a qualified-id of the form:
880099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //
890099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //     ::[opt] nested-name-specifier  ~ class-name
90124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    //
91c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    //   where the nested-name-specifier designates a namespace scope, and in
92c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    //   a qualified-id of the form:
93124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    //
940099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //     ::opt nested-name-specifier class-name ::  ~ class-name
95124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    //
96dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   the class-names are looked up as types in the scope designated by
97c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    //   the nested-name-specifier.
9893649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor    //
99c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    // Here, we check the first case (completely) and determine whether the
100dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // code below is permitted to look at the prefix of the
101c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    // nested-name-specifier.
102c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    DeclContext *DC = computeDeclContext(SS, EnteringContext);
103c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    if (DC && DC->isFileContext()) {
104c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl      AlreadySearched = true;
105c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl      LookupCtx = DC;
106c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl      isDependent = false;
107c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    } else if (DC && isa<CXXRecordDecl>(DC))
108c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl      LookAtPrefix = false;
109dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
110c0fee50ce432a63b7c4ee73e73a464af1bf388a0Sebastian Redl    // The second case from the C++03 rules quoted further above.
11193649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor    NestedNameSpecifier *Prefix = 0;
11293649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor    if (AlreadySearched) {
11393649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor      // Nothing left to do.
11493649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor    } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) {
11593649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor      CXXScopeSpec PrefixSS;
1167e384943ac136f6c2ea55c309108b83939cd4c21Douglas Gregor      PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data()));
11793649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor      LookupCtx = computeDeclContext(PrefixSS, EnteringContext);
11893649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor      isDependent = isDependentScopeSpecifier(PrefixSS);
11993649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor    } else if (ObjectTypePtr) {
120124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      LookupCtx = computeDeclContext(SearchType);
121124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      isDependent = SearchType->isDependentType();
122124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    } else {
123124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      LookupCtx = computeDeclContext(SS, EnteringContext);
12493649fdc5e0c46e26bcba06ad39aa80196d3df27Douglas Gregor      isDependent = LookupCtx && LookupCtx->isDependentContext();
125124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    }
126dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
127edc90500b1d2587bf0b698fada14537d6741fddfDouglas Gregor    LookInScope = false;
128124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  } else if (ObjectTypePtr) {
129124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // C++ [basic.lookup.classref]p3:
130124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    //   If the unqualified-id is ~type-name, the type-name is looked up
131124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    //   in the context of the entire postfix-expression. If the type T
132124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    //   of the object expression is of a class type C, the type-name is
133124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    //   also looked up in the scope of class C. At least one of the
134124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    //   lookups shall find a name that refers to (possibly
135124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    //   cv-qualified) T.
136124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    LookupCtx = computeDeclContext(SearchType);
137124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    isDependent = SearchType->isDependentType();
138dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    assert((isDependent || !SearchType->isIncompleteType()) &&
139124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor           "Caller should have completed object type");
140124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
141124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    LookInScope = true;
142124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  } else {
143124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // Perform lookup into the current scope (only).
144124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    LookInScope = true;
145124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  }
146124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
1477ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor  TypeDecl *NonMatchingTypeDecl = 0;
148124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName);
149124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  for (unsigned Step = 0; Step != 2; ++Step) {
150124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // Look for the name first in the computed lookup context (if we
1517ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor    // have one) and, if that fails to find a match, in the scope (if
152124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // we're allowed to look there).
153124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    Found.clear();
154124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (Step == 0 && LookupCtx)
155124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      LookupQualifiedName(Found, LookupCtx);
156a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor    else if (Step == 1 && LookInScope && S)
157124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      LookupName(Found, S);
158124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    else
159124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      continue;
160124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
161124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // FIXME: Should we be suppressing ambiguities here?
162124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (Found.isAmbiguous())
163b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParsedType();
164124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
165124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) {
166124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      QualType T = Context.getTypeDeclType(Type);
167124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
168124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      if (SearchType.isNull() || SearchType->isDependentType() ||
169124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          Context.hasSameUnqualifiedType(T, SearchType)) {
170124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        // We found our type!
171124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
172b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        return ParsedType::make(T);
173124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      }
17436784e78bcce1dbaf35f94a655394e348b4d9ac7John Wiegley
1757ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor      if (!SearchType.isNull())
1767ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor        NonMatchingTypeDecl = Type;
177124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    }
178124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
179124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // If the name that we found is a class template name, and it is
180124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // the same name as the template name in the last part of the
181124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // nested-name-specifier (if present) or the object type, then
182124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // this is the destructor for that class.
183124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // FIXME: This is a workaround until we get real drafting for core
184dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // issue 399, for which there isn't even an obvious direction.
185124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) {
186124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      QualType MemberOfType;
187124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      if (SS.isSet()) {
188124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) {
189124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          // Figure out the type of the context, if it has one.
1903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx))
1913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            MemberOfType = Context.getTypeDeclType(Record);
192124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        }
193124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      }
194124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      if (MemberOfType.isNull())
195124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        MemberOfType = SearchType;
196dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
197124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      if (MemberOfType.isNull())
198124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        continue;
199124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
200124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      // We're referring into a class template specialization. If the
201124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      // class template we found is the same as the template being
202124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      // specialized, we found what we are looking for.
203124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      if (const RecordType *Record = MemberOfType->getAs<RecordType>()) {
204124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        if (ClassTemplateSpecializationDecl *Spec
205124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor              = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
206124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          if (Spec->getSpecializedTemplate()->getCanonicalDecl() ==
207124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor                Template->getCanonicalDecl())
208b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall            return ParsedType::make(MemberOfType);
209124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        }
210124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
211124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        continue;
212124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      }
213dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
214124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      // We're referring to an unresolved class template
215124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      // specialization. Determine whether we class template we found
216124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      // is the same as the template being specialized or, if we don't
217124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      // know which template is being specialized, that it at least
218124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      // has the same name.
219124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      if (const TemplateSpecializationType *SpecType
220124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor            = MemberOfType->getAs<TemplateSpecializationType>()) {
221124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        TemplateName SpecName = SpecType->getTemplateName();
222124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
223124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        // The class template we found is the same template being
224124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        // specialized.
225124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) {
226124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl())
227b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall            return ParsedType::make(MemberOfType);
228124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
229124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          continue;
230124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        }
231124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
232124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        // The class template we found has the same name as the
233124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        // (dependent) template name being specialized.
234dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        if (DependentTemplateName *DepTemplate
235124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor                                    = SpecName.getAsDependentTemplateName()) {
236124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          if (DepTemplate->isIdentifier() &&
237124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor              DepTemplate->getIdentifier() == Template->getIdentifier())
238b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall            return ParsedType::make(MemberOfType);
239124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
240124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor          continue;
241124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor        }
242124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor      }
243124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    }
244124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  }
245124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
246124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  if (isDependent) {
247124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // We didn't find our type, but that's okay: it's dependent
248124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    // anyway.
249e29425bd22fbb9200bbec7b743197b9c6dad3e40Douglas Gregor
250e29425bd22fbb9200bbec7b743197b9c6dad3e40Douglas Gregor    // FIXME: What if we have no nested-name-specifier?
251e29425bd22fbb9200bbec7b743197b9c6dad3e40Douglas Gregor    QualType T = CheckTypenameType(ETK_None, SourceLocation(),
252e29425bd22fbb9200bbec7b743197b9c6dad3e40Douglas Gregor                                   SS.getWithLocInContext(Context),
253e29425bd22fbb9200bbec7b743197b9c6dad3e40Douglas Gregor                                   II, NameLoc);
254b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    return ParsedType::make(T);
255124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  }
256124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
2577ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor  if (NonMatchingTypeDecl) {
2587ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor    QualType T = Context.getTypeDeclType(NonMatchingTypeDecl);
2597ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor    Diag(NameLoc, diag::err_destructor_expr_type_mismatch)
2607ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor      << T << SearchType;
2617ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor    Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here)
2627ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor      << T;
2637ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor  } else if (ObjectTypePtr)
2647ec1873d694cf870264694d2b61219a03492bc30Douglas Gregor    Diag(NameLoc, diag::err_ident_in_dtor_not_a_type)
265dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      << &II;
266124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor  else
267124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor    Diag(NameLoc, diag::err_destructor_class_name);
268124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
269b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType();
270124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor}
271124b878dba5007df0a268ea128a6ad8dc5dd2c5eDouglas Gregor
27253a75c07dbe79b3dd5dd88a0378aefa18f793083David BlaikieParsedType Sema::getDestructorType(const DeclSpec& DS, ParsedType ObjectType) {
2734db8c4483ea7b271bcce3a0312a0ac434313c09bDavid Blaikie    if (DS.getTypeSpecType() == DeclSpec::TST_error || !ObjectType)
27453a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      return ParsedType();
27553a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    assert(DS.getTypeSpecType() == DeclSpec::TST_decltype
27653a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie           && "only get destructor types from declspecs");
27753a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
27853a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    QualType SearchType = GetTypeFromParser(ObjectType);
27953a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    if (SearchType->isDependentType() || Context.hasSameUnqualifiedType(SearchType, T)) {
28053a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      return ParsedType::make(T);
28153a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    }
28253a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie
28353a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch)
28453a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie      << T << SearchType;
28553a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie    return ParsedType();
28653a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie}
28753a75c07dbe79b3dd5dd88a0378aefa18f793083David Blaikie
28857fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor/// \brief Build a C++ typeid expression with a type operand.
28960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::BuildCXXTypeId(QualType TypeInfoType,
2904eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor                                SourceLocation TypeidLoc,
2914eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor                                TypeSourceInfo *Operand,
2924eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor                                SourceLocation RParenLoc) {
29357fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor  // C++ [expr.typeid]p4:
294dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   The top-level cv-qualifiers of the lvalue expression or the type-id
29557fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor  //   that is the operand of typeid are always ignored.
296dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If the type of the type-id is a class type or a reference to a class
29757fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor  //   type, the class shall be completely-defined.
298d1c1d7bd14dce533e8755164ff59988f2ea5da94Douglas Gregor  Qualifiers Quals;
299d1c1d7bd14dce533e8755164ff59988f2ea5da94Douglas Gregor  QualType T
300d1c1d7bd14dce533e8755164ff59988f2ea5da94Douglas Gregor    = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(),
301d1c1d7bd14dce533e8755164ff59988f2ea5da94Douglas Gregor                                      Quals);
30257fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor  if (T->getAs<RecordType>() &&
30357fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid))
30457fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    return ExprError();
305dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
30657fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor  return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(),
30757fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor                                           Operand,
30857fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor                                           SourceRange(TypeidLoc, RParenLoc)));
30957fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor}
31057fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor
31157fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor/// \brief Build a C++ typeid expression with an expression operand.
31260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::BuildCXXTypeId(QualType TypeInfoType,
3134eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor                                SourceLocation TypeidLoc,
3144eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor                                Expr *E,
3154eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor                                SourceLocation RParenLoc) {
31657fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor  if (E && !E->isTypeDependent()) {
3176dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    if (E->getType()->isPlaceholderType()) {
3186dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      ExprResult result = CheckPlaceholderExpr(E);
3196dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      if (result.isInvalid()) return ExprError();
3206dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      E = result.take();
3216dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    }
3226dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
32357fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    QualType T = E->getType();
32457fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    if (const RecordType *RecordT = T->getAs<RecordType>()) {
32557fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl());
32657fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      // C++ [expr.typeid]p3:
32757fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      //   [...] If the type of the expression is a class type, the class
32857fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      //   shall be completely-defined.
32957fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid))
33057fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor        return ExprError();
331dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
33257fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      // C++ [expr.typeid]p3:
333906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl      //   When typeid is applied to an expression other than an glvalue of a
33457fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      //   polymorphic class type [...] [the] expression is an unevaluated
33557fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      //   operand. [...]
3360d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith      if (RecordD->isPolymorphic() && E->isGLValue()) {
337ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman        // The subexpression is potentially evaluated; switch the context
338ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman        // and recheck the subexpression.
339ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman        ExprResult Result = TranformToPotentiallyEvaluated(E);
340ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman        if (Result.isInvalid()) return ExprError();
341ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman        E = Result.take();
3426fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
3436fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor        // We require a vtable to query the type at run time.
3446fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor        MarkVTableUsed(TypeidLoc, RecordD);
3456fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      }
34657fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    }
347dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
34857fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    // C++ [expr.typeid]p4:
34957fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    //   [...] If the type of the type-id is a reference to a possibly
350dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   cv-qualified type, the result of the typeid expression refers to a
351dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   std::type_info object representing the cv-unqualified referenced
35257fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    //   type.
353d1c1d7bd14dce533e8755164ff59988f2ea5da94Douglas Gregor    Qualifiers Quals;
354d1c1d7bd14dce533e8755164ff59988f2ea5da94Douglas Gregor    QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals);
355d1c1d7bd14dce533e8755164ff59988f2ea5da94Douglas Gregor    if (!Context.hasSameType(T, UnqualT)) {
356d1c1d7bd14dce533e8755164ff59988f2ea5da94Douglas Gregor      T = UnqualT;
357c1c0dfb376b829b94d4c61e9f358ce23e6aa3169Eli Friedman      E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).take();
35857fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    }
35957fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor  }
360dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
36157fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor  return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(),
3629ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                           E,
363dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                           SourceRange(TypeidLoc, RParenLoc)));
36457fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor}
36557fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor
36657fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor/// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression);
36760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
368c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian RedlSema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc,
369c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl                     bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
37057fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor  // Find the std::type_info type.
371ce0682ff4647b885d41847b0f97918504282c2e8Sebastian Redl  if (!getStdNamespace())
372f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl    return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
373e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
3744eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor  if (!CXXTypeInfoDecl) {
3754eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info");
3764eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName);
3774eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    LookupQualifiedName(R, getStdNamespace());
3784eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    CXXTypeInfoDecl = R.getAsSingle<RecordDecl>();
379ed36b2a80878c29603bdc89a7969253fb6446174Nico Weber    // Microsoft's typeinfo doesn't have type_info in std but in the global
380ed36b2a80878c29603bdc89a7969253fb6446174Nico Weber    // namespace if _HAS_EXCEPTIONS is defined to 0. See PR13153.
381ed36b2a80878c29603bdc89a7969253fb6446174Nico Weber    if (!CXXTypeInfoDecl && LangOpts.MicrosoftMode) {
382ed36b2a80878c29603bdc89a7969253fb6446174Nico Weber      LookupQualifiedName(R, Context.getTranslationUnitDecl());
383ed36b2a80878c29603bdc89a7969253fb6446174Nico Weber      CXXTypeInfoDecl = R.getAsSingle<RecordDecl>();
384ed36b2a80878c29603bdc89a7969253fb6446174Nico Weber    }
3854eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    if (!CXXTypeInfoDecl)
3864eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor      return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
3874eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor  }
388dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
38911d1a697dcf677c42394dfa9506ace4935f3b00dNico Weber  if (!getLangOpts().RTTI) {
39011d1a697dcf677c42394dfa9506ace4935f3b00dNico Weber    return ExprError(Diag(OpLoc, diag::err_no_typeid_with_fno_rtti));
39111d1a697dcf677c42394dfa9506ace4935f3b00dNico Weber  }
39211d1a697dcf677c42394dfa9506ace4935f3b00dNico Weber
3934eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor  QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl);
394dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
39557fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor  if (isType) {
39657fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    // The operand is a type; handle it as such.
39757fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    TypeSourceInfo *TInfo = 0;
398b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr),
399b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   &TInfo);
40057fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    if (T.isNull())
40157fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      return ExprError();
402dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
40357fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    if (!TInfo)
40457fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor      TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc);
405c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
40657fdc8a4382164955c7b30d09f4ce46fc7e67659Douglas Gregor    return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc);
407ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  }
4081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
409dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // The operand is an expression.
4109ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc);
41101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet}
41201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
4136915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet/// Retrieve the UuidAttr associated with QT.
4146915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichetstatic UuidAttr *GetUuidAttrOfType(QualType QT) {
4156915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet  // Optionally remove one level of pointer, reference or array indirection.
4161ad23d62007162df82b58bca31b4aa277a5f6586Dmitri Gribenko  const Type *Ty = QT.getTypePtr();
417913b7bf8c40454641588611b7bbad981dc53c882Francois Pichet  if (QT->isPointerType() || QT->isReferenceType())
418913b7bf8c40454641588611b7bbad981dc53c882Francois Pichet    Ty = QT->getPointeeType().getTypePtr();
419913b7bf8c40454641588611b7bbad981dc53c882Francois Pichet  else if (QT->isArrayType())
420913b7bf8c40454641588611b7bbad981dc53c882Francois Pichet    Ty = cast<ArrayType>(QT)->getElementType().getTypePtr();
421913b7bf8c40454641588611b7bbad981dc53c882Francois Pichet
4228db75a2b9c963a3bc0bf928e711353552f4fef79Francois Pichet  // Loop all record redeclaration looking for an uuid attribute.
4236915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet  CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
4248db75a2b9c963a3bc0bf928e711353552f4fef79Francois Pichet  for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
4258db75a2b9c963a3bc0bf928e711353552f4fef79Francois Pichet       E = RD->redecls_end(); I != E; ++I) {
4268db75a2b9c963a3bc0bf928e711353552f4fef79Francois Pichet    if (UuidAttr *Uuid = I->getAttr<UuidAttr>())
4276915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet      return Uuid;
4286915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet  }
4298db75a2b9c963a3bc0bf928e711353552f4fef79Francois Pichet
4306915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet  return 0;
431913b7bf8c40454641588611b7bbad981dc53c882Francois Pichet}
432913b7bf8c40454641588611b7bbad981dc53c882Francois Pichet
43301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// \brief Build a Microsoft __uuidof expression with a type operand.
43401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetExprResult Sema::BuildCXXUuidof(QualType TypeInfoType,
43501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                SourceLocation TypeidLoc,
43601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                TypeSourceInfo *Operand,
43701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                SourceLocation RParenLoc) {
4386915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet  if (!Operand->getType()->isDependentType()) {
4396915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet    if (!GetUuidAttrOfType(Operand->getType()))
4406915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet      return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid));
4416915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet  }
442dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
44301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  // FIXME: add __uuidof semantic analysis for type operand.
44401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(),
44501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                           Operand,
44601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                           SourceRange(TypeidLoc, RParenLoc)));
44701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet}
44801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
44901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// \brief Build a Microsoft __uuidof expression with an expression operand.
45001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetExprResult Sema::BuildCXXUuidof(QualType TypeInfoType,
45101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                SourceLocation TypeidLoc,
45201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                Expr *E,
45301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                SourceLocation RParenLoc) {
4546915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet  if (!E->getType()->isDependentType()) {
455dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (!GetUuidAttrOfType(E->getType()) &&
4566915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet        !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
4576915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet      return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid));
4586915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet  }
4596915c529dbb43073dad148c2e83a1c56bfcc05c8Francois Pichet  // FIXME: add __uuidof semantic analysis for type operand.
46001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(),
46101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                           E,
462dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                           SourceRange(TypeidLoc, RParenLoc)));
46301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet}
46401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
46501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet/// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression);
46601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetExprResult
46701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois PichetSema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc,
46801b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                     bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
469dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // If MSVCGuidDecl has not been cached, do the lookup.
47001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  if (!MSVCGuidDecl) {
47101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID");
47201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    LookupResult R(*this, GuidII, SourceLocation(), LookupTagName);
47301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    LookupQualifiedName(R, Context.getTranslationUnitDecl());
47401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    MSVCGuidDecl = R.getAsSingle<RecordDecl>();
47501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (!MSVCGuidDecl)
47601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof));
477dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  }
478dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
47901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl);
480dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
48101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  if (isType) {
48201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    // The operand is a type; handle it as such.
48301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    TypeSourceInfo *TInfo = 0;
48401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr),
48501b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet                                   &TInfo);
48601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (T.isNull())
48701b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      return ExprError();
488dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
48901b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    if (!TInfo)
49001b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet      TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc);
49101b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
49201b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet    return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc);
49301b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  }
49401b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet
495dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // The operand is an expression.
49601b7c3028da5bbcb9f8e52ba67e4613070de0e60Francois Pichet  return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc);
497c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl}
498c42e1183846228a7fa5143ad76507d6d60f5c6f3Sebastian Redl
4991b273c403734d343d720acb28f04011807c8aa56Steve Naroff/// ActOnCXXBoolLiteral - Parse {true,false} literals.
50060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
5011b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
5022f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
5035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Unknown C++ Boolean value!");
504f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true,
505f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                                Context.BoolTy, OpLoc));
5065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
50750dd289f45738ed22b7583d52ed2525b927042ffChris Lattner
5086e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl/// ActOnCXXNullPtrLiteral - Parse 'nullptr'.
50960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
5106e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian RedlSema::ActOnCXXNullPtrLiteral(SourceLocation Loc) {
5116e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
5126e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl}
5136e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
51450dd289f45738ed22b7583d52ed2525b927042ffChris Lattner/// ActOnCXXThrow - Parse throw expressions.
51560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
516bca01b46850f867b2f4137f25c882022b58f8471Douglas GregorSema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) {
517bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  bool IsThrownVarInScope = false;
518bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  if (Ex) {
519bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    // C++0x [class.copymove]p31:
520bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    //   When certain criteria are met, an implementation is allowed to omit the
521bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    //   copy/move construction of a class object [...]
522bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    //
523bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    //     - in a throw-expression, when the operand is the name of a
524bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    //       non-volatile automatic object (other than a function or catch-
525bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    //       clause parameter) whose scope does not extend beyond the end of the
526bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    //       innermost enclosing try-block (if there is one), the copy/move
527bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    //       operation from the operand to the exception object (15.1) can be
528bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    //       omitted by constructing the automatic object directly into the
529bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    //       exception object
530bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens()))
531bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
532bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor        if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) {
533bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor          for( ; S; S = S->getParent()) {
534bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor            if (S->isDeclScope(Var)) {
535bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor              IsThrownVarInScope = true;
536bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor              break;
537bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor            }
538bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor
539bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor            if (S->getFlags() &
540bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor                (Scope::FnScope | Scope::ClassScope | Scope::BlockScope |
541bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor                 Scope::FunctionPrototypeScope | Scope::ObjCMethodScope |
542bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor                 Scope::TryScope))
543bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor              break;
544bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor          }
545bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor        }
546bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor      }
547bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  }
548bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor
549bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope);
550bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor}
551bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor
552bca01b46850f867b2f4137f25c882022b58f8471Douglas GregorExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex,
553bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor                               bool IsThrownVarInScope) {
554729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson  // Don't report an error if 'throw' is used in system headers.
5554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CXXExceptions &&
556729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson      !getSourceManager().isInSystemHeader(OpLoc))
557b1fba3145392dc0cf54691576a4e6141e5081869Anders Carlsson    Diag(OpLoc, diag::err_exceptions_disabled) << "throw";
558bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor
559429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (Ex && !Ex->isTypeDependent()) {
560bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    ExprResult ExRes = CheckCXXThrowOperand(OpLoc, Ex, IsThrownVarInScope);
561429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (ExRes.isInvalid())
562429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
563429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    Ex = ExRes.take();
564429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
565bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor
566bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc,
567bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor                                          IsThrownVarInScope));
568972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl}
569972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl
570972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl/// CheckCXXThrowOperand - Validate the operand of a throw.
571bca01b46850f867b2f4137f25c882022b58f8471Douglas GregorExprResult Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *E,
572bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor                                      bool IsThrownVarInScope) {
573972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl  // C++ [except.throw]p3:
574154fe9812faddcd94568a64aee5f3cb0d47003d9Douglas Gregor  //   A throw-expression initializes a temporary object, called the exception
575154fe9812faddcd94568a64aee5f3cb0d47003d9Douglas Gregor  //   object, the type of which is determined by removing any top-level
576154fe9812faddcd94568a64aee5f3cb0d47003d9Douglas Gregor  //   cv-qualifiers from the static type of the operand of throw and adjusting
577dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the type from "array of T" or "function returning T" to "pointer to T"
578154fe9812faddcd94568a64aee5f3cb0d47003d9Douglas Gregor  //   or "pointer to function returning T", [...]
579154fe9812faddcd94568a64aee5f3cb0d47003d9Douglas Gregor  if (E->getType().hasQualifiers())
580429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    E = ImpCastExprToType(E, E->getType().getUnqualifiedType(), CK_NoOp,
581c1c0dfb376b829b94d4c61e9f358ce23e6aa3169Eli Friedman                          E->getValueKind()).take();
582dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
583429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  ExprResult Res = DefaultFunctionArrayConversion(E);
584429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (Res.isInvalid())
585429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return ExprError();
586429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  E = Res.take();
587972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl
588972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl  //   If the type of the exception would be an incomplete type or a pointer
589972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl  //   to an incomplete type other than (cv) void the program is ill-formed.
590972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl  QualType Ty = E->getType();
591ac418162692a951ca3796d6830496a85a2d12493John McCall  bool isPointer = false;
5926217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType* Ptr = Ty->getAs<PointerType>()) {
593972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl    Ty = Ptr->getPointeeType();
594ac418162692a951ca3796d6830496a85a2d12493John McCall    isPointer = true;
595972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl  }
596972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl  if (!isPointer || !Ty->isVoidType()) {
597972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl    if (RequireCompleteType(ThrowLoc, Ty,
598d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                            isPointer? diag::err_throw_incomplete_ptr
599d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                     : diag::err_throw_incomplete,
600d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                            E->getSourceRange()))
601429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
6027b9a5aa7c0d76f577699d25ce6afe21ecccb60b7Rafael Espindola
603bf422f9d7e2e3454b2296b02202f4d5ae12644f6Douglas Gregor    if (RequireNonAbstractType(ThrowLoc, E->getType(),
6046a26e2e54aa2a8cc6c977081befd8e80e7573ca4Douglas Gregor                               diag::err_throw_abstract_type, E))
605429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
606972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl  }
607972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl
608ac418162692a951ca3796d6830496a85a2d12493John McCall  // Initialize the exception result.  This implicitly weeds out
609ac418162692a951ca3796d6830496a85a2d12493John McCall  // abstract types or types with inaccessible copy constructors.
610bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor
611bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  // C++0x [class.copymove]p31:
612bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  //   When certain criteria are met, an implementation is allowed to omit the
613bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  //   copy/move construction of a class object [...]
614bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  //
615bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  //     - in a throw-expression, when the operand is the name of a
616bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  //       non-volatile automatic object (other than a function or catch-clause
617bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  //       parameter) whose scope does not extend beyond the end of the
618bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  //       innermost enclosing try-block (if there is one), the copy/move
619bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  //       operation from the operand to the exception object (15.1) can be
620bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  //       omitted by constructing the automatic object directly into the
621bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  //       exception object
622bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  const VarDecl *NRVOVariable = 0;
623bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor  if (IsThrownVarInScope)
624bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor    NRVOVariable = getCopyElisionCandidate(QualType(), E, false);
625bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor
626ac418162692a951ca3796d6830496a85a2d12493John McCall  InitializedEntity Entity =
62772dfa27b415b15157a9d1fc33b6ed21f0085bed2Douglas Gregor      InitializedEntity::InitializeException(ThrowLoc, E->getType(),
628bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor                                             /*NRVO=*/NRVOVariable != 0);
629429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  Res = PerformMoveOrCopyInitialization(Entity, NRVOVariable,
630bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor                                        QualType(), E,
631bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor                                        IsThrownVarInScope);
632ac418162692a951ca3796d6830496a85a2d12493John McCall  if (Res.isInvalid())
633429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return ExprError();
634429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  E = Res.take();
6356fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
6365ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman  // If the exception has class type, we need additional handling.
6375ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman  const RecordType *RecordTy = Ty->getAs<RecordType>();
6385ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman  if (!RecordTy)
639429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return Owned(E);
6405ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman  CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
6415ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman
6426fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // If we are throwing a polymorphic class type or pointer thereof,
6436fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // exception handling will make use of the vtable.
6445ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman  MarkVTableUsed(ThrowLoc, RD);
6455ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman
64698efb9f6df133f5508d260c4510c6c3bd70f34adEli Friedman  // If a pointer is thrown, the referenced object will not be destroyed.
64798efb9f6df133f5508d260c4510c6c3bd70f34adEli Friedman  if (isPointer)
648429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return Owned(E);
64998efb9f6df133f5508d260c4510c6c3bd70f34adEli Friedman
650213d70b58b4f48050c3e545ce1bd4b0ec3af74beRichard Smith  // If the class has a destructor, we must be able to call it.
651213d70b58b4f48050c3e545ce1bd4b0ec3af74beRichard Smith  if (RD->hasIrrelevantDestructor())
652429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return Owned(E);
6535ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman
6542835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl  CXXDestructorDecl *Destructor = LookupDestructor(RD);
6555ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman  if (!Destructor)
656429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return Owned(E);
6575ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman
6585f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  MarkFunctionReferenced(E->getExprLoc(), Destructor);
6595ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman  CheckDestructorAccess(E->getExprLoc(), Destructor,
660ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor                        PDiag(diag::err_access_dtor_exception) << Ty);
661213d70b58b4f48050c3e545ce1bd4b0ec3af74beRichard Smith  DiagnoseUseOfDecl(Destructor, E->getExprLoc());
662429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  return Owned(E);
66350dd289f45738ed22b7583d52ed2525b927042ffChris Lattner}
66407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis
66572899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli FriedmanQualType Sema::getCurrentThisType() {
66672899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  DeclContext *DC = getFunctionLevelDeclContext();
667cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  QualType ThisTy = CXXThisTypeOverride;
6687a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) {
6697a614d8380297fcd2bc23986241905d97222948cRichard Smith    if (method && method->isInstance())
6707a614d8380297fcd2bc23986241905d97222948cRichard Smith      ThisTy = method->getThisType(Context);
6717a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
672cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
6737a614d8380297fcd2bc23986241905d97222948cRichard Smith  return ThisTy;
6745808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall}
6755808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall
676cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas GregorSema::CXXThisScopeRAII::CXXThisScopeRAII(Sema &S,
677cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                         Decl *ContextDecl,
678cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                         unsigned CXXThisTypeQuals,
679cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                         bool Enabled)
680cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  : S(S), OldCXXThisTypeOverride(S.CXXThisTypeOverride), Enabled(false)
681cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor{
682cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (!Enabled || !ContextDecl)
683cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    return;
684cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
685cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  CXXRecordDecl *Record = 0;
686cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(ContextDecl))
687cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    Record = Template->getTemplatedDecl();
688cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  else
689cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    Record = cast<CXXRecordDecl>(ContextDecl);
690cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
691cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  S.CXXThisTypeOverride
692cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    = S.Context.getPointerType(
693cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor        S.Context.getRecordType(Record).withCVRQualifiers(CXXThisTypeQuals));
694cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
695cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  this->Enabled = true;
696cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor}
697cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
698cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
699cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas GregorSema::CXXThisScopeRAII::~CXXThisScopeRAII() {
700cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (Enabled) {
701cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    S.CXXThisTypeOverride = OldCXXThisTypeOverride;
702cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  }
703cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor}
704cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
705a1f2114d9e81923c750f6b439302ac03552c37dbDouglas Gregorvoid Sema::CheckCXXThisCapture(SourceLocation Loc, bool Explicit) {
70672899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  // We don't need to capture this in an unevaluated context.
707a1f2114d9e81923c750f6b439302ac03552c37dbDouglas Gregor  if (ExprEvalContexts.back().Context == Unevaluated && !Explicit)
70872899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    return;
70972899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman
71072899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  // Otherwise, check that we can capture 'this'.
71172899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  unsigned NumClosures = 0;
71272899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  for (unsigned idx = FunctionScopes.size() - 1; idx != 0; idx--) {
713b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman    if (CapturingScopeInfo *CSI =
714b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman            dyn_cast<CapturingScopeInfo>(FunctionScopes[idx])) {
715b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman      if (CSI->CXXThisCaptureIndex != 0) {
716b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman        // 'this' is already being captured; there isn't anything more to do.
71772899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman        break;
71872899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman      }
719a1f2114d9e81923c750f6b439302ac03552c37dbDouglas Gregor
720b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman      if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByref ||
7213ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor          CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByval ||
722a1f2114d9e81923c750f6b439302ac03552c37dbDouglas Gregor          CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_Block ||
723a1f2114d9e81923c750f6b439302ac03552c37dbDouglas Gregor          Explicit) {
724a1f2114d9e81923c750f6b439302ac03552c37dbDouglas Gregor        // This closure can capture 'this'; continue looking upwards.
72572899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman        NumClosures++;
726a1f2114d9e81923c750f6b439302ac03552c37dbDouglas Gregor        Explicit = false;
72772899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman        continue;
72872899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman      }
729b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman      // This context can't implicitly capture 'this'; fail out.
730a1f2114d9e81923c750f6b439302ac03552c37dbDouglas Gregor      Diag(Loc, diag::err_this_capture) << Explicit;
73172899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman      return;
73272899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    }
73372899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    break;
73472899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  }
73572899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman
73672899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  // Mark that we're implicitly capturing 'this' in all the scopes we skipped.
73772899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  // FIXME: We need to delay this marking in PotentiallyPotentiallyEvaluated
73872899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  // contexts.
73972899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  for (unsigned idx = FunctionScopes.size() - 1;
74072899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman       NumClosures; --idx, --NumClosures) {
741b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman    CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[idx]);
742668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman    Expr *ThisExpr = 0;
743999713eea940f4e087cc3ac878689c5c5c7a7225Douglas Gregor    QualType ThisTy = getCurrentThisType();
744668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman    if (LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI)) {
745668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman      // For lambda expressions, build a field and an initializing expression.
746668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman      CXXRecordDecl *Lambda = LSI->Lambda;
747668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman      FieldDecl *Field
748668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman        = FieldDecl::Create(Context, Lambda, Loc, Loc, 0, ThisTy,
749668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman                            Context.getTrivialTypeSourceInfo(ThisTy, Loc),
750ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                            0, false, ICIS_NoInit);
751668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman      Field->setImplicit(true);
752668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman      Field->setAccess(AS_private);
753668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman      Lambda->addDecl(Field);
754668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman      ThisExpr = new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/true);
755668165ab1e604b063c0aa0df8ff91c80879670bfEli Friedman    }
756b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman    bool isNested = NumClosures > 1;
757999713eea940f4e087cc3ac878689c5c5c7a7225Douglas Gregor    CSI->addThisCapture(isNested, Loc, ThisTy, ThisExpr);
75872899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  }
75972899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman}
76072899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman
7617a614d8380297fcd2bc23986241905d97222948cRichard SmithExprResult Sema::ActOnCXXThis(SourceLocation Loc) {
7625808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall  /// C++ 9.3.2: In the body of a non-static member function, the keyword this
7635808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall  /// is a non-lvalue expression whose value is the address of the object for
7645808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall  /// which the function is called.
7655808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall
766341350ee62abd1ad818e1e3d926cd718960e439bDouglas Gregor  QualType ThisTy = getCurrentThisType();
7677a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use);
7685808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall
76972899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  CheckCXXThisCapture(Loc);
7707a614d8380297fcd2bc23986241905d97222948cRichard Smith  return Owned(new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false));
77107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis}
772987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
773cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregorbool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) {
774cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  // If we're outside the body of a member function, then we'll have a specified
775cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  // type for 'this'.
776cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (CXXThisTypeOverride.isNull())
777cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    return false;
778cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
779cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  // Determine whether we're looking into a class that's currently being
780cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  // defined.
781cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  CXXRecordDecl *Class = BaseType->getAsCXXRecordDecl();
782cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  return Class && Class->isBeingDefined();
783cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor}
784cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
78560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
786ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas GregorSema::ActOnCXXTypeConstructExpr(ParsedType TypeRep,
787987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis                                SourceLocation LParenLoc,
788f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                                MultiExprArg exprs,
789987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis                                SourceLocation RParenLoc) {
790ae4c77dc8a77ee89e5b2de8003283249e38075c3Douglas Gregor  if (!TypeRep)
791ae4c77dc8a77ee89e5b2de8003283249e38075c3Douglas Gregor    return ExprError();
792dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
7939d125033a9853f3b572a4c9e2f9e2d4e5e346973John McCall  TypeSourceInfo *TInfo;
7949d125033a9853f3b572a4c9e2f9e2d4e5e346973John McCall  QualType Ty = GetTypeFromParser(TypeRep, &TInfo);
7959d125033a9853f3b572a4c9e2f9e2d4e5e346973John McCall  if (!TInfo)
7969d125033a9853f3b572a4c9e2f9e2d4e5e346973John McCall    TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation());
797ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor
798ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc);
799ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor}
800ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor
801ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
802ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor/// Can be interpreted either as function-style casting ("int(x)")
803ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor/// or class type construction ("ClassType(x,y,z)")
804ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor/// or creation of a value-initialized type ("int()").
805ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas GregorExprResult
806ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas GregorSema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
807ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor                                SourceLocation LParenLoc,
808ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor                                MultiExprArg exprs,
809ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor                                SourceLocation RParenLoc) {
810ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  QualType Ty = TInfo->getType();
811ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor  SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc();
812987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
8133b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  if (Ty->isDependentType() || CallExpr::hasAnyTypeDependentArguments(exprs)) {
814ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor    return Owned(CXXUnresolvedConstructExpr::Create(Context, TInfo,
815d81e6ca6e378c3996a139066a5c4b7fc1869630cDouglas Gregor                                                    LParenLoc,
8163b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                                    exprs,
817d81e6ca6e378c3996a139066a5c4b7fc1869630cDouglas Gregor                                                    RParenLoc));
818ba49817c5b9f502602672861cf369fd0e53966e8Douglas Gregor  }
819ba49817c5b9f502602672861cf369fd0e53966e8Douglas Gregor
8203b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  unsigned NumExprs = exprs.size();
8213b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer  Expr **Exprs = exprs.data();
8223b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer
8236dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl  bool ListInitialization = LParenLoc.isInvalid();
8246dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl  assert((!ListInitialization || (NumExprs == 1 && isa<InitListExpr>(Exprs[0])))
8256dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl         && "List initialization must have initializer list as expression.");
8266dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl  SourceRange FullRange = SourceRange(TyBeginLoc,
8276dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl      ListInitialization ? Exprs[0]->getSourceRange().getEnd() : RParenLoc);
8286dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl
829506ae418eb171d072f2fb4f6bc46d258b52cbf97Douglas Gregor  // C++ [expr.type.conv]p1:
830987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // If the expression list is a single expression, the type conversion
831987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // expression is equivalent (in definedness, and if defined in meaning) to the
832987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  // corresponding cast expression.
83320ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl  if (NumExprs == 1 && !ListInitialization) {
834b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall    Expr *Arg = Exprs[0];
835b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall    return BuildCXXFunctionalCastExpr(TInfo, LParenLoc, Arg, RParenLoc);
836987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis  }
837987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis
838c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman  QualType ElemTy = Ty;
839c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman  if (Ty->isArrayType()) {
840c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman    if (!ListInitialization)
841c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman      return ExprError(Diag(TyBeginLoc,
842c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman                            diag::err_value_init_for_array_type) << FullRange);
843c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman    ElemTy = Context.getBaseElementType(Ty);
844c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman  }
845c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman
846c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman  if (!Ty->isVoidType() &&
847c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman      RequireCompleteType(TyBeginLoc, ElemTy,
848d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                          diag::err_invalid_incomplete_type_use, FullRange))
849c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman    return ExprError();
850c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman
851c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman  if (RequireNonAbstractType(TyBeginLoc, Ty,
852c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman                             diag::err_allocation_of_abstract_type))
853c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman    return ExprError();
854c60ccf5b4fb657ca40da3019c2bbe15dd8ab9732Eli Friedman
85519311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor  InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo);
85619311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor  InitializationKind Kind
85720ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl    = NumExprs ? ListInitialization
85820ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl                    ? InitializationKind::CreateDirectList(TyBeginLoc)
85920ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl                    : InitializationKind::CreateDirect(TyBeginLoc,
86020ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl                                                       LParenLoc, RParenLoc)
861dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi               : InitializationKind::CreateValue(TyBeginLoc,
86219311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor                                                 LParenLoc, RParenLoc);
86319311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor  InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs);
8643fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  ExprResult Result = InitSeq.Perform(*this, Entity, Kind, exprs);
86519311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor
86620ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl  if (!Result.isInvalid() && ListInitialization &&
86720ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl      isa<InitListExpr>(Result.get())) {
86820ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl    // If the list-initialization doesn't involve a constructor call, we'll get
86920ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl    // the initializer-list (with corrected type) back, but that's not what we
87020ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl    // want, since it will be treated as an initializer list in further
87120ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl    // processing. Explicitly insert a cast here.
87220ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl    InitListExpr *List = cast<InitListExpr>(Result.take());
87320ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl    Result = Owned(CXXFunctionalCastExpr::Create(Context, List->getType(),
87420ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl                                    Expr::getValueKindForType(TInfo->getType()),
87520ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl                                                 TInfo, TyBeginLoc, CK_NoOp,
87620ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl                                                 List, /*Path=*/0, RParenLoc));
87720ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl  }
87820ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl
87919311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor  // FIXME: Improve AST representation?
8803fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
881987a14bf5883ef6e5d07f1c83eb6d41a8212a78cArgyrios Kyrtzidis}
8825921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis
8836ec278d1a354517e20f13a877481453ee7940c78John McCall/// doesUsualArrayDeleteWantSize - Answers whether the usual
8846ec278d1a354517e20f13a877481453ee7940c78John McCall/// operator delete[] for the given type has a size_t parameter.
8856ec278d1a354517e20f13a877481453ee7940c78John McCallstatic bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc,
8866ec278d1a354517e20f13a877481453ee7940c78John McCall                                         QualType allocType) {
8876ec278d1a354517e20f13a877481453ee7940c78John McCall  const RecordType *record =
8886ec278d1a354517e20f13a877481453ee7940c78John McCall    allocType->getBaseElementTypeUnsafe()->getAs<RecordType>();
8896ec278d1a354517e20f13a877481453ee7940c78John McCall  if (!record) return false;
8906ec278d1a354517e20f13a877481453ee7940c78John McCall
8916ec278d1a354517e20f13a877481453ee7940c78John McCall  // Try to find an operator delete[] in class scope.
8926ec278d1a354517e20f13a877481453ee7940c78John McCall
8936ec278d1a354517e20f13a877481453ee7940c78John McCall  DeclarationName deleteName =
8946ec278d1a354517e20f13a877481453ee7940c78John McCall    S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete);
8956ec278d1a354517e20f13a877481453ee7940c78John McCall  LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName);
8966ec278d1a354517e20f13a877481453ee7940c78John McCall  S.LookupQualifiedName(ops, record->getDecl());
8976ec278d1a354517e20f13a877481453ee7940c78John McCall
8986ec278d1a354517e20f13a877481453ee7940c78John McCall  // We're just doing this for information.
8996ec278d1a354517e20f13a877481453ee7940c78John McCall  ops.suppressDiagnostics();
9006ec278d1a354517e20f13a877481453ee7940c78John McCall
9016ec278d1a354517e20f13a877481453ee7940c78John McCall  // Very likely: there's no operator delete[].
9026ec278d1a354517e20f13a877481453ee7940c78John McCall  if (ops.empty()) return false;
9036ec278d1a354517e20f13a877481453ee7940c78John McCall
9046ec278d1a354517e20f13a877481453ee7940c78John McCall  // If it's ambiguous, it should be illegal to call operator delete[]
9056ec278d1a354517e20f13a877481453ee7940c78John McCall  // on this thing, so it doesn't matter if we allocate extra space or not.
9066ec278d1a354517e20f13a877481453ee7940c78John McCall  if (ops.isAmbiguous()) return false;
9076ec278d1a354517e20f13a877481453ee7940c78John McCall
9086ec278d1a354517e20f13a877481453ee7940c78John McCall  LookupResult::Filter filter = ops.makeFilter();
9096ec278d1a354517e20f13a877481453ee7940c78John McCall  while (filter.hasNext()) {
9106ec278d1a354517e20f13a877481453ee7940c78John McCall    NamedDecl *del = filter.next()->getUnderlyingDecl();
9116ec278d1a354517e20f13a877481453ee7940c78John McCall
9126ec278d1a354517e20f13a877481453ee7940c78John McCall    // C++0x [basic.stc.dynamic.deallocation]p2:
9136ec278d1a354517e20f13a877481453ee7940c78John McCall    //   A template instance is never a usual deallocation function,
9146ec278d1a354517e20f13a877481453ee7940c78John McCall    //   regardless of its signature.
9156ec278d1a354517e20f13a877481453ee7940c78John McCall    if (isa<FunctionTemplateDecl>(del)) {
9166ec278d1a354517e20f13a877481453ee7940c78John McCall      filter.erase();
9176ec278d1a354517e20f13a877481453ee7940c78John McCall      continue;
9186ec278d1a354517e20f13a877481453ee7940c78John McCall    }
9196ec278d1a354517e20f13a877481453ee7940c78John McCall
9206ec278d1a354517e20f13a877481453ee7940c78John McCall    // C++0x [basic.stc.dynamic.deallocation]p2:
9216ec278d1a354517e20f13a877481453ee7940c78John McCall    //   If class T does not declare [an operator delete[] with one
9226ec278d1a354517e20f13a877481453ee7940c78John McCall    //   parameter] but does declare a member deallocation function
9236ec278d1a354517e20f13a877481453ee7940c78John McCall    //   named operator delete[] with exactly two parameters, the
9246ec278d1a354517e20f13a877481453ee7940c78John McCall    //   second of which has type std::size_t, then this function
9256ec278d1a354517e20f13a877481453ee7940c78John McCall    //   is a usual deallocation function.
9266ec278d1a354517e20f13a877481453ee7940c78John McCall    if (!cast<CXXMethodDecl>(del)->isUsualDeallocationFunction()) {
9276ec278d1a354517e20f13a877481453ee7940c78John McCall      filter.erase();
9286ec278d1a354517e20f13a877481453ee7940c78John McCall      continue;
9296ec278d1a354517e20f13a877481453ee7940c78John McCall    }
9306ec278d1a354517e20f13a877481453ee7940c78John McCall  }
9316ec278d1a354517e20f13a877481453ee7940c78John McCall  filter.done();
9326ec278d1a354517e20f13a877481453ee7940c78John McCall
9336ec278d1a354517e20f13a877481453ee7940c78John McCall  if (!ops.isSingleResult()) return false;
9346ec278d1a354517e20f13a877481453ee7940c78John McCall
9356ec278d1a354517e20f13a877481453ee7940c78John McCall  const FunctionDecl *del = cast<FunctionDecl>(ops.getFoundDecl());
9366ec278d1a354517e20f13a877481453ee7940c78John McCall  return (del->getNumParams() == 2);
9376ec278d1a354517e20f13a877481453ee7940c78John McCall}
9385921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis
9396dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl/// \brief Parsed a C++ 'new' expression (C++ 5.3.4).
940ef2b5b327b524f9ea3243c07e04fb24706e63120James Dennett///
9416dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl/// E.g.:
9424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// @code new (memory) int[size][4] @endcode
9434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// or
9444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// @code ::new Foo(23, "hello") @endcode
9456dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl///
9466dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl/// \param StartLoc The first location of the expression.
9476dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl/// \param UseGlobal True if 'new' was prefixed with '::'.
9486dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl/// \param PlacementLParen Opening paren of the placement arguments.
9496dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl/// \param PlacementArgs Placement new arguments.
9506dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl/// \param PlacementRParen Closing paren of the placement arguments.
9516dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl/// \param TypeIdParens If the type is in parens, the source range.
9526dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl/// \param D The type to be allocated, as well as array dimensions.
953ef2b5b327b524f9ea3243c07e04fb24706e63120James Dennett/// \param Initializer The initializing expression or initializer-list, or null
954ef2b5b327b524f9ea3243c07e04fb24706e63120James Dennett///   if there is none.
95560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
9564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian RedlSema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal,
957f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl                  SourceLocation PlacementLParen, MultiExprArg PlacementArgs,
958dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                  SourceLocation PlacementRParen, SourceRange TypeIdParens,
9592aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                  Declarator &D, Expr *Initializer) {
96034b41d939a1328f484511c6002ba2456db879a29Richard Smith  bool TypeContainsAuto = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
96134b41d939a1328f484511c6002ba2456db879a29Richard Smith
962cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  Expr *ArraySize = 0;
963cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  // If the specified type is an array, unwrap it and save the expression.
964cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  if (D.getNumTypeObjects() > 0 &&
965cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl      D.getTypeObject(0).Kind == DeclaratorChunk::Array) {
966ef2b5b327b524f9ea3243c07e04fb24706e63120James Dennett     DeclaratorChunk &Chunk = D.getTypeObject(0);
96734b41d939a1328f484511c6002ba2456db879a29Richard Smith    if (TypeContainsAuto)
96834b41d939a1328f484511c6002ba2456db879a29Richard Smith      return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto)
96934b41d939a1328f484511c6002ba2456db879a29Richard Smith        << D.getSourceRange());
970cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (Chunk.Arr.hasStatic)
971f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl      return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new)
972f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl        << D.getSourceRange());
973cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    if (!Chunk.Arr.NumElts)
974f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl      return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size)
975f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl        << D.getSourceRange());
9768ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl
977cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts);
9788ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    D.DropFirstTypeObject();
979cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
980cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl
981043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor  // Every dimension shall be of constant size.
9828ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  if (ArraySize) {
9838ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) {
984043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      if (D.getTypeObject(I).Kind != DeclaratorChunk::Array)
985043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor        break;
986043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor
987043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr;
988043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      if (Expr *NumElts = (Expr *)Array.NumElts) {
989282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        if (!NumElts->isTypeDependent() && !NumElts->isValueDependent()) {
990ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor          Array.NumElts
991ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor            = VerifyIntegerConstantExpression(NumElts, 0,
992ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                              diag::err_new_array_nonconst)
993ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                .take();
994282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith          if (!Array.NumElts)
995282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith            return ExprError();
996043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor        }
997043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      }
998043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor    }
999043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor  }
10008ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl
10010b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/0);
1002bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType AllocType = TInfo->getType();
1003eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  if (D.isInvalidType())
1004f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl    return ExprError();
1005dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
10062aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  SourceRange DirectInitRange;
10072aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer))
10082aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    DirectInitRange = List->getSourceRange();
10092aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl
10101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return BuildCXXNew(StartLoc, UseGlobal,
10113433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor                     PlacementLParen,
10123fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                     PlacementArgs,
10133433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor                     PlacementRParen,
10144bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor                     TypeIdParens,
10151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     AllocType,
10161bb2a93ab7b1499dda6f6b58865bd0dce1864228Douglas Gregor                     TInfo,
10179ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                     ArraySize,
10182aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                     DirectInitRange,
10192aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                     Initializer,
102034b41d939a1328f484511c6002ba2456db879a29Richard Smith                     TypeContainsAuto);
10213433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor}
10223433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor
1023bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redlstatic bool isLegalArrayNewInitializer(CXXNewExpr::InitializationStyle Style,
1024bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl                                       Expr *Init) {
10252aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  if (!Init)
10262aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    return true;
10271f27805fe4f5463fd5b4d5396affe1ef23320688Sebastian Redl  if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init))
10281f27805fe4f5463fd5b4d5396affe1ef23320688Sebastian Redl    return PLE->getNumExprs() == 0;
10292aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  if (isa<ImplicitValueInitExpr>(Init))
10302aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    return true;
10312aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init))
10322aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    return !CCE->isListInitialization() &&
10332aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl           CCE->getConstructor()->isDefaultConstructor();
1034bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl  else if (Style == CXXNewExpr::ListInit) {
1035bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl    assert(isa<InitListExpr>(Init) &&
1036bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl           "Shouldn't create list CXXConstructExprs for arrays.");
1037bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl    return true;
1038bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl  }
10392aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  return false;
10402aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl}
10412aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl
104260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
10433433cf7dfda405bf51bba914a338adf645a87e3bDouglas GregorSema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal,
10443433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor                  SourceLocation PlacementLParen,
10453433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor                  MultiExprArg PlacementArgs,
10463433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor                  SourceLocation PlacementRParen,
10474bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor                  SourceRange TypeIdParens,
10483433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor                  QualType AllocType,
10491bb2a93ab7b1499dda6f6b58865bd0dce1864228Douglas Gregor                  TypeSourceInfo *AllocTypeInfo,
10509ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                  Expr *ArraySize,
10512aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                  SourceRange DirectInitRange,
10522aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                  Expr *Initializer,
105334b41d939a1328f484511c6002ba2456db879a29Richard Smith                  bool TypeMayContainAuto) {
10541bb2a93ab7b1499dda6f6b58865bd0dce1864228Douglas Gregor  SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange();
10554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
10562aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  CXXNewExpr::InitializationStyle initStyle;
10572aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  if (DirectInitRange.isValid()) {
10582aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    assert(Initializer && "Have parens but no initializer.");
10592aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    initStyle = CXXNewExpr::CallInit;
10602aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  } else if (Initializer && isa<InitListExpr>(Initializer))
10612aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    initStyle = CXXNewExpr::ListInit;
10622aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  else {
1063428c620478d513081399798db5550bf0c779f244Sebastian Redl    // In template instantiation, the initializer could be a CXXDefaultArgExpr
1064428c620478d513081399798db5550bf0c779f244Sebastian Redl    // unwrapped from a CXXConstructExpr that was implicitly built. There is no
1065428c620478d513081399798db5550bf0c779f244Sebastian Redl    // particularly sane way we can handle this (especially since it can even
1066428c620478d513081399798db5550bf0c779f244Sebastian Redl    // occur for array new), so we throw the initializer away and have it be
1067428c620478d513081399798db5550bf0c779f244Sebastian Redl    // rebuilt.
1068428c620478d513081399798db5550bf0c779f244Sebastian Redl    if (Initializer && isa<CXXDefaultArgExpr>(Initializer))
1069428c620478d513081399798db5550bf0c779f244Sebastian Redl      Initializer = 0;
10702aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    assert((!Initializer || isa<ImplicitValueInitExpr>(Initializer) ||
10712aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl            isa<CXXConstructExpr>(Initializer)) &&
10722aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl           "Initializer expression that cannot have been implicitly created.");
10732aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    initStyle = CXXNewExpr::NoInit;
10742aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  }
10752aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl
10762aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  Expr **Inits = &Initializer;
10772aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  unsigned NumInits = Initializer ? 1 : 0;
10782aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  if (initStyle == CXXNewExpr::CallInit) {
10792aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    if (ParenListExpr *List = dyn_cast<ParenListExpr>(Initializer)) {
10802aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl      Inits = List->getExprs();
10812aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl      NumInits = List->getNumExprs();
10822aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    } else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Initializer)){
10832aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl      if (!isa<CXXTemporaryObjectExpr>(CCE)) {
10842aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl        // Can happen in template instantiation. Since this is just an implicit
10852aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl        // construction, we just take it apart and rebuild it.
10862aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl        Inits = CCE->getArgs();
10872aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl        NumInits = CCE->getNumArgs();
10882aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl      }
10892aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    }
10902aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  }
10912aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl
10928ad6c8696a23f410398fc126929b107404c59a95Richard Smith  // C++11 [decl.spec.auto]p6. Deduce the type which 'auto' stands in for.
10938ad6c8696a23f410398fc126929b107404c59a95Richard Smith  AutoType *AT = 0;
10948ad6c8696a23f410398fc126929b107404c59a95Richard Smith  if (TypeMayContainAuto &&
10958ad6c8696a23f410398fc126929b107404c59a95Richard Smith      (AT = AllocType->getContainedAutoType()) && !AT->isDeduced()) {
10962aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    if (initStyle == CXXNewExpr::NoInit || NumInits == 0)
109734b41d939a1328f484511c6002ba2456db879a29Richard Smith      return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg)
109834b41d939a1328f484511c6002ba2456db879a29Richard Smith                       << AllocType << TypeRange);
10992aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    if (initStyle == CXXNewExpr::ListInit)
110096a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      return ExprError(Diag(Inits[0]->getLocStart(),
11012aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                            diag::err_auto_new_requires_parens)
11022aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                       << AllocType << TypeRange);
11032aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    if (NumInits > 1) {
11042aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl      Expr *FirstBad = Inits[1];
110596a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      return ExprError(Diag(FirstBad->getLocStart(),
110634b41d939a1328f484511c6002ba2456db879a29Richard Smith                            diag::err_auto_new_ctor_multiple_expressions)
110734b41d939a1328f484511c6002ba2456db879a29Richard Smith                       << AllocType << TypeRange);
110834b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
11092aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    Expr *Deduce = Inits[0];
1110a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith    TypeSourceInfo *DeducedType = 0;
11118ad6c8696a23f410398fc126929b107404c59a95Richard Smith    if (DeduceAutoType(AllocTypeInfo, Deduce, DeducedType) == DAR_Failed)
111234b41d939a1328f484511c6002ba2456db879a29Richard Smith      return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure)
11136dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl                       << AllocType << Deduce->getType()
11146dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl                       << TypeRange << Deduce->getSourceRange());
1115a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith    if (!DeducedType)
1116a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith      return ExprError();
111734b41d939a1328f484511c6002ba2456db879a29Richard Smith
1118a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith    AllocTypeInfo = DeducedType;
1119a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith    AllocType = AllocTypeInfo->getType();
112034b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
11216dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl
11223caf04ea0c01ff6822209c4621c3fa64a48029a4Douglas Gregor  // Per C++0x [expr.new]p5, the type being constructed may be a
11233caf04ea0c01ff6822209c4621c3fa64a48029a4Douglas Gregor  // typedef of an array type.
11249ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize) {
11253caf04ea0c01ff6822209c4621c3fa64a48029a4Douglas Gregor    if (const ConstantArrayType *Array
11263caf04ea0c01ff6822209c4621c3fa64a48029a4Douglas Gregor                              = Context.getAsConstantArrayType(AllocType)) {
11279996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis      ArraySize = IntegerLiteral::Create(Context, Array->getSize(),
11289996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                         Context.getSizeType(),
11299996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                         TypeRange.getEnd());
11303caf04ea0c01ff6822209c4621c3fa64a48029a4Douglas Gregor      AllocType = Array->getElementType();
11313caf04ea0c01ff6822209c4621c3fa64a48029a4Douglas Gregor    }
11323caf04ea0c01ff6822209c4621c3fa64a48029a4Douglas Gregor  }
11334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
1134a0750768718bb5d05150641b5bba74847a21bc09Douglas Gregor  if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange))
1135a0750768718bb5d05150641b5bba74847a21bc09Douglas Gregor    return ExprError();
1136a0750768718bb5d05150641b5bba74847a21bc09Douglas Gregor
11372aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  if (initStyle == CXXNewExpr::ListInit && isStdInitializerList(AllocType, 0)) {
11386dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    Diag(AllocTypeInfo->getTypeLoc().getBeginLoc(),
11396dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl         diag::warn_dangling_std_initializer_list)
1140772291a67d483c1c2abf324eec5d8d6ca476bfdcSebastian Redl        << /*at end of FE*/0 << Inits[0]->getSourceRange();
11416dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl  }
11426dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl
1143f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, infer 'retaining' for the allocated
11444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount &&
1145f85e193739c953358c865005855253af4f68a497John McCall      AllocType.getObjCLifetime() == Qualifiers::OCL_None &&
1146f85e193739c953358c865005855253af4f68a497John McCall      AllocType->isObjCLifetimeType()) {
1147f85e193739c953358c865005855253af4f68a497John McCall    AllocType = Context.getLifetimeQualifiedType(AllocType,
1148f85e193739c953358c865005855253af4f68a497John McCall                                    AllocType->getObjCARCImplicitLifetime());
1149f85e193739c953358c865005855253af4f68a497John McCall  }
1150cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl
1151f85e193739c953358c865005855253af4f68a497John McCall  QualType ResultType = Context.getPointerType(AllocType);
1152f85e193739c953358c865005855253af4f68a497John McCall
1153f39aec17b89f8f0dd78e78c50ad2fa08f12272e3Richard Smith  // C++98 5.3.4p6: "The expression in a direct-new-declarator shall have
1154f39aec17b89f8f0dd78e78c50ad2fa08f12272e3Richard Smith  //   integral or enumeration type with a non-negative value."
1155f39aec17b89f8f0dd78e78c50ad2fa08f12272e3Richard Smith  // C++11 [expr.new]p6: The expression [...] shall be of integral or unscoped
1156f39aec17b89f8f0dd78e78c50ad2fa08f12272e3Richard Smith  //   enumeration type, or a class type for which a single non-explicit
1157f39aec17b89f8f0dd78e78c50ad2fa08f12272e3Richard Smith  //   conversion function to integral or unscoped enumeration type exists.
11582850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  if (ArraySize && !ArraySize->isTypeDependent()) {
1159ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    class SizeConvertDiagnoser : public ICEConvertDiagnoser {
1160ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      Expr *ArraySize;
1161ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
1162ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    public:
1163ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      SizeConvertDiagnoser(Expr *ArraySize)
1164ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        : ICEConvertDiagnoser(false, false), ArraySize(ArraySize) { }
1165ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
1166ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1167ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                               QualType T) {
1168ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        return S.Diag(Loc, diag::err_array_size_not_integral)
1169ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                 << S.getLangOpts().CPlusPlus0x << T;
1170ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      }
1171ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
1172ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1173ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                                   QualType T) {
1174ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        return S.Diag(Loc, diag::err_array_size_incomplete_type)
1175ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                 << T << ArraySize->getSourceRange();
1176ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      }
1177ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
1178ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S,
1179ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                                     SourceLocation Loc,
1180ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                                     QualType T,
1181ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                                     QualType ConvTy) {
1182ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        return S.Diag(Loc, diag::err_array_size_explicit_conversion) << T << ConvTy;
1183ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      }
1184ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
1185ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      virtual DiagnosticBuilder noteExplicitConv(Sema &S,
1186ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                                 CXXConversionDecl *Conv,
1187ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                                 QualType ConvTy) {
1188ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        return S.Diag(Conv->getLocation(), diag::note_array_size_conversion)
1189ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                 << ConvTy->isEnumeralType() << ConvTy;
1190ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      }
1191ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
1192ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1193ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                                  QualType T) {
1194ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        return S.Diag(Loc, diag::err_array_size_ambiguous_conversion) << T;
1195ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      }
1196ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
1197ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      virtual DiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1198ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                              QualType ConvTy) {
1199ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        return S.Diag(Conv->getLocation(), diag::note_array_size_conversion)
1200ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                 << ConvTy->isEnumeralType() << ConvTy;
1201ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      }
1202ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
1203ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      virtual DiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc,
1204ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                                   QualType T,
1205ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                                   QualType ConvTy) {
1206ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        return S.Diag(Loc,
1207ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                      S.getLangOpts().CPlusPlus0x
1208ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                        ? diag::warn_cxx98_compat_array_size_conversion
1209ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                        : diag::ext_array_size_conversion)
1210ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                 << T << ConvTy->isEnumeralType() << ConvTy;
1211ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      }
1212ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    } SizeDiagnoser(ArraySize);
1213ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor
1214ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    ExprResult ConvertedSize
1215ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      = ConvertToIntegralOrEnumerationType(StartLoc, ArraySize, SizeDiagnoser,
1216ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                           /*AllowScopedEnumerations*/ false);
12176bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor    if (ConvertedSize.isInvalid())
12186bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor      return ExprError();
1219dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
12209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    ArraySize = ConvertedSize.take();
1221806054db6653d29cb0d9692df3612cbcd03d0530John McCall    QualType SizeType = ArraySize->getType();
12221274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (!SizeType->isIntegralOrUnscopedEnumerationType())
12236bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor      return ExprError();
1224dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
12250b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith    // C++98 [expr.new]p7:
12260b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith    //   The expression in a direct-new-declarator shall have integral type
12270b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith    //   with a non-negative value.
12280b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith    //
12290b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith    // Let's see if this is a constant < 0. If so, we reject it out of
12300b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith    // hand. Otherwise, if it's not a constant, we must have an unparenthesized
12310b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith    // array type.
12320b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith    //
12330b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith    // Note: such a construct has well-defined semantics in C++11: it throws
12340b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith    // std::bad_array_new_length.
12352850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl    if (!ArraySize->isValueDependent()) {
12362850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl      llvm::APSInt Value;
1237282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      // We've already performed any required implicit conversion to integer or
1238282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      // unscoped enumeration type.
12390b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith      if (ArraySize->isIntegerConstantExpr(Value, Context)) {
12402850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl        if (Value < llvm::APSInt(
1241dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                        llvm::APInt::getNullValue(Value.getBitWidth()),
12420b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith                                 Value.isUnsigned())) {
12434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          if (getLangOpts().CPlusPlus0x)
124496a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar            Diag(ArraySize->getLocStart(),
12450b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith                 diag::warn_typecheck_negative_array_new_size)
12462767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor              << ArraySize->getSourceRange();
12470b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith          else
124896a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar            return ExprError(Diag(ArraySize->getLocStart(),
12490b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith                                  diag::err_typecheck_negative_array_size)
12500b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith                             << ArraySize->getSourceRange());
12510b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith        } else if (!AllocType->isDependentType()) {
12520b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith          unsigned ActiveSizeBits =
12530b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith            ConstantArrayType::getNumAddressingBits(Context, AllocType, Value);
12540b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith          if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) {
12554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie            if (getLangOpts().CPlusPlus0x)
125696a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar              Diag(ArraySize->getLocStart(),
12570b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith                   diag::warn_array_new_too_large)
12580b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith                << Value.toString(10)
12590b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith                << ArraySize->getSourceRange();
12600b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith            else
126196a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar              return ExprError(Diag(ArraySize->getLocStart(),
12620b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith                                    diag::err_array_too_large)
12630b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith                               << Value.toString(10)
12640b458fd8b6321c11e8b22727e0e9b9960e93ff4dRichard Smith                               << ArraySize->getSourceRange());
12652767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          }
12662767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        }
12674bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor      } else if (TypeIdParens.isValid()) {
12684bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor        // Can't have dynamic array size when the type-id is in parentheses.
12694bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor        Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst)
12704bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor          << ArraySize->getSourceRange()
12714bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor          << FixItHint::CreateRemoval(TypeIdParens.getBegin())
12724bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor          << FixItHint::CreateRemoval(TypeIdParens.getEnd());
1273dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
12744bd40318cbea15310a37343db46de96c4fcc15e6Douglas Gregor        TypeIdParens = SourceRange();
12752850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl      }
1276cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    }
1277dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1278f85e193739c953358c865005855253af4f68a497John McCall    // ARC: warn about ABI issues.
12794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount) {
1280f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseAllocType = Context.getBaseElementType(AllocType);
1281f85e193739c953358c865005855253af4f68a497John McCall      if (BaseAllocType.hasStrongOrWeakObjCLifetime())
1282f85e193739c953358c865005855253af4f68a497John McCall        Diag(StartLoc, diag::warn_err_new_delete_object_array)
1283f85e193739c953358c865005855253af4f68a497John McCall          << 0 << BaseAllocType;
1284f85e193739c953358c865005855253af4f68a497John McCall    }
1285f85e193739c953358c865005855253af4f68a497John McCall
12867d16627081caede9691a6f46b796da4073ac14adJohn McCall    // Note that we do *not* convert the argument in any way.  It can
12877d16627081caede9691a6f46b796da4073ac14adJohn McCall    // be signed, larger than size_t, whatever.
1288cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  }
12894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
12904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  FunctionDecl *OperatorNew = 0;
12914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  FunctionDecl *OperatorDelete = 0;
12925354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  Expr **PlaceArgs = PlacementArgs.data();
1293f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  unsigned NumPlaceArgs = PlacementArgs.size();
1294dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
12952850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  if (!AllocType->isDependentType() &&
129613a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles      !Expr::hasAnyTypeDependentArguments(
129713a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles        llvm::makeArrayRef(PlaceArgs, NumPlaceArgs)) &&
12982850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl      FindAllocationFunctions(StartLoc,
129900e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl                              SourceRange(PlacementLParen, PlacementRParen),
130000e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl                              UseGlobal, AllocType, ArraySize, PlaceArgs,
130100e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl                              NumPlaceArgs, OperatorNew, OperatorDelete))
1302f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl    return ExprError();
13036ec278d1a354517e20f13a877481453ee7940c78John McCall
13046ec278d1a354517e20f13a877481453ee7940c78John McCall  // If this is an array allocation, compute whether the usual array
13056ec278d1a354517e20f13a877481453ee7940c78John McCall  // deallocation function for the type has a size_t parameter.
13066ec278d1a354517e20f13a877481453ee7940c78John McCall  bool UsualArrayDeleteWantsSize = false;
13076ec278d1a354517e20f13a877481453ee7940c78John McCall  if (ArraySize && !AllocType->isDependentType())
13086ec278d1a354517e20f13a877481453ee7940c78John McCall    UsualArrayDeleteWantsSize
13096ec278d1a354517e20f13a877481453ee7940c78John McCall      = doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType);
13106ec278d1a354517e20f13a877481453ee7940c78John McCall
13115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Expr *, 8> AllPlaceArgs;
1312498429fa24555ac1c849d8bc6a893d29bd3da4b6Fariborz Jahanian  if (OperatorNew) {
1313498429fa24555ac1c849d8bc6a893d29bd3da4b6Fariborz Jahanian    // Add default arguments, if any.
1314dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    const FunctionProtoType *Proto =
1315498429fa24555ac1c849d8bc6a893d29bd3da4b6Fariborz Jahanian      OperatorNew->getType()->getAs<FunctionProtoType>();
1316dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    VariadicCallType CallType =
13174cd1c706c2bfd3533df563c3b7292c85c7143f31Fariborz Jahanian      Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply;
1318dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
131928e9483a1994cb3615a123379b3b299fd5248e22Anders Carlsson    if (GatherArgumentsForCall(PlacementLParen, OperatorNew,
1320dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                               Proto, 1, PlaceArgs, NumPlaceArgs,
132128e9483a1994cb3615a123379b3b299fd5248e22Anders Carlsson                               AllPlaceArgs, CallType))
1322048f52aa9c9c78538fa369af2fc4b7031a55fa77Fariborz Jahanian      return ExprError();
1323dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1324498429fa24555ac1c849d8bc6a893d29bd3da4b6Fariborz Jahanian    NumPlaceArgs = AllPlaceArgs.size();
1325498429fa24555ac1c849d8bc6a893d29bd3da4b6Fariborz Jahanian    if (NumPlaceArgs > 0)
1326498429fa24555ac1c849d8bc6a893d29bd3da4b6Fariborz Jahanian      PlaceArgs = &AllPlaceArgs[0];
1327e61eb0443a77dd178934d070f458e1a08b84eb96Eli Friedman
1328e61eb0443a77dd178934d070f458e1a08b84eb96Eli Friedman    DiagnoseSentinelCalls(OperatorNew, PlacementLParen,
1329e61eb0443a77dd178934d070f458e1a08b84eb96Eli Friedman                          PlaceArgs, NumPlaceArgs);
1330e61eb0443a77dd178934d070f458e1a08b84eb96Eli Friedman
1331e61eb0443a77dd178934d070f458e1a08b84eb96Eli Friedman    // FIXME: Missing call to CheckFunctionCall or equivalent
1332498429fa24555ac1c849d8bc6a893d29bd3da4b6Fariborz Jahanian  }
1333dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1334fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky  // Warn if the type is over-aligned and is being allocated by global operator
1335fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky  // new.
1336507a8a3fbb2c43247474daa7ccb8dd0a46c32ec5Nick Lewycky  if (NumPlaceArgs == 0 && OperatorNew &&
1337fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky      (OperatorNew->isImplicit() ||
1338fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky       getSourceManager().isInSystemHeader(OperatorNew->getLocStart()))) {
1339fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky    if (unsigned Align = Context.getPreferredTypeAlign(AllocType.getTypePtr())){
1340fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky      unsigned SuitableAlign = Context.getTargetInfo().getSuitableAlign();
1341fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky      if (Align > SuitableAlign)
1342fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky        Diag(StartLoc, diag::warn_overaligned_type)
1343fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky            << AllocType
1344fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky            << unsigned(Align / Context.getCharWidth())
1345fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky            << unsigned(SuitableAlign / Context.getCharWidth());
1346fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky    }
1347fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky  }
1348fca84b232dd74c91b2e0f963a3f8c3bd351a9037Nick Lewycky
1349bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl  QualType InitType = AllocType;
13502aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl  // Array 'new' can't have any initializers except empty parentheses.
1351bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl  // Initializer lists are also allowed, in C++11. Rely on the parser for the
1352bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl  // dialect distinction.
1353bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl  if (ResultType->isArrayType() || ArraySize) {
1354bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl    if (!isLegalArrayNewInitializer(initStyle, Initializer)) {
1355bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl      SourceRange InitRange(Inits[0]->getLocStart(),
1356bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl                            Inits[NumInits - 1]->getLocEnd());
1357bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl      Diag(StartLoc, diag::err_new_array_init_args) << InitRange;
1358bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl      return ExprError();
1359bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl    }
1360bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl    if (InitListExpr *ILE = dyn_cast_or_null<InitListExpr>(Initializer)) {
1361bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl      // We do the initialization typechecking against the array type
1362bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl      // corresponding to the number of initializers + 1 (to also check
1363bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl      // default-initialization).
1364bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl      unsigned NumElements = ILE->getNumInits() + 1;
1365bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl      InitType = Context.getConstantArrayType(AllocType,
1366bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl          llvm::APInt(Context.getTypeSize(Context.getSizeType()), NumElements),
1367bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl                                              ArrayType::Normal, 0);
1368bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl    }
136948c950137e4318abb1080f438208fda8c14ca8b9Anders Carlsson  }
137048c950137e4318abb1080f438208fda8c14ca8b9Anders Carlsson
137199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (!AllocType->isDependentType() &&
137213a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles      !Expr::hasAnyTypeDependentArguments(
137313a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles        llvm::makeArrayRef(Inits, NumInits))) {
13746dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl    // C++11 [expr.new]p15:
137599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    //   A new-expression that creates an object of type T initializes that
137699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    //   object as follows:
137799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    InitializationKind Kind
137899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    //     - If the new-initializer is omitted, the object is default-
137999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    //       initialized (8.5); if no initialization is performed,
138099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    //       the object has indeterminate value
13812aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl      = initStyle == CXXNewExpr::NoInit
13822aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl          ? InitializationKind::CreateDefault(TypeRange.getBegin())
1383dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - Otherwise, the new-initializer is interpreted according to the
138499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    //       initialization rules of 8.5 for direct-initialization.
13852aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl          : initStyle == CXXNewExpr::ListInit
13862aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl              ? InitializationKind::CreateDirectList(TypeRange.getBegin())
13872aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl              : InitializationKind::CreateDirect(TypeRange.getBegin(),
13882aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                                                 DirectInitRange.getBegin(),
13892aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                                                 DirectInitRange.getEnd());
1390dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
139199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    InitializedEntity Entity
1392bd45d257a404ed1f87090c8a32b5dd75576b7d11Sebastian Redl      = InitializedEntity::InitializeNew(StartLoc, InitType);
13932aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    InitializationSequence InitSeq(*this, Entity, Kind, Inits, NumInits);
1394dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind,
13952aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                                          MultiExprArg(Inits, NumInits));
139699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    if (FullInit.isInvalid())
139799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor      return ExprError();
1398dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
13992aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    // FullInit is our initializer; strip off CXXBindTemporaryExprs, because
14002aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    // we don't want the initialized object to be destructed.
14012aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    if (CXXBindTemporaryExpr *Binder =
14022aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl            dyn_cast_or_null<CXXBindTemporaryExpr>(FullInit.get()))
14032aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl      FullInit = Owned(Binder->getSubExpr());
1404dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
14052aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl    Initializer = FullInit.take();
14064c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  }
1407dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
14086d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  // Mark the new and delete operators as referenced.
14096d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  if (OperatorNew)
14105f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    MarkFunctionReferenced(StartLoc, OperatorNew);
14116d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  if (OperatorDelete)
14125f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    MarkFunctionReferenced(StartLoc, OperatorDelete);
14136d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
141484ff0fccb180098b70504c03c3072a19e6d573afJohn McCall  // C++0x [expr.new]p17:
141584ff0fccb180098b70504c03c3072a19e6d573afJohn McCall  //   If the new expression creates an array of objects of class type,
141684ff0fccb180098b70504c03c3072a19e6d573afJohn McCall  //   access and ambiguity control are done for the destructor.
1417426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie  QualType BaseAllocType = Context.getBaseElementType(AllocType);
1418426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie  if (ArraySize && !BaseAllocType->isDependentType()) {
1419426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie    if (const RecordType *BaseRecordType = BaseAllocType->getAs<RecordType>()) {
1420426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie      if (CXXDestructorDecl *dtor = LookupDestructor(
1421426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie              cast<CXXRecordDecl>(BaseRecordType->getDecl()))) {
1422426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie        MarkFunctionReferenced(StartLoc, dtor);
1423426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie        CheckDestructorAccess(StartLoc, dtor,
1424426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie                              PDiag(diag::err_access_dtor)
1425426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie                                << BaseAllocType);
1426426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie        DiagnoseUseOfDecl(dtor, StartLoc);
1427426d6ca163e20187761aa55a67797dac51508d0dDavid Blaikie      }
142884ff0fccb180098b70504c03c3072a19e6d573afJohn McCall    }
142984ff0fccb180098b70504c03c3072a19e6d573afJohn McCall  }
1430dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1431ad7fe864862305c2f71e047cdf6706ef43aebdc0Ted Kremenek  return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew,
14327cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                        OperatorDelete,
14336ec278d1a354517e20f13a877481453ee7940c78John McCall                                        UsualArrayDeleteWantsSize,
14343b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                   llvm::makeArrayRef(PlaceArgs, NumPlaceArgs),
14353b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer                                        TypeIdParens,
14362aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                                        ArraySize, initStyle, Initializer,
14371bb2a93ab7b1499dda6f6b58865bd0dce1864228Douglas Gregor                                        ResultType, AllocTypeInfo,
14382aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl                                        StartLoc, DirectInitRange));
14394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
14404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
14412aed8b88613863f3c439cdfb205bdf8b608fb205Sebastian Redl/// \brief Checks that a type is suitable as the allocated type
14424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// in a new-expression.
14433433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregorbool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                              SourceRange R) {
14454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
14464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  //   abstract class type or array thereof.
1447e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor  if (AllocType->isFunctionType())
14483433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor    return Diag(Loc, diag::err_bad_new_type)
14493433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor      << AllocType << 0 << R;
1450e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor  else if (AllocType->isReferenceType())
14513433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor    return Diag(Loc, diag::err_bad_new_type)
14523433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor      << AllocType << 1 << R;
1453e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor  else if (!AllocType->isDependentType() &&
1454d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor           RequireCompleteType(Loc, AllocType, diag::err_new_incomplete_type,R))
1455e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor    return true;
14563433cf7dfda405bf51bba914a338adf645a87e3bDouglas Gregor  else if (RequireNonAbstractType(Loc, AllocType,
1457e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor                                  diag::err_allocation_of_abstract_type))
14584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl    return true;
1459a0750768718bb5d05150641b5bba74847a21bc09Douglas Gregor  else if (AllocType->isVariablyModifiedType())
1460a0750768718bb5d05150641b5bba74847a21bc09Douglas Gregor    return Diag(Loc, diag::err_variably_modified_new_type)
1461a0750768718bb5d05150641b5bba74847a21bc09Douglas Gregor             << AllocType;
14625666d36cce566b59be271670364794de9803af04Douglas Gregor  else if (unsigned AddressSpace = AllocType.getAddressSpace())
14635666d36cce566b59be271670364794de9803af04Douglas Gregor    return Diag(Loc, diag::err_address_space_qualified_new)
14645666d36cce566b59be271670364794de9803af04Douglas Gregor      << AllocType.getUnqualifiedType() << AddressSpace;
14654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  else if (getLangOpts().ObjCAutoRefCount) {
1466f85e193739c953358c865005855253af4f68a497John McCall    if (const ArrayType *AT = Context.getAsArrayType(AllocType)) {
1467f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseAllocType = Context.getBaseElementType(AT);
1468f85e193739c953358c865005855253af4f68a497John McCall      if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None &&
1469f85e193739c953358c865005855253af4f68a497John McCall          BaseAllocType->isObjCLifetimeType())
1470b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis        return Diag(Loc, diag::err_arc_new_array_without_ownership)
1471f85e193739c953358c865005855253af4f68a497John McCall          << BaseAllocType;
1472f85e193739c953358c865005855253af4f68a497John McCall    }
1473f85e193739c953358c865005855253af4f68a497John McCall  }
14745666d36cce566b59be271670364794de9803af04Douglas Gregor
14754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  return false;
14764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
14774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
14786d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor/// \brief Determine whether the given function is a non-placement
14796d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor/// deallocation function.
14806d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregorstatic bool isNonPlacementDeallocationFunction(FunctionDecl *FD) {
14816d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  if (FD->isInvalidDecl())
14826d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    return false;
14836d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
14846d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD))
14856d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    return Method->isUsualDeallocationFunction();
14866d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
14876d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  return ((FD->getOverloadedOperator() == OO_Delete ||
14886d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor           FD->getOverloadedOperator() == OO_Array_Delete) &&
14896d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor          FD->getNumParams() == 1);
14906d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor}
14916d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
1492b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/// FindAllocationFunctions - Finds the overloads of operator new and delete
1493b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/// that are appropriate for the allocation.
149400e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redlbool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range,
149500e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl                                   bool UseGlobal, QualType AllocType,
149600e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl                                   bool IsArray, Expr **PlaceArgs,
149700e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl                                   unsigned NumPlaceArgs,
1498b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl                                   FunctionDecl *&OperatorNew,
14991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                   FunctionDecl *&OperatorDelete) {
1500b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  // --- Choosing an allocation function ---
1501b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  // C++ 5.3.4p8 - 14 & 18
1502b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  // 1) If UseGlobal is true, only look in the global scope. Else, also look
1503b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  //   in the scope of the allocated class.
1504b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  // 2) If an array size is given, look for operator new[], else look for
1505b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  //   operator new.
1506b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  // 3) The first argument is always size_t. Append the arguments from the
1507b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  //   placement form.
1508b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
15095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs);
1510b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  // We don't care about the actual value of this argument.
1511b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  // FIXME: Should the Sema create the expression and embed it in the syntax
1512b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  // tree? Or should the consumer just recalculate the value?
15139996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  IntegerLiteral Size(Context, llvm::APInt::getNullValue(
1514bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                      Context.getTargetInfo().getPointerWidth(0)),
1515d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson                      Context.getSizeType(),
1516d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson                      SourceLocation());
1517d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson  AllocArgs[0] = &Size;
1518b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1);
1519b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
15206d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  // C++ [expr.new]p8:
15216d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  //   If the allocated type is a non-array type, the allocation
15220099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //   function's name is operator new and the deallocation function's
15236d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  //   name is operator delete. If the allocated type is an array
15240099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //   type, the allocation function's name is operator new[] and the
15250099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //   deallocation function's name is operator delete[].
1526b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName(
1527b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl                                        IsArray ? OO_Array_New : OO_New);
15286d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName(
15296d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor                                        IsArray ? OO_Array_Delete : OO_Delete);
15306d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
1531d2932986a16244b7f9a3f9a7a6b0daf543c91540Argyrios Kyrtzidis  QualType AllocElemType = Context.getBaseElementType(AllocType);
1532d2932986a16244b7f9a3f9a7a6b0daf543c91540Argyrios Kyrtzidis
1533d2932986a16244b7f9a3f9a7a6b0daf543c91540Argyrios Kyrtzidis  if (AllocElemType->isRecordType() && !UseGlobal) {
15341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CXXRecordDecl *Record
1535d2932986a16244b7f9a3f9a7a6b0daf543c91540Argyrios Kyrtzidis      = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl());
153600e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl    if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0],
15377f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl                          AllocArgs.size(), Record, /*AllowMissing=*/true,
15387f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl                          OperatorNew))
1539b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl      return true;
1540b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  }
1541b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  if (!OperatorNew) {
1542b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl    // Didn't find a member overload. Look for a global one.
1543b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl    DeclareGlobalNewDelete();
15447f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    DeclContext *TUDecl = Context.getTranslationUnitDecl();
154500e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl    if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0],
15467f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl                          AllocArgs.size(), TUDecl, /*AllowMissing=*/false,
15477f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl                          OperatorNew))
15487f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl      return true;
15497f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  }
1550b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
15519c82afc7d1f57b427053e6679d87539b0dc63b1aJohn McCall  // We don't need an operator delete if we're running under
15529c82afc7d1f57b427053e6679d87539b0dc63b1aJohn McCall  // -fno-exceptions.
15534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().Exceptions) {
15549c82afc7d1f57b427053e6679d87539b0dc63b1aJohn McCall    OperatorDelete = 0;
15559c82afc7d1f57b427053e6679d87539b0dc63b1aJohn McCall    return false;
15569c82afc7d1f57b427053e6679d87539b0dc63b1aJohn McCall  }
15579c82afc7d1f57b427053e6679d87539b0dc63b1aJohn McCall
1558d958389d52170be1c6dee93031d34b13809b786bAnders Carlsson  // FindAllocationOverload can change the passed in arguments, so we need to
1559d958389d52170be1c6dee93031d34b13809b786bAnders Carlsson  // copy them back.
1560d958389d52170be1c6dee93031d34b13809b786bAnders Carlsson  if (NumPlaceArgs > 0)
1561d958389d52170be1c6dee93031d34b13809b786bAnders Carlsson    std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs);
15621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15636d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  // C++ [expr.new]p19:
15646d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  //
15656d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  //   If the new-expression begins with a unary :: operator, the
15660099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //   deallocation function's name is looked up in the global
15676d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  //   scope. Otherwise, if the allocated type is a class type T or an
15680099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //   array thereof, the deallocation function's name is looked up in
15696d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  //   the scope of T. If this lookup fails to find the name, or if
15706d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  //   the allocated type is not a class type or array thereof, the
15710099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //   deallocation function's name is looked up in the global scope.
15726d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName);
1573d2932986a16244b7f9a3f9a7a6b0daf543c91540Argyrios Kyrtzidis  if (AllocElemType->isRecordType() && !UseGlobal) {
15746d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    CXXRecordDecl *RD
1575d2932986a16244b7f9a3f9a7a6b0daf543c91540Argyrios Kyrtzidis      = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl());
15766d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    LookupQualifiedName(FoundDelete, RD);
15776d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  }
157890c8c57bcd84083df85f76aac2aa62acb85eb077John McCall  if (FoundDelete.isAmbiguous())
157990c8c57bcd84083df85f76aac2aa62acb85eb077John McCall    return true; // FIXME: clean up expressions?
15806d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
15816d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  if (FoundDelete.empty()) {
15826d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    DeclareGlobalNewDelete();
15836d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl());
15846d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  }
15856d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
15866d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  FoundDelete.suppressDiagnostics();
15879aa472c45d2bd81b7b52c225e8acc560d716db97John McCall
15885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches;
15899aa472c45d2bd81b7b52c225e8acc560d716db97John McCall
1590edeb6c9b47e802da1a9a149ea1ea4f1ddd7f6182John McCall  // Whether we're looking for a placement operator delete is dictated
1591edeb6c9b47e802da1a9a149ea1ea4f1ddd7f6182John McCall  // by whether we selected a placement operator new, not by whether
1592edeb6c9b47e802da1a9a149ea1ea4f1ddd7f6182John McCall  // we had explicit placement arguments.  This matters for things like
1593edeb6c9b47e802da1a9a149ea1ea4f1ddd7f6182John McCall  //   struct A { void *operator new(size_t, int = 0); ... };
1594edeb6c9b47e802da1a9a149ea1ea4f1ddd7f6182John McCall  //   A *a = new A()
1595edeb6c9b47e802da1a9a149ea1ea4f1ddd7f6182John McCall  bool isPlacementNew = (NumPlaceArgs > 0 || OperatorNew->param_size() != 1);
1596edeb6c9b47e802da1a9a149ea1ea4f1ddd7f6182John McCall
1597edeb6c9b47e802da1a9a149ea1ea4f1ddd7f6182John McCall  if (isPlacementNew) {
15986d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    // C++ [expr.new]p20:
15996d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   A declaration of a placement deallocation function matches the
16006d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   declaration of a placement allocation function if it has the
16016d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   same number of parameters and, after parameter transformations
16026d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   (8.3.5), all parameter types except the first are
16036d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   identical. [...]
1604dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //
16056d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    // To perform this comparison, we compute the function type that
16066d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    // the deallocation function should have, and use that type both
16076d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    // for template argument deduction and for comparison purposes.
1608e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    //
1609e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    // FIXME: this comparison should ignore CC and the like.
16106d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    QualType ExpectedFunctionType;
16116d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    {
16126d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor      const FunctionProtoType *Proto
16136d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        = OperatorNew->getType()->getAs<FunctionProtoType>();
1614e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
16155f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<QualType, 4> ArgTypes;
1616dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ArgTypes.push_back(Context.VoidPtrTy);
16176d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor      for (unsigned I = 1, N = Proto->getNumArgs(); I < N; ++I)
16186d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        ArgTypes.push_back(Proto->getArgType(I));
16196d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
1620e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      FunctionProtoType::ExtProtoInfo EPI;
1621e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      EPI.Variadic = Proto->isVariadic();
1622e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
16236d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor      ExpectedFunctionType
16246d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        = Context.getFunctionType(Context.VoidTy, ArgTypes.data(),
1625e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                  ArgTypes.size(), EPI);
16266d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    }
16276d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
1628dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    for (LookupResult::iterator D = FoundDelete.begin(),
16296d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor                             DEnd = FoundDelete.end();
16306d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor         D != DEnd; ++D) {
16316d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor      FunctionDecl *Fn = 0;
1632dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (FunctionTemplateDecl *FnTmpl
16336d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor            = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) {
16346d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        // Perform template argument deduction to try to match the
16356d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        // expected function type.
16366d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        TemplateDeductionInfo Info(Context, StartLoc);
16376d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        if (DeduceTemplateArguments(FnTmpl, 0, ExpectedFunctionType, Fn, Info))
16386d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor          continue;
16396d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor      } else
16406d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl());
16416d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
16426d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor      if (Context.hasSameType(Fn->getType(), ExpectedFunctionType))
16439aa472c45d2bd81b7b52c225e8acc560d716db97John McCall        Matches.push_back(std::make_pair(D.getPair(), Fn));
16446d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    }
16456d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  } else {
16466d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    // C++ [expr.new]p20:
16476d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   [...] Any non-placement deallocation function matches a
16486d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   non-placement allocation function. [...]
1649dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    for (LookupResult::iterator D = FoundDelete.begin(),
16506d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor                             DEnd = FoundDelete.end();
16516d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor         D != DEnd; ++D) {
16526d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor      if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl()))
16536d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        if (isNonPlacementDeallocationFunction(Fn))
16549aa472c45d2bd81b7b52c225e8acc560d716db97John McCall          Matches.push_back(std::make_pair(D.getPair(), Fn));
16556d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    }
16566d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  }
16576d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
16586d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  // C++ [expr.new]p20:
16596d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  //   [...] If the lookup finds a single matching deallocation
16606d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  //   function, that function will be called; otherwise, no
16616d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  //   deallocation function will be called.
16626d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  if (Matches.size() == 1) {
16639aa472c45d2bd81b7b52c225e8acc560d716db97John McCall    OperatorDelete = Matches[0].second;
16646d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
16656d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    // C++0x [expr.new]p20:
16666d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   If the lookup finds the two-parameter form of a usual
16676d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   deallocation function (3.7.4.2) and that function, considered
16686d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   as a placement deallocation function, would have been
16696d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   selected as a match for the allocation function, the program
16706d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    //   is ill-formed.
16714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (NumPlaceArgs && getLangOpts().CPlusPlus0x &&
16726d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        isNonPlacementDeallocationFunction(OperatorDelete)) {
16736d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor      Diag(StartLoc, diag::err_placement_new_non_placement_delete)
1674dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        << SourceRange(PlaceArgs[0]->getLocStart(),
16756d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor                       PlaceArgs[NumPlaceArgs - 1]->getLocEnd());
16766d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor      Diag(OperatorDelete->getLocation(), diag::note_previous_decl)
16776d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor        << DeleteName;
167890c8c57bcd84083df85f76aac2aa62acb85eb077John McCall    } else {
167990c8c57bcd84083df85f76aac2aa62acb85eb077John McCall      CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(),
16809aa472c45d2bd81b7b52c225e8acc560d716db97John McCall                            Matches[0].first);
16816d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor    }
16826d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor  }
16836d90870fe821a9b1a7822d3e28032042d03e5680Douglas Gregor
16847f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  return false;
16857f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl}
1686b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
16877f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl/// FindAllocationOverload - Find an fitting overload for the allocation
16887f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl/// function in the specified scope.
168900e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redlbool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range,
169000e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl                                  DeclarationName Name, Expr** Args,
169100e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl                                  unsigned NumArgs, DeclContext *Ctx,
16922be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt                                  bool AllowMissing, FunctionDecl *&Operator,
16932be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt                                  bool Diagnose) {
1694a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  LookupResult R(*this, Name, StartLoc, LookupOrdinaryName);
1695a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  LookupQualifiedName(R, Ctx);
1696f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (R.empty()) {
16972be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt    if (AllowMissing || !Diagnose)
16987f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl      return false;
16997f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
17004330d65797e10618e39817ca59e8772671c3de59Chris Lattner      << Name << Range;
17017f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  }
1702b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
170390c8c57bcd84083df85f76aac2aa62acb85eb077John McCall  if (R.isAmbiguous())
170490c8c57bcd84083df85f76aac2aa62acb85eb077John McCall    return true;
170590c8c57bcd84083df85f76aac2aa62acb85eb077John McCall
170690c8c57bcd84083df85f76aac2aa62acb85eb077John McCall  R.suppressDiagnostics();
1707f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
17085769d6195087229770d7ac90449443e026c47103John McCall  OverloadCandidateSet Candidates(StartLoc);
1709dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end();
17105d64e5b6bc03462e8b7d0cd804565829afb1b508Douglas Gregor       Alloc != AllocEnd; ++Alloc) {
17113fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor    // Even member operator new/delete are implicitly treated as
17123fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor    // static, so don't use AddMemberCandidate.
17139aa472c45d2bd81b7b52c225e8acc560d716db97John McCall    NamedDecl *D = (*Alloc)->getUnderlyingDecl();
17144a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth
17159aa472c45d2bd81b7b52c225e8acc560d716db97John McCall    if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) {
17169aa472c45d2bd81b7b52c225e8acc560d716db97John McCall      AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(),
171713a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                   /*ExplicitTemplateArgs=*/0,
171813a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                   llvm::makeArrayRef(Args, NumArgs),
17194a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth                                   Candidates,
17204a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth                                   /*SuppressUserConversions=*/false);
17219091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor      continue;
17224a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth    }
17234a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth
17249aa472c45d2bd81b7b52c225e8acc560d716db97John McCall    FunctionDecl *Fn = cast<FunctionDecl>(D);
172513a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles    AddOverloadCandidate(Fn, Alloc.getPair(),
172613a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                         llvm::makeArrayRef(Args, NumArgs), Candidates,
17274a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth                         /*SuppressUserConversions=*/false);
1728b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  }
1729b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
17307f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  // Do the resolution.
17317f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  OverloadCandidateSet::iterator Best;
1732120d63cd4465230c2cd56508c7cd8e0ad00848e7John McCall  switch (Candidates.BestViableFunction(*this, StartLoc, Best)) {
17337f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  case OR_Success: {
17347f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    // Got one!
17357f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    FunctionDecl *FnDecl = Best->Function;
17365f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    MarkFunctionReferenced(StartLoc, FnDecl);
17377f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    // The first argument is size_t, and the first parameter must be size_t,
17387f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    // too. This is checked on declaration and can be assumed. (It can't be
17397f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    // asserted on, though, since invalid decls are left in there.)
174090c8c57bcd84083df85f76aac2aa62acb85eb077John McCall    // Watch out for variadic allocator function.
1741048f52aa9c9c78538fa369af2fc4b7031a55fa77Fariborz Jahanian    unsigned NumArgsInFnDecl = FnDecl->getNumParams();
1742048f52aa9c9c78538fa369af2fc4b7031a55fa77Fariborz Jahanian    for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) {
17432be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt      InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
17442be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt                                                       FnDecl->getParamDecl(i));
17452be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt
17462be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt      if (!Diagnose && !CanPerformCopyInitialization(Entity, Owned(Args[i])))
17472be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt        return true;
17482be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt
174960d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Result
17502be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt        = PerformCopyInitialization(Entity, SourceLocation(), Owned(Args[i]));
175129ecaba4ebe8c9a2627cf405e36473b764cc5cfdDouglas Gregor      if (Result.isInvalid())
17527f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl        return true;
1753dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
175429ecaba4ebe8c9a2627cf405e36473b764cc5cfdDouglas Gregor      Args[i] = Result.takeAs<Expr>();
17557f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    }
17569a561d539158a30b68fc258b81a994f3fac10212Richard Smith
17577f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    Operator = FnDecl;
17589a561d539158a30b68fc258b81a994f3fac10212Richard Smith
17599a561d539158a30b68fc258b81a994f3fac10212Richard Smith    if (CheckAllocationAccess(StartLoc, Range, R.getNamingClass(),
17609a561d539158a30b68fc258b81a994f3fac10212Richard Smith                              Best->FoundDecl, Diagnose) == AR_inaccessible)
17619a561d539158a30b68fc258b81a994f3fac10212Richard Smith      return true;
17629a561d539158a30b68fc258b81a994f3fac10212Richard Smith
17637f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    return false;
17647f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  }
17657f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl
17667f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  case OR_No_Viable_Function:
1767361d380eef53bd2ac3d5efbfea1636e1f8fbdd8dChandler Carruth    if (Diagnose) {
17682be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt      Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
17692be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt        << Name << Range;
177013a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles      Candidates.NoteCandidates(*this, OCD_AllCandidates,
177113a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                llvm::makeArrayRef(Args, NumArgs));
1772361d380eef53bd2ac3d5efbfea1636e1f8fbdd8dChandler Carruth    }
17737f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    return true;
17747f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl
17757f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  case OR_Ambiguous:
1776361d380eef53bd2ac3d5efbfea1636e1f8fbdd8dChandler Carruth    if (Diagnose) {
17772be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt      Diag(StartLoc, diag::err_ovl_ambiguous_call)
17782be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt        << Name << Range;
177913a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles      Candidates.NoteCandidates(*this, OCD_ViableCandidates,
178013a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                llvm::makeArrayRef(Args, NumArgs));
1781361d380eef53bd2ac3d5efbfea1636e1f8fbdd8dChandler Carruth    }
17827f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl    return true;
178348f3bb9f780f6e64ab71ba0202ca04b07473805aDouglas Gregor
17840a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  case OR_Deleted: {
1785361d380eef53bd2ac3d5efbfea1636e1f8fbdd8dChandler Carruth    if (Diagnose) {
17862be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt      Diag(StartLoc, diag::err_ovl_deleted_call)
17872be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt        << Best->Function->isDeleted()
17882be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt        << Name
17892be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt        << getDeletedOrUnavailableSuffix(Best->Function)
17902be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt        << Range;
179113a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles      Candidates.NoteCandidates(*this, OCD_AllCandidates,
179213a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                llvm::makeArrayRef(Args, NumArgs));
1793361d380eef53bd2ac3d5efbfea1636e1f8fbdd8dChandler Carruth    }
179448f3bb9f780f6e64ab71ba0202ca04b07473805aDouglas Gregor    return true;
17957f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  }
17960a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
1797b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unreachable, bad result from BestViableFunction");
1798b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl}
1799b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
18007f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl
1801b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/// DeclareGlobalNewDelete - Declare the global forms of operator new and
1802b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/// delete. These are:
1803b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/// @code
18048999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl///   // C++03:
1805b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl///   void* operator new(std::size_t) throw(std::bad_alloc);
1806b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl///   void* operator new[](std::size_t) throw(std::bad_alloc);
1807b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl///   void operator delete(void *) throw();
1808b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl///   void operator delete[](void *) throw();
18098999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl///   // C++0x:
18108999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl///   void* operator new(std::size_t);
18118999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl///   void* operator new[](std::size_t);
18128999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl///   void operator delete(void *);
18138999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl///   void operator delete[](void *);
1814b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/// @endcode
18158999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl/// C++0x operator delete is implicitly noexcept.
1816b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/// Note that the placement and nothrow forms of new are *not* implicitly
1817b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/// declared. Their use requires including \<new\>.
18181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Sema::DeclareGlobalNewDelete() {
1819b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  if (GlobalNewDeleteDeclared)
1820b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl    return;
1821dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
18227adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  // C++ [basic.std.dynamic]p2:
1823dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   [...] The following allocation and deallocation functions (18.4) are
1824dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   implicitly declared in global scope in each translation unit of a
18257adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  //   program
1826dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //
18278999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  //     C++03:
18287adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  //     void* operator new(std::size_t) throw(std::bad_alloc);
1829dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //     void* operator new[](std::size_t) throw(std::bad_alloc);
1830dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //     void  operator delete(void*) throw();
18317adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  //     void  operator delete[](void*) throw();
18328999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  //     C++0x:
18338999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  //     void* operator new(std::size_t);
18348999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  //     void* operator new[](std::size_t);
18358999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  //     void  operator delete(void*);
18368999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  //     void  operator delete[](void*);
18377adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  //
1838dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   These implicit declarations introduce only the function names operator
18397adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  //   new, operator new[], operator delete, operator delete[].
18407adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  //
18417adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  // Here, we need to refer to std::bad_alloc, so we will implicitly declare
18427adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  // "std" or "bad_alloc" as necessary to form the exception specification.
18437adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  // However, we do not make these implicit declarations visible to name
18447adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  // lookup.
18458999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  // Note that the C++0x versions of operator delete are deallocation functions,
18468999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  // and thus are implicitly noexcept.
18474e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!StdBadAlloc && !getLangOpts().CPlusPlus0x) {
18487adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor    // The "std::bad_alloc" class has not yet been declared, so build it
18497adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor    // implicitly.
1850dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class,
1851dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                        getOrCreateStdNamespace(),
1852ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                        SourceLocation(), SourceLocation(),
1853dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                      &PP.getIdentifierTable().get("bad_alloc"),
1854ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                        0);
185576c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis    getStdBadAlloc()->setImplicit(true);
18567adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  }
1857dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1858b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  GlobalNewDeleteDeclared = true;
1859b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
1860b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  QualType VoidPtr = Context.getPointerType(Context.VoidTy);
1861b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  QualType SizeT = Context.getSizeType();
18624e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  bool AssumeSaneOperatorNew = getLangOpts().AssumeSaneOperatorNew;
1863b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
1864b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  DeclareGlobalAllocationFunction(
1865b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl      Context.DeclarationNames.getCXXOperatorName(OO_New),
1866fc2844846e91398205fddc71196fe9dda04e105fNuno Lopes      VoidPtr, SizeT, AssumeSaneOperatorNew);
1867b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  DeclareGlobalAllocationFunction(
1868b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl      Context.DeclarationNames.getCXXOperatorName(OO_Array_New),
1869fc2844846e91398205fddc71196fe9dda04e105fNuno Lopes      VoidPtr, SizeT, AssumeSaneOperatorNew);
1870b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  DeclareGlobalAllocationFunction(
1871b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl      Context.DeclarationNames.getCXXOperatorName(OO_Delete),
1872b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl      Context.VoidTy, VoidPtr);
1873b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  DeclareGlobalAllocationFunction(
1874b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl      Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete),
1875b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl      Context.VoidTy, VoidPtr);
1876b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl}
1877b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
1878b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/// DeclareGlobalAllocationFunction - Declares a single implicit global
1879b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl/// allocation function if it doesn't already exist.
1880b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redlvoid Sema::DeclareGlobalAllocationFunction(DeclarationName Name,
1881fc2844846e91398205fddc71196fe9dda04e105fNuno Lopes                                           QualType Return, QualType Argument,
1882fc2844846e91398205fddc71196fe9dda04e105fNuno Lopes                                           bool AddMallocAttr) {
1883b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  DeclContext *GlobalCtx = Context.getTranslationUnitDecl();
1884b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
1885b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  // Check if this function is already declared.
18866ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  {
18875cc3709b843c799e268f944b7bf9dbdc79cefe8dDouglas Gregor    DeclContext::lookup_iterator Alloc, AllocEnd;
188817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name);
18896ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor         Alloc != AllocEnd; ++Alloc) {
18904a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth      // Only look at non-template functions, as it is the predefined,
18914a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth      // non-templated allocation function we are trying to declare here.
18924a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth      if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) {
18934a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth        QualType InitialParamType =
18946e790ab61bf4835944971955e84279112833ef0cDouglas Gregor          Context.getCanonicalType(
18954a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth            Func->getParamDecl(0)->getType().getUnqualifiedType());
18964a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth        // FIXME: Do we need to check for default arguments here?
18977b86862ff555a9d848ac7abf6042d192b6d5a04dDouglas Gregor        if (Func->getNumParams() == 1 && InitialParamType == Argument) {
18987b86862ff555a9d848ac7abf6042d192b6d5a04dDouglas Gregor          if(AddMallocAttr && !Func->hasAttr<MallocAttr>())
1899cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            Func->addAttr(::new (Context) MallocAttr(SourceLocation(), Context));
19004a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth          return;
19017b86862ff555a9d848ac7abf6042d192b6d5a04dDouglas Gregor        }
19024a73ea9c4e87aa111901f3c27b08f0571b58a4cfChandler Carruth      }
1903b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl    }
1904b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  }
1905b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
19067adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  QualType BadAllocType;
1907dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  bool HasBadAllocExceptionSpec
19087adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor    = (Name.getCXXOverloadedOperator() == OO_New ||
19097adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor       Name.getCXXOverloadedOperator() == OO_Array_New);
19104e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (HasBadAllocExceptionSpec && !getLangOpts().CPlusPlus0x) {
19117adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor    assert(StdBadAlloc && "Must have std::bad_alloc declared");
191276c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis    BadAllocType = Context.getTypeDeclType(getStdBadAlloc());
19137adb10fa317cd7eacb0959f775e77353d4f24ad1Douglas Gregor  }
1914e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1915e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
1916e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  if (HasBadAllocExceptionSpec) {
19174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus0x) {
19188999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl      EPI.ExceptionSpecType = EST_Dynamic;
19198999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl      EPI.NumExceptions = 1;
19208999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl      EPI.Exceptions = &BadAllocType;
19218999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl    }
192260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  } else {
19234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    EPI.ExceptionSpecType = getLangOpts().CPlusPlus0x ?
19248999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl                                EST_BasicNoexcept : EST_DynamicNone;
1925e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  }
1926dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1927e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  QualType FnType = Context.getFunctionType(Return, &Argument, 1, EPI);
1928b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  FunctionDecl *Alloc =
1929ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FunctionDecl::Create(Context, GlobalCtx, SourceLocation(),
1930ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                         SourceLocation(), Name,
1931d931b086984257de68868a64a235c2b4b34003fbJohn McCall                         FnType, /*TInfo=*/0, SC_None,
1932d931b086984257de68868a64a235c2b4b34003fbJohn McCall                         SC_None, false, true);
1933b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  Alloc->setImplicit();
1934dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1935fc2844846e91398205fddc71196fe9dda04e105fNuno Lopes  if (AddMallocAttr)
1936cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    Alloc->addAttr(::new (Context) MallocAttr(SourceLocation(), Context));
1937dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1938b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
1939ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(), 0,
1940ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           Argument, /*TInfo=*/0,
1941ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SC_None, SC_None, 0);
19424278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Alloc->setParams(Param);
1943b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
19446ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // FIXME: Also add this declaration to the IdentifierResolver, but
19456ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // make sure it is at the end of the chain to coincide with the
19466ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // global scope.
19475f1e0942a32657b625702aa52f82430d0120f424John McCall  Context.getTranslationUnitDecl()->addDecl(Alloc);
1948b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl}
1949b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
195078f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlssonbool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD,
195178f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson                                    DeclarationName Name,
19522be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt                                    FunctionDecl* &Operator, bool Diagnose) {
1953a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName);
195478f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  // Try to find operator delete/operator delete[] in class scope.
1955a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  LookupQualifiedName(Found, RD);
1956dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1957a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall  if (Found.isAmbiguous())
195878f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson    return true;
195978f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson
19602389324eb73a1c99d5dfc1dc9eed047ddc96a7b4Chandler Carruth  Found.suppressDiagnostics();
19612389324eb73a1c99d5dfc1dc9eed047ddc96a7b4Chandler Carruth
19625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclAccessPair,4> Matches;
196378f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
196478f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson       F != FEnd; ++F) {
196509556fd1fb576144e2beda023bf3386f2292243bChandler Carruth    NamedDecl *ND = (*F)->getUnderlyingDecl();
196609556fd1fb576144e2beda023bf3386f2292243bChandler Carruth
196709556fd1fb576144e2beda023bf3386f2292243bChandler Carruth    // Ignore template operator delete members from the check for a usual
196809556fd1fb576144e2beda023bf3386f2292243bChandler Carruth    // deallocation function.
196909556fd1fb576144e2beda023bf3386f2292243bChandler Carruth    if (isa<FunctionTemplateDecl>(ND))
197009556fd1fb576144e2beda023bf3386f2292243bChandler Carruth      continue;
197109556fd1fb576144e2beda023bf3386f2292243bChandler Carruth
197209556fd1fb576144e2beda023bf3386f2292243bChandler Carruth    if (cast<CXXMethodDecl>(ND)->isUsualDeallocationFunction())
1973046a7466f23107bd752d9a8106aada85061699bcJohn McCall      Matches.push_back(F.getPair());
1974046a7466f23107bd752d9a8106aada85061699bcJohn McCall  }
1975046a7466f23107bd752d9a8106aada85061699bcJohn McCall
1976046a7466f23107bd752d9a8106aada85061699bcJohn McCall  // There's exactly one suitable operator;  pick it.
1977046a7466f23107bd752d9a8106aada85061699bcJohn McCall  if (Matches.size() == 1) {
1978046a7466f23107bd752d9a8106aada85061699bcJohn McCall    Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl());
19792be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt
19802be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt    if (Operator->isDeleted()) {
19812be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt      if (Diagnose) {
19822be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt        Diag(StartLoc, diag::err_deleted_function_use);
19836c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith        NoteDeletedFunction(Operator);
19842be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt      }
19852be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt      return true;
19862be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt    }
19872be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt
19889a561d539158a30b68fc258b81a994f3fac10212Richard Smith    if (CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(),
19899a561d539158a30b68fc258b81a994f3fac10212Richard Smith                              Matches[0], Diagnose) == AR_inaccessible)
19909a561d539158a30b68fc258b81a994f3fac10212Richard Smith      return true;
19919a561d539158a30b68fc258b81a994f3fac10212Richard Smith
1992046a7466f23107bd752d9a8106aada85061699bcJohn McCall    return false;
1993046a7466f23107bd752d9a8106aada85061699bcJohn McCall
1994046a7466f23107bd752d9a8106aada85061699bcJohn McCall  // We found multiple suitable operators;  complain about the ambiguity.
1995046a7466f23107bd752d9a8106aada85061699bcJohn McCall  } else if (!Matches.empty()) {
19962be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt    if (Diagnose) {
1997cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt      Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found)
1998cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt        << Name << RD;
1999cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt
20005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      for (SmallVectorImpl<DeclAccessPair>::iterator
2001cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt             F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F)
2002cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt        Diag((*F)->getUnderlyingDecl()->getLocation(),
2003cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt             diag::note_member_declared_here) << Name;
2004cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt    }
2005046a7466f23107bd752d9a8106aada85061699bcJohn McCall    return true;
200678f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  }
200778f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson
200878f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  // We did find operator delete/operator delete[] declarations, but
200978f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  // none of them were suitable.
201078f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  if (!Found.empty()) {
20112be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt    if (Diagnose) {
2012cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt      Diag(StartLoc, diag::err_no_suitable_delete_member_function_found)
2013cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt        << Name << RD;
2014cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt
2015cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt      for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
2016cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt           F != FEnd; ++F)
2017cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt        Diag((*F)->getUnderlyingDecl()->getLocation(),
2018cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt             diag::note_member_declared_here) << Name;
2019cb45a0f42964ab5fa1474b25abcc1ae3a8bd0ab8Sean Hunt    }
202078f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson    return true;
202178f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  }
202278f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson
202378f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  // Look for a global declaration.
202478f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  DeclareGlobalNewDelete();
202578f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  DeclContext *TUDecl = Context.getTranslationUnitDecl();
2026dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
202778f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation());
202878f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  Expr* DeallocArgs[1];
202978f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  DeallocArgs[0] = &Null;
203078f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  if (FindAllocationOverload(StartLoc, SourceRange(), Name,
20312be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt                             DeallocArgs, 1, TUDecl, !Diagnose,
20322be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt                             Operator, Diagnose))
203378f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson    return true;
203478f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson
203578f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  assert(Operator && "Did not find a deallocation function!");
203678f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson  return false;
203778f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson}
203878f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson
20394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
20404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// @code ::delete ptr; @endcode
20414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// or
20424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl/// @code delete [] ptr; @endcode
204360d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
20444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian RedlSema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
2045429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                     bool ArrayForm, Expr *ExE) {
20469cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor  // C++ [expr.delete]p1:
20479cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor  //   The operand shall have a pointer type, or a class type having a single
20489cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor  //   conversion function to a pointer type. The result has type void.
20499cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor  //
20504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // DR599 amends "pointer type" to "pointer to object type" in both cases.
20514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2052429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  ExprResult Ex = Owned(ExE);
2053d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson  FunctionDecl *OperatorDelete = 0;
20544076dacf1497fb95cb298b9d964fbdbdaf9bde6cArgyrios Kyrtzidis  bool ArrayFormAsWritten = ArrayForm;
20556ec278d1a354517e20f13a877481453ee7940c78John McCall  bool UsualArrayDeleteWantsSize = false;
20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2057429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (!Ex.get()->isTypeDependent()) {
20585aba3eb1be234336767f86c7252ed307255f4d4dJohn McCall    // Perform lvalue-to-rvalue cast, if needed.
20595aba3eb1be234336767f86c7252ed307255f4d4dJohn McCall    Ex = DefaultLvalueConversion(Ex.take());
20605aba3eb1be234336767f86c7252ed307255f4d4dJohn McCall
2061429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    QualType Type = Ex.get()->getType();
20624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
20639cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor    if (const RecordType *Record = Type->getAs<RecordType>()) {
2064dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (RequireCompleteType(StartLoc, Type,
2065d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                              diag::err_delete_incomplete_class_type))
2066254a9427ff84d694724fdecd0642dad8ceaa0645Douglas Gregor        return ExprError();
2067dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
20685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<CXXConversionDecl*, 4> ObjectPtrConversions;
206932daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall
20705346278f81930e7fd0545bbbb2fc217c6921b109Fariborz Jahanian      CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
2071dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions();
2072eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall      for (UnresolvedSetImpl::iterator I = Conversions->begin(),
2073ba13543329afac4a0d01304ec2ec4924d99306a6John McCall             E = Conversions->end(); I != E; ++I) {
207432daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall        NamedDecl *D = I.getDecl();
207532daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall        if (isa<UsingShadowDecl>(D))
207632daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall          D = cast<UsingShadowDecl>(D)->getTargetDecl();
207732daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall
20789cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor        // Skip over templated conversion functions; they aren't considered.
207932daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall        if (isa<FunctionTemplateDecl>(D))
20809cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor          continue;
2081dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
208232daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall        CXXConversionDecl *Conv = cast<CXXConversionDecl>(D);
2083dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
20849cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor        QualType ConvType = Conv->getConversionType().getNonReferenceType();
20859cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor        if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>())
20861357869bc5983cdfbc986db1f3d18265bb34cb0eEli Friedman          if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType())
20878b915e7048b20b0feb60ec90c365a8eb68360cd4Fariborz Jahanian            ObjectPtrConversions.push_back(Conv);
20889cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor      }
20898b915e7048b20b0feb60ec90c365a8eb68360cd4Fariborz Jahanian      if (ObjectPtrConversions.size() == 1) {
20908b915e7048b20b0feb60ec90c365a8eb68360cd4Fariborz Jahanian        // We have a single conversion to a pointer-to-object type. Perform
20918b915e7048b20b0feb60ec90c365a8eb68360cd4Fariborz Jahanian        // that conversion.
209232daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall        // TODO: don't redo the conversion calculation.
2093429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        ExprResult Res =
2094429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          PerformImplicitConversion(Ex.get(),
209532daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall                            ObjectPtrConversions.front()->getConversionType(),
2096429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                    AA_Converting);
2097429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        if (Res.isUsable()) {
20983fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer          Ex = Res;
2099429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          Type = Ex.get()->getType();
21008b915e7048b20b0feb60ec90c365a8eb68360cd4Fariborz Jahanian        }
21018b915e7048b20b0feb60ec90c365a8eb68360cd4Fariborz Jahanian      }
21028b915e7048b20b0feb60ec90c365a8eb68360cd4Fariborz Jahanian      else if (ObjectPtrConversions.size() > 1) {
21038b915e7048b20b0feb60ec90c365a8eb68360cd4Fariborz Jahanian        Diag(StartLoc, diag::err_ambiguous_delete_operand)
2104429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley              << Type << Ex.get()->getSourceRange();
210532daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall        for (unsigned i= 0; i < ObjectPtrConversions.size(); i++)
210632daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall          NoteOverloadCandidate(ObjectPtrConversions[i]);
21078b915e7048b20b0feb60ec90c365a8eb68360cd4Fariborz Jahanian        return ExprError();
21089cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor      }
21092850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl    }
21104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2111f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl    if (!Type->isPointerType())
2112f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl      return ExprError(Diag(StartLoc, diag::err_delete_operand)
2113429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << Type << Ex.get()->getSourceRange());
21144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
21156217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = Type->getAs<PointerType>()->getPointeeType();
2116e52c91478a144f913019591ee1839e1846345f71Eli Friedman    QualType PointeeElem = Context.getBaseElementType(Pointee);
2117e52c91478a144f913019591ee1839e1846345f71Eli Friedman
2118e52c91478a144f913019591ee1839e1846345f71Eli Friedman    if (unsigned AddressSpace = Pointee.getAddressSpace())
2119e52c91478a144f913019591ee1839e1846345f71Eli Friedman      return Diag(Ex.get()->getLocStart(),
2120e52c91478a144f913019591ee1839e1846345f71Eli Friedman                  diag::err_address_space_qualified_delete)
2121e52c91478a144f913019591ee1839e1846345f71Eli Friedman               << Pointee.getUnqualifiedType() << AddressSpace;
2122e52c91478a144f913019591ee1839e1846345f71Eli Friedman
2123e52c91478a144f913019591ee1839e1846345f71Eli Friedman    CXXRecordDecl *PointeeRD = 0;
212494a615718d06704816c6e31a811f823c05e39f52Douglas Gregor    if (Pointee->isVoidType() && !isSFINAEContext()) {
2125dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // The C++ standard bans deleting a pointer to a non-object type, which
212694a615718d06704816c6e31a811f823c05e39f52Douglas Gregor      // effectively bans deletion of "void*". However, most compilers support
212794a615718d06704816c6e31a811f823c05e39f52Douglas Gregor      // this, so we treat it as a warning unless we're in a SFINAE context.
212894a615718d06704816c6e31a811f823c05e39f52Douglas Gregor      Diag(StartLoc, diag::ext_delete_void_ptr_operand)
2129429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << Type << Ex.get()->getSourceRange();
2130e52c91478a144f913019591ee1839e1846345f71Eli Friedman    } else if (Pointee->isFunctionType() || Pointee->isVoidType()) {
2131f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl      return ExprError(Diag(StartLoc, diag::err_delete_operand)
2132429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << Type << Ex.get()->getSourceRange());
2133e52c91478a144f913019591ee1839e1846345f71Eli Friedman    } else if (!Pointee->isDependentType()) {
2134e52c91478a144f913019591ee1839e1846345f71Eli Friedman      if (!RequireCompleteType(StartLoc, Pointee,
2135d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                               diag::warn_delete_incomplete, Ex.get())) {
2136e52c91478a144f913019591ee1839e1846345f71Eli Friedman        if (const RecordType *RT = PointeeElem->getAs<RecordType>())
2137e52c91478a144f913019591ee1839e1846345f71Eli Friedman          PointeeRD = cast<CXXRecordDecl>(RT->getDecl());
2138e52c91478a144f913019591ee1839e1846345f71Eli Friedman      }
2139e52c91478a144f913019591ee1839e1846345f71Eli Friedman    }
2140e52c91478a144f913019591ee1839e1846345f71Eli Friedman
21411070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregor    // C++ [expr.delete]p2:
2142dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   [Note: a pointer to a const type can be the operand of a
2143dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   delete-expression; it is not necessary to cast away the constness
2144dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   (5.2.11) of the pointer expression before it is used as the operand
21451070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregor    //   of the delete-expression. ]
21464076dacf1497fb95cb298b9d964fbdbdaf9bde6cArgyrios Kyrtzidis
21474076dacf1497fb95cb298b9d964fbdbdaf9bde6cArgyrios Kyrtzidis    if (Pointee->isArrayType() && !ArrayForm) {
21484076dacf1497fb95cb298b9d964fbdbdaf9bde6cArgyrios Kyrtzidis      Diag(StartLoc, diag::warn_delete_array_type)
2149429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          << Type << Ex.get()->getSourceRange()
21504076dacf1497fb95cb298b9d964fbdbdaf9bde6cArgyrios Kyrtzidis          << FixItHint::CreateInsertion(PP.getLocForEndOfToken(StartLoc), "[]");
21514076dacf1497fb95cb298b9d964fbdbdaf9bde6cArgyrios Kyrtzidis      ArrayForm = true;
21524076dacf1497fb95cb298b9d964fbdbdaf9bde6cArgyrios Kyrtzidis    }
21534076dacf1497fb95cb298b9d964fbdbdaf9bde6cArgyrios Kyrtzidis
2154d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson    DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName(
2155d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson                                      ArrayForm ? OO_Array_Delete : OO_Delete);
2156d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson
2157e52c91478a144f913019591ee1839e1846345f71Eli Friedman    if (PointeeRD) {
2158dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (!UseGlobal &&
2159e52c91478a144f913019591ee1839e1846345f71Eli Friedman          FindDeallocationFunction(StartLoc, PointeeRD, DeleteName,
2160e52c91478a144f913019591ee1839e1846345f71Eli Friedman                                   OperatorDelete))
216150724302e24d44a27e3bc45e7185a710d6eb3c2dAnders Carlsson        return ExprError();
2162dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
21636ec278d1a354517e20f13a877481453ee7940c78John McCall      // If we're allocating an array of records, check whether the
21646ec278d1a354517e20f13a877481453ee7940c78John McCall      // usual operator delete[] has a size_t parameter.
21656ec278d1a354517e20f13a877481453ee7940c78John McCall      if (ArrayForm) {
21666ec278d1a354517e20f13a877481453ee7940c78John McCall        // If the user specifically asked to use the global allocator,
21676ec278d1a354517e20f13a877481453ee7940c78John McCall        // we'll need to do the lookup into the class.
21686ec278d1a354517e20f13a877481453ee7940c78John McCall        if (UseGlobal)
21696ec278d1a354517e20f13a877481453ee7940c78John McCall          UsualArrayDeleteWantsSize =
21706ec278d1a354517e20f13a877481453ee7940c78John McCall            doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem);
21716ec278d1a354517e20f13a877481453ee7940c78John McCall
21726ec278d1a354517e20f13a877481453ee7940c78John McCall        // Otherwise, the usual operator delete[] should be the
21736ec278d1a354517e20f13a877481453ee7940c78John McCall        // function we just found.
21746ec278d1a354517e20f13a877481453ee7940c78John McCall        else if (isa<CXXMethodDecl>(OperatorDelete))
21756ec278d1a354517e20f13a877481453ee7940c78John McCall          UsualArrayDeleteWantsSize = (OperatorDelete->getNumParams() == 2);
21766ec278d1a354517e20f13a877481453ee7940c78John McCall      }
21776ec278d1a354517e20f13a877481453ee7940c78John McCall
2178213d70b58b4f48050c3e545ce1bd4b0ec3af74beRichard Smith      if (!PointeeRD->hasIrrelevantDestructor())
2179e52c91478a144f913019591ee1839e1846345f71Eli Friedman        if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) {
21805f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman          MarkFunctionReferenced(StartLoc,
218134374e6ce5710a91c478f69379220ff20c3e7f15Fariborz Jahanian                                    const_cast<CXXDestructorDecl*>(Dtor));
21829b623639378d53a675921ddfa7316034d571881eDouglas Gregor          DiagnoseUseOfDecl(Dtor, StartLoc);
21839b623639378d53a675921ddfa7316034d571881eDouglas Gregor        }
21846f0074ae2f466bae9f415da268d61a2dc1fabe26Argyrios Kyrtzidis
21856f0074ae2f466bae9f415da268d61a2dc1fabe26Argyrios Kyrtzidis      // C++ [expr.delete]p3:
21866f0074ae2f466bae9f415da268d61a2dc1fabe26Argyrios Kyrtzidis      //   In the first alternative (delete object), if the static type of the
21876f0074ae2f466bae9f415da268d61a2dc1fabe26Argyrios Kyrtzidis      //   object to be deleted is different from its dynamic type, the static
21886f0074ae2f466bae9f415da268d61a2dc1fabe26Argyrios Kyrtzidis      //   type shall be a base class of the dynamic type of the object to be
21896f0074ae2f466bae9f415da268d61a2dc1fabe26Argyrios Kyrtzidis      //   deleted and the static type shall have a virtual destructor or the
21906f0074ae2f466bae9f415da268d61a2dc1fabe26Argyrios Kyrtzidis      //   behavior is undefined.
21916f0074ae2f466bae9f415da268d61a2dc1fabe26Argyrios Kyrtzidis      //
21926f0074ae2f466bae9f415da268d61a2dc1fabe26Argyrios Kyrtzidis      // Note: a final class cannot be derived from, no issue there
2193ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman      if (PointeeRD->isPolymorphic() && !PointeeRD->hasAttr<FinalAttr>()) {
2194e52c91478a144f913019591ee1839e1846345f71Eli Friedman        CXXDestructorDecl *dtor = PointeeRD->getDestructor();
2195ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman        if (dtor && !dtor->isVirtual()) {
2196ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman          if (PointeeRD->isAbstract()) {
2197ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman            // If the class is abstract, we warn by default, because we're
2198ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman            // sure the code has undefined behavior.
2199ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman            Diag(StartLoc, diag::warn_delete_abstract_non_virtual_dtor)
2200ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman                << PointeeElem;
2201ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman          } else if (!ArrayForm) {
2202ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman            // Otherwise, if this is not an array delete, it's a bit suspect,
2203ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman            // but not necessarily wrong.
2204ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman            Diag(StartLoc, diag::warn_delete_non_virtual_dtor) << PointeeElem;
2205ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman          }
2206ef8c79c52200e4f7c32f8ef2744c1557b7f3f3eaEli Friedman        }
22076f0074ae2f466bae9f415da268d61a2dc1fabe26Argyrios Kyrtzidis      }
2208f85e193739c953358c865005855253af4f68a497John McCall
22094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (getLangOpts().ObjCAutoRefCount &&
2210f85e193739c953358c865005855253af4f68a497John McCall               PointeeElem->isObjCLifetimeType() &&
2211f85e193739c953358c865005855253af4f68a497John McCall               (PointeeElem.getObjCLifetime() == Qualifiers::OCL_Strong ||
2212f85e193739c953358c865005855253af4f68a497John McCall                PointeeElem.getObjCLifetime() == Qualifiers::OCL_Weak) &&
2213f85e193739c953358c865005855253af4f68a497John McCall               ArrayForm) {
2214f85e193739c953358c865005855253af4f68a497John McCall      Diag(StartLoc, diag::warn_err_new_delete_object_array)
2215f85e193739c953358c865005855253af4f68a497John McCall        << 1 << PointeeElem;
2216d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson    }
2217dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2218d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson    if (!OperatorDelete) {
221978f74551815f8b23bc0f8acf87640e963fad07d4Anders Carlsson      // Look for a global declaration.
2220d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson      DeclareGlobalNewDelete();
2221d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson      DeclContext *TUDecl = Context.getTranslationUnitDecl();
2222429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      Expr *Arg = Ex.get();
222334f60a4a7fb87e9f4dfd08f8751ce76db9981215Abramo Bagnara      if (!Context.hasSameType(Arg->getType(), Context.VoidPtrTy))
222434f60a4a7fb87e9f4dfd08f8751ce76db9981215Abramo Bagnara        Arg = ImplicitCastExpr::Create(Context, Context.VoidPtrTy,
222534f60a4a7fb87e9f4dfd08f8751ce76db9981215Abramo Bagnara                                       CK_BitCast, Arg, 0, VK_RValue);
22261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName,
2227429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                 &Arg, 1, TUDecl, /*AllowMissing=*/false,
2228d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson                                 OperatorDelete))
2229d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson        return ExprError();
2230d67c4c31a83773a4280bdf8d8b5097a1ce92488eAnders Carlsson    }
22311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22325f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    MarkFunctionReferenced(StartLoc, OperatorDelete);
22336ec278d1a354517e20f13a877481453ee7940c78John McCall
2234d880f52be3e4a8ccad92ac31932eeae5e0870a93Douglas Gregor    // Check access and ambiguity of operator delete and destructor.
2235e52c91478a144f913019591ee1839e1846345f71Eli Friedman    if (PointeeRD) {
2236e52c91478a144f913019591ee1839e1846345f71Eli Friedman      if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) {
2237429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor,
2238d880f52be3e4a8ccad92ac31932eeae5e0870a93Douglas Gregor                      PDiag(diag::err_access_dtor) << PointeeElem);
2239d880f52be3e4a8ccad92ac31932eeae5e0870a93Douglas Gregor      }
2240d880f52be3e4a8ccad92ac31932eeae5e0870a93Douglas Gregor    }
2241d880f52be3e4a8ccad92ac31932eeae5e0870a93Douglas Gregor
22422850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  }
22434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
2244f53597fb16142bdb4a66901f8c0b768db4f2a548Sebastian Redl  return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm,
22456ec278d1a354517e20f13a877481453ee7940c78John McCall                                           ArrayFormAsWritten,
22466ec278d1a354517e20f13a877481453ee7940c78John McCall                                           UsualArrayDeleteWantsSize,
2247429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                           OperatorDelete, Ex.take(), StartLoc));
22484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
22494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
22508cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor/// \brief Check the use of the given variable as a C++ condition in an if,
22518cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor/// while, do-while, or switch statement.
225260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar,
2253f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                        SourceLocation StmtLoc,
2254f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                        bool ConvertToBoolean) {
22558cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  QualType T = ConditionVar->getType();
2256dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
22578cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  // C++ [stmt.select]p2:
22588cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  //   The declarator shall not specify a function or an array.
22598cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  if (T->isFunctionType())
2260dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return ExprError(Diag(ConditionVar->getLocation(),
22618cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor                          diag::err_invalid_use_of_function_type)
22628cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor                       << ConditionVar->getSourceRange());
22638cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  else if (T->isArrayType())
2264dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return ExprError(Diag(ConditionVar->getLocation(),
22658cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor                          diag::err_invalid_use_of_array_type)
22668cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor                     << ConditionVar->getSourceRange());
2267a7605db78f39fa744da94faacee504e7601bb7c5Douglas Gregor
2268429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  ExprResult Condition =
2269e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    Owned(DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2270e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                              SourceLocation(),
2271e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                              ConditionVar,
2272f4b88a45902af1802a1cb42ba48b1c474474f228John McCall                              /*enclosing*/ false,
2273e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                              ConditionVar->getLocation(),
2274e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                              ConditionVar->getType().getNonReferenceType(),
2275429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                              VK_LValue));
2276cf7c14c3451658311aeaed84c6395082fa91e60dEli Friedman
22775f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  MarkDeclRefReferenced(cast<DeclRefExpr>(Condition.get()));
2278cf7c14c3451658311aeaed84c6395082fa91e60dEli Friedman
2279429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ConvertToBoolean) {
2280429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    Condition = CheckBooleanCondition(Condition.take(), StmtLoc);
2281429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Condition.isInvalid())
2282429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
2283429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
2284dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
22853fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Condition;
22868cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor}
22878cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor
22885921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
2289429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn WiegleyExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr) {
22905921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  // C++ 6.4p4:
22915921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  // The value of a condition that is an initialized declaration in a statement
22925921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  // other than a switch statement is the value of the declared variable
22935921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  // implicitly converted to type bool. If that conversion is ill-formed, the
22945921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  // program is ill-formed.
22955921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  // The value of a condition that is an expression is the value of the
22965921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  // expression, implicitly converted to bool.
22975921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  //
229809f41cf63f4df0bf4e98ee473e44e9a95b68f0ffDouglas Gregor  return PerformContextuallyConvertToBool(CondExpr);
22995921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis}
230077a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor
230177a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor/// Helper function to determine whether this is the (deprecated) C++
230277a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor/// conversion from a string literal to a pointer to non-const char or
230377a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor/// non-const wchar_t (for narrow and wide string literals,
230477a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor/// respectively).
23051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
230677a52233f7c0f162672652051bfe78b65ad4f789Douglas GregorSema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
230777a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  // Look inside the implicit cast, if it exists.
230877a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
230977a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor    From = Cast->getSubExpr();
231077a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor
231177a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  // A string literal (2.13.4) that is not a wide string literal can
231277a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  // be converted to an rvalue of type "pointer to char"; a wide
231377a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  // string literal can be converted to an rvalue of type "pointer
231477a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  // to wchar_t" (C++ 4.2p2).
23151984eb9a1522ad56e1310643a85f66b2b3424c91Douglas Gregor  if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens()))
23166217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const PointerType *ToPtrType = ToType->getAs<PointerType>())
23171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (const BuiltinType *ToPointeeType
2318183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall          = ToPtrType->getPointeeType()->getAs<BuiltinType>()) {
231977a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor        // This conversion is considered only when there is an
232077a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor        // explicit appropriate pointer target type (C++ 4.2p2).
23215cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor        if (!ToPtrType->getPointeeType().hasQualifiers()) {
23225cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor          switch (StrLit->getKind()) {
23235cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor            case StringLiteral::UTF8:
23245cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor            case StringLiteral::UTF16:
23255cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor            case StringLiteral::UTF32:
23265cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor              // We don't allow UTF literals to be implicitly converted
23275cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor              break;
23285cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor            case StringLiteral::Ascii:
23295cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor              return (ToPointeeType->getKind() == BuiltinType::Char_U ||
23305cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                      ToPointeeType->getKind() == BuiltinType::Char_S);
23315cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor            case StringLiteral::Wide:
23325cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor              return ToPointeeType->isWideCharType();
23335cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor          }
23345cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor        }
233577a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor      }
233677a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor
233777a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  return false;
233877a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor}
233994b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
2340dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic ExprResult BuildCXXCastArgument(Sema &S,
23412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall                                       SourceLocation CastLoc,
23422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall                                       QualType Ty,
23432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall                                       CastKind Kind,
23442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall                                       CXXMethodDecl *Method,
2345ca82a82082edc982a1fb5fcfef2dd2c8cf9bc824John McCall                                       DeclAccessPair FoundDecl,
23467cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                       bool HadMultipleCandidates,
23472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall                                       Expr *From) {
2348ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor  switch (Kind) {
2349b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Unhandled cast kind!");
23502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ConstructorConversion: {
235113e1bca90dc227e1e9c30900841f8bf976c0c83eDouglas Gregor    CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method);
23524e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer    SmallVector<Expr*, 8> ConstructorArgs;
2353dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
23545354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    if (S.CompleteConstructorCall(Constructor, From, CastLoc, ConstructorArgs))
2355f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      return ExprError();
2356dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2357b9abd87283ac6e929b7e12a577663bc99e61d020John McCall    S.CheckConstructorAccess(CastLoc, Constructor,
2358b9abd87283ac6e929b7e12a577663bc99e61d020John McCall                             InitializedEntity::InitializeTemporary(Ty),
2359b9abd87283ac6e929b7e12a577663bc99e61d020John McCall                             Constructor->getAccess());
236013e1bca90dc227e1e9c30900841f8bf976c0c83eDouglas Gregor
236113e1bca90dc227e1e9c30900841f8bf976c0c83eDouglas Gregor    ExprResult Result
236213e1bca90dc227e1e9c30900841f8bf976c0c83eDouglas Gregor      = S.BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method),
23633fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                ConstructorArgs,
236413e1bca90dc227e1e9c30900841f8bf976c0c83eDouglas Gregor                                HadMultipleCandidates, /*ZeroInit*/ false,
236513e1bca90dc227e1e9c30900841f8bf976c0c83eDouglas Gregor                                CXXConstructExpr::CK_Complete, SourceRange());
2366ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor    if (Result.isInvalid())
2367f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      return ExprError();
2368dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2369ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor    return S.MaybeBindToTemporary(Result.takeAs<Expr>());
2370ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor  }
2371dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
23722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UserDefinedConversion: {
2373ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor    assert(!From->getType()->isPointerType() && "Arg can't have pointer type!");
2374dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2375ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor    // Create an implicit call expr that calls it.
23763f01c8a58b7b2d78303675182a8de6b7fbe45ae1Eli Friedman    CXXConversionDecl *Conv = cast<CXXConversionDecl>(Method);
23773f01c8a58b7b2d78303675182a8de6b7fbe45ae1Eli Friedman    ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Conv,
23787cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                                 HadMultipleCandidates);
2379f2ae52605a49e5fc7a581f2c1ae02f1811034578Douglas Gregor    if (Result.isInvalid())
2380f2ae52605a49e5fc7a581f2c1ae02f1811034578Douglas Gregor      return ExprError();
2381960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara    // Record usage of conversion in an implicit cast.
2382960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara    Result = S.Owned(ImplicitCastExpr::Create(S.Context,
2383960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara                                              Result.get()->getType(),
2384960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara                                              CK_UserDefinedConversion,
2385960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara                                              Result.get(), 0,
2386960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara                                              Result.get()->getValueKind()));
2387dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2388ca82a82082edc982a1fb5fcfef2dd2c8cf9bc824John McCall    S.CheckMemberOperatorAccess(CastLoc, From, /*arg*/ 0, FoundDecl);
2389ca82a82082edc982a1fb5fcfef2dd2c8cf9bc824John McCall
2390f2ae52605a49e5fc7a581f2c1ae02f1811034578Douglas Gregor    return S.MaybeBindToTemporary(Result.get());
2391ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor  }
2392ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor  }
2393dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi}
2394ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor
239509f41cf63f4df0bf4e98ee473e44e9a95b68f0ffDouglas Gregor/// PerformImplicitConversion - Perform an implicit conversion of the
239609f41cf63f4df0bf4e98ee473e44e9a95b68f0ffDouglas Gregor/// expression From to the type ToType using the pre-computed implicit
2397429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley/// conversion sequence ICS. Returns the converted
23986864748fc9a780e6db0bb5a7bd20aa889882dc94Douglas Gregor/// expression. Action is the kind of conversion we're performing,
239909f41cf63f4df0bf4e98ee473e44e9a95b68f0ffDouglas Gregor/// used in the error message.
2400429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn WiegleyExprResult
2401429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn WiegleySema::PerformImplicitConversion(Expr *From, QualType ToType,
240209f41cf63f4df0bf4e98ee473e44e9a95b68f0ffDouglas Gregor                                const ImplicitConversionSequence &ICS,
2403f85e193739c953358c865005855253af4f68a497John McCall                                AssignmentAction Action,
2404f85e193739c953358c865005855253af4f68a497John McCall                                CheckedConversionKind CCK) {
24051d31833450e6d2947a33cb0840d87661d92eec07John McCall  switch (ICS.getKind()) {
2406429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  case ImplicitConversionSequence::StandardConversion: {
2407c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard,
2408c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                               Action, CCK);
2409429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Res.isInvalid())
2410429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
2411429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    From = Res.take();
241294b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    break;
2413429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
241494b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
2415f6c213a931f8eedf91531f3204cc828f18466fd5Anders Carlsson  case ImplicitConversionSequence::UserDefinedConversion: {
2416dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
24177fe5d72fbc8605b60d42d32394248ea76cf763a0Fariborz Jahanian      FunctionDecl *FD = ICS.UserDefined.ConversionFunction;
2418daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      CastKind CastKind;
2419f6c213a931f8eedf91531f3204cc828f18466fd5Anders Carlsson      QualType BeforeToType;
2420cc7a6484d8afd6f8bede2757666c42248228e408Sebastian Redl      assert(FD && "FIXME: aggregate initialization from init list");
2421f6c213a931f8eedf91531f3204cc828f18466fd5Anders Carlsson      if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) {
24222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        CastKind = CK_UserDefinedConversion;
2423dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2424f6c213a931f8eedf91531f3204cc828f18466fd5Anders Carlsson        // If the user-defined conversion is specified by a conversion function,
2425f6c213a931f8eedf91531f3204cc828f18466fd5Anders Carlsson        // the initial standard conversion sequence converts the source type to
2426f6c213a931f8eedf91531f3204cc828f18466fd5Anders Carlsson        // the implicit object parameter of the conversion function.
2427f6c213a931f8eedf91531f3204cc828f18466fd5Anders Carlsson        BeforeToType = Context.getTagDeclType(Conv->getParent());
24289ec9445402f4365fb0ac6e28e8be78cda3e9d8afJohn McCall      } else {
24299ec9445402f4365fb0ac6e28e8be78cda3e9d8afJohn McCall        const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD);
24302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        CastKind = CK_ConstructorConversion;
2431966256afd49a4af0c002046a19bb0041a507909bFariborz Jahanian        // Do no conversion if dealing with ... for the first conversion.
2432e44201a2841a38cc4165f630afd1709e7d9cd752Douglas Gregor        if (!ICS.UserDefined.EllipsisConversion) {
2433dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          // If the user-defined conversion is specified by a constructor, the
2434966256afd49a4af0c002046a19bb0041a507909bFariborz Jahanian          // initial standard conversion sequence converts the source type to the
2435966256afd49a4af0c002046a19bb0041a507909bFariborz Jahanian          // type required by the argument of the constructor
2436e44201a2841a38cc4165f630afd1709e7d9cd752Douglas Gregor          BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType();
2437e44201a2841a38cc4165f630afd1709e7d9cd752Douglas Gregor        }
2438dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      }
2439a3998bd364f8c6b4a520f479083da1d6e30482a1Douglas Gregor      // Watch out for elipsis conversion.
24404c0cea2d9e933195f3af4aaf7bb5c65de80bdefcFariborz Jahanian      if (!ICS.UserDefined.EllipsisConversion) {
2441429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        ExprResult Res =
2442c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith          PerformImplicitConversion(From, BeforeToType,
2443c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                    ICS.UserDefined.Before, AA_Converting,
2444c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                    CCK);
2445429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        if (Res.isInvalid())
2446429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          return ExprError();
2447429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        From = Res.take();
2448966256afd49a4af0c002046a19bb0041a507909bFariborz Jahanian      }
2449dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2450dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ExprResult CastArg
2451ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor        = BuildCXXCastArgument(*this,
2452ba70ab6e92ab3055acc898738e5f86739b91685eDouglas Gregor                               From->getLocStart(),
24530aebc81e02397a5987aaa8e8c7acbdb01a31d7c3Anders Carlsson                               ToType.getNonReferenceType(),
245483eecbefa4931b95231c9f2a61fb7b9b15e00eecDouglas Gregor                               CastKind, cast<CXXMethodDecl>(FD),
245583eecbefa4931b95231c9f2a61fb7b9b15e00eecDouglas Gregor                               ICS.UserDefined.FoundConversionFunction,
24567cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                               ICS.UserDefined.HadMultipleCandidates,
24579ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                               From);
24580aebc81e02397a5987aaa8e8c7acbdb01a31d7c3Anders Carlsson
24590aebc81e02397a5987aaa8e8c7acbdb01a31d7c3Anders Carlsson      if (CastArg.isInvalid())
2460429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        return ExprError();
2461d888962cff03b543fbe9ac6051ec6addf5b993b4Eli Friedman
2462429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      From = CastArg.take();
2463d888962cff03b543fbe9ac6051ec6addf5b993b4Eli Friedman
2464c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      return PerformImplicitConversion(From, ToType, ICS.UserDefined.After,
2465c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                       AA_Converting, CCK);
246693034ca3d025e948ddfcdd78868957efc70741a7Fariborz Jahanian  }
24671d31833450e6d2947a33cb0840d87661d92eec07John McCall
24681d31833450e6d2947a33cb0840d87661d92eec07John McCall  case ImplicitConversionSequence::AmbiguousConversion:
2469120d63cd4465230c2cd56508c7cd8e0ad00848e7John McCall    ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(),
24701d31833450e6d2947a33cb0840d87661d92eec07John McCall                          PDiag(diag::err_typecheck_ambiguous_condition)
24711d31833450e6d2947a33cb0840d87661d92eec07John McCall                            << From->getSourceRange());
2472429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley     return ExprError();
2473dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
247494b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  case ImplicitConversionSequence::EllipsisConversion:
2475b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Cannot perform an ellipsis conversion");
247694b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
247794b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  case ImplicitConversionSequence::BadConversion:
2478429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return ExprError();
247994b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  }
248094b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
248194b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  // Everything went well.
2482429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  return Owned(From);
248394b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor}
248494b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
2485c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith/// PerformImplicitConversion - Perform an implicit conversion of the
248694b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor/// expression From to the type ToType by following the standard
2487429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley/// conversion sequence SCS. Returns the converted
248845920e82bd87454545642ee2612e656a4140889dDouglas Gregor/// expression. Flavor is the context in which we're performing this
248945920e82bd87454545642ee2612e656a4140889dDouglas Gregor/// conversion, for use in error messages.
2490429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn WiegleyExprResult
2491c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard SmithSema::PerformImplicitConversion(Expr *From, QualType ToType,
249245920e82bd87454545642ee2612e656a4140889dDouglas Gregor                                const StandardConversionSequence& SCS,
2493f85e193739c953358c865005855253af4f68a497John McCall                                AssignmentAction Action,
2494f85e193739c953358c865005855253af4f68a497John McCall                                CheckedConversionKind CCK) {
2495f85e193739c953358c865005855253af4f68a497John McCall  bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast);
2496f85e193739c953358c865005855253af4f68a497John McCall
2497390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // Overall FIXME: we are recomputing too many types here and doing far too
2498390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // much extra work. What this means is that we need to keep track of more
2499390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // information that is computed when we try the implicit conversion initially,
2500390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // so that we don't need to recompute anything here.
250194b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  QualType FromType = From->getType();
2502f85e193739c953358c865005855253af4f68a497John McCall
2503225c41eb3e960fd2e1d1b547f0f19a278d608bc5Douglas Gregor  if (SCS.CopyConstructor) {
25047c3e8a1ddb19030a7937e02afd59607c40026348Anders Carlsson    // FIXME: When can ToType be a reference type?
25057c3e8a1ddb19030a7937e02afd59607c40026348Anders Carlsson    assert(!ToType->isReferenceType());
2506b3c477415f49668be7a770c8f44c217b42e3ccd3Fariborz Jahanian    if (SCS.Second == ICK_Derived_To_Base) {
25074e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer      SmallVector<Expr*, 8> ConstructorArgs;
2508b3c477415f49668be7a770c8f44c217b42e3ccd3Fariborz Jahanian      if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor),
25095354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                  From, /*FIXME:ConstructLoc*/SourceLocation(),
2510b3c477415f49668be7a770c8f44c217b42e3ccd3Fariborz Jahanian                                  ConstructorArgs))
2511429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        return ExprError();
2512429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
2513429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                   ToType, SCS.CopyConstructor,
25143fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                   ConstructorArgs,
25157cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                   /*HadMultipleCandidates*/ false,
2516429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                   /*ZeroInit*/ false,
2517429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                   CXXConstructExpr::CK_Complete,
2518429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                   SourceRange());
2519b3c477415f49668be7a770c8f44c217b42e3ccd3Fariborz Jahanian    }
2520429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
2521429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                 ToType, SCS.CopyConstructor,
25225354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer                                 From, /*HadMultipleCandidates*/ false,
2523429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                 /*ZeroInit*/ false,
2524429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                 CXXConstructExpr::CK_Complete,
2525429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                 SourceRange());
2526225c41eb3e960fd2e1d1b547f0f19a278d608bc5Douglas Gregor  }
2527225c41eb3e960fd2e1d1b547f0f19a278d608bc5Douglas Gregor
2528ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor  // Resolve overloaded function references.
2529ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor  if (Context.hasSameType(FromType, Context.OverloadTy)) {
2530ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor    DeclAccessPair Found;
2531ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor    FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType,
2532ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor                                                          true, Found);
2533ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor    if (!Fn)
2534429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
2535ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor
253696a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    if (DiagnoseUseOfDecl(Fn, From->getLocStart()))
2537429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
2538dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2539ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor    From = FixOverloadedFunctionReference(From, Found, Fn);
2540ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor    FromType = From->getType();
2541ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor  }
2542ad4e02f1711e5e90f4e653397b626e0d1929002cDouglas Gregor
2543c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith  // Perform the first implicit conversion.
254494b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  switch (SCS.First) {
254594b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  case ICK_Identity:
254694b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    // Nothing to do.
254794b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    break;
254894b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
2549d814eaf0dfb30f1cb6f90b056f8126f7e31e7ef4Eli Friedman  case ICK_Lvalue_To_Rvalue: {
25503c3b7f90a863af43fa63043d396553ecf205351cJohn McCall    assert(From->getObjectKind() != OK_ObjCProperty);
2551f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    FromType = FromType.getUnqualifiedType();
2552d814eaf0dfb30f1cb6f90b056f8126f7e31e7ef4Eli Friedman    ExprResult FromRes = DefaultLvalueConversion(From);
2553d814eaf0dfb30f1cb6f90b056f8126f7e31e7ef4Eli Friedman    assert(!FromRes.isInvalid() && "Can't perform deduced conversion?!");
2554d814eaf0dfb30f1cb6f90b056f8126f7e31e7ef4Eli Friedman    From = FromRes.take();
2555f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    break;
2556d814eaf0dfb30f1cb6f90b056f8126f7e31e7ef4Eli Friedman  }
2557f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
255894b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  case ICK_Array_To_Pointer:
255948f3bb9f780f6e64ab71ba0202ca04b07473805aDouglas Gregor    FromType = Context.getArrayDecayedType(FromType);
2560c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay,
2561c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             VK_RValue, /*BasePath=*/0, CCK).take();
256248f3bb9f780f6e64ab71ba0202ca04b07473805aDouglas Gregor    break;
256348f3bb9f780f6e64ab71ba0202ca04b07473805aDouglas Gregor
256448f3bb9f780f6e64ab71ba0202ca04b07473805aDouglas Gregor  case ICK_Function_To_Pointer:
256594b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    FromType = Context.getPointerType(FromType);
2566c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay,
2567c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             VK_RValue, /*BasePath=*/0, CCK).take();
256894b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    break;
256994b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
257094b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  default:
2571b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Improper first standard conversion");
257294b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  }
257394b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
2574c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith  // Perform the second implicit conversion
257594b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  switch (SCS.Second) {
257694b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  case ICK_Identity:
25772c7588f1260c6655cfb73126b695d2f79ae170bbSebastian Redl    // If both sides are functions (or pointers/references to them), there could
25782c7588f1260c6655cfb73126b695d2f79ae170bbSebastian Redl    // be incompatible exception declarations.
25792c7588f1260c6655cfb73126b695d2f79ae170bbSebastian Redl    if (CheckExceptionSpecCompatibility(From, ToType))
2580429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
25812c7588f1260c6655cfb73126b695d2f79ae170bbSebastian Redl    // Nothing else to do.
258294b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    break;
258394b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
258443c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor  case ICK_NoReturn_Adjustment:
258543c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    // If both sides are functions (or pointers/references to them), there could
258643c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    // be incompatible exception declarations.
258743c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    if (CheckExceptionSpecCompatibility(From, ToType))
2588429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
2589dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2590c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType, CK_NoOp,
2591c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             VK_RValue, /*BasePath=*/0, CCK).take();
259243c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    break;
2593dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
259494b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  case ICK_Integral_Promotion:
259594b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  case ICK_Integral_Conversion:
2596c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType, CK_IntegralCast,
2597c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             VK_RValue, /*BasePath=*/0, CCK).take();
259873c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman    break;
259973c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman
260073c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman  case ICK_Floating_Promotion:
260194b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  case ICK_Floating_Conversion:
2602c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType, CK_FloatingCast,
2603c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             VK_RValue, /*BasePath=*/0, CCK).take();
260473c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman    break;
260573c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman
260673c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman  case ICK_Complex_Promotion:
2607daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  case ICK_Complex_Conversion: {
2608daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType();
2609daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    QualType ToEl = ToType->getAs<ComplexType>()->getElementType();
2610daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    CastKind CK;
2611daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    if (FromEl->isRealFloatingType()) {
2612daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      if (ToEl->isRealFloatingType())
2613daa8e4e888758d55a7a759dd4a91b83921cef222John McCall        CK = CK_FloatingComplexCast;
2614daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      else
2615daa8e4e888758d55a7a759dd4a91b83921cef222John McCall        CK = CK_FloatingComplexToIntegralComplex;
2616daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    } else if (ToEl->isRealFloatingType()) {
2617daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      CK = CK_IntegralComplexToFloatingComplex;
2618daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    } else {
2619daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      CK = CK_IntegralComplexCast;
2620daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    }
2621c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType, CK,
2622c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             VK_RValue, /*BasePath=*/0, CCK).take();
262373c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman    break;
2624daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  }
262573c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman
262694b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  case ICK_Floating_Integral:
26270c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor    if (ToType->isRealFloatingType())
2628c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      From = ImpCastExprToType(From, ToType, CK_IntegralToFloating,
2629c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                               VK_RValue, /*BasePath=*/0, CCK).take();
263073c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman    else
2631c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral,
2632c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                               VK_RValue, /*BasePath=*/0, CCK).take();
263373c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman    break;
263473c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman
2635f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor  case ICK_Compatible_Conversion:
2636c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      From = ImpCastExprToType(From, ToType, CK_NoOp,
2637c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                               VK_RValue, /*BasePath=*/0, CCK).take();
263894b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    break;
263994b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
2640f85e193739c953358c865005855253af4f68a497John McCall  case ICK_Writeback_Conversion:
264161faec1e9cd1ddaaae9e7216c4b751681af271e4Anders Carlsson  case ICK_Pointer_Conversion: {
2642a3998bd364f8c6b4a520f479083da1d6e30482a1Douglas Gregor    if (SCS.IncompatibleObjC && Action != AA_Casting) {
264345920e82bd87454545642ee2612e656a4140889dDouglas Gregor      // Diagnose incompatible Objective-C conversions
26448cf0d22420eab58b8b434fa5d3a6e83c63ddefddDouglas Gregor      if (Action == AA_Initializing || Action == AA_Assigning)
264596a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(From->getLocStart(),
264684950c7fec2b056a424125997cb90451a3c83194Fariborz Jahanian             diag::ext_typecheck_convert_incompatible_pointer)
264784950c7fec2b056a424125997cb90451a3c83194Fariborz Jahanian          << ToType << From->getType() << Action
26486722155dfe056e2c3dfbacbcaffae04dea0c2be0Anna Zaks          << From->getSourceRange() << 0;
264984950c7fec2b056a424125997cb90451a3c83194Fariborz Jahanian      else
265096a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(From->getLocStart(),
265184950c7fec2b056a424125997cb90451a3c83194Fariborz Jahanian             diag::ext_typecheck_convert_incompatible_pointer)
265284950c7fec2b056a424125997cb90451a3c83194Fariborz Jahanian          << From->getType() << ToType << Action
26536722155dfe056e2c3dfbacbcaffae04dea0c2be0Anna Zaks          << From->getSourceRange() << 0;
2654f85e193739c953358c865005855253af4f68a497John McCall
2655926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      if (From->getType()->isObjCObjectPointerType() &&
2656926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor          ToType->isObjCObjectPointerType())
2657926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor        EmitRelatedResultTypeNote(From);
265882007c3a3f60ce2299c74333a881e4fdfc2135ceFariborz Jahanian    }
26594e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    else if (getLangOpts().ObjCAutoRefCount &&
266082007c3a3f60ce2299c74333a881e4fdfc2135ceFariborz Jahanian             !CheckObjCARCUnavailableWeakConversion(ToType,
266182007c3a3f60ce2299c74333a881e4fdfc2135ceFariborz Jahanian                                                    From->getType())) {
26627f3a6d37ba113910e2cbf7490d0f4bb162cd994eJohn McCall      if (Action == AA_Initializing)
266396a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(From->getLocStart(),
26647f3a6d37ba113910e2cbf7490d0f4bb162cd994eJohn McCall             diag::err_arc_weak_unavailable_assign);
26657f3a6d37ba113910e2cbf7490d0f4bb162cd994eJohn McCall      else
266696a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(From->getLocStart(),
26677f3a6d37ba113910e2cbf7490d0f4bb162cd994eJohn McCall             diag::err_arc_convesion_of_weak_unavailable)
26687f3a6d37ba113910e2cbf7490d0f4bb162cd994eJohn McCall          << (Action == AA_Casting) << From->getType() << ToType
26697f3a6d37ba113910e2cbf7490d0f4bb162cd994eJohn McCall          << From->getSourceRange();
26707f3a6d37ba113910e2cbf7490d0f4bb162cd994eJohn McCall    }
267182007c3a3f60ce2299c74333a881e4fdfc2135ceFariborz Jahanian
2672daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    CastKind Kind = CK_Invalid;
2673f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    CXXCastPath BasePath;
267414d0aee957f11b9613fa4312919bec3cc5456a1cDouglas Gregor    if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle))
2675429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
2676dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2677dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    // Make sure we extend blocks if necessary.
2678dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    // FIXME: doing this here is really ugly.
2679dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    if (Kind == CK_BlockPointerToObjCPointerCast) {
2680dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      ExprResult E = From;
2681dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      (void) PrepareCastToObjCObjectPointer(E);
2682dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall      From = E.take();
2683dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall    }
2684dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall
2685c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK)
2686c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith             .take();
268794b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    break;
268861faec1e9cd1ddaaae9e7216c4b751681af271e4Anders Carlsson  }
2689dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
269061faec1e9cd1ddaaae9e7216c4b751681af271e4Anders Carlsson  case ICK_Pointer_Member: {
2691daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    CastKind Kind = CK_Invalid;
2692f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    CXXCastPath BasePath;
269314d0aee957f11b9613fa4312919bec3cc5456a1cDouglas Gregor    if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle))
2694429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
26952c7588f1260c6655cfb73126b695d2f79ae170bbSebastian Redl    if (CheckExceptionSpecCompatibility(From, ToType))
2696429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
2697c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK)
2698c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith             .take();
269961faec1e9cd1ddaaae9e7216c4b751681af271e4Anders Carlsson    break;
270061faec1e9cd1ddaaae9e7216c4b751681af271e4Anders Carlsson  }
2701dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2702737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case ICK_Boolean_Conversion:
2703aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    // Perform half-to-boolean conversion via float.
2704aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    if (From->getType()->isHalfType()) {
2705aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).take();
2706aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      FromType = Context.FloatTy;
2707aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    }
2708aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
2709c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, Context.BoolTy,
2710c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             ScalarTypeToBooleanCastKind(FromType),
2711c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             VK_RValue, /*BasePath=*/0, CCK).take();
271294b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    break;
271394b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
27146fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  case ICK_Derived_To_Base: {
2715f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    CXXCastPath BasePath;
2716dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (CheckDerivedToBaseConversion(From->getType(),
2717b7a86f5c5f9cdea126817247cc1fd3a7441388daDouglas Gregor                                     ToType.getNonReferenceType(),
2718b7a86f5c5f9cdea126817247cc1fd3a7441388daDouglas Gregor                                     From->getLocStart(),
2719dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                     From->getSourceRange(),
27206fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor                                     &BasePath,
272114d0aee957f11b9613fa4312919bec3cc5456a1cDouglas Gregor                                     CStyle))
2722429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
27236fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
2724c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType.getNonReferenceType(),
2725c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                      CK_DerivedToBase, From->getValueKind(),
2726c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                      &BasePath, CCK).take();
2727b7a86f5c5f9cdea126817247cc1fd3a7441388daDouglas Gregor    break;
27286fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  }
27296fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
2730fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor  case ICK_Vector_Conversion:
2731c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType, CK_BitCast,
2732c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             VK_RValue, /*BasePath=*/0, CCK).take();
2733fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor    break;
2734fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor
2735fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor  case ICK_Vector_Splat:
2736c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType, CK_VectorSplat,
2737c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             VK_RValue, /*BasePath=*/0, CCK).take();
2738fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor    break;
2739dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2740fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor  case ICK_Complex_Real:
2741daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    // Case 1.  x -> _Complex y
2742daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) {
2743daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      QualType ElType = ToComplex->getElementType();
2744daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      bool isFloatingComplex = ElType->isRealFloatingType();
2745daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
2746daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      // x -> y
2747daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      if (Context.hasSameUnqualifiedType(ElType, From->getType())) {
2748daa8e4e888758d55a7a759dd4a91b83921cef222John McCall        // do nothing
2749daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      } else if (From->getType()->isRealFloatingType()) {
2750c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith        From = ImpCastExprToType(From, ElType,
2751c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).take();
2752daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      } else {
2753daa8e4e888758d55a7a759dd4a91b83921cef222John McCall        assert(From->getType()->isIntegerType());
2754c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith        From = ImpCastExprToType(From, ElType,
2755c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).take();
2756daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      }
2757daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      // y -> _Complex y
2758c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      From = ImpCastExprToType(From, ToType,
2759c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                   isFloatingComplex ? CK_FloatingRealToComplex
2760c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                     : CK_IntegralRealToComplex).take();
2761daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
2762daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    // Case 2.  _Complex x -> y
2763daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    } else {
2764daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      const ComplexType *FromComplex = From->getType()->getAs<ComplexType>();
2765daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      assert(FromComplex);
2766daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
2767daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      QualType ElType = FromComplex->getElementType();
2768daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      bool isFloatingComplex = ElType->isRealFloatingType();
2769daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
2770daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      // _Complex x -> x
2771c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      From = ImpCastExprToType(From, ElType,
2772c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                   isFloatingComplex ? CK_FloatingComplexToReal
2773c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                     : CK_IntegralComplexToReal,
2774c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                               VK_RValue, /*BasePath=*/0, CCK).take();
2775daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
2776daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      // x -> y
2777daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      if (Context.hasSameUnqualifiedType(ElType, ToType)) {
2778daa8e4e888758d55a7a759dd4a91b83921cef222John McCall        // do nothing
2779daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      } else if (ToType->isRealFloatingType()) {
2780c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith        From = ImpCastExprToType(From, ToType,
2781c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                   isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating,
2782c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                 VK_RValue, /*BasePath=*/0, CCK).take();
2783daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      } else {
2784daa8e4e888758d55a7a759dd4a91b83921cef222John McCall        assert(ToType->isIntegerType());
2785c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith        From = ImpCastExprToType(From, ToType,
2786c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                   isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast,
2787c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                 VK_RValue, /*BasePath=*/0, CCK).take();
2788daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      }
2789daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    }
2790fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor    break;
2791e3c8c64e7735c3589e1a34e6000c93183a55920cFariborz Jahanian
2792e3c8c64e7735c3589e1a34e6000c93183a55920cFariborz Jahanian  case ICK_Block_Pointer_Conversion: {
2793c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast,
2794c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             VK_RValue, /*BasePath=*/0, CCK).take();
2795f85e193739c953358c865005855253af4f68a497John McCall    break;
2796f85e193739c953358c865005855253af4f68a497John McCall  }
2797e3c8c64e7735c3589e1a34e6000c93183a55920cFariborz Jahanian
2798d97f558f48865aa96e7bdf9c1c9315790c3d77c9Fariborz Jahanian  case ICK_TransparentUnionConversion: {
2799429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult FromRes = Owned(From);
2800d97f558f48865aa96e7bdf9c1c9315790c3d77c9Fariborz Jahanian    Sema::AssignConvertType ConvTy =
2801429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      CheckTransparentUnionArgumentConstraints(ToType, FromRes);
2802429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (FromRes.isInvalid())
2803429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
2804429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    From = FromRes.take();
2805d97f558f48865aa96e7bdf9c1c9315790c3d77c9Fariborz Jahanian    assert ((ConvTy == Sema::Compatible) &&
2806d97f558f48865aa96e7bdf9c1c9315790c3d77c9Fariborz Jahanian            "Improper transparent union conversion");
2807d97f558f48865aa96e7bdf9c1c9315790c3d77c9Fariborz Jahanian    (void)ConvTy;
2808d97f558f48865aa96e7bdf9c1c9315790c3d77c9Fariborz Jahanian    break;
2809d97f558f48865aa96e7bdf9c1c9315790c3d77c9Fariborz Jahanian  }
2810d97f558f48865aa96e7bdf9c1c9315790c3d77c9Fariborz Jahanian
2811fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor  case ICK_Lvalue_To_Rvalue:
2812fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor  case ICK_Array_To_Pointer:
2813fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor  case ICK_Function_To_Pointer:
2814fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor  case ICK_Qualification:
2815fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor  case ICK_Num_Conversion_Kinds:
2816b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Improper second standard conversion");
281794b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  }
281894b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
281994b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  switch (SCS.Third) {
282094b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  case ICK_Identity:
282194b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    // Nothing to do.
282294b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    break;
282394b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
2824906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  case ICK_Qualification: {
2825906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    // The qualification keeps the category of the inner expression, unless the
2826906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    // target type isn't a reference.
28275baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    ExprValueKind VK = ToType->isReferenceType() ?
2828c1c0dfb376b829b94d4c61e9f358ce23e6aa3169Eli Friedman                                  From->getValueKind() : VK_RValue;
2829c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context),
2830c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                             CK_NoOp, VK, /*BasePath=*/0, CCK).take();
2831a9bff30776888977f580c9cac212fd1583ee963eDouglas Gregor
2832069a6da34a86c97ffe78c379da246fc8116daaffDouglas Gregor    if (SCS.DeprecatedStringLiteralToCharPtr &&
28334e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        !getLangOpts().WritableStrings)
2834a9bff30776888977f580c9cac212fd1583ee963eDouglas Gregor      Diag(From->getLocStart(), diag::warn_deprecated_string_literal_conversion)
2835a9bff30776888977f580c9cac212fd1583ee963eDouglas Gregor        << ToType.getNonReferenceType();
2836a9bff30776888977f580c9cac212fd1583ee963eDouglas Gregor
283794b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor    break;
2838906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    }
2839906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
284094b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  default:
2841b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Improper third standard conversion");
284294b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor  }
284394b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
284447bfcca2d6972d98a1b25239cd1aa658b60680e2Douglas Gregor  // If this conversion sequence involved a scalar -> atomic conversion, perform
284547bfcca2d6972d98a1b25239cd1aa658b60680e2Douglas Gregor  // that conversion now.
284647bfcca2d6972d98a1b25239cd1aa658b60680e2Douglas Gregor  if (const AtomicType *ToAtomic = ToType->getAs<AtomicType>())
284747bfcca2d6972d98a1b25239cd1aa658b60680e2Douglas Gregor    if (Context.hasSameType(ToAtomic->getValueType(), From->getType()))
284847bfcca2d6972d98a1b25239cd1aa658b60680e2Douglas Gregor      From = ImpCastExprToType(From, ToType, CK_NonAtomicToAtomic, VK_RValue, 0,
284947bfcca2d6972d98a1b25239cd1aa658b60680e2Douglas Gregor                               CCK).take();
285047bfcca2d6972d98a1b25239cd1aa658b60680e2Douglas Gregor
2851429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  return Owned(From);
285294b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor}
285394b1dd2368dc9eeedf2794db654deae225fac763Douglas Gregor
28540dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian RedlExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait UTT,
28553d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor                                     SourceLocation KWLoc,
28563d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor                                     ParsedType Ty,
28573d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor                                     SourceLocation RParen) {
28583d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor  TypeSourceInfo *TSInfo;
28593d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor  QualType T = GetTypeFromParser(Ty, &TSInfo);
28603d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor
28613d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor  if (!TSInfo)
28623d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor    TSInfo = Context.getTrivialTypeSourceInfo(T);
28630dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  return BuildUnaryTypeTrait(UTT, KWLoc, TSInfo, RParen);
28643d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor}
28651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2866ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth/// \brief Check the completeness of a type in a unary type trait.
2867ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth///
2868ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth/// If the particular type trait requires a complete type, tries to complete
2869ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth/// it. If completing the type fails, a diagnostic is emitted and false
2870ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth/// returned. If completing the type succeeds or no completion was required,
2871ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth/// returns true.
2872ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruthstatic bool CheckUnaryTypeTraitTypeCompleteness(Sema &S,
2873ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth                                                UnaryTypeTrait UTT,
2874ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth                                                SourceLocation Loc,
2875ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth                                                QualType ArgTy) {
2876ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  // C++0x [meta.unary.prop]p3:
2877ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  //   For all of the class templates X declared in this Clause, instantiating
2878ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  //   that template with a template argument that is a class template
2879ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  //   specialization may result in the implicit instantiation of the template
2880ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  //   argument if and only if the semantics of X require that the argument
2881ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  //   must be a complete type.
2882ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  // We apply this rule to all the type trait expressions used to implement
2883ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  // these class templates. We also try to follow any GCC documented behavior
2884ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  // in these expressions to ensure portability of standard libraries.
2885ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  switch (UTT) {
2886ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // is_complete_type somewhat obviously cannot require a complete type.
2887ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsCompleteType:
2888d6efe9b66fce943ada47100e90237e710c4716feChandler Carruth    // Fall-through
2889ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth
2890ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // These traits are modeled on the type predicates in C++0x
2891ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // [meta.unary.cat] and [meta.unary.comp]. They are not specified as
2892ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // requiring a complete type, as whether or not they return true cannot be
2893ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // impacted by the completeness of the type.
2894ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsVoid:
2895ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsIntegral:
2896ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsFloatingPoint:
2897ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsArray:
2898ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsPointer:
2899ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsLvalueReference:
2900ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsRvalueReference:
2901ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsMemberFunctionPointer:
2902ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsMemberObjectPointer:
2903ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsEnum:
2904ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsUnion:
2905ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsClass:
2906ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsFunction:
2907ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsReference:
2908ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsArithmetic:
2909ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsFundamental:
2910ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsObject:
2911ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsScalar:
2912ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsCompound:
2913ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsMemberPointer:
2914d6efe9b66fce943ada47100e90237e710c4716feChandler Carruth    // Fall-through
2915ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth
2916ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // These traits are modeled on type predicates in C++0x [meta.unary.prop]
2917ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // which requires some of its traits to have the complete type. However,
2918ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // the completeness of the type cannot impact these traits' semantics, and
2919ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // so they don't require it. This matches the comments on these traits in
2920ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // Table 49.
2921ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsConst:
2922ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsVolatile:
2923ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsSigned:
2924ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsUnsigned:
2925ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    return true;
2926ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth
2927ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // C++0x [meta.unary.prop] Table 49 requires the following traits to be
2928d6efe9b66fce943ada47100e90237e710c4716feChandler Carruth    // applied to a complete type.
2929ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsTrivial:
2930feb375d31b7e9108b04a9f55b721d5e0c793a558Sean Hunt  case UTT_IsTriviallyCopyable:
2931ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsStandardLayout:
2932ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsPOD:
2933ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsLiteral:
2934ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsEmpty:
2935ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsPolymorphic:
2936ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_IsAbstract:
2937d6efe9b66fce943ada47100e90237e710c4716feChandler Carruth    // Fall-through
2938ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth
29395e9392ba18f5925e26cc5714d1412eda0d219826Douglas Gregor  // These traits require a complete type.
29405e9392ba18f5925e26cc5714d1412eda0d219826Douglas Gregor  case UTT_IsFinal:
29415e9392ba18f5925e26cc5714d1412eda0d219826Douglas Gregor
2942d6efe9b66fce943ada47100e90237e710c4716feChandler Carruth    // These trait expressions are designed to help implement predicates in
2943ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // [meta.unary.prop] despite not being named the same. They are specified
2944ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // by both GCC and the Embarcadero C++ compiler, and require the complete
2945ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // type due to the overarching C++0x type predicates being implemented
2946ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // requiring the complete type.
2947ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_HasNothrowAssign:
2948ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_HasNothrowConstructor:
2949ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_HasNothrowCopy:
2950ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_HasTrivialAssign:
2951023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  case UTT_HasTrivialDefaultConstructor:
2952ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_HasTrivialCopy:
2953ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_HasTrivialDestructor:
2954ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth  case UTT_HasVirtualDestructor:
2955ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // Arrays of unknown bound are expressly allowed.
2956ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    QualType ElTy = ArgTy;
2957ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    if (ArgTy->isIncompleteArrayType())
2958ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth      ElTy = S.Context.getAsArrayType(ArgTy)->getElementType();
2959ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth
2960ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    // The void type is expressly allowed.
2961ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    if (ElTy->isVoidType())
2962ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth      return true;
2963ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth
2964ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    return !S.RequireCompleteType(
2965ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth      Loc, ElTy, diag::err_incomplete_type_used_in_type_trait_expr);
2966cf5664114eb75c6a5fef2bed1c0f0d0fb19debc9John Wiegley  }
296773e0a91c9130a048dfb0928643b9cde1f838b87dChandler Carruth  llvm_unreachable("Type trait not handled by switch");
2968ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth}
2969ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth
2970ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruthstatic bool EvaluateUnaryTypeTrait(Sema &Self, UnaryTypeTrait UTT,
2971ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth                                   SourceLocation KeyLoc, QualType T) {
2972d064c70bc63ad742d11625e485c1dc1631f6e303Chandler Carruth  assert(!T->isDependentType() && "Cannot evaluate traits of dependent type");
2973cf5664114eb75c6a5fef2bed1c0f0d0fb19debc9John Wiegley
29740dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  ASTContext &C = Self.Context;
29750dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  switch(UTT) {
2976c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // Type trait expressions corresponding to the primary type category
2977c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // predicates in C++0x [meta.unary.cat].
2978c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsVoid:
2979c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T->isVoidType();
298020c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case UTT_IsIntegral:
298120c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley    return T->isIntegralType(C);
298220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case UTT_IsFloatingPoint:
298320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley    return T->isFloatingType();
298420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case UTT_IsArray:
298520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley    return T->isArrayType();
2986c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsPointer:
2987c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T->isPointerType();
298820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case UTT_IsLvalueReference:
298920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley    return T->isLValueReferenceType();
2990c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsRvalueReference:
2991c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T->isRValueReferenceType();
299220c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case UTT_IsMemberFunctionPointer:
299320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley    return T->isMemberFunctionPointerType();
299420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case UTT_IsMemberObjectPointer:
299520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley    return T->isMemberDataPointerType();
2996c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsEnum:
2997c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T->isEnumeralType();
2998c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsUnion:
2999aaf147be3cc7403d8f7889d363421bb291ef23f2Chandler Carruth    return T->isUnionType();
3000c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsClass:
30016666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos    return T->isClassType() || T->isStructureType() || T->isInterfaceType();
3002c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsFunction:
3003c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T->isFunctionType();
3004c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth
3005c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // Type trait expressions which correspond to the convenient composition
3006c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // predicates in C++0x [meta.unary.comp].
3007c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsReference:
3008c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T->isReferenceType();
3009c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsArithmetic:
3010aaf147be3cc7403d8f7889d363421bb291ef23f2Chandler Carruth    return T->isArithmeticType() && !T->isEnumeralType();
3011c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsFundamental:
3012aaf147be3cc7403d8f7889d363421bb291ef23f2Chandler Carruth    return T->isFundamentalType();
301320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case UTT_IsObject:
3014aaf147be3cc7403d8f7889d363421bb291ef23f2Chandler Carruth    return T->isObjectType();
301520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case UTT_IsScalar:
3016f85e193739c953358c865005855253af4f68a497John McCall    // Note: semantic analysis depends on Objective-C lifetime types to be
3017f85e193739c953358c865005855253af4f68a497John McCall    // considered scalar types. However, such types do not actually behave
3018f85e193739c953358c865005855253af4f68a497John McCall    // like scalar types at run time (since they may require retain/release
3019f85e193739c953358c865005855253af4f68a497John McCall    // operations), so we report them as non-scalar.
3020f85e193739c953358c865005855253af4f68a497John McCall    if (T->isObjCLifetimeType()) {
3021f85e193739c953358c865005855253af4f68a497John McCall      switch (T.getObjCLifetime()) {
3022f85e193739c953358c865005855253af4f68a497John McCall      case Qualifiers::OCL_None:
3023f85e193739c953358c865005855253af4f68a497John McCall      case Qualifiers::OCL_ExplicitNone:
3024f85e193739c953358c865005855253af4f68a497John McCall        return true;
3025f85e193739c953358c865005855253af4f68a497John McCall
3026f85e193739c953358c865005855253af4f68a497John McCall      case Qualifiers::OCL_Strong:
3027f85e193739c953358c865005855253af4f68a497John McCall      case Qualifiers::OCL_Weak:
3028f85e193739c953358c865005855253af4f68a497John McCall      case Qualifiers::OCL_Autoreleasing:
3029f85e193739c953358c865005855253af4f68a497John McCall        return false;
3030f85e193739c953358c865005855253af4f68a497John McCall      }
3031f85e193739c953358c865005855253af4f68a497John McCall    }
3032f85e193739c953358c865005855253af4f68a497John McCall
3033cec0ced5bd3de14d7cebbc1a240618282c91f656Chandler Carruth    return T->isScalarType();
3034c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsCompound:
3035aaf147be3cc7403d8f7889d363421bb291ef23f2Chandler Carruth    return T->isCompoundType();
3036c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsMemberPointer:
3037c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T->isMemberPointerType();
3038c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth
3039c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // Type trait expressions which correspond to the type property predicates
3040c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // in C++0x [meta.unary.prop].
3041c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsConst:
3042c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T.isConstQualified();
3043c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsVolatile:
3044c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T.isVolatileQualified();
3045c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsTrivial:
3046f85e193739c953358c865005855253af4f68a497John McCall    return T.isTrivialType(Self.Context);
3047feb375d31b7e9108b04a9f55b721d5e0c793a558Sean Hunt  case UTT_IsTriviallyCopyable:
3048f85e193739c953358c865005855253af4f68a497John McCall    return T.isTriviallyCopyableType(Self.Context);
304920c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case UTT_IsStandardLayout:
3050636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    return T->isStandardLayoutType();
3051c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsPOD:
3052470360d8035ce4213a963d281c3b542edfea9fdaBenjamin Kramer    return T.isPODType(Self.Context);
3053c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsLiteral:
3054c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T->isLiteralType();
3055c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsEmpty:
3056c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3057c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth      return !RD->isUnion() && RD->isEmpty();
3058c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return false;
3059c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsPolymorphic:
3060c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3061c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth      return RD->isPolymorphic();
3062c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return false;
3063c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsAbstract:
3064c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3065c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth      return RD->isAbstract();
3066c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return false;
30675e9392ba18f5925e26cc5714d1412eda0d219826Douglas Gregor  case UTT_IsFinal:
30685e9392ba18f5925e26cc5714d1412eda0d219826Douglas Gregor    if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
30695e9392ba18f5925e26cc5714d1412eda0d219826Douglas Gregor      return RD->hasAttr<FinalAttr>();
30705e9392ba18f5925e26cc5714d1412eda0d219826Douglas Gregor    return false;
3071c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsSigned:
3072c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return T->isSignedIntegerType();
307320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case UTT_IsUnsigned:
307420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley    return T->isUnsignedIntegerType();
3075c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth
3076c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // Type trait expressions which query classes regarding their construction,
3077c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // destruction, and copying. Rather than being based directly on the
3078c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // related type predicates in the standard, they are specified by both
3079c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those
3080c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // specifications.
3081c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    //
3082c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    //   1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html
3083c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    //   2: http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index
3084023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  case UTT_HasTrivialDefaultConstructor:
30850dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
30860dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   If __is_pod (type) is true then the trait is true, else if type is
30870dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   a cv class or union type (or array thereof) with a trivial default
30880dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   constructor ([class.ctor]) then the trait is true, else it is false.
3089f85e193739c953358c865005855253af4f68a497John McCall    if (T.isPODType(Self.Context))
30900dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return true;
30910dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (const RecordType *RT =
30920dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl          C.getBaseElementType(T)->getAs<RecordType>())
3093023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt      return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDefaultConstructor();
30940dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return false;
30950dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  case UTT_HasTrivialCopy:
30960dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
30970dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   If __is_pod (type) is true or type is a reference type then
30980dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   the trait is true, else if type is a cv class or union type
30990dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   with a trivial copy constructor ([class.copy]) then the trait
31000dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   is true, else it is false.
3101f85e193739c953358c865005855253af4f68a497John McCall    if (T.isPODType(Self.Context) || T->isReferenceType())
31020dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return true;
31030dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (const RecordType *RT = T->getAs<RecordType>())
31040dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor();
31050dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return false;
31060dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  case UTT_HasTrivialAssign:
31070dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
31080dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   If type is const qualified or is a reference type then the
31090dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   trait is false. Otherwise if __is_pod (type) is true then the
31100dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   trait is true, else if type is a cv class or union type with
31110dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   a trivial copy assignment ([class.copy]) then the trait is
31120dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   true, else it is false.
31130dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // Note: the const and reference restrictions are interesting,
31140dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // given that const and reference members don't prevent a class
31150dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // from having a trivial copy assignment operator (but do cause
31160dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // errors if the copy assignment operator is actually used, q.v.
31170dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // [class.copy]p12).
31180dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl
31190dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (C.getBaseElementType(T).isConstQualified())
31200dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return false;
3121f85e193739c953358c865005855253af4f68a497John McCall    if (T.isPODType(Self.Context))
31220dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return true;
31230dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (const RecordType *RT = T->getAs<RecordType>())
31240dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment();
31250dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return false;
31260dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  case UTT_HasTrivialDestructor:
31270dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
31280dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   If __is_pod (type) is true or type is a reference type
31290dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   then the trait is true, else if type is a cv class or union
31300dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   type (or array thereof) with a trivial destructor
31310dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   ([class.dtor]) then the trait is true, else it is
31320dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   false.
3133f85e193739c953358c865005855253af4f68a497John McCall    if (T.isPODType(Self.Context) || T->isReferenceType())
31340dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return true;
3135f85e193739c953358c865005855253af4f68a497John McCall
3136f85e193739c953358c865005855253af4f68a497John McCall    // Objective-C++ ARC: autorelease types don't require destruction.
3137f85e193739c953358c865005855253af4f68a497John McCall    if (T->isObjCLifetimeType() &&
3138f85e193739c953358c865005855253af4f68a497John McCall        T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing)
3139f85e193739c953358c865005855253af4f68a497John McCall      return true;
3140f85e193739c953358c865005855253af4f68a497John McCall
31410dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (const RecordType *RT =
31420dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl          C.getBaseElementType(T)->getAs<RecordType>())
31430dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor();
31440dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return false;
31450dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  // TODO: Propagate nothrowness for implicitly declared special members.
31460dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  case UTT_HasNothrowAssign:
31470dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
31480dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   If type is const qualified or is a reference type then the
31490dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   trait is false. Otherwise if __has_trivial_assign (type)
31500dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   is true then the trait is true, else if type is a cv class
31510dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   or union type with copy assignment operators that are known
31520dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   not to throw an exception then the trait is true, else it is
31530dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   false.
31540dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (C.getBaseElementType(T).isConstQualified())
31550dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return false;
31560dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (T->isReferenceType())
31570dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return false;
3158f85e193739c953358c865005855253af4f68a497John McCall    if (T.isPODType(Self.Context) || T->isObjCLifetimeType())
3159f85e193739c953358c865005855253af4f68a497John McCall      return true;
31600dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (const RecordType *RT = T->getAs<RecordType>()) {
31610dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      CXXRecordDecl* RD = cast<CXXRecordDecl>(RT->getDecl());
31620dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      if (RD->hasTrivialCopyAssignment())
31630dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl        return true;
31640dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl
31650dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      bool FoundAssign = false;
31660dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      DeclarationName Name = C.DeclarationNames.getCXXOperatorName(OO_Equal);
3167f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl      LookupResult Res(Self, DeclarationNameInfo(Name, KeyLoc),
3168f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl                       Sema::LookupOrdinaryName);
3169f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl      if (Self.LookupQualifiedName(Res, RD)) {
3170d41679d6881d2b424d8b3600fc774308087735a7Douglas Gregor        Res.suppressDiagnostics();
3171f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl        for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end();
3172f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl             Op != OpEnd; ++Op) {
3173d41679d6881d2b424d8b3600fc774308087735a7Douglas Gregor          if (isa<FunctionTemplateDecl>(*Op))
3174d41679d6881d2b424d8b3600fc774308087735a7Douglas Gregor            continue;
3175d41679d6881d2b424d8b3600fc774308087735a7Douglas Gregor
3176f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl          CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op);
3177f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl          if (Operator->isCopyAssignmentOperator()) {
3178f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl            FoundAssign = true;
3179f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl            const FunctionProtoType *CPT
3180f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl                = Operator->getType()->getAs<FunctionProtoType>();
3181e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith            CPT = Self.ResolveExceptionSpec(KeyLoc, CPT);
3182e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith            if (!CPT)
3183e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith              return false;
31847a614d8380297fcd2bc23986241905d97222948cRichard Smith            if (!CPT->isNothrow(Self.Context))
31857a614d8380297fcd2bc23986241905d97222948cRichard Smith              return false;
31860dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl          }
31870dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl        }
31880dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      }
3189d41679d6881d2b424d8b3600fc774308087735a7Douglas Gregor
31907a614d8380297fcd2bc23986241905d97222948cRichard Smith      return FoundAssign;
31910dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    }
31920dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return false;
31930dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  case UTT_HasNothrowCopy:
31940dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
31950dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   If __has_trivial_copy (type) is true then the trait is true, else
31960dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   if type is a cv class or union type with copy constructors that are
31970dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   known not to throw an exception then the trait is true, else it is
31980dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   false.
3199f85e193739c953358c865005855253af4f68a497John McCall    if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType())
32000dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return true;
32010dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (const RecordType *RT = T->getAs<RecordType>()) {
32020dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
32030dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      if (RD->hasTrivialCopyConstructor())
32040dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl        return true;
32050dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl
32060dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      bool FoundConstructor = false;
32070dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      unsigned FoundTQs;
32080dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      DeclContext::lookup_const_iterator Con, ConEnd;
32095f4e8999aa30e2986e99fbfa4c43e61e99a18687Sebastian Redl      for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD);
32100dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl           Con != ConEnd; ++Con) {
321108295a592d7e54e6bea00daeb2abe7ac79a3aabaSebastian Redl        // A template constructor is never a copy constructor.
321208295a592d7e54e6bea00daeb2abe7ac79a3aabaSebastian Redl        // FIXME: However, it may actually be selected at the actual overload
321308295a592d7e54e6bea00daeb2abe7ac79a3aabaSebastian Redl        // resolution point.
321408295a592d7e54e6bea00daeb2abe7ac79a3aabaSebastian Redl        if (isa<FunctionTemplateDecl>(*Con))
321508295a592d7e54e6bea00daeb2abe7ac79a3aabaSebastian Redl          continue;
32160dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl        CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
32170dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl        if (Constructor->isCopyConstructor(FoundTQs)) {
32180dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl          FoundConstructor = true;
32190dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl          const FunctionProtoType *CPT
32200dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl              = Constructor->getType()->getAs<FunctionProtoType>();
3221e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          CPT = Self.ResolveExceptionSpec(KeyLoc, CPT);
3222e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          if (!CPT)
3223e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith            return false;
322460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl          // FIXME: check whether evaluating default arguments can throw.
3225751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl          // For now, we'll be conservative and assume that they can throw.
32267a614d8380297fcd2bc23986241905d97222948cRichard Smith          if (!CPT->isNothrow(Self.Context) || CPT->getNumArgs() > 1)
32277a614d8380297fcd2bc23986241905d97222948cRichard Smith            return false;
32280dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl        }
32290dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      }
32300dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl
32317a614d8380297fcd2bc23986241905d97222948cRichard Smith      return FoundConstructor;
32320dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    }
32330dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return false;
32340dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  case UTT_HasNothrowConstructor:
32350dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
32360dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   If __has_trivial_constructor (type) is true then the trait is
32370dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   true, else if type is a cv class or union type (or array
32380dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   thereof) with a default constructor that is known not to
32390dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   throw an exception then the trait is true, else it is false.
3240f85e193739c953358c865005855253af4f68a497John McCall    if (T.isPODType(C) || T->isObjCLifetimeType())
32410dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      return true;
32420dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) {
32430dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3244023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt      if (RD->hasTrivialDefaultConstructor())
32450dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl        return true;
32460dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl
3247751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl      DeclContext::lookup_const_iterator Con, ConEnd;
3248751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl      for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD);
3249751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl           Con != ConEnd; ++Con) {
325008295a592d7e54e6bea00daeb2abe7ac79a3aabaSebastian Redl        // FIXME: In C++0x, a constructor template can be a default constructor.
325108295a592d7e54e6bea00daeb2abe7ac79a3aabaSebastian Redl        if (isa<FunctionTemplateDecl>(*Con))
325208295a592d7e54e6bea00daeb2abe7ac79a3aabaSebastian Redl          continue;
3253751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl        CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
3254751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl        if (Constructor->isDefaultConstructor()) {
3255751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl          const FunctionProtoType *CPT
3256751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl              = Constructor->getType()->getAs<FunctionProtoType>();
3257e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          CPT = Self.ResolveExceptionSpec(KeyLoc, CPT);
3258e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          if (!CPT)
3259e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith            return false;
3260751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl          // TODO: check whether evaluating default arguments can throw.
3261751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl          // For now, we'll be conservative and assume that they can throw.
32628026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl          return CPT->isNothrow(Self.Context) && CPT->getNumArgs() == 0;
3263751025d5d174ab75dc3788211581d9fbe6224841Sebastian Redl        }
32640dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      }
32650dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    }
32660dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return false;
32670dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  case UTT_HasVirtualDestructor:
32680dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
32690dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   If type is a class type with a virtual destructor ([class.dtor])
32700dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    //   then the trait is true, else it is false.
32710dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    if (const RecordType *Record = T->getAs<RecordType>()) {
32720dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl      CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
3273f8aca8664512c80a018eb4a4a93c61ad6793abcdSebastian Redl      if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD))
32740dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl        return Destructor->isVirtual();
32750dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    }
32760dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return false;
3277c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth
3278c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // These type trait expressions are modeled on the specifications for the
3279c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // Embarcadero C++0x type trait functions:
3280c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    //   http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index
3281c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth  case UTT_IsCompleteType:
3282c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_):
3283c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    //   Returns True if and only if T is a complete type at the point of the
3284c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    //   function call.
3285c41d6b57f3a42fbe29e33b73e670b398fade4718Chandler Carruth    return !T->isIncompleteType();
32860dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  }
328783f563cc10d6265be395b608323ec52e3370915cChandler Carruth  llvm_unreachable("Type trait not covered by switch");
32880dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl}
32890dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl
32900dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian RedlExprResult Sema::BuildUnaryTypeTrait(UnaryTypeTrait UTT,
32913d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor                                     SourceLocation KWLoc,
32923d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor                                     TypeSourceInfo *TSInfo,
32933d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor                                     SourceLocation RParen) {
32943d37c0ada0e46b87be0a10e8d52d990a97d3907aDouglas Gregor  QualType T = TSInfo->getType();
3295eb65a108bd1eb613ed2a9ebfdbd73a9c872f0309Chandler Carruth  if (!CheckUnaryTypeTraitTypeCompleteness(*this, UTT, KWLoc, T))
3296eb65a108bd1eb613ed2a9ebfdbd73a9c872f0309Chandler Carruth    return ExprError();
329764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
32980dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  bool Value = false;
32990dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  if (!T->isDependentType())
3300ccb4ecf9b11d7b652515780253253db976d0447fChandler Carruth    Value = EvaluateUnaryTypeTrait(*this, UTT, KWLoc, T);
33010dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl
33020dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl  return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, UTT, TSInfo, Value,
33033292d5ce510061ea50152f53b7ab462b622176f4Anders Carlsson                                                RParen, Context.BoolTy));
330464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
33057c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl
33066ad6f2848d7652ab2991286eb48be440d3493b28Francois PichetExprResult Sema::ActOnBinaryTypeTrait(BinaryTypeTrait BTT,
33076ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                      SourceLocation KWLoc,
33086ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                      ParsedType LhsTy,
33096ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                      ParsedType RhsTy,
33106ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                      SourceLocation RParen) {
33116ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeSourceInfo *LhsTSInfo;
33126ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  QualType LhsT = GetTypeFromParser(LhsTy, &LhsTSInfo);
33136ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (!LhsTSInfo)
33146ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    LhsTSInfo = Context.getTrivialTypeSourceInfo(LhsT);
33156ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
33166ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  TypeSourceInfo *RhsTSInfo;
33176ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  QualType RhsT = GetTypeFromParser(RhsTy, &RhsTSInfo);
33186ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (!RhsTSInfo)
33196ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    RhsTSInfo = Context.getTrivialTypeSourceInfo(RhsT);
33206ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
33216ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  return BuildBinaryTypeTrait(BTT, KWLoc, LhsTSInfo, RhsTSInfo, RParen);
33226ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
33236ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
332414b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor/// \brief Determine whether T has a non-trivial Objective-C lifetime in
332514b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor/// ARC mode.
332614b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregorstatic bool hasNontrivialObjCLifetime(QualType T) {
332714b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor  switch (T.getObjCLifetime()) {
332814b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor  case Qualifiers::OCL_ExplicitNone:
332914b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor    return false;
333014b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor
333114b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor  case Qualifiers::OCL_Strong:
333214b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor  case Qualifiers::OCL_Weak:
333314b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor  case Qualifiers::OCL_Autoreleasing:
333414b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor    return true;
333514b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor
333614b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor  case Qualifiers::OCL_None:
333714b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor    return T->isObjCLifetimeType();
333814b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor  }
333914b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor
334014b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor  llvm_unreachable("Unknown ObjC lifetime qualifier");
334114b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor}
334214b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor
33434ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregorstatic bool evaluateTypeTrait(Sema &S, TypeTrait Kind, SourceLocation KWLoc,
33444ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                              ArrayRef<TypeSourceInfo *> Args,
33454ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                              SourceLocation RParenLoc) {
33464ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  switch (Kind) {
33474ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  case clang::TT_IsTriviallyConstructible: {
33484ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // C++11 [meta.unary.prop]:
334962348f041adccdf9376f5c355ed64ecbce0b4f0fDmitri Gribenko    //   is_trivially_constructible is defined as:
33504ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    //
335162348f041adccdf9376f5c355ed64ecbce0b4f0fDmitri Gribenko    //     is_constructible<T, Args...>::value is true and the variable
335262348f041adccdf9376f5c355ed64ecbce0b4f0fDmitri Gribenko    //     definition for is_constructible, as defined below, is known to call no
335362348f041adccdf9376f5c355ed64ecbce0b4f0fDmitri Gribenko    //     operation that is not trivial.
33544ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    //
33554ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    //   The predicate condition for a template specialization
33564ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    //   is_constructible<T, Args...> shall be satisfied if and only if the
33574ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    //   following variable definition would be well-formed for some invented
33584ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    //   variable t:
33594ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    //
33604ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    //     T t(create<Args>()...);
33614ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Args.empty()) {
33624ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      S.Diag(KWLoc, diag::err_type_trait_arity)
33634ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        << 1 << 1 << 1 << (int)Args.size();
33644ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      return false;
33654ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
33664ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
33674ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    bool SawVoid = false;
33684ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    for (unsigned I = 0, N = Args.size(); I != N; ++I) {
33694ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      if (Args[I]->getType()->isVoidType()) {
33704ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        SawVoid = true;
33714ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        continue;
33724ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      }
33734ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
33744ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      if (!Args[I]->getType()->isIncompleteType() &&
33754ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        S.RequireCompleteType(KWLoc, Args[I]->getType(),
33764ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor          diag::err_incomplete_type_used_in_type_trait_expr))
33774ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        return false;
33784ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
33794ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
33804ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // If any argument was 'void', of course it won't type-check.
33814ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (SawVoid)
33824ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      return false;
33834ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
33844ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    llvm::SmallVector<OpaqueValueExpr, 2> OpaqueArgExprs;
33854ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    llvm::SmallVector<Expr *, 2> ArgExprs;
33864ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    ArgExprs.reserve(Args.size() - 1);
33874ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    for (unsigned I = 1, N = Args.size(); I != N; ++I) {
33884ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      QualType T = Args[I]->getType();
33894ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      if (T->isObjectType() || T->isFunctionType())
33904ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor        T = S.Context.getRValueReferenceType(T);
33914ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      OpaqueArgExprs.push_back(
339296a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        OpaqueValueExpr(Args[I]->getTypeLoc().getLocStart(),
33934ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                        T.getNonLValueExprType(S.Context),
33944ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                        Expr::getValueKindForType(T)));
33954ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      ArgExprs.push_back(&OpaqueArgExprs.back());
33964ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
33974ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
33984ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // Perform the initialization in an unevaluated context within a SFINAE
33994ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // trap at translation unit scope.
34004ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated);
34014ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/true);
34024ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl());
34034ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    InitializedEntity To(InitializedEntity::InitializeTemporary(Args[0]));
34044ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc,
34054ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                                                                 RParenLoc));
34064ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    InitializationSequence Init(S, To, InitKind,
34074ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                                ArgExprs.begin(), ArgExprs.size());
34084ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Init.Failed())
34094ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      return false;
34104ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
34115354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    ExprResult Result = Init.Perform(S, To, InitKind, ArgExprs);
34124ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Result.isInvalid() || SFINAE.hasErrorOccurred())
34134ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      return false;
341414b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor
341514b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor    // Under Objective-C ARC, if the destination has non-trivial Objective-C
341614b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor    // lifetime, this is a non-trivial construction.
341714b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor    if (S.getLangOpts().ObjCAutoRefCount &&
341814b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor        hasNontrivialObjCLifetime(Args[0]->getType().getNonReferenceType()))
341914b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor      return false;
342014b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor
342114b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor    // The initialization succeeded; now make sure there are no non-trivial
34224ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    // calls.
34234ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return !Result.get()->hasNonTrivialCall(S.Context);
34244ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  }
34254ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  }
34264ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
34274ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  return false;
34284ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor}
34294ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
34304ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas GregorExprResult Sema::BuildTypeTrait(TypeTrait Kind, SourceLocation KWLoc,
34314ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                                ArrayRef<TypeSourceInfo *> Args,
34324ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                                SourceLocation RParenLoc) {
34334ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  bool Dependent = false;
34344ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  for (unsigned I = 0, N = Args.size(); I != N; ++I) {
34354ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (Args[I]->getType()->isDependentType()) {
34364ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      Dependent = true;
34374ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      break;
34384ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    }
34394ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  }
34404ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
34414ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  bool Value = false;
34424ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  if (!Dependent)
34434ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    Value = evaluateTypeTrait(*this, Kind, KWLoc, Args, RParenLoc);
34444ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
34454ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  return TypeTraitExpr::Create(Context, Context.BoolTy, KWLoc, Kind,
34464ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                               Args, RParenLoc, Value);
34474ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor}
34484ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
34494ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas GregorExprResult Sema::ActOnTypeTrait(TypeTrait Kind, SourceLocation KWLoc,
34504ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                                ArrayRef<ParsedType> Args,
34514ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor                                SourceLocation RParenLoc) {
34524ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  llvm::SmallVector<TypeSourceInfo *, 4> ConvertedArgs;
34534ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  ConvertedArgs.reserve(Args.size());
34544ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
34554ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  for (unsigned I = 0, N = Args.size(); I != N; ++I) {
34564ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    TypeSourceInfo *TInfo;
34574ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    QualType T = GetTypeFromParser(Args[I], &TInfo);
34584ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    if (!TInfo)
34594ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor      TInfo = Context.getTrivialTypeSourceInfo(T, KWLoc);
34604ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
34614ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    ConvertedArgs.push_back(TInfo);
34624ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  }
34634ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
34644ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  return BuildTypeTrait(Kind, KWLoc, ConvertedArgs, RParenLoc);
34654ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor}
34664ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
34676ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichetstatic bool EvaluateBinaryTypeTrait(Sema &Self, BinaryTypeTrait BTT,
34686ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                    QualType LhsT, QualType RhsT,
34696ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                    SourceLocation KeyLoc) {
3470d064c70bc63ad742d11625e485c1dc1631f6e303Chandler Carruth  assert(!LhsT->isDependentType() && !RhsT->isDependentType() &&
3471d064c70bc63ad742d11625e485c1dc1631f6e303Chandler Carruth         "Cannot evaluate traits of dependent types");
34726ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
34736ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  switch(BTT) {
3474d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall  case BTT_IsBaseOf: {
34756ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    // C++0x [meta.rel]p2
3476d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    // Base is a base class of Derived without regard to cv-qualifiers or
34776ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    // Base and Derived are not unions and name the same class type without
34786ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    // regard to cv-qualifiers.
34796ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
3480d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    const RecordType *lhsRecord = LhsT->getAs<RecordType>();
3481d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    if (!lhsRecord) return false;
3482d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall
3483d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    const RecordType *rhsRecord = RhsT->getAs<RecordType>();
3484d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    if (!rhsRecord) return false;
3485d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall
3486d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT)
3487d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall             == (lhsRecord == rhsRecord));
3488d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall
3489d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    if (lhsRecord == rhsRecord)
3490d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall      return !lhsRecord->getDecl()->isUnion();
3491d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall
3492d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    // C++0x [meta.rel]p2:
3493d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    //   If Base and Derived are class types and are different types
3494d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    //   (ignoring possible cv-qualifiers) then Derived shall be a
3495d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    //   complete type.
3496d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    if (Self.RequireCompleteType(KeyLoc, RhsT,
3497d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall                          diag::err_incomplete_type_used_in_type_trait_expr))
3498d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall      return false;
3499d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall
3500d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall    return cast<CXXRecordDecl>(rhsRecord->getDecl())
3501d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall      ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl()));
3502d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall  }
350320c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case BTT_IsSame:
350420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley    return Self.Context.hasSameType(LhsT, RhsT);
3505f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case BTT_TypeCompatible:
3506f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet    return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(),
3507f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet                                           RhsT.getUnqualifiedType());
350820c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case BTT_IsConvertible:
35099f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor  case BTT_IsConvertibleTo: {
35109f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    // C++0x [meta.rel]p4:
35119f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //   Given the following function prototype:
35129f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //
35139f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //     template <class T>
35149f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //       typename add_rvalue_reference<T>::type create();
35159f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //
35169f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //   the predicate condition for a template specialization
35179f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //   is_convertible<From, To> shall be satisfied if and only if
35189f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //   the return expression in the following code would be
35199f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //   well-formed, including any implicit conversions to the return
35209f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //   type of the function:
35219f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //
35229f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //     To test() {
35239f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //       return create<From>();
35249f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //     }
35259f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //
35269f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //   Access checking is performed as if in a context unrelated to To and
35279f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //   From. Only the validity of the immediate context of the expression
35289f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //   of the return-statement (including conversions to the return type)
35299f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //   is considered.
35309f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    //
35319f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    // We model the initialization as a copy-initialization of a temporary
35329f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    // of the appropriate type, which for this expression is identical to the
35339f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    // return statement (since NRVO doesn't apply).
35342217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman
35352217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman    // Functions aren't allowed to return function or array types.
35362217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman    if (RhsT->isFunctionType() || RhsT->isArrayType())
35372217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman      return false;
35382217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman
35392217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman    // A return statement in a void function must have void type.
35402217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman    if (RhsT->isVoidType())
35412217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman      return LhsT->isVoidType();
35422217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman
35432217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman    // A function definition requires a complete, non-abstract return type.
35442217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman    if (Self.RequireCompleteType(KeyLoc, RhsT, 0) ||
35452217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman        Self.RequireNonAbstractType(KeyLoc, RhsT, 0))
35462217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman      return false;
35472217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman
35482217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman    // Compute the result of add_rvalue_reference.
35499f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    if (LhsT->isObjectType() || LhsT->isFunctionType())
35509f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor      LhsT = Self.Context.getRValueReferenceType(LhsT);
35512217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman
35522217f853e1909b80f87ce0dcec5543e894d11bc9Eli Friedman    // Build a fake source and destination for initialization.
35539f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT));
3554b608b987718c6d841115464f79ab2d1820a63e17Douglas Gregor    OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context),
35559f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor                         Expr::getValueKindForType(LhsT));
35569f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    Expr *FromPtr = &From;
35579f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc,
35589f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor                                                           SourceLocation()));
35599f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor
35603add9f0ac80b0a4d78611dcdb4fd89c37f1c13d8Eli Friedman    // Perform the initialization in an unevaluated context within a SFINAE
35613add9f0ac80b0a4d78611dcdb4fd89c37f1c13d8Eli Friedman    // trap at translation unit scope.
35623add9f0ac80b0a4d78611dcdb4fd89c37f1c13d8Eli Friedman    EnterExpressionEvaluationContext Unevaluated(Self, Sema::Unevaluated);
35631eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor    Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true);
35641eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor    Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl());
35659f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    InitializationSequence Init(Self, To, Kind, &FromPtr, 1);
3566383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl    if (Init.Failed())
35679f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor      return false;
35681eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor
35695354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    ExprResult Result = Init.Perform(Self, To, Kind, FromPtr);
35709f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor    return !Result.isInvalid() && !SFINAE.hasErrorOccurred();
35719f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor  }
357225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
357325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  case BTT_IsTriviallyAssignable: {
357425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    // C++11 [meta.unary.prop]p3:
357525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    //   is_trivially_assignable is defined as:
357625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    //     is_assignable<T, U>::value is true and the assignment, as defined by
357725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    //     is_assignable, is known to call no operation that is not trivial
357825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    //
357925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    //   is_assignable is defined as:
358025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    //     The expression declval<T>() = declval<U>() is well-formed when
358125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    //     treated as an unevaluated operand (Clause 5).
358225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    //
358325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    //   For both, T and U shall be complete types, (possibly cv-qualified)
358425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    //   void, or arrays of unknown bound.
358525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType() &&
358625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        Self.RequireCompleteType(KeyLoc, LhsT,
358725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor          diag::err_incomplete_type_used_in_type_trait_expr))
358825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      return false;
358925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType() &&
359025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor        Self.RequireCompleteType(KeyLoc, RhsT,
359125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor          diag::err_incomplete_type_used_in_type_trait_expr))
359225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      return false;
359325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
359425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    // cv void is never assignable.
359525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    if (LhsT->isVoidType() || RhsT->isVoidType())
359625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      return false;
359725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
359825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    // Build expressions that emulate the effect of declval<T>() and
359925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    // declval<U>().
360025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    if (LhsT->isObjectType() || LhsT->isFunctionType())
360125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      LhsT = Self.Context.getRValueReferenceType(LhsT);
360225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    if (RhsT->isObjectType() || RhsT->isFunctionType())
360325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      RhsT = Self.Context.getRValueReferenceType(RhsT);
360425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    OpaqueValueExpr Lhs(KeyLoc, LhsT.getNonLValueExprType(Self.Context),
360525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor                        Expr::getValueKindForType(LhsT));
360625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    OpaqueValueExpr Rhs(KeyLoc, RhsT.getNonLValueExprType(Self.Context),
360725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor                        Expr::getValueKindForType(RhsT));
360825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
360925d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    // Attempt the assignment in an unevaluated context within a SFINAE
361025d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    // trap at translation unit scope.
361125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(Self, Sema::Unevaluated);
361225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true);
361325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl());
361425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    ExprResult Result = Self.BuildBinOp(/*S=*/0, KeyLoc, BO_Assign, &Lhs, &Rhs);
361525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    if (Result.isInvalid() || SFINAE.hasErrorOccurred())
361625d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor      return false;
361725d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor
361814b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor    // Under Objective-C ARC, if the destination has non-trivial Objective-C
361914b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor    // lifetime, this is a non-trivial assignment.
362014b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor    if (Self.getLangOpts().ObjCAutoRefCount &&
362114b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor        hasNontrivialObjCLifetime(LhsT.getNonReferenceType()))
362214b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor      return false;
362314b23277db34fd33292dd9ec23abb13e7cb72613Douglas Gregor
362425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor    return !Result.get()->hasNonTrivialCall(Self.Context);
362525d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  }
36266ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
36276ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  llvm_unreachable("Unknown type trait or not implemented");
36286ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
36296ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
36306ad6f2848d7652ab2991286eb48be440d3493b28Francois PichetExprResult Sema::BuildBinaryTypeTrait(BinaryTypeTrait BTT,
36316ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                      SourceLocation KWLoc,
36326ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                      TypeSourceInfo *LhsTSInfo,
36336ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                      TypeSourceInfo *RhsTSInfo,
36346ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                      SourceLocation RParen) {
36356ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  QualType LhsT = LhsTSInfo->getType();
36366ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  QualType RhsT = RhsTSInfo->getType();
3637dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3638d89d30fdd9e3061fb100fb8f976aab5c6cf2c901John McCall  if (BTT == BTT_TypeCompatible) {
36394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus) {
3640f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet      Diag(KWLoc, diag::err_types_compatible_p_in_cplusplus)
3641f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet        << SourceRange(KWLoc, RParen);
3642f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet      return ExprError();
3643f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet    }
36446ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
36456ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
36466ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  bool Value = false;
36476ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  if (!LhsT->isDependentType() && !RhsT->isDependentType())
36486ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    Value = EvaluateBinaryTypeTrait(*this, BTT, LhsT, RhsT, KWLoc);
36496ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
3650f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  // Select trait result type.
3651f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  QualType ResultType;
3652f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  switch (BTT) {
3653f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case BTT_IsBaseOf:       ResultType = Context.BoolTy; break;
365420c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case BTT_IsConvertible:  ResultType = Context.BoolTy; break;
365520c0da7787c9a7d2529e42a4a91d777778595d74John Wiegley  case BTT_IsSame:         ResultType = Context.BoolTy; break;
3656f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  case BTT_TypeCompatible: ResultType = Context.IntTy; break;
36579f3611365d0f2297a910cf246e056708726ed10aDouglas Gregor  case BTT_IsConvertibleTo: ResultType = Context.BoolTy; break;
365825d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  case BTT_IsTriviallyAssignable: ResultType = Context.BoolTy;
3659f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet  }
3660f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet
36616ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  return Owned(new (Context) BinaryTypeTraitExpr(KWLoc, BTT, LhsTSInfo,
36626ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet                                                 RhsTSInfo, Value, RParen,
3663f187237d916afa97c491ac32fe98be7d335c5b63Francois Pichet                                                 ResultType));
36646ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet}
36656ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
366621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John WiegleyExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT,
366721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley                                     SourceLocation KWLoc,
366821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley                                     ParsedType Ty,
366921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley                                     Expr* DimExpr,
367021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley                                     SourceLocation RParen) {
367121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  TypeSourceInfo *TSInfo;
367221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  QualType T = GetTypeFromParser(Ty, &TSInfo);
367321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  if (!TSInfo)
367421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    TSInfo = Context.getTrivialTypeSourceInfo(T);
367521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
367621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen);
367721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
367821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
367921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegleystatic uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT,
368021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley                                           QualType T, Expr *DimExpr,
368121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley                                           SourceLocation KeyLoc) {
3682d064c70bc63ad742d11625e485c1dc1631f6e303Chandler Carruth  assert(!T->isDependentType() && "Cannot evaluate traits of dependent type");
368321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
368421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  switch(ATT) {
368521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayRank:
368621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    if (T->isArrayType()) {
368721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      unsigned Dim = 0;
368821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      while (const ArrayType *AT = Self.Context.getAsArrayType(T)) {
368921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley        ++Dim;
369021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley        T = AT->getElementType();
369121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      }
369221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      return Dim;
369321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    }
3694cf5664114eb75c6a5fef2bed1c0f0d0fb19debc9John Wiegley    return 0;
3695cf5664114eb75c6a5fef2bed1c0f0d0fb19debc9John Wiegley
369621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case ATT_ArrayExtent: {
369721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    llvm::APSInt Value;
369821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    uint64_t Dim;
3699282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    if (Self.VerifyIntegerConstantExpression(DimExpr, &Value,
3700ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor          diag::err_dimension_expr_not_constant_integer,
3701282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith          false).isInvalid())
3702282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return 0;
3703282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    if (Value.isSigned() && Value.isNegative()) {
3704e7d6ca079a68ed9ea7fa6e5d6bfc9f625a37df76Daniel Dunbar      Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer)
3705e7d6ca079a68ed9ea7fa6e5d6bfc9f625a37df76Daniel Dunbar        << DimExpr->getSourceRange();
3706282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return 0;
3707cf5664114eb75c6a5fef2bed1c0f0d0fb19debc9John Wiegley    }
3708282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    Dim = Value.getLimitedValue();
370921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
371021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    if (T->isArrayType()) {
371121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      unsigned D = 0;
371221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      bool Matched = false;
371321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      while (const ArrayType *AT = Self.Context.getAsArrayType(T)) {
371421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley        if (Dim == D) {
371521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley          Matched = true;
371621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley          break;
371721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley        }
371821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley        ++D;
371921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley        T = AT->getElementType();
372021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley      }
372121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
3722cf5664114eb75c6a5fef2bed1c0f0d0fb19debc9John Wiegley      if (Matched && T->isArrayType()) {
3723cf5664114eb75c6a5fef2bed1c0f0d0fb19debc9John Wiegley        if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T))
3724cf5664114eb75c6a5fef2bed1c0f0d0fb19debc9John Wiegley          return CAT->getSize().getLimitedValue();
3725cf5664114eb75c6a5fef2bed1c0f0d0fb19debc9John Wiegley      }
372621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    }
3727cf5664114eb75c6a5fef2bed1c0f0d0fb19debc9John Wiegley    return 0;
372821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
372921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
373021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  llvm_unreachable("Unknown type trait or not implemented");
373121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
373221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
373321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John WiegleyExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT,
373421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley                                     SourceLocation KWLoc,
373521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley                                     TypeSourceInfo *TSInfo,
373621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley                                     Expr* DimExpr,
373721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley                                     SourceLocation RParen) {
373821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  QualType T = TSInfo->getType();
373921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
3740af5a3c6af37a16b0ca686e6af994010b0b3785ddChandler Carruth  // FIXME: This should likely be tracked as an APInt to remove any host
3741af5a3c6af37a16b0ca686e6af994010b0b3785ddChandler Carruth  // assumptions about the width of size_t on the target.
3742d064c70bc63ad742d11625e485c1dc1631f6e303Chandler Carruth  uint64_t Value = 0;
3743d064c70bc63ad742d11625e485c1dc1631f6e303Chandler Carruth  if (!T->isDependentType())
3744d064c70bc63ad742d11625e485c1dc1631f6e303Chandler Carruth    Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc);
3745d064c70bc63ad742d11625e485c1dc1631f6e303Chandler Carruth
3746af5a3c6af37a16b0ca686e6af994010b0b3785ddChandler Carruth  // While the specification for these traits from the Embarcadero C++
3747af5a3c6af37a16b0ca686e6af994010b0b3785ddChandler Carruth  // compiler's documentation says the return type is 'unsigned int', Clang
3748af5a3c6af37a16b0ca686e6af994010b0b3785ddChandler Carruth  // returns 'size_t'. On Windows, the primary platform for the Embarcadero
3749af5a3c6af37a16b0ca686e6af994010b0b3785ddChandler Carruth  // compiler, there is no difference. On several other platforms this is an
3750af5a3c6af37a16b0ca686e6af994010b0b3785ddChandler Carruth  // important distinction.
375121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  return Owned(new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value,
375206207f6f5f248f678498d61d6dbb36774b7fe5aeChandler Carruth                                                DimExpr, RParen,
3753af5a3c6af37a16b0ca686e6af994010b0b3785ddChandler Carruth                                                Context.getSizeType()));
375421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley}
375521ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
3756552622067dc45013d240f73952fece703f5e63bdJohn WiegleyExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET,
37574aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth                                      SourceLocation KWLoc,
37584aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth                                      Expr *Queried,
37594aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth                                      SourceLocation RParen) {
3760552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  // If error parsing the expression, ignore.
3761552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  if (!Queried)
37624aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth    return ExprError();
3763552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
37644aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth  ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen);
3765552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
37663fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
3767552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
3768552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
37694aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruthstatic bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) {
37704aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth  switch (ET) {
37714aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth  case ET_IsLValueExpr: return E->isLValue();
37724aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth  case ET_IsRValueExpr: return E->isRValue();
37734aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth  }
37744aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth  llvm_unreachable("Expression trait not covered by switch");
37754aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth}
37764aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth
3777552622067dc45013d240f73952fece703f5e63bdJohn WiegleyExprResult Sema::BuildExpressionTrait(ExpressionTrait ET,
37784aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth                                      SourceLocation KWLoc,
37794aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth                                      Expr *Queried,
37804aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth                                      SourceLocation RParen) {
3781552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  if (Queried->isTypeDependent()) {
3782552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    // Delay type-checking for type-dependent expressions.
3783552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  } else if (Queried->getType()->isPlaceholderType()) {
3784552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    ExprResult PE = CheckPlaceholderExpr(Queried);
3785552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    if (PE.isInvalid()) return ExprError();
3786552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    return BuildExpressionTrait(ET, KWLoc, PE.take(), RParen);
3787552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  }
3788552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
37894aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth  bool Value = EvaluateExpressionTrait(ET, Queried);
3790f7ef00048d0a3f6079d82e00a9a8dba92179b9a8Chandler Carruth
37914aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth  return Owned(new (Context) ExpressionTraitExpr(KWLoc, ET, Queried, Value,
37924aa0af346b7bb37548078dfab728d28d7776aff3Chandler Carruth                                                 RParen, Context.BoolTy));
3793552622067dc45013d240f73952fece703f5e63bdJohn Wiegley}
3794552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
3795dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard TrieuQualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS,
3796f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                            ExprValueKind &VK,
3797f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                            SourceLocation Loc,
3798f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                            bool isIndirect) {
3799dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu  assert(!LHS.get()->getType()->isPlaceholderType() &&
3800dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu         !RHS.get()->getType()->isPlaceholderType() &&
3801ea4aba0e97ca71369a0f6e287443b670cf5c353fJohn McCall         "placeholders should have been weeded out by now");
3802ea4aba0e97ca71369a0f6e287443b670cf5c353fJohn McCall
3803ea4aba0e97ca71369a0f6e287443b670cf5c353fJohn McCall  // The LHS undergoes lvalue conversions if this is ->*.
3804ea4aba0e97ca71369a0f6e287443b670cf5c353fJohn McCall  if (isIndirect) {
3805dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu    LHS = DefaultLvalueConversion(LHS.take());
3806dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu    if (LHS.isInvalid()) return QualType();
3807ea4aba0e97ca71369a0f6e287443b670cf5c353fJohn McCall  }
3808ea4aba0e97ca71369a0f6e287443b670cf5c353fJohn McCall
3809ea4aba0e97ca71369a0f6e287443b670cf5c353fJohn McCall  // The RHS always undergoes lvalue conversions.
3810dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu  RHS = DefaultLvalueConversion(RHS.take());
3811dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu  if (RHS.isInvalid()) return QualType();
3812ea4aba0e97ca71369a0f6e287443b670cf5c353fJohn McCall
38137c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  const char *OpSpelling = isIndirect ? "->*" : ".*";
38147c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  // C++ 5.5p2
38157c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  //   The binary operator .* [p3: ->*] binds its second operand, which shall
38167c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  //   be of type "pointer to member of T" (where T is a completely-defined
38177c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  //   class type) [...]
3818dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu  QualType RHSType = RHS.get()->getType();
3819dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu  const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>();
3820e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor  if (!MemPtr) {
38217c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl    Diag(Loc, diag::err_bad_memptr_rhs)
3822dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu      << OpSpelling << RHSType << RHS.get()->getSourceRange();
38237c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl    return QualType();
38241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
3825e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor
38267c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  QualType Class(MemPtr->getClass(), 0);
38277c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl
38287d520baec9b762633a09d31ee0db12e41fe2758aDouglas Gregor  // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the
38297d520baec9b762633a09d31ee0db12e41fe2758aDouglas Gregor  // member pointer points must be completely-defined. However, there is no
38307d520baec9b762633a09d31ee0db12e41fe2758aDouglas Gregor  // reason for this semantic distinction, and the rule is not enforced by
38317d520baec9b762633a09d31ee0db12e41fe2758aDouglas Gregor  // other compilers. Therefore, we do not check this property, as it is
38327d520baec9b762633a09d31ee0db12e41fe2758aDouglas Gregor  // likely to be considered a defect.
383359fc2690e622e9db7f7a8f5036562b19d3bfb2b5Sebastian Redl
38347c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  // C++ 5.5p2
38357c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  //   [...] to its first operand, which shall be of class T or of a class of
38367c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  //   which T is an unambiguous and accessible base class. [p3: a pointer to
38377c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  //   such a class]
3838dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu  QualType LHSType = LHS.get()->getType();
38397c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  if (isIndirect) {
3840dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu    if (const PointerType *Ptr = LHSType->getAs<PointerType>())
3841dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu      LHSType = Ptr->getPointeeType();
38427c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl    else {
38437c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl      Diag(Loc, diag::err_bad_memptr_lhs)
3844dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu        << OpSpelling << 1 << LHSType
3845849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateReplacement(SourceRange(Loc), ".*");
38467c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl      return QualType();
38477c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl    }
38487c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  }
38497c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl
3850dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu  if (!Context.hasSameUnqualifiedType(Class, LHSType)) {
385117e1d35848247cbc5ff31df58f0b67e85b64837cSebastian Redl    // If we want to check the hierarchy, we need a complete type.
3852d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(Loc, LHSType, diag::err_bad_memptr_lhs,
3853d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                            OpSpelling, (int)isIndirect)) {
385417e1d35848247cbc5ff31df58f0b67e85b64837cSebastian Redl      return QualType();
385517e1d35848247cbc5ff31df58f0b67e85b64837cSebastian Redl    }
38565cf86ba6b5a724bf91cb52feade1158f1fbeb605Anders Carlsson    CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
3857a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor                       /*DetectVirtual=*/false);
3858390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Would it be useful to print full ambiguity paths, or is that
3859390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // overkill?
3860dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu    if (!IsDerivedFrom(LHSType, Class, Paths) ||
38617c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl        Paths.isAmbiguous(Context.getCanonicalType(Class))) {
38627c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl      Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling
3863dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu        << (int)isIndirect << LHS.get()->getType();
38647c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl      return QualType();
38657c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl    }
38663005efeb131ccddcda9c332c83e49c32349e2aedEli Friedman    // Cast LHS to type of use.
38673005efeb131ccddcda9c332c83e49c32349e2aedEli Friedman    QualType UseType = isIndirect ? Context.getPointerType(Class) : Class;
3868c1c0dfb376b829b94d4c61e9f358ce23e6aa3169Eli Friedman    ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind();
3869906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
3870f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    CXXCastPath BasePath;
38715cf86ba6b5a724bf91cb52feade1158f1fbeb605Anders Carlsson    BuildBasePathArray(Paths, BasePath);
3872dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu    LHS = ImpCastExprToType(LHS.take(), UseType, CK_DerivedToBase, VK,
3873dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu                            &BasePath);
38747c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  }
38757c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl
3876dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu  if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) {
387705ebda9490e15f900edb2e5c690e067abcb37b2eFariborz Jahanian    // Diagnose use of pointer-to-member type which when used as
387805ebda9490e15f900edb2e5c690e067abcb37b2eFariborz Jahanian    // the functional cast in a pointer-to-member expression.
387905ebda9490e15f900edb2e5c690e067abcb37b2eFariborz Jahanian    Diag(Loc, diag::err_pointer_to_member_type) << isIndirect;
388005ebda9490e15f900edb2e5c690e067abcb37b2eFariborz Jahanian     return QualType();
388105ebda9490e15f900edb2e5c690e067abcb37b2eFariborz Jahanian  }
3882f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
38837c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  // C++ 5.5p2
38847c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  //   The result is an object or a function of the type specified by the
38857c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  //   second operand.
38867c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  // The cv qualifiers are the union of those in the pointer and the left side,
38877c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  // in accordance with 5.5p5 and 5.2.5.
38887c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  QualType Result = MemPtr->getPointeeType();
3889dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu  Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers());
3890f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
38916b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor  // C++0x [expr.mptr.oper]p6:
38926b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor  //   In a .* expression whose object expression is an rvalue, the program is
3893dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   ill-formed if the second operand is a pointer to member function with
3894dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   ref-qualifier &. In a ->* expression or in a .* expression whose object
3895dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   expression is an lvalue, the program is ill-formed if the second operand
38966b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor  //   is a pointer to member function with ref-qualifier &&.
38976b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor  if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) {
38986b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor    switch (Proto->getRefQualifier()) {
38996b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor    case RQ_None:
39006b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor      // Do nothing
39016b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor      break;
39026b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor
39036b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor    case RQ_LValue:
3904dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu      if (!isIndirect && !LHS.get()->Classify(Context).isLValue())
39056b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor        Diag(Loc, diag::err_pointer_to_member_oper_value_classify)
3906dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu          << RHSType << 1 << LHS.get()->getSourceRange();
39076b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor      break;
3908dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
39096b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor    case RQ_RValue:
3910dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu      if (isIndirect || !LHS.get()->Classify(Context).isRValue())
39116b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor        Diag(Loc, diag::err_pointer_to_member_oper_value_classify)
3912dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu          << RHSType << 0 << LHS.get()->getSourceRange();
39136b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor      break;
39146b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor    }
39156b4df91dc4ed4a3fc78587c49a3ed3df96b65d9cDouglas Gregor  }
3916dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3917f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  // C++ [expr.mptr.oper]p6:
3918f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  //   The result of a .* expression whose second operand is a pointer
3919f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  //   to a data member is of the same value category as its
3920f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  //   first operand. The result of a .* expression whose second
3921f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  //   operand is a pointer to a member function is a prvalue. The
3922f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  //   result of an ->* expression is an lvalue if its second operand
3923f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  //   is a pointer to data member and a prvalue otherwise.
3924864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  if (Result->isFunctionType()) {
3925f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    VK = VK_RValue;
3926864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    return Context.BoundMemberTy;
3927864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  } else if (isIndirect) {
3928f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    VK = VK_LValue;
3929864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  } else {
3930dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu    VK = LHS.get()->getValueKind();
3931864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  }
3932f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
39337c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl  return Result;
39347c8bd60da52c3280f2fac27977efd74cd290b131Sebastian Redl}
39353201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
39363201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// \brief Try to convert a type to another according to C++0x 5.16p3.
39373201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl///
39383201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// This is part of the parameter validation for the ? operator. If either
39393201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// value operand is a class type, the two operands are attempted to be
39403201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// converted to each other. This function does the conversion in one direction.
3941b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor/// It returns true if the program is ill-formed and has already been diagnosed
3942b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor/// as such.
39433201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redlstatic bool TryClassUnification(Sema &Self, Expr *From, Expr *To,
39443201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl                                SourceLocation QuestionLoc,
3945b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor                                bool &HaveConversion,
3946b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor                                QualType &ToType) {
3947b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  HaveConversion = false;
3948b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  ToType = To->getType();
3949dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3950dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(),
3951b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor                                                           SourceLocation());
39523201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  // C++0x 5.16p3
39533201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   The process for determining whether an operand expression E1 of type T1
39543201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   can be converted to match an operand expression E2 of type T2 is defined
39553201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   as follows:
39563201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   -- If E2 is an lvalue:
39577eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  bool ToIsLvalue = To->isLValue();
39580fd8ff73630adab9a33123f23ef62fcf5c3cf326Douglas Gregor  if (ToIsLvalue) {
39593201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   E1 can be converted to match E2 if E1 can be implicitly converted to
39603201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   type "lvalue reference to T2", subject to the constraint that in the
39613201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   conversion the reference must bind directly to E1.
3962b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    QualType T = Self.Context.getLValueReferenceType(ToType);
3963b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    InitializedEntity Entity = InitializedEntity::InitializeTemporary(T);
3964dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3965b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    InitializationSequence InitSeq(Self, Entity, Kind, &From, 1);
3966b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    if (InitSeq.isDirectReferenceBinding()) {
3967b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor      ToType = T;
3968b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor      HaveConversion = true;
3969b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor      return false;
39703201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    }
3971dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3972b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    if (InitSeq.isAmbiguous())
3973b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor      return InitSeq.Diagnose(Self, Entity, Kind, &From, 1);
39743201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  }
3975b1bdc6232d7b4d8574b5436982435ad57f429a91John McCall
39763201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   -- If E2 is an rvalue, or if the conversion above cannot be done:
39773201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //      -- if E1 and E2 have class type, and the underlying class types are
39783201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //         the same or one is a base class of the other:
39793201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  QualType FTy = From->getType();
39803201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  QualType TTy = To->getType();
39816217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *FRec = FTy->getAs<RecordType>();
39826217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *TRec = TTy->getAs<RecordType>();
3983dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  bool FDerivedFromT = FRec && TRec && FRec != TRec &&
3984b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor                       Self.IsDerivedFrom(FTy, TTy);
3985dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (FRec && TRec &&
3986b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor      (FRec == TRec || FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) {
39873201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //         E1 can be converted to match E2 if the class of T2 is the
39883201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //         same type as, or a base class of, the class of T1, and
39893201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //         [cv2 > cv1].
3990b1bdc6232d7b4d8574b5436982435ad57f429a91John McCall    if (FRec == TRec || FDerivedFromT) {
3991b1bdc6232d7b4d8574b5436982435ad57f429a91John McCall      if (TTy.isAtLeastAsQualifiedAs(FTy)) {
3992b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor        InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy);
3993b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor        InitializationSequence InitSeq(Self, Entity, Kind, &From, 1);
3994383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl        if (InitSeq) {
3995b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor          HaveConversion = true;
3996b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor          return false;
3997b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor        }
3998dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3999b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor        if (InitSeq.isAmbiguous())
4000b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor          return InitSeq.Diagnose(Self, Entity, Kind, &From, 1);
4001dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      }
40023201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    }
4003dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4004b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    return false;
40053201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  }
4006dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4007b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  //     -- Otherwise: E1 can be converted to match E2 if E1 can be
4008b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  //        implicitly converted to the type that expression E2 would have
4009dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //        if E2 were converted to an rvalue (or the type it has, if E2 is
40100fd8ff73630adab9a33123f23ef62fcf5c3cf326Douglas Gregor  //        an rvalue).
40110fd8ff73630adab9a33123f23ef62fcf5c3cf326Douglas Gregor  //
40120fd8ff73630adab9a33123f23ef62fcf5c3cf326Douglas Gregor  // This actually refers very narrowly to the lvalue-to-rvalue conversion, not
40130fd8ff73630adab9a33123f23ef62fcf5c3cf326Douglas Gregor  // to the array-to-pointer or function-to-pointer conversions.
40140fd8ff73630adab9a33123f23ef62fcf5c3cf326Douglas Gregor  if (!TTy->getAs<TagType>())
40150fd8ff73630adab9a33123f23ef62fcf5c3cf326Douglas Gregor    TTy = TTy.getUnqualifiedType();
4016dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4017b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy);
4018b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  InitializationSequence InitSeq(Self, Entity, Kind, &From, 1);
4019383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl  HaveConversion = !InitSeq.Failed();
4020b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  ToType = TTy;
4021b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  if (InitSeq.isAmbiguous())
4022b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    return InitSeq.Diagnose(Self, Entity, Kind, &From, 1);
4023b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor
40243201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  return false;
40253201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl}
40263201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
40273201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// \brief Try to find a common type for two according to C++0x 5.16p5.
40283201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl///
40293201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// This is part of the parameter validation for the ? operator. If either
40303201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// value operand is a class type, overload resolution is used to find a
40313201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// conversion to a common type.
4032429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegleystatic bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS,
403382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth                                    SourceLocation QuestionLoc) {
4034429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  Expr *Args[2] = { LHS.get(), RHS.get() };
403582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  OverloadCandidateSet CandidateSet(QuestionLoc);
403682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, 2,
403782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth                                    CandidateSet);
40383201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
40393201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  OverloadCandidateSet::iterator Best;
404082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) {
4041429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    case OR_Success: {
40423201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      // We found a match. Perform the conversions on the arguments and move on.
4043429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      ExprResult LHSRes =
4044429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        Self.PerformImplicitConversion(LHS.get(), Best->BuiltinTypes.ParamTypes[0],
4045429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                       Best->Conversions[0], Sema::AA_Converting);
4046429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (LHSRes.isInvalid())
40473201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl        break;
40483fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer      LHS = LHSRes;
4049429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
4050429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      ExprResult RHSRes =
4051429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        Self.PerformImplicitConversion(RHS.get(), Best->BuiltinTypes.ParamTypes[1],
4052429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                       Best->Conversions[1], Sema::AA_Converting);
4053429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (RHSRes.isInvalid())
4054429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        break;
40553fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer      RHS = RHSRes;
405625ca421a6049350a2748c8fd0c19a052eba6ae99Chandler Carruth      if (Best->Function)
40575f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman        Self.MarkFunctionReferenced(QuestionLoc, Best->Function);
40583201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      return false;
4059429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    }
4060429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
406120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_No_Viable_Function:
406282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
406382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      // Emit a better diagnostic if one of the expressions is a null pointer
406482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      // constant and the other is a pointer type. In this case, the user most
406582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      // likely forgot to take the address of the other expression.
4066429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc))
406782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth        return true;
406882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
406982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands)
4070429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << LHS.get()->getType() << RHS.get()->getType()
4071429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
40723201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      return true;
40733201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
407420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_Ambiguous:
407582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl)
4076429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << LHS.get()->getType() << RHS.get()->getType()
4077429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
4078390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // FIXME: Print the possible common types by printing the return types of
4079390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // the viable candidates.
40803201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      break;
40813201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
408220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_Deleted:
4083b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Conditional operator has only built-in overloads");
40843201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  }
40853201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  return true;
40863201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl}
40873201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
408876458501a8963fa11b91c9337a487de6871169b4Sebastian Redl/// \brief Perform an "extended" implicit conversion as returned by
408976458501a8963fa11b91c9337a487de6871169b4Sebastian Redl/// TryClassUnification.
4090429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegleystatic bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) {
4091b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  InitializedEntity Entity = InitializedEntity::InitializeTemporary(T);
4092429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(),
4093b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor                                                           SourceLocation());
4094429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  Expr *Arg = E.take();
4095429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  InitializationSequence InitSeq(Self, Entity, Kind, &Arg, 1);
40965354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer  ExprResult Result = InitSeq.Perform(Self, Entity, Kind, Arg);
4097b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  if (Result.isInvalid())
409876458501a8963fa11b91c9337a487de6871169b4Sebastian Redl    return true;
4099dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4100429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  E = Result;
410176458501a8963fa11b91c9337a487de6871169b4Sebastian Redl  return false;
410276458501a8963fa11b91c9337a487de6871169b4Sebastian Redl}
410376458501a8963fa11b91c9337a487de6871169b4Sebastian Redl
41043201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// \brief Check the operands of ?: under C++ semantics.
41053201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl///
41063201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y
41073201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl/// extension. In this case, LHS == Cond. (But they're not aliases.)
410850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard SmithQualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS,
410950d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith                                           ExprResult &RHS, ExprValueKind &VK,
411050d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith                                           ExprObjectKind &OK,
41113201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl                                           SourceLocation QuestionLoc) {
4112390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++
4113390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // interface pointers.
41143201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
4115604fb38cd943649bee579103d17ca6cda4aad144Richard Smith  // C++11 [expr.cond]p1
41163201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   The first expression is contextually converted to bool.
4117429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (!Cond.get()->isTypeDependent()) {
4118429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult CondRes = CheckCXXBooleanCondition(Cond.take());
4119429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (CondRes.isInvalid())
41203201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      return QualType();
41213fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    Cond = CondRes;
41223201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  }
41233201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
4124f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  // Assume r-value.
4125f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  VK = VK_RValue;
41260943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall  OK = OK_Ordinary;
4127f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
41283201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  // Either of the arguments dependent?
4129429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent())
41303201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    return Context.DependentTy;
41313201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
4132604fb38cd943649bee579103d17ca6cda4aad144Richard Smith  // C++11 [expr.cond]p2
41333201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   If either the second or the third operand has type (cv) void, ...
4134429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  QualType LTy = LHS.get()->getType();
4135429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  QualType RTy = RHS.get()->getType();
41363201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  bool LVoid = LTy->isVoidType();
41373201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  bool RVoid = RTy->isVoidType();
41383201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  if (LVoid || RVoid) {
41393201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   ... then the [l2r] conversions are performed on the second and third
41403201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   operands ...
4141429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    LHS = DefaultFunctionArrayLvalueConversion(LHS.take());
4142429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    RHS = DefaultFunctionArrayLvalueConversion(RHS.take());
4143429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (LHS.isInvalid() || RHS.isInvalid())
4144429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
4145604fb38cd943649bee579103d17ca6cda4aad144Richard Smith
4146604fb38cd943649bee579103d17ca6cda4aad144Richard Smith    // Finish off the lvalue-to-rvalue conversion by copy-initializing a
4147604fb38cd943649bee579103d17ca6cda4aad144Richard Smith    // temporary if necessary. DefaultFunctionArrayLvalueConversion doesn't
4148604fb38cd943649bee579103d17ca6cda4aad144Richard Smith    // do this part for us.
4149604fb38cd943649bee579103d17ca6cda4aad144Richard Smith    ExprResult &NonVoid = LVoid ? RHS : LHS;
4150604fb38cd943649bee579103d17ca6cda4aad144Richard Smith    if (NonVoid.get()->getType()->isRecordType() &&
4151604fb38cd943649bee579103d17ca6cda4aad144Richard Smith        NonVoid.get()->isGLValue()) {
4152654f1d508cbc9553f4931b340dfa19b453f72ebdDavid Blaikie      if (RequireNonAbstractType(QuestionLoc, NonVoid.get()->getType(),
4153654f1d508cbc9553f4931b340dfa19b453f72ebdDavid Blaikie                             diag::err_allocation_of_abstract_type))
4154654f1d508cbc9553f4931b340dfa19b453f72ebdDavid Blaikie        return QualType();
4155604fb38cd943649bee579103d17ca6cda4aad144Richard Smith      InitializedEntity Entity =
4156604fb38cd943649bee579103d17ca6cda4aad144Richard Smith          InitializedEntity::InitializeTemporary(NonVoid.get()->getType());
4157604fb38cd943649bee579103d17ca6cda4aad144Richard Smith      NonVoid = PerformCopyInitialization(Entity, SourceLocation(), NonVoid);
4158604fb38cd943649bee579103d17ca6cda4aad144Richard Smith      if (NonVoid.isInvalid())
4159604fb38cd943649bee579103d17ca6cda4aad144Richard Smith        return QualType();
4160604fb38cd943649bee579103d17ca6cda4aad144Richard Smith    }
4161604fb38cd943649bee579103d17ca6cda4aad144Richard Smith
4162429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    LTy = LHS.get()->getType();
4163429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    RTy = RHS.get()->getType();
41643201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
41653201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   ... and one of the following shall hold:
41663201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   -- The second or the third operand (but not both) is a throw-
4167604fb38cd943649bee579103d17ca6cda4aad144Richard Smith    //      expression; the result is of the type of the other and is a prvalue.
4168429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    bool LThrow = isa<CXXThrowExpr>(LHS.get());
4169429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    bool RThrow = isa<CXXThrowExpr>(RHS.get());
41703201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    if (LThrow && !RThrow)
41713201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      return RTy;
41723201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    if (RThrow && !LThrow)
41733201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      return LTy;
41743201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
41753201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   -- Both the second and third operands have type void; the result is of
4176604fb38cd943649bee579103d17ca6cda4aad144Richard Smith    //      type void and is a prvalue.
41773201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    if (LVoid && RVoid)
41783201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      return Context.VoidTy;
41793201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
41803201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    // Neither holds, error.
41813201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    Diag(QuestionLoc, diag::err_conditional_void_nonvoid)
41823201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1)
4183429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
41843201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    return QualType();
41853201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  }
41863201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
41873201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  // Neither is void.
41883201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
418950d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  // C++11 [expr.cond]p3
41903201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   Otherwise, if the second and third operand have different types, and
419150d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //   either has (cv) class type [...] an attempt is made to convert each of
419250d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //   those operands to the type of the other.
4193dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (!Context.hasSameType(LTy, RTy) &&
41943201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      (LTy->isRecordType() || RTy->isRecordType())) {
41953201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft;
41963201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    // These return true if a single direction is already ambiguous.
4197b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    QualType L2RType, R2LType;
4198b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    bool HaveL2R, HaveR2L;
4199429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType))
42003201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      return QualType();
4201429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType))
42023201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      return QualType();
4203dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
42043201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   If both can be converted, [...] the program is ill-formed.
42053201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    if (HaveL2R && HaveR2L) {
42063201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      Diag(QuestionLoc, diag::err_conditional_ambiguous)
4207429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
42083201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      return QualType();
42093201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    }
42103201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
42113201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   If exactly one conversion is possible, that conversion is applied to
42123201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   the chosen operand and the converted operands are used in place of the
42133201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   original operands for the remainder of this section.
42143201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    if (HaveL2R) {
4215429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid())
42163201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl        return QualType();
4217429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      LTy = LHS.get()->getType();
42183201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    } else if (HaveR2L) {
4219429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid())
42203201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl        return QualType();
4221429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      RTy = RHS.get()->getType();
42223201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    }
42233201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  }
42243201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
422550d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  // C++11 [expr.cond]p3
422650d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //   if both are glvalues of the same value category and the same type except
422750d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //   for cv-qualification, an attempt is made to convert each of those
422850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //   operands to the type of the other.
422950d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  ExprValueKind LVK = LHS.get()->getValueKind();
423050d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  ExprValueKind RVK = RHS.get()->getValueKind();
423150d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  if (!Context.hasSameType(LTy, RTy) &&
423250d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      Context.hasSameUnqualifiedType(LTy, RTy) &&
423350d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      LVK == RVK && LVK != VK_RValue) {
423450d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    // Since the unqualified types are reference-related and we require the
423550d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    // result to be as if a reference bound directly, the only conversion
423650d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    // we can perform is to add cv-qualifiers.
423750d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    Qualifiers LCVR = Qualifiers::fromCVRMask(LTy.getCVRQualifiers());
423850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    Qualifiers RCVR = Qualifiers::fromCVRMask(RTy.getCVRQualifiers());
423950d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    if (RCVR.isStrictSupersetOf(LCVR)) {
424050d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      LHS = ImpCastExprToType(LHS.take(), RTy, CK_NoOp, LVK);
424150d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      LTy = LHS.get()->getType();
424250d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    }
424350d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    else if (LCVR.isStrictSupersetOf(RCVR)) {
424450d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      RHS = ImpCastExprToType(RHS.take(), LTy, CK_NoOp, RVK);
424550d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      RTy = RHS.get()->getType();
424650d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    }
424750d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  }
424850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith
424950d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  // C++11 [expr.cond]p4
4250f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  //   If the second and third operands are glvalues of the same value
4251f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  //   category and have the same type, the result is of that type and
4252f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  //   value category and it is a bit-field if the second or the third
4253f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  //   operand is a bit-field, or if both are bit-fields.
42540943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall  // We only extend this to bitfields, not to the crazy other kinds of
42550943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall  // l-values.
42561927b1f1d3194c1b9e7c3fa622542dd244f1a963Douglas Gregor  bool Same = Context.hasSameType(LTy, RTy);
425750d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  if (Same && LVK == RVK && LVK != VK_RValue &&
4258429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      LHS.get()->isOrdinaryOrBitFieldObject() &&
4259429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      RHS.get()->isOrdinaryOrBitFieldObject()) {
4260429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    VK = LHS.get()->getValueKind();
4261429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (LHS.get()->getObjectKind() == OK_BitField ||
4262429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        RHS.get()->getObjectKind() == OK_BitField)
42630943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall      OK = OK_BitField;
4264f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    return LTy;
42653911a1a0ab0bbbf2304db8e965f5b8365eb6887bFariborz Jahanian  }
42663201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
426750d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  // C++11 [expr.cond]p5
426850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //   Otherwise, the result is a prvalue. If the second and third operands
42693201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   do not have the same type, and either has (cv) class type, ...
42703201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  if (!Same && (LTy->isRecordType() || RTy->isRecordType())) {
42713201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   ... overload resolution is used to determine the conversions (if any)
42723201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   to be applied to the operands. If the overload resolution fails, the
42733201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    //   program is ill-formed.
42743201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc))
42753201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl      return QualType();
42763201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  }
42773201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
427850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  // C++11 [expr.cond]p6
427950d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //   Lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard
42803201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   conversions are performed on the second and third operands.
4281429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  LHS = DefaultFunctionArrayLvalueConversion(LHS.take());
4282429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  RHS = DefaultFunctionArrayLvalueConversion(RHS.take());
4283429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (LHS.isInvalid() || RHS.isInvalid())
4284429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return QualType();
4285429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  LTy = LHS.get()->getType();
4286429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  RTy = RHS.get()->getType();
42873201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
42883201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   After those conversions, one of the following shall hold:
42893201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   -- The second and third operands have the same type; the result
4290b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor  //      is of that type. If the operands have class type, the result
4291b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor  //      is a prvalue temporary of the result type, which is
4292b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor  //      copy-initialized from either the second operand or the third
4293b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor  //      operand depending on the value of the first operand.
4294b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor  if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) {
4295b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor    if (LTy->isRecordType()) {
4296b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor      // The operands have class type. Make a temporary copy.
4297654f1d508cbc9553f4931b340dfa19b453f72ebdDavid Blaikie      if (RequireNonAbstractType(QuestionLoc, LTy,
4298654f1d508cbc9553f4931b340dfa19b453f72ebdDavid Blaikie                                 diag::err_allocation_of_abstract_type))
4299654f1d508cbc9553f4931b340dfa19b453f72ebdDavid Blaikie        return QualType();
4300b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor      InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy);
4301654f1d508cbc9553f4931b340dfa19b453f72ebdDavid Blaikie
4302dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ExprResult LHSCopy = PerformCopyInitialization(Entity,
4303dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                     SourceLocation(),
4304429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                                     LHS);
4305b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor      if (LHSCopy.isInvalid())
4306b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor        return QualType();
4307dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4308dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ExprResult RHSCopy = PerformCopyInitialization(Entity,
4309dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                     SourceLocation(),
4310429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                                     RHS);
4311b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor      if (RHSCopy.isInvalid())
4312b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor        return QualType();
4313dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4314429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      LHS = LHSCopy;
4315429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      RHS = RHSCopy;
4316b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor    }
4317b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor
43183201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    return LTy;
4319b65a45835afcc93fa99e22b14b4c9734c261d831Douglas Gregor  }
43203201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
4321fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor  // Extension: conditional operator involving vector types.
4322dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (LTy->isVectorType() || RTy->isVectorType())
4323b9b4b789ebd28d6fde1c42de820b036ffaf95162Eli Friedman    return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false);
4324fb4a5436839aae5f5599f2970997e23ee6b895b6Douglas Gregor
43253201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   -- The second and third operands have arithmetic or enumeration type;
43263201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //      the usual arithmetic conversions are performed to bring them to a
43273201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //      common type, and the result is of that type.
43283201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  if (LTy->isArithmeticType() && RTy->isArithmeticType()) {
43293201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl    UsualArithmeticConversions(LHS, RHS);
4330429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (LHS.isInvalid() || RHS.isInvalid())
4331429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
4332429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return LHS.get()->getType();
43333201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  }
43343201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
43353201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  //   -- The second and third operands have pointer type, or one has pointer
433650d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //      type and the other is a null pointer constant, or both are null
433750d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //      pointer constants, at least one of which is non-integral; pointer
433850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //      conversions and qualification conversions are performed to bring them
433950d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //      to their composite pointer type. The result is of the composite
434050d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //      pointer type.
4341de8ac49d32e85d4f65f3e9624f5e21d631babbb0Eli Friedman  //   -- The second and third operands have pointer to member type, or one has
4342de8ac49d32e85d4f65f3e9624f5e21d631babbb0Eli Friedman  //      pointer to member type and the other is a null pointer constant;
4343de8ac49d32e85d4f65f3e9624f5e21d631babbb0Eli Friedman  //      pointer to member conversions and qualification conversions are
4344de8ac49d32e85d4f65f3e9624f5e21d631babbb0Eli Friedman  //      performed to bring them to a common type, whose cv-qualification
4345de8ac49d32e85d4f65f3e9624f5e21d631babbb0Eli Friedman  //      shall match the cv-qualification of either the second or the third
4346de8ac49d32e85d4f65f3e9624f5e21d631babbb0Eli Friedman  //      operand. The result is of the common type.
4347b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor  bool NonStandardCompositeType = false;
43488f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS,
4349b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor                              isSFINAEContext()? 0 : &NonStandardCompositeType);
4350b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor  if (!Composite.isNull()) {
4351b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor    if (NonStandardCompositeType)
4352dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Diag(QuestionLoc,
4353b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor           diag::ext_typecheck_cond_incompatible_operands_nonstandard)
4354b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor        << LTy << RTy << Composite
4355429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
4356dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4357d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl    return Composite;
4358b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor  }
4359dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
43601927b1f1d3194c1b9e7c3fa622542dd244f1a963Douglas Gregor  // Similarly, attempt to find composite type of two objective-c pointers.
43615501636b41318a9161d5f2b278e6e2c6a794e4d2Fariborz Jahanian  Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc);
43625501636b41318a9161d5f2b278e6e2c6a794e4d2Fariborz Jahanian  if (!Composite.isNull())
43635501636b41318a9161d5f2b278e6e2c6a794e4d2Fariborz Jahanian    return Composite;
43643201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl
43657ef932429ed0edcc5e4bf44e516f5f4be6a8a03fChandler Carruth  // Check if we are using a null with a non-pointer type.
4366429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc))
43677ef932429ed0edcc5e4bf44e516f5f4be6a8a03fChandler Carruth    return QualType();
43687ef932429ed0edcc5e4bf44e516f5f4be6a8a03fChandler Carruth
43693201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands)
4370429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    << LHS.get()->getType() << RHS.get()->getType()
4371429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
43723201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl  return QualType();
43733201f6beec688ab9fe8750527e28f52d5420e22dSebastian Redl}
4374d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl
4375d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl/// \brief Find a merged pointer type and convert the two expressions to it.
4376d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl///
437720b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor/// This finds the composite pointer type (or member pointer type) for @p E1
437850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith/// and @p E2 according to C++11 5.9p2. It converts both expressions to this
437920b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor/// type and returns it.
4380d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl/// It does not emit diagnostics.
4381b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor///
43828f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor/// \param Loc The location of the operator requiring these two expressions to
43838f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor/// be converted to the composite pointer type.
43848f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor///
4385b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor/// If \p NonStandardCompositeType is non-NULL, then we are permitted to find
4386b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor/// a non-standard (but still sane) composite type to which both expressions
4387b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor/// can be converted. When such a type is chosen, \c *NonStandardCompositeType
4388b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor/// will be set true.
4389dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiQualType Sema::FindCompositePointerType(SourceLocation Loc,
43908f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor                                        Expr *&E1, Expr *&E2,
4391b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor                                        bool *NonStandardCompositeType) {
4392b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor  if (NonStandardCompositeType)
4393b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor    *NonStandardCompositeType = false;
4394dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
43954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus && "This function assumes C++");
4396d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  QualType T1 = E1->getType(), T2 = E2->getType();
43971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
439850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  // C++11 5.9p2
4399d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  //   Pointer conversions and qualification conversions are performed on
4400d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  //   pointer operands to bring them to their composite pointer type. If
4401d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  //   one operand is a null pointer constant, the composite pointer type is
440250d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //   std::nullptr_t if the other operand is also a null pointer constant or,
440350d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  //   if the other operand is a pointer, the type of the other operand.
440450d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  if (!T1->isAnyPointerType() && !T1->isMemberPointerType() &&
440550d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      !T2->isAnyPointerType() && !T2->isMemberPointerType()) {
440650d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    if (T1->isNullPtrType() &&
440750d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith        E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
440850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      E2 = ImpCastExprToType(E2, T1, CK_NullToPointer).take();
440950d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      return T1;
441050d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    }
441150d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    if (T2->isNullPtrType() &&
441250d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith        E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
441350d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      E1 = ImpCastExprToType(E1, T2, CK_NullToPointer).take();
441450d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith      return T2;
441550d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    }
441650d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith    return QualType();
441750d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  }
441850d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith
4419ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
442073c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman    if (T2->isMemberPointerType())
4421429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      E1 = ImpCastExprToType(E1, T2, CK_NullToMemberPointer).take();
442273c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman    else
4423429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      E1 = ImpCastExprToType(E1, T2, CK_NullToPointer).take();
4424d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl    return T2;
4425d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  }
4426ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
442773c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman    if (T1->isMemberPointerType())
4428429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      E2 = ImpCastExprToType(E2, T1, CK_NullToMemberPointer).take();
442973c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman    else
4430429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      E2 = ImpCastExprToType(E2, T1, CK_NullToPointer).take();
4431d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl    return T1;
4432d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  }
44331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
443420b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor  // Now both have to be pointers or member pointers.
4435a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl  if ((!T1->isPointerType() && !T1->isMemberPointerType()) ||
4436a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl      (!T2->isPointerType() && !T2->isMemberPointerType()))
4437d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl    return QualType();
4438d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl
4439d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  //   Otherwise, of one of the operands has type "pointer to cv1 void," then
4440d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  //   the other has type "pointer to cv2 T" and the composite pointer type is
4441d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  //   "pointer to cv12 void," where cv12 is the union of cv1 and cv2.
4442d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  //   Otherwise, the composite pointer type is a pointer type similar to the
4443d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  //   type of one of the operands, with a cv-qualification signature that is
4444d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  //   the union of the cv-qualification signatures of the operand types.
4445d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  // In practice, the first part here is redundant; it's subsumed by the second.
4446d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  // What we do here is, we build the two possible composite types, and try the
4447d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  // conversions in both directions. If only one works, or if the two composite
4448d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  // types are the same, we have succeeded.
44490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // FIXME: extended qualifiers?
44505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<unsigned, 4> QualifierVector;
4451a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl  QualifierVector QualifierUnion;
44525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<std::pair<const Type *, const Type *>, 4>
4453a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl      ContainingClassVector;
4454a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl  ContainingClassVector MemberOfClass;
4455a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl  QualType Composite1 = Context.getCanonicalType(T1),
4456a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl           Composite2 = Context.getCanonicalType(T2);
4457dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  unsigned NeedConstBefore = 0;
445820b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor  do {
445920b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    const PointerType *Ptr1, *Ptr2;
446020b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    if ((Ptr1 = Composite1->getAs<PointerType>()) &&
446120b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor        (Ptr2 = Composite2->getAs<PointerType>())) {
446220b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      Composite1 = Ptr1->getPointeeType();
446320b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      Composite2 = Ptr2->getPointeeType();
4464dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4465b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor      // If we're allowed to create a non-standard composite type, keep track
4466dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // of where we need to fill in additional 'const' qualifiers.
4467b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor      if (NonStandardCompositeType &&
4468b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor          Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers())
4469b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor        NeedConstBefore = QualifierUnion.size();
4470dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
447120b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      QualifierUnion.push_back(
447220b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor                 Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers());
447320b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0));
447420b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      continue;
447520b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    }
44761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
447720b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    const MemberPointerType *MemPtr1, *MemPtr2;
447820b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) &&
447920b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor        (MemPtr2 = Composite2->getAs<MemberPointerType>())) {
448020b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      Composite1 = MemPtr1->getPointeeType();
448120b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      Composite2 = MemPtr2->getPointeeType();
4482dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4483b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor      // If we're allowed to create a non-standard composite type, keep track
4484dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // of where we need to fill in additional 'const' qualifiers.
4485b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor      if (NonStandardCompositeType &&
4486b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor          Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers())
4487b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor        NeedConstBefore = QualifierUnion.size();
4488dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
448920b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      QualifierUnion.push_back(
449020b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor                 Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers());
449120b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(),
449220b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor                                             MemPtr2->getClass()));
449320b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      continue;
449420b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    }
44951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
449620b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    // FIXME: block pointer types?
44971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
449820b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    // Cannot unwrap any more types.
449920b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    break;
450020b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor  } while (true);
45011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4502b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor  if (NeedConstBefore && NonStandardCompositeType) {
4503b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor    // Extension: Add 'const' to qualifiers that come before the first qualifier
4504dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // mismatch, so that our (non-standard!) composite type meets the
4505b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor    // requirements of C++ [conv.qual]p4 bullet 3.
4506b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor    for (unsigned I = 0; I != NeedConstBefore; ++I) {
4507b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor      if ((QualifierUnion[I] & Qualifiers::Const) == 0) {
4508b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor        QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const;
4509b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor        *NonStandardCompositeType = true;
4510b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor      }
4511b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor    }
4512b2cb1cbd727469e1567a6f2535895e6b64e12c35Douglas Gregor  }
4513dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
451420b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor  // Rewrap the composites as pointers or member pointers with the union CVRs.
4515a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl  ContainingClassVector::reverse_iterator MOC
4516a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl    = MemberOfClass.rbegin();
4517a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl  for (QualifierVector::reverse_iterator
4518a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl         I = QualifierUnion.rbegin(),
4519a439e6f8f57eaeb0a02960d8f2d8ad124ba8b3d5Sebastian Redl         E = QualifierUnion.rend();
452020b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor       I != E; (void)++I, ++MOC) {
45210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(*I);
452220b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    if (MOC->first && MOC->second) {
452320b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      // Rebuild member pointer type
45240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Composite1 = Context.getMemberPointerType(
45250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                    Context.getQualifiedType(Composite1, Quals),
45260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                    MOC->first);
45270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Composite2 = Context.getMemberPointerType(
45280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                    Context.getQualifiedType(Composite2, Quals),
45290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                    MOC->second);
453020b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    } else {
453120b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor      // Rebuild pointer type
45320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Composite1
45330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        = Context.getPointerType(Context.getQualifiedType(Composite1, Quals));
45340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Composite2
45350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        = Context.getPointerType(Context.getQualifiedType(Composite2, Quals));
453620b3e9918cf7d7845c920baabc4fb2f1ac0ee1d2Douglas Gregor    }
4537d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  }
4538d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl
45398f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  // Try to convert to the first composite pointer type.
45408f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  InitializedEntity Entity1
45418f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    = InitializedEntity::InitializeTemporary(Composite1);
45428f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  InitializationKind Kind
45438f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    = InitializationKind::CreateCopy(Loc, SourceLocation());
45448f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  InitializationSequence E1ToC1(*this, Entity1, Kind, &E1, 1);
45458f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  InitializationSequence E2ToC1(*this, Entity1, Kind, &E2, 1);
45468f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor
45478f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  if (E1ToC1 && E2ToC1) {
45488f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    // Conversion to Composite1 is viable.
45498f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    if (!Context.hasSameType(Composite1, Composite2)) {
45508f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor      // Composite2 is a different type from Composite1. Check whether
45518f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor      // Composite2 is also viable.
45528f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor      InitializedEntity Entity2
45538f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor        = InitializedEntity::InitializeTemporary(Composite2);
45548f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor      InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1);
45558f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor      InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1);
45568f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor      if (E1ToC2 && E2ToC2) {
45578f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor        // Both Composite1 and Composite2 are viable and are different;
45588f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor        // this is an ambiguity.
45598f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor        return QualType();
45608f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor      }
45618f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    }
4562d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl
45638f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    // Convert E1 to Composite1
456460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult E1Result
45655354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer      = E1ToC1.Perform(*this, Entity1, Kind, E1);
45668f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    if (E1Result.isInvalid())
45678f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor      return QualType();
45688f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    E1 = E1Result.takeAs<Expr>();
45698f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor
45708f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    // Convert E2 to Composite1
457160d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult E2Result
45725354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer      = E2ToC1.Perform(*this, Entity1, Kind, E2);
45738f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    if (E2Result.isInvalid())
45748f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor      return QualType();
45758f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    E2 = E2Result.takeAs<Expr>();
4576dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
45778f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    return Composite1;
4578d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl  }
45798f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor
45808f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  // Check whether Composite2 is viable.
45818f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  InitializedEntity Entity2
45828f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    = InitializedEntity::InitializeTemporary(Composite2);
45838f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1);
45848f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1);
45858f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  if (!E1ToC2 || !E2ToC2)
45868f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    return QualType();
4587dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
45888f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  // Convert E1 to Composite2
458960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult E1Result
45905354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    = E1ToC2.Perform(*this, Entity2, Kind, E1);
45918f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  if (E1Result.isInvalid())
45928f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    return QualType();
45938f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  E1 = E1Result.takeAs<Expr>();
4594dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
45958f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  // Convert E2 to Composite2
459660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult E2Result
45975354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    = E2ToC2.Perform(*this, Entity2, Kind, E2);
45988f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  if (E2Result.isInvalid())
45998f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor    return QualType();
46008f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  E2 = E2Result.takeAs<Expr>();
4601dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
46028f00dcf1414dd0aefa18c12eb105428c83fba4f5Douglas Gregor  return Composite2;
4603d1bd7fc4cd88f8790c56620d22560e19717f468aSebastian Redl}
4604165a0a07c0a91f8d61ee3737b62e7f376bb7e1c7Anders Carlsson
460560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::MaybeBindToTemporary(Expr *E) {
460619cc1c715e97885f10b3496f91d0a8ae24642faaDouglas Gregor  if (!E)
460719cc1c715e97885f10b3496f91d0a8ae24642faaDouglas Gregor    return ExprError();
4608dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4609f85e193739c953358c865005855253af4f68a497John McCall  assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?");
4610f85e193739c953358c865005855253af4f68a497John McCall
4611f85e193739c953358c865005855253af4f68a497John McCall  // If the result is a glvalue, we shouldn't bind it.
4612f85e193739c953358c865005855253af4f68a497John McCall  if (!E->isRValue())
4613089c2602ebaccdda271beaabdd32575b354d4d09Anders Carlsson    return Owned(E);
46141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4615f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, calls that return a retainable type can return retained,
4616f85e193739c953358c865005855253af4f68a497John McCall  // in which case we have to insert a consuming cast.
46174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount &&
4618f85e193739c953358c865005855253af4f68a497John McCall      E->getType()->isObjCRetainableType()) {
4619f85e193739c953358c865005855253af4f68a497John McCall
4620f85e193739c953358c865005855253af4f68a497John McCall    bool ReturnsRetained;
4621f85e193739c953358c865005855253af4f68a497John McCall
4622f85e193739c953358c865005855253af4f68a497John McCall    // For actual calls, we compute this by examining the type of the
4623f85e193739c953358c865005855253af4f68a497John McCall    // called value.
4624f85e193739c953358c865005855253af4f68a497John McCall    if (CallExpr *Call = dyn_cast<CallExpr>(E)) {
4625f85e193739c953358c865005855253af4f68a497John McCall      Expr *Callee = Call->getCallee()->IgnoreParens();
4626f85e193739c953358c865005855253af4f68a497John McCall      QualType T = Callee->getType();
4627f85e193739c953358c865005855253af4f68a497John McCall
4628f85e193739c953358c865005855253af4f68a497John McCall      if (T == Context.BoundMemberTy) {
4629f85e193739c953358c865005855253af4f68a497John McCall        // Handle pointer-to-members.
4630f85e193739c953358c865005855253af4f68a497John McCall        if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee))
4631f85e193739c953358c865005855253af4f68a497John McCall          T = BinOp->getRHS()->getType();
4632f85e193739c953358c865005855253af4f68a497John McCall        else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee))
4633f85e193739c953358c865005855253af4f68a497John McCall          T = Mem->getMemberDecl()->getType();
4634f85e193739c953358c865005855253af4f68a497John McCall      }
4635f85e193739c953358c865005855253af4f68a497John McCall
4636f85e193739c953358c865005855253af4f68a497John McCall      if (const PointerType *Ptr = T->getAs<PointerType>())
4637f85e193739c953358c865005855253af4f68a497John McCall        T = Ptr->getPointeeType();
4638f85e193739c953358c865005855253af4f68a497John McCall      else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>())
4639f85e193739c953358c865005855253af4f68a497John McCall        T = Ptr->getPointeeType();
4640f85e193739c953358c865005855253af4f68a497John McCall      else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>())
4641f85e193739c953358c865005855253af4f68a497John McCall        T = MemPtr->getPointeeType();
4642f85e193739c953358c865005855253af4f68a497John McCall
4643f85e193739c953358c865005855253af4f68a497John McCall      const FunctionType *FTy = T->getAs<FunctionType>();
4644f85e193739c953358c865005855253af4f68a497John McCall      assert(FTy && "call to value not of function type?");
4645f85e193739c953358c865005855253af4f68a497John McCall      ReturnsRetained = FTy->getExtInfo().getProducesResult();
4646f85e193739c953358c865005855253af4f68a497John McCall
4647f85e193739c953358c865005855253af4f68a497John McCall    // ActOnStmtExpr arranges things so that StmtExprs of retainable
4648f85e193739c953358c865005855253af4f68a497John McCall    // type always produce a +1 object.
4649f85e193739c953358c865005855253af4f68a497John McCall    } else if (isa<StmtExpr>(E)) {
4650f85e193739c953358c865005855253af4f68a497John McCall      ReturnsRetained = true;
4651f85e193739c953358c865005855253af4f68a497John McCall
4652ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // We hit this case with the lambda conversion-to-block optimization;
4653ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // we don't want any extra casts here.
4654ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    } else if (isa<CastExpr>(E) &&
4655ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek               isa<BlockExpr>(cast<CastExpr>(E)->getSubExpr())) {
4656ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return Owned(E);
4657ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
4658f85e193739c953358c865005855253af4f68a497John McCall    // For message sends and property references, we try to find an
4659f85e193739c953358c865005855253af4f68a497John McCall    // actual method.  FIXME: we should infer retention by selector in
4660f85e193739c953358c865005855253af4f68a497John McCall    // cases where we don't have an actual method.
4661ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    } else {
4662ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ObjCMethodDecl *D = 0;
4663ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) {
4664ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        D = Send->getMethodDecl();
4665eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard      } else if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(E)) {
4666eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard        D = BoxedExpr->getBoxingMethod();
4667ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      } else if (ObjCArrayLiteral *ArrayLit = dyn_cast<ObjCArrayLiteral>(E)) {
4668ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        D = ArrayLit->getArrayWithObjectsMethod();
4669ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      } else if (ObjCDictionaryLiteral *DictLit
4670ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                        = dyn_cast<ObjCDictionaryLiteral>(E)) {
4671ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        D = DictLit->getDictWithObjectsMethod();
4672ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      }
46735132655e4296b780672e9a96b46a740135073534Douglas Gregor
4674f85e193739c953358c865005855253af4f68a497John McCall      ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>());
4675fc4b191b07df309d95aefae39f4cf7c456c91afaJohn McCall
4676fc4b191b07df309d95aefae39f4cf7c456c91afaJohn McCall      // Don't do reclaims on performSelector calls; despite their
4677fc4b191b07df309d95aefae39f4cf7c456c91afaJohn McCall      // return type, the invoked method doesn't necessarily actually
4678fc4b191b07df309d95aefae39f4cf7c456c91afaJohn McCall      // return an object.
4679fc4b191b07df309d95aefae39f4cf7c456c91afaJohn McCall      if (!ReturnsRetained &&
4680fc4b191b07df309d95aefae39f4cf7c456c91afaJohn McCall          D && D->getMethodFamily() == OMF_performSelector)
4681fc4b191b07df309d95aefae39f4cf7c456c91afaJohn McCall        return Owned(E);
4682f85e193739c953358c865005855253af4f68a497John McCall    }
4683f85e193739c953358c865005855253af4f68a497John McCall
4684567c5862be3e278bfc14778612f2d66eafdc4724John McCall    // Don't reclaim an object of Class type.
4685567c5862be3e278bfc14778612f2d66eafdc4724John McCall    if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType())
4686567c5862be3e278bfc14778612f2d66eafdc4724John McCall      return Owned(E);
4687567c5862be3e278bfc14778612f2d66eafdc4724John McCall
46887e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall    ExprNeedsCleanups = true;
46897e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall
469033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall    CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject
469133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall                                   : CK_ARCReclaimReturnedObject);
46927e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall    return Owned(ImplicitCastExpr::Create(Context, E->getType(), ck, E, 0,
46937e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall                                          VK_RValue));
4694f85e193739c953358c865005855253af4f68a497John McCall  }
46951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46964e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CPlusPlus)
4697f85e193739c953358c865005855253af4f68a497John McCall    return Owned(E);
4698f85e193739c953358c865005855253af4f68a497John McCall
4699b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne  // Search for the base element type (cf. ASTContext::getBaseElementType) with
4700b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne  // a fast path for the common case that the type is directly a RecordType.
4701b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne  const Type *T = Context.getCanonicalType(E->getType().getTypePtr());
4702b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne  const RecordType *RT = 0;
4703b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne  while (!RT) {
4704b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne    switch (T->getTypeClass()) {
4705b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne    case Type::Record:
4706b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne      RT = cast<RecordType>(T);
4707b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne      break;
4708b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne    case Type::ConstantArray:
4709b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne    case Type::IncompleteArray:
4710b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne    case Type::VariableArray:
4711b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne    case Type::DependentSizedArray:
4712b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne      T = cast<ArrayType>(T)->getElementType().getTypePtr();
4713b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne      break;
4714b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne    default:
4715b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne      return Owned(E);
4716b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne    }
4717b4ab8431b414ca399edc890f12f758cb19c090b8Peter Collingbourne  }
471886ff308724171494395a840fd2efbe25e62f352eJohn McCall
471976f3f69db1416425070177243e9f390122c553e0Richard Smith  // That should be enough to guarantee that this type is complete, if we're
472076f3f69db1416425070177243e9f390122c553e0Richard Smith  // not processing a decltype expression.
4721b7ee2e5cc999a45ec4fd7b879477816714aabb7eJeffrey Yasskin  CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
4722213d70b58b4f48050c3e545ce1bd4b0ec3af74beRichard Smith  if (RD->isInvalidDecl() || RD->isDependentContext())
472386ff308724171494395a840fd2efbe25e62f352eJohn McCall    return Owned(E);
472476f3f69db1416425070177243e9f390122c553e0Richard Smith
472576f3f69db1416425070177243e9f390122c553e0Richard Smith  bool IsDecltype = ExprEvalContexts.back().IsDecltype;
472676f3f69db1416425070177243e9f390122c553e0Richard Smith  CXXDestructorDecl *Destructor = IsDecltype ? 0 : LookupDestructor(RD);
4727f85e193739c953358c865005855253af4f68a497John McCall
4728f85e193739c953358c865005855253af4f68a497John McCall  if (Destructor) {
47295f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    MarkFunctionReferenced(E->getExprLoc(), Destructor);
4730c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall    CheckDestructorAccess(E->getExprLoc(), Destructor,
4731c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall                          PDiag(diag::err_access_dtor_temp)
4732c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall                            << E->getType());
4733213d70b58b4f48050c3e545ce1bd4b0ec3af74beRichard Smith    DiagnoseUseOfDecl(Destructor, E->getExprLoc());
4734213d70b58b4f48050c3e545ce1bd4b0ec3af74beRichard Smith
473576f3f69db1416425070177243e9f390122c553e0Richard Smith    // If destructor is trivial, we can avoid the extra copy.
473676f3f69db1416425070177243e9f390122c553e0Richard Smith    if (Destructor->isTrivial())
473776f3f69db1416425070177243e9f390122c553e0Richard Smith      return Owned(E);
4738f85e193739c953358c865005855253af4f68a497John McCall
473980ee6e878a169e6255d4686a91bb696151ff229fJohn McCall    // We need a cleanup, but we don't need to remember the temporary.
4740f85e193739c953358c865005855253af4f68a497John McCall    ExprNeedsCleanups = true;
474176f3f69db1416425070177243e9f390122c553e0Richard Smith  }
4742213d70b58b4f48050c3e545ce1bd4b0ec3af74beRichard Smith
4743213d70b58b4f48050c3e545ce1bd4b0ec3af74beRichard Smith  CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor);
474476f3f69db1416425070177243e9f390122c553e0Richard Smith  CXXBindTemporaryExpr *Bind = CXXBindTemporaryExpr::Create(Context, Temp, E);
474576f3f69db1416425070177243e9f390122c553e0Richard Smith
474676f3f69db1416425070177243e9f390122c553e0Richard Smith  if (IsDecltype)
474776f3f69db1416425070177243e9f390122c553e0Richard Smith    ExprEvalContexts.back().DelayedDecltypeBinds.push_back(Bind);
474876f3f69db1416425070177243e9f390122c553e0Richard Smith
474976f3f69db1416425070177243e9f390122c553e0Richard Smith  return Owned(Bind);
4750def11996fb558f58a8a7ae87aa95eb0bb96bfe4aAnders Carlsson}
4751def11996fb558f58a8a7ae87aa95eb0bb96bfe4aAnders Carlsson
475280ee6e878a169e6255d4686a91bb696151ff229fJohn McCallExprResult
475380ee6e878a169e6255d4686a91bb696151ff229fJohn McCallSema::MaybeCreateExprWithCleanups(ExprResult SubExpr) {
475480ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  if (SubExpr.isInvalid())
475580ee6e878a169e6255d4686a91bb696151ff229fJohn McCall    return ExprError();
475680ee6e878a169e6255d4686a91bb696151ff229fJohn McCall
475780ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  return Owned(MaybeCreateExprWithCleanups(SubExpr.take()));
475880ee6e878a169e6255d4686a91bb696151ff229fJohn McCall}
475980ee6e878a169e6255d4686a91bb696151ff229fJohn McCall
47604765fa05b5652fcc4356371c2f481d0ea9a1b007John McCallExpr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) {
476199ba36de0d5b34624e74e77bffa73929617976cbAnders Carlsson  assert(SubExpr && "sub expression can't be null!");
47621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4763d2cce136878badcee6694b216479d7f1b72a1e68Eli Friedman  CleanupVarDeclMarking();
4764d2cce136878badcee6694b216479d7f1b72a1e68Eli Friedman
476580ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects;
476680ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  assert(ExprCleanupObjects.size() >= FirstCleanup);
476780ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  assert(ExprNeedsCleanups || ExprCleanupObjects.size() == FirstCleanup);
4768f85e193739c953358c865005855253af4f68a497John McCall  if (!ExprNeedsCleanups)
476999ba36de0d5b34624e74e77bffa73929617976cbAnders Carlsson    return SubExpr;
47701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
477180ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  ArrayRef<ExprWithCleanups::CleanupObject> Cleanups
477280ee6e878a169e6255d4686a91bb696151ff229fJohn McCall    = llvm::makeArrayRef(ExprCleanupObjects.begin() + FirstCleanup,
477380ee6e878a169e6255d4686a91bb696151ff229fJohn McCall                         ExprCleanupObjects.size() - FirstCleanup);
47741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
477580ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  Expr *E = ExprWithCleanups::Create(Context, SubExpr, Cleanups);
477680ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  DiscardCleanupsInEvaluationContext();
4777dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
477880ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  return E;
477990f9382b3e97afa55e6aaaa4ab31c7473a8c7bb9Douglas Gregor}
478090f9382b3e97afa55e6aaaa4ab31c7473a8c7bb9Douglas Gregor
47814765fa05b5652fcc4356371c2f481d0ea9a1b007John McCallStmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) {
4782bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis  assert(SubStmt && "sub statement can't be null!");
4783bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis
4784d2cce136878badcee6694b216479d7f1b72a1e68Eli Friedman  CleanupVarDeclMarking();
4785d2cce136878badcee6694b216479d7f1b72a1e68Eli Friedman
4786f85e193739c953358c865005855253af4f68a497John McCall  if (!ExprNeedsCleanups)
4787bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis    return SubStmt;
4788bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis
4789bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis  // FIXME: In order to attach the temporaries, wrap the statement into
4790bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis  // a StmtExpr; currently this is only used for asm statements.
4791bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis  // This is hacky, either create a new CXXStmtWithTemporaries statement or
4792bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis  // a new AsmStmtWithTemporaries.
4793bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis  CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, &SubStmt, 1,
4794bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis                                                      SourceLocation(),
4795bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis                                                      SourceLocation());
4796bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis  Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(),
4797bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis                                   SourceLocation());
47984765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  return MaybeCreateExprWithCleanups(E);
4799bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis}
4800bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis
480176f3f69db1416425070177243e9f390122c553e0Richard Smith/// Process the expression contained within a decltype. For such expressions,
480276f3f69db1416425070177243e9f390122c553e0Richard Smith/// certain semantic checks on temporaries are delayed until this point, and
480376f3f69db1416425070177243e9f390122c553e0Richard Smith/// are omitted for the 'topmost' call in the decltype expression. If the
480476f3f69db1416425070177243e9f390122c553e0Richard Smith/// topmost call bound a temporary, strip that temporary off the expression.
480576f3f69db1416425070177243e9f390122c553e0Richard SmithExprResult Sema::ActOnDecltypeExpression(Expr *E) {
480676f3f69db1416425070177243e9f390122c553e0Richard Smith  ExpressionEvaluationContextRecord &Rec = ExprEvalContexts.back();
480776f3f69db1416425070177243e9f390122c553e0Richard Smith  assert(Rec.IsDecltype && "not in a decltype expression");
480876f3f69db1416425070177243e9f390122c553e0Richard Smith
480976f3f69db1416425070177243e9f390122c553e0Richard Smith  // C++11 [expr.call]p11:
481076f3f69db1416425070177243e9f390122c553e0Richard Smith  //   If a function call is a prvalue of object type,
481176f3f69db1416425070177243e9f390122c553e0Richard Smith  // -- if the function call is either
481276f3f69db1416425070177243e9f390122c553e0Richard Smith  //   -- the operand of a decltype-specifier, or
481376f3f69db1416425070177243e9f390122c553e0Richard Smith  //   -- the right operand of a comma operator that is the operand of a
481476f3f69db1416425070177243e9f390122c553e0Richard Smith  //      decltype-specifier,
481576f3f69db1416425070177243e9f390122c553e0Richard Smith  //   a temporary object is not introduced for the prvalue.
481676f3f69db1416425070177243e9f390122c553e0Richard Smith
481776f3f69db1416425070177243e9f390122c553e0Richard Smith  // Recursively rebuild ParenExprs and comma expressions to strip out the
481876f3f69db1416425070177243e9f390122c553e0Richard Smith  // outermost CXXBindTemporaryExpr, if any.
481976f3f69db1416425070177243e9f390122c553e0Richard Smith  if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
482076f3f69db1416425070177243e9f390122c553e0Richard Smith    ExprResult SubExpr = ActOnDecltypeExpression(PE->getSubExpr());
482176f3f69db1416425070177243e9f390122c553e0Richard Smith    if (SubExpr.isInvalid())
482276f3f69db1416425070177243e9f390122c553e0Richard Smith      return ExprError();
482376f3f69db1416425070177243e9f390122c553e0Richard Smith    if (SubExpr.get() == PE->getSubExpr())
482476f3f69db1416425070177243e9f390122c553e0Richard Smith      return Owned(E);
482576f3f69db1416425070177243e9f390122c553e0Richard Smith    return ActOnParenExpr(PE->getLParen(), PE->getRParen(), SubExpr.take());
482676f3f69db1416425070177243e9f390122c553e0Richard Smith  }
482776f3f69db1416425070177243e9f390122c553e0Richard Smith  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
482876f3f69db1416425070177243e9f390122c553e0Richard Smith    if (BO->getOpcode() == BO_Comma) {
482976f3f69db1416425070177243e9f390122c553e0Richard Smith      ExprResult RHS = ActOnDecltypeExpression(BO->getRHS());
483076f3f69db1416425070177243e9f390122c553e0Richard Smith      if (RHS.isInvalid())
483176f3f69db1416425070177243e9f390122c553e0Richard Smith        return ExprError();
483276f3f69db1416425070177243e9f390122c553e0Richard Smith      if (RHS.get() == BO->getRHS())
483376f3f69db1416425070177243e9f390122c553e0Richard Smith        return Owned(E);
483476f3f69db1416425070177243e9f390122c553e0Richard Smith      return Owned(new (Context) BinaryOperator(BO->getLHS(), RHS.take(),
483576f3f69db1416425070177243e9f390122c553e0Richard Smith                                                BO_Comma, BO->getType(),
483676f3f69db1416425070177243e9f390122c553e0Richard Smith                                                BO->getValueKind(),
483776f3f69db1416425070177243e9f390122c553e0Richard Smith                                                BO->getObjectKind(),
483876f3f69db1416425070177243e9f390122c553e0Richard Smith                                                BO->getOperatorLoc()));
483976f3f69db1416425070177243e9f390122c553e0Richard Smith    }
484076f3f69db1416425070177243e9f390122c553e0Richard Smith  }
484176f3f69db1416425070177243e9f390122c553e0Richard Smith
484276f3f69db1416425070177243e9f390122c553e0Richard Smith  CXXBindTemporaryExpr *TopBind = dyn_cast<CXXBindTemporaryExpr>(E);
484376f3f69db1416425070177243e9f390122c553e0Richard Smith  if (TopBind)
484476f3f69db1416425070177243e9f390122c553e0Richard Smith    E = TopBind->getSubExpr();
484576f3f69db1416425070177243e9f390122c553e0Richard Smith
484676f3f69db1416425070177243e9f390122c553e0Richard Smith  // Disable the special decltype handling now.
484776f3f69db1416425070177243e9f390122c553e0Richard Smith  Rec.IsDecltype = false;
484876f3f69db1416425070177243e9f390122c553e0Richard Smith
4849d22f0847a95938203fe12ab50bfc33d79c664a77Richard Smith  // In MS mode, don't perform any extra checking of call return types within a
4850d22f0847a95938203fe12ab50bfc33d79c664a77Richard Smith  // decltype expression.
4851d22f0847a95938203fe12ab50bfc33d79c664a77Richard Smith  if (getLangOpts().MicrosoftMode)
4852d22f0847a95938203fe12ab50bfc33d79c664a77Richard Smith    return Owned(E);
4853d22f0847a95938203fe12ab50bfc33d79c664a77Richard Smith
485476f3f69db1416425070177243e9f390122c553e0Richard Smith  // Perform the semantic checks we delayed until this point.
485576f3f69db1416425070177243e9f390122c553e0Richard Smith  CallExpr *TopCall = dyn_cast<CallExpr>(E);
485676f3f69db1416425070177243e9f390122c553e0Richard Smith  for (unsigned I = 0, N = Rec.DelayedDecltypeCalls.size(); I != N; ++I) {
485776f3f69db1416425070177243e9f390122c553e0Richard Smith    CallExpr *Call = Rec.DelayedDecltypeCalls[I];
485876f3f69db1416425070177243e9f390122c553e0Richard Smith    if (Call == TopCall)
485976f3f69db1416425070177243e9f390122c553e0Richard Smith      continue;
486076f3f69db1416425070177243e9f390122c553e0Richard Smith
486176f3f69db1416425070177243e9f390122c553e0Richard Smith    if (CheckCallReturnType(Call->getCallReturnType(),
486296a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar                            Call->getLocStart(),
486376f3f69db1416425070177243e9f390122c553e0Richard Smith                            Call, Call->getDirectCallee()))
486476f3f69db1416425070177243e9f390122c553e0Richard Smith      return ExprError();
486576f3f69db1416425070177243e9f390122c553e0Richard Smith  }
486676f3f69db1416425070177243e9f390122c553e0Richard Smith
486776f3f69db1416425070177243e9f390122c553e0Richard Smith  // Now all relevant types are complete, check the destructors are accessible
486876f3f69db1416425070177243e9f390122c553e0Richard Smith  // and non-deleted, and annotate them on the temporaries.
486976f3f69db1416425070177243e9f390122c553e0Richard Smith  for (unsigned I = 0, N = Rec.DelayedDecltypeBinds.size(); I != N; ++I) {
487076f3f69db1416425070177243e9f390122c553e0Richard Smith    CXXBindTemporaryExpr *Bind = Rec.DelayedDecltypeBinds[I];
487176f3f69db1416425070177243e9f390122c553e0Richard Smith    if (Bind == TopBind)
487276f3f69db1416425070177243e9f390122c553e0Richard Smith      continue;
487376f3f69db1416425070177243e9f390122c553e0Richard Smith
487476f3f69db1416425070177243e9f390122c553e0Richard Smith    CXXTemporary *Temp = Bind->getTemporary();
487576f3f69db1416425070177243e9f390122c553e0Richard Smith
487676f3f69db1416425070177243e9f390122c553e0Richard Smith    CXXRecordDecl *RD =
487776f3f69db1416425070177243e9f390122c553e0Richard Smith      Bind->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
487876f3f69db1416425070177243e9f390122c553e0Richard Smith    CXXDestructorDecl *Destructor = LookupDestructor(RD);
487976f3f69db1416425070177243e9f390122c553e0Richard Smith    Temp->setDestructor(Destructor);
488076f3f69db1416425070177243e9f390122c553e0Richard Smith
48812f68ca09ca8b5944fcab14578a161511afde406fRichard Smith    MarkFunctionReferenced(Bind->getExprLoc(), Destructor);
48822f68ca09ca8b5944fcab14578a161511afde406fRichard Smith    CheckDestructorAccess(Bind->getExprLoc(), Destructor,
488376f3f69db1416425070177243e9f390122c553e0Richard Smith                          PDiag(diag::err_access_dtor_temp)
48842f68ca09ca8b5944fcab14578a161511afde406fRichard Smith                            << Bind->getType());
48852f68ca09ca8b5944fcab14578a161511afde406fRichard Smith    DiagnoseUseOfDecl(Destructor, Bind->getExprLoc());
488676f3f69db1416425070177243e9f390122c553e0Richard Smith
488776f3f69db1416425070177243e9f390122c553e0Richard Smith    // We need a cleanup, but we don't need to remember the temporary.
488876f3f69db1416425070177243e9f390122c553e0Richard Smith    ExprNeedsCleanups = true;
488976f3f69db1416425070177243e9f390122c553e0Richard Smith  }
489076f3f69db1416425070177243e9f390122c553e0Richard Smith
489176f3f69db1416425070177243e9f390122c553e0Richard Smith  // Possibly strip off the top CXXBindTemporaryExpr.
489276f3f69db1416425070177243e9f390122c553e0Richard Smith  return Owned(E);
489376f3f69db1416425070177243e9f390122c553e0Richard Smith}
489476f3f69db1416425070177243e9f390122c553e0Richard Smith
489560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
48969ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, SourceLocation OpLoc,
4897b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                   tok::TokenKind OpKind, ParsedType &ObjectType,
4898d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor                                   bool &MayBePseudoDestructor) {
48992dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // Since this might be a postfix expression, get rid of ParenListExprs.
490060d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
49019ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (Result.isInvalid()) return ExprError();
49029ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Base = Result.get();
49031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49043c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  Result = CheckPlaceholderExpr(Base);
49053c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  if (Result.isInvalid()) return ExprError();
49063c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  Base = Result.take();
49073c3b7f90a863af43fa63043d396553ecf205351cJohn McCall
49089ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  QualType BaseType = Base->getType();
4909d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor  MayBePseudoDestructor = false;
49102dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (BaseType->isDependentType()) {
491143d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor    // If we have a pointer to a dependent type and are using the -> operator,
491243d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor    // the object type is the type that the pointer points to. We might still
491343d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor    // have enough information about that type to do something useful.
491443d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor    if (OpKind == tok::arrow)
491543d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor      if (const PointerType *Ptr = BaseType->getAs<PointerType>())
491643d8863df9d02f81acdf5f73fbc288f285bf442eDouglas Gregor        BaseType = Ptr->getPointeeType();
4917dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4918b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ObjectType = ParsedType::make(BaseType);
4919d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    MayBePseudoDestructor = true;
49209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return Owned(Base);
49212dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  }
49221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49232dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  // C++ [over.match.oper]p8:
49241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   [...] When operator->returns, the operator-> is applied  to the value
49252dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  //   returned, with the original second operand.
49262dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor  if (OpKind == tok::arrow) {
4927c4e8321deb2bd83f734a09749460050f40ec51d1John McCall    // The set of types we've considered so far.
4928432887fadd6bf87a6985906d6cef356b1f617d84John McCall    llvm::SmallPtrSet<CanQualType,8> CTypes;
49295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 8> Locations;
4930432887fadd6bf87a6985906d6cef356b1f617d84John McCall    CTypes.insert(Context.getCanonicalType(BaseType));
4931dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
49322dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    while (BaseType->isRecordType()) {
49339ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Result = BuildOverloadedArrowExpr(S, Base, OpLoc);
49349ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      if (Result.isInvalid())
49352dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor        return ExprError();
49369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Base = Result.get();
49379ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base))
4938de699e551062f2b20e66decd4de87ee0804b67ccAnders Carlsson        Locations.push_back(OpCall->getDirectCallee()->getLocation());
49399ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      BaseType = Base->getType();
4940c4e8321deb2bd83f734a09749460050f40ec51d1John McCall      CanQualType CBaseType = Context.getCanonicalType(BaseType);
4941432887fadd6bf87a6985906d6cef356b1f617d84John McCall      if (!CTypes.insert(CBaseType)) {
49424a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian        Diag(OpLoc, diag::err_operator_arrow_circular);
49437a8233a69103a6eeb6602bd4a17fb5d9b7bacaadFariborz Jahanian        for (unsigned i = 0; i < Locations.size(); i++)
49447a8233a69103a6eeb6602bd4a17fb5d9b7bacaadFariborz Jahanian          Diag(Locations[i], diag::note_declared_at);
49454a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian        return ExprError();
49464a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian      }
49472dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor    }
49481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49491d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor    if (BaseType->isPointerType() || BaseType->isObjCObjectPointerType())
495031658dfe908d07666e2820ced8443a9a1988f1ebDouglas Gregor      BaseType = BaseType->getPointeeType();
495131658dfe908d07666e2820ced8443a9a1988f1ebDouglas Gregor  }
49521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49531d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  // Objective-C properties allow "." access on Objective-C pointer types,
49541d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  // so adjust the base type to the object type itself.
49551d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  if (BaseType->isObjCObjectPointerType())
49561d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor    BaseType = BaseType->getPointeeType();
49571d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor
49581d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  // C++ [basic.lookup.classref]p2:
49591d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  //   [...] If the type of the object expression is of pointer to scalar
49601d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  //   type, the unqualified-id is looked up in the context of the complete
49611d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  //   postfix-expression.
49621d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  //
49631d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  // This also indicates that we could be parsing a pseudo-destructor-name.
49641d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  // Note that Objective-C class and object types can be pseudo-destructor
49651d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  // expressions or normal member (ivar or property) access expressions.
49661d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  if (BaseType->isObjCObjectOrInterfaceType()) {
49671d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor    MayBePseudoDestructor = true;
49681d7049a6eddcc1a4bd33c6a595d4ad2ae8c1ceceDouglas Gregor  } else if (!BaseType->isRecordType()) {
4969b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ObjectType = ParsedType();
4970d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor    MayBePseudoDestructor = true;
49719ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return Owned(Base);
4972c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  }
49731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4974cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  // The object type must be complete (or dependent), or
4975cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  // C++11 [expr.prim.general]p3:
4976cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  //   Unlike the object expression in other contexts, *this is not required to
4977cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  //   be of complete type for purposes of class member access (5.2.5) outside
4978cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  //   the member function body.
497903c5705a99a96a471b2868898ee9688a6721e02aDouglas Gregor  if (!BaseType->isDependentType() &&
4980cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor      !isThisOutsideMemberFunctionBody(BaseType) &&
4981d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      RequireCompleteType(OpLoc, BaseType, diag::err_incomplete_member_access))
498203c5705a99a96a471b2868898ee9688a6721e02aDouglas Gregor    return ExprError();
4983dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4984c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  // C++ [basic.lookup.classref]p2:
49851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   If the id-expression in a class member access (5.2.5) is an
498603c5705a99a96a471b2868898ee9688a6721e02aDouglas Gregor  //   unqualified-id, and the type of the object expression is of a class
4987c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  //   type C (or of pointer to a class type C), the unqualified-id is looked
4988c68afe2cbe7f875a9243c411077602fb5f5dc74bDouglas Gregor  //   up in the scope of class C. [...]
4989b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ObjectType = ParsedType::make(BaseType);
49903fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Base;
49912dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor}
49922dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor
499360d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc,
49949ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                   Expr *MemExpr) {
499577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc);
49969ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Diag(MemExpr->getLocStart(), diag::err_dtor_expr_without_call)
49979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    << isa<CXXPseudoDestructorExpr>(MemExpr)
4998849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    << FixItHint::CreateInsertion(ExpectedLParenLoc, "()");
4999dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
500077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  return ActOnCallExpr(/*Scope*/ 0,
50019ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                       MemExpr,
500277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                       /*LPLoc*/ ExpectedLParenLoc,
5003f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                       MultiExprArg(),
500477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor                       /*RPLoc*/ ExpectedLParenLoc);
500577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor}
5006d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
5007e0dbedfdd1f4f57cc9e8fd594385cff961593014Eli Friedmanstatic bool CheckArrow(Sema& S, QualType& ObjectType, Expr *&Base,
500891ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                   tok::TokenKind& OpKind, SourceLocation OpLoc) {
5009e0dbedfdd1f4f57cc9e8fd594385cff961593014Eli Friedman  if (Base->hasPlaceholderType()) {
5010e0dbedfdd1f4f57cc9e8fd594385cff961593014Eli Friedman    ExprResult result = S.CheckPlaceholderExpr(Base);
5011e0dbedfdd1f4f57cc9e8fd594385cff961593014Eli Friedman    if (result.isInvalid()) return true;
5012e0dbedfdd1f4f57cc9e8fd594385cff961593014Eli Friedman    Base = result.take();
5013e0dbedfdd1f4f57cc9e8fd594385cff961593014Eli Friedman  }
5014e0dbedfdd1f4f57cc9e8fd594385cff961593014Eli Friedman  ObjectType = Base->getType();
5015e0dbedfdd1f4f57cc9e8fd594385cff961593014Eli Friedman
5016b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  // C++ [expr.pseudo]p2:
5017dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   The left-hand side of the dot operator shall be of scalar type. The
5018b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  //   left-hand side of the arrow operator shall be of pointer to scalar type.
5019dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   This scalar type is the object type.
5020e0dbedfdd1f4f57cc9e8fd594385cff961593014Eli Friedman  // Note that this is rather different from the normal handling for the
5021e0dbedfdd1f4f57cc9e8fd594385cff961593014Eli Friedman  // arrow operator.
5022b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  if (OpKind == tok::arrow) {
5023b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor    if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) {
5024b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      ObjectType = Ptr->getPointeeType();
50259ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    } else if (!Base->isTypeDependent()) {
5026b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      // The user wrote "p->" when she probably meant "p."; fix it.
502791ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie      S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
5028b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor        << ObjectType << true
5029849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateReplacement(OpLoc, ".");
503091ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie      if (S.isSFINAEContext())
503191ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie        return true;
5032dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5033b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      OpKind = tok::period;
5034b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor    }
5035b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  }
5036dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
503791ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  return false;
503891ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie}
503991ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
504091ec7894ec186dd36f509682f00486c98d8228edDavid BlaikieExprResult Sema::BuildPseudoDestructorExpr(Expr *Base,
504191ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           SourceLocation OpLoc,
504291ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           tok::TokenKind OpKind,
504391ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           const CXXScopeSpec &SS,
504491ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           TypeSourceInfo *ScopeTypeInfo,
504591ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           SourceLocation CCLoc,
504691ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           SourceLocation TildeLoc,
504791ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                         PseudoDestructorTypeStorage Destructed,
504891ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           bool HasTrailingLParen) {
504991ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo();
505091ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
50518c9fe200890663e1026be25d66f0929caa87a0caEli Friedman  QualType ObjectType;
505291ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc))
505391ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    return ExprError();
505491ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
50550cb8939851364eb37a0f8bf8bb47874b7d966915Douglas Gregor  if (!ObjectType->isDependentType() && !ObjectType->isScalarType() &&
50560cb8939851364eb37a0f8bf8bb47874b7d966915Douglas Gregor      !ObjectType->isVectorType()) {
50574e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().MicrosoftMode && ObjectType->isVoidType())
50582d757ec0e6cbd9aab4bcea7a0a83d71f5080c736Nico Weber      Diag(OpLoc, diag::ext_pseudo_dtor_on_void) << Base->getSourceRange();
5059df1be86ef5f5d55fc23b2339ee76e076424d9ba0Nico Weber    else
5060df1be86ef5f5d55fc23b2339ee76e076424d9ba0Nico Weber      Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar)
5061df1be86ef5f5d55fc23b2339ee76e076424d9ba0Nico Weber        << ObjectType << Base->getSourceRange();
5062b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor    return ExprError();
5063b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  }
5064b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor
5065b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  // C++ [expr.pseudo]p2:
5066dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   [...] The cv-unqualified versions of the object type and of the type
5067b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  //   designated by the pseudo-destructor-name shall be the same type.
5068a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor  if (DestructedTypeInfo) {
5069a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor    QualType DestructedType = DestructedTypeInfo->getType();
5070a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor    SourceLocation DestructedTypeStart
5071bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara      = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin();
5072f85e193739c953358c865005855253af4f68a497John McCall    if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) {
5073f85e193739c953358c865005855253af4f68a497John McCall      if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) {
5074f85e193739c953358c865005855253af4f68a497John McCall        Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch)
5075f85e193739c953358c865005855253af4f68a497John McCall          << ObjectType << DestructedType << Base->getSourceRange()
5076f85e193739c953358c865005855253af4f68a497John McCall          << DestructedTypeInfo->getTypeLoc().getLocalSourceRange();
5077f85e193739c953358c865005855253af4f68a497John McCall
5078f85e193739c953358c865005855253af4f68a497John McCall        // Recover by setting the destructed type to the object type.
5079f85e193739c953358c865005855253af4f68a497John McCall        DestructedType = ObjectType;
5080f85e193739c953358c865005855253af4f68a497John McCall        DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType,
5081a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor                                                           DestructedTypeStart);
5082f85e193739c953358c865005855253af4f68a497John McCall        Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo);
5083f85e193739c953358c865005855253af4f68a497John McCall      } else if (DestructedType.getObjCLifetime() !=
5084f85e193739c953358c865005855253af4f68a497John McCall                                                ObjectType.getObjCLifetime()) {
5085f85e193739c953358c865005855253af4f68a497John McCall
5086f85e193739c953358c865005855253af4f68a497John McCall        if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) {
5087f85e193739c953358c865005855253af4f68a497John McCall          // Okay: just pretend that the user provided the correctly-qualified
5088f85e193739c953358c865005855253af4f68a497John McCall          // type.
5089f85e193739c953358c865005855253af4f68a497John McCall        } else {
5090f85e193739c953358c865005855253af4f68a497John McCall          Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals)
5091f85e193739c953358c865005855253af4f68a497John McCall            << ObjectType << DestructedType << Base->getSourceRange()
5092f85e193739c953358c865005855253af4f68a497John McCall            << DestructedTypeInfo->getTypeLoc().getLocalSourceRange();
5093f85e193739c953358c865005855253af4f68a497John McCall        }
5094f85e193739c953358c865005855253af4f68a497John McCall
5095f85e193739c953358c865005855253af4f68a497John McCall        // Recover by setting the destructed type to the object type.
5096f85e193739c953358c865005855253af4f68a497John McCall        DestructedType = ObjectType;
5097f85e193739c953358c865005855253af4f68a497John McCall        DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType,
5098f85e193739c953358c865005855253af4f68a497John McCall                                                           DestructedTypeStart);
5099f85e193739c953358c865005855253af4f68a497John McCall        Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo);
5100f85e193739c953358c865005855253af4f68a497John McCall      }
5101a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor    }
5102b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  }
5103dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5104b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  // C++ [expr.pseudo]p2:
5105b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  //   [...] Furthermore, the two type-names in a pseudo-destructor-name of the
5106b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  //   form
5107b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  //
5108dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //     ::[opt] nested-name-specifier[opt] type-name :: ~ type-name
5109b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  //
5110b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  //   shall designate the same scalar type.
5111b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  if (ScopeTypeInfo) {
5112b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor    QualType ScopeType = ScopeTypeInfo->getType();
5113b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor    if (!ScopeType->isDependentType() && !ObjectType->isDependentType() &&
511481e317a444dd756a1cafe94031e4b3f3c138dac6John McCall        !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) {
5115dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5116bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara      Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(),
5117b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor           diag::err_pseudo_dtor_type_mismatch)
51189ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        << ObjectType << ScopeType << Base->getSourceRange()
5119bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara        << ScopeTypeInfo->getTypeLoc().getLocalSourceRange();
5120dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5121b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      ScopeType = QualType();
5122b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      ScopeTypeInfo = 0;
5123b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor    }
5124b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  }
5125dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
51269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Expr *Result
51279ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    = new (Context) CXXPseudoDestructorExpr(Context, Base,
51289ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            OpKind == tok::arrow, OpLoc,
5129f3db29fff6a583ecda823cf909ab7737d8d30129Douglas Gregor                                            SS.getWithLocInContext(Context),
51309ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            ScopeTypeInfo,
51319ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            CCLoc,
51329ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            TildeLoc,
51339ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            Destructed);
5134dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5135b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  if (HasTrailingLParen)
51369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return Owned(Result);
5137dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
51389ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return DiagnoseDtorReference(Destructed.getLocation(), Result);
513977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor}
514077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
514160d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base,
51422d9f5fa1bcbde31b83c79027c3f1da2d64e3b1feJohn McCall                                           SourceLocation OpLoc,
51432d9f5fa1bcbde31b83c79027c3f1da2d64e3b1feJohn McCall                                           tok::TokenKind OpKind,
51442d9f5fa1bcbde31b83c79027c3f1da2d64e3b1feJohn McCall                                           CXXScopeSpec &SS,
51452d9f5fa1bcbde31b83c79027c3f1da2d64e3b1feJohn McCall                                           UnqualifiedId &FirstTypeName,
51462d9f5fa1bcbde31b83c79027c3f1da2d64e3b1feJohn McCall                                           SourceLocation CCLoc,
51472d9f5fa1bcbde31b83c79027c3f1da2d64e3b1feJohn McCall                                           SourceLocation TildeLoc,
51482d9f5fa1bcbde31b83c79027c3f1da2d64e3b1feJohn McCall                                           UnqualifiedId &SecondTypeName,
51492d9f5fa1bcbde31b83c79027c3f1da2d64e3b1feJohn McCall                                           bool HasTrailingLParen) {
515077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId ||
515177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor          FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) &&
515277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor         "Invalid first type name in pseudo-destructor");
515377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId ||
515477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor          SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) &&
515577549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor         "Invalid second type name in pseudo-destructor");
515677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor
51578c9fe200890663e1026be25d66f0929caa87a0caEli Friedman  QualType ObjectType;
515891ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc))
515991ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    return ExprError();
5160a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor
5161a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor  // Compute the object type that we should use for name lookup purposes. Only
5162a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor  // record types and dependent types matter.
5163b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType ObjectTypePtrForLookup;
5164a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor  if (!SS.isSet()) {
51652d9f5fa1bcbde31b83c79027c3f1da2d64e3b1feJohn McCall    if (ObjectType->isRecordType())
51662d9f5fa1bcbde31b83c79027c3f1da2d64e3b1feJohn McCall      ObjectTypePtrForLookup = ParsedType::make(ObjectType);
5167b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    else if (ObjectType->isDependentType())
5168b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy);
5169a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor  }
5170dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5171dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // Convert the name of the type being destructed (following the ~) into a
5172b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  // type (with source-location information).
517377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  QualType DestructedType;
517477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  TypeSourceInfo *DestructedTypeInfo = 0;
5175a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor  PseudoDestructorTypeStorage Destructed;
517677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) {
5177dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ParsedType T = getTypeName(*SecondTypeName.Identifier,
5178b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                               SecondTypeName.StartLocation,
51791e52dfc648ce0b25ef57ae29ef1b4337d80011efFariborz Jahanian                               S, &SS, true, false, ObjectTypePtrForLookup);
5180dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (!T &&
5181a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor        ((SS.isSet() && !computeDeclContext(SS, false)) ||
5182a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor         (!SS.isSet() && ObjectType->isDependentType()))) {
5183dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // The name of the type being destroyed is a dependent name, and we
5184a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor      // couldn't find anything useful in scope. Just store the identifier and
5185a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor      // it's location, and we'll perform (qualified) name lookup again at
5186a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor      // template instantiation time.
5187a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor      Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier,
5188a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor                                               SecondTypeName.StartLocation);
5189a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor    } else if (!T) {
5190dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Diag(SecondTypeName.StartLocation,
519177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor           diag::err_pseudo_dtor_destructor_non_type)
519277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor        << SecondTypeName.Identifier << ObjectType;
519377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      if (isSFINAEContext())
519477549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor        return ExprError();
5195dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
519677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      // Recover by assuming we had the right type all along.
519777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      DestructedType = ObjectType;
5198b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor    } else
519977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      DestructedType = GetTypeFromParser(T, &DestructedTypeInfo);
520077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  } else {
5201b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor    // Resolve the template-id to a type.
520277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId;
52035354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer    ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
5204b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                       TemplateId->NumArgs);
5205059101f922de6eb765601459925f4c8914420b23Douglas Gregor    TypeResult T = ActOnTemplateIdType(TemplateId->SS,
520655d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara                                       TemplateId->TemplateKWLoc,
5207059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                       TemplateId->Template,
5208b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                       TemplateId->TemplateNameLoc,
5209b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                       TemplateId->LAngleLoc,
5210b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                       TemplateArgsPtr,
5211b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                       TemplateId->RAngleLoc);
5212b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor    if (T.isInvalid() || !T.get()) {
5213b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      // Recover by assuming we had the right type all along.
5214b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      DestructedType = ObjectType;
5215b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor    } else
5216b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo);
521777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  }
5218dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5219dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // If we've performed some kind of recovery, (re-)build the type source
5220b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  // information.
5221a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor  if (!DestructedType.isNull()) {
5222a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor    if (!DestructedTypeInfo)
5223a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor      DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType,
5224b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                                  SecondTypeName.StartLocation);
5225a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor    Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo);
5226a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor  }
5227dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5228b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  // Convert the name of the scope type (the type prior to '::') into a type.
5229b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor  TypeSourceInfo *ScopeTypeInfo = 0;
523077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  QualType ScopeType;
5231dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId ||
523277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      FirstTypeName.Identifier) {
523377549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) {
5234dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ParsedType T = getTypeName(*FirstTypeName.Identifier,
5235b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                 FirstTypeName.StartLocation,
5236f3db29fff6a583ecda823cf909ab7737d8d30129Douglas Gregor                                 S, &SS, true, false, ObjectTypePtrForLookup);
523777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      if (!T) {
5238dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        Diag(FirstTypeName.StartLocation,
523977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor             diag::err_pseudo_dtor_destructor_non_type)
524077549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor          << FirstTypeName.Identifier << ObjectType;
5241dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
524277549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor        if (isSFINAEContext())
5243b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor          return ExprError();
5244dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5245b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor        // Just drop this type. It's unnecessary anyway.
5246b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor        ScopeType = QualType();
5247b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      } else
5248b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor        ScopeType = GetTypeFromParser(T, &ScopeTypeInfo);
524977549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    } else {
5250b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      // Resolve the template-id to a type.
525177549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor      TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId;
52525354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer      ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
5253b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                         TemplateId->NumArgs);
5254059101f922de6eb765601459925f4c8914420b23Douglas Gregor      TypeResult T = ActOnTemplateIdType(TemplateId->SS,
525555d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara                                         TemplateId->TemplateKWLoc,
5256059101f922de6eb765601459925f4c8914420b23Douglas Gregor                                         TemplateId->Template,
5257b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                         TemplateId->TemplateNameLoc,
5258b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                         TemplateId->LAngleLoc,
5259b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                         TemplateArgsPtr,
5260b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor                                         TemplateId->RAngleLoc);
5261b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      if (T.isInvalid() || !T.get()) {
5262b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor        // Recover by dropping this type.
5263b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor        ScopeType = QualType();
5264b57fb49b6569db9716877de6857e4de572edfb75Douglas Gregor      } else
5265dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo);
526677549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor    }
526777549080fb7b9af31606b3c1b4830a94429fb1fdDouglas Gregor  }
5268dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5269b4a418fd23b6b66dff5379fecc50c09abe368eddDouglas Gregor  if (!ScopeType.isNull() && !ScopeTypeInfo)
5270b4a418fd23b6b66dff5379fecc50c09abe368eddDouglas Gregor    ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType,
5271b4a418fd23b6b66dff5379fecc50c09abe368eddDouglas Gregor                                                  FirstTypeName.StartLocation);
5272b4a418fd23b6b66dff5379fecc50c09abe368eddDouglas Gregor
5273dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
52749ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS,
5275fce46ee68f779e239826e69e45d01d4c8e5323caDouglas Gregor                                   ScopeTypeInfo, CCLoc, TildeLoc,
5276a2e7dd2f4a50d835351153aee568d35ccc986310Douglas Gregor                                   Destructed, HasTrailingLParen);
5277d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor}
5278d4dca08d6b7ed2e3e3718caa6fd735960b135e9aDouglas Gregor
527991ec7894ec186dd36f509682f00486c98d8228edDavid BlaikieExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base,
528091ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           SourceLocation OpLoc,
528191ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           tok::TokenKind OpKind,
528291ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           SourceLocation TildeLoc,
528391ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           const DeclSpec& DS,
528491ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                           bool HasTrailingLParen) {
52858c9fe200890663e1026be25d66f0929caa87a0caEli Friedman  QualType ObjectType;
528691ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc))
528791ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie    return ExprError();
528891ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
528991ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
529091ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
529191ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  TypeLocBuilder TLB;
529291ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
529391ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
529491ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  TypeSourceInfo *DestructedTypeInfo = TLB.getTypeSourceInfo(Context, T);
529591ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  PseudoDestructorTypeStorage Destructed(DestructedTypeInfo);
529691ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
529791ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie  return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, CXXScopeSpec(),
529891ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                   0, SourceLocation(), TildeLoc,
529991ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie                                   Destructed, HasTrailingLParen);
530091ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie}
530191ec7894ec186dd36f509682f00486c98d8228edDavid Blaikie
5302429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn WiegleyExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl,
53033f01c8a58b7b2d78303675182a8de6b7fbe45ae1Eli Friedman                                        CXXConversionDecl *Method,
53047cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                        bool HadMultipleCandidates) {
530523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman  if (Method->getParent()->isLambda() &&
530623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      Method->getConversionType()->isBlockPointerType()) {
530723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    // This is a lambda coversion to block pointer; check if the argument
530823f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    // is a LambdaExpr.
530923f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    Expr *SubE = E;
531023f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    CastExpr *CE = dyn_cast<CastExpr>(SubE);
531123f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    if (CE && CE->getCastKind() == CK_NoOp)
531223f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      SubE = CE->getSubExpr();
531323f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    SubE = SubE->IgnoreParens();
531423f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    if (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(SubE))
531523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      SubE = BE->getSubExpr();
531623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    if (isa<LambdaExpr>(SubE)) {
531723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      // For the conversion to block pointer on a lambda expression, we
531823f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      // construct a special BlockLiteral instead; this doesn't really make
531923f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      // a difference in ARC, but outside of ARC the resulting block literal
532023f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      // follows the normal lifetime rules for block literals instead of being
532123f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      // autoreleased.
532223f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      DiagnosticErrorTrap Trap(Diags);
532323f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      ExprResult Exp = BuildBlockForLambdaConversion(E->getExprLoc(),
532423f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman                                                     E->getExprLoc(),
532523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman                                                     Method, E);
532623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      if (Exp.isInvalid())
532723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman        Diag(E->getExprLoc(), diag::note_lambda_to_block_conv);
532823f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman      return Exp;
532923f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    }
533023f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman  }
533123f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman
533223f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman
5333429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/0,
5334429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                          FoundDecl, Method);
5335429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (Exp.isInvalid())
5336f2ae52605a49e5fc7a581f2c1ae02f1811034578Douglas Gregor    return true;
5337772fffa2349524dbcad59dbfec7b71a47be229e6Eli Friedman
5338dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  MemberExpr *ME =
5339429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      new (Context) MemberExpr(Exp.take(), /*IsArrow=*/false, Method,
5340960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara                               SourceLocation(), Context.BoundMemberTy,
5341f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                               VK_RValue, OK_Ordinary);
53427cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara  if (HadMultipleCandidates)
53437cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara    ME->setHadMultipleCandidates(true);
53447cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara
5345f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  QualType ResultType = Method->getResultType();
5346f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  ExprValueKind VK = Expr::getValueKindForType(ResultType);
5347f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  ResultType = ResultType.getNonLValueExprType(Context);
5348f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
53495f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  MarkFunctionReferenced(Exp.get()->getLocStart(), Method);
53507edfb69ae192d9c1f5b1f32af30130f34f98386eDouglas Gregor  CXXMemberCallExpr *CE =
53513b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer    new (Context) CXXMemberCallExpr(Context, ME, MultiExprArg(), ResultType, VK,
5352429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                    Exp.get()->getLocEnd());
5353b74002395520cff5933e16e06ba3f470b627bfb2Fariborz Jahanian  return CE;
5354b74002395520cff5933e16e06ba3f470b627bfb2Fariborz Jahanian}
5355b74002395520cff5933e16e06ba3f470b627bfb2Fariborz Jahanian
53562e156225a29407a50dd19041aa5750171ad44ea3Sebastian RedlExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand,
53572e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl                                      SourceLocation RParen) {
5358e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  CanThrowResult CanThrow = canThrow(Operand);
53592e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl  return Owned(new (Context) CXXNoexceptExpr(Context.BoolTy, Operand,
5360e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             CanThrow, KeyLoc, RParen));
53612e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl}
53622e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl
53632e156225a29407a50dd19041aa5750171ad44ea3Sebastian RedlExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation,
53642e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl                                   Expr *Operand, SourceLocation RParen) {
53652e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl  return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen);
536602bc21a88ecbdf49b2e674c210a4cbf8c48c6e58Sebastian Redl}
536702bc21a88ecbdf49b2e674c210a4cbf8c48c6e58Sebastian Redl
5368e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedmanstatic bool IsSpecialDiscardedValue(Expr *E) {
5369e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  // In C++11, discarded-value expressions of a certain form are special,
5370e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  // according to [expr]p10:
5371e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  //   The lvalue-to-rvalue conversion (4.1) is applied only if the
5372e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  //   expression is an lvalue of volatile-qualified type and it has
5373e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  //   one of the following forms:
5374e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  E = E->IgnoreParens();
5375e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman
53760218068cbbb2e98164431653d76587e5b8bb9c38Eli Friedman  //   - id-expression (5.1.1),
5377e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  if (isa<DeclRefExpr>(E))
5378e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    return true;
5379e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman
53800218068cbbb2e98164431653d76587e5b8bb9c38Eli Friedman  //   - subscripting (5.2.1),
5381e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  if (isa<ArraySubscriptExpr>(E))
5382e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    return true;
5383e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman
53840218068cbbb2e98164431653d76587e5b8bb9c38Eli Friedman  //   - class member access (5.2.5),
5385e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  if (isa<MemberExpr>(E))
5386e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    return true;
5387e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman
53880218068cbbb2e98164431653d76587e5b8bb9c38Eli Friedman  //   - indirection (5.3.1),
5389e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
5390e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    if (UO->getOpcode() == UO_Deref)
5391e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman      return true;
5392e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman
5393e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
53940218068cbbb2e98164431653d76587e5b8bb9c38Eli Friedman    //   - pointer-to-member operation (5.5),
5395e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    if (BO->isPtrMemOp())
5396e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman      return true;
5397e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman
53980218068cbbb2e98164431653d76587e5b8bb9c38Eli Friedman    //   - comma expression (5.18) where the right operand is one of the above.
5399e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    if (BO->getOpcode() == BO_Comma)
5400e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman      return IsSpecialDiscardedValue(BO->getRHS());
5401e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  }
5402e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman
54030218068cbbb2e98164431653d76587e5b8bb9c38Eli Friedman  //   - conditional expression (5.16) where both the second and the third
5404e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  //     operands are one of the above, or
5405e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
5406e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    return IsSpecialDiscardedValue(CO->getTrueExpr()) &&
5407e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman           IsSpecialDiscardedValue(CO->getFalseExpr());
5408e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  // The related edge case of "*x ?: *x".
5409e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  if (BinaryConditionalOperator *BCO =
5410e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman          dyn_cast<BinaryConditionalOperator>(E)) {
5411e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(BCO->getTrueExpr()))
5412e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman      return IsSpecialDiscardedValue(OVE->getSourceExpr()) &&
5413e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman             IsSpecialDiscardedValue(BCO->getFalseExpr());
5414e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  }
5415e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman
5416e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  // Objective-C++ extensions to the rule.
5417e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  if (isa<PseudoObjectExpr>(E) || isa<ObjCIvarRefExpr>(E))
5418e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    return true;
5419e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman
5420e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  return false;
5421e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman}
5422e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman
5423f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// Perform the conversions required for an expression used in a
5424f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// context that ignores the result.
5425429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn WiegleyExprResult Sema::IgnoredValueConversions(Expr *E) {
54263c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  if (E->hasPlaceholderType()) {
54273c3b7f90a863af43fa63043d396553ecf205351cJohn McCall    ExprResult result = CheckPlaceholderExpr(E);
54283c3b7f90a863af43fa63043d396553ecf205351cJohn McCall    if (result.isInvalid()) return Owned(E);
54293c3b7f90a863af43fa63043d396553ecf205351cJohn McCall    E = result.take();
54303c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  }
54313c3b7f90a863af43fa63043d396553ecf205351cJohn McCall
5432a878cda355074d7d50211be49e7871840a55a5d9John McCall  // C99 6.3.2.1:
5433a878cda355074d7d50211be49e7871840a55a5d9John McCall  //   [Except in specific positions,] an lvalue that does not have
5434a878cda355074d7d50211be49e7871840a55a5d9John McCall  //   array type is converted to the value stored in the
5435a878cda355074d7d50211be49e7871840a55a5d9John McCall  //   designated object (and is no longer an lvalue).
5436e6d134b244cd666c47a3d2dd78f82ff0824188bdJohn McCall  if (E->isRValue()) {
5437e6d134b244cd666c47a3d2dd78f82ff0824188bdJohn McCall    // In C, function designators (i.e. expressions of function type)
5438e6d134b244cd666c47a3d2dd78f82ff0824188bdJohn McCall    // are r-values, but we still want to do function-to-pointer decay
5439e6d134b244cd666c47a3d2dd78f82ff0824188bdJohn McCall    // on them.  This is both technically correct and convenient for
5440e6d134b244cd666c47a3d2dd78f82ff0824188bdJohn McCall    // some clients.
54414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus && E->getType()->isFunctionType())
5442e6d134b244cd666c47a3d2dd78f82ff0824188bdJohn McCall      return DefaultFunctionArrayConversion(E);
5443e6d134b244cd666c47a3d2dd78f82ff0824188bdJohn McCall
5444e6d134b244cd666c47a3d2dd78f82ff0824188bdJohn McCall    return Owned(E);
5445e6d134b244cd666c47a3d2dd78f82ff0824188bdJohn McCall  }
5446f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
5447e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  if (getLangOpts().CPlusPlus)  {
5448e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    // The C++11 standard defines the notion of a discarded-value expression;
5449e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    // normally, we don't need to do anything to handle it, but if it is a
5450e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    // volatile lvalue with a special form, we perform an lvalue-to-rvalue
5451e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    // conversion.
5452e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    if (getLangOpts().CPlusPlus0x && E->isGLValue() &&
5453e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman        E->getType().isVolatileQualified() &&
5454e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman        IsSpecialDiscardedValue(E)) {
5455e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman      ExprResult Res = DefaultLvalueConversion(E);
5456e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman      if (Res.isInvalid())
5457e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman        return Owned(E);
5458e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman      E = Res.take();
5459e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    }
5460e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman    return Owned(E);
5461e26073c85259be1c2f397797cc59286ac85fb3b8Eli Friedman  }
5462f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
5463f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  // GCC seems to also exclude expressions of incomplete enum type.
5464f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  if (const EnumType *T = E->getType()->getAs<EnumType>()) {
5465f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    if (!T->getDecl()->isComplete()) {
5466f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      // FIXME: stupid workaround for a codegen bug!
5467429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).take();
5468429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return Owned(E);
5469f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall    }
5470f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
5471f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
5472429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  ExprResult Res = DefaultFunctionArrayLvalueConversion(E);
5473429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (Res.isInvalid())
5474429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return Owned(E);
5475429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  E = Res.take();
5476429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
547785515d64c15838eadb3ffe82b635f8217b04be8aJohn McCall  if (!E->getType()->isVoidType())
547885515d64c15838eadb3ffe82b635f8217b04be8aJohn McCall    RequireCompleteType(E->getExprLoc(), E->getType(),
547985515d64c15838eadb3ffe82b635f8217b04be8aJohn McCall                        diag::err_incomplete_type);
5480429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  return Owned(E);
5481f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
5482f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
5483def07624ecc535431e0c814b4b5b842de8a06997David BlaikieExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC) {
5484429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  ExprResult FullExpr = Owned(FE);
5485429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
5486429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (!FullExpr.get())
5487bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    return ExprError();
5488a878cda355074d7d50211be49e7871840a55a5d9John McCall
5489429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (DiagnoseUnexpandedParameterPack(FullExpr.get()))
5490d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    return ExprError();
5491d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
54925e3a8bea1cb3a8508a99982278934df32ccc7387Douglas Gregor  // Top-level message sends default to 'id' when we're in a debugger.
54934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().DebuggerCastResultToId &&
54945e3a8bea1cb3a8508a99982278934df32ccc7387Douglas Gregor      FullExpr.get()->getType() == Context.UnknownAnyTy &&
54955e3a8bea1cb3a8508a99982278934df32ccc7387Douglas Gregor      isa<ObjCMessageExpr>(FullExpr.get())) {
54965e3a8bea1cb3a8508a99982278934df32ccc7387Douglas Gregor    FullExpr = forceUnknownAnyToType(FullExpr.take(), Context.getObjCIdType());
54975e3a8bea1cb3a8508a99982278934df32ccc7387Douglas Gregor    if (FullExpr.isInvalid())
54985e3a8bea1cb3a8508a99982278934df32ccc7387Douglas Gregor      return ExprError();
54995e3a8bea1cb3a8508a99982278934df32ccc7387Douglas Gregor  }
55005e3a8bea1cb3a8508a99982278934df32ccc7387Douglas Gregor
5501fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  FullExpr = CheckPlaceholderExpr(FullExpr.take());
5502fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  if (FullExpr.isInvalid())
5503fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall    return ExprError();
5504353ee246e754e38db9b738240d18f1ecf2bb389bDouglas Gregor
5505429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  FullExpr = IgnoredValueConversions(FullExpr.take());
5506429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (FullExpr.isInvalid())
5507429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return ExprError();
5508429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
5509def07624ecc535431e0c814b4b5b842de8a06997David Blaikie  CheckImplicitConversions(FullExpr.get(), CC);
55104765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  return MaybeCreateExprWithCleanups(FullExpr);
5511165a0a07c0a91f8d61ee3737b62e7f376bb7e1c7Anders Carlsson}
5512bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis
5513bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios KyrtzidisStmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) {
5514bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis  if (!FullStmt) return StmtError();
5515bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis
55164765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  return MaybeCreateStmtWithCleanups(FullStmt);
5517bf8cafadb9d4e0d7a90fe78fc175efb80ae34d42Argyrios Kyrtzidis}
55181e862693c63067ae467b0b3884c44f753cd6e821Francois Pichet
5519ba0513de93d2fab6db5ab30b6927209fcc883078Douglas GregorSema::IfExistsResult
5520ba0513de93d2fab6db5ab30b6927209fcc883078Douglas GregorSema::CheckMicrosoftIfExistsSymbol(Scope *S,
5521ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor                                   CXXScopeSpec &SS,
5522ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor                                   const DeclarationNameInfo &TargetNameInfo) {
55231e862693c63067ae467b0b3884c44f753cd6e821Francois Pichet  DeclarationName TargetName = TargetNameInfo.getName();
55241e862693c63067ae467b0b3884c44f753cd6e821Francois Pichet  if (!TargetName)
55253896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    return IER_DoesNotExist;
5526ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor
55273896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  // If the name itself is dependent, then the result is dependent.
55283896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  if (TargetName.isDependentName())
55293896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    return IER_Dependent;
5530ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor
55311e862693c63067ae467b0b3884c44f753cd6e821Francois Pichet  // Do the redeclaration lookup in the current scope.
55321e862693c63067ae467b0b3884c44f753cd6e821Francois Pichet  LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName,
55331e862693c63067ae467b0b3884c44f753cd6e821Francois Pichet                 Sema::NotForRedeclaration);
55343896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  LookupParsedName(R, S, &SS);
55351e862693c63067ae467b0b3884c44f753cd6e821Francois Pichet  R.suppressDiagnostics();
55363896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
55373896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  switch (R.getResultKind()) {
55383896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case LookupResult::Found:
55393896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case LookupResult::FoundOverloaded:
55403896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case LookupResult::FoundUnresolvedValue:
55413896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case LookupResult::Ambiguous:
55423896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    return IER_Exists;
55433896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
55443896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case LookupResult::NotFound:
55453896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    return IER_DoesNotExist;
55463896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor
55473896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  case LookupResult::NotFoundInCurrentInstantiation:
55483896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor    return IER_Dependent;
55493896fc5d4daaf003e451e797e37de57dd8cf9cd5Douglas Gregor  }
55507530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie
55517530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid LookupResult Kind!");
55521e862693c63067ae467b0b3884c44f753cd6e821Francois Pichet}
5553ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor
555465019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas GregorSema::IfExistsResult
555565019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas GregorSema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc,
555665019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor                                   bool IsIfExists, CXXScopeSpec &SS,
555765019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor                                   UnqualifiedId &Name) {
5558ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor  DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name);
555965019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor
556065019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  // Check for unexpanded parameter packs.
556165019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  SmallVector<UnexpandedParameterPack, 4> Unexpanded;
556265019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  collectUnexpandedParameterPacks(SS, Unexpanded);
556365019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  collectUnexpandedParameterPacks(TargetNameInfo, Unexpanded);
556465019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  if (!Unexpanded.empty()) {
556565019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor    DiagnoseUnexpandedParameterPacks(KeywordLoc,
556665019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor                                     IsIfExists? UPPC_IfExists
556765019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor                                               : UPPC_IfNotExists,
556865019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor                                     Unexpanded);
556965019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor    return IER_Error;
557065019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor  }
557165019acfc46ffb191fac4e781ac0c4b8d0c8434eDouglas Gregor
5572ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor  return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo);
5573ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor}
5574