1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "clang/Sema/SemaInternal.h" |
15 | #include "TreeTransform.h" |
16 | #include "TypeLocBuilder.h" |
17 | #include "clang/AST/ASTContext.h" |
18 | #include "clang/AST/ASTLambda.h" |
19 | #include "clang/AST/CXXInheritance.h" |
20 | #include "clang/AST/CharUnits.h" |
21 | #include "clang/AST/DeclObjC.h" |
22 | #include "clang/AST/ExprCXX.h" |
23 | #include "clang/AST/ExprObjC.h" |
24 | #include "clang/AST/RecursiveASTVisitor.h" |
25 | #include "clang/AST/TypeLoc.h" |
26 | #include "clang/Basic/AlignedAllocation.h" |
27 | #include "clang/Basic/PartialDiagnostic.h" |
28 | #include "clang/Basic/TargetInfo.h" |
29 | #include "clang/Lex/Preprocessor.h" |
30 | #include "clang/Sema/DeclSpec.h" |
31 | #include "clang/Sema/Initialization.h" |
32 | #include "clang/Sema/Lookup.h" |
33 | #include "clang/Sema/ParsedTemplate.h" |
34 | #include "clang/Sema/Scope.h" |
35 | #include "clang/Sema/ScopeInfo.h" |
36 | #include "clang/Sema/SemaLambda.h" |
37 | #include "clang/Sema/TemplateDeduction.h" |
38 | #include "llvm/ADT/APInt.h" |
39 | #include "llvm/ADT/STLExtras.h" |
40 | #include "llvm/Support/ErrorHandling.h" |
41 | using namespace clang; |
42 | using namespace sema; |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | ParsedType Sema::getInheritingConstructorName(CXXScopeSpec &SS, |
49 | SourceLocation NameLoc, |
50 | IdentifierInfo &Name) { |
51 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
52 | |
53 | |
54 | QualType Type; |
55 | switch (NNS->getKind()) { |
56 | case NestedNameSpecifier::TypeSpec: |
57 | case NestedNameSpecifier::TypeSpecWithTemplate: |
58 | Type = QualType(NNS->getAsType(), 0); |
59 | break; |
60 | |
61 | case NestedNameSpecifier::Identifier: |
62 | |
63 | |
64 | (0) . __assert_fail ("NNS->getAsIdentifier() == &Name && \"not a constructor name\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 64, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(NNS->getAsIdentifier() == &Name && "not a constructor name"); |
65 | Type = Context.getDependentNameType(ETK_None, NNS->getPrefix(), |
66 | NNS->getAsIdentifier()); |
67 | break; |
68 | |
69 | case NestedNameSpecifier::Global: |
70 | case NestedNameSpecifier::Super: |
71 | case NestedNameSpecifier::Namespace: |
72 | case NestedNameSpecifier::NamespaceAlias: |
73 | llvm_unreachable("Nested name specifier is not a type for inheriting ctor"); |
74 | } |
75 | |
76 | |
77 | |
78 | return CreateParsedType(Type, |
79 | Context.getTrivialTypeSourceInfo(Type, NameLoc)); |
80 | } |
81 | |
82 | ParsedType Sema::getConstructorName(IdentifierInfo &II, |
83 | SourceLocation NameLoc, |
84 | Scope *S, CXXScopeSpec &SS, |
85 | bool EnteringContext) { |
86 | CXXRecordDecl *CurClass = getCurrentClass(S, &SS); |
87 | (0) . __assert_fail ("CurClass && &II == CurClass->getIdentifier() && \"not a constructor name\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 88, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurClass && &II == CurClass->getIdentifier() && |
88 | (0) . __assert_fail ("CurClass && &II == CurClass->getIdentifier() && \"not a constructor name\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 88, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "not a constructor name"); |
89 | |
90 | |
91 | |
92 | |
93 | if (CurClass->isDependentContext() && !EnteringContext) { |
94 | QualType T = Context.getDependentNameType(ETK_None, SS.getScopeRep(), &II); |
95 | return ParsedType::make(T); |
96 | } |
97 | |
98 | if (SS.isNotEmpty() && RequireCompleteDeclContext(SS, CurClass)) |
99 | return ParsedType(); |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | CXXRecordDecl *InjectedClassName = nullptr; |
108 | for (NamedDecl *ND : CurClass->lookup(&II)) { |
109 | auto *RD = dyn_cast<CXXRecordDecl>(ND); |
110 | if (RD && RD->isInjectedClassName()) { |
111 | InjectedClassName = RD; |
112 | break; |
113 | } |
114 | } |
115 | if (!InjectedClassName) { |
116 | if (!CurClass->isInvalidDecl()) { |
117 | |
118 | |
119 | Diag(SS.getLastQualifierNameLoc(), |
120 | diag::err_incomplete_nested_name_spec) << CurClass << SS.getRange(); |
121 | } |
122 | return ParsedType(); |
123 | } |
124 | |
125 | QualType T = Context.getTypeDeclType(InjectedClassName); |
126 | DiagnoseUseOfDecl(InjectedClassName, NameLoc); |
127 | MarkAnyDeclReferenced(NameLoc, InjectedClassName, ); |
128 | |
129 | return ParsedType::make(T); |
130 | } |
131 | |
132 | ParsedType Sema::getDestructorName(SourceLocation TildeLoc, |
133 | IdentifierInfo &II, |
134 | SourceLocation NameLoc, |
135 | Scope *S, CXXScopeSpec &SS, |
136 | ParsedType ObjectTypePtr, |
137 | bool EnteringContext) { |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | |
149 | |
150 | |
151 | |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 | QualType SearchType; |
161 | DeclContext *LookupCtx = nullptr; |
162 | bool isDependent = false; |
163 | bool LookInScope = false; |
164 | |
165 | if (SS.isInvalid()) |
166 | return nullptr; |
167 | |
168 | |
169 | |
170 | |
171 | if (ObjectTypePtr) |
172 | SearchType = GetTypeFromParser(ObjectTypePtr); |
173 | |
174 | if (SS.isSet()) { |
175 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
176 | |
177 | bool AlreadySearched = false; |
178 | bool LookAtPrefix = true; |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | DeclContext *DC = computeDeclContext(SS, EnteringContext); |
191 | if (DC && DC->isFileContext()) { |
192 | AlreadySearched = true; |
193 | LookupCtx = DC; |
194 | isDependent = false; |
195 | } else if (DC && isa<CXXRecordDecl>(DC)) { |
196 | LookAtPrefix = false; |
197 | LookInScope = true; |
198 | } |
199 | |
200 | |
201 | NestedNameSpecifier *Prefix = nullptr; |
202 | if (AlreadySearched) { |
203 | |
204 | } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) { |
205 | CXXScopeSpec PrefixSS; |
206 | PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data())); |
207 | LookupCtx = computeDeclContext(PrefixSS, EnteringContext); |
208 | isDependent = isDependentScopeSpecifier(PrefixSS); |
209 | } else if (ObjectTypePtr) { |
210 | LookupCtx = computeDeclContext(SearchType); |
211 | isDependent = SearchType->isDependentType(); |
212 | } else { |
213 | LookupCtx = computeDeclContext(SS, EnteringContext); |
214 | isDependent = LookupCtx && LookupCtx->isDependentContext(); |
215 | } |
216 | } else if (ObjectTypePtr) { |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | LookupCtx = computeDeclContext(SearchType); |
225 | isDependent = SearchType->isDependentType(); |
226 | (0) . __assert_fail ("(isDependent || !SearchType->isIncompleteType()) && \"Caller should have completed object type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 227, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((isDependent || !SearchType->isIncompleteType()) && |
227 | (0) . __assert_fail ("(isDependent || !SearchType->isIncompleteType()) && \"Caller should have completed object type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 227, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Caller should have completed object type"); |
228 | |
229 | LookInScope = true; |
230 | } else { |
231 | |
232 | LookInScope = true; |
233 | } |
234 | |
235 | TypeDecl *NonMatchingTypeDecl = nullptr; |
236 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); |
237 | for (unsigned Step = 0; Step != 2; ++Step) { |
238 | |
239 | |
240 | |
241 | Found.clear(); |
242 | if (Step == 0 && LookupCtx) { |
243 | if (RequireCompleteDeclContext(SS, LookupCtx)) |
244 | return nullptr; |
245 | LookupQualifiedName(Found, LookupCtx); |
246 | } else if (Step == 1 && LookInScope && S) { |
247 | LookupName(Found, S); |
248 | } else { |
249 | continue; |
250 | } |
251 | |
252 | |
253 | if (Found.isAmbiguous()) |
254 | return nullptr; |
255 | |
256 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
257 | QualType T = Context.getTypeDeclType(Type); |
258 | MarkAnyDeclReferenced(Type->getLocation(), Type, ); |
259 | |
260 | if (SearchType.isNull() || SearchType->isDependentType() || |
261 | Context.hasSameUnqualifiedType(T, SearchType)) { |
262 | |
263 | |
264 | return CreateParsedType(T, |
265 | Context.getTrivialTypeSourceInfo(T, NameLoc)); |
266 | } |
267 | |
268 | if (!SearchType.isNull()) |
269 | NonMatchingTypeDecl = Type; |
270 | } |
271 | |
272 | |
273 | |
274 | |
275 | |
276 | |
277 | |
278 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { |
279 | QualType MemberOfType; |
280 | if (SS.isSet()) { |
281 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { |
282 | |
283 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) |
284 | MemberOfType = Context.getTypeDeclType(Record); |
285 | } |
286 | } |
287 | if (MemberOfType.isNull()) |
288 | MemberOfType = SearchType; |
289 | |
290 | if (MemberOfType.isNull()) |
291 | continue; |
292 | |
293 | |
294 | |
295 | |
296 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { |
297 | if (ClassTemplateSpecializationDecl *Spec |
298 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
299 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == |
300 | Template->getCanonicalDecl()) |
301 | return CreateParsedType( |
302 | MemberOfType, |
303 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
304 | } |
305 | |
306 | continue; |
307 | } |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | |
314 | if (const TemplateSpecializationType *SpecType |
315 | = MemberOfType->getAs<TemplateSpecializationType>()) { |
316 | TemplateName SpecName = SpecType->getTemplateName(); |
317 | |
318 | |
319 | |
320 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { |
321 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) |
322 | return CreateParsedType( |
323 | MemberOfType, |
324 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
325 | |
326 | continue; |
327 | } |
328 | |
329 | |
330 | |
331 | if (DependentTemplateName *DepTemplate |
332 | = SpecName.getAsDependentTemplateName()) { |
333 | if (DepTemplate->isIdentifier() && |
334 | DepTemplate->getIdentifier() == Template->getIdentifier()) |
335 | return CreateParsedType( |
336 | MemberOfType, |
337 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
338 | |
339 | continue; |
340 | } |
341 | } |
342 | } |
343 | } |
344 | |
345 | if (isDependent) { |
346 | |
347 | |
348 | |
349 | |
350 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), |
351 | SS.getWithLocInContext(Context), |
352 | II, NameLoc); |
353 | return ParsedType::make(T); |
354 | } |
355 | |
356 | if (NonMatchingTypeDecl) { |
357 | QualType T = Context.getTypeDeclType(NonMatchingTypeDecl); |
358 | Diag(NameLoc, diag::err_destructor_expr_type_mismatch) |
359 | << T << SearchType; |
360 | Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here) |
361 | << T; |
362 | } else if (ObjectTypePtr) |
363 | Diag(NameLoc, diag::err_ident_in_dtor_not_a_type) |
364 | << &II; |
365 | else { |
366 | SemaDiagnosticBuilder DtorDiag = Diag(NameLoc, |
367 | diag::err_destructor_class_name); |
368 | if (S) { |
369 | const DeclContext *Ctx = S->getEntity(); |
370 | if (const CXXRecordDecl *Class = dyn_cast_or_null<CXXRecordDecl>(Ctx)) |
371 | DtorDiag << FixItHint::CreateReplacement(SourceRange(NameLoc), |
372 | Class->getNameAsString()); |
373 | } |
374 | } |
375 | |
376 | return nullptr; |
377 | } |
378 | |
379 | ParsedType Sema::getDestructorTypeForDecltype(const DeclSpec &DS, |
380 | ParsedType ObjectType) { |
381 | if (DS.getTypeSpecType() == DeclSpec::TST_error) |
382 | return nullptr; |
383 | |
384 | if (DS.getTypeSpecType() == DeclSpec::TST_decltype_auto) { |
385 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decltype_auto_invalid); |
386 | return nullptr; |
387 | } |
388 | |
389 | (0) . __assert_fail ("DS.getTypeSpecType() == DeclSpec..TST_decltype && \"unexpected type in getDestructorType\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 390, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DS.getTypeSpecType() == DeclSpec::TST_decltype && |
390 | (0) . __assert_fail ("DS.getTypeSpecType() == DeclSpec..TST_decltype && \"unexpected type in getDestructorType\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 390, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "unexpected type in getDestructorType"); |
391 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
392 | |
393 | |
394 | |
395 | QualType SearchType = GetTypeFromParser(ObjectType); |
396 | if (!SearchType.isNull() && !SearchType->isDependentType() && |
397 | !Context.hasSameUnqualifiedType(T, SearchType)) { |
398 | Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch) |
399 | << T << SearchType; |
400 | return nullptr; |
401 | } |
402 | |
403 | return ParsedType::make(T); |
404 | } |
405 | |
406 | bool Sema::checkLiteralOperatorId(const CXXScopeSpec &SS, |
407 | const UnqualifiedId &Name) { |
408 | assert(Name.getKind() == UnqualifiedIdKind::IK_LiteralOperatorId); |
409 | |
410 | if (!SS.isValid()) |
411 | return false; |
412 | |
413 | switch (SS.getScopeRep()->getKind()) { |
414 | case NestedNameSpecifier::Identifier: |
415 | case NestedNameSpecifier::TypeSpec: |
416 | case NestedNameSpecifier::TypeSpecWithTemplate: |
417 | |
418 | |
419 | |
420 | |
421 | Diag(Name.getBeginLoc(), diag::err_literal_operator_id_outside_namespace) |
422 | << SS.getScopeRep(); |
423 | return true; |
424 | |
425 | case NestedNameSpecifier::Global: |
426 | case NestedNameSpecifier::Super: |
427 | case NestedNameSpecifier::Namespace: |
428 | case NestedNameSpecifier::NamespaceAlias: |
429 | return false; |
430 | } |
431 | |
432 | llvm_unreachable("unknown nested name specifier kind"); |
433 | } |
434 | |
435 | |
436 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
437 | SourceLocation TypeidLoc, |
438 | TypeSourceInfo *Operand, |
439 | SourceLocation RParenLoc) { |
440 | |
441 | |
442 | |
443 | |
444 | |
445 | Qualifiers Quals; |
446 | QualType T |
447 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), |
448 | Quals); |
449 | if (T->getAs<RecordType>() && |
450 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
451 | return ExprError(); |
452 | |
453 | if (T->isVariablyModifiedType()) |
454 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) << T); |
455 | |
456 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), Operand, |
457 | SourceRange(TypeidLoc, RParenLoc)); |
458 | } |
459 | |
460 | |
461 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
462 | SourceLocation TypeidLoc, |
463 | Expr *E, |
464 | SourceLocation RParenLoc) { |
465 | bool WasEvaluated = false; |
466 | if (E && !E->isTypeDependent()) { |
467 | if (E->getType()->isPlaceholderType()) { |
468 | ExprResult result = CheckPlaceholderExpr(E); |
469 | if (result.isInvalid()) return ExprError(); |
470 | E = result.get(); |
471 | } |
472 | |
473 | QualType T = E->getType(); |
474 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
475 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
476 | |
477 | |
478 | |
479 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
480 | return ExprError(); |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | if (RecordD->isPolymorphic() && E->isGLValue()) { |
487 | |
488 | |
489 | ExprResult Result = TransformToPotentiallyEvaluated(E); |
490 | if (Result.isInvalid()) return ExprError(); |
491 | E = Result.get(); |
492 | |
493 | |
494 | MarkVTableUsed(TypeidLoc, RecordD); |
495 | WasEvaluated = true; |
496 | } |
497 | } |
498 | |
499 | |
500 | |
501 | |
502 | |
503 | |
504 | Qualifiers Quals; |
505 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
506 | if (!Context.hasSameType(T, UnqualT)) { |
507 | T = UnqualT; |
508 | E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).get(); |
509 | } |
510 | } |
511 | |
512 | if (E->getType()->isVariablyModifiedType()) |
513 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) |
514 | << E->getType()); |
515 | else if (!inTemplateInstantiation() && |
516 | E->HasSideEffects(Context, WasEvaluated)) { |
517 | |
518 | |
519 | Diag(E->getExprLoc(), WasEvaluated |
520 | ? diag::warn_side_effects_typeid |
521 | : diag::warn_side_effects_unevaluated_context); |
522 | } |
523 | |
524 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), E, |
525 | SourceRange(TypeidLoc, RParenLoc)); |
526 | } |
527 | |
528 | |
529 | ExprResult |
530 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
531 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
532 | |
533 | if (getLangOpts().OpenCLCPlusPlus) { |
534 | return ExprError(Diag(OpLoc, diag::err_openclcxx_not_supported) |
535 | << "typeid"); |
536 | } |
537 | |
538 | |
539 | if (!getStdNamespace()) |
540 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
541 | |
542 | if (!CXXTypeInfoDecl) { |
543 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
544 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
545 | LookupQualifiedName(R, getStdNamespace()); |
546 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
547 | |
548 | |
549 | if (!CXXTypeInfoDecl && LangOpts.MSVCCompat) { |
550 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
551 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
552 | } |
553 | if (!CXXTypeInfoDecl) |
554 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
555 | } |
556 | |
557 | if (!getLangOpts().RTTI) { |
558 | return ExprError(Diag(OpLoc, diag::err_no_typeid_with_fno_rtti)); |
559 | } |
560 | |
561 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
562 | |
563 | if (isType) { |
564 | |
565 | TypeSourceInfo *TInfo = nullptr; |
566 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
567 | &TInfo); |
568 | if (T.isNull()) |
569 | return ExprError(); |
570 | |
571 | if (!TInfo) |
572 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
573 | |
574 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
575 | } |
576 | |
577 | |
578 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
579 | } |
580 | |
581 | |
582 | |
583 | static void |
584 | getUuidAttrOfType(Sema &SemaRef, QualType QT, |
585 | llvm::SmallSetVector<const UuidAttr *, 1> &UuidAttrs) { |
586 | |
587 | const Type *Ty = QT.getTypePtr(); |
588 | if (QT->isPointerType() || QT->isReferenceType()) |
589 | Ty = QT->getPointeeType().getTypePtr(); |
590 | else if (QT->isArrayType()) |
591 | Ty = Ty->getBaseElementTypeUnsafe(); |
592 | |
593 | const auto *TD = Ty->getAsTagDecl(); |
594 | if (!TD) |
595 | return; |
596 | |
597 | if (const auto *Uuid = TD->getMostRecentDecl()->getAttr<UuidAttr>()) { |
598 | UuidAttrs.insert(Uuid); |
599 | return; |
600 | } |
601 | |
602 | |
603 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(TD)) { |
604 | const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); |
605 | for (const TemplateArgument &TA : TAL.asArray()) { |
606 | const UuidAttr *UuidForTA = nullptr; |
607 | if (TA.getKind() == TemplateArgument::Type) |
608 | getUuidAttrOfType(SemaRef, TA.getAsType(), UuidAttrs); |
609 | else if (TA.getKind() == TemplateArgument::Declaration) |
610 | getUuidAttrOfType(SemaRef, TA.getAsDecl()->getType(), UuidAttrs); |
611 | |
612 | if (UuidForTA) |
613 | UuidAttrs.insert(UuidForTA); |
614 | } |
615 | } |
616 | } |
617 | |
618 | |
619 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
620 | SourceLocation TypeidLoc, |
621 | TypeSourceInfo *Operand, |
622 | SourceLocation RParenLoc) { |
623 | StringRef UuidStr; |
624 | if (!Operand->getType()->isDependentType()) { |
625 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
626 | getUuidAttrOfType(*this, Operand->getType(), UuidAttrs); |
627 | if (UuidAttrs.empty()) |
628 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
629 | if (UuidAttrs.size() > 1) |
630 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
631 | UuidStr = UuidAttrs.back()->getGuid(); |
632 | } |
633 | |
634 | return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), Operand, UuidStr, |
635 | SourceRange(TypeidLoc, RParenLoc)); |
636 | } |
637 | |
638 | |
639 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
640 | SourceLocation TypeidLoc, |
641 | Expr *E, |
642 | SourceLocation RParenLoc) { |
643 | StringRef UuidStr; |
644 | if (!E->getType()->isDependentType()) { |
645 | if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
646 | UuidStr = "00000000-0000-0000-0000-000000000000"; |
647 | } else { |
648 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
649 | getUuidAttrOfType(*this, E->getType(), UuidAttrs); |
650 | if (UuidAttrs.empty()) |
651 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
652 | if (UuidAttrs.size() > 1) |
653 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
654 | UuidStr = UuidAttrs.back()->getGuid(); |
655 | } |
656 | } |
657 | |
658 | return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), E, UuidStr, |
659 | SourceRange(TypeidLoc, RParenLoc)); |
660 | } |
661 | |
662 | |
663 | ExprResult |
664 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
665 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
666 | |
667 | if (!MSVCGuidDecl) { |
668 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
669 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
670 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
671 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
672 | if (!MSVCGuidDecl) |
673 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
674 | } |
675 | |
676 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
677 | |
678 | if (isType) { |
679 | |
680 | TypeSourceInfo *TInfo = nullptr; |
681 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
682 | &TInfo); |
683 | if (T.isNull()) |
684 | return ExprError(); |
685 | |
686 | if (!TInfo) |
687 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
688 | |
689 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
690 | } |
691 | |
692 | |
693 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
694 | } |
695 | |
696 | |
697 | ExprResult |
698 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
699 | (0) . __assert_fail ("(Kind == tok..kw_true || Kind == tok..kw_false) && \"Unknown C++ Boolean value!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 700, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
700 | (0) . __assert_fail ("(Kind == tok..kw_true || Kind == tok..kw_false) && \"Unknown C++ Boolean value!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 700, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Unknown C++ Boolean value!"); |
701 | return new (Context) |
702 | CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
703 | } |
704 | |
705 | |
706 | ExprResult |
707 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
708 | return new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
709 | } |
710 | |
711 | |
712 | ExprResult |
713 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { |
714 | bool IsThrownVarInScope = false; |
715 | if (Ex) { |
716 | |
717 | |
718 | |
719 | |
720 | |
721 | |
722 | |
723 | |
724 | |
725 | |
726 | |
727 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens())) |
728 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
729 | if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) { |
730 | for( ; S; S = S->getParent()) { |
731 | if (S->isDeclScope(Var)) { |
732 | IsThrownVarInScope = true; |
733 | break; |
734 | } |
735 | |
736 | if (S->getFlags() & |
737 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | |
738 | Scope::FunctionPrototypeScope | Scope::ObjCMethodScope | |
739 | Scope::TryScope)) |
740 | break; |
741 | } |
742 | } |
743 | } |
744 | } |
745 | |
746 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); |
747 | } |
748 | |
749 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, |
750 | bool IsThrownVarInScope) { |
751 | |
752 | if (!getLangOpts().CXXExceptions && |
753 | !getSourceManager().isInSystemHeader(OpLoc) && !getLangOpts().CUDA) { |
754 | |
755 | targetDiag(OpLoc, diag::err_exceptions_disabled) << "throw"; |
756 | } |
757 | |
758 | |
759 | if (getLangOpts().CUDA) |
760 | CUDADiagIfDeviceCode(OpLoc, diag::err_cuda_device_exceptions) |
761 | << "throw" << CurrentCUDATarget(); |
762 | |
763 | if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope()) |
764 | Diag(OpLoc, diag::err_omp_simd_region_cannot_use_stmt) << "throw"; |
765 | |
766 | if (Ex && !Ex->isTypeDependent()) { |
767 | QualType ExceptionObjectTy = Context.getExceptionObjectType(Ex->getType()); |
768 | if (CheckCXXThrowOperand(OpLoc, ExceptionObjectTy, Ex)) |
769 | return ExprError(); |
770 | |
771 | |
772 | |
773 | |
774 | |
775 | |
776 | |
777 | |
778 | |
779 | |
780 | |
781 | |
782 | |
783 | |
784 | |
785 | |
786 | const VarDecl *NRVOVariable = nullptr; |
787 | if (IsThrownVarInScope) |
788 | NRVOVariable = getCopyElisionCandidate(QualType(), Ex, CES_Strict); |
789 | |
790 | InitializedEntity Entity = InitializedEntity::InitializeException( |
791 | OpLoc, ExceptionObjectTy, |
792 | != nullptr); |
793 | ExprResult Res = PerformMoveOrCopyInitialization( |
794 | Entity, NRVOVariable, QualType(), Ex, IsThrownVarInScope); |
795 | if (Res.isInvalid()) |
796 | return ExprError(); |
797 | Ex = Res.get(); |
798 | } |
799 | |
800 | return new (Context) |
801 | CXXThrowExpr(Ex, Context.VoidTy, OpLoc, IsThrownVarInScope); |
802 | } |
803 | |
804 | static void |
805 | collectPublicBases(CXXRecordDecl *RD, |
806 | llvm::DenseMap<CXXRecordDecl *, unsigned> &SubobjectsSeen, |
807 | llvm::SmallPtrSetImpl<CXXRecordDecl *> &VBases, |
808 | llvm::SetVector<CXXRecordDecl *> &PublicSubobjectsSeen, |
809 | bool ParentIsPublic) { |
810 | for (const CXXBaseSpecifier &BS : RD->bases()) { |
811 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
812 | bool NewSubobject; |
813 | |
814 | |
815 | if (BS.isVirtual()) |
816 | NewSubobject = VBases.insert(BaseDecl).second; |
817 | else |
818 | NewSubobject = true; |
819 | |
820 | if (NewSubobject) |
821 | ++SubobjectsSeen[BaseDecl]; |
822 | |
823 | |
824 | bool PublicPath = ParentIsPublic && BS.getAccessSpecifier() == AS_public; |
825 | if (PublicPath) |
826 | PublicSubobjectsSeen.insert(BaseDecl); |
827 | |
828 | |
829 | collectPublicBases(BaseDecl, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
830 | PublicPath); |
831 | } |
832 | } |
833 | |
834 | static void getUnambiguousPublicSubobjects( |
835 | CXXRecordDecl *RD, llvm::SmallVectorImpl<CXXRecordDecl *> &Objects) { |
836 | llvm::DenseMap<CXXRecordDecl *, unsigned> SubobjectsSeen; |
837 | llvm::SmallSet<CXXRecordDecl *, 2> VBases; |
838 | llvm::SetVector<CXXRecordDecl *> PublicSubobjectsSeen; |
839 | SubobjectsSeen[RD] = 1; |
840 | PublicSubobjectsSeen.insert(RD); |
841 | collectPublicBases(RD, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
842 | ); |
843 | |
844 | for (CXXRecordDecl *PublicSubobject : PublicSubobjectsSeen) { |
845 | |
846 | if (SubobjectsSeen[PublicSubobject] > 1) |
847 | continue; |
848 | |
849 | Objects.push_back(PublicSubobject); |
850 | } |
851 | } |
852 | |
853 | |
854 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, |
855 | QualType ExceptionObjectTy, Expr *E) { |
856 | |
857 | |
858 | QualType Ty = ExceptionObjectTy; |
859 | bool isPointer = false; |
860 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
861 | Ty = Ptr->getPointeeType(); |
862 | isPointer = true; |
863 | } |
864 | if (!isPointer || !Ty->isVoidType()) { |
865 | if (RequireCompleteType(ThrowLoc, Ty, |
866 | isPointer ? diag::err_throw_incomplete_ptr |
867 | : diag::err_throw_incomplete, |
868 | E->getSourceRange())) |
869 | return true; |
870 | |
871 | if (RequireNonAbstractType(ThrowLoc, ExceptionObjectTy, |
872 | diag::err_throw_abstract_type, E)) |
873 | return true; |
874 | } |
875 | |
876 | |
877 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
878 | if (!RD) |
879 | return false; |
880 | |
881 | |
882 | |
883 | MarkVTableUsed(ThrowLoc, RD); |
884 | |
885 | |
886 | if (isPointer) |
887 | return false; |
888 | |
889 | |
890 | if (!RD->hasIrrelevantDestructor()) { |
891 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
892 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
893 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
894 | PDiag(diag::err_access_dtor_exception) << Ty); |
895 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
896 | return true; |
897 | } |
898 | } |
899 | |
900 | |
901 | |
902 | |
903 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
904 | |
905 | |
906 | |
907 | llvm::SmallVector<CXXRecordDecl *, 2> UnambiguousPublicSubobjects; |
908 | getUnambiguousPublicSubobjects(RD, UnambiguousPublicSubobjects); |
909 | |
910 | for (CXXRecordDecl *Subobject : UnambiguousPublicSubobjects) { |
911 | |
912 | |
913 | |
914 | |
915 | CXXConstructorDecl *CD = LookupCopyingConstructor(Subobject, 0); |
916 | if (!CD) |
917 | continue; |
918 | |
919 | |
920 | MarkFunctionReferenced(E->getExprLoc(), CD); |
921 | |
922 | |
923 | |
924 | if (CD->isTrivial()) |
925 | continue; |
926 | |
927 | |
928 | |
929 | |
930 | |
931 | |
932 | |
933 | Context.addCopyConstructorForExceptionObject(Subobject, CD); |
934 | |
935 | |
936 | |
937 | for (unsigned I = 1, E = CD->getNumParams(); I != E; ++I) { |
938 | if (CheckCXXDefaultArgExpr(ThrowLoc, CD, CD->getParamDecl(I))) |
939 | return true; |
940 | } |
941 | } |
942 | } |
943 | |
944 | return false; |
945 | } |
946 | |
947 | static QualType adjustCVQualifiersForCXXThisWithinLambda( |
948 | ArrayRef<FunctionScopeInfo *> FunctionScopes, QualType ThisTy, |
949 | DeclContext *CurSemaContext, ASTContext &ASTCtx) { |
950 | |
951 | QualType ClassType = ThisTy->getPointeeType(); |
952 | LambdaScopeInfo *CurLSI = nullptr; |
953 | DeclContext *CurDC = CurSemaContext; |
954 | |
955 | |
956 | |
957 | |
958 | |
959 | |
960 | |
961 | |
962 | |
963 | |
964 | |
965 | |
966 | |
967 | |
968 | |
969 | |
970 | |
971 | |
972 | |
973 | |
974 | |
975 | |
976 | |
977 | |
978 | |
979 | |
980 | |
981 | |
982 | |
983 | |
984 | |
985 | |
986 | |
987 | |
988 | |
989 | |
990 | |
991 | for (int I = FunctionScopes.size(); |
992 | I-- && isa<LambdaScopeInfo>(FunctionScopes[I]) && |
993 | (!CurLSI || !CurLSI->Lambda || CurLSI->Lambda->getDeclContext() == |
994 | cast<LambdaScopeInfo>(FunctionScopes[I])->CallOperator); |
995 | CurDC = getLambdaAwareParentOfDeclContext(CurDC)) { |
996 | CurLSI = cast<LambdaScopeInfo>(FunctionScopes[I]); |
997 | |
998 | if (!CurLSI->isCXXThisCaptured()) |
999 | continue; |
1000 | |
1001 | auto C = CurLSI->getCXXThisCapture(); |
1002 | |
1003 | if (C.isCopyCapture()) { |
1004 | ClassType.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
1005 | if (CurLSI->CallOperator->isConst()) |
1006 | ClassType.addConst(); |
1007 | return ASTCtx.getPointerType(ClassType); |
1008 | } |
1009 | } |
1010 | |
1011 | |
1012 | |
1013 | if (isLambdaCallOperator(CurDC)) { |
1014 | (0) . __assert_fail ("CurLSI && \"While computing 'this' capture-type for a generic \" \"lambda, we must have a corresponding LambdaScopeInfo\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1015, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurLSI && "While computing 'this' capture-type for a generic " |
1015 | (0) . __assert_fail ("CurLSI && \"While computing 'this' capture-type for a generic \" \"lambda, we must have a corresponding LambdaScopeInfo\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1015, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "lambda, we must have a corresponding LambdaScopeInfo"); |
1016 | (0) . __assert_fail ("isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator) && \"While computing 'this' capture-type for a generic lambda, when we \" \"run out of enclosing LSI's, yet the enclosing DC is a \" \"lambda-call-operator we must be (i.e. Current LSI) in a generic \" \"lambda call oeprator\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1020, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator) && |
1017 | (0) . __assert_fail ("isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator) && \"While computing 'this' capture-type for a generic lambda, when we \" \"run out of enclosing LSI's, yet the enclosing DC is a \" \"lambda-call-operator we must be (i.e. Current LSI) in a generic \" \"lambda call oeprator\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1020, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "While computing 'this' capture-type for a generic lambda, when we " |
1018 | (0) . __assert_fail ("isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator) && \"While computing 'this' capture-type for a generic lambda, when we \" \"run out of enclosing LSI's, yet the enclosing DC is a \" \"lambda-call-operator we must be (i.e. Current LSI) in a generic \" \"lambda call oeprator\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1020, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "run out of enclosing LSI's, yet the enclosing DC is a " |
1019 | (0) . __assert_fail ("isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator) && \"While computing 'this' capture-type for a generic lambda, when we \" \"run out of enclosing LSI's, yet the enclosing DC is a \" \"lambda-call-operator we must be (i.e. Current LSI) in a generic \" \"lambda call oeprator\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1020, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "lambda-call-operator we must be (i.e. Current LSI) in a generic " |
1020 | (0) . __assert_fail ("isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator) && \"While computing 'this' capture-type for a generic lambda, when we \" \"run out of enclosing LSI's, yet the enclosing DC is a \" \"lambda-call-operator we must be (i.e. Current LSI) in a generic \" \"lambda call oeprator\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1020, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "lambda call oeprator"); |
1021 | CallOperator)", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1021, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurDC == getLambdaAwareParentOfDeclContext(CurLSI->CallOperator)); |
1022 | |
1023 | auto IsThisCaptured = |
1024 | [](CXXRecordDecl *Closure, bool &IsByCopy, bool &IsConst) { |
1025 | IsConst = false; |
1026 | IsByCopy = false; |
1027 | for (auto &&C : Closure->captures()) { |
1028 | if (C.capturesThis()) { |
1029 | if (C.getCaptureKind() == LCK_StarThis) |
1030 | IsByCopy = true; |
1031 | if (Closure->getLambdaCallOperator()->isConst()) |
1032 | IsConst = true; |
1033 | return true; |
1034 | } |
1035 | } |
1036 | return false; |
1037 | }; |
1038 | |
1039 | bool IsByCopyCapture = false; |
1040 | bool IsConstCapture = false; |
1041 | CXXRecordDecl *Closure = cast<CXXRecordDecl>(CurDC->getParent()); |
1042 | while (Closure && |
1043 | IsThisCaptured(Closure, IsByCopyCapture, IsConstCapture)) { |
1044 | if (IsByCopyCapture) { |
1045 | ClassType.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
1046 | if (IsConstCapture) |
1047 | ClassType.addConst(); |
1048 | return ASTCtx.getPointerType(ClassType); |
1049 | } |
1050 | Closure = isLambdaCallOperator(Closure->getParent()) |
1051 | ? cast<CXXRecordDecl>(Closure->getParent()->getParent()) |
1052 | : nullptr; |
1053 | } |
1054 | } |
1055 | return ASTCtx.getPointerType(ClassType); |
1056 | } |
1057 | |
1058 | QualType Sema::getCurrentThisType() { |
1059 | DeclContext *DC = getFunctionLevelDeclContext(); |
1060 | QualType ThisTy = CXXThisTypeOverride; |
1061 | |
1062 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) { |
1063 | if (method && method->isInstance()) |
1064 | ThisTy = method->getThisType(); |
1065 | } |
1066 | |
1067 | if (ThisTy.isNull() && isLambdaCallOperator(CurContext) && |
1068 | inTemplateInstantiation()) { |
1069 | |
1070 | (0) . __assert_fail ("isa(DC) && \"Trying to get 'this' type from static method?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1071, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<CXXRecordDecl>(DC) && |
1071 | (0) . __assert_fail ("isa(DC) && \"Trying to get 'this' type from static method?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1071, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Trying to get 'this' type from static method?"); |
1072 | |
1073 | |
1074 | |
1075 | |
1076 | |
1077 | QualType ClassTy = Context.getTypeDeclType(cast<CXXRecordDecl>(DC)); |
1078 | |
1079 | |
1080 | ThisTy = Context.getPointerType(ClassTy); |
1081 | } |
1082 | |
1083 | |
1084 | |
1085 | |
1086 | if (!ThisTy.isNull() && isLambdaCallOperator(CurContext)) |
1087 | return adjustCVQualifiersForCXXThisWithinLambda(FunctionScopes, ThisTy, |
1088 | CurContext, Context); |
1089 | return ThisTy; |
1090 | } |
1091 | |
1092 | Sema::CXXThisScopeRAII::CXXThisScopeRAII(Sema &S, |
1093 | Decl *ContextDecl, |
1094 | Qualifiers CXXThisTypeQuals, |
1095 | bool Enabled) |
1096 | : S(S), OldCXXThisTypeOverride(S.CXXThisTypeOverride), Enabled(false) |
1097 | { |
1098 | if (!Enabled || !ContextDecl) |
1099 | return; |
1100 | |
1101 | CXXRecordDecl *Record = nullptr; |
1102 | if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(ContextDecl)) |
1103 | Record = Template->getTemplatedDecl(); |
1104 | else |
1105 | Record = cast<CXXRecordDecl>(ContextDecl); |
1106 | |
1107 | QualType T = S.Context.getRecordType(Record); |
1108 | T = S.getASTContext().getQualifiedType(T, CXXThisTypeQuals); |
1109 | |
1110 | S.CXXThisTypeOverride = S.Context.getPointerType(T); |
1111 | |
1112 | this->Enabled = true; |
1113 | } |
1114 | |
1115 | |
1116 | Sema::CXXThisScopeRAII::~CXXThisScopeRAII() { |
1117 | if (Enabled) { |
1118 | S.CXXThisTypeOverride = OldCXXThisTypeOverride; |
1119 | } |
1120 | } |
1121 | |
1122 | static Expr *captureThis(Sema &S, ASTContext &Context, RecordDecl *RD, |
1123 | QualType ThisTy, SourceLocation Loc, |
1124 | const bool ByCopy) { |
1125 | |
1126 | QualType AdjustedThisTy = ThisTy; |
1127 | |
1128 | |
1129 | QualType CaptureThisFieldTy = ThisTy; |
1130 | if (ByCopy) { |
1131 | |
1132 | |
1133 | |
1134 | CaptureThisFieldTy = ThisTy->getPointeeType(); |
1135 | CaptureThisFieldTy.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
1136 | AdjustedThisTy = Context.getPointerType(CaptureThisFieldTy); |
1137 | } |
1138 | |
1139 | FieldDecl *Field = FieldDecl::Create( |
1140 | Context, RD, Loc, Loc, nullptr, CaptureThisFieldTy, |
1141 | Context.getTrivialTypeSourceInfo(CaptureThisFieldTy, Loc), nullptr, false, |
1142 | ICIS_NoInit); |
1143 | |
1144 | Field->setImplicit(true); |
1145 | Field->setAccess(AS_private); |
1146 | RD->addDecl(Field); |
1147 | Expr *This = |
1148 | new (Context) CXXThisExpr(Loc, ThisTy, true); |
1149 | if (ByCopy) { |
1150 | Expr *StarThis = S.CreateBuiltinUnaryOp(Loc, |
1151 | UO_Deref, |
1152 | This).get(); |
1153 | InitializedEntity Entity = InitializedEntity::InitializeLambdaCapture( |
1154 | nullptr, CaptureThisFieldTy, Loc); |
1155 | InitializationKind InitKind = InitializationKind::CreateDirect(Loc, Loc, Loc); |
1156 | InitializationSequence Init(S, Entity, InitKind, StarThis); |
1157 | ExprResult ER = Init.Perform(S, Entity, InitKind, StarThis); |
1158 | if (ER.isInvalid()) return nullptr; |
1159 | return ER.get(); |
1160 | } |
1161 | return This; |
1162 | } |
1163 | |
1164 | bool Sema::CheckCXXThisCapture(SourceLocation Loc, const bool Explicit, |
1165 | bool BuildAndDiagnose, const unsigned *const FunctionScopeIndexToStopAt, |
1166 | const bool ByCopy) { |
1167 | |
1168 | if (isUnevaluatedContext() && !Explicit) |
1169 | return true; |
1170 | |
1171 | (0) . __assert_fail ("(!ByCopy || Explicit) && \"cannot implicitly capture *this by value\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1171, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!ByCopy || Explicit) && "cannot implicitly capture *this by value"); |
1172 | |
1173 | const int MaxFunctionScopesIndex = FunctionScopeIndexToStopAt |
1174 | ? *FunctionScopeIndexToStopAt |
1175 | : FunctionScopes.size() - 1; |
1176 | |
1177 | |
1178 | |
1179 | |
1180 | |
1181 | |
1182 | |
1183 | |
1184 | |
1185 | |
1186 | |
1187 | |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | |
1198 | |
1199 | |
1200 | unsigned NumCapturingClosures = 0; |
1201 | for (int idx = MaxFunctionScopesIndex; idx >= 0; idx--) { |
1202 | if (CapturingScopeInfo *CSI = |
1203 | dyn_cast<CapturingScopeInfo>(FunctionScopes[idx])) { |
1204 | if (CSI->CXXThisCaptureIndex != 0) { |
1205 | |
1206 | CSI->Captures[CSI->CXXThisCaptureIndex - 1].markUsed(BuildAndDiagnose); |
1207 | break; |
1208 | } |
1209 | LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI); |
1210 | if (LSI && isGenericLambdaCallOperatorSpecialization(LSI->CallOperator)) { |
1211 | |
1212 | if (BuildAndDiagnose) |
1213 | Diag(Loc, diag::err_this_capture) |
1214 | << (Explicit && idx == MaxFunctionScopesIndex); |
1215 | return true; |
1216 | } |
1217 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByref || |
1218 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByval || |
1219 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_Block || |
1220 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_CapturedRegion || |
1221 | (Explicit && idx == MaxFunctionScopesIndex)) { |
1222 | |
1223 | |
1224 | |
1225 | |
1226 | |
1227 | NumCapturingClosures++; |
1228 | continue; |
1229 | } |
1230 | |
1231 | if (BuildAndDiagnose) |
1232 | Diag(Loc, diag::err_this_capture) |
1233 | << (Explicit && idx == MaxFunctionScopesIndex); |
1234 | return true; |
1235 | } |
1236 | break; |
1237 | } |
1238 | if (!BuildAndDiagnose) return false; |
1239 | |
1240 | |
1241 | |
1242 | |
1243 | |
1244 | |
1245 | |
1246 | |
1247 | |
1248 | |
1249 | (0) . __assert_fail ("(!ByCopy || dyn_cast(FunctionScopes[MaxFunctionScopesIndex])) && \"Only a lambda can capture the enclosing object (referred to by \" \"*this) by copy\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1252, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!ByCopy || |
1250 | (0) . __assert_fail ("(!ByCopy || dyn_cast(FunctionScopes[MaxFunctionScopesIndex])) && \"Only a lambda can capture the enclosing object (referred to by \" \"*this) by copy\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1252, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> dyn_cast<LambdaScopeInfo>(FunctionScopes[MaxFunctionScopesIndex])) && |
1251 | (0) . __assert_fail ("(!ByCopy || dyn_cast(FunctionScopes[MaxFunctionScopesIndex])) && \"Only a lambda can capture the enclosing object (referred to by \" \"*this) by copy\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1252, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only a lambda can capture the enclosing object (referred to by " |
1252 | (0) . __assert_fail ("(!ByCopy || dyn_cast(FunctionScopes[MaxFunctionScopesIndex])) && \"Only a lambda can capture the enclosing object (referred to by \" \"*this) by copy\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1252, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "*this) by copy"); |
1253 | |
1254 | |
1255 | QualType ThisTy = getCurrentThisType(); |
1256 | for (int idx = MaxFunctionScopesIndex; NumCapturingClosures; |
1257 | --idx, --NumCapturingClosures) { |
1258 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[idx]); |
1259 | Expr *ThisExpr = nullptr; |
1260 | |
1261 | if (LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI)) { |
1262 | |
1263 | |
1264 | |
1265 | ThisExpr = captureThis(*this, Context, LSI->Lambda, ThisTy, Loc, |
1266 | ByCopy && idx == MaxFunctionScopesIndex); |
1267 | |
1268 | } else if (CapturedRegionScopeInfo *RSI |
1269 | = dyn_cast<CapturedRegionScopeInfo>(FunctionScopes[idx])) |
1270 | ThisExpr = |
1271 | captureThis(*this, Context, RSI->TheRecordDecl, ThisTy, Loc, |
1272 | false); |
1273 | |
1274 | bool isNested = NumCapturingClosures > 1; |
1275 | CSI->addThisCapture(isNested, Loc, ThisExpr, ByCopy); |
1276 | } |
1277 | return false; |
1278 | } |
1279 | |
1280 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { |
1281 | |
1282 | |
1283 | |
1284 | |
1285 | QualType ThisTy = getCurrentThisType(); |
1286 | if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); |
1287 | |
1288 | CheckCXXThisCapture(Loc); |
1289 | return new (Context) CXXThisExpr(Loc, ThisTy, ); |
1290 | } |
1291 | |
1292 | bool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) { |
1293 | |
1294 | |
1295 | if (CXXThisTypeOverride.isNull()) |
1296 | return false; |
1297 | |
1298 | |
1299 | |
1300 | CXXRecordDecl *Class = BaseType->getAsCXXRecordDecl(); |
1301 | return Class && Class->isBeingDefined(); |
1302 | } |
1303 | |
1304 | |
1305 | |
1306 | |
1307 | |
1308 | ExprResult |
1309 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
1310 | SourceLocation LParenOrBraceLoc, |
1311 | MultiExprArg exprs, |
1312 | SourceLocation RParenOrBraceLoc, |
1313 | bool ListInitialization) { |
1314 | if (!TypeRep) |
1315 | return ExprError(); |
1316 | |
1317 | TypeSourceInfo *TInfo; |
1318 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
1319 | if (!TInfo) |
1320 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
1321 | |
1322 | auto Result = BuildCXXTypeConstructExpr(TInfo, LParenOrBraceLoc, exprs, |
1323 | RParenOrBraceLoc, ListInitialization); |
1324 | |
1325 | |
1326 | |
1327 | if (!Result.isInvalid() && Result.get()->isInstantiationDependent() && |
1328 | !Result.get()->isTypeDependent()) |
1329 | Result = CorrectDelayedTyposInExpr(Result.get()); |
1330 | return Result; |
1331 | } |
1332 | |
1333 | ExprResult |
1334 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
1335 | SourceLocation LParenOrBraceLoc, |
1336 | MultiExprArg Exprs, |
1337 | SourceLocation RParenOrBraceLoc, |
1338 | bool ListInitialization) { |
1339 | QualType Ty = TInfo->getType(); |
1340 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
1341 | |
1342 | if (Ty->isDependentType() || CallExpr::hasAnyTypeDependentArguments(Exprs)) { |
1343 | |
1344 | |
1345 | SourceRange Locs = ListInitialization |
1346 | ? SourceRange() |
1347 | : SourceRange(LParenOrBraceLoc, RParenOrBraceLoc); |
1348 | return CXXUnresolvedConstructExpr::Create(Context, TInfo, Locs.getBegin(), |
1349 | Exprs, Locs.getEnd()); |
1350 | } |
1351 | |
1352 | (0) . __assert_fail ("(!ListInitialization || (Exprs.size() == 1 && isa(Exprs[0]))) && \"List initialization must have initializer list as expression.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1354, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!ListInitialization || |
1353 | (0) . __assert_fail ("(!ListInitialization || (Exprs.size() == 1 && isa(Exprs[0]))) && \"List initialization must have initializer list as expression.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1354, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (Exprs.size() == 1 && isa<InitListExpr>(Exprs[0]))) && |
1354 | (0) . __assert_fail ("(!ListInitialization || (Exprs.size() == 1 && isa(Exprs[0]))) && \"List initialization must have initializer list as expression.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1354, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "List initialization must have initializer list as expression."); |
1355 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenOrBraceLoc); |
1356 | |
1357 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
1358 | InitializationKind Kind = |
1359 | Exprs.size() |
1360 | ? ListInitialization |
1361 | ? InitializationKind::CreateDirectList( |
1362 | TyBeginLoc, LParenOrBraceLoc, RParenOrBraceLoc) |
1363 | : InitializationKind::CreateDirect(TyBeginLoc, LParenOrBraceLoc, |
1364 | RParenOrBraceLoc) |
1365 | : InitializationKind::CreateValue(TyBeginLoc, LParenOrBraceLoc, |
1366 | RParenOrBraceLoc); |
1367 | |
1368 | |
1369 | |
1370 | |
1371 | DeducedType *Deduced = Ty->getContainedDeducedType(); |
1372 | if (Deduced && isa<DeducedTemplateSpecializationType>(Deduced)) { |
1373 | Ty = DeduceTemplateSpecializationFromInitializer(TInfo, Entity, |
1374 | Kind, Exprs); |
1375 | if (Ty.isNull()) |
1376 | return ExprError(); |
1377 | Entity = InitializedEntity::InitializeTemporary(TInfo, Ty); |
1378 | } |
1379 | |
1380 | |
1381 | |
1382 | |
1383 | |
1384 | if (Exprs.size() == 1 && !ListInitialization && |
1385 | !isa<InitListExpr>(Exprs[0])) { |
1386 | Expr *Arg = Exprs[0]; |
1387 | return BuildCXXFunctionalCastExpr(TInfo, Ty, LParenOrBraceLoc, Arg, |
1388 | RParenOrBraceLoc); |
1389 | } |
1390 | |
1391 | |
1392 | QualType ElemTy = Ty; |
1393 | if (Ty->isArrayType()) { |
1394 | if (!ListInitialization) |
1395 | return ExprError(Diag(TyBeginLoc, diag::err_value_init_for_array_type) |
1396 | << FullRange); |
1397 | ElemTy = Context.getBaseElementType(Ty); |
1398 | } |
1399 | |
1400 | |
1401 | |
1402 | if (Ty->isFunctionType()) |
1403 | return ExprError(Diag(TyBeginLoc, diag::err_init_for_function_type) |
1404 | << Ty << FullRange); |
1405 | |
1406 | |
1407 | |
1408 | |
1409 | if (!Ty->isVoidType() && |
1410 | RequireCompleteType(TyBeginLoc, ElemTy, |
1411 | diag::err_invalid_incomplete_type_use, FullRange)) |
1412 | return ExprError(); |
1413 | |
1414 | |
1415 | |
1416 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs); |
1417 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Exprs); |
1418 | |
1419 | if (Result.isInvalid()) |
1420 | return Result; |
1421 | |
1422 | Expr *Inner = Result.get(); |
1423 | if (CXXBindTemporaryExpr *BTE = dyn_cast_or_null<CXXBindTemporaryExpr>(Inner)) |
1424 | Inner = BTE->getSubExpr(); |
1425 | if (!isa<CXXTemporaryObjectExpr>(Inner) && |
1426 | !isa<CXXScalarValueInitExpr>(Inner)) { |
1427 | |
1428 | |
1429 | |
1430 | |
1431 | |
1432 | |
1433 | |
1434 | |
1435 | QualType ResultType = Result.get()->getType(); |
1436 | SourceRange Locs = ListInitialization |
1437 | ? SourceRange() |
1438 | : SourceRange(LParenOrBraceLoc, RParenOrBraceLoc); |
1439 | Result = CXXFunctionalCastExpr::Create( |
1440 | Context, ResultType, Expr::getValueKindForType(Ty), TInfo, CK_NoOp, |
1441 | Result.get(), , Locs.getBegin(), Locs.getEnd()); |
1442 | } |
1443 | |
1444 | return Result; |
1445 | } |
1446 | |
1447 | bool Sema::isUsualDeallocationFunction(const CXXMethodDecl *Method) { |
1448 | |
1449 | const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext); |
1450 | if (getLangOpts().CUDA && |
1451 | IdentifyCUDAPreference(Caller, Method) <= CFP_WrongSide) |
1452 | return false; |
1453 | |
1454 | SmallVector<const FunctionDecl*, 4> PreventedBy; |
1455 | bool Result = Method->isUsualDeallocationFunction(PreventedBy); |
1456 | |
1457 | if (Result || !getLangOpts().CUDA || PreventedBy.empty()) |
1458 | return Result; |
1459 | |
1460 | |
1461 | |
1462 | return llvm::none_of(PreventedBy, [&](const FunctionDecl *FD) { |
1463 | (0) . __assert_fail ("FD->getNumParams() == 1 && \"Only single-operand functions should be in PreventedBy\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1464, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FD->getNumParams() == 1 && |
1464 | (0) . __assert_fail ("FD->getNumParams() == 1 && \"Only single-operand functions should be in PreventedBy\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1464, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only single-operand functions should be in PreventedBy"); |
1465 | return IdentifyCUDAPreference(Caller, FD) >= CFP_HostDevice; |
1466 | }); |
1467 | } |
1468 | |
1469 | |
1470 | |
1471 | static bool isNonPlacementDeallocationFunction(Sema &S, FunctionDecl *FD) { |
1472 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
1473 | return S.isUsualDeallocationFunction(Method); |
1474 | |
1475 | if (FD->getOverloadedOperator() != OO_Delete && |
1476 | FD->getOverloadedOperator() != OO_Array_Delete) |
1477 | return false; |
1478 | |
1479 | unsigned UsualParams = 1; |
1480 | |
1481 | if (S.getLangOpts().SizedDeallocation && UsualParams < FD->getNumParams() && |
1482 | S.Context.hasSameUnqualifiedType( |
1483 | FD->getParamDecl(UsualParams)->getType(), |
1484 | S.Context.getSizeType())) |
1485 | ++UsualParams; |
1486 | |
1487 | if (S.getLangOpts().AlignedAllocation && UsualParams < FD->getNumParams() && |
1488 | S.Context.hasSameUnqualifiedType( |
1489 | FD->getParamDecl(UsualParams)->getType(), |
1490 | S.Context.getTypeDeclType(S.getStdAlignValT()))) |
1491 | ++UsualParams; |
1492 | |
1493 | return UsualParams == FD->getNumParams(); |
1494 | } |
1495 | |
1496 | namespace { |
1497 | struct UsualDeallocFnInfo { |
1498 | UsualDeallocFnInfo() : Found(), FD(nullptr) {} |
1499 | UsualDeallocFnInfo(Sema &S, DeclAccessPair Found) |
1500 | : Found(Found), FD(dyn_cast<FunctionDecl>(Found->getUnderlyingDecl())), |
1501 | Destroying(false), HasSizeT(false), HasAlignValT(false), |
1502 | CUDAPref(Sema::CFP_Native) { |
1503 | |
1504 | if (!FD) |
1505 | return; |
1506 | unsigned NumBaseParams = 1; |
1507 | if (FD->isDestroyingOperatorDelete()) { |
1508 | Destroying = true; |
1509 | ++NumBaseParams; |
1510 | } |
1511 | |
1512 | if (NumBaseParams < FD->getNumParams() && |
1513 | S.Context.hasSameUnqualifiedType( |
1514 | FD->getParamDecl(NumBaseParams)->getType(), |
1515 | S.Context.getSizeType())) { |
1516 | ++NumBaseParams; |
1517 | HasSizeT = true; |
1518 | } |
1519 | |
1520 | if (NumBaseParams < FD->getNumParams() && |
1521 | FD->getParamDecl(NumBaseParams)->getType()->isAlignValT()) { |
1522 | ++NumBaseParams; |
1523 | HasAlignValT = true; |
1524 | } |
1525 | |
1526 | |
1527 | if (S.getLangOpts().CUDA) |
1528 | if (auto *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
1529 | CUDAPref = S.IdentifyCUDAPreference(Caller, FD); |
1530 | } |
1531 | |
1532 | explicit operator bool() const { return FD; } |
1533 | |
1534 | bool isBetterThan(const UsualDeallocFnInfo &Other, bool WantSize, |
1535 | bool WantAlign) const { |
1536 | |
1537 | |
1538 | |
1539 | if (Destroying != Other.Destroying) |
1540 | return Destroying; |
1541 | |
1542 | |
1543 | |
1544 | |
1545 | |
1546 | if (HasAlignValT != Other.HasAlignValT) |
1547 | return HasAlignValT == WantAlign; |
1548 | |
1549 | if (HasSizeT != Other.HasSizeT) |
1550 | return HasSizeT == WantSize; |
1551 | |
1552 | |
1553 | return CUDAPref > Other.CUDAPref; |
1554 | } |
1555 | |
1556 | DeclAccessPair Found; |
1557 | FunctionDecl *FD; |
1558 | bool Destroying, HasSizeT, HasAlignValT; |
1559 | Sema::CUDAFunctionPreference CUDAPref; |
1560 | }; |
1561 | } |
1562 | |
1563 | |
1564 | |
1565 | |
1566 | |
1567 | static bool hasNewExtendedAlignment(Sema &S, QualType AllocType) { |
1568 | return S.getLangOpts().AlignedAllocation && |
1569 | S.getASTContext().getTypeAlignIfKnown(AllocType) > |
1570 | S.getASTContext().getTargetInfo().getNewAlign(); |
1571 | } |
1572 | |
1573 | |
1574 | |
1575 | static UsualDeallocFnInfo resolveDeallocationOverload( |
1576 | Sema &S, LookupResult &R, bool WantSize, bool WantAlign, |
1577 | llvm::SmallVectorImpl<UsualDeallocFnInfo> *BestFns = nullptr) { |
1578 | UsualDeallocFnInfo Best; |
1579 | |
1580 | for (auto I = R.begin(), E = R.end(); I != E; ++I) { |
1581 | UsualDeallocFnInfo Info(S, I.getPair()); |
1582 | if (!Info || !isNonPlacementDeallocationFunction(S, Info.FD) || |
1583 | Info.CUDAPref == Sema::CFP_Never) |
1584 | continue; |
1585 | |
1586 | if (!Best) { |
1587 | Best = Info; |
1588 | if (BestFns) |
1589 | BestFns->push_back(Info); |
1590 | continue; |
1591 | } |
1592 | |
1593 | if (Best.isBetterThan(Info, WantSize, WantAlign)) |
1594 | continue; |
1595 | |
1596 | |
1597 | |
1598 | if (BestFns && Info.isBetterThan(Best, WantSize, WantAlign)) |
1599 | BestFns->clear(); |
1600 | |
1601 | Best = Info; |
1602 | if (BestFns) |
1603 | BestFns->push_back(Info); |
1604 | } |
1605 | |
1606 | return Best; |
1607 | } |
1608 | |
1609 | |
1610 | |
1611 | |
1612 | |
1613 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, |
1614 | QualType allocType) { |
1615 | const RecordType *record = |
1616 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
1617 | if (!record) return false; |
1618 | |
1619 | |
1620 | |
1621 | DeclarationName deleteName = |
1622 | S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
1623 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); |
1624 | S.LookupQualifiedName(ops, record->getDecl()); |
1625 | |
1626 | |
1627 | ops.suppressDiagnostics(); |
1628 | |
1629 | |
1630 | if (ops.empty()) return false; |
1631 | |
1632 | |
1633 | |
1634 | if (ops.isAmbiguous()) return false; |
1635 | |
1636 | |
1637 | |
1638 | |
1639 | auto Best = resolveDeallocationOverload( |
1640 | S, ops, , |
1641 | (S, allocType)); |
1642 | return Best && Best.HasSizeT; |
1643 | } |
1644 | |
1645 | |
1646 | |
1647 | |
1648 | |
1649 | |
1650 | |
1651 | |
1652 | |
1653 | |
1654 | |
1655 | |
1656 | |
1657 | |
1658 | |
1659 | |
1660 | |
1661 | ExprResult |
1662 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
1663 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
1664 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
1665 | Declarator &D, Expr *Initializer) { |
1666 | Expr *ArraySize = nullptr; |
1667 | |
1668 | if (D.getNumTypeObjects() > 0 && |
1669 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
1670 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
1671 | if (D.getDeclSpec().hasAutoTypeSpec()) |
1672 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) |
1673 | << D.getSourceRange()); |
1674 | if (Chunk.Arr.hasStatic) |
1675 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
1676 | << D.getSourceRange()); |
1677 | if (!Chunk.Arr.NumElts) |
1678 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
1679 | << D.getSourceRange()); |
1680 | |
1681 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
1682 | D.DropFirstTypeObject(); |
1683 | } |
1684 | |
1685 | |
1686 | if (ArraySize) { |
1687 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
1688 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
1689 | break; |
1690 | |
1691 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
1692 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
1693 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent()) { |
1694 | if (getLangOpts().CPlusPlus14) { |
1695 | |
1696 | |
1697 | |
1698 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
1699 | (0) . __assert_fail ("IntWidth && \"Builtin type of size 0?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1699, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IntWidth && "Builtin type of size 0?"); |
1700 | llvm::APSInt Value(IntWidth); |
1701 | Array.NumElts |
1702 | = CheckConvertedConstantExpression(NumElts, Context.getSizeType(), Value, |
1703 | CCEK_NewExpr) |
1704 | .get(); |
1705 | } else { |
1706 | Array.NumElts |
1707 | = VerifyIntegerConstantExpression(NumElts, nullptr, |
1708 | diag::err_new_array_nonconst) |
1709 | .get(); |
1710 | } |
1711 | if (!Array.NumElts) |
1712 | return ExprError(); |
1713 | } |
1714 | } |
1715 | } |
1716 | } |
1717 | |
1718 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, ); |
1719 | QualType AllocType = TInfo->getType(); |
1720 | if (D.isInvalidType()) |
1721 | return ExprError(); |
1722 | |
1723 | SourceRange DirectInitRange; |
1724 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) |
1725 | DirectInitRange = List->getSourceRange(); |
1726 | |
1727 | return BuildCXXNew(SourceRange(StartLoc, D.getEndLoc()), UseGlobal, |
1728 | PlacementLParen, PlacementArgs, PlacementRParen, |
1729 | TypeIdParens, AllocType, TInfo, ArraySize, DirectInitRange, |
1730 | Initializer); |
1731 | } |
1732 | |
1733 | static bool isLegalArrayNewInitializer(CXXNewExpr::InitializationStyle Style, |
1734 | Expr *Init) { |
1735 | if (!Init) |
1736 | return true; |
1737 | if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) |
1738 | return PLE->getNumExprs() == 0; |
1739 | if (isa<ImplicitValueInitExpr>(Init)) |
1740 | return true; |
1741 | else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) |
1742 | return !CCE->isListInitialization() && |
1743 | CCE->getConstructor()->isDefaultConstructor(); |
1744 | else if (Style == CXXNewExpr::ListInit) { |
1745 | (0) . __assert_fail ("isa(Init) && \"Shouldn't create list CXXConstructExprs for arrays.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1746, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<InitListExpr>(Init) && |
1746 | (0) . __assert_fail ("isa(Init) && \"Shouldn't create list CXXConstructExprs for arrays.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1746, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Shouldn't create list CXXConstructExprs for arrays."); |
1747 | return true; |
1748 | } |
1749 | return false; |
1750 | } |
1751 | |
1752 | bool |
1753 | Sema::isUnavailableAlignedAllocationFunction(const FunctionDecl &FD) const { |
1754 | if (!getLangOpts().AlignedAllocationUnavailable) |
1755 | return false; |
1756 | if (FD.isDefined()) |
1757 | return false; |
1758 | bool IsAligned = false; |
1759 | if (FD.isReplaceableGlobalAllocationFunction(&IsAligned) && IsAligned) |
1760 | return true; |
1761 | return false; |
1762 | } |
1763 | |
1764 | |
1765 | |
1766 | void Sema::diagnoseUnavailableAlignedAllocation(const FunctionDecl &FD, |
1767 | SourceLocation Loc) { |
1768 | if (isUnavailableAlignedAllocationFunction(FD)) { |
1769 | const llvm::Triple &T = getASTContext().getTargetInfo().getTriple(); |
1770 | StringRef OSName = AvailabilityAttr::getPlatformNameSourceSpelling( |
1771 | getASTContext().getTargetInfo().getPlatformName()); |
1772 | |
1773 | OverloadedOperatorKind Kind = FD.getDeclName().getCXXOverloadedOperator(); |
1774 | bool IsDelete = Kind == OO_Delete || Kind == OO_Array_Delete; |
1775 | Diag(Loc, diag::err_aligned_allocation_unavailable) |
1776 | << IsDelete << FD.getType().getAsString() << OSName |
1777 | << alignedAllocMinVersion(T.getOS()).getAsString(); |
1778 | Diag(Loc, diag::note_silence_aligned_allocation_unavailable); |
1779 | } |
1780 | } |
1781 | |
1782 | ExprResult |
1783 | Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, |
1784 | SourceLocation PlacementLParen, |
1785 | MultiExprArg PlacementArgs, |
1786 | SourceLocation PlacementRParen, |
1787 | SourceRange TypeIdParens, |
1788 | QualType AllocType, |
1789 | TypeSourceInfo *AllocTypeInfo, |
1790 | Expr *ArraySize, |
1791 | SourceRange DirectInitRange, |
1792 | Expr *Initializer) { |
1793 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
1794 | SourceLocation StartLoc = Range.getBegin(); |
1795 | |
1796 | CXXNewExpr::InitializationStyle initStyle; |
1797 | if (DirectInitRange.isValid()) { |
1798 | (0) . __assert_fail ("Initializer && \"Have parens but no initializer.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1798, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Initializer && "Have parens but no initializer."); |
1799 | initStyle = CXXNewExpr::CallInit; |
1800 | } else if (Initializer && isa<InitListExpr>(Initializer)) |
1801 | initStyle = CXXNewExpr::ListInit; |
1802 | else { |
1803 | (0) . __assert_fail ("(!Initializer || isa(Initializer) || isa(Initializer)) && \"Initializer expression that cannot have been implicitly created.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1805, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!Initializer || isa<ImplicitValueInitExpr>(Initializer) || |
1804 | (0) . __assert_fail ("(!Initializer || isa(Initializer) || isa(Initializer)) && \"Initializer expression that cannot have been implicitly created.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1805, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> isa<CXXConstructExpr>(Initializer)) && |
1805 | (0) . __assert_fail ("(!Initializer || isa(Initializer) || isa(Initializer)) && \"Initializer expression that cannot have been implicitly created.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1805, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Initializer expression that cannot have been implicitly created."); |
1806 | initStyle = CXXNewExpr::NoInit; |
1807 | } |
1808 | |
1809 | Expr **Inits = &Initializer; |
1810 | unsigned NumInits = Initializer ? 1 : 0; |
1811 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) { |
1812 | (0) . __assert_fail ("initStyle == CXXNewExpr..CallInit && \"paren init for non-call init\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1812, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(initStyle == CXXNewExpr::CallInit && "paren init for non-call init"); |
1813 | Inits = List->getExprs(); |
1814 | NumInits = List->getNumExprs(); |
1815 | } |
1816 | |
1817 | |
1818 | |
1819 | |
1820 | InitializationKind Kind |
1821 | |
1822 | |
1823 | |
1824 | = initStyle == CXXNewExpr::NoInit |
1825 | ? InitializationKind::CreateDefault(TypeRange.getBegin()) |
1826 | |
1827 | |
1828 | |
1829 | : initStyle == CXXNewExpr::ListInit |
1830 | ? InitializationKind::CreateDirectList( |
1831 | TypeRange.getBegin(), Initializer->getBeginLoc(), |
1832 | Initializer->getEndLoc()) |
1833 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
1834 | DirectInitRange.getBegin(), |
1835 | DirectInitRange.getEnd()); |
1836 | |
1837 | |
1838 | auto *Deduced = AllocType->getContainedDeducedType(); |
1839 | if (Deduced && isa<DeducedTemplateSpecializationType>(Deduced)) { |
1840 | if (ArraySize) |
1841 | return ExprError(Diag(ArraySize->getExprLoc(), |
1842 | diag::err_deduced_class_template_compound_type) |
1843 | << 2 << ArraySize->getSourceRange()); |
1844 | |
1845 | InitializedEntity Entity |
1846 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
1847 | AllocType = DeduceTemplateSpecializationFromInitializer( |
1848 | AllocTypeInfo, Entity, Kind, MultiExprArg(Inits, NumInits)); |
1849 | if (AllocType.isNull()) |
1850 | return ExprError(); |
1851 | } else if (Deduced) { |
1852 | bool Braced = (initStyle == CXXNewExpr::ListInit); |
1853 | if (NumInits == 1) { |
1854 | if (auto p = dyn_cast_or_null<InitListExpr>(Inits[0])) { |
1855 | Inits = p->getInits(); |
1856 | NumInits = p->getNumInits(); |
1857 | Braced = true; |
1858 | } |
1859 | } |
1860 | |
1861 | if (initStyle == CXXNewExpr::NoInit || NumInits == 0) |
1862 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) |
1863 | << AllocType << TypeRange); |
1864 | if (NumInits > 1) { |
1865 | Expr *FirstBad = Inits[1]; |
1866 | return ExprError(Diag(FirstBad->getBeginLoc(), |
1867 | diag::err_auto_new_ctor_multiple_expressions) |
1868 | << AllocType << TypeRange); |
1869 | } |
1870 | if (Braced && !getLangOpts().CPlusPlus17) |
1871 | Diag(Initializer->getBeginLoc(), diag::ext_auto_new_list_init) |
1872 | << AllocType << TypeRange; |
1873 | QualType DeducedType; |
1874 | if (DeduceAutoType(AllocTypeInfo, Inits[0], DeducedType) == DAR_Failed) |
1875 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) |
1876 | << AllocType << Inits[0]->getType() |
1877 | << TypeRange << Inits[0]->getSourceRange()); |
1878 | if (DeducedType.isNull()) |
1879 | return ExprError(); |
1880 | AllocType = DeducedType; |
1881 | } |
1882 | |
1883 | |
1884 | |
1885 | if (!ArraySize) { |
1886 | if (const ConstantArrayType *Array |
1887 | = Context.getAsConstantArrayType(AllocType)) { |
1888 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
1889 | Context.getSizeType(), |
1890 | TypeRange.getEnd()); |
1891 | AllocType = Array->getElementType(); |
1892 | } |
1893 | } |
1894 | |
1895 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
1896 | return ExprError(); |
1897 | |
1898 | |
1899 | if (getLangOpts().ObjCAutoRefCount && |
1900 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && |
1901 | AllocType->isObjCLifetimeType()) { |
1902 | AllocType = Context.getLifetimeQualifiedType(AllocType, |
1903 | AllocType->getObjCARCImplicitLifetime()); |
1904 | } |
1905 | |
1906 | QualType ResultType = Context.getPointerType(AllocType); |
1907 | |
1908 | if (ArraySize && ArraySize->getType()->isNonOverloadPlaceholderType()) { |
1909 | ExprResult result = CheckPlaceholderExpr(ArraySize); |
1910 | if (result.isInvalid()) return ExprError(); |
1911 | ArraySize = result.get(); |
1912 | } |
1913 | |
1914 | |
1915 | |
1916 | |
1917 | |
1918 | |
1919 | |
1920 | llvm::Optional<uint64_t> KnownArraySize; |
1921 | if (ArraySize && !ArraySize->isTypeDependent()) { |
1922 | ExprResult ConvertedSize; |
1923 | if (getLangOpts().CPlusPlus14) { |
1924 | (0) . __assert_fail ("Context.getTargetInfo().getIntWidth() && \"Builtin type of size 0?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 1924, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Context.getTargetInfo().getIntWidth() && "Builtin type of size 0?"); |
1925 | |
1926 | ConvertedSize = PerformImplicitConversion(ArraySize, Context.getSizeType(), |
1927 | AA_Converting); |
1928 | |
1929 | if (!ConvertedSize.isInvalid() && |
1930 | ArraySize->getType()->getAs<RecordType>()) |
1931 | |
1932 | Diag(StartLoc, diag::warn_cxx98_compat_array_size_conversion) |
1933 | << ArraySize->getType() << 0 << "'size_t'"; |
1934 | } else { |
1935 | class SizeConvertDiagnoser : public ICEConvertDiagnoser { |
1936 | protected: |
1937 | Expr *ArraySize; |
1938 | |
1939 | public: |
1940 | SizeConvertDiagnoser(Expr *ArraySize) |
1941 | : ICEConvertDiagnoser(, false, false), |
1942 | ArraySize(ArraySize) {} |
1943 | |
1944 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
1945 | QualType T) override { |
1946 | return S.Diag(Loc, diag::err_array_size_not_integral) |
1947 | << S.getLangOpts().CPlusPlus11 << T; |
1948 | } |
1949 | |
1950 | SemaDiagnosticBuilder diagnoseIncomplete( |
1951 | Sema &S, SourceLocation Loc, QualType T) override { |
1952 | return S.Diag(Loc, diag::err_array_size_incomplete_type) |
1953 | << T << ArraySize->getSourceRange(); |
1954 | } |
1955 | |
1956 | SemaDiagnosticBuilder diagnoseExplicitConv( |
1957 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { |
1958 | return S.Diag(Loc, diag::err_array_size_explicit_conversion) << T << ConvTy; |
1959 | } |
1960 | |
1961 | SemaDiagnosticBuilder noteExplicitConv( |
1962 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
1963 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
1964 | << ConvTy->isEnumeralType() << ConvTy; |
1965 | } |
1966 | |
1967 | SemaDiagnosticBuilder diagnoseAmbiguous( |
1968 | Sema &S, SourceLocation Loc, QualType T) override { |
1969 | return S.Diag(Loc, diag::err_array_size_ambiguous_conversion) << T; |
1970 | } |
1971 | |
1972 | SemaDiagnosticBuilder noteAmbiguous( |
1973 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
1974 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
1975 | << ConvTy->isEnumeralType() << ConvTy; |
1976 | } |
1977 | |
1978 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
1979 | QualType T, |
1980 | QualType ConvTy) override { |
1981 | return S.Diag(Loc, |
1982 | S.getLangOpts().CPlusPlus11 |
1983 | ? diag::warn_cxx98_compat_array_size_conversion |
1984 | : diag::ext_array_size_conversion) |
1985 | << T << ConvTy->isEnumeralType() << ConvTy; |
1986 | } |
1987 | } SizeDiagnoser(ArraySize); |
1988 | |
1989 | ConvertedSize = PerformContextualImplicitConversion(StartLoc, ArraySize, |
1990 | SizeDiagnoser); |
1991 | } |
1992 | if (ConvertedSize.isInvalid()) |
1993 | return ExprError(); |
1994 | |
1995 | ArraySize = ConvertedSize.get(); |
1996 | QualType SizeType = ArraySize->getType(); |
1997 | |
1998 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
1999 | return ExprError(); |
2000 | |
2001 | |
2002 | |
2003 | |
2004 | |
2005 | |
2006 | |
2007 | |
2008 | if (!ArraySize->isValueDependent()) { |
2009 | llvm::APSInt Value; |
2010 | |
2011 | |
2012 | |
2013 | |
2014 | |
2015 | if (ArraySize->isIntegerConstantExpr(Value, Context)) { |
2016 | if (Value.isSigned() && Value.isNegative()) { |
2017 | return ExprError(Diag(ArraySize->getBeginLoc(), |
2018 | diag::err_typecheck_negative_array_size) |
2019 | << ArraySize->getSourceRange()); |
2020 | } |
2021 | |
2022 | if (!AllocType->isDependentType()) { |
2023 | unsigned ActiveSizeBits = |
2024 | ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
2025 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) |
2026 | return ExprError( |
2027 | Diag(ArraySize->getBeginLoc(), diag::err_array_too_large) |
2028 | << Value.toString(10) << ArraySize->getSourceRange()); |
2029 | } |
2030 | |
2031 | KnownArraySize = Value.getZExtValue(); |
2032 | } else if (TypeIdParens.isValid()) { |
2033 | |
2034 | Diag(ArraySize->getBeginLoc(), diag::ext_new_paren_array_nonconst) |
2035 | << ArraySize->getSourceRange() |
2036 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
2037 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
2038 | |
2039 | TypeIdParens = SourceRange(); |
2040 | } |
2041 | } |
2042 | |
2043 | |
2044 | |
2045 | } |
2046 | |
2047 | FunctionDecl *OperatorNew = nullptr; |
2048 | FunctionDecl *OperatorDelete = nullptr; |
2049 | unsigned Alignment = |
2050 | AllocType->isDependentType() ? 0 : Context.getTypeAlign(AllocType); |
2051 | unsigned NewAlignment = Context.getTargetInfo().getNewAlign(); |
2052 | bool PassAlignment = getLangOpts().AlignedAllocation && |
2053 | Alignment > NewAlignment; |
2054 | |
2055 | AllocationFunctionScope Scope = UseGlobal ? AFS_Global : AFS_Both; |
2056 | if (!AllocType->isDependentType() && |
2057 | !Expr::hasAnyTypeDependentArguments(PlacementArgs) && |
2058 | FindAllocationFunctions(StartLoc, |
2059 | SourceRange(PlacementLParen, PlacementRParen), |
2060 | Scope, Scope, AllocType, ArraySize, PassAlignment, |
2061 | PlacementArgs, OperatorNew, OperatorDelete)) |
2062 | return ExprError(); |
2063 | |
2064 | |
2065 | |
2066 | bool UsualArrayDeleteWantsSize = false; |
2067 | if (ArraySize && !AllocType->isDependentType()) |
2068 | UsualArrayDeleteWantsSize = |
2069 | doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType); |
2070 | |
2071 | SmallVector<Expr *, 8> AllPlaceArgs; |
2072 | if (OperatorNew) { |
2073 | const FunctionProtoType *Proto = |
2074 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
2075 | VariadicCallType CallType = Proto->isVariadic() ? VariadicFunction |
2076 | : VariadicDoesNotApply; |
2077 | |
2078 | |
2079 | |
2080 | |
2081 | |
2082 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, Proto, |
2083 | PassAlignment ? 2 : 1, PlacementArgs, |
2084 | AllPlaceArgs, CallType)) |
2085 | return ExprError(); |
2086 | |
2087 | if (!AllPlaceArgs.empty()) |
2088 | PlacementArgs = AllPlaceArgs; |
2089 | |
2090 | |
2091 | DiagnoseSentinelCalls(OperatorNew, PlacementLParen, PlacementArgs); |
2092 | |
2093 | |
2094 | |
2095 | |
2096 | |
2097 | if (PlacementArgs.empty() && !PassAlignment && |
2098 | (OperatorNew->isImplicit() || |
2099 | (OperatorNew->getBeginLoc().isValid() && |
2100 | getSourceManager().isInSystemHeader(OperatorNew->getBeginLoc())))) { |
2101 | if (Alignment > NewAlignment) |
2102 | Diag(StartLoc, diag::warn_overaligned_type) |
2103 | << AllocType |
2104 | << unsigned(Alignment / Context.getCharWidth()) |
2105 | << unsigned(NewAlignment / Context.getCharWidth()); |
2106 | } |
2107 | } |
2108 | |
2109 | |
2110 | |
2111 | |
2112 | if (ArraySize && !isLegalArrayNewInitializer(initStyle, Initializer)) { |
2113 | SourceRange InitRange(Inits[0]->getBeginLoc(), |
2114 | Inits[NumInits - 1]->getEndLoc()); |
2115 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
2116 | return ExprError(); |
2117 | } |
2118 | |
2119 | |
2120 | |
2121 | if (!AllocType->isDependentType() && |
2122 | !Expr::hasAnyTypeDependentArguments( |
2123 | llvm::makeArrayRef(Inits, NumInits))) { |
2124 | |
2125 | QualType InitType; |
2126 | if (KnownArraySize) |
2127 | InitType = Context.getConstantArrayType( |
2128 | AllocType, llvm::APInt(Context.getTypeSize(Context.getSizeType()), |
2129 | *KnownArraySize), |
2130 | ArrayType::Normal, 0); |
2131 | else if (ArraySize) |
2132 | InitType = |
2133 | Context.getIncompleteArrayType(AllocType, ArrayType::Normal, 0); |
2134 | else |
2135 | InitType = AllocType; |
2136 | |
2137 | InitializedEntity Entity |
2138 | = InitializedEntity::InitializeNew(StartLoc, InitType); |
2139 | InitializationSequence InitSeq(*this, Entity, Kind, |
2140 | MultiExprArg(Inits, NumInits)); |
2141 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
2142 | MultiExprArg(Inits, NumInits)); |
2143 | if (FullInit.isInvalid()) |
2144 | return ExprError(); |
2145 | |
2146 | |
2147 | |
2148 | |
2149 | if (CXXBindTemporaryExpr *Binder = |
2150 | dyn_cast_or_null<CXXBindTemporaryExpr>(FullInit.get())) |
2151 | FullInit = Binder->getSubExpr(); |
2152 | |
2153 | Initializer = FullInit.get(); |
2154 | } |
2155 | |
2156 | |
2157 | if (OperatorNew) { |
2158 | if (DiagnoseUseOfDecl(OperatorNew, StartLoc)) |
2159 | return ExprError(); |
2160 | MarkFunctionReferenced(StartLoc, OperatorNew); |
2161 | } |
2162 | if (OperatorDelete) { |
2163 | if (DiagnoseUseOfDecl(OperatorDelete, StartLoc)) |
2164 | return ExprError(); |
2165 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
2166 | } |
2167 | |
2168 | |
2169 | |
2170 | |
2171 | QualType BaseAllocType = Context.getBaseElementType(AllocType); |
2172 | if (ArraySize && !BaseAllocType->isDependentType()) { |
2173 | if (const RecordType *BaseRecordType = BaseAllocType->getAs<RecordType>()) { |
2174 | if (CXXDestructorDecl *dtor = LookupDestructor( |
2175 | cast<CXXRecordDecl>(BaseRecordType->getDecl()))) { |
2176 | MarkFunctionReferenced(StartLoc, dtor); |
2177 | CheckDestructorAccess(StartLoc, dtor, |
2178 | PDiag(diag::err_access_dtor) |
2179 | << BaseAllocType); |
2180 | if (DiagnoseUseOfDecl(dtor, StartLoc)) |
2181 | return ExprError(); |
2182 | } |
2183 | } |
2184 | } |
2185 | |
2186 | return CXXNewExpr::Create(Context, UseGlobal, OperatorNew, OperatorDelete, |
2187 | PassAlignment, UsualArrayDeleteWantsSize, |
2188 | PlacementArgs, TypeIdParens, ArraySize, initStyle, |
2189 | Initializer, ResultType, AllocTypeInfo, Range, |
2190 | DirectInitRange); |
2191 | } |
2192 | |
2193 | |
2194 | |
2195 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
2196 | SourceRange R) { |
2197 | |
2198 | |
2199 | if (AllocType->isFunctionType()) |
2200 | return Diag(Loc, diag::err_bad_new_type) |
2201 | << AllocType << 0 << R; |
2202 | else if (AllocType->isReferenceType()) |
2203 | return Diag(Loc, diag::err_bad_new_type) |
2204 | << AllocType << 1 << R; |
2205 | else if (!AllocType->isDependentType() && |
2206 | RequireCompleteType(Loc, AllocType, diag::err_new_incomplete_type,R)) |
2207 | return true; |
2208 | else if (RequireNonAbstractType(Loc, AllocType, |
2209 | diag::err_allocation_of_abstract_type)) |
2210 | return true; |
2211 | else if (AllocType->isVariablyModifiedType()) |
2212 | return Diag(Loc, diag::err_variably_modified_new_type) |
2213 | << AllocType; |
2214 | else if (AllocType.getAddressSpace() != LangAS::Default && |
2215 | !getLangOpts().OpenCLCPlusPlus) |
2216 | return Diag(Loc, diag::err_address_space_qualified_new) |
2217 | << AllocType.getUnqualifiedType() |
2218 | << AllocType.getQualifiers().getAddressSpaceAttributePrintValue(); |
2219 | else if (getLangOpts().ObjCAutoRefCount) { |
2220 | if (const ArrayType *AT = Context.getAsArrayType(AllocType)) { |
2221 | QualType BaseAllocType = Context.getBaseElementType(AT); |
2222 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && |
2223 | BaseAllocType->isObjCLifetimeType()) |
2224 | return Diag(Loc, diag::err_arc_new_array_without_ownership) |
2225 | << BaseAllocType; |
2226 | } |
2227 | } |
2228 | |
2229 | return false; |
2230 | } |
2231 | |
2232 | static bool resolveAllocationOverload( |
2233 | Sema &S, LookupResult &R, SourceRange Range, SmallVectorImpl<Expr *> &Args, |
2234 | bool &PassAlignment, FunctionDecl *&Operator, |
2235 | OverloadCandidateSet *AlignedCandidates, Expr *AlignArg, bool Diagnose) { |
2236 | OverloadCandidateSet Candidates(R.getNameLoc(), |
2237 | OverloadCandidateSet::CSK_Normal); |
2238 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
2239 | Alloc != AllocEnd; ++Alloc) { |
2240 | |
2241 | |
2242 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
2243 | |
2244 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
2245 | S.AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
2246 | , Args, |
2247 | Candidates, |
2248 | ); |
2249 | continue; |
2250 | } |
2251 | |
2252 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
2253 | S.AddOverloadCandidate(Fn, Alloc.getPair(), Args, Candidates, |
2254 | ); |
2255 | } |
2256 | |
2257 | |
2258 | OverloadCandidateSet::iterator Best; |
2259 | switch (Candidates.BestViableFunction(S, R.getNameLoc(), Best)) { |
2260 | case OR_Success: { |
2261 | |
2262 | FunctionDecl *FnDecl = Best->Function; |
2263 | if (S.CheckAllocationAccess(R.getNameLoc(), Range, R.getNamingClass(), |
2264 | Best->FoundDecl) == Sema::AR_inaccessible) |
2265 | return true; |
2266 | |
2267 | Operator = FnDecl; |
2268 | return false; |
2269 | } |
2270 | |
2271 | case OR_No_Viable_Function: |
2272 | |
2273 | |
2274 | |
2275 | |
2276 | if (PassAlignment) { |
2277 | PassAlignment = false; |
2278 | AlignArg = Args[1]; |
2279 | Args.erase(Args.begin() + 1); |
2280 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
2281 | Operator, &Candidates, AlignArg, |
2282 | Diagnose); |
2283 | } |
2284 | |
2285 | |
2286 | |
2287 | |
2288 | |
2289 | |
2290 | |
2291 | if (R.getLookupName().getCXXOverloadedOperator() == OO_Array_New && |
2292 | S.Context.getLangOpts().MSVCCompat) { |
2293 | R.clear(); |
2294 | R.setLookupName(S.Context.DeclarationNames.getCXXOperatorName(OO_New)); |
2295 | S.LookupQualifiedName(R, S.Context.getTranslationUnitDecl()); |
2296 | |
2297 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
2298 | Operator, , |
2299 | , Diagnose); |
2300 | } |
2301 | |
2302 | if (Diagnose) { |
2303 | S.Diag(R.getNameLoc(), diag::err_ovl_no_viable_function_in_call) |
2304 | << R.getLookupName() << Range; |
2305 | |
2306 | |
2307 | |
2308 | |
2309 | if (AlignedCandidates) { |
2310 | auto IsAligned = [](OverloadCandidate &C) { |
2311 | return C.Function->getNumParams() > 1 && |
2312 | C.Function->getParamDecl(1)->getType()->isAlignValT(); |
2313 | }; |
2314 | auto IsUnaligned = [&](OverloadCandidate &C) { return !IsAligned(C); }; |
2315 | |
2316 | |
2317 | |
2318 | Args.insert(Args.begin() + 1, AlignArg); |
2319 | AlignedCandidates->NoteCandidates(S, OCD_AllCandidates, Args, "", |
2320 | R.getNameLoc(), IsAligned); |
2321 | Args.erase(Args.begin() + 1); |
2322 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args, "", R.getNameLoc(), |
2323 | IsUnaligned); |
2324 | } else { |
2325 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
2326 | } |
2327 | } |
2328 | return true; |
2329 | |
2330 | case OR_Ambiguous: |
2331 | if (Diagnose) { |
2332 | S.Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) |
2333 | << R.getLookupName() << Range; |
2334 | Candidates.NoteCandidates(S, OCD_ViableCandidates, Args); |
2335 | } |
2336 | return true; |
2337 | |
2338 | case OR_Deleted: { |
2339 | if (Diagnose) { |
2340 | S.Diag(R.getNameLoc(), diag::err_ovl_deleted_call) |
2341 | << R.getLookupName() << Range; |
2342 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
2343 | } |
2344 | return true; |
2345 | } |
2346 | } |
2347 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); |
2348 | } |
2349 | |
2350 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
2351 | AllocationFunctionScope NewScope, |
2352 | AllocationFunctionScope DeleteScope, |
2353 | QualType AllocType, bool IsArray, |
2354 | bool &PassAlignment, MultiExprArg PlaceArgs, |
2355 | FunctionDecl *&OperatorNew, |
2356 | FunctionDecl *&OperatorDelete, |
2357 | bool Diagnose) { |
2358 | |
2359 | |
2360 | |
2361 | |
2362 | |
2363 | |
2364 | |
2365 | |
2366 | |
2367 | |
2368 | SmallVector<Expr*, 8> AllocArgs; |
2369 | AllocArgs.reserve((PassAlignment ? 2 : 1) + PlaceArgs.size()); |
2370 | |
2371 | |
2372 | |
2373 | |
2374 | |
2375 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
2376 | Context.getTargetInfo().getPointerWidth(0)), |
2377 | Context.getSizeType(), |
2378 | SourceLocation()); |
2379 | AllocArgs.push_back(&Size); |
2380 | |
2381 | QualType AlignValT = Context.VoidTy; |
2382 | if (PassAlignment) { |
2383 | DeclareGlobalNewDelete(); |
2384 | AlignValT = Context.getTypeDeclType(getStdAlignValT()); |
2385 | } |
2386 | CXXScalarValueInitExpr Align(AlignValT, nullptr, SourceLocation()); |
2387 | if (PassAlignment) |
2388 | AllocArgs.push_back(&Align); |
2389 | |
2390 | AllocArgs.insert(AllocArgs.end(), PlaceArgs.begin(), PlaceArgs.end()); |
2391 | |
2392 | |
2393 | |
2394 | |
2395 | |
2396 | |
2397 | |
2398 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
2399 | IsArray ? OO_Array_New : OO_New); |
2400 | |
2401 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
2402 | |
2403 | |
2404 | { |
2405 | LookupResult R(*this, NewName, StartLoc, LookupOrdinaryName); |
2406 | |
2407 | |
2408 | |
2409 | |
2410 | |
2411 | |
2412 | if (AllocElemType->isRecordType() && NewScope != AFS_Global) |
2413 | LookupQualifiedName(R, AllocElemType->getAsCXXRecordDecl()); |
2414 | |
2415 | |
2416 | |
2417 | if (R.isAmbiguous()) |
2418 | return true; |
2419 | |
2420 | |
2421 | |
2422 | |
2423 | if (R.empty()) { |
2424 | if (NewScope == AFS_Class) |
2425 | return true; |
2426 | |
2427 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
2428 | } |
2429 | |
2430 | if (getLangOpts().OpenCLCPlusPlus && R.empty()) { |
2431 | Diag(StartLoc, diag::err_openclcxx_not_supported) << "default new"; |
2432 | return true; |
2433 | } |
2434 | |
2435 | (0) . __assert_fail ("!R.empty() && \"implicitly declared allocation functions not found\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 2435, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!R.empty() && "implicitly declared allocation functions not found"); |
2436 | (0) . __assert_fail ("!R.isAmbiguous() && \"global allocation functions are ambiguous\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 2436, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!R.isAmbiguous() && "global allocation functions are ambiguous"); |
2437 | |
2438 | |
2439 | R.suppressDiagnostics(); |
2440 | |
2441 | if (resolveAllocationOverload(*this, R, Range, AllocArgs, PassAlignment, |
2442 | OperatorNew, , |
2443 | , Diagnose)) |
2444 | return true; |
2445 | } |
2446 | |
2447 | |
2448 | if (!getLangOpts().Exceptions) { |
2449 | OperatorDelete = nullptr; |
2450 | return false; |
2451 | } |
2452 | |
2453 | |
2454 | |
2455 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
2456 | OperatorNew->getDeclName().getCXXOverloadedOperator() == OO_Array_New |
2457 | ? OO_Array_Delete |
2458 | : OO_Delete); |
2459 | |
2460 | |
2461 | |
2462 | |
2463 | |
2464 | |
2465 | |
2466 | |
2467 | |
2468 | |
2469 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
2470 | if (AllocElemType->isRecordType() && DeleteScope != AFS_Global) { |
2471 | CXXRecordDecl *RD |
2472 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
2473 | LookupQualifiedName(FoundDelete, RD); |
2474 | } |
2475 | if (FoundDelete.isAmbiguous()) |
2476 | return true; |
2477 | |
2478 | bool FoundGlobalDelete = FoundDelete.empty(); |
2479 | if (FoundDelete.empty()) { |
2480 | if (DeleteScope == AFS_Class) |
2481 | return true; |
2482 | |
2483 | DeclareGlobalNewDelete(); |
2484 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
2485 | } |
2486 | |
2487 | FoundDelete.suppressDiagnostics(); |
2488 | |
2489 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
2490 | |
2491 | |
2492 | |
2493 | |
2494 | |
2495 | |
2496 | |
2497 | |
2498 | |
2499 | |
2500 | |
2501 | |
2502 | |
2503 | |
2504 | bool isPlacementNew = !PlaceArgs.empty() || OperatorNew->param_size() != 1 || |
2505 | OperatorNew->isVariadic(); |
2506 | |
2507 | if (isPlacementNew) { |
2508 | |
2509 | |
2510 | |
2511 | |
2512 | |
2513 | |
2514 | |
2515 | |
2516 | |
2517 | |
2518 | QualType ExpectedFunctionType; |
2519 | { |
2520 | const FunctionProtoType *Proto |
2521 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
2522 | |
2523 | SmallVector<QualType, 4> ArgTypes; |
2524 | ArgTypes.push_back(Context.VoidPtrTy); |
2525 | for (unsigned I = 1, N = Proto->getNumParams(); I < N; ++I) |
2526 | ArgTypes.push_back(Proto->getParamType(I)); |
2527 | |
2528 | FunctionProtoType::ExtProtoInfo EPI; |
2529 | |
2530 | EPI.Variadic = Proto->isVariadic(); |
2531 | |
2532 | ExpectedFunctionType |
2533 | = Context.getFunctionType(Context.VoidTy, ArgTypes, EPI); |
2534 | } |
2535 | |
2536 | for (LookupResult::iterator D = FoundDelete.begin(), |
2537 | DEnd = FoundDelete.end(); |
2538 | D != DEnd; ++D) { |
2539 | FunctionDecl *Fn = nullptr; |
2540 | if (FunctionTemplateDecl *FnTmpl = |
2541 | dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
2542 | |
2543 | |
2544 | TemplateDeductionInfo Info(StartLoc); |
2545 | if (DeduceTemplateArguments(FnTmpl, nullptr, ExpectedFunctionType, Fn, |
2546 | Info)) |
2547 | continue; |
2548 | } else |
2549 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
2550 | |
2551 | if (Context.hasSameType(adjustCCAndNoReturn(Fn->getType(), |
2552 | ExpectedFunctionType, |
2553 | ), |
2554 | ExpectedFunctionType)) |
2555 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
2556 | } |
2557 | |
2558 | if (getLangOpts().CUDA) |
2559 | EraseUnwantedCUDAMatches(dyn_cast<FunctionDecl>(CurContext), Matches); |
2560 | } else { |
2561 | |
2562 | |
2563 | |
2564 | |
2565 | |
2566 | |
2567 | |
2568 | llvm::SmallVector<UsualDeallocFnInfo, 4> BestDeallocFns; |
2569 | UsualDeallocFnInfo Selected = resolveDeallocationOverload( |
2570 | *this, FoundDelete, FoundGlobalDelete, |
2571 | hasNewExtendedAlignment(*this, AllocElemType), |
2572 | &BestDeallocFns); |
2573 | if (Selected) |
2574 | Matches.push_back(std::make_pair(Selected.Found, Selected.FD)); |
2575 | else { |
2576 | |
2577 | |
2578 | for (auto Fn : BestDeallocFns) |
2579 | Matches.push_back(std::make_pair(Fn.Found, Fn.FD)); |
2580 | } |
2581 | } |
2582 | |
2583 | |
2584 | |
2585 | |
2586 | |
2587 | if (Matches.size() == 1) { |
2588 | OperatorDelete = Matches[0].second; |
2589 | |
2590 | |
2591 | |
2592 | |
2593 | |
2594 | |
2595 | |
2596 | if (getLangOpts().CPlusPlus11 && isPlacementNew && |
2597 | isNonPlacementDeallocationFunction(*this, OperatorDelete)) { |
2598 | UsualDeallocFnInfo Info(*this, |
2599 | DeclAccessPair::make(OperatorDelete, AS_public)); |
2600 | |
2601 | |
2602 | |
2603 | |
2604 | bool IsSizedDelete = Info.HasSizeT; |
2605 | if (IsSizedDelete && !FoundGlobalDelete) { |
2606 | auto NonSizedDelete = |
2607 | resolveDeallocationOverload(*this, FoundDelete, , |
2608 | .HasAlignValT); |
2609 | if (NonSizedDelete && !NonSizedDelete.HasSizeT && |
2610 | NonSizedDelete.HasAlignValT == Info.HasAlignValT) |
2611 | IsSizedDelete = false; |
2612 | } |
2613 | |
2614 | if (IsSizedDelete) { |
2615 | SourceRange R = PlaceArgs.empty() |
2616 | ? SourceRange() |
2617 | : SourceRange(PlaceArgs.front()->getBeginLoc(), |
2618 | PlaceArgs.back()->getEndLoc()); |
2619 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) << R; |
2620 | if (!OperatorDelete->isImplicit()) |
2621 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
2622 | << DeleteName; |
2623 | } |
2624 | } |
2625 | |
2626 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
2627 | Matches[0].first); |
2628 | } else if (!Matches.empty()) { |
2629 | |
2630 | |
2631 | |
2632 | Diag(StartLoc, diag::warn_ambiguous_suitable_delete_function_found) |
2633 | << DeleteName << AllocElemType; |
2634 | |
2635 | for (auto &Match : Matches) |
2636 | Diag(Match.second->getLocation(), |
2637 | diag::note_member_declared_here) << DeleteName; |
2638 | } |
2639 | |
2640 | return false; |
2641 | } |
2642 | |
2643 | |
2644 | |
2645 | |
2646 | |
2647 | |
2648 | |
2649 | |
2650 | |
2651 | |
2652 | |
2653 | |
2654 | |
2655 | |
2656 | |
2657 | |
2658 | |
2659 | |
2660 | |
2661 | |
2662 | |
2663 | |
2664 | |
2665 | |
2666 | void Sema::DeclareGlobalNewDelete() { |
2667 | if (GlobalNewDeleteDeclared) |
2668 | return; |
2669 | |
2670 | |
2671 | |
2672 | if (getLangOpts().OpenCLCPlusPlus) |
2673 | return; |
2674 | |
2675 | |
2676 | |
2677 | |
2678 | |
2679 | |
2680 | |
2681 | |
2682 | |
2683 | |
2684 | |
2685 | |
2686 | |
2687 | |
2688 | |
2689 | |
2690 | |
2691 | |
2692 | |
2693 | |
2694 | |
2695 | |
2696 | |
2697 | |
2698 | |
2699 | |
2700 | |
2701 | |
2702 | |
2703 | |
2704 | |
2705 | if (!StdBadAlloc && !getLangOpts().CPlusPlus11) { |
2706 | |
2707 | |
2708 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
2709 | getOrCreateStdNamespace(), |
2710 | SourceLocation(), SourceLocation(), |
2711 | &PP.getIdentifierTable().get("bad_alloc"), |
2712 | nullptr); |
2713 | getStdBadAlloc()->setImplicit(true); |
2714 | } |
2715 | if (!StdAlignValT && getLangOpts().AlignedAllocation) { |
2716 | |
2717 | |
2718 | auto *AlignValT = EnumDecl::Create( |
2719 | Context, getOrCreateStdNamespace(), SourceLocation(), SourceLocation(), |
2720 | &PP.getIdentifierTable().get("align_val_t"), nullptr, true, true, true); |
2721 | AlignValT->setIntegerType(Context.getSizeType()); |
2722 | AlignValT->setPromotionType(Context.getSizeType()); |
2723 | AlignValT->setImplicit(true); |
2724 | StdAlignValT = AlignValT; |
2725 | } |
2726 | |
2727 | GlobalNewDeleteDeclared = true; |
2728 | |
2729 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
2730 | QualType SizeT = Context.getSizeType(); |
2731 | |
2732 | auto DeclareGlobalAllocationFunctions = [&](OverloadedOperatorKind Kind, |
2733 | QualType Return, QualType Param) { |
2734 | llvm::SmallVector<QualType, 3> Params; |
2735 | Params.push_back(Param); |
2736 | |
2737 | |
2738 | bool HasSizedVariant = getLangOpts().SizedDeallocation && |
2739 | (Kind == OO_Delete || Kind == OO_Array_Delete); |
2740 | bool HasAlignedVariant = getLangOpts().AlignedAllocation; |
2741 | |
2742 | int NumSizeVariants = (HasSizedVariant ? 2 : 1); |
2743 | int NumAlignVariants = (HasAlignedVariant ? 2 : 1); |
2744 | for (int Sized = 0; Sized < NumSizeVariants; ++Sized) { |
2745 | if (Sized) |
2746 | Params.push_back(SizeT); |
2747 | |
2748 | for (int Aligned = 0; Aligned < NumAlignVariants; ++Aligned) { |
2749 | if (Aligned) |
2750 | Params.push_back(Context.getTypeDeclType(getStdAlignValT())); |
2751 | |
2752 | DeclareGlobalAllocationFunction( |
2753 | Context.DeclarationNames.getCXXOperatorName(Kind), Return, Params); |
2754 | |
2755 | if (Aligned) |
2756 | Params.pop_back(); |
2757 | } |
2758 | } |
2759 | }; |
2760 | |
2761 | DeclareGlobalAllocationFunctions(OO_New, VoidPtr, SizeT); |
2762 | DeclareGlobalAllocationFunctions(OO_Array_New, VoidPtr, SizeT); |
2763 | DeclareGlobalAllocationFunctions(OO_Delete, Context.VoidTy, VoidPtr); |
2764 | DeclareGlobalAllocationFunctions(OO_Array_Delete, Context.VoidTy, VoidPtr); |
2765 | } |
2766 | |
2767 | |
2768 | |
2769 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
2770 | QualType Return, |
2771 | ArrayRef<QualType> Params) { |
2772 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
2773 | |
2774 | |
2775 | DeclContext::lookup_result R = GlobalCtx->lookup(Name); |
2776 | for (DeclContext::lookup_iterator Alloc = R.begin(), AllocEnd = R.end(); |
2777 | Alloc != AllocEnd; ++Alloc) { |
2778 | |
2779 | |
2780 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
2781 | if (Func->getNumParams() == Params.size()) { |
2782 | llvm::SmallVector<QualType, 3> FuncParams; |
2783 | for (auto *P : Func->parameters()) |
2784 | FuncParams.push_back( |
2785 | Context.getCanonicalType(P->getType().getUnqualifiedType())); |
2786 | if (llvm::makeArrayRef(FuncParams) == Params) { |
2787 | |
2788 | |
2789 | |
2790 | Func->setVisibleDespiteOwningModule(); |
2791 | return; |
2792 | } |
2793 | } |
2794 | } |
2795 | } |
2796 | |
2797 | FunctionProtoType::ExtProtoInfo EPI(Context.getDefaultCallingConvention( |
2798 | , )); |
2799 | |
2800 | QualType BadAllocType; |
2801 | bool HasBadAllocExceptionSpec |
2802 | = (Name.getCXXOverloadedOperator() == OO_New || |
2803 | Name.getCXXOverloadedOperator() == OO_Array_New); |
2804 | if (HasBadAllocExceptionSpec) { |
2805 | if (!getLangOpts().CPlusPlus11) { |
2806 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
2807 | (0) . __assert_fail ("StdBadAlloc && \"Must have std..bad_alloc declared\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 2807, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
2808 | EPI.ExceptionSpec.Type = EST_Dynamic; |
2809 | EPI.ExceptionSpec.Exceptions = llvm::makeArrayRef(BadAllocType); |
2810 | } |
2811 | } else { |
2812 | EPI.ExceptionSpec = |
2813 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
2814 | } |
2815 | |
2816 | auto CreateAllocationFunctionDecl = [&](Attr *) { |
2817 | QualType FnType = Context.getFunctionType(Return, Params, EPI); |
2818 | FunctionDecl *Alloc = FunctionDecl::Create( |
2819 | Context, GlobalCtx, SourceLocation(), SourceLocation(), Name, |
2820 | FnType, , SC_None, false, true); |
2821 | Alloc->setImplicit(); |
2822 | |
2823 | Alloc->setVisibleDespiteOwningModule(); |
2824 | |
2825 | Alloc->addAttr(VisibilityAttr::CreateImplicit( |
2826 | Context, LangOpts.GlobalAllocationFunctionVisibilityHidden |
2827 | ? VisibilityAttr::Hidden |
2828 | : VisibilityAttr::Default)); |
2829 | |
2830 | llvm::SmallVector<ParmVarDecl *, 3> ParamDecls; |
2831 | for (QualType T : Params) { |
2832 | ParamDecls.push_back(ParmVarDecl::Create( |
2833 | Context, Alloc, SourceLocation(), SourceLocation(), nullptr, T, |
2834 | , SC_None, nullptr)); |
2835 | ParamDecls.back()->setImplicit(); |
2836 | } |
2837 | Alloc->setParams(ParamDecls); |
2838 | if (ExtraAttr) |
2839 | Alloc->addAttr(ExtraAttr); |
2840 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
2841 | IdResolver.tryAddTopLevelDecl(Alloc, Name); |
2842 | }; |
2843 | |
2844 | if (!LangOpts.CUDA) |
2845 | CreateAllocationFunctionDecl(nullptr); |
2846 | else { |
2847 | |
2848 | |
2849 | CreateAllocationFunctionDecl(CUDAHostAttr::CreateImplicit(Context)); |
2850 | CreateAllocationFunctionDecl(CUDADeviceAttr::CreateImplicit(Context)); |
2851 | } |
2852 | } |
2853 | |
2854 | FunctionDecl *Sema::FindUsualDeallocationFunction(SourceLocation StartLoc, |
2855 | bool CanProvideSize, |
2856 | bool Overaligned, |
2857 | DeclarationName Name) { |
2858 | DeclareGlobalNewDelete(); |
2859 | |
2860 | LookupResult FoundDelete(*this, Name, StartLoc, LookupOrdinaryName); |
2861 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
2862 | |
2863 | |
2864 | |
2865 | |
2866 | |
2867 | auto Result = resolveDeallocationOverload(*this, FoundDelete, CanProvideSize, |
2868 | Overaligned); |
2869 | (0) . __assert_fail ("Result.FD && \"operator delete missing from global scope?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 2869, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Result.FD && "operator delete missing from global scope?"); |
2870 | return Result.FD; |
2871 | } |
2872 | |
2873 | FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc, |
2874 | CXXRecordDecl *RD) { |
2875 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
2876 | |
2877 | FunctionDecl *OperatorDelete = nullptr; |
2878 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
2879 | return nullptr; |
2880 | if (OperatorDelete) |
2881 | return OperatorDelete; |
2882 | |
2883 | |
2884 | |
2885 | return FindUsualDeallocationFunction( |
2886 | Loc, true, hasNewExtendedAlignment(*this, Context.getRecordType(RD)), |
2887 | Name); |
2888 | } |
2889 | |
2890 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
2891 | DeclarationName Name, |
2892 | FunctionDecl *&Operator, bool Diagnose) { |
2893 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
2894 | |
2895 | LookupQualifiedName(Found, RD); |
2896 | |
2897 | if (Found.isAmbiguous()) |
2898 | return true; |
2899 | |
2900 | Found.suppressDiagnostics(); |
2901 | |
2902 | bool Overaligned = hasNewExtendedAlignment(*this, Context.getRecordType(RD)); |
2903 | |
2904 | |
2905 | |
2906 | |
2907 | llvm::SmallVector<UsualDeallocFnInfo, 4> Matches; |
2908 | resolveDeallocationOverload(*this, Found, false, |
2909 | Overaligned, &Matches); |
2910 | |
2911 | |
2912 | if (Matches.size() == 1) { |
2913 | Operator = cast<CXXMethodDecl>(Matches[0].FD); |
2914 | |
2915 | |
2916 | if (Operator->isDeleted()) { |
2917 | if (Diagnose) { |
2918 | Diag(StartLoc, diag::err_deleted_function_use); |
2919 | NoteDeletedFunction(Operator); |
2920 | } |
2921 | return true; |
2922 | } |
2923 | |
2924 | if (CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
2925 | Matches[0].Found, Diagnose) == AR_inaccessible) |
2926 | return true; |
2927 | |
2928 | return false; |
2929 | } |
2930 | |
2931 | |
2932 | |
2933 | |
2934 | if (!Matches.empty()) { |
2935 | if (Diagnose) { |
2936 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
2937 | << Name << RD; |
2938 | for (auto &Match : Matches) |
2939 | Diag(Match.FD->getLocation(), diag::note_member_declared_here) << Name; |
2940 | } |
2941 | return true; |
2942 | } |
2943 | |
2944 | |
2945 | |
2946 | if (!Found.empty()) { |
2947 | if (Diagnose) { |
2948 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
2949 | << Name << RD; |
2950 | |
2951 | for (NamedDecl *D : Found) |
2952 | Diag(D->getUnderlyingDecl()->getLocation(), |
2953 | diag::note_member_declared_here) << Name; |
2954 | } |
2955 | return true; |
2956 | } |
2957 | |
2958 | Operator = nullptr; |
2959 | return false; |
2960 | } |
2961 | |
2962 | namespace { |
2963 | |
2964 | |
2965 | class MismatchingNewDeleteDetector { |
2966 | public: |
2967 | enum MismatchResult { |
2968 | |
2969 | NoMismatch, |
2970 | |
2971 | VarInitMismatches, |
2972 | |
2973 | MemberInitMismatches, |
2974 | |
2975 | |
2976 | AnalyzeLater |
2977 | }; |
2978 | |
2979 | |
2980 | |
2981 | |
2982 | explicit MismatchingNewDeleteDetector(bool EndOfTU) |
2983 | : Field(nullptr), IsArrayForm(false), EndOfTU(EndOfTU), |
2984 | HasUndefinedConstructors(false) {} |
2985 | |
2986 | |
2987 | |
2988 | |
2989 | |
2990 | |
2991 | |
2992 | |
2993 | |
2994 | |
2995 | |
2996 | MismatchResult analyzeDeleteExpr(const CXXDeleteExpr *DE); |
2997 | |
2998 | |
2999 | |
3000 | |
3001 | MismatchResult analyzeField(FieldDecl *Field, bool DeleteWasArrayForm); |
3002 | FieldDecl *Field; |
3003 | |
3004 | llvm::SmallVector<const CXXNewExpr *, 4> NewExprs; |
3005 | |
3006 | bool IsArrayForm; |
3007 | |
3008 | private: |
3009 | const bool EndOfTU; |
3010 | |
3011 | bool HasUndefinedConstructors; |
3012 | |
3013 | |
3014 | |
3015 | const CXXNewExpr *getNewExprFromInitListOrExpr(const Expr *E); |
3016 | |
3017 | |
3018 | |
3019 | |
3020 | |
3021 | |
3022 | |
3023 | MismatchResult analyzeMemberExpr(const MemberExpr *ME); |
3024 | |
3025 | |
3026 | |
3027 | |
3028 | |
3029 | |
3030 | |
3031 | bool hasMatchingVarInit(const DeclRefExpr *D); |
3032 | |
3033 | |
3034 | |
3035 | |
3036 | |
3037 | |
3038 | |
3039 | |
3040 | bool hasMatchingNewInCtor(const CXXConstructorDecl *CD); |
3041 | |
3042 | |
3043 | bool hasMatchingNewInCtorInit(const CXXCtorInitializer *CI); |
3044 | |
3045 | |
3046 | MismatchResult analyzeInClassInitializer(); |
3047 | }; |
3048 | } |
3049 | |
3050 | MismatchingNewDeleteDetector::MismatchResult |
3051 | MismatchingNewDeleteDetector::analyzeDeleteExpr(const CXXDeleteExpr *DE) { |
3052 | NewExprs.clear(); |
3053 | (0) . __assert_fail ("DE && \"Expected delete-expression\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3053, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DE && "Expected delete-expression"); |
3054 | IsArrayForm = DE->isArrayForm(); |
3055 | const Expr *E = DE->getArgument()->IgnoreParenImpCasts(); |
3056 | if (const MemberExpr *ME = dyn_cast<const MemberExpr>(E)) { |
3057 | return analyzeMemberExpr(ME); |
3058 | } else if (const DeclRefExpr *D = dyn_cast<const DeclRefExpr>(E)) { |
3059 | if (!hasMatchingVarInit(D)) |
3060 | return VarInitMismatches; |
3061 | } |
3062 | return NoMismatch; |
3063 | } |
3064 | |
3065 | const CXXNewExpr * |
3066 | MismatchingNewDeleteDetector::getNewExprFromInitListOrExpr(const Expr *E) { |
3067 | (0) . __assert_fail ("E != nullptr && \"Expected a valid initializer expression\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3067, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E != nullptr && "Expected a valid initializer expression"); |
3068 | E = E->IgnoreParenImpCasts(); |
3069 | if (const InitListExpr *ILE = dyn_cast<const InitListExpr>(E)) { |
3070 | if (ILE->getNumInits() == 1) |
3071 | E = dyn_cast<const CXXNewExpr>(ILE->getInit(0)->IgnoreParenImpCasts()); |
3072 | } |
3073 | |
3074 | return dyn_cast_or_null<const CXXNewExpr>(E); |
3075 | } |
3076 | |
3077 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtorInit( |
3078 | const CXXCtorInitializer *CI) { |
3079 | const CXXNewExpr *NE = nullptr; |
3080 | if (Field == CI->getMember() && |
3081 | (NE = getNewExprFromInitListOrExpr(CI->getInit()))) { |
3082 | if (NE->isArray() == IsArrayForm) |
3083 | return true; |
3084 | else |
3085 | NewExprs.push_back(NE); |
3086 | } |
3087 | return false; |
3088 | } |
3089 | |
3090 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtor( |
3091 | const CXXConstructorDecl *CD) { |
3092 | if (CD->isImplicit()) |
3093 | return false; |
3094 | const FunctionDecl *Definition = CD; |
3095 | if (!CD->isThisDeclarationADefinition() && !CD->isDefined(Definition)) { |
3096 | HasUndefinedConstructors = true; |
3097 | return EndOfTU; |
3098 | } |
3099 | for (const auto *CI : cast<const CXXConstructorDecl>(Definition)->inits()) { |
3100 | if (hasMatchingNewInCtorInit(CI)) |
3101 | return true; |
3102 | } |
3103 | return false; |
3104 | } |
3105 | |
3106 | MismatchingNewDeleteDetector::MismatchResult |
3107 | MismatchingNewDeleteDetector::analyzeInClassInitializer() { |
3108 | (0) . __assert_fail ("Field != nullptr && \"This should be called only for members\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3108, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Field != nullptr && "This should be called only for members"); |
3109 | const Expr *InitExpr = Field->getInClassInitializer(); |
3110 | if (!InitExpr) |
3111 | return EndOfTU ? NoMismatch : AnalyzeLater; |
3112 | if (const CXXNewExpr *NE = getNewExprFromInitListOrExpr(InitExpr)) { |
3113 | if (NE->isArray() != IsArrayForm) { |
3114 | NewExprs.push_back(NE); |
3115 | return MemberInitMismatches; |
3116 | } |
3117 | } |
3118 | return NoMismatch; |
3119 | } |
3120 | |
3121 | MismatchingNewDeleteDetector::MismatchResult |
3122 | MismatchingNewDeleteDetector::analyzeField(FieldDecl *Field, |
3123 | bool DeleteWasArrayForm) { |
3124 | (0) . __assert_fail ("Field != nullptr && \"Analysis requires a valid class member.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3124, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Field != nullptr && "Analysis requires a valid class member."); |
3125 | this->Field = Field; |
3126 | IsArrayForm = DeleteWasArrayForm; |
3127 | const CXXRecordDecl *RD = cast<const CXXRecordDecl>(Field->getParent()); |
3128 | for (const auto *CD : RD->ctors()) { |
3129 | if (hasMatchingNewInCtor(CD)) |
3130 | return NoMismatch; |
3131 | } |
3132 | if (HasUndefinedConstructors) |
3133 | return EndOfTU ? NoMismatch : AnalyzeLater; |
3134 | if (!NewExprs.empty()) |
3135 | return MemberInitMismatches; |
3136 | return Field->hasInClassInitializer() ? analyzeInClassInitializer() |
3137 | : NoMismatch; |
3138 | } |
3139 | |
3140 | MismatchingNewDeleteDetector::MismatchResult |
3141 | MismatchingNewDeleteDetector::analyzeMemberExpr(const MemberExpr *ME) { |
3142 | (0) . __assert_fail ("ME != nullptr && \"Expected a member expression\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3142, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ME != nullptr && "Expected a member expression"); |
3143 | if (FieldDecl *F = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
3144 | return analyzeField(F, IsArrayForm); |
3145 | return NoMismatch; |
3146 | } |
3147 | |
3148 | bool MismatchingNewDeleteDetector::hasMatchingVarInit(const DeclRefExpr *D) { |
3149 | const CXXNewExpr *NE = nullptr; |
3150 | if (const VarDecl *VD = dyn_cast<const VarDecl>(D->getDecl())) { |
3151 | if (VD->hasInit() && (NE = getNewExprFromInitListOrExpr(VD->getInit())) && |
3152 | NE->isArray() != IsArrayForm) { |
3153 | NewExprs.push_back(NE); |
3154 | } |
3155 | } |
3156 | return NewExprs.empty(); |
3157 | } |
3158 | |
3159 | static void |
3160 | DiagnoseMismatchedNewDelete(Sema &SemaRef, SourceLocation DeleteLoc, |
3161 | const MismatchingNewDeleteDetector &Detector) { |
3162 | SourceLocation EndOfDelete = SemaRef.getLocForEndOfToken(DeleteLoc); |
3163 | FixItHint H; |
3164 | if (!Detector.IsArrayForm) |
3165 | H = FixItHint::CreateInsertion(EndOfDelete, "[]"); |
3166 | else { |
3167 | SourceLocation RSquare = Lexer::findLocationAfterToken( |
3168 | DeleteLoc, tok::l_square, SemaRef.getSourceManager(), |
3169 | SemaRef.getLangOpts(), true); |
3170 | if (RSquare.isValid()) |
3171 | H = FixItHint::CreateRemoval(SourceRange(EndOfDelete, RSquare)); |
3172 | } |
3173 | SemaRef.Diag(DeleteLoc, diag::warn_mismatched_delete_new) |
3174 | << Detector.IsArrayForm << H; |
3175 | |
3176 | for (const auto *NE : Detector.NewExprs) |
3177 | SemaRef.Diag(NE->getExprLoc(), diag::note_allocated_here) |
3178 | << Detector.IsArrayForm; |
3179 | } |
3180 | |
3181 | void Sema::AnalyzeDeleteExprMismatch(const CXXDeleteExpr *DE) { |
3182 | if (Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) |
3183 | return; |
3184 | MismatchingNewDeleteDetector Detector(); |
3185 | switch (Detector.analyzeDeleteExpr(DE)) { |
3186 | case MismatchingNewDeleteDetector::VarInitMismatches: |
3187 | case MismatchingNewDeleteDetector::MemberInitMismatches: { |
3188 | DiagnoseMismatchedNewDelete(*this, DE->getBeginLoc(), Detector); |
3189 | break; |
3190 | } |
3191 | case MismatchingNewDeleteDetector::AnalyzeLater: { |
3192 | DeleteExprs[Detector.Field].push_back( |
3193 | std::make_pair(DE->getBeginLoc(), DE->isArrayForm())); |
3194 | break; |
3195 | } |
3196 | case MismatchingNewDeleteDetector::NoMismatch: |
3197 | break; |
3198 | } |
3199 | } |
3200 | |
3201 | void Sema::AnalyzeDeleteExprMismatch(FieldDecl *Field, SourceLocation DeleteLoc, |
3202 | bool DeleteWasArrayForm) { |
3203 | MismatchingNewDeleteDetector Detector(); |
3204 | switch (Detector.analyzeField(Field, DeleteWasArrayForm)) { |
3205 | case MismatchingNewDeleteDetector::VarInitMismatches: |
3206 | llvm_unreachable("This analysis should have been done for class members."); |
3207 | case MismatchingNewDeleteDetector::AnalyzeLater: |
3208 | llvm_unreachable("Analysis cannot be postponed any point beyond end of " |
3209 | "translation unit."); |
3210 | case MismatchingNewDeleteDetector::MemberInitMismatches: |
3211 | DiagnoseMismatchedNewDelete(*this, DeleteLoc, Detector); |
3212 | break; |
3213 | case MismatchingNewDeleteDetector::NoMismatch: |
3214 | break; |
3215 | } |
3216 | } |
3217 | |
3218 | |
3219 | |
3220 | |
3221 | |
3222 | ExprResult |
3223 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
3224 | bool ArrayForm, Expr *ExE) { |
3225 | |
3226 | |
3227 | |
3228 | |
3229 | |
3230 | |
3231 | |
3232 | ExprResult Ex = ExE; |
3233 | FunctionDecl *OperatorDelete = nullptr; |
3234 | bool ArrayFormAsWritten = ArrayForm; |
3235 | bool UsualArrayDeleteWantsSize = false; |
3236 | |
3237 | if (!Ex.get()->isTypeDependent()) { |
3238 | |
3239 | Ex = DefaultLvalueConversion(Ex.get()); |
3240 | if (Ex.isInvalid()) |
3241 | return ExprError(); |
3242 | |
3243 | QualType Type = Ex.get()->getType(); |
3244 | |
3245 | class DeleteConverter : public ContextualImplicitConverter { |
3246 | public: |
3247 | DeleteConverter() : ContextualImplicitConverter(false, true) {} |
3248 | |
3249 | bool match(QualType ConvType) override { |
3250 | |
3251 | |
3252 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
3253 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
3254 | return true; |
3255 | return false; |
3256 | } |
3257 | |
3258 | SemaDiagnosticBuilder diagnoseNoMatch(Sema &S, SourceLocation Loc, |
3259 | QualType T) override { |
3260 | return S.Diag(Loc, diag::err_delete_operand) << T; |
3261 | } |
3262 | |
3263 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
3264 | QualType T) override { |
3265 | return S.Diag(Loc, diag::err_delete_incomplete_class_type) << T; |
3266 | } |
3267 | |
3268 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
3269 | QualType T, |
3270 | QualType ConvTy) override { |
3271 | return S.Diag(Loc, diag::err_delete_explicit_conversion) << T << ConvTy; |
3272 | } |
3273 | |
3274 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
3275 | QualType ConvTy) override { |
3276 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
3277 | << ConvTy; |
3278 | } |
3279 | |
3280 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
3281 | QualType T) override { |
3282 | return S.Diag(Loc, diag::err_ambiguous_delete_operand) << T; |
3283 | } |
3284 | |
3285 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
3286 | QualType ConvTy) override { |
3287 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
3288 | << ConvTy; |
3289 | } |
3290 | |
3291 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
3292 | QualType T, |
3293 | QualType ConvTy) override { |
3294 | llvm_unreachable("conversion functions are permitted"); |
3295 | } |
3296 | } Converter; |
3297 | |
3298 | Ex = PerformContextualImplicitConversion(StartLoc, Ex.get(), Converter); |
3299 | if (Ex.isInvalid()) |
3300 | return ExprError(); |
3301 | Type = Ex.get()->getType(); |
3302 | if (!Converter.match(Type)) |
3303 | |
3304 | |
3305 | return ExprError(); |
3306 | |
3307 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
3308 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
3309 | |
3310 | if (Pointee.getAddressSpace() != LangAS::Default && |
3311 | !getLangOpts().OpenCLCPlusPlus) |
3312 | return Diag(Ex.get()->getBeginLoc(), |
3313 | diag::err_address_space_qualified_delete) |
3314 | << Pointee.getUnqualifiedType() |
3315 | << Pointee.getQualifiers().getAddressSpaceAttributePrintValue(); |
3316 | |
3317 | CXXRecordDecl *PointeeRD = nullptr; |
3318 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
3319 | |
3320 | |
3321 | |
3322 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
3323 | << Type << Ex.get()->getSourceRange(); |
3324 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) { |
3325 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
3326 | << Type << Ex.get()->getSourceRange()); |
3327 | } else if (!Pointee->isDependentType()) { |
3328 | |
3329 | |
3330 | if (!RequireCompleteType(StartLoc, Pointee, |
3331 | diag::warn_delete_incomplete, Ex.get())) { |
3332 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) |
3333 | PointeeRD = cast<CXXRecordDecl>(RT->getDecl()); |
3334 | } |
3335 | } |
3336 | |
3337 | if (Pointee->isArrayType() && !ArrayForm) { |
3338 | Diag(StartLoc, diag::warn_delete_array_type) |
3339 | << Type << Ex.get()->getSourceRange() |
3340 | << FixItHint::CreateInsertion(getLocForEndOfToken(StartLoc), "[]"); |
3341 | ArrayForm = true; |
3342 | } |
3343 | |
3344 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
3345 | ArrayForm ? OO_Array_Delete : OO_Delete); |
3346 | |
3347 | if (PointeeRD) { |
3348 | if (!UseGlobal && |
3349 | FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, |
3350 | OperatorDelete)) |
3351 | return ExprError(); |
3352 | |
3353 | |
3354 | |
3355 | if (ArrayForm) { |
3356 | |
3357 | |
3358 | if (UseGlobal) |
3359 | UsualArrayDeleteWantsSize = |
3360 | doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem); |
3361 | |
3362 | |
3363 | |
3364 | else if (OperatorDelete && isa<CXXMethodDecl>(OperatorDelete)) |
3365 | UsualArrayDeleteWantsSize = |
3366 | UsualDeallocFnInfo(*this, |
3367 | DeclAccessPair::make(OperatorDelete, AS_public)) |
3368 | .HasSizeT; |
3369 | } |
3370 | |
3371 | if (!PointeeRD->hasIrrelevantDestructor()) |
3372 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
3373 | MarkFunctionReferenced(StartLoc, |
3374 | const_cast<CXXDestructorDecl*>(Dtor)); |
3375 | if (DiagnoseUseOfDecl(Dtor, StartLoc)) |
3376 | return ExprError(); |
3377 | } |
3378 | |
3379 | CheckVirtualDtorCall(PointeeRD->getDestructor(), StartLoc, |
3380 | , , |
3381 | !ArrayForm, |
3382 | SourceLocation()); |
3383 | } |
3384 | |
3385 | if (!OperatorDelete) { |
3386 | if (getLangOpts().OpenCLCPlusPlus) { |
3387 | Diag(StartLoc, diag::err_openclcxx_not_supported) << "default delete"; |
3388 | return ExprError(); |
3389 | } |
3390 | |
3391 | bool IsComplete = isCompleteType(StartLoc, Pointee); |
3392 | bool CanProvideSize = |
3393 | IsComplete && (!ArrayForm || UsualArrayDeleteWantsSize || |
3394 | Pointee.isDestructedType()); |
3395 | bool Overaligned = hasNewExtendedAlignment(*this, Pointee); |
3396 | |
3397 | |
3398 | OperatorDelete = FindUsualDeallocationFunction(StartLoc, CanProvideSize, |
3399 | Overaligned, DeleteName); |
3400 | } |
3401 | |
3402 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
3403 | |
3404 | |
3405 | |
3406 | bool IsVirtualDelete = false; |
3407 | if (PointeeRD) { |
3408 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
3409 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, |
3410 | PDiag(diag::err_access_dtor) << PointeeElem); |
3411 | IsVirtualDelete = Dtor->isVirtual(); |
3412 | } |
3413 | } |
3414 | |
3415 | DiagnoseUseOfDecl(OperatorDelete, StartLoc); |
3416 | |
3417 | |
3418 | |
3419 | |
3420 | |
3421 | QualType ParamType = OperatorDelete->getParamDecl(0)->getType(); |
3422 | if (!IsVirtualDelete && !ParamType->getPointeeType()->isVoidType()) { |
3423 | Qualifiers Qs = Pointee.getQualifiers(); |
3424 | if (Qs.hasCVRQualifiers()) { |
3425 | |
3426 | |
3427 | Qs.removeCVRQualifiers(); |
3428 | QualType Unqual = Context.getPointerType( |
3429 | Context.getQualifiedType(Pointee.getUnqualifiedType(), Qs)); |
3430 | Ex = ImpCastExprToType(Ex.get(), Unqual, CK_NoOp); |
3431 | } |
3432 | Ex = PerformImplicitConversion(Ex.get(), ParamType, AA_Passing); |
3433 | if (Ex.isInvalid()) |
3434 | return ExprError(); |
3435 | } |
3436 | } |
3437 | |
3438 | CXXDeleteExpr *Result = new (Context) CXXDeleteExpr( |
3439 | Context.VoidTy, UseGlobal, ArrayForm, ArrayFormAsWritten, |
3440 | UsualArrayDeleteWantsSize, OperatorDelete, Ex.get(), StartLoc); |
3441 | AnalyzeDeleteExprMismatch(Result); |
3442 | return Result; |
3443 | } |
3444 | |
3445 | static bool resolveBuiltinNewDeleteOverload(Sema &S, CallExpr *TheCall, |
3446 | bool IsDelete, |
3447 | FunctionDecl *&Operator) { |
3448 | |
3449 | DeclarationName NewName = S.Context.DeclarationNames.getCXXOperatorName( |
3450 | IsDelete ? OO_Delete : OO_New); |
3451 | |
3452 | LookupResult R(S, NewName, TheCall->getBeginLoc(), Sema::LookupOrdinaryName); |
3453 | S.LookupQualifiedName(R, S.Context.getTranslationUnitDecl()); |
3454 | (0) . __assert_fail ("!R.empty() && \"implicitly declared allocation functions not found\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3454, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!R.empty() && "implicitly declared allocation functions not found"); |
3455 | (0) . __assert_fail ("!R.isAmbiguous() && \"global allocation functions are ambiguous\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3455, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!R.isAmbiguous() && "global allocation functions are ambiguous"); |
3456 | |
3457 | |
3458 | R.suppressDiagnostics(); |
3459 | |
3460 | SmallVector<Expr *, 8> Args(TheCall->arg_begin(), TheCall->arg_end()); |
3461 | OverloadCandidateSet Candidates(R.getNameLoc(), |
3462 | OverloadCandidateSet::CSK_Normal); |
3463 | for (LookupResult::iterator FnOvl = R.begin(), FnOvlEnd = R.end(); |
3464 | FnOvl != FnOvlEnd; ++FnOvl) { |
3465 | |
3466 | |
3467 | NamedDecl *D = (*FnOvl)->getUnderlyingDecl(); |
3468 | |
3469 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
3470 | S.AddTemplateOverloadCandidate(FnTemplate, FnOvl.getPair(), |
3471 | , Args, |
3472 | Candidates, |
3473 | ); |
3474 | continue; |
3475 | } |
3476 | |
3477 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
3478 | S.AddOverloadCandidate(Fn, FnOvl.getPair(), Args, Candidates, |
3479 | ); |
3480 | } |
3481 | |
3482 | SourceRange Range = TheCall->getSourceRange(); |
3483 | |
3484 | |
3485 | OverloadCandidateSet::iterator Best; |
3486 | switch (Candidates.BestViableFunction(S, R.getNameLoc(), Best)) { |
3487 | case OR_Success: { |
3488 | |
3489 | FunctionDecl *FnDecl = Best->Function; |
3490 | (0) . __assert_fail ("R.getNamingClass() == nullptr && \"class members should not be considered\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3491, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(R.getNamingClass() == nullptr && |
3491 | (0) . __assert_fail ("R.getNamingClass() == nullptr && \"class members should not be considered\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3491, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "class members should not be considered"); |
3492 | |
3493 | if (!FnDecl->isReplaceableGlobalAllocationFunction()) { |
3494 | S.Diag(R.getNameLoc(), diag::err_builtin_operator_new_delete_not_usual) |
3495 | << (IsDelete ? 1 : 0) << Range; |
3496 | S.Diag(FnDecl->getLocation(), diag::note_non_usual_function_declared_here) |
3497 | << R.getLookupName() << FnDecl->getSourceRange(); |
3498 | return true; |
3499 | } |
3500 | |
3501 | Operator = FnDecl; |
3502 | return false; |
3503 | } |
3504 | |
3505 | case OR_No_Viable_Function: |
3506 | S.Diag(R.getNameLoc(), diag::err_ovl_no_viable_function_in_call) |
3507 | << R.getLookupName() << Range; |
3508 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
3509 | return true; |
3510 | |
3511 | case OR_Ambiguous: |
3512 | S.Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) |
3513 | << R.getLookupName() << Range; |
3514 | Candidates.NoteCandidates(S, OCD_ViableCandidates, Args); |
3515 | return true; |
3516 | |
3517 | case OR_Deleted: { |
3518 | S.Diag(R.getNameLoc(), diag::err_ovl_deleted_call) |
3519 | << R.getLookupName() << Range; |
3520 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
3521 | return true; |
3522 | } |
3523 | } |
3524 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); |
3525 | } |
3526 | |
3527 | ExprResult |
3528 | Sema::SemaBuiltinOperatorNewDeleteOverloaded(ExprResult TheCallResult, |
3529 | bool IsDelete) { |
3530 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
3531 | if (!getLangOpts().CPlusPlus) { |
3532 | Diag(TheCall->getExprLoc(), diag::err_builtin_requires_language) |
3533 | << (IsDelete ? "__builtin_operator_delete" : "__builtin_operator_new") |
3534 | << "C++"; |
3535 | return ExprError(); |
3536 | } |
3537 | |
3538 | |
3539 | DeclareGlobalNewDelete(); |
3540 | |
3541 | FunctionDecl *OperatorNewOrDelete = nullptr; |
3542 | if (resolveBuiltinNewDeleteOverload(*this, TheCall, IsDelete, |
3543 | OperatorNewOrDelete)) |
3544 | return ExprError(); |
3545 | (0) . __assert_fail ("OperatorNewOrDelete && \"should be found\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3545, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OperatorNewOrDelete && "should be found"); |
3546 | |
3547 | DiagnoseUseOfDecl(OperatorNewOrDelete, TheCall->getExprLoc()); |
3548 | MarkFunctionReferenced(TheCall->getExprLoc(), OperatorNewOrDelete); |
3549 | |
3550 | TheCall->setType(OperatorNewOrDelete->getReturnType()); |
3551 | for (unsigned i = 0; i != TheCall->getNumArgs(); ++i) { |
3552 | QualType ParamTy = OperatorNewOrDelete->getParamDecl(i)->getType(); |
3553 | InitializedEntity Entity = |
3554 | InitializedEntity::InitializeParameter(Context, ParamTy, false); |
3555 | ExprResult Arg = PerformCopyInitialization( |
3556 | Entity, TheCall->getArg(i)->getBeginLoc(), TheCall->getArg(i)); |
3557 | if (Arg.isInvalid()) |
3558 | return ExprError(); |
3559 | TheCall->setArg(i, Arg.get()); |
3560 | } |
3561 | auto Callee = dyn_cast<ImplicitCastExpr>(TheCall->getCallee()); |
3562 | (0) . __assert_fail ("Callee && Callee->getCastKind() == CK_BuiltinFnToFnPtr && \"Callee expected to be implicit cast to a builtin function pointer\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3563, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Callee && Callee->getCastKind() == CK_BuiltinFnToFnPtr && |
3563 | (0) . __assert_fail ("Callee && Callee->getCastKind() == CK_BuiltinFnToFnPtr && \"Callee expected to be implicit cast to a builtin function pointer\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3563, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Callee expected to be implicit cast to a builtin function pointer"); |
3564 | Callee->setType(OperatorNewOrDelete->getType()); |
3565 | |
3566 | return TheCallResult; |
3567 | } |
3568 | |
3569 | void Sema::CheckVirtualDtorCall(CXXDestructorDecl *dtor, SourceLocation Loc, |
3570 | bool IsDelete, bool CallCanBeVirtual, |
3571 | bool WarnOnNonAbstractTypes, |
3572 | SourceLocation DtorLoc) { |
3573 | if (!dtor || dtor->isVirtual() || !CallCanBeVirtual || isUnevaluatedContext()) |
3574 | return; |
3575 | |
3576 | |
3577 | |
3578 | |
3579 | |
3580 | |
3581 | |
3582 | |
3583 | const CXXRecordDecl *PointeeRD = dtor->getParent(); |
3584 | |
3585 | if (!PointeeRD->isPolymorphic() || PointeeRD->hasAttr<FinalAttr>()) |
3586 | return; |
3587 | |
3588 | |
3589 | |
3590 | |
3591 | if (getSourceManager().isInSystemHeader(PointeeRD->getLocation())) |
3592 | return; |
3593 | |
3594 | QualType ClassType = dtor->getThisType()->getPointeeType(); |
3595 | if (PointeeRD->isAbstract()) { |
3596 | |
3597 | |
3598 | Diag(Loc, diag::warn_delete_abstract_non_virtual_dtor) << (IsDelete ? 0 : 1) |
3599 | << ClassType; |
3600 | } else if (WarnOnNonAbstractTypes) { |
3601 | |
3602 | |
3603 | Diag(Loc, diag::warn_delete_non_virtual_dtor) << (IsDelete ? 0 : 1) |
3604 | << ClassType; |
3605 | } |
3606 | if (!IsDelete) { |
3607 | std::string TypeStr; |
3608 | ClassType.getAsStringInternal(TypeStr, getPrintingPolicy()); |
3609 | Diag(DtorLoc, diag::note_delete_non_virtual) |
3610 | << FixItHint::CreateInsertion(DtorLoc, TypeStr + "::"); |
3611 | } |
3612 | } |
3613 | |
3614 | Sema::ConditionResult Sema::ActOnConditionVariable(Decl *ConditionVar, |
3615 | SourceLocation StmtLoc, |
3616 | ConditionKind CK) { |
3617 | ExprResult E = |
3618 | CheckConditionVariable(cast<VarDecl>(ConditionVar), StmtLoc, CK); |
3619 | if (E.isInvalid()) |
3620 | return ConditionError(); |
3621 | return ConditionResult(*this, ConditionVar, MakeFullExpr(E.get(), StmtLoc), |
3622 | CK == ConditionKind::ConstexprIf); |
3623 | } |
3624 | |
3625 | |
3626 | |
3627 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
3628 | SourceLocation StmtLoc, |
3629 | ConditionKind CK) { |
3630 | if (ConditionVar->isInvalidDecl()) |
3631 | return ExprError(); |
3632 | |
3633 | QualType T = ConditionVar->getType(); |
3634 | |
3635 | |
3636 | |
3637 | if (T->isFunctionType()) |
3638 | return ExprError(Diag(ConditionVar->getLocation(), |
3639 | diag::err_invalid_use_of_function_type) |
3640 | << ConditionVar->getSourceRange()); |
3641 | else if (T->isArrayType()) |
3642 | return ExprError(Diag(ConditionVar->getLocation(), |
3643 | diag::err_invalid_use_of_array_type) |
3644 | << ConditionVar->getSourceRange()); |
3645 | |
3646 | ExprResult Condition = DeclRefExpr::Create( |
3647 | Context, NestedNameSpecifierLoc(), SourceLocation(), ConditionVar, |
3648 | false, ConditionVar->getLocation(), |
3649 | ConditionVar->getType().getNonReferenceType(), VK_LValue); |
3650 | |
3651 | MarkDeclRefReferenced(cast<DeclRefExpr>(Condition.get())); |
3652 | |
3653 | switch (CK) { |
3654 | case ConditionKind::Boolean: |
3655 | return CheckBooleanCondition(StmtLoc, Condition.get()); |
3656 | |
3657 | case ConditionKind::ConstexprIf: |
3658 | return CheckBooleanCondition(StmtLoc, Condition.get(), true); |
3659 | |
3660 | case ConditionKind::Switch: |
3661 | return CheckSwitchCondition(StmtLoc, Condition.get()); |
3662 | } |
3663 | |
3664 | llvm_unreachable("unexpected condition kind"); |
3665 | } |
3666 | |
3667 | |
3668 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr, bool IsConstexpr) { |
3669 | |
3670 | |
3671 | |
3672 | |
3673 | |
3674 | |
3675 | |
3676 | |
3677 | |
3678 | llvm::APSInt Value(); |
3679 | return (IsConstexpr && !CondExpr->isValueDependent()) |
3680 | ? CheckConvertedConstantExpression(CondExpr, Context.BoolTy, Value, |
3681 | CCEK_ConstexprIf) |
3682 | : PerformContextuallyConvertToBool(CondExpr); |
3683 | } |
3684 | |
3685 | |
3686 | |
3687 | |
3688 | |
3689 | bool |
3690 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
3691 | |
3692 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
3693 | From = Cast->getSubExpr(); |
3694 | |
3695 | |
3696 | |
3697 | |
3698 | |
3699 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
3700 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
3701 | if (const BuiltinType *ToPointeeType |
3702 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
3703 | |
3704 | |
3705 | if (!ToPtrType->getPointeeType().hasQualifiers()) { |
3706 | switch (StrLit->getKind()) { |
3707 | case StringLiteral::UTF8: |
3708 | case StringLiteral::UTF16: |
3709 | case StringLiteral::UTF32: |
3710 | |
3711 | break; |
3712 | case StringLiteral::Ascii: |
3713 | return (ToPointeeType->getKind() == BuiltinType::Char_U || |
3714 | ToPointeeType->getKind() == BuiltinType::Char_S); |
3715 | case StringLiteral::Wide: |
3716 | return Context.typesAreCompatible(Context.getWideCharType(), |
3717 | QualType(ToPointeeType, 0)); |
3718 | } |
3719 | } |
3720 | } |
3721 | |
3722 | return false; |
3723 | } |
3724 | |
3725 | static ExprResult BuildCXXCastArgument(Sema &S, |
3726 | SourceLocation CastLoc, |
3727 | QualType Ty, |
3728 | CastKind Kind, |
3729 | CXXMethodDecl *Method, |
3730 | DeclAccessPair FoundDecl, |
3731 | bool HadMultipleCandidates, |
3732 | Expr *From) { |
3733 | switch (Kind) { |
3734 | default: llvm_unreachable("Unhandled cast kind!"); |
3735 | case CK_ConstructorConversion: { |
3736 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method); |
3737 | SmallVector<Expr*, 8> ConstructorArgs; |
3738 | |
3739 | if (S.RequireNonAbstractType(CastLoc, Ty, |
3740 | diag::err_allocation_of_abstract_type)) |
3741 | return ExprError(); |
3742 | |
3743 | if (S.CompleteConstructorCall(Constructor, From, CastLoc, ConstructorArgs)) |
3744 | return ExprError(); |
3745 | |
3746 | S.CheckConstructorAccess(CastLoc, Constructor, FoundDecl, |
3747 | InitializedEntity::InitializeTemporary(Ty)); |
3748 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
3749 | return ExprError(); |
3750 | |
3751 | ExprResult Result = S.BuildCXXConstructExpr( |
3752 | CastLoc, Ty, FoundDecl, cast<CXXConstructorDecl>(Method), |
3753 | ConstructorArgs, HadMultipleCandidates, |
3754 | false, false, false, |
3755 | CXXConstructExpr::CK_Complete, SourceRange()); |
3756 | if (Result.isInvalid()) |
3757 | return ExprError(); |
3758 | |
3759 | return S.MaybeBindToTemporary(Result.getAs<Expr>()); |
3760 | } |
3761 | |
3762 | case CK_UserDefinedConversion: { |
3763 | (0) . __assert_fail ("!From->getType()->isPointerType() && \"Arg can't have pointer type!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3763, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
3764 | |
3765 | S.CheckMemberOperatorAccess(CastLoc, From, nullptr, FoundDecl); |
3766 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
3767 | return ExprError(); |
3768 | |
3769 | |
3770 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(Method); |
3771 | ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Conv, |
3772 | HadMultipleCandidates); |
3773 | if (Result.isInvalid()) |
3774 | return ExprError(); |
3775 | |
3776 | Result = ImplicitCastExpr::Create(S.Context, Result.get()->getType(), |
3777 | CK_UserDefinedConversion, Result.get(), |
3778 | nullptr, Result.get()->getValueKind()); |
3779 | |
3780 | return S.MaybeBindToTemporary(Result.get()); |
3781 | } |
3782 | } |
3783 | } |
3784 | |
3785 | |
3786 | |
3787 | |
3788 | |
3789 | |
3790 | ExprResult |
3791 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
3792 | const ImplicitConversionSequence &ICS, |
3793 | AssignmentAction Action, |
3794 | CheckedConversionKind CCK) { |
3795 | |
3796 | if (CCK == CCK_ForBuiltinOverloadedOp && !From->getType()->isRecordType()) |
3797 | return From; |
3798 | |
3799 | switch (ICS.getKind()) { |
3800 | case ImplicitConversionSequence::StandardConversion: { |
3801 | ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard, |
3802 | Action, CCK); |
3803 | if (Res.isInvalid()) |
3804 | return ExprError(); |
3805 | From = Res.get(); |
3806 | break; |
3807 | } |
3808 | |
3809 | case ImplicitConversionSequence::UserDefinedConversion: { |
3810 | |
3811 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
3812 | CastKind CastKind; |
3813 | QualType BeforeToType; |
3814 | (0) . __assert_fail ("FD && \"no conversion function for user-defined conversion seq\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3814, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FD && "no conversion function for user-defined conversion seq"); |
3815 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
3816 | CastKind = CK_UserDefinedConversion; |
3817 | |
3818 | |
3819 | |
3820 | |
3821 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
3822 | } else { |
3823 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); |
3824 | CastKind = CK_ConstructorConversion; |
3825 | |
3826 | if (!ICS.UserDefined.EllipsisConversion) { |
3827 | |
3828 | |
3829 | |
3830 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
3831 | } |
3832 | } |
3833 | |
3834 | if (!ICS.UserDefined.EllipsisConversion) { |
3835 | ExprResult Res = |
3836 | PerformImplicitConversion(From, BeforeToType, |
3837 | ICS.UserDefined.Before, AA_Converting, |
3838 | CCK); |
3839 | if (Res.isInvalid()) |
3840 | return ExprError(); |
3841 | From = Res.get(); |
3842 | } |
3843 | |
3844 | ExprResult CastArg = BuildCXXCastArgument( |
3845 | *this, From->getBeginLoc(), ToType.getNonReferenceType(), CastKind, |
3846 | cast<CXXMethodDecl>(FD), ICS.UserDefined.FoundConversionFunction, |
3847 | ICS.UserDefined.HadMultipleCandidates, From); |
3848 | |
3849 | if (CastArg.isInvalid()) |
3850 | return ExprError(); |
3851 | |
3852 | From = CastArg.get(); |
3853 | |
3854 | |
3855 | |
3856 | |
3857 | if (CCK == CCK_ForBuiltinOverloadedOp) |
3858 | return From; |
3859 | |
3860 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
3861 | AA_Converting, CCK); |
3862 | } |
3863 | |
3864 | case ImplicitConversionSequence::AmbiguousConversion: |
3865 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
3866 | PDiag(diag::err_typecheck_ambiguous_condition) |
3867 | << From->getSourceRange()); |
3868 | return ExprError(); |
3869 | |
3870 | case ImplicitConversionSequence::EllipsisConversion: |
3871 | llvm_unreachable("Cannot perform an ellipsis conversion"); |
3872 | |
3873 | case ImplicitConversionSequence::BadConversion: |
3874 | bool Diagnosed = |
3875 | DiagnoseAssignmentResult(Incompatible, From->getExprLoc(), ToType, |
3876 | From->getType(), From, Action); |
3877 | (0) . __assert_fail ("Diagnosed && \"failed to diagnose bad conversion\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3877, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Diagnosed && "failed to diagnose bad conversion"); (void)Diagnosed; |
3878 | return ExprError(); |
3879 | } |
3880 | |
3881 | |
3882 | return From; |
3883 | } |
3884 | |
3885 | |
3886 | |
3887 | |
3888 | |
3889 | |
3890 | ExprResult |
3891 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
3892 | const StandardConversionSequence& SCS, |
3893 | AssignmentAction Action, |
3894 | CheckedConversionKind CCK) { |
3895 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); |
3896 | |
3897 | |
3898 | |
3899 | |
3900 | |
3901 | QualType FromType = From->getType(); |
3902 | |
3903 | if (SCS.CopyConstructor) { |
3904 | |
3905 | isReferenceType()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3905, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!ToType->isReferenceType()); |
3906 | if (SCS.Second == ICK_Derived_To_Base) { |
3907 | SmallVector<Expr*, 8> ConstructorArgs; |
3908 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
3909 | From, SourceLocation(), |
3910 | ConstructorArgs)) |
3911 | return ExprError(); |
3912 | return BuildCXXConstructExpr( |
3913 | SourceLocation(), ToType, |
3914 | SCS.FoundCopyConstructor, SCS.CopyConstructor, |
3915 | ConstructorArgs, false, |
3916 | false, false, false, |
3917 | CXXConstructExpr::CK_Complete, SourceRange()); |
3918 | } |
3919 | return BuildCXXConstructExpr( |
3920 | SourceLocation(), ToType, |
3921 | SCS.FoundCopyConstructor, SCS.CopyConstructor, |
3922 | From, false, |
3923 | false, false, false, |
3924 | CXXConstructExpr::CK_Complete, SourceRange()); |
3925 | } |
3926 | |
3927 | |
3928 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
3929 | DeclAccessPair Found; |
3930 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
3931 | true, Found); |
3932 | if (!Fn) |
3933 | return ExprError(); |
3934 | |
3935 | if (DiagnoseUseOfDecl(Fn, From->getBeginLoc())) |
3936 | return ExprError(); |
3937 | |
3938 | From = FixOverloadedFunctionReference(From, Found, Fn); |
3939 | FromType = From->getType(); |
3940 | } |
3941 | |
3942 | |
3943 | |
3944 | QualType ToAtomicType; |
3945 | if (const AtomicType *ToAtomic = ToType->getAs<AtomicType>()) { |
3946 | ToAtomicType = ToType; |
3947 | ToType = ToAtomic->getValueType(); |
3948 | } |
3949 | |
3950 | QualType InitialFromType = FromType; |
3951 | |
3952 | switch (SCS.First) { |
3953 | case ICK_Identity: |
3954 | if (const AtomicType *FromAtomic = FromType->getAs<AtomicType>()) { |
3955 | FromType = FromAtomic->getValueType().getUnqualifiedType(); |
3956 | From = ImplicitCastExpr::Create(Context, FromType, CK_AtomicToNonAtomic, |
3957 | From, , VK_RValue); |
3958 | } |
3959 | break; |
3960 | |
3961 | case ICK_Lvalue_To_Rvalue: { |
3962 | getObjectKind() != OK_ObjCProperty", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3962, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(From->getObjectKind() != OK_ObjCProperty); |
3963 | ExprResult FromRes = DefaultLvalueConversion(From); |
3964 | (0) . __assert_fail ("!FromRes.isInvalid() && \"Can't perform deduced conversion?!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 3964, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!FromRes.isInvalid() && "Can't perform deduced conversion?!"); |
3965 | From = FromRes.get(); |
3966 | FromType = From->getType(); |
3967 | break; |
3968 | } |
3969 | |
3970 | case ICK_Array_To_Pointer: |
3971 | FromType = Context.getArrayDecayedType(FromType); |
3972 | From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay, |
3973 | VK_RValue, , CCK).get(); |
3974 | break; |
3975 | |
3976 | case ICK_Function_To_Pointer: |
3977 | FromType = Context.getPointerType(FromType); |
3978 | From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay, |
3979 | VK_RValue, , CCK).get(); |
3980 | break; |
3981 | |
3982 | default: |
3983 | llvm_unreachable("Improper first standard conversion"); |
3984 | } |
3985 | |
3986 | |
3987 | switch (SCS.Second) { |
3988 | case ICK_Identity: |
3989 | |
3990 | |
3991 | |
3992 | |
3993 | |
3994 | switch (Action) { |
3995 | case AA_Assigning: |
3996 | case AA_Initializing: |
3997 | |
3998 | case AA_Passing: |
3999 | case AA_Returning: |
4000 | case AA_Sending: |
4001 | case AA_Passing_CFAudited: |
4002 | if (CheckExceptionSpecCompatibility(From, ToType)) |
4003 | return ExprError(); |
4004 | break; |
4005 | |
4006 | case AA_Casting: |
4007 | case AA_Converting: |
4008 | |
4009 | |
4010 | break; |
4011 | } |
4012 | |
4013 | break; |
4014 | |
4015 | case ICK_Integral_Promotion: |
4016 | case ICK_Integral_Conversion: |
4017 | if (ToType->isBooleanType()) { |
4018 | (0) . __assert_fail ("FromType->castAs()->getDecl()->isFixed() && SCS.Second == ICK_Integral_Promotion && \"only enums with fixed underlying type can promote to bool\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 4020, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FromType->castAs<EnumType>()->getDecl()->isFixed() && |
4019 | (0) . __assert_fail ("FromType->castAs()->getDecl()->isFixed() && SCS.Second == ICK_Integral_Promotion && \"only enums with fixed underlying type can promote to bool\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 4020, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> SCS.Second == ICK_Integral_Promotion && |
4020 | (0) . __assert_fail ("FromType->castAs()->getDecl()->isFixed() && SCS.Second == ICK_Integral_Promotion && \"only enums with fixed underlying type can promote to bool\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 4020, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "only enums with fixed underlying type can promote to bool"); |
4021 | From = ImpCastExprToType(From, ToType, CK_IntegralToBoolean, |
4022 | VK_RValue, , CCK).get(); |
4023 | } else { |
4024 | From = ImpCastExprToType(From, ToType, CK_IntegralCast, |
4025 | VK_RValue, , CCK).get(); |
4026 | } |
4027 | break; |
4028 | |
4029 | case ICK_Floating_Promotion: |
4030 | case ICK_Floating_Conversion: |
4031 | From = ImpCastExprToType(From, ToType, CK_FloatingCast, |
4032 | VK_RValue, , CCK).get(); |
4033 | break; |
4034 | |
4035 | case ICK_Complex_Promotion: |
4036 | case ICK_Complex_Conversion: { |
4037 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); |
4038 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); |
4039 | CastKind CK; |
4040 | if (FromEl->isRealFloatingType()) { |
4041 | if (ToEl->isRealFloatingType()) |
4042 | CK = CK_FloatingComplexCast; |
4043 | else |
4044 | CK = CK_FloatingComplexToIntegralComplex; |
4045 | } else if (ToEl->isRealFloatingType()) { |
4046 | CK = CK_IntegralComplexToFloatingComplex; |
4047 | } else { |
4048 | CK = CK_IntegralComplexCast; |
4049 | } |
4050 | From = ImpCastExprToType(From, ToType, CK, |
4051 | VK_RValue, , CCK).get(); |
4052 | break; |
4053 | } |
4054 | |
4055 | case ICK_Floating_Integral: |
4056 | if (ToType->isRealFloatingType()) |
4057 | From = ImpCastExprToType(From, ToType, CK_IntegralToFloating, |
4058 | VK_RValue, , CCK).get(); |
4059 | else |
4060 | From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral, |
4061 | VK_RValue, , CCK).get(); |
4062 | break; |
4063 | |
4064 | case ICK_Compatible_Conversion: |
4065 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
4066 | VK_RValue, , CCK).get(); |
4067 | break; |
4068 | |
4069 | case ICK_Writeback_Conversion: |
4070 | case ICK_Pointer_Conversion: { |
4071 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
4072 | |
4073 | if (Action == AA_Initializing || Action == AA_Assigning) |
4074 | Diag(From->getBeginLoc(), |
4075 | diag::ext_typecheck_convert_incompatible_pointer) |
4076 | << ToType << From->getType() << Action << From->getSourceRange() |
4077 | << 0; |
4078 | else |
4079 | Diag(From->getBeginLoc(), |
4080 | diag::ext_typecheck_convert_incompatible_pointer) |
4081 | << From->getType() << ToType << Action << From->getSourceRange() |
4082 | << 0; |
4083 | |
4084 | if (From->getType()->isObjCObjectPointerType() && |
4085 | ToType->isObjCObjectPointerType()) |
4086 | EmitRelatedResultTypeNote(From); |
4087 | } else if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && |
4088 | !CheckObjCARCUnavailableWeakConversion(ToType, |
4089 | From->getType())) { |
4090 | if (Action == AA_Initializing) |
4091 | Diag(From->getBeginLoc(), diag::err_arc_weak_unavailable_assign); |
4092 | else |
4093 | Diag(From->getBeginLoc(), diag::err_arc_convesion_of_weak_unavailable) |
4094 | << (Action == AA_Casting) << From->getType() << ToType |
4095 | << From->getSourceRange(); |
4096 | } |
4097 | |
4098 | CastKind Kind; |
4099 | CXXCastPath BasePath; |
4100 | if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
4101 | return ExprError(); |
4102 | |
4103 | |
4104 | |
4105 | if (Kind == CK_BlockPointerToObjCPointerCast) { |
4106 | ExprResult E = From; |
4107 | (void) PrepareCastToObjCObjectPointer(E); |
4108 | From = E.get(); |
4109 | } |
4110 | if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) |
4111 | CheckObjCConversion(SourceRange(), ToType, From, CCK); |
4112 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
4113 | .get(); |
4114 | break; |
4115 | } |
4116 | |
4117 | case ICK_Pointer_Member: { |
4118 | CastKind Kind; |
4119 | CXXCastPath BasePath; |
4120 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
4121 | return ExprError(); |
4122 | if (CheckExceptionSpecCompatibility(From, ToType)) |
4123 | return ExprError(); |
4124 | |
4125 | |
4126 | |
4127 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
4128 | (void)isCompleteType(From->getExprLoc(), From->getType()); |
4129 | (void)isCompleteType(From->getExprLoc(), ToType); |
4130 | } |
4131 | |
4132 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
4133 | .get(); |
4134 | break; |
4135 | } |
4136 | |
4137 | case ICK_Boolean_Conversion: |
4138 | |
4139 | if (From->getType()->isHalfType()) { |
4140 | From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).get(); |
4141 | FromType = Context.FloatTy; |
4142 | } |
4143 | |
4144 | From = ImpCastExprToType(From, Context.BoolTy, |
4145 | ScalarTypeToBooleanCastKind(FromType), |
4146 | VK_RValue, , CCK).get(); |
4147 | break; |
4148 | |
4149 | case ICK_Derived_To_Base: { |
4150 | CXXCastPath BasePath; |
4151 | if (CheckDerivedToBaseConversion( |
4152 | From->getType(), ToType.getNonReferenceType(), From->getBeginLoc(), |
4153 | From->getSourceRange(), &BasePath, CStyle)) |
4154 | return ExprError(); |
4155 | |
4156 | From = ImpCastExprToType(From, ToType.getNonReferenceType(), |
4157 | CK_DerivedToBase, From->getValueKind(), |
4158 | &BasePath, CCK).get(); |
4159 | break; |
4160 | } |
4161 | |
4162 | case ICK_Vector_Conversion: |
4163 | From = ImpCastExprToType(From, ToType, CK_BitCast, |
4164 | VK_RValue, , CCK).get(); |
4165 | break; |
4166 | |
4167 | case ICK_Vector_Splat: { |
4168 | |
4169 | Expr *Elem = prepareVectorSplat(ToType, From).get(); |
4170 | From = ImpCastExprToType(Elem, ToType, CK_VectorSplat, VK_RValue, |
4171 | , CCK).get(); |
4172 | break; |
4173 | } |
4174 | |
4175 | case ICK_Complex_Real: |
4176 | |
4177 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
4178 | QualType ElType = ToComplex->getElementType(); |
4179 | bool isFloatingComplex = ElType->isRealFloatingType(); |
4180 | |
4181 | |
4182 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { |
4183 | |
4184 | } else if (From->getType()->isRealFloatingType()) { |
4185 | From = ImpCastExprToType(From, ElType, |
4186 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).get(); |
4187 | } else { |
4188 | getType()->isIntegerType()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 4188, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(From->getType()->isIntegerType()); |
4189 | From = ImpCastExprToType(From, ElType, |
4190 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).get(); |
4191 | } |
4192 | |
4193 | From = ImpCastExprToType(From, ToType, |
4194 | isFloatingComplex ? CK_FloatingRealToComplex |
4195 | : CK_IntegralRealToComplex).get(); |
4196 | |
4197 | |
4198 | } else { |
4199 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); |
4200 | assert(FromComplex); |
4201 | |
4202 | QualType ElType = FromComplex->getElementType(); |
4203 | bool isFloatingComplex = ElType->isRealFloatingType(); |
4204 | |
4205 | |
4206 | From = ImpCastExprToType(From, ElType, |
4207 | isFloatingComplex ? CK_FloatingComplexToReal |
4208 | : CK_IntegralComplexToReal, |
4209 | VK_RValue, , CCK).get(); |
4210 | |
4211 | |
4212 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { |
4213 | |
4214 | } else if (ToType->isRealFloatingType()) { |
4215 | From = ImpCastExprToType(From, ToType, |
4216 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating, |
4217 | VK_RValue, , CCK).get(); |
4218 | } else { |
4219 | isIntegerType()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 4219, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ToType->isIntegerType()); |
4220 | From = ImpCastExprToType(From, ToType, |
4221 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast, |
4222 | VK_RValue, , CCK).get(); |
4223 | } |
4224 | } |
4225 | break; |
4226 | |
4227 | case ICK_Block_Pointer_Conversion: { |
4228 | From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast, |
4229 | VK_RValue, , CCK).get(); |
4230 | break; |
4231 | } |
4232 | |
4233 | case ICK_TransparentUnionConversion: { |
4234 | ExprResult FromRes = From; |
4235 | Sema::AssignConvertType ConvTy = |
4236 | CheckTransparentUnionArgumentConstraints(ToType, FromRes); |
4237 | if (FromRes.isInvalid()) |
4238 | return ExprError(); |
4239 | From = FromRes.get(); |
4240 | (0) . __assert_fail ("(ConvTy == Sema..Compatible) && \"Improper transparent union conversion\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 4241, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert ((ConvTy == Sema::Compatible) && |
4241 | (0) . __assert_fail ("(ConvTy == Sema..Compatible) && \"Improper transparent union conversion\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 4241, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Improper transparent union conversion"); |
4242 | (void)ConvTy; |
4243 | break; |
4244 | } |
4245 | |
4246 | case ICK_Zero_Event_Conversion: |
4247 | case ICK_Zero_Queue_Conversion: |
4248 | From = ImpCastExprToType(From, ToType, |
4249 | CK_ZeroToOCLOpaqueType, |
4250 | From->getValueKind()).get(); |
4251 | break; |
4252 | |
4253 | case ICK_Lvalue_To_Rvalue: |
4254 | case ICK_Array_To_Pointer: |
4255 | case ICK_Function_To_Pointer: |
4256 | case ICK_Function_Conversion: |
4257 | case ICK_Qualification: |
4258 | case ICK_Num_Conversion_Kinds: |
4259 | case ICK_C_Only_Conversion: |
4260 | case ICK_Incompatible_Pointer_Conversion: |
4261 | llvm_unreachable("Improper second standard conversion"); |
4262 | } |
4263 | |
4264 | switch (SCS.Third) { |
4265 | case ICK_Identity: |
4266 | |
4267 | break; |
4268 | |
4269 | case ICK_Function_Conversion: |
4270 | |
4271 | |
4272 | if (CheckExceptionSpecCompatibility(From, ToType)) |
4273 | return ExprError(); |
4274 | |
4275 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
4276 | VK_RValue, , CCK).get(); |
4277 | break; |
4278 | |
4279 | case ICK_Qualification: { |
4280 | |
4281 | |
4282 | ExprValueKind VK = |
4283 | ToType->isReferenceType() ? From->getValueKind() : VK_RValue; |
4284 | |
4285 | CastKind CK = CK_NoOp; |
4286 | |
4287 | if (ToType->isReferenceType() && |
4288 | ToType->getPointeeType().getAddressSpace() != |
4289 | From->getType().getAddressSpace()) |
4290 | CK = CK_AddressSpaceConversion; |
4291 | |
4292 | if (ToType->isPointerType() && |
4293 | ToType->getPointeeType().getAddressSpace() != |
4294 | From->getType()->getPointeeType().getAddressSpace()) |
4295 | CK = CK_AddressSpaceConversion; |
4296 | |
4297 | From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context), CK, VK, |
4298 | , CCK) |
4299 | .get(); |
4300 | |
4301 | if (SCS.DeprecatedStringLiteralToCharPtr && |
4302 | !getLangOpts().WritableStrings) { |
4303 | Diag(From->getBeginLoc(), |
4304 | getLangOpts().CPlusPlus11 |
4305 | ? diag::ext_deprecated_string_literal_conversion |
4306 | : diag::warn_deprecated_string_literal_conversion) |
4307 | << ToType.getNonReferenceType(); |
4308 | } |
4309 | |
4310 | break; |
4311 | } |
4312 | |
4313 | default: |
4314 | llvm_unreachable("Improper third standard conversion"); |
4315 | } |
4316 | |
4317 | |
4318 | |
4319 | if (!ToAtomicType.isNull()) { |
4320 | castAs()->getValueType(), From->getType())", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 4321, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Context.hasSameType( |
4321 | castAs()->getValueType(), From->getType())", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 4321, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ToAtomicType->castAs<AtomicType>()->getValueType(), From->getType())); |
4322 | From = ImpCastExprToType(From, ToAtomicType, CK_NonAtomicToAtomic, |
4323 | VK_RValue, nullptr, CCK).get(); |
4324 | } |
4325 | |
4326 | |
4327 | |
4328 | if (!isCast(CCK)) |
4329 | diagnoseNullableToNonnullConversion(ToType, InitialFromType, |
4330 | From->getBeginLoc()); |
4331 | |
4332 | return From; |
4333 | } |
4334 | |
4335 | |
4336 | |
4337 | |
4338 | |
4339 | |
4340 | |
4341 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT, |
4342 | SourceLocation Loc, |
4343 | QualType ArgTy) { |
4344 | |
4345 | |
4346 | |
4347 | |
4348 | |
4349 | |
4350 | |
4351 | |
4352 | |
4353 | switch (UTT) { |
4354 | default: llvm_unreachable("not a UTT"); |
4355 | |
4356 | case UTT_IsCompleteType: |
4357 | |
4358 | |
4359 | |
4360 | |
4361 | |
4362 | |
4363 | case UTT_IsVoid: |
4364 | case UTT_IsIntegral: |
4365 | case UTT_IsFloatingPoint: |
4366 | case UTT_IsArray: |
4367 | case UTT_IsPointer: |
4368 | case UTT_IsLvalueReference: |
4369 | case UTT_IsRvalueReference: |
4370 | case UTT_IsMemberFunctionPointer: |
4371 | case UTT_IsMemberObjectPointer: |
4372 | case UTT_IsEnum: |
4373 | case UTT_IsUnion: |
4374 | case UTT_IsClass: |
4375 | case UTT_IsFunction: |
4376 | case UTT_IsReference: |
4377 | case UTT_IsArithmetic: |
4378 | case UTT_IsFundamental: |
4379 | case UTT_IsObject: |
4380 | case UTT_IsScalar: |
4381 | case UTT_IsCompound: |
4382 | case UTT_IsMemberPointer: |
4383 | |
4384 | |
4385 | |
4386 | |
4387 | |
4388 | |
4389 | |
4390 | case UTT_IsConst: |
4391 | case UTT_IsVolatile: |
4392 | case UTT_IsSigned: |
4393 | case UTT_IsUnsigned: |
4394 | |
4395 | |
4396 | case UTT_IsInterfaceClass: |
4397 | return true; |
4398 | |
4399 | |
4400 | |
4401 | case UTT_IsEmpty: |
4402 | case UTT_IsPolymorphic: |
4403 | case UTT_IsAbstract: |
4404 | if (const auto *RD = ArgTy->getAsCXXRecordDecl()) |
4405 | if (!RD->isUnion()) |
4406 | return !S.RequireCompleteType( |
4407 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
4408 | return true; |
4409 | |
4410 | |
4411 | |
4412 | case UTT_IsFinal: |
4413 | case UTT_IsSealed: |
4414 | if (ArgTy->getAsCXXRecordDecl()) |
4415 | return !S.RequireCompleteType( |
4416 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
4417 | return true; |
4418 | |
4419 | |
4420 | |
4421 | case UTT_IsAggregate: |
4422 | case UTT_IsTrivial: |
4423 | case UTT_IsTriviallyCopyable: |
4424 | case UTT_IsStandardLayout: |
4425 | case UTT_IsPOD: |
4426 | case UTT_IsLiteral: |
4427 | |
4428 | |
4429 | |
4430 | case UTT_HasNothrowAssign: |
4431 | case UTT_HasNothrowMoveAssign: |
4432 | case UTT_HasNothrowConstructor: |
4433 | case UTT_HasNothrowCopy: |
4434 | case UTT_HasTrivialAssign: |
4435 | case UTT_HasTrivialMoveAssign: |
4436 | case UTT_HasTrivialDefaultConstructor: |
4437 | case UTT_HasTrivialMoveConstructor: |
4438 | case UTT_HasTrivialCopy: |
4439 | case UTT_HasTrivialDestructor: |
4440 | case UTT_HasVirtualDestructor: |
4441 | ArgTy = QualType(ArgTy->getBaseElementTypeUnsafe(), 0); |
4442 | LLVM_FALLTHROUGH; |
4443 | |
4444 | |
4445 | |
4446 | case UTT_IsDestructible: |
4447 | case UTT_IsNothrowDestructible: |
4448 | case UTT_IsTriviallyDestructible: |
4449 | case UTT_HasUniqueObjectRepresentations: |
4450 | if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType()) |
4451 | return true; |
4452 | |
4453 | return !S.RequireCompleteType( |
4454 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
4455 | } |
4456 | } |
4457 | |
4458 | static bool HasNoThrowOperator(const RecordType *RT, OverloadedOperatorKind Op, |
4459 | Sema &Self, SourceLocation KeyLoc, ASTContext &C, |
4460 | bool (CXXRecordDecl::*HasTrivial)() const, |
4461 | bool (CXXRecordDecl::*HasNonTrivial)() const, |
4462 | bool (CXXMethodDecl::*IsDesiredOp)() const) |
4463 | { |
4464 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
4465 | if ((RD->*HasTrivial)() && !(RD->*HasNonTrivial)()) |
4466 | return true; |
4467 | |
4468 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(Op); |
4469 | DeclarationNameInfo NameInfo(Name, KeyLoc); |
4470 | LookupResult Res(Self, NameInfo, Sema::LookupOrdinaryName); |
4471 | if (Self.LookupQualifiedName(Res, RD)) { |
4472 | bool FoundOperator = false; |
4473 | Res.suppressDiagnostics(); |
4474 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
4475 | Op != OpEnd; ++Op) { |
4476 | if (isa<FunctionTemplateDecl>(*Op)) |
4477 | continue; |
4478 | |
4479 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
4480 | if((Operator->*IsDesiredOp)()) { |
4481 | FoundOperator = true; |
4482 | const FunctionProtoType *CPT = |
4483 | Operator->getType()->getAs<FunctionProtoType>(); |
4484 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
4485 | if (!CPT || !CPT->isNothrow()) |
4486 | return false; |
4487 | } |
4488 | } |
4489 | return FoundOperator; |
4490 | } |
4491 | return false; |
4492 | } |
4493 | |
4494 | static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT, |
4495 | SourceLocation KeyLoc, QualType T) { |
4496 | (0) . __assert_fail ("!T->isDependentType() && \"Cannot evaluate traits of dependent type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 4496, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
4497 | |
4498 | ASTContext &C = Self.Context; |
4499 | switch(UTT) { |
4500 | default: llvm_unreachable("not a UTT"); |
4501 | |
4502 | |
4503 | case UTT_IsVoid: |
4504 | return T->isVoidType(); |
4505 | case UTT_IsIntegral: |
4506 | return T->isIntegralType(C); |
4507 | case UTT_IsFloatingPoint: |
4508 | return T->isFloatingType(); |
4509 | case UTT_IsArray: |
4510 | return T->isArrayType(); |
4511 | case UTT_IsPointer: |
4512 | return T->isPointerType(); |
4513 | case UTT_IsLvalueReference: |
4514 | return T->isLValueReferenceType(); |
4515 | case UTT_IsRvalueReference: |
4516 | return T->isRValueReferenceType(); |
4517 | case UTT_IsMemberFunctionPointer: |
4518 | return T->isMemberFunctionPointerType(); |
4519 | case UTT_IsMemberObjectPointer: |
4520 | return T->isMemberDataPointerType(); |
4521 | case UTT_IsEnum: |
4522 | return T->isEnumeralType(); |
4523 | case UTT_IsUnion: |
4524 | return T->isUnionType(); |
4525 | case UTT_IsClass: |
4526 | return T->isClassType() || T->isStructureType() || T->isInterfaceType(); |
4527 | case UTT_IsFunction: |
4528 | return T->isFunctionType(); |
4529 | |
4530 | |
4531 | |
4532 | case UTT_IsReference: |
4533 | return T->isReferenceType(); |
4534 | case UTT_IsArithmetic: |
4535 | return T->isArithmeticType() && !T->isEnumeralType(); |
4536 | case UTT_IsFundamental: |
4537 | return T->isFundamentalType(); |
4538 | case UTT_IsObject: |
4539 | return T->isObjectType(); |
4540 | case UTT_IsScalar: |
4541 | |
4542 | |
4543 | |
4544 | |
4545 | if (T->isObjCLifetimeType()) { |
4546 | switch (T.getObjCLifetime()) { |
4547 | case Qualifiers::OCL_None: |
4548 | case Qualifiers::OCL_ExplicitNone: |
4549 | return true; |
4550 | |
4551 | case Qualifiers::OCL_Strong: |
4552 | case Qualifiers::OCL_Weak: |
4553 | case Qualifiers::OCL_Autoreleasing: |
4554 | return false; |
4555 | } |
4556 | } |
4557 | |
4558 | return T->isScalarType(); |
4559 | case UTT_IsCompound: |
4560 | return T->isCompoundType(); |
4561 | case UTT_IsMemberPointer: |
4562 | return T->isMemberPointerType(); |
4563 | |
4564 | |
4565 | |
4566 | case UTT_IsConst: |
4567 | return T.isConstQualified(); |
4568 | case UTT_IsVolatile: |
4569 | return T.isVolatileQualified(); |
4570 | case UTT_IsTrivial: |
4571 | return T.isTrivialType(C); |
4572 | case UTT_IsTriviallyCopyable: |
4573 | return T.isTriviallyCopyableType(C); |
4574 | case UTT_IsStandardLayout: |
4575 | return T->isStandardLayoutType(); |
4576 | case UTT_IsPOD: |
4577 | return T.isPODType(C); |
4578 | case UTT_IsLiteral: |
4579 | return T->isLiteralType(C); |
4580 | case UTT_IsEmpty: |
4581 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
4582 | return !RD->isUnion() && RD->isEmpty(); |
4583 | return false; |
4584 | case UTT_IsPolymorphic: |
4585 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
4586 | return !RD->isUnion() && RD->isPolymorphic(); |
4587 | return false; |
4588 | case UTT_IsAbstract: |
4589 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
4590 | return !RD->isUnion() && RD->isAbstract(); |
4591 | return false; |
4592 | case UTT_IsAggregate: |
4593 | |
4594 | |
4595 | |
4596 | return T->isAggregateType() || T->isVectorType() || T->isExtVectorType() || |
4597 | T->isAnyComplexType(); |
4598 | |
4599 | |
4600 | |
4601 | case UTT_IsInterfaceClass: |
4602 | return false; |
4603 | case UTT_IsFinal: |
4604 | case UTT_IsSealed: |
4605 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
4606 | return RD->hasAttr<FinalAttr>(); |
4607 | return false; |
4608 | case UTT_IsSigned: |
4609 | return T->isSignedIntegerType(); |
4610 | case UTT_IsUnsigned: |
4611 | return T->isUnsignedIntegerType(); |
4612 | |
4613 | |
4614 | |
4615 | |
4616 | |
4617 | |
4618 | |
4619 | |
4620 | |
4621 | |
4622 | |
4623 | |
4624 | |
4625 | |
4626 | |
4627 | |
4628 | case UTT_HasTrivialDefaultConstructor: |
4629 | |
4630 | |
4631 | |
4632 | |
4633 | if (T.isPODType(C)) |
4634 | return true; |
4635 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
4636 | return RD->hasTrivialDefaultConstructor() && |
4637 | !RD->hasNonTrivialDefaultConstructor(); |
4638 | return false; |
4639 | case UTT_HasTrivialMoveConstructor: |
4640 | |
4641 | |
4642 | |
4643 | if (T.isPODType(C)) |
4644 | return true; |
4645 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
4646 | return RD->hasTrivialMoveConstructor() && !RD->hasNonTrivialMoveConstructor(); |
4647 | return false; |
4648 | case UTT_HasTrivialCopy: |
4649 | |
4650 | |
4651 | |
4652 | |
4653 | |
4654 | if (T.isPODType(C) || T->isReferenceType()) |
4655 | return true; |
4656 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
4657 | return RD->hasTrivialCopyConstructor() && |
4658 | !RD->hasNonTrivialCopyConstructor(); |
4659 | return false; |
4660 | case UTT_HasTrivialMoveAssign: |
4661 | |
4662 | |
4663 | |
4664 | if (T.isPODType(C)) |
4665 | return true; |
4666 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
4667 | return RD->hasTrivialMoveAssignment() && !RD->hasNonTrivialMoveAssignment(); |
4668 | return false; |
4669 | case UTT_HasTrivialAssign: |
4670 | |
4671 | |
4672 | |
4673 | |
4674 | |
4675 | |
4676 | |
4677 | |
4678 | |
4679 | |
4680 | |
4681 | |
4682 | if (T.isConstQualified()) |
4683 | return false; |
4684 | if (T.isPODType(C)) |
4685 | return true; |
4686 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
4687 | return RD->hasTrivialCopyAssignment() && |
4688 | !RD->hasNonTrivialCopyAssignment(); |
4689 | return false; |
4690 | case UTT_IsDestructible: |
4691 | case UTT_IsTriviallyDestructible: |
4692 | case UTT_IsNothrowDestructible: |
4693 | |
4694 | |
4695 | if (T->isReferenceType()) |
4696 | return true; |
4697 | |
4698 | |
4699 | if (T->isObjCLifetimeType() && |
4700 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
4701 | return true; |
4702 | |
4703 | |
4704 | |
4705 | |
4706 | if (T->isIncompleteType() || T->isFunctionType()) |
4707 | return false; |
4708 | |
4709 | |
4710 | |
4711 | if (UTT == UTT_IsTriviallyDestructible && T.isDestructedType()) |
4712 | return false; |
4713 | |
4714 | |
4715 | |
4716 | |
4717 | |
4718 | if (auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) { |
4719 | CXXDestructorDecl *Destructor = Self.LookupDestructor(RD); |
4720 | if (!Destructor) |
4721 | return false; |
4722 | |
4723 | |
4724 | |
4725 | if (Destructor->isDeleted()) |
4726 | return false; |
4727 | if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public) |
4728 | return false; |
4729 | if (UTT == UTT_IsNothrowDestructible) { |
4730 | const FunctionProtoType *CPT = |
4731 | Destructor->getType()->getAs<FunctionProtoType>(); |
4732 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
4733 | if (!CPT || !CPT->isNothrow()) |
4734 | return false; |
4735 | } |
4736 | } |
4737 | return true; |
4738 | |
4739 | case UTT_HasTrivialDestructor: |
4740 | |
4741 | |
4742 | |
4743 | |
4744 | |
4745 | |
4746 | if (T.isPODType(C) || T->isReferenceType()) |
4747 | return true; |
4748 | |
4749 | |
4750 | if (T->isObjCLifetimeType() && |
4751 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
4752 | return true; |
4753 | |
4754 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
4755 | return RD->hasTrivialDestructor(); |
4756 | return false; |
4757 | |
4758 | case UTT_HasNothrowAssign: |
4759 | |
4760 | |
4761 | |
4762 | |
4763 | |
4764 | |
4765 | |
4766 | if (C.getBaseElementType(T).isConstQualified()) |
4767 | return false; |
4768 | if (T->isReferenceType()) |
4769 | return false; |
4770 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
4771 | return true; |
4772 | |
4773 | if (const RecordType *RT = T->getAs<RecordType>()) |
4774 | return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C, |
4775 | &CXXRecordDecl::hasTrivialCopyAssignment, |
4776 | &CXXRecordDecl::hasNonTrivialCopyAssignment, |
4777 | &CXXMethodDecl::isCopyAssignmentOperator); |
4778 | return false; |
4779 | case UTT_HasNothrowMoveAssign: |
4780 | |
4781 | |
4782 | |
4783 | if (T.isPODType(C)) |
4784 | return true; |
4785 | |
4786 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) |
4787 | return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C, |
4788 | &CXXRecordDecl::hasTrivialMoveAssignment, |
4789 | &CXXRecordDecl::hasNonTrivialMoveAssignment, |
4790 | &CXXMethodDecl::isMoveAssignmentOperator); |
4791 | return false; |
4792 | case UTT_HasNothrowCopy: |
4793 | |
4794 | |
4795 | |
4796 | |
4797 | |
4798 | if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType()) |
4799 | return true; |
4800 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
4801 | if (RD->hasTrivialCopyConstructor() && |
4802 | !RD->hasNonTrivialCopyConstructor()) |
4803 | return true; |
4804 | |
4805 | bool FoundConstructor = false; |
4806 | unsigned FoundTQs; |
4807 | for (const auto *ND : Self.LookupConstructors(RD)) { |
4808 | |
4809 | |
4810 | |
4811 | if (isa<FunctionTemplateDecl>(ND->getUnderlyingDecl())) |
4812 | continue; |
4813 | |
4814 | if (isa<UsingDecl>(ND)) |
4815 | continue; |
4816 | auto *Constructor = cast<CXXConstructorDecl>(ND->getUnderlyingDecl()); |
4817 | if (Constructor->isCopyConstructor(FoundTQs)) { |
4818 | FoundConstructor = true; |
4819 | const FunctionProtoType *CPT |
4820 | = Constructor->getType()->getAs<FunctionProtoType>(); |
4821 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
4822 | if (!CPT) |
4823 | return false; |
4824 | |
4825 | |
4826 | if (!CPT->isNothrow() || CPT->getNumParams() > 1) |
4827 | return false; |
4828 | } |
4829 | } |
4830 | |
4831 | return FoundConstructor; |
4832 | } |
4833 | return false; |
4834 | case UTT_HasNothrowConstructor: |
4835 | |
4836 | |
4837 | |
4838 | |
4839 | |
4840 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
4841 | return true; |
4842 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) { |
4843 | if (RD->hasTrivialDefaultConstructor() && |
4844 | !RD->hasNonTrivialDefaultConstructor()) |
4845 | return true; |
4846 | |
4847 | bool FoundConstructor = false; |
4848 | for (const auto *ND : Self.LookupConstructors(RD)) { |
4849 | |
4850 | if (isa<FunctionTemplateDecl>(ND->getUnderlyingDecl())) |
4851 | continue; |
4852 | |
4853 | if (isa<UsingDecl>(ND)) |
4854 | continue; |
4855 | auto *Constructor = cast<CXXConstructorDecl>(ND->getUnderlyingDecl()); |
4856 | if (Constructor->isDefaultConstructor()) { |
4857 | FoundConstructor = true; |
4858 | const FunctionProtoType *CPT |
4859 | = Constructor->getType()->getAs<FunctionProtoType>(); |
4860 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
4861 | if (!CPT) |
4862 | return false; |
4863 | |
4864 | |
4865 | if (!CPT->isNothrow() || CPT->getNumParams() > 0) |
4866 | return false; |
4867 | } |
4868 | } |
4869 | return FoundConstructor; |
4870 | } |
4871 | return false; |
4872 | case UTT_HasVirtualDestructor: |
4873 | |
4874 | |
4875 | |
4876 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
4877 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
4878 | return Destructor->isVirtual(); |
4879 | return false; |
4880 | |
4881 | |
4882 | |
4883 | |
4884 | case UTT_IsCompleteType: |
4885 | |
4886 | |
4887 | |
4888 | return !T->isIncompleteType(); |
4889 | case UTT_HasUniqueObjectRepresentations: |
4890 | return C.hasUniqueObjectRepresentations(T); |
4891 | } |
4892 | } |
4893 | |
4894 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
4895 | QualType RhsT, SourceLocation KeyLoc); |
4896 | |
4897 | static bool evaluateTypeTrait(Sema &S, TypeTrait Kind, SourceLocation KWLoc, |
4898 | ArrayRef<TypeSourceInfo *> Args, |
4899 | SourceLocation RParenLoc) { |
4900 | if (Kind <= UTT_Last) |
4901 | return EvaluateUnaryTypeTrait(S, Kind, KWLoc, Args[0]->getType()); |
4902 | |
4903 | |
4904 | |
4905 | if (Kind <= BTT_Last && Kind != BTT_ReferenceBindsToTemporary) |
4906 | return EvaluateBinaryTypeTrait(S, Kind, Args[0]->getType(), |
4907 | Args[1]->getType(), RParenLoc); |
4908 | |
4909 | switch (Kind) { |
4910 | case clang::BTT_ReferenceBindsToTemporary: |
4911 | case clang::TT_IsConstructible: |
4912 | case clang::TT_IsNothrowConstructible: |
4913 | case clang::TT_IsTriviallyConstructible: { |
4914 | |
4915 | |
4916 | |
4917 | |
4918 | |
4919 | |
4920 | |
4921 | |
4922 | |
4923 | |
4924 | |
4925 | |
4926 | |
4927 | assert(!Args.empty()); |
4928 | |
4929 | |
4930 | |
4931 | |
4932 | for (const auto *TSI : Args) { |
4933 | QualType ArgTy = TSI->getType(); |
4934 | if (ArgTy->isVoidType() || ArgTy->isIncompleteArrayType()) |
4935 | continue; |
4936 | |
4937 | if (S.RequireCompleteType(KWLoc, ArgTy, |
4938 | diag::err_incomplete_type_used_in_type_trait_expr)) |
4939 | return false; |
4940 | } |
4941 | |
4942 | |
4943 | QualType T = Args[0]->getType(); |
4944 | if (T->isIncompleteType() || T->isFunctionType()) |
4945 | return false; |
4946 | |
4947 | |
4948 | CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
4949 | if (RD && RD->isAbstract()) |
4950 | return false; |
4951 | |
4952 | SmallVector<OpaqueValueExpr, 2> OpaqueArgExprs; |
4953 | SmallVector<Expr *, 2> ArgExprs; |
4954 | ArgExprs.reserve(Args.size() - 1); |
4955 | for (unsigned I = 1, N = Args.size(); I != N; ++I) { |
4956 | QualType ArgTy = Args[I]->getType(); |
4957 | if (ArgTy->isObjectType() || ArgTy->isFunctionType()) |
4958 | ArgTy = S.Context.getRValueReferenceType(ArgTy); |
4959 | OpaqueArgExprs.push_back( |
4960 | OpaqueValueExpr(Args[I]->getTypeLoc().getBeginLoc(), |
4961 | ArgTy.getNonLValueExprType(S.Context), |
4962 | Expr::getValueKindForType(ArgTy))); |
4963 | } |
4964 | for (Expr &E : OpaqueArgExprs) |
4965 | ArgExprs.push_back(&E); |
4966 | |
4967 | |
4968 | |
4969 | EnterExpressionEvaluationContext Unevaluated( |
4970 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
4971 | Sema::SFINAETrap SFINAE(S, ); |
4972 | Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl()); |
4973 | InitializedEntity To(InitializedEntity::InitializeTemporary(Args[0])); |
4974 | InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc, |
4975 | RParenLoc)); |
4976 | InitializationSequence Init(S, To, InitKind, ArgExprs); |
4977 | if (Init.Failed()) |
4978 | return false; |
4979 | |
4980 | ExprResult Result = Init.Perform(S, To, InitKind, ArgExprs); |
4981 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
4982 | return false; |
4983 | |
4984 | if (Kind == clang::TT_IsConstructible) |
4985 | return true; |
4986 | |
4987 | if (Kind == clang::BTT_ReferenceBindsToTemporary) { |
4988 | if (!T->isReferenceType()) |
4989 | return false; |
4990 | |
4991 | return !Init.isDirectReferenceBinding(); |
4992 | } |
4993 | |
4994 | if (Kind == clang::TT_IsNothrowConstructible) |
4995 | return S.canThrow(Result.get()) == CT_Cannot; |
4996 | |
4997 | if (Kind == clang::TT_IsTriviallyConstructible) { |
4998 | |
4999 | |
5000 | if (T.getNonReferenceType().hasNonTrivialObjCLifetime()) |
5001 | return false; |
5002 | |
5003 | |
5004 | |
5005 | return !Result.get()->hasNonTrivialCall(S.Context); |
5006 | } |
5007 | |
5008 | llvm_unreachable("unhandled type trait"); |
5009 | return false; |
5010 | } |
5011 | default: llvm_unreachable("not a TT"); |
5012 | } |
5013 | |
5014 | return false; |
5015 | } |
5016 | |
5017 | ExprResult Sema::BuildTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
5018 | ArrayRef<TypeSourceInfo *> Args, |
5019 | SourceLocation RParenLoc) { |
5020 | QualType ResultType = Context.getLogicalOperationType(); |
5021 | |
5022 | if (Kind <= UTT_Last && !CheckUnaryTypeTraitTypeCompleteness( |
5023 | *this, Kind, KWLoc, Args[0]->getType())) |
5024 | return ExprError(); |
5025 | |
5026 | bool Dependent = false; |
5027 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
5028 | if (Args[I]->getType()->isDependentType()) { |
5029 | Dependent = true; |
5030 | break; |
5031 | } |
5032 | } |
5033 | |
5034 | bool Result = false; |
5035 | if (!Dependent) |
5036 | Result = evaluateTypeTrait(*this, Kind, KWLoc, Args, RParenLoc); |
5037 | |
5038 | return TypeTraitExpr::Create(Context, ResultType, KWLoc, Kind, Args, |
5039 | RParenLoc, Result); |
5040 | } |
5041 | |
5042 | ExprResult Sema::ActOnTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
5043 | ArrayRef<ParsedType> Args, |
5044 | SourceLocation RParenLoc) { |
5045 | SmallVector<TypeSourceInfo *, 4> ConvertedArgs; |
5046 | ConvertedArgs.reserve(Args.size()); |
5047 | |
5048 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
5049 | TypeSourceInfo *TInfo; |
5050 | QualType T = GetTypeFromParser(Args[I], &TInfo); |
5051 | if (!TInfo) |
5052 | TInfo = Context.getTrivialTypeSourceInfo(T, KWLoc); |
5053 | |
5054 | ConvertedArgs.push_back(TInfo); |
5055 | } |
5056 | |
5057 | return BuildTypeTrait(Kind, KWLoc, ConvertedArgs, RParenLoc); |
5058 | } |
5059 | |
5060 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
5061 | QualType RhsT, SourceLocation KeyLoc) { |
5062 | (0) . __assert_fail ("!LhsT->isDependentType() && !RhsT->isDependentType() && \"Cannot evaluate traits of dependent types\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5063, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!LhsT->isDependentType() && !RhsT->isDependentType() && |
5063 | (0) . __assert_fail ("!LhsT->isDependentType() && !RhsT->isDependentType() && \"Cannot evaluate traits of dependent types\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5063, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Cannot evaluate traits of dependent types"); |
5064 | |
5065 | switch(BTT) { |
5066 | case BTT_IsBaseOf: { |
5067 | |
5068 | |
5069 | |
5070 | |
5071 | |
5072 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); |
5073 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); |
5074 | if (!rhsRecord || !lhsRecord) { |
5075 | const ObjCObjectType *LHSObjTy = LhsT->getAs<ObjCObjectType>(); |
5076 | const ObjCObjectType *RHSObjTy = RhsT->getAs<ObjCObjectType>(); |
5077 | if (!LHSObjTy || !RHSObjTy) |
5078 | return false; |
5079 | |
5080 | ObjCInterfaceDecl *BaseInterface = LHSObjTy->getInterface(); |
5081 | ObjCInterfaceDecl *DerivedInterface = RHSObjTy->getInterface(); |
5082 | if (!BaseInterface || !DerivedInterface) |
5083 | return false; |
5084 | |
5085 | if (Self.RequireCompleteType( |
5086 | KeyLoc, RhsT, diag::err_incomplete_type_used_in_type_trait_expr)) |
5087 | return false; |
5088 | |
5089 | return BaseInterface->isSuperClassOf(DerivedInterface); |
5090 | } |
5091 | |
5092 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) |
5093 | == (lhsRecord == rhsRecord)); |
5094 | |
5095 | if (lhsRecord == rhsRecord) |
5096 | return !lhsRecord->getDecl()->isUnion(); |
5097 | |
5098 | |
5099 | |
5100 | |
5101 | |
5102 | if (Self.RequireCompleteType(KeyLoc, RhsT, |
5103 | diag::err_incomplete_type_used_in_type_trait_expr)) |
5104 | return false; |
5105 | |
5106 | return cast<CXXRecordDecl>(rhsRecord->getDecl()) |
5107 | ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl())); |
5108 | } |
5109 | case BTT_IsSame: |
5110 | return Self.Context.hasSameType(LhsT, RhsT); |
5111 | case BTT_TypeCompatible: { |
5112 | |
5113 | Qualifiers LhsQuals, RhsQuals; |
5114 | QualType Lhs = Self.getASTContext().getUnqualifiedArrayType(LhsT, LhsQuals); |
5115 | QualType Rhs = Self.getASTContext().getUnqualifiedArrayType(RhsT, RhsQuals); |
5116 | return Self.Context.typesAreCompatible(Lhs, Rhs); |
5117 | } |
5118 | case BTT_IsConvertible: |
5119 | case BTT_IsConvertibleTo: { |
5120 | |
5121 | |
5122 | |
5123 | |
5124 | |
5125 | |
5126 | |
5127 | |
5128 | |
5129 | |
5130 | |
5131 | |
5132 | |
5133 | |
5134 | |
5135 | |
5136 | |
5137 | |
5138 | |
5139 | |
5140 | |
5141 | |
5142 | |
5143 | |
5144 | |
5145 | |
5146 | if (RhsT->isFunctionType() || RhsT->isArrayType()) |
5147 | return false; |
5148 | |
5149 | |
5150 | if (RhsT->isVoidType()) |
5151 | return LhsT->isVoidType(); |
5152 | |
5153 | |
5154 | if (!Self.isCompleteType(KeyLoc, RhsT) || Self.isAbstractType(KeyLoc, RhsT)) |
5155 | return false; |
5156 | |
5157 | |
5158 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
5159 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
5160 | |
5161 | |
5162 | InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); |
5163 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
5164 | Expr::getValueKindForType(LhsT)); |
5165 | Expr *FromPtr = &From; |
5166 | InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, |
5167 | SourceLocation())); |
5168 | |
5169 | |
5170 | |
5171 | EnterExpressionEvaluationContext Unevaluated( |
5172 | Self, Sema::ExpressionEvaluationContext::Unevaluated); |
5173 | Sema::SFINAETrap SFINAE(Self, ); |
5174 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
5175 | InitializationSequence Init(Self, To, Kind, FromPtr); |
5176 | if (Init.Failed()) |
5177 | return false; |
5178 | |
5179 | ExprResult Result = Init.Perform(Self, To, Kind, FromPtr); |
5180 | return !Result.isInvalid() && !SFINAE.hasErrorOccurred(); |
5181 | } |
5182 | |
5183 | case BTT_IsAssignable: |
5184 | case BTT_IsNothrowAssignable: |
5185 | case BTT_IsTriviallyAssignable: { |
5186 | |
5187 | |
5188 | |
5189 | |
5190 | |
5191 | |
5192 | |
5193 | |
5194 | |
5195 | |
5196 | |
5197 | if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType() && |
5198 | Self.RequireCompleteType(KeyLoc, LhsT, |
5199 | diag::err_incomplete_type_used_in_type_trait_expr)) |
5200 | return false; |
5201 | if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType() && |
5202 | Self.RequireCompleteType(KeyLoc, RhsT, |
5203 | diag::err_incomplete_type_used_in_type_trait_expr)) |
5204 | return false; |
5205 | |
5206 | |
5207 | if (LhsT->isVoidType() || RhsT->isVoidType()) |
5208 | return false; |
5209 | |
5210 | |
5211 | |
5212 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
5213 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
5214 | if (RhsT->isObjectType() || RhsT->isFunctionType()) |
5215 | RhsT = Self.Context.getRValueReferenceType(RhsT); |
5216 | OpaqueValueExpr Lhs(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
5217 | Expr::getValueKindForType(LhsT)); |
5218 | OpaqueValueExpr Rhs(KeyLoc, RhsT.getNonLValueExprType(Self.Context), |
5219 | Expr::getValueKindForType(RhsT)); |
5220 | |
5221 | |
5222 | |
5223 | EnterExpressionEvaluationContext Unevaluated( |
5224 | Self, Sema::ExpressionEvaluationContext::Unevaluated); |
5225 | Sema::SFINAETrap SFINAE(Self, ); |
5226 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
5227 | ExprResult Result = Self.BuildBinOp(, KeyLoc, BO_Assign, &Lhs, |
5228 | &Rhs); |
5229 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
5230 | return false; |
5231 | |
5232 | if (BTT == BTT_IsAssignable) |
5233 | return true; |
5234 | |
5235 | if (BTT == BTT_IsNothrowAssignable) |
5236 | return Self.canThrow(Result.get()) == CT_Cannot; |
5237 | |
5238 | if (BTT == BTT_IsTriviallyAssignable) { |
5239 | |
5240 | |
5241 | if (LhsT.getNonReferenceType().hasNonTrivialObjCLifetime()) |
5242 | return false; |
5243 | |
5244 | return !Result.get()->hasNonTrivialCall(Self.Context); |
5245 | } |
5246 | |
5247 | llvm_unreachable("unhandled type trait"); |
5248 | return false; |
5249 | } |
5250 | default: llvm_unreachable("not a BTT"); |
5251 | } |
5252 | llvm_unreachable("Unknown type trait or not implemented"); |
5253 | } |
5254 | |
5255 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, |
5256 | SourceLocation KWLoc, |
5257 | ParsedType Ty, |
5258 | Expr* DimExpr, |
5259 | SourceLocation RParen) { |
5260 | TypeSourceInfo *TSInfo; |
5261 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
5262 | if (!TSInfo) |
5263 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
5264 | |
5265 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); |
5266 | } |
5267 | |
5268 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, |
5269 | QualType T, Expr *DimExpr, |
5270 | SourceLocation KeyLoc) { |
5271 | (0) . __assert_fail ("!T->isDependentType() && \"Cannot evaluate traits of dependent type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5271, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
5272 | |
5273 | switch(ATT) { |
5274 | case ATT_ArrayRank: |
5275 | if (T->isArrayType()) { |
5276 | unsigned Dim = 0; |
5277 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
5278 | ++Dim; |
5279 | T = AT->getElementType(); |
5280 | } |
5281 | return Dim; |
5282 | } |
5283 | return 0; |
5284 | |
5285 | case ATT_ArrayExtent: { |
5286 | llvm::APSInt Value; |
5287 | uint64_t Dim; |
5288 | if (Self.VerifyIntegerConstantExpression(DimExpr, &Value, |
5289 | diag::err_dimension_expr_not_constant_integer, |
5290 | false).isInvalid()) |
5291 | return 0; |
5292 | if (Value.isSigned() && Value.isNegative()) { |
5293 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) |
5294 | << DimExpr->getSourceRange(); |
5295 | return 0; |
5296 | } |
5297 | Dim = Value.getLimitedValue(); |
5298 | |
5299 | if (T->isArrayType()) { |
5300 | unsigned D = 0; |
5301 | bool Matched = false; |
5302 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
5303 | if (Dim == D) { |
5304 | Matched = true; |
5305 | break; |
5306 | } |
5307 | ++D; |
5308 | T = AT->getElementType(); |
5309 | } |
5310 | |
5311 | if (Matched && T->isArrayType()) { |
5312 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) |
5313 | return CAT->getSize().getLimitedValue(); |
5314 | } |
5315 | } |
5316 | return 0; |
5317 | } |
5318 | } |
5319 | llvm_unreachable("Unknown type trait or not implemented"); |
5320 | } |
5321 | |
5322 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, |
5323 | SourceLocation KWLoc, |
5324 | TypeSourceInfo *TSInfo, |
5325 | Expr* DimExpr, |
5326 | SourceLocation RParen) { |
5327 | QualType T = TSInfo->getType(); |
5328 | |
5329 | |
5330 | |
5331 | uint64_t Value = 0; |
5332 | if (!T->isDependentType()) |
5333 | Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc); |
5334 | |
5335 | |
5336 | |
5337 | |
5338 | |
5339 | |
5340 | return new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, DimExpr, |
5341 | RParen, Context.getSizeType()); |
5342 | } |
5343 | |
5344 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, |
5345 | SourceLocation KWLoc, |
5346 | Expr *Queried, |
5347 | SourceLocation RParen) { |
5348 | |
5349 | if (!Queried) |
5350 | return ExprError(); |
5351 | |
5352 | ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen); |
5353 | |
5354 | return Result; |
5355 | } |
5356 | |
5357 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { |
5358 | switch (ET) { |
5359 | case ET_IsLValueExpr: return E->isLValue(); |
5360 | case ET_IsRValueExpr: return E->isRValue(); |
5361 | } |
5362 | llvm_unreachable("Expression trait not covered by switch"); |
5363 | } |
5364 | |
5365 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, |
5366 | SourceLocation KWLoc, |
5367 | Expr *Queried, |
5368 | SourceLocation RParen) { |
5369 | if (Queried->isTypeDependent()) { |
5370 | |
5371 | } else if (Queried->getType()->isPlaceholderType()) { |
5372 | ExprResult PE = CheckPlaceholderExpr(Queried); |
5373 | if (PE.isInvalid()) return ExprError(); |
5374 | return BuildExpressionTrait(ET, KWLoc, PE.get(), RParen); |
5375 | } |
5376 | |
5377 | bool Value = EvaluateExpressionTrait(ET, Queried); |
5378 | |
5379 | return new (Context) |
5380 | ExpressionTraitExpr(KWLoc, ET, Queried, Value, RParen, Context.BoolTy); |
5381 | } |
5382 | |
5383 | QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, |
5384 | ExprValueKind &VK, |
5385 | SourceLocation Loc, |
5386 | bool isIndirect) { |
5387 | (0) . __assert_fail ("!LHS.get()->getType()->isPlaceholderType() && !RHS.get()->getType()->isPlaceholderType() && \"placeholders should have been weeded out by now\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5389, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!LHS.get()->getType()->isPlaceholderType() && |
5388 | (0) . __assert_fail ("!LHS.get()->getType()->isPlaceholderType() && !RHS.get()->getType()->isPlaceholderType() && \"placeholders should have been weeded out by now\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5389, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> !RHS.get()->getType()->isPlaceholderType() && |
5389 | (0) . __assert_fail ("!LHS.get()->getType()->isPlaceholderType() && !RHS.get()->getType()->isPlaceholderType() && \"placeholders should have been weeded out by now\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5389, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "placeholders should have been weeded out by now"); |
5390 | |
5391 | |
5392 | |
5393 | if (isIndirect) |
5394 | LHS = DefaultLvalueConversion(LHS.get()); |
5395 | else if (LHS.get()->isRValue()) |
5396 | LHS = TemporaryMaterializationConversion(LHS.get()); |
5397 | if (LHS.isInvalid()) |
5398 | return QualType(); |
5399 | |
5400 | |
5401 | RHS = DefaultLvalueConversion(RHS.get()); |
5402 | if (RHS.isInvalid()) return QualType(); |
5403 | |
5404 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
5405 | |
5406 | |
5407 | |
5408 | |
5409 | QualType RHSType = RHS.get()->getType(); |
5410 | const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>(); |
5411 | if (!MemPtr) { |
5412 | Diag(Loc, diag::err_bad_memptr_rhs) |
5413 | << OpSpelling << RHSType << RHS.get()->getSourceRange(); |
5414 | return QualType(); |
5415 | } |
5416 | |
5417 | QualType Class(MemPtr->getClass(), 0); |
5418 | |
5419 | |
5420 | |
5421 | |
5422 | |
5423 | |
5424 | |
5425 | |
5426 | |
5427 | |
5428 | |
5429 | QualType LHSType = LHS.get()->getType(); |
5430 | if (isIndirect) { |
5431 | if (const PointerType *Ptr = LHSType->getAs<PointerType>()) |
5432 | LHSType = Ptr->getPointeeType(); |
5433 | else { |
5434 | Diag(Loc, diag::err_bad_memptr_lhs) |
5435 | << OpSpelling << 1 << LHSType |
5436 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
5437 | return QualType(); |
5438 | } |
5439 | } |
5440 | |
5441 | if (!Context.hasSameUnqualifiedType(Class, LHSType)) { |
5442 | |
5443 | if (RequireCompleteType(Loc, LHSType, diag::err_bad_memptr_lhs, |
5444 | OpSpelling, (int)isIndirect)) { |
5445 | return QualType(); |
5446 | } |
5447 | |
5448 | if (!IsDerivedFrom(Loc, LHSType, Class)) { |
5449 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
5450 | << (int)isIndirect << LHS.get()->getType(); |
5451 | return QualType(); |
5452 | } |
5453 | |
5454 | CXXCastPath BasePath; |
5455 | if (CheckDerivedToBaseConversion( |
5456 | LHSType, Class, Loc, |
5457 | SourceRange(LHS.get()->getBeginLoc(), RHS.get()->getEndLoc()), |
5458 | &BasePath)) |
5459 | return QualType(); |
5460 | |
5461 | |
5462 | QualType UseType = Context.getQualifiedType(Class, LHSType.getQualifiers()); |
5463 | if (isIndirect) |
5464 | UseType = Context.getPointerType(UseType); |
5465 | ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind(); |
5466 | LHS = ImpCastExprToType(LHS.get(), UseType, CK_DerivedToBase, VK, |
5467 | &BasePath); |
5468 | } |
5469 | |
5470 | if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) { |
5471 | |
5472 | |
5473 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
5474 | return QualType(); |
5475 | } |
5476 | |
5477 | |
5478 | |
5479 | |
5480 | |
5481 | |
5482 | QualType Result = MemPtr->getPointeeType(); |
5483 | Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers()); |
5484 | |
5485 | |
5486 | |
5487 | |
5488 | |
5489 | |
5490 | |
5491 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { |
5492 | switch (Proto->getRefQualifier()) { |
5493 | case RQ_None: |
5494 | |
5495 | break; |
5496 | |
5497 | case RQ_LValue: |
5498 | if (!isIndirect && !LHS.get()->Classify(Context).isLValue()) { |
5499 | |
5500 | |
5501 | if (Proto->isConst() && !Proto->isVolatile()) |
5502 | Diag(Loc, getLangOpts().CPlusPlus2a |
5503 | ? diag::warn_cxx17_compat_pointer_to_const_ref_member_on_rvalue |
5504 | : diag::ext_pointer_to_const_ref_member_on_rvalue); |
5505 | else |
5506 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
5507 | << RHSType << 1 << LHS.get()->getSourceRange(); |
5508 | } |
5509 | break; |
5510 | |
5511 | case RQ_RValue: |
5512 | if (isIndirect || !LHS.get()->Classify(Context).isRValue()) |
5513 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
5514 | << RHSType << 0 << LHS.get()->getSourceRange(); |
5515 | break; |
5516 | } |
5517 | } |
5518 | |
5519 | |
5520 | |
5521 | |
5522 | |
5523 | |
5524 | |
5525 | |
5526 | if (Result->isFunctionType()) { |
5527 | VK = VK_RValue; |
5528 | return Context.BoundMemberTy; |
5529 | } else if (isIndirect) { |
5530 | VK = VK_LValue; |
5531 | } else { |
5532 | VK = LHS.get()->getValueKind(); |
5533 | } |
5534 | |
5535 | return Result; |
5536 | } |
5537 | |
5538 | |
5539 | |
5540 | |
5541 | |
5542 | |
5543 | |
5544 | |
5545 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
5546 | SourceLocation QuestionLoc, |
5547 | bool &HaveConversion, |
5548 | QualType &ToType) { |
5549 | HaveConversion = false; |
5550 | ToType = To->getType(); |
5551 | |
5552 | InitializationKind Kind = |
5553 | InitializationKind::CreateCopy(To->getBeginLoc(), SourceLocation()); |
5554 | |
5555 | |
5556 | |
5557 | |
5558 | |
5559 | |
5560 | |
5561 | |
5562 | |
5563 | |
5564 | |
5565 | if (To->isLValue() || To->isXValue()) { |
5566 | QualType T = To->isLValue() ? Self.Context.getLValueReferenceType(ToType) |
5567 | : Self.Context.getRValueReferenceType(ToType); |
5568 | |
5569 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
5570 | |
5571 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
5572 | if (InitSeq.isDirectReferenceBinding()) { |
5573 | ToType = T; |
5574 | HaveConversion = true; |
5575 | return false; |
5576 | } |
5577 | |
5578 | if (InitSeq.isAmbiguous()) |
5579 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
5580 | } |
5581 | |
5582 | |
5583 | |
5584 | |
5585 | QualType FTy = From->getType(); |
5586 | QualType TTy = To->getType(); |
5587 | const RecordType *FRec = FTy->getAs<RecordType>(); |
5588 | const RecordType *TRec = TTy->getAs<RecordType>(); |
5589 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
5590 | Self.IsDerivedFrom(QuestionLoc, FTy, TTy); |
5591 | if (FRec && TRec && (FRec == TRec || FDerivedFromT || |
5592 | Self.IsDerivedFrom(QuestionLoc, TTy, FTy))) { |
5593 | |
5594 | |
5595 | |
5596 | if (FRec == TRec || FDerivedFromT) { |
5597 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
5598 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
5599 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
5600 | if (InitSeq) { |
5601 | HaveConversion = true; |
5602 | return false; |
5603 | } |
5604 | |
5605 | if (InitSeq.isAmbiguous()) |
5606 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
5607 | } |
5608 | } |
5609 | |
5610 | return false; |
5611 | } |
5612 | |
5613 | |
5614 | |
5615 | |
5616 | |
5617 | |
5618 | |
5619 | |
5620 | TTy = TTy.getNonLValueExprType(Self.Context); |
5621 | |
5622 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
5623 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
5624 | HaveConversion = !InitSeq.Failed(); |
5625 | ToType = TTy; |
5626 | if (InitSeq.isAmbiguous()) |
5627 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
5628 | |
5629 | return false; |
5630 | } |
5631 | |
5632 | |
5633 | |
5634 | |
5635 | |
5636 | |
5637 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, |
5638 | SourceLocation QuestionLoc) { |
5639 | Expr *Args[2] = { LHS.get(), RHS.get() }; |
5640 | OverloadCandidateSet CandidateSet(QuestionLoc, |
5641 | OverloadCandidateSet::CSK_Operator); |
5642 | Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, |
5643 | CandidateSet); |
5644 | |
5645 | OverloadCandidateSet::iterator Best; |
5646 | switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) { |
5647 | case OR_Success: { |
5648 | |
5649 | ExprResult LHSRes = Self.PerformImplicitConversion( |
5650 | LHS.get(), Best->BuiltinParamTypes[0], Best->Conversions[0], |
5651 | Sema::AA_Converting); |
5652 | if (LHSRes.isInvalid()) |
5653 | break; |
5654 | LHS = LHSRes; |
5655 | |
5656 | ExprResult RHSRes = Self.PerformImplicitConversion( |
5657 | RHS.get(), Best->BuiltinParamTypes[1], Best->Conversions[1], |
5658 | Sema::AA_Converting); |
5659 | if (RHSRes.isInvalid()) |
5660 | break; |
5661 | RHS = RHSRes; |
5662 | if (Best->Function) |
5663 | Self.MarkFunctionReferenced(QuestionLoc, Best->Function); |
5664 | return false; |
5665 | } |
5666 | |
5667 | case OR_No_Viable_Function: |
5668 | |
5669 | |
5670 | |
5671 | |
5672 | if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
5673 | return true; |
5674 | |
5675 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
5676 | << LHS.get()->getType() << RHS.get()->getType() |
5677 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
5678 | return true; |
5679 | |
5680 | case OR_Ambiguous: |
5681 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) |
5682 | << LHS.get()->getType() << RHS.get()->getType() |
5683 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
5684 | |
5685 | |
5686 | break; |
5687 | |
5688 | case OR_Deleted: |
5689 | llvm_unreachable("Conditional operator has only built-in overloads"); |
5690 | } |
5691 | return true; |
5692 | } |
5693 | |
5694 | |
5695 | |
5696 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { |
5697 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
5698 | InitializationKind Kind = |
5699 | InitializationKind::CreateCopy(E.get()->getBeginLoc(), SourceLocation()); |
5700 | Expr *Arg = E.get(); |
5701 | InitializationSequence InitSeq(Self, Entity, Kind, Arg); |
5702 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, Arg); |
5703 | if (Result.isInvalid()) |
5704 | return true; |
5705 | |
5706 | E = Result; |
5707 | return false; |
5708 | } |
5709 | |
5710 | |
5711 | |
5712 | |
5713 | |
5714 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
5715 | ExprResult &RHS, ExprValueKind &VK, |
5716 | ExprObjectKind &OK, |
5717 | SourceLocation QuestionLoc) { |
5718 | |
5719 | |
5720 | |
5721 | |
5722 | |
5723 | |
5724 | |
5725 | |
5726 | |
5727 | |
5728 | |
5729 | |
5730 | |
5731 | |
5732 | if (!Cond.get()->isTypeDependent()) { |
5733 | ExprResult CondRes = CheckCXXBooleanCondition(Cond.get()); |
5734 | if (CondRes.isInvalid()) |
5735 | return QualType(); |
5736 | Cond = CondRes; |
5737 | } |
5738 | |
5739 | |
5740 | VK = VK_RValue; |
5741 | OK = OK_Ordinary; |
5742 | |
5743 | |
5744 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) |
5745 | return Context.DependentTy; |
5746 | |
5747 | |
5748 | |
5749 | QualType LTy = LHS.get()->getType(); |
5750 | QualType RTy = RHS.get()->getType(); |
5751 | bool LVoid = LTy->isVoidType(); |
5752 | bool RVoid = RTy->isVoidType(); |
5753 | if (LVoid || RVoid) { |
5754 | |
5755 | |
5756 | |
5757 | |
5758 | bool LThrow = isa<CXXThrowExpr>(LHS.get()->IgnoreParenImpCasts()); |
5759 | bool RThrow = isa<CXXThrowExpr>(RHS.get()->IgnoreParenImpCasts()); |
5760 | if (LThrow != RThrow) { |
5761 | Expr *NonThrow = LThrow ? RHS.get() : LHS.get(); |
5762 | VK = NonThrow->getValueKind(); |
5763 | |
5764 | |
5765 | OK = NonThrow->getObjectKind(); |
5766 | return NonThrow->getType(); |
5767 | } |
5768 | |
5769 | |
5770 | |
5771 | if (LVoid && RVoid) |
5772 | return Context.VoidTy; |
5773 | |
5774 | |
5775 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
5776 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
5777 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
5778 | return QualType(); |
5779 | } |
5780 | |
5781 | |
5782 | |
5783 | |
5784 | |
5785 | |
5786 | |
5787 | if (!Context.hasSameType(LTy, RTy) && |
5788 | (LTy->isRecordType() || RTy->isRecordType())) { |
5789 | |
5790 | QualType L2RType, R2LType; |
5791 | bool HaveL2R, HaveR2L; |
5792 | if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType)) |
5793 | return QualType(); |
5794 | if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType)) |
5795 | return QualType(); |
5796 | |
5797 | |
5798 | if (HaveL2R && HaveR2L) { |
5799 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
5800 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
5801 | return QualType(); |
5802 | } |
5803 | |
5804 | |
5805 | |
5806 | |
5807 | if (HaveL2R) { |
5808 | if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid()) |
5809 | return QualType(); |
5810 | LTy = LHS.get()->getType(); |
5811 | } else if (HaveR2L) { |
5812 | if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid()) |
5813 | return QualType(); |
5814 | RTy = RHS.get()->getType(); |
5815 | } |
5816 | } |
5817 | |
5818 | |
5819 | |
5820 | |
5821 | |
5822 | |
5823 | |
5824 | |
5825 | |
5826 | ExprValueKind LVK = LHS.get()->getValueKind(); |
5827 | ExprValueKind RVK = RHS.get()->getValueKind(); |
5828 | if (!Context.hasSameType(LTy, RTy) && |
5829 | LVK == RVK && LVK != VK_RValue) { |
5830 | |
5831 | |
5832 | bool DerivedToBase, ObjCConversion, ObjCLifetimeConversion; |
5833 | if (CompareReferenceRelationship( |
5834 | QuestionLoc, LTy, RTy, DerivedToBase, |
5835 | ObjCConversion, ObjCLifetimeConversion) == Ref_Compatible && |
5836 | !DerivedToBase && !ObjCConversion && !ObjCLifetimeConversion && |
5837 | |
5838 | |
5839 | !RHS.get()->refersToBitField() && |
5840 | !RHS.get()->refersToVectorElement()) { |
5841 | RHS = ImpCastExprToType(RHS.get(), LTy, CK_NoOp, RVK); |
5842 | RTy = RHS.get()->getType(); |
5843 | } else if (CompareReferenceRelationship( |
5844 | QuestionLoc, RTy, LTy, DerivedToBase, |
5845 | ObjCConversion, ObjCLifetimeConversion) == Ref_Compatible && |
5846 | !DerivedToBase && !ObjCConversion && !ObjCLifetimeConversion && |
5847 | !LHS.get()->refersToBitField() && |
5848 | !LHS.get()->refersToVectorElement()) { |
5849 | LHS = ImpCastExprToType(LHS.get(), RTy, CK_NoOp, LVK); |
5850 | LTy = LHS.get()->getType(); |
5851 | } |
5852 | } |
5853 | |
5854 | |
5855 | |
5856 | |
5857 | |
5858 | |
5859 | |
5860 | |
5861 | bool Same = Context.hasSameType(LTy, RTy); |
5862 | if (Same && LVK == RVK && LVK != VK_RValue && |
5863 | LHS.get()->isOrdinaryOrBitFieldObject() && |
5864 | RHS.get()->isOrdinaryOrBitFieldObject()) { |
5865 | VK = LHS.get()->getValueKind(); |
5866 | if (LHS.get()->getObjectKind() == OK_BitField || |
5867 | RHS.get()->getObjectKind() == OK_BitField) |
5868 | OK = OK_BitField; |
5869 | |
5870 | |
5871 | |
5872 | if (LTy->isFunctionPointerType() || LTy->isMemberFunctionPointerType()) { |
5873 | Qualifiers Qs = LTy.getQualifiers(); |
5874 | LTy = FindCompositePointerType(QuestionLoc, LHS, RHS, |
5875 | ); |
5876 | LTy = Context.getQualifiedType(LTy, Qs); |
5877 | |
5878 | (0) . __assert_fail ("!LTy.isNull() && \"failed to find composite pointer type for \" \"canonically equivalent function ptr types\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5879, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!LTy.isNull() && "failed to find composite pointer type for " |
5879 | (0) . __assert_fail ("!LTy.isNull() && \"failed to find composite pointer type for \" \"canonically equivalent function ptr types\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5879, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "canonically equivalent function ptr types"); |
5880 | (0) . __assert_fail ("Context.hasSameType(LTy, RTy) && \"bad composite pointer type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5880, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Context.hasSameType(LTy, RTy) && "bad composite pointer type"); |
5881 | } |
5882 | |
5883 | return LTy; |
5884 | } |
5885 | |
5886 | |
5887 | |
5888 | |
5889 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
5890 | |
5891 | |
5892 | |
5893 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
5894 | return QualType(); |
5895 | } |
5896 | |
5897 | |
5898 | |
5899 | |
5900 | LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); |
5901 | RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); |
5902 | if (LHS.isInvalid() || RHS.isInvalid()) |
5903 | return QualType(); |
5904 | LTy = LHS.get()->getType(); |
5905 | RTy = RHS.get()->getType(); |
5906 | |
5907 | |
5908 | |
5909 | |
5910 | |
5911 | |
5912 | |
5913 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
5914 | if (LTy->isRecordType()) { |
5915 | |
5916 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
5917 | |
5918 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
5919 | SourceLocation(), |
5920 | LHS); |
5921 | if (LHSCopy.isInvalid()) |
5922 | return QualType(); |
5923 | |
5924 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
5925 | SourceLocation(), |
5926 | RHS); |
5927 | if (RHSCopy.isInvalid()) |
5928 | return QualType(); |
5929 | |
5930 | LHS = LHSCopy; |
5931 | RHS = RHSCopy; |
5932 | } |
5933 | |
5934 | |
5935 | |
5936 | if (LTy->isFunctionPointerType() || LTy->isMemberFunctionPointerType()) { |
5937 | LTy = FindCompositePointerType(QuestionLoc, LHS, RHS); |
5938 | (0) . __assert_fail ("!LTy.isNull() && \"failed to find composite pointer type for \" \"canonically equivalent function ptr types\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5939, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!LTy.isNull() && "failed to find composite pointer type for " |
5939 | (0) . __assert_fail ("!LTy.isNull() && \"failed to find composite pointer type for \" \"canonically equivalent function ptr types\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 5939, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "canonically equivalent function ptr types"); |
5940 | } |
5941 | |
5942 | return LTy; |
5943 | } |
5944 | |
5945 | |
5946 | if (LTy->isVectorType() || RTy->isVectorType()) |
5947 | return CheckVectorOperands(LHS, RHS, QuestionLoc, , |
5948 | , |
5949 | ); |
5950 | |
5951 | |
5952 | |
5953 | |
5954 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
5955 | QualType ResTy = UsualArithmeticConversions(LHS, RHS); |
5956 | if (LHS.isInvalid() || RHS.isInvalid()) |
5957 | return QualType(); |
5958 | if (ResTy.isNull()) { |
5959 | Diag(QuestionLoc, |
5960 | diag::err_typecheck_cond_incompatible_operands) << LTy << RTy |
5961 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
5962 | return QualType(); |
5963 | } |
5964 | |
5965 | LHS = ImpCastExprToType(LHS.get(), ResTy, PrepareScalarCast(LHS, ResTy)); |
5966 | RHS = ImpCastExprToType(RHS.get(), ResTy, PrepareScalarCast(RHS, ResTy)); |
5967 | |
5968 | return ResTy; |
5969 | } |
5970 | |
5971 | |
5972 | |
5973 | |
5974 | |
5975 | |
5976 | |
5977 | |
5978 | |
5979 | |
5980 | |
5981 | |
5982 | |
5983 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS); |
5984 | if (!Composite.isNull()) |
5985 | return Composite; |
5986 | |
5987 | |
5988 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
5989 | if (!Composite.isNull()) |
5990 | return Composite; |
5991 | |
5992 | |
5993 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
5994 | return QualType(); |
5995 | |
5996 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
5997 | << LHS.get()->getType() << RHS.get()->getType() |
5998 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
5999 | return QualType(); |
6000 | } |
6001 | |
6002 | static FunctionProtoType::ExceptionSpecInfo |
6003 | mergeExceptionSpecs(Sema &S, FunctionProtoType::ExceptionSpecInfo ESI1, |
6004 | FunctionProtoType::ExceptionSpecInfo ESI2, |
6005 | SmallVectorImpl<QualType> &ExceptionTypeStorage) { |
6006 | ExceptionSpecificationType EST1 = ESI1.Type; |
6007 | ExceptionSpecificationType EST2 = ESI2.Type; |
6008 | |
6009 | |
6010 | if (EST1 == EST_None) return ESI1; |
6011 | if (EST2 == EST_None) return ESI2; |
6012 | if (EST1 == EST_MSAny) return ESI1; |
6013 | if (EST2 == EST_MSAny) return ESI2; |
6014 | if (EST1 == EST_NoexceptFalse) return ESI1; |
6015 | if (EST2 == EST_NoexceptFalse) return ESI2; |
6016 | |
6017 | |
6018 | if (EST1 == EST_DynamicNone) return ESI2; |
6019 | if (EST2 == EST_DynamicNone) return ESI1; |
6020 | if (EST1 == EST_BasicNoexcept) return ESI2; |
6021 | if (EST2 == EST_BasicNoexcept) return ESI1; |
6022 | if (EST1 == EST_NoexceptTrue) return ESI2; |
6023 | if (EST2 == EST_NoexceptTrue) return ESI1; |
6024 | |
6025 | |
6026 | |
6027 | |
6028 | |
6029 | |
6030 | if (EST1 == EST_DependentNoexcept || EST2 == EST_DependentNoexcept) { |
6031 | (0) . __assert_fail ("!S.getLangOpts().CPlusPlus17 && \"computing composite pointer type of dependent types\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6032, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!S.getLangOpts().CPlusPlus17 && |
6032 | (0) . __assert_fail ("!S.getLangOpts().CPlusPlus17 && \"computing composite pointer type of dependent types\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6032, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "computing composite pointer type of dependent types"); |
6033 | return FunctionProtoType::ExceptionSpecInfo(); |
6034 | } |
6035 | |
6036 | |
6037 | |
6038 | switch (EST1) { |
6039 | case EST_None: |
6040 | case EST_DynamicNone: |
6041 | case EST_MSAny: |
6042 | case EST_BasicNoexcept: |
6043 | case EST_DependentNoexcept: |
6044 | case EST_NoexceptFalse: |
6045 | case EST_NoexceptTrue: |
6046 | llvm_unreachable("handled above"); |
6047 | |
6048 | case EST_Dynamic: { |
6049 | |
6050 | |
6051 | (0) . __assert_fail ("EST2 == EST_Dynamic && \"other cases should already be handled\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6051, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(EST2 == EST_Dynamic && "other cases should already be handled"); |
6052 | llvm::SmallPtrSet<QualType, 8> Found; |
6053 | for (auto &Exceptions : {ESI1.Exceptions, ESI2.Exceptions}) |
6054 | for (QualType E : Exceptions) |
6055 | if (Found.insert(S.Context.getCanonicalType(E)).second) |
6056 | ExceptionTypeStorage.push_back(E); |
6057 | |
6058 | FunctionProtoType::ExceptionSpecInfo Result(EST_Dynamic); |
6059 | Result.Exceptions = ExceptionTypeStorage; |
6060 | return Result; |
6061 | } |
6062 | |
6063 | case EST_Unevaluated: |
6064 | case EST_Uninstantiated: |
6065 | case EST_Unparsed: |
6066 | llvm_unreachable("shouldn't see unresolved exception specifications here"); |
6067 | } |
6068 | |
6069 | llvm_unreachable("invalid ExceptionSpecificationType"); |
6070 | } |
6071 | |
6072 | |
6073 | |
6074 | |
6075 | |
6076 | |
6077 | |
6078 | |
6079 | |
6080 | |
6081 | |
6082 | |
6083 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
6084 | Expr *&E1, Expr *&E2, |
6085 | bool ConvertArgs) { |
6086 | (0) . __assert_fail ("getLangOpts().CPlusPlus && \"This function assumes C++\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6086, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getLangOpts().CPlusPlus && "This function assumes C++"); |
6087 | |
6088 | |
6089 | |
6090 | |
6091 | QualType T1 = E1->getType(), T2 = E2->getType(); |
6092 | |
6093 | |
6094 | |
6095 | bool T1IsPointerLike = T1->isAnyPointerType() || T1->isMemberPointerType() || |
6096 | T1->isNullPtrType(); |
6097 | bool T2IsPointerLike = T2->isAnyPointerType() || T2->isMemberPointerType() || |
6098 | T2->isNullPtrType(); |
6099 | if (!T1IsPointerLike && !T2IsPointerLike) |
6100 | return QualType(); |
6101 | |
6102 | |
6103 | |
6104 | |
6105 | |
6106 | |
6107 | if (T1IsPointerLike && |
6108 | E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
6109 | if (ConvertArgs) |
6110 | E2 = ImpCastExprToType(E2, T1, T1->isMemberPointerType() |
6111 | ? CK_NullToMemberPointer |
6112 | : CK_NullToPointer).get(); |
6113 | return T1; |
6114 | } |
6115 | if (T2IsPointerLike && |
6116 | E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
6117 | if (ConvertArgs) |
6118 | E1 = ImpCastExprToType(E1, T2, T2->isMemberPointerType() |
6119 | ? CK_NullToMemberPointer |
6120 | : CK_NullToPointer).get(); |
6121 | return T2; |
6122 | } |
6123 | |
6124 | |
6125 | if (!T1IsPointerLike || !T2IsPointerLike) |
6126 | return QualType(); |
6127 | (0) . __assert_fail ("!T1->isNullPtrType() && !T2->isNullPtrType() && \"nullptr_t should be a null pointer constant\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6128, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!T1->isNullPtrType() && !T2->isNullPtrType() && |
6128 | (0) . __assert_fail ("!T1->isNullPtrType() && !T2->isNullPtrType() && \"nullptr_t should be a null pointer constant\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6128, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "nullptr_t should be a null pointer constant"); |
6129 | |
6130 | |
6131 | |
6132 | |
6133 | |
6134 | |
6135 | |
6136 | |
6137 | |
6138 | |
6139 | |
6140 | |
6141 | |
6142 | |
6143 | |
6144 | |
6145 | |
6146 | |
6147 | |
6148 | |
6149 | |
6150 | |
6151 | |
6152 | |
6153 | |
6154 | |
6155 | |
6156 | |
6157 | |
6158 | |
6159 | |
6160 | |
6161 | |
6162 | |
6163 | |
6164 | SmallVector<unsigned, 4> QualifierUnion; |
6165 | SmallVector<std::pair<const Type *, const Type *>, 4> MemberOfClass; |
6166 | QualType Composite1 = T1; |
6167 | QualType Composite2 = T2; |
6168 | unsigned NeedConstBefore = 0; |
6169 | while (true) { |
6170 | const PointerType *Ptr1, *Ptr2; |
6171 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
6172 | (Ptr2 = Composite2->getAs<PointerType>())) { |
6173 | Composite1 = Ptr1->getPointeeType(); |
6174 | Composite2 = Ptr2->getPointeeType(); |
6175 | |
6176 | |
6177 | |
6178 | if (Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
6179 | NeedConstBefore = QualifierUnion.size(); |
6180 | |
6181 | QualifierUnion.push_back( |
6182 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
6183 | MemberOfClass.push_back(std::make_pair(nullptr, nullptr)); |
6184 | continue; |
6185 | } |
6186 | |
6187 | const MemberPointerType *MemPtr1, *MemPtr2; |
6188 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
6189 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
6190 | Composite1 = MemPtr1->getPointeeType(); |
6191 | Composite2 = MemPtr2->getPointeeType(); |
6192 | |
6193 | |
6194 | |
6195 | if (Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
6196 | NeedConstBefore = QualifierUnion.size(); |
6197 | |
6198 | QualifierUnion.push_back( |
6199 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
6200 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
6201 | MemPtr2->getClass())); |
6202 | continue; |
6203 | } |
6204 | |
6205 | |
6206 | |
6207 | |
6208 | break; |
6209 | } |
6210 | |
6211 | |
6212 | |
6213 | |
6214 | |
6215 | |
6216 | |
6217 | |
6218 | |
6219 | |
6220 | if (QualifierUnion.size() == 1) { |
6221 | if (auto *FPT1 = Composite1->getAs<FunctionProtoType>()) { |
6222 | if (auto *FPT2 = Composite2->getAs<FunctionProtoType>()) { |
6223 | FunctionProtoType::ExtProtoInfo EPI1 = FPT1->getExtProtoInfo(); |
6224 | FunctionProtoType::ExtProtoInfo EPI2 = FPT2->getExtProtoInfo(); |
6225 | |
6226 | |
6227 | bool Noreturn = |
6228 | EPI1.ExtInfo.getNoReturn() && EPI2.ExtInfo.getNoReturn(); |
6229 | EPI1.ExtInfo = EPI1.ExtInfo.withNoReturn(Noreturn); |
6230 | EPI2.ExtInfo = EPI2.ExtInfo.withNoReturn(Noreturn); |
6231 | |
6232 | |
6233 | SmallVector<QualType, 8> ExceptionTypeStorage; |
6234 | EPI1.ExceptionSpec = EPI2.ExceptionSpec = |
6235 | mergeExceptionSpecs(*this, EPI1.ExceptionSpec, EPI2.ExceptionSpec, |
6236 | ExceptionTypeStorage); |
6237 | |
6238 | Composite1 = Context.getFunctionType(FPT1->getReturnType(), |
6239 | FPT1->getParamTypes(), EPI1); |
6240 | Composite2 = Context.getFunctionType(FPT2->getReturnType(), |
6241 | FPT2->getParamTypes(), EPI2); |
6242 | } |
6243 | } |
6244 | } |
6245 | |
6246 | if (NeedConstBefore) { |
6247 | |
6248 | |
6249 | |
6250 | for (unsigned I = 0; I != NeedConstBefore; ++I) |
6251 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) |
6252 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
6253 | } |
6254 | |
6255 | |
6256 | auto MOC = MemberOfClass.rbegin(); |
6257 | for (unsigned CVR : llvm::reverse(QualifierUnion)) { |
6258 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
6259 | auto Classes = *MOC++; |
6260 | if (Classes.first && Classes.second) { |
6261 | |
6262 | Composite1 = Context.getMemberPointerType( |
6263 | Context.getQualifiedType(Composite1, Quals), Classes.first); |
6264 | Composite2 = Context.getMemberPointerType( |
6265 | Context.getQualifiedType(Composite2, Quals), Classes.second); |
6266 | } else { |
6267 | |
6268 | Composite1 = |
6269 | Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
6270 | Composite2 = |
6271 | Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
6272 | } |
6273 | } |
6274 | |
6275 | struct Conversion { |
6276 | Sema &S; |
6277 | Expr *&E1, *&E2; |
6278 | QualType Composite; |
6279 | InitializedEntity Entity; |
6280 | InitializationKind Kind; |
6281 | InitializationSequence E1ToC, E2ToC; |
6282 | bool Viable; |
6283 | |
6284 | Conversion(Sema &S, SourceLocation Loc, Expr *&E1, Expr *&E2, |
6285 | QualType Composite) |
6286 | : S(S), E1(E1), E2(E2), Composite(Composite), |
6287 | Entity(InitializedEntity::InitializeTemporary(Composite)), |
6288 | Kind(InitializationKind::CreateCopy(Loc, SourceLocation())), |
6289 | E1ToC(S, Entity, Kind, E1), E2ToC(S, Entity, Kind, E2), |
6290 | Viable(E1ToC && E2ToC) {} |
6291 | |
6292 | bool perform() { |
6293 | ExprResult E1Result = E1ToC.Perform(S, Entity, Kind, E1); |
6294 | if (E1Result.isInvalid()) |
6295 | return true; |
6296 | E1 = E1Result.getAs<Expr>(); |
6297 | |
6298 | ExprResult E2Result = E2ToC.Perform(S, Entity, Kind, E2); |
6299 | if (E2Result.isInvalid()) |
6300 | return true; |
6301 | E2 = E2Result.getAs<Expr>(); |
6302 | |
6303 | return false; |
6304 | } |
6305 | }; |
6306 | |
6307 | |
6308 | Conversion C1(*this, Loc, E1, E2, Composite1); |
6309 | if (C1.Viable && Context.hasSameType(Composite1, Composite2)) { |
6310 | if (ConvertArgs && C1.perform()) |
6311 | return QualType(); |
6312 | return C1.Composite; |
6313 | } |
6314 | Conversion C2(*this, Loc, E1, E2, Composite2); |
6315 | |
6316 | if (C1.Viable == C2.Viable) { |
6317 | |
6318 | |
6319 | |
6320 | return QualType(); |
6321 | } |
6322 | |
6323 | |
6324 | if (ConvertArgs && (C1.Viable ? C1 : C2).perform()) |
6325 | return QualType(); |
6326 | |
6327 | return C1.Viable ? C1.Composite : C2.Composite; |
6328 | } |
6329 | |
6330 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
6331 | if (!E) |
6332 | return ExprError(); |
6333 | |
6334 | (0) . __assert_fail ("!isa(E) && \"Double-bound temporary?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6334, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
6335 | |
6336 | |
6337 | if (!E->isRValue()) |
6338 | return E; |
6339 | |
6340 | |
6341 | |
6342 | if (getLangOpts().ObjCAutoRefCount && |
6343 | E->getType()->isObjCRetainableType()) { |
6344 | |
6345 | bool ReturnsRetained; |
6346 | |
6347 | |
6348 | |
6349 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) { |
6350 | Expr *Callee = Call->getCallee()->IgnoreParens(); |
6351 | QualType T = Callee->getType(); |
6352 | |
6353 | if (T == Context.BoundMemberTy) { |
6354 | |
6355 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee)) |
6356 | T = BinOp->getRHS()->getType(); |
6357 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee)) |
6358 | T = Mem->getMemberDecl()->getType(); |
6359 | } |
6360 | |
6361 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
6362 | T = Ptr->getPointeeType(); |
6363 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) |
6364 | T = Ptr->getPointeeType(); |
6365 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) |
6366 | T = MemPtr->getPointeeType(); |
6367 | |
6368 | const FunctionType *FTy = T->getAs<FunctionType>(); |
6369 | (0) . __assert_fail ("FTy && \"call to value not of function type?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6369, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FTy && "call to value not of function type?"); |
6370 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); |
6371 | |
6372 | |
6373 | |
6374 | } else if (isa<StmtExpr>(E)) { |
6375 | ReturnsRetained = true; |
6376 | |
6377 | |
6378 | |
6379 | } else if (isa<CastExpr>(E) && |
6380 | isa<BlockExpr>(cast<CastExpr>(E)->getSubExpr())) { |
6381 | return E; |
6382 | |
6383 | |
6384 | |
6385 | |
6386 | } else { |
6387 | ObjCMethodDecl *D = nullptr; |
6388 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) { |
6389 | D = Send->getMethodDecl(); |
6390 | } else if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(E)) { |
6391 | D = BoxedExpr->getBoxingMethod(); |
6392 | } else if (ObjCArrayLiteral *ArrayLit = dyn_cast<ObjCArrayLiteral>(E)) { |
6393 | |
6394 | |
6395 | if (ArrayLit->getNumElements() == 0 && |
6396 | Context.getLangOpts().ObjCRuntime.hasEmptyCollections()) |
6397 | return E; |
6398 | |
6399 | D = ArrayLit->getArrayWithObjectsMethod(); |
6400 | } else if (ObjCDictionaryLiteral *DictLit |
6401 | = dyn_cast<ObjCDictionaryLiteral>(E)) { |
6402 | |
6403 | |
6404 | if (DictLit->getNumElements() == 0 && |
6405 | Context.getLangOpts().ObjCRuntime.hasEmptyCollections()) |
6406 | return E; |
6407 | |
6408 | D = DictLit->getDictWithObjectsMethod(); |
6409 | } |
6410 | |
6411 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); |
6412 | |
6413 | |
6414 | |
6415 | |
6416 | if (!ReturnsRetained && |
6417 | D && D->getMethodFamily() == OMF_performSelector) |
6418 | return E; |
6419 | } |
6420 | |
6421 | |
6422 | if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType()) |
6423 | return E; |
6424 | |
6425 | Cleanup.setExprNeedsCleanups(true); |
6426 | |
6427 | CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject |
6428 | : CK_ARCReclaimReturnedObject); |
6429 | return ImplicitCastExpr::Create(Context, E->getType(), ck, E, nullptr, |
6430 | VK_RValue); |
6431 | } |
6432 | |
6433 | if (!getLangOpts().CPlusPlus) |
6434 | return E; |
6435 | |
6436 | |
6437 | |
6438 | const Type *T = Context.getCanonicalType(E->getType().getTypePtr()); |
6439 | const RecordType *RT = nullptr; |
6440 | while (!RT) { |
6441 | switch (T->getTypeClass()) { |
6442 | case Type::Record: |
6443 | RT = cast<RecordType>(T); |
6444 | break; |
6445 | case Type::ConstantArray: |
6446 | case Type::IncompleteArray: |
6447 | case Type::VariableArray: |
6448 | case Type::DependentSizedArray: |
6449 | T = cast<ArrayType>(T)->getElementType().getTypePtr(); |
6450 | break; |
6451 | default: |
6452 | return E; |
6453 | } |
6454 | } |
6455 | |
6456 | |
6457 | |
6458 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
6459 | if (RD->isInvalidDecl() || RD->isDependentContext()) |
6460 | return E; |
6461 | |
6462 | bool IsDecltype = ExprEvalContexts.back().ExprContext == |
6463 | ExpressionEvaluationContextRecord::EK_Decltype; |
6464 | CXXDestructorDecl *Destructor = IsDecltype ? nullptr : LookupDestructor(RD); |
6465 | |
6466 | if (Destructor) { |
6467 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
6468 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
6469 | PDiag(diag::err_access_dtor_temp) |
6470 | << E->getType()); |
6471 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
6472 | return ExprError(); |
6473 | |
6474 | |
6475 | if (Destructor->isTrivial()) |
6476 | return E; |
6477 | |
6478 | |
6479 | Cleanup.setExprNeedsCleanups(true); |
6480 | } |
6481 | |
6482 | CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor); |
6483 | CXXBindTemporaryExpr *Bind = CXXBindTemporaryExpr::Create(Context, Temp, E); |
6484 | |
6485 | if (IsDecltype) |
6486 | ExprEvalContexts.back().DelayedDecltypeBinds.push_back(Bind); |
6487 | |
6488 | return Bind; |
6489 | } |
6490 | |
6491 | ExprResult |
6492 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
6493 | if (SubExpr.isInvalid()) |
6494 | return ExprError(); |
6495 | |
6496 | return MaybeCreateExprWithCleanups(SubExpr.get()); |
6497 | } |
6498 | |
6499 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
6500 | (0) . __assert_fail ("SubExpr && \"subexpression can't be null!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6500, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SubExpr && "subexpression can't be null!"); |
6501 | |
6502 | CleanupVarDeclMarking(); |
6503 | |
6504 | unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects; |
6505 | = FirstCleanup", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6505, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ExprCleanupObjects.size() >= FirstCleanup); |
6506 | assert(Cleanup.exprNeedsCleanups() || |
6507 | ExprCleanupObjects.size() == FirstCleanup); |
6508 | if (!Cleanup.exprNeedsCleanups()) |
6509 | return SubExpr; |
6510 | |
6511 | auto Cleanups = llvm::makeArrayRef(ExprCleanupObjects.begin() + FirstCleanup, |
6512 | ExprCleanupObjects.size() - FirstCleanup); |
6513 | |
6514 | auto *E = ExprWithCleanups::Create( |
6515 | Context, SubExpr, Cleanup.cleanupsHaveSideEffects(), Cleanups); |
6516 | DiscardCleanupsInEvaluationContext(); |
6517 | |
6518 | return E; |
6519 | } |
6520 | |
6521 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
6522 | (0) . __assert_fail ("SubStmt && \"sub-statement can't be null!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6522, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SubStmt && "sub-statement can't be null!"); |
6523 | |
6524 | CleanupVarDeclMarking(); |
6525 | |
6526 | if (!Cleanup.exprNeedsCleanups()) |
6527 | return SubStmt; |
6528 | |
6529 | |
6530 | |
6531 | |
6532 | |
6533 | CompoundStmt *CompStmt = CompoundStmt::Create( |
6534 | Context, SubStmt, SourceLocation(), SourceLocation()); |
6535 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), |
6536 | SourceLocation()); |
6537 | return MaybeCreateExprWithCleanups(E); |
6538 | } |
6539 | |
6540 | |
6541 | |
6542 | |
6543 | |
6544 | ExprResult Sema::ActOnDecltypeExpression(Expr *E) { |
6545 | (0) . __assert_fail ("ExprEvalContexts.back().ExprContext == ExpressionEvaluationContextRecord..EK_Decltype && \"not in a decltype expression\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6547, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ExprEvalContexts.back().ExprContext == |
6546 | (0) . __assert_fail ("ExprEvalContexts.back().ExprContext == ExpressionEvaluationContextRecord..EK_Decltype && \"not in a decltype expression\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6547, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ExpressionEvaluationContextRecord::EK_Decltype && |
6547 | (0) . __assert_fail ("ExprEvalContexts.back().ExprContext == ExpressionEvaluationContextRecord..EK_Decltype && \"not in a decltype expression\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6547, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "not in a decltype expression"); |
6548 | |
6549 | ExprResult Result = CheckPlaceholderExpr(E); |
6550 | if (Result.isInvalid()) |
6551 | return ExprError(); |
6552 | E = Result.get(); |
6553 | |
6554 | |
6555 | |
6556 | |
6557 | |
6558 | |
6559 | |
6560 | |
6561 | |
6562 | |
6563 | |
6564 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
6565 | ExprResult SubExpr = ActOnDecltypeExpression(PE->getSubExpr()); |
6566 | if (SubExpr.isInvalid()) |
6567 | return ExprError(); |
6568 | if (SubExpr.get() == PE->getSubExpr()) |
6569 | return E; |
6570 | return ActOnParenExpr(PE->getLParen(), PE->getRParen(), SubExpr.get()); |
6571 | } |
6572 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
6573 | if (BO->getOpcode() == BO_Comma) { |
6574 | ExprResult RHS = ActOnDecltypeExpression(BO->getRHS()); |
6575 | if (RHS.isInvalid()) |
6576 | return ExprError(); |
6577 | if (RHS.get() == BO->getRHS()) |
6578 | return E; |
6579 | return new (Context) BinaryOperator( |
6580 | BO->getLHS(), RHS.get(), BO_Comma, BO->getType(), BO->getValueKind(), |
6581 | BO->getObjectKind(), BO->getOperatorLoc(), BO->getFPFeatures()); |
6582 | } |
6583 | } |
6584 | |
6585 | CXXBindTemporaryExpr *TopBind = dyn_cast<CXXBindTemporaryExpr>(E); |
6586 | CallExpr *TopCall = TopBind ? dyn_cast<CallExpr>(TopBind->getSubExpr()) |
6587 | : nullptr; |
6588 | if (TopCall) |
6589 | E = TopCall; |
6590 | else |
6591 | TopBind = nullptr; |
6592 | |
6593 | |
6594 | ExprEvalContexts.back().ExprContext = |
6595 | ExpressionEvaluationContextRecord::EK_Other; |
6596 | |
6597 | |
6598 | |
6599 | if (getLangOpts().MSVCCompat) |
6600 | return E; |
6601 | |
6602 | |
6603 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeCalls.size(); |
6604 | I != N; ++I) { |
6605 | CallExpr *Call = ExprEvalContexts.back().DelayedDecltypeCalls[I]; |
6606 | if (Call == TopCall) |
6607 | continue; |
6608 | |
6609 | if (CheckCallReturnType(Call->getCallReturnType(Context), |
6610 | Call->getBeginLoc(), Call, Call->getDirectCallee())) |
6611 | return ExprError(); |
6612 | } |
6613 | |
6614 | |
6615 | |
6616 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeBinds.size(); |
6617 | I != N; ++I) { |
6618 | CXXBindTemporaryExpr *Bind = |
6619 | ExprEvalContexts.back().DelayedDecltypeBinds[I]; |
6620 | if (Bind == TopBind) |
6621 | continue; |
6622 | |
6623 | CXXTemporary *Temp = Bind->getTemporary(); |
6624 | |
6625 | CXXRecordDecl *RD = |
6626 | Bind->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
6627 | CXXDestructorDecl *Destructor = LookupDestructor(RD); |
6628 | Temp->setDestructor(Destructor); |
6629 | |
6630 | MarkFunctionReferenced(Bind->getExprLoc(), Destructor); |
6631 | CheckDestructorAccess(Bind->getExprLoc(), Destructor, |
6632 | PDiag(diag::err_access_dtor_temp) |
6633 | << Bind->getType()); |
6634 | if (DiagnoseUseOfDecl(Destructor, Bind->getExprLoc())) |
6635 | return ExprError(); |
6636 | |
6637 | |
6638 | Cleanup.setExprNeedsCleanups(true); |
6639 | } |
6640 | |
6641 | |
6642 | return E; |
6643 | } |
6644 | |
6645 | |
6646 | static void noteOperatorArrows(Sema &S, |
6647 | ArrayRef<FunctionDecl *> OperatorArrows) { |
6648 | unsigned SkipStart = OperatorArrows.size(), SkipCount = 0; |
6649 | |
6650 | unsigned Limit = 9; |
6651 | if (OperatorArrows.size() > Limit) { |
6652 | |
6653 | SkipStart = (Limit - 1) / 2 + (Limit - 1) % 2; |
6654 | SkipCount = OperatorArrows.size() - (Limit - 1); |
6655 | } |
6656 | |
6657 | for (unsigned I = 0; I < OperatorArrows.size(); ) { |
6658 | if (I == SkipStart) { |
6659 | S.Diag(OperatorArrows[I]->getLocation(), |
6660 | diag::note_operator_arrows_suppressed) |
6661 | << SkipCount; |
6662 | I += SkipCount; |
6663 | } else { |
6664 | S.Diag(OperatorArrows[I]->getLocation(), diag::note_operator_arrow_here) |
6665 | << OperatorArrows[I]->getCallResultType(); |
6666 | ++I; |
6667 | } |
6668 | } |
6669 | } |
6670 | |
6671 | ExprResult Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, |
6672 | SourceLocation OpLoc, |
6673 | tok::TokenKind OpKind, |
6674 | ParsedType &ObjectType, |
6675 | bool &MayBePseudoDestructor) { |
6676 | |
6677 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
6678 | if (Result.isInvalid()) return ExprError(); |
6679 | Base = Result.get(); |
6680 | |
6681 | Result = CheckPlaceholderExpr(Base); |
6682 | if (Result.isInvalid()) return ExprError(); |
6683 | Base = Result.get(); |
6684 | |
6685 | QualType BaseType = Base->getType(); |
6686 | MayBePseudoDestructor = false; |
6687 | if (BaseType->isDependentType()) { |
6688 | |
6689 | |
6690 | |
6691 | if (OpKind == tok::arrow) |
6692 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
6693 | BaseType = Ptr->getPointeeType(); |
6694 | |
6695 | ObjectType = ParsedType::make(BaseType); |
6696 | MayBePseudoDestructor = true; |
6697 | return Base; |
6698 | } |
6699 | |
6700 | |
6701 | |
6702 | |
6703 | if (OpKind == tok::arrow) { |
6704 | QualType StartingType = BaseType; |
6705 | bool NoArrowOperatorFound = false; |
6706 | bool FirstIteration = true; |
6707 | FunctionDecl *CurFD = dyn_cast<FunctionDecl>(CurContext); |
6708 | |
6709 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
6710 | SmallVector<FunctionDecl*, 8> OperatorArrows; |
6711 | CTypes.insert(Context.getCanonicalType(BaseType)); |
6712 | |
6713 | while (BaseType->isRecordType()) { |
6714 | if (OperatorArrows.size() >= getLangOpts().ArrowDepth) { |
6715 | Diag(OpLoc, diag::err_operator_arrow_depth_exceeded) |
6716 | << StartingType << getLangOpts().ArrowDepth << Base->getSourceRange(); |
6717 | noteOperatorArrows(*this, OperatorArrows); |
6718 | Diag(OpLoc, diag::note_operator_arrow_depth) |
6719 | << getLangOpts().ArrowDepth; |
6720 | return ExprError(); |
6721 | } |
6722 | |
6723 | Result = BuildOverloadedArrowExpr( |
6724 | S, Base, OpLoc, |
6725 | |
6726 | |
6727 | |
6728 | |
6729 | (FirstIteration && CurFD && CurFD->isFunctionTemplateSpecialization()) |
6730 | ? nullptr |
6731 | : &NoArrowOperatorFound); |
6732 | if (Result.isInvalid()) { |
6733 | if (NoArrowOperatorFound) { |
6734 | if (FirstIteration) { |
6735 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
6736 | << BaseType << 1 << Base->getSourceRange() |
6737 | << FixItHint::CreateReplacement(OpLoc, "."); |
6738 | OpKind = tok::period; |
6739 | break; |
6740 | } |
6741 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
6742 | << BaseType << Base->getSourceRange(); |
6743 | CallExpr *CE = dyn_cast<CallExpr>(Base); |
6744 | if (Decl *CD = (CE ? CE->getCalleeDecl() : nullptr)) { |
6745 | Diag(CD->getBeginLoc(), |
6746 | diag::note_member_reference_arrow_from_operator_arrow); |
6747 | } |
6748 | } |
6749 | return ExprError(); |
6750 | } |
6751 | Base = Result.get(); |
6752 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
6753 | OperatorArrows.push_back(OpCall->getDirectCallee()); |
6754 | BaseType = Base->getType(); |
6755 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
6756 | if (!CTypes.insert(CBaseType).second) { |
6757 | Diag(OpLoc, diag::err_operator_arrow_circular) << StartingType; |
6758 | noteOperatorArrows(*this, OperatorArrows); |
6759 | return ExprError(); |
6760 | } |
6761 | FirstIteration = false; |
6762 | } |
6763 | |
6764 | if (OpKind == tok::arrow && |
6765 | (BaseType->isPointerType() || BaseType->isObjCObjectPointerType())) |
6766 | BaseType = BaseType->getPointeeType(); |
6767 | } |
6768 | |
6769 | |
6770 | |
6771 | if (BaseType->isObjCObjectPointerType()) |
6772 | BaseType = BaseType->getPointeeType(); |
6773 | |
6774 | |
6775 | |
6776 | |
6777 | |
6778 | |
6779 | |
6780 | |
6781 | |
6782 | |
6783 | |
6784 | |
6785 | if (BaseType->isObjCObjectOrInterfaceType()) { |
6786 | ObjectType = ParsedType::make(BaseType); |
6787 | MayBePseudoDestructor = true; |
6788 | return Base; |
6789 | } else if (!BaseType->isRecordType()) { |
6790 | ObjectType = nullptr; |
6791 | MayBePseudoDestructor = true; |
6792 | return Base; |
6793 | } |
6794 | |
6795 | |
6796 | |
6797 | |
6798 | |
6799 | |
6800 | if (!BaseType->isDependentType() && |
6801 | !isThisOutsideMemberFunctionBody(BaseType) && |
6802 | RequireCompleteType(OpLoc, BaseType, diag::err_incomplete_member_access)) |
6803 | return ExprError(); |
6804 | |
6805 | |
6806 | |
6807 | |
6808 | |
6809 | |
6810 | ObjectType = ParsedType::make(BaseType); |
6811 | return Base; |
6812 | } |
6813 | |
6814 | static bool CheckArrow(Sema& S, QualType& ObjectType, Expr *&Base, |
6815 | tok::TokenKind& OpKind, SourceLocation OpLoc) { |
6816 | if (Base->hasPlaceholderType()) { |
6817 | ExprResult result = S.CheckPlaceholderExpr(Base); |
6818 | if (result.isInvalid()) return true; |
6819 | Base = result.get(); |
6820 | } |
6821 | ObjectType = Base->getType(); |
6822 | |
6823 | |
6824 | |
6825 | |
6826 | |
6827 | |
6828 | |
6829 | if (OpKind == tok::arrow) { |
6830 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
6831 | ObjectType = Ptr->getPointeeType(); |
6832 | } else if (!Base->isTypeDependent()) { |
6833 | |
6834 | S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
6835 | << ObjectType << true |
6836 | << FixItHint::CreateReplacement(OpLoc, "."); |
6837 | if (S.isSFINAEContext()) |
6838 | return true; |
6839 | |
6840 | OpKind = tok::period; |
6841 | } |
6842 | } |
6843 | |
6844 | return false; |
6845 | } |
6846 | |
6847 | |
6848 | |
6849 | static bool |
6850 | canRecoverDotPseudoDestructorCallsOnPointerObjects(Sema &SemaRef, |
6851 | QualType DestructedType) { |
6852 | |
6853 | if (auto *RD = DestructedType->getAsCXXRecordDecl()) { |
6854 | if (RD->hasDefinition()) |
6855 | if (CXXDestructorDecl *D = SemaRef.LookupDestructor(RD)) |
6856 | return SemaRef.CanUseDecl(D, ); |
6857 | return false; |
6858 | } |
6859 | |
6860 | |
6861 | return DestructedType->isDependentType() || DestructedType->isScalarType() || |
6862 | DestructedType->isVectorType(); |
6863 | } |
6864 | |
6865 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
6866 | SourceLocation OpLoc, |
6867 | tok::TokenKind OpKind, |
6868 | const CXXScopeSpec &SS, |
6869 | TypeSourceInfo *ScopeTypeInfo, |
6870 | SourceLocation CCLoc, |
6871 | SourceLocation TildeLoc, |
6872 | PseudoDestructorTypeStorage Destructed) { |
6873 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
6874 | |
6875 | QualType ObjectType; |
6876 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
6877 | return ExprError(); |
6878 | |
6879 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType() && |
6880 | !ObjectType->isVectorType()) { |
6881 | if (getLangOpts().MSVCCompat && ObjectType->isVoidType()) |
6882 | Diag(OpLoc, diag::ext_pseudo_dtor_on_void) << Base->getSourceRange(); |
6883 | else { |
6884 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
6885 | << ObjectType << Base->getSourceRange(); |
6886 | return ExprError(); |
6887 | } |
6888 | } |
6889 | |
6890 | |
6891 | |
6892 | |
6893 | if (DestructedTypeInfo) { |
6894 | QualType DestructedType = DestructedTypeInfo->getType(); |
6895 | SourceLocation DestructedTypeStart |
6896 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
6897 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { |
6898 | if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
6899 | |
6900 | |
6901 | |
6902 | if (OpKind == tok::period && ObjectType->isPointerType() && |
6903 | Context.hasSameUnqualifiedType(DestructedType, |
6904 | ObjectType->getPointeeType())) { |
6905 | auto Diagnostic = |
6906 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
6907 | << ObjectType << << Base->getSourceRange(); |
6908 | |
6909 | |
6910 | if (canRecoverDotPseudoDestructorCallsOnPointerObjects( |
6911 | *this, DestructedType)) |
6912 | Diagnostic << FixItHint::CreateReplacement(OpLoc, "->"); |
6913 | |
6914 | |
6915 | |
6916 | ObjectType = DestructedType; |
6917 | OpKind = tok::arrow; |
6918 | } else { |
6919 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
6920 | << ObjectType << DestructedType << Base->getSourceRange() |
6921 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
6922 | |
6923 | |
6924 | DestructedType = ObjectType; |
6925 | DestructedTypeInfo = |
6926 | Context.getTrivialTypeSourceInfo(ObjectType, DestructedTypeStart); |
6927 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
6928 | } |
6929 | } else if (DestructedType.getObjCLifetime() != |
6930 | ObjectType.getObjCLifetime()) { |
6931 | |
6932 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { |
6933 | |
6934 | |
6935 | } else { |
6936 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) |
6937 | << ObjectType << DestructedType << Base->getSourceRange() |
6938 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
6939 | } |
6940 | |
6941 | |
6942 | DestructedType = ObjectType; |
6943 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
6944 | DestructedTypeStart); |
6945 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
6946 | } |
6947 | } |
6948 | } |
6949 | |
6950 | |
6951 | |
6952 | |
6953 | |
6954 | |
6955 | |
6956 | |
6957 | if (ScopeTypeInfo) { |
6958 | QualType ScopeType = ScopeTypeInfo->getType(); |
6959 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
6960 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
6961 | |
6962 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
6963 | diag::err_pseudo_dtor_type_mismatch) |
6964 | << ObjectType << ScopeType << Base->getSourceRange() |
6965 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
6966 | |
6967 | ScopeType = QualType(); |
6968 | ScopeTypeInfo = nullptr; |
6969 | } |
6970 | } |
6971 | |
6972 | Expr *Result |
6973 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
6974 | OpKind == tok::arrow, OpLoc, |
6975 | SS.getWithLocInContext(Context), |
6976 | ScopeTypeInfo, |
6977 | CCLoc, |
6978 | TildeLoc, |
6979 | Destructed); |
6980 | |
6981 | return Result; |
6982 | } |
6983 | |
6984 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
6985 | SourceLocation OpLoc, |
6986 | tok::TokenKind OpKind, |
6987 | CXXScopeSpec &SS, |
6988 | UnqualifiedId &FirstTypeName, |
6989 | SourceLocation CCLoc, |
6990 | SourceLocation TildeLoc, |
6991 | UnqualifiedId &SecondTypeName) { |
6992 | (0) . __assert_fail ("(FirstTypeName.getKind() == UnqualifiedIdKind..IK_TemplateId || FirstTypeName.getKind() == UnqualifiedIdKind..IK_Identifier) && \"Invalid first type name in pseudo-destructor\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6994, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((FirstTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
6993 | (0) . __assert_fail ("(FirstTypeName.getKind() == UnqualifiedIdKind..IK_TemplateId || FirstTypeName.getKind() == UnqualifiedIdKind..IK_Identifier) && \"Invalid first type name in pseudo-destructor\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6994, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> FirstTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) && |
6994 | (0) . __assert_fail ("(FirstTypeName.getKind() == UnqualifiedIdKind..IK_TemplateId || FirstTypeName.getKind() == UnqualifiedIdKind..IK_Identifier) && \"Invalid first type name in pseudo-destructor\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6994, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid first type name in pseudo-destructor"); |
6995 | (0) . __assert_fail ("(SecondTypeName.getKind() == UnqualifiedIdKind..IK_TemplateId || SecondTypeName.getKind() == UnqualifiedIdKind..IK_Identifier) && \"Invalid second type name in pseudo-destructor\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6997, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((SecondTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
6996 | (0) . __assert_fail ("(SecondTypeName.getKind() == UnqualifiedIdKind..IK_TemplateId || SecondTypeName.getKind() == UnqualifiedIdKind..IK_Identifier) && \"Invalid second type name in pseudo-destructor\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6997, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> SecondTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) && |
6997 | (0) . __assert_fail ("(SecondTypeName.getKind() == UnqualifiedIdKind..IK_TemplateId || SecondTypeName.getKind() == UnqualifiedIdKind..IK_Identifier) && \"Invalid second type name in pseudo-destructor\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 6997, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid second type name in pseudo-destructor"); |
6998 | |
6999 | QualType ObjectType; |
7000 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
7001 | return ExprError(); |
7002 | |
7003 | |
7004 | |
7005 | ParsedType ObjectTypePtrForLookup; |
7006 | if (!SS.isSet()) { |
7007 | if (ObjectType->isRecordType()) |
7008 | ObjectTypePtrForLookup = ParsedType::make(ObjectType); |
7009 | else if (ObjectType->isDependentType()) |
7010 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
7011 | } |
7012 | |
7013 | |
7014 | |
7015 | QualType DestructedType; |
7016 | TypeSourceInfo *DestructedTypeInfo = nullptr; |
7017 | PseudoDestructorTypeStorage Destructed; |
7018 | if (SecondTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) { |
7019 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
7020 | SecondTypeName.StartLocation, |
7021 | S, &SS, true, false, ObjectTypePtrForLookup, |
7022 | ); |
7023 | if (!T && |
7024 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
7025 | (!SS.isSet() && ObjectType->isDependentType()))) { |
7026 | |
7027 | |
7028 | |
7029 | |
7030 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
7031 | SecondTypeName.StartLocation); |
7032 | } else if (!T) { |
7033 | Diag(SecondTypeName.StartLocation, |
7034 | diag::err_pseudo_dtor_destructor_non_type) |
7035 | << SecondTypeName.Identifier << ObjectType; |
7036 | if (isSFINAEContext()) |
7037 | return ExprError(); |
7038 | |
7039 | |
7040 | DestructedType = ObjectType; |
7041 | } else |
7042 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
7043 | } else { |
7044 | |
7045 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
7046 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
7047 | TemplateId->NumArgs); |
7048 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
7049 | TemplateId->TemplateKWLoc, |
7050 | TemplateId->Template, |
7051 | TemplateId->Name, |
7052 | TemplateId->TemplateNameLoc, |
7053 | TemplateId->LAngleLoc, |
7054 | TemplateArgsPtr, |
7055 | TemplateId->RAngleLoc, |
7056 | ); |
7057 | if (T.isInvalid() || !T.get()) { |
7058 | |
7059 | DestructedType = ObjectType; |
7060 | } else |
7061 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
7062 | } |
7063 | |
7064 | |
7065 | |
7066 | if (!DestructedType.isNull()) { |
7067 | if (!DestructedTypeInfo) |
7068 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
7069 | SecondTypeName.StartLocation); |
7070 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
7071 | } |
7072 | |
7073 | |
7074 | TypeSourceInfo *ScopeTypeInfo = nullptr; |
7075 | QualType ScopeType; |
7076 | if (FirstTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
7077 | FirstTypeName.Identifier) { |
7078 | if (FirstTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) { |
7079 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
7080 | FirstTypeName.StartLocation, |
7081 | S, &SS, true, false, ObjectTypePtrForLookup, |
7082 | ); |
7083 | if (!T) { |
7084 | Diag(FirstTypeName.StartLocation, |
7085 | diag::err_pseudo_dtor_destructor_non_type) |
7086 | << FirstTypeName.Identifier << ObjectType; |
7087 | |
7088 | if (isSFINAEContext()) |
7089 | return ExprError(); |
7090 | |
7091 | |
7092 | ScopeType = QualType(); |
7093 | } else |
7094 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
7095 | } else { |
7096 | |
7097 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
7098 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
7099 | TemplateId->NumArgs); |
7100 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
7101 | TemplateId->TemplateKWLoc, |
7102 | TemplateId->Template, |
7103 | TemplateId->Name, |
7104 | TemplateId->TemplateNameLoc, |
7105 | TemplateId->LAngleLoc, |
7106 | TemplateArgsPtr, |
7107 | TemplateId->RAngleLoc, |
7108 | ); |
7109 | if (T.isInvalid() || !T.get()) { |
7110 | |
7111 | ScopeType = QualType(); |
7112 | } else |
7113 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
7114 | } |
7115 | } |
7116 | |
7117 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
7118 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
7119 | FirstTypeName.StartLocation); |
7120 | |
7121 | |
7122 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
7123 | ScopeTypeInfo, CCLoc, TildeLoc, |
7124 | Destructed); |
7125 | } |
7126 | |
7127 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
7128 | SourceLocation OpLoc, |
7129 | tok::TokenKind OpKind, |
7130 | SourceLocation TildeLoc, |
7131 | const DeclSpec& DS) { |
7132 | QualType ObjectType; |
7133 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
7134 | return ExprError(); |
7135 | |
7136 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc(), |
7137 | false); |
7138 | |
7139 | TypeLocBuilder TLB; |
7140 | DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); |
7141 | DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); |
7142 | TypeSourceInfo *DestructedTypeInfo = TLB.getTypeSourceInfo(Context, T); |
7143 | PseudoDestructorTypeStorage Destructed(DestructedTypeInfo); |
7144 | |
7145 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, CXXScopeSpec(), |
7146 | nullptr, SourceLocation(), TildeLoc, |
7147 | Destructed); |
7148 | } |
7149 | |
7150 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl, |
7151 | CXXConversionDecl *Method, |
7152 | bool HadMultipleCandidates) { |
7153 | |
7154 | |
7155 | ExprResult Exp = PerformObjectArgumentInitialization(E, , |
7156 | FoundDecl, Method); |
7157 | if (Exp.isInvalid()) |
7158 | return true; |
7159 | |
7160 | if (Method->getParent()->isLambda() && |
7161 | Method->getConversionType()->isBlockPointerType()) { |
7162 | |
7163 | |
7164 | Expr *SubE = E; |
7165 | CastExpr *CE = dyn_cast<CastExpr>(SubE); |
7166 | if (CE && CE->getCastKind() == CK_NoOp) |
7167 | SubE = CE->getSubExpr(); |
7168 | SubE = SubE->IgnoreParens(); |
7169 | if (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(SubE)) |
7170 | SubE = BE->getSubExpr(); |
7171 | if (isa<LambdaExpr>(SubE)) { |
7172 | |
7173 | |
7174 | |
7175 | |
7176 | |
7177 | DiagnosticErrorTrap Trap(Diags); |
7178 | PushExpressionEvaluationContext( |
7179 | ExpressionEvaluationContext::PotentiallyEvaluated); |
7180 | ExprResult BlockExp = BuildBlockForLambdaConversion( |
7181 | Exp.get()->getExprLoc(), Exp.get()->getExprLoc(), Method, Exp.get()); |
7182 | PopExpressionEvaluationContext(); |
7183 | |
7184 | if (BlockExp.isInvalid()) |
7185 | Diag(Exp.get()->getExprLoc(), diag::note_lambda_to_block_conv); |
7186 | return BlockExp; |
7187 | } |
7188 | } |
7189 | |
7190 | MemberExpr *ME = new (Context) MemberExpr( |
7191 | Exp.get(), , SourceLocation(), Method, SourceLocation(), |
7192 | Context.BoundMemberTy, VK_RValue, OK_Ordinary); |
7193 | if (HadMultipleCandidates) |
7194 | ME->setHadMultipleCandidates(true); |
7195 | MarkMemberReferenced(ME); |
7196 | |
7197 | QualType ResultType = Method->getReturnType(); |
7198 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
7199 | ResultType = ResultType.getNonLValueExprType(Context); |
7200 | |
7201 | CXXMemberCallExpr *CE = CXXMemberCallExpr::Create( |
7202 | Context, ME, {}, ResultType, VK, Exp.get()->getEndLoc()); |
7203 | |
7204 | if (CheckFunctionCall(Method, CE, |
7205 | Method->getType()->castAs<FunctionProtoType>())) |
7206 | return ExprError(); |
7207 | |
7208 | return CE; |
7209 | } |
7210 | |
7211 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
7212 | SourceLocation RParen) { |
7213 | |
7214 | |
7215 | |
7216 | ExprResult R = CheckPlaceholderExpr(Operand); |
7217 | if (R.isInvalid()) |
7218 | return R; |
7219 | |
7220 | |
7221 | Operand = R.get(); |
7222 | |
7223 | if (!inTemplateInstantiation() && Operand->HasSideEffects(Context, false)) { |
7224 | |
7225 | |
7226 | Diag(Operand->getExprLoc(), diag::warn_side_effects_unevaluated_context); |
7227 | } |
7228 | |
7229 | CanThrowResult CanThrow = canThrow(Operand); |
7230 | return new (Context) |
7231 | CXXNoexceptExpr(Context.BoolTy, Operand, CanThrow, KeyLoc, RParen); |
7232 | } |
7233 | |
7234 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
7235 | Expr *Operand, SourceLocation RParen) { |
7236 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
7237 | } |
7238 | |
7239 | static bool IsSpecialDiscardedValue(Expr *E) { |
7240 | |
7241 | |
7242 | |
7243 | |
7244 | |
7245 | E = E->IgnoreParens(); |
7246 | |
7247 | |
7248 | if (isa<DeclRefExpr>(E)) |
7249 | return true; |
7250 | |
7251 | |
7252 | if (isa<ArraySubscriptExpr>(E)) |
7253 | return true; |
7254 | |
7255 | |
7256 | if (isa<MemberExpr>(E)) |
7257 | return true; |
7258 | |
7259 | |
7260 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
7261 | if (UO->getOpcode() == UO_Deref) |
7262 | return true; |
7263 | |
7264 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
7265 | |
7266 | if (BO->isPtrMemOp()) |
7267 | return true; |
7268 | |
7269 | |
7270 | if (BO->getOpcode() == BO_Comma) |
7271 | return IsSpecialDiscardedValue(BO->getRHS()); |
7272 | } |
7273 | |
7274 | |
7275 | |
7276 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) |
7277 | return IsSpecialDiscardedValue(CO->getTrueExpr()) && |
7278 | IsSpecialDiscardedValue(CO->getFalseExpr()); |
7279 | |
7280 | if (BinaryConditionalOperator *BCO = |
7281 | dyn_cast<BinaryConditionalOperator>(E)) { |
7282 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(BCO->getTrueExpr())) |
7283 | return IsSpecialDiscardedValue(OVE->getSourceExpr()) && |
7284 | IsSpecialDiscardedValue(BCO->getFalseExpr()); |
7285 | } |
7286 | |
7287 | |
7288 | if (isa<PseudoObjectExpr>(E) || isa<ObjCIvarRefExpr>(E)) |
7289 | return true; |
7290 | |
7291 | return false; |
7292 | } |
7293 | |
7294 | |
7295 | |
7296 | ExprResult Sema::IgnoredValueConversions(Expr *E) { |
7297 | if (E->hasPlaceholderType()) { |
7298 | ExprResult result = CheckPlaceholderExpr(E); |
7299 | if (result.isInvalid()) return E; |
7300 | E = result.get(); |
7301 | } |
7302 | |
7303 | |
7304 | |
7305 | |
7306 | |
7307 | if (E->isRValue()) { |
7308 | |
7309 | |
7310 | |
7311 | |
7312 | if (!getLangOpts().CPlusPlus && E->getType()->isFunctionType()) |
7313 | return DefaultFunctionArrayConversion(E); |
7314 | |
7315 | return E; |
7316 | } |
7317 | |
7318 | if (getLangOpts().CPlusPlus) { |
7319 | |
7320 | |
7321 | |
7322 | |
7323 | if (getLangOpts().CPlusPlus11 && E->isGLValue() && |
7324 | E->getType().isVolatileQualified() && |
7325 | IsSpecialDiscardedValue(E)) { |
7326 | ExprResult Res = DefaultLvalueConversion(E); |
7327 | if (Res.isInvalid()) |
7328 | return E; |
7329 | E = Res.get(); |
7330 | } |
7331 | |
7332 | |
7333 | |
7334 | |
7335 | |
7336 | |
7337 | |
7338 | |
7339 | |
7340 | |
7341 | return E; |
7342 | } |
7343 | |
7344 | |
7345 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
7346 | if (!T->getDecl()->isComplete()) { |
7347 | |
7348 | E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).get(); |
7349 | return E; |
7350 | } |
7351 | } |
7352 | |
7353 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
7354 | if (Res.isInvalid()) |
7355 | return E; |
7356 | E = Res.get(); |
7357 | |
7358 | if (!E->getType()->isVoidType()) |
7359 | RequireCompleteType(E->getExprLoc(), E->getType(), |
7360 | diag::err_incomplete_type); |
7361 | return E; |
7362 | } |
7363 | |
7364 | |
7365 | |
7366 | |
7367 | |
7368 | |
7369 | |
7370 | |
7371 | |
7372 | |
7373 | |
7374 | |
7375 | |
7376 | static inline bool VariableCanNeverBeAConstantExpression(VarDecl *Var, |
7377 | ASTContext &Context) { |
7378 | if (isa<ParmVarDecl>(Var)) return true; |
7379 | const VarDecl *DefVD = nullptr; |
7380 | |
7381 | |
7382 | if (!Var->getAnyInitializer(DefVD)) return true; |
7383 | assert(DefVD); |
7384 | if (DefVD->isWeak()) return false; |
7385 | EvaluatedStmt *Eval = DefVD->ensureEvaluatedStmt(); |
7386 | |
7387 | Expr *Init = cast<Expr>(Eval->Value); |
7388 | |
7389 | if (Var->getType()->isDependentType() || Init->isValueDependent()) { |
7390 | |
7391 | |
7392 | |
7393 | return false; |
7394 | } |
7395 | |
7396 | return !IsVariableAConstantExpression(Var, Context); |
7397 | } |
7398 | |
7399 | |
7400 | |
7401 | |
7402 | |
7403 | |
7404 | |
7405 | |
7406 | static void CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures( |
7407 | Expr *const FE, LambdaScopeInfo *const CurrentLSI, Sema &S) { |
7408 | |
7409 | assert(!S.isUnevaluatedContext()); |
7410 | isDependentContext()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 7410, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(S.CurContext->isDependentContext()); |
7411 | #ifndef NDEBUG |
7412 | DeclContext *DC = S.CurContext; |
7413 | while (DC && isa<CapturedDecl>(DC)) |
7414 | DC = DC->getParent(); |
7415 | (0) . __assert_fail ("CurrentLSI->CallOperator == DC && \"The current call operator must be synchronized with Sema's CurContext\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 7417, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert( |
7416 | (0) . __assert_fail ("CurrentLSI->CallOperator == DC && \"The current call operator must be synchronized with Sema's CurContext\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 7417, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> CurrentLSI->CallOperator == DC && |
7417 | (0) . __assert_fail ("CurrentLSI->CallOperator == DC && \"The current call operator must be synchronized with Sema's CurContext\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 7417, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "The current call operator must be synchronized with Sema's CurContext"); |
7418 | #endif |
7419 | |
7420 | const bool IsFullExprInstantiationDependent = FE->isInstantiationDependent(); |
7421 | |
7422 | |
7423 | |
7424 | |
7425 | const unsigned NumPotentialCaptures = |
7426 | CurrentLSI->getNumPotentialVariableCaptures(); |
7427 | for (unsigned I = 0; I != NumPotentialCaptures; ++I) { |
7428 | Expr *VarExpr = nullptr; |
7429 | VarDecl *Var = nullptr; |
7430 | CurrentLSI->getPotentialVariableCapture(I, Var, VarExpr); |
7431 | |
7432 | |
7433 | |
7434 | |
7435 | |
7436 | |
7437 | |
7438 | |
7439 | |
7440 | |
7441 | |
7442 | if (CurrentLSI->isVariableExprMarkedAsNonODRUsed(VarExpr) && |
7443 | !IsFullExprInstantiationDependent) |
7444 | continue; |
7445 | |
7446 | |
7447 | |
7448 | if (const Optional<unsigned> Index = |
7449 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
7450 | S.FunctionScopes, Var, S)) { |
7451 | const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); |
7452 | MarkVarDeclODRUsed(Var, VarExpr->getExprLoc(), S, |
7453 | &FunctionScopeIndexOfCapturableLambda); |
7454 | } |
7455 | const bool IsVarNeverAConstantExpression = |
7456 | VariableCanNeverBeAConstantExpression(Var, S.Context); |
7457 | if (!IsFullExprInstantiationDependent || IsVarNeverAConstantExpression) { |
7458 | |
7459 | |
7460 | |
7461 | |
7462 | |
7463 | |
7464 | QualType CaptureType, DeclRefType; |
7465 | SourceLocation ExprLoc = VarExpr->getExprLoc(); |
7466 | if (S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit, |
7467 | SourceLocation(), |
7468 | , CaptureType, |
7469 | DeclRefType, nullptr)) { |
7470 | |
7471 | |
7472 | S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit, |
7473 | SourceLocation(), |
7474 | , CaptureType, |
7475 | DeclRefType, nullptr); |
7476 | } |
7477 | } |
7478 | } |
7479 | |
7480 | |
7481 | if (CurrentLSI->hasPotentialThisCapture()) { |
7482 | |
7483 | |
7484 | if (const Optional<unsigned> Index = |
7485 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
7486 | S.FunctionScopes, nullptr, S)) { |
7487 | const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); |
7488 | S.CheckCXXThisCapture(CurrentLSI->PotentialThisCaptureLocation, |
7489 | false, true, |
7490 | &FunctionScopeIndexOfCapturableLambda); |
7491 | } |
7492 | } |
7493 | |
7494 | |
7495 | CurrentLSI->clearPotentialCaptures(); |
7496 | } |
7497 | |
7498 | static ExprResult attemptRecovery(Sema &SemaRef, |
7499 | const TypoCorrectionConsumer &Consumer, |
7500 | const TypoCorrection &TC) { |
7501 | LookupResult R(SemaRef, Consumer.getLookupResult().getLookupNameInfo(), |
7502 | Consumer.getLookupResult().getLookupKind()); |
7503 | const CXXScopeSpec *SS = Consumer.getSS(); |
7504 | CXXScopeSpec NewSS; |
7505 | |
7506 | |
7507 | if (auto *NNS = TC.getCorrectionSpecifier()) |
7508 | NewSS.MakeTrivial(SemaRef.Context, NNS, TC.getCorrectionRange()); |
7509 | else if (SS && !TC.WillReplaceSpecifier()) |
7510 | NewSS = *SS; |
7511 | |
7512 | if (auto *ND = TC.getFoundDecl()) { |
7513 | R.setLookupName(ND->getDeclName()); |
7514 | R.addDecl(ND); |
7515 | if (ND->isCXXClassMember()) { |
7516 | |
7517 | CXXRecordDecl *Record = nullptr; |
7518 | if (auto *NNS = TC.getCorrectionSpecifier()) |
7519 | Record = NNS->getAsType()->getAsCXXRecordDecl(); |
7520 | if (!Record) |
7521 | Record = |
7522 | dyn_cast<CXXRecordDecl>(ND->getDeclContext()->getRedeclContext()); |
7523 | if (Record) |
7524 | R.setNamingClass(Record); |
7525 | |
7526 | |
7527 | |
7528 | bool MightBeImplicitMember; |
7529 | if (!Consumer.isAddressOfOperand()) |
7530 | MightBeImplicitMember = true; |
7531 | else if (!NewSS.isEmpty()) |
7532 | MightBeImplicitMember = false; |
7533 | else if (R.isOverloadedResult()) |
7534 | MightBeImplicitMember = false; |
7535 | else if (R.isUnresolvableResult()) |
7536 | MightBeImplicitMember = true; |
7537 | else |
7538 | MightBeImplicitMember = isa<FieldDecl>(ND) || |
7539 | isa<IndirectFieldDecl>(ND) || |
7540 | isa<MSPropertyDecl>(ND); |
7541 | |
7542 | if (MightBeImplicitMember) |
7543 | return SemaRef.BuildPossibleImplicitMemberExpr( |
7544 | NewSS, SourceLocation(), R, |
7545 | nullptr, nullptr); |
7546 | } else if (auto *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
7547 | return SemaRef.LookupInObjCMethod(R, Consumer.getScope(), |
7548 | Ivar->getIdentifier()); |
7549 | } |
7550 | } |
7551 | |
7552 | return SemaRef.BuildDeclarationNameExpr(NewSS, R, false, |
7553 | true); |
7554 | } |
7555 | |
7556 | namespace { |
7557 | class FindTypoExprs : public RecursiveASTVisitor<FindTypoExprs> { |
7558 | llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs; |
7559 | |
7560 | public: |
7561 | explicit FindTypoExprs(llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs) |
7562 | : TypoExprs(TypoExprs) {} |
7563 | bool VisitTypoExpr(TypoExpr *TE) { |
7564 | TypoExprs.insert(TE); |
7565 | return true; |
7566 | } |
7567 | }; |
7568 | |
7569 | class TransformTypos : public TreeTransform<TransformTypos> { |
7570 | typedef TreeTransform<TransformTypos> BaseTransform; |
7571 | |
7572 | VarDecl *InitDecl; |
7573 | |
7574 | llvm::function_ref<ExprResult(Expr *)> ExprFilter; |
7575 | llvm::SmallSetVector<TypoExpr *, 2> TypoExprs, AmbiguousTypoExprs; |
7576 | llvm::SmallDenseMap<TypoExpr *, ExprResult, 2> TransformCache; |
7577 | llvm::SmallDenseMap<OverloadExpr *, Expr *, 4> OverloadResolution; |
7578 | |
7579 | |
7580 | |
7581 | |
7582 | |
7583 | void EmitAllDiagnostics() { |
7584 | for (TypoExpr *TE : TypoExprs) { |
7585 | auto &State = SemaRef.getTypoExprState(TE); |
7586 | if (State.DiagHandler) { |
7587 | TypoCorrection TC = State.Consumer->getCurrentCorrection(); |
7588 | ExprResult Replacement = TransformCache[TE]; |
7589 | |
7590 | |
7591 | |
7592 | |
7593 | |
7594 | |
7595 | if (auto *ND = getDeclFromExpr( |
7596 | Replacement.isInvalid() ? nullptr : Replacement.get())) |
7597 | TC.setCorrectionDecl(ND); |
7598 | |
7599 | State.DiagHandler(TC); |
7600 | } |
7601 | SemaRef.clearDelayedTypo(TE); |
7602 | } |
7603 | } |
7604 | |
7605 | |
7606 | |
7607 | |
7608 | |
7609 | |
7610 | |
7611 | |
7612 | |
7613 | |
7614 | bool CheckAndAdvanceTypoExprCorrectionStreams() { |
7615 | for (auto TE : TypoExprs) { |
7616 | auto &State = SemaRef.getTypoExprState(TE); |
7617 | TransformCache.erase(TE); |
7618 | if (!State.Consumer->finished()) |
7619 | return true; |
7620 | State.Consumer->resetCorrectionStream(); |
7621 | } |
7622 | return false; |
7623 | } |
7624 | |
7625 | NamedDecl *getDeclFromExpr(Expr *E) { |
7626 | if (auto *OE = dyn_cast_or_null<OverloadExpr>(E)) |
7627 | E = OverloadResolution[OE]; |
7628 | |
7629 | if (!E) |
7630 | return nullptr; |
7631 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
7632 | return DRE->getFoundDecl(); |
7633 | if (auto *ME = dyn_cast<MemberExpr>(E)) |
7634 | return ME->getFoundDecl(); |
7635 | |
7636 | |
7637 | |
7638 | return nullptr; |
7639 | } |
7640 | |
7641 | ExprResult TryTransform(Expr *E) { |
7642 | Sema::SFINAETrap Trap(SemaRef); |
7643 | ExprResult Res = TransformExpr(E); |
7644 | if (Trap.hasErrorOccurred() || Res.isInvalid()) |
7645 | return ExprError(); |
7646 | |
7647 | return ExprFilter(Res.get()); |
7648 | } |
7649 | |
7650 | public: |
7651 | TransformTypos(Sema &SemaRef, VarDecl *InitDecl, llvm::function_ref<ExprResult(Expr *)> Filter) |
7652 | : BaseTransform(SemaRef), InitDecl(InitDecl), ExprFilter(Filter) {} |
7653 | |
7654 | ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc, |
7655 | MultiExprArg Args, |
7656 | SourceLocation RParenLoc, |
7657 | Expr *ExecConfig = nullptr) { |
7658 | auto Result = BaseTransform::RebuildCallExpr(Callee, LParenLoc, Args, |
7659 | RParenLoc, ExecConfig); |
7660 | if (auto *OE = dyn_cast<OverloadExpr>(Callee)) { |
7661 | if (Result.isUsable()) { |
7662 | Expr *ResultCall = Result.get(); |
7663 | if (auto *BE = dyn_cast<CXXBindTemporaryExpr>(ResultCall)) |
7664 | ResultCall = BE->getSubExpr(); |
7665 | if (auto *CE = dyn_cast<CallExpr>(ResultCall)) |
7666 | OverloadResolution[OE] = CE->getCallee(); |
7667 | } |
7668 | } |
7669 | return Result; |
7670 | } |
7671 | |
7672 | ExprResult TransformLambdaExpr(LambdaExpr *E) { return Owned(E); } |
7673 | |
7674 | ExprResult TransformBlockExpr(BlockExpr *E) { return Owned(E); } |
7675 | |
7676 | ExprResult Transform(Expr *E) { |
7677 | ExprResult Res; |
7678 | while (true) { |
7679 | Res = TryTransform(E); |
7680 | |
7681 | |
7682 | |
7683 | if (!Res.isInvalid() || |
7684 | !CheckAndAdvanceTypoExprCorrectionStreams()) |
7685 | break; |
7686 | } |
7687 | |
7688 | |
7689 | |
7690 | |
7691 | |
7692 | |
7693 | |
7694 | |
7695 | |
7696 | SemaRef.DisableTypoCorrection = true; |
7697 | while (!AmbiguousTypoExprs.empty()) { |
7698 | auto TE = AmbiguousTypoExprs.back(); |
7699 | auto Cached = TransformCache[TE]; |
7700 | auto &State = SemaRef.getTypoExprState(TE); |
7701 | State.Consumer->saveCurrentPosition(); |
7702 | TransformCache.erase(TE); |
7703 | if (!TryTransform(E).isInvalid()) { |
7704 | State.Consumer->resetCorrectionStream(); |
7705 | TransformCache.erase(TE); |
7706 | Res = ExprError(); |
7707 | break; |
7708 | } |
7709 | AmbiguousTypoExprs.remove(TE); |
7710 | State.Consumer->restoreSavedPosition(); |
7711 | TransformCache[TE] = Cached; |
7712 | } |
7713 | SemaRef.DisableTypoCorrection = false; |
7714 | |
7715 | |
7716 | if (!Res.isUsable()) |
7717 | FindTypoExprs(TypoExprs).TraverseStmt(E); |
7718 | |
7719 | EmitAllDiagnostics(); |
7720 | |
7721 | return Res; |
7722 | } |
7723 | |
7724 | ExprResult TransformTypoExpr(TypoExpr *E) { |
7725 | |
7726 | |
7727 | |
7728 | auto &CacheEntry = TransformCache[E]; |
7729 | if (!TypoExprs.insert(E) && !CacheEntry.isUnset()) { |
7730 | return CacheEntry; |
7731 | } |
7732 | |
7733 | auto &State = SemaRef.getTypoExprState(E); |
7734 | (0) . __assert_fail ("State.Consumer && \"Cannot transform a cleared TypoExpr\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 7734, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(State.Consumer && "Cannot transform a cleared TypoExpr"); |
7735 | |
7736 | |
7737 | |
7738 | while (TypoCorrection TC = State.Consumer->getNextCorrection()) { |
7739 | if (InitDecl && TC.getFoundDecl() == InitDecl) |
7740 | continue; |
7741 | |
7742 | |
7743 | ExprResult NE = State.RecoveryHandler ? |
7744 | State.RecoveryHandler(SemaRef, E, TC) : |
7745 | attemptRecovery(SemaRef, *State.Consumer, TC); |
7746 | if (!NE.isInvalid()) { |
7747 | |
7748 | |
7749 | |
7750 | TypoCorrection Next; |
7751 | if ((Next = State.Consumer->peekNextCorrection()) && |
7752 | Next.getEditDistance(false) == TC.getEditDistance(false)) { |
7753 | AmbiguousTypoExprs.insert(E); |
7754 | } else { |
7755 | AmbiguousTypoExprs.remove(E); |
7756 | } |
7757 | (0) . __assert_fail ("!NE.isUnset() && \"Typo was transformed into a valid-but-null ExprResult\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 7758, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!NE.isUnset() && |
7758 | (0) . __assert_fail ("!NE.isUnset() && \"Typo was transformed into a valid-but-null ExprResult\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 7758, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Typo was transformed into a valid-but-null ExprResult"); |
7759 | return CacheEntry = NE; |
7760 | } |
7761 | } |
7762 | return CacheEntry = ExprError(); |
7763 | } |
7764 | }; |
7765 | } |
7766 | |
7767 | ExprResult |
7768 | Sema::CorrectDelayedTyposInExpr(Expr *E, VarDecl *InitDecl, |
7769 | llvm::function_ref<ExprResult(Expr *)> Filter) { |
7770 | |
7771 | |
7772 | |
7773 | if (E && !ExprEvalContexts.empty() && ExprEvalContexts.back().NumTypos && |
7774 | (E->isTypeDependent() || E->isValueDependent() || |
7775 | E->isInstantiationDependent())) { |
7776 | auto TyposResolved = DelayedTypos.size(); |
7777 | auto Result = TransformTypos(*this, InitDecl, Filter).Transform(E); |
7778 | TyposResolved -= DelayedTypos.size(); |
7779 | if (Result.isInvalid() || Result.get() != E) { |
7780 | ExprEvalContexts.back().NumTypos -= TyposResolved; |
7781 | return Result; |
7782 | } |
7783 | (0) . __assert_fail ("TyposResolved == 0 && \"Corrected typo but got same Expr back?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaExprCXX.cpp", 7783, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TyposResolved == 0 && "Corrected typo but got same Expr back?"); |
7784 | } |
7785 | return E; |
7786 | } |
7787 | |
7788 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC, |
7789 | bool DiscardedValue, |
7790 | bool IsConstexpr) { |
7791 | ExprResult FullExpr = FE; |
7792 | |
7793 | if (!FullExpr.get()) |
7794 | return ExprError(); |
7795 | |
7796 | if (DiagnoseUnexpandedParameterPack(FullExpr.get())) |
7797 | return ExprError(); |
7798 | |
7799 | if (DiscardedValue) { |
7800 | |
7801 | if (getLangOpts().DebuggerCastResultToId && |
7802 | FullExpr.get()->getType() == Context.UnknownAnyTy) { |
7803 | FullExpr = forceUnknownAnyToType(FullExpr.get(), Context.getObjCIdType()); |
7804 | if (FullExpr.isInvalid()) |
7805 | return ExprError(); |
7806 | } |
7807 | |
7808 | FullExpr = CheckPlaceholderExpr(FullExpr.get()); |
7809 | if (FullExpr.isInvalid()) |
7810 | return ExprError(); |
7811 | |
7812 | FullExpr = IgnoredValueConversions(FullExpr.get()); |
7813 | if (FullExpr.isInvalid()) |
7814 | return ExprError(); |
7815 | |
7816 | DiagnoseUnusedExprResult(FullExpr.get()); |
7817 | } |
7818 | |
7819 | FullExpr = CorrectDelayedTyposInExpr(FullExpr.get()); |
7820 | if (FullExpr.isInvalid()) |
7821 | return ExprError(); |
7822 | |
7823 | CheckCompletedExpr(FullExpr.get(), CC, IsConstexpr); |
7824 | |
7825 | |
7826 | |
7827 | |
7828 | |
7829 | |
7830 | |
7831 | |
7832 | |
7833 | |
7834 | |
7835 | |
7836 | |
7837 | |
7838 | |
7839 | |
7840 | |
7841 | |
7842 | |
7843 | |
7844 | |
7845 | |
7846 | |
7847 | |
7848 | |
7849 | |
7850 | |
7851 | |
7852 | |
7853 | |
7854 | |
7855 | LambdaScopeInfo *const CurrentLSI = |
7856 | getCurLambda(); |
7857 | |
7858 | |
7859 | |
7860 | |
7861 | |
7862 | |
7863 | |
7864 | |
7865 | |
7866 | |
7867 | |
7868 | |
7869 | |
7870 | |
7871 | DeclContext *DC = CurContext; |
7872 | while (DC && isa<CapturedDecl>(DC)) |
7873 | DC = DC->getParent(); |
7874 | const bool IsInLambdaDeclContext = isLambdaCallOperator(DC); |
7875 | if (IsInLambdaDeclContext && CurrentLSI && |
7876 | CurrentLSI->hasPotentialCaptures() && !FullExpr.isInvalid()) |
7877 | CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(FE, CurrentLSI, |
7878 | *this); |
7879 | return MaybeCreateExprWithCleanups(FullExpr); |
7880 | } |
7881 | |
7882 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
7883 | if (!FullStmt) return StmtError(); |
7884 | |
7885 | return MaybeCreateStmtWithCleanups(FullStmt); |
7886 | } |
7887 | |
7888 | Sema::IfExistsResult |
7889 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, |
7890 | CXXScopeSpec &SS, |
7891 | const DeclarationNameInfo &TargetNameInfo) { |
7892 | DeclarationName TargetName = TargetNameInfo.getName(); |
7893 | if (!TargetName) |
7894 | return IER_DoesNotExist; |
7895 | |
7896 | |
7897 | if (TargetName.isDependentName()) |
7898 | return IER_Dependent; |
7899 | |
7900 | |
7901 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, |
7902 | Sema::NotForRedeclaration); |
7903 | LookupParsedName(R, S, &SS); |
7904 | R.suppressDiagnostics(); |
7905 | |
7906 | switch (R.getResultKind()) { |
7907 | case LookupResult::Found: |
7908 | case LookupResult::FoundOverloaded: |
7909 | case LookupResult::FoundUnresolvedValue: |
7910 | case LookupResult::Ambiguous: |
7911 | return IER_Exists; |
7912 | |
7913 | case LookupResult::NotFound: |
7914 | return IER_DoesNotExist; |
7915 | |
7916 | case LookupResult::NotFoundInCurrentInstantiation: |
7917 | return IER_Dependent; |
7918 | } |
7919 | |
7920 | llvm_unreachable("Invalid LookupResult Kind!"); |
7921 | } |
7922 | |
7923 | Sema::IfExistsResult |
7924 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc, |
7925 | bool IsIfExists, CXXScopeSpec &SS, |
7926 | UnqualifiedId &Name) { |
7927 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
7928 | |
7929 | |
7930 | auto UPPC = IsIfExists ? UPPC_IfExists : UPPC_IfNotExists; |
7931 | if (DiagnoseUnexpandedParameterPack(SS, UPPC) || |
7932 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC)) |
7933 | return IER_Error; |
7934 | |
7935 | return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo); |
7936 | } |
7937 | |