1f4a2713aSLionel Sambuc //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc ///
10f4a2713aSLionel Sambuc /// \file
11f4a2713aSLionel Sambuc /// \brief Implements semantic analysis for C++ expressions.
12f4a2713aSLionel Sambuc ///
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
16*0a6a1f1dSLionel Sambuc #include "TreeTransform.h"
17f4a2713aSLionel Sambuc #include "TypeLocBuilder.h"
18f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
19*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTLambda.h"
20f4a2713aSLionel Sambuc #include "clang/AST/CXXInheritance.h"
21f4a2713aSLionel Sambuc #include "clang/AST/CharUnits.h"
22f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
23f4a2713aSLionel Sambuc #include "clang/AST/EvaluatedExprVisitor.h"
24f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
25f4a2713aSLionel Sambuc #include "clang/AST/ExprObjC.h"
26f4a2713aSLionel Sambuc #include "clang/AST/RecursiveASTVisitor.h"
27f4a2713aSLionel Sambuc #include "clang/AST/TypeLoc.h"
28f4a2713aSLionel Sambuc #include "clang/Basic/PartialDiagnostic.h"
29f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
30f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
31f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
32f4a2713aSLionel Sambuc #include "clang/Sema/Initialization.h"
33f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
34f4a2713aSLionel Sambuc #include "clang/Sema/ParsedTemplate.h"
35f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
36f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h"
37f4a2713aSLionel Sambuc #include "clang/Sema/SemaLambda.h"
38f4a2713aSLionel Sambuc #include "clang/Sema/TemplateDeduction.h"
39f4a2713aSLionel Sambuc #include "llvm/ADT/APInt.h"
40f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
41f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
42f4a2713aSLionel Sambuc using namespace clang;
43f4a2713aSLionel Sambuc using namespace sema;
44f4a2713aSLionel Sambuc
45f4a2713aSLionel Sambuc /// \brief Handle the result of the special case name lookup for inheriting
46f4a2713aSLionel Sambuc /// constructor declarations. 'NS::X::X' and 'NS::X<...>::X' are treated as
47f4a2713aSLionel Sambuc /// constructor names in member using declarations, even if 'X' is not the
48f4a2713aSLionel Sambuc /// name of the corresponding type.
getInheritingConstructorName(CXXScopeSpec & SS,SourceLocation NameLoc,IdentifierInfo & Name)49f4a2713aSLionel Sambuc ParsedType Sema::getInheritingConstructorName(CXXScopeSpec &SS,
50f4a2713aSLionel Sambuc SourceLocation NameLoc,
51f4a2713aSLionel Sambuc IdentifierInfo &Name) {
52f4a2713aSLionel Sambuc NestedNameSpecifier *NNS = SS.getScopeRep();
53f4a2713aSLionel Sambuc
54f4a2713aSLionel Sambuc // Convert the nested-name-specifier into a type.
55f4a2713aSLionel Sambuc QualType Type;
56f4a2713aSLionel Sambuc switch (NNS->getKind()) {
57f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpec:
58f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpecWithTemplate:
59f4a2713aSLionel Sambuc Type = QualType(NNS->getAsType(), 0);
60f4a2713aSLionel Sambuc break;
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc case NestedNameSpecifier::Identifier:
63f4a2713aSLionel Sambuc // Strip off the last layer of the nested-name-specifier and build a
64f4a2713aSLionel Sambuc // typename type for it.
65f4a2713aSLionel Sambuc assert(NNS->getAsIdentifier() == &Name && "not a constructor name");
66f4a2713aSLionel Sambuc Type = Context.getDependentNameType(ETK_None, NNS->getPrefix(),
67f4a2713aSLionel Sambuc NNS->getAsIdentifier());
68f4a2713aSLionel Sambuc break;
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc case NestedNameSpecifier::Global:
71*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Super:
72f4a2713aSLionel Sambuc case NestedNameSpecifier::Namespace:
73f4a2713aSLionel Sambuc case NestedNameSpecifier::NamespaceAlias:
74f4a2713aSLionel Sambuc llvm_unreachable("Nested name specifier is not a type for inheriting ctor");
75f4a2713aSLionel Sambuc }
76f4a2713aSLionel Sambuc
77f4a2713aSLionel Sambuc // This reference to the type is located entirely at the location of the
78f4a2713aSLionel Sambuc // final identifier in the qualified-id.
79f4a2713aSLionel Sambuc return CreateParsedType(Type,
80f4a2713aSLionel Sambuc Context.getTrivialTypeSourceInfo(Type, NameLoc));
81f4a2713aSLionel Sambuc }
82f4a2713aSLionel Sambuc
getDestructorName(SourceLocation TildeLoc,IdentifierInfo & II,SourceLocation NameLoc,Scope * S,CXXScopeSpec & SS,ParsedType ObjectTypePtr,bool EnteringContext)83f4a2713aSLionel Sambuc ParsedType Sema::getDestructorName(SourceLocation TildeLoc,
84f4a2713aSLionel Sambuc IdentifierInfo &II,
85f4a2713aSLionel Sambuc SourceLocation NameLoc,
86f4a2713aSLionel Sambuc Scope *S, CXXScopeSpec &SS,
87f4a2713aSLionel Sambuc ParsedType ObjectTypePtr,
88f4a2713aSLionel Sambuc bool EnteringContext) {
89f4a2713aSLionel Sambuc // Determine where to perform name lookup.
90f4a2713aSLionel Sambuc
91f4a2713aSLionel Sambuc // FIXME: This area of the standard is very messy, and the current
92f4a2713aSLionel Sambuc // wording is rather unclear about which scopes we search for the
93f4a2713aSLionel Sambuc // destructor name; see core issues 399 and 555. Issue 399 in
94f4a2713aSLionel Sambuc // particular shows where the current description of destructor name
95f4a2713aSLionel Sambuc // lookup is completely out of line with existing practice, e.g.,
96f4a2713aSLionel Sambuc // this appears to be ill-formed:
97f4a2713aSLionel Sambuc //
98f4a2713aSLionel Sambuc // namespace N {
99f4a2713aSLionel Sambuc // template <typename T> struct S {
100f4a2713aSLionel Sambuc // ~S();
101f4a2713aSLionel Sambuc // };
102f4a2713aSLionel Sambuc // }
103f4a2713aSLionel Sambuc //
104f4a2713aSLionel Sambuc // void f(N::S<int>* s) {
105f4a2713aSLionel Sambuc // s->N::S<int>::~S();
106f4a2713aSLionel Sambuc // }
107f4a2713aSLionel Sambuc //
108f4a2713aSLionel Sambuc // See also PR6358 and PR6359.
109f4a2713aSLionel Sambuc // For this reason, we're currently only doing the C++03 version of this
110f4a2713aSLionel Sambuc // code; the C++0x version has to wait until we get a proper spec.
111f4a2713aSLionel Sambuc QualType SearchType;
112*0a6a1f1dSLionel Sambuc DeclContext *LookupCtx = nullptr;
113f4a2713aSLionel Sambuc bool isDependent = false;
114f4a2713aSLionel Sambuc bool LookInScope = false;
115f4a2713aSLionel Sambuc
116f4a2713aSLionel Sambuc // If we have an object type, it's because we are in a
117f4a2713aSLionel Sambuc // pseudo-destructor-expression or a member access expression, and
118f4a2713aSLionel Sambuc // we know what type we're looking for.
119f4a2713aSLionel Sambuc if (ObjectTypePtr)
120f4a2713aSLionel Sambuc SearchType = GetTypeFromParser(ObjectTypePtr);
121f4a2713aSLionel Sambuc
122f4a2713aSLionel Sambuc if (SS.isSet()) {
123*0a6a1f1dSLionel Sambuc NestedNameSpecifier *NNS = SS.getScopeRep();
124f4a2713aSLionel Sambuc
125f4a2713aSLionel Sambuc bool AlreadySearched = false;
126f4a2713aSLionel Sambuc bool LookAtPrefix = true;
127*0a6a1f1dSLionel Sambuc // C++11 [basic.lookup.qual]p6:
128f4a2713aSLionel Sambuc // If a pseudo-destructor-name (5.2.4) contains a nested-name-specifier,
129f4a2713aSLionel Sambuc // the type-names are looked up as types in the scope designated by the
130*0a6a1f1dSLionel Sambuc // nested-name-specifier. Similarly, in a qualified-id of the form:
131f4a2713aSLionel Sambuc //
132*0a6a1f1dSLionel Sambuc // nested-name-specifier[opt] class-name :: ~ class-name
133f4a2713aSLionel Sambuc //
134*0a6a1f1dSLionel Sambuc // the second class-name is looked up in the same scope as the first.
135f4a2713aSLionel Sambuc //
136*0a6a1f1dSLionel Sambuc // Here, we determine whether the code below is permitted to look at the
137*0a6a1f1dSLionel Sambuc // prefix of the nested-name-specifier.
138f4a2713aSLionel Sambuc DeclContext *DC = computeDeclContext(SS, EnteringContext);
139f4a2713aSLionel Sambuc if (DC && DC->isFileContext()) {
140f4a2713aSLionel Sambuc AlreadySearched = true;
141f4a2713aSLionel Sambuc LookupCtx = DC;
142f4a2713aSLionel Sambuc isDependent = false;
143*0a6a1f1dSLionel Sambuc } else if (DC && isa<CXXRecordDecl>(DC)) {
144f4a2713aSLionel Sambuc LookAtPrefix = false;
145*0a6a1f1dSLionel Sambuc LookInScope = true;
146*0a6a1f1dSLionel Sambuc }
147f4a2713aSLionel Sambuc
148f4a2713aSLionel Sambuc // The second case from the C++03 rules quoted further above.
149*0a6a1f1dSLionel Sambuc NestedNameSpecifier *Prefix = nullptr;
150f4a2713aSLionel Sambuc if (AlreadySearched) {
151f4a2713aSLionel Sambuc // Nothing left to do.
152f4a2713aSLionel Sambuc } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) {
153f4a2713aSLionel Sambuc CXXScopeSpec PrefixSS;
154f4a2713aSLionel Sambuc PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data()));
155f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(PrefixSS, EnteringContext);
156f4a2713aSLionel Sambuc isDependent = isDependentScopeSpecifier(PrefixSS);
157f4a2713aSLionel Sambuc } else if (ObjectTypePtr) {
158f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(SearchType);
159f4a2713aSLionel Sambuc isDependent = SearchType->isDependentType();
160f4a2713aSLionel Sambuc } else {
161f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(SS, EnteringContext);
162f4a2713aSLionel Sambuc isDependent = LookupCtx && LookupCtx->isDependentContext();
163f4a2713aSLionel Sambuc }
164f4a2713aSLionel Sambuc } else if (ObjectTypePtr) {
165f4a2713aSLionel Sambuc // C++ [basic.lookup.classref]p3:
166f4a2713aSLionel Sambuc // If the unqualified-id is ~type-name, the type-name is looked up
167f4a2713aSLionel Sambuc // in the context of the entire postfix-expression. If the type T
168f4a2713aSLionel Sambuc // of the object expression is of a class type C, the type-name is
169f4a2713aSLionel Sambuc // also looked up in the scope of class C. At least one of the
170f4a2713aSLionel Sambuc // lookups shall find a name that refers to (possibly
171f4a2713aSLionel Sambuc // cv-qualified) T.
172f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(SearchType);
173f4a2713aSLionel Sambuc isDependent = SearchType->isDependentType();
174f4a2713aSLionel Sambuc assert((isDependent || !SearchType->isIncompleteType()) &&
175f4a2713aSLionel Sambuc "Caller should have completed object type");
176f4a2713aSLionel Sambuc
177f4a2713aSLionel Sambuc LookInScope = true;
178f4a2713aSLionel Sambuc } else {
179f4a2713aSLionel Sambuc // Perform lookup into the current scope (only).
180f4a2713aSLionel Sambuc LookInScope = true;
181f4a2713aSLionel Sambuc }
182f4a2713aSLionel Sambuc
183*0a6a1f1dSLionel Sambuc TypeDecl *NonMatchingTypeDecl = nullptr;
184f4a2713aSLionel Sambuc LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName);
185f4a2713aSLionel Sambuc for (unsigned Step = 0; Step != 2; ++Step) {
186f4a2713aSLionel Sambuc // Look for the name first in the computed lookup context (if we
187f4a2713aSLionel Sambuc // have one) and, if that fails to find a match, in the scope (if
188f4a2713aSLionel Sambuc // we're allowed to look there).
189f4a2713aSLionel Sambuc Found.clear();
190f4a2713aSLionel Sambuc if (Step == 0 && LookupCtx)
191f4a2713aSLionel Sambuc LookupQualifiedName(Found, LookupCtx);
192f4a2713aSLionel Sambuc else if (Step == 1 && LookInScope && S)
193f4a2713aSLionel Sambuc LookupName(Found, S);
194f4a2713aSLionel Sambuc else
195f4a2713aSLionel Sambuc continue;
196f4a2713aSLionel Sambuc
197f4a2713aSLionel Sambuc // FIXME: Should we be suppressing ambiguities here?
198f4a2713aSLionel Sambuc if (Found.isAmbiguous())
199f4a2713aSLionel Sambuc return ParsedType();
200f4a2713aSLionel Sambuc
201f4a2713aSLionel Sambuc if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) {
202f4a2713aSLionel Sambuc QualType T = Context.getTypeDeclType(Type);
203*0a6a1f1dSLionel Sambuc MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false);
204f4a2713aSLionel Sambuc
205f4a2713aSLionel Sambuc if (SearchType.isNull() || SearchType->isDependentType() ||
206f4a2713aSLionel Sambuc Context.hasSameUnqualifiedType(T, SearchType)) {
207f4a2713aSLionel Sambuc // We found our type!
208f4a2713aSLionel Sambuc
209*0a6a1f1dSLionel Sambuc return CreateParsedType(T,
210*0a6a1f1dSLionel Sambuc Context.getTrivialTypeSourceInfo(T, NameLoc));
211f4a2713aSLionel Sambuc }
212f4a2713aSLionel Sambuc
213f4a2713aSLionel Sambuc if (!SearchType.isNull())
214f4a2713aSLionel Sambuc NonMatchingTypeDecl = Type;
215f4a2713aSLionel Sambuc }
216f4a2713aSLionel Sambuc
217f4a2713aSLionel Sambuc // If the name that we found is a class template name, and it is
218f4a2713aSLionel Sambuc // the same name as the template name in the last part of the
219f4a2713aSLionel Sambuc // nested-name-specifier (if present) or the object type, then
220f4a2713aSLionel Sambuc // this is the destructor for that class.
221f4a2713aSLionel Sambuc // FIXME: This is a workaround until we get real drafting for core
222f4a2713aSLionel Sambuc // issue 399, for which there isn't even an obvious direction.
223f4a2713aSLionel Sambuc if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) {
224f4a2713aSLionel Sambuc QualType MemberOfType;
225f4a2713aSLionel Sambuc if (SS.isSet()) {
226f4a2713aSLionel Sambuc if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) {
227f4a2713aSLionel Sambuc // Figure out the type of the context, if it has one.
228f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx))
229f4a2713aSLionel Sambuc MemberOfType = Context.getTypeDeclType(Record);
230f4a2713aSLionel Sambuc }
231f4a2713aSLionel Sambuc }
232f4a2713aSLionel Sambuc if (MemberOfType.isNull())
233f4a2713aSLionel Sambuc MemberOfType = SearchType;
234f4a2713aSLionel Sambuc
235f4a2713aSLionel Sambuc if (MemberOfType.isNull())
236f4a2713aSLionel Sambuc continue;
237f4a2713aSLionel Sambuc
238f4a2713aSLionel Sambuc // We're referring into a class template specialization. If the
239f4a2713aSLionel Sambuc // class template we found is the same as the template being
240f4a2713aSLionel Sambuc // specialized, we found what we are looking for.
241f4a2713aSLionel Sambuc if (const RecordType *Record = MemberOfType->getAs<RecordType>()) {
242f4a2713aSLionel Sambuc if (ClassTemplateSpecializationDecl *Spec
243f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
244f4a2713aSLionel Sambuc if (Spec->getSpecializedTemplate()->getCanonicalDecl() ==
245f4a2713aSLionel Sambuc Template->getCanonicalDecl())
246*0a6a1f1dSLionel Sambuc return CreateParsedType(
247*0a6a1f1dSLionel Sambuc MemberOfType,
248*0a6a1f1dSLionel Sambuc Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc));
249f4a2713aSLionel Sambuc }
250f4a2713aSLionel Sambuc
251f4a2713aSLionel Sambuc continue;
252f4a2713aSLionel Sambuc }
253f4a2713aSLionel Sambuc
254f4a2713aSLionel Sambuc // We're referring to an unresolved class template
255f4a2713aSLionel Sambuc // specialization. Determine whether we class template we found
256f4a2713aSLionel Sambuc // is the same as the template being specialized or, if we don't
257f4a2713aSLionel Sambuc // know which template is being specialized, that it at least
258f4a2713aSLionel Sambuc // has the same name.
259f4a2713aSLionel Sambuc if (const TemplateSpecializationType *SpecType
260f4a2713aSLionel Sambuc = MemberOfType->getAs<TemplateSpecializationType>()) {
261f4a2713aSLionel Sambuc TemplateName SpecName = SpecType->getTemplateName();
262f4a2713aSLionel Sambuc
263f4a2713aSLionel Sambuc // The class template we found is the same template being
264f4a2713aSLionel Sambuc // specialized.
265f4a2713aSLionel Sambuc if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) {
266f4a2713aSLionel Sambuc if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl())
267*0a6a1f1dSLionel Sambuc return CreateParsedType(
268*0a6a1f1dSLionel Sambuc MemberOfType,
269*0a6a1f1dSLionel Sambuc Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc));
270f4a2713aSLionel Sambuc
271f4a2713aSLionel Sambuc continue;
272f4a2713aSLionel Sambuc }
273f4a2713aSLionel Sambuc
274f4a2713aSLionel Sambuc // The class template we found has the same name as the
275f4a2713aSLionel Sambuc // (dependent) template name being specialized.
276f4a2713aSLionel Sambuc if (DependentTemplateName *DepTemplate
277f4a2713aSLionel Sambuc = SpecName.getAsDependentTemplateName()) {
278f4a2713aSLionel Sambuc if (DepTemplate->isIdentifier() &&
279f4a2713aSLionel Sambuc DepTemplate->getIdentifier() == Template->getIdentifier())
280*0a6a1f1dSLionel Sambuc return CreateParsedType(
281*0a6a1f1dSLionel Sambuc MemberOfType,
282*0a6a1f1dSLionel Sambuc Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc));
283f4a2713aSLionel Sambuc
284f4a2713aSLionel Sambuc continue;
285f4a2713aSLionel Sambuc }
286f4a2713aSLionel Sambuc }
287f4a2713aSLionel Sambuc }
288f4a2713aSLionel Sambuc }
289f4a2713aSLionel Sambuc
290f4a2713aSLionel Sambuc if (isDependent) {
291f4a2713aSLionel Sambuc // We didn't find our type, but that's okay: it's dependent
292f4a2713aSLionel Sambuc // anyway.
293f4a2713aSLionel Sambuc
294f4a2713aSLionel Sambuc // FIXME: What if we have no nested-name-specifier?
295f4a2713aSLionel Sambuc QualType T = CheckTypenameType(ETK_None, SourceLocation(),
296f4a2713aSLionel Sambuc SS.getWithLocInContext(Context),
297f4a2713aSLionel Sambuc II, NameLoc);
298f4a2713aSLionel Sambuc return ParsedType::make(T);
299f4a2713aSLionel Sambuc }
300f4a2713aSLionel Sambuc
301f4a2713aSLionel Sambuc if (NonMatchingTypeDecl) {
302f4a2713aSLionel Sambuc QualType T = Context.getTypeDeclType(NonMatchingTypeDecl);
303f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_destructor_expr_type_mismatch)
304f4a2713aSLionel Sambuc << T << SearchType;
305f4a2713aSLionel Sambuc Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here)
306f4a2713aSLionel Sambuc << T;
307f4a2713aSLionel Sambuc } else if (ObjectTypePtr)
308f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_ident_in_dtor_not_a_type)
309f4a2713aSLionel Sambuc << &II;
310f4a2713aSLionel Sambuc else {
311f4a2713aSLionel Sambuc SemaDiagnosticBuilder DtorDiag = Diag(NameLoc,
312f4a2713aSLionel Sambuc diag::err_destructor_class_name);
313f4a2713aSLionel Sambuc if (S) {
314f4a2713aSLionel Sambuc const DeclContext *Ctx = S->getEntity();
315f4a2713aSLionel Sambuc if (const CXXRecordDecl *Class = dyn_cast_or_null<CXXRecordDecl>(Ctx))
316f4a2713aSLionel Sambuc DtorDiag << FixItHint::CreateReplacement(SourceRange(NameLoc),
317f4a2713aSLionel Sambuc Class->getNameAsString());
318f4a2713aSLionel Sambuc }
319f4a2713aSLionel Sambuc }
320f4a2713aSLionel Sambuc
321f4a2713aSLionel Sambuc return ParsedType();
322f4a2713aSLionel Sambuc }
323f4a2713aSLionel Sambuc
getDestructorType(const DeclSpec & DS,ParsedType ObjectType)324f4a2713aSLionel Sambuc ParsedType Sema::getDestructorType(const DeclSpec& DS, ParsedType ObjectType) {
325f4a2713aSLionel Sambuc if (DS.getTypeSpecType() == DeclSpec::TST_error || !ObjectType)
326f4a2713aSLionel Sambuc return ParsedType();
327f4a2713aSLionel Sambuc assert(DS.getTypeSpecType() == DeclSpec::TST_decltype
328f4a2713aSLionel Sambuc && "only get destructor types from declspecs");
329f4a2713aSLionel Sambuc QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
330f4a2713aSLionel Sambuc QualType SearchType = GetTypeFromParser(ObjectType);
331f4a2713aSLionel Sambuc if (SearchType->isDependentType() || Context.hasSameUnqualifiedType(SearchType, T)) {
332f4a2713aSLionel Sambuc return ParsedType::make(T);
333f4a2713aSLionel Sambuc }
334f4a2713aSLionel Sambuc
335f4a2713aSLionel Sambuc Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch)
336f4a2713aSLionel Sambuc << T << SearchType;
337f4a2713aSLionel Sambuc return ParsedType();
338f4a2713aSLionel Sambuc }
339f4a2713aSLionel Sambuc
checkLiteralOperatorId(const CXXScopeSpec & SS,const UnqualifiedId & Name)340*0a6a1f1dSLionel Sambuc bool Sema::checkLiteralOperatorId(const CXXScopeSpec &SS,
341*0a6a1f1dSLionel Sambuc const UnqualifiedId &Name) {
342*0a6a1f1dSLionel Sambuc assert(Name.getKind() == UnqualifiedId::IK_LiteralOperatorId);
343*0a6a1f1dSLionel Sambuc
344*0a6a1f1dSLionel Sambuc if (!SS.isValid())
345*0a6a1f1dSLionel Sambuc return false;
346*0a6a1f1dSLionel Sambuc
347*0a6a1f1dSLionel Sambuc switch (SS.getScopeRep()->getKind()) {
348*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Identifier:
349*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::TypeSpec:
350*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::TypeSpecWithTemplate:
351*0a6a1f1dSLionel Sambuc // Per C++11 [over.literal]p2, literal operators can only be declared at
352*0a6a1f1dSLionel Sambuc // namespace scope. Therefore, this unqualified-id cannot name anything.
353*0a6a1f1dSLionel Sambuc // Reject it early, because we have no AST representation for this in the
354*0a6a1f1dSLionel Sambuc // case where the scope is dependent.
355*0a6a1f1dSLionel Sambuc Diag(Name.getLocStart(), diag::err_literal_operator_id_outside_namespace)
356*0a6a1f1dSLionel Sambuc << SS.getScopeRep();
357*0a6a1f1dSLionel Sambuc return true;
358*0a6a1f1dSLionel Sambuc
359*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Global:
360*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Super:
361*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Namespace:
362*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::NamespaceAlias:
363*0a6a1f1dSLionel Sambuc return false;
364*0a6a1f1dSLionel Sambuc }
365*0a6a1f1dSLionel Sambuc
366*0a6a1f1dSLionel Sambuc llvm_unreachable("unknown nested name specifier kind");
367*0a6a1f1dSLionel Sambuc }
368*0a6a1f1dSLionel Sambuc
369f4a2713aSLionel Sambuc /// \brief Build a C++ typeid expression with a type operand.
BuildCXXTypeId(QualType TypeInfoType,SourceLocation TypeidLoc,TypeSourceInfo * Operand,SourceLocation RParenLoc)370f4a2713aSLionel Sambuc ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType,
371f4a2713aSLionel Sambuc SourceLocation TypeidLoc,
372f4a2713aSLionel Sambuc TypeSourceInfo *Operand,
373f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
374f4a2713aSLionel Sambuc // C++ [expr.typeid]p4:
375f4a2713aSLionel Sambuc // The top-level cv-qualifiers of the lvalue expression or the type-id
376f4a2713aSLionel Sambuc // that is the operand of typeid are always ignored.
377f4a2713aSLionel Sambuc // If the type of the type-id is a class type or a reference to a class
378f4a2713aSLionel Sambuc // type, the class shall be completely-defined.
379f4a2713aSLionel Sambuc Qualifiers Quals;
380f4a2713aSLionel Sambuc QualType T
381f4a2713aSLionel Sambuc = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(),
382f4a2713aSLionel Sambuc Quals);
383f4a2713aSLionel Sambuc if (T->getAs<RecordType>() &&
384f4a2713aSLionel Sambuc RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid))
385f4a2713aSLionel Sambuc return ExprError();
386f4a2713aSLionel Sambuc
387*0a6a1f1dSLionel Sambuc if (T->isVariablyModifiedType())
388*0a6a1f1dSLionel Sambuc return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) << T);
389*0a6a1f1dSLionel Sambuc
390*0a6a1f1dSLionel Sambuc return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), Operand,
391*0a6a1f1dSLionel Sambuc SourceRange(TypeidLoc, RParenLoc));
392f4a2713aSLionel Sambuc }
393f4a2713aSLionel Sambuc
394f4a2713aSLionel Sambuc /// \brief Build a C++ typeid expression with an expression operand.
BuildCXXTypeId(QualType TypeInfoType,SourceLocation TypeidLoc,Expr * E,SourceLocation RParenLoc)395f4a2713aSLionel Sambuc ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType,
396f4a2713aSLionel Sambuc SourceLocation TypeidLoc,
397f4a2713aSLionel Sambuc Expr *E,
398f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
399*0a6a1f1dSLionel Sambuc bool WasEvaluated = false;
400f4a2713aSLionel Sambuc if (E && !E->isTypeDependent()) {
401f4a2713aSLionel Sambuc if (E->getType()->isPlaceholderType()) {
402f4a2713aSLionel Sambuc ExprResult result = CheckPlaceholderExpr(E);
403f4a2713aSLionel Sambuc if (result.isInvalid()) return ExprError();
404*0a6a1f1dSLionel Sambuc E = result.get();
405f4a2713aSLionel Sambuc }
406f4a2713aSLionel Sambuc
407f4a2713aSLionel Sambuc QualType T = E->getType();
408f4a2713aSLionel Sambuc if (const RecordType *RecordT = T->getAs<RecordType>()) {
409f4a2713aSLionel Sambuc CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl());
410f4a2713aSLionel Sambuc // C++ [expr.typeid]p3:
411f4a2713aSLionel Sambuc // [...] If the type of the expression is a class type, the class
412f4a2713aSLionel Sambuc // shall be completely-defined.
413f4a2713aSLionel Sambuc if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid))
414f4a2713aSLionel Sambuc return ExprError();
415f4a2713aSLionel Sambuc
416f4a2713aSLionel Sambuc // C++ [expr.typeid]p3:
417f4a2713aSLionel Sambuc // When typeid is applied to an expression other than an glvalue of a
418f4a2713aSLionel Sambuc // polymorphic class type [...] [the] expression is an unevaluated
419f4a2713aSLionel Sambuc // operand. [...]
420f4a2713aSLionel Sambuc if (RecordD->isPolymorphic() && E->isGLValue()) {
421f4a2713aSLionel Sambuc // The subexpression is potentially evaluated; switch the context
422f4a2713aSLionel Sambuc // and recheck the subexpression.
423f4a2713aSLionel Sambuc ExprResult Result = TransformToPotentiallyEvaluated(E);
424f4a2713aSLionel Sambuc if (Result.isInvalid()) return ExprError();
425*0a6a1f1dSLionel Sambuc E = Result.get();
426f4a2713aSLionel Sambuc
427f4a2713aSLionel Sambuc // We require a vtable to query the type at run time.
428f4a2713aSLionel Sambuc MarkVTableUsed(TypeidLoc, RecordD);
429*0a6a1f1dSLionel Sambuc WasEvaluated = true;
430f4a2713aSLionel Sambuc }
431f4a2713aSLionel Sambuc }
432f4a2713aSLionel Sambuc
433f4a2713aSLionel Sambuc // C++ [expr.typeid]p4:
434f4a2713aSLionel Sambuc // [...] If the type of the type-id is a reference to a possibly
435f4a2713aSLionel Sambuc // cv-qualified type, the result of the typeid expression refers to a
436f4a2713aSLionel Sambuc // std::type_info object representing the cv-unqualified referenced
437f4a2713aSLionel Sambuc // type.
438f4a2713aSLionel Sambuc Qualifiers Quals;
439f4a2713aSLionel Sambuc QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals);
440f4a2713aSLionel Sambuc if (!Context.hasSameType(T, UnqualT)) {
441f4a2713aSLionel Sambuc T = UnqualT;
442*0a6a1f1dSLionel Sambuc E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).get();
443f4a2713aSLionel Sambuc }
444f4a2713aSLionel Sambuc }
445f4a2713aSLionel Sambuc
446*0a6a1f1dSLionel Sambuc if (E->getType()->isVariablyModifiedType())
447*0a6a1f1dSLionel Sambuc return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid)
448*0a6a1f1dSLionel Sambuc << E->getType());
449*0a6a1f1dSLionel Sambuc else if (ActiveTemplateInstantiations.empty() &&
450*0a6a1f1dSLionel Sambuc E->HasSideEffects(Context, WasEvaluated)) {
451*0a6a1f1dSLionel Sambuc // The expression operand for typeid is in an unevaluated expression
452*0a6a1f1dSLionel Sambuc // context, so side effects could result in unintended consequences.
453*0a6a1f1dSLionel Sambuc Diag(E->getExprLoc(), WasEvaluated
454*0a6a1f1dSLionel Sambuc ? diag::warn_side_effects_typeid
455*0a6a1f1dSLionel Sambuc : diag::warn_side_effects_unevaluated_context);
456*0a6a1f1dSLionel Sambuc }
457*0a6a1f1dSLionel Sambuc
458*0a6a1f1dSLionel Sambuc return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), E,
459*0a6a1f1dSLionel Sambuc SourceRange(TypeidLoc, RParenLoc));
460f4a2713aSLionel Sambuc }
461f4a2713aSLionel Sambuc
462f4a2713aSLionel Sambuc /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression);
463f4a2713aSLionel Sambuc ExprResult
ActOnCXXTypeid(SourceLocation OpLoc,SourceLocation LParenLoc,bool isType,void * TyOrExpr,SourceLocation RParenLoc)464f4a2713aSLionel Sambuc Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc,
465f4a2713aSLionel Sambuc bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
466f4a2713aSLionel Sambuc // Find the std::type_info type.
467f4a2713aSLionel Sambuc if (!getStdNamespace())
468f4a2713aSLionel Sambuc return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
469f4a2713aSLionel Sambuc
470f4a2713aSLionel Sambuc if (!CXXTypeInfoDecl) {
471f4a2713aSLionel Sambuc IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info");
472f4a2713aSLionel Sambuc LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName);
473f4a2713aSLionel Sambuc LookupQualifiedName(R, getStdNamespace());
474f4a2713aSLionel Sambuc CXXTypeInfoDecl = R.getAsSingle<RecordDecl>();
475f4a2713aSLionel Sambuc // Microsoft's typeinfo doesn't have type_info in std but in the global
476f4a2713aSLionel Sambuc // namespace if _HAS_EXCEPTIONS is defined to 0. See PR13153.
477*0a6a1f1dSLionel Sambuc if (!CXXTypeInfoDecl && LangOpts.MSVCCompat) {
478f4a2713aSLionel Sambuc LookupQualifiedName(R, Context.getTranslationUnitDecl());
479f4a2713aSLionel Sambuc CXXTypeInfoDecl = R.getAsSingle<RecordDecl>();
480f4a2713aSLionel Sambuc }
481f4a2713aSLionel Sambuc if (!CXXTypeInfoDecl)
482f4a2713aSLionel Sambuc return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
483f4a2713aSLionel Sambuc }
484f4a2713aSLionel Sambuc
485f4a2713aSLionel Sambuc if (!getLangOpts().RTTI) {
486f4a2713aSLionel Sambuc return ExprError(Diag(OpLoc, diag::err_no_typeid_with_fno_rtti));
487f4a2713aSLionel Sambuc }
488f4a2713aSLionel Sambuc
489f4a2713aSLionel Sambuc QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl);
490f4a2713aSLionel Sambuc
491f4a2713aSLionel Sambuc if (isType) {
492f4a2713aSLionel Sambuc // The operand is a type; handle it as such.
493*0a6a1f1dSLionel Sambuc TypeSourceInfo *TInfo = nullptr;
494f4a2713aSLionel Sambuc QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr),
495f4a2713aSLionel Sambuc &TInfo);
496f4a2713aSLionel Sambuc if (T.isNull())
497f4a2713aSLionel Sambuc return ExprError();
498f4a2713aSLionel Sambuc
499f4a2713aSLionel Sambuc if (!TInfo)
500f4a2713aSLionel Sambuc TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc);
501f4a2713aSLionel Sambuc
502f4a2713aSLionel Sambuc return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc);
503f4a2713aSLionel Sambuc }
504f4a2713aSLionel Sambuc
505f4a2713aSLionel Sambuc // The operand is an expression.
506f4a2713aSLionel Sambuc return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc);
507f4a2713aSLionel Sambuc }
508f4a2713aSLionel Sambuc
509f4a2713aSLionel Sambuc /// \brief Build a Microsoft __uuidof expression with a type operand.
BuildCXXUuidof(QualType TypeInfoType,SourceLocation TypeidLoc,TypeSourceInfo * Operand,SourceLocation RParenLoc)510f4a2713aSLionel Sambuc ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType,
511f4a2713aSLionel Sambuc SourceLocation TypeidLoc,
512f4a2713aSLionel Sambuc TypeSourceInfo *Operand,
513f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
514f4a2713aSLionel Sambuc if (!Operand->getType()->isDependentType()) {
515f4a2713aSLionel Sambuc bool HasMultipleGUIDs = false;
516f4a2713aSLionel Sambuc if (!CXXUuidofExpr::GetUuidAttrOfType(Operand->getType(),
517f4a2713aSLionel Sambuc &HasMultipleGUIDs)) {
518f4a2713aSLionel Sambuc if (HasMultipleGUIDs)
519f4a2713aSLionel Sambuc return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids));
520f4a2713aSLionel Sambuc else
521f4a2713aSLionel Sambuc return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid));
522f4a2713aSLionel Sambuc }
523f4a2713aSLionel Sambuc }
524f4a2713aSLionel Sambuc
525*0a6a1f1dSLionel Sambuc return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), Operand,
526*0a6a1f1dSLionel Sambuc SourceRange(TypeidLoc, RParenLoc));
527f4a2713aSLionel Sambuc }
528f4a2713aSLionel Sambuc
529f4a2713aSLionel Sambuc /// \brief Build a Microsoft __uuidof expression with an expression operand.
BuildCXXUuidof(QualType TypeInfoType,SourceLocation TypeidLoc,Expr * E,SourceLocation RParenLoc)530f4a2713aSLionel Sambuc ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType,
531f4a2713aSLionel Sambuc SourceLocation TypeidLoc,
532f4a2713aSLionel Sambuc Expr *E,
533f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
534f4a2713aSLionel Sambuc if (!E->getType()->isDependentType()) {
535f4a2713aSLionel Sambuc bool HasMultipleGUIDs = false;
536f4a2713aSLionel Sambuc if (!CXXUuidofExpr::GetUuidAttrOfType(E->getType(), &HasMultipleGUIDs) &&
537f4a2713aSLionel Sambuc !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
538f4a2713aSLionel Sambuc if (HasMultipleGUIDs)
539f4a2713aSLionel Sambuc return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids));
540f4a2713aSLionel Sambuc else
541f4a2713aSLionel Sambuc return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid));
542f4a2713aSLionel Sambuc }
543f4a2713aSLionel Sambuc }
544f4a2713aSLionel Sambuc
545*0a6a1f1dSLionel Sambuc return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), E,
546*0a6a1f1dSLionel Sambuc SourceRange(TypeidLoc, RParenLoc));
547f4a2713aSLionel Sambuc }
548f4a2713aSLionel Sambuc
549f4a2713aSLionel Sambuc /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression);
550f4a2713aSLionel Sambuc ExprResult
ActOnCXXUuidof(SourceLocation OpLoc,SourceLocation LParenLoc,bool isType,void * TyOrExpr,SourceLocation RParenLoc)551f4a2713aSLionel Sambuc Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc,
552f4a2713aSLionel Sambuc bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
553f4a2713aSLionel Sambuc // If MSVCGuidDecl has not been cached, do the lookup.
554f4a2713aSLionel Sambuc if (!MSVCGuidDecl) {
555f4a2713aSLionel Sambuc IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID");
556f4a2713aSLionel Sambuc LookupResult R(*this, GuidII, SourceLocation(), LookupTagName);
557f4a2713aSLionel Sambuc LookupQualifiedName(R, Context.getTranslationUnitDecl());
558f4a2713aSLionel Sambuc MSVCGuidDecl = R.getAsSingle<RecordDecl>();
559f4a2713aSLionel Sambuc if (!MSVCGuidDecl)
560f4a2713aSLionel Sambuc return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof));
561f4a2713aSLionel Sambuc }
562f4a2713aSLionel Sambuc
563f4a2713aSLionel Sambuc QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl);
564f4a2713aSLionel Sambuc
565f4a2713aSLionel Sambuc if (isType) {
566f4a2713aSLionel Sambuc // The operand is a type; handle it as such.
567*0a6a1f1dSLionel Sambuc TypeSourceInfo *TInfo = nullptr;
568f4a2713aSLionel Sambuc QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr),
569f4a2713aSLionel Sambuc &TInfo);
570f4a2713aSLionel Sambuc if (T.isNull())
571f4a2713aSLionel Sambuc return ExprError();
572f4a2713aSLionel Sambuc
573f4a2713aSLionel Sambuc if (!TInfo)
574f4a2713aSLionel Sambuc TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc);
575f4a2713aSLionel Sambuc
576f4a2713aSLionel Sambuc return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc);
577f4a2713aSLionel Sambuc }
578f4a2713aSLionel Sambuc
579f4a2713aSLionel Sambuc // The operand is an expression.
580f4a2713aSLionel Sambuc return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc);
581f4a2713aSLionel Sambuc }
582f4a2713aSLionel Sambuc
583f4a2713aSLionel Sambuc /// ActOnCXXBoolLiteral - Parse {true,false} literals.
584f4a2713aSLionel Sambuc ExprResult
ActOnCXXBoolLiteral(SourceLocation OpLoc,tok::TokenKind Kind)585f4a2713aSLionel Sambuc Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
586f4a2713aSLionel Sambuc assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
587f4a2713aSLionel Sambuc "Unknown C++ Boolean value!");
588*0a6a1f1dSLionel Sambuc return new (Context)
589*0a6a1f1dSLionel Sambuc CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc);
590f4a2713aSLionel Sambuc }
591f4a2713aSLionel Sambuc
592f4a2713aSLionel Sambuc /// ActOnCXXNullPtrLiteral - Parse 'nullptr'.
593f4a2713aSLionel Sambuc ExprResult
ActOnCXXNullPtrLiteral(SourceLocation Loc)594f4a2713aSLionel Sambuc Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) {
595*0a6a1f1dSLionel Sambuc return new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc);
596f4a2713aSLionel Sambuc }
597f4a2713aSLionel Sambuc
598f4a2713aSLionel Sambuc /// ActOnCXXThrow - Parse throw expressions.
599f4a2713aSLionel Sambuc ExprResult
ActOnCXXThrow(Scope * S,SourceLocation OpLoc,Expr * Ex)600f4a2713aSLionel Sambuc Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) {
601f4a2713aSLionel Sambuc bool IsThrownVarInScope = false;
602f4a2713aSLionel Sambuc if (Ex) {
603f4a2713aSLionel Sambuc // C++0x [class.copymove]p31:
604f4a2713aSLionel Sambuc // When certain criteria are met, an implementation is allowed to omit the
605f4a2713aSLionel Sambuc // copy/move construction of a class object [...]
606f4a2713aSLionel Sambuc //
607f4a2713aSLionel Sambuc // - in a throw-expression, when the operand is the name of a
608f4a2713aSLionel Sambuc // non-volatile automatic object (other than a function or catch-
609f4a2713aSLionel Sambuc // clause parameter) whose scope does not extend beyond the end of the
610f4a2713aSLionel Sambuc // innermost enclosing try-block (if there is one), the copy/move
611f4a2713aSLionel Sambuc // operation from the operand to the exception object (15.1) can be
612f4a2713aSLionel Sambuc // omitted by constructing the automatic object directly into the
613f4a2713aSLionel Sambuc // exception object
614f4a2713aSLionel Sambuc if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens()))
615f4a2713aSLionel Sambuc if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
616f4a2713aSLionel Sambuc if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) {
617f4a2713aSLionel Sambuc for( ; S; S = S->getParent()) {
618f4a2713aSLionel Sambuc if (S->isDeclScope(Var)) {
619f4a2713aSLionel Sambuc IsThrownVarInScope = true;
620f4a2713aSLionel Sambuc break;
621f4a2713aSLionel Sambuc }
622f4a2713aSLionel Sambuc
623f4a2713aSLionel Sambuc if (S->getFlags() &
624f4a2713aSLionel Sambuc (Scope::FnScope | Scope::ClassScope | Scope::BlockScope |
625f4a2713aSLionel Sambuc Scope::FunctionPrototypeScope | Scope::ObjCMethodScope |
626f4a2713aSLionel Sambuc Scope::TryScope))
627f4a2713aSLionel Sambuc break;
628f4a2713aSLionel Sambuc }
629f4a2713aSLionel Sambuc }
630f4a2713aSLionel Sambuc }
631f4a2713aSLionel Sambuc }
632f4a2713aSLionel Sambuc
633f4a2713aSLionel Sambuc return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope);
634f4a2713aSLionel Sambuc }
635f4a2713aSLionel Sambuc
BuildCXXThrow(SourceLocation OpLoc,Expr * Ex,bool IsThrownVarInScope)636f4a2713aSLionel Sambuc ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex,
637f4a2713aSLionel Sambuc bool IsThrownVarInScope) {
638f4a2713aSLionel Sambuc // Don't report an error if 'throw' is used in system headers.
639f4a2713aSLionel Sambuc if (!getLangOpts().CXXExceptions &&
640f4a2713aSLionel Sambuc !getSourceManager().isInSystemHeader(OpLoc))
641f4a2713aSLionel Sambuc Diag(OpLoc, diag::err_exceptions_disabled) << "throw";
642f4a2713aSLionel Sambuc
643*0a6a1f1dSLionel Sambuc if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope())
644*0a6a1f1dSLionel Sambuc Diag(OpLoc, diag::err_omp_simd_region_cannot_use_stmt) << "throw";
645*0a6a1f1dSLionel Sambuc
646f4a2713aSLionel Sambuc if (Ex && !Ex->isTypeDependent()) {
647f4a2713aSLionel Sambuc ExprResult ExRes = CheckCXXThrowOperand(OpLoc, Ex, IsThrownVarInScope);
648f4a2713aSLionel Sambuc if (ExRes.isInvalid())
649f4a2713aSLionel Sambuc return ExprError();
650*0a6a1f1dSLionel Sambuc Ex = ExRes.get();
651f4a2713aSLionel Sambuc }
652f4a2713aSLionel Sambuc
653*0a6a1f1dSLionel Sambuc return new (Context)
654*0a6a1f1dSLionel Sambuc CXXThrowExpr(Ex, Context.VoidTy, OpLoc, IsThrownVarInScope);
655f4a2713aSLionel Sambuc }
656f4a2713aSLionel Sambuc
657f4a2713aSLionel Sambuc /// CheckCXXThrowOperand - Validate the operand of a throw.
CheckCXXThrowOperand(SourceLocation ThrowLoc,Expr * E,bool IsThrownVarInScope)658f4a2713aSLionel Sambuc ExprResult Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *E,
659f4a2713aSLionel Sambuc bool IsThrownVarInScope) {
660f4a2713aSLionel Sambuc // C++ [except.throw]p3:
661f4a2713aSLionel Sambuc // A throw-expression initializes a temporary object, called the exception
662f4a2713aSLionel Sambuc // object, the type of which is determined by removing any top-level
663f4a2713aSLionel Sambuc // cv-qualifiers from the static type of the operand of throw and adjusting
664f4a2713aSLionel Sambuc // the type from "array of T" or "function returning T" to "pointer to T"
665f4a2713aSLionel Sambuc // or "pointer to function returning T", [...]
666f4a2713aSLionel Sambuc if (E->getType().hasQualifiers())
667f4a2713aSLionel Sambuc E = ImpCastExprToType(E, E->getType().getUnqualifiedType(), CK_NoOp,
668*0a6a1f1dSLionel Sambuc E->getValueKind()).get();
669f4a2713aSLionel Sambuc
670f4a2713aSLionel Sambuc ExprResult Res = DefaultFunctionArrayConversion(E);
671f4a2713aSLionel Sambuc if (Res.isInvalid())
672f4a2713aSLionel Sambuc return ExprError();
673*0a6a1f1dSLionel Sambuc E = Res.get();
674f4a2713aSLionel Sambuc
675f4a2713aSLionel Sambuc // If the type of the exception would be an incomplete type or a pointer
676f4a2713aSLionel Sambuc // to an incomplete type other than (cv) void the program is ill-formed.
677f4a2713aSLionel Sambuc QualType Ty = E->getType();
678f4a2713aSLionel Sambuc bool isPointer = false;
679f4a2713aSLionel Sambuc if (const PointerType* Ptr = Ty->getAs<PointerType>()) {
680f4a2713aSLionel Sambuc Ty = Ptr->getPointeeType();
681f4a2713aSLionel Sambuc isPointer = true;
682f4a2713aSLionel Sambuc }
683f4a2713aSLionel Sambuc if (!isPointer || !Ty->isVoidType()) {
684f4a2713aSLionel Sambuc if (RequireCompleteType(ThrowLoc, Ty,
685f4a2713aSLionel Sambuc isPointer? diag::err_throw_incomplete_ptr
686f4a2713aSLionel Sambuc : diag::err_throw_incomplete,
687f4a2713aSLionel Sambuc E->getSourceRange()))
688f4a2713aSLionel Sambuc return ExprError();
689f4a2713aSLionel Sambuc
690f4a2713aSLionel Sambuc if (RequireNonAbstractType(ThrowLoc, E->getType(),
691f4a2713aSLionel Sambuc diag::err_throw_abstract_type, E))
692f4a2713aSLionel Sambuc return ExprError();
693f4a2713aSLionel Sambuc }
694f4a2713aSLionel Sambuc
695f4a2713aSLionel Sambuc // Initialize the exception result. This implicitly weeds out
696f4a2713aSLionel Sambuc // abstract types or types with inaccessible copy constructors.
697f4a2713aSLionel Sambuc
698f4a2713aSLionel Sambuc // C++0x [class.copymove]p31:
699f4a2713aSLionel Sambuc // When certain criteria are met, an implementation is allowed to omit the
700f4a2713aSLionel Sambuc // copy/move construction of a class object [...]
701f4a2713aSLionel Sambuc //
702f4a2713aSLionel Sambuc // - in a throw-expression, when the operand is the name of a
703f4a2713aSLionel Sambuc // non-volatile automatic object (other than a function or catch-clause
704f4a2713aSLionel Sambuc // parameter) whose scope does not extend beyond the end of the
705f4a2713aSLionel Sambuc // innermost enclosing try-block (if there is one), the copy/move
706f4a2713aSLionel Sambuc // operation from the operand to the exception object (15.1) can be
707f4a2713aSLionel Sambuc // omitted by constructing the automatic object directly into the
708f4a2713aSLionel Sambuc // exception object
709*0a6a1f1dSLionel Sambuc const VarDecl *NRVOVariable = nullptr;
710f4a2713aSLionel Sambuc if (IsThrownVarInScope)
711f4a2713aSLionel Sambuc NRVOVariable = getCopyElisionCandidate(QualType(), E, false);
712f4a2713aSLionel Sambuc
713f4a2713aSLionel Sambuc InitializedEntity Entity =
714f4a2713aSLionel Sambuc InitializedEntity::InitializeException(ThrowLoc, E->getType(),
715*0a6a1f1dSLionel Sambuc /*NRVO=*/NRVOVariable != nullptr);
716f4a2713aSLionel Sambuc Res = PerformMoveOrCopyInitialization(Entity, NRVOVariable,
717f4a2713aSLionel Sambuc QualType(), E,
718f4a2713aSLionel Sambuc IsThrownVarInScope);
719f4a2713aSLionel Sambuc if (Res.isInvalid())
720f4a2713aSLionel Sambuc return ExprError();
721*0a6a1f1dSLionel Sambuc E = Res.get();
722f4a2713aSLionel Sambuc
723f4a2713aSLionel Sambuc // If the exception has class type, we need additional handling.
724f4a2713aSLionel Sambuc const RecordType *RecordTy = Ty->getAs<RecordType>();
725f4a2713aSLionel Sambuc if (!RecordTy)
726*0a6a1f1dSLionel Sambuc return E;
727f4a2713aSLionel Sambuc CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
728f4a2713aSLionel Sambuc
729f4a2713aSLionel Sambuc // If we are throwing a polymorphic class type or pointer thereof,
730f4a2713aSLionel Sambuc // exception handling will make use of the vtable.
731f4a2713aSLionel Sambuc MarkVTableUsed(ThrowLoc, RD);
732f4a2713aSLionel Sambuc
733f4a2713aSLionel Sambuc // If a pointer is thrown, the referenced object will not be destroyed.
734f4a2713aSLionel Sambuc if (isPointer)
735*0a6a1f1dSLionel Sambuc return E;
736f4a2713aSLionel Sambuc
737f4a2713aSLionel Sambuc // If the class has a destructor, we must be able to call it.
738f4a2713aSLionel Sambuc if (RD->hasIrrelevantDestructor())
739*0a6a1f1dSLionel Sambuc return E;
740f4a2713aSLionel Sambuc
741f4a2713aSLionel Sambuc CXXDestructorDecl *Destructor = LookupDestructor(RD);
742f4a2713aSLionel Sambuc if (!Destructor)
743*0a6a1f1dSLionel Sambuc return E;
744f4a2713aSLionel Sambuc
745f4a2713aSLionel Sambuc MarkFunctionReferenced(E->getExprLoc(), Destructor);
746f4a2713aSLionel Sambuc CheckDestructorAccess(E->getExprLoc(), Destructor,
747f4a2713aSLionel Sambuc PDiag(diag::err_access_dtor_exception) << Ty);
748f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(Destructor, E->getExprLoc()))
749f4a2713aSLionel Sambuc return ExprError();
750*0a6a1f1dSLionel Sambuc return E;
751f4a2713aSLionel Sambuc }
752f4a2713aSLionel Sambuc
getCurrentThisType()753f4a2713aSLionel Sambuc QualType Sema::getCurrentThisType() {
754f4a2713aSLionel Sambuc DeclContext *DC = getFunctionLevelDeclContext();
755f4a2713aSLionel Sambuc QualType ThisTy = CXXThisTypeOverride;
756f4a2713aSLionel Sambuc if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) {
757f4a2713aSLionel Sambuc if (method && method->isInstance())
758f4a2713aSLionel Sambuc ThisTy = method->getThisType(Context);
759f4a2713aSLionel Sambuc }
760*0a6a1f1dSLionel Sambuc if (ThisTy.isNull()) {
761*0a6a1f1dSLionel Sambuc if (isGenericLambdaCallOperatorSpecialization(CurContext) &&
762*0a6a1f1dSLionel Sambuc CurContext->getParent()->getParent()->isRecord()) {
763*0a6a1f1dSLionel Sambuc // This is a generic lambda call operator that is being instantiated
764*0a6a1f1dSLionel Sambuc // within a default initializer - so use the enclosing class as 'this'.
765*0a6a1f1dSLionel Sambuc // There is no enclosing member function to retrieve the 'this' pointer
766*0a6a1f1dSLionel Sambuc // from.
767*0a6a1f1dSLionel Sambuc QualType ClassTy = Context.getTypeDeclType(
768*0a6a1f1dSLionel Sambuc cast<CXXRecordDecl>(CurContext->getParent()->getParent()));
769*0a6a1f1dSLionel Sambuc // There are no cv-qualifiers for 'this' within default initializers,
770*0a6a1f1dSLionel Sambuc // per [expr.prim.general]p4.
771*0a6a1f1dSLionel Sambuc return Context.getPointerType(ClassTy);
772*0a6a1f1dSLionel Sambuc }
773*0a6a1f1dSLionel Sambuc }
774f4a2713aSLionel Sambuc return ThisTy;
775f4a2713aSLionel Sambuc }
776f4a2713aSLionel Sambuc
CXXThisScopeRAII(Sema & S,Decl * ContextDecl,unsigned CXXThisTypeQuals,bool Enabled)777f4a2713aSLionel Sambuc Sema::CXXThisScopeRAII::CXXThisScopeRAII(Sema &S,
778f4a2713aSLionel Sambuc Decl *ContextDecl,
779f4a2713aSLionel Sambuc unsigned CXXThisTypeQuals,
780f4a2713aSLionel Sambuc bool Enabled)
781f4a2713aSLionel Sambuc : S(S), OldCXXThisTypeOverride(S.CXXThisTypeOverride), Enabled(false)
782f4a2713aSLionel Sambuc {
783f4a2713aSLionel Sambuc if (!Enabled || !ContextDecl)
784f4a2713aSLionel Sambuc return;
785f4a2713aSLionel Sambuc
786*0a6a1f1dSLionel Sambuc CXXRecordDecl *Record = nullptr;
787f4a2713aSLionel Sambuc if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(ContextDecl))
788f4a2713aSLionel Sambuc Record = Template->getTemplatedDecl();
789f4a2713aSLionel Sambuc else
790f4a2713aSLionel Sambuc Record = cast<CXXRecordDecl>(ContextDecl);
791f4a2713aSLionel Sambuc
792f4a2713aSLionel Sambuc S.CXXThisTypeOverride
793f4a2713aSLionel Sambuc = S.Context.getPointerType(
794f4a2713aSLionel Sambuc S.Context.getRecordType(Record).withCVRQualifiers(CXXThisTypeQuals));
795f4a2713aSLionel Sambuc
796f4a2713aSLionel Sambuc this->Enabled = true;
797f4a2713aSLionel Sambuc }
798f4a2713aSLionel Sambuc
799f4a2713aSLionel Sambuc
~CXXThisScopeRAII()800f4a2713aSLionel Sambuc Sema::CXXThisScopeRAII::~CXXThisScopeRAII() {
801f4a2713aSLionel Sambuc if (Enabled) {
802f4a2713aSLionel Sambuc S.CXXThisTypeOverride = OldCXXThisTypeOverride;
803f4a2713aSLionel Sambuc }
804f4a2713aSLionel Sambuc }
805f4a2713aSLionel Sambuc
captureThis(ASTContext & Context,RecordDecl * RD,QualType ThisTy,SourceLocation Loc)806f4a2713aSLionel Sambuc static Expr *captureThis(ASTContext &Context, RecordDecl *RD,
807f4a2713aSLionel Sambuc QualType ThisTy, SourceLocation Loc) {
808f4a2713aSLionel Sambuc FieldDecl *Field
809*0a6a1f1dSLionel Sambuc = FieldDecl::Create(Context, RD, Loc, Loc, nullptr, ThisTy,
810f4a2713aSLionel Sambuc Context.getTrivialTypeSourceInfo(ThisTy, Loc),
811*0a6a1f1dSLionel Sambuc nullptr, false, ICIS_NoInit);
812f4a2713aSLionel Sambuc Field->setImplicit(true);
813f4a2713aSLionel Sambuc Field->setAccess(AS_private);
814f4a2713aSLionel Sambuc RD->addDecl(Field);
815f4a2713aSLionel Sambuc return new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit*/true);
816f4a2713aSLionel Sambuc }
817f4a2713aSLionel Sambuc
CheckCXXThisCapture(SourceLocation Loc,bool Explicit,bool BuildAndDiagnose,const unsigned * const FunctionScopeIndexToStopAt)818f4a2713aSLionel Sambuc bool Sema::CheckCXXThisCapture(SourceLocation Loc, bool Explicit,
819f4a2713aSLionel Sambuc bool BuildAndDiagnose, const unsigned *const FunctionScopeIndexToStopAt) {
820f4a2713aSLionel Sambuc // We don't need to capture this in an unevaluated context.
821f4a2713aSLionel Sambuc if (isUnevaluatedContext() && !Explicit)
822f4a2713aSLionel Sambuc return true;
823f4a2713aSLionel Sambuc
824f4a2713aSLionel Sambuc const unsigned MaxFunctionScopesIndex = FunctionScopeIndexToStopAt ?
825f4a2713aSLionel Sambuc *FunctionScopeIndexToStopAt : FunctionScopes.size() - 1;
826f4a2713aSLionel Sambuc // Otherwise, check that we can capture 'this'.
827f4a2713aSLionel Sambuc unsigned NumClosures = 0;
828f4a2713aSLionel Sambuc for (unsigned idx = MaxFunctionScopesIndex; idx != 0; idx--) {
829f4a2713aSLionel Sambuc if (CapturingScopeInfo *CSI =
830f4a2713aSLionel Sambuc dyn_cast<CapturingScopeInfo>(FunctionScopes[idx])) {
831f4a2713aSLionel Sambuc if (CSI->CXXThisCaptureIndex != 0) {
832f4a2713aSLionel Sambuc // 'this' is already being captured; there isn't anything more to do.
833f4a2713aSLionel Sambuc break;
834f4a2713aSLionel Sambuc }
835f4a2713aSLionel Sambuc LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI);
836f4a2713aSLionel Sambuc if (LSI && isGenericLambdaCallOperatorSpecialization(LSI->CallOperator)) {
837f4a2713aSLionel Sambuc // This context can't implicitly capture 'this'; fail out.
838f4a2713aSLionel Sambuc if (BuildAndDiagnose)
839f4a2713aSLionel Sambuc Diag(Loc, diag::err_this_capture) << Explicit;
840f4a2713aSLionel Sambuc return true;
841f4a2713aSLionel Sambuc }
842f4a2713aSLionel Sambuc if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByref ||
843f4a2713aSLionel Sambuc CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByval ||
844f4a2713aSLionel Sambuc CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_Block ||
845f4a2713aSLionel Sambuc CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_CapturedRegion ||
846f4a2713aSLionel Sambuc Explicit) {
847f4a2713aSLionel Sambuc // This closure can capture 'this'; continue looking upwards.
848f4a2713aSLionel Sambuc NumClosures++;
849f4a2713aSLionel Sambuc Explicit = false;
850f4a2713aSLionel Sambuc continue;
851f4a2713aSLionel Sambuc }
852f4a2713aSLionel Sambuc // This context can't implicitly capture 'this'; fail out.
853f4a2713aSLionel Sambuc if (BuildAndDiagnose)
854f4a2713aSLionel Sambuc Diag(Loc, diag::err_this_capture) << Explicit;
855f4a2713aSLionel Sambuc return true;
856f4a2713aSLionel Sambuc }
857f4a2713aSLionel Sambuc break;
858f4a2713aSLionel Sambuc }
859f4a2713aSLionel Sambuc if (!BuildAndDiagnose) return false;
860f4a2713aSLionel Sambuc // Mark that we're implicitly capturing 'this' in all the scopes we skipped.
861f4a2713aSLionel Sambuc // FIXME: We need to delay this marking in PotentiallyPotentiallyEvaluated
862f4a2713aSLionel Sambuc // contexts.
863f4a2713aSLionel Sambuc for (unsigned idx = MaxFunctionScopesIndex; NumClosures;
864f4a2713aSLionel Sambuc --idx, --NumClosures) {
865f4a2713aSLionel Sambuc CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[idx]);
866*0a6a1f1dSLionel Sambuc Expr *ThisExpr = nullptr;
867f4a2713aSLionel Sambuc QualType ThisTy = getCurrentThisType();
868f4a2713aSLionel Sambuc if (LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI))
869f4a2713aSLionel Sambuc // For lambda expressions, build a field and an initializing expression.
870f4a2713aSLionel Sambuc ThisExpr = captureThis(Context, LSI->Lambda, ThisTy, Loc);
871f4a2713aSLionel Sambuc else if (CapturedRegionScopeInfo *RSI
872f4a2713aSLionel Sambuc = dyn_cast<CapturedRegionScopeInfo>(FunctionScopes[idx]))
873f4a2713aSLionel Sambuc ThisExpr = captureThis(Context, RSI->TheRecordDecl, ThisTy, Loc);
874f4a2713aSLionel Sambuc
875f4a2713aSLionel Sambuc bool isNested = NumClosures > 1;
876f4a2713aSLionel Sambuc CSI->addThisCapture(isNested, Loc, ThisTy, ThisExpr);
877f4a2713aSLionel Sambuc }
878f4a2713aSLionel Sambuc return false;
879f4a2713aSLionel Sambuc }
880f4a2713aSLionel Sambuc
ActOnCXXThis(SourceLocation Loc)881f4a2713aSLionel Sambuc ExprResult Sema::ActOnCXXThis(SourceLocation Loc) {
882f4a2713aSLionel Sambuc /// C++ 9.3.2: In the body of a non-static member function, the keyword this
883f4a2713aSLionel Sambuc /// is a non-lvalue expression whose value is the address of the object for
884f4a2713aSLionel Sambuc /// which the function is called.
885f4a2713aSLionel Sambuc
886f4a2713aSLionel Sambuc QualType ThisTy = getCurrentThisType();
887f4a2713aSLionel Sambuc if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use);
888f4a2713aSLionel Sambuc
889f4a2713aSLionel Sambuc CheckCXXThisCapture(Loc);
890*0a6a1f1dSLionel Sambuc return new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false);
891f4a2713aSLionel Sambuc }
892f4a2713aSLionel Sambuc
isThisOutsideMemberFunctionBody(QualType BaseType)893f4a2713aSLionel Sambuc bool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) {
894f4a2713aSLionel Sambuc // If we're outside the body of a member function, then we'll have a specified
895f4a2713aSLionel Sambuc // type for 'this'.
896f4a2713aSLionel Sambuc if (CXXThisTypeOverride.isNull())
897f4a2713aSLionel Sambuc return false;
898f4a2713aSLionel Sambuc
899f4a2713aSLionel Sambuc // Determine whether we're looking into a class that's currently being
900f4a2713aSLionel Sambuc // defined.
901f4a2713aSLionel Sambuc CXXRecordDecl *Class = BaseType->getAsCXXRecordDecl();
902f4a2713aSLionel Sambuc return Class && Class->isBeingDefined();
903f4a2713aSLionel Sambuc }
904f4a2713aSLionel Sambuc
905f4a2713aSLionel Sambuc ExprResult
ActOnCXXTypeConstructExpr(ParsedType TypeRep,SourceLocation LParenLoc,MultiExprArg exprs,SourceLocation RParenLoc)906f4a2713aSLionel Sambuc Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep,
907f4a2713aSLionel Sambuc SourceLocation LParenLoc,
908f4a2713aSLionel Sambuc MultiExprArg exprs,
909f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
910f4a2713aSLionel Sambuc if (!TypeRep)
911f4a2713aSLionel Sambuc return ExprError();
912f4a2713aSLionel Sambuc
913f4a2713aSLionel Sambuc TypeSourceInfo *TInfo;
914f4a2713aSLionel Sambuc QualType Ty = GetTypeFromParser(TypeRep, &TInfo);
915f4a2713aSLionel Sambuc if (!TInfo)
916f4a2713aSLionel Sambuc TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation());
917f4a2713aSLionel Sambuc
918f4a2713aSLionel Sambuc return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc);
919f4a2713aSLionel Sambuc }
920f4a2713aSLionel Sambuc
921f4a2713aSLionel Sambuc /// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
922f4a2713aSLionel Sambuc /// Can be interpreted either as function-style casting ("int(x)")
923f4a2713aSLionel Sambuc /// or class type construction ("ClassType(x,y,z)")
924f4a2713aSLionel Sambuc /// or creation of a value-initialized type ("int()").
925f4a2713aSLionel Sambuc ExprResult
BuildCXXTypeConstructExpr(TypeSourceInfo * TInfo,SourceLocation LParenLoc,MultiExprArg Exprs,SourceLocation RParenLoc)926f4a2713aSLionel Sambuc Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
927f4a2713aSLionel Sambuc SourceLocation LParenLoc,
928f4a2713aSLionel Sambuc MultiExprArg Exprs,
929f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
930f4a2713aSLionel Sambuc QualType Ty = TInfo->getType();
931f4a2713aSLionel Sambuc SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc();
932f4a2713aSLionel Sambuc
933f4a2713aSLionel Sambuc if (Ty->isDependentType() || CallExpr::hasAnyTypeDependentArguments(Exprs)) {
934*0a6a1f1dSLionel Sambuc return CXXUnresolvedConstructExpr::Create(Context, TInfo, LParenLoc, Exprs,
935*0a6a1f1dSLionel Sambuc RParenLoc);
936f4a2713aSLionel Sambuc }
937f4a2713aSLionel Sambuc
938f4a2713aSLionel Sambuc bool ListInitialization = LParenLoc.isInvalid();
939f4a2713aSLionel Sambuc assert((!ListInitialization || (Exprs.size() == 1 && isa<InitListExpr>(Exprs[0])))
940f4a2713aSLionel Sambuc && "List initialization must have initializer list as expression.");
941f4a2713aSLionel Sambuc SourceRange FullRange = SourceRange(TyBeginLoc,
942f4a2713aSLionel Sambuc ListInitialization ? Exprs[0]->getSourceRange().getEnd() : RParenLoc);
943f4a2713aSLionel Sambuc
944f4a2713aSLionel Sambuc // C++ [expr.type.conv]p1:
945f4a2713aSLionel Sambuc // If the expression list is a single expression, the type conversion
946f4a2713aSLionel Sambuc // expression is equivalent (in definedness, and if defined in meaning) to the
947f4a2713aSLionel Sambuc // corresponding cast expression.
948f4a2713aSLionel Sambuc if (Exprs.size() == 1 && !ListInitialization) {
949f4a2713aSLionel Sambuc Expr *Arg = Exprs[0];
950f4a2713aSLionel Sambuc return BuildCXXFunctionalCastExpr(TInfo, LParenLoc, Arg, RParenLoc);
951f4a2713aSLionel Sambuc }
952f4a2713aSLionel Sambuc
953f4a2713aSLionel Sambuc QualType ElemTy = Ty;
954f4a2713aSLionel Sambuc if (Ty->isArrayType()) {
955f4a2713aSLionel Sambuc if (!ListInitialization)
956f4a2713aSLionel Sambuc return ExprError(Diag(TyBeginLoc,
957f4a2713aSLionel Sambuc diag::err_value_init_for_array_type) << FullRange);
958f4a2713aSLionel Sambuc ElemTy = Context.getBaseElementType(Ty);
959f4a2713aSLionel Sambuc }
960f4a2713aSLionel Sambuc
961f4a2713aSLionel Sambuc if (!Ty->isVoidType() &&
962f4a2713aSLionel Sambuc RequireCompleteType(TyBeginLoc, ElemTy,
963f4a2713aSLionel Sambuc diag::err_invalid_incomplete_type_use, FullRange))
964f4a2713aSLionel Sambuc return ExprError();
965f4a2713aSLionel Sambuc
966f4a2713aSLionel Sambuc if (RequireNonAbstractType(TyBeginLoc, Ty,
967f4a2713aSLionel Sambuc diag::err_allocation_of_abstract_type))
968f4a2713aSLionel Sambuc return ExprError();
969f4a2713aSLionel Sambuc
970f4a2713aSLionel Sambuc InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo);
971f4a2713aSLionel Sambuc InitializationKind Kind =
972f4a2713aSLionel Sambuc Exprs.size() ? ListInitialization
973f4a2713aSLionel Sambuc ? InitializationKind::CreateDirectList(TyBeginLoc)
974f4a2713aSLionel Sambuc : InitializationKind::CreateDirect(TyBeginLoc, LParenLoc, RParenLoc)
975f4a2713aSLionel Sambuc : InitializationKind::CreateValue(TyBeginLoc, LParenLoc, RParenLoc);
976f4a2713aSLionel Sambuc InitializationSequence InitSeq(*this, Entity, Kind, Exprs);
977f4a2713aSLionel Sambuc ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Exprs);
978f4a2713aSLionel Sambuc
979f4a2713aSLionel Sambuc if (Result.isInvalid() || !ListInitialization)
980f4a2713aSLionel Sambuc return Result;
981f4a2713aSLionel Sambuc
982f4a2713aSLionel Sambuc Expr *Inner = Result.get();
983f4a2713aSLionel Sambuc if (CXXBindTemporaryExpr *BTE = dyn_cast_or_null<CXXBindTemporaryExpr>(Inner))
984f4a2713aSLionel Sambuc Inner = BTE->getSubExpr();
985f4a2713aSLionel Sambuc if (isa<InitListExpr>(Inner)) {
986f4a2713aSLionel Sambuc // If the list-initialization doesn't involve a constructor call, we'll get
987f4a2713aSLionel Sambuc // the initializer-list (with corrected type) back, but that's not what we
988f4a2713aSLionel Sambuc // want, since it will be treated as an initializer list in further
989f4a2713aSLionel Sambuc // processing. Explicitly insert a cast here.
990f4a2713aSLionel Sambuc QualType ResultType = Result.get()->getType();
991*0a6a1f1dSLionel Sambuc Result = CXXFunctionalCastExpr::Create(
992f4a2713aSLionel Sambuc Context, ResultType, Expr::getValueKindForType(TInfo->getType()), TInfo,
993*0a6a1f1dSLionel Sambuc CK_NoOp, Result.get(), /*Path=*/nullptr, LParenLoc, RParenLoc);
994f4a2713aSLionel Sambuc }
995f4a2713aSLionel Sambuc
996f4a2713aSLionel Sambuc // FIXME: Improve AST representation?
997f4a2713aSLionel Sambuc return Result;
998f4a2713aSLionel Sambuc }
999f4a2713aSLionel Sambuc
1000f4a2713aSLionel Sambuc /// doesUsualArrayDeleteWantSize - Answers whether the usual
1001f4a2713aSLionel Sambuc /// operator delete[] for the given type has a size_t parameter.
doesUsualArrayDeleteWantSize(Sema & S,SourceLocation loc,QualType allocType)1002f4a2713aSLionel Sambuc static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc,
1003f4a2713aSLionel Sambuc QualType allocType) {
1004f4a2713aSLionel Sambuc const RecordType *record =
1005f4a2713aSLionel Sambuc allocType->getBaseElementTypeUnsafe()->getAs<RecordType>();
1006f4a2713aSLionel Sambuc if (!record) return false;
1007f4a2713aSLionel Sambuc
1008f4a2713aSLionel Sambuc // Try to find an operator delete[] in class scope.
1009f4a2713aSLionel Sambuc
1010f4a2713aSLionel Sambuc DeclarationName deleteName =
1011f4a2713aSLionel Sambuc S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete);
1012f4a2713aSLionel Sambuc LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName);
1013f4a2713aSLionel Sambuc S.LookupQualifiedName(ops, record->getDecl());
1014f4a2713aSLionel Sambuc
1015f4a2713aSLionel Sambuc // We're just doing this for information.
1016f4a2713aSLionel Sambuc ops.suppressDiagnostics();
1017f4a2713aSLionel Sambuc
1018f4a2713aSLionel Sambuc // Very likely: there's no operator delete[].
1019f4a2713aSLionel Sambuc if (ops.empty()) return false;
1020f4a2713aSLionel Sambuc
1021f4a2713aSLionel Sambuc // If it's ambiguous, it should be illegal to call operator delete[]
1022f4a2713aSLionel Sambuc // on this thing, so it doesn't matter if we allocate extra space or not.
1023f4a2713aSLionel Sambuc if (ops.isAmbiguous()) return false;
1024f4a2713aSLionel Sambuc
1025f4a2713aSLionel Sambuc LookupResult::Filter filter = ops.makeFilter();
1026f4a2713aSLionel Sambuc while (filter.hasNext()) {
1027f4a2713aSLionel Sambuc NamedDecl *del = filter.next()->getUnderlyingDecl();
1028f4a2713aSLionel Sambuc
1029f4a2713aSLionel Sambuc // C++0x [basic.stc.dynamic.deallocation]p2:
1030f4a2713aSLionel Sambuc // A template instance is never a usual deallocation function,
1031f4a2713aSLionel Sambuc // regardless of its signature.
1032f4a2713aSLionel Sambuc if (isa<FunctionTemplateDecl>(del)) {
1033f4a2713aSLionel Sambuc filter.erase();
1034f4a2713aSLionel Sambuc continue;
1035f4a2713aSLionel Sambuc }
1036f4a2713aSLionel Sambuc
1037f4a2713aSLionel Sambuc // C++0x [basic.stc.dynamic.deallocation]p2:
1038f4a2713aSLionel Sambuc // If class T does not declare [an operator delete[] with one
1039f4a2713aSLionel Sambuc // parameter] but does declare a member deallocation function
1040f4a2713aSLionel Sambuc // named operator delete[] with exactly two parameters, the
1041f4a2713aSLionel Sambuc // second of which has type std::size_t, then this function
1042f4a2713aSLionel Sambuc // is a usual deallocation function.
1043f4a2713aSLionel Sambuc if (!cast<CXXMethodDecl>(del)->isUsualDeallocationFunction()) {
1044f4a2713aSLionel Sambuc filter.erase();
1045f4a2713aSLionel Sambuc continue;
1046f4a2713aSLionel Sambuc }
1047f4a2713aSLionel Sambuc }
1048f4a2713aSLionel Sambuc filter.done();
1049f4a2713aSLionel Sambuc
1050f4a2713aSLionel Sambuc if (!ops.isSingleResult()) return false;
1051f4a2713aSLionel Sambuc
1052f4a2713aSLionel Sambuc const FunctionDecl *del = cast<FunctionDecl>(ops.getFoundDecl());
1053f4a2713aSLionel Sambuc return (del->getNumParams() == 2);
1054f4a2713aSLionel Sambuc }
1055f4a2713aSLionel Sambuc
1056f4a2713aSLionel Sambuc /// \brief Parsed a C++ 'new' expression (C++ 5.3.4).
1057f4a2713aSLionel Sambuc ///
1058f4a2713aSLionel Sambuc /// E.g.:
1059f4a2713aSLionel Sambuc /// @code new (memory) int[size][4] @endcode
1060f4a2713aSLionel Sambuc /// or
1061f4a2713aSLionel Sambuc /// @code ::new Foo(23, "hello") @endcode
1062f4a2713aSLionel Sambuc ///
1063f4a2713aSLionel Sambuc /// \param StartLoc The first location of the expression.
1064f4a2713aSLionel Sambuc /// \param UseGlobal True if 'new' was prefixed with '::'.
1065f4a2713aSLionel Sambuc /// \param PlacementLParen Opening paren of the placement arguments.
1066f4a2713aSLionel Sambuc /// \param PlacementArgs Placement new arguments.
1067f4a2713aSLionel Sambuc /// \param PlacementRParen Closing paren of the placement arguments.
1068f4a2713aSLionel Sambuc /// \param TypeIdParens If the type is in parens, the source range.
1069f4a2713aSLionel Sambuc /// \param D The type to be allocated, as well as array dimensions.
1070f4a2713aSLionel Sambuc /// \param Initializer The initializing expression or initializer-list, or null
1071f4a2713aSLionel Sambuc /// if there is none.
1072f4a2713aSLionel Sambuc ExprResult
ActOnCXXNew(SourceLocation StartLoc,bool UseGlobal,SourceLocation PlacementLParen,MultiExprArg PlacementArgs,SourceLocation PlacementRParen,SourceRange TypeIdParens,Declarator & D,Expr * Initializer)1073f4a2713aSLionel Sambuc Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal,
1074f4a2713aSLionel Sambuc SourceLocation PlacementLParen, MultiExprArg PlacementArgs,
1075f4a2713aSLionel Sambuc SourceLocation PlacementRParen, SourceRange TypeIdParens,
1076f4a2713aSLionel Sambuc Declarator &D, Expr *Initializer) {
1077f4a2713aSLionel Sambuc bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
1078f4a2713aSLionel Sambuc
1079*0a6a1f1dSLionel Sambuc Expr *ArraySize = nullptr;
1080f4a2713aSLionel Sambuc // If the specified type is an array, unwrap it and save the expression.
1081f4a2713aSLionel Sambuc if (D.getNumTypeObjects() > 0 &&
1082f4a2713aSLionel Sambuc D.getTypeObject(0).Kind == DeclaratorChunk::Array) {
1083f4a2713aSLionel Sambuc DeclaratorChunk &Chunk = D.getTypeObject(0);
1084f4a2713aSLionel Sambuc if (TypeContainsAuto)
1085f4a2713aSLionel Sambuc return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto)
1086f4a2713aSLionel Sambuc << D.getSourceRange());
1087f4a2713aSLionel Sambuc if (Chunk.Arr.hasStatic)
1088f4a2713aSLionel Sambuc return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new)
1089f4a2713aSLionel Sambuc << D.getSourceRange());
1090f4a2713aSLionel Sambuc if (!Chunk.Arr.NumElts)
1091f4a2713aSLionel Sambuc return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size)
1092f4a2713aSLionel Sambuc << D.getSourceRange());
1093f4a2713aSLionel Sambuc
1094f4a2713aSLionel Sambuc ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts);
1095f4a2713aSLionel Sambuc D.DropFirstTypeObject();
1096f4a2713aSLionel Sambuc }
1097f4a2713aSLionel Sambuc
1098f4a2713aSLionel Sambuc // Every dimension shall be of constant size.
1099f4a2713aSLionel Sambuc if (ArraySize) {
1100f4a2713aSLionel Sambuc for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) {
1101f4a2713aSLionel Sambuc if (D.getTypeObject(I).Kind != DeclaratorChunk::Array)
1102f4a2713aSLionel Sambuc break;
1103f4a2713aSLionel Sambuc
1104f4a2713aSLionel Sambuc DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr;
1105f4a2713aSLionel Sambuc if (Expr *NumElts = (Expr *)Array.NumElts) {
1106f4a2713aSLionel Sambuc if (!NumElts->isTypeDependent() && !NumElts->isValueDependent()) {
1107*0a6a1f1dSLionel Sambuc if (getLangOpts().CPlusPlus14) {
1108f4a2713aSLionel Sambuc // C++1y [expr.new]p6: Every constant-expression in a noptr-new-declarator
1109f4a2713aSLionel Sambuc // shall be a converted constant expression (5.19) of type std::size_t
1110f4a2713aSLionel Sambuc // and shall evaluate to a strictly positive value.
1111f4a2713aSLionel Sambuc unsigned IntWidth = Context.getTargetInfo().getIntWidth();
1112f4a2713aSLionel Sambuc assert(IntWidth && "Builtin type of size 0?");
1113f4a2713aSLionel Sambuc llvm::APSInt Value(IntWidth);
1114f4a2713aSLionel Sambuc Array.NumElts
1115f4a2713aSLionel Sambuc = CheckConvertedConstantExpression(NumElts, Context.getSizeType(), Value,
1116f4a2713aSLionel Sambuc CCEK_NewExpr)
1117*0a6a1f1dSLionel Sambuc .get();
1118f4a2713aSLionel Sambuc } else {
1119f4a2713aSLionel Sambuc Array.NumElts
1120*0a6a1f1dSLionel Sambuc = VerifyIntegerConstantExpression(NumElts, nullptr,
1121f4a2713aSLionel Sambuc diag::err_new_array_nonconst)
1122*0a6a1f1dSLionel Sambuc .get();
1123f4a2713aSLionel Sambuc }
1124f4a2713aSLionel Sambuc if (!Array.NumElts)
1125f4a2713aSLionel Sambuc return ExprError();
1126f4a2713aSLionel Sambuc }
1127f4a2713aSLionel Sambuc }
1128f4a2713aSLionel Sambuc }
1129f4a2713aSLionel Sambuc }
1130f4a2713aSLionel Sambuc
1131*0a6a1f1dSLionel Sambuc TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/nullptr);
1132f4a2713aSLionel Sambuc QualType AllocType = TInfo->getType();
1133f4a2713aSLionel Sambuc if (D.isInvalidType())
1134f4a2713aSLionel Sambuc return ExprError();
1135f4a2713aSLionel Sambuc
1136f4a2713aSLionel Sambuc SourceRange DirectInitRange;
1137f4a2713aSLionel Sambuc if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer))
1138f4a2713aSLionel Sambuc DirectInitRange = List->getSourceRange();
1139f4a2713aSLionel Sambuc
1140f4a2713aSLionel Sambuc return BuildCXXNew(SourceRange(StartLoc, D.getLocEnd()), UseGlobal,
1141f4a2713aSLionel Sambuc PlacementLParen,
1142f4a2713aSLionel Sambuc PlacementArgs,
1143f4a2713aSLionel Sambuc PlacementRParen,
1144f4a2713aSLionel Sambuc TypeIdParens,
1145f4a2713aSLionel Sambuc AllocType,
1146f4a2713aSLionel Sambuc TInfo,
1147f4a2713aSLionel Sambuc ArraySize,
1148f4a2713aSLionel Sambuc DirectInitRange,
1149f4a2713aSLionel Sambuc Initializer,
1150f4a2713aSLionel Sambuc TypeContainsAuto);
1151f4a2713aSLionel Sambuc }
1152f4a2713aSLionel Sambuc
isLegalArrayNewInitializer(CXXNewExpr::InitializationStyle Style,Expr * Init)1153f4a2713aSLionel Sambuc static bool isLegalArrayNewInitializer(CXXNewExpr::InitializationStyle Style,
1154f4a2713aSLionel Sambuc Expr *Init) {
1155f4a2713aSLionel Sambuc if (!Init)
1156f4a2713aSLionel Sambuc return true;
1157f4a2713aSLionel Sambuc if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init))
1158f4a2713aSLionel Sambuc return PLE->getNumExprs() == 0;
1159f4a2713aSLionel Sambuc if (isa<ImplicitValueInitExpr>(Init))
1160f4a2713aSLionel Sambuc return true;
1161f4a2713aSLionel Sambuc else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init))
1162f4a2713aSLionel Sambuc return !CCE->isListInitialization() &&
1163f4a2713aSLionel Sambuc CCE->getConstructor()->isDefaultConstructor();
1164f4a2713aSLionel Sambuc else if (Style == CXXNewExpr::ListInit) {
1165f4a2713aSLionel Sambuc assert(isa<InitListExpr>(Init) &&
1166f4a2713aSLionel Sambuc "Shouldn't create list CXXConstructExprs for arrays.");
1167f4a2713aSLionel Sambuc return true;
1168f4a2713aSLionel Sambuc }
1169f4a2713aSLionel Sambuc return false;
1170f4a2713aSLionel Sambuc }
1171f4a2713aSLionel Sambuc
1172f4a2713aSLionel Sambuc ExprResult
BuildCXXNew(SourceRange Range,bool UseGlobal,SourceLocation PlacementLParen,MultiExprArg PlacementArgs,SourceLocation PlacementRParen,SourceRange TypeIdParens,QualType AllocType,TypeSourceInfo * AllocTypeInfo,Expr * ArraySize,SourceRange DirectInitRange,Expr * Initializer,bool TypeMayContainAuto)1173f4a2713aSLionel Sambuc Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
1174f4a2713aSLionel Sambuc SourceLocation PlacementLParen,
1175f4a2713aSLionel Sambuc MultiExprArg PlacementArgs,
1176f4a2713aSLionel Sambuc SourceLocation PlacementRParen,
1177f4a2713aSLionel Sambuc SourceRange TypeIdParens,
1178f4a2713aSLionel Sambuc QualType AllocType,
1179f4a2713aSLionel Sambuc TypeSourceInfo *AllocTypeInfo,
1180f4a2713aSLionel Sambuc Expr *ArraySize,
1181f4a2713aSLionel Sambuc SourceRange DirectInitRange,
1182f4a2713aSLionel Sambuc Expr *Initializer,
1183f4a2713aSLionel Sambuc bool TypeMayContainAuto) {
1184f4a2713aSLionel Sambuc SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange();
1185f4a2713aSLionel Sambuc SourceLocation StartLoc = Range.getBegin();
1186f4a2713aSLionel Sambuc
1187f4a2713aSLionel Sambuc CXXNewExpr::InitializationStyle initStyle;
1188f4a2713aSLionel Sambuc if (DirectInitRange.isValid()) {
1189f4a2713aSLionel Sambuc assert(Initializer && "Have parens but no initializer.");
1190f4a2713aSLionel Sambuc initStyle = CXXNewExpr::CallInit;
1191f4a2713aSLionel Sambuc } else if (Initializer && isa<InitListExpr>(Initializer))
1192f4a2713aSLionel Sambuc initStyle = CXXNewExpr::ListInit;
1193f4a2713aSLionel Sambuc else {
1194f4a2713aSLionel Sambuc assert((!Initializer || isa<ImplicitValueInitExpr>(Initializer) ||
1195f4a2713aSLionel Sambuc isa<CXXConstructExpr>(Initializer)) &&
1196f4a2713aSLionel Sambuc "Initializer expression that cannot have been implicitly created.");
1197f4a2713aSLionel Sambuc initStyle = CXXNewExpr::NoInit;
1198f4a2713aSLionel Sambuc }
1199f4a2713aSLionel Sambuc
1200f4a2713aSLionel Sambuc Expr **Inits = &Initializer;
1201f4a2713aSLionel Sambuc unsigned NumInits = Initializer ? 1 : 0;
1202f4a2713aSLionel Sambuc if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) {
1203f4a2713aSLionel Sambuc assert(initStyle == CXXNewExpr::CallInit && "paren init for non-call init");
1204f4a2713aSLionel Sambuc Inits = List->getExprs();
1205f4a2713aSLionel Sambuc NumInits = List->getNumExprs();
1206f4a2713aSLionel Sambuc }
1207f4a2713aSLionel Sambuc
1208*0a6a1f1dSLionel Sambuc // C++11 [dcl.spec.auto]p6. Deduce the type which 'auto' stands in for.
1209f4a2713aSLionel Sambuc if (TypeMayContainAuto && AllocType->isUndeducedType()) {
1210f4a2713aSLionel Sambuc if (initStyle == CXXNewExpr::NoInit || NumInits == 0)
1211f4a2713aSLionel Sambuc return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg)
1212f4a2713aSLionel Sambuc << AllocType << TypeRange);
1213*0a6a1f1dSLionel Sambuc if (initStyle == CXXNewExpr::ListInit ||
1214*0a6a1f1dSLionel Sambuc (NumInits == 1 && isa<InitListExpr>(Inits[0])))
1215f4a2713aSLionel Sambuc return ExprError(Diag(Inits[0]->getLocStart(),
1216*0a6a1f1dSLionel Sambuc diag::err_auto_new_list_init)
1217f4a2713aSLionel Sambuc << AllocType << TypeRange);
1218f4a2713aSLionel Sambuc if (NumInits > 1) {
1219f4a2713aSLionel Sambuc Expr *FirstBad = Inits[1];
1220f4a2713aSLionel Sambuc return ExprError(Diag(FirstBad->getLocStart(),
1221f4a2713aSLionel Sambuc diag::err_auto_new_ctor_multiple_expressions)
1222f4a2713aSLionel Sambuc << AllocType << TypeRange);
1223f4a2713aSLionel Sambuc }
1224f4a2713aSLionel Sambuc Expr *Deduce = Inits[0];
1225f4a2713aSLionel Sambuc QualType DeducedType;
1226f4a2713aSLionel Sambuc if (DeduceAutoType(AllocTypeInfo, Deduce, DeducedType) == DAR_Failed)
1227f4a2713aSLionel Sambuc return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure)
1228f4a2713aSLionel Sambuc << AllocType << Deduce->getType()
1229f4a2713aSLionel Sambuc << TypeRange << Deduce->getSourceRange());
1230f4a2713aSLionel Sambuc if (DeducedType.isNull())
1231f4a2713aSLionel Sambuc return ExprError();
1232f4a2713aSLionel Sambuc AllocType = DeducedType;
1233f4a2713aSLionel Sambuc }
1234f4a2713aSLionel Sambuc
1235f4a2713aSLionel Sambuc // Per C++0x [expr.new]p5, the type being constructed may be a
1236f4a2713aSLionel Sambuc // typedef of an array type.
1237f4a2713aSLionel Sambuc if (!ArraySize) {
1238f4a2713aSLionel Sambuc if (const ConstantArrayType *Array
1239f4a2713aSLionel Sambuc = Context.getAsConstantArrayType(AllocType)) {
1240f4a2713aSLionel Sambuc ArraySize = IntegerLiteral::Create(Context, Array->getSize(),
1241f4a2713aSLionel Sambuc Context.getSizeType(),
1242f4a2713aSLionel Sambuc TypeRange.getEnd());
1243f4a2713aSLionel Sambuc AllocType = Array->getElementType();
1244f4a2713aSLionel Sambuc }
1245f4a2713aSLionel Sambuc }
1246f4a2713aSLionel Sambuc
1247f4a2713aSLionel Sambuc if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange))
1248f4a2713aSLionel Sambuc return ExprError();
1249f4a2713aSLionel Sambuc
1250*0a6a1f1dSLionel Sambuc if (initStyle == CXXNewExpr::ListInit &&
1251*0a6a1f1dSLionel Sambuc isStdInitializerList(AllocType, nullptr)) {
1252f4a2713aSLionel Sambuc Diag(AllocTypeInfo->getTypeLoc().getBeginLoc(),
1253f4a2713aSLionel Sambuc diag::warn_dangling_std_initializer_list)
1254f4a2713aSLionel Sambuc << /*at end of FE*/0 << Inits[0]->getSourceRange();
1255f4a2713aSLionel Sambuc }
1256f4a2713aSLionel Sambuc
1257f4a2713aSLionel Sambuc // In ARC, infer 'retaining' for the allocated
1258f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount &&
1259f4a2713aSLionel Sambuc AllocType.getObjCLifetime() == Qualifiers::OCL_None &&
1260f4a2713aSLionel Sambuc AllocType->isObjCLifetimeType()) {
1261f4a2713aSLionel Sambuc AllocType = Context.getLifetimeQualifiedType(AllocType,
1262f4a2713aSLionel Sambuc AllocType->getObjCARCImplicitLifetime());
1263f4a2713aSLionel Sambuc }
1264f4a2713aSLionel Sambuc
1265f4a2713aSLionel Sambuc QualType ResultType = Context.getPointerType(AllocType);
1266f4a2713aSLionel Sambuc
1267f4a2713aSLionel Sambuc if (ArraySize && ArraySize->getType()->isNonOverloadPlaceholderType()) {
1268f4a2713aSLionel Sambuc ExprResult result = CheckPlaceholderExpr(ArraySize);
1269f4a2713aSLionel Sambuc if (result.isInvalid()) return ExprError();
1270*0a6a1f1dSLionel Sambuc ArraySize = result.get();
1271f4a2713aSLionel Sambuc }
1272f4a2713aSLionel Sambuc // C++98 5.3.4p6: "The expression in a direct-new-declarator shall have
1273f4a2713aSLionel Sambuc // integral or enumeration type with a non-negative value."
1274f4a2713aSLionel Sambuc // C++11 [expr.new]p6: The expression [...] shall be of integral or unscoped
1275f4a2713aSLionel Sambuc // enumeration type, or a class type for which a single non-explicit
1276f4a2713aSLionel Sambuc // conversion function to integral or unscoped enumeration type exists.
1277f4a2713aSLionel Sambuc // C++1y [expr.new]p6: The expression [...] is implicitly converted to
1278f4a2713aSLionel Sambuc // std::size_t.
1279f4a2713aSLionel Sambuc if (ArraySize && !ArraySize->isTypeDependent()) {
1280f4a2713aSLionel Sambuc ExprResult ConvertedSize;
1281*0a6a1f1dSLionel Sambuc if (getLangOpts().CPlusPlus14) {
1282*0a6a1f1dSLionel Sambuc assert(Context.getTargetInfo().getIntWidth() && "Builtin type of size 0?");
1283*0a6a1f1dSLionel Sambuc
1284f4a2713aSLionel Sambuc ConvertedSize = PerformImplicitConversion(ArraySize, Context.getSizeType(),
1285f4a2713aSLionel Sambuc AA_Converting);
1286f4a2713aSLionel Sambuc
1287f4a2713aSLionel Sambuc if (!ConvertedSize.isInvalid() &&
1288f4a2713aSLionel Sambuc ArraySize->getType()->getAs<RecordType>())
1289f4a2713aSLionel Sambuc // Diagnose the compatibility of this conversion.
1290f4a2713aSLionel Sambuc Diag(StartLoc, diag::warn_cxx98_compat_array_size_conversion)
1291f4a2713aSLionel Sambuc << ArraySize->getType() << 0 << "'size_t'";
1292f4a2713aSLionel Sambuc } else {
1293f4a2713aSLionel Sambuc class SizeConvertDiagnoser : public ICEConvertDiagnoser {
1294f4a2713aSLionel Sambuc protected:
1295f4a2713aSLionel Sambuc Expr *ArraySize;
1296f4a2713aSLionel Sambuc
1297f4a2713aSLionel Sambuc public:
1298f4a2713aSLionel Sambuc SizeConvertDiagnoser(Expr *ArraySize)
1299f4a2713aSLionel Sambuc : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false, false, false),
1300f4a2713aSLionel Sambuc ArraySize(ArraySize) {}
1301f4a2713aSLionel Sambuc
1302*0a6a1f1dSLionel Sambuc SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1303*0a6a1f1dSLionel Sambuc QualType T) override {
1304f4a2713aSLionel Sambuc return S.Diag(Loc, diag::err_array_size_not_integral)
1305f4a2713aSLionel Sambuc << S.getLangOpts().CPlusPlus11 << T;
1306f4a2713aSLionel Sambuc }
1307f4a2713aSLionel Sambuc
1308*0a6a1f1dSLionel Sambuc SemaDiagnosticBuilder diagnoseIncomplete(
1309*0a6a1f1dSLionel Sambuc Sema &S, SourceLocation Loc, QualType T) override {
1310f4a2713aSLionel Sambuc return S.Diag(Loc, diag::err_array_size_incomplete_type)
1311f4a2713aSLionel Sambuc << T << ArraySize->getSourceRange();
1312f4a2713aSLionel Sambuc }
1313f4a2713aSLionel Sambuc
1314*0a6a1f1dSLionel Sambuc SemaDiagnosticBuilder diagnoseExplicitConv(
1315*0a6a1f1dSLionel Sambuc Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override {
1316f4a2713aSLionel Sambuc return S.Diag(Loc, diag::err_array_size_explicit_conversion) << T << ConvTy;
1317f4a2713aSLionel Sambuc }
1318f4a2713aSLionel Sambuc
1319*0a6a1f1dSLionel Sambuc SemaDiagnosticBuilder noteExplicitConv(
1320*0a6a1f1dSLionel Sambuc Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
1321f4a2713aSLionel Sambuc return S.Diag(Conv->getLocation(), diag::note_array_size_conversion)
1322f4a2713aSLionel Sambuc << ConvTy->isEnumeralType() << ConvTy;
1323f4a2713aSLionel Sambuc }
1324f4a2713aSLionel Sambuc
1325*0a6a1f1dSLionel Sambuc SemaDiagnosticBuilder diagnoseAmbiguous(
1326*0a6a1f1dSLionel Sambuc Sema &S, SourceLocation Loc, QualType T) override {
1327f4a2713aSLionel Sambuc return S.Diag(Loc, diag::err_array_size_ambiguous_conversion) << T;
1328f4a2713aSLionel Sambuc }
1329f4a2713aSLionel Sambuc
1330*0a6a1f1dSLionel Sambuc SemaDiagnosticBuilder noteAmbiguous(
1331*0a6a1f1dSLionel Sambuc Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
1332f4a2713aSLionel Sambuc return S.Diag(Conv->getLocation(), diag::note_array_size_conversion)
1333f4a2713aSLionel Sambuc << ConvTy->isEnumeralType() << ConvTy;
1334f4a2713aSLionel Sambuc }
1335f4a2713aSLionel Sambuc
1336f4a2713aSLionel Sambuc virtual SemaDiagnosticBuilder diagnoseConversion(
1337*0a6a1f1dSLionel Sambuc Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override {
1338f4a2713aSLionel Sambuc return S.Diag(Loc,
1339f4a2713aSLionel Sambuc S.getLangOpts().CPlusPlus11
1340f4a2713aSLionel Sambuc ? diag::warn_cxx98_compat_array_size_conversion
1341f4a2713aSLionel Sambuc : diag::ext_array_size_conversion)
1342f4a2713aSLionel Sambuc << T << ConvTy->isEnumeralType() << ConvTy;
1343f4a2713aSLionel Sambuc }
1344f4a2713aSLionel Sambuc } SizeDiagnoser(ArraySize);
1345f4a2713aSLionel Sambuc
1346f4a2713aSLionel Sambuc ConvertedSize = PerformContextualImplicitConversion(StartLoc, ArraySize,
1347f4a2713aSLionel Sambuc SizeDiagnoser);
1348f4a2713aSLionel Sambuc }
1349f4a2713aSLionel Sambuc if (ConvertedSize.isInvalid())
1350f4a2713aSLionel Sambuc return ExprError();
1351f4a2713aSLionel Sambuc
1352*0a6a1f1dSLionel Sambuc ArraySize = ConvertedSize.get();
1353f4a2713aSLionel Sambuc QualType SizeType = ArraySize->getType();
1354f4a2713aSLionel Sambuc
1355f4a2713aSLionel Sambuc if (!SizeType->isIntegralOrUnscopedEnumerationType())
1356f4a2713aSLionel Sambuc return ExprError();
1357f4a2713aSLionel Sambuc
1358f4a2713aSLionel Sambuc // C++98 [expr.new]p7:
1359f4a2713aSLionel Sambuc // The expression in a direct-new-declarator shall have integral type
1360f4a2713aSLionel Sambuc // with a non-negative value.
1361f4a2713aSLionel Sambuc //
1362f4a2713aSLionel Sambuc // Let's see if this is a constant < 0. If so, we reject it out of
1363f4a2713aSLionel Sambuc // hand. Otherwise, if it's not a constant, we must have an unparenthesized
1364f4a2713aSLionel Sambuc // array type.
1365f4a2713aSLionel Sambuc //
1366f4a2713aSLionel Sambuc // Note: such a construct has well-defined semantics in C++11: it throws
1367f4a2713aSLionel Sambuc // std::bad_array_new_length.
1368f4a2713aSLionel Sambuc if (!ArraySize->isValueDependent()) {
1369f4a2713aSLionel Sambuc llvm::APSInt Value;
1370f4a2713aSLionel Sambuc // We've already performed any required implicit conversion to integer or
1371f4a2713aSLionel Sambuc // unscoped enumeration type.
1372f4a2713aSLionel Sambuc if (ArraySize->isIntegerConstantExpr(Value, Context)) {
1373f4a2713aSLionel Sambuc if (Value < llvm::APSInt(
1374f4a2713aSLionel Sambuc llvm::APInt::getNullValue(Value.getBitWidth()),
1375f4a2713aSLionel Sambuc Value.isUnsigned())) {
1376f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11)
1377f4a2713aSLionel Sambuc Diag(ArraySize->getLocStart(),
1378f4a2713aSLionel Sambuc diag::warn_typecheck_negative_array_new_size)
1379f4a2713aSLionel Sambuc << ArraySize->getSourceRange();
1380f4a2713aSLionel Sambuc else
1381f4a2713aSLionel Sambuc return ExprError(Diag(ArraySize->getLocStart(),
1382f4a2713aSLionel Sambuc diag::err_typecheck_negative_array_size)
1383f4a2713aSLionel Sambuc << ArraySize->getSourceRange());
1384f4a2713aSLionel Sambuc } else if (!AllocType->isDependentType()) {
1385f4a2713aSLionel Sambuc unsigned ActiveSizeBits =
1386f4a2713aSLionel Sambuc ConstantArrayType::getNumAddressingBits(Context, AllocType, Value);
1387f4a2713aSLionel Sambuc if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) {
1388f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11)
1389f4a2713aSLionel Sambuc Diag(ArraySize->getLocStart(),
1390f4a2713aSLionel Sambuc diag::warn_array_new_too_large)
1391f4a2713aSLionel Sambuc << Value.toString(10)
1392f4a2713aSLionel Sambuc << ArraySize->getSourceRange();
1393f4a2713aSLionel Sambuc else
1394f4a2713aSLionel Sambuc return ExprError(Diag(ArraySize->getLocStart(),
1395f4a2713aSLionel Sambuc diag::err_array_too_large)
1396f4a2713aSLionel Sambuc << Value.toString(10)
1397f4a2713aSLionel Sambuc << ArraySize->getSourceRange());
1398f4a2713aSLionel Sambuc }
1399f4a2713aSLionel Sambuc }
1400f4a2713aSLionel Sambuc } else if (TypeIdParens.isValid()) {
1401f4a2713aSLionel Sambuc // Can't have dynamic array size when the type-id is in parentheses.
1402f4a2713aSLionel Sambuc Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst)
1403f4a2713aSLionel Sambuc << ArraySize->getSourceRange()
1404f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(TypeIdParens.getBegin())
1405f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(TypeIdParens.getEnd());
1406f4a2713aSLionel Sambuc
1407f4a2713aSLionel Sambuc TypeIdParens = SourceRange();
1408f4a2713aSLionel Sambuc }
1409f4a2713aSLionel Sambuc }
1410f4a2713aSLionel Sambuc
1411f4a2713aSLionel Sambuc // Note that we do *not* convert the argument in any way. It can
1412f4a2713aSLionel Sambuc // be signed, larger than size_t, whatever.
1413f4a2713aSLionel Sambuc }
1414f4a2713aSLionel Sambuc
1415*0a6a1f1dSLionel Sambuc FunctionDecl *OperatorNew = nullptr;
1416*0a6a1f1dSLionel Sambuc FunctionDecl *OperatorDelete = nullptr;
1417f4a2713aSLionel Sambuc
1418f4a2713aSLionel Sambuc if (!AllocType->isDependentType() &&
1419f4a2713aSLionel Sambuc !Expr::hasAnyTypeDependentArguments(PlacementArgs) &&
1420f4a2713aSLionel Sambuc FindAllocationFunctions(StartLoc,
1421f4a2713aSLionel Sambuc SourceRange(PlacementLParen, PlacementRParen),
1422f4a2713aSLionel Sambuc UseGlobal, AllocType, ArraySize, PlacementArgs,
1423f4a2713aSLionel Sambuc OperatorNew, OperatorDelete))
1424f4a2713aSLionel Sambuc return ExprError();
1425f4a2713aSLionel Sambuc
1426f4a2713aSLionel Sambuc // If this is an array allocation, compute whether the usual array
1427f4a2713aSLionel Sambuc // deallocation function for the type has a size_t parameter.
1428f4a2713aSLionel Sambuc bool UsualArrayDeleteWantsSize = false;
1429f4a2713aSLionel Sambuc if (ArraySize && !AllocType->isDependentType())
1430f4a2713aSLionel Sambuc UsualArrayDeleteWantsSize
1431f4a2713aSLionel Sambuc = doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType);
1432f4a2713aSLionel Sambuc
1433f4a2713aSLionel Sambuc SmallVector<Expr *, 8> AllPlaceArgs;
1434f4a2713aSLionel Sambuc if (OperatorNew) {
1435f4a2713aSLionel Sambuc const FunctionProtoType *Proto =
1436f4a2713aSLionel Sambuc OperatorNew->getType()->getAs<FunctionProtoType>();
1437*0a6a1f1dSLionel Sambuc VariadicCallType CallType = Proto->isVariadic() ? VariadicFunction
1438*0a6a1f1dSLionel Sambuc : VariadicDoesNotApply;
1439f4a2713aSLionel Sambuc
1440*0a6a1f1dSLionel Sambuc // We've already converted the placement args, just fill in any default
1441*0a6a1f1dSLionel Sambuc // arguments. Skip the first parameter because we don't have a corresponding
1442*0a6a1f1dSLionel Sambuc // argument.
1443f4a2713aSLionel Sambuc if (GatherArgumentsForCall(PlacementLParen, OperatorNew, Proto, 1,
1444f4a2713aSLionel Sambuc PlacementArgs, AllPlaceArgs, CallType))
1445f4a2713aSLionel Sambuc return ExprError();
1446f4a2713aSLionel Sambuc
1447f4a2713aSLionel Sambuc if (!AllPlaceArgs.empty())
1448f4a2713aSLionel Sambuc PlacementArgs = AllPlaceArgs;
1449f4a2713aSLionel Sambuc
1450*0a6a1f1dSLionel Sambuc // FIXME: This is wrong: PlacementArgs misses out the first (size) argument.
1451f4a2713aSLionel Sambuc DiagnoseSentinelCalls(OperatorNew, PlacementLParen, PlacementArgs);
1452f4a2713aSLionel Sambuc
1453f4a2713aSLionel Sambuc // FIXME: Missing call to CheckFunctionCall or equivalent
1454f4a2713aSLionel Sambuc }
1455f4a2713aSLionel Sambuc
1456f4a2713aSLionel Sambuc // Warn if the type is over-aligned and is being allocated by global operator
1457f4a2713aSLionel Sambuc // new.
1458f4a2713aSLionel Sambuc if (PlacementArgs.empty() && OperatorNew &&
1459f4a2713aSLionel Sambuc (OperatorNew->isImplicit() ||
1460f4a2713aSLionel Sambuc getSourceManager().isInSystemHeader(OperatorNew->getLocStart()))) {
1461f4a2713aSLionel Sambuc if (unsigned Align = Context.getPreferredTypeAlign(AllocType.getTypePtr())){
1462f4a2713aSLionel Sambuc unsigned SuitableAlign = Context.getTargetInfo().getSuitableAlign();
1463f4a2713aSLionel Sambuc if (Align > SuitableAlign)
1464f4a2713aSLionel Sambuc Diag(StartLoc, diag::warn_overaligned_type)
1465f4a2713aSLionel Sambuc << AllocType
1466f4a2713aSLionel Sambuc << unsigned(Align / Context.getCharWidth())
1467f4a2713aSLionel Sambuc << unsigned(SuitableAlign / Context.getCharWidth());
1468f4a2713aSLionel Sambuc }
1469f4a2713aSLionel Sambuc }
1470f4a2713aSLionel Sambuc
1471f4a2713aSLionel Sambuc QualType InitType = AllocType;
1472f4a2713aSLionel Sambuc // Array 'new' can't have any initializers except empty parentheses.
1473f4a2713aSLionel Sambuc // Initializer lists are also allowed, in C++11. Rely on the parser for the
1474f4a2713aSLionel Sambuc // dialect distinction.
1475f4a2713aSLionel Sambuc if (ResultType->isArrayType() || ArraySize) {
1476f4a2713aSLionel Sambuc if (!isLegalArrayNewInitializer(initStyle, Initializer)) {
1477f4a2713aSLionel Sambuc SourceRange InitRange(Inits[0]->getLocStart(),
1478f4a2713aSLionel Sambuc Inits[NumInits - 1]->getLocEnd());
1479f4a2713aSLionel Sambuc Diag(StartLoc, diag::err_new_array_init_args) << InitRange;
1480f4a2713aSLionel Sambuc return ExprError();
1481f4a2713aSLionel Sambuc }
1482f4a2713aSLionel Sambuc if (InitListExpr *ILE = dyn_cast_or_null<InitListExpr>(Initializer)) {
1483f4a2713aSLionel Sambuc // We do the initialization typechecking against the array type
1484f4a2713aSLionel Sambuc // corresponding to the number of initializers + 1 (to also check
1485f4a2713aSLionel Sambuc // default-initialization).
1486f4a2713aSLionel Sambuc unsigned NumElements = ILE->getNumInits() + 1;
1487f4a2713aSLionel Sambuc InitType = Context.getConstantArrayType(AllocType,
1488f4a2713aSLionel Sambuc llvm::APInt(Context.getTypeSize(Context.getSizeType()), NumElements),
1489f4a2713aSLionel Sambuc ArrayType::Normal, 0);
1490f4a2713aSLionel Sambuc }
1491f4a2713aSLionel Sambuc }
1492f4a2713aSLionel Sambuc
1493f4a2713aSLionel Sambuc // If we can perform the initialization, and we've not already done so,
1494f4a2713aSLionel Sambuc // do it now.
1495f4a2713aSLionel Sambuc if (!AllocType->isDependentType() &&
1496f4a2713aSLionel Sambuc !Expr::hasAnyTypeDependentArguments(
1497*0a6a1f1dSLionel Sambuc llvm::makeArrayRef(Inits, NumInits))) {
1498f4a2713aSLionel Sambuc // C++11 [expr.new]p15:
1499f4a2713aSLionel Sambuc // A new-expression that creates an object of type T initializes that
1500f4a2713aSLionel Sambuc // object as follows:
1501f4a2713aSLionel Sambuc InitializationKind Kind
1502f4a2713aSLionel Sambuc // - If the new-initializer is omitted, the object is default-
1503f4a2713aSLionel Sambuc // initialized (8.5); if no initialization is performed,
1504f4a2713aSLionel Sambuc // the object has indeterminate value
1505f4a2713aSLionel Sambuc = initStyle == CXXNewExpr::NoInit
1506f4a2713aSLionel Sambuc ? InitializationKind::CreateDefault(TypeRange.getBegin())
1507f4a2713aSLionel Sambuc // - Otherwise, the new-initializer is interpreted according to the
1508f4a2713aSLionel Sambuc // initialization rules of 8.5 for direct-initialization.
1509f4a2713aSLionel Sambuc : initStyle == CXXNewExpr::ListInit
1510f4a2713aSLionel Sambuc ? InitializationKind::CreateDirectList(TypeRange.getBegin())
1511f4a2713aSLionel Sambuc : InitializationKind::CreateDirect(TypeRange.getBegin(),
1512f4a2713aSLionel Sambuc DirectInitRange.getBegin(),
1513f4a2713aSLionel Sambuc DirectInitRange.getEnd());
1514f4a2713aSLionel Sambuc
1515f4a2713aSLionel Sambuc InitializedEntity Entity
1516f4a2713aSLionel Sambuc = InitializedEntity::InitializeNew(StartLoc, InitType);
1517f4a2713aSLionel Sambuc InitializationSequence InitSeq(*this, Entity, Kind, MultiExprArg(Inits, NumInits));
1518f4a2713aSLionel Sambuc ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind,
1519f4a2713aSLionel Sambuc MultiExprArg(Inits, NumInits));
1520f4a2713aSLionel Sambuc if (FullInit.isInvalid())
1521f4a2713aSLionel Sambuc return ExprError();
1522f4a2713aSLionel Sambuc
1523f4a2713aSLionel Sambuc // FullInit is our initializer; strip off CXXBindTemporaryExprs, because
1524f4a2713aSLionel Sambuc // we don't want the initialized object to be destructed.
1525f4a2713aSLionel Sambuc if (CXXBindTemporaryExpr *Binder =
1526f4a2713aSLionel Sambuc dyn_cast_or_null<CXXBindTemporaryExpr>(FullInit.get()))
1527*0a6a1f1dSLionel Sambuc FullInit = Binder->getSubExpr();
1528f4a2713aSLionel Sambuc
1529*0a6a1f1dSLionel Sambuc Initializer = FullInit.get();
1530f4a2713aSLionel Sambuc }
1531f4a2713aSLionel Sambuc
1532f4a2713aSLionel Sambuc // Mark the new and delete operators as referenced.
1533f4a2713aSLionel Sambuc if (OperatorNew) {
1534f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(OperatorNew, StartLoc))
1535f4a2713aSLionel Sambuc return ExprError();
1536f4a2713aSLionel Sambuc MarkFunctionReferenced(StartLoc, OperatorNew);
1537f4a2713aSLionel Sambuc }
1538f4a2713aSLionel Sambuc if (OperatorDelete) {
1539f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(OperatorDelete, StartLoc))
1540f4a2713aSLionel Sambuc return ExprError();
1541f4a2713aSLionel Sambuc MarkFunctionReferenced(StartLoc, OperatorDelete);
1542f4a2713aSLionel Sambuc }
1543f4a2713aSLionel Sambuc
1544f4a2713aSLionel Sambuc // C++0x [expr.new]p17:
1545f4a2713aSLionel Sambuc // If the new expression creates an array of objects of class type,
1546f4a2713aSLionel Sambuc // access and ambiguity control are done for the destructor.
1547f4a2713aSLionel Sambuc QualType BaseAllocType = Context.getBaseElementType(AllocType);
1548f4a2713aSLionel Sambuc if (ArraySize && !BaseAllocType->isDependentType()) {
1549f4a2713aSLionel Sambuc if (const RecordType *BaseRecordType = BaseAllocType->getAs<RecordType>()) {
1550f4a2713aSLionel Sambuc if (CXXDestructorDecl *dtor = LookupDestructor(
1551f4a2713aSLionel Sambuc cast<CXXRecordDecl>(BaseRecordType->getDecl()))) {
1552f4a2713aSLionel Sambuc MarkFunctionReferenced(StartLoc, dtor);
1553f4a2713aSLionel Sambuc CheckDestructorAccess(StartLoc, dtor,
1554f4a2713aSLionel Sambuc PDiag(diag::err_access_dtor)
1555f4a2713aSLionel Sambuc << BaseAllocType);
1556f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(dtor, StartLoc))
1557f4a2713aSLionel Sambuc return ExprError();
1558f4a2713aSLionel Sambuc }
1559f4a2713aSLionel Sambuc }
1560f4a2713aSLionel Sambuc }
1561f4a2713aSLionel Sambuc
1562*0a6a1f1dSLionel Sambuc return new (Context)
1563*0a6a1f1dSLionel Sambuc CXXNewExpr(Context, UseGlobal, OperatorNew, OperatorDelete,
1564*0a6a1f1dSLionel Sambuc UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens,
1565*0a6a1f1dSLionel Sambuc ArraySize, initStyle, Initializer, ResultType, AllocTypeInfo,
1566*0a6a1f1dSLionel Sambuc Range, DirectInitRange);
1567f4a2713aSLionel Sambuc }
1568f4a2713aSLionel Sambuc
1569f4a2713aSLionel Sambuc /// \brief Checks that a type is suitable as the allocated type
1570f4a2713aSLionel Sambuc /// in a new-expression.
CheckAllocatedType(QualType AllocType,SourceLocation Loc,SourceRange R)1571f4a2713aSLionel Sambuc bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
1572f4a2713aSLionel Sambuc SourceRange R) {
1573f4a2713aSLionel Sambuc // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
1574f4a2713aSLionel Sambuc // abstract class type or array thereof.
1575f4a2713aSLionel Sambuc if (AllocType->isFunctionType())
1576f4a2713aSLionel Sambuc return Diag(Loc, diag::err_bad_new_type)
1577f4a2713aSLionel Sambuc << AllocType << 0 << R;
1578f4a2713aSLionel Sambuc else if (AllocType->isReferenceType())
1579f4a2713aSLionel Sambuc return Diag(Loc, diag::err_bad_new_type)
1580f4a2713aSLionel Sambuc << AllocType << 1 << R;
1581f4a2713aSLionel Sambuc else if (!AllocType->isDependentType() &&
1582f4a2713aSLionel Sambuc RequireCompleteType(Loc, AllocType, diag::err_new_incomplete_type,R))
1583f4a2713aSLionel Sambuc return true;
1584f4a2713aSLionel Sambuc else if (RequireNonAbstractType(Loc, AllocType,
1585f4a2713aSLionel Sambuc diag::err_allocation_of_abstract_type))
1586f4a2713aSLionel Sambuc return true;
1587f4a2713aSLionel Sambuc else if (AllocType->isVariablyModifiedType())
1588f4a2713aSLionel Sambuc return Diag(Loc, diag::err_variably_modified_new_type)
1589f4a2713aSLionel Sambuc << AllocType;
1590f4a2713aSLionel Sambuc else if (unsigned AddressSpace = AllocType.getAddressSpace())
1591f4a2713aSLionel Sambuc return Diag(Loc, diag::err_address_space_qualified_new)
1592f4a2713aSLionel Sambuc << AllocType.getUnqualifiedType() << AddressSpace;
1593f4a2713aSLionel Sambuc else if (getLangOpts().ObjCAutoRefCount) {
1594f4a2713aSLionel Sambuc if (const ArrayType *AT = Context.getAsArrayType(AllocType)) {
1595f4a2713aSLionel Sambuc QualType BaseAllocType = Context.getBaseElementType(AT);
1596f4a2713aSLionel Sambuc if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None &&
1597f4a2713aSLionel Sambuc BaseAllocType->isObjCLifetimeType())
1598f4a2713aSLionel Sambuc return Diag(Loc, diag::err_arc_new_array_without_ownership)
1599f4a2713aSLionel Sambuc << BaseAllocType;
1600f4a2713aSLionel Sambuc }
1601f4a2713aSLionel Sambuc }
1602f4a2713aSLionel Sambuc
1603f4a2713aSLionel Sambuc return false;
1604f4a2713aSLionel Sambuc }
1605f4a2713aSLionel Sambuc
1606f4a2713aSLionel Sambuc /// \brief Determine whether the given function is a non-placement
1607f4a2713aSLionel Sambuc /// deallocation function.
isNonPlacementDeallocationFunction(Sema & S,FunctionDecl * FD)1608f4a2713aSLionel Sambuc static bool isNonPlacementDeallocationFunction(Sema &S, FunctionDecl *FD) {
1609f4a2713aSLionel Sambuc if (FD->isInvalidDecl())
1610f4a2713aSLionel Sambuc return false;
1611f4a2713aSLionel Sambuc
1612f4a2713aSLionel Sambuc if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD))
1613f4a2713aSLionel Sambuc return Method->isUsualDeallocationFunction();
1614f4a2713aSLionel Sambuc
1615f4a2713aSLionel Sambuc if (FD->getOverloadedOperator() != OO_Delete &&
1616f4a2713aSLionel Sambuc FD->getOverloadedOperator() != OO_Array_Delete)
1617f4a2713aSLionel Sambuc return false;
1618f4a2713aSLionel Sambuc
1619f4a2713aSLionel Sambuc if (FD->getNumParams() == 1)
1620f4a2713aSLionel Sambuc return true;
1621f4a2713aSLionel Sambuc
1622f4a2713aSLionel Sambuc return S.getLangOpts().SizedDeallocation && FD->getNumParams() == 2 &&
1623f4a2713aSLionel Sambuc S.Context.hasSameUnqualifiedType(FD->getParamDecl(1)->getType(),
1624f4a2713aSLionel Sambuc S.Context.getSizeType());
1625f4a2713aSLionel Sambuc }
1626f4a2713aSLionel Sambuc
1627f4a2713aSLionel Sambuc /// FindAllocationFunctions - Finds the overloads of operator new and delete
1628f4a2713aSLionel Sambuc /// that are appropriate for the allocation.
FindAllocationFunctions(SourceLocation StartLoc,SourceRange Range,bool UseGlobal,QualType AllocType,bool IsArray,MultiExprArg PlaceArgs,FunctionDecl * & OperatorNew,FunctionDecl * & OperatorDelete)1629f4a2713aSLionel Sambuc bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range,
1630f4a2713aSLionel Sambuc bool UseGlobal, QualType AllocType,
1631f4a2713aSLionel Sambuc bool IsArray, MultiExprArg PlaceArgs,
1632f4a2713aSLionel Sambuc FunctionDecl *&OperatorNew,
1633f4a2713aSLionel Sambuc FunctionDecl *&OperatorDelete) {
1634f4a2713aSLionel Sambuc // --- Choosing an allocation function ---
1635f4a2713aSLionel Sambuc // C++ 5.3.4p8 - 14 & 18
1636f4a2713aSLionel Sambuc // 1) If UseGlobal is true, only look in the global scope. Else, also look
1637f4a2713aSLionel Sambuc // in the scope of the allocated class.
1638f4a2713aSLionel Sambuc // 2) If an array size is given, look for operator new[], else look for
1639f4a2713aSLionel Sambuc // operator new.
1640f4a2713aSLionel Sambuc // 3) The first argument is always size_t. Append the arguments from the
1641f4a2713aSLionel Sambuc // placement form.
1642f4a2713aSLionel Sambuc
1643f4a2713aSLionel Sambuc SmallVector<Expr*, 8> AllocArgs(1 + PlaceArgs.size());
1644f4a2713aSLionel Sambuc // We don't care about the actual value of this argument.
1645f4a2713aSLionel Sambuc // FIXME: Should the Sema create the expression and embed it in the syntax
1646f4a2713aSLionel Sambuc // tree? Or should the consumer just recalculate the value?
1647f4a2713aSLionel Sambuc IntegerLiteral Size(Context, llvm::APInt::getNullValue(
1648f4a2713aSLionel Sambuc Context.getTargetInfo().getPointerWidth(0)),
1649f4a2713aSLionel Sambuc Context.getSizeType(),
1650f4a2713aSLionel Sambuc SourceLocation());
1651f4a2713aSLionel Sambuc AllocArgs[0] = &Size;
1652f4a2713aSLionel Sambuc std::copy(PlaceArgs.begin(), PlaceArgs.end(), AllocArgs.begin() + 1);
1653f4a2713aSLionel Sambuc
1654f4a2713aSLionel Sambuc // C++ [expr.new]p8:
1655f4a2713aSLionel Sambuc // If the allocated type is a non-array type, the allocation
1656f4a2713aSLionel Sambuc // function's name is operator new and the deallocation function's
1657f4a2713aSLionel Sambuc // name is operator delete. If the allocated type is an array
1658f4a2713aSLionel Sambuc // type, the allocation function's name is operator new[] and the
1659f4a2713aSLionel Sambuc // deallocation function's name is operator delete[].
1660f4a2713aSLionel Sambuc DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName(
1661f4a2713aSLionel Sambuc IsArray ? OO_Array_New : OO_New);
1662f4a2713aSLionel Sambuc DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName(
1663f4a2713aSLionel Sambuc IsArray ? OO_Array_Delete : OO_Delete);
1664f4a2713aSLionel Sambuc
1665f4a2713aSLionel Sambuc QualType AllocElemType = Context.getBaseElementType(AllocType);
1666f4a2713aSLionel Sambuc
1667f4a2713aSLionel Sambuc if (AllocElemType->isRecordType() && !UseGlobal) {
1668f4a2713aSLionel Sambuc CXXRecordDecl *Record
1669f4a2713aSLionel Sambuc = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl());
1670f4a2713aSLionel Sambuc if (FindAllocationOverload(StartLoc, Range, NewName, AllocArgs, Record,
1671f4a2713aSLionel Sambuc /*AllowMissing=*/true, OperatorNew))
1672f4a2713aSLionel Sambuc return true;
1673f4a2713aSLionel Sambuc }
1674f4a2713aSLionel Sambuc
1675f4a2713aSLionel Sambuc if (!OperatorNew) {
1676f4a2713aSLionel Sambuc // Didn't find a member overload. Look for a global one.
1677f4a2713aSLionel Sambuc DeclareGlobalNewDelete();
1678f4a2713aSLionel Sambuc DeclContext *TUDecl = Context.getTranslationUnitDecl();
1679*0a6a1f1dSLionel Sambuc bool FallbackEnabled = IsArray && Context.getLangOpts().MSVCCompat;
1680f4a2713aSLionel Sambuc if (FindAllocationOverload(StartLoc, Range, NewName, AllocArgs, TUDecl,
1681f4a2713aSLionel Sambuc /*AllowMissing=*/FallbackEnabled, OperatorNew,
1682f4a2713aSLionel Sambuc /*Diagnose=*/!FallbackEnabled)) {
1683f4a2713aSLionel Sambuc if (!FallbackEnabled)
1684f4a2713aSLionel Sambuc return true;
1685f4a2713aSLionel Sambuc
1686f4a2713aSLionel Sambuc // MSVC will fall back on trying to find a matching global operator new
1687f4a2713aSLionel Sambuc // if operator new[] cannot be found. Also, MSVC will leak by not
1688f4a2713aSLionel Sambuc // generating a call to operator delete or operator delete[], but we
1689f4a2713aSLionel Sambuc // will not replicate that bug.
1690f4a2713aSLionel Sambuc NewName = Context.DeclarationNames.getCXXOperatorName(OO_New);
1691f4a2713aSLionel Sambuc DeleteName = Context.DeclarationNames.getCXXOperatorName(OO_Delete);
1692f4a2713aSLionel Sambuc if (FindAllocationOverload(StartLoc, Range, NewName, AllocArgs, TUDecl,
1693f4a2713aSLionel Sambuc /*AllowMissing=*/false, OperatorNew))
1694f4a2713aSLionel Sambuc return true;
1695f4a2713aSLionel Sambuc }
1696f4a2713aSLionel Sambuc }
1697f4a2713aSLionel Sambuc
1698f4a2713aSLionel Sambuc // We don't need an operator delete if we're running under
1699f4a2713aSLionel Sambuc // -fno-exceptions.
1700f4a2713aSLionel Sambuc if (!getLangOpts().Exceptions) {
1701*0a6a1f1dSLionel Sambuc OperatorDelete = nullptr;
1702f4a2713aSLionel Sambuc return false;
1703f4a2713aSLionel Sambuc }
1704f4a2713aSLionel Sambuc
1705f4a2713aSLionel Sambuc // C++ [expr.new]p19:
1706f4a2713aSLionel Sambuc //
1707f4a2713aSLionel Sambuc // If the new-expression begins with a unary :: operator, the
1708f4a2713aSLionel Sambuc // deallocation function's name is looked up in the global
1709f4a2713aSLionel Sambuc // scope. Otherwise, if the allocated type is a class type T or an
1710f4a2713aSLionel Sambuc // array thereof, the deallocation function's name is looked up in
1711f4a2713aSLionel Sambuc // the scope of T. If this lookup fails to find the name, or if
1712f4a2713aSLionel Sambuc // the allocated type is not a class type or array thereof, the
1713f4a2713aSLionel Sambuc // deallocation function's name is looked up in the global scope.
1714f4a2713aSLionel Sambuc LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName);
1715f4a2713aSLionel Sambuc if (AllocElemType->isRecordType() && !UseGlobal) {
1716f4a2713aSLionel Sambuc CXXRecordDecl *RD
1717f4a2713aSLionel Sambuc = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl());
1718f4a2713aSLionel Sambuc LookupQualifiedName(FoundDelete, RD);
1719f4a2713aSLionel Sambuc }
1720f4a2713aSLionel Sambuc if (FoundDelete.isAmbiguous())
1721f4a2713aSLionel Sambuc return true; // FIXME: clean up expressions?
1722f4a2713aSLionel Sambuc
1723f4a2713aSLionel Sambuc if (FoundDelete.empty()) {
1724f4a2713aSLionel Sambuc DeclareGlobalNewDelete();
1725f4a2713aSLionel Sambuc LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl());
1726f4a2713aSLionel Sambuc }
1727f4a2713aSLionel Sambuc
1728f4a2713aSLionel Sambuc FoundDelete.suppressDiagnostics();
1729f4a2713aSLionel Sambuc
1730f4a2713aSLionel Sambuc SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches;
1731f4a2713aSLionel Sambuc
1732f4a2713aSLionel Sambuc // Whether we're looking for a placement operator delete is dictated
1733f4a2713aSLionel Sambuc // by whether we selected a placement operator new, not by whether
1734f4a2713aSLionel Sambuc // we had explicit placement arguments. This matters for things like
1735f4a2713aSLionel Sambuc // struct A { void *operator new(size_t, int = 0); ... };
1736f4a2713aSLionel Sambuc // A *a = new A()
1737f4a2713aSLionel Sambuc bool isPlacementNew = (!PlaceArgs.empty() || OperatorNew->param_size() != 1);
1738f4a2713aSLionel Sambuc
1739f4a2713aSLionel Sambuc if (isPlacementNew) {
1740f4a2713aSLionel Sambuc // C++ [expr.new]p20:
1741f4a2713aSLionel Sambuc // A declaration of a placement deallocation function matches the
1742f4a2713aSLionel Sambuc // declaration of a placement allocation function if it has the
1743f4a2713aSLionel Sambuc // same number of parameters and, after parameter transformations
1744f4a2713aSLionel Sambuc // (8.3.5), all parameter types except the first are
1745f4a2713aSLionel Sambuc // identical. [...]
1746f4a2713aSLionel Sambuc //
1747f4a2713aSLionel Sambuc // To perform this comparison, we compute the function type that
1748f4a2713aSLionel Sambuc // the deallocation function should have, and use that type both
1749f4a2713aSLionel Sambuc // for template argument deduction and for comparison purposes.
1750f4a2713aSLionel Sambuc //
1751f4a2713aSLionel Sambuc // FIXME: this comparison should ignore CC and the like.
1752f4a2713aSLionel Sambuc QualType ExpectedFunctionType;
1753f4a2713aSLionel Sambuc {
1754f4a2713aSLionel Sambuc const FunctionProtoType *Proto
1755f4a2713aSLionel Sambuc = OperatorNew->getType()->getAs<FunctionProtoType>();
1756f4a2713aSLionel Sambuc
1757f4a2713aSLionel Sambuc SmallVector<QualType, 4> ArgTypes;
1758f4a2713aSLionel Sambuc ArgTypes.push_back(Context.VoidPtrTy);
1759*0a6a1f1dSLionel Sambuc for (unsigned I = 1, N = Proto->getNumParams(); I < N; ++I)
1760*0a6a1f1dSLionel Sambuc ArgTypes.push_back(Proto->getParamType(I));
1761f4a2713aSLionel Sambuc
1762f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI;
1763f4a2713aSLionel Sambuc EPI.Variadic = Proto->isVariadic();
1764f4a2713aSLionel Sambuc
1765f4a2713aSLionel Sambuc ExpectedFunctionType
1766f4a2713aSLionel Sambuc = Context.getFunctionType(Context.VoidTy, ArgTypes, EPI);
1767f4a2713aSLionel Sambuc }
1768f4a2713aSLionel Sambuc
1769f4a2713aSLionel Sambuc for (LookupResult::iterator D = FoundDelete.begin(),
1770f4a2713aSLionel Sambuc DEnd = FoundDelete.end();
1771f4a2713aSLionel Sambuc D != DEnd; ++D) {
1772*0a6a1f1dSLionel Sambuc FunctionDecl *Fn = nullptr;
1773f4a2713aSLionel Sambuc if (FunctionTemplateDecl *FnTmpl
1774f4a2713aSLionel Sambuc = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) {
1775f4a2713aSLionel Sambuc // Perform template argument deduction to try to match the
1776f4a2713aSLionel Sambuc // expected function type.
1777f4a2713aSLionel Sambuc TemplateDeductionInfo Info(StartLoc);
1778*0a6a1f1dSLionel Sambuc if (DeduceTemplateArguments(FnTmpl, nullptr, ExpectedFunctionType, Fn,
1779*0a6a1f1dSLionel Sambuc Info))
1780f4a2713aSLionel Sambuc continue;
1781f4a2713aSLionel Sambuc } else
1782f4a2713aSLionel Sambuc Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl());
1783f4a2713aSLionel Sambuc
1784f4a2713aSLionel Sambuc if (Context.hasSameType(Fn->getType(), ExpectedFunctionType))
1785f4a2713aSLionel Sambuc Matches.push_back(std::make_pair(D.getPair(), Fn));
1786f4a2713aSLionel Sambuc }
1787f4a2713aSLionel Sambuc } else {
1788f4a2713aSLionel Sambuc // C++ [expr.new]p20:
1789f4a2713aSLionel Sambuc // [...] Any non-placement deallocation function matches a
1790f4a2713aSLionel Sambuc // non-placement allocation function. [...]
1791f4a2713aSLionel Sambuc for (LookupResult::iterator D = FoundDelete.begin(),
1792f4a2713aSLionel Sambuc DEnd = FoundDelete.end();
1793f4a2713aSLionel Sambuc D != DEnd; ++D) {
1794f4a2713aSLionel Sambuc if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl()))
1795f4a2713aSLionel Sambuc if (isNonPlacementDeallocationFunction(*this, Fn))
1796f4a2713aSLionel Sambuc Matches.push_back(std::make_pair(D.getPair(), Fn));
1797f4a2713aSLionel Sambuc }
1798f4a2713aSLionel Sambuc
1799f4a2713aSLionel Sambuc // C++1y [expr.new]p22:
1800f4a2713aSLionel Sambuc // For a non-placement allocation function, the normal deallocation
1801f4a2713aSLionel Sambuc // function lookup is used
1802f4a2713aSLionel Sambuc // C++1y [expr.delete]p?:
1803f4a2713aSLionel Sambuc // If [...] deallocation function lookup finds both a usual deallocation
1804f4a2713aSLionel Sambuc // function with only a pointer parameter and a usual deallocation
1805f4a2713aSLionel Sambuc // function with both a pointer parameter and a size parameter, then the
1806f4a2713aSLionel Sambuc // selected deallocation function shall be the one with two parameters.
1807f4a2713aSLionel Sambuc // Otherwise, the selected deallocation function shall be the function
1808f4a2713aSLionel Sambuc // with one parameter.
1809f4a2713aSLionel Sambuc if (getLangOpts().SizedDeallocation && Matches.size() == 2) {
1810f4a2713aSLionel Sambuc if (Matches[0].second->getNumParams() == 1)
1811f4a2713aSLionel Sambuc Matches.erase(Matches.begin());
1812f4a2713aSLionel Sambuc else
1813f4a2713aSLionel Sambuc Matches.erase(Matches.begin() + 1);
1814f4a2713aSLionel Sambuc assert(Matches[0].second->getNumParams() == 2 &&
1815*0a6a1f1dSLionel Sambuc "found an unexpected usual deallocation function");
1816f4a2713aSLionel Sambuc }
1817f4a2713aSLionel Sambuc }
1818f4a2713aSLionel Sambuc
1819f4a2713aSLionel Sambuc // C++ [expr.new]p20:
1820f4a2713aSLionel Sambuc // [...] If the lookup finds a single matching deallocation
1821f4a2713aSLionel Sambuc // function, that function will be called; otherwise, no
1822f4a2713aSLionel Sambuc // deallocation function will be called.
1823f4a2713aSLionel Sambuc if (Matches.size() == 1) {
1824f4a2713aSLionel Sambuc OperatorDelete = Matches[0].second;
1825f4a2713aSLionel Sambuc
1826f4a2713aSLionel Sambuc // C++0x [expr.new]p20:
1827f4a2713aSLionel Sambuc // If the lookup finds the two-parameter form of a usual
1828f4a2713aSLionel Sambuc // deallocation function (3.7.4.2) and that function, considered
1829f4a2713aSLionel Sambuc // as a placement deallocation function, would have been
1830f4a2713aSLionel Sambuc // selected as a match for the allocation function, the program
1831f4a2713aSLionel Sambuc // is ill-formed.
1832f4a2713aSLionel Sambuc if (!PlaceArgs.empty() && getLangOpts().CPlusPlus11 &&
1833f4a2713aSLionel Sambuc isNonPlacementDeallocationFunction(*this, OperatorDelete)) {
1834f4a2713aSLionel Sambuc Diag(StartLoc, diag::err_placement_new_non_placement_delete)
1835f4a2713aSLionel Sambuc << SourceRange(PlaceArgs.front()->getLocStart(),
1836f4a2713aSLionel Sambuc PlaceArgs.back()->getLocEnd());
1837f4a2713aSLionel Sambuc if (!OperatorDelete->isImplicit())
1838f4a2713aSLionel Sambuc Diag(OperatorDelete->getLocation(), diag::note_previous_decl)
1839f4a2713aSLionel Sambuc << DeleteName;
1840f4a2713aSLionel Sambuc } else {
1841f4a2713aSLionel Sambuc CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(),
1842f4a2713aSLionel Sambuc Matches[0].first);
1843f4a2713aSLionel Sambuc }
1844f4a2713aSLionel Sambuc }
1845f4a2713aSLionel Sambuc
1846f4a2713aSLionel Sambuc return false;
1847f4a2713aSLionel Sambuc }
1848f4a2713aSLionel Sambuc
1849*0a6a1f1dSLionel Sambuc /// \brief Find an fitting overload for the allocation function
1850*0a6a1f1dSLionel Sambuc /// in the specified scope.
1851*0a6a1f1dSLionel Sambuc ///
1852*0a6a1f1dSLionel Sambuc /// \param StartLoc The location of the 'new' token.
1853*0a6a1f1dSLionel Sambuc /// \param Range The range of the placement arguments.
1854*0a6a1f1dSLionel Sambuc /// \param Name The name of the function ('operator new' or 'operator new[]').
1855*0a6a1f1dSLionel Sambuc /// \param Args The placement arguments specified.
1856*0a6a1f1dSLionel Sambuc /// \param Ctx The scope in which we should search; either a class scope or the
1857*0a6a1f1dSLionel Sambuc /// translation unit.
1858*0a6a1f1dSLionel Sambuc /// \param AllowMissing If \c true, report an error if we can't find any
1859*0a6a1f1dSLionel Sambuc /// allocation functions. Otherwise, succeed but don't fill in \p
1860*0a6a1f1dSLionel Sambuc /// Operator.
1861*0a6a1f1dSLionel Sambuc /// \param Operator Filled in with the found allocation function. Unchanged if
1862*0a6a1f1dSLionel Sambuc /// no allocation function was found.
1863*0a6a1f1dSLionel Sambuc /// \param Diagnose If \c true, issue errors if the allocation function is not
1864*0a6a1f1dSLionel Sambuc /// usable.
FindAllocationOverload(SourceLocation StartLoc,SourceRange Range,DeclarationName Name,MultiExprArg Args,DeclContext * Ctx,bool AllowMissing,FunctionDecl * & Operator,bool Diagnose)1865f4a2713aSLionel Sambuc bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range,
1866f4a2713aSLionel Sambuc DeclarationName Name, MultiExprArg Args,
1867f4a2713aSLionel Sambuc DeclContext *Ctx,
1868f4a2713aSLionel Sambuc bool AllowMissing, FunctionDecl *&Operator,
1869f4a2713aSLionel Sambuc bool Diagnose) {
1870f4a2713aSLionel Sambuc LookupResult R(*this, Name, StartLoc, LookupOrdinaryName);
1871f4a2713aSLionel Sambuc LookupQualifiedName(R, Ctx);
1872f4a2713aSLionel Sambuc if (R.empty()) {
1873f4a2713aSLionel Sambuc if (AllowMissing || !Diagnose)
1874f4a2713aSLionel Sambuc return false;
1875f4a2713aSLionel Sambuc return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
1876f4a2713aSLionel Sambuc << Name << Range;
1877f4a2713aSLionel Sambuc }
1878f4a2713aSLionel Sambuc
1879f4a2713aSLionel Sambuc if (R.isAmbiguous())
1880f4a2713aSLionel Sambuc return true;
1881f4a2713aSLionel Sambuc
1882f4a2713aSLionel Sambuc R.suppressDiagnostics();
1883f4a2713aSLionel Sambuc
1884*0a6a1f1dSLionel Sambuc OverloadCandidateSet Candidates(StartLoc, OverloadCandidateSet::CSK_Normal);
1885f4a2713aSLionel Sambuc for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end();
1886f4a2713aSLionel Sambuc Alloc != AllocEnd; ++Alloc) {
1887f4a2713aSLionel Sambuc // Even member operator new/delete are implicitly treated as
1888f4a2713aSLionel Sambuc // static, so don't use AddMemberCandidate.
1889f4a2713aSLionel Sambuc NamedDecl *D = (*Alloc)->getUnderlyingDecl();
1890f4a2713aSLionel Sambuc
1891f4a2713aSLionel Sambuc if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) {
1892f4a2713aSLionel Sambuc AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(),
1893*0a6a1f1dSLionel Sambuc /*ExplicitTemplateArgs=*/nullptr,
1894f4a2713aSLionel Sambuc Args, Candidates,
1895f4a2713aSLionel Sambuc /*SuppressUserConversions=*/false);
1896f4a2713aSLionel Sambuc continue;
1897f4a2713aSLionel Sambuc }
1898f4a2713aSLionel Sambuc
1899f4a2713aSLionel Sambuc FunctionDecl *Fn = cast<FunctionDecl>(D);
1900f4a2713aSLionel Sambuc AddOverloadCandidate(Fn, Alloc.getPair(), Args, Candidates,
1901f4a2713aSLionel Sambuc /*SuppressUserConversions=*/false);
1902f4a2713aSLionel Sambuc }
1903f4a2713aSLionel Sambuc
1904f4a2713aSLionel Sambuc // Do the resolution.
1905f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
1906f4a2713aSLionel Sambuc switch (Candidates.BestViableFunction(*this, StartLoc, Best)) {
1907f4a2713aSLionel Sambuc case OR_Success: {
1908f4a2713aSLionel Sambuc // Got one!
1909f4a2713aSLionel Sambuc FunctionDecl *FnDecl = Best->Function;
1910f4a2713aSLionel Sambuc if (CheckAllocationAccess(StartLoc, Range, R.getNamingClass(),
1911f4a2713aSLionel Sambuc Best->FoundDecl, Diagnose) == AR_inaccessible)
1912f4a2713aSLionel Sambuc return true;
1913f4a2713aSLionel Sambuc
1914*0a6a1f1dSLionel Sambuc Operator = FnDecl;
1915f4a2713aSLionel Sambuc return false;
1916f4a2713aSLionel Sambuc }
1917f4a2713aSLionel Sambuc
1918f4a2713aSLionel Sambuc case OR_No_Viable_Function:
1919f4a2713aSLionel Sambuc if (Diagnose) {
1920f4a2713aSLionel Sambuc Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
1921f4a2713aSLionel Sambuc << Name << Range;
1922f4a2713aSLionel Sambuc Candidates.NoteCandidates(*this, OCD_AllCandidates, Args);
1923f4a2713aSLionel Sambuc }
1924f4a2713aSLionel Sambuc return true;
1925f4a2713aSLionel Sambuc
1926f4a2713aSLionel Sambuc case OR_Ambiguous:
1927f4a2713aSLionel Sambuc if (Diagnose) {
1928f4a2713aSLionel Sambuc Diag(StartLoc, diag::err_ovl_ambiguous_call)
1929f4a2713aSLionel Sambuc << Name << Range;
1930f4a2713aSLionel Sambuc Candidates.NoteCandidates(*this, OCD_ViableCandidates, Args);
1931f4a2713aSLionel Sambuc }
1932f4a2713aSLionel Sambuc return true;
1933f4a2713aSLionel Sambuc
1934f4a2713aSLionel Sambuc case OR_Deleted: {
1935f4a2713aSLionel Sambuc if (Diagnose) {
1936f4a2713aSLionel Sambuc Diag(StartLoc, diag::err_ovl_deleted_call)
1937f4a2713aSLionel Sambuc << Best->Function->isDeleted()
1938f4a2713aSLionel Sambuc << Name
1939f4a2713aSLionel Sambuc << getDeletedOrUnavailableSuffix(Best->Function)
1940f4a2713aSLionel Sambuc << Range;
1941f4a2713aSLionel Sambuc Candidates.NoteCandidates(*this, OCD_AllCandidates, Args);
1942f4a2713aSLionel Sambuc }
1943f4a2713aSLionel Sambuc return true;
1944f4a2713aSLionel Sambuc }
1945f4a2713aSLionel Sambuc }
1946f4a2713aSLionel Sambuc llvm_unreachable("Unreachable, bad result from BestViableFunction");
1947f4a2713aSLionel Sambuc }
1948f4a2713aSLionel Sambuc
1949f4a2713aSLionel Sambuc
1950f4a2713aSLionel Sambuc /// DeclareGlobalNewDelete - Declare the global forms of operator new and
1951f4a2713aSLionel Sambuc /// delete. These are:
1952f4a2713aSLionel Sambuc /// @code
1953f4a2713aSLionel Sambuc /// // C++03:
1954f4a2713aSLionel Sambuc /// void* operator new(std::size_t) throw(std::bad_alloc);
1955f4a2713aSLionel Sambuc /// void* operator new[](std::size_t) throw(std::bad_alloc);
1956f4a2713aSLionel Sambuc /// void operator delete(void *) throw();
1957f4a2713aSLionel Sambuc /// void operator delete[](void *) throw();
1958f4a2713aSLionel Sambuc /// // C++11:
1959f4a2713aSLionel Sambuc /// void* operator new(std::size_t);
1960f4a2713aSLionel Sambuc /// void* operator new[](std::size_t);
1961f4a2713aSLionel Sambuc /// void operator delete(void *) noexcept;
1962f4a2713aSLionel Sambuc /// void operator delete[](void *) noexcept;
1963f4a2713aSLionel Sambuc /// // C++1y:
1964f4a2713aSLionel Sambuc /// void* operator new(std::size_t);
1965f4a2713aSLionel Sambuc /// void* operator new[](std::size_t);
1966f4a2713aSLionel Sambuc /// void operator delete(void *) noexcept;
1967f4a2713aSLionel Sambuc /// void operator delete[](void *) noexcept;
1968f4a2713aSLionel Sambuc /// void operator delete(void *, std::size_t) noexcept;
1969f4a2713aSLionel Sambuc /// void operator delete[](void *, std::size_t) noexcept;
1970f4a2713aSLionel Sambuc /// @endcode
1971f4a2713aSLionel Sambuc /// Note that the placement and nothrow forms of new are *not* implicitly
1972f4a2713aSLionel Sambuc /// declared. Their use requires including \<new\>.
DeclareGlobalNewDelete()1973f4a2713aSLionel Sambuc void Sema::DeclareGlobalNewDelete() {
1974f4a2713aSLionel Sambuc if (GlobalNewDeleteDeclared)
1975f4a2713aSLionel Sambuc return;
1976f4a2713aSLionel Sambuc
1977f4a2713aSLionel Sambuc // C++ [basic.std.dynamic]p2:
1978f4a2713aSLionel Sambuc // [...] The following allocation and deallocation functions (18.4) are
1979f4a2713aSLionel Sambuc // implicitly declared in global scope in each translation unit of a
1980f4a2713aSLionel Sambuc // program
1981f4a2713aSLionel Sambuc //
1982f4a2713aSLionel Sambuc // C++03:
1983f4a2713aSLionel Sambuc // void* operator new(std::size_t) throw(std::bad_alloc);
1984f4a2713aSLionel Sambuc // void* operator new[](std::size_t) throw(std::bad_alloc);
1985f4a2713aSLionel Sambuc // void operator delete(void*) throw();
1986f4a2713aSLionel Sambuc // void operator delete[](void*) throw();
1987f4a2713aSLionel Sambuc // C++11:
1988f4a2713aSLionel Sambuc // void* operator new(std::size_t);
1989f4a2713aSLionel Sambuc // void* operator new[](std::size_t);
1990f4a2713aSLionel Sambuc // void operator delete(void*) noexcept;
1991f4a2713aSLionel Sambuc // void operator delete[](void*) noexcept;
1992f4a2713aSLionel Sambuc // C++1y:
1993f4a2713aSLionel Sambuc // void* operator new(std::size_t);
1994f4a2713aSLionel Sambuc // void* operator new[](std::size_t);
1995f4a2713aSLionel Sambuc // void operator delete(void*) noexcept;
1996f4a2713aSLionel Sambuc // void operator delete[](void*) noexcept;
1997f4a2713aSLionel Sambuc // void operator delete(void*, std::size_t) noexcept;
1998f4a2713aSLionel Sambuc // void operator delete[](void*, std::size_t) noexcept;
1999f4a2713aSLionel Sambuc //
2000f4a2713aSLionel Sambuc // These implicit declarations introduce only the function names operator
2001f4a2713aSLionel Sambuc // new, operator new[], operator delete, operator delete[].
2002f4a2713aSLionel Sambuc //
2003f4a2713aSLionel Sambuc // Here, we need to refer to std::bad_alloc, so we will implicitly declare
2004f4a2713aSLionel Sambuc // "std" or "bad_alloc" as necessary to form the exception specification.
2005f4a2713aSLionel Sambuc // However, we do not make these implicit declarations visible to name
2006f4a2713aSLionel Sambuc // lookup.
2007f4a2713aSLionel Sambuc if (!StdBadAlloc && !getLangOpts().CPlusPlus11) {
2008f4a2713aSLionel Sambuc // The "std::bad_alloc" class has not yet been declared, so build it
2009f4a2713aSLionel Sambuc // implicitly.
2010f4a2713aSLionel Sambuc StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class,
2011f4a2713aSLionel Sambuc getOrCreateStdNamespace(),
2012f4a2713aSLionel Sambuc SourceLocation(), SourceLocation(),
2013f4a2713aSLionel Sambuc &PP.getIdentifierTable().get("bad_alloc"),
2014*0a6a1f1dSLionel Sambuc nullptr);
2015f4a2713aSLionel Sambuc getStdBadAlloc()->setImplicit(true);
2016f4a2713aSLionel Sambuc }
2017f4a2713aSLionel Sambuc
2018f4a2713aSLionel Sambuc GlobalNewDeleteDeclared = true;
2019f4a2713aSLionel Sambuc
2020f4a2713aSLionel Sambuc QualType VoidPtr = Context.getPointerType(Context.VoidTy);
2021f4a2713aSLionel Sambuc QualType SizeT = Context.getSizeType();
2022f4a2713aSLionel Sambuc bool AssumeSaneOperatorNew = getLangOpts().AssumeSaneOperatorNew;
2023f4a2713aSLionel Sambuc
2024f4a2713aSLionel Sambuc DeclareGlobalAllocationFunction(
2025f4a2713aSLionel Sambuc Context.DeclarationNames.getCXXOperatorName(OO_New),
2026f4a2713aSLionel Sambuc VoidPtr, SizeT, QualType(), AssumeSaneOperatorNew);
2027f4a2713aSLionel Sambuc DeclareGlobalAllocationFunction(
2028f4a2713aSLionel Sambuc Context.DeclarationNames.getCXXOperatorName(OO_Array_New),
2029f4a2713aSLionel Sambuc VoidPtr, SizeT, QualType(), AssumeSaneOperatorNew);
2030f4a2713aSLionel Sambuc DeclareGlobalAllocationFunction(
2031f4a2713aSLionel Sambuc Context.DeclarationNames.getCXXOperatorName(OO_Delete),
2032f4a2713aSLionel Sambuc Context.VoidTy, VoidPtr);
2033f4a2713aSLionel Sambuc DeclareGlobalAllocationFunction(
2034f4a2713aSLionel Sambuc Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete),
2035f4a2713aSLionel Sambuc Context.VoidTy, VoidPtr);
2036f4a2713aSLionel Sambuc if (getLangOpts().SizedDeallocation) {
2037f4a2713aSLionel Sambuc DeclareGlobalAllocationFunction(
2038f4a2713aSLionel Sambuc Context.DeclarationNames.getCXXOperatorName(OO_Delete),
2039f4a2713aSLionel Sambuc Context.VoidTy, VoidPtr, Context.getSizeType());
2040f4a2713aSLionel Sambuc DeclareGlobalAllocationFunction(
2041f4a2713aSLionel Sambuc Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete),
2042f4a2713aSLionel Sambuc Context.VoidTy, VoidPtr, Context.getSizeType());
2043f4a2713aSLionel Sambuc }
2044f4a2713aSLionel Sambuc }
2045f4a2713aSLionel Sambuc
2046f4a2713aSLionel Sambuc /// DeclareGlobalAllocationFunction - Declares a single implicit global
2047f4a2713aSLionel Sambuc /// allocation function if it doesn't already exist.
DeclareGlobalAllocationFunction(DeclarationName Name,QualType Return,QualType Param1,QualType Param2,bool AddMallocAttr)2048f4a2713aSLionel Sambuc void Sema::DeclareGlobalAllocationFunction(DeclarationName Name,
2049f4a2713aSLionel Sambuc QualType Return,
2050f4a2713aSLionel Sambuc QualType Param1, QualType Param2,
2051f4a2713aSLionel Sambuc bool AddMallocAttr) {
2052f4a2713aSLionel Sambuc DeclContext *GlobalCtx = Context.getTranslationUnitDecl();
2053f4a2713aSLionel Sambuc unsigned NumParams = Param2.isNull() ? 1 : 2;
2054f4a2713aSLionel Sambuc
2055f4a2713aSLionel Sambuc // Check if this function is already declared.
2056f4a2713aSLionel Sambuc DeclContext::lookup_result R = GlobalCtx->lookup(Name);
2057f4a2713aSLionel Sambuc for (DeclContext::lookup_iterator Alloc = R.begin(), AllocEnd = R.end();
2058f4a2713aSLionel Sambuc Alloc != AllocEnd; ++Alloc) {
2059f4a2713aSLionel Sambuc // Only look at non-template functions, as it is the predefined,
2060f4a2713aSLionel Sambuc // non-templated allocation function we are trying to declare here.
2061f4a2713aSLionel Sambuc if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) {
2062f4a2713aSLionel Sambuc if (Func->getNumParams() == NumParams) {
2063f4a2713aSLionel Sambuc QualType InitialParam1Type =
2064f4a2713aSLionel Sambuc Context.getCanonicalType(Func->getParamDecl(0)
2065f4a2713aSLionel Sambuc ->getType().getUnqualifiedType());
2066f4a2713aSLionel Sambuc QualType InitialParam2Type =
2067f4a2713aSLionel Sambuc NumParams == 2
2068f4a2713aSLionel Sambuc ? Context.getCanonicalType(Func->getParamDecl(1)
2069f4a2713aSLionel Sambuc ->getType().getUnqualifiedType())
2070f4a2713aSLionel Sambuc : QualType();
2071f4a2713aSLionel Sambuc // FIXME: Do we need to check for default arguments here?
2072f4a2713aSLionel Sambuc if (InitialParam1Type == Param1 &&
2073f4a2713aSLionel Sambuc (NumParams == 1 || InitialParam2Type == Param2)) {
2074f4a2713aSLionel Sambuc if (AddMallocAttr && !Func->hasAttr<MallocAttr>())
2075*0a6a1f1dSLionel Sambuc Func->addAttr(MallocAttr::CreateImplicit(Context));
2076f4a2713aSLionel Sambuc // Make the function visible to name lookup, even if we found it in
2077f4a2713aSLionel Sambuc // an unimported module. It either is an implicitly-declared global
2078f4a2713aSLionel Sambuc // allocation function, or is suppressing that function.
2079f4a2713aSLionel Sambuc Func->setHidden(false);
2080f4a2713aSLionel Sambuc return;
2081f4a2713aSLionel Sambuc }
2082f4a2713aSLionel Sambuc }
2083f4a2713aSLionel Sambuc }
2084f4a2713aSLionel Sambuc }
2085f4a2713aSLionel Sambuc
2086*0a6a1f1dSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI;
2087*0a6a1f1dSLionel Sambuc
2088f4a2713aSLionel Sambuc QualType BadAllocType;
2089f4a2713aSLionel Sambuc bool HasBadAllocExceptionSpec
2090f4a2713aSLionel Sambuc = (Name.getCXXOverloadedOperator() == OO_New ||
2091f4a2713aSLionel Sambuc Name.getCXXOverloadedOperator() == OO_Array_New);
2092f4a2713aSLionel Sambuc if (HasBadAllocExceptionSpec) {
2093f4a2713aSLionel Sambuc if (!getLangOpts().CPlusPlus11) {
2094*0a6a1f1dSLionel Sambuc BadAllocType = Context.getTypeDeclType(getStdBadAlloc());
2095*0a6a1f1dSLionel Sambuc assert(StdBadAlloc && "Must have std::bad_alloc declared");
2096*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec.Type = EST_Dynamic;
2097*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec.Exceptions = llvm::makeArrayRef(BadAllocType);
2098f4a2713aSLionel Sambuc }
2099f4a2713aSLionel Sambuc } else {
2100*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec =
2101*0a6a1f1dSLionel Sambuc getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone;
2102f4a2713aSLionel Sambuc }
2103f4a2713aSLionel Sambuc
2104f4a2713aSLionel Sambuc QualType Params[] = { Param1, Param2 };
2105f4a2713aSLionel Sambuc
2106f4a2713aSLionel Sambuc QualType FnType = Context.getFunctionType(
2107*0a6a1f1dSLionel Sambuc Return, llvm::makeArrayRef(Params, NumParams), EPI);
2108f4a2713aSLionel Sambuc FunctionDecl *Alloc =
2109f4a2713aSLionel Sambuc FunctionDecl::Create(Context, GlobalCtx, SourceLocation(),
2110f4a2713aSLionel Sambuc SourceLocation(), Name,
2111*0a6a1f1dSLionel Sambuc FnType, /*TInfo=*/nullptr, SC_None, false, true);
2112f4a2713aSLionel Sambuc Alloc->setImplicit();
2113f4a2713aSLionel Sambuc
2114f4a2713aSLionel Sambuc if (AddMallocAttr)
2115*0a6a1f1dSLionel Sambuc Alloc->addAttr(MallocAttr::CreateImplicit(Context));
2116f4a2713aSLionel Sambuc
2117f4a2713aSLionel Sambuc ParmVarDecl *ParamDecls[2];
2118*0a6a1f1dSLionel Sambuc for (unsigned I = 0; I != NumParams; ++I) {
2119f4a2713aSLionel Sambuc ParamDecls[I] = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
2120*0a6a1f1dSLionel Sambuc SourceLocation(), nullptr,
2121*0a6a1f1dSLionel Sambuc Params[I], /*TInfo=*/nullptr,
2122*0a6a1f1dSLionel Sambuc SC_None, nullptr);
2123*0a6a1f1dSLionel Sambuc ParamDecls[I]->setImplicit();
2124*0a6a1f1dSLionel Sambuc }
2125*0a6a1f1dSLionel Sambuc Alloc->setParams(llvm::makeArrayRef(ParamDecls, NumParams));
2126f4a2713aSLionel Sambuc
2127f4a2713aSLionel Sambuc Context.getTranslationUnitDecl()->addDecl(Alloc);
2128*0a6a1f1dSLionel Sambuc IdResolver.tryAddTopLevelDecl(Alloc, Name);
2129f4a2713aSLionel Sambuc }
2130f4a2713aSLionel Sambuc
FindUsualDeallocationFunction(SourceLocation StartLoc,bool CanProvideSize,DeclarationName Name)2131f4a2713aSLionel Sambuc FunctionDecl *Sema::FindUsualDeallocationFunction(SourceLocation StartLoc,
2132f4a2713aSLionel Sambuc bool CanProvideSize,
2133f4a2713aSLionel Sambuc DeclarationName Name) {
2134f4a2713aSLionel Sambuc DeclareGlobalNewDelete();
2135f4a2713aSLionel Sambuc
2136f4a2713aSLionel Sambuc LookupResult FoundDelete(*this, Name, StartLoc, LookupOrdinaryName);
2137f4a2713aSLionel Sambuc LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl());
2138f4a2713aSLionel Sambuc
2139f4a2713aSLionel Sambuc // C++ [expr.new]p20:
2140f4a2713aSLionel Sambuc // [...] Any non-placement deallocation function matches a
2141f4a2713aSLionel Sambuc // non-placement allocation function. [...]
2142f4a2713aSLionel Sambuc llvm::SmallVector<FunctionDecl*, 2> Matches;
2143f4a2713aSLionel Sambuc for (LookupResult::iterator D = FoundDelete.begin(),
2144f4a2713aSLionel Sambuc DEnd = FoundDelete.end();
2145f4a2713aSLionel Sambuc D != DEnd; ++D) {
2146f4a2713aSLionel Sambuc if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*D))
2147f4a2713aSLionel Sambuc if (isNonPlacementDeallocationFunction(*this, Fn))
2148f4a2713aSLionel Sambuc Matches.push_back(Fn);
2149f4a2713aSLionel Sambuc }
2150f4a2713aSLionel Sambuc
2151f4a2713aSLionel Sambuc // C++1y [expr.delete]p?:
2152f4a2713aSLionel Sambuc // If the type is complete and deallocation function lookup finds both a
2153f4a2713aSLionel Sambuc // usual deallocation function with only a pointer parameter and a usual
2154f4a2713aSLionel Sambuc // deallocation function with both a pointer parameter and a size
2155f4a2713aSLionel Sambuc // parameter, then the selected deallocation function shall be the one
2156f4a2713aSLionel Sambuc // with two parameters. Otherwise, the selected deallocation function
2157f4a2713aSLionel Sambuc // shall be the function with one parameter.
2158f4a2713aSLionel Sambuc if (getLangOpts().SizedDeallocation && Matches.size() == 2) {
2159f4a2713aSLionel Sambuc unsigned NumArgs = CanProvideSize ? 2 : 1;
2160f4a2713aSLionel Sambuc if (Matches[0]->getNumParams() != NumArgs)
2161f4a2713aSLionel Sambuc Matches.erase(Matches.begin());
2162f4a2713aSLionel Sambuc else
2163f4a2713aSLionel Sambuc Matches.erase(Matches.begin() + 1);
2164f4a2713aSLionel Sambuc assert(Matches[0]->getNumParams() == NumArgs &&
2165*0a6a1f1dSLionel Sambuc "found an unexpected usual deallocation function");
2166f4a2713aSLionel Sambuc }
2167f4a2713aSLionel Sambuc
2168f4a2713aSLionel Sambuc assert(Matches.size() == 1 &&
2169f4a2713aSLionel Sambuc "unexpectedly have multiple usual deallocation functions");
2170f4a2713aSLionel Sambuc return Matches.front();
2171f4a2713aSLionel Sambuc }
2172f4a2713aSLionel Sambuc
FindDeallocationFunction(SourceLocation StartLoc,CXXRecordDecl * RD,DeclarationName Name,FunctionDecl * & Operator,bool Diagnose)2173f4a2713aSLionel Sambuc bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD,
2174f4a2713aSLionel Sambuc DeclarationName Name,
2175f4a2713aSLionel Sambuc FunctionDecl* &Operator, bool Diagnose) {
2176f4a2713aSLionel Sambuc LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName);
2177f4a2713aSLionel Sambuc // Try to find operator delete/operator delete[] in class scope.
2178f4a2713aSLionel Sambuc LookupQualifiedName(Found, RD);
2179f4a2713aSLionel Sambuc
2180f4a2713aSLionel Sambuc if (Found.isAmbiguous())
2181f4a2713aSLionel Sambuc return true;
2182f4a2713aSLionel Sambuc
2183f4a2713aSLionel Sambuc Found.suppressDiagnostics();
2184f4a2713aSLionel Sambuc
2185f4a2713aSLionel Sambuc SmallVector<DeclAccessPair,4> Matches;
2186f4a2713aSLionel Sambuc for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
2187f4a2713aSLionel Sambuc F != FEnd; ++F) {
2188f4a2713aSLionel Sambuc NamedDecl *ND = (*F)->getUnderlyingDecl();
2189f4a2713aSLionel Sambuc
2190f4a2713aSLionel Sambuc // Ignore template operator delete members from the check for a usual
2191f4a2713aSLionel Sambuc // deallocation function.
2192f4a2713aSLionel Sambuc if (isa<FunctionTemplateDecl>(ND))
2193f4a2713aSLionel Sambuc continue;
2194f4a2713aSLionel Sambuc
2195f4a2713aSLionel Sambuc if (cast<CXXMethodDecl>(ND)->isUsualDeallocationFunction())
2196f4a2713aSLionel Sambuc Matches.push_back(F.getPair());
2197f4a2713aSLionel Sambuc }
2198f4a2713aSLionel Sambuc
2199f4a2713aSLionel Sambuc // There's exactly one suitable operator; pick it.
2200f4a2713aSLionel Sambuc if (Matches.size() == 1) {
2201f4a2713aSLionel Sambuc Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl());
2202f4a2713aSLionel Sambuc
2203f4a2713aSLionel Sambuc if (Operator->isDeleted()) {
2204f4a2713aSLionel Sambuc if (Diagnose) {
2205f4a2713aSLionel Sambuc Diag(StartLoc, diag::err_deleted_function_use);
2206f4a2713aSLionel Sambuc NoteDeletedFunction(Operator);
2207f4a2713aSLionel Sambuc }
2208f4a2713aSLionel Sambuc return true;
2209f4a2713aSLionel Sambuc }
2210f4a2713aSLionel Sambuc
2211f4a2713aSLionel Sambuc if (CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(),
2212f4a2713aSLionel Sambuc Matches[0], Diagnose) == AR_inaccessible)
2213f4a2713aSLionel Sambuc return true;
2214f4a2713aSLionel Sambuc
2215f4a2713aSLionel Sambuc return false;
2216f4a2713aSLionel Sambuc
2217f4a2713aSLionel Sambuc // We found multiple suitable operators; complain about the ambiguity.
2218f4a2713aSLionel Sambuc } else if (!Matches.empty()) {
2219f4a2713aSLionel Sambuc if (Diagnose) {
2220f4a2713aSLionel Sambuc Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found)
2221f4a2713aSLionel Sambuc << Name << RD;
2222f4a2713aSLionel Sambuc
2223f4a2713aSLionel Sambuc for (SmallVectorImpl<DeclAccessPair>::iterator
2224f4a2713aSLionel Sambuc F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F)
2225f4a2713aSLionel Sambuc Diag((*F)->getUnderlyingDecl()->getLocation(),
2226f4a2713aSLionel Sambuc diag::note_member_declared_here) << Name;
2227f4a2713aSLionel Sambuc }
2228f4a2713aSLionel Sambuc return true;
2229f4a2713aSLionel Sambuc }
2230f4a2713aSLionel Sambuc
2231f4a2713aSLionel Sambuc // We did find operator delete/operator delete[] declarations, but
2232f4a2713aSLionel Sambuc // none of them were suitable.
2233f4a2713aSLionel Sambuc if (!Found.empty()) {
2234f4a2713aSLionel Sambuc if (Diagnose) {
2235f4a2713aSLionel Sambuc Diag(StartLoc, diag::err_no_suitable_delete_member_function_found)
2236f4a2713aSLionel Sambuc << Name << RD;
2237f4a2713aSLionel Sambuc
2238f4a2713aSLionel Sambuc for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
2239f4a2713aSLionel Sambuc F != FEnd; ++F)
2240f4a2713aSLionel Sambuc Diag((*F)->getUnderlyingDecl()->getLocation(),
2241f4a2713aSLionel Sambuc diag::note_member_declared_here) << Name;
2242f4a2713aSLionel Sambuc }
2243f4a2713aSLionel Sambuc return true;
2244f4a2713aSLionel Sambuc }
2245f4a2713aSLionel Sambuc
2246*0a6a1f1dSLionel Sambuc Operator = nullptr;
2247f4a2713aSLionel Sambuc return false;
2248f4a2713aSLionel Sambuc }
2249f4a2713aSLionel Sambuc
2250f4a2713aSLionel Sambuc /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
2251f4a2713aSLionel Sambuc /// @code ::delete ptr; @endcode
2252f4a2713aSLionel Sambuc /// or
2253f4a2713aSLionel Sambuc /// @code delete [] ptr; @endcode
2254f4a2713aSLionel Sambuc ExprResult
ActOnCXXDelete(SourceLocation StartLoc,bool UseGlobal,bool ArrayForm,Expr * ExE)2255f4a2713aSLionel Sambuc Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
2256f4a2713aSLionel Sambuc bool ArrayForm, Expr *ExE) {
2257f4a2713aSLionel Sambuc // C++ [expr.delete]p1:
2258f4a2713aSLionel Sambuc // The operand shall have a pointer type, or a class type having a single
2259f4a2713aSLionel Sambuc // non-explicit conversion function to a pointer type. The result has type
2260f4a2713aSLionel Sambuc // void.
2261f4a2713aSLionel Sambuc //
2262f4a2713aSLionel Sambuc // DR599 amends "pointer type" to "pointer to object type" in both cases.
2263f4a2713aSLionel Sambuc
2264*0a6a1f1dSLionel Sambuc ExprResult Ex = ExE;
2265*0a6a1f1dSLionel Sambuc FunctionDecl *OperatorDelete = nullptr;
2266f4a2713aSLionel Sambuc bool ArrayFormAsWritten = ArrayForm;
2267f4a2713aSLionel Sambuc bool UsualArrayDeleteWantsSize = false;
2268f4a2713aSLionel Sambuc
2269f4a2713aSLionel Sambuc if (!Ex.get()->isTypeDependent()) {
2270f4a2713aSLionel Sambuc // Perform lvalue-to-rvalue cast, if needed.
2271*0a6a1f1dSLionel Sambuc Ex = DefaultLvalueConversion(Ex.get());
2272f4a2713aSLionel Sambuc if (Ex.isInvalid())
2273f4a2713aSLionel Sambuc return ExprError();
2274f4a2713aSLionel Sambuc
2275f4a2713aSLionel Sambuc QualType Type = Ex.get()->getType();
2276f4a2713aSLionel Sambuc
2277f4a2713aSLionel Sambuc class DeleteConverter : public ContextualImplicitConverter {
2278f4a2713aSLionel Sambuc public:
2279f4a2713aSLionel Sambuc DeleteConverter() : ContextualImplicitConverter(false, true) {}
2280f4a2713aSLionel Sambuc
2281*0a6a1f1dSLionel Sambuc bool match(QualType ConvType) override {
2282f4a2713aSLionel Sambuc // FIXME: If we have an operator T* and an operator void*, we must pick
2283f4a2713aSLionel Sambuc // the operator T*.
2284f4a2713aSLionel Sambuc if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>())
2285f4a2713aSLionel Sambuc if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType())
2286f4a2713aSLionel Sambuc return true;
2287f4a2713aSLionel Sambuc return false;
2288f4a2713aSLionel Sambuc }
2289f4a2713aSLionel Sambuc
2290f4a2713aSLionel Sambuc SemaDiagnosticBuilder diagnoseNoMatch(Sema &S, SourceLocation Loc,
2291*0a6a1f1dSLionel Sambuc QualType T) override {
2292f4a2713aSLionel Sambuc return S.Diag(Loc, diag::err_delete_operand) << T;
2293f4a2713aSLionel Sambuc }
2294f4a2713aSLionel Sambuc
2295f4a2713aSLionel Sambuc SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2296*0a6a1f1dSLionel Sambuc QualType T) override {
2297f4a2713aSLionel Sambuc return S.Diag(Loc, diag::err_delete_incomplete_class_type) << T;
2298f4a2713aSLionel Sambuc }
2299f4a2713aSLionel Sambuc
2300f4a2713aSLionel Sambuc SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2301*0a6a1f1dSLionel Sambuc QualType T,
2302*0a6a1f1dSLionel Sambuc QualType ConvTy) override {
2303f4a2713aSLionel Sambuc return S.Diag(Loc, diag::err_delete_explicit_conversion) << T << ConvTy;
2304f4a2713aSLionel Sambuc }
2305f4a2713aSLionel Sambuc
2306f4a2713aSLionel Sambuc SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2307*0a6a1f1dSLionel Sambuc QualType ConvTy) override {
2308f4a2713aSLionel Sambuc return S.Diag(Conv->getLocation(), diag::note_delete_conversion)
2309f4a2713aSLionel Sambuc << ConvTy;
2310f4a2713aSLionel Sambuc }
2311f4a2713aSLionel Sambuc
2312f4a2713aSLionel Sambuc SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2313*0a6a1f1dSLionel Sambuc QualType T) override {
2314f4a2713aSLionel Sambuc return S.Diag(Loc, diag::err_ambiguous_delete_operand) << T;
2315f4a2713aSLionel Sambuc }
2316f4a2713aSLionel Sambuc
2317f4a2713aSLionel Sambuc SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2318*0a6a1f1dSLionel Sambuc QualType ConvTy) override {
2319f4a2713aSLionel Sambuc return S.Diag(Conv->getLocation(), diag::note_delete_conversion)
2320f4a2713aSLionel Sambuc << ConvTy;
2321f4a2713aSLionel Sambuc }
2322f4a2713aSLionel Sambuc
2323f4a2713aSLionel Sambuc SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc,
2324*0a6a1f1dSLionel Sambuc QualType T,
2325*0a6a1f1dSLionel Sambuc QualType ConvTy) override {
2326f4a2713aSLionel Sambuc llvm_unreachable("conversion functions are permitted");
2327f4a2713aSLionel Sambuc }
2328f4a2713aSLionel Sambuc } Converter;
2329f4a2713aSLionel Sambuc
2330*0a6a1f1dSLionel Sambuc Ex = PerformContextualImplicitConversion(StartLoc, Ex.get(), Converter);
2331f4a2713aSLionel Sambuc if (Ex.isInvalid())
2332f4a2713aSLionel Sambuc return ExprError();
2333f4a2713aSLionel Sambuc Type = Ex.get()->getType();
2334f4a2713aSLionel Sambuc if (!Converter.match(Type))
2335f4a2713aSLionel Sambuc // FIXME: PerformContextualImplicitConversion should return ExprError
2336f4a2713aSLionel Sambuc // itself in this case.
2337f4a2713aSLionel Sambuc return ExprError();
2338f4a2713aSLionel Sambuc
2339f4a2713aSLionel Sambuc QualType Pointee = Type->getAs<PointerType>()->getPointeeType();
2340f4a2713aSLionel Sambuc QualType PointeeElem = Context.getBaseElementType(Pointee);
2341f4a2713aSLionel Sambuc
2342f4a2713aSLionel Sambuc if (unsigned AddressSpace = Pointee.getAddressSpace())
2343f4a2713aSLionel Sambuc return Diag(Ex.get()->getLocStart(),
2344f4a2713aSLionel Sambuc diag::err_address_space_qualified_delete)
2345f4a2713aSLionel Sambuc << Pointee.getUnqualifiedType() << AddressSpace;
2346f4a2713aSLionel Sambuc
2347*0a6a1f1dSLionel Sambuc CXXRecordDecl *PointeeRD = nullptr;
2348f4a2713aSLionel Sambuc if (Pointee->isVoidType() && !isSFINAEContext()) {
2349f4a2713aSLionel Sambuc // The C++ standard bans deleting a pointer to a non-object type, which
2350f4a2713aSLionel Sambuc // effectively bans deletion of "void*". However, most compilers support
2351f4a2713aSLionel Sambuc // this, so we treat it as a warning unless we're in a SFINAE context.
2352f4a2713aSLionel Sambuc Diag(StartLoc, diag::ext_delete_void_ptr_operand)
2353f4a2713aSLionel Sambuc << Type << Ex.get()->getSourceRange();
2354f4a2713aSLionel Sambuc } else if (Pointee->isFunctionType() || Pointee->isVoidType()) {
2355f4a2713aSLionel Sambuc return ExprError(Diag(StartLoc, diag::err_delete_operand)
2356f4a2713aSLionel Sambuc << Type << Ex.get()->getSourceRange());
2357f4a2713aSLionel Sambuc } else if (!Pointee->isDependentType()) {
2358f4a2713aSLionel Sambuc if (!RequireCompleteType(StartLoc, Pointee,
2359f4a2713aSLionel Sambuc diag::warn_delete_incomplete, Ex.get())) {
2360f4a2713aSLionel Sambuc if (const RecordType *RT = PointeeElem->getAs<RecordType>())
2361f4a2713aSLionel Sambuc PointeeRD = cast<CXXRecordDecl>(RT->getDecl());
2362f4a2713aSLionel Sambuc }
2363f4a2713aSLionel Sambuc }
2364f4a2713aSLionel Sambuc
2365f4a2713aSLionel Sambuc // C++ [expr.delete]p2:
2366f4a2713aSLionel Sambuc // [Note: a pointer to a const type can be the operand of a
2367f4a2713aSLionel Sambuc // delete-expression; it is not necessary to cast away the constness
2368f4a2713aSLionel Sambuc // (5.2.11) of the pointer expression before it is used as the operand
2369f4a2713aSLionel Sambuc // of the delete-expression. ]
2370f4a2713aSLionel Sambuc
2371f4a2713aSLionel Sambuc if (Pointee->isArrayType() && !ArrayForm) {
2372f4a2713aSLionel Sambuc Diag(StartLoc, diag::warn_delete_array_type)
2373f4a2713aSLionel Sambuc << Type << Ex.get()->getSourceRange()
2374f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(PP.getLocForEndOfToken(StartLoc), "[]");
2375f4a2713aSLionel Sambuc ArrayForm = true;
2376f4a2713aSLionel Sambuc }
2377f4a2713aSLionel Sambuc
2378f4a2713aSLionel Sambuc DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName(
2379f4a2713aSLionel Sambuc ArrayForm ? OO_Array_Delete : OO_Delete);
2380f4a2713aSLionel Sambuc
2381f4a2713aSLionel Sambuc if (PointeeRD) {
2382f4a2713aSLionel Sambuc if (!UseGlobal &&
2383f4a2713aSLionel Sambuc FindDeallocationFunction(StartLoc, PointeeRD, DeleteName,
2384f4a2713aSLionel Sambuc OperatorDelete))
2385f4a2713aSLionel Sambuc return ExprError();
2386f4a2713aSLionel Sambuc
2387f4a2713aSLionel Sambuc // If we're allocating an array of records, check whether the
2388f4a2713aSLionel Sambuc // usual operator delete[] has a size_t parameter.
2389f4a2713aSLionel Sambuc if (ArrayForm) {
2390f4a2713aSLionel Sambuc // If the user specifically asked to use the global allocator,
2391f4a2713aSLionel Sambuc // we'll need to do the lookup into the class.
2392f4a2713aSLionel Sambuc if (UseGlobal)
2393f4a2713aSLionel Sambuc UsualArrayDeleteWantsSize =
2394f4a2713aSLionel Sambuc doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem);
2395f4a2713aSLionel Sambuc
2396f4a2713aSLionel Sambuc // Otherwise, the usual operator delete[] should be the
2397f4a2713aSLionel Sambuc // function we just found.
2398*0a6a1f1dSLionel Sambuc else if (OperatorDelete && isa<CXXMethodDecl>(OperatorDelete))
2399f4a2713aSLionel Sambuc UsualArrayDeleteWantsSize = (OperatorDelete->getNumParams() == 2);
2400f4a2713aSLionel Sambuc }
2401f4a2713aSLionel Sambuc
2402f4a2713aSLionel Sambuc if (!PointeeRD->hasIrrelevantDestructor())
2403f4a2713aSLionel Sambuc if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) {
2404f4a2713aSLionel Sambuc MarkFunctionReferenced(StartLoc,
2405f4a2713aSLionel Sambuc const_cast<CXXDestructorDecl*>(Dtor));
2406f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(Dtor, StartLoc))
2407f4a2713aSLionel Sambuc return ExprError();
2408f4a2713aSLionel Sambuc }
2409f4a2713aSLionel Sambuc
2410f4a2713aSLionel Sambuc // C++ [expr.delete]p3:
2411f4a2713aSLionel Sambuc // In the first alternative (delete object), if the static type of the
2412f4a2713aSLionel Sambuc // object to be deleted is different from its dynamic type, the static
2413f4a2713aSLionel Sambuc // type shall be a base class of the dynamic type of the object to be
2414f4a2713aSLionel Sambuc // deleted and the static type shall have a virtual destructor or the
2415f4a2713aSLionel Sambuc // behavior is undefined.
2416f4a2713aSLionel Sambuc //
2417f4a2713aSLionel Sambuc // Note: a final class cannot be derived from, no issue there
2418f4a2713aSLionel Sambuc if (PointeeRD->isPolymorphic() && !PointeeRD->hasAttr<FinalAttr>()) {
2419f4a2713aSLionel Sambuc CXXDestructorDecl *dtor = PointeeRD->getDestructor();
2420f4a2713aSLionel Sambuc if (dtor && !dtor->isVirtual()) {
2421f4a2713aSLionel Sambuc if (PointeeRD->isAbstract()) {
2422f4a2713aSLionel Sambuc // If the class is abstract, we warn by default, because we're
2423f4a2713aSLionel Sambuc // sure the code has undefined behavior.
2424f4a2713aSLionel Sambuc Diag(StartLoc, diag::warn_delete_abstract_non_virtual_dtor)
2425f4a2713aSLionel Sambuc << PointeeElem;
2426f4a2713aSLionel Sambuc } else if (!ArrayForm) {
2427f4a2713aSLionel Sambuc // Otherwise, if this is not an array delete, it's a bit suspect,
2428f4a2713aSLionel Sambuc // but not necessarily wrong.
2429f4a2713aSLionel Sambuc Diag(StartLoc, diag::warn_delete_non_virtual_dtor) << PointeeElem;
2430f4a2713aSLionel Sambuc }
2431f4a2713aSLionel Sambuc }
2432f4a2713aSLionel Sambuc }
2433f4a2713aSLionel Sambuc
2434f4a2713aSLionel Sambuc }
2435f4a2713aSLionel Sambuc
2436f4a2713aSLionel Sambuc if (!OperatorDelete)
2437f4a2713aSLionel Sambuc // Look for a global declaration.
2438f4a2713aSLionel Sambuc OperatorDelete = FindUsualDeallocationFunction(
2439f4a2713aSLionel Sambuc StartLoc, !RequireCompleteType(StartLoc, Pointee, 0) &&
2440f4a2713aSLionel Sambuc (!ArrayForm || UsualArrayDeleteWantsSize ||
2441f4a2713aSLionel Sambuc Pointee.isDestructedType()),
2442f4a2713aSLionel Sambuc DeleteName);
2443f4a2713aSLionel Sambuc
2444f4a2713aSLionel Sambuc MarkFunctionReferenced(StartLoc, OperatorDelete);
2445f4a2713aSLionel Sambuc
2446f4a2713aSLionel Sambuc // Check access and ambiguity of operator delete and destructor.
2447f4a2713aSLionel Sambuc if (PointeeRD) {
2448f4a2713aSLionel Sambuc if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) {
2449f4a2713aSLionel Sambuc CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor,
2450f4a2713aSLionel Sambuc PDiag(diag::err_access_dtor) << PointeeElem);
2451f4a2713aSLionel Sambuc }
2452f4a2713aSLionel Sambuc }
2453f4a2713aSLionel Sambuc }
2454f4a2713aSLionel Sambuc
2455*0a6a1f1dSLionel Sambuc return new (Context) CXXDeleteExpr(
2456*0a6a1f1dSLionel Sambuc Context.VoidTy, UseGlobal, ArrayForm, ArrayFormAsWritten,
2457*0a6a1f1dSLionel Sambuc UsualArrayDeleteWantsSize, OperatorDelete, Ex.get(), StartLoc);
2458f4a2713aSLionel Sambuc }
2459f4a2713aSLionel Sambuc
2460f4a2713aSLionel Sambuc /// \brief Check the use of the given variable as a C++ condition in an if,
2461f4a2713aSLionel Sambuc /// while, do-while, or switch statement.
CheckConditionVariable(VarDecl * ConditionVar,SourceLocation StmtLoc,bool ConvertToBoolean)2462f4a2713aSLionel Sambuc ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar,
2463f4a2713aSLionel Sambuc SourceLocation StmtLoc,
2464f4a2713aSLionel Sambuc bool ConvertToBoolean) {
2465f4a2713aSLionel Sambuc if (ConditionVar->isInvalidDecl())
2466f4a2713aSLionel Sambuc return ExprError();
2467f4a2713aSLionel Sambuc
2468f4a2713aSLionel Sambuc QualType T = ConditionVar->getType();
2469f4a2713aSLionel Sambuc
2470f4a2713aSLionel Sambuc // C++ [stmt.select]p2:
2471f4a2713aSLionel Sambuc // The declarator shall not specify a function or an array.
2472f4a2713aSLionel Sambuc if (T->isFunctionType())
2473f4a2713aSLionel Sambuc return ExprError(Diag(ConditionVar->getLocation(),
2474f4a2713aSLionel Sambuc diag::err_invalid_use_of_function_type)
2475f4a2713aSLionel Sambuc << ConditionVar->getSourceRange());
2476f4a2713aSLionel Sambuc else if (T->isArrayType())
2477f4a2713aSLionel Sambuc return ExprError(Diag(ConditionVar->getLocation(),
2478f4a2713aSLionel Sambuc diag::err_invalid_use_of_array_type)
2479f4a2713aSLionel Sambuc << ConditionVar->getSourceRange());
2480f4a2713aSLionel Sambuc
2481*0a6a1f1dSLionel Sambuc ExprResult Condition = DeclRefExpr::Create(
2482*0a6a1f1dSLionel Sambuc Context, NestedNameSpecifierLoc(), SourceLocation(), ConditionVar,
2483*0a6a1f1dSLionel Sambuc /*enclosing*/ false, ConditionVar->getLocation(),
2484*0a6a1f1dSLionel Sambuc ConditionVar->getType().getNonReferenceType(), VK_LValue);
2485f4a2713aSLionel Sambuc
2486f4a2713aSLionel Sambuc MarkDeclRefReferenced(cast<DeclRefExpr>(Condition.get()));
2487f4a2713aSLionel Sambuc
2488f4a2713aSLionel Sambuc if (ConvertToBoolean) {
2489*0a6a1f1dSLionel Sambuc Condition = CheckBooleanCondition(Condition.get(), StmtLoc);
2490f4a2713aSLionel Sambuc if (Condition.isInvalid())
2491f4a2713aSLionel Sambuc return ExprError();
2492f4a2713aSLionel Sambuc }
2493f4a2713aSLionel Sambuc
2494f4a2713aSLionel Sambuc return Condition;
2495f4a2713aSLionel Sambuc }
2496f4a2713aSLionel Sambuc
2497f4a2713aSLionel Sambuc /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
CheckCXXBooleanCondition(Expr * CondExpr)2498f4a2713aSLionel Sambuc ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr) {
2499f4a2713aSLionel Sambuc // C++ 6.4p4:
2500f4a2713aSLionel Sambuc // The value of a condition that is an initialized declaration in a statement
2501f4a2713aSLionel Sambuc // other than a switch statement is the value of the declared variable
2502f4a2713aSLionel Sambuc // implicitly converted to type bool. If that conversion is ill-formed, the
2503f4a2713aSLionel Sambuc // program is ill-formed.
2504f4a2713aSLionel Sambuc // The value of a condition that is an expression is the value of the
2505f4a2713aSLionel Sambuc // expression, implicitly converted to bool.
2506f4a2713aSLionel Sambuc //
2507f4a2713aSLionel Sambuc return PerformContextuallyConvertToBool(CondExpr);
2508f4a2713aSLionel Sambuc }
2509f4a2713aSLionel Sambuc
2510f4a2713aSLionel Sambuc /// Helper function to determine whether this is the (deprecated) C++
2511f4a2713aSLionel Sambuc /// conversion from a string literal to a pointer to non-const char or
2512f4a2713aSLionel Sambuc /// non-const wchar_t (for narrow and wide string literals,
2513f4a2713aSLionel Sambuc /// respectively).
2514f4a2713aSLionel Sambuc bool
IsStringLiteralToNonConstPointerConversion(Expr * From,QualType ToType)2515f4a2713aSLionel Sambuc Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
2516f4a2713aSLionel Sambuc // Look inside the implicit cast, if it exists.
2517f4a2713aSLionel Sambuc if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
2518f4a2713aSLionel Sambuc From = Cast->getSubExpr();
2519f4a2713aSLionel Sambuc
2520f4a2713aSLionel Sambuc // A string literal (2.13.4) that is not a wide string literal can
2521f4a2713aSLionel Sambuc // be converted to an rvalue of type "pointer to char"; a wide
2522f4a2713aSLionel Sambuc // string literal can be converted to an rvalue of type "pointer
2523f4a2713aSLionel Sambuc // to wchar_t" (C++ 4.2p2).
2524f4a2713aSLionel Sambuc if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens()))
2525f4a2713aSLionel Sambuc if (const PointerType *ToPtrType = ToType->getAs<PointerType>())
2526f4a2713aSLionel Sambuc if (const BuiltinType *ToPointeeType
2527f4a2713aSLionel Sambuc = ToPtrType->getPointeeType()->getAs<BuiltinType>()) {
2528f4a2713aSLionel Sambuc // This conversion is considered only when there is an
2529f4a2713aSLionel Sambuc // explicit appropriate pointer target type (C++ 4.2p2).
2530f4a2713aSLionel Sambuc if (!ToPtrType->getPointeeType().hasQualifiers()) {
2531f4a2713aSLionel Sambuc switch (StrLit->getKind()) {
2532f4a2713aSLionel Sambuc case StringLiteral::UTF8:
2533f4a2713aSLionel Sambuc case StringLiteral::UTF16:
2534f4a2713aSLionel Sambuc case StringLiteral::UTF32:
2535f4a2713aSLionel Sambuc // We don't allow UTF literals to be implicitly converted
2536f4a2713aSLionel Sambuc break;
2537f4a2713aSLionel Sambuc case StringLiteral::Ascii:
2538f4a2713aSLionel Sambuc return (ToPointeeType->getKind() == BuiltinType::Char_U ||
2539f4a2713aSLionel Sambuc ToPointeeType->getKind() == BuiltinType::Char_S);
2540f4a2713aSLionel Sambuc case StringLiteral::Wide:
2541f4a2713aSLionel Sambuc return ToPointeeType->isWideCharType();
2542f4a2713aSLionel Sambuc }
2543f4a2713aSLionel Sambuc }
2544f4a2713aSLionel Sambuc }
2545f4a2713aSLionel Sambuc
2546f4a2713aSLionel Sambuc return false;
2547f4a2713aSLionel Sambuc }
2548f4a2713aSLionel Sambuc
BuildCXXCastArgument(Sema & S,SourceLocation CastLoc,QualType Ty,CastKind Kind,CXXMethodDecl * Method,DeclAccessPair FoundDecl,bool HadMultipleCandidates,Expr * From)2549f4a2713aSLionel Sambuc static ExprResult BuildCXXCastArgument(Sema &S,
2550f4a2713aSLionel Sambuc SourceLocation CastLoc,
2551f4a2713aSLionel Sambuc QualType Ty,
2552f4a2713aSLionel Sambuc CastKind Kind,
2553f4a2713aSLionel Sambuc CXXMethodDecl *Method,
2554f4a2713aSLionel Sambuc DeclAccessPair FoundDecl,
2555f4a2713aSLionel Sambuc bool HadMultipleCandidates,
2556f4a2713aSLionel Sambuc Expr *From) {
2557f4a2713aSLionel Sambuc switch (Kind) {
2558f4a2713aSLionel Sambuc default: llvm_unreachable("Unhandled cast kind!");
2559f4a2713aSLionel Sambuc case CK_ConstructorConversion: {
2560f4a2713aSLionel Sambuc CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method);
2561f4a2713aSLionel Sambuc SmallVector<Expr*, 8> ConstructorArgs;
2562f4a2713aSLionel Sambuc
2563f4a2713aSLionel Sambuc if (S.RequireNonAbstractType(CastLoc, Ty,
2564f4a2713aSLionel Sambuc diag::err_allocation_of_abstract_type))
2565f4a2713aSLionel Sambuc return ExprError();
2566f4a2713aSLionel Sambuc
2567f4a2713aSLionel Sambuc if (S.CompleteConstructorCall(Constructor, From, CastLoc, ConstructorArgs))
2568f4a2713aSLionel Sambuc return ExprError();
2569f4a2713aSLionel Sambuc
2570f4a2713aSLionel Sambuc S.CheckConstructorAccess(CastLoc, Constructor,
2571f4a2713aSLionel Sambuc InitializedEntity::InitializeTemporary(Ty),
2572f4a2713aSLionel Sambuc Constructor->getAccess());
2573f4a2713aSLionel Sambuc
2574*0a6a1f1dSLionel Sambuc ExprResult Result = S.BuildCXXConstructExpr(
2575*0a6a1f1dSLionel Sambuc CastLoc, Ty, cast<CXXConstructorDecl>(Method),
2576f4a2713aSLionel Sambuc ConstructorArgs, HadMultipleCandidates,
2577*0a6a1f1dSLionel Sambuc /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false,
2578f4a2713aSLionel Sambuc CXXConstructExpr::CK_Complete, SourceRange());
2579f4a2713aSLionel Sambuc if (Result.isInvalid())
2580f4a2713aSLionel Sambuc return ExprError();
2581f4a2713aSLionel Sambuc
2582*0a6a1f1dSLionel Sambuc return S.MaybeBindToTemporary(Result.getAs<Expr>());
2583f4a2713aSLionel Sambuc }
2584f4a2713aSLionel Sambuc
2585f4a2713aSLionel Sambuc case CK_UserDefinedConversion: {
2586f4a2713aSLionel Sambuc assert(!From->getType()->isPointerType() && "Arg can't have pointer type!");
2587f4a2713aSLionel Sambuc
2588f4a2713aSLionel Sambuc // Create an implicit call expr that calls it.
2589f4a2713aSLionel Sambuc CXXConversionDecl *Conv = cast<CXXConversionDecl>(Method);
2590f4a2713aSLionel Sambuc ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Conv,
2591f4a2713aSLionel Sambuc HadMultipleCandidates);
2592f4a2713aSLionel Sambuc if (Result.isInvalid())
2593f4a2713aSLionel Sambuc return ExprError();
2594f4a2713aSLionel Sambuc // Record usage of conversion in an implicit cast.
2595*0a6a1f1dSLionel Sambuc Result = ImplicitCastExpr::Create(S.Context, Result.get()->getType(),
2596*0a6a1f1dSLionel Sambuc CK_UserDefinedConversion, Result.get(),
2597*0a6a1f1dSLionel Sambuc nullptr, Result.get()->getValueKind());
2598f4a2713aSLionel Sambuc
2599*0a6a1f1dSLionel Sambuc S.CheckMemberOperatorAccess(CastLoc, From, /*arg*/ nullptr, FoundDecl);
2600f4a2713aSLionel Sambuc
2601f4a2713aSLionel Sambuc return S.MaybeBindToTemporary(Result.get());
2602f4a2713aSLionel Sambuc }
2603f4a2713aSLionel Sambuc }
2604f4a2713aSLionel Sambuc }
2605f4a2713aSLionel Sambuc
2606f4a2713aSLionel Sambuc /// PerformImplicitConversion - Perform an implicit conversion of the
2607f4a2713aSLionel Sambuc /// expression From to the type ToType using the pre-computed implicit
2608f4a2713aSLionel Sambuc /// conversion sequence ICS. Returns the converted
2609f4a2713aSLionel Sambuc /// expression. Action is the kind of conversion we're performing,
2610f4a2713aSLionel Sambuc /// used in the error message.
2611f4a2713aSLionel Sambuc ExprResult
PerformImplicitConversion(Expr * From,QualType ToType,const ImplicitConversionSequence & ICS,AssignmentAction Action,CheckedConversionKind CCK)2612f4a2713aSLionel Sambuc Sema::PerformImplicitConversion(Expr *From, QualType ToType,
2613f4a2713aSLionel Sambuc const ImplicitConversionSequence &ICS,
2614f4a2713aSLionel Sambuc AssignmentAction Action,
2615f4a2713aSLionel Sambuc CheckedConversionKind CCK) {
2616f4a2713aSLionel Sambuc switch (ICS.getKind()) {
2617f4a2713aSLionel Sambuc case ImplicitConversionSequence::StandardConversion: {
2618f4a2713aSLionel Sambuc ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard,
2619f4a2713aSLionel Sambuc Action, CCK);
2620f4a2713aSLionel Sambuc if (Res.isInvalid())
2621f4a2713aSLionel Sambuc return ExprError();
2622*0a6a1f1dSLionel Sambuc From = Res.get();
2623f4a2713aSLionel Sambuc break;
2624f4a2713aSLionel Sambuc }
2625f4a2713aSLionel Sambuc
2626f4a2713aSLionel Sambuc case ImplicitConversionSequence::UserDefinedConversion: {
2627f4a2713aSLionel Sambuc
2628f4a2713aSLionel Sambuc FunctionDecl *FD = ICS.UserDefined.ConversionFunction;
2629f4a2713aSLionel Sambuc CastKind CastKind;
2630f4a2713aSLionel Sambuc QualType BeforeToType;
2631f4a2713aSLionel Sambuc assert(FD && "FIXME: aggregate initialization from init list");
2632f4a2713aSLionel Sambuc if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) {
2633f4a2713aSLionel Sambuc CastKind = CK_UserDefinedConversion;
2634f4a2713aSLionel Sambuc
2635f4a2713aSLionel Sambuc // If the user-defined conversion is specified by a conversion function,
2636f4a2713aSLionel Sambuc // the initial standard conversion sequence converts the source type to
2637f4a2713aSLionel Sambuc // the implicit object parameter of the conversion function.
2638f4a2713aSLionel Sambuc BeforeToType = Context.getTagDeclType(Conv->getParent());
2639f4a2713aSLionel Sambuc } else {
2640f4a2713aSLionel Sambuc const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD);
2641f4a2713aSLionel Sambuc CastKind = CK_ConstructorConversion;
2642f4a2713aSLionel Sambuc // Do no conversion if dealing with ... for the first conversion.
2643f4a2713aSLionel Sambuc if (!ICS.UserDefined.EllipsisConversion) {
2644f4a2713aSLionel Sambuc // If the user-defined conversion is specified by a constructor, the
2645*0a6a1f1dSLionel Sambuc // initial standard conversion sequence converts the source type to
2646*0a6a1f1dSLionel Sambuc // the type required by the argument of the constructor
2647f4a2713aSLionel Sambuc BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType();
2648f4a2713aSLionel Sambuc }
2649f4a2713aSLionel Sambuc }
2650f4a2713aSLionel Sambuc // Watch out for ellipsis conversion.
2651f4a2713aSLionel Sambuc if (!ICS.UserDefined.EllipsisConversion) {
2652f4a2713aSLionel Sambuc ExprResult Res =
2653f4a2713aSLionel Sambuc PerformImplicitConversion(From, BeforeToType,
2654f4a2713aSLionel Sambuc ICS.UserDefined.Before, AA_Converting,
2655f4a2713aSLionel Sambuc CCK);
2656f4a2713aSLionel Sambuc if (Res.isInvalid())
2657f4a2713aSLionel Sambuc return ExprError();
2658*0a6a1f1dSLionel Sambuc From = Res.get();
2659f4a2713aSLionel Sambuc }
2660f4a2713aSLionel Sambuc
2661f4a2713aSLionel Sambuc ExprResult CastArg
2662f4a2713aSLionel Sambuc = BuildCXXCastArgument(*this,
2663f4a2713aSLionel Sambuc From->getLocStart(),
2664f4a2713aSLionel Sambuc ToType.getNonReferenceType(),
2665f4a2713aSLionel Sambuc CastKind, cast<CXXMethodDecl>(FD),
2666f4a2713aSLionel Sambuc ICS.UserDefined.FoundConversionFunction,
2667f4a2713aSLionel Sambuc ICS.UserDefined.HadMultipleCandidates,
2668f4a2713aSLionel Sambuc From);
2669f4a2713aSLionel Sambuc
2670f4a2713aSLionel Sambuc if (CastArg.isInvalid())
2671f4a2713aSLionel Sambuc return ExprError();
2672f4a2713aSLionel Sambuc
2673*0a6a1f1dSLionel Sambuc From = CastArg.get();
2674f4a2713aSLionel Sambuc
2675f4a2713aSLionel Sambuc return PerformImplicitConversion(From, ToType, ICS.UserDefined.After,
2676f4a2713aSLionel Sambuc AA_Converting, CCK);
2677f4a2713aSLionel Sambuc }
2678f4a2713aSLionel Sambuc
2679f4a2713aSLionel Sambuc case ImplicitConversionSequence::AmbiguousConversion:
2680f4a2713aSLionel Sambuc ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(),
2681f4a2713aSLionel Sambuc PDiag(diag::err_typecheck_ambiguous_condition)
2682f4a2713aSLionel Sambuc << From->getSourceRange());
2683f4a2713aSLionel Sambuc return ExprError();
2684f4a2713aSLionel Sambuc
2685f4a2713aSLionel Sambuc case ImplicitConversionSequence::EllipsisConversion:
2686f4a2713aSLionel Sambuc llvm_unreachable("Cannot perform an ellipsis conversion");
2687f4a2713aSLionel Sambuc
2688f4a2713aSLionel Sambuc case ImplicitConversionSequence::BadConversion:
2689f4a2713aSLionel Sambuc return ExprError();
2690f4a2713aSLionel Sambuc }
2691f4a2713aSLionel Sambuc
2692f4a2713aSLionel Sambuc // Everything went well.
2693*0a6a1f1dSLionel Sambuc return From;
2694f4a2713aSLionel Sambuc }
2695f4a2713aSLionel Sambuc
2696f4a2713aSLionel Sambuc /// PerformImplicitConversion - Perform an implicit conversion of the
2697f4a2713aSLionel Sambuc /// expression From to the type ToType by following the standard
2698f4a2713aSLionel Sambuc /// conversion sequence SCS. Returns the converted
2699f4a2713aSLionel Sambuc /// expression. Flavor is the context in which we're performing this
2700f4a2713aSLionel Sambuc /// conversion, for use in error messages.
2701f4a2713aSLionel Sambuc ExprResult
PerformImplicitConversion(Expr * From,QualType ToType,const StandardConversionSequence & SCS,AssignmentAction Action,CheckedConversionKind CCK)2702f4a2713aSLionel Sambuc Sema::PerformImplicitConversion(Expr *From, QualType ToType,
2703f4a2713aSLionel Sambuc const StandardConversionSequence& SCS,
2704f4a2713aSLionel Sambuc AssignmentAction Action,
2705f4a2713aSLionel Sambuc CheckedConversionKind CCK) {
2706f4a2713aSLionel Sambuc bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast);
2707f4a2713aSLionel Sambuc
2708f4a2713aSLionel Sambuc // Overall FIXME: we are recomputing too many types here and doing far too
2709f4a2713aSLionel Sambuc // much extra work. What this means is that we need to keep track of more
2710f4a2713aSLionel Sambuc // information that is computed when we try the implicit conversion initially,
2711f4a2713aSLionel Sambuc // so that we don't need to recompute anything here.
2712f4a2713aSLionel Sambuc QualType FromType = From->getType();
2713f4a2713aSLionel Sambuc
2714f4a2713aSLionel Sambuc if (SCS.CopyConstructor) {
2715f4a2713aSLionel Sambuc // FIXME: When can ToType be a reference type?
2716f4a2713aSLionel Sambuc assert(!ToType->isReferenceType());
2717f4a2713aSLionel Sambuc if (SCS.Second == ICK_Derived_To_Base) {
2718f4a2713aSLionel Sambuc SmallVector<Expr*, 8> ConstructorArgs;
2719f4a2713aSLionel Sambuc if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor),
2720f4a2713aSLionel Sambuc From, /*FIXME:ConstructLoc*/SourceLocation(),
2721f4a2713aSLionel Sambuc ConstructorArgs))
2722f4a2713aSLionel Sambuc return ExprError();
2723*0a6a1f1dSLionel Sambuc return BuildCXXConstructExpr(
2724*0a6a1f1dSLionel Sambuc /*FIXME:ConstructLoc*/ SourceLocation(), ToType, SCS.CopyConstructor,
2725*0a6a1f1dSLionel Sambuc ConstructorArgs, /*HadMultipleCandidates*/ false,
2726*0a6a1f1dSLionel Sambuc /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false,
2727*0a6a1f1dSLionel Sambuc CXXConstructExpr::CK_Complete, SourceRange());
2728f4a2713aSLionel Sambuc }
2729*0a6a1f1dSLionel Sambuc return BuildCXXConstructExpr(
2730*0a6a1f1dSLionel Sambuc /*FIXME:ConstructLoc*/ SourceLocation(), ToType, SCS.CopyConstructor,
2731f4a2713aSLionel Sambuc From, /*HadMultipleCandidates*/ false,
2732*0a6a1f1dSLionel Sambuc /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false,
2733*0a6a1f1dSLionel Sambuc CXXConstructExpr::CK_Complete, SourceRange());
2734f4a2713aSLionel Sambuc }
2735f4a2713aSLionel Sambuc
2736f4a2713aSLionel Sambuc // Resolve overloaded function references.
2737f4a2713aSLionel Sambuc if (Context.hasSameType(FromType, Context.OverloadTy)) {
2738f4a2713aSLionel Sambuc DeclAccessPair Found;
2739f4a2713aSLionel Sambuc FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType,
2740f4a2713aSLionel Sambuc true, Found);
2741f4a2713aSLionel Sambuc if (!Fn)
2742f4a2713aSLionel Sambuc return ExprError();
2743f4a2713aSLionel Sambuc
2744f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(Fn, From->getLocStart()))
2745f4a2713aSLionel Sambuc return ExprError();
2746f4a2713aSLionel Sambuc
2747f4a2713aSLionel Sambuc From = FixOverloadedFunctionReference(From, Found, Fn);
2748f4a2713aSLionel Sambuc FromType = From->getType();
2749f4a2713aSLionel Sambuc }
2750f4a2713aSLionel Sambuc
2751f4a2713aSLionel Sambuc // If we're converting to an atomic type, first convert to the corresponding
2752f4a2713aSLionel Sambuc // non-atomic type.
2753f4a2713aSLionel Sambuc QualType ToAtomicType;
2754f4a2713aSLionel Sambuc if (const AtomicType *ToAtomic = ToType->getAs<AtomicType>()) {
2755f4a2713aSLionel Sambuc ToAtomicType = ToType;
2756f4a2713aSLionel Sambuc ToType = ToAtomic->getValueType();
2757f4a2713aSLionel Sambuc }
2758f4a2713aSLionel Sambuc
2759f4a2713aSLionel Sambuc // Perform the first implicit conversion.
2760f4a2713aSLionel Sambuc switch (SCS.First) {
2761f4a2713aSLionel Sambuc case ICK_Identity:
2762*0a6a1f1dSLionel Sambuc if (const AtomicType *FromAtomic = FromType->getAs<AtomicType>()) {
2763*0a6a1f1dSLionel Sambuc FromType = FromAtomic->getValueType().getUnqualifiedType();
2764*0a6a1f1dSLionel Sambuc From = ImplicitCastExpr::Create(Context, FromType, CK_AtomicToNonAtomic,
2765*0a6a1f1dSLionel Sambuc From, /*BasePath=*/nullptr, VK_RValue);
2766*0a6a1f1dSLionel Sambuc }
2767f4a2713aSLionel Sambuc break;
2768f4a2713aSLionel Sambuc
2769f4a2713aSLionel Sambuc case ICK_Lvalue_To_Rvalue: {
2770f4a2713aSLionel Sambuc assert(From->getObjectKind() != OK_ObjCProperty);
2771f4a2713aSLionel Sambuc ExprResult FromRes = DefaultLvalueConversion(From);
2772f4a2713aSLionel Sambuc assert(!FromRes.isInvalid() && "Can't perform deduced conversion?!");
2773*0a6a1f1dSLionel Sambuc From = FromRes.get();
2774*0a6a1f1dSLionel Sambuc FromType = From->getType();
2775f4a2713aSLionel Sambuc break;
2776f4a2713aSLionel Sambuc }
2777f4a2713aSLionel Sambuc
2778f4a2713aSLionel Sambuc case ICK_Array_To_Pointer:
2779f4a2713aSLionel Sambuc FromType = Context.getArrayDecayedType(FromType);
2780f4a2713aSLionel Sambuc From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay,
2781*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2782f4a2713aSLionel Sambuc break;
2783f4a2713aSLionel Sambuc
2784f4a2713aSLionel Sambuc case ICK_Function_To_Pointer:
2785f4a2713aSLionel Sambuc FromType = Context.getPointerType(FromType);
2786f4a2713aSLionel Sambuc From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay,
2787*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2788f4a2713aSLionel Sambuc break;
2789f4a2713aSLionel Sambuc
2790f4a2713aSLionel Sambuc default:
2791f4a2713aSLionel Sambuc llvm_unreachable("Improper first standard conversion");
2792f4a2713aSLionel Sambuc }
2793f4a2713aSLionel Sambuc
2794f4a2713aSLionel Sambuc // Perform the second implicit conversion
2795f4a2713aSLionel Sambuc switch (SCS.Second) {
2796f4a2713aSLionel Sambuc case ICK_Identity:
2797*0a6a1f1dSLionel Sambuc // C++ [except.spec]p5:
2798*0a6a1f1dSLionel Sambuc // [For] assignment to and initialization of pointers to functions,
2799*0a6a1f1dSLionel Sambuc // pointers to member functions, and references to functions: the
2800*0a6a1f1dSLionel Sambuc // target entity shall allow at least the exceptions allowed by the
2801*0a6a1f1dSLionel Sambuc // source value in the assignment or initialization.
2802*0a6a1f1dSLionel Sambuc switch (Action) {
2803*0a6a1f1dSLionel Sambuc case AA_Assigning:
2804*0a6a1f1dSLionel Sambuc case AA_Initializing:
2805*0a6a1f1dSLionel Sambuc // Note, function argument passing and returning are initialization.
2806*0a6a1f1dSLionel Sambuc case AA_Passing:
2807*0a6a1f1dSLionel Sambuc case AA_Returning:
2808*0a6a1f1dSLionel Sambuc case AA_Sending:
2809*0a6a1f1dSLionel Sambuc case AA_Passing_CFAudited:
2810f4a2713aSLionel Sambuc if (CheckExceptionSpecCompatibility(From, ToType))
2811f4a2713aSLionel Sambuc return ExprError();
2812*0a6a1f1dSLionel Sambuc break;
2813*0a6a1f1dSLionel Sambuc
2814*0a6a1f1dSLionel Sambuc case AA_Casting:
2815*0a6a1f1dSLionel Sambuc case AA_Converting:
2816*0a6a1f1dSLionel Sambuc // Casts and implicit conversions are not initialization, so are not
2817*0a6a1f1dSLionel Sambuc // checked for exception specification mismatches.
2818*0a6a1f1dSLionel Sambuc break;
2819*0a6a1f1dSLionel Sambuc }
2820f4a2713aSLionel Sambuc // Nothing else to do.
2821f4a2713aSLionel Sambuc break;
2822f4a2713aSLionel Sambuc
2823f4a2713aSLionel Sambuc case ICK_NoReturn_Adjustment:
2824f4a2713aSLionel Sambuc // If both sides are functions (or pointers/references to them), there could
2825f4a2713aSLionel Sambuc // be incompatible exception declarations.
2826f4a2713aSLionel Sambuc if (CheckExceptionSpecCompatibility(From, ToType))
2827f4a2713aSLionel Sambuc return ExprError();
2828f4a2713aSLionel Sambuc
2829f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, CK_NoOp,
2830*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2831f4a2713aSLionel Sambuc break;
2832f4a2713aSLionel Sambuc
2833f4a2713aSLionel Sambuc case ICK_Integral_Promotion:
2834f4a2713aSLionel Sambuc case ICK_Integral_Conversion:
2835f4a2713aSLionel Sambuc if (ToType->isBooleanType()) {
2836f4a2713aSLionel Sambuc assert(FromType->castAs<EnumType>()->getDecl()->isFixed() &&
2837f4a2713aSLionel Sambuc SCS.Second == ICK_Integral_Promotion &&
2838f4a2713aSLionel Sambuc "only enums with fixed underlying type can promote to bool");
2839f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, CK_IntegralToBoolean,
2840*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2841f4a2713aSLionel Sambuc } else {
2842f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, CK_IntegralCast,
2843*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2844f4a2713aSLionel Sambuc }
2845f4a2713aSLionel Sambuc break;
2846f4a2713aSLionel Sambuc
2847f4a2713aSLionel Sambuc case ICK_Floating_Promotion:
2848f4a2713aSLionel Sambuc case ICK_Floating_Conversion:
2849f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, CK_FloatingCast,
2850*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2851f4a2713aSLionel Sambuc break;
2852f4a2713aSLionel Sambuc
2853f4a2713aSLionel Sambuc case ICK_Complex_Promotion:
2854f4a2713aSLionel Sambuc case ICK_Complex_Conversion: {
2855f4a2713aSLionel Sambuc QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType();
2856f4a2713aSLionel Sambuc QualType ToEl = ToType->getAs<ComplexType>()->getElementType();
2857f4a2713aSLionel Sambuc CastKind CK;
2858f4a2713aSLionel Sambuc if (FromEl->isRealFloatingType()) {
2859f4a2713aSLionel Sambuc if (ToEl->isRealFloatingType())
2860f4a2713aSLionel Sambuc CK = CK_FloatingComplexCast;
2861f4a2713aSLionel Sambuc else
2862f4a2713aSLionel Sambuc CK = CK_FloatingComplexToIntegralComplex;
2863f4a2713aSLionel Sambuc } else if (ToEl->isRealFloatingType()) {
2864f4a2713aSLionel Sambuc CK = CK_IntegralComplexToFloatingComplex;
2865f4a2713aSLionel Sambuc } else {
2866f4a2713aSLionel Sambuc CK = CK_IntegralComplexCast;
2867f4a2713aSLionel Sambuc }
2868f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, CK,
2869*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2870f4a2713aSLionel Sambuc break;
2871f4a2713aSLionel Sambuc }
2872f4a2713aSLionel Sambuc
2873f4a2713aSLionel Sambuc case ICK_Floating_Integral:
2874f4a2713aSLionel Sambuc if (ToType->isRealFloatingType())
2875f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, CK_IntegralToFloating,
2876*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2877f4a2713aSLionel Sambuc else
2878f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral,
2879*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2880f4a2713aSLionel Sambuc break;
2881f4a2713aSLionel Sambuc
2882f4a2713aSLionel Sambuc case ICK_Compatible_Conversion:
2883f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, CK_NoOp,
2884*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2885f4a2713aSLionel Sambuc break;
2886f4a2713aSLionel Sambuc
2887f4a2713aSLionel Sambuc case ICK_Writeback_Conversion:
2888f4a2713aSLionel Sambuc case ICK_Pointer_Conversion: {
2889f4a2713aSLionel Sambuc if (SCS.IncompatibleObjC && Action != AA_Casting) {
2890f4a2713aSLionel Sambuc // Diagnose incompatible Objective-C conversions
2891f4a2713aSLionel Sambuc if (Action == AA_Initializing || Action == AA_Assigning)
2892f4a2713aSLionel Sambuc Diag(From->getLocStart(),
2893f4a2713aSLionel Sambuc diag::ext_typecheck_convert_incompatible_pointer)
2894f4a2713aSLionel Sambuc << ToType << From->getType() << Action
2895f4a2713aSLionel Sambuc << From->getSourceRange() << 0;
2896f4a2713aSLionel Sambuc else
2897f4a2713aSLionel Sambuc Diag(From->getLocStart(),
2898f4a2713aSLionel Sambuc diag::ext_typecheck_convert_incompatible_pointer)
2899f4a2713aSLionel Sambuc << From->getType() << ToType << Action
2900f4a2713aSLionel Sambuc << From->getSourceRange() << 0;
2901f4a2713aSLionel Sambuc
2902f4a2713aSLionel Sambuc if (From->getType()->isObjCObjectPointerType() &&
2903f4a2713aSLionel Sambuc ToType->isObjCObjectPointerType())
2904f4a2713aSLionel Sambuc EmitRelatedResultTypeNote(From);
2905f4a2713aSLionel Sambuc }
2906f4a2713aSLionel Sambuc else if (getLangOpts().ObjCAutoRefCount &&
2907f4a2713aSLionel Sambuc !CheckObjCARCUnavailableWeakConversion(ToType,
2908f4a2713aSLionel Sambuc From->getType())) {
2909f4a2713aSLionel Sambuc if (Action == AA_Initializing)
2910f4a2713aSLionel Sambuc Diag(From->getLocStart(),
2911f4a2713aSLionel Sambuc diag::err_arc_weak_unavailable_assign);
2912f4a2713aSLionel Sambuc else
2913f4a2713aSLionel Sambuc Diag(From->getLocStart(),
2914f4a2713aSLionel Sambuc diag::err_arc_convesion_of_weak_unavailable)
2915f4a2713aSLionel Sambuc << (Action == AA_Casting) << From->getType() << ToType
2916f4a2713aSLionel Sambuc << From->getSourceRange();
2917f4a2713aSLionel Sambuc }
2918f4a2713aSLionel Sambuc
2919f4a2713aSLionel Sambuc CastKind Kind = CK_Invalid;
2920f4a2713aSLionel Sambuc CXXCastPath BasePath;
2921f4a2713aSLionel Sambuc if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle))
2922f4a2713aSLionel Sambuc return ExprError();
2923f4a2713aSLionel Sambuc
2924f4a2713aSLionel Sambuc // Make sure we extend blocks if necessary.
2925f4a2713aSLionel Sambuc // FIXME: doing this here is really ugly.
2926f4a2713aSLionel Sambuc if (Kind == CK_BlockPointerToObjCPointerCast) {
2927f4a2713aSLionel Sambuc ExprResult E = From;
2928f4a2713aSLionel Sambuc (void) PrepareCastToObjCObjectPointer(E);
2929*0a6a1f1dSLionel Sambuc From = E.get();
2930f4a2713aSLionel Sambuc }
2931f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount)
2932f4a2713aSLionel Sambuc CheckObjCARCConversion(SourceRange(), ToType, From, CCK);
2933f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK)
2934*0a6a1f1dSLionel Sambuc .get();
2935f4a2713aSLionel Sambuc break;
2936f4a2713aSLionel Sambuc }
2937f4a2713aSLionel Sambuc
2938f4a2713aSLionel Sambuc case ICK_Pointer_Member: {
2939f4a2713aSLionel Sambuc CastKind Kind = CK_Invalid;
2940f4a2713aSLionel Sambuc CXXCastPath BasePath;
2941f4a2713aSLionel Sambuc if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle))
2942f4a2713aSLionel Sambuc return ExprError();
2943f4a2713aSLionel Sambuc if (CheckExceptionSpecCompatibility(From, ToType))
2944f4a2713aSLionel Sambuc return ExprError();
2945*0a6a1f1dSLionel Sambuc
2946*0a6a1f1dSLionel Sambuc // We may not have been able to figure out what this member pointer resolved
2947*0a6a1f1dSLionel Sambuc // to up until this exact point. Attempt to lock-in it's inheritance model.
2948*0a6a1f1dSLionel Sambuc QualType FromType = From->getType();
2949*0a6a1f1dSLionel Sambuc if (FromType->isMemberPointerType())
2950*0a6a1f1dSLionel Sambuc if (Context.getTargetInfo().getCXXABI().isMicrosoft())
2951*0a6a1f1dSLionel Sambuc RequireCompleteType(From->getExprLoc(), FromType, 0);
2952*0a6a1f1dSLionel Sambuc
2953f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK)
2954*0a6a1f1dSLionel Sambuc .get();
2955f4a2713aSLionel Sambuc break;
2956f4a2713aSLionel Sambuc }
2957f4a2713aSLionel Sambuc
2958f4a2713aSLionel Sambuc case ICK_Boolean_Conversion:
2959f4a2713aSLionel Sambuc // Perform half-to-boolean conversion via float.
2960f4a2713aSLionel Sambuc if (From->getType()->isHalfType()) {
2961*0a6a1f1dSLionel Sambuc From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).get();
2962f4a2713aSLionel Sambuc FromType = Context.FloatTy;
2963f4a2713aSLionel Sambuc }
2964f4a2713aSLionel Sambuc
2965f4a2713aSLionel Sambuc From = ImpCastExprToType(From, Context.BoolTy,
2966f4a2713aSLionel Sambuc ScalarTypeToBooleanCastKind(FromType),
2967*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2968f4a2713aSLionel Sambuc break;
2969f4a2713aSLionel Sambuc
2970f4a2713aSLionel Sambuc case ICK_Derived_To_Base: {
2971f4a2713aSLionel Sambuc CXXCastPath BasePath;
2972f4a2713aSLionel Sambuc if (CheckDerivedToBaseConversion(From->getType(),
2973f4a2713aSLionel Sambuc ToType.getNonReferenceType(),
2974f4a2713aSLionel Sambuc From->getLocStart(),
2975f4a2713aSLionel Sambuc From->getSourceRange(),
2976f4a2713aSLionel Sambuc &BasePath,
2977f4a2713aSLionel Sambuc CStyle))
2978f4a2713aSLionel Sambuc return ExprError();
2979f4a2713aSLionel Sambuc
2980f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType.getNonReferenceType(),
2981f4a2713aSLionel Sambuc CK_DerivedToBase, From->getValueKind(),
2982*0a6a1f1dSLionel Sambuc &BasePath, CCK).get();
2983f4a2713aSLionel Sambuc break;
2984f4a2713aSLionel Sambuc }
2985f4a2713aSLionel Sambuc
2986f4a2713aSLionel Sambuc case ICK_Vector_Conversion:
2987f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, CK_BitCast,
2988*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2989f4a2713aSLionel Sambuc break;
2990f4a2713aSLionel Sambuc
2991f4a2713aSLionel Sambuc case ICK_Vector_Splat:
2992f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType, CK_VectorSplat,
2993*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
2994f4a2713aSLionel Sambuc break;
2995f4a2713aSLionel Sambuc
2996f4a2713aSLionel Sambuc case ICK_Complex_Real:
2997f4a2713aSLionel Sambuc // Case 1. x -> _Complex y
2998f4a2713aSLionel Sambuc if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) {
2999f4a2713aSLionel Sambuc QualType ElType = ToComplex->getElementType();
3000f4a2713aSLionel Sambuc bool isFloatingComplex = ElType->isRealFloatingType();
3001f4a2713aSLionel Sambuc
3002f4a2713aSLionel Sambuc // x -> y
3003f4a2713aSLionel Sambuc if (Context.hasSameUnqualifiedType(ElType, From->getType())) {
3004f4a2713aSLionel Sambuc // do nothing
3005f4a2713aSLionel Sambuc } else if (From->getType()->isRealFloatingType()) {
3006f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ElType,
3007*0a6a1f1dSLionel Sambuc isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).get();
3008f4a2713aSLionel Sambuc } else {
3009f4a2713aSLionel Sambuc assert(From->getType()->isIntegerType());
3010f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ElType,
3011*0a6a1f1dSLionel Sambuc isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).get();
3012f4a2713aSLionel Sambuc }
3013f4a2713aSLionel Sambuc // y -> _Complex y
3014f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType,
3015f4a2713aSLionel Sambuc isFloatingComplex ? CK_FloatingRealToComplex
3016*0a6a1f1dSLionel Sambuc : CK_IntegralRealToComplex).get();
3017f4a2713aSLionel Sambuc
3018f4a2713aSLionel Sambuc // Case 2. _Complex x -> y
3019f4a2713aSLionel Sambuc } else {
3020f4a2713aSLionel Sambuc const ComplexType *FromComplex = From->getType()->getAs<ComplexType>();
3021f4a2713aSLionel Sambuc assert(FromComplex);
3022f4a2713aSLionel Sambuc
3023f4a2713aSLionel Sambuc QualType ElType = FromComplex->getElementType();
3024f4a2713aSLionel Sambuc bool isFloatingComplex = ElType->isRealFloatingType();
3025f4a2713aSLionel Sambuc
3026f4a2713aSLionel Sambuc // _Complex x -> x
3027f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ElType,
3028f4a2713aSLionel Sambuc isFloatingComplex ? CK_FloatingComplexToReal
3029f4a2713aSLionel Sambuc : CK_IntegralComplexToReal,
3030*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
3031f4a2713aSLionel Sambuc
3032f4a2713aSLionel Sambuc // x -> y
3033f4a2713aSLionel Sambuc if (Context.hasSameUnqualifiedType(ElType, ToType)) {
3034f4a2713aSLionel Sambuc // do nothing
3035f4a2713aSLionel Sambuc } else if (ToType->isRealFloatingType()) {
3036f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType,
3037f4a2713aSLionel Sambuc isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating,
3038*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
3039f4a2713aSLionel Sambuc } else {
3040f4a2713aSLionel Sambuc assert(ToType->isIntegerType());
3041f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType,
3042f4a2713aSLionel Sambuc isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast,
3043*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
3044f4a2713aSLionel Sambuc }
3045f4a2713aSLionel Sambuc }
3046f4a2713aSLionel Sambuc break;
3047f4a2713aSLionel Sambuc
3048f4a2713aSLionel Sambuc case ICK_Block_Pointer_Conversion: {
3049f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast,
3050*0a6a1f1dSLionel Sambuc VK_RValue, /*BasePath=*/nullptr, CCK).get();
3051f4a2713aSLionel Sambuc break;
3052f4a2713aSLionel Sambuc }
3053f4a2713aSLionel Sambuc
3054f4a2713aSLionel Sambuc case ICK_TransparentUnionConversion: {
3055*0a6a1f1dSLionel Sambuc ExprResult FromRes = From;
3056f4a2713aSLionel Sambuc Sema::AssignConvertType ConvTy =
3057f4a2713aSLionel Sambuc CheckTransparentUnionArgumentConstraints(ToType, FromRes);
3058f4a2713aSLionel Sambuc if (FromRes.isInvalid())
3059f4a2713aSLionel Sambuc return ExprError();
3060*0a6a1f1dSLionel Sambuc From = FromRes.get();
3061f4a2713aSLionel Sambuc assert ((ConvTy == Sema::Compatible) &&
3062f4a2713aSLionel Sambuc "Improper transparent union conversion");
3063f4a2713aSLionel Sambuc (void)ConvTy;
3064f4a2713aSLionel Sambuc break;
3065f4a2713aSLionel Sambuc }
3066f4a2713aSLionel Sambuc
3067f4a2713aSLionel Sambuc case ICK_Zero_Event_Conversion:
3068f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType,
3069f4a2713aSLionel Sambuc CK_ZeroToOCLEvent,
3070*0a6a1f1dSLionel Sambuc From->getValueKind()).get();
3071f4a2713aSLionel Sambuc break;
3072f4a2713aSLionel Sambuc
3073f4a2713aSLionel Sambuc case ICK_Lvalue_To_Rvalue:
3074f4a2713aSLionel Sambuc case ICK_Array_To_Pointer:
3075f4a2713aSLionel Sambuc case ICK_Function_To_Pointer:
3076f4a2713aSLionel Sambuc case ICK_Qualification:
3077f4a2713aSLionel Sambuc case ICK_Num_Conversion_Kinds:
3078f4a2713aSLionel Sambuc llvm_unreachable("Improper second standard conversion");
3079f4a2713aSLionel Sambuc }
3080f4a2713aSLionel Sambuc
3081f4a2713aSLionel Sambuc switch (SCS.Third) {
3082f4a2713aSLionel Sambuc case ICK_Identity:
3083f4a2713aSLionel Sambuc // Nothing to do.
3084f4a2713aSLionel Sambuc break;
3085f4a2713aSLionel Sambuc
3086f4a2713aSLionel Sambuc case ICK_Qualification: {
3087f4a2713aSLionel Sambuc // The qualification keeps the category of the inner expression, unless the
3088f4a2713aSLionel Sambuc // target type isn't a reference.
3089f4a2713aSLionel Sambuc ExprValueKind VK = ToType->isReferenceType() ?
3090f4a2713aSLionel Sambuc From->getValueKind() : VK_RValue;
3091f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context),
3092*0a6a1f1dSLionel Sambuc CK_NoOp, VK, /*BasePath=*/nullptr, CCK).get();
3093f4a2713aSLionel Sambuc
3094f4a2713aSLionel Sambuc if (SCS.DeprecatedStringLiteralToCharPtr &&
3095*0a6a1f1dSLionel Sambuc !getLangOpts().WritableStrings) {
3096*0a6a1f1dSLionel Sambuc Diag(From->getLocStart(), getLangOpts().CPlusPlus11
3097*0a6a1f1dSLionel Sambuc ? diag::ext_deprecated_string_literal_conversion
3098*0a6a1f1dSLionel Sambuc : diag::warn_deprecated_string_literal_conversion)
3099f4a2713aSLionel Sambuc << ToType.getNonReferenceType();
3100*0a6a1f1dSLionel Sambuc }
3101f4a2713aSLionel Sambuc
3102f4a2713aSLionel Sambuc break;
3103f4a2713aSLionel Sambuc }
3104f4a2713aSLionel Sambuc
3105f4a2713aSLionel Sambuc default:
3106f4a2713aSLionel Sambuc llvm_unreachable("Improper third standard conversion");
3107f4a2713aSLionel Sambuc }
3108f4a2713aSLionel Sambuc
3109f4a2713aSLionel Sambuc // If this conversion sequence involved a scalar -> atomic conversion, perform
3110f4a2713aSLionel Sambuc // that conversion now.
3111f4a2713aSLionel Sambuc if (!ToAtomicType.isNull()) {
3112f4a2713aSLionel Sambuc assert(Context.hasSameType(
3113f4a2713aSLionel Sambuc ToAtomicType->castAs<AtomicType>()->getValueType(), From->getType()));
3114f4a2713aSLionel Sambuc From = ImpCastExprToType(From, ToAtomicType, CK_NonAtomicToAtomic,
3115*0a6a1f1dSLionel Sambuc VK_RValue, nullptr, CCK).get();
3116f4a2713aSLionel Sambuc }
3117f4a2713aSLionel Sambuc
3118*0a6a1f1dSLionel Sambuc return From;
3119f4a2713aSLionel Sambuc }
3120f4a2713aSLionel Sambuc
3121f4a2713aSLionel Sambuc /// \brief Check the completeness of a type in a unary type trait.
3122f4a2713aSLionel Sambuc ///
3123f4a2713aSLionel Sambuc /// If the particular type trait requires a complete type, tries to complete
3124f4a2713aSLionel Sambuc /// it. If completing the type fails, a diagnostic is emitted and false
3125f4a2713aSLionel Sambuc /// returned. If completing the type succeeds or no completion was required,
3126f4a2713aSLionel Sambuc /// returns true.
CheckUnaryTypeTraitTypeCompleteness(Sema & S,TypeTrait UTT,SourceLocation Loc,QualType ArgTy)3127*0a6a1f1dSLionel Sambuc static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT,
3128f4a2713aSLionel Sambuc SourceLocation Loc,
3129f4a2713aSLionel Sambuc QualType ArgTy) {
3130f4a2713aSLionel Sambuc // C++0x [meta.unary.prop]p3:
3131f4a2713aSLionel Sambuc // For all of the class templates X declared in this Clause, instantiating
3132f4a2713aSLionel Sambuc // that template with a template argument that is a class template
3133f4a2713aSLionel Sambuc // specialization may result in the implicit instantiation of the template
3134f4a2713aSLionel Sambuc // argument if and only if the semantics of X require that the argument
3135f4a2713aSLionel Sambuc // must be a complete type.
3136f4a2713aSLionel Sambuc // We apply this rule to all the type trait expressions used to implement
3137f4a2713aSLionel Sambuc // these class templates. We also try to follow any GCC documented behavior
3138f4a2713aSLionel Sambuc // in these expressions to ensure portability of standard libraries.
3139f4a2713aSLionel Sambuc switch (UTT) {
3140*0a6a1f1dSLionel Sambuc default: llvm_unreachable("not a UTT");
3141f4a2713aSLionel Sambuc // is_complete_type somewhat obviously cannot require a complete type.
3142f4a2713aSLionel Sambuc case UTT_IsCompleteType:
3143f4a2713aSLionel Sambuc // Fall-through
3144f4a2713aSLionel Sambuc
3145f4a2713aSLionel Sambuc // These traits are modeled on the type predicates in C++0x
3146f4a2713aSLionel Sambuc // [meta.unary.cat] and [meta.unary.comp]. They are not specified as
3147f4a2713aSLionel Sambuc // requiring a complete type, as whether or not they return true cannot be
3148f4a2713aSLionel Sambuc // impacted by the completeness of the type.
3149f4a2713aSLionel Sambuc case UTT_IsVoid:
3150f4a2713aSLionel Sambuc case UTT_IsIntegral:
3151f4a2713aSLionel Sambuc case UTT_IsFloatingPoint:
3152f4a2713aSLionel Sambuc case UTT_IsArray:
3153f4a2713aSLionel Sambuc case UTT_IsPointer:
3154f4a2713aSLionel Sambuc case UTT_IsLvalueReference:
3155f4a2713aSLionel Sambuc case UTT_IsRvalueReference:
3156f4a2713aSLionel Sambuc case UTT_IsMemberFunctionPointer:
3157f4a2713aSLionel Sambuc case UTT_IsMemberObjectPointer:
3158f4a2713aSLionel Sambuc case UTT_IsEnum:
3159f4a2713aSLionel Sambuc case UTT_IsUnion:
3160f4a2713aSLionel Sambuc case UTT_IsClass:
3161f4a2713aSLionel Sambuc case UTT_IsFunction:
3162f4a2713aSLionel Sambuc case UTT_IsReference:
3163f4a2713aSLionel Sambuc case UTT_IsArithmetic:
3164f4a2713aSLionel Sambuc case UTT_IsFundamental:
3165f4a2713aSLionel Sambuc case UTT_IsObject:
3166f4a2713aSLionel Sambuc case UTT_IsScalar:
3167f4a2713aSLionel Sambuc case UTT_IsCompound:
3168f4a2713aSLionel Sambuc case UTT_IsMemberPointer:
3169f4a2713aSLionel Sambuc // Fall-through
3170f4a2713aSLionel Sambuc
3171f4a2713aSLionel Sambuc // These traits are modeled on type predicates in C++0x [meta.unary.prop]
3172f4a2713aSLionel Sambuc // which requires some of its traits to have the complete type. However,
3173f4a2713aSLionel Sambuc // the completeness of the type cannot impact these traits' semantics, and
3174f4a2713aSLionel Sambuc // so they don't require it. This matches the comments on these traits in
3175f4a2713aSLionel Sambuc // Table 49.
3176f4a2713aSLionel Sambuc case UTT_IsConst:
3177f4a2713aSLionel Sambuc case UTT_IsVolatile:
3178f4a2713aSLionel Sambuc case UTT_IsSigned:
3179f4a2713aSLionel Sambuc case UTT_IsUnsigned:
3180f4a2713aSLionel Sambuc return true;
3181f4a2713aSLionel Sambuc
3182f4a2713aSLionel Sambuc // C++0x [meta.unary.prop] Table 49 requires the following traits to be
3183f4a2713aSLionel Sambuc // applied to a complete type.
3184f4a2713aSLionel Sambuc case UTT_IsTrivial:
3185f4a2713aSLionel Sambuc case UTT_IsTriviallyCopyable:
3186f4a2713aSLionel Sambuc case UTT_IsStandardLayout:
3187f4a2713aSLionel Sambuc case UTT_IsPOD:
3188f4a2713aSLionel Sambuc case UTT_IsLiteral:
3189f4a2713aSLionel Sambuc case UTT_IsEmpty:
3190f4a2713aSLionel Sambuc case UTT_IsPolymorphic:
3191f4a2713aSLionel Sambuc case UTT_IsAbstract:
3192f4a2713aSLionel Sambuc case UTT_IsInterfaceClass:
3193*0a6a1f1dSLionel Sambuc case UTT_IsDestructible:
3194*0a6a1f1dSLionel Sambuc case UTT_IsNothrowDestructible:
3195f4a2713aSLionel Sambuc // Fall-through
3196f4a2713aSLionel Sambuc
3197f4a2713aSLionel Sambuc // These traits require a complete type.
3198f4a2713aSLionel Sambuc case UTT_IsFinal:
3199f4a2713aSLionel Sambuc case UTT_IsSealed:
3200f4a2713aSLionel Sambuc
3201f4a2713aSLionel Sambuc // These trait expressions are designed to help implement predicates in
3202f4a2713aSLionel Sambuc // [meta.unary.prop] despite not being named the same. They are specified
3203f4a2713aSLionel Sambuc // by both GCC and the Embarcadero C++ compiler, and require the complete
3204f4a2713aSLionel Sambuc // type due to the overarching C++0x type predicates being implemented
3205f4a2713aSLionel Sambuc // requiring the complete type.
3206f4a2713aSLionel Sambuc case UTT_HasNothrowAssign:
3207f4a2713aSLionel Sambuc case UTT_HasNothrowMoveAssign:
3208f4a2713aSLionel Sambuc case UTT_HasNothrowConstructor:
3209f4a2713aSLionel Sambuc case UTT_HasNothrowCopy:
3210f4a2713aSLionel Sambuc case UTT_HasTrivialAssign:
3211f4a2713aSLionel Sambuc case UTT_HasTrivialMoveAssign:
3212f4a2713aSLionel Sambuc case UTT_HasTrivialDefaultConstructor:
3213f4a2713aSLionel Sambuc case UTT_HasTrivialMoveConstructor:
3214f4a2713aSLionel Sambuc case UTT_HasTrivialCopy:
3215f4a2713aSLionel Sambuc case UTT_HasTrivialDestructor:
3216f4a2713aSLionel Sambuc case UTT_HasVirtualDestructor:
3217f4a2713aSLionel Sambuc // Arrays of unknown bound are expressly allowed.
3218f4a2713aSLionel Sambuc QualType ElTy = ArgTy;
3219f4a2713aSLionel Sambuc if (ArgTy->isIncompleteArrayType())
3220f4a2713aSLionel Sambuc ElTy = S.Context.getAsArrayType(ArgTy)->getElementType();
3221f4a2713aSLionel Sambuc
3222f4a2713aSLionel Sambuc // The void type is expressly allowed.
3223f4a2713aSLionel Sambuc if (ElTy->isVoidType())
3224f4a2713aSLionel Sambuc return true;
3225f4a2713aSLionel Sambuc
3226f4a2713aSLionel Sambuc return !S.RequireCompleteType(
3227f4a2713aSLionel Sambuc Loc, ElTy, diag::err_incomplete_type_used_in_type_trait_expr);
3228f4a2713aSLionel Sambuc }
3229f4a2713aSLionel Sambuc }
3230f4a2713aSLionel Sambuc
HasNoThrowOperator(const RecordType * RT,OverloadedOperatorKind Op,Sema & Self,SourceLocation KeyLoc,ASTContext & C,bool (CXXRecordDecl::* HasTrivial)()const,bool (CXXRecordDecl::* HasNonTrivial)()const,bool (CXXMethodDecl::* IsDesiredOp)()const)3231f4a2713aSLionel Sambuc static bool HasNoThrowOperator(const RecordType *RT, OverloadedOperatorKind Op,
3232f4a2713aSLionel Sambuc Sema &Self, SourceLocation KeyLoc, ASTContext &C,
3233f4a2713aSLionel Sambuc bool (CXXRecordDecl::*HasTrivial)() const,
3234f4a2713aSLionel Sambuc bool (CXXRecordDecl::*HasNonTrivial)() const,
3235f4a2713aSLionel Sambuc bool (CXXMethodDecl::*IsDesiredOp)() const)
3236f4a2713aSLionel Sambuc {
3237f4a2713aSLionel Sambuc CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3238f4a2713aSLionel Sambuc if ((RD->*HasTrivial)() && !(RD->*HasNonTrivial)())
3239f4a2713aSLionel Sambuc return true;
3240f4a2713aSLionel Sambuc
3241f4a2713aSLionel Sambuc DeclarationName Name = C.DeclarationNames.getCXXOperatorName(Op);
3242f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo(Name, KeyLoc);
3243f4a2713aSLionel Sambuc LookupResult Res(Self, NameInfo, Sema::LookupOrdinaryName);
3244f4a2713aSLionel Sambuc if (Self.LookupQualifiedName(Res, RD)) {
3245f4a2713aSLionel Sambuc bool FoundOperator = false;
3246f4a2713aSLionel Sambuc Res.suppressDiagnostics();
3247f4a2713aSLionel Sambuc for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end();
3248f4a2713aSLionel Sambuc Op != OpEnd; ++Op) {
3249f4a2713aSLionel Sambuc if (isa<FunctionTemplateDecl>(*Op))
3250f4a2713aSLionel Sambuc continue;
3251f4a2713aSLionel Sambuc
3252f4a2713aSLionel Sambuc CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op);
3253f4a2713aSLionel Sambuc if((Operator->*IsDesiredOp)()) {
3254f4a2713aSLionel Sambuc FoundOperator = true;
3255f4a2713aSLionel Sambuc const FunctionProtoType *CPT =
3256f4a2713aSLionel Sambuc Operator->getType()->getAs<FunctionProtoType>();
3257f4a2713aSLionel Sambuc CPT = Self.ResolveExceptionSpec(KeyLoc, CPT);
3258*0a6a1f1dSLionel Sambuc if (!CPT || !CPT->isNothrow(C))
3259f4a2713aSLionel Sambuc return false;
3260f4a2713aSLionel Sambuc }
3261f4a2713aSLionel Sambuc }
3262f4a2713aSLionel Sambuc return FoundOperator;
3263f4a2713aSLionel Sambuc }
3264f4a2713aSLionel Sambuc return false;
3265f4a2713aSLionel Sambuc }
3266f4a2713aSLionel Sambuc
EvaluateUnaryTypeTrait(Sema & Self,TypeTrait UTT,SourceLocation KeyLoc,QualType T)3267*0a6a1f1dSLionel Sambuc static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
3268f4a2713aSLionel Sambuc SourceLocation KeyLoc, QualType T) {
3269f4a2713aSLionel Sambuc assert(!T->isDependentType() && "Cannot evaluate traits of dependent type");
3270f4a2713aSLionel Sambuc
3271f4a2713aSLionel Sambuc ASTContext &C = Self.Context;
3272f4a2713aSLionel Sambuc switch(UTT) {
3273*0a6a1f1dSLionel Sambuc default: llvm_unreachable("not a UTT");
3274f4a2713aSLionel Sambuc // Type trait expressions corresponding to the primary type category
3275f4a2713aSLionel Sambuc // predicates in C++0x [meta.unary.cat].
3276f4a2713aSLionel Sambuc case UTT_IsVoid:
3277f4a2713aSLionel Sambuc return T->isVoidType();
3278f4a2713aSLionel Sambuc case UTT_IsIntegral:
3279f4a2713aSLionel Sambuc return T->isIntegralType(C);
3280f4a2713aSLionel Sambuc case UTT_IsFloatingPoint:
3281f4a2713aSLionel Sambuc return T->isFloatingType();
3282f4a2713aSLionel Sambuc case UTT_IsArray:
3283f4a2713aSLionel Sambuc return T->isArrayType();
3284f4a2713aSLionel Sambuc case UTT_IsPointer:
3285f4a2713aSLionel Sambuc return T->isPointerType();
3286f4a2713aSLionel Sambuc case UTT_IsLvalueReference:
3287f4a2713aSLionel Sambuc return T->isLValueReferenceType();
3288f4a2713aSLionel Sambuc case UTT_IsRvalueReference:
3289f4a2713aSLionel Sambuc return T->isRValueReferenceType();
3290f4a2713aSLionel Sambuc case UTT_IsMemberFunctionPointer:
3291f4a2713aSLionel Sambuc return T->isMemberFunctionPointerType();
3292f4a2713aSLionel Sambuc case UTT_IsMemberObjectPointer:
3293f4a2713aSLionel Sambuc return T->isMemberDataPointerType();
3294f4a2713aSLionel Sambuc case UTT_IsEnum:
3295f4a2713aSLionel Sambuc return T->isEnumeralType();
3296f4a2713aSLionel Sambuc case UTT_IsUnion:
3297f4a2713aSLionel Sambuc return T->isUnionType();
3298f4a2713aSLionel Sambuc case UTT_IsClass:
3299f4a2713aSLionel Sambuc return T->isClassType() || T->isStructureType() || T->isInterfaceType();
3300f4a2713aSLionel Sambuc case UTT_IsFunction:
3301f4a2713aSLionel Sambuc return T->isFunctionType();
3302f4a2713aSLionel Sambuc
3303f4a2713aSLionel Sambuc // Type trait expressions which correspond to the convenient composition
3304f4a2713aSLionel Sambuc // predicates in C++0x [meta.unary.comp].
3305f4a2713aSLionel Sambuc case UTT_IsReference:
3306f4a2713aSLionel Sambuc return T->isReferenceType();
3307f4a2713aSLionel Sambuc case UTT_IsArithmetic:
3308f4a2713aSLionel Sambuc return T->isArithmeticType() && !T->isEnumeralType();
3309f4a2713aSLionel Sambuc case UTT_IsFundamental:
3310f4a2713aSLionel Sambuc return T->isFundamentalType();
3311f4a2713aSLionel Sambuc case UTT_IsObject:
3312f4a2713aSLionel Sambuc return T->isObjectType();
3313f4a2713aSLionel Sambuc case UTT_IsScalar:
3314f4a2713aSLionel Sambuc // Note: semantic analysis depends on Objective-C lifetime types to be
3315f4a2713aSLionel Sambuc // considered scalar types. However, such types do not actually behave
3316f4a2713aSLionel Sambuc // like scalar types at run time (since they may require retain/release
3317f4a2713aSLionel Sambuc // operations), so we report them as non-scalar.
3318f4a2713aSLionel Sambuc if (T->isObjCLifetimeType()) {
3319f4a2713aSLionel Sambuc switch (T.getObjCLifetime()) {
3320f4a2713aSLionel Sambuc case Qualifiers::OCL_None:
3321f4a2713aSLionel Sambuc case Qualifiers::OCL_ExplicitNone:
3322f4a2713aSLionel Sambuc return true;
3323f4a2713aSLionel Sambuc
3324f4a2713aSLionel Sambuc case Qualifiers::OCL_Strong:
3325f4a2713aSLionel Sambuc case Qualifiers::OCL_Weak:
3326f4a2713aSLionel Sambuc case Qualifiers::OCL_Autoreleasing:
3327f4a2713aSLionel Sambuc return false;
3328f4a2713aSLionel Sambuc }
3329f4a2713aSLionel Sambuc }
3330f4a2713aSLionel Sambuc
3331f4a2713aSLionel Sambuc return T->isScalarType();
3332f4a2713aSLionel Sambuc case UTT_IsCompound:
3333f4a2713aSLionel Sambuc return T->isCompoundType();
3334f4a2713aSLionel Sambuc case UTT_IsMemberPointer:
3335f4a2713aSLionel Sambuc return T->isMemberPointerType();
3336f4a2713aSLionel Sambuc
3337f4a2713aSLionel Sambuc // Type trait expressions which correspond to the type property predicates
3338f4a2713aSLionel Sambuc // in C++0x [meta.unary.prop].
3339f4a2713aSLionel Sambuc case UTT_IsConst:
3340f4a2713aSLionel Sambuc return T.isConstQualified();
3341f4a2713aSLionel Sambuc case UTT_IsVolatile:
3342f4a2713aSLionel Sambuc return T.isVolatileQualified();
3343f4a2713aSLionel Sambuc case UTT_IsTrivial:
3344f4a2713aSLionel Sambuc return T.isTrivialType(Self.Context);
3345f4a2713aSLionel Sambuc case UTT_IsTriviallyCopyable:
3346f4a2713aSLionel Sambuc return T.isTriviallyCopyableType(Self.Context);
3347f4a2713aSLionel Sambuc case UTT_IsStandardLayout:
3348f4a2713aSLionel Sambuc return T->isStandardLayoutType();
3349f4a2713aSLionel Sambuc case UTT_IsPOD:
3350f4a2713aSLionel Sambuc return T.isPODType(Self.Context);
3351f4a2713aSLionel Sambuc case UTT_IsLiteral:
3352f4a2713aSLionel Sambuc return T->isLiteralType(Self.Context);
3353f4a2713aSLionel Sambuc case UTT_IsEmpty:
3354f4a2713aSLionel Sambuc if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3355f4a2713aSLionel Sambuc return !RD->isUnion() && RD->isEmpty();
3356f4a2713aSLionel Sambuc return false;
3357f4a2713aSLionel Sambuc case UTT_IsPolymorphic:
3358f4a2713aSLionel Sambuc if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3359f4a2713aSLionel Sambuc return RD->isPolymorphic();
3360f4a2713aSLionel Sambuc return false;
3361f4a2713aSLionel Sambuc case UTT_IsAbstract:
3362f4a2713aSLionel Sambuc if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3363f4a2713aSLionel Sambuc return RD->isAbstract();
3364f4a2713aSLionel Sambuc return false;
3365f4a2713aSLionel Sambuc case UTT_IsInterfaceClass:
3366f4a2713aSLionel Sambuc if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3367f4a2713aSLionel Sambuc return RD->isInterface();
3368f4a2713aSLionel Sambuc return false;
3369f4a2713aSLionel Sambuc case UTT_IsFinal:
3370f4a2713aSLionel Sambuc if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3371f4a2713aSLionel Sambuc return RD->hasAttr<FinalAttr>();
3372f4a2713aSLionel Sambuc return false;
3373f4a2713aSLionel Sambuc case UTT_IsSealed:
3374f4a2713aSLionel Sambuc if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3375f4a2713aSLionel Sambuc if (FinalAttr *FA = RD->getAttr<FinalAttr>())
3376f4a2713aSLionel Sambuc return FA->isSpelledAsSealed();
3377f4a2713aSLionel Sambuc return false;
3378f4a2713aSLionel Sambuc case UTT_IsSigned:
3379f4a2713aSLionel Sambuc return T->isSignedIntegerType();
3380f4a2713aSLionel Sambuc case UTT_IsUnsigned:
3381f4a2713aSLionel Sambuc return T->isUnsignedIntegerType();
3382f4a2713aSLionel Sambuc
3383f4a2713aSLionel Sambuc // Type trait expressions which query classes regarding their construction,
3384f4a2713aSLionel Sambuc // destruction, and copying. Rather than being based directly on the
3385f4a2713aSLionel Sambuc // related type predicates in the standard, they are specified by both
3386f4a2713aSLionel Sambuc // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those
3387f4a2713aSLionel Sambuc // specifications.
3388f4a2713aSLionel Sambuc //
3389f4a2713aSLionel Sambuc // 1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html
3390f4a2713aSLionel Sambuc // 2: http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index
3391f4a2713aSLionel Sambuc //
3392f4a2713aSLionel Sambuc // Note that these builtins do not behave as documented in g++: if a class
3393f4a2713aSLionel Sambuc // has both a trivial and a non-trivial special member of a particular kind,
3394f4a2713aSLionel Sambuc // they return false! For now, we emulate this behavior.
3395f4a2713aSLionel Sambuc // FIXME: This appears to be a g++ bug: more complex cases reveal that it
3396f4a2713aSLionel Sambuc // does not correctly compute triviality in the presence of multiple special
3397f4a2713aSLionel Sambuc // members of the same kind. Revisit this once the g++ bug is fixed.
3398f4a2713aSLionel Sambuc case UTT_HasTrivialDefaultConstructor:
3399f4a2713aSLionel Sambuc // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
3400f4a2713aSLionel Sambuc // If __is_pod (type) is true then the trait is true, else if type is
3401f4a2713aSLionel Sambuc // a cv class or union type (or array thereof) with a trivial default
3402f4a2713aSLionel Sambuc // constructor ([class.ctor]) then the trait is true, else it is false.
3403f4a2713aSLionel Sambuc if (T.isPODType(Self.Context))
3404f4a2713aSLionel Sambuc return true;
3405f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl())
3406f4a2713aSLionel Sambuc return RD->hasTrivialDefaultConstructor() &&
3407f4a2713aSLionel Sambuc !RD->hasNonTrivialDefaultConstructor();
3408f4a2713aSLionel Sambuc return false;
3409f4a2713aSLionel Sambuc case UTT_HasTrivialMoveConstructor:
3410f4a2713aSLionel Sambuc // This trait is implemented by MSVC 2012 and needed to parse the
3411f4a2713aSLionel Sambuc // standard library headers. Specifically this is used as the logic
3412f4a2713aSLionel Sambuc // behind std::is_trivially_move_constructible (20.9.4.3).
3413f4a2713aSLionel Sambuc if (T.isPODType(Self.Context))
3414f4a2713aSLionel Sambuc return true;
3415f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl())
3416f4a2713aSLionel Sambuc return RD->hasTrivialMoveConstructor() && !RD->hasNonTrivialMoveConstructor();
3417f4a2713aSLionel Sambuc return false;
3418f4a2713aSLionel Sambuc case UTT_HasTrivialCopy:
3419f4a2713aSLionel Sambuc // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
3420f4a2713aSLionel Sambuc // If __is_pod (type) is true or type is a reference type then
3421f4a2713aSLionel Sambuc // the trait is true, else if type is a cv class or union type
3422f4a2713aSLionel Sambuc // with a trivial copy constructor ([class.copy]) then the trait
3423f4a2713aSLionel Sambuc // is true, else it is false.
3424f4a2713aSLionel Sambuc if (T.isPODType(Self.Context) || T->isReferenceType())
3425f4a2713aSLionel Sambuc return true;
3426f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3427f4a2713aSLionel Sambuc return RD->hasTrivialCopyConstructor() &&
3428f4a2713aSLionel Sambuc !RD->hasNonTrivialCopyConstructor();
3429f4a2713aSLionel Sambuc return false;
3430f4a2713aSLionel Sambuc case UTT_HasTrivialMoveAssign:
3431f4a2713aSLionel Sambuc // This trait is implemented by MSVC 2012 and needed to parse the
3432f4a2713aSLionel Sambuc // standard library headers. Specifically it is used as the logic
3433f4a2713aSLionel Sambuc // behind std::is_trivially_move_assignable (20.9.4.3)
3434f4a2713aSLionel Sambuc if (T.isPODType(Self.Context))
3435f4a2713aSLionel Sambuc return true;
3436f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl())
3437f4a2713aSLionel Sambuc return RD->hasTrivialMoveAssignment() && !RD->hasNonTrivialMoveAssignment();
3438f4a2713aSLionel Sambuc return false;
3439f4a2713aSLionel Sambuc case UTT_HasTrivialAssign:
3440f4a2713aSLionel Sambuc // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
3441f4a2713aSLionel Sambuc // If type is const qualified or is a reference type then the
3442f4a2713aSLionel Sambuc // trait is false. Otherwise if __is_pod (type) is true then the
3443f4a2713aSLionel Sambuc // trait is true, else if type is a cv class or union type with
3444f4a2713aSLionel Sambuc // a trivial copy assignment ([class.copy]) then the trait is
3445f4a2713aSLionel Sambuc // true, else it is false.
3446f4a2713aSLionel Sambuc // Note: the const and reference restrictions are interesting,
3447f4a2713aSLionel Sambuc // given that const and reference members don't prevent a class
3448f4a2713aSLionel Sambuc // from having a trivial copy assignment operator (but do cause
3449f4a2713aSLionel Sambuc // errors if the copy assignment operator is actually used, q.v.
3450f4a2713aSLionel Sambuc // [class.copy]p12).
3451f4a2713aSLionel Sambuc
3452f4a2713aSLionel Sambuc if (T.isConstQualified())
3453f4a2713aSLionel Sambuc return false;
3454f4a2713aSLionel Sambuc if (T.isPODType(Self.Context))
3455f4a2713aSLionel Sambuc return true;
3456f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3457f4a2713aSLionel Sambuc return RD->hasTrivialCopyAssignment() &&
3458f4a2713aSLionel Sambuc !RD->hasNonTrivialCopyAssignment();
3459f4a2713aSLionel Sambuc return false;
3460*0a6a1f1dSLionel Sambuc case UTT_IsDestructible:
3461*0a6a1f1dSLionel Sambuc case UTT_IsNothrowDestructible:
3462*0a6a1f1dSLionel Sambuc // FIXME: Implement UTT_IsDestructible and UTT_IsNothrowDestructible.
3463*0a6a1f1dSLionel Sambuc // For now, let's fall through.
3464f4a2713aSLionel Sambuc case UTT_HasTrivialDestructor:
3465*0a6a1f1dSLionel Sambuc // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html
3466f4a2713aSLionel Sambuc // If __is_pod (type) is true or type is a reference type
3467f4a2713aSLionel Sambuc // then the trait is true, else if type is a cv class or union
3468f4a2713aSLionel Sambuc // type (or array thereof) with a trivial destructor
3469f4a2713aSLionel Sambuc // ([class.dtor]) then the trait is true, else it is
3470f4a2713aSLionel Sambuc // false.
3471f4a2713aSLionel Sambuc if (T.isPODType(Self.Context) || T->isReferenceType())
3472f4a2713aSLionel Sambuc return true;
3473f4a2713aSLionel Sambuc
3474f4a2713aSLionel Sambuc // Objective-C++ ARC: autorelease types don't require destruction.
3475f4a2713aSLionel Sambuc if (T->isObjCLifetimeType() &&
3476f4a2713aSLionel Sambuc T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing)
3477f4a2713aSLionel Sambuc return true;
3478f4a2713aSLionel Sambuc
3479f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl())
3480f4a2713aSLionel Sambuc return RD->hasTrivialDestructor();
3481f4a2713aSLionel Sambuc return false;
3482f4a2713aSLionel Sambuc // TODO: Propagate nothrowness for implicitly declared special members.
3483f4a2713aSLionel Sambuc case UTT_HasNothrowAssign:
3484f4a2713aSLionel Sambuc // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
3485f4a2713aSLionel Sambuc // If type is const qualified or is a reference type then the
3486f4a2713aSLionel Sambuc // trait is false. Otherwise if __has_trivial_assign (type)
3487f4a2713aSLionel Sambuc // is true then the trait is true, else if type is a cv class
3488f4a2713aSLionel Sambuc // or union type with copy assignment operators that are known
3489f4a2713aSLionel Sambuc // not to throw an exception then the trait is true, else it is
3490f4a2713aSLionel Sambuc // false.
3491f4a2713aSLionel Sambuc if (C.getBaseElementType(T).isConstQualified())
3492f4a2713aSLionel Sambuc return false;
3493f4a2713aSLionel Sambuc if (T->isReferenceType())
3494f4a2713aSLionel Sambuc return false;
3495f4a2713aSLionel Sambuc if (T.isPODType(Self.Context) || T->isObjCLifetimeType())
3496f4a2713aSLionel Sambuc return true;
3497f4a2713aSLionel Sambuc
3498f4a2713aSLionel Sambuc if (const RecordType *RT = T->getAs<RecordType>())
3499f4a2713aSLionel Sambuc return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C,
3500f4a2713aSLionel Sambuc &CXXRecordDecl::hasTrivialCopyAssignment,
3501f4a2713aSLionel Sambuc &CXXRecordDecl::hasNonTrivialCopyAssignment,
3502f4a2713aSLionel Sambuc &CXXMethodDecl::isCopyAssignmentOperator);
3503f4a2713aSLionel Sambuc return false;
3504f4a2713aSLionel Sambuc case UTT_HasNothrowMoveAssign:
3505f4a2713aSLionel Sambuc // This trait is implemented by MSVC 2012 and needed to parse the
3506f4a2713aSLionel Sambuc // standard library headers. Specifically this is used as the logic
3507f4a2713aSLionel Sambuc // behind std::is_nothrow_move_assignable (20.9.4.3).
3508f4a2713aSLionel Sambuc if (T.isPODType(Self.Context))
3509f4a2713aSLionel Sambuc return true;
3510f4a2713aSLionel Sambuc
3511f4a2713aSLionel Sambuc if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>())
3512f4a2713aSLionel Sambuc return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C,
3513f4a2713aSLionel Sambuc &CXXRecordDecl::hasTrivialMoveAssignment,
3514f4a2713aSLionel Sambuc &CXXRecordDecl::hasNonTrivialMoveAssignment,
3515f4a2713aSLionel Sambuc &CXXMethodDecl::isMoveAssignmentOperator);
3516f4a2713aSLionel Sambuc return false;
3517f4a2713aSLionel Sambuc case UTT_HasNothrowCopy:
3518f4a2713aSLionel Sambuc // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
3519f4a2713aSLionel Sambuc // If __has_trivial_copy (type) is true then the trait is true, else
3520f4a2713aSLionel Sambuc // if type is a cv class or union type with copy constructors that are
3521f4a2713aSLionel Sambuc // known not to throw an exception then the trait is true, else it is
3522f4a2713aSLionel Sambuc // false.
3523f4a2713aSLionel Sambuc if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType())
3524f4a2713aSLionel Sambuc return true;
3525f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) {
3526f4a2713aSLionel Sambuc if (RD->hasTrivialCopyConstructor() &&
3527f4a2713aSLionel Sambuc !RD->hasNonTrivialCopyConstructor())
3528f4a2713aSLionel Sambuc return true;
3529f4a2713aSLionel Sambuc
3530f4a2713aSLionel Sambuc bool FoundConstructor = false;
3531f4a2713aSLionel Sambuc unsigned FoundTQs;
3532f4a2713aSLionel Sambuc DeclContext::lookup_const_result R = Self.LookupConstructors(RD);
3533f4a2713aSLionel Sambuc for (DeclContext::lookup_const_iterator Con = R.begin(),
3534f4a2713aSLionel Sambuc ConEnd = R.end(); Con != ConEnd; ++Con) {
3535f4a2713aSLionel Sambuc // A template constructor is never a copy constructor.
3536f4a2713aSLionel Sambuc // FIXME: However, it may actually be selected at the actual overload
3537f4a2713aSLionel Sambuc // resolution point.
3538f4a2713aSLionel Sambuc if (isa<FunctionTemplateDecl>(*Con))
3539f4a2713aSLionel Sambuc continue;
3540f4a2713aSLionel Sambuc CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
3541f4a2713aSLionel Sambuc if (Constructor->isCopyConstructor(FoundTQs)) {
3542f4a2713aSLionel Sambuc FoundConstructor = true;
3543f4a2713aSLionel Sambuc const FunctionProtoType *CPT
3544f4a2713aSLionel Sambuc = Constructor->getType()->getAs<FunctionProtoType>();
3545f4a2713aSLionel Sambuc CPT = Self.ResolveExceptionSpec(KeyLoc, CPT);
3546f4a2713aSLionel Sambuc if (!CPT)
3547f4a2713aSLionel Sambuc return false;
3548*0a6a1f1dSLionel Sambuc // TODO: check whether evaluating default arguments can throw.
3549f4a2713aSLionel Sambuc // For now, we'll be conservative and assume that they can throw.
3550*0a6a1f1dSLionel Sambuc if (!CPT->isNothrow(Self.Context) || CPT->getNumParams() > 1)
3551f4a2713aSLionel Sambuc return false;
3552f4a2713aSLionel Sambuc }
3553f4a2713aSLionel Sambuc }
3554f4a2713aSLionel Sambuc
3555f4a2713aSLionel Sambuc return FoundConstructor;
3556f4a2713aSLionel Sambuc }
3557f4a2713aSLionel Sambuc return false;
3558f4a2713aSLionel Sambuc case UTT_HasNothrowConstructor:
3559*0a6a1f1dSLionel Sambuc // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html
3560f4a2713aSLionel Sambuc // If __has_trivial_constructor (type) is true then the trait is
3561f4a2713aSLionel Sambuc // true, else if type is a cv class or union type (or array
3562f4a2713aSLionel Sambuc // thereof) with a default constructor that is known not to
3563f4a2713aSLionel Sambuc // throw an exception then the trait is true, else it is false.
3564f4a2713aSLionel Sambuc if (T.isPODType(C) || T->isObjCLifetimeType())
3565f4a2713aSLionel Sambuc return true;
3566f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) {
3567f4a2713aSLionel Sambuc if (RD->hasTrivialDefaultConstructor() &&
3568f4a2713aSLionel Sambuc !RD->hasNonTrivialDefaultConstructor())
3569f4a2713aSLionel Sambuc return true;
3570f4a2713aSLionel Sambuc
3571*0a6a1f1dSLionel Sambuc bool FoundConstructor = false;
3572f4a2713aSLionel Sambuc DeclContext::lookup_const_result R = Self.LookupConstructors(RD);
3573f4a2713aSLionel Sambuc for (DeclContext::lookup_const_iterator Con = R.begin(),
3574f4a2713aSLionel Sambuc ConEnd = R.end(); Con != ConEnd; ++Con) {
3575f4a2713aSLionel Sambuc // FIXME: In C++0x, a constructor template can be a default constructor.
3576f4a2713aSLionel Sambuc if (isa<FunctionTemplateDecl>(*Con))
3577f4a2713aSLionel Sambuc continue;
3578f4a2713aSLionel Sambuc CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
3579f4a2713aSLionel Sambuc if (Constructor->isDefaultConstructor()) {
3580*0a6a1f1dSLionel Sambuc FoundConstructor = true;
3581f4a2713aSLionel Sambuc const FunctionProtoType *CPT
3582f4a2713aSLionel Sambuc = Constructor->getType()->getAs<FunctionProtoType>();
3583f4a2713aSLionel Sambuc CPT = Self.ResolveExceptionSpec(KeyLoc, CPT);
3584f4a2713aSLionel Sambuc if (!CPT)
3585f4a2713aSLionel Sambuc return false;
3586*0a6a1f1dSLionel Sambuc // FIXME: check whether evaluating default arguments can throw.
3587f4a2713aSLionel Sambuc // For now, we'll be conservative and assume that they can throw.
3588*0a6a1f1dSLionel Sambuc if (!CPT->isNothrow(Self.Context) || CPT->getNumParams() > 0)
3589*0a6a1f1dSLionel Sambuc return false;
3590f4a2713aSLionel Sambuc }
3591f4a2713aSLionel Sambuc }
3592*0a6a1f1dSLionel Sambuc return FoundConstructor;
3593f4a2713aSLionel Sambuc }
3594f4a2713aSLionel Sambuc return false;
3595f4a2713aSLionel Sambuc case UTT_HasVirtualDestructor:
3596f4a2713aSLionel Sambuc // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
3597f4a2713aSLionel Sambuc // If type is a class type with a virtual destructor ([class.dtor])
3598f4a2713aSLionel Sambuc // then the trait is true, else it is false.
3599f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
3600f4a2713aSLionel Sambuc if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD))
3601f4a2713aSLionel Sambuc return Destructor->isVirtual();
3602f4a2713aSLionel Sambuc return false;
3603f4a2713aSLionel Sambuc
3604f4a2713aSLionel Sambuc // These type trait expressions are modeled on the specifications for the
3605f4a2713aSLionel Sambuc // Embarcadero C++0x type trait functions:
3606f4a2713aSLionel Sambuc // http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index
3607f4a2713aSLionel Sambuc case UTT_IsCompleteType:
3608f4a2713aSLionel Sambuc // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_):
3609f4a2713aSLionel Sambuc // Returns True if and only if T is a complete type at the point of the
3610f4a2713aSLionel Sambuc // function call.
3611f4a2713aSLionel Sambuc return !T->isIncompleteType();
3612f4a2713aSLionel Sambuc }
3613f4a2713aSLionel Sambuc }
3614f4a2713aSLionel Sambuc
3615f4a2713aSLionel Sambuc /// \brief Determine whether T has a non-trivial Objective-C lifetime in
3616f4a2713aSLionel Sambuc /// ARC mode.
hasNontrivialObjCLifetime(QualType T)3617f4a2713aSLionel Sambuc static bool hasNontrivialObjCLifetime(QualType T) {
3618f4a2713aSLionel Sambuc switch (T.getObjCLifetime()) {
3619f4a2713aSLionel Sambuc case Qualifiers::OCL_ExplicitNone:
3620f4a2713aSLionel Sambuc return false;
3621f4a2713aSLionel Sambuc
3622f4a2713aSLionel Sambuc case Qualifiers::OCL_Strong:
3623f4a2713aSLionel Sambuc case Qualifiers::OCL_Weak:
3624f4a2713aSLionel Sambuc case Qualifiers::OCL_Autoreleasing:
3625f4a2713aSLionel Sambuc return true;
3626f4a2713aSLionel Sambuc
3627f4a2713aSLionel Sambuc case Qualifiers::OCL_None:
3628f4a2713aSLionel Sambuc return T->isObjCLifetimeType();
3629f4a2713aSLionel Sambuc }
3630f4a2713aSLionel Sambuc
3631f4a2713aSLionel Sambuc llvm_unreachable("Unknown ObjC lifetime qualifier");
3632f4a2713aSLionel Sambuc }
3633f4a2713aSLionel Sambuc
3634*0a6a1f1dSLionel Sambuc static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT,
3635*0a6a1f1dSLionel Sambuc QualType RhsT, SourceLocation KeyLoc);
3636*0a6a1f1dSLionel Sambuc
evaluateTypeTrait(Sema & S,TypeTrait Kind,SourceLocation KWLoc,ArrayRef<TypeSourceInfo * > Args,SourceLocation RParenLoc)3637f4a2713aSLionel Sambuc static bool evaluateTypeTrait(Sema &S, TypeTrait Kind, SourceLocation KWLoc,
3638f4a2713aSLionel Sambuc ArrayRef<TypeSourceInfo *> Args,
3639f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
3640*0a6a1f1dSLionel Sambuc if (Kind <= UTT_Last)
3641*0a6a1f1dSLionel Sambuc return EvaluateUnaryTypeTrait(S, Kind, KWLoc, Args[0]->getType());
3642*0a6a1f1dSLionel Sambuc
3643*0a6a1f1dSLionel Sambuc if (Kind <= BTT_Last)
3644*0a6a1f1dSLionel Sambuc return EvaluateBinaryTypeTrait(S, Kind, Args[0]->getType(),
3645*0a6a1f1dSLionel Sambuc Args[1]->getType(), RParenLoc);
3646*0a6a1f1dSLionel Sambuc
3647f4a2713aSLionel Sambuc switch (Kind) {
3648*0a6a1f1dSLionel Sambuc case clang::TT_IsConstructible:
3649*0a6a1f1dSLionel Sambuc case clang::TT_IsNothrowConstructible:
3650f4a2713aSLionel Sambuc case clang::TT_IsTriviallyConstructible: {
3651f4a2713aSLionel Sambuc // C++11 [meta.unary.prop]:
3652f4a2713aSLionel Sambuc // is_trivially_constructible is defined as:
3653f4a2713aSLionel Sambuc //
3654f4a2713aSLionel Sambuc // is_constructible<T, Args...>::value is true and the variable
3655f4a2713aSLionel Sambuc // definition for is_constructible, as defined below, is known to call
3656f4a2713aSLionel Sambuc // no operation that is not trivial.
3657f4a2713aSLionel Sambuc //
3658f4a2713aSLionel Sambuc // The predicate condition for a template specialization
3659f4a2713aSLionel Sambuc // is_constructible<T, Args...> shall be satisfied if and only if the
3660f4a2713aSLionel Sambuc // following variable definition would be well-formed for some invented
3661f4a2713aSLionel Sambuc // variable t:
3662f4a2713aSLionel Sambuc //
3663f4a2713aSLionel Sambuc // T t(create<Args>()...);
3664*0a6a1f1dSLionel Sambuc assert(!Args.empty());
3665f4a2713aSLionel Sambuc
3666f4a2713aSLionel Sambuc // Precondition: T and all types in the parameter pack Args shall be
3667f4a2713aSLionel Sambuc // complete types, (possibly cv-qualified) void, or arrays of
3668f4a2713aSLionel Sambuc // unknown bound.
3669f4a2713aSLionel Sambuc for (unsigned I = 0, N = Args.size(); I != N; ++I) {
3670f4a2713aSLionel Sambuc QualType ArgTy = Args[I]->getType();
3671f4a2713aSLionel Sambuc if (ArgTy->isVoidType() || ArgTy->isIncompleteArrayType())
3672f4a2713aSLionel Sambuc continue;
3673f4a2713aSLionel Sambuc
3674f4a2713aSLionel Sambuc if (S.RequireCompleteType(KWLoc, ArgTy,
3675f4a2713aSLionel Sambuc diag::err_incomplete_type_used_in_type_trait_expr))
3676f4a2713aSLionel Sambuc return false;
3677f4a2713aSLionel Sambuc }
3678f4a2713aSLionel Sambuc
3679f4a2713aSLionel Sambuc // Make sure the first argument is a complete type.
3680f4a2713aSLionel Sambuc if (Args[0]->getType()->isIncompleteType())
3681f4a2713aSLionel Sambuc return false;
3682f4a2713aSLionel Sambuc
3683*0a6a1f1dSLionel Sambuc // Make sure the first argument is not an abstract type.
3684*0a6a1f1dSLionel Sambuc CXXRecordDecl *RD = Args[0]->getType()->getAsCXXRecordDecl();
3685*0a6a1f1dSLionel Sambuc if (RD && RD->isAbstract())
3686*0a6a1f1dSLionel Sambuc return false;
3687*0a6a1f1dSLionel Sambuc
3688f4a2713aSLionel Sambuc SmallVector<OpaqueValueExpr, 2> OpaqueArgExprs;
3689f4a2713aSLionel Sambuc SmallVector<Expr *, 2> ArgExprs;
3690f4a2713aSLionel Sambuc ArgExprs.reserve(Args.size() - 1);
3691f4a2713aSLionel Sambuc for (unsigned I = 1, N = Args.size(); I != N; ++I) {
3692f4a2713aSLionel Sambuc QualType T = Args[I]->getType();
3693f4a2713aSLionel Sambuc if (T->isObjectType() || T->isFunctionType())
3694f4a2713aSLionel Sambuc T = S.Context.getRValueReferenceType(T);
3695f4a2713aSLionel Sambuc OpaqueArgExprs.push_back(
3696f4a2713aSLionel Sambuc OpaqueValueExpr(Args[I]->getTypeLoc().getLocStart(),
3697f4a2713aSLionel Sambuc T.getNonLValueExprType(S.Context),
3698f4a2713aSLionel Sambuc Expr::getValueKindForType(T)));
3699f4a2713aSLionel Sambuc }
3700*0a6a1f1dSLionel Sambuc for (Expr &E : OpaqueArgExprs)
3701*0a6a1f1dSLionel Sambuc ArgExprs.push_back(&E);
3702f4a2713aSLionel Sambuc
3703f4a2713aSLionel Sambuc // Perform the initialization in an unevaluated context within a SFINAE
3704f4a2713aSLionel Sambuc // trap at translation unit scope.
3705f4a2713aSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated);
3706f4a2713aSLionel Sambuc Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/true);
3707f4a2713aSLionel Sambuc Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl());
3708f4a2713aSLionel Sambuc InitializedEntity To(InitializedEntity::InitializeTemporary(Args[0]));
3709f4a2713aSLionel Sambuc InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc,
3710f4a2713aSLionel Sambuc RParenLoc));
3711f4a2713aSLionel Sambuc InitializationSequence Init(S, To, InitKind, ArgExprs);
3712f4a2713aSLionel Sambuc if (Init.Failed())
3713f4a2713aSLionel Sambuc return false;
3714f4a2713aSLionel Sambuc
3715f4a2713aSLionel Sambuc ExprResult Result = Init.Perform(S, To, InitKind, ArgExprs);
3716f4a2713aSLionel Sambuc if (Result.isInvalid() || SFINAE.hasErrorOccurred())
3717f4a2713aSLionel Sambuc return false;
3718f4a2713aSLionel Sambuc
3719*0a6a1f1dSLionel Sambuc if (Kind == clang::TT_IsConstructible)
3720*0a6a1f1dSLionel Sambuc return true;
3721*0a6a1f1dSLionel Sambuc
3722*0a6a1f1dSLionel Sambuc if (Kind == clang::TT_IsNothrowConstructible)
3723*0a6a1f1dSLionel Sambuc return S.canThrow(Result.get()) == CT_Cannot;
3724*0a6a1f1dSLionel Sambuc
3725*0a6a1f1dSLionel Sambuc if (Kind == clang::TT_IsTriviallyConstructible) {
3726f4a2713aSLionel Sambuc // Under Objective-C ARC, if the destination has non-trivial Objective-C
3727f4a2713aSLionel Sambuc // lifetime, this is a non-trivial construction.
3728f4a2713aSLionel Sambuc if (S.getLangOpts().ObjCAutoRefCount &&
3729f4a2713aSLionel Sambuc hasNontrivialObjCLifetime(Args[0]->getType().getNonReferenceType()))
3730f4a2713aSLionel Sambuc return false;
3731f4a2713aSLionel Sambuc
3732f4a2713aSLionel Sambuc // The initialization succeeded; now make sure there are no non-trivial
3733f4a2713aSLionel Sambuc // calls.
3734f4a2713aSLionel Sambuc return !Result.get()->hasNonTrivialCall(S.Context);
3735f4a2713aSLionel Sambuc }
3736*0a6a1f1dSLionel Sambuc
3737*0a6a1f1dSLionel Sambuc llvm_unreachable("unhandled type trait");
3738*0a6a1f1dSLionel Sambuc return false;
3739*0a6a1f1dSLionel Sambuc }
3740*0a6a1f1dSLionel Sambuc default: llvm_unreachable("not a TT");
3741f4a2713aSLionel Sambuc }
3742f4a2713aSLionel Sambuc
3743f4a2713aSLionel Sambuc return false;
3744f4a2713aSLionel Sambuc }
3745f4a2713aSLionel Sambuc
BuildTypeTrait(TypeTrait Kind,SourceLocation KWLoc,ArrayRef<TypeSourceInfo * > Args,SourceLocation RParenLoc)3746f4a2713aSLionel Sambuc ExprResult Sema::BuildTypeTrait(TypeTrait Kind, SourceLocation KWLoc,
3747f4a2713aSLionel Sambuc ArrayRef<TypeSourceInfo *> Args,
3748f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
3749*0a6a1f1dSLionel Sambuc QualType ResultType = Context.getLogicalOperationType();
3750*0a6a1f1dSLionel Sambuc
3751*0a6a1f1dSLionel Sambuc if (Kind <= UTT_Last && !CheckUnaryTypeTraitTypeCompleteness(
3752*0a6a1f1dSLionel Sambuc *this, Kind, KWLoc, Args[0]->getType()))
3753*0a6a1f1dSLionel Sambuc return ExprError();
3754*0a6a1f1dSLionel Sambuc
3755f4a2713aSLionel Sambuc bool Dependent = false;
3756f4a2713aSLionel Sambuc for (unsigned I = 0, N = Args.size(); I != N; ++I) {
3757f4a2713aSLionel Sambuc if (Args[I]->getType()->isDependentType()) {
3758f4a2713aSLionel Sambuc Dependent = true;
3759f4a2713aSLionel Sambuc break;
3760f4a2713aSLionel Sambuc }
3761f4a2713aSLionel Sambuc }
3762f4a2713aSLionel Sambuc
3763*0a6a1f1dSLionel Sambuc bool Result = false;
3764f4a2713aSLionel Sambuc if (!Dependent)
3765*0a6a1f1dSLionel Sambuc Result = evaluateTypeTrait(*this, Kind, KWLoc, Args, RParenLoc);
3766f4a2713aSLionel Sambuc
3767*0a6a1f1dSLionel Sambuc return TypeTraitExpr::Create(Context, ResultType, KWLoc, Kind, Args,
3768*0a6a1f1dSLionel Sambuc RParenLoc, Result);
3769f4a2713aSLionel Sambuc }
3770f4a2713aSLionel Sambuc
ActOnTypeTrait(TypeTrait Kind,SourceLocation KWLoc,ArrayRef<ParsedType> Args,SourceLocation RParenLoc)3771f4a2713aSLionel Sambuc ExprResult Sema::ActOnTypeTrait(TypeTrait Kind, SourceLocation KWLoc,
3772f4a2713aSLionel Sambuc ArrayRef<ParsedType> Args,
3773f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
3774f4a2713aSLionel Sambuc SmallVector<TypeSourceInfo *, 4> ConvertedArgs;
3775f4a2713aSLionel Sambuc ConvertedArgs.reserve(Args.size());
3776f4a2713aSLionel Sambuc
3777f4a2713aSLionel Sambuc for (unsigned I = 0, N = Args.size(); I != N; ++I) {
3778f4a2713aSLionel Sambuc TypeSourceInfo *TInfo;
3779f4a2713aSLionel Sambuc QualType T = GetTypeFromParser(Args[I], &TInfo);
3780f4a2713aSLionel Sambuc if (!TInfo)
3781f4a2713aSLionel Sambuc TInfo = Context.getTrivialTypeSourceInfo(T, KWLoc);
3782f4a2713aSLionel Sambuc
3783f4a2713aSLionel Sambuc ConvertedArgs.push_back(TInfo);
3784f4a2713aSLionel Sambuc }
3785f4a2713aSLionel Sambuc
3786f4a2713aSLionel Sambuc return BuildTypeTrait(Kind, KWLoc, ConvertedArgs, RParenLoc);
3787f4a2713aSLionel Sambuc }
3788f4a2713aSLionel Sambuc
EvaluateBinaryTypeTrait(Sema & Self,TypeTrait BTT,QualType LhsT,QualType RhsT,SourceLocation KeyLoc)3789*0a6a1f1dSLionel Sambuc static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT,
3790*0a6a1f1dSLionel Sambuc QualType RhsT, SourceLocation KeyLoc) {
3791f4a2713aSLionel Sambuc assert(!LhsT->isDependentType() && !RhsT->isDependentType() &&
3792f4a2713aSLionel Sambuc "Cannot evaluate traits of dependent types");
3793f4a2713aSLionel Sambuc
3794f4a2713aSLionel Sambuc switch(BTT) {
3795f4a2713aSLionel Sambuc case BTT_IsBaseOf: {
3796f4a2713aSLionel Sambuc // C++0x [meta.rel]p2
3797f4a2713aSLionel Sambuc // Base is a base class of Derived without regard to cv-qualifiers or
3798f4a2713aSLionel Sambuc // Base and Derived are not unions and name the same class type without
3799f4a2713aSLionel Sambuc // regard to cv-qualifiers.
3800f4a2713aSLionel Sambuc
3801f4a2713aSLionel Sambuc const RecordType *lhsRecord = LhsT->getAs<RecordType>();
3802f4a2713aSLionel Sambuc if (!lhsRecord) return false;
3803f4a2713aSLionel Sambuc
3804f4a2713aSLionel Sambuc const RecordType *rhsRecord = RhsT->getAs<RecordType>();
3805f4a2713aSLionel Sambuc if (!rhsRecord) return false;
3806f4a2713aSLionel Sambuc
3807f4a2713aSLionel Sambuc assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT)
3808f4a2713aSLionel Sambuc == (lhsRecord == rhsRecord));
3809f4a2713aSLionel Sambuc
3810f4a2713aSLionel Sambuc if (lhsRecord == rhsRecord)
3811f4a2713aSLionel Sambuc return !lhsRecord->getDecl()->isUnion();
3812f4a2713aSLionel Sambuc
3813f4a2713aSLionel Sambuc // C++0x [meta.rel]p2:
3814f4a2713aSLionel Sambuc // If Base and Derived are class types and are different types
3815f4a2713aSLionel Sambuc // (ignoring possible cv-qualifiers) then Derived shall be a
3816f4a2713aSLionel Sambuc // complete type.
3817f4a2713aSLionel Sambuc if (Self.RequireCompleteType(KeyLoc, RhsT,
3818f4a2713aSLionel Sambuc diag::err_incomplete_type_used_in_type_trait_expr))
3819f4a2713aSLionel Sambuc return false;
3820f4a2713aSLionel Sambuc
3821f4a2713aSLionel Sambuc return cast<CXXRecordDecl>(rhsRecord->getDecl())
3822f4a2713aSLionel Sambuc ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl()));
3823f4a2713aSLionel Sambuc }
3824f4a2713aSLionel Sambuc case BTT_IsSame:
3825f4a2713aSLionel Sambuc return Self.Context.hasSameType(LhsT, RhsT);
3826f4a2713aSLionel Sambuc case BTT_TypeCompatible:
3827f4a2713aSLionel Sambuc return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(),
3828f4a2713aSLionel Sambuc RhsT.getUnqualifiedType());
3829f4a2713aSLionel Sambuc case BTT_IsConvertible:
3830f4a2713aSLionel Sambuc case BTT_IsConvertibleTo: {
3831f4a2713aSLionel Sambuc // C++0x [meta.rel]p4:
3832f4a2713aSLionel Sambuc // Given the following function prototype:
3833f4a2713aSLionel Sambuc //
3834f4a2713aSLionel Sambuc // template <class T>
3835f4a2713aSLionel Sambuc // typename add_rvalue_reference<T>::type create();
3836f4a2713aSLionel Sambuc //
3837f4a2713aSLionel Sambuc // the predicate condition for a template specialization
3838f4a2713aSLionel Sambuc // is_convertible<From, To> shall be satisfied if and only if
3839f4a2713aSLionel Sambuc // the return expression in the following code would be
3840f4a2713aSLionel Sambuc // well-formed, including any implicit conversions to the return
3841f4a2713aSLionel Sambuc // type of the function:
3842f4a2713aSLionel Sambuc //
3843f4a2713aSLionel Sambuc // To test() {
3844f4a2713aSLionel Sambuc // return create<From>();
3845f4a2713aSLionel Sambuc // }
3846f4a2713aSLionel Sambuc //
3847f4a2713aSLionel Sambuc // Access checking is performed as if in a context unrelated to To and
3848f4a2713aSLionel Sambuc // From. Only the validity of the immediate context of the expression
3849f4a2713aSLionel Sambuc // of the return-statement (including conversions to the return type)
3850f4a2713aSLionel Sambuc // is considered.
3851f4a2713aSLionel Sambuc //
3852f4a2713aSLionel Sambuc // We model the initialization as a copy-initialization of a temporary
3853f4a2713aSLionel Sambuc // of the appropriate type, which for this expression is identical to the
3854f4a2713aSLionel Sambuc // return statement (since NRVO doesn't apply).
3855f4a2713aSLionel Sambuc
3856f4a2713aSLionel Sambuc // Functions aren't allowed to return function or array types.
3857f4a2713aSLionel Sambuc if (RhsT->isFunctionType() || RhsT->isArrayType())
3858f4a2713aSLionel Sambuc return false;
3859f4a2713aSLionel Sambuc
3860f4a2713aSLionel Sambuc // A return statement in a void function must have void type.
3861f4a2713aSLionel Sambuc if (RhsT->isVoidType())
3862f4a2713aSLionel Sambuc return LhsT->isVoidType();
3863f4a2713aSLionel Sambuc
3864f4a2713aSLionel Sambuc // A function definition requires a complete, non-abstract return type.
3865f4a2713aSLionel Sambuc if (Self.RequireCompleteType(KeyLoc, RhsT, 0) ||
3866f4a2713aSLionel Sambuc Self.RequireNonAbstractType(KeyLoc, RhsT, 0))
3867f4a2713aSLionel Sambuc return false;
3868f4a2713aSLionel Sambuc
3869f4a2713aSLionel Sambuc // Compute the result of add_rvalue_reference.
3870f4a2713aSLionel Sambuc if (LhsT->isObjectType() || LhsT->isFunctionType())
3871f4a2713aSLionel Sambuc LhsT = Self.Context.getRValueReferenceType(LhsT);
3872f4a2713aSLionel Sambuc
3873f4a2713aSLionel Sambuc // Build a fake source and destination for initialization.
3874f4a2713aSLionel Sambuc InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT));
3875f4a2713aSLionel Sambuc OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context),
3876f4a2713aSLionel Sambuc Expr::getValueKindForType(LhsT));
3877f4a2713aSLionel Sambuc Expr *FromPtr = &From;
3878f4a2713aSLionel Sambuc InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc,
3879f4a2713aSLionel Sambuc SourceLocation()));
3880f4a2713aSLionel Sambuc
3881f4a2713aSLionel Sambuc // Perform the initialization in an unevaluated context within a SFINAE
3882f4a2713aSLionel Sambuc // trap at translation unit scope.
3883f4a2713aSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(Self, Sema::Unevaluated);
3884f4a2713aSLionel Sambuc Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true);
3885f4a2713aSLionel Sambuc Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl());
3886f4a2713aSLionel Sambuc InitializationSequence Init(Self, To, Kind, FromPtr);
3887f4a2713aSLionel Sambuc if (Init.Failed())
3888f4a2713aSLionel Sambuc return false;
3889f4a2713aSLionel Sambuc
3890f4a2713aSLionel Sambuc ExprResult Result = Init.Perform(Self, To, Kind, FromPtr);
3891f4a2713aSLionel Sambuc return !Result.isInvalid() && !SFINAE.hasErrorOccurred();
3892f4a2713aSLionel Sambuc }
3893f4a2713aSLionel Sambuc
3894*0a6a1f1dSLionel Sambuc case BTT_IsNothrowAssignable:
3895f4a2713aSLionel Sambuc case BTT_IsTriviallyAssignable: {
3896f4a2713aSLionel Sambuc // C++11 [meta.unary.prop]p3:
3897f4a2713aSLionel Sambuc // is_trivially_assignable is defined as:
3898f4a2713aSLionel Sambuc // is_assignable<T, U>::value is true and the assignment, as defined by
3899f4a2713aSLionel Sambuc // is_assignable, is known to call no operation that is not trivial
3900f4a2713aSLionel Sambuc //
3901f4a2713aSLionel Sambuc // is_assignable is defined as:
3902f4a2713aSLionel Sambuc // The expression declval<T>() = declval<U>() is well-formed when
3903f4a2713aSLionel Sambuc // treated as an unevaluated operand (Clause 5).
3904f4a2713aSLionel Sambuc //
3905f4a2713aSLionel Sambuc // For both, T and U shall be complete types, (possibly cv-qualified)
3906f4a2713aSLionel Sambuc // void, or arrays of unknown bound.
3907f4a2713aSLionel Sambuc if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType() &&
3908f4a2713aSLionel Sambuc Self.RequireCompleteType(KeyLoc, LhsT,
3909f4a2713aSLionel Sambuc diag::err_incomplete_type_used_in_type_trait_expr))
3910f4a2713aSLionel Sambuc return false;
3911f4a2713aSLionel Sambuc if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType() &&
3912f4a2713aSLionel Sambuc Self.RequireCompleteType(KeyLoc, RhsT,
3913f4a2713aSLionel Sambuc diag::err_incomplete_type_used_in_type_trait_expr))
3914f4a2713aSLionel Sambuc return false;
3915f4a2713aSLionel Sambuc
3916f4a2713aSLionel Sambuc // cv void is never assignable.
3917f4a2713aSLionel Sambuc if (LhsT->isVoidType() || RhsT->isVoidType())
3918f4a2713aSLionel Sambuc return false;
3919f4a2713aSLionel Sambuc
3920f4a2713aSLionel Sambuc // Build expressions that emulate the effect of declval<T>() and
3921f4a2713aSLionel Sambuc // declval<U>().
3922f4a2713aSLionel Sambuc if (LhsT->isObjectType() || LhsT->isFunctionType())
3923f4a2713aSLionel Sambuc LhsT = Self.Context.getRValueReferenceType(LhsT);
3924f4a2713aSLionel Sambuc if (RhsT->isObjectType() || RhsT->isFunctionType())
3925f4a2713aSLionel Sambuc RhsT = Self.Context.getRValueReferenceType(RhsT);
3926f4a2713aSLionel Sambuc OpaqueValueExpr Lhs(KeyLoc, LhsT.getNonLValueExprType(Self.Context),
3927f4a2713aSLionel Sambuc Expr::getValueKindForType(LhsT));
3928f4a2713aSLionel Sambuc OpaqueValueExpr Rhs(KeyLoc, RhsT.getNonLValueExprType(Self.Context),
3929f4a2713aSLionel Sambuc Expr::getValueKindForType(RhsT));
3930f4a2713aSLionel Sambuc
3931f4a2713aSLionel Sambuc // Attempt the assignment in an unevaluated context within a SFINAE
3932f4a2713aSLionel Sambuc // trap at translation unit scope.
3933f4a2713aSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(Self, Sema::Unevaluated);
3934f4a2713aSLionel Sambuc Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true);
3935f4a2713aSLionel Sambuc Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl());
3936*0a6a1f1dSLionel Sambuc ExprResult Result = Self.BuildBinOp(/*S=*/nullptr, KeyLoc, BO_Assign, &Lhs,
3937*0a6a1f1dSLionel Sambuc &Rhs);
3938f4a2713aSLionel Sambuc if (Result.isInvalid() || SFINAE.hasErrorOccurred())
3939f4a2713aSLionel Sambuc return false;
3940f4a2713aSLionel Sambuc
3941*0a6a1f1dSLionel Sambuc if (BTT == BTT_IsNothrowAssignable)
3942*0a6a1f1dSLionel Sambuc return Self.canThrow(Result.get()) == CT_Cannot;
3943*0a6a1f1dSLionel Sambuc
3944*0a6a1f1dSLionel Sambuc if (BTT == BTT_IsTriviallyAssignable) {
3945f4a2713aSLionel Sambuc // Under Objective-C ARC, if the destination has non-trivial Objective-C
3946f4a2713aSLionel Sambuc // lifetime, this is a non-trivial assignment.
3947f4a2713aSLionel Sambuc if (Self.getLangOpts().ObjCAutoRefCount &&
3948f4a2713aSLionel Sambuc hasNontrivialObjCLifetime(LhsT.getNonReferenceType()))
3949f4a2713aSLionel Sambuc return false;
3950f4a2713aSLionel Sambuc
3951f4a2713aSLionel Sambuc return !Result.get()->hasNonTrivialCall(Self.Context);
3952f4a2713aSLionel Sambuc }
3953*0a6a1f1dSLionel Sambuc
3954*0a6a1f1dSLionel Sambuc llvm_unreachable("unhandled type trait");
3955*0a6a1f1dSLionel Sambuc return false;
3956*0a6a1f1dSLionel Sambuc }
3957*0a6a1f1dSLionel Sambuc default: llvm_unreachable("not a BTT");
3958f4a2713aSLionel Sambuc }
3959f4a2713aSLionel Sambuc llvm_unreachable("Unknown type trait or not implemented");
3960f4a2713aSLionel Sambuc }
3961f4a2713aSLionel Sambuc
ActOnArrayTypeTrait(ArrayTypeTrait ATT,SourceLocation KWLoc,ParsedType Ty,Expr * DimExpr,SourceLocation RParen)3962f4a2713aSLionel Sambuc ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT,
3963f4a2713aSLionel Sambuc SourceLocation KWLoc,
3964f4a2713aSLionel Sambuc ParsedType Ty,
3965f4a2713aSLionel Sambuc Expr* DimExpr,
3966f4a2713aSLionel Sambuc SourceLocation RParen) {
3967f4a2713aSLionel Sambuc TypeSourceInfo *TSInfo;
3968f4a2713aSLionel Sambuc QualType T = GetTypeFromParser(Ty, &TSInfo);
3969f4a2713aSLionel Sambuc if (!TSInfo)
3970f4a2713aSLionel Sambuc TSInfo = Context.getTrivialTypeSourceInfo(T);
3971f4a2713aSLionel Sambuc
3972f4a2713aSLionel Sambuc return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen);
3973f4a2713aSLionel Sambuc }
3974f4a2713aSLionel Sambuc
EvaluateArrayTypeTrait(Sema & Self,ArrayTypeTrait ATT,QualType T,Expr * DimExpr,SourceLocation KeyLoc)3975f4a2713aSLionel Sambuc static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT,
3976f4a2713aSLionel Sambuc QualType T, Expr *DimExpr,
3977f4a2713aSLionel Sambuc SourceLocation KeyLoc) {
3978f4a2713aSLionel Sambuc assert(!T->isDependentType() && "Cannot evaluate traits of dependent type");
3979f4a2713aSLionel Sambuc
3980f4a2713aSLionel Sambuc switch(ATT) {
3981f4a2713aSLionel Sambuc case ATT_ArrayRank:
3982f4a2713aSLionel Sambuc if (T->isArrayType()) {
3983f4a2713aSLionel Sambuc unsigned Dim = 0;
3984f4a2713aSLionel Sambuc while (const ArrayType *AT = Self.Context.getAsArrayType(T)) {
3985f4a2713aSLionel Sambuc ++Dim;
3986f4a2713aSLionel Sambuc T = AT->getElementType();
3987f4a2713aSLionel Sambuc }
3988f4a2713aSLionel Sambuc return Dim;
3989f4a2713aSLionel Sambuc }
3990f4a2713aSLionel Sambuc return 0;
3991f4a2713aSLionel Sambuc
3992f4a2713aSLionel Sambuc case ATT_ArrayExtent: {
3993f4a2713aSLionel Sambuc llvm::APSInt Value;
3994f4a2713aSLionel Sambuc uint64_t Dim;
3995f4a2713aSLionel Sambuc if (Self.VerifyIntegerConstantExpression(DimExpr, &Value,
3996f4a2713aSLionel Sambuc diag::err_dimension_expr_not_constant_integer,
3997f4a2713aSLionel Sambuc false).isInvalid())
3998f4a2713aSLionel Sambuc return 0;
3999f4a2713aSLionel Sambuc if (Value.isSigned() && Value.isNegative()) {
4000f4a2713aSLionel Sambuc Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer)
4001f4a2713aSLionel Sambuc << DimExpr->getSourceRange();
4002f4a2713aSLionel Sambuc return 0;
4003f4a2713aSLionel Sambuc }
4004f4a2713aSLionel Sambuc Dim = Value.getLimitedValue();
4005f4a2713aSLionel Sambuc
4006f4a2713aSLionel Sambuc if (T->isArrayType()) {
4007f4a2713aSLionel Sambuc unsigned D = 0;
4008f4a2713aSLionel Sambuc bool Matched = false;
4009f4a2713aSLionel Sambuc while (const ArrayType *AT = Self.Context.getAsArrayType(T)) {
4010f4a2713aSLionel Sambuc if (Dim == D) {
4011f4a2713aSLionel Sambuc Matched = true;
4012f4a2713aSLionel Sambuc break;
4013f4a2713aSLionel Sambuc }
4014f4a2713aSLionel Sambuc ++D;
4015f4a2713aSLionel Sambuc T = AT->getElementType();
4016f4a2713aSLionel Sambuc }
4017f4a2713aSLionel Sambuc
4018f4a2713aSLionel Sambuc if (Matched && T->isArrayType()) {
4019f4a2713aSLionel Sambuc if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T))
4020f4a2713aSLionel Sambuc return CAT->getSize().getLimitedValue();
4021f4a2713aSLionel Sambuc }
4022f4a2713aSLionel Sambuc }
4023f4a2713aSLionel Sambuc return 0;
4024f4a2713aSLionel Sambuc }
4025f4a2713aSLionel Sambuc }
4026f4a2713aSLionel Sambuc llvm_unreachable("Unknown type trait or not implemented");
4027f4a2713aSLionel Sambuc }
4028f4a2713aSLionel Sambuc
BuildArrayTypeTrait(ArrayTypeTrait ATT,SourceLocation KWLoc,TypeSourceInfo * TSInfo,Expr * DimExpr,SourceLocation RParen)4029f4a2713aSLionel Sambuc ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT,
4030f4a2713aSLionel Sambuc SourceLocation KWLoc,
4031f4a2713aSLionel Sambuc TypeSourceInfo *TSInfo,
4032f4a2713aSLionel Sambuc Expr* DimExpr,
4033f4a2713aSLionel Sambuc SourceLocation RParen) {
4034f4a2713aSLionel Sambuc QualType T = TSInfo->getType();
4035f4a2713aSLionel Sambuc
4036f4a2713aSLionel Sambuc // FIXME: This should likely be tracked as an APInt to remove any host
4037f4a2713aSLionel Sambuc // assumptions about the width of size_t on the target.
4038f4a2713aSLionel Sambuc uint64_t Value = 0;
4039f4a2713aSLionel Sambuc if (!T->isDependentType())
4040f4a2713aSLionel Sambuc Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc);
4041f4a2713aSLionel Sambuc
4042f4a2713aSLionel Sambuc // While the specification for these traits from the Embarcadero C++
4043f4a2713aSLionel Sambuc // compiler's documentation says the return type is 'unsigned int', Clang
4044f4a2713aSLionel Sambuc // returns 'size_t'. On Windows, the primary platform for the Embarcadero
4045f4a2713aSLionel Sambuc // compiler, there is no difference. On several other platforms this is an
4046f4a2713aSLionel Sambuc // important distinction.
4047*0a6a1f1dSLionel Sambuc return new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, DimExpr,
4048*0a6a1f1dSLionel Sambuc RParen, Context.getSizeType());
4049f4a2713aSLionel Sambuc }
4050f4a2713aSLionel Sambuc
ActOnExpressionTrait(ExpressionTrait ET,SourceLocation KWLoc,Expr * Queried,SourceLocation RParen)4051f4a2713aSLionel Sambuc ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET,
4052f4a2713aSLionel Sambuc SourceLocation KWLoc,
4053f4a2713aSLionel Sambuc Expr *Queried,
4054f4a2713aSLionel Sambuc SourceLocation RParen) {
4055f4a2713aSLionel Sambuc // If error parsing the expression, ignore.
4056f4a2713aSLionel Sambuc if (!Queried)
4057f4a2713aSLionel Sambuc return ExprError();
4058f4a2713aSLionel Sambuc
4059f4a2713aSLionel Sambuc ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen);
4060f4a2713aSLionel Sambuc
4061f4a2713aSLionel Sambuc return Result;
4062f4a2713aSLionel Sambuc }
4063f4a2713aSLionel Sambuc
EvaluateExpressionTrait(ExpressionTrait ET,Expr * E)4064f4a2713aSLionel Sambuc static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) {
4065f4a2713aSLionel Sambuc switch (ET) {
4066f4a2713aSLionel Sambuc case ET_IsLValueExpr: return E->isLValue();
4067f4a2713aSLionel Sambuc case ET_IsRValueExpr: return E->isRValue();
4068f4a2713aSLionel Sambuc }
4069f4a2713aSLionel Sambuc llvm_unreachable("Expression trait not covered by switch");
4070f4a2713aSLionel Sambuc }
4071f4a2713aSLionel Sambuc
BuildExpressionTrait(ExpressionTrait ET,SourceLocation KWLoc,Expr * Queried,SourceLocation RParen)4072f4a2713aSLionel Sambuc ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET,
4073f4a2713aSLionel Sambuc SourceLocation KWLoc,
4074f4a2713aSLionel Sambuc Expr *Queried,
4075f4a2713aSLionel Sambuc SourceLocation RParen) {
4076f4a2713aSLionel Sambuc if (Queried->isTypeDependent()) {
4077f4a2713aSLionel Sambuc // Delay type-checking for type-dependent expressions.
4078f4a2713aSLionel Sambuc } else if (Queried->getType()->isPlaceholderType()) {
4079f4a2713aSLionel Sambuc ExprResult PE = CheckPlaceholderExpr(Queried);
4080f4a2713aSLionel Sambuc if (PE.isInvalid()) return ExprError();
4081*0a6a1f1dSLionel Sambuc return BuildExpressionTrait(ET, KWLoc, PE.get(), RParen);
4082f4a2713aSLionel Sambuc }
4083f4a2713aSLionel Sambuc
4084f4a2713aSLionel Sambuc bool Value = EvaluateExpressionTrait(ET, Queried);
4085f4a2713aSLionel Sambuc
4086*0a6a1f1dSLionel Sambuc return new (Context)
4087*0a6a1f1dSLionel Sambuc ExpressionTraitExpr(KWLoc, ET, Queried, Value, RParen, Context.BoolTy);
4088f4a2713aSLionel Sambuc }
4089f4a2713aSLionel Sambuc
CheckPointerToMemberOperands(ExprResult & LHS,ExprResult & RHS,ExprValueKind & VK,SourceLocation Loc,bool isIndirect)4090f4a2713aSLionel Sambuc QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS,
4091f4a2713aSLionel Sambuc ExprValueKind &VK,
4092f4a2713aSLionel Sambuc SourceLocation Loc,
4093f4a2713aSLionel Sambuc bool isIndirect) {
4094f4a2713aSLionel Sambuc assert(!LHS.get()->getType()->isPlaceholderType() &&
4095f4a2713aSLionel Sambuc !RHS.get()->getType()->isPlaceholderType() &&
4096f4a2713aSLionel Sambuc "placeholders should have been weeded out by now");
4097f4a2713aSLionel Sambuc
4098f4a2713aSLionel Sambuc // The LHS undergoes lvalue conversions if this is ->*.
4099f4a2713aSLionel Sambuc if (isIndirect) {
4100*0a6a1f1dSLionel Sambuc LHS = DefaultLvalueConversion(LHS.get());
4101f4a2713aSLionel Sambuc if (LHS.isInvalid()) return QualType();
4102f4a2713aSLionel Sambuc }
4103f4a2713aSLionel Sambuc
4104f4a2713aSLionel Sambuc // The RHS always undergoes lvalue conversions.
4105*0a6a1f1dSLionel Sambuc RHS = DefaultLvalueConversion(RHS.get());
4106f4a2713aSLionel Sambuc if (RHS.isInvalid()) return QualType();
4107f4a2713aSLionel Sambuc
4108f4a2713aSLionel Sambuc const char *OpSpelling = isIndirect ? "->*" : ".*";
4109f4a2713aSLionel Sambuc // C++ 5.5p2
4110f4a2713aSLionel Sambuc // The binary operator .* [p3: ->*] binds its second operand, which shall
4111f4a2713aSLionel Sambuc // be of type "pointer to member of T" (where T is a completely-defined
4112f4a2713aSLionel Sambuc // class type) [...]
4113f4a2713aSLionel Sambuc QualType RHSType = RHS.get()->getType();
4114f4a2713aSLionel Sambuc const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>();
4115f4a2713aSLionel Sambuc if (!MemPtr) {
4116f4a2713aSLionel Sambuc Diag(Loc, diag::err_bad_memptr_rhs)
4117f4a2713aSLionel Sambuc << OpSpelling << RHSType << RHS.get()->getSourceRange();
4118f4a2713aSLionel Sambuc return QualType();
4119f4a2713aSLionel Sambuc }
4120f4a2713aSLionel Sambuc
4121f4a2713aSLionel Sambuc QualType Class(MemPtr->getClass(), 0);
4122f4a2713aSLionel Sambuc
4123f4a2713aSLionel Sambuc // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the
4124f4a2713aSLionel Sambuc // member pointer points must be completely-defined. However, there is no
4125f4a2713aSLionel Sambuc // reason for this semantic distinction, and the rule is not enforced by
4126f4a2713aSLionel Sambuc // other compilers. Therefore, we do not check this property, as it is
4127f4a2713aSLionel Sambuc // likely to be considered a defect.
4128f4a2713aSLionel Sambuc
4129f4a2713aSLionel Sambuc // C++ 5.5p2
4130f4a2713aSLionel Sambuc // [...] to its first operand, which shall be of class T or of a class of
4131f4a2713aSLionel Sambuc // which T is an unambiguous and accessible base class. [p3: a pointer to
4132f4a2713aSLionel Sambuc // such a class]
4133f4a2713aSLionel Sambuc QualType LHSType = LHS.get()->getType();
4134f4a2713aSLionel Sambuc if (isIndirect) {
4135f4a2713aSLionel Sambuc if (const PointerType *Ptr = LHSType->getAs<PointerType>())
4136f4a2713aSLionel Sambuc LHSType = Ptr->getPointeeType();
4137f4a2713aSLionel Sambuc else {
4138f4a2713aSLionel Sambuc Diag(Loc, diag::err_bad_memptr_lhs)
4139f4a2713aSLionel Sambuc << OpSpelling << 1 << LHSType
4140f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(SourceRange(Loc), ".*");
4141f4a2713aSLionel Sambuc return QualType();
4142f4a2713aSLionel Sambuc }
4143f4a2713aSLionel Sambuc }
4144f4a2713aSLionel Sambuc
4145f4a2713aSLionel Sambuc if (!Context.hasSameUnqualifiedType(Class, LHSType)) {
4146f4a2713aSLionel Sambuc // If we want to check the hierarchy, we need a complete type.
4147f4a2713aSLionel Sambuc if (RequireCompleteType(Loc, LHSType, diag::err_bad_memptr_lhs,
4148f4a2713aSLionel Sambuc OpSpelling, (int)isIndirect)) {
4149f4a2713aSLionel Sambuc return QualType();
4150f4a2713aSLionel Sambuc }
4151*0a6a1f1dSLionel Sambuc
4152*0a6a1f1dSLionel Sambuc if (!IsDerivedFrom(LHSType, Class)) {
4153f4a2713aSLionel Sambuc Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling
4154f4a2713aSLionel Sambuc << (int)isIndirect << LHS.get()->getType();
4155f4a2713aSLionel Sambuc return QualType();
4156f4a2713aSLionel Sambuc }
4157*0a6a1f1dSLionel Sambuc
4158*0a6a1f1dSLionel Sambuc CXXCastPath BasePath;
4159*0a6a1f1dSLionel Sambuc if (CheckDerivedToBaseConversion(LHSType, Class, Loc,
4160*0a6a1f1dSLionel Sambuc SourceRange(LHS.get()->getLocStart(),
4161*0a6a1f1dSLionel Sambuc RHS.get()->getLocEnd()),
4162*0a6a1f1dSLionel Sambuc &BasePath))
4163*0a6a1f1dSLionel Sambuc return QualType();
4164*0a6a1f1dSLionel Sambuc
4165f4a2713aSLionel Sambuc // Cast LHS to type of use.
4166f4a2713aSLionel Sambuc QualType UseType = isIndirect ? Context.getPointerType(Class) : Class;
4167f4a2713aSLionel Sambuc ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind();
4168*0a6a1f1dSLionel Sambuc LHS = ImpCastExprToType(LHS.get(), UseType, CK_DerivedToBase, VK,
4169f4a2713aSLionel Sambuc &BasePath);
4170f4a2713aSLionel Sambuc }
4171f4a2713aSLionel Sambuc
4172f4a2713aSLionel Sambuc if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) {
4173f4a2713aSLionel Sambuc // Diagnose use of pointer-to-member type which when used as
4174f4a2713aSLionel Sambuc // the functional cast in a pointer-to-member expression.
4175f4a2713aSLionel Sambuc Diag(Loc, diag::err_pointer_to_member_type) << isIndirect;
4176f4a2713aSLionel Sambuc return QualType();
4177f4a2713aSLionel Sambuc }
4178f4a2713aSLionel Sambuc
4179f4a2713aSLionel Sambuc // C++ 5.5p2
4180f4a2713aSLionel Sambuc // The result is an object or a function of the type specified by the
4181f4a2713aSLionel Sambuc // second operand.
4182f4a2713aSLionel Sambuc // The cv qualifiers are the union of those in the pointer and the left side,
4183f4a2713aSLionel Sambuc // in accordance with 5.5p5 and 5.2.5.
4184f4a2713aSLionel Sambuc QualType Result = MemPtr->getPointeeType();
4185f4a2713aSLionel Sambuc Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers());
4186f4a2713aSLionel Sambuc
4187f4a2713aSLionel Sambuc // C++0x [expr.mptr.oper]p6:
4188f4a2713aSLionel Sambuc // In a .* expression whose object expression is an rvalue, the program is
4189f4a2713aSLionel Sambuc // ill-formed if the second operand is a pointer to member function with
4190f4a2713aSLionel Sambuc // ref-qualifier &. In a ->* expression or in a .* expression whose object
4191f4a2713aSLionel Sambuc // expression is an lvalue, the program is ill-formed if the second operand
4192f4a2713aSLionel Sambuc // is a pointer to member function with ref-qualifier &&.
4193f4a2713aSLionel Sambuc if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) {
4194f4a2713aSLionel Sambuc switch (Proto->getRefQualifier()) {
4195f4a2713aSLionel Sambuc case RQ_None:
4196f4a2713aSLionel Sambuc // Do nothing
4197f4a2713aSLionel Sambuc break;
4198f4a2713aSLionel Sambuc
4199f4a2713aSLionel Sambuc case RQ_LValue:
4200f4a2713aSLionel Sambuc if (!isIndirect && !LHS.get()->Classify(Context).isLValue())
4201f4a2713aSLionel Sambuc Diag(Loc, diag::err_pointer_to_member_oper_value_classify)
4202f4a2713aSLionel Sambuc << RHSType << 1 << LHS.get()->getSourceRange();
4203f4a2713aSLionel Sambuc break;
4204f4a2713aSLionel Sambuc
4205f4a2713aSLionel Sambuc case RQ_RValue:
4206f4a2713aSLionel Sambuc if (isIndirect || !LHS.get()->Classify(Context).isRValue())
4207f4a2713aSLionel Sambuc Diag(Loc, diag::err_pointer_to_member_oper_value_classify)
4208f4a2713aSLionel Sambuc << RHSType << 0 << LHS.get()->getSourceRange();
4209f4a2713aSLionel Sambuc break;
4210f4a2713aSLionel Sambuc }
4211f4a2713aSLionel Sambuc }
4212f4a2713aSLionel Sambuc
4213f4a2713aSLionel Sambuc // C++ [expr.mptr.oper]p6:
4214f4a2713aSLionel Sambuc // The result of a .* expression whose second operand is a pointer
4215f4a2713aSLionel Sambuc // to a data member is of the same value category as its
4216f4a2713aSLionel Sambuc // first operand. The result of a .* expression whose second
4217f4a2713aSLionel Sambuc // operand is a pointer to a member function is a prvalue. The
4218f4a2713aSLionel Sambuc // result of an ->* expression is an lvalue if its second operand
4219f4a2713aSLionel Sambuc // is a pointer to data member and a prvalue otherwise.
4220f4a2713aSLionel Sambuc if (Result->isFunctionType()) {
4221f4a2713aSLionel Sambuc VK = VK_RValue;
4222f4a2713aSLionel Sambuc return Context.BoundMemberTy;
4223f4a2713aSLionel Sambuc } else if (isIndirect) {
4224f4a2713aSLionel Sambuc VK = VK_LValue;
4225f4a2713aSLionel Sambuc } else {
4226f4a2713aSLionel Sambuc VK = LHS.get()->getValueKind();
4227f4a2713aSLionel Sambuc }
4228f4a2713aSLionel Sambuc
4229f4a2713aSLionel Sambuc return Result;
4230f4a2713aSLionel Sambuc }
4231f4a2713aSLionel Sambuc
4232f4a2713aSLionel Sambuc /// \brief Try to convert a type to another according to C++0x 5.16p3.
4233f4a2713aSLionel Sambuc ///
4234f4a2713aSLionel Sambuc /// This is part of the parameter validation for the ? operator. If either
4235f4a2713aSLionel Sambuc /// value operand is a class type, the two operands are attempted to be
4236f4a2713aSLionel Sambuc /// converted to each other. This function does the conversion in one direction.
4237f4a2713aSLionel Sambuc /// It returns true if the program is ill-formed and has already been diagnosed
4238f4a2713aSLionel Sambuc /// as such.
TryClassUnification(Sema & Self,Expr * From,Expr * To,SourceLocation QuestionLoc,bool & HaveConversion,QualType & ToType)4239f4a2713aSLionel Sambuc static bool TryClassUnification(Sema &Self, Expr *From, Expr *To,
4240f4a2713aSLionel Sambuc SourceLocation QuestionLoc,
4241f4a2713aSLionel Sambuc bool &HaveConversion,
4242f4a2713aSLionel Sambuc QualType &ToType) {
4243f4a2713aSLionel Sambuc HaveConversion = false;
4244f4a2713aSLionel Sambuc ToType = To->getType();
4245f4a2713aSLionel Sambuc
4246f4a2713aSLionel Sambuc InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(),
4247f4a2713aSLionel Sambuc SourceLocation());
4248f4a2713aSLionel Sambuc // C++0x 5.16p3
4249f4a2713aSLionel Sambuc // The process for determining whether an operand expression E1 of type T1
4250f4a2713aSLionel Sambuc // can be converted to match an operand expression E2 of type T2 is defined
4251f4a2713aSLionel Sambuc // as follows:
4252f4a2713aSLionel Sambuc // -- If E2 is an lvalue:
4253f4a2713aSLionel Sambuc bool ToIsLvalue = To->isLValue();
4254f4a2713aSLionel Sambuc if (ToIsLvalue) {
4255f4a2713aSLionel Sambuc // E1 can be converted to match E2 if E1 can be implicitly converted to
4256f4a2713aSLionel Sambuc // type "lvalue reference to T2", subject to the constraint that in the
4257f4a2713aSLionel Sambuc // conversion the reference must bind directly to E1.
4258f4a2713aSLionel Sambuc QualType T = Self.Context.getLValueReferenceType(ToType);
4259f4a2713aSLionel Sambuc InitializedEntity Entity = InitializedEntity::InitializeTemporary(T);
4260f4a2713aSLionel Sambuc
4261f4a2713aSLionel Sambuc InitializationSequence InitSeq(Self, Entity, Kind, From);
4262f4a2713aSLionel Sambuc if (InitSeq.isDirectReferenceBinding()) {
4263f4a2713aSLionel Sambuc ToType = T;
4264f4a2713aSLionel Sambuc HaveConversion = true;
4265f4a2713aSLionel Sambuc return false;
4266f4a2713aSLionel Sambuc }
4267f4a2713aSLionel Sambuc
4268f4a2713aSLionel Sambuc if (InitSeq.isAmbiguous())
4269f4a2713aSLionel Sambuc return InitSeq.Diagnose(Self, Entity, Kind, From);
4270f4a2713aSLionel Sambuc }
4271f4a2713aSLionel Sambuc
4272f4a2713aSLionel Sambuc // -- If E2 is an rvalue, or if the conversion above cannot be done:
4273f4a2713aSLionel Sambuc // -- if E1 and E2 have class type, and the underlying class types are
4274f4a2713aSLionel Sambuc // the same or one is a base class of the other:
4275f4a2713aSLionel Sambuc QualType FTy = From->getType();
4276f4a2713aSLionel Sambuc QualType TTy = To->getType();
4277f4a2713aSLionel Sambuc const RecordType *FRec = FTy->getAs<RecordType>();
4278f4a2713aSLionel Sambuc const RecordType *TRec = TTy->getAs<RecordType>();
4279f4a2713aSLionel Sambuc bool FDerivedFromT = FRec && TRec && FRec != TRec &&
4280f4a2713aSLionel Sambuc Self.IsDerivedFrom(FTy, TTy);
4281f4a2713aSLionel Sambuc if (FRec && TRec &&
4282f4a2713aSLionel Sambuc (FRec == TRec || FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) {
4283f4a2713aSLionel Sambuc // E1 can be converted to match E2 if the class of T2 is the
4284f4a2713aSLionel Sambuc // same type as, or a base class of, the class of T1, and
4285f4a2713aSLionel Sambuc // [cv2 > cv1].
4286f4a2713aSLionel Sambuc if (FRec == TRec || FDerivedFromT) {
4287f4a2713aSLionel Sambuc if (TTy.isAtLeastAsQualifiedAs(FTy)) {
4288f4a2713aSLionel Sambuc InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy);
4289f4a2713aSLionel Sambuc InitializationSequence InitSeq(Self, Entity, Kind, From);
4290f4a2713aSLionel Sambuc if (InitSeq) {
4291f4a2713aSLionel Sambuc HaveConversion = true;
4292f4a2713aSLionel Sambuc return false;
4293f4a2713aSLionel Sambuc }
4294f4a2713aSLionel Sambuc
4295f4a2713aSLionel Sambuc if (InitSeq.isAmbiguous())
4296f4a2713aSLionel Sambuc return InitSeq.Diagnose(Self, Entity, Kind, From);
4297f4a2713aSLionel Sambuc }
4298f4a2713aSLionel Sambuc }
4299f4a2713aSLionel Sambuc
4300f4a2713aSLionel Sambuc return false;
4301f4a2713aSLionel Sambuc }
4302f4a2713aSLionel Sambuc
4303f4a2713aSLionel Sambuc // -- Otherwise: E1 can be converted to match E2 if E1 can be
4304f4a2713aSLionel Sambuc // implicitly converted to the type that expression E2 would have
4305f4a2713aSLionel Sambuc // if E2 were converted to an rvalue (or the type it has, if E2 is
4306f4a2713aSLionel Sambuc // an rvalue).
4307f4a2713aSLionel Sambuc //
4308f4a2713aSLionel Sambuc // This actually refers very narrowly to the lvalue-to-rvalue conversion, not
4309f4a2713aSLionel Sambuc // to the array-to-pointer or function-to-pointer conversions.
4310f4a2713aSLionel Sambuc if (!TTy->getAs<TagType>())
4311f4a2713aSLionel Sambuc TTy = TTy.getUnqualifiedType();
4312f4a2713aSLionel Sambuc
4313f4a2713aSLionel Sambuc InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy);
4314f4a2713aSLionel Sambuc InitializationSequence InitSeq(Self, Entity, Kind, From);
4315f4a2713aSLionel Sambuc HaveConversion = !InitSeq.Failed();
4316f4a2713aSLionel Sambuc ToType = TTy;
4317f4a2713aSLionel Sambuc if (InitSeq.isAmbiguous())
4318f4a2713aSLionel Sambuc return InitSeq.Diagnose(Self, Entity, Kind, From);
4319f4a2713aSLionel Sambuc
4320f4a2713aSLionel Sambuc return false;
4321f4a2713aSLionel Sambuc }
4322f4a2713aSLionel Sambuc
4323f4a2713aSLionel Sambuc /// \brief Try to find a common type for two according to C++0x 5.16p5.
4324f4a2713aSLionel Sambuc ///
4325f4a2713aSLionel Sambuc /// This is part of the parameter validation for the ? operator. If either
4326f4a2713aSLionel Sambuc /// value operand is a class type, overload resolution is used to find a
4327f4a2713aSLionel Sambuc /// conversion to a common type.
FindConditionalOverload(Sema & Self,ExprResult & LHS,ExprResult & RHS,SourceLocation QuestionLoc)4328f4a2713aSLionel Sambuc static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS,
4329f4a2713aSLionel Sambuc SourceLocation QuestionLoc) {
4330f4a2713aSLionel Sambuc Expr *Args[2] = { LHS.get(), RHS.get() };
4331*0a6a1f1dSLionel Sambuc OverloadCandidateSet CandidateSet(QuestionLoc,
4332*0a6a1f1dSLionel Sambuc OverloadCandidateSet::CSK_Operator);
4333f4a2713aSLionel Sambuc Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args,
4334f4a2713aSLionel Sambuc CandidateSet);
4335f4a2713aSLionel Sambuc
4336f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
4337f4a2713aSLionel Sambuc switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) {
4338f4a2713aSLionel Sambuc case OR_Success: {
4339f4a2713aSLionel Sambuc // We found a match. Perform the conversions on the arguments and move on.
4340f4a2713aSLionel Sambuc ExprResult LHSRes =
4341f4a2713aSLionel Sambuc Self.PerformImplicitConversion(LHS.get(), Best->BuiltinTypes.ParamTypes[0],
4342f4a2713aSLionel Sambuc Best->Conversions[0], Sema::AA_Converting);
4343f4a2713aSLionel Sambuc if (LHSRes.isInvalid())
4344f4a2713aSLionel Sambuc break;
4345f4a2713aSLionel Sambuc LHS = LHSRes;
4346f4a2713aSLionel Sambuc
4347f4a2713aSLionel Sambuc ExprResult RHSRes =
4348f4a2713aSLionel Sambuc Self.PerformImplicitConversion(RHS.get(), Best->BuiltinTypes.ParamTypes[1],
4349f4a2713aSLionel Sambuc Best->Conversions[1], Sema::AA_Converting);
4350f4a2713aSLionel Sambuc if (RHSRes.isInvalid())
4351f4a2713aSLionel Sambuc break;
4352f4a2713aSLionel Sambuc RHS = RHSRes;
4353f4a2713aSLionel Sambuc if (Best->Function)
4354f4a2713aSLionel Sambuc Self.MarkFunctionReferenced(QuestionLoc, Best->Function);
4355f4a2713aSLionel Sambuc return false;
4356f4a2713aSLionel Sambuc }
4357f4a2713aSLionel Sambuc
4358f4a2713aSLionel Sambuc case OR_No_Viable_Function:
4359f4a2713aSLionel Sambuc
4360f4a2713aSLionel Sambuc // Emit a better diagnostic if one of the expressions is a null pointer
4361f4a2713aSLionel Sambuc // constant and the other is a pointer type. In this case, the user most
4362f4a2713aSLionel Sambuc // likely forgot to take the address of the other expression.
4363f4a2713aSLionel Sambuc if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc))
4364f4a2713aSLionel Sambuc return true;
4365f4a2713aSLionel Sambuc
4366f4a2713aSLionel Sambuc Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands)
4367f4a2713aSLionel Sambuc << LHS.get()->getType() << RHS.get()->getType()
4368f4a2713aSLionel Sambuc << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
4369f4a2713aSLionel Sambuc return true;
4370f4a2713aSLionel Sambuc
4371f4a2713aSLionel Sambuc case OR_Ambiguous:
4372f4a2713aSLionel Sambuc Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl)
4373f4a2713aSLionel Sambuc << LHS.get()->getType() << RHS.get()->getType()
4374f4a2713aSLionel Sambuc << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
4375f4a2713aSLionel Sambuc // FIXME: Print the possible common types by printing the return types of
4376f4a2713aSLionel Sambuc // the viable candidates.
4377f4a2713aSLionel Sambuc break;
4378f4a2713aSLionel Sambuc
4379f4a2713aSLionel Sambuc case OR_Deleted:
4380f4a2713aSLionel Sambuc llvm_unreachable("Conditional operator has only built-in overloads");
4381f4a2713aSLionel Sambuc }
4382f4a2713aSLionel Sambuc return true;
4383f4a2713aSLionel Sambuc }
4384f4a2713aSLionel Sambuc
4385f4a2713aSLionel Sambuc /// \brief Perform an "extended" implicit conversion as returned by
4386f4a2713aSLionel Sambuc /// TryClassUnification.
ConvertForConditional(Sema & Self,ExprResult & E,QualType T)4387f4a2713aSLionel Sambuc static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) {
4388f4a2713aSLionel Sambuc InitializedEntity Entity = InitializedEntity::InitializeTemporary(T);
4389f4a2713aSLionel Sambuc InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(),
4390f4a2713aSLionel Sambuc SourceLocation());
4391*0a6a1f1dSLionel Sambuc Expr *Arg = E.get();
4392f4a2713aSLionel Sambuc InitializationSequence InitSeq(Self, Entity, Kind, Arg);
4393f4a2713aSLionel Sambuc ExprResult Result = InitSeq.Perform(Self, Entity, Kind, Arg);
4394f4a2713aSLionel Sambuc if (Result.isInvalid())
4395f4a2713aSLionel Sambuc return true;
4396f4a2713aSLionel Sambuc
4397f4a2713aSLionel Sambuc E = Result;
4398f4a2713aSLionel Sambuc return false;
4399f4a2713aSLionel Sambuc }
4400f4a2713aSLionel Sambuc
4401f4a2713aSLionel Sambuc /// \brief Check the operands of ?: under C++ semantics.
4402f4a2713aSLionel Sambuc ///
4403f4a2713aSLionel Sambuc /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y
4404f4a2713aSLionel Sambuc /// extension. In this case, LHS == Cond. (But they're not aliases.)
CXXCheckConditionalOperands(ExprResult & Cond,ExprResult & LHS,ExprResult & RHS,ExprValueKind & VK,ExprObjectKind & OK,SourceLocation QuestionLoc)4405f4a2713aSLionel Sambuc QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS,
4406f4a2713aSLionel Sambuc ExprResult &RHS, ExprValueKind &VK,
4407f4a2713aSLionel Sambuc ExprObjectKind &OK,
4408f4a2713aSLionel Sambuc SourceLocation QuestionLoc) {
4409f4a2713aSLionel Sambuc // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++
4410f4a2713aSLionel Sambuc // interface pointers.
4411f4a2713aSLionel Sambuc
4412f4a2713aSLionel Sambuc // C++11 [expr.cond]p1
4413f4a2713aSLionel Sambuc // The first expression is contextually converted to bool.
4414f4a2713aSLionel Sambuc if (!Cond.get()->isTypeDependent()) {
4415*0a6a1f1dSLionel Sambuc ExprResult CondRes = CheckCXXBooleanCondition(Cond.get());
4416f4a2713aSLionel Sambuc if (CondRes.isInvalid())
4417f4a2713aSLionel Sambuc return QualType();
4418f4a2713aSLionel Sambuc Cond = CondRes;
4419f4a2713aSLionel Sambuc }
4420f4a2713aSLionel Sambuc
4421f4a2713aSLionel Sambuc // Assume r-value.
4422f4a2713aSLionel Sambuc VK = VK_RValue;
4423f4a2713aSLionel Sambuc OK = OK_Ordinary;
4424f4a2713aSLionel Sambuc
4425f4a2713aSLionel Sambuc // Either of the arguments dependent?
4426f4a2713aSLionel Sambuc if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent())
4427f4a2713aSLionel Sambuc return Context.DependentTy;
4428f4a2713aSLionel Sambuc
4429f4a2713aSLionel Sambuc // C++11 [expr.cond]p2
4430f4a2713aSLionel Sambuc // If either the second or the third operand has type (cv) void, ...
4431f4a2713aSLionel Sambuc QualType LTy = LHS.get()->getType();
4432f4a2713aSLionel Sambuc QualType RTy = RHS.get()->getType();
4433f4a2713aSLionel Sambuc bool LVoid = LTy->isVoidType();
4434f4a2713aSLionel Sambuc bool RVoid = RTy->isVoidType();
4435f4a2713aSLionel Sambuc if (LVoid || RVoid) {
4436*0a6a1f1dSLionel Sambuc // ... one of the following shall hold:
4437*0a6a1f1dSLionel Sambuc // -- The second or the third operand (but not both) is a (possibly
4438*0a6a1f1dSLionel Sambuc // parenthesized) throw-expression; the result is of the type
4439*0a6a1f1dSLionel Sambuc // and value category of the other.
4440*0a6a1f1dSLionel Sambuc bool LThrow = isa<CXXThrowExpr>(LHS.get()->IgnoreParenImpCasts());
4441*0a6a1f1dSLionel Sambuc bool RThrow = isa<CXXThrowExpr>(RHS.get()->IgnoreParenImpCasts());
4442*0a6a1f1dSLionel Sambuc if (LThrow != RThrow) {
4443*0a6a1f1dSLionel Sambuc Expr *NonThrow = LThrow ? RHS.get() : LHS.get();
4444*0a6a1f1dSLionel Sambuc VK = NonThrow->getValueKind();
4445*0a6a1f1dSLionel Sambuc // DR (no number yet): the result is a bit-field if the
4446*0a6a1f1dSLionel Sambuc // non-throw-expression operand is a bit-field.
4447*0a6a1f1dSLionel Sambuc OK = NonThrow->getObjectKind();
4448*0a6a1f1dSLionel Sambuc return NonThrow->getType();
4449f4a2713aSLionel Sambuc }
4450f4a2713aSLionel Sambuc
4451f4a2713aSLionel Sambuc // -- Both the second and third operands have type void; the result is of
4452f4a2713aSLionel Sambuc // type void and is a prvalue.
4453f4a2713aSLionel Sambuc if (LVoid && RVoid)
4454f4a2713aSLionel Sambuc return Context.VoidTy;
4455f4a2713aSLionel Sambuc
4456f4a2713aSLionel Sambuc // Neither holds, error.
4457f4a2713aSLionel Sambuc Diag(QuestionLoc, diag::err_conditional_void_nonvoid)
4458f4a2713aSLionel Sambuc << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1)
4459f4a2713aSLionel Sambuc << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
4460f4a2713aSLionel Sambuc return QualType();
4461f4a2713aSLionel Sambuc }
4462f4a2713aSLionel Sambuc
4463f4a2713aSLionel Sambuc // Neither is void.
4464f4a2713aSLionel Sambuc
4465f4a2713aSLionel Sambuc // C++11 [expr.cond]p3
4466f4a2713aSLionel Sambuc // Otherwise, if the second and third operand have different types, and
4467f4a2713aSLionel Sambuc // either has (cv) class type [...] an attempt is made to convert each of
4468f4a2713aSLionel Sambuc // those operands to the type of the other.
4469f4a2713aSLionel Sambuc if (!Context.hasSameType(LTy, RTy) &&
4470f4a2713aSLionel Sambuc (LTy->isRecordType() || RTy->isRecordType())) {
4471f4a2713aSLionel Sambuc // These return true if a single direction is already ambiguous.
4472f4a2713aSLionel Sambuc QualType L2RType, R2LType;
4473f4a2713aSLionel Sambuc bool HaveL2R, HaveR2L;
4474f4a2713aSLionel Sambuc if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType))
4475f4a2713aSLionel Sambuc return QualType();
4476f4a2713aSLionel Sambuc if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType))
4477f4a2713aSLionel Sambuc return QualType();
4478f4a2713aSLionel Sambuc
4479f4a2713aSLionel Sambuc // If both can be converted, [...] the program is ill-formed.
4480f4a2713aSLionel Sambuc if (HaveL2R && HaveR2L) {
4481f4a2713aSLionel Sambuc Diag(QuestionLoc, diag::err_conditional_ambiguous)
4482f4a2713aSLionel Sambuc << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
4483f4a2713aSLionel Sambuc return QualType();
4484f4a2713aSLionel Sambuc }
4485f4a2713aSLionel Sambuc
4486f4a2713aSLionel Sambuc // If exactly one conversion is possible, that conversion is applied to
4487f4a2713aSLionel Sambuc // the chosen operand and the converted operands are used in place of the
4488f4a2713aSLionel Sambuc // original operands for the remainder of this section.
4489f4a2713aSLionel Sambuc if (HaveL2R) {
4490f4a2713aSLionel Sambuc if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid())
4491f4a2713aSLionel Sambuc return QualType();
4492f4a2713aSLionel Sambuc LTy = LHS.get()->getType();
4493f4a2713aSLionel Sambuc } else if (HaveR2L) {
4494f4a2713aSLionel Sambuc if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid())
4495f4a2713aSLionel Sambuc return QualType();
4496f4a2713aSLionel Sambuc RTy = RHS.get()->getType();
4497f4a2713aSLionel Sambuc }
4498f4a2713aSLionel Sambuc }
4499f4a2713aSLionel Sambuc
4500f4a2713aSLionel Sambuc // C++11 [expr.cond]p3
4501f4a2713aSLionel Sambuc // if both are glvalues of the same value category and the same type except
4502f4a2713aSLionel Sambuc // for cv-qualification, an attempt is made to convert each of those
4503f4a2713aSLionel Sambuc // operands to the type of the other.
4504f4a2713aSLionel Sambuc ExprValueKind LVK = LHS.get()->getValueKind();
4505f4a2713aSLionel Sambuc ExprValueKind RVK = RHS.get()->getValueKind();
4506f4a2713aSLionel Sambuc if (!Context.hasSameType(LTy, RTy) &&
4507f4a2713aSLionel Sambuc Context.hasSameUnqualifiedType(LTy, RTy) &&
4508f4a2713aSLionel Sambuc LVK == RVK && LVK != VK_RValue) {
4509f4a2713aSLionel Sambuc // Since the unqualified types are reference-related and we require the
4510f4a2713aSLionel Sambuc // result to be as if a reference bound directly, the only conversion
4511f4a2713aSLionel Sambuc // we can perform is to add cv-qualifiers.
4512f4a2713aSLionel Sambuc Qualifiers LCVR = Qualifiers::fromCVRMask(LTy.getCVRQualifiers());
4513f4a2713aSLionel Sambuc Qualifiers RCVR = Qualifiers::fromCVRMask(RTy.getCVRQualifiers());
4514f4a2713aSLionel Sambuc if (RCVR.isStrictSupersetOf(LCVR)) {
4515*0a6a1f1dSLionel Sambuc LHS = ImpCastExprToType(LHS.get(), RTy, CK_NoOp, LVK);
4516f4a2713aSLionel Sambuc LTy = LHS.get()->getType();
4517f4a2713aSLionel Sambuc }
4518f4a2713aSLionel Sambuc else if (LCVR.isStrictSupersetOf(RCVR)) {
4519*0a6a1f1dSLionel Sambuc RHS = ImpCastExprToType(RHS.get(), LTy, CK_NoOp, RVK);
4520f4a2713aSLionel Sambuc RTy = RHS.get()->getType();
4521f4a2713aSLionel Sambuc }
4522f4a2713aSLionel Sambuc }
4523f4a2713aSLionel Sambuc
4524f4a2713aSLionel Sambuc // C++11 [expr.cond]p4
4525f4a2713aSLionel Sambuc // If the second and third operands are glvalues of the same value
4526f4a2713aSLionel Sambuc // category and have the same type, the result is of that type and
4527f4a2713aSLionel Sambuc // value category and it is a bit-field if the second or the third
4528f4a2713aSLionel Sambuc // operand is a bit-field, or if both are bit-fields.
4529f4a2713aSLionel Sambuc // We only extend this to bitfields, not to the crazy other kinds of
4530f4a2713aSLionel Sambuc // l-values.
4531f4a2713aSLionel Sambuc bool Same = Context.hasSameType(LTy, RTy);
4532f4a2713aSLionel Sambuc if (Same && LVK == RVK && LVK != VK_RValue &&
4533f4a2713aSLionel Sambuc LHS.get()->isOrdinaryOrBitFieldObject() &&
4534f4a2713aSLionel Sambuc RHS.get()->isOrdinaryOrBitFieldObject()) {
4535f4a2713aSLionel Sambuc VK = LHS.get()->getValueKind();
4536f4a2713aSLionel Sambuc if (LHS.get()->getObjectKind() == OK_BitField ||
4537f4a2713aSLionel Sambuc RHS.get()->getObjectKind() == OK_BitField)
4538f4a2713aSLionel Sambuc OK = OK_BitField;
4539f4a2713aSLionel Sambuc return LTy;
4540f4a2713aSLionel Sambuc }
4541f4a2713aSLionel Sambuc
4542f4a2713aSLionel Sambuc // C++11 [expr.cond]p5
4543f4a2713aSLionel Sambuc // Otherwise, the result is a prvalue. If the second and third operands
4544f4a2713aSLionel Sambuc // do not have the same type, and either has (cv) class type, ...
4545f4a2713aSLionel Sambuc if (!Same && (LTy->isRecordType() || RTy->isRecordType())) {
4546f4a2713aSLionel Sambuc // ... overload resolution is used to determine the conversions (if any)
4547f4a2713aSLionel Sambuc // to be applied to the operands. If the overload resolution fails, the
4548f4a2713aSLionel Sambuc // program is ill-formed.
4549f4a2713aSLionel Sambuc if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc))
4550f4a2713aSLionel Sambuc return QualType();
4551f4a2713aSLionel Sambuc }
4552f4a2713aSLionel Sambuc
4553f4a2713aSLionel Sambuc // C++11 [expr.cond]p6
4554f4a2713aSLionel Sambuc // Lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard
4555f4a2713aSLionel Sambuc // conversions are performed on the second and third operands.
4556*0a6a1f1dSLionel Sambuc LHS = DefaultFunctionArrayLvalueConversion(LHS.get());
4557*0a6a1f1dSLionel Sambuc RHS = DefaultFunctionArrayLvalueConversion(RHS.get());
4558f4a2713aSLionel Sambuc if (LHS.isInvalid() || RHS.isInvalid())
4559f4a2713aSLionel Sambuc return QualType();
4560f4a2713aSLionel Sambuc LTy = LHS.get()->getType();
4561f4a2713aSLionel Sambuc RTy = RHS.get()->getType();
4562f4a2713aSLionel Sambuc
4563f4a2713aSLionel Sambuc // After those conversions, one of the following shall hold:
4564f4a2713aSLionel Sambuc // -- The second and third operands have the same type; the result
4565f4a2713aSLionel Sambuc // is of that type. If the operands have class type, the result
4566f4a2713aSLionel Sambuc // is a prvalue temporary of the result type, which is
4567f4a2713aSLionel Sambuc // copy-initialized from either the second operand or the third
4568f4a2713aSLionel Sambuc // operand depending on the value of the first operand.
4569f4a2713aSLionel Sambuc if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) {
4570f4a2713aSLionel Sambuc if (LTy->isRecordType()) {
4571f4a2713aSLionel Sambuc // The operands have class type. Make a temporary copy.
4572f4a2713aSLionel Sambuc if (RequireNonAbstractType(QuestionLoc, LTy,
4573f4a2713aSLionel Sambuc diag::err_allocation_of_abstract_type))
4574f4a2713aSLionel Sambuc return QualType();
4575f4a2713aSLionel Sambuc InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy);
4576f4a2713aSLionel Sambuc
4577f4a2713aSLionel Sambuc ExprResult LHSCopy = PerformCopyInitialization(Entity,
4578f4a2713aSLionel Sambuc SourceLocation(),
4579f4a2713aSLionel Sambuc LHS);
4580f4a2713aSLionel Sambuc if (LHSCopy.isInvalid())
4581f4a2713aSLionel Sambuc return QualType();
4582f4a2713aSLionel Sambuc
4583f4a2713aSLionel Sambuc ExprResult RHSCopy = PerformCopyInitialization(Entity,
4584f4a2713aSLionel Sambuc SourceLocation(),
4585f4a2713aSLionel Sambuc RHS);
4586f4a2713aSLionel Sambuc if (RHSCopy.isInvalid())
4587f4a2713aSLionel Sambuc return QualType();
4588f4a2713aSLionel Sambuc
4589f4a2713aSLionel Sambuc LHS = LHSCopy;
4590f4a2713aSLionel Sambuc RHS = RHSCopy;
4591f4a2713aSLionel Sambuc }
4592f4a2713aSLionel Sambuc
4593f4a2713aSLionel Sambuc return LTy;
4594f4a2713aSLionel Sambuc }
4595f4a2713aSLionel Sambuc
4596f4a2713aSLionel Sambuc // Extension: conditional operator involving vector types.
4597f4a2713aSLionel Sambuc if (LTy->isVectorType() || RTy->isVectorType())
4598f4a2713aSLionel Sambuc return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false);
4599f4a2713aSLionel Sambuc
4600f4a2713aSLionel Sambuc // -- The second and third operands have arithmetic or enumeration type;
4601f4a2713aSLionel Sambuc // the usual arithmetic conversions are performed to bring them to a
4602f4a2713aSLionel Sambuc // common type, and the result is of that type.
4603f4a2713aSLionel Sambuc if (LTy->isArithmeticType() && RTy->isArithmeticType()) {
4604*0a6a1f1dSLionel Sambuc QualType ResTy = UsualArithmeticConversions(LHS, RHS);
4605f4a2713aSLionel Sambuc if (LHS.isInvalid() || RHS.isInvalid())
4606f4a2713aSLionel Sambuc return QualType();
4607*0a6a1f1dSLionel Sambuc
4608*0a6a1f1dSLionel Sambuc LHS = ImpCastExprToType(LHS.get(), ResTy, PrepareScalarCast(LHS, ResTy));
4609*0a6a1f1dSLionel Sambuc RHS = ImpCastExprToType(RHS.get(), ResTy, PrepareScalarCast(RHS, ResTy));
4610*0a6a1f1dSLionel Sambuc
4611*0a6a1f1dSLionel Sambuc return ResTy;
4612f4a2713aSLionel Sambuc }
4613f4a2713aSLionel Sambuc
4614f4a2713aSLionel Sambuc // -- The second and third operands have pointer type, or one has pointer
4615f4a2713aSLionel Sambuc // type and the other is a null pointer constant, or both are null
4616f4a2713aSLionel Sambuc // pointer constants, at least one of which is non-integral; pointer
4617f4a2713aSLionel Sambuc // conversions and qualification conversions are performed to bring them
4618f4a2713aSLionel Sambuc // to their composite pointer type. The result is of the composite
4619f4a2713aSLionel Sambuc // pointer type.
4620f4a2713aSLionel Sambuc // -- The second and third operands have pointer to member type, or one has
4621f4a2713aSLionel Sambuc // pointer to member type and the other is a null pointer constant;
4622f4a2713aSLionel Sambuc // pointer to member conversions and qualification conversions are
4623f4a2713aSLionel Sambuc // performed to bring them to a common type, whose cv-qualification
4624f4a2713aSLionel Sambuc // shall match the cv-qualification of either the second or the third
4625f4a2713aSLionel Sambuc // operand. The result is of the common type.
4626f4a2713aSLionel Sambuc bool NonStandardCompositeType = false;
4627f4a2713aSLionel Sambuc QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS,
4628*0a6a1f1dSLionel Sambuc isSFINAEContext() ? nullptr
4629*0a6a1f1dSLionel Sambuc : &NonStandardCompositeType);
4630f4a2713aSLionel Sambuc if (!Composite.isNull()) {
4631f4a2713aSLionel Sambuc if (NonStandardCompositeType)
4632f4a2713aSLionel Sambuc Diag(QuestionLoc,
4633f4a2713aSLionel Sambuc diag::ext_typecheck_cond_incompatible_operands_nonstandard)
4634f4a2713aSLionel Sambuc << LTy << RTy << Composite
4635f4a2713aSLionel Sambuc << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
4636f4a2713aSLionel Sambuc
4637f4a2713aSLionel Sambuc return Composite;
4638f4a2713aSLionel Sambuc }
4639f4a2713aSLionel Sambuc
4640f4a2713aSLionel Sambuc // Similarly, attempt to find composite type of two objective-c pointers.
4641f4a2713aSLionel Sambuc Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc);
4642f4a2713aSLionel Sambuc if (!Composite.isNull())
4643f4a2713aSLionel Sambuc return Composite;
4644f4a2713aSLionel Sambuc
4645f4a2713aSLionel Sambuc // Check if we are using a null with a non-pointer type.
4646f4a2713aSLionel Sambuc if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc))
4647f4a2713aSLionel Sambuc return QualType();
4648f4a2713aSLionel Sambuc
4649f4a2713aSLionel Sambuc Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands)
4650f4a2713aSLionel Sambuc << LHS.get()->getType() << RHS.get()->getType()
4651f4a2713aSLionel Sambuc << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
4652f4a2713aSLionel Sambuc return QualType();
4653f4a2713aSLionel Sambuc }
4654f4a2713aSLionel Sambuc
4655f4a2713aSLionel Sambuc /// \brief Find a merged pointer type and convert the two expressions to it.
4656f4a2713aSLionel Sambuc ///
4657f4a2713aSLionel Sambuc /// This finds the composite pointer type (or member pointer type) for @p E1
4658f4a2713aSLionel Sambuc /// and @p E2 according to C++11 5.9p2. It converts both expressions to this
4659f4a2713aSLionel Sambuc /// type and returns it.
4660f4a2713aSLionel Sambuc /// It does not emit diagnostics.
4661f4a2713aSLionel Sambuc ///
4662f4a2713aSLionel Sambuc /// \param Loc The location of the operator requiring these two expressions to
4663f4a2713aSLionel Sambuc /// be converted to the composite pointer type.
4664f4a2713aSLionel Sambuc ///
4665f4a2713aSLionel Sambuc /// If \p NonStandardCompositeType is non-NULL, then we are permitted to find
4666f4a2713aSLionel Sambuc /// a non-standard (but still sane) composite type to which both expressions
4667f4a2713aSLionel Sambuc /// can be converted. When such a type is chosen, \c *NonStandardCompositeType
4668f4a2713aSLionel Sambuc /// will be set true.
FindCompositePointerType(SourceLocation Loc,Expr * & E1,Expr * & E2,bool * NonStandardCompositeType)4669f4a2713aSLionel Sambuc QualType Sema::FindCompositePointerType(SourceLocation Loc,
4670f4a2713aSLionel Sambuc Expr *&E1, Expr *&E2,
4671f4a2713aSLionel Sambuc bool *NonStandardCompositeType) {
4672f4a2713aSLionel Sambuc if (NonStandardCompositeType)
4673f4a2713aSLionel Sambuc *NonStandardCompositeType = false;
4674f4a2713aSLionel Sambuc
4675f4a2713aSLionel Sambuc assert(getLangOpts().CPlusPlus && "This function assumes C++");
4676f4a2713aSLionel Sambuc QualType T1 = E1->getType(), T2 = E2->getType();
4677f4a2713aSLionel Sambuc
4678f4a2713aSLionel Sambuc // C++11 5.9p2
4679f4a2713aSLionel Sambuc // Pointer conversions and qualification conversions are performed on
4680f4a2713aSLionel Sambuc // pointer operands to bring them to their composite pointer type. If
4681f4a2713aSLionel Sambuc // one operand is a null pointer constant, the composite pointer type is
4682f4a2713aSLionel Sambuc // std::nullptr_t if the other operand is also a null pointer constant or,
4683f4a2713aSLionel Sambuc // if the other operand is a pointer, the type of the other operand.
4684f4a2713aSLionel Sambuc if (!T1->isAnyPointerType() && !T1->isMemberPointerType() &&
4685f4a2713aSLionel Sambuc !T2->isAnyPointerType() && !T2->isMemberPointerType()) {
4686f4a2713aSLionel Sambuc if (T1->isNullPtrType() &&
4687f4a2713aSLionel Sambuc E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
4688*0a6a1f1dSLionel Sambuc E2 = ImpCastExprToType(E2, T1, CK_NullToPointer).get();
4689f4a2713aSLionel Sambuc return T1;
4690f4a2713aSLionel Sambuc }
4691f4a2713aSLionel Sambuc if (T2->isNullPtrType() &&
4692f4a2713aSLionel Sambuc E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
4693*0a6a1f1dSLionel Sambuc E1 = ImpCastExprToType(E1, T2, CK_NullToPointer).get();
4694f4a2713aSLionel Sambuc return T2;
4695f4a2713aSLionel Sambuc }
4696f4a2713aSLionel Sambuc return QualType();
4697f4a2713aSLionel Sambuc }
4698f4a2713aSLionel Sambuc
4699f4a2713aSLionel Sambuc if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
4700f4a2713aSLionel Sambuc if (T2->isMemberPointerType())
4701*0a6a1f1dSLionel Sambuc E1 = ImpCastExprToType(E1, T2, CK_NullToMemberPointer).get();
4702f4a2713aSLionel Sambuc else
4703*0a6a1f1dSLionel Sambuc E1 = ImpCastExprToType(E1, T2, CK_NullToPointer).get();
4704f4a2713aSLionel Sambuc return T2;
4705f4a2713aSLionel Sambuc }
4706f4a2713aSLionel Sambuc if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
4707f4a2713aSLionel Sambuc if (T1->isMemberPointerType())
4708*0a6a1f1dSLionel Sambuc E2 = ImpCastExprToType(E2, T1, CK_NullToMemberPointer).get();
4709f4a2713aSLionel Sambuc else
4710*0a6a1f1dSLionel Sambuc E2 = ImpCastExprToType(E2, T1, CK_NullToPointer).get();
4711f4a2713aSLionel Sambuc return T1;
4712f4a2713aSLionel Sambuc }
4713f4a2713aSLionel Sambuc
4714f4a2713aSLionel Sambuc // Now both have to be pointers or member pointers.
4715f4a2713aSLionel Sambuc if ((!T1->isPointerType() && !T1->isMemberPointerType()) ||
4716f4a2713aSLionel Sambuc (!T2->isPointerType() && !T2->isMemberPointerType()))
4717f4a2713aSLionel Sambuc return QualType();
4718f4a2713aSLionel Sambuc
4719f4a2713aSLionel Sambuc // Otherwise, of one of the operands has type "pointer to cv1 void," then
4720f4a2713aSLionel Sambuc // the other has type "pointer to cv2 T" and the composite pointer type is
4721f4a2713aSLionel Sambuc // "pointer to cv12 void," where cv12 is the union of cv1 and cv2.
4722f4a2713aSLionel Sambuc // Otherwise, the composite pointer type is a pointer type similar to the
4723f4a2713aSLionel Sambuc // type of one of the operands, with a cv-qualification signature that is
4724f4a2713aSLionel Sambuc // the union of the cv-qualification signatures of the operand types.
4725f4a2713aSLionel Sambuc // In practice, the first part here is redundant; it's subsumed by the second.
4726f4a2713aSLionel Sambuc // What we do here is, we build the two possible composite types, and try the
4727f4a2713aSLionel Sambuc // conversions in both directions. If only one works, or if the two composite
4728f4a2713aSLionel Sambuc // types are the same, we have succeeded.
4729f4a2713aSLionel Sambuc // FIXME: extended qualifiers?
4730f4a2713aSLionel Sambuc typedef SmallVector<unsigned, 4> QualifierVector;
4731f4a2713aSLionel Sambuc QualifierVector QualifierUnion;
4732f4a2713aSLionel Sambuc typedef SmallVector<std::pair<const Type *, const Type *>, 4>
4733f4a2713aSLionel Sambuc ContainingClassVector;
4734f4a2713aSLionel Sambuc ContainingClassVector MemberOfClass;
4735f4a2713aSLionel Sambuc QualType Composite1 = Context.getCanonicalType(T1),
4736f4a2713aSLionel Sambuc Composite2 = Context.getCanonicalType(T2);
4737f4a2713aSLionel Sambuc unsigned NeedConstBefore = 0;
4738f4a2713aSLionel Sambuc do {
4739f4a2713aSLionel Sambuc const PointerType *Ptr1, *Ptr2;
4740f4a2713aSLionel Sambuc if ((Ptr1 = Composite1->getAs<PointerType>()) &&
4741f4a2713aSLionel Sambuc (Ptr2 = Composite2->getAs<PointerType>())) {
4742f4a2713aSLionel Sambuc Composite1 = Ptr1->getPointeeType();
4743f4a2713aSLionel Sambuc Composite2 = Ptr2->getPointeeType();
4744f4a2713aSLionel Sambuc
4745f4a2713aSLionel Sambuc // If we're allowed to create a non-standard composite type, keep track
4746f4a2713aSLionel Sambuc // of where we need to fill in additional 'const' qualifiers.
4747f4a2713aSLionel Sambuc if (NonStandardCompositeType &&
4748f4a2713aSLionel Sambuc Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers())
4749f4a2713aSLionel Sambuc NeedConstBefore = QualifierUnion.size();
4750f4a2713aSLionel Sambuc
4751f4a2713aSLionel Sambuc QualifierUnion.push_back(
4752f4a2713aSLionel Sambuc Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers());
4753*0a6a1f1dSLionel Sambuc MemberOfClass.push_back(std::make_pair(nullptr, nullptr));
4754f4a2713aSLionel Sambuc continue;
4755f4a2713aSLionel Sambuc }
4756f4a2713aSLionel Sambuc
4757f4a2713aSLionel Sambuc const MemberPointerType *MemPtr1, *MemPtr2;
4758f4a2713aSLionel Sambuc if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) &&
4759f4a2713aSLionel Sambuc (MemPtr2 = Composite2->getAs<MemberPointerType>())) {
4760f4a2713aSLionel Sambuc Composite1 = MemPtr1->getPointeeType();
4761f4a2713aSLionel Sambuc Composite2 = MemPtr2->getPointeeType();
4762f4a2713aSLionel Sambuc
4763f4a2713aSLionel Sambuc // If we're allowed to create a non-standard composite type, keep track
4764f4a2713aSLionel Sambuc // of where we need to fill in additional 'const' qualifiers.
4765f4a2713aSLionel Sambuc if (NonStandardCompositeType &&
4766f4a2713aSLionel Sambuc Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers())
4767f4a2713aSLionel Sambuc NeedConstBefore = QualifierUnion.size();
4768f4a2713aSLionel Sambuc
4769f4a2713aSLionel Sambuc QualifierUnion.push_back(
4770f4a2713aSLionel Sambuc Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers());
4771f4a2713aSLionel Sambuc MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(),
4772f4a2713aSLionel Sambuc MemPtr2->getClass()));
4773f4a2713aSLionel Sambuc continue;
4774f4a2713aSLionel Sambuc }
4775f4a2713aSLionel Sambuc
4776f4a2713aSLionel Sambuc // FIXME: block pointer types?
4777f4a2713aSLionel Sambuc
4778f4a2713aSLionel Sambuc // Cannot unwrap any more types.
4779f4a2713aSLionel Sambuc break;
4780f4a2713aSLionel Sambuc } while (true);
4781f4a2713aSLionel Sambuc
4782f4a2713aSLionel Sambuc if (NeedConstBefore && NonStandardCompositeType) {
4783f4a2713aSLionel Sambuc // Extension: Add 'const' to qualifiers that come before the first qualifier
4784f4a2713aSLionel Sambuc // mismatch, so that our (non-standard!) composite type meets the
4785f4a2713aSLionel Sambuc // requirements of C++ [conv.qual]p4 bullet 3.
4786f4a2713aSLionel Sambuc for (unsigned I = 0; I != NeedConstBefore; ++I) {
4787f4a2713aSLionel Sambuc if ((QualifierUnion[I] & Qualifiers::Const) == 0) {
4788f4a2713aSLionel Sambuc QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const;
4789f4a2713aSLionel Sambuc *NonStandardCompositeType = true;
4790f4a2713aSLionel Sambuc }
4791f4a2713aSLionel Sambuc }
4792f4a2713aSLionel Sambuc }
4793f4a2713aSLionel Sambuc
4794f4a2713aSLionel Sambuc // Rewrap the composites as pointers or member pointers with the union CVRs.
4795f4a2713aSLionel Sambuc ContainingClassVector::reverse_iterator MOC
4796f4a2713aSLionel Sambuc = MemberOfClass.rbegin();
4797f4a2713aSLionel Sambuc for (QualifierVector::reverse_iterator
4798f4a2713aSLionel Sambuc I = QualifierUnion.rbegin(),
4799f4a2713aSLionel Sambuc E = QualifierUnion.rend();
4800f4a2713aSLionel Sambuc I != E; (void)++I, ++MOC) {
4801f4a2713aSLionel Sambuc Qualifiers Quals = Qualifiers::fromCVRMask(*I);
4802f4a2713aSLionel Sambuc if (MOC->first && MOC->second) {
4803f4a2713aSLionel Sambuc // Rebuild member pointer type
4804f4a2713aSLionel Sambuc Composite1 = Context.getMemberPointerType(
4805f4a2713aSLionel Sambuc Context.getQualifiedType(Composite1, Quals),
4806f4a2713aSLionel Sambuc MOC->first);
4807f4a2713aSLionel Sambuc Composite2 = Context.getMemberPointerType(
4808f4a2713aSLionel Sambuc Context.getQualifiedType(Composite2, Quals),
4809f4a2713aSLionel Sambuc MOC->second);
4810f4a2713aSLionel Sambuc } else {
4811f4a2713aSLionel Sambuc // Rebuild pointer type
4812f4a2713aSLionel Sambuc Composite1
4813f4a2713aSLionel Sambuc = Context.getPointerType(Context.getQualifiedType(Composite1, Quals));
4814f4a2713aSLionel Sambuc Composite2
4815f4a2713aSLionel Sambuc = Context.getPointerType(Context.getQualifiedType(Composite2, Quals));
4816f4a2713aSLionel Sambuc }
4817f4a2713aSLionel Sambuc }
4818f4a2713aSLionel Sambuc
4819f4a2713aSLionel Sambuc // Try to convert to the first composite pointer type.
4820f4a2713aSLionel Sambuc InitializedEntity Entity1
4821f4a2713aSLionel Sambuc = InitializedEntity::InitializeTemporary(Composite1);
4822f4a2713aSLionel Sambuc InitializationKind Kind
4823f4a2713aSLionel Sambuc = InitializationKind::CreateCopy(Loc, SourceLocation());
4824f4a2713aSLionel Sambuc InitializationSequence E1ToC1(*this, Entity1, Kind, E1);
4825f4a2713aSLionel Sambuc InitializationSequence E2ToC1(*this, Entity1, Kind, E2);
4826f4a2713aSLionel Sambuc
4827f4a2713aSLionel Sambuc if (E1ToC1 && E2ToC1) {
4828f4a2713aSLionel Sambuc // Conversion to Composite1 is viable.
4829f4a2713aSLionel Sambuc if (!Context.hasSameType(Composite1, Composite2)) {
4830f4a2713aSLionel Sambuc // Composite2 is a different type from Composite1. Check whether
4831f4a2713aSLionel Sambuc // Composite2 is also viable.
4832f4a2713aSLionel Sambuc InitializedEntity Entity2
4833f4a2713aSLionel Sambuc = InitializedEntity::InitializeTemporary(Composite2);
4834f4a2713aSLionel Sambuc InitializationSequence E1ToC2(*this, Entity2, Kind, E1);
4835f4a2713aSLionel Sambuc InitializationSequence E2ToC2(*this, Entity2, Kind, E2);
4836f4a2713aSLionel Sambuc if (E1ToC2 && E2ToC2) {
4837f4a2713aSLionel Sambuc // Both Composite1 and Composite2 are viable and are different;
4838f4a2713aSLionel Sambuc // this is an ambiguity.
4839f4a2713aSLionel Sambuc return QualType();
4840f4a2713aSLionel Sambuc }
4841f4a2713aSLionel Sambuc }
4842f4a2713aSLionel Sambuc
4843f4a2713aSLionel Sambuc // Convert E1 to Composite1
4844f4a2713aSLionel Sambuc ExprResult E1Result
4845f4a2713aSLionel Sambuc = E1ToC1.Perform(*this, Entity1, Kind, E1);
4846f4a2713aSLionel Sambuc if (E1Result.isInvalid())
4847f4a2713aSLionel Sambuc return QualType();
4848*0a6a1f1dSLionel Sambuc E1 = E1Result.getAs<Expr>();
4849f4a2713aSLionel Sambuc
4850f4a2713aSLionel Sambuc // Convert E2 to Composite1
4851f4a2713aSLionel Sambuc ExprResult E2Result
4852f4a2713aSLionel Sambuc = E2ToC1.Perform(*this, Entity1, Kind, E2);
4853f4a2713aSLionel Sambuc if (E2Result.isInvalid())
4854f4a2713aSLionel Sambuc return QualType();
4855*0a6a1f1dSLionel Sambuc E2 = E2Result.getAs<Expr>();
4856f4a2713aSLionel Sambuc
4857f4a2713aSLionel Sambuc return Composite1;
4858f4a2713aSLionel Sambuc }
4859f4a2713aSLionel Sambuc
4860f4a2713aSLionel Sambuc // Check whether Composite2 is viable.
4861f4a2713aSLionel Sambuc InitializedEntity Entity2
4862f4a2713aSLionel Sambuc = InitializedEntity::InitializeTemporary(Composite2);
4863f4a2713aSLionel Sambuc InitializationSequence E1ToC2(*this, Entity2, Kind, E1);
4864f4a2713aSLionel Sambuc InitializationSequence E2ToC2(*this, Entity2, Kind, E2);
4865f4a2713aSLionel Sambuc if (!E1ToC2 || !E2ToC2)
4866f4a2713aSLionel Sambuc return QualType();
4867f4a2713aSLionel Sambuc
4868f4a2713aSLionel Sambuc // Convert E1 to Composite2
4869f4a2713aSLionel Sambuc ExprResult E1Result
4870f4a2713aSLionel Sambuc = E1ToC2.Perform(*this, Entity2, Kind, E1);
4871f4a2713aSLionel Sambuc if (E1Result.isInvalid())
4872f4a2713aSLionel Sambuc return QualType();
4873*0a6a1f1dSLionel Sambuc E1 = E1Result.getAs<Expr>();
4874f4a2713aSLionel Sambuc
4875f4a2713aSLionel Sambuc // Convert E2 to Composite2
4876f4a2713aSLionel Sambuc ExprResult E2Result
4877f4a2713aSLionel Sambuc = E2ToC2.Perform(*this, Entity2, Kind, E2);
4878f4a2713aSLionel Sambuc if (E2Result.isInvalid())
4879f4a2713aSLionel Sambuc return QualType();
4880*0a6a1f1dSLionel Sambuc E2 = E2Result.getAs<Expr>();
4881f4a2713aSLionel Sambuc
4882f4a2713aSLionel Sambuc return Composite2;
4883f4a2713aSLionel Sambuc }
4884f4a2713aSLionel Sambuc
MaybeBindToTemporary(Expr * E)4885f4a2713aSLionel Sambuc ExprResult Sema::MaybeBindToTemporary(Expr *E) {
4886f4a2713aSLionel Sambuc if (!E)
4887f4a2713aSLionel Sambuc return ExprError();
4888f4a2713aSLionel Sambuc
4889f4a2713aSLionel Sambuc assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?");
4890f4a2713aSLionel Sambuc
4891f4a2713aSLionel Sambuc // If the result is a glvalue, we shouldn't bind it.
4892f4a2713aSLionel Sambuc if (!E->isRValue())
4893*0a6a1f1dSLionel Sambuc return E;
4894f4a2713aSLionel Sambuc
4895f4a2713aSLionel Sambuc // In ARC, calls that return a retainable type can return retained,
4896f4a2713aSLionel Sambuc // in which case we have to insert a consuming cast.
4897f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount &&
4898f4a2713aSLionel Sambuc E->getType()->isObjCRetainableType()) {
4899f4a2713aSLionel Sambuc
4900f4a2713aSLionel Sambuc bool ReturnsRetained;
4901f4a2713aSLionel Sambuc
4902f4a2713aSLionel Sambuc // For actual calls, we compute this by examining the type of the
4903f4a2713aSLionel Sambuc // called value.
4904f4a2713aSLionel Sambuc if (CallExpr *Call = dyn_cast<CallExpr>(E)) {
4905f4a2713aSLionel Sambuc Expr *Callee = Call->getCallee()->IgnoreParens();
4906f4a2713aSLionel Sambuc QualType T = Callee->getType();
4907f4a2713aSLionel Sambuc
4908f4a2713aSLionel Sambuc if (T == Context.BoundMemberTy) {
4909f4a2713aSLionel Sambuc // Handle pointer-to-members.
4910f4a2713aSLionel Sambuc if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee))
4911f4a2713aSLionel Sambuc T = BinOp->getRHS()->getType();
4912f4a2713aSLionel Sambuc else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee))
4913f4a2713aSLionel Sambuc T = Mem->getMemberDecl()->getType();
4914f4a2713aSLionel Sambuc }
4915f4a2713aSLionel Sambuc
4916f4a2713aSLionel Sambuc if (const PointerType *Ptr = T->getAs<PointerType>())
4917f4a2713aSLionel Sambuc T = Ptr->getPointeeType();
4918f4a2713aSLionel Sambuc else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>())
4919f4a2713aSLionel Sambuc T = Ptr->getPointeeType();
4920f4a2713aSLionel Sambuc else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>())
4921f4a2713aSLionel Sambuc T = MemPtr->getPointeeType();
4922f4a2713aSLionel Sambuc
4923f4a2713aSLionel Sambuc const FunctionType *FTy = T->getAs<FunctionType>();
4924f4a2713aSLionel Sambuc assert(FTy && "call to value not of function type?");
4925f4a2713aSLionel Sambuc ReturnsRetained = FTy->getExtInfo().getProducesResult();
4926f4a2713aSLionel Sambuc
4927f4a2713aSLionel Sambuc // ActOnStmtExpr arranges things so that StmtExprs of retainable
4928f4a2713aSLionel Sambuc // type always produce a +1 object.
4929f4a2713aSLionel Sambuc } else if (isa<StmtExpr>(E)) {
4930f4a2713aSLionel Sambuc ReturnsRetained = true;
4931f4a2713aSLionel Sambuc
4932f4a2713aSLionel Sambuc // We hit this case with the lambda conversion-to-block optimization;
4933f4a2713aSLionel Sambuc // we don't want any extra casts here.
4934f4a2713aSLionel Sambuc } else if (isa<CastExpr>(E) &&
4935f4a2713aSLionel Sambuc isa<BlockExpr>(cast<CastExpr>(E)->getSubExpr())) {
4936*0a6a1f1dSLionel Sambuc return E;
4937f4a2713aSLionel Sambuc
4938f4a2713aSLionel Sambuc // For message sends and property references, we try to find an
4939f4a2713aSLionel Sambuc // actual method. FIXME: we should infer retention by selector in
4940f4a2713aSLionel Sambuc // cases where we don't have an actual method.
4941f4a2713aSLionel Sambuc } else {
4942*0a6a1f1dSLionel Sambuc ObjCMethodDecl *D = nullptr;
4943f4a2713aSLionel Sambuc if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) {
4944f4a2713aSLionel Sambuc D = Send->getMethodDecl();
4945f4a2713aSLionel Sambuc } else if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(E)) {
4946f4a2713aSLionel Sambuc D = BoxedExpr->getBoxingMethod();
4947f4a2713aSLionel Sambuc } else if (ObjCArrayLiteral *ArrayLit = dyn_cast<ObjCArrayLiteral>(E)) {
4948f4a2713aSLionel Sambuc D = ArrayLit->getArrayWithObjectsMethod();
4949f4a2713aSLionel Sambuc } else if (ObjCDictionaryLiteral *DictLit
4950f4a2713aSLionel Sambuc = dyn_cast<ObjCDictionaryLiteral>(E)) {
4951f4a2713aSLionel Sambuc D = DictLit->getDictWithObjectsMethod();
4952f4a2713aSLionel Sambuc }
4953f4a2713aSLionel Sambuc
4954f4a2713aSLionel Sambuc ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>());
4955f4a2713aSLionel Sambuc
4956f4a2713aSLionel Sambuc // Don't do reclaims on performSelector calls; despite their
4957f4a2713aSLionel Sambuc // return type, the invoked method doesn't necessarily actually
4958f4a2713aSLionel Sambuc // return an object.
4959f4a2713aSLionel Sambuc if (!ReturnsRetained &&
4960f4a2713aSLionel Sambuc D && D->getMethodFamily() == OMF_performSelector)
4961*0a6a1f1dSLionel Sambuc return E;
4962f4a2713aSLionel Sambuc }
4963f4a2713aSLionel Sambuc
4964f4a2713aSLionel Sambuc // Don't reclaim an object of Class type.
4965f4a2713aSLionel Sambuc if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType())
4966*0a6a1f1dSLionel Sambuc return E;
4967f4a2713aSLionel Sambuc
4968f4a2713aSLionel Sambuc ExprNeedsCleanups = true;
4969f4a2713aSLionel Sambuc
4970f4a2713aSLionel Sambuc CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject
4971f4a2713aSLionel Sambuc : CK_ARCReclaimReturnedObject);
4972*0a6a1f1dSLionel Sambuc return ImplicitCastExpr::Create(Context, E->getType(), ck, E, nullptr,
4973*0a6a1f1dSLionel Sambuc VK_RValue);
4974f4a2713aSLionel Sambuc }
4975f4a2713aSLionel Sambuc
4976f4a2713aSLionel Sambuc if (!getLangOpts().CPlusPlus)
4977*0a6a1f1dSLionel Sambuc return E;
4978f4a2713aSLionel Sambuc
4979f4a2713aSLionel Sambuc // Search for the base element type (cf. ASTContext::getBaseElementType) with
4980f4a2713aSLionel Sambuc // a fast path for the common case that the type is directly a RecordType.
4981f4a2713aSLionel Sambuc const Type *T = Context.getCanonicalType(E->getType().getTypePtr());
4982*0a6a1f1dSLionel Sambuc const RecordType *RT = nullptr;
4983f4a2713aSLionel Sambuc while (!RT) {
4984f4a2713aSLionel Sambuc switch (T->getTypeClass()) {
4985f4a2713aSLionel Sambuc case Type::Record:
4986f4a2713aSLionel Sambuc RT = cast<RecordType>(T);
4987f4a2713aSLionel Sambuc break;
4988f4a2713aSLionel Sambuc case Type::ConstantArray:
4989f4a2713aSLionel Sambuc case Type::IncompleteArray:
4990f4a2713aSLionel Sambuc case Type::VariableArray:
4991f4a2713aSLionel Sambuc case Type::DependentSizedArray:
4992f4a2713aSLionel Sambuc T = cast<ArrayType>(T)->getElementType().getTypePtr();
4993f4a2713aSLionel Sambuc break;
4994f4a2713aSLionel Sambuc default:
4995*0a6a1f1dSLionel Sambuc return E;
4996f4a2713aSLionel Sambuc }
4997f4a2713aSLionel Sambuc }
4998f4a2713aSLionel Sambuc
4999f4a2713aSLionel Sambuc // That should be enough to guarantee that this type is complete, if we're
5000f4a2713aSLionel Sambuc // not processing a decltype expression.
5001f4a2713aSLionel Sambuc CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
5002f4a2713aSLionel Sambuc if (RD->isInvalidDecl() || RD->isDependentContext())
5003*0a6a1f1dSLionel Sambuc return E;
5004f4a2713aSLionel Sambuc
5005f4a2713aSLionel Sambuc bool IsDecltype = ExprEvalContexts.back().IsDecltype;
5006*0a6a1f1dSLionel Sambuc CXXDestructorDecl *Destructor = IsDecltype ? nullptr : LookupDestructor(RD);
5007f4a2713aSLionel Sambuc
5008f4a2713aSLionel Sambuc if (Destructor) {
5009f4a2713aSLionel Sambuc MarkFunctionReferenced(E->getExprLoc(), Destructor);
5010f4a2713aSLionel Sambuc CheckDestructorAccess(E->getExprLoc(), Destructor,
5011f4a2713aSLionel Sambuc PDiag(diag::err_access_dtor_temp)
5012f4a2713aSLionel Sambuc << E->getType());
5013f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(Destructor, E->getExprLoc()))
5014f4a2713aSLionel Sambuc return ExprError();
5015f4a2713aSLionel Sambuc
5016f4a2713aSLionel Sambuc // If destructor is trivial, we can avoid the extra copy.
5017f4a2713aSLionel Sambuc if (Destructor->isTrivial())
5018*0a6a1f1dSLionel Sambuc return E;
5019f4a2713aSLionel Sambuc
5020f4a2713aSLionel Sambuc // We need a cleanup, but we don't need to remember the temporary.
5021f4a2713aSLionel Sambuc ExprNeedsCleanups = true;
5022f4a2713aSLionel Sambuc }
5023f4a2713aSLionel Sambuc
5024f4a2713aSLionel Sambuc CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor);
5025f4a2713aSLionel Sambuc CXXBindTemporaryExpr *Bind = CXXBindTemporaryExpr::Create(Context, Temp, E);
5026f4a2713aSLionel Sambuc
5027f4a2713aSLionel Sambuc if (IsDecltype)
5028f4a2713aSLionel Sambuc ExprEvalContexts.back().DelayedDecltypeBinds.push_back(Bind);
5029f4a2713aSLionel Sambuc
5030*0a6a1f1dSLionel Sambuc return Bind;
5031f4a2713aSLionel Sambuc }
5032f4a2713aSLionel Sambuc
5033f4a2713aSLionel Sambuc ExprResult
MaybeCreateExprWithCleanups(ExprResult SubExpr)5034f4a2713aSLionel Sambuc Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) {
5035f4a2713aSLionel Sambuc if (SubExpr.isInvalid())
5036f4a2713aSLionel Sambuc return ExprError();
5037f4a2713aSLionel Sambuc
5038*0a6a1f1dSLionel Sambuc return MaybeCreateExprWithCleanups(SubExpr.get());
5039f4a2713aSLionel Sambuc }
5040f4a2713aSLionel Sambuc
MaybeCreateExprWithCleanups(Expr * SubExpr)5041f4a2713aSLionel Sambuc Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) {
5042f4a2713aSLionel Sambuc assert(SubExpr && "subexpression can't be null!");
5043f4a2713aSLionel Sambuc
5044f4a2713aSLionel Sambuc CleanupVarDeclMarking();
5045f4a2713aSLionel Sambuc
5046f4a2713aSLionel Sambuc unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects;
5047f4a2713aSLionel Sambuc assert(ExprCleanupObjects.size() >= FirstCleanup);
5048f4a2713aSLionel Sambuc assert(ExprNeedsCleanups || ExprCleanupObjects.size() == FirstCleanup);
5049f4a2713aSLionel Sambuc if (!ExprNeedsCleanups)
5050f4a2713aSLionel Sambuc return SubExpr;
5051f4a2713aSLionel Sambuc
5052*0a6a1f1dSLionel Sambuc auto Cleanups = llvm::makeArrayRef(ExprCleanupObjects.begin() + FirstCleanup,
5053f4a2713aSLionel Sambuc ExprCleanupObjects.size() - FirstCleanup);
5054f4a2713aSLionel Sambuc
5055f4a2713aSLionel Sambuc Expr *E = ExprWithCleanups::Create(Context, SubExpr, Cleanups);
5056f4a2713aSLionel Sambuc DiscardCleanupsInEvaluationContext();
5057f4a2713aSLionel Sambuc
5058f4a2713aSLionel Sambuc return E;
5059f4a2713aSLionel Sambuc }
5060f4a2713aSLionel Sambuc
MaybeCreateStmtWithCleanups(Stmt * SubStmt)5061f4a2713aSLionel Sambuc Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) {
5062*0a6a1f1dSLionel Sambuc assert(SubStmt && "sub-statement can't be null!");
5063f4a2713aSLionel Sambuc
5064f4a2713aSLionel Sambuc CleanupVarDeclMarking();
5065f4a2713aSLionel Sambuc
5066f4a2713aSLionel Sambuc if (!ExprNeedsCleanups)
5067f4a2713aSLionel Sambuc return SubStmt;
5068f4a2713aSLionel Sambuc
5069f4a2713aSLionel Sambuc // FIXME: In order to attach the temporaries, wrap the statement into
5070f4a2713aSLionel Sambuc // a StmtExpr; currently this is only used for asm statements.
5071f4a2713aSLionel Sambuc // This is hacky, either create a new CXXStmtWithTemporaries statement or
5072f4a2713aSLionel Sambuc // a new AsmStmtWithTemporaries.
5073f4a2713aSLionel Sambuc CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, SubStmt,
5074f4a2713aSLionel Sambuc SourceLocation(),
5075f4a2713aSLionel Sambuc SourceLocation());
5076f4a2713aSLionel Sambuc Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(),
5077f4a2713aSLionel Sambuc SourceLocation());
5078f4a2713aSLionel Sambuc return MaybeCreateExprWithCleanups(E);
5079f4a2713aSLionel Sambuc }
5080f4a2713aSLionel Sambuc
5081f4a2713aSLionel Sambuc /// Process the expression contained within a decltype. For such expressions,
5082f4a2713aSLionel Sambuc /// certain semantic checks on temporaries are delayed until this point, and
5083f4a2713aSLionel Sambuc /// are omitted for the 'topmost' call in the decltype expression. If the
5084f4a2713aSLionel Sambuc /// topmost call bound a temporary, strip that temporary off the expression.
ActOnDecltypeExpression(Expr * E)5085f4a2713aSLionel Sambuc ExprResult Sema::ActOnDecltypeExpression(Expr *E) {
5086f4a2713aSLionel Sambuc assert(ExprEvalContexts.back().IsDecltype && "not in a decltype expression");
5087f4a2713aSLionel Sambuc
5088f4a2713aSLionel Sambuc // C++11 [expr.call]p11:
5089f4a2713aSLionel Sambuc // If a function call is a prvalue of object type,
5090f4a2713aSLionel Sambuc // -- if the function call is either
5091f4a2713aSLionel Sambuc // -- the operand of a decltype-specifier, or
5092f4a2713aSLionel Sambuc // -- the right operand of a comma operator that is the operand of a
5093f4a2713aSLionel Sambuc // decltype-specifier,
5094f4a2713aSLionel Sambuc // a temporary object is not introduced for the prvalue.
5095f4a2713aSLionel Sambuc
5096f4a2713aSLionel Sambuc // Recursively rebuild ParenExprs and comma expressions to strip out the
5097f4a2713aSLionel Sambuc // outermost CXXBindTemporaryExpr, if any.
5098f4a2713aSLionel Sambuc if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
5099f4a2713aSLionel Sambuc ExprResult SubExpr = ActOnDecltypeExpression(PE->getSubExpr());
5100f4a2713aSLionel Sambuc if (SubExpr.isInvalid())
5101f4a2713aSLionel Sambuc return ExprError();
5102f4a2713aSLionel Sambuc if (SubExpr.get() == PE->getSubExpr())
5103*0a6a1f1dSLionel Sambuc return E;
5104*0a6a1f1dSLionel Sambuc return ActOnParenExpr(PE->getLParen(), PE->getRParen(), SubExpr.get());
5105f4a2713aSLionel Sambuc }
5106f4a2713aSLionel Sambuc if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
5107f4a2713aSLionel Sambuc if (BO->getOpcode() == BO_Comma) {
5108f4a2713aSLionel Sambuc ExprResult RHS = ActOnDecltypeExpression(BO->getRHS());
5109f4a2713aSLionel Sambuc if (RHS.isInvalid())
5110f4a2713aSLionel Sambuc return ExprError();
5111f4a2713aSLionel Sambuc if (RHS.get() == BO->getRHS())
5112*0a6a1f1dSLionel Sambuc return E;
5113*0a6a1f1dSLionel Sambuc return new (Context) BinaryOperator(
5114*0a6a1f1dSLionel Sambuc BO->getLHS(), RHS.get(), BO_Comma, BO->getType(), BO->getValueKind(),
5115*0a6a1f1dSLionel Sambuc BO->getObjectKind(), BO->getOperatorLoc(), BO->isFPContractable());
5116f4a2713aSLionel Sambuc }
5117f4a2713aSLionel Sambuc }
5118f4a2713aSLionel Sambuc
5119f4a2713aSLionel Sambuc CXXBindTemporaryExpr *TopBind = dyn_cast<CXXBindTemporaryExpr>(E);
5120*0a6a1f1dSLionel Sambuc CallExpr *TopCall = TopBind ? dyn_cast<CallExpr>(TopBind->getSubExpr())
5121*0a6a1f1dSLionel Sambuc : nullptr;
5122*0a6a1f1dSLionel Sambuc if (TopCall)
5123*0a6a1f1dSLionel Sambuc E = TopCall;
5124*0a6a1f1dSLionel Sambuc else
5125*0a6a1f1dSLionel Sambuc TopBind = nullptr;
5126f4a2713aSLionel Sambuc
5127f4a2713aSLionel Sambuc // Disable the special decltype handling now.
5128f4a2713aSLionel Sambuc ExprEvalContexts.back().IsDecltype = false;
5129f4a2713aSLionel Sambuc
5130f4a2713aSLionel Sambuc // In MS mode, don't perform any extra checking of call return types within a
5131f4a2713aSLionel Sambuc // decltype expression.
5132*0a6a1f1dSLionel Sambuc if (getLangOpts().MSVCCompat)
5133*0a6a1f1dSLionel Sambuc return E;
5134f4a2713aSLionel Sambuc
5135f4a2713aSLionel Sambuc // Perform the semantic checks we delayed until this point.
5136f4a2713aSLionel Sambuc for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeCalls.size();
5137f4a2713aSLionel Sambuc I != N; ++I) {
5138f4a2713aSLionel Sambuc CallExpr *Call = ExprEvalContexts.back().DelayedDecltypeCalls[I];
5139f4a2713aSLionel Sambuc if (Call == TopCall)
5140f4a2713aSLionel Sambuc continue;
5141f4a2713aSLionel Sambuc
5142f4a2713aSLionel Sambuc if (CheckCallReturnType(Call->getCallReturnType(),
5143f4a2713aSLionel Sambuc Call->getLocStart(),
5144f4a2713aSLionel Sambuc Call, Call->getDirectCallee()))
5145f4a2713aSLionel Sambuc return ExprError();
5146f4a2713aSLionel Sambuc }
5147f4a2713aSLionel Sambuc
5148f4a2713aSLionel Sambuc // Now all relevant types are complete, check the destructors are accessible
5149f4a2713aSLionel Sambuc // and non-deleted, and annotate them on the temporaries.
5150f4a2713aSLionel Sambuc for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeBinds.size();
5151f4a2713aSLionel Sambuc I != N; ++I) {
5152f4a2713aSLionel Sambuc CXXBindTemporaryExpr *Bind =
5153f4a2713aSLionel Sambuc ExprEvalContexts.back().DelayedDecltypeBinds[I];
5154f4a2713aSLionel Sambuc if (Bind == TopBind)
5155f4a2713aSLionel Sambuc continue;
5156f4a2713aSLionel Sambuc
5157f4a2713aSLionel Sambuc CXXTemporary *Temp = Bind->getTemporary();
5158f4a2713aSLionel Sambuc
5159f4a2713aSLionel Sambuc CXXRecordDecl *RD =
5160f4a2713aSLionel Sambuc Bind->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
5161f4a2713aSLionel Sambuc CXXDestructorDecl *Destructor = LookupDestructor(RD);
5162f4a2713aSLionel Sambuc Temp->setDestructor(Destructor);
5163f4a2713aSLionel Sambuc
5164f4a2713aSLionel Sambuc MarkFunctionReferenced(Bind->getExprLoc(), Destructor);
5165f4a2713aSLionel Sambuc CheckDestructorAccess(Bind->getExprLoc(), Destructor,
5166f4a2713aSLionel Sambuc PDiag(diag::err_access_dtor_temp)
5167f4a2713aSLionel Sambuc << Bind->getType());
5168f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(Destructor, Bind->getExprLoc()))
5169f4a2713aSLionel Sambuc return ExprError();
5170f4a2713aSLionel Sambuc
5171f4a2713aSLionel Sambuc // We need a cleanup, but we don't need to remember the temporary.
5172f4a2713aSLionel Sambuc ExprNeedsCleanups = true;
5173f4a2713aSLionel Sambuc }
5174f4a2713aSLionel Sambuc
5175f4a2713aSLionel Sambuc // Possibly strip off the top CXXBindTemporaryExpr.
5176*0a6a1f1dSLionel Sambuc return E;
5177f4a2713aSLionel Sambuc }
5178f4a2713aSLionel Sambuc
5179f4a2713aSLionel Sambuc /// Note a set of 'operator->' functions that were used for a member access.
noteOperatorArrows(Sema & S,ArrayRef<FunctionDecl * > OperatorArrows)5180f4a2713aSLionel Sambuc static void noteOperatorArrows(Sema &S,
5181*0a6a1f1dSLionel Sambuc ArrayRef<FunctionDecl *> OperatorArrows) {
5182f4a2713aSLionel Sambuc unsigned SkipStart = OperatorArrows.size(), SkipCount = 0;
5183f4a2713aSLionel Sambuc // FIXME: Make this configurable?
5184f4a2713aSLionel Sambuc unsigned Limit = 9;
5185f4a2713aSLionel Sambuc if (OperatorArrows.size() > Limit) {
5186f4a2713aSLionel Sambuc // Produce Limit-1 normal notes and one 'skipping' note.
5187f4a2713aSLionel Sambuc SkipStart = (Limit - 1) / 2 + (Limit - 1) % 2;
5188f4a2713aSLionel Sambuc SkipCount = OperatorArrows.size() - (Limit - 1);
5189f4a2713aSLionel Sambuc }
5190f4a2713aSLionel Sambuc
5191f4a2713aSLionel Sambuc for (unsigned I = 0; I < OperatorArrows.size(); /**/) {
5192f4a2713aSLionel Sambuc if (I == SkipStart) {
5193f4a2713aSLionel Sambuc S.Diag(OperatorArrows[I]->getLocation(),
5194f4a2713aSLionel Sambuc diag::note_operator_arrows_suppressed)
5195f4a2713aSLionel Sambuc << SkipCount;
5196f4a2713aSLionel Sambuc I += SkipCount;
5197f4a2713aSLionel Sambuc } else {
5198f4a2713aSLionel Sambuc S.Diag(OperatorArrows[I]->getLocation(), diag::note_operator_arrow_here)
5199f4a2713aSLionel Sambuc << OperatorArrows[I]->getCallResultType();
5200f4a2713aSLionel Sambuc ++I;
5201f4a2713aSLionel Sambuc }
5202f4a2713aSLionel Sambuc }
5203f4a2713aSLionel Sambuc }
5204f4a2713aSLionel Sambuc
5205f4a2713aSLionel Sambuc ExprResult
ActOnStartCXXMemberReference(Scope * S,Expr * Base,SourceLocation OpLoc,tok::TokenKind OpKind,ParsedType & ObjectType,bool & MayBePseudoDestructor)5206f4a2713aSLionel Sambuc Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, SourceLocation OpLoc,
5207f4a2713aSLionel Sambuc tok::TokenKind OpKind, ParsedType &ObjectType,
5208f4a2713aSLionel Sambuc bool &MayBePseudoDestructor) {
5209f4a2713aSLionel Sambuc // Since this might be a postfix expression, get rid of ParenListExprs.
5210f4a2713aSLionel Sambuc ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
5211f4a2713aSLionel Sambuc if (Result.isInvalid()) return ExprError();
5212f4a2713aSLionel Sambuc Base = Result.get();
5213f4a2713aSLionel Sambuc
5214f4a2713aSLionel Sambuc Result = CheckPlaceholderExpr(Base);
5215f4a2713aSLionel Sambuc if (Result.isInvalid()) return ExprError();
5216*0a6a1f1dSLionel Sambuc Base = Result.get();
5217f4a2713aSLionel Sambuc
5218f4a2713aSLionel Sambuc QualType BaseType = Base->getType();
5219f4a2713aSLionel Sambuc MayBePseudoDestructor = false;
5220f4a2713aSLionel Sambuc if (BaseType->isDependentType()) {
5221f4a2713aSLionel Sambuc // If we have a pointer to a dependent type and are using the -> operator,
5222f4a2713aSLionel Sambuc // the object type is the type that the pointer points to. We might still
5223f4a2713aSLionel Sambuc // have enough information about that type to do something useful.
5224f4a2713aSLionel Sambuc if (OpKind == tok::arrow)
5225f4a2713aSLionel Sambuc if (const PointerType *Ptr = BaseType->getAs<PointerType>())
5226f4a2713aSLionel Sambuc BaseType = Ptr->getPointeeType();
5227f4a2713aSLionel Sambuc
5228f4a2713aSLionel Sambuc ObjectType = ParsedType::make(BaseType);
5229f4a2713aSLionel Sambuc MayBePseudoDestructor = true;
5230*0a6a1f1dSLionel Sambuc return Base;
5231f4a2713aSLionel Sambuc }
5232f4a2713aSLionel Sambuc
5233f4a2713aSLionel Sambuc // C++ [over.match.oper]p8:
5234f4a2713aSLionel Sambuc // [...] When operator->returns, the operator-> is applied to the value
5235f4a2713aSLionel Sambuc // returned, with the original second operand.
5236f4a2713aSLionel Sambuc if (OpKind == tok::arrow) {
5237f4a2713aSLionel Sambuc QualType StartingType = BaseType;
5238f4a2713aSLionel Sambuc bool NoArrowOperatorFound = false;
5239f4a2713aSLionel Sambuc bool FirstIteration = true;
5240f4a2713aSLionel Sambuc FunctionDecl *CurFD = dyn_cast<FunctionDecl>(CurContext);
5241f4a2713aSLionel Sambuc // The set of types we've considered so far.
5242f4a2713aSLionel Sambuc llvm::SmallPtrSet<CanQualType,8> CTypes;
5243f4a2713aSLionel Sambuc SmallVector<FunctionDecl*, 8> OperatorArrows;
5244f4a2713aSLionel Sambuc CTypes.insert(Context.getCanonicalType(BaseType));
5245f4a2713aSLionel Sambuc
5246f4a2713aSLionel Sambuc while (BaseType->isRecordType()) {
5247f4a2713aSLionel Sambuc if (OperatorArrows.size() >= getLangOpts().ArrowDepth) {
5248f4a2713aSLionel Sambuc Diag(OpLoc, diag::err_operator_arrow_depth_exceeded)
5249f4a2713aSLionel Sambuc << StartingType << getLangOpts().ArrowDepth << Base->getSourceRange();
5250f4a2713aSLionel Sambuc noteOperatorArrows(*this, OperatorArrows);
5251f4a2713aSLionel Sambuc Diag(OpLoc, diag::note_operator_arrow_depth)
5252f4a2713aSLionel Sambuc << getLangOpts().ArrowDepth;
5253f4a2713aSLionel Sambuc return ExprError();
5254f4a2713aSLionel Sambuc }
5255f4a2713aSLionel Sambuc
5256f4a2713aSLionel Sambuc Result = BuildOverloadedArrowExpr(
5257f4a2713aSLionel Sambuc S, Base, OpLoc,
5258f4a2713aSLionel Sambuc // When in a template specialization and on the first loop iteration,
5259f4a2713aSLionel Sambuc // potentially give the default diagnostic (with the fixit in a
5260f4a2713aSLionel Sambuc // separate note) instead of having the error reported back to here
5261f4a2713aSLionel Sambuc // and giving a diagnostic with a fixit attached to the error itself.
5262f4a2713aSLionel Sambuc (FirstIteration && CurFD && CurFD->isFunctionTemplateSpecialization())
5263*0a6a1f1dSLionel Sambuc ? nullptr
5264f4a2713aSLionel Sambuc : &NoArrowOperatorFound);
5265f4a2713aSLionel Sambuc if (Result.isInvalid()) {
5266f4a2713aSLionel Sambuc if (NoArrowOperatorFound) {
5267f4a2713aSLionel Sambuc if (FirstIteration) {
5268f4a2713aSLionel Sambuc Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
5269f4a2713aSLionel Sambuc << BaseType << 1 << Base->getSourceRange()
5270f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(OpLoc, ".");
5271f4a2713aSLionel Sambuc OpKind = tok::period;
5272f4a2713aSLionel Sambuc break;
5273f4a2713aSLionel Sambuc }
5274f4a2713aSLionel Sambuc Diag(OpLoc, diag::err_typecheck_member_reference_arrow)
5275f4a2713aSLionel Sambuc << BaseType << Base->getSourceRange();
5276f4a2713aSLionel Sambuc CallExpr *CE = dyn_cast<CallExpr>(Base);
5277*0a6a1f1dSLionel Sambuc if (Decl *CD = (CE ? CE->getCalleeDecl() : nullptr)) {
5278f4a2713aSLionel Sambuc Diag(CD->getLocStart(),
5279f4a2713aSLionel Sambuc diag::note_member_reference_arrow_from_operator_arrow);
5280f4a2713aSLionel Sambuc }
5281f4a2713aSLionel Sambuc }
5282f4a2713aSLionel Sambuc return ExprError();
5283f4a2713aSLionel Sambuc }
5284f4a2713aSLionel Sambuc Base = Result.get();
5285f4a2713aSLionel Sambuc if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base))
5286f4a2713aSLionel Sambuc OperatorArrows.push_back(OpCall->getDirectCallee());
5287f4a2713aSLionel Sambuc BaseType = Base->getType();
5288f4a2713aSLionel Sambuc CanQualType CBaseType = Context.getCanonicalType(BaseType);
5289*0a6a1f1dSLionel Sambuc if (!CTypes.insert(CBaseType).second) {
5290f4a2713aSLionel Sambuc Diag(OpLoc, diag::err_operator_arrow_circular) << StartingType;
5291f4a2713aSLionel Sambuc noteOperatorArrows(*this, OperatorArrows);
5292f4a2713aSLionel Sambuc return ExprError();
5293f4a2713aSLionel Sambuc }
5294f4a2713aSLionel Sambuc FirstIteration = false;
5295f4a2713aSLionel Sambuc }
5296f4a2713aSLionel Sambuc
5297f4a2713aSLionel Sambuc if (OpKind == tok::arrow &&
5298f4a2713aSLionel Sambuc (BaseType->isPointerType() || BaseType->isObjCObjectPointerType()))
5299f4a2713aSLionel Sambuc BaseType = BaseType->getPointeeType();
5300f4a2713aSLionel Sambuc }
5301f4a2713aSLionel Sambuc
5302f4a2713aSLionel Sambuc // Objective-C properties allow "." access on Objective-C pointer types,
5303f4a2713aSLionel Sambuc // so adjust the base type to the object type itself.
5304f4a2713aSLionel Sambuc if (BaseType->isObjCObjectPointerType())
5305f4a2713aSLionel Sambuc BaseType = BaseType->getPointeeType();
5306f4a2713aSLionel Sambuc
5307f4a2713aSLionel Sambuc // C++ [basic.lookup.classref]p2:
5308f4a2713aSLionel Sambuc // [...] If the type of the object expression is of pointer to scalar
5309f4a2713aSLionel Sambuc // type, the unqualified-id is looked up in the context of the complete
5310f4a2713aSLionel Sambuc // postfix-expression.
5311f4a2713aSLionel Sambuc //
5312f4a2713aSLionel Sambuc // This also indicates that we could be parsing a pseudo-destructor-name.
5313f4a2713aSLionel Sambuc // Note that Objective-C class and object types can be pseudo-destructor
5314f4a2713aSLionel Sambuc // expressions or normal member (ivar or property) access expressions.
5315f4a2713aSLionel Sambuc if (BaseType->isObjCObjectOrInterfaceType()) {
5316f4a2713aSLionel Sambuc MayBePseudoDestructor = true;
5317f4a2713aSLionel Sambuc } else if (!BaseType->isRecordType()) {
5318f4a2713aSLionel Sambuc ObjectType = ParsedType();
5319f4a2713aSLionel Sambuc MayBePseudoDestructor = true;
5320*0a6a1f1dSLionel Sambuc return Base;
5321f4a2713aSLionel Sambuc }
5322f4a2713aSLionel Sambuc
5323f4a2713aSLionel Sambuc // The object type must be complete (or dependent), or
5324f4a2713aSLionel Sambuc // C++11 [expr.prim.general]p3:
5325f4a2713aSLionel Sambuc // Unlike the object expression in other contexts, *this is not required to
5326f4a2713aSLionel Sambuc // be of complete type for purposes of class member access (5.2.5) outside
5327f4a2713aSLionel Sambuc // the member function body.
5328f4a2713aSLionel Sambuc if (!BaseType->isDependentType() &&
5329f4a2713aSLionel Sambuc !isThisOutsideMemberFunctionBody(BaseType) &&
5330f4a2713aSLionel Sambuc RequireCompleteType(OpLoc, BaseType, diag::err_incomplete_member_access))
5331f4a2713aSLionel Sambuc return ExprError();
5332f4a2713aSLionel Sambuc
5333f4a2713aSLionel Sambuc // C++ [basic.lookup.classref]p2:
5334f4a2713aSLionel Sambuc // If the id-expression in a class member access (5.2.5) is an
5335f4a2713aSLionel Sambuc // unqualified-id, and the type of the object expression is of a class
5336f4a2713aSLionel Sambuc // type C (or of pointer to a class type C), the unqualified-id is looked
5337f4a2713aSLionel Sambuc // up in the scope of class C. [...]
5338f4a2713aSLionel Sambuc ObjectType = ParsedType::make(BaseType);
5339f4a2713aSLionel Sambuc return Base;
5340f4a2713aSLionel Sambuc }
5341f4a2713aSLionel Sambuc
DiagnoseDtorReference(SourceLocation NameLoc,Expr * MemExpr)5342f4a2713aSLionel Sambuc ExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc,
5343f4a2713aSLionel Sambuc Expr *MemExpr) {
5344f4a2713aSLionel Sambuc SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc);
5345f4a2713aSLionel Sambuc Diag(MemExpr->getLocStart(), diag::err_dtor_expr_without_call)
5346f4a2713aSLionel Sambuc << isa<CXXPseudoDestructorExpr>(MemExpr)
5347f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(ExpectedLParenLoc, "()");
5348f4a2713aSLionel Sambuc
5349*0a6a1f1dSLionel Sambuc return ActOnCallExpr(/*Scope*/ nullptr,
5350f4a2713aSLionel Sambuc MemExpr,
5351f4a2713aSLionel Sambuc /*LPLoc*/ ExpectedLParenLoc,
5352f4a2713aSLionel Sambuc None,
5353f4a2713aSLionel Sambuc /*RPLoc*/ ExpectedLParenLoc);
5354f4a2713aSLionel Sambuc }
5355f4a2713aSLionel Sambuc
CheckArrow(Sema & S,QualType & ObjectType,Expr * & Base,tok::TokenKind & OpKind,SourceLocation OpLoc)5356f4a2713aSLionel Sambuc static bool CheckArrow(Sema& S, QualType& ObjectType, Expr *&Base,
5357f4a2713aSLionel Sambuc tok::TokenKind& OpKind, SourceLocation OpLoc) {
5358f4a2713aSLionel Sambuc if (Base->hasPlaceholderType()) {
5359f4a2713aSLionel Sambuc ExprResult result = S.CheckPlaceholderExpr(Base);
5360f4a2713aSLionel Sambuc if (result.isInvalid()) return true;
5361*0a6a1f1dSLionel Sambuc Base = result.get();
5362f4a2713aSLionel Sambuc }
5363f4a2713aSLionel Sambuc ObjectType = Base->getType();
5364f4a2713aSLionel Sambuc
5365f4a2713aSLionel Sambuc // C++ [expr.pseudo]p2:
5366f4a2713aSLionel Sambuc // The left-hand side of the dot operator shall be of scalar type. The
5367f4a2713aSLionel Sambuc // left-hand side of the arrow operator shall be of pointer to scalar type.
5368f4a2713aSLionel Sambuc // This scalar type is the object type.
5369f4a2713aSLionel Sambuc // Note that this is rather different from the normal handling for the
5370f4a2713aSLionel Sambuc // arrow operator.
5371f4a2713aSLionel Sambuc if (OpKind == tok::arrow) {
5372f4a2713aSLionel Sambuc if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) {
5373f4a2713aSLionel Sambuc ObjectType = Ptr->getPointeeType();
5374f4a2713aSLionel Sambuc } else if (!Base->isTypeDependent()) {
5375f4a2713aSLionel Sambuc // The user wrote "p->" when she probably meant "p."; fix it.
5376f4a2713aSLionel Sambuc S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
5377f4a2713aSLionel Sambuc << ObjectType << true
5378f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(OpLoc, ".");
5379f4a2713aSLionel Sambuc if (S.isSFINAEContext())
5380f4a2713aSLionel Sambuc return true;
5381f4a2713aSLionel Sambuc
5382f4a2713aSLionel Sambuc OpKind = tok::period;
5383f4a2713aSLionel Sambuc }
5384f4a2713aSLionel Sambuc }
5385f4a2713aSLionel Sambuc
5386f4a2713aSLionel Sambuc return false;
5387f4a2713aSLionel Sambuc }
5388f4a2713aSLionel Sambuc
BuildPseudoDestructorExpr(Expr * Base,SourceLocation OpLoc,tok::TokenKind OpKind,const CXXScopeSpec & SS,TypeSourceInfo * ScopeTypeInfo,SourceLocation CCLoc,SourceLocation TildeLoc,PseudoDestructorTypeStorage Destructed,bool HasTrailingLParen)5389f4a2713aSLionel Sambuc ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base,
5390f4a2713aSLionel Sambuc SourceLocation OpLoc,
5391f4a2713aSLionel Sambuc tok::TokenKind OpKind,
5392f4a2713aSLionel Sambuc const CXXScopeSpec &SS,
5393f4a2713aSLionel Sambuc TypeSourceInfo *ScopeTypeInfo,
5394f4a2713aSLionel Sambuc SourceLocation CCLoc,
5395f4a2713aSLionel Sambuc SourceLocation TildeLoc,
5396f4a2713aSLionel Sambuc PseudoDestructorTypeStorage Destructed,
5397f4a2713aSLionel Sambuc bool HasTrailingLParen) {
5398f4a2713aSLionel Sambuc TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo();
5399f4a2713aSLionel Sambuc
5400f4a2713aSLionel Sambuc QualType ObjectType;
5401f4a2713aSLionel Sambuc if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc))
5402f4a2713aSLionel Sambuc return ExprError();
5403f4a2713aSLionel Sambuc
5404f4a2713aSLionel Sambuc if (!ObjectType->isDependentType() && !ObjectType->isScalarType() &&
5405f4a2713aSLionel Sambuc !ObjectType->isVectorType()) {
5406*0a6a1f1dSLionel Sambuc if (getLangOpts().MSVCCompat && ObjectType->isVoidType())
5407f4a2713aSLionel Sambuc Diag(OpLoc, diag::ext_pseudo_dtor_on_void) << Base->getSourceRange();
5408*0a6a1f1dSLionel Sambuc else {
5409f4a2713aSLionel Sambuc Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar)
5410f4a2713aSLionel Sambuc << ObjectType << Base->getSourceRange();
5411f4a2713aSLionel Sambuc return ExprError();
5412f4a2713aSLionel Sambuc }
5413*0a6a1f1dSLionel Sambuc }
5414f4a2713aSLionel Sambuc
5415f4a2713aSLionel Sambuc // C++ [expr.pseudo]p2:
5416f4a2713aSLionel Sambuc // [...] The cv-unqualified versions of the object type and of the type
5417f4a2713aSLionel Sambuc // designated by the pseudo-destructor-name shall be the same type.
5418f4a2713aSLionel Sambuc if (DestructedTypeInfo) {
5419f4a2713aSLionel Sambuc QualType DestructedType = DestructedTypeInfo->getType();
5420f4a2713aSLionel Sambuc SourceLocation DestructedTypeStart
5421f4a2713aSLionel Sambuc = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin();
5422f4a2713aSLionel Sambuc if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) {
5423f4a2713aSLionel Sambuc if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) {
5424f4a2713aSLionel Sambuc Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch)
5425f4a2713aSLionel Sambuc << ObjectType << DestructedType << Base->getSourceRange()
5426f4a2713aSLionel Sambuc << DestructedTypeInfo->getTypeLoc().getLocalSourceRange();
5427f4a2713aSLionel Sambuc
5428f4a2713aSLionel Sambuc // Recover by setting the destructed type to the object type.
5429f4a2713aSLionel Sambuc DestructedType = ObjectType;
5430f4a2713aSLionel Sambuc DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType,
5431f4a2713aSLionel Sambuc DestructedTypeStart);
5432f4a2713aSLionel Sambuc Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo);
5433f4a2713aSLionel Sambuc } else if (DestructedType.getObjCLifetime() !=
5434f4a2713aSLionel Sambuc ObjectType.getObjCLifetime()) {
5435f4a2713aSLionel Sambuc
5436f4a2713aSLionel Sambuc if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) {
5437f4a2713aSLionel Sambuc // Okay: just pretend that the user provided the correctly-qualified
5438f4a2713aSLionel Sambuc // type.
5439f4a2713aSLionel Sambuc } else {
5440f4a2713aSLionel Sambuc Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals)
5441f4a2713aSLionel Sambuc << ObjectType << DestructedType << Base->getSourceRange()
5442f4a2713aSLionel Sambuc << DestructedTypeInfo->getTypeLoc().getLocalSourceRange();
5443f4a2713aSLionel Sambuc }
5444f4a2713aSLionel Sambuc
5445f4a2713aSLionel Sambuc // Recover by setting the destructed type to the object type.
5446f4a2713aSLionel Sambuc DestructedType = ObjectType;
5447f4a2713aSLionel Sambuc DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType,
5448f4a2713aSLionel Sambuc DestructedTypeStart);
5449f4a2713aSLionel Sambuc Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo);
5450f4a2713aSLionel Sambuc }
5451f4a2713aSLionel Sambuc }
5452f4a2713aSLionel Sambuc }
5453f4a2713aSLionel Sambuc
5454f4a2713aSLionel Sambuc // C++ [expr.pseudo]p2:
5455f4a2713aSLionel Sambuc // [...] Furthermore, the two type-names in a pseudo-destructor-name of the
5456f4a2713aSLionel Sambuc // form
5457f4a2713aSLionel Sambuc //
5458f4a2713aSLionel Sambuc // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name
5459f4a2713aSLionel Sambuc //
5460f4a2713aSLionel Sambuc // shall designate the same scalar type.
5461f4a2713aSLionel Sambuc if (ScopeTypeInfo) {
5462f4a2713aSLionel Sambuc QualType ScopeType = ScopeTypeInfo->getType();
5463f4a2713aSLionel Sambuc if (!ScopeType->isDependentType() && !ObjectType->isDependentType() &&
5464f4a2713aSLionel Sambuc !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) {
5465f4a2713aSLionel Sambuc
5466f4a2713aSLionel Sambuc Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(),
5467f4a2713aSLionel Sambuc diag::err_pseudo_dtor_type_mismatch)
5468f4a2713aSLionel Sambuc << ObjectType << ScopeType << Base->getSourceRange()
5469f4a2713aSLionel Sambuc << ScopeTypeInfo->getTypeLoc().getLocalSourceRange();
5470f4a2713aSLionel Sambuc
5471f4a2713aSLionel Sambuc ScopeType = QualType();
5472*0a6a1f1dSLionel Sambuc ScopeTypeInfo = nullptr;
5473f4a2713aSLionel Sambuc }
5474f4a2713aSLionel Sambuc }
5475f4a2713aSLionel Sambuc
5476f4a2713aSLionel Sambuc Expr *Result
5477f4a2713aSLionel Sambuc = new (Context) CXXPseudoDestructorExpr(Context, Base,
5478f4a2713aSLionel Sambuc OpKind == tok::arrow, OpLoc,
5479f4a2713aSLionel Sambuc SS.getWithLocInContext(Context),
5480f4a2713aSLionel Sambuc ScopeTypeInfo,
5481f4a2713aSLionel Sambuc CCLoc,
5482f4a2713aSLionel Sambuc TildeLoc,
5483f4a2713aSLionel Sambuc Destructed);
5484f4a2713aSLionel Sambuc
5485f4a2713aSLionel Sambuc if (HasTrailingLParen)
5486*0a6a1f1dSLionel Sambuc return Result;
5487f4a2713aSLionel Sambuc
5488f4a2713aSLionel Sambuc return DiagnoseDtorReference(Destructed.getLocation(), Result);
5489f4a2713aSLionel Sambuc }
5490f4a2713aSLionel Sambuc
ActOnPseudoDestructorExpr(Scope * S,Expr * Base,SourceLocation OpLoc,tok::TokenKind OpKind,CXXScopeSpec & SS,UnqualifiedId & FirstTypeName,SourceLocation CCLoc,SourceLocation TildeLoc,UnqualifiedId & SecondTypeName,bool HasTrailingLParen)5491f4a2713aSLionel Sambuc ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base,
5492f4a2713aSLionel Sambuc SourceLocation OpLoc,
5493f4a2713aSLionel Sambuc tok::TokenKind OpKind,
5494f4a2713aSLionel Sambuc CXXScopeSpec &SS,
5495f4a2713aSLionel Sambuc UnqualifiedId &FirstTypeName,
5496f4a2713aSLionel Sambuc SourceLocation CCLoc,
5497f4a2713aSLionel Sambuc SourceLocation TildeLoc,
5498f4a2713aSLionel Sambuc UnqualifiedId &SecondTypeName,
5499f4a2713aSLionel Sambuc bool HasTrailingLParen) {
5500f4a2713aSLionel Sambuc assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId ||
5501f4a2713aSLionel Sambuc FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) &&
5502f4a2713aSLionel Sambuc "Invalid first type name in pseudo-destructor");
5503f4a2713aSLionel Sambuc assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId ||
5504f4a2713aSLionel Sambuc SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) &&
5505f4a2713aSLionel Sambuc "Invalid second type name in pseudo-destructor");
5506f4a2713aSLionel Sambuc
5507f4a2713aSLionel Sambuc QualType ObjectType;
5508f4a2713aSLionel Sambuc if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc))
5509f4a2713aSLionel Sambuc return ExprError();
5510f4a2713aSLionel Sambuc
5511f4a2713aSLionel Sambuc // Compute the object type that we should use for name lookup purposes. Only
5512f4a2713aSLionel Sambuc // record types and dependent types matter.
5513f4a2713aSLionel Sambuc ParsedType ObjectTypePtrForLookup;
5514f4a2713aSLionel Sambuc if (!SS.isSet()) {
5515f4a2713aSLionel Sambuc if (ObjectType->isRecordType())
5516f4a2713aSLionel Sambuc ObjectTypePtrForLookup = ParsedType::make(ObjectType);
5517f4a2713aSLionel Sambuc else if (ObjectType->isDependentType())
5518f4a2713aSLionel Sambuc ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy);
5519f4a2713aSLionel Sambuc }
5520f4a2713aSLionel Sambuc
5521f4a2713aSLionel Sambuc // Convert the name of the type being destructed (following the ~) into a
5522f4a2713aSLionel Sambuc // type (with source-location information).
5523f4a2713aSLionel Sambuc QualType DestructedType;
5524*0a6a1f1dSLionel Sambuc TypeSourceInfo *DestructedTypeInfo = nullptr;
5525f4a2713aSLionel Sambuc PseudoDestructorTypeStorage Destructed;
5526f4a2713aSLionel Sambuc if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) {
5527f4a2713aSLionel Sambuc ParsedType T = getTypeName(*SecondTypeName.Identifier,
5528f4a2713aSLionel Sambuc SecondTypeName.StartLocation,
5529f4a2713aSLionel Sambuc S, &SS, true, false, ObjectTypePtrForLookup);
5530f4a2713aSLionel Sambuc if (!T &&
5531f4a2713aSLionel Sambuc ((SS.isSet() && !computeDeclContext(SS, false)) ||
5532f4a2713aSLionel Sambuc (!SS.isSet() && ObjectType->isDependentType()))) {
5533f4a2713aSLionel Sambuc // The name of the type being destroyed is a dependent name, and we
5534f4a2713aSLionel Sambuc // couldn't find anything useful in scope. Just store the identifier and
5535f4a2713aSLionel Sambuc // it's location, and we'll perform (qualified) name lookup again at
5536f4a2713aSLionel Sambuc // template instantiation time.
5537f4a2713aSLionel Sambuc Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier,
5538f4a2713aSLionel Sambuc SecondTypeName.StartLocation);
5539f4a2713aSLionel Sambuc } else if (!T) {
5540f4a2713aSLionel Sambuc Diag(SecondTypeName.StartLocation,
5541f4a2713aSLionel Sambuc diag::err_pseudo_dtor_destructor_non_type)
5542f4a2713aSLionel Sambuc << SecondTypeName.Identifier << ObjectType;
5543f4a2713aSLionel Sambuc if (isSFINAEContext())
5544f4a2713aSLionel Sambuc return ExprError();
5545f4a2713aSLionel Sambuc
5546f4a2713aSLionel Sambuc // Recover by assuming we had the right type all along.
5547f4a2713aSLionel Sambuc DestructedType = ObjectType;
5548f4a2713aSLionel Sambuc } else
5549f4a2713aSLionel Sambuc DestructedType = GetTypeFromParser(T, &DestructedTypeInfo);
5550f4a2713aSLionel Sambuc } else {
5551f4a2713aSLionel Sambuc // Resolve the template-id to a type.
5552f4a2713aSLionel Sambuc TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId;
5553f4a2713aSLionel Sambuc ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
5554f4a2713aSLionel Sambuc TemplateId->NumArgs);
5555f4a2713aSLionel Sambuc TypeResult T = ActOnTemplateIdType(TemplateId->SS,
5556f4a2713aSLionel Sambuc TemplateId->TemplateKWLoc,
5557f4a2713aSLionel Sambuc TemplateId->Template,
5558f4a2713aSLionel Sambuc TemplateId->TemplateNameLoc,
5559f4a2713aSLionel Sambuc TemplateId->LAngleLoc,
5560f4a2713aSLionel Sambuc TemplateArgsPtr,
5561f4a2713aSLionel Sambuc TemplateId->RAngleLoc);
5562f4a2713aSLionel Sambuc if (T.isInvalid() || !T.get()) {
5563f4a2713aSLionel Sambuc // Recover by assuming we had the right type all along.
5564f4a2713aSLionel Sambuc DestructedType = ObjectType;
5565f4a2713aSLionel Sambuc } else
5566f4a2713aSLionel Sambuc DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo);
5567f4a2713aSLionel Sambuc }
5568f4a2713aSLionel Sambuc
5569f4a2713aSLionel Sambuc // If we've performed some kind of recovery, (re-)build the type source
5570f4a2713aSLionel Sambuc // information.
5571f4a2713aSLionel Sambuc if (!DestructedType.isNull()) {
5572f4a2713aSLionel Sambuc if (!DestructedTypeInfo)
5573f4a2713aSLionel Sambuc DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType,
5574f4a2713aSLionel Sambuc SecondTypeName.StartLocation);
5575f4a2713aSLionel Sambuc Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo);
5576f4a2713aSLionel Sambuc }
5577f4a2713aSLionel Sambuc
5578f4a2713aSLionel Sambuc // Convert the name of the scope type (the type prior to '::') into a type.
5579*0a6a1f1dSLionel Sambuc TypeSourceInfo *ScopeTypeInfo = nullptr;
5580f4a2713aSLionel Sambuc QualType ScopeType;
5581f4a2713aSLionel Sambuc if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId ||
5582f4a2713aSLionel Sambuc FirstTypeName.Identifier) {
5583f4a2713aSLionel Sambuc if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) {
5584f4a2713aSLionel Sambuc ParsedType T = getTypeName(*FirstTypeName.Identifier,
5585f4a2713aSLionel Sambuc FirstTypeName.StartLocation,
5586f4a2713aSLionel Sambuc S, &SS, true, false, ObjectTypePtrForLookup);
5587f4a2713aSLionel Sambuc if (!T) {
5588f4a2713aSLionel Sambuc Diag(FirstTypeName.StartLocation,
5589f4a2713aSLionel Sambuc diag::err_pseudo_dtor_destructor_non_type)
5590f4a2713aSLionel Sambuc << FirstTypeName.Identifier << ObjectType;
5591f4a2713aSLionel Sambuc
5592f4a2713aSLionel Sambuc if (isSFINAEContext())
5593f4a2713aSLionel Sambuc return ExprError();
5594f4a2713aSLionel Sambuc
5595f4a2713aSLionel Sambuc // Just drop this type. It's unnecessary anyway.
5596f4a2713aSLionel Sambuc ScopeType = QualType();
5597f4a2713aSLionel Sambuc } else
5598f4a2713aSLionel Sambuc ScopeType = GetTypeFromParser(T, &ScopeTypeInfo);
5599f4a2713aSLionel Sambuc } else {
5600f4a2713aSLionel Sambuc // Resolve the template-id to a type.
5601f4a2713aSLionel Sambuc TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId;
5602f4a2713aSLionel Sambuc ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
5603f4a2713aSLionel Sambuc TemplateId->NumArgs);
5604f4a2713aSLionel Sambuc TypeResult T = ActOnTemplateIdType(TemplateId->SS,
5605f4a2713aSLionel Sambuc TemplateId->TemplateKWLoc,
5606f4a2713aSLionel Sambuc TemplateId->Template,
5607f4a2713aSLionel Sambuc TemplateId->TemplateNameLoc,
5608f4a2713aSLionel Sambuc TemplateId->LAngleLoc,
5609f4a2713aSLionel Sambuc TemplateArgsPtr,
5610f4a2713aSLionel Sambuc TemplateId->RAngleLoc);
5611f4a2713aSLionel Sambuc if (T.isInvalid() || !T.get()) {
5612f4a2713aSLionel Sambuc // Recover by dropping this type.
5613f4a2713aSLionel Sambuc ScopeType = QualType();
5614f4a2713aSLionel Sambuc } else
5615f4a2713aSLionel Sambuc ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo);
5616f4a2713aSLionel Sambuc }
5617f4a2713aSLionel Sambuc }
5618f4a2713aSLionel Sambuc
5619f4a2713aSLionel Sambuc if (!ScopeType.isNull() && !ScopeTypeInfo)
5620f4a2713aSLionel Sambuc ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType,
5621f4a2713aSLionel Sambuc FirstTypeName.StartLocation);
5622f4a2713aSLionel Sambuc
5623f4a2713aSLionel Sambuc
5624f4a2713aSLionel Sambuc return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS,
5625f4a2713aSLionel Sambuc ScopeTypeInfo, CCLoc, TildeLoc,
5626f4a2713aSLionel Sambuc Destructed, HasTrailingLParen);
5627f4a2713aSLionel Sambuc }
5628f4a2713aSLionel Sambuc
ActOnPseudoDestructorExpr(Scope * S,Expr * Base,SourceLocation OpLoc,tok::TokenKind OpKind,SourceLocation TildeLoc,const DeclSpec & DS,bool HasTrailingLParen)5629f4a2713aSLionel Sambuc ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base,
5630f4a2713aSLionel Sambuc SourceLocation OpLoc,
5631f4a2713aSLionel Sambuc tok::TokenKind OpKind,
5632f4a2713aSLionel Sambuc SourceLocation TildeLoc,
5633f4a2713aSLionel Sambuc const DeclSpec& DS,
5634f4a2713aSLionel Sambuc bool HasTrailingLParen) {
5635f4a2713aSLionel Sambuc QualType ObjectType;
5636f4a2713aSLionel Sambuc if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc))
5637f4a2713aSLionel Sambuc return ExprError();
5638f4a2713aSLionel Sambuc
5639*0a6a1f1dSLionel Sambuc QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc(),
5640*0a6a1f1dSLionel Sambuc false);
5641f4a2713aSLionel Sambuc
5642f4a2713aSLionel Sambuc TypeLocBuilder TLB;
5643f4a2713aSLionel Sambuc DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
5644f4a2713aSLionel Sambuc DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
5645f4a2713aSLionel Sambuc TypeSourceInfo *DestructedTypeInfo = TLB.getTypeSourceInfo(Context, T);
5646f4a2713aSLionel Sambuc PseudoDestructorTypeStorage Destructed(DestructedTypeInfo);
5647f4a2713aSLionel Sambuc
5648f4a2713aSLionel Sambuc return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, CXXScopeSpec(),
5649*0a6a1f1dSLionel Sambuc nullptr, SourceLocation(), TildeLoc,
5650f4a2713aSLionel Sambuc Destructed, HasTrailingLParen);
5651f4a2713aSLionel Sambuc }
5652f4a2713aSLionel Sambuc
BuildCXXMemberCallExpr(Expr * E,NamedDecl * FoundDecl,CXXConversionDecl * Method,bool HadMultipleCandidates)5653f4a2713aSLionel Sambuc ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl,
5654f4a2713aSLionel Sambuc CXXConversionDecl *Method,
5655f4a2713aSLionel Sambuc bool HadMultipleCandidates) {
5656f4a2713aSLionel Sambuc if (Method->getParent()->isLambda() &&
5657f4a2713aSLionel Sambuc Method->getConversionType()->isBlockPointerType()) {
5658f4a2713aSLionel Sambuc // This is a lambda coversion to block pointer; check if the argument
5659f4a2713aSLionel Sambuc // is a LambdaExpr.
5660f4a2713aSLionel Sambuc Expr *SubE = E;
5661f4a2713aSLionel Sambuc CastExpr *CE = dyn_cast<CastExpr>(SubE);
5662f4a2713aSLionel Sambuc if (CE && CE->getCastKind() == CK_NoOp)
5663f4a2713aSLionel Sambuc SubE = CE->getSubExpr();
5664f4a2713aSLionel Sambuc SubE = SubE->IgnoreParens();
5665f4a2713aSLionel Sambuc if (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(SubE))
5666f4a2713aSLionel Sambuc SubE = BE->getSubExpr();
5667f4a2713aSLionel Sambuc if (isa<LambdaExpr>(SubE)) {
5668f4a2713aSLionel Sambuc // For the conversion to block pointer on a lambda expression, we
5669f4a2713aSLionel Sambuc // construct a special BlockLiteral instead; this doesn't really make
5670f4a2713aSLionel Sambuc // a difference in ARC, but outside of ARC the resulting block literal
5671f4a2713aSLionel Sambuc // follows the normal lifetime rules for block literals instead of being
5672f4a2713aSLionel Sambuc // autoreleased.
5673f4a2713aSLionel Sambuc DiagnosticErrorTrap Trap(Diags);
5674f4a2713aSLionel Sambuc ExprResult Exp = BuildBlockForLambdaConversion(E->getExprLoc(),
5675f4a2713aSLionel Sambuc E->getExprLoc(),
5676f4a2713aSLionel Sambuc Method, E);
5677f4a2713aSLionel Sambuc if (Exp.isInvalid())
5678f4a2713aSLionel Sambuc Diag(E->getExprLoc(), diag::note_lambda_to_block_conv);
5679f4a2713aSLionel Sambuc return Exp;
5680f4a2713aSLionel Sambuc }
5681f4a2713aSLionel Sambuc }
5682f4a2713aSLionel Sambuc
5683*0a6a1f1dSLionel Sambuc ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/nullptr,
5684f4a2713aSLionel Sambuc FoundDecl, Method);
5685f4a2713aSLionel Sambuc if (Exp.isInvalid())
5686f4a2713aSLionel Sambuc return true;
5687f4a2713aSLionel Sambuc
5688f4a2713aSLionel Sambuc MemberExpr *ME =
5689*0a6a1f1dSLionel Sambuc new (Context) MemberExpr(Exp.get(), /*IsArrow=*/false, Method,
5690f4a2713aSLionel Sambuc SourceLocation(), Context.BoundMemberTy,
5691f4a2713aSLionel Sambuc VK_RValue, OK_Ordinary);
5692f4a2713aSLionel Sambuc if (HadMultipleCandidates)
5693f4a2713aSLionel Sambuc ME->setHadMultipleCandidates(true);
5694f4a2713aSLionel Sambuc MarkMemberReferenced(ME);
5695f4a2713aSLionel Sambuc
5696*0a6a1f1dSLionel Sambuc QualType ResultType = Method->getReturnType();
5697f4a2713aSLionel Sambuc ExprValueKind VK = Expr::getValueKindForType(ResultType);
5698f4a2713aSLionel Sambuc ResultType = ResultType.getNonLValueExprType(Context);
5699f4a2713aSLionel Sambuc
5700f4a2713aSLionel Sambuc CXXMemberCallExpr *CE =
5701f4a2713aSLionel Sambuc new (Context) CXXMemberCallExpr(Context, ME, None, ResultType, VK,
5702f4a2713aSLionel Sambuc Exp.get()->getLocEnd());
5703f4a2713aSLionel Sambuc return CE;
5704f4a2713aSLionel Sambuc }
5705f4a2713aSLionel Sambuc
BuildCXXNoexceptExpr(SourceLocation KeyLoc,Expr * Operand,SourceLocation RParen)5706f4a2713aSLionel Sambuc ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand,
5707f4a2713aSLionel Sambuc SourceLocation RParen) {
5708*0a6a1f1dSLionel Sambuc // If the operand is an unresolved lookup expression, the expression is ill-
5709*0a6a1f1dSLionel Sambuc // formed per [over.over]p1, because overloaded function names cannot be used
5710*0a6a1f1dSLionel Sambuc // without arguments except in explicit contexts.
5711*0a6a1f1dSLionel Sambuc ExprResult R = CheckPlaceholderExpr(Operand);
5712*0a6a1f1dSLionel Sambuc if (R.isInvalid())
5713*0a6a1f1dSLionel Sambuc return R;
5714*0a6a1f1dSLionel Sambuc
5715*0a6a1f1dSLionel Sambuc // The operand may have been modified when checking the placeholder type.
5716*0a6a1f1dSLionel Sambuc Operand = R.get();
5717*0a6a1f1dSLionel Sambuc
5718*0a6a1f1dSLionel Sambuc if (ActiveTemplateInstantiations.empty() &&
5719*0a6a1f1dSLionel Sambuc Operand->HasSideEffects(Context, false)) {
5720*0a6a1f1dSLionel Sambuc // The expression operand for noexcept is in an unevaluated expression
5721*0a6a1f1dSLionel Sambuc // context, so side effects could result in unintended consequences.
5722*0a6a1f1dSLionel Sambuc Diag(Operand->getExprLoc(), diag::warn_side_effects_unevaluated_context);
5723*0a6a1f1dSLionel Sambuc }
5724*0a6a1f1dSLionel Sambuc
5725f4a2713aSLionel Sambuc CanThrowResult CanThrow = canThrow(Operand);
5726*0a6a1f1dSLionel Sambuc return new (Context)
5727*0a6a1f1dSLionel Sambuc CXXNoexceptExpr(Context.BoolTy, Operand, CanThrow, KeyLoc, RParen);
5728f4a2713aSLionel Sambuc }
5729f4a2713aSLionel Sambuc
ActOnNoexceptExpr(SourceLocation KeyLoc,SourceLocation,Expr * Operand,SourceLocation RParen)5730f4a2713aSLionel Sambuc ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation,
5731f4a2713aSLionel Sambuc Expr *Operand, SourceLocation RParen) {
5732f4a2713aSLionel Sambuc return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen);
5733f4a2713aSLionel Sambuc }
5734f4a2713aSLionel Sambuc
IsSpecialDiscardedValue(Expr * E)5735f4a2713aSLionel Sambuc static bool IsSpecialDiscardedValue(Expr *E) {
5736f4a2713aSLionel Sambuc // In C++11, discarded-value expressions of a certain form are special,
5737f4a2713aSLionel Sambuc // according to [expr]p10:
5738f4a2713aSLionel Sambuc // The lvalue-to-rvalue conversion (4.1) is applied only if the
5739f4a2713aSLionel Sambuc // expression is an lvalue of volatile-qualified type and it has
5740f4a2713aSLionel Sambuc // one of the following forms:
5741f4a2713aSLionel Sambuc E = E->IgnoreParens();
5742f4a2713aSLionel Sambuc
5743f4a2713aSLionel Sambuc // - id-expression (5.1.1),
5744f4a2713aSLionel Sambuc if (isa<DeclRefExpr>(E))
5745f4a2713aSLionel Sambuc return true;
5746f4a2713aSLionel Sambuc
5747f4a2713aSLionel Sambuc // - subscripting (5.2.1),
5748f4a2713aSLionel Sambuc if (isa<ArraySubscriptExpr>(E))
5749f4a2713aSLionel Sambuc return true;
5750f4a2713aSLionel Sambuc
5751f4a2713aSLionel Sambuc // - class member access (5.2.5),
5752f4a2713aSLionel Sambuc if (isa<MemberExpr>(E))
5753f4a2713aSLionel Sambuc return true;
5754f4a2713aSLionel Sambuc
5755f4a2713aSLionel Sambuc // - indirection (5.3.1),
5756f4a2713aSLionel Sambuc if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
5757f4a2713aSLionel Sambuc if (UO->getOpcode() == UO_Deref)
5758f4a2713aSLionel Sambuc return true;
5759f4a2713aSLionel Sambuc
5760f4a2713aSLionel Sambuc if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
5761f4a2713aSLionel Sambuc // - pointer-to-member operation (5.5),
5762f4a2713aSLionel Sambuc if (BO->isPtrMemOp())
5763f4a2713aSLionel Sambuc return true;
5764f4a2713aSLionel Sambuc
5765f4a2713aSLionel Sambuc // - comma expression (5.18) where the right operand is one of the above.
5766f4a2713aSLionel Sambuc if (BO->getOpcode() == BO_Comma)
5767f4a2713aSLionel Sambuc return IsSpecialDiscardedValue(BO->getRHS());
5768f4a2713aSLionel Sambuc }
5769f4a2713aSLionel Sambuc
5770f4a2713aSLionel Sambuc // - conditional expression (5.16) where both the second and the third
5771f4a2713aSLionel Sambuc // operands are one of the above, or
5772f4a2713aSLionel Sambuc if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
5773f4a2713aSLionel Sambuc return IsSpecialDiscardedValue(CO->getTrueExpr()) &&
5774f4a2713aSLionel Sambuc IsSpecialDiscardedValue(CO->getFalseExpr());
5775f4a2713aSLionel Sambuc // The related edge case of "*x ?: *x".
5776f4a2713aSLionel Sambuc if (BinaryConditionalOperator *BCO =
5777f4a2713aSLionel Sambuc dyn_cast<BinaryConditionalOperator>(E)) {
5778f4a2713aSLionel Sambuc if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(BCO->getTrueExpr()))
5779f4a2713aSLionel Sambuc return IsSpecialDiscardedValue(OVE->getSourceExpr()) &&
5780f4a2713aSLionel Sambuc IsSpecialDiscardedValue(BCO->getFalseExpr());
5781f4a2713aSLionel Sambuc }
5782f4a2713aSLionel Sambuc
5783f4a2713aSLionel Sambuc // Objective-C++ extensions to the rule.
5784f4a2713aSLionel Sambuc if (isa<PseudoObjectExpr>(E) || isa<ObjCIvarRefExpr>(E))
5785f4a2713aSLionel Sambuc return true;
5786f4a2713aSLionel Sambuc
5787f4a2713aSLionel Sambuc return false;
5788f4a2713aSLionel Sambuc }
5789f4a2713aSLionel Sambuc
5790f4a2713aSLionel Sambuc /// Perform the conversions required for an expression used in a
5791f4a2713aSLionel Sambuc /// context that ignores the result.
IgnoredValueConversions(Expr * E)5792f4a2713aSLionel Sambuc ExprResult Sema::IgnoredValueConversions(Expr *E) {
5793f4a2713aSLionel Sambuc if (E->hasPlaceholderType()) {
5794f4a2713aSLionel Sambuc ExprResult result = CheckPlaceholderExpr(E);
5795*0a6a1f1dSLionel Sambuc if (result.isInvalid()) return E;
5796*0a6a1f1dSLionel Sambuc E = result.get();
5797f4a2713aSLionel Sambuc }
5798f4a2713aSLionel Sambuc
5799f4a2713aSLionel Sambuc // C99 6.3.2.1:
5800f4a2713aSLionel Sambuc // [Except in specific positions,] an lvalue that does not have
5801f4a2713aSLionel Sambuc // array type is converted to the value stored in the
5802f4a2713aSLionel Sambuc // designated object (and is no longer an lvalue).
5803f4a2713aSLionel Sambuc if (E->isRValue()) {
5804f4a2713aSLionel Sambuc // In C, function designators (i.e. expressions of function type)
5805f4a2713aSLionel Sambuc // are r-values, but we still want to do function-to-pointer decay
5806f4a2713aSLionel Sambuc // on them. This is both technically correct and convenient for
5807f4a2713aSLionel Sambuc // some clients.
5808f4a2713aSLionel Sambuc if (!getLangOpts().CPlusPlus && E->getType()->isFunctionType())
5809f4a2713aSLionel Sambuc return DefaultFunctionArrayConversion(E);
5810f4a2713aSLionel Sambuc
5811*0a6a1f1dSLionel Sambuc return E;
5812f4a2713aSLionel Sambuc }
5813f4a2713aSLionel Sambuc
5814f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus) {
5815f4a2713aSLionel Sambuc // The C++11 standard defines the notion of a discarded-value expression;
5816f4a2713aSLionel Sambuc // normally, we don't need to do anything to handle it, but if it is a
5817f4a2713aSLionel Sambuc // volatile lvalue with a special form, we perform an lvalue-to-rvalue
5818f4a2713aSLionel Sambuc // conversion.
5819f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11 && E->isGLValue() &&
5820f4a2713aSLionel Sambuc E->getType().isVolatileQualified() &&
5821f4a2713aSLionel Sambuc IsSpecialDiscardedValue(E)) {
5822f4a2713aSLionel Sambuc ExprResult Res = DefaultLvalueConversion(E);
5823f4a2713aSLionel Sambuc if (Res.isInvalid())
5824*0a6a1f1dSLionel Sambuc return E;
5825*0a6a1f1dSLionel Sambuc E = Res.get();
5826f4a2713aSLionel Sambuc }
5827*0a6a1f1dSLionel Sambuc return E;
5828f4a2713aSLionel Sambuc }
5829f4a2713aSLionel Sambuc
5830f4a2713aSLionel Sambuc // GCC seems to also exclude expressions of incomplete enum type.
5831f4a2713aSLionel Sambuc if (const EnumType *T = E->getType()->getAs<EnumType>()) {
5832f4a2713aSLionel Sambuc if (!T->getDecl()->isComplete()) {
5833f4a2713aSLionel Sambuc // FIXME: stupid workaround for a codegen bug!
5834*0a6a1f1dSLionel Sambuc E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).get();
5835*0a6a1f1dSLionel Sambuc return E;
5836f4a2713aSLionel Sambuc }
5837f4a2713aSLionel Sambuc }
5838f4a2713aSLionel Sambuc
5839f4a2713aSLionel Sambuc ExprResult Res = DefaultFunctionArrayLvalueConversion(E);
5840f4a2713aSLionel Sambuc if (Res.isInvalid())
5841*0a6a1f1dSLionel Sambuc return E;
5842*0a6a1f1dSLionel Sambuc E = Res.get();
5843f4a2713aSLionel Sambuc
5844f4a2713aSLionel Sambuc if (!E->getType()->isVoidType())
5845f4a2713aSLionel Sambuc RequireCompleteType(E->getExprLoc(), E->getType(),
5846f4a2713aSLionel Sambuc diag::err_incomplete_type);
5847*0a6a1f1dSLionel Sambuc return E;
5848f4a2713aSLionel Sambuc }
5849f4a2713aSLionel Sambuc
5850f4a2713aSLionel Sambuc // If we can unambiguously determine whether Var can never be used
5851f4a2713aSLionel Sambuc // in a constant expression, return true.
5852f4a2713aSLionel Sambuc // - if the variable and its initializer are non-dependent, then
5853f4a2713aSLionel Sambuc // we can unambiguously check if the variable is a constant expression.
5854f4a2713aSLionel Sambuc // - if the initializer is not value dependent - we can determine whether
5855f4a2713aSLionel Sambuc // it can be used to initialize a constant expression. If Init can not
5856f4a2713aSLionel Sambuc // be used to initialize a constant expression we conclude that Var can
5857f4a2713aSLionel Sambuc // never be a constant expression.
5858f4a2713aSLionel Sambuc // - FXIME: if the initializer is dependent, we can still do some analysis and
5859f4a2713aSLionel Sambuc // identify certain cases unambiguously as non-const by using a Visitor:
5860f4a2713aSLionel Sambuc // - such as those that involve odr-use of a ParmVarDecl, involve a new
5861f4a2713aSLionel Sambuc // delete, lambda-expr, dynamic-cast, reinterpret-cast etc...
VariableCanNeverBeAConstantExpression(VarDecl * Var,ASTContext & Context)5862f4a2713aSLionel Sambuc static inline bool VariableCanNeverBeAConstantExpression(VarDecl *Var,
5863f4a2713aSLionel Sambuc ASTContext &Context) {
5864f4a2713aSLionel Sambuc if (isa<ParmVarDecl>(Var)) return true;
5865*0a6a1f1dSLionel Sambuc const VarDecl *DefVD = nullptr;
5866f4a2713aSLionel Sambuc
5867f4a2713aSLionel Sambuc // If there is no initializer - this can not be a constant expression.
5868f4a2713aSLionel Sambuc if (!Var->getAnyInitializer(DefVD)) return true;
5869f4a2713aSLionel Sambuc assert(DefVD);
5870f4a2713aSLionel Sambuc if (DefVD->isWeak()) return false;
5871f4a2713aSLionel Sambuc EvaluatedStmt *Eval = DefVD->ensureEvaluatedStmt();
5872f4a2713aSLionel Sambuc
5873f4a2713aSLionel Sambuc Expr *Init = cast<Expr>(Eval->Value);
5874f4a2713aSLionel Sambuc
5875f4a2713aSLionel Sambuc if (Var->getType()->isDependentType() || Init->isValueDependent()) {
5876*0a6a1f1dSLionel Sambuc // FIXME: Teach the constant evaluator to deal with the non-dependent parts
5877*0a6a1f1dSLionel Sambuc // of value-dependent expressions, and use it here to determine whether the
5878*0a6a1f1dSLionel Sambuc // initializer is a potential constant expression.
5879f4a2713aSLionel Sambuc return false;
5880f4a2713aSLionel Sambuc }
5881*0a6a1f1dSLionel Sambuc
5882f4a2713aSLionel Sambuc return !IsVariableAConstantExpression(Var, Context);
5883f4a2713aSLionel Sambuc }
5884f4a2713aSLionel Sambuc
5885*0a6a1f1dSLionel Sambuc /// \brief Check if the current lambda has any potential captures
5886*0a6a1f1dSLionel Sambuc /// that must be captured by any of its enclosing lambdas that are ready to
5887*0a6a1f1dSLionel Sambuc /// capture. If there is a lambda that can capture a nested
5888f4a2713aSLionel Sambuc /// potential-capture, go ahead and do so. Also, check to see if any
5889f4a2713aSLionel Sambuc /// variables are uncaptureable or do not involve an odr-use so do not
5890f4a2713aSLionel Sambuc /// need to be captured.
5891f4a2713aSLionel Sambuc
CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(Expr * const FE,LambdaScopeInfo * const CurrentLSI,Sema & S)5892*0a6a1f1dSLionel Sambuc static void CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(
5893*0a6a1f1dSLionel Sambuc Expr *const FE, LambdaScopeInfo *const CurrentLSI, Sema &S) {
5894f4a2713aSLionel Sambuc
5895f4a2713aSLionel Sambuc assert(!S.isUnevaluatedContext());
5896f4a2713aSLionel Sambuc assert(S.CurContext->isDependentContext());
5897*0a6a1f1dSLionel Sambuc assert(CurrentLSI->CallOperator == S.CurContext &&
5898*0a6a1f1dSLionel Sambuc "The current call operator must be synchronized with Sema's CurContext");
5899*0a6a1f1dSLionel Sambuc
5900*0a6a1f1dSLionel Sambuc const bool IsFullExprInstantiationDependent = FE->isInstantiationDependent();
5901*0a6a1f1dSLionel Sambuc
5902*0a6a1f1dSLionel Sambuc ArrayRef<const FunctionScopeInfo *> FunctionScopesArrayRef(
5903*0a6a1f1dSLionel Sambuc S.FunctionScopes.data(), S.FunctionScopes.size());
5904*0a6a1f1dSLionel Sambuc
5905f4a2713aSLionel Sambuc // All the potentially captureable variables in the current nested
5906f4a2713aSLionel Sambuc // lambda (within a generic outer lambda), must be captured by an
5907f4a2713aSLionel Sambuc // outer lambda that is enclosed within a non-dependent context.
5908*0a6a1f1dSLionel Sambuc const unsigned NumPotentialCaptures =
5909*0a6a1f1dSLionel Sambuc CurrentLSI->getNumPotentialVariableCaptures();
5910*0a6a1f1dSLionel Sambuc for (unsigned I = 0; I != NumPotentialCaptures; ++I) {
5911*0a6a1f1dSLionel Sambuc Expr *VarExpr = nullptr;
5912*0a6a1f1dSLionel Sambuc VarDecl *Var = nullptr;
5913f4a2713aSLionel Sambuc CurrentLSI->getPotentialVariableCapture(I, Var, VarExpr);
5914*0a6a1f1dSLionel Sambuc // If the variable is clearly identified as non-odr-used and the full
5915*0a6a1f1dSLionel Sambuc // expression is not instantiation dependent, only then do we not
5916*0a6a1f1dSLionel Sambuc // need to check enclosing lambda's for speculative captures.
5917*0a6a1f1dSLionel Sambuc // For e.g.:
5918*0a6a1f1dSLionel Sambuc // Even though 'x' is not odr-used, it should be captured.
5919*0a6a1f1dSLionel Sambuc // int test() {
5920*0a6a1f1dSLionel Sambuc // const int x = 10;
5921*0a6a1f1dSLionel Sambuc // auto L = [=](auto a) {
5922*0a6a1f1dSLionel Sambuc // (void) +x + a;
5923*0a6a1f1dSLionel Sambuc // };
5924*0a6a1f1dSLionel Sambuc // }
5925f4a2713aSLionel Sambuc if (CurrentLSI->isVariableExprMarkedAsNonODRUsed(VarExpr) &&
5926f4a2713aSLionel Sambuc !IsFullExprInstantiationDependent)
5927f4a2713aSLionel Sambuc continue;
5928*0a6a1f1dSLionel Sambuc
5929*0a6a1f1dSLionel Sambuc // If we have a capture-capable lambda for the variable, go ahead and
5930*0a6a1f1dSLionel Sambuc // capture the variable in that lambda (and all its enclosing lambdas).
5931*0a6a1f1dSLionel Sambuc if (const Optional<unsigned> Index =
5932*0a6a1f1dSLionel Sambuc getStackIndexOfNearestEnclosingCaptureCapableLambda(
5933*0a6a1f1dSLionel Sambuc FunctionScopesArrayRef, Var, S)) {
5934*0a6a1f1dSLionel Sambuc const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue();
5935*0a6a1f1dSLionel Sambuc MarkVarDeclODRUsed(Var, VarExpr->getExprLoc(), S,
5936*0a6a1f1dSLionel Sambuc &FunctionScopeIndexOfCapturableLambda);
5937f4a2713aSLionel Sambuc }
5938f4a2713aSLionel Sambuc const bool IsVarNeverAConstantExpression =
5939f4a2713aSLionel Sambuc VariableCanNeverBeAConstantExpression(Var, S.Context);
5940f4a2713aSLionel Sambuc if (!IsFullExprInstantiationDependent || IsVarNeverAConstantExpression) {
5941f4a2713aSLionel Sambuc // This full expression is not instantiation dependent or the variable
5942f4a2713aSLionel Sambuc // can not be used in a constant expression - which means
5943f4a2713aSLionel Sambuc // this variable must be odr-used here, so diagnose a
5944f4a2713aSLionel Sambuc // capture violation early, if the variable is un-captureable.
5945f4a2713aSLionel Sambuc // This is purely for diagnosing errors early. Otherwise, this
5946f4a2713aSLionel Sambuc // error would get diagnosed when the lambda becomes capture ready.
5947f4a2713aSLionel Sambuc QualType CaptureType, DeclRefType;
5948f4a2713aSLionel Sambuc SourceLocation ExprLoc = VarExpr->getExprLoc();
5949f4a2713aSLionel Sambuc if (S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit,
5950f4a2713aSLionel Sambuc /*EllipsisLoc*/ SourceLocation(),
5951f4a2713aSLionel Sambuc /*BuildAndDiagnose*/false, CaptureType,
5952*0a6a1f1dSLionel Sambuc DeclRefType, nullptr)) {
5953f4a2713aSLionel Sambuc // We will never be able to capture this variable, and we need
5954f4a2713aSLionel Sambuc // to be able to in any and all instantiations, so diagnose it.
5955f4a2713aSLionel Sambuc S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit,
5956f4a2713aSLionel Sambuc /*EllipsisLoc*/ SourceLocation(),
5957f4a2713aSLionel Sambuc /*BuildAndDiagnose*/true, CaptureType,
5958*0a6a1f1dSLionel Sambuc DeclRefType, nullptr);
5959f4a2713aSLionel Sambuc }
5960f4a2713aSLionel Sambuc }
5961f4a2713aSLionel Sambuc }
5962f4a2713aSLionel Sambuc
5963*0a6a1f1dSLionel Sambuc // Check if 'this' needs to be captured.
5964f4a2713aSLionel Sambuc if (CurrentLSI->hasPotentialThisCapture()) {
5965*0a6a1f1dSLionel Sambuc // If we have a capture-capable lambda for 'this', go ahead and capture
5966*0a6a1f1dSLionel Sambuc // 'this' in that lambda (and all its enclosing lambdas).
5967*0a6a1f1dSLionel Sambuc if (const Optional<unsigned> Index =
5968*0a6a1f1dSLionel Sambuc getStackIndexOfNearestEnclosingCaptureCapableLambda(
5969*0a6a1f1dSLionel Sambuc FunctionScopesArrayRef, /*0 is 'this'*/ nullptr, S)) {
5970*0a6a1f1dSLionel Sambuc const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue();
5971f4a2713aSLionel Sambuc S.CheckCXXThisCapture(CurrentLSI->PotentialThisCaptureLocation,
5972f4a2713aSLionel Sambuc /*Explicit*/ false, /*BuildAndDiagnose*/ true,
5973f4a2713aSLionel Sambuc &FunctionScopeIndexOfCapturableLambda);
5974f4a2713aSLionel Sambuc }
5975f4a2713aSLionel Sambuc }
5976*0a6a1f1dSLionel Sambuc
5977*0a6a1f1dSLionel Sambuc // Reset all the potential captures at the end of each full-expression.
5978f4a2713aSLionel Sambuc CurrentLSI->clearPotentialCaptures();
5979f4a2713aSLionel Sambuc }
5980f4a2713aSLionel Sambuc
attemptRecovery(Sema & SemaRef,const TypoCorrectionConsumer & Consumer,TypoCorrection TC)5981*0a6a1f1dSLionel Sambuc static ExprResult attemptRecovery(Sema &SemaRef,
5982*0a6a1f1dSLionel Sambuc const TypoCorrectionConsumer &Consumer,
5983*0a6a1f1dSLionel Sambuc TypoCorrection TC) {
5984*0a6a1f1dSLionel Sambuc LookupResult R(SemaRef, Consumer.getLookupResult().getLookupNameInfo(),
5985*0a6a1f1dSLionel Sambuc Consumer.getLookupResult().getLookupKind());
5986*0a6a1f1dSLionel Sambuc const CXXScopeSpec *SS = Consumer.getSS();
5987*0a6a1f1dSLionel Sambuc CXXScopeSpec NewSS;
5988*0a6a1f1dSLionel Sambuc
5989*0a6a1f1dSLionel Sambuc // Use an approprate CXXScopeSpec for building the expr.
5990*0a6a1f1dSLionel Sambuc if (auto *NNS = TC.getCorrectionSpecifier())
5991*0a6a1f1dSLionel Sambuc NewSS.MakeTrivial(SemaRef.Context, NNS, TC.getCorrectionRange());
5992*0a6a1f1dSLionel Sambuc else if (SS && !TC.WillReplaceSpecifier())
5993*0a6a1f1dSLionel Sambuc NewSS = *SS;
5994*0a6a1f1dSLionel Sambuc
5995*0a6a1f1dSLionel Sambuc if (auto *ND = TC.getCorrectionDecl()) {
5996*0a6a1f1dSLionel Sambuc R.setLookupName(ND->getDeclName());
5997*0a6a1f1dSLionel Sambuc R.addDecl(ND);
5998*0a6a1f1dSLionel Sambuc if (ND->isCXXClassMember()) {
5999*0a6a1f1dSLionel Sambuc // Figure out the correct naming class to add to the LookupResult.
6000*0a6a1f1dSLionel Sambuc CXXRecordDecl *Record = nullptr;
6001*0a6a1f1dSLionel Sambuc if (auto *NNS = TC.getCorrectionSpecifier())
6002*0a6a1f1dSLionel Sambuc Record = NNS->getAsType()->getAsCXXRecordDecl();
6003*0a6a1f1dSLionel Sambuc if (!Record)
6004*0a6a1f1dSLionel Sambuc Record =
6005*0a6a1f1dSLionel Sambuc dyn_cast<CXXRecordDecl>(ND->getDeclContext()->getRedeclContext());
6006*0a6a1f1dSLionel Sambuc if (Record)
6007*0a6a1f1dSLionel Sambuc R.setNamingClass(Record);
6008*0a6a1f1dSLionel Sambuc
6009*0a6a1f1dSLionel Sambuc // Detect and handle the case where the decl might be an implicit
6010*0a6a1f1dSLionel Sambuc // member.
6011*0a6a1f1dSLionel Sambuc bool MightBeImplicitMember;
6012*0a6a1f1dSLionel Sambuc if (!Consumer.isAddressOfOperand())
6013*0a6a1f1dSLionel Sambuc MightBeImplicitMember = true;
6014*0a6a1f1dSLionel Sambuc else if (!NewSS.isEmpty())
6015*0a6a1f1dSLionel Sambuc MightBeImplicitMember = false;
6016*0a6a1f1dSLionel Sambuc else if (R.isOverloadedResult())
6017*0a6a1f1dSLionel Sambuc MightBeImplicitMember = false;
6018*0a6a1f1dSLionel Sambuc else if (R.isUnresolvableResult())
6019*0a6a1f1dSLionel Sambuc MightBeImplicitMember = true;
6020*0a6a1f1dSLionel Sambuc else
6021*0a6a1f1dSLionel Sambuc MightBeImplicitMember = isa<FieldDecl>(ND) ||
6022*0a6a1f1dSLionel Sambuc isa<IndirectFieldDecl>(ND) ||
6023*0a6a1f1dSLionel Sambuc isa<MSPropertyDecl>(ND);
6024*0a6a1f1dSLionel Sambuc
6025*0a6a1f1dSLionel Sambuc if (MightBeImplicitMember)
6026*0a6a1f1dSLionel Sambuc return SemaRef.BuildPossibleImplicitMemberExpr(
6027*0a6a1f1dSLionel Sambuc NewSS, /*TemplateKWLoc*/ SourceLocation(), R,
6028*0a6a1f1dSLionel Sambuc /*TemplateArgs*/ nullptr);
6029*0a6a1f1dSLionel Sambuc } else if (auto *Ivar = dyn_cast<ObjCIvarDecl>(ND)) {
6030*0a6a1f1dSLionel Sambuc return SemaRef.LookupInObjCMethod(R, Consumer.getScope(),
6031*0a6a1f1dSLionel Sambuc Ivar->getIdentifier());
6032*0a6a1f1dSLionel Sambuc }
6033*0a6a1f1dSLionel Sambuc }
6034*0a6a1f1dSLionel Sambuc
6035*0a6a1f1dSLionel Sambuc return SemaRef.BuildDeclarationNameExpr(NewSS, R, /*NeedsADL*/ false,
6036*0a6a1f1dSLionel Sambuc /*AcceptInvalidDecl*/ true);
6037*0a6a1f1dSLionel Sambuc }
6038*0a6a1f1dSLionel Sambuc
6039*0a6a1f1dSLionel Sambuc namespace {
6040*0a6a1f1dSLionel Sambuc class FindTypoExprs : public RecursiveASTVisitor<FindTypoExprs> {
6041*0a6a1f1dSLionel Sambuc llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs;
6042*0a6a1f1dSLionel Sambuc
6043*0a6a1f1dSLionel Sambuc public:
FindTypoExprs(llvm::SmallSetVector<TypoExpr *,2> & TypoExprs)6044*0a6a1f1dSLionel Sambuc explicit FindTypoExprs(llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs)
6045*0a6a1f1dSLionel Sambuc : TypoExprs(TypoExprs) {}
VisitTypoExpr(TypoExpr * TE)6046*0a6a1f1dSLionel Sambuc bool VisitTypoExpr(TypoExpr *TE) {
6047*0a6a1f1dSLionel Sambuc TypoExprs.insert(TE);
6048*0a6a1f1dSLionel Sambuc return true;
6049*0a6a1f1dSLionel Sambuc }
6050*0a6a1f1dSLionel Sambuc };
6051*0a6a1f1dSLionel Sambuc
6052*0a6a1f1dSLionel Sambuc class TransformTypos : public TreeTransform<TransformTypos> {
6053*0a6a1f1dSLionel Sambuc typedef TreeTransform<TransformTypos> BaseTransform;
6054*0a6a1f1dSLionel Sambuc
6055*0a6a1f1dSLionel Sambuc llvm::function_ref<ExprResult(Expr *)> ExprFilter;
6056*0a6a1f1dSLionel Sambuc llvm::SmallSetVector<TypoExpr *, 2> TypoExprs, AmbiguousTypoExprs;
6057*0a6a1f1dSLionel Sambuc llvm::SmallDenseMap<TypoExpr *, ExprResult, 2> TransformCache;
6058*0a6a1f1dSLionel Sambuc llvm::SmallDenseMap<OverloadExpr *, Expr *, 4> OverloadResolution;
6059*0a6a1f1dSLionel Sambuc
6060*0a6a1f1dSLionel Sambuc /// \brief Emit diagnostics for all of the TypoExprs encountered.
6061*0a6a1f1dSLionel Sambuc /// If the TypoExprs were successfully corrected, then the diagnostics should
6062*0a6a1f1dSLionel Sambuc /// suggest the corrections. Otherwise the diagnostics will not suggest
6063*0a6a1f1dSLionel Sambuc /// anything (having been passed an empty TypoCorrection).
EmitAllDiagnostics()6064*0a6a1f1dSLionel Sambuc void EmitAllDiagnostics() {
6065*0a6a1f1dSLionel Sambuc for (auto E : TypoExprs) {
6066*0a6a1f1dSLionel Sambuc TypoExpr *TE = cast<TypoExpr>(E);
6067*0a6a1f1dSLionel Sambuc auto &State = SemaRef.getTypoExprState(TE);
6068*0a6a1f1dSLionel Sambuc if (State.DiagHandler) {
6069*0a6a1f1dSLionel Sambuc TypoCorrection TC = State.Consumer->getCurrentCorrection();
6070*0a6a1f1dSLionel Sambuc ExprResult Replacement = TransformCache[TE];
6071*0a6a1f1dSLionel Sambuc
6072*0a6a1f1dSLionel Sambuc // Extract the NamedDecl from the transformed TypoExpr and add it to the
6073*0a6a1f1dSLionel Sambuc // TypoCorrection, replacing the existing decls. This ensures the right
6074*0a6a1f1dSLionel Sambuc // NamedDecl is used in diagnostics e.g. in the case where overload
6075*0a6a1f1dSLionel Sambuc // resolution was used to select one from several possible decls that
6076*0a6a1f1dSLionel Sambuc // had been stored in the TypoCorrection.
6077*0a6a1f1dSLionel Sambuc if (auto *ND = getDeclFromExpr(
6078*0a6a1f1dSLionel Sambuc Replacement.isInvalid() ? nullptr : Replacement.get()))
6079*0a6a1f1dSLionel Sambuc TC.setCorrectionDecl(ND);
6080*0a6a1f1dSLionel Sambuc
6081*0a6a1f1dSLionel Sambuc State.DiagHandler(TC);
6082*0a6a1f1dSLionel Sambuc }
6083*0a6a1f1dSLionel Sambuc SemaRef.clearDelayedTypo(TE);
6084*0a6a1f1dSLionel Sambuc }
6085*0a6a1f1dSLionel Sambuc }
6086*0a6a1f1dSLionel Sambuc
6087*0a6a1f1dSLionel Sambuc /// \brief If corrections for the first TypoExpr have been exhausted for a
6088*0a6a1f1dSLionel Sambuc /// given combination of the other TypoExprs, retry those corrections against
6089*0a6a1f1dSLionel Sambuc /// the next combination of substitutions for the other TypoExprs by advancing
6090*0a6a1f1dSLionel Sambuc /// to the next potential correction of the second TypoExpr. For the second
6091*0a6a1f1dSLionel Sambuc /// and subsequent TypoExprs, if its stream of corrections has been exhausted,
6092*0a6a1f1dSLionel Sambuc /// the stream is reset and the next TypoExpr's stream is advanced by one (a
6093*0a6a1f1dSLionel Sambuc /// TypoExpr's correction stream is advanced by removing the TypoExpr from the
6094*0a6a1f1dSLionel Sambuc /// TransformCache). Returns true if there is still any untried combinations
6095*0a6a1f1dSLionel Sambuc /// of corrections.
CheckAndAdvanceTypoExprCorrectionStreams()6096*0a6a1f1dSLionel Sambuc bool CheckAndAdvanceTypoExprCorrectionStreams() {
6097*0a6a1f1dSLionel Sambuc for (auto TE : TypoExprs) {
6098*0a6a1f1dSLionel Sambuc auto &State = SemaRef.getTypoExprState(TE);
6099*0a6a1f1dSLionel Sambuc TransformCache.erase(TE);
6100*0a6a1f1dSLionel Sambuc if (!State.Consumer->finished())
6101*0a6a1f1dSLionel Sambuc return true;
6102*0a6a1f1dSLionel Sambuc State.Consumer->resetCorrectionStream();
6103*0a6a1f1dSLionel Sambuc }
6104*0a6a1f1dSLionel Sambuc return false;
6105*0a6a1f1dSLionel Sambuc }
6106*0a6a1f1dSLionel Sambuc
getDeclFromExpr(Expr * E)6107*0a6a1f1dSLionel Sambuc NamedDecl *getDeclFromExpr(Expr *E) {
6108*0a6a1f1dSLionel Sambuc if (auto *OE = dyn_cast_or_null<OverloadExpr>(E))
6109*0a6a1f1dSLionel Sambuc E = OverloadResolution[OE];
6110*0a6a1f1dSLionel Sambuc
6111*0a6a1f1dSLionel Sambuc if (!E)
6112*0a6a1f1dSLionel Sambuc return nullptr;
6113*0a6a1f1dSLionel Sambuc if (auto *DRE = dyn_cast<DeclRefExpr>(E))
6114*0a6a1f1dSLionel Sambuc return DRE->getDecl();
6115*0a6a1f1dSLionel Sambuc if (auto *ME = dyn_cast<MemberExpr>(E))
6116*0a6a1f1dSLionel Sambuc return ME->getMemberDecl();
6117*0a6a1f1dSLionel Sambuc // FIXME: Add any other expr types that could be be seen by the delayed typo
6118*0a6a1f1dSLionel Sambuc // correction TreeTransform for which the corresponding TypoCorrection could
6119*0a6a1f1dSLionel Sambuc // contain multiple decls.
6120*0a6a1f1dSLionel Sambuc return nullptr;
6121*0a6a1f1dSLionel Sambuc }
6122*0a6a1f1dSLionel Sambuc
TryTransform(Expr * E)6123*0a6a1f1dSLionel Sambuc ExprResult TryTransform(Expr *E) {
6124*0a6a1f1dSLionel Sambuc Sema::SFINAETrap Trap(SemaRef);
6125*0a6a1f1dSLionel Sambuc ExprResult Res = TransformExpr(E);
6126*0a6a1f1dSLionel Sambuc if (Trap.hasErrorOccurred() || Res.isInvalid())
6127*0a6a1f1dSLionel Sambuc return ExprError();
6128*0a6a1f1dSLionel Sambuc
6129*0a6a1f1dSLionel Sambuc return ExprFilter(Res.get());
6130*0a6a1f1dSLionel Sambuc }
6131*0a6a1f1dSLionel Sambuc
6132*0a6a1f1dSLionel Sambuc public:
TransformTypos(Sema & SemaRef,llvm::function_ref<ExprResult (Expr *)> Filter)6133*0a6a1f1dSLionel Sambuc TransformTypos(Sema &SemaRef, llvm::function_ref<ExprResult(Expr *)> Filter)
6134*0a6a1f1dSLionel Sambuc : BaseTransform(SemaRef), ExprFilter(Filter) {}
6135*0a6a1f1dSLionel Sambuc
RebuildCallExpr(Expr * Callee,SourceLocation LParenLoc,MultiExprArg Args,SourceLocation RParenLoc,Expr * ExecConfig=nullptr)6136*0a6a1f1dSLionel Sambuc ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc,
6137*0a6a1f1dSLionel Sambuc MultiExprArg Args,
6138*0a6a1f1dSLionel Sambuc SourceLocation RParenLoc,
6139*0a6a1f1dSLionel Sambuc Expr *ExecConfig = nullptr) {
6140*0a6a1f1dSLionel Sambuc auto Result = BaseTransform::RebuildCallExpr(Callee, LParenLoc, Args,
6141*0a6a1f1dSLionel Sambuc RParenLoc, ExecConfig);
6142*0a6a1f1dSLionel Sambuc if (auto *OE = dyn_cast<OverloadExpr>(Callee)) {
6143*0a6a1f1dSLionel Sambuc if (Result.isUsable()) {
6144*0a6a1f1dSLionel Sambuc Expr *ResultCall = Result.get();
6145*0a6a1f1dSLionel Sambuc if (auto *BE = dyn_cast<CXXBindTemporaryExpr>(ResultCall))
6146*0a6a1f1dSLionel Sambuc ResultCall = BE->getSubExpr();
6147*0a6a1f1dSLionel Sambuc if (auto *CE = dyn_cast<CallExpr>(ResultCall))
6148*0a6a1f1dSLionel Sambuc OverloadResolution[OE] = CE->getCallee();
6149*0a6a1f1dSLionel Sambuc }
6150*0a6a1f1dSLionel Sambuc }
6151*0a6a1f1dSLionel Sambuc return Result;
6152*0a6a1f1dSLionel Sambuc }
6153*0a6a1f1dSLionel Sambuc
TransformLambdaExpr(LambdaExpr * E)6154*0a6a1f1dSLionel Sambuc ExprResult TransformLambdaExpr(LambdaExpr *E) { return Owned(E); }
6155*0a6a1f1dSLionel Sambuc
Transform(Expr * E)6156*0a6a1f1dSLionel Sambuc ExprResult Transform(Expr *E) {
6157*0a6a1f1dSLionel Sambuc ExprResult Res;
6158*0a6a1f1dSLionel Sambuc while (true) {
6159*0a6a1f1dSLionel Sambuc Res = TryTransform(E);
6160*0a6a1f1dSLionel Sambuc
6161*0a6a1f1dSLionel Sambuc // Exit if either the transform was valid or if there were no TypoExprs
6162*0a6a1f1dSLionel Sambuc // to transform that still have any untried correction candidates..
6163*0a6a1f1dSLionel Sambuc if (!Res.isInvalid() ||
6164*0a6a1f1dSLionel Sambuc !CheckAndAdvanceTypoExprCorrectionStreams())
6165*0a6a1f1dSLionel Sambuc break;
6166*0a6a1f1dSLionel Sambuc }
6167*0a6a1f1dSLionel Sambuc
6168*0a6a1f1dSLionel Sambuc // Ensure none of the TypoExprs have multiple typo correction candidates
6169*0a6a1f1dSLionel Sambuc // with the same edit length that pass all the checks and filters.
6170*0a6a1f1dSLionel Sambuc // TODO: Properly handle various permutations of possible corrections when
6171*0a6a1f1dSLionel Sambuc // there is more than one potentially ambiguous typo correction.
6172*0a6a1f1dSLionel Sambuc while (!AmbiguousTypoExprs.empty()) {
6173*0a6a1f1dSLionel Sambuc auto TE = AmbiguousTypoExprs.back();
6174*0a6a1f1dSLionel Sambuc auto Cached = TransformCache[TE];
6175*0a6a1f1dSLionel Sambuc auto &State = SemaRef.getTypoExprState(TE);
6176*0a6a1f1dSLionel Sambuc State.Consumer->saveCurrentPosition();
6177*0a6a1f1dSLionel Sambuc TransformCache.erase(TE);
6178*0a6a1f1dSLionel Sambuc if (!TryTransform(E).isInvalid()) {
6179*0a6a1f1dSLionel Sambuc State.Consumer->resetCorrectionStream();
6180*0a6a1f1dSLionel Sambuc TransformCache.erase(TE);
6181*0a6a1f1dSLionel Sambuc Res = ExprError();
6182*0a6a1f1dSLionel Sambuc break;
6183*0a6a1f1dSLionel Sambuc }
6184*0a6a1f1dSLionel Sambuc AmbiguousTypoExprs.remove(TE);
6185*0a6a1f1dSLionel Sambuc State.Consumer->restoreSavedPosition();
6186*0a6a1f1dSLionel Sambuc TransformCache[TE] = Cached;
6187*0a6a1f1dSLionel Sambuc }
6188*0a6a1f1dSLionel Sambuc
6189*0a6a1f1dSLionel Sambuc // Ensure that all of the TypoExprs within the current Expr have been found.
6190*0a6a1f1dSLionel Sambuc if (!Res.isUsable())
6191*0a6a1f1dSLionel Sambuc FindTypoExprs(TypoExprs).TraverseStmt(E);
6192*0a6a1f1dSLionel Sambuc
6193*0a6a1f1dSLionel Sambuc EmitAllDiagnostics();
6194*0a6a1f1dSLionel Sambuc
6195*0a6a1f1dSLionel Sambuc return Res;
6196*0a6a1f1dSLionel Sambuc }
6197*0a6a1f1dSLionel Sambuc
TransformTypoExpr(TypoExpr * E)6198*0a6a1f1dSLionel Sambuc ExprResult TransformTypoExpr(TypoExpr *E) {
6199*0a6a1f1dSLionel Sambuc // If the TypoExpr hasn't been seen before, record it. Otherwise, return the
6200*0a6a1f1dSLionel Sambuc // cached transformation result if there is one and the TypoExpr isn't the
6201*0a6a1f1dSLionel Sambuc // first one that was encountered.
6202*0a6a1f1dSLionel Sambuc auto &CacheEntry = TransformCache[E];
6203*0a6a1f1dSLionel Sambuc if (!TypoExprs.insert(E) && !CacheEntry.isUnset()) {
6204*0a6a1f1dSLionel Sambuc return CacheEntry;
6205*0a6a1f1dSLionel Sambuc }
6206*0a6a1f1dSLionel Sambuc
6207*0a6a1f1dSLionel Sambuc auto &State = SemaRef.getTypoExprState(E);
6208*0a6a1f1dSLionel Sambuc assert(State.Consumer && "Cannot transform a cleared TypoExpr");
6209*0a6a1f1dSLionel Sambuc
6210*0a6a1f1dSLionel Sambuc // For the first TypoExpr and an uncached TypoExpr, find the next likely
6211*0a6a1f1dSLionel Sambuc // typo correction and return it.
6212*0a6a1f1dSLionel Sambuc while (TypoCorrection TC = State.Consumer->getNextCorrection()) {
6213*0a6a1f1dSLionel Sambuc ExprResult NE = State.RecoveryHandler ?
6214*0a6a1f1dSLionel Sambuc State.RecoveryHandler(SemaRef, E, TC) :
6215*0a6a1f1dSLionel Sambuc attemptRecovery(SemaRef, *State.Consumer, TC);
6216*0a6a1f1dSLionel Sambuc if (!NE.isInvalid()) {
6217*0a6a1f1dSLionel Sambuc // Check whether there may be a second viable correction with the same
6218*0a6a1f1dSLionel Sambuc // edit distance; if so, remember this TypoExpr may have an ambiguous
6219*0a6a1f1dSLionel Sambuc // correction so it can be more thoroughly vetted later.
6220*0a6a1f1dSLionel Sambuc TypoCorrection Next;
6221*0a6a1f1dSLionel Sambuc if ((Next = State.Consumer->peekNextCorrection()) &&
6222*0a6a1f1dSLionel Sambuc Next.getEditDistance(false) == TC.getEditDistance(false)) {
6223*0a6a1f1dSLionel Sambuc AmbiguousTypoExprs.insert(E);
6224*0a6a1f1dSLionel Sambuc } else {
6225*0a6a1f1dSLionel Sambuc AmbiguousTypoExprs.remove(E);
6226*0a6a1f1dSLionel Sambuc }
6227*0a6a1f1dSLionel Sambuc assert(!NE.isUnset() &&
6228*0a6a1f1dSLionel Sambuc "Typo was transformed into a valid-but-null ExprResult");
6229*0a6a1f1dSLionel Sambuc return CacheEntry = NE;
6230*0a6a1f1dSLionel Sambuc }
6231*0a6a1f1dSLionel Sambuc }
6232*0a6a1f1dSLionel Sambuc return CacheEntry = ExprError();
6233*0a6a1f1dSLionel Sambuc }
6234*0a6a1f1dSLionel Sambuc };
6235*0a6a1f1dSLionel Sambuc }
6236*0a6a1f1dSLionel Sambuc
CorrectDelayedTyposInExpr(Expr * E,llvm::function_ref<ExprResult (Expr *)> Filter)6237*0a6a1f1dSLionel Sambuc ExprResult Sema::CorrectDelayedTyposInExpr(
6238*0a6a1f1dSLionel Sambuc Expr *E, llvm::function_ref<ExprResult(Expr *)> Filter) {
6239*0a6a1f1dSLionel Sambuc // If the current evaluation context indicates there are uncorrected typos
6240*0a6a1f1dSLionel Sambuc // and the current expression isn't guaranteed to not have typos, try to
6241*0a6a1f1dSLionel Sambuc // resolve any TypoExpr nodes that might be in the expression.
6242*0a6a1f1dSLionel Sambuc if (E && !ExprEvalContexts.empty() && ExprEvalContexts.back().NumTypos &&
6243*0a6a1f1dSLionel Sambuc (E->isTypeDependent() || E->isValueDependent() ||
6244*0a6a1f1dSLionel Sambuc E->isInstantiationDependent())) {
6245*0a6a1f1dSLionel Sambuc auto TyposInContext = ExprEvalContexts.back().NumTypos;
6246*0a6a1f1dSLionel Sambuc assert(TyposInContext < ~0U && "Recursive call of CorrectDelayedTyposInExpr");
6247*0a6a1f1dSLionel Sambuc ExprEvalContexts.back().NumTypos = ~0U;
6248*0a6a1f1dSLionel Sambuc auto TyposResolved = DelayedTypos.size();
6249*0a6a1f1dSLionel Sambuc auto Result = TransformTypos(*this, Filter).Transform(E);
6250*0a6a1f1dSLionel Sambuc ExprEvalContexts.back().NumTypos = TyposInContext;
6251*0a6a1f1dSLionel Sambuc TyposResolved -= DelayedTypos.size();
6252*0a6a1f1dSLionel Sambuc if (Result.isInvalid() || Result.get() != E) {
6253*0a6a1f1dSLionel Sambuc ExprEvalContexts.back().NumTypos -= TyposResolved;
6254*0a6a1f1dSLionel Sambuc return Result;
6255*0a6a1f1dSLionel Sambuc }
6256*0a6a1f1dSLionel Sambuc assert(TyposResolved == 0 && "Corrected typo but got same Expr back?");
6257*0a6a1f1dSLionel Sambuc }
6258*0a6a1f1dSLionel Sambuc return E;
6259*0a6a1f1dSLionel Sambuc }
6260f4a2713aSLionel Sambuc
ActOnFinishFullExpr(Expr * FE,SourceLocation CC,bool DiscardedValue,bool IsConstexpr,bool IsLambdaInitCaptureInitializer)6261f4a2713aSLionel Sambuc ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC,
6262f4a2713aSLionel Sambuc bool DiscardedValue,
6263*0a6a1f1dSLionel Sambuc bool IsConstexpr,
6264*0a6a1f1dSLionel Sambuc bool IsLambdaInitCaptureInitializer) {
6265*0a6a1f1dSLionel Sambuc ExprResult FullExpr = FE;
6266f4a2713aSLionel Sambuc
6267f4a2713aSLionel Sambuc if (!FullExpr.get())
6268f4a2713aSLionel Sambuc return ExprError();
6269f4a2713aSLionel Sambuc
6270*0a6a1f1dSLionel Sambuc // If we are an init-expression in a lambdas init-capture, we should not
6271*0a6a1f1dSLionel Sambuc // diagnose an unexpanded pack now (will be diagnosed once lambda-expr
6272*0a6a1f1dSLionel Sambuc // containing full-expression is done).
6273*0a6a1f1dSLionel Sambuc // template<class ... Ts> void test(Ts ... t) {
6274*0a6a1f1dSLionel Sambuc // test([&a(t)]() { <-- (t) is an init-expr that shouldn't be diagnosed now.
6275*0a6a1f1dSLionel Sambuc // return a;
6276*0a6a1f1dSLionel Sambuc // }() ...);
6277*0a6a1f1dSLionel Sambuc // }
6278*0a6a1f1dSLionel Sambuc // FIXME: This is a hack. It would be better if we pushed the lambda scope
6279*0a6a1f1dSLionel Sambuc // when we parse the lambda introducer, and teach capturing (but not
6280*0a6a1f1dSLionel Sambuc // unexpanded pack detection) to walk over LambdaScopeInfos which don't have a
6281*0a6a1f1dSLionel Sambuc // corresponding class yet (that is, have LambdaScopeInfo either represent a
6282*0a6a1f1dSLionel Sambuc // lambda where we've entered the introducer but not the body, or represent a
6283*0a6a1f1dSLionel Sambuc // lambda where we've entered the body, depending on where the
6284*0a6a1f1dSLionel Sambuc // parser/instantiation has got to).
6285*0a6a1f1dSLionel Sambuc if (!IsLambdaInitCaptureInitializer &&
6286*0a6a1f1dSLionel Sambuc DiagnoseUnexpandedParameterPack(FullExpr.get()))
6287f4a2713aSLionel Sambuc return ExprError();
6288f4a2713aSLionel Sambuc
6289f4a2713aSLionel Sambuc // Top-level expressions default to 'id' when we're in a debugger.
6290f4a2713aSLionel Sambuc if (DiscardedValue && getLangOpts().DebuggerCastResultToId &&
6291f4a2713aSLionel Sambuc FullExpr.get()->getType() == Context.UnknownAnyTy) {
6292*0a6a1f1dSLionel Sambuc FullExpr = forceUnknownAnyToType(FullExpr.get(), Context.getObjCIdType());
6293f4a2713aSLionel Sambuc if (FullExpr.isInvalid())
6294f4a2713aSLionel Sambuc return ExprError();
6295f4a2713aSLionel Sambuc }
6296f4a2713aSLionel Sambuc
6297f4a2713aSLionel Sambuc if (DiscardedValue) {
6298*0a6a1f1dSLionel Sambuc FullExpr = CheckPlaceholderExpr(FullExpr.get());
6299f4a2713aSLionel Sambuc if (FullExpr.isInvalid())
6300f4a2713aSLionel Sambuc return ExprError();
6301f4a2713aSLionel Sambuc
6302*0a6a1f1dSLionel Sambuc FullExpr = IgnoredValueConversions(FullExpr.get());
6303f4a2713aSLionel Sambuc if (FullExpr.isInvalid())
6304f4a2713aSLionel Sambuc return ExprError();
6305f4a2713aSLionel Sambuc }
6306f4a2713aSLionel Sambuc
6307*0a6a1f1dSLionel Sambuc FullExpr = CorrectDelayedTyposInExpr(FullExpr.get());
6308*0a6a1f1dSLionel Sambuc if (FullExpr.isInvalid())
6309*0a6a1f1dSLionel Sambuc return ExprError();
6310*0a6a1f1dSLionel Sambuc
6311f4a2713aSLionel Sambuc CheckCompletedExpr(FullExpr.get(), CC, IsConstexpr);
6312f4a2713aSLionel Sambuc
6313f4a2713aSLionel Sambuc // At the end of this full expression (which could be a deeply nested
6314f4a2713aSLionel Sambuc // lambda), if there is a potential capture within the nested lambda,
6315f4a2713aSLionel Sambuc // have the outer capture-able lambda try and capture it.
6316f4a2713aSLionel Sambuc // Consider the following code:
6317f4a2713aSLionel Sambuc // void f(int, int);
6318f4a2713aSLionel Sambuc // void f(const int&, double);
6319f4a2713aSLionel Sambuc // void foo() {
6320f4a2713aSLionel Sambuc // const int x = 10, y = 20;
6321f4a2713aSLionel Sambuc // auto L = [=](auto a) {
6322f4a2713aSLionel Sambuc // auto M = [=](auto b) {
6323f4a2713aSLionel Sambuc // f(x, b); <-- requires x to be captured by L and M
6324f4a2713aSLionel Sambuc // f(y, a); <-- requires y to be captured by L, but not all Ms
6325f4a2713aSLionel Sambuc // };
6326f4a2713aSLionel Sambuc // };
6327f4a2713aSLionel Sambuc // }
6328f4a2713aSLionel Sambuc
6329f4a2713aSLionel Sambuc // FIXME: Also consider what happens for something like this that involves
6330f4a2713aSLionel Sambuc // the gnu-extension statement-expressions or even lambda-init-captures:
6331f4a2713aSLionel Sambuc // void f() {
6332f4a2713aSLionel Sambuc // const int n = 0;
6333f4a2713aSLionel Sambuc // auto L = [&](auto a) {
6334f4a2713aSLionel Sambuc // +n + ({ 0; a; });
6335f4a2713aSLionel Sambuc // };
6336f4a2713aSLionel Sambuc // }
6337f4a2713aSLionel Sambuc //
6338f4a2713aSLionel Sambuc // Here, we see +n, and then the full-expression 0; ends, so we don't
6339f4a2713aSLionel Sambuc // capture n (and instead remove it from our list of potential captures),
6340f4a2713aSLionel Sambuc // and then the full-expression +n + ({ 0; }); ends, but it's too late
6341f4a2713aSLionel Sambuc // for us to see that we need to capture n after all.
6342f4a2713aSLionel Sambuc
6343f4a2713aSLionel Sambuc LambdaScopeInfo *const CurrentLSI = getCurLambda();
6344f4a2713aSLionel Sambuc // FIXME: PR 17877 showed that getCurLambda() can return a valid pointer
6345f4a2713aSLionel Sambuc // even if CurContext is not a lambda call operator. Refer to that Bug Report
6346f4a2713aSLionel Sambuc // for an example of the code that might cause this asynchrony.
6347f4a2713aSLionel Sambuc // By ensuring we are in the context of a lambda's call operator
6348f4a2713aSLionel Sambuc // we can fix the bug (we only need to check whether we need to capture
6349f4a2713aSLionel Sambuc // if we are within a lambda's body); but per the comments in that
6350f4a2713aSLionel Sambuc // PR, a proper fix would entail :
6351f4a2713aSLionel Sambuc // "Alternative suggestion:
6352f4a2713aSLionel Sambuc // - Add to Sema an integer holding the smallest (outermost) scope
6353f4a2713aSLionel Sambuc // index that we are *lexically* within, and save/restore/set to
6354f4a2713aSLionel Sambuc // FunctionScopes.size() in InstantiatingTemplate's
6355f4a2713aSLionel Sambuc // constructor/destructor.
6356f4a2713aSLionel Sambuc // - Teach the handful of places that iterate over FunctionScopes to
6357f4a2713aSLionel Sambuc // stop at the outermost enclosing lexical scope."
6358f4a2713aSLionel Sambuc const bool IsInLambdaDeclContext = isLambdaCallOperator(CurContext);
6359f4a2713aSLionel Sambuc if (IsInLambdaDeclContext && CurrentLSI &&
6360f4a2713aSLionel Sambuc CurrentLSI->hasPotentialCaptures() && !FullExpr.isInvalid())
6361*0a6a1f1dSLionel Sambuc CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(FE, CurrentLSI,
6362*0a6a1f1dSLionel Sambuc *this);
6363f4a2713aSLionel Sambuc return MaybeCreateExprWithCleanups(FullExpr);
6364f4a2713aSLionel Sambuc }
6365f4a2713aSLionel Sambuc
ActOnFinishFullStmt(Stmt * FullStmt)6366f4a2713aSLionel Sambuc StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) {
6367f4a2713aSLionel Sambuc if (!FullStmt) return StmtError();
6368f4a2713aSLionel Sambuc
6369f4a2713aSLionel Sambuc return MaybeCreateStmtWithCleanups(FullStmt);
6370f4a2713aSLionel Sambuc }
6371f4a2713aSLionel Sambuc
6372f4a2713aSLionel Sambuc Sema::IfExistsResult
CheckMicrosoftIfExistsSymbol(Scope * S,CXXScopeSpec & SS,const DeclarationNameInfo & TargetNameInfo)6373f4a2713aSLionel Sambuc Sema::CheckMicrosoftIfExistsSymbol(Scope *S,
6374f4a2713aSLionel Sambuc CXXScopeSpec &SS,
6375f4a2713aSLionel Sambuc const DeclarationNameInfo &TargetNameInfo) {
6376f4a2713aSLionel Sambuc DeclarationName TargetName = TargetNameInfo.getName();
6377f4a2713aSLionel Sambuc if (!TargetName)
6378f4a2713aSLionel Sambuc return IER_DoesNotExist;
6379f4a2713aSLionel Sambuc
6380f4a2713aSLionel Sambuc // If the name itself is dependent, then the result is dependent.
6381f4a2713aSLionel Sambuc if (TargetName.isDependentName())
6382f4a2713aSLionel Sambuc return IER_Dependent;
6383f4a2713aSLionel Sambuc
6384f4a2713aSLionel Sambuc // Do the redeclaration lookup in the current scope.
6385f4a2713aSLionel Sambuc LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName,
6386f4a2713aSLionel Sambuc Sema::NotForRedeclaration);
6387f4a2713aSLionel Sambuc LookupParsedName(R, S, &SS);
6388f4a2713aSLionel Sambuc R.suppressDiagnostics();
6389f4a2713aSLionel Sambuc
6390f4a2713aSLionel Sambuc switch (R.getResultKind()) {
6391f4a2713aSLionel Sambuc case LookupResult::Found:
6392f4a2713aSLionel Sambuc case LookupResult::FoundOverloaded:
6393f4a2713aSLionel Sambuc case LookupResult::FoundUnresolvedValue:
6394f4a2713aSLionel Sambuc case LookupResult::Ambiguous:
6395f4a2713aSLionel Sambuc return IER_Exists;
6396f4a2713aSLionel Sambuc
6397f4a2713aSLionel Sambuc case LookupResult::NotFound:
6398f4a2713aSLionel Sambuc return IER_DoesNotExist;
6399f4a2713aSLionel Sambuc
6400f4a2713aSLionel Sambuc case LookupResult::NotFoundInCurrentInstantiation:
6401f4a2713aSLionel Sambuc return IER_Dependent;
6402f4a2713aSLionel Sambuc }
6403f4a2713aSLionel Sambuc
6404f4a2713aSLionel Sambuc llvm_unreachable("Invalid LookupResult Kind!");
6405f4a2713aSLionel Sambuc }
6406f4a2713aSLionel Sambuc
6407f4a2713aSLionel Sambuc Sema::IfExistsResult
CheckMicrosoftIfExistsSymbol(Scope * S,SourceLocation KeywordLoc,bool IsIfExists,CXXScopeSpec & SS,UnqualifiedId & Name)6408f4a2713aSLionel Sambuc Sema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc,
6409f4a2713aSLionel Sambuc bool IsIfExists, CXXScopeSpec &SS,
6410f4a2713aSLionel Sambuc UnqualifiedId &Name) {
6411f4a2713aSLionel Sambuc DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name);
6412f4a2713aSLionel Sambuc
6413f4a2713aSLionel Sambuc // Check for unexpanded parameter packs.
6414f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 4> Unexpanded;
6415f4a2713aSLionel Sambuc collectUnexpandedParameterPacks(SS, Unexpanded);
6416f4a2713aSLionel Sambuc collectUnexpandedParameterPacks(TargetNameInfo, Unexpanded);
6417f4a2713aSLionel Sambuc if (!Unexpanded.empty()) {
6418f4a2713aSLionel Sambuc DiagnoseUnexpandedParameterPacks(KeywordLoc,
6419f4a2713aSLionel Sambuc IsIfExists? UPPC_IfExists
6420f4a2713aSLionel Sambuc : UPPC_IfNotExists,
6421f4a2713aSLionel Sambuc Unexpanded);
6422f4a2713aSLionel Sambuc return IER_Error;
6423f4a2713aSLionel Sambuc }
6424f4a2713aSLionel Sambuc
6425f4a2713aSLionel Sambuc return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo);
6426f4a2713aSLionel Sambuc }
6427