blob: fb4ff7891fa899544d2f061500624a0466088c43 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for C++ expressions.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCall2a7fb272010-08-25 05:32:35 +000015#include "clang/Sema/DeclSpec.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
17#include "clang/Sema/Lookup.h"
John McCall2a7fb272010-08-25 05:32:35 +000018#include "clang/Sema/ParsedTemplate.h"
John McCall469a1eb2011-02-02 13:00:07 +000019#include "clang/Sema/ScopeInfo.h"
John McCall2a7fb272010-08-25 05:32:35 +000020#include "clang/Sema/TemplateDeduction.h"
Steve Naroff210679c2007-08-25 14:02:58 +000021#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000022#include "clang/AST/CXXInheritance.h"
John McCall7cd088e2010-08-24 07:21:54 +000023#include "clang/AST/DeclObjC.h"
Anders Carlssond497ba72009-08-26 22:59:12 +000024#include "clang/AST/ExprCXX.h"
Fariborz Jahaniand4266622010-06-16 18:56:04 +000025#include "clang/AST/ExprObjC.h"
Douglas Gregorb57fb492010-02-24 22:38:50 +000026#include "clang/AST/TypeLoc.h"
Anders Carlssond497ba72009-08-26 22:59:12 +000027#include "clang/Basic/PartialDiagnostic.h"
Sebastian Redlb5a57a62008-12-03 20:26:15 +000028#include "clang/Basic/TargetInfo.h"
Anders Carlssond497ba72009-08-26 22:59:12 +000029#include "clang/Lex/Preprocessor.h"
Douglas Gregor3fc749d2008-12-23 00:26:44 +000030#include "llvm/ADT/STLExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000031using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000032using namespace sema;
Reid Spencer5f016e22007-07-11 17:01:13 +000033
John McCallb3d87482010-08-24 05:47:05 +000034ParsedType Sema::getDestructorName(SourceLocation TildeLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000035 IdentifierInfo &II,
John McCallb3d87482010-08-24 05:47:05 +000036 SourceLocation NameLoc,
37 Scope *S, CXXScopeSpec &SS,
38 ParsedType ObjectTypePtr,
39 bool EnteringContext) {
Douglas Gregor124b8782010-02-16 19:09:40 +000040 // Determine where to perform name lookup.
41
42 // FIXME: This area of the standard is very messy, and the current
43 // wording is rather unclear about which scopes we search for the
44 // destructor name; see core issues 399 and 555. Issue 399 in
45 // particular shows where the current description of destructor name
46 // lookup is completely out of line with existing practice, e.g.,
47 // this appears to be ill-formed:
48 //
49 // namespace N {
50 // template <typename T> struct S {
51 // ~S();
52 // };
53 // }
54 //
55 // void f(N::S<int>* s) {
56 // s->N::S<int>::~S();
57 // }
58 //
Douglas Gregor93649fd2010-02-23 00:15:22 +000059 // See also PR6358 and PR6359.
Sebastian Redlc0fee502010-07-07 23:17:38 +000060 // For this reason, we're currently only doing the C++03 version of this
61 // code; the C++0x version has to wait until we get a proper spec.
Douglas Gregor124b8782010-02-16 19:09:40 +000062 QualType SearchType;
63 DeclContext *LookupCtx = 0;
64 bool isDependent = false;
65 bool LookInScope = false;
66
67 // If we have an object type, it's because we are in a
68 // pseudo-destructor-expression or a member access expression, and
69 // we know what type we're looking for.
70 if (ObjectTypePtr)
71 SearchType = GetTypeFromParser(ObjectTypePtr);
72
73 if (SS.isSet()) {
Douglas Gregor93649fd2010-02-23 00:15:22 +000074 NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000075
Douglas Gregor93649fd2010-02-23 00:15:22 +000076 bool AlreadySearched = false;
77 bool LookAtPrefix = true;
Sebastian Redlc0fee502010-07-07 23:17:38 +000078 // C++ [basic.lookup.qual]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000079 // If a pseudo-destructor-name (5.2.4) contains a nested-name-specifier,
Sebastian Redlc0fee502010-07-07 23:17:38 +000080 // the type-names are looked up as types in the scope designated by the
81 // nested-name-specifier. In a qualified-id of the form:
NAKAMURA Takumi00995302011-01-27 07:09:49 +000082 //
83 // ::[opt] nested-name-specifier ~ class-name
Sebastian Redlc0fee502010-07-07 23:17:38 +000084 //
85 // where the nested-name-specifier designates a namespace scope, and in
Chandler Carruth5e895a82010-02-21 10:19:54 +000086 // a qualified-id of the form:
Douglas Gregor124b8782010-02-16 19:09:40 +000087 //
NAKAMURA Takumi00995302011-01-27 07:09:49 +000088 // ::opt nested-name-specifier class-name :: ~ class-name
Douglas Gregor124b8782010-02-16 19:09:40 +000089 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000090 // the class-names are looked up as types in the scope designated by
Sebastian Redlc0fee502010-07-07 23:17:38 +000091 // the nested-name-specifier.
Douglas Gregor124b8782010-02-16 19:09:40 +000092 //
Sebastian Redlc0fee502010-07-07 23:17:38 +000093 // Here, we check the first case (completely) and determine whether the
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000094 // code below is permitted to look at the prefix of the
Sebastian Redlc0fee502010-07-07 23:17:38 +000095 // nested-name-specifier.
96 DeclContext *DC = computeDeclContext(SS, EnteringContext);
97 if (DC && DC->isFileContext()) {
98 AlreadySearched = true;
99 LookupCtx = DC;
100 isDependent = false;
101 } else if (DC && isa<CXXRecordDecl>(DC))
102 LookAtPrefix = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000103
Sebastian Redlc0fee502010-07-07 23:17:38 +0000104 // The second case from the C++03 rules quoted further above.
Douglas Gregor93649fd2010-02-23 00:15:22 +0000105 NestedNameSpecifier *Prefix = 0;
106 if (AlreadySearched) {
107 // Nothing left to do.
108 } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) {
109 CXXScopeSpec PrefixSS;
Douglas Gregor7e384942011-02-25 16:07:42 +0000110 PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data()));
Douglas Gregor93649fd2010-02-23 00:15:22 +0000111 LookupCtx = computeDeclContext(PrefixSS, EnteringContext);
112 isDependent = isDependentScopeSpecifier(PrefixSS);
Douglas Gregor93649fd2010-02-23 00:15:22 +0000113 } else if (ObjectTypePtr) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000114 LookupCtx = computeDeclContext(SearchType);
115 isDependent = SearchType->isDependentType();
116 } else {
117 LookupCtx = computeDeclContext(SS, EnteringContext);
Douglas Gregor93649fd2010-02-23 00:15:22 +0000118 isDependent = LookupCtx && LookupCtx->isDependentContext();
Douglas Gregor124b8782010-02-16 19:09:40 +0000119 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000120
Douglas Gregoredc90502010-02-25 04:46:04 +0000121 LookInScope = false;
Douglas Gregor124b8782010-02-16 19:09:40 +0000122 } else if (ObjectTypePtr) {
123 // C++ [basic.lookup.classref]p3:
124 // If the unqualified-id is ~type-name, the type-name is looked up
125 // in the context of the entire postfix-expression. If the type T
126 // of the object expression is of a class type C, the type-name is
127 // also looked up in the scope of class C. At least one of the
128 // lookups shall find a name that refers to (possibly
129 // cv-qualified) T.
130 LookupCtx = computeDeclContext(SearchType);
131 isDependent = SearchType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000132 assert((isDependent || !SearchType->isIncompleteType()) &&
Douglas Gregor124b8782010-02-16 19:09:40 +0000133 "Caller should have completed object type");
134
135 LookInScope = true;
136 } else {
137 // Perform lookup into the current scope (only).
138 LookInScope = true;
139 }
140
Douglas Gregor7ec18732011-03-04 22:32:08 +0000141 TypeDecl *NonMatchingTypeDecl = 0;
Douglas Gregor124b8782010-02-16 19:09:40 +0000142 LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName);
143 for (unsigned Step = 0; Step != 2; ++Step) {
144 // Look for the name first in the computed lookup context (if we
Douglas Gregor7ec18732011-03-04 22:32:08 +0000145 // have one) and, if that fails to find a match, in the scope (if
Douglas Gregor124b8782010-02-16 19:09:40 +0000146 // we're allowed to look there).
147 Found.clear();
148 if (Step == 0 && LookupCtx)
149 LookupQualifiedName(Found, LookupCtx);
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000150 else if (Step == 1 && LookInScope && S)
Douglas Gregor124b8782010-02-16 19:09:40 +0000151 LookupName(Found, S);
152 else
153 continue;
154
155 // FIXME: Should we be suppressing ambiguities here?
156 if (Found.isAmbiguous())
John McCallb3d87482010-08-24 05:47:05 +0000157 return ParsedType();
Douglas Gregor124b8782010-02-16 19:09:40 +0000158
159 if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) {
160 QualType T = Context.getTypeDeclType(Type);
Douglas Gregor124b8782010-02-16 19:09:40 +0000161
162 if (SearchType.isNull() || SearchType->isDependentType() ||
163 Context.hasSameUnqualifiedType(T, SearchType)) {
164 // We found our type!
165
John McCallb3d87482010-08-24 05:47:05 +0000166 return ParsedType::make(T);
Douglas Gregor124b8782010-02-16 19:09:40 +0000167 }
John Wiegley36784e72011-03-08 08:13:22 +0000168
Douglas Gregor7ec18732011-03-04 22:32:08 +0000169 if (!SearchType.isNull())
170 NonMatchingTypeDecl = Type;
Douglas Gregor124b8782010-02-16 19:09:40 +0000171 }
172
173 // If the name that we found is a class template name, and it is
174 // the same name as the template name in the last part of the
175 // nested-name-specifier (if present) or the object type, then
176 // this is the destructor for that class.
177 // FIXME: This is a workaround until we get real drafting for core
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000178 // issue 399, for which there isn't even an obvious direction.
Douglas Gregor124b8782010-02-16 19:09:40 +0000179 if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) {
180 QualType MemberOfType;
181 if (SS.isSet()) {
182 if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) {
183 // Figure out the type of the context, if it has one.
John McCall3cb0ebd2010-03-10 03:28:59 +0000184 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx))
185 MemberOfType = Context.getTypeDeclType(Record);
Douglas Gregor124b8782010-02-16 19:09:40 +0000186 }
187 }
188 if (MemberOfType.isNull())
189 MemberOfType = SearchType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000190
Douglas Gregor124b8782010-02-16 19:09:40 +0000191 if (MemberOfType.isNull())
192 continue;
193
194 // We're referring into a class template specialization. If the
195 // class template we found is the same as the template being
196 // specialized, we found what we are looking for.
197 if (const RecordType *Record = MemberOfType->getAs<RecordType>()) {
198 if (ClassTemplateSpecializationDecl *Spec
199 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
200 if (Spec->getSpecializedTemplate()->getCanonicalDecl() ==
201 Template->getCanonicalDecl())
John McCallb3d87482010-08-24 05:47:05 +0000202 return ParsedType::make(MemberOfType);
Douglas Gregor124b8782010-02-16 19:09:40 +0000203 }
204
205 continue;
206 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000207
Douglas Gregor124b8782010-02-16 19:09:40 +0000208 // We're referring to an unresolved class template
209 // specialization. Determine whether we class template we found
210 // is the same as the template being specialized or, if we don't
211 // know which template is being specialized, that it at least
212 // has the same name.
213 if (const TemplateSpecializationType *SpecType
214 = MemberOfType->getAs<TemplateSpecializationType>()) {
215 TemplateName SpecName = SpecType->getTemplateName();
216
217 // The class template we found is the same template being
218 // specialized.
219 if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) {
220 if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl())
John McCallb3d87482010-08-24 05:47:05 +0000221 return ParsedType::make(MemberOfType);
Douglas Gregor124b8782010-02-16 19:09:40 +0000222
223 continue;
224 }
225
226 // The class template we found has the same name as the
227 // (dependent) template name being specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000228 if (DependentTemplateName *DepTemplate
Douglas Gregor124b8782010-02-16 19:09:40 +0000229 = SpecName.getAsDependentTemplateName()) {
230 if (DepTemplate->isIdentifier() &&
231 DepTemplate->getIdentifier() == Template->getIdentifier())
John McCallb3d87482010-08-24 05:47:05 +0000232 return ParsedType::make(MemberOfType);
Douglas Gregor124b8782010-02-16 19:09:40 +0000233
234 continue;
235 }
236 }
237 }
238 }
239
240 if (isDependent) {
241 // We didn't find our type, but that's okay: it's dependent
242 // anyway.
Douglas Gregore29425b2011-02-28 22:42:13 +0000243
244 // FIXME: What if we have no nested-name-specifier?
245 QualType T = CheckTypenameType(ETK_None, SourceLocation(),
246 SS.getWithLocInContext(Context),
247 II, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000248 return ParsedType::make(T);
Douglas Gregor124b8782010-02-16 19:09:40 +0000249 }
250
Douglas Gregor7ec18732011-03-04 22:32:08 +0000251 if (NonMatchingTypeDecl) {
252 QualType T = Context.getTypeDeclType(NonMatchingTypeDecl);
253 Diag(NameLoc, diag::err_destructor_expr_type_mismatch)
254 << T << SearchType;
255 Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here)
256 << T;
257 } else if (ObjectTypePtr)
258 Diag(NameLoc, diag::err_ident_in_dtor_not_a_type)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000259 << &II;
Douglas Gregor124b8782010-02-16 19:09:40 +0000260 else
261 Diag(NameLoc, diag::err_destructor_class_name);
262
John McCallb3d87482010-08-24 05:47:05 +0000263 return ParsedType();
Douglas Gregor124b8782010-02-16 19:09:40 +0000264}
265
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000266/// \brief Build a C++ typeid expression with a type operand.
John McCall60d7b3a2010-08-24 06:29:42 +0000267ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType,
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000268 SourceLocation TypeidLoc,
269 TypeSourceInfo *Operand,
270 SourceLocation RParenLoc) {
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000271 // C++ [expr.typeid]p4:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000272 // The top-level cv-qualifiers of the lvalue expression or the type-id
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000273 // that is the operand of typeid are always ignored.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000274 // If the type of the type-id is a class type or a reference to a class
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000275 // type, the class shall be completely-defined.
Douglas Gregord1c1d7b2010-06-02 06:16:02 +0000276 Qualifiers Quals;
277 QualType T
278 = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(),
279 Quals);
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000280 if (T->getAs<RecordType>() &&
281 RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid))
282 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000283
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000284 return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(),
285 Operand,
286 SourceRange(TypeidLoc, RParenLoc)));
287}
288
289/// \brief Build a C++ typeid expression with an expression operand.
John McCall60d7b3a2010-08-24 06:29:42 +0000290ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType,
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000291 SourceLocation TypeidLoc,
292 Expr *E,
293 SourceLocation RParenLoc) {
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000294 bool isUnevaluatedOperand = true;
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000295 if (E && !E->isTypeDependent()) {
296 QualType T = E->getType();
297 if (const RecordType *RecordT = T->getAs<RecordType>()) {
298 CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl());
299 // C++ [expr.typeid]p3:
300 // [...] If the type of the expression is a class type, the class
301 // shall be completely-defined.
302 if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid))
303 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000304
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000305 // C++ [expr.typeid]p3:
Sebastian Redl906082e2010-07-20 04:20:21 +0000306 // When typeid is applied to an expression other than an glvalue of a
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000307 // polymorphic class type [...] [the] expression is an unevaluated
308 // operand. [...]
Sebastian Redl906082e2010-07-20 04:20:21 +0000309 if (RecordD->isPolymorphic() && E->Classify(Context).isGLValue()) {
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000310 isUnevaluatedOperand = false;
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000311
312 // We require a vtable to query the type at run time.
313 MarkVTableUsed(TypeidLoc, RecordD);
314 }
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000315 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000316
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000317 // C++ [expr.typeid]p4:
318 // [...] If the type of the type-id is a reference to a possibly
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000319 // cv-qualified type, the result of the typeid expression refers to a
320 // std::type_info object representing the cv-unqualified referenced
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000321 // type.
Douglas Gregord1c1d7b2010-06-02 06:16:02 +0000322 Qualifiers Quals;
323 QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals);
324 if (!Context.hasSameType(T, UnqualT)) {
325 T = UnqualT;
John Wiegley429bb272011-04-08 18:41:53 +0000326 E = ImpCastExprToType(E, UnqualT, CK_NoOp, CastCategory(E)).take();
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000327 }
328 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000329
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000330 // If this is an unevaluated operand, clear out the set of
331 // declaration references we have been computing and eliminate any
332 // temporaries introduced in its computation.
333 if (isUnevaluatedOperand)
334 ExprEvalContexts.back().Context = Unevaluated;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000335
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000336 return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(),
John McCall9ae2f072010-08-23 23:25:46 +0000337 E,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000338 SourceRange(TypeidLoc, RParenLoc)));
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000339}
340
341/// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression);
John McCall60d7b3a2010-08-24 06:29:42 +0000342ExprResult
Sebastian Redlc42e1182008-11-11 11:37:55 +0000343Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc,
344 bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000345 // Find the std::type_info type.
Sebastian Redlce0682f2011-03-31 19:29:24 +0000346 if (!getStdNamespace())
Sebastian Redlf53597f2009-03-15 17:47:39 +0000347 return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000348
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000349 if (!CXXTypeInfoDecl) {
350 IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info");
351 LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName);
352 LookupQualifiedName(R, getStdNamespace());
353 CXXTypeInfoDecl = R.getAsSingle<RecordDecl>();
354 if (!CXXTypeInfoDecl)
355 return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
356 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000357
Douglas Gregor4eb4f0f2010-09-08 23:14:30 +0000358 QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000359
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000360 if (isType) {
361 // The operand is a type; handle it as such.
362 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +0000363 QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr),
364 &TInfo);
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000365 if (T.isNull())
366 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000367
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000368 if (!TInfo)
369 TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000370
Douglas Gregor57fdc8a2010-04-26 22:37:10 +0000371 return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000374 // The operand is an expression.
John McCall9ae2f072010-08-23 23:25:46 +0000375 return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000376}
377
Francois Pichet6915c522010-12-27 01:32:00 +0000378/// Retrieve the UuidAttr associated with QT.
379static UuidAttr *GetUuidAttrOfType(QualType QT) {
380 // Optionally remove one level of pointer, reference or array indirection.
John McCallf4c73712011-01-19 06:33:43 +0000381 const Type *Ty = QT.getTypePtr();;
Francois Pichet913b7bf2010-12-20 03:51:03 +0000382 if (QT->isPointerType() || QT->isReferenceType())
383 Ty = QT->getPointeeType().getTypePtr();
384 else if (QT->isArrayType())
385 Ty = cast<ArrayType>(QT)->getElementType().getTypePtr();
386
Francois Pichet6915c522010-12-27 01:32:00 +0000387 // Loop all class definition and declaration looking for an uuid attribute.
388 CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
389 while (RD) {
390 if (UuidAttr *Uuid = RD->getAttr<UuidAttr>())
391 return Uuid;
392 RD = RD->getPreviousDeclaration();
393 }
394 return 0;
Francois Pichet913b7bf2010-12-20 03:51:03 +0000395}
396
Francois Pichet01b7c302010-09-08 12:20:18 +0000397/// \brief Build a Microsoft __uuidof expression with a type operand.
398ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType,
399 SourceLocation TypeidLoc,
400 TypeSourceInfo *Operand,
401 SourceLocation RParenLoc) {
Francois Pichet6915c522010-12-27 01:32:00 +0000402 if (!Operand->getType()->isDependentType()) {
403 if (!GetUuidAttrOfType(Operand->getType()))
404 return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid));
405 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000406
Francois Pichet01b7c302010-09-08 12:20:18 +0000407 // FIXME: add __uuidof semantic analysis for type operand.
408 return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(),
409 Operand,
410 SourceRange(TypeidLoc, RParenLoc)));
411}
412
413/// \brief Build a Microsoft __uuidof expression with an expression operand.
414ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType,
415 SourceLocation TypeidLoc,
416 Expr *E,
417 SourceLocation RParenLoc) {
Francois Pichet6915c522010-12-27 01:32:00 +0000418 if (!E->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000419 if (!GetUuidAttrOfType(E->getType()) &&
Francois Pichet6915c522010-12-27 01:32:00 +0000420 !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
421 return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid));
422 }
423 // FIXME: add __uuidof semantic analysis for type operand.
Francois Pichet01b7c302010-09-08 12:20:18 +0000424 return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(),
425 E,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000426 SourceRange(TypeidLoc, RParenLoc)));
Francois Pichet01b7c302010-09-08 12:20:18 +0000427}
428
429/// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression);
430ExprResult
431Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc,
432 bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000433 // If MSVCGuidDecl has not been cached, do the lookup.
Francois Pichet01b7c302010-09-08 12:20:18 +0000434 if (!MSVCGuidDecl) {
435 IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID");
436 LookupResult R(*this, GuidII, SourceLocation(), LookupTagName);
437 LookupQualifiedName(R, Context.getTranslationUnitDecl());
438 MSVCGuidDecl = R.getAsSingle<RecordDecl>();
439 if (!MSVCGuidDecl)
440 return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000441 }
442
Francois Pichet01b7c302010-09-08 12:20:18 +0000443 QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000444
Francois Pichet01b7c302010-09-08 12:20:18 +0000445 if (isType) {
446 // The operand is a type; handle it as such.
447 TypeSourceInfo *TInfo = 0;
448 QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr),
449 &TInfo);
450 if (T.isNull())
451 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000452
Francois Pichet01b7c302010-09-08 12:20:18 +0000453 if (!TInfo)
454 TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc);
455
456 return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc);
457 }
458
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000459 // The operand is an expression.
Francois Pichet01b7c302010-09-08 12:20:18 +0000460 return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc);
461}
462
Steve Naroff1b273c42007-09-16 14:56:35 +0000463/// ActOnCXXBoolLiteral - Parse {true,false} literals.
John McCall60d7b3a2010-08-24 06:29:42 +0000464ExprResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000465Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
Douglas Gregor2f639b92008-10-24 15:36:09 +0000466 assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 "Unknown C++ Boolean value!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000468 return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true,
469 Context.BoolTy, OpLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000470}
Chris Lattner50dd2892008-02-26 00:51:44 +0000471
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000472/// ActOnCXXNullPtrLiteral - Parse 'nullptr'.
John McCall60d7b3a2010-08-24 06:29:42 +0000473ExprResult
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000474Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) {
475 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
476}
477
Chris Lattner50dd2892008-02-26 00:51:44 +0000478/// ActOnCXXThrow - Parse throw expressions.
John McCall60d7b3a2010-08-24 06:29:42 +0000479ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000480Sema::ActOnCXXThrow(SourceLocation OpLoc, Expr *Ex) {
Anders Carlsson729b8532011-02-23 03:46:46 +0000481 // Don't report an error if 'throw' is used in system headers.
Anders Carlsson15348ae2011-02-28 02:27:16 +0000482 if (!getLangOptions().CXXExceptions &&
Anders Carlsson729b8532011-02-23 03:46:46 +0000483 !getSourceManager().isInSystemHeader(OpLoc))
Anders Carlssonb1fba312011-02-19 21:53:09 +0000484 Diag(OpLoc, diag::err_exceptions_disabled) << "throw";
Anders Carlsson7f11d9c2011-02-19 19:26:44 +0000485
John Wiegley429bb272011-04-08 18:41:53 +0000486 if (Ex && !Ex->isTypeDependent()) {
487 ExprResult ExRes = CheckCXXThrowOperand(OpLoc, Ex);
488 if (ExRes.isInvalid())
489 return ExprError();
490 Ex = ExRes.take();
491 }
Sebastian Redl972041f2009-04-27 20:27:31 +0000492 return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc));
493}
494
495/// CheckCXXThrowOperand - Validate the operand of a throw.
John Wiegley429bb272011-04-08 18:41:53 +0000496ExprResult Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *E) {
Sebastian Redl972041f2009-04-27 20:27:31 +0000497 // C++ [except.throw]p3:
Douglas Gregor154fe982009-12-23 22:04:40 +0000498 // A throw-expression initializes a temporary object, called the exception
499 // object, the type of which is determined by removing any top-level
500 // cv-qualifiers from the static type of the operand of throw and adjusting
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000501 // the type from "array of T" or "function returning T" to "pointer to T"
Douglas Gregor154fe982009-12-23 22:04:40 +0000502 // or "pointer to function returning T", [...]
503 if (E->getType().hasQualifiers())
John Wiegley429bb272011-04-08 18:41:53 +0000504 E = ImpCastExprToType(E, E->getType().getUnqualifiedType(), CK_NoOp,
505 CastCategory(E)).take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000506
John Wiegley429bb272011-04-08 18:41:53 +0000507 ExprResult Res = DefaultFunctionArrayConversion(E);
508 if (Res.isInvalid())
509 return ExprError();
510 E = Res.take();
Sebastian Redl972041f2009-04-27 20:27:31 +0000511
512 // If the type of the exception would be an incomplete type or a pointer
513 // to an incomplete type other than (cv) void the program is ill-formed.
514 QualType Ty = E->getType();
John McCallac418162010-04-22 01:10:34 +0000515 bool isPointer = false;
Ted Kremenek6217b802009-07-29 21:53:49 +0000516 if (const PointerType* Ptr = Ty->getAs<PointerType>()) {
Sebastian Redl972041f2009-04-27 20:27:31 +0000517 Ty = Ptr->getPointeeType();
John McCallac418162010-04-22 01:10:34 +0000518 isPointer = true;
Sebastian Redl972041f2009-04-27 20:27:31 +0000519 }
520 if (!isPointer || !Ty->isVoidType()) {
521 if (RequireCompleteType(ThrowLoc, Ty,
Anders Carlssond497ba72009-08-26 22:59:12 +0000522 PDiag(isPointer ? diag::err_throw_incomplete_ptr
523 : diag::err_throw_incomplete)
524 << E->getSourceRange()))
John Wiegley429bb272011-04-08 18:41:53 +0000525 return ExprError();
Rafael Espindola7b9a5aa2010-03-02 21:28:26 +0000526
Douglas Gregorbf422f92010-04-15 18:05:39 +0000527 if (RequireNonAbstractType(ThrowLoc, E->getType(),
528 PDiag(diag::err_throw_abstract_type)
529 << E->getSourceRange()))
John Wiegley429bb272011-04-08 18:41:53 +0000530 return ExprError();
Sebastian Redl972041f2009-04-27 20:27:31 +0000531 }
532
John McCallac418162010-04-22 01:10:34 +0000533 // Initialize the exception result. This implicitly weeds out
534 // abstract types or types with inaccessible copy constructors.
Douglas Gregor72dfa272011-01-21 22:46:35 +0000535 const VarDecl *NRVOVariable = getCopyElisionCandidate(QualType(), E, false);
536
Douglas Gregorf5d8f462011-01-21 18:05:27 +0000537 // FIXME: Determine whether we can elide this copy per C++0x [class.copy]p32.
John McCallac418162010-04-22 01:10:34 +0000538 InitializedEntity Entity =
Douglas Gregor72dfa272011-01-21 22:46:35 +0000539 InitializedEntity::InitializeException(ThrowLoc, E->getType(),
540 /*NRVO=*/false);
John Wiegley429bb272011-04-08 18:41:53 +0000541 Res = PerformMoveOrCopyInitialization(Entity, NRVOVariable,
542 QualType(), E);
John McCallac418162010-04-22 01:10:34 +0000543 if (Res.isInvalid())
John Wiegley429bb272011-04-08 18:41:53 +0000544 return ExprError();
545 E = Res.take();
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000546
Eli Friedman5ed9b932010-06-03 20:39:03 +0000547 // If the exception has class type, we need additional handling.
548 const RecordType *RecordTy = Ty->getAs<RecordType>();
549 if (!RecordTy)
John Wiegley429bb272011-04-08 18:41:53 +0000550 return Owned(E);
Eli Friedman5ed9b932010-06-03 20:39:03 +0000551 CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
552
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000553 // If we are throwing a polymorphic class type or pointer thereof,
554 // exception handling will make use of the vtable.
Eli Friedman5ed9b932010-06-03 20:39:03 +0000555 MarkVTableUsed(ThrowLoc, RD);
556
Eli Friedman98efb9f2010-10-12 20:32:36 +0000557 // If a pointer is thrown, the referenced object will not be destroyed.
558 if (isPointer)
John Wiegley429bb272011-04-08 18:41:53 +0000559 return Owned(E);
Eli Friedman98efb9f2010-10-12 20:32:36 +0000560
Eli Friedman5ed9b932010-06-03 20:39:03 +0000561 // If the class has a non-trivial destructor, we must be able to call it.
562 if (RD->hasTrivialDestructor())
John Wiegley429bb272011-04-08 18:41:53 +0000563 return Owned(E);
Eli Friedman5ed9b932010-06-03 20:39:03 +0000564
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000565 CXXDestructorDecl *Destructor
Douglas Gregordb89f282010-07-01 22:47:18 +0000566 = const_cast<CXXDestructorDecl*>(LookupDestructor(RD));
Eli Friedman5ed9b932010-06-03 20:39:03 +0000567 if (!Destructor)
John Wiegley429bb272011-04-08 18:41:53 +0000568 return Owned(E);
Eli Friedman5ed9b932010-06-03 20:39:03 +0000569
570 MarkDeclarationReferenced(E->getExprLoc(), Destructor);
571 CheckDestructorAccess(E->getExprLoc(), Destructor,
Douglas Gregored8abf12010-07-08 06:14:04 +0000572 PDiag(diag::err_access_dtor_exception) << Ty);
John Wiegley429bb272011-04-08 18:41:53 +0000573 return Owned(E);
Chris Lattner50dd2892008-02-26 00:51:44 +0000574}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000575
John McCall5808ce42011-02-03 08:15:49 +0000576CXXMethodDecl *Sema::tryCaptureCXXThis() {
577 // Ignore block scopes: we can capture through them.
578 // Ignore nested enum scopes: we'll diagnose non-constant expressions
579 // where they're invalid, and other uses are legitimate.
580 // Don't ignore nested class scopes: you can't use 'this' in a local class.
John McCall469a1eb2011-02-02 13:00:07 +0000581 DeclContext *DC = CurContext;
John McCall5808ce42011-02-03 08:15:49 +0000582 while (true) {
583 if (isa<BlockDecl>(DC)) DC = cast<BlockDecl>(DC)->getDeclContext();
584 else if (isa<EnumDecl>(DC)) DC = cast<EnumDecl>(DC)->getDeclContext();
585 else break;
586 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000587
John McCall5808ce42011-02-03 08:15:49 +0000588 // If we're not in an instance method, error out.
John McCall469a1eb2011-02-02 13:00:07 +0000589 CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC);
590 if (!method || !method->isInstance())
John McCall5808ce42011-02-03 08:15:49 +0000591 return 0;
John McCall469a1eb2011-02-02 13:00:07 +0000592
593 // Mark that we're closing on 'this' in all the block scopes, if applicable.
594 for (unsigned idx = FunctionScopes.size() - 1;
595 isa<BlockScopeInfo>(FunctionScopes[idx]);
596 --idx)
597 cast<BlockScopeInfo>(FunctionScopes[idx])->CapturesCXXThis = true;
598
John McCall5808ce42011-02-03 08:15:49 +0000599 return method;
600}
601
602ExprResult Sema::ActOnCXXThis(SourceLocation loc) {
603 /// C++ 9.3.2: In the body of a non-static member function, the keyword this
604 /// is a non-lvalue expression whose value is the address of the object for
605 /// which the function is called.
606
607 CXXMethodDecl *method = tryCaptureCXXThis();
608 if (!method) return Diag(loc, diag::err_invalid_this_use);
609
610 return Owned(new (Context) CXXThisExpr(loc, method->getThisType(Context),
John McCall469a1eb2011-02-02 13:00:07 +0000611 /*isImplicit=*/false));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000612}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000613
John McCall60d7b3a2010-08-24 06:29:42 +0000614ExprResult
Douglas Gregorab6677e2010-09-08 00:15:04 +0000615Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep,
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000616 SourceLocation LParenLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000617 MultiExprArg exprs,
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000618 SourceLocation RParenLoc) {
Douglas Gregorae4c77d2010-02-05 19:11:37 +0000619 if (!TypeRep)
620 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000621
John McCall9d125032010-01-15 18:39:57 +0000622 TypeSourceInfo *TInfo;
623 QualType Ty = GetTypeFromParser(TypeRep, &TInfo);
624 if (!TInfo)
625 TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation());
Douglas Gregorab6677e2010-09-08 00:15:04 +0000626
627 return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc);
628}
629
630/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
631/// Can be interpreted either as function-style casting ("int(x)")
632/// or class type construction ("ClassType(x,y,z)")
633/// or creation of a value-initialized type ("int()").
634ExprResult
635Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
636 SourceLocation LParenLoc,
637 MultiExprArg exprs,
638 SourceLocation RParenLoc) {
639 QualType Ty = TInfo->getType();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000640 unsigned NumExprs = exprs.size();
641 Expr **Exprs = (Expr**)exprs.get();
Douglas Gregorab6677e2010-09-08 00:15:04 +0000642 SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000643 SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
644
Sebastian Redlf53597f2009-03-15 17:47:39 +0000645 if (Ty->isDependentType() ||
Douglas Gregorba498172009-03-13 21:01:28 +0000646 CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) {
Sebastian Redlf53597f2009-03-15 17:47:39 +0000647 exprs.release();
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Douglas Gregorab6677e2010-09-08 00:15:04 +0000649 return Owned(CXXUnresolvedConstructExpr::Create(Context, TInfo,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000650 LParenLoc,
651 Exprs, NumExprs,
652 RParenLoc));
Douglas Gregorba498172009-03-13 21:01:28 +0000653 }
654
Anders Carlssonbb60a502009-08-27 03:53:50 +0000655 if (Ty->isArrayType())
656 return ExprError(Diag(TyBeginLoc,
657 diag::err_value_init_for_array_type) << FullRange);
658 if (!Ty->isVoidType() &&
659 RequireCompleteType(TyBeginLoc, Ty,
660 PDiag(diag::err_invalid_incomplete_type_use)
661 << FullRange))
662 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000663
Anders Carlssonbb60a502009-08-27 03:53:50 +0000664 if (RequireNonAbstractType(TyBeginLoc, Ty,
665 diag::err_allocation_of_abstract_type))
666 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000667
668
Douglas Gregor506ae412009-01-16 18:33:17 +0000669 // C++ [expr.type.conv]p1:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000670 // If the expression list is a single expression, the type conversion
671 // expression is equivalent (in definedness, and if defined in meaning) to the
672 // corresponding cast expression.
673 //
674 if (NumExprs == 1) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000675 CastKind Kind = CK_Invalid;
John McCallf89e55a2010-11-18 06:31:45 +0000676 ExprValueKind VK = VK_RValue;
John McCallf871d0c2010-08-07 06:22:56 +0000677 CXXCastPath BasePath;
John Wiegley429bb272011-04-08 18:41:53 +0000678 ExprResult CastExpr =
679 CheckCastTypes(TInfo->getTypeLoc().getSourceRange(), Ty, Exprs[0],
680 Kind, VK, BasePath,
681 /*FunctionalStyle=*/true);
682 if (CastExpr.isInvalid())
Sebastian Redlf53597f2009-03-15 17:47:39 +0000683 return ExprError();
John Wiegley429bb272011-04-08 18:41:53 +0000684 Exprs[0] = CastExpr.take();
Anders Carlsson0aebc812009-09-09 21:33:21 +0000685
686 exprs.release();
Anders Carlsson0aebc812009-09-09 21:33:21 +0000687
John McCallf871d0c2010-08-07 06:22:56 +0000688 return Owned(CXXFunctionalCastExpr::Create(Context,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000689 Ty.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000690 VK, TInfo, TyBeginLoc, Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000691 Exprs[0], &BasePath,
692 RParenLoc));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000693 }
694
Douglas Gregor19311e72010-09-08 21:40:08 +0000695 InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo);
696 InitializationKind Kind
697 = NumExprs ? InitializationKind::CreateDirect(TyBeginLoc,
698 LParenLoc, RParenLoc)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000699 : InitializationKind::CreateValue(TyBeginLoc,
Douglas Gregor19311e72010-09-08 21:40:08 +0000700 LParenLoc, RParenLoc);
701 InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs);
702 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(exprs));
Sebastian Redlf53597f2009-03-15 17:47:39 +0000703
Douglas Gregor19311e72010-09-08 21:40:08 +0000704 // FIXME: Improve AST representation?
705 return move(Result);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000706}
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000707
John McCall6ec278d2011-01-27 09:37:56 +0000708/// doesUsualArrayDeleteWantSize - Answers whether the usual
709/// operator delete[] for the given type has a size_t parameter.
710static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc,
711 QualType allocType) {
712 const RecordType *record =
713 allocType->getBaseElementTypeUnsafe()->getAs<RecordType>();
714 if (!record) return false;
715
716 // Try to find an operator delete[] in class scope.
717
718 DeclarationName deleteName =
719 S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete);
720 LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName);
721 S.LookupQualifiedName(ops, record->getDecl());
722
723 // We're just doing this for information.
724 ops.suppressDiagnostics();
725
726 // Very likely: there's no operator delete[].
727 if (ops.empty()) return false;
728
729 // If it's ambiguous, it should be illegal to call operator delete[]
730 // on this thing, so it doesn't matter if we allocate extra space or not.
731 if (ops.isAmbiguous()) return false;
732
733 LookupResult::Filter filter = ops.makeFilter();
734 while (filter.hasNext()) {
735 NamedDecl *del = filter.next()->getUnderlyingDecl();
736
737 // C++0x [basic.stc.dynamic.deallocation]p2:
738 // A template instance is never a usual deallocation function,
739 // regardless of its signature.
740 if (isa<FunctionTemplateDecl>(del)) {
741 filter.erase();
742 continue;
743 }
744
745 // C++0x [basic.stc.dynamic.deallocation]p2:
746 // If class T does not declare [an operator delete[] with one
747 // parameter] but does declare a member deallocation function
748 // named operator delete[] with exactly two parameters, the
749 // second of which has type std::size_t, then this function
750 // is a usual deallocation function.
751 if (!cast<CXXMethodDecl>(del)->isUsualDeallocationFunction()) {
752 filter.erase();
753 continue;
754 }
755 }
756 filter.done();
757
758 if (!ops.isSingleResult()) return false;
759
760 const FunctionDecl *del = cast<FunctionDecl>(ops.getFoundDecl());
761 return (del->getNumParams() == 2);
762}
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000763
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000764/// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.:
765/// @code new (memory) int[size][4] @endcode
766/// or
767/// @code ::new Foo(23, "hello") @endcode
768/// For the interpretation of this heap of arguments, consult the base version.
John McCall60d7b3a2010-08-24 06:29:42 +0000769ExprResult
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000770Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000771 SourceLocation PlacementLParen, MultiExprArg PlacementArgs,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000772 SourceLocation PlacementRParen, SourceRange TypeIdParens,
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000773 Declarator &D, SourceLocation ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000774 MultiExprArg ConstructorArgs,
Mike Stump1eb44332009-09-09 15:08:12 +0000775 SourceLocation ConstructorRParen) {
Richard Smith34b41d92011-02-20 03:19:35 +0000776 bool TypeContainsAuto = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
777
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000778 Expr *ArraySize = 0;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000779 // If the specified type is an array, unwrap it and save the expression.
780 if (D.getNumTypeObjects() > 0 &&
781 D.getTypeObject(0).Kind == DeclaratorChunk::Array) {
782 DeclaratorChunk &Chunk = D.getTypeObject(0);
Richard Smith34b41d92011-02-20 03:19:35 +0000783 if (TypeContainsAuto)
784 return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto)
785 << D.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000786 if (Chunk.Arr.hasStatic)
Sebastian Redlf53597f2009-03-15 17:47:39 +0000787 return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new)
788 << D.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000789 if (!Chunk.Arr.NumElts)
Sebastian Redlf53597f2009-03-15 17:47:39 +0000790 return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size)
791 << D.getSourceRange());
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000792
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000793 ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts);
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000794 D.DropFirstTypeObject();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000795 }
796
Douglas Gregor043cad22009-09-11 00:18:58 +0000797 // Every dimension shall be of constant size.
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000798 if (ArraySize) {
799 for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) {
Douglas Gregor043cad22009-09-11 00:18:58 +0000800 if (D.getTypeObject(I).Kind != DeclaratorChunk::Array)
801 break;
802
803 DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr;
804 if (Expr *NumElts = (Expr *)Array.NumElts) {
805 if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() &&
806 !NumElts->isIntegerConstantExpr(Context)) {
807 Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst)
808 << NumElts->getSourceRange();
809 return ExprError();
810 }
811 }
812 }
813 }
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000814
Richard Smith34b41d92011-02-20 03:19:35 +0000815 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/0, /*OwnedDecl=*/0,
816 /*AllowAuto=*/true);
John McCallbf1a0282010-06-04 23:28:52 +0000817 QualType AllocType = TInfo->getType();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000818 if (D.isInvalidType())
Sebastian Redlf53597f2009-03-15 17:47:39 +0000819 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000820
Mike Stump1eb44332009-09-09 15:08:12 +0000821 return BuildCXXNew(StartLoc, UseGlobal,
Douglas Gregor3433cf72009-05-21 00:00:09 +0000822 PlacementLParen,
Mike Stump1eb44332009-09-09 15:08:12 +0000823 move(PlacementArgs),
Douglas Gregor3433cf72009-05-21 00:00:09 +0000824 PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +0000825 TypeIdParens,
Mike Stump1eb44332009-09-09 15:08:12 +0000826 AllocType,
Douglas Gregor1bb2a932010-09-07 21:49:58 +0000827 TInfo,
John McCall9ae2f072010-08-23 23:25:46 +0000828 ArraySize,
Douglas Gregor3433cf72009-05-21 00:00:09 +0000829 ConstructorLParen,
830 move(ConstructorArgs),
Richard Smith34b41d92011-02-20 03:19:35 +0000831 ConstructorRParen,
832 TypeContainsAuto);
Douglas Gregor3433cf72009-05-21 00:00:09 +0000833}
834
John McCall60d7b3a2010-08-24 06:29:42 +0000835ExprResult
Douglas Gregor3433cf72009-05-21 00:00:09 +0000836Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal,
837 SourceLocation PlacementLParen,
838 MultiExprArg PlacementArgs,
839 SourceLocation PlacementRParen,
Douglas Gregor4bd40312010-07-13 15:54:32 +0000840 SourceRange TypeIdParens,
Douglas Gregor3433cf72009-05-21 00:00:09 +0000841 QualType AllocType,
Douglas Gregor1bb2a932010-09-07 21:49:58 +0000842 TypeSourceInfo *AllocTypeInfo,
John McCall9ae2f072010-08-23 23:25:46 +0000843 Expr *ArraySize,
Douglas Gregor3433cf72009-05-21 00:00:09 +0000844 SourceLocation ConstructorLParen,
845 MultiExprArg ConstructorArgs,
Richard Smith34b41d92011-02-20 03:19:35 +0000846 SourceLocation ConstructorRParen,
847 bool TypeMayContainAuto) {
Douglas Gregor1bb2a932010-09-07 21:49:58 +0000848 SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000849
Richard Smith34b41d92011-02-20 03:19:35 +0000850 // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for.
851 if (TypeMayContainAuto && AllocType->getContainedAutoType()) {
852 if (ConstructorArgs.size() == 0)
853 return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg)
854 << AllocType << TypeRange);
855 if (ConstructorArgs.size() != 1) {
856 Expr *FirstBad = ConstructorArgs.get()[1];
857 return ExprError(Diag(FirstBad->getSourceRange().getBegin(),
858 diag::err_auto_new_ctor_multiple_expressions)
859 << AllocType << TypeRange);
860 }
Richard Smitha085da82011-03-17 16:11:59 +0000861 TypeSourceInfo *DeducedType = 0;
862 if (!DeduceAutoType(AllocTypeInfo, ConstructorArgs.get()[0], DeducedType))
Richard Smith34b41d92011-02-20 03:19:35 +0000863 return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure)
864 << AllocType
865 << ConstructorArgs.get()[0]->getType()
866 << TypeRange
867 << ConstructorArgs.get()[0]->getSourceRange());
Richard Smitha085da82011-03-17 16:11:59 +0000868 if (!DeducedType)
869 return ExprError();
Richard Smith34b41d92011-02-20 03:19:35 +0000870
Richard Smitha085da82011-03-17 16:11:59 +0000871 AllocTypeInfo = DeducedType;
872 AllocType = AllocTypeInfo->getType();
Richard Smith34b41d92011-02-20 03:19:35 +0000873 }
874
Douglas Gregor3caf04e2010-05-16 16:01:03 +0000875 // Per C++0x [expr.new]p5, the type being constructed may be a
876 // typedef of an array type.
John McCall9ae2f072010-08-23 23:25:46 +0000877 if (!ArraySize) {
Douglas Gregor3caf04e2010-05-16 16:01:03 +0000878 if (const ConstantArrayType *Array
879 = Context.getAsConstantArrayType(AllocType)) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000880 ArraySize = IntegerLiteral::Create(Context, Array->getSize(),
881 Context.getSizeType(),
882 TypeRange.getEnd());
Douglas Gregor3caf04e2010-05-16 16:01:03 +0000883 AllocType = Array->getElementType();
884 }
885 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000886
Douglas Gregora0750762010-10-06 16:00:31 +0000887 if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange))
888 return ExprError();
889
Douglas Gregor3caf04e2010-05-16 16:01:03 +0000890 QualType ResultType = Context.getPointerType(AllocType);
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000891
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000892 // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral
893 // or enumeration type with a non-negative value."
Sebastian Redl28507842009-02-26 14:39:58 +0000894 if (ArraySize && !ArraySize->isTypeDependent()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000896 QualType SizeType = ArraySize->getType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000897
John McCall60d7b3a2010-08-24 06:29:42 +0000898 ExprResult ConvertedSize
John McCall9ae2f072010-08-23 23:25:46 +0000899 = ConvertToIntegralOrEnumerationType(StartLoc, ArraySize,
Douglas Gregor6bc574d2010-06-30 00:20:43 +0000900 PDiag(diag::err_array_size_not_integral),
901 PDiag(diag::err_array_size_incomplete_type)
902 << ArraySize->getSourceRange(),
903 PDiag(diag::err_array_size_explicit_conversion),
904 PDiag(diag::note_array_size_conversion),
905 PDiag(diag::err_array_size_ambiguous_conversion),
906 PDiag(diag::note_array_size_conversion),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000907 PDiag(getLangOptions().CPlusPlus0x? 0
Douglas Gregor6bc574d2010-06-30 00:20:43 +0000908 : diag::ext_array_size_conversion));
909 if (ConvertedSize.isInvalid())
910 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000911
John McCall9ae2f072010-08-23 23:25:46 +0000912 ArraySize = ConvertedSize.take();
Douglas Gregor6bc574d2010-06-30 00:20:43 +0000913 SizeType = ArraySize->getType();
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000914 if (!SizeType->isIntegralOrUnscopedEnumerationType())
Douglas Gregor6bc574d2010-06-30 00:20:43 +0000915 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000916
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000917 // Let's see if this is a constant < 0. If so, we reject it out of hand.
918 // We don't care about special rules, so we tell the machinery it's not
919 // evaluated - it gives us a result in more cases.
Sebastian Redl28507842009-02-26 14:39:58 +0000920 if (!ArraySize->isValueDependent()) {
921 llvm::APSInt Value;
922 if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) {
923 if (Value < llvm::APSInt(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000924 llvm::APInt::getNullValue(Value.getBitWidth()),
Anders Carlssonac18b2e2009-09-23 00:37:25 +0000925 Value.isUnsigned()))
Sebastian Redlf53597f2009-03-15 17:47:39 +0000926 return ExprError(Diag(ArraySize->getSourceRange().getBegin(),
Douglas Gregor2767ce22010-08-18 00:39:00 +0000927 diag::err_typecheck_negative_array_size)
Sebastian Redlf53597f2009-03-15 17:47:39 +0000928 << ArraySize->getSourceRange());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000929
Douglas Gregor2767ce22010-08-18 00:39:00 +0000930 if (!AllocType->isDependentType()) {
931 unsigned ActiveSizeBits
932 = ConstantArrayType::getNumAddressingBits(Context, AllocType, Value);
933 if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000934 Diag(ArraySize->getSourceRange().getBegin(),
Douglas Gregor2767ce22010-08-18 00:39:00 +0000935 diag::err_array_too_large)
936 << Value.toString(10)
937 << ArraySize->getSourceRange();
938 return ExprError();
939 }
940 }
Douglas Gregor4bd40312010-07-13 15:54:32 +0000941 } else if (TypeIdParens.isValid()) {
942 // Can't have dynamic array size when the type-id is in parentheses.
943 Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst)
944 << ArraySize->getSourceRange()
945 << FixItHint::CreateRemoval(TypeIdParens.getBegin())
946 << FixItHint::CreateRemoval(TypeIdParens.getEnd());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000947
Douglas Gregor4bd40312010-07-13 15:54:32 +0000948 TypeIdParens = SourceRange();
Sebastian Redl28507842009-02-26 14:39:58 +0000949 }
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000950 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000951
John Wiegley429bb272011-04-08 18:41:53 +0000952 ArraySize = ImpCastExprToType(ArraySize, Context.getSizeType(),
953 CK_IntegralCast).take();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000954 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000955
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000956 FunctionDecl *OperatorNew = 0;
957 FunctionDecl *OperatorDelete = 0;
Sebastian Redlf53597f2009-03-15 17:47:39 +0000958 Expr **PlaceArgs = (Expr**)PlacementArgs.get();
959 unsigned NumPlaceArgs = PlacementArgs.size();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000960
Sebastian Redl28507842009-02-26 14:39:58 +0000961 if (!AllocType->isDependentType() &&
962 !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) &&
963 FindAllocationFunctions(StartLoc,
Sebastian Redl00e68e22009-02-09 18:24:27 +0000964 SourceRange(PlacementLParen, PlacementRParen),
965 UseGlobal, AllocType, ArraySize, PlaceArgs,
966 NumPlaceArgs, OperatorNew, OperatorDelete))
Sebastian Redlf53597f2009-03-15 17:47:39 +0000967 return ExprError();
John McCall6ec278d2011-01-27 09:37:56 +0000968
969 // If this is an array allocation, compute whether the usual array
970 // deallocation function for the type has a size_t parameter.
971 bool UsualArrayDeleteWantsSize = false;
972 if (ArraySize && !AllocType->isDependentType())
973 UsualArrayDeleteWantsSize
974 = doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType);
975
Fariborz Jahanian048f52a2009-11-24 18:29:37 +0000976 llvm::SmallVector<Expr *, 8> AllPlaceArgs;
Fariborz Jahanian498429f2009-11-19 18:39:40 +0000977 if (OperatorNew) {
978 // Add default arguments, if any.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000979 const FunctionProtoType *Proto =
Fariborz Jahanian498429f2009-11-19 18:39:40 +0000980 OperatorNew->getType()->getAs<FunctionProtoType>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000981 VariadicCallType CallType =
Fariborz Jahanian4cd1c702009-11-24 19:27:49 +0000982 Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000983
Anders Carlsson28e94832010-05-03 02:07:56 +0000984 if (GatherArgumentsForCall(PlacementLParen, OperatorNew,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000985 Proto, 1, PlaceArgs, NumPlaceArgs,
Anders Carlsson28e94832010-05-03 02:07:56 +0000986 AllPlaceArgs, CallType))
Fariborz Jahanian048f52a2009-11-24 18:29:37 +0000987 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000988
Fariborz Jahanian498429f2009-11-19 18:39:40 +0000989 NumPlaceArgs = AllPlaceArgs.size();
990 if (NumPlaceArgs > 0)
991 PlaceArgs = &AllPlaceArgs[0];
992 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000993
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000994 bool Init = ConstructorLParen.isValid();
995 // --- Choosing a constructor ---
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000996 CXXConstructorDecl *Constructor = 0;
Sebastian Redlf53597f2009-03-15 17:47:39 +0000997 Expr **ConsArgs = (Expr**)ConstructorArgs.get();
998 unsigned NumConsArgs = ConstructorArgs.size();
John McCallca0408f2010-08-23 06:44:23 +0000999 ASTOwningVector<Expr*> ConvertedConstructorArgs(*this);
Eli Friedmana8ce9ec2009-11-08 22:15:39 +00001000
Anders Carlsson48c95012010-05-03 15:45:23 +00001001 // Array 'new' can't have any initializers.
Anders Carlsson55cbd6e2010-05-16 16:24:20 +00001002 if (NumConsArgs && (ResultType->isArrayType() || ArraySize)) {
Anders Carlsson48c95012010-05-03 15:45:23 +00001003 SourceRange InitRange(ConsArgs[0]->getLocStart(),
1004 ConsArgs[NumConsArgs - 1]->getLocEnd());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001005
Anders Carlsson48c95012010-05-03 15:45:23 +00001006 Diag(StartLoc, diag::err_new_array_init_args) << InitRange;
1007 return ExprError();
1008 }
1009
Douglas Gregor99a2e602009-12-16 01:38:02 +00001010 if (!AllocType->isDependentType() &&
1011 !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) {
1012 // C++0x [expr.new]p15:
1013 // A new-expression that creates an object of type T initializes that
1014 // object as follows:
1015 InitializationKind Kind
1016 // - If the new-initializer is omitted, the object is default-
1017 // initialized (8.5); if no initialization is performed,
1018 // the object has indeterminate value
Douglas Gregor1bb2a932010-09-07 21:49:58 +00001019 = !Init? InitializationKind::CreateDefault(TypeRange.getBegin())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001020 // - Otherwise, the new-initializer is interpreted according to the
Douglas Gregor99a2e602009-12-16 01:38:02 +00001021 // initialization rules of 8.5 for direct-initialization.
Douglas Gregor1bb2a932010-09-07 21:49:58 +00001022 : InitializationKind::CreateDirect(TypeRange.getBegin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001023 ConstructorLParen,
Douglas Gregor99a2e602009-12-16 01:38:02 +00001024 ConstructorRParen);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001025
Douglas Gregor99a2e602009-12-16 01:38:02 +00001026 InitializedEntity Entity
Douglas Gregord6542d82009-12-22 15:35:07 +00001027 = InitializedEntity::InitializeNew(StartLoc, AllocType);
Douglas Gregor99a2e602009-12-16 01:38:02 +00001028 InitializationSequence InitSeq(*this, Entity, Kind, ConsArgs, NumConsArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001029 ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind,
Douglas Gregor99a2e602009-12-16 01:38:02 +00001030 move(ConstructorArgs));
1031 if (FullInit.isInvalid())
1032 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001033
1034 // FullInit is our initializer; walk through it to determine if it's a
Douglas Gregor99a2e602009-12-16 01:38:02 +00001035 // constructor call, which CXXNewExpr handles directly.
1036 if (Expr *FullInitExpr = (Expr *)FullInit.get()) {
1037 if (CXXBindTemporaryExpr *Binder
1038 = dyn_cast<CXXBindTemporaryExpr>(FullInitExpr))
1039 FullInitExpr = Binder->getSubExpr();
1040 if (CXXConstructExpr *Construct
1041 = dyn_cast<CXXConstructExpr>(FullInitExpr)) {
1042 Constructor = Construct->getConstructor();
1043 for (CXXConstructExpr::arg_iterator A = Construct->arg_begin(),
1044 AEnd = Construct->arg_end();
1045 A != AEnd; ++A)
John McCall3fa5cae2010-10-26 07:05:15 +00001046 ConvertedConstructorArgs.push_back(*A);
Douglas Gregor99a2e602009-12-16 01:38:02 +00001047 } else {
1048 // Take the converted initializer.
1049 ConvertedConstructorArgs.push_back(FullInit.release());
1050 }
1051 } else {
1052 // No initialization required.
1053 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001054
Douglas Gregor99a2e602009-12-16 01:38:02 +00001055 // Take the converted arguments and use them for the new expression.
Douglas Gregor39da0b82009-09-09 23:08:42 +00001056 NumConsArgs = ConvertedConstructorArgs.size();
1057 ConsArgs = (Expr **)ConvertedConstructorArgs.take();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001058 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001059
Douglas Gregor6d908702010-02-26 05:06:18 +00001060 // Mark the new and delete operators as referenced.
1061 if (OperatorNew)
1062 MarkDeclarationReferenced(StartLoc, OperatorNew);
1063 if (OperatorDelete)
1064 MarkDeclarationReferenced(StartLoc, OperatorDelete);
1065
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001066 // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001067
Sebastian Redlf53597f2009-03-15 17:47:39 +00001068 PlacementArgs.release();
1069 ConstructorArgs.release();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001070
Ted Kremenekad7fe862010-02-11 22:51:03 +00001071 return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew,
Douglas Gregor4bd40312010-07-13 15:54:32 +00001072 PlaceArgs, NumPlaceArgs, TypeIdParens,
Ted Kremenekad7fe862010-02-11 22:51:03 +00001073 ArraySize, Constructor, Init,
1074 ConsArgs, NumConsArgs, OperatorDelete,
John McCall6ec278d2011-01-27 09:37:56 +00001075 UsualArrayDeleteWantsSize,
Douglas Gregor1bb2a932010-09-07 21:49:58 +00001076 ResultType, AllocTypeInfo,
1077 StartLoc,
Ted Kremenekad7fe862010-02-11 22:51:03 +00001078 Init ? ConstructorRParen :
Chandler Carruth428edaf2010-10-25 08:47:36 +00001079 TypeRange.getEnd(),
1080 ConstructorLParen, ConstructorRParen));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001081}
1082
1083/// CheckAllocatedType - Checks that a type is suitable as the allocated type
1084/// in a new-expression.
1085/// dimension off and stores the size expression in ArraySize.
Douglas Gregor3433cf72009-05-21 00:00:09 +00001086bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00001087 SourceRange R) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001088 // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
1089 // abstract class type or array thereof.
Douglas Gregore7450f52009-03-24 19:52:54 +00001090 if (AllocType->isFunctionType())
Douglas Gregor3433cf72009-05-21 00:00:09 +00001091 return Diag(Loc, diag::err_bad_new_type)
1092 << AllocType << 0 << R;
Douglas Gregore7450f52009-03-24 19:52:54 +00001093 else if (AllocType->isReferenceType())
Douglas Gregor3433cf72009-05-21 00:00:09 +00001094 return Diag(Loc, diag::err_bad_new_type)
1095 << AllocType << 1 << R;
Douglas Gregore7450f52009-03-24 19:52:54 +00001096 else if (!AllocType->isDependentType() &&
Douglas Gregor3433cf72009-05-21 00:00:09 +00001097 RequireCompleteType(Loc, AllocType,
Anders Carlssonb7906612009-08-26 23:45:07 +00001098 PDiag(diag::err_new_incomplete_type)
1099 << R))
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001100 return true;
Douglas Gregor3433cf72009-05-21 00:00:09 +00001101 else if (RequireNonAbstractType(Loc, AllocType,
Douglas Gregore7450f52009-03-24 19:52:54 +00001102 diag::err_allocation_of_abstract_type))
1103 return true;
Douglas Gregora0750762010-10-06 16:00:31 +00001104 else if (AllocType->isVariablyModifiedType())
1105 return Diag(Loc, diag::err_variably_modified_new_type)
1106 << AllocType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001107
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001108 return false;
1109}
1110
Douglas Gregor6d908702010-02-26 05:06:18 +00001111/// \brief Determine whether the given function is a non-placement
1112/// deallocation function.
1113static bool isNonPlacementDeallocationFunction(FunctionDecl *FD) {
1114 if (FD->isInvalidDecl())
1115 return false;
1116
1117 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD))
1118 return Method->isUsualDeallocationFunction();
1119
1120 return ((FD->getOverloadedOperator() == OO_Delete ||
1121 FD->getOverloadedOperator() == OO_Array_Delete) &&
1122 FD->getNumParams() == 1);
1123}
1124
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001125/// FindAllocationFunctions - Finds the overloads of operator new and delete
1126/// that are appropriate for the allocation.
Sebastian Redl00e68e22009-02-09 18:24:27 +00001127bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range,
1128 bool UseGlobal, QualType AllocType,
1129 bool IsArray, Expr **PlaceArgs,
1130 unsigned NumPlaceArgs,
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001131 FunctionDecl *&OperatorNew,
Mike Stump1eb44332009-09-09 15:08:12 +00001132 FunctionDecl *&OperatorDelete) {
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001133 // --- Choosing an allocation function ---
1134 // C++ 5.3.4p8 - 14 & 18
1135 // 1) If UseGlobal is true, only look in the global scope. Else, also look
1136 // in the scope of the allocated class.
1137 // 2) If an array size is given, look for operator new[], else look for
1138 // operator new.
1139 // 3) The first argument is always size_t. Append the arguments from the
1140 // placement form.
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001141
1142 llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs);
1143 // We don't care about the actual value of this argument.
1144 // FIXME: Should the Sema create the expression and embed it in the syntax
1145 // tree? Or should the consumer just recalculate the value?
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00001146 IntegerLiteral Size(Context, llvm::APInt::getNullValue(
Anders Carlssond67c4c32009-08-16 20:29:29 +00001147 Context.Target.getPointerWidth(0)),
1148 Context.getSizeType(),
1149 SourceLocation());
1150 AllocArgs[0] = &Size;
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001151 std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1);
1152
Douglas Gregor6d908702010-02-26 05:06:18 +00001153 // C++ [expr.new]p8:
1154 // If the allocated type is a non-array type, the allocation
NAKAMURA Takumi00995302011-01-27 07:09:49 +00001155 // function's name is operator new and the deallocation function's
Douglas Gregor6d908702010-02-26 05:06:18 +00001156 // name is operator delete. If the allocated type is an array
NAKAMURA Takumi00995302011-01-27 07:09:49 +00001157 // type, the allocation function's name is operator new[] and the
1158 // deallocation function's name is operator delete[].
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001159 DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName(
1160 IsArray ? OO_Array_New : OO_New);
Douglas Gregor6d908702010-02-26 05:06:18 +00001161 DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName(
1162 IsArray ? OO_Array_Delete : OO_Delete);
1163
Argyrios Kyrtzidisd2932982010-08-25 23:14:56 +00001164 QualType AllocElemType = Context.getBaseElementType(AllocType);
1165
1166 if (AllocElemType->isRecordType() && !UseGlobal) {
Mike Stump1eb44332009-09-09 15:08:12 +00001167 CXXRecordDecl *Record
Argyrios Kyrtzidisd2932982010-08-25 23:14:56 +00001168 = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl());
Sebastian Redl00e68e22009-02-09 18:24:27 +00001169 if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0],
Sebastian Redl7f662392008-12-04 22:20:51 +00001170 AllocArgs.size(), Record, /*AllowMissing=*/true,
1171 OperatorNew))
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001172 return true;
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001173 }
1174 if (!OperatorNew) {
1175 // Didn't find a member overload. Look for a global one.
1176 DeclareGlobalNewDelete();
Sebastian Redl7f662392008-12-04 22:20:51 +00001177 DeclContext *TUDecl = Context.getTranslationUnitDecl();
Sebastian Redl00e68e22009-02-09 18:24:27 +00001178 if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0],
Sebastian Redl7f662392008-12-04 22:20:51 +00001179 AllocArgs.size(), TUDecl, /*AllowMissing=*/false,
1180 OperatorNew))
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001181 return true;
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001182 }
1183
John McCall9c82afc2010-04-20 02:18:25 +00001184 // We don't need an operator delete if we're running under
1185 // -fno-exceptions.
1186 if (!getLangOptions().Exceptions) {
1187 OperatorDelete = 0;
1188 return false;
1189 }
1190
Anders Carlssond9583892009-05-31 20:26:12 +00001191 // FindAllocationOverload can change the passed in arguments, so we need to
1192 // copy them back.
1193 if (NumPlaceArgs > 0)
1194 std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Douglas Gregor6d908702010-02-26 05:06:18 +00001196 // C++ [expr.new]p19:
1197 //
1198 // If the new-expression begins with a unary :: operator, the
NAKAMURA Takumi00995302011-01-27 07:09:49 +00001199 // deallocation function's name is looked up in the global
Douglas Gregor6d908702010-02-26 05:06:18 +00001200 // scope. Otherwise, if the allocated type is a class type T or an
NAKAMURA Takumi00995302011-01-27 07:09:49 +00001201 // array thereof, the deallocation function's name is looked up in
Douglas Gregor6d908702010-02-26 05:06:18 +00001202 // the scope of T. If this lookup fails to find the name, or if
1203 // the allocated type is not a class type or array thereof, the
NAKAMURA Takumi00995302011-01-27 07:09:49 +00001204 // deallocation function's name is looked up in the global scope.
Douglas Gregor6d908702010-02-26 05:06:18 +00001205 LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName);
Argyrios Kyrtzidisd2932982010-08-25 23:14:56 +00001206 if (AllocElemType->isRecordType() && !UseGlobal) {
Douglas Gregor6d908702010-02-26 05:06:18 +00001207 CXXRecordDecl *RD
Argyrios Kyrtzidisd2932982010-08-25 23:14:56 +00001208 = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl());
Douglas Gregor6d908702010-02-26 05:06:18 +00001209 LookupQualifiedName(FoundDelete, RD);
1210 }
John McCall90c8c572010-03-18 08:19:33 +00001211 if (FoundDelete.isAmbiguous())
1212 return true; // FIXME: clean up expressions?
Douglas Gregor6d908702010-02-26 05:06:18 +00001213
1214 if (FoundDelete.empty()) {
1215 DeclareGlobalNewDelete();
1216 LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl());
1217 }
1218
1219 FoundDelete.suppressDiagnostics();
John McCall9aa472c2010-03-19 07:35:19 +00001220
1221 llvm::SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches;
1222
John McCalledeb6c92010-09-14 21:34:24 +00001223 // Whether we're looking for a placement operator delete is dictated
1224 // by whether we selected a placement operator new, not by whether
1225 // we had explicit placement arguments. This matters for things like
1226 // struct A { void *operator new(size_t, int = 0); ... };
1227 // A *a = new A()
1228 bool isPlacementNew = (NumPlaceArgs > 0 || OperatorNew->param_size() != 1);
1229
1230 if (isPlacementNew) {
Douglas Gregor6d908702010-02-26 05:06:18 +00001231 // C++ [expr.new]p20:
1232 // A declaration of a placement deallocation function matches the
1233 // declaration of a placement allocation function if it has the
1234 // same number of parameters and, after parameter transformations
1235 // (8.3.5), all parameter types except the first are
1236 // identical. [...]
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001237 //
Douglas Gregor6d908702010-02-26 05:06:18 +00001238 // To perform this comparison, we compute the function type that
1239 // the deallocation function should have, and use that type both
1240 // for template argument deduction and for comparison purposes.
John McCalle23cf432010-12-14 08:05:40 +00001241 //
1242 // FIXME: this comparison should ignore CC and the like.
Douglas Gregor6d908702010-02-26 05:06:18 +00001243 QualType ExpectedFunctionType;
1244 {
1245 const FunctionProtoType *Proto
1246 = OperatorNew->getType()->getAs<FunctionProtoType>();
John McCalle23cf432010-12-14 08:05:40 +00001247
Douglas Gregor6d908702010-02-26 05:06:18 +00001248 llvm::SmallVector<QualType, 4> ArgTypes;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001249 ArgTypes.push_back(Context.VoidPtrTy);
Douglas Gregor6d908702010-02-26 05:06:18 +00001250 for (unsigned I = 1, N = Proto->getNumArgs(); I < N; ++I)
1251 ArgTypes.push_back(Proto->getArgType(I));
1252
John McCalle23cf432010-12-14 08:05:40 +00001253 FunctionProtoType::ExtProtoInfo EPI;
1254 EPI.Variadic = Proto->isVariadic();
1255
Douglas Gregor6d908702010-02-26 05:06:18 +00001256 ExpectedFunctionType
1257 = Context.getFunctionType(Context.VoidTy, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001258 ArgTypes.size(), EPI);
Douglas Gregor6d908702010-02-26 05:06:18 +00001259 }
1260
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001261 for (LookupResult::iterator D = FoundDelete.begin(),
Douglas Gregor6d908702010-02-26 05:06:18 +00001262 DEnd = FoundDelete.end();
1263 D != DEnd; ++D) {
1264 FunctionDecl *Fn = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001265 if (FunctionTemplateDecl *FnTmpl
Douglas Gregor6d908702010-02-26 05:06:18 +00001266 = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) {
1267 // Perform template argument deduction to try to match the
1268 // expected function type.
1269 TemplateDeductionInfo Info(Context, StartLoc);
1270 if (DeduceTemplateArguments(FnTmpl, 0, ExpectedFunctionType, Fn, Info))
1271 continue;
1272 } else
1273 Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl());
1274
1275 if (Context.hasSameType(Fn->getType(), ExpectedFunctionType))
John McCall9aa472c2010-03-19 07:35:19 +00001276 Matches.push_back(std::make_pair(D.getPair(), Fn));
Douglas Gregor6d908702010-02-26 05:06:18 +00001277 }
1278 } else {
1279 // C++ [expr.new]p20:
1280 // [...] Any non-placement deallocation function matches a
1281 // non-placement allocation function. [...]
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001282 for (LookupResult::iterator D = FoundDelete.begin(),
Douglas Gregor6d908702010-02-26 05:06:18 +00001283 DEnd = FoundDelete.end();
1284 D != DEnd; ++D) {
1285 if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl()))
1286 if (isNonPlacementDeallocationFunction(Fn))
John McCall9aa472c2010-03-19 07:35:19 +00001287 Matches.push_back(std::make_pair(D.getPair(), Fn));
Douglas Gregor6d908702010-02-26 05:06:18 +00001288 }
1289 }
1290
1291 // C++ [expr.new]p20:
1292 // [...] If the lookup finds a single matching deallocation
1293 // function, that function will be called; otherwise, no
1294 // deallocation function will be called.
1295 if (Matches.size() == 1) {
John McCall9aa472c2010-03-19 07:35:19 +00001296 OperatorDelete = Matches[0].second;
Douglas Gregor6d908702010-02-26 05:06:18 +00001297
1298 // C++0x [expr.new]p20:
1299 // If the lookup finds the two-parameter form of a usual
1300 // deallocation function (3.7.4.2) and that function, considered
1301 // as a placement deallocation function, would have been
1302 // selected as a match for the allocation function, the program
1303 // is ill-formed.
1304 if (NumPlaceArgs && getLangOptions().CPlusPlus0x &&
1305 isNonPlacementDeallocationFunction(OperatorDelete)) {
1306 Diag(StartLoc, diag::err_placement_new_non_placement_delete)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001307 << SourceRange(PlaceArgs[0]->getLocStart(),
Douglas Gregor6d908702010-02-26 05:06:18 +00001308 PlaceArgs[NumPlaceArgs - 1]->getLocEnd());
1309 Diag(OperatorDelete->getLocation(), diag::note_previous_decl)
1310 << DeleteName;
John McCall90c8c572010-03-18 08:19:33 +00001311 } else {
1312 CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(),
John McCall9aa472c2010-03-19 07:35:19 +00001313 Matches[0].first);
Douglas Gregor6d908702010-02-26 05:06:18 +00001314 }
1315 }
1316
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001317 return false;
1318}
1319
Sebastian Redl7f662392008-12-04 22:20:51 +00001320/// FindAllocationOverload - Find an fitting overload for the allocation
1321/// function in the specified scope.
Sebastian Redl00e68e22009-02-09 18:24:27 +00001322bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range,
1323 DeclarationName Name, Expr** Args,
1324 unsigned NumArgs, DeclContext *Ctx,
Mike Stump1eb44332009-09-09 15:08:12 +00001325 bool AllowMissing, FunctionDecl *&Operator) {
John McCalla24dc2e2009-11-17 02:14:36 +00001326 LookupResult R(*this, Name, StartLoc, LookupOrdinaryName);
1327 LookupQualifiedName(R, Ctx);
John McCallf36e02d2009-10-09 21:13:30 +00001328 if (R.empty()) {
Sebastian Redl7f662392008-12-04 22:20:51 +00001329 if (AllowMissing)
1330 return false;
Sebastian Redl7f662392008-12-04 22:20:51 +00001331 return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
Chris Lattner4330d652009-02-17 07:29:20 +00001332 << Name << Range;
Sebastian Redl7f662392008-12-04 22:20:51 +00001333 }
1334
John McCall90c8c572010-03-18 08:19:33 +00001335 if (R.isAmbiguous())
1336 return true;
1337
1338 R.suppressDiagnostics();
John McCallf36e02d2009-10-09 21:13:30 +00001339
John McCall5769d612010-02-08 23:07:23 +00001340 OverloadCandidateSet Candidates(StartLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001341 for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end();
Douglas Gregor5d64e5b2009-09-30 00:03:47 +00001342 Alloc != AllocEnd; ++Alloc) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001343 // Even member operator new/delete are implicitly treated as
1344 // static, so don't use AddMemberCandidate.
John McCall9aa472c2010-03-19 07:35:19 +00001345 NamedDecl *D = (*Alloc)->getUnderlyingDecl();
Chandler Carruth4a73ea92010-02-03 11:02:14 +00001346
John McCall9aa472c2010-03-19 07:35:19 +00001347 if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) {
1348 AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(),
Chandler Carruth4a73ea92010-02-03 11:02:14 +00001349 /*ExplicitTemplateArgs=*/0, Args, NumArgs,
1350 Candidates,
1351 /*SuppressUserConversions=*/false);
Douglas Gregor90916562009-09-29 18:16:17 +00001352 continue;
Chandler Carruth4a73ea92010-02-03 11:02:14 +00001353 }
1354
John McCall9aa472c2010-03-19 07:35:19 +00001355 FunctionDecl *Fn = cast<FunctionDecl>(D);
1356 AddOverloadCandidate(Fn, Alloc.getPair(), Args, NumArgs, Candidates,
Chandler Carruth4a73ea92010-02-03 11:02:14 +00001357 /*SuppressUserConversions=*/false);
Sebastian Redl7f662392008-12-04 22:20:51 +00001358 }
1359
1360 // Do the resolution.
1361 OverloadCandidateSet::iterator Best;
John McCall120d63c2010-08-24 20:38:10 +00001362 switch (Candidates.BestViableFunction(*this, StartLoc, Best)) {
Sebastian Redl7f662392008-12-04 22:20:51 +00001363 case OR_Success: {
1364 // Got one!
1365 FunctionDecl *FnDecl = Best->Function;
Chandler Carruth25ca4212011-02-25 19:41:05 +00001366 MarkDeclarationReferenced(StartLoc, FnDecl);
Sebastian Redl7f662392008-12-04 22:20:51 +00001367 // The first argument is size_t, and the first parameter must be size_t,
1368 // too. This is checked on declaration and can be assumed. (It can't be
1369 // asserted on, though, since invalid decls are left in there.)
John McCall90c8c572010-03-18 08:19:33 +00001370 // Watch out for variadic allocator function.
Fariborz Jahanian048f52a2009-11-24 18:29:37 +00001371 unsigned NumArgsInFnDecl = FnDecl->getNumParams();
1372 for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) {
John McCall60d7b3a2010-08-24 06:29:42 +00001373 ExprResult Result
Douglas Gregor29ecaba2010-03-26 20:35:59 +00001374 = PerformCopyInitialization(InitializedEntity::InitializeParameter(
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00001375 Context,
Douglas Gregor29ecaba2010-03-26 20:35:59 +00001376 FnDecl->getParamDecl(i)),
1377 SourceLocation(),
John McCall3fa5cae2010-10-26 07:05:15 +00001378 Owned(Args[i]));
Douglas Gregor29ecaba2010-03-26 20:35:59 +00001379 if (Result.isInvalid())
Sebastian Redl7f662392008-12-04 22:20:51 +00001380 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001381
Douglas Gregor29ecaba2010-03-26 20:35:59 +00001382 Args[i] = Result.takeAs<Expr>();
Sebastian Redl7f662392008-12-04 22:20:51 +00001383 }
1384 Operator = FnDecl;
John McCall9aa472c2010-03-19 07:35:19 +00001385 CheckAllocationAccess(StartLoc, Range, R.getNamingClass(), Best->FoundDecl);
Sebastian Redl7f662392008-12-04 22:20:51 +00001386 return false;
1387 }
1388
1389 case OR_No_Viable_Function:
Sebastian Redl7f662392008-12-04 22:20:51 +00001390 Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
Chris Lattner4330d652009-02-17 07:29:20 +00001391 << Name << Range;
John McCall120d63c2010-08-24 20:38:10 +00001392 Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs);
Sebastian Redl7f662392008-12-04 22:20:51 +00001393 return true;
1394
1395 case OR_Ambiguous:
Sebastian Redl7f662392008-12-04 22:20:51 +00001396 Diag(StartLoc, diag::err_ovl_ambiguous_call)
Sebastian Redl00e68e22009-02-09 18:24:27 +00001397 << Name << Range;
John McCall120d63c2010-08-24 20:38:10 +00001398 Candidates.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs);
Sebastian Redl7f662392008-12-04 22:20:51 +00001399 return true;
Douglas Gregor48f3bb92009-02-18 21:56:37 +00001400
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001401 case OR_Deleted: {
Douglas Gregor48f3bb92009-02-18 21:56:37 +00001402 Diag(StartLoc, diag::err_ovl_deleted_call)
1403 << Best->Function->isDeleted()
Fariborz Jahanian5e24f2a2011-02-25 20:51:14 +00001404 << Name
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001405 << getDeletedOrUnavailableSuffix(Best->Function)
Fariborz Jahanian5e24f2a2011-02-25 20:51:14 +00001406 << Range;
John McCall120d63c2010-08-24 20:38:10 +00001407 Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs);
Douglas Gregor48f3bb92009-02-18 21:56:37 +00001408 return true;
Sebastian Redl7f662392008-12-04 22:20:51 +00001409 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001410 }
Sebastian Redl7f662392008-12-04 22:20:51 +00001411 assert(false && "Unreachable, bad result from BestViableFunction");
1412 return true;
1413}
1414
1415
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001416/// DeclareGlobalNewDelete - Declare the global forms of operator new and
1417/// delete. These are:
1418/// @code
Sebastian Redl8999fe12011-03-14 18:08:30 +00001419/// // C++03:
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001420/// void* operator new(std::size_t) throw(std::bad_alloc);
1421/// void* operator new[](std::size_t) throw(std::bad_alloc);
1422/// void operator delete(void *) throw();
1423/// void operator delete[](void *) throw();
Sebastian Redl8999fe12011-03-14 18:08:30 +00001424/// // C++0x:
1425/// void* operator new(std::size_t);
1426/// void* operator new[](std::size_t);
1427/// void operator delete(void *);
1428/// void operator delete[](void *);
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001429/// @endcode
Sebastian Redl8999fe12011-03-14 18:08:30 +00001430/// C++0x operator delete is implicitly noexcept.
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001431/// Note that the placement and nothrow forms of new are *not* implicitly
1432/// declared. Their use requires including \<new\>.
Mike Stump1eb44332009-09-09 15:08:12 +00001433void Sema::DeclareGlobalNewDelete() {
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001434 if (GlobalNewDeleteDeclared)
1435 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001436
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001437 // C++ [basic.std.dynamic]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001438 // [...] The following allocation and deallocation functions (18.4) are
1439 // implicitly declared in global scope in each translation unit of a
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001440 // program
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001441 //
Sebastian Redl8999fe12011-03-14 18:08:30 +00001442 // C++03:
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001443 // void* operator new(std::size_t) throw(std::bad_alloc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001444 // void* operator new[](std::size_t) throw(std::bad_alloc);
1445 // void operator delete(void*) throw();
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001446 // void operator delete[](void*) throw();
Sebastian Redl8999fe12011-03-14 18:08:30 +00001447 // C++0x:
1448 // void* operator new(std::size_t);
1449 // void* operator new[](std::size_t);
1450 // void operator delete(void*);
1451 // void operator delete[](void*);
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001452 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001453 // These implicit declarations introduce only the function names operator
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001454 // new, operator new[], operator delete, operator delete[].
1455 //
1456 // Here, we need to refer to std::bad_alloc, so we will implicitly declare
1457 // "std" or "bad_alloc" as necessary to form the exception specification.
1458 // However, we do not make these implicit declarations visible to name
1459 // lookup.
Sebastian Redl8999fe12011-03-14 18:08:30 +00001460 // Note that the C++0x versions of operator delete are deallocation functions,
1461 // and thus are implicitly noexcept.
1462 if (!StdBadAlloc && !getLangOptions().CPlusPlus0x) {
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001463 // The "std::bad_alloc" class has not yet been declared, so build it
1464 // implicitly.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001465 StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class,
1466 getOrCreateStdNamespace(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001467 SourceLocation(), SourceLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001468 &PP.getIdentifierTable().get("bad_alloc"),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001469 0);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00001470 getStdBadAlloc()->setImplicit(true);
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001471 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001472
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001473 GlobalNewDeleteDeclared = true;
1474
1475 QualType VoidPtr = Context.getPointerType(Context.VoidTy);
1476 QualType SizeT = Context.getSizeType();
Nuno Lopesfc284482009-12-16 16:59:22 +00001477 bool AssumeSaneOperatorNew = getLangOptions().AssumeSaneOperatorNew;
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001478
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001479 DeclareGlobalAllocationFunction(
1480 Context.DeclarationNames.getCXXOperatorName(OO_New),
Nuno Lopesfc284482009-12-16 16:59:22 +00001481 VoidPtr, SizeT, AssumeSaneOperatorNew);
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001482 DeclareGlobalAllocationFunction(
1483 Context.DeclarationNames.getCXXOperatorName(OO_Array_New),
Nuno Lopesfc284482009-12-16 16:59:22 +00001484 VoidPtr, SizeT, AssumeSaneOperatorNew);
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001485 DeclareGlobalAllocationFunction(
1486 Context.DeclarationNames.getCXXOperatorName(OO_Delete),
1487 Context.VoidTy, VoidPtr);
1488 DeclareGlobalAllocationFunction(
1489 Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete),
1490 Context.VoidTy, VoidPtr);
1491}
1492
1493/// DeclareGlobalAllocationFunction - Declares a single implicit global
1494/// allocation function if it doesn't already exist.
1495void Sema::DeclareGlobalAllocationFunction(DeclarationName Name,
Nuno Lopesfc284482009-12-16 16:59:22 +00001496 QualType Return, QualType Argument,
1497 bool AddMallocAttr) {
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001498 DeclContext *GlobalCtx = Context.getTranslationUnitDecl();
1499
1500 // Check if this function is already declared.
Douglas Gregor6ed40e32008-12-23 21:05:05 +00001501 {
Douglas Gregor5cc37092008-12-23 22:05:29 +00001502 DeclContext::lookup_iterator Alloc, AllocEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001503 for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name);
Douglas Gregor6ed40e32008-12-23 21:05:05 +00001504 Alloc != AllocEnd; ++Alloc) {
Chandler Carruth4a73ea92010-02-03 11:02:14 +00001505 // Only look at non-template functions, as it is the predefined,
1506 // non-templated allocation function we are trying to declare here.
1507 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) {
1508 QualType InitialParamType =
Douglas Gregor6e790ab2009-12-22 23:42:49 +00001509 Context.getCanonicalType(
Chandler Carruth4a73ea92010-02-03 11:02:14 +00001510 Func->getParamDecl(0)->getType().getUnqualifiedType());
1511 // FIXME: Do we need to check for default arguments here?
Douglas Gregor7b868622010-08-18 15:06:25 +00001512 if (Func->getNumParams() == 1 && InitialParamType == Argument) {
1513 if(AddMallocAttr && !Func->hasAttr<MallocAttr>())
Sean Huntcf807c42010-08-18 23:23:40 +00001514 Func->addAttr(::new (Context) MallocAttr(SourceLocation(), Context));
Chandler Carruth4a73ea92010-02-03 11:02:14 +00001515 return;
Douglas Gregor7b868622010-08-18 15:06:25 +00001516 }
Chandler Carruth4a73ea92010-02-03 11:02:14 +00001517 }
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001518 }
1519 }
1520
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001521 QualType BadAllocType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001522 bool HasBadAllocExceptionSpec
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001523 = (Name.getCXXOverloadedOperator() == OO_New ||
1524 Name.getCXXOverloadedOperator() == OO_Array_New);
Sebastian Redl8999fe12011-03-14 18:08:30 +00001525 if (HasBadAllocExceptionSpec && !getLangOptions().CPlusPlus0x) {
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001526 assert(StdBadAlloc && "Must have std::bad_alloc declared");
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00001527 BadAllocType = Context.getTypeDeclType(getStdBadAlloc());
Douglas Gregor7adb10f2009-09-15 22:30:29 +00001528 }
John McCalle23cf432010-12-14 08:05:40 +00001529
1530 FunctionProtoType::ExtProtoInfo EPI;
John McCalle23cf432010-12-14 08:05:40 +00001531 if (HasBadAllocExceptionSpec) {
Sebastian Redl8999fe12011-03-14 18:08:30 +00001532 if (!getLangOptions().CPlusPlus0x) {
1533 EPI.ExceptionSpecType = EST_Dynamic;
1534 EPI.NumExceptions = 1;
1535 EPI.Exceptions = &BadAllocType;
1536 }
Sebastian Redl60618fa2011-03-12 11:50:43 +00001537 } else {
Sebastian Redl8999fe12011-03-14 18:08:30 +00001538 EPI.ExceptionSpecType = getLangOptions().CPlusPlus0x ?
1539 EST_BasicNoexcept : EST_DynamicNone;
John McCalle23cf432010-12-14 08:05:40 +00001540 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001541
John McCalle23cf432010-12-14 08:05:40 +00001542 QualType FnType = Context.getFunctionType(Return, &Argument, 1, EPI);
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001543 FunctionDecl *Alloc =
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001544 FunctionDecl::Create(Context, GlobalCtx, SourceLocation(),
1545 SourceLocation(), Name,
John McCalld931b082010-08-26 03:08:43 +00001546 FnType, /*TInfo=*/0, SC_None,
1547 SC_None, false, true);
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001548 Alloc->setImplicit();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001549
Nuno Lopesfc284482009-12-16 16:59:22 +00001550 if (AddMallocAttr)
Sean Huntcf807c42010-08-18 23:23:40 +00001551 Alloc->addAttr(::new (Context) MallocAttr(SourceLocation(), Context));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001552
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001553 ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001554 SourceLocation(), 0,
1555 Argument, /*TInfo=*/0,
1556 SC_None, SC_None, 0);
Douglas Gregor838db382010-02-11 01:19:42 +00001557 Alloc->setParams(&Param, 1);
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001558
Douglas Gregor6ed40e32008-12-23 21:05:05 +00001559 // FIXME: Also add this declaration to the IdentifierResolver, but
1560 // make sure it is at the end of the chain to coincide with the
1561 // global scope.
John McCall5f1e0942010-08-24 08:50:51 +00001562 Context.getTranslationUnitDecl()->addDecl(Alloc);
Sebastian Redlb5a57a62008-12-03 20:26:15 +00001563}
1564
Anders Carlsson78f74552009-11-15 18:45:20 +00001565bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD,
1566 DeclarationName Name,
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001567 FunctionDecl* &Operator) {
John McCalla24dc2e2009-11-17 02:14:36 +00001568 LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName);
Anders Carlsson78f74552009-11-15 18:45:20 +00001569 // Try to find operator delete/operator delete[] in class scope.
John McCalla24dc2e2009-11-17 02:14:36 +00001570 LookupQualifiedName(Found, RD);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001571
John McCalla24dc2e2009-11-17 02:14:36 +00001572 if (Found.isAmbiguous())
Anders Carlsson78f74552009-11-15 18:45:20 +00001573 return true;
Anders Carlsson78f74552009-11-15 18:45:20 +00001574
Chandler Carruth23893242010-06-28 00:30:51 +00001575 Found.suppressDiagnostics();
1576
John McCall046a7462010-08-04 00:31:26 +00001577 llvm::SmallVector<DeclAccessPair,4> Matches;
Anders Carlsson78f74552009-11-15 18:45:20 +00001578 for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
1579 F != FEnd; ++F) {
Chandler Carruth09556fd2010-08-08 07:04:00 +00001580 NamedDecl *ND = (*F)->getUnderlyingDecl();
1581
1582 // Ignore template operator delete members from the check for a usual
1583 // deallocation function.
1584 if (isa<FunctionTemplateDecl>(ND))
1585 continue;
1586
1587 if (cast<CXXMethodDecl>(ND)->isUsualDeallocationFunction())
John McCall046a7462010-08-04 00:31:26 +00001588 Matches.push_back(F.getPair());
1589 }
1590
1591 // There's exactly one suitable operator; pick it.
1592 if (Matches.size() == 1) {
1593 Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl());
1594 CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(),
1595 Matches[0]);
1596 return false;
1597
1598 // We found multiple suitable operators; complain about the ambiguity.
1599 } else if (!Matches.empty()) {
1600 Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found)
1601 << Name << RD;
1602
1603 for (llvm::SmallVectorImpl<DeclAccessPair>::iterator
1604 F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F)
1605 Diag((*F)->getUnderlyingDecl()->getLocation(),
1606 diag::note_member_declared_here) << Name;
1607 return true;
Anders Carlsson78f74552009-11-15 18:45:20 +00001608 }
1609
1610 // We did find operator delete/operator delete[] declarations, but
1611 // none of them were suitable.
1612 if (!Found.empty()) {
1613 Diag(StartLoc, diag::err_no_suitable_delete_member_function_found)
1614 << Name << RD;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001615
Anders Carlsson78f74552009-11-15 18:45:20 +00001616 for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
John McCall046a7462010-08-04 00:31:26 +00001617 F != FEnd; ++F)
1618 Diag((*F)->getUnderlyingDecl()->getLocation(),
1619 diag::note_member_declared_here) << Name;
Anders Carlsson78f74552009-11-15 18:45:20 +00001620
1621 return true;
1622 }
1623
1624 // Look for a global declaration.
1625 DeclareGlobalNewDelete();
1626 DeclContext *TUDecl = Context.getTranslationUnitDecl();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001627
Anders Carlsson78f74552009-11-15 18:45:20 +00001628 CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation());
1629 Expr* DeallocArgs[1];
1630 DeallocArgs[0] = &Null;
1631 if (FindAllocationOverload(StartLoc, SourceRange(), Name,
1632 DeallocArgs, 1, TUDecl, /*AllowMissing=*/false,
1633 Operator))
1634 return true;
1635
1636 assert(Operator && "Did not find a deallocation function!");
1637 return false;
1638}
1639
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001640/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
1641/// @code ::delete ptr; @endcode
1642/// or
1643/// @code delete [] ptr; @endcode
John McCall60d7b3a2010-08-24 06:29:42 +00001644ExprResult
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001645Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
John Wiegley429bb272011-04-08 18:41:53 +00001646 bool ArrayForm, Expr *ExE) {
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +00001647 // C++ [expr.delete]p1:
1648 // The operand shall have a pointer type, or a class type having a single
1649 // conversion function to a pointer type. The result has type void.
1650 //
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001651 // DR599 amends "pointer type" to "pointer to object type" in both cases.
1652
John Wiegley429bb272011-04-08 18:41:53 +00001653 ExprResult Ex = Owned(ExE);
Anders Carlssond67c4c32009-08-16 20:29:29 +00001654 FunctionDecl *OperatorDelete = 0;
Argyrios Kyrtzidis4076dac2010-09-13 20:15:54 +00001655 bool ArrayFormAsWritten = ArrayForm;
John McCall6ec278d2011-01-27 09:37:56 +00001656 bool UsualArrayDeleteWantsSize = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001657
John Wiegley429bb272011-04-08 18:41:53 +00001658 if (!Ex.get()->isTypeDependent()) {
1659 QualType Type = Ex.get()->getType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001660
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +00001661 if (const RecordType *Record = Type->getAs<RecordType>()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001662 if (RequireCompleteType(StartLoc, Type,
Douglas Gregor254a9422010-07-29 14:44:35 +00001663 PDiag(diag::err_delete_incomplete_class_type)))
1664 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001665
John McCall32daa422010-03-31 01:36:47 +00001666 llvm::SmallVector<CXXConversionDecl*, 4> ObjectPtrConversions;
1667
Fariborz Jahanian53462782009-09-11 21:44:33 +00001668 CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001669 const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions();
John McCalleec51cf2010-01-20 00:46:10 +00001670 for (UnresolvedSetImpl::iterator I = Conversions->begin(),
John McCallba135432009-11-21 08:51:07 +00001671 E = Conversions->end(); I != E; ++I) {
John McCall32daa422010-03-31 01:36:47 +00001672 NamedDecl *D = I.getDecl();
1673 if (isa<UsingShadowDecl>(D))
1674 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1675
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +00001676 // Skip over templated conversion functions; they aren't considered.
John McCall32daa422010-03-31 01:36:47 +00001677 if (isa<FunctionTemplateDecl>(D))
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +00001678 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001679
John McCall32daa422010-03-31 01:36:47 +00001680 CXXConversionDecl *Conv = cast<CXXConversionDecl>(D);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001681
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +00001682 QualType ConvType = Conv->getConversionType().getNonReferenceType();
1683 if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>())
Eli Friedman13578692010-08-05 02:49:48 +00001684 if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType())
Fariborz Jahanian8b915e72009-09-15 22:15:23 +00001685 ObjectPtrConversions.push_back(Conv);
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +00001686 }
Fariborz Jahanian8b915e72009-09-15 22:15:23 +00001687 if (ObjectPtrConversions.size() == 1) {
1688 // We have a single conversion to a pointer-to-object type. Perform
1689 // that conversion.
John McCall32daa422010-03-31 01:36:47 +00001690 // TODO: don't redo the conversion calculation.
John Wiegley429bb272011-04-08 18:41:53 +00001691 ExprResult Res =
1692 PerformImplicitConversion(Ex.get(),
John McCall32daa422010-03-31 01:36:47 +00001693 ObjectPtrConversions.front()->getConversionType(),
John Wiegley429bb272011-04-08 18:41:53 +00001694 AA_Converting);
1695 if (Res.isUsable()) {
1696 Ex = move(Res);
1697 Type = Ex.get()->getType();
Fariborz Jahanian8b915e72009-09-15 22:15:23 +00001698 }
1699 }
1700 else if (ObjectPtrConversions.size() > 1) {
1701 Diag(StartLoc, diag::err_ambiguous_delete_operand)
John Wiegley429bb272011-04-08 18:41:53 +00001702 << Type << Ex.get()->getSourceRange();
John McCall32daa422010-03-31 01:36:47 +00001703 for (unsigned i= 0; i < ObjectPtrConversions.size(); i++)
1704 NoteOverloadCandidate(ObjectPtrConversions[i]);
Fariborz Jahanian8b915e72009-09-15 22:15:23 +00001705 return ExprError();
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +00001706 }
Sebastian Redl28507842009-02-26 14:39:58 +00001707 }
1708
Sebastian Redlf53597f2009-03-15 17:47:39 +00001709 if (!Type->isPointerType())
1710 return ExprError(Diag(StartLoc, diag::err_delete_operand)
John Wiegley429bb272011-04-08 18:41:53 +00001711 << Type << Ex.get()->getSourceRange());
Sebastian Redl28507842009-02-26 14:39:58 +00001712
Ted Kremenek6217b802009-07-29 21:53:49 +00001713 QualType Pointee = Type->getAs<PointerType>()->getPointeeType();
Douglas Gregor94a61572010-05-24 17:01:56 +00001714 if (Pointee->isVoidType() && !isSFINAEContext()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001715 // The C++ standard bans deleting a pointer to a non-object type, which
Douglas Gregor94a61572010-05-24 17:01:56 +00001716 // effectively bans deletion of "void*". However, most compilers support
1717 // this, so we treat it as a warning unless we're in a SFINAE context.
1718 Diag(StartLoc, diag::ext_delete_void_ptr_operand)
John Wiegley429bb272011-04-08 18:41:53 +00001719 << Type << Ex.get()->getSourceRange();
Douglas Gregor94a61572010-05-24 17:01:56 +00001720 } else if (Pointee->isFunctionType() || Pointee->isVoidType())
Sebastian Redlf53597f2009-03-15 17:47:39 +00001721 return ExprError(Diag(StartLoc, diag::err_delete_operand)
John Wiegley429bb272011-04-08 18:41:53 +00001722 << Type << Ex.get()->getSourceRange());
Douglas Gregor8dcb29d2009-03-24 20:13:58 +00001723 else if (!Pointee->isDependentType() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001724 RequireCompleteType(StartLoc, Pointee,
Anders Carlssonb7906612009-08-26 23:45:07 +00001725 PDiag(diag::warn_delete_incomplete)
John Wiegley429bb272011-04-08 18:41:53 +00001726 << Ex.get()->getSourceRange()))
Douglas Gregor8dcb29d2009-03-24 20:13:58 +00001727 return ExprError();
Sebastian Redl28507842009-02-26 14:39:58 +00001728
Douglas Gregor1070c9f2009-09-29 21:38:53 +00001729 // C++ [expr.delete]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001730 // [Note: a pointer to a const type can be the operand of a
1731 // delete-expression; it is not necessary to cast away the constness
1732 // (5.2.11) of the pointer expression before it is used as the operand
Douglas Gregor1070c9f2009-09-29 21:38:53 +00001733 // of the delete-expression. ]
John Wiegley429bb272011-04-08 18:41:53 +00001734 Ex = ImpCastExprToType(Ex.take(), Context.getPointerType(Context.VoidTy),
John McCall2de56d12010-08-25 11:45:40 +00001735 CK_NoOp);
Argyrios Kyrtzidis4076dac2010-09-13 20:15:54 +00001736
1737 if (Pointee->isArrayType() && !ArrayForm) {
1738 Diag(StartLoc, diag::warn_delete_array_type)
John Wiegley429bb272011-04-08 18:41:53 +00001739 << Type << Ex.get()->getSourceRange()
Argyrios Kyrtzidis4076dac2010-09-13 20:15:54 +00001740 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(StartLoc), "[]");
1741 ArrayForm = true;
1742 }
1743
Anders Carlssond67c4c32009-08-16 20:29:29 +00001744 DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName(
1745 ArrayForm ? OO_Array_Delete : OO_Delete);
1746
Argyrios Kyrtzidisd2932982010-08-25 23:14:56 +00001747 QualType PointeeElem = Context.getBaseElementType(Pointee);
1748 if (const RecordType *RT = PointeeElem->getAs<RecordType>()) {
Anders Carlsson78f74552009-11-15 18:45:20 +00001749 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1750
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001751 if (!UseGlobal &&
Anders Carlsson78f74552009-11-15 18:45:20 +00001752 FindDeallocationFunction(StartLoc, RD, DeleteName, OperatorDelete))
Anders Carlsson0ba63ea2009-11-14 03:17:38 +00001753 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001754
John McCall6ec278d2011-01-27 09:37:56 +00001755 // If we're allocating an array of records, check whether the
1756 // usual operator delete[] has a size_t parameter.
1757 if (ArrayForm) {
1758 // If the user specifically asked to use the global allocator,
1759 // we'll need to do the lookup into the class.
1760 if (UseGlobal)
1761 UsualArrayDeleteWantsSize =
1762 doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem);
1763
1764 // Otherwise, the usual operator delete[] should be the
1765 // function we just found.
1766 else if (isa<CXXMethodDecl>(OperatorDelete))
1767 UsualArrayDeleteWantsSize = (OperatorDelete->getNumParams() == 2);
1768 }
1769
Anders Carlsson78f74552009-11-15 18:45:20 +00001770 if (!RD->hasTrivialDestructor())
Douglas Gregor9b623632010-10-12 23:32:35 +00001771 if (CXXDestructorDecl *Dtor = LookupDestructor(RD)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001772 MarkDeclarationReferenced(StartLoc,
Fariborz Jahanian34374e62009-09-03 23:18:17 +00001773 const_cast<CXXDestructorDecl*>(Dtor));
Douglas Gregor9b623632010-10-12 23:32:35 +00001774 DiagnoseUseOfDecl(Dtor, StartLoc);
1775 }
Anders Carlssond67c4c32009-08-16 20:29:29 +00001776 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001777
Anders Carlssond67c4c32009-08-16 20:29:29 +00001778 if (!OperatorDelete) {
Anders Carlsson78f74552009-11-15 18:45:20 +00001779 // Look for a global declaration.
Anders Carlssond67c4c32009-08-16 20:29:29 +00001780 DeclareGlobalNewDelete();
1781 DeclContext *TUDecl = Context.getTranslationUnitDecl();
John Wiegley429bb272011-04-08 18:41:53 +00001782 Expr *Arg = Ex.get();
Mike Stump1eb44332009-09-09 15:08:12 +00001783 if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName,
John Wiegley429bb272011-04-08 18:41:53 +00001784 &Arg, 1, TUDecl, /*AllowMissing=*/false,
Anders Carlssond67c4c32009-08-16 20:29:29 +00001785 OperatorDelete))
1786 return ExprError();
1787 }
Mike Stump1eb44332009-09-09 15:08:12 +00001788
John McCall9c82afc2010-04-20 02:18:25 +00001789 MarkDeclarationReferenced(StartLoc, OperatorDelete);
John McCall6ec278d2011-01-27 09:37:56 +00001790
Douglas Gregord880f522011-02-01 15:50:11 +00001791 // Check access and ambiguity of operator delete and destructor.
1792 if (const RecordType *RT = PointeeElem->getAs<RecordType>()) {
1793 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1794 if (CXXDestructorDecl *Dtor = LookupDestructor(RD)) {
John Wiegley429bb272011-04-08 18:41:53 +00001795 CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor,
Douglas Gregord880f522011-02-01 15:50:11 +00001796 PDiag(diag::err_access_dtor) << PointeeElem);
1797 }
1798 }
1799
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001800 }
1801
Sebastian Redlf53597f2009-03-15 17:47:39 +00001802 return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm,
John McCall6ec278d2011-01-27 09:37:56 +00001803 ArrayFormAsWritten,
1804 UsualArrayDeleteWantsSize,
John Wiegley429bb272011-04-08 18:41:53 +00001805 OperatorDelete, Ex.take(), StartLoc));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001806}
1807
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00001808/// \brief Check the use of the given variable as a C++ condition in an if,
1809/// while, do-while, or switch statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001810ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar,
John McCallf89e55a2010-11-18 06:31:45 +00001811 SourceLocation StmtLoc,
1812 bool ConvertToBoolean) {
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00001813 QualType T = ConditionVar->getType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001814
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00001815 // C++ [stmt.select]p2:
1816 // The declarator shall not specify a function or an array.
1817 if (T->isFunctionType())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001818 return ExprError(Diag(ConditionVar->getLocation(),
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00001819 diag::err_invalid_use_of_function_type)
1820 << ConditionVar->getSourceRange());
1821 else if (T->isArrayType())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001822 return ExprError(Diag(ConditionVar->getLocation(),
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00001823 diag::err_invalid_use_of_array_type)
1824 << ConditionVar->getSourceRange());
Douglas Gregora7605db2009-11-24 16:07:02 +00001825
John Wiegley429bb272011-04-08 18:41:53 +00001826 ExprResult Condition =
1827 Owned(DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
Douglas Gregor40d96a62011-02-28 21:54:11 +00001828 ConditionVar,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001829 ConditionVar->getLocation(),
John McCallf89e55a2010-11-18 06:31:45 +00001830 ConditionVar->getType().getNonReferenceType(),
John Wiegley429bb272011-04-08 18:41:53 +00001831 VK_LValue));
1832 if (ConvertToBoolean) {
1833 Condition = CheckBooleanCondition(Condition.take(), StmtLoc);
1834 if (Condition.isInvalid())
1835 return ExprError();
1836 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001837
John Wiegley429bb272011-04-08 18:41:53 +00001838 return move(Condition);
Douglas Gregor8cfe5a72009-11-23 23:44:04 +00001839}
1840
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +00001841/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
John Wiegley429bb272011-04-08 18:41:53 +00001842ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr) {
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +00001843 // C++ 6.4p4:
1844 // The value of a condition that is an initialized declaration in a statement
1845 // other than a switch statement is the value of the declared variable
1846 // implicitly converted to type bool. If that conversion is ill-formed, the
1847 // program is ill-formed.
1848 // The value of a condition that is an expression is the value of the
1849 // expression, implicitly converted to bool.
1850 //
Douglas Gregor09f41cf2009-01-14 15:45:31 +00001851 return PerformContextuallyConvertToBool(CondExpr);
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +00001852}
Douglas Gregor77a52232008-09-12 00:47:35 +00001853
1854/// Helper function to determine whether this is the (deprecated) C++
1855/// conversion from a string literal to a pointer to non-const char or
1856/// non-const wchar_t (for narrow and wide string literals,
1857/// respectively).
Mike Stump1eb44332009-09-09 15:08:12 +00001858bool
Douglas Gregor77a52232008-09-12 00:47:35 +00001859Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
1860 // Look inside the implicit cast, if it exists.
1861 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
1862 From = Cast->getSubExpr();
1863
1864 // A string literal (2.13.4) that is not a wide string literal can
1865 // be converted to an rvalue of type "pointer to char"; a wide
1866 // string literal can be converted to an rvalue of type "pointer
1867 // to wchar_t" (C++ 4.2p2).
Douglas Gregor1984eb92010-06-22 23:47:37 +00001868 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens()))
Ted Kremenek6217b802009-07-29 21:53:49 +00001869 if (const PointerType *ToPtrType = ToType->getAs<PointerType>())
Mike Stump1eb44332009-09-09 15:08:12 +00001870 if (const BuiltinType *ToPointeeType
John McCall183700f2009-09-21 23:43:11 +00001871 = ToPtrType->getPointeeType()->getAs<BuiltinType>()) {
Douglas Gregor77a52232008-09-12 00:47:35 +00001872 // This conversion is considered only when there is an
1873 // explicit appropriate pointer target type (C++ 4.2p2).
John McCall0953e762009-09-24 19:53:00 +00001874 if (!ToPtrType->getPointeeType().hasQualifiers() &&
Douglas Gregor77a52232008-09-12 00:47:35 +00001875 ((StrLit->isWide() && ToPointeeType->isWideCharType()) ||
1876 (!StrLit->isWide() &&
1877 (ToPointeeType->getKind() == BuiltinType::Char_U ||
1878 ToPointeeType->getKind() == BuiltinType::Char_S))))
1879 return true;
1880 }
1881
1882 return false;
1883}
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001884
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001885static ExprResult BuildCXXCastArgument(Sema &S,
John McCall2de56d12010-08-25 11:45:40 +00001886 SourceLocation CastLoc,
1887 QualType Ty,
1888 CastKind Kind,
1889 CXXMethodDecl *Method,
Douglas Gregor83eecbe2011-01-20 01:32:05 +00001890 NamedDecl *FoundDecl,
John McCall2de56d12010-08-25 11:45:40 +00001891 Expr *From) {
Douglas Gregorba70ab62010-04-16 22:17:36 +00001892 switch (Kind) {
1893 default: assert(0 && "Unhandled cast kind!");
John McCall2de56d12010-08-25 11:45:40 +00001894 case CK_ConstructorConversion: {
John McCallca0408f2010-08-23 06:44:23 +00001895 ASTOwningVector<Expr*> ConstructorArgs(S);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001896
Douglas Gregorba70ab62010-04-16 22:17:36 +00001897 if (S.CompleteConstructorCall(cast<CXXConstructorDecl>(Method),
John McCallf312b1e2010-08-26 23:41:50 +00001898 MultiExprArg(&From, 1),
Douglas Gregorba70ab62010-04-16 22:17:36 +00001899 CastLoc, ConstructorArgs))
John McCallf312b1e2010-08-26 23:41:50 +00001900 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001901
1902 ExprResult Result =
1903 S.BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method),
John McCall7a1fad32010-08-24 07:32:53 +00001904 move_arg(ConstructorArgs),
Chandler Carruth428edaf2010-10-25 08:47:36 +00001905 /*ZeroInit*/ false, CXXConstructExpr::CK_Complete,
1906 SourceRange());
Douglas Gregorba70ab62010-04-16 22:17:36 +00001907 if (Result.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001908 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001909
Douglas Gregorba70ab62010-04-16 22:17:36 +00001910 return S.MaybeBindToTemporary(Result.takeAs<Expr>());
1911 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001912
John McCall2de56d12010-08-25 11:45:40 +00001913 case CK_UserDefinedConversion: {
Douglas Gregorba70ab62010-04-16 22:17:36 +00001914 assert(!From->getType()->isPointerType() && "Arg can't have pointer type!");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001915
Douglas Gregorba70ab62010-04-16 22:17:36 +00001916 // Create an implicit call expr that calls it.
Douglas Gregor83eecbe2011-01-20 01:32:05 +00001917 ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Method);
Douglas Gregorf2ae5262011-01-20 00:18:04 +00001918 if (Result.isInvalid())
1919 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001920
Douglas Gregorf2ae5262011-01-20 00:18:04 +00001921 return S.MaybeBindToTemporary(Result.get());
Douglas Gregorba70ab62010-04-16 22:17:36 +00001922 }
1923 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001924}
Douglas Gregorba70ab62010-04-16 22:17:36 +00001925
Douglas Gregor09f41cf2009-01-14 15:45:31 +00001926/// PerformImplicitConversion - Perform an implicit conversion of the
1927/// expression From to the type ToType using the pre-computed implicit
John Wiegley429bb272011-04-08 18:41:53 +00001928/// conversion sequence ICS. Returns the converted
Douglas Gregor68647482009-12-16 03:45:30 +00001929/// expression. Action is the kind of conversion we're performing,
Douglas Gregor09f41cf2009-01-14 15:45:31 +00001930/// used in the error message.
John Wiegley429bb272011-04-08 18:41:53 +00001931ExprResult
1932Sema::PerformImplicitConversion(Expr *From, QualType ToType,
Douglas Gregor09f41cf2009-01-14 15:45:31 +00001933 const ImplicitConversionSequence &ICS,
Douglas Gregor14d0aee2011-01-27 00:58:17 +00001934 AssignmentAction Action, bool CStyle) {
John McCall1d318332010-01-12 00:44:57 +00001935 switch (ICS.getKind()) {
John Wiegley429bb272011-04-08 18:41:53 +00001936 case ImplicitConversionSequence::StandardConversion: {
1937 ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard,
1938 Action, CStyle);
1939 if (Res.isInvalid())
1940 return ExprError();
1941 From = Res.take();
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001942 break;
John Wiegley429bb272011-04-08 18:41:53 +00001943 }
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001944
Anders Carlssonf6c213a2009-09-15 06:28:28 +00001945 case ImplicitConversionSequence::UserDefinedConversion: {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001946
Fariborz Jahanian7fe5d722009-08-28 22:04:50 +00001947 FunctionDecl *FD = ICS.UserDefined.ConversionFunction;
John McCalldaa8e4e2010-11-15 09:13:47 +00001948 CastKind CastKind;
Anders Carlssonf6c213a2009-09-15 06:28:28 +00001949 QualType BeforeToType;
1950 if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) {
John McCall2de56d12010-08-25 11:45:40 +00001951 CastKind = CK_UserDefinedConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001952
Anders Carlssonf6c213a2009-09-15 06:28:28 +00001953 // If the user-defined conversion is specified by a conversion function,
1954 // the initial standard conversion sequence converts the source type to
1955 // the implicit object parameter of the conversion function.
1956 BeforeToType = Context.getTagDeclType(Conv->getParent());
John McCall9ec94452010-12-04 09:57:16 +00001957 } else {
1958 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD);
John McCall2de56d12010-08-25 11:45:40 +00001959 CastKind = CK_ConstructorConversion;
Fariborz Jahanian966256a2009-11-06 00:23:08 +00001960 // Do no conversion if dealing with ... for the first conversion.
Douglas Gregore44201a2009-11-20 02:31:03 +00001961 if (!ICS.UserDefined.EllipsisConversion) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001962 // If the user-defined conversion is specified by a constructor, the
Fariborz Jahanian966256a2009-11-06 00:23:08 +00001963 // initial standard conversion sequence converts the source type to the
1964 // type required by the argument of the constructor
Douglas Gregore44201a2009-11-20 02:31:03 +00001965 BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType();
1966 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001967 }
Douglas Gregora3998bd2010-12-02 21:47:04 +00001968 // Watch out for elipsis conversion.
Fariborz Jahanian4c0cea22009-11-06 00:55:14 +00001969 if (!ICS.UserDefined.EllipsisConversion) {
John Wiegley429bb272011-04-08 18:41:53 +00001970 ExprResult Res =
1971 PerformImplicitConversion(From, BeforeToType,
1972 ICS.UserDefined.Before, AA_Converting,
1973 CStyle);
1974 if (Res.isInvalid())
1975 return ExprError();
1976 From = Res.take();
Fariborz Jahanian966256a2009-11-06 00:23:08 +00001977 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001978
1979 ExprResult CastArg
Douglas Gregorba70ab62010-04-16 22:17:36 +00001980 = BuildCXXCastArgument(*this,
1981 From->getLocStart(),
Anders Carlsson0aebc812009-09-09 21:33:21 +00001982 ToType.getNonReferenceType(),
Douglas Gregor83eecbe2011-01-20 01:32:05 +00001983 CastKind, cast<CXXMethodDecl>(FD),
1984 ICS.UserDefined.FoundConversionFunction,
John McCall9ae2f072010-08-23 23:25:46 +00001985 From);
Anders Carlsson0aebc812009-09-09 21:33:21 +00001986
1987 if (CastArg.isInvalid())
John Wiegley429bb272011-04-08 18:41:53 +00001988 return ExprError();
Eli Friedmand8889622009-11-27 04:41:50 +00001989
John Wiegley429bb272011-04-08 18:41:53 +00001990 From = CastArg.take();
Eli Friedmand8889622009-11-27 04:41:50 +00001991
Eli Friedmand8889622009-11-27 04:41:50 +00001992 return PerformImplicitConversion(From, ToType, ICS.UserDefined.After,
Douglas Gregor14d0aee2011-01-27 00:58:17 +00001993 AA_Converting, CStyle);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +00001994 }
John McCall1d318332010-01-12 00:44:57 +00001995
1996 case ImplicitConversionSequence::AmbiguousConversion:
John McCall120d63c2010-08-24 20:38:10 +00001997 ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(),
John McCall1d318332010-01-12 00:44:57 +00001998 PDiag(diag::err_typecheck_ambiguous_condition)
1999 << From->getSourceRange());
John Wiegley429bb272011-04-08 18:41:53 +00002000 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002001
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002002 case ImplicitConversionSequence::EllipsisConversion:
2003 assert(false && "Cannot perform an ellipsis conversion");
John Wiegley429bb272011-04-08 18:41:53 +00002004 return Owned(From);
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002005
2006 case ImplicitConversionSequence::BadConversion:
John Wiegley429bb272011-04-08 18:41:53 +00002007 return ExprError();
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002008 }
2009
2010 // Everything went well.
John Wiegley429bb272011-04-08 18:41:53 +00002011 return Owned(From);
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002012}
2013
2014/// PerformImplicitConversion - Perform an implicit conversion of the
2015/// expression From to the type ToType by following the standard
John Wiegley429bb272011-04-08 18:41:53 +00002016/// conversion sequence SCS. Returns the converted
Douglas Gregor45920e82008-12-19 17:40:08 +00002017/// expression. Flavor is the context in which we're performing this
2018/// conversion, for use in error messages.
John Wiegley429bb272011-04-08 18:41:53 +00002019ExprResult
2020Sema::PerformImplicitConversion(Expr *From, QualType ToType,
Douglas Gregor45920e82008-12-19 17:40:08 +00002021 const StandardConversionSequence& SCS,
Douglas Gregor14d0aee2011-01-27 00:58:17 +00002022 AssignmentAction Action, bool CStyle) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002023 // Overall FIXME: we are recomputing too many types here and doing far too
2024 // much extra work. What this means is that we need to keep track of more
2025 // information that is computed when we try the implicit conversion initially,
2026 // so that we don't need to recompute anything here.
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002027 QualType FromType = From->getType();
2028
Douglas Gregor225c41e2008-11-03 19:09:14 +00002029 if (SCS.CopyConstructor) {
Anders Carlsson7c3e8a12009-05-19 04:45:15 +00002030 // FIXME: When can ToType be a reference type?
2031 assert(!ToType->isReferenceType());
Fariborz Jahanianb3c47742009-09-25 18:59:21 +00002032 if (SCS.Second == ICK_Derived_To_Base) {
John McCallca0408f2010-08-23 06:44:23 +00002033 ASTOwningVector<Expr*> ConstructorArgs(*this);
Fariborz Jahanianb3c47742009-09-25 18:59:21 +00002034 if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor),
John McCallca0408f2010-08-23 06:44:23 +00002035 MultiExprArg(*this, &From, 1),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002036 /*FIXME:ConstructLoc*/SourceLocation(),
Fariborz Jahanianb3c47742009-09-25 18:59:21 +00002037 ConstructorArgs))
John Wiegley429bb272011-04-08 18:41:53 +00002038 return ExprError();
2039 return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
2040 ToType, SCS.CopyConstructor,
2041 move_arg(ConstructorArgs),
2042 /*ZeroInit*/ false,
2043 CXXConstructExpr::CK_Complete,
2044 SourceRange());
Fariborz Jahanianb3c47742009-09-25 18:59:21 +00002045 }
John Wiegley429bb272011-04-08 18:41:53 +00002046 return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
2047 ToType, SCS.CopyConstructor,
2048 MultiExprArg(*this, &From, 1),
2049 /*ZeroInit*/ false,
2050 CXXConstructExpr::CK_Complete,
2051 SourceRange());
Douglas Gregor225c41e2008-11-03 19:09:14 +00002052 }
2053
Douglas Gregorad4e02f2010-04-29 18:24:40 +00002054 // Resolve overloaded function references.
2055 if (Context.hasSameType(FromType, Context.OverloadTy)) {
2056 DeclAccessPair Found;
2057 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType,
2058 true, Found);
2059 if (!Fn)
John Wiegley429bb272011-04-08 18:41:53 +00002060 return ExprError();
Douglas Gregorad4e02f2010-04-29 18:24:40 +00002061
2062 if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00002063 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002064
Douglas Gregorad4e02f2010-04-29 18:24:40 +00002065 From = FixOverloadedFunctionReference(From, Found, Fn);
2066 FromType = From->getType();
2067 }
2068
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002069 // Perform the first implicit conversion.
2070 switch (SCS.First) {
2071 case ICK_Identity:
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002072 // Nothing to do.
2073 break;
2074
John McCallf6a16482010-12-04 03:47:34 +00002075 case ICK_Lvalue_To_Rvalue:
2076 // Should this get its own ICK?
2077 if (From->getObjectKind() == OK_ObjCProperty) {
John Wiegley429bb272011-04-08 18:41:53 +00002078 ExprResult FromRes = ConvertPropertyForRValue(From);
2079 if (FromRes.isInvalid())
2080 return ExprError();
2081 From = FromRes.take();
John McCall241d5582010-12-07 22:54:16 +00002082 if (!From->isGLValue()) break;
John McCallf6a16482010-12-04 03:47:34 +00002083 }
2084
Chandler Carruth35001ca2011-02-17 21:10:52 +00002085 // Check for trivial buffer overflows.
Ted Kremenek3aea4da2011-03-01 18:41:00 +00002086 CheckArrayAccess(From);
Chandler Carruth35001ca2011-02-17 21:10:52 +00002087
John McCallf6a16482010-12-04 03:47:34 +00002088 FromType = FromType.getUnqualifiedType();
2089 From = ImplicitCastExpr::Create(Context, FromType, CK_LValueToRValue,
2090 From, 0, VK_RValue);
2091 break;
2092
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002093 case ICK_Array_To_Pointer:
Douglas Gregor48f3bb92009-02-18 21:56:37 +00002094 FromType = Context.getArrayDecayedType(FromType);
John Wiegley429bb272011-04-08 18:41:53 +00002095 From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay).take();
Douglas Gregor48f3bb92009-02-18 21:56:37 +00002096 break;
2097
2098 case ICK_Function_To_Pointer:
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002099 FromType = Context.getPointerType(FromType);
John Wiegley429bb272011-04-08 18:41:53 +00002100 From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay).take();
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002101 break;
2102
2103 default:
2104 assert(false && "Improper first standard conversion");
2105 break;
2106 }
2107
2108 // Perform the second implicit conversion
2109 switch (SCS.Second) {
2110 case ICK_Identity:
Sebastian Redl2c7588f2009-10-10 12:04:10 +00002111 // If both sides are functions (or pointers/references to them), there could
2112 // be incompatible exception declarations.
2113 if (CheckExceptionSpecCompatibility(From, ToType))
John Wiegley429bb272011-04-08 18:41:53 +00002114 return ExprError();
Sebastian Redl2c7588f2009-10-10 12:04:10 +00002115 // Nothing else to do.
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002116 break;
2117
Douglas Gregor43c79c22009-12-09 00:47:37 +00002118 case ICK_NoReturn_Adjustment:
2119 // If both sides are functions (or pointers/references to them), there could
2120 // be incompatible exception declarations.
2121 if (CheckExceptionSpecCompatibility(From, ToType))
John Wiegley429bb272011-04-08 18:41:53 +00002122 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002123
John Wiegley429bb272011-04-08 18:41:53 +00002124 From = ImpCastExprToType(From, ToType, CK_NoOp).take();
Douglas Gregor43c79c22009-12-09 00:47:37 +00002125 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002126
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002127 case ICK_Integral_Promotion:
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002128 case ICK_Integral_Conversion:
John Wiegley429bb272011-04-08 18:41:53 +00002129 From = ImpCastExprToType(From, ToType, CK_IntegralCast).take();
Eli Friedman73c39ab2009-10-20 08:27:19 +00002130 break;
2131
2132 case ICK_Floating_Promotion:
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002133 case ICK_Floating_Conversion:
John Wiegley429bb272011-04-08 18:41:53 +00002134 From = ImpCastExprToType(From, ToType, CK_FloatingCast).take();
Eli Friedman73c39ab2009-10-20 08:27:19 +00002135 break;
2136
2137 case ICK_Complex_Promotion:
John McCalldaa8e4e2010-11-15 09:13:47 +00002138 case ICK_Complex_Conversion: {
2139 QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType();
2140 QualType ToEl = ToType->getAs<ComplexType>()->getElementType();
2141 CastKind CK;
2142 if (FromEl->isRealFloatingType()) {
2143 if (ToEl->isRealFloatingType())
2144 CK = CK_FloatingComplexCast;
2145 else
2146 CK = CK_FloatingComplexToIntegralComplex;
2147 } else if (ToEl->isRealFloatingType()) {
2148 CK = CK_IntegralComplexToFloatingComplex;
2149 } else {
2150 CK = CK_IntegralComplexCast;
2151 }
John Wiegley429bb272011-04-08 18:41:53 +00002152 From = ImpCastExprToType(From, ToType, CK).take();
Eli Friedman73c39ab2009-10-20 08:27:19 +00002153 break;
John McCalldaa8e4e2010-11-15 09:13:47 +00002154 }
Eli Friedman73c39ab2009-10-20 08:27:19 +00002155
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002156 case ICK_Floating_Integral:
Douglas Gregor0c293ea2010-06-22 23:07:26 +00002157 if (ToType->isRealFloatingType())
John Wiegley429bb272011-04-08 18:41:53 +00002158 From = ImpCastExprToType(From, ToType, CK_IntegralToFloating).take();
Eli Friedman73c39ab2009-10-20 08:27:19 +00002159 else
John Wiegley429bb272011-04-08 18:41:53 +00002160 From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral).take();
Eli Friedman73c39ab2009-10-20 08:27:19 +00002161 break;
2162
Douglas Gregorf9201e02009-02-11 23:02:49 +00002163 case ICK_Compatible_Conversion:
John Wiegley429bb272011-04-08 18:41:53 +00002164 From = ImpCastExprToType(From, ToType, CK_NoOp).take();
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002165 break;
2166
Anders Carlsson61faec12009-09-12 04:46:44 +00002167 case ICK_Pointer_Conversion: {
Douglas Gregora3998bd2010-12-02 21:47:04 +00002168 if (SCS.IncompatibleObjC && Action != AA_Casting) {
Douglas Gregor45920e82008-12-19 17:40:08 +00002169 // Diagnose incompatible Objective-C conversions
Fariborz Jahanian84950c72011-03-21 19:08:42 +00002170 if (Action == AA_Initializing)
2171 Diag(From->getSourceRange().getBegin(),
2172 diag::ext_typecheck_convert_incompatible_pointer)
2173 << ToType << From->getType() << Action
2174 << From->getSourceRange();
2175 else
2176 Diag(From->getSourceRange().getBegin(),
2177 diag::ext_typecheck_convert_incompatible_pointer)
2178 << From->getType() << ToType << Action
2179 << From->getSourceRange();
Douglas Gregor45920e82008-12-19 17:40:08 +00002180 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002181
John McCalldaa8e4e2010-11-15 09:13:47 +00002182 CastKind Kind = CK_Invalid;
John McCallf871d0c2010-08-07 06:22:56 +00002183 CXXCastPath BasePath;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00002184 if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle))
John Wiegley429bb272011-04-08 18:41:53 +00002185 return ExprError();
2186 From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath).take();
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002187 break;
Anders Carlsson61faec12009-09-12 04:46:44 +00002188 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002189
Anders Carlsson61faec12009-09-12 04:46:44 +00002190 case ICK_Pointer_Member: {
John McCalldaa8e4e2010-11-15 09:13:47 +00002191 CastKind Kind = CK_Invalid;
John McCallf871d0c2010-08-07 06:22:56 +00002192 CXXCastPath BasePath;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00002193 if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle))
John Wiegley429bb272011-04-08 18:41:53 +00002194 return ExprError();
Sebastian Redl2c7588f2009-10-10 12:04:10 +00002195 if (CheckExceptionSpecCompatibility(From, ToType))
John Wiegley429bb272011-04-08 18:41:53 +00002196 return ExprError();
2197 From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath).take();
Anders Carlsson61faec12009-09-12 04:46:44 +00002198 break;
2199 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002200
Abramo Bagnara737d5442011-04-07 09:26:19 +00002201 case ICK_Boolean_Conversion:
John Wiegley429bb272011-04-08 18:41:53 +00002202 From = ImpCastExprToType(From, Context.BoolTy,
2203 ScalarTypeToBooleanCastKind(FromType)).take();
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002204 break;
2205
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002206 case ICK_Derived_To_Base: {
John McCallf871d0c2010-08-07 06:22:56 +00002207 CXXCastPath BasePath;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002208 if (CheckDerivedToBaseConversion(From->getType(),
Douglas Gregorb7a86f52009-11-06 01:02:41 +00002209 ToType.getNonReferenceType(),
2210 From->getLocStart(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002211 From->getSourceRange(),
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002212 &BasePath,
Douglas Gregor14d0aee2011-01-27 00:58:17 +00002213 CStyle))
John Wiegley429bb272011-04-08 18:41:53 +00002214 return ExprError();
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002215
John Wiegley429bb272011-04-08 18:41:53 +00002216 From = ImpCastExprToType(From, ToType.getNonReferenceType(),
John McCall2de56d12010-08-25 11:45:40 +00002217 CK_DerivedToBase, CastCategory(From),
John Wiegley429bb272011-04-08 18:41:53 +00002218 &BasePath).take();
Douglas Gregorb7a86f52009-11-06 01:02:41 +00002219 break;
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002220 }
2221
Douglas Gregorfb4a5432010-05-18 22:42:18 +00002222 case ICK_Vector_Conversion:
John Wiegley429bb272011-04-08 18:41:53 +00002223 From = ImpCastExprToType(From, ToType, CK_BitCast).take();
Douglas Gregorfb4a5432010-05-18 22:42:18 +00002224 break;
2225
2226 case ICK_Vector_Splat:
John Wiegley429bb272011-04-08 18:41:53 +00002227 From = ImpCastExprToType(From, ToType, CK_VectorSplat).take();
Douglas Gregorfb4a5432010-05-18 22:42:18 +00002228 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002229
Douglas Gregorfb4a5432010-05-18 22:42:18 +00002230 case ICK_Complex_Real:
John McCalldaa8e4e2010-11-15 09:13:47 +00002231 // Case 1. x -> _Complex y
2232 if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) {
2233 QualType ElType = ToComplex->getElementType();
2234 bool isFloatingComplex = ElType->isRealFloatingType();
2235
2236 // x -> y
2237 if (Context.hasSameUnqualifiedType(ElType, From->getType())) {
2238 // do nothing
2239 } else if (From->getType()->isRealFloatingType()) {
John Wiegley429bb272011-04-08 18:41:53 +00002240 From = ImpCastExprToType(From, ElType,
2241 isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).take();
John McCalldaa8e4e2010-11-15 09:13:47 +00002242 } else {
2243 assert(From->getType()->isIntegerType());
John Wiegley429bb272011-04-08 18:41:53 +00002244 From = ImpCastExprToType(From, ElType,
2245 isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).take();
John McCalldaa8e4e2010-11-15 09:13:47 +00002246 }
2247 // y -> _Complex y
John Wiegley429bb272011-04-08 18:41:53 +00002248 From = ImpCastExprToType(From, ToType,
John McCalldaa8e4e2010-11-15 09:13:47 +00002249 isFloatingComplex ? CK_FloatingRealToComplex
John Wiegley429bb272011-04-08 18:41:53 +00002250 : CK_IntegralRealToComplex).take();
John McCalldaa8e4e2010-11-15 09:13:47 +00002251
2252 // Case 2. _Complex x -> y
2253 } else {
2254 const ComplexType *FromComplex = From->getType()->getAs<ComplexType>();
2255 assert(FromComplex);
2256
2257 QualType ElType = FromComplex->getElementType();
2258 bool isFloatingComplex = ElType->isRealFloatingType();
2259
2260 // _Complex x -> x
John Wiegley429bb272011-04-08 18:41:53 +00002261 From = ImpCastExprToType(From, ElType,
John McCalldaa8e4e2010-11-15 09:13:47 +00002262 isFloatingComplex ? CK_FloatingComplexToReal
John Wiegley429bb272011-04-08 18:41:53 +00002263 : CK_IntegralComplexToReal).take();
John McCalldaa8e4e2010-11-15 09:13:47 +00002264
2265 // x -> y
2266 if (Context.hasSameUnqualifiedType(ElType, ToType)) {
2267 // do nothing
2268 } else if (ToType->isRealFloatingType()) {
John Wiegley429bb272011-04-08 18:41:53 +00002269 From = ImpCastExprToType(From, ToType,
2270 isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating).take();
John McCalldaa8e4e2010-11-15 09:13:47 +00002271 } else {
2272 assert(ToType->isIntegerType());
John Wiegley429bb272011-04-08 18:41:53 +00002273 From = ImpCastExprToType(From, ToType,
2274 isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast).take();
John McCalldaa8e4e2010-11-15 09:13:47 +00002275 }
2276 }
Douglas Gregorfb4a5432010-05-18 22:42:18 +00002277 break;
Fariborz Jahaniane3c8c642011-02-12 19:07:46 +00002278
2279 case ICK_Block_Pointer_Conversion: {
John Wiegley429bb272011-04-08 18:41:53 +00002280 From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast,
2281 VK_RValue).take();
Fariborz Jahaniane3c8c642011-02-12 19:07:46 +00002282 break;
2283 }
2284
Fariborz Jahaniand97f5582011-03-23 19:50:54 +00002285 case ICK_TransparentUnionConversion: {
John Wiegley429bb272011-04-08 18:41:53 +00002286 ExprResult FromRes = Owned(From);
Fariborz Jahaniand97f5582011-03-23 19:50:54 +00002287 Sema::AssignConvertType ConvTy =
John Wiegley429bb272011-04-08 18:41:53 +00002288 CheckTransparentUnionArgumentConstraints(ToType, FromRes);
2289 if (FromRes.isInvalid())
2290 return ExprError();
2291 From = FromRes.take();
Fariborz Jahaniand97f5582011-03-23 19:50:54 +00002292 assert ((ConvTy == Sema::Compatible) &&
2293 "Improper transparent union conversion");
2294 (void)ConvTy;
2295 break;
2296 }
2297
Douglas Gregorfb4a5432010-05-18 22:42:18 +00002298 case ICK_Lvalue_To_Rvalue:
2299 case ICK_Array_To_Pointer:
2300 case ICK_Function_To_Pointer:
2301 case ICK_Qualification:
2302 case ICK_Num_Conversion_Kinds:
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002303 assert(false && "Improper second standard conversion");
2304 break;
2305 }
2306
2307 switch (SCS.Third) {
2308 case ICK_Identity:
2309 // Nothing to do.
2310 break;
2311
Sebastian Redl906082e2010-07-20 04:20:21 +00002312 case ICK_Qualification: {
2313 // The qualification keeps the category of the inner expression, unless the
2314 // target type isn't a reference.
John McCall5baba9d2010-08-25 10:28:54 +00002315 ExprValueKind VK = ToType->isReferenceType() ?
2316 CastCategory(From) : VK_RValue;
John Wiegley429bb272011-04-08 18:41:53 +00002317 From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context),
2318 CK_NoOp, VK).take();
Douglas Gregora9bff302010-02-28 18:30:25 +00002319
Douglas Gregor069a6da2011-03-14 16:13:32 +00002320 if (SCS.DeprecatedStringLiteralToCharPtr &&
2321 !getLangOptions().WritableStrings)
Douglas Gregora9bff302010-02-28 18:30:25 +00002322 Diag(From->getLocStart(), diag::warn_deprecated_string_literal_conversion)
2323 << ToType.getNonReferenceType();
2324
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002325 break;
Sebastian Redl906082e2010-07-20 04:20:21 +00002326 }
2327
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002328 default:
Douglas Gregorfb4a5432010-05-18 22:42:18 +00002329 assert(false && "Improper third standard conversion");
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002330 break;
2331 }
2332
John Wiegley429bb272011-04-08 18:41:53 +00002333 return Owned(From);
Douglas Gregor94b1dd22008-10-24 04:54:22 +00002334}
2335
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002336ExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait UTT,
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002337 SourceLocation KWLoc,
2338 ParsedType Ty,
2339 SourceLocation RParen) {
2340 TypeSourceInfo *TSInfo;
2341 QualType T = GetTypeFromParser(Ty, &TSInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002343 if (!TSInfo)
2344 TSInfo = Context.getTrivialTypeSourceInfo(T);
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002345 return BuildUnaryTypeTrait(UTT, KWLoc, TSInfo, RParen);
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002346}
2347
Sebastian Redlf8aca862010-09-14 23:40:14 +00002348static bool EvaluateUnaryTypeTrait(Sema &Self, UnaryTypeTrait UTT, QualType T,
2349 SourceLocation KeyLoc) {
Douglas Gregora0506182011-01-27 20:35:44 +00002350 // FIXME: For many of these traits, we need a complete type before we can
2351 // check these properties.
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002352 assert(!T->isDependentType() &&
2353 "Cannot evaluate traits for dependent types.");
2354 ASTContext &C = Self.Context;
2355 switch(UTT) {
2356 default: assert(false && "Unknown type trait or not implemented");
2357 case UTT_IsPOD: return T->isPODType();
2358 case UTT_IsLiteral: return T->isLiteralType();
2359 case UTT_IsClass: // Fallthrough
2360 case UTT_IsUnion:
2361 if (const RecordType *Record = T->getAs<RecordType>()) {
2362 bool Union = Record->getDecl()->isUnion();
2363 return UTT == UTT_IsUnion ? Union : !Union;
2364 }
2365 return false;
2366 case UTT_IsEnum: return T->isEnumeralType();
2367 case UTT_IsPolymorphic:
2368 if (const RecordType *Record = T->getAs<RecordType>()) {
2369 // Type traits are only parsed in C++, so we've got CXXRecords.
2370 return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic();
2371 }
2372 return false;
2373 case UTT_IsAbstract:
2374 if (const RecordType *RT = T->getAs<RecordType>())
2375 return cast<CXXRecordDecl>(RT->getDecl())->isAbstract();
2376 return false;
2377 case UTT_IsEmpty:
2378 if (const RecordType *Record = T->getAs<RecordType>()) {
2379 return !Record->getDecl()->isUnion()
2380 && cast<CXXRecordDecl>(Record->getDecl())->isEmpty();
2381 }
2382 return false;
2383 case UTT_HasTrivialConstructor:
2384 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
2385 // If __is_pod (type) is true then the trait is true, else if type is
2386 // a cv class or union type (or array thereof) with a trivial default
2387 // constructor ([class.ctor]) then the trait is true, else it is false.
2388 if (T->isPODType())
2389 return true;
2390 if (const RecordType *RT =
2391 C.getBaseElementType(T)->getAs<RecordType>())
2392 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor();
2393 return false;
2394 case UTT_HasTrivialCopy:
2395 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
2396 // If __is_pod (type) is true or type is a reference type then
2397 // the trait is true, else if type is a cv class or union type
2398 // with a trivial copy constructor ([class.copy]) then the trait
2399 // is true, else it is false.
2400 if (T->isPODType() || T->isReferenceType())
2401 return true;
2402 if (const RecordType *RT = T->getAs<RecordType>())
2403 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor();
2404 return false;
2405 case UTT_HasTrivialAssign:
2406 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
2407 // If type is const qualified or is a reference type then the
2408 // trait is false. Otherwise if __is_pod (type) is true then the
2409 // trait is true, else if type is a cv class or union type with
2410 // a trivial copy assignment ([class.copy]) then the trait is
2411 // true, else it is false.
2412 // Note: the const and reference restrictions are interesting,
2413 // given that const and reference members don't prevent a class
2414 // from having a trivial copy assignment operator (but do cause
2415 // errors if the copy assignment operator is actually used, q.v.
2416 // [class.copy]p12).
2417
2418 if (C.getBaseElementType(T).isConstQualified())
2419 return false;
2420 if (T->isPODType())
2421 return true;
2422 if (const RecordType *RT = T->getAs<RecordType>())
2423 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment();
2424 return false;
2425 case UTT_HasTrivialDestructor:
2426 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
2427 // If __is_pod (type) is true or type is a reference type
2428 // then the trait is true, else if type is a cv class or union
2429 // type (or array thereof) with a trivial destructor
2430 // ([class.dtor]) then the trait is true, else it is
2431 // false.
2432 if (T->isPODType() || T->isReferenceType())
2433 return true;
2434 if (const RecordType *RT =
2435 C.getBaseElementType(T)->getAs<RecordType>())
2436 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor();
2437 return false;
2438 // TODO: Propagate nothrowness for implicitly declared special members.
2439 case UTT_HasNothrowAssign:
2440 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
2441 // If type is const qualified or is a reference type then the
2442 // trait is false. Otherwise if __has_trivial_assign (type)
2443 // is true then the trait is true, else if type is a cv class
2444 // or union type with copy assignment operators that are known
2445 // not to throw an exception then the trait is true, else it is
2446 // false.
2447 if (C.getBaseElementType(T).isConstQualified())
2448 return false;
2449 if (T->isReferenceType())
2450 return false;
2451 if (T->isPODType())
2452 return true;
2453 if (const RecordType *RT = T->getAs<RecordType>()) {
2454 CXXRecordDecl* RD = cast<CXXRecordDecl>(RT->getDecl());
2455 if (RD->hasTrivialCopyAssignment())
2456 return true;
2457
2458 bool FoundAssign = false;
2459 bool AllNoThrow = true;
2460 DeclarationName Name = C.DeclarationNames.getCXXOperatorName(OO_Equal);
Sebastian Redlf8aca862010-09-14 23:40:14 +00002461 LookupResult Res(Self, DeclarationNameInfo(Name, KeyLoc),
2462 Sema::LookupOrdinaryName);
2463 if (Self.LookupQualifiedName(Res, RD)) {
2464 for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end();
2465 Op != OpEnd; ++Op) {
2466 CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op);
2467 if (Operator->isCopyAssignmentOperator()) {
2468 FoundAssign = true;
2469 const FunctionProtoType *CPT
2470 = Operator->getType()->getAs<FunctionProtoType>();
Sebastian Redl8026f6d2011-03-13 17:09:40 +00002471 if (!CPT->isNothrow(Self.Context)) {
Sebastian Redlf8aca862010-09-14 23:40:14 +00002472 AllNoThrow = false;
2473 break;
2474 }
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002475 }
2476 }
2477 }
2478
2479 return FoundAssign && AllNoThrow;
2480 }
2481 return false;
2482 case UTT_HasNothrowCopy:
2483 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
2484 // If __has_trivial_copy (type) is true then the trait is true, else
2485 // if type is a cv class or union type with copy constructors that are
2486 // known not to throw an exception then the trait is true, else it is
2487 // false.
2488 if (T->isPODType() || T->isReferenceType())
2489 return true;
2490 if (const RecordType *RT = T->getAs<RecordType>()) {
2491 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
2492 if (RD->hasTrivialCopyConstructor())
2493 return true;
2494
2495 bool FoundConstructor = false;
2496 bool AllNoThrow = true;
2497 unsigned FoundTQs;
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002498 DeclContext::lookup_const_iterator Con, ConEnd;
Sebastian Redl5f4e8992010-09-13 21:10:20 +00002499 for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD);
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002500 Con != ConEnd; ++Con) {
Sebastian Redl08295a52010-09-13 22:18:28 +00002501 // A template constructor is never a copy constructor.
2502 // FIXME: However, it may actually be selected at the actual overload
2503 // resolution point.
2504 if (isa<FunctionTemplateDecl>(*Con))
2505 continue;
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002506 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
2507 if (Constructor->isCopyConstructor(FoundTQs)) {
2508 FoundConstructor = true;
2509 const FunctionProtoType *CPT
2510 = Constructor->getType()->getAs<FunctionProtoType>();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002511 // FIXME: check whether evaluating default arguments can throw.
Sebastian Redl751025d2010-09-13 22:02:47 +00002512 // For now, we'll be conservative and assume that they can throw.
Sebastian Redl8026f6d2011-03-13 17:09:40 +00002513 if (!CPT->isNothrow(Self.Context) || CPT->getNumArgs() > 1) {
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002514 AllNoThrow = false;
2515 break;
2516 }
2517 }
2518 }
2519
2520 return FoundConstructor && AllNoThrow;
2521 }
2522 return false;
2523 case UTT_HasNothrowConstructor:
2524 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
2525 // If __has_trivial_constructor (type) is true then the trait is
2526 // true, else if type is a cv class or union type (or array
2527 // thereof) with a default constructor that is known not to
2528 // throw an exception then the trait is true, else it is false.
2529 if (T->isPODType())
2530 return true;
2531 if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) {
2532 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
2533 if (RD->hasTrivialConstructor())
2534 return true;
2535
Sebastian Redl751025d2010-09-13 22:02:47 +00002536 DeclContext::lookup_const_iterator Con, ConEnd;
2537 for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD);
2538 Con != ConEnd; ++Con) {
Sebastian Redl08295a52010-09-13 22:18:28 +00002539 // FIXME: In C++0x, a constructor template can be a default constructor.
2540 if (isa<FunctionTemplateDecl>(*Con))
2541 continue;
Sebastian Redl751025d2010-09-13 22:02:47 +00002542 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
2543 if (Constructor->isDefaultConstructor()) {
2544 const FunctionProtoType *CPT
2545 = Constructor->getType()->getAs<FunctionProtoType>();
2546 // TODO: check whether evaluating default arguments can throw.
2547 // For now, we'll be conservative and assume that they can throw.
Sebastian Redl8026f6d2011-03-13 17:09:40 +00002548 return CPT->isNothrow(Self.Context) && CPT->getNumArgs() == 0;
Sebastian Redl751025d2010-09-13 22:02:47 +00002549 }
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002550 }
2551 }
2552 return false;
2553 case UTT_HasVirtualDestructor:
2554 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
2555 // If type is a class type with a virtual destructor ([class.dtor])
2556 // then the trait is true, else it is false.
2557 if (const RecordType *Record = T->getAs<RecordType>()) {
2558 CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
Sebastian Redlf8aca862010-09-14 23:40:14 +00002559 if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD))
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002560 return Destructor->isVirtual();
2561 }
2562 return false;
2563 }
2564}
2565
2566ExprResult Sema::BuildUnaryTypeTrait(UnaryTypeTrait UTT,
Douglas Gregor3d37c0a2010-09-09 16:14:44 +00002567 SourceLocation KWLoc,
2568 TypeSourceInfo *TSInfo,
2569 SourceLocation RParen) {
2570 QualType T = TSInfo->getType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002571
Anders Carlsson3292d5c2009-07-07 19:06:02 +00002572 // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html
2573 // all traits except __is_class, __is_enum and __is_union require a the type
Sebastian Redl607a1782010-09-08 00:48:43 +00002574 // to be complete, an array of unknown bound, or void.
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002575 if (UTT != UTT_IsClass && UTT != UTT_IsEnum && UTT != UTT_IsUnion) {
Sebastian Redl607a1782010-09-08 00:48:43 +00002576 QualType E = T;
2577 if (T->isIncompleteArrayType())
2578 E = Context.getAsArrayType(T)->getElementType();
2579 if (!T->isVoidType() &&
2580 RequireCompleteType(KWLoc, E,
Anders Carlssond497ba72009-08-26 22:59:12 +00002581 diag::err_incomplete_type_used_in_type_trait_expr))
Anders Carlsson3292d5c2009-07-07 19:06:02 +00002582 return ExprError();
2583 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00002584
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002585 bool Value = false;
2586 if (!T->isDependentType())
Sebastian Redlf8aca862010-09-14 23:40:14 +00002587 Value = EvaluateUnaryTypeTrait(*this, UTT, T, KWLoc);
Sebastian Redl0dfd8482010-09-13 20:56:31 +00002588
2589 return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, UTT, TSInfo, Value,
Anders Carlsson3292d5c2009-07-07 19:06:02 +00002590 RParen, Context.BoolTy));
Sebastian Redl64b45f72009-01-05 20:52:13 +00002591}
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002592
Francois Pichet6ad6f282010-12-07 00:08:36 +00002593ExprResult Sema::ActOnBinaryTypeTrait(BinaryTypeTrait BTT,
2594 SourceLocation KWLoc,
2595 ParsedType LhsTy,
2596 ParsedType RhsTy,
2597 SourceLocation RParen) {
2598 TypeSourceInfo *LhsTSInfo;
2599 QualType LhsT = GetTypeFromParser(LhsTy, &LhsTSInfo);
2600 if (!LhsTSInfo)
2601 LhsTSInfo = Context.getTrivialTypeSourceInfo(LhsT);
2602
2603 TypeSourceInfo *RhsTSInfo;
2604 QualType RhsT = GetTypeFromParser(RhsTy, &RhsTSInfo);
2605 if (!RhsTSInfo)
2606 RhsTSInfo = Context.getTrivialTypeSourceInfo(RhsT);
2607
2608 return BuildBinaryTypeTrait(BTT, KWLoc, LhsTSInfo, RhsTSInfo, RParen);
2609}
2610
2611static bool EvaluateBinaryTypeTrait(Sema &Self, BinaryTypeTrait BTT,
2612 QualType LhsT, QualType RhsT,
2613 SourceLocation KeyLoc) {
2614 assert((!LhsT->isDependentType() || RhsT->isDependentType()) &&
2615 "Cannot evaluate traits for dependent types.");
2616
2617 switch(BTT) {
John McCalld89d30f2011-01-28 22:02:36 +00002618 case BTT_IsBaseOf: {
Francois Pichet6ad6f282010-12-07 00:08:36 +00002619 // C++0x [meta.rel]p2
John McCalld89d30f2011-01-28 22:02:36 +00002620 // Base is a base class of Derived without regard to cv-qualifiers or
Francois Pichet6ad6f282010-12-07 00:08:36 +00002621 // Base and Derived are not unions and name the same class type without
2622 // regard to cv-qualifiers.
Francois Pichet6ad6f282010-12-07 00:08:36 +00002623
John McCalld89d30f2011-01-28 22:02:36 +00002624 const RecordType *lhsRecord = LhsT->getAs<RecordType>();
2625 if (!lhsRecord) return false;
2626
2627 const RecordType *rhsRecord = RhsT->getAs<RecordType>();
2628 if (!rhsRecord) return false;
2629
2630 assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT)
2631 == (lhsRecord == rhsRecord));
2632
2633 if (lhsRecord == rhsRecord)
2634 return !lhsRecord->getDecl()->isUnion();
2635
2636 // C++0x [meta.rel]p2:
2637 // If Base and Derived are class types and are different types
2638 // (ignoring possible cv-qualifiers) then Derived shall be a
2639 // complete type.
2640 if (Self.RequireCompleteType(KeyLoc, RhsT,
2641 diag::err_incomplete_type_used_in_type_trait_expr))
2642 return false;
2643
2644 return cast<CXXRecordDecl>(rhsRecord->getDecl())
2645 ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl()));
2646 }
2647
Francois Pichetf1872372010-12-08 22:35:30 +00002648 case BTT_TypeCompatible:
2649 return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(),
2650 RhsT.getUnqualifiedType());
Douglas Gregor9f361132011-01-27 20:28:01 +00002651
2652 case BTT_IsConvertibleTo: {
2653 // C++0x [meta.rel]p4:
2654 // Given the following function prototype:
2655 //
2656 // template <class T>
2657 // typename add_rvalue_reference<T>::type create();
2658 //
2659 // the predicate condition for a template specialization
2660 // is_convertible<From, To> shall be satisfied if and only if
2661 // the return expression in the following code would be
2662 // well-formed, including any implicit conversions to the return
2663 // type of the function:
2664 //
2665 // To test() {
2666 // return create<From>();
2667 // }
2668 //
2669 // Access checking is performed as if in a context unrelated to To and
2670 // From. Only the validity of the immediate context of the expression
2671 // of the return-statement (including conversions to the return type)
2672 // is considered.
2673 //
2674 // We model the initialization as a copy-initialization of a temporary
2675 // of the appropriate type, which for this expression is identical to the
2676 // return statement (since NRVO doesn't apply).
2677 if (LhsT->isObjectType() || LhsT->isFunctionType())
2678 LhsT = Self.Context.getRValueReferenceType(LhsT);
2679
2680 InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT));
Douglas Gregorb608b982011-01-28 02:26:04 +00002681 OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context),
Douglas Gregor9f361132011-01-27 20:28:01 +00002682 Expr::getValueKindForType(LhsT));
2683 Expr *FromPtr = &From;
2684 InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc,
2685 SourceLocation()));
2686
Douglas Gregor1eee5dc2011-01-27 22:31:44 +00002687 // Perform the initialization within a SFINAE trap at translation unit
2688 // scope.
2689 Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true);
2690 Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl());
Douglas Gregor9f361132011-01-27 20:28:01 +00002691 InitializationSequence Init(Self, To, Kind, &FromPtr, 1);
2692 if (Init.getKind() == InitializationSequence::FailedSequence)
2693 return false;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +00002694
Douglas Gregor9f361132011-01-27 20:28:01 +00002695 ExprResult Result = Init.Perform(Self, To, Kind, MultiExprArg(&FromPtr, 1));
2696 return !Result.isInvalid() && !SFINAE.hasErrorOccurred();
2697 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002698 }
2699 llvm_unreachable("Unknown type trait or not implemented");
2700}
2701
2702ExprResult Sema::BuildBinaryTypeTrait(BinaryTypeTrait BTT,
2703 SourceLocation KWLoc,
2704 TypeSourceInfo *LhsTSInfo,
2705 TypeSourceInfo *RhsTSInfo,
2706 SourceLocation RParen) {
2707 QualType LhsT = LhsTSInfo->getType();
2708 QualType RhsT = RhsTSInfo->getType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002709
John McCalld89d30f2011-01-28 22:02:36 +00002710 if (BTT == BTT_TypeCompatible) {
Francois Pichetf1872372010-12-08 22:35:30 +00002711 if (getLangOptions().CPlusPlus) {
2712 Diag(KWLoc, diag::err_types_compatible_p_in_cplusplus)
2713 << SourceRange(KWLoc, RParen);
2714 return ExprError();
2715 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00002716 }
2717
2718 bool Value = false;
2719 if (!LhsT->isDependentType() && !RhsT->isDependentType())
2720 Value = EvaluateBinaryTypeTrait(*this, BTT, LhsT, RhsT, KWLoc);
2721
Francois Pichetf1872372010-12-08 22:35:30 +00002722 // Select trait result type.
2723 QualType ResultType;
2724 switch (BTT) {
Francois Pichetf1872372010-12-08 22:35:30 +00002725 case BTT_IsBaseOf: ResultType = Context.BoolTy; break;
2726 case BTT_TypeCompatible: ResultType = Context.IntTy; break;
Douglas Gregor9f361132011-01-27 20:28:01 +00002727 case BTT_IsConvertibleTo: ResultType = Context.BoolTy; break;
Francois Pichetf1872372010-12-08 22:35:30 +00002728 }
2729
Francois Pichet6ad6f282010-12-07 00:08:36 +00002730 return Owned(new (Context) BinaryTypeTraitExpr(KWLoc, BTT, LhsTSInfo,
2731 RhsTSInfo, Value, RParen,
Francois Pichetf1872372010-12-08 22:35:30 +00002732 ResultType));
Francois Pichet6ad6f282010-12-07 00:08:36 +00002733}
2734
John Wiegley429bb272011-04-08 18:41:53 +00002735QualType Sema::CheckPointerToMemberOperands(ExprResult &lex, ExprResult &rex,
John McCallf89e55a2010-11-18 06:31:45 +00002736 ExprValueKind &VK,
2737 SourceLocation Loc,
2738 bool isIndirect) {
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002739 const char *OpSpelling = isIndirect ? "->*" : ".*";
2740 // C++ 5.5p2
2741 // The binary operator .* [p3: ->*] binds its second operand, which shall
2742 // be of type "pointer to member of T" (where T is a completely-defined
2743 // class type) [...]
John Wiegley429bb272011-04-08 18:41:53 +00002744 QualType RType = rex.get()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00002745 const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>();
Douglas Gregore7450f52009-03-24 19:52:54 +00002746 if (!MemPtr) {
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002747 Diag(Loc, diag::err_bad_memptr_rhs)
John Wiegley429bb272011-04-08 18:41:53 +00002748 << OpSpelling << RType << rex.get()->getSourceRange();
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002749 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00002750 }
Douglas Gregore7450f52009-03-24 19:52:54 +00002751
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002752 QualType Class(MemPtr->getClass(), 0);
2753
Douglas Gregor7d520ba2010-10-13 20:41:14 +00002754 // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the
2755 // member pointer points must be completely-defined. However, there is no
2756 // reason for this semantic distinction, and the rule is not enforced by
2757 // other compilers. Therefore, we do not check this property, as it is
2758 // likely to be considered a defect.
Sebastian Redl59fc2692010-04-10 10:14:54 +00002759
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002760 // C++ 5.5p2
2761 // [...] to its first operand, which shall be of class T or of a class of
2762 // which T is an unambiguous and accessible base class. [p3: a pointer to
2763 // such a class]
John Wiegley429bb272011-04-08 18:41:53 +00002764 QualType LType = lex.get()->getType();
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002765 if (isIndirect) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002766 if (const PointerType *Ptr = LType->getAs<PointerType>())
John McCallf89e55a2010-11-18 06:31:45 +00002767 LType = Ptr->getPointeeType();
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002768 else {
2769 Diag(Loc, diag::err_bad_memptr_lhs)
Fariborz Jahanianef78ac62009-10-26 20:45:27 +00002770 << OpSpelling << 1 << LType
Douglas Gregor849b2432010-03-31 17:46:05 +00002771 << FixItHint::CreateReplacement(SourceRange(Loc), ".*");
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002772 return QualType();
2773 }
2774 }
2775
Douglas Gregora4923eb2009-11-16 21:35:15 +00002776 if (!Context.hasSameUnqualifiedType(Class, LType)) {
Sebastian Redl17e1d352010-04-23 17:18:26 +00002777 // If we want to check the hierarchy, we need a complete type.
2778 if (RequireCompleteType(Loc, LType, PDiag(diag::err_bad_memptr_lhs)
2779 << OpSpelling << (int)isIndirect)) {
2780 return QualType();
2781 }
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00002782 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Douglas Gregora8f32e02009-10-06 17:59:45 +00002783 /*DetectVirtual=*/false);
Mike Stump390b4cc2009-05-16 07:39:55 +00002784 // FIXME: Would it be useful to print full ambiguity paths, or is that
2785 // overkill?
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002786 if (!IsDerivedFrom(LType, Class, Paths) ||
2787 Paths.isAmbiguous(Context.getCanonicalType(Class))) {
2788 Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling
John Wiegley429bb272011-04-08 18:41:53 +00002789 << (int)isIndirect << lex.get()->getType();
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002790 return QualType();
2791 }
Eli Friedman3005efe2010-01-16 00:00:48 +00002792 // Cast LHS to type of use.
2793 QualType UseType = isIndirect ? Context.getPointerType(Class) : Class;
John McCall5baba9d2010-08-25 10:28:54 +00002794 ExprValueKind VK =
John Wiegley429bb272011-04-08 18:41:53 +00002795 isIndirect ? VK_RValue : CastCategory(lex.get());
Sebastian Redl906082e2010-07-20 04:20:21 +00002796
John McCallf871d0c2010-08-07 06:22:56 +00002797 CXXCastPath BasePath;
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00002798 BuildBasePathArray(Paths, BasePath);
John Wiegley429bb272011-04-08 18:41:53 +00002799 lex = ImpCastExprToType(lex.take(), UseType, CK_DerivedToBase, VK, &BasePath);
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002800 }
2801
John Wiegley429bb272011-04-08 18:41:53 +00002802 if (isa<CXXScalarValueInitExpr>(rex.get()->IgnoreParens())) {
Fariborz Jahanian05ebda92009-11-18 21:54:48 +00002803 // Diagnose use of pointer-to-member type which when used as
2804 // the functional cast in a pointer-to-member expression.
2805 Diag(Loc, diag::err_pointer_to_member_type) << isIndirect;
2806 return QualType();
2807 }
John McCallf89e55a2010-11-18 06:31:45 +00002808
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002809 // C++ 5.5p2
2810 // The result is an object or a function of the type specified by the
2811 // second operand.
2812 // The cv qualifiers are the union of those in the pointer and the left side,
2813 // in accordance with 5.5p5 and 5.2.5.
2814 // FIXME: This returns a dereferenced member function pointer as a normal
2815 // function type. However, the only operation valid on such functions is
Mike Stump390b4cc2009-05-16 07:39:55 +00002816 // calling them. There's also a GCC extension to get a function pointer to the
2817 // thing, which is another complication, because this type - unlike the type
2818 // that is the result of this expression - takes the class as the first
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002819 // argument.
2820 // We probably need a "MemberFunctionClosureType" or something like that.
2821 QualType Result = MemPtr->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002822 Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers());
John McCallf89e55a2010-11-18 06:31:45 +00002823
Douglas Gregor6b4df912011-01-26 16:40:18 +00002824 // C++0x [expr.mptr.oper]p6:
2825 // In a .* expression whose object expression is an rvalue, the program is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002826 // ill-formed if the second operand is a pointer to member function with
2827 // ref-qualifier &. In a ->* expression or in a .* expression whose object
2828 // expression is an lvalue, the program is ill-formed if the second operand
Douglas Gregor6b4df912011-01-26 16:40:18 +00002829 // is a pointer to member function with ref-qualifier &&.
2830 if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) {
2831 switch (Proto->getRefQualifier()) {
2832 case RQ_None:
2833 // Do nothing
2834 break;
2835
2836 case RQ_LValue:
John Wiegley429bb272011-04-08 18:41:53 +00002837 if (!isIndirect && !lex.get()->Classify(Context).isLValue())
Douglas Gregor6b4df912011-01-26 16:40:18 +00002838 Diag(Loc, diag::err_pointer_to_member_oper_value_classify)
John Wiegley429bb272011-04-08 18:41:53 +00002839 << RType << 1 << lex.get()->getSourceRange();
Douglas Gregor6b4df912011-01-26 16:40:18 +00002840 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002841
Douglas Gregor6b4df912011-01-26 16:40:18 +00002842 case RQ_RValue:
John Wiegley429bb272011-04-08 18:41:53 +00002843 if (isIndirect || !lex.get()->Classify(Context).isRValue())
Douglas Gregor6b4df912011-01-26 16:40:18 +00002844 Diag(Loc, diag::err_pointer_to_member_oper_value_classify)
John Wiegley429bb272011-04-08 18:41:53 +00002845 << RType << 0 << lex.get()->getSourceRange();
Douglas Gregor6b4df912011-01-26 16:40:18 +00002846 break;
2847 }
2848 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002849
John McCallf89e55a2010-11-18 06:31:45 +00002850 // C++ [expr.mptr.oper]p6:
2851 // The result of a .* expression whose second operand is a pointer
2852 // to a data member is of the same value category as its
2853 // first operand. The result of a .* expression whose second
2854 // operand is a pointer to a member function is a prvalue. The
2855 // result of an ->* expression is an lvalue if its second operand
2856 // is a pointer to data member and a prvalue otherwise.
2857 if (Result->isFunctionType())
2858 VK = VK_RValue;
2859 else if (isIndirect)
2860 VK = VK_LValue;
2861 else
John Wiegley429bb272011-04-08 18:41:53 +00002862 VK = lex.get()->getValueKind();
John McCallf89e55a2010-11-18 06:31:45 +00002863
Sebastian Redl7c8bd602009-02-07 20:10:22 +00002864 return Result;
2865}
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002866
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002867/// \brief Try to convert a type to another according to C++0x 5.16p3.
2868///
2869/// This is part of the parameter validation for the ? operator. If either
2870/// value operand is a class type, the two operands are attempted to be
2871/// converted to each other. This function does the conversion in one direction.
Douglas Gregorb70cf442010-03-26 20:14:36 +00002872/// It returns true if the program is ill-formed and has already been diagnosed
2873/// as such.
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002874static bool TryClassUnification(Sema &Self, Expr *From, Expr *To,
2875 SourceLocation QuestionLoc,
Douglas Gregorb70cf442010-03-26 20:14:36 +00002876 bool &HaveConversion,
2877 QualType &ToType) {
2878 HaveConversion = false;
2879 ToType = To->getType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002880
2881 InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(),
Douglas Gregorb70cf442010-03-26 20:14:36 +00002882 SourceLocation());
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002883 // C++0x 5.16p3
2884 // The process for determining whether an operand expression E1 of type T1
2885 // can be converted to match an operand expression E2 of type T2 is defined
2886 // as follows:
2887 // -- If E2 is an lvalue:
John McCall7eb0a9e2010-11-24 05:12:34 +00002888 bool ToIsLvalue = To->isLValue();
Douglas Gregor0fd8ff72010-03-26 20:59:55 +00002889 if (ToIsLvalue) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002890 // E1 can be converted to match E2 if E1 can be implicitly converted to
2891 // type "lvalue reference to T2", subject to the constraint that in the
2892 // conversion the reference must bind directly to E1.
Douglas Gregorb70cf442010-03-26 20:14:36 +00002893 QualType T = Self.Context.getLValueReferenceType(ToType);
2894 InitializedEntity Entity = InitializedEntity::InitializeTemporary(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002895
Douglas Gregorb70cf442010-03-26 20:14:36 +00002896 InitializationSequence InitSeq(Self, Entity, Kind, &From, 1);
2897 if (InitSeq.isDirectReferenceBinding()) {
2898 ToType = T;
2899 HaveConversion = true;
2900 return false;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002901 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002902
Douglas Gregorb70cf442010-03-26 20:14:36 +00002903 if (InitSeq.isAmbiguous())
2904 return InitSeq.Diagnose(Self, Entity, Kind, &From, 1);
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002905 }
John McCallb1bdc622010-02-25 01:37:24 +00002906
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002907 // -- If E2 is an rvalue, or if the conversion above cannot be done:
2908 // -- if E1 and E2 have class type, and the underlying class types are
2909 // the same or one is a base class of the other:
2910 QualType FTy = From->getType();
2911 QualType TTy = To->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00002912 const RecordType *FRec = FTy->getAs<RecordType>();
2913 const RecordType *TRec = TTy->getAs<RecordType>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002914 bool FDerivedFromT = FRec && TRec && FRec != TRec &&
Douglas Gregorb70cf442010-03-26 20:14:36 +00002915 Self.IsDerivedFrom(FTy, TTy);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002916 if (FRec && TRec &&
Douglas Gregorb70cf442010-03-26 20:14:36 +00002917 (FRec == TRec || FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002918 // E1 can be converted to match E2 if the class of T2 is the
2919 // same type as, or a base class of, the class of T1, and
2920 // [cv2 > cv1].
John McCallb1bdc622010-02-25 01:37:24 +00002921 if (FRec == TRec || FDerivedFromT) {
2922 if (TTy.isAtLeastAsQualifiedAs(FTy)) {
Douglas Gregorb70cf442010-03-26 20:14:36 +00002923 InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy);
2924 InitializationSequence InitSeq(Self, Entity, Kind, &From, 1);
2925 if (InitSeq.getKind() != InitializationSequence::FailedSequence) {
2926 HaveConversion = true;
2927 return false;
2928 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002929
Douglas Gregorb70cf442010-03-26 20:14:36 +00002930 if (InitSeq.isAmbiguous())
2931 return InitSeq.Diagnose(Self, Entity, Kind, &From, 1);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002932 }
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002933 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002934
Douglas Gregorb70cf442010-03-26 20:14:36 +00002935 return false;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002936 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002937
Douglas Gregorb70cf442010-03-26 20:14:36 +00002938 // -- Otherwise: E1 can be converted to match E2 if E1 can be
2939 // implicitly converted to the type that expression E2 would have
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002940 // if E2 were converted to an rvalue (or the type it has, if E2 is
Douglas Gregor0fd8ff72010-03-26 20:59:55 +00002941 // an rvalue).
2942 //
2943 // This actually refers very narrowly to the lvalue-to-rvalue conversion, not
2944 // to the array-to-pointer or function-to-pointer conversions.
2945 if (!TTy->getAs<TagType>())
2946 TTy = TTy.getUnqualifiedType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002947
Douglas Gregorb70cf442010-03-26 20:14:36 +00002948 InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy);
2949 InitializationSequence InitSeq(Self, Entity, Kind, &From, 1);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002950 HaveConversion = InitSeq.getKind() != InitializationSequence::FailedSequence;
Douglas Gregorb70cf442010-03-26 20:14:36 +00002951 ToType = TTy;
2952 if (InitSeq.isAmbiguous())
2953 return InitSeq.Diagnose(Self, Entity, Kind, &From, 1);
2954
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002955 return false;
2956}
2957
2958/// \brief Try to find a common type for two according to C++0x 5.16p5.
2959///
2960/// This is part of the parameter validation for the ? operator. If either
2961/// value operand is a class type, overload resolution is used to find a
2962/// conversion to a common type.
John Wiegley429bb272011-04-08 18:41:53 +00002963static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS,
Chandler Carruth82214a82011-02-18 23:54:50 +00002964 SourceLocation QuestionLoc) {
John Wiegley429bb272011-04-08 18:41:53 +00002965 Expr *Args[2] = { LHS.get(), RHS.get() };
Chandler Carruth82214a82011-02-18 23:54:50 +00002966 OverloadCandidateSet CandidateSet(QuestionLoc);
2967 Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, 2,
2968 CandidateSet);
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002969
2970 OverloadCandidateSet::iterator Best;
Chandler Carruth82214a82011-02-18 23:54:50 +00002971 switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) {
John Wiegley429bb272011-04-08 18:41:53 +00002972 case OR_Success: {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002973 // We found a match. Perform the conversions on the arguments and move on.
John Wiegley429bb272011-04-08 18:41:53 +00002974 ExprResult LHSRes =
2975 Self.PerformImplicitConversion(LHS.get(), Best->BuiltinTypes.ParamTypes[0],
2976 Best->Conversions[0], Sema::AA_Converting);
2977 if (LHSRes.isInvalid())
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002978 break;
John Wiegley429bb272011-04-08 18:41:53 +00002979 LHS = move(LHSRes);
2980
2981 ExprResult RHSRes =
2982 Self.PerformImplicitConversion(RHS.get(), Best->BuiltinTypes.ParamTypes[1],
2983 Best->Conversions[1], Sema::AA_Converting);
2984 if (RHSRes.isInvalid())
2985 break;
2986 RHS = move(RHSRes);
Chandler Carruth25ca4212011-02-25 19:41:05 +00002987 if (Best->Function)
2988 Self.MarkDeclarationReferenced(QuestionLoc, Best->Function);
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002989 return false;
John Wiegley429bb272011-04-08 18:41:53 +00002990 }
2991
Douglas Gregor20093b42009-12-09 23:02:17 +00002992 case OR_No_Viable_Function:
Chandler Carruth82214a82011-02-18 23:54:50 +00002993
2994 // Emit a better diagnostic if one of the expressions is a null pointer
2995 // constant and the other is a pointer type. In this case, the user most
2996 // likely forgot to take the address of the other expression.
John Wiegley429bb272011-04-08 18:41:53 +00002997 if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc))
Chandler Carruth82214a82011-02-18 23:54:50 +00002998 return true;
2999
3000 Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands)
John Wiegley429bb272011-04-08 18:41:53 +00003001 << LHS.get()->getType() << RHS.get()->getType()
3002 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003003 return true;
3004
Douglas Gregor20093b42009-12-09 23:02:17 +00003005 case OR_Ambiguous:
Chandler Carruth82214a82011-02-18 23:54:50 +00003006 Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl)
John Wiegley429bb272011-04-08 18:41:53 +00003007 << LHS.get()->getType() << RHS.get()->getType()
3008 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
Mike Stump390b4cc2009-05-16 07:39:55 +00003009 // FIXME: Print the possible common types by printing the return types of
3010 // the viable candidates.
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003011 break;
3012
Douglas Gregor20093b42009-12-09 23:02:17 +00003013 case OR_Deleted:
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003014 assert(false && "Conditional operator has only built-in overloads");
3015 break;
3016 }
3017 return true;
3018}
3019
Sebastian Redl76458502009-04-17 16:30:52 +00003020/// \brief Perform an "extended" implicit conversion as returned by
3021/// TryClassUnification.
John Wiegley429bb272011-04-08 18:41:53 +00003022static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) {
Douglas Gregorb70cf442010-03-26 20:14:36 +00003023 InitializedEntity Entity = InitializedEntity::InitializeTemporary(T);
John Wiegley429bb272011-04-08 18:41:53 +00003024 InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(),
Douglas Gregorb70cf442010-03-26 20:14:36 +00003025 SourceLocation());
John Wiegley429bb272011-04-08 18:41:53 +00003026 Expr *Arg = E.take();
3027 InitializationSequence InitSeq(Self, Entity, Kind, &Arg, 1);
3028 ExprResult Result = InitSeq.Perform(Self, Entity, Kind, MultiExprArg(&Arg, 1));
Douglas Gregorb70cf442010-03-26 20:14:36 +00003029 if (Result.isInvalid())
Sebastian Redl76458502009-04-17 16:30:52 +00003030 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003031
John Wiegley429bb272011-04-08 18:41:53 +00003032 E = Result;
Sebastian Redl76458502009-04-17 16:30:52 +00003033 return false;
3034}
3035
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003036/// \brief Check the operands of ?: under C++ semantics.
3037///
3038/// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y
3039/// extension. In this case, LHS == Cond. (But they're not aliases.)
John Wiegley429bb272011-04-08 18:41:53 +00003040QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS,
John McCall56ca35d2011-02-17 10:25:35 +00003041 ExprValueKind &VK, ExprObjectKind &OK,
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003042 SourceLocation QuestionLoc) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003043 // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++
3044 // interface pointers.
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003045
3046 // C++0x 5.16p1
3047 // The first expression is contextually converted to bool.
John Wiegley429bb272011-04-08 18:41:53 +00003048 if (!Cond.get()->isTypeDependent()) {
3049 ExprResult CondRes = CheckCXXBooleanCondition(Cond.take());
3050 if (CondRes.isInvalid())
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003051 return QualType();
John Wiegley429bb272011-04-08 18:41:53 +00003052 Cond = move(CondRes);
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003053 }
3054
John McCallf89e55a2010-11-18 06:31:45 +00003055 // Assume r-value.
3056 VK = VK_RValue;
John McCall09431682010-11-18 19:01:18 +00003057 OK = OK_Ordinary;
John McCallf89e55a2010-11-18 06:31:45 +00003058
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003059 // Either of the arguments dependent?
John Wiegley429bb272011-04-08 18:41:53 +00003060 if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent())
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003061 return Context.DependentTy;
3062
3063 // C++0x 5.16p2
3064 // If either the second or the third operand has type (cv) void, ...
John Wiegley429bb272011-04-08 18:41:53 +00003065 QualType LTy = LHS.get()->getType();
3066 QualType RTy = RHS.get()->getType();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003067 bool LVoid = LTy->isVoidType();
3068 bool RVoid = RTy->isVoidType();
3069 if (LVoid || RVoid) {
3070 // ... then the [l2r] conversions are performed on the second and third
3071 // operands ...
John Wiegley429bb272011-04-08 18:41:53 +00003072 LHS = DefaultFunctionArrayLvalueConversion(LHS.take());
3073 RHS = DefaultFunctionArrayLvalueConversion(RHS.take());
3074 if (LHS.isInvalid() || RHS.isInvalid())
3075 return QualType();
3076 LTy = LHS.get()->getType();
3077 RTy = RHS.get()->getType();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003078
3079 // ... and one of the following shall hold:
3080 // -- The second or the third operand (but not both) is a throw-
3081 // expression; the result is of the type of the other and is an rvalue.
John Wiegley429bb272011-04-08 18:41:53 +00003082 bool LThrow = isa<CXXThrowExpr>(LHS.get());
3083 bool RThrow = isa<CXXThrowExpr>(RHS.get());
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003084 if (LThrow && !RThrow)
3085 return RTy;
3086 if (RThrow && !LThrow)
3087 return LTy;
3088
3089 // -- Both the second and third operands have type void; the result is of
3090 // type void and is an rvalue.
3091 if (LVoid && RVoid)
3092 return Context.VoidTy;
3093
3094 // Neither holds, error.
3095 Diag(QuestionLoc, diag::err_conditional_void_nonvoid)
3096 << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1)
John Wiegley429bb272011-04-08 18:41:53 +00003097 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003098 return QualType();
3099 }
3100
3101 // Neither is void.
3102
3103 // C++0x 5.16p3
3104 // Otherwise, if the second and third operand have different types, and
3105 // either has (cv) class type, and attempt is made to convert each of those
3106 // operands to the other.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003107 if (!Context.hasSameType(LTy, RTy) &&
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003108 (LTy->isRecordType() || RTy->isRecordType())) {
3109 ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft;
3110 // These return true if a single direction is already ambiguous.
Douglas Gregorb70cf442010-03-26 20:14:36 +00003111 QualType L2RType, R2LType;
3112 bool HaveL2R, HaveR2L;
John Wiegley429bb272011-04-08 18:41:53 +00003113 if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType))
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003114 return QualType();
John Wiegley429bb272011-04-08 18:41:53 +00003115 if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType))
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003116 return QualType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003117
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003118 // If both can be converted, [...] the program is ill-formed.
3119 if (HaveL2R && HaveR2L) {
3120 Diag(QuestionLoc, diag::err_conditional_ambiguous)
John Wiegley429bb272011-04-08 18:41:53 +00003121 << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003122 return QualType();
3123 }
3124
3125 // If exactly one conversion is possible, that conversion is applied to
3126 // the chosen operand and the converted operands are used in place of the
3127 // original operands for the remainder of this section.
3128 if (HaveL2R) {
John Wiegley429bb272011-04-08 18:41:53 +00003129 if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid())
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003130 return QualType();
John Wiegley429bb272011-04-08 18:41:53 +00003131 LTy = LHS.get()->getType();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003132 } else if (HaveR2L) {
John Wiegley429bb272011-04-08 18:41:53 +00003133 if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid())
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003134 return QualType();
John Wiegley429bb272011-04-08 18:41:53 +00003135 RTy = RHS.get()->getType();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003136 }
3137 }
3138
3139 // C++0x 5.16p4
John McCallf89e55a2010-11-18 06:31:45 +00003140 // If the second and third operands are glvalues of the same value
3141 // category and have the same type, the result is of that type and
3142 // value category and it is a bit-field if the second or the third
3143 // operand is a bit-field, or if both are bit-fields.
John McCall09431682010-11-18 19:01:18 +00003144 // We only extend this to bitfields, not to the crazy other kinds of
3145 // l-values.
Douglas Gregor1927b1f2010-04-01 22:47:07 +00003146 bool Same = Context.hasSameType(LTy, RTy);
John McCallf89e55a2010-11-18 06:31:45 +00003147 if (Same &&
John Wiegley429bb272011-04-08 18:41:53 +00003148 LHS.get()->isGLValue() &&
3149 LHS.get()->getValueKind() == RHS.get()->getValueKind() &&
3150 LHS.get()->isOrdinaryOrBitFieldObject() &&
3151 RHS.get()->isOrdinaryOrBitFieldObject()) {
3152 VK = LHS.get()->getValueKind();
3153 if (LHS.get()->getObjectKind() == OK_BitField ||
3154 RHS.get()->getObjectKind() == OK_BitField)
John McCall09431682010-11-18 19:01:18 +00003155 OK = OK_BitField;
John McCallf89e55a2010-11-18 06:31:45 +00003156 return LTy;
Fariborz Jahanian3911a1a2010-09-25 01:08:05 +00003157 }
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003158
3159 // C++0x 5.16p5
3160 // Otherwise, the result is an rvalue. If the second and third operands
3161 // do not have the same type, and either has (cv) class type, ...
3162 if (!Same && (LTy->isRecordType() || RTy->isRecordType())) {
3163 // ... overload resolution is used to determine the conversions (if any)
3164 // to be applied to the operands. If the overload resolution fails, the
3165 // program is ill-formed.
3166 if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc))
3167 return QualType();
3168 }
3169
3170 // C++0x 5.16p6
3171 // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard
3172 // conversions are performed on the second and third operands.
John Wiegley429bb272011-04-08 18:41:53 +00003173 LHS = DefaultFunctionArrayLvalueConversion(LHS.take());
3174 RHS = DefaultFunctionArrayLvalueConversion(RHS.take());
3175 if (LHS.isInvalid() || RHS.isInvalid())
3176 return QualType();
3177 LTy = LHS.get()->getType();
3178 RTy = RHS.get()->getType();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003179
3180 // After those conversions, one of the following shall hold:
3181 // -- The second and third operands have the same type; the result
Douglas Gregorb65a4582010-05-19 23:40:50 +00003182 // is of that type. If the operands have class type, the result
3183 // is a prvalue temporary of the result type, which is
3184 // copy-initialized from either the second operand or the third
3185 // operand depending on the value of the first operand.
3186 if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) {
3187 if (LTy->isRecordType()) {
3188 // The operands have class type. Make a temporary copy.
3189 InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003190 ExprResult LHSCopy = PerformCopyInitialization(Entity,
3191 SourceLocation(),
John Wiegley429bb272011-04-08 18:41:53 +00003192 LHS);
Douglas Gregorb65a4582010-05-19 23:40:50 +00003193 if (LHSCopy.isInvalid())
3194 return QualType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003195
3196 ExprResult RHSCopy = PerformCopyInitialization(Entity,
3197 SourceLocation(),
John Wiegley429bb272011-04-08 18:41:53 +00003198 RHS);
Douglas Gregorb65a4582010-05-19 23:40:50 +00003199 if (RHSCopy.isInvalid())
3200 return QualType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003201
John Wiegley429bb272011-04-08 18:41:53 +00003202 LHS = LHSCopy;
3203 RHS = RHSCopy;
Douglas Gregorb65a4582010-05-19 23:40:50 +00003204 }
3205
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003206 return LTy;
Douglas Gregorb65a4582010-05-19 23:40:50 +00003207 }
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003208
Douglas Gregorfb4a5432010-05-18 22:42:18 +00003209 // Extension: conditional operator involving vector types.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003210 if (LTy->isVectorType() || RTy->isVectorType())
Douglas Gregorfb4a5432010-05-18 22:42:18 +00003211 return CheckVectorOperands(QuestionLoc, LHS, RHS);
3212
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003213 // -- The second and third operands have arithmetic or enumeration type;
3214 // the usual arithmetic conversions are performed to bring them to a
3215 // common type, and the result is of that type.
3216 if (LTy->isArithmeticType() && RTy->isArithmeticType()) {
3217 UsualArithmeticConversions(LHS, RHS);
John Wiegley429bb272011-04-08 18:41:53 +00003218 if (LHS.isInvalid() || RHS.isInvalid())
3219 return QualType();
3220 return LHS.get()->getType();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003221 }
3222
3223 // -- The second and third operands have pointer type, or one has pointer
3224 // type and the other is a null pointer constant; pointer conversions
3225 // and qualification conversions are performed to bring them to their
3226 // composite pointer type. The result is of the composite pointer type.
Eli Friedmande8ac492010-01-02 22:56:07 +00003227 // -- The second and third operands have pointer to member type, or one has
3228 // pointer to member type and the other is a null pointer constant;
3229 // pointer to member conversions and qualification conversions are
3230 // performed to bring them to a common type, whose cv-qualification
3231 // shall match the cv-qualification of either the second or the third
3232 // operand. The result is of the common type.
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003233 bool NonStandardCompositeType = false;
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003234 QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS,
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003235 isSFINAEContext()? 0 : &NonStandardCompositeType);
3236 if (!Composite.isNull()) {
3237 if (NonStandardCompositeType)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003238 Diag(QuestionLoc,
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003239 diag::ext_typecheck_cond_incompatible_operands_nonstandard)
3240 << LTy << RTy << Composite
John Wiegley429bb272011-04-08 18:41:53 +00003241 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003242
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003243 return Composite;
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003244 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003245
Douglas Gregor1927b1f2010-04-01 22:47:07 +00003246 // Similarly, attempt to find composite type of two objective-c pointers.
Fariborz Jahanian55016362009-12-10 20:46:08 +00003247 Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc);
3248 if (!Composite.isNull())
3249 return Composite;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003250
Chandler Carruth7ef93242011-02-19 00:13:59 +00003251 // Check if we are using a null with a non-pointer type.
John Wiegley429bb272011-04-08 18:41:53 +00003252 if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc))
Chandler Carruth7ef93242011-02-19 00:13:59 +00003253 return QualType();
3254
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003255 Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands)
John Wiegley429bb272011-04-08 18:41:53 +00003256 << LHS.get()->getType() << RHS.get()->getType()
3257 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00003258 return QualType();
3259}
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003260
3261/// \brief Find a merged pointer type and convert the two expressions to it.
3262///
Douglas Gregor20b3e992009-08-24 17:42:35 +00003263/// This finds the composite pointer type (or member pointer type) for @p E1
3264/// and @p E2 according to C++0x 5.9p2. It converts both expressions to this
3265/// type and returns it.
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003266/// It does not emit diagnostics.
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003267///
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003268/// \param Loc The location of the operator requiring these two expressions to
3269/// be converted to the composite pointer type.
3270///
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003271/// If \p NonStandardCompositeType is non-NULL, then we are permitted to find
3272/// a non-standard (but still sane) composite type to which both expressions
3273/// can be converted. When such a type is chosen, \c *NonStandardCompositeType
3274/// will be set true.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003275QualType Sema::FindCompositePointerType(SourceLocation Loc,
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003276 Expr *&E1, Expr *&E2,
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003277 bool *NonStandardCompositeType) {
3278 if (NonStandardCompositeType)
3279 *NonStandardCompositeType = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003280
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003281 assert(getLangOptions().CPlusPlus && "This function assumes C++");
3282 QualType T1 = E1->getType(), T2 = E2->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00003283
Fariborz Jahanian0cedfbd2009-12-08 20:04:24 +00003284 if (!T1->isAnyPointerType() && !T1->isMemberPointerType() &&
3285 !T2->isAnyPointerType() && !T2->isMemberPointerType())
Douglas Gregor20b3e992009-08-24 17:42:35 +00003286 return QualType();
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003287
3288 // C++0x 5.9p2
3289 // Pointer conversions and qualification conversions are performed on
3290 // pointer operands to bring them to their composite pointer type. If
3291 // one operand is a null pointer constant, the composite pointer type is
3292 // the type of the other operand.
Douglas Gregorce940492009-09-25 04:25:58 +00003293 if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
Eli Friedman73c39ab2009-10-20 08:27:19 +00003294 if (T2->isMemberPointerType())
John Wiegley429bb272011-04-08 18:41:53 +00003295 E1 = ImpCastExprToType(E1, T2, CK_NullToMemberPointer).take();
Eli Friedman73c39ab2009-10-20 08:27:19 +00003296 else
John Wiegley429bb272011-04-08 18:41:53 +00003297 E1 = ImpCastExprToType(E1, T2, CK_NullToPointer).take();
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003298 return T2;
3299 }
Douglas Gregorce940492009-09-25 04:25:58 +00003300 if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
Eli Friedman73c39ab2009-10-20 08:27:19 +00003301 if (T1->isMemberPointerType())
John Wiegley429bb272011-04-08 18:41:53 +00003302 E2 = ImpCastExprToType(E2, T1, CK_NullToMemberPointer).take();
Eli Friedman73c39ab2009-10-20 08:27:19 +00003303 else
John Wiegley429bb272011-04-08 18:41:53 +00003304 E2 = ImpCastExprToType(E2, T1, CK_NullToPointer).take();
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003305 return T1;
3306 }
Mike Stump1eb44332009-09-09 15:08:12 +00003307
Douglas Gregor20b3e992009-08-24 17:42:35 +00003308 // Now both have to be pointers or member pointers.
Sebastian Redla439e6f2009-11-16 21:03:45 +00003309 if ((!T1->isPointerType() && !T1->isMemberPointerType()) ||
3310 (!T2->isPointerType() && !T2->isMemberPointerType()))
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003311 return QualType();
3312
3313 // Otherwise, of one of the operands has type "pointer to cv1 void," then
3314 // the other has type "pointer to cv2 T" and the composite pointer type is
3315 // "pointer to cv12 void," where cv12 is the union of cv1 and cv2.
3316 // Otherwise, the composite pointer type is a pointer type similar to the
3317 // type of one of the operands, with a cv-qualification signature that is
3318 // the union of the cv-qualification signatures of the operand types.
3319 // In practice, the first part here is redundant; it's subsumed by the second.
3320 // What we do here is, we build the two possible composite types, and try the
3321 // conversions in both directions. If only one works, or if the two composite
3322 // types are the same, we have succeeded.
John McCall0953e762009-09-24 19:53:00 +00003323 // FIXME: extended qualifiers?
Sebastian Redla439e6f2009-11-16 21:03:45 +00003324 typedef llvm::SmallVector<unsigned, 4> QualifierVector;
3325 QualifierVector QualifierUnion;
3326 typedef llvm::SmallVector<std::pair<const Type *, const Type *>, 4>
3327 ContainingClassVector;
3328 ContainingClassVector MemberOfClass;
3329 QualType Composite1 = Context.getCanonicalType(T1),
3330 Composite2 = Context.getCanonicalType(T2);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003331 unsigned NeedConstBefore = 0;
Douglas Gregor20b3e992009-08-24 17:42:35 +00003332 do {
3333 const PointerType *Ptr1, *Ptr2;
3334 if ((Ptr1 = Composite1->getAs<PointerType>()) &&
3335 (Ptr2 = Composite2->getAs<PointerType>())) {
3336 Composite1 = Ptr1->getPointeeType();
3337 Composite2 = Ptr2->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003338
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003339 // If we're allowed to create a non-standard composite type, keep track
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003340 // of where we need to fill in additional 'const' qualifiers.
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003341 if (NonStandardCompositeType &&
3342 Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers())
3343 NeedConstBefore = QualifierUnion.size();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003344
Douglas Gregor20b3e992009-08-24 17:42:35 +00003345 QualifierUnion.push_back(
3346 Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers());
3347 MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0));
3348 continue;
3349 }
Mike Stump1eb44332009-09-09 15:08:12 +00003350
Douglas Gregor20b3e992009-08-24 17:42:35 +00003351 const MemberPointerType *MemPtr1, *MemPtr2;
3352 if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) &&
3353 (MemPtr2 = Composite2->getAs<MemberPointerType>())) {
3354 Composite1 = MemPtr1->getPointeeType();
3355 Composite2 = MemPtr2->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003356
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003357 // If we're allowed to create a non-standard composite type, keep track
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003358 // of where we need to fill in additional 'const' qualifiers.
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003359 if (NonStandardCompositeType &&
3360 Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers())
3361 NeedConstBefore = QualifierUnion.size();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003362
Douglas Gregor20b3e992009-08-24 17:42:35 +00003363 QualifierUnion.push_back(
3364 Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers());
3365 MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(),
3366 MemPtr2->getClass()));
3367 continue;
3368 }
Mike Stump1eb44332009-09-09 15:08:12 +00003369
Douglas Gregor20b3e992009-08-24 17:42:35 +00003370 // FIXME: block pointer types?
Mike Stump1eb44332009-09-09 15:08:12 +00003371
Douglas Gregor20b3e992009-08-24 17:42:35 +00003372 // Cannot unwrap any more types.
3373 break;
3374 } while (true);
Mike Stump1eb44332009-09-09 15:08:12 +00003375
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003376 if (NeedConstBefore && NonStandardCompositeType) {
3377 // Extension: Add 'const' to qualifiers that come before the first qualifier
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003378 // mismatch, so that our (non-standard!) composite type meets the
Douglas Gregorb2cb1cb2010-02-25 22:29:57 +00003379 // requirements of C++ [conv.qual]p4 bullet 3.
3380 for (unsigned I = 0; I != NeedConstBefore; ++I) {
3381 if ((QualifierUnion[I] & Qualifiers::Const) == 0) {
3382 QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const;
3383 *NonStandardCompositeType = true;
3384 }
3385 }
3386 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003387
Douglas Gregor20b3e992009-08-24 17:42:35 +00003388 // Rewrap the composites as pointers or member pointers with the union CVRs.
Sebastian Redla439e6f2009-11-16 21:03:45 +00003389 ContainingClassVector::reverse_iterator MOC
3390 = MemberOfClass.rbegin();
3391 for (QualifierVector::reverse_iterator
3392 I = QualifierUnion.rbegin(),
3393 E = QualifierUnion.rend();
Douglas Gregor20b3e992009-08-24 17:42:35 +00003394 I != E; (void)++I, ++MOC) {
John McCall0953e762009-09-24 19:53:00 +00003395 Qualifiers Quals = Qualifiers::fromCVRMask(*I);
Douglas Gregor20b3e992009-08-24 17:42:35 +00003396 if (MOC->first && MOC->second) {
3397 // Rebuild member pointer type
John McCall0953e762009-09-24 19:53:00 +00003398 Composite1 = Context.getMemberPointerType(
3399 Context.getQualifiedType(Composite1, Quals),
3400 MOC->first);
3401 Composite2 = Context.getMemberPointerType(
3402 Context.getQualifiedType(Composite2, Quals),
3403 MOC->second);
Douglas Gregor20b3e992009-08-24 17:42:35 +00003404 } else {
3405 // Rebuild pointer type
John McCall0953e762009-09-24 19:53:00 +00003406 Composite1
3407 = Context.getPointerType(Context.getQualifiedType(Composite1, Quals));
3408 Composite2
3409 = Context.getPointerType(Context.getQualifiedType(Composite2, Quals));
Douglas Gregor20b3e992009-08-24 17:42:35 +00003410 }
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003411 }
3412
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003413 // Try to convert to the first composite pointer type.
3414 InitializedEntity Entity1
3415 = InitializedEntity::InitializeTemporary(Composite1);
3416 InitializationKind Kind
3417 = InitializationKind::CreateCopy(Loc, SourceLocation());
3418 InitializationSequence E1ToC1(*this, Entity1, Kind, &E1, 1);
3419 InitializationSequence E2ToC1(*this, Entity1, Kind, &E2, 1);
Mike Stump1eb44332009-09-09 15:08:12 +00003420
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003421 if (E1ToC1 && E2ToC1) {
3422 // Conversion to Composite1 is viable.
3423 if (!Context.hasSameType(Composite1, Composite2)) {
3424 // Composite2 is a different type from Composite1. Check whether
3425 // Composite2 is also viable.
3426 InitializedEntity Entity2
3427 = InitializedEntity::InitializeTemporary(Composite2);
3428 InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1);
3429 InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1);
3430 if (E1ToC2 && E2ToC2) {
3431 // Both Composite1 and Composite2 are viable and are different;
3432 // this is an ambiguity.
3433 return QualType();
3434 }
3435 }
3436
3437 // Convert E1 to Composite1
John McCall60d7b3a2010-08-24 06:29:42 +00003438 ExprResult E1Result
John McCallca0408f2010-08-23 06:44:23 +00003439 = E1ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E1,1));
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003440 if (E1Result.isInvalid())
3441 return QualType();
3442 E1 = E1Result.takeAs<Expr>();
3443
3444 // Convert E2 to Composite1
John McCall60d7b3a2010-08-24 06:29:42 +00003445 ExprResult E2Result
John McCallca0408f2010-08-23 06:44:23 +00003446 = E2ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E2,1));
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003447 if (E2Result.isInvalid())
3448 return QualType();
3449 E2 = E2Result.takeAs<Expr>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003450
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003451 return Composite1;
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003452 }
3453
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003454 // Check whether Composite2 is viable.
3455 InitializedEntity Entity2
3456 = InitializedEntity::InitializeTemporary(Composite2);
3457 InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1);
3458 InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1);
3459 if (!E1ToC2 || !E2ToC2)
3460 return QualType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003461
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003462 // Convert E1 to Composite2
John McCall60d7b3a2010-08-24 06:29:42 +00003463 ExprResult E1Result
John McCallca0408f2010-08-23 06:44:23 +00003464 = E1ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E1, 1));
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003465 if (E1Result.isInvalid())
3466 return QualType();
3467 E1 = E1Result.takeAs<Expr>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003468
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003469 // Convert E2 to Composite2
John McCall60d7b3a2010-08-24 06:29:42 +00003470 ExprResult E2Result
John McCallca0408f2010-08-23 06:44:23 +00003471 = E2ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E2, 1));
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003472 if (E2Result.isInvalid())
3473 return QualType();
3474 E2 = E2Result.takeAs<Expr>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003475
Douglas Gregor8f00dcf2010-04-16 23:20:25 +00003476 return Composite2;
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00003477}
Anders Carlsson165a0a02009-05-17 18:41:29 +00003478
John McCall60d7b3a2010-08-24 06:29:42 +00003479ExprResult Sema::MaybeBindToTemporary(Expr *E) {
Douglas Gregor19cc1c72010-11-01 21:10:29 +00003480 if (!E)
3481 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003482
Anders Carlsson089c2602009-08-15 23:41:35 +00003483 if (!Context.getLangOptions().CPlusPlus)
3484 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00003485
Douglas Gregor51326552009-12-24 18:51:59 +00003486 assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?");
3487
Ted Kremenek6217b802009-07-29 21:53:49 +00003488 const RecordType *RT = E->getType()->getAs<RecordType>();
Anders Carlssondef11992009-05-30 20:36:53 +00003489 if (!RT)
3490 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00003491
Douglas Gregor5e6fcd42011-02-08 02:14:35 +00003492 // If the result is a glvalue, we shouldn't bind it.
3493 if (E->Classify(Context).isGLValue())
3494 return Owned(E);
John McCall86ff3082010-02-04 22:26:26 +00003495
3496 // That should be enough to guarantee that this type is complete.
3497 // If it has a trivial destructor, we can avoid the extra copy.
Jeffrey Yasskinb7ee2e52011-01-27 19:17:54 +00003498 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall507384f2010-08-12 02:40:37 +00003499 if (RD->isInvalidDecl() || RD->hasTrivialDestructor())
John McCall86ff3082010-02-04 22:26:26 +00003500 return Owned(E);
3501
Douglas Gregordb89f282010-07-01 22:47:18 +00003502 CXXTemporary *Temp = CXXTemporary::Create(Context, LookupDestructor(RD));
Anders Carlsson860306e2009-05-30 21:21:49 +00003503 ExprTemporaries.push_back(Temp);
Douglas Gregordb89f282010-07-01 22:47:18 +00003504 if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) {
Fariborz Jahaniana83f7ed2009-08-03 19:13:25 +00003505 MarkDeclarationReferenced(E->getExprLoc(), Destructor);
John McCallc91cc662010-04-07 00:41:46 +00003506 CheckDestructorAccess(E->getExprLoc(), Destructor,
3507 PDiag(diag::err_access_dtor_temp)
3508 << E->getType());
3509 }
Anders Carlssondef11992009-05-30 20:36:53 +00003510 // FIXME: Add the temporary to the temporaries vector.
3511 return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E));
3512}
3513
John McCall4765fa02010-12-06 08:20:24 +00003514Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) {
Anders Carlsson99ba36d2009-06-05 15:38:08 +00003515 assert(SubExpr && "sub expression can't be null!");
Mike Stump1eb44332009-09-09 15:08:12 +00003516
Douglas Gregor1f5f3a42009-12-03 17:10:37 +00003517 unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries;
3518 assert(ExprTemporaries.size() >= FirstTemporary);
3519 if (ExprTemporaries.size() == FirstTemporary)
Anders Carlsson99ba36d2009-06-05 15:38:08 +00003520 return SubExpr;
Mike Stump1eb44332009-09-09 15:08:12 +00003521
John McCall4765fa02010-12-06 08:20:24 +00003522 Expr *E = ExprWithCleanups::Create(Context, SubExpr,
3523 &ExprTemporaries[FirstTemporary],
3524 ExprTemporaries.size() - FirstTemporary);
Douglas Gregor1f5f3a42009-12-03 17:10:37 +00003525 ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary,
3526 ExprTemporaries.end());
Mike Stump1eb44332009-09-09 15:08:12 +00003527
Anders Carlsson99ba36d2009-06-05 15:38:08 +00003528 return E;
3529}
3530
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003531ExprResult
John McCall4765fa02010-12-06 08:20:24 +00003532Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) {
Douglas Gregor90f93822009-12-22 22:17:25 +00003533 if (SubExpr.isInvalid())
3534 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003535
John McCall4765fa02010-12-06 08:20:24 +00003536 return Owned(MaybeCreateExprWithCleanups(SubExpr.take()));
Douglas Gregor90f93822009-12-22 22:17:25 +00003537}
3538
John McCall4765fa02010-12-06 08:20:24 +00003539Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) {
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +00003540 assert(SubStmt && "sub statement can't be null!");
3541
3542 unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries;
3543 assert(ExprTemporaries.size() >= FirstTemporary);
3544 if (ExprTemporaries.size() == FirstTemporary)
3545 return SubStmt;
3546
3547 // FIXME: In order to attach the temporaries, wrap the statement into
3548 // a StmtExpr; currently this is only used for asm statements.
3549 // This is hacky, either create a new CXXStmtWithTemporaries statement or
3550 // a new AsmStmtWithTemporaries.
3551 CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, &SubStmt, 1,
3552 SourceLocation(),
3553 SourceLocation());
3554 Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(),
3555 SourceLocation());
John McCall4765fa02010-12-06 08:20:24 +00003556 return MaybeCreateExprWithCleanups(E);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +00003557}
3558
John McCall60d7b3a2010-08-24 06:29:42 +00003559ExprResult
John McCall9ae2f072010-08-23 23:25:46 +00003560Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, SourceLocation OpLoc,
John McCallb3d87482010-08-24 05:47:05 +00003561 tok::TokenKind OpKind, ParsedType &ObjectType,
Douglas Gregord4dca082010-02-24 18:44:31 +00003562 bool &MayBePseudoDestructor) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +00003563 // Since this might be a postfix expression, get rid of ParenListExprs.
John McCall60d7b3a2010-08-24 06:29:42 +00003564 ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
John McCall9ae2f072010-08-23 23:25:46 +00003565 if (Result.isInvalid()) return ExprError();
3566 Base = Result.get();
Mike Stump1eb44332009-09-09 15:08:12 +00003567
John McCall9ae2f072010-08-23 23:25:46 +00003568 QualType BaseType = Base->getType();
Douglas Gregord4dca082010-02-24 18:44:31 +00003569 MayBePseudoDestructor = false;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00003570 if (BaseType->isDependentType()) {
Douglas Gregor43d88632009-11-04 22:49:18 +00003571 // If we have a pointer to a dependent type and are using the -> operator,
3572 // the object type is the type that the pointer points to. We might still
3573 // have enough information about that type to do something useful.
3574 if (OpKind == tok::arrow)
3575 if (const PointerType *Ptr = BaseType->getAs<PointerType>())
3576 BaseType = Ptr->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003577
John McCallb3d87482010-08-24 05:47:05 +00003578 ObjectType = ParsedType::make(BaseType);
Douglas Gregord4dca082010-02-24 18:44:31 +00003579 MayBePseudoDestructor = true;
John McCall9ae2f072010-08-23 23:25:46 +00003580 return Owned(Base);
Douglas Gregor2dd078a2009-09-02 22:59:36 +00003581 }
Mike Stump1eb44332009-09-09 15:08:12 +00003582
Douglas Gregor2dd078a2009-09-02 22:59:36 +00003583 // C++ [over.match.oper]p8:
Mike Stump1eb44332009-09-09 15:08:12 +00003584 // [...] When operator->returns, the operator-> is applied to the value
Douglas Gregor2dd078a2009-09-02 22:59:36 +00003585 // returned, with the original second operand.
3586 if (OpKind == tok::arrow) {
John McCallc4e83212009-09-30 01:01:30 +00003587 // The set of types we've considered so far.
John McCall432887f2009-09-30 01:30:54 +00003588 llvm::SmallPtrSet<CanQualType,8> CTypes;
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +00003589 llvm::SmallVector<SourceLocation, 8> Locations;
John McCall432887f2009-09-30 01:30:54 +00003590 CTypes.insert(Context.getCanonicalType(BaseType));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003591
Douglas Gregor2dd078a2009-09-02 22:59:36 +00003592 while (BaseType->isRecordType()) {
John McCall9ae2f072010-08-23 23:25:46 +00003593 Result = BuildOverloadedArrowExpr(S, Base, OpLoc);
3594 if (Result.isInvalid())
Douglas Gregor2dd078a2009-09-02 22:59:36 +00003595 return ExprError();
John McCall9ae2f072010-08-23 23:25:46 +00003596 Base = Result.get();
3597 if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base))
Anders Carlssonde699e52009-10-13 22:55:59 +00003598 Locations.push_back(OpCall->getDirectCallee()->getLocation());
John McCall9ae2f072010-08-23 23:25:46 +00003599 BaseType = Base->getType();
John McCallc4e83212009-09-30 01:01:30 +00003600 CanQualType CBaseType = Context.getCanonicalType(BaseType);
John McCall432887f2009-09-30 01:30:54 +00003601 if (!CTypes.insert(CBaseType)) {
Fariborz Jahanian4a4e3452009-09-30 00:19:41 +00003602 Diag(OpLoc, diag::err_operator_arrow_circular);
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +00003603 for (unsigned i = 0; i < Locations.size(); i++)
3604 Diag(Locations[i], diag::note_declared_at);
Fariborz Jahanian4a4e3452009-09-30 00:19:41 +00003605 return ExprError();
3606 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +00003607 }
Mike Stump1eb44332009-09-09 15:08:12 +00003608
Douglas Gregor31658df2009-11-20 19:58:21 +00003609 if (BaseType->isPointerType())
3610 BaseType = BaseType->getPointeeType();
3611 }
Mike Stump1eb44332009-09-09 15:08:12 +00003612
3613 // We could end up with various non-record types here, such as extended
Douglas Gregor2dd078a2009-09-02 22:59:36 +00003614 // vector types or Objective-C interfaces. Just return early and let
3615 // ActOnMemberReferenceExpr do the work.
Douglas Gregorc68afe22009-09-03 21:38:09 +00003616 if (!BaseType->isRecordType()) {
3617 // C++ [basic.lookup.classref]p2:
3618 // [...] If the type of the object expression is of pointer to scalar
3619 // type, the unqualified-id is looked up in the context of the complete
3620 // postfix-expression.
Douglas Gregord4dca082010-02-24 18:44:31 +00003621 //
3622 // This also indicates that we should be parsing a
3623 // pseudo-destructor-name.
John McCallb3d87482010-08-24 05:47:05 +00003624 ObjectType = ParsedType();
Douglas Gregord4dca082010-02-24 18:44:31 +00003625 MayBePseudoDestructor = true;
John McCall9ae2f072010-08-23 23:25:46 +00003626 return Owned(Base);
Douglas Gregorc68afe22009-09-03 21:38:09 +00003627 }
Mike Stump1eb44332009-09-09 15:08:12 +00003628
Douglas Gregor03c57052009-11-17 05:17:33 +00003629 // The object type must be complete (or dependent).
3630 if (!BaseType->isDependentType() &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003631 RequireCompleteType(OpLoc, BaseType,
Douglas Gregor03c57052009-11-17 05:17:33 +00003632 PDiag(diag::err_incomplete_member_access)))
3633 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003634
Douglas Gregorc68afe22009-09-03 21:38:09 +00003635 // C++ [basic.lookup.classref]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00003636 // If the id-expression in a class member access (5.2.5) is an
Douglas Gregor03c57052009-11-17 05:17:33 +00003637 // unqualified-id, and the type of the object expression is of a class
Douglas Gregorc68afe22009-09-03 21:38:09 +00003638 // type C (or of pointer to a class type C), the unqualified-id is looked
3639 // up in the scope of class C. [...]
John McCallb3d87482010-08-24 05:47:05 +00003640 ObjectType = ParsedType::make(BaseType);
Mike Stump1eb44332009-09-09 15:08:12 +00003641 return move(Base);
Douglas Gregor2dd078a2009-09-02 22:59:36 +00003642}
3643
John McCall60d7b3a2010-08-24 06:29:42 +00003644ExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003645 Expr *MemExpr) {
Douglas Gregor77549082010-02-24 21:29:12 +00003646 SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc);
John McCall9ae2f072010-08-23 23:25:46 +00003647 Diag(MemExpr->getLocStart(), diag::err_dtor_expr_without_call)
3648 << isa<CXXPseudoDestructorExpr>(MemExpr)
Douglas Gregor849b2432010-03-31 17:46:05 +00003649 << FixItHint::CreateInsertion(ExpectedLParenLoc, "()");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003650
Douglas Gregor77549082010-02-24 21:29:12 +00003651 return ActOnCallExpr(/*Scope*/ 0,
John McCall9ae2f072010-08-23 23:25:46 +00003652 MemExpr,
Douglas Gregor77549082010-02-24 21:29:12 +00003653 /*LPLoc*/ ExpectedLParenLoc,
John McCallf312b1e2010-08-26 23:41:50 +00003654 MultiExprArg(),
Douglas Gregor77549082010-02-24 21:29:12 +00003655 /*RPLoc*/ ExpectedLParenLoc);
3656}
Douglas Gregord4dca082010-02-24 18:44:31 +00003657
John McCall60d7b3a2010-08-24 06:29:42 +00003658ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base,
John McCall2d9f5fa2011-02-25 05:21:17 +00003659 SourceLocation OpLoc,
3660 tok::TokenKind OpKind,
3661 const CXXScopeSpec &SS,
3662 TypeSourceInfo *ScopeTypeInfo,
3663 SourceLocation CCLoc,
3664 SourceLocation TildeLoc,
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003665 PseudoDestructorTypeStorage Destructed,
John McCall2d9f5fa2011-02-25 05:21:17 +00003666 bool HasTrailingLParen) {
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003667 TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003668
Douglas Gregorb57fb492010-02-24 22:38:50 +00003669 // C++ [expr.pseudo]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003670 // The left-hand side of the dot operator shall be of scalar type. The
Douglas Gregorb57fb492010-02-24 22:38:50 +00003671 // left-hand side of the arrow operator shall be of pointer to scalar type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003672 // This scalar type is the object type.
John McCall9ae2f072010-08-23 23:25:46 +00003673 QualType ObjectType = Base->getType();
Douglas Gregorb57fb492010-02-24 22:38:50 +00003674 if (OpKind == tok::arrow) {
3675 if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) {
3676 ObjectType = Ptr->getPointeeType();
John McCall9ae2f072010-08-23 23:25:46 +00003677 } else if (!Base->isTypeDependent()) {
Douglas Gregorb57fb492010-02-24 22:38:50 +00003678 // The user wrote "p->" when she probably meant "p."; fix it.
3679 Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
3680 << ObjectType << true
Douglas Gregor849b2432010-03-31 17:46:05 +00003681 << FixItHint::CreateReplacement(OpLoc, ".");
Douglas Gregorb57fb492010-02-24 22:38:50 +00003682 if (isSFINAEContext())
3683 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003684
Douglas Gregorb57fb492010-02-24 22:38:50 +00003685 OpKind = tok::period;
3686 }
3687 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003688
Douglas Gregorb57fb492010-02-24 22:38:50 +00003689 if (!ObjectType->isDependentType() && !ObjectType->isScalarType()) {
3690 Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar)
John McCall9ae2f072010-08-23 23:25:46 +00003691 << ObjectType << Base->getSourceRange();
Douglas Gregorb57fb492010-02-24 22:38:50 +00003692 return ExprError();
3693 }
3694
3695 // C++ [expr.pseudo]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003696 // [...] The cv-unqualified versions of the object type and of the type
Douglas Gregorb57fb492010-02-24 22:38:50 +00003697 // designated by the pseudo-destructor-name shall be the same type.
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003698 if (DestructedTypeInfo) {
3699 QualType DestructedType = DestructedTypeInfo->getType();
3700 SourceLocation DestructedTypeStart
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003701 = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003702 if (!DestructedType->isDependentType() && !ObjectType->isDependentType() &&
3703 !Context.hasSameUnqualifiedType(DestructedType, ObjectType)) {
3704 Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch)
John McCall9ae2f072010-08-23 23:25:46 +00003705 << ObjectType << DestructedType << Base->getSourceRange()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003706 << DestructedTypeInfo->getTypeLoc().getLocalSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003707
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003708 // Recover by setting the destructed type to the object type.
3709 DestructedType = ObjectType;
3710 DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType,
3711 DestructedTypeStart);
3712 Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo);
3713 }
Douglas Gregorb57fb492010-02-24 22:38:50 +00003714 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003715
Douglas Gregorb57fb492010-02-24 22:38:50 +00003716 // C++ [expr.pseudo]p2:
3717 // [...] Furthermore, the two type-names in a pseudo-destructor-name of the
3718 // form
3719 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003720 // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name
Douglas Gregorb57fb492010-02-24 22:38:50 +00003721 //
3722 // shall designate the same scalar type.
3723 if (ScopeTypeInfo) {
3724 QualType ScopeType = ScopeTypeInfo->getType();
3725 if (!ScopeType->isDependentType() && !ObjectType->isDependentType() &&
John McCall81e317a2010-06-11 17:36:40 +00003726 !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003727
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003728 Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(),
Douglas Gregorb57fb492010-02-24 22:38:50 +00003729 diag::err_pseudo_dtor_type_mismatch)
John McCall9ae2f072010-08-23 23:25:46 +00003730 << ObjectType << ScopeType << Base->getSourceRange()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003731 << ScopeTypeInfo->getTypeLoc().getLocalSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003732
Douglas Gregorb57fb492010-02-24 22:38:50 +00003733 ScopeType = QualType();
3734 ScopeTypeInfo = 0;
3735 }
3736 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003737
John McCall9ae2f072010-08-23 23:25:46 +00003738 Expr *Result
3739 = new (Context) CXXPseudoDestructorExpr(Context, Base,
3740 OpKind == tok::arrow, OpLoc,
Douglas Gregorf3db29f2011-02-25 18:19:59 +00003741 SS.getWithLocInContext(Context),
John McCall9ae2f072010-08-23 23:25:46 +00003742 ScopeTypeInfo,
3743 CCLoc,
3744 TildeLoc,
3745 Destructed);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003746
Douglas Gregorb57fb492010-02-24 22:38:50 +00003747 if (HasTrailingLParen)
John McCall9ae2f072010-08-23 23:25:46 +00003748 return Owned(Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003749
John McCall9ae2f072010-08-23 23:25:46 +00003750 return DiagnoseDtorReference(Destructed.getLocation(), Result);
Douglas Gregor77549082010-02-24 21:29:12 +00003751}
3752
John McCall60d7b3a2010-08-24 06:29:42 +00003753ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base,
John McCall2d9f5fa2011-02-25 05:21:17 +00003754 SourceLocation OpLoc,
3755 tok::TokenKind OpKind,
3756 CXXScopeSpec &SS,
3757 UnqualifiedId &FirstTypeName,
3758 SourceLocation CCLoc,
3759 SourceLocation TildeLoc,
3760 UnqualifiedId &SecondTypeName,
3761 bool HasTrailingLParen) {
Douglas Gregor77549082010-02-24 21:29:12 +00003762 assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId ||
3763 FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) &&
3764 "Invalid first type name in pseudo-destructor");
3765 assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId ||
3766 SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) &&
3767 "Invalid second type name in pseudo-destructor");
3768
Douglas Gregor77549082010-02-24 21:29:12 +00003769 // C++ [expr.pseudo]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003770 // The left-hand side of the dot operator shall be of scalar type. The
Douglas Gregor77549082010-02-24 21:29:12 +00003771 // left-hand side of the arrow operator shall be of pointer to scalar type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003772 // This scalar type is the object type.
John McCall9ae2f072010-08-23 23:25:46 +00003773 QualType ObjectType = Base->getType();
Douglas Gregor77549082010-02-24 21:29:12 +00003774 if (OpKind == tok::arrow) {
3775 if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) {
3776 ObjectType = Ptr->getPointeeType();
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003777 } else if (!ObjectType->isDependentType()) {
Douglas Gregor77549082010-02-24 21:29:12 +00003778 // The user wrote "p->" when she probably meant "p."; fix it.
3779 Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003780 << ObjectType << true
Douglas Gregor849b2432010-03-31 17:46:05 +00003781 << FixItHint::CreateReplacement(OpLoc, ".");
Douglas Gregor77549082010-02-24 21:29:12 +00003782 if (isSFINAEContext())
3783 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003784
Douglas Gregor77549082010-02-24 21:29:12 +00003785 OpKind = tok::period;
3786 }
3787 }
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003788
3789 // Compute the object type that we should use for name lookup purposes. Only
3790 // record types and dependent types matter.
John McCallb3d87482010-08-24 05:47:05 +00003791 ParsedType ObjectTypePtrForLookup;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003792 if (!SS.isSet()) {
John McCall2d9f5fa2011-02-25 05:21:17 +00003793 if (ObjectType->isRecordType())
3794 ObjectTypePtrForLookup = ParsedType::make(ObjectType);
John McCallb3d87482010-08-24 05:47:05 +00003795 else if (ObjectType->isDependentType())
3796 ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy);
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003797 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003798
3799 // Convert the name of the type being destructed (following the ~) into a
Douglas Gregorb57fb492010-02-24 22:38:50 +00003800 // type (with source-location information).
Douglas Gregor77549082010-02-24 21:29:12 +00003801 QualType DestructedType;
3802 TypeSourceInfo *DestructedTypeInfo = 0;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003803 PseudoDestructorTypeStorage Destructed;
Douglas Gregor77549082010-02-24 21:29:12 +00003804 if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003805 ParsedType T = getTypeName(*SecondTypeName.Identifier,
John McCallb3d87482010-08-24 05:47:05 +00003806 SecondTypeName.StartLocation,
Fariborz Jahanian1e52dfc2011-02-08 18:05:59 +00003807 S, &SS, true, false, ObjectTypePtrForLookup);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003808 if (!T &&
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003809 ((SS.isSet() && !computeDeclContext(SS, false)) ||
3810 (!SS.isSet() && ObjectType->isDependentType()))) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003811 // The name of the type being destroyed is a dependent name, and we
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003812 // couldn't find anything useful in scope. Just store the identifier and
3813 // it's location, and we'll perform (qualified) name lookup again at
3814 // template instantiation time.
3815 Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier,
3816 SecondTypeName.StartLocation);
3817 } else if (!T) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003818 Diag(SecondTypeName.StartLocation,
Douglas Gregor77549082010-02-24 21:29:12 +00003819 diag::err_pseudo_dtor_destructor_non_type)
3820 << SecondTypeName.Identifier << ObjectType;
3821 if (isSFINAEContext())
3822 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003823
Douglas Gregor77549082010-02-24 21:29:12 +00003824 // Recover by assuming we had the right type all along.
3825 DestructedType = ObjectType;
Douglas Gregorb57fb492010-02-24 22:38:50 +00003826 } else
Douglas Gregor77549082010-02-24 21:29:12 +00003827 DestructedType = GetTypeFromParser(T, &DestructedTypeInfo);
Douglas Gregor77549082010-02-24 21:29:12 +00003828 } else {
Douglas Gregorb57fb492010-02-24 22:38:50 +00003829 // Resolve the template-id to a type.
Douglas Gregor77549082010-02-24 21:29:12 +00003830 TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId;
Douglas Gregorb57fb492010-02-24 22:38:50 +00003831 ASTTemplateArgsPtr TemplateArgsPtr(*this,
3832 TemplateId->getTemplateArgs(),
3833 TemplateId->NumArgs);
Douglas Gregor059101f2011-03-02 00:47:37 +00003834 TypeResult T = ActOnTemplateIdType(TemplateId->SS,
3835 TemplateId->Template,
Douglas Gregorb57fb492010-02-24 22:38:50 +00003836 TemplateId->TemplateNameLoc,
3837 TemplateId->LAngleLoc,
3838 TemplateArgsPtr,
3839 TemplateId->RAngleLoc);
3840 if (T.isInvalid() || !T.get()) {
3841 // Recover by assuming we had the right type all along.
3842 DestructedType = ObjectType;
3843 } else
3844 DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo);
Douglas Gregor77549082010-02-24 21:29:12 +00003845 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003846
3847 // If we've performed some kind of recovery, (re-)build the type source
Douglas Gregorb57fb492010-02-24 22:38:50 +00003848 // information.
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003849 if (!DestructedType.isNull()) {
3850 if (!DestructedTypeInfo)
3851 DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType,
Douglas Gregorb57fb492010-02-24 22:38:50 +00003852 SecondTypeName.StartLocation);
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003853 Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo);
3854 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003855
Douglas Gregorb57fb492010-02-24 22:38:50 +00003856 // Convert the name of the scope type (the type prior to '::') into a type.
3857 TypeSourceInfo *ScopeTypeInfo = 0;
Douglas Gregor77549082010-02-24 21:29:12 +00003858 QualType ScopeType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003859 if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId ||
Douglas Gregor77549082010-02-24 21:29:12 +00003860 FirstTypeName.Identifier) {
3861 if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003862 ParsedType T = getTypeName(*FirstTypeName.Identifier,
John McCallb3d87482010-08-24 05:47:05 +00003863 FirstTypeName.StartLocation,
Douglas Gregorf3db29f2011-02-25 18:19:59 +00003864 S, &SS, true, false, ObjectTypePtrForLookup);
Douglas Gregor77549082010-02-24 21:29:12 +00003865 if (!T) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003866 Diag(FirstTypeName.StartLocation,
Douglas Gregor77549082010-02-24 21:29:12 +00003867 diag::err_pseudo_dtor_destructor_non_type)
3868 << FirstTypeName.Identifier << ObjectType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003869
Douglas Gregorb57fb492010-02-24 22:38:50 +00003870 if (isSFINAEContext())
3871 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003872
Douglas Gregorb57fb492010-02-24 22:38:50 +00003873 // Just drop this type. It's unnecessary anyway.
3874 ScopeType = QualType();
3875 } else
3876 ScopeType = GetTypeFromParser(T, &ScopeTypeInfo);
Douglas Gregor77549082010-02-24 21:29:12 +00003877 } else {
Douglas Gregorb57fb492010-02-24 22:38:50 +00003878 // Resolve the template-id to a type.
Douglas Gregor77549082010-02-24 21:29:12 +00003879 TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId;
Douglas Gregorb57fb492010-02-24 22:38:50 +00003880 ASTTemplateArgsPtr TemplateArgsPtr(*this,
3881 TemplateId->getTemplateArgs(),
3882 TemplateId->NumArgs);
Douglas Gregor059101f2011-03-02 00:47:37 +00003883 TypeResult T = ActOnTemplateIdType(TemplateId->SS,
3884 TemplateId->Template,
Douglas Gregorb57fb492010-02-24 22:38:50 +00003885 TemplateId->TemplateNameLoc,
3886 TemplateId->LAngleLoc,
3887 TemplateArgsPtr,
3888 TemplateId->RAngleLoc);
3889 if (T.isInvalid() || !T.get()) {
3890 // Recover by dropping this type.
3891 ScopeType = QualType();
3892 } else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003893 ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo);
Douglas Gregor77549082010-02-24 21:29:12 +00003894 }
3895 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003896
Douglas Gregorb4a418f2010-02-24 23:02:30 +00003897 if (!ScopeType.isNull() && !ScopeTypeInfo)
3898 ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType,
3899 FirstTypeName.StartLocation);
3900
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003901
John McCall9ae2f072010-08-23 23:25:46 +00003902 return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS,
Douglas Gregorfce46ee2010-02-24 23:50:37 +00003903 ScopeTypeInfo, CCLoc, TildeLoc,
Douglas Gregora2e7dd22010-02-25 01:56:36 +00003904 Destructed, HasTrailingLParen);
Douglas Gregord4dca082010-02-24 18:44:31 +00003905}
3906
John Wiegley429bb272011-04-08 18:41:53 +00003907ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl,
Douglas Gregorf2ae5262011-01-20 00:18:04 +00003908 CXXMethodDecl *Method) {
John Wiegley429bb272011-04-08 18:41:53 +00003909 ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/0,
3910 FoundDecl, Method);
3911 if (Exp.isInvalid())
Douglas Gregorf2ae5262011-01-20 00:18:04 +00003912 return true;
Eli Friedman772fffa2009-12-09 04:53:56 +00003913
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003914 MemberExpr *ME =
John Wiegley429bb272011-04-08 18:41:53 +00003915 new (Context) MemberExpr(Exp.take(), /*IsArrow=*/false, Method,
John McCallf89e55a2010-11-18 06:31:45 +00003916 SourceLocation(), Method->getType(),
3917 VK_RValue, OK_Ordinary);
3918 QualType ResultType = Method->getResultType();
3919 ExprValueKind VK = Expr::getValueKindForType(ResultType);
3920 ResultType = ResultType.getNonLValueExprType(Context);
3921
John Wiegley429bb272011-04-08 18:41:53 +00003922 MarkDeclarationReferenced(Exp.get()->getLocStart(), Method);
Douglas Gregor7edfb692009-11-23 12:27:39 +00003923 CXXMemberCallExpr *CE =
John McCallf89e55a2010-11-18 06:31:45 +00003924 new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, VK,
John Wiegley429bb272011-04-08 18:41:53 +00003925 Exp.get()->getLocEnd());
Fariborz Jahanianb7400232009-09-28 23:23:40 +00003926 return CE;
3927}
3928
Sebastian Redl2e156222010-09-10 20:55:43 +00003929ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand,
3930 SourceLocation RParen) {
Sebastian Redl2e156222010-09-10 20:55:43 +00003931 return Owned(new (Context) CXXNoexceptExpr(Context.BoolTy, Operand,
3932 Operand->CanThrow(Context),
3933 KeyLoc, RParen));
3934}
3935
3936ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation,
3937 Expr *Operand, SourceLocation RParen) {
3938 return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00003939}
3940
John McCallf6a16482010-12-04 03:47:34 +00003941/// Perform the conversions required for an expression used in a
3942/// context that ignores the result.
John Wiegley429bb272011-04-08 18:41:53 +00003943ExprResult Sema::IgnoredValueConversions(Expr *E) {
John McCalla878cda2010-12-02 02:07:15 +00003944 // C99 6.3.2.1:
3945 // [Except in specific positions,] an lvalue that does not have
3946 // array type is converted to the value stored in the
3947 // designated object (and is no longer an lvalue).
John Wiegley429bb272011-04-08 18:41:53 +00003948 if (E->isRValue()) return Owned(E);
John McCalla878cda2010-12-02 02:07:15 +00003949
John McCallf6a16482010-12-04 03:47:34 +00003950 // We always want to do this on ObjC property references.
3951 if (E->getObjectKind() == OK_ObjCProperty) {
John Wiegley429bb272011-04-08 18:41:53 +00003952 ExprResult Res = ConvertPropertyForRValue(E);
3953 if (Res.isInvalid()) return Owned(E);
3954 E = Res.take();
3955 if (E->isRValue()) return Owned(E);
John McCallf6a16482010-12-04 03:47:34 +00003956 }
3957
3958 // Otherwise, this rule does not apply in C++, at least not for the moment.
John Wiegley429bb272011-04-08 18:41:53 +00003959 if (getLangOptions().CPlusPlus) return Owned(E);
John McCallf6a16482010-12-04 03:47:34 +00003960
3961 // GCC seems to also exclude expressions of incomplete enum type.
3962 if (const EnumType *T = E->getType()->getAs<EnumType>()) {
3963 if (!T->getDecl()->isComplete()) {
3964 // FIXME: stupid workaround for a codegen bug!
John Wiegley429bb272011-04-08 18:41:53 +00003965 E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).take();
3966 return Owned(E);
John McCallf6a16482010-12-04 03:47:34 +00003967 }
3968 }
3969
John Wiegley429bb272011-04-08 18:41:53 +00003970 ExprResult Res = DefaultFunctionArrayLvalueConversion(E);
3971 if (Res.isInvalid())
3972 return Owned(E);
3973 E = Res.take();
3974
John McCall85515d62010-12-04 12:29:11 +00003975 if (!E->getType()->isVoidType())
3976 RequireCompleteType(E->getExprLoc(), E->getType(),
3977 diag::err_incomplete_type);
John Wiegley429bb272011-04-08 18:41:53 +00003978 return Owned(E);
John McCallf6a16482010-12-04 03:47:34 +00003979}
3980
John Wiegley429bb272011-04-08 18:41:53 +00003981ExprResult Sema::ActOnFinishFullExpr(Expr *FE) {
3982 ExprResult FullExpr = Owned(FE);
3983
3984 if (!FullExpr.get())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003985 return ExprError();
John McCallf6a16482010-12-04 03:47:34 +00003986
John Wiegley429bb272011-04-08 18:41:53 +00003987 if (DiagnoseUnexpandedParameterPack(FullExpr.get()))
Douglas Gregord0937222010-12-13 22:49:22 +00003988 return ExprError();
3989
Douglas Gregor353ee242011-03-07 02:05:23 +00003990 // 13.4.1 ... An overloaded function name shall not be used without arguments
3991 // in contexts other than those listed [i.e list of targets].
3992 //
3993 // void foo(); void foo(int);
3994 // template<class T> void fooT(); template<class T> void fooT(int);
3995
3996 // Therefore these should error:
3997 // foo;
3998 // fooT<int>;
3999
John Wiegley429bb272011-04-08 18:41:53 +00004000 if (FullExpr.get()->getType() == Context.OverloadTy) {
Douglas Gregordb2eae62011-03-16 19:16:25 +00004001 ExprResult Fixed
John Wiegley429bb272011-04-08 18:41:53 +00004002 = ResolveAndFixSingleFunctionTemplateSpecialization(FullExpr.get(),
Douglas Gregordb2eae62011-03-16 19:16:25 +00004003 /*DoFunctionPointerConversion=*/false,
4004 /*Complain=*/true,
John Wiegley429bb272011-04-08 18:41:53 +00004005 FullExpr.get()->getSourceRange(),
Douglas Gregordb2eae62011-03-16 19:16:25 +00004006 QualType(),
4007 diag::err_addr_ovl_ambiguous);
4008 if (Fixed.isInvalid())
Douglas Gregor353ee242011-03-07 02:05:23 +00004009 return ExprError();
Douglas Gregordb2eae62011-03-16 19:16:25 +00004010
4011 FullExpr = Fixed.get();
Douglas Gregor353ee242011-03-07 02:05:23 +00004012 }
4013
4014
John Wiegley429bb272011-04-08 18:41:53 +00004015 FullExpr = IgnoredValueConversions(FullExpr.take());
4016 if (FullExpr.isInvalid())
4017 return ExprError();
4018
4019 CheckImplicitConversions(FullExpr.get());
John McCall4765fa02010-12-06 08:20:24 +00004020 return MaybeCreateExprWithCleanups(FullExpr);
Anders Carlsson165a0a02009-05-17 18:41:29 +00004021}
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +00004022
4023StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) {
4024 if (!FullStmt) return StmtError();
4025
John McCall4765fa02010-12-06 08:20:24 +00004026 return MaybeCreateStmtWithCleanups(FullStmt);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +00004027}