1 | //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// |
9 | /// \file |
10 | /// Implements semantic analysis for C++ expressions. |
11 | /// |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #include "TreeTransform.h" |
15 | #include "TypeLocBuilder.h" |
16 | #include "clang/AST/ASTContext.h" |
17 | #include "clang/AST/ASTLambda.h" |
18 | #include "clang/AST/CXXInheritance.h" |
19 | #include "clang/AST/CharUnits.h" |
20 | #include "clang/AST/DeclObjC.h" |
21 | #include "clang/AST/ExprCXX.h" |
22 | #include "clang/AST/ExprConcepts.h" |
23 | #include "clang/AST/ExprObjC.h" |
24 | #include "clang/AST/RecursiveASTVisitor.h" |
25 | #include "clang/AST/Type.h" |
26 | #include "clang/AST/TypeLoc.h" |
27 | #include "clang/Basic/AlignedAllocation.h" |
28 | #include "clang/Basic/DiagnosticSema.h" |
29 | #include "clang/Basic/PartialDiagnostic.h" |
30 | #include "clang/Basic/TargetInfo.h" |
31 | #include "clang/Basic/TokenKinds.h" |
32 | #include "clang/Basic/TypeTraits.h" |
33 | #include "clang/Lex/Preprocessor.h" |
34 | #include "clang/Sema/DeclSpec.h" |
35 | #include "clang/Sema/EnterExpressionEvaluationContext.h" |
36 | #include "clang/Sema/Initialization.h" |
37 | #include "clang/Sema/Lookup.h" |
38 | #include "clang/Sema/ParsedTemplate.h" |
39 | #include "clang/Sema/Scope.h" |
40 | #include "clang/Sema/ScopeInfo.h" |
41 | #include "clang/Sema/SemaInternal.h" |
42 | #include "clang/Sema/SemaLambda.h" |
43 | #include "clang/Sema/Template.h" |
44 | #include "clang/Sema/TemplateDeduction.h" |
45 | #include "llvm/ADT/APInt.h" |
46 | #include "llvm/ADT/STLExtras.h" |
47 | #include "llvm/ADT/StringExtras.h" |
48 | #include "llvm/Support/ErrorHandling.h" |
49 | #include "llvm/Support/TypeSize.h" |
50 | #include <optional> |
51 | using namespace clang; |
52 | using namespace sema; |
53 | |
54 | /// Handle the result of the special case name lookup for inheriting |
55 | /// constructor declarations. 'NS::X::X' and 'NS::X<...>::X' are treated as |
56 | /// constructor names in member using declarations, even if 'X' is not the |
57 | /// name of the corresponding type. |
58 | ParsedType Sema::getInheritingConstructorName(CXXScopeSpec &SS, |
59 | SourceLocation NameLoc, |
60 | IdentifierInfo &Name) { |
61 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
62 | |
63 | // Convert the nested-name-specifier into a type. |
64 | QualType Type; |
65 | switch (NNS->getKind()) { |
66 | case NestedNameSpecifier::TypeSpec: |
67 | case NestedNameSpecifier::TypeSpecWithTemplate: |
68 | Type = QualType(NNS->getAsType(), 0); |
69 | break; |
70 | |
71 | case NestedNameSpecifier::Identifier: |
72 | // Strip off the last layer of the nested-name-specifier and build a |
73 | // typename type for it. |
74 | assert(NNS->getAsIdentifier() == &Name && "not a constructor name" ); |
75 | Type = Context.getDependentNameType( |
76 | Keyword: ElaboratedTypeKeyword::None, NNS: NNS->getPrefix(), Name: NNS->getAsIdentifier()); |
77 | break; |
78 | |
79 | case NestedNameSpecifier::Global: |
80 | case NestedNameSpecifier::Super: |
81 | case NestedNameSpecifier::Namespace: |
82 | case NestedNameSpecifier::NamespaceAlias: |
83 | llvm_unreachable("Nested name specifier is not a type for inheriting ctor" ); |
84 | } |
85 | |
86 | // This reference to the type is located entirely at the location of the |
87 | // final identifier in the qualified-id. |
88 | return CreateParsedType(T: Type, |
89 | TInfo: Context.getTrivialTypeSourceInfo(T: Type, Loc: NameLoc)); |
90 | } |
91 | |
92 | ParsedType Sema::getConstructorName(IdentifierInfo &II, |
93 | SourceLocation NameLoc, |
94 | Scope *S, CXXScopeSpec &SS, |
95 | bool EnteringContext) { |
96 | CXXRecordDecl *CurClass = getCurrentClass(S, SS: &SS); |
97 | assert(CurClass && &II == CurClass->getIdentifier() && |
98 | "not a constructor name" ); |
99 | |
100 | // When naming a constructor as a member of a dependent context (eg, in a |
101 | // friend declaration or an inherited constructor declaration), form an |
102 | // unresolved "typename" type. |
103 | if (CurClass->isDependentContext() && !EnteringContext && SS.getScopeRep()) { |
104 | QualType T = Context.getDependentNameType(Keyword: ElaboratedTypeKeyword::None, |
105 | NNS: SS.getScopeRep(), Name: &II); |
106 | return ParsedType::make(P: T); |
107 | } |
108 | |
109 | if (SS.isNotEmpty() && RequireCompleteDeclContext(SS, CurClass)) |
110 | return ParsedType(); |
111 | |
112 | // Find the injected-class-name declaration. Note that we make no attempt to |
113 | // diagnose cases where the injected-class-name is shadowed: the only |
114 | // declaration that can validly shadow the injected-class-name is a |
115 | // non-static data member, and if the class contains both a non-static data |
116 | // member and a constructor then it is ill-formed (we check that in |
117 | // CheckCompletedCXXClass). |
118 | CXXRecordDecl *InjectedClassName = nullptr; |
119 | for (NamedDecl *ND : CurClass->lookup(&II)) { |
120 | auto *RD = dyn_cast<CXXRecordDecl>(ND); |
121 | if (RD && RD->isInjectedClassName()) { |
122 | InjectedClassName = RD; |
123 | break; |
124 | } |
125 | } |
126 | if (!InjectedClassName) { |
127 | if (!CurClass->isInvalidDecl()) { |
128 | // FIXME: RequireCompleteDeclContext doesn't check dependent contexts |
129 | // properly. Work around it here for now. |
130 | Diag(SS.getLastQualifierNameLoc(), |
131 | diag::err_incomplete_nested_name_spec) << CurClass << SS.getRange(); |
132 | } |
133 | return ParsedType(); |
134 | } |
135 | |
136 | QualType T = Context.getTypeDeclType(InjectedClassName); |
137 | DiagnoseUseOfDecl(InjectedClassName, NameLoc); |
138 | MarkAnyDeclReferenced(NameLoc, InjectedClassName, /*OdrUse=*/false); |
139 | |
140 | return ParsedType::make(P: T); |
141 | } |
142 | |
143 | ParsedType Sema::getDestructorName(IdentifierInfo &II, SourceLocation NameLoc, |
144 | Scope *S, CXXScopeSpec &SS, |
145 | ParsedType ObjectTypePtr, |
146 | bool EnteringContext) { |
147 | // Determine where to perform name lookup. |
148 | |
149 | // FIXME: This area of the standard is very messy, and the current |
150 | // wording is rather unclear about which scopes we search for the |
151 | // destructor name; see core issues 399 and 555. Issue 399 in |
152 | // particular shows where the current description of destructor name |
153 | // lookup is completely out of line with existing practice, e.g., |
154 | // this appears to be ill-formed: |
155 | // |
156 | // namespace N { |
157 | // template <typename T> struct S { |
158 | // ~S(); |
159 | // }; |
160 | // } |
161 | // |
162 | // void f(N::S<int>* s) { |
163 | // s->N::S<int>::~S(); |
164 | // } |
165 | // |
166 | // See also PR6358 and PR6359. |
167 | // |
168 | // For now, we accept all the cases in which the name given could plausibly |
169 | // be interpreted as a correct destructor name, issuing off-by-default |
170 | // extension diagnostics on the cases that don't strictly conform to the |
171 | // C++20 rules. This basically means we always consider looking in the |
172 | // nested-name-specifier prefix, the complete nested-name-specifier, and |
173 | // the scope, and accept if we find the expected type in any of the three |
174 | // places. |
175 | |
176 | if (SS.isInvalid()) |
177 | return nullptr; |
178 | |
179 | // Whether we've failed with a diagnostic already. |
180 | bool Failed = false; |
181 | |
182 | llvm::SmallVector<NamedDecl*, 8> FoundDecls; |
183 | llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 8> FoundDeclSet; |
184 | |
185 | // If we have an object type, it's because we are in a |
186 | // pseudo-destructor-expression or a member access expression, and |
187 | // we know what type we're looking for. |
188 | QualType SearchType = |
189 | ObjectTypePtr ? GetTypeFromParser(Ty: ObjectTypePtr) : QualType(); |
190 | |
191 | auto CheckLookupResult = [&](LookupResult &Found) -> ParsedType { |
192 | auto IsAcceptableResult = [&](NamedDecl *D) -> bool { |
193 | auto *Type = dyn_cast<TypeDecl>(Val: D->getUnderlyingDecl()); |
194 | if (!Type) |
195 | return false; |
196 | |
197 | if (SearchType.isNull() || SearchType->isDependentType()) |
198 | return true; |
199 | |
200 | QualType T = Context.getTypeDeclType(Decl: Type); |
201 | return Context.hasSameUnqualifiedType(T1: T, T2: SearchType); |
202 | }; |
203 | |
204 | unsigned NumAcceptableResults = 0; |
205 | for (NamedDecl *D : Found) { |
206 | if (IsAcceptableResult(D)) |
207 | ++NumAcceptableResults; |
208 | |
209 | // Don't list a class twice in the lookup failure diagnostic if it's |
210 | // found by both its injected-class-name and by the name in the enclosing |
211 | // scope. |
212 | if (auto *RD = dyn_cast<CXXRecordDecl>(Val: D)) |
213 | if (RD->isInjectedClassName()) |
214 | D = cast<NamedDecl>(RD->getParent()); |
215 | |
216 | if (FoundDeclSet.insert(D).second) |
217 | FoundDecls.push_back(Elt: D); |
218 | } |
219 | |
220 | // As an extension, attempt to "fix" an ambiguity by erasing all non-type |
221 | // results, and all non-matching results if we have a search type. It's not |
222 | // clear what the right behavior is if destructor lookup hits an ambiguity, |
223 | // but other compilers do generally accept at least some kinds of |
224 | // ambiguity. |
225 | if (Found.isAmbiguous() && NumAcceptableResults == 1) { |
226 | Diag(NameLoc, diag::ext_dtor_name_ambiguous); |
227 | LookupResult::Filter F = Found.makeFilter(); |
228 | while (F.hasNext()) { |
229 | NamedDecl *D = F.next(); |
230 | if (auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl())) |
231 | Diag(D->getLocation(), diag::note_destructor_type_here) |
232 | << Context.getTypeDeclType(TD); |
233 | else |
234 | Diag(D->getLocation(), diag::note_destructor_nontype_here); |
235 | |
236 | if (!IsAcceptableResult(D)) |
237 | F.erase(); |
238 | } |
239 | F.done(); |
240 | } |
241 | |
242 | if (Found.isAmbiguous()) |
243 | Failed = true; |
244 | |
245 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
246 | if (IsAcceptableResult(Type)) { |
247 | QualType T = Context.getTypeDeclType(Decl: Type); |
248 | MarkAnyDeclReferenced(Loc: Type->getLocation(), D: Type, /*OdrUse=*/MightBeOdrUse: false); |
249 | return CreateParsedType( |
250 | T: Context.getElaboratedType(Keyword: ElaboratedTypeKeyword::None, NNS: nullptr, NamedType: T), |
251 | TInfo: Context.getTrivialTypeSourceInfo(T, Loc: NameLoc)); |
252 | } |
253 | } |
254 | |
255 | return nullptr; |
256 | }; |
257 | |
258 | bool IsDependent = false; |
259 | |
260 | auto LookupInObjectType = [&]() -> ParsedType { |
261 | if (Failed || SearchType.isNull()) |
262 | return nullptr; |
263 | |
264 | IsDependent |= SearchType->isDependentType(); |
265 | |
266 | LookupResult Found(*this, &II, NameLoc, LookupDestructorName); |
267 | DeclContext *LookupCtx = computeDeclContext(T: SearchType); |
268 | if (!LookupCtx) |
269 | return nullptr; |
270 | LookupQualifiedName(R&: Found, LookupCtx); |
271 | return CheckLookupResult(Found); |
272 | }; |
273 | |
274 | auto LookupInNestedNameSpec = [&](CXXScopeSpec &LookupSS) -> ParsedType { |
275 | if (Failed) |
276 | return nullptr; |
277 | |
278 | IsDependent |= isDependentScopeSpecifier(SS: LookupSS); |
279 | DeclContext *LookupCtx = computeDeclContext(SS: LookupSS, EnteringContext); |
280 | if (!LookupCtx) |
281 | return nullptr; |
282 | |
283 | LookupResult Found(*this, &II, NameLoc, LookupDestructorName); |
284 | if (RequireCompleteDeclContext(SS&: LookupSS, DC: LookupCtx)) { |
285 | Failed = true; |
286 | return nullptr; |
287 | } |
288 | LookupQualifiedName(R&: Found, LookupCtx); |
289 | return CheckLookupResult(Found); |
290 | }; |
291 | |
292 | auto LookupInScope = [&]() -> ParsedType { |
293 | if (Failed || !S) |
294 | return nullptr; |
295 | |
296 | LookupResult Found(*this, &II, NameLoc, LookupDestructorName); |
297 | LookupName(R&: Found, S); |
298 | return CheckLookupResult(Found); |
299 | }; |
300 | |
301 | // C++2a [basic.lookup.qual]p6: |
302 | // In a qualified-id of the form |
303 | // |
304 | // nested-name-specifier[opt] type-name :: ~ type-name |
305 | // |
306 | // the second type-name is looked up in the same scope as the first. |
307 | // |
308 | // We interpret this as meaning that if you do a dual-scope lookup for the |
309 | // first name, you also do a dual-scope lookup for the second name, per |
310 | // C++ [basic.lookup.classref]p4: |
311 | // |
312 | // If the id-expression in a class member access is a qualified-id of the |
313 | // form |
314 | // |
315 | // class-name-or-namespace-name :: ... |
316 | // |
317 | // the class-name-or-namespace-name following the . or -> is first looked |
318 | // up in the class of the object expression and the name, if found, is used. |
319 | // Otherwise, it is looked up in the context of the entire |
320 | // postfix-expression. |
321 | // |
322 | // This looks in the same scopes as for an unqualified destructor name: |
323 | // |
324 | // C++ [basic.lookup.classref]p3: |
325 | // If the unqualified-id is ~ type-name, the type-name is looked up |
326 | // in the context of the entire postfix-expression. If the type T |
327 | // of the object expression is of a class type C, the type-name is |
328 | // also looked up in the scope of class C. At least one of the |
329 | // lookups shall find a name that refers to cv T. |
330 | // |
331 | // FIXME: The intent is unclear here. Should type-name::~type-name look in |
332 | // the scope anyway if it finds a non-matching name declared in the class? |
333 | // If both lookups succeed and find a dependent result, which result should |
334 | // we retain? (Same question for p->~type-name().) |
335 | |
336 | if (NestedNameSpecifier *Prefix = |
337 | SS.isSet() ? SS.getScopeRep()->getPrefix() : nullptr) { |
338 | // This is |
339 | // |
340 | // nested-name-specifier type-name :: ~ type-name |
341 | // |
342 | // Look for the second type-name in the nested-name-specifier. |
343 | CXXScopeSpec PrefixSS; |
344 | PrefixSS.Adopt(Other: NestedNameSpecifierLoc(Prefix, SS.location_data())); |
345 | if (ParsedType T = LookupInNestedNameSpec(PrefixSS)) |
346 | return T; |
347 | } else { |
348 | // This is one of |
349 | // |
350 | // type-name :: ~ type-name |
351 | // ~ type-name |
352 | // |
353 | // Look in the scope and (if any) the object type. |
354 | if (ParsedType T = LookupInScope()) |
355 | return T; |
356 | if (ParsedType T = LookupInObjectType()) |
357 | return T; |
358 | } |
359 | |
360 | if (Failed) |
361 | return nullptr; |
362 | |
363 | if (IsDependent) { |
364 | // We didn't find our type, but that's OK: it's dependent anyway. |
365 | |
366 | // FIXME: What if we have no nested-name-specifier? |
367 | QualType T = |
368 | CheckTypenameType(Keyword: ElaboratedTypeKeyword::None, KeywordLoc: SourceLocation(), |
369 | QualifierLoc: SS.getWithLocInContext(Context), II, IILoc: NameLoc); |
370 | return ParsedType::make(P: T); |
371 | } |
372 | |
373 | // The remaining cases are all non-standard extensions imitating the behavior |
374 | // of various other compilers. |
375 | unsigned NumNonExtensionDecls = FoundDecls.size(); |
376 | |
377 | if (SS.isSet()) { |
378 | // For compatibility with older broken C++ rules and existing code, |
379 | // |
380 | // nested-name-specifier :: ~ type-name |
381 | // |
382 | // also looks for type-name within the nested-name-specifier. |
383 | if (ParsedType T = LookupInNestedNameSpec(SS)) { |
384 | Diag(SS.getEndLoc(), diag::ext_dtor_named_in_wrong_scope) |
385 | << SS.getRange() |
386 | << FixItHint::CreateInsertion(SS.getEndLoc(), |
387 | ("::" + II.getName()).str()); |
388 | return T; |
389 | } |
390 | |
391 | // For compatibility with other compilers and older versions of Clang, |
392 | // |
393 | // nested-name-specifier type-name :: ~ type-name |
394 | // |
395 | // also looks for type-name in the scope. Unfortunately, we can't |
396 | // reasonably apply this fallback for dependent nested-name-specifiers. |
397 | if (SS.isValid() && SS.getScopeRep()->getPrefix()) { |
398 | if (ParsedType T = LookupInScope()) { |
399 | Diag(SS.getEndLoc(), diag::ext_qualified_dtor_named_in_lexical_scope) |
400 | << FixItHint::CreateRemoval(SS.getRange()); |
401 | Diag(FoundDecls.back()->getLocation(), diag::note_destructor_type_here) |
402 | << GetTypeFromParser(T); |
403 | return T; |
404 | } |
405 | } |
406 | } |
407 | |
408 | // We didn't find anything matching; tell the user what we did find (if |
409 | // anything). |
410 | |
411 | // Don't tell the user about declarations we shouldn't have found. |
412 | FoundDecls.resize(N: NumNonExtensionDecls); |
413 | |
414 | // List types before non-types. |
415 | std::stable_sort(first: FoundDecls.begin(), last: FoundDecls.end(), |
416 | comp: [](NamedDecl *A, NamedDecl *B) { |
417 | return isa<TypeDecl>(Val: A->getUnderlyingDecl()) > |
418 | isa<TypeDecl>(Val: B->getUnderlyingDecl()); |
419 | }); |
420 | |
421 | // Suggest a fixit to properly name the destroyed type. |
422 | auto MakeFixItHint = [&]{ |
423 | const CXXRecordDecl *Destroyed = nullptr; |
424 | // FIXME: If we have a scope specifier, suggest its last component? |
425 | if (!SearchType.isNull()) |
426 | Destroyed = SearchType->getAsCXXRecordDecl(); |
427 | else if (S) |
428 | Destroyed = dyn_cast_or_null<CXXRecordDecl>(Val: S->getEntity()); |
429 | if (Destroyed) |
430 | return FixItHint::CreateReplacement(SourceRange(NameLoc), |
431 | Destroyed->getNameAsString()); |
432 | return FixItHint(); |
433 | }; |
434 | |
435 | if (FoundDecls.empty()) { |
436 | // FIXME: Attempt typo-correction? |
437 | Diag(NameLoc, diag::err_undeclared_destructor_name) |
438 | << &II << MakeFixItHint(); |
439 | } else if (!SearchType.isNull() && FoundDecls.size() == 1) { |
440 | if (auto *TD = dyn_cast<TypeDecl>(Val: FoundDecls[0]->getUnderlyingDecl())) { |
441 | assert(!SearchType.isNull() && |
442 | "should only reject a type result if we have a search type" ); |
443 | QualType T = Context.getTypeDeclType(Decl: TD); |
444 | Diag(NameLoc, diag::err_destructor_expr_type_mismatch) |
445 | << T << SearchType << MakeFixItHint(); |
446 | } else { |
447 | Diag(NameLoc, diag::err_destructor_expr_nontype) |
448 | << &II << MakeFixItHint(); |
449 | } |
450 | } else { |
451 | Diag(NameLoc, SearchType.isNull() ? diag::err_destructor_name_nontype |
452 | : diag::err_destructor_expr_mismatch) |
453 | << &II << SearchType << MakeFixItHint(); |
454 | } |
455 | |
456 | for (NamedDecl *FoundD : FoundDecls) { |
457 | if (auto *TD = dyn_cast<TypeDecl>(FoundD->getUnderlyingDecl())) |
458 | Diag(FoundD->getLocation(), diag::note_destructor_type_here) |
459 | << Context.getTypeDeclType(TD); |
460 | else |
461 | Diag(FoundD->getLocation(), diag::note_destructor_nontype_here) |
462 | << FoundD; |
463 | } |
464 | |
465 | return nullptr; |
466 | } |
467 | |
468 | ParsedType Sema::getDestructorTypeForDecltype(const DeclSpec &DS, |
469 | ParsedType ObjectType) { |
470 | if (DS.getTypeSpecType() == DeclSpec::TST_error) |
471 | return nullptr; |
472 | |
473 | if (DS.getTypeSpecType() == DeclSpec::TST_decltype_auto) { |
474 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decltype_auto_invalid); |
475 | return nullptr; |
476 | } |
477 | |
478 | assert(DS.getTypeSpecType() == DeclSpec::TST_decltype && |
479 | "unexpected type in getDestructorType" ); |
480 | QualType T = BuildDecltypeType(E: DS.getRepAsExpr()); |
481 | |
482 | // If we know the type of the object, check that the correct destructor |
483 | // type was named now; we can give better diagnostics this way. |
484 | QualType SearchType = GetTypeFromParser(Ty: ObjectType); |
485 | if (!SearchType.isNull() && !SearchType->isDependentType() && |
486 | !Context.hasSameUnqualifiedType(T1: T, T2: SearchType)) { |
487 | Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch) |
488 | << T << SearchType; |
489 | return nullptr; |
490 | } |
491 | |
492 | return ParsedType::make(P: T); |
493 | } |
494 | |
495 | bool Sema::checkLiteralOperatorId(const CXXScopeSpec &SS, |
496 | const UnqualifiedId &Name, bool IsUDSuffix) { |
497 | assert(Name.getKind() == UnqualifiedIdKind::IK_LiteralOperatorId); |
498 | if (!IsUDSuffix) { |
499 | // [over.literal] p8 |
500 | // |
501 | // double operator""_Bq(long double); // OK: not a reserved identifier |
502 | // double operator"" _Bq(long double); // ill-formed, no diagnostic required |
503 | IdentifierInfo *II = Name.Identifier; |
504 | ReservedIdentifierStatus Status = II->isReserved(LangOpts: PP.getLangOpts()); |
505 | SourceLocation Loc = Name.getEndLoc(); |
506 | if (!PP.getSourceManager().isInSystemHeader(Loc)) { |
507 | if (auto Hint = FixItHint::CreateReplacement( |
508 | RemoveRange: Name.getSourceRange(), |
509 | Code: (StringRef("operator\"\"" ) + II->getName()).str()); |
510 | isReservedInAllContexts(Status)) { |
511 | Diag(Loc, diag::warn_reserved_extern_symbol) |
512 | << II << static_cast<int>(Status) << Hint; |
513 | } else { |
514 | Diag(Loc, diag::warn_deprecated_literal_operator_id) << II << Hint; |
515 | } |
516 | } |
517 | } |
518 | |
519 | if (!SS.isValid()) |
520 | return false; |
521 | |
522 | switch (SS.getScopeRep()->getKind()) { |
523 | case NestedNameSpecifier::Identifier: |
524 | case NestedNameSpecifier::TypeSpec: |
525 | case NestedNameSpecifier::TypeSpecWithTemplate: |
526 | // Per C++11 [over.literal]p2, literal operators can only be declared at |
527 | // namespace scope. Therefore, this unqualified-id cannot name anything. |
528 | // Reject it early, because we have no AST representation for this in the |
529 | // case where the scope is dependent. |
530 | Diag(Name.getBeginLoc(), diag::err_literal_operator_id_outside_namespace) |
531 | << SS.getScopeRep(); |
532 | return true; |
533 | |
534 | case NestedNameSpecifier::Global: |
535 | case NestedNameSpecifier::Super: |
536 | case NestedNameSpecifier::Namespace: |
537 | case NestedNameSpecifier::NamespaceAlias: |
538 | return false; |
539 | } |
540 | |
541 | llvm_unreachable("unknown nested name specifier kind" ); |
542 | } |
543 | |
544 | /// Build a C++ typeid expression with a type operand. |
545 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
546 | SourceLocation TypeidLoc, |
547 | TypeSourceInfo *Operand, |
548 | SourceLocation RParenLoc) { |
549 | // C++ [expr.typeid]p4: |
550 | // The top-level cv-qualifiers of the lvalue expression or the type-id |
551 | // that is the operand of typeid are always ignored. |
552 | // If the type of the type-id is a class type or a reference to a class |
553 | // type, the class shall be completely-defined. |
554 | Qualifiers Quals; |
555 | QualType T |
556 | = Context.getUnqualifiedArrayType(T: Operand->getType().getNonReferenceType(), |
557 | Quals); |
558 | if (T->getAs<RecordType>() && |
559 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
560 | return ExprError(); |
561 | |
562 | if (T->isVariablyModifiedType()) |
563 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) << T); |
564 | |
565 | if (CheckQualifiedFunctionForTypeId(T, Loc: TypeidLoc)) |
566 | return ExprError(); |
567 | |
568 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), Operand, |
569 | SourceRange(TypeidLoc, RParenLoc)); |
570 | } |
571 | |
572 | /// Build a C++ typeid expression with an expression operand. |
573 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
574 | SourceLocation TypeidLoc, |
575 | Expr *E, |
576 | SourceLocation RParenLoc) { |
577 | bool WasEvaluated = false; |
578 | if (E && !E->isTypeDependent()) { |
579 | if (E->hasPlaceholderType()) { |
580 | ExprResult result = CheckPlaceholderExpr(E); |
581 | if (result.isInvalid()) return ExprError(); |
582 | E = result.get(); |
583 | } |
584 | |
585 | QualType T = E->getType(); |
586 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
587 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(Val: RecordT->getDecl()); |
588 | // C++ [expr.typeid]p3: |
589 | // [...] If the type of the expression is a class type, the class |
590 | // shall be completely-defined. |
591 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
592 | return ExprError(); |
593 | |
594 | // C++ [expr.typeid]p3: |
595 | // When typeid is applied to an expression other than an glvalue of a |
596 | // polymorphic class type [...] [the] expression is an unevaluated |
597 | // operand. [...] |
598 | if (RecordD->isPolymorphic() && E->isGLValue()) { |
599 | if (isUnevaluatedContext()) { |
600 | // The operand was processed in unevaluated context, switch the |
601 | // context and recheck the subexpression. |
602 | ExprResult Result = TransformToPotentiallyEvaluated(E); |
603 | if (Result.isInvalid()) |
604 | return ExprError(); |
605 | E = Result.get(); |
606 | } |
607 | |
608 | // We require a vtable to query the type at run time. |
609 | MarkVTableUsed(Loc: TypeidLoc, Class: RecordD); |
610 | WasEvaluated = true; |
611 | } |
612 | } |
613 | |
614 | ExprResult Result = CheckUnevaluatedOperand(E); |
615 | if (Result.isInvalid()) |
616 | return ExprError(); |
617 | E = Result.get(); |
618 | |
619 | // C++ [expr.typeid]p4: |
620 | // [...] If the type of the type-id is a reference to a possibly |
621 | // cv-qualified type, the result of the typeid expression refers to a |
622 | // std::type_info object representing the cv-unqualified referenced |
623 | // type. |
624 | Qualifiers Quals; |
625 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
626 | if (!Context.hasSameType(T1: T, T2: UnqualT)) { |
627 | T = UnqualT; |
628 | E = ImpCastExprToType(E, Type: UnqualT, CK: CK_NoOp, VK: E->getValueKind()).get(); |
629 | } |
630 | } |
631 | |
632 | if (E->getType()->isVariablyModifiedType()) |
633 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) |
634 | << E->getType()); |
635 | else if (!inTemplateInstantiation() && |
636 | E->HasSideEffects(Ctx: Context, IncludePossibleEffects: WasEvaluated)) { |
637 | // The expression operand for typeid is in an unevaluated expression |
638 | // context, so side effects could result in unintended consequences. |
639 | Diag(E->getExprLoc(), WasEvaluated |
640 | ? diag::warn_side_effects_typeid |
641 | : diag::warn_side_effects_unevaluated_context); |
642 | } |
643 | |
644 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), E, |
645 | SourceRange(TypeidLoc, RParenLoc)); |
646 | } |
647 | |
648 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); |
649 | ExprResult |
650 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
651 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
652 | // typeid is not supported in OpenCL. |
653 | if (getLangOpts().OpenCLCPlusPlus) { |
654 | return ExprError(Diag(OpLoc, diag::err_openclcxx_not_supported) |
655 | << "typeid" ); |
656 | } |
657 | |
658 | // Find the std::type_info type. |
659 | if (!getStdNamespace()) |
660 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
661 | |
662 | if (!CXXTypeInfoDecl) { |
663 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get(Name: "type_info" ); |
664 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
665 | LookupQualifiedName(R, getStdNamespace()); |
666 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
667 | // Microsoft's typeinfo doesn't have type_info in std but in the global |
668 | // namespace if _HAS_EXCEPTIONS is defined to 0. See PR13153. |
669 | if (!CXXTypeInfoDecl && LangOpts.MSVCCompat) { |
670 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
671 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
672 | } |
673 | if (!CXXTypeInfoDecl) |
674 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
675 | } |
676 | |
677 | if (!getLangOpts().RTTI) { |
678 | return ExprError(Diag(OpLoc, diag::err_no_typeid_with_fno_rtti)); |
679 | } |
680 | |
681 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
682 | |
683 | if (isType) { |
684 | // The operand is a type; handle it as such. |
685 | TypeSourceInfo *TInfo = nullptr; |
686 | QualType T = GetTypeFromParser(Ty: ParsedType::getFromOpaquePtr(P: TyOrExpr), |
687 | TInfo: &TInfo); |
688 | if (T.isNull()) |
689 | return ExprError(); |
690 | |
691 | if (!TInfo) |
692 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc: OpLoc); |
693 | |
694 | return BuildCXXTypeId(TypeInfoType, TypeidLoc: OpLoc, Operand: TInfo, RParenLoc); |
695 | } |
696 | |
697 | // The operand is an expression. |
698 | ExprResult Result = |
699 | BuildCXXTypeId(TypeInfoType, TypeidLoc: OpLoc, E: (Expr *)TyOrExpr, RParenLoc); |
700 | |
701 | if (!getLangOpts().RTTIData && !Result.isInvalid()) |
702 | if (auto *CTE = dyn_cast<CXXTypeidExpr>(Result.get())) |
703 | if (CTE->isPotentiallyEvaluated() && !CTE->isMostDerived(Context)) |
704 | Diag(OpLoc, diag::warn_no_typeid_with_rtti_disabled) |
705 | << (getDiagnostics().getDiagnosticOptions().getFormat() == |
706 | DiagnosticOptions::MSVC); |
707 | return Result; |
708 | } |
709 | |
710 | /// Grabs __declspec(uuid()) off a type, or returns 0 if we cannot resolve to |
711 | /// a single GUID. |
712 | static void |
713 | getUuidAttrOfType(Sema &SemaRef, QualType QT, |
714 | llvm::SmallSetVector<const UuidAttr *, 1> &UuidAttrs) { |
715 | // Optionally remove one level of pointer, reference or array indirection. |
716 | const Type *Ty = QT.getTypePtr(); |
717 | if (QT->isPointerType() || QT->isReferenceType()) |
718 | Ty = QT->getPointeeType().getTypePtr(); |
719 | else if (QT->isArrayType()) |
720 | Ty = Ty->getBaseElementTypeUnsafe(); |
721 | |
722 | const auto *TD = Ty->getAsTagDecl(); |
723 | if (!TD) |
724 | return; |
725 | |
726 | if (const auto *Uuid = TD->getMostRecentDecl()->getAttr<UuidAttr>()) { |
727 | UuidAttrs.insert(Uuid); |
728 | return; |
729 | } |
730 | |
731 | // __uuidof can grab UUIDs from template arguments. |
732 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: TD)) { |
733 | const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); |
734 | for (const TemplateArgument &TA : TAL.asArray()) { |
735 | const UuidAttr *UuidForTA = nullptr; |
736 | if (TA.getKind() == TemplateArgument::Type) |
737 | getUuidAttrOfType(SemaRef, TA.getAsType(), UuidAttrs); |
738 | else if (TA.getKind() == TemplateArgument::Declaration) |
739 | getUuidAttrOfType(SemaRef, TA.getAsDecl()->getType(), UuidAttrs); |
740 | |
741 | if (UuidForTA) |
742 | UuidAttrs.insert(UuidForTA); |
743 | } |
744 | } |
745 | } |
746 | |
747 | /// Build a Microsoft __uuidof expression with a type operand. |
748 | ExprResult Sema::BuildCXXUuidof(QualType Type, |
749 | SourceLocation TypeidLoc, |
750 | TypeSourceInfo *Operand, |
751 | SourceLocation RParenLoc) { |
752 | MSGuidDecl *Guid = nullptr; |
753 | if (!Operand->getType()->isDependentType()) { |
754 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
755 | getUuidAttrOfType(*this, Operand->getType(), UuidAttrs); |
756 | if (UuidAttrs.empty()) |
757 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
758 | if (UuidAttrs.size() > 1) |
759 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
760 | Guid = UuidAttrs.back()->getGuidDecl(); |
761 | } |
762 | |
763 | return new (Context) |
764 | CXXUuidofExpr(Type, Operand, Guid, SourceRange(TypeidLoc, RParenLoc)); |
765 | } |
766 | |
767 | /// Build a Microsoft __uuidof expression with an expression operand. |
768 | ExprResult Sema::BuildCXXUuidof(QualType Type, SourceLocation TypeidLoc, |
769 | Expr *E, SourceLocation RParenLoc) { |
770 | MSGuidDecl *Guid = nullptr; |
771 | if (!E->getType()->isDependentType()) { |
772 | if (E->isNullPointerConstant(Ctx&: Context, NPC: Expr::NPC_ValueDependentIsNull)) { |
773 | // A null pointer results in {00000000-0000-0000-0000-000000000000}. |
774 | Guid = Context.getMSGuidDecl(Parts: MSGuidDecl::Parts{}); |
775 | } else { |
776 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
777 | getUuidAttrOfType(*this, E->getType(), UuidAttrs); |
778 | if (UuidAttrs.empty()) |
779 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
780 | if (UuidAttrs.size() > 1) |
781 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
782 | Guid = UuidAttrs.back()->getGuidDecl(); |
783 | } |
784 | } |
785 | |
786 | return new (Context) |
787 | CXXUuidofExpr(Type, E, Guid, SourceRange(TypeidLoc, RParenLoc)); |
788 | } |
789 | |
790 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
791 | ExprResult |
792 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
793 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
794 | QualType GuidType = Context.getMSGuidType(); |
795 | GuidType.addConst(); |
796 | |
797 | if (isType) { |
798 | // The operand is a type; handle it as such. |
799 | TypeSourceInfo *TInfo = nullptr; |
800 | QualType T = GetTypeFromParser(Ty: ParsedType::getFromOpaquePtr(P: TyOrExpr), |
801 | TInfo: &TInfo); |
802 | if (T.isNull()) |
803 | return ExprError(); |
804 | |
805 | if (!TInfo) |
806 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc: OpLoc); |
807 | |
808 | return BuildCXXUuidof(Type: GuidType, TypeidLoc: OpLoc, Operand: TInfo, RParenLoc); |
809 | } |
810 | |
811 | // The operand is an expression. |
812 | return BuildCXXUuidof(Type: GuidType, TypeidLoc: OpLoc, E: (Expr*)TyOrExpr, RParenLoc); |
813 | } |
814 | |
815 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
816 | ExprResult |
817 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
818 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
819 | "Unknown C++ Boolean value!" ); |
820 | return new (Context) |
821 | CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
822 | } |
823 | |
824 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
825 | ExprResult |
826 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
827 | return new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
828 | } |
829 | |
830 | /// ActOnCXXThrow - Parse throw expressions. |
831 | ExprResult |
832 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { |
833 | bool IsThrownVarInScope = false; |
834 | if (Ex) { |
835 | // C++0x [class.copymove]p31: |
836 | // When certain criteria are met, an implementation is allowed to omit the |
837 | // copy/move construction of a class object [...] |
838 | // |
839 | // - in a throw-expression, when the operand is the name of a |
840 | // non-volatile automatic object (other than a function or catch- |
841 | // clause parameter) whose scope does not extend beyond the end of the |
842 | // innermost enclosing try-block (if there is one), the copy/move |
843 | // operation from the operand to the exception object (15.1) can be |
844 | // omitted by constructing the automatic object directly into the |
845 | // exception object |
846 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: Ex->IgnoreParens())) |
847 | if (const auto *Var = dyn_cast<VarDecl>(Val: DRE->getDecl()); |
848 | Var && Var->hasLocalStorage() && |
849 | !Var->getType().isVolatileQualified()) { |
850 | for (; S; S = S->getParent()) { |
851 | if (S->isDeclScope(Var)) { |
852 | IsThrownVarInScope = true; |
853 | break; |
854 | } |
855 | |
856 | // FIXME: Many of the scope checks here seem incorrect. |
857 | if (S->getFlags() & |
858 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | |
859 | Scope::ObjCMethodScope | Scope::TryScope)) |
860 | break; |
861 | } |
862 | } |
863 | } |
864 | |
865 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); |
866 | } |
867 | |
868 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, |
869 | bool IsThrownVarInScope) { |
870 | const llvm::Triple &T = Context.getTargetInfo().getTriple(); |
871 | const bool IsOpenMPGPUTarget = |
872 | getLangOpts().OpenMPIsTargetDevice && (T.isNVPTX() || T.isAMDGCN()); |
873 | // Don't report an error if 'throw' is used in system headers or in an OpenMP |
874 | // target region compiled for a GPU architecture. |
875 | if (!IsOpenMPGPUTarget && !getLangOpts().CXXExceptions && |
876 | !getSourceManager().isInSystemHeader(Loc: OpLoc) && !getLangOpts().CUDA) { |
877 | // Delay error emission for the OpenMP device code. |
878 | targetDiag(OpLoc, diag::err_exceptions_disabled) << "throw" ; |
879 | } |
880 | |
881 | // In OpenMP target regions, we replace 'throw' with a trap on GPU targets. |
882 | if (IsOpenMPGPUTarget) |
883 | targetDiag(OpLoc, diag::warn_throw_not_valid_on_target) << T.str(); |
884 | |
885 | // Exceptions aren't allowed in CUDA device code. |
886 | if (getLangOpts().CUDA) |
887 | CUDADiagIfDeviceCode(OpLoc, diag::err_cuda_device_exceptions) |
888 | << "throw" << CurrentCUDATarget(); |
889 | |
890 | if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope()) |
891 | Diag(OpLoc, diag::err_omp_simd_region_cannot_use_stmt) << "throw" ; |
892 | |
893 | if (Ex && !Ex->isTypeDependent()) { |
894 | // Initialize the exception result. This implicitly weeds out |
895 | // abstract types or types with inaccessible copy constructors. |
896 | |
897 | // C++0x [class.copymove]p31: |
898 | // When certain criteria are met, an implementation is allowed to omit the |
899 | // copy/move construction of a class object [...] |
900 | // |
901 | // - in a throw-expression, when the operand is the name of a |
902 | // non-volatile automatic object (other than a function or |
903 | // catch-clause |
904 | // parameter) whose scope does not extend beyond the end of the |
905 | // innermost enclosing try-block (if there is one), the copy/move |
906 | // operation from the operand to the exception object (15.1) can be |
907 | // omitted by constructing the automatic object directly into the |
908 | // exception object |
909 | NamedReturnInfo NRInfo = |
910 | IsThrownVarInScope ? getNamedReturnInfo(E&: Ex) : NamedReturnInfo(); |
911 | |
912 | QualType ExceptionObjectTy = Context.getExceptionObjectType(T: Ex->getType()); |
913 | if (CheckCXXThrowOperand(ThrowLoc: OpLoc, ThrowTy: ExceptionObjectTy, E: Ex)) |
914 | return ExprError(); |
915 | |
916 | InitializedEntity Entity = |
917 | InitializedEntity::InitializeException(ThrowLoc: OpLoc, Type: ExceptionObjectTy); |
918 | ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRInfo, Value: Ex); |
919 | if (Res.isInvalid()) |
920 | return ExprError(); |
921 | Ex = Res.get(); |
922 | } |
923 | |
924 | // PPC MMA non-pointer types are not allowed as throw expr types. |
925 | if (Ex && Context.getTargetInfo().getTriple().isPPC64()) |
926 | CheckPPCMMAType(Type: Ex->getType(), TypeLoc: Ex->getBeginLoc()); |
927 | |
928 | return new (Context) |
929 | CXXThrowExpr(Ex, Context.VoidTy, OpLoc, IsThrownVarInScope); |
930 | } |
931 | |
932 | static void |
933 | collectPublicBases(CXXRecordDecl *RD, |
934 | llvm::DenseMap<CXXRecordDecl *, unsigned> &SubobjectsSeen, |
935 | llvm::SmallPtrSetImpl<CXXRecordDecl *> &VBases, |
936 | llvm::SetVector<CXXRecordDecl *> &PublicSubobjectsSeen, |
937 | bool ParentIsPublic) { |
938 | for (const CXXBaseSpecifier &BS : RD->bases()) { |
939 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
940 | bool NewSubobject; |
941 | // Virtual bases constitute the same subobject. Non-virtual bases are |
942 | // always distinct subobjects. |
943 | if (BS.isVirtual()) |
944 | NewSubobject = VBases.insert(Ptr: BaseDecl).second; |
945 | else |
946 | NewSubobject = true; |
947 | |
948 | if (NewSubobject) |
949 | ++SubobjectsSeen[BaseDecl]; |
950 | |
951 | // Only add subobjects which have public access throughout the entire chain. |
952 | bool PublicPath = ParentIsPublic && BS.getAccessSpecifier() == AS_public; |
953 | if (PublicPath) |
954 | PublicSubobjectsSeen.insert(X: BaseDecl); |
955 | |
956 | // Recurse on to each base subobject. |
957 | collectPublicBases(RD: BaseDecl, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
958 | ParentIsPublic: PublicPath); |
959 | } |
960 | } |
961 | |
962 | static void getUnambiguousPublicSubobjects( |
963 | CXXRecordDecl *RD, llvm::SmallVectorImpl<CXXRecordDecl *> &Objects) { |
964 | llvm::DenseMap<CXXRecordDecl *, unsigned> SubobjectsSeen; |
965 | llvm::SmallSet<CXXRecordDecl *, 2> VBases; |
966 | llvm::SetVector<CXXRecordDecl *> PublicSubobjectsSeen; |
967 | SubobjectsSeen[RD] = 1; |
968 | PublicSubobjectsSeen.insert(X: RD); |
969 | collectPublicBases(RD, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
970 | /*ParentIsPublic=*/true); |
971 | |
972 | for (CXXRecordDecl *PublicSubobject : PublicSubobjectsSeen) { |
973 | // Skip ambiguous objects. |
974 | if (SubobjectsSeen[PublicSubobject] > 1) |
975 | continue; |
976 | |
977 | Objects.push_back(Elt: PublicSubobject); |
978 | } |
979 | } |
980 | |
981 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
982 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, |
983 | QualType ExceptionObjectTy, Expr *E) { |
984 | // If the type of the exception would be an incomplete type or a pointer |
985 | // to an incomplete type other than (cv) void the program is ill-formed. |
986 | QualType Ty = ExceptionObjectTy; |
987 | bool isPointer = false; |
988 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
989 | Ty = Ptr->getPointeeType(); |
990 | isPointer = true; |
991 | } |
992 | |
993 | // Cannot throw WebAssembly reference type. |
994 | if (Ty.isWebAssemblyReferenceType()) { |
995 | Diag(ThrowLoc, diag::err_wasm_reftype_tc) << 0 << E->getSourceRange(); |
996 | return true; |
997 | } |
998 | |
999 | // Cannot throw WebAssembly table. |
1000 | if (isPointer && Ty.isWebAssemblyReferenceType()) { |
1001 | Diag(ThrowLoc, diag::err_wasm_table_art) << 2 << E->getSourceRange(); |
1002 | return true; |
1003 | } |
1004 | |
1005 | if (!isPointer || !Ty->isVoidType()) { |
1006 | if (RequireCompleteType(ThrowLoc, Ty, |
1007 | isPointer ? diag::err_throw_incomplete_ptr |
1008 | : diag::err_throw_incomplete, |
1009 | E->getSourceRange())) |
1010 | return true; |
1011 | |
1012 | if (!isPointer && Ty->isSizelessType()) { |
1013 | Diag(ThrowLoc, diag::err_throw_sizeless) << Ty << E->getSourceRange(); |
1014 | return true; |
1015 | } |
1016 | |
1017 | if (RequireNonAbstractType(ThrowLoc, ExceptionObjectTy, |
1018 | diag::err_throw_abstract_type, E)) |
1019 | return true; |
1020 | } |
1021 | |
1022 | // If the exception has class type, we need additional handling. |
1023 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
1024 | if (!RD) |
1025 | return false; |
1026 | |
1027 | // If we are throwing a polymorphic class type or pointer thereof, |
1028 | // exception handling will make use of the vtable. |
1029 | MarkVTableUsed(Loc: ThrowLoc, Class: RD); |
1030 | |
1031 | // If a pointer is thrown, the referenced object will not be destroyed. |
1032 | if (isPointer) |
1033 | return false; |
1034 | |
1035 | // If the class has a destructor, we must be able to call it. |
1036 | if (!RD->hasIrrelevantDestructor()) { |
1037 | if (CXXDestructorDecl *Destructor = LookupDestructor(Class: RD)) { |
1038 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
1039 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
1040 | PDiag(diag::err_access_dtor_exception) << Ty); |
1041 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
1042 | return true; |
1043 | } |
1044 | } |
1045 | |
1046 | // The MSVC ABI creates a list of all types which can catch the exception |
1047 | // object. This list also references the appropriate copy constructor to call |
1048 | // if the object is caught by value and has a non-trivial copy constructor. |
1049 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
1050 | // We are only interested in the public, unambiguous bases contained within |
1051 | // the exception object. Bases which are ambiguous or otherwise |
1052 | // inaccessible are not catchable types. |
1053 | llvm::SmallVector<CXXRecordDecl *, 2> UnambiguousPublicSubobjects; |
1054 | getUnambiguousPublicSubobjects(RD, Objects&: UnambiguousPublicSubobjects); |
1055 | |
1056 | for (CXXRecordDecl *Subobject : UnambiguousPublicSubobjects) { |
1057 | // Attempt to lookup the copy constructor. Various pieces of machinery |
1058 | // will spring into action, like template instantiation, which means this |
1059 | // cannot be a simple walk of the class's decls. Instead, we must perform |
1060 | // lookup and overload resolution. |
1061 | CXXConstructorDecl *CD = LookupCopyingConstructor(Class: Subobject, Quals: 0); |
1062 | if (!CD || CD->isDeleted()) |
1063 | continue; |
1064 | |
1065 | // Mark the constructor referenced as it is used by this throw expression. |
1066 | MarkFunctionReferenced(E->getExprLoc(), CD); |
1067 | |
1068 | // Skip this copy constructor if it is trivial, we don't need to record it |
1069 | // in the catchable type data. |
1070 | if (CD->isTrivial()) |
1071 | continue; |
1072 | |
1073 | // The copy constructor is non-trivial, create a mapping from this class |
1074 | // type to this constructor. |
1075 | // N.B. The selection of copy constructor is not sensitive to this |
1076 | // particular throw-site. Lookup will be performed at the catch-site to |
1077 | // ensure that the copy constructor is, in fact, accessible (via |
1078 | // friendship or any other means). |
1079 | Context.addCopyConstructorForExceptionObject(RD: Subobject, CD); |
1080 | |
1081 | // We don't keep the instantiated default argument expressions around so |
1082 | // we must rebuild them here. |
1083 | for (unsigned I = 1, E = CD->getNumParams(); I != E; ++I) { |
1084 | if (CheckCXXDefaultArgExpr(CallLoc: ThrowLoc, FD: CD, Param: CD->getParamDecl(I))) |
1085 | return true; |
1086 | } |
1087 | } |
1088 | } |
1089 | |
1090 | // Under the Itanium C++ ABI, memory for the exception object is allocated by |
1091 | // the runtime with no ability for the compiler to request additional |
1092 | // alignment. Warn if the exception type requires alignment beyond the minimum |
1093 | // guaranteed by the target C++ runtime. |
1094 | if (Context.getTargetInfo().getCXXABI().isItaniumFamily()) { |
1095 | CharUnits TypeAlign = Context.getTypeAlignInChars(T: Ty); |
1096 | CharUnits ExnObjAlign = Context.getExnObjectAlignment(); |
1097 | if (ExnObjAlign < TypeAlign) { |
1098 | Diag(ThrowLoc, diag::warn_throw_underaligned_obj); |
1099 | Diag(ThrowLoc, diag::note_throw_underaligned_obj) |
1100 | << Ty << (unsigned)TypeAlign.getQuantity() |
1101 | << (unsigned)ExnObjAlign.getQuantity(); |
1102 | } |
1103 | } |
1104 | if (!isPointer && getLangOpts().AssumeNothrowExceptionDtor) { |
1105 | if (CXXDestructorDecl *Dtor = RD->getDestructor()) { |
1106 | auto Ty = Dtor->getType(); |
1107 | if (auto *FT = Ty.getTypePtr()->getAs<FunctionProtoType>()) { |
1108 | if (!isUnresolvedExceptionSpec(FT->getExceptionSpecType()) && |
1109 | !FT->isNothrow()) |
1110 | Diag(ThrowLoc, diag::err_throw_object_throwing_dtor) << RD; |
1111 | } |
1112 | } |
1113 | } |
1114 | |
1115 | return false; |
1116 | } |
1117 | |
1118 | static QualType adjustCVQualifiersForCXXThisWithinLambda( |
1119 | ArrayRef<FunctionScopeInfo *> FunctionScopes, QualType ThisTy, |
1120 | DeclContext *CurSemaContext, ASTContext &ASTCtx) { |
1121 | |
1122 | QualType ClassType = ThisTy->getPointeeType(); |
1123 | LambdaScopeInfo *CurLSI = nullptr; |
1124 | DeclContext *CurDC = CurSemaContext; |
1125 | |
1126 | // Iterate through the stack of lambdas starting from the innermost lambda to |
1127 | // the outermost lambda, checking if '*this' is ever captured by copy - since |
1128 | // that could change the cv-qualifiers of the '*this' object. |
1129 | // The object referred to by '*this' starts out with the cv-qualifiers of its |
1130 | // member function. We then start with the innermost lambda and iterate |
1131 | // outward checking to see if any lambda performs a by-copy capture of '*this' |
1132 | // - and if so, any nested lambda must respect the 'constness' of that |
1133 | // capturing lamdbda's call operator. |
1134 | // |
1135 | |
1136 | // Since the FunctionScopeInfo stack is representative of the lexical |
1137 | // nesting of the lambda expressions during initial parsing (and is the best |
1138 | // place for querying information about captures about lambdas that are |
1139 | // partially processed) and perhaps during instantiation of function templates |
1140 | // that contain lambda expressions that need to be transformed BUT not |
1141 | // necessarily during instantiation of a nested generic lambda's function call |
1142 | // operator (which might even be instantiated at the end of the TU) - at which |
1143 | // time the DeclContext tree is mature enough to query capture information |
1144 | // reliably - we use a two pronged approach to walk through all the lexically |
1145 | // enclosing lambda expressions: |
1146 | // |
1147 | // 1) Climb down the FunctionScopeInfo stack as long as each item represents |
1148 | // a Lambda (i.e. LambdaScopeInfo) AND each LSI's 'closure-type' is lexically |
1149 | // enclosed by the call-operator of the LSI below it on the stack (while |
1150 | // tracking the enclosing DC for step 2 if needed). Note the topmost LSI on |
1151 | // the stack represents the innermost lambda. |
1152 | // |
1153 | // 2) If we run out of enclosing LSI's, check if the enclosing DeclContext |
1154 | // represents a lambda's call operator. If it does, we must be instantiating |
1155 | // a generic lambda's call operator (represented by the Current LSI, and |
1156 | // should be the only scenario where an inconsistency between the LSI and the |
1157 | // DeclContext should occur), so climb out the DeclContexts if they |
1158 | // represent lambdas, while querying the corresponding closure types |
1159 | // regarding capture information. |
1160 | |
1161 | // 1) Climb down the function scope info stack. |
1162 | for (int I = FunctionScopes.size(); |
1163 | I-- && isa<LambdaScopeInfo>(Val: FunctionScopes[I]) && |
1164 | (!CurLSI || !CurLSI->Lambda || CurLSI->Lambda->getDeclContext() == |
1165 | cast<LambdaScopeInfo>(Val: FunctionScopes[I])->CallOperator); |
1166 | CurDC = getLambdaAwareParentOfDeclContext(DC: CurDC)) { |
1167 | CurLSI = cast<LambdaScopeInfo>(Val: FunctionScopes[I]); |
1168 | |
1169 | if (!CurLSI->isCXXThisCaptured()) |
1170 | continue; |
1171 | |
1172 | auto C = CurLSI->getCXXThisCapture(); |
1173 | |
1174 | if (C.isCopyCapture()) { |
1175 | if (CurLSI->lambdaCaptureShouldBeConst()) |
1176 | ClassType.addConst(); |
1177 | return ASTCtx.getPointerType(T: ClassType); |
1178 | } |
1179 | } |
1180 | |
1181 | // 2) We've run out of ScopeInfos but check 1. if CurDC is a lambda (which |
1182 | // can happen during instantiation of its nested generic lambda call |
1183 | // operator); 2. if we're in a lambda scope (lambda body). |
1184 | if (CurLSI && isLambdaCallOperator(DC: CurDC)) { |
1185 | assert(isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator) && |
1186 | "While computing 'this' capture-type for a generic lambda, when we " |
1187 | "run out of enclosing LSI's, yet the enclosing DC is a " |
1188 | "lambda-call-operator we must be (i.e. Current LSI) in a generic " |
1189 | "lambda call oeprator" ); |
1190 | assert(CurDC == getLambdaAwareParentOfDeclContext(CurLSI->CallOperator)); |
1191 | |
1192 | auto IsThisCaptured = |
1193 | [](CXXRecordDecl *Closure, bool &IsByCopy, bool &IsConst) { |
1194 | IsConst = false; |
1195 | IsByCopy = false; |
1196 | for (auto &&C : Closure->captures()) { |
1197 | if (C.capturesThis()) { |
1198 | if (C.getCaptureKind() == LCK_StarThis) |
1199 | IsByCopy = true; |
1200 | if (Closure->getLambdaCallOperator()->isConst()) |
1201 | IsConst = true; |
1202 | return true; |
1203 | } |
1204 | } |
1205 | return false; |
1206 | }; |
1207 | |
1208 | bool IsByCopyCapture = false; |
1209 | bool IsConstCapture = false; |
1210 | CXXRecordDecl *Closure = cast<CXXRecordDecl>(Val: CurDC->getParent()); |
1211 | while (Closure && |
1212 | IsThisCaptured(Closure, IsByCopyCapture, IsConstCapture)) { |
1213 | if (IsByCopyCapture) { |
1214 | if (IsConstCapture) |
1215 | ClassType.addConst(); |
1216 | return ASTCtx.getPointerType(T: ClassType); |
1217 | } |
1218 | Closure = isLambdaCallOperator(Closure->getParent()) |
1219 | ? cast<CXXRecordDecl>(Closure->getParent()->getParent()) |
1220 | : nullptr; |
1221 | } |
1222 | } |
1223 | return ASTCtx.getPointerType(T: ClassType); |
1224 | } |
1225 | |
1226 | QualType Sema::getCurrentThisType() { |
1227 | DeclContext *DC = getFunctionLevelDeclContext(); |
1228 | QualType ThisTy = CXXThisTypeOverride; |
1229 | |
1230 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(Val: DC)) { |
1231 | if (method && method->isImplicitObjectMemberFunction()) |
1232 | ThisTy = method->getThisType().getNonReferenceType(); |
1233 | } |
1234 | |
1235 | if (ThisTy.isNull() && isLambdaCallWithImplicitObjectParameter(DC: CurContext) && |
1236 | inTemplateInstantiation() && isa<CXXRecordDecl>(Val: DC)) { |
1237 | |
1238 | // This is a lambda call operator that is being instantiated as a default |
1239 | // initializer. DC must point to the enclosing class type, so we can recover |
1240 | // the 'this' type from it. |
1241 | QualType ClassTy = Context.getTypeDeclType(cast<CXXRecordDecl>(Val: DC)); |
1242 | // There are no cv-qualifiers for 'this' within default initializers, |
1243 | // per [expr.prim.general]p4. |
1244 | ThisTy = Context.getPointerType(T: ClassTy); |
1245 | } |
1246 | |
1247 | // If we are within a lambda's call operator, the cv-qualifiers of 'this' |
1248 | // might need to be adjusted if the lambda or any of its enclosing lambda's |
1249 | // captures '*this' by copy. |
1250 | if (!ThisTy.isNull() && isLambdaCallOperator(DC: CurContext)) |
1251 | return adjustCVQualifiersForCXXThisWithinLambda(FunctionScopes, ThisTy, |
1252 | CurSemaContext: CurContext, ASTCtx&: Context); |
1253 | return ThisTy; |
1254 | } |
1255 | |
1256 | Sema::CXXThisScopeRAII::CXXThisScopeRAII(Sema &S, |
1257 | Decl *ContextDecl, |
1258 | Qualifiers CXXThisTypeQuals, |
1259 | bool Enabled) |
1260 | : S(S), OldCXXThisTypeOverride(S.CXXThisTypeOverride), Enabled(false) |
1261 | { |
1262 | if (!Enabled || !ContextDecl) |
1263 | return; |
1264 | |
1265 | CXXRecordDecl *Record = nullptr; |
1266 | if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(Val: ContextDecl)) |
1267 | Record = Template->getTemplatedDecl(); |
1268 | else |
1269 | Record = cast<CXXRecordDecl>(Val: ContextDecl); |
1270 | |
1271 | QualType T = S.Context.getRecordType(Record); |
1272 | T = S.getASTContext().getQualifiedType(T, Qs: CXXThisTypeQuals); |
1273 | |
1274 | S.CXXThisTypeOverride = |
1275 | S.Context.getLangOpts().HLSL ? T : S.Context.getPointerType(T); |
1276 | |
1277 | this->Enabled = true; |
1278 | } |
1279 | |
1280 | |
1281 | Sema::CXXThisScopeRAII::~CXXThisScopeRAII() { |
1282 | if (Enabled) { |
1283 | S.CXXThisTypeOverride = OldCXXThisTypeOverride; |
1284 | } |
1285 | } |
1286 | |
1287 | static void buildLambdaThisCaptureFixit(Sema &Sema, LambdaScopeInfo *LSI) { |
1288 | SourceLocation DiagLoc = LSI->IntroducerRange.getEnd(); |
1289 | assert(!LSI->isCXXThisCaptured()); |
1290 | // [=, this] {}; // until C++20: Error: this when = is the default |
1291 | if (LSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByval && |
1292 | !Sema.getLangOpts().CPlusPlus20) |
1293 | return; |
1294 | Sema.Diag(DiagLoc, diag::note_lambda_this_capture_fixit) |
1295 | << FixItHint::CreateInsertion( |
1296 | DiagLoc, LSI->NumExplicitCaptures > 0 ? ", this" : "this" ); |
1297 | } |
1298 | |
1299 | bool Sema::CheckCXXThisCapture(SourceLocation Loc, const bool Explicit, |
1300 | bool BuildAndDiagnose, const unsigned *const FunctionScopeIndexToStopAt, |
1301 | const bool ByCopy) { |
1302 | // We don't need to capture this in an unevaluated context. |
1303 | if (isUnevaluatedContext() && !Explicit) |
1304 | return true; |
1305 | |
1306 | assert((!ByCopy || Explicit) && "cannot implicitly capture *this by value" ); |
1307 | |
1308 | const int MaxFunctionScopesIndex = FunctionScopeIndexToStopAt |
1309 | ? *FunctionScopeIndexToStopAt |
1310 | : FunctionScopes.size() - 1; |
1311 | |
1312 | // Check that we can capture the *enclosing object* (referred to by '*this') |
1313 | // by the capturing-entity/closure (lambda/block/etc) at |
1314 | // MaxFunctionScopesIndex-deep on the FunctionScopes stack. |
1315 | |
1316 | // Note: The *enclosing object* can only be captured by-value by a |
1317 | // closure that is a lambda, using the explicit notation: |
1318 | // [*this] { ... }. |
1319 | // Every other capture of the *enclosing object* results in its by-reference |
1320 | // capture. |
1321 | |
1322 | // For a closure 'L' (at MaxFunctionScopesIndex in the FunctionScopes |
1323 | // stack), we can capture the *enclosing object* only if: |
1324 | // - 'L' has an explicit byref or byval capture of the *enclosing object* |
1325 | // - or, 'L' has an implicit capture. |
1326 | // AND |
1327 | // -- there is no enclosing closure |
1328 | // -- or, there is some enclosing closure 'E' that has already captured the |
1329 | // *enclosing object*, and every intervening closure (if any) between 'E' |
1330 | // and 'L' can implicitly capture the *enclosing object*. |
1331 | // -- or, every enclosing closure can implicitly capture the |
1332 | // *enclosing object* |
1333 | |
1334 | |
1335 | unsigned NumCapturingClosures = 0; |
1336 | for (int idx = MaxFunctionScopesIndex; idx >= 0; idx--) { |
1337 | if (CapturingScopeInfo *CSI = |
1338 | dyn_cast<CapturingScopeInfo>(Val: FunctionScopes[idx])) { |
1339 | if (CSI->CXXThisCaptureIndex != 0) { |
1340 | // 'this' is already being captured; there isn't anything more to do. |
1341 | CSI->Captures[CSI->CXXThisCaptureIndex - 1].markUsed(IsODRUse: BuildAndDiagnose); |
1342 | break; |
1343 | } |
1344 | LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(Val: CSI); |
1345 | if (LSI && isGenericLambdaCallOperatorSpecialization(MD: LSI->CallOperator)) { |
1346 | // This context can't implicitly capture 'this'; fail out. |
1347 | if (BuildAndDiagnose) { |
1348 | LSI->CallOperator->setInvalidDecl(); |
1349 | Diag(Loc, diag::err_this_capture) |
1350 | << (Explicit && idx == MaxFunctionScopesIndex); |
1351 | if (!Explicit) |
1352 | buildLambdaThisCaptureFixit(Sema&: *this, LSI); |
1353 | } |
1354 | return true; |
1355 | } |
1356 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByref || |
1357 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByval || |
1358 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_Block || |
1359 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_CapturedRegion || |
1360 | (Explicit && idx == MaxFunctionScopesIndex)) { |
1361 | // Regarding (Explicit && idx == MaxFunctionScopesIndex): only the first |
1362 | // iteration through can be an explicit capture, all enclosing closures, |
1363 | // if any, must perform implicit captures. |
1364 | |
1365 | // This closure can capture 'this'; continue looking upwards. |
1366 | NumCapturingClosures++; |
1367 | continue; |
1368 | } |
1369 | // This context can't implicitly capture 'this'; fail out. |
1370 | if (BuildAndDiagnose) { |
1371 | LSI->CallOperator->setInvalidDecl(); |
1372 | Diag(Loc, diag::err_this_capture) |
1373 | << (Explicit && idx == MaxFunctionScopesIndex); |
1374 | } |
1375 | if (!Explicit) |
1376 | buildLambdaThisCaptureFixit(Sema&: *this, LSI); |
1377 | return true; |
1378 | } |
1379 | break; |
1380 | } |
1381 | if (!BuildAndDiagnose) return false; |
1382 | |
1383 | // If we got here, then the closure at MaxFunctionScopesIndex on the |
1384 | // FunctionScopes stack, can capture the *enclosing object*, so capture it |
1385 | // (including implicit by-reference captures in any enclosing closures). |
1386 | |
1387 | // In the loop below, respect the ByCopy flag only for the closure requesting |
1388 | // the capture (i.e. first iteration through the loop below). Ignore it for |
1389 | // all enclosing closure's up to NumCapturingClosures (since they must be |
1390 | // implicitly capturing the *enclosing object* by reference (see loop |
1391 | // above)). |
1392 | assert((!ByCopy || |
1393 | isa<LambdaScopeInfo>(FunctionScopes[MaxFunctionScopesIndex])) && |
1394 | "Only a lambda can capture the enclosing object (referred to by " |
1395 | "*this) by copy" ); |
1396 | QualType ThisTy = getCurrentThisType(); |
1397 | for (int idx = MaxFunctionScopesIndex; NumCapturingClosures; |
1398 | --idx, --NumCapturingClosures) { |
1399 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(Val: FunctionScopes[idx]); |
1400 | |
1401 | // The type of the corresponding data member (not a 'this' pointer if 'by |
1402 | // copy'). |
1403 | QualType CaptureType = ByCopy ? ThisTy->getPointeeType() : ThisTy; |
1404 | |
1405 | bool isNested = NumCapturingClosures > 1; |
1406 | CSI->addThisCapture(isNested, Loc, CaptureType, ByCopy); |
1407 | } |
1408 | return false; |
1409 | } |
1410 | |
1411 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { |
1412 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
1413 | /// is a non-lvalue expression whose value is the address of the object for |
1414 | /// which the function is called. |
1415 | QualType ThisTy = getCurrentThisType(); |
1416 | |
1417 | if (ThisTy.isNull()) { |
1418 | DeclContext *DC = getFunctionLevelDeclContext(); |
1419 | |
1420 | if (const auto *Method = dyn_cast<CXXMethodDecl>(Val: DC); |
1421 | Method && Method->isExplicitObjectMemberFunction()) { |
1422 | return Diag(Loc, diag::err_invalid_this_use) << 1; |
1423 | } |
1424 | |
1425 | if (isLambdaCallWithExplicitObjectParameter(CurContext)) |
1426 | return Diag(Loc, diag::err_invalid_this_use) << 1; |
1427 | |
1428 | return Diag(Loc, diag::err_invalid_this_use) << 0; |
1429 | } |
1430 | |
1431 | return BuildCXXThisExpr(Loc, Type: ThisTy, /*IsImplicit=*/false); |
1432 | } |
1433 | |
1434 | Expr *Sema::BuildCXXThisExpr(SourceLocation Loc, QualType Type, |
1435 | bool IsImplicit) { |
1436 | auto *This = CXXThisExpr::Create(Ctx: Context, L: Loc, Ty: Type, IsImplicit); |
1437 | MarkThisReferenced(This); |
1438 | return This; |
1439 | } |
1440 | |
1441 | void Sema::MarkThisReferenced(CXXThisExpr *This) { |
1442 | CheckCXXThisCapture(Loc: This->getExprLoc()); |
1443 | } |
1444 | |
1445 | bool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) { |
1446 | // If we're outside the body of a member function, then we'll have a specified |
1447 | // type for 'this'. |
1448 | if (CXXThisTypeOverride.isNull()) |
1449 | return false; |
1450 | |
1451 | // Determine whether we're looking into a class that's currently being |
1452 | // defined. |
1453 | CXXRecordDecl *Class = BaseType->getAsCXXRecordDecl(); |
1454 | return Class && Class->isBeingDefined(); |
1455 | } |
1456 | |
1457 | /// Parse construction of a specified type. |
1458 | /// Can be interpreted either as function-style casting ("int(x)") |
1459 | /// or class type construction ("ClassType(x,y,z)") |
1460 | /// or creation of a value-initialized type ("int()"). |
1461 | ExprResult |
1462 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
1463 | SourceLocation LParenOrBraceLoc, |
1464 | MultiExprArg exprs, |
1465 | SourceLocation RParenOrBraceLoc, |
1466 | bool ListInitialization) { |
1467 | if (!TypeRep) |
1468 | return ExprError(); |
1469 | |
1470 | TypeSourceInfo *TInfo; |
1471 | QualType Ty = GetTypeFromParser(Ty: TypeRep, TInfo: &TInfo); |
1472 | if (!TInfo) |
1473 | TInfo = Context.getTrivialTypeSourceInfo(T: Ty, Loc: SourceLocation()); |
1474 | |
1475 | auto Result = BuildCXXTypeConstructExpr(Type: TInfo, LParenLoc: LParenOrBraceLoc, Exprs: exprs, |
1476 | RParenLoc: RParenOrBraceLoc, ListInitialization); |
1477 | // Avoid creating a non-type-dependent expression that contains typos. |
1478 | // Non-type-dependent expressions are liable to be discarded without |
1479 | // checking for embedded typos. |
1480 | if (!Result.isInvalid() && Result.get()->isInstantiationDependent() && |
1481 | !Result.get()->isTypeDependent()) |
1482 | Result = CorrectDelayedTyposInExpr(E: Result.get()); |
1483 | else if (Result.isInvalid()) |
1484 | Result = CreateRecoveryExpr(Begin: TInfo->getTypeLoc().getBeginLoc(), |
1485 | End: RParenOrBraceLoc, SubExprs: exprs, T: Ty); |
1486 | return Result; |
1487 | } |
1488 | |
1489 | ExprResult |
1490 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
1491 | SourceLocation LParenOrBraceLoc, |
1492 | MultiExprArg Exprs, |
1493 | SourceLocation RParenOrBraceLoc, |
1494 | bool ListInitialization) { |
1495 | QualType Ty = TInfo->getType(); |
1496 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
1497 | |
1498 | assert((!ListInitialization || Exprs.size() == 1) && |
1499 | "List initialization must have exactly one expression." ); |
1500 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenOrBraceLoc); |
1501 | |
1502 | InitializedEntity Entity = |
1503 | InitializedEntity::InitializeTemporary(Context, TypeInfo: TInfo); |
1504 | InitializationKind Kind = |
1505 | Exprs.size() |
1506 | ? ListInitialization |
1507 | ? InitializationKind::CreateDirectList( |
1508 | InitLoc: TyBeginLoc, LBraceLoc: LParenOrBraceLoc, RBraceLoc: RParenOrBraceLoc) |
1509 | : InitializationKind::CreateDirect(InitLoc: TyBeginLoc, LParenLoc: LParenOrBraceLoc, |
1510 | RParenLoc: RParenOrBraceLoc) |
1511 | : InitializationKind::CreateValue(InitLoc: TyBeginLoc, LParenLoc: LParenOrBraceLoc, |
1512 | RParenLoc: RParenOrBraceLoc); |
1513 | |
1514 | // C++17 [expr.type.conv]p1: |
1515 | // If the type is a placeholder for a deduced class type, [...perform class |
1516 | // template argument deduction...] |
1517 | // C++23: |
1518 | // Otherwise, if the type contains a placeholder type, it is replaced by the |
1519 | // type determined by placeholder type deduction. |
1520 | DeducedType *Deduced = Ty->getContainedDeducedType(); |
1521 | if (Deduced && !Deduced->isDeduced() && |
1522 | isa<DeducedTemplateSpecializationType>(Val: Deduced)) { |
1523 | Ty = DeduceTemplateSpecializationFromInitializer(TInfo, Entity, |
1524 | Kind, Init: Exprs); |
1525 | if (Ty.isNull()) |
1526 | return ExprError(); |
1527 | Entity = InitializedEntity::InitializeTemporary(TypeInfo: TInfo, Type: Ty); |
1528 | } else if (Deduced && !Deduced->isDeduced()) { |
1529 | MultiExprArg Inits = Exprs; |
1530 | if (ListInitialization) { |
1531 | auto *ILE = cast<InitListExpr>(Val: Exprs[0]); |
1532 | Inits = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
1533 | } |
1534 | |
1535 | if (Inits.empty()) |
1536 | return ExprError(Diag(TyBeginLoc, diag::err_auto_expr_init_no_expression) |
1537 | << Ty << FullRange); |
1538 | if (Inits.size() > 1) { |
1539 | Expr *FirstBad = Inits[1]; |
1540 | return ExprError(Diag(FirstBad->getBeginLoc(), |
1541 | diag::err_auto_expr_init_multiple_expressions) |
1542 | << Ty << FullRange); |
1543 | } |
1544 | if (getLangOpts().CPlusPlus23) { |
1545 | if (Ty->getAs<AutoType>()) |
1546 | Diag(TyBeginLoc, diag::warn_cxx20_compat_auto_expr) << FullRange; |
1547 | } |
1548 | Expr *Deduce = Inits[0]; |
1549 | if (isa<InitListExpr>(Deduce)) |
1550 | return ExprError( |
1551 | Diag(Deduce->getBeginLoc(), diag::err_auto_expr_init_paren_braces) |
1552 | << ListInitialization << Ty << FullRange); |
1553 | QualType DeducedType; |
1554 | TemplateDeductionInfo Info(Deduce->getExprLoc()); |
1555 | TemplateDeductionResult Result = |
1556 | DeduceAutoType(AutoTypeLoc: TInfo->getTypeLoc(), Initializer: Deduce, Result&: DeducedType, Info); |
1557 | if (Result != TemplateDeductionResult::Success && |
1558 | Result != TemplateDeductionResult::AlreadyDiagnosed) |
1559 | return ExprError(Diag(TyBeginLoc, diag::err_auto_expr_deduction_failure) |
1560 | << Ty << Deduce->getType() << FullRange |
1561 | << Deduce->getSourceRange()); |
1562 | if (DeducedType.isNull()) { |
1563 | assert(Result == TemplateDeductionResult::AlreadyDiagnosed); |
1564 | return ExprError(); |
1565 | } |
1566 | |
1567 | Ty = DeducedType; |
1568 | Entity = InitializedEntity::InitializeTemporary(TypeInfo: TInfo, Type: Ty); |
1569 | } |
1570 | |
1571 | if (Ty->isDependentType() || CallExpr::hasAnyTypeDependentArguments(Exprs)) |
1572 | return CXXUnresolvedConstructExpr::Create( |
1573 | Context, T: Ty.getNonReferenceType(), TSI: TInfo, LParenLoc: LParenOrBraceLoc, Args: Exprs, |
1574 | RParenLoc: RParenOrBraceLoc, IsListInit: ListInitialization); |
1575 | |
1576 | // C++ [expr.type.conv]p1: |
1577 | // If the expression list is a parenthesized single expression, the type |
1578 | // conversion expression is equivalent (in definedness, and if defined in |
1579 | // meaning) to the corresponding cast expression. |
1580 | if (Exprs.size() == 1 && !ListInitialization && |
1581 | !isa<InitListExpr>(Val: Exprs[0])) { |
1582 | Expr *Arg = Exprs[0]; |
1583 | return BuildCXXFunctionalCastExpr(TInfo, Type: Ty, LParenLoc: LParenOrBraceLoc, CastExpr: Arg, |
1584 | RParenLoc: RParenOrBraceLoc); |
1585 | } |
1586 | |
1587 | // For an expression of the form T(), T shall not be an array type. |
1588 | QualType ElemTy = Ty; |
1589 | if (Ty->isArrayType()) { |
1590 | if (!ListInitialization) |
1591 | return ExprError(Diag(TyBeginLoc, diag::err_value_init_for_array_type) |
1592 | << FullRange); |
1593 | ElemTy = Context.getBaseElementType(QT: Ty); |
1594 | } |
1595 | |
1596 | // Only construct objects with object types. |
1597 | // The standard doesn't explicitly forbid function types here, but that's an |
1598 | // obvious oversight, as there's no way to dynamically construct a function |
1599 | // in general. |
1600 | if (Ty->isFunctionType()) |
1601 | return ExprError(Diag(TyBeginLoc, diag::err_init_for_function_type) |
1602 | << Ty << FullRange); |
1603 | |
1604 | // C++17 [expr.type.conv]p2: |
1605 | // If the type is cv void and the initializer is (), the expression is a |
1606 | // prvalue of the specified type that performs no initialization. |
1607 | if (!Ty->isVoidType() && |
1608 | RequireCompleteType(TyBeginLoc, ElemTy, |
1609 | diag::err_invalid_incomplete_type_use, FullRange)) |
1610 | return ExprError(); |
1611 | |
1612 | // Otherwise, the expression is a prvalue of the specified type whose |
1613 | // result object is direct-initialized (11.6) with the initializer. |
1614 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs); |
1615 | ExprResult Result = InitSeq.Perform(S&: *this, Entity, Kind, Args: Exprs); |
1616 | |
1617 | if (Result.isInvalid()) |
1618 | return Result; |
1619 | |
1620 | Expr *Inner = Result.get(); |
1621 | if (CXXBindTemporaryExpr *BTE = dyn_cast_or_null<CXXBindTemporaryExpr>(Val: Inner)) |
1622 | Inner = BTE->getSubExpr(); |
1623 | if (auto *CE = dyn_cast<ConstantExpr>(Val: Inner); |
1624 | CE && CE->isImmediateInvocation()) |
1625 | Inner = CE->getSubExpr(); |
1626 | if (!isa<CXXTemporaryObjectExpr>(Val: Inner) && |
1627 | !isa<CXXScalarValueInitExpr>(Val: Inner)) { |
1628 | // If we created a CXXTemporaryObjectExpr, that node also represents the |
1629 | // functional cast. Otherwise, create an explicit cast to represent |
1630 | // the syntactic form of a functional-style cast that was used here. |
1631 | // |
1632 | // FIXME: Creating a CXXFunctionalCastExpr around a CXXConstructExpr |
1633 | // would give a more consistent AST representation than using a |
1634 | // CXXTemporaryObjectExpr. It's also weird that the functional cast |
1635 | // is sometimes handled by initialization and sometimes not. |
1636 | QualType ResultType = Result.get()->getType(); |
1637 | SourceRange Locs = ListInitialization |
1638 | ? SourceRange() |
1639 | : SourceRange(LParenOrBraceLoc, RParenOrBraceLoc); |
1640 | Result = CXXFunctionalCastExpr::Create( |
1641 | Context, T: ResultType, VK: Expr::getValueKindForType(T: Ty), Written: TInfo, Kind: CK_NoOp, |
1642 | Op: Result.get(), /*Path=*/nullptr, FPO: CurFPFeatureOverrides(), |
1643 | LPLoc: Locs.getBegin(), RPLoc: Locs.getEnd()); |
1644 | } |
1645 | |
1646 | return Result; |
1647 | } |
1648 | |
1649 | bool Sema::isUsualDeallocationFunction(const CXXMethodDecl *Method) { |
1650 | // [CUDA] Ignore this function, if we can't call it. |
1651 | const FunctionDecl *Caller = getCurFunctionDecl(/*AllowLambda=*/true); |
1652 | if (getLangOpts().CUDA) { |
1653 | auto CallPreference = IdentifyCUDAPreference(Caller, Method); |
1654 | // If it's not callable at all, it's not the right function. |
1655 | if (CallPreference < CFP_WrongSide) |
1656 | return false; |
1657 | if (CallPreference == CFP_WrongSide) { |
1658 | // Maybe. We have to check if there are better alternatives. |
1659 | DeclContext::lookup_result R = |
1660 | Method->getDeclContext()->lookup(Method->getDeclName()); |
1661 | for (const auto *D : R) { |
1662 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
1663 | if (IdentifyCUDAPreference(Caller, FD) > CFP_WrongSide) |
1664 | return false; |
1665 | } |
1666 | } |
1667 | // We've found no better variants. |
1668 | } |
1669 | } |
1670 | |
1671 | SmallVector<const FunctionDecl*, 4> PreventedBy; |
1672 | bool Result = Method->isUsualDeallocationFunction(PreventedBy); |
1673 | |
1674 | if (Result || !getLangOpts().CUDA || PreventedBy.empty()) |
1675 | return Result; |
1676 | |
1677 | // In case of CUDA, return true if none of the 1-argument deallocator |
1678 | // functions are actually callable. |
1679 | return llvm::none_of(Range&: PreventedBy, P: [&](const FunctionDecl *FD) { |
1680 | assert(FD->getNumParams() == 1 && |
1681 | "Only single-operand functions should be in PreventedBy" ); |
1682 | return IdentifyCUDAPreference(Caller, Callee: FD) >= CFP_HostDevice; |
1683 | }); |
1684 | } |
1685 | |
1686 | /// Determine whether the given function is a non-placement |
1687 | /// deallocation function. |
1688 | static bool isNonPlacementDeallocationFunction(Sema &S, FunctionDecl *FD) { |
1689 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Val: FD)) |
1690 | return S.isUsualDeallocationFunction(Method); |
1691 | |
1692 | if (FD->getOverloadedOperator() != OO_Delete && |
1693 | FD->getOverloadedOperator() != OO_Array_Delete) |
1694 | return false; |
1695 | |
1696 | unsigned UsualParams = 1; |
1697 | |
1698 | if (S.getLangOpts().SizedDeallocation && UsualParams < FD->getNumParams() && |
1699 | S.Context.hasSameUnqualifiedType( |
1700 | T1: FD->getParamDecl(i: UsualParams)->getType(), |
1701 | T2: S.Context.getSizeType())) |
1702 | ++UsualParams; |
1703 | |
1704 | if (S.getLangOpts().AlignedAllocation && UsualParams < FD->getNumParams() && |
1705 | S.Context.hasSameUnqualifiedType( |
1706 | T1: FD->getParamDecl(i: UsualParams)->getType(), |
1707 | T2: S.Context.getTypeDeclType(S.getStdAlignValT()))) |
1708 | ++UsualParams; |
1709 | |
1710 | return UsualParams == FD->getNumParams(); |
1711 | } |
1712 | |
1713 | namespace { |
1714 | struct UsualDeallocFnInfo { |
1715 | UsualDeallocFnInfo() : Found(), FD(nullptr) {} |
1716 | UsualDeallocFnInfo(Sema &S, DeclAccessPair Found) |
1717 | : Found(Found), FD(dyn_cast<FunctionDecl>(Val: Found->getUnderlyingDecl())), |
1718 | Destroying(false), HasSizeT(false), HasAlignValT(false), |
1719 | CUDAPref(Sema::CFP_Native) { |
1720 | // A function template declaration is never a usual deallocation function. |
1721 | if (!FD) |
1722 | return; |
1723 | unsigned NumBaseParams = 1; |
1724 | if (FD->isDestroyingOperatorDelete()) { |
1725 | Destroying = true; |
1726 | ++NumBaseParams; |
1727 | } |
1728 | |
1729 | if (NumBaseParams < FD->getNumParams() && |
1730 | S.Context.hasSameUnqualifiedType( |
1731 | T1: FD->getParamDecl(i: NumBaseParams)->getType(), |
1732 | T2: S.Context.getSizeType())) { |
1733 | ++NumBaseParams; |
1734 | HasSizeT = true; |
1735 | } |
1736 | |
1737 | if (NumBaseParams < FD->getNumParams() && |
1738 | FD->getParamDecl(i: NumBaseParams)->getType()->isAlignValT()) { |
1739 | ++NumBaseParams; |
1740 | HasAlignValT = true; |
1741 | } |
1742 | |
1743 | // In CUDA, determine how much we'd like / dislike to call this. |
1744 | if (S.getLangOpts().CUDA) |
1745 | CUDAPref = S.IdentifyCUDAPreference( |
1746 | Caller: S.getCurFunctionDecl(/*AllowLambda=*/true), Callee: FD); |
1747 | } |
1748 | |
1749 | explicit operator bool() const { return FD; } |
1750 | |
1751 | bool isBetterThan(const UsualDeallocFnInfo &Other, bool WantSize, |
1752 | bool WantAlign) const { |
1753 | // C++ P0722: |
1754 | // A destroying operator delete is preferred over a non-destroying |
1755 | // operator delete. |
1756 | if (Destroying != Other.Destroying) |
1757 | return Destroying; |
1758 | |
1759 | // C++17 [expr.delete]p10: |
1760 | // If the type has new-extended alignment, a function with a parameter |
1761 | // of type std::align_val_t is preferred; otherwise a function without |
1762 | // such a parameter is preferred |
1763 | if (HasAlignValT != Other.HasAlignValT) |
1764 | return HasAlignValT == WantAlign; |
1765 | |
1766 | if (HasSizeT != Other.HasSizeT) |
1767 | return HasSizeT == WantSize; |
1768 | |
1769 | // Use CUDA call preference as a tiebreaker. |
1770 | return CUDAPref > Other.CUDAPref; |
1771 | } |
1772 | |
1773 | DeclAccessPair Found; |
1774 | FunctionDecl *FD; |
1775 | bool Destroying, HasSizeT, HasAlignValT; |
1776 | Sema::CUDAFunctionPreference CUDAPref; |
1777 | }; |
1778 | } |
1779 | |
1780 | /// Determine whether a type has new-extended alignment. This may be called when |
1781 | /// the type is incomplete (for a delete-expression with an incomplete pointee |
1782 | /// type), in which case it will conservatively return false if the alignment is |
1783 | /// not known. |
1784 | static bool hasNewExtendedAlignment(Sema &S, QualType AllocType) { |
1785 | return S.getLangOpts().AlignedAllocation && |
1786 | S.getASTContext().getTypeAlignIfKnown(T: AllocType) > |
1787 | S.getASTContext().getTargetInfo().getNewAlign(); |
1788 | } |
1789 | |
1790 | /// Select the correct "usual" deallocation function to use from a selection of |
1791 | /// deallocation functions (either global or class-scope). |
1792 | static UsualDeallocFnInfo resolveDeallocationOverload( |
1793 | Sema &S, LookupResult &R, bool WantSize, bool WantAlign, |
1794 | llvm::SmallVectorImpl<UsualDeallocFnInfo> *BestFns = nullptr) { |
1795 | UsualDeallocFnInfo Best; |
1796 | |
1797 | for (auto I = R.begin(), E = R.end(); I != E; ++I) { |
1798 | UsualDeallocFnInfo Info(S, I.getPair()); |
1799 | if (!Info || !isNonPlacementDeallocationFunction(S, FD: Info.FD) || |
1800 | Info.CUDAPref == Sema::CFP_Never) |
1801 | continue; |
1802 | |
1803 | if (!Best) { |
1804 | Best = Info; |
1805 | if (BestFns) |
1806 | BestFns->push_back(Elt: Info); |
1807 | continue; |
1808 | } |
1809 | |
1810 | if (Best.isBetterThan(Other: Info, WantSize, WantAlign)) |
1811 | continue; |
1812 | |
1813 | // If more than one preferred function is found, all non-preferred |
1814 | // functions are eliminated from further consideration. |
1815 | if (BestFns && Info.isBetterThan(Other: Best, WantSize, WantAlign)) |
1816 | BestFns->clear(); |
1817 | |
1818 | Best = Info; |
1819 | if (BestFns) |
1820 | BestFns->push_back(Elt: Info); |
1821 | } |
1822 | |
1823 | return Best; |
1824 | } |
1825 | |
1826 | /// Determine whether a given type is a class for which 'delete[]' would call |
1827 | /// a member 'operator delete[]' with a 'size_t' parameter. This implies that |
1828 | /// we need to store the array size (even if the type is |
1829 | /// trivially-destructible). |
1830 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, |
1831 | QualType allocType) { |
1832 | const RecordType *record = |
1833 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
1834 | if (!record) return false; |
1835 | |
1836 | // Try to find an operator delete[] in class scope. |
1837 | |
1838 | DeclarationName deleteName = |
1839 | S.Context.DeclarationNames.getCXXOperatorName(Op: OO_Array_Delete); |
1840 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); |
1841 | S.LookupQualifiedName(ops, record->getDecl()); |
1842 | |
1843 | // We're just doing this for information. |
1844 | ops.suppressDiagnostics(); |
1845 | |
1846 | // Very likely: there's no operator delete[]. |
1847 | if (ops.empty()) return false; |
1848 | |
1849 | // If it's ambiguous, it should be illegal to call operator delete[] |
1850 | // on this thing, so it doesn't matter if we allocate extra space or not. |
1851 | if (ops.isAmbiguous()) return false; |
1852 | |
1853 | // C++17 [expr.delete]p10: |
1854 | // If the deallocation functions have class scope, the one without a |
1855 | // parameter of type std::size_t is selected. |
1856 | auto Best = resolveDeallocationOverload( |
1857 | S, R&: ops, /*WantSize*/false, |
1858 | /*WantAlign*/hasNewExtendedAlignment(S, AllocType: allocType)); |
1859 | return Best && Best.HasSizeT; |
1860 | } |
1861 | |
1862 | /// Parsed a C++ 'new' expression (C++ 5.3.4). |
1863 | /// |
1864 | /// E.g.: |
1865 | /// @code new (memory) int[size][4] @endcode |
1866 | /// or |
1867 | /// @code ::new Foo(23, "hello") @endcode |
1868 | /// |
1869 | /// \param StartLoc The first location of the expression. |
1870 | /// \param UseGlobal True if 'new' was prefixed with '::'. |
1871 | /// \param PlacementLParen Opening paren of the placement arguments. |
1872 | /// \param PlacementArgs Placement new arguments. |
1873 | /// \param PlacementRParen Closing paren of the placement arguments. |
1874 | /// \param TypeIdParens If the type is in parens, the source range. |
1875 | /// \param D The type to be allocated, as well as array dimensions. |
1876 | /// \param Initializer The initializing expression or initializer-list, or null |
1877 | /// if there is none. |
1878 | ExprResult |
1879 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
1880 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
1881 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
1882 | Declarator &D, Expr *Initializer) { |
1883 | std::optional<Expr *> ArraySize; |
1884 | // If the specified type is an array, unwrap it and save the expression. |
1885 | if (D.getNumTypeObjects() > 0 && |
1886 | D.getTypeObject(i: 0).Kind == DeclaratorChunk::Array) { |
1887 | DeclaratorChunk &Chunk = D.getTypeObject(i: 0); |
1888 | if (D.getDeclSpec().hasAutoTypeSpec()) |
1889 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) |
1890 | << D.getSourceRange()); |
1891 | if (Chunk.Arr.hasStatic) |
1892 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
1893 | << D.getSourceRange()); |
1894 | if (!Chunk.Arr.NumElts && !Initializer) |
1895 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
1896 | << D.getSourceRange()); |
1897 | |
1898 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
1899 | D.DropFirstTypeObject(); |
1900 | } |
1901 | |
1902 | // Every dimension shall be of constant size. |
1903 | if (ArraySize) { |
1904 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
1905 | if (D.getTypeObject(i: I).Kind != DeclaratorChunk::Array) |
1906 | break; |
1907 | |
1908 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(i: I).Arr; |
1909 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
1910 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent()) { |
1911 | // FIXME: GCC permits constant folding here. We should either do so consistently |
1912 | // or not do so at all, rather than changing behavior in C++14 onwards. |
1913 | if (getLangOpts().CPlusPlus14) { |
1914 | // C++1y [expr.new]p6: Every constant-expression in a noptr-new-declarator |
1915 | // shall be a converted constant expression (5.19) of type std::size_t |
1916 | // and shall evaluate to a strictly positive value. |
1917 | llvm::APSInt Value(Context.getIntWidth(T: Context.getSizeType())); |
1918 | Array.NumElts |
1919 | = CheckConvertedConstantExpression(From: NumElts, T: Context.getSizeType(), Value, |
1920 | CCE: CCEK_ArrayBound) |
1921 | .get(); |
1922 | } else { |
1923 | Array.NumElts = |
1924 | VerifyIntegerConstantExpression( |
1925 | NumElts, nullptr, diag::err_new_array_nonconst, AllowFold) |
1926 | .get(); |
1927 | } |
1928 | if (!Array.NumElts) |
1929 | return ExprError(); |
1930 | } |
1931 | } |
1932 | } |
1933 | } |
1934 | |
1935 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D); |
1936 | QualType AllocType = TInfo->getType(); |
1937 | if (D.isInvalidType()) |
1938 | return ExprError(); |
1939 | |
1940 | SourceRange DirectInitRange; |
1941 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Val: Initializer)) |
1942 | DirectInitRange = List->getSourceRange(); |
1943 | |
1944 | return BuildCXXNew(Range: SourceRange(StartLoc, D.getEndLoc()), UseGlobal, |
1945 | PlacementLParen, PlacementArgs, PlacementRParen, |
1946 | TypeIdParens, AllocType, AllocTypeInfo: TInfo, ArraySize, DirectInitRange, |
1947 | Initializer); |
1948 | } |
1949 | |
1950 | static bool isLegalArrayNewInitializer(CXXNewInitializationStyle Style, |
1951 | Expr *Init, bool IsCPlusPlus20) { |
1952 | if (!Init) |
1953 | return true; |
1954 | if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(Val: Init)) |
1955 | return IsCPlusPlus20 || PLE->getNumExprs() == 0; |
1956 | if (isa<ImplicitValueInitExpr>(Val: Init)) |
1957 | return true; |
1958 | else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Val: Init)) |
1959 | return !CCE->isListInitialization() && |
1960 | CCE->getConstructor()->isDefaultConstructor(); |
1961 | else if (Style == CXXNewInitializationStyle::Braces) { |
1962 | assert(isa<InitListExpr>(Init) && |
1963 | "Shouldn't create list CXXConstructExprs for arrays." ); |
1964 | return true; |
1965 | } |
1966 | return false; |
1967 | } |
1968 | |
1969 | bool |
1970 | Sema::isUnavailableAlignedAllocationFunction(const FunctionDecl &FD) const { |
1971 | if (!getLangOpts().AlignedAllocationUnavailable) |
1972 | return false; |
1973 | if (FD.isDefined()) |
1974 | return false; |
1975 | std::optional<unsigned> AlignmentParam; |
1976 | if (FD.isReplaceableGlobalAllocationFunction(AlignmentParam: &AlignmentParam) && |
1977 | AlignmentParam) |
1978 | return true; |
1979 | return false; |
1980 | } |
1981 | |
1982 | // Emit a diagnostic if an aligned allocation/deallocation function that is not |
1983 | // implemented in the standard library is selected. |
1984 | void Sema::diagnoseUnavailableAlignedAllocation(const FunctionDecl &FD, |
1985 | SourceLocation Loc) { |
1986 | if (isUnavailableAlignedAllocationFunction(FD)) { |
1987 | const llvm::Triple &T = getASTContext().getTargetInfo().getTriple(); |
1988 | StringRef OSName = AvailabilityAttr::getPlatformNameSourceSpelling( |
1989 | getASTContext().getTargetInfo().getPlatformName()); |
1990 | VersionTuple OSVersion = alignedAllocMinVersion(OS: T.getOS()); |
1991 | |
1992 | OverloadedOperatorKind Kind = FD.getDeclName().getCXXOverloadedOperator(); |
1993 | bool IsDelete = Kind == OO_Delete || Kind == OO_Array_Delete; |
1994 | Diag(Loc, diag::err_aligned_allocation_unavailable) |
1995 | << IsDelete << FD.getType().getAsString() << OSName |
1996 | << OSVersion.getAsString() << OSVersion.empty(); |
1997 | Diag(Loc, diag::note_silence_aligned_allocation_unavailable); |
1998 | } |
1999 | } |
2000 | |
2001 | ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, |
2002 | SourceLocation PlacementLParen, |
2003 | MultiExprArg PlacementArgs, |
2004 | SourceLocation PlacementRParen, |
2005 | SourceRange TypeIdParens, QualType AllocType, |
2006 | TypeSourceInfo *AllocTypeInfo, |
2007 | std::optional<Expr *> ArraySize, |
2008 | SourceRange DirectInitRange, Expr *Initializer) { |
2009 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
2010 | SourceLocation StartLoc = Range.getBegin(); |
2011 | |
2012 | CXXNewInitializationStyle InitStyle; |
2013 | if (DirectInitRange.isValid()) { |
2014 | assert(Initializer && "Have parens but no initializer." ); |
2015 | InitStyle = CXXNewInitializationStyle::Parens; |
2016 | } else if (Initializer && isa<InitListExpr>(Val: Initializer)) |
2017 | InitStyle = CXXNewInitializationStyle::Braces; |
2018 | else { |
2019 | assert((!Initializer || isa<ImplicitValueInitExpr>(Initializer) || |
2020 | isa<CXXConstructExpr>(Initializer)) && |
2021 | "Initializer expression that cannot have been implicitly created." ); |
2022 | InitStyle = CXXNewInitializationStyle::None; |
2023 | } |
2024 | |
2025 | MultiExprArg Exprs(&Initializer, Initializer ? 1 : 0); |
2026 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Val: Initializer)) { |
2027 | assert(InitStyle == CXXNewInitializationStyle::Parens && |
2028 | "paren init for non-call init" ); |
2029 | Exprs = MultiExprArg(List->getExprs(), List->getNumExprs()); |
2030 | } |
2031 | |
2032 | // C++11 [expr.new]p15: |
2033 | // A new-expression that creates an object of type T initializes that |
2034 | // object as follows: |
2035 | InitializationKind Kind = [&] { |
2036 | switch (InitStyle) { |
2037 | // - If the new-initializer is omitted, the object is default- |
2038 | // initialized (8.5); if no initialization is performed, |
2039 | // the object has indeterminate value |
2040 | case CXXNewInitializationStyle::None: |
2041 | return InitializationKind::CreateDefault(InitLoc: TypeRange.getBegin()); |
2042 | // - Otherwise, the new-initializer is interpreted according to the |
2043 | // initialization rules of 8.5 for direct-initialization. |
2044 | case CXXNewInitializationStyle::Parens: |
2045 | return InitializationKind::CreateDirect(InitLoc: TypeRange.getBegin(), |
2046 | LParenLoc: DirectInitRange.getBegin(), |
2047 | RParenLoc: DirectInitRange.getEnd()); |
2048 | case CXXNewInitializationStyle::Braces: |
2049 | return InitializationKind::CreateDirectList(TypeRange.getBegin(), |
2050 | Initializer->getBeginLoc(), |
2051 | Initializer->getEndLoc()); |
2052 | } |
2053 | llvm_unreachable("Unknown initialization kind" ); |
2054 | }(); |
2055 | |
2056 | // C++11 [dcl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
2057 | auto *Deduced = AllocType->getContainedDeducedType(); |
2058 | if (Deduced && !Deduced->isDeduced() && |
2059 | isa<DeducedTemplateSpecializationType>(Deduced)) { |
2060 | if (ArraySize) |
2061 | return ExprError( |
2062 | Diag(*ArraySize ? (*ArraySize)->getExprLoc() : TypeRange.getBegin(), |
2063 | diag::err_deduced_class_template_compound_type) |
2064 | << /*array*/ 2 |
2065 | << (*ArraySize ? (*ArraySize)->getSourceRange() : TypeRange)); |
2066 | |
2067 | InitializedEntity Entity |
2068 | = InitializedEntity::InitializeNew(NewLoc: StartLoc, Type: AllocType); |
2069 | AllocType = DeduceTemplateSpecializationFromInitializer( |
2070 | TInfo: AllocTypeInfo, Entity, Kind, Init: Exprs); |
2071 | if (AllocType.isNull()) |
2072 | return ExprError(); |
2073 | } else if (Deduced && !Deduced->isDeduced()) { |
2074 | MultiExprArg Inits = Exprs; |
2075 | bool Braced = (InitStyle == CXXNewInitializationStyle::Braces); |
2076 | if (Braced) { |
2077 | auto *ILE = cast<InitListExpr>(Val: Exprs[0]); |
2078 | Inits = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
2079 | } |
2080 | |
2081 | if (InitStyle == CXXNewInitializationStyle::None || Inits.empty()) |
2082 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) |
2083 | << AllocType << TypeRange); |
2084 | if (Inits.size() > 1) { |
2085 | Expr *FirstBad = Inits[1]; |
2086 | return ExprError(Diag(FirstBad->getBeginLoc(), |
2087 | diag::err_auto_new_ctor_multiple_expressions) |
2088 | << AllocType << TypeRange); |
2089 | } |
2090 | if (Braced && !getLangOpts().CPlusPlus17) |
2091 | Diag(Initializer->getBeginLoc(), diag::ext_auto_new_list_init) |
2092 | << AllocType << TypeRange; |
2093 | Expr *Deduce = Inits[0]; |
2094 | if (isa<InitListExpr>(Deduce)) |
2095 | return ExprError( |
2096 | Diag(Deduce->getBeginLoc(), diag::err_auto_expr_init_paren_braces) |
2097 | << Braced << AllocType << TypeRange); |
2098 | QualType DeducedType; |
2099 | TemplateDeductionInfo Info(Deduce->getExprLoc()); |
2100 | TemplateDeductionResult Result = |
2101 | DeduceAutoType(AutoTypeLoc: AllocTypeInfo->getTypeLoc(), Initializer: Deduce, Result&: DeducedType, Info); |
2102 | if (Result != TemplateDeductionResult::Success && |
2103 | Result != TemplateDeductionResult::AlreadyDiagnosed) |
2104 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) |
2105 | << AllocType << Deduce->getType() << TypeRange |
2106 | << Deduce->getSourceRange()); |
2107 | if (DeducedType.isNull()) { |
2108 | assert(Result == TemplateDeductionResult::AlreadyDiagnosed); |
2109 | return ExprError(); |
2110 | } |
2111 | AllocType = DeducedType; |
2112 | } |
2113 | |
2114 | // Per C++0x [expr.new]p5, the type being constructed may be a |
2115 | // typedef of an array type. |
2116 | if (!ArraySize) { |
2117 | if (const ConstantArrayType *Array |
2118 | = Context.getAsConstantArrayType(T: AllocType)) { |
2119 | ArraySize = IntegerLiteral::Create(C: Context, V: Array->getSize(), |
2120 | type: Context.getSizeType(), |
2121 | l: TypeRange.getEnd()); |
2122 | AllocType = Array->getElementType(); |
2123 | } |
2124 | } |
2125 | |
2126 | if (CheckAllocatedType(AllocType, Loc: TypeRange.getBegin(), R: TypeRange)) |
2127 | return ExprError(); |
2128 | |
2129 | if (ArraySize && !checkArrayElementAlignment(EltTy: AllocType, Loc: TypeRange.getBegin())) |
2130 | return ExprError(); |
2131 | |
2132 | // In ARC, infer 'retaining' for the allocated |
2133 | if (getLangOpts().ObjCAutoRefCount && |
2134 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && |
2135 | AllocType->isObjCLifetimeType()) { |
2136 | AllocType = Context.getLifetimeQualifiedType(type: AllocType, |
2137 | lifetime: AllocType->getObjCARCImplicitLifetime()); |
2138 | } |
2139 | |
2140 | QualType ResultType = Context.getPointerType(T: AllocType); |
2141 | |
2142 | if (ArraySize && *ArraySize && |
2143 | (*ArraySize)->getType()->isNonOverloadPlaceholderType()) { |
2144 | ExprResult result = CheckPlaceholderExpr(E: *ArraySize); |
2145 | if (result.isInvalid()) return ExprError(); |
2146 | ArraySize = result.get(); |
2147 | } |
2148 | // C++98 5.3.4p6: "The expression in a direct-new-declarator shall have |
2149 | // integral or enumeration type with a non-negative value." |
2150 | // C++11 [expr.new]p6: The expression [...] shall be of integral or unscoped |
2151 | // enumeration type, or a class type for which a single non-explicit |
2152 | // conversion function to integral or unscoped enumeration type exists. |
2153 | // C++1y [expr.new]p6: The expression [...] is implicitly converted to |
2154 | // std::size_t. |
2155 | std::optional<uint64_t> KnownArraySize; |
2156 | if (ArraySize && *ArraySize && !(*ArraySize)->isTypeDependent()) { |
2157 | ExprResult ConvertedSize; |
2158 | if (getLangOpts().CPlusPlus14) { |
2159 | assert(Context.getTargetInfo().getIntWidth() && "Builtin type of size 0?" ); |
2160 | |
2161 | ConvertedSize = PerformImplicitConversion(From: *ArraySize, ToType: Context.getSizeType(), |
2162 | Action: AA_Converting); |
2163 | |
2164 | if (!ConvertedSize.isInvalid() && |
2165 | (*ArraySize)->getType()->getAs<RecordType>()) |
2166 | // Diagnose the compatibility of this conversion. |
2167 | Diag(StartLoc, diag::warn_cxx98_compat_array_size_conversion) |
2168 | << (*ArraySize)->getType() << 0 << "'size_t'" ; |
2169 | } else { |
2170 | class SizeConvertDiagnoser : public ICEConvertDiagnoser { |
2171 | protected: |
2172 | Expr *ArraySize; |
2173 | |
2174 | public: |
2175 | SizeConvertDiagnoser(Expr *ArraySize) |
2176 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false, false, false), |
2177 | ArraySize(ArraySize) {} |
2178 | |
2179 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
2180 | QualType T) override { |
2181 | return S.Diag(Loc, diag::err_array_size_not_integral) |
2182 | << S.getLangOpts().CPlusPlus11 << T; |
2183 | } |
2184 | |
2185 | SemaDiagnosticBuilder diagnoseIncomplete( |
2186 | Sema &S, SourceLocation Loc, QualType T) override { |
2187 | return S.Diag(Loc, diag::err_array_size_incomplete_type) |
2188 | << T << ArraySize->getSourceRange(); |
2189 | } |
2190 | |
2191 | SemaDiagnosticBuilder diagnoseExplicitConv( |
2192 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { |
2193 | return S.Diag(Loc, diag::err_array_size_explicit_conversion) << T << ConvTy; |
2194 | } |
2195 | |
2196 | SemaDiagnosticBuilder noteExplicitConv( |
2197 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
2198 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
2199 | << ConvTy->isEnumeralType() << ConvTy; |
2200 | } |
2201 | |
2202 | SemaDiagnosticBuilder diagnoseAmbiguous( |
2203 | Sema &S, SourceLocation Loc, QualType T) override { |
2204 | return S.Diag(Loc, diag::err_array_size_ambiguous_conversion) << T; |
2205 | } |
2206 | |
2207 | SemaDiagnosticBuilder noteAmbiguous( |
2208 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
2209 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
2210 | << ConvTy->isEnumeralType() << ConvTy; |
2211 | } |
2212 | |
2213 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
2214 | QualType T, |
2215 | QualType ConvTy) override { |
2216 | return S.Diag(Loc, |
2217 | S.getLangOpts().CPlusPlus11 |
2218 | ? diag::warn_cxx98_compat_array_size_conversion |
2219 | : diag::ext_array_size_conversion) |
2220 | << T << ConvTy->isEnumeralType() << ConvTy; |
2221 | } |
2222 | } SizeDiagnoser(*ArraySize); |
2223 | |
2224 | ConvertedSize = PerformContextualImplicitConversion(Loc: StartLoc, FromE: *ArraySize, |
2225 | Converter&: SizeDiagnoser); |
2226 | } |
2227 | if (ConvertedSize.isInvalid()) |
2228 | return ExprError(); |
2229 | |
2230 | ArraySize = ConvertedSize.get(); |
2231 | QualType SizeType = (*ArraySize)->getType(); |
2232 | |
2233 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
2234 | return ExprError(); |
2235 | |
2236 | // C++98 [expr.new]p7: |
2237 | // The expression in a direct-new-declarator shall have integral type |
2238 | // with a non-negative value. |
2239 | // |
2240 | // Let's see if this is a constant < 0. If so, we reject it out of hand, |
2241 | // per CWG1464. Otherwise, if it's not a constant, we must have an |
2242 | // unparenthesized array type. |
2243 | |
2244 | // We've already performed any required implicit conversion to integer or |
2245 | // unscoped enumeration type. |
2246 | // FIXME: Per CWG1464, we are required to check the value prior to |
2247 | // converting to size_t. This will never find a negative array size in |
2248 | // C++14 onwards, because Value is always unsigned here! |
2249 | if (std::optional<llvm::APSInt> Value = |
2250 | (*ArraySize)->getIntegerConstantExpr(Ctx: Context)) { |
2251 | if (Value->isSigned() && Value->isNegative()) { |
2252 | return ExprError(Diag((*ArraySize)->getBeginLoc(), |
2253 | diag::err_typecheck_negative_array_size) |
2254 | << (*ArraySize)->getSourceRange()); |
2255 | } |
2256 | |
2257 | if (!AllocType->isDependentType()) { |
2258 | unsigned ActiveSizeBits = |
2259 | ConstantArrayType::getNumAddressingBits(Context, ElementType: AllocType, NumElements: *Value); |
2260 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) |
2261 | return ExprError( |
2262 | Diag((*ArraySize)->getBeginLoc(), diag::err_array_too_large) |
2263 | << toString(*Value, 10) << (*ArraySize)->getSourceRange()); |
2264 | } |
2265 | |
2266 | KnownArraySize = Value->getZExtValue(); |
2267 | } else if (TypeIdParens.isValid()) { |
2268 | // Can't have dynamic array size when the type-id is in parentheses. |
2269 | Diag((*ArraySize)->getBeginLoc(), diag::ext_new_paren_array_nonconst) |
2270 | << (*ArraySize)->getSourceRange() |
2271 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
2272 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
2273 | |
2274 | TypeIdParens = SourceRange(); |
2275 | } |
2276 | |
2277 | // Note that we do *not* convert the argument in any way. It can |
2278 | // be signed, larger than size_t, whatever. |
2279 | } |
2280 | |
2281 | FunctionDecl *OperatorNew = nullptr; |
2282 | FunctionDecl *OperatorDelete = nullptr; |
2283 | unsigned Alignment = |
2284 | AllocType->isDependentType() ? 0 : Context.getTypeAlign(T: AllocType); |
2285 | unsigned NewAlignment = Context.getTargetInfo().getNewAlign(); |
2286 | bool PassAlignment = getLangOpts().AlignedAllocation && |
2287 | Alignment > NewAlignment; |
2288 | |
2289 | if (CheckArgsForPlaceholders(args: PlacementArgs)) |
2290 | return ExprError(); |
2291 | |
2292 | AllocationFunctionScope Scope = UseGlobal ? AFS_Global : AFS_Both; |
2293 | if (!AllocType->isDependentType() && |
2294 | !Expr::hasAnyTypeDependentArguments(Exprs: PlacementArgs) && |
2295 | FindAllocationFunctions( |
2296 | StartLoc, Range: SourceRange(PlacementLParen, PlacementRParen), NewScope: Scope, DeleteScope: Scope, |
2297 | AllocType, IsArray: ArraySize.has_value(), PassAlignment, PlaceArgs: PlacementArgs, |
2298 | OperatorNew, OperatorDelete)) |
2299 | return ExprError(); |
2300 | |
2301 | // If this is an array allocation, compute whether the usual array |
2302 | // deallocation function for the type has a size_t parameter. |
2303 | bool UsualArrayDeleteWantsSize = false; |
2304 | if (ArraySize && !AllocType->isDependentType()) |
2305 | UsualArrayDeleteWantsSize = |
2306 | doesUsualArrayDeleteWantSize(S&: *this, loc: StartLoc, allocType: AllocType); |
2307 | |
2308 | SmallVector<Expr *, 8> AllPlaceArgs; |
2309 | if (OperatorNew) { |
2310 | auto *Proto = OperatorNew->getType()->castAs<FunctionProtoType>(); |
2311 | VariadicCallType CallType = Proto->isVariadic() ? VariadicFunction |
2312 | : VariadicDoesNotApply; |
2313 | |
2314 | // We've already converted the placement args, just fill in any default |
2315 | // arguments. Skip the first parameter because we don't have a corresponding |
2316 | // argument. Skip the second parameter too if we're passing in the |
2317 | // alignment; we've already filled it in. |
2318 | unsigned NumImplicitArgs = PassAlignment ? 2 : 1; |
2319 | if (GatherArgumentsForCall(CallLoc: PlacementLParen, FDecl: OperatorNew, Proto: Proto, |
2320 | FirstParam: NumImplicitArgs, Args: PlacementArgs, AllArgs&: AllPlaceArgs, |
2321 | CallType)) |
2322 | return ExprError(); |
2323 | |
2324 | if (!AllPlaceArgs.empty()) |
2325 | PlacementArgs = AllPlaceArgs; |
2326 | |
2327 | // We would like to perform some checking on the given `operator new` call, |
2328 | // but the PlacementArgs does not contain the implicit arguments, |
2329 | // namely allocation size and maybe allocation alignment, |
2330 | // so we need to conjure them. |
2331 | |
2332 | QualType SizeTy = Context.getSizeType(); |
2333 | unsigned SizeTyWidth = Context.getTypeSize(T: SizeTy); |
2334 | |
2335 | llvm::APInt SingleEltSize( |
2336 | SizeTyWidth, Context.getTypeSizeInChars(T: AllocType).getQuantity()); |
2337 | |
2338 | // How many bytes do we want to allocate here? |
2339 | std::optional<llvm::APInt> AllocationSize; |
2340 | if (!ArraySize && !AllocType->isDependentType()) { |
2341 | // For non-array operator new, we only want to allocate one element. |
2342 | AllocationSize = SingleEltSize; |
2343 | } else if (KnownArraySize && !AllocType->isDependentType()) { |
2344 | // For array operator new, only deal with static array size case. |
2345 | bool Overflow; |
2346 | AllocationSize = llvm::APInt(SizeTyWidth, *KnownArraySize) |
2347 | .umul_ov(RHS: SingleEltSize, Overflow); |
2348 | (void)Overflow; |
2349 | assert( |
2350 | !Overflow && |
2351 | "Expected that all the overflows would have been handled already." ); |
2352 | } |
2353 | |
2354 | IntegerLiteral AllocationSizeLiteral( |
2355 | Context, AllocationSize.value_or(u: llvm::APInt::getZero(numBits: SizeTyWidth)), |
2356 | SizeTy, SourceLocation()); |
2357 | // Otherwise, if we failed to constant-fold the allocation size, we'll |
2358 | // just give up and pass-in something opaque, that isn't a null pointer. |
2359 | OpaqueValueExpr OpaqueAllocationSize(SourceLocation(), SizeTy, VK_PRValue, |
2360 | OK_Ordinary, /*SourceExpr=*/nullptr); |
2361 | |
2362 | // Let's synthesize the alignment argument in case we will need it. |
2363 | // Since we *really* want to allocate these on stack, this is slightly ugly |
2364 | // because there might not be a `std::align_val_t` type. |
2365 | EnumDecl *StdAlignValT = getStdAlignValT(); |
2366 | QualType AlignValT = |
2367 | StdAlignValT ? Context.getTypeDeclType(StdAlignValT) : SizeTy; |
2368 | IntegerLiteral AlignmentLiteral( |
2369 | Context, |
2370 | llvm::APInt(Context.getTypeSize(T: SizeTy), |
2371 | Alignment / Context.getCharWidth()), |
2372 | SizeTy, SourceLocation()); |
2373 | ImplicitCastExpr DesiredAlignment(ImplicitCastExpr::OnStack, AlignValT, |
2374 | CK_IntegralCast, &AlignmentLiteral, |
2375 | VK_PRValue, FPOptionsOverride()); |
2376 | |
2377 | // Adjust placement args by prepending conjured size and alignment exprs. |
2378 | llvm::SmallVector<Expr *, 8> CallArgs; |
2379 | CallArgs.reserve(N: NumImplicitArgs + PlacementArgs.size()); |
2380 | CallArgs.emplace_back(AllocationSize |
2381 | ? static_cast<Expr *>(&AllocationSizeLiteral) |
2382 | : &OpaqueAllocationSize); |
2383 | if (PassAlignment) |
2384 | CallArgs.emplace_back(Args: &DesiredAlignment); |
2385 | CallArgs.insert(I: CallArgs.end(), From: PlacementArgs.begin(), To: PlacementArgs.end()); |
2386 | |
2387 | DiagnoseSentinelCalls(OperatorNew, PlacementLParen, CallArgs); |
2388 | |
2389 | checkCall(FDecl: OperatorNew, Proto: Proto, /*ThisArg=*/nullptr, Args: CallArgs, |
2390 | /*IsMemberFunction=*/false, Loc: StartLoc, Range, CallType); |
2391 | |
2392 | // Warn if the type is over-aligned and is being allocated by (unaligned) |
2393 | // global operator new. |
2394 | if (PlacementArgs.empty() && !PassAlignment && |
2395 | (OperatorNew->isImplicit() || |
2396 | (OperatorNew->getBeginLoc().isValid() && |
2397 | getSourceManager().isInSystemHeader(Loc: OperatorNew->getBeginLoc())))) { |
2398 | if (Alignment > NewAlignment) |
2399 | Diag(StartLoc, diag::warn_overaligned_type) |
2400 | << AllocType |
2401 | << unsigned(Alignment / Context.getCharWidth()) |
2402 | << unsigned(NewAlignment / Context.getCharWidth()); |
2403 | } |
2404 | } |
2405 | |
2406 | // Array 'new' can't have any initializers except empty parentheses. |
2407 | // Initializer lists are also allowed, in C++11. Rely on the parser for the |
2408 | // dialect distinction. |
2409 | if (ArraySize && !isLegalArrayNewInitializer(Style: InitStyle, Init: Initializer, |
2410 | IsCPlusPlus20: getLangOpts().CPlusPlus20)) { |
2411 | SourceRange InitRange(Exprs.front()->getBeginLoc(), |
2412 | Exprs.back()->getEndLoc()); |
2413 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
2414 | return ExprError(); |
2415 | } |
2416 | |
2417 | // If we can perform the initialization, and we've not already done so, |
2418 | // do it now. |
2419 | if (!AllocType->isDependentType() && |
2420 | !Expr::hasAnyTypeDependentArguments(Exprs)) { |
2421 | // The type we initialize is the complete type, including the array bound. |
2422 | QualType InitType; |
2423 | if (KnownArraySize) |
2424 | InitType = Context.getConstantArrayType( |
2425 | EltTy: AllocType, |
2426 | ArySize: llvm::APInt(Context.getTypeSize(T: Context.getSizeType()), |
2427 | *KnownArraySize), |
2428 | SizeExpr: *ArraySize, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
2429 | else if (ArraySize) |
2430 | InitType = Context.getIncompleteArrayType(EltTy: AllocType, |
2431 | ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
2432 | else |
2433 | InitType = AllocType; |
2434 | |
2435 | InitializedEntity Entity |
2436 | = InitializedEntity::InitializeNew(NewLoc: StartLoc, Type: InitType); |
2437 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs); |
2438 | ExprResult FullInit = InitSeq.Perform(S&: *this, Entity, Kind, Args: Exprs); |
2439 | if (FullInit.isInvalid()) |
2440 | return ExprError(); |
2441 | |
2442 | // FullInit is our initializer; strip off CXXBindTemporaryExprs, because |
2443 | // we don't want the initialized object to be destructed. |
2444 | // FIXME: We should not create these in the first place. |
2445 | if (CXXBindTemporaryExpr *Binder = |
2446 | dyn_cast_or_null<CXXBindTemporaryExpr>(Val: FullInit.get())) |
2447 | FullInit = Binder->getSubExpr(); |
2448 | |
2449 | Initializer = FullInit.get(); |
2450 | |
2451 | // FIXME: If we have a KnownArraySize, check that the array bound of the |
2452 | // initializer is no greater than that constant value. |
2453 | |
2454 | if (ArraySize && !*ArraySize) { |
2455 | auto *CAT = Context.getAsConstantArrayType(T: Initializer->getType()); |
2456 | if (CAT) { |
2457 | // FIXME: Track that the array size was inferred rather than explicitly |
2458 | // specified. |
2459 | ArraySize = IntegerLiteral::Create( |
2460 | C: Context, V: CAT->getSize(), type: Context.getSizeType(), l: TypeRange.getEnd()); |
2461 | } else { |
2462 | Diag(TypeRange.getEnd(), diag::err_new_array_size_unknown_from_init) |
2463 | << Initializer->getSourceRange(); |
2464 | } |
2465 | } |
2466 | } |
2467 | |
2468 | // Mark the new and delete operators as referenced. |
2469 | if (OperatorNew) { |
2470 | if (DiagnoseUseOfDecl(OperatorNew, StartLoc)) |
2471 | return ExprError(); |
2472 | MarkFunctionReferenced(Loc: StartLoc, Func: OperatorNew); |
2473 | } |
2474 | if (OperatorDelete) { |
2475 | if (DiagnoseUseOfDecl(OperatorDelete, StartLoc)) |
2476 | return ExprError(); |
2477 | MarkFunctionReferenced(Loc: StartLoc, Func: OperatorDelete); |
2478 | } |
2479 | |
2480 | return CXXNewExpr::Create(Ctx: Context, IsGlobalNew: UseGlobal, OperatorNew, OperatorDelete, |
2481 | ShouldPassAlignment: PassAlignment, UsualArrayDeleteWantsSize, |
2482 | PlacementArgs, TypeIdParens, ArraySize, InitializationStyle: InitStyle, |
2483 | Initializer, Ty: ResultType, AllocatedTypeInfo: AllocTypeInfo, Range, |
2484 | DirectInitRange); |
2485 | } |
2486 | |
2487 | /// Checks that a type is suitable as the allocated type |
2488 | /// in a new-expression. |
2489 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
2490 | SourceRange R) { |
2491 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
2492 | // abstract class type or array thereof. |
2493 | if (AllocType->isFunctionType()) |
2494 | return Diag(Loc, diag::err_bad_new_type) |
2495 | << AllocType << 0 << R; |
2496 | else if (AllocType->isReferenceType()) |
2497 | return Diag(Loc, diag::err_bad_new_type) |
2498 | << AllocType << 1 << R; |
2499 | else if (!AllocType->isDependentType() && |
2500 | RequireCompleteSizedType( |
2501 | Loc, AllocType, diag::err_new_incomplete_or_sizeless_type, R)) |
2502 | return true; |
2503 | else if (RequireNonAbstractType(Loc, AllocType, |
2504 | diag::err_allocation_of_abstract_type)) |
2505 | return true; |
2506 | else if (AllocType->isVariablyModifiedType()) |
2507 | return Diag(Loc, diag::err_variably_modified_new_type) |
2508 | << AllocType; |
2509 | else if (AllocType.getAddressSpace() != LangAS::Default && |
2510 | !getLangOpts().OpenCLCPlusPlus) |
2511 | return Diag(Loc, diag::err_address_space_qualified_new) |
2512 | << AllocType.getUnqualifiedType() |
2513 | << AllocType.getQualifiers().getAddressSpaceAttributePrintValue(); |
2514 | else if (getLangOpts().ObjCAutoRefCount) { |
2515 | if (const ArrayType *AT = Context.getAsArrayType(T: AllocType)) { |
2516 | QualType BaseAllocType = Context.getBaseElementType(VAT: AT); |
2517 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && |
2518 | BaseAllocType->isObjCLifetimeType()) |
2519 | return Diag(Loc, diag::err_arc_new_array_without_ownership) |
2520 | << BaseAllocType; |
2521 | } |
2522 | } |
2523 | |
2524 | return false; |
2525 | } |
2526 | |
2527 | static bool resolveAllocationOverload( |
2528 | Sema &S, LookupResult &R, SourceRange Range, SmallVectorImpl<Expr *> &Args, |
2529 | bool &PassAlignment, FunctionDecl *&Operator, |
2530 | OverloadCandidateSet *AlignedCandidates, Expr *AlignArg, bool Diagnose) { |
2531 | OverloadCandidateSet Candidates(R.getNameLoc(), |
2532 | OverloadCandidateSet::CSK_Normal); |
2533 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
2534 | Alloc != AllocEnd; ++Alloc) { |
2535 | // Even member operator new/delete are implicitly treated as |
2536 | // static, so don't use AddMemberCandidate. |
2537 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
2538 | |
2539 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(Val: D)) { |
2540 | S.AddTemplateOverloadCandidate(FunctionTemplate: FnTemplate, FoundDecl: Alloc.getPair(), |
2541 | /*ExplicitTemplateArgs=*/nullptr, Args, |
2542 | CandidateSet&: Candidates, |
2543 | /*SuppressUserConversions=*/false); |
2544 | continue; |
2545 | } |
2546 | |
2547 | FunctionDecl *Fn = cast<FunctionDecl>(Val: D); |
2548 | S.AddOverloadCandidate(Function: Fn, FoundDecl: Alloc.getPair(), Args, CandidateSet&: Candidates, |
2549 | /*SuppressUserConversions=*/false); |
2550 | } |
2551 | |
2552 | // Do the resolution. |
2553 | OverloadCandidateSet::iterator Best; |
2554 | switch (Candidates.BestViableFunction(S, Loc: R.getNameLoc(), Best)) { |
2555 | case OR_Success: { |
2556 | // Got one! |
2557 | FunctionDecl *FnDecl = Best->Function; |
2558 | if (S.CheckAllocationAccess(OperatorLoc: R.getNameLoc(), PlacementRange: Range, NamingClass: R.getNamingClass(), |
2559 | FoundDecl: Best->FoundDecl) == Sema::AR_inaccessible) |
2560 | return true; |
2561 | |
2562 | Operator = FnDecl; |
2563 | return false; |
2564 | } |
2565 | |
2566 | case OR_No_Viable_Function: |
2567 | // C++17 [expr.new]p13: |
2568 | // If no matching function is found and the allocated object type has |
2569 | // new-extended alignment, the alignment argument is removed from the |
2570 | // argument list, and overload resolution is performed again. |
2571 | if (PassAlignment) { |
2572 | PassAlignment = false; |
2573 | AlignArg = Args[1]; |
2574 | Args.erase(CI: Args.begin() + 1); |
2575 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
2576 | Operator, AlignedCandidates: &Candidates, AlignArg, |
2577 | Diagnose); |
2578 | } |
2579 | |
2580 | // MSVC will fall back on trying to find a matching global operator new |
2581 | // if operator new[] cannot be found. Also, MSVC will leak by not |
2582 | // generating a call to operator delete or operator delete[], but we |
2583 | // will not replicate that bug. |
2584 | // FIXME: Find out how this interacts with the std::align_val_t fallback |
2585 | // once MSVC implements it. |
2586 | if (R.getLookupName().getCXXOverloadedOperator() == OO_Array_New && |
2587 | S.Context.getLangOpts().MSVCCompat) { |
2588 | R.clear(); |
2589 | R.setLookupName(S.Context.DeclarationNames.getCXXOperatorName(Op: OO_New)); |
2590 | S.LookupQualifiedName(R, S.Context.getTranslationUnitDecl()); |
2591 | // FIXME: This will give bad diagnostics pointing at the wrong functions. |
2592 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
2593 | Operator, /*Candidates=*/AlignedCandidates: nullptr, |
2594 | /*AlignArg=*/nullptr, Diagnose); |
2595 | } |
2596 | |
2597 | if (Diagnose) { |
2598 | // If this is an allocation of the form 'new (p) X' for some object |
2599 | // pointer p (or an expression that will decay to such a pointer), |
2600 | // diagnose the missing inclusion of <new>. |
2601 | if (!R.isClassLookup() && Args.size() == 2 && |
2602 | (Args[1]->getType()->isObjectPointerType() || |
2603 | Args[1]->getType()->isArrayType())) { |
2604 | S.Diag(R.getNameLoc(), diag::err_need_header_before_placement_new) |
2605 | << R.getLookupName() << Range; |
2606 | // Listing the candidates is unlikely to be useful; skip it. |
2607 | return true; |
2608 | } |
2609 | |
2610 | // Finish checking all candidates before we note any. This checking can |
2611 | // produce additional diagnostics so can't be interleaved with our |
2612 | // emission of notes. |
2613 | // |
2614 | // For an aligned allocation, separately check the aligned and unaligned |
2615 | // candidates with their respective argument lists. |
2616 | SmallVector<OverloadCandidate*, 32> Cands; |
2617 | SmallVector<OverloadCandidate*, 32> AlignedCands; |
2618 | llvm::SmallVector<Expr*, 4> AlignedArgs; |
2619 | if (AlignedCandidates) { |
2620 | auto IsAligned = [](OverloadCandidate &C) { |
2621 | return C.Function->getNumParams() > 1 && |
2622 | C.Function->getParamDecl(1)->getType()->isAlignValT(); |
2623 | }; |
2624 | auto IsUnaligned = [&](OverloadCandidate &C) { return !IsAligned(C); }; |
2625 | |
2626 | AlignedArgs.reserve(N: Args.size() + 1); |
2627 | AlignedArgs.push_back(Elt: Args[0]); |
2628 | AlignedArgs.push_back(Elt: AlignArg); |
2629 | AlignedArgs.append(in_start: Args.begin() + 1, in_end: Args.end()); |
2630 | AlignedCands = AlignedCandidates->CompleteCandidates( |
2631 | S, OCD_AllCandidates, AlignedArgs, R.getNameLoc(), IsAligned); |
2632 | |
2633 | Cands = Candidates.CompleteCandidates(S, OCD_AllCandidates, Args, |
2634 | R.getNameLoc(), IsUnaligned); |
2635 | } else { |
2636 | Cands = Candidates.CompleteCandidates(S, OCD: OCD_AllCandidates, Args, |
2637 | OpLoc: R.getNameLoc()); |
2638 | } |
2639 | |
2640 | S.Diag(R.getNameLoc(), diag::err_ovl_no_viable_function_in_call) |
2641 | << R.getLookupName() << Range; |
2642 | if (AlignedCandidates) |
2643 | AlignedCandidates->NoteCandidates(S, Args: AlignedArgs, Cands: AlignedCands, Opc: "" , |
2644 | OpLoc: R.getNameLoc()); |
2645 | Candidates.NoteCandidates(S, Args, Cands, Opc: "" , OpLoc: R.getNameLoc()); |
2646 | } |
2647 | return true; |
2648 | |
2649 | case OR_Ambiguous: |
2650 | if (Diagnose) { |
2651 | Candidates.NoteCandidates( |
2652 | PartialDiagnosticAt(R.getNameLoc(), |
2653 | S.PDiag(diag::err_ovl_ambiguous_call) |
2654 | << R.getLookupName() << Range), |
2655 | S, OCD_AmbiguousCandidates, Args); |
2656 | } |
2657 | return true; |
2658 | |
2659 | case OR_Deleted: { |
2660 | if (Diagnose) { |
2661 | Candidates.NoteCandidates( |
2662 | PartialDiagnosticAt(R.getNameLoc(), |
2663 | S.PDiag(diag::err_ovl_deleted_call) |
2664 | << R.getLookupName() << Range), |
2665 | S, OCD_AllCandidates, Args); |
2666 | } |
2667 | return true; |
2668 | } |
2669 | } |
2670 | llvm_unreachable("Unreachable, bad result from BestViableFunction" ); |
2671 | } |
2672 | |
2673 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
2674 | AllocationFunctionScope NewScope, |
2675 | AllocationFunctionScope DeleteScope, |
2676 | QualType AllocType, bool IsArray, |
2677 | bool &PassAlignment, MultiExprArg PlaceArgs, |
2678 | FunctionDecl *&OperatorNew, |
2679 | FunctionDecl *&OperatorDelete, |
2680 | bool Diagnose) { |
2681 | // --- Choosing an allocation function --- |
2682 | // C++ 5.3.4p8 - 14 & 18 |
2683 | // 1) If looking in AFS_Global scope for allocation functions, only look in |
2684 | // the global scope. Else, if AFS_Class, only look in the scope of the |
2685 | // allocated class. If AFS_Both, look in both. |
2686 | // 2) If an array size is given, look for operator new[], else look for |
2687 | // operator new. |
2688 | // 3) The first argument is always size_t. Append the arguments from the |
2689 | // placement form. |
2690 | |
2691 | SmallVector<Expr*, 8> AllocArgs; |
2692 | AllocArgs.reserve(N: (PassAlignment ? 2 : 1) + PlaceArgs.size()); |
2693 | |
2694 | // We don't care about the actual value of these arguments. |
2695 | // FIXME: Should the Sema create the expression and embed it in the syntax |
2696 | // tree? Or should the consumer just recalculate the value? |
2697 | // FIXME: Using a dummy value will interact poorly with attribute enable_if. |
2698 | QualType SizeTy = Context.getSizeType(); |
2699 | unsigned SizeTyWidth = Context.getTypeSize(T: SizeTy); |
2700 | IntegerLiteral Size(Context, llvm::APInt::getZero(numBits: SizeTyWidth), SizeTy, |
2701 | SourceLocation()); |
2702 | AllocArgs.push_back(&Size); |
2703 | |
2704 | QualType AlignValT = Context.VoidTy; |
2705 | if (PassAlignment) { |
2706 | DeclareGlobalNewDelete(); |
2707 | AlignValT = Context.getTypeDeclType(getStdAlignValT()); |
2708 | } |
2709 | CXXScalarValueInitExpr Align(AlignValT, nullptr, SourceLocation()); |
2710 | if (PassAlignment) |
2711 | AllocArgs.push_back(&Align); |
2712 | |
2713 | AllocArgs.insert(I: AllocArgs.end(), From: PlaceArgs.begin(), To: PlaceArgs.end()); |
2714 | |
2715 | // C++ [expr.new]p8: |
2716 | // If the allocated type is a non-array type, the allocation |
2717 | // function's name is operator new and the deallocation function's |
2718 | // name is operator delete. If the allocated type is an array |
2719 | // type, the allocation function's name is operator new[] and the |
2720 | // deallocation function's name is operator delete[]. |
2721 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
2722 | Op: IsArray ? OO_Array_New : OO_New); |
2723 | |
2724 | QualType AllocElemType = Context.getBaseElementType(QT: AllocType); |
2725 | |
2726 | // Find the allocation function. |
2727 | { |
2728 | LookupResult R(*this, NewName, StartLoc, LookupOrdinaryName); |
2729 | |
2730 | // C++1z [expr.new]p9: |
2731 | // If the new-expression begins with a unary :: operator, the allocation |
2732 | // function's name is looked up in the global scope. Otherwise, if the |
2733 | // allocated type is a class type T or array thereof, the allocation |
2734 | // function's name is looked up in the scope of T. |
2735 | if (AllocElemType->isRecordType() && NewScope != AFS_Global) |
2736 | LookupQualifiedName(R, AllocElemType->getAsCXXRecordDecl()); |
2737 | |
2738 | // We can see ambiguity here if the allocation function is found in |
2739 | // multiple base classes. |
2740 | if (R.isAmbiguous()) |
2741 | return true; |
2742 | |
2743 | // If this lookup fails to find the name, or if the allocated type is not |
2744 | // a class type, the allocation function's name is looked up in the |
2745 | // global scope. |
2746 | if (R.empty()) { |
2747 | if (NewScope == AFS_Class) |
2748 | return true; |
2749 | |
2750 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
2751 | } |
2752 | |
2753 | if (getLangOpts().OpenCLCPlusPlus && R.empty()) { |
2754 | if (PlaceArgs.empty()) { |
2755 | Diag(StartLoc, diag::err_openclcxx_not_supported) << "default new" ; |
2756 | } else { |
2757 | Diag(StartLoc, diag::err_openclcxx_placement_new); |
2758 | } |
2759 | return true; |
2760 | } |
2761 | |
2762 | assert(!R.empty() && "implicitly declared allocation functions not found" ); |
2763 | assert(!R.isAmbiguous() && "global allocation functions are ambiguous" ); |
2764 | |
2765 | // We do our own custom access checks below. |
2766 | R.suppressDiagnostics(); |
2767 | |
2768 | if (resolveAllocationOverload(S&: *this, R, Range, Args&: AllocArgs, PassAlignment, |
2769 | Operator&: OperatorNew, /*Candidates=*/AlignedCandidates: nullptr, |
2770 | /*AlignArg=*/nullptr, Diagnose)) |
2771 | return true; |
2772 | } |
2773 | |
2774 | // We don't need an operator delete if we're running under -fno-exceptions. |
2775 | if (!getLangOpts().Exceptions) { |
2776 | OperatorDelete = nullptr; |
2777 | return false; |
2778 | } |
2779 | |
2780 | // Note, the name of OperatorNew might have been changed from array to |
2781 | // non-array by resolveAllocationOverload. |
2782 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
2783 | Op: OperatorNew->getDeclName().getCXXOverloadedOperator() == OO_Array_New |
2784 | ? OO_Array_Delete |
2785 | : OO_Delete); |
2786 | |
2787 | // C++ [expr.new]p19: |
2788 | // |
2789 | // If the new-expression begins with a unary :: operator, the |
2790 | // deallocation function's name is looked up in the global |
2791 | // scope. Otherwise, if the allocated type is a class type T or an |
2792 | // array thereof, the deallocation function's name is looked up in |
2793 | // the scope of T. If this lookup fails to find the name, or if |
2794 | // the allocated type is not a class type or array thereof, the |
2795 | // deallocation function's name is looked up in the global scope. |
2796 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
2797 | if (AllocElemType->isRecordType() && DeleteScope != AFS_Global) { |
2798 | auto *RD = |
2799 | cast<CXXRecordDecl>(Val: AllocElemType->castAs<RecordType>()->getDecl()); |
2800 | LookupQualifiedName(FoundDelete, RD); |
2801 | } |
2802 | if (FoundDelete.isAmbiguous()) |
2803 | return true; // FIXME: clean up expressions? |
2804 | |
2805 | // Filter out any destroying operator deletes. We can't possibly call such a |
2806 | // function in this context, because we're handling the case where the object |
2807 | // was not successfully constructed. |
2808 | // FIXME: This is not covered by the language rules yet. |
2809 | { |
2810 | LookupResult::Filter Filter = FoundDelete.makeFilter(); |
2811 | while (Filter.hasNext()) { |
2812 | auto *FD = dyn_cast<FunctionDecl>(Val: Filter.next()->getUnderlyingDecl()); |
2813 | if (FD && FD->isDestroyingOperatorDelete()) |
2814 | Filter.erase(); |
2815 | } |
2816 | Filter.done(); |
2817 | } |
2818 | |
2819 | bool FoundGlobalDelete = FoundDelete.empty(); |
2820 | if (FoundDelete.empty()) { |
2821 | FoundDelete.clear(Kind: LookupOrdinaryName); |
2822 | |
2823 | if (DeleteScope == AFS_Class) |
2824 | return true; |
2825 | |
2826 | DeclareGlobalNewDelete(); |
2827 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
2828 | } |
2829 | |
2830 | FoundDelete.suppressDiagnostics(); |
2831 | |
2832 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
2833 | |
2834 | // Whether we're looking for a placement operator delete is dictated |
2835 | // by whether we selected a placement operator new, not by whether |
2836 | // we had explicit placement arguments. This matters for things like |
2837 | // struct A { void *operator new(size_t, int = 0); ... }; |
2838 | // A *a = new A() |
2839 | // |
2840 | // We don't have any definition for what a "placement allocation function" |
2841 | // is, but we assume it's any allocation function whose |
2842 | // parameter-declaration-clause is anything other than (size_t). |
2843 | // |
2844 | // FIXME: Should (size_t, std::align_val_t) also be considered non-placement? |
2845 | // This affects whether an exception from the constructor of an overaligned |
2846 | // type uses the sized or non-sized form of aligned operator delete. |
2847 | bool isPlacementNew = !PlaceArgs.empty() || OperatorNew->param_size() != 1 || |
2848 | OperatorNew->isVariadic(); |
2849 | |
2850 | if (isPlacementNew) { |
2851 | // C++ [expr.new]p20: |
2852 | // A declaration of a placement deallocation function matches the |
2853 | // declaration of a placement allocation function if it has the |
2854 | // same number of parameters and, after parameter transformations |
2855 | // (8.3.5), all parameter types except the first are |
2856 | // identical. [...] |
2857 | // |
2858 | // To perform this comparison, we compute the function type that |
2859 | // the deallocation function should have, and use that type both |
2860 | // for template argument deduction and for comparison purposes. |
2861 | QualType ExpectedFunctionType; |
2862 | { |
2863 | auto *Proto = OperatorNew->getType()->castAs<FunctionProtoType>(); |
2864 | |
2865 | SmallVector<QualType, 4> ArgTypes; |
2866 | ArgTypes.push_back(Elt: Context.VoidPtrTy); |
2867 | for (unsigned I = 1, N = Proto->getNumParams(); I < N; ++I) |
2868 | ArgTypes.push_back(Elt: Proto->getParamType(I)); |
2869 | |
2870 | FunctionProtoType::ExtProtoInfo EPI; |
2871 | // FIXME: This is not part of the standard's rule. |
2872 | EPI.Variadic = Proto->isVariadic(); |
2873 | |
2874 | ExpectedFunctionType |
2875 | = Context.getFunctionType(ResultTy: Context.VoidTy, Args: ArgTypes, EPI); |
2876 | } |
2877 | |
2878 | for (LookupResult::iterator D = FoundDelete.begin(), |
2879 | DEnd = FoundDelete.end(); |
2880 | D != DEnd; ++D) { |
2881 | FunctionDecl *Fn = nullptr; |
2882 | if (FunctionTemplateDecl *FnTmpl = |
2883 | dyn_cast<FunctionTemplateDecl>(Val: (*D)->getUnderlyingDecl())) { |
2884 | // Perform template argument deduction to try to match the |
2885 | // expected function type. |
2886 | TemplateDeductionInfo Info(StartLoc); |
2887 | if (DeduceTemplateArguments(FunctionTemplate: FnTmpl, ExplicitTemplateArgs: nullptr, ArgFunctionType: ExpectedFunctionType, Specialization&: Fn, |
2888 | Info) != TemplateDeductionResult::Success) |
2889 | continue; |
2890 | } else |
2891 | Fn = cast<FunctionDecl>(Val: (*D)->getUnderlyingDecl()); |
2892 | |
2893 | if (Context.hasSameType(adjustCCAndNoReturn(ArgFunctionType: Fn->getType(), |
2894 | FunctionType: ExpectedFunctionType, |
2895 | /*AdjustExcpetionSpec*/AdjustExceptionSpec: true), |
2896 | ExpectedFunctionType)) |
2897 | Matches.push_back(Elt: std::make_pair(x: D.getPair(), y&: Fn)); |
2898 | } |
2899 | |
2900 | if (getLangOpts().CUDA) |
2901 | EraseUnwantedCUDAMatches(Caller: getCurFunctionDecl(/*AllowLambda=*/true), |
2902 | Matches); |
2903 | } else { |
2904 | // C++1y [expr.new]p22: |
2905 | // For a non-placement allocation function, the normal deallocation |
2906 | // function lookup is used |
2907 | // |
2908 | // Per [expr.delete]p10, this lookup prefers a member operator delete |
2909 | // without a size_t argument, but prefers a non-member operator delete |
2910 | // with a size_t where possible (which it always is in this case). |
2911 | llvm::SmallVector<UsualDeallocFnInfo, 4> BestDeallocFns; |
2912 | UsualDeallocFnInfo Selected = resolveDeallocationOverload( |
2913 | S&: *this, R&: FoundDelete, /*WantSize*/ FoundGlobalDelete, |
2914 | /*WantAlign*/ hasNewExtendedAlignment(S&: *this, AllocType: AllocElemType), |
2915 | BestFns: &BestDeallocFns); |
2916 | if (Selected) |
2917 | Matches.push_back(Elt: std::make_pair(x&: Selected.Found, y&: Selected.FD)); |
2918 | else { |
2919 | // If we failed to select an operator, all remaining functions are viable |
2920 | // but ambiguous. |
2921 | for (auto Fn : BestDeallocFns) |
2922 | Matches.push_back(Elt: std::make_pair(x&: Fn.Found, y&: Fn.FD)); |
2923 | } |
2924 | } |
2925 | |
2926 | // C++ [expr.new]p20: |
2927 | // [...] If the lookup finds a single matching deallocation |
2928 | // function, that function will be called; otherwise, no |
2929 | // deallocation function will be called. |
2930 | if (Matches.size() == 1) { |
2931 | OperatorDelete = Matches[0].second; |
2932 | |
2933 | // C++1z [expr.new]p23: |
2934 | // If the lookup finds a usual deallocation function (3.7.4.2) |
2935 | // with a parameter of type std::size_t and that function, considered |
2936 | // as a placement deallocation function, would have been |
2937 | // selected as a match for the allocation function, the program |
2938 | // is ill-formed. |
2939 | if (getLangOpts().CPlusPlus11 && isPlacementNew && |
2940 | isNonPlacementDeallocationFunction(S&: *this, FD: OperatorDelete)) { |
2941 | UsualDeallocFnInfo Info(*this, |
2942 | DeclAccessPair::make(OperatorDelete, AS_public)); |
2943 | // Core issue, per mail to core reflector, 2016-10-09: |
2944 | // If this is a member operator delete, and there is a corresponding |
2945 | // non-sized member operator delete, this isn't /really/ a sized |
2946 | // deallocation function, it just happens to have a size_t parameter. |
2947 | bool IsSizedDelete = Info.HasSizeT; |
2948 | if (IsSizedDelete && !FoundGlobalDelete) { |
2949 | auto NonSizedDelete = |
2950 | resolveDeallocationOverload(S&: *this, R&: FoundDelete, /*WantSize*/false, |
2951 | /*WantAlign*/Info.HasAlignValT); |
2952 | if (NonSizedDelete && !NonSizedDelete.HasSizeT && |
2953 | NonSizedDelete.HasAlignValT == Info.HasAlignValT) |
2954 | IsSizedDelete = false; |
2955 | } |
2956 | |
2957 | if (IsSizedDelete) { |
2958 | SourceRange R = PlaceArgs.empty() |
2959 | ? SourceRange() |
2960 | : SourceRange(PlaceArgs.front()->getBeginLoc(), |
2961 | PlaceArgs.back()->getEndLoc()); |
2962 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) << R; |
2963 | if (!OperatorDelete->isImplicit()) |
2964 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
2965 | << DeleteName; |
2966 | } |
2967 | } |
2968 | |
2969 | CheckAllocationAccess(OperatorLoc: StartLoc, PlacementRange: Range, NamingClass: FoundDelete.getNamingClass(), |
2970 | FoundDecl: Matches[0].first); |
2971 | } else if (!Matches.empty()) { |
2972 | // We found multiple suitable operators. Per [expr.new]p20, that means we |
2973 | // call no 'operator delete' function, but we should at least warn the user. |
2974 | // FIXME: Suppress this warning if the construction cannot throw. |
2975 | Diag(StartLoc, diag::warn_ambiguous_suitable_delete_function_found) |
2976 | << DeleteName << AllocElemType; |
2977 | |
2978 | for (auto &Match : Matches) |
2979 | Diag(Match.second->getLocation(), |
2980 | diag::note_member_declared_here) << DeleteName; |
2981 | } |
2982 | |
2983 | return false; |
2984 | } |
2985 | |
2986 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
2987 | /// delete. These are: |
2988 | /// @code |
2989 | /// // C++03: |
2990 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
2991 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
2992 | /// void operator delete(void *) throw(); |
2993 | /// void operator delete[](void *) throw(); |
2994 | /// // C++11: |
2995 | /// void* operator new(std::size_t); |
2996 | /// void* operator new[](std::size_t); |
2997 | /// void operator delete(void *) noexcept; |
2998 | /// void operator delete[](void *) noexcept; |
2999 | /// // C++1y: |
3000 | /// void* operator new(std::size_t); |
3001 | /// void* operator new[](std::size_t); |
3002 | /// void operator delete(void *) noexcept; |
3003 | /// void operator delete[](void *) noexcept; |
3004 | /// void operator delete(void *, std::size_t) noexcept; |
3005 | /// void operator delete[](void *, std::size_t) noexcept; |
3006 | /// @endcode |
3007 | /// Note that the placement and nothrow forms of new are *not* implicitly |
3008 | /// declared. Their use requires including \<new\>. |
3009 | void Sema::DeclareGlobalNewDelete() { |
3010 | if (GlobalNewDeleteDeclared) |
3011 | return; |
3012 | |
3013 | // The implicitly declared new and delete operators |
3014 | // are not supported in OpenCL. |
3015 | if (getLangOpts().OpenCLCPlusPlus) |
3016 | return; |
3017 | |
3018 | // C++ [basic.stc.dynamic.general]p2: |
3019 | // The library provides default definitions for the global allocation |
3020 | // and deallocation functions. Some global allocation and deallocation |
3021 | // functions are replaceable ([new.delete]); these are attached to the |
3022 | // global module ([module.unit]). |
3023 | if (getLangOpts().CPlusPlusModules && getCurrentModule()) |
3024 | PushGlobalModuleFragment(BeginLoc: SourceLocation()); |
3025 | |
3026 | // C++ [basic.std.dynamic]p2: |
3027 | // [...] The following allocation and deallocation functions (18.4) are |
3028 | // implicitly declared in global scope in each translation unit of a |
3029 | // program |
3030 | // |
3031 | // C++03: |
3032 | // void* operator new(std::size_t) throw(std::bad_alloc); |
3033 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
3034 | // void operator delete(void*) throw(); |
3035 | // void operator delete[](void*) throw(); |
3036 | // C++11: |
3037 | // void* operator new(std::size_t); |
3038 | // void* operator new[](std::size_t); |
3039 | // void operator delete(void*) noexcept; |
3040 | // void operator delete[](void*) noexcept; |
3041 | // C++1y: |
3042 | // void* operator new(std::size_t); |
3043 | // void* operator new[](std::size_t); |
3044 | // void operator delete(void*) noexcept; |
3045 | // void operator delete[](void*) noexcept; |
3046 | // void operator delete(void*, std::size_t) noexcept; |
3047 | // void operator delete[](void*, std::size_t) noexcept; |
3048 | // |
3049 | // These implicit declarations introduce only the function names operator |
3050 | // new, operator new[], operator delete, operator delete[]. |
3051 | // |
3052 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
3053 | // "std" or "bad_alloc" as necessary to form the exception specification. |
3054 | // However, we do not make these implicit declarations visible to name |
3055 | // lookup. |
3056 | if (!StdBadAlloc && !getLangOpts().CPlusPlus11) { |
3057 | // The "std::bad_alloc" class has not yet been declared, so build it |
3058 | // implicitly. |
3059 | StdBadAlloc = CXXRecordDecl::Create( |
3060 | Context, TagTypeKind::Class, getOrCreateStdNamespace(), |
3061 | SourceLocation(), SourceLocation(), |
3062 | &PP.getIdentifierTable().get(Name: "bad_alloc" ), nullptr); |
3063 | getStdBadAlloc()->setImplicit(true); |
3064 | |
3065 | // The implicitly declared "std::bad_alloc" should live in global module |
3066 | // fragment. |
3067 | if (TheGlobalModuleFragment) { |
3068 | getStdBadAlloc()->setModuleOwnershipKind( |
3069 | Decl::ModuleOwnershipKind::ReachableWhenImported); |
3070 | getStdBadAlloc()->setLocalOwningModule(TheGlobalModuleFragment); |
3071 | } |
3072 | } |
3073 | if (!StdAlignValT && getLangOpts().AlignedAllocation) { |
3074 | // The "std::align_val_t" enum class has not yet been declared, so build it |
3075 | // implicitly. |
3076 | auto *AlignValT = EnumDecl::Create( |
3077 | Context, getOrCreateStdNamespace(), SourceLocation(), SourceLocation(), |
3078 | &PP.getIdentifierTable().get(Name: "align_val_t" ), nullptr, true, true, true); |
3079 | |
3080 | // The implicitly declared "std::align_val_t" should live in global module |
3081 | // fragment. |
3082 | if (TheGlobalModuleFragment) { |
3083 | AlignValT->setModuleOwnershipKind( |
3084 | Decl::ModuleOwnershipKind::ReachableWhenImported); |
3085 | AlignValT->setLocalOwningModule(TheGlobalModuleFragment); |
3086 | } |
3087 | |
3088 | AlignValT->setIntegerType(Context.getSizeType()); |
3089 | AlignValT->setPromotionType(Context.getSizeType()); |
3090 | AlignValT->setImplicit(true); |
3091 | |
3092 | StdAlignValT = AlignValT; |
3093 | } |
3094 | |
3095 | GlobalNewDeleteDeclared = true; |
3096 | |
3097 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
3098 | QualType SizeT = Context.getSizeType(); |
3099 | |
3100 | auto DeclareGlobalAllocationFunctions = [&](OverloadedOperatorKind Kind, |
3101 | QualType Return, QualType Param) { |
3102 | llvm::SmallVector<QualType, 3> Params; |
3103 | Params.push_back(Elt: Param); |
3104 | |
3105 | // Create up to four variants of the function (sized/aligned). |
3106 | bool HasSizedVariant = getLangOpts().SizedDeallocation && |
3107 | (Kind == OO_Delete || Kind == OO_Array_Delete); |
3108 | bool HasAlignedVariant = getLangOpts().AlignedAllocation; |
3109 | |
3110 | int NumSizeVariants = (HasSizedVariant ? 2 : 1); |
3111 | int NumAlignVariants = (HasAlignedVariant ? 2 : 1); |
3112 | for (int Sized = 0; Sized < NumSizeVariants; ++Sized) { |
3113 | if (Sized) |
3114 | Params.push_back(Elt: SizeT); |
3115 | |
3116 | for (int Aligned = 0; Aligned < NumAlignVariants; ++Aligned) { |
3117 | if (Aligned) |
3118 | Params.push_back(Elt: Context.getTypeDeclType(getStdAlignValT())); |
3119 | |
3120 | DeclareGlobalAllocationFunction( |
3121 | Name: Context.DeclarationNames.getCXXOperatorName(Op: Kind), Return, Params); |
3122 | |
3123 | if (Aligned) |
3124 | Params.pop_back(); |
3125 | } |
3126 | } |
3127 | }; |
3128 | |
3129 | DeclareGlobalAllocationFunctions(OO_New, VoidPtr, SizeT); |
3130 | DeclareGlobalAllocationFunctions(OO_Array_New, VoidPtr, SizeT); |
3131 | DeclareGlobalAllocationFunctions(OO_Delete, Context.VoidTy, VoidPtr); |
3132 | DeclareGlobalAllocationFunctions(OO_Array_Delete, Context.VoidTy, VoidPtr); |
3133 | |
3134 | if (getLangOpts().CPlusPlusModules && getCurrentModule()) |
3135 | PopGlobalModuleFragment(); |
3136 | } |
3137 | |
3138 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
3139 | /// allocation function if it doesn't already exist. |
3140 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
3141 | QualType Return, |
3142 | ArrayRef<QualType> Params) { |
3143 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
3144 | |
3145 | // Check if this function is already declared. |
3146 | DeclContext::lookup_result R = GlobalCtx->lookup(Name); |
3147 | for (DeclContext::lookup_iterator Alloc = R.begin(), AllocEnd = R.end(); |
3148 | Alloc != AllocEnd; ++Alloc) { |
3149 | // Only look at non-template functions, as it is the predefined, |
3150 | // non-templated allocation function we are trying to declare here. |
3151 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Val: *Alloc)) { |
3152 | if (Func->getNumParams() == Params.size()) { |
3153 | llvm::SmallVector<QualType, 3> FuncParams; |
3154 | for (auto *P : Func->parameters()) |
3155 | FuncParams.push_back( |
3156 | Context.getCanonicalType(P->getType().getUnqualifiedType())); |
3157 | if (llvm::ArrayRef(FuncParams) == Params) { |
3158 | // Make the function visible to name lookup, even if we found it in |
3159 | // an unimported module. It either is an implicitly-declared global |
3160 | // allocation function, or is suppressing that function. |
3161 | Func->setVisibleDespiteOwningModule(); |
3162 | return; |
3163 | } |
3164 | } |
3165 | } |
3166 | } |
3167 | |
3168 | FunctionProtoType::ExtProtoInfo EPI(Context.getDefaultCallingConvention( |
3169 | /*IsVariadic=*/false, /*IsCXXMethod=*/false, /*IsBuiltin=*/true)); |
3170 | |
3171 | QualType BadAllocType; |
3172 | bool HasBadAllocExceptionSpec |
3173 | = (Name.getCXXOverloadedOperator() == OO_New || |
3174 | Name.getCXXOverloadedOperator() == OO_Array_New); |
3175 | if (HasBadAllocExceptionSpec) { |
3176 | if (!getLangOpts().CPlusPlus11) { |
3177 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
3178 | assert(StdBadAlloc && "Must have std::bad_alloc declared" ); |
3179 | EPI.ExceptionSpec.Type = EST_Dynamic; |
3180 | EPI.ExceptionSpec.Exceptions = llvm::ArrayRef(BadAllocType); |
3181 | } |
3182 | if (getLangOpts().NewInfallible) { |
3183 | EPI.ExceptionSpec.Type = EST_DynamicNone; |
3184 | } |
3185 | } else { |
3186 | EPI.ExceptionSpec = |
3187 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
3188 | } |
3189 | |
3190 | auto CreateAllocationFunctionDecl = [&](Attr *) { |
3191 | QualType FnType = Context.getFunctionType(ResultTy: Return, Args: Params, EPI); |
3192 | FunctionDecl *Alloc = FunctionDecl::Create( |
3193 | C&: Context, DC: GlobalCtx, StartLoc: SourceLocation(), NLoc: SourceLocation(), N: Name, T: FnType, |
3194 | /*TInfo=*/nullptr, SC: SC_None, UsesFPIntrin: getCurFPFeatures().isFPConstrained(), isInlineSpecified: false, |
3195 | hasWrittenPrototype: true); |
3196 | Alloc->setImplicit(); |
3197 | // Global allocation functions should always be visible. |
3198 | Alloc->setVisibleDespiteOwningModule(); |
3199 | |
3200 | if (HasBadAllocExceptionSpec && getLangOpts().NewInfallible && |
3201 | !getLangOpts().CheckNew) |
3202 | Alloc->addAttr( |
3203 | ReturnsNonNullAttr::CreateImplicit(Context, Alloc->getLocation())); |
3204 | |
3205 | // C++ [basic.stc.dynamic.general]p2: |
3206 | // The library provides default definitions for the global allocation |
3207 | // and deallocation functions. Some global allocation and deallocation |
3208 | // functions are replaceable ([new.delete]); these are attached to the |
3209 | // global module ([module.unit]). |
3210 | // |
3211 | // In the language wording, these functions are attched to the global |
3212 | // module all the time. But in the implementation, the global module |
3213 | // is only meaningful when we're in a module unit. So here we attach |
3214 | // these allocation functions to global module conditionally. |
3215 | if (TheGlobalModuleFragment) { |
3216 | Alloc->setModuleOwnershipKind( |
3217 | Decl::ModuleOwnershipKind::ReachableWhenImported); |
3218 | Alloc->setLocalOwningModule(TheGlobalModuleFragment); |
3219 | } |
3220 | |
3221 | if (LangOpts.hasGlobalAllocationFunctionVisibility()) |
3222 | Alloc->addAttr(VisibilityAttr::CreateImplicit( |
3223 | Context, LangOpts.hasHiddenGlobalAllocationFunctionVisibility() |
3224 | ? VisibilityAttr::Hidden |
3225 | : LangOpts.hasProtectedGlobalAllocationFunctionVisibility() |
3226 | ? VisibilityAttr::Protected |
3227 | : VisibilityAttr::Default)); |
3228 | |
3229 | llvm::SmallVector<ParmVarDecl *, 3> ParamDecls; |
3230 | for (QualType T : Params) { |
3231 | ParamDecls.push_back(Elt: ParmVarDecl::Create( |
3232 | Context, Alloc, SourceLocation(), SourceLocation(), nullptr, T, |
3233 | /*TInfo=*/nullptr, SC_None, nullptr)); |
3234 | ParamDecls.back()->setImplicit(); |
3235 | } |
3236 | Alloc->setParams(ParamDecls); |
3237 | if (ExtraAttr) |
3238 | Alloc->addAttr(ExtraAttr); |
3239 | AddKnownFunctionAttributesForReplaceableGlobalAllocationFunction(FD: Alloc); |
3240 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
3241 | IdResolver.tryAddTopLevelDecl(Alloc, Name); |
3242 | }; |
3243 | |
3244 | if (!LangOpts.CUDA) |
3245 | CreateAllocationFunctionDecl(nullptr); |
3246 | else { |
3247 | // Host and device get their own declaration so each can be |
3248 | // defined or re-declared independently. |
3249 | CreateAllocationFunctionDecl(CUDAHostAttr::CreateImplicit(Context)); |
3250 | CreateAllocationFunctionDecl(CUDADeviceAttr::CreateImplicit(Context)); |
3251 | } |
3252 | } |
3253 | |
3254 | FunctionDecl *Sema::FindUsualDeallocationFunction(SourceLocation StartLoc, |
3255 | bool CanProvideSize, |
3256 | bool Overaligned, |
3257 | DeclarationName Name) { |
3258 | DeclareGlobalNewDelete(); |
3259 | |
3260 | LookupResult FoundDelete(*this, Name, StartLoc, LookupOrdinaryName); |
3261 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
3262 | |
3263 | // FIXME: It's possible for this to result in ambiguity, through a |
3264 | // user-declared variadic operator delete or the enable_if attribute. We |
3265 | // should probably not consider those cases to be usual deallocation |
3266 | // functions. But for now we just make an arbitrary choice in that case. |
3267 | auto Result = resolveDeallocationOverload(S&: *this, R&: FoundDelete, WantSize: CanProvideSize, |
3268 | WantAlign: Overaligned); |
3269 | assert(Result.FD && "operator delete missing from global scope?" ); |
3270 | return Result.FD; |
3271 | } |
3272 | |
3273 | FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc, |
3274 | CXXRecordDecl *RD) { |
3275 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op: OO_Delete); |
3276 | |
3277 | FunctionDecl *OperatorDelete = nullptr; |
3278 | if (FindDeallocationFunction(StartLoc: Loc, RD, Name, Operator&: OperatorDelete)) |
3279 | return nullptr; |
3280 | if (OperatorDelete) |
3281 | return OperatorDelete; |
3282 | |
3283 | // If there's no class-specific operator delete, look up the global |
3284 | // non-array delete. |
3285 | return FindUsualDeallocationFunction( |
3286 | StartLoc: Loc, CanProvideSize: true, Overaligned: hasNewExtendedAlignment(S&: *this, AllocType: Context.getRecordType(RD)), |
3287 | Name); |
3288 | } |
3289 | |
3290 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
3291 | DeclarationName Name, |
3292 | FunctionDecl *&Operator, bool Diagnose, |
3293 | bool WantSize, bool WantAligned) { |
3294 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
3295 | // Try to find operator delete/operator delete[] in class scope. |
3296 | LookupQualifiedName(Found, RD); |
3297 | |
3298 | if (Found.isAmbiguous()) |
3299 | return true; |
3300 | |
3301 | Found.suppressDiagnostics(); |
3302 | |
3303 | bool Overaligned = |
3304 | WantAligned || hasNewExtendedAlignment(S&: *this, AllocType: Context.getRecordType(RD)); |
3305 | |
3306 | // C++17 [expr.delete]p10: |
3307 | // If the deallocation functions have class scope, the one without a |
3308 | // parameter of type std::size_t is selected. |
3309 | llvm::SmallVector<UsualDeallocFnInfo, 4> Matches; |
3310 | resolveDeallocationOverload(S&: *this, R&: Found, /*WantSize*/ WantSize, |
3311 | /*WantAlign*/ Overaligned, BestFns: &Matches); |
3312 | |
3313 | // If we could find an overload, use it. |
3314 | if (Matches.size() == 1) { |
3315 | Operator = cast<CXXMethodDecl>(Val: Matches[0].FD); |
3316 | |
3317 | // FIXME: DiagnoseUseOfDecl? |
3318 | if (Operator->isDeleted()) { |
3319 | if (Diagnose) { |
3320 | Diag(StartLoc, diag::err_deleted_function_use); |
3321 | NoteDeletedFunction(FD: Operator); |
3322 | } |
3323 | return true; |
3324 | } |
3325 | |
3326 | if (CheckAllocationAccess(OperatorLoc: StartLoc, PlacementRange: SourceRange(), NamingClass: Found.getNamingClass(), |
3327 | FoundDecl: Matches[0].Found, Diagnose) == AR_inaccessible) |
3328 | return true; |
3329 | |
3330 | return false; |
3331 | } |
3332 | |
3333 | // We found multiple suitable operators; complain about the ambiguity. |
3334 | // FIXME: The standard doesn't say to do this; it appears that the intent |
3335 | // is that this should never happen. |
3336 | if (!Matches.empty()) { |
3337 | if (Diagnose) { |
3338 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
3339 | << Name << RD; |
3340 | for (auto &Match : Matches) |
3341 | Diag(Match.FD->getLocation(), diag::note_member_declared_here) << Name; |
3342 | } |
3343 | return true; |
3344 | } |
3345 | |
3346 | // We did find operator delete/operator delete[] declarations, but |
3347 | // none of them were suitable. |
3348 | if (!Found.empty()) { |
3349 | if (Diagnose) { |
3350 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
3351 | << Name << RD; |
3352 | |
3353 | for (NamedDecl *D : Found) |
3354 | Diag(D->getUnderlyingDecl()->getLocation(), |
3355 | diag::note_member_declared_here) << Name; |
3356 | } |
3357 | return true; |
3358 | } |
3359 | |
3360 | Operator = nullptr; |
3361 | return false; |
3362 | } |
3363 | |
3364 | namespace { |
3365 | /// Checks whether delete-expression, and new-expression used for |
3366 | /// initializing deletee have the same array form. |
3367 | class MismatchingNewDeleteDetector { |
3368 | public: |
3369 | enum MismatchResult { |
3370 | /// Indicates that there is no mismatch or a mismatch cannot be proven. |
3371 | NoMismatch, |
3372 | /// Indicates that variable is initialized with mismatching form of \a new. |
3373 | VarInitMismatches, |
3374 | /// Indicates that member is initialized with mismatching form of \a new. |
3375 | MemberInitMismatches, |
3376 | /// Indicates that 1 or more constructors' definitions could not been |
3377 | /// analyzed, and they will be checked again at the end of translation unit. |
3378 | AnalyzeLater |
3379 | }; |
3380 | |
3381 | /// \param EndOfTU True, if this is the final analysis at the end of |
3382 | /// translation unit. False, if this is the initial analysis at the point |
3383 | /// delete-expression was encountered. |
3384 | explicit MismatchingNewDeleteDetector(bool EndOfTU) |
3385 | : Field(nullptr), IsArrayForm(false), EndOfTU(EndOfTU), |
3386 | HasUndefinedConstructors(false) {} |
3387 | |
3388 | /// Checks whether pointee of a delete-expression is initialized with |
3389 | /// matching form of new-expression. |
3390 | /// |
3391 | /// If return value is \c VarInitMismatches or \c MemberInitMismatches at the |
3392 | /// point where delete-expression is encountered, then a warning will be |
3393 | /// issued immediately. If return value is \c AnalyzeLater at the point where |
3394 | /// delete-expression is seen, then member will be analyzed at the end of |
3395 | /// translation unit. \c AnalyzeLater is returned iff at least one constructor |
3396 | /// couldn't be analyzed. If at least one constructor initializes the member |
3397 | /// with matching type of new, the return value is \c NoMismatch. |
3398 | MismatchResult analyzeDeleteExpr(const CXXDeleteExpr *DE); |
3399 | /// Analyzes a class member. |
3400 | /// \param Field Class member to analyze. |
3401 | /// \param DeleteWasArrayForm Array form-ness of the delete-expression used |
3402 | /// for deleting the \p Field. |
3403 | MismatchResult analyzeField(FieldDecl *Field, bool DeleteWasArrayForm); |
3404 | FieldDecl *Field; |
3405 | /// List of mismatching new-expressions used for initialization of the pointee |
3406 | llvm::SmallVector<const CXXNewExpr *, 4> NewExprs; |
3407 | /// Indicates whether delete-expression was in array form. |
3408 | bool IsArrayForm; |
3409 | |
3410 | private: |
3411 | const bool EndOfTU; |
3412 | /// Indicates that there is at least one constructor without body. |
3413 | bool HasUndefinedConstructors; |
3414 | /// Returns \c CXXNewExpr from given initialization expression. |
3415 | /// \param E Expression used for initializing pointee in delete-expression. |
3416 | /// E can be a single-element \c InitListExpr consisting of new-expression. |
3417 | const CXXNewExpr *getNewExprFromInitListOrExpr(const Expr *E); |
3418 | /// Returns whether member is initialized with mismatching form of |
3419 | /// \c new either by the member initializer or in-class initialization. |
3420 | /// |
3421 | /// If bodies of all constructors are not visible at the end of translation |
3422 | /// unit or at least one constructor initializes member with the matching |
3423 | /// form of \c new, mismatch cannot be proven, and this function will return |
3424 | /// \c NoMismatch. |
3425 | MismatchResult analyzeMemberExpr(const MemberExpr *ME); |
3426 | /// Returns whether variable is initialized with mismatching form of |
3427 | /// \c new. |
3428 | /// |
3429 | /// If variable is initialized with matching form of \c new or variable is not |
3430 | /// initialized with a \c new expression, this function will return true. |
3431 | /// If variable is initialized with mismatching form of \c new, returns false. |
3432 | /// \param D Variable to analyze. |
3433 | bool hasMatchingVarInit(const DeclRefExpr *D); |
3434 | /// Checks whether the constructor initializes pointee with mismatching |
3435 | /// form of \c new. |
3436 | /// |
3437 | /// Returns true, if member is initialized with matching form of \c new in |
3438 | /// member initializer list. Returns false, if member is initialized with the |
3439 | /// matching form of \c new in this constructor's initializer or given |
3440 | /// constructor isn't defined at the point where delete-expression is seen, or |
3441 | /// member isn't initialized by the constructor. |
3442 | bool hasMatchingNewInCtor(const CXXConstructorDecl *CD); |
3443 | /// Checks whether member is initialized with matching form of |
3444 | /// \c new in member initializer list. |
3445 | bool hasMatchingNewInCtorInit(const CXXCtorInitializer *CI); |
3446 | /// Checks whether member is initialized with mismatching form of \c new by |
3447 | /// in-class initializer. |
3448 | MismatchResult analyzeInClassInitializer(); |
3449 | }; |
3450 | } |
3451 | |
3452 | MismatchingNewDeleteDetector::MismatchResult |
3453 | MismatchingNewDeleteDetector::analyzeDeleteExpr(const CXXDeleteExpr *DE) { |
3454 | NewExprs.clear(); |
3455 | assert(DE && "Expected delete-expression" ); |
3456 | IsArrayForm = DE->isArrayForm(); |
3457 | const Expr *E = DE->getArgument()->IgnoreParenImpCasts(); |
3458 | if (const MemberExpr *ME = dyn_cast<const MemberExpr>(Val: E)) { |
3459 | return analyzeMemberExpr(ME); |
3460 | } else if (const DeclRefExpr *D = dyn_cast<const DeclRefExpr>(Val: E)) { |
3461 | if (!hasMatchingVarInit(D)) |
3462 | return VarInitMismatches; |
3463 | } |
3464 | return NoMismatch; |
3465 | } |
3466 | |
3467 | const CXXNewExpr * |
3468 | MismatchingNewDeleteDetector::getNewExprFromInitListOrExpr(const Expr *E) { |
3469 | assert(E != nullptr && "Expected a valid initializer expression" ); |
3470 | E = E->IgnoreParenImpCasts(); |
3471 | if (const InitListExpr *ILE = dyn_cast<const InitListExpr>(Val: E)) { |
3472 | if (ILE->getNumInits() == 1) |
3473 | E = dyn_cast<const CXXNewExpr>(Val: ILE->getInit(Init: 0)->IgnoreParenImpCasts()); |
3474 | } |
3475 | |
3476 | return dyn_cast_or_null<const CXXNewExpr>(Val: E); |
3477 | } |
3478 | |
3479 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtorInit( |
3480 | const CXXCtorInitializer *CI) { |
3481 | const CXXNewExpr *NE = nullptr; |
3482 | if (Field == CI->getMember() && |
3483 | (NE = getNewExprFromInitListOrExpr(E: CI->getInit()))) { |
3484 | if (NE->isArray() == IsArrayForm) |
3485 | return true; |
3486 | else |
3487 | NewExprs.push_back(Elt: NE); |
3488 | } |
3489 | return false; |
3490 | } |
3491 | |
3492 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtor( |
3493 | const CXXConstructorDecl *CD) { |
3494 | if (CD->isImplicit()) |
3495 | return false; |
3496 | const FunctionDecl *Definition = CD; |
3497 | if (!CD->isThisDeclarationADefinition() && !CD->isDefined(Definition)) { |
3498 | HasUndefinedConstructors = true; |
3499 | return EndOfTU; |
3500 | } |
3501 | for (const auto *CI : cast<const CXXConstructorDecl>(Definition)->inits()) { |
3502 | if (hasMatchingNewInCtorInit(CI)) |
3503 | return true; |
3504 | } |
3505 | return false; |
3506 | } |
3507 | |
3508 | MismatchingNewDeleteDetector::MismatchResult |
3509 | MismatchingNewDeleteDetector::analyzeInClassInitializer() { |
3510 | assert(Field != nullptr && "This should be called only for members" ); |
3511 | const Expr *InitExpr = Field->getInClassInitializer(); |
3512 | if (!InitExpr) |
3513 | return EndOfTU ? NoMismatch : AnalyzeLater; |
3514 | if (const CXXNewExpr *NE = getNewExprFromInitListOrExpr(E: InitExpr)) { |
3515 | if (NE->isArray() != IsArrayForm) { |
3516 | NewExprs.push_back(Elt: NE); |
3517 | return MemberInitMismatches; |
3518 | } |
3519 | } |
3520 | return NoMismatch; |
3521 | } |
3522 | |
3523 | MismatchingNewDeleteDetector::MismatchResult |
3524 | MismatchingNewDeleteDetector::analyzeField(FieldDecl *Field, |
3525 | bool DeleteWasArrayForm) { |
3526 | assert(Field != nullptr && "Analysis requires a valid class member." ); |
3527 | this->Field = Field; |
3528 | IsArrayForm = DeleteWasArrayForm; |
3529 | const CXXRecordDecl *RD = cast<const CXXRecordDecl>(Val: Field->getParent()); |
3530 | for (const auto *CD : RD->ctors()) { |
3531 | if (hasMatchingNewInCtor(CD)) |
3532 | return NoMismatch; |
3533 | } |
3534 | if (HasUndefinedConstructors) |
3535 | return EndOfTU ? NoMismatch : AnalyzeLater; |
3536 | if (!NewExprs.empty()) |
3537 | return MemberInitMismatches; |
3538 | return Field->hasInClassInitializer() ? analyzeInClassInitializer() |
3539 | : NoMismatch; |
3540 | } |
3541 | |
3542 | MismatchingNewDeleteDetector::MismatchResult |
3543 | MismatchingNewDeleteDetector::analyzeMemberExpr(const MemberExpr *ME) { |
3544 | assert(ME != nullptr && "Expected a member expression" ); |
3545 | if (FieldDecl *F = dyn_cast<FieldDecl>(Val: ME->getMemberDecl())) |
3546 | return analyzeField(Field: F, DeleteWasArrayForm: IsArrayForm); |
3547 | return NoMismatch; |
3548 | } |
3549 | |
3550 | bool MismatchingNewDeleteDetector::hasMatchingVarInit(const DeclRefExpr *D) { |
3551 | const CXXNewExpr *NE = nullptr; |
3552 | if (const VarDecl *VD = dyn_cast<const VarDecl>(Val: D->getDecl())) { |
3553 | if (VD->hasInit() && (NE = getNewExprFromInitListOrExpr(E: VD->getInit())) && |
3554 | NE->isArray() != IsArrayForm) { |
3555 | NewExprs.push_back(Elt: NE); |
3556 | } |
3557 | } |
3558 | return NewExprs.empty(); |
3559 | } |
3560 | |
3561 | static void |
3562 | DiagnoseMismatchedNewDelete(Sema &SemaRef, SourceLocation DeleteLoc, |
3563 | const MismatchingNewDeleteDetector &Detector) { |
3564 | SourceLocation EndOfDelete = SemaRef.getLocForEndOfToken(Loc: DeleteLoc); |
3565 | FixItHint H; |
3566 | if (!Detector.IsArrayForm) |
3567 | H = FixItHint::CreateInsertion(InsertionLoc: EndOfDelete, Code: "[]" ); |
3568 | else { |
3569 | SourceLocation RSquare = Lexer::findLocationAfterToken( |
3570 | loc: DeleteLoc, TKind: tok::l_square, SM: SemaRef.getSourceManager(), |
3571 | LangOpts: SemaRef.getLangOpts(), SkipTrailingWhitespaceAndNewLine: true); |
3572 | if (RSquare.isValid()) |
3573 | H = FixItHint::CreateRemoval(RemoveRange: SourceRange(EndOfDelete, RSquare)); |
3574 | } |
3575 | SemaRef.Diag(DeleteLoc, diag::warn_mismatched_delete_new) |
3576 | << Detector.IsArrayForm << H; |
3577 | |
3578 | for (const auto *NE : Detector.NewExprs) |
3579 | SemaRef.Diag(NE->getExprLoc(), diag::note_allocated_here) |
3580 | << Detector.IsArrayForm; |
3581 | } |
3582 | |
3583 | void Sema::AnalyzeDeleteExprMismatch(const CXXDeleteExpr *DE) { |
3584 | if (Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) |
3585 | return; |
3586 | MismatchingNewDeleteDetector Detector(/*EndOfTU=*/false); |
3587 | switch (Detector.analyzeDeleteExpr(DE)) { |
3588 | case MismatchingNewDeleteDetector::VarInitMismatches: |
3589 | case MismatchingNewDeleteDetector::MemberInitMismatches: { |
3590 | DiagnoseMismatchedNewDelete(SemaRef&: *this, DeleteLoc: DE->getBeginLoc(), Detector); |
3591 | break; |
3592 | } |
3593 | case MismatchingNewDeleteDetector::AnalyzeLater: { |
3594 | DeleteExprs[Detector.Field].push_back( |
3595 | Elt: std::make_pair(x: DE->getBeginLoc(), y: DE->isArrayForm())); |
3596 | break; |
3597 | } |
3598 | case MismatchingNewDeleteDetector::NoMismatch: |
3599 | break; |
3600 | } |
3601 | } |
3602 | |
3603 | void Sema::AnalyzeDeleteExprMismatch(FieldDecl *Field, SourceLocation DeleteLoc, |
3604 | bool DeleteWasArrayForm) { |
3605 | MismatchingNewDeleteDetector Detector(/*EndOfTU=*/true); |
3606 | switch (Detector.analyzeField(Field, DeleteWasArrayForm)) { |
3607 | case MismatchingNewDeleteDetector::VarInitMismatches: |
3608 | llvm_unreachable("This analysis should have been done for class members." ); |
3609 | case MismatchingNewDeleteDetector::AnalyzeLater: |
3610 | llvm_unreachable("Analysis cannot be postponed any point beyond end of " |
3611 | "translation unit." ); |
3612 | case MismatchingNewDeleteDetector::MemberInitMismatches: |
3613 | DiagnoseMismatchedNewDelete(SemaRef&: *this, DeleteLoc, Detector); |
3614 | break; |
3615 | case MismatchingNewDeleteDetector::NoMismatch: |
3616 | break; |
3617 | } |
3618 | } |
3619 | |
3620 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
3621 | /// @code ::delete ptr; @endcode |
3622 | /// or |
3623 | /// @code delete [] ptr; @endcode |
3624 | ExprResult |
3625 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
3626 | bool ArrayForm, Expr *ExE) { |
3627 | // C++ [expr.delete]p1: |
3628 | // The operand shall have a pointer type, or a class type having a single |
3629 | // non-explicit conversion function to a pointer type. The result has type |
3630 | // void. |
3631 | // |
3632 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
3633 | |
3634 | ExprResult Ex = ExE; |
3635 | FunctionDecl *OperatorDelete = nullptr; |
3636 | bool ArrayFormAsWritten = ArrayForm; |
3637 | bool UsualArrayDeleteWantsSize = false; |
3638 | |
3639 | if (!Ex.get()->isTypeDependent()) { |
3640 | // Perform lvalue-to-rvalue cast, if needed. |
3641 | Ex = DefaultLvalueConversion(E: Ex.get()); |
3642 | if (Ex.isInvalid()) |
3643 | return ExprError(); |
3644 | |
3645 | QualType Type = Ex.get()->getType(); |
3646 | |
3647 | class DeleteConverter : public ContextualImplicitConverter { |
3648 | public: |
3649 | DeleteConverter() : ContextualImplicitConverter(false, true) {} |
3650 | |
3651 | bool match(QualType ConvType) override { |
3652 | // FIXME: If we have an operator T* and an operator void*, we must pick |
3653 | // the operator T*. |
3654 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
3655 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
3656 | return true; |
3657 | return false; |
3658 | } |
3659 | |
3660 | SemaDiagnosticBuilder diagnoseNoMatch(Sema &S, SourceLocation Loc, |
3661 | QualType T) override { |
3662 | return S.Diag(Loc, diag::err_delete_operand) << T; |
3663 | } |
3664 | |
3665 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
3666 | QualType T) override { |
3667 | return S.Diag(Loc, diag::err_delete_incomplete_class_type) << T; |
3668 | } |
3669 | |
3670 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
3671 | QualType T, |
3672 | QualType ConvTy) override { |
3673 | return S.Diag(Loc, diag::err_delete_explicit_conversion) << T << ConvTy; |
3674 | } |
3675 | |
3676 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
3677 | QualType ConvTy) override { |
3678 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
3679 | << ConvTy; |
3680 | } |
3681 | |
3682 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
3683 | QualType T) override { |
3684 | return S.Diag(Loc, diag::err_ambiguous_delete_operand) << T; |
3685 | } |
3686 | |
3687 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
3688 | QualType ConvTy) override { |
3689 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
3690 | << ConvTy; |
3691 | } |
3692 | |
3693 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
3694 | QualType T, |
3695 | QualType ConvTy) override { |
3696 | llvm_unreachable("conversion functions are permitted" ); |
3697 | } |
3698 | } Converter; |
3699 | |
3700 | Ex = PerformContextualImplicitConversion(Loc: StartLoc, FromE: Ex.get(), Converter); |
3701 | if (Ex.isInvalid()) |
3702 | return ExprError(); |
3703 | Type = Ex.get()->getType(); |
3704 | if (!Converter.match(ConvType: Type)) |
3705 | // FIXME: PerformContextualImplicitConversion should return ExprError |
3706 | // itself in this case. |
3707 | return ExprError(); |
3708 | |
3709 | QualType Pointee = Type->castAs<PointerType>()->getPointeeType(); |
3710 | QualType PointeeElem = Context.getBaseElementType(QT: Pointee); |
3711 | |
3712 | if (Pointee.getAddressSpace() != LangAS::Default && |
3713 | !getLangOpts().OpenCLCPlusPlus) |
3714 | return Diag(Ex.get()->getBeginLoc(), |
3715 | diag::err_address_space_qualified_delete) |
3716 | << Pointee.getUnqualifiedType() |
3717 | << Pointee.getQualifiers().getAddressSpaceAttributePrintValue(); |
3718 | |
3719 | CXXRecordDecl *PointeeRD = nullptr; |
3720 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
3721 | // The C++ standard bans deleting a pointer to a non-object type, which |
3722 | // effectively bans deletion of "void*". However, most compilers support |
3723 | // this, so we treat it as a warning unless we're in a SFINAE context. |
3724 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
3725 | << Type << Ex.get()->getSourceRange(); |
3726 | } else if (Pointee->isFunctionType() || Pointee->isVoidType() || |
3727 | Pointee->isSizelessType()) { |
3728 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
3729 | << Type << Ex.get()->getSourceRange()); |
3730 | } else if (!Pointee->isDependentType()) { |
3731 | // FIXME: This can result in errors if the definition was imported from a |
3732 | // module but is hidden. |
3733 | if (!RequireCompleteType(StartLoc, Pointee, |
3734 | diag::warn_delete_incomplete, Ex.get())) { |
3735 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) |
3736 | PointeeRD = cast<CXXRecordDecl>(Val: RT->getDecl()); |
3737 | } |
3738 | } |
3739 | |
3740 | if (Pointee->isArrayType() && !ArrayForm) { |
3741 | Diag(StartLoc, diag::warn_delete_array_type) |
3742 | << Type << Ex.get()->getSourceRange() |
3743 | << FixItHint::CreateInsertion(getLocForEndOfToken(StartLoc), "[]" ); |
3744 | ArrayForm = true; |
3745 | } |
3746 | |
3747 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
3748 | Op: ArrayForm ? OO_Array_Delete : OO_Delete); |
3749 | |
3750 | if (PointeeRD) { |
3751 | if (!UseGlobal && |
3752 | FindDeallocationFunction(StartLoc, RD: PointeeRD, Name: DeleteName, |
3753 | Operator&: OperatorDelete)) |
3754 | return ExprError(); |
3755 | |
3756 | // If we're allocating an array of records, check whether the |
3757 | // usual operator delete[] has a size_t parameter. |
3758 | if (ArrayForm) { |
3759 | // If the user specifically asked to use the global allocator, |
3760 | // we'll need to do the lookup into the class. |
3761 | if (UseGlobal) |
3762 | UsualArrayDeleteWantsSize = |
3763 | doesUsualArrayDeleteWantSize(S&: *this, loc: StartLoc, allocType: PointeeElem); |
3764 | |
3765 | // Otherwise, the usual operator delete[] should be the |
3766 | // function we just found. |
3767 | else if (OperatorDelete && isa<CXXMethodDecl>(Val: OperatorDelete)) |
3768 | UsualArrayDeleteWantsSize = |
3769 | UsualDeallocFnInfo(*this, |
3770 | DeclAccessPair::make(OperatorDelete, AS_public)) |
3771 | .HasSizeT; |
3772 | } |
3773 | |
3774 | if (!PointeeRD->hasIrrelevantDestructor()) |
3775 | if (CXXDestructorDecl *Dtor = LookupDestructor(Class: PointeeRD)) { |
3776 | MarkFunctionReferenced(StartLoc, |
3777 | const_cast<CXXDestructorDecl*>(Dtor)); |
3778 | if (DiagnoseUseOfDecl(Dtor, StartLoc)) |
3779 | return ExprError(); |
3780 | } |
3781 | |
3782 | CheckVirtualDtorCall(dtor: PointeeRD->getDestructor(), Loc: StartLoc, |
3783 | /*IsDelete=*/true, /*CallCanBeVirtual=*/true, |
3784 | /*WarnOnNonAbstractTypes=*/!ArrayForm, |
3785 | DtorLoc: SourceLocation()); |
3786 | } |
3787 | |
3788 | if (!OperatorDelete) { |
3789 | if (getLangOpts().OpenCLCPlusPlus) { |
3790 | Diag(StartLoc, diag::err_openclcxx_not_supported) << "default delete" ; |
3791 | return ExprError(); |
3792 | } |
3793 | |
3794 | bool IsComplete = isCompleteType(Loc: StartLoc, T: Pointee); |
3795 | bool CanProvideSize = |
3796 | IsComplete && (!ArrayForm || UsualArrayDeleteWantsSize || |
3797 | Pointee.isDestructedType()); |
3798 | bool Overaligned = hasNewExtendedAlignment(S&: *this, AllocType: Pointee); |
3799 | |
3800 | // Look for a global declaration. |
3801 | OperatorDelete = FindUsualDeallocationFunction(StartLoc, CanProvideSize, |
3802 | Overaligned, Name: DeleteName); |
3803 | } |
3804 | |
3805 | MarkFunctionReferenced(Loc: StartLoc, Func: OperatorDelete); |
3806 | |
3807 | // Check access and ambiguity of destructor if we're going to call it. |
3808 | // Note that this is required even for a virtual delete. |
3809 | bool IsVirtualDelete = false; |
3810 | if (PointeeRD) { |
3811 | if (CXXDestructorDecl *Dtor = LookupDestructor(Class: PointeeRD)) { |
3812 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, |
3813 | PDiag(diag::err_access_dtor) << PointeeElem); |
3814 | IsVirtualDelete = Dtor->isVirtual(); |
3815 | } |
3816 | } |
3817 | |
3818 | DiagnoseUseOfDecl(OperatorDelete, StartLoc); |
3819 | |
3820 | // Convert the operand to the type of the first parameter of operator |
3821 | // delete. This is only necessary if we selected a destroying operator |
3822 | // delete that we are going to call (non-virtually); converting to void* |
3823 | // is trivial and left to AST consumers to handle. |
3824 | QualType ParamType = OperatorDelete->getParamDecl(i: 0)->getType(); |
3825 | if (!IsVirtualDelete && !ParamType->getPointeeType()->isVoidType()) { |
3826 | Qualifiers Qs = Pointee.getQualifiers(); |
3827 | if (Qs.hasCVRQualifiers()) { |
3828 | // Qualifiers are irrelevant to this conversion; we're only looking |
3829 | // for access and ambiguity. |
3830 | Qs.removeCVRQualifiers(); |
3831 | QualType Unqual = Context.getPointerType( |
3832 | T: Context.getQualifiedType(T: Pointee.getUnqualifiedType(), Qs)); |
3833 | Ex = ImpCastExprToType(E: Ex.get(), Type: Unqual, CK: CK_NoOp); |
3834 | } |
3835 | Ex = PerformImplicitConversion(From: Ex.get(), ToType: ParamType, Action: AA_Passing); |
3836 | if (Ex.isInvalid()) |
3837 | return ExprError(); |
3838 | } |
3839 | } |
3840 | |
3841 | CXXDeleteExpr *Result = new (Context) CXXDeleteExpr( |
3842 | Context.VoidTy, UseGlobal, ArrayForm, ArrayFormAsWritten, |
3843 | UsualArrayDeleteWantsSize, OperatorDelete, Ex.get(), StartLoc); |
3844 | AnalyzeDeleteExprMismatch(DE: Result); |
3845 | return Result; |
3846 | } |
3847 | |
3848 | static bool resolveBuiltinNewDeleteOverload(Sema &S, CallExpr *TheCall, |
3849 | bool IsDelete, |
3850 | FunctionDecl *&Operator) { |
3851 | |
3852 | DeclarationName NewName = S.Context.DeclarationNames.getCXXOperatorName( |
3853 | Op: IsDelete ? OO_Delete : OO_New); |
3854 | |
3855 | LookupResult R(S, NewName, TheCall->getBeginLoc(), Sema::LookupOrdinaryName); |
3856 | S.LookupQualifiedName(R, S.Context.getTranslationUnitDecl()); |
3857 | assert(!R.empty() && "implicitly declared allocation functions not found" ); |
3858 | assert(!R.isAmbiguous() && "global allocation functions are ambiguous" ); |
3859 | |
3860 | // We do our own custom access checks below. |
3861 | R.suppressDiagnostics(); |
3862 | |
3863 | SmallVector<Expr *, 8> Args(TheCall->arguments()); |
3864 | OverloadCandidateSet Candidates(R.getNameLoc(), |
3865 | OverloadCandidateSet::CSK_Normal); |
3866 | for (LookupResult::iterator FnOvl = R.begin(), FnOvlEnd = R.end(); |
3867 | FnOvl != FnOvlEnd; ++FnOvl) { |
3868 | // Even member operator new/delete are implicitly treated as |
3869 | // static, so don't use AddMemberCandidate. |
3870 | NamedDecl *D = (*FnOvl)->getUnderlyingDecl(); |
3871 | |
3872 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(Val: D)) { |
3873 | S.AddTemplateOverloadCandidate(FunctionTemplate: FnTemplate, FoundDecl: FnOvl.getPair(), |
3874 | /*ExplicitTemplateArgs=*/nullptr, Args, |
3875 | CandidateSet&: Candidates, |
3876 | /*SuppressUserConversions=*/false); |
3877 | continue; |
3878 | } |
3879 | |
3880 | FunctionDecl *Fn = cast<FunctionDecl>(Val: D); |
3881 | S.AddOverloadCandidate(Function: Fn, FoundDecl: FnOvl.getPair(), Args, CandidateSet&: Candidates, |
3882 | /*SuppressUserConversions=*/false); |
3883 | } |
3884 | |
3885 | SourceRange Range = TheCall->getSourceRange(); |
3886 | |
3887 | // Do the resolution. |
3888 | OverloadCandidateSet::iterator Best; |
3889 | switch (Candidates.BestViableFunction(S, Loc: R.getNameLoc(), Best)) { |
3890 | case OR_Success: { |
3891 | // Got one! |
3892 | FunctionDecl *FnDecl = Best->Function; |
3893 | assert(R.getNamingClass() == nullptr && |
3894 | "class members should not be considered" ); |
3895 | |
3896 | if (!FnDecl->isReplaceableGlobalAllocationFunction()) { |
3897 | S.Diag(R.getNameLoc(), diag::err_builtin_operator_new_delete_not_usual) |
3898 | << (IsDelete ? 1 : 0) << Range; |
3899 | S.Diag(FnDecl->getLocation(), diag::note_non_usual_function_declared_here) |
3900 | << R.getLookupName() << FnDecl->getSourceRange(); |
3901 | return true; |
3902 | } |
3903 | |
3904 | Operator = FnDecl; |
3905 | return false; |
3906 | } |
3907 | |
3908 | case OR_No_Viable_Function: |
3909 | Candidates.NoteCandidates( |
3910 | PartialDiagnosticAt(R.getNameLoc(), |
3911 | S.PDiag(diag::err_ovl_no_viable_function_in_call) |
3912 | << R.getLookupName() << Range), |
3913 | S, OCD_AllCandidates, Args); |
3914 | return true; |
3915 | |
3916 | case OR_Ambiguous: |
3917 | Candidates.NoteCandidates( |
3918 | PartialDiagnosticAt(R.getNameLoc(), |
3919 | S.PDiag(diag::err_ovl_ambiguous_call) |
3920 | << R.getLookupName() << Range), |
3921 | S, OCD_AmbiguousCandidates, Args); |
3922 | return true; |
3923 | |
3924 | case OR_Deleted: { |
3925 | Candidates.NoteCandidates( |
3926 | PartialDiagnosticAt(R.getNameLoc(), S.PDiag(diag::err_ovl_deleted_call) |
3927 | << R.getLookupName() << Range), |
3928 | S, OCD_AllCandidates, Args); |
3929 | return true; |
3930 | } |
3931 | } |
3932 | llvm_unreachable("Unreachable, bad result from BestViableFunction" ); |
3933 | } |
3934 | |
3935 | ExprResult |
3936 | Sema::SemaBuiltinOperatorNewDeleteOverloaded(ExprResult TheCallResult, |
3937 | bool IsDelete) { |
3938 | CallExpr *TheCall = cast<CallExpr>(Val: TheCallResult.get()); |
3939 | if (!getLangOpts().CPlusPlus) { |
3940 | Diag(TheCall->getExprLoc(), diag::err_builtin_requires_language) |
3941 | << (IsDelete ? "__builtin_operator_delete" : "__builtin_operator_new" ) |
3942 | << "C++" ; |
3943 | return ExprError(); |
3944 | } |
3945 | // CodeGen assumes it can find the global new and delete to call, |
3946 | // so ensure that they are declared. |
3947 | DeclareGlobalNewDelete(); |
3948 | |
3949 | FunctionDecl *OperatorNewOrDelete = nullptr; |
3950 | if (resolveBuiltinNewDeleteOverload(S&: *this, TheCall, IsDelete, |
3951 | Operator&: OperatorNewOrDelete)) |
3952 | return ExprError(); |
3953 | assert(OperatorNewOrDelete && "should be found" ); |
3954 | |
3955 | DiagnoseUseOfDecl(D: OperatorNewOrDelete, Locs: TheCall->getExprLoc()); |
3956 | MarkFunctionReferenced(Loc: TheCall->getExprLoc(), Func: OperatorNewOrDelete); |
3957 | |
3958 | TheCall->setType(OperatorNewOrDelete->getReturnType()); |
3959 | for (unsigned i = 0; i != TheCall->getNumArgs(); ++i) { |
3960 | QualType ParamTy = OperatorNewOrDelete->getParamDecl(i)->getType(); |
3961 | InitializedEntity Entity = |
3962 | InitializedEntity::InitializeParameter(Context, Type: ParamTy, Consumed: false); |
3963 | ExprResult Arg = PerformCopyInitialization( |
3964 | Entity, EqualLoc: TheCall->getArg(Arg: i)->getBeginLoc(), Init: TheCall->getArg(Arg: i)); |
3965 | if (Arg.isInvalid()) |
3966 | return ExprError(); |
3967 | TheCall->setArg(Arg: i, ArgExpr: Arg.get()); |
3968 | } |
3969 | auto Callee = dyn_cast<ImplicitCastExpr>(Val: TheCall->getCallee()); |
3970 | assert(Callee && Callee->getCastKind() == CK_BuiltinFnToFnPtr && |
3971 | "Callee expected to be implicit cast to a builtin function pointer" ); |
3972 | Callee->setType(OperatorNewOrDelete->getType()); |
3973 | |
3974 | return TheCallResult; |
3975 | } |
3976 | |
3977 | void Sema::CheckVirtualDtorCall(CXXDestructorDecl *dtor, SourceLocation Loc, |
3978 | bool IsDelete, bool CallCanBeVirtual, |
3979 | bool WarnOnNonAbstractTypes, |
3980 | SourceLocation DtorLoc) { |
3981 | if (!dtor || dtor->isVirtual() || !CallCanBeVirtual || isUnevaluatedContext()) |
3982 | return; |
3983 | |
3984 | // C++ [expr.delete]p3: |
3985 | // In the first alternative (delete object), if the static type of the |
3986 | // object to be deleted is different from its dynamic type, the static |
3987 | // type shall be a base class of the dynamic type of the object to be |
3988 | // deleted and the static type shall have a virtual destructor or the |
3989 | // behavior is undefined. |
3990 | // |
3991 | const CXXRecordDecl *PointeeRD = dtor->getParent(); |
3992 | // Note: a final class cannot be derived from, no issue there |
3993 | if (!PointeeRD->isPolymorphic() || PointeeRD->hasAttr<FinalAttr>()) |
3994 | return; |
3995 | |
3996 | // If the superclass is in a system header, there's nothing that can be done. |
3997 | // The `delete` (where we emit the warning) can be in a system header, |
3998 | // what matters for this warning is where the deleted type is defined. |
3999 | if (getSourceManager().isInSystemHeader(Loc: PointeeRD->getLocation())) |
4000 | return; |
4001 | |
4002 | QualType ClassType = dtor->getFunctionObjectParameterType(); |
4003 | if (PointeeRD->isAbstract()) { |
4004 | // If the class is abstract, we warn by default, because we're |
4005 | // sure the code has undefined behavior. |
4006 | Diag(Loc, diag::warn_delete_abstract_non_virtual_dtor) << (IsDelete ? 0 : 1) |
4007 | << ClassType; |
4008 | } else if (WarnOnNonAbstractTypes) { |
4009 | // Otherwise, if this is not an array delete, it's a bit suspect, |
4010 | // but not necessarily wrong. |
4011 | Diag(Loc, diag::warn_delete_non_virtual_dtor) << (IsDelete ? 0 : 1) |
4012 | << ClassType; |
4013 | } |
4014 | if (!IsDelete) { |
4015 | std::string TypeStr; |
4016 | ClassType.getAsStringInternal(Str&: TypeStr, Policy: getPrintingPolicy()); |
4017 | Diag(DtorLoc, diag::note_delete_non_virtual) |
4018 | << FixItHint::CreateInsertion(DtorLoc, TypeStr + "::" ); |
4019 | } |
4020 | } |
4021 | |
4022 | Sema::ConditionResult Sema::ActOnConditionVariable(Decl *ConditionVar, |
4023 | SourceLocation StmtLoc, |
4024 | ConditionKind CK) { |
4025 | ExprResult E = |
4026 | CheckConditionVariable(ConditionVar: cast<VarDecl>(Val: ConditionVar), StmtLoc, CK); |
4027 | if (E.isInvalid()) |
4028 | return ConditionError(); |
4029 | return ConditionResult(*this, ConditionVar, MakeFullExpr(Arg: E.get(), CC: StmtLoc), |
4030 | CK == ConditionKind::ConstexprIf); |
4031 | } |
4032 | |
4033 | /// Check the use of the given variable as a C++ condition in an if, |
4034 | /// while, do-while, or switch statement. |
4035 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
4036 | SourceLocation StmtLoc, |
4037 | ConditionKind CK) { |
4038 | if (ConditionVar->isInvalidDecl()) |
4039 | return ExprError(); |
4040 | |
4041 | QualType T = ConditionVar->getType(); |
4042 | |
4043 | // C++ [stmt.select]p2: |
4044 | // The declarator shall not specify a function or an array. |
4045 | if (T->isFunctionType()) |
4046 | return ExprError(Diag(ConditionVar->getLocation(), |
4047 | diag::err_invalid_use_of_function_type) |
4048 | << ConditionVar->getSourceRange()); |
4049 | else if (T->isArrayType()) |
4050 | return ExprError(Diag(ConditionVar->getLocation(), |
4051 | diag::err_invalid_use_of_array_type) |
4052 | << ConditionVar->getSourceRange()); |
4053 | |
4054 | ExprResult Condition = BuildDeclRefExpr( |
4055 | ConditionVar, ConditionVar->getType().getNonReferenceType(), VK_LValue, |
4056 | ConditionVar->getLocation()); |
4057 | |
4058 | switch (CK) { |
4059 | case ConditionKind::Boolean: |
4060 | return CheckBooleanCondition(Loc: StmtLoc, E: Condition.get()); |
4061 | |
4062 | case ConditionKind::ConstexprIf: |
4063 | return CheckBooleanCondition(Loc: StmtLoc, E: Condition.get(), IsConstexpr: true); |
4064 | |
4065 | case ConditionKind::Switch: |
4066 | return CheckSwitchCondition(SwitchLoc: StmtLoc, Cond: Condition.get()); |
4067 | } |
4068 | |
4069 | llvm_unreachable("unexpected condition kind" ); |
4070 | } |
4071 | |
4072 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
4073 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr, bool IsConstexpr) { |
4074 | // C++11 6.4p4: |
4075 | // The value of a condition that is an initialized declaration in a statement |
4076 | // other than a switch statement is the value of the declared variable |
4077 | // implicitly converted to type bool. If that conversion is ill-formed, the |
4078 | // program is ill-formed. |
4079 | // The value of a condition that is an expression is the value of the |
4080 | // expression, implicitly converted to bool. |
4081 | // |
4082 | // C++23 8.5.2p2 |
4083 | // If the if statement is of the form if constexpr, the value of the condition |
4084 | // is contextually converted to bool and the converted expression shall be |
4085 | // a constant expression. |
4086 | // |
4087 | |
4088 | ExprResult E = PerformContextuallyConvertToBool(From: CondExpr); |
4089 | if (!IsConstexpr || E.isInvalid() || E.get()->isValueDependent()) |
4090 | return E; |
4091 | |
4092 | // FIXME: Return this value to the caller so they don't need to recompute it. |
4093 | llvm::APSInt Cond; |
4094 | E = VerifyIntegerConstantExpression( |
4095 | E.get(), &Cond, |
4096 | diag::err_constexpr_if_condition_expression_is_not_constant); |
4097 | return E; |
4098 | } |
4099 | |
4100 | /// Helper function to determine whether this is the (deprecated) C++ |
4101 | /// conversion from a string literal to a pointer to non-const char or |
4102 | /// non-const wchar_t (for narrow and wide string literals, |
4103 | /// respectively). |
4104 | bool |
4105 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
4106 | // Look inside the implicit cast, if it exists. |
4107 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Val: From)) |
4108 | From = Cast->getSubExpr(); |
4109 | |
4110 | // A string literal (2.13.4) that is not a wide string literal can |
4111 | // be converted to an rvalue of type "pointer to char"; a wide |
4112 | // string literal can be converted to an rvalue of type "pointer |
4113 | // to wchar_t" (C++ 4.2p2). |
4114 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(Val: From->IgnoreParens())) |
4115 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
4116 | if (const BuiltinType *ToPointeeType |
4117 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
4118 | // This conversion is considered only when there is an |
4119 | // explicit appropriate pointer target type (C++ 4.2p2). |
4120 | if (!ToPtrType->getPointeeType().hasQualifiers()) { |
4121 | switch (StrLit->getKind()) { |
4122 | case StringLiteralKind::UTF8: |
4123 | case StringLiteralKind::UTF16: |
4124 | case StringLiteralKind::UTF32: |
4125 | // We don't allow UTF literals to be implicitly converted |
4126 | break; |
4127 | case StringLiteralKind::Ordinary: |
4128 | return (ToPointeeType->getKind() == BuiltinType::Char_U || |
4129 | ToPointeeType->getKind() == BuiltinType::Char_S); |
4130 | case StringLiteralKind::Wide: |
4131 | return Context.typesAreCompatible(T1: Context.getWideCharType(), |
4132 | T2: QualType(ToPointeeType, 0)); |
4133 | case StringLiteralKind::Unevaluated: |
4134 | assert(false && "Unevaluated string literal in expression" ); |
4135 | break; |
4136 | } |
4137 | } |
4138 | } |
4139 | |
4140 | return false; |
4141 | } |
4142 | |
4143 | static ExprResult BuildCXXCastArgument(Sema &S, |
4144 | SourceLocation CastLoc, |
4145 | QualType Ty, |
4146 | CastKind Kind, |
4147 | CXXMethodDecl *Method, |
4148 | DeclAccessPair FoundDecl, |
4149 | bool HadMultipleCandidates, |
4150 | Expr *From) { |
4151 | switch (Kind) { |
4152 | default: llvm_unreachable("Unhandled cast kind!" ); |
4153 | case CK_ConstructorConversion: { |
4154 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Val: Method); |
4155 | SmallVector<Expr*, 8> ConstructorArgs; |
4156 | |
4157 | if (S.RequireNonAbstractType(CastLoc, Ty, |
4158 | diag::err_allocation_of_abstract_type)) |
4159 | return ExprError(); |
4160 | |
4161 | if (S.CompleteConstructorCall(Constructor, DeclInitType: Ty, ArgsPtr: From, Loc: CastLoc, |
4162 | ConvertedArgs&: ConstructorArgs)) |
4163 | return ExprError(); |
4164 | |
4165 | S.CheckConstructorAccess(Loc: CastLoc, D: Constructor, FoundDecl, |
4166 | Entity: InitializedEntity::InitializeTemporary(Type: Ty)); |
4167 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
4168 | return ExprError(); |
4169 | |
4170 | ExprResult Result = S.BuildCXXConstructExpr( |
4171 | ConstructLoc: CastLoc, DeclInitType: Ty, FoundDecl, Constructor: cast<CXXConstructorDecl>(Val: Method), |
4172 | Exprs: ConstructorArgs, HadMultipleCandidates, |
4173 | /*ListInit*/ IsListInitialization: false, /*StdInitListInit*/ IsStdInitListInitialization: false, /*ZeroInit*/ RequiresZeroInit: false, |
4174 | ConstructKind: CXXConstructionKind::Complete, ParenRange: SourceRange()); |
4175 | if (Result.isInvalid()) |
4176 | return ExprError(); |
4177 | |
4178 | return S.MaybeBindToTemporary(E: Result.getAs<Expr>()); |
4179 | } |
4180 | |
4181 | case CK_UserDefinedConversion: { |
4182 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!" ); |
4183 | |
4184 | S.CheckMemberOperatorAccess(Loc: CastLoc, ObjectExpr: From, /*arg*/ ArgExpr: nullptr, FoundDecl); |
4185 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
4186 | return ExprError(); |
4187 | |
4188 | // Create an implicit call expr that calls it. |
4189 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(Val: Method); |
4190 | ExprResult Result = S.BuildCXXMemberCallExpr(Exp: From, FoundDecl, Method: Conv, |
4191 | HadMultipleCandidates); |
4192 | if (Result.isInvalid()) |
4193 | return ExprError(); |
4194 | // Record usage of conversion in an implicit cast. |
4195 | Result = ImplicitCastExpr::Create(Context: S.Context, T: Result.get()->getType(), |
4196 | Kind: CK_UserDefinedConversion, Operand: Result.get(), |
4197 | BasePath: nullptr, Cat: Result.get()->getValueKind(), |
4198 | FPO: S.CurFPFeatureOverrides()); |
4199 | |
4200 | return S.MaybeBindToTemporary(E: Result.get()); |
4201 | } |
4202 | } |
4203 | } |
4204 | |
4205 | /// PerformImplicitConversion - Perform an implicit conversion of the |
4206 | /// expression From to the type ToType using the pre-computed implicit |
4207 | /// conversion sequence ICS. Returns the converted |
4208 | /// expression. Action is the kind of conversion we're performing, |
4209 | /// used in the error message. |
4210 | ExprResult |
4211 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
4212 | const ImplicitConversionSequence &ICS, |
4213 | AssignmentAction Action, |
4214 | CheckedConversionKind CCK) { |
4215 | // C++ [over.match.oper]p7: [...] operands of class type are converted [...] |
4216 | if (CCK == CCK_ForBuiltinOverloadedOp && !From->getType()->isRecordType()) |
4217 | return From; |
4218 | |
4219 | switch (ICS.getKind()) { |
4220 | case ImplicitConversionSequence::StandardConversion: { |
4221 | ExprResult Res = PerformImplicitConversion(From, ToType, SCS: ICS.Standard, |
4222 | Action, CCK); |
4223 | if (Res.isInvalid()) |
4224 | return ExprError(); |
4225 | From = Res.get(); |
4226 | break; |
4227 | } |
4228 | |
4229 | case ImplicitConversionSequence::UserDefinedConversion: { |
4230 | |
4231 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
4232 | CastKind CastKind; |
4233 | QualType BeforeToType; |
4234 | assert(FD && "no conversion function for user-defined conversion seq" ); |
4235 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(Val: FD)) { |
4236 | CastKind = CK_UserDefinedConversion; |
4237 | |
4238 | // If the user-defined conversion is specified by a conversion function, |
4239 | // the initial standard conversion sequence converts the source type to |
4240 | // the implicit object parameter of the conversion function. |
4241 | BeforeToType = Context.getTagDeclType(Decl: Conv->getParent()); |
4242 | } else { |
4243 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(Val: FD); |
4244 | CastKind = CK_ConstructorConversion; |
4245 | // Do no conversion if dealing with ... for the first conversion. |
4246 | if (!ICS.UserDefined.EllipsisConversion) { |
4247 | // If the user-defined conversion is specified by a constructor, the |
4248 | // initial standard conversion sequence converts the source type to |
4249 | // the type required by the argument of the constructor |
4250 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
4251 | } |
4252 | } |
4253 | // Watch out for ellipsis conversion. |
4254 | if (!ICS.UserDefined.EllipsisConversion) { |
4255 | ExprResult Res = |
4256 | PerformImplicitConversion(From, ToType: BeforeToType, |
4257 | SCS: ICS.UserDefined.Before, Action: AA_Converting, |
4258 | CCK); |
4259 | if (Res.isInvalid()) |
4260 | return ExprError(); |
4261 | From = Res.get(); |
4262 | } |
4263 | |
4264 | ExprResult CastArg = BuildCXXCastArgument( |
4265 | *this, From->getBeginLoc(), ToType.getNonReferenceType(), CastKind, |
4266 | cast<CXXMethodDecl>(Val: FD), ICS.UserDefined.FoundConversionFunction, |
4267 | ICS.UserDefined.HadMultipleCandidates, From); |
4268 | |
4269 | if (CastArg.isInvalid()) |
4270 | return ExprError(); |
4271 | |
4272 | From = CastArg.get(); |
4273 | |
4274 | // C++ [over.match.oper]p7: |
4275 | // [...] the second standard conversion sequence of a user-defined |
4276 | // conversion sequence is not applied. |
4277 | if (CCK == CCK_ForBuiltinOverloadedOp) |
4278 | return From; |
4279 | |
4280 | return PerformImplicitConversion(From, ToType, SCS: ICS.UserDefined.After, |
4281 | Action: AA_Converting, CCK); |
4282 | } |
4283 | |
4284 | case ImplicitConversionSequence::AmbiguousConversion: |
4285 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
4286 | PDiag(diag::err_typecheck_ambiguous_condition) |
4287 | << From->getSourceRange()); |
4288 | return ExprError(); |
4289 | |
4290 | case ImplicitConversionSequence::EllipsisConversion: |
4291 | case ImplicitConversionSequence::StaticObjectArgumentConversion: |
4292 | llvm_unreachable("bad conversion" ); |
4293 | |
4294 | case ImplicitConversionSequence::BadConversion: |
4295 | Sema::AssignConvertType ConvTy = |
4296 | CheckAssignmentConstraints(Loc: From->getExprLoc(), LHSType: ToType, RHSType: From->getType()); |
4297 | bool Diagnosed = DiagnoseAssignmentResult( |
4298 | ConvTy: ConvTy == Compatible ? Incompatible : ConvTy, Loc: From->getExprLoc(), |
4299 | DstType: ToType, SrcType: From->getType(), SrcExpr: From, Action); |
4300 | assert(Diagnosed && "failed to diagnose bad conversion" ); (void)Diagnosed; |
4301 | return ExprError(); |
4302 | } |
4303 | |
4304 | // Everything went well. |
4305 | return From; |
4306 | } |
4307 | |
4308 | /// PerformImplicitConversion - Perform an implicit conversion of the |
4309 | /// expression From to the type ToType by following the standard |
4310 | /// conversion sequence SCS. Returns the converted |
4311 | /// expression. Flavor is the context in which we're performing this |
4312 | /// conversion, for use in error messages. |
4313 | ExprResult |
4314 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
4315 | const StandardConversionSequence& SCS, |
4316 | AssignmentAction Action, |
4317 | CheckedConversionKind CCK) { |
4318 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); |
4319 | |
4320 | // Overall FIXME: we are recomputing too many types here and doing far too |
4321 | // much extra work. What this means is that we need to keep track of more |
4322 | // information that is computed when we try the implicit conversion initially, |
4323 | // so that we don't need to recompute anything here. |
4324 | QualType FromType = From->getType(); |
4325 | |
4326 | if (SCS.CopyConstructor) { |
4327 | // FIXME: When can ToType be a reference type? |
4328 | assert(!ToType->isReferenceType()); |
4329 | if (SCS.Second == ICK_Derived_To_Base) { |
4330 | SmallVector<Expr*, 8> ConstructorArgs; |
4331 | if (CompleteConstructorCall( |
4332 | Constructor: cast<CXXConstructorDecl>(Val: SCS.CopyConstructor), DeclInitType: ToType, ArgsPtr: From, |
4333 | /*FIXME:ConstructLoc*/ Loc: SourceLocation(), ConvertedArgs&: ConstructorArgs)) |
4334 | return ExprError(); |
4335 | return BuildCXXConstructExpr( |
4336 | /*FIXME:ConstructLoc*/ ConstructLoc: SourceLocation(), DeclInitType: ToType, |
4337 | FoundDecl: SCS.FoundCopyConstructor, Constructor: SCS.CopyConstructor, Exprs: ConstructorArgs, |
4338 | /*HadMultipleCandidates*/ false, |
4339 | /*ListInit*/ IsListInitialization: false, /*StdInitListInit*/ IsStdInitListInitialization: false, /*ZeroInit*/ RequiresZeroInit: false, |
4340 | ConstructKind: CXXConstructionKind::Complete, ParenRange: SourceRange()); |
4341 | } |
4342 | return BuildCXXConstructExpr( |
4343 | /*FIXME:ConstructLoc*/ ConstructLoc: SourceLocation(), DeclInitType: ToType, |
4344 | FoundDecl: SCS.FoundCopyConstructor, Constructor: SCS.CopyConstructor, Exprs: From, |
4345 | /*HadMultipleCandidates*/ false, |
4346 | /*ListInit*/ IsListInitialization: false, /*StdInitListInit*/ IsStdInitListInitialization: false, /*ZeroInit*/ RequiresZeroInit: false, |
4347 | ConstructKind: CXXConstructionKind::Complete, ParenRange: SourceRange()); |
4348 | } |
4349 | |
4350 | // Resolve overloaded function references. |
4351 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
4352 | DeclAccessPair Found; |
4353 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(AddressOfExpr: From, TargetType: ToType, |
4354 | Complain: true, Found); |
4355 | if (!Fn) |
4356 | return ExprError(); |
4357 | |
4358 | if (DiagnoseUseOfDecl(D: Fn, Locs: From->getBeginLoc())) |
4359 | return ExprError(); |
4360 | |
4361 | ExprResult Res = FixOverloadedFunctionReference(E: From, FoundDecl: Found, Fn); |
4362 | if (Res.isInvalid()) |
4363 | return ExprError(); |
4364 | |
4365 | // We might get back another placeholder expression if we resolved to a |
4366 | // builtin. |
4367 | Res = CheckPlaceholderExpr(E: Res.get()); |
4368 | if (Res.isInvalid()) |
4369 | return ExprError(); |
4370 | |
4371 | From = Res.get(); |
4372 | FromType = From->getType(); |
4373 | } |
4374 | |
4375 | // If we're converting to an atomic type, first convert to the corresponding |
4376 | // non-atomic type. |
4377 | QualType ToAtomicType; |
4378 | if (const AtomicType *ToAtomic = ToType->getAs<AtomicType>()) { |
4379 | ToAtomicType = ToType; |
4380 | ToType = ToAtomic->getValueType(); |
4381 | } |
4382 | |
4383 | QualType InitialFromType = FromType; |
4384 | // Perform the first implicit conversion. |
4385 | switch (SCS.First) { |
4386 | case ICK_Identity: |
4387 | if (const AtomicType *FromAtomic = FromType->getAs<AtomicType>()) { |
4388 | FromType = FromAtomic->getValueType().getUnqualifiedType(); |
4389 | From = ImplicitCastExpr::Create(Context, T: FromType, Kind: CK_AtomicToNonAtomic, |
4390 | Operand: From, /*BasePath=*/nullptr, Cat: VK_PRValue, |
4391 | FPO: FPOptionsOverride()); |
4392 | } |
4393 | break; |
4394 | |
4395 | case ICK_Lvalue_To_Rvalue: { |
4396 | assert(From->getObjectKind() != OK_ObjCProperty); |
4397 | ExprResult FromRes = DefaultLvalueConversion(E: From); |
4398 | if (FromRes.isInvalid()) |
4399 | return ExprError(); |
4400 | |
4401 | From = FromRes.get(); |
4402 | FromType = From->getType(); |
4403 | break; |
4404 | } |
4405 | |
4406 | case ICK_Array_To_Pointer: |
4407 | FromType = Context.getArrayDecayedType(T: FromType); |
4408 | From = ImpCastExprToType(E: From, Type: FromType, CK: CK_ArrayToPointerDecay, VK: VK_PRValue, |
4409 | /*BasePath=*/nullptr, CCK) |
4410 | .get(); |
4411 | break; |
4412 | |
4413 | case ICK_Function_To_Pointer: |
4414 | FromType = Context.getPointerType(T: FromType); |
4415 | From = ImpCastExprToType(E: From, Type: FromType, CK: CK_FunctionToPointerDecay, |
4416 | VK: VK_PRValue, /*BasePath=*/nullptr, CCK) |
4417 | .get(); |
4418 | break; |
4419 | |
4420 | default: |
4421 | llvm_unreachable("Improper first standard conversion" ); |
4422 | } |
4423 | |
4424 | // Perform the second implicit conversion |
4425 | switch (SCS.Second) { |
4426 | case ICK_Identity: |
4427 | // C++ [except.spec]p5: |
4428 | // [For] assignment to and initialization of pointers to functions, |
4429 | // pointers to member functions, and references to functions: the |
4430 | // target entity shall allow at least the exceptions allowed by the |
4431 | // source value in the assignment or initialization. |
4432 | switch (Action) { |
4433 | case AA_Assigning: |
4434 | case AA_Initializing: |
4435 | // Note, function argument passing and returning are initialization. |
4436 | case AA_Passing: |
4437 | case AA_Returning: |
4438 | case AA_Sending: |
4439 | case AA_Passing_CFAudited: |
4440 | if (CheckExceptionSpecCompatibility(From, ToType)) |
4441 | return ExprError(); |
4442 | break; |
4443 | |
4444 | case AA_Casting: |
4445 | case AA_Converting: |
4446 | // Casts and implicit conversions are not initialization, so are not |
4447 | // checked for exception specification mismatches. |
4448 | break; |
4449 | } |
4450 | // Nothing else to do. |
4451 | break; |
4452 | |
4453 | case ICK_Integral_Promotion: |
4454 | case ICK_Integral_Conversion: |
4455 | if (ToType->isBooleanType()) { |
4456 | assert(FromType->castAs<EnumType>()->getDecl()->isFixed() && |
4457 | SCS.Second == ICK_Integral_Promotion && |
4458 | "only enums with fixed underlying type can promote to bool" ); |
4459 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_IntegralToBoolean, VK: VK_PRValue, |
4460 | /*BasePath=*/nullptr, CCK) |
4461 | .get(); |
4462 | } else { |
4463 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_IntegralCast, VK: VK_PRValue, |
4464 | /*BasePath=*/nullptr, CCK) |
4465 | .get(); |
4466 | } |
4467 | break; |
4468 | |
4469 | case ICK_Floating_Promotion: |
4470 | case ICK_Floating_Conversion: |
4471 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_FloatingCast, VK: VK_PRValue, |
4472 | /*BasePath=*/nullptr, CCK) |
4473 | .get(); |
4474 | break; |
4475 | |
4476 | case ICK_Complex_Promotion: |
4477 | case ICK_Complex_Conversion: { |
4478 | QualType FromEl = From->getType()->castAs<ComplexType>()->getElementType(); |
4479 | QualType ToEl = ToType->castAs<ComplexType>()->getElementType(); |
4480 | CastKind CK; |
4481 | if (FromEl->isRealFloatingType()) { |
4482 | if (ToEl->isRealFloatingType()) |
4483 | CK = CK_FloatingComplexCast; |
4484 | else |
4485 | CK = CK_FloatingComplexToIntegralComplex; |
4486 | } else if (ToEl->isRealFloatingType()) { |
4487 | CK = CK_IntegralComplexToFloatingComplex; |
4488 | } else { |
4489 | CK = CK_IntegralComplexCast; |
4490 | } |
4491 | From = ImpCastExprToType(E: From, Type: ToType, CK, VK: VK_PRValue, /*BasePath=*/nullptr, |
4492 | CCK) |
4493 | .get(); |
4494 | break; |
4495 | } |
4496 | |
4497 | case ICK_Floating_Integral: |
4498 | if (ToType->isRealFloatingType()) |
4499 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_IntegralToFloating, VK: VK_PRValue, |
4500 | /*BasePath=*/nullptr, CCK) |
4501 | .get(); |
4502 | else |
4503 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_FloatingToIntegral, VK: VK_PRValue, |
4504 | /*BasePath=*/nullptr, CCK) |
4505 | .get(); |
4506 | break; |
4507 | |
4508 | case ICK_Fixed_Point_Conversion: |
4509 | assert((FromType->isFixedPointType() || ToType->isFixedPointType()) && |
4510 | "Attempting implicit fixed point conversion without a fixed " |
4511 | "point operand" ); |
4512 | if (FromType->isFloatingType()) |
4513 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_FloatingToFixedPoint, |
4514 | VK: VK_PRValue, |
4515 | /*BasePath=*/nullptr, CCK).get(); |
4516 | else if (ToType->isFloatingType()) |
4517 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_FixedPointToFloating, |
4518 | VK: VK_PRValue, |
4519 | /*BasePath=*/nullptr, CCK).get(); |
4520 | else if (FromType->isIntegralType(Ctx: Context)) |
4521 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_IntegralToFixedPoint, |
4522 | VK: VK_PRValue, |
4523 | /*BasePath=*/nullptr, CCK).get(); |
4524 | else if (ToType->isIntegralType(Ctx: Context)) |
4525 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_FixedPointToIntegral, |
4526 | VK: VK_PRValue, |
4527 | /*BasePath=*/nullptr, CCK).get(); |
4528 | else if (ToType->isBooleanType()) |
4529 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_FixedPointToBoolean, |
4530 | VK: VK_PRValue, |
4531 | /*BasePath=*/nullptr, CCK).get(); |
4532 | else |
4533 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_FixedPointCast, |
4534 | VK: VK_PRValue, |
4535 | /*BasePath=*/nullptr, CCK).get(); |
4536 | break; |
4537 | |
4538 | case ICK_Compatible_Conversion: |
4539 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_NoOp, VK: From->getValueKind(), |
4540 | /*BasePath=*/nullptr, CCK).get(); |
4541 | break; |
4542 | |
4543 | case ICK_Writeback_Conversion: |
4544 | case ICK_Pointer_Conversion: { |
4545 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
4546 | // Diagnose incompatible Objective-C conversions |
4547 | if (Action == AA_Initializing || Action == AA_Assigning) |
4548 | Diag(From->getBeginLoc(), |
4549 | diag::ext_typecheck_convert_incompatible_pointer) |
4550 | << ToType << From->getType() << Action << From->getSourceRange() |
4551 | << 0; |
4552 | else |
4553 | Diag(From->getBeginLoc(), |
4554 | diag::ext_typecheck_convert_incompatible_pointer) |
4555 | << From->getType() << ToType << Action << From->getSourceRange() |
4556 | << 0; |
4557 | |
4558 | if (From->getType()->isObjCObjectPointerType() && |
4559 | ToType->isObjCObjectPointerType()) |
4560 | EmitRelatedResultTypeNote(E: From); |
4561 | } else if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && |
4562 | !CheckObjCARCUnavailableWeakConversion(castType: ToType, |
4563 | ExprType: From->getType())) { |
4564 | if (Action == AA_Initializing) |
4565 | Diag(From->getBeginLoc(), diag::err_arc_weak_unavailable_assign); |
4566 | else |
4567 | Diag(From->getBeginLoc(), diag::err_arc_convesion_of_weak_unavailable) |
4568 | << (Action == AA_Casting) << From->getType() << ToType |
4569 | << From->getSourceRange(); |
4570 | } |
4571 | |
4572 | // Defer address space conversion to the third conversion. |
4573 | QualType FromPteeType = From->getType()->getPointeeType(); |
4574 | QualType ToPteeType = ToType->getPointeeType(); |
4575 | QualType NewToType = ToType; |
4576 | if (!FromPteeType.isNull() && !ToPteeType.isNull() && |
4577 | FromPteeType.getAddressSpace() != ToPteeType.getAddressSpace()) { |
4578 | NewToType = Context.removeAddrSpaceQualType(T: ToPteeType); |
4579 | NewToType = Context.getAddrSpaceQualType(T: NewToType, |
4580 | AddressSpace: FromPteeType.getAddressSpace()); |
4581 | if (ToType->isObjCObjectPointerType()) |
4582 | NewToType = Context.getObjCObjectPointerType(OIT: NewToType); |
4583 | else if (ToType->isBlockPointerType()) |
4584 | NewToType = Context.getBlockPointerType(T: NewToType); |
4585 | else |
4586 | NewToType = Context.getPointerType(T: NewToType); |
4587 | } |
4588 | |
4589 | CastKind Kind; |
4590 | CXXCastPath BasePath; |
4591 | if (CheckPointerConversion(From, ToType: NewToType, Kind, BasePath, IgnoreBaseAccess: CStyle)) |
4592 | return ExprError(); |
4593 | |
4594 | // Make sure we extend blocks if necessary. |
4595 | // FIXME: doing this here is really ugly. |
4596 | if (Kind == CK_BlockPointerToObjCPointerCast) { |
4597 | ExprResult E = From; |
4598 | (void) PrepareCastToObjCObjectPointer(E); |
4599 | From = E.get(); |
4600 | } |
4601 | if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) |
4602 | CheckObjCConversion(castRange: SourceRange(), castType: NewToType, op&: From, CCK); |
4603 | From = ImpCastExprToType(E: From, Type: NewToType, CK: Kind, VK: VK_PRValue, BasePath: &BasePath, CCK) |
4604 | .get(); |
4605 | break; |
4606 | } |
4607 | |
4608 | case ICK_Pointer_Member: { |
4609 | CastKind Kind; |
4610 | CXXCastPath BasePath; |
4611 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, IgnoreBaseAccess: CStyle)) |
4612 | return ExprError(); |
4613 | if (CheckExceptionSpecCompatibility(From, ToType)) |
4614 | return ExprError(); |
4615 | |
4616 | // We may not have been able to figure out what this member pointer resolved |
4617 | // to up until this exact point. Attempt to lock-in it's inheritance model. |
4618 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
4619 | (void)isCompleteType(Loc: From->getExprLoc(), T: From->getType()); |
4620 | (void)isCompleteType(Loc: From->getExprLoc(), T: ToType); |
4621 | } |
4622 | |
4623 | From = |
4624 | ImpCastExprToType(E: From, Type: ToType, CK: Kind, VK: VK_PRValue, BasePath: &BasePath, CCK).get(); |
4625 | break; |
4626 | } |
4627 | |
4628 | case ICK_Boolean_Conversion: |
4629 | // Perform half-to-boolean conversion via float. |
4630 | if (From->getType()->isHalfType()) { |
4631 | From = ImpCastExprToType(E: From, Type: Context.FloatTy, CK: CK_FloatingCast).get(); |
4632 | FromType = Context.FloatTy; |
4633 | } |
4634 | |
4635 | From = ImpCastExprToType(E: From, Type: Context.BoolTy, |
4636 | CK: ScalarTypeToBooleanCastKind(ScalarTy: FromType), VK: VK_PRValue, |
4637 | /*BasePath=*/nullptr, CCK) |
4638 | .get(); |
4639 | break; |
4640 | |
4641 | case ICK_Derived_To_Base: { |
4642 | CXXCastPath BasePath; |
4643 | if (CheckDerivedToBaseConversion( |
4644 | From->getType(), ToType.getNonReferenceType(), From->getBeginLoc(), |
4645 | From->getSourceRange(), &BasePath, CStyle)) |
4646 | return ExprError(); |
4647 | |
4648 | From = ImpCastExprToType(E: From, Type: ToType.getNonReferenceType(), |
4649 | CK: CK_DerivedToBase, VK: From->getValueKind(), |
4650 | BasePath: &BasePath, CCK).get(); |
4651 | break; |
4652 | } |
4653 | |
4654 | case ICK_Vector_Conversion: |
4655 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_BitCast, VK: VK_PRValue, |
4656 | /*BasePath=*/nullptr, CCK) |
4657 | .get(); |
4658 | break; |
4659 | |
4660 | case ICK_SVE_Vector_Conversion: |
4661 | case ICK_RVV_Vector_Conversion: |
4662 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_BitCast, VK: VK_PRValue, |
4663 | /*BasePath=*/nullptr, CCK) |
4664 | .get(); |
4665 | break; |
4666 | |
4667 | case ICK_Vector_Splat: { |
4668 | // Vector splat from any arithmetic type to a vector. |
4669 | Expr *Elem = prepareVectorSplat(VectorTy: ToType, SplattedExpr: From).get(); |
4670 | From = ImpCastExprToType(E: Elem, Type: ToType, CK: CK_VectorSplat, VK: VK_PRValue, |
4671 | /*BasePath=*/nullptr, CCK) |
4672 | .get(); |
4673 | break; |
4674 | } |
4675 | |
4676 | case ICK_Complex_Real: |
4677 | // Case 1. x -> _Complex y |
4678 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
4679 | QualType ElType = ToComplex->getElementType(); |
4680 | bool isFloatingComplex = ElType->isRealFloatingType(); |
4681 | |
4682 | // x -> y |
4683 | if (Context.hasSameUnqualifiedType(T1: ElType, T2: From->getType())) { |
4684 | // do nothing |
4685 | } else if (From->getType()->isRealFloatingType()) { |
4686 | From = ImpCastExprToType(E: From, Type: ElType, |
4687 | CK: isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).get(); |
4688 | } else { |
4689 | assert(From->getType()->isIntegerType()); |
4690 | From = ImpCastExprToType(E: From, Type: ElType, |
4691 | CK: isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).get(); |
4692 | } |
4693 | // y -> _Complex y |
4694 | From = ImpCastExprToType(E: From, Type: ToType, |
4695 | CK: isFloatingComplex ? CK_FloatingRealToComplex |
4696 | : CK_IntegralRealToComplex).get(); |
4697 | |
4698 | // Case 2. _Complex x -> y |
4699 | } else { |
4700 | auto *FromComplex = From->getType()->castAs<ComplexType>(); |
4701 | QualType ElType = FromComplex->getElementType(); |
4702 | bool isFloatingComplex = ElType->isRealFloatingType(); |
4703 | |
4704 | // _Complex x -> x |
4705 | From = ImpCastExprToType(E: From, Type: ElType, |
4706 | CK: isFloatingComplex ? CK_FloatingComplexToReal |
4707 | : CK_IntegralComplexToReal, |
4708 | VK: VK_PRValue, /*BasePath=*/nullptr, CCK) |
4709 | .get(); |
4710 | |
4711 | // x -> y |
4712 | if (Context.hasSameUnqualifiedType(T1: ElType, T2: ToType)) { |
4713 | // do nothing |
4714 | } else if (ToType->isRealFloatingType()) { |
4715 | From = ImpCastExprToType(E: From, Type: ToType, |
4716 | CK: isFloatingComplex ? CK_FloatingCast |
4717 | : CK_IntegralToFloating, |
4718 | VK: VK_PRValue, /*BasePath=*/nullptr, CCK) |
4719 | .get(); |
4720 | } else { |
4721 | assert(ToType->isIntegerType()); |
4722 | From = ImpCastExprToType(E: From, Type: ToType, |
4723 | CK: isFloatingComplex ? CK_FloatingToIntegral |
4724 | : CK_IntegralCast, |
4725 | VK: VK_PRValue, /*BasePath=*/nullptr, CCK) |
4726 | .get(); |
4727 | } |
4728 | } |
4729 | break; |
4730 | |
4731 | case ICK_Block_Pointer_Conversion: { |
4732 | LangAS AddrSpaceL = |
4733 | ToType->castAs<BlockPointerType>()->getPointeeType().getAddressSpace(); |
4734 | LangAS AddrSpaceR = |
4735 | FromType->castAs<BlockPointerType>()->getPointeeType().getAddressSpace(); |
4736 | assert(Qualifiers::isAddressSpaceSupersetOf(AddrSpaceL, AddrSpaceR) && |
4737 | "Invalid cast" ); |
4738 | CastKind Kind = |
4739 | AddrSpaceL != AddrSpaceR ? CK_AddressSpaceConversion : CK_BitCast; |
4740 | From = ImpCastExprToType(E: From, Type: ToType.getUnqualifiedType(), CK: Kind, |
4741 | VK: VK_PRValue, /*BasePath=*/nullptr, CCK) |
4742 | .get(); |
4743 | break; |
4744 | } |
4745 | |
4746 | case ICK_TransparentUnionConversion: { |
4747 | ExprResult FromRes = From; |
4748 | Sema::AssignConvertType ConvTy = |
4749 | CheckTransparentUnionArgumentConstraints(ArgType: ToType, RHS&: FromRes); |
4750 | if (FromRes.isInvalid()) |
4751 | return ExprError(); |
4752 | From = FromRes.get(); |
4753 | assert ((ConvTy == Sema::Compatible) && |
4754 | "Improper transparent union conversion" ); |
4755 | (void)ConvTy; |
4756 | break; |
4757 | } |
4758 | |
4759 | case ICK_Zero_Event_Conversion: |
4760 | case ICK_Zero_Queue_Conversion: |
4761 | From = ImpCastExprToType(E: From, Type: ToType, |
4762 | CK: CK_ZeroToOCLOpaqueType, |
4763 | VK: From->getValueKind()).get(); |
4764 | break; |
4765 | |
4766 | case ICK_Lvalue_To_Rvalue: |
4767 | case ICK_Array_To_Pointer: |
4768 | case ICK_Function_To_Pointer: |
4769 | case ICK_Function_Conversion: |
4770 | case ICK_Qualification: |
4771 | case ICK_Num_Conversion_Kinds: |
4772 | case ICK_C_Only_Conversion: |
4773 | case ICK_Incompatible_Pointer_Conversion: |
4774 | llvm_unreachable("Improper second standard conversion" ); |
4775 | } |
4776 | |
4777 | switch (SCS.Third) { |
4778 | case ICK_Identity: |
4779 | // Nothing to do. |
4780 | break; |
4781 | |
4782 | case ICK_Function_Conversion: |
4783 | // If both sides are functions (or pointers/references to them), there could |
4784 | // be incompatible exception declarations. |
4785 | if (CheckExceptionSpecCompatibility(From, ToType)) |
4786 | return ExprError(); |
4787 | |
4788 | From = ImpCastExprToType(E: From, Type: ToType, CK: CK_NoOp, VK: VK_PRValue, |
4789 | /*BasePath=*/nullptr, CCK) |
4790 | .get(); |
4791 | break; |
4792 | |
4793 | case ICK_Qualification: { |
4794 | ExprValueKind VK = From->getValueKind(); |
4795 | CastKind CK = CK_NoOp; |
4796 | |
4797 | if (ToType->isReferenceType() && |
4798 | ToType->getPointeeType().getAddressSpace() != |
4799 | From->getType().getAddressSpace()) |
4800 | CK = CK_AddressSpaceConversion; |
4801 | |
4802 | if (ToType->isPointerType() && |
4803 | ToType->getPointeeType().getAddressSpace() != |
4804 | From->getType()->getPointeeType().getAddressSpace()) |
4805 | CK = CK_AddressSpaceConversion; |
4806 | |
4807 | if (!isCast(CCK) && |
4808 | !ToType->getPointeeType().getQualifiers().hasUnaligned() && |
4809 | From->getType()->getPointeeType().getQualifiers().hasUnaligned()) { |
4810 | Diag(From->getBeginLoc(), diag::warn_imp_cast_drops_unaligned) |
4811 | << InitialFromType << ToType; |
4812 | } |
4813 | |
4814 | From = ImpCastExprToType(E: From, Type: ToType.getNonLValueExprType(Context), CK, VK, |
4815 | /*BasePath=*/nullptr, CCK) |
4816 | .get(); |
4817 | |
4818 | if (SCS.DeprecatedStringLiteralToCharPtr && |
4819 | !getLangOpts().WritableStrings) { |
4820 | Diag(From->getBeginLoc(), |
4821 | getLangOpts().CPlusPlus11 |
4822 | ? diag::ext_deprecated_string_literal_conversion |
4823 | : diag::warn_deprecated_string_literal_conversion) |
4824 | << ToType.getNonReferenceType(); |
4825 | } |
4826 | |
4827 | break; |
4828 | } |
4829 | |
4830 | default: |
4831 | llvm_unreachable("Improper third standard conversion" ); |
4832 | } |
4833 | |
4834 | // If this conversion sequence involved a scalar -> atomic conversion, perform |
4835 | // that conversion now. |
4836 | if (!ToAtomicType.isNull()) { |
4837 | assert(Context.hasSameType( |
4838 | ToAtomicType->castAs<AtomicType>()->getValueType(), From->getType())); |
4839 | From = ImpCastExprToType(E: From, Type: ToAtomicType, CK: CK_NonAtomicToAtomic, |
4840 | VK: VK_PRValue, BasePath: nullptr, CCK) |
4841 | .get(); |
4842 | } |
4843 | |
4844 | // Materialize a temporary if we're implicitly converting to a reference |
4845 | // type. This is not required by the C++ rules but is necessary to maintain |
4846 | // AST invariants. |
4847 | if (ToType->isReferenceType() && From->isPRValue()) { |
4848 | ExprResult Res = TemporaryMaterializationConversion(E: From); |
4849 | if (Res.isInvalid()) |
4850 | return ExprError(); |
4851 | From = Res.get(); |
4852 | } |
4853 | |
4854 | // If this conversion sequence succeeded and involved implicitly converting a |
4855 | // _Nullable type to a _Nonnull one, complain. |
4856 | if (!isCast(CCK)) |
4857 | diagnoseNullableToNonnullConversion(DstType: ToType, SrcType: InitialFromType, |
4858 | Loc: From->getBeginLoc()); |
4859 | |
4860 | return From; |
4861 | } |
4862 | |
4863 | /// Check the completeness of a type in a unary type trait. |
4864 | /// |
4865 | /// If the particular type trait requires a complete type, tries to complete |
4866 | /// it. If completing the type fails, a diagnostic is emitted and false |
4867 | /// returned. If completing the type succeeds or no completion was required, |
4868 | /// returns true. |
4869 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT, |
4870 | SourceLocation Loc, |
4871 | QualType ArgTy) { |
4872 | // C++0x [meta.unary.prop]p3: |
4873 | // For all of the class templates X declared in this Clause, instantiating |
4874 | // that template with a template argument that is a class template |
4875 | // specialization may result in the implicit instantiation of the template |
4876 | // argument if and only if the semantics of X require that the argument |
4877 | // must be a complete type. |
4878 | // We apply this rule to all the type trait expressions used to implement |
4879 | // these class templates. We also try to follow any GCC documented behavior |
4880 | // in these expressions to ensure portability of standard libraries. |
4881 | switch (UTT) { |
4882 | default: llvm_unreachable("not a UTT" ); |
4883 | // is_complete_type somewhat obviously cannot require a complete type. |
4884 | case UTT_IsCompleteType: |
4885 | // Fall-through |
4886 | |
4887 | // These traits are modeled on the type predicates in C++0x |
4888 | // [meta.unary.cat] and [meta.unary.comp]. They are not specified as |
4889 | // requiring a complete type, as whether or not they return true cannot be |
4890 | // impacted by the completeness of the type. |
4891 | case UTT_IsVoid: |
4892 | case UTT_IsIntegral: |
4893 | case UTT_IsFloatingPoint: |
4894 | case UTT_IsArray: |
4895 | case UTT_IsBoundedArray: |
4896 | case UTT_IsPointer: |
4897 | case UTT_IsNullPointer: |
4898 | case UTT_IsReferenceable: |
4899 | case UTT_IsLvalueReference: |
4900 | case UTT_IsRvalueReference: |
4901 | case UTT_IsMemberFunctionPointer: |
4902 | case UTT_IsMemberObjectPointer: |
4903 | case UTT_IsEnum: |
4904 | case UTT_IsScopedEnum: |
4905 | case UTT_IsUnion: |
4906 | case UTT_IsClass: |
4907 | case UTT_IsFunction: |
4908 | case UTT_IsReference: |
4909 | case UTT_IsArithmetic: |
4910 | case UTT_IsFundamental: |
4911 | case UTT_IsObject: |
4912 | case UTT_IsScalar: |
4913 | case UTT_IsCompound: |
4914 | case UTT_IsMemberPointer: |
4915 | // Fall-through |
4916 | |
4917 | // These traits are modeled on type predicates in C++0x [meta.unary.prop] |
4918 | // which requires some of its traits to have the complete type. However, |
4919 | // the completeness of the type cannot impact these traits' semantics, and |
4920 | // so they don't require it. This matches the comments on these traits in |
4921 | // Table 49. |
4922 | case UTT_IsConst: |
4923 | case UTT_IsVolatile: |
4924 | case UTT_IsSigned: |
4925 | case UTT_IsUnboundedArray: |
4926 | case UTT_IsUnsigned: |
4927 | |
4928 | // This type trait always returns false, checking the type is moot. |
4929 | case UTT_IsInterfaceClass: |
4930 | return true; |
4931 | |
4932 | // C++14 [meta.unary.prop]: |
4933 | // If T is a non-union class type, T shall be a complete type. |
4934 | case UTT_IsEmpty: |
4935 | case UTT_IsPolymorphic: |
4936 | case UTT_IsAbstract: |
4937 | if (const auto *RD = ArgTy->getAsCXXRecordDecl()) |
4938 | if (!RD->isUnion()) |
4939 | return !S.RequireCompleteType( |
4940 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
4941 | return true; |
4942 | |
4943 | // C++14 [meta.unary.prop]: |
4944 | // If T is a class type, T shall be a complete type. |
4945 | case UTT_IsFinal: |
4946 | case UTT_IsSealed: |
4947 | if (ArgTy->getAsCXXRecordDecl()) |
4948 | return !S.RequireCompleteType( |
4949 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
4950 | return true; |
4951 | |
4952 | // LWG3823: T shall be an array type, a complete type, or cv void. |
4953 | case UTT_IsAggregate: |
4954 | if (ArgTy->isArrayType() || ArgTy->isVoidType()) |
4955 | return true; |
4956 | |
4957 | return !S.RequireCompleteType( |
4958 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
4959 | |
4960 | // C++1z [meta.unary.prop]: |
4961 | // remove_all_extents_t<T> shall be a complete type or cv void. |
4962 | case UTT_IsTrivial: |
4963 | case UTT_IsTriviallyCopyable: |
4964 | case UTT_IsStandardLayout: |
4965 | case UTT_IsPOD: |
4966 | case UTT_IsLiteral: |
4967 | // By analogy, is_trivially_relocatable and is_trivially_equality_comparable |
4968 | // impose the same constraints. |
4969 | case UTT_IsTriviallyRelocatable: |
4970 | case UTT_IsTriviallyEqualityComparable: |
4971 | case UTT_CanPassInRegs: |
4972 | // Per the GCC type traits documentation, T shall be a complete type, cv void, |
4973 | // or an array of unknown bound. But GCC actually imposes the same constraints |
4974 | // as above. |
4975 | case UTT_HasNothrowAssign: |
4976 | case UTT_HasNothrowMoveAssign: |
4977 | case UTT_HasNothrowConstructor: |
4978 | case UTT_HasNothrowCopy: |
4979 | case UTT_HasTrivialAssign: |
4980 | case UTT_HasTrivialMoveAssign: |
4981 | case UTT_HasTrivialDefaultConstructor: |
4982 | case UTT_HasTrivialMoveConstructor: |
4983 | case UTT_HasTrivialCopy: |
4984 | case UTT_HasTrivialDestructor: |
4985 | case UTT_HasVirtualDestructor: |
4986 | ArgTy = QualType(ArgTy->getBaseElementTypeUnsafe(), 0); |
4987 | [[fallthrough]]; |
4988 | |
4989 | // C++1z [meta.unary.prop]: |
4990 | // T shall be a complete type, cv void, or an array of unknown bound. |
4991 | case UTT_IsDestructible: |
4992 | case UTT_IsNothrowDestructible: |
4993 | case UTT_IsTriviallyDestructible: |
4994 | case UTT_HasUniqueObjectRepresentations: |
4995 | if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType()) |
4996 | return true; |
4997 | |
4998 | return !S.RequireCompleteType( |
4999 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
5000 | } |
5001 | } |
5002 | |
5003 | static bool HasNoThrowOperator(const RecordType *RT, OverloadedOperatorKind Op, |
5004 | Sema &Self, SourceLocation KeyLoc, ASTContext &C, |
5005 | bool (CXXRecordDecl::*HasTrivial)() const, |
5006 | bool (CXXRecordDecl::*HasNonTrivial)() const, |
5007 | bool (CXXMethodDecl::*IsDesiredOp)() const) |
5008 | { |
5009 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Val: RT->getDecl()); |
5010 | if ((RD->*HasTrivial)() && !(RD->*HasNonTrivial)()) |
5011 | return true; |
5012 | |
5013 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(Op); |
5014 | DeclarationNameInfo NameInfo(Name, KeyLoc); |
5015 | LookupResult Res(Self, NameInfo, Sema::LookupOrdinaryName); |
5016 | if (Self.LookupQualifiedName(Res, RD)) { |
5017 | bool FoundOperator = false; |
5018 | Res.suppressDiagnostics(); |
5019 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
5020 | Op != OpEnd; ++Op) { |
5021 | if (isa<FunctionTemplateDecl>(Val: *Op)) |
5022 | continue; |
5023 | |
5024 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(Val: *Op); |
5025 | if((Operator->*IsDesiredOp)()) { |
5026 | FoundOperator = true; |
5027 | auto *CPT = Operator->getType()->castAs<FunctionProtoType>(); |
5028 | CPT = Self.ResolveExceptionSpec(Loc: KeyLoc, FPT: CPT); |
5029 | if (!CPT || !CPT->isNothrow()) |
5030 | return false; |
5031 | } |
5032 | } |
5033 | return FoundOperator; |
5034 | } |
5035 | return false; |
5036 | } |
5037 | |
5038 | static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT, |
5039 | SourceLocation KeyLoc, QualType T) { |
5040 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type" ); |
5041 | |
5042 | ASTContext &C = Self.Context; |
5043 | switch(UTT) { |
5044 | default: llvm_unreachable("not a UTT" ); |
5045 | // Type trait expressions corresponding to the primary type category |
5046 | // predicates in C++0x [meta.unary.cat]. |
5047 | case UTT_IsVoid: |
5048 | return T->isVoidType(); |
5049 | case UTT_IsIntegral: |
5050 | return T->isIntegralType(Ctx: C); |
5051 | case UTT_IsFloatingPoint: |
5052 | return T->isFloatingType(); |
5053 | case UTT_IsArray: |
5054 | return T->isArrayType(); |
5055 | case UTT_IsBoundedArray: |
5056 | if (!T->isVariableArrayType()) { |
5057 | return T->isArrayType() && !T->isIncompleteArrayType(); |
5058 | } |
5059 | |
5060 | Self.Diag(KeyLoc, diag::err_vla_unsupported) |
5061 | << 1 << tok::kw___is_bounded_array; |
5062 | return false; |
5063 | case UTT_IsUnboundedArray: |
5064 | if (!T->isVariableArrayType()) { |
5065 | return T->isIncompleteArrayType(); |
5066 | } |
5067 | |
5068 | Self.Diag(KeyLoc, diag::err_vla_unsupported) |
5069 | << 1 << tok::kw___is_unbounded_array; |
5070 | return false; |
5071 | case UTT_IsPointer: |
5072 | return T->isAnyPointerType(); |
5073 | case UTT_IsNullPointer: |
5074 | return T->isNullPtrType(); |
5075 | case UTT_IsLvalueReference: |
5076 | return T->isLValueReferenceType(); |
5077 | case UTT_IsRvalueReference: |
5078 | return T->isRValueReferenceType(); |
5079 | case UTT_IsMemberFunctionPointer: |
5080 | return T->isMemberFunctionPointerType(); |
5081 | case UTT_IsMemberObjectPointer: |
5082 | return T->isMemberDataPointerType(); |
5083 | case UTT_IsEnum: |
5084 | return T->isEnumeralType(); |
5085 | case UTT_IsScopedEnum: |
5086 | return T->isScopedEnumeralType(); |
5087 | case UTT_IsUnion: |
5088 | return T->isUnionType(); |
5089 | case UTT_IsClass: |
5090 | return T->isClassType() || T->isStructureType() || T->isInterfaceType(); |
5091 | case UTT_IsFunction: |
5092 | return T->isFunctionType(); |
5093 | |
5094 | // Type trait expressions which correspond to the convenient composition |
5095 | // predicates in C++0x [meta.unary.comp]. |
5096 | case UTT_IsReference: |
5097 | return T->isReferenceType(); |
5098 | case UTT_IsArithmetic: |
5099 | return T->isArithmeticType() && !T->isEnumeralType(); |
5100 | case UTT_IsFundamental: |
5101 | return T->isFundamentalType(); |
5102 | case UTT_IsObject: |
5103 | return T->isObjectType(); |
5104 | case UTT_IsScalar: |
5105 | // Note: semantic analysis depends on Objective-C lifetime types to be |
5106 | // considered scalar types. However, such types do not actually behave |
5107 | // like scalar types at run time (since they may require retain/release |
5108 | // operations), so we report them as non-scalar. |
5109 | if (T->isObjCLifetimeType()) { |
5110 | switch (T.getObjCLifetime()) { |
5111 | case Qualifiers::OCL_None: |
5112 | case Qualifiers::OCL_ExplicitNone: |
5113 | return true; |
5114 | |
5115 | case Qualifiers::OCL_Strong: |
5116 | case Qualifiers::OCL_Weak: |
5117 | case Qualifiers::OCL_Autoreleasing: |
5118 | return false; |
5119 | } |
5120 | } |
5121 | |
5122 | return T->isScalarType(); |
5123 | case UTT_IsCompound: |
5124 | return T->isCompoundType(); |
5125 | case UTT_IsMemberPointer: |
5126 | return T->isMemberPointerType(); |
5127 | |
5128 | // Type trait expressions which correspond to the type property predicates |
5129 | // in C++0x [meta.unary.prop]. |
5130 | case UTT_IsConst: |
5131 | return T.isConstQualified(); |
5132 | case UTT_IsVolatile: |
5133 | return T.isVolatileQualified(); |
5134 | case UTT_IsTrivial: |
5135 | return T.isTrivialType(Context: C); |
5136 | case UTT_IsTriviallyCopyable: |
5137 | return T.isTriviallyCopyableType(Context: C); |
5138 | case UTT_IsStandardLayout: |
5139 | return T->isStandardLayoutType(); |
5140 | case UTT_IsPOD: |
5141 | return T.isPODType(Context: C); |
5142 | case UTT_IsLiteral: |
5143 | return T->isLiteralType(Ctx: C); |
5144 | case UTT_IsEmpty: |
5145 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
5146 | return !RD->isUnion() && RD->isEmpty(); |
5147 | return false; |
5148 | case UTT_IsPolymorphic: |
5149 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
5150 | return !RD->isUnion() && RD->isPolymorphic(); |
5151 | return false; |
5152 | case UTT_IsAbstract: |
5153 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
5154 | return !RD->isUnion() && RD->isAbstract(); |
5155 | return false; |
5156 | case UTT_IsAggregate: |
5157 | // Report vector extensions and complex types as aggregates because they |
5158 | // support aggregate initialization. GCC mirrors this behavior for vectors |
5159 | // but not _Complex. |
5160 | return T->isAggregateType() || T->isVectorType() || T->isExtVectorType() || |
5161 | T->isAnyComplexType(); |
5162 | // __is_interface_class only returns true when CL is invoked in /CLR mode and |
5163 | // even then only when it is used with the 'interface struct ...' syntax |
5164 | // Clang doesn't support /CLR which makes this type trait moot. |
5165 | case UTT_IsInterfaceClass: |
5166 | return false; |
5167 | case UTT_IsFinal: |
5168 | case UTT_IsSealed: |
5169 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
5170 | return RD->hasAttr<FinalAttr>(); |
5171 | return false; |
5172 | case UTT_IsSigned: |
5173 | // Enum types should always return false. |
5174 | // Floating points should always return true. |
5175 | return T->isFloatingType() || |
5176 | (T->isSignedIntegerType() && !T->isEnumeralType()); |
5177 | case UTT_IsUnsigned: |
5178 | // Enum types should always return false. |
5179 | return T->isUnsignedIntegerType() && !T->isEnumeralType(); |
5180 | |
5181 | // Type trait expressions which query classes regarding their construction, |
5182 | // destruction, and copying. Rather than being based directly on the |
5183 | // related type predicates in the standard, they are specified by both |
5184 | // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those |
5185 | // specifications. |
5186 | // |
5187 | // 1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html |
5188 | // 2: http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
5189 | // |
5190 | // Note that these builtins do not behave as documented in g++: if a class |
5191 | // has both a trivial and a non-trivial special member of a particular kind, |
5192 | // they return false! For now, we emulate this behavior. |
5193 | // FIXME: This appears to be a g++ bug: more complex cases reveal that it |
5194 | // does not correctly compute triviality in the presence of multiple special |
5195 | // members of the same kind. Revisit this once the g++ bug is fixed. |
5196 | case UTT_HasTrivialDefaultConstructor: |
5197 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
5198 | // If __is_pod (type) is true then the trait is true, else if type is |
5199 | // a cv class or union type (or array thereof) with a trivial default |
5200 | // constructor ([class.ctor]) then the trait is true, else it is false. |
5201 | if (T.isPODType(Context: C)) |
5202 | return true; |
5203 | if (CXXRecordDecl *RD = C.getBaseElementType(QT: T)->getAsCXXRecordDecl()) |
5204 | return RD->hasTrivialDefaultConstructor() && |
5205 | !RD->hasNonTrivialDefaultConstructor(); |
5206 | return false; |
5207 | case UTT_HasTrivialMoveConstructor: |
5208 | // This trait is implemented by MSVC 2012 and needed to parse the |
5209 | // standard library headers. Specifically this is used as the logic |
5210 | // behind std::is_trivially_move_constructible (20.9.4.3). |
5211 | if (T.isPODType(Context: C)) |
5212 | return true; |
5213 | if (CXXRecordDecl *RD = C.getBaseElementType(QT: T)->getAsCXXRecordDecl()) |
5214 | return RD->hasTrivialMoveConstructor() && !RD->hasNonTrivialMoveConstructor(); |
5215 | return false; |
5216 | case UTT_HasTrivialCopy: |
5217 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
5218 | // If __is_pod (type) is true or type is a reference type then |
5219 | // the trait is true, else if type is a cv class or union type |
5220 | // with a trivial copy constructor ([class.copy]) then the trait |
5221 | // is true, else it is false. |
5222 | if (T.isPODType(Context: C) || T->isReferenceType()) |
5223 | return true; |
5224 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
5225 | return RD->hasTrivialCopyConstructor() && |
5226 | !RD->hasNonTrivialCopyConstructor(); |
5227 | return false; |
5228 | case UTT_HasTrivialMoveAssign: |
5229 | // This trait is implemented by MSVC 2012 and needed to parse the |
5230 | // standard library headers. Specifically it is used as the logic |
5231 | // behind std::is_trivially_move_assignable (20.9.4.3) |
5232 | if (T.isPODType(Context: C)) |
5233 | return true; |
5234 | if (CXXRecordDecl *RD = C.getBaseElementType(QT: T)->getAsCXXRecordDecl()) |
5235 | return RD->hasTrivialMoveAssignment() && !RD->hasNonTrivialMoveAssignment(); |
5236 | return false; |
5237 | case UTT_HasTrivialAssign: |
5238 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
5239 | // If type is const qualified or is a reference type then the |
5240 | // trait is false. Otherwise if __is_pod (type) is true then the |
5241 | // trait is true, else if type is a cv class or union type with |
5242 | // a trivial copy assignment ([class.copy]) then the trait is |
5243 | // true, else it is false. |
5244 | // Note: the const and reference restrictions are interesting, |
5245 | // given that const and reference members don't prevent a class |
5246 | // from having a trivial copy assignment operator (but do cause |
5247 | // errors if the copy assignment operator is actually used, q.v. |
5248 | // [class.copy]p12). |
5249 | |
5250 | if (T.isConstQualified()) |
5251 | return false; |
5252 | if (T.isPODType(Context: C)) |
5253 | return true; |
5254 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
5255 | return RD->hasTrivialCopyAssignment() && |
5256 | !RD->hasNonTrivialCopyAssignment(); |
5257 | return false; |
5258 | case UTT_IsDestructible: |
5259 | case UTT_IsTriviallyDestructible: |
5260 | case UTT_IsNothrowDestructible: |
5261 | // C++14 [meta.unary.prop]: |
5262 | // For reference types, is_destructible<T>::value is true. |
5263 | if (T->isReferenceType()) |
5264 | return true; |
5265 | |
5266 | // Objective-C++ ARC: autorelease types don't require destruction. |
5267 | if (T->isObjCLifetimeType() && |
5268 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
5269 | return true; |
5270 | |
5271 | // C++14 [meta.unary.prop]: |
5272 | // For incomplete types and function types, is_destructible<T>::value is |
5273 | // false. |
5274 | if (T->isIncompleteType() || T->isFunctionType()) |
5275 | return false; |
5276 | |
5277 | // A type that requires destruction (via a non-trivial destructor or ARC |
5278 | // lifetime semantics) is not trivially-destructible. |
5279 | if (UTT == UTT_IsTriviallyDestructible && T.isDestructedType()) |
5280 | return false; |
5281 | |
5282 | // C++14 [meta.unary.prop]: |
5283 | // For object types and given U equal to remove_all_extents_t<T>, if the |
5284 | // expression std::declval<U&>().~U() is well-formed when treated as an |
5285 | // unevaluated operand (Clause 5), then is_destructible<T>::value is true |
5286 | if (auto *RD = C.getBaseElementType(QT: T)->getAsCXXRecordDecl()) { |
5287 | CXXDestructorDecl *Destructor = Self.LookupDestructor(Class: RD); |
5288 | if (!Destructor) |
5289 | return false; |
5290 | // C++14 [dcl.fct.def.delete]p2: |
5291 | // A program that refers to a deleted function implicitly or |
5292 | // explicitly, other than to declare it, is ill-formed. |
5293 | if (Destructor->isDeleted()) |
5294 | return false; |
5295 | if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public) |
5296 | return false; |
5297 | if (UTT == UTT_IsNothrowDestructible) { |
5298 | auto *CPT = Destructor->getType()->castAs<FunctionProtoType>(); |
5299 | CPT = Self.ResolveExceptionSpec(Loc: KeyLoc, FPT: CPT); |
5300 | if (!CPT || !CPT->isNothrow()) |
5301 | return false; |
5302 | } |
5303 | } |
5304 | return true; |
5305 | |
5306 | case UTT_HasTrivialDestructor: |
5307 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
5308 | // If __is_pod (type) is true or type is a reference type |
5309 | // then the trait is true, else if type is a cv class or union |
5310 | // type (or array thereof) with a trivial destructor |
5311 | // ([class.dtor]) then the trait is true, else it is |
5312 | // false. |
5313 | if (T.isPODType(Context: C) || T->isReferenceType()) |
5314 | return true; |
5315 | |
5316 | // Objective-C++ ARC: autorelease types don't require destruction. |
5317 | if (T->isObjCLifetimeType() && |
5318 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
5319 | return true; |
5320 | |
5321 | if (CXXRecordDecl *RD = C.getBaseElementType(QT: T)->getAsCXXRecordDecl()) |
5322 | return RD->hasTrivialDestructor(); |
5323 | return false; |
5324 | // TODO: Propagate nothrowness for implicitly declared special members. |
5325 | case UTT_HasNothrowAssign: |
5326 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
5327 | // If type is const qualified or is a reference type then the |
5328 | // trait is false. Otherwise if __has_trivial_assign (type) |
5329 | // is true then the trait is true, else if type is a cv class |
5330 | // or union type with copy assignment operators that are known |
5331 | // not to throw an exception then the trait is true, else it is |
5332 | // false. |
5333 | if (C.getBaseElementType(QT: T).isConstQualified()) |
5334 | return false; |
5335 | if (T->isReferenceType()) |
5336 | return false; |
5337 | if (T.isPODType(Context: C) || T->isObjCLifetimeType()) |
5338 | return true; |
5339 | |
5340 | if (const RecordType *RT = T->getAs<RecordType>()) |
5341 | return HasNoThrowOperator(RT, Op: OO_Equal, Self, KeyLoc, C, |
5342 | HasTrivial: &CXXRecordDecl::hasTrivialCopyAssignment, |
5343 | HasNonTrivial: &CXXRecordDecl::hasNonTrivialCopyAssignment, |
5344 | IsDesiredOp: &CXXMethodDecl::isCopyAssignmentOperator); |
5345 | return false; |
5346 | case UTT_HasNothrowMoveAssign: |
5347 | // This trait is implemented by MSVC 2012 and needed to parse the |
5348 | // standard library headers. Specifically this is used as the logic |
5349 | // behind std::is_nothrow_move_assignable (20.9.4.3). |
5350 | if (T.isPODType(Context: C)) |
5351 | return true; |
5352 | |
5353 | if (const RecordType *RT = C.getBaseElementType(QT: T)->getAs<RecordType>()) |
5354 | return HasNoThrowOperator(RT, Op: OO_Equal, Self, KeyLoc, C, |
5355 | HasTrivial: &CXXRecordDecl::hasTrivialMoveAssignment, |
5356 | HasNonTrivial: &CXXRecordDecl::hasNonTrivialMoveAssignment, |
5357 | IsDesiredOp: &CXXMethodDecl::isMoveAssignmentOperator); |
5358 | return false; |
5359 | case UTT_HasNothrowCopy: |
5360 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
5361 | // If __has_trivial_copy (type) is true then the trait is true, else |
5362 | // if type is a cv class or union type with copy constructors that are |
5363 | // known not to throw an exception then the trait is true, else it is |
5364 | // false. |
5365 | if (T.isPODType(Context: C) || T->isReferenceType() || T->isObjCLifetimeType()) |
5366 | return true; |
5367 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
5368 | if (RD->hasTrivialCopyConstructor() && |
5369 | !RD->hasNonTrivialCopyConstructor()) |
5370 | return true; |
5371 | |
5372 | bool FoundConstructor = false; |
5373 | unsigned FoundTQs; |
5374 | for (const auto *ND : Self.LookupConstructors(Class: RD)) { |
5375 | // A template constructor is never a copy constructor. |
5376 | // FIXME: However, it may actually be selected at the actual overload |
5377 | // resolution point. |
5378 | if (isa<FunctionTemplateDecl>(Val: ND->getUnderlyingDecl())) |
5379 | continue; |
5380 | // UsingDecl itself is not a constructor |
5381 | if (isa<UsingDecl>(Val: ND)) |
5382 | continue; |
5383 | auto *Constructor = cast<CXXConstructorDecl>(Val: ND->getUnderlyingDecl()); |
5384 | if (Constructor->isCopyConstructor(TypeQuals&: FoundTQs)) { |
5385 | FoundConstructor = true; |
5386 | auto *CPT = Constructor->getType()->castAs<FunctionProtoType>(); |
5387 | CPT = Self.ResolveExceptionSpec(Loc: KeyLoc, FPT: CPT); |
5388 | if (!CPT) |
5389 | return false; |
5390 | // TODO: check whether evaluating default arguments can throw. |
5391 | // For now, we'll be conservative and assume that they can throw. |
5392 | if (!CPT->isNothrow() || CPT->getNumParams() > 1) |
5393 | return false; |
5394 | } |
5395 | } |
5396 | |
5397 | return FoundConstructor; |
5398 | } |
5399 | return false; |
5400 | case UTT_HasNothrowConstructor: |
5401 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
5402 | // If __has_trivial_constructor (type) is true then the trait is |
5403 | // true, else if type is a cv class or union type (or array |
5404 | // thereof) with a default constructor that is known not to |
5405 | // throw an exception then the trait is true, else it is false. |
5406 | if (T.isPODType(Context: C) || T->isObjCLifetimeType()) |
5407 | return true; |
5408 | if (CXXRecordDecl *RD = C.getBaseElementType(QT: T)->getAsCXXRecordDecl()) { |
5409 | if (RD->hasTrivialDefaultConstructor() && |
5410 | !RD->hasNonTrivialDefaultConstructor()) |
5411 | return true; |
5412 | |
5413 | bool FoundConstructor = false; |
5414 | for (const auto *ND : Self.LookupConstructors(Class: RD)) { |
5415 | // FIXME: In C++0x, a constructor template can be a default constructor. |
5416 | if (isa<FunctionTemplateDecl>(Val: ND->getUnderlyingDecl())) |
5417 | continue; |
5418 | // UsingDecl itself is not a constructor |
5419 | if (isa<UsingDecl>(Val: ND)) |
5420 | continue; |
5421 | auto *Constructor = cast<CXXConstructorDecl>(Val: ND->getUnderlyingDecl()); |
5422 | if (Constructor->isDefaultConstructor()) { |
5423 | FoundConstructor = true; |
5424 | auto *CPT = Constructor->getType()->castAs<FunctionProtoType>(); |
5425 | CPT = Self.ResolveExceptionSpec(Loc: KeyLoc, FPT: CPT); |
5426 | if (!CPT) |
5427 | return false; |
5428 | // FIXME: check whether evaluating default arguments can throw. |
5429 | // For now, we'll be conservative and assume that they can throw. |
5430 | if (!CPT->isNothrow() || CPT->getNumParams() > 0) |
5431 | return false; |
5432 | } |
5433 | } |
5434 | return FoundConstructor; |
5435 | } |
5436 | return false; |
5437 | case UTT_HasVirtualDestructor: |
5438 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
5439 | // If type is a class type with a virtual destructor ([class.dtor]) |
5440 | // then the trait is true, else it is false. |
5441 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
5442 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(Class: RD)) |
5443 | return Destructor->isVirtual(); |
5444 | return false; |
5445 | |
5446 | // These type trait expressions are modeled on the specifications for the |
5447 | // Embarcadero C++0x type trait functions: |
5448 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
5449 | case UTT_IsCompleteType: |
5450 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_): |
5451 | // Returns True if and only if T is a complete type at the point of the |
5452 | // function call. |
5453 | return !T->isIncompleteType(); |
5454 | case UTT_HasUniqueObjectRepresentations: |
5455 | return C.hasUniqueObjectRepresentations(Ty: T); |
5456 | case UTT_IsTriviallyRelocatable: |
5457 | return T.isTriviallyRelocatableType(Context: C); |
5458 | case UTT_IsReferenceable: |
5459 | return T.isReferenceable(); |
5460 | case UTT_CanPassInRegs: |
5461 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl(); RD && !T.hasQualifiers()) |
5462 | return RD->canPassInRegisters(); |
5463 | Self.Diag(KeyLoc, diag::err_builtin_pass_in_regs_non_class) << T; |
5464 | return false; |
5465 | case UTT_IsTriviallyEqualityComparable: |
5466 | return T.isTriviallyEqualityComparableType(Context: C); |
5467 | } |
5468 | } |
5469 | |
5470 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
5471 | QualType RhsT, SourceLocation KeyLoc); |
5472 | |
5473 | static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind, |
5474 | SourceLocation KWLoc, |
5475 | ArrayRef<TypeSourceInfo *> Args, |
5476 | SourceLocation RParenLoc, |
5477 | bool IsDependent) { |
5478 | if (IsDependent) |
5479 | return false; |
5480 | |
5481 | if (Kind <= UTT_Last) |
5482 | return EvaluateUnaryTypeTrait(Self&: S, UTT: Kind, KeyLoc: KWLoc, T: Args[0]->getType()); |
5483 | |
5484 | // Evaluate ReferenceBindsToTemporary and ReferenceConstructsFromTemporary |
5485 | // alongside the IsConstructible traits to avoid duplication. |
5486 | if (Kind <= BTT_Last && Kind != BTT_ReferenceBindsToTemporary && Kind != BTT_ReferenceConstructsFromTemporary) |
5487 | return EvaluateBinaryTypeTrait(Self&: S, BTT: Kind, LhsT: Args[0]->getType(), |
5488 | RhsT: Args[1]->getType(), KeyLoc: RParenLoc); |
5489 | |
5490 | switch (Kind) { |
5491 | case clang::BTT_ReferenceBindsToTemporary: |
5492 | case clang::BTT_ReferenceConstructsFromTemporary: |
5493 | case clang::TT_IsConstructible: |
5494 | case clang::TT_IsNothrowConstructible: |
5495 | case clang::TT_IsTriviallyConstructible: { |
5496 | // C++11 [meta.unary.prop]: |
5497 | // is_trivially_constructible is defined as: |
5498 | // |
5499 | // is_constructible<T, Args...>::value is true and the variable |
5500 | // definition for is_constructible, as defined below, is known to call |
5501 | // no operation that is not trivial. |
5502 | // |
5503 | // The predicate condition for a template specialization |
5504 | // is_constructible<T, Args...> shall be satisfied if and only if the |
5505 | // following variable definition would be well-formed for some invented |
5506 | // variable t: |
5507 | // |
5508 | // T t(create<Args>()...); |
5509 | assert(!Args.empty()); |
5510 | |
5511 | // Precondition: T and all types in the parameter pack Args shall be |
5512 | // complete types, (possibly cv-qualified) void, or arrays of |
5513 | // unknown bound. |
5514 | for (const auto *TSI : Args) { |
5515 | QualType ArgTy = TSI->getType(); |
5516 | if (ArgTy->isVoidType() || ArgTy->isIncompleteArrayType()) |
5517 | continue; |
5518 | |
5519 | if (S.RequireCompleteType(KWLoc, ArgTy, |
5520 | diag::err_incomplete_type_used_in_type_trait_expr)) |
5521 | return false; |
5522 | } |
5523 | |
5524 | // Make sure the first argument is not incomplete nor a function type. |
5525 | QualType T = Args[0]->getType(); |
5526 | if (T->isIncompleteType() || T->isFunctionType()) |
5527 | return false; |
5528 | |
5529 | // Make sure the first argument is not an abstract type. |
5530 | CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
5531 | if (RD && RD->isAbstract()) |
5532 | return false; |
5533 | |
5534 | llvm::BumpPtrAllocator OpaqueExprAllocator; |
5535 | SmallVector<Expr *, 2> ArgExprs; |
5536 | ArgExprs.reserve(N: Args.size() - 1); |
5537 | for (unsigned I = 1, N = Args.size(); I != N; ++I) { |
5538 | QualType ArgTy = Args[I]->getType(); |
5539 | if (ArgTy->isObjectType() || ArgTy->isFunctionType()) |
5540 | ArgTy = S.Context.getRValueReferenceType(T: ArgTy); |
5541 | ArgExprs.push_back( |
5542 | new (OpaqueExprAllocator.Allocate<OpaqueValueExpr>()) |
5543 | OpaqueValueExpr(Args[I]->getTypeLoc().getBeginLoc(), |
5544 | ArgTy.getNonLValueExprType(Context: S.Context), |
5545 | Expr::getValueKindForType(T: ArgTy))); |
5546 | } |
5547 | |
5548 | // Perform the initialization in an unevaluated context within a SFINAE |
5549 | // trap at translation unit scope. |
5550 | EnterExpressionEvaluationContext Unevaluated( |
5551 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
5552 | Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/true); |
5553 | Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl()); |
5554 | InitializedEntity To( |
5555 | InitializedEntity::InitializeTemporary(Context&: S.Context, TypeInfo: Args[0])); |
5556 | InitializationKind InitKind(InitializationKind::CreateDirect(InitLoc: KWLoc, LParenLoc: KWLoc, |
5557 | RParenLoc)); |
5558 | InitializationSequence Init(S, To, InitKind, ArgExprs); |
5559 | if (Init.Failed()) |
5560 | return false; |
5561 | |
5562 | ExprResult Result = Init.Perform(S, Entity: To, Kind: InitKind, Args: ArgExprs); |
5563 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
5564 | return false; |
5565 | |
5566 | if (Kind == clang::TT_IsConstructible) |
5567 | return true; |
5568 | |
5569 | if (Kind == clang::BTT_ReferenceBindsToTemporary || Kind == clang::BTT_ReferenceConstructsFromTemporary) { |
5570 | if (!T->isReferenceType()) |
5571 | return false; |
5572 | |
5573 | if (!Init.isDirectReferenceBinding()) |
5574 | return true; |
5575 | |
5576 | if (Kind == clang::BTT_ReferenceBindsToTemporary) |
5577 | return false; |
5578 | |
5579 | QualType U = Args[1]->getType(); |
5580 | if (U->isReferenceType()) |
5581 | return false; |
5582 | |
5583 | QualType TPtr = S.Context.getPointerType(T: S.BuiltinRemoveReference(BaseType: T, UKind: UnaryTransformType::RemoveCVRef, Loc: {})); |
5584 | QualType UPtr = S.Context.getPointerType(T: S.BuiltinRemoveReference(BaseType: U, UKind: UnaryTransformType::RemoveCVRef, Loc: {})); |
5585 | return EvaluateBinaryTypeTrait(Self&: S, BTT: TypeTrait::BTT_IsConvertibleTo, LhsT: UPtr, RhsT: TPtr, KeyLoc: RParenLoc); |
5586 | } |
5587 | |
5588 | if (Kind == clang::TT_IsNothrowConstructible) |
5589 | return S.canThrow(Result.get()) == CT_Cannot; |
5590 | |
5591 | if (Kind == clang::TT_IsTriviallyConstructible) { |
5592 | // Under Objective-C ARC and Weak, if the destination has non-trivial |
5593 | // Objective-C lifetime, this is a non-trivial construction. |
5594 | if (T.getNonReferenceType().hasNonTrivialObjCLifetime()) |
5595 | return false; |
5596 | |
5597 | // The initialization succeeded; now make sure there are no non-trivial |
5598 | // calls. |
5599 | return !Result.get()->hasNonTrivialCall(Ctx: S.Context); |
5600 | } |
5601 | |
5602 | llvm_unreachable("unhandled type trait" ); |
5603 | return false; |
5604 | } |
5605 | default: llvm_unreachable("not a TT" ); |
5606 | } |
5607 | |
5608 | return false; |
5609 | } |
5610 | |
5611 | namespace { |
5612 | void DiagnoseBuiltinDeprecation(Sema& S, TypeTrait Kind, |
5613 | SourceLocation KWLoc) { |
5614 | TypeTrait Replacement; |
5615 | switch (Kind) { |
5616 | case UTT_HasNothrowAssign: |
5617 | case UTT_HasNothrowMoveAssign: |
5618 | Replacement = BTT_IsNothrowAssignable; |
5619 | break; |
5620 | case UTT_HasNothrowCopy: |
5621 | case UTT_HasNothrowConstructor: |
5622 | Replacement = TT_IsNothrowConstructible; |
5623 | break; |
5624 | case UTT_HasTrivialAssign: |
5625 | case UTT_HasTrivialMoveAssign: |
5626 | Replacement = BTT_IsTriviallyAssignable; |
5627 | break; |
5628 | case UTT_HasTrivialCopy: |
5629 | Replacement = UTT_IsTriviallyCopyable; |
5630 | break; |
5631 | case UTT_HasTrivialDefaultConstructor: |
5632 | case UTT_HasTrivialMoveConstructor: |
5633 | Replacement = TT_IsTriviallyConstructible; |
5634 | break; |
5635 | case UTT_HasTrivialDestructor: |
5636 | Replacement = UTT_IsTriviallyDestructible; |
5637 | break; |
5638 | default: |
5639 | return; |
5640 | } |
5641 | S.Diag(KWLoc, diag::warn_deprecated_builtin) |
5642 | << getTraitSpelling(Kind) << getTraitSpelling(Replacement); |
5643 | } |
5644 | } |
5645 | |
5646 | bool Sema::CheckTypeTraitArity(unsigned Arity, SourceLocation Loc, size_t N) { |
5647 | if (Arity && N != Arity) { |
5648 | Diag(Loc, diag::err_type_trait_arity) |
5649 | << Arity << 0 << (Arity > 1) << (int)N << SourceRange(Loc); |
5650 | return false; |
5651 | } |
5652 | |
5653 | if (!Arity && N == 0) { |
5654 | Diag(Loc, diag::err_type_trait_arity) |
5655 | << 1 << 1 << 1 << (int)N << SourceRange(Loc); |
5656 | return false; |
5657 | } |
5658 | return true; |
5659 | } |
5660 | |
5661 | enum class TypeTraitReturnType { |
5662 | Bool, |
5663 | }; |
5664 | |
5665 | static TypeTraitReturnType GetReturnType(TypeTrait Kind) { |
5666 | return TypeTraitReturnType::Bool; |
5667 | } |
5668 | |
5669 | ExprResult Sema::BuildTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
5670 | ArrayRef<TypeSourceInfo *> Args, |
5671 | SourceLocation RParenLoc) { |
5672 | if (!CheckTypeTraitArity(Arity: getTypeTraitArity(T: Kind), Loc: KWLoc, N: Args.size())) |
5673 | return ExprError(); |
5674 | |
5675 | if (Kind <= UTT_Last && !CheckUnaryTypeTraitTypeCompleteness( |
5676 | S&: *this, UTT: Kind, Loc: KWLoc, ArgTy: Args[0]->getType())) |
5677 | return ExprError(); |
5678 | |
5679 | DiagnoseBuiltinDeprecation(S&: *this, Kind, KWLoc); |
5680 | |
5681 | bool Dependent = false; |
5682 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
5683 | if (Args[I]->getType()->isDependentType()) { |
5684 | Dependent = true; |
5685 | break; |
5686 | } |
5687 | } |
5688 | |
5689 | switch (GetReturnType(Kind)) { |
5690 | case TypeTraitReturnType::Bool: { |
5691 | bool Result = EvaluateBooleanTypeTrait(S&: *this, Kind, KWLoc, Args, RParenLoc, |
5692 | IsDependent: Dependent); |
5693 | return TypeTraitExpr::Create(C: Context, T: Context.getLogicalOperationType(), |
5694 | Loc: KWLoc, Kind, Args, RParenLoc, Value: Result); |
5695 | } |
5696 | } |
5697 | llvm_unreachable("unhandled type trait return type" ); |
5698 | } |
5699 | |
5700 | ExprResult Sema::ActOnTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
5701 | ArrayRef<ParsedType> Args, |
5702 | SourceLocation RParenLoc) { |
5703 | SmallVector<TypeSourceInfo *, 4> ConvertedArgs; |
5704 | ConvertedArgs.reserve(N: Args.size()); |
5705 | |
5706 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
5707 | TypeSourceInfo *TInfo; |
5708 | QualType T = GetTypeFromParser(Ty: Args[I], TInfo: &TInfo); |
5709 | if (!TInfo) |
5710 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc: KWLoc); |
5711 | |
5712 | ConvertedArgs.push_back(Elt: TInfo); |
5713 | } |
5714 | |
5715 | return BuildTypeTrait(Kind, KWLoc, Args: ConvertedArgs, RParenLoc); |
5716 | } |
5717 | |
5718 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
5719 | QualType RhsT, SourceLocation KeyLoc) { |
5720 | assert(!LhsT->isDependentType() && !RhsT->isDependentType() && |
5721 | "Cannot evaluate traits of dependent types" ); |
5722 | |
5723 | switch(BTT) { |
5724 | case BTT_IsBaseOf: { |
5725 | // C++0x [meta.rel]p2 |
5726 | // Base is a base class of Derived without regard to cv-qualifiers or |
5727 | // Base and Derived are not unions and name the same class type without |
5728 | // regard to cv-qualifiers. |
5729 | |
5730 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); |
5731 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); |
5732 | if (!rhsRecord || !lhsRecord) { |
5733 | const ObjCObjectType *LHSObjTy = LhsT->getAs<ObjCObjectType>(); |
5734 | const ObjCObjectType *RHSObjTy = RhsT->getAs<ObjCObjectType>(); |
5735 | if (!LHSObjTy || !RHSObjTy) |
5736 | return false; |
5737 | |
5738 | ObjCInterfaceDecl *BaseInterface = LHSObjTy->getInterface(); |
5739 | ObjCInterfaceDecl *DerivedInterface = RHSObjTy->getInterface(); |
5740 | if (!BaseInterface || !DerivedInterface) |
5741 | return false; |
5742 | |
5743 | if (Self.RequireCompleteType( |
5744 | KeyLoc, RhsT, diag::err_incomplete_type_used_in_type_trait_expr)) |
5745 | return false; |
5746 | |
5747 | return BaseInterface->isSuperClassOf(I: DerivedInterface); |
5748 | } |
5749 | |
5750 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) |
5751 | == (lhsRecord == rhsRecord)); |
5752 | |
5753 | // Unions are never base classes, and never have base classes. |
5754 | // It doesn't matter if they are complete or not. See PR#41843 |
5755 | if (lhsRecord && lhsRecord->getDecl()->isUnion()) |
5756 | return false; |
5757 | if (rhsRecord && rhsRecord->getDecl()->isUnion()) |
5758 | return false; |
5759 | |
5760 | if (lhsRecord == rhsRecord) |
5761 | return true; |
5762 | |
5763 | // C++0x [meta.rel]p2: |
5764 | // If Base and Derived are class types and are different types |
5765 | // (ignoring possible cv-qualifiers) then Derived shall be a |
5766 | // complete type. |
5767 | if (Self.RequireCompleteType(KeyLoc, RhsT, |
5768 | diag::err_incomplete_type_used_in_type_trait_expr)) |
5769 | return false; |
5770 | |
5771 | return cast<CXXRecordDecl>(Val: rhsRecord->getDecl()) |
5772 | ->isDerivedFrom(Base: cast<CXXRecordDecl>(Val: lhsRecord->getDecl())); |
5773 | } |
5774 | case BTT_IsSame: |
5775 | return Self.Context.hasSameType(T1: LhsT, T2: RhsT); |
5776 | case BTT_TypeCompatible: { |
5777 | // GCC ignores cv-qualifiers on arrays for this builtin. |
5778 | Qualifiers LhsQuals, RhsQuals; |
5779 | QualType Lhs = Self.getASTContext().getUnqualifiedArrayType(T: LhsT, Quals&: LhsQuals); |
5780 | QualType Rhs = Self.getASTContext().getUnqualifiedArrayType(T: RhsT, Quals&: RhsQuals); |
5781 | return Self.Context.typesAreCompatible(T1: Lhs, T2: Rhs); |
5782 | } |
5783 | case BTT_IsConvertible: |
5784 | case BTT_IsConvertibleTo: |
5785 | case BTT_IsNothrowConvertible: { |
5786 | // C++0x [meta.rel]p4: |
5787 | // Given the following function prototype: |
5788 | // |
5789 | // template <class T> |
5790 | // typename add_rvalue_reference<T>::type create(); |
5791 | // |
5792 | // the predicate condition for a template specialization |
5793 | // is_convertible<From, To> shall be satisfied if and only if |
5794 | // the return expression in the following code would be |
5795 | // well-formed, including any implicit conversions to the return |
5796 | // type of the function: |
5797 | // |
5798 | // To test() { |
5799 | // return create<From>(); |
5800 | // } |
5801 | // |
5802 | // Access checking is performed as if in a context unrelated to To and |
5803 | // From. Only the validity of the immediate context of the expression |
5804 | // of the return-statement (including conversions to the return type) |
5805 | // is considered. |
5806 | // |
5807 | // We model the initialization as a copy-initialization of a temporary |
5808 | // of the appropriate type, which for this expression is identical to the |
5809 | // return statement (since NRVO doesn't apply). |
5810 | |
5811 | // Functions aren't allowed to return function or array types. |
5812 | if (RhsT->isFunctionType() || RhsT->isArrayType()) |
5813 | return false; |
5814 | |
5815 | // A return statement in a void function must have void type. |
5816 | if (RhsT->isVoidType()) |
5817 | return LhsT->isVoidType(); |
5818 | |
5819 | // A function definition requires a complete, non-abstract return type. |
5820 | if (!Self.isCompleteType(Loc: KeyLoc, T: RhsT) || Self.isAbstractType(Loc: KeyLoc, T: RhsT)) |
5821 | return false; |
5822 | |
5823 | // Compute the result of add_rvalue_reference. |
5824 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
5825 | LhsT = Self.Context.getRValueReferenceType(T: LhsT); |
5826 | |
5827 | // Build a fake source and destination for initialization. |
5828 | InitializedEntity To(InitializedEntity::InitializeTemporary(Type: RhsT)); |
5829 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Context: Self.Context), |
5830 | Expr::getValueKindForType(T: LhsT)); |
5831 | Expr *FromPtr = &From; |
5832 | InitializationKind Kind(InitializationKind::CreateCopy(InitLoc: KeyLoc, |
5833 | EqualLoc: SourceLocation())); |
5834 | |
5835 | // Perform the initialization in an unevaluated context within a SFINAE |
5836 | // trap at translation unit scope. |
5837 | EnterExpressionEvaluationContext Unevaluated( |
5838 | Self, Sema::ExpressionEvaluationContext::Unevaluated); |
5839 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
5840 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
5841 | InitializationSequence Init(Self, To, Kind, FromPtr); |
5842 | if (Init.Failed()) |
5843 | return false; |
5844 | |
5845 | ExprResult Result = Init.Perform(S&: Self, Entity: To, Kind, Args: FromPtr); |
5846 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
5847 | return false; |
5848 | |
5849 | if (BTT != BTT_IsNothrowConvertible) |
5850 | return true; |
5851 | |
5852 | return Self.canThrow(Result.get()) == CT_Cannot; |
5853 | } |
5854 | |
5855 | case BTT_IsAssignable: |
5856 | case BTT_IsNothrowAssignable: |
5857 | case BTT_IsTriviallyAssignable: { |
5858 | // C++11 [meta.unary.prop]p3: |
5859 | // is_trivially_assignable is defined as: |
5860 | // is_assignable<T, U>::value is true and the assignment, as defined by |
5861 | // is_assignable, is known to call no operation that is not trivial |
5862 | // |
5863 | // is_assignable is defined as: |
5864 | // The expression declval<T>() = declval<U>() is well-formed when |
5865 | // treated as an unevaluated operand (Clause 5). |
5866 | // |
5867 | // For both, T and U shall be complete types, (possibly cv-qualified) |
5868 | // void, or arrays of unknown bound. |
5869 | if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType() && |
5870 | Self.RequireCompleteType(KeyLoc, LhsT, |
5871 | diag::err_incomplete_type_used_in_type_trait_expr)) |
5872 | return false; |
5873 | if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType() && |
5874 | Self.RequireCompleteType(KeyLoc, RhsT, |
5875 | diag::err_incomplete_type_used_in_type_trait_expr)) |
5876 | return false; |
5877 | |
5878 | // cv void is never assignable. |
5879 | if (LhsT->isVoidType() || RhsT->isVoidType()) |
5880 | return false; |
5881 | |
5882 | // Build expressions that emulate the effect of declval<T>() and |
5883 | // declval<U>(). |
5884 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
5885 | LhsT = Self.Context.getRValueReferenceType(T: LhsT); |
5886 | if (RhsT->isObjectType() || RhsT->isFunctionType()) |
5887 | RhsT = Self.Context.getRValueReferenceType(T: RhsT); |
5888 | OpaqueValueExpr Lhs(KeyLoc, LhsT.getNonLValueExprType(Context: Self.Context), |
5889 | Expr::getValueKindForType(T: LhsT)); |
5890 | OpaqueValueExpr Rhs(KeyLoc, RhsT.getNonLValueExprType(Context: Self.Context), |
5891 | Expr::getValueKindForType(T: RhsT)); |
5892 | |
5893 | // Attempt the assignment in an unevaluated context within a SFINAE |
5894 | // trap at translation unit scope. |
5895 | EnterExpressionEvaluationContext Unevaluated( |
5896 | Self, Sema::ExpressionEvaluationContext::Unevaluated); |
5897 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
5898 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
5899 | ExprResult Result = Self.BuildBinOp(/*S=*/nullptr, KeyLoc, BO_Assign, &Lhs, |
5900 | &Rhs); |
5901 | if (Result.isInvalid()) |
5902 | return false; |
5903 | |
5904 | // Treat the assignment as unused for the purpose of -Wdeprecated-volatile. |
5905 | Self.CheckUnusedVolatileAssignment(E: Result.get()); |
5906 | |
5907 | if (SFINAE.hasErrorOccurred()) |
5908 | return false; |
5909 | |
5910 | if (BTT == BTT_IsAssignable) |
5911 | return true; |
5912 | |
5913 | if (BTT == BTT_IsNothrowAssignable) |
5914 | return Self.canThrow(Result.get()) == CT_Cannot; |
5915 | |
5916 | if (BTT == BTT_IsTriviallyAssignable) { |
5917 | // Under Objective-C ARC and Weak, if the destination has non-trivial |
5918 | // Objective-C lifetime, this is a non-trivial assignment. |
5919 | if (LhsT.getNonReferenceType().hasNonTrivialObjCLifetime()) |
5920 | return false; |
5921 | |
5922 | return !Result.get()->hasNonTrivialCall(Ctx: Self.Context); |
5923 | } |
5924 | |
5925 | llvm_unreachable("unhandled type trait" ); |
5926 | return false; |
5927 | } |
5928 | default: llvm_unreachable("not a BTT" ); |
5929 | } |
5930 | llvm_unreachable("Unknown type trait or not implemented" ); |
5931 | } |
5932 | |
5933 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, |
5934 | SourceLocation KWLoc, |
5935 | ParsedType Ty, |
5936 | Expr* DimExpr, |
5937 | SourceLocation RParen) { |
5938 | TypeSourceInfo *TSInfo; |
5939 | QualType T = GetTypeFromParser(Ty, TInfo: &TSInfo); |
5940 | if (!TSInfo) |
5941 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
5942 | |
5943 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); |
5944 | } |
5945 | |
5946 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, |
5947 | QualType T, Expr *DimExpr, |
5948 | SourceLocation KeyLoc) { |
5949 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type" ); |
5950 | |
5951 | switch(ATT) { |
5952 | case ATT_ArrayRank: |
5953 | if (T->isArrayType()) { |
5954 | unsigned Dim = 0; |
5955 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
5956 | ++Dim; |
5957 | T = AT->getElementType(); |
5958 | } |
5959 | return Dim; |
5960 | } |
5961 | return 0; |
5962 | |
5963 | case ATT_ArrayExtent: { |
5964 | llvm::APSInt Value; |
5965 | uint64_t Dim; |
5966 | if (Self.VerifyIntegerConstantExpression( |
5967 | DimExpr, &Value, diag::err_dimension_expr_not_constant_integer) |
5968 | .isInvalid()) |
5969 | return 0; |
5970 | if (Value.isSigned() && Value.isNegative()) { |
5971 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) |
5972 | << DimExpr->getSourceRange(); |
5973 | return 0; |
5974 | } |
5975 | Dim = Value.getLimitedValue(); |
5976 | |
5977 | if (T->isArrayType()) { |
5978 | unsigned D = 0; |
5979 | bool Matched = false; |
5980 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
5981 | if (Dim == D) { |
5982 | Matched = true; |
5983 | break; |
5984 | } |
5985 | ++D; |
5986 | T = AT->getElementType(); |
5987 | } |
5988 | |
5989 | if (Matched && T->isArrayType()) { |
5990 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) |
5991 | return CAT->getSize().getLimitedValue(); |
5992 | } |
5993 | } |
5994 | return 0; |
5995 | } |
5996 | } |
5997 | llvm_unreachable("Unknown type trait or not implemented" ); |
5998 | } |
5999 | |
6000 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, |
6001 | SourceLocation KWLoc, |
6002 | TypeSourceInfo *TSInfo, |
6003 | Expr* DimExpr, |
6004 | SourceLocation RParen) { |
6005 | QualType T = TSInfo->getType(); |
6006 | |
6007 | // FIXME: This should likely be tracked as an APInt to remove any host |
6008 | // assumptions about the width of size_t on the target. |
6009 | uint64_t Value = 0; |
6010 | if (!T->isDependentType()) |
6011 | Value = EvaluateArrayTypeTrait(Self&: *this, ATT, T, DimExpr, KeyLoc: KWLoc); |
6012 | |
6013 | // While the specification for these traits from the Embarcadero C++ |
6014 | // compiler's documentation says the return type is 'unsigned int', Clang |
6015 | // returns 'size_t'. On Windows, the primary platform for the Embarcadero |
6016 | // compiler, there is no difference. On several other platforms this is an |
6017 | // important distinction. |
6018 | return new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, DimExpr, |
6019 | RParen, Context.getSizeType()); |
6020 | } |
6021 | |
6022 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, |
6023 | SourceLocation KWLoc, |
6024 | Expr *Queried, |
6025 | SourceLocation RParen) { |
6026 | // If error parsing the expression, ignore. |
6027 | if (!Queried) |
6028 | return ExprError(); |
6029 | |
6030 | ExprResult Result = BuildExpressionTrait(OET: ET, KWLoc, Queried, RParen); |
6031 | |
6032 | return Result; |
6033 | } |
6034 | |
6035 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { |
6036 | switch (ET) { |
6037 | case ET_IsLValueExpr: return E->isLValue(); |
6038 | case ET_IsRValueExpr: |
6039 | return E->isPRValue(); |
6040 | } |
6041 | llvm_unreachable("Expression trait not covered by switch" ); |
6042 | } |
6043 | |
6044 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, |
6045 | SourceLocation KWLoc, |
6046 | Expr *Queried, |
6047 | SourceLocation RParen) { |
6048 | if (Queried->isTypeDependent()) { |
6049 | // Delay type-checking for type-dependent expressions. |
6050 | } else if (Queried->hasPlaceholderType()) { |
6051 | ExprResult PE = CheckPlaceholderExpr(E: Queried); |
6052 | if (PE.isInvalid()) return ExprError(); |
6053 | return BuildExpressionTrait(ET, KWLoc, Queried: PE.get(), RParen); |
6054 | } |
6055 | |
6056 | bool Value = EvaluateExpressionTrait(ET, E: Queried); |
6057 | |
6058 | return new (Context) |
6059 | ExpressionTraitExpr(KWLoc, ET, Queried, Value, RParen, Context.BoolTy); |
6060 | } |
6061 | |
6062 | QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, |
6063 | ExprValueKind &VK, |
6064 | SourceLocation Loc, |
6065 | bool isIndirect) { |
6066 | assert(!LHS.get()->hasPlaceholderType() && !RHS.get()->hasPlaceholderType() && |
6067 | "placeholders should have been weeded out by now" ); |
6068 | |
6069 | // The LHS undergoes lvalue conversions if this is ->*, and undergoes the |
6070 | // temporary materialization conversion otherwise. |
6071 | if (isIndirect) |
6072 | LHS = DefaultLvalueConversion(E: LHS.get()); |
6073 | else if (LHS.get()->isPRValue()) |
6074 | LHS = TemporaryMaterializationConversion(E: LHS.get()); |
6075 | if (LHS.isInvalid()) |
6076 | return QualType(); |
6077 | |
6078 | // The RHS always undergoes lvalue conversions. |
6079 | RHS = DefaultLvalueConversion(E: RHS.get()); |
6080 | if (RHS.isInvalid()) return QualType(); |
6081 | |
6082 | const char *OpSpelling = isIndirect ? "->*" : ".*" ; |
6083 | // C++ 5.5p2 |
6084 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
6085 | // be of type "pointer to member of T" (where T is a completely-defined |
6086 | // class type) [...] |
6087 | QualType RHSType = RHS.get()->getType(); |
6088 | const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>(); |
6089 | if (!MemPtr) { |
6090 | Diag(Loc, diag::err_bad_memptr_rhs) |
6091 | << OpSpelling << RHSType << RHS.get()->getSourceRange(); |
6092 | return QualType(); |
6093 | } |
6094 | |
6095 | QualType Class(MemPtr->getClass(), 0); |
6096 | |
6097 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the |
6098 | // member pointer points must be completely-defined. However, there is no |
6099 | // reason for this semantic distinction, and the rule is not enforced by |
6100 | // other compilers. Therefore, we do not check this property, as it is |
6101 | // likely to be considered a defect. |
6102 | |
6103 | // C++ 5.5p2 |
6104 | // [...] to its first operand, which shall be of class T or of a class of |
6105 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
6106 | // such a class] |
6107 | QualType LHSType = LHS.get()->getType(); |
6108 | if (isIndirect) { |
6109 | if (const PointerType *Ptr = LHSType->getAs<PointerType>()) |
6110 | LHSType = Ptr->getPointeeType(); |
6111 | else { |
6112 | Diag(Loc, diag::err_bad_memptr_lhs) |
6113 | << OpSpelling << 1 << LHSType |
6114 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*" ); |
6115 | return QualType(); |
6116 | } |
6117 | } |
6118 | |
6119 | if (!Context.hasSameUnqualifiedType(T1: Class, T2: LHSType)) { |
6120 | // If we want to check the hierarchy, we need a complete type. |
6121 | if (RequireCompleteType(Loc, LHSType, diag::err_bad_memptr_lhs, |
6122 | OpSpelling, (int)isIndirect)) { |
6123 | return QualType(); |
6124 | } |
6125 | |
6126 | if (!IsDerivedFrom(Loc, Derived: LHSType, Base: Class)) { |
6127 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
6128 | << (int)isIndirect << LHS.get()->getType(); |
6129 | return QualType(); |
6130 | } |
6131 | |
6132 | CXXCastPath BasePath; |
6133 | if (CheckDerivedToBaseConversion( |
6134 | Derived: LHSType, Base: Class, Loc, |
6135 | Range: SourceRange(LHS.get()->getBeginLoc(), RHS.get()->getEndLoc()), |
6136 | BasePath: &BasePath)) |
6137 | return QualType(); |
6138 | |
6139 | // Cast LHS to type of use. |
6140 | QualType UseType = Context.getQualifiedType(T: Class, Qs: LHSType.getQualifiers()); |
6141 | if (isIndirect) |
6142 | UseType = Context.getPointerType(T: UseType); |
6143 | ExprValueKind VK = isIndirect ? VK_PRValue : LHS.get()->getValueKind(); |
6144 | LHS = ImpCastExprToType(E: LHS.get(), Type: UseType, CK: CK_DerivedToBase, VK, |
6145 | BasePath: &BasePath); |
6146 | } |
6147 | |
6148 | if (isa<CXXScalarValueInitExpr>(Val: RHS.get()->IgnoreParens())) { |
6149 | // Diagnose use of pointer-to-member type which when used as |
6150 | // the functional cast in a pointer-to-member expression. |
6151 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
6152 | return QualType(); |
6153 | } |
6154 | |
6155 | // C++ 5.5p2 |
6156 | // The result is an object or a function of the type specified by the |
6157 | // second operand. |
6158 | // The cv qualifiers are the union of those in the pointer and the left side, |
6159 | // in accordance with 5.5p5 and 5.2.5. |
6160 | QualType Result = MemPtr->getPointeeType(); |
6161 | Result = Context.getCVRQualifiedType(T: Result, CVR: LHSType.getCVRQualifiers()); |
6162 | |
6163 | // C++0x [expr.mptr.oper]p6: |
6164 | // In a .* expression whose object expression is an rvalue, the program is |
6165 | // ill-formed if the second operand is a pointer to member function with |
6166 | // ref-qualifier &. In a ->* expression or in a .* expression whose object |
6167 | // expression is an lvalue, the program is ill-formed if the second operand |
6168 | // is a pointer to member function with ref-qualifier &&. |
6169 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { |
6170 | switch (Proto->getRefQualifier()) { |
6171 | case RQ_None: |
6172 | // Do nothing |
6173 | break; |
6174 | |
6175 | case RQ_LValue: |
6176 | if (!isIndirect && !LHS.get()->Classify(Ctx&: Context).isLValue()) { |
6177 | // C++2a allows functions with ref-qualifier & if their cv-qualifier-seq |
6178 | // is (exactly) 'const'. |
6179 | if (Proto->isConst() && !Proto->isVolatile()) |
6180 | Diag(Loc, getLangOpts().CPlusPlus20 |
6181 | ? diag::warn_cxx17_compat_pointer_to_const_ref_member_on_rvalue |
6182 | : diag::ext_pointer_to_const_ref_member_on_rvalue); |
6183 | else |
6184 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
6185 | << RHSType << 1 << LHS.get()->getSourceRange(); |
6186 | } |
6187 | break; |
6188 | |
6189 | case RQ_RValue: |
6190 | if (isIndirect || !LHS.get()->Classify(Context).isRValue()) |
6191 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
6192 | << RHSType << 0 << LHS.get()->getSourceRange(); |
6193 | break; |
6194 | } |
6195 | } |
6196 | |
6197 | // C++ [expr.mptr.oper]p6: |
6198 | // The result of a .* expression whose second operand is a pointer |
6199 | // to a data member is of the same value category as its |
6200 | // first operand. The result of a .* expression whose second |
6201 | // operand is a pointer to a member function is a prvalue. The |
6202 | // result of an ->* expression is an lvalue if its second operand |
6203 | // is a pointer to data member and a prvalue otherwise. |
6204 | if (Result->isFunctionType()) { |
6205 | VK = VK_PRValue; |
6206 | return Context.BoundMemberTy; |
6207 | } else if (isIndirect) { |
6208 | VK = VK_LValue; |
6209 | } else { |
6210 | VK = LHS.get()->getValueKind(); |
6211 | } |
6212 | |
6213 | return Result; |
6214 | } |
6215 | |
6216 | /// Try to convert a type to another according to C++11 5.16p3. |
6217 | /// |
6218 | /// This is part of the parameter validation for the ? operator. If either |
6219 | /// value operand is a class type, the two operands are attempted to be |
6220 | /// converted to each other. This function does the conversion in one direction. |
6221 | /// It returns true if the program is ill-formed and has already been diagnosed |
6222 | /// as such. |
6223 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
6224 | SourceLocation QuestionLoc, |
6225 | bool &HaveConversion, |
6226 | QualType &ToType) { |
6227 | HaveConversion = false; |
6228 | ToType = To->getType(); |
6229 | |
6230 | InitializationKind Kind = |
6231 | InitializationKind::CreateCopy(InitLoc: To->getBeginLoc(), EqualLoc: SourceLocation()); |
6232 | // C++11 5.16p3 |
6233 | // The process for determining whether an operand expression E1 of type T1 |
6234 | // can be converted to match an operand expression E2 of type T2 is defined |
6235 | // as follows: |
6236 | // -- If E2 is an lvalue: E1 can be converted to match E2 if E1 can be |
6237 | // implicitly converted to type "lvalue reference to T2", subject to the |
6238 | // constraint that in the conversion the reference must bind directly to |
6239 | // an lvalue. |
6240 | // -- If E2 is an xvalue: E1 can be converted to match E2 if E1 can be |
6241 | // implicitly converted to the type "rvalue reference to R2", subject to |
6242 | // the constraint that the reference must bind directly. |
6243 | if (To->isGLValue()) { |
6244 | QualType T = Self.Context.getReferenceQualifiedType(e: To); |
6245 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(Type: T); |
6246 | |
6247 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
6248 | if (InitSeq.isDirectReferenceBinding()) { |
6249 | ToType = T; |
6250 | HaveConversion = true; |
6251 | return false; |
6252 | } |
6253 | |
6254 | if (InitSeq.isAmbiguous()) |
6255 | return InitSeq.Diagnose(S&: Self, Entity, Kind, Args: From); |
6256 | } |
6257 | |
6258 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
6259 | // -- if E1 and E2 have class type, and the underlying class types are |
6260 | // the same or one is a base class of the other: |
6261 | QualType FTy = From->getType(); |
6262 | QualType TTy = To->getType(); |
6263 | const RecordType *FRec = FTy->getAs<RecordType>(); |
6264 | const RecordType *TRec = TTy->getAs<RecordType>(); |
6265 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
6266 | Self.IsDerivedFrom(Loc: QuestionLoc, Derived: FTy, Base: TTy); |
6267 | if (FRec && TRec && (FRec == TRec || FDerivedFromT || |
6268 | Self.IsDerivedFrom(Loc: QuestionLoc, Derived: TTy, Base: FTy))) { |
6269 | // E1 can be converted to match E2 if the class of T2 is the |
6270 | // same type as, or a base class of, the class of T1, and |
6271 | // [cv2 > cv1]. |
6272 | if (FRec == TRec || FDerivedFromT) { |
6273 | if (TTy.isAtLeastAsQualifiedAs(other: FTy)) { |
6274 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(Type: TTy); |
6275 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
6276 | if (InitSeq) { |
6277 | HaveConversion = true; |
6278 | return false; |
6279 | } |
6280 | |
6281 | if (InitSeq.isAmbiguous()) |
6282 | return InitSeq.Diagnose(S&: Self, Entity, Kind, Args: From); |
6283 | } |
6284 | } |
6285 | |
6286 | return false; |
6287 | } |
6288 | |
6289 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
6290 | // implicitly converted to the type that expression E2 would have |
6291 | // if E2 were converted to an rvalue (or the type it has, if E2 is |
6292 | // an rvalue). |
6293 | // |
6294 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
6295 | // to the array-to-pointer or function-to-pointer conversions. |
6296 | TTy = TTy.getNonLValueExprType(Context: Self.Context); |
6297 | |
6298 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(Type: TTy); |
6299 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
6300 | HaveConversion = !InitSeq.Failed(); |
6301 | ToType = TTy; |
6302 | if (InitSeq.isAmbiguous()) |
6303 | return InitSeq.Diagnose(S&: Self, Entity, Kind, Args: From); |
6304 | |
6305 | return false; |
6306 | } |
6307 | |
6308 | /// Try to find a common type for two according to C++0x 5.16p5. |
6309 | /// |
6310 | /// This is part of the parameter validation for the ? operator. If either |
6311 | /// value operand is a class type, overload resolution is used to find a |
6312 | /// conversion to a common type. |
6313 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, |
6314 | SourceLocation QuestionLoc) { |
6315 | Expr *Args[2] = { LHS.get(), RHS.get() }; |
6316 | OverloadCandidateSet CandidateSet(QuestionLoc, |
6317 | OverloadCandidateSet::CSK_Operator); |
6318 | Self.AddBuiltinOperatorCandidates(Op: OO_Conditional, OpLoc: QuestionLoc, Args, |
6319 | CandidateSet); |
6320 | |
6321 | OverloadCandidateSet::iterator Best; |
6322 | switch (CandidateSet.BestViableFunction(S&: Self, Loc: QuestionLoc, Best)) { |
6323 | case OR_Success: { |
6324 | // We found a match. Perform the conversions on the arguments and move on. |
6325 | ExprResult LHSRes = Self.PerformImplicitConversion( |
6326 | LHS.get(), Best->BuiltinParamTypes[0], Best->Conversions[0], |
6327 | Sema::AA_Converting); |
6328 | if (LHSRes.isInvalid()) |
6329 | break; |
6330 | LHS = LHSRes; |
6331 | |
6332 | ExprResult RHSRes = Self.PerformImplicitConversion( |
6333 | RHS.get(), Best->BuiltinParamTypes[1], Best->Conversions[1], |
6334 | Sema::AA_Converting); |
6335 | if (RHSRes.isInvalid()) |
6336 | break; |
6337 | RHS = RHSRes; |
6338 | if (Best->Function) |
6339 | Self.MarkFunctionReferenced(Loc: QuestionLoc, Func: Best->Function); |
6340 | return false; |
6341 | } |
6342 | |
6343 | case OR_No_Viable_Function: |
6344 | |
6345 | // Emit a better diagnostic if one of the expressions is a null pointer |
6346 | // constant and the other is a pointer type. In this case, the user most |
6347 | // likely forgot to take the address of the other expression. |
6348 | if (Self.DiagnoseConditionalForNull(LHSExpr: LHS.get(), RHSExpr: RHS.get(), QuestionLoc)) |
6349 | return true; |
6350 | |
6351 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
6352 | << LHS.get()->getType() << RHS.get()->getType() |
6353 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
6354 | return true; |
6355 | |
6356 | case OR_Ambiguous: |
6357 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) |
6358 | << LHS.get()->getType() << RHS.get()->getType() |
6359 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
6360 | // FIXME: Print the possible common types by printing the return types of |
6361 | // the viable candidates. |
6362 | break; |
6363 | |
6364 | case OR_Deleted: |
6365 | llvm_unreachable("Conditional operator has only built-in overloads" ); |
6366 | } |
6367 | return true; |
6368 | } |
6369 | |
6370 | /// Perform an "extended" implicit conversion as returned by |
6371 | /// TryClassUnification. |
6372 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { |
6373 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(Type: T); |
6374 | InitializationKind Kind = |
6375 | InitializationKind::CreateCopy(InitLoc: E.get()->getBeginLoc(), EqualLoc: SourceLocation()); |
6376 | Expr *Arg = E.get(); |
6377 | InitializationSequence InitSeq(Self, Entity, Kind, Arg); |
6378 | ExprResult Result = InitSeq.Perform(S&: Self, Entity, Kind, Args: Arg); |
6379 | if (Result.isInvalid()) |
6380 | return true; |
6381 | |
6382 | E = Result; |
6383 | return false; |
6384 | } |
6385 | |
6386 | // Check the condition operand of ?: to see if it is valid for the GCC |
6387 | // extension. |
6388 | static bool isValidVectorForConditionalCondition(ASTContext &Ctx, |
6389 | QualType CondTy) { |
6390 | if (!CondTy->isVectorType() && !CondTy->isExtVectorType()) |
6391 | return false; |
6392 | const QualType EltTy = |
6393 | cast<VectorType>(Val: CondTy.getCanonicalType())->getElementType(); |
6394 | assert(!EltTy->isEnumeralType() && "Vectors cant be enum types" ); |
6395 | return EltTy->isIntegralType(Ctx); |
6396 | } |
6397 | |
6398 | static bool isValidSizelessVectorForConditionalCondition(ASTContext &Ctx, |
6399 | QualType CondTy) { |
6400 | if (!CondTy->isSveVLSBuiltinType()) |
6401 | return false; |
6402 | const QualType EltTy = |
6403 | cast<BuiltinType>(Val: CondTy.getCanonicalType())->getSveEltType(Ctx); |
6404 | assert(!EltTy->isEnumeralType() && "Vectors cant be enum types" ); |
6405 | return EltTy->isIntegralType(Ctx); |
6406 | } |
6407 | |
6408 | QualType Sema::CheckVectorConditionalTypes(ExprResult &Cond, ExprResult &LHS, |
6409 | ExprResult &RHS, |
6410 | SourceLocation QuestionLoc) { |
6411 | LHS = DefaultFunctionArrayLvalueConversion(E: LHS.get()); |
6412 | RHS = DefaultFunctionArrayLvalueConversion(E: RHS.get()); |
6413 | |
6414 | QualType CondType = Cond.get()->getType(); |
6415 | const auto *CondVT = CondType->castAs<VectorType>(); |
6416 | QualType CondElementTy = CondVT->getElementType(); |
6417 | unsigned CondElementCount = CondVT->getNumElements(); |
6418 | QualType LHSType = LHS.get()->getType(); |
6419 | const auto *LHSVT = LHSType->getAs<VectorType>(); |
6420 | QualType RHSType = RHS.get()->getType(); |
6421 | const auto *RHSVT = RHSType->getAs<VectorType>(); |
6422 | |
6423 | QualType ResultType; |
6424 | |
6425 | |
6426 | if (LHSVT && RHSVT) { |
6427 | if (isa<ExtVectorType>(Val: CondVT) != isa<ExtVectorType>(Val: LHSVT)) { |
6428 | Diag(QuestionLoc, diag::err_conditional_vector_cond_result_mismatch) |
6429 | << /*isExtVector*/ isa<ExtVectorType>(CondVT); |
6430 | return {}; |
6431 | } |
6432 | |
6433 | // If both are vector types, they must be the same type. |
6434 | if (!Context.hasSameType(T1: LHSType, T2: RHSType)) { |
6435 | Diag(QuestionLoc, diag::err_conditional_vector_mismatched) |
6436 | << LHSType << RHSType; |
6437 | return {}; |
6438 | } |
6439 | ResultType = Context.getCommonSugaredType(X: LHSType, Y: RHSType); |
6440 | } else if (LHSVT || RHSVT) { |
6441 | ResultType = CheckVectorOperands( |
6442 | LHS, RHS, Loc: QuestionLoc, /*isCompAssign*/ IsCompAssign: false, /*AllowBothBool*/ true, |
6443 | /*AllowBoolConversions*/ AllowBoolConversion: false, |
6444 | /*AllowBoolOperation*/ true, |
6445 | /*ReportInvalid*/ true); |
6446 | if (ResultType.isNull()) |
6447 | return {}; |
6448 | } else { |
6449 | // Both are scalar. |
6450 | LHSType = LHSType.getUnqualifiedType(); |
6451 | RHSType = RHSType.getUnqualifiedType(); |
6452 | QualType ResultElementTy = |
6453 | Context.hasSameType(T1: LHSType, T2: RHSType) |
6454 | ? Context.getCommonSugaredType(X: LHSType, Y: RHSType) |
6455 | : UsualArithmeticConversions(LHS, RHS, Loc: QuestionLoc, |
6456 | ACK: ACK_Conditional); |
6457 | |
6458 | if (ResultElementTy->isEnumeralType()) { |
6459 | Diag(QuestionLoc, diag::err_conditional_vector_operand_type) |
6460 | << ResultElementTy; |
6461 | return {}; |
6462 | } |
6463 | if (CondType->isExtVectorType()) |
6464 | ResultType = |
6465 | Context.getExtVectorType(VectorType: ResultElementTy, NumElts: CondVT->getNumElements()); |
6466 | else |
6467 | ResultType = Context.getVectorType( |
6468 | VectorType: ResultElementTy, NumElts: CondVT->getNumElements(), VecKind: VectorKind::Generic); |
6469 | |
6470 | LHS = ImpCastExprToType(E: LHS.get(), Type: ResultType, CK: CK_VectorSplat); |
6471 | RHS = ImpCastExprToType(E: RHS.get(), Type: ResultType, CK: CK_VectorSplat); |
6472 | } |
6473 | |
6474 | assert(!ResultType.isNull() && ResultType->isVectorType() && |
6475 | (!CondType->isExtVectorType() || ResultType->isExtVectorType()) && |
6476 | "Result should have been a vector type" ); |
6477 | auto *ResultVectorTy = ResultType->castAs<VectorType>(); |
6478 | QualType ResultElementTy = ResultVectorTy->getElementType(); |
6479 | unsigned ResultElementCount = ResultVectorTy->getNumElements(); |
6480 | |
6481 | if (ResultElementCount != CondElementCount) { |
6482 | Diag(QuestionLoc, diag::err_conditional_vector_size) << CondType |
6483 | << ResultType; |
6484 | return {}; |
6485 | } |
6486 | |
6487 | if (Context.getTypeSize(T: ResultElementTy) != |
6488 | Context.getTypeSize(T: CondElementTy)) { |
6489 | Diag(QuestionLoc, diag::err_conditional_vector_element_size) << CondType |
6490 | << ResultType; |
6491 | return {}; |
6492 | } |
6493 | |
6494 | return ResultType; |
6495 | } |
6496 | |
6497 | QualType Sema::CheckSizelessVectorConditionalTypes(ExprResult &Cond, |
6498 | ExprResult &LHS, |
6499 | ExprResult &RHS, |
6500 | SourceLocation QuestionLoc) { |
6501 | LHS = DefaultFunctionArrayLvalueConversion(E: LHS.get()); |
6502 | RHS = DefaultFunctionArrayLvalueConversion(E: RHS.get()); |
6503 | |
6504 | QualType CondType = Cond.get()->getType(); |
6505 | const auto *CondBT = CondType->castAs<BuiltinType>(); |
6506 | QualType CondElementTy = CondBT->getSveEltType(Context); |
6507 | llvm::ElementCount CondElementCount = |
6508 | Context.getBuiltinVectorTypeInfo(VecTy: CondBT).EC; |
6509 | |
6510 | QualType LHSType = LHS.get()->getType(); |
6511 | const auto *LHSBT = |
6512 | LHSType->isSveVLSBuiltinType() ? LHSType->getAs<BuiltinType>() : nullptr; |
6513 | QualType RHSType = RHS.get()->getType(); |
6514 | const auto *RHSBT = |
6515 | RHSType->isSveVLSBuiltinType() ? RHSType->getAs<BuiltinType>() : nullptr; |
6516 | |
6517 | QualType ResultType; |
6518 | |
6519 | if (LHSBT && RHSBT) { |
6520 | // If both are sizeless vector types, they must be the same type. |
6521 | if (!Context.hasSameType(T1: LHSType, T2: RHSType)) { |
6522 | Diag(QuestionLoc, diag::err_conditional_vector_mismatched) |
6523 | << LHSType << RHSType; |
6524 | return QualType(); |
6525 | } |
6526 | ResultType = LHSType; |
6527 | } else if (LHSBT || RHSBT) { |
6528 | ResultType = CheckSizelessVectorOperands( |
6529 | LHS, RHS, Loc: QuestionLoc, /*IsCompAssign*/ false, OperationKind: ACK_Conditional); |
6530 | if (ResultType.isNull()) |
6531 | return QualType(); |
6532 | } else { |
6533 | // Both are scalar so splat |
6534 | QualType ResultElementTy; |
6535 | LHSType = LHSType.getCanonicalType().getUnqualifiedType(); |
6536 | RHSType = RHSType.getCanonicalType().getUnqualifiedType(); |
6537 | |
6538 | if (Context.hasSameType(T1: LHSType, T2: RHSType)) |
6539 | ResultElementTy = LHSType; |
6540 | else |
6541 | ResultElementTy = |
6542 | UsualArithmeticConversions(LHS, RHS, Loc: QuestionLoc, ACK: ACK_Conditional); |
6543 | |
6544 | if (ResultElementTy->isEnumeralType()) { |
6545 | Diag(QuestionLoc, diag::err_conditional_vector_operand_type) |
6546 | << ResultElementTy; |
6547 | return QualType(); |
6548 | } |
6549 | |
6550 | ResultType = Context.getScalableVectorType( |
6551 | EltTy: ResultElementTy, NumElts: CondElementCount.getKnownMinValue()); |
6552 | |
6553 | LHS = ImpCastExprToType(E: LHS.get(), Type: ResultType, CK: CK_VectorSplat); |
6554 | RHS = ImpCastExprToType(E: RHS.get(), Type: ResultType, CK: CK_VectorSplat); |
6555 | } |
6556 | |
6557 | assert(!ResultType.isNull() && ResultType->isSveVLSBuiltinType() && |
6558 | "Result should have been a vector type" ); |
6559 | auto *ResultBuiltinTy = ResultType->castAs<BuiltinType>(); |
6560 | QualType ResultElementTy = ResultBuiltinTy->getSveEltType(Context); |
6561 | llvm::ElementCount ResultElementCount = |
6562 | Context.getBuiltinVectorTypeInfo(VecTy: ResultBuiltinTy).EC; |
6563 | |
6564 | if (ResultElementCount != CondElementCount) { |
6565 | Diag(QuestionLoc, diag::err_conditional_vector_size) |
6566 | << CondType << ResultType; |
6567 | return QualType(); |
6568 | } |
6569 | |
6570 | if (Context.getTypeSize(T: ResultElementTy) != |
6571 | Context.getTypeSize(T: CondElementTy)) { |
6572 | Diag(QuestionLoc, diag::err_conditional_vector_element_size) |
6573 | << CondType << ResultType; |
6574 | return QualType(); |
6575 | } |
6576 | |
6577 | return ResultType; |
6578 | } |
6579 | |
6580 | /// Check the operands of ?: under C++ semantics. |
6581 | /// |
6582 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
6583 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
6584 | /// |
6585 | /// This function also implements GCC's vector extension and the |
6586 | /// OpenCL/ext_vector_type extension for conditionals. The vector extensions |
6587 | /// permit the use of a?b:c where the type of a is that of a integer vector with |
6588 | /// the same number of elements and size as the vectors of b and c. If one of |
6589 | /// either b or c is a scalar it is implicitly converted to match the type of |
6590 | /// the vector. Otherwise the expression is ill-formed. If both b and c are |
6591 | /// scalars, then b and c are checked and converted to the type of a if |
6592 | /// possible. |
6593 | /// |
6594 | /// The expressions are evaluated differently for GCC's and OpenCL's extensions. |
6595 | /// For the GCC extension, the ?: operator is evaluated as |
6596 | /// (a[0] != 0 ? b[0] : c[0], .. , a[n] != 0 ? b[n] : c[n]). |
6597 | /// For the OpenCL extensions, the ?: operator is evaluated as |
6598 | /// (most-significant-bit-set(a[0]) ? b[0] : c[0], .. , |
6599 | /// most-significant-bit-set(a[n]) ? b[n] : c[n]). |
6600 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
6601 | ExprResult &RHS, ExprValueKind &VK, |
6602 | ExprObjectKind &OK, |
6603 | SourceLocation QuestionLoc) { |
6604 | // FIXME: Handle C99's complex types, block pointers and Obj-C++ interface |
6605 | // pointers. |
6606 | |
6607 | // Assume r-value. |
6608 | VK = VK_PRValue; |
6609 | OK = OK_Ordinary; |
6610 | bool IsVectorConditional = |
6611 | isValidVectorForConditionalCondition(Ctx&: Context, CondTy: Cond.get()->getType()); |
6612 | |
6613 | bool IsSizelessVectorConditional = |
6614 | isValidSizelessVectorForConditionalCondition(Ctx&: Context, |
6615 | CondTy: Cond.get()->getType()); |
6616 | |
6617 | // C++11 [expr.cond]p1 |
6618 | // The first expression is contextually converted to bool. |
6619 | if (!Cond.get()->isTypeDependent()) { |
6620 | ExprResult CondRes = IsVectorConditional || IsSizelessVectorConditional |
6621 | ? DefaultFunctionArrayLvalueConversion(E: Cond.get()) |
6622 | : CheckCXXBooleanCondition(CondExpr: Cond.get()); |
6623 | if (CondRes.isInvalid()) |
6624 | return QualType(); |
6625 | Cond = CondRes; |
6626 | } else { |
6627 | // To implement C++, the first expression typically doesn't alter the result |
6628 | // type of the conditional, however the GCC compatible vector extension |
6629 | // changes the result type to be that of the conditional. Since we cannot |
6630 | // know if this is a vector extension here, delay the conversion of the |
6631 | // LHS/RHS below until later. |
6632 | return Context.DependentTy; |
6633 | } |
6634 | |
6635 | |
6636 | // Either of the arguments dependent? |
6637 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) |
6638 | return Context.DependentTy; |
6639 | |
6640 | // C++11 [expr.cond]p2 |
6641 | // If either the second or the third operand has type (cv) void, ... |
6642 | QualType LTy = LHS.get()->getType(); |
6643 | QualType RTy = RHS.get()->getType(); |
6644 | bool LVoid = LTy->isVoidType(); |
6645 | bool RVoid = RTy->isVoidType(); |
6646 | if (LVoid || RVoid) { |
6647 | // ... one of the following shall hold: |
6648 | // -- The second or the third operand (but not both) is a (possibly |
6649 | // parenthesized) throw-expression; the result is of the type |
6650 | // and value category of the other. |
6651 | bool LThrow = isa<CXXThrowExpr>(Val: LHS.get()->IgnoreParenImpCasts()); |
6652 | bool RThrow = isa<CXXThrowExpr>(Val: RHS.get()->IgnoreParenImpCasts()); |
6653 | |
6654 | // Void expressions aren't legal in the vector-conditional expressions. |
6655 | if (IsVectorConditional) { |
6656 | SourceRange DiagLoc = |
6657 | LVoid ? LHS.get()->getSourceRange() : RHS.get()->getSourceRange(); |
6658 | bool IsThrow = LVoid ? LThrow : RThrow; |
6659 | Diag(DiagLoc.getBegin(), diag::err_conditional_vector_has_void) |
6660 | << DiagLoc << IsThrow; |
6661 | return QualType(); |
6662 | } |
6663 | |
6664 | if (LThrow != RThrow) { |
6665 | Expr *NonThrow = LThrow ? RHS.get() : LHS.get(); |
6666 | VK = NonThrow->getValueKind(); |
6667 | // DR (no number yet): the result is a bit-field if the |
6668 | // non-throw-expression operand is a bit-field. |
6669 | OK = NonThrow->getObjectKind(); |
6670 | return NonThrow->getType(); |
6671 | } |
6672 | |
6673 | // -- Both the second and third operands have type void; the result is of |
6674 | // type void and is a prvalue. |
6675 | if (LVoid && RVoid) |
6676 | return Context.getCommonSugaredType(X: LTy, Y: RTy); |
6677 | |
6678 | // Neither holds, error. |
6679 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
6680 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
6681 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
6682 | return QualType(); |
6683 | } |
6684 | |
6685 | // Neither is void. |
6686 | if (IsVectorConditional) |
6687 | return CheckVectorConditionalTypes(Cond, LHS, RHS, QuestionLoc); |
6688 | |
6689 | if (IsSizelessVectorConditional) |
6690 | return CheckSizelessVectorConditionalTypes(Cond, LHS, RHS, QuestionLoc); |
6691 | |
6692 | // WebAssembly tables are not allowed as conditional LHS or RHS. |
6693 | if (LTy->isWebAssemblyTableType() || RTy->isWebAssemblyTableType()) { |
6694 | Diag(QuestionLoc, diag::err_wasm_table_conditional_expression) |
6695 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
6696 | return QualType(); |
6697 | } |
6698 | |
6699 | // C++11 [expr.cond]p3 |
6700 | // Otherwise, if the second and third operand have different types, and |
6701 | // either has (cv) class type [...] an attempt is made to convert each of |
6702 | // those operands to the type of the other. |
6703 | if (!Context.hasSameType(T1: LTy, T2: RTy) && |
6704 | (LTy->isRecordType() || RTy->isRecordType())) { |
6705 | // These return true if a single direction is already ambiguous. |
6706 | QualType L2RType, R2LType; |
6707 | bool HaveL2R, HaveR2L; |
6708 | if (TryClassUnification(Self&: *this, From: LHS.get(), To: RHS.get(), QuestionLoc, HaveConversion&: HaveL2R, ToType&: L2RType)) |
6709 | return QualType(); |
6710 | if (TryClassUnification(Self&: *this, From: RHS.get(), To: LHS.get(), QuestionLoc, HaveConversion&: HaveR2L, ToType&: R2LType)) |
6711 | return QualType(); |
6712 | |
6713 | // If both can be converted, [...] the program is ill-formed. |
6714 | if (HaveL2R && HaveR2L) { |
6715 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
6716 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
6717 | return QualType(); |
6718 | } |
6719 | |
6720 | // If exactly one conversion is possible, that conversion is applied to |
6721 | // the chosen operand and the converted operands are used in place of the |
6722 | // original operands for the remainder of this section. |
6723 | if (HaveL2R) { |
6724 | if (ConvertForConditional(Self&: *this, E&: LHS, T: L2RType) || LHS.isInvalid()) |
6725 | return QualType(); |
6726 | LTy = LHS.get()->getType(); |
6727 | } else if (HaveR2L) { |
6728 | if (ConvertForConditional(Self&: *this, E&: RHS, T: R2LType) || RHS.isInvalid()) |
6729 | return QualType(); |
6730 | RTy = RHS.get()->getType(); |
6731 | } |
6732 | } |
6733 | |
6734 | // C++11 [expr.cond]p3 |
6735 | // if both are glvalues of the same value category and the same type except |
6736 | // for cv-qualification, an attempt is made to convert each of those |
6737 | // operands to the type of the other. |
6738 | // FIXME: |
6739 | // Resolving a defect in P0012R1: we extend this to cover all cases where |
6740 | // one of the operands is reference-compatible with the other, in order |
6741 | // to support conditionals between functions differing in noexcept. This |
6742 | // will similarly cover difference in array bounds after P0388R4. |
6743 | // FIXME: If LTy and RTy have a composite pointer type, should we convert to |
6744 | // that instead? |
6745 | ExprValueKind LVK = LHS.get()->getValueKind(); |
6746 | ExprValueKind RVK = RHS.get()->getValueKind(); |
6747 | if (!Context.hasSameType(T1: LTy, T2: RTy) && LVK == RVK && LVK != VK_PRValue) { |
6748 | // DerivedToBase was already handled by the class-specific case above. |
6749 | // FIXME: Should we allow ObjC conversions here? |
6750 | const ReferenceConversions AllowedConversions = |
6751 | ReferenceConversions::Qualification | |
6752 | ReferenceConversions::NestedQualification | |
6753 | ReferenceConversions::Function; |
6754 | |
6755 | ReferenceConversions RefConv; |
6756 | if (CompareReferenceRelationship(Loc: QuestionLoc, T1: LTy, T2: RTy, Conv: &RefConv) == |
6757 | Ref_Compatible && |
6758 | !(RefConv & ~AllowedConversions) && |
6759 | // [...] subject to the constraint that the reference must bind |
6760 | // directly [...] |
6761 | !RHS.get()->refersToBitField() && !RHS.get()->refersToVectorElement()) { |
6762 | RHS = ImpCastExprToType(E: RHS.get(), Type: LTy, CK: CK_NoOp, VK: RVK); |
6763 | RTy = RHS.get()->getType(); |
6764 | } else if (CompareReferenceRelationship(Loc: QuestionLoc, T1: RTy, T2: LTy, Conv: &RefConv) == |
6765 | Ref_Compatible && |
6766 | !(RefConv & ~AllowedConversions) && |
6767 | !LHS.get()->refersToBitField() && |
6768 | !LHS.get()->refersToVectorElement()) { |
6769 | LHS = ImpCastExprToType(E: LHS.get(), Type: RTy, CK: CK_NoOp, VK: LVK); |
6770 | LTy = LHS.get()->getType(); |
6771 | } |
6772 | } |
6773 | |
6774 | // C++11 [expr.cond]p4 |
6775 | // If the second and third operands are glvalues of the same value |
6776 | // category and have the same type, the result is of that type and |
6777 | // value category and it is a bit-field if the second or the third |
6778 | // operand is a bit-field, or if both are bit-fields. |
6779 | // We only extend this to bitfields, not to the crazy other kinds of |
6780 | // l-values. |
6781 | bool Same = Context.hasSameType(T1: LTy, T2: RTy); |
6782 | if (Same && LVK == RVK && LVK != VK_PRValue && |
6783 | LHS.get()->isOrdinaryOrBitFieldObject() && |
6784 | RHS.get()->isOrdinaryOrBitFieldObject()) { |
6785 | VK = LHS.get()->getValueKind(); |
6786 | if (LHS.get()->getObjectKind() == OK_BitField || |
6787 | RHS.get()->getObjectKind() == OK_BitField) |
6788 | OK = OK_BitField; |
6789 | return Context.getCommonSugaredType(X: LTy, Y: RTy); |
6790 | } |
6791 | |
6792 | // C++11 [expr.cond]p5 |
6793 | // Otherwise, the result is a prvalue. If the second and third operands |
6794 | // do not have the same type, and either has (cv) class type, ... |
6795 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
6796 | // ... overload resolution is used to determine the conversions (if any) |
6797 | // to be applied to the operands. If the overload resolution fails, the |
6798 | // program is ill-formed. |
6799 | if (FindConditionalOverload(Self&: *this, LHS, RHS, QuestionLoc)) |
6800 | return QualType(); |
6801 | } |
6802 | |
6803 | // C++11 [expr.cond]p6 |
6804 | // Lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard |
6805 | // conversions are performed on the second and third operands. |
6806 | LHS = DefaultFunctionArrayLvalueConversion(E: LHS.get()); |
6807 | RHS = DefaultFunctionArrayLvalueConversion(E: RHS.get()); |
6808 | if (LHS.isInvalid() || RHS.isInvalid()) |
6809 | return QualType(); |
6810 | LTy = LHS.get()->getType(); |
6811 | RTy = RHS.get()->getType(); |
6812 | |
6813 | // After those conversions, one of the following shall hold: |
6814 | // -- The second and third operands have the same type; the result |
6815 | // is of that type. If the operands have class type, the result |
6816 | // is a prvalue temporary of the result type, which is |
6817 | // copy-initialized from either the second operand or the third |
6818 | // operand depending on the value of the first operand. |
6819 | if (Context.hasSameType(T1: LTy, T2: RTy)) { |
6820 | if (LTy->isRecordType()) { |
6821 | // The operands have class type. Make a temporary copy. |
6822 | ExprResult LHSCopy = PerformCopyInitialization( |
6823 | Entity: InitializedEntity::InitializeTemporary(Type: LTy), EqualLoc: SourceLocation(), Init: LHS); |
6824 | if (LHSCopy.isInvalid()) |
6825 | return QualType(); |
6826 | |
6827 | ExprResult RHSCopy = PerformCopyInitialization( |
6828 | Entity: InitializedEntity::InitializeTemporary(Type: RTy), EqualLoc: SourceLocation(), Init: RHS); |
6829 | if (RHSCopy.isInvalid()) |
6830 | return QualType(); |
6831 | |
6832 | LHS = LHSCopy; |
6833 | RHS = RHSCopy; |
6834 | } |
6835 | return Context.getCommonSugaredType(X: LTy, Y: RTy); |
6836 | } |
6837 | |
6838 | // Extension: conditional operator involving vector types. |
6839 | if (LTy->isVectorType() || RTy->isVectorType()) |
6840 | return CheckVectorOperands(LHS, RHS, Loc: QuestionLoc, /*isCompAssign*/ IsCompAssign: false, |
6841 | /*AllowBothBool*/ true, |
6842 | /*AllowBoolConversions*/ AllowBoolConversion: false, |
6843 | /*AllowBoolOperation*/ false, |
6844 | /*ReportInvalid*/ true); |
6845 | |
6846 | // -- The second and third operands have arithmetic or enumeration type; |
6847 | // the usual arithmetic conversions are performed to bring them to a |
6848 | // common type, and the result is of that type. |
6849 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
6850 | QualType ResTy = |
6851 | UsualArithmeticConversions(LHS, RHS, Loc: QuestionLoc, ACK: ACK_Conditional); |
6852 | if (LHS.isInvalid() || RHS.isInvalid()) |
6853 | return QualType(); |
6854 | if (ResTy.isNull()) { |
6855 | Diag(QuestionLoc, |
6856 | diag::err_typecheck_cond_incompatible_operands) << LTy << RTy |
6857 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
6858 | return QualType(); |
6859 | } |
6860 | |
6861 | LHS = ImpCastExprToType(E: LHS.get(), Type: ResTy, CK: PrepareScalarCast(src&: LHS, destType: ResTy)); |
6862 | RHS = ImpCastExprToType(E: RHS.get(), Type: ResTy, CK: PrepareScalarCast(src&: RHS, destType: ResTy)); |
6863 | |
6864 | return ResTy; |
6865 | } |
6866 | |
6867 | // -- The second and third operands have pointer type, or one has pointer |
6868 | // type and the other is a null pointer constant, or both are null |
6869 | // pointer constants, at least one of which is non-integral; pointer |
6870 | // conversions and qualification conversions are performed to bring them |
6871 | // to their composite pointer type. The result is of the composite |
6872 | // pointer type. |
6873 | // -- The second and third operands have pointer to member type, or one has |
6874 | // pointer to member type and the other is a null pointer constant; |
6875 | // pointer to member conversions and qualification conversions are |
6876 | // performed to bring them to a common type, whose cv-qualification |
6877 | // shall match the cv-qualification of either the second or the third |
6878 | // operand. The result is of the common type. |
6879 | QualType Composite = FindCompositePointerType(Loc: QuestionLoc, E1&: LHS, E2&: RHS); |
6880 | if (!Composite.isNull()) |
6881 | return Composite; |
6882 | |
6883 | // Similarly, attempt to find composite type of two objective-c pointers. |
6884 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
6885 | if (LHS.isInvalid() || RHS.isInvalid()) |
6886 | return QualType(); |
6887 | if (!Composite.isNull()) |
6888 | return Composite; |
6889 | |
6890 | // Check if we are using a null with a non-pointer type. |
6891 | if (DiagnoseConditionalForNull(LHSExpr: LHS.get(), RHSExpr: RHS.get(), QuestionLoc)) |
6892 | return QualType(); |
6893 | |
6894 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
6895 | << LHS.get()->getType() << RHS.get()->getType() |
6896 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
6897 | return QualType(); |
6898 | } |
6899 | |
6900 | /// Find a merged pointer type and convert the two expressions to it. |
6901 | /// |
6902 | /// This finds the composite pointer type for \p E1 and \p E2 according to |
6903 | /// C++2a [expr.type]p3. It converts both expressions to this type and returns |
6904 | /// it. It does not emit diagnostics (FIXME: that's not true if \p ConvertArgs |
6905 | /// is \c true). |
6906 | /// |
6907 | /// \param Loc The location of the operator requiring these two expressions to |
6908 | /// be converted to the composite pointer type. |
6909 | /// |
6910 | /// \param ConvertArgs If \c false, do not convert E1 and E2 to the target type. |
6911 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
6912 | Expr *&E1, Expr *&E2, |
6913 | bool ConvertArgs) { |
6914 | assert(getLangOpts().CPlusPlus && "This function assumes C++" ); |
6915 | |
6916 | // C++1z [expr]p14: |
6917 | // The composite pointer type of two operands p1 and p2 having types T1 |
6918 | // and T2 |
6919 | QualType T1 = E1->getType(), T2 = E2->getType(); |
6920 | |
6921 | // where at least one is a pointer or pointer to member type or |
6922 | // std::nullptr_t is: |
6923 | bool T1IsPointerLike = T1->isAnyPointerType() || T1->isMemberPointerType() || |
6924 | T1->isNullPtrType(); |
6925 | bool T2IsPointerLike = T2->isAnyPointerType() || T2->isMemberPointerType() || |
6926 | T2->isNullPtrType(); |
6927 | if (!T1IsPointerLike && !T2IsPointerLike) |
6928 | return QualType(); |
6929 | |
6930 | // - if both p1 and p2 are null pointer constants, std::nullptr_t; |
6931 | // This can't actually happen, following the standard, but we also use this |
6932 | // to implement the end of [expr.conv], which hits this case. |
6933 | // |
6934 | // - if either p1 or p2 is a null pointer constant, T2 or T1, respectively; |
6935 | if (T1IsPointerLike && |
6936 | E2->isNullPointerConstant(Ctx&: Context, NPC: Expr::NPC_ValueDependentIsNull)) { |
6937 | if (ConvertArgs) |
6938 | E2 = ImpCastExprToType(E: E2, Type: T1, CK: T1->isMemberPointerType() |
6939 | ? CK_NullToMemberPointer |
6940 | : CK_NullToPointer).get(); |
6941 | return T1; |
6942 | } |
6943 | if (T2IsPointerLike && |
6944 | E1->isNullPointerConstant(Ctx&: Context, NPC: Expr::NPC_ValueDependentIsNull)) { |
6945 | if (ConvertArgs) |
6946 | E1 = ImpCastExprToType(E: E1, Type: T2, CK: T2->isMemberPointerType() |
6947 | ? CK_NullToMemberPointer |
6948 | : CK_NullToPointer).get(); |
6949 | return T2; |
6950 | } |
6951 | |
6952 | // Now both have to be pointers or member pointers. |
6953 | if (!T1IsPointerLike || !T2IsPointerLike) |
6954 | return QualType(); |
6955 | assert(!T1->isNullPtrType() && !T2->isNullPtrType() && |
6956 | "nullptr_t should be a null pointer constant" ); |
6957 | |
6958 | struct Step { |
6959 | enum Kind { Pointer, ObjCPointer, MemberPointer, Array } K; |
6960 | // Qualifiers to apply under the step kind. |
6961 | Qualifiers Quals; |
6962 | /// The class for a pointer-to-member; a constant array type with a bound |
6963 | /// (if any) for an array. |
6964 | const Type *ClassOrBound; |
6965 | |
6966 | Step(Kind K, const Type *ClassOrBound = nullptr) |
6967 | : K(K), ClassOrBound(ClassOrBound) {} |
6968 | QualType rebuild(ASTContext &Ctx, QualType T) const { |
6969 | T = Ctx.getQualifiedType(T, Qs: Quals); |
6970 | switch (K) { |
6971 | case Pointer: |
6972 | return Ctx.getPointerType(T); |
6973 | case MemberPointer: |
6974 | return Ctx.getMemberPointerType(T, Cls: ClassOrBound); |
6975 | case ObjCPointer: |
6976 | return Ctx.getObjCObjectPointerType(OIT: T); |
6977 | case Array: |
6978 | if (auto *CAT = cast_or_null<ConstantArrayType>(Val: ClassOrBound)) |
6979 | return Ctx.getConstantArrayType(EltTy: T, ArySize: CAT->getSize(), SizeExpr: nullptr, |
6980 | ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
6981 | else |
6982 | return Ctx.getIncompleteArrayType(EltTy: T, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
6983 | } |
6984 | llvm_unreachable("unknown step kind" ); |
6985 | } |
6986 | }; |
6987 | |
6988 | SmallVector<Step, 8> Steps; |
6989 | |
6990 | // - if T1 is "pointer to cv1 C1" and T2 is "pointer to cv2 C2", where C1 |
6991 | // is reference-related to C2 or C2 is reference-related to C1 (8.6.3), |
6992 | // the cv-combined type of T1 and T2 or the cv-combined type of T2 and T1, |
6993 | // respectively; |
6994 | // - if T1 is "pointer to member of C1 of type cv1 U1" and T2 is "pointer |
6995 | // to member of C2 of type cv2 U2" for some non-function type U, where |
6996 | // C1 is reference-related to C2 or C2 is reference-related to C1, the |
6997 | // cv-combined type of T2 and T1 or the cv-combined type of T1 and T2, |
6998 | // respectively; |
6999 | // - if T1 and T2 are similar types (4.5), the cv-combined type of T1 and |
7000 | // T2; |
7001 | // |
7002 | // Dismantle T1 and T2 to simultaneously determine whether they are similar |
7003 | // and to prepare to form the cv-combined type if so. |
7004 | QualType Composite1 = T1; |
7005 | QualType Composite2 = T2; |
7006 | unsigned NeedConstBefore = 0; |
7007 | while (true) { |
7008 | assert(!Composite1.isNull() && !Composite2.isNull()); |
7009 | |
7010 | Qualifiers Q1, Q2; |
7011 | Composite1 = Context.getUnqualifiedArrayType(T: Composite1, Quals&: Q1); |
7012 | Composite2 = Context.getUnqualifiedArrayType(T: Composite2, Quals&: Q2); |
7013 | |
7014 | // Top-level qualifiers are ignored. Merge at all lower levels. |
7015 | if (!Steps.empty()) { |
7016 | // Find the qualifier union: (approximately) the unique minimal set of |
7017 | // qualifiers that is compatible with both types. |
7018 | Qualifiers Quals = Qualifiers::fromCVRUMask(CVRU: Q1.getCVRUQualifiers() | |
7019 | Q2.getCVRUQualifiers()); |
7020 | |
7021 | // Under one level of pointer or pointer-to-member, we can change to an |
7022 | // unambiguous compatible address space. |
7023 | if (Q1.getAddressSpace() == Q2.getAddressSpace()) { |
7024 | Quals.setAddressSpace(Q1.getAddressSpace()); |
7025 | } else if (Steps.size() == 1) { |
7026 | bool MaybeQ1 = Q1.isAddressSpaceSupersetOf(other: Q2); |
7027 | bool MaybeQ2 = Q2.isAddressSpaceSupersetOf(other: Q1); |
7028 | if (MaybeQ1 == MaybeQ2) { |
7029 | // Exception for ptr size address spaces. Should be able to choose |
7030 | // either address space during comparison. |
7031 | if (isPtrSizeAddressSpace(AS: Q1.getAddressSpace()) || |
7032 | isPtrSizeAddressSpace(AS: Q2.getAddressSpace())) |
7033 | MaybeQ1 = true; |
7034 | else |
7035 | return QualType(); // No unique best address space. |
7036 | } |
7037 | Quals.setAddressSpace(MaybeQ1 ? Q1.getAddressSpace() |
7038 | : Q2.getAddressSpace()); |
7039 | } else { |
7040 | return QualType(); |
7041 | } |
7042 | |
7043 | // FIXME: In C, we merge __strong and none to __strong at the top level. |
7044 | if (Q1.getObjCGCAttr() == Q2.getObjCGCAttr()) |
7045 | Quals.setObjCGCAttr(Q1.getObjCGCAttr()); |
7046 | else if (T1->isVoidPointerType() || T2->isVoidPointerType()) |
7047 | assert(Steps.size() == 1); |
7048 | else |
7049 | return QualType(); |
7050 | |
7051 | // Mismatched lifetime qualifiers never compatibly include each other. |
7052 | if (Q1.getObjCLifetime() == Q2.getObjCLifetime()) |
7053 | Quals.setObjCLifetime(Q1.getObjCLifetime()); |
7054 | else if (T1->isVoidPointerType() || T2->isVoidPointerType()) |
7055 | assert(Steps.size() == 1); |
7056 | else |
7057 | return QualType(); |
7058 | |
7059 | Steps.back().Quals = Quals; |
7060 | if (Q1 != Quals || Q2 != Quals) |
7061 | NeedConstBefore = Steps.size() - 1; |
7062 | } |
7063 | |
7064 | // FIXME: Can we unify the following with UnwrapSimilarTypes? |
7065 | |
7066 | const ArrayType *Arr1, *Arr2; |
7067 | if ((Arr1 = Context.getAsArrayType(T: Composite1)) && |
7068 | (Arr2 = Context.getAsArrayType(T: Composite2))) { |
7069 | auto *CAT1 = dyn_cast<ConstantArrayType>(Val: Arr1); |
7070 | auto *CAT2 = dyn_cast<ConstantArrayType>(Val: Arr2); |
7071 | if (CAT1 && CAT2 && CAT1->getSize() == CAT2->getSize()) { |
7072 | Composite1 = Arr1->getElementType(); |
7073 | Composite2 = Arr2->getElementType(); |
7074 | Steps.emplace_back(Args: Step::Array, Args&: CAT1); |
7075 | continue; |
7076 | } |
7077 | bool IAT1 = isa<IncompleteArrayType>(Val: Arr1); |
7078 | bool IAT2 = isa<IncompleteArrayType>(Val: Arr2); |
7079 | if ((IAT1 && IAT2) || |
7080 | (getLangOpts().CPlusPlus20 && (IAT1 != IAT2) && |
7081 | ((bool)CAT1 != (bool)CAT2) && |
7082 | (Steps.empty() || Steps.back().K != Step::Array))) { |
7083 | // In C++20 onwards, we can unify an array of N T with an array of |
7084 | // a different or unknown bound. But we can't form an array whose |
7085 | // element type is an array of unknown bound by doing so. |
7086 | Composite1 = Arr1->getElementType(); |
7087 | Composite2 = Arr2->getElementType(); |
7088 | Steps.emplace_back(Args: Step::Array); |
7089 | if (CAT1 || CAT2) |
7090 | NeedConstBefore = Steps.size(); |
7091 | continue; |
7092 | } |
7093 | } |
7094 | |
7095 | const PointerType *Ptr1, *Ptr2; |
7096 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
7097 | (Ptr2 = Composite2->getAs<PointerType>())) { |
7098 | Composite1 = Ptr1->getPointeeType(); |
7099 | Composite2 = Ptr2->getPointeeType(); |
7100 | Steps.emplace_back(Args: Step::Pointer); |
7101 | continue; |
7102 | } |
7103 | |
7104 | const ObjCObjectPointerType *ObjPtr1, *ObjPtr2; |
7105 | if ((ObjPtr1 = Composite1->getAs<ObjCObjectPointerType>()) && |
7106 | (ObjPtr2 = Composite2->getAs<ObjCObjectPointerType>())) { |
7107 | Composite1 = ObjPtr1->getPointeeType(); |
7108 | Composite2 = ObjPtr2->getPointeeType(); |
7109 | Steps.emplace_back(Args: Step::ObjCPointer); |
7110 | continue; |
7111 | } |
7112 | |
7113 | const MemberPointerType *MemPtr1, *MemPtr2; |
7114 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
7115 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
7116 | Composite1 = MemPtr1->getPointeeType(); |
7117 | Composite2 = MemPtr2->getPointeeType(); |
7118 | |
7119 | // At the top level, we can perform a base-to-derived pointer-to-member |
7120 | // conversion: |
7121 | // |
7122 | // - [...] where C1 is reference-related to C2 or C2 is |
7123 | // reference-related to C1 |
7124 | // |
7125 | // (Note that the only kinds of reference-relatedness in scope here are |
7126 | // "same type or derived from".) At any other level, the class must |
7127 | // exactly match. |
7128 | const Type *Class = nullptr; |
7129 | QualType Cls1(MemPtr1->getClass(), 0); |
7130 | QualType Cls2(MemPtr2->getClass(), 0); |
7131 | if (Context.hasSameType(T1: Cls1, T2: Cls2)) |
7132 | Class = MemPtr1->getClass(); |
7133 | else if (Steps.empty()) |
7134 | Class = IsDerivedFrom(Loc, Derived: Cls1, Base: Cls2) ? MemPtr1->getClass() : |
7135 | IsDerivedFrom(Loc, Derived: Cls2, Base: Cls1) ? MemPtr2->getClass() : nullptr; |
7136 | if (!Class) |
7137 | return QualType(); |
7138 | |
7139 | Steps.emplace_back(Args: Step::MemberPointer, Args&: Class); |
7140 | continue; |
7141 | } |
7142 | |
7143 | // Special case: at the top level, we can decompose an Objective-C pointer |
7144 | // and a 'cv void *'. Unify the qualifiers. |
7145 | if (Steps.empty() && ((Composite1->isVoidPointerType() && |
7146 | Composite2->isObjCObjectPointerType()) || |
7147 | (Composite1->isObjCObjectPointerType() && |
7148 | Composite2->isVoidPointerType()))) { |
7149 | Composite1 = Composite1->getPointeeType(); |
7150 | Composite2 = Composite2->getPointeeType(); |
7151 | Steps.emplace_back(Args: Step::Pointer); |
7152 | continue; |
7153 | } |
7154 | |
7155 | // FIXME: block pointer types? |
7156 | |
7157 | // Cannot unwrap any more types. |
7158 | break; |
7159 | } |
7160 | |
7161 | // - if T1 or T2 is "pointer to noexcept function" and the other type is |
7162 | // "pointer to function", where the function types are otherwise the same, |
7163 | // "pointer to function"; |
7164 | // - if T1 or T2 is "pointer to member of C1 of type function", the other |
7165 | // type is "pointer to member of C2 of type noexcept function", and C1 |
7166 | // is reference-related to C2 or C2 is reference-related to C1, where |
7167 | // the function types are otherwise the same, "pointer to member of C2 of |
7168 | // type function" or "pointer to member of C1 of type function", |
7169 | // respectively; |
7170 | // |
7171 | // We also support 'noreturn' here, so as a Clang extension we generalize the |
7172 | // above to: |
7173 | // |
7174 | // - [Clang] If T1 and T2 are both of type "pointer to function" or |
7175 | // "pointer to member function" and the pointee types can be unified |
7176 | // by a function pointer conversion, that conversion is applied |
7177 | // before checking the following rules. |
7178 | // |
7179 | // We've already unwrapped down to the function types, and we want to merge |
7180 | // rather than just convert, so do this ourselves rather than calling |
7181 | // IsFunctionConversion. |
7182 | // |
7183 | // FIXME: In order to match the standard wording as closely as possible, we |
7184 | // currently only do this under a single level of pointers. Ideally, we would |
7185 | // allow this in general, and set NeedConstBefore to the relevant depth on |
7186 | // the side(s) where we changed anything. If we permit that, we should also |
7187 | // consider this conversion when determining type similarity and model it as |
7188 | // a qualification conversion. |
7189 | if (Steps.size() == 1) { |
7190 | if (auto *FPT1 = Composite1->getAs<FunctionProtoType>()) { |
7191 | if (auto *FPT2 = Composite2->getAs<FunctionProtoType>()) { |
7192 | FunctionProtoType::ExtProtoInfo EPI1 = FPT1->getExtProtoInfo(); |
7193 | FunctionProtoType::ExtProtoInfo EPI2 = FPT2->getExtProtoInfo(); |
7194 | |
7195 | // The result is noreturn if both operands are. |
7196 | bool Noreturn = |
7197 | EPI1.ExtInfo.getNoReturn() && EPI2.ExtInfo.getNoReturn(); |
7198 | EPI1.ExtInfo = EPI1.ExtInfo.withNoReturn(noReturn: Noreturn); |
7199 | EPI2.ExtInfo = EPI2.ExtInfo.withNoReturn(noReturn: Noreturn); |
7200 | |
7201 | // The result is nothrow if both operands are. |
7202 | SmallVector<QualType, 8> ExceptionTypeStorage; |
7203 | EPI1.ExceptionSpec = EPI2.ExceptionSpec = Context.mergeExceptionSpecs( |
7204 | ESI1: EPI1.ExceptionSpec, ESI2: EPI2.ExceptionSpec, ExceptionTypeStorage, |
7205 | AcceptDependent: getLangOpts().CPlusPlus17); |
7206 | |
7207 | Composite1 = Context.getFunctionType(ResultTy: FPT1->getReturnType(), |
7208 | Args: FPT1->getParamTypes(), EPI: EPI1); |
7209 | Composite2 = Context.getFunctionType(ResultTy: FPT2->getReturnType(), |
7210 | Args: FPT2->getParamTypes(), EPI: EPI2); |
7211 | } |
7212 | } |
7213 | } |
7214 | |
7215 | // There are some more conversions we can perform under exactly one pointer. |
7216 | if (Steps.size() == 1 && Steps.front().K == Step::Pointer && |
7217 | !Context.hasSameType(T1: Composite1, T2: Composite2)) { |
7218 | // - if T1 or T2 is "pointer to cv1 void" and the other type is |
7219 | // "pointer to cv2 T", where T is an object type or void, |
7220 | // "pointer to cv12 void", where cv12 is the union of cv1 and cv2; |
7221 | if (Composite1->isVoidType() && Composite2->isObjectType()) |
7222 | Composite2 = Composite1; |
7223 | else if (Composite2->isVoidType() && Composite1->isObjectType()) |
7224 | Composite1 = Composite2; |
7225 | // - if T1 is "pointer to cv1 C1" and T2 is "pointer to cv2 C2", where C1 |
7226 | // is reference-related to C2 or C2 is reference-related to C1 (8.6.3), |
7227 | // the cv-combined type of T1 and T2 or the cv-combined type of T2 and |
7228 | // T1, respectively; |
7229 | // |
7230 | // The "similar type" handling covers all of this except for the "T1 is a |
7231 | // base class of T2" case in the definition of reference-related. |
7232 | else if (IsDerivedFrom(Loc, Derived: Composite1, Base: Composite2)) |
7233 | Composite1 = Composite2; |
7234 | else if (IsDerivedFrom(Loc, Derived: Composite2, Base: Composite1)) |
7235 | Composite2 = Composite1; |
7236 | } |
7237 | |
7238 | // At this point, either the inner types are the same or we have failed to |
7239 | // find a composite pointer type. |
7240 | if (!Context.hasSameType(T1: Composite1, T2: Composite2)) |
7241 | return QualType(); |
7242 | |
7243 | // Per C++ [conv.qual]p3, add 'const' to every level before the last |
7244 | // differing qualifier. |
7245 | for (unsigned I = 0; I != NeedConstBefore; ++I) |
7246 | Steps[I].Quals.addConst(); |
7247 | |
7248 | // Rebuild the composite type. |
7249 | QualType Composite = Context.getCommonSugaredType(X: Composite1, Y: Composite2); |
7250 | for (auto &S : llvm::reverse(C&: Steps)) |
7251 | Composite = S.rebuild(Ctx&: Context, T: Composite); |
7252 | |
7253 | if (ConvertArgs) { |
7254 | // Convert the expressions to the composite pointer type. |
7255 | InitializedEntity Entity = |
7256 | InitializedEntity::InitializeTemporary(Type: Composite); |
7257 | InitializationKind Kind = |
7258 | InitializationKind::CreateCopy(InitLoc: Loc, EqualLoc: SourceLocation()); |
7259 | |
7260 | InitializationSequence E1ToC(*this, Entity, Kind, E1); |
7261 | if (!E1ToC) |
7262 | return QualType(); |
7263 | |
7264 | InitializationSequence E2ToC(*this, Entity, Kind, E2); |
7265 | if (!E2ToC) |
7266 | return QualType(); |
7267 | |
7268 | // FIXME: Let the caller know if these fail to avoid duplicate diagnostics. |
7269 | ExprResult E1Result = E1ToC.Perform(S&: *this, Entity, Kind, Args: E1); |
7270 | if (E1Result.isInvalid()) |
7271 | return QualType(); |
7272 | E1 = E1Result.get(); |
7273 | |
7274 | ExprResult E2Result = E2ToC.Perform(S&: *this, Entity, Kind, Args: E2); |
7275 | if (E2Result.isInvalid()) |
7276 | return QualType(); |
7277 | E2 = E2Result.get(); |
7278 | } |
7279 | |
7280 | return Composite; |
7281 | } |
7282 | |
7283 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
7284 | if (!E) |
7285 | return ExprError(); |
7286 | |
7287 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?" ); |
7288 | |
7289 | // If the result is a glvalue, we shouldn't bind it. |
7290 | if (E->isGLValue()) |
7291 | return E; |
7292 | |
7293 | // In ARC, calls that return a retainable type can return retained, |
7294 | // in which case we have to insert a consuming cast. |
7295 | if (getLangOpts().ObjCAutoRefCount && |
7296 | E->getType()->isObjCRetainableType()) { |
7297 | |
7298 | bool ReturnsRetained; |
7299 | |
7300 | // For actual calls, we compute this by examining the type of the |
7301 | // called value. |
7302 | if (CallExpr *Call = dyn_cast<CallExpr>(Val: E)) { |
7303 | Expr *Callee = Call->getCallee()->IgnoreParens(); |
7304 | QualType T = Callee->getType(); |
7305 | |
7306 | if (T == Context.BoundMemberTy) { |
7307 | // Handle pointer-to-members. |
7308 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Val: Callee)) |
7309 | T = BinOp->getRHS()->getType(); |
7310 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Val: Callee)) |
7311 | T = Mem->getMemberDecl()->getType(); |
7312 | } |
7313 | |
7314 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
7315 | T = Ptr->getPointeeType(); |
7316 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) |
7317 | T = Ptr->getPointeeType(); |
7318 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) |
7319 | T = MemPtr->getPointeeType(); |
7320 | |
7321 | auto *FTy = T->castAs<FunctionType>(); |
7322 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); |
7323 | |
7324 | // ActOnStmtExpr arranges things so that StmtExprs of retainable |
7325 | // type always produce a +1 object. |
7326 | } else if (isa<StmtExpr>(Val: E)) { |
7327 | ReturnsRetained = true; |
7328 | |
7329 | // We hit this case with the lambda conversion-to-block optimization; |
7330 | // we don't want any extra casts here. |
7331 | } else if (isa<CastExpr>(Val: E) && |
7332 | isa<BlockExpr>(Val: cast<CastExpr>(Val: E)->getSubExpr())) { |
7333 | return E; |
7334 | |
7335 | // For message sends and property references, we try to find an |
7336 | // actual method. FIXME: we should infer retention by selector in |
7337 | // cases where we don't have an actual method. |
7338 | } else { |
7339 | ObjCMethodDecl *D = nullptr; |
7340 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(Val: E)) { |
7341 | D = Send->getMethodDecl(); |
7342 | } else if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(Val: E)) { |
7343 | D = BoxedExpr->getBoxingMethod(); |
7344 | } else if (ObjCArrayLiteral *ArrayLit = dyn_cast<ObjCArrayLiteral>(Val: E)) { |
7345 | // Don't do reclaims if we're using the zero-element array |
7346 | // constant. |
7347 | if (ArrayLit->getNumElements() == 0 && |
7348 | Context.getLangOpts().ObjCRuntime.hasEmptyCollections()) |
7349 | return E; |
7350 | |
7351 | D = ArrayLit->getArrayWithObjectsMethod(); |
7352 | } else if (ObjCDictionaryLiteral *DictLit |
7353 | = dyn_cast<ObjCDictionaryLiteral>(Val: E)) { |
7354 | // Don't do reclaims if we're using the zero-element dictionary |
7355 | // constant. |
7356 | if (DictLit->getNumElements() == 0 && |
7357 | Context.getLangOpts().ObjCRuntime.hasEmptyCollections()) |
7358 | return E; |
7359 | |
7360 | D = DictLit->getDictWithObjectsMethod(); |
7361 | } |
7362 | |
7363 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); |
7364 | |
7365 | // Don't do reclaims on performSelector calls; despite their |
7366 | // return type, the invoked method doesn't necessarily actually |
7367 | // return an object. |
7368 | if (!ReturnsRetained && |
7369 | D && D->getMethodFamily() == OMF_performSelector) |
7370 | return E; |
7371 | } |
7372 | |
7373 | // Don't reclaim an object of Class type. |
7374 | if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType()) |
7375 | return E; |
7376 | |
7377 | Cleanup.setExprNeedsCleanups(true); |
7378 | |
7379 | CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject |
7380 | : CK_ARCReclaimReturnedObject); |
7381 | return ImplicitCastExpr::Create(Context, T: E->getType(), Kind: ck, Operand: E, BasePath: nullptr, |
7382 | Cat: VK_PRValue, FPO: FPOptionsOverride()); |
7383 | } |
7384 | |
7385 | if (E->getType().isDestructedType() == QualType::DK_nontrivial_c_struct) |
7386 | Cleanup.setExprNeedsCleanups(true); |
7387 | |
7388 | if (!getLangOpts().CPlusPlus) |
7389 | return E; |
7390 | |
7391 | // Search for the base element type (cf. ASTContext::getBaseElementType) with |
7392 | // a fast path for the common case that the type is directly a RecordType. |
7393 | const Type *T = Context.getCanonicalType(T: E->getType().getTypePtr()); |
7394 | const RecordType *RT = nullptr; |
7395 | while (!RT) { |
7396 | switch (T->getTypeClass()) { |
7397 | case Type::Record: |
7398 | RT = cast<RecordType>(Val: T); |
7399 | break; |
7400 | case Type::ConstantArray: |
7401 | case Type::IncompleteArray: |
7402 | case Type::VariableArray: |
7403 | case Type::DependentSizedArray: |
7404 | T = cast<ArrayType>(Val: T)->getElementType().getTypePtr(); |
7405 | break; |
7406 | default: |
7407 | return E; |
7408 | } |
7409 | } |
7410 | |
7411 | // That should be enough to guarantee that this type is complete, if we're |
7412 | // not processing a decltype expression. |
7413 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Val: RT->getDecl()); |
7414 | if (RD->isInvalidDecl() || RD->isDependentContext()) |
7415 | return E; |
7416 | |
7417 | bool IsDecltype = ExprEvalContexts.back().ExprContext == |
7418 | ExpressionEvaluationContextRecord::EK_Decltype; |
7419 | CXXDestructorDecl *Destructor = IsDecltype ? nullptr : LookupDestructor(Class: RD); |
7420 | |
7421 | if (Destructor) { |
7422 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
7423 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
7424 | PDiag(diag::err_access_dtor_temp) |
7425 | << E->getType()); |
7426 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
7427 | return ExprError(); |
7428 | |
7429 | // If destructor is trivial, we can avoid the extra copy. |
7430 | if (Destructor->isTrivial()) |
7431 | return E; |
7432 | |
7433 | // We need a cleanup, but we don't need to remember the temporary. |
7434 | Cleanup.setExprNeedsCleanups(true); |
7435 | } |
7436 | |
7437 | CXXTemporary *Temp = CXXTemporary::Create(C: Context, Destructor); |
7438 | CXXBindTemporaryExpr *Bind = CXXBindTemporaryExpr::Create(C: Context, Temp, SubExpr: E); |
7439 | |
7440 | if (IsDecltype) |
7441 | ExprEvalContexts.back().DelayedDecltypeBinds.push_back(Elt: Bind); |
7442 | |
7443 | return Bind; |
7444 | } |
7445 | |
7446 | ExprResult |
7447 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
7448 | if (SubExpr.isInvalid()) |
7449 | return ExprError(); |
7450 | |
7451 | return MaybeCreateExprWithCleanups(SubExpr: SubExpr.get()); |
7452 | } |
7453 | |
7454 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
7455 | assert(SubExpr && "subexpression can't be null!" ); |
7456 | |
7457 | CleanupVarDeclMarking(); |
7458 | |
7459 | unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects; |
7460 | assert(ExprCleanupObjects.size() >= FirstCleanup); |
7461 | assert(Cleanup.exprNeedsCleanups() || |
7462 | ExprCleanupObjects.size() == FirstCleanup); |
7463 | if (!Cleanup.exprNeedsCleanups()) |
7464 | return SubExpr; |
7465 | |
7466 | auto Cleanups = llvm::ArrayRef(ExprCleanupObjects.begin() + FirstCleanup, |
7467 | ExprCleanupObjects.size() - FirstCleanup); |
7468 | |
7469 | auto *E = ExprWithCleanups::Create( |
7470 | C: Context, subexpr: SubExpr, CleanupsHaveSideEffects: Cleanup.cleanupsHaveSideEffects(), objects: Cleanups); |
7471 | DiscardCleanupsInEvaluationContext(); |
7472 | |
7473 | return E; |
7474 | } |
7475 | |
7476 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
7477 | assert(SubStmt && "sub-statement can't be null!" ); |
7478 | |
7479 | CleanupVarDeclMarking(); |
7480 | |
7481 | if (!Cleanup.exprNeedsCleanups()) |
7482 | return SubStmt; |
7483 | |
7484 | // FIXME: In order to attach the temporaries, wrap the statement into |
7485 | // a StmtExpr; currently this is only used for asm statements. |
7486 | // This is hacky, either create a new CXXStmtWithTemporaries statement or |
7487 | // a new AsmStmtWithTemporaries. |
7488 | CompoundStmt *CompStmt = |
7489 | CompoundStmt::Create(C: Context, Stmts: SubStmt, FPFeatures: FPOptionsOverride(), |
7490 | LB: SourceLocation(), RB: SourceLocation()); |
7491 | Expr *E = new (Context) |
7492 | StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), SourceLocation(), |
7493 | /*FIXME TemplateDepth=*/0); |
7494 | return MaybeCreateExprWithCleanups(SubExpr: E); |
7495 | } |
7496 | |
7497 | /// Process the expression contained within a decltype. For such expressions, |
7498 | /// certain semantic checks on temporaries are delayed until this point, and |
7499 | /// are omitted for the 'topmost' call in the decltype expression. If the |
7500 | /// topmost call bound a temporary, strip that temporary off the expression. |
7501 | ExprResult Sema::ActOnDecltypeExpression(Expr *E) { |
7502 | assert(ExprEvalContexts.back().ExprContext == |
7503 | ExpressionEvaluationContextRecord::EK_Decltype && |
7504 | "not in a decltype expression" ); |
7505 | |
7506 | ExprResult Result = CheckPlaceholderExpr(E); |
7507 | if (Result.isInvalid()) |
7508 | return ExprError(); |
7509 | E = Result.get(); |
7510 | |
7511 | // C++11 [expr.call]p11: |
7512 | // If a function call is a prvalue of object type, |
7513 | // -- if the function call is either |
7514 | // -- the operand of a decltype-specifier, or |
7515 | // -- the right operand of a comma operator that is the operand of a |
7516 | // decltype-specifier, |
7517 | // a temporary object is not introduced for the prvalue. |
7518 | |
7519 | // Recursively rebuild ParenExprs and comma expressions to strip out the |
7520 | // outermost CXXBindTemporaryExpr, if any. |
7521 | if (ParenExpr *PE = dyn_cast<ParenExpr>(Val: E)) { |
7522 | ExprResult SubExpr = ActOnDecltypeExpression(E: PE->getSubExpr()); |
7523 | if (SubExpr.isInvalid()) |
7524 | return ExprError(); |
7525 | if (SubExpr.get() == PE->getSubExpr()) |
7526 | return E; |
7527 | return ActOnParenExpr(L: PE->getLParen(), R: PE->getRParen(), E: SubExpr.get()); |
7528 | } |
7529 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Val: E)) { |
7530 | if (BO->getOpcode() == BO_Comma) { |
7531 | ExprResult RHS = ActOnDecltypeExpression(E: BO->getRHS()); |
7532 | if (RHS.isInvalid()) |
7533 | return ExprError(); |
7534 | if (RHS.get() == BO->getRHS()) |
7535 | return E; |
7536 | return BinaryOperator::Create(C: Context, lhs: BO->getLHS(), rhs: RHS.get(), opc: BO_Comma, |
7537 | ResTy: BO->getType(), VK: BO->getValueKind(), |
7538 | OK: BO->getObjectKind(), opLoc: BO->getOperatorLoc(), |
7539 | FPFeatures: BO->getFPFeatures()); |
7540 | } |
7541 | } |
7542 | |
7543 | CXXBindTemporaryExpr *TopBind = dyn_cast<CXXBindTemporaryExpr>(Val: E); |
7544 | CallExpr *TopCall = TopBind ? dyn_cast<CallExpr>(Val: TopBind->getSubExpr()) |
7545 | : nullptr; |
7546 | if (TopCall) |
7547 | E = TopCall; |
7548 | else |
7549 | TopBind = nullptr; |
7550 | |
7551 | // Disable the special decltype handling now. |
7552 | ExprEvalContexts.back().ExprContext = |
7553 | ExpressionEvaluationContextRecord::EK_Other; |
7554 | |
7555 | Result = CheckUnevaluatedOperand(E); |
7556 | if (Result.isInvalid()) |
7557 | return ExprError(); |
7558 | E = Result.get(); |
7559 | |
7560 | // In MS mode, don't perform any extra checking of call return types within a |
7561 | // decltype expression. |
7562 | if (getLangOpts().MSVCCompat) |
7563 | return E; |
7564 | |
7565 | // Perform the semantic checks we delayed until this point. |
7566 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeCalls.size(); |
7567 | I != N; ++I) { |
7568 | CallExpr *Call = ExprEvalContexts.back().DelayedDecltypeCalls[I]; |
7569 | if (Call == TopCall) |
7570 | continue; |
7571 | |
7572 | if (CheckCallReturnType(ReturnType: Call->getCallReturnType(Ctx: Context), |
7573 | Loc: Call->getBeginLoc(), CE: Call, FD: Call->getDirectCallee())) |
7574 | return ExprError(); |
7575 | } |
7576 | |
7577 | // Now all relevant types are complete, check the destructors are accessible |
7578 | // and non-deleted, and annotate them on the temporaries. |
7579 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeBinds.size(); |
7580 | I != N; ++I) { |
7581 | CXXBindTemporaryExpr *Bind = |
7582 | ExprEvalContexts.back().DelayedDecltypeBinds[I]; |
7583 | if (Bind == TopBind) |
7584 | continue; |
7585 | |
7586 | CXXTemporary *Temp = Bind->getTemporary(); |
7587 | |
7588 | CXXRecordDecl *RD = |
7589 | Bind->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
7590 | CXXDestructorDecl *Destructor = LookupDestructor(Class: RD); |
7591 | Temp->setDestructor(Destructor); |
7592 | |
7593 | MarkFunctionReferenced(Loc: Bind->getExprLoc(), Func: Destructor); |
7594 | CheckDestructorAccess(Bind->getExprLoc(), Destructor, |
7595 | PDiag(diag::err_access_dtor_temp) |
7596 | << Bind->getType()); |
7597 | if (DiagnoseUseOfDecl(D: Destructor, Locs: Bind->getExprLoc())) |
7598 | return ExprError(); |
7599 | |
7600 | // We need a cleanup, but we don't need to remember the temporary. |
7601 | Cleanup.setExprNeedsCleanups(true); |
7602 | } |
7603 | |
7604 | // Possibly strip off the top CXXBindTemporaryExpr. |
7605 | return E; |
7606 | } |
7607 | |
7608 | /// Note a set of 'operator->' functions that were used for a member access. |
7609 | static void noteOperatorArrows(Sema &S, |
7610 | ArrayRef<FunctionDecl *> OperatorArrows) { |
7611 | unsigned SkipStart = OperatorArrows.size(), SkipCount = 0; |
7612 | // FIXME: Make this configurable? |
7613 | unsigned Limit = 9; |
7614 | if (OperatorArrows.size() > Limit) { |
7615 | // Produce Limit-1 normal notes and one 'skipping' note. |
7616 | SkipStart = (Limit - 1) / 2 + (Limit - 1) % 2; |
7617 | SkipCount = OperatorArrows.size() - (Limit - 1); |
7618 | } |
7619 | |
7620 | for (unsigned I = 0; I < OperatorArrows.size(); /**/) { |
7621 | if (I == SkipStart) { |
7622 | S.Diag(OperatorArrows[I]->getLocation(), |
7623 | diag::note_operator_arrows_suppressed) |
7624 | << SkipCount; |
7625 | I += SkipCount; |
7626 | } else { |
7627 | S.Diag(OperatorArrows[I]->getLocation(), diag::note_operator_arrow_here) |
7628 | << OperatorArrows[I]->getCallResultType(); |
7629 | ++I; |
7630 | } |
7631 | } |
7632 | } |
7633 | |
7634 | ExprResult Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, |
7635 | SourceLocation OpLoc, |
7636 | tok::TokenKind OpKind, |
7637 | ParsedType &ObjectType, |
7638 | bool &MayBePseudoDestructor) { |
7639 | // Since this might be a postfix expression, get rid of ParenListExprs. |
7640 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, ME: Base); |
7641 | if (Result.isInvalid()) return ExprError(); |
7642 | Base = Result.get(); |
7643 | |
7644 | Result = CheckPlaceholderExpr(E: Base); |
7645 | if (Result.isInvalid()) return ExprError(); |
7646 | Base = Result.get(); |
7647 | |
7648 | QualType BaseType = Base->getType(); |
7649 | MayBePseudoDestructor = false; |
7650 | if (BaseType->isDependentType()) { |
7651 | // If we have a pointer to a dependent type and are using the -> operator, |
7652 | // the object type is the type that the pointer points to. We might still |
7653 | // have enough information about that type to do something useful. |
7654 | if (OpKind == tok::arrow) |
7655 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
7656 | BaseType = Ptr->getPointeeType(); |
7657 | |
7658 | ObjectType = ParsedType::make(P: BaseType); |
7659 | MayBePseudoDestructor = true; |
7660 | return Base; |
7661 | } |
7662 | |
7663 | // C++ [over.match.oper]p8: |
7664 | // [...] When operator->returns, the operator-> is applied to the value |
7665 | // returned, with the original second operand. |
7666 | if (OpKind == tok::arrow) { |
7667 | QualType StartingType = BaseType; |
7668 | bool NoArrowOperatorFound = false; |
7669 | bool FirstIteration = true; |
7670 | FunctionDecl *CurFD = dyn_cast<FunctionDecl>(Val: CurContext); |
7671 | // The set of types we've considered so far. |
7672 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
7673 | SmallVector<FunctionDecl*, 8> OperatorArrows; |
7674 | CTypes.insert(Ptr: Context.getCanonicalType(T: BaseType)); |
7675 | |
7676 | while (BaseType->isRecordType()) { |
7677 | if (OperatorArrows.size() >= getLangOpts().ArrowDepth) { |
7678 | Diag(OpLoc, diag::err_operator_arrow_depth_exceeded) |
7679 | << StartingType << getLangOpts().ArrowDepth << Base->getSourceRange(); |
7680 | noteOperatorArrows(S&: *this, OperatorArrows); |
7681 | Diag(OpLoc, diag::note_operator_arrow_depth) |
7682 | << getLangOpts().ArrowDepth; |
7683 | return ExprError(); |
7684 | } |
7685 | |
7686 | Result = BuildOverloadedArrowExpr( |
7687 | S, Base, OpLoc, |
7688 | // When in a template specialization and on the first loop iteration, |
7689 | // potentially give the default diagnostic (with the fixit in a |
7690 | // separate note) instead of having the error reported back to here |
7691 | // and giving a diagnostic with a fixit attached to the error itself. |
7692 | NoArrowOperatorFound: (FirstIteration && CurFD && CurFD->isFunctionTemplateSpecialization()) |
7693 | ? nullptr |
7694 | : &NoArrowOperatorFound); |
7695 | if (Result.isInvalid()) { |
7696 | if (NoArrowOperatorFound) { |
7697 | if (FirstIteration) { |
7698 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
7699 | << BaseType << 1 << Base->getSourceRange() |
7700 | << FixItHint::CreateReplacement(OpLoc, "." ); |
7701 | OpKind = tok::period; |
7702 | break; |
7703 | } |
7704 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
7705 | << BaseType << Base->getSourceRange(); |
7706 | CallExpr *CE = dyn_cast<CallExpr>(Val: Base); |
7707 | if (Decl *CD = (CE ? CE->getCalleeDecl() : nullptr)) { |
7708 | Diag(CD->getBeginLoc(), |
7709 | diag::note_member_reference_arrow_from_operator_arrow); |
7710 | } |
7711 | } |
7712 | return ExprError(); |
7713 | } |
7714 | Base = Result.get(); |
7715 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Val: Base)) |
7716 | OperatorArrows.push_back(Elt: OpCall->getDirectCallee()); |
7717 | BaseType = Base->getType(); |
7718 | CanQualType CBaseType = Context.getCanonicalType(T: BaseType); |
7719 | if (!CTypes.insert(Ptr: CBaseType).second) { |
7720 | Diag(OpLoc, diag::err_operator_arrow_circular) << StartingType; |
7721 | noteOperatorArrows(S&: *this, OperatorArrows); |
7722 | return ExprError(); |
7723 | } |
7724 | FirstIteration = false; |
7725 | } |
7726 | |
7727 | if (OpKind == tok::arrow) { |
7728 | if (BaseType->isPointerType()) |
7729 | BaseType = BaseType->getPointeeType(); |
7730 | else if (auto *AT = Context.getAsArrayType(T: BaseType)) |
7731 | BaseType = AT->getElementType(); |
7732 | } |
7733 | } |
7734 | |
7735 | // Objective-C properties allow "." access on Objective-C pointer types, |
7736 | // so adjust the base type to the object type itself. |
7737 | if (BaseType->isObjCObjectPointerType()) |
7738 | BaseType = BaseType->getPointeeType(); |
7739 | |
7740 | // C++ [basic.lookup.classref]p2: |
7741 | // [...] If the type of the object expression is of pointer to scalar |
7742 | // type, the unqualified-id is looked up in the context of the complete |
7743 | // postfix-expression. |
7744 | // |
7745 | // This also indicates that we could be parsing a pseudo-destructor-name. |
7746 | // Note that Objective-C class and object types can be pseudo-destructor |
7747 | // expressions or normal member (ivar or property) access expressions, and |
7748 | // it's legal for the type to be incomplete if this is a pseudo-destructor |
7749 | // call. We'll do more incomplete-type checks later in the lookup process, |
7750 | // so just skip this check for ObjC types. |
7751 | if (!BaseType->isRecordType()) { |
7752 | ObjectType = ParsedType::make(P: BaseType); |
7753 | MayBePseudoDestructor = true; |
7754 | return Base; |
7755 | } |
7756 | |
7757 | // The object type must be complete (or dependent), or |
7758 | // C++11 [expr.prim.general]p3: |
7759 | // Unlike the object expression in other contexts, *this is not required to |
7760 | // be of complete type for purposes of class member access (5.2.5) outside |
7761 | // the member function body. |
7762 | if (!BaseType->isDependentType() && |
7763 | !isThisOutsideMemberFunctionBody(BaseType) && |
7764 | RequireCompleteType(OpLoc, BaseType, |
7765 | diag::err_incomplete_member_access)) { |
7766 | return CreateRecoveryExpr(Begin: Base->getBeginLoc(), End: Base->getEndLoc(), SubExprs: {Base}); |
7767 | } |
7768 | |
7769 | // C++ [basic.lookup.classref]p2: |
7770 | // If the id-expression in a class member access (5.2.5) is an |
7771 | // unqualified-id, and the type of the object expression is of a class |
7772 | // type C (or of pointer to a class type C), the unqualified-id is looked |
7773 | // up in the scope of class C. [...] |
7774 | ObjectType = ParsedType::make(P: BaseType); |
7775 | return Base; |
7776 | } |
7777 | |
7778 | static bool CheckArrow(Sema &S, QualType &ObjectType, Expr *&Base, |
7779 | tok::TokenKind &OpKind, SourceLocation OpLoc) { |
7780 | if (Base->hasPlaceholderType()) { |
7781 | ExprResult result = S.CheckPlaceholderExpr(E: Base); |
7782 | if (result.isInvalid()) return true; |
7783 | Base = result.get(); |
7784 | } |
7785 | ObjectType = Base->getType(); |
7786 | |
7787 | // C++ [expr.pseudo]p2: |
7788 | // The left-hand side of the dot operator shall be of scalar type. The |
7789 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
7790 | // This scalar type is the object type. |
7791 | // Note that this is rather different from the normal handling for the |
7792 | // arrow operator. |
7793 | if (OpKind == tok::arrow) { |
7794 | // The operator requires a prvalue, so perform lvalue conversions. |
7795 | // Only do this if we might plausibly end with a pointer, as otherwise |
7796 | // this was likely to be intended to be a '.'. |
7797 | if (ObjectType->isPointerType() || ObjectType->isArrayType() || |
7798 | ObjectType->isFunctionType()) { |
7799 | ExprResult BaseResult = S.DefaultFunctionArrayLvalueConversion(E: Base); |
7800 | if (BaseResult.isInvalid()) |
7801 | return true; |
7802 | Base = BaseResult.get(); |
7803 | ObjectType = Base->getType(); |
7804 | } |
7805 | |
7806 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
7807 | ObjectType = Ptr->getPointeeType(); |
7808 | } else if (!Base->isTypeDependent()) { |
7809 | // The user wrote "p->" when they probably meant "p."; fix it. |
7810 | S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
7811 | << ObjectType << true |
7812 | << FixItHint::CreateReplacement(OpLoc, "." ); |
7813 | if (S.isSFINAEContext()) |
7814 | return true; |
7815 | |
7816 | OpKind = tok::period; |
7817 | } |
7818 | } |
7819 | |
7820 | return false; |
7821 | } |
7822 | |
7823 | /// Check if it's ok to try and recover dot pseudo destructor calls on |
7824 | /// pointer objects. |
7825 | static bool |
7826 | canRecoverDotPseudoDestructorCallsOnPointerObjects(Sema &SemaRef, |
7827 | QualType DestructedType) { |
7828 | // If this is a record type, check if its destructor is callable. |
7829 | if (auto *RD = DestructedType->getAsCXXRecordDecl()) { |
7830 | if (RD->hasDefinition()) |
7831 | if (CXXDestructorDecl *D = SemaRef.LookupDestructor(Class: RD)) |
7832 | return SemaRef.CanUseDecl(D, /*TreatUnavailableAsInvalid=*/false); |
7833 | return false; |
7834 | } |
7835 | |
7836 | // Otherwise, check if it's a type for which it's valid to use a pseudo-dtor. |
7837 | return DestructedType->isDependentType() || DestructedType->isScalarType() || |
7838 | DestructedType->isVectorType(); |
7839 | } |
7840 | |
7841 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
7842 | SourceLocation OpLoc, |
7843 | tok::TokenKind OpKind, |
7844 | const CXXScopeSpec &SS, |
7845 | TypeSourceInfo *ScopeTypeInfo, |
7846 | SourceLocation CCLoc, |
7847 | SourceLocation TildeLoc, |
7848 | PseudoDestructorTypeStorage Destructed) { |
7849 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
7850 | |
7851 | QualType ObjectType; |
7852 | if (CheckArrow(S&: *this, ObjectType, Base, OpKind, OpLoc)) |
7853 | return ExprError(); |
7854 | |
7855 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType() && |
7856 | !ObjectType->isVectorType()) { |
7857 | if (getLangOpts().MSVCCompat && ObjectType->isVoidType()) |
7858 | Diag(OpLoc, diag::ext_pseudo_dtor_on_void) << Base->getSourceRange(); |
7859 | else { |
7860 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
7861 | << ObjectType << Base->getSourceRange(); |
7862 | return ExprError(); |
7863 | } |
7864 | } |
7865 | |
7866 | // C++ [expr.pseudo]p2: |
7867 | // [...] The cv-unqualified versions of the object type and of the type |
7868 | // designated by the pseudo-destructor-name shall be the same type. |
7869 | if (DestructedTypeInfo) { |
7870 | QualType DestructedType = DestructedTypeInfo->getType(); |
7871 | SourceLocation DestructedTypeStart = |
7872 | DestructedTypeInfo->getTypeLoc().getBeginLoc(); |
7873 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { |
7874 | if (!Context.hasSameUnqualifiedType(T1: DestructedType, T2: ObjectType)) { |
7875 | // Detect dot pseudo destructor calls on pointer objects, e.g.: |
7876 | // Foo *foo; |
7877 | // foo.~Foo(); |
7878 | if (OpKind == tok::period && ObjectType->isPointerType() && |
7879 | Context.hasSameUnqualifiedType(T1: DestructedType, |
7880 | T2: ObjectType->getPointeeType())) { |
7881 | auto Diagnostic = |
7882 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
7883 | << ObjectType << /*IsArrow=*/0 << Base->getSourceRange(); |
7884 | |
7885 | // Issue a fixit only when the destructor is valid. |
7886 | if (canRecoverDotPseudoDestructorCallsOnPointerObjects( |
7887 | SemaRef&: *this, DestructedType)) |
7888 | Diagnostic << FixItHint::CreateReplacement(RemoveRange: OpLoc, Code: "->" ); |
7889 | |
7890 | // Recover by setting the object type to the destructed type and the |
7891 | // operator to '->'. |
7892 | ObjectType = DestructedType; |
7893 | OpKind = tok::arrow; |
7894 | } else { |
7895 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
7896 | << ObjectType << DestructedType << Base->getSourceRange() |
7897 | << DestructedTypeInfo->getTypeLoc().getSourceRange(); |
7898 | |
7899 | // Recover by setting the destructed type to the object type. |
7900 | DestructedType = ObjectType; |
7901 | DestructedTypeInfo = |
7902 | Context.getTrivialTypeSourceInfo(T: ObjectType, Loc: DestructedTypeStart); |
7903 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
7904 | } |
7905 | } else if (DestructedType.getObjCLifetime() != |
7906 | ObjectType.getObjCLifetime()) { |
7907 | |
7908 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { |
7909 | // Okay: just pretend that the user provided the correctly-qualified |
7910 | // type. |
7911 | } else { |
7912 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) |
7913 | << ObjectType << DestructedType << Base->getSourceRange() |
7914 | << DestructedTypeInfo->getTypeLoc().getSourceRange(); |
7915 | } |
7916 | |
7917 | // Recover by setting the destructed type to the object type. |
7918 | DestructedType = ObjectType; |
7919 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(T: ObjectType, |
7920 | Loc: DestructedTypeStart); |
7921 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
7922 | } |
7923 | } |
7924 | } |
7925 | |
7926 | // C++ [expr.pseudo]p2: |
7927 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
7928 | // form |
7929 | // |
7930 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
7931 | // |
7932 | // shall designate the same scalar type. |
7933 | if (ScopeTypeInfo) { |
7934 | QualType ScopeType = ScopeTypeInfo->getType(); |
7935 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
7936 | !Context.hasSameUnqualifiedType(T1: ScopeType, T2: ObjectType)) { |
7937 | |
7938 | Diag(ScopeTypeInfo->getTypeLoc().getSourceRange().getBegin(), |
7939 | diag::err_pseudo_dtor_type_mismatch) |
7940 | << ObjectType << ScopeType << Base->getSourceRange() |
7941 | << ScopeTypeInfo->getTypeLoc().getSourceRange(); |
7942 | |
7943 | ScopeType = QualType(); |
7944 | ScopeTypeInfo = nullptr; |
7945 | } |
7946 | } |
7947 | |
7948 | Expr *Result |
7949 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
7950 | OpKind == tok::arrow, OpLoc, |
7951 | SS.getWithLocInContext(Context), |
7952 | ScopeTypeInfo, |
7953 | CCLoc, |
7954 | TildeLoc, |
7955 | Destructed); |
7956 | |
7957 | return Result; |
7958 | } |
7959 | |
7960 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
7961 | SourceLocation OpLoc, |
7962 | tok::TokenKind OpKind, |
7963 | CXXScopeSpec &SS, |
7964 | UnqualifiedId &FirstTypeName, |
7965 | SourceLocation CCLoc, |
7966 | SourceLocation TildeLoc, |
7967 | UnqualifiedId &SecondTypeName) { |
7968 | assert((FirstTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
7969 | FirstTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) && |
7970 | "Invalid first type name in pseudo-destructor" ); |
7971 | assert((SecondTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
7972 | SecondTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) && |
7973 | "Invalid second type name in pseudo-destructor" ); |
7974 | |
7975 | QualType ObjectType; |
7976 | if (CheckArrow(S&: *this, ObjectType, Base, OpKind, OpLoc)) |
7977 | return ExprError(); |
7978 | |
7979 | // Compute the object type that we should use for name lookup purposes. Only |
7980 | // record types and dependent types matter. |
7981 | ParsedType ObjectTypePtrForLookup; |
7982 | if (!SS.isSet()) { |
7983 | if (ObjectType->isRecordType()) |
7984 | ObjectTypePtrForLookup = ParsedType::make(P: ObjectType); |
7985 | else if (ObjectType->isDependentType()) |
7986 | ObjectTypePtrForLookup = ParsedType::make(P: Context.DependentTy); |
7987 | } |
7988 | |
7989 | // Convert the name of the type being destructed (following the ~) into a |
7990 | // type (with source-location information). |
7991 | QualType DestructedType; |
7992 | TypeSourceInfo *DestructedTypeInfo = nullptr; |
7993 | PseudoDestructorTypeStorage Destructed; |
7994 | if (SecondTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) { |
7995 | ParsedType T = getTypeName(II: *SecondTypeName.Identifier, |
7996 | NameLoc: SecondTypeName.StartLocation, |
7997 | S, SS: &SS, isClassName: true, HasTrailingDot: false, ObjectType: ObjectTypePtrForLookup, |
7998 | /*IsCtorOrDtorName*/true); |
7999 | if (!T && |
8000 | ((SS.isSet() && !computeDeclContext(SS, EnteringContext: false)) || |
8001 | (!SS.isSet() && ObjectType->isDependentType()))) { |
8002 | // The name of the type being destroyed is a dependent name, and we |
8003 | // couldn't find anything useful in scope. Just store the identifier and |
8004 | // it's location, and we'll perform (qualified) name lookup again at |
8005 | // template instantiation time. |
8006 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
8007 | SecondTypeName.StartLocation); |
8008 | } else if (!T) { |
8009 | Diag(SecondTypeName.StartLocation, |
8010 | diag::err_pseudo_dtor_destructor_non_type) |
8011 | << SecondTypeName.Identifier << ObjectType; |
8012 | if (isSFINAEContext()) |
8013 | return ExprError(); |
8014 | |
8015 | // Recover by assuming we had the right type all along. |
8016 | DestructedType = ObjectType; |
8017 | } else |
8018 | DestructedType = GetTypeFromParser(Ty: T, TInfo: &DestructedTypeInfo); |
8019 | } else { |
8020 | // Resolve the template-id to a type. |
8021 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
8022 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
8023 | TemplateId->NumArgs); |
8024 | TypeResult T = ActOnTemplateIdType(S, |
8025 | SS, |
8026 | TemplateKWLoc: TemplateId->TemplateKWLoc, |
8027 | Template: TemplateId->Template, |
8028 | TemplateII: TemplateId->Name, |
8029 | TemplateIILoc: TemplateId->TemplateNameLoc, |
8030 | LAngleLoc: TemplateId->LAngleLoc, |
8031 | TemplateArgs: TemplateArgsPtr, |
8032 | RAngleLoc: TemplateId->RAngleLoc, |
8033 | /*IsCtorOrDtorName*/true); |
8034 | if (T.isInvalid() || !T.get()) { |
8035 | // Recover by assuming we had the right type all along. |
8036 | DestructedType = ObjectType; |
8037 | } else |
8038 | DestructedType = GetTypeFromParser(Ty: T.get(), TInfo: &DestructedTypeInfo); |
8039 | } |
8040 | |
8041 | // If we've performed some kind of recovery, (re-)build the type source |
8042 | // information. |
8043 | if (!DestructedType.isNull()) { |
8044 | if (!DestructedTypeInfo) |
8045 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(T: DestructedType, |
8046 | Loc: SecondTypeName.StartLocation); |
8047 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
8048 | } |
8049 | |
8050 | // Convert the name of the scope type (the type prior to '::') into a type. |
8051 | TypeSourceInfo *ScopeTypeInfo = nullptr; |
8052 | QualType ScopeType; |
8053 | if (FirstTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
8054 | FirstTypeName.Identifier) { |
8055 | if (FirstTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) { |
8056 | ParsedType T = getTypeName(II: *FirstTypeName.Identifier, |
8057 | NameLoc: FirstTypeName.StartLocation, |
8058 | S, SS: &SS, isClassName: true, HasTrailingDot: false, ObjectType: ObjectTypePtrForLookup, |
8059 | /*IsCtorOrDtorName*/true); |
8060 | if (!T) { |
8061 | Diag(FirstTypeName.StartLocation, |
8062 | diag::err_pseudo_dtor_destructor_non_type) |
8063 | << FirstTypeName.Identifier << ObjectType; |
8064 | |
8065 | if (isSFINAEContext()) |
8066 | return ExprError(); |
8067 | |
8068 | // Just drop this type. It's unnecessary anyway. |
8069 | ScopeType = QualType(); |
8070 | } else |
8071 | ScopeType = GetTypeFromParser(Ty: T, TInfo: &ScopeTypeInfo); |
8072 | } else { |
8073 | // Resolve the template-id to a type. |
8074 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
8075 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
8076 | TemplateId->NumArgs); |
8077 | TypeResult T = ActOnTemplateIdType(S, |
8078 | SS, |
8079 | TemplateKWLoc: TemplateId->TemplateKWLoc, |
8080 | Template: TemplateId->Template, |
8081 | TemplateII: TemplateId->Name, |
8082 | TemplateIILoc: TemplateId->TemplateNameLoc, |
8083 | LAngleLoc: TemplateId->LAngleLoc, |
8084 | TemplateArgs: TemplateArgsPtr, |
8085 | RAngleLoc: TemplateId->RAngleLoc, |
8086 | /*IsCtorOrDtorName*/true); |
8087 | if (T.isInvalid() || !T.get()) { |
8088 | // Recover by dropping this type. |
8089 | ScopeType = QualType(); |
8090 | } else |
8091 | ScopeType = GetTypeFromParser(Ty: T.get(), TInfo: &ScopeTypeInfo); |
8092 | } |
8093 | } |
8094 | |
8095 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
8096 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(T: ScopeType, |
8097 | Loc: FirstTypeName.StartLocation); |
8098 | |
8099 | |
8100 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
8101 | ScopeTypeInfo, CCLoc, TildeLoc, |
8102 | Destructed); |
8103 | } |
8104 | |
8105 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
8106 | SourceLocation OpLoc, |
8107 | tok::TokenKind OpKind, |
8108 | SourceLocation TildeLoc, |
8109 | const DeclSpec& DS) { |
8110 | QualType ObjectType; |
8111 | QualType T; |
8112 | TypeLocBuilder TLB; |
8113 | if (CheckArrow(S&: *this, ObjectType, Base, OpKind, OpLoc)) |
8114 | return ExprError(); |
8115 | |
8116 | switch (DS.getTypeSpecType()) { |
8117 | case DeclSpec::TST_decltype_auto: { |
8118 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decltype_auto_invalid); |
8119 | return true; |
8120 | } |
8121 | case DeclSpec::TST_decltype: { |
8122 | T = BuildDecltypeType(E: DS.getRepAsExpr(), /*AsUnevaluated=*/false); |
8123 | DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); |
8124 | DecltypeTL.setDecltypeLoc(DS.getTypeSpecTypeLoc()); |
8125 | DecltypeTL.setRParenLoc(DS.getTypeofParensRange().getEnd()); |
8126 | break; |
8127 | } |
8128 | case DeclSpec::TST_typename_pack_indexing: { |
8129 | T = ActOnPackIndexingType(Pattern: DS.getRepAsType().get(), IndexExpr: DS.getPackIndexingExpr(), |
8130 | Loc: DS.getBeginLoc(), EllipsisLoc: DS.getEllipsisLoc()); |
8131 | TLB.pushTrivial(Context&: getASTContext(), |
8132 | T: cast<PackIndexingType>(Val: T.getTypePtr())->getPattern(), |
8133 | Loc: DS.getBeginLoc()); |
8134 | PackIndexingTypeLoc PITL = TLB.push<PackIndexingTypeLoc>(T); |
8135 | PITL.setEllipsisLoc(DS.getEllipsisLoc()); |
8136 | break; |
8137 | } |
8138 | default: |
8139 | llvm_unreachable("Unsupported type in pseudo destructor" ); |
8140 | } |
8141 | TypeSourceInfo *DestructedTypeInfo = TLB.getTypeSourceInfo(Context, T); |
8142 | PseudoDestructorTypeStorage Destructed(DestructedTypeInfo); |
8143 | |
8144 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS: CXXScopeSpec(), |
8145 | ScopeTypeInfo: nullptr, CCLoc: SourceLocation(), TildeLoc, |
8146 | Destructed); |
8147 | } |
8148 | |
8149 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
8150 | SourceLocation RParen) { |
8151 | // If the operand is an unresolved lookup expression, the expression is ill- |
8152 | // formed per [over.over]p1, because overloaded function names cannot be used |
8153 | // without arguments except in explicit contexts. |
8154 | ExprResult R = CheckPlaceholderExpr(E: Operand); |
8155 | if (R.isInvalid()) |
8156 | return R; |
8157 | |
8158 | R = CheckUnevaluatedOperand(E: R.get()); |
8159 | if (R.isInvalid()) |
8160 | return ExprError(); |
8161 | |
8162 | Operand = R.get(); |
8163 | |
8164 | if (!inTemplateInstantiation() && !Operand->isInstantiationDependent() && |
8165 | Operand->HasSideEffects(Ctx: Context, IncludePossibleEffects: false)) { |
8166 | // The expression operand for noexcept is in an unevaluated expression |
8167 | // context, so side effects could result in unintended consequences. |
8168 | Diag(Operand->getExprLoc(), diag::warn_side_effects_unevaluated_context); |
8169 | } |
8170 | |
8171 | CanThrowResult CanThrow = canThrow(Operand); |
8172 | return new (Context) |
8173 | CXXNoexceptExpr(Context.BoolTy, Operand, CanThrow, KeyLoc, RParen); |
8174 | } |
8175 | |
8176 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
8177 | Expr *Operand, SourceLocation RParen) { |
8178 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
8179 | } |
8180 | |
8181 | static void MaybeDecrementCount( |
8182 | Expr *E, llvm::DenseMap<const VarDecl *, int> &RefsMinusAssignments) { |
8183 | DeclRefExpr *LHS = nullptr; |
8184 | bool IsCompoundAssign = false; |
8185 | bool isIncrementDecrementUnaryOp = false; |
8186 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Val: E)) { |
8187 | if (BO->getLHS()->getType()->isDependentType() || |
8188 | BO->getRHS()->getType()->isDependentType()) { |
8189 | if (BO->getOpcode() != BO_Assign) |
8190 | return; |
8191 | } else if (!BO->isAssignmentOp()) |
8192 | return; |
8193 | else |
8194 | IsCompoundAssign = BO->isCompoundAssignmentOp(); |
8195 | LHS = dyn_cast<DeclRefExpr>(Val: BO->getLHS()); |
8196 | } else if (CXXOperatorCallExpr *COCE = dyn_cast<CXXOperatorCallExpr>(Val: E)) { |
8197 | if (COCE->getOperator() != OO_Equal) |
8198 | return; |
8199 | LHS = dyn_cast<DeclRefExpr>(COCE->getArg(0)); |
8200 | } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(Val: E)) { |
8201 | if (!UO->isIncrementDecrementOp()) |
8202 | return; |
8203 | isIncrementDecrementUnaryOp = true; |
8204 | LHS = dyn_cast<DeclRefExpr>(Val: UO->getSubExpr()); |
8205 | } |
8206 | if (!LHS) |
8207 | return; |
8208 | VarDecl *VD = dyn_cast<VarDecl>(Val: LHS->getDecl()); |
8209 | if (!VD) |
8210 | return; |
8211 | // Don't decrement RefsMinusAssignments if volatile variable with compound |
8212 | // assignment (+=, ...) or increment/decrement unary operator to avoid |
8213 | // potential unused-but-set-variable warning. |
8214 | if ((IsCompoundAssign || isIncrementDecrementUnaryOp) && |
8215 | VD->getType().isVolatileQualified()) |
8216 | return; |
8217 | auto iter = RefsMinusAssignments.find(Val: VD); |
8218 | if (iter == RefsMinusAssignments.end()) |
8219 | return; |
8220 | iter->getSecond()--; |
8221 | } |
8222 | |
8223 | /// Perform the conversions required for an expression used in a |
8224 | /// context that ignores the result. |
8225 | ExprResult Sema::IgnoredValueConversions(Expr *E) { |
8226 | MaybeDecrementCount(E, RefsMinusAssignments); |
8227 | |
8228 | if (E->hasPlaceholderType()) { |
8229 | ExprResult result = CheckPlaceholderExpr(E); |
8230 | if (result.isInvalid()) return E; |
8231 | E = result.get(); |
8232 | } |
8233 | |
8234 | if (getLangOpts().CPlusPlus) { |
8235 | // The C++11 standard defines the notion of a discarded-value expression; |
8236 | // normally, we don't need to do anything to handle it, but if it is a |
8237 | // volatile lvalue with a special form, we perform an lvalue-to-rvalue |
8238 | // conversion. |
8239 | if (getLangOpts().CPlusPlus11 && E->isReadIfDiscardedInCPlusPlus11()) { |
8240 | ExprResult Res = DefaultLvalueConversion(E); |
8241 | if (Res.isInvalid()) |
8242 | return E; |
8243 | E = Res.get(); |
8244 | } else { |
8245 | // Per C++2a [expr.ass]p5, a volatile assignment is not deprecated if |
8246 | // it occurs as a discarded-value expression. |
8247 | CheckUnusedVolatileAssignment(E); |
8248 | } |
8249 | |
8250 | // C++1z: |
8251 | // If the expression is a prvalue after this optional conversion, the |
8252 | // temporary materialization conversion is applied. |
8253 | // |
8254 | // We do not materialize temporaries by default in order to avoid creating |
8255 | // unnecessary temporary objects. If we skip this step, IR generation is |
8256 | // able to synthesize the storage for itself in the aggregate case, and |
8257 | // adding the extra node to the AST is just clutter. |
8258 | if (isInMaterializeTemporaryObjectContext() && getLangOpts().CPlusPlus17 && |
8259 | E->isPRValue() && !E->getType()->isVoidType()) { |
8260 | ExprResult Res = TemporaryMaterializationConversion(E); |
8261 | if (Res.isInvalid()) |
8262 | return E; |
8263 | E = Res.get(); |
8264 | } |
8265 | return E; |
8266 | } |
8267 | |
8268 | // C99 6.3.2.1: |
8269 | // [Except in specific positions,] an lvalue that does not have |
8270 | // array type is converted to the value stored in the |
8271 | // designated object (and is no longer an lvalue). |
8272 | if (E->isPRValue()) { |
8273 | // In C, function designators (i.e. expressions of function type) |
8274 | // are r-values, but we still want to do function-to-pointer decay |
8275 | // on them. This is both technically correct and convenient for |
8276 | // some clients. |
8277 | if (!getLangOpts().CPlusPlus && E->getType()->isFunctionType()) |
8278 | return DefaultFunctionArrayConversion(E); |
8279 | |
8280 | return E; |
8281 | } |
8282 | |
8283 | // GCC seems to also exclude expressions of incomplete enum type. |
8284 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
8285 | if (!T->getDecl()->isComplete()) { |
8286 | // FIXME: stupid workaround for a codegen bug! |
8287 | E = ImpCastExprToType(E, Type: Context.VoidTy, CK: CK_ToVoid).get(); |
8288 | return E; |
8289 | } |
8290 | } |
8291 | |
8292 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
8293 | if (Res.isInvalid()) |
8294 | return E; |
8295 | E = Res.get(); |
8296 | |
8297 | if (!E->getType()->isVoidType()) |
8298 | RequireCompleteType(E->getExprLoc(), E->getType(), |
8299 | diag::err_incomplete_type); |
8300 | return E; |
8301 | } |
8302 | |
8303 | ExprResult Sema::CheckUnevaluatedOperand(Expr *E) { |
8304 | // Per C++2a [expr.ass]p5, a volatile assignment is not deprecated if |
8305 | // it occurs as an unevaluated operand. |
8306 | CheckUnusedVolatileAssignment(E); |
8307 | |
8308 | return E; |
8309 | } |
8310 | |
8311 | // If we can unambiguously determine whether Var can never be used |
8312 | // in a constant expression, return true. |
8313 | // - if the variable and its initializer are non-dependent, then |
8314 | // we can unambiguously check if the variable is a constant expression. |
8315 | // - if the initializer is not value dependent - we can determine whether |
8316 | // it can be used to initialize a constant expression. If Init can not |
8317 | // be used to initialize a constant expression we conclude that Var can |
8318 | // never be a constant expression. |
8319 | // - FXIME: if the initializer is dependent, we can still do some analysis and |
8320 | // identify certain cases unambiguously as non-const by using a Visitor: |
8321 | // - such as those that involve odr-use of a ParmVarDecl, involve a new |
8322 | // delete, lambda-expr, dynamic-cast, reinterpret-cast etc... |
8323 | static inline bool VariableCanNeverBeAConstantExpression(VarDecl *Var, |
8324 | ASTContext &Context) { |
8325 | if (isa<ParmVarDecl>(Val: Var)) return true; |
8326 | const VarDecl *DefVD = nullptr; |
8327 | |
8328 | // If there is no initializer - this can not be a constant expression. |
8329 | const Expr *Init = Var->getAnyInitializer(D&: DefVD); |
8330 | if (!Init) |
8331 | return true; |
8332 | assert(DefVD); |
8333 | if (DefVD->isWeak()) |
8334 | return false; |
8335 | |
8336 | if (Var->getType()->isDependentType() || Init->isValueDependent()) { |
8337 | // FIXME: Teach the constant evaluator to deal with the non-dependent parts |
8338 | // of value-dependent expressions, and use it here to determine whether the |
8339 | // initializer is a potential constant expression. |
8340 | return false; |
8341 | } |
8342 | |
8343 | return !Var->isUsableInConstantExpressions(C: Context); |
8344 | } |
8345 | |
8346 | /// Check if the current lambda has any potential captures |
8347 | /// that must be captured by any of its enclosing lambdas that are ready to |
8348 | /// capture. If there is a lambda that can capture a nested |
8349 | /// potential-capture, go ahead and do so. Also, check to see if any |
8350 | /// variables are uncaptureable or do not involve an odr-use so do not |
8351 | /// need to be captured. |
8352 | |
8353 | static void CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures( |
8354 | Expr *const FE, LambdaScopeInfo *const CurrentLSI, Sema &S) { |
8355 | |
8356 | assert(!S.isUnevaluatedContext()); |
8357 | assert(S.CurContext->isDependentContext()); |
8358 | #ifndef NDEBUG |
8359 | DeclContext *DC = S.CurContext; |
8360 | while (DC && isa<CapturedDecl>(Val: DC)) |
8361 | DC = DC->getParent(); |
8362 | assert( |
8363 | CurrentLSI->CallOperator == DC && |
8364 | "The current call operator must be synchronized with Sema's CurContext" ); |
8365 | #endif // NDEBUG |
8366 | |
8367 | const bool IsFullExprInstantiationDependent = FE->isInstantiationDependent(); |
8368 | |
8369 | // All the potentially captureable variables in the current nested |
8370 | // lambda (within a generic outer lambda), must be captured by an |
8371 | // outer lambda that is enclosed within a non-dependent context. |
8372 | CurrentLSI->visitPotentialCaptures(Callback: [&](ValueDecl *Var, Expr *VarExpr) { |
8373 | // If the variable is clearly identified as non-odr-used and the full |
8374 | // expression is not instantiation dependent, only then do we not |
8375 | // need to check enclosing lambda's for speculative captures. |
8376 | // For e.g.: |
8377 | // Even though 'x' is not odr-used, it should be captured. |
8378 | // int test() { |
8379 | // const int x = 10; |
8380 | // auto L = [=](auto a) { |
8381 | // (void) +x + a; |
8382 | // }; |
8383 | // } |
8384 | if (CurrentLSI->isVariableExprMarkedAsNonODRUsed(CapturingVarExpr: VarExpr) && |
8385 | !IsFullExprInstantiationDependent) |
8386 | return; |
8387 | |
8388 | VarDecl *UnderlyingVar = Var->getPotentiallyDecomposedVarDecl(); |
8389 | if (!UnderlyingVar) |
8390 | return; |
8391 | |
8392 | // If we have a capture-capable lambda for the variable, go ahead and |
8393 | // capture the variable in that lambda (and all its enclosing lambdas). |
8394 | if (const std::optional<unsigned> Index = |
8395 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
8396 | FunctionScopes: S.FunctionScopes, VarToCapture: Var, S)) |
8397 | S.MarkCaptureUsedInEnclosingContext(Capture: Var, Loc: VarExpr->getExprLoc(), CapturingScopeIndex: *Index); |
8398 | const bool IsVarNeverAConstantExpression = |
8399 | VariableCanNeverBeAConstantExpression(Var: UnderlyingVar, Context&: S.Context); |
8400 | if (!IsFullExprInstantiationDependent || IsVarNeverAConstantExpression) { |
8401 | // This full expression is not instantiation dependent or the variable |
8402 | // can not be used in a constant expression - which means |
8403 | // this variable must be odr-used here, so diagnose a |
8404 | // capture violation early, if the variable is un-captureable. |
8405 | // This is purely for diagnosing errors early. Otherwise, this |
8406 | // error would get diagnosed when the lambda becomes capture ready. |
8407 | QualType CaptureType, DeclRefType; |
8408 | SourceLocation ExprLoc = VarExpr->getExprLoc(); |
8409 | if (S.tryCaptureVariable(Var, Loc: ExprLoc, Kind: S.TryCapture_Implicit, |
8410 | /*EllipsisLoc*/ SourceLocation(), |
8411 | /*BuildAndDiagnose*/false, CaptureType, |
8412 | DeclRefType, FunctionScopeIndexToStopAt: nullptr)) { |
8413 | // We will never be able to capture this variable, and we need |
8414 | // to be able to in any and all instantiations, so diagnose it. |
8415 | S.tryCaptureVariable(Var, Loc: ExprLoc, Kind: S.TryCapture_Implicit, |
8416 | /*EllipsisLoc*/ SourceLocation(), |
8417 | /*BuildAndDiagnose*/true, CaptureType, |
8418 | DeclRefType, FunctionScopeIndexToStopAt: nullptr); |
8419 | } |
8420 | } |
8421 | }); |
8422 | |
8423 | // Check if 'this' needs to be captured. |
8424 | if (CurrentLSI->hasPotentialThisCapture()) { |
8425 | // If we have a capture-capable lambda for 'this', go ahead and capture |
8426 | // 'this' in that lambda (and all its enclosing lambdas). |
8427 | if (const std::optional<unsigned> Index = |
8428 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
8429 | FunctionScopes: S.FunctionScopes, /*0 is 'this'*/ VarToCapture: nullptr, S)) { |
8430 | const unsigned FunctionScopeIndexOfCapturableLambda = *Index; |
8431 | S.CheckCXXThisCapture(Loc: CurrentLSI->PotentialThisCaptureLocation, |
8432 | /*Explicit*/ false, /*BuildAndDiagnose*/ true, |
8433 | FunctionScopeIndexToStopAt: &FunctionScopeIndexOfCapturableLambda); |
8434 | } |
8435 | } |
8436 | |
8437 | // Reset all the potential captures at the end of each full-expression. |
8438 | CurrentLSI->clearPotentialCaptures(); |
8439 | } |
8440 | |
8441 | static ExprResult attemptRecovery(Sema &SemaRef, |
8442 | const TypoCorrectionConsumer &Consumer, |
8443 | const TypoCorrection &TC) { |
8444 | LookupResult R(SemaRef, Consumer.getLookupResult().getLookupNameInfo(), |
8445 | Consumer.getLookupResult().getLookupKind()); |
8446 | const CXXScopeSpec *SS = Consumer.getSS(); |
8447 | CXXScopeSpec NewSS; |
8448 | |
8449 | // Use an approprate CXXScopeSpec for building the expr. |
8450 | if (auto *NNS = TC.getCorrectionSpecifier()) |
8451 | NewSS.MakeTrivial(Context&: SemaRef.Context, Qualifier: NNS, R: TC.getCorrectionRange()); |
8452 | else if (SS && !TC.WillReplaceSpecifier()) |
8453 | NewSS = *SS; |
8454 | |
8455 | if (auto *ND = TC.getFoundDecl()) { |
8456 | R.setLookupName(ND->getDeclName()); |
8457 | R.addDecl(D: ND); |
8458 | if (ND->isCXXClassMember()) { |
8459 | // Figure out the correct naming class to add to the LookupResult. |
8460 | CXXRecordDecl *Record = nullptr; |
8461 | if (auto *NNS = TC.getCorrectionSpecifier()) |
8462 | Record = NNS->getAsType()->getAsCXXRecordDecl(); |
8463 | if (!Record) |
8464 | Record = |
8465 | dyn_cast<CXXRecordDecl>(ND->getDeclContext()->getRedeclContext()); |
8466 | if (Record) |
8467 | R.setNamingClass(Record); |
8468 | |
8469 | // Detect and handle the case where the decl might be an implicit |
8470 | // member. |
8471 | bool MightBeImplicitMember; |
8472 | if (!Consumer.isAddressOfOperand()) |
8473 | MightBeImplicitMember = true; |
8474 | else if (!NewSS.isEmpty()) |
8475 | MightBeImplicitMember = false; |
8476 | else if (R.isOverloadedResult()) |
8477 | MightBeImplicitMember = false; |
8478 | else if (R.isUnresolvableResult()) |
8479 | MightBeImplicitMember = true; |
8480 | else |
8481 | MightBeImplicitMember = isa<FieldDecl>(Val: ND) || |
8482 | isa<IndirectFieldDecl>(Val: ND) || |
8483 | isa<MSPropertyDecl>(Val: ND); |
8484 | |
8485 | if (MightBeImplicitMember) |
8486 | return SemaRef.BuildPossibleImplicitMemberExpr( |
8487 | SS: NewSS, /*TemplateKWLoc*/ SourceLocation(), R, |
8488 | /*TemplateArgs*/ nullptr, /*S*/ nullptr); |
8489 | } else if (auto *Ivar = dyn_cast<ObjCIvarDecl>(Val: ND)) { |
8490 | return SemaRef.LookupInObjCMethod(LookUp&: R, S: Consumer.getScope(), |
8491 | II: Ivar->getIdentifier()); |
8492 | } |
8493 | } |
8494 | |
8495 | return SemaRef.BuildDeclarationNameExpr(SS: NewSS, R, /*NeedsADL*/ false, |
8496 | /*AcceptInvalidDecl*/ true); |
8497 | } |
8498 | |
8499 | namespace { |
8500 | class FindTypoExprs : public RecursiveASTVisitor<FindTypoExprs> { |
8501 | llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs; |
8502 | |
8503 | public: |
8504 | explicit FindTypoExprs(llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs) |
8505 | : TypoExprs(TypoExprs) {} |
8506 | bool VisitTypoExpr(TypoExpr *TE) { |
8507 | TypoExprs.insert(X: TE); |
8508 | return true; |
8509 | } |
8510 | }; |
8511 | |
8512 | class TransformTypos : public TreeTransform<TransformTypos> { |
8513 | typedef TreeTransform<TransformTypos> BaseTransform; |
8514 | |
8515 | VarDecl *InitDecl; // A decl to avoid as a correction because it is in the |
8516 | // process of being initialized. |
8517 | llvm::function_ref<ExprResult(Expr *)> ExprFilter; |
8518 | llvm::SmallSetVector<TypoExpr *, 2> TypoExprs, AmbiguousTypoExprs; |
8519 | llvm::SmallDenseMap<TypoExpr *, ExprResult, 2> TransformCache; |
8520 | llvm::SmallDenseMap<OverloadExpr *, Expr *, 4> OverloadResolution; |
8521 | |
8522 | /// Emit diagnostics for all of the TypoExprs encountered. |
8523 | /// |
8524 | /// If the TypoExprs were successfully corrected, then the diagnostics should |
8525 | /// suggest the corrections. Otherwise the diagnostics will not suggest |
8526 | /// anything (having been passed an empty TypoCorrection). |
8527 | /// |
8528 | /// If we've failed to correct due to ambiguous corrections, we need to |
8529 | /// be sure to pass empty corrections and replacements. Otherwise it's |
8530 | /// possible that the Consumer has a TypoCorrection that failed to ambiguity |
8531 | /// and we don't want to report those diagnostics. |
8532 | void EmitAllDiagnostics(bool IsAmbiguous) { |
8533 | for (TypoExpr *TE : TypoExprs) { |
8534 | auto &State = SemaRef.getTypoExprState(TE); |
8535 | if (State.DiagHandler) { |
8536 | TypoCorrection TC = IsAmbiguous |
8537 | ? TypoCorrection() : State.Consumer->getCurrentCorrection(); |
8538 | ExprResult Replacement = IsAmbiguous ? ExprError() : TransformCache[TE]; |
8539 | |
8540 | // Extract the NamedDecl from the transformed TypoExpr and add it to the |
8541 | // TypoCorrection, replacing the existing decls. This ensures the right |
8542 | // NamedDecl is used in diagnostics e.g. in the case where overload |
8543 | // resolution was used to select one from several possible decls that |
8544 | // had been stored in the TypoCorrection. |
8545 | if (auto *ND = getDeclFromExpr( |
8546 | E: Replacement.isInvalid() ? nullptr : Replacement.get())) |
8547 | TC.setCorrectionDecl(ND); |
8548 | |
8549 | State.DiagHandler(TC); |
8550 | } |
8551 | SemaRef.clearDelayedTypo(TE); |
8552 | } |
8553 | } |
8554 | |
8555 | /// Try to advance the typo correction state of the first unfinished TypoExpr. |
8556 | /// We allow advancement of the correction stream by removing it from the |
8557 | /// TransformCache which allows `TransformTypoExpr` to advance during the |
8558 | /// next transformation attempt. |
8559 | /// |
8560 | /// Any substitution attempts for the previous TypoExprs (which must have been |
8561 | /// finished) will need to be retried since it's possible that they will now |
8562 | /// be invalid given the latest advancement. |
8563 | /// |
8564 | /// We need to be sure that we're making progress - it's possible that the |
8565 | /// tree is so malformed that the transform never makes it to the |
8566 | /// `TransformTypoExpr`. |
8567 | /// |
8568 | /// Returns true if there are any untried correction combinations. |
8569 | bool CheckAndAdvanceTypoExprCorrectionStreams() { |
8570 | for (auto *TE : TypoExprs) { |
8571 | auto &State = SemaRef.getTypoExprState(TE); |
8572 | TransformCache.erase(Val: TE); |
8573 | if (!State.Consumer->hasMadeAnyCorrectionProgress()) |
8574 | return false; |
8575 | if (!State.Consumer->finished()) |
8576 | return true; |
8577 | State.Consumer->resetCorrectionStream(); |
8578 | } |
8579 | return false; |
8580 | } |
8581 | |
8582 | NamedDecl *getDeclFromExpr(Expr *E) { |
8583 | if (auto *OE = dyn_cast_or_null<OverloadExpr>(Val: E)) |
8584 | E = OverloadResolution[OE]; |
8585 | |
8586 | if (!E) |
8587 | return nullptr; |
8588 | if (auto *DRE = dyn_cast<DeclRefExpr>(Val: E)) |
8589 | return DRE->getFoundDecl(); |
8590 | if (auto *ME = dyn_cast<MemberExpr>(Val: E)) |
8591 | return ME->getFoundDecl(); |
8592 | // FIXME: Add any other expr types that could be seen by the delayed typo |
8593 | // correction TreeTransform for which the corresponding TypoCorrection could |
8594 | // contain multiple decls. |
8595 | return nullptr; |
8596 | } |
8597 | |
8598 | ExprResult TryTransform(Expr *E) { |
8599 | Sema::SFINAETrap Trap(SemaRef); |
8600 | ExprResult Res = TransformExpr(E); |
8601 | if (Trap.hasErrorOccurred() || Res.isInvalid()) |
8602 | return ExprError(); |
8603 | |
8604 | return ExprFilter(Res.get()); |
8605 | } |
8606 | |
8607 | // Since correcting typos may intoduce new TypoExprs, this function |
8608 | // checks for new TypoExprs and recurses if it finds any. Note that it will |
8609 | // only succeed if it is able to correct all typos in the given expression. |
8610 | ExprResult CheckForRecursiveTypos(ExprResult Res, bool &IsAmbiguous) { |
8611 | if (Res.isInvalid()) { |
8612 | return Res; |
8613 | } |
8614 | // Check to see if any new TypoExprs were created. If so, we need to recurse |
8615 | // to check their validity. |
8616 | Expr *FixedExpr = Res.get(); |
8617 | |
8618 | auto SavedTypoExprs = std::move(TypoExprs); |
8619 | auto SavedAmbiguousTypoExprs = std::move(AmbiguousTypoExprs); |
8620 | TypoExprs.clear(); |
8621 | AmbiguousTypoExprs.clear(); |
8622 | |
8623 | FindTypoExprs(TypoExprs).TraverseStmt(FixedExpr); |
8624 | if (!TypoExprs.empty()) { |
8625 | // Recurse to handle newly created TypoExprs. If we're not able to |
8626 | // handle them, discard these TypoExprs. |
8627 | ExprResult RecurResult = |
8628 | RecursiveTransformLoop(E: FixedExpr, IsAmbiguous); |
8629 | if (RecurResult.isInvalid()) { |
8630 | Res = ExprError(); |
8631 | // Recursive corrections didn't work, wipe them away and don't add |
8632 | // them to the TypoExprs set. Remove them from Sema's TypoExpr list |
8633 | // since we don't want to clear them twice. Note: it's possible the |
8634 | // TypoExprs were created recursively and thus won't be in our |
8635 | // Sema's TypoExprs - they were created in our `RecursiveTransformLoop`. |
8636 | auto &SemaTypoExprs = SemaRef.TypoExprs; |
8637 | for (auto *TE : TypoExprs) { |
8638 | TransformCache.erase(Val: TE); |
8639 | SemaRef.clearDelayedTypo(TE); |
8640 | |
8641 | auto SI = find(SemaTypoExprs, TE); |
8642 | if (SI != SemaTypoExprs.end()) { |
8643 | SemaTypoExprs.erase(SI); |
8644 | } |
8645 | } |
8646 | } else { |
8647 | // TypoExpr is valid: add newly created TypoExprs since we were |
8648 | // able to correct them. |
8649 | Res = RecurResult; |
8650 | SavedTypoExprs.set_union(TypoExprs); |
8651 | } |
8652 | } |
8653 | |
8654 | TypoExprs = std::move(SavedTypoExprs); |
8655 | AmbiguousTypoExprs = std::move(SavedAmbiguousTypoExprs); |
8656 | |
8657 | return Res; |
8658 | } |
8659 | |
8660 | // Try to transform the given expression, looping through the correction |
8661 | // candidates with `CheckAndAdvanceTypoExprCorrectionStreams`. |
8662 | // |
8663 | // If valid ambiguous typo corrections are seen, `IsAmbiguous` is set to |
8664 | // true and this method immediately will return an `ExprError`. |
8665 | ExprResult RecursiveTransformLoop(Expr *E, bool &IsAmbiguous) { |
8666 | ExprResult Res; |
8667 | auto SavedTypoExprs = std::move(SemaRef.TypoExprs); |
8668 | SemaRef.TypoExprs.clear(); |
8669 | |
8670 | while (true) { |
8671 | Res = CheckForRecursiveTypos(Res: TryTransform(E), IsAmbiguous); |
8672 | |
8673 | // Recursion encountered an ambiguous correction. This means that our |
8674 | // correction itself is ambiguous, so stop now. |
8675 | if (IsAmbiguous) |
8676 | break; |
8677 | |
8678 | // If the transform is still valid after checking for any new typos, |
8679 | // it's good to go. |
8680 | if (!Res.isInvalid()) |
8681 | break; |
8682 | |
8683 | // The transform was invalid, see if we have any TypoExprs with untried |
8684 | // correction candidates. |
8685 | if (!CheckAndAdvanceTypoExprCorrectionStreams()) |
8686 | break; |
8687 | } |
8688 | |
8689 | // If we found a valid result, double check to make sure it's not ambiguous. |
8690 | if (!IsAmbiguous && !Res.isInvalid() && !AmbiguousTypoExprs.empty()) { |
8691 | auto SavedTransformCache = |
8692 | llvm::SmallDenseMap<TypoExpr *, ExprResult, 2>(TransformCache); |
8693 | |
8694 | // Ensure none of the TypoExprs have multiple typo correction candidates |
8695 | // with the same edit length that pass all the checks and filters. |
8696 | while (!AmbiguousTypoExprs.empty()) { |
8697 | auto TE = AmbiguousTypoExprs.back(); |
8698 | |
8699 | // TryTransform itself can create new Typos, adding them to the TypoExpr map |
8700 | // and invalidating our TypoExprState, so always fetch it instead of storing. |
8701 | SemaRef.getTypoExprState(TE).Consumer->saveCurrentPosition(); |
8702 | |
8703 | TypoCorrection TC = SemaRef.getTypoExprState(TE).Consumer->peekNextCorrection(); |
8704 | TypoCorrection Next; |
8705 | do { |
8706 | // Fetch the next correction by erasing the typo from the cache and calling |
8707 | // `TryTransform` which will iterate through corrections in |
8708 | // `TransformTypoExpr`. |
8709 | TransformCache.erase(Val: TE); |
8710 | ExprResult AmbigRes = CheckForRecursiveTypos(Res: TryTransform(E), IsAmbiguous); |
8711 | |
8712 | if (!AmbigRes.isInvalid() || IsAmbiguous) { |
8713 | SemaRef.getTypoExprState(TE).Consumer->resetCorrectionStream(); |
8714 | SavedTransformCache.erase(Val: TE); |
8715 | Res = ExprError(); |
8716 | IsAmbiguous = true; |
8717 | break; |
8718 | } |
8719 | } while ((Next = SemaRef.getTypoExprState(TE).Consumer->peekNextCorrection()) && |
8720 | Next.getEditDistance(false) == TC.getEditDistance(false)); |
8721 | |
8722 | if (IsAmbiguous) |
8723 | break; |
8724 | |
8725 | AmbiguousTypoExprs.remove(X: TE); |
8726 | SemaRef.getTypoExprState(TE).Consumer->restoreSavedPosition(); |
8727 | TransformCache[TE] = SavedTransformCache[TE]; |
8728 | } |
8729 | TransformCache = std::move(SavedTransformCache); |
8730 | } |
8731 | |
8732 | // Wipe away any newly created TypoExprs that we don't know about. Since we |
8733 | // clear any invalid TypoExprs in `CheckForRecursiveTypos`, this is only |
8734 | // possible if a `TypoExpr` is created during a transformation but then |
8735 | // fails before we can discover it. |
8736 | auto &SemaTypoExprs = SemaRef.TypoExprs; |
8737 | for (auto Iterator = SemaTypoExprs.begin(); Iterator != SemaTypoExprs.end();) { |
8738 | auto TE = *Iterator; |
8739 | auto FI = find(TypoExprs, TE); |
8740 | if (FI != TypoExprs.end()) { |
8741 | Iterator++; |
8742 | continue; |
8743 | } |
8744 | SemaRef.clearDelayedTypo(TE); |
8745 | Iterator = SemaTypoExprs.erase(Iterator); |
8746 | } |
8747 | SemaRef.TypoExprs = std::move(SavedTypoExprs); |
8748 | |
8749 | return Res; |
8750 | } |
8751 | |
8752 | public: |
8753 | TransformTypos(Sema &SemaRef, VarDecl *InitDecl, llvm::function_ref<ExprResult(Expr *)> Filter) |
8754 | : BaseTransform(SemaRef), InitDecl(InitDecl), ExprFilter(Filter) {} |
8755 | |
8756 | ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc, |
8757 | MultiExprArg Args, |
8758 | SourceLocation RParenLoc, |
8759 | Expr *ExecConfig = nullptr) { |
8760 | auto Result = BaseTransform::RebuildCallExpr(Callee, LParenLoc, Args, |
8761 | RParenLoc, ExecConfig); |
8762 | if (auto *OE = dyn_cast<OverloadExpr>(Val: Callee)) { |
8763 | if (Result.isUsable()) { |
8764 | Expr *ResultCall = Result.get(); |
8765 | if (auto *BE = dyn_cast<CXXBindTemporaryExpr>(ResultCall)) |
8766 | ResultCall = BE->getSubExpr(); |
8767 | if (auto *CE = dyn_cast<CallExpr>(ResultCall)) |
8768 | OverloadResolution[OE] = CE->getCallee(); |
8769 | } |
8770 | } |
8771 | return Result; |
8772 | } |
8773 | |
8774 | ExprResult TransformLambdaExpr(LambdaExpr *E) { return Owned(E); } |
8775 | |
8776 | ExprResult TransformBlockExpr(BlockExpr *E) { return Owned(E); } |
8777 | |
8778 | ExprResult Transform(Expr *E) { |
8779 | bool IsAmbiguous = false; |
8780 | ExprResult Res = RecursiveTransformLoop(E, IsAmbiguous); |
8781 | |
8782 | if (!Res.isUsable()) |
8783 | FindTypoExprs(TypoExprs).TraverseStmt(E); |
8784 | |
8785 | EmitAllDiagnostics(IsAmbiguous); |
8786 | |
8787 | return Res; |
8788 | } |
8789 | |
8790 | ExprResult TransformTypoExpr(TypoExpr *E) { |
8791 | // If the TypoExpr hasn't been seen before, record it. Otherwise, return the |
8792 | // cached transformation result if there is one and the TypoExpr isn't the |
8793 | // first one that was encountered. |
8794 | auto &CacheEntry = TransformCache[E]; |
8795 | if (!TypoExprs.insert(X: E) && !CacheEntry.isUnset()) { |
8796 | return CacheEntry; |
8797 | } |
8798 | |
8799 | auto &State = SemaRef.getTypoExprState(E); |
8800 | assert(State.Consumer && "Cannot transform a cleared TypoExpr" ); |
8801 | |
8802 | // For the first TypoExpr and an uncached TypoExpr, find the next likely |
8803 | // typo correction and return it. |
8804 | while (TypoCorrection TC = State.Consumer->getNextCorrection()) { |
8805 | if (InitDecl && TC.getFoundDecl() == InitDecl) |
8806 | continue; |
8807 | // FIXME: If we would typo-correct to an invalid declaration, it's |
8808 | // probably best to just suppress all errors from this typo correction. |
8809 | ExprResult NE = State.RecoveryHandler ? |
8810 | State.RecoveryHandler(SemaRef, E, TC) : |
8811 | attemptRecovery(SemaRef, *State.Consumer, TC); |
8812 | if (!NE.isInvalid()) { |
8813 | // Check whether there may be a second viable correction with the same |
8814 | // edit distance; if so, remember this TypoExpr may have an ambiguous |
8815 | // correction so it can be more thoroughly vetted later. |
8816 | TypoCorrection Next; |
8817 | if ((Next = State.Consumer->peekNextCorrection()) && |
8818 | Next.getEditDistance(Normalized: false) == TC.getEditDistance(Normalized: false)) { |
8819 | AmbiguousTypoExprs.insert(X: E); |
8820 | } else { |
8821 | AmbiguousTypoExprs.remove(X: E); |
8822 | } |
8823 | assert(!NE.isUnset() && |
8824 | "Typo was transformed into a valid-but-null ExprResult" ); |
8825 | return CacheEntry = NE; |
8826 | } |
8827 | } |
8828 | return CacheEntry = ExprError(); |
8829 | } |
8830 | }; |
8831 | } |
8832 | |
8833 | ExprResult |
8834 | Sema::CorrectDelayedTyposInExpr(Expr *E, VarDecl *InitDecl, |
8835 | bool RecoverUncorrectedTypos, |
8836 | llvm::function_ref<ExprResult(Expr *)> Filter) { |
8837 | // If the current evaluation context indicates there are uncorrected typos |
8838 | // and the current expression isn't guaranteed to not have typos, try to |
8839 | // resolve any TypoExpr nodes that might be in the expression. |
8840 | if (E && !ExprEvalContexts.empty() && ExprEvalContexts.back().NumTypos && |
8841 | (E->isTypeDependent() || E->isValueDependent() || |
8842 | E->isInstantiationDependent())) { |
8843 | auto TyposResolved = DelayedTypos.size(); |
8844 | auto Result = TransformTypos(*this, InitDecl, Filter).Transform(E); |
8845 | TyposResolved -= DelayedTypos.size(); |
8846 | if (Result.isInvalid() || Result.get() != E) { |
8847 | ExprEvalContexts.back().NumTypos -= TyposResolved; |
8848 | if (Result.isInvalid() && RecoverUncorrectedTypos) { |
8849 | struct TyposReplace : TreeTransform<TyposReplace> { |
8850 | TyposReplace(Sema &SemaRef) : TreeTransform(SemaRef) {} |
8851 | ExprResult TransformTypoExpr(clang::TypoExpr *E) { |
8852 | return this->SemaRef.CreateRecoveryExpr(E->getBeginLoc(), |
8853 | E->getEndLoc(), {}); |
8854 | } |
8855 | } TT(*this); |
8856 | return TT.TransformExpr(E); |
8857 | } |
8858 | return Result; |
8859 | } |
8860 | assert(TyposResolved == 0 && "Corrected typo but got same Expr back?" ); |
8861 | } |
8862 | return E; |
8863 | } |
8864 | |
8865 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC, |
8866 | bool DiscardedValue, bool IsConstexpr, |
8867 | bool IsTemplateArgument) { |
8868 | ExprResult FullExpr = FE; |
8869 | |
8870 | if (!FullExpr.get()) |
8871 | return ExprError(); |
8872 | |
8873 | if (!IsTemplateArgument && DiagnoseUnexpandedParameterPack(E: FullExpr.get())) |
8874 | return ExprError(); |
8875 | |
8876 | if (DiscardedValue) { |
8877 | // Top-level expressions default to 'id' when we're in a debugger. |
8878 | if (getLangOpts().DebuggerCastResultToId && |
8879 | FullExpr.get()->getType() == Context.UnknownAnyTy) { |
8880 | FullExpr = forceUnknownAnyToType(E: FullExpr.get(), ToType: Context.getObjCIdType()); |
8881 | if (FullExpr.isInvalid()) |
8882 | return ExprError(); |
8883 | } |
8884 | |
8885 | FullExpr = CheckPlaceholderExpr(E: FullExpr.get()); |
8886 | if (FullExpr.isInvalid()) |
8887 | return ExprError(); |
8888 | |
8889 | FullExpr = IgnoredValueConversions(E: FullExpr.get()); |
8890 | if (FullExpr.isInvalid()) |
8891 | return ExprError(); |
8892 | |
8893 | DiagnoseUnusedExprResult(FullExpr.get(), diag::warn_unused_expr); |
8894 | } |
8895 | |
8896 | FullExpr = CorrectDelayedTyposInExpr(E: FullExpr.get(), /*InitDecl=*/nullptr, |
8897 | /*RecoverUncorrectedTypos=*/true); |
8898 | if (FullExpr.isInvalid()) |
8899 | return ExprError(); |
8900 | |
8901 | CheckCompletedExpr(E: FullExpr.get(), CheckLoc: CC, IsConstexpr); |
8902 | |
8903 | // At the end of this full expression (which could be a deeply nested |
8904 | // lambda), if there is a potential capture within the nested lambda, |
8905 | // have the outer capture-able lambda try and capture it. |
8906 | // Consider the following code: |
8907 | // void f(int, int); |
8908 | // void f(const int&, double); |
8909 | // void foo() { |
8910 | // const int x = 10, y = 20; |
8911 | // auto L = [=](auto a) { |
8912 | // auto M = [=](auto b) { |
8913 | // f(x, b); <-- requires x to be captured by L and M |
8914 | // f(y, a); <-- requires y to be captured by L, but not all Ms |
8915 | // }; |
8916 | // }; |
8917 | // } |
8918 | |
8919 | // FIXME: Also consider what happens for something like this that involves |
8920 | // the gnu-extension statement-expressions or even lambda-init-captures: |
8921 | // void f() { |
8922 | // const int n = 0; |
8923 | // auto L = [&](auto a) { |
8924 | // +n + ({ 0; a; }); |
8925 | // }; |
8926 | // } |
8927 | // |
8928 | // Here, we see +n, and then the full-expression 0; ends, so we don't |
8929 | // capture n (and instead remove it from our list of potential captures), |
8930 | // and then the full-expression +n + ({ 0; }); ends, but it's too late |
8931 | // for us to see that we need to capture n after all. |
8932 | |
8933 | LambdaScopeInfo *const CurrentLSI = |
8934 | getCurLambda(/*IgnoreCapturedRegions=*/IgnoreNonLambdaCapturingScope: true); |
8935 | // FIXME: PR 17877 showed that getCurLambda() can return a valid pointer |
8936 | // even if CurContext is not a lambda call operator. Refer to that Bug Report |
8937 | // for an example of the code that might cause this asynchrony. |
8938 | // By ensuring we are in the context of a lambda's call operator |
8939 | // we can fix the bug (we only need to check whether we need to capture |
8940 | // if we are within a lambda's body); but per the comments in that |
8941 | // PR, a proper fix would entail : |
8942 | // "Alternative suggestion: |
8943 | // - Add to Sema an integer holding the smallest (outermost) scope |
8944 | // index that we are *lexically* within, and save/restore/set to |
8945 | // FunctionScopes.size() in InstantiatingTemplate's |
8946 | // constructor/destructor. |
8947 | // - Teach the handful of places that iterate over FunctionScopes to |
8948 | // stop at the outermost enclosing lexical scope." |
8949 | DeclContext *DC = CurContext; |
8950 | while (DC && isa<CapturedDecl>(Val: DC)) |
8951 | DC = DC->getParent(); |
8952 | const bool IsInLambdaDeclContext = isLambdaCallOperator(DC); |
8953 | if (IsInLambdaDeclContext && CurrentLSI && |
8954 | CurrentLSI->hasPotentialCaptures() && !FullExpr.isInvalid()) |
8955 | CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(FE, CurrentLSI, |
8956 | S&: *this); |
8957 | return MaybeCreateExprWithCleanups(SubExpr: FullExpr); |
8958 | } |
8959 | |
8960 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
8961 | if (!FullStmt) return StmtError(); |
8962 | |
8963 | return MaybeCreateStmtWithCleanups(SubStmt: FullStmt); |
8964 | } |
8965 | |
8966 | Sema::IfExistsResult |
8967 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, |
8968 | CXXScopeSpec &SS, |
8969 | const DeclarationNameInfo &TargetNameInfo) { |
8970 | DeclarationName TargetName = TargetNameInfo.getName(); |
8971 | if (!TargetName) |
8972 | return IER_DoesNotExist; |
8973 | |
8974 | // If the name itself is dependent, then the result is dependent. |
8975 | if (TargetName.isDependentName()) |
8976 | return IER_Dependent; |
8977 | |
8978 | // Do the redeclaration lookup in the current scope. |
8979 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, |
8980 | Sema::NotForRedeclaration); |
8981 | LookupParsedName(R, S, SS: &SS); |
8982 | R.suppressDiagnostics(); |
8983 | |
8984 | switch (R.getResultKind()) { |
8985 | case LookupResult::Found: |
8986 | case LookupResult::FoundOverloaded: |
8987 | case LookupResult::FoundUnresolvedValue: |
8988 | case LookupResult::Ambiguous: |
8989 | return IER_Exists; |
8990 | |
8991 | case LookupResult::NotFound: |
8992 | return IER_DoesNotExist; |
8993 | |
8994 | case LookupResult::NotFoundInCurrentInstantiation: |
8995 | return IER_Dependent; |
8996 | } |
8997 | |
8998 | llvm_unreachable("Invalid LookupResult Kind!" ); |
8999 | } |
9000 | |
9001 | Sema::IfExistsResult |
9002 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc, |
9003 | bool IsIfExists, CXXScopeSpec &SS, |
9004 | UnqualifiedId &Name) { |
9005 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
9006 | |
9007 | // Check for an unexpanded parameter pack. |
9008 | auto UPPC = IsIfExists ? UPPC_IfExists : UPPC_IfNotExists; |
9009 | if (DiagnoseUnexpandedParameterPack(SS, UPPC) || |
9010 | DiagnoseUnexpandedParameterPack(NameInfo: TargetNameInfo, UPPC)) |
9011 | return IER_Error; |
9012 | |
9013 | return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo); |
9014 | } |
9015 | |
9016 | concepts::Requirement *Sema::ActOnSimpleRequirement(Expr *E) { |
9017 | return BuildExprRequirement(E, /*IsSimple=*/IsSatisfied: true, |
9018 | /*NoexceptLoc=*/SourceLocation(), |
9019 | /*ReturnTypeRequirement=*/{}); |
9020 | } |
9021 | |
9022 | concepts::Requirement * |
9023 | Sema::ActOnTypeRequirement(SourceLocation TypenameKWLoc, CXXScopeSpec &SS, |
9024 | SourceLocation NameLoc, IdentifierInfo *TypeName, |
9025 | TemplateIdAnnotation *TemplateId) { |
9026 | assert(((!TypeName && TemplateId) || (TypeName && !TemplateId)) && |
9027 | "Exactly one of TypeName and TemplateId must be specified." ); |
9028 | TypeSourceInfo *TSI = nullptr; |
9029 | if (TypeName) { |
9030 | QualType T = |
9031 | CheckTypenameType(Keyword: ElaboratedTypeKeyword::Typename, KeywordLoc: TypenameKWLoc, |
9032 | QualifierLoc: SS.getWithLocInContext(Context), II: *TypeName, IILoc: NameLoc, |
9033 | TSI: &TSI, /*DeducedTSTContext=*/false); |
9034 | if (T.isNull()) |
9035 | return nullptr; |
9036 | } else { |
9037 | ASTTemplateArgsPtr ArgsPtr(TemplateId->getTemplateArgs(), |
9038 | TemplateId->NumArgs); |
9039 | TypeResult T = ActOnTypenameType(S: CurScope, TypenameLoc: TypenameKWLoc, SS, |
9040 | TemplateLoc: TemplateId->TemplateKWLoc, |
9041 | TemplateName: TemplateId->Template, TemplateII: TemplateId->Name, |
9042 | TemplateIILoc: TemplateId->TemplateNameLoc, |
9043 | LAngleLoc: TemplateId->LAngleLoc, TemplateArgs: ArgsPtr, |
9044 | RAngleLoc: TemplateId->RAngleLoc); |
9045 | if (T.isInvalid()) |
9046 | return nullptr; |
9047 | if (GetTypeFromParser(Ty: T.get(), TInfo: &TSI).isNull()) |
9048 | return nullptr; |
9049 | } |
9050 | return BuildTypeRequirement(Type: TSI); |
9051 | } |
9052 | |
9053 | concepts::Requirement * |
9054 | Sema::ActOnCompoundRequirement(Expr *E, SourceLocation NoexceptLoc) { |
9055 | return BuildExprRequirement(E, /*IsSimple=*/IsSatisfied: false, NoexceptLoc, |
9056 | /*ReturnTypeRequirement=*/{}); |
9057 | } |
9058 | |
9059 | concepts::Requirement * |
9060 | Sema::ActOnCompoundRequirement( |
9061 | Expr *E, SourceLocation NoexceptLoc, CXXScopeSpec &SS, |
9062 | TemplateIdAnnotation *TypeConstraint, unsigned Depth) { |
9063 | // C++2a [expr.prim.req.compound] p1.3.3 |
9064 | // [..] the expression is deduced against an invented function template |
9065 | // F [...] F is a void function template with a single type template |
9066 | // parameter T declared with the constrained-parameter. Form a new |
9067 | // cv-qualifier-seq cv by taking the union of const and volatile specifiers |
9068 | // around the constrained-parameter. F has a single parameter whose |
9069 | // type-specifier is cv T followed by the abstract-declarator. [...] |
9070 | // |
9071 | // The cv part is done in the calling function - we get the concept with |
9072 | // arguments and the abstract declarator with the correct CV qualification and |
9073 | // have to synthesize T and the single parameter of F. |
9074 | auto &II = Context.Idents.get(Name: "expr-type" ); |
9075 | auto *TParam = TemplateTypeParmDecl::Create(C: Context, DC: CurContext, |
9076 | KeyLoc: SourceLocation(), |
9077 | NameLoc: SourceLocation(), D: Depth, |
9078 | /*Index=*/P: 0, Id: &II, |
9079 | /*Typename=*/true, |
9080 | /*ParameterPack=*/false, |
9081 | /*HasTypeConstraint=*/true); |
9082 | |
9083 | if (BuildTypeConstraint(SS, TypeConstraint, ConstrainedParameter: TParam, |
9084 | /*EllipsisLoc=*/SourceLocation(), |
9085 | /*AllowUnexpandedPack=*/true)) |
9086 | // Just produce a requirement with no type requirements. |
9087 | return BuildExprRequirement(E, /*IsSimple=*/IsSatisfied: false, NoexceptLoc, ReturnTypeRequirement: {}); |
9088 | |
9089 | auto *TPL = TemplateParameterList::Create(C: Context, TemplateLoc: SourceLocation(), |
9090 | LAngleLoc: SourceLocation(), |
9091 | Params: ArrayRef<NamedDecl *>(TParam), |
9092 | RAngleLoc: SourceLocation(), |
9093 | /*RequiresClause=*/nullptr); |
9094 | return BuildExprRequirement( |
9095 | E, /*IsSimple=*/IsSatisfied: false, NoexceptLoc, |
9096 | ReturnTypeRequirement: concepts::ExprRequirement::ReturnTypeRequirement(TPL)); |
9097 | } |
9098 | |
9099 | concepts::ExprRequirement * |
9100 | Sema::BuildExprRequirement( |
9101 | Expr *E, bool IsSimple, SourceLocation NoexceptLoc, |
9102 | concepts::ExprRequirement::ReturnTypeRequirement ReturnTypeRequirement) { |
9103 | auto Status = concepts::ExprRequirement::SS_Satisfied; |
9104 | ConceptSpecializationExpr *SubstitutedConstraintExpr = nullptr; |
9105 | if (E->isInstantiationDependent() || E->getType()->isPlaceholderType() || |
9106 | ReturnTypeRequirement.isDependent()) |
9107 | Status = concepts::ExprRequirement::SS_Dependent; |
9108 | else if (NoexceptLoc.isValid() && canThrow(E) == CanThrowResult::CT_Can) |
9109 | Status = concepts::ExprRequirement::SS_NoexceptNotMet; |
9110 | else if (ReturnTypeRequirement.isSubstitutionFailure()) |
9111 | Status = concepts::ExprRequirement::SS_TypeRequirementSubstitutionFailure; |
9112 | else if (ReturnTypeRequirement.isTypeConstraint()) { |
9113 | // C++2a [expr.prim.req]p1.3.3 |
9114 | // The immediately-declared constraint ([temp]) of decltype((E)) shall |
9115 | // be satisfied. |
9116 | TemplateParameterList *TPL = |
9117 | ReturnTypeRequirement.getTypeConstraintTemplateParameterList(); |
9118 | QualType MatchedType = |
9119 | Context.getReferenceQualifiedType(e: E).getCanonicalType(); |
9120 | llvm::SmallVector<TemplateArgument, 1> Args; |
9121 | Args.push_back(Elt: TemplateArgument(MatchedType)); |
9122 | |
9123 | auto *Param = cast<TemplateTypeParmDecl>(Val: TPL->getParam(Idx: 0)); |
9124 | |
9125 | MultiLevelTemplateArgumentList MLTAL(Param, Args, /*Final=*/false); |
9126 | MLTAL.addOuterRetainedLevels(Num: TPL->getDepth()); |
9127 | const TypeConstraint *TC = Param->getTypeConstraint(); |
9128 | assert(TC && "Type Constraint cannot be null here" ); |
9129 | auto *IDC = TC->getImmediatelyDeclaredConstraint(); |
9130 | assert(IDC && "ImmediatelyDeclaredConstraint can't be null here." ); |
9131 | ExprResult Constraint = SubstExpr(E: IDC, TemplateArgs: MLTAL); |
9132 | if (Constraint.isInvalid()) { |
9133 | return new (Context) concepts::ExprRequirement( |
9134 | concepts::createSubstDiagAt(S&: *this, Location: IDC->getExprLoc(), |
9135 | Printer: [&](llvm::raw_ostream &OS) { |
9136 | IDC->printPretty(OS, /*Helper=*/nullptr, |
9137 | getPrintingPolicy()); |
9138 | }), |
9139 | IsSimple, NoexceptLoc, ReturnTypeRequirement); |
9140 | } |
9141 | SubstitutedConstraintExpr = |
9142 | cast<ConceptSpecializationExpr>(Val: Constraint.get()); |
9143 | if (!SubstitutedConstraintExpr->isSatisfied()) |
9144 | Status = concepts::ExprRequirement::SS_ConstraintsNotSatisfied; |
9145 | } |
9146 | return new (Context) concepts::ExprRequirement(E, IsSimple, NoexceptLoc, |
9147 | ReturnTypeRequirement, Status, |
9148 | SubstitutedConstraintExpr); |
9149 | } |
9150 | |
9151 | concepts::ExprRequirement * |
9152 | Sema::BuildExprRequirement( |
9153 | concepts::Requirement::SubstitutionDiagnostic *ExprSubstitutionDiagnostic, |
9154 | bool IsSimple, SourceLocation NoexceptLoc, |
9155 | concepts::ExprRequirement::ReturnTypeRequirement ReturnTypeRequirement) { |
9156 | return new (Context) concepts::ExprRequirement(ExprSubstitutionDiagnostic, |
9157 | IsSimple, NoexceptLoc, |
9158 | ReturnTypeRequirement); |
9159 | } |
9160 | |
9161 | concepts::TypeRequirement * |
9162 | Sema::BuildTypeRequirement(TypeSourceInfo *Type) { |
9163 | return new (Context) concepts::TypeRequirement(Type); |
9164 | } |
9165 | |
9166 | concepts::TypeRequirement * |
9167 | Sema::BuildTypeRequirement( |
9168 | concepts::Requirement::SubstitutionDiagnostic *SubstDiag) { |
9169 | return new (Context) concepts::TypeRequirement(SubstDiag); |
9170 | } |
9171 | |
9172 | concepts::Requirement *Sema::ActOnNestedRequirement(Expr *Constraint) { |
9173 | return BuildNestedRequirement(E: Constraint); |
9174 | } |
9175 | |
9176 | concepts::NestedRequirement * |
9177 | Sema::BuildNestedRequirement(Expr *Constraint) { |
9178 | ConstraintSatisfaction Satisfaction; |
9179 | if (!Constraint->isInstantiationDependent() && |
9180 | CheckConstraintSatisfaction(nullptr, {Constraint}, /*TemplateArgs=*/{}, |
9181 | Constraint->getSourceRange(), Satisfaction)) |
9182 | return nullptr; |
9183 | return new (Context) concepts::NestedRequirement(Context, Constraint, |
9184 | Satisfaction); |
9185 | } |
9186 | |
9187 | concepts::NestedRequirement * |
9188 | Sema::BuildNestedRequirement(StringRef InvalidConstraintEntity, |
9189 | const ASTConstraintSatisfaction &Satisfaction) { |
9190 | return new (Context) concepts::NestedRequirement( |
9191 | InvalidConstraintEntity, |
9192 | ASTConstraintSatisfaction::Rebuild(C: Context, Satisfaction)); |
9193 | } |
9194 | |
9195 | RequiresExprBodyDecl * |
9196 | Sema::ActOnStartRequiresExpr(SourceLocation RequiresKWLoc, |
9197 | ArrayRef<ParmVarDecl *> LocalParameters, |
9198 | Scope *BodyScope) { |
9199 | assert(BodyScope); |
9200 | |
9201 | RequiresExprBodyDecl *Body = RequiresExprBodyDecl::Create(C&: Context, DC: CurContext, |
9202 | StartLoc: RequiresKWLoc); |
9203 | |
9204 | PushDeclContext(BodyScope, Body); |
9205 | |
9206 | for (ParmVarDecl *Param : LocalParameters) { |
9207 | if (Param->hasDefaultArg()) |
9208 | // C++2a [expr.prim.req] p4 |
9209 | // [...] A local parameter of a requires-expression shall not have a |
9210 | // default argument. [...] |
9211 | Diag(Param->getDefaultArgRange().getBegin(), |
9212 | diag::err_requires_expr_local_parameter_default_argument); |
9213 | // Ignore default argument and move on |
9214 | |
9215 | Param->setDeclContext(Body); |
9216 | // If this has an identifier, add it to the scope stack. |
9217 | if (Param->getIdentifier()) { |
9218 | CheckShadow(BodyScope, Param); |
9219 | PushOnScopeChains(Param, BodyScope); |
9220 | } |
9221 | } |
9222 | return Body; |
9223 | } |
9224 | |
9225 | void Sema::ActOnFinishRequiresExpr() { |
9226 | assert(CurContext && "DeclContext imbalance!" ); |
9227 | CurContext = CurContext->getLexicalParent(); |
9228 | assert(CurContext && "Popped translation unit!" ); |
9229 | } |
9230 | |
9231 | ExprResult Sema::ActOnRequiresExpr( |
9232 | SourceLocation RequiresKWLoc, RequiresExprBodyDecl *Body, |
9233 | SourceLocation LParenLoc, ArrayRef<ParmVarDecl *> LocalParameters, |
9234 | SourceLocation RParenLoc, ArrayRef<concepts::Requirement *> Requirements, |
9235 | SourceLocation ClosingBraceLoc) { |
9236 | auto *RE = RequiresExpr::Create(C&: Context, RequiresKWLoc, Body, LParenLoc, |
9237 | LocalParameters, RParenLoc, Requirements, |
9238 | RBraceLoc: ClosingBraceLoc); |
9239 | if (DiagnoseUnexpandedParameterPackInRequiresExpr(RE)) |
9240 | return ExprError(); |
9241 | return RE; |
9242 | } |
9243 | |