1 | //===- ASTContext.cpp - Context to hold long-lived AST nodes --------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file implements the ASTContext interface. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "clang/AST/ASTContext.h" |
14 | #include "ByteCode/Context.h" |
15 | #include "CXXABI.h" |
16 | #include "clang/AST/APValue.h" |
17 | #include "clang/AST/ASTConcept.h" |
18 | #include "clang/AST/ASTMutationListener.h" |
19 | #include "clang/AST/ASTStructuralEquivalence.h" |
20 | #include "clang/AST/ASTTypeTraits.h" |
21 | #include "clang/AST/Attr.h" |
22 | #include "clang/AST/AttrIterator.h" |
23 | #include "clang/AST/CharUnits.h" |
24 | #include "clang/AST/Comment.h" |
25 | #include "clang/AST/Decl.h" |
26 | #include "clang/AST/DeclBase.h" |
27 | #include "clang/AST/DeclCXX.h" |
28 | #include "clang/AST/DeclContextInternals.h" |
29 | #include "clang/AST/DeclObjC.h" |
30 | #include "clang/AST/DeclOpenMP.h" |
31 | #include "clang/AST/DeclTemplate.h" |
32 | #include "clang/AST/DeclarationName.h" |
33 | #include "clang/AST/DependenceFlags.h" |
34 | #include "clang/AST/Expr.h" |
35 | #include "clang/AST/ExprCXX.h" |
36 | #include "clang/AST/ExternalASTSource.h" |
37 | #include "clang/AST/Mangle.h" |
38 | #include "clang/AST/MangleNumberingContext.h" |
39 | #include "clang/AST/NestedNameSpecifier.h" |
40 | #include "clang/AST/ParentMapContext.h" |
41 | #include "clang/AST/RawCommentList.h" |
42 | #include "clang/AST/RecordLayout.h" |
43 | #include "clang/AST/Stmt.h" |
44 | #include "clang/AST/TemplateBase.h" |
45 | #include "clang/AST/TemplateName.h" |
46 | #include "clang/AST/Type.h" |
47 | #include "clang/AST/TypeLoc.h" |
48 | #include "clang/AST/UnresolvedSet.h" |
49 | #include "clang/AST/VTableBuilder.h" |
50 | #include "clang/Basic/AddressSpaces.h" |
51 | #include "clang/Basic/Builtins.h" |
52 | #include "clang/Basic/CommentOptions.h" |
53 | #include "clang/Basic/ExceptionSpecificationType.h" |
54 | #include "clang/Basic/IdentifierTable.h" |
55 | #include "clang/Basic/LLVM.h" |
56 | #include "clang/Basic/LangOptions.h" |
57 | #include "clang/Basic/Linkage.h" |
58 | #include "clang/Basic/Module.h" |
59 | #include "clang/Basic/NoSanitizeList.h" |
60 | #include "clang/Basic/ObjCRuntime.h" |
61 | #include "clang/Basic/ProfileList.h" |
62 | #include "clang/Basic/SourceLocation.h" |
63 | #include "clang/Basic/SourceManager.h" |
64 | #include "clang/Basic/Specifiers.h" |
65 | #include "clang/Basic/TargetCXXABI.h" |
66 | #include "clang/Basic/TargetInfo.h" |
67 | #include "clang/Basic/XRayLists.h" |
68 | #include "llvm/ADT/APFixedPoint.h" |
69 | #include "llvm/ADT/APInt.h" |
70 | #include "llvm/ADT/APSInt.h" |
71 | #include "llvm/ADT/ArrayRef.h" |
72 | #include "llvm/ADT/DenseMap.h" |
73 | #include "llvm/ADT/DenseSet.h" |
74 | #include "llvm/ADT/FoldingSet.h" |
75 | #include "llvm/ADT/PointerUnion.h" |
76 | #include "llvm/ADT/STLExtras.h" |
77 | #include "llvm/ADT/SmallPtrSet.h" |
78 | #include "llvm/ADT/SmallVector.h" |
79 | #include "llvm/ADT/StringExtras.h" |
80 | #include "llvm/ADT/StringRef.h" |
81 | #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" |
82 | #include "llvm/Support/Capacity.h" |
83 | #include "llvm/Support/Compiler.h" |
84 | #include "llvm/Support/ErrorHandling.h" |
85 | #include "llvm/Support/MD5.h" |
86 | #include "llvm/Support/MathExtras.h" |
87 | #include "llvm/Support/SipHash.h" |
88 | #include "llvm/Support/raw_ostream.h" |
89 | #include "llvm/TargetParser/AArch64TargetParser.h" |
90 | #include "llvm/TargetParser/Triple.h" |
91 | #include <algorithm> |
92 | #include <cassert> |
93 | #include <cstddef> |
94 | #include <cstdint> |
95 | #include <cstdlib> |
96 | #include <map> |
97 | #include <memory> |
98 | #include <optional> |
99 | #include <string> |
100 | #include <tuple> |
101 | #include <utility> |
102 | |
103 | using namespace clang; |
104 | |
105 | enum FloatingRank { |
106 | BFloat16Rank, |
107 | Float16Rank, |
108 | HalfRank, |
109 | FloatRank, |
110 | DoubleRank, |
111 | LongDoubleRank, |
112 | Float128Rank, |
113 | Ibm128Rank |
114 | }; |
115 | |
116 | template <> struct llvm::DenseMapInfo<llvm::FoldingSetNodeID> { |
117 | static FoldingSetNodeID getEmptyKey() { return FoldingSetNodeID{}; } |
118 | |
119 | static FoldingSetNodeID getTombstoneKey() { |
120 | FoldingSetNodeID id; |
121 | for (size_t i = 0; i < sizeof(id) / sizeof(unsigned); ++i) { |
122 | id.AddInteger(I: std::numeric_limits<unsigned>::max()); |
123 | } |
124 | return id; |
125 | } |
126 | |
127 | static unsigned getHashValue(const FoldingSetNodeID &Val) { |
128 | return Val.ComputeHash(); |
129 | } |
130 | |
131 | static bool isEqual(const FoldingSetNodeID &LHS, |
132 | const FoldingSetNodeID &RHS) { |
133 | return LHS == RHS; |
134 | } |
135 | }; |
136 | |
137 | /// \returns The locations that are relevant when searching for Doc comments |
138 | /// related to \p D. |
139 | static SmallVector<SourceLocation, 2> |
140 | getDeclLocsForCommentSearch(const Decl *D, SourceManager &SourceMgr) { |
141 | assert(D); |
142 | |
143 | // User can not attach documentation to implicit declarations. |
144 | if (D->isImplicit()) |
145 | return {}; |
146 | |
147 | // User can not attach documentation to implicit instantiations. |
148 | if (const auto *FD = dyn_cast<FunctionDecl>(Val: D)) { |
149 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
150 | return {}; |
151 | } |
152 | |
153 | if (const auto *VD = dyn_cast<VarDecl>(Val: D)) { |
154 | if (VD->isStaticDataMember() && |
155 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
156 | return {}; |
157 | } |
158 | |
159 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(Val: D)) { |
160 | if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
161 | return {}; |
162 | } |
163 | |
164 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) { |
165 | TemplateSpecializationKind TSK = CTSD->getSpecializationKind(); |
166 | if (TSK == TSK_ImplicitInstantiation || |
167 | TSK == TSK_Undeclared) |
168 | return {}; |
169 | } |
170 | |
171 | if (const auto *ED = dyn_cast<EnumDecl>(Val: D)) { |
172 | if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
173 | return {}; |
174 | } |
175 | if (const auto *TD = dyn_cast<TagDecl>(Val: D)) { |
176 | // When tag declaration (but not definition!) is part of the |
177 | // decl-specifier-seq of some other declaration, it doesn't get comment |
178 | if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition()) |
179 | return {}; |
180 | } |
181 | // TODO: handle comments for function parameters properly. |
182 | if (isa<ParmVarDecl>(Val: D)) |
183 | return {}; |
184 | |
185 | // TODO: we could look up template parameter documentation in the template |
186 | // documentation. |
187 | if (isa<TemplateTypeParmDecl>(Val: D) || |
188 | isa<NonTypeTemplateParmDecl>(Val: D) || |
189 | isa<TemplateTemplateParmDecl>(Val: D)) |
190 | return {}; |
191 | |
192 | SmallVector<SourceLocation, 2> Locations; |
193 | // Find declaration location. |
194 | // For Objective-C declarations we generally don't expect to have multiple |
195 | // declarators, thus use declaration starting location as the "declaration |
196 | // location". |
197 | // For all other declarations multiple declarators are used quite frequently, |
198 | // so we use the location of the identifier as the "declaration location". |
199 | SourceLocation BaseLocation; |
200 | if (isa<ObjCMethodDecl>(Val: D) || isa<ObjCContainerDecl>(Val: D) || |
201 | isa<ObjCPropertyDecl>(Val: D) || isa<RedeclarableTemplateDecl>(Val: D) || |
202 | isa<ClassTemplateSpecializationDecl>(Val: D) || |
203 | // Allow association with Y across {} in `typedef struct X {} Y`. |
204 | isa<TypedefDecl>(Val: D)) |
205 | BaseLocation = D->getBeginLoc(); |
206 | else |
207 | BaseLocation = D->getLocation(); |
208 | |
209 | if (!D->getLocation().isMacroID()) { |
210 | Locations.emplace_back(Args&: BaseLocation); |
211 | } else { |
212 | const auto *DeclCtx = D->getDeclContext(); |
213 | |
214 | // When encountering definitions generated from a macro (that are not |
215 | // contained by another declaration in the macro) we need to try and find |
216 | // the comment at the location of the expansion but if there is no comment |
217 | // there we should retry to see if there is a comment inside the macro as |
218 | // well. To this end we return first BaseLocation to first look at the |
219 | // expansion site, the second value is the spelling location of the |
220 | // beginning of the declaration defined inside the macro. |
221 | if (!(DeclCtx && |
222 | Decl::castFromDeclContext(DeclCtx)->getLocation().isMacroID())) { |
223 | Locations.emplace_back(Args: SourceMgr.getExpansionLoc(Loc: BaseLocation)); |
224 | } |
225 | |
226 | // We use Decl::getBeginLoc() and not just BaseLocation here to ensure that |
227 | // we don't refer to the macro argument location at the expansion site (this |
228 | // can happen if the name's spelling is provided via macro argument), and |
229 | // always to the declaration itself. |
230 | Locations.emplace_back(Args: SourceMgr.getSpellingLoc(Loc: D->getBeginLoc())); |
231 | } |
232 | |
233 | return Locations; |
234 | } |
235 | |
236 | RawComment *ASTContext::getRawCommentForDeclNoCacheImpl( |
237 | const Decl *D, const SourceLocation RepresentativeLocForDecl, |
238 | const std::map<unsigned, RawComment *> &CommentsInTheFile) const { |
239 | // If the declaration doesn't map directly to a location in a file, we |
240 | // can't find the comment. |
241 | if (RepresentativeLocForDecl.isInvalid() || |
242 | !RepresentativeLocForDecl.isFileID()) |
243 | return nullptr; |
244 | |
245 | // If there are no comments anywhere, we won't find anything. |
246 | if (CommentsInTheFile.empty()) |
247 | return nullptr; |
248 | |
249 | // Decompose the location for the declaration and find the beginning of the |
250 | // file buffer. |
251 | const std::pair<FileID, unsigned> DeclLocDecomp = |
252 | SourceMgr.getDecomposedLoc(Loc: RepresentativeLocForDecl); |
253 | |
254 | // Slow path. |
255 | auto OffsetCommentBehindDecl = |
256 | CommentsInTheFile.lower_bound(x: DeclLocDecomp.second); |
257 | |
258 | // First check whether we have a trailing comment. |
259 | if (OffsetCommentBehindDecl != CommentsInTheFile.end()) { |
260 | RawComment *CommentBehindDecl = OffsetCommentBehindDecl->second; |
261 | if ((CommentBehindDecl->isDocumentation() || |
262 | LangOpts.CommentOpts.ParseAllComments) && |
263 | CommentBehindDecl->isTrailingComment() && |
264 | (isa<FieldDecl>(Val: D) || isa<EnumConstantDecl>(Val: D) || isa<VarDecl>(Val: D) || |
265 | isa<ObjCMethodDecl>(Val: D) || isa<ObjCPropertyDecl>(Val: D))) { |
266 | |
267 | // Check that Doxygen trailing comment comes after the declaration, starts |
268 | // on the same line and in the same file as the declaration. |
269 | if (SourceMgr.getLineNumber(FID: DeclLocDecomp.first, FilePos: DeclLocDecomp.second) == |
270 | Comments.getCommentBeginLine(C: CommentBehindDecl, File: DeclLocDecomp.first, |
271 | Offset: OffsetCommentBehindDecl->first)) { |
272 | return CommentBehindDecl; |
273 | } |
274 | } |
275 | } |
276 | |
277 | // The comment just after the declaration was not a trailing comment. |
278 | // Let's look at the previous comment. |
279 | if (OffsetCommentBehindDecl == CommentsInTheFile.begin()) |
280 | return nullptr; |
281 | |
282 | auto OffsetCommentBeforeDecl = --OffsetCommentBehindDecl; |
283 | RawComment *CommentBeforeDecl = OffsetCommentBeforeDecl->second; |
284 | |
285 | // Check that we actually have a non-member Doxygen comment. |
286 | if (!(CommentBeforeDecl->isDocumentation() || |
287 | LangOpts.CommentOpts.ParseAllComments) || |
288 | CommentBeforeDecl->isTrailingComment()) |
289 | return nullptr; |
290 | |
291 | // Decompose the end of the comment. |
292 | const unsigned CommentEndOffset = |
293 | Comments.getCommentEndOffset(C: CommentBeforeDecl); |
294 | |
295 | // Get the corresponding buffer. |
296 | bool Invalid = false; |
297 | const char *Buffer = SourceMgr.getBufferData(FID: DeclLocDecomp.first, |
298 | Invalid: &Invalid).data(); |
299 | if (Invalid) |
300 | return nullptr; |
301 | |
302 | // Extract text between the comment and declaration. |
303 | StringRef Text(Buffer + CommentEndOffset, |
304 | DeclLocDecomp.second - CommentEndOffset); |
305 | |
306 | // There should be no other declarations or preprocessor directives between |
307 | // comment and declaration. |
308 | if (Text.find_last_of(Chars: ";{}#@") != StringRef::npos) |
309 | return nullptr; |
310 | |
311 | return CommentBeforeDecl; |
312 | } |
313 | |
314 | RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { |
315 | const auto DeclLocs = getDeclLocsForCommentSearch(D, SourceMgr); |
316 | |
317 | for (const auto DeclLoc : DeclLocs) { |
318 | // If the declaration doesn't map directly to a location in a file, we |
319 | // can't find the comment. |
320 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
321 | continue; |
322 | |
323 | if (ExternalSource && !CommentsLoaded) { |
324 | ExternalSource->ReadComments(); |
325 | CommentsLoaded = true; |
326 | } |
327 | |
328 | if (Comments.empty()) |
329 | continue; |
330 | |
331 | const FileID File = SourceMgr.getDecomposedLoc(Loc: DeclLoc).first; |
332 | if (!File.isValid()) |
333 | continue; |
334 | |
335 | const auto CommentsInThisFile = Comments.getCommentsInFile(File); |
336 | if (!CommentsInThisFile || CommentsInThisFile->empty()) |
337 | continue; |
338 | |
339 | if (RawComment *Comment = |
340 | getRawCommentForDeclNoCacheImpl(D, DeclLoc, *CommentsInThisFile)) |
341 | return Comment; |
342 | } |
343 | |
344 | return nullptr; |
345 | } |
346 | |
347 | void ASTContext::addComment(const RawComment &RC) { |
348 | assert(LangOpts.RetainCommentsFromSystemHeaders || |
349 | !SourceMgr.isInSystemHeader(RC.getSourceRange().getBegin())); |
350 | Comments.addComment(RC, CommentOpts: LangOpts.CommentOpts, Allocator&: BumpAlloc); |
351 | } |
352 | |
353 | /// If we have a 'templated' declaration for a template, adjust 'D' to |
354 | /// refer to the actual template. |
355 | /// If we have an implicit instantiation, adjust 'D' to refer to template. |
356 | static const Decl &adjustDeclToTemplate(const Decl &D) { |
357 | if (const auto *FD = dyn_cast<FunctionDecl>(Val: &D)) { |
358 | // Is this function declaration part of a function template? |
359 | if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
360 | return *FTD; |
361 | |
362 | // Nothing to do if function is not an implicit instantiation. |
363 | if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
364 | return D; |
365 | |
366 | // Function is an implicit instantiation of a function template? |
367 | if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) |
368 | return *FTD; |
369 | |
370 | // Function is instantiated from a member definition of a class template? |
371 | if (const FunctionDecl *MemberDecl = |
372 | FD->getInstantiatedFromMemberFunction()) |
373 | return *MemberDecl; |
374 | |
375 | return D; |
376 | } |
377 | if (const auto *VD = dyn_cast<VarDecl>(Val: &D)) { |
378 | // Static data member is instantiated from a member definition of a class |
379 | // template? |
380 | if (VD->isStaticDataMember()) |
381 | if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember()) |
382 | return *MemberDecl; |
383 | |
384 | return D; |
385 | } |
386 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(Val: &D)) { |
387 | // Is this class declaration part of a class template? |
388 | if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate()) |
389 | return *CTD; |
390 | |
391 | // Class is an implicit instantiation of a class template or partial |
392 | // specialization? |
393 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: CRD)) { |
394 | if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation) |
395 | return D; |
396 | llvm::PointerUnion<ClassTemplateDecl *, |
397 | ClassTemplatePartialSpecializationDecl *> |
398 | PU = CTSD->getSpecializedTemplateOrPartial(); |
399 | return isa<ClassTemplateDecl *>(PU) |
400 | ? *static_cast<const Decl *>(cast<ClassTemplateDecl *>(PU)) |
401 | : *static_cast<const Decl *>( |
402 | cast<ClassTemplatePartialSpecializationDecl *>(PU)); |
403 | } |
404 | |
405 | // Class is instantiated from a member definition of a class template? |
406 | if (const MemberSpecializationInfo *Info = |
407 | CRD->getMemberSpecializationInfo()) |
408 | return *Info->getInstantiatedFrom(); |
409 | |
410 | return D; |
411 | } |
412 | if (const auto *ED = dyn_cast<EnumDecl>(Val: &D)) { |
413 | // Enum is instantiated from a member definition of a class template? |
414 | if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum()) |
415 | return *MemberDecl; |
416 | |
417 | return D; |
418 | } |
419 | // FIXME: Adjust alias templates? |
420 | return D; |
421 | } |
422 | |
423 | const RawComment *ASTContext::getRawCommentForAnyRedecl( |
424 | const Decl *D, |
425 | const Decl **OriginalDecl) const { |
426 | if (!D) { |
427 | if (OriginalDecl) |
428 | OriginalDecl = nullptr; |
429 | return nullptr; |
430 | } |
431 | |
432 | D = &adjustDeclToTemplate(D: *D); |
433 | |
434 | // Any comment directly attached to D? |
435 | { |
436 | auto DeclComment = DeclRawComments.find(D); |
437 | if (DeclComment != DeclRawComments.end()) { |
438 | if (OriginalDecl) |
439 | *OriginalDecl = D; |
440 | return DeclComment->second; |
441 | } |
442 | } |
443 | |
444 | // Any comment attached to any redeclaration of D? |
445 | const Decl *CanonicalD = D->getCanonicalDecl(); |
446 | if (!CanonicalD) |
447 | return nullptr; |
448 | |
449 | { |
450 | auto RedeclComment = RedeclChainComments.find(CanonicalD); |
451 | if (RedeclComment != RedeclChainComments.end()) { |
452 | if (OriginalDecl) |
453 | *OriginalDecl = RedeclComment->second; |
454 | auto CommentAtRedecl = DeclRawComments.find(RedeclComment->second); |
455 | assert(CommentAtRedecl != DeclRawComments.end() && |
456 | "This decl is supposed to have comment attached."); |
457 | return CommentAtRedecl->second; |
458 | } |
459 | } |
460 | |
461 | // Any redeclarations of D that we haven't checked for comments yet? |
462 | const Decl *LastCheckedRedecl = [&]() { |
463 | const Decl *LastChecked = CommentlessRedeclChains.lookup(CanonicalD); |
464 | bool CanUseCommentlessCache = false; |
465 | if (LastChecked) { |
466 | for (auto *Redecl : CanonicalD->redecls()) { |
467 | if (Redecl == D) { |
468 | CanUseCommentlessCache = true; |
469 | break; |
470 | } |
471 | if (Redecl == LastChecked) |
472 | break; |
473 | } |
474 | } |
475 | // FIXME: This could be improved so that even if CanUseCommentlessCache |
476 | // is false, once we've traversed past CanonicalD we still skip ahead |
477 | // LastChecked. |
478 | return CanUseCommentlessCache ? LastChecked : nullptr; |
479 | }(); |
480 | |
481 | for (const Decl *Redecl : D->redecls()) { |
482 | assert(Redecl); |
483 | // Skip all redeclarations that have been checked previously. |
484 | if (LastCheckedRedecl) { |
485 | if (LastCheckedRedecl == Redecl) { |
486 | LastCheckedRedecl = nullptr; |
487 | } |
488 | continue; |
489 | } |
490 | const RawComment *RedeclComment = getRawCommentForDeclNoCache(D: Redecl); |
491 | if (RedeclComment) { |
492 | cacheRawCommentForDecl(OriginalD: *Redecl, Comment: *RedeclComment); |
493 | if (OriginalDecl) |
494 | *OriginalDecl = Redecl; |
495 | return RedeclComment; |
496 | } |
497 | CommentlessRedeclChains[CanonicalD] = Redecl; |
498 | } |
499 | |
500 | if (OriginalDecl) |
501 | *OriginalDecl = nullptr; |
502 | return nullptr; |
503 | } |
504 | |
505 | void ASTContext::cacheRawCommentForDecl(const Decl &OriginalD, |
506 | const RawComment &Comment) const { |
507 | assert(Comment.isDocumentation() || LangOpts.CommentOpts.ParseAllComments); |
508 | DeclRawComments.try_emplace(&OriginalD, &Comment); |
509 | const Decl *const CanonicalDecl = OriginalD.getCanonicalDecl(); |
510 | RedeclChainComments.try_emplace(CanonicalDecl, &OriginalD); |
511 | CommentlessRedeclChains.erase(CanonicalDecl); |
512 | } |
513 | |
514 | static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod, |
515 | SmallVectorImpl<const NamedDecl *> &Redeclared) { |
516 | const DeclContext *DC = ObjCMethod->getDeclContext(); |
517 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(DC)) { |
518 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); |
519 | if (!ID) |
520 | return; |
521 | // Add redeclared method here. |
522 | for (const auto *Ext : ID->known_extensions()) { |
523 | if (ObjCMethodDecl *RedeclaredMethod = |
524 | Ext->getMethod(ObjCMethod->getSelector(), |
525 | ObjCMethod->isInstanceMethod())) |
526 | Redeclared.push_back(RedeclaredMethod); |
527 | } |
528 | } |
529 | } |
530 | |
531 | void ASTContext::attachCommentsToJustParsedDecls(ArrayRef<Decl *> Decls, |
532 | const Preprocessor *PP) { |
533 | if (Comments.empty() || Decls.empty()) |
534 | return; |
535 | |
536 | FileID File; |
537 | for (const Decl *D : Decls) { |
538 | if (D->isInvalidDecl()) |
539 | continue; |
540 | |
541 | D = &adjustDeclToTemplate(D: *D); |
542 | SourceLocation Loc = D->getLocation(); |
543 | if (Loc.isValid()) { |
544 | // See if there are any new comments that are not attached to a decl. |
545 | // The location doesn't have to be precise - we care only about the file. |
546 | File = SourceMgr.getDecomposedLoc(Loc).first; |
547 | break; |
548 | } |
549 | } |
550 | |
551 | if (File.isInvalid()) |
552 | return; |
553 | |
554 | auto CommentsInThisFile = Comments.getCommentsInFile(File); |
555 | if (!CommentsInThisFile || CommentsInThisFile->empty() || |
556 | CommentsInThisFile->rbegin()->second->isAttached()) |
557 | return; |
558 | |
559 | // There is at least one comment not attached to a decl. |
560 | // Maybe it should be attached to one of Decls? |
561 | // |
562 | // Note that this way we pick up not only comments that precede the |
563 | // declaration, but also comments that *follow* the declaration -- thanks to |
564 | // the lookahead in the lexer: we've consumed the semicolon and looked |
565 | // ahead through comments. |
566 | for (const Decl *D : Decls) { |
567 | assert(D); |
568 | if (D->isInvalidDecl()) |
569 | continue; |
570 | |
571 | D = &adjustDeclToTemplate(D: *D); |
572 | |
573 | if (DeclRawComments.count(D) > 0) |
574 | continue; |
575 | |
576 | const auto DeclLocs = getDeclLocsForCommentSearch(D, SourceMgr); |
577 | |
578 | for (const auto DeclLoc : DeclLocs) { |
579 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
580 | continue; |
581 | |
582 | if (RawComment *const DocComment = getRawCommentForDeclNoCacheImpl( |
583 | D, DeclLoc, *CommentsInThisFile)) { |
584 | cacheRawCommentForDecl(OriginalD: *D, Comment: *DocComment); |
585 | comments::FullComment *FC = DocComment->parse(Context: *this, PP, D); |
586 | ParsedComments[D->getCanonicalDecl()] = FC; |
587 | break; |
588 | } |
589 | } |
590 | } |
591 | } |
592 | |
593 | comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC, |
594 | const Decl *D) const { |
595 | auto *ThisDeclInfo = new (*this) comments::DeclInfo; |
596 | ThisDeclInfo->CommentDecl = D; |
597 | ThisDeclInfo->IsFilled = false; |
598 | ThisDeclInfo->fill(); |
599 | ThisDeclInfo->CommentDecl = FC->getDecl(); |
600 | if (!ThisDeclInfo->TemplateParameters) |
601 | ThisDeclInfo->TemplateParameters = FC->getDeclInfo()->TemplateParameters; |
602 | comments::FullComment *CFC = |
603 | new (*this) comments::FullComment(FC->getBlocks(), |
604 | ThisDeclInfo); |
605 | return CFC; |
606 | } |
607 | |
608 | comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const { |
609 | const RawComment *RC = getRawCommentForDeclNoCache(D); |
610 | return RC ? RC->parse(Context: *this, PP: nullptr, D) : nullptr; |
611 | } |
612 | |
613 | comments::FullComment *ASTContext::getCommentForDecl( |
614 | const Decl *D, |
615 | const Preprocessor *PP) const { |
616 | if (!D || D->isInvalidDecl()) |
617 | return nullptr; |
618 | D = &adjustDeclToTemplate(D: *D); |
619 | |
620 | const Decl *Canonical = D->getCanonicalDecl(); |
621 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = |
622 | ParsedComments.find(Canonical); |
623 | |
624 | if (Pos != ParsedComments.end()) { |
625 | if (Canonical != D) { |
626 | comments::FullComment *FC = Pos->second; |
627 | comments::FullComment *CFC = cloneFullComment(FC, D); |
628 | return CFC; |
629 | } |
630 | return Pos->second; |
631 | } |
632 | |
633 | const Decl *OriginalDecl = nullptr; |
634 | |
635 | const RawComment *RC = getRawCommentForAnyRedecl(D, OriginalDecl: &OriginalDecl); |
636 | if (!RC) { |
637 | if (isa<ObjCMethodDecl>(Val: D) || isa<FunctionDecl>(Val: D)) { |
638 | SmallVector<const NamedDecl*, 8> Overridden; |
639 | const auto *OMD = dyn_cast<ObjCMethodDecl>(Val: D); |
640 | if (OMD && OMD->isPropertyAccessor()) |
641 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
642 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) |
643 | return cloneFullComment(FC, D); |
644 | if (OMD) |
645 | addRedeclaredMethods(ObjCMethod: OMD, Redeclared&: Overridden); |
646 | getOverriddenMethods(Method: dyn_cast<NamedDecl>(Val: D), Overridden); |
647 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) |
648 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) |
649 | return cloneFullComment(FC, D); |
650 | } |
651 | else if (const auto *TD = dyn_cast<TypedefNameDecl>(Val: D)) { |
652 | // Attach any tag type's documentation to its typedef if latter |
653 | // does not have one of its own. |
654 | QualType QT = TD->getUnderlyingType(); |
655 | if (const auto *TT = QT->getAs<TagType>()) |
656 | if (const Decl *TD = TT->getDecl()) |
657 | if (comments::FullComment *FC = getCommentForDecl(D: TD, PP)) |
658 | return cloneFullComment(FC, D); |
659 | } |
660 | else if (const auto *IC = dyn_cast<ObjCInterfaceDecl>(Val: D)) { |
661 | while (IC->getSuperClass()) { |
662 | IC = IC->getSuperClass(); |
663 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
664 | return cloneFullComment(FC, D); |
665 | } |
666 | } |
667 | else if (const auto *CD = dyn_cast<ObjCCategoryDecl>(Val: D)) { |
668 | if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) |
669 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
670 | return cloneFullComment(FC, D); |
671 | } |
672 | else if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: D)) { |
673 | if (!(RD = RD->getDefinition())) |
674 | return nullptr; |
675 | // Check non-virtual bases. |
676 | for (const auto &I : RD->bases()) { |
677 | if (I.isVirtual() || (I.getAccessSpecifier() != AS_public)) |
678 | continue; |
679 | QualType Ty = I.getType(); |
680 | if (Ty.isNull()) |
681 | continue; |
682 | if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) { |
683 | if (!(NonVirtualBase= NonVirtualBase->getDefinition())) |
684 | continue; |
685 | |
686 | if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) |
687 | return cloneFullComment(FC, D); |
688 | } |
689 | } |
690 | // Check virtual bases. |
691 | for (const auto &I : RD->vbases()) { |
692 | if (I.getAccessSpecifier() != AS_public) |
693 | continue; |
694 | QualType Ty = I.getType(); |
695 | if (Ty.isNull()) |
696 | continue; |
697 | if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { |
698 | if (!(VirtualBase= VirtualBase->getDefinition())) |
699 | continue; |
700 | if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) |
701 | return cloneFullComment(FC, D); |
702 | } |
703 | } |
704 | } |
705 | return nullptr; |
706 | } |
707 | |
708 | // If the RawComment was attached to other redeclaration of this Decl, we |
709 | // should parse the comment in context of that other Decl. This is important |
710 | // because comments can contain references to parameter names which can be |
711 | // different across redeclarations. |
712 | if (D != OriginalDecl && OriginalDecl) |
713 | return getCommentForDecl(D: OriginalDecl, PP); |
714 | |
715 | comments::FullComment *FC = RC->parse(Context: *this, PP, D); |
716 | ParsedComments[Canonical] = FC; |
717 | return FC; |
718 | } |
719 | |
720 | void |
721 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
722 | const ASTContext &C, |
723 | TemplateTemplateParmDecl *Parm) { |
724 | ID.AddInteger(Parm->getDepth()); |
725 | ID.AddInteger(Parm->getPosition()); |
726 | ID.AddBoolean(B: Parm->isParameterPack()); |
727 | |
728 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
729 | ID.AddInteger(I: Params->size()); |
730 | for (TemplateParameterList::const_iterator P = Params->begin(), |
731 | PEnd = Params->end(); |
732 | P != PEnd; ++P) { |
733 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
734 | ID.AddInteger(I: 0); |
735 | ID.AddBoolean(B: TTP->isParameterPack()); |
736 | ID.AddInteger( |
737 | TTP->getNumExpansionParameters().toInternalRepresentation()); |
738 | continue; |
739 | } |
740 | |
741 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
742 | ID.AddInteger(I: 1); |
743 | ID.AddBoolean(B: NTTP->isParameterPack()); |
744 | ID.AddPointer(Ptr: C.getUnconstrainedType(T: C.getCanonicalType(NTTP->getType())) |
745 | .getAsOpaquePtr()); |
746 | if (NTTP->isExpandedParameterPack()) { |
747 | ID.AddBoolean(B: true); |
748 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
749 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
750 | QualType T = NTTP->getExpansionType(I); |
751 | ID.AddPointer(Ptr: T.getCanonicalType().getAsOpaquePtr()); |
752 | } |
753 | } else |
754 | ID.AddBoolean(B: false); |
755 | continue; |
756 | } |
757 | |
758 | auto *TTP = cast<TemplateTemplateParmDecl>(Val: *P); |
759 | ID.AddInteger(I: 2); |
760 | Profile(ID, C, TTP); |
761 | } |
762 | } |
763 | |
764 | TemplateTemplateParmDecl * |
765 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
766 | TemplateTemplateParmDecl *TTP) const { |
767 | // Check if we already have a canonical template template parameter. |
768 | llvm::FoldingSetNodeID ID; |
769 | CanonicalTemplateTemplateParm::Profile(ID, C: *this, Parm: TTP); |
770 | void *InsertPos = nullptr; |
771 | CanonicalTemplateTemplateParm *Canonical |
772 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
773 | if (Canonical) |
774 | return Canonical->getParam(); |
775 | |
776 | // Build a canonical template parameter list. |
777 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
778 | SmallVector<NamedDecl *, 4> CanonParams; |
779 | CanonParams.reserve(N: Params->size()); |
780 | for (TemplateParameterList::const_iterator P = Params->begin(), |
781 | PEnd = Params->end(); |
782 | P != PEnd; ++P) { |
783 | // Note that, per C++20 [temp.over.link]/6, when determining whether |
784 | // template-parameters are equivalent, constraints are ignored. |
785 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
786 | TemplateTypeParmDecl *NewTTP = TemplateTypeParmDecl::Create( |
787 | C: *this, DC: getTranslationUnitDecl(), KeyLoc: SourceLocation(), NameLoc: SourceLocation(), |
788 | D: TTP->getDepth(), P: TTP->getIndex(), Id: nullptr, Typename: false, |
789 | ParameterPack: TTP->isParameterPack(), /*HasTypeConstraint=*/false, |
790 | NumExpanded: TTP->getNumExpansionParameters()); |
791 | CanonParams.push_back(NewTTP); |
792 | } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
793 | QualType T = getUnconstrainedType(T: getCanonicalType(NTTP->getType())); |
794 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
795 | NonTypeTemplateParmDecl *Param; |
796 | if (NTTP->isExpandedParameterPack()) { |
797 | SmallVector<QualType, 2> ExpandedTypes; |
798 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
799 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
800 | ExpandedTypes.push_back(Elt: getCanonicalType(NTTP->getExpansionType(I))); |
801 | ExpandedTInfos.push_back( |
802 | Elt: getTrivialTypeSourceInfo(T: ExpandedTypes.back())); |
803 | } |
804 | |
805 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
806 | SourceLocation(), |
807 | SourceLocation(), |
808 | NTTP->getDepth(), |
809 | NTTP->getPosition(), nullptr, |
810 | T, |
811 | TInfo, |
812 | ExpandedTypes, |
813 | ExpandedTInfos); |
814 | } else { |
815 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
816 | SourceLocation(), |
817 | SourceLocation(), |
818 | NTTP->getDepth(), |
819 | NTTP->getPosition(), nullptr, |
820 | T, |
821 | NTTP->isParameterPack(), |
822 | TInfo); |
823 | } |
824 | CanonParams.push_back(Param); |
825 | } else |
826 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
827 | TTP: cast<TemplateTemplateParmDecl>(Val: *P))); |
828 | } |
829 | |
830 | TemplateTemplateParmDecl *CanonTTP = TemplateTemplateParmDecl::Create( |
831 | *this, getTranslationUnitDecl(), SourceLocation(), TTP->getDepth(), |
832 | TTP->getPosition(), TTP->isParameterPack(), nullptr, /*Typename=*/false, |
833 | TemplateParameterList::Create(C: *this, TemplateLoc: SourceLocation(), LAngleLoc: SourceLocation(), |
834 | Params: CanonParams, RAngleLoc: SourceLocation(), |
835 | /*RequiresClause=*/nullptr)); |
836 | |
837 | // Get the new insert position for the node we care about. |
838 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
839 | assert(!Canonical && "Shouldn't be in the map!"); |
840 | (void)Canonical; |
841 | |
842 | // Create the canonical template template parameter entry. |
843 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
844 | CanonTemplateTemplateParms.InsertNode(N: Canonical, InsertPos); |
845 | return CanonTTP; |
846 | } |
847 | |
848 | TemplateTemplateParmDecl * |
849 | ASTContext::findCanonicalTemplateTemplateParmDeclInternal( |
850 | TemplateTemplateParmDecl *TTP) const { |
851 | llvm::FoldingSetNodeID ID; |
852 | CanonicalTemplateTemplateParm::Profile(ID, C: *this, Parm: TTP); |
853 | void *InsertPos = nullptr; |
854 | CanonicalTemplateTemplateParm *Canonical = |
855 | CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
856 | return Canonical ? Canonical->getParam() : nullptr; |
857 | } |
858 | |
859 | TemplateTemplateParmDecl * |
860 | ASTContext::insertCanonicalTemplateTemplateParmDeclInternal( |
861 | TemplateTemplateParmDecl *CanonTTP) const { |
862 | llvm::FoldingSetNodeID ID; |
863 | CanonicalTemplateTemplateParm::Profile(ID, C: *this, Parm: CanonTTP); |
864 | void *InsertPos = nullptr; |
865 | if (auto *Existing = |
866 | CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos)) |
867 | return Existing->getParam(); |
868 | CanonTemplateTemplateParms.InsertNode( |
869 | N: new (*this) CanonicalTemplateTemplateParm(CanonTTP), InsertPos); |
870 | return CanonTTP; |
871 | } |
872 | |
873 | /// Check if a type can have its sanitizer instrumentation elided based on its |
874 | /// presence within an ignorelist. |
875 | bool ASTContext::isTypeIgnoredBySanitizer(const SanitizerMask &Mask, |
876 | const QualType &Ty) const { |
877 | std::string TyName = Ty.getUnqualifiedType().getAsString(Policy: getPrintingPolicy()); |
878 | return NoSanitizeL->containsType(Mask, TyName); |
879 | } |
880 | |
881 | TargetCXXABI::Kind ASTContext::getCXXABIKind() const { |
882 | auto Kind = getTargetInfo().getCXXABI().getKind(); |
883 | return getLangOpts().CXXABI.value_or(u&: Kind); |
884 | } |
885 | |
886 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
887 | if (!LangOpts.CPlusPlus) return nullptr; |
888 | |
889 | switch (getCXXABIKind()) { |
890 | case TargetCXXABI::AppleARM64: |
891 | case TargetCXXABI::Fuchsia: |
892 | case TargetCXXABI::GenericARM: // Same as Itanium at this level |
893 | case TargetCXXABI::iOS: |
894 | case TargetCXXABI::WatchOS: |
895 | case TargetCXXABI::GenericAArch64: |
896 | case TargetCXXABI::GenericMIPS: |
897 | case TargetCXXABI::GenericItanium: |
898 | case TargetCXXABI::WebAssembly: |
899 | case TargetCXXABI::XL: |
900 | return CreateItaniumCXXABI(Ctx&: *this); |
901 | case TargetCXXABI::Microsoft: |
902 | return CreateMicrosoftCXXABI(Ctx&: *this); |
903 | } |
904 | llvm_unreachable("Invalid CXXABI type!"); |
905 | } |
906 | |
907 | interp::Context &ASTContext::getInterpContext() { |
908 | if (!InterpContext) { |
909 | InterpContext.reset(new interp::Context(*this)); |
910 | } |
911 | return *InterpContext; |
912 | } |
913 | |
914 | ParentMapContext &ASTContext::getParentMapContext() { |
915 | if (!ParentMapCtx) |
916 | ParentMapCtx.reset(new ParentMapContext(*this)); |
917 | return *ParentMapCtx; |
918 | } |
919 | |
920 | static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI, |
921 | const LangOptions &LangOpts) { |
922 | switch (LangOpts.getAddressSpaceMapMangling()) { |
923 | case LangOptions::ASMM_Target: |
924 | return TI.useAddressSpaceMapMangling(); |
925 | case LangOptions::ASMM_On: |
926 | return true; |
927 | case LangOptions::ASMM_Off: |
928 | return false; |
929 | } |
930 | llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything."); |
931 | } |
932 | |
933 | ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM, |
934 | IdentifierTable &idents, SelectorTable &sels, |
935 | Builtin::Context &builtins, TranslationUnitKind TUKind) |
936 | : ConstantArrayTypes(this_(), ConstantArrayTypesLog2InitSize), |
937 | DependentSizedArrayTypes(this_()), DependentSizedExtVectorTypes(this_()), |
938 | DependentAddressSpaceTypes(this_()), DependentVectorTypes(this_()), |
939 | DependentSizedMatrixTypes(this_()), |
940 | FunctionProtoTypes(this_(), FunctionProtoTypesLog2InitSize), |
941 | DependentTypeOfExprTypes(this_()), DependentDecltypeTypes(this_()), |
942 | DependentPackIndexingTypes(this_()), TemplateSpecializationTypes(this_()), |
943 | DependentTemplateSpecializationTypes(this_()), |
944 | DependentBitIntTypes(this_()), SubstTemplateTemplateParmPacks(this_()), |
945 | DeducedTemplates(this_()), ArrayParameterTypes(this_()), |
946 | CanonTemplateTemplateParms(this_()), SourceMgr(SM), LangOpts(LOpts), |
947 | NoSanitizeL(new NoSanitizeList(LangOpts.NoSanitizeFiles, SM)), |
948 | XRayFilter(new XRayFunctionFilter(LangOpts.XRayAlwaysInstrumentFiles, |
949 | LangOpts.XRayNeverInstrumentFiles, |
950 | LangOpts.XRayAttrListFiles, SM)), |
951 | ProfList(new ProfileList(LangOpts.ProfileListFiles, SM)), |
952 | PrintingPolicy(LOpts), Idents(idents), Selectors(sels), |
953 | BuiltinInfo(builtins), TUKind(TUKind), DeclarationNames(*this), |
954 | Comments(SM), CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), |
955 | CompCategories(this_()), LastSDM(nullptr, 0) { |
956 | addTranslationUnitDecl(); |
957 | } |
958 | |
959 | void ASTContext::cleanup() { |
960 | // Release the DenseMaps associated with DeclContext objects. |
961 | // FIXME: Is this the ideal solution? |
962 | ReleaseDeclContextMaps(); |
963 | |
964 | // Call all of the deallocation functions on all of their targets. |
965 | for (auto &Pair : Deallocations) |
966 | (Pair.first)(Pair.second); |
967 | Deallocations.clear(); |
968 | |
969 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
970 | // because they can contain DenseMaps. |
971 | for (llvm::DenseMap<const ObjCInterfaceDecl *, |
972 | const ASTRecordLayout *>::iterator |
973 | I = ObjCLayouts.begin(), |
974 | E = ObjCLayouts.end(); |
975 | I != E;) |
976 | // Increment in loop to prevent using deallocated memory. |
977 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) |
978 | R->Destroy(Ctx&: *this); |
979 | ObjCLayouts.clear(); |
980 | |
981 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
982 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
983 | // Increment in loop to prevent using deallocated memory. |
984 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) |
985 | R->Destroy(Ctx&: *this); |
986 | } |
987 | ASTRecordLayouts.clear(); |
988 | |
989 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
990 | AEnd = DeclAttrs.end(); |
991 | A != AEnd; ++A) |
992 | A->second->~AttrVec(); |
993 | DeclAttrs.clear(); |
994 | |
995 | for (const auto &Value : ModuleInitializers) |
996 | Value.second->~PerModuleInitializers(); |
997 | ModuleInitializers.clear(); |
998 | } |
999 | |
1000 | ASTContext::~ASTContext() { cleanup(); } |
1001 | |
1002 | void ASTContext::setTraversalScope(const std::vector<Decl *> &TopLevelDecls) { |
1003 | TraversalScope = TopLevelDecls; |
1004 | getParentMapContext().clear(); |
1005 | } |
1006 | |
1007 | void ASTContext::AddDeallocation(void (*Callback)(void *), void *Data) const { |
1008 | Deallocations.push_back({Callback, Data}); |
1009 | } |
1010 | |
1011 | void |
1012 | ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) { |
1013 | ExternalSource = std::move(Source); |
1014 | } |
1015 | |
1016 | void ASTContext::PrintStats() const { |
1017 | llvm::errs() << "\n*** AST Context Stats:\n"; |
1018 | llvm::errs() << " "<< Types.size() << " types total.\n"; |
1019 | |
1020 | unsigned counts[] = { |
1021 | #define TYPE(Name, Parent) 0, |
1022 | #define ABSTRACT_TYPE(Name, Parent) |
1023 | #include "clang/AST/TypeNodes.inc" |
1024 | 0 // Extra |
1025 | }; |
1026 | |
1027 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
1028 | Type *T = Types[i]; |
1029 | counts[(unsigned)T->getTypeClass()]++; |
1030 | } |
1031 | |
1032 | unsigned Idx = 0; |
1033 | unsigned TotalBytes = 0; |
1034 | #define TYPE(Name, Parent) \ |
1035 | if (counts[Idx]) \ |
1036 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
1037 | << " types, " << sizeof(Name##Type) << " each " \ |
1038 | << "(" << counts[Idx] * sizeof(Name##Type) \ |
1039 | << " bytes)\n"; \ |
1040 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
1041 | ++Idx; |
1042 | #define ABSTRACT_TYPE(Name, Parent) |
1043 | #include "clang/AST/TypeNodes.inc" |
1044 | |
1045 | llvm::errs() << "Total bytes = "<< TotalBytes << "\n"; |
1046 | |
1047 | // Implicit special member functions. |
1048 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
1049 | << NumImplicitDefaultConstructors |
1050 | << " implicit default constructors created\n"; |
1051 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
1052 | << NumImplicitCopyConstructors |
1053 | << " implicit copy constructors created\n"; |
1054 | if (getLangOpts().CPlusPlus) |
1055 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
1056 | << NumImplicitMoveConstructors |
1057 | << " implicit move constructors created\n"; |
1058 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
1059 | << NumImplicitCopyAssignmentOperators |
1060 | << " implicit copy assignment operators created\n"; |
1061 | if (getLangOpts().CPlusPlus) |
1062 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
1063 | << NumImplicitMoveAssignmentOperators |
1064 | << " implicit move assignment operators created\n"; |
1065 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
1066 | << NumImplicitDestructors |
1067 | << " implicit destructors created\n"; |
1068 | |
1069 | if (ExternalSource) { |
1070 | llvm::errs() << "\n"; |
1071 | ExternalSource->PrintStats(); |
1072 | } |
1073 | |
1074 | BumpAlloc.PrintStats(); |
1075 | } |
1076 | |
1077 | void ASTContext::mergeDefinitionIntoModule(NamedDecl *ND, Module *M, |
1078 | bool NotifyListeners) { |
1079 | if (NotifyListeners) |
1080 | if (auto *Listener = getASTMutationListener(); |
1081 | Listener && !ND->isUnconditionallyVisible()) |
1082 | Listener->RedefinedHiddenDefinition(D: ND, M); |
1083 | |
1084 | MergedDefModules[cast<NamedDecl>(ND->getCanonicalDecl())].push_back(M); |
1085 | } |
1086 | |
1087 | void ASTContext::deduplicateMergedDefinitionsFor(NamedDecl *ND) { |
1088 | auto It = MergedDefModules.find(cast<NamedDecl>(ND->getCanonicalDecl())); |
1089 | if (It == MergedDefModules.end()) |
1090 | return; |
1091 | |
1092 | auto &Merged = It->second; |
1093 | llvm::DenseSet<Module*> Found; |
1094 | for (Module *&M : Merged) |
1095 | if (!Found.insert(M).second) |
1096 | M = nullptr; |
1097 | llvm::erase(Merged, nullptr); |
1098 | } |
1099 | |
1100 | ArrayRef<Module *> |
1101 | ASTContext::getModulesWithMergedDefinition(const NamedDecl *Def) { |
1102 | auto MergedIt = |
1103 | MergedDefModules.find(cast<NamedDecl>(Def->getCanonicalDecl())); |
1104 | if (MergedIt == MergedDefModules.end()) |
1105 | return {}; |
1106 | return MergedIt->second; |
1107 | } |
1108 | |
1109 | void ASTContext::PerModuleInitializers::resolve(ASTContext &Ctx) { |
1110 | if (LazyInitializers.empty()) |
1111 | return; |
1112 | |
1113 | auto *Source = Ctx.getExternalSource(); |
1114 | assert(Source && "lazy initializers but no external source"); |
1115 | |
1116 | auto LazyInits = std::move(LazyInitializers); |
1117 | LazyInitializers.clear(); |
1118 | |
1119 | for (auto ID : LazyInits) |
1120 | Initializers.push_back(Source->GetExternalDecl(ID)); |
1121 | |
1122 | assert(LazyInitializers.empty() && |
1123 | "GetExternalDecl for lazy module initializer added more inits"); |
1124 | } |
1125 | |
1126 | void ASTContext::addModuleInitializer(Module *M, Decl *D) { |
1127 | // One special case: if we add a module initializer that imports another |
1128 | // module, and that module's only initializer is an ImportDecl, simplify. |
1129 | if (const auto *ID = dyn_cast<ImportDecl>(Val: D)) { |
1130 | auto It = ModuleInitializers.find(ID->getImportedModule()); |
1131 | |
1132 | // Maybe the ImportDecl does nothing at all. (Common case.) |
1133 | if (It == ModuleInitializers.end()) |
1134 | return; |
1135 | |
1136 | // Maybe the ImportDecl only imports another ImportDecl. |
1137 | auto &Imported = *It->second; |
1138 | if (Imported.Initializers.size() + Imported.LazyInitializers.size() == 1) { |
1139 | Imported.resolve(*this); |
1140 | auto *OnlyDecl = Imported.Initializers.front(); |
1141 | if (isa<ImportDecl>(OnlyDecl)) |
1142 | D = OnlyDecl; |
1143 | } |
1144 | } |
1145 | |
1146 | auto *&Inits = ModuleInitializers[M]; |
1147 | if (!Inits) |
1148 | Inits = new (*this) PerModuleInitializers; |
1149 | Inits->Initializers.push_back(D); |
1150 | } |
1151 | |
1152 | void ASTContext::addLazyModuleInitializers(Module *M, |
1153 | ArrayRef<GlobalDeclID> IDs) { |
1154 | auto *&Inits = ModuleInitializers[M]; |
1155 | if (!Inits) |
1156 | Inits = new (*this) PerModuleInitializers; |
1157 | Inits->LazyInitializers.insert(Inits->LazyInitializers.end(), |
1158 | IDs.begin(), IDs.end()); |
1159 | } |
1160 | |
1161 | ArrayRef<Decl *> ASTContext::getModuleInitializers(Module *M) { |
1162 | auto It = ModuleInitializers.find(M); |
1163 | if (It == ModuleInitializers.end()) |
1164 | return {}; |
1165 | |
1166 | auto *Inits = It->second; |
1167 | Inits->resolve(*this); |
1168 | return Inits->Initializers; |
1169 | } |
1170 | |
1171 | void ASTContext::setCurrentNamedModule(Module *M) { |
1172 | assert(M->isNamedModule()); |
1173 | assert(!CurrentCXXNamedModule && |
1174 | "We should set named module for ASTContext for only once"); |
1175 | CurrentCXXNamedModule = M; |
1176 | } |
1177 | |
1178 | bool ASTContext::isInSameModule(const Module *M1, const Module *M2) { |
1179 | if (!M1 != !M2) |
1180 | return false; |
1181 | |
1182 | /// Get the representative module for M. The representative module is the |
1183 | /// first module unit for a specific primary module name. So that the module |
1184 | /// units have the same representative module belongs to the same module. |
1185 | /// |
1186 | /// The process is helpful to reduce the expensive string operations. |
1187 | auto GetRepresentativeModule = [this](const Module *M) { |
1188 | auto Iter = SameModuleLookupSet.find(M); |
1189 | if (Iter != SameModuleLookupSet.end()) |
1190 | return Iter->second; |
1191 | |
1192 | const Module *RepresentativeModule = |
1193 | PrimaryModuleNameMap.try_emplace(M->getPrimaryModuleInterfaceName(), M) |
1194 | .first->second; |
1195 | SameModuleLookupSet[M] = RepresentativeModule; |
1196 | return RepresentativeModule; |
1197 | }; |
1198 | |
1199 | assert(M1 && "Shouldn't call `isInSameModule` if both M1 and M2 are none."); |
1200 | return GetRepresentativeModule(M1) == GetRepresentativeModule(M2); |
1201 | } |
1202 | |
1203 | ExternCContextDecl *ASTContext::getExternCContextDecl() const { |
1204 | if (!ExternCContext) |
1205 | ExternCContext = ExternCContextDecl::Create(C: *this, TU: getTranslationUnitDecl()); |
1206 | |
1207 | return ExternCContext; |
1208 | } |
1209 | |
1210 | BuiltinTemplateDecl * |
1211 | ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK, |
1212 | const IdentifierInfo *II) const { |
1213 | auto *BuiltinTemplate = |
1214 | BuiltinTemplateDecl::Create(*this, getTranslationUnitDecl(), II, BTK); |
1215 | BuiltinTemplate->setImplicit(); |
1216 | getTranslationUnitDecl()->addDecl(D: BuiltinTemplate); |
1217 | |
1218 | return BuiltinTemplate; |
1219 | } |
1220 | |
1221 | #define BuiltinTemplate(BTName) \ |
1222 | BuiltinTemplateDecl *ASTContext::get##BTName##Decl() const { \ |
1223 | if (!Decl##BTName) \ |
1224 | Decl##BTName = \ |
1225 | buildBuiltinTemplateDecl(BTK##BTName, get##BTName##Name()); \ |
1226 | return Decl##BTName; \ |
1227 | } |
1228 | #include "clang/Basic/BuiltinTemplates.inc" |
1229 | |
1230 | RecordDecl *ASTContext::buildImplicitRecord(StringRef Name, |
1231 | RecordDecl::TagKind TK) const { |
1232 | SourceLocation Loc; |
1233 | RecordDecl *NewDecl; |
1234 | if (getLangOpts().CPlusPlus) |
1235 | NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, |
1236 | Loc, &Idents.get(Name)); |
1237 | else |
1238 | NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc, |
1239 | &Idents.get(Name)); |
1240 | NewDecl->setImplicit(); |
1241 | NewDecl->addAttr(TypeVisibilityAttr::CreateImplicit( |
1242 | const_cast<ASTContext &>(*this), TypeVisibilityAttr::Default)); |
1243 | return NewDecl; |
1244 | } |
1245 | |
1246 | TypedefDecl *ASTContext::buildImplicitTypedef(QualType T, |
1247 | StringRef Name) const { |
1248 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
1249 | TypedefDecl *NewDecl = TypedefDecl::Create( |
1250 | const_cast<ASTContext &>(*this), getTranslationUnitDecl(), |
1251 | SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo); |
1252 | NewDecl->setImplicit(); |
1253 | return NewDecl; |
1254 | } |
1255 | |
1256 | TypedefDecl *ASTContext::getInt128Decl() const { |
1257 | if (!Int128Decl) |
1258 | Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t"); |
1259 | return Int128Decl; |
1260 | } |
1261 | |
1262 | TypedefDecl *ASTContext::getUInt128Decl() const { |
1263 | if (!UInt128Decl) |
1264 | UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t"); |
1265 | return UInt128Decl; |
1266 | } |
1267 | |
1268 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
1269 | auto *Ty = new (*this, alignof(BuiltinType)) BuiltinType(K); |
1270 | R = CanQualType::CreateUnsafe(Other: QualType(Ty, 0)); |
1271 | Types.push_back(Ty); |
1272 | } |
1273 | |
1274 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target, |
1275 | const TargetInfo *AuxTarget) { |
1276 | assert((!this->Target || this->Target == &Target) && |
1277 | "Incorrect target reinitialization"); |
1278 | assert(VoidTy.isNull() && "Context reinitialized?"); |
1279 | |
1280 | this->Target = &Target; |
1281 | this->AuxTarget = AuxTarget; |
1282 | |
1283 | ABI.reset(createCXXABI(Target)); |
1284 | AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(TI: Target, LangOpts); |
1285 | |
1286 | // C99 6.2.5p19. |
1287 | InitBuiltinType(VoidTy, BuiltinType::Void); |
1288 | |
1289 | // C99 6.2.5p2. |
1290 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
1291 | // C99 6.2.5p3. |
1292 | if (LangOpts.CharIsSigned) |
1293 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
1294 | else |
1295 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
1296 | // C99 6.2.5p4. |
1297 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
1298 | InitBuiltinType(ShortTy, BuiltinType::Short); |
1299 | InitBuiltinType(IntTy, BuiltinType::Int); |
1300 | InitBuiltinType(LongTy, BuiltinType::Long); |
1301 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
1302 | |
1303 | // C99 6.2.5p6. |
1304 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
1305 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
1306 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
1307 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
1308 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
1309 | |
1310 | // C99 6.2.5p10. |
1311 | InitBuiltinType(FloatTy, BuiltinType::Float); |
1312 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
1313 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
1314 | |
1315 | // GNU extension, __float128 for IEEE quadruple precision |
1316 | InitBuiltinType(Float128Ty, BuiltinType::Float128); |
1317 | |
1318 | // __ibm128 for IBM extended precision |
1319 | InitBuiltinType(Ibm128Ty, BuiltinType::Ibm128); |
1320 | |
1321 | // C11 extension ISO/IEC TS 18661-3 |
1322 | InitBuiltinType(Float16Ty, BuiltinType::Float16); |
1323 | |
1324 | // ISO/IEC JTC1 SC22 WG14 N1169 Extension |
1325 | InitBuiltinType(ShortAccumTy, BuiltinType::ShortAccum); |
1326 | InitBuiltinType(AccumTy, BuiltinType::Accum); |
1327 | InitBuiltinType(LongAccumTy, BuiltinType::LongAccum); |
1328 | InitBuiltinType(UnsignedShortAccumTy, BuiltinType::UShortAccum); |
1329 | InitBuiltinType(UnsignedAccumTy, BuiltinType::UAccum); |
1330 | InitBuiltinType(UnsignedLongAccumTy, BuiltinType::ULongAccum); |
1331 | InitBuiltinType(ShortFractTy, BuiltinType::ShortFract); |
1332 | InitBuiltinType(FractTy, BuiltinType::Fract); |
1333 | InitBuiltinType(LongFractTy, BuiltinType::LongFract); |
1334 | InitBuiltinType(UnsignedShortFractTy, BuiltinType::UShortFract); |
1335 | InitBuiltinType(UnsignedFractTy, BuiltinType::UFract); |
1336 | InitBuiltinType(UnsignedLongFractTy, BuiltinType::ULongFract); |
1337 | InitBuiltinType(SatShortAccumTy, BuiltinType::SatShortAccum); |
1338 | InitBuiltinType(SatAccumTy, BuiltinType::SatAccum); |
1339 | InitBuiltinType(SatLongAccumTy, BuiltinType::SatLongAccum); |
1340 | InitBuiltinType(SatUnsignedShortAccumTy, BuiltinType::SatUShortAccum); |
1341 | InitBuiltinType(SatUnsignedAccumTy, BuiltinType::SatUAccum); |
1342 | InitBuiltinType(SatUnsignedLongAccumTy, BuiltinType::SatULongAccum); |
1343 | InitBuiltinType(SatShortFractTy, BuiltinType::SatShortFract); |
1344 | InitBuiltinType(SatFractTy, BuiltinType::SatFract); |
1345 | InitBuiltinType(SatLongFractTy, BuiltinType::SatLongFract); |
1346 | InitBuiltinType(SatUnsignedShortFractTy, BuiltinType::SatUShortFract); |
1347 | InitBuiltinType(SatUnsignedFractTy, BuiltinType::SatUFract); |
1348 | InitBuiltinType(SatUnsignedLongFractTy, BuiltinType::SatULongFract); |
1349 | |
1350 | // GNU extension, 128-bit integers. |
1351 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
1352 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
1353 | |
1354 | // C++ 3.9.1p5 |
1355 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
1356 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
1357 | else // -fshort-wchar makes wchar_t be unsigned. |
1358 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
1359 | if (LangOpts.CPlusPlus && LangOpts.WChar) |
1360 | WideCharTy = WCharTy; |
1361 | else { |
1362 | // C99 (or C++ using -fno-wchar). |
1363 | WideCharTy = getFromTargetType(Target.getWCharType()); |
1364 | } |
1365 | |
1366 | WIntTy = getFromTargetType(Target.getWIntType()); |
1367 | |
1368 | // C++20 (proposed) |
1369 | InitBuiltinType(Char8Ty, BuiltinType::Char8); |
1370 | |
1371 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
1372 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
1373 | else // C99 |
1374 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
1375 | |
1376 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
1377 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
1378 | else // C99 |
1379 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
1380 | |
1381 | // Placeholder type for type-dependent expressions whose type is |
1382 | // completely unknown. No code should ever check a type against |
1383 | // DependentTy and users should never see it; however, it is here to |
1384 | // help diagnose failures to properly check for type-dependent |
1385 | // expressions. |
1386 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
1387 | |
1388 | // Placeholder type for functions. |
1389 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
1390 | |
1391 | // Placeholder type for bound members. |
1392 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
1393 | |
1394 | // Placeholder type for unresolved templates. |
1395 | InitBuiltinType(UnresolvedTemplateTy, BuiltinType::UnresolvedTemplate); |
1396 | |
1397 | // Placeholder type for pseudo-objects. |
1398 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
1399 | |
1400 | // "any" type; useful for debugger-like clients. |
1401 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
1402 | |
1403 | // Placeholder type for unbridged ARC casts. |
1404 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
1405 | |
1406 | // Placeholder type for builtin functions. |
1407 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
1408 | |
1409 | // Placeholder type for OMP array sections. |
1410 | if (LangOpts.OpenMP) { |
1411 | InitBuiltinType(ArraySectionTy, BuiltinType::ArraySection); |
1412 | InitBuiltinType(OMPArrayShapingTy, BuiltinType::OMPArrayShaping); |
1413 | InitBuiltinType(OMPIteratorTy, BuiltinType::OMPIterator); |
1414 | } |
1415 | // Placeholder type for OpenACC array sections, if we are ALSO in OMP mode, |
1416 | // don't bother, as we're just using the same type as OMP. |
1417 | if (LangOpts.OpenACC && !LangOpts.OpenMP) { |
1418 | InitBuiltinType(ArraySectionTy, BuiltinType::ArraySection); |
1419 | } |
1420 | if (LangOpts.MatrixTypes) |
1421 | InitBuiltinType(IncompleteMatrixIdxTy, BuiltinType::IncompleteMatrixIdx); |
1422 | |
1423 | // Builtin types for 'id', 'Class', and 'SEL'. |
1424 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
1425 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
1426 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
1427 | |
1428 | if (LangOpts.OpenCL) { |
1429 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
1430 | InitBuiltinType(SingletonId, BuiltinType::Id); |
1431 | #include "clang/Basic/OpenCLImageTypes.def" |
1432 | |
1433 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
1434 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
1435 | InitBuiltinType(OCLClkEventTy, BuiltinType::OCLClkEvent); |
1436 | InitBuiltinType(OCLQueueTy, BuiltinType::OCLQueue); |
1437 | InitBuiltinType(OCLReserveIDTy, BuiltinType::OCLReserveID); |
1438 | |
1439 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
1440 | InitBuiltinType(Id##Ty, BuiltinType::Id); |
1441 | #include "clang/Basic/OpenCLExtensionTypes.def" |
1442 | } |
1443 | |
1444 | if (LangOpts.HLSL) { |
1445 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \ |
1446 | InitBuiltinType(SingletonId, BuiltinType::Id); |
1447 | #include "clang/Basic/HLSLIntangibleTypes.def" |
1448 | } |
1449 | |
1450 | if (Target.hasAArch64ACLETypes() || |
1451 | (AuxTarget && AuxTarget->hasAArch64ACLETypes())) { |
1452 | #define SVE_TYPE(Name, Id, SingletonId) \ |
1453 | InitBuiltinType(SingletonId, BuiltinType::Id); |
1454 | #include "clang/Basic/AArch64ACLETypes.def" |
1455 | } |
1456 | |
1457 | if (Target.getTriple().isPPC64()) { |
1458 | #define PPC_VECTOR_MMA_TYPE(Name, Id, Size) \ |
1459 | InitBuiltinType(Id##Ty, BuiltinType::Id); |
1460 | #include "clang/Basic/PPCTypes.def" |
1461 | #define PPC_VECTOR_VSX_TYPE(Name, Id, Size) \ |
1462 | InitBuiltinType(Id##Ty, BuiltinType::Id); |
1463 | #include "clang/Basic/PPCTypes.def" |
1464 | } |
1465 | |
1466 | if (Target.hasRISCVVTypes()) { |
1467 | #define RVV_TYPE(Name, Id, SingletonId) \ |
1468 | InitBuiltinType(SingletonId, BuiltinType::Id); |
1469 | #include "clang/Basic/RISCVVTypes.def" |
1470 | } |
1471 | |
1472 | if (Target.getTriple().isWasm() && Target.hasFeature(Feature: "reference-types")) { |
1473 | #define WASM_TYPE(Name, Id, SingletonId) \ |
1474 | InitBuiltinType(SingletonId, BuiltinType::Id); |
1475 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
1476 | } |
1477 | |
1478 | if (Target.getTriple().isAMDGPU() || |
1479 | (AuxTarget && AuxTarget->getTriple().isAMDGPU())) { |
1480 | #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \ |
1481 | InitBuiltinType(SingletonId, BuiltinType::Id); |
1482 | #include "clang/Basic/AMDGPUTypes.def" |
1483 | } |
1484 | |
1485 | // Builtin type for __objc_yes and __objc_no |
1486 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
1487 | SignedCharTy : BoolTy); |
1488 | |
1489 | ObjCConstantStringType = QualType(); |
1490 | |
1491 | ObjCSuperType = QualType(); |
1492 | |
1493 | // void * type |
1494 | if (LangOpts.OpenCLGenericAddressSpace) { |
1495 | auto Q = VoidTy.getQualifiers(); |
1496 | Q.setAddressSpace(LangAS::opencl_generic); |
1497 | VoidPtrTy = getPointerType(getCanonicalType( |
1498 | getQualifiedType(VoidTy.getUnqualifiedType(), Q))); |
1499 | } else { |
1500 | VoidPtrTy = getPointerType(VoidTy); |
1501 | } |
1502 | |
1503 | // nullptr type (C++0x 2.14.7) |
1504 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
1505 | |
1506 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
1507 | InitBuiltinType(HalfTy, BuiltinType::Half); |
1508 | |
1509 | InitBuiltinType(BFloat16Ty, BuiltinType::BFloat16); |
1510 | |
1511 | // Builtin type used to help define __builtin_va_list. |
1512 | VaListTagDecl = nullptr; |
1513 | |
1514 | // MSVC predeclares struct _GUID, and we need it to create MSGuidDecls. |
1515 | if (LangOpts.MicrosoftExt || LangOpts.Borland) { |
1516 | MSGuidTagDecl = buildImplicitRecord(Name: "_GUID"); |
1517 | getTranslationUnitDecl()->addDecl(MSGuidTagDecl); |
1518 | } |
1519 | } |
1520 | |
1521 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
1522 | return SourceMgr.getDiagnostics(); |
1523 | } |
1524 | |
1525 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
1526 | AttrVec *&Result = DeclAttrs[D]; |
1527 | if (!Result) { |
1528 | void *Mem = Allocate(Size: sizeof(AttrVec)); |
1529 | Result = new (Mem) AttrVec; |
1530 | } |
1531 | |
1532 | return *Result; |
1533 | } |
1534 | |
1535 | /// Erase the attributes corresponding to the given declaration. |
1536 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
1537 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
1538 | if (Pos != DeclAttrs.end()) { |
1539 | Pos->second->~AttrVec(); |
1540 | DeclAttrs.erase(Pos); |
1541 | } |
1542 | } |
1543 | |
1544 | // FIXME: Remove ? |
1545 | MemberSpecializationInfo * |
1546 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
1547 | assert(Var->isStaticDataMember() && "Not a static data member"); |
1548 | return getTemplateOrSpecializationInfo(Var) |
1549 | .dyn_cast<MemberSpecializationInfo *>(); |
1550 | } |
1551 | |
1552 | ASTContext::TemplateOrSpecializationInfo |
1553 | ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) { |
1554 | llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos = |
1555 | TemplateOrInstantiation.find(Var); |
1556 | if (Pos == TemplateOrInstantiation.end()) |
1557 | return {}; |
1558 | |
1559 | return Pos->second; |
1560 | } |
1561 | |
1562 | void |
1563 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
1564 | TemplateSpecializationKind TSK, |
1565 | SourceLocation PointOfInstantiation) { |
1566 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
1567 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
1568 | setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo( |
1569 | Tmpl, TSK, PointOfInstantiation)); |
1570 | } |
1571 | |
1572 | void |
1573 | ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst, |
1574 | TemplateOrSpecializationInfo TSI) { |
1575 | assert(!TemplateOrInstantiation[Inst] && |
1576 | "Already noted what the variable was instantiated from"); |
1577 | TemplateOrInstantiation[Inst] = TSI; |
1578 | } |
1579 | |
1580 | NamedDecl * |
1581 | ASTContext::getInstantiatedFromUsingDecl(NamedDecl *UUD) { |
1582 | return InstantiatedFromUsingDecl.lookup(UUD); |
1583 | } |
1584 | |
1585 | void |
1586 | ASTContext::setInstantiatedFromUsingDecl(NamedDecl *Inst, NamedDecl *Pattern) { |
1587 | assert((isa<UsingDecl>(Pattern) || |
1588 | isa<UnresolvedUsingValueDecl>(Pattern) || |
1589 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
1590 | "pattern decl is not a using decl"); |
1591 | assert((isa<UsingDecl>(Inst) || |
1592 | isa<UnresolvedUsingValueDecl>(Inst) || |
1593 | isa<UnresolvedUsingTypenameDecl>(Inst)) && |
1594 | "instantiation did not produce a using decl"); |
1595 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
1596 | InstantiatedFromUsingDecl[Inst] = Pattern; |
1597 | } |
1598 | |
1599 | UsingEnumDecl * |
1600 | ASTContext::getInstantiatedFromUsingEnumDecl(UsingEnumDecl *UUD) { |
1601 | return InstantiatedFromUsingEnumDecl.lookup(UUD); |
1602 | } |
1603 | |
1604 | void ASTContext::setInstantiatedFromUsingEnumDecl(UsingEnumDecl *Inst, |
1605 | UsingEnumDecl *Pattern) { |
1606 | assert(!InstantiatedFromUsingEnumDecl[Inst] && "pattern already exists"); |
1607 | InstantiatedFromUsingEnumDecl[Inst] = Pattern; |
1608 | } |
1609 | |
1610 | UsingShadowDecl * |
1611 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
1612 | return InstantiatedFromUsingShadowDecl.lookup(Inst); |
1613 | } |
1614 | |
1615 | void |
1616 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
1617 | UsingShadowDecl *Pattern) { |
1618 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
1619 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
1620 | } |
1621 | |
1622 | FieldDecl * |
1623 | ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) const { |
1624 | return InstantiatedFromUnnamedFieldDecl.lookup(Field); |
1625 | } |
1626 | |
1627 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
1628 | FieldDecl *Tmpl) { |
1629 | assert((!Inst->getDeclName() || Inst->isPlaceholderVar(getLangOpts())) && |
1630 | "Instantiated field decl is not unnamed"); |
1631 | assert((!Inst->getDeclName() || Inst->isPlaceholderVar(getLangOpts())) && |
1632 | "Template field decl is not unnamed"); |
1633 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
1634 | "Already noted what unnamed field was instantiated from"); |
1635 | |
1636 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
1637 | } |
1638 | |
1639 | ASTContext::overridden_cxx_method_iterator |
1640 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
1641 | return overridden_methods(Method).begin(); |
1642 | } |
1643 | |
1644 | ASTContext::overridden_cxx_method_iterator |
1645 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
1646 | return overridden_methods(Method).end(); |
1647 | } |
1648 | |
1649 | unsigned |
1650 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
1651 | auto Range = overridden_methods(Method); |
1652 | return Range.end() - Range.begin(); |
1653 | } |
1654 | |
1655 | ASTContext::overridden_method_range |
1656 | ASTContext::overridden_methods(const CXXMethodDecl *Method) const { |
1657 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos = |
1658 | OverriddenMethods.find(Method->getCanonicalDecl()); |
1659 | if (Pos == OverriddenMethods.end()) |
1660 | return overridden_method_range(nullptr, nullptr); |
1661 | return overridden_method_range(Pos->second.begin(), Pos->second.end()); |
1662 | } |
1663 | |
1664 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
1665 | const CXXMethodDecl *Overridden) { |
1666 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
1667 | OverriddenMethods[Method].push_back(Overridden); |
1668 | } |
1669 | |
1670 | void ASTContext::getOverriddenMethods( |
1671 | const NamedDecl *D, |
1672 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
1673 | assert(D); |
1674 | |
1675 | if (const auto *CXXMethod = dyn_cast<CXXMethodDecl>(Val: D)) { |
1676 | Overridden.append(overridden_methods_begin(CXXMethod), |
1677 | overridden_methods_end(CXXMethod)); |
1678 | return; |
1679 | } |
1680 | |
1681 | const auto *Method = dyn_cast<ObjCMethodDecl>(Val: D); |
1682 | if (!Method) |
1683 | return; |
1684 | |
1685 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
1686 | Method->getOverriddenMethods(Overridden&: OverDecls); |
1687 | Overridden.append(in_start: OverDecls.begin(), in_end: OverDecls.end()); |
1688 | } |
1689 | |
1690 | std::optional<ASTContext::CXXRecordDeclRelocationInfo> |
1691 | ASTContext::getRelocationInfoForCXXRecord(const CXXRecordDecl *RD) const { |
1692 | assert(RD); |
1693 | CXXRecordDecl *D = RD->getDefinition(); |
1694 | auto it = RelocatableClasses.find(D); |
1695 | if (it != RelocatableClasses.end()) |
1696 | return it->getSecond(); |
1697 | return std::nullopt; |
1698 | } |
1699 | |
1700 | void ASTContext::setRelocationInfoForCXXRecord( |
1701 | const CXXRecordDecl *RD, CXXRecordDeclRelocationInfo Info) { |
1702 | assert(RD); |
1703 | CXXRecordDecl *D = RD->getDefinition(); |
1704 | assert(RelocatableClasses.find(D) == RelocatableClasses.end()); |
1705 | RelocatableClasses.insert({D, Info}); |
1706 | } |
1707 | |
1708 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
1709 | assert(!Import->getNextLocalImport() && |
1710 | "Import declaration already in the chain"); |
1711 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
1712 | if (!FirstLocalImport) { |
1713 | FirstLocalImport = Import; |
1714 | LastLocalImport = Import; |
1715 | return; |
1716 | } |
1717 | |
1718 | LastLocalImport->setNextLocalImport(Import); |
1719 | LastLocalImport = Import; |
1720 | } |
1721 | |
1722 | //===----------------------------------------------------------------------===// |
1723 | // Type Sizing and Analysis |
1724 | //===----------------------------------------------------------------------===// |
1725 | |
1726 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
1727 | /// scalar floating point type. |
1728 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
1729 | switch (T->castAs<BuiltinType>()->getKind()) { |
1730 | default: |
1731 | llvm_unreachable("Not a floating point type!"); |
1732 | case BuiltinType::BFloat16: |
1733 | return Target->getBFloat16Format(); |
1734 | case BuiltinType::Float16: |
1735 | return Target->getHalfFormat(); |
1736 | case BuiltinType::Half: |
1737 | return Target->getHalfFormat(); |
1738 | case BuiltinType::Float: return Target->getFloatFormat(); |
1739 | case BuiltinType::Double: return Target->getDoubleFormat(); |
1740 | case BuiltinType::Ibm128: |
1741 | return Target->getIbm128Format(); |
1742 | case BuiltinType::LongDouble: |
1743 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice) |
1744 | return AuxTarget->getLongDoubleFormat(); |
1745 | return Target->getLongDoubleFormat(); |
1746 | case BuiltinType::Float128: |
1747 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice) |
1748 | return AuxTarget->getFloat128Format(); |
1749 | return Target->getFloat128Format(); |
1750 | } |
1751 | } |
1752 | |
1753 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const { |
1754 | unsigned Align = Target->getCharWidth(); |
1755 | |
1756 | const unsigned AlignFromAttr = D->getMaxAlignment(); |
1757 | if (AlignFromAttr) |
1758 | Align = AlignFromAttr; |
1759 | |
1760 | // __attribute__((aligned)) can increase or decrease alignment |
1761 | // *except* on a struct or struct member, where it only increases |
1762 | // alignment unless 'packed' is also specified. |
1763 | // |
1764 | // It is an error for alignas to decrease alignment, so we can |
1765 | // ignore that possibility; Sema should diagnose it. |
1766 | bool UseAlignAttrOnly; |
1767 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: D)) |
1768 | UseAlignAttrOnly = |
1769 | FD->hasAttr<PackedAttr>() || FD->getParent()->hasAttr<PackedAttr>(); |
1770 | else |
1771 | UseAlignAttrOnly = AlignFromAttr != 0; |
1772 | // If we're using the align attribute only, just ignore everything |
1773 | // else about the declaration and its type. |
1774 | if (UseAlignAttrOnly) { |
1775 | // do nothing |
1776 | } else if (const auto *VD = dyn_cast<ValueDecl>(Val: D)) { |
1777 | QualType T = VD->getType(); |
1778 | if (const auto *RT = T->getAs<ReferenceType>()) { |
1779 | if (ForAlignof) |
1780 | T = RT->getPointeeType(); |
1781 | else |
1782 | T = getPointerType(T: RT->getPointeeType()); |
1783 | } |
1784 | QualType BaseT = getBaseElementType(QT: T); |
1785 | if (T->isFunctionType()) |
1786 | Align = getTypeInfoImpl(T: T.getTypePtr()).Align; |
1787 | else if (!BaseT->isIncompleteType()) { |
1788 | // Adjust alignments of declarations with array type by the |
1789 | // large-array alignment on the target. |
1790 | if (const ArrayType *arrayType = getAsArrayType(T)) { |
1791 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
1792 | if (!ForAlignof && MinWidth) { |
1793 | if (isa<VariableArrayType>(Val: arrayType)) |
1794 | Align = std::max(a: Align, b: Target->getLargeArrayAlign()); |
1795 | else if (isa<ConstantArrayType>(Val: arrayType) && |
1796 | MinWidth <= getTypeSize(cast<ConstantArrayType>(Val: arrayType))) |
1797 | Align = std::max(a: Align, b: Target->getLargeArrayAlign()); |
1798 | } |
1799 | } |
1800 | Align = std::max(a: Align, b: getPreferredTypeAlign(T: T.getTypePtr())); |
1801 | if (BaseT.getQualifiers().hasUnaligned()) |
1802 | Align = Target->getCharWidth(); |
1803 | } |
1804 | |
1805 | // Ensure minimum alignment for global variables. |
1806 | if (const auto *VD = dyn_cast<VarDecl>(Val: D)) |
1807 | if (VD->hasGlobalStorage() && !ForAlignof) { |
1808 | uint64_t TypeSize = |
1809 | !BaseT->isIncompleteType() ? getTypeSize(T: T.getTypePtr()) : 0; |
1810 | Align = std::max(a: Align, b: getMinGlobalAlignOfVar(Size: TypeSize, VD)); |
1811 | } |
1812 | |
1813 | // Fields can be subject to extra alignment constraints, like if |
1814 | // the field is packed, the struct is packed, or the struct has a |
1815 | // a max-field-alignment constraint (#pragma pack). So calculate |
1816 | // the actual alignment of the field within the struct, and then |
1817 | // (as we're expected to) constrain that by the alignment of the type. |
1818 | if (const auto *Field = dyn_cast<FieldDecl>(Val: VD)) { |
1819 | const RecordDecl *Parent = Field->getParent(); |
1820 | // We can only produce a sensible answer if the record is valid. |
1821 | if (!Parent->isInvalidDecl()) { |
1822 | const ASTRecordLayout &Layout = getASTRecordLayout(D: Parent); |
1823 | |
1824 | // Start with the record's overall alignment. |
1825 | unsigned FieldAlign = toBits(CharSize: Layout.getAlignment()); |
1826 | |
1827 | // Use the GCD of that and the offset within the record. |
1828 | uint64_t Offset = Layout.getFieldOffset(FieldNo: Field->getFieldIndex()); |
1829 | if (Offset > 0) { |
1830 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
1831 | // which means we get to do this crazy thing instead of Euclid's. |
1832 | uint64_t LowBitOfOffset = Offset & (~Offset + 1); |
1833 | if (LowBitOfOffset < FieldAlign) |
1834 | FieldAlign = static_cast<unsigned>(LowBitOfOffset); |
1835 | } |
1836 | |
1837 | Align = std::min(a: Align, b: FieldAlign); |
1838 | } |
1839 | } |
1840 | } |
1841 | |
1842 | // Some targets have hard limitation on the maximum requestable alignment in |
1843 | // aligned attribute for static variables. |
1844 | const unsigned MaxAlignedAttr = getTargetInfo().getMaxAlignedAttribute(); |
1845 | const auto *VD = dyn_cast<VarDecl>(Val: D); |
1846 | if (MaxAlignedAttr && VD && VD->getStorageClass() == SC_Static) |
1847 | Align = std::min(a: Align, b: MaxAlignedAttr); |
1848 | |
1849 | return toCharUnitsFromBits(BitSize: Align); |
1850 | } |
1851 | |
1852 | CharUnits ASTContext::getExnObjectAlignment() const { |
1853 | return toCharUnitsFromBits(BitSize: Target->getExnObjectAlignment()); |
1854 | } |
1855 | |
1856 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
1857 | // chars. If the type is a record, its data size is returned. This is |
1858 | // the size of the memcpy that's performed when assigning this type |
1859 | // using a trivial copy/move assignment operator. |
1860 | TypeInfoChars ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
1861 | TypeInfoChars Info = getTypeInfoInChars(T); |
1862 | |
1863 | // In C++, objects can sometimes be allocated into the tail padding |
1864 | // of a base-class subobject. We decide whether that's possible |
1865 | // during class layout, so here we can just trust the layout results. |
1866 | if (getLangOpts().CPlusPlus) { |
1867 | if (const auto *RT = T->getAs<RecordType>(); |
1868 | RT && !RT->getDecl()->isInvalidDecl()) { |
1869 | const ASTRecordLayout &layout = getASTRecordLayout(D: RT->getDecl()); |
1870 | Info.Width = layout.getDataSize(); |
1871 | } |
1872 | } |
1873 | |
1874 | return Info; |
1875 | } |
1876 | |
1877 | /// getConstantArrayInfoInChars - Performing the computation in CharUnits |
1878 | /// instead of in bits prevents overflowing the uint64_t for some large arrays. |
1879 | TypeInfoChars |
1880 | static getConstantArrayInfoInChars(const ASTContext &Context, |
1881 | const ConstantArrayType *CAT) { |
1882 | TypeInfoChars EltInfo = Context.getTypeInfoInChars(CAT->getElementType()); |
1883 | uint64_t Size = CAT->getZExtSize(); |
1884 | assert((Size == 0 || static_cast<uint64_t>(EltInfo.Width.getQuantity()) <= |
1885 | (uint64_t)(-1)/Size) && |
1886 | "Overflow in array type char size evaluation"); |
1887 | uint64_t Width = EltInfo.Width.getQuantity() * Size; |
1888 | unsigned Align = EltInfo.Align.getQuantity(); |
1889 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() || |
1890 | Context.getTargetInfo().getPointerWidth(AddrSpace: LangAS::Default) == 64) |
1891 | Width = llvm::alignTo(Value: Width, Align); |
1892 | return TypeInfoChars(CharUnits::fromQuantity(Quantity: Width), |
1893 | CharUnits::fromQuantity(Quantity: Align), |
1894 | EltInfo.AlignRequirement); |
1895 | } |
1896 | |
1897 | TypeInfoChars ASTContext::getTypeInfoInChars(const Type *T) const { |
1898 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: T)) |
1899 | return getConstantArrayInfoInChars(Context: *this, CAT); |
1900 | TypeInfo Info = getTypeInfo(T); |
1901 | return TypeInfoChars(toCharUnitsFromBits(BitSize: Info.Width), |
1902 | toCharUnitsFromBits(BitSize: Info.Align), Info.AlignRequirement); |
1903 | } |
1904 | |
1905 | TypeInfoChars ASTContext::getTypeInfoInChars(QualType T) const { |
1906 | return getTypeInfoInChars(T: T.getTypePtr()); |
1907 | } |
1908 | |
1909 | bool ASTContext::isPromotableIntegerType(QualType T) const { |
1910 | // HLSL doesn't promote all small integer types to int, it |
1911 | // just uses the rank-based promotion rules for all types. |
1912 | if (getLangOpts().HLSL) |
1913 | return false; |
1914 | |
1915 | if (const auto *BT = T->getAs<BuiltinType>()) |
1916 | switch (BT->getKind()) { |
1917 | case BuiltinType::Bool: |
1918 | case BuiltinType::Char_S: |
1919 | case BuiltinType::Char_U: |
1920 | case BuiltinType::SChar: |
1921 | case BuiltinType::UChar: |
1922 | case BuiltinType::Short: |
1923 | case BuiltinType::UShort: |
1924 | case BuiltinType::WChar_S: |
1925 | case BuiltinType::WChar_U: |
1926 | case BuiltinType::Char8: |
1927 | case BuiltinType::Char16: |
1928 | case BuiltinType::Char32: |
1929 | return true; |
1930 | default: |
1931 | return false; |
1932 | } |
1933 | |
1934 | // Enumerated types are promotable to their compatible integer types |
1935 | // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2). |
1936 | if (const auto *ET = T->getAs<EnumType>()) { |
1937 | if (T->isDependentType() || ET->getDecl()->getPromotionType().isNull() || |
1938 | ET->getDecl()->isScoped()) |
1939 | return false; |
1940 | |
1941 | return true; |
1942 | } |
1943 | |
1944 | return false; |
1945 | } |
1946 | |
1947 | bool ASTContext::isAlignmentRequired(const Type *T) const { |
1948 | return getTypeInfo(T).AlignRequirement != AlignRequirementKind::None; |
1949 | } |
1950 | |
1951 | bool ASTContext::isAlignmentRequired(QualType T) const { |
1952 | return isAlignmentRequired(T: T.getTypePtr()); |
1953 | } |
1954 | |
1955 | unsigned ASTContext::getTypeAlignIfKnown(QualType T, |
1956 | bool NeedsPreferredAlignment) const { |
1957 | // An alignment on a typedef overrides anything else. |
1958 | if (const auto *TT = T->getAs<TypedefType>()) |
1959 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
1960 | return Align; |
1961 | |
1962 | // If we have an (array of) complete type, we're done. |
1963 | T = getBaseElementType(QT: T); |
1964 | if (!T->isIncompleteType()) |
1965 | return NeedsPreferredAlignment ? getPreferredTypeAlign(T) : getTypeAlign(T); |
1966 | |
1967 | // If we had an array type, its element type might be a typedef |
1968 | // type with an alignment attribute. |
1969 | if (const auto *TT = T->getAs<TypedefType>()) |
1970 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
1971 | return Align; |
1972 | |
1973 | // Otherwise, see if the declaration of the type had an attribute. |
1974 | if (const auto *TT = T->getAs<TagType>()) |
1975 | return TT->getDecl()->getMaxAlignment(); |
1976 | |
1977 | return 0; |
1978 | } |
1979 | |
1980 | TypeInfo ASTContext::getTypeInfo(const Type *T) const { |
1981 | TypeInfoMap::iterator I = MemoizedTypeInfo.find(Val: T); |
1982 | if (I != MemoizedTypeInfo.end()) |
1983 | return I->second; |
1984 | |
1985 | // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup. |
1986 | TypeInfo TI = getTypeInfoImpl(T); |
1987 | MemoizedTypeInfo[T] = TI; |
1988 | return TI; |
1989 | } |
1990 | |
1991 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
1992 | /// method does not work on incomplete types. |
1993 | /// |
1994 | /// FIXME: Pointers into different addr spaces could have different sizes and |
1995 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
1996 | /// should take a QualType, &c. |
1997 | TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { |
1998 | uint64_t Width = 0; |
1999 | unsigned Align = 8; |
2000 | AlignRequirementKind AlignRequirement = AlignRequirementKind::None; |
2001 | LangAS AS = LangAS::Default; |
2002 | switch (T->getTypeClass()) { |
2003 | #define TYPE(Class, Base) |
2004 | #define ABSTRACT_TYPE(Class, Base) |
2005 | #define NON_CANONICAL_TYPE(Class, Base) |
2006 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
2007 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \ |
2008 | case Type::Class: \ |
2009 | assert(!T->isDependentType() && "should not see dependent types here"); \ |
2010 | return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr()); |
2011 | #include "clang/AST/TypeNodes.inc" |
2012 | llvm_unreachable("Should not see dependent types"); |
2013 | |
2014 | case Type::FunctionNoProto: |
2015 | case Type::FunctionProto: |
2016 | // GCC extension: alignof(function) = 32 bits |
2017 | Width = 0; |
2018 | Align = 32; |
2019 | break; |
2020 | |
2021 | case Type::IncompleteArray: |
2022 | case Type::VariableArray: |
2023 | case Type::ConstantArray: |
2024 | case Type::ArrayParameter: { |
2025 | // Model non-constant sized arrays as size zero, but track the alignment. |
2026 | uint64_t Size = 0; |
2027 | if (const auto *CAT = dyn_cast<ConstantArrayType>(T)) |
2028 | Size = CAT->getZExtSize(); |
2029 | |
2030 | TypeInfo EltInfo = getTypeInfo(cast<ArrayType>(T)->getElementType()); |
2031 | assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && |
2032 | "Overflow in array type bit size evaluation"); |
2033 | Width = EltInfo.Width * Size; |
2034 | Align = EltInfo.Align; |
2035 | AlignRequirement = EltInfo.AlignRequirement; |
2036 | if (!getTargetInfo().getCXXABI().isMicrosoft() || |
2037 | getTargetInfo().getPointerWidth(AddrSpace: LangAS::Default) == 64) |
2038 | Width = llvm::alignTo(Value: Width, Align); |
2039 | break; |
2040 | } |
2041 | |
2042 | case Type::ExtVector: |
2043 | case Type::Vector: { |
2044 | const auto *VT = cast<VectorType>(T); |
2045 | TypeInfo EltInfo = getTypeInfo(VT->getElementType()); |
2046 | Width = VT->isPackedVectorBoolType(*this) |
2047 | ? VT->getNumElements() |
2048 | : EltInfo.Width * VT->getNumElements(); |
2049 | // Enforce at least byte size and alignment. |
2050 | Width = std::max<unsigned>(8, Width); |
2051 | Align = std::max<unsigned>(8, Width); |
2052 | |
2053 | // If the alignment is not a power of 2, round up to the next power of 2. |
2054 | // This happens for non-power-of-2 length vectors. |
2055 | if (Align & (Align-1)) { |
2056 | Align = llvm::bit_ceil(Align); |
2057 | Width = llvm::alignTo(Value: Width, Align); |
2058 | } |
2059 | // Adjust the alignment based on the target max. |
2060 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
2061 | if (TargetVectorAlign && TargetVectorAlign < Align) |
2062 | Align = TargetVectorAlign; |
2063 | if (VT->getVectorKind() == VectorKind::SveFixedLengthData) |
2064 | // Adjust the alignment for fixed-length SVE vectors. This is important |
2065 | // for non-power-of-2 vector lengths. |
2066 | Align = 128; |
2067 | else if (VT->getVectorKind() == VectorKind::SveFixedLengthPredicate) |
2068 | // Adjust the alignment for fixed-length SVE predicates. |
2069 | Align = 16; |
2070 | else if (VT->getVectorKind() == VectorKind::RVVFixedLengthData || |
2071 | VT->getVectorKind() == VectorKind::RVVFixedLengthMask || |
2072 | VT->getVectorKind() == VectorKind::RVVFixedLengthMask_1 || |
2073 | VT->getVectorKind() == VectorKind::RVVFixedLengthMask_2 || |
2074 | VT->getVectorKind() == VectorKind::RVVFixedLengthMask_4) |
2075 | // Adjust the alignment for fixed-length RVV vectors. |
2076 | Align = std::min<unsigned>(64, Width); |
2077 | break; |
2078 | } |
2079 | |
2080 | case Type::ConstantMatrix: { |
2081 | const auto *MT = cast<ConstantMatrixType>(T); |
2082 | TypeInfo ElementInfo = getTypeInfo(MT->getElementType()); |
2083 | // The internal layout of a matrix value is implementation defined. |
2084 | // Initially be ABI compatible with arrays with respect to alignment and |
2085 | // size. |
2086 | Width = ElementInfo.Width * MT->getNumRows() * MT->getNumColumns(); |
2087 | Align = ElementInfo.Align; |
2088 | break; |
2089 | } |
2090 | |
2091 | case Type::Builtin: |
2092 | switch (cast<BuiltinType>(T)->getKind()) { |
2093 | default: llvm_unreachable("Unknown builtin type!"); |
2094 | case BuiltinType::Void: |
2095 | // GCC extension: alignof(void) = 8 bits. |
2096 | Width = 0; |
2097 | Align = 8; |
2098 | break; |
2099 | case BuiltinType::Bool: |
2100 | Width = Target->getBoolWidth(); |
2101 | Align = Target->getBoolAlign(); |
2102 | break; |
2103 | case BuiltinType::Char_S: |
2104 | case BuiltinType::Char_U: |
2105 | case BuiltinType::UChar: |
2106 | case BuiltinType::SChar: |
2107 | case BuiltinType::Char8: |
2108 | Width = Target->getCharWidth(); |
2109 | Align = Target->getCharAlign(); |
2110 | break; |
2111 | case BuiltinType::WChar_S: |
2112 | case BuiltinType::WChar_U: |
2113 | Width = Target->getWCharWidth(); |
2114 | Align = Target->getWCharAlign(); |
2115 | break; |
2116 | case BuiltinType::Char16: |
2117 | Width = Target->getChar16Width(); |
2118 | Align = Target->getChar16Align(); |
2119 | break; |
2120 | case BuiltinType::Char32: |
2121 | Width = Target->getChar32Width(); |
2122 | Align = Target->getChar32Align(); |
2123 | break; |
2124 | case BuiltinType::UShort: |
2125 | case BuiltinType::Short: |
2126 | Width = Target->getShortWidth(); |
2127 | Align = Target->getShortAlign(); |
2128 | break; |
2129 | case BuiltinType::UInt: |
2130 | case BuiltinType::Int: |
2131 | Width = Target->getIntWidth(); |
2132 | Align = Target->getIntAlign(); |
2133 | break; |
2134 | case BuiltinType::ULong: |
2135 | case BuiltinType::Long: |
2136 | Width = Target->getLongWidth(); |
2137 | Align = Target->getLongAlign(); |
2138 | break; |
2139 | case BuiltinType::ULongLong: |
2140 | case BuiltinType::LongLong: |
2141 | Width = Target->getLongLongWidth(); |
2142 | Align = Target->getLongLongAlign(); |
2143 | break; |
2144 | case BuiltinType::Int128: |
2145 | case BuiltinType::UInt128: |
2146 | Width = 128; |
2147 | Align = Target->getInt128Align(); |
2148 | break; |
2149 | case BuiltinType::ShortAccum: |
2150 | case BuiltinType::UShortAccum: |
2151 | case BuiltinType::SatShortAccum: |
2152 | case BuiltinType::SatUShortAccum: |
2153 | Width = Target->getShortAccumWidth(); |
2154 | Align = Target->getShortAccumAlign(); |
2155 | break; |
2156 | case BuiltinType::Accum: |
2157 | case BuiltinType::UAccum: |
2158 | case BuiltinType::SatAccum: |
2159 | case BuiltinType::SatUAccum: |
2160 | Width = Target->getAccumWidth(); |
2161 | Align = Target->getAccumAlign(); |
2162 | break; |
2163 | case BuiltinType::LongAccum: |
2164 | case BuiltinType::ULongAccum: |
2165 | case BuiltinType::SatLongAccum: |
2166 | case BuiltinType::SatULongAccum: |
2167 | Width = Target->getLongAccumWidth(); |
2168 | Align = Target->getLongAccumAlign(); |
2169 | break; |
2170 | case BuiltinType::ShortFract: |
2171 | case BuiltinType::UShortFract: |
2172 | case BuiltinType::SatShortFract: |
2173 | case BuiltinType::SatUShortFract: |
2174 | Width = Target->getShortFractWidth(); |
2175 | Align = Target->getShortFractAlign(); |
2176 | break; |
2177 | case BuiltinType::Fract: |
2178 | case BuiltinType::UFract: |
2179 | case BuiltinType::SatFract: |
2180 | case BuiltinType::SatUFract: |
2181 | Width = Target->getFractWidth(); |
2182 | Align = Target->getFractAlign(); |
2183 | break; |
2184 | case BuiltinType::LongFract: |
2185 | case BuiltinType::ULongFract: |
2186 | case BuiltinType::SatLongFract: |
2187 | case BuiltinType::SatULongFract: |
2188 | Width = Target->getLongFractWidth(); |
2189 | Align = Target->getLongFractAlign(); |
2190 | break; |
2191 | case BuiltinType::BFloat16: |
2192 | if (Target->hasBFloat16Type()) { |
2193 | Width = Target->getBFloat16Width(); |
2194 | Align = Target->getBFloat16Align(); |
2195 | } else if ((getLangOpts().SYCLIsDevice || |
2196 | (getLangOpts().OpenMP && |
2197 | getLangOpts().OpenMPIsTargetDevice)) && |
2198 | AuxTarget->hasBFloat16Type()) { |
2199 | Width = AuxTarget->getBFloat16Width(); |
2200 | Align = AuxTarget->getBFloat16Align(); |
2201 | } |
2202 | break; |
2203 | case BuiltinType::Float16: |
2204 | case BuiltinType::Half: |
2205 | if (Target->hasFloat16Type() || !getLangOpts().OpenMP || |
2206 | !getLangOpts().OpenMPIsTargetDevice) { |
2207 | Width = Target->getHalfWidth(); |
2208 | Align = Target->getHalfAlign(); |
2209 | } else { |
2210 | assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice && |
2211 | "Expected OpenMP device compilation."); |
2212 | Width = AuxTarget->getHalfWidth(); |
2213 | Align = AuxTarget->getHalfAlign(); |
2214 | } |
2215 | break; |
2216 | case BuiltinType::Float: |
2217 | Width = Target->getFloatWidth(); |
2218 | Align = Target->getFloatAlign(); |
2219 | break; |
2220 | case BuiltinType::Double: |
2221 | Width = Target->getDoubleWidth(); |
2222 | Align = Target->getDoubleAlign(); |
2223 | break; |
2224 | case BuiltinType::Ibm128: |
2225 | Width = Target->getIbm128Width(); |
2226 | Align = Target->getIbm128Align(); |
2227 | break; |
2228 | case BuiltinType::LongDouble: |
2229 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice && |
2230 | (Target->getLongDoubleWidth() != AuxTarget->getLongDoubleWidth() || |
2231 | Target->getLongDoubleAlign() != AuxTarget->getLongDoubleAlign())) { |
2232 | Width = AuxTarget->getLongDoubleWidth(); |
2233 | Align = AuxTarget->getLongDoubleAlign(); |
2234 | } else { |
2235 | Width = Target->getLongDoubleWidth(); |
2236 | Align = Target->getLongDoubleAlign(); |
2237 | } |
2238 | break; |
2239 | case BuiltinType::Float128: |
2240 | if (Target->hasFloat128Type() || !getLangOpts().OpenMP || |
2241 | !getLangOpts().OpenMPIsTargetDevice) { |
2242 | Width = Target->getFloat128Width(); |
2243 | Align = Target->getFloat128Align(); |
2244 | } else { |
2245 | assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice && |
2246 | "Expected OpenMP device compilation."); |
2247 | Width = AuxTarget->getFloat128Width(); |
2248 | Align = AuxTarget->getFloat128Align(); |
2249 | } |
2250 | break; |
2251 | case BuiltinType::NullPtr: |
2252 | // C++ 3.9.1p11: sizeof(nullptr_t) == sizeof(void*) |
2253 | Width = Target->getPointerWidth(AddrSpace: LangAS::Default); |
2254 | Align = Target->getPointerAlign(AddrSpace: LangAS::Default); |
2255 | break; |
2256 | case BuiltinType::ObjCId: |
2257 | case BuiltinType::ObjCClass: |
2258 | case BuiltinType::ObjCSel: |
2259 | Width = Target->getPointerWidth(AddrSpace: LangAS::Default); |
2260 | Align = Target->getPointerAlign(AddrSpace: LangAS::Default); |
2261 | break; |
2262 | case BuiltinType::OCLSampler: |
2263 | case BuiltinType::OCLEvent: |
2264 | case BuiltinType::OCLClkEvent: |
2265 | case BuiltinType::OCLQueue: |
2266 | case BuiltinType::OCLReserveID: |
2267 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
2268 | case BuiltinType::Id: |
2269 | #include "clang/Basic/OpenCLImageTypes.def" |
2270 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
2271 | case BuiltinType::Id: |
2272 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2273 | AS = Target->getOpenCLTypeAddrSpace(TK: getOpenCLTypeKind(T)); |
2274 | Width = Target->getPointerWidth(AddrSpace: AS); |
2275 | Align = Target->getPointerAlign(AddrSpace: AS); |
2276 | break; |
2277 | // The SVE types are effectively target-specific. The length of an |
2278 | // SVE_VECTOR_TYPE is only known at runtime, but it is always a multiple |
2279 | // of 128 bits. There is one predicate bit for each vector byte, so the |
2280 | // length of an SVE_PREDICATE_TYPE is always a multiple of 16 bits. |
2281 | // |
2282 | // Because the length is only known at runtime, we use a dummy value |
2283 | // of 0 for the static length. The alignment values are those defined |
2284 | // by the Procedure Call Standard for the Arm Architecture. |
2285 | #define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId) \ |
2286 | case BuiltinType::Id: \ |
2287 | Width = 0; \ |
2288 | Align = 128; \ |
2289 | break; |
2290 | #define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId) \ |
2291 | case BuiltinType::Id: \ |
2292 | Width = 0; \ |
2293 | Align = 16; \ |
2294 | break; |
2295 | #define SVE_OPAQUE_TYPE(Name, MangledName, Id, SingletonId) \ |
2296 | case BuiltinType::Id: \ |
2297 | Width = 0; \ |
2298 | Align = 16; \ |
2299 | break; |
2300 | #define SVE_SCALAR_TYPE(Name, MangledName, Id, SingletonId, Bits) \ |
2301 | case BuiltinType::Id: \ |
2302 | Width = Bits; \ |
2303 | Align = Bits; \ |
2304 | break; |
2305 | #include "clang/Basic/AArch64ACLETypes.def" |
2306 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
2307 | case BuiltinType::Id: \ |
2308 | Width = Size; \ |
2309 | Align = Size; \ |
2310 | break; |
2311 | #include "clang/Basic/PPCTypes.def" |
2312 | #define RVV_VECTOR_TYPE(Name, Id, SingletonId, ElKind, ElBits, NF, IsSigned, \ |
2313 | IsFP, IsBF) \ |
2314 | case BuiltinType::Id: \ |
2315 | Width = 0; \ |
2316 | Align = ElBits; \ |
2317 | break; |
2318 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, ElKind) \ |
2319 | case BuiltinType::Id: \ |
2320 | Width = 0; \ |
2321 | Align = 8; \ |
2322 | break; |
2323 | #include "clang/Basic/RISCVVTypes.def" |
2324 | #define WASM_TYPE(Name, Id, SingletonId) \ |
2325 | case BuiltinType::Id: \ |
2326 | Width = 0; \ |
2327 | Align = 8; \ |
2328 | break; |
2329 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
2330 | #define AMDGPU_TYPE(NAME, ID, SINGLETONID, WIDTH, ALIGN) \ |
2331 | case BuiltinType::ID: \ |
2332 | Width = WIDTH; \ |
2333 | Align = ALIGN; \ |
2334 | break; |
2335 | #include "clang/Basic/AMDGPUTypes.def" |
2336 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
2337 | #include "clang/Basic/HLSLIntangibleTypes.def" |
2338 | Width = Target->getPointerWidth(AddrSpace: LangAS::Default); |
2339 | Align = Target->getPointerAlign(AddrSpace: LangAS::Default); |
2340 | break; |
2341 | } |
2342 | break; |
2343 | case Type::ObjCObjectPointer: |
2344 | Width = Target->getPointerWidth(AddrSpace: LangAS::Default); |
2345 | Align = Target->getPointerAlign(AddrSpace: LangAS::Default); |
2346 | break; |
2347 | case Type::BlockPointer: |
2348 | AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
2349 | Width = Target->getPointerWidth(AddrSpace: AS); |
2350 | Align = Target->getPointerAlign(AddrSpace: AS); |
2351 | break; |
2352 | case Type::LValueReference: |
2353 | case Type::RValueReference: |
2354 | // alignof and sizeof should never enter this code path here, so we go |
2355 | // the pointer route. |
2356 | AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
2357 | Width = Target->getPointerWidth(AddrSpace: AS); |
2358 | Align = Target->getPointerAlign(AddrSpace: AS); |
2359 | break; |
2360 | case Type::Pointer: |
2361 | AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
2362 | Width = Target->getPointerWidth(AddrSpace: AS); |
2363 | Align = Target->getPointerAlign(AddrSpace: AS); |
2364 | break; |
2365 | case Type::MemberPointer: { |
2366 | const auto *MPT = cast<MemberPointerType>(T); |
2367 | CXXABI::MemberPointerInfo MPI = ABI->getMemberPointerInfo(MPT); |
2368 | Width = MPI.Width; |
2369 | Align = MPI.Align; |
2370 | break; |
2371 | } |
2372 | case Type::Complex: { |
2373 | // Complex types have the same alignment as their elements, but twice the |
2374 | // size. |
2375 | TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType()); |
2376 | Width = EltInfo.Width * 2; |
2377 | Align = EltInfo.Align; |
2378 | break; |
2379 | } |
2380 | case Type::ObjCObject: |
2381 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
2382 | case Type::Adjusted: |
2383 | case Type::Decayed: |
2384 | return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr()); |
2385 | case Type::ObjCInterface: { |
2386 | const auto *ObjCI = cast<ObjCInterfaceType>(T); |
2387 | if (ObjCI->getDecl()->isInvalidDecl()) { |
2388 | Width = 8; |
2389 | Align = 8; |
2390 | break; |
2391 | } |
2392 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(D: ObjCI->getDecl()); |
2393 | Width = toBits(CharSize: Layout.getSize()); |
2394 | Align = toBits(CharSize: Layout.getAlignment()); |
2395 | break; |
2396 | } |
2397 | case Type::BitInt: { |
2398 | const auto *EIT = cast<BitIntType>(T); |
2399 | Align = Target->getBitIntAlign(NumBits: EIT->getNumBits()); |
2400 | Width = Target->getBitIntWidth(NumBits: EIT->getNumBits()); |
2401 | break; |
2402 | } |
2403 | case Type::Record: |
2404 | case Type::Enum: { |
2405 | const auto *TT = cast<TagType>(T); |
2406 | |
2407 | if (TT->getDecl()->isInvalidDecl()) { |
2408 | Width = 8; |
2409 | Align = 8; |
2410 | break; |
2411 | } |
2412 | |
2413 | if (const auto *ET = dyn_cast<EnumType>(TT)) { |
2414 | const EnumDecl *ED = ET->getDecl(); |
2415 | TypeInfo Info = |
2416 | getTypeInfo(T: ED->getIntegerType()->getUnqualifiedDesugaredType()); |
2417 | if (unsigned AttrAlign = ED->getMaxAlignment()) { |
2418 | Info.Align = AttrAlign; |
2419 | Info.AlignRequirement = AlignRequirementKind::RequiredByEnum; |
2420 | } |
2421 | return Info; |
2422 | } |
2423 | |
2424 | const auto *RT = cast<RecordType>(TT); |
2425 | const RecordDecl *RD = RT->getDecl(); |
2426 | const ASTRecordLayout &Layout = getASTRecordLayout(D: RD); |
2427 | Width = toBits(CharSize: Layout.getSize()); |
2428 | Align = toBits(CharSize: Layout.getAlignment()); |
2429 | AlignRequirement = RD->hasAttr<AlignedAttr>() |
2430 | ? AlignRequirementKind::RequiredByRecord |
2431 | : AlignRequirementKind::None; |
2432 | break; |
2433 | } |
2434 | |
2435 | case Type::SubstTemplateTypeParm: |
2436 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
2437 | getReplacementType().getTypePtr()); |
2438 | |
2439 | case Type::Auto: |
2440 | case Type::DeducedTemplateSpecialization: { |
2441 | const auto *A = cast<DeducedType>(T); |
2442 | assert(!A->getDeducedType().isNull() && |
2443 | "cannot request the size of an undeduced or dependent auto type"); |
2444 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
2445 | } |
2446 | |
2447 | case Type::Paren: |
2448 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
2449 | |
2450 | case Type::MacroQualified: |
2451 | return getTypeInfo( |
2452 | cast<MacroQualifiedType>(T)->getUnderlyingType().getTypePtr()); |
2453 | |
2454 | case Type::ObjCTypeParam: |
2455 | return getTypeInfo(cast<ObjCTypeParamType>(T)->desugar().getTypePtr()); |
2456 | |
2457 | case Type::Using: |
2458 | return getTypeInfo(cast<UsingType>(T)->desugar().getTypePtr()); |
2459 | |
2460 | case Type::Typedef: { |
2461 | const auto *TT = cast<TypedefType>(T); |
2462 | TypeInfo Info = getTypeInfo(TT->desugar().getTypePtr()); |
2463 | // If the typedef has an aligned attribute on it, it overrides any computed |
2464 | // alignment we have. This violates the GCC documentation (which says that |
2465 | // attribute(aligned) can only round up) but matches its implementation. |
2466 | if (unsigned AttrAlign = TT->getDecl()->getMaxAlignment()) { |
2467 | Align = AttrAlign; |
2468 | AlignRequirement = AlignRequirementKind::RequiredByTypedef; |
2469 | } else { |
2470 | Align = Info.Align; |
2471 | AlignRequirement = Info.AlignRequirement; |
2472 | } |
2473 | Width = Info.Width; |
2474 | break; |
2475 | } |
2476 | |
2477 | case Type::Elaborated: |
2478 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
2479 | |
2480 | case Type::Attributed: |
2481 | return getTypeInfo( |
2482 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
2483 | |
2484 | case Type::CountAttributed: |
2485 | return getTypeInfo(cast<CountAttributedType>(T)->desugar().getTypePtr()); |
2486 | |
2487 | case Type::BTFTagAttributed: |
2488 | return getTypeInfo( |
2489 | cast<BTFTagAttributedType>(T)->getWrappedType().getTypePtr()); |
2490 | |
2491 | case Type::HLSLAttributedResource: |
2492 | return getTypeInfo( |
2493 | cast<HLSLAttributedResourceType>(T)->getWrappedType().getTypePtr()); |
2494 | |
2495 | case Type::HLSLInlineSpirv: { |
2496 | const auto *ST = cast<HLSLInlineSpirvType>(T); |
2497 | // Size is specified in bytes, convert to bits |
2498 | Width = ST->getSize() * 8; |
2499 | Align = ST->getAlignment(); |
2500 | if (Width == 0 && Align == 0) { |
2501 | // We are defaulting to laying out opaque SPIR-V types as 32-bit ints. |
2502 | Width = 32; |
2503 | Align = 32; |
2504 | } |
2505 | break; |
2506 | } |
2507 | |
2508 | case Type::Atomic: { |
2509 | // Start with the base type information. |
2510 | TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
2511 | Width = Info.Width; |
2512 | Align = Info.Align; |
2513 | |
2514 | if (!Width) { |
2515 | // An otherwise zero-sized type should still generate an |
2516 | // atomic operation. |
2517 | Width = Target->getCharWidth(); |
2518 | assert(Align); |
2519 | } else if (Width <= Target->getMaxAtomicPromoteWidth()) { |
2520 | // If the size of the type doesn't exceed the platform's max |
2521 | // atomic promotion width, make the size and alignment more |
2522 | // favorable to atomic operations: |
2523 | |
2524 | // Round the size up to a power of 2. |
2525 | Width = llvm::bit_ceil(Width); |
2526 | |
2527 | // Set the alignment equal to the size. |
2528 | Align = static_cast<unsigned>(Width); |
2529 | } |
2530 | } |
2531 | break; |
2532 | |
2533 | case Type::Pipe: |
2534 | Width = Target->getPointerWidth(AddrSpace: LangAS::opencl_global); |
2535 | Align = Target->getPointerAlign(AddrSpace: LangAS::opencl_global); |
2536 | break; |
2537 | } |
2538 | |
2539 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
2540 | return TypeInfo(Width, Align, AlignRequirement); |
2541 | } |
2542 | |
2543 | unsigned ASTContext::getTypeUnadjustedAlign(const Type *T) const { |
2544 | UnadjustedAlignMap::iterator I = MemoizedUnadjustedAlign.find(Val: T); |
2545 | if (I != MemoizedUnadjustedAlign.end()) |
2546 | return I->second; |
2547 | |
2548 | unsigned UnadjustedAlign; |
2549 | if (const auto *RT = T->getAs<RecordType>()) { |
2550 | const RecordDecl *RD = RT->getDecl(); |
2551 | const ASTRecordLayout &Layout = getASTRecordLayout(D: RD); |
2552 | UnadjustedAlign = toBits(CharSize: Layout.getUnadjustedAlignment()); |
2553 | } else if (const auto *ObjCI = T->getAs<ObjCInterfaceType>()) { |
2554 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(D: ObjCI->getDecl()); |
2555 | UnadjustedAlign = toBits(CharSize: Layout.getUnadjustedAlignment()); |
2556 | } else { |
2557 | UnadjustedAlign = getTypeAlign(T: T->getUnqualifiedDesugaredType()); |
2558 | } |
2559 | |
2560 | MemoizedUnadjustedAlign[T] = UnadjustedAlign; |
2561 | return UnadjustedAlign; |
2562 | } |
2563 | |
2564 | unsigned ASTContext::getOpenMPDefaultSimdAlign(QualType T) const { |
2565 | unsigned SimdAlign = llvm::OpenMPIRBuilder::getOpenMPDefaultSimdAlign( |
2566 | TargetTriple: getTargetInfo().getTriple(), Features: Target->getTargetOpts().FeatureMap); |
2567 | return SimdAlign; |
2568 | } |
2569 | |
2570 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
2571 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
2572 | return CharUnits::fromQuantity(Quantity: BitSize / getCharWidth()); |
2573 | } |
2574 | |
2575 | /// toBits - Convert a size in characters to a size in characters. |
2576 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
2577 | return CharSize.getQuantity() * getCharWidth(); |
2578 | } |
2579 | |
2580 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
2581 | /// This method does not work on incomplete types. |
2582 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
2583 | return getTypeInfoInChars(T).Width; |
2584 | } |
2585 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
2586 | return getTypeInfoInChars(T).Width; |
2587 | } |
2588 | |
2589 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
2590 | /// characters. This method does not work on incomplete types. |
2591 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
2592 | return toCharUnitsFromBits(BitSize: getTypeAlign(T)); |
2593 | } |
2594 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
2595 | return toCharUnitsFromBits(BitSize: getTypeAlign(T)); |
2596 | } |
2597 | |
2598 | /// getTypeUnadjustedAlignInChars - Return the ABI-specified alignment of a |
2599 | /// type, in characters, before alignment adjustments. This method does |
2600 | /// not work on incomplete types. |
2601 | CharUnits ASTContext::getTypeUnadjustedAlignInChars(QualType T) const { |
2602 | return toCharUnitsFromBits(BitSize: getTypeUnadjustedAlign(T)); |
2603 | } |
2604 | CharUnits ASTContext::getTypeUnadjustedAlignInChars(const Type *T) const { |
2605 | return toCharUnitsFromBits(BitSize: getTypeUnadjustedAlign(T)); |
2606 | } |
2607 | |
2608 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
2609 | /// type for the current target in bits. This can be different than the ABI |
2610 | /// alignment in cases where it is beneficial for performance or backwards |
2611 | /// compatibility preserving to overalign a data type. (Note: despite the name, |
2612 | /// the preferred alignment is ABI-impacting, and not an optimization.) |
2613 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
2614 | TypeInfo TI = getTypeInfo(T); |
2615 | unsigned ABIAlign = TI.Align; |
2616 | |
2617 | T = T->getBaseElementTypeUnsafe(); |
2618 | |
2619 | // The preferred alignment of member pointers is that of a pointer. |
2620 | if (T->isMemberPointerType()) |
2621 | return getPreferredTypeAlign(T: getPointerDiffType().getTypePtr()); |
2622 | |
2623 | if (!Target->allowsLargerPreferedTypeAlignment()) |
2624 | return ABIAlign; |
2625 | |
2626 | if (const auto *RT = T->getAs<RecordType>()) { |
2627 | const RecordDecl *RD = RT->getDecl(); |
2628 | |
2629 | // When used as part of a typedef, or together with a 'packed' attribute, |
2630 | // the 'aligned' attribute can be used to decrease alignment. Note that the |
2631 | // 'packed' case is already taken into consideration when computing the |
2632 | // alignment, we only need to handle the typedef case here. |
2633 | if (TI.AlignRequirement == AlignRequirementKind::RequiredByTypedef || |
2634 | RD->isInvalidDecl()) |
2635 | return ABIAlign; |
2636 | |
2637 | unsigned PreferredAlign = static_cast<unsigned>( |
2638 | toBits(CharSize: getASTRecordLayout(D: RD).PreferredAlignment)); |
2639 | assert(PreferredAlign >= ABIAlign && |
2640 | "PreferredAlign should be at least as large as ABIAlign."); |
2641 | return PreferredAlign; |
2642 | } |
2643 | |
2644 | // Double (and, for targets supporting AIX `power` alignment, long double) and |
2645 | // long long should be naturally aligned (despite requiring less alignment) if |
2646 | // possible. |
2647 | if (const auto *CT = T->getAs<ComplexType>()) |
2648 | T = CT->getElementType().getTypePtr(); |
2649 | if (const auto *ET = T->getAs<EnumType>()) |
2650 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
2651 | if (T->isSpecificBuiltinType(K: BuiltinType::Double) || |
2652 | T->isSpecificBuiltinType(K: BuiltinType::LongLong) || |
2653 | T->isSpecificBuiltinType(K: BuiltinType::ULongLong) || |
2654 | (T->isSpecificBuiltinType(K: BuiltinType::LongDouble) && |
2655 | Target->defaultsToAIXPowerAlignment())) |
2656 | // Don't increase the alignment if an alignment attribute was specified on a |
2657 | // typedef declaration. |
2658 | if (!TI.isAlignRequired()) |
2659 | return std::max(a: ABIAlign, b: (unsigned)getTypeSize(T)); |
2660 | |
2661 | return ABIAlign; |
2662 | } |
2663 | |
2664 | /// getTargetDefaultAlignForAttributeAligned - Return the default alignment |
2665 | /// for __attribute__((aligned)) on this target, to be used if no alignment |
2666 | /// value is specified. |
2667 | unsigned ASTContext::getTargetDefaultAlignForAttributeAligned() const { |
2668 | return getTargetInfo().getDefaultAlignForAttributeAligned(); |
2669 | } |
2670 | |
2671 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
2672 | /// to a global variable of the specified type. |
2673 | unsigned ASTContext::getAlignOfGlobalVar(QualType T, const VarDecl *VD) const { |
2674 | uint64_t TypeSize = getTypeSize(T: T.getTypePtr()); |
2675 | return std::max(a: getPreferredTypeAlign(T), |
2676 | b: getMinGlobalAlignOfVar(Size: TypeSize, VD)); |
2677 | } |
2678 | |
2679 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
2680 | /// should be given to a global variable of the specified type. |
2681 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T, |
2682 | const VarDecl *VD) const { |
2683 | return toCharUnitsFromBits(BitSize: getAlignOfGlobalVar(T, VD)); |
2684 | } |
2685 | |
2686 | unsigned ASTContext::getMinGlobalAlignOfVar(uint64_t Size, |
2687 | const VarDecl *VD) const { |
2688 | // Make the default handling as that of a non-weak definition in the |
2689 | // current translation unit. |
2690 | bool HasNonWeakDef = !VD || (VD->hasDefinition() && !VD->isWeak()); |
2691 | return getTargetInfo().getMinGlobalAlign(Size, HasNonWeakDef); |
2692 | } |
2693 | |
2694 | CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const { |
2695 | CharUnits Offset = CharUnits::Zero(); |
2696 | const ASTRecordLayout *Layout = &getASTRecordLayout(RD); |
2697 | while (const CXXRecordDecl *Base = Layout->getBaseSharingVBPtr()) { |
2698 | Offset += Layout->getBaseClassOffset(Base); |
2699 | Layout = &getASTRecordLayout(Base); |
2700 | } |
2701 | return Offset; |
2702 | } |
2703 | |
2704 | CharUnits ASTContext::getMemberPointerPathAdjustment(const APValue &MP) const { |
2705 | const ValueDecl *MPD = MP.getMemberPointerDecl(); |
2706 | CharUnits ThisAdjustment = CharUnits::Zero(); |
2707 | ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath(); |
2708 | bool DerivedMember = MP.isMemberPointerToDerivedMember(); |
2709 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext()); |
2710 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
2711 | const CXXRecordDecl *Base = RD; |
2712 | const CXXRecordDecl *Derived = Path[I]; |
2713 | if (DerivedMember) |
2714 | std::swap(a&: Base, b&: Derived); |
2715 | ThisAdjustment += getASTRecordLayout(Derived).getBaseClassOffset(Base); |
2716 | RD = Path[I]; |
2717 | } |
2718 | if (DerivedMember) |
2719 | ThisAdjustment = -ThisAdjustment; |
2720 | return ThisAdjustment; |
2721 | } |
2722 | |
2723 | /// DeepCollectObjCIvars - |
2724 | /// This routine first collects all declared, but not synthesized, ivars in |
2725 | /// super class and then collects all ivars, including those synthesized for |
2726 | /// current class. This routine is used for implementation of current class |
2727 | /// when all ivars, declared and synthesized are known. |
2728 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
2729 | bool leafClass, |
2730 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
2731 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
2732 | DeepCollectObjCIvars(OI: SuperClass, leafClass: false, Ivars); |
2733 | if (!leafClass) { |
2734 | llvm::append_range(C&: Ivars, R: OI->ivars()); |
2735 | } else { |
2736 | auto *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
2737 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
2738 | Iv= Iv->getNextIvar()) |
2739 | Ivars.push_back(Elt: Iv); |
2740 | } |
2741 | } |
2742 | |
2743 | /// CollectInheritedProtocols - Collect all protocols in current class and |
2744 | /// those inherited by it. |
2745 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
2746 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
2747 | if (const auto *OI = dyn_cast<ObjCInterfaceDecl>(Val: CDecl)) { |
2748 | // We can use protocol_iterator here instead of |
2749 | // all_referenced_protocol_iterator since we are walking all categories. |
2750 | for (auto *Proto : OI->all_referenced_protocols()) { |
2751 | CollectInheritedProtocols(Proto, Protocols); |
2752 | } |
2753 | |
2754 | // Categories of this Interface. |
2755 | for (const auto *Cat : OI->visible_categories()) |
2756 | CollectInheritedProtocols(Cat, Protocols); |
2757 | |
2758 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
2759 | while (SD) { |
2760 | CollectInheritedProtocols(SD, Protocols); |
2761 | SD = SD->getSuperClass(); |
2762 | } |
2763 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(Val: CDecl)) { |
2764 | for (auto *Proto : OC->protocols()) { |
2765 | CollectInheritedProtocols(Proto, Protocols); |
2766 | } |
2767 | } else if (const auto *OP = dyn_cast<ObjCProtocolDecl>(Val: CDecl)) { |
2768 | // Insert the protocol. |
2769 | if (!Protocols.insert( |
2770 | Ptr: const_cast<ObjCProtocolDecl *>(OP->getCanonicalDecl())).second) |
2771 | return; |
2772 | |
2773 | for (auto *Proto : OP->protocols()) |
2774 | CollectInheritedProtocols(Proto, Protocols); |
2775 | } |
2776 | } |
2777 | |
2778 | static bool unionHasUniqueObjectRepresentations(const ASTContext &Context, |
2779 | const RecordDecl *RD, |
2780 | bool CheckIfTriviallyCopyable) { |
2781 | assert(RD->isUnion() && "Must be union type"); |
2782 | CharUnits UnionSize = Context.getTypeSizeInChars(RD->getTypeForDecl()); |
2783 | |
2784 | for (const auto *Field : RD->fields()) { |
2785 | if (!Context.hasUniqueObjectRepresentations(Ty: Field->getType(), |
2786 | CheckIfTriviallyCopyable)) |
2787 | return false; |
2788 | CharUnits FieldSize = Context.getTypeSizeInChars(Field->getType()); |
2789 | if (FieldSize != UnionSize) |
2790 | return false; |
2791 | } |
2792 | return !RD->field_empty(); |
2793 | } |
2794 | |
2795 | static int64_t getSubobjectOffset(const FieldDecl *Field, |
2796 | const ASTContext &Context, |
2797 | const clang::ASTRecordLayout & /*Layout*/) { |
2798 | return Context.getFieldOffset(Field); |
2799 | } |
2800 | |
2801 | static int64_t getSubobjectOffset(const CXXRecordDecl *RD, |
2802 | const ASTContext &Context, |
2803 | const clang::ASTRecordLayout &Layout) { |
2804 | return Context.toBits(CharSize: Layout.getBaseClassOffset(Base: RD)); |
2805 | } |
2806 | |
2807 | static std::optional<int64_t> |
2808 | structHasUniqueObjectRepresentations(const ASTContext &Context, |
2809 | const RecordDecl *RD, |
2810 | bool CheckIfTriviallyCopyable); |
2811 | |
2812 | static std::optional<int64_t> |
2813 | getSubobjectSizeInBits(const FieldDecl *Field, const ASTContext &Context, |
2814 | bool CheckIfTriviallyCopyable) { |
2815 | if (Field->getType()->isRecordType()) { |
2816 | const RecordDecl *RD = Field->getType()->getAsRecordDecl(); |
2817 | if (!RD->isUnion()) |
2818 | return structHasUniqueObjectRepresentations(Context, RD, |
2819 | CheckIfTriviallyCopyable); |
2820 | } |
2821 | |
2822 | // A _BitInt type may not be unique if it has padding bits |
2823 | // but if it is a bitfield the padding bits are not used. |
2824 | bool IsBitIntType = Field->getType()->isBitIntType(); |
2825 | if (!Field->getType()->isReferenceType() && !IsBitIntType && |
2826 | !Context.hasUniqueObjectRepresentations(Ty: Field->getType(), |
2827 | CheckIfTriviallyCopyable)) |
2828 | return std::nullopt; |
2829 | |
2830 | int64_t FieldSizeInBits = |
2831 | Context.toBits(CharSize: Context.getTypeSizeInChars(Field->getType())); |
2832 | if (Field->isBitField()) { |
2833 | // If we have explicit padding bits, they don't contribute bits |
2834 | // to the actual object representation, so return 0. |
2835 | if (Field->isUnnamedBitField()) |
2836 | return 0; |
2837 | |
2838 | int64_t BitfieldSize = Field->getBitWidthValue(); |
2839 | if (IsBitIntType) { |
2840 | if ((unsigned)BitfieldSize > |
2841 | cast<BitIntType>(Field->getType())->getNumBits()) |
2842 | return std::nullopt; |
2843 | } else if (BitfieldSize > FieldSizeInBits) { |
2844 | return std::nullopt; |
2845 | } |
2846 | FieldSizeInBits = BitfieldSize; |
2847 | } else if (IsBitIntType && !Context.hasUniqueObjectRepresentations( |
2848 | Ty: Field->getType(), CheckIfTriviallyCopyable)) { |
2849 | return std::nullopt; |
2850 | } |
2851 | return FieldSizeInBits; |
2852 | } |
2853 | |
2854 | static std::optional<int64_t> |
2855 | getSubobjectSizeInBits(const CXXRecordDecl *RD, const ASTContext &Context, |
2856 | bool CheckIfTriviallyCopyable) { |
2857 | return structHasUniqueObjectRepresentations(Context, RD, |
2858 | CheckIfTriviallyCopyable); |
2859 | } |
2860 | |
2861 | template <typename RangeT> |
2862 | static std::optional<int64_t> structSubobjectsHaveUniqueObjectRepresentations( |
2863 | const RangeT &Subobjects, int64_t CurOffsetInBits, |
2864 | const ASTContext &Context, const clang::ASTRecordLayout &Layout, |
2865 | bool CheckIfTriviallyCopyable) { |
2866 | for (const auto *Subobject : Subobjects) { |
2867 | std::optional<int64_t> SizeInBits = |
2868 | getSubobjectSizeInBits(Subobject, Context, CheckIfTriviallyCopyable); |
2869 | if (!SizeInBits) |
2870 | return std::nullopt; |
2871 | if (*SizeInBits != 0) { |
2872 | int64_t Offset = getSubobjectOffset(Subobject, Context, Layout); |
2873 | if (Offset != CurOffsetInBits) |
2874 | return std::nullopt; |
2875 | CurOffsetInBits += *SizeInBits; |
2876 | } |
2877 | } |
2878 | return CurOffsetInBits; |
2879 | } |
2880 | |
2881 | static std::optional<int64_t> |
2882 | structHasUniqueObjectRepresentations(const ASTContext &Context, |
2883 | const RecordDecl *RD, |
2884 | bool CheckIfTriviallyCopyable) { |
2885 | assert(!RD->isUnion() && "Must be struct/class type"); |
2886 | const auto &Layout = Context.getASTRecordLayout(D: RD); |
2887 | |
2888 | int64_t CurOffsetInBits = 0; |
2889 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(Val: RD)) { |
2890 | if (ClassDecl->isDynamicClass()) |
2891 | return std::nullopt; |
2892 | |
2893 | SmallVector<CXXRecordDecl *, 4> Bases; |
2894 | for (const auto &Base : ClassDecl->bases()) { |
2895 | // Empty types can be inherited from, and non-empty types can potentially |
2896 | // have tail padding, so just make sure there isn't an error. |
2897 | Bases.emplace_back(Args: Base.getType()->getAsCXXRecordDecl()); |
2898 | } |
2899 | |
2900 | llvm::sort(C&: Bases, Comp: [&](const CXXRecordDecl *L, const CXXRecordDecl *R) { |
2901 | return Layout.getBaseClassOffset(Base: L) < Layout.getBaseClassOffset(Base: R); |
2902 | }); |
2903 | |
2904 | std::optional<int64_t> OffsetAfterBases = |
2905 | structSubobjectsHaveUniqueObjectRepresentations( |
2906 | Subobjects: Bases, CurOffsetInBits, Context, Layout, CheckIfTriviallyCopyable); |
2907 | if (!OffsetAfterBases) |
2908 | return std::nullopt; |
2909 | CurOffsetInBits = *OffsetAfterBases; |
2910 | } |
2911 | |
2912 | std::optional<int64_t> OffsetAfterFields = |
2913 | structSubobjectsHaveUniqueObjectRepresentations( |
2914 | Subobjects: RD->fields(), CurOffsetInBits, Context, Layout, |
2915 | CheckIfTriviallyCopyable); |
2916 | if (!OffsetAfterFields) |
2917 | return std::nullopt; |
2918 | CurOffsetInBits = *OffsetAfterFields; |
2919 | |
2920 | return CurOffsetInBits; |
2921 | } |
2922 | |
2923 | bool ASTContext::hasUniqueObjectRepresentations( |
2924 | QualType Ty, bool CheckIfTriviallyCopyable) const { |
2925 | // C++17 [meta.unary.prop]: |
2926 | // The predicate condition for a template specialization |
2927 | // has_unique_object_representations<T> shall be satisfied if and only if: |
2928 | // (9.1) - T is trivially copyable, and |
2929 | // (9.2) - any two objects of type T with the same value have the same |
2930 | // object representation, where: |
2931 | // - two objects of array or non-union class type are considered to have |
2932 | // the same value if their respective sequences of direct subobjects |
2933 | // have the same values, and |
2934 | // - two objects of union type are considered to have the same value if |
2935 | // they have the same active member and the corresponding members have |
2936 | // the same value. |
2937 | // The set of scalar types for which this condition holds is |
2938 | // implementation-defined. [ Note: If a type has padding bits, the condition |
2939 | // does not hold; otherwise, the condition holds true for unsigned integral |
2940 | // types. -- end note ] |
2941 | assert(!Ty.isNull() && "Null QualType sent to unique object rep check"); |
2942 | |
2943 | // Arrays are unique only if their element type is unique. |
2944 | if (Ty->isArrayType()) |
2945 | return hasUniqueObjectRepresentations(Ty: getBaseElementType(QT: Ty), |
2946 | CheckIfTriviallyCopyable); |
2947 | |
2948 | assert((Ty->isVoidType() || !Ty->isIncompleteType()) && |
2949 | "hasUniqueObjectRepresentations should not be called with an " |
2950 | "incomplete type"); |
2951 | |
2952 | // (9.1) - T is trivially copyable... |
2953 | if (CheckIfTriviallyCopyable && !Ty.isTriviallyCopyableType(Context: *this)) |
2954 | return false; |
2955 | |
2956 | // All integrals and enums are unique. |
2957 | if (Ty->isIntegralOrEnumerationType()) { |
2958 | // Address discriminated integer types are not unique. |
2959 | if (Ty.hasAddressDiscriminatedPointerAuth()) |
2960 | return false; |
2961 | // Except _BitInt types that have padding bits. |
2962 | if (const auto *BIT = Ty->getAs<BitIntType>()) |
2963 | return getTypeSize(BIT) == BIT->getNumBits(); |
2964 | |
2965 | return true; |
2966 | } |
2967 | |
2968 | // All other pointers (except __ptrauth pointers) are unique. |
2969 | if (Ty->isPointerType()) |
2970 | return !Ty.hasAddressDiscriminatedPointerAuth(); |
2971 | |
2972 | if (const auto *MPT = Ty->getAs<MemberPointerType>()) |
2973 | return !ABI->getMemberPointerInfo(MPT).HasPadding; |
2974 | |
2975 | if (Ty->isRecordType()) { |
2976 | const RecordDecl *Record = Ty->castAs<RecordType>()->getDecl(); |
2977 | |
2978 | if (Record->isInvalidDecl()) |
2979 | return false; |
2980 | |
2981 | if (Record->isUnion()) |
2982 | return unionHasUniqueObjectRepresentations(Context: *this, RD: Record, |
2983 | CheckIfTriviallyCopyable); |
2984 | |
2985 | std::optional<int64_t> StructSize = structHasUniqueObjectRepresentations( |
2986 | Context: *this, RD: Record, CheckIfTriviallyCopyable); |
2987 | |
2988 | return StructSize && *StructSize == static_cast<int64_t>(getTypeSize(T: Ty)); |
2989 | } |
2990 | |
2991 | // FIXME: More cases to handle here (list by rsmith): |
2992 | // vectors (careful about, eg, vector of 3 foo) |
2993 | // _Complex int and friends |
2994 | // _Atomic T |
2995 | // Obj-C block pointers |
2996 | // Obj-C object pointers |
2997 | // and perhaps OpenCL's various builtin types (pipe, sampler_t, event_t, |
2998 | // clk_event_t, queue_t, reserve_id_t) |
2999 | // There're also Obj-C class types and the Obj-C selector type, but I think it |
3000 | // makes sense for those to return false here. |
3001 | |
3002 | return false; |
3003 | } |
3004 | |
3005 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
3006 | unsigned count = 0; |
3007 | // Count ivars declared in class extension. |
3008 | for (const auto *Ext : OI->known_extensions()) |
3009 | count += Ext->ivar_size(); |
3010 | |
3011 | // Count ivar defined in this class's implementation. This |
3012 | // includes synthesized ivars. |
3013 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
3014 | count += ImplDecl->ivar_size(); |
3015 | |
3016 | return count; |
3017 | } |
3018 | |
3019 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
3020 | if (!E) |
3021 | return false; |
3022 | |
3023 | // nullptr_t is always treated as null. |
3024 | if (E->getType()->isNullPtrType()) return true; |
3025 | |
3026 | if (E->getType()->isAnyPointerType() && |
3027 | E->IgnoreParenCasts()->isNullPointerConstant(Ctx&: *this, |
3028 | NPC: Expr::NPC_ValueDependentIsNull)) |
3029 | return true; |
3030 | |
3031 | // Unfortunately, __null has type 'int'. |
3032 | if (isa<GNUNullExpr>(Val: E)) return true; |
3033 | |
3034 | return false; |
3035 | } |
3036 | |
3037 | /// Get the implementation of ObjCInterfaceDecl, or nullptr if none |
3038 | /// exists. |
3039 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
3040 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
3041 | I = ObjCImpls.find(D); |
3042 | if (I != ObjCImpls.end()) |
3043 | return cast<ObjCImplementationDecl>(Val: I->second); |
3044 | return nullptr; |
3045 | } |
3046 | |
3047 | /// Get the implementation of ObjCCategoryDecl, or nullptr if none |
3048 | /// exists. |
3049 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
3050 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
3051 | I = ObjCImpls.find(D); |
3052 | if (I != ObjCImpls.end()) |
3053 | return cast<ObjCCategoryImplDecl>(Val: I->second); |
3054 | return nullptr; |
3055 | } |
3056 | |
3057 | /// Set the implementation of ObjCInterfaceDecl. |
3058 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
3059 | ObjCImplementationDecl *ImplD) { |
3060 | assert(IFaceD && ImplD && "Passed null params"); |
3061 | ObjCImpls[IFaceD] = ImplD; |
3062 | } |
3063 | |
3064 | /// Set the implementation of ObjCCategoryDecl. |
3065 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
3066 | ObjCCategoryImplDecl *ImplD) { |
3067 | assert(CatD && ImplD && "Passed null params"); |
3068 | ObjCImpls[CatD] = ImplD; |
3069 | } |
3070 | |
3071 | const ObjCMethodDecl * |
3072 | ASTContext::getObjCMethodRedeclaration(const ObjCMethodDecl *MD) const { |
3073 | return ObjCMethodRedecls.lookup(Val: MD); |
3074 | } |
3075 | |
3076 | void ASTContext::setObjCMethodRedeclaration(const ObjCMethodDecl *MD, |
3077 | const ObjCMethodDecl *Redecl) { |
3078 | assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration"); |
3079 | ObjCMethodRedecls[MD] = Redecl; |
3080 | } |
3081 | |
3082 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
3083 | const NamedDecl *ND) const { |
3084 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
3085 | return ID; |
3086 | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
3087 | return CD->getClassInterface(); |
3088 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
3089 | return IMD->getClassInterface(); |
3090 | |
3091 | return nullptr; |
3092 | } |
3093 | |
3094 | /// Get the copy initialization expression of VarDecl, or nullptr if |
3095 | /// none exists. |
3096 | BlockVarCopyInit ASTContext::getBlockVarCopyInit(const VarDecl *VD) const { |
3097 | assert(VD && "Passed null params"); |
3098 | assert(VD->hasAttr<BlocksAttr>() && |
3099 | "getBlockVarCopyInits - not __block var"); |
3100 | auto I = BlockVarCopyInits.find(Val: VD); |
3101 | if (I != BlockVarCopyInits.end()) |
3102 | return I->second; |
3103 | return {nullptr, false}; |
3104 | } |
3105 | |
3106 | /// Set the copy initialization expression of a block var decl. |
3107 | void ASTContext::setBlockVarCopyInit(const VarDecl*VD, Expr *CopyExpr, |
3108 | bool CanThrow) { |
3109 | assert(VD && CopyExpr && "Passed null params"); |
3110 | assert(VD->hasAttr<BlocksAttr>() && |
3111 | "setBlockVarCopyInits - not __block var"); |
3112 | BlockVarCopyInits[VD].setExprAndFlag(CopyExpr, CanThrow); |
3113 | } |
3114 | |
3115 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
3116 | unsigned DataSize) const { |
3117 | if (!DataSize) |
3118 | DataSize = TypeLoc::getFullDataSizeForType(Ty: T); |
3119 | else |
3120 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
3121 | "incorrect data size provided to CreateTypeSourceInfo!"); |
3122 | |
3123 | auto *TInfo = |
3124 | (TypeSourceInfo*)BumpAlloc.Allocate(Size: sizeof(TypeSourceInfo) + DataSize, Alignment: 8); |
3125 | new (TInfo) TypeSourceInfo(T, DataSize); |
3126 | return TInfo; |
3127 | } |
3128 | |
3129 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
3130 | SourceLocation L) const { |
3131 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
3132 | DI->getTypeLoc().initialize(Context&: const_cast<ASTContext &>(*this), Loc: L); |
3133 | return DI; |
3134 | } |
3135 | |
3136 | const ASTRecordLayout & |
3137 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
3138 | return getObjCLayout(D); |
3139 | } |
3140 | |
3141 | static auto getCanonicalTemplateArguments(const ASTContext &C, |
3142 | ArrayRef<TemplateArgument> Args, |
3143 | bool &AnyNonCanonArgs) { |
3144 | SmallVector<TemplateArgument, 16> CanonArgs(Args); |
3145 | AnyNonCanonArgs |= C.canonicalizeTemplateArguments(Args: CanonArgs); |
3146 | return CanonArgs; |
3147 | } |
3148 | |
3149 | bool ASTContext::canonicalizeTemplateArguments( |
3150 | MutableArrayRef<TemplateArgument> Args) const { |
3151 | bool AnyNonCanonArgs = false; |
3152 | for (auto &Arg : Args) { |
3153 | TemplateArgument OrigArg = Arg; |
3154 | Arg = getCanonicalTemplateArgument(Arg); |
3155 | AnyNonCanonArgs |= !Arg.structurallyEquals(Other: OrigArg); |
3156 | } |
3157 | return AnyNonCanonArgs; |
3158 | } |
3159 | |
3160 | //===----------------------------------------------------------------------===// |
3161 | // Type creation/memoization methods |
3162 | //===----------------------------------------------------------------------===// |
3163 | |
3164 | QualType |
3165 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
3166 | unsigned fastQuals = quals.getFastQualifiers(); |
3167 | quals.removeFastQualifiers(); |
3168 | |
3169 | // Check if we've already instantiated this type. |
3170 | llvm::FoldingSetNodeID ID; |
3171 | ExtQuals::Profile(ID, BaseType: baseType, Quals: quals); |
3172 | void *insertPos = nullptr; |
3173 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos&: insertPos)) { |
3174 | assert(eq->getQualifiers() == quals); |
3175 | return QualType(eq, fastQuals); |
3176 | } |
3177 | |
3178 | // If the base type is not canonical, make the appropriate canonical type. |
3179 | QualType canon; |
3180 | if (!baseType->isCanonicalUnqualified()) { |
3181 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
3182 | canonSplit.Quals.addConsistentQualifiers(qs: quals); |
3183 | canon = getExtQualType(baseType: canonSplit.Ty, quals: canonSplit.Quals); |
3184 | |
3185 | // Re-find the insert position. |
3186 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos&: insertPos); |
3187 | } |
3188 | |
3189 | auto *eq = new (*this, alignof(ExtQuals)) ExtQuals(baseType, canon, quals); |
3190 | ExtQualNodes.InsertNode(N: eq, InsertPos: insertPos); |
3191 | return QualType(eq, fastQuals); |
3192 | } |
3193 | |
3194 | QualType ASTContext::getAddrSpaceQualType(QualType T, |
3195 | LangAS AddressSpace) const { |
3196 | QualType CanT = getCanonicalType(T); |
3197 | if (CanT.getAddressSpace() == AddressSpace) |
3198 | return T; |
3199 | |
3200 | // If we are composing extended qualifiers together, merge together |
3201 | // into one ExtQuals node. |
3202 | QualifierCollector Quals; |
3203 | const Type *TypeNode = Quals.strip(type: T); |
3204 | |
3205 | // If this type already has an address space specified, it cannot get |
3206 | // another one. |
3207 | assert(!Quals.hasAddressSpace() && |
3208 | "Type cannot be in multiple addr spaces!"); |
3209 | Quals.addAddressSpace(space: AddressSpace); |
3210 | |
3211 | return getExtQualType(baseType: TypeNode, quals: Quals); |
3212 | } |
3213 | |
3214 | QualType ASTContext::removeAddrSpaceQualType(QualType T) const { |
3215 | // If the type is not qualified with an address space, just return it |
3216 | // immediately. |
3217 | if (!T.hasAddressSpace()) |
3218 | return T; |
3219 | |
3220 | QualifierCollector Quals; |
3221 | const Type *TypeNode; |
3222 | // For arrays, strip the qualifier off the element type, then reconstruct the |
3223 | // array type |
3224 | if (T.getTypePtr()->isArrayType()) { |
3225 | T = getUnqualifiedArrayType(T, Quals); |
3226 | TypeNode = T.getTypePtr(); |
3227 | } else { |
3228 | // If we are composing extended qualifiers together, merge together |
3229 | // into one ExtQuals node. |
3230 | while (T.hasAddressSpace()) { |
3231 | TypeNode = Quals.strip(type: T); |
3232 | |
3233 | // If the type no longer has an address space after stripping qualifiers, |
3234 | // jump out. |
3235 | if (!QualType(TypeNode, 0).hasAddressSpace()) |
3236 | break; |
3237 | |
3238 | // There might be sugar in the way. Strip it and try again. |
3239 | T = T.getSingleStepDesugaredType(Context: *this); |
3240 | } |
3241 | } |
3242 | |
3243 | Quals.removeAddressSpace(); |
3244 | |
3245 | // Removal of the address space can mean there are no longer any |
3246 | // non-fast qualifiers, so creating an ExtQualType isn't possible (asserts) |
3247 | // or required. |
3248 | if (Quals.hasNonFastQualifiers()) |
3249 | return getExtQualType(baseType: TypeNode, quals: Quals); |
3250 | else |
3251 | return QualType(TypeNode, Quals.getFastQualifiers()); |
3252 | } |
3253 | |
3254 | uint16_t |
3255 | ASTContext::getPointerAuthVTablePointerDiscriminator(const CXXRecordDecl *RD) { |
3256 | assert(RD->isPolymorphic() && |
3257 | "Attempted to get vtable pointer discriminator on a monomorphic type"); |
3258 | std::unique_ptr<MangleContext> MC(createMangleContext()); |
3259 | SmallString<256> Str; |
3260 | llvm::raw_svector_ostream Out(Str); |
3261 | MC->mangleCXXVTable(RD, Out); |
3262 | return llvm::getPointerAuthStableSipHash(S: Str); |
3263 | } |
3264 | |
3265 | /// Encode a function type for use in the discriminator of a function pointer |
3266 | /// type. We can't use the itanium scheme for this since C has quite permissive |
3267 | /// rules for type compatibility that we need to be compatible with. |
3268 | /// |
3269 | /// Formally, this function associates every function pointer type T with an |
3270 | /// encoded string E(T). Let the equivalence relation T1 ~ T2 be defined as |
3271 | /// E(T1) == E(T2). E(T) is part of the ABI of values of type T. C type |
3272 | /// compatibility requires equivalent treatment under the ABI, so |
3273 | /// CCompatible(T1, T2) must imply E(T1) == E(T2), that is, CCompatible must be |
3274 | /// a subset of ~. Crucially, however, it must be a proper subset because |
3275 | /// CCompatible is not an equivalence relation: for example, int[] is compatible |
3276 | /// with both int[1] and int[2], but the latter are not compatible with each |
3277 | /// other. Therefore this encoding function must be careful to only distinguish |
3278 | /// types if there is no third type with which they are both required to be |
3279 | /// compatible. |
3280 | static void encodeTypeForFunctionPointerAuth(const ASTContext &Ctx, |
3281 | raw_ostream &OS, QualType QT) { |
3282 | // FIXME: Consider address space qualifiers. |
3283 | const Type *T = QT.getCanonicalType().getTypePtr(); |
3284 | |
3285 | // FIXME: Consider using the C++ type mangling when we encounter a construct |
3286 | // that is incompatible with C. |
3287 | |
3288 | switch (T->getTypeClass()) { |
3289 | case Type::Atomic: |
3290 | return encodeTypeForFunctionPointerAuth( |
3291 | Ctx, OS, QT: cast<AtomicType>(Val: T)->getValueType()); |
3292 | |
3293 | case Type::LValueReference: |
3294 | OS << "R"; |
3295 | encodeTypeForFunctionPointerAuth(Ctx, OS, |
3296 | QT: cast<ReferenceType>(Val: T)->getPointeeType()); |
3297 | return; |
3298 | case Type::RValueReference: |
3299 | OS << "O"; |
3300 | encodeTypeForFunctionPointerAuth(Ctx, OS, |
3301 | QT: cast<ReferenceType>(Val: T)->getPointeeType()); |
3302 | return; |
3303 | |
3304 | case Type::Pointer: |
3305 | // C11 6.7.6.1p2: |
3306 | // For two pointer types to be compatible, both shall be identically |
3307 | // qualified and both shall be pointers to compatible types. |
3308 | // FIXME: we should also consider pointee types. |
3309 | OS << "P"; |
3310 | return; |
3311 | |
3312 | case Type::ObjCObjectPointer: |
3313 | case Type::BlockPointer: |
3314 | OS << "P"; |
3315 | return; |
3316 | |
3317 | case Type::Complex: |
3318 | OS << "C"; |
3319 | return encodeTypeForFunctionPointerAuth( |
3320 | Ctx, OS, QT: cast<ComplexType>(Val: T)->getElementType()); |
3321 | |
3322 | case Type::VariableArray: |
3323 | case Type::ConstantArray: |
3324 | case Type::IncompleteArray: |
3325 | case Type::ArrayParameter: |
3326 | // C11 6.7.6.2p6: |
3327 | // For two array types to be compatible, both shall have compatible |
3328 | // element types, and if both size specifiers are present, and are integer |
3329 | // constant expressions, then both size specifiers shall have the same |
3330 | // constant value [...] |
3331 | // |
3332 | // So since ElemType[N] has to be compatible ElemType[], we can't encode the |
3333 | // width of the array. |
3334 | OS << "A"; |
3335 | return encodeTypeForFunctionPointerAuth( |
3336 | Ctx, OS, QT: cast<ArrayType>(Val: T)->getElementType()); |
3337 | |
3338 | case Type::ObjCInterface: |
3339 | case Type::ObjCObject: |
3340 | OS << "<objc_object>"; |
3341 | return; |
3342 | |
3343 | case Type::Enum: { |
3344 | // C11 6.7.2.2p4: |
3345 | // Each enumerated type shall be compatible with char, a signed integer |
3346 | // type, or an unsigned integer type. |
3347 | // |
3348 | // So we have to treat enum types as integers. |
3349 | QualType UnderlyingType = cast<EnumType>(Val: T)->getDecl()->getIntegerType(); |
3350 | return encodeTypeForFunctionPointerAuth( |
3351 | Ctx, OS, UnderlyingType.isNull() ? Ctx.IntTy : UnderlyingType); |
3352 | } |
3353 | |
3354 | case Type::FunctionNoProto: |
3355 | case Type::FunctionProto: { |
3356 | // C11 6.7.6.3p15: |
3357 | // For two function types to be compatible, both shall specify compatible |
3358 | // return types. Moreover, the parameter type lists, if both are present, |
3359 | // shall agree in the number of parameters and in the use of the ellipsis |
3360 | // terminator; corresponding parameters shall have compatible types. |
3361 | // |
3362 | // That paragraph goes on to describe how unprototyped functions are to be |
3363 | // handled, which we ignore here. Unprototyped function pointers are hashed |
3364 | // as though they were prototyped nullary functions since thats probably |
3365 | // what the user meant. This behavior is non-conforming. |
3366 | // FIXME: If we add a "custom discriminator" function type attribute we |
3367 | // should encode functions as their discriminators. |
3368 | OS << "F"; |
3369 | const auto *FuncType = cast<FunctionType>(Val: T); |
3370 | encodeTypeForFunctionPointerAuth(Ctx, OS, QT: FuncType->getReturnType()); |
3371 | if (const auto *FPT = dyn_cast<FunctionProtoType>(Val: FuncType)) { |
3372 | for (QualType Param : FPT->param_types()) { |
3373 | Param = Ctx.getSignatureParameterType(T: Param); |
3374 | encodeTypeForFunctionPointerAuth(Ctx, OS, QT: Param); |
3375 | } |
3376 | if (FPT->isVariadic()) |
3377 | OS << "z"; |
3378 | } |
3379 | OS << "E"; |
3380 | return; |
3381 | } |
3382 | |
3383 | case Type::MemberPointer: { |
3384 | OS << "M"; |
3385 | const auto *MPT = T->castAs<MemberPointerType>(); |
3386 | encodeTypeForFunctionPointerAuth( |
3387 | Ctx, OS, QT: QualType(MPT->getQualifier()->getAsType(), 0)); |
3388 | encodeTypeForFunctionPointerAuth(Ctx, OS, QT: MPT->getPointeeType()); |
3389 | return; |
3390 | } |
3391 | case Type::ExtVector: |
3392 | case Type::Vector: |
3393 | OS << "Dv"<< Ctx.getTypeSizeInChars(T).getQuantity(); |
3394 | break; |
3395 | |
3396 | // Don't bother discriminating based on these types. |
3397 | case Type::Pipe: |
3398 | case Type::BitInt: |
3399 | case Type::ConstantMatrix: |
3400 | OS << "?"; |
3401 | return; |
3402 | |
3403 | case Type::Builtin: { |
3404 | const auto *BTy = T->castAs<BuiltinType>(); |
3405 | switch (BTy->getKind()) { |
3406 | #define SIGNED_TYPE(Id, SingletonId) \ |
3407 | case BuiltinType::Id: \ |
3408 | OS << "i"; \ |
3409 | return; |
3410 | #define UNSIGNED_TYPE(Id, SingletonId) \ |
3411 | case BuiltinType::Id: \ |
3412 | OS << "i"; \ |
3413 | return; |
3414 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
3415 | #define BUILTIN_TYPE(Id, SingletonId) |
3416 | #include "clang/AST/BuiltinTypes.def" |
3417 | llvm_unreachable("placeholder types should not appear here."); |
3418 | |
3419 | case BuiltinType::Half: |
3420 | OS << "Dh"; |
3421 | return; |
3422 | case BuiltinType::Float: |
3423 | OS << "f"; |
3424 | return; |
3425 | case BuiltinType::Double: |
3426 | OS << "d"; |
3427 | return; |
3428 | case BuiltinType::LongDouble: |
3429 | OS << "e"; |
3430 | return; |
3431 | case BuiltinType::Float16: |
3432 | OS << "DF16_"; |
3433 | return; |
3434 | case BuiltinType::Float128: |
3435 | OS << "g"; |
3436 | return; |
3437 | |
3438 | case BuiltinType::Void: |
3439 | OS << "v"; |
3440 | return; |
3441 | |
3442 | case BuiltinType::ObjCId: |
3443 | case BuiltinType::ObjCClass: |
3444 | case BuiltinType::ObjCSel: |
3445 | case BuiltinType::NullPtr: |
3446 | OS << "P"; |
3447 | return; |
3448 | |
3449 | // Don't bother discriminating based on OpenCL types. |
3450 | case BuiltinType::OCLSampler: |
3451 | case BuiltinType::OCLEvent: |
3452 | case BuiltinType::OCLClkEvent: |
3453 | case BuiltinType::OCLQueue: |
3454 | case BuiltinType::OCLReserveID: |
3455 | case BuiltinType::BFloat16: |
3456 | case BuiltinType::VectorQuad: |
3457 | case BuiltinType::VectorPair: |
3458 | OS << "?"; |
3459 | return; |
3460 | |
3461 | // Don't bother discriminating based on these seldom-used types. |
3462 | case BuiltinType::Ibm128: |
3463 | return; |
3464 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
3465 | case BuiltinType::Id: \ |
3466 | return; |
3467 | #include "clang/Basic/OpenCLImageTypes.def" |
3468 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
3469 | case BuiltinType::Id: \ |
3470 | return; |
3471 | #include "clang/Basic/OpenCLExtensionTypes.def" |
3472 | #define SVE_TYPE(Name, Id, SingletonId) \ |
3473 | case BuiltinType::Id: \ |
3474 | return; |
3475 | #include "clang/Basic/AArch64ACLETypes.def" |
3476 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \ |
3477 | case BuiltinType::Id: \ |
3478 | return; |
3479 | #include "clang/Basic/HLSLIntangibleTypes.def" |
3480 | case BuiltinType::Dependent: |
3481 | llvm_unreachable("should never get here"); |
3482 | #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id: |
3483 | #include "clang/Basic/AMDGPUTypes.def" |
3484 | case BuiltinType::WasmExternRef: |
3485 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
3486 | #include "clang/Basic/RISCVVTypes.def" |
3487 | llvm_unreachable("not yet implemented"); |
3488 | } |
3489 | llvm_unreachable("should never get here"); |
3490 | } |
3491 | case Type::Record: { |
3492 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
3493 | const IdentifierInfo *II = RD->getIdentifier(); |
3494 | |
3495 | // In C++, an immediate typedef of an anonymous struct or union |
3496 | // is considered to name it for ODR purposes, but C's specification |
3497 | // of type compatibility does not have a similar rule. Using the typedef |
3498 | // name in function type discriminators anyway, as we do here, |
3499 | // therefore technically violates the C standard: two function pointer |
3500 | // types defined in terms of two typedef'd anonymous structs with |
3501 | // different names are formally still compatible, but we are assigning |
3502 | // them different discriminators and therefore incompatible ABIs. |
3503 | // |
3504 | // This is a relatively minor violation that significantly improves |
3505 | // discrimination in some cases and has not caused problems in |
3506 | // practice. Regardless, it is now part of the ABI in places where |
3507 | // function type discrimination is used, and it can no longer be |
3508 | // changed except on new platforms. |
3509 | |
3510 | if (!II) |
3511 | if (const TypedefNameDecl *Typedef = RD->getTypedefNameForAnonDecl()) |
3512 | II = Typedef->getDeclName().getAsIdentifierInfo(); |
3513 | |
3514 | if (!II) { |
3515 | OS << "<anonymous_record>"; |
3516 | return; |
3517 | } |
3518 | OS << II->getLength() << II->getName(); |
3519 | return; |
3520 | } |
3521 | case Type::HLSLAttributedResource: |
3522 | case Type::HLSLInlineSpirv: |
3523 | llvm_unreachable("should never get here"); |
3524 | break; |
3525 | case Type::DeducedTemplateSpecialization: |
3526 | case Type::Auto: |
3527 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
3528 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
3529 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
3530 | #define ABSTRACT_TYPE(Class, Base) |
3531 | #define TYPE(Class, Base) |
3532 | #include "clang/AST/TypeNodes.inc" |
3533 | llvm_unreachable("unexpected non-canonical or dependent type!"); |
3534 | return; |
3535 | } |
3536 | } |
3537 | |
3538 | uint16_t ASTContext::getPointerAuthTypeDiscriminator(QualType T) { |
3539 | assert(!T->isDependentType() && |
3540 | "cannot compute type discriminator of a dependent type"); |
3541 | |
3542 | SmallString<256> Str; |
3543 | llvm::raw_svector_ostream Out(Str); |
3544 | |
3545 | if (T->isFunctionPointerType() || T->isFunctionReferenceType()) |
3546 | T = T->getPointeeType(); |
3547 | |
3548 | if (T->isFunctionType()) { |
3549 | encodeTypeForFunctionPointerAuth(Ctx: *this, OS&: Out, QT: T); |
3550 | } else { |
3551 | T = T.getUnqualifiedType(); |
3552 | // Calls to member function pointers don't need to worry about |
3553 | // language interop or the laxness of the C type compatibility rules. |
3554 | // We just mangle the member pointer type directly, which is |
3555 | // implicitly much stricter about type matching. However, we do |
3556 | // strip any top-level exception specification before this mangling. |
3557 | // C++23 requires calls to work when the function type is convertible |
3558 | // to the pointer type by a function pointer conversion, which can |
3559 | // change the exception specification. This does not technically |
3560 | // require the exception specification to not affect representation, |
3561 | // because the function pointer conversion is still always a direct |
3562 | // value conversion and therefore an opportunity to resign the |
3563 | // pointer. (This is in contrast to e.g. qualification conversions, |
3564 | // which can be applied in nested pointer positions, effectively |
3565 | // requiring qualified and unqualified representations to match.) |
3566 | // However, it is pragmatic to ignore exception specifications |
3567 | // because it allows a certain amount of `noexcept` mismatching |
3568 | // to not become a visible ODR problem. This also leaves some |
3569 | // room for the committee to add laxness to function pointer |
3570 | // conversions in future standards. |
3571 | if (auto *MPT = T->getAs<MemberPointerType>()) |
3572 | if (MPT->isMemberFunctionPointer()) { |
3573 | QualType PointeeType = MPT->getPointeeType(); |
3574 | if (PointeeType->castAs<FunctionProtoType>()->getExceptionSpecType() != |
3575 | EST_None) { |
3576 | QualType FT = getFunctionTypeWithExceptionSpec(Orig: PointeeType, ESI: EST_None); |
3577 | T = getMemberPointerType(T: FT, Qualifier: MPT->getQualifier(), |
3578 | Cls: MPT->getMostRecentCXXRecordDecl()); |
3579 | } |
3580 | } |
3581 | std::unique_ptr<MangleContext> MC(createMangleContext()); |
3582 | MC->mangleCanonicalTypeName(T, Out); |
3583 | } |
3584 | |
3585 | return llvm::getPointerAuthStableSipHash(S: Str); |
3586 | } |
3587 | |
3588 | QualType ASTContext::getObjCGCQualType(QualType T, |
3589 | Qualifiers::GC GCAttr) const { |
3590 | QualType CanT = getCanonicalType(T); |
3591 | if (CanT.getObjCGCAttr() == GCAttr) |
3592 | return T; |
3593 | |
3594 | if (const auto *ptr = T->getAs<PointerType>()) { |
3595 | QualType Pointee = ptr->getPointeeType(); |
3596 | if (Pointee->isAnyPointerType()) { |
3597 | QualType ResultType = getObjCGCQualType(T: Pointee, GCAttr); |
3598 | return getPointerType(T: ResultType); |
3599 | } |
3600 | } |
3601 | |
3602 | // If we are composing extended qualifiers together, merge together |
3603 | // into one ExtQuals node. |
3604 | QualifierCollector Quals; |
3605 | const Type *TypeNode = Quals.strip(type: T); |
3606 | |
3607 | // If this type already has an ObjCGC specified, it cannot get |
3608 | // another one. |
3609 | assert(!Quals.hasObjCGCAttr() && |
3610 | "Type cannot have multiple ObjCGCs!"); |
3611 | Quals.addObjCGCAttr(type: GCAttr); |
3612 | |
3613 | return getExtQualType(baseType: TypeNode, quals: Quals); |
3614 | } |
3615 | |
3616 | QualType ASTContext::removePtrSizeAddrSpace(QualType T) const { |
3617 | if (const PointerType *Ptr = T->getAs<PointerType>()) { |
3618 | QualType Pointee = Ptr->getPointeeType(); |
3619 | if (isPtrSizeAddressSpace(AS: Pointee.getAddressSpace())) { |
3620 | return getPointerType(T: removeAddrSpaceQualType(T: Pointee)); |
3621 | } |
3622 | } |
3623 | return T; |
3624 | } |
3625 | |
3626 | QualType ASTContext::getCountAttributedType( |
3627 | QualType WrappedTy, Expr *CountExpr, bool CountInBytes, bool OrNull, |
3628 | ArrayRef<TypeCoupledDeclRefInfo> DependentDecls) const { |
3629 | assert(WrappedTy->isPointerType() || WrappedTy->isArrayType()); |
3630 | |
3631 | llvm::FoldingSetNodeID ID; |
3632 | CountAttributedType::Profile(ID, WrappedTy, CountExpr, CountInBytes, Nullable: OrNull); |
3633 | |
3634 | void *InsertPos = nullptr; |
3635 | CountAttributedType *CATy = |
3636 | CountAttributedTypes.FindNodeOrInsertPos(ID, InsertPos); |
3637 | if (CATy) |
3638 | return QualType(CATy, 0); |
3639 | |
3640 | QualType CanonTy = getCanonicalType(T: WrappedTy); |
3641 | size_t Size = CountAttributedType::totalSizeToAlloc<TypeCoupledDeclRefInfo>( |
3642 | DependentDecls.size()); |
3643 | CATy = (CountAttributedType *)Allocate(Size, Align: TypeAlignment); |
3644 | new (CATy) CountAttributedType(WrappedTy, CanonTy, CountExpr, CountInBytes, |
3645 | OrNull, DependentDecls); |
3646 | Types.push_back(CATy); |
3647 | CountAttributedTypes.InsertNode(N: CATy, InsertPos); |
3648 | |
3649 | return QualType(CATy, 0); |
3650 | } |
3651 | |
3652 | QualType |
3653 | ASTContext::adjustType(QualType Orig, |
3654 | llvm::function_ref<QualType(QualType)> Adjust) const { |
3655 | switch (Orig->getTypeClass()) { |
3656 | case Type::Attributed: { |
3657 | const auto *AT = cast<AttributedType>(Val&: Orig); |
3658 | return getAttributedType(attrKind: AT->getAttrKind(), |
3659 | modifiedType: adjustType(Orig: AT->getModifiedType(), Adjust), |
3660 | equivalentType: adjustType(Orig: AT->getEquivalentType(), Adjust), |
3661 | attr: AT->getAttr()); |
3662 | } |
3663 | |
3664 | case Type::BTFTagAttributed: { |
3665 | const auto *BTFT = dyn_cast<BTFTagAttributedType>(Val&: Orig); |
3666 | return getBTFTagAttributedType(BTFAttr: BTFT->getAttr(), |
3667 | Wrapped: adjustType(Orig: BTFT->getWrappedType(), Adjust)); |
3668 | } |
3669 | |
3670 | case Type::Elaborated: { |
3671 | const auto *ET = cast<ElaboratedType>(Val&: Orig); |
3672 | return getElaboratedType(Keyword: ET->getKeyword(), NNS: ET->getQualifier(), |
3673 | NamedType: adjustType(Orig: ET->getNamedType(), Adjust)); |
3674 | } |
3675 | |
3676 | case Type::Paren: |
3677 | return getParenType( |
3678 | NamedType: adjustType(Orig: cast<ParenType>(Val&: Orig)->getInnerType(), Adjust)); |
3679 | |
3680 | case Type::Adjusted: { |
3681 | const auto *AT = cast<AdjustedType>(Val&: Orig); |
3682 | return getAdjustedType(Orig: AT->getOriginalType(), |
3683 | New: adjustType(Orig: AT->getAdjustedType(), Adjust)); |
3684 | } |
3685 | |
3686 | case Type::MacroQualified: { |
3687 | const auto *MQT = cast<MacroQualifiedType>(Val&: Orig); |
3688 | return getMacroQualifiedType(UnderlyingTy: adjustType(Orig: MQT->getUnderlyingType(), Adjust), |
3689 | MacroII: MQT->getMacroIdentifier()); |
3690 | } |
3691 | |
3692 | default: |
3693 | return Adjust(Orig); |
3694 | } |
3695 | } |
3696 | |
3697 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
3698 | FunctionType::ExtInfo Info) { |
3699 | if (T->getExtInfo() == Info) |
3700 | return T; |
3701 | |
3702 | QualType Result; |
3703 | if (const auto *FNPT = dyn_cast<FunctionNoProtoType>(Val: T)) { |
3704 | Result = getFunctionNoProtoType(FNPT->getReturnType(), Info); |
3705 | } else { |
3706 | const auto *FPT = cast<FunctionProtoType>(Val: T); |
3707 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
3708 | EPI.ExtInfo = Info; |
3709 | Result = getFunctionType(ResultTy: FPT->getReturnType(), Args: FPT->getParamTypes(), EPI); |
3710 | } |
3711 | |
3712 | return cast<FunctionType>(Val: Result.getTypePtr()); |
3713 | } |
3714 | |
3715 | QualType ASTContext::adjustFunctionResultType(QualType FunctionType, |
3716 | QualType ResultType) { |
3717 | return adjustType(FunctionType, [&](QualType Orig) { |
3718 | if (const auto *FNPT = Orig->getAs<FunctionNoProtoType>()) |
3719 | return getFunctionNoProtoType(ResultType, FNPT->getExtInfo()); |
3720 | |
3721 | const auto *FPT = Orig->castAs<FunctionProtoType>(); |
3722 | return getFunctionType(ResultType, FPT->getParamTypes(), |
3723 | FPT->getExtProtoInfo()); |
3724 | }); |
3725 | } |
3726 | |
3727 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
3728 | QualType ResultType) { |
3729 | FD = FD->getMostRecentDecl(); |
3730 | while (true) { |
3731 | FD->setType(adjustFunctionResultType(FunctionType: FD->getType(), ResultType)); |
3732 | if (FunctionDecl *Next = FD->getPreviousDecl()) |
3733 | FD = Next; |
3734 | else |
3735 | break; |
3736 | } |
3737 | if (ASTMutationListener *L = getASTMutationListener()) |
3738 | L->DeducedReturnType(FD, ReturnType: ResultType); |
3739 | } |
3740 | |
3741 | /// Get a function type and produce the equivalent function type with the |
3742 | /// specified exception specification. Type sugar that can be present on a |
3743 | /// declaration of a function with an exception specification is permitted |
3744 | /// and preserved. Other type sugar (for instance, typedefs) is not. |
3745 | QualType ASTContext::getFunctionTypeWithExceptionSpec( |
3746 | QualType Orig, const FunctionProtoType::ExceptionSpecInfo &ESI) const { |
3747 | return adjustType(Orig, [&](QualType Ty) { |
3748 | const auto *Proto = Ty->castAs<FunctionProtoType>(); |
3749 | return getFunctionType(Proto->getReturnType(), Proto->getParamTypes(), |
3750 | Proto->getExtProtoInfo().withExceptionSpec(ESI)); |
3751 | }); |
3752 | } |
3753 | |
3754 | bool ASTContext::hasSameFunctionTypeIgnoringExceptionSpec(QualType T, |
3755 | QualType U) const { |
3756 | return hasSameType(T1: T, T2: U) || |
3757 | (getLangOpts().CPlusPlus17 && |
3758 | hasSameType(T1: getFunctionTypeWithExceptionSpec(Orig: T, ESI: EST_None), |
3759 | T2: getFunctionTypeWithExceptionSpec(Orig: U, ESI: EST_None))); |
3760 | } |
3761 | |
3762 | QualType ASTContext::getFunctionTypeWithoutPtrSizes(QualType T) { |
3763 | if (const auto *Proto = T->getAs<FunctionProtoType>()) { |
3764 | QualType RetTy = removePtrSizeAddrSpace(T: Proto->getReturnType()); |
3765 | SmallVector<QualType, 16> Args(Proto->param_types().size()); |
3766 | for (unsigned i = 0, n = Args.size(); i != n; ++i) |
3767 | Args[i] = removePtrSizeAddrSpace(T: Proto->param_types()[i]); |
3768 | return getFunctionType(ResultTy: RetTy, Args, EPI: Proto->getExtProtoInfo()); |
3769 | } |
3770 | |
3771 | if (const FunctionNoProtoType *Proto = T->getAs<FunctionNoProtoType>()) { |
3772 | QualType RetTy = removePtrSizeAddrSpace(T: Proto->getReturnType()); |
3773 | return getFunctionNoProtoType(RetTy, Proto->getExtInfo()); |
3774 | } |
3775 | |
3776 | return T; |
3777 | } |
3778 | |
3779 | bool ASTContext::hasSameFunctionTypeIgnoringPtrSizes(QualType T, QualType U) { |
3780 | return hasSameType(T1: T, T2: U) || |
3781 | hasSameType(T1: getFunctionTypeWithoutPtrSizes(T), |
3782 | T2: getFunctionTypeWithoutPtrSizes(T: U)); |
3783 | } |
3784 | |
3785 | QualType ASTContext::getFunctionTypeWithoutParamABIs(QualType T) const { |
3786 | if (const auto *Proto = T->getAs<FunctionProtoType>()) { |
3787 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
3788 | EPI.ExtParameterInfos = nullptr; |
3789 | return getFunctionType(ResultTy: Proto->getReturnType(), Args: Proto->param_types(), EPI); |
3790 | } |
3791 | return T; |
3792 | } |
3793 | |
3794 | bool ASTContext::hasSameFunctionTypeIgnoringParamABI(QualType T, |
3795 | QualType U) const { |
3796 | return hasSameType(T1: T, T2: U) || hasSameType(T1: getFunctionTypeWithoutParamABIs(T), |
3797 | T2: getFunctionTypeWithoutParamABIs(T: U)); |
3798 | } |
3799 | |
3800 | void ASTContext::adjustExceptionSpec( |
3801 | FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI, |
3802 | bool AsWritten) { |
3803 | // Update the type. |
3804 | QualType Updated = |
3805 | getFunctionTypeWithExceptionSpec(Orig: FD->getType(), ESI); |
3806 | FD->setType(Updated); |
3807 | |
3808 | if (!AsWritten) |
3809 | return; |
3810 | |
3811 | // Update the type in the type source information too. |
3812 | if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) { |
3813 | // If the type and the type-as-written differ, we may need to update |
3814 | // the type-as-written too. |
3815 | if (TSInfo->getType() != FD->getType()) |
3816 | Updated = getFunctionTypeWithExceptionSpec(Orig: TSInfo->getType(), ESI); |
3817 | |
3818 | // FIXME: When we get proper type location information for exceptions, |
3819 | // we'll also have to rebuild the TypeSourceInfo. For now, we just patch |
3820 | // up the TypeSourceInfo; |
3821 | assert(TypeLoc::getFullDataSizeForType(Updated) == |
3822 | TypeLoc::getFullDataSizeForType(TSInfo->getType()) && |
3823 | "TypeLoc size mismatch from updating exception specification"); |
3824 | TSInfo->overrideType(T: Updated); |
3825 | } |
3826 | } |
3827 | |
3828 | /// getComplexType - Return the uniqued reference to the type for a complex |
3829 | /// number with the specified element type. |
3830 | QualType ASTContext::getComplexType(QualType T) const { |
3831 | // Unique pointers, to guarantee there is only one pointer of a particular |
3832 | // structure. |
3833 | llvm::FoldingSetNodeID ID; |
3834 | ComplexType::Profile(ID, Element: T); |
3835 | |
3836 | void *InsertPos = nullptr; |
3837 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
3838 | return QualType(CT, 0); |
3839 | |
3840 | // If the pointee type isn't canonical, this won't be a canonical type either, |
3841 | // so fill in the canonical type field. |
3842 | QualType Canonical; |
3843 | if (!T.isCanonical()) { |
3844 | Canonical = getComplexType(T: getCanonicalType(T)); |
3845 | |
3846 | // Get the new insert position for the node we care about. |
3847 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
3848 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
3849 | } |
3850 | auto *New = new (*this, alignof(ComplexType)) ComplexType(T, Canonical); |
3851 | Types.push_back(New); |
3852 | ComplexTypes.InsertNode(N: New, InsertPos); |
3853 | return QualType(New, 0); |
3854 | } |
3855 | |
3856 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
3857 | /// the specified type. |
3858 | QualType ASTContext::getPointerType(QualType T) const { |
3859 | // Unique pointers, to guarantee there is only one pointer of a particular |
3860 | // structure. |
3861 | llvm::FoldingSetNodeID ID; |
3862 | PointerType::Profile(ID, Pointee: T); |
3863 | |
3864 | void *InsertPos = nullptr; |
3865 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
3866 | return QualType(PT, 0); |
3867 | |
3868 | // If the pointee type isn't canonical, this won't be a canonical type either, |
3869 | // so fill in the canonical type field. |
3870 | QualType Canonical; |
3871 | if (!T.isCanonical()) { |
3872 | Canonical = getPointerType(T: getCanonicalType(T)); |
3873 | |
3874 | // Get the new insert position for the node we care about. |
3875 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
3876 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
3877 | } |
3878 | auto *New = new (*this, alignof(PointerType)) PointerType(T, Canonical); |
3879 | Types.push_back(New); |
3880 | PointerTypes.InsertNode(N: New, InsertPos); |
3881 | return QualType(New, 0); |
3882 | } |
3883 | |
3884 | QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const { |
3885 | llvm::FoldingSetNodeID ID; |
3886 | AdjustedType::Profile(ID, Orig, New); |
3887 | void *InsertPos = nullptr; |
3888 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
3889 | if (AT) |
3890 | return QualType(AT, 0); |
3891 | |
3892 | QualType Canonical = getCanonicalType(T: New); |
3893 | |
3894 | // Get the new insert position for the node we care about. |
3895 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
3896 | assert(!AT && "Shouldn't be in the map!"); |
3897 | |
3898 | AT = new (*this, alignof(AdjustedType)) |
3899 | AdjustedType(Type::Adjusted, Orig, New, Canonical); |
3900 | Types.push_back(AT); |
3901 | AdjustedTypes.InsertNode(N: AT, InsertPos); |
3902 | return QualType(AT, 0); |
3903 | } |
3904 | |
3905 | QualType ASTContext::getDecayedType(QualType Orig, QualType Decayed) const { |
3906 | llvm::FoldingSetNodeID ID; |
3907 | AdjustedType::Profile(ID, Orig, New: Decayed); |
3908 | void *InsertPos = nullptr; |
3909 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
3910 | if (AT) |
3911 | return QualType(AT, 0); |
3912 | |
3913 | QualType Canonical = getCanonicalType(T: Decayed); |
3914 | |
3915 | // Get the new insert position for the node we care about. |
3916 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
3917 | assert(!AT && "Shouldn't be in the map!"); |
3918 | |
3919 | AT = new (*this, alignof(DecayedType)) DecayedType(Orig, Decayed, Canonical); |
3920 | Types.push_back(AT); |
3921 | AdjustedTypes.InsertNode(N: AT, InsertPos); |
3922 | return QualType(AT, 0); |
3923 | } |
3924 | |
3925 | QualType ASTContext::getDecayedType(QualType T) const { |
3926 | assert((T->isArrayType() || T->isFunctionType()) && "T does not decay"); |
3927 | |
3928 | QualType Decayed; |
3929 | |
3930 | // C99 6.7.5.3p7: |
3931 | // A declaration of a parameter as "array of type" shall be |
3932 | // adjusted to "qualified pointer to type", where the type |
3933 | // qualifiers (if any) are those specified within the [ and ] of |
3934 | // the array type derivation. |
3935 | if (T->isArrayType()) |
3936 | Decayed = getArrayDecayedType(T); |
3937 | |
3938 | // C99 6.7.5.3p8: |
3939 | // A declaration of a parameter as "function returning type" |
3940 | // shall be adjusted to "pointer to function returning type", as |
3941 | // in 6.3.2.1. |
3942 | if (T->isFunctionType()) |
3943 | Decayed = getPointerType(T); |
3944 | |
3945 | return getDecayedType(Orig: T, Decayed); |
3946 | } |
3947 | |
3948 | QualType ASTContext::getArrayParameterType(QualType Ty) const { |
3949 | if (Ty->isArrayParameterType()) |
3950 | return Ty; |
3951 | assert(Ty->isConstantArrayType() && "Ty must be an array type."); |
3952 | QualType DTy = Ty.getDesugaredType(Context: *this); |
3953 | const auto *ATy = cast<ConstantArrayType>(Val&: DTy); |
3954 | llvm::FoldingSetNodeID ID; |
3955 | ATy->Profile(ID, *this, ATy->getElementType(), ATy->getZExtSize(), |
3956 | ATy->getSizeExpr(), ATy->getSizeModifier(), |
3957 | ATy->getIndexTypeQualifiers().getAsOpaqueValue()); |
3958 | void *InsertPos = nullptr; |
3959 | ArrayParameterType *AT = |
3960 | ArrayParameterTypes.FindNodeOrInsertPos(ID, InsertPos); |
3961 | if (AT) |
3962 | return QualType(AT, 0); |
3963 | |
3964 | QualType Canonical; |
3965 | if (!DTy.isCanonical()) { |
3966 | Canonical = getArrayParameterType(Ty: getCanonicalType(T: Ty)); |
3967 | |
3968 | // Get the new insert position for the node we care about. |
3969 | AT = ArrayParameterTypes.FindNodeOrInsertPos(ID, InsertPos); |
3970 | assert(!AT && "Shouldn't be in the map!"); |
3971 | } |
3972 | |
3973 | AT = new (*this, alignof(ArrayParameterType)) |
3974 | ArrayParameterType(ATy, Canonical); |
3975 | Types.push_back(AT); |
3976 | ArrayParameterTypes.InsertNode(N: AT, InsertPos); |
3977 | return QualType(AT, 0); |
3978 | } |
3979 | |
3980 | /// getBlockPointerType - Return the uniqued reference to the type for |
3981 | /// a pointer to the specified block. |
3982 | QualType ASTContext::getBlockPointerType(QualType T) const { |
3983 | assert(T->isFunctionType() && "block of function types only"); |
3984 | // Unique pointers, to guarantee there is only one block of a particular |
3985 | // structure. |
3986 | llvm::FoldingSetNodeID ID; |
3987 | BlockPointerType::Profile(ID, Pointee: T); |
3988 | |
3989 | void *InsertPos = nullptr; |
3990 | if (BlockPointerType *PT = |
3991 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
3992 | return QualType(PT, 0); |
3993 | |
3994 | // If the block pointee type isn't canonical, this won't be a canonical |
3995 | // type either so fill in the canonical type field. |
3996 | QualType Canonical; |
3997 | if (!T.isCanonical()) { |
3998 | Canonical = getBlockPointerType(T: getCanonicalType(T)); |
3999 | |
4000 | // Get the new insert position for the node we care about. |
4001 | BlockPointerType *NewIP = |
4002 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
4003 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
4004 | } |
4005 | auto *New = |
4006 | new (*this, alignof(BlockPointerType)) BlockPointerType(T, Canonical); |
4007 | Types.push_back(New); |
4008 | BlockPointerTypes.InsertNode(N: New, InsertPos); |
4009 | return QualType(New, 0); |
4010 | } |
4011 | |
4012 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
4013 | /// lvalue reference to the specified type. |
4014 | QualType |
4015 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
4016 | assert((!T->isPlaceholderType() || |
4017 | T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) && |
4018 | "Unresolved placeholder type"); |
4019 | |
4020 | // Unique pointers, to guarantee there is only one pointer of a particular |
4021 | // structure. |
4022 | llvm::FoldingSetNodeID ID; |
4023 | ReferenceType::Profile(ID, Referencee: T, SpelledAsLValue); |
4024 | |
4025 | void *InsertPos = nullptr; |
4026 | if (LValueReferenceType *RT = |
4027 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4028 | return QualType(RT, 0); |
4029 | |
4030 | const auto *InnerRef = T->getAs<ReferenceType>(); |
4031 | |
4032 | // If the referencee type isn't canonical, this won't be a canonical type |
4033 | // either, so fill in the canonical type field. |
4034 | QualType Canonical; |
4035 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
4036 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
4037 | Canonical = getLValueReferenceType(T: getCanonicalType(T: PointeeType)); |
4038 | |
4039 | // Get the new insert position for the node we care about. |
4040 | LValueReferenceType *NewIP = |
4041 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
4042 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
4043 | } |
4044 | |
4045 | auto *New = new (*this, alignof(LValueReferenceType)) |
4046 | LValueReferenceType(T, Canonical, SpelledAsLValue); |
4047 | Types.push_back(New); |
4048 | LValueReferenceTypes.InsertNode(N: New, InsertPos); |
4049 | |
4050 | return QualType(New, 0); |
4051 | } |
4052 | |
4053 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
4054 | /// rvalue reference to the specified type. |
4055 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
4056 | assert((!T->isPlaceholderType() || |
4057 | T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) && |
4058 | "Unresolved placeholder type"); |
4059 | |
4060 | // Unique pointers, to guarantee there is only one pointer of a particular |
4061 | // structure. |
4062 | llvm::FoldingSetNodeID ID; |
4063 | ReferenceType::Profile(ID, Referencee: T, SpelledAsLValue: false); |
4064 | |
4065 | void *InsertPos = nullptr; |
4066 | if (RValueReferenceType *RT = |
4067 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4068 | return QualType(RT, 0); |
4069 | |
4070 | const auto *InnerRef = T->getAs<ReferenceType>(); |
4071 | |
4072 | // If the referencee type isn't canonical, this won't be a canonical type |
4073 | // either, so fill in the canonical type field. |
4074 | QualType Canonical; |
4075 | if (InnerRef || !T.isCanonical()) { |
4076 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
4077 | Canonical = getRValueReferenceType(T: getCanonicalType(T: PointeeType)); |
4078 | |
4079 | // Get the new insert position for the node we care about. |
4080 | RValueReferenceType *NewIP = |
4081 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
4082 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
4083 | } |
4084 | |
4085 | auto *New = new (*this, alignof(RValueReferenceType)) |
4086 | RValueReferenceType(T, Canonical); |
4087 | Types.push_back(New); |
4088 | RValueReferenceTypes.InsertNode(N: New, InsertPos); |
4089 | return QualType(New, 0); |
4090 | } |
4091 | |
4092 | QualType ASTContext::getMemberPointerType(QualType T, |
4093 | NestedNameSpecifier *Qualifier, |
4094 | const CXXRecordDecl *Cls) const { |
4095 | if (!Qualifier) { |
4096 | assert(Cls && "At least one of Qualifier or Cls must be provided"); |
4097 | Qualifier = NestedNameSpecifier::Create(Context: *this, /*Prefix=*/nullptr, |
4098 | T: getTypeDeclType(Cls).getTypePtr()); |
4099 | } else if (!Cls) { |
4100 | Cls = Qualifier->getAsRecordDecl(); |
4101 | } |
4102 | // Unique pointers, to guarantee there is only one pointer of a particular |
4103 | // structure. |
4104 | llvm::FoldingSetNodeID ID; |
4105 | MemberPointerType::Profile(ID, Pointee: T, Qualifier, Cls); |
4106 | |
4107 | void *InsertPos = nullptr; |
4108 | if (MemberPointerType *PT = |
4109 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4110 | return QualType(PT, 0); |
4111 | |
4112 | NestedNameSpecifier *CanonicalQualifier = [&] { |
4113 | if (!Cls) |
4114 | return getCanonicalNestedNameSpecifier(NNS: Qualifier); |
4115 | NestedNameSpecifier *R = NestedNameSpecifier::Create( |
4116 | *this, /*Prefix=*/nullptr, Cls->getCanonicalDecl()->getTypeForDecl()); |
4117 | assert(R == getCanonicalNestedNameSpecifier(R)); |
4118 | return R; |
4119 | }(); |
4120 | // If the pointee or class type isn't canonical, this won't be a canonical |
4121 | // type either, so fill in the canonical type field. |
4122 | QualType Canonical; |
4123 | if (!T.isCanonical() || Qualifier != CanonicalQualifier) { |
4124 | Canonical = |
4125 | getMemberPointerType(T: getCanonicalType(T), Qualifier: CanonicalQualifier, Cls); |
4126 | assert(!cast<MemberPointerType>(Canonical)->isSugared()); |
4127 | // Get the new insert position for the node we care about. |
4128 | [[maybe_unused]] MemberPointerType *NewIP = |
4129 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
4130 | assert(!NewIP && "Shouldn't be in the map!"); |
4131 | } |
4132 | auto *New = new (*this, alignof(MemberPointerType)) |
4133 | MemberPointerType(T, Qualifier, Canonical); |
4134 | Types.push_back(New); |
4135 | MemberPointerTypes.InsertNode(N: New, InsertPos); |
4136 | return QualType(New, 0); |
4137 | } |
4138 | |
4139 | /// getConstantArrayType - Return the unique reference to the type for an |
4140 | /// array of the specified element type. |
4141 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
4142 | const llvm::APInt &ArySizeIn, |
4143 | const Expr *SizeExpr, |
4144 | ArraySizeModifier ASM, |
4145 | unsigned IndexTypeQuals) const { |
4146 | assert((EltTy->isDependentType() || |
4147 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
4148 | "Constant array of VLAs is illegal!"); |
4149 | |
4150 | // We only need the size as part of the type if it's instantiation-dependent. |
4151 | if (SizeExpr && !SizeExpr->isInstantiationDependent()) |
4152 | SizeExpr = nullptr; |
4153 | |
4154 | // Convert the array size into a canonical width matching the pointer size for |
4155 | // the target. |
4156 | llvm::APInt ArySize(ArySizeIn); |
4157 | ArySize = ArySize.zextOrTrunc(width: Target->getMaxPointerWidth()); |
4158 | |
4159 | llvm::FoldingSetNodeID ID; |
4160 | ConstantArrayType::Profile(ID, Ctx: *this, ET: EltTy, ArraySize: ArySize.getZExtValue(), SizeExpr, |
4161 | SizeMod: ASM, TypeQuals: IndexTypeQuals); |
4162 | |
4163 | void *InsertPos = nullptr; |
4164 | if (ConstantArrayType *ATP = |
4165 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4166 | return QualType(ATP, 0); |
4167 | |
4168 | // If the element type isn't canonical or has qualifiers, or the array bound |
4169 | // is instantiation-dependent, this won't be a canonical type either, so fill |
4170 | // in the canonical type field. |
4171 | QualType Canon; |
4172 | // FIXME: Check below should look for qualifiers behind sugar. |
4173 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers() || SizeExpr) { |
4174 | SplitQualType canonSplit = getCanonicalType(T: EltTy).split(); |
4175 | Canon = getConstantArrayType(EltTy: QualType(canonSplit.Ty, 0), ArySizeIn: ArySize, SizeExpr: nullptr, |
4176 | ASM, IndexTypeQuals); |
4177 | Canon = getQualifiedType(T: Canon, Qs: canonSplit.Quals); |
4178 | |
4179 | // Get the new insert position for the node we care about. |
4180 | ConstantArrayType *NewIP = |
4181 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
4182 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
4183 | } |
4184 | |
4185 | auto *New = ConstantArrayType::Create(Ctx: *this, ET: EltTy, Can: Canon, Sz: ArySize, SzExpr: SizeExpr, |
4186 | SzMod: ASM, Qual: IndexTypeQuals); |
4187 | ConstantArrayTypes.InsertNode(N: New, InsertPos); |
4188 | Types.push_back(New); |
4189 | return QualType(New, 0); |
4190 | } |
4191 | |
4192 | /// getVariableArrayDecayedType - Turns the given type, which may be |
4193 | /// variably-modified, into the corresponding type with all the known |
4194 | /// sizes replaced with [*]. |
4195 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
4196 | // Vastly most common case. |
4197 | if (!type->isVariablyModifiedType()) return type; |
4198 | |
4199 | QualType result; |
4200 | |
4201 | SplitQualType split = type.getSplitDesugaredType(); |
4202 | const Type *ty = split.Ty; |
4203 | switch (ty->getTypeClass()) { |
4204 | #define TYPE(Class, Base) |
4205 | #define ABSTRACT_TYPE(Class, Base) |
4206 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
4207 | #include "clang/AST/TypeNodes.inc" |
4208 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
4209 | |
4210 | // These types should never be variably-modified. |
4211 | case Type::Builtin: |
4212 | case Type::Complex: |
4213 | case Type::Vector: |
4214 | case Type::DependentVector: |
4215 | case Type::ExtVector: |
4216 | case Type::DependentSizedExtVector: |
4217 | case Type::ConstantMatrix: |
4218 | case Type::DependentSizedMatrix: |
4219 | case Type::DependentAddressSpace: |
4220 | case Type::ObjCObject: |
4221 | case Type::ObjCInterface: |
4222 | case Type::ObjCObjectPointer: |
4223 | case Type::Record: |
4224 | case Type::Enum: |
4225 | case Type::UnresolvedUsing: |
4226 | case Type::TypeOfExpr: |
4227 | case Type::TypeOf: |
4228 | case Type::Decltype: |
4229 | case Type::UnaryTransform: |
4230 | case Type::DependentName: |
4231 | case Type::InjectedClassName: |
4232 | case Type::TemplateSpecialization: |
4233 | case Type::DependentTemplateSpecialization: |
4234 | case Type::TemplateTypeParm: |
4235 | case Type::SubstTemplateTypeParmPack: |
4236 | case Type::Auto: |
4237 | case Type::DeducedTemplateSpecialization: |
4238 | case Type::PackExpansion: |
4239 | case Type::PackIndexing: |
4240 | case Type::BitInt: |
4241 | case Type::DependentBitInt: |
4242 | case Type::ArrayParameter: |
4243 | case Type::HLSLAttributedResource: |
4244 | case Type::HLSLInlineSpirv: |
4245 | llvm_unreachable("type should never be variably-modified"); |
4246 | |
4247 | // These types can be variably-modified but should never need to |
4248 | // further decay. |
4249 | case Type::FunctionNoProto: |
4250 | case Type::FunctionProto: |
4251 | case Type::BlockPointer: |
4252 | case Type::MemberPointer: |
4253 | case Type::Pipe: |
4254 | return type; |
4255 | |
4256 | // These types can be variably-modified. All these modifications |
4257 | // preserve structure except as noted by comments. |
4258 | // TODO: if we ever care about optimizing VLAs, there are no-op |
4259 | // optimizations available here. |
4260 | case Type::Pointer: |
4261 | result = getPointerType(getVariableArrayDecayedType( |
4262 | type: cast<PointerType>(ty)->getPointeeType())); |
4263 | break; |
4264 | |
4265 | case Type::LValueReference: { |
4266 | const auto *lv = cast<LValueReferenceType>(ty); |
4267 | result = getLValueReferenceType( |
4268 | T: getVariableArrayDecayedType(type: lv->getPointeeType()), |
4269 | SpelledAsLValue: lv->isSpelledAsLValue()); |
4270 | break; |
4271 | } |
4272 | |
4273 | case Type::RValueReference: { |
4274 | const auto *lv = cast<RValueReferenceType>(ty); |
4275 | result = getRValueReferenceType( |
4276 | T: getVariableArrayDecayedType(type: lv->getPointeeType())); |
4277 | break; |
4278 | } |
4279 | |
4280 | case Type::Atomic: { |
4281 | const auto *at = cast<AtomicType>(ty); |
4282 | result = getAtomicType(T: getVariableArrayDecayedType(type: at->getValueType())); |
4283 | break; |
4284 | } |
4285 | |
4286 | case Type::ConstantArray: { |
4287 | const auto *cat = cast<ConstantArrayType>(ty); |
4288 | result = getConstantArrayType( |
4289 | EltTy: getVariableArrayDecayedType(type: cat->getElementType()), |
4290 | ArySizeIn: cat->getSize(), |
4291 | SizeExpr: cat->getSizeExpr(), |
4292 | ASM: cat->getSizeModifier(), |
4293 | IndexTypeQuals: cat->getIndexTypeCVRQualifiers()); |
4294 | break; |
4295 | } |
4296 | |
4297 | case Type::DependentSizedArray: { |
4298 | const auto *dat = cast<DependentSizedArrayType>(ty); |
4299 | result = getDependentSizedArrayType( |
4300 | EltTy: getVariableArrayDecayedType(type: dat->getElementType()), NumElts: dat->getSizeExpr(), |
4301 | ASM: dat->getSizeModifier(), IndexTypeQuals: dat->getIndexTypeCVRQualifiers()); |
4302 | break; |
4303 | } |
4304 | |
4305 | // Turn incomplete types into [*] types. |
4306 | case Type::IncompleteArray: { |
4307 | const auto *iat = cast<IncompleteArrayType>(ty); |
4308 | result = |
4309 | getVariableArrayType(EltTy: getVariableArrayDecayedType(type: iat->getElementType()), |
4310 | /*size*/ NumElts: nullptr, ASM: ArraySizeModifier::Normal, |
4311 | IndexTypeQuals: iat->getIndexTypeCVRQualifiers()); |
4312 | break; |
4313 | } |
4314 | |
4315 | // Turn VLA types into [*] types. |
4316 | case Type::VariableArray: { |
4317 | const auto *vat = cast<VariableArrayType>(ty); |
4318 | result = |
4319 | getVariableArrayType(EltTy: getVariableArrayDecayedType(type: vat->getElementType()), |
4320 | /*size*/ NumElts: nullptr, ASM: ArraySizeModifier::Star, |
4321 | IndexTypeQuals: vat->getIndexTypeCVRQualifiers()); |
4322 | break; |
4323 | } |
4324 | } |
4325 | |
4326 | // Apply the top-level qualifiers from the original. |
4327 | return getQualifiedType(T: result, Qs: split.Quals); |
4328 | } |
4329 | |
4330 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
4331 | /// variable array of the specified element type. |
4332 | QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, |
4333 | ArraySizeModifier ASM, |
4334 | unsigned IndexTypeQuals) const { |
4335 | // Since we don't unique expressions, it isn't possible to unique VLA's |
4336 | // that have an expression provided for their size. |
4337 | QualType Canon; |
4338 | |
4339 | // Be sure to pull qualifiers off the element type. |
4340 | // FIXME: Check below should look for qualifiers behind sugar. |
4341 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
4342 | SplitQualType canonSplit = getCanonicalType(T: EltTy).split(); |
4343 | Canon = getVariableArrayType(EltTy: QualType(canonSplit.Ty, 0), NumElts, ASM, |
4344 | IndexTypeQuals); |
4345 | Canon = getQualifiedType(T: Canon, Qs: canonSplit.Quals); |
4346 | } |
4347 | |
4348 | auto *New = new (*this, alignof(VariableArrayType)) |
4349 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals); |
4350 | |
4351 | VariableArrayTypes.push_back(x: New); |
4352 | Types.push_back(New); |
4353 | return QualType(New, 0); |
4354 | } |
4355 | |
4356 | /// getDependentSizedArrayType - Returns a non-unique reference to |
4357 | /// the type for a dependently-sized array of the specified element |
4358 | /// type. |
4359 | QualType |
4360 | ASTContext::getDependentSizedArrayType(QualType elementType, Expr *numElements, |
4361 | ArraySizeModifier ASM, |
4362 | unsigned elementTypeQuals) const { |
4363 | assert((!numElements || numElements->isTypeDependent() || |
4364 | numElements->isValueDependent()) && |
4365 | "Size must be type- or value-dependent!"); |
4366 | |
4367 | SplitQualType canonElementType = getCanonicalType(T: elementType).split(); |
4368 | |
4369 | void *insertPos = nullptr; |
4370 | llvm::FoldingSetNodeID ID; |
4371 | DependentSizedArrayType::Profile( |
4372 | ID, Context: *this, ET: numElements ? QualType(canonElementType.Ty, 0) : elementType, |
4373 | SizeMod: ASM, TypeQuals: elementTypeQuals, E: numElements); |
4374 | |
4375 | // Look for an existing type with these properties. |
4376 | DependentSizedArrayType *canonTy = |
4377 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos&: insertPos); |
4378 | |
4379 | // Dependently-sized array types that do not have a specified number |
4380 | // of elements will have their sizes deduced from a dependent |
4381 | // initializer. |
4382 | if (!numElements) { |
4383 | if (canonTy) |
4384 | return QualType(canonTy, 0); |
4385 | |
4386 | auto *newType = new (*this, alignof(DependentSizedArrayType)) |
4387 | DependentSizedArrayType(elementType, QualType(), numElements, ASM, |
4388 | elementTypeQuals); |
4389 | DependentSizedArrayTypes.InsertNode(N: newType, InsertPos: insertPos); |
4390 | Types.push_back(newType); |
4391 | return QualType(newType, 0); |
4392 | } |
4393 | |
4394 | // If we don't have one, build one. |
4395 | if (!canonTy) { |
4396 | canonTy = new (*this, alignof(DependentSizedArrayType)) |
4397 | DependentSizedArrayType(QualType(canonElementType.Ty, 0), QualType(), |
4398 | numElements, ASM, elementTypeQuals); |
4399 | DependentSizedArrayTypes.InsertNode(N: canonTy, InsertPos: insertPos); |
4400 | Types.push_back(canonTy); |
4401 | } |
4402 | |
4403 | // Apply qualifiers from the element type to the array. |
4404 | QualType canon = getQualifiedType(T: QualType(canonTy,0), |
4405 | Qs: canonElementType.Quals); |
4406 | |
4407 | // If we didn't need extra canonicalization for the element type or the size |
4408 | // expression, then just use that as our result. |
4409 | if (QualType(canonElementType.Ty, 0) == elementType && |
4410 | canonTy->getSizeExpr() == numElements) |
4411 | return canon; |
4412 | |
4413 | // Otherwise, we need to build a type which follows the spelling |
4414 | // of the element type. |
4415 | auto *sugaredType = new (*this, alignof(DependentSizedArrayType)) |
4416 | DependentSizedArrayType(elementType, canon, numElements, ASM, |
4417 | elementTypeQuals); |
4418 | Types.push_back(Elt: sugaredType); |
4419 | return QualType(sugaredType, 0); |
4420 | } |
4421 | |
4422 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
4423 | ArraySizeModifier ASM, |
4424 | unsigned elementTypeQuals) const { |
4425 | llvm::FoldingSetNodeID ID; |
4426 | IncompleteArrayType::Profile(ID, ET: elementType, SizeMod: ASM, TypeQuals: elementTypeQuals); |
4427 | |
4428 | void *insertPos = nullptr; |
4429 | if (IncompleteArrayType *iat = |
4430 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos&: insertPos)) |
4431 | return QualType(iat, 0); |
4432 | |
4433 | // If the element type isn't canonical, this won't be a canonical type |
4434 | // either, so fill in the canonical type field. We also have to pull |
4435 | // qualifiers off the element type. |
4436 | QualType canon; |
4437 | |
4438 | // FIXME: Check below should look for qualifiers behind sugar. |
4439 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
4440 | SplitQualType canonSplit = getCanonicalType(T: elementType).split(); |
4441 | canon = getIncompleteArrayType(elementType: QualType(canonSplit.Ty, 0), |
4442 | ASM, elementTypeQuals); |
4443 | canon = getQualifiedType(T: canon, Qs: canonSplit.Quals); |
4444 | |
4445 | // Get the new insert position for the node we care about. |
4446 | IncompleteArrayType *existing = |
4447 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos&: insertPos); |
4448 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
4449 | } |
4450 | |
4451 | auto *newType = new (*this, alignof(IncompleteArrayType)) |
4452 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
4453 | |
4454 | IncompleteArrayTypes.InsertNode(N: newType, InsertPos: insertPos); |
4455 | Types.push_back(newType); |
4456 | return QualType(newType, 0); |
4457 | } |
4458 | |
4459 | ASTContext::BuiltinVectorTypeInfo |
4460 | ASTContext::getBuiltinVectorTypeInfo(const BuiltinType *Ty) const { |
4461 | #define SVE_INT_ELTTY(BITS, ELTS, SIGNED, NUMVECTORS) \ |
4462 | {getIntTypeForBitwidth(BITS, SIGNED), llvm::ElementCount::getScalable(ELTS), \ |
4463 | NUMVECTORS}; |
4464 | |
4465 | #define SVE_ELTTY(ELTTY, ELTS, NUMVECTORS) \ |
4466 | {ELTTY, llvm::ElementCount::getScalable(ELTS), NUMVECTORS}; |
4467 | |
4468 | switch (Ty->getKind()) { |
4469 | default: |
4470 | llvm_unreachable("Unsupported builtin vector type"); |
4471 | |
4472 | #define SVE_VECTOR_TYPE_INT(Name, MangledName, Id, SingletonId, NumEls, \ |
4473 | ElBits, NF, IsSigned) \ |
4474 | case BuiltinType::Id: \ |
4475 | return {getIntTypeForBitwidth(ElBits, IsSigned), \ |
4476 | llvm::ElementCount::getScalable(NumEls), NF}; |
4477 | #define SVE_VECTOR_TYPE_FLOAT(Name, MangledName, Id, SingletonId, NumEls, \ |
4478 | ElBits, NF) \ |
4479 | case BuiltinType::Id: \ |
4480 | return {ElBits == 16 ? HalfTy : (ElBits == 32 ? FloatTy : DoubleTy), \ |
4481 | llvm::ElementCount::getScalable(NumEls), NF}; |
4482 | #define SVE_VECTOR_TYPE_BFLOAT(Name, MangledName, Id, SingletonId, NumEls, \ |
4483 | ElBits, NF) \ |
4484 | case BuiltinType::Id: \ |
4485 | return {BFloat16Ty, llvm::ElementCount::getScalable(NumEls), NF}; |
4486 | #define SVE_VECTOR_TYPE_MFLOAT(Name, MangledName, Id, SingletonId, NumEls, \ |
4487 | ElBits, NF) \ |
4488 | case BuiltinType::Id: \ |
4489 | return {MFloat8Ty, llvm::ElementCount::getScalable(NumEls), NF}; |
4490 | #define SVE_PREDICATE_TYPE_ALL(Name, MangledName, Id, SingletonId, NumEls, NF) \ |
4491 | case BuiltinType::Id: \ |
4492 | return {BoolTy, llvm::ElementCount::getScalable(NumEls), NF}; |
4493 | #include "clang/Basic/AArch64ACLETypes.def" |
4494 | |
4495 | #define RVV_VECTOR_TYPE_INT(Name, Id, SingletonId, NumEls, ElBits, NF, \ |
4496 | IsSigned) \ |
4497 | case BuiltinType::Id: \ |
4498 | return {getIntTypeForBitwidth(ElBits, IsSigned), \ |
4499 | llvm::ElementCount::getScalable(NumEls), NF}; |
4500 | #define RVV_VECTOR_TYPE_FLOAT(Name, Id, SingletonId, NumEls, ElBits, NF) \ |
4501 | case BuiltinType::Id: \ |
4502 | return {ElBits == 16 ? Float16Ty : (ElBits == 32 ? FloatTy : DoubleTy), \ |
4503 | llvm::ElementCount::getScalable(NumEls), NF}; |
4504 | #define RVV_VECTOR_TYPE_BFLOAT(Name, Id, SingletonId, NumEls, ElBits, NF) \ |
4505 | case BuiltinType::Id: \ |
4506 | return {BFloat16Ty, llvm::ElementCount::getScalable(NumEls), NF}; |
4507 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, NumEls) \ |
4508 | case BuiltinType::Id: \ |
4509 | return {BoolTy, llvm::ElementCount::getScalable(NumEls), 1}; |
4510 | #include "clang/Basic/RISCVVTypes.def" |
4511 | } |
4512 | } |
4513 | |
4514 | /// getExternrefType - Return a WebAssembly externref type, which represents an |
4515 | /// opaque reference to a host value. |
4516 | QualType ASTContext::getWebAssemblyExternrefType() const { |
4517 | if (Target->getTriple().isWasm() && Target->hasFeature(Feature: "reference-types")) { |
4518 | #define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS) \ |
4519 | if (BuiltinType::Id == BuiltinType::WasmExternRef) \ |
4520 | return SingletonId; |
4521 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
4522 | } |
4523 | llvm_unreachable( |
4524 | "shouldn't try to generate type externref outside WebAssembly target"); |
4525 | } |
4526 | |
4527 | /// getScalableVectorType - Return the unique reference to a scalable vector |
4528 | /// type of the specified element type and size. VectorType must be a built-in |
4529 | /// type. |
4530 | QualType ASTContext::getScalableVectorType(QualType EltTy, unsigned NumElts, |
4531 | unsigned NumFields) const { |
4532 | if (Target->hasAArch64ACLETypes()) { |
4533 | uint64_t EltTySize = getTypeSize(T: EltTy); |
4534 | |
4535 | #define SVE_VECTOR_TYPE_INT(Name, MangledName, Id, SingletonId, NumEls, \ |
4536 | ElBits, NF, IsSigned) \ |
4537 | if (EltTy->hasIntegerRepresentation() && !EltTy->isBooleanType() && \ |
4538 | EltTy->hasSignedIntegerRepresentation() == IsSigned && \ |
4539 | EltTySize == ElBits && NumElts == (NumEls * NF) && NumFields == 1) { \ |
4540 | return SingletonId; \ |
4541 | } |
4542 | #define SVE_VECTOR_TYPE_FLOAT(Name, MangledName, Id, SingletonId, NumEls, \ |
4543 | ElBits, NF) \ |
4544 | if (EltTy->hasFloatingRepresentation() && !EltTy->isBFloat16Type() && \ |
4545 | EltTySize == ElBits && NumElts == (NumEls * NF) && NumFields == 1) { \ |
4546 | return SingletonId; \ |
4547 | } |
4548 | #define SVE_VECTOR_TYPE_BFLOAT(Name, MangledName, Id, SingletonId, NumEls, \ |
4549 | ElBits, NF) \ |
4550 | if (EltTy->hasFloatingRepresentation() && EltTy->isBFloat16Type() && \ |
4551 | EltTySize == ElBits && NumElts == (NumEls * NF) && NumFields == 1) { \ |
4552 | return SingletonId; \ |
4553 | } |
4554 | #define SVE_VECTOR_TYPE_MFLOAT(Name, MangledName, Id, SingletonId, NumEls, \ |
4555 | ElBits, NF) \ |
4556 | if (EltTy->isMFloat8Type() && EltTySize == ElBits && \ |
4557 | NumElts == (NumEls * NF) && NumFields == 1) { \ |
4558 | return SingletonId; \ |
4559 | } |
4560 | #define SVE_PREDICATE_TYPE_ALL(Name, MangledName, Id, SingletonId, NumEls, NF) \ |
4561 | if (EltTy->isBooleanType() && NumElts == (NumEls * NF) && NumFields == 1) \ |
4562 | return SingletonId; |
4563 | #include "clang/Basic/AArch64ACLETypes.def" |
4564 | } else if (Target->hasRISCVVTypes()) { |
4565 | uint64_t EltTySize = getTypeSize(T: EltTy); |
4566 | #define RVV_VECTOR_TYPE(Name, Id, SingletonId, NumEls, ElBits, NF, IsSigned, \ |
4567 | IsFP, IsBF) \ |
4568 | if (!EltTy->isBooleanType() && \ |
4569 | ((EltTy->hasIntegerRepresentation() && \ |
4570 | EltTy->hasSignedIntegerRepresentation() == IsSigned) || \ |
4571 | (EltTy->hasFloatingRepresentation() && !EltTy->isBFloat16Type() && \ |
4572 | IsFP && !IsBF) || \ |
4573 | (EltTy->hasFloatingRepresentation() && EltTy->isBFloat16Type() && \ |
4574 | IsBF && !IsFP)) && \ |
4575 | EltTySize == ElBits && NumElts == NumEls && NumFields == NF) \ |
4576 | return SingletonId; |
4577 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, NumEls) \ |
4578 | if (EltTy->isBooleanType() && NumElts == NumEls) \ |
4579 | return SingletonId; |
4580 | #include "clang/Basic/RISCVVTypes.def" |
4581 | } |
4582 | return QualType(); |
4583 | } |
4584 | |
4585 | /// getVectorType - Return the unique reference to a vector type of |
4586 | /// the specified element type and size. VectorType must be a built-in type. |
4587 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
4588 | VectorKind VecKind) const { |
4589 | assert(vecType->isBuiltinType() || |
4590 | (vecType->isBitIntType() && |
4591 | // Only support _BitInt elements with byte-sized power of 2 NumBits. |
4592 | llvm::isPowerOf2_32(vecType->castAs<BitIntType>()->getNumBits()))); |
4593 | |
4594 | // Check if we've already instantiated a vector of this type. |
4595 | llvm::FoldingSetNodeID ID; |
4596 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
4597 | |
4598 | void *InsertPos = nullptr; |
4599 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4600 | return QualType(VTP, 0); |
4601 | |
4602 | // If the element type isn't canonical, this won't be a canonical type either, |
4603 | // so fill in the canonical type field. |
4604 | QualType Canonical; |
4605 | if (!vecType.isCanonical()) { |
4606 | Canonical = getVectorType(vecType: getCanonicalType(T: vecType), NumElts, VecKind); |
4607 | |
4608 | // Get the new insert position for the node we care about. |
4609 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4610 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
4611 | } |
4612 | auto *New = new (*this, alignof(VectorType)) |
4613 | VectorType(vecType, NumElts, Canonical, VecKind); |
4614 | VectorTypes.InsertNode(N: New, InsertPos); |
4615 | Types.push_back(New); |
4616 | return QualType(New, 0); |
4617 | } |
4618 | |
4619 | QualType ASTContext::getDependentVectorType(QualType VecType, Expr *SizeExpr, |
4620 | SourceLocation AttrLoc, |
4621 | VectorKind VecKind) const { |
4622 | llvm::FoldingSetNodeID ID; |
4623 | DependentVectorType::Profile(ID, Context: *this, ElementType: getCanonicalType(T: VecType), SizeExpr, |
4624 | VecKind); |
4625 | void *InsertPos = nullptr; |
4626 | DependentVectorType *Canon = |
4627 | DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4628 | DependentVectorType *New; |
4629 | |
4630 | if (Canon) { |
4631 | New = new (*this, alignof(DependentVectorType)) DependentVectorType( |
4632 | VecType, QualType(Canon, 0), SizeExpr, AttrLoc, VecKind); |
4633 | } else { |
4634 | QualType CanonVecTy = getCanonicalType(T: VecType); |
4635 | if (CanonVecTy == VecType) { |
4636 | New = new (*this, alignof(DependentVectorType)) |
4637 | DependentVectorType(VecType, QualType(), SizeExpr, AttrLoc, VecKind); |
4638 | |
4639 | DependentVectorType *CanonCheck = |
4640 | DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4641 | assert(!CanonCheck && |
4642 | "Dependent-sized vector_size canonical type broken"); |
4643 | (void)CanonCheck; |
4644 | DependentVectorTypes.InsertNode(N: New, InsertPos); |
4645 | } else { |
4646 | QualType CanonTy = getDependentVectorType(VecType: CanonVecTy, SizeExpr, |
4647 | AttrLoc: SourceLocation(), VecKind); |
4648 | New = new (*this, alignof(DependentVectorType)) |
4649 | DependentVectorType(VecType, CanonTy, SizeExpr, AttrLoc, VecKind); |
4650 | } |
4651 | } |
4652 | |
4653 | Types.push_back(New); |
4654 | return QualType(New, 0); |
4655 | } |
4656 | |
4657 | /// getExtVectorType - Return the unique reference to an extended vector type of |
4658 | /// the specified element type and size. VectorType must be a built-in type. |
4659 | QualType ASTContext::getExtVectorType(QualType vecType, |
4660 | unsigned NumElts) const { |
4661 | assert(vecType->isBuiltinType() || vecType->isDependentType() || |
4662 | (vecType->isBitIntType() && |
4663 | // Only support _BitInt elements with byte-sized power of 2 NumBits. |
4664 | llvm::isPowerOf2_32(vecType->castAs<BitIntType>()->getNumBits()))); |
4665 | |
4666 | // Check if we've already instantiated a vector of this type. |
4667 | llvm::FoldingSetNodeID ID; |
4668 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
4669 | VectorKind::Generic); |
4670 | void *InsertPos = nullptr; |
4671 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4672 | return QualType(VTP, 0); |
4673 | |
4674 | // If the element type isn't canonical, this won't be a canonical type either, |
4675 | // so fill in the canonical type field. |
4676 | QualType Canonical; |
4677 | if (!vecType.isCanonical()) { |
4678 | Canonical = getExtVectorType(vecType: getCanonicalType(T: vecType), NumElts); |
4679 | |
4680 | // Get the new insert position for the node we care about. |
4681 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4682 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
4683 | } |
4684 | auto *New = new (*this, alignof(ExtVectorType)) |
4685 | ExtVectorType(vecType, NumElts, Canonical); |
4686 | VectorTypes.InsertNode(New, InsertPos); |
4687 | Types.push_back(New); |
4688 | return QualType(New, 0); |
4689 | } |
4690 | |
4691 | QualType |
4692 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
4693 | Expr *SizeExpr, |
4694 | SourceLocation AttrLoc) const { |
4695 | llvm::FoldingSetNodeID ID; |
4696 | DependentSizedExtVectorType::Profile(ID, Context: *this, ElementType: getCanonicalType(T: vecType), |
4697 | SizeExpr); |
4698 | |
4699 | void *InsertPos = nullptr; |
4700 | DependentSizedExtVectorType *Canon |
4701 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4702 | DependentSizedExtVectorType *New; |
4703 | if (Canon) { |
4704 | // We already have a canonical version of this array type; use it as |
4705 | // the canonical type for a newly-built type. |
4706 | New = new (*this, alignof(DependentSizedExtVectorType)) |
4707 | DependentSizedExtVectorType(vecType, QualType(Canon, 0), SizeExpr, |
4708 | AttrLoc); |
4709 | } else { |
4710 | QualType CanonVecTy = getCanonicalType(T: vecType); |
4711 | if (CanonVecTy == vecType) { |
4712 | New = new (*this, alignof(DependentSizedExtVectorType)) |
4713 | DependentSizedExtVectorType(vecType, QualType(), SizeExpr, AttrLoc); |
4714 | |
4715 | DependentSizedExtVectorType *CanonCheck |
4716 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4717 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
4718 | (void)CanonCheck; |
4719 | DependentSizedExtVectorTypes.InsertNode(N: New, InsertPos); |
4720 | } else { |
4721 | QualType CanonExtTy = getDependentSizedExtVectorType(vecType: CanonVecTy, SizeExpr, |
4722 | AttrLoc: SourceLocation()); |
4723 | New = new (*this, alignof(DependentSizedExtVectorType)) |
4724 | DependentSizedExtVectorType(vecType, CanonExtTy, SizeExpr, AttrLoc); |
4725 | } |
4726 | } |
4727 | |
4728 | Types.push_back(New); |
4729 | return QualType(New, 0); |
4730 | } |
4731 | |
4732 | QualType ASTContext::getConstantMatrixType(QualType ElementTy, unsigned NumRows, |
4733 | unsigned NumColumns) const { |
4734 | llvm::FoldingSetNodeID ID; |
4735 | ConstantMatrixType::Profile(ID, ElementTy, NumRows, NumColumns, |
4736 | Type::ConstantMatrix); |
4737 | |
4738 | assert(MatrixType::isValidElementType(ElementTy) && |
4739 | "need a valid element type"); |
4740 | assert(ConstantMatrixType::isDimensionValid(NumRows) && |
4741 | ConstantMatrixType::isDimensionValid(NumColumns) && |
4742 | "need valid matrix dimensions"); |
4743 | void *InsertPos = nullptr; |
4744 | if (ConstantMatrixType *MTP = MatrixTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4745 | return QualType(MTP, 0); |
4746 | |
4747 | QualType Canonical; |
4748 | if (!ElementTy.isCanonical()) { |
4749 | Canonical = |
4750 | getConstantMatrixType(ElementTy: getCanonicalType(T: ElementTy), NumRows, NumColumns); |
4751 | |
4752 | ConstantMatrixType *NewIP = MatrixTypes.FindNodeOrInsertPos(ID, InsertPos); |
4753 | assert(!NewIP && "Matrix type shouldn't already exist in the map"); |
4754 | (void)NewIP; |
4755 | } |
4756 | |
4757 | auto *New = new (*this, alignof(ConstantMatrixType)) |
4758 | ConstantMatrixType(ElementTy, NumRows, NumColumns, Canonical); |
4759 | MatrixTypes.InsertNode(N: New, InsertPos); |
4760 | Types.push_back(New); |
4761 | return QualType(New, 0); |
4762 | } |
4763 | |
4764 | QualType ASTContext::getDependentSizedMatrixType(QualType ElementTy, |
4765 | Expr *RowExpr, |
4766 | Expr *ColumnExpr, |
4767 | SourceLocation AttrLoc) const { |
4768 | QualType CanonElementTy = getCanonicalType(T: ElementTy); |
4769 | llvm::FoldingSetNodeID ID; |
4770 | DependentSizedMatrixType::Profile(ID, Context: *this, ElementType: CanonElementTy, RowExpr, |
4771 | ColumnExpr); |
4772 | |
4773 | void *InsertPos = nullptr; |
4774 | DependentSizedMatrixType *Canon = |
4775 | DependentSizedMatrixTypes.FindNodeOrInsertPos(ID, InsertPos); |
4776 | |
4777 | if (!Canon) { |
4778 | Canon = new (*this, alignof(DependentSizedMatrixType)) |
4779 | DependentSizedMatrixType(CanonElementTy, QualType(), RowExpr, |
4780 | ColumnExpr, AttrLoc); |
4781 | #ifndef NDEBUG |
4782 | DependentSizedMatrixType *CanonCheck = |
4783 | DependentSizedMatrixTypes.FindNodeOrInsertPos(ID, InsertPos); |
4784 | assert(!CanonCheck && "Dependent-sized matrix canonical type broken"); |
4785 | #endif |
4786 | DependentSizedMatrixTypes.InsertNode(N: Canon, InsertPos); |
4787 | Types.push_back(Canon); |
4788 | } |
4789 | |
4790 | // Already have a canonical version of the matrix type |
4791 | // |
4792 | // If it exactly matches the requested type, use it directly. |
4793 | if (Canon->getElementType() == ElementTy && Canon->getRowExpr() == RowExpr && |
4794 | Canon->getRowExpr() == ColumnExpr) |
4795 | return QualType(Canon, 0); |
4796 | |
4797 | // Use Canon as the canonical type for newly-built type. |
4798 | DependentSizedMatrixType *New = new (*this, alignof(DependentSizedMatrixType)) |
4799 | DependentSizedMatrixType(ElementTy, QualType(Canon, 0), RowExpr, |
4800 | ColumnExpr, AttrLoc); |
4801 | Types.push_back(New); |
4802 | return QualType(New, 0); |
4803 | } |
4804 | |
4805 | QualType ASTContext::getDependentAddressSpaceType(QualType PointeeType, |
4806 | Expr *AddrSpaceExpr, |
4807 | SourceLocation AttrLoc) const { |
4808 | assert(AddrSpaceExpr->isInstantiationDependent()); |
4809 | |
4810 | QualType canonPointeeType = getCanonicalType(T: PointeeType); |
4811 | |
4812 | void *insertPos = nullptr; |
4813 | llvm::FoldingSetNodeID ID; |
4814 | DependentAddressSpaceType::Profile(ID, Context: *this, PointeeType: canonPointeeType, |
4815 | AddrSpaceExpr); |
4816 | |
4817 | DependentAddressSpaceType *canonTy = |
4818 | DependentAddressSpaceTypes.FindNodeOrInsertPos(ID, InsertPos&: insertPos); |
4819 | |
4820 | if (!canonTy) { |
4821 | canonTy = new (*this, alignof(DependentAddressSpaceType)) |
4822 | DependentAddressSpaceType(canonPointeeType, QualType(), AddrSpaceExpr, |
4823 | AttrLoc); |
4824 | DependentAddressSpaceTypes.InsertNode(N: canonTy, InsertPos: insertPos); |
4825 | Types.push_back(canonTy); |
4826 | } |
4827 | |
4828 | if (canonPointeeType == PointeeType && |
4829 | canonTy->getAddrSpaceExpr() == AddrSpaceExpr) |
4830 | return QualType(canonTy, 0); |
4831 | |
4832 | auto *sugaredType = new (*this, alignof(DependentAddressSpaceType)) |
4833 | DependentAddressSpaceType(PointeeType, QualType(canonTy, 0), |
4834 | AddrSpaceExpr, AttrLoc); |
4835 | Types.push_back(Elt: sugaredType); |
4836 | return QualType(sugaredType, 0); |
4837 | } |
4838 | |
4839 | /// Determine whether \p T is canonical as the result type of a function. |
4840 | static bool isCanonicalResultType(QualType T) { |
4841 | return T.isCanonical() && |
4842 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
4843 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
4844 | } |
4845 | |
4846 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
4847 | QualType |
4848 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
4849 | const FunctionType::ExtInfo &Info) const { |
4850 | // FIXME: This assertion cannot be enabled (yet) because the ObjC rewriter |
4851 | // functionality creates a function without a prototype regardless of |
4852 | // language mode (so it makes them even in C++). Once the rewriter has been |
4853 | // fixed, this assertion can be enabled again. |
4854 | //assert(!LangOpts.requiresStrictPrototypes() && |
4855 | // "strict prototypes are disabled"); |
4856 | |
4857 | // Unique functions, to guarantee there is only one function of a particular |
4858 | // structure. |
4859 | llvm::FoldingSetNodeID ID; |
4860 | FunctionNoProtoType::Profile(ID, ResultType: ResultTy, Info); |
4861 | |
4862 | void *InsertPos = nullptr; |
4863 | if (FunctionNoProtoType *FT = |
4864 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4865 | return QualType(FT, 0); |
4866 | |
4867 | QualType Canonical; |
4868 | if (!isCanonicalResultType(T: ResultTy)) { |
4869 | Canonical = |
4870 | getFunctionNoProtoType(ResultTy: getCanonicalFunctionResultType(ResultType: ResultTy), Info); |
4871 | |
4872 | // Get the new insert position for the node we care about. |
4873 | FunctionNoProtoType *NewIP = |
4874 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
4875 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
4876 | } |
4877 | |
4878 | auto *New = new (*this, alignof(FunctionNoProtoType)) |
4879 | FunctionNoProtoType(ResultTy, Canonical, Info); |
4880 | Types.push_back(New); |
4881 | FunctionNoProtoTypes.InsertNode(N: New, InsertPos); |
4882 | return QualType(New, 0); |
4883 | } |
4884 | |
4885 | CanQualType |
4886 | ASTContext::getCanonicalFunctionResultType(QualType ResultType) const { |
4887 | CanQualType CanResultType = getCanonicalType(T: ResultType); |
4888 | |
4889 | // Canonical result types do not have ARC lifetime qualifiers. |
4890 | if (CanResultType.getQualifiers().hasObjCLifetime()) { |
4891 | Qualifiers Qs = CanResultType.getQualifiers(); |
4892 | Qs.removeObjCLifetime(); |
4893 | return CanQualType::CreateUnsafe( |
4894 | Other: getQualifiedType(T: CanResultType.getUnqualifiedType(), Qs)); |
4895 | } |
4896 | |
4897 | return CanResultType; |
4898 | } |
4899 | |
4900 | static bool isCanonicalExceptionSpecification( |
4901 | const FunctionProtoType::ExceptionSpecInfo &ESI, bool NoexceptInType) { |
4902 | if (ESI.Type == EST_None) |
4903 | return true; |
4904 | if (!NoexceptInType) |
4905 | return false; |
4906 | |
4907 | // C++17 onwards: exception specification is part of the type, as a simple |
4908 | // boolean "can this function type throw". |
4909 | if (ESI.Type == EST_BasicNoexcept) |
4910 | return true; |
4911 | |
4912 | // A noexcept(expr) specification is (possibly) canonical if expr is |
4913 | // value-dependent. |
4914 | if (ESI.Type == EST_DependentNoexcept) |
4915 | return true; |
4916 | |
4917 | // A dynamic exception specification is canonical if it only contains pack |
4918 | // expansions (so we can't tell whether it's non-throwing) and all its |
4919 | // contained types are canonical. |
4920 | if (ESI.Type == EST_Dynamic) { |
4921 | bool AnyPackExpansions = false; |
4922 | for (QualType ET : ESI.Exceptions) { |
4923 | if (!ET.isCanonical()) |
4924 | return false; |
4925 | if (ET->getAs<PackExpansionType>()) |
4926 | AnyPackExpansions = true; |
4927 | } |
4928 | return AnyPackExpansions; |
4929 | } |
4930 | |
4931 | return false; |
4932 | } |
4933 | |
4934 | QualType ASTContext::getFunctionTypeInternal( |
4935 | QualType ResultTy, ArrayRef<QualType> ArgArray, |
4936 | const FunctionProtoType::ExtProtoInfo &EPI, bool OnlyWantCanonical) const { |
4937 | size_t NumArgs = ArgArray.size(); |
4938 | |
4939 | // Unique functions, to guarantee there is only one function of a particular |
4940 | // structure. |
4941 | llvm::FoldingSetNodeID ID; |
4942 | FunctionProtoType::Profile(ID, Result: ResultTy, ArgTys: ArgArray.begin(), NumArgs, EPI, |
4943 | Context: *this, Canonical: true); |
4944 | |
4945 | QualType Canonical; |
4946 | bool Unique = false; |
4947 | |
4948 | void *InsertPos = nullptr; |
4949 | if (FunctionProtoType *FPT = |
4950 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) { |
4951 | QualType Existing = QualType(FPT, 0); |
4952 | |
4953 | // If we find a pre-existing equivalent FunctionProtoType, we can just reuse |
4954 | // it so long as our exception specification doesn't contain a dependent |
4955 | // noexcept expression, or we're just looking for a canonical type. |
4956 | // Otherwise, we're going to need to create a type |
4957 | // sugar node to hold the concrete expression. |
4958 | if (OnlyWantCanonical || !isComputedNoexcept(EPI.ExceptionSpec.Type) || |
4959 | EPI.ExceptionSpec.NoexceptExpr == FPT->getNoexceptExpr()) |
4960 | return Existing; |
4961 | |
4962 | // We need a new type sugar node for this one, to hold the new noexcept |
4963 | // expression. We do no canonicalization here, but that's OK since we don't |
4964 | // expect to see the same noexcept expression much more than once. |
4965 | Canonical = getCanonicalType(T: Existing); |
4966 | Unique = true; |
4967 | } |
4968 | |
4969 | bool NoexceptInType = getLangOpts().CPlusPlus17; |
4970 | bool IsCanonicalExceptionSpec = |
4971 | isCanonicalExceptionSpecification(EPI.ExceptionSpec, NoexceptInType); |
4972 | |
4973 | // Determine whether the type being created is already canonical or not. |
4974 | bool isCanonical = !Unique && IsCanonicalExceptionSpec && |
4975 | isCanonicalResultType(T: ResultTy) && !EPI.HasTrailingReturn; |
4976 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
4977 | if (!ArgArray[i].isCanonicalAsParam()) |
4978 | isCanonical = false; |
4979 | |
4980 | if (OnlyWantCanonical) |
4981 | assert(isCanonical && |
4982 | "given non-canonical parameters constructing canonical type"); |
4983 | |
4984 | // If this type isn't canonical, get the canonical version of it if we don't |
4985 | // already have it. The exception spec is only partially part of the |
4986 | // canonical type, and only in C++17 onwards. |
4987 | if (!isCanonical && Canonical.isNull()) { |
4988 | SmallVector<QualType, 16> CanonicalArgs; |
4989 | CanonicalArgs.reserve(N: NumArgs); |
4990 | for (unsigned i = 0; i != NumArgs; ++i) |
4991 | CanonicalArgs.push_back(Elt: getCanonicalParamType(T: ArgArray[i])); |
4992 | |
4993 | llvm::SmallVector<QualType, 8> ExceptionTypeStorage; |
4994 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
4995 | CanonicalEPI.HasTrailingReturn = false; |
4996 | |
4997 | if (IsCanonicalExceptionSpec) { |
4998 | // Exception spec is already OK. |
4999 | } else if (NoexceptInType) { |
5000 | switch (EPI.ExceptionSpec.Type) { |
5001 | case EST_Unparsed: case EST_Unevaluated: case EST_Uninstantiated: |
5002 | // We don't know yet. It shouldn't matter what we pick here; no-one |
5003 | // should ever look at this. |
5004 | [[fallthrough]]; |
5005 | case EST_None: case EST_MSAny: case EST_NoexceptFalse: |
5006 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
5007 | break; |
5008 | |
5009 | // A dynamic exception specification is almost always "not noexcept", |
5010 | // with the exception that a pack expansion might expand to no types. |
5011 | case EST_Dynamic: { |
5012 | bool AnyPacks = false; |
5013 | for (QualType ET : EPI.ExceptionSpec.Exceptions) { |
5014 | if (ET->getAs<PackExpansionType>()) |
5015 | AnyPacks = true; |
5016 | ExceptionTypeStorage.push_back(getCanonicalType(ET)); |
5017 | } |
5018 | if (!AnyPacks) |
5019 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
5020 | else { |
5021 | CanonicalEPI.ExceptionSpec.Type = EST_Dynamic; |
5022 | CanonicalEPI.ExceptionSpec.Exceptions = ExceptionTypeStorage; |
5023 | } |
5024 | break; |
5025 | } |
5026 | |
5027 | case EST_DynamicNone: |
5028 | case EST_BasicNoexcept: |
5029 | case EST_NoexceptTrue: |
5030 | case EST_NoThrow: |
5031 | CanonicalEPI.ExceptionSpec.Type = EST_BasicNoexcept; |
5032 | break; |
5033 | |
5034 | case EST_DependentNoexcept: |
5035 | llvm_unreachable("dependent noexcept is already canonical"); |
5036 | } |
5037 | } else { |
5038 | CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo(); |
5039 | } |
5040 | |
5041 | // Adjust the canonical function result type. |
5042 | CanQualType CanResultTy = getCanonicalFunctionResultType(ResultType: ResultTy); |
5043 | Canonical = |
5044 | getFunctionTypeInternal(ResultTy: CanResultTy, ArgArray: CanonicalArgs, EPI: CanonicalEPI, OnlyWantCanonical: true); |
5045 | |
5046 | // Get the new insert position for the node we care about. |
5047 | FunctionProtoType *NewIP = |
5048 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
5049 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
5050 | } |
5051 | |
5052 | // Compute the needed size to hold this FunctionProtoType and the |
5053 | // various trailing objects. |
5054 | auto ESH = FunctionProtoType::getExceptionSpecSize( |
5055 | EPI.ExceptionSpec.Type, EPI.ExceptionSpec.Exceptions.size()); |
5056 | size_t Size = FunctionProtoType::totalSizeToAlloc< |
5057 | QualType, SourceLocation, FunctionType::FunctionTypeExtraBitfields, |
5058 | FunctionType::FunctionTypeArmAttributes, FunctionType::ExceptionType, |
5059 | Expr *, FunctionDecl *, FunctionProtoType::ExtParameterInfo, Qualifiers, |
5060 | FunctionEffect, EffectConditionExpr>( |
5061 | NumArgs, EPI.Variadic, EPI.requiresFunctionProtoTypeExtraBitfields(), |
5062 | EPI.requiresFunctionProtoTypeArmAttributes(), ESH.NumExceptionType, |
5063 | ESH.NumExprPtr, ESH.NumFunctionDeclPtr, |
5064 | EPI.ExtParameterInfos ? NumArgs : 0, |
5065 | EPI.TypeQuals.hasNonFastQualifiers() ? 1 : 0, EPI.FunctionEffects.size(), |
5066 | EPI.FunctionEffects.conditions().size()); |
5067 | |
5068 | auto *FTP = (FunctionProtoType *)Allocate(Size, Align: alignof(FunctionProtoType)); |
5069 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
5070 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
5071 | Types.push_back(FTP); |
5072 | if (!Unique) |
5073 | FunctionProtoTypes.InsertNode(N: FTP, InsertPos); |
5074 | if (!EPI.FunctionEffects.empty()) |
5075 | AnyFunctionEffects = true; |
5076 | return QualType(FTP, 0); |
5077 | } |
5078 | |
5079 | QualType ASTContext::getPipeType(QualType T, bool ReadOnly) const { |
5080 | llvm::FoldingSetNodeID ID; |
5081 | PipeType::Profile(ID, T, isRead: ReadOnly); |
5082 | |
5083 | void *InsertPos = nullptr; |
5084 | if (PipeType *PT = PipeTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5085 | return QualType(PT, 0); |
5086 | |
5087 | // If the pipe element type isn't canonical, this won't be a canonical type |
5088 | // either, so fill in the canonical type field. |
5089 | QualType Canonical; |
5090 | if (!T.isCanonical()) { |
5091 | Canonical = getPipeType(T: getCanonicalType(T), ReadOnly); |
5092 | |
5093 | // Get the new insert position for the node we care about. |
5094 | PipeType *NewIP = PipeTypes.FindNodeOrInsertPos(ID, InsertPos); |
5095 | assert(!NewIP && "Shouldn't be in the map!"); |
5096 | (void)NewIP; |
5097 | } |
5098 | auto *New = new (*this, alignof(PipeType)) PipeType(T, Canonical, ReadOnly); |
5099 | Types.push_back(New); |
5100 | PipeTypes.InsertNode(N: New, InsertPos); |
5101 | return QualType(New, 0); |
5102 | } |
5103 | |
5104 | QualType ASTContext::adjustStringLiteralBaseType(QualType Ty) const { |
5105 | // OpenCL v1.1 s6.5.3: a string literal is in the constant address space. |
5106 | return LangOpts.OpenCL ? getAddrSpaceQualType(T: Ty, AddressSpace: LangAS::opencl_constant) |
5107 | : Ty; |
5108 | } |
5109 | |
5110 | QualType ASTContext::getReadPipeType(QualType T) const { |
5111 | return getPipeType(T, ReadOnly: true); |
5112 | } |
5113 | |
5114 | QualType ASTContext::getWritePipeType(QualType T) const { |
5115 | return getPipeType(T, ReadOnly: false); |
5116 | } |
5117 | |
5118 | QualType ASTContext::getBitIntType(bool IsUnsigned, unsigned NumBits) const { |
5119 | llvm::FoldingSetNodeID ID; |
5120 | BitIntType::Profile(ID, IsUnsigned, NumBits); |
5121 | |
5122 | void *InsertPos = nullptr; |
5123 | if (BitIntType *EIT = BitIntTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5124 | return QualType(EIT, 0); |
5125 | |
5126 | auto *New = new (*this, alignof(BitIntType)) BitIntType(IsUnsigned, NumBits); |
5127 | BitIntTypes.InsertNode(N: New, InsertPos); |
5128 | Types.push_back(New); |
5129 | return QualType(New, 0); |
5130 | } |
5131 | |
5132 | QualType ASTContext::getDependentBitIntType(bool IsUnsigned, |
5133 | Expr *NumBitsExpr) const { |
5134 | assert(NumBitsExpr->isInstantiationDependent() && "Only good for dependent"); |
5135 | llvm::FoldingSetNodeID ID; |
5136 | DependentBitIntType::Profile(ID, Context: *this, IsUnsigned, NumBitsExpr); |
5137 | |
5138 | void *InsertPos = nullptr; |
5139 | if (DependentBitIntType *Existing = |
5140 | DependentBitIntTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5141 | return QualType(Existing, 0); |
5142 | |
5143 | auto *New = new (*this, alignof(DependentBitIntType)) |
5144 | DependentBitIntType(IsUnsigned, NumBitsExpr); |
5145 | DependentBitIntTypes.InsertNode(N: New, InsertPos); |
5146 | |
5147 | Types.push_back(New); |
5148 | return QualType(New, 0); |
5149 | } |
5150 | |
5151 | #ifndef NDEBUG |
5152 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
5153 | if (!isa<CXXRecordDecl>(Val: D)) return false; |
5154 | const auto *RD = cast<CXXRecordDecl>(Val: D); |
5155 | if (isa<ClassTemplatePartialSpecializationDecl>(Val: RD)) |
5156 | return true; |
5157 | if (RD->getDescribedClassTemplate() && |
5158 | !isa<ClassTemplateSpecializationDecl>(Val: RD)) |
5159 | return true; |
5160 | return false; |
5161 | } |
5162 | #endif |
5163 | |
5164 | /// getInjectedClassNameType - Return the unique reference to the |
5165 | /// injected class name type for the specified templated declaration. |
5166 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
5167 | QualType TST) const { |
5168 | assert(NeedsInjectedClassNameType(Decl)); |
5169 | if (Decl->TypeForDecl) { |
5170 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
5171 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
5172 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
5173 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
5174 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
5175 | } else { |
5176 | Type *newType = new (*this, alignof(InjectedClassNameType)) |
5177 | InjectedClassNameType(Decl, TST); |
5178 | Decl->TypeForDecl = newType; |
5179 | Types.push_back(Elt: newType); |
5180 | } |
5181 | return QualType(Decl->TypeForDecl, 0); |
5182 | } |
5183 | |
5184 | /// getTypeDeclType - Return the unique reference to the type for the |
5185 | /// specified type declaration. |
5186 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
5187 | assert(Decl && "Passed null for Decl param"); |
5188 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
5189 | |
5190 | if (const auto *Typedef = dyn_cast<TypedefNameDecl>(Val: Decl)) |
5191 | return getTypedefType(Decl: Typedef); |
5192 | |
5193 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
5194 | "Template type parameter types are always available."); |
5195 | |
5196 | if (const auto *Record = dyn_cast<RecordDecl>(Val: Decl)) { |
5197 | assert(Record->isFirstDecl() && "struct/union has previous declaration"); |
5198 | assert(!NeedsInjectedClassNameType(Record)); |
5199 | return getRecordType(Decl: Record); |
5200 | } else if (const auto *Enum = dyn_cast<EnumDecl>(Val: Decl)) { |
5201 | assert(Enum->isFirstDecl() && "enum has previous declaration"); |
5202 | return getEnumType(Decl: Enum); |
5203 | } else if (const auto *Using = dyn_cast<UnresolvedUsingTypenameDecl>(Val: Decl)) { |
5204 | return getUnresolvedUsingType(Decl: Using); |
5205 | } else |
5206 | llvm_unreachable("TypeDecl without a type?"); |
5207 | |
5208 | return QualType(Decl->TypeForDecl, 0); |
5209 | } |
5210 | |
5211 | /// getTypedefType - Return the unique reference to the type for the |
5212 | /// specified typedef name decl. |
5213 | QualType ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
5214 | QualType Underlying) const { |
5215 | if (!Decl->TypeForDecl) { |
5216 | if (Underlying.isNull()) |
5217 | Underlying = Decl->getUnderlyingType(); |
5218 | auto *NewType = new (*this, alignof(TypedefType)) TypedefType( |
5219 | Type::Typedef, Decl, Underlying, /*HasTypeDifferentFromDecl=*/false); |
5220 | Decl->TypeForDecl = NewType; |
5221 | Types.push_back(Elt: NewType); |
5222 | return QualType(NewType, 0); |
5223 | } |
5224 | if (Underlying.isNull() || Decl->getUnderlyingType() == Underlying) |
5225 | return QualType(Decl->TypeForDecl, 0); |
5226 | assert(hasSameType(Decl->getUnderlyingType(), Underlying)); |
5227 | |
5228 | llvm::FoldingSetNodeID ID; |
5229 | TypedefType::Profile(ID, Decl, Underlying); |
5230 | |
5231 | void *InsertPos = nullptr; |
5232 | if (TypedefType *T = TypedefTypes.FindNodeOrInsertPos(ID, InsertPos)) { |
5233 | assert(!T->typeMatchesDecl() && |
5234 | "non-divergent case should be handled with TypeDecl"); |
5235 | return QualType(T, 0); |
5236 | } |
5237 | |
5238 | void *Mem = Allocate(TypedefType::totalSizeToAlloc<QualType>(true), |
5239 | alignof(TypedefType)); |
5240 | auto *NewType = new (Mem) TypedefType(Type::Typedef, Decl, Underlying, |
5241 | /*HasTypeDifferentFromDecl=*/true); |
5242 | TypedefTypes.InsertNode(NewType, InsertPos); |
5243 | Types.push_back(Elt: NewType); |
5244 | return QualType(NewType, 0); |
5245 | } |
5246 | |
5247 | QualType ASTContext::getUsingType(const UsingShadowDecl *Found, |
5248 | QualType Underlying) const { |
5249 | llvm::FoldingSetNodeID ID; |
5250 | UsingType::Profile(ID, Found, Underlying); |
5251 | |
5252 | void *InsertPos = nullptr; |
5253 | if (UsingType *T = UsingTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5254 | return QualType(T, 0); |
5255 | |
5256 | const Type *TypeForDecl = |
5257 | cast<TypeDecl>(Val: Found->getTargetDecl())->getTypeForDecl(); |
5258 | |
5259 | assert(!Underlying.hasLocalQualifiers()); |
5260 | QualType Canon = Underlying->getCanonicalTypeInternal(); |
5261 | assert(TypeForDecl->getCanonicalTypeInternal() == Canon); |
5262 | |
5263 | if (Underlying.getTypePtr() == TypeForDecl) |
5264 | Underlying = QualType(); |
5265 | void *Mem = |
5266 | Allocate(UsingType::totalSizeToAlloc<QualType>(!Underlying.isNull()), |
5267 | alignof(UsingType)); |
5268 | UsingType *NewType = new (Mem) UsingType(Found, Underlying, Canon); |
5269 | Types.push_back(NewType); |
5270 | UsingTypes.InsertNode(N: NewType, InsertPos); |
5271 | return QualType(NewType, 0); |
5272 | } |
5273 | |
5274 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
5275 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
5276 | |
5277 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
5278 | if (PrevDecl->TypeForDecl) |
5279 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
5280 | |
5281 | auto *newType = new (*this, alignof(RecordType)) RecordType(Decl); |
5282 | Decl->TypeForDecl = newType; |
5283 | Types.push_back(newType); |
5284 | return QualType(newType, 0); |
5285 | } |
5286 | |
5287 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
5288 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
5289 | |
5290 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
5291 | if (PrevDecl->TypeForDecl) |
5292 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
5293 | |
5294 | auto *newType = new (*this, alignof(EnumType)) EnumType(Decl); |
5295 | Decl->TypeForDecl = newType; |
5296 | Types.push_back(newType); |
5297 | return QualType(newType, 0); |
5298 | } |
5299 | |
5300 | bool ASTContext::computeBestEnumTypes(bool IsPacked, unsigned NumNegativeBits, |
5301 | unsigned NumPositiveBits, |
5302 | QualType &BestType, |
5303 | QualType &BestPromotionType) { |
5304 | unsigned IntWidth = Target->getIntWidth(); |
5305 | unsigned CharWidth = Target->getCharWidth(); |
5306 | unsigned ShortWidth = Target->getShortWidth(); |
5307 | bool EnumTooLarge = false; |
5308 | unsigned BestWidth; |
5309 | if (NumNegativeBits) { |
5310 | // If there is a negative value, figure out the smallest integer type (of |
5311 | // int/long/longlong) that fits. |
5312 | // If it's packed, check also if it fits a char or a short. |
5313 | if (IsPacked && NumNegativeBits <= CharWidth && |
5314 | NumPositiveBits < CharWidth) { |
5315 | BestType = SignedCharTy; |
5316 | BestWidth = CharWidth; |
5317 | } else if (IsPacked && NumNegativeBits <= ShortWidth && |
5318 | NumPositiveBits < ShortWidth) { |
5319 | BestType = ShortTy; |
5320 | BestWidth = ShortWidth; |
5321 | } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) { |
5322 | BestType = IntTy; |
5323 | BestWidth = IntWidth; |
5324 | } else { |
5325 | BestWidth = Target->getLongWidth(); |
5326 | |
5327 | if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) { |
5328 | BestType = LongTy; |
5329 | } else { |
5330 | BestWidth = Target->getLongLongWidth(); |
5331 | |
5332 | if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth) |
5333 | EnumTooLarge = true; |
5334 | BestType = LongLongTy; |
5335 | } |
5336 | } |
5337 | BestPromotionType = (BestWidth <= IntWidth ? IntTy : BestType); |
5338 | } else { |
5339 | // If there is no negative value, figure out the smallest type that fits |
5340 | // all of the enumerator values. |
5341 | // If it's packed, check also if it fits a char or a short. |
5342 | if (IsPacked && NumPositiveBits <= CharWidth) { |
5343 | BestType = UnsignedCharTy; |
5344 | BestPromotionType = IntTy; |
5345 | BestWidth = CharWidth; |
5346 | } else if (IsPacked && NumPositiveBits <= ShortWidth) { |
5347 | BestType = UnsignedShortTy; |
5348 | BestPromotionType = IntTy; |
5349 | BestWidth = ShortWidth; |
5350 | } else if (NumPositiveBits <= IntWidth) { |
5351 | BestType = UnsignedIntTy; |
5352 | BestWidth = IntWidth; |
5353 | BestPromotionType = (NumPositiveBits == BestWidth || !LangOpts.CPlusPlus) |
5354 | ? UnsignedIntTy |
5355 | : IntTy; |
5356 | } else if (NumPositiveBits <= (BestWidth = Target->getLongWidth())) { |
5357 | BestType = UnsignedLongTy; |
5358 | BestPromotionType = (NumPositiveBits == BestWidth || !LangOpts.CPlusPlus) |
5359 | ? UnsignedLongTy |
5360 | : LongTy; |
5361 | } else { |
5362 | BestWidth = Target->getLongLongWidth(); |
5363 | if (NumPositiveBits > BestWidth) { |
5364 | // This can happen with bit-precise integer types, but those are not |
5365 | // allowed as the type for an enumerator per C23 6.7.2.2p4 and p12. |
5366 | // FIXME: GCC uses __int128_t and __uint128_t for cases that fit within |
5367 | // a 128-bit integer, we should consider doing the same. |
5368 | EnumTooLarge = true; |
5369 | } |
5370 | BestType = UnsignedLongLongTy; |
5371 | BestPromotionType = (NumPositiveBits == BestWidth || !LangOpts.CPlusPlus) |
5372 | ? UnsignedLongLongTy |
5373 | : LongLongTy; |
5374 | } |
5375 | } |
5376 | return EnumTooLarge; |
5377 | } |
5378 | |
5379 | bool ASTContext::isRepresentableIntegerValue(llvm::APSInt &Value, QualType T) { |
5380 | assert((T->isIntegralType(*this) || T->isEnumeralType()) && |
5381 | "Integral type required!"); |
5382 | unsigned BitWidth = getIntWidth(T); |
5383 | |
5384 | if (Value.isUnsigned() || Value.isNonNegative()) { |
5385 | if (T->isSignedIntegerOrEnumerationType()) |
5386 | --BitWidth; |
5387 | return Value.getActiveBits() <= BitWidth; |
5388 | } |
5389 | return Value.getSignificantBits() <= BitWidth; |
5390 | } |
5391 | |
5392 | QualType ASTContext::getUnresolvedUsingType( |
5393 | const UnresolvedUsingTypenameDecl *Decl) const { |
5394 | if (Decl->TypeForDecl) |
5395 | return QualType(Decl->TypeForDecl, 0); |
5396 | |
5397 | if (const UnresolvedUsingTypenameDecl *CanonicalDecl = |
5398 | Decl->getCanonicalDecl()) |
5399 | if (CanonicalDecl->TypeForDecl) |
5400 | return QualType(Decl->TypeForDecl = CanonicalDecl->TypeForDecl, 0); |
5401 | |
5402 | Type *newType = |
5403 | new (*this, alignof(UnresolvedUsingType)) UnresolvedUsingType(Decl); |
5404 | Decl->TypeForDecl = newType; |
5405 | Types.push_back(Elt: newType); |
5406 | return QualType(newType, 0); |
5407 | } |
5408 | |
5409 | QualType ASTContext::getAttributedType(attr::Kind attrKind, |
5410 | QualType modifiedType, |
5411 | QualType equivalentType, |
5412 | const Attr *attr) const { |
5413 | llvm::FoldingSetNodeID id; |
5414 | AttributedType::Profile(ID&: id, attrKind, modified: modifiedType, equivalent: equivalentType, attr); |
5415 | |
5416 | void *insertPos = nullptr; |
5417 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(ID: id, InsertPos&: insertPos); |
5418 | if (type) return QualType(type, 0); |
5419 | |
5420 | assert(!attr || attr->getKind() == attrKind); |
5421 | |
5422 | QualType canon = getCanonicalType(T: equivalentType); |
5423 | type = new (*this, alignof(AttributedType)) |
5424 | AttributedType(canon, attrKind, attr, modifiedType, equivalentType); |
5425 | |
5426 | Types.push_back(type); |
5427 | AttributedTypes.InsertNode(N: type, InsertPos: insertPos); |
5428 | |
5429 | return QualType(type, 0); |
5430 | } |
5431 | |
5432 | QualType ASTContext::getAttributedType(const Attr *attr, QualType modifiedType, |
5433 | QualType equivalentType) const { |
5434 | return getAttributedType(attrKind: attr->getKind(), modifiedType, equivalentType, attr); |
5435 | } |
5436 | |
5437 | QualType ASTContext::getAttributedType(NullabilityKind nullability, |
5438 | QualType modifiedType, |
5439 | QualType equivalentType) { |
5440 | switch (nullability) { |
5441 | case NullabilityKind::NonNull: |
5442 | return getAttributedType(attr::TypeNonNull, modifiedType, equivalentType); |
5443 | |
5444 | case NullabilityKind::Nullable: |
5445 | return getAttributedType(attr::TypeNullable, modifiedType, equivalentType); |
5446 | |
5447 | case NullabilityKind::NullableResult: |
5448 | return getAttributedType(attr::TypeNullableResult, modifiedType, |
5449 | equivalentType); |
5450 | |
5451 | case NullabilityKind::Unspecified: |
5452 | return getAttributedType(attr::TypeNullUnspecified, modifiedType, |
5453 | equivalentType); |
5454 | } |
5455 | |
5456 | llvm_unreachable("Unknown nullability kind"); |
5457 | } |
5458 | |
5459 | QualType ASTContext::getBTFTagAttributedType(const BTFTypeTagAttr *BTFAttr, |
5460 | QualType Wrapped) const { |
5461 | llvm::FoldingSetNodeID ID; |
5462 | BTFTagAttributedType::Profile(ID, Wrapped, BTFAttr); |
5463 | |
5464 | void *InsertPos = nullptr; |
5465 | BTFTagAttributedType *Ty = |
5466 | BTFTagAttributedTypes.FindNodeOrInsertPos(ID, InsertPos); |
5467 | if (Ty) |
5468 | return QualType(Ty, 0); |
5469 | |
5470 | QualType Canon = getCanonicalType(T: Wrapped); |
5471 | Ty = new (*this, alignof(BTFTagAttributedType)) |
5472 | BTFTagAttributedType(Canon, Wrapped, BTFAttr); |
5473 | |
5474 | Types.push_back(Ty); |
5475 | BTFTagAttributedTypes.InsertNode(N: Ty, InsertPos); |
5476 | |
5477 | return QualType(Ty, 0); |
5478 | } |
5479 | |
5480 | QualType ASTContext::getHLSLAttributedResourceType( |
5481 | QualType Wrapped, QualType Contained, |
5482 | const HLSLAttributedResourceType::Attributes &Attrs) { |
5483 | |
5484 | llvm::FoldingSetNodeID ID; |
5485 | HLSLAttributedResourceType::Profile(ID, Wrapped, Contained, Attrs); |
5486 | |
5487 | void *InsertPos = nullptr; |
5488 | HLSLAttributedResourceType *Ty = |
5489 | HLSLAttributedResourceTypes.FindNodeOrInsertPos(ID, InsertPos); |
5490 | if (Ty) |
5491 | return QualType(Ty, 0); |
5492 | |
5493 | Ty = new (*this, alignof(HLSLAttributedResourceType)) |
5494 | HLSLAttributedResourceType(Wrapped, Contained, Attrs); |
5495 | |
5496 | Types.push_back(Ty); |
5497 | HLSLAttributedResourceTypes.InsertNode(N: Ty, InsertPos); |
5498 | |
5499 | return QualType(Ty, 0); |
5500 | } |
5501 | |
5502 | QualType ASTContext::getHLSLInlineSpirvType(uint32_t Opcode, uint32_t Size, |
5503 | uint32_t Alignment, |
5504 | ArrayRef<SpirvOperand> Operands) { |
5505 | llvm::FoldingSetNodeID ID; |
5506 | HLSLInlineSpirvType::Profile(ID, Opcode, Size, Alignment, Operands); |
5507 | |
5508 | void *InsertPos = nullptr; |
5509 | HLSLInlineSpirvType *Ty = |
5510 | HLSLInlineSpirvTypes.FindNodeOrInsertPos(ID, InsertPos); |
5511 | if (Ty) |
5512 | return QualType(Ty, 0); |
5513 | |
5514 | void *Mem = Allocate( |
5515 | HLSLInlineSpirvType::totalSizeToAlloc<SpirvOperand>(Operands.size()), |
5516 | alignof(HLSLInlineSpirvType)); |
5517 | |
5518 | Ty = new (Mem) HLSLInlineSpirvType(Opcode, Size, Alignment, Operands); |
5519 | |
5520 | Types.push_back(Ty); |
5521 | HLSLInlineSpirvTypes.InsertNode(N: Ty, InsertPos); |
5522 | |
5523 | return QualType(Ty, 0); |
5524 | } |
5525 | |
5526 | /// Retrieve a substitution-result type. |
5527 | QualType ASTContext::getSubstTemplateTypeParmType(QualType Replacement, |
5528 | Decl *AssociatedDecl, |
5529 | unsigned Index, |
5530 | UnsignedOrNone PackIndex, |
5531 | bool Final) const { |
5532 | llvm::FoldingSetNodeID ID; |
5533 | SubstTemplateTypeParmType::Profile(ID, Replacement, AssociatedDecl, Index, |
5534 | PackIndex, Final); |
5535 | void *InsertPos = nullptr; |
5536 | SubstTemplateTypeParmType *SubstParm = |
5537 | SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
5538 | |
5539 | if (!SubstParm) { |
5540 | void *Mem = Allocate(SubstTemplateTypeParmType::totalSizeToAlloc<QualType>( |
5541 | !Replacement.isCanonical()), |
5542 | alignof(SubstTemplateTypeParmType)); |
5543 | SubstParm = new (Mem) SubstTemplateTypeParmType(Replacement, AssociatedDecl, |
5544 | Index, PackIndex, Final); |
5545 | Types.push_back(SubstParm); |
5546 | SubstTemplateTypeParmTypes.InsertNode(N: SubstParm, InsertPos); |
5547 | } |
5548 | |
5549 | return QualType(SubstParm, 0); |
5550 | } |
5551 | |
5552 | /// Retrieve a |
5553 | QualType |
5554 | ASTContext::getSubstTemplateTypeParmPackType(Decl *AssociatedDecl, |
5555 | unsigned Index, bool Final, |
5556 | const TemplateArgument &ArgPack) { |
5557 | #ifndef NDEBUG |
5558 | for (const auto &P : ArgPack.pack_elements()) |
5559 | assert(P.getKind() == TemplateArgument::Type && "Pack contains a non-type"); |
5560 | #endif |
5561 | |
5562 | llvm::FoldingSetNodeID ID; |
5563 | SubstTemplateTypeParmPackType::Profile(ID, AssociatedDecl, Index, Final, |
5564 | ArgPack); |
5565 | void *InsertPos = nullptr; |
5566 | if (SubstTemplateTypeParmPackType *SubstParm = |
5567 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5568 | return QualType(SubstParm, 0); |
5569 | |
5570 | QualType Canon; |
5571 | { |
5572 | TemplateArgument CanonArgPack = getCanonicalTemplateArgument(Arg: ArgPack); |
5573 | if (!AssociatedDecl->isCanonicalDecl() || |
5574 | !CanonArgPack.structurallyEquals(Other: ArgPack)) { |
5575 | Canon = getSubstTemplateTypeParmPackType( |
5576 | AssociatedDecl: AssociatedDecl->getCanonicalDecl(), Index, Final, ArgPack: CanonArgPack); |
5577 | [[maybe_unused]] const auto *Nothing = |
5578 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
5579 | assert(!Nothing); |
5580 | } |
5581 | } |
5582 | |
5583 | auto *SubstParm = new (*this, alignof(SubstTemplateTypeParmPackType)) |
5584 | SubstTemplateTypeParmPackType(Canon, AssociatedDecl, Index, Final, |
5585 | ArgPack); |
5586 | Types.push_back(SubstParm); |
5587 | SubstTemplateTypeParmPackTypes.InsertNode(N: SubstParm, InsertPos); |
5588 | return QualType(SubstParm, 0); |
5589 | } |
5590 | |
5591 | /// Retrieve the template type parameter type for a template |
5592 | /// parameter or parameter pack with the given depth, index, and (optionally) |
5593 | /// name. |
5594 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
5595 | bool ParameterPack, |
5596 | TemplateTypeParmDecl *TTPDecl) const { |
5597 | llvm::FoldingSetNodeID ID; |
5598 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
5599 | void *InsertPos = nullptr; |
5600 | TemplateTypeParmType *TypeParm |
5601 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
5602 | |
5603 | if (TypeParm) |
5604 | return QualType(TypeParm, 0); |
5605 | |
5606 | if (TTPDecl) { |
5607 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
5608 | TypeParm = new (*this, alignof(TemplateTypeParmType)) |
5609 | TemplateTypeParmType(Depth, Index, ParameterPack, TTPDecl, Canon); |
5610 | |
5611 | TemplateTypeParmType *TypeCheck |
5612 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
5613 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
5614 | (void)TypeCheck; |
5615 | } else |
5616 | TypeParm = new (*this, alignof(TemplateTypeParmType)) TemplateTypeParmType( |
5617 | Depth, Index, ParameterPack, /*TTPDecl=*/nullptr, /*Canon=*/QualType()); |
5618 | |
5619 | Types.push_back(TypeParm); |
5620 | TemplateTypeParmTypes.InsertNode(N: TypeParm, InsertPos); |
5621 | |
5622 | return QualType(TypeParm, 0); |
5623 | } |
5624 | |
5625 | TypeSourceInfo *ASTContext::getTemplateSpecializationTypeInfo( |
5626 | TemplateName Name, SourceLocation NameLoc, |
5627 | const TemplateArgumentListInfo &SpecifiedArgs, |
5628 | ArrayRef<TemplateArgument> CanonicalArgs, QualType Underlying) const { |
5629 | QualType TST = getTemplateSpecializationType(T: Name, SpecifiedArgs: SpecifiedArgs.arguments(), |
5630 | CanonicalArgs, Canon: Underlying); |
5631 | |
5632 | TypeSourceInfo *DI = CreateTypeSourceInfo(T: TST); |
5633 | TemplateSpecializationTypeLoc TL = |
5634 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
5635 | TL.setTemplateKeywordLoc(SourceLocation()); |
5636 | TL.setTemplateNameLoc(NameLoc); |
5637 | TL.setLAngleLoc(SpecifiedArgs.getLAngleLoc()); |
5638 | TL.setRAngleLoc(SpecifiedArgs.getRAngleLoc()); |
5639 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
5640 | TL.setArgLocInfo(i, AI: SpecifiedArgs[i].getLocInfo()); |
5641 | return DI; |
5642 | } |
5643 | |
5644 | QualType ASTContext::getTemplateSpecializationType( |
5645 | TemplateName Template, ArrayRef<TemplateArgumentLoc> SpecifiedArgs, |
5646 | ArrayRef<TemplateArgument> CanonicalArgs, QualType Underlying) const { |
5647 | SmallVector<TemplateArgument, 4> SpecifiedArgVec; |
5648 | SpecifiedArgVec.reserve(N: SpecifiedArgs.size()); |
5649 | for (const TemplateArgumentLoc &Arg : SpecifiedArgs) |
5650 | SpecifiedArgVec.push_back(Elt: Arg.getArgument()); |
5651 | |
5652 | return getTemplateSpecializationType(T: Template, SpecifiedArgs: SpecifiedArgVec, CanonicalArgs, |
5653 | Underlying); |
5654 | } |
5655 | |
5656 | [[maybe_unused]] static bool |
5657 | hasAnyPackExpansions(ArrayRef<TemplateArgument> Args) { |
5658 | for (const TemplateArgument &Arg : Args) |
5659 | if (Arg.isPackExpansion()) |
5660 | return true; |
5661 | return false; |
5662 | } |
5663 | |
5664 | QualType ASTContext::getCanonicalTemplateSpecializationType( |
5665 | TemplateName Template, ArrayRef<TemplateArgument> Args) const { |
5666 | assert(Template == |
5667 | getCanonicalTemplateName(Template, /*IgnoreDeduced=*/true)); |
5668 | assert(!Args.empty()); |
5669 | #ifndef NDEBUG |
5670 | for (const auto &Arg : Args) |
5671 | assert(Arg.structurallyEquals(getCanonicalTemplateArgument(Arg))); |
5672 | #endif |
5673 | |
5674 | llvm::FoldingSetNodeID ID; |
5675 | TemplateSpecializationType::Profile(ID, T: Template, Args, Underlying: QualType(), Context: *this); |
5676 | void *InsertPos = nullptr; |
5677 | if (auto *T = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5678 | return QualType(T, 0); |
5679 | |
5680 | void *Mem = Allocate(Size: sizeof(TemplateSpecializationType) + |
5681 | sizeof(TemplateArgument) * Args.size(), |
5682 | Align: alignof(TemplateSpecializationType)); |
5683 | auto *Spec = new (Mem) |
5684 | TemplateSpecializationType(Template, /*IsAlias=*/false, Args, QualType()); |
5685 | assert(Spec->isDependentType() && |
5686 | "canonical template specialization must be dependent"); |
5687 | Types.push_back(Spec); |
5688 | TemplateSpecializationTypes.InsertNode(N: Spec, InsertPos); |
5689 | return QualType(Spec, 0); |
5690 | } |
5691 | |
5692 | QualType ASTContext::getTemplateSpecializationType( |
5693 | TemplateName Template, ArrayRef<TemplateArgument> SpecifiedArgs, |
5694 | ArrayRef<TemplateArgument> CanonicalArgs, QualType Underlying) const { |
5695 | assert(!Template.getUnderlying().getAsDependentTemplateName() && |
5696 | "No dependent template names here!"); |
5697 | |
5698 | const auto *TD = Template.getAsTemplateDecl(/*IgnoreDeduced=*/true); |
5699 | bool IsTypeAlias = TD && TD->isTypeAlias(); |
5700 | if (Underlying.isNull()) { |
5701 | TemplateName CanonTemplate = |
5702 | getCanonicalTemplateName(Name: Template, /*IgnoreDeduced=*/true); |
5703 | bool NonCanonical = Template != CanonTemplate; |
5704 | SmallVector<TemplateArgument, 4> CanonArgsVec; |
5705 | if (CanonicalArgs.empty()) { |
5706 | CanonArgsVec = SmallVector<TemplateArgument, 4>(SpecifiedArgs); |
5707 | NonCanonical |= canonicalizeTemplateArguments(Args: CanonArgsVec); |
5708 | CanonicalArgs = CanonArgsVec; |
5709 | } else { |
5710 | NonCanonical |= !llvm::equal( |
5711 | LRange&: SpecifiedArgs, RRange&: CanonicalArgs, |
5712 | P: [](const TemplateArgument &A, const TemplateArgument &B) { |
5713 | return A.structurallyEquals(Other: B); |
5714 | }); |
5715 | } |
5716 | |
5717 | // We can get here with an alias template when the specialization |
5718 | // contains a pack expansion that does not match up with a parameter |
5719 | // pack, or a builtin template which cannot be resolved due to dependency. |
5720 | assert((!isa_and_nonnull<TypeAliasTemplateDecl>(TD) || |
5721 | hasAnyPackExpansions(CanonicalArgs)) && |
5722 | "Caller must compute aliased type"); |
5723 | IsTypeAlias = false; |
5724 | |
5725 | Underlying = |
5726 | getCanonicalTemplateSpecializationType(Template: CanonTemplate, Args: CanonicalArgs); |
5727 | if (!NonCanonical) |
5728 | return Underlying; |
5729 | } |
5730 | void *Mem = Allocate(Size: sizeof(TemplateSpecializationType) + |
5731 | sizeof(TemplateArgument) * SpecifiedArgs.size() + |
5732 | (IsTypeAlias ? sizeof(QualType) : 0), |
5733 | Align: alignof(TemplateSpecializationType)); |
5734 | auto *Spec = new (Mem) TemplateSpecializationType(Template, IsTypeAlias, |
5735 | SpecifiedArgs, Underlying); |
5736 | Types.push_back(Spec); |
5737 | return QualType(Spec, 0); |
5738 | } |
5739 | |
5740 | QualType ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
5741 | NestedNameSpecifier *NNS, |
5742 | QualType NamedType, |
5743 | TagDecl *OwnedTagDecl) const { |
5744 | llvm::FoldingSetNodeID ID; |
5745 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType, OwnedTagDecl); |
5746 | |
5747 | void *InsertPos = nullptr; |
5748 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
5749 | if (T) |
5750 | return QualType(T, 0); |
5751 | |
5752 | QualType Canon = NamedType; |
5753 | if (!Canon.isCanonical()) { |
5754 | Canon = getCanonicalType(T: NamedType); |
5755 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
5756 | assert(!CheckT && "Elaborated canonical type broken"); |
5757 | (void)CheckT; |
5758 | } |
5759 | |
5760 | void *Mem = |
5761 | Allocate(ElaboratedType::totalSizeToAlloc<TagDecl *>(!!OwnedTagDecl), |
5762 | alignof(ElaboratedType)); |
5763 | T = new (Mem) ElaboratedType(Keyword, NNS, NamedType, Canon, OwnedTagDecl); |
5764 | |
5765 | Types.push_back(T); |
5766 | ElaboratedTypes.InsertNode(N: T, InsertPos); |
5767 | return QualType(T, 0); |
5768 | } |
5769 | |
5770 | QualType |
5771 | ASTContext::getParenType(QualType InnerType) const { |
5772 | llvm::FoldingSetNodeID ID; |
5773 | ParenType::Profile(ID, Inner: InnerType); |
5774 | |
5775 | void *InsertPos = nullptr; |
5776 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
5777 | if (T) |
5778 | return QualType(T, 0); |
5779 | |
5780 | QualType Canon = InnerType; |
5781 | if (!Canon.isCanonical()) { |
5782 | Canon = getCanonicalType(T: InnerType); |
5783 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
5784 | assert(!CheckT && "Paren canonical type broken"); |
5785 | (void)CheckT; |
5786 | } |
5787 | |
5788 | T = new (*this, alignof(ParenType)) ParenType(InnerType, Canon); |
5789 | Types.push_back(T); |
5790 | ParenTypes.InsertNode(N: T, InsertPos); |
5791 | return QualType(T, 0); |
5792 | } |
5793 | |
5794 | QualType |
5795 | ASTContext::getMacroQualifiedType(QualType UnderlyingTy, |
5796 | const IdentifierInfo *MacroII) const { |
5797 | QualType Canon = UnderlyingTy; |
5798 | if (!Canon.isCanonical()) |
5799 | Canon = getCanonicalType(T: UnderlyingTy); |
5800 | |
5801 | auto *newType = new (*this, alignof(MacroQualifiedType)) |
5802 | MacroQualifiedType(UnderlyingTy, Canon, MacroII); |
5803 | Types.push_back(newType); |
5804 | return QualType(newType, 0); |
5805 | } |
5806 | |
5807 | static ElaboratedTypeKeyword |
5808 | getCanonicalElaboratedTypeKeyword(ElaboratedTypeKeyword Keyword) { |
5809 | switch (Keyword) { |
5810 | // These are just themselves. |
5811 | case ElaboratedTypeKeyword::None: |
5812 | case ElaboratedTypeKeyword::Struct: |
5813 | case ElaboratedTypeKeyword::Union: |
5814 | case ElaboratedTypeKeyword::Enum: |
5815 | case ElaboratedTypeKeyword::Interface: |
5816 | return Keyword; |
5817 | |
5818 | // These are equivalent. |
5819 | case ElaboratedTypeKeyword::Typename: |
5820 | return ElaboratedTypeKeyword::None; |
5821 | |
5822 | // These are functionally equivalent, so relying on their equivalence is |
5823 | // IFNDR. By making them equivalent, we disallow overloading, which at least |
5824 | // can produce a diagnostic. |
5825 | case ElaboratedTypeKeyword::Class: |
5826 | return ElaboratedTypeKeyword::Struct; |
5827 | } |
5828 | llvm_unreachable("unexpected keyword kind"); |
5829 | } |
5830 | |
5831 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
5832 | NestedNameSpecifier *NNS, |
5833 | const IdentifierInfo *Name) const { |
5834 | llvm::FoldingSetNodeID ID; |
5835 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
5836 | |
5837 | void *InsertPos = nullptr; |
5838 | if (DependentNameType *T = |
5839 | DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5840 | return QualType(T, 0); |
5841 | |
5842 | ElaboratedTypeKeyword CanonKeyword = |
5843 | getCanonicalElaboratedTypeKeyword(Keyword); |
5844 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
5845 | |
5846 | QualType Canon; |
5847 | if (CanonKeyword != Keyword || CanonNNS != NNS) { |
5848 | Canon = getDependentNameType(Keyword: CanonKeyword, NNS: CanonNNS, Name); |
5849 | [[maybe_unused]] DependentNameType *T = |
5850 | DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
5851 | assert(!T && "broken canonicalization"); |
5852 | assert(Canon.isCanonical()); |
5853 | } |
5854 | |
5855 | DependentNameType *T = new (*this, alignof(DependentNameType)) |
5856 | DependentNameType(Keyword, NNS, Name, Canon); |
5857 | Types.push_back(T); |
5858 | DependentNameTypes.InsertNode(N: T, InsertPos); |
5859 | return QualType(T, 0); |
5860 | } |
5861 | |
5862 | QualType ASTContext::getDependentTemplateSpecializationType( |
5863 | ElaboratedTypeKeyword Keyword, const DependentTemplateStorage &Name, |
5864 | ArrayRef<TemplateArgumentLoc> Args) const { |
5865 | // TODO: avoid this copy |
5866 | SmallVector<TemplateArgument, 16> ArgCopy; |
5867 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
5868 | ArgCopy.push_back(Elt: Args[I].getArgument()); |
5869 | return getDependentTemplateSpecializationType(Keyword, Name, Args: ArgCopy); |
5870 | } |
5871 | |
5872 | QualType ASTContext::getDependentTemplateSpecializationType( |
5873 | ElaboratedTypeKeyword Keyword, const DependentTemplateStorage &Name, |
5874 | ArrayRef<TemplateArgument> Args, bool IsCanonical) const { |
5875 | llvm::FoldingSetNodeID ID; |
5876 | DependentTemplateSpecializationType::Profile(ID, Context: *this, Keyword, Name, Args); |
5877 | |
5878 | void *InsertPos = nullptr; |
5879 | if (auto *T = DependentTemplateSpecializationTypes.FindNodeOrInsertPos( |
5880 | ID, InsertPos)) |
5881 | return QualType(T, 0); |
5882 | |
5883 | NestedNameSpecifier *NNS = Name.getQualifier(); |
5884 | |
5885 | QualType Canon; |
5886 | if (!IsCanonical) { |
5887 | ElaboratedTypeKeyword CanonKeyword = |
5888 | getCanonicalElaboratedTypeKeyword(Keyword); |
5889 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
5890 | bool AnyNonCanonArgs = false; |
5891 | auto CanonArgs = |
5892 | ::getCanonicalTemplateArguments(C: *this, Args, AnyNonCanonArgs); |
5893 | |
5894 | if (CanonKeyword != Keyword || AnyNonCanonArgs || CanonNNS != NNS || |
5895 | !Name.hasTemplateKeyword()) { |
5896 | Canon = getDependentTemplateSpecializationType( |
5897 | Keyword: CanonKeyword, Name: {CanonNNS, Name.getName(), /*HasTemplateKeyword=*/true}, |
5898 | Args: CanonArgs, |
5899 | /*IsCanonical=*/true); |
5900 | // Find the insert position again. |
5901 | [[maybe_unused]] auto *Nothing = |
5902 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, |
5903 | InsertPos); |
5904 | assert(!Nothing && "canonical type broken"); |
5905 | } |
5906 | } else { |
5907 | assert(Keyword == getCanonicalElaboratedTypeKeyword(Keyword)); |
5908 | assert(Name.hasTemplateKeyword()); |
5909 | assert(NNS == getCanonicalNestedNameSpecifier(NNS)); |
5910 | #ifndef NDEBUG |
5911 | for (const auto &Arg : Args) |
5912 | assert(Arg.structurallyEquals(getCanonicalTemplateArgument(Arg))); |
5913 | #endif |
5914 | } |
5915 | void *Mem = Allocate(Size: (sizeof(DependentTemplateSpecializationType) + |
5916 | sizeof(TemplateArgument) * Args.size()), |
5917 | Align: alignof(DependentTemplateSpecializationType)); |
5918 | auto *T = |
5919 | new (Mem) DependentTemplateSpecializationType(Keyword, Name, Args, Canon); |
5920 | Types.push_back(T); |
5921 | DependentTemplateSpecializationTypes.InsertNode(N: T, InsertPos); |
5922 | return QualType(T, 0); |
5923 | } |
5924 | |
5925 | TemplateArgument ASTContext::getInjectedTemplateArg(NamedDecl *Param) const { |
5926 | TemplateArgument Arg; |
5927 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Val: Param)) { |
5928 | QualType ArgType = getTypeDeclType(TTP); |
5929 | if (TTP->isParameterPack()) |
5930 | ArgType = getPackExpansionType(Pattern: ArgType, NumExpansions: std::nullopt); |
5931 | |
5932 | Arg = TemplateArgument(ArgType); |
5933 | } else if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Val: Param)) { |
5934 | QualType T = |
5935 | NTTP->getType().getNonPackExpansionType().getNonLValueExprType(*this); |
5936 | // For class NTTPs, ensure we include the 'const' so the type matches that |
5937 | // of a real template argument. |
5938 | // FIXME: It would be more faithful to model this as something like an |
5939 | // lvalue-to-rvalue conversion applied to a const-qualified lvalue. |
5940 | ExprValueKind VK; |
5941 | if (T->isRecordType()) { |
5942 | // C++ [temp.param]p8: An id-expression naming a non-type |
5943 | // template-parameter of class type T denotes a static storage duration |
5944 | // object of type const T. |
5945 | T.addConst(); |
5946 | VK = VK_LValue; |
5947 | } else { |
5948 | VK = Expr::getValueKindForType(T: NTTP->getType()); |
5949 | } |
5950 | Expr *E = new (*this) |
5951 | DeclRefExpr(*this, NTTP, /*RefersToEnclosingVariableOrCapture=*/false, |
5952 | T, VK, NTTP->getLocation()); |
5953 | |
5954 | if (NTTP->isParameterPack()) |
5955 | E = new (*this) PackExpansionExpr(E, NTTP->getLocation(), std::nullopt); |
5956 | Arg = TemplateArgument(E, /*IsCanonical=*/false); |
5957 | } else { |
5958 | auto *TTP = cast<TemplateTemplateParmDecl>(Val: Param); |
5959 | TemplateName Name = getQualifiedTemplateName( |
5960 | NNS: nullptr, /*TemplateKeyword=*/false, Template: TemplateName(TTP)); |
5961 | if (TTP->isParameterPack()) |
5962 | Arg = TemplateArgument(Name, /*NumExpansions=*/std::nullopt); |
5963 | else |
5964 | Arg = TemplateArgument(Name); |
5965 | } |
5966 | |
5967 | if (Param->isTemplateParameterPack()) |
5968 | Arg = |
5969 | TemplateArgument::CreatePackCopy(Context&: const_cast<ASTContext &>(*this), Args: Arg); |
5970 | |
5971 | return Arg; |
5972 | } |
5973 | |
5974 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
5975 | UnsignedOrNone NumExpansions, |
5976 | bool ExpectPackInType) const { |
5977 | assert((!ExpectPackInType || Pattern->containsUnexpandedParameterPack()) && |
5978 | "Pack expansions must expand one or more parameter packs"); |
5979 | |
5980 | llvm::FoldingSetNodeID ID; |
5981 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
5982 | |
5983 | void *InsertPos = nullptr; |
5984 | PackExpansionType *T = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
5985 | if (T) |
5986 | return QualType(T, 0); |
5987 | |
5988 | QualType Canon; |
5989 | if (!Pattern.isCanonical()) { |
5990 | Canon = getPackExpansionType(Pattern: getCanonicalType(T: Pattern), NumExpansions, |
5991 | /*ExpectPackInType=*/false); |
5992 | |
5993 | // Find the insert position again, in case we inserted an element into |
5994 | // PackExpansionTypes and invalidated our insert position. |
5995 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
5996 | } |
5997 | |
5998 | T = new (*this, alignof(PackExpansionType)) |
5999 | PackExpansionType(Pattern, Canon, NumExpansions); |
6000 | Types.push_back(T); |
6001 | PackExpansionTypes.InsertNode(N: T, InsertPos); |
6002 | return QualType(T, 0); |
6003 | } |
6004 | |
6005 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
6006 | /// alphabetically. |
6007 | static int CmpProtocolNames(ObjCProtocolDecl *const *LHS, |
6008 | ObjCProtocolDecl *const *RHS) { |
6009 | return DeclarationName::compare(LHS: (*LHS)->getDeclName(), RHS: (*RHS)->getDeclName()); |
6010 | } |
6011 | |
6012 | static bool areSortedAndUniqued(ArrayRef<ObjCProtocolDecl *> Protocols) { |
6013 | if (Protocols.empty()) return true; |
6014 | |
6015 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
6016 | return false; |
6017 | |
6018 | for (unsigned i = 1; i != Protocols.size(); ++i) |
6019 | if (CmpProtocolNames(LHS: &Protocols[i - 1], RHS: &Protocols[i]) >= 0 || |
6020 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
6021 | return false; |
6022 | return true; |
6023 | } |
6024 | |
6025 | static void |
6026 | SortAndUniqueProtocols(SmallVectorImpl<ObjCProtocolDecl *> &Protocols) { |
6027 | // Sort protocols, keyed by name. |
6028 | llvm::array_pod_sort(Start: Protocols.begin(), End: Protocols.end(), Compare: CmpProtocolNames); |
6029 | |
6030 | // Canonicalize. |
6031 | for (ObjCProtocolDecl *&P : Protocols) |
6032 | P = P->getCanonicalDecl(); |
6033 | |
6034 | // Remove duplicates. |
6035 | auto ProtocolsEnd = llvm::unique(R&: Protocols); |
6036 | Protocols.erase(CS: ProtocolsEnd, CE: Protocols.end()); |
6037 | } |
6038 | |
6039 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
6040 | ObjCProtocolDecl * const *Protocols, |
6041 | unsigned NumProtocols) const { |
6042 | return getObjCObjectType(Base: BaseType, typeArgs: {}, |
6043 | protocols: llvm::ArrayRef(Protocols, NumProtocols), |
6044 | /*isKindOf=*/false); |
6045 | } |
6046 | |
6047 | QualType ASTContext::getObjCObjectType( |
6048 | QualType baseType, |
6049 | ArrayRef<QualType> typeArgs, |
6050 | ArrayRef<ObjCProtocolDecl *> protocols, |
6051 | bool isKindOf) const { |
6052 | // If the base type is an interface and there aren't any protocols or |
6053 | // type arguments to add, then the interface type will do just fine. |
6054 | if (typeArgs.empty() && protocols.empty() && !isKindOf && |
6055 | isa<ObjCInterfaceType>(Val: baseType)) |
6056 | return baseType; |
6057 | |
6058 | // Look in the folding set for an existing type. |
6059 | llvm::FoldingSetNodeID ID; |
6060 | ObjCObjectTypeImpl::Profile(ID, Base: baseType, typeArgs, protocols, isKindOf); |
6061 | void *InsertPos = nullptr; |
6062 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
6063 | return QualType(QT, 0); |
6064 | |
6065 | // Determine the type arguments to be used for canonicalization, |
6066 | // which may be explicitly specified here or written on the base |
6067 | // type. |
6068 | ArrayRef<QualType> effectiveTypeArgs = typeArgs; |
6069 | if (effectiveTypeArgs.empty()) { |
6070 | if (const auto *baseObject = baseType->getAs<ObjCObjectType>()) |
6071 | effectiveTypeArgs = baseObject->getTypeArgs(); |
6072 | } |
6073 | |
6074 | // Build the canonical type, which has the canonical base type and a |
6075 | // sorted-and-uniqued list of protocols and the type arguments |
6076 | // canonicalized. |
6077 | QualType canonical; |
6078 | bool typeArgsAreCanonical = llvm::all_of( |
6079 | Range&: effectiveTypeArgs, P: [&](QualType type) { return type.isCanonical(); }); |
6080 | bool protocolsSorted = areSortedAndUniqued(Protocols: protocols); |
6081 | if (!typeArgsAreCanonical || !protocolsSorted || !baseType.isCanonical()) { |
6082 | // Determine the canonical type arguments. |
6083 | ArrayRef<QualType> canonTypeArgs; |
6084 | SmallVector<QualType, 4> canonTypeArgsVec; |
6085 | if (!typeArgsAreCanonical) { |
6086 | canonTypeArgsVec.reserve(N: effectiveTypeArgs.size()); |
6087 | for (auto typeArg : effectiveTypeArgs) |
6088 | canonTypeArgsVec.push_back(Elt: getCanonicalType(T: typeArg)); |
6089 | canonTypeArgs = canonTypeArgsVec; |
6090 | } else { |
6091 | canonTypeArgs = effectiveTypeArgs; |
6092 | } |
6093 | |
6094 | ArrayRef<ObjCProtocolDecl *> canonProtocols; |
6095 | SmallVector<ObjCProtocolDecl*, 8> canonProtocolsVec; |
6096 | if (!protocolsSorted) { |
6097 | canonProtocolsVec.append(in_start: protocols.begin(), in_end: protocols.end()); |
6098 | SortAndUniqueProtocols(Protocols&: canonProtocolsVec); |
6099 | canonProtocols = canonProtocolsVec; |
6100 | } else { |
6101 | canonProtocols = protocols; |
6102 | } |
6103 | |
6104 | canonical = getObjCObjectType(baseType: getCanonicalType(T: baseType), typeArgs: canonTypeArgs, |
6105 | protocols: canonProtocols, isKindOf); |
6106 | |
6107 | // Regenerate InsertPos. |
6108 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
6109 | } |
6110 | |
6111 | unsigned size = sizeof(ObjCObjectTypeImpl); |
6112 | size += typeArgs.size() * sizeof(QualType); |
6113 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
6114 | void *mem = Allocate(Size: size, Align: alignof(ObjCObjectTypeImpl)); |
6115 | auto *T = |
6116 | new (mem) ObjCObjectTypeImpl(canonical, baseType, typeArgs, protocols, |
6117 | isKindOf); |
6118 | |
6119 | Types.push_back(T); |
6120 | ObjCObjectTypes.InsertNode(N: T, InsertPos); |
6121 | return QualType(T, 0); |
6122 | } |
6123 | |
6124 | /// Apply Objective-C protocol qualifiers to the given type. |
6125 | /// If this is for the canonical type of a type parameter, we can apply |
6126 | /// protocol qualifiers on the ObjCObjectPointerType. |
6127 | QualType |
6128 | ASTContext::applyObjCProtocolQualifiers(QualType type, |
6129 | ArrayRef<ObjCProtocolDecl *> protocols, bool &hasError, |
6130 | bool allowOnPointerType) const { |
6131 | hasError = false; |
6132 | |
6133 | if (const auto *objT = dyn_cast<ObjCTypeParamType>(Val: type.getTypePtr())) { |
6134 | return getObjCTypeParamType(Decl: objT->getDecl(), protocols); |
6135 | } |
6136 | |
6137 | // Apply protocol qualifiers to ObjCObjectPointerType. |
6138 | if (allowOnPointerType) { |
6139 | if (const auto *objPtr = |
6140 | dyn_cast<ObjCObjectPointerType>(Val: type.getTypePtr())) { |
6141 | const ObjCObjectType *objT = objPtr->getObjectType(); |
6142 | // Merge protocol lists and construct ObjCObjectType. |
6143 | SmallVector<ObjCProtocolDecl*, 8> protocolsVec; |
6144 | protocolsVec.append(objT->qual_begin(), |
6145 | objT->qual_end()); |
6146 | protocolsVec.append(in_start: protocols.begin(), in_end: protocols.end()); |
6147 | ArrayRef<ObjCProtocolDecl *> protocols = protocolsVec; |
6148 | type = getObjCObjectType( |
6149 | baseType: objT->getBaseType(), |
6150 | typeArgs: objT->getTypeArgsAsWritten(), |
6151 | protocols, |
6152 | isKindOf: objT->isKindOfTypeAsWritten()); |
6153 | return getObjCObjectPointerType(OIT: type); |
6154 | } |
6155 | } |
6156 | |
6157 | // Apply protocol qualifiers to ObjCObjectType. |
6158 | if (const auto *objT = dyn_cast<ObjCObjectType>(Val: type.getTypePtr())){ |
6159 | // FIXME: Check for protocols to which the class type is already |
6160 | // known to conform. |
6161 | |
6162 | return getObjCObjectType(baseType: objT->getBaseType(), |
6163 | typeArgs: objT->getTypeArgsAsWritten(), |
6164 | protocols, |
6165 | isKindOf: objT->isKindOfTypeAsWritten()); |
6166 | } |
6167 | |
6168 | // If the canonical type is ObjCObjectType, ... |
6169 | if (type->isObjCObjectType()) { |
6170 | // Silently overwrite any existing protocol qualifiers. |
6171 | // TODO: determine whether that's the right thing to do. |
6172 | |
6173 | // FIXME: Check for protocols to which the class type is already |
6174 | // known to conform. |
6175 | return getObjCObjectType(baseType: type, typeArgs: {}, protocols, isKindOf: false); |
6176 | } |
6177 | |
6178 | // id<protocol-list> |
6179 | if (type->isObjCIdType()) { |
6180 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); |
6181 | type = getObjCObjectType(ObjCBuiltinIdTy, {}, protocols, |
6182 | objPtr->isKindOfType()); |
6183 | return getObjCObjectPointerType(OIT: type); |
6184 | } |
6185 | |
6186 | // Class<protocol-list> |
6187 | if (type->isObjCClassType()) { |
6188 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); |
6189 | type = getObjCObjectType(ObjCBuiltinClassTy, {}, protocols, |
6190 | objPtr->isKindOfType()); |
6191 | return getObjCObjectPointerType(OIT: type); |
6192 | } |
6193 | |
6194 | hasError = true; |
6195 | return type; |
6196 | } |
6197 | |
6198 | QualType |
6199 | ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl, |
6200 | ArrayRef<ObjCProtocolDecl *> protocols) const { |
6201 | // Look in the folding set for an existing type. |
6202 | llvm::FoldingSetNodeID ID; |
6203 | ObjCTypeParamType::Profile(ID, Decl, Decl->getUnderlyingType(), protocols); |
6204 | void *InsertPos = nullptr; |
6205 | if (ObjCTypeParamType *TypeParam = |
6206 | ObjCTypeParamTypes.FindNodeOrInsertPos(ID, InsertPos)) |
6207 | return QualType(TypeParam, 0); |
6208 | |
6209 | // We canonicalize to the underlying type. |
6210 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
6211 | if (!protocols.empty()) { |
6212 | // Apply the protocol qualifers. |
6213 | bool hasError; |
6214 | Canonical = getCanonicalType(T: applyObjCProtocolQualifiers( |
6215 | type: Canonical, protocols, hasError, allowOnPointerType: true /*allowOnPointerType*/)); |
6216 | assert(!hasError && "Error when apply protocol qualifier to bound type"); |
6217 | } |
6218 | |
6219 | unsigned size = sizeof(ObjCTypeParamType); |
6220 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
6221 | void *mem = Allocate(Size: size, Align: alignof(ObjCTypeParamType)); |
6222 | auto *newType = new (mem) ObjCTypeParamType(Decl, Canonical, protocols); |
6223 | |
6224 | Types.push_back(Elt: newType); |
6225 | ObjCTypeParamTypes.InsertNode(newType, InsertPos); |
6226 | return QualType(newType, 0); |
6227 | } |
6228 | |
6229 | void ASTContext::adjustObjCTypeParamBoundType(const ObjCTypeParamDecl *Orig, |
6230 | ObjCTypeParamDecl *New) const { |
6231 | New->setTypeSourceInfo(getTrivialTypeSourceInfo(T: Orig->getUnderlyingType())); |
6232 | // Update TypeForDecl after updating TypeSourceInfo. |
6233 | auto NewTypeParamTy = cast<ObjCTypeParamType>(New->getTypeForDecl()); |
6234 | SmallVector<ObjCProtocolDecl *, 8> protocols; |
6235 | protocols.append(NewTypeParamTy->qual_begin(), NewTypeParamTy->qual_end()); |
6236 | QualType UpdatedTy = getObjCTypeParamType(Decl: New, protocols); |
6237 | New->setTypeForDecl(UpdatedTy.getTypePtr()); |
6238 | } |
6239 | |
6240 | /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's |
6241 | /// protocol list adopt all protocols in QT's qualified-id protocol |
6242 | /// list. |
6243 | bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT, |
6244 | ObjCInterfaceDecl *IC) { |
6245 | if (!QT->isObjCQualifiedIdType()) |
6246 | return false; |
6247 | |
6248 | if (const auto *OPT = QT->getAs<ObjCObjectPointerType>()) { |
6249 | // If both the right and left sides have qualifiers. |
6250 | for (auto *Proto : OPT->quals()) { |
6251 | if (!IC->ClassImplementsProtocol(Proto, false)) |
6252 | return false; |
6253 | } |
6254 | return true; |
6255 | } |
6256 | return false; |
6257 | } |
6258 | |
6259 | /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in |
6260 | /// QT's qualified-id protocol list adopt all protocols in IDecl's list |
6261 | /// of protocols. |
6262 | bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT, |
6263 | ObjCInterfaceDecl *IDecl) { |
6264 | if (!QT->isObjCQualifiedIdType()) |
6265 | return false; |
6266 | const auto *OPT = QT->getAs<ObjCObjectPointerType>(); |
6267 | if (!OPT) |
6268 | return false; |
6269 | if (!IDecl->hasDefinition()) |
6270 | return false; |
6271 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols; |
6272 | CollectInheritedProtocols(IDecl, InheritedProtocols); |
6273 | if (InheritedProtocols.empty()) |
6274 | return false; |
6275 | // Check that if every protocol in list of id<plist> conforms to a protocol |
6276 | // of IDecl's, then bridge casting is ok. |
6277 | bool Conforms = false; |
6278 | for (auto *Proto : OPT->quals()) { |
6279 | Conforms = false; |
6280 | for (auto *PI : InheritedProtocols) { |
6281 | if (ProtocolCompatibleWithProtocol(Proto, PI)) { |
6282 | Conforms = true; |
6283 | break; |
6284 | } |
6285 | } |
6286 | if (!Conforms) |
6287 | break; |
6288 | } |
6289 | if (Conforms) |
6290 | return true; |
6291 | |
6292 | for (auto *PI : InheritedProtocols) { |
6293 | // If both the right and left sides have qualifiers. |
6294 | bool Adopts = false; |
6295 | for (auto *Proto : OPT->quals()) { |
6296 | // return 'true' if 'PI' is in the inheritance hierarchy of Proto |
6297 | if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto))) |
6298 | break; |
6299 | } |
6300 | if (!Adopts) |
6301 | return false; |
6302 | } |
6303 | return true; |
6304 | } |
6305 | |
6306 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
6307 | /// the given object type. |
6308 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
6309 | llvm::FoldingSetNodeID ID; |
6310 | ObjCObjectPointerType::Profile(ID, T: ObjectT); |
6311 | |
6312 | void *InsertPos = nullptr; |
6313 | if (ObjCObjectPointerType *QT = |
6314 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
6315 | return QualType(QT, 0); |
6316 | |
6317 | // Find the canonical object type. |
6318 | QualType Canonical; |
6319 | if (!ObjectT.isCanonical()) { |
6320 | Canonical = getObjCObjectPointerType(ObjectT: getCanonicalType(T: ObjectT)); |
6321 | |
6322 | // Regenerate InsertPos. |
6323 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
6324 | } |
6325 | |
6326 | // No match. |
6327 | void *Mem = |
6328 | Allocate(Size: sizeof(ObjCObjectPointerType), Align: alignof(ObjCObjectPointerType)); |
6329 | auto *QType = |
6330 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
6331 | |
6332 | Types.push_back(QType); |
6333 | ObjCObjectPointerTypes.InsertNode(N: QType, InsertPos); |
6334 | return QualType(QType, 0); |
6335 | } |
6336 | |
6337 | /// getObjCInterfaceType - Return the unique reference to the type for the |
6338 | /// specified ObjC interface decl. The list of protocols is optional. |
6339 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
6340 | ObjCInterfaceDecl *PrevDecl) const { |
6341 | if (Decl->TypeForDecl) |
6342 | return QualType(Decl->TypeForDecl, 0); |
6343 | |
6344 | if (PrevDecl) { |
6345 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
6346 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
6347 | return QualType(PrevDecl->TypeForDecl, 0); |
6348 | } |
6349 | |
6350 | // Prefer the definition, if there is one. |
6351 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
6352 | Decl = Def; |
6353 | |
6354 | void *Mem = Allocate(Size: sizeof(ObjCInterfaceType), Align: alignof(ObjCInterfaceType)); |
6355 | auto *T = new (Mem) ObjCInterfaceType(Decl); |
6356 | Decl->TypeForDecl = T; |
6357 | Types.push_back(T); |
6358 | return QualType(T, 0); |
6359 | } |
6360 | |
6361 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
6362 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
6363 | /// multiple declarations that refer to "typeof(x)" all contain different |
6364 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
6365 | /// on canonical type's (which are always unique). |
6366 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr, TypeOfKind Kind) const { |
6367 | TypeOfExprType *toe; |
6368 | if (tofExpr->isTypeDependent()) { |
6369 | llvm::FoldingSetNodeID ID; |
6370 | DependentTypeOfExprType::Profile(ID, Context: *this, E: tofExpr, |
6371 | IsUnqual: Kind == TypeOfKind::Unqualified); |
6372 | |
6373 | void *InsertPos = nullptr; |
6374 | DependentTypeOfExprType *Canon = |
6375 | DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
6376 | if (Canon) { |
6377 | // We already have a "canonical" version of an identical, dependent |
6378 | // typeof(expr) type. Use that as our canonical type. |
6379 | toe = new (*this, alignof(TypeOfExprType)) TypeOfExprType( |
6380 | *this, tofExpr, Kind, QualType((TypeOfExprType *)Canon, 0)); |
6381 | } else { |
6382 | // Build a new, canonical typeof(expr) type. |
6383 | Canon = new (*this, alignof(DependentTypeOfExprType)) |
6384 | DependentTypeOfExprType(*this, tofExpr, Kind); |
6385 | DependentTypeOfExprTypes.InsertNode(N: Canon, InsertPos); |
6386 | toe = Canon; |
6387 | } |
6388 | } else { |
6389 | QualType Canonical = getCanonicalType(T: tofExpr->getType()); |
6390 | toe = new (*this, alignof(TypeOfExprType)) |
6391 | TypeOfExprType(*this, tofExpr, Kind, Canonical); |
6392 | } |
6393 | Types.push_back(toe); |
6394 | return QualType(toe, 0); |
6395 | } |
6396 | |
6397 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
6398 | /// TypeOfType nodes. The only motivation to unique these nodes would be |
6399 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
6400 | /// an issue. This doesn't affect the type checker, since it operates |
6401 | /// on canonical types (which are always unique). |
6402 | QualType ASTContext::getTypeOfType(QualType tofType, TypeOfKind Kind) const { |
6403 | QualType Canonical = getCanonicalType(T: tofType); |
6404 | auto *tot = new (*this, alignof(TypeOfType)) |
6405 | TypeOfType(*this, tofType, Canonical, Kind); |
6406 | Types.push_back(tot); |
6407 | return QualType(tot, 0); |
6408 | } |
6409 | |
6410 | /// getReferenceQualifiedType - Given an expr, will return the type for |
6411 | /// that expression, as in [dcl.type.simple]p4 but without taking id-expressions |
6412 | /// and class member access into account. |
6413 | QualType ASTContext::getReferenceQualifiedType(const Expr *E) const { |
6414 | // C++11 [dcl.type.simple]p4: |
6415 | // [...] |
6416 | QualType T = E->getType(); |
6417 | switch (E->getValueKind()) { |
6418 | // - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the |
6419 | // type of e; |
6420 | case VK_XValue: |
6421 | return getRValueReferenceType(T); |
6422 | // - otherwise, if e is an lvalue, decltype(e) is T&, where T is the |
6423 | // type of e; |
6424 | case VK_LValue: |
6425 | return getLValueReferenceType(T); |
6426 | // - otherwise, decltype(e) is the type of e. |
6427 | case VK_PRValue: |
6428 | return T; |
6429 | } |
6430 | llvm_unreachable("Unknown value kind"); |
6431 | } |
6432 | |
6433 | /// Unlike many "get<Type>" functions, we don't unique DecltypeType |
6434 | /// nodes. This would never be helpful, since each such type has its own |
6435 | /// expression, and would not give a significant memory saving, since there |
6436 | /// is an Expr tree under each such type. |
6437 | QualType ASTContext::getDecltypeType(Expr *E, QualType UnderlyingType) const { |
6438 | // C++11 [temp.type]p2: |
6439 | // If an expression e involves a template parameter, decltype(e) denotes a |
6440 | // unique dependent type. Two such decltype-specifiers refer to the same |
6441 | // type only if their expressions are equivalent (14.5.6.1). |
6442 | QualType CanonType; |
6443 | if (!E->isInstantiationDependent()) { |
6444 | CanonType = getCanonicalType(T: UnderlyingType); |
6445 | } else if (!UnderlyingType.isNull()) { |
6446 | CanonType = getDecltypeType(E, UnderlyingType: QualType()); |
6447 | } else { |
6448 | llvm::FoldingSetNodeID ID; |
6449 | DependentDecltypeType::Profile(ID, Context: *this, E); |
6450 | |
6451 | void *InsertPos = nullptr; |
6452 | if (DependentDecltypeType *Canon = |
6453 | DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos)) |
6454 | return QualType(Canon, 0); |
6455 | |
6456 | // Build a new, canonical decltype(expr) type. |
6457 | auto *DT = |
6458 | new (*this, alignof(DependentDecltypeType)) DependentDecltypeType(E); |
6459 | DependentDecltypeTypes.InsertNode(N: DT, InsertPos); |
6460 | Types.push_back(DT); |
6461 | return QualType(DT, 0); |
6462 | } |
6463 | auto *DT = new (*this, alignof(DecltypeType)) |
6464 | DecltypeType(E, UnderlyingType, CanonType); |
6465 | Types.push_back(DT); |
6466 | return QualType(DT, 0); |
6467 | } |
6468 | |
6469 | QualType ASTContext::getPackIndexingType(QualType Pattern, Expr *IndexExpr, |
6470 | bool FullySubstituted, |
6471 | ArrayRef<QualType> Expansions, |
6472 | UnsignedOrNone Index) const { |
6473 | QualType Canonical; |
6474 | if (FullySubstituted && Index) { |
6475 | Canonical = getCanonicalType(T: Expansions[*Index]); |
6476 | } else { |
6477 | llvm::FoldingSetNodeID ID; |
6478 | PackIndexingType::Profile(ID, Context: *this, Pattern: Pattern.getCanonicalType(), E: IndexExpr, |
6479 | FullySubstituted, Expansions); |
6480 | void *InsertPos = nullptr; |
6481 | PackIndexingType *Canon = |
6482 | DependentPackIndexingTypes.FindNodeOrInsertPos(ID, InsertPos); |
6483 | if (!Canon) { |
6484 | void *Mem = Allocate( |
6485 | PackIndexingType::totalSizeToAlloc<QualType>(Expansions.size()), |
6486 | TypeAlignment); |
6487 | Canon = |
6488 | new (Mem) PackIndexingType(QualType(), Pattern.getCanonicalType(), |
6489 | IndexExpr, FullySubstituted, Expansions); |
6490 | DependentPackIndexingTypes.InsertNode(N: Canon, InsertPos); |
6491 | } |
6492 | Canonical = QualType(Canon, 0); |
6493 | } |
6494 | |
6495 | void *Mem = |
6496 | Allocate(PackIndexingType::totalSizeToAlloc<QualType>(Expansions.size()), |
6497 | TypeAlignment); |
6498 | auto *T = new (Mem) PackIndexingType(Canonical, Pattern, IndexExpr, |
6499 | FullySubstituted, Expansions); |
6500 | Types.push_back(T); |
6501 | return QualType(T, 0); |
6502 | } |
6503 | |
6504 | /// getUnaryTransformationType - We don't unique these, since the memory |
6505 | /// savings are minimal and these are rare. |
6506 | QualType |
6507 | ASTContext::getUnaryTransformType(QualType BaseType, QualType UnderlyingType, |
6508 | UnaryTransformType::UTTKind Kind) const { |
6509 | |
6510 | llvm::FoldingSetNodeID ID; |
6511 | UnaryTransformType::Profile(ID, BaseType, UnderlyingType, UKind: Kind); |
6512 | |
6513 | void *InsertPos = nullptr; |
6514 | if (UnaryTransformType *UT = |
6515 | UnaryTransformTypes.FindNodeOrInsertPos(ID, InsertPos)) |
6516 | return QualType(UT, 0); |
6517 | |
6518 | QualType CanonType; |
6519 | if (!BaseType->isDependentType()) { |
6520 | CanonType = UnderlyingType.getCanonicalType(); |
6521 | } else { |
6522 | assert(UnderlyingType.isNull() || BaseType == UnderlyingType); |
6523 | UnderlyingType = QualType(); |
6524 | if (QualType CanonBase = BaseType.getCanonicalType(); |
6525 | BaseType != CanonBase) { |
6526 | CanonType = getUnaryTransformType(BaseType: CanonBase, UnderlyingType: QualType(), Kind); |
6527 | assert(CanonType.isCanonical()); |
6528 | |
6529 | // Find the insertion position again. |
6530 | [[maybe_unused]] UnaryTransformType *UT = |
6531 | UnaryTransformTypes.FindNodeOrInsertPos(ID, InsertPos); |
6532 | assert(!UT && "broken canonicalization"); |
6533 | } |
6534 | } |
6535 | |
6536 | auto *UT = new (*this, alignof(UnaryTransformType)) |
6537 | UnaryTransformType(BaseType, UnderlyingType, Kind, CanonType); |
6538 | UnaryTransformTypes.InsertNode(N: UT, InsertPos); |
6539 | Types.push_back(UT); |
6540 | return QualType(UT, 0); |
6541 | } |
6542 | |
6543 | QualType ASTContext::getAutoTypeInternal( |
6544 | QualType DeducedType, AutoTypeKeyword Keyword, bool IsDependent, |
6545 | bool IsPack, ConceptDecl *TypeConstraintConcept, |
6546 | ArrayRef<TemplateArgument> TypeConstraintArgs, bool IsCanon) const { |
6547 | if (DeducedType.isNull() && Keyword == AutoTypeKeyword::Auto && |
6548 | !TypeConstraintConcept && !IsDependent) |
6549 | return getAutoDeductType(); |
6550 | |
6551 | // Look in the folding set for an existing type. |
6552 | llvm::FoldingSetNodeID ID; |
6553 | bool IsDeducedDependent = |
6554 | !DeducedType.isNull() && DeducedType->isDependentType(); |
6555 | AutoType::Profile(ID, Context: *this, Deduced: DeducedType, Keyword, |
6556 | IsDependent: IsDependent || IsDeducedDependent, CD: TypeConstraintConcept, |
6557 | Arguments: TypeConstraintArgs); |
6558 | if (auto const AT_iter = AutoTypes.find(Val: ID); AT_iter != AutoTypes.end()) |
6559 | return QualType(AT_iter->getSecond(), 0); |
6560 | |
6561 | QualType Canon; |
6562 | if (!IsCanon) { |
6563 | if (!DeducedType.isNull()) { |
6564 | Canon = DeducedType.getCanonicalType(); |
6565 | } else if (TypeConstraintConcept) { |
6566 | bool AnyNonCanonArgs = false; |
6567 | ConceptDecl *CanonicalConcept = TypeConstraintConcept->getCanonicalDecl(); |
6568 | auto CanonicalConceptArgs = ::getCanonicalTemplateArguments( |
6569 | C: *this, Args: TypeConstraintArgs, AnyNonCanonArgs); |
6570 | if (CanonicalConcept != TypeConstraintConcept || AnyNonCanonArgs) { |
6571 | Canon = getAutoTypeInternal(DeducedType: QualType(), Keyword, IsDependent, IsPack, |
6572 | TypeConstraintConcept: CanonicalConcept, TypeConstraintArgs: CanonicalConceptArgs, |
6573 | /*IsCanon=*/true); |
6574 | } |
6575 | } |
6576 | } |
6577 | |
6578 | void *Mem = Allocate(Size: sizeof(AutoType) + |
6579 | sizeof(TemplateArgument) * TypeConstraintArgs.size(), |
6580 | Align: alignof(AutoType)); |
6581 | auto *AT = new (Mem) AutoType( |
6582 | DeducedType, Keyword, |
6583 | (IsDependent ? TypeDependence::DependentInstantiation |
6584 | : TypeDependence::None) | |
6585 | (IsPack ? TypeDependence::UnexpandedPack : TypeDependence::None), |
6586 | Canon, TypeConstraintConcept, TypeConstraintArgs); |
6587 | #ifndef NDEBUG |
6588 | llvm::FoldingSetNodeID InsertedID; |
6589 | AT->Profile(InsertedID, *this); |
6590 | assert(InsertedID == ID && "ID does not match"); |
6591 | #endif |
6592 | Types.push_back(Elt: AT); |
6593 | AutoTypes.try_emplace(ID, AT); |
6594 | return QualType(AT, 0); |
6595 | } |
6596 | |
6597 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
6598 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
6599 | /// canonical deduced-but-dependent 'auto' type. |
6600 | QualType |
6601 | ASTContext::getAutoType(QualType DeducedType, AutoTypeKeyword Keyword, |
6602 | bool IsDependent, bool IsPack, |
6603 | ConceptDecl *TypeConstraintConcept, |
6604 | ArrayRef<TemplateArgument> TypeConstraintArgs) const { |
6605 | assert((!IsPack || IsDependent) && "only use IsPack for a dependent pack"); |
6606 | assert((!IsDependent || DeducedType.isNull()) && |
6607 | "A dependent auto should be undeduced"); |
6608 | return getAutoTypeInternal(DeducedType, Keyword, IsDependent, IsPack, |
6609 | TypeConstraintConcept, TypeConstraintArgs); |
6610 | } |
6611 | |
6612 | QualType ASTContext::getUnconstrainedType(QualType T) const { |
6613 | QualType CanonT = T.getNonPackExpansionType().getCanonicalType(); |
6614 | |
6615 | // Remove a type-constraint from a top-level auto or decltype(auto). |
6616 | if (auto *AT = CanonT->getAs<AutoType>()) { |
6617 | if (!AT->isConstrained()) |
6618 | return T; |
6619 | return getQualifiedType(getAutoType(DeducedType: QualType(), Keyword: AT->getKeyword(), |
6620 | IsDependent: AT->isDependentType(), |
6621 | IsPack: AT->containsUnexpandedParameterPack()), |
6622 | T.getQualifiers()); |
6623 | } |
6624 | |
6625 | // FIXME: We only support constrained auto at the top level in the type of a |
6626 | // non-type template parameter at the moment. Once we lift that restriction, |
6627 | // we'll need to recursively build types containing auto here. |
6628 | assert(!CanonT->getContainedAutoType() || |
6629 | !CanonT->getContainedAutoType()->isConstrained()); |
6630 | return T; |
6631 | } |
6632 | |
6633 | QualType ASTContext::getDeducedTemplateSpecializationTypeInternal( |
6634 | TemplateName Template, QualType DeducedType, bool IsDependent, |
6635 | QualType Canon) const { |
6636 | // Look in the folding set for an existing type. |
6637 | void *InsertPos = nullptr; |
6638 | llvm::FoldingSetNodeID ID; |
6639 | DeducedTemplateSpecializationType::Profile(ID, Template, Deduced: DeducedType, |
6640 | IsDependent); |
6641 | if (DeducedTemplateSpecializationType *DTST = |
6642 | DeducedTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos)) |
6643 | return QualType(DTST, 0); |
6644 | |
6645 | auto *DTST = new (*this, alignof(DeducedTemplateSpecializationType)) |
6646 | DeducedTemplateSpecializationType(Template, DeducedType, IsDependent, |
6647 | Canon); |
6648 | |
6649 | #ifndef NDEBUG |
6650 | llvm::FoldingSetNodeID TempID; |
6651 | DTST->Profile(ID&: TempID); |
6652 | assert(ID == TempID && "ID does not match"); |
6653 | #endif |
6654 | Types.push_back(DTST); |
6655 | DeducedTemplateSpecializationTypes.InsertNode(N: DTST, InsertPos); |
6656 | return QualType(DTST, 0); |
6657 | } |
6658 | |
6659 | /// Return the uniqued reference to the deduced template specialization type |
6660 | /// which has been deduced to the given type, or to the canonical undeduced |
6661 | /// such type, or the canonical deduced-but-dependent such type. |
6662 | QualType ASTContext::getDeducedTemplateSpecializationType( |
6663 | TemplateName Template, QualType DeducedType, bool IsDependent) const { |
6664 | QualType Canon = DeducedType.isNull() |
6665 | ? getDeducedTemplateSpecializationTypeInternal( |
6666 | Template: getCanonicalTemplateName(Name: Template), DeducedType: QualType(), |
6667 | IsDependent, Canon: QualType()) |
6668 | : DeducedType.getCanonicalType(); |
6669 | return getDeducedTemplateSpecializationTypeInternal(Template, DeducedType, |
6670 | IsDependent, Canon); |
6671 | } |
6672 | |
6673 | /// getAtomicType - Return the uniqued reference to the atomic type for |
6674 | /// the given value type. |
6675 | QualType ASTContext::getAtomicType(QualType T) const { |
6676 | // Unique pointers, to guarantee there is only one pointer of a particular |
6677 | // structure. |
6678 | llvm::FoldingSetNodeID ID; |
6679 | AtomicType::Profile(ID, T); |
6680 | |
6681 | void *InsertPos = nullptr; |
6682 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
6683 | return QualType(AT, 0); |
6684 | |
6685 | // If the atomic value type isn't canonical, this won't be a canonical type |
6686 | // either, so fill in the canonical type field. |
6687 | QualType Canonical; |
6688 | if (!T.isCanonical()) { |
6689 | Canonical = getAtomicType(T: getCanonicalType(T)); |
6690 | |
6691 | // Get the new insert position for the node we care about. |
6692 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
6693 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
6694 | } |
6695 | auto *New = new (*this, alignof(AtomicType)) AtomicType(T, Canonical); |
6696 | Types.push_back(New); |
6697 | AtomicTypes.InsertNode(N: New, InsertPos); |
6698 | return QualType(New, 0); |
6699 | } |
6700 | |
6701 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
6702 | QualType ASTContext::getAutoDeductType() const { |
6703 | if (AutoDeductTy.isNull()) |
6704 | AutoDeductTy = QualType(new (*this, alignof(AutoType)) |
6705 | AutoType(QualType(), AutoTypeKeyword::Auto, |
6706 | TypeDependence::None, QualType(), |
6707 | /*concept*/ nullptr, /*args*/ {}), |
6708 | 0); |
6709 | return AutoDeductTy; |
6710 | } |
6711 | |
6712 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
6713 | QualType ASTContext::getAutoRRefDeductType() const { |
6714 | if (AutoRRefDeductTy.isNull()) |
6715 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
6716 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
6717 | return AutoRRefDeductTy; |
6718 | } |
6719 | |
6720 | /// getTagDeclType - Return the unique reference to the type for the |
6721 | /// specified TagDecl (struct/union/class/enum) decl. |
6722 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
6723 | assert(Decl); |
6724 | // FIXME: What is the design on getTagDeclType when it requires casting |
6725 | // away const? mutable? |
6726 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
6727 | } |
6728 | |
6729 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
6730 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
6731 | /// needs to agree with the definition in <stddef.h>. |
6732 | CanQualType ASTContext::getSizeType() const { |
6733 | return getFromTargetType(Type: Target->getSizeType()); |
6734 | } |
6735 | |
6736 | /// Return the unique signed counterpart of the integer type |
6737 | /// corresponding to size_t. |
6738 | CanQualType ASTContext::getSignedSizeType() const { |
6739 | return getFromTargetType(Type: Target->getSignedSizeType()); |
6740 | } |
6741 | |
6742 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
6743 | CanQualType ASTContext::getIntMaxType() const { |
6744 | return getFromTargetType(Type: Target->getIntMaxType()); |
6745 | } |
6746 | |
6747 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
6748 | CanQualType ASTContext::getUIntMaxType() const { |
6749 | return getFromTargetType(Type: Target->getUIntMaxType()); |
6750 | } |
6751 | |
6752 | /// getSignedWCharType - Return the type of "signed wchar_t". |
6753 | /// Used when in C++, as a GCC extension. |
6754 | QualType ASTContext::getSignedWCharType() const { |
6755 | // FIXME: derive from "Target" ? |
6756 | return WCharTy; |
6757 | } |
6758 | |
6759 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
6760 | /// Used when in C++, as a GCC extension. |
6761 | QualType ASTContext::getUnsignedWCharType() const { |
6762 | // FIXME: derive from "Target" ? |
6763 | return UnsignedIntTy; |
6764 | } |
6765 | |
6766 | QualType ASTContext::getIntPtrType() const { |
6767 | return getFromTargetType(Type: Target->getIntPtrType()); |
6768 | } |
6769 | |
6770 | QualType ASTContext::getUIntPtrType() const { |
6771 | return getCorrespondingUnsignedType(T: getIntPtrType()); |
6772 | } |
6773 | |
6774 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
6775 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
6776 | QualType ASTContext::getPointerDiffType() const { |
6777 | return getFromTargetType(Type: Target->getPtrDiffType(AddrSpace: LangAS::Default)); |
6778 | } |
6779 | |
6780 | /// Return the unique unsigned counterpart of "ptrdiff_t" |
6781 | /// integer type. The standard (C11 7.21.6.1p7) refers to this type |
6782 | /// in the definition of %tu format specifier. |
6783 | QualType ASTContext::getUnsignedPointerDiffType() const { |
6784 | return getFromTargetType(Type: Target->getUnsignedPtrDiffType(AddrSpace: LangAS::Default)); |
6785 | } |
6786 | |
6787 | /// Return the unique type for "pid_t" defined in |
6788 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
6789 | QualType ASTContext::getProcessIDType() const { |
6790 | return getFromTargetType(Type: Target->getProcessIDType()); |
6791 | } |
6792 | |
6793 | //===----------------------------------------------------------------------===// |
6794 | // Type Operators |
6795 | //===----------------------------------------------------------------------===// |
6796 | |
6797 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
6798 | // Push qualifiers into arrays, and then discard any remaining |
6799 | // qualifiers. |
6800 | T = getCanonicalType(T); |
6801 | T = getVariableArrayDecayedType(type: T); |
6802 | const Type *Ty = T.getTypePtr(); |
6803 | QualType Result; |
6804 | if (getLangOpts().HLSL && isa<ConstantArrayType>(Val: Ty)) { |
6805 | Result = getArrayParameterType(Ty: QualType(Ty, 0)); |
6806 | } else if (isa<ArrayType>(Val: Ty)) { |
6807 | Result = getArrayDecayedType(T: QualType(Ty,0)); |
6808 | } else if (isa<FunctionType>(Val: Ty)) { |
6809 | Result = getPointerType(T: QualType(Ty, 0)); |
6810 | } else { |
6811 | Result = QualType(Ty, 0); |
6812 | } |
6813 | |
6814 | return CanQualType::CreateUnsafe(Other: Result); |
6815 | } |
6816 | |
6817 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
6818 | Qualifiers &quals) const { |
6819 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
6820 | |
6821 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
6822 | // the unqualified desugared type and then drops it on the floor. |
6823 | // We then have to strip that sugar back off with |
6824 | // getUnqualifiedDesugaredType(), which is silly. |
6825 | const auto *AT = |
6826 | dyn_cast<ArrayType>(Val: splitType.Ty->getUnqualifiedDesugaredType()); |
6827 | |
6828 | // If we don't have an array, just use the results in splitType. |
6829 | if (!AT) { |
6830 | quals = splitType.Quals; |
6831 | return QualType(splitType.Ty, 0); |
6832 | } |
6833 | |
6834 | // Otherwise, recurse on the array's element type. |
6835 | QualType elementType = AT->getElementType(); |
6836 | QualType unqualElementType = getUnqualifiedArrayType(type: elementType, quals); |
6837 | |
6838 | // If that didn't change the element type, AT has no qualifiers, so we |
6839 | // can just use the results in splitType. |
6840 | if (elementType == unqualElementType) { |
6841 | assert(quals.empty()); // from the recursive call |
6842 | quals = splitType.Quals; |
6843 | return QualType(splitType.Ty, 0); |
6844 | } |
6845 | |
6846 | // Otherwise, add in the qualifiers from the outermost type, then |
6847 | // build the type back up. |
6848 | quals.addConsistentQualifiers(qs: splitType.Quals); |
6849 | |
6850 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) { |
6851 | return getConstantArrayType(EltTy: unqualElementType, ArySizeIn: CAT->getSize(), |
6852 | SizeExpr: CAT->getSizeExpr(), ASM: CAT->getSizeModifier(), IndexTypeQuals: 0); |
6853 | } |
6854 | |
6855 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(Val: AT)) { |
6856 | return getIncompleteArrayType(elementType: unqualElementType, ASM: IAT->getSizeModifier(), elementTypeQuals: 0); |
6857 | } |
6858 | |
6859 | if (const auto *VAT = dyn_cast<VariableArrayType>(Val: AT)) { |
6860 | return getVariableArrayType(EltTy: unqualElementType, NumElts: VAT->getSizeExpr(), |
6861 | ASM: VAT->getSizeModifier(), |
6862 | IndexTypeQuals: VAT->getIndexTypeCVRQualifiers()); |
6863 | } |
6864 | |
6865 | const auto *DSAT = cast<DependentSizedArrayType>(Val: AT); |
6866 | return getDependentSizedArrayType(elementType: unqualElementType, numElements: DSAT->getSizeExpr(), |
6867 | ASM: DSAT->getSizeModifier(), elementTypeQuals: 0); |
6868 | } |
6869 | |
6870 | /// Attempt to unwrap two types that may both be array types with the same bound |
6871 | /// (or both be array types of unknown bound) for the purpose of comparing the |
6872 | /// cv-decomposition of two types per C++ [conv.qual]. |
6873 | /// |
6874 | /// \param AllowPiMismatch Allow the Pi1 and Pi2 to differ as described in |
6875 | /// C++20 [conv.qual], if permitted by the current language mode. |
6876 | void ASTContext::UnwrapSimilarArrayTypes(QualType &T1, QualType &T2, |
6877 | bool AllowPiMismatch) const { |
6878 | while (true) { |
6879 | auto *AT1 = getAsArrayType(T: T1); |
6880 | if (!AT1) |
6881 | return; |
6882 | |
6883 | auto *AT2 = getAsArrayType(T: T2); |
6884 | if (!AT2) |
6885 | return; |
6886 | |
6887 | // If we don't have two array types with the same constant bound nor two |
6888 | // incomplete array types, we've unwrapped everything we can. |
6889 | // C++20 also permits one type to be a constant array type and the other |
6890 | // to be an incomplete array type. |
6891 | // FIXME: Consider also unwrapping array of unknown bound and VLA. |
6892 | if (auto *CAT1 = dyn_cast<ConstantArrayType>(Val: AT1)) { |
6893 | auto *CAT2 = dyn_cast<ConstantArrayType>(Val: AT2); |
6894 | if (!((CAT2 && CAT1->getSize() == CAT2->getSize()) || |
6895 | (AllowPiMismatch && getLangOpts().CPlusPlus20 && |
6896 | isa<IncompleteArrayType>(Val: AT2)))) |
6897 | return; |
6898 | } else if (isa<IncompleteArrayType>(Val: AT1)) { |
6899 | if (!(isa<IncompleteArrayType>(Val: AT2) || |
6900 | (AllowPiMismatch && getLangOpts().CPlusPlus20 && |
6901 | isa<ConstantArrayType>(Val: AT2)))) |
6902 | return; |
6903 | } else { |
6904 | return; |
6905 | } |
6906 | |
6907 | T1 = AT1->getElementType(); |
6908 | T2 = AT2->getElementType(); |
6909 | } |
6910 | } |
6911 | |
6912 | /// Attempt to unwrap two types that may be similar (C++ [conv.qual]). |
6913 | /// |
6914 | /// If T1 and T2 are both pointer types of the same kind, or both array types |
6915 | /// with the same bound, unwraps layers from T1 and T2 until a pointer type is |
6916 | /// unwrapped. Top-level qualifiers on T1 and T2 are ignored. |
6917 | /// |
6918 | /// This function will typically be called in a loop that successively |
6919 | /// "unwraps" pointer and pointer-to-member types to compare them at each |
6920 | /// level. |
6921 | /// |
6922 | /// \param AllowPiMismatch Allow the Pi1 and Pi2 to differ as described in |
6923 | /// C++20 [conv.qual], if permitted by the current language mode. |
6924 | /// |
6925 | /// \return \c true if a pointer type was unwrapped, \c false if we reached a |
6926 | /// pair of types that can't be unwrapped further. |
6927 | bool ASTContext::UnwrapSimilarTypes(QualType &T1, QualType &T2, |
6928 | bool AllowPiMismatch) const { |
6929 | UnwrapSimilarArrayTypes(T1, T2, AllowPiMismatch); |
6930 | |
6931 | const auto *T1PtrType = T1->getAs<PointerType>(); |
6932 | const auto *T2PtrType = T2->getAs<PointerType>(); |
6933 | if (T1PtrType && T2PtrType) { |
6934 | T1 = T1PtrType->getPointeeType(); |
6935 | T2 = T2PtrType->getPointeeType(); |
6936 | return true; |
6937 | } |
6938 | |
6939 | if (const auto *T1MPType = T1->getAs<MemberPointerType>(), |
6940 | *T2MPType = T2->getAs<MemberPointerType>(); |
6941 | T1MPType && T2MPType) { |
6942 | if (auto *RD1 = T1MPType->getMostRecentCXXRecordDecl(), |
6943 | *RD2 = T2MPType->getMostRecentCXXRecordDecl(); |
6944 | RD1 != RD2 && RD1->getCanonicalDecl() != RD2->getCanonicalDecl()) |
6945 | return false; |
6946 | if (getCanonicalNestedNameSpecifier(NNS: T1MPType->getQualifier()) != |
6947 | getCanonicalNestedNameSpecifier(NNS: T2MPType->getQualifier())) |
6948 | return false; |
6949 | T1 = T1MPType->getPointeeType(); |
6950 | T2 = T2MPType->getPointeeType(); |
6951 | return true; |
6952 | } |
6953 | |
6954 | if (getLangOpts().ObjC) { |
6955 | const auto *T1OPType = T1->getAs<ObjCObjectPointerType>(); |
6956 | const auto *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
6957 | if (T1OPType && T2OPType) { |
6958 | T1 = T1OPType->getPointeeType(); |
6959 | T2 = T2OPType->getPointeeType(); |
6960 | return true; |
6961 | } |
6962 | } |
6963 | |
6964 | // FIXME: Block pointers, too? |
6965 | |
6966 | return false; |
6967 | } |
6968 | |
6969 | bool ASTContext::hasSimilarType(QualType T1, QualType T2) const { |
6970 | while (true) { |
6971 | Qualifiers Quals; |
6972 | T1 = getUnqualifiedArrayType(type: T1, quals&: Quals); |
6973 | T2 = getUnqualifiedArrayType(type: T2, quals&: Quals); |
6974 | if (hasSameType(T1, T2)) |
6975 | return true; |
6976 | if (!UnwrapSimilarTypes(T1, T2)) |
6977 | return false; |
6978 | } |
6979 | } |
6980 | |
6981 | bool ASTContext::hasCvrSimilarType(QualType T1, QualType T2) { |
6982 | while (true) { |
6983 | Qualifiers Quals1, Quals2; |
6984 | T1 = getUnqualifiedArrayType(type: T1, quals&: Quals1); |
6985 | T2 = getUnqualifiedArrayType(type: T2, quals&: Quals2); |
6986 | |
6987 | Quals1.removeCVRQualifiers(); |
6988 | Quals2.removeCVRQualifiers(); |
6989 | if (Quals1 != Quals2) |
6990 | return false; |
6991 | |
6992 | if (hasSameType(T1, T2)) |
6993 | return true; |
6994 | |
6995 | if (!UnwrapSimilarTypes(T1, T2, /*AllowPiMismatch*/ false)) |
6996 | return false; |
6997 | } |
6998 | } |
6999 | |
7000 | DeclarationNameInfo |
7001 | ASTContext::getNameForTemplate(TemplateName Name, |
7002 | SourceLocation NameLoc) const { |
7003 | switch (Name.getKind()) { |
7004 | case TemplateName::QualifiedTemplate: |
7005 | case TemplateName::Template: |
7006 | // DNInfo work in progress: CHECKME: what about DNLoc? |
7007 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
7008 | NameLoc); |
7009 | |
7010 | case TemplateName::OverloadedTemplate: { |
7011 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
7012 | // DNInfo work in progress: CHECKME: what about DNLoc? |
7013 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
7014 | } |
7015 | |
7016 | case TemplateName::AssumedTemplate: { |
7017 | AssumedTemplateStorage *Storage = Name.getAsAssumedTemplateName(); |
7018 | return DeclarationNameInfo(Storage->getDeclName(), NameLoc); |
7019 | } |
7020 | |
7021 | case TemplateName::DependentTemplate: { |
7022 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
7023 | IdentifierOrOverloadedOperator TN = DTN->getName(); |
7024 | DeclarationName DName; |
7025 | if (const IdentifierInfo *II = TN.getIdentifier()) { |
7026 | DName = DeclarationNames.getIdentifier(ID: II); |
7027 | return DeclarationNameInfo(DName, NameLoc); |
7028 | } else { |
7029 | DName = DeclarationNames.getCXXOperatorName(Op: TN.getOperator()); |
7030 | // DNInfo work in progress: FIXME: source locations? |
7031 | DeclarationNameLoc DNLoc = |
7032 | DeclarationNameLoc::makeCXXOperatorNameLoc(Range: SourceRange()); |
7033 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
7034 | } |
7035 | } |
7036 | |
7037 | case TemplateName::SubstTemplateTemplateParm: { |
7038 | SubstTemplateTemplateParmStorage *subst |
7039 | = Name.getAsSubstTemplateTemplateParm(); |
7040 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
7041 | NameLoc); |
7042 | } |
7043 | |
7044 | case TemplateName::SubstTemplateTemplateParmPack: { |
7045 | SubstTemplateTemplateParmPackStorage *subst |
7046 | = Name.getAsSubstTemplateTemplateParmPack(); |
7047 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
7048 | NameLoc); |
7049 | } |
7050 | case TemplateName::UsingTemplate: |
7051 | return DeclarationNameInfo(Name.getAsUsingShadowDecl()->getDeclName(), |
7052 | NameLoc); |
7053 | case TemplateName::DeducedTemplate: { |
7054 | DeducedTemplateStorage *DTS = Name.getAsDeducedTemplateName(); |
7055 | return getNameForTemplate(Name: DTS->getUnderlying(), NameLoc); |
7056 | } |
7057 | } |
7058 | |
7059 | llvm_unreachable("bad template name kind!"); |
7060 | } |
7061 | |
7062 | static const TemplateArgument * |
7063 | getDefaultTemplateArgumentOrNone(const NamedDecl *P) { |
7064 | auto handleParam = [](auto *TP) -> const TemplateArgument * { |
7065 | if (!TP->hasDefaultArgument()) |
7066 | return nullptr; |
7067 | return &TP->getDefaultArgument().getArgument(); |
7068 | }; |
7069 | switch (P->getKind()) { |
7070 | case NamedDecl::TemplateTypeParm: |
7071 | return handleParam(cast<TemplateTypeParmDecl>(Val: P)); |
7072 | case NamedDecl::NonTypeTemplateParm: |
7073 | return handleParam(cast<NonTypeTemplateParmDecl>(Val: P)); |
7074 | case NamedDecl::TemplateTemplateParm: |
7075 | return handleParam(cast<TemplateTemplateParmDecl>(Val: P)); |
7076 | default: |
7077 | llvm_unreachable("Unexpected template parameter kind"); |
7078 | } |
7079 | } |
7080 | |
7081 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name, |
7082 | bool IgnoreDeduced) const { |
7083 | while (std::optional<TemplateName> UnderlyingOrNone = |
7084 | Name.desugar(IgnoreDeduced)) |
7085 | Name = *UnderlyingOrNone; |
7086 | |
7087 | switch (Name.getKind()) { |
7088 | case TemplateName::Template: { |
7089 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
7090 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Val: Template)) |
7091 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
7092 | |
7093 | // The canonical template name is the canonical template declaration. |
7094 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
7095 | } |
7096 | |
7097 | case TemplateName::OverloadedTemplate: |
7098 | case TemplateName::AssumedTemplate: |
7099 | llvm_unreachable("cannot canonicalize unresolved template"); |
7100 | |
7101 | case TemplateName::DependentTemplate: { |
7102 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
7103 | assert(DTN && "Non-dependent template names must refer to template decls."); |
7104 | NestedNameSpecifier *Qualifier = DTN->getQualifier(); |
7105 | NestedNameSpecifier *CanonQualifier = |
7106 | getCanonicalNestedNameSpecifier(NNS: Qualifier); |
7107 | if (Qualifier != CanonQualifier || !DTN->hasTemplateKeyword()) |
7108 | return getDependentTemplateName(Name: {CanonQualifier, DTN->getName(), |
7109 | /*HasTemplateKeyword=*/true}); |
7110 | return Name; |
7111 | } |
7112 | |
7113 | case TemplateName::SubstTemplateTemplateParmPack: { |
7114 | SubstTemplateTemplateParmPackStorage *subst = |
7115 | Name.getAsSubstTemplateTemplateParmPack(); |
7116 | TemplateArgument canonArgPack = |
7117 | getCanonicalTemplateArgument(Arg: subst->getArgumentPack()); |
7118 | return getSubstTemplateTemplateParmPack( |
7119 | ArgPack: canonArgPack, AssociatedDecl: subst->getAssociatedDecl()->getCanonicalDecl(), |
7120 | Index: subst->getIndex(), Final: subst->getFinal()); |
7121 | } |
7122 | case TemplateName::DeducedTemplate: { |
7123 | assert(IgnoreDeduced == false); |
7124 | DeducedTemplateStorage *DTS = Name.getAsDeducedTemplateName(); |
7125 | DefaultArguments DefArgs = DTS->getDefaultArguments(); |
7126 | TemplateName Underlying = DTS->getUnderlying(); |
7127 | |
7128 | TemplateName CanonUnderlying = |
7129 | getCanonicalTemplateName(Name: Underlying, /*IgnoreDeduced=*/true); |
7130 | bool NonCanonical = CanonUnderlying != Underlying; |
7131 | auto CanonArgs = |
7132 | getCanonicalTemplateArguments(C: *this, Args: DefArgs.Args, AnyNonCanonArgs&: NonCanonical); |
7133 | |
7134 | ArrayRef<NamedDecl *> Params = |
7135 | CanonUnderlying.getAsTemplateDecl()->getTemplateParameters()->asArray(); |
7136 | assert(CanonArgs.size() <= Params.size()); |
7137 | // A deduced template name which deduces the same default arguments already |
7138 | // declared in the underlying template is the same template as the |
7139 | // underlying template. We need need to note any arguments which differ from |
7140 | // the corresponding declaration. If any argument differs, we must build a |
7141 | // deduced template name. |
7142 | for (int I = CanonArgs.size() - 1; I >= 0; --I) { |
7143 | const TemplateArgument *A = getDefaultTemplateArgumentOrNone(P: Params[I]); |
7144 | if (!A) |
7145 | break; |
7146 | auto CanonParamDefArg = getCanonicalTemplateArgument(Arg: *A); |
7147 | TemplateArgument &CanonDefArg = CanonArgs[I]; |
7148 | if (CanonDefArg.structurallyEquals(Other: CanonParamDefArg)) |
7149 | continue; |
7150 | // Keep popping from the back any deault arguments which are the same. |
7151 | if (I == int(CanonArgs.size() - 1)) |
7152 | CanonArgs.pop_back(); |
7153 | NonCanonical = true; |
7154 | } |
7155 | return NonCanonical ? getDeducedTemplateName( |
7156 | Underlying: CanonUnderlying, |
7157 | /*DefaultArgs=*/{.StartPos: DefArgs.StartPos, .Args: CanonArgs}) |
7158 | : Name; |
7159 | } |
7160 | case TemplateName::UsingTemplate: |
7161 | case TemplateName::QualifiedTemplate: |
7162 | case TemplateName::SubstTemplateTemplateParm: |
7163 | llvm_unreachable("always sugar node"); |
7164 | } |
7165 | |
7166 | llvm_unreachable("bad template name!"); |
7167 | } |
7168 | |
7169 | bool ASTContext::hasSameTemplateName(const TemplateName &X, |
7170 | const TemplateName &Y, |
7171 | bool IgnoreDeduced) const { |
7172 | return getCanonicalTemplateName(Name: X, IgnoreDeduced) == |
7173 | getCanonicalTemplateName(Name: Y, IgnoreDeduced); |
7174 | } |
7175 | |
7176 | bool ASTContext::isSameAssociatedConstraint( |
7177 | const AssociatedConstraint &ACX, const AssociatedConstraint &ACY) const { |
7178 | if (ACX.ArgPackSubstIndex != ACY.ArgPackSubstIndex) |
7179 | return false; |
7180 | if (!isSameConstraintExpr(XCE: ACX.ConstraintExpr, YCE: ACY.ConstraintExpr)) |
7181 | return false; |
7182 | return true; |
7183 | } |
7184 | |
7185 | bool ASTContext::isSameConstraintExpr(const Expr *XCE, const Expr *YCE) const { |
7186 | if (!XCE != !YCE) |
7187 | return false; |
7188 | |
7189 | if (!XCE) |
7190 | return true; |
7191 | |
7192 | llvm::FoldingSetNodeID XCEID, YCEID; |
7193 | XCE->Profile(XCEID, *this, /*Canonical=*/true, /*ProfileLambdaExpr=*/true); |
7194 | YCE->Profile(YCEID, *this, /*Canonical=*/true, /*ProfileLambdaExpr=*/true); |
7195 | return XCEID == YCEID; |
7196 | } |
7197 | |
7198 | bool ASTContext::isSameTypeConstraint(const TypeConstraint *XTC, |
7199 | const TypeConstraint *YTC) const { |
7200 | if (!XTC != !YTC) |
7201 | return false; |
7202 | |
7203 | if (!XTC) |
7204 | return true; |
7205 | |
7206 | auto *NCX = XTC->getNamedConcept(); |
7207 | auto *NCY = YTC->getNamedConcept(); |
7208 | if (!NCX || !NCY || !isSameEntity(NCX, NCY)) |
7209 | return false; |
7210 | if (XTC->getConceptReference()->hasExplicitTemplateArgs() != |
7211 | YTC->getConceptReference()->hasExplicitTemplateArgs()) |
7212 | return false; |
7213 | if (XTC->getConceptReference()->hasExplicitTemplateArgs()) |
7214 | if (XTC->getConceptReference() |
7215 | ->getTemplateArgsAsWritten() |
7216 | ->NumTemplateArgs != |
7217 | YTC->getConceptReference()->getTemplateArgsAsWritten()->NumTemplateArgs) |
7218 | return false; |
7219 | |
7220 | // Compare slowly by profiling. |
7221 | // |
7222 | // We couldn't compare the profiling result for the template |
7223 | // args here. Consider the following example in different modules: |
7224 | // |
7225 | // template <__integer_like _Tp, C<_Tp> Sentinel> |
7226 | // constexpr _Tp operator()(_Tp &&__t, Sentinel &&last) const { |
7227 | // return __t; |
7228 | // } |
7229 | // |
7230 | // When we compare the profiling result for `C<_Tp>` in different |
7231 | // modules, it will compare the type of `_Tp` in different modules. |
7232 | // However, the type of `_Tp` in different modules refer to different |
7233 | // types here naturally. So we couldn't compare the profiling result |
7234 | // for the template args directly. |
7235 | return isSameConstraintExpr(XCE: XTC->getImmediatelyDeclaredConstraint(), |
7236 | YCE: YTC->getImmediatelyDeclaredConstraint()); |
7237 | } |
7238 | |
7239 | bool ASTContext::isSameTemplateParameter(const NamedDecl *X, |
7240 | const NamedDecl *Y) const { |
7241 | if (X->getKind() != Y->getKind()) |
7242 | return false; |
7243 | |
7244 | if (auto *TX = dyn_cast<TemplateTypeParmDecl>(Val: X)) { |
7245 | auto *TY = cast<TemplateTypeParmDecl>(Val: Y); |
7246 | if (TX->isParameterPack() != TY->isParameterPack()) |
7247 | return false; |
7248 | if (TX->hasTypeConstraint() != TY->hasTypeConstraint()) |
7249 | return false; |
7250 | return isSameTypeConstraint(XTC: TX->getTypeConstraint(), |
7251 | YTC: TY->getTypeConstraint()); |
7252 | } |
7253 | |
7254 | if (auto *TX = dyn_cast<NonTypeTemplateParmDecl>(Val: X)) { |
7255 | auto *TY = cast<NonTypeTemplateParmDecl>(Val: Y); |
7256 | return TX->isParameterPack() == TY->isParameterPack() && |
7257 | TX->getASTContext().hasSameType(TX->getType(), TY->getType()) && |
7258 | isSameConstraintExpr(XCE: TX->getPlaceholderTypeConstraint(), |
7259 | YCE: TY->getPlaceholderTypeConstraint()); |
7260 | } |
7261 | |
7262 | auto *TX = cast<TemplateTemplateParmDecl>(Val: X); |
7263 | auto *TY = cast<TemplateTemplateParmDecl>(Val: Y); |
7264 | return TX->isParameterPack() == TY->isParameterPack() && |
7265 | isSameTemplateParameterList(X: TX->getTemplateParameters(), |
7266 | Y: TY->getTemplateParameters()); |
7267 | } |
7268 | |
7269 | bool ASTContext::isSameTemplateParameterList( |
7270 | const TemplateParameterList *X, const TemplateParameterList *Y) const { |
7271 | if (X->size() != Y->size()) |
7272 | return false; |
7273 | |
7274 | for (unsigned I = 0, N = X->size(); I != N; ++I) |
7275 | if (!isSameTemplateParameter(X: X->getParam(Idx: I), Y: Y->getParam(Idx: I))) |
7276 | return false; |
7277 | |
7278 | return isSameConstraintExpr(XCE: X->getRequiresClause(), YCE: Y->getRequiresClause()); |
7279 | } |
7280 | |
7281 | bool ASTContext::isSameDefaultTemplateArgument(const NamedDecl *X, |
7282 | const NamedDecl *Y) const { |
7283 | // If the type parameter isn't the same already, we don't need to check the |
7284 | // default argument further. |
7285 | if (!isSameTemplateParameter(X, Y)) |
7286 | return false; |
7287 | |
7288 | if (auto *TTPX = dyn_cast<TemplateTypeParmDecl>(Val: X)) { |
7289 | auto *TTPY = cast<TemplateTypeParmDecl>(Val: Y); |
7290 | if (!TTPX->hasDefaultArgument() || !TTPY->hasDefaultArgument()) |
7291 | return false; |
7292 | |
7293 | return hasSameType(T1: TTPX->getDefaultArgument().getArgument().getAsType(), |
7294 | T2: TTPY->getDefaultArgument().getArgument().getAsType()); |
7295 | } |
7296 | |
7297 | if (auto *NTTPX = dyn_cast<NonTypeTemplateParmDecl>(Val: X)) { |
7298 | auto *NTTPY = cast<NonTypeTemplateParmDecl>(Val: Y); |
7299 | if (!NTTPX->hasDefaultArgument() || !NTTPY->hasDefaultArgument()) |
7300 | return false; |
7301 | |
7302 | Expr *DefaultArgumentX = |
7303 | NTTPX->getDefaultArgument().getArgument().getAsExpr()->IgnoreImpCasts(); |
7304 | Expr *DefaultArgumentY = |
7305 | NTTPY->getDefaultArgument().getArgument().getAsExpr()->IgnoreImpCasts(); |
7306 | llvm::FoldingSetNodeID XID, YID; |
7307 | DefaultArgumentX->Profile(XID, *this, /*Canonical=*/true); |
7308 | DefaultArgumentY->Profile(YID, *this, /*Canonical=*/true); |
7309 | return XID == YID; |
7310 | } |
7311 | |
7312 | auto *TTPX = cast<TemplateTemplateParmDecl>(Val: X); |
7313 | auto *TTPY = cast<TemplateTemplateParmDecl>(Val: Y); |
7314 | |
7315 | if (!TTPX->hasDefaultArgument() || !TTPY->hasDefaultArgument()) |
7316 | return false; |
7317 | |
7318 | const TemplateArgument &TAX = TTPX->getDefaultArgument().getArgument(); |
7319 | const TemplateArgument &TAY = TTPY->getDefaultArgument().getArgument(); |
7320 | return hasSameTemplateName(X: TAX.getAsTemplate(), Y: TAY.getAsTemplate()); |
7321 | } |
7322 | |
7323 | static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) { |
7324 | if (auto *NS = X->getAsNamespace()) |
7325 | return NS; |
7326 | if (auto *NAS = X->getAsNamespaceAlias()) |
7327 | return NAS->getNamespace(); |
7328 | return nullptr; |
7329 | } |
7330 | |
7331 | static bool isSameQualifier(const NestedNameSpecifier *X, |
7332 | const NestedNameSpecifier *Y) { |
7333 | if (auto *NSX = getNamespace(X)) { |
7334 | auto *NSY = getNamespace(X: Y); |
7335 | if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl()) |
7336 | return false; |
7337 | } else if (X->getKind() != Y->getKind()) |
7338 | return false; |
7339 | |
7340 | // FIXME: For namespaces and types, we're permitted to check that the entity |
7341 | // is named via the same tokens. We should probably do so. |
7342 | switch (X->getKind()) { |
7343 | case NestedNameSpecifier::Identifier: |
7344 | if (X->getAsIdentifier() != Y->getAsIdentifier()) |
7345 | return false; |
7346 | break; |
7347 | case NestedNameSpecifier::Namespace: |
7348 | case NestedNameSpecifier::NamespaceAlias: |
7349 | // We've already checked that we named the same namespace. |
7350 | break; |
7351 | case NestedNameSpecifier::TypeSpec: |
7352 | if (X->getAsType()->getCanonicalTypeInternal() != |
7353 | Y->getAsType()->getCanonicalTypeInternal()) |
7354 | return false; |
7355 | break; |
7356 | case NestedNameSpecifier::Global: |
7357 | case NestedNameSpecifier::Super: |
7358 | return true; |
7359 | } |
7360 | |
7361 | // Recurse into earlier portion of NNS, if any. |
7362 | auto *PX = X->getPrefix(); |
7363 | auto *PY = Y->getPrefix(); |
7364 | if (PX && PY) |
7365 | return isSameQualifier(X: PX, Y: PY); |
7366 | return !PX && !PY; |
7367 | } |
7368 | |
7369 | static bool hasSameCudaAttrs(const FunctionDecl *A, const FunctionDecl *B) { |
7370 | if (!A->getASTContext().getLangOpts().CUDA) |
7371 | return true; // Target attributes are overloadable in CUDA compilation only. |
7372 | if (A->hasAttr<CUDADeviceAttr>() != B->hasAttr<CUDADeviceAttr>()) |
7373 | return false; |
7374 | if (A->hasAttr<CUDADeviceAttr>() && B->hasAttr<CUDADeviceAttr>()) |
7375 | return A->hasAttr<CUDAHostAttr>() == B->hasAttr<CUDAHostAttr>(); |
7376 | return true; // unattributed and __host__ functions are the same. |
7377 | } |
7378 | |
7379 | /// Determine whether the attributes we can overload on are identical for A and |
7380 | /// B. Will ignore any overloadable attrs represented in the type of A and B. |
7381 | static bool hasSameOverloadableAttrs(const FunctionDecl *A, |
7382 | const FunctionDecl *B) { |
7383 | // Note that pass_object_size attributes are represented in the function's |
7384 | // ExtParameterInfo, so we don't need to check them here. |
7385 | |
7386 | llvm::FoldingSetNodeID Cand1ID, Cand2ID; |
7387 | auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>(); |
7388 | auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>(); |
7389 | |
7390 | for (auto Pair : zip_longest(AEnableIfAttrs, BEnableIfAttrs)) { |
7391 | std::optional<EnableIfAttr *> Cand1A = std::get<0>(Pair); |
7392 | std::optional<EnableIfAttr *> Cand2A = std::get<1>(Pair); |
7393 | |
7394 | // Return false if the number of enable_if attributes is different. |
7395 | if (!Cand1A || !Cand2A) |
7396 | return false; |
7397 | |
7398 | Cand1ID.clear(); |
7399 | Cand2ID.clear(); |
7400 | |
7401 | (*Cand1A)->getCond()->Profile(Cand1ID, A->getASTContext(), true); |
7402 | (*Cand2A)->getCond()->Profile(Cand2ID, B->getASTContext(), true); |
7403 | |
7404 | // Return false if any of the enable_if expressions of A and B are |
7405 | // different. |
7406 | if (Cand1ID != Cand2ID) |
7407 | return false; |
7408 | } |
7409 | return hasSameCudaAttrs(A, B); |
7410 | } |
7411 | |
7412 | bool ASTContext::isSameEntity(const NamedDecl *X, const NamedDecl *Y) const { |
7413 | // Caution: this function is called by the AST reader during deserialization, |
7414 | // so it cannot rely on AST invariants being met. Non-trivial accessors |
7415 | // should be avoided, along with any traversal of redeclaration chains. |
7416 | |
7417 | if (X == Y) |
7418 | return true; |
7419 | |
7420 | if (X->getDeclName() != Y->getDeclName()) |
7421 | return false; |
7422 | |
7423 | // Must be in the same context. |
7424 | // |
7425 | // Note that we can't use DeclContext::Equals here, because the DeclContexts |
7426 | // could be two different declarations of the same function. (We will fix the |
7427 | // semantic DC to refer to the primary definition after merging.) |
7428 | if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()), |
7429 | cast<Decl>(Y->getDeclContext()->getRedeclContext()))) |
7430 | return false; |
7431 | |
7432 | // Two typedefs refer to the same entity if they have the same underlying |
7433 | // type. |
7434 | if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(Val: X)) |
7435 | if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Val: Y)) |
7436 | return hasSameType(T1: TypedefX->getUnderlyingType(), |
7437 | T2: TypedefY->getUnderlyingType()); |
7438 | |
7439 | // Must have the same kind. |
7440 | if (X->getKind() != Y->getKind()) |
7441 | return false; |
7442 | |
7443 | // Objective-C classes and protocols with the same name always match. |
7444 | if (isa<ObjCInterfaceDecl>(Val: X) || isa<ObjCProtocolDecl>(Val: X)) |
7445 | return true; |
7446 | |
7447 | if (isa<ClassTemplateSpecializationDecl>(Val: X)) { |
7448 | // No need to handle these here: we merge them when adding them to the |
7449 | // template. |
7450 | return false; |
7451 | } |
7452 | |
7453 | // Compatible tags match. |
7454 | if (const auto *TagX = dyn_cast<TagDecl>(Val: X)) { |
7455 | const auto *TagY = cast<TagDecl>(Val: Y); |
7456 | return (TagX->getTagKind() == TagY->getTagKind()) || |
7457 | ((TagX->getTagKind() == TagTypeKind::Struct || |
7458 | TagX->getTagKind() == TagTypeKind::Class || |
7459 | TagX->getTagKind() == TagTypeKind::Interface) && |
7460 | (TagY->getTagKind() == TagTypeKind::Struct || |
7461 | TagY->getTagKind() == TagTypeKind::Class || |
7462 | TagY->getTagKind() == TagTypeKind::Interface)); |
7463 | } |
7464 | |
7465 | // Functions with the same type and linkage match. |
7466 | // FIXME: This needs to cope with merging of prototyped/non-prototyped |
7467 | // functions, etc. |
7468 | if (const auto *FuncX = dyn_cast<FunctionDecl>(Val: X)) { |
7469 | const auto *FuncY = cast<FunctionDecl>(Val: Y); |
7470 | if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(Val: X)) { |
7471 | const auto *CtorY = cast<CXXConstructorDecl>(Val: Y); |
7472 | if (CtorX->getInheritedConstructor() && |
7473 | !isSameEntity(CtorX->getInheritedConstructor().getConstructor(), |
7474 | CtorY->getInheritedConstructor().getConstructor())) |
7475 | return false; |
7476 | } |
7477 | |
7478 | if (FuncX->isMultiVersion() != FuncY->isMultiVersion()) |
7479 | return false; |
7480 | |
7481 | // Multiversioned functions with different feature strings are represented |
7482 | // as separate declarations. |
7483 | if (FuncX->isMultiVersion()) { |
7484 | const auto *TAX = FuncX->getAttr<TargetAttr>(); |
7485 | const auto *TAY = FuncY->getAttr<TargetAttr>(); |
7486 | assert(TAX && TAY && "Multiversion Function without target attribute"); |
7487 | |
7488 | if (TAX->getFeaturesStr() != TAY->getFeaturesStr()) |
7489 | return false; |
7490 | } |
7491 | |
7492 | // Per C++20 [temp.over.link]/4, friends in different classes are sometimes |
7493 | // not the same entity if they are constrained. |
7494 | if ((FuncX->isMemberLikeConstrainedFriend() || |
7495 | FuncY->isMemberLikeConstrainedFriend()) && |
7496 | !FuncX->getLexicalDeclContext()->Equals( |
7497 | FuncY->getLexicalDeclContext())) { |
7498 | return false; |
7499 | } |
7500 | |
7501 | if (!isSameAssociatedConstraint(ACX: FuncX->getTrailingRequiresClause(), |
7502 | ACY: FuncY->getTrailingRequiresClause())) |
7503 | return false; |
7504 | |
7505 | auto GetTypeAsWritten = [](const FunctionDecl *FD) { |
7506 | // Map to the first declaration that we've already merged into this one. |
7507 | // The TSI of redeclarations might not match (due to calling conventions |
7508 | // being inherited onto the type but not the TSI), but the TSI type of |
7509 | // the first declaration of the function should match across modules. |
7510 | FD = FD->getCanonicalDecl(); |
7511 | return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType() |
7512 | : FD->getType(); |
7513 | }; |
7514 | QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY); |
7515 | if (!hasSameType(T1: XT, T2: YT)) { |
7516 | // We can get functions with different types on the redecl chain in C++17 |
7517 | // if they have differing exception specifications and at least one of |
7518 | // the excpetion specs is unresolved. |
7519 | auto *XFPT = XT->getAs<FunctionProtoType>(); |
7520 | auto *YFPT = YT->getAs<FunctionProtoType>(); |
7521 | if (getLangOpts().CPlusPlus17 && XFPT && YFPT && |
7522 | (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) || |
7523 | isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) && |
7524 | hasSameFunctionTypeIgnoringExceptionSpec(T: XT, U: YT)) |
7525 | return true; |
7526 | return false; |
7527 | } |
7528 | |
7529 | return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() && |
7530 | hasSameOverloadableAttrs(A: FuncX, B: FuncY); |
7531 | } |
7532 | |
7533 | // Variables with the same type and linkage match. |
7534 | if (const auto *VarX = dyn_cast<VarDecl>(Val: X)) { |
7535 | const auto *VarY = cast<VarDecl>(Val: Y); |
7536 | if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) { |
7537 | // During deserialization, we might compare variables before we load |
7538 | // their types. Assume the types will end up being the same. |
7539 | if (VarX->getType().isNull() || VarY->getType().isNull()) |
7540 | return true; |
7541 | |
7542 | if (hasSameType(VarX->getType(), VarY->getType())) |
7543 | return true; |
7544 | |
7545 | // We can get decls with different types on the redecl chain. Eg. |
7546 | // template <typename T> struct S { static T Var[]; }; // #1 |
7547 | // template <typename T> T S<T>::Var[sizeof(T)]; // #2 |
7548 | // Only? happens when completing an incomplete array type. In this case |
7549 | // when comparing #1 and #2 we should go through their element type. |
7550 | const ArrayType *VarXTy = getAsArrayType(T: VarX->getType()); |
7551 | const ArrayType *VarYTy = getAsArrayType(T: VarY->getType()); |
7552 | if (!VarXTy || !VarYTy) |
7553 | return false; |
7554 | if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType()) |
7555 | return hasSameType(T1: VarXTy->getElementType(), T2: VarYTy->getElementType()); |
7556 | } |
7557 | return false; |
7558 | } |
7559 | |
7560 | // Namespaces with the same name and inlinedness match. |
7561 | if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(Val: X)) { |
7562 | const auto *NamespaceY = cast<NamespaceDecl>(Val: Y); |
7563 | return NamespaceX->isInline() == NamespaceY->isInline(); |
7564 | } |
7565 | |
7566 | // Identical template names and kinds match if their template parameter lists |
7567 | // and patterns match. |
7568 | if (const auto *TemplateX = dyn_cast<TemplateDecl>(Val: X)) { |
7569 | const auto *TemplateY = cast<TemplateDecl>(Val: Y); |
7570 | |
7571 | // ConceptDecl wouldn't be the same if their constraint expression differs. |
7572 | if (const auto *ConceptX = dyn_cast<ConceptDecl>(Val: X)) { |
7573 | const auto *ConceptY = cast<ConceptDecl>(Val: Y); |
7574 | if (!isSameConstraintExpr(XCE: ConceptX->getConstraintExpr(), |
7575 | YCE: ConceptY->getConstraintExpr())) |
7576 | return false; |
7577 | } |
7578 | |
7579 | return isSameEntity(X: TemplateX->getTemplatedDecl(), |
7580 | Y: TemplateY->getTemplatedDecl()) && |
7581 | isSameTemplateParameterList(X: TemplateX->getTemplateParameters(), |
7582 | Y: TemplateY->getTemplateParameters()); |
7583 | } |
7584 | |
7585 | // Fields with the same name and the same type match. |
7586 | if (const auto *FDX = dyn_cast<FieldDecl>(Val: X)) { |
7587 | const auto *FDY = cast<FieldDecl>(Val: Y); |
7588 | // FIXME: Also check the bitwidth is odr-equivalent, if any. |
7589 | return hasSameType(FDX->getType(), FDY->getType()); |
7590 | } |
7591 | |
7592 | // Indirect fields with the same target field match. |
7593 | if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(Val: X)) { |
7594 | const auto *IFDY = cast<IndirectFieldDecl>(Val: Y); |
7595 | return IFDX->getAnonField()->getCanonicalDecl() == |
7596 | IFDY->getAnonField()->getCanonicalDecl(); |
7597 | } |
7598 | |
7599 | // Enumerators with the same name match. |
7600 | if (isa<EnumConstantDecl>(Val: X)) |
7601 | // FIXME: Also check the value is odr-equivalent. |
7602 | return true; |
7603 | |
7604 | // Using shadow declarations with the same target match. |
7605 | if (const auto *USX = dyn_cast<UsingShadowDecl>(Val: X)) { |
7606 | const auto *USY = cast<UsingShadowDecl>(Val: Y); |
7607 | return declaresSameEntity(USX->getTargetDecl(), USY->getTargetDecl()); |
7608 | } |
7609 | |
7610 | // Using declarations with the same qualifier match. (We already know that |
7611 | // the name matches.) |
7612 | if (const auto *UX = dyn_cast<UsingDecl>(Val: X)) { |
7613 | const auto *UY = cast<UsingDecl>(Val: Y); |
7614 | return isSameQualifier(X: UX->getQualifier(), Y: UY->getQualifier()) && |
7615 | UX->hasTypename() == UY->hasTypename() && |
7616 | UX->isAccessDeclaration() == UY->isAccessDeclaration(); |
7617 | } |
7618 | if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(Val: X)) { |
7619 | const auto *UY = cast<UnresolvedUsingValueDecl>(Val: Y); |
7620 | return isSameQualifier(X: UX->getQualifier(), Y: UY->getQualifier()) && |
7621 | UX->isAccessDeclaration() == UY->isAccessDeclaration(); |
7622 | } |
7623 | if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(Val: X)) { |
7624 | return isSameQualifier( |
7625 | X: UX->getQualifier(), |
7626 | Y: cast<UnresolvedUsingTypenameDecl>(Val: Y)->getQualifier()); |
7627 | } |
7628 | |
7629 | // Using-pack declarations are only created by instantiation, and match if |
7630 | // they're instantiated from matching UnresolvedUsing...Decls. |
7631 | if (const auto *UX = dyn_cast<UsingPackDecl>(Val: X)) { |
7632 | return declaresSameEntity( |
7633 | UX->getInstantiatedFromUsingDecl(), |
7634 | cast<UsingPackDecl>(Val: Y)->getInstantiatedFromUsingDecl()); |
7635 | } |
7636 | |
7637 | // Namespace alias definitions with the same target match. |
7638 | if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(Val: X)) { |
7639 | const auto *NAY = cast<NamespaceAliasDecl>(Val: Y); |
7640 | return NAX->getNamespace()->Equals(NAY->getNamespace()); |
7641 | } |
7642 | |
7643 | return false; |
7644 | } |
7645 | |
7646 | TemplateArgument |
7647 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
7648 | switch (Arg.getKind()) { |
7649 | case TemplateArgument::Null: |
7650 | return Arg; |
7651 | |
7652 | case TemplateArgument::Expression: |
7653 | return TemplateArgument(Arg.getAsExpr(), /*IsCanonical=*/true, |
7654 | Arg.getIsDefaulted()); |
7655 | |
7656 | case TemplateArgument::Declaration: { |
7657 | auto *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
7658 | return TemplateArgument(D, getCanonicalType(T: Arg.getParamTypeForDecl()), |
7659 | Arg.getIsDefaulted()); |
7660 | } |
7661 | |
7662 | case TemplateArgument::NullPtr: |
7663 | return TemplateArgument(getCanonicalType(T: Arg.getNullPtrType()), |
7664 | /*isNullPtr*/ true, Arg.getIsDefaulted()); |
7665 | |
7666 | case TemplateArgument::Template: |
7667 | return TemplateArgument(getCanonicalTemplateName(Name: Arg.getAsTemplate()), |
7668 | Arg.getIsDefaulted()); |
7669 | |
7670 | case TemplateArgument::TemplateExpansion: |
7671 | return TemplateArgument( |
7672 | getCanonicalTemplateName(Name: Arg.getAsTemplateOrTemplatePattern()), |
7673 | Arg.getNumTemplateExpansions(), Arg.getIsDefaulted()); |
7674 | |
7675 | case TemplateArgument::Integral: |
7676 | return TemplateArgument(Arg, getCanonicalType(T: Arg.getIntegralType())); |
7677 | |
7678 | case TemplateArgument::StructuralValue: |
7679 | return TemplateArgument(*this, |
7680 | getCanonicalType(T: Arg.getStructuralValueType()), |
7681 | Arg.getAsStructuralValue(), Arg.getIsDefaulted()); |
7682 | |
7683 | case TemplateArgument::Type: |
7684 | return TemplateArgument(getCanonicalType(T: Arg.getAsType()), |
7685 | /*isNullPtr*/ false, Arg.getIsDefaulted()); |
7686 | |
7687 | case TemplateArgument::Pack: { |
7688 | bool AnyNonCanonArgs = false; |
7689 | auto CanonArgs = ::getCanonicalTemplateArguments( |
7690 | C: *this, Args: Arg.pack_elements(), AnyNonCanonArgs); |
7691 | if (!AnyNonCanonArgs) |
7692 | return Arg; |
7693 | auto NewArg = TemplateArgument::CreatePackCopy( |
7694 | Context&: const_cast<ASTContext &>(*this), Args: CanonArgs); |
7695 | NewArg.setIsDefaulted(Arg.getIsDefaulted()); |
7696 | return NewArg; |
7697 | } |
7698 | } |
7699 | |
7700 | // Silence GCC warning |
7701 | llvm_unreachable("Unhandled template argument kind"); |
7702 | } |
7703 | |
7704 | bool ASTContext::isSameTemplateArgument(const TemplateArgument &Arg1, |
7705 | const TemplateArgument &Arg2) const { |
7706 | if (Arg1.getKind() != Arg2.getKind()) |
7707 | return false; |
7708 | |
7709 | switch (Arg1.getKind()) { |
7710 | case TemplateArgument::Null: |
7711 | llvm_unreachable("Comparing NULL template argument"); |
7712 | |
7713 | case TemplateArgument::Type: |
7714 | return hasSameType(T1: Arg1.getAsType(), T2: Arg2.getAsType()); |
7715 | |
7716 | case TemplateArgument::Declaration: |
7717 | return Arg1.getAsDecl()->getUnderlyingDecl()->getCanonicalDecl() == |
7718 | Arg2.getAsDecl()->getUnderlyingDecl()->getCanonicalDecl(); |
7719 | |
7720 | case TemplateArgument::NullPtr: |
7721 | return hasSameType(T1: Arg1.getNullPtrType(), T2: Arg2.getNullPtrType()); |
7722 | |
7723 | case TemplateArgument::Template: |
7724 | case TemplateArgument::TemplateExpansion: |
7725 | return getCanonicalTemplateName(Name: Arg1.getAsTemplateOrTemplatePattern()) == |
7726 | getCanonicalTemplateName(Name: Arg2.getAsTemplateOrTemplatePattern()); |
7727 | |
7728 | case TemplateArgument::Integral: |
7729 | return llvm::APSInt::isSameValue(I1: Arg1.getAsIntegral(), |
7730 | I2: Arg2.getAsIntegral()); |
7731 | |
7732 | case TemplateArgument::StructuralValue: |
7733 | return Arg1.structurallyEquals(Other: Arg2); |
7734 | |
7735 | case TemplateArgument::Expression: { |
7736 | llvm::FoldingSetNodeID ID1, ID2; |
7737 | Arg1.getAsExpr()->Profile(ID1, *this, /*Canonical=*/true); |
7738 | Arg2.getAsExpr()->Profile(ID2, *this, /*Canonical=*/true); |
7739 | return ID1 == ID2; |
7740 | } |
7741 | |
7742 | case TemplateArgument::Pack: |
7743 | return llvm::equal( |
7744 | LRange: Arg1.getPackAsArray(), RRange: Arg2.getPackAsArray(), |
7745 | P: [&](const TemplateArgument &Arg1, const TemplateArgument &Arg2) { |
7746 | return isSameTemplateArgument(Arg1, Arg2); |
7747 | }); |
7748 | } |
7749 | |
7750 | llvm_unreachable("Unhandled template argument kind"); |
7751 | } |
7752 | |
7753 | NestedNameSpecifier * |
7754 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
7755 | if (!NNS) |
7756 | return nullptr; |
7757 | |
7758 | switch (NNS->getKind()) { |
7759 | case NestedNameSpecifier::Identifier: |
7760 | // Canonicalize the prefix but keep the identifier the same. |
7761 | return NestedNameSpecifier::Create(Context: *this, |
7762 | Prefix: getCanonicalNestedNameSpecifier(NNS: NNS->getPrefix()), |
7763 | II: NNS->getAsIdentifier()); |
7764 | |
7765 | case NestedNameSpecifier::Namespace: |
7766 | // A namespace is canonical; build a nested-name-specifier with |
7767 | // this namespace and no prefix. |
7768 | return NestedNameSpecifier::Create(*this, nullptr, |
7769 | NNS->getAsNamespace()->getFirstDecl()); |
7770 | |
7771 | case NestedNameSpecifier::NamespaceAlias: |
7772 | // A namespace is canonical; build a nested-name-specifier with |
7773 | // this namespace and no prefix. |
7774 | return NestedNameSpecifier::Create( |
7775 | *this, nullptr, |
7776 | NNS->getAsNamespaceAlias()->getNamespace()->getFirstDecl()); |
7777 | |
7778 | // The difference between TypeSpec and TypeSpecWithTemplate is that the |
7779 | // latter will have the 'template' keyword when printed. |
7780 | case NestedNameSpecifier::TypeSpec: { |
7781 | const Type *T = getCanonicalType(T: NNS->getAsType()); |
7782 | |
7783 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
7784 | // break it apart into its prefix and identifier, then reconsititute those |
7785 | // as the canonical nested-name-specifier. This is required to canonicalize |
7786 | // a dependent nested-name-specifier involving typedefs of dependent-name |
7787 | // types, e.g., |
7788 | // typedef typename T::type T1; |
7789 | // typedef typename T1::type T2; |
7790 | if (const auto *DNT = T->getAs<DependentNameType>()) |
7791 | return NestedNameSpecifier::Create(Context: *this, Prefix: DNT->getQualifier(), |
7792 | II: DNT->getIdentifier()); |
7793 | if (const auto *DTST = T->getAs<DependentTemplateSpecializationType>()) { |
7794 | const DependentTemplateStorage &DTN = DTST->getDependentTemplateName(); |
7795 | QualType NewT = getDependentTemplateSpecializationType( |
7796 | Keyword: ElaboratedTypeKeyword::None, |
7797 | Name: {/*NNS=*/nullptr, DTN.getName(), /*HasTemplateKeyword=*/true}, |
7798 | Args: DTST->template_arguments(), /*IsCanonical=*/true); |
7799 | assert(NewT.isCanonical()); |
7800 | NestedNameSpecifier *Prefix = DTN.getQualifier(); |
7801 | if (!Prefix) |
7802 | Prefix = getCanonicalNestedNameSpecifier(NNS: NNS->getPrefix()); |
7803 | return NestedNameSpecifier::Create(Context: *this, Prefix, T: NewT.getTypePtr()); |
7804 | } |
7805 | return NestedNameSpecifier::Create(Context: *this, Prefix: nullptr, T); |
7806 | } |
7807 | |
7808 | case NestedNameSpecifier::Global: |
7809 | case NestedNameSpecifier::Super: |
7810 | // The global specifier and __super specifer are canonical and unique. |
7811 | return NNS; |
7812 | } |
7813 | |
7814 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
7815 | } |
7816 | |
7817 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
7818 | // Handle the non-qualified case efficiently. |
7819 | if (!T.hasLocalQualifiers()) { |
7820 | // Handle the common positive case fast. |
7821 | if (const auto *AT = dyn_cast<ArrayType>(Val&: T)) |
7822 | return AT; |
7823 | } |
7824 | |
7825 | // Handle the common negative case fast. |
7826 | if (!isa<ArrayType>(Val: T.getCanonicalType())) |
7827 | return nullptr; |
7828 | |
7829 | // Apply any qualifiers from the array type to the element type. This |
7830 | // implements C99 6.7.3p8: "If the specification of an array type includes |
7831 | // any type qualifiers, the element type is so qualified, not the array type." |
7832 | |
7833 | // If we get here, we either have type qualifiers on the type, or we have |
7834 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
7835 | // we must propagate them down into the element type. |
7836 | |
7837 | SplitQualType split = T.getSplitDesugaredType(); |
7838 | Qualifiers qs = split.Quals; |
7839 | |
7840 | // If we have a simple case, just return now. |
7841 | const auto *ATy = dyn_cast<ArrayType>(Val: split.Ty); |
7842 | if (!ATy || qs.empty()) |
7843 | return ATy; |
7844 | |
7845 | // Otherwise, we have an array and we have qualifiers on it. Push the |
7846 | // qualifiers into the array element type and return a new array type. |
7847 | QualType NewEltTy = getQualifiedType(T: ATy->getElementType(), Qs: qs); |
7848 | |
7849 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: ATy)) |
7850 | return cast<ArrayType>(getConstantArrayType(EltTy: NewEltTy, ArySizeIn: CAT->getSize(), |
7851 | SizeExpr: CAT->getSizeExpr(), |
7852 | ASM: CAT->getSizeModifier(), |
7853 | IndexTypeQuals: CAT->getIndexTypeCVRQualifiers())); |
7854 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(Val: ATy)) |
7855 | return cast<ArrayType>(getIncompleteArrayType(elementType: NewEltTy, |
7856 | ASM: IAT->getSizeModifier(), |
7857 | elementTypeQuals: IAT->getIndexTypeCVRQualifiers())); |
7858 | |
7859 | if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(Val: ATy)) |
7860 | return cast<ArrayType>(getDependentSizedArrayType( |
7861 | elementType: NewEltTy, numElements: DSAT->getSizeExpr(), ASM: DSAT->getSizeModifier(), |
7862 | elementTypeQuals: DSAT->getIndexTypeCVRQualifiers())); |
7863 | |
7864 | const auto *VAT = cast<VariableArrayType>(Val: ATy); |
7865 | return cast<ArrayType>( |
7866 | getVariableArrayType(EltTy: NewEltTy, NumElts: VAT->getSizeExpr(), ASM: VAT->getSizeModifier(), |
7867 | IndexTypeQuals: VAT->getIndexTypeCVRQualifiers())); |
7868 | } |
7869 | |
7870 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
7871 | if (getLangOpts().HLSL && T->isConstantArrayType()) |
7872 | return getArrayParameterType(Ty: T); |
7873 | if (T->isArrayType() || T->isFunctionType()) |
7874 | return getDecayedType(T); |
7875 | return T; |
7876 | } |
7877 | |
7878 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
7879 | T = getVariableArrayDecayedType(type: T); |
7880 | T = getAdjustedParameterType(T); |
7881 | return T.getUnqualifiedType(); |
7882 | } |
7883 | |
7884 | QualType ASTContext::getExceptionObjectType(QualType T) const { |
7885 | // C++ [except.throw]p3: |
7886 | // A throw-expression initializes a temporary object, called the exception |
7887 | // object, the type of which is determined by removing any top-level |
7888 | // cv-qualifiers from the static type of the operand of throw and adjusting |
7889 | // the type from "array of T" or "function returning T" to "pointer to T" |
7890 | // or "pointer to function returning T", [...] |
7891 | T = getVariableArrayDecayedType(type: T); |
7892 | if (T->isArrayType() || T->isFunctionType()) |
7893 | T = getDecayedType(T); |
7894 | return T.getUnqualifiedType(); |
7895 | } |
7896 | |
7897 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
7898 | /// specified array type to a pointer. This operation is non-trivial when |
7899 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
7900 | /// this returns a pointer to a properly qualified element of the array. |
7901 | /// |
7902 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
7903 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
7904 | // Get the element type with 'getAsArrayType' so that we don't lose any |
7905 | // typedefs in the element type of the array. This also handles propagation |
7906 | // of type qualifiers from the array type into the element type if present |
7907 | // (C99 6.7.3p8). |
7908 | const ArrayType *PrettyArrayType = getAsArrayType(T: Ty); |
7909 | assert(PrettyArrayType && "Not an array type!"); |
7910 | |
7911 | QualType PtrTy = getPointerType(T: PrettyArrayType->getElementType()); |
7912 | |
7913 | // int x[restrict 4] -> int *restrict |
7914 | QualType Result = getQualifiedType(T: PtrTy, |
7915 | Qs: PrettyArrayType->getIndexTypeQualifiers()); |
7916 | |
7917 | // int x[_Nullable] -> int * _Nullable |
7918 | if (auto Nullability = Ty->getNullability()) { |
7919 | Result = const_cast<ASTContext *>(this)->getAttributedType(nullability: *Nullability, |
7920 | modifiedType: Result, equivalentType: Result); |
7921 | } |
7922 | return Result; |
7923 | } |
7924 | |
7925 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
7926 | return getBaseElementType(QT: array->getElementType()); |
7927 | } |
7928 | |
7929 | QualType ASTContext::getBaseElementType(QualType type) const { |
7930 | Qualifiers qs; |
7931 | while (true) { |
7932 | SplitQualType split = type.getSplitDesugaredType(); |
7933 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
7934 | if (!array) break; |
7935 | |
7936 | type = array->getElementType(); |
7937 | qs.addConsistentQualifiers(qs: split.Quals); |
7938 | } |
7939 | |
7940 | return getQualifiedType(T: type, Qs: qs); |
7941 | } |
7942 | |
7943 | /// getConstantArrayElementCount - Returns number of constant array elements. |
7944 | uint64_t |
7945 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
7946 | uint64_t ElementCount = 1; |
7947 | do { |
7948 | ElementCount *= CA->getZExtSize(); |
7949 | CA = dyn_cast_or_null<ConstantArrayType>( |
7950 | CA->getElementType()->getAsArrayTypeUnsafe()); |
7951 | } while (CA); |
7952 | return ElementCount; |
7953 | } |
7954 | |
7955 | uint64_t ASTContext::getArrayInitLoopExprElementCount( |
7956 | const ArrayInitLoopExpr *AILE) const { |
7957 | if (!AILE) |
7958 | return 0; |
7959 | |
7960 | uint64_t ElementCount = 1; |
7961 | |
7962 | do { |
7963 | ElementCount *= AILE->getArraySize().getZExtValue(); |
7964 | AILE = dyn_cast<ArrayInitLoopExpr>(Val: AILE->getSubExpr()); |
7965 | } while (AILE); |
7966 | |
7967 | return ElementCount; |
7968 | } |
7969 | |
7970 | /// getFloatingRank - Return a relative rank for floating point types. |
7971 | /// This routine will assert if passed a built-in type that isn't a float. |
7972 | static FloatingRank getFloatingRank(QualType T) { |
7973 | if (const auto *CT = T->getAs<ComplexType>()) |
7974 | return getFloatingRank(T: CT->getElementType()); |
7975 | |
7976 | switch (T->castAs<BuiltinType>()->getKind()) { |
7977 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
7978 | case BuiltinType::Float16: return Float16Rank; |
7979 | case BuiltinType::Half: return HalfRank; |
7980 | case BuiltinType::Float: return FloatRank; |
7981 | case BuiltinType::Double: return DoubleRank; |
7982 | case BuiltinType::LongDouble: return LongDoubleRank; |
7983 | case BuiltinType::Float128: return Float128Rank; |
7984 | case BuiltinType::BFloat16: return BFloat16Rank; |
7985 | case BuiltinType::Ibm128: return Ibm128Rank; |
7986 | } |
7987 | } |
7988 | |
7989 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
7990 | /// point types, ignoring the domain of the type (i.e. 'double' == |
7991 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
7992 | /// LHS < RHS, return -1. |
7993 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
7994 | FloatingRank LHSR = getFloatingRank(T: LHS); |
7995 | FloatingRank RHSR = getFloatingRank(T: RHS); |
7996 | |
7997 | if (LHSR == RHSR) |
7998 | return 0; |
7999 | if (LHSR > RHSR) |
8000 | return 1; |
8001 | return -1; |
8002 | } |
8003 | |
8004 | int ASTContext::getFloatingTypeSemanticOrder(QualType LHS, QualType RHS) const { |
8005 | if (&getFloatTypeSemantics(T: LHS) == &getFloatTypeSemantics(T: RHS)) |
8006 | return 0; |
8007 | return getFloatingTypeOrder(LHS, RHS); |
8008 | } |
8009 | |
8010 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
8011 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
8012 | /// or if it is not canonicalized. |
8013 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
8014 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
8015 | |
8016 | // Results in this 'losing' to any type of the same size, but winning if |
8017 | // larger. |
8018 | if (const auto *EIT = dyn_cast<BitIntType>(Val: T)) |
8019 | return 0 + (EIT->getNumBits() << 3); |
8020 | |
8021 | switch (cast<BuiltinType>(Val: T)->getKind()) { |
8022 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
8023 | case BuiltinType::Bool: |
8024 | return 1 + (getIntWidth(BoolTy) << 3); |
8025 | case BuiltinType::Char_S: |
8026 | case BuiltinType::Char_U: |
8027 | case BuiltinType::SChar: |
8028 | case BuiltinType::UChar: |
8029 | return 2 + (getIntWidth(CharTy) << 3); |
8030 | case BuiltinType::Short: |
8031 | case BuiltinType::UShort: |
8032 | return 3 + (getIntWidth(ShortTy) << 3); |
8033 | case BuiltinType::Int: |
8034 | case BuiltinType::UInt: |
8035 | return 4 + (getIntWidth(IntTy) << 3); |
8036 | case BuiltinType::Long: |
8037 | case BuiltinType::ULong: |
8038 | return 5 + (getIntWidth(LongTy) << 3); |
8039 | case BuiltinType::LongLong: |
8040 | case BuiltinType::ULongLong: |
8041 | return 6 + (getIntWidth(LongLongTy) << 3); |
8042 | case BuiltinType::Int128: |
8043 | case BuiltinType::UInt128: |
8044 | return 7 + (getIntWidth(Int128Ty) << 3); |
8045 | |
8046 | // "The ranks of char8_t, char16_t, char32_t, and wchar_t equal the ranks of |
8047 | // their underlying types" [c++20 conv.rank] |
8048 | case BuiltinType::Char8: |
8049 | return getIntegerRank(UnsignedCharTy.getTypePtr()); |
8050 | case BuiltinType::Char16: |
8051 | return getIntegerRank( |
8052 | T: getFromTargetType(Type: Target->getChar16Type()).getTypePtr()); |
8053 | case BuiltinType::Char32: |
8054 | return getIntegerRank( |
8055 | T: getFromTargetType(Type: Target->getChar32Type()).getTypePtr()); |
8056 | case BuiltinType::WChar_S: |
8057 | case BuiltinType::WChar_U: |
8058 | return getIntegerRank( |
8059 | T: getFromTargetType(Type: Target->getWCharType()).getTypePtr()); |
8060 | } |
8061 | } |
8062 | |
8063 | /// Whether this is a promotable bitfield reference according |
8064 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
8065 | /// |
8066 | /// \returns the type this bit-field will promote to, or NULL if no |
8067 | /// promotion occurs. |
8068 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
8069 | if (E->isTypeDependent() || E->isValueDependent()) |
8070 | return {}; |
8071 | |
8072 | // C++ [conv.prom]p5: |
8073 | // If the bit-field has an enumerated type, it is treated as any other |
8074 | // value of that type for promotion purposes. |
8075 | if (getLangOpts().CPlusPlus && E->getType()->isEnumeralType()) |
8076 | return {}; |
8077 | |
8078 | // FIXME: We should not do this unless E->refersToBitField() is true. This |
8079 | // matters in C where getSourceBitField() will find bit-fields for various |
8080 | // cases where the source expression is not a bit-field designator. |
8081 | |
8082 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? |
8083 | if (!Field) |
8084 | return {}; |
8085 | |
8086 | QualType FT = Field->getType(); |
8087 | |
8088 | uint64_t BitWidth = Field->getBitWidthValue(); |
8089 | uint64_t IntSize = getTypeSize(IntTy); |
8090 | // C++ [conv.prom]p5: |
8091 | // A prvalue for an integral bit-field can be converted to a prvalue of type |
8092 | // int if int can represent all the values of the bit-field; otherwise, it |
8093 | // can be converted to unsigned int if unsigned int can represent all the |
8094 | // values of the bit-field. If the bit-field is larger yet, no integral |
8095 | // promotion applies to it. |
8096 | // C11 6.3.1.1/2: |
8097 | // [For a bit-field of type _Bool, int, signed int, or unsigned int:] |
8098 | // If an int can represent all values of the original type (as restricted by |
8099 | // the width, for a bit-field), the value is converted to an int; otherwise, |
8100 | // it is converted to an unsigned int. |
8101 | // |
8102 | // FIXME: C does not permit promotion of a 'long : 3' bitfield to int. |
8103 | // We perform that promotion here to match GCC and C++. |
8104 | // FIXME: C does not permit promotion of an enum bit-field whose rank is |
8105 | // greater than that of 'int'. We perform that promotion to match GCC. |
8106 | // |
8107 | // C23 6.3.1.1p2: |
8108 | // The value from a bit-field of a bit-precise integer type is converted to |
8109 | // the corresponding bit-precise integer type. (The rest is the same as in |
8110 | // C11.) |
8111 | if (QualType QT = Field->getType(); QT->isBitIntType()) |
8112 | return QT; |
8113 | |
8114 | if (BitWidth < IntSize) |
8115 | return IntTy; |
8116 | |
8117 | if (BitWidth == IntSize) |
8118 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
8119 | |
8120 | // Bit-fields wider than int are not subject to promotions, and therefore act |
8121 | // like the base type. GCC has some weird bugs in this area that we |
8122 | // deliberately do not follow (GCC follows a pre-standard resolution to |
8123 | // C's DR315 which treats bit-width as being part of the type, and this leaks |
8124 | // into their semantics in some cases). |
8125 | return {}; |
8126 | } |
8127 | |
8128 | /// getPromotedIntegerType - Returns the type that Promotable will |
8129 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
8130 | /// integer type. |
8131 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
8132 | assert(!Promotable.isNull()); |
8133 | assert(isPromotableIntegerType(Promotable)); |
8134 | if (const auto *ET = Promotable->getAs<EnumType>()) |
8135 | return ET->getDecl()->getPromotionType(); |
8136 | |
8137 | if (const auto *BT = Promotable->getAs<BuiltinType>()) { |
8138 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
8139 | // (3.9.1) can be converted to a prvalue of the first of the following |
8140 | // types that can represent all the values of its underlying type: |
8141 | // int, unsigned int, long int, unsigned long int, long long int, or |
8142 | // unsigned long long int [...] |
8143 | // FIXME: Is there some better way to compute this? |
8144 | if (BT->getKind() == BuiltinType::WChar_S || |
8145 | BT->getKind() == BuiltinType::WChar_U || |
8146 | BT->getKind() == BuiltinType::Char8 || |
8147 | BT->getKind() == BuiltinType::Char16 || |
8148 | BT->getKind() == BuiltinType::Char32) { |
8149 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
8150 | uint64_t FromSize = getTypeSize(BT); |
8151 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
8152 | LongLongTy, UnsignedLongLongTy }; |
8153 | for (const auto &PT : PromoteTypes) { |
8154 | uint64_t ToSize = getTypeSize(PT); |
8155 | if (FromSize < ToSize || |
8156 | (FromSize == ToSize && FromIsSigned == PT->isSignedIntegerType())) |
8157 | return PT; |
8158 | } |
8159 | llvm_unreachable("char type should fit into long long"); |
8160 | } |
8161 | } |
8162 | |
8163 | // At this point, we should have a signed or unsigned integer type. |
8164 | if (Promotable->isSignedIntegerType()) |
8165 | return IntTy; |
8166 | uint64_t PromotableSize = getIntWidth(T: Promotable); |
8167 | uint64_t IntSize = getIntWidth(IntTy); |
8168 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
8169 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
8170 | } |
8171 | |
8172 | /// Recurses in pointer/array types until it finds an objc retainable |
8173 | /// type and returns its ownership. |
8174 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
8175 | while (!T.isNull()) { |
8176 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
8177 | return T.getObjCLifetime(); |
8178 | if (T->isArrayType()) |
8179 | T = getBaseElementType(type: T); |
8180 | else if (const auto *PT = T->getAs<PointerType>()) |
8181 | T = PT->getPointeeType(); |
8182 | else if (const auto *RT = T->getAs<ReferenceType>()) |
8183 | T = RT->getPointeeType(); |
8184 | else |
8185 | break; |
8186 | } |
8187 | |
8188 | return Qualifiers::OCL_None; |
8189 | } |
8190 | |
8191 | static const Type *getIntegerTypeForEnum(const EnumType *ET) { |
8192 | // Incomplete enum types are not treated as integer types. |
8193 | // FIXME: In C++, enum types are never integer types. |
8194 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) |
8195 | return ET->getDecl()->getIntegerType().getTypePtr(); |
8196 | return nullptr; |
8197 | } |
8198 | |
8199 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
8200 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
8201 | /// LHS < RHS, return -1. |
8202 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
8203 | const Type *LHSC = getCanonicalType(T: LHS).getTypePtr(); |
8204 | const Type *RHSC = getCanonicalType(T: RHS).getTypePtr(); |
8205 | |
8206 | // Unwrap enums to their underlying type. |
8207 | if (const auto *ET = dyn_cast<EnumType>(Val: LHSC)) |
8208 | LHSC = getIntegerTypeForEnum(ET); |
8209 | if (const auto *ET = dyn_cast<EnumType>(Val: RHSC)) |
8210 | RHSC = getIntegerTypeForEnum(ET); |
8211 | |
8212 | if (LHSC == RHSC) return 0; |
8213 | |
8214 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
8215 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
8216 | |
8217 | unsigned LHSRank = getIntegerRank(T: LHSC); |
8218 | unsigned RHSRank = getIntegerRank(T: RHSC); |
8219 | |
8220 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
8221 | if (LHSRank == RHSRank) return 0; |
8222 | return LHSRank > RHSRank ? 1 : -1; |
8223 | } |
8224 | |
8225 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
8226 | if (LHSUnsigned) { |
8227 | // If the unsigned [LHS] type is larger, return it. |
8228 | if (LHSRank >= RHSRank) |
8229 | return 1; |
8230 | |
8231 | // If the signed type can represent all values of the unsigned type, it |
8232 | // wins. Because we are dealing with 2's complement and types that are |
8233 | // powers of two larger than each other, this is always safe. |
8234 | return -1; |
8235 | } |
8236 | |
8237 | // If the unsigned [RHS] type is larger, return it. |
8238 | if (RHSRank >= LHSRank) |
8239 | return -1; |
8240 | |
8241 | // If the signed type can represent all values of the unsigned type, it |
8242 | // wins. Because we are dealing with 2's complement and types that are |
8243 | // powers of two larger than each other, this is always safe. |
8244 | return 1; |
8245 | } |
8246 | |
8247 | TypedefDecl *ASTContext::getCFConstantStringDecl() const { |
8248 | if (CFConstantStringTypeDecl) |
8249 | return CFConstantStringTypeDecl; |
8250 | |
8251 | assert(!CFConstantStringTagDecl && |
8252 | "tag and typedef should be initialized together"); |
8253 | CFConstantStringTagDecl = buildImplicitRecord(Name: "__NSConstantString_tag"); |
8254 | CFConstantStringTagDecl->startDefinition(); |
8255 | |
8256 | struct { |
8257 | QualType Type; |
8258 | const char *Name; |
8259 | } Fields[5]; |
8260 | unsigned Count = 0; |
8261 | |
8262 | /// Objective-C ABI |
8263 | /// |
8264 | /// typedef struct __NSConstantString_tag { |
8265 | /// const int *isa; |
8266 | /// int flags; |
8267 | /// const char *str; |
8268 | /// long length; |
8269 | /// } __NSConstantString; |
8270 | /// |
8271 | /// Swift ABI (4.1, 4.2) |
8272 | /// |
8273 | /// typedef struct __NSConstantString_tag { |
8274 | /// uintptr_t _cfisa; |
8275 | /// uintptr_t _swift_rc; |
8276 | /// _Atomic(uint64_t) _cfinfoa; |
8277 | /// const char *_ptr; |
8278 | /// uint32_t _length; |
8279 | /// } __NSConstantString; |
8280 | /// |
8281 | /// Swift ABI (5.0) |
8282 | /// |
8283 | /// typedef struct __NSConstantString_tag { |
8284 | /// uintptr_t _cfisa; |
8285 | /// uintptr_t _swift_rc; |
8286 | /// _Atomic(uint64_t) _cfinfoa; |
8287 | /// const char *_ptr; |
8288 | /// uintptr_t _length; |
8289 | /// } __NSConstantString; |
8290 | |
8291 | const auto CFRuntime = getLangOpts().CFRuntime; |
8292 | if (static_cast<unsigned>(CFRuntime) < |
8293 | static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift)) { |
8294 | Fields[Count++] = { getPointerType(IntTy.withConst()), "isa"}; |
8295 | Fields[Count++] = { IntTy, "flags"}; |
8296 | Fields[Count++] = { getPointerType(CharTy.withConst()), "str"}; |
8297 | Fields[Count++] = { LongTy, "length"}; |
8298 | } else { |
8299 | Fields[Count++] = { getUIntPtrType(), "_cfisa"}; |
8300 | Fields[Count++] = { getUIntPtrType(), "_swift_rc"}; |
8301 | Fields[Count++] = { getFromTargetType(Type: Target->getUInt64Type()), "_swift_rc"}; |
8302 | Fields[Count++] = { getPointerType(CharTy.withConst()), "_ptr"}; |
8303 | if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 || |
8304 | CFRuntime == LangOptions::CoreFoundationABI::Swift4_2) |
8305 | Fields[Count++] = { IntTy, "_ptr"}; |
8306 | else |
8307 | Fields[Count++] = { getUIntPtrType(), "_ptr"}; |
8308 | } |
8309 | |
8310 | // Create fields |
8311 | for (unsigned i = 0; i < Count; ++i) { |
8312 | FieldDecl *Field = |
8313 | FieldDecl::Create(C: *this, DC: CFConstantStringTagDecl, StartLoc: SourceLocation(), |
8314 | IdLoc: SourceLocation(), Id: &Idents.get(Name: Fields[i].Name), |
8315 | T: Fields[i].Type, /*TInfo=*/nullptr, |
8316 | /*BitWidth=*/BW: nullptr, /*Mutable=*/false, InitStyle: ICIS_NoInit); |
8317 | Field->setAccess(AS_public); |
8318 | CFConstantStringTagDecl->addDecl(Field); |
8319 | } |
8320 | |
8321 | CFConstantStringTagDecl->completeDefinition(); |
8322 | // This type is designed to be compatible with NSConstantString, but cannot |
8323 | // use the same name, since NSConstantString is an interface. |
8324 | auto tagType = getTagDeclType(CFConstantStringTagDecl); |
8325 | CFConstantStringTypeDecl = |
8326 | buildImplicitTypedef(T: tagType, Name: "__NSConstantString"); |
8327 | |
8328 | return CFConstantStringTypeDecl; |
8329 | } |
8330 | |
8331 | RecordDecl *ASTContext::getCFConstantStringTagDecl() const { |
8332 | if (!CFConstantStringTagDecl) |
8333 | getCFConstantStringDecl(); // Build the tag and the typedef. |
8334 | return CFConstantStringTagDecl; |
8335 | } |
8336 | |
8337 | // getCFConstantStringType - Return the type used for constant CFStrings. |
8338 | QualType ASTContext::getCFConstantStringType() const { |
8339 | return getTypedefType(getCFConstantStringDecl()); |
8340 | } |
8341 | |
8342 | QualType ASTContext::getObjCSuperType() const { |
8343 | if (ObjCSuperType.isNull()) { |
8344 | RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord(Name: "objc_super"); |
8345 | getTranslationUnitDecl()->addDecl(ObjCSuperTypeDecl); |
8346 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
8347 | } |
8348 | return ObjCSuperType; |
8349 | } |
8350 | |
8351 | void ASTContext::setCFConstantStringType(QualType T) { |
8352 | const auto *TD = T->castAs<TypedefType>(); |
8353 | CFConstantStringTypeDecl = cast<TypedefDecl>(Val: TD->getDecl()); |
8354 | const auto *TagType = TD->castAs<RecordType>(); |
8355 | CFConstantStringTagDecl = TagType->getDecl(); |
8356 | } |
8357 | |
8358 | QualType ASTContext::getBlockDescriptorType() const { |
8359 | if (BlockDescriptorType) |
8360 | return getTagDeclType(BlockDescriptorType); |
8361 | |
8362 | RecordDecl *RD; |
8363 | // FIXME: Needs the FlagAppleBlock bit. |
8364 | RD = buildImplicitRecord(Name: "__block_descriptor"); |
8365 | RD->startDefinition(); |
8366 | |
8367 | QualType FieldTypes[] = { |
8368 | UnsignedLongTy, |
8369 | UnsignedLongTy, |
8370 | }; |
8371 | |
8372 | static const char *const FieldNames[] = { |
8373 | "reserved", |
8374 | "Size" |
8375 | }; |
8376 | |
8377 | for (size_t i = 0; i < 2; ++i) { |
8378 | FieldDecl *Field = FieldDecl::Create( |
8379 | *this, RD, SourceLocation(), SourceLocation(), |
8380 | &Idents.get(Name: FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
8381 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); |
8382 | Field->setAccess(AS_public); |
8383 | RD->addDecl(Field); |
8384 | } |
8385 | |
8386 | RD->completeDefinition(); |
8387 | |
8388 | BlockDescriptorType = RD; |
8389 | |
8390 | return getTagDeclType(BlockDescriptorType); |
8391 | } |
8392 | |
8393 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
8394 | if (BlockDescriptorExtendedType) |
8395 | return getTagDeclType(BlockDescriptorExtendedType); |
8396 | |
8397 | RecordDecl *RD; |
8398 | // FIXME: Needs the FlagAppleBlock bit. |
8399 | RD = buildImplicitRecord(Name: "__block_descriptor_withcopydispose"); |
8400 | RD->startDefinition(); |
8401 | |
8402 | QualType FieldTypes[] = { |
8403 | UnsignedLongTy, |
8404 | UnsignedLongTy, |
8405 | getPointerType(VoidPtrTy), |
8406 | getPointerType(VoidPtrTy) |
8407 | }; |
8408 | |
8409 | static const char *const FieldNames[] = { |
8410 | "reserved", |
8411 | "Size", |
8412 | "CopyFuncPtr", |
8413 | "DestroyFuncPtr" |
8414 | }; |
8415 | |
8416 | for (size_t i = 0; i < 4; ++i) { |
8417 | FieldDecl *Field = FieldDecl::Create( |
8418 | *this, RD, SourceLocation(), SourceLocation(), |
8419 | &Idents.get(Name: FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
8420 | /*BitWidth=*/nullptr, |
8421 | /*Mutable=*/false, ICIS_NoInit); |
8422 | Field->setAccess(AS_public); |
8423 | RD->addDecl(Field); |
8424 | } |
8425 | |
8426 | RD->completeDefinition(); |
8427 | |
8428 | BlockDescriptorExtendedType = RD; |
8429 | return getTagDeclType(BlockDescriptorExtendedType); |
8430 | } |
8431 | |
8432 | OpenCLTypeKind ASTContext::getOpenCLTypeKind(const Type *T) const { |
8433 | const auto *BT = dyn_cast<BuiltinType>(Val: T); |
8434 | |
8435 | if (!BT) { |
8436 | if (isa<PipeType>(Val: T)) |
8437 | return OCLTK_Pipe; |
8438 | |
8439 | return OCLTK_Default; |
8440 | } |
8441 | |
8442 | switch (BT->getKind()) { |
8443 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
8444 | case BuiltinType::Id: \ |
8445 | return OCLTK_Image; |
8446 | #include "clang/Basic/OpenCLImageTypes.def" |
8447 | |
8448 | case BuiltinType::OCLClkEvent: |
8449 | return OCLTK_ClkEvent; |
8450 | |
8451 | case BuiltinType::OCLEvent: |
8452 | return OCLTK_Event; |
8453 | |
8454 | case BuiltinType::OCLQueue: |
8455 | return OCLTK_Queue; |
8456 | |
8457 | case BuiltinType::OCLReserveID: |
8458 | return OCLTK_ReserveID; |
8459 | |
8460 | case BuiltinType::OCLSampler: |
8461 | return OCLTK_Sampler; |
8462 | |
8463 | default: |
8464 | return OCLTK_Default; |
8465 | } |
8466 | } |
8467 | |
8468 | LangAS ASTContext::getOpenCLTypeAddrSpace(const Type *T) const { |
8469 | return Target->getOpenCLTypeAddrSpace(TK: getOpenCLTypeKind(T)); |
8470 | } |
8471 | |
8472 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
8473 | /// requires copy/dispose. Note that this must match the logic |
8474 | /// in buildByrefHelpers. |
8475 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
8476 | const VarDecl *D) { |
8477 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
8478 | const Expr *copyExpr = getBlockVarCopyInit(VD: D).getCopyExpr(); |
8479 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
8480 | |
8481 | return true; |
8482 | } |
8483 | |
8484 | if (Ty.hasAddressDiscriminatedPointerAuth()) |
8485 | return true; |
8486 | |
8487 | // The block needs copy/destroy helpers if Ty is non-trivial to destructively |
8488 | // move or destroy. |
8489 | if (Ty.isNonTrivialToPrimitiveDestructiveMove() || Ty.isDestructedType()) |
8490 | return true; |
8491 | |
8492 | if (!Ty->isObjCRetainableType()) return false; |
8493 | |
8494 | Qualifiers qs = Ty.getQualifiers(); |
8495 | |
8496 | // If we have lifetime, that dominates. |
8497 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
8498 | switch (lifetime) { |
8499 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
8500 | |
8501 | // These are just bits as far as the runtime is concerned. |
8502 | case Qualifiers::OCL_ExplicitNone: |
8503 | case Qualifiers::OCL_Autoreleasing: |
8504 | return false; |
8505 | |
8506 | // These cases should have been taken care of when checking the type's |
8507 | // non-triviality. |
8508 | case Qualifiers::OCL_Weak: |
8509 | case Qualifiers::OCL_Strong: |
8510 | llvm_unreachable("impossible"); |
8511 | } |
8512 | llvm_unreachable("fell out of lifetime switch!"); |
8513 | } |
8514 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
8515 | Ty->isObjCObjectPointerType()); |
8516 | } |
8517 | |
8518 | bool ASTContext::getByrefLifetime(QualType Ty, |
8519 | Qualifiers::ObjCLifetime &LifeTime, |
8520 | bool &HasByrefExtendedLayout) const { |
8521 | if (!getLangOpts().ObjC || |
8522 | getLangOpts().getGC() != LangOptions::NonGC) |
8523 | return false; |
8524 | |
8525 | HasByrefExtendedLayout = false; |
8526 | if (Ty->isRecordType()) { |
8527 | HasByrefExtendedLayout = true; |
8528 | LifeTime = Qualifiers::OCL_None; |
8529 | } else if ((LifeTime = Ty.getObjCLifetime())) { |
8530 | // Honor the ARC qualifiers. |
8531 | } else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) { |
8532 | // The MRR rule. |
8533 | LifeTime = Qualifiers::OCL_ExplicitNone; |
8534 | } else { |
8535 | LifeTime = Qualifiers::OCL_None; |
8536 | } |
8537 | return true; |
8538 | } |
8539 | |
8540 | CanQualType ASTContext::getNSUIntegerType() const { |
8541 | assert(Target && "Expected target to be initialized"); |
8542 | const llvm::Triple &T = Target->getTriple(); |
8543 | // Windows is LLP64 rather than LP64 |
8544 | if (T.isOSWindows() && T.isArch64Bit()) |
8545 | return UnsignedLongLongTy; |
8546 | return UnsignedLongTy; |
8547 | } |
8548 | |
8549 | CanQualType ASTContext::getNSIntegerType() const { |
8550 | assert(Target && "Expected target to be initialized"); |
8551 | const llvm::Triple &T = Target->getTriple(); |
8552 | // Windows is LLP64 rather than LP64 |
8553 | if (T.isOSWindows() && T.isArch64Bit()) |
8554 | return LongLongTy; |
8555 | return LongTy; |
8556 | } |
8557 | |
8558 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
8559 | if (!ObjCInstanceTypeDecl) |
8560 | ObjCInstanceTypeDecl = |
8561 | buildImplicitTypedef(T: getObjCIdType(), Name: "instancetype"); |
8562 | return ObjCInstanceTypeDecl; |
8563 | } |
8564 | |
8565 | // This returns true if a type has been typedefed to BOOL: |
8566 | // typedef <type> BOOL; |
8567 | static bool isTypeTypedefedAsBOOL(QualType T) { |
8568 | if (const auto *TT = dyn_cast<TypedefType>(Val&: T)) |
8569 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
8570 | return II->isStr(Str: "BOOL"); |
8571 | |
8572 | return false; |
8573 | } |
8574 | |
8575 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
8576 | /// purpose. |
8577 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
8578 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
8579 | return CharUnits::Zero(); |
8580 | |
8581 | CharUnits sz = getTypeSizeInChars(T: type); |
8582 | |
8583 | // Make all integer and enum types at least as large as an int |
8584 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
8585 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
8586 | // Treat arrays as pointers, since that's how they're passed in. |
8587 | else if (type->isArrayType()) |
8588 | sz = getTypeSizeInChars(VoidPtrTy); |
8589 | return sz; |
8590 | } |
8591 | |
8592 | bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const { |
8593 | return getTargetInfo().getCXXABI().isMicrosoft() && |
8594 | VD->isStaticDataMember() && |
8595 | VD->getType()->isIntegralOrEnumerationType() && |
8596 | !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit(); |
8597 | } |
8598 | |
8599 | ASTContext::InlineVariableDefinitionKind |
8600 | ASTContext::getInlineVariableDefinitionKind(const VarDecl *VD) const { |
8601 | if (!VD->isInline()) |
8602 | return InlineVariableDefinitionKind::None; |
8603 | |
8604 | // In almost all cases, it's a weak definition. |
8605 | auto *First = VD->getFirstDecl(); |
8606 | if (First->isInlineSpecified() || !First->isStaticDataMember()) |
8607 | return InlineVariableDefinitionKind::Weak; |
8608 | |
8609 | // If there's a file-context declaration in this translation unit, it's a |
8610 | // non-discardable definition. |
8611 | for (auto *D : VD->redecls()) |
8612 | if (D->getLexicalDeclContext()->isFileContext() && |
8613 | !D->isInlineSpecified() && (D->isConstexpr() || First->isConstexpr())) |
8614 | return InlineVariableDefinitionKind::Strong; |
8615 | |
8616 | // If we've not seen one yet, we don't know. |
8617 | return InlineVariableDefinitionKind::WeakUnknown; |
8618 | } |
8619 | |
8620 | static std::string charUnitsToString(const CharUnits &CU) { |
8621 | return llvm::itostr(X: CU.getQuantity()); |
8622 | } |
8623 | |
8624 | /// getObjCEncodingForBlock - Return the encoded type for this block |
8625 | /// declaration. |
8626 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
8627 | std::string S; |
8628 | |
8629 | const BlockDecl *Decl = Expr->getBlockDecl(); |
8630 | QualType BlockTy = |
8631 | Expr->getType()->castAs<BlockPointerType>()->getPointeeType(); |
8632 | QualType BlockReturnTy = BlockTy->castAs<FunctionType>()->getReturnType(); |
8633 | // Encode result type. |
8634 | if (getLangOpts().EncodeExtendedBlockSig) |
8635 | getObjCEncodingForMethodParameter(QT: Decl::OBJC_TQ_None, T: BlockReturnTy, S, |
8636 | Extended: true /*Extended*/); |
8637 | else |
8638 | getObjCEncodingForType(T: BlockReturnTy, S); |
8639 | // Compute size of all parameters. |
8640 | // Start with computing size of a pointer in number of bytes. |
8641 | // FIXME: There might(should) be a better way of doing this computation! |
8642 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
8643 | CharUnits ParmOffset = PtrSize; |
8644 | for (auto *PI : Decl->parameters()) { |
8645 | QualType PType = PI->getType(); |
8646 | CharUnits sz = getObjCEncodingTypeSize(type: PType); |
8647 | if (sz.isZero()) |
8648 | continue; |
8649 | assert(sz.isPositive() && "BlockExpr - Incomplete param type"); |
8650 | ParmOffset += sz; |
8651 | } |
8652 | // Size of the argument frame |
8653 | S += charUnitsToString(CU: ParmOffset); |
8654 | // Block pointer and offset. |
8655 | S += "@?0"; |
8656 | |
8657 | // Argument types. |
8658 | ParmOffset = PtrSize; |
8659 | for (auto *PVDecl : Decl->parameters()) { |
8660 | QualType PType = PVDecl->getOriginalType(); |
8661 | if (const auto *AT = |
8662 | dyn_cast<ArrayType>(Val: PType->getCanonicalTypeInternal())) { |
8663 | // Use array's original type only if it has known number of |
8664 | // elements. |
8665 | if (!isa<ConstantArrayType>(Val: AT)) |
8666 | PType = PVDecl->getType(); |
8667 | } else if (PType->isFunctionType()) |
8668 | PType = PVDecl->getType(); |
8669 | if (getLangOpts().EncodeExtendedBlockSig) |
8670 | getObjCEncodingForMethodParameter(QT: Decl::OBJC_TQ_None, T: PType, |
8671 | S, Extended: true /*Extended*/); |
8672 | else |
8673 | getObjCEncodingForType(T: PType, S); |
8674 | S += charUnitsToString(CU: ParmOffset); |
8675 | ParmOffset += getObjCEncodingTypeSize(type: PType); |
8676 | } |
8677 | |
8678 | return S; |
8679 | } |
8680 | |
8681 | std::string |
8682 | ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl) const { |
8683 | std::string S; |
8684 | // Encode result type. |
8685 | getObjCEncodingForType(T: Decl->getReturnType(), S); |
8686 | CharUnits ParmOffset; |
8687 | // Compute size of all parameters. |
8688 | for (auto *PI : Decl->parameters()) { |
8689 | QualType PType = PI->getType(); |
8690 | CharUnits sz = getObjCEncodingTypeSize(type: PType); |
8691 | if (sz.isZero()) |
8692 | continue; |
8693 | |
8694 | assert(sz.isPositive() && |
8695 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
8696 | ParmOffset += sz; |
8697 | } |
8698 | S += charUnitsToString(CU: ParmOffset); |
8699 | ParmOffset = CharUnits::Zero(); |
8700 | |
8701 | // Argument types. |
8702 | for (auto *PVDecl : Decl->parameters()) { |
8703 | QualType PType = PVDecl->getOriginalType(); |
8704 | if (const auto *AT = |
8705 | dyn_cast<ArrayType>(Val: PType->getCanonicalTypeInternal())) { |
8706 | // Use array's original type only if it has known number of |
8707 | // elements. |
8708 | if (!isa<ConstantArrayType>(Val: AT)) |
8709 | PType = PVDecl->getType(); |
8710 | } else if (PType->isFunctionType()) |
8711 | PType = PVDecl->getType(); |
8712 | getObjCEncodingForType(T: PType, S); |
8713 | S += charUnitsToString(CU: ParmOffset); |
8714 | ParmOffset += getObjCEncodingTypeSize(type: PType); |
8715 | } |
8716 | |
8717 | return S; |
8718 | } |
8719 | |
8720 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
8721 | /// method parameter or return type. If Extended, include class names and |
8722 | /// block object types. |
8723 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
8724 | QualType T, std::string& S, |
8725 | bool Extended) const { |
8726 | // Encode type qualifier, 'in', 'inout', etc. for the parameter. |
8727 | getObjCEncodingForTypeQualifier(QT, S); |
8728 | // Encode parameter type. |
8729 | ObjCEncOptions Options = ObjCEncOptions() |
8730 | .setExpandPointedToStructures() |
8731 | .setExpandStructures() |
8732 | .setIsOutermostType(); |
8733 | if (Extended) |
8734 | Options.setEncodeBlockParameters().setEncodeClassNames(); |
8735 | getObjCEncodingForTypeImpl(t: T, S, Options, /*Field=*/nullptr); |
8736 | } |
8737 | |
8738 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
8739 | /// declaration. |
8740 | std::string ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
8741 | bool Extended) const { |
8742 | // FIXME: This is not very efficient. |
8743 | // Encode return type. |
8744 | std::string S; |
8745 | getObjCEncodingForMethodParameter(QT: Decl->getObjCDeclQualifier(), |
8746 | T: Decl->getReturnType(), S, Extended); |
8747 | // Compute size of all parameters. |
8748 | // Start with computing size of a pointer in number of bytes. |
8749 | // FIXME: There might(should) be a better way of doing this computation! |
8750 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
8751 | // The first two arguments (self and _cmd) are pointers; account for |
8752 | // their size. |
8753 | CharUnits ParmOffset = 2 * PtrSize; |
8754 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
8755 | E = Decl->sel_param_end(); PI != E; ++PI) { |
8756 | QualType PType = (*PI)->getType(); |
8757 | CharUnits sz = getObjCEncodingTypeSize(type: PType); |
8758 | if (sz.isZero()) |
8759 | continue; |
8760 | |
8761 | assert(sz.isPositive() && |
8762 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
8763 | ParmOffset += sz; |
8764 | } |
8765 | S += charUnitsToString(CU: ParmOffset); |
8766 | S += "@0:"; |
8767 | S += charUnitsToString(CU: PtrSize); |
8768 | |
8769 | // Argument types. |
8770 | ParmOffset = 2 * PtrSize; |
8771 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
8772 | E = Decl->sel_param_end(); PI != E; ++PI) { |
8773 | const ParmVarDecl *PVDecl = *PI; |
8774 | QualType PType = PVDecl->getOriginalType(); |
8775 | if (const auto *AT = |
8776 | dyn_cast<ArrayType>(Val: PType->getCanonicalTypeInternal())) { |
8777 | // Use array's original type only if it has known number of |
8778 | // elements. |
8779 | if (!isa<ConstantArrayType>(Val: AT)) |
8780 | PType = PVDecl->getType(); |
8781 | } else if (PType->isFunctionType()) |
8782 | PType = PVDecl->getType(); |
8783 | getObjCEncodingForMethodParameter(QT: PVDecl->getObjCDeclQualifier(), |
8784 | T: PType, S, Extended); |
8785 | S += charUnitsToString(CU: ParmOffset); |
8786 | ParmOffset += getObjCEncodingTypeSize(type: PType); |
8787 | } |
8788 | |
8789 | return S; |
8790 | } |
8791 | |
8792 | ObjCPropertyImplDecl * |
8793 | ASTContext::getObjCPropertyImplDeclForPropertyDecl( |
8794 | const ObjCPropertyDecl *PD, |
8795 | const Decl *Container) const { |
8796 | if (!Container) |
8797 | return nullptr; |
8798 | if (const auto *CID = dyn_cast<ObjCCategoryImplDecl>(Val: Container)) { |
8799 | for (auto *PID : CID->property_impls()) |
8800 | if (PID->getPropertyDecl() == PD) |
8801 | return PID; |
8802 | } else { |
8803 | const auto *OID = cast<ObjCImplementationDecl>(Val: Container); |
8804 | for (auto *PID : OID->property_impls()) |
8805 | if (PID->getPropertyDecl() == PD) |
8806 | return PID; |
8807 | } |
8808 | return nullptr; |
8809 | } |
8810 | |
8811 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
8812 | /// property declaration. If non-NULL, Container must be either an |
8813 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
8814 | /// NULL when getting encodings for protocol properties. |
8815 | /// Property attributes are stored as a comma-delimited C string. The simple |
8816 | /// attributes readonly and bycopy are encoded as single characters. The |
8817 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
8818 | /// encoded as single characters, followed by an identifier. Property types |
8819 | /// are also encoded as a parametrized attribute. The characters used to encode |
8820 | /// these attributes are defined by the following enumeration: |
8821 | /// @code |
8822 | /// enum PropertyAttributes { |
8823 | /// kPropertyReadOnly = 'R', // property is read-only. |
8824 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
8825 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
8826 | /// kPropertyDynamic = 'D', // property is dynamic |
8827 | /// kPropertyGetter = 'G', // followed by getter selector name |
8828 | /// kPropertySetter = 'S', // followed by setter selector name |
8829 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
8830 | /// kPropertyType = 'T' // followed by old-style type encoding. |
8831 | /// kPropertyWeak = 'W' // 'weak' property |
8832 | /// kPropertyStrong = 'P' // property GC'able |
8833 | /// kPropertyNonAtomic = 'N' // property non-atomic |
8834 | /// kPropertyOptional = '?' // property optional |
8835 | /// }; |
8836 | /// @endcode |
8837 | std::string |
8838 | ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
8839 | const Decl *Container) const { |
8840 | // Collect information from the property implementation decl(s). |
8841 | bool Dynamic = false; |
8842 | ObjCPropertyImplDecl *SynthesizePID = nullptr; |
8843 | |
8844 | if (ObjCPropertyImplDecl *PropertyImpDecl = |
8845 | getObjCPropertyImplDeclForPropertyDecl(PD, Container)) { |
8846 | if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
8847 | Dynamic = true; |
8848 | else |
8849 | SynthesizePID = PropertyImpDecl; |
8850 | } |
8851 | |
8852 | // FIXME: This is not very efficient. |
8853 | std::string S = "T"; |
8854 | |
8855 | // Encode result type. |
8856 | // GCC has some special rules regarding encoding of properties which |
8857 | // closely resembles encoding of ivars. |
8858 | getObjCEncodingForPropertyType(T: PD->getType(), S); |
8859 | |
8860 | if (PD->isOptional()) |
8861 | S += ",?"; |
8862 | |
8863 | if (PD->isReadOnly()) { |
8864 | S += ",R"; |
8865 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy) |
8866 | S += ",C"; |
8867 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_retain) |
8868 | S += ",&"; |
8869 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak) |
8870 | S += ",W"; |
8871 | } else { |
8872 | switch (PD->getSetterKind()) { |
8873 | case ObjCPropertyDecl::Assign: break; |
8874 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
8875 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
8876 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
8877 | } |
8878 | } |
8879 | |
8880 | // It really isn't clear at all what this means, since properties |
8881 | // are "dynamic by default". |
8882 | if (Dynamic) |
8883 | S += ",D"; |
8884 | |
8885 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_nonatomic) |
8886 | S += ",N"; |
8887 | |
8888 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_getter) { |
8889 | S += ",G"; |
8890 | S += PD->getGetterName().getAsString(); |
8891 | } |
8892 | |
8893 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_setter) { |
8894 | S += ",S"; |
8895 | S += PD->getSetterName().getAsString(); |
8896 | } |
8897 | |
8898 | if (SynthesizePID) { |
8899 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
8900 | S += ",V"; |
8901 | S += OID->getNameAsString(); |
8902 | } |
8903 | |
8904 | // FIXME: OBJCGC: weak & strong |
8905 | return S; |
8906 | } |
8907 | |
8908 | /// getLegacyIntegralTypeEncoding - |
8909 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
8910 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
8911 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
8912 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
8913 | if (PointeeTy->getAs<TypedefType>()) { |
8914 | if (const auto *BT = PointeeTy->getAs<BuiltinType>()) { |
8915 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
8916 | PointeeTy = UnsignedIntTy; |
8917 | else |
8918 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
8919 | PointeeTy = IntTy; |
8920 | } |
8921 | } |
8922 | } |
8923 | |
8924 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
8925 | const FieldDecl *Field, |
8926 | QualType *NotEncodedT) const { |
8927 | // We follow the behavior of gcc, expanding structures which are |
8928 | // directly pointed to, and expanding embedded structures. Note that |
8929 | // these rules are sufficient to prevent recursive encoding of the |
8930 | // same type. |
8931 | getObjCEncodingForTypeImpl(t: T, S, |
8932 | Options: ObjCEncOptions() |
8933 | .setExpandPointedToStructures() |
8934 | .setExpandStructures() |
8935 | .setIsOutermostType(), |
8936 | Field, NotEncodedT); |
8937 | } |
8938 | |
8939 | void ASTContext::getObjCEncodingForPropertyType(QualType T, |
8940 | std::string& S) const { |
8941 | // Encode result type. |
8942 | // GCC has some special rules regarding encoding of properties which |
8943 | // closely resembles encoding of ivars. |
8944 | getObjCEncodingForTypeImpl(t: T, S, |
8945 | Options: ObjCEncOptions() |
8946 | .setExpandPointedToStructures() |
8947 | .setExpandStructures() |
8948 | .setIsOutermostType() |
8949 | .setEncodingProperty(), |
8950 | /*Field=*/nullptr); |
8951 | } |
8952 | |
8953 | static char getObjCEncodingForPrimitiveType(const ASTContext *C, |
8954 | const BuiltinType *BT) { |
8955 | BuiltinType::Kind kind = BT->getKind(); |
8956 | switch (kind) { |
8957 | case BuiltinType::Void: return 'v'; |
8958 | case BuiltinType::Bool: return 'B'; |
8959 | case BuiltinType::Char8: |
8960 | case BuiltinType::Char_U: |
8961 | case BuiltinType::UChar: return 'C'; |
8962 | case BuiltinType::Char16: |
8963 | case BuiltinType::UShort: return 'S'; |
8964 | case BuiltinType::Char32: |
8965 | case BuiltinType::UInt: return 'I'; |
8966 | case BuiltinType::ULong: |
8967 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
8968 | case BuiltinType::UInt128: return 'T'; |
8969 | case BuiltinType::ULongLong: return 'Q'; |
8970 | case BuiltinType::Char_S: |
8971 | case BuiltinType::SChar: return 'c'; |
8972 | case BuiltinType::Short: return 's'; |
8973 | case BuiltinType::WChar_S: |
8974 | case BuiltinType::WChar_U: |
8975 | case BuiltinType::Int: return 'i'; |
8976 | case BuiltinType::Long: |
8977 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
8978 | case BuiltinType::LongLong: return 'q'; |
8979 | case BuiltinType::Int128: return 't'; |
8980 | case BuiltinType::Float: return 'f'; |
8981 | case BuiltinType::Double: return 'd'; |
8982 | case BuiltinType::LongDouble: return 'D'; |
8983 | case BuiltinType::NullPtr: return '*'; // like char* |
8984 | |
8985 | case BuiltinType::BFloat16: |
8986 | case BuiltinType::Float16: |
8987 | case BuiltinType::Float128: |
8988 | case BuiltinType::Ibm128: |
8989 | case BuiltinType::Half: |
8990 | case BuiltinType::ShortAccum: |
8991 | case BuiltinType::Accum: |
8992 | case BuiltinType::LongAccum: |
8993 | case BuiltinType::UShortAccum: |
8994 | case BuiltinType::UAccum: |
8995 | case BuiltinType::ULongAccum: |
8996 | case BuiltinType::ShortFract: |
8997 | case BuiltinType::Fract: |
8998 | case BuiltinType::LongFract: |
8999 | case BuiltinType::UShortFract: |
9000 | case BuiltinType::UFract: |
9001 | case BuiltinType::ULongFract: |
9002 | case BuiltinType::SatShortAccum: |
9003 | case BuiltinType::SatAccum: |
9004 | case BuiltinType::SatLongAccum: |
9005 | case BuiltinType::SatUShortAccum: |
9006 | case BuiltinType::SatUAccum: |
9007 | case BuiltinType::SatULongAccum: |
9008 | case BuiltinType::SatShortFract: |
9009 | case BuiltinType::SatFract: |
9010 | case BuiltinType::SatLongFract: |
9011 | case BuiltinType::SatUShortFract: |
9012 | case BuiltinType::SatUFract: |
9013 | case BuiltinType::SatULongFract: |
9014 | // FIXME: potentially need @encodes for these! |
9015 | return ' '; |
9016 | |
9017 | #define SVE_TYPE(Name, Id, SingletonId) \ |
9018 | case BuiltinType::Id: |
9019 | #include "clang/Basic/AArch64ACLETypes.def" |
9020 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
9021 | #include "clang/Basic/RISCVVTypes.def" |
9022 | #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
9023 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
9024 | #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id: |
9025 | #include "clang/Basic/AMDGPUTypes.def" |
9026 | { |
9027 | DiagnosticsEngine &Diags = C->getDiagnostics(); |
9028 | unsigned DiagID = Diags.getCustomDiagID(L: DiagnosticsEngine::Error, |
9029 | FormatString: "cannot yet @encode type %0"); |
9030 | Diags.Report(DiagID) << BT->getName(Policy: C->getPrintingPolicy()); |
9031 | return ' '; |
9032 | } |
9033 | |
9034 | case BuiltinType::ObjCId: |
9035 | case BuiltinType::ObjCClass: |
9036 | case BuiltinType::ObjCSel: |
9037 | llvm_unreachable("@encoding ObjC primitive type"); |
9038 | |
9039 | // OpenCL and placeholder types don't need @encodings. |
9040 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
9041 | case BuiltinType::Id: |
9042 | #include "clang/Basic/OpenCLImageTypes.def" |
9043 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
9044 | case BuiltinType::Id: |
9045 | #include "clang/Basic/OpenCLExtensionTypes.def" |
9046 | case BuiltinType::OCLEvent: |
9047 | case BuiltinType::OCLClkEvent: |
9048 | case BuiltinType::OCLQueue: |
9049 | case BuiltinType::OCLReserveID: |
9050 | case BuiltinType::OCLSampler: |
9051 | case BuiltinType::Dependent: |
9052 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
9053 | case BuiltinType::Id: |
9054 | #include "clang/Basic/PPCTypes.def" |
9055 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
9056 | #include "clang/Basic/HLSLIntangibleTypes.def" |
9057 | #define BUILTIN_TYPE(KIND, ID) |
9058 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
9059 | case BuiltinType::KIND: |
9060 | #include "clang/AST/BuiltinTypes.def" |
9061 | llvm_unreachable("invalid builtin type for @encode"); |
9062 | } |
9063 | llvm_unreachable("invalid BuiltinType::Kind value"); |
9064 | } |
9065 | |
9066 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
9067 | EnumDecl *Enum = ET->getDecl(); |
9068 | |
9069 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
9070 | if (!Enum->isFixed()) |
9071 | return 'i'; |
9072 | |
9073 | // The encoding of a fixed enum type matches its fixed underlying type. |
9074 | const auto *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
9075 | return getObjCEncodingForPrimitiveType(C, BT); |
9076 | } |
9077 | |
9078 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
9079 | QualType T, const FieldDecl *FD) { |
9080 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
9081 | S += 'b'; |
9082 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
9083 | // The GNU runtime requires more information; bitfields are encoded as b, |
9084 | // then the offset (in bits) of the first element, then the type of the |
9085 | // bitfield, then the size in bits. For example, in this structure: |
9086 | // |
9087 | // struct |
9088 | // { |
9089 | // int integer; |
9090 | // int flags:2; |
9091 | // }; |
9092 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
9093 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
9094 | // information is not especially sensible, but we're stuck with it for |
9095 | // compatibility with GCC, although providing it breaks anything that |
9096 | // actually uses runtime introspection and wants to work on both runtimes... |
9097 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
9098 | uint64_t Offset; |
9099 | |
9100 | if (const auto *IVD = dyn_cast<ObjCIvarDecl>(Val: FD)) { |
9101 | Offset = Ctx->lookupFieldBitOffset(OID: IVD->getContainingInterface(), Ivar: IVD); |
9102 | } else { |
9103 | const RecordDecl *RD = FD->getParent(); |
9104 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(D: RD); |
9105 | Offset = RL.getFieldOffset(FieldNo: FD->getFieldIndex()); |
9106 | } |
9107 | |
9108 | S += llvm::utostr(X: Offset); |
9109 | |
9110 | if (const auto *ET = T->getAs<EnumType>()) |
9111 | S += ObjCEncodingForEnumType(C: Ctx, ET); |
9112 | else { |
9113 | const auto *BT = T->castAs<BuiltinType>(); |
9114 | S += getObjCEncodingForPrimitiveType(C: Ctx, BT); |
9115 | } |
9116 | } |
9117 | S += llvm::utostr(X: FD->getBitWidthValue()); |
9118 | } |
9119 | |
9120 | // Helper function for determining whether the encoded type string would include |
9121 | // a template specialization type. |
9122 | static bool hasTemplateSpecializationInEncodedString(const Type *T, |
9123 | bool VisitBasesAndFields) { |
9124 | T = T->getBaseElementTypeUnsafe(); |
9125 | |
9126 | if (auto *PT = T->getAs<PointerType>()) |
9127 | return hasTemplateSpecializationInEncodedString( |
9128 | T: PT->getPointeeType().getTypePtr(), VisitBasesAndFields: false); |
9129 | |
9130 | auto *CXXRD = T->getAsCXXRecordDecl(); |
9131 | |
9132 | if (!CXXRD) |
9133 | return false; |
9134 | |
9135 | if (isa<ClassTemplateSpecializationDecl>(Val: CXXRD)) |
9136 | return true; |
9137 | |
9138 | if (!CXXRD->hasDefinition() || !VisitBasesAndFields) |
9139 | return false; |
9140 | |
9141 | for (const auto &B : CXXRD->bases()) |
9142 | if (hasTemplateSpecializationInEncodedString(T: B.getType().getTypePtr(), |
9143 | VisitBasesAndFields: true)) |
9144 | return true; |
9145 | |
9146 | for (auto *FD : CXXRD->fields()) |
9147 | if (hasTemplateSpecializationInEncodedString(FD->getType().getTypePtr(), |
9148 | true)) |
9149 | return true; |
9150 | |
9151 | return false; |
9152 | } |
9153 | |
9154 | // FIXME: Use SmallString for accumulating string. |
9155 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string &S, |
9156 | const ObjCEncOptions Options, |
9157 | const FieldDecl *FD, |
9158 | QualType *NotEncodedT) const { |
9159 | CanQualType CT = getCanonicalType(T); |
9160 | switch (CT->getTypeClass()) { |
9161 | case Type::Builtin: |
9162 | case Type::Enum: |
9163 | if (FD && FD->isBitField()) |
9164 | return EncodeBitField(Ctx: this, S, T, FD); |
9165 | if (const auto *BT = dyn_cast<BuiltinType>(Val&: CT)) |
9166 | S += getObjCEncodingForPrimitiveType(C: this, BT); |
9167 | else |
9168 | S += ObjCEncodingForEnumType(C: this, ET: cast<EnumType>(Val&: CT)); |
9169 | return; |
9170 | |
9171 | case Type::Complex: |
9172 | S += 'j'; |
9173 | getObjCEncodingForTypeImpl(T: T->castAs<ComplexType>()->getElementType(), S, |
9174 | Options: ObjCEncOptions(), |
9175 | /*Field=*/FD: nullptr); |
9176 | return; |
9177 | |
9178 | case Type::Atomic: |
9179 | S += 'A'; |
9180 | getObjCEncodingForTypeImpl(T: T->castAs<AtomicType>()->getValueType(), S, |
9181 | Options: ObjCEncOptions(), |
9182 | /*Field=*/FD: nullptr); |
9183 | return; |
9184 | |
9185 | // encoding for pointer or reference types. |
9186 | case Type::Pointer: |
9187 | case Type::LValueReference: |
9188 | case Type::RValueReference: { |
9189 | QualType PointeeTy; |
9190 | if (isa<PointerType>(Val: CT)) { |
9191 | const auto *PT = T->castAs<PointerType>(); |
9192 | if (PT->isObjCSelType()) { |
9193 | S += ':'; |
9194 | return; |
9195 | } |
9196 | PointeeTy = PT->getPointeeType(); |
9197 | } else { |
9198 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
9199 | } |
9200 | |
9201 | bool isReadOnly = false; |
9202 | // For historical/compatibility reasons, the read-only qualifier of the |
9203 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
9204 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
9205 | // Also, do not emit the 'r' for anything but the outermost type! |
9206 | if (T->getAs<TypedefType>()) { |
9207 | if (Options.IsOutermostType() && T.isConstQualified()) { |
9208 | isReadOnly = true; |
9209 | S += 'r'; |
9210 | } |
9211 | } else if (Options.IsOutermostType()) { |
9212 | QualType P = PointeeTy; |
9213 | while (auto PT = P->getAs<PointerType>()) |
9214 | P = PT->getPointeeType(); |
9215 | if (P.isConstQualified()) { |
9216 | isReadOnly = true; |
9217 | S += 'r'; |
9218 | } |
9219 | } |
9220 | if (isReadOnly) { |
9221 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
9222 | // combinations need to be rearranged. |
9223 | // Rewrite "in const" from "nr" to "rn" |
9224 | if (StringRef(S).ends_with(Suffix: "nr")) |
9225 | S.replace(i1: S.end()-2, i2: S.end(), s: "rn"); |
9226 | } |
9227 | |
9228 | if (PointeeTy->isCharType()) { |
9229 | // char pointer types should be encoded as '*' unless it is a |
9230 | // type that has been typedef'd to 'BOOL'. |
9231 | if (!isTypeTypedefedAsBOOL(T: PointeeTy)) { |
9232 | S += '*'; |
9233 | return; |
9234 | } |
9235 | } else if (const auto *RTy = PointeeTy->getAs<RecordType>()) { |
9236 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
9237 | if (RTy->getDecl()->getIdentifier() == &Idents.get(Name: "objc_class")) { |
9238 | S += '#'; |
9239 | return; |
9240 | } |
9241 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
9242 | if (RTy->getDecl()->getIdentifier() == &Idents.get(Name: "objc_object")) { |
9243 | S += '@'; |
9244 | return; |
9245 | } |
9246 | // If the encoded string for the class includes template names, just emit |
9247 | // "^v" for pointers to the class. |
9248 | if (getLangOpts().CPlusPlus && |
9249 | (!getLangOpts().EncodeCXXClassTemplateSpec && |
9250 | hasTemplateSpecializationInEncodedString( |
9251 | RTy, Options.ExpandPointedToStructures()))) { |
9252 | S += "^v"; |
9253 | return; |
9254 | } |
9255 | // fall through... |
9256 | } |
9257 | S += '^'; |
9258 | getLegacyIntegralTypeEncoding(PointeeTy); |
9259 | |
9260 | ObjCEncOptions NewOptions; |
9261 | if (Options.ExpandPointedToStructures()) |
9262 | NewOptions.setExpandStructures(); |
9263 | getObjCEncodingForTypeImpl(T: PointeeTy, S, Options: NewOptions, |
9264 | /*Field=*/FD: nullptr, NotEncodedT); |
9265 | return; |
9266 | } |
9267 | |
9268 | case Type::ConstantArray: |
9269 | case Type::IncompleteArray: |
9270 | case Type::VariableArray: { |
9271 | const auto *AT = cast<ArrayType>(Val&: CT); |
9272 | |
9273 | if (isa<IncompleteArrayType>(Val: AT) && !Options.IsStructField()) { |
9274 | // Incomplete arrays are encoded as a pointer to the array element. |
9275 | S += '^'; |
9276 | |
9277 | getObjCEncodingForTypeImpl( |
9278 | T: AT->getElementType(), S, |
9279 | Options: Options.keepingOnly(Mask: ObjCEncOptions().setExpandStructures()), FD); |
9280 | } else { |
9281 | S += '['; |
9282 | |
9283 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) |
9284 | S += llvm::utostr(X: CAT->getZExtSize()); |
9285 | else { |
9286 | //Variable length arrays are encoded as a regular array with 0 elements. |
9287 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
9288 | "Unknown array type!"); |
9289 | S += '0'; |
9290 | } |
9291 | |
9292 | getObjCEncodingForTypeImpl( |
9293 | T: AT->getElementType(), S, |
9294 | Options: Options.keepingOnly(Mask: ObjCEncOptions().setExpandStructures()), FD, |
9295 | NotEncodedT); |
9296 | S += ']'; |
9297 | } |
9298 | return; |
9299 | } |
9300 | |
9301 | case Type::FunctionNoProto: |
9302 | case Type::FunctionProto: |
9303 | S += '?'; |
9304 | return; |
9305 | |
9306 | case Type::Record: { |
9307 | RecordDecl *RDecl = cast<RecordType>(Val&: CT)->getDecl(); |
9308 | S += RDecl->isUnion() ? '(' : '{'; |
9309 | // Anonymous structures print as '?' |
9310 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
9311 | S += II->getName(); |
9312 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(Val: RDecl)) { |
9313 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
9314 | llvm::raw_string_ostream OS(S); |
9315 | printTemplateArgumentList(OS, Args: TemplateArgs.asArray(), |
9316 | Policy: getPrintingPolicy()); |
9317 | } |
9318 | } else { |
9319 | S += '?'; |
9320 | } |
9321 | if (Options.ExpandStructures()) { |
9322 | S += '='; |
9323 | if (!RDecl->isUnion()) { |
9324 | getObjCEncodingForStructureImpl(RD: RDecl, S, Field: FD, includeVBases: true, NotEncodedT); |
9325 | } else { |
9326 | for (const auto *Field : RDecl->fields()) { |
9327 | if (FD) { |
9328 | S += '"'; |
9329 | S += Field->getNameAsString(); |
9330 | S += '"'; |
9331 | } |
9332 | |
9333 | // Special case bit-fields. |
9334 | if (Field->isBitField()) { |
9335 | getObjCEncodingForTypeImpl(T: Field->getType(), S, |
9336 | Options: ObjCEncOptions().setExpandStructures(), |
9337 | FD: Field); |
9338 | } else { |
9339 | QualType qt = Field->getType(); |
9340 | getLegacyIntegralTypeEncoding(PointeeTy&: qt); |
9341 | getObjCEncodingForTypeImpl( |
9342 | T: qt, S, |
9343 | Options: ObjCEncOptions().setExpandStructures().setIsStructField(), FD, |
9344 | NotEncodedT); |
9345 | } |
9346 | } |
9347 | } |
9348 | } |
9349 | S += RDecl->isUnion() ? ')' : '}'; |
9350 | return; |
9351 | } |
9352 | |
9353 | case Type::BlockPointer: { |
9354 | const auto *BT = T->castAs<BlockPointerType>(); |
9355 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
9356 | if (Options.EncodeBlockParameters()) { |
9357 | const auto *FT = BT->getPointeeType()->castAs<FunctionType>(); |
9358 | |
9359 | S += '<'; |
9360 | // Block return type |
9361 | getObjCEncodingForTypeImpl(T: FT->getReturnType(), S, |
9362 | Options: Options.forComponentType(), FD, NotEncodedT); |
9363 | // Block self |
9364 | S += "@?"; |
9365 | // Block parameters |
9366 | if (const auto *FPT = dyn_cast<FunctionProtoType>(Val: FT)) { |
9367 | for (const auto &I : FPT->param_types()) |
9368 | getObjCEncodingForTypeImpl(T: I, S, Options: Options.forComponentType(), FD, |
9369 | NotEncodedT); |
9370 | } |
9371 | S += '>'; |
9372 | } |
9373 | return; |
9374 | } |
9375 | |
9376 | case Type::ObjCObject: { |
9377 | // hack to match legacy encoding of *id and *Class |
9378 | QualType Ty = getObjCObjectPointerType(ObjectT: CT); |
9379 | if (Ty->isObjCIdType()) { |
9380 | S += "{objc_object=}"; |
9381 | return; |
9382 | } |
9383 | else if (Ty->isObjCClassType()) { |
9384 | S += "{objc_class=}"; |
9385 | return; |
9386 | } |
9387 | // TODO: Double check to make sure this intentionally falls through. |
9388 | [[fallthrough]]; |
9389 | } |
9390 | |
9391 | case Type::ObjCInterface: { |
9392 | // Ignore protocol qualifiers when mangling at this level. |
9393 | // @encode(class_name) |
9394 | ObjCInterfaceDecl *OI = T->castAs<ObjCObjectType>()->getInterface(); |
9395 | S += '{'; |
9396 | S += OI->getObjCRuntimeNameAsString(); |
9397 | if (Options.ExpandStructures()) { |
9398 | S += '='; |
9399 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
9400 | DeepCollectObjCIvars(OI, leafClass: true, Ivars); |
9401 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
9402 | const FieldDecl *Field = Ivars[i]; |
9403 | if (Field->isBitField()) |
9404 | getObjCEncodingForTypeImpl(T: Field->getType(), S, |
9405 | Options: ObjCEncOptions().setExpandStructures(), |
9406 | FD: Field); |
9407 | else |
9408 | getObjCEncodingForTypeImpl(T: Field->getType(), S, |
9409 | Options: ObjCEncOptions().setExpandStructures(), FD, |
9410 | NotEncodedT); |
9411 | } |
9412 | } |
9413 | S += '}'; |
9414 | return; |
9415 | } |
9416 | |
9417 | case Type::ObjCObjectPointer: { |
9418 | const auto *OPT = T->castAs<ObjCObjectPointerType>(); |
9419 | if (OPT->isObjCIdType()) { |
9420 | S += '@'; |
9421 | return; |
9422 | } |
9423 | |
9424 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
9425 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
9426 | // Since this is a binary compatibility issue, need to consult with |
9427 | // runtime folks. Fortunately, this is a *very* obscure construct. |
9428 | S += '#'; |
9429 | return; |
9430 | } |
9431 | |
9432 | if (OPT->isObjCQualifiedIdType()) { |
9433 | getObjCEncodingForTypeImpl( |
9434 | T: getObjCIdType(), S, |
9435 | Options: Options.keepingOnly(Mask: ObjCEncOptions() |
9436 | .setExpandPointedToStructures() |
9437 | .setExpandStructures()), |
9438 | FD); |
9439 | if (FD || Options.EncodingProperty() || Options.EncodeClassNames()) { |
9440 | // Note that we do extended encoding of protocol qualifier list |
9441 | // Only when doing ivar or property encoding. |
9442 | S += '"'; |
9443 | for (const auto *I : OPT->quals()) { |
9444 | S += '<'; |
9445 | S += I->getObjCRuntimeNameAsString(); |
9446 | S += '>'; |
9447 | } |
9448 | S += '"'; |
9449 | } |
9450 | return; |
9451 | } |
9452 | |
9453 | S += '@'; |
9454 | if (OPT->getInterfaceDecl() && |
9455 | (FD || Options.EncodingProperty() || Options.EncodeClassNames())) { |
9456 | S += '"'; |
9457 | S += OPT->getInterfaceDecl()->getObjCRuntimeNameAsString(); |
9458 | for (const auto *I : OPT->quals()) { |
9459 | S += '<'; |
9460 | S += I->getObjCRuntimeNameAsString(); |
9461 | S += '>'; |
9462 | } |
9463 | S += '"'; |
9464 | } |
9465 | return; |
9466 | } |
9467 | |
9468 | // gcc just blithely ignores member pointers. |
9469 | // FIXME: we should do better than that. 'M' is available. |
9470 | case Type::MemberPointer: |
9471 | // This matches gcc's encoding, even though technically it is insufficient. |
9472 | //FIXME. We should do a better job than gcc. |
9473 | case Type::Vector: |
9474 | case Type::ExtVector: |
9475 | // Until we have a coherent encoding of these three types, issue warning. |
9476 | if (NotEncodedT) |
9477 | *NotEncodedT = T; |
9478 | return; |
9479 | |
9480 | case Type::ConstantMatrix: |
9481 | if (NotEncodedT) |
9482 | *NotEncodedT = T; |
9483 | return; |
9484 | |
9485 | case Type::BitInt: |
9486 | if (NotEncodedT) |
9487 | *NotEncodedT = T; |
9488 | return; |
9489 | |
9490 | // We could see an undeduced auto type here during error recovery. |
9491 | // Just ignore it. |
9492 | case Type::Auto: |
9493 | case Type::DeducedTemplateSpecialization: |
9494 | return; |
9495 | |
9496 | case Type::HLSLAttributedResource: |
9497 | case Type::HLSLInlineSpirv: |
9498 | llvm_unreachable("unexpected type"); |
9499 | |
9500 | case Type::ArrayParameter: |
9501 | case Type::Pipe: |
9502 | #define ABSTRACT_TYPE(KIND, BASE) |
9503 | #define TYPE(KIND, BASE) |
9504 | #define DEPENDENT_TYPE(KIND, BASE) \ |
9505 | case Type::KIND: |
9506 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
9507 | case Type::KIND: |
9508 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
9509 | case Type::KIND: |
9510 | #include "clang/AST/TypeNodes.inc" |
9511 | llvm_unreachable("@encode for dependent type!"); |
9512 | } |
9513 | llvm_unreachable("bad type kind!"); |
9514 | } |
9515 | |
9516 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
9517 | std::string &S, |
9518 | const FieldDecl *FD, |
9519 | bool includeVBases, |
9520 | QualType *NotEncodedT) const { |
9521 | assert(RDecl && "Expected non-null RecordDecl"); |
9522 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
9523 | if (!RDecl->getDefinition() || RDecl->getDefinition()->isInvalidDecl()) |
9524 | return; |
9525 | |
9526 | const auto *CXXRec = dyn_cast<CXXRecordDecl>(Val: RDecl); |
9527 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
9528 | const ASTRecordLayout &layout = getASTRecordLayout(D: RDecl); |
9529 | |
9530 | if (CXXRec) { |
9531 | for (const auto &BI : CXXRec->bases()) { |
9532 | if (!BI.isVirtual()) { |
9533 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
9534 | if (base->isEmpty()) |
9535 | continue; |
9536 | uint64_t offs = toBits(CharSize: layout.getBaseClassOffset(Base: base)); |
9537 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(x: offs), |
9538 | std::make_pair(x&: offs, y&: base)); |
9539 | } |
9540 | } |
9541 | } |
9542 | |
9543 | for (FieldDecl *Field : RDecl->fields()) { |
9544 | if (!Field->isZeroLengthBitField() && Field->isZeroSize(Ctx: *this)) |
9545 | continue; |
9546 | uint64_t offs = layout.getFieldOffset(FieldNo: Field->getFieldIndex()); |
9547 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(x: offs), |
9548 | std::make_pair(x&: offs, y&: Field)); |
9549 | } |
9550 | |
9551 | if (CXXRec && includeVBases) { |
9552 | for (const auto &BI : CXXRec->vbases()) { |
9553 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
9554 | if (base->isEmpty()) |
9555 | continue; |
9556 | uint64_t offs = toBits(CharSize: layout.getVBaseClassOffset(VBase: base)); |
9557 | if (offs >= uint64_t(toBits(CharSize: layout.getNonVirtualSize())) && |
9558 | FieldOrBaseOffsets.find(x: offs) == FieldOrBaseOffsets.end()) |
9559 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
9560 | std::make_pair(x&: offs, y&: base)); |
9561 | } |
9562 | } |
9563 | |
9564 | CharUnits size; |
9565 | if (CXXRec) { |
9566 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
9567 | } else { |
9568 | size = layout.getSize(); |
9569 | } |
9570 | |
9571 | #ifndef NDEBUG |
9572 | uint64_t CurOffs = 0; |
9573 | #endif |
9574 | std::multimap<uint64_t, NamedDecl *>::iterator |
9575 | CurLayObj = FieldOrBaseOffsets.begin(); |
9576 | |
9577 | if (CXXRec && CXXRec->isDynamicClass() && |
9578 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
9579 | if (FD) { |
9580 | S += "\"_vptr$"; |
9581 | std::string recname = CXXRec->getNameAsString(); |
9582 | if (recname.empty()) recname = "?"; |
9583 | S += recname; |
9584 | S += '"'; |
9585 | } |
9586 | S += "^^?"; |
9587 | #ifndef NDEBUG |
9588 | CurOffs += getTypeSize(VoidPtrTy); |
9589 | #endif |
9590 | } |
9591 | |
9592 | if (!RDecl->hasFlexibleArrayMember()) { |
9593 | // Mark the end of the structure. |
9594 | uint64_t offs = toBits(CharSize: size); |
9595 | FieldOrBaseOffsets.insert(position: FieldOrBaseOffsets.upper_bound(x: offs), |
9596 | x: std::make_pair(x&: offs, y: nullptr)); |
9597 | } |
9598 | |
9599 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
9600 | #ifndef NDEBUG |
9601 | assert(CurOffs <= CurLayObj->first); |
9602 | if (CurOffs < CurLayObj->first) { |
9603 | uint64_t padding = CurLayObj->first - CurOffs; |
9604 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
9605 | // packing/alignment of members is different that normal, in which case |
9606 | // the encoding will be out-of-sync with the real layout. |
9607 | // If the runtime switches to just consider the size of types without |
9608 | // taking into account alignment, we could make padding explicit in the |
9609 | // encoding (e.g. using arrays of chars). The encoding strings would be |
9610 | // longer then though. |
9611 | CurOffs += padding; |
9612 | } |
9613 | #endif |
9614 | |
9615 | NamedDecl *dcl = CurLayObj->second; |
9616 | if (!dcl) |
9617 | break; // reached end of structure. |
9618 | |
9619 | if (auto *base = dyn_cast<CXXRecordDecl>(Val: dcl)) { |
9620 | // We expand the bases without their virtual bases since those are going |
9621 | // in the initial structure. Note that this differs from gcc which |
9622 | // expands virtual bases each time one is encountered in the hierarchy, |
9623 | // making the encoding type bigger than it really is. |
9624 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false, |
9625 | NotEncodedT); |
9626 | assert(!base->isEmpty()); |
9627 | #ifndef NDEBUG |
9628 | CurOffs += toBits(CharSize: getASTRecordLayout(base).getNonVirtualSize()); |
9629 | #endif |
9630 | } else { |
9631 | const auto *field = cast<FieldDecl>(Val: dcl); |
9632 | if (FD) { |
9633 | S += '"'; |
9634 | S += field->getNameAsString(); |
9635 | S += '"'; |
9636 | } |
9637 | |
9638 | if (field->isBitField()) { |
9639 | EncodeBitField(this, S, field->getType(), field); |
9640 | #ifndef NDEBUG |
9641 | CurOffs += field->getBitWidthValue(); |
9642 | #endif |
9643 | } else { |
9644 | QualType qt = field->getType(); |
9645 | getLegacyIntegralTypeEncoding(PointeeTy&: qt); |
9646 | getObjCEncodingForTypeImpl( |
9647 | T: qt, S, Options: ObjCEncOptions().setExpandStructures().setIsStructField(), |
9648 | FD, NotEncodedT); |
9649 | #ifndef NDEBUG |
9650 | CurOffs += getTypeSize(field->getType()); |
9651 | #endif |
9652 | } |
9653 | } |
9654 | } |
9655 | } |
9656 | |
9657 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
9658 | std::string& S) const { |
9659 | if (QT & Decl::OBJC_TQ_In) |
9660 | S += 'n'; |
9661 | if (QT & Decl::OBJC_TQ_Inout) |
9662 | S += 'N'; |
9663 | if (QT & Decl::OBJC_TQ_Out) |
9664 | S += 'o'; |
9665 | if (QT & Decl::OBJC_TQ_Bycopy) |
9666 | S += 'O'; |
9667 | if (QT & Decl::OBJC_TQ_Byref) |
9668 | S += 'R'; |
9669 | if (QT & Decl::OBJC_TQ_Oneway) |
9670 | S += 'V'; |
9671 | } |
9672 | |
9673 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
9674 | if (!ObjCIdDecl) { |
9675 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, {}, {}); |
9676 | T = getObjCObjectPointerType(ObjectT: T); |
9677 | ObjCIdDecl = buildImplicitTypedef(T, Name: "id"); |
9678 | } |
9679 | return ObjCIdDecl; |
9680 | } |
9681 | |
9682 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
9683 | if (!ObjCSelDecl) { |
9684 | QualType T = getPointerType(ObjCBuiltinSelTy); |
9685 | ObjCSelDecl = buildImplicitTypedef(T, Name: "SEL"); |
9686 | } |
9687 | return ObjCSelDecl; |
9688 | } |
9689 | |
9690 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
9691 | if (!ObjCClassDecl) { |
9692 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, {}, {}); |
9693 | T = getObjCObjectPointerType(ObjectT: T); |
9694 | ObjCClassDecl = buildImplicitTypedef(T, Name: "Class"); |
9695 | } |
9696 | return ObjCClassDecl; |
9697 | } |
9698 | |
9699 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
9700 | if (!ObjCProtocolClassDecl) { |
9701 | ObjCProtocolClassDecl |
9702 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
9703 | SourceLocation(), |
9704 | &Idents.get(Name: "Protocol"), |
9705 | /*typeParamList=*/nullptr, |
9706 | /*PrevDecl=*/nullptr, |
9707 | SourceLocation(), true); |
9708 | } |
9709 | |
9710 | return ObjCProtocolClassDecl; |
9711 | } |
9712 | |
9713 | //===----------------------------------------------------------------------===// |
9714 | // __builtin_va_list Construction Functions |
9715 | //===----------------------------------------------------------------------===// |
9716 | |
9717 | static TypedefDecl *CreateCharPtrNamedVaListDecl(const ASTContext *Context, |
9718 | StringRef Name) { |
9719 | // typedef char* __builtin[_ms]_va_list; |
9720 | QualType T = Context->getPointerType(Context->CharTy); |
9721 | return Context->buildImplicitTypedef(T, Name); |
9722 | } |
9723 | |
9724 | static TypedefDecl *CreateMSVaListDecl(const ASTContext *Context) { |
9725 | return CreateCharPtrNamedVaListDecl(Context, Name: "__builtin_ms_va_list"); |
9726 | } |
9727 | |
9728 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
9729 | return CreateCharPtrNamedVaListDecl(Context, Name: "__builtin_va_list"); |
9730 | } |
9731 | |
9732 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
9733 | // typedef void* __builtin_va_list; |
9734 | QualType T = Context->getPointerType(Context->VoidTy); |
9735 | return Context->buildImplicitTypedef(T, Name: "__builtin_va_list"); |
9736 | } |
9737 | |
9738 | static TypedefDecl * |
9739 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
9740 | // struct __va_list |
9741 | RecordDecl *VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list"); |
9742 | if (Context->getLangOpts().CPlusPlus) { |
9743 | // namespace std { struct __va_list { |
9744 | auto *NS = NamespaceDecl::Create( |
9745 | const_cast<ASTContext &>(*Context), Context->getTranslationUnitDecl(), |
9746 | /*Inline=*/false, SourceLocation(), SourceLocation(), |
9747 | &Context->Idents.get(Name: "std"), |
9748 | /*PrevDecl=*/nullptr, /*Nested=*/false); |
9749 | NS->setImplicit(); |
9750 | VaListTagDecl->setDeclContext(NS); |
9751 | } |
9752 | |
9753 | VaListTagDecl->startDefinition(); |
9754 | |
9755 | const size_t NumFields = 5; |
9756 | QualType FieldTypes[NumFields]; |
9757 | const char *FieldNames[NumFields]; |
9758 | |
9759 | // void *__stack; |
9760 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
9761 | FieldNames[0] = "__stack"; |
9762 | |
9763 | // void *__gr_top; |
9764 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
9765 | FieldNames[1] = "__gr_top"; |
9766 | |
9767 | // void *__vr_top; |
9768 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
9769 | FieldNames[2] = "__vr_top"; |
9770 | |
9771 | // int __gr_offs; |
9772 | FieldTypes[3] = Context->IntTy; |
9773 | FieldNames[3] = "__gr_offs"; |
9774 | |
9775 | // int __vr_offs; |
9776 | FieldTypes[4] = Context->IntTy; |
9777 | FieldNames[4] = "__vr_offs"; |
9778 | |
9779 | // Create fields |
9780 | for (unsigned i = 0; i < NumFields; ++i) { |
9781 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
9782 | VaListTagDecl, |
9783 | SourceLocation(), |
9784 | SourceLocation(), |
9785 | &Context->Idents.get(Name: FieldNames[i]), |
9786 | FieldTypes[i], /*TInfo=*/nullptr, |
9787 | /*BitWidth=*/nullptr, |
9788 | /*Mutable=*/false, |
9789 | ICIS_NoInit); |
9790 | Field->setAccess(AS_public); |
9791 | VaListTagDecl->addDecl(Field); |
9792 | } |
9793 | VaListTagDecl->completeDefinition(); |
9794 | Context->VaListTagDecl = VaListTagDecl; |
9795 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
9796 | |
9797 | // } __builtin_va_list; |
9798 | return Context->buildImplicitTypedef(T: VaListTagType, Name: "__builtin_va_list"); |
9799 | } |
9800 | |
9801 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
9802 | // typedef struct __va_list_tag { |
9803 | RecordDecl *VaListTagDecl; |
9804 | |
9805 | VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list_tag"); |
9806 | VaListTagDecl->startDefinition(); |
9807 | |
9808 | const size_t NumFields = 5; |
9809 | QualType FieldTypes[NumFields]; |
9810 | const char *FieldNames[NumFields]; |
9811 | |
9812 | // unsigned char gpr; |
9813 | FieldTypes[0] = Context->UnsignedCharTy; |
9814 | FieldNames[0] = "gpr"; |
9815 | |
9816 | // unsigned char fpr; |
9817 | FieldTypes[1] = Context->UnsignedCharTy; |
9818 | FieldNames[1] = "fpr"; |
9819 | |
9820 | // unsigned short reserved; |
9821 | FieldTypes[2] = Context->UnsignedShortTy; |
9822 | FieldNames[2] = "reserved"; |
9823 | |
9824 | // void* overflow_arg_area; |
9825 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
9826 | FieldNames[3] = "overflow_arg_area"; |
9827 | |
9828 | // void* reg_save_area; |
9829 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
9830 | FieldNames[4] = "reg_save_area"; |
9831 | |
9832 | // Create fields |
9833 | for (unsigned i = 0; i < NumFields; ++i) { |
9834 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
9835 | SourceLocation(), |
9836 | SourceLocation(), |
9837 | &Context->Idents.get(Name: FieldNames[i]), |
9838 | FieldTypes[i], /*TInfo=*/nullptr, |
9839 | /*BitWidth=*/nullptr, |
9840 | /*Mutable=*/false, |
9841 | ICIS_NoInit); |
9842 | Field->setAccess(AS_public); |
9843 | VaListTagDecl->addDecl(Field); |
9844 | } |
9845 | VaListTagDecl->completeDefinition(); |
9846 | Context->VaListTagDecl = VaListTagDecl; |
9847 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
9848 | |
9849 | // } __va_list_tag; |
9850 | TypedefDecl *VaListTagTypedefDecl = |
9851 | Context->buildImplicitTypedef(T: VaListTagType, Name: "__va_list_tag"); |
9852 | |
9853 | QualType VaListTagTypedefType = |
9854 | Context->getTypedefType(VaListTagTypedefDecl); |
9855 | |
9856 | // typedef __va_list_tag __builtin_va_list[1]; |
9857 | llvm::APInt Size(Context->getTypeSize(T: Context->getSizeType()), 1); |
9858 | QualType VaListTagArrayType = Context->getConstantArrayType( |
9859 | EltTy: VaListTagTypedefType, ArySizeIn: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9860 | return Context->buildImplicitTypedef(T: VaListTagArrayType, Name: "__builtin_va_list"); |
9861 | } |
9862 | |
9863 | static TypedefDecl * |
9864 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
9865 | // struct __va_list_tag { |
9866 | RecordDecl *VaListTagDecl; |
9867 | VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list_tag"); |
9868 | VaListTagDecl->startDefinition(); |
9869 | |
9870 | const size_t NumFields = 4; |
9871 | QualType FieldTypes[NumFields]; |
9872 | const char *FieldNames[NumFields]; |
9873 | |
9874 | // unsigned gp_offset; |
9875 | FieldTypes[0] = Context->UnsignedIntTy; |
9876 | FieldNames[0] = "gp_offset"; |
9877 | |
9878 | // unsigned fp_offset; |
9879 | FieldTypes[1] = Context->UnsignedIntTy; |
9880 | FieldNames[1] = "fp_offset"; |
9881 | |
9882 | // void* overflow_arg_area; |
9883 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
9884 | FieldNames[2] = "overflow_arg_area"; |
9885 | |
9886 | // void* reg_save_area; |
9887 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
9888 | FieldNames[3] = "reg_save_area"; |
9889 | |
9890 | // Create fields |
9891 | for (unsigned i = 0; i < NumFields; ++i) { |
9892 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
9893 | VaListTagDecl, |
9894 | SourceLocation(), |
9895 | SourceLocation(), |
9896 | &Context->Idents.get(Name: FieldNames[i]), |
9897 | FieldTypes[i], /*TInfo=*/nullptr, |
9898 | /*BitWidth=*/nullptr, |
9899 | /*Mutable=*/false, |
9900 | ICIS_NoInit); |
9901 | Field->setAccess(AS_public); |
9902 | VaListTagDecl->addDecl(Field); |
9903 | } |
9904 | VaListTagDecl->completeDefinition(); |
9905 | Context->VaListTagDecl = VaListTagDecl; |
9906 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
9907 | |
9908 | // }; |
9909 | |
9910 | // typedef struct __va_list_tag __builtin_va_list[1]; |
9911 | llvm::APInt Size(Context->getTypeSize(T: Context->getSizeType()), 1); |
9912 | QualType VaListTagArrayType = Context->getConstantArrayType( |
9913 | EltTy: VaListTagType, ArySizeIn: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9914 | return Context->buildImplicitTypedef(T: VaListTagArrayType, Name: "__builtin_va_list"); |
9915 | } |
9916 | |
9917 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
9918 | // typedef int __builtin_va_list[4]; |
9919 | llvm::APInt Size(Context->getTypeSize(T: Context->getSizeType()), 4); |
9920 | QualType IntArrayType = Context->getConstantArrayType( |
9921 | EltTy: Context->IntTy, ArySizeIn: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9922 | return Context->buildImplicitTypedef(T: IntArrayType, Name: "__builtin_va_list"); |
9923 | } |
9924 | |
9925 | static TypedefDecl * |
9926 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
9927 | // struct __va_list |
9928 | RecordDecl *VaListDecl = Context->buildImplicitRecord(Name: "__va_list"); |
9929 | if (Context->getLangOpts().CPlusPlus) { |
9930 | // namespace std { struct __va_list { |
9931 | NamespaceDecl *NS; |
9932 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
9933 | Context->getTranslationUnitDecl(), |
9934 | /*Inline=*/false, SourceLocation(), |
9935 | SourceLocation(), &Context->Idents.get(Name: "std"), |
9936 | /*PrevDecl=*/nullptr, /*Nested=*/false); |
9937 | NS->setImplicit(); |
9938 | VaListDecl->setDeclContext(NS); |
9939 | } |
9940 | |
9941 | VaListDecl->startDefinition(); |
9942 | |
9943 | // void * __ap; |
9944 | FieldDecl *Field = FieldDecl::Create(C: const_cast<ASTContext &>(*Context), |
9945 | DC: VaListDecl, |
9946 | StartLoc: SourceLocation(), |
9947 | IdLoc: SourceLocation(), |
9948 | Id: &Context->Idents.get(Name: "__ap"), |
9949 | T: Context->getPointerType(Context->VoidTy), |
9950 | /*TInfo=*/nullptr, |
9951 | /*BitWidth=*/BW: nullptr, |
9952 | /*Mutable=*/false, |
9953 | InitStyle: ICIS_NoInit); |
9954 | Field->setAccess(AS_public); |
9955 | VaListDecl->addDecl(Field); |
9956 | |
9957 | // }; |
9958 | VaListDecl->completeDefinition(); |
9959 | Context->VaListTagDecl = VaListDecl; |
9960 | |
9961 | // typedef struct __va_list __builtin_va_list; |
9962 | QualType T = Context->getRecordType(Decl: VaListDecl); |
9963 | return Context->buildImplicitTypedef(T, Name: "__builtin_va_list"); |
9964 | } |
9965 | |
9966 | static TypedefDecl * |
9967 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { |
9968 | // struct __va_list_tag { |
9969 | RecordDecl *VaListTagDecl; |
9970 | VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list_tag"); |
9971 | VaListTagDecl->startDefinition(); |
9972 | |
9973 | const size_t NumFields = 4; |
9974 | QualType FieldTypes[NumFields]; |
9975 | const char *FieldNames[NumFields]; |
9976 | |
9977 | // long __gpr; |
9978 | FieldTypes[0] = Context->LongTy; |
9979 | FieldNames[0] = "__gpr"; |
9980 | |
9981 | // long __fpr; |
9982 | FieldTypes[1] = Context->LongTy; |
9983 | FieldNames[1] = "__fpr"; |
9984 | |
9985 | // void *__overflow_arg_area; |
9986 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
9987 | FieldNames[2] = "__overflow_arg_area"; |
9988 | |
9989 | // void *__reg_save_area; |
9990 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
9991 | FieldNames[3] = "__reg_save_area"; |
9992 | |
9993 | // Create fields |
9994 | for (unsigned i = 0; i < NumFields; ++i) { |
9995 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
9996 | VaListTagDecl, |
9997 | SourceLocation(), |
9998 | SourceLocation(), |
9999 | &Context->Idents.get(Name: FieldNames[i]), |
10000 | FieldTypes[i], /*TInfo=*/nullptr, |
10001 | /*BitWidth=*/nullptr, |
10002 | /*Mutable=*/false, |
10003 | ICIS_NoInit); |
10004 | Field->setAccess(AS_public); |
10005 | VaListTagDecl->addDecl(Field); |
10006 | } |
10007 | VaListTagDecl->completeDefinition(); |
10008 | Context->VaListTagDecl = VaListTagDecl; |
10009 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
10010 | |
10011 | // }; |
10012 | |
10013 | // typedef __va_list_tag __builtin_va_list[1]; |
10014 | llvm::APInt Size(Context->getTypeSize(T: Context->getSizeType()), 1); |
10015 | QualType VaListTagArrayType = Context->getConstantArrayType( |
10016 | EltTy: VaListTagType, ArySizeIn: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
10017 | |
10018 | return Context->buildImplicitTypedef(T: VaListTagArrayType, Name: "__builtin_va_list"); |
10019 | } |
10020 | |
10021 | static TypedefDecl *CreateHexagonBuiltinVaListDecl(const ASTContext *Context) { |
10022 | // typedef struct __va_list_tag { |
10023 | RecordDecl *VaListTagDecl; |
10024 | VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list_tag"); |
10025 | VaListTagDecl->startDefinition(); |
10026 | |
10027 | const size_t NumFields = 3; |
10028 | QualType FieldTypes[NumFields]; |
10029 | const char *FieldNames[NumFields]; |
10030 | |
10031 | // void *CurrentSavedRegisterArea; |
10032 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
10033 | FieldNames[0] = "__current_saved_reg_area_pointer"; |
10034 | |
10035 | // void *SavedRegAreaEnd; |
10036 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
10037 | FieldNames[1] = "__saved_reg_area_end_pointer"; |
10038 | |
10039 | // void *OverflowArea; |
10040 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
10041 | FieldNames[2] = "__overflow_area_pointer"; |
10042 | |
10043 | // Create fields |
10044 | for (unsigned i = 0; i < NumFields; ++i) { |
10045 | FieldDecl *Field = FieldDecl::Create( |
10046 | const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(), |
10047 | SourceLocation(), &Context->Idents.get(Name: FieldNames[i]), FieldTypes[i], |
10048 | /*TInfo=*/nullptr, |
10049 | /*BitWidth=*/nullptr, |
10050 | /*Mutable=*/false, ICIS_NoInit); |
10051 | Field->setAccess(AS_public); |
10052 | VaListTagDecl->addDecl(Field); |
10053 | } |
10054 | VaListTagDecl->completeDefinition(); |
10055 | Context->VaListTagDecl = VaListTagDecl; |
10056 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
10057 | |
10058 | // } __va_list_tag; |
10059 | TypedefDecl *VaListTagTypedefDecl = |
10060 | Context->buildImplicitTypedef(T: VaListTagType, Name: "__va_list_tag"); |
10061 | |
10062 | QualType VaListTagTypedefType = Context->getTypedefType(VaListTagTypedefDecl); |
10063 | |
10064 | // typedef __va_list_tag __builtin_va_list[1]; |
10065 | llvm::APInt Size(Context->getTypeSize(T: Context->getSizeType()), 1); |
10066 | QualType VaListTagArrayType = Context->getConstantArrayType( |
10067 | EltTy: VaListTagTypedefType, ArySizeIn: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
10068 | |
10069 | return Context->buildImplicitTypedef(T: VaListTagArrayType, Name: "__builtin_va_list"); |
10070 | } |
10071 | |
10072 | static TypedefDecl * |
10073 | CreateXtensaABIBuiltinVaListDecl(const ASTContext *Context) { |
10074 | // typedef struct __va_list_tag { |
10075 | RecordDecl *VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list_tag"); |
10076 | |
10077 | VaListTagDecl->startDefinition(); |
10078 | |
10079 | // int* __va_stk; |
10080 | // int* __va_reg; |
10081 | // int __va_ndx; |
10082 | constexpr size_t NumFields = 3; |
10083 | QualType FieldTypes[NumFields] = {Context->getPointerType(Context->IntTy), |
10084 | Context->getPointerType(Context->IntTy), |
10085 | Context->IntTy}; |
10086 | const char *FieldNames[NumFields] = {"__va_stk", "__va_reg", "__va_ndx"}; |
10087 | |
10088 | // Create fields |
10089 | for (unsigned i = 0; i < NumFields; ++i) { |
10090 | FieldDecl *Field = FieldDecl::Create( |
10091 | *Context, VaListTagDecl, SourceLocation(), SourceLocation(), |
10092 | &Context->Idents.get(Name: FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
10093 | /*BitWidth=*/nullptr, |
10094 | /*Mutable=*/false, ICIS_NoInit); |
10095 | Field->setAccess(AS_public); |
10096 | VaListTagDecl->addDecl(Field); |
10097 | } |
10098 | VaListTagDecl->completeDefinition(); |
10099 | Context->VaListTagDecl = VaListTagDecl; |
10100 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
10101 | |
10102 | // } __va_list_tag; |
10103 | TypedefDecl *VaListTagTypedefDecl = |
10104 | Context->buildImplicitTypedef(T: VaListTagType, Name: "__builtin_va_list"); |
10105 | |
10106 | return VaListTagTypedefDecl; |
10107 | } |
10108 | |
10109 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
10110 | TargetInfo::BuiltinVaListKind Kind) { |
10111 | switch (Kind) { |
10112 | case TargetInfo::CharPtrBuiltinVaList: |
10113 | return CreateCharPtrBuiltinVaListDecl(Context); |
10114 | case TargetInfo::VoidPtrBuiltinVaList: |
10115 | return CreateVoidPtrBuiltinVaListDecl(Context); |
10116 | case TargetInfo::AArch64ABIBuiltinVaList: |
10117 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
10118 | case TargetInfo::PowerABIBuiltinVaList: |
10119 | return CreatePowerABIBuiltinVaListDecl(Context); |
10120 | case TargetInfo::X86_64ABIBuiltinVaList: |
10121 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
10122 | case TargetInfo::PNaClABIBuiltinVaList: |
10123 | return CreatePNaClABIBuiltinVaListDecl(Context); |
10124 | case TargetInfo::AAPCSABIBuiltinVaList: |
10125 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
10126 | case TargetInfo::SystemZBuiltinVaList: |
10127 | return CreateSystemZBuiltinVaListDecl(Context); |
10128 | case TargetInfo::HexagonBuiltinVaList: |
10129 | return CreateHexagonBuiltinVaListDecl(Context); |
10130 | case TargetInfo::XtensaABIBuiltinVaList: |
10131 | return CreateXtensaABIBuiltinVaListDecl(Context); |
10132 | } |
10133 | |
10134 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
10135 | } |
10136 | |
10137 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
10138 | if (!BuiltinVaListDecl) { |
10139 | BuiltinVaListDecl = CreateVaListDecl(Context: this, Kind: Target->getBuiltinVaListKind()); |
10140 | assert(BuiltinVaListDecl->isImplicit()); |
10141 | } |
10142 | |
10143 | return BuiltinVaListDecl; |
10144 | } |
10145 | |
10146 | Decl *ASTContext::getVaListTagDecl() const { |
10147 | // Force the creation of VaListTagDecl by building the __builtin_va_list |
10148 | // declaration. |
10149 | if (!VaListTagDecl) |
10150 | (void)getBuiltinVaListDecl(); |
10151 | |
10152 | return VaListTagDecl; |
10153 | } |
10154 | |
10155 | TypedefDecl *ASTContext::getBuiltinMSVaListDecl() const { |
10156 | if (!BuiltinMSVaListDecl) |
10157 | BuiltinMSVaListDecl = CreateMSVaListDecl(Context: this); |
10158 | |
10159 | return BuiltinMSVaListDecl; |
10160 | } |
10161 | |
10162 | bool ASTContext::canBuiltinBeRedeclared(const FunctionDecl *FD) const { |
10163 | // Allow redecl custom type checking builtin for HLSL. |
10164 | if (LangOpts.HLSL && FD->getBuiltinID() != Builtin::NotBuiltin && |
10165 | BuiltinInfo.hasCustomTypechecking(ID: FD->getBuiltinID())) |
10166 | return true; |
10167 | // Allow redecl custom type checking builtin for SPIR-V. |
10168 | if (getTargetInfo().getTriple().isSPIROrSPIRV() && |
10169 | BuiltinInfo.isTSBuiltin(ID: FD->getBuiltinID()) && |
10170 | BuiltinInfo.hasCustomTypechecking(ID: FD->getBuiltinID())) |
10171 | return true; |
10172 | return BuiltinInfo.canBeRedeclared(ID: FD->getBuiltinID()); |
10173 | } |
10174 | |
10175 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
10176 | assert(ObjCConstantStringType.isNull() && |
10177 | "'NSConstantString' type already set!"); |
10178 | |
10179 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
10180 | } |
10181 | |
10182 | /// Retrieve the template name that corresponds to a non-empty |
10183 | /// lookup. |
10184 | TemplateName |
10185 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
10186 | UnresolvedSetIterator End) const { |
10187 | unsigned size = End - Begin; |
10188 | assert(size > 1 && "set is not overloaded!"); |
10189 | |
10190 | void *memory = Allocate(Size: sizeof(OverloadedTemplateStorage) + |
10191 | size * sizeof(FunctionTemplateDecl*)); |
10192 | auto *OT = new (memory) OverloadedTemplateStorage(size); |
10193 | |
10194 | NamedDecl **Storage = OT->getStorage(); |
10195 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
10196 | NamedDecl *D = *I; |
10197 | assert(isa<FunctionTemplateDecl>(D) || |
10198 | isa<UnresolvedUsingValueDecl>(D) || |
10199 | (isa<UsingShadowDecl>(D) && |
10200 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
10201 | *Storage++ = D; |
10202 | } |
10203 | |
10204 | return TemplateName(OT); |
10205 | } |
10206 | |
10207 | /// Retrieve a template name representing an unqualified-id that has been |
10208 | /// assumed to name a template for ADL purposes. |
10209 | TemplateName ASTContext::getAssumedTemplateName(DeclarationName Name) const { |
10210 | auto *OT = new (*this) AssumedTemplateStorage(Name); |
10211 | return TemplateName(OT); |
10212 | } |
10213 | |
10214 | /// Retrieve the template name that represents a qualified |
10215 | /// template name such as \c std::vector. |
10216 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
10217 | bool TemplateKeyword, |
10218 | TemplateName Template) const { |
10219 | assert(Template.getKind() == TemplateName::Template || |
10220 | Template.getKind() == TemplateName::UsingTemplate); |
10221 | |
10222 | // FIXME: Canonicalization? |
10223 | llvm::FoldingSetNodeID ID; |
10224 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, TN: Template); |
10225 | |
10226 | void *InsertPos = nullptr; |
10227 | QualifiedTemplateName *QTN = |
10228 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
10229 | if (!QTN) { |
10230 | QTN = new (*this, alignof(QualifiedTemplateName)) |
10231 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
10232 | QualifiedTemplateNames.InsertNode(N: QTN, InsertPos); |
10233 | } |
10234 | |
10235 | return TemplateName(QTN); |
10236 | } |
10237 | |
10238 | /// Retrieve the template name that represents a dependent |
10239 | /// template name such as \c MetaFun::template operator+. |
10240 | TemplateName |
10241 | ASTContext::getDependentTemplateName(const DependentTemplateStorage &S) const { |
10242 | llvm::FoldingSetNodeID ID; |
10243 | S.Profile(ID); |
10244 | |
10245 | void *InsertPos = nullptr; |
10246 | if (DependentTemplateName *QTN = |
10247 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos)) |
10248 | return TemplateName(QTN); |
10249 | |
10250 | DependentTemplateName *QTN = |
10251 | new (*this, alignof(DependentTemplateName)) DependentTemplateName(S); |
10252 | DependentTemplateNames.InsertNode(N: QTN, InsertPos); |
10253 | return TemplateName(QTN); |
10254 | } |
10255 | |
10256 | TemplateName ASTContext::getSubstTemplateTemplateParm(TemplateName Replacement, |
10257 | Decl *AssociatedDecl, |
10258 | unsigned Index, |
10259 | UnsignedOrNone PackIndex, |
10260 | bool Final) const { |
10261 | llvm::FoldingSetNodeID ID; |
10262 | SubstTemplateTemplateParmStorage::Profile(ID, Replacement, AssociatedDecl, |
10263 | Index, PackIndex, Final); |
10264 | |
10265 | void *insertPos = nullptr; |
10266 | SubstTemplateTemplateParmStorage *subst |
10267 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos&: insertPos); |
10268 | |
10269 | if (!subst) { |
10270 | subst = new (*this) SubstTemplateTemplateParmStorage( |
10271 | Replacement, AssociatedDecl, Index, PackIndex, Final); |
10272 | SubstTemplateTemplateParms.InsertNode(N: subst, InsertPos: insertPos); |
10273 | } |
10274 | |
10275 | return TemplateName(subst); |
10276 | } |
10277 | |
10278 | TemplateName |
10279 | ASTContext::getSubstTemplateTemplateParmPack(const TemplateArgument &ArgPack, |
10280 | Decl *AssociatedDecl, |
10281 | unsigned Index, bool Final) const { |
10282 | auto &Self = const_cast<ASTContext &>(*this); |
10283 | llvm::FoldingSetNodeID ID; |
10284 | SubstTemplateTemplateParmPackStorage::Profile(ID, Context&: Self, ArgPack, |
10285 | AssociatedDecl, Index, Final); |
10286 | |
10287 | void *InsertPos = nullptr; |
10288 | SubstTemplateTemplateParmPackStorage *Subst |
10289 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
10290 | |
10291 | if (!Subst) { |
10292 | Subst = new (*this) SubstTemplateTemplateParmPackStorage( |
10293 | ArgPack.pack_elements(), AssociatedDecl, Index, Final); |
10294 | SubstTemplateTemplateParmPacks.InsertNode(N: Subst, InsertPos); |
10295 | } |
10296 | |
10297 | return TemplateName(Subst); |
10298 | } |
10299 | |
10300 | /// Retrieve the template name that represents a template name |
10301 | /// deduced from a specialization. |
10302 | TemplateName |
10303 | ASTContext::getDeducedTemplateName(TemplateName Underlying, |
10304 | DefaultArguments DefaultArgs) const { |
10305 | if (!DefaultArgs) |
10306 | return Underlying; |
10307 | |
10308 | llvm::FoldingSetNodeID ID; |
10309 | DeducedTemplateStorage::Profile(ID, Context: *this, Underlying, DefArgs: DefaultArgs); |
10310 | |
10311 | void *InsertPos = nullptr; |
10312 | DeducedTemplateStorage *DTS = |
10313 | DeducedTemplates.FindNodeOrInsertPos(ID, InsertPos); |
10314 | if (!DTS) { |
10315 | void *Mem = Allocate(Size: sizeof(DeducedTemplateStorage) + |
10316 | sizeof(TemplateArgument) * DefaultArgs.Args.size(), |
10317 | Align: alignof(DeducedTemplateStorage)); |
10318 | DTS = new (Mem) DeducedTemplateStorage(Underlying, DefaultArgs); |
10319 | DeducedTemplates.InsertNode(N: DTS, InsertPos); |
10320 | } |
10321 | return TemplateName(DTS); |
10322 | } |
10323 | |
10324 | /// getFromTargetType - Given one of the integer types provided by |
10325 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
10326 | /// is actually a value of type @c TargetInfo::IntType. |
10327 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
10328 | switch (Type) { |
10329 | case TargetInfo::NoInt: return {}; |
10330 | case TargetInfo::SignedChar: return SignedCharTy; |
10331 | case TargetInfo::UnsignedChar: return UnsignedCharTy; |
10332 | case TargetInfo::SignedShort: return ShortTy; |
10333 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
10334 | case TargetInfo::SignedInt: return IntTy; |
10335 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
10336 | case TargetInfo::SignedLong: return LongTy; |
10337 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
10338 | case TargetInfo::SignedLongLong: return LongLongTy; |
10339 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
10340 | } |
10341 | |
10342 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
10343 | } |
10344 | |
10345 | //===----------------------------------------------------------------------===// |
10346 | // Type Predicates. |
10347 | //===----------------------------------------------------------------------===// |
10348 | |
10349 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
10350 | /// garbage collection attribute. |
10351 | /// |
10352 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
10353 | if (getLangOpts().getGC() == LangOptions::NonGC) |
10354 | return Qualifiers::GCNone; |
10355 | |
10356 | assert(getLangOpts().ObjC); |
10357 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
10358 | |
10359 | // Default behaviour under objective-C's gc is for ObjC pointers |
10360 | // (or pointers to them) be treated as though they were declared |
10361 | // as __strong. |
10362 | if (GCAttrs == Qualifiers::GCNone) { |
10363 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
10364 | return Qualifiers::Strong; |
10365 | else if (Ty->isPointerType()) |
10366 | return getObjCGCAttrKind(Ty: Ty->castAs<PointerType>()->getPointeeType()); |
10367 | } else { |
10368 | // It's not valid to set GC attributes on anything that isn't a |
10369 | // pointer. |
10370 | #ifndef NDEBUG |
10371 | QualType CT = Ty->getCanonicalTypeInternal(); |
10372 | while (const auto *AT = dyn_cast<ArrayType>(Val&: CT)) |
10373 | CT = AT->getElementType(); |
10374 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
10375 | #endif |
10376 | } |
10377 | return GCAttrs; |
10378 | } |
10379 | |
10380 | //===----------------------------------------------------------------------===// |
10381 | // Type Compatibility Testing |
10382 | //===----------------------------------------------------------------------===// |
10383 | |
10384 | /// areCompatVectorTypes - Return true if the two specified vector types are |
10385 | /// compatible. |
10386 | static bool areCompatVectorTypes(const VectorType *LHS, |
10387 | const VectorType *RHS) { |
10388 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
10389 | return LHS->getElementType() == RHS->getElementType() && |
10390 | LHS->getNumElements() == RHS->getNumElements(); |
10391 | } |
10392 | |
10393 | /// areCompatMatrixTypes - Return true if the two specified matrix types are |
10394 | /// compatible. |
10395 | static bool areCompatMatrixTypes(const ConstantMatrixType *LHS, |
10396 | const ConstantMatrixType *RHS) { |
10397 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
10398 | return LHS->getElementType() == RHS->getElementType() && |
10399 | LHS->getNumRows() == RHS->getNumRows() && |
10400 | LHS->getNumColumns() == RHS->getNumColumns(); |
10401 | } |
10402 | |
10403 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
10404 | QualType SecondVec) { |
10405 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
10406 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
10407 | |
10408 | if (hasSameUnqualifiedType(T1: FirstVec, T2: SecondVec)) |
10409 | return true; |
10410 | |
10411 | // Treat Neon vector types and most AltiVec vector types as if they are the |
10412 | // equivalent GCC vector types. |
10413 | const auto *First = FirstVec->castAs<VectorType>(); |
10414 | const auto *Second = SecondVec->castAs<VectorType>(); |
10415 | if (First->getNumElements() == Second->getNumElements() && |
10416 | hasSameType(T1: First->getElementType(), T2: Second->getElementType()) && |
10417 | First->getVectorKind() != VectorKind::AltiVecPixel && |
10418 | First->getVectorKind() != VectorKind::AltiVecBool && |
10419 | Second->getVectorKind() != VectorKind::AltiVecPixel && |
10420 | Second->getVectorKind() != VectorKind::AltiVecBool && |
10421 | First->getVectorKind() != VectorKind::SveFixedLengthData && |
10422 | First->getVectorKind() != VectorKind::SveFixedLengthPredicate && |
10423 | Second->getVectorKind() != VectorKind::SveFixedLengthData && |
10424 | Second->getVectorKind() != VectorKind::SveFixedLengthPredicate && |
10425 | First->getVectorKind() != VectorKind::RVVFixedLengthData && |
10426 | Second->getVectorKind() != VectorKind::RVVFixedLengthData && |
10427 | First->getVectorKind() != VectorKind::RVVFixedLengthMask && |
10428 | Second->getVectorKind() != VectorKind::RVVFixedLengthMask && |
10429 | First->getVectorKind() != VectorKind::RVVFixedLengthMask_1 && |
10430 | Second->getVectorKind() != VectorKind::RVVFixedLengthMask_1 && |
10431 | First->getVectorKind() != VectorKind::RVVFixedLengthMask_2 && |
10432 | Second->getVectorKind() != VectorKind::RVVFixedLengthMask_2 && |
10433 | First->getVectorKind() != VectorKind::RVVFixedLengthMask_4 && |
10434 | Second->getVectorKind() != VectorKind::RVVFixedLengthMask_4) |
10435 | return true; |
10436 | |
10437 | return false; |
10438 | } |
10439 | |
10440 | /// getSVETypeSize - Return SVE vector or predicate register size. |
10441 | static uint64_t getSVETypeSize(ASTContext &Context, const BuiltinType *Ty) { |
10442 | assert(Ty->isSveVLSBuiltinType() && "Invalid SVE Type"); |
10443 | if (Ty->getKind() == BuiltinType::SveBool || |
10444 | Ty->getKind() == BuiltinType::SveCount) |
10445 | return (Context.getLangOpts().VScaleMin * 128) / Context.getCharWidth(); |
10446 | return Context.getLangOpts().VScaleMin * 128; |
10447 | } |
10448 | |
10449 | bool ASTContext::areCompatibleSveTypes(QualType FirstType, |
10450 | QualType SecondType) { |
10451 | auto IsValidCast = [this](QualType FirstType, QualType SecondType) { |
10452 | if (const auto *BT = FirstType->getAs<BuiltinType>()) { |
10453 | if (const auto *VT = SecondType->getAs<VectorType>()) { |
10454 | // Predicates have the same representation as uint8 so we also have to |
10455 | // check the kind to make these types incompatible. |
10456 | if (VT->getVectorKind() == VectorKind::SveFixedLengthPredicate) |
10457 | return BT->getKind() == BuiltinType::SveBool; |
10458 | else if (VT->getVectorKind() == VectorKind::SveFixedLengthData) |
10459 | return VT->getElementType().getCanonicalType() == |
10460 | FirstType->getSveEltType(Ctx: *this); |
10461 | else if (VT->getVectorKind() == VectorKind::Generic) |
10462 | return getTypeSize(SecondType) == getSVETypeSize(*this, BT) && |
10463 | hasSameType(VT->getElementType(), |
10464 | getBuiltinVectorTypeInfo(BT).ElementType); |
10465 | } |
10466 | } |
10467 | return false; |
10468 | }; |
10469 | |
10470 | return IsValidCast(FirstType, SecondType) || |
10471 | IsValidCast(SecondType, FirstType); |
10472 | } |
10473 | |
10474 | bool ASTContext::areLaxCompatibleSveTypes(QualType FirstType, |
10475 | QualType SecondType) { |
10476 | auto IsLaxCompatible = [this](QualType FirstType, QualType SecondType) { |
10477 | const auto *BT = FirstType->getAs<BuiltinType>(); |
10478 | if (!BT) |
10479 | return false; |
10480 | |
10481 | const auto *VecTy = SecondType->getAs<VectorType>(); |
10482 | if (VecTy && (VecTy->getVectorKind() == VectorKind::SveFixedLengthData || |
10483 | VecTy->getVectorKind() == VectorKind::Generic)) { |
10484 | const LangOptions::LaxVectorConversionKind LVCKind = |
10485 | getLangOpts().getLaxVectorConversions(); |
10486 | |
10487 | // Can not convert between sve predicates and sve vectors because of |
10488 | // different size. |
10489 | if (BT->getKind() == BuiltinType::SveBool && |
10490 | VecTy->getVectorKind() == VectorKind::SveFixedLengthData) |
10491 | return false; |
10492 | |
10493 | // If __ARM_FEATURE_SVE_BITS != N do not allow GNU vector lax conversion. |
10494 | // "Whenever __ARM_FEATURE_SVE_BITS==N, GNUT implicitly |
10495 | // converts to VLAT and VLAT implicitly converts to GNUT." |
10496 | // ACLE Spec Version 00bet6, 3.7.3.2. Behavior common to vectors and |
10497 | // predicates. |
10498 | if (VecTy->getVectorKind() == VectorKind::Generic && |
10499 | getTypeSize(T: SecondType) != getSVETypeSize(Context&: *this, Ty: BT)) |
10500 | return false; |
10501 | |
10502 | // If -flax-vector-conversions=all is specified, the types are |
10503 | // certainly compatible. |
10504 | if (LVCKind == LangOptions::LaxVectorConversionKind::All) |
10505 | return true; |
10506 | |
10507 | // If -flax-vector-conversions=integer is specified, the types are |
10508 | // compatible if the elements are integer types. |
10509 | if (LVCKind == LangOptions::LaxVectorConversionKind::Integer) |
10510 | return VecTy->getElementType().getCanonicalType()->isIntegerType() && |
10511 | FirstType->getSveEltType(Ctx: *this)->isIntegerType(); |
10512 | } |
10513 | |
10514 | return false; |
10515 | }; |
10516 | |
10517 | return IsLaxCompatible(FirstType, SecondType) || |
10518 | IsLaxCompatible(SecondType, FirstType); |
10519 | } |
10520 | |
10521 | /// getRVVTypeSize - Return RVV vector register size. |
10522 | static uint64_t getRVVTypeSize(ASTContext &Context, const BuiltinType *Ty) { |
10523 | assert(Ty->isRVVVLSBuiltinType() && "Invalid RVV Type"); |
10524 | auto VScale = |
10525 | Context.getTargetInfo().getVScaleRange(LangOpts: Context.getLangOpts(), IsArmStreamingFunction: false); |
10526 | if (!VScale) |
10527 | return 0; |
10528 | |
10529 | ASTContext::BuiltinVectorTypeInfo Info = Context.getBuiltinVectorTypeInfo(Ty); |
10530 | |
10531 | uint64_t EltSize = Context.getTypeSize(Info.ElementType); |
10532 | if (Info.ElementType == Context.BoolTy) |
10533 | EltSize = 1; |
10534 | |
10535 | uint64_t MinElts = Info.EC.getKnownMinValue(); |
10536 | return VScale->first * MinElts * EltSize; |
10537 | } |
10538 | |
10539 | bool ASTContext::areCompatibleRVVTypes(QualType FirstType, |
10540 | QualType SecondType) { |
10541 | assert( |
10542 | ((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || |
10543 | (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && |
10544 | "Expected RVV builtin type and vector type!"); |
10545 | |
10546 | auto IsValidCast = [this](QualType FirstType, QualType SecondType) { |
10547 | if (const auto *BT = FirstType->getAs<BuiltinType>()) { |
10548 | if (const auto *VT = SecondType->getAs<VectorType>()) { |
10549 | if (VT->getVectorKind() == VectorKind::RVVFixedLengthMask) { |
10550 | BuiltinVectorTypeInfo Info = getBuiltinVectorTypeInfo(Ty: BT); |
10551 | return FirstType->isRVVVLSBuiltinType() && |
10552 | Info.ElementType == BoolTy && |
10553 | getTypeSize(SecondType) == ((getRVVTypeSize(*this, BT))); |
10554 | } |
10555 | if (VT->getVectorKind() == VectorKind::RVVFixedLengthMask_1) { |
10556 | BuiltinVectorTypeInfo Info = getBuiltinVectorTypeInfo(Ty: BT); |
10557 | return FirstType->isRVVVLSBuiltinType() && |
10558 | Info.ElementType == BoolTy && |
10559 | getTypeSize(SecondType) == ((getRVVTypeSize(*this, BT) * 8)); |
10560 | } |
10561 | if (VT->getVectorKind() == VectorKind::RVVFixedLengthMask_2) { |
10562 | BuiltinVectorTypeInfo Info = getBuiltinVectorTypeInfo(Ty: BT); |
10563 | return FirstType->isRVVVLSBuiltinType() && |
10564 | Info.ElementType == BoolTy && |
10565 | getTypeSize(SecondType) == ((getRVVTypeSize(*this, BT)) * 4); |
10566 | } |
10567 | if (VT->getVectorKind() == VectorKind::RVVFixedLengthMask_4) { |
10568 | BuiltinVectorTypeInfo Info = getBuiltinVectorTypeInfo(Ty: BT); |
10569 | return FirstType->isRVVVLSBuiltinType() && |
10570 | Info.ElementType == BoolTy && |
10571 | getTypeSize(SecondType) == ((getRVVTypeSize(*this, BT)) * 2); |
10572 | } |
10573 | if (VT->getVectorKind() == VectorKind::RVVFixedLengthData || |
10574 | VT->getVectorKind() == VectorKind::Generic) |
10575 | return FirstType->isRVVVLSBuiltinType() && |
10576 | getTypeSize(SecondType) == getRVVTypeSize(*this, BT) && |
10577 | hasSameType(VT->getElementType(), |
10578 | getBuiltinVectorTypeInfo(BT).ElementType); |
10579 | } |
10580 | } |
10581 | return false; |
10582 | }; |
10583 | |
10584 | return IsValidCast(FirstType, SecondType) || |
10585 | IsValidCast(SecondType, FirstType); |
10586 | } |
10587 | |
10588 | bool ASTContext::areLaxCompatibleRVVTypes(QualType FirstType, |
10589 | QualType SecondType) { |
10590 | assert( |
10591 | ((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || |
10592 | (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && |
10593 | "Expected RVV builtin type and vector type!"); |
10594 | |
10595 | auto IsLaxCompatible = [this](QualType FirstType, QualType SecondType) { |
10596 | const auto *BT = FirstType->getAs<BuiltinType>(); |
10597 | if (!BT) |
10598 | return false; |
10599 | |
10600 | if (!BT->isRVVVLSBuiltinType()) |
10601 | return false; |
10602 | |
10603 | const auto *VecTy = SecondType->getAs<VectorType>(); |
10604 | if (VecTy && VecTy->getVectorKind() == VectorKind::Generic) { |
10605 | const LangOptions::LaxVectorConversionKind LVCKind = |
10606 | getLangOpts().getLaxVectorConversions(); |
10607 | |
10608 | // If __riscv_v_fixed_vlen != N do not allow vector lax conversion. |
10609 | if (getTypeSize(T: SecondType) != getRVVTypeSize(Context&: *this, Ty: BT)) |
10610 | return false; |
10611 | |
10612 | // If -flax-vector-conversions=all is specified, the types are |
10613 | // certainly compatible. |
10614 | if (LVCKind == LangOptions::LaxVectorConversionKind::All) |
10615 | return true; |
10616 | |
10617 | // If -flax-vector-conversions=integer is specified, the types are |
10618 | // compatible if the elements are integer types. |
10619 | if (LVCKind == LangOptions::LaxVectorConversionKind::Integer) |
10620 | return VecTy->getElementType().getCanonicalType()->isIntegerType() && |
10621 | FirstType->getRVVEltType(Ctx: *this)->isIntegerType(); |
10622 | } |
10623 | |
10624 | return false; |
10625 | }; |
10626 | |
10627 | return IsLaxCompatible(FirstType, SecondType) || |
10628 | IsLaxCompatible(SecondType, FirstType); |
10629 | } |
10630 | |
10631 | bool ASTContext::hasDirectOwnershipQualifier(QualType Ty) const { |
10632 | while (true) { |
10633 | // __strong id |
10634 | if (const AttributedType *Attr = dyn_cast<AttributedType>(Val&: Ty)) { |
10635 | if (Attr->getAttrKind() == attr::ObjCOwnership) |
10636 | return true; |
10637 | |
10638 | Ty = Attr->getModifiedType(); |
10639 | |
10640 | // X *__strong (...) |
10641 | } else if (const ParenType *Paren = dyn_cast<ParenType>(Val&: Ty)) { |
10642 | Ty = Paren->getInnerType(); |
10643 | |
10644 | // We do not want to look through typedefs, typeof(expr), |
10645 | // typeof(type), or any other way that the type is somehow |
10646 | // abstracted. |
10647 | } else { |
10648 | return false; |
10649 | } |
10650 | } |
10651 | } |
10652 | |
10653 | //===----------------------------------------------------------------------===// |
10654 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
10655 | //===----------------------------------------------------------------------===// |
10656 | |
10657 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
10658 | /// inheritance hierarchy of 'rProto'. |
10659 | bool |
10660 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
10661 | ObjCProtocolDecl *rProto) const { |
10662 | if (declaresSameEntity(lProto, rProto)) |
10663 | return true; |
10664 | for (auto *PI : rProto->protocols()) |
10665 | if (ProtocolCompatibleWithProtocol(lProto, rProto: PI)) |
10666 | return true; |
10667 | return false; |
10668 | } |
10669 | |
10670 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
10671 | /// Class<pr1, ...>. |
10672 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible( |
10673 | const ObjCObjectPointerType *lhs, const ObjCObjectPointerType *rhs) { |
10674 | for (auto *lhsProto : lhs->quals()) { |
10675 | bool match = false; |
10676 | for (auto *rhsProto : rhs->quals()) { |
10677 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
10678 | match = true; |
10679 | break; |
10680 | } |
10681 | } |
10682 | if (!match) |
10683 | return false; |
10684 | } |
10685 | return true; |
10686 | } |
10687 | |
10688 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
10689 | /// ObjCQualifiedIDType. |
10690 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible( |
10691 | const ObjCObjectPointerType *lhs, const ObjCObjectPointerType *rhs, |
10692 | bool compare) { |
10693 | // Allow id<P..> and an 'id' in all cases. |
10694 | if (lhs->isObjCIdType() || rhs->isObjCIdType()) |
10695 | return true; |
10696 | |
10697 | // Don't allow id<P..> to convert to Class or Class<P..> in either direction. |
10698 | if (lhs->isObjCClassType() || lhs->isObjCQualifiedClassType() || |
10699 | rhs->isObjCClassType() || rhs->isObjCQualifiedClassType()) |
10700 | return false; |
10701 | |
10702 | if (lhs->isObjCQualifiedIdType()) { |
10703 | if (rhs->qual_empty()) { |
10704 | // If the RHS is a unqualified interface pointer "NSString*", |
10705 | // make sure we check the class hierarchy. |
10706 | if (ObjCInterfaceDecl *rhsID = rhs->getInterfaceDecl()) { |
10707 | for (auto *I : lhs->quals()) { |
10708 | // when comparing an id<P> on lhs with a static type on rhs, |
10709 | // see if static class implements all of id's protocols, directly or |
10710 | // through its super class and categories. |
10711 | if (!rhsID->ClassImplementsProtocol(I, true)) |
10712 | return false; |
10713 | } |
10714 | } |
10715 | // If there are no qualifiers and no interface, we have an 'id'. |
10716 | return true; |
10717 | } |
10718 | // Both the right and left sides have qualifiers. |
10719 | for (auto *lhsProto : lhs->quals()) { |
10720 | bool match = false; |
10721 | |
10722 | // when comparing an id<P> on lhs with a static type on rhs, |
10723 | // see if static class implements all of id's protocols, directly or |
10724 | // through its super class and categories. |
10725 | for (auto *rhsProto : rhs->quals()) { |
10726 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
10727 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
10728 | match = true; |
10729 | break; |
10730 | } |
10731 | } |
10732 | // If the RHS is a qualified interface pointer "NSString<P>*", |
10733 | // make sure we check the class hierarchy. |
10734 | if (ObjCInterfaceDecl *rhsID = rhs->getInterfaceDecl()) { |
10735 | for (auto *I : lhs->quals()) { |
10736 | // when comparing an id<P> on lhs with a static type on rhs, |
10737 | // see if static class implements all of id's protocols, directly or |
10738 | // through its super class and categories. |
10739 | if (rhsID->ClassImplementsProtocol(I, true)) { |
10740 | match = true; |
10741 | break; |
10742 | } |
10743 | } |
10744 | } |
10745 | if (!match) |
10746 | return false; |
10747 | } |
10748 | |
10749 | return true; |
10750 | } |
10751 | |
10752 | assert(rhs->isObjCQualifiedIdType() && "One of the LHS/RHS should be id<x>"); |
10753 | |
10754 | if (lhs->getInterfaceType()) { |
10755 | // If both the right and left sides have qualifiers. |
10756 | for (auto *lhsProto : lhs->quals()) { |
10757 | bool match = false; |
10758 | |
10759 | // when comparing an id<P> on rhs with a static type on lhs, |
10760 | // see if static class implements all of id's protocols, directly or |
10761 | // through its super class and categories. |
10762 | // First, lhs protocols in the qualifier list must be found, direct |
10763 | // or indirect in rhs's qualifier list or it is a mismatch. |
10764 | for (auto *rhsProto : rhs->quals()) { |
10765 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
10766 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
10767 | match = true; |
10768 | break; |
10769 | } |
10770 | } |
10771 | if (!match) |
10772 | return false; |
10773 | } |
10774 | |
10775 | // Static class's protocols, or its super class or category protocols |
10776 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
10777 | if (ObjCInterfaceDecl *lhsID = lhs->getInterfaceDecl()) { |
10778 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
10779 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
10780 | // This is rather dubious but matches gcc's behavior. If lhs has |
10781 | // no type qualifier and its class has no static protocol(s) |
10782 | // assume that it is mismatch. |
10783 | if (LHSInheritedProtocols.empty() && lhs->qual_empty()) |
10784 | return false; |
10785 | for (auto *lhsProto : LHSInheritedProtocols) { |
10786 | bool match = false; |
10787 | for (auto *rhsProto : rhs->quals()) { |
10788 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
10789 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
10790 | match = true; |
10791 | break; |
10792 | } |
10793 | } |
10794 | if (!match) |
10795 | return false; |
10796 | } |
10797 | } |
10798 | return true; |
10799 | } |
10800 | return false; |
10801 | } |
10802 | |
10803 | /// canAssignObjCInterfaces - Return true if the two interface types are |
10804 | /// compatible for assignment from RHS to LHS. This handles validation of any |
10805 | /// protocol qualifiers on the LHS or RHS. |
10806 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
10807 | const ObjCObjectPointerType *RHSOPT) { |
10808 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
10809 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
10810 | |
10811 | // If either type represents the built-in 'id' type, return true. |
10812 | if (LHS->isObjCUnqualifiedId() || RHS->isObjCUnqualifiedId()) |
10813 | return true; |
10814 | |
10815 | // Function object that propagates a successful result or handles |
10816 | // __kindof types. |
10817 | auto finish = [&](bool succeeded) -> bool { |
10818 | if (succeeded) |
10819 | return true; |
10820 | |
10821 | if (!RHS->isKindOfType()) |
10822 | return false; |
10823 | |
10824 | // Strip off __kindof and protocol qualifiers, then check whether |
10825 | // we can assign the other way. |
10826 | return canAssignObjCInterfaces(LHSOPT: RHSOPT->stripObjCKindOfTypeAndQuals(ctx: *this), |
10827 | RHSOPT: LHSOPT->stripObjCKindOfTypeAndQuals(ctx: *this)); |
10828 | }; |
10829 | |
10830 | // Casts from or to id<P> are allowed when the other side has compatible |
10831 | // protocols. |
10832 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) { |
10833 | return finish(ObjCQualifiedIdTypesAreCompatible(lhs: LHSOPT, rhs: RHSOPT, compare: false)); |
10834 | } |
10835 | |
10836 | // Verify protocol compatibility for casts from Class<P1> to Class<P2>. |
10837 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) { |
10838 | return finish(ObjCQualifiedClassTypesAreCompatible(lhs: LHSOPT, rhs: RHSOPT)); |
10839 | } |
10840 | |
10841 | // Casts from Class to Class<Foo>, or vice-versa, are allowed. |
10842 | if (LHS->isObjCClass() && RHS->isObjCClass()) { |
10843 | return true; |
10844 | } |
10845 | |
10846 | // If we have 2 user-defined types, fall into that path. |
10847 | if (LHS->getInterface() && RHS->getInterface()) { |
10848 | return finish(canAssignObjCInterfaces(LHS, RHS)); |
10849 | } |
10850 | |
10851 | return false; |
10852 | } |
10853 | |
10854 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
10855 | /// for providing type-safety for objective-c pointers used to pass/return |
10856 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
10857 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
10858 | /// not OK. For the return type, the opposite is not OK. |
10859 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
10860 | const ObjCObjectPointerType *LHSOPT, |
10861 | const ObjCObjectPointerType *RHSOPT, |
10862 | bool BlockReturnType) { |
10863 | |
10864 | // Function object that propagates a successful result or handles |
10865 | // __kindof types. |
10866 | auto finish = [&](bool succeeded) -> bool { |
10867 | if (succeeded) |
10868 | return true; |
10869 | |
10870 | const ObjCObjectPointerType *Expected = BlockReturnType ? RHSOPT : LHSOPT; |
10871 | if (!Expected->isKindOfType()) |
10872 | return false; |
10873 | |
10874 | // Strip off __kindof and protocol qualifiers, then check whether |
10875 | // we can assign the other way. |
10876 | return canAssignObjCInterfacesInBlockPointer( |
10877 | LHSOPT: RHSOPT->stripObjCKindOfTypeAndQuals(ctx: *this), |
10878 | RHSOPT: LHSOPT->stripObjCKindOfTypeAndQuals(ctx: *this), |
10879 | BlockReturnType); |
10880 | }; |
10881 | |
10882 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
10883 | return true; |
10884 | |
10885 | if (LHSOPT->isObjCBuiltinType()) { |
10886 | return finish(RHSOPT->isObjCBuiltinType() || |
10887 | RHSOPT->isObjCQualifiedIdType()); |
10888 | } |
10889 | |
10890 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) { |
10891 | if (getLangOpts().CompatibilityQualifiedIdBlockParamTypeChecking) |
10892 | // Use for block parameters previous type checking for compatibility. |
10893 | return finish(ObjCQualifiedIdTypesAreCompatible(lhs: LHSOPT, rhs: RHSOPT, compare: false) || |
10894 | // Or corrected type checking as in non-compat mode. |
10895 | (!BlockReturnType && |
10896 | ObjCQualifiedIdTypesAreCompatible(lhs: RHSOPT, rhs: LHSOPT, compare: false))); |
10897 | else |
10898 | return finish(ObjCQualifiedIdTypesAreCompatible( |
10899 | lhs: (BlockReturnType ? LHSOPT : RHSOPT), |
10900 | rhs: (BlockReturnType ? RHSOPT : LHSOPT), compare: false)); |
10901 | } |
10902 | |
10903 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
10904 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
10905 | if (LHS && RHS) { // We have 2 user-defined types. |
10906 | if (LHS != RHS) { |
10907 | if (LHS->getDecl()->isSuperClassOf(I: RHS->getDecl())) |
10908 | return finish(BlockReturnType); |
10909 | if (RHS->getDecl()->isSuperClassOf(I: LHS->getDecl())) |
10910 | return finish(!BlockReturnType); |
10911 | } |
10912 | else |
10913 | return true; |
10914 | } |
10915 | return false; |
10916 | } |
10917 | |
10918 | /// Comparison routine for Objective-C protocols to be used with |
10919 | /// llvm::array_pod_sort. |
10920 | static int compareObjCProtocolsByName(ObjCProtocolDecl * const *lhs, |
10921 | ObjCProtocolDecl * const *rhs) { |
10922 | return (*lhs)->getName().compare((*rhs)->getName()); |
10923 | } |
10924 | |
10925 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
10926 | /// of protocols inherited from two distinct objective-c pointer objects with |
10927 | /// the given common base. |
10928 | /// It is used to build composite qualifier list of the composite type of |
10929 | /// the conditional expression involving two objective-c pointer objects. |
10930 | static |
10931 | void getIntersectionOfProtocols(ASTContext &Context, |
10932 | const ObjCInterfaceDecl *CommonBase, |
10933 | const ObjCObjectPointerType *LHSOPT, |
10934 | const ObjCObjectPointerType *RHSOPT, |
10935 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionSet) { |
10936 | |
10937 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
10938 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
10939 | assert(LHS->getInterface() && "LHS must have an interface base"); |
10940 | assert(RHS->getInterface() && "RHS must have an interface base"); |
10941 | |
10942 | // Add all of the protocols for the LHS. |
10943 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSProtocolSet; |
10944 | |
10945 | // Start with the protocol qualifiers. |
10946 | for (auto *proto : LHS->quals()) { |
10947 | Context.CollectInheritedProtocols(proto, LHSProtocolSet); |
10948 | } |
10949 | |
10950 | // Also add the protocols associated with the LHS interface. |
10951 | Context.CollectInheritedProtocols(LHS->getInterface(), LHSProtocolSet); |
10952 | |
10953 | // Add all of the protocols for the RHS. |
10954 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSProtocolSet; |
10955 | |
10956 | // Start with the protocol qualifiers. |
10957 | for (auto *proto : RHS->quals()) { |
10958 | Context.CollectInheritedProtocols(proto, RHSProtocolSet); |
10959 | } |
10960 | |
10961 | // Also add the protocols associated with the RHS interface. |
10962 | Context.CollectInheritedProtocols(RHS->getInterface(), RHSProtocolSet); |
10963 | |
10964 | // Compute the intersection of the collected protocol sets. |
10965 | for (auto *proto : LHSProtocolSet) { |
10966 | if (RHSProtocolSet.count(Ptr: proto)) |
10967 | IntersectionSet.push_back(Elt: proto); |
10968 | } |
10969 | |
10970 | // Compute the set of protocols that is implied by either the common type or |
10971 | // the protocols within the intersection. |
10972 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ImpliedProtocols; |
10973 | Context.CollectInheritedProtocols(CommonBase, ImpliedProtocols); |
10974 | |
10975 | // Remove any implied protocols from the list of inherited protocols. |
10976 | if (!ImpliedProtocols.empty()) { |
10977 | llvm::erase_if(C&: IntersectionSet, P: [&](ObjCProtocolDecl *proto) -> bool { |
10978 | return ImpliedProtocols.contains(Ptr: proto); |
10979 | }); |
10980 | } |
10981 | |
10982 | // Sort the remaining protocols by name. |
10983 | llvm::array_pod_sort(Start: IntersectionSet.begin(), End: IntersectionSet.end(), |
10984 | Compare: compareObjCProtocolsByName); |
10985 | } |
10986 | |
10987 | /// Determine whether the first type is a subtype of the second. |
10988 | static bool canAssignObjCObjectTypes(ASTContext &ctx, QualType lhs, |
10989 | QualType rhs) { |
10990 | // Common case: two object pointers. |
10991 | const auto *lhsOPT = lhs->getAs<ObjCObjectPointerType>(); |
10992 | const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
10993 | if (lhsOPT && rhsOPT) |
10994 | return ctx.canAssignObjCInterfaces(LHSOPT: lhsOPT, RHSOPT: rhsOPT); |
10995 | |
10996 | // Two block pointers. |
10997 | const auto *lhsBlock = lhs->getAs<BlockPointerType>(); |
10998 | const auto *rhsBlock = rhs->getAs<BlockPointerType>(); |
10999 | if (lhsBlock && rhsBlock) |
11000 | return ctx.typesAreBlockPointerCompatible(lhs, rhs); |
11001 | |
11002 | // If either is an unqualified 'id' and the other is a block, it's |
11003 | // acceptable. |
11004 | if ((lhsOPT && lhsOPT->isObjCIdType() && rhsBlock) || |
11005 | (rhsOPT && rhsOPT->isObjCIdType() && lhsBlock)) |
11006 | return true; |
11007 | |
11008 | return false; |
11009 | } |
11010 | |
11011 | // Check that the given Objective-C type argument lists are equivalent. |
11012 | static bool sameObjCTypeArgs(ASTContext &ctx, |
11013 | const ObjCInterfaceDecl *iface, |
11014 | ArrayRef<QualType> lhsArgs, |
11015 | ArrayRef<QualType> rhsArgs, |
11016 | bool stripKindOf) { |
11017 | if (lhsArgs.size() != rhsArgs.size()) |
11018 | return false; |
11019 | |
11020 | ObjCTypeParamList *typeParams = iface->getTypeParamList(); |
11021 | if (!typeParams) |
11022 | return false; |
11023 | |
11024 | for (unsigned i = 0, n = lhsArgs.size(); i != n; ++i) { |
11025 | if (ctx.hasSameType(T1: lhsArgs[i], T2: rhsArgs[i])) |
11026 | continue; |
11027 | |
11028 | switch (typeParams->begin()[i]->getVariance()) { |
11029 | case ObjCTypeParamVariance::Invariant: |
11030 | if (!stripKindOf || |
11031 | !ctx.hasSameType(T1: lhsArgs[i].stripObjCKindOfType(ctx), |
11032 | T2: rhsArgs[i].stripObjCKindOfType(ctx))) { |
11033 | return false; |
11034 | } |
11035 | break; |
11036 | |
11037 | case ObjCTypeParamVariance::Covariant: |
11038 | if (!canAssignObjCObjectTypes(ctx, lhs: lhsArgs[i], rhs: rhsArgs[i])) |
11039 | return false; |
11040 | break; |
11041 | |
11042 | case ObjCTypeParamVariance::Contravariant: |
11043 | if (!canAssignObjCObjectTypes(ctx, lhs: rhsArgs[i], rhs: lhsArgs[i])) |
11044 | return false; |
11045 | break; |
11046 | } |
11047 | } |
11048 | |
11049 | return true; |
11050 | } |
11051 | |
11052 | QualType ASTContext::areCommonBaseCompatible( |
11053 | const ObjCObjectPointerType *Lptr, |
11054 | const ObjCObjectPointerType *Rptr) { |
11055 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
11056 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
11057 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
11058 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
11059 | |
11060 | if (!LDecl || !RDecl) |
11061 | return {}; |
11062 | |
11063 | // When either LHS or RHS is a kindof type, we should return a kindof type. |
11064 | // For example, for common base of kindof(ASub1) and kindof(ASub2), we return |
11065 | // kindof(A). |
11066 | bool anyKindOf = LHS->isKindOfType() || RHS->isKindOfType(); |
11067 | |
11068 | // Follow the left-hand side up the class hierarchy until we either hit a |
11069 | // root or find the RHS. Record the ancestors in case we don't find it. |
11070 | llvm::SmallDenseMap<const ObjCInterfaceDecl *, const ObjCObjectType *, 4> |
11071 | LHSAncestors; |
11072 | while (true) { |
11073 | // Record this ancestor. We'll need this if the common type isn't in the |
11074 | // path from the LHS to the root. |
11075 | LHSAncestors[LHS->getInterface()->getCanonicalDecl()] = LHS; |
11076 | |
11077 | if (declaresSameEntity(LHS->getInterface(), RDecl)) { |
11078 | // Get the type arguments. |
11079 | ArrayRef<QualType> LHSTypeArgs = LHS->getTypeArgsAsWritten(); |
11080 | bool anyChanges = false; |
11081 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
11082 | // Both have type arguments, compare them. |
11083 | if (!sameObjCTypeArgs(ctx&: *this, iface: LHS->getInterface(), |
11084 | lhsArgs: LHS->getTypeArgs(), rhsArgs: RHS->getTypeArgs(), |
11085 | /*stripKindOf=*/true)) |
11086 | return {}; |
11087 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
11088 | // If only one has type arguments, the result will not have type |
11089 | // arguments. |
11090 | LHSTypeArgs = {}; |
11091 | anyChanges = true; |
11092 | } |
11093 | |
11094 | // Compute the intersection of protocols. |
11095 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
11096 | getIntersectionOfProtocols(Context&: *this, CommonBase: LHS->getInterface(), LHSOPT: Lptr, RHSOPT: Rptr, |
11097 | IntersectionSet&: Protocols); |
11098 | if (!Protocols.empty()) |
11099 | anyChanges = true; |
11100 | |
11101 | // If anything in the LHS will have changed, build a new result type. |
11102 | // If we need to return a kindof type but LHS is not a kindof type, we |
11103 | // build a new result type. |
11104 | if (anyChanges || LHS->isKindOfType() != anyKindOf) { |
11105 | QualType Result = getObjCInterfaceType(Decl: LHS->getInterface()); |
11106 | Result = getObjCObjectType(baseType: Result, typeArgs: LHSTypeArgs, protocols: Protocols, |
11107 | isKindOf: anyKindOf || LHS->isKindOfType()); |
11108 | return getObjCObjectPointerType(ObjectT: Result); |
11109 | } |
11110 | |
11111 | return getObjCObjectPointerType(ObjectT: QualType(LHS, 0)); |
11112 | } |
11113 | |
11114 | // Find the superclass. |
11115 | QualType LHSSuperType = LHS->getSuperClassType(); |
11116 | if (LHSSuperType.isNull()) |
11117 | break; |
11118 | |
11119 | LHS = LHSSuperType->castAs<ObjCObjectType>(); |
11120 | } |
11121 | |
11122 | // We didn't find anything by following the LHS to its root; now check |
11123 | // the RHS against the cached set of ancestors. |
11124 | while (true) { |
11125 | auto KnownLHS = LHSAncestors.find(Val: RHS->getInterface()->getCanonicalDecl()); |
11126 | if (KnownLHS != LHSAncestors.end()) { |
11127 | LHS = KnownLHS->second; |
11128 | |
11129 | // Get the type arguments. |
11130 | ArrayRef<QualType> RHSTypeArgs = RHS->getTypeArgsAsWritten(); |
11131 | bool anyChanges = false; |
11132 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
11133 | // Both have type arguments, compare them. |
11134 | if (!sameObjCTypeArgs(ctx&: *this, iface: LHS->getInterface(), |
11135 | lhsArgs: LHS->getTypeArgs(), rhsArgs: RHS->getTypeArgs(), |
11136 | /*stripKindOf=*/true)) |
11137 | return {}; |
11138 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
11139 | // If only one has type arguments, the result will not have type |
11140 | // arguments. |
11141 | RHSTypeArgs = {}; |
11142 | anyChanges = true; |
11143 | } |
11144 | |
11145 | // Compute the intersection of protocols. |
11146 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
11147 | getIntersectionOfProtocols(Context&: *this, CommonBase: RHS->getInterface(), LHSOPT: Lptr, RHSOPT: Rptr, |
11148 | IntersectionSet&: Protocols); |
11149 | if (!Protocols.empty()) |
11150 | anyChanges = true; |
11151 | |
11152 | // If we need to return a kindof type but RHS is not a kindof type, we |
11153 | // build a new result type. |
11154 | if (anyChanges || RHS->isKindOfType() != anyKindOf) { |
11155 | QualType Result = getObjCInterfaceType(Decl: RHS->getInterface()); |
11156 | Result = getObjCObjectType(baseType: Result, typeArgs: RHSTypeArgs, protocols: Protocols, |
11157 | isKindOf: anyKindOf || RHS->isKindOfType()); |
11158 | return getObjCObjectPointerType(ObjectT: Result); |
11159 | } |
11160 | |
11161 | return getObjCObjectPointerType(ObjectT: QualType(RHS, 0)); |
11162 | } |
11163 | |
11164 | // Find the superclass of the RHS. |
11165 | QualType RHSSuperType = RHS->getSuperClassType(); |
11166 | if (RHSSuperType.isNull()) |
11167 | break; |
11168 | |
11169 | RHS = RHSSuperType->castAs<ObjCObjectType>(); |
11170 | } |
11171 | |
11172 | return {}; |
11173 | } |
11174 | |
11175 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
11176 | const ObjCObjectType *RHS) { |
11177 | assert(LHS->getInterface() && "LHS is not an interface type"); |
11178 | assert(RHS->getInterface() && "RHS is not an interface type"); |
11179 | |
11180 | // Verify that the base decls are compatible: the RHS must be a subclass of |
11181 | // the LHS. |
11182 | ObjCInterfaceDecl *LHSInterface = LHS->getInterface(); |
11183 | bool IsSuperClass = LHSInterface->isSuperClassOf(I: RHS->getInterface()); |
11184 | if (!IsSuperClass) |
11185 | return false; |
11186 | |
11187 | // If the LHS has protocol qualifiers, determine whether all of them are |
11188 | // satisfied by the RHS (i.e., the RHS has a superset of the protocols in the |
11189 | // LHS). |
11190 | if (LHS->getNumProtocols() > 0) { |
11191 | // OK if conversion of LHS to SuperClass results in narrowing of types |
11192 | // ; i.e., SuperClass may implement at least one of the protocols |
11193 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
11194 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
11195 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
11196 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
11197 | // Also, if RHS has explicit quelifiers, include them for comparing with LHS's |
11198 | // qualifiers. |
11199 | for (auto *RHSPI : RHS->quals()) |
11200 | CollectInheritedProtocols(RHSPI, SuperClassInheritedProtocols); |
11201 | // If there is no protocols associated with RHS, it is not a match. |
11202 | if (SuperClassInheritedProtocols.empty()) |
11203 | return false; |
11204 | |
11205 | for (const auto *LHSProto : LHS->quals()) { |
11206 | bool SuperImplementsProtocol = false; |
11207 | for (auto *SuperClassProto : SuperClassInheritedProtocols) |
11208 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
11209 | SuperImplementsProtocol = true; |
11210 | break; |
11211 | } |
11212 | if (!SuperImplementsProtocol) |
11213 | return false; |
11214 | } |
11215 | } |
11216 | |
11217 | // If the LHS is specialized, we may need to check type arguments. |
11218 | if (LHS->isSpecialized()) { |
11219 | // Follow the superclass chain until we've matched the LHS class in the |
11220 | // hierarchy. This substitutes type arguments through. |
11221 | const ObjCObjectType *RHSSuper = RHS; |
11222 | while (!declaresSameEntity(RHSSuper->getInterface(), LHSInterface)) |
11223 | RHSSuper = RHSSuper->getSuperClassType()->castAs<ObjCObjectType>(); |
11224 | |
11225 | // If the RHS is specializd, compare type arguments. |
11226 | if (RHSSuper->isSpecialized() && |
11227 | !sameObjCTypeArgs(ctx&: *this, iface: LHS->getInterface(), |
11228 | lhsArgs: LHS->getTypeArgs(), rhsArgs: RHSSuper->getTypeArgs(), |
11229 | /*stripKindOf=*/true)) { |
11230 | return false; |
11231 | } |
11232 | } |
11233 | |
11234 | return true; |
11235 | } |
11236 | |
11237 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
11238 | // get the "pointed to" types |
11239 | const auto *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
11240 | const auto *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
11241 | |
11242 | if (!LHSOPT || !RHSOPT) |
11243 | return false; |
11244 | |
11245 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
11246 | canAssignObjCInterfaces(LHSOPT: RHSOPT, RHSOPT: LHSOPT); |
11247 | } |
11248 | |
11249 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
11250 | return canAssignObjCInterfaces( |
11251 | LHSOPT: getObjCObjectPointerType(ObjectT: To)->castAs<ObjCObjectPointerType>(), |
11252 | RHSOPT: getObjCObjectPointerType(ObjectT: From)->castAs<ObjCObjectPointerType>()); |
11253 | } |
11254 | |
11255 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
11256 | /// both shall have the identically qualified version of a compatible type. |
11257 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
11258 | /// same. See 6.7.[2,3,5] for additional rules. |
11259 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
11260 | bool CompareUnqualified) { |
11261 | if (getLangOpts().CPlusPlus) |
11262 | return hasSameType(T1: LHS, T2: RHS); |
11263 | |
11264 | return !mergeTypes(LHS, RHS, OfBlockPointer: false, Unqualified: CompareUnqualified).isNull(); |
11265 | } |
11266 | |
11267 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
11268 | return typesAreCompatible(LHS, RHS); |
11269 | } |
11270 | |
11271 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
11272 | return !mergeTypes(LHS, RHS, OfBlockPointer: true).isNull(); |
11273 | } |
11274 | |
11275 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
11276 | /// of T is compatible with SubType, return the merged type, else return |
11277 | /// QualType() |
11278 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
11279 | bool OfBlockPointer, |
11280 | bool Unqualified) { |
11281 | if (const RecordType *UT = T->getAsUnionType()) { |
11282 | RecordDecl *UD = UT->getDecl(); |
11283 | if (UD->hasAttr<TransparentUnionAttr>()) { |
11284 | for (const auto *I : UD->fields()) { |
11285 | QualType ET = I->getType().getUnqualifiedType(); |
11286 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
11287 | if (!MT.isNull()) |
11288 | return MT; |
11289 | } |
11290 | } |
11291 | } |
11292 | |
11293 | return {}; |
11294 | } |
11295 | |
11296 | /// mergeFunctionParameterTypes - merge two types which appear as function |
11297 | /// parameter types |
11298 | QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs, |
11299 | bool OfBlockPointer, |
11300 | bool Unqualified) { |
11301 | // GNU extension: two types are compatible if they appear as a function |
11302 | // argument, one of the types is a transparent union type and the other |
11303 | // type is compatible with a union member |
11304 | QualType lmerge = mergeTransparentUnionType(T: lhs, SubType: rhs, OfBlockPointer, |
11305 | Unqualified); |
11306 | if (!lmerge.isNull()) |
11307 | return lmerge; |
11308 | |
11309 | QualType rmerge = mergeTransparentUnionType(T: rhs, SubType: lhs, OfBlockPointer, |
11310 | Unqualified); |
11311 | if (!rmerge.isNull()) |
11312 | return rmerge; |
11313 | |
11314 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
11315 | } |
11316 | |
11317 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
11318 | bool OfBlockPointer, bool Unqualified, |
11319 | bool AllowCXX, |
11320 | bool IsConditionalOperator) { |
11321 | const auto *lbase = lhs->castAs<FunctionType>(); |
11322 | const auto *rbase = rhs->castAs<FunctionType>(); |
11323 | const auto *lproto = dyn_cast<FunctionProtoType>(Val: lbase); |
11324 | const auto *rproto = dyn_cast<FunctionProtoType>(Val: rbase); |
11325 | bool allLTypes = true; |
11326 | bool allRTypes = true; |
11327 | |
11328 | // Check return type |
11329 | QualType retType; |
11330 | if (OfBlockPointer) { |
11331 | QualType RHS = rbase->getReturnType(); |
11332 | QualType LHS = lbase->getReturnType(); |
11333 | bool UnqualifiedResult = Unqualified; |
11334 | if (!UnqualifiedResult) |
11335 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
11336 | retType = mergeTypes(LHS, RHS, OfBlockPointer: true, Unqualified: UnqualifiedResult, BlockReturnType: true); |
11337 | } |
11338 | else |
11339 | retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), OfBlockPointer: false, |
11340 | Unqualified); |
11341 | if (retType.isNull()) |
11342 | return {}; |
11343 | |
11344 | if (Unqualified) |
11345 | retType = retType.getUnqualifiedType(); |
11346 | |
11347 | CanQualType LRetType = getCanonicalType(T: lbase->getReturnType()); |
11348 | CanQualType RRetType = getCanonicalType(T: rbase->getReturnType()); |
11349 | if (Unqualified) { |
11350 | LRetType = LRetType.getUnqualifiedType(); |
11351 | RRetType = RRetType.getUnqualifiedType(); |
11352 | } |
11353 | |
11354 | if (getCanonicalType(T: retType) != LRetType) |
11355 | allLTypes = false; |
11356 | if (getCanonicalType(T: retType) != RRetType) |
11357 | allRTypes = false; |
11358 | |
11359 | // FIXME: double check this |
11360 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
11361 | // rbase->getRegParmAttr() != 0 && |
11362 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
11363 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
11364 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
11365 | |
11366 | // Compatible functions must have compatible calling conventions |
11367 | if (lbaseInfo.getCC() != rbaseInfo.getCC()) |
11368 | return {}; |
11369 | |
11370 | // Regparm is part of the calling convention. |
11371 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
11372 | return {}; |
11373 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
11374 | return {}; |
11375 | |
11376 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
11377 | return {}; |
11378 | if (lbaseInfo.getNoCallerSavedRegs() != rbaseInfo.getNoCallerSavedRegs()) |
11379 | return {}; |
11380 | if (lbaseInfo.getNoCfCheck() != rbaseInfo.getNoCfCheck()) |
11381 | return {}; |
11382 | |
11383 | // When merging declarations, it's common for supplemental information like |
11384 | // attributes to only be present in one of the declarations, and we generally |
11385 | // want type merging to preserve the union of information. So a merged |
11386 | // function type should be noreturn if it was noreturn in *either* operand |
11387 | // type. |
11388 | // |
11389 | // But for the conditional operator, this is backwards. The result of the |
11390 | // operator could be either operand, and its type should conservatively |
11391 | // reflect that. So a function type in a composite type is noreturn only |
11392 | // if it's noreturn in *both* operand types. |
11393 | // |
11394 | // Arguably, noreturn is a kind of subtype, and the conditional operator |
11395 | // ought to produce the most specific common supertype of its operand types. |
11396 | // That would differ from this rule in contravariant positions. However, |
11397 | // neither C nor C++ generally uses this kind of subtype reasoning. Also, |
11398 | // as a practical matter, it would only affect C code that does abstraction of |
11399 | // higher-order functions (taking noreturn callbacks!), which is uncommon to |
11400 | // say the least. So we use the simpler rule. |
11401 | bool NoReturn = IsConditionalOperator |
11402 | ? lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn() |
11403 | : lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
11404 | if (lbaseInfo.getNoReturn() != NoReturn) |
11405 | allLTypes = false; |
11406 | if (rbaseInfo.getNoReturn() != NoReturn) |
11407 | allRTypes = false; |
11408 | |
11409 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(noReturn: NoReturn); |
11410 | |
11411 | std::optional<FunctionEffectSet> MergedFX; |
11412 | |
11413 | if (lproto && rproto) { // two C99 style function prototypes |
11414 | assert((AllowCXX || |
11415 | (!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec())) && |
11416 | "C++ shouldn't be here"); |
11417 | // Compatible functions must have the same number of parameters |
11418 | if (lproto->getNumParams() != rproto->getNumParams()) |
11419 | return {}; |
11420 | |
11421 | // Variadic and non-variadic functions aren't compatible |
11422 | if (lproto->isVariadic() != rproto->isVariadic()) |
11423 | return {}; |
11424 | |
11425 | if (lproto->getMethodQuals() != rproto->getMethodQuals()) |
11426 | return {}; |
11427 | |
11428 | // Function effects are handled similarly to noreturn, see above. |
11429 | FunctionEffectsRef LHSFX = lproto->getFunctionEffects(); |
11430 | FunctionEffectsRef RHSFX = rproto->getFunctionEffects(); |
11431 | if (LHSFX != RHSFX) { |
11432 | if (IsConditionalOperator) |
11433 | MergedFX = FunctionEffectSet::getIntersection(LHS: LHSFX, RHS: RHSFX); |
11434 | else { |
11435 | FunctionEffectSet::Conflicts Errs; |
11436 | MergedFX = FunctionEffectSet::getUnion(LHSFX, RHSFX, Errs); |
11437 | // Here we're discarding a possible error due to conflicts in the effect |
11438 | // sets. But we're not in a context where we can report it. The |
11439 | // operation does however guarantee maintenance of invariants. |
11440 | } |
11441 | if (*MergedFX != LHSFX) |
11442 | allLTypes = false; |
11443 | if (*MergedFX != RHSFX) |
11444 | allRTypes = false; |
11445 | } |
11446 | |
11447 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> newParamInfos; |
11448 | bool canUseLeft, canUseRight; |
11449 | if (!mergeExtParameterInfo(FirstFnType: lproto, SecondFnType: rproto, CanUseFirst&: canUseLeft, CanUseSecond&: canUseRight, |
11450 | NewParamInfos&: newParamInfos)) |
11451 | return {}; |
11452 | |
11453 | if (!canUseLeft) |
11454 | allLTypes = false; |
11455 | if (!canUseRight) |
11456 | allRTypes = false; |
11457 | |
11458 | // Check parameter type compatibility |
11459 | SmallVector<QualType, 10> types; |
11460 | for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) { |
11461 | QualType lParamType = lproto->getParamType(i).getUnqualifiedType(); |
11462 | QualType rParamType = rproto->getParamType(i).getUnqualifiedType(); |
11463 | QualType paramType = mergeFunctionParameterTypes( |
11464 | lhs: lParamType, rhs: rParamType, OfBlockPointer, Unqualified); |
11465 | if (paramType.isNull()) |
11466 | return {}; |
11467 | |
11468 | if (Unqualified) |
11469 | paramType = paramType.getUnqualifiedType(); |
11470 | |
11471 | types.push_back(Elt: paramType); |
11472 | if (Unqualified) { |
11473 | lParamType = lParamType.getUnqualifiedType(); |
11474 | rParamType = rParamType.getUnqualifiedType(); |
11475 | } |
11476 | |
11477 | if (getCanonicalType(T: paramType) != getCanonicalType(T: lParamType)) |
11478 | allLTypes = false; |
11479 | if (getCanonicalType(T: paramType) != getCanonicalType(T: rParamType)) |
11480 | allRTypes = false; |
11481 | } |
11482 | |
11483 | if (allLTypes) return lhs; |
11484 | if (allRTypes) return rhs; |
11485 | |
11486 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
11487 | EPI.ExtInfo = einfo; |
11488 | EPI.ExtParameterInfos = |
11489 | newParamInfos.empty() ? nullptr : newParamInfos.data(); |
11490 | if (MergedFX) |
11491 | EPI.FunctionEffects = *MergedFX; |
11492 | return getFunctionType(ResultTy: retType, Args: types, EPI); |
11493 | } |
11494 | |
11495 | if (lproto) allRTypes = false; |
11496 | if (rproto) allLTypes = false; |
11497 | |
11498 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
11499 | if (proto) { |
11500 | assert((AllowCXX || !proto->hasExceptionSpec()) && "C++ shouldn't be here"); |
11501 | if (proto->isVariadic()) |
11502 | return {}; |
11503 | // Check that the types are compatible with the types that |
11504 | // would result from default argument promotions (C99 6.7.5.3p15). |
11505 | // The only types actually affected are promotable integer |
11506 | // types and floats, which would be passed as a different |
11507 | // type depending on whether the prototype is visible. |
11508 | for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) { |
11509 | QualType paramTy = proto->getParamType(i); |
11510 | |
11511 | // Look at the converted type of enum types, since that is the type used |
11512 | // to pass enum values. |
11513 | if (const auto *Enum = paramTy->getAs<EnumType>()) { |
11514 | paramTy = Enum->getDecl()->getIntegerType(); |
11515 | if (paramTy.isNull()) |
11516 | return {}; |
11517 | } |
11518 | |
11519 | if (isPromotableIntegerType(paramTy) || |
11520 | getCanonicalType(paramTy).getUnqualifiedType() == FloatTy) |
11521 | return {}; |
11522 | } |
11523 | |
11524 | if (allLTypes) return lhs; |
11525 | if (allRTypes) return rhs; |
11526 | |
11527 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
11528 | EPI.ExtInfo = einfo; |
11529 | if (MergedFX) |
11530 | EPI.FunctionEffects = *MergedFX; |
11531 | return getFunctionType(ResultTy: retType, Args: proto->getParamTypes(), EPI); |
11532 | } |
11533 | |
11534 | if (allLTypes) return lhs; |
11535 | if (allRTypes) return rhs; |
11536 | return getFunctionNoProtoType(ResultTy: retType, Info: einfo); |
11537 | } |
11538 | |
11539 | /// Given that we have an enum type and a non-enum type, try to merge them. |
11540 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
11541 | QualType other, bool isBlockReturnType) { |
11542 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
11543 | // a signed integer type, or an unsigned integer type. |
11544 | // Compatibility is based on the underlying type, not the promotion |
11545 | // type. |
11546 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
11547 | if (underlyingType.isNull()) |
11548 | return {}; |
11549 | if (Context.hasSameType(T1: underlyingType, T2: other)) |
11550 | return other; |
11551 | |
11552 | // In block return types, we're more permissive and accept any |
11553 | // integral type of the same size. |
11554 | if (isBlockReturnType && other->isIntegerType() && |
11555 | Context.getTypeSize(T: underlyingType) == Context.getTypeSize(T: other)) |
11556 | return other; |
11557 | |
11558 | return {}; |
11559 | } |
11560 | |
11561 | QualType ASTContext::mergeTagDefinitions(QualType LHS, QualType RHS) { |
11562 | // C17 and earlier and C++ disallow two tag definitions within the same TU |
11563 | // from being compatible. |
11564 | if (LangOpts.CPlusPlus || !LangOpts.C23) |
11565 | return {}; |
11566 | |
11567 | // C23, on the other hand, requires the members to be "the same enough", so |
11568 | // we use a structural equivalence check. |
11569 | StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls; |
11570 | StructuralEquivalenceContext Ctx( |
11571 | getLangOpts(), *this, *this, NonEquivalentDecls, |
11572 | StructuralEquivalenceKind::Default, /*StrictTypeSpelling=*/false, |
11573 | /*Complain=*/false, /*ErrorOnTagTypeMismatch=*/true); |
11574 | return Ctx.IsEquivalent(T1: LHS, T2: RHS) ? LHS : QualType{}; |
11575 | } |
11576 | |
11577 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, bool OfBlockPointer, |
11578 | bool Unqualified, bool BlockReturnType, |
11579 | bool IsConditionalOperator) { |
11580 | // For C++ we will not reach this code with reference types (see below), |
11581 | // for OpenMP variant call overloading we might. |
11582 | // |
11583 | // C++ [expr]: If an expression initially has the type "reference to T", the |
11584 | // type is adjusted to "T" prior to any further analysis, the expression |
11585 | // designates the object or function denoted by the reference, and the |
11586 | // expression is an lvalue unless the reference is an rvalue reference and |
11587 | // the expression is a function call (possibly inside parentheses). |
11588 | auto *LHSRefTy = LHS->getAs<ReferenceType>(); |
11589 | auto *RHSRefTy = RHS->getAs<ReferenceType>(); |
11590 | if (LangOpts.OpenMP && LHSRefTy && RHSRefTy && |
11591 | LHS->getTypeClass() == RHS->getTypeClass()) |
11592 | return mergeTypes(LHS: LHSRefTy->getPointeeType(), RHS: RHSRefTy->getPointeeType(), |
11593 | OfBlockPointer, Unqualified, BlockReturnType); |
11594 | if (LHSRefTy || RHSRefTy) |
11595 | return {}; |
11596 | |
11597 | if (Unqualified) { |
11598 | LHS = LHS.getUnqualifiedType(); |
11599 | RHS = RHS.getUnqualifiedType(); |
11600 | } |
11601 | |
11602 | QualType LHSCan = getCanonicalType(T: LHS), |
11603 | RHSCan = getCanonicalType(T: RHS); |
11604 | |
11605 | // If two types are identical, they are compatible. |
11606 | if (LHSCan == RHSCan) |
11607 | return LHS; |
11608 | |
11609 | // If the qualifiers are different, the types aren't compatible... mostly. |
11610 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
11611 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
11612 | if (LQuals != RQuals) { |
11613 | // If any of these qualifiers are different, we have a type |
11614 | // mismatch. |
11615 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
11616 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
11617 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime() || |
11618 | !LQuals.getPointerAuth().isEquivalent(Other: RQuals.getPointerAuth()) || |
11619 | LQuals.hasUnaligned() != RQuals.hasUnaligned()) |
11620 | return {}; |
11621 | |
11622 | // Exactly one GC qualifier difference is allowed: __strong is |
11623 | // okay if the other type has no GC qualifier but is an Objective |
11624 | // C object pointer (i.e. implicitly strong by default). We fix |
11625 | // this by pretending that the unqualified type was actually |
11626 | // qualified __strong. |
11627 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
11628 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
11629 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
11630 | |
11631 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
11632 | return {}; |
11633 | |
11634 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
11635 | return mergeTypes(LHS, RHS: getObjCGCQualType(T: RHS, GCAttr: Qualifiers::Strong)); |
11636 | } |
11637 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
11638 | return mergeTypes(LHS: getObjCGCQualType(T: LHS, GCAttr: Qualifiers::Strong), RHS); |
11639 | } |
11640 | return {}; |
11641 | } |
11642 | |
11643 | // Okay, qualifiers are equal. |
11644 | |
11645 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
11646 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
11647 | |
11648 | // We want to consider the two function types to be the same for these |
11649 | // comparisons, just force one to the other. |
11650 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
11651 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
11652 | |
11653 | // Same as above for arrays |
11654 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
11655 | LHSClass = Type::ConstantArray; |
11656 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
11657 | RHSClass = Type::ConstantArray; |
11658 | |
11659 | // ObjCInterfaces are just specialized ObjCObjects. |
11660 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
11661 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
11662 | |
11663 | // Canonicalize ExtVector -> Vector. |
11664 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
11665 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
11666 | |
11667 | // If the canonical type classes don't match. |
11668 | if (LHSClass != RHSClass) { |
11669 | // Note that we only have special rules for turning block enum |
11670 | // returns into block int returns, not vice-versa. |
11671 | if (const auto *ETy = LHS->getAs<EnumType>()) { |
11672 | return mergeEnumWithInteger(Context&: *this, ET: ETy, other: RHS, isBlockReturnType: false); |
11673 | } |
11674 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
11675 | return mergeEnumWithInteger(Context&: *this, ET: ETy, other: LHS, isBlockReturnType: BlockReturnType); |
11676 | } |
11677 | // allow block pointer type to match an 'id' type. |
11678 | if (OfBlockPointer && !BlockReturnType) { |
11679 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
11680 | return LHS; |
11681 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
11682 | return RHS; |
11683 | } |
11684 | // Allow __auto_type to match anything; it merges to the type with more |
11685 | // information. |
11686 | if (const auto *AT = LHS->getAs<AutoType>()) { |
11687 | if (!AT->isDeduced() && AT->isGNUAutoType()) |
11688 | return RHS; |
11689 | } |
11690 | if (const auto *AT = RHS->getAs<AutoType>()) { |
11691 | if (!AT->isDeduced() && AT->isGNUAutoType()) |
11692 | return LHS; |
11693 | } |
11694 | return {}; |
11695 | } |
11696 | |
11697 | // The canonical type classes match. |
11698 | switch (LHSClass) { |
11699 | #define TYPE(Class, Base) |
11700 | #define ABSTRACT_TYPE(Class, Base) |
11701 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
11702 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
11703 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
11704 | #include "clang/AST/TypeNodes.inc" |
11705 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
11706 | |
11707 | case Type::Auto: |
11708 | case Type::DeducedTemplateSpecialization: |
11709 | case Type::LValueReference: |
11710 | case Type::RValueReference: |
11711 | case Type::MemberPointer: |
11712 | llvm_unreachable("C++ should never be in mergeTypes"); |
11713 | |
11714 | case Type::ObjCInterface: |
11715 | case Type::IncompleteArray: |
11716 | case Type::VariableArray: |
11717 | case Type::FunctionProto: |
11718 | case Type::ExtVector: |
11719 | llvm_unreachable("Types are eliminated above"); |
11720 | |
11721 | case Type::Pointer: |
11722 | { |
11723 | // Merge two pointer types, while trying to preserve typedef info |
11724 | QualType LHSPointee = LHS->castAs<PointerType>()->getPointeeType(); |
11725 | QualType RHSPointee = RHS->castAs<PointerType>()->getPointeeType(); |
11726 | if (Unqualified) { |
11727 | LHSPointee = LHSPointee.getUnqualifiedType(); |
11728 | RHSPointee = RHSPointee.getUnqualifiedType(); |
11729 | } |
11730 | QualType ResultType = mergeTypes(LHS: LHSPointee, RHS: RHSPointee, OfBlockPointer: false, |
11731 | Unqualified); |
11732 | if (ResultType.isNull()) |
11733 | return {}; |
11734 | if (getCanonicalType(T: LHSPointee) == getCanonicalType(T: ResultType)) |
11735 | return LHS; |
11736 | if (getCanonicalType(T: RHSPointee) == getCanonicalType(T: ResultType)) |
11737 | return RHS; |
11738 | return getPointerType(T: ResultType); |
11739 | } |
11740 | case Type::BlockPointer: |
11741 | { |
11742 | // Merge two block pointer types, while trying to preserve typedef info |
11743 | QualType LHSPointee = LHS->castAs<BlockPointerType>()->getPointeeType(); |
11744 | QualType RHSPointee = RHS->castAs<BlockPointerType>()->getPointeeType(); |
11745 | if (Unqualified) { |
11746 | LHSPointee = LHSPointee.getUnqualifiedType(); |
11747 | RHSPointee = RHSPointee.getUnqualifiedType(); |
11748 | } |
11749 | if (getLangOpts().OpenCL) { |
11750 | Qualifiers LHSPteeQual = LHSPointee.getQualifiers(); |
11751 | Qualifiers RHSPteeQual = RHSPointee.getQualifiers(); |
11752 | // Blocks can't be an expression in a ternary operator (OpenCL v2.0 |
11753 | // 6.12.5) thus the following check is asymmetric. |
11754 | if (!LHSPteeQual.isAddressSpaceSupersetOf(other: RHSPteeQual, Ctx: *this)) |
11755 | return {}; |
11756 | LHSPteeQual.removeAddressSpace(); |
11757 | RHSPteeQual.removeAddressSpace(); |
11758 | LHSPointee = |
11759 | QualType(LHSPointee.getTypePtr(), LHSPteeQual.getAsOpaqueValue()); |
11760 | RHSPointee = |
11761 | QualType(RHSPointee.getTypePtr(), RHSPteeQual.getAsOpaqueValue()); |
11762 | } |
11763 | QualType ResultType = mergeTypes(LHS: LHSPointee, RHS: RHSPointee, OfBlockPointer, |
11764 | Unqualified); |
11765 | if (ResultType.isNull()) |
11766 | return {}; |
11767 | if (getCanonicalType(T: LHSPointee) == getCanonicalType(T: ResultType)) |
11768 | return LHS; |
11769 | if (getCanonicalType(T: RHSPointee) == getCanonicalType(T: ResultType)) |
11770 | return RHS; |
11771 | return getBlockPointerType(T: ResultType); |
11772 | } |
11773 | case Type::Atomic: |
11774 | { |
11775 | // Merge two pointer types, while trying to preserve typedef info |
11776 | QualType LHSValue = LHS->castAs<AtomicType>()->getValueType(); |
11777 | QualType RHSValue = RHS->castAs<AtomicType>()->getValueType(); |
11778 | if (Unqualified) { |
11779 | LHSValue = LHSValue.getUnqualifiedType(); |
11780 | RHSValue = RHSValue.getUnqualifiedType(); |
11781 | } |
11782 | QualType ResultType = mergeTypes(LHS: LHSValue, RHS: RHSValue, OfBlockPointer: false, |
11783 | Unqualified); |
11784 | if (ResultType.isNull()) |
11785 | return {}; |
11786 | if (getCanonicalType(T: LHSValue) == getCanonicalType(T: ResultType)) |
11787 | return LHS; |
11788 | if (getCanonicalType(T: RHSValue) == getCanonicalType(T: ResultType)) |
11789 | return RHS; |
11790 | return getAtomicType(T: ResultType); |
11791 | } |
11792 | case Type::ConstantArray: |
11793 | { |
11794 | const ConstantArrayType* LCAT = getAsConstantArrayType(T: LHS); |
11795 | const ConstantArrayType* RCAT = getAsConstantArrayType(T: RHS); |
11796 | if (LCAT && RCAT && RCAT->getZExtSize() != LCAT->getZExtSize()) |
11797 | return {}; |
11798 | |
11799 | QualType LHSElem = getAsArrayType(T: LHS)->getElementType(); |
11800 | QualType RHSElem = getAsArrayType(T: RHS)->getElementType(); |
11801 | if (Unqualified) { |
11802 | LHSElem = LHSElem.getUnqualifiedType(); |
11803 | RHSElem = RHSElem.getUnqualifiedType(); |
11804 | } |
11805 | |
11806 | QualType ResultType = mergeTypes(LHS: LHSElem, RHS: RHSElem, OfBlockPointer: false, Unqualified); |
11807 | if (ResultType.isNull()) |
11808 | return {}; |
11809 | |
11810 | const VariableArrayType* LVAT = getAsVariableArrayType(T: LHS); |
11811 | const VariableArrayType* RVAT = getAsVariableArrayType(T: RHS); |
11812 | |
11813 | // If either side is a variable array, and both are complete, check whether |
11814 | // the current dimension is definite. |
11815 | if (LVAT || RVAT) { |
11816 | auto SizeFetch = [this](const VariableArrayType* VAT, |
11817 | const ConstantArrayType* CAT) |
11818 | -> std::pair<bool,llvm::APInt> { |
11819 | if (VAT) { |
11820 | std::optional<llvm::APSInt> TheInt; |
11821 | Expr *E = VAT->getSizeExpr(); |
11822 | if (E && (TheInt = E->getIntegerConstantExpr(*this))) |
11823 | return std::make_pair(true, *TheInt); |
11824 | return std::make_pair(false, llvm::APSInt()); |
11825 | } |
11826 | if (CAT) |
11827 | return std::make_pair(true, CAT->getSize()); |
11828 | return std::make_pair(false, llvm::APInt()); |
11829 | }; |
11830 | |
11831 | bool HaveLSize, HaveRSize; |
11832 | llvm::APInt LSize, RSize; |
11833 | std::tie(HaveLSize, LSize) = SizeFetch(LVAT, LCAT); |
11834 | std::tie(HaveRSize, RSize) = SizeFetch(RVAT, RCAT); |
11835 | if (HaveLSize && HaveRSize && !llvm::APInt::isSameValue(I1: LSize, I2: RSize)) |
11836 | return {}; // Definite, but unequal, array dimension |
11837 | } |
11838 | |
11839 | if (LCAT && getCanonicalType(T: LHSElem) == getCanonicalType(T: ResultType)) |
11840 | return LHS; |
11841 | if (RCAT && getCanonicalType(T: RHSElem) == getCanonicalType(T: ResultType)) |
11842 | return RHS; |
11843 | if (LCAT) |
11844 | return getConstantArrayType(EltTy: ResultType, ArySizeIn: LCAT->getSize(), |
11845 | SizeExpr: LCAT->getSizeExpr(), ASM: ArraySizeModifier(), IndexTypeQuals: 0); |
11846 | if (RCAT) |
11847 | return getConstantArrayType(EltTy: ResultType, ArySizeIn: RCAT->getSize(), |
11848 | SizeExpr: RCAT->getSizeExpr(), ASM: ArraySizeModifier(), IndexTypeQuals: 0); |
11849 | if (LVAT && getCanonicalType(T: LHSElem) == getCanonicalType(T: ResultType)) |
11850 | return LHS; |
11851 | if (RVAT && getCanonicalType(T: RHSElem) == getCanonicalType(T: ResultType)) |
11852 | return RHS; |
11853 | if (LVAT) { |
11854 | // FIXME: This isn't correct! But tricky to implement because |
11855 | // the array's size has to be the size of LHS, but the type |
11856 | // has to be different. |
11857 | return LHS; |
11858 | } |
11859 | if (RVAT) { |
11860 | // FIXME: This isn't correct! But tricky to implement because |
11861 | // the array's size has to be the size of RHS, but the type |
11862 | // has to be different. |
11863 | return RHS; |
11864 | } |
11865 | if (getCanonicalType(T: LHSElem) == getCanonicalType(T: ResultType)) return LHS; |
11866 | if (getCanonicalType(T: RHSElem) == getCanonicalType(T: ResultType)) return RHS; |
11867 | return getIncompleteArrayType(elementType: ResultType, ASM: ArraySizeModifier(), elementTypeQuals: 0); |
11868 | } |
11869 | case Type::FunctionNoProto: |
11870 | return mergeFunctionTypes(lhs: LHS, rhs: RHS, OfBlockPointer, Unqualified, |
11871 | /*AllowCXX=*/false, IsConditionalOperator); |
11872 | case Type::Record: |
11873 | case Type::Enum: |
11874 | return mergeTagDefinitions(LHS, RHS); |
11875 | case Type::Builtin: |
11876 | // Only exactly equal builtin types are compatible, which is tested above. |
11877 | return {}; |
11878 | case Type::Complex: |
11879 | // Distinct complex types are incompatible. |
11880 | return {}; |
11881 | case Type::Vector: |
11882 | // FIXME: The merged type should be an ExtVector! |
11883 | if (areCompatVectorTypes(LHSCan->castAs<VectorType>(), |
11884 | RHSCan->castAs<VectorType>())) |
11885 | return LHS; |
11886 | return {}; |
11887 | case Type::ConstantMatrix: |
11888 | if (areCompatMatrixTypes(LHSCan->castAs<ConstantMatrixType>(), |
11889 | RHSCan->castAs<ConstantMatrixType>())) |
11890 | return LHS; |
11891 | return {}; |
11892 | case Type::ObjCObject: { |
11893 | // Check if the types are assignment compatible. |
11894 | // FIXME: This should be type compatibility, e.g. whether |
11895 | // "LHS x; RHS x;" at global scope is legal. |
11896 | if (canAssignObjCInterfaces(LHS->castAs<ObjCObjectType>(), |
11897 | RHS->castAs<ObjCObjectType>())) |
11898 | return LHS; |
11899 | return {}; |
11900 | } |
11901 | case Type::ObjCObjectPointer: |
11902 | if (OfBlockPointer) { |
11903 | if (canAssignObjCInterfacesInBlockPointer( |
11904 | LHSOPT: LHS->castAs<ObjCObjectPointerType>(), |
11905 | RHSOPT: RHS->castAs<ObjCObjectPointerType>(), BlockReturnType)) |
11906 | return LHS; |
11907 | return {}; |
11908 | } |
11909 | if (canAssignObjCInterfaces(LHS->castAs<ObjCObjectPointerType>(), |
11910 | RHS->castAs<ObjCObjectPointerType>())) |
11911 | return LHS; |
11912 | return {}; |
11913 | case Type::Pipe: |
11914 | assert(LHS != RHS && |
11915 | "Equivalent pipe types should have already been handled!"); |
11916 | return {}; |
11917 | case Type::ArrayParameter: |
11918 | assert(LHS != RHS && |
11919 | "Equivalent ArrayParameter types should have already been handled!"); |
11920 | return {}; |
11921 | case Type::BitInt: { |
11922 | // Merge two bit-precise int types, while trying to preserve typedef info. |
11923 | bool LHSUnsigned = LHS->castAs<BitIntType>()->isUnsigned(); |
11924 | bool RHSUnsigned = RHS->castAs<BitIntType>()->isUnsigned(); |
11925 | unsigned LHSBits = LHS->castAs<BitIntType>()->getNumBits(); |
11926 | unsigned RHSBits = RHS->castAs<BitIntType>()->getNumBits(); |
11927 | |
11928 | // Like unsigned/int, shouldn't have a type if they don't match. |
11929 | if (LHSUnsigned != RHSUnsigned) |
11930 | return {}; |
11931 | |
11932 | if (LHSBits != RHSBits) |
11933 | return {}; |
11934 | return LHS; |
11935 | } |
11936 | case Type::HLSLAttributedResource: { |
11937 | const HLSLAttributedResourceType *LHSTy = |
11938 | LHS->castAs<HLSLAttributedResourceType>(); |
11939 | const HLSLAttributedResourceType *RHSTy = |
11940 | RHS->castAs<HLSLAttributedResourceType>(); |
11941 | assert(LHSTy->getWrappedType() == RHSTy->getWrappedType() && |
11942 | LHSTy->getWrappedType()->isHLSLResourceType() && |
11943 | "HLSLAttributedResourceType should always wrap __hlsl_resource_t"); |
11944 | |
11945 | if (LHSTy->getAttrs() == RHSTy->getAttrs() && |
11946 | LHSTy->getContainedType() == RHSTy->getContainedType()) |
11947 | return LHS; |
11948 | return {}; |
11949 | } |
11950 | case Type::HLSLInlineSpirv: |
11951 | const HLSLInlineSpirvType *LHSTy = LHS->castAs<HLSLInlineSpirvType>(); |
11952 | const HLSLInlineSpirvType *RHSTy = RHS->castAs<HLSLInlineSpirvType>(); |
11953 | |
11954 | if (LHSTy->getOpcode() == RHSTy->getOpcode() && |
11955 | LHSTy->getSize() == RHSTy->getSize() && |
11956 | LHSTy->getAlignment() == RHSTy->getAlignment()) { |
11957 | for (size_t I = 0; I < LHSTy->getOperands().size(); I++) |
11958 | if (LHSTy->getOperands()[I] != RHSTy->getOperands()[I]) |
11959 | return {}; |
11960 | |
11961 | return LHS; |
11962 | } |
11963 | return {}; |
11964 | } |
11965 | |
11966 | llvm_unreachable("Invalid Type::Class!"); |
11967 | } |
11968 | |
11969 | bool ASTContext::mergeExtParameterInfo( |
11970 | const FunctionProtoType *FirstFnType, const FunctionProtoType *SecondFnType, |
11971 | bool &CanUseFirst, bool &CanUseSecond, |
11972 | SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &NewParamInfos) { |
11973 | assert(NewParamInfos.empty() && "param info list not empty"); |
11974 | CanUseFirst = CanUseSecond = true; |
11975 | bool FirstHasInfo = FirstFnType->hasExtParameterInfos(); |
11976 | bool SecondHasInfo = SecondFnType->hasExtParameterInfos(); |
11977 | |
11978 | // Fast path: if the first type doesn't have ext parameter infos, |
11979 | // we match if and only if the second type also doesn't have them. |
11980 | if (!FirstHasInfo && !SecondHasInfo) |
11981 | return true; |
11982 | |
11983 | bool NeedParamInfo = false; |
11984 | size_t E = FirstHasInfo ? FirstFnType->getExtParameterInfos().size() |
11985 | : SecondFnType->getExtParameterInfos().size(); |
11986 | |
11987 | for (size_t I = 0; I < E; ++I) { |
11988 | FunctionProtoType::ExtParameterInfo FirstParam, SecondParam; |
11989 | if (FirstHasInfo) |
11990 | FirstParam = FirstFnType->getExtParameterInfo(I); |
11991 | if (SecondHasInfo) |
11992 | SecondParam = SecondFnType->getExtParameterInfo(I); |
11993 | |
11994 | // Cannot merge unless everything except the noescape flag matches. |
11995 | if (FirstParam.withIsNoEscape(NoEscape: false) != SecondParam.withIsNoEscape(NoEscape: false)) |
11996 | return false; |
11997 | |
11998 | bool FirstNoEscape = FirstParam.isNoEscape(); |
11999 | bool SecondNoEscape = SecondParam.isNoEscape(); |
12000 | bool IsNoEscape = FirstNoEscape && SecondNoEscape; |
12001 | NewParamInfos.push_back(Elt: FirstParam.withIsNoEscape(NoEscape: IsNoEscape)); |
12002 | if (NewParamInfos.back().getOpaqueValue()) |
12003 | NeedParamInfo = true; |
12004 | if (FirstNoEscape != IsNoEscape) |
12005 | CanUseFirst = false; |
12006 | if (SecondNoEscape != IsNoEscape) |
12007 | CanUseSecond = false; |
12008 | } |
12009 | |
12010 | if (!NeedParamInfo) |
12011 | NewParamInfos.clear(); |
12012 | |
12013 | return true; |
12014 | } |
12015 | |
12016 | void ASTContext::ResetObjCLayout(const ObjCInterfaceDecl *D) { |
12017 | if (auto It = ObjCLayouts.find(Val: D); It != ObjCLayouts.end()) { |
12018 | It->second = nullptr; |
12019 | for (auto *SubClass : ObjCSubClasses[D]) |
12020 | ResetObjCLayout(SubClass); |
12021 | } |
12022 | } |
12023 | |
12024 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
12025 | /// 'RHS' attributes and returns the merged version; including for function |
12026 | /// return types. |
12027 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
12028 | QualType LHSCan = getCanonicalType(T: LHS), |
12029 | RHSCan = getCanonicalType(T: RHS); |
12030 | // If two types are identical, they are compatible. |
12031 | if (LHSCan == RHSCan) |
12032 | return LHS; |
12033 | if (RHSCan->isFunctionType()) { |
12034 | if (!LHSCan->isFunctionType()) |
12035 | return {}; |
12036 | QualType OldReturnType = |
12037 | cast<FunctionType>(Val: RHSCan.getTypePtr())->getReturnType(); |
12038 | QualType NewReturnType = |
12039 | cast<FunctionType>(Val: LHSCan.getTypePtr())->getReturnType(); |
12040 | QualType ResReturnType = |
12041 | mergeObjCGCQualifiers(LHS: NewReturnType, RHS: OldReturnType); |
12042 | if (ResReturnType.isNull()) |
12043 | return {}; |
12044 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
12045 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
12046 | // In either case, use OldReturnType to build the new function type. |
12047 | const auto *F = LHS->castAs<FunctionType>(); |
12048 | if (const auto *FPT = cast<FunctionProtoType>(Val: F)) { |
12049 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
12050 | EPI.ExtInfo = getFunctionExtInfo(t: LHS); |
12051 | QualType ResultType = |
12052 | getFunctionType(ResultTy: OldReturnType, Args: FPT->getParamTypes(), EPI); |
12053 | return ResultType; |
12054 | } |
12055 | } |
12056 | return {}; |
12057 | } |
12058 | |
12059 | // If the qualifiers are different, the types can still be merged. |
12060 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
12061 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
12062 | if (LQuals != RQuals) { |
12063 | // If any of these qualifiers are different, we have a type mismatch. |
12064 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
12065 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
12066 | return {}; |
12067 | |
12068 | // Exactly one GC qualifier difference is allowed: __strong is |
12069 | // okay if the other type has no GC qualifier but is an Objective |
12070 | // C object pointer (i.e. implicitly strong by default). We fix |
12071 | // this by pretending that the unqualified type was actually |
12072 | // qualified __strong. |
12073 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
12074 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
12075 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
12076 | |
12077 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
12078 | return {}; |
12079 | |
12080 | if (GC_L == Qualifiers::Strong) |
12081 | return LHS; |
12082 | if (GC_R == Qualifiers::Strong) |
12083 | return RHS; |
12084 | return {}; |
12085 | } |
12086 | |
12087 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
12088 | QualType LHSBaseQT = LHS->castAs<ObjCObjectPointerType>()->getPointeeType(); |
12089 | QualType RHSBaseQT = RHS->castAs<ObjCObjectPointerType>()->getPointeeType(); |
12090 | QualType ResQT = mergeObjCGCQualifiers(LHS: LHSBaseQT, RHS: RHSBaseQT); |
12091 | if (ResQT == LHSBaseQT) |
12092 | return LHS; |
12093 | if (ResQT == RHSBaseQT) |
12094 | return RHS; |
12095 | } |
12096 | return {}; |
12097 | } |
12098 | |
12099 | //===----------------------------------------------------------------------===// |
12100 | // Integer Predicates |
12101 | //===----------------------------------------------------------------------===// |
12102 | |
12103 | unsigned ASTContext::getIntWidth(QualType T) const { |
12104 | if (const auto *ET = T->getAs<EnumType>()) |
12105 | T = ET->getDecl()->getIntegerType(); |
12106 | if (T->isBooleanType()) |
12107 | return 1; |
12108 | if (const auto *EIT = T->getAs<BitIntType>()) |
12109 | return EIT->getNumBits(); |
12110 | // For builtin types, just use the standard type sizing method |
12111 | return (unsigned)getTypeSize(T); |
12112 | } |
12113 | |
12114 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
12115 | assert((T->hasIntegerRepresentation() || T->isEnumeralType() || |
12116 | T->isFixedPointType()) && |
12117 | "Unexpected type"); |
12118 | |
12119 | // Turn <4 x signed int> -> <4 x unsigned int> |
12120 | if (const auto *VTy = T->getAs<VectorType>()) |
12121 | return getVectorType(vecType: getCorrespondingUnsignedType(T: VTy->getElementType()), |
12122 | NumElts: VTy->getNumElements(), VecKind: VTy->getVectorKind()); |
12123 | |
12124 | // For _BitInt, return an unsigned _BitInt with same width. |
12125 | if (const auto *EITy = T->getAs<BitIntType>()) |
12126 | return getBitIntType(/*Unsigned=*/IsUnsigned: true, NumBits: EITy->getNumBits()); |
12127 | |
12128 | // For enums, get the underlying integer type of the enum, and let the general |
12129 | // integer type signchanging code handle it. |
12130 | if (const auto *ETy = T->getAs<EnumType>()) |
12131 | T = ETy->getDecl()->getIntegerType(); |
12132 | |
12133 | switch (T->castAs<BuiltinType>()->getKind()) { |
12134 | case BuiltinType::Char_U: |
12135 | // Plain `char` is mapped to `unsigned char` even if it's already unsigned |
12136 | case BuiltinType::Char_S: |
12137 | case BuiltinType::SChar: |
12138 | case BuiltinType::Char8: |
12139 | return UnsignedCharTy; |
12140 | case BuiltinType::Short: |
12141 | return UnsignedShortTy; |
12142 | case BuiltinType::Int: |
12143 | return UnsignedIntTy; |
12144 | case BuiltinType::Long: |
12145 | return UnsignedLongTy; |
12146 | case BuiltinType::LongLong: |
12147 | return UnsignedLongLongTy; |
12148 | case BuiltinType::Int128: |
12149 | return UnsignedInt128Ty; |
12150 | // wchar_t is special. It is either signed or not, but when it's signed, |
12151 | // there's no matching "unsigned wchar_t". Therefore we return the unsigned |
12152 | // version of its underlying type instead. |
12153 | case BuiltinType::WChar_S: |
12154 | return getUnsignedWCharType(); |
12155 | |
12156 | case BuiltinType::ShortAccum: |
12157 | return UnsignedShortAccumTy; |
12158 | case BuiltinType::Accum: |
12159 | return UnsignedAccumTy; |
12160 | case BuiltinType::LongAccum: |
12161 | return UnsignedLongAccumTy; |
12162 | case BuiltinType::SatShortAccum: |
12163 | return SatUnsignedShortAccumTy; |
12164 | case BuiltinType::SatAccum: |
12165 | return SatUnsignedAccumTy; |
12166 | case BuiltinType::SatLongAccum: |
12167 | return SatUnsignedLongAccumTy; |
12168 | case BuiltinType::ShortFract: |
12169 | return UnsignedShortFractTy; |
12170 | case BuiltinType::Fract: |
12171 | return UnsignedFractTy; |
12172 | case BuiltinType::LongFract: |
12173 | return UnsignedLongFractTy; |
12174 | case BuiltinType::SatShortFract: |
12175 | return SatUnsignedShortFractTy; |
12176 | case BuiltinType::SatFract: |
12177 | return SatUnsignedFractTy; |
12178 | case BuiltinType::SatLongFract: |
12179 | return SatUnsignedLongFractTy; |
12180 | default: |
12181 | assert((T->hasUnsignedIntegerRepresentation() || |
12182 | T->isUnsignedFixedPointType()) && |
12183 | "Unexpected signed integer or fixed point type"); |
12184 | return T; |
12185 | } |
12186 | } |
12187 | |
12188 | QualType ASTContext::getCorrespondingSignedType(QualType T) const { |
12189 | assert((T->hasIntegerRepresentation() || T->isEnumeralType() || |
12190 | T->isFixedPointType()) && |
12191 | "Unexpected type"); |
12192 | |
12193 | // Turn <4 x unsigned int> -> <4 x signed int> |
12194 | if (const auto *VTy = T->getAs<VectorType>()) |
12195 | return getVectorType(vecType: getCorrespondingSignedType(T: VTy->getElementType()), |
12196 | NumElts: VTy->getNumElements(), VecKind: VTy->getVectorKind()); |
12197 | |
12198 | // For _BitInt, return a signed _BitInt with same width. |
12199 | if (const auto *EITy = T->getAs<BitIntType>()) |
12200 | return getBitIntType(/*Unsigned=*/IsUnsigned: false, NumBits: EITy->getNumBits()); |
12201 | |
12202 | // For enums, get the underlying integer type of the enum, and let the general |
12203 | // integer type signchanging code handle it. |
12204 | if (const auto *ETy = T->getAs<EnumType>()) |
12205 | T = ETy->getDecl()->getIntegerType(); |
12206 | |
12207 | switch (T->castAs<BuiltinType>()->getKind()) { |
12208 | case BuiltinType::Char_S: |
12209 | // Plain `char` is mapped to `signed char` even if it's already signed |
12210 | case BuiltinType::Char_U: |
12211 | case BuiltinType::UChar: |
12212 | case BuiltinType::Char8: |
12213 | return SignedCharTy; |
12214 | case BuiltinType::UShort: |
12215 | return ShortTy; |
12216 | case BuiltinType::UInt: |
12217 | return IntTy; |
12218 | case BuiltinType::ULong: |
12219 | return LongTy; |
12220 | case BuiltinType::ULongLong: |
12221 | return LongLongTy; |
12222 | case BuiltinType::UInt128: |
12223 | return Int128Ty; |
12224 | // wchar_t is special. It is either unsigned or not, but when it's unsigned, |
12225 | // there's no matching "signed wchar_t". Therefore we return the signed |
12226 | // version of its underlying type instead. |
12227 | case BuiltinType::WChar_U: |
12228 | return getSignedWCharType(); |
12229 | |
12230 | case BuiltinType::UShortAccum: |
12231 | return ShortAccumTy; |
12232 | case BuiltinType::UAccum: |
12233 | return AccumTy; |
12234 | case BuiltinType::ULongAccum: |
12235 | return LongAccumTy; |
12236 | case BuiltinType::SatUShortAccum: |
12237 | return SatShortAccumTy; |
12238 | case BuiltinType::SatUAccum: |
12239 | return SatAccumTy; |
12240 | case BuiltinType::SatULongAccum: |
12241 | return SatLongAccumTy; |
12242 | case BuiltinType::UShortFract: |
12243 | return ShortFractTy; |
12244 | case BuiltinType::UFract: |
12245 | return FractTy; |
12246 | case BuiltinType::ULongFract: |
12247 | return LongFractTy; |
12248 | case BuiltinType::SatUShortFract: |
12249 | return SatShortFractTy; |
12250 | case BuiltinType::SatUFract: |
12251 | return SatFractTy; |
12252 | case BuiltinType::SatULongFract: |
12253 | return SatLongFractTy; |
12254 | default: |
12255 | assert( |
12256 | (T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) && |
12257 | "Unexpected signed integer or fixed point type"); |
12258 | return T; |
12259 | } |
12260 | } |
12261 | |
12262 | ASTMutationListener::~ASTMutationListener() = default; |
12263 | |
12264 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, |
12265 | QualType ReturnType) {} |
12266 | |
12267 | //===----------------------------------------------------------------------===// |
12268 | // Builtin Type Computation |
12269 | //===----------------------------------------------------------------------===// |
12270 | |
12271 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
12272 | /// pointer over the consumed characters. This returns the resultant type. If |
12273 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
12274 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
12275 | /// a vector of "i*". |
12276 | /// |
12277 | /// RequiresICE is filled in on return to indicate whether the value is required |
12278 | /// to be an Integer Constant Expression. |
12279 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
12280 | ASTContext::GetBuiltinTypeError &Error, |
12281 | bool &RequiresICE, |
12282 | bool AllowTypeModifiers) { |
12283 | // Modifiers. |
12284 | int HowLong = 0; |
12285 | bool Signed = false, Unsigned = false; |
12286 | RequiresICE = false; |
12287 | |
12288 | // Read the prefixed modifiers first. |
12289 | bool Done = false; |
12290 | #ifndef NDEBUG |
12291 | bool IsSpecial = false; |
12292 | #endif |
12293 | while (!Done) { |
12294 | switch (*Str++) { |
12295 | default: Done = true; --Str; break; |
12296 | case 'I': |
12297 | RequiresICE = true; |
12298 | break; |
12299 | case 'S': |
12300 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
12301 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
12302 | Signed = true; |
12303 | break; |
12304 | case 'U': |
12305 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
12306 | assert(!Unsigned && "Can't use 'U' modifier multiple times!"); |
12307 | Unsigned = true; |
12308 | break; |
12309 | case 'L': |
12310 | assert(!IsSpecial && "Can't use 'L' with 'W', 'N', 'Z' or 'O' modifiers"); |
12311 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
12312 | ++HowLong; |
12313 | break; |
12314 | case 'N': |
12315 | // 'N' behaves like 'L' for all non LP64 targets and 'int' otherwise. |
12316 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"); |
12317 | assert(HowLong == 0 && "Can't use both 'L' and 'N' modifiers!"); |
12318 | #ifndef NDEBUG |
12319 | IsSpecial = true; |
12320 | #endif |
12321 | if (Context.getTargetInfo().getLongWidth() == 32) |
12322 | ++HowLong; |
12323 | break; |
12324 | case 'W': |
12325 | // This modifier represents int64 type. |
12326 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"); |
12327 | assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!"); |
12328 | #ifndef NDEBUG |
12329 | IsSpecial = true; |
12330 | #endif |
12331 | switch (Context.getTargetInfo().getInt64Type()) { |
12332 | default: |
12333 | llvm_unreachable("Unexpected integer type"); |
12334 | case TargetInfo::SignedLong: |
12335 | HowLong = 1; |
12336 | break; |
12337 | case TargetInfo::SignedLongLong: |
12338 | HowLong = 2; |
12339 | break; |
12340 | } |
12341 | break; |
12342 | case 'Z': |
12343 | // This modifier represents int32 type. |
12344 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"); |
12345 | assert(HowLong == 0 && "Can't use both 'L' and 'Z' modifiers!"); |
12346 | #ifndef NDEBUG |
12347 | IsSpecial = true; |
12348 | #endif |
12349 | switch (Context.getTargetInfo().getIntTypeByWidth(BitWidth: 32, IsSigned: true)) { |
12350 | default: |
12351 | llvm_unreachable("Unexpected integer type"); |
12352 | case TargetInfo::SignedInt: |
12353 | HowLong = 0; |
12354 | break; |
12355 | case TargetInfo::SignedLong: |
12356 | HowLong = 1; |
12357 | break; |
12358 | case TargetInfo::SignedLongLong: |
12359 | HowLong = 2; |
12360 | break; |
12361 | } |
12362 | break; |
12363 | case 'O': |
12364 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"); |
12365 | assert(HowLong == 0 && "Can't use both 'L' and 'O' modifiers!"); |
12366 | #ifndef NDEBUG |
12367 | IsSpecial = true; |
12368 | #endif |
12369 | if (Context.getLangOpts().OpenCL) |
12370 | HowLong = 1; |
12371 | else |
12372 | HowLong = 2; |
12373 | break; |
12374 | } |
12375 | } |
12376 | |
12377 | QualType Type; |
12378 | |
12379 | // Read the base type. |
12380 | switch (*Str++) { |
12381 | default: llvm_unreachable("Unknown builtin type letter!"); |
12382 | case 'x': |
12383 | assert(HowLong == 0 && !Signed && !Unsigned && |
12384 | "Bad modifiers used with 'x'!"); |
12385 | Type = Context.Float16Ty; |
12386 | break; |
12387 | case 'y': |
12388 | assert(HowLong == 0 && !Signed && !Unsigned && |
12389 | "Bad modifiers used with 'y'!"); |
12390 | Type = Context.BFloat16Ty; |
12391 | break; |
12392 | case 'v': |
12393 | assert(HowLong == 0 && !Signed && !Unsigned && |
12394 | "Bad modifiers used with 'v'!"); |
12395 | Type = Context.VoidTy; |
12396 | break; |
12397 | case 'h': |
12398 | assert(HowLong == 0 && !Signed && !Unsigned && |
12399 | "Bad modifiers used with 'h'!"); |
12400 | Type = Context.HalfTy; |
12401 | break; |
12402 | case 'f': |
12403 | assert(HowLong == 0 && !Signed && !Unsigned && |
12404 | "Bad modifiers used with 'f'!"); |
12405 | Type = Context.FloatTy; |
12406 | break; |
12407 | case 'd': |
12408 | assert(HowLong < 3 && !Signed && !Unsigned && |
12409 | "Bad modifiers used with 'd'!"); |
12410 | if (HowLong == 1) |
12411 | Type = Context.LongDoubleTy; |
12412 | else if (HowLong == 2) |
12413 | Type = Context.Float128Ty; |
12414 | else |
12415 | Type = Context.DoubleTy; |
12416 | break; |
12417 | case 's': |
12418 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
12419 | if (Unsigned) |
12420 | Type = Context.UnsignedShortTy; |
12421 | else |
12422 | Type = Context.ShortTy; |
12423 | break; |
12424 | case 'i': |
12425 | if (HowLong == 3) |
12426 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
12427 | else if (HowLong == 2) |
12428 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
12429 | else if (HowLong == 1) |
12430 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
12431 | else |
12432 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
12433 | break; |
12434 | case 'c': |
12435 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
12436 | if (Signed) |
12437 | Type = Context.SignedCharTy; |
12438 | else if (Unsigned) |
12439 | Type = Context.UnsignedCharTy; |
12440 | else |
12441 | Type = Context.CharTy; |
12442 | break; |
12443 | case 'b': // boolean |
12444 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
12445 | Type = Context.BoolTy; |
12446 | break; |
12447 | case 'z': // size_t. |
12448 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
12449 | Type = Context.getSizeType(); |
12450 | break; |
12451 | case 'w': // wchar_t. |
12452 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'w'!"); |
12453 | Type = Context.getWideCharType(); |
12454 | break; |
12455 | case 'F': |
12456 | Type = Context.getCFConstantStringType(); |
12457 | break; |
12458 | case 'G': |
12459 | Type = Context.getObjCIdType(); |
12460 | break; |
12461 | case 'H': |
12462 | Type = Context.getObjCSelType(); |
12463 | break; |
12464 | case 'M': |
12465 | Type = Context.getObjCSuperType(); |
12466 | break; |
12467 | case 'a': |
12468 | Type = Context.getBuiltinVaListType(); |
12469 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
12470 | break; |
12471 | case 'A': |
12472 | // This is a "reference" to a va_list; however, what exactly |
12473 | // this means depends on how va_list is defined. There are two |
12474 | // different kinds of va_list: ones passed by value, and ones |
12475 | // passed by reference. An example of a by-value va_list is |
12476 | // x86, where va_list is a char*. An example of by-ref va_list |
12477 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
12478 | // we want this argument to be a char*&; for x86-64, we want |
12479 | // it to be a __va_list_tag*. |
12480 | Type = Context.getBuiltinVaListType(); |
12481 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
12482 | if (Type->isArrayType()) |
12483 | Type = Context.getArrayDecayedType(Ty: Type); |
12484 | else |
12485 | Type = Context.getLValueReferenceType(T: Type); |
12486 | break; |
12487 | case 'q': { |
12488 | char *End; |
12489 | unsigned NumElements = strtoul(nptr: Str, endptr: &End, base: 10); |
12490 | assert(End != Str && "Missing vector size"); |
12491 | Str = End; |
12492 | |
12493 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
12494 | RequiresICE, AllowTypeModifiers: false); |
12495 | assert(!RequiresICE && "Can't require vector ICE"); |
12496 | |
12497 | Type = Context.getScalableVectorType(EltTy: ElementType, NumElts: NumElements); |
12498 | break; |
12499 | } |
12500 | case 'Q': { |
12501 | switch (*Str++) { |
12502 | case 'a': { |
12503 | Type = Context.SveCountTy; |
12504 | break; |
12505 | } |
12506 | case 'b': { |
12507 | Type = Context.AMDGPUBufferRsrcTy; |
12508 | break; |
12509 | } |
12510 | default: |
12511 | llvm_unreachable("Unexpected target builtin type"); |
12512 | } |
12513 | break; |
12514 | } |
12515 | case 'V': { |
12516 | char *End; |
12517 | unsigned NumElements = strtoul(nptr: Str, endptr: &End, base: 10); |
12518 | assert(End != Str && "Missing vector size"); |
12519 | Str = End; |
12520 | |
12521 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
12522 | RequiresICE, AllowTypeModifiers: false); |
12523 | assert(!RequiresICE && "Can't require vector ICE"); |
12524 | |
12525 | // TODO: No way to make AltiVec vectors in builtins yet. |
12526 | Type = Context.getVectorType(vecType: ElementType, NumElts: NumElements, VecKind: VectorKind::Generic); |
12527 | break; |
12528 | } |
12529 | case 'E': { |
12530 | char *End; |
12531 | |
12532 | unsigned NumElements = strtoul(nptr: Str, endptr: &End, base: 10); |
12533 | assert(End != Str && "Missing vector size"); |
12534 | |
12535 | Str = End; |
12536 | |
12537 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
12538 | AllowTypeModifiers: false); |
12539 | Type = Context.getExtVectorType(vecType: ElementType, NumElts: NumElements); |
12540 | break; |
12541 | } |
12542 | case 'X': { |
12543 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
12544 | AllowTypeModifiers: false); |
12545 | assert(!RequiresICE && "Can't require complex ICE"); |
12546 | Type = Context.getComplexType(T: ElementType); |
12547 | break; |
12548 | } |
12549 | case 'Y': |
12550 | Type = Context.getPointerDiffType(); |
12551 | break; |
12552 | case 'P': |
12553 | Type = Context.getFILEType(); |
12554 | if (Type.isNull()) { |
12555 | Error = ASTContext::GE_Missing_stdio; |
12556 | return {}; |
12557 | } |
12558 | break; |
12559 | case 'J': |
12560 | if (Signed) |
12561 | Type = Context.getsigjmp_bufType(); |
12562 | else |
12563 | Type = Context.getjmp_bufType(); |
12564 | |
12565 | if (Type.isNull()) { |
12566 | Error = ASTContext::GE_Missing_setjmp; |
12567 | return {}; |
12568 | } |
12569 | break; |
12570 | case 'K': |
12571 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
12572 | Type = Context.getucontext_tType(); |
12573 | |
12574 | if (Type.isNull()) { |
12575 | Error = ASTContext::GE_Missing_ucontext; |
12576 | return {}; |
12577 | } |
12578 | break; |
12579 | case 'p': |
12580 | Type = Context.getProcessIDType(); |
12581 | break; |
12582 | case 'm': |
12583 | Type = Context.MFloat8Ty; |
12584 | break; |
12585 | } |
12586 | |
12587 | // If there are modifiers and if we're allowed to parse them, go for it. |
12588 | Done = !AllowTypeModifiers; |
12589 | while (!Done) { |
12590 | switch (char c = *Str++) { |
12591 | default: Done = true; --Str; break; |
12592 | case '*': |
12593 | case '&': { |
12594 | // Both pointers and references can have their pointee types |
12595 | // qualified with an address space. |
12596 | char *End; |
12597 | unsigned AddrSpace = strtoul(nptr: Str, endptr: &End, base: 10); |
12598 | if (End != Str) { |
12599 | // Note AddrSpace == 0 is not the same as an unspecified address space. |
12600 | Type = Context.getAddrSpaceQualType( |
12601 | T: Type, |
12602 | AddressSpace: Context.getLangASForBuiltinAddressSpace(AS: AddrSpace)); |
12603 | Str = End; |
12604 | } |
12605 | if (c == '*') |
12606 | Type = Context.getPointerType(T: Type); |
12607 | else |
12608 | Type = Context.getLValueReferenceType(T: Type); |
12609 | break; |
12610 | } |
12611 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
12612 | case 'C': |
12613 | Type = Type.withConst(); |
12614 | break; |
12615 | case 'D': |
12616 | Type = Context.getVolatileType(T: Type); |
12617 | break; |
12618 | case 'R': |
12619 | Type = Type.withRestrict(); |
12620 | break; |
12621 | } |
12622 | } |
12623 | |
12624 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
12625 | "Integer constant 'I' type must be an integer"); |
12626 | |
12627 | return Type; |
12628 | } |
12629 | |
12630 | // On some targets such as PowerPC, some of the builtins are defined with custom |
12631 | // type descriptors for target-dependent types. These descriptors are decoded in |
12632 | // other functions, but it may be useful to be able to fall back to default |
12633 | // descriptor decoding to define builtins mixing target-dependent and target- |
12634 | // independent types. This function allows decoding one type descriptor with |
12635 | // default decoding. |
12636 | QualType ASTContext::DecodeTypeStr(const char *&Str, const ASTContext &Context, |
12637 | GetBuiltinTypeError &Error, bool &RequireICE, |
12638 | bool AllowTypeModifiers) const { |
12639 | return DecodeTypeFromStr(Str, Context, Error, RequiresICE&: RequireICE, AllowTypeModifiers); |
12640 | } |
12641 | |
12642 | /// GetBuiltinType - Return the type for the specified builtin. |
12643 | QualType ASTContext::GetBuiltinType(unsigned Id, |
12644 | GetBuiltinTypeError &Error, |
12645 | unsigned *IntegerConstantArgs) const { |
12646 | const char *TypeStr = BuiltinInfo.getTypeString(ID: Id); |
12647 | if (TypeStr[0] == '\0') { |
12648 | Error = GE_Missing_type; |
12649 | return {}; |
12650 | } |
12651 | |
12652 | SmallVector<QualType, 8> ArgTypes; |
12653 | |
12654 | bool RequiresICE = false; |
12655 | Error = GE_None; |
12656 | QualType ResType = DecodeTypeFromStr(Str&: TypeStr, Context: *this, Error, |
12657 | RequiresICE, AllowTypeModifiers: true); |
12658 | if (Error != GE_None) |
12659 | return {}; |
12660 | |
12661 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
12662 | |
12663 | while (TypeStr[0] && TypeStr[0] != '.') { |
12664 | QualType Ty = DecodeTypeFromStr(Str&: TypeStr, Context: *this, Error, RequiresICE, AllowTypeModifiers: true); |
12665 | if (Error != GE_None) |
12666 | return {}; |
12667 | |
12668 | // If this argument is required to be an IntegerConstantExpression and the |
12669 | // caller cares, fill in the bitmask we return. |
12670 | if (RequiresICE && IntegerConstantArgs) |
12671 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
12672 | |
12673 | // Do array -> pointer decay. The builtin should use the decayed type. |
12674 | if (Ty->isArrayType()) |
12675 | Ty = getArrayDecayedType(Ty); |
12676 | |
12677 | ArgTypes.push_back(Elt: Ty); |
12678 | } |
12679 | |
12680 | if (Id == Builtin::BI__GetExceptionInfo) |
12681 | return {}; |
12682 | |
12683 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
12684 | "'.' should only occur at end of builtin type list!"); |
12685 | |
12686 | bool Variadic = (TypeStr[0] == '.'); |
12687 | |
12688 | FunctionType::ExtInfo EI(getDefaultCallingConvention( |
12689 | IsVariadic: Variadic, /*IsCXXMethod=*/false, /*IsBuiltin=*/true)); |
12690 | if (BuiltinInfo.isNoReturn(ID: Id)) EI = EI.withNoReturn(noReturn: true); |
12691 | |
12692 | |
12693 | // We really shouldn't be making a no-proto type here. |
12694 | if (ArgTypes.empty() && Variadic && !getLangOpts().requiresStrictPrototypes()) |
12695 | return getFunctionNoProtoType(ResultTy: ResType, Info: EI); |
12696 | |
12697 | FunctionProtoType::ExtProtoInfo EPI; |
12698 | EPI.ExtInfo = EI; |
12699 | EPI.Variadic = Variadic; |
12700 | if (getLangOpts().CPlusPlus && BuiltinInfo.isNoThrow(ID: Id)) |
12701 | EPI.ExceptionSpec.Type = |
12702 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
12703 | |
12704 | return getFunctionType(ResultTy: ResType, Args: ArgTypes, EPI); |
12705 | } |
12706 | |
12707 | static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, |
12708 | const FunctionDecl *FD) { |
12709 | if (!FD->isExternallyVisible()) |
12710 | return GVA_Internal; |
12711 | |
12712 | // Non-user-provided functions get emitted as weak definitions with every |
12713 | // use, no matter whether they've been explicitly instantiated etc. |
12714 | if (!FD->isUserProvided()) |
12715 | return GVA_DiscardableODR; |
12716 | |
12717 | GVALinkage External; |
12718 | switch (FD->getTemplateSpecializationKind()) { |
12719 | case TSK_Undeclared: |
12720 | case TSK_ExplicitSpecialization: |
12721 | External = GVA_StrongExternal; |
12722 | break; |
12723 | |
12724 | case TSK_ExplicitInstantiationDefinition: |
12725 | return GVA_StrongODR; |
12726 | |
12727 | // C++11 [temp.explicit]p10: |
12728 | // [ Note: The intent is that an inline function that is the subject of |
12729 | // an explicit instantiation declaration will still be implicitly |
12730 | // instantiated when used so that the body can be considered for |
12731 | // inlining, but that no out-of-line copy of the inline function would be |
12732 | // generated in the translation unit. -- end note ] |
12733 | case TSK_ExplicitInstantiationDeclaration: |
12734 | return GVA_AvailableExternally; |
12735 | |
12736 | case TSK_ImplicitInstantiation: |
12737 | External = GVA_DiscardableODR; |
12738 | break; |
12739 | } |
12740 | |
12741 | if (!FD->isInlined()) |
12742 | return External; |
12743 | |
12744 | if ((!Context.getLangOpts().CPlusPlus && |
12745 | !Context.getTargetInfo().getCXXABI().isMicrosoft() && |
12746 | !FD->hasAttr<DLLExportAttr>()) || |
12747 | FD->hasAttr<GNUInlineAttr>()) { |
12748 | // FIXME: This doesn't match gcc's behavior for dllexport inline functions. |
12749 | |
12750 | // GNU or C99 inline semantics. Determine whether this symbol should be |
12751 | // externally visible. |
12752 | if (FD->isInlineDefinitionExternallyVisible()) |
12753 | return External; |
12754 | |
12755 | // C99 inline semantics, where the symbol is not externally visible. |
12756 | return GVA_AvailableExternally; |
12757 | } |
12758 | |
12759 | // Functions specified with extern and inline in -fms-compatibility mode |
12760 | // forcibly get emitted. While the body of the function cannot be later |
12761 | // replaced, the function definition cannot be discarded. |
12762 | if (FD->isMSExternInline()) |
12763 | return GVA_StrongODR; |
12764 | |
12765 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
12766 | isa<CXXConstructorDecl>(Val: FD) && |
12767 | cast<CXXConstructorDecl>(Val: FD)->isInheritingConstructor()) |
12768 | // Our approach to inheriting constructors is fundamentally different from |
12769 | // that used by the MS ABI, so keep our inheriting constructor thunks |
12770 | // internal rather than trying to pick an unambiguous mangling for them. |
12771 | return GVA_Internal; |
12772 | |
12773 | return GVA_DiscardableODR; |
12774 | } |
12775 | |
12776 | static GVALinkage adjustGVALinkageForAttributes(const ASTContext &Context, |
12777 | const Decl *D, GVALinkage L) { |
12778 | // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx |
12779 | // dllexport/dllimport on inline functions. |
12780 | if (D->hasAttr<DLLImportAttr>()) { |
12781 | if (L == GVA_DiscardableODR || L == GVA_StrongODR) |
12782 | return GVA_AvailableExternally; |
12783 | } else if (D->hasAttr<DLLExportAttr>()) { |
12784 | if (L == GVA_DiscardableODR) |
12785 | return GVA_StrongODR; |
12786 | } else if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice) { |
12787 | // Device-side functions with __global__ attribute must always be |
12788 | // visible externally so they can be launched from host. |
12789 | if (D->hasAttr<CUDAGlobalAttr>() && |
12790 | (L == GVA_DiscardableODR || L == GVA_Internal)) |
12791 | return GVA_StrongODR; |
12792 | // Single source offloading languages like CUDA/HIP need to be able to |
12793 | // access static device variables from host code of the same compilation |
12794 | // unit. This is done by externalizing the static variable with a shared |
12795 | // name between the host and device compilation which is the same for the |
12796 | // same compilation unit whereas different among different compilation |
12797 | // units. |
12798 | if (Context.shouldExternalize(D)) |
12799 | return GVA_StrongExternal; |
12800 | } |
12801 | return L; |
12802 | } |
12803 | |
12804 | /// Adjust the GVALinkage for a declaration based on what an external AST source |
12805 | /// knows about whether there can be other definitions of this declaration. |
12806 | static GVALinkage |
12807 | adjustGVALinkageForExternalDefinitionKind(const ASTContext &Ctx, const Decl *D, |
12808 | GVALinkage L) { |
12809 | ExternalASTSource *Source = Ctx.getExternalSource(); |
12810 | if (!Source) |
12811 | return L; |
12812 | |
12813 | switch (Source->hasExternalDefinitions(D)) { |
12814 | case ExternalASTSource::EK_Never: |
12815 | // Other translation units rely on us to provide the definition. |
12816 | if (L == GVA_DiscardableODR) |
12817 | return GVA_StrongODR; |
12818 | break; |
12819 | |
12820 | case ExternalASTSource::EK_Always: |
12821 | return GVA_AvailableExternally; |
12822 | |
12823 | case ExternalASTSource::EK_ReplyHazy: |
12824 | break; |
12825 | } |
12826 | return L; |
12827 | } |
12828 | |
12829 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { |
12830 | return adjustGVALinkageForExternalDefinitionKind(*this, FD, |
12831 | adjustGVALinkageForAttributes(*this, FD, |
12832 | basicGVALinkageForFunction(Context: *this, FD))); |
12833 | } |
12834 | |
12835 | static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, |
12836 | const VarDecl *VD) { |
12837 | // As an extension for interactive REPLs, make sure constant variables are |
12838 | // only emitted once instead of LinkageComputer::getLVForNamespaceScopeDecl |
12839 | // marking them as internal. |
12840 | if (Context.getLangOpts().CPlusPlus && |
12841 | Context.getLangOpts().IncrementalExtensions && |
12842 | VD->getType().isConstQualified() && |
12843 | !VD->getType().isVolatileQualified() && !VD->isInline() && |
12844 | !isa<VarTemplateSpecializationDecl>(Val: VD) && !VD->getDescribedVarTemplate()) |
12845 | return GVA_DiscardableODR; |
12846 | |
12847 | if (!VD->isExternallyVisible()) |
12848 | return GVA_Internal; |
12849 | |
12850 | if (VD->isStaticLocal()) { |
12851 | const DeclContext *LexicalContext = VD->getParentFunctionOrMethod(); |
12852 | while (LexicalContext && !isa<FunctionDecl>(Val: LexicalContext)) |
12853 | LexicalContext = LexicalContext->getLexicalParent(); |
12854 | |
12855 | // ObjC Blocks can create local variables that don't have a FunctionDecl |
12856 | // LexicalContext. |
12857 | if (!LexicalContext) |
12858 | return GVA_DiscardableODR; |
12859 | |
12860 | // Otherwise, let the static local variable inherit its linkage from the |
12861 | // nearest enclosing function. |
12862 | auto StaticLocalLinkage = |
12863 | Context.GetGVALinkageForFunction(FD: cast<FunctionDecl>(Val: LexicalContext)); |
12864 | |
12865 | // Itanium ABI 5.2.2: "Each COMDAT group [for a static local variable] must |
12866 | // be emitted in any object with references to the symbol for the object it |
12867 | // contains, whether inline or out-of-line." |
12868 | // Similar behavior is observed with MSVC. An alternative ABI could use |
12869 | // StrongODR/AvailableExternally to match the function, but none are |
12870 | // known/supported currently. |
12871 | if (StaticLocalLinkage == GVA_StrongODR || |
12872 | StaticLocalLinkage == GVA_AvailableExternally) |
12873 | return GVA_DiscardableODR; |
12874 | return StaticLocalLinkage; |
12875 | } |
12876 | |
12877 | // MSVC treats in-class initialized static data members as definitions. |
12878 | // By giving them non-strong linkage, out-of-line definitions won't |
12879 | // cause link errors. |
12880 | if (Context.isMSStaticDataMemberInlineDefinition(VD)) |
12881 | return GVA_DiscardableODR; |
12882 | |
12883 | // Most non-template variables have strong linkage; inline variables are |
12884 | // linkonce_odr or (occasionally, for compatibility) weak_odr. |
12885 | GVALinkage StrongLinkage; |
12886 | switch (Context.getInlineVariableDefinitionKind(VD)) { |
12887 | case ASTContext::InlineVariableDefinitionKind::None: |
12888 | StrongLinkage = GVA_StrongExternal; |
12889 | break; |
12890 | case ASTContext::InlineVariableDefinitionKind::Weak: |
12891 | case ASTContext::InlineVariableDefinitionKind::WeakUnknown: |
12892 | StrongLinkage = GVA_DiscardableODR; |
12893 | break; |
12894 | case ASTContext::InlineVariableDefinitionKind::Strong: |
12895 | StrongLinkage = GVA_StrongODR; |
12896 | break; |
12897 | } |
12898 | |
12899 | switch (VD->getTemplateSpecializationKind()) { |
12900 | case TSK_Undeclared: |
12901 | return StrongLinkage; |
12902 | |
12903 | case TSK_ExplicitSpecialization: |
12904 | return Context.getTargetInfo().getCXXABI().isMicrosoft() && |
12905 | VD->isStaticDataMember() |
12906 | ? GVA_StrongODR |
12907 | : StrongLinkage; |
12908 | |
12909 | case TSK_ExplicitInstantiationDefinition: |
12910 | return GVA_StrongODR; |
12911 | |
12912 | case TSK_ExplicitInstantiationDeclaration: |
12913 | return GVA_AvailableExternally; |
12914 | |
12915 | case TSK_ImplicitInstantiation: |
12916 | return GVA_DiscardableODR; |
12917 | } |
12918 | |
12919 | llvm_unreachable("Invalid Linkage!"); |
12920 | } |
12921 | |
12922 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) const { |
12923 | return adjustGVALinkageForExternalDefinitionKind(*this, VD, |
12924 | adjustGVALinkageForAttributes(*this, VD, |
12925 | basicGVALinkageForVariable(Context: *this, VD))); |
12926 | } |
12927 | |
12928 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
12929 | if (const auto *VD = dyn_cast<VarDecl>(Val: D)) { |
12930 | if (!VD->isFileVarDecl()) |
12931 | return false; |
12932 | // Global named register variables (GNU extension) are never emitted. |
12933 | if (VD->getStorageClass() == SC_Register) |
12934 | return false; |
12935 | if (VD->getDescribedVarTemplate() || |
12936 | isa<VarTemplatePartialSpecializationDecl>(Val: VD)) |
12937 | return false; |
12938 | } else if (const auto *FD = dyn_cast<FunctionDecl>(Val: D)) { |
12939 | // We never need to emit an uninstantiated function template. |
12940 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
12941 | return false; |
12942 | } else if (isa<PragmaCommentDecl>(Val: D)) |
12943 | return true; |
12944 | else if (isa<PragmaDetectMismatchDecl>(Val: D)) |
12945 | return true; |
12946 | else if (isa<OMPRequiresDecl>(Val: D)) |
12947 | return true; |
12948 | else if (isa<OMPThreadPrivateDecl>(Val: D)) |
12949 | return !D->getDeclContext()->isDependentContext(); |
12950 | else if (isa<OMPAllocateDecl>(Val: D)) |
12951 | return !D->getDeclContext()->isDependentContext(); |
12952 | else if (isa<OMPDeclareReductionDecl>(Val: D) || isa<OMPDeclareMapperDecl>(Val: D)) |
12953 | return !D->getDeclContext()->isDependentContext(); |
12954 | else if (isa<ImportDecl>(Val: D)) |
12955 | return true; |
12956 | else |
12957 | return false; |
12958 | |
12959 | // If this is a member of a class template, we do not need to emit it. |
12960 | if (D->getDeclContext()->isDependentContext()) |
12961 | return false; |
12962 | |
12963 | // Weak references don't produce any output by themselves. |
12964 | if (D->hasAttr<WeakRefAttr>()) |
12965 | return false; |
12966 | |
12967 | // Aliases and used decls are required. |
12968 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
12969 | return true; |
12970 | |
12971 | if (const auto *FD = dyn_cast<FunctionDecl>(Val: D)) { |
12972 | // Forward declarations aren't required. |
12973 | if (!FD->doesThisDeclarationHaveABody()) |
12974 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
12975 | |
12976 | // Function definitions with the sycl_kernel_entry_point attribute are |
12977 | // required during device compilation so that SYCL kernel caller offload |
12978 | // entry points are emitted. |
12979 | if (LangOpts.SYCLIsDevice && FD->hasAttr<SYCLKernelEntryPointAttr>()) |
12980 | return true; |
12981 | |
12982 | // FIXME: Functions declared with SYCL_EXTERNAL are required during |
12983 | // device compilation. |
12984 | |
12985 | // Constructors and destructors are required. |
12986 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
12987 | return true; |
12988 | |
12989 | // The key function for a class is required. This rule only comes |
12990 | // into play when inline functions can be key functions, though. |
12991 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
12992 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FD)) { |
12993 | const CXXRecordDecl *RD = MD->getParent(); |
12994 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
12995 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
12996 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
12997 | return true; |
12998 | } |
12999 | } |
13000 | } |
13001 | |
13002 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
13003 | |
13004 | // static, static inline, always_inline, and extern inline functions can |
13005 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
13006 | // Implicit template instantiations can also be deferred in C++. |
13007 | return !isDiscardableGVALinkage(L: Linkage); |
13008 | } |
13009 | |
13010 | const auto *VD = cast<VarDecl>(Val: D); |
13011 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
13012 | |
13013 | // If the decl is marked as `declare target to`, it should be emitted for the |
13014 | // host and for the device. |
13015 | if (LangOpts.OpenMP && |
13016 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) |
13017 | return true; |
13018 | |
13019 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly && |
13020 | !isMSStaticDataMemberInlineDefinition(VD)) |
13021 | return false; |
13022 | |
13023 | if (VD->shouldEmitInExternalSource()) |
13024 | return false; |
13025 | |
13026 | // Variables that can be needed in other TUs are required. |
13027 | auto Linkage = GetGVALinkageForVariable(VD); |
13028 | if (!isDiscardableGVALinkage(L: Linkage)) |
13029 | return true; |
13030 | |
13031 | // We never need to emit a variable that is available in another TU. |
13032 | if (Linkage == GVA_AvailableExternally) |
13033 | return false; |
13034 | |
13035 | // Variables that have destruction with side-effects are required. |
13036 | if (VD->needsDestruction(Ctx: *this)) |
13037 | return true; |
13038 | |
13039 | // Variables that have initialization with side-effects are required. |
13040 | if (VD->getInit() && VD->getInit()->HasSideEffects(Ctx: *this) && |
13041 | // We can get a value-dependent initializer during error recovery. |
13042 | (VD->getInit()->isValueDependent() || !VD->evaluateValue())) |
13043 | return true; |
13044 | |
13045 | // Likewise, variables with tuple-like bindings are required if their |
13046 | // bindings have side-effects. |
13047 | if (const auto *DD = dyn_cast<DecompositionDecl>(Val: VD)) { |
13048 | for (const auto *BD : DD->flat_bindings()) |
13049 | if (const auto *BindingVD = BD->getHoldingVar()) |
13050 | if (DeclMustBeEmitted(BindingVD)) |
13051 | return true; |
13052 | } |
13053 | |
13054 | return false; |
13055 | } |
13056 | |
13057 | void ASTContext::forEachMultiversionedFunctionVersion( |
13058 | const FunctionDecl *FD, |
13059 | llvm::function_ref<void(FunctionDecl *)> Pred) const { |
13060 | assert(FD->isMultiVersion() && "Only valid for multiversioned functions"); |
13061 | llvm::SmallDenseSet<const FunctionDecl*, 4> SeenDecls; |
13062 | FD = FD->getMostRecentDecl(); |
13063 | // FIXME: The order of traversal here matters and depends on the order of |
13064 | // lookup results, which happens to be (mostly) oldest-to-newest, but we |
13065 | // shouldn't rely on that. |
13066 | for (auto *CurDecl : |
13067 | FD->getDeclContext()->getRedeclContext()->lookup(FD->getDeclName())) { |
13068 | FunctionDecl *CurFD = CurDecl->getAsFunction()->getMostRecentDecl(); |
13069 | if (CurFD && hasSameType(CurFD->getType(), FD->getType()) && |
13070 | SeenDecls.insert(CurFD).second) { |
13071 | Pred(CurFD); |
13072 | } |
13073 | } |
13074 | } |
13075 | |
13076 | CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic, |
13077 | bool IsCXXMethod, |
13078 | bool IsBuiltin) const { |
13079 | // Pass through to the C++ ABI object |
13080 | if (IsCXXMethod) |
13081 | return ABI->getDefaultMethodCallConv(IsVariadic); |
13082 | |
13083 | // Builtins ignore user-specified default calling convention and remain the |
13084 | // Target's default calling convention. |
13085 | if (!IsBuiltin) { |
13086 | switch (LangOpts.getDefaultCallingConv()) { |
13087 | case LangOptions::DCC_None: |
13088 | break; |
13089 | case LangOptions::DCC_CDecl: |
13090 | return CC_C; |
13091 | case LangOptions::DCC_FastCall: |
13092 | if (getTargetInfo().hasFeature(Feature: "sse2") && !IsVariadic) |
13093 | return CC_X86FastCall; |
13094 | break; |
13095 | case LangOptions::DCC_StdCall: |
13096 | if (!IsVariadic) |
13097 | return CC_X86StdCall; |
13098 | break; |
13099 | case LangOptions::DCC_VectorCall: |
13100 | // __vectorcall cannot be applied to variadic functions. |
13101 | if (!IsVariadic) |
13102 | return CC_X86VectorCall; |
13103 | break; |
13104 | case LangOptions::DCC_RegCall: |
13105 | // __regcall cannot be applied to variadic functions. |
13106 | if (!IsVariadic) |
13107 | return CC_X86RegCall; |
13108 | break; |
13109 | case LangOptions::DCC_RtdCall: |
13110 | if (!IsVariadic) |
13111 | return CC_M68kRTD; |
13112 | break; |
13113 | } |
13114 | } |
13115 | return Target->getDefaultCallingConv(); |
13116 | } |
13117 | |
13118 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
13119 | // Pass through to the C++ ABI object |
13120 | return ABI->isNearlyEmpty(RD); |
13121 | } |
13122 | |
13123 | VTableContextBase *ASTContext::getVTableContext() { |
13124 | if (!VTContext) { |
13125 | auto ABI = Target->getCXXABI(); |
13126 | if (ABI.isMicrosoft()) |
13127 | VTContext.reset(new MicrosoftVTableContext(*this)); |
13128 | else { |
13129 | auto ComponentLayout = getLangOpts().RelativeCXXABIVTables |
13130 | ? ItaniumVTableContext::Relative |
13131 | : ItaniumVTableContext::Pointer; |
13132 | VTContext.reset(new ItaniumVTableContext(*this, ComponentLayout)); |
13133 | } |
13134 | } |
13135 | return VTContext.get(); |
13136 | } |
13137 | |
13138 | MangleContext *ASTContext::createMangleContext(const TargetInfo *T) { |
13139 | if (!T) |
13140 | T = Target; |
13141 | switch (T->getCXXABI().getKind()) { |
13142 | case TargetCXXABI::AppleARM64: |
13143 | case TargetCXXABI::Fuchsia: |
13144 | case TargetCXXABI::GenericAArch64: |
13145 | case TargetCXXABI::GenericItanium: |
13146 | case TargetCXXABI::GenericARM: |
13147 | case TargetCXXABI::GenericMIPS: |
13148 | case TargetCXXABI::iOS: |
13149 | case TargetCXXABI::WebAssembly: |
13150 | case TargetCXXABI::WatchOS: |
13151 | case TargetCXXABI::XL: |
13152 | return ItaniumMangleContext::create(Context&: *this, Diags&: getDiagnostics()); |
13153 | case TargetCXXABI::Microsoft: |
13154 | return MicrosoftMangleContext::create(Context&: *this, Diags&: getDiagnostics()); |
13155 | } |
13156 | llvm_unreachable("Unsupported ABI"); |
13157 | } |
13158 | |
13159 | MangleContext *ASTContext::createDeviceMangleContext(const TargetInfo &T) { |
13160 | assert(T.getCXXABI().getKind() != TargetCXXABI::Microsoft && |
13161 | "Device mangle context does not support Microsoft mangling."); |
13162 | switch (T.getCXXABI().getKind()) { |
13163 | case TargetCXXABI::AppleARM64: |
13164 | case TargetCXXABI::Fuchsia: |
13165 | case TargetCXXABI::GenericAArch64: |
13166 | case TargetCXXABI::GenericItanium: |
13167 | case TargetCXXABI::GenericARM: |
13168 | case TargetCXXABI::GenericMIPS: |
13169 | case TargetCXXABI::iOS: |
13170 | case TargetCXXABI::WebAssembly: |
13171 | case TargetCXXABI::WatchOS: |
13172 | case TargetCXXABI::XL: |
13173 | return ItaniumMangleContext::create( |
13174 | Context&: *this, Diags&: getDiagnostics(), |
13175 | Discriminator: [](ASTContext &, const NamedDecl *ND) -> UnsignedOrNone { |
13176 | if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: ND)) |
13177 | return RD->getDeviceLambdaManglingNumber(); |
13178 | return std::nullopt; |
13179 | }, |
13180 | /*IsAux=*/true); |
13181 | case TargetCXXABI::Microsoft: |
13182 | return MicrosoftMangleContext::create(Context&: *this, Diags&: getDiagnostics(), |
13183 | /*IsAux=*/true); |
13184 | } |
13185 | llvm_unreachable("Unsupported ABI"); |
13186 | } |
13187 | |
13188 | CXXABI::~CXXABI() = default; |
13189 | |
13190 | size_t ASTContext::getSideTableAllocatedMemory() const { |
13191 | return ASTRecordLayouts.getMemorySize() + |
13192 | llvm::capacity_in_bytes(ObjCLayouts) + |
13193 | llvm::capacity_in_bytes(KeyFunctions) + |
13194 | llvm::capacity_in_bytes(ObjCImpls) + |
13195 | llvm::capacity_in_bytes(BlockVarCopyInits) + |
13196 | llvm::capacity_in_bytes(DeclAttrs) + |
13197 | llvm::capacity_in_bytes(TemplateOrInstantiation) + |
13198 | llvm::capacity_in_bytes(InstantiatedFromUsingDecl) + |
13199 | llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) + |
13200 | llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) + |
13201 | llvm::capacity_in_bytes(OverriddenMethods) + |
13202 | llvm::capacity_in_bytes(Types) + |
13203 | llvm::capacity_in_bytes(VariableArrayTypes); |
13204 | } |
13205 | |
13206 | /// getIntTypeForBitwidth - |
13207 | /// sets integer QualTy according to specified details: |
13208 | /// bitwidth, signed/unsigned. |
13209 | /// Returns empty type if there is no appropriate target types. |
13210 | QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth, |
13211 | unsigned Signed) const { |
13212 | TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(BitWidth: DestWidth, IsSigned: Signed); |
13213 | CanQualType QualTy = getFromTargetType(Type: Ty); |
13214 | if (!QualTy && DestWidth == 128) |
13215 | return Signed ? Int128Ty : UnsignedInt128Ty; |
13216 | return QualTy; |
13217 | } |
13218 | |
13219 | /// getRealTypeForBitwidth - |
13220 | /// sets floating point QualTy according to specified bitwidth. |
13221 | /// Returns empty type if there is no appropriate target types. |
13222 | QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth, |
13223 | FloatModeKind ExplicitType) const { |
13224 | FloatModeKind Ty = |
13225 | getTargetInfo().getRealTypeByWidth(BitWidth: DestWidth, ExplicitType); |
13226 | switch (Ty) { |
13227 | case FloatModeKind::Half: |
13228 | return HalfTy; |
13229 | case FloatModeKind::Float: |
13230 | return FloatTy; |
13231 | case FloatModeKind::Double: |
13232 | return DoubleTy; |
13233 | case FloatModeKind::LongDouble: |
13234 | return LongDoubleTy; |
13235 | case FloatModeKind::Float128: |
13236 | return Float128Ty; |
13237 | case FloatModeKind::Ibm128: |
13238 | return Ibm128Ty; |
13239 | case FloatModeKind::NoFloat: |
13240 | return {}; |
13241 | } |
13242 | |
13243 | llvm_unreachable("Unhandled TargetInfo::RealType value"); |
13244 | } |
13245 | |
13246 | void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) { |
13247 | if (Number <= 1) |
13248 | return; |
13249 | |
13250 | MangleNumbers[ND] = Number; |
13251 | |
13252 | if (Listener) |
13253 | Listener->AddedManglingNumber(ND, Number); |
13254 | } |
13255 | |
13256 | unsigned ASTContext::getManglingNumber(const NamedDecl *ND, |
13257 | bool ForAuxTarget) const { |
13258 | auto I = MangleNumbers.find(ND); |
13259 | unsigned Res = I != MangleNumbers.end() ? I->second : 1; |
13260 | // CUDA/HIP host compilation encodes host and device mangling numbers |
13261 | // as lower and upper half of 32 bit integer. |
13262 | if (LangOpts.CUDA && !LangOpts.CUDAIsDevice) { |
13263 | Res = ForAuxTarget ? Res >> 16 : Res & 0xFFFF; |
13264 | } else { |
13265 | assert(!ForAuxTarget && "Only CUDA/HIP host compilation supports mangling " |
13266 | "number for aux target"); |
13267 | } |
13268 | return Res > 1 ? Res : 1; |
13269 | } |
13270 | |
13271 | void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) { |
13272 | if (Number <= 1) |
13273 | return; |
13274 | |
13275 | StaticLocalNumbers[VD] = Number; |
13276 | |
13277 | if (Listener) |
13278 | Listener->AddedStaticLocalNumbers(VD, Number); |
13279 | } |
13280 | |
13281 | unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const { |
13282 | auto I = StaticLocalNumbers.find(VD); |
13283 | return I != StaticLocalNumbers.end() ? I->second : 1; |
13284 | } |
13285 | |
13286 | void ASTContext::setIsDestroyingOperatorDelete(const FunctionDecl *FD, |
13287 | bool IsDestroying) { |
13288 | if (!IsDestroying) { |
13289 | assert(!DestroyingOperatorDeletes.contains(FD->getCanonicalDecl())); |
13290 | return; |
13291 | } |
13292 | DestroyingOperatorDeletes.insert(V: FD->getCanonicalDecl()); |
13293 | } |
13294 | |
13295 | bool ASTContext::isDestroyingOperatorDelete(const FunctionDecl *FD) const { |
13296 | return DestroyingOperatorDeletes.contains(V: FD->getCanonicalDecl()); |
13297 | } |
13298 | |
13299 | void ASTContext::setIsTypeAwareOperatorNewOrDelete(const FunctionDecl *FD, |
13300 | bool IsTypeAware) { |
13301 | if (!IsTypeAware) { |
13302 | assert(!TypeAwareOperatorNewAndDeletes.contains(FD->getCanonicalDecl())); |
13303 | return; |
13304 | } |
13305 | TypeAwareOperatorNewAndDeletes.insert(V: FD->getCanonicalDecl()); |
13306 | } |
13307 | |
13308 | bool ASTContext::isTypeAwareOperatorNewOrDelete(const FunctionDecl *FD) const { |
13309 | return TypeAwareOperatorNewAndDeletes.contains(V: FD->getCanonicalDecl()); |
13310 | } |
13311 | |
13312 | MangleNumberingContext & |
13313 | ASTContext::getManglingNumberContext(const DeclContext *DC) { |
13314 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
13315 | std::unique_ptr<MangleNumberingContext> &MCtx = MangleNumberingContexts[DC]; |
13316 | if (!MCtx) |
13317 | MCtx = createMangleNumberingContext(); |
13318 | return *MCtx; |
13319 | } |
13320 | |
13321 | MangleNumberingContext & |
13322 | ASTContext::getManglingNumberContext(NeedExtraManglingDecl_t, const Decl *D) { |
13323 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
13324 | std::unique_ptr<MangleNumberingContext> &MCtx = |
13325 | ExtraMangleNumberingContexts[D]; |
13326 | if (!MCtx) |
13327 | MCtx = createMangleNumberingContext(); |
13328 | return *MCtx; |
13329 | } |
13330 | |
13331 | std::unique_ptr<MangleNumberingContext> |
13332 | ASTContext::createMangleNumberingContext() const { |
13333 | return ABI->createMangleNumberingContext(); |
13334 | } |
13335 | |
13336 | const CXXConstructorDecl * |
13337 | ASTContext::getCopyConstructorForExceptionObject(CXXRecordDecl *RD) { |
13338 | return ABI->getCopyConstructorForExceptionObject( |
13339 | cast<CXXRecordDecl>(RD->getFirstDecl())); |
13340 | } |
13341 | |
13342 | void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD, |
13343 | CXXConstructorDecl *CD) { |
13344 | return ABI->addCopyConstructorForExceptionObject( |
13345 | cast<CXXRecordDecl>(RD->getFirstDecl()), |
13346 | cast<CXXConstructorDecl>(CD->getFirstDecl())); |
13347 | } |
13348 | |
13349 | void ASTContext::addTypedefNameForUnnamedTagDecl(TagDecl *TD, |
13350 | TypedefNameDecl *DD) { |
13351 | return ABI->addTypedefNameForUnnamedTagDecl(TD, DD); |
13352 | } |
13353 | |
13354 | TypedefNameDecl * |
13355 | ASTContext::getTypedefNameForUnnamedTagDecl(const TagDecl *TD) { |
13356 | return ABI->getTypedefNameForUnnamedTagDecl(TD); |
13357 | } |
13358 | |
13359 | void ASTContext::addDeclaratorForUnnamedTagDecl(TagDecl *TD, |
13360 | DeclaratorDecl *DD) { |
13361 | return ABI->addDeclaratorForUnnamedTagDecl(TD, DD); |
13362 | } |
13363 | |
13364 | DeclaratorDecl *ASTContext::getDeclaratorForUnnamedTagDecl(const TagDecl *TD) { |
13365 | return ABI->getDeclaratorForUnnamedTagDecl(TD); |
13366 | } |
13367 | |
13368 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
13369 | ParamIndices[D] = index; |
13370 | } |
13371 | |
13372 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
13373 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
13374 | assert(I != ParamIndices.end() && |
13375 | "ParmIndices lacks entry set by ParmVarDecl"); |
13376 | return I->second; |
13377 | } |
13378 | |
13379 | QualType ASTContext::getStringLiteralArrayType(QualType EltTy, |
13380 | unsigned Length) const { |
13381 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
13382 | if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings) |
13383 | EltTy = EltTy.withConst(); |
13384 | |
13385 | EltTy = adjustStringLiteralBaseType(Ty: EltTy); |
13386 | |
13387 | // Get an array type for the string, according to C99 6.4.5. This includes |
13388 | // the null terminator character. |
13389 | return getConstantArrayType(EltTy, ArySizeIn: llvm::APInt(32, Length + 1), SizeExpr: nullptr, |
13390 | ASM: ArraySizeModifier::Normal, /*IndexTypeQuals*/ 0); |
13391 | } |
13392 | |
13393 | StringLiteral * |
13394 | ASTContext::getPredefinedStringLiteralFromCache(StringRef Key) const { |
13395 | StringLiteral *&Result = StringLiteralCache[Key]; |
13396 | if (!Result) |
13397 | Result = StringLiteral::Create( |
13398 | *this, Key, StringLiteralKind::Ordinary, |
13399 | /*Pascal*/ false, getStringLiteralArrayType(CharTy, Key.size()), |
13400 | SourceLocation()); |
13401 | return Result; |
13402 | } |
13403 | |
13404 | MSGuidDecl * |
13405 | ASTContext::getMSGuidDecl(MSGuidDecl::Parts Parts) const { |
13406 | assert(MSGuidTagDecl && "building MS GUID without MS extensions?"); |
13407 | |
13408 | llvm::FoldingSetNodeID ID; |
13409 | MSGuidDecl::Profile(ID, P: Parts); |
13410 | |
13411 | void *InsertPos; |
13412 | if (MSGuidDecl *Existing = MSGuidDecls.FindNodeOrInsertPos(ID, InsertPos)) |
13413 | return Existing; |
13414 | |
13415 | QualType GUIDType = getMSGuidType().withConst(); |
13416 | MSGuidDecl *New = MSGuidDecl::Create(C: *this, T: GUIDType, P: Parts); |
13417 | MSGuidDecls.InsertNode(New, InsertPos); |
13418 | return New; |
13419 | } |
13420 | |
13421 | UnnamedGlobalConstantDecl * |
13422 | ASTContext::getUnnamedGlobalConstantDecl(QualType Ty, |
13423 | const APValue &APVal) const { |
13424 | llvm::FoldingSetNodeID ID; |
13425 | UnnamedGlobalConstantDecl::Profile(ID, Ty, APVal); |
13426 | |
13427 | void *InsertPos; |
13428 | if (UnnamedGlobalConstantDecl *Existing = |
13429 | UnnamedGlobalConstantDecls.FindNodeOrInsertPos(ID, InsertPos)) |
13430 | return Existing; |
13431 | |
13432 | UnnamedGlobalConstantDecl *New = |
13433 | UnnamedGlobalConstantDecl::Create(C: *this, T: Ty, APVal); |
13434 | UnnamedGlobalConstantDecls.InsertNode(New, InsertPos); |
13435 | return New; |
13436 | } |
13437 | |
13438 | TemplateParamObjectDecl * |
13439 | ASTContext::getTemplateParamObjectDecl(QualType T, const APValue &V) const { |
13440 | assert(T->isRecordType() && "template param object of unexpected type"); |
13441 | |
13442 | // C++ [temp.param]p8: |
13443 | // [...] a static storage duration object of type 'const T' [...] |
13444 | T.addConst(); |
13445 | |
13446 | llvm::FoldingSetNodeID ID; |
13447 | TemplateParamObjectDecl::Profile(ID, T, V); |
13448 | |
13449 | void *InsertPos; |
13450 | if (TemplateParamObjectDecl *Existing = |
13451 | TemplateParamObjectDecls.FindNodeOrInsertPos(ID, InsertPos)) |
13452 | return Existing; |
13453 | |
13454 | TemplateParamObjectDecl *New = TemplateParamObjectDecl::Create(C: *this, T, V); |
13455 | TemplateParamObjectDecls.InsertNode(New, InsertPos); |
13456 | return New; |
13457 | } |
13458 | |
13459 | bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { |
13460 | const llvm::Triple &T = getTargetInfo().getTriple(); |
13461 | if (!T.isOSDarwin()) |
13462 | return false; |
13463 | |
13464 | if (!(T.isiOS() && T.isOSVersionLT(Major: 7)) && |
13465 | !(T.isMacOSX() && T.isOSVersionLT(Major: 10, Minor: 9))) |
13466 | return false; |
13467 | |
13468 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
13469 | CharUnits sizeChars = getTypeSizeInChars(T: AtomicTy); |
13470 | uint64_t Size = sizeChars.getQuantity(); |
13471 | CharUnits alignChars = getTypeAlignInChars(T: AtomicTy); |
13472 | unsigned Align = alignChars.getQuantity(); |
13473 | unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth(); |
13474 | return (Size != Align || toBits(CharSize: sizeChars) > MaxInlineWidthInBits); |
13475 | } |
13476 | |
13477 | bool |
13478 | ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl, |
13479 | const ObjCMethodDecl *MethodImpl) { |
13480 | // No point trying to match an unavailable/deprecated mothod. |
13481 | if (MethodDecl->hasAttr<UnavailableAttr>() |
13482 | || MethodDecl->hasAttr<DeprecatedAttr>()) |
13483 | return false; |
13484 | if (MethodDecl->getObjCDeclQualifier() != |
13485 | MethodImpl->getObjCDeclQualifier()) |
13486 | return false; |
13487 | if (!hasSameType(T1: MethodDecl->getReturnType(), T2: MethodImpl->getReturnType())) |
13488 | return false; |
13489 | |
13490 | if (MethodDecl->param_size() != MethodImpl->param_size()) |
13491 | return false; |
13492 | |
13493 | for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(), |
13494 | IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(), |
13495 | EF = MethodDecl->param_end(); |
13496 | IM != EM && IF != EF; ++IM, ++IF) { |
13497 | const ParmVarDecl *DeclVar = (*IF); |
13498 | const ParmVarDecl *ImplVar = (*IM); |
13499 | if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier()) |
13500 | return false; |
13501 | if (!hasSameType(DeclVar->getType(), ImplVar->getType())) |
13502 | return false; |
13503 | } |
13504 | |
13505 | return (MethodDecl->isVariadic() == MethodImpl->isVariadic()); |
13506 | } |
13507 | |
13508 | uint64_t ASTContext::getTargetNullPointerValue(QualType QT) const { |
13509 | LangAS AS; |
13510 | if (QT->getUnqualifiedDesugaredType()->isNullPtrType()) |
13511 | AS = LangAS::Default; |
13512 | else |
13513 | AS = QT->getPointeeType().getAddressSpace(); |
13514 | |
13515 | return getTargetInfo().getNullPointerValue(AddrSpace: AS); |
13516 | } |
13517 | |
13518 | unsigned ASTContext::getTargetAddressSpace(LangAS AS) const { |
13519 | return getTargetInfo().getTargetAddressSpace(AS); |
13520 | } |
13521 | |
13522 | bool ASTContext::hasSameExpr(const Expr *X, const Expr *Y) const { |
13523 | if (X == Y) |
13524 | return true; |
13525 | if (!X || !Y) |
13526 | return false; |
13527 | llvm::FoldingSetNodeID IDX, IDY; |
13528 | X->Profile(IDX, *this, /*Canonical=*/true); |
13529 | Y->Profile(IDY, *this, /*Canonical=*/true); |
13530 | return IDX == IDY; |
13531 | } |
13532 | |
13533 | // The getCommon* helpers return, for given 'same' X and Y entities given as |
13534 | // inputs, another entity which is also the 'same' as the inputs, but which |
13535 | // is closer to the canonical form of the inputs, each according to a given |
13536 | // criteria. |
13537 | // The getCommon*Checked variants are 'null inputs not-allowed' equivalents of |
13538 | // the regular ones. |
13539 | |
13540 | static Decl *getCommonDecl(Decl *X, Decl *Y) { |
13541 | if (!declaresSameEntity(D1: X, D2: Y)) |
13542 | return nullptr; |
13543 | for (const Decl *DX : X->redecls()) { |
13544 | // If we reach Y before reaching the first decl, that means X is older. |
13545 | if (DX == Y) |
13546 | return X; |
13547 | // If we reach the first decl, then Y is older. |
13548 | if (DX->isFirstDecl()) |
13549 | return Y; |
13550 | } |
13551 | llvm_unreachable("Corrupt redecls chain"); |
13552 | } |
13553 | |
13554 | template <class T, std::enable_if_t<std::is_base_of_v<Decl, T>, bool> = true> |
13555 | static T *getCommonDecl(T *X, T *Y) { |
13556 | return cast_or_null<T>( |
13557 | getCommonDecl(X: const_cast<Decl *>(cast_or_null<Decl>(X)), |
13558 | Y: const_cast<Decl *>(cast_or_null<Decl>(Y)))); |
13559 | } |
13560 | |
13561 | template <class T, std::enable_if_t<std::is_base_of_v<Decl, T>, bool> = true> |
13562 | static T *getCommonDeclChecked(T *X, T *Y) { |
13563 | return cast<T>(getCommonDecl(X: const_cast<Decl *>(cast<Decl>(X)), |
13564 | Y: const_cast<Decl *>(cast<Decl>(Y)))); |
13565 | } |
13566 | |
13567 | static TemplateName getCommonTemplateName(ASTContext &Ctx, TemplateName X, |
13568 | TemplateName Y, |
13569 | bool IgnoreDeduced = false) { |
13570 | if (X.getAsVoidPointer() == Y.getAsVoidPointer()) |
13571 | return X; |
13572 | // FIXME: There are cases here where we could find a common template name |
13573 | // with more sugar. For example one could be a SubstTemplateTemplate* |
13574 | // replacing the other. |
13575 | TemplateName CX = Ctx.getCanonicalTemplateName(Name: X, IgnoreDeduced); |
13576 | if (CX.getAsVoidPointer() != |
13577 | Ctx.getCanonicalTemplateName(Name: Y).getAsVoidPointer()) |
13578 | return TemplateName(); |
13579 | return CX; |
13580 | } |
13581 | |
13582 | static TemplateName getCommonTemplateNameChecked(ASTContext &Ctx, |
13583 | TemplateName X, TemplateName Y, |
13584 | bool IgnoreDeduced) { |
13585 | TemplateName R = getCommonTemplateName(Ctx, X, Y, IgnoreDeduced); |
13586 | assert(R.getAsVoidPointer() != nullptr); |
13587 | return R; |
13588 | } |
13589 | |
13590 | static auto getCommonTypes(ASTContext &Ctx, ArrayRef<QualType> Xs, |
13591 | ArrayRef<QualType> Ys, bool Unqualified = false) { |
13592 | assert(Xs.size() == Ys.size()); |
13593 | SmallVector<QualType, 8> Rs(Xs.size()); |
13594 | for (size_t I = 0; I < Rs.size(); ++I) |
13595 | Rs[I] = Ctx.getCommonSugaredType(X: Xs[I], Y: Ys[I], Unqualified); |
13596 | return Rs; |
13597 | } |
13598 | |
13599 | template <class T> |
13600 | static SourceLocation getCommonAttrLoc(const T *X, const T *Y) { |
13601 | return X->getAttributeLoc() == Y->getAttributeLoc() ? X->getAttributeLoc() |
13602 | : SourceLocation(); |
13603 | } |
13604 | |
13605 | static TemplateArgument getCommonTemplateArgument(ASTContext &Ctx, |
13606 | const TemplateArgument &X, |
13607 | const TemplateArgument &Y) { |
13608 | if (X.getKind() != Y.getKind()) |
13609 | return TemplateArgument(); |
13610 | |
13611 | switch (X.getKind()) { |
13612 | case TemplateArgument::ArgKind::Type: |
13613 | if (!Ctx.hasSameType(T1: X.getAsType(), T2: Y.getAsType())) |
13614 | return TemplateArgument(); |
13615 | return TemplateArgument( |
13616 | Ctx.getCommonSugaredType(X: X.getAsType(), Y: Y.getAsType())); |
13617 | case TemplateArgument::ArgKind::NullPtr: |
13618 | if (!Ctx.hasSameType(T1: X.getNullPtrType(), T2: Y.getNullPtrType())) |
13619 | return TemplateArgument(); |
13620 | return TemplateArgument( |
13621 | Ctx.getCommonSugaredType(X: X.getNullPtrType(), Y: Y.getNullPtrType()), |
13622 | /*Unqualified=*/true); |
13623 | case TemplateArgument::ArgKind::Expression: |
13624 | if (!Ctx.hasSameType(T1: X.getAsExpr()->getType(), T2: Y.getAsExpr()->getType())) |
13625 | return TemplateArgument(); |
13626 | // FIXME: Try to keep the common sugar. |
13627 | return X; |
13628 | case TemplateArgument::ArgKind::Template: { |
13629 | TemplateName TX = X.getAsTemplate(), TY = Y.getAsTemplate(); |
13630 | TemplateName CTN = ::getCommonTemplateName(Ctx, X: TX, Y: TY); |
13631 | if (!CTN.getAsVoidPointer()) |
13632 | return TemplateArgument(); |
13633 | return TemplateArgument(CTN); |
13634 | } |
13635 | case TemplateArgument::ArgKind::TemplateExpansion: { |
13636 | TemplateName TX = X.getAsTemplateOrTemplatePattern(), |
13637 | TY = Y.getAsTemplateOrTemplatePattern(); |
13638 | TemplateName CTN = ::getCommonTemplateName(Ctx, X: TX, Y: TY); |
13639 | if (!CTN.getAsVoidPointer()) |
13640 | return TemplateName(); |
13641 | auto NExpX = X.getNumTemplateExpansions(); |
13642 | assert(NExpX == Y.getNumTemplateExpansions()); |
13643 | return TemplateArgument(CTN, NExpX); |
13644 | } |
13645 | default: |
13646 | // FIXME: Handle the other argument kinds. |
13647 | return X; |
13648 | } |
13649 | } |
13650 | |
13651 | static bool getCommonTemplateArguments(ASTContext &Ctx, |
13652 | SmallVectorImpl<TemplateArgument> &R, |
13653 | ArrayRef<TemplateArgument> Xs, |
13654 | ArrayRef<TemplateArgument> Ys) { |
13655 | if (Xs.size() != Ys.size()) |
13656 | return true; |
13657 | R.resize(N: Xs.size()); |
13658 | for (size_t I = 0; I < R.size(); ++I) { |
13659 | R[I] = getCommonTemplateArgument(Ctx, X: Xs[I], Y: Ys[I]); |
13660 | if (R[I].isNull()) |
13661 | return true; |
13662 | } |
13663 | return false; |
13664 | } |
13665 | |
13666 | static auto getCommonTemplateArguments(ASTContext &Ctx, |
13667 | ArrayRef<TemplateArgument> Xs, |
13668 | ArrayRef<TemplateArgument> Ys) { |
13669 | SmallVector<TemplateArgument, 8> R; |
13670 | bool Different = getCommonTemplateArguments(Ctx, R, Xs, Ys); |
13671 | assert(!Different); |
13672 | (void)Different; |
13673 | return R; |
13674 | } |
13675 | |
13676 | template <class T> |
13677 | static ElaboratedTypeKeyword getCommonTypeKeyword(const T *X, const T *Y) { |
13678 | return X->getKeyword() == Y->getKeyword() ? X->getKeyword() |
13679 | : ElaboratedTypeKeyword::None; |
13680 | } |
13681 | |
13682 | /// Returns a NestedNameSpecifier which has only the common sugar |
13683 | /// present in both NNS1 and NNS2. |
13684 | static NestedNameSpecifier *getCommonNNS(ASTContext &Ctx, |
13685 | NestedNameSpecifier *NNS1, |
13686 | NestedNameSpecifier *NNS2, |
13687 | bool IsSame) { |
13688 | // If they are identical, all sugar is common. |
13689 | if (NNS1 == NNS2) |
13690 | return NNS1; |
13691 | |
13692 | // IsSame implies both NNSes are equivalent. |
13693 | NestedNameSpecifier *Canon = Ctx.getCanonicalNestedNameSpecifier(NNS: NNS1); |
13694 | if (Canon != Ctx.getCanonicalNestedNameSpecifier(NNS: NNS2)) { |
13695 | assert(!IsSame && "Should be the same NestedNameSpecifier"); |
13696 | // If they are not the same, there is nothing to unify. |
13697 | // FIXME: It would be useful here if we could represent a canonically |
13698 | // empty NNS, which is not identical to an empty-as-written NNS. |
13699 | return nullptr; |
13700 | } |
13701 | |
13702 | NestedNameSpecifier *R = nullptr; |
13703 | NestedNameSpecifier::SpecifierKind K1 = NNS1->getKind(), K2 = NNS2->getKind(); |
13704 | switch (K1) { |
13705 | case NestedNameSpecifier::SpecifierKind::Identifier: { |
13706 | assert(K2 == NestedNameSpecifier::SpecifierKind::Identifier); |
13707 | IdentifierInfo *II = NNS1->getAsIdentifier(); |
13708 | assert(II == NNS2->getAsIdentifier()); |
13709 | // For an identifier, the prefixes are significant, so they must be the |
13710 | // same. |
13711 | NestedNameSpecifier *P = ::getCommonNNS(Ctx, NNS1: NNS1->getPrefix(), |
13712 | NNS2: NNS2->getPrefix(), /*IsSame=*/true); |
13713 | R = NestedNameSpecifier::Create(Context: Ctx, Prefix: P, II); |
13714 | break; |
13715 | } |
13716 | case NestedNameSpecifier::SpecifierKind::Namespace: |
13717 | case NestedNameSpecifier::SpecifierKind::NamespaceAlias: { |
13718 | assert(K2 == NestedNameSpecifier::SpecifierKind::Namespace || |
13719 | K2 == NestedNameSpecifier::SpecifierKind::NamespaceAlias); |
13720 | // The prefixes for namespaces are not significant, its declaration |
13721 | // identifies it uniquely. |
13722 | NestedNameSpecifier *P = |
13723 | ::getCommonNNS(Ctx, NNS1: NNS1->getPrefix(), NNS2: NNS2->getPrefix(), |
13724 | /*IsSame=*/false); |
13725 | NamespaceAliasDecl *A1 = NNS1->getAsNamespaceAlias(), |
13726 | *A2 = NNS2->getAsNamespaceAlias(); |
13727 | // Are they the same namespace alias? |
13728 | if (declaresSameEntity(A1, A2)) { |
13729 | R = NestedNameSpecifier::Create(Context: Ctx, Prefix: P, Alias: ::getCommonDeclChecked(X: A1, Y: A2)); |
13730 | break; |
13731 | } |
13732 | // Otherwise, look at the namespaces only. |
13733 | NamespaceDecl *N1 = A1 ? A1->getNamespace() : NNS1->getAsNamespace(), |
13734 | *N2 = A2 ? A2->getNamespace() : NNS2->getAsNamespace(); |
13735 | R = NestedNameSpecifier::Create(Context: Ctx, Prefix: P, NS: ::getCommonDeclChecked(X: N1, Y: N2)); |
13736 | break; |
13737 | } |
13738 | case NestedNameSpecifier::SpecifierKind::TypeSpec: { |
13739 | // FIXME: See comment below, on Super case. |
13740 | if (K2 == NestedNameSpecifier::SpecifierKind::Super) |
13741 | return Ctx.getCanonicalNestedNameSpecifier(NNS: NNS1); |
13742 | |
13743 | assert(K2 == NestedNameSpecifier::SpecifierKind::TypeSpec); |
13744 | |
13745 | const Type *T1 = NNS1->getAsType(), *T2 = NNS2->getAsType(); |
13746 | if (T1 == T2) { |
13747 | // If the types are indentical, then only the prefixes differ. |
13748 | // A well-formed NNS never has these types, as they have |
13749 | // special normalized forms. |
13750 | assert((!isa<DependentNameType, ElaboratedType>(T1))); |
13751 | // Only for a DependentTemplateSpecializationType the prefix |
13752 | // is actually significant. A DependentName, which would be another |
13753 | // plausible case, cannot occur here, as explained above. |
13754 | bool IsSame = isa<DependentTemplateSpecializationType>(Val: T1); |
13755 | NestedNameSpecifier *P = |
13756 | ::getCommonNNS(Ctx, NNS1: NNS1->getPrefix(), NNS2: NNS2->getPrefix(), IsSame); |
13757 | R = NestedNameSpecifier::Create(Context: Ctx, Prefix: P, T: T1); |
13758 | break; |
13759 | } |
13760 | // TODO: Try to salvage the original prefix. |
13761 | // If getCommonSugaredType removed any top level sugar, the original prefix |
13762 | // is not applicable anymore. |
13763 | const Type *T = Ctx.getCommonSugaredType(X: QualType(T1, 0), Y: QualType(T2, 0), |
13764 | /*Unqualified=*/true) |
13765 | .getTypePtr(); |
13766 | |
13767 | // A NestedNameSpecifier has special normalization rules for certain types. |
13768 | switch (T->getTypeClass()) { |
13769 | case Type::Elaborated: { |
13770 | // An ElaboratedType is stripped off, it's Qualifier becomes the prefix. |
13771 | auto *ET = cast<ElaboratedType>(Val: T); |
13772 | R = NestedNameSpecifier::Create(Context: Ctx, Prefix: ET->getQualifier(), |
13773 | T: ET->getNamedType().getTypePtr()); |
13774 | break; |
13775 | } |
13776 | case Type::DependentName: { |
13777 | // A DependentName is turned into an Identifier NNS. |
13778 | auto *DN = cast<DependentNameType>(Val: T); |
13779 | R = NestedNameSpecifier::Create(Context: Ctx, Prefix: DN->getQualifier(), |
13780 | II: DN->getIdentifier()); |
13781 | break; |
13782 | } |
13783 | case Type::DependentTemplateSpecialization: { |
13784 | // A DependentTemplateSpecializationType loses it's Qualifier, which |
13785 | // is turned into the prefix. |
13786 | auto *DTST = cast<DependentTemplateSpecializationType>(Val: T); |
13787 | const DependentTemplateStorage &DTN = DTST->getDependentTemplateName(); |
13788 | DependentTemplateStorage NewDTN(/*Qualifier=*/nullptr, DTN.getName(), |
13789 | DTN.hasTemplateKeyword()); |
13790 | T = Ctx.getDependentTemplateSpecializationType(DTST->getKeyword(), NewDTN, |
13791 | DTST->template_arguments()) |
13792 | .getTypePtr(); |
13793 | R = NestedNameSpecifier::Create(Context: Ctx, Prefix: DTN.getQualifier(), T); |
13794 | break; |
13795 | } |
13796 | default: |
13797 | R = NestedNameSpecifier::Create(Context: Ctx, /*Prefix=*/nullptr, T); |
13798 | break; |
13799 | } |
13800 | break; |
13801 | } |
13802 | case NestedNameSpecifier::SpecifierKind::Super: |
13803 | // FIXME: Can __super even be used with data members? |
13804 | // If it's only usable in functions, we will never see it here, |
13805 | // unless we save the qualifiers used in function types. |
13806 | // In that case, it might be possible NNS2 is a type, |
13807 | // in which case we should degrade the result to |
13808 | // a CXXRecordType. |
13809 | return Ctx.getCanonicalNestedNameSpecifier(NNS: NNS1); |
13810 | case NestedNameSpecifier::SpecifierKind::Global: |
13811 | // The global NNS is a singleton. |
13812 | assert(K2 == NestedNameSpecifier::SpecifierKind::Global && |
13813 | "Global NNS cannot be equivalent to any other kind"); |
13814 | llvm_unreachable("Global NestedNameSpecifiers did not compare equal"); |
13815 | } |
13816 | assert(Ctx.getCanonicalNestedNameSpecifier(R) == Canon); |
13817 | return R; |
13818 | } |
13819 | |
13820 | template <class T> |
13821 | static NestedNameSpecifier *getCommonQualifier(ASTContext &Ctx, const T *X, |
13822 | const T *Y, bool IsSame) { |
13823 | return ::getCommonNNS(Ctx, NNS1: X->getQualifier(), NNS2: Y->getQualifier(), IsSame); |
13824 | } |
13825 | |
13826 | template <class T> |
13827 | static QualType getCommonElementType(ASTContext &Ctx, const T *X, const T *Y) { |
13828 | return Ctx.getCommonSugaredType(X: X->getElementType(), Y: Y->getElementType()); |
13829 | } |
13830 | |
13831 | template <class T> |
13832 | static QualType getCommonArrayElementType(ASTContext &Ctx, const T *X, |
13833 | Qualifiers &QX, const T *Y, |
13834 | Qualifiers &QY) { |
13835 | QualType EX = X->getElementType(), EY = Y->getElementType(); |
13836 | QualType R = Ctx.getCommonSugaredType(X: EX, Y: EY, |
13837 | /*Unqualified=*/true); |
13838 | // Qualifiers common to both element types. |
13839 | Qualifiers RQ = R.getQualifiers(); |
13840 | // For each side, move to the top level any qualifiers which are not common to |
13841 | // both element types. The caller must assume top level qualifiers might |
13842 | // be different, even if they are the same type, and can be treated as sugar. |
13843 | QX += EX.getQualifiers() - RQ; |
13844 | QY += EY.getQualifiers() - RQ; |
13845 | return R; |
13846 | } |
13847 | |
13848 | template <class T> |
13849 | static QualType getCommonPointeeType(ASTContext &Ctx, const T *X, const T *Y) { |
13850 | return Ctx.getCommonSugaredType(X: X->getPointeeType(), Y: Y->getPointeeType()); |
13851 | } |
13852 | |
13853 | template <class T> static auto *getCommonSizeExpr(ASTContext &Ctx, T *X, T *Y) { |
13854 | assert(Ctx.hasSameExpr(X->getSizeExpr(), Y->getSizeExpr())); |
13855 | return X->getSizeExpr(); |
13856 | } |
13857 | |
13858 | static auto getCommonSizeModifier(const ArrayType *X, const ArrayType *Y) { |
13859 | assert(X->getSizeModifier() == Y->getSizeModifier()); |
13860 | return X->getSizeModifier(); |
13861 | } |
13862 | |
13863 | static auto getCommonIndexTypeCVRQualifiers(const ArrayType *X, |
13864 | const ArrayType *Y) { |
13865 | assert(X->getIndexTypeCVRQualifiers() == Y->getIndexTypeCVRQualifiers()); |
13866 | return X->getIndexTypeCVRQualifiers(); |
13867 | } |
13868 | |
13869 | // Merges two type lists such that the resulting vector will contain |
13870 | // each type (in a canonical sense) only once, in the order they appear |
13871 | // from X to Y. If they occur in both X and Y, the result will contain |
13872 | // the common sugared type between them. |
13873 | static void mergeTypeLists(ASTContext &Ctx, SmallVectorImpl<QualType> &Out, |
13874 | ArrayRef<QualType> X, ArrayRef<QualType> Y) { |
13875 | llvm::DenseMap<QualType, unsigned> Found; |
13876 | for (auto Ts : {X, Y}) { |
13877 | for (QualType T : Ts) { |
13878 | auto Res = Found.try_emplace(Ctx.getCanonicalType(T), Out.size()); |
13879 | if (!Res.second) { |
13880 | QualType &U = Out[Res.first->second]; |
13881 | U = Ctx.getCommonSugaredType(X: U, Y: T); |
13882 | } else { |
13883 | Out.emplace_back(Args&: T); |
13884 | } |
13885 | } |
13886 | } |
13887 | } |
13888 | |
13889 | FunctionProtoType::ExceptionSpecInfo |
13890 | ASTContext::mergeExceptionSpecs(FunctionProtoType::ExceptionSpecInfo ESI1, |
13891 | FunctionProtoType::ExceptionSpecInfo ESI2, |
13892 | SmallVectorImpl<QualType> &ExceptionTypeStorage, |
13893 | bool AcceptDependent) { |
13894 | ExceptionSpecificationType EST1 = ESI1.Type, EST2 = ESI2.Type; |
13895 | |
13896 | // If either of them can throw anything, that is the result. |
13897 | for (auto I : {EST_None, EST_MSAny, EST_NoexceptFalse}) { |
13898 | if (EST1 == I) |
13899 | return ESI1; |
13900 | if (EST2 == I) |
13901 | return ESI2; |
13902 | } |
13903 | |
13904 | // If either of them is non-throwing, the result is the other. |
13905 | for (auto I : |
13906 | {EST_NoThrow, EST_DynamicNone, EST_BasicNoexcept, EST_NoexceptTrue}) { |
13907 | if (EST1 == I) |
13908 | return ESI2; |
13909 | if (EST2 == I) |
13910 | return ESI1; |
13911 | } |
13912 | |
13913 | // If we're left with value-dependent computed noexcept expressions, we're |
13914 | // stuck. Before C++17, we can just drop the exception specification entirely, |
13915 | // since it's not actually part of the canonical type. And this should never |
13916 | // happen in C++17, because it would mean we were computing the composite |
13917 | // pointer type of dependent types, which should never happen. |
13918 | if (EST1 == EST_DependentNoexcept || EST2 == EST_DependentNoexcept) { |
13919 | assert(AcceptDependent && |
13920 | "computing composite pointer type of dependent types"); |
13921 | return FunctionProtoType::ExceptionSpecInfo(); |
13922 | } |
13923 | |
13924 | // Switch over the possibilities so that people adding new values know to |
13925 | // update this function. |
13926 | switch (EST1) { |
13927 | case EST_None: |
13928 | case EST_DynamicNone: |
13929 | case EST_MSAny: |
13930 | case EST_BasicNoexcept: |
13931 | case EST_DependentNoexcept: |
13932 | case EST_NoexceptFalse: |
13933 | case EST_NoexceptTrue: |
13934 | case EST_NoThrow: |
13935 | llvm_unreachable("These ESTs should be handled above"); |
13936 | |
13937 | case EST_Dynamic: { |
13938 | // This is the fun case: both exception specifications are dynamic. Form |
13939 | // the union of the two lists. |
13940 | assert(EST2 == EST_Dynamic && "other cases should already be handled"); |
13941 | mergeTypeLists(*this, ExceptionTypeStorage, ESI1.Exceptions, |
13942 | ESI2.Exceptions); |
13943 | FunctionProtoType::ExceptionSpecInfo Result(EST_Dynamic); |
13944 | Result.Exceptions = ExceptionTypeStorage; |
13945 | return Result; |
13946 | } |
13947 | |
13948 | case EST_Unevaluated: |
13949 | case EST_Uninstantiated: |
13950 | case EST_Unparsed: |
13951 | llvm_unreachable("shouldn't see unresolved exception specifications here"); |
13952 | } |
13953 | |
13954 | llvm_unreachable("invalid ExceptionSpecificationType"); |
13955 | } |
13956 | |
13957 | static QualType getCommonNonSugarTypeNode(ASTContext &Ctx, const Type *X, |
13958 | Qualifiers &QX, const Type *Y, |
13959 | Qualifiers &QY) { |
13960 | Type::TypeClass TC = X->getTypeClass(); |
13961 | assert(TC == Y->getTypeClass()); |
13962 | switch (TC) { |
13963 | #define UNEXPECTED_TYPE(Class, Kind) \ |
13964 | case Type::Class: \ |
13965 | llvm_unreachable("Unexpected " Kind ": " #Class); |
13966 | |
13967 | #define NON_CANONICAL_TYPE(Class, Base) UNEXPECTED_TYPE(Class, "non-canonical") |
13968 | #define TYPE(Class, Base) |
13969 | #include "clang/AST/TypeNodes.inc" |
13970 | |
13971 | #define SUGAR_FREE_TYPE(Class) UNEXPECTED_TYPE(Class, "sugar-free") |
13972 | SUGAR_FREE_TYPE(Builtin) |
13973 | SUGAR_FREE_TYPE(DeducedTemplateSpecialization) |
13974 | SUGAR_FREE_TYPE(DependentBitInt) |
13975 | SUGAR_FREE_TYPE(Enum) |
13976 | SUGAR_FREE_TYPE(BitInt) |
13977 | SUGAR_FREE_TYPE(ObjCInterface) |
13978 | SUGAR_FREE_TYPE(Record) |
13979 | SUGAR_FREE_TYPE(SubstTemplateTypeParmPack) |
13980 | SUGAR_FREE_TYPE(UnresolvedUsing) |
13981 | SUGAR_FREE_TYPE(HLSLAttributedResource) |
13982 | SUGAR_FREE_TYPE(HLSLInlineSpirv) |
13983 | #undef SUGAR_FREE_TYPE |
13984 | #define NON_UNIQUE_TYPE(Class) UNEXPECTED_TYPE(Class, "non-unique") |
13985 | NON_UNIQUE_TYPE(TypeOfExpr) |
13986 | NON_UNIQUE_TYPE(VariableArray) |
13987 | #undef NON_UNIQUE_TYPE |
13988 | |
13989 | UNEXPECTED_TYPE(TypeOf, "sugar") |
13990 | |
13991 | #undef UNEXPECTED_TYPE |
13992 | |
13993 | case Type::Auto: { |
13994 | const auto *AX = cast<AutoType>(X), *AY = cast<AutoType>(Y); |
13995 | assert(AX->getDeducedType().isNull()); |
13996 | assert(AY->getDeducedType().isNull()); |
13997 | assert(AX->getKeyword() == AY->getKeyword()); |
13998 | assert(AX->isInstantiationDependentType() == |
13999 | AY->isInstantiationDependentType()); |
14000 | auto As = getCommonTemplateArguments(Ctx, AX->getTypeConstraintArguments(), |
14001 | AY->getTypeConstraintArguments()); |
14002 | return Ctx.getAutoType(DeducedType: QualType(), Keyword: AX->getKeyword(), |
14003 | IsDependent: AX->isInstantiationDependentType(), |
14004 | IsPack: AX->containsUnexpandedParameterPack(), |
14005 | TypeConstraintConcept: getCommonDeclChecked(AX->getTypeConstraintConcept(), |
14006 | AY->getTypeConstraintConcept()), |
14007 | TypeConstraintArgs: As); |
14008 | } |
14009 | case Type::IncompleteArray: { |
14010 | const auto *AX = cast<IncompleteArrayType>(X), |
14011 | *AY = cast<IncompleteArrayType>(Y); |
14012 | return Ctx.getIncompleteArrayType( |
14013 | elementType: getCommonArrayElementType(Ctx, AX, QX, AY, QY), |
14014 | ASM: getCommonSizeModifier(AX, AY), elementTypeQuals: getCommonIndexTypeCVRQualifiers(AX, AY)); |
14015 | } |
14016 | case Type::DependentSizedArray: { |
14017 | const auto *AX = cast<DependentSizedArrayType>(X), |
14018 | *AY = cast<DependentSizedArrayType>(Y); |
14019 | return Ctx.getDependentSizedArrayType( |
14020 | elementType: getCommonArrayElementType(Ctx, AX, QX, AY, QY), |
14021 | numElements: getCommonSizeExpr(Ctx, AX, AY), ASM: getCommonSizeModifier(AX, AY), |
14022 | elementTypeQuals: getCommonIndexTypeCVRQualifiers(AX, AY)); |
14023 | } |
14024 | case Type::ConstantArray: { |
14025 | const auto *AX = cast<ConstantArrayType>(X), |
14026 | *AY = cast<ConstantArrayType>(Y); |
14027 | assert(AX->getSize() == AY->getSize()); |
14028 | const Expr *SizeExpr = Ctx.hasSameExpr(X: AX->getSizeExpr(), Y: AY->getSizeExpr()) |
14029 | ? AX->getSizeExpr() |
14030 | : nullptr; |
14031 | return Ctx.getConstantArrayType( |
14032 | EltTy: getCommonArrayElementType(Ctx, AX, QX, AY, QY), ArySizeIn: AX->getSize(), SizeExpr, |
14033 | ASM: getCommonSizeModifier(AX, AY), IndexTypeQuals: getCommonIndexTypeCVRQualifiers(AX, AY)); |
14034 | } |
14035 | case Type::ArrayParameter: { |
14036 | const auto *AX = cast<ArrayParameterType>(X), |
14037 | *AY = cast<ArrayParameterType>(Y); |
14038 | assert(AX->getSize() == AY->getSize()); |
14039 | const Expr *SizeExpr = Ctx.hasSameExpr(X: AX->getSizeExpr(), Y: AY->getSizeExpr()) |
14040 | ? AX->getSizeExpr() |
14041 | : nullptr; |
14042 | auto ArrayTy = Ctx.getConstantArrayType( |
14043 | EltTy: getCommonArrayElementType(Ctx, AX, QX, AY, QY), ArySizeIn: AX->getSize(), SizeExpr, |
14044 | ASM: getCommonSizeModifier(AX, AY), IndexTypeQuals: getCommonIndexTypeCVRQualifiers(AX, AY)); |
14045 | return Ctx.getArrayParameterType(Ty: ArrayTy); |
14046 | } |
14047 | case Type::Atomic: { |
14048 | const auto *AX = cast<AtomicType>(X), *AY = cast<AtomicType>(Y); |
14049 | return Ctx.getAtomicType( |
14050 | T: Ctx.getCommonSugaredType(X: AX->getValueType(), Y: AY->getValueType())); |
14051 | } |
14052 | case Type::Complex: { |
14053 | const auto *CX = cast<ComplexType>(X), *CY = cast<ComplexType>(Y); |
14054 | return Ctx.getComplexType(getCommonArrayElementType(Ctx, CX, QX, CY, QY)); |
14055 | } |
14056 | case Type::Pointer: { |
14057 | const auto *PX = cast<PointerType>(X), *PY = cast<PointerType>(Y); |
14058 | return Ctx.getPointerType(getCommonPointeeType(Ctx, PX, PY)); |
14059 | } |
14060 | case Type::BlockPointer: { |
14061 | const auto *PX = cast<BlockPointerType>(X), *PY = cast<BlockPointerType>(Y); |
14062 | return Ctx.getBlockPointerType(T: getCommonPointeeType(Ctx, PX, PY)); |
14063 | } |
14064 | case Type::ObjCObjectPointer: { |
14065 | const auto *PX = cast<ObjCObjectPointerType>(X), |
14066 | *PY = cast<ObjCObjectPointerType>(Y); |
14067 | return Ctx.getObjCObjectPointerType(ObjectT: getCommonPointeeType(Ctx, PX, PY)); |
14068 | } |
14069 | case Type::MemberPointer: { |
14070 | const auto *PX = cast<MemberPointerType>(X), |
14071 | *PY = cast<MemberPointerType>(Y); |
14072 | assert(declaresSameEntity(PX->getMostRecentCXXRecordDecl(), |
14073 | PY->getMostRecentCXXRecordDecl())); |
14074 | return Ctx.getMemberPointerType( |
14075 | T: getCommonPointeeType(Ctx, PX, PY), |
14076 | Qualifier: getCommonQualifier(Ctx, PX, PY, /*IsSame=*/true), |
14077 | Cls: PX->getMostRecentCXXRecordDecl()); |
14078 | } |
14079 | case Type::LValueReference: { |
14080 | const auto *PX = cast<LValueReferenceType>(X), |
14081 | *PY = cast<LValueReferenceType>(Y); |
14082 | // FIXME: Preserve PointeeTypeAsWritten. |
14083 | return Ctx.getLValueReferenceType(T: getCommonPointeeType(Ctx, PX, PY), |
14084 | SpelledAsLValue: PX->isSpelledAsLValue() || |
14085 | PY->isSpelledAsLValue()); |
14086 | } |
14087 | case Type::RValueReference: { |
14088 | const auto *PX = cast<RValueReferenceType>(X), |
14089 | *PY = cast<RValueReferenceType>(Y); |
14090 | // FIXME: Preserve PointeeTypeAsWritten. |
14091 | return Ctx.getRValueReferenceType(T: getCommonPointeeType(Ctx, PX, PY)); |
14092 | } |
14093 | case Type::DependentAddressSpace: { |
14094 | const auto *PX = cast<DependentAddressSpaceType>(X), |
14095 | *PY = cast<DependentAddressSpaceType>(Y); |
14096 | assert(Ctx.hasSameExpr(PX->getAddrSpaceExpr(), PY->getAddrSpaceExpr())); |
14097 | return Ctx.getDependentAddressSpaceType(PointeeType: getCommonPointeeType(Ctx, PX, PY), |
14098 | AddrSpaceExpr: PX->getAddrSpaceExpr(), |
14099 | AttrLoc: getCommonAttrLoc(PX, PY)); |
14100 | } |
14101 | case Type::FunctionNoProto: { |
14102 | const auto *FX = cast<FunctionNoProtoType>(X), |
14103 | *FY = cast<FunctionNoProtoType>(Y); |
14104 | assert(FX->getExtInfo() == FY->getExtInfo()); |
14105 | return Ctx.getFunctionNoProtoType( |
14106 | Ctx.getCommonSugaredType(X: FX->getReturnType(), Y: FY->getReturnType()), |
14107 | FX->getExtInfo()); |
14108 | } |
14109 | case Type::FunctionProto: { |
14110 | const auto *FX = cast<FunctionProtoType>(X), |
14111 | *FY = cast<FunctionProtoType>(Y); |
14112 | FunctionProtoType::ExtProtoInfo EPIX = FX->getExtProtoInfo(), |
14113 | EPIY = FY->getExtProtoInfo(); |
14114 | assert(EPIX.ExtInfo == EPIY.ExtInfo); |
14115 | assert(EPIX.ExtParameterInfos == EPIY.ExtParameterInfos); |
14116 | assert(EPIX.RefQualifier == EPIY.RefQualifier); |
14117 | assert(EPIX.TypeQuals == EPIY.TypeQuals); |
14118 | assert(EPIX.Variadic == EPIY.Variadic); |
14119 | |
14120 | // FIXME: Can we handle an empty EllipsisLoc? |
14121 | // Use emtpy EllipsisLoc if X and Y differ. |
14122 | |
14123 | EPIX.HasTrailingReturn = EPIX.HasTrailingReturn && EPIY.HasTrailingReturn; |
14124 | |
14125 | QualType R = |
14126 | Ctx.getCommonSugaredType(X: FX->getReturnType(), Y: FY->getReturnType()); |
14127 | auto P = getCommonTypes(Ctx, FX->param_types(), FY->param_types(), |
14128 | /*Unqualified=*/true); |
14129 | |
14130 | SmallVector<QualType, 8> Exceptions; |
14131 | EPIX.ExceptionSpec = Ctx.mergeExceptionSpecs( |
14132 | ESI1: EPIX.ExceptionSpec, ESI2: EPIY.ExceptionSpec, ExceptionTypeStorage&: Exceptions, AcceptDependent: true); |
14133 | return Ctx.getFunctionType(ResultTy: R, Args: P, EPI: EPIX); |
14134 | } |
14135 | case Type::ObjCObject: { |
14136 | const auto *OX = cast<ObjCObjectType>(X), *OY = cast<ObjCObjectType>(Y); |
14137 | assert( |
14138 | std::equal(OX->getProtocols().begin(), OX->getProtocols().end(), |
14139 | OY->getProtocols().begin(), OY->getProtocols().end(), |
14140 | [](const ObjCProtocolDecl *P0, const ObjCProtocolDecl *P1) { |
14141 | return P0->getCanonicalDecl() == P1->getCanonicalDecl(); |
14142 | }) && |
14143 | "protocol lists must be the same"); |
14144 | auto TAs = getCommonTypes(Ctx, OX->getTypeArgsAsWritten(), |
14145 | OY->getTypeArgsAsWritten()); |
14146 | return Ctx.getObjCObjectType( |
14147 | Ctx.getCommonSugaredType(X: OX->getBaseType(), Y: OY->getBaseType()), TAs, |
14148 | OX->getProtocols(), |
14149 | OX->isKindOfTypeAsWritten() && OY->isKindOfTypeAsWritten()); |
14150 | } |
14151 | case Type::ConstantMatrix: { |
14152 | const auto *MX = cast<ConstantMatrixType>(X), |
14153 | *MY = cast<ConstantMatrixType>(Y); |
14154 | assert(MX->getNumRows() == MY->getNumRows()); |
14155 | assert(MX->getNumColumns() == MY->getNumColumns()); |
14156 | return Ctx.getConstantMatrixType(ElementTy: getCommonElementType(Ctx, MX, MY), |
14157 | NumRows: MX->getNumRows(), NumColumns: MX->getNumColumns()); |
14158 | } |
14159 | case Type::DependentSizedMatrix: { |
14160 | const auto *MX = cast<DependentSizedMatrixType>(X), |
14161 | *MY = cast<DependentSizedMatrixType>(Y); |
14162 | assert(Ctx.hasSameExpr(MX->getRowExpr(), MY->getRowExpr())); |
14163 | assert(Ctx.hasSameExpr(MX->getColumnExpr(), MY->getColumnExpr())); |
14164 | return Ctx.getDependentSizedMatrixType( |
14165 | ElementTy: getCommonElementType(Ctx, MX, MY), RowExpr: MX->getRowExpr(), |
14166 | ColumnExpr: MX->getColumnExpr(), AttrLoc: getCommonAttrLoc(MX, MY)); |
14167 | } |
14168 | case Type::Vector: { |
14169 | const auto *VX = cast<VectorType>(X), *VY = cast<VectorType>(Y); |
14170 | assert(VX->getNumElements() == VY->getNumElements()); |
14171 | assert(VX->getVectorKind() == VY->getVectorKind()); |
14172 | return Ctx.getVectorType(vecType: getCommonElementType(Ctx, VX, VY), |
14173 | NumElts: VX->getNumElements(), VecKind: VX->getVectorKind()); |
14174 | } |
14175 | case Type::ExtVector: { |
14176 | const auto *VX = cast<ExtVectorType>(X), *VY = cast<ExtVectorType>(Y); |
14177 | assert(VX->getNumElements() == VY->getNumElements()); |
14178 | return Ctx.getExtVectorType(vecType: getCommonElementType(Ctx, VX, VY), |
14179 | NumElts: VX->getNumElements()); |
14180 | } |
14181 | case Type::DependentSizedExtVector: { |
14182 | const auto *VX = cast<DependentSizedExtVectorType>(X), |
14183 | *VY = cast<DependentSizedExtVectorType>(Y); |
14184 | return Ctx.getDependentSizedExtVectorType(vecType: getCommonElementType(Ctx, VX, VY), |
14185 | SizeExpr: getCommonSizeExpr(Ctx, VX, VY), |
14186 | AttrLoc: getCommonAttrLoc(VX, VY)); |
14187 | } |
14188 | case Type::DependentVector: { |
14189 | const auto *VX = cast<DependentVectorType>(X), |
14190 | *VY = cast<DependentVectorType>(Y); |
14191 | assert(VX->getVectorKind() == VY->getVectorKind()); |
14192 | return Ctx.getDependentVectorType( |
14193 | VecType: getCommonElementType(Ctx, VX, VY), SizeExpr: getCommonSizeExpr(Ctx, VX, VY), |
14194 | AttrLoc: getCommonAttrLoc(VX, VY), VecKind: VX->getVectorKind()); |
14195 | } |
14196 | case Type::InjectedClassName: { |
14197 | const auto *IX = cast<InjectedClassNameType>(X), |
14198 | *IY = cast<InjectedClassNameType>(Y); |
14199 | return Ctx.getInjectedClassNameType( |
14200 | Decl: getCommonDeclChecked(IX->getDecl(), IY->getDecl()), |
14201 | TST: Ctx.getCommonSugaredType(X: IX->getInjectedSpecializationType(), |
14202 | Y: IY->getInjectedSpecializationType())); |
14203 | } |
14204 | case Type::TemplateSpecialization: { |
14205 | const auto *TX = cast<TemplateSpecializationType>(X), |
14206 | *TY = cast<TemplateSpecializationType>(Y); |
14207 | auto As = getCommonTemplateArguments(Ctx, TX->template_arguments(), |
14208 | TY->template_arguments()); |
14209 | return Ctx.getTemplateSpecializationType( |
14210 | ::getCommonTemplateNameChecked(Ctx, X: TX->getTemplateName(), |
14211 | Y: TY->getTemplateName(), |
14212 | /*IgnoreDeduced=*/true), |
14213 | As, /*CanonicalArgs=*/std::nullopt, X->getCanonicalTypeInternal()); |
14214 | } |
14215 | case Type::Decltype: { |
14216 | const auto *DX = cast<DecltypeType>(X); |
14217 | [[maybe_unused]] const auto *DY = cast<DecltypeType>(Y); |
14218 | assert(DX->isDependentType()); |
14219 | assert(DY->isDependentType()); |
14220 | assert(Ctx.hasSameExpr(DX->getUnderlyingExpr(), DY->getUnderlyingExpr())); |
14221 | // As Decltype is not uniqued, building a common type would be wasteful. |
14222 | return QualType(DX, 0); |
14223 | } |
14224 | case Type::PackIndexing: { |
14225 | const auto *DX = cast<PackIndexingType>(X); |
14226 | [[maybe_unused]] const auto *DY = cast<PackIndexingType>(Y); |
14227 | assert(DX->isDependentType()); |
14228 | assert(DY->isDependentType()); |
14229 | assert(Ctx.hasSameExpr(DX->getIndexExpr(), DY->getIndexExpr())); |
14230 | return QualType(DX, 0); |
14231 | } |
14232 | case Type::DependentName: { |
14233 | const auto *NX = cast<DependentNameType>(X), |
14234 | *NY = cast<DependentNameType>(Y); |
14235 | assert(NX->getIdentifier() == NY->getIdentifier()); |
14236 | return Ctx.getDependentNameType( |
14237 | Keyword: getCommonTypeKeyword(NX, NY), |
14238 | NNS: getCommonQualifier(Ctx, NX, NY, /*IsSame=*/true), Name: NX->getIdentifier()); |
14239 | } |
14240 | case Type::DependentTemplateSpecialization: { |
14241 | const auto *TX = cast<DependentTemplateSpecializationType>(X), |
14242 | *TY = cast<DependentTemplateSpecializationType>(Y); |
14243 | auto As = getCommonTemplateArguments(Ctx, TX->template_arguments(), |
14244 | TY->template_arguments()); |
14245 | const DependentTemplateStorage &SX = TX->getDependentTemplateName(), |
14246 | &SY = TY->getDependentTemplateName(); |
14247 | assert(SX.getName() == SY.getName()); |
14248 | DependentTemplateStorage Name( |
14249 | getCommonNNS(Ctx, NNS1: SX.getQualifier(), NNS2: SY.getQualifier(), |
14250 | /*IsSame=*/true), |
14251 | SX.getName(), SX.hasTemplateKeyword() || SY.hasTemplateKeyword()); |
14252 | return Ctx.getDependentTemplateSpecializationType( |
14253 | getCommonTypeKeyword(TX, TY), Name, As); |
14254 | } |
14255 | case Type::UnaryTransform: { |
14256 | const auto *TX = cast<UnaryTransformType>(X), |
14257 | *TY = cast<UnaryTransformType>(Y); |
14258 | assert(TX->getUTTKind() == TY->getUTTKind()); |
14259 | return Ctx.getUnaryTransformType( |
14260 | BaseType: Ctx.getCommonSugaredType(X: TX->getBaseType(), Y: TY->getBaseType()), |
14261 | UnderlyingType: Ctx.getCommonSugaredType(X: TX->getUnderlyingType(), |
14262 | Y: TY->getUnderlyingType()), |
14263 | Kind: TX->getUTTKind()); |
14264 | } |
14265 | case Type::PackExpansion: { |
14266 | const auto *PX = cast<PackExpansionType>(X), |
14267 | *PY = cast<PackExpansionType>(Y); |
14268 | assert(PX->getNumExpansions() == PY->getNumExpansions()); |
14269 | return Ctx.getPackExpansionType( |
14270 | Pattern: Ctx.getCommonSugaredType(X: PX->getPattern(), Y: PY->getPattern()), |
14271 | NumExpansions: PX->getNumExpansions(), ExpectPackInType: false); |
14272 | } |
14273 | case Type::Pipe: { |
14274 | const auto *PX = cast<PipeType>(X), *PY = cast<PipeType>(Y); |
14275 | assert(PX->isReadOnly() == PY->isReadOnly()); |
14276 | auto MP = PX->isReadOnly() ? &ASTContext::getReadPipeType |
14277 | : &ASTContext::getWritePipeType; |
14278 | return (Ctx.*MP)(getCommonElementType(Ctx, PX, PY)); |
14279 | } |
14280 | case Type::TemplateTypeParm: { |
14281 | const auto *TX = cast<TemplateTypeParmType>(X), |
14282 | *TY = cast<TemplateTypeParmType>(Y); |
14283 | assert(TX->getDepth() == TY->getDepth()); |
14284 | assert(TX->getIndex() == TY->getIndex()); |
14285 | assert(TX->isParameterPack() == TY->isParameterPack()); |
14286 | return Ctx.getTemplateTypeParmType( |
14287 | Depth: TX->getDepth(), Index: TX->getIndex(), ParameterPack: TX->isParameterPack(), |
14288 | TTPDecl: getCommonDecl(TX->getDecl(), TY->getDecl())); |
14289 | } |
14290 | } |
14291 | llvm_unreachable("Unknown Type Class"); |
14292 | } |
14293 | |
14294 | static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X, |
14295 | const Type *Y, |
14296 | SplitQualType Underlying) { |
14297 | Type::TypeClass TC = X->getTypeClass(); |
14298 | if (TC != Y->getTypeClass()) |
14299 | return QualType(); |
14300 | switch (TC) { |
14301 | #define UNEXPECTED_TYPE(Class, Kind) \ |
14302 | case Type::Class: \ |
14303 | llvm_unreachable("Unexpected " Kind ": " #Class); |
14304 | #define TYPE(Class, Base) |
14305 | #define DEPENDENT_TYPE(Class, Base) UNEXPECTED_TYPE(Class, "dependent") |
14306 | #include "clang/AST/TypeNodes.inc" |
14307 | |
14308 | #define CANONICAL_TYPE(Class) UNEXPECTED_TYPE(Class, "canonical") |
14309 | CANONICAL_TYPE(Atomic) |
14310 | CANONICAL_TYPE(BitInt) |
14311 | CANONICAL_TYPE(BlockPointer) |
14312 | CANONICAL_TYPE(Builtin) |
14313 | CANONICAL_TYPE(Complex) |
14314 | CANONICAL_TYPE(ConstantArray) |
14315 | CANONICAL_TYPE(ArrayParameter) |
14316 | CANONICAL_TYPE(ConstantMatrix) |
14317 | CANONICAL_TYPE(Enum) |
14318 | CANONICAL_TYPE(ExtVector) |
14319 | CANONICAL_TYPE(FunctionNoProto) |
14320 | CANONICAL_TYPE(FunctionProto) |
14321 | CANONICAL_TYPE(IncompleteArray) |
14322 | CANONICAL_TYPE(HLSLAttributedResource) |
14323 | CANONICAL_TYPE(HLSLInlineSpirv) |
14324 | CANONICAL_TYPE(LValueReference) |
14325 | CANONICAL_TYPE(ObjCInterface) |
14326 | CANONICAL_TYPE(ObjCObject) |
14327 | CANONICAL_TYPE(ObjCObjectPointer) |
14328 | CANONICAL_TYPE(Pipe) |
14329 | CANONICAL_TYPE(Pointer) |
14330 | CANONICAL_TYPE(Record) |
14331 | CANONICAL_TYPE(RValueReference) |
14332 | CANONICAL_TYPE(VariableArray) |
14333 | CANONICAL_TYPE(Vector) |
14334 | #undef CANONICAL_TYPE |
14335 | |
14336 | #undef UNEXPECTED_TYPE |
14337 | |
14338 | case Type::Adjusted: { |
14339 | const auto *AX = cast<AdjustedType>(X), *AY = cast<AdjustedType>(Y); |
14340 | QualType OX = AX->getOriginalType(), OY = AY->getOriginalType(); |
14341 | if (!Ctx.hasSameType(T1: OX, T2: OY)) |
14342 | return QualType(); |
14343 | // FIXME: It's inefficient to have to unify the original types. |
14344 | return Ctx.getAdjustedType(Orig: Ctx.getCommonSugaredType(X: OX, Y: OY), |
14345 | New: Ctx.getQualifiedType(split: Underlying)); |
14346 | } |
14347 | case Type::Decayed: { |
14348 | const auto *DX = cast<DecayedType>(X), *DY = cast<DecayedType>(Y); |
14349 | QualType OX = DX->getOriginalType(), OY = DY->getOriginalType(); |
14350 | if (!Ctx.hasSameType(T1: OX, T2: OY)) |
14351 | return QualType(); |
14352 | // FIXME: It's inefficient to have to unify the original types. |
14353 | return Ctx.getDecayedType(Orig: Ctx.getCommonSugaredType(X: OX, Y: OY), |
14354 | Decayed: Ctx.getQualifiedType(split: Underlying)); |
14355 | } |
14356 | case Type::Attributed: { |
14357 | const auto *AX = cast<AttributedType>(X), *AY = cast<AttributedType>(Y); |
14358 | AttributedType::Kind Kind = AX->getAttrKind(); |
14359 | if (Kind != AY->getAttrKind()) |
14360 | return QualType(); |
14361 | QualType MX = AX->getModifiedType(), MY = AY->getModifiedType(); |
14362 | if (!Ctx.hasSameType(T1: MX, T2: MY)) |
14363 | return QualType(); |
14364 | // FIXME: It's inefficient to have to unify the modified types. |
14365 | return Ctx.getAttributedType(Kind, Ctx.getCommonSugaredType(X: MX, Y: MY), |
14366 | Ctx.getQualifiedType(split: Underlying), |
14367 | AX->getAttr()); |
14368 | } |
14369 | case Type::BTFTagAttributed: { |
14370 | const auto *BX = cast<BTFTagAttributedType>(X); |
14371 | const BTFTypeTagAttr *AX = BX->getAttr(); |
14372 | // The attribute is not uniqued, so just compare the tag. |
14373 | if (AX->getBTFTypeTag() != |
14374 | cast<BTFTagAttributedType>(Y)->getAttr()->getBTFTypeTag()) |
14375 | return QualType(); |
14376 | return Ctx.getBTFTagAttributedType(BTFAttr: AX, Wrapped: Ctx.getQualifiedType(split: Underlying)); |
14377 | } |
14378 | case Type::Auto: { |
14379 | const auto *AX = cast<AutoType>(X), *AY = cast<AutoType>(Y); |
14380 | |
14381 | AutoTypeKeyword KW = AX->getKeyword(); |
14382 | if (KW != AY->getKeyword()) |
14383 | return QualType(); |
14384 | |
14385 | ConceptDecl *CD = ::getCommonDecl(AX->getTypeConstraintConcept(), |
14386 | AY->getTypeConstraintConcept()); |
14387 | SmallVector<TemplateArgument, 8> As; |
14388 | if (CD && |
14389 | getCommonTemplateArguments(Ctx, As, AX->getTypeConstraintArguments(), |
14390 | AY->getTypeConstraintArguments())) { |
14391 | CD = nullptr; // The arguments differ, so make it unconstrained. |
14392 | As.clear(); |
14393 | } |
14394 | |
14395 | // Both auto types can't be dependent, otherwise they wouldn't have been |
14396 | // sugar. This implies they can't contain unexpanded packs either. |
14397 | return Ctx.getAutoType(DeducedType: Ctx.getQualifiedType(split: Underlying), Keyword: AX->getKeyword(), |
14398 | /*IsDependent=*/false, /*IsPack=*/false, TypeConstraintConcept: CD, TypeConstraintArgs: As); |
14399 | } |
14400 | case Type::PackIndexing: |
14401 | case Type::Decltype: |
14402 | return QualType(); |
14403 | case Type::DeducedTemplateSpecialization: |
14404 | // FIXME: Try to merge these. |
14405 | return QualType(); |
14406 | |
14407 | case Type::Elaborated: { |
14408 | const auto *EX = cast<ElaboratedType>(X), *EY = cast<ElaboratedType>(Y); |
14409 | return Ctx.getElaboratedType( |
14410 | Keyword: ::getCommonTypeKeyword(EX, EY), |
14411 | NNS: ::getCommonQualifier(Ctx, EX, EY, /*IsSame=*/false), |
14412 | NamedType: Ctx.getQualifiedType(split: Underlying), |
14413 | OwnedTagDecl: ::getCommonDecl(EX->getOwnedTagDecl(), EY->getOwnedTagDecl())); |
14414 | } |
14415 | case Type::MacroQualified: { |
14416 | const auto *MX = cast<MacroQualifiedType>(X), |
14417 | *MY = cast<MacroQualifiedType>(Y); |
14418 | const IdentifierInfo *IX = MX->getMacroIdentifier(); |
14419 | if (IX != MY->getMacroIdentifier()) |
14420 | return QualType(); |
14421 | return Ctx.getMacroQualifiedType(UnderlyingTy: Ctx.getQualifiedType(split: Underlying), MacroII: IX); |
14422 | } |
14423 | case Type::SubstTemplateTypeParm: { |
14424 | const auto *SX = cast<SubstTemplateTypeParmType>(X), |
14425 | *SY = cast<SubstTemplateTypeParmType>(Y); |
14426 | Decl *CD = |
14427 | ::getCommonDecl(SX->getAssociatedDecl(), SY->getAssociatedDecl()); |
14428 | if (!CD) |
14429 | return QualType(); |
14430 | unsigned Index = SX->getIndex(); |
14431 | if (Index != SY->getIndex()) |
14432 | return QualType(); |
14433 | auto PackIndex = SX->getPackIndex(); |
14434 | if (PackIndex != SY->getPackIndex()) |
14435 | return QualType(); |
14436 | return Ctx.getSubstTemplateTypeParmType(Replacement: Ctx.getQualifiedType(split: Underlying), |
14437 | AssociatedDecl: CD, Index, PackIndex: PackIndex, |
14438 | Final: SX->getFinal() && SY->getFinal()); |
14439 | } |
14440 | case Type::ObjCTypeParam: |
14441 | // FIXME: Try to merge these. |
14442 | return QualType(); |
14443 | case Type::Paren: |
14444 | return Ctx.getParenType(InnerType: Ctx.getQualifiedType(split: Underlying)); |
14445 | |
14446 | case Type::TemplateSpecialization: { |
14447 | const auto *TX = cast<TemplateSpecializationType>(X), |
14448 | *TY = cast<TemplateSpecializationType>(Y); |
14449 | TemplateName CTN = |
14450 | ::getCommonTemplateName(Ctx, X: TX->getTemplateName(), |
14451 | Y: TY->getTemplateName(), /*IgnoreDeduced=*/true); |
14452 | if (!CTN.getAsVoidPointer()) |
14453 | return QualType(); |
14454 | SmallVector<TemplateArgument, 8> As; |
14455 | if (getCommonTemplateArguments(Ctx, As, TX->template_arguments(), |
14456 | TY->template_arguments())) |
14457 | return QualType(); |
14458 | return Ctx.getTemplateSpecializationType(CTN, As, |
14459 | /*CanonicalArgs=*/std::nullopt, |
14460 | Ctx.getQualifiedType(split: Underlying)); |
14461 | } |
14462 | case Type::Typedef: { |
14463 | const auto *TX = cast<TypedefType>(X), *TY = cast<TypedefType>(Y); |
14464 | const TypedefNameDecl *CD = ::getCommonDecl(TX->getDecl(), TY->getDecl()); |
14465 | if (!CD) |
14466 | return QualType(); |
14467 | return Ctx.getTypedefType(Decl: CD, Underlying: Ctx.getQualifiedType(split: Underlying)); |
14468 | } |
14469 | case Type::TypeOf: { |
14470 | // The common sugar between two typeof expressions, where one is |
14471 | // potentially a typeof_unqual and the other is not, we unify to the |
14472 | // qualified type as that retains the most information along with the type. |
14473 | // We only return a typeof_unqual type when both types are unqual types. |
14474 | TypeOfKind Kind = TypeOfKind::Qualified; |
14475 | if (cast<TypeOfType>(X)->getKind() == cast<TypeOfType>(Y)->getKind() && |
14476 | cast<TypeOfType>(X)->getKind() == TypeOfKind::Unqualified) |
14477 | Kind = TypeOfKind::Unqualified; |
14478 | return Ctx.getTypeOfType(tofType: Ctx.getQualifiedType(split: Underlying), Kind); |
14479 | } |
14480 | case Type::TypeOfExpr: |
14481 | return QualType(); |
14482 | |
14483 | case Type::UnaryTransform: { |
14484 | const auto *UX = cast<UnaryTransformType>(X), |
14485 | *UY = cast<UnaryTransformType>(Y); |
14486 | UnaryTransformType::UTTKind KX = UX->getUTTKind(); |
14487 | if (KX != UY->getUTTKind()) |
14488 | return QualType(); |
14489 | QualType BX = UX->getBaseType(), BY = UY->getBaseType(); |
14490 | if (!Ctx.hasSameType(T1: BX, T2: BY)) |
14491 | return QualType(); |
14492 | // FIXME: It's inefficient to have to unify the base types. |
14493 | return Ctx.getUnaryTransformType(BaseType: Ctx.getCommonSugaredType(X: BX, Y: BY), |
14494 | UnderlyingType: Ctx.getQualifiedType(split: Underlying), Kind: KX); |
14495 | } |
14496 | case Type::Using: { |
14497 | const auto *UX = cast<UsingType>(X), *UY = cast<UsingType>(Y); |
14498 | const UsingShadowDecl *CD = |
14499 | ::getCommonDecl(UX->getFoundDecl(), UY->getFoundDecl()); |
14500 | if (!CD) |
14501 | return QualType(); |
14502 | return Ctx.getUsingType(Found: CD, Underlying: Ctx.getQualifiedType(split: Underlying)); |
14503 | } |
14504 | case Type::MemberPointer: { |
14505 | const auto *PX = cast<MemberPointerType>(X), |
14506 | *PY = cast<MemberPointerType>(Y); |
14507 | CXXRecordDecl *Cls = PX->getMostRecentCXXRecordDecl(); |
14508 | assert(Cls == PY->getMostRecentCXXRecordDecl()); |
14509 | return Ctx.getMemberPointerType( |
14510 | T: ::getCommonPointeeType(Ctx, PX, PY), |
14511 | Qualifier: ::getCommonQualifier(Ctx, PX, PY, /*IsSame=*/false), Cls); |
14512 | } |
14513 | case Type::CountAttributed: { |
14514 | const auto *DX = cast<CountAttributedType>(X), |
14515 | *DY = cast<CountAttributedType>(Y); |
14516 | if (DX->isCountInBytes() != DY->isCountInBytes()) |
14517 | return QualType(); |
14518 | if (DX->isOrNull() != DY->isOrNull()) |
14519 | return QualType(); |
14520 | Expr *CEX = DX->getCountExpr(); |
14521 | Expr *CEY = DY->getCountExpr(); |
14522 | llvm::ArrayRef<clang::TypeCoupledDeclRefInfo> CDX = DX->getCoupledDecls(); |
14523 | if (Ctx.hasSameExpr(X: CEX, Y: CEY)) |
14524 | return Ctx.getCountAttributedType(WrappedTy: Ctx.getQualifiedType(split: Underlying), CountExpr: CEX, |
14525 | CountInBytes: DX->isCountInBytes(), OrNull: DX->isOrNull(), |
14526 | DependentDecls: CDX); |
14527 | if (!CEX->isIntegerConstantExpr(Ctx) || !CEY->isIntegerConstantExpr(Ctx)) |
14528 | return QualType(); |
14529 | // Two declarations with the same integer constant may still differ in their |
14530 | // expression pointers, so we need to evaluate them. |
14531 | llvm::APSInt VX = *CEX->getIntegerConstantExpr(Ctx); |
14532 | llvm::APSInt VY = *CEY->getIntegerConstantExpr(Ctx); |
14533 | if (VX != VY) |
14534 | return QualType(); |
14535 | return Ctx.getCountAttributedType(WrappedTy: Ctx.getQualifiedType(split: Underlying), CountExpr: CEX, |
14536 | CountInBytes: DX->isCountInBytes(), OrNull: DX->isOrNull(), |
14537 | DependentDecls: CDX); |
14538 | } |
14539 | } |
14540 | llvm_unreachable("Unhandled Type Class"); |
14541 | } |
14542 | |
14543 | static auto unwrapSugar(SplitQualType &T, Qualifiers &QTotal) { |
14544 | SmallVector<SplitQualType, 8> R; |
14545 | while (true) { |
14546 | QTotal.addConsistentQualifiers(qs: T.Quals); |
14547 | QualType NT = T.Ty->getLocallyUnqualifiedSingleStepDesugaredType(); |
14548 | if (NT == QualType(T.Ty, 0)) |
14549 | break; |
14550 | R.push_back(Elt: T); |
14551 | T = NT.split(); |
14552 | } |
14553 | return R; |
14554 | } |
14555 | |
14556 | QualType ASTContext::getCommonSugaredType(QualType X, QualType Y, |
14557 | bool Unqualified) { |
14558 | assert(Unqualified ? hasSameUnqualifiedType(X, Y) : hasSameType(X, Y)); |
14559 | if (X == Y) |
14560 | return X; |
14561 | if (!Unqualified) { |
14562 | if (X.isCanonical()) |
14563 | return X; |
14564 | if (Y.isCanonical()) |
14565 | return Y; |
14566 | } |
14567 | |
14568 | SplitQualType SX = X.split(), SY = Y.split(); |
14569 | Qualifiers QX, QY; |
14570 | // Desugar SX and SY, setting the sugar and qualifiers aside into Xs and Ys, |
14571 | // until we reach their underlying "canonical nodes". Note these are not |
14572 | // necessarily canonical types, as they may still have sugared properties. |
14573 | // QX and QY will store the sum of all qualifiers in Xs and Ys respectively. |
14574 | auto Xs = ::unwrapSugar(T&: SX, QTotal&: QX), Ys = ::unwrapSugar(T&: SY, QTotal&: QY); |
14575 | |
14576 | // If this is an ArrayType, the element qualifiers are interchangeable with |
14577 | // the top level qualifiers. |
14578 | // * In case the canonical nodes are the same, the elements types are already |
14579 | // the same. |
14580 | // * Otherwise, the element types will be made the same, and any different |
14581 | // element qualifiers will be moved up to the top level qualifiers, per |
14582 | // 'getCommonArrayElementType'. |
14583 | // In both cases, this means there may be top level qualifiers which differ |
14584 | // between X and Y. If so, these differing qualifiers are redundant with the |
14585 | // element qualifiers, and can be removed without changing the canonical type. |
14586 | // The desired behaviour is the same as for the 'Unqualified' case here: |
14587 | // treat the redundant qualifiers as sugar, remove the ones which are not |
14588 | // common to both sides. |
14589 | bool KeepCommonQualifiers = Unqualified || isa<ArrayType>(Val: SX.Ty); |
14590 | |
14591 | if (SX.Ty != SY.Ty) { |
14592 | // The canonical nodes differ. Build a common canonical node out of the two, |
14593 | // unifying their sugar. This may recurse back here. |
14594 | SX.Ty = |
14595 | ::getCommonNonSugarTypeNode(Ctx&: *this, X: SX.Ty, QX, Y: SY.Ty, QY).getTypePtr(); |
14596 | } else { |
14597 | // The canonical nodes were identical: We may have desugared too much. |
14598 | // Add any common sugar back in. |
14599 | while (!Xs.empty() && !Ys.empty() && Xs.back().Ty == Ys.back().Ty) { |
14600 | QX -= SX.Quals; |
14601 | QY -= SY.Quals; |
14602 | SX = Xs.pop_back_val(); |
14603 | SY = Ys.pop_back_val(); |
14604 | } |
14605 | } |
14606 | if (KeepCommonQualifiers) |
14607 | QX = Qualifiers::removeCommonQualifiers(L&: QX, R&: QY); |
14608 | else |
14609 | assert(QX == QY); |
14610 | |
14611 | // Even though the remaining sugar nodes in Xs and Ys differ, some may be |
14612 | // related. Walk up these nodes, unifying them and adding the result. |
14613 | while (!Xs.empty() && !Ys.empty()) { |
14614 | auto Underlying = SplitQualType( |
14615 | SX.Ty, Qualifiers::removeCommonQualifiers(L&: SX.Quals, R&: SY.Quals)); |
14616 | SX = Xs.pop_back_val(); |
14617 | SY = Ys.pop_back_val(); |
14618 | SX.Ty = ::getCommonSugarTypeNode(Ctx&: *this, X: SX.Ty, Y: SY.Ty, Underlying) |
14619 | .getTypePtrOrNull(); |
14620 | // Stop at the first pair which is unrelated. |
14621 | if (!SX.Ty) { |
14622 | SX.Ty = Underlying.Ty; |
14623 | break; |
14624 | } |
14625 | QX -= Underlying.Quals; |
14626 | }; |
14627 | |
14628 | // Add back the missing accumulated qualifiers, which were stripped off |
14629 | // with the sugar nodes we could not unify. |
14630 | QualType R = getQualifiedType(T: SX.Ty, Qs: QX); |
14631 | assert(Unqualified ? hasSameUnqualifiedType(R, X) : hasSameType(R, X)); |
14632 | return R; |
14633 | } |
14634 | |
14635 | QualType ASTContext::getCorrespondingUnsaturatedType(QualType Ty) const { |
14636 | assert(Ty->isFixedPointType()); |
14637 | |
14638 | if (Ty->isUnsaturatedFixedPointType()) |
14639 | return Ty; |
14640 | |
14641 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
14642 | default: |
14643 | llvm_unreachable("Not a saturated fixed point type!"); |
14644 | case BuiltinType::SatShortAccum: |
14645 | return ShortAccumTy; |
14646 | case BuiltinType::SatAccum: |
14647 | return AccumTy; |
14648 | case BuiltinType::SatLongAccum: |
14649 | return LongAccumTy; |
14650 | case BuiltinType::SatUShortAccum: |
14651 | return UnsignedShortAccumTy; |
14652 | case BuiltinType::SatUAccum: |
14653 | return UnsignedAccumTy; |
14654 | case BuiltinType::SatULongAccum: |
14655 | return UnsignedLongAccumTy; |
14656 | case BuiltinType::SatShortFract: |
14657 | return ShortFractTy; |
14658 | case BuiltinType::SatFract: |
14659 | return FractTy; |
14660 | case BuiltinType::SatLongFract: |
14661 | return LongFractTy; |
14662 | case BuiltinType::SatUShortFract: |
14663 | return UnsignedShortFractTy; |
14664 | case BuiltinType::SatUFract: |
14665 | return UnsignedFractTy; |
14666 | case BuiltinType::SatULongFract: |
14667 | return UnsignedLongFractTy; |
14668 | } |
14669 | } |
14670 | |
14671 | QualType ASTContext::getCorrespondingSaturatedType(QualType Ty) const { |
14672 | assert(Ty->isFixedPointType()); |
14673 | |
14674 | if (Ty->isSaturatedFixedPointType()) return Ty; |
14675 | |
14676 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
14677 | default: |
14678 | llvm_unreachable("Not a fixed point type!"); |
14679 | case BuiltinType::ShortAccum: |
14680 | return SatShortAccumTy; |
14681 | case BuiltinType::Accum: |
14682 | return SatAccumTy; |
14683 | case BuiltinType::LongAccum: |
14684 | return SatLongAccumTy; |
14685 | case BuiltinType::UShortAccum: |
14686 | return SatUnsignedShortAccumTy; |
14687 | case BuiltinType::UAccum: |
14688 | return SatUnsignedAccumTy; |
14689 | case BuiltinType::ULongAccum: |
14690 | return SatUnsignedLongAccumTy; |
14691 | case BuiltinType::ShortFract: |
14692 | return SatShortFractTy; |
14693 | case BuiltinType::Fract: |
14694 | return SatFractTy; |
14695 | case BuiltinType::LongFract: |
14696 | return SatLongFractTy; |
14697 | case BuiltinType::UShortFract: |
14698 | return SatUnsignedShortFractTy; |
14699 | case BuiltinType::UFract: |
14700 | return SatUnsignedFractTy; |
14701 | case BuiltinType::ULongFract: |
14702 | return SatUnsignedLongFractTy; |
14703 | } |
14704 | } |
14705 | |
14706 | LangAS ASTContext::getLangASForBuiltinAddressSpace(unsigned AS) const { |
14707 | if (LangOpts.OpenCL) |
14708 | return getTargetInfo().getOpenCLBuiltinAddressSpace(AS); |
14709 | |
14710 | if (LangOpts.CUDA) |
14711 | return getTargetInfo().getCUDABuiltinAddressSpace(AS); |
14712 | |
14713 | return getLangASFromTargetAS(TargetAS: AS); |
14714 | } |
14715 | |
14716 | // Explicitly instantiate this in case a Redeclarable<T> is used from a TU that |
14717 | // doesn't include ASTContext.h |
14718 | template |
14719 | clang::LazyGenerationalUpdatePtr< |
14720 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType |
14721 | clang::LazyGenerationalUpdatePtr< |
14722 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue( |
14723 | const clang::ASTContext &Ctx, Decl *Value); |
14724 | |
14725 | unsigned char ASTContext::getFixedPointScale(QualType Ty) const { |
14726 | assert(Ty->isFixedPointType()); |
14727 | |
14728 | const TargetInfo &Target = getTargetInfo(); |
14729 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
14730 | default: |
14731 | llvm_unreachable("Not a fixed point type!"); |
14732 | case BuiltinType::ShortAccum: |
14733 | case BuiltinType::SatShortAccum: |
14734 | return Target.getShortAccumScale(); |
14735 | case BuiltinType::Accum: |
14736 | case BuiltinType::SatAccum: |
14737 | return Target.getAccumScale(); |
14738 | case BuiltinType::LongAccum: |
14739 | case BuiltinType::SatLongAccum: |
14740 | return Target.getLongAccumScale(); |
14741 | case BuiltinType::UShortAccum: |
14742 | case BuiltinType::SatUShortAccum: |
14743 | return Target.getUnsignedShortAccumScale(); |
14744 | case BuiltinType::UAccum: |
14745 | case BuiltinType::SatUAccum: |
14746 | return Target.getUnsignedAccumScale(); |
14747 | case BuiltinType::ULongAccum: |
14748 | case BuiltinType::SatULongAccum: |
14749 | return Target.getUnsignedLongAccumScale(); |
14750 | case BuiltinType::ShortFract: |
14751 | case BuiltinType::SatShortFract: |
14752 | return Target.getShortFractScale(); |
14753 | case BuiltinType::Fract: |
14754 | case BuiltinType::SatFract: |
14755 | return Target.getFractScale(); |
14756 | case BuiltinType::LongFract: |
14757 | case BuiltinType::SatLongFract: |
14758 | return Target.getLongFractScale(); |
14759 | case BuiltinType::UShortFract: |
14760 | case BuiltinType::SatUShortFract: |
14761 | return Target.getUnsignedShortFractScale(); |
14762 | case BuiltinType::UFract: |
14763 | case BuiltinType::SatUFract: |
14764 | return Target.getUnsignedFractScale(); |
14765 | case BuiltinType::ULongFract: |
14766 | case BuiltinType::SatULongFract: |
14767 | return Target.getUnsignedLongFractScale(); |
14768 | } |
14769 | } |
14770 | |
14771 | unsigned char ASTContext::getFixedPointIBits(QualType Ty) const { |
14772 | assert(Ty->isFixedPointType()); |
14773 | |
14774 | const TargetInfo &Target = getTargetInfo(); |
14775 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
14776 | default: |
14777 | llvm_unreachable("Not a fixed point type!"); |
14778 | case BuiltinType::ShortAccum: |
14779 | case BuiltinType::SatShortAccum: |
14780 | return Target.getShortAccumIBits(); |
14781 | case BuiltinType::Accum: |
14782 | case BuiltinType::SatAccum: |
14783 | return Target.getAccumIBits(); |
14784 | case BuiltinType::LongAccum: |
14785 | case BuiltinType::SatLongAccum: |
14786 | return Target.getLongAccumIBits(); |
14787 | case BuiltinType::UShortAccum: |
14788 | case BuiltinType::SatUShortAccum: |
14789 | return Target.getUnsignedShortAccumIBits(); |
14790 | case BuiltinType::UAccum: |
14791 | case BuiltinType::SatUAccum: |
14792 | return Target.getUnsignedAccumIBits(); |
14793 | case BuiltinType::ULongAccum: |
14794 | case BuiltinType::SatULongAccum: |
14795 | return Target.getUnsignedLongAccumIBits(); |
14796 | case BuiltinType::ShortFract: |
14797 | case BuiltinType::SatShortFract: |
14798 | case BuiltinType::Fract: |
14799 | case BuiltinType::SatFract: |
14800 | case BuiltinType::LongFract: |
14801 | case BuiltinType::SatLongFract: |
14802 | case BuiltinType::UShortFract: |
14803 | case BuiltinType::SatUShortFract: |
14804 | case BuiltinType::UFract: |
14805 | case BuiltinType::SatUFract: |
14806 | case BuiltinType::ULongFract: |
14807 | case BuiltinType::SatULongFract: |
14808 | return 0; |
14809 | } |
14810 | } |
14811 | |
14812 | llvm::FixedPointSemantics |
14813 | ASTContext::getFixedPointSemantics(QualType Ty) const { |
14814 | assert((Ty->isFixedPointType() || Ty->isIntegerType()) && |
14815 | "Can only get the fixed point semantics for a " |
14816 | "fixed point or integer type."); |
14817 | if (Ty->isIntegerType()) |
14818 | return llvm::FixedPointSemantics::GetIntegerSemantics( |
14819 | Width: getIntWidth(T: Ty), IsSigned: Ty->isSignedIntegerType()); |
14820 | |
14821 | bool isSigned = Ty->isSignedFixedPointType(); |
14822 | return llvm::FixedPointSemantics( |
14823 | static_cast<unsigned>(getTypeSize(T: Ty)), getFixedPointScale(Ty), isSigned, |
14824 | Ty->isSaturatedFixedPointType(), |
14825 | !isSigned && getTargetInfo().doUnsignedFixedPointTypesHavePadding()); |
14826 | } |
14827 | |
14828 | llvm::APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const { |
14829 | assert(Ty->isFixedPointType()); |
14830 | return llvm::APFixedPoint::getMax(Sema: getFixedPointSemantics(Ty)); |
14831 | } |
14832 | |
14833 | llvm::APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const { |
14834 | assert(Ty->isFixedPointType()); |
14835 | return llvm::APFixedPoint::getMin(Sema: getFixedPointSemantics(Ty)); |
14836 | } |
14837 | |
14838 | QualType ASTContext::getCorrespondingSignedFixedPointType(QualType Ty) const { |
14839 | assert(Ty->isUnsignedFixedPointType() && |
14840 | "Expected unsigned fixed point type"); |
14841 | |
14842 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
14843 | case BuiltinType::UShortAccum: |
14844 | return ShortAccumTy; |
14845 | case BuiltinType::UAccum: |
14846 | return AccumTy; |
14847 | case BuiltinType::ULongAccum: |
14848 | return LongAccumTy; |
14849 | case BuiltinType::SatUShortAccum: |
14850 | return SatShortAccumTy; |
14851 | case BuiltinType::SatUAccum: |
14852 | return SatAccumTy; |
14853 | case BuiltinType::SatULongAccum: |
14854 | return SatLongAccumTy; |
14855 | case BuiltinType::UShortFract: |
14856 | return ShortFractTy; |
14857 | case BuiltinType::UFract: |
14858 | return FractTy; |
14859 | case BuiltinType::ULongFract: |
14860 | return LongFractTy; |
14861 | case BuiltinType::SatUShortFract: |
14862 | return SatShortFractTy; |
14863 | case BuiltinType::SatUFract: |
14864 | return SatFractTy; |
14865 | case BuiltinType::SatULongFract: |
14866 | return SatLongFractTy; |
14867 | default: |
14868 | llvm_unreachable("Unexpected unsigned fixed point type"); |
14869 | } |
14870 | } |
14871 | |
14872 | // Given a list of FMV features, return a concatenated list of the |
14873 | // corresponding backend features (which may contain duplicates). |
14874 | static std::vector<std::string> getFMVBackendFeaturesFor( |
14875 | const llvm::SmallVectorImpl<StringRef> &FMVFeatStrings) { |
14876 | std::vector<std::string> BackendFeats; |
14877 | llvm::AArch64::ExtensionSet FeatureBits; |
14878 | for (StringRef F : FMVFeatStrings) |
14879 | if (auto FMVExt = llvm::AArch64::parseFMVExtension(F)) |
14880 | if (FMVExt->ID) |
14881 | FeatureBits.enable(*FMVExt->ID); |
14882 | FeatureBits.toLLVMFeatureList(BackendFeats); |
14883 | return BackendFeats; |
14884 | } |
14885 | |
14886 | ParsedTargetAttr |
14887 | ASTContext::filterFunctionTargetAttrs(const TargetAttr *TD) const { |
14888 | assert(TD != nullptr); |
14889 | ParsedTargetAttr ParsedAttr = Target->parseTargetAttr(Str: TD->getFeaturesStr()); |
14890 | |
14891 | llvm::erase_if(C&: ParsedAttr.Features, P: [&](const std::string &Feat) { |
14892 | return !Target->isValidFeatureName(Feature: StringRef{Feat}.substr(Start: 1)); |
14893 | }); |
14894 | return ParsedAttr; |
14895 | } |
14896 | |
14897 | void ASTContext::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, |
14898 | const FunctionDecl *FD) const { |
14899 | if (FD) |
14900 | getFunctionFeatureMap(FeatureMap, GlobalDecl().getWithDecl(FD)); |
14901 | else |
14902 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), |
14903 | CPU: Target->getTargetOpts().CPU, |
14904 | FeatureVec: Target->getTargetOpts().Features); |
14905 | } |
14906 | |
14907 | // Fills in the supplied string map with the set of target features for the |
14908 | // passed in function. |
14909 | void ASTContext::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, |
14910 | GlobalDecl GD) const { |
14911 | StringRef TargetCPU = Target->getTargetOpts().CPU; |
14912 | const FunctionDecl *FD = GD.getDecl()->getAsFunction(); |
14913 | if (const auto *TD = FD->getAttr<TargetAttr>()) { |
14914 | ParsedTargetAttr ParsedAttr = filterFunctionTargetAttrs(TD: TD); |
14915 | |
14916 | // Make a copy of the features as passed on the command line into the |
14917 | // beginning of the additional features from the function to override. |
14918 | // AArch64 handles command line option features in parseTargetAttr(). |
14919 | if (!Target->getTriple().isAArch64()) |
14920 | ParsedAttr.Features.insert( |
14921 | position: ParsedAttr.Features.begin(), |
14922 | first: Target->getTargetOpts().FeaturesAsWritten.begin(), |
14923 | last: Target->getTargetOpts().FeaturesAsWritten.end()); |
14924 | |
14925 | if (ParsedAttr.CPU != ""&& Target->isValidCPUName(Name: ParsedAttr.CPU)) |
14926 | TargetCPU = ParsedAttr.CPU; |
14927 | |
14928 | // Now populate the feature map, first with the TargetCPU which is either |
14929 | // the default or a new one from the target attribute string. Then we'll use |
14930 | // the passed in features (FeaturesAsWritten) along with the new ones from |
14931 | // the attribute. |
14932 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), CPU: TargetCPU, |
14933 | FeatureVec: ParsedAttr.Features); |
14934 | } else if (const auto *SD = FD->getAttr<CPUSpecificAttr>()) { |
14935 | llvm::SmallVector<StringRef, 32> FeaturesTmp; |
14936 | Target->getCPUSpecificCPUDispatchFeatures( |
14937 | Name: SD->getCPUName(GD.getMultiVersionIndex())->getName(), Features&: FeaturesTmp); |
14938 | std::vector<std::string> Features(FeaturesTmp.begin(), FeaturesTmp.end()); |
14939 | Features.insert(position: Features.begin(), |
14940 | first: Target->getTargetOpts().FeaturesAsWritten.begin(), |
14941 | last: Target->getTargetOpts().FeaturesAsWritten.end()); |
14942 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), CPU: TargetCPU, FeatureVec: Features); |
14943 | } else if (const auto *TC = FD->getAttr<TargetClonesAttr>()) { |
14944 | if (Target->getTriple().isAArch64()) { |
14945 | llvm::SmallVector<StringRef, 8> Feats; |
14946 | TC->getFeatures(Feats, GD.getMultiVersionIndex()); |
14947 | std::vector<std::string> Features = getFMVBackendFeaturesFor(FMVFeatStrings: Feats); |
14948 | Features.insert(position: Features.begin(), |
14949 | first: Target->getTargetOpts().FeaturesAsWritten.begin(), |
14950 | last: Target->getTargetOpts().FeaturesAsWritten.end()); |
14951 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), CPU: TargetCPU, FeatureVec: Features); |
14952 | } else if (Target->getTriple().isRISCV()) { |
14953 | StringRef VersionStr = TC->getFeatureStr(GD.getMultiVersionIndex()); |
14954 | std::vector<std::string> Features; |
14955 | if (VersionStr != "default") { |
14956 | ParsedTargetAttr ParsedAttr = Target->parseTargetAttr(Str: VersionStr); |
14957 | Features.insert(position: Features.begin(), first: ParsedAttr.Features.begin(), |
14958 | last: ParsedAttr.Features.end()); |
14959 | } |
14960 | Features.insert(position: Features.begin(), |
14961 | first: Target->getTargetOpts().FeaturesAsWritten.begin(), |
14962 | last: Target->getTargetOpts().FeaturesAsWritten.end()); |
14963 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), CPU: TargetCPU, FeatureVec: Features); |
14964 | } else { |
14965 | std::vector<std::string> Features; |
14966 | StringRef VersionStr = TC->getFeatureStr(GD.getMultiVersionIndex()); |
14967 | if (VersionStr.starts_with(Prefix: "arch=")) |
14968 | TargetCPU = VersionStr.drop_front(N: sizeof("arch=") - 1); |
14969 | else if (VersionStr != "default") |
14970 | Features.push_back(x: (StringRef{"+"} + VersionStr).str()); |
14971 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), CPU: TargetCPU, FeatureVec: Features); |
14972 | } |
14973 | } else if (const auto *TV = FD->getAttr<TargetVersionAttr>()) { |
14974 | std::vector<std::string> Features; |
14975 | if (Target->getTriple().isRISCV()) { |
14976 | ParsedTargetAttr ParsedAttr = Target->parseTargetAttr(Str: TV->getName()); |
14977 | Features.insert(position: Features.begin(), first: ParsedAttr.Features.begin(), |
14978 | last: ParsedAttr.Features.end()); |
14979 | } else { |
14980 | assert(Target->getTriple().isAArch64()); |
14981 | llvm::SmallVector<StringRef, 8> Feats; |
14982 | TV->getFeatures(Feats); |
14983 | Features = getFMVBackendFeaturesFor(FMVFeatStrings: Feats); |
14984 | } |
14985 | Features.insert(position: Features.begin(), |
14986 | first: Target->getTargetOpts().FeaturesAsWritten.begin(), |
14987 | last: Target->getTargetOpts().FeaturesAsWritten.end()); |
14988 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), CPU: TargetCPU, FeatureVec: Features); |
14989 | } else { |
14990 | FeatureMap = Target->getTargetOpts().FeatureMap; |
14991 | } |
14992 | } |
14993 | |
14994 | static SYCLKernelInfo BuildSYCLKernelInfo(ASTContext &Context, |
14995 | CanQualType KernelNameType, |
14996 | const FunctionDecl *FD) { |
14997 | // Host and device compilation may use different ABIs and different ABIs |
14998 | // may allocate name mangling discriminators differently. A discriminator |
14999 | // override is used to ensure consistent discriminator allocation across |
15000 | // host and device compilation. |
15001 | auto DeviceDiscriminatorOverrider = |
15002 | [](ASTContext &Ctx, const NamedDecl *ND) -> UnsignedOrNone { |
15003 | if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: ND)) |
15004 | if (RD->isLambda()) |
15005 | return RD->getDeviceLambdaManglingNumber(); |
15006 | return std::nullopt; |
15007 | }; |
15008 | std::unique_ptr<MangleContext> MC{ItaniumMangleContext::create( |
15009 | Context, Diags&: Context.getDiagnostics(), Discriminator: DeviceDiscriminatorOverrider)}; |
15010 | |
15011 | // Construct a mangled name for the SYCL kernel caller offload entry point. |
15012 | // FIXME: The Itanium typeinfo mangling (_ZTS<type>) is currently used to |
15013 | // name the SYCL kernel caller offload entry point function. This mangling |
15014 | // does not suffice to clearly identify symbols that correspond to SYCL |
15015 | // kernel caller functions, nor is this mangling natural for targets that |
15016 | // use a non-Itanium ABI. |
15017 | std::string Buffer; |
15018 | Buffer.reserve(res: 128); |
15019 | llvm::raw_string_ostream Out(Buffer); |
15020 | MC->mangleCanonicalTypeName(T: KernelNameType, Out); |
15021 | std::string KernelName = Out.str(); |
15022 | |
15023 | return {KernelNameType, FD, KernelName}; |
15024 | } |
15025 | |
15026 | void ASTContext::registerSYCLEntryPointFunction(FunctionDecl *FD) { |
15027 | // If the function declaration to register is invalid or dependent, the |
15028 | // registration attempt is ignored. |
15029 | if (FD->isInvalidDecl() || FD->isTemplated()) |
15030 | return; |
15031 | |
15032 | const auto *SKEPAttr = FD->getAttr<SYCLKernelEntryPointAttr>(); |
15033 | assert(SKEPAttr && "Missing sycl_kernel_entry_point attribute"); |
15034 | |
15035 | // Be tolerant of multiple registration attempts so long as each attempt |
15036 | // is for the same entity. Callers are obligated to detect and diagnose |
15037 | // conflicting kernel names prior to calling this function. |
15038 | CanQualType KernelNameType = getCanonicalType(SKEPAttr->getKernelName()); |
15039 | auto IT = SYCLKernels.find(KernelNameType); |
15040 | assert((IT == SYCLKernels.end() || |
15041 | declaresSameEntity(FD, IT->second.getKernelEntryPointDecl())) && |
15042 | "SYCL kernel name conflict"); |
15043 | (void)IT; |
15044 | SYCLKernels.insert(std::make_pair( |
15045 | KernelNameType, BuildSYCLKernelInfo(*this, KernelNameType, FD))); |
15046 | } |
15047 | |
15048 | const SYCLKernelInfo &ASTContext::getSYCLKernelInfo(QualType T) const { |
15049 | CanQualType KernelNameType = getCanonicalType(T); |
15050 | return SYCLKernels.at(KernelNameType); |
15051 | } |
15052 | |
15053 | const SYCLKernelInfo *ASTContext::findSYCLKernelInfo(QualType T) const { |
15054 | CanQualType KernelNameType = getCanonicalType(T); |
15055 | auto IT = SYCLKernels.find(KernelNameType); |
15056 | if (IT != SYCLKernels.end()) |
15057 | return &IT->second; |
15058 | return nullptr; |
15059 | } |
15060 | |
15061 | OMPTraitInfo &ASTContext::getNewOMPTraitInfo() { |
15062 | OMPTraitInfoVector.emplace_back(new OMPTraitInfo()); |
15063 | return *OMPTraitInfoVector.back(); |
15064 | } |
15065 | |
15066 | const StreamingDiagnostic &clang:: |
15067 | operator<<(const StreamingDiagnostic &DB, |
15068 | const ASTContext::SectionInfo &Section) { |
15069 | if (Section.Decl) |
15070 | return DB << Section.Decl; |
15071 | return DB << "a prior #pragma section"; |
15072 | } |
15073 | |
15074 | bool ASTContext::mayExternalize(const Decl *D) const { |
15075 | bool IsInternalVar = |
15076 | isa<VarDecl>(Val: D) && |
15077 | basicGVALinkageForVariable(Context: *this, VD: cast<VarDecl>(Val: D)) == GVA_Internal; |
15078 | bool IsExplicitDeviceVar = (D->hasAttr<CUDADeviceAttr>() && |
15079 | !D->getAttr<CUDADeviceAttr>()->isImplicit()) || |
15080 | (D->hasAttr<CUDAConstantAttr>() && |
15081 | !D->getAttr<CUDAConstantAttr>()->isImplicit()); |
15082 | // CUDA/HIP: managed variables need to be externalized since it is |
15083 | // a declaration in IR, therefore cannot have internal linkage. Kernels in |
15084 | // anonymous name space needs to be externalized to avoid duplicate symbols. |
15085 | return (IsInternalVar && |
15086 | (D->hasAttr<HIPManagedAttr>() || IsExplicitDeviceVar)) || |
15087 | (D->hasAttr<CUDAGlobalAttr>() && |
15088 | basicGVALinkageForFunction(*this, cast<FunctionDecl>(D)) == |
15089 | GVA_Internal); |
15090 | } |
15091 | |
15092 | bool ASTContext::shouldExternalize(const Decl *D) const { |
15093 | return mayExternalize(D) && |
15094 | (D->hasAttr<HIPManagedAttr>() || D->hasAttr<CUDAGlobalAttr>() || |
15095 | CUDADeviceVarODRUsedByHost.count(cast<VarDecl>(D))); |
15096 | } |
15097 | |
15098 | StringRef ASTContext::getCUIDHash() const { |
15099 | if (!CUIDHash.empty()) |
15100 | return CUIDHash; |
15101 | if (LangOpts.CUID.empty()) |
15102 | return StringRef(); |
15103 | CUIDHash = llvm::utohexstr(X: llvm::MD5Hash(Str: LangOpts.CUID), /*LowerCase=*/true); |
15104 | return CUIDHash; |
15105 | } |
15106 | |
15107 | const CXXRecordDecl * |
15108 | ASTContext::baseForVTableAuthentication(const CXXRecordDecl *ThisClass) { |
15109 | assert(ThisClass); |
15110 | assert(ThisClass->isPolymorphic()); |
15111 | const CXXRecordDecl *PrimaryBase = ThisClass; |
15112 | while (1) { |
15113 | assert(PrimaryBase); |
15114 | assert(PrimaryBase->isPolymorphic()); |
15115 | auto &Layout = getASTRecordLayout(PrimaryBase); |
15116 | auto Base = Layout.getPrimaryBase(); |
15117 | if (!Base || Base == PrimaryBase || !Base->isPolymorphic()) |
15118 | break; |
15119 | PrimaryBase = Base; |
15120 | } |
15121 | return PrimaryBase; |
15122 | } |
15123 | |
15124 | bool ASTContext::useAbbreviatedThunkName(GlobalDecl VirtualMethodDecl, |
15125 | StringRef MangledName) { |
15126 | auto *Method = cast<CXXMethodDecl>(Val: VirtualMethodDecl.getDecl()); |
15127 | assert(Method->isVirtual()); |
15128 | bool DefaultIncludesPointerAuth = |
15129 | LangOpts.PointerAuthCalls || LangOpts.PointerAuthIntrinsics; |
15130 | |
15131 | if (!DefaultIncludesPointerAuth) |
15132 | return true; |
15133 | |
15134 | auto Existing = ThunksToBeAbbreviated.find(VirtualMethodDecl); |
15135 | if (Existing != ThunksToBeAbbreviated.end()) |
15136 | return Existing->second.contains(MangledName.str()); |
15137 | |
15138 | std::unique_ptr<MangleContext> Mangler(createMangleContext()); |
15139 | llvm::StringMap<llvm::SmallVector<std::string, 2>> Thunks; |
15140 | auto VtableContext = getVTableContext(); |
15141 | if (const auto *ThunkInfos = VtableContext->getThunkInfo(GD: VirtualMethodDecl)) { |
15142 | auto *Destructor = dyn_cast<CXXDestructorDecl>(Val: Method); |
15143 | for (const auto &Thunk : *ThunkInfos) { |
15144 | SmallString<256> ElidedName; |
15145 | llvm::raw_svector_ostream ElidedNameStream(ElidedName); |
15146 | if (Destructor) |
15147 | Mangler->mangleCXXDtorThunk(DD: Destructor, Type: VirtualMethodDecl.getDtorType(), |
15148 | Thunk, /* elideOverrideInfo */ ElideOverrideInfo: true, |
15149 | ElidedNameStream); |
15150 | else |
15151 | Mangler->mangleThunk(MD: Method, Thunk, /* elideOverrideInfo */ ElideOverrideInfo: true, |
15152 | ElidedNameStream); |
15153 | SmallString<256> MangledName; |
15154 | llvm::raw_svector_ostream mangledNameStream(MangledName); |
15155 | if (Destructor) |
15156 | Mangler->mangleCXXDtorThunk(DD: Destructor, Type: VirtualMethodDecl.getDtorType(), |
15157 | Thunk, /* elideOverrideInfo */ ElideOverrideInfo: false, |
15158 | mangledNameStream); |
15159 | else |
15160 | Mangler->mangleThunk(MD: Method, Thunk, /* elideOverrideInfo */ ElideOverrideInfo: false, |
15161 | mangledNameStream); |
15162 | |
15163 | Thunks[ElidedName].push_back(Elt: std::string(MangledName)); |
15164 | } |
15165 | } |
15166 | llvm::StringSet<> SimplifiedThunkNames; |
15167 | for (auto &ThunkList : Thunks) { |
15168 | llvm::sort(C&: ThunkList.second); |
15169 | SimplifiedThunkNames.insert(key: ThunkList.second[0]); |
15170 | } |
15171 | bool Result = SimplifiedThunkNames.contains(key: MangledName); |
15172 | ThunksToBeAbbreviated[VirtualMethodDecl] = std::move(SimplifiedThunkNames); |
15173 | return Result; |
15174 | } |
15175 |
Definitions
- FloatingRank
- DenseMapInfo
- getEmptyKey
- getTombstoneKey
- getHashValue
- isEqual
- getDeclLocsForCommentSearch
- getRawCommentForDeclNoCacheImpl
- getRawCommentForDeclNoCache
- addComment
- adjustDeclToTemplate
- getRawCommentForAnyRedecl
- cacheRawCommentForDecl
- addRedeclaredMethods
- attachCommentsToJustParsedDecls
- cloneFullComment
- getLocalCommentForDeclUncached
- getCommentForDecl
- Profile
- getCanonicalTemplateTemplateParmDecl
- findCanonicalTemplateTemplateParmDeclInternal
- insertCanonicalTemplateTemplateParmDeclInternal
- isTypeIgnoredBySanitizer
- getCXXABIKind
- createCXXABI
- getInterpContext
- getParentMapContext
- isAddrSpaceMapManglingEnabled
- ASTContext
- cleanup
- ~ASTContext
- setTraversalScope
- AddDeallocation
- setExternalSource
- PrintStats
- mergeDefinitionIntoModule
- deduplicateMergedDefinitionsFor
- getModulesWithMergedDefinition
- resolve
- addModuleInitializer
- addLazyModuleInitializers
- getModuleInitializers
- setCurrentNamedModule
- isInSameModule
- getExternCContextDecl
- buildBuiltinTemplateDecl
- buildImplicitRecord
- buildImplicitTypedef
- getInt128Decl
- getUInt128Decl
- InitBuiltinType
- InitBuiltinTypes
- getDiagnostics
- getDeclAttrs
- eraseDeclAttrs
- getInstantiatedFromStaticDataMember
- getTemplateOrSpecializationInfo
- setInstantiatedFromStaticDataMember
- setTemplateOrSpecializationInfo
- getInstantiatedFromUsingDecl
- setInstantiatedFromUsingDecl
- getInstantiatedFromUsingEnumDecl
- setInstantiatedFromUsingEnumDecl
- getInstantiatedFromUsingShadowDecl
- setInstantiatedFromUsingShadowDecl
- getInstantiatedFromUnnamedFieldDecl
- setInstantiatedFromUnnamedFieldDecl
- overridden_methods_begin
- overridden_methods_end
- overridden_methods_size
- overridden_methods
- addOverriddenMethod
- getOverriddenMethods
- getRelocationInfoForCXXRecord
- setRelocationInfoForCXXRecord
- addedLocalImportDecl
- getFloatTypeSemantics
- getDeclAlign
- getExnObjectAlignment
- getTypeInfoDataSizeInChars
- getConstantArrayInfoInChars
- getTypeInfoInChars
- getTypeInfoInChars
- isPromotableIntegerType
- isAlignmentRequired
- isAlignmentRequired
- getTypeAlignIfKnown
- getTypeInfo
- getTypeInfoImpl
- getTypeUnadjustedAlign
- getOpenMPDefaultSimdAlign
- toCharUnitsFromBits
- toBits
- getTypeSizeInChars
- getTypeSizeInChars
- getTypeAlignInChars
- getTypeAlignInChars
- getTypeUnadjustedAlignInChars
- getTypeUnadjustedAlignInChars
- getPreferredTypeAlign
- getTargetDefaultAlignForAttributeAligned
- getAlignOfGlobalVar
- getAlignOfGlobalVarInChars
- getMinGlobalAlignOfVar
- getOffsetOfBaseWithVBPtr
- getMemberPointerPathAdjustment
- DeepCollectObjCIvars
- CollectInheritedProtocols
- unionHasUniqueObjectRepresentations
- getSubobjectOffset
- getSubobjectOffset
- getSubobjectSizeInBits
- getSubobjectSizeInBits
- structSubobjectsHaveUniqueObjectRepresentations
- structHasUniqueObjectRepresentations
- hasUniqueObjectRepresentations
- CountNonClassIvars
- isSentinelNullExpr
- getObjCImplementation
- getObjCImplementation
- setObjCImplementation
- setObjCImplementation
- getObjCMethodRedeclaration
- setObjCMethodRedeclaration
- getObjContainingInterface
- getBlockVarCopyInit
- setBlockVarCopyInit
- CreateTypeSourceInfo
- getTrivialTypeSourceInfo
- getASTObjCInterfaceLayout
- getCanonicalTemplateArguments
- canonicalizeTemplateArguments
- getExtQualType
- getAddrSpaceQualType
- removeAddrSpaceQualType
- getPointerAuthVTablePointerDiscriminator
- encodeTypeForFunctionPointerAuth
- getPointerAuthTypeDiscriminator
- getObjCGCQualType
- removePtrSizeAddrSpace
- getCountAttributedType
- adjustType
- adjustFunctionType
- adjustFunctionResultType
- adjustDeducedFunctionResultType
- getFunctionTypeWithExceptionSpec
- hasSameFunctionTypeIgnoringExceptionSpec
- getFunctionTypeWithoutPtrSizes
- hasSameFunctionTypeIgnoringPtrSizes
- getFunctionTypeWithoutParamABIs
- hasSameFunctionTypeIgnoringParamABI
- adjustExceptionSpec
- getComplexType
- getPointerType
- getAdjustedType
- getDecayedType
- getDecayedType
- getArrayParameterType
- getBlockPointerType
- getLValueReferenceType
- getRValueReferenceType
- getMemberPointerType
- getConstantArrayType
- getVariableArrayDecayedType
- getVariableArrayType
- getDependentSizedArrayType
- getIncompleteArrayType
- getBuiltinVectorTypeInfo
- getWebAssemblyExternrefType
- getScalableVectorType
- getVectorType
- getDependentVectorType
- getExtVectorType
- getDependentSizedExtVectorType
- getConstantMatrixType
- getDependentSizedMatrixType
- getDependentAddressSpaceType
- isCanonicalResultType
- getFunctionNoProtoType
- getCanonicalFunctionResultType
- isCanonicalExceptionSpecification
- getFunctionTypeInternal
- getPipeType
- adjustStringLiteralBaseType
- getReadPipeType
- getWritePipeType
- getBitIntType
- getDependentBitIntType
- NeedsInjectedClassNameType
- getInjectedClassNameType
- getTypeDeclTypeSlow
- getTypedefType
- getUsingType
- getRecordType
- getEnumType
- computeBestEnumTypes
- isRepresentableIntegerValue
- getUnresolvedUsingType
- getAttributedType
- getAttributedType
- getAttributedType
- getBTFTagAttributedType
- getHLSLAttributedResourceType
- getHLSLInlineSpirvType
- getSubstTemplateTypeParmType
- getSubstTemplateTypeParmPackType
- getTemplateTypeParmType
- getTemplateSpecializationTypeInfo
- getTemplateSpecializationType
- hasAnyPackExpansions
- getCanonicalTemplateSpecializationType
- getTemplateSpecializationType
- getElaboratedType
- getParenType
- getMacroQualifiedType
- getCanonicalElaboratedTypeKeyword
- getDependentNameType
- getDependentTemplateSpecializationType
- getDependentTemplateSpecializationType
- getInjectedTemplateArg
- getPackExpansionType
- CmpProtocolNames
- areSortedAndUniqued
- SortAndUniqueProtocols
- getObjCObjectType
- getObjCObjectType
- applyObjCProtocolQualifiers
- getObjCTypeParamType
- adjustObjCTypeParamBoundType
- ObjCObjectAdoptsQTypeProtocols
- QIdProtocolsAdoptObjCObjectProtocols
- getObjCObjectPointerType
- getObjCInterfaceType
- getTypeOfExprType
- getTypeOfType
- getReferenceQualifiedType
- getDecltypeType
- getPackIndexingType
- getUnaryTransformType
- getAutoTypeInternal
- getAutoType
- getUnconstrainedType
- getDeducedTemplateSpecializationTypeInternal
- getDeducedTemplateSpecializationType
- getAtomicType
- getAutoDeductType
- getAutoRRefDeductType
- getTagDeclType
- getSizeType
- getSignedSizeType
- getIntMaxType
- getUIntMaxType
- getSignedWCharType
- getUnsignedWCharType
- getIntPtrType
- getUIntPtrType
- getPointerDiffType
- getUnsignedPointerDiffType
- getProcessIDType
- getCanonicalParamType
- getUnqualifiedArrayType
- UnwrapSimilarArrayTypes
- UnwrapSimilarTypes
- hasSimilarType
- hasCvrSimilarType
- getNameForTemplate
- getDefaultTemplateArgumentOrNone
- getCanonicalTemplateName
- hasSameTemplateName
- isSameAssociatedConstraint
- isSameConstraintExpr
- isSameTypeConstraint
- isSameTemplateParameter
- isSameTemplateParameterList
- isSameDefaultTemplateArgument
- getNamespace
- isSameQualifier
- hasSameCudaAttrs
- hasSameOverloadableAttrs
- isSameEntity
- getCanonicalTemplateArgument
- isSameTemplateArgument
- getCanonicalNestedNameSpecifier
- getAsArrayType
- getAdjustedParameterType
- getSignatureParameterType
- getExceptionObjectType
- getArrayDecayedType
- getBaseElementType
- getBaseElementType
- getConstantArrayElementCount
- getArrayInitLoopExprElementCount
- getFloatingRank
- getFloatingTypeOrder
- getFloatingTypeSemanticOrder
- getIntegerRank
- isPromotableBitField
- getPromotedIntegerType
- getInnerObjCOwnership
- getIntegerTypeForEnum
- getIntegerTypeOrder
- getCFConstantStringDecl
- getCFConstantStringTagDecl
- getCFConstantStringType
- getObjCSuperType
- setCFConstantStringType
- getBlockDescriptorType
- getBlockDescriptorExtendedType
- getOpenCLTypeKind
- getOpenCLTypeAddrSpace
- BlockRequiresCopying
- getByrefLifetime
- getNSUIntegerType
- getNSIntegerType
- getObjCInstanceTypeDecl
- isTypeTypedefedAsBOOL
- getObjCEncodingTypeSize
- isMSStaticDataMemberInlineDefinition
- getInlineVariableDefinitionKind
- charUnitsToString
- getObjCEncodingForBlock
- getObjCEncodingForFunctionDecl
- getObjCEncodingForMethodParameter
- getObjCEncodingForMethodDecl
- getObjCPropertyImplDeclForPropertyDecl
- getObjCEncodingForPropertyDecl
- getLegacyIntegralTypeEncoding
- getObjCEncodingForType
- getObjCEncodingForPropertyType
- getObjCEncodingForPrimitiveType
- ObjCEncodingForEnumType
- EncodeBitField
- hasTemplateSpecializationInEncodedString
- getObjCEncodingForTypeImpl
- getObjCEncodingForStructureImpl
- getObjCEncodingForTypeQualifier
- getObjCIdDecl
- getObjCSelDecl
- getObjCClassDecl
- getObjCProtocolDecl
- CreateCharPtrNamedVaListDecl
- CreateMSVaListDecl
- CreateCharPtrBuiltinVaListDecl
- CreateVoidPtrBuiltinVaListDecl
- CreateAArch64ABIBuiltinVaListDecl
- CreatePowerABIBuiltinVaListDecl
- CreateX86_64ABIBuiltinVaListDecl
- CreatePNaClABIBuiltinVaListDecl
- CreateAAPCSABIBuiltinVaListDecl
- CreateSystemZBuiltinVaListDecl
- CreateHexagonBuiltinVaListDecl
- CreateXtensaABIBuiltinVaListDecl
- CreateVaListDecl
- getBuiltinVaListDecl
- getVaListTagDecl
- getBuiltinMSVaListDecl
- canBuiltinBeRedeclared
- setObjCConstantStringInterface
- getOverloadedTemplateName
- getAssumedTemplateName
- getQualifiedTemplateName
- getDependentTemplateName
- getSubstTemplateTemplateParm
- getSubstTemplateTemplateParmPack
- getDeducedTemplateName
- getFromTargetType
- getObjCGCAttrKind
- areCompatVectorTypes
- areCompatMatrixTypes
- areCompatibleVectorTypes
- getSVETypeSize
- areCompatibleSveTypes
- areLaxCompatibleSveTypes
- getRVVTypeSize
- areCompatibleRVVTypes
- areLaxCompatibleRVVTypes
- hasDirectOwnershipQualifier
- ProtocolCompatibleWithProtocol
- ObjCQualifiedClassTypesAreCompatible
- ObjCQualifiedIdTypesAreCompatible
- canAssignObjCInterfaces
- canAssignObjCInterfacesInBlockPointer
- compareObjCProtocolsByName
- getIntersectionOfProtocols
- canAssignObjCObjectTypes
- sameObjCTypeArgs
- areCommonBaseCompatible
- canAssignObjCInterfaces
- areComparableObjCPointerTypes
- canBindObjCObjectType
- typesAreCompatible
- propertyTypesAreCompatible
- typesAreBlockPointerCompatible
- mergeTransparentUnionType
- mergeFunctionParameterTypes
- mergeFunctionTypes
- mergeEnumWithInteger
- mergeTagDefinitions
- mergeTypes
- mergeExtParameterInfo
- ResetObjCLayout
- mergeObjCGCQualifiers
- getIntWidth
- getCorrespondingUnsignedType
- getCorrespondingSignedType
- ~ASTMutationListener
- DeducedReturnType
- DecodeTypeFromStr
- DecodeTypeStr
- GetBuiltinType
- basicGVALinkageForFunction
- adjustGVALinkageForAttributes
- adjustGVALinkageForExternalDefinitionKind
- GetGVALinkageForFunction
- basicGVALinkageForVariable
- GetGVALinkageForVariable
- DeclMustBeEmitted
- forEachMultiversionedFunctionVersion
- getDefaultCallingConvention
- isNearlyEmpty
- getVTableContext
- createMangleContext
- createDeviceMangleContext
- ~CXXABI
- getSideTableAllocatedMemory
- getIntTypeForBitwidth
- getRealTypeForBitwidth
- setManglingNumber
- getManglingNumber
- setStaticLocalNumber
- getStaticLocalNumber
- setIsDestroyingOperatorDelete
- isDestroyingOperatorDelete
- setIsTypeAwareOperatorNewOrDelete
- isTypeAwareOperatorNewOrDelete
- getManglingNumberContext
- getManglingNumberContext
- createMangleNumberingContext
- getCopyConstructorForExceptionObject
- addCopyConstructorForExceptionObject
- addTypedefNameForUnnamedTagDecl
- getTypedefNameForUnnamedTagDecl
- addDeclaratorForUnnamedTagDecl
- getDeclaratorForUnnamedTagDecl
- setParameterIndex
- getParameterIndex
- getStringLiteralArrayType
- getPredefinedStringLiteralFromCache
- getMSGuidDecl
- getUnnamedGlobalConstantDecl
- getTemplateParamObjectDecl
- AtomicUsesUnsupportedLibcall
- ObjCMethodsAreEqual
- getTargetNullPointerValue
- getTargetAddressSpace
- hasSameExpr
- getCommonDecl
- getCommonDecl
- getCommonDeclChecked
- getCommonTemplateName
- getCommonTemplateNameChecked
- getCommonTypes
- getCommonAttrLoc
- getCommonTemplateArgument
- getCommonTemplateArguments
- getCommonTemplateArguments
- getCommonTypeKeyword
- getCommonNNS
- getCommonQualifier
- getCommonElementType
- getCommonArrayElementType
- getCommonPointeeType
- getCommonSizeExpr
- getCommonSizeModifier
- getCommonIndexTypeCVRQualifiers
- mergeTypeLists
- mergeExceptionSpecs
- getCommonNonSugarTypeNode
- getCommonSugarTypeNode
- unwrapSugar
- getCommonSugaredType
- getCorrespondingUnsaturatedType
- getCorrespondingSaturatedType
- getLangASForBuiltinAddressSpace
- getFixedPointScale
- getFixedPointIBits
- getFixedPointSemantics
- getFixedPointMax
- getFixedPointMin
- getCorrespondingSignedFixedPointType
- getFMVBackendFeaturesFor
- filterFunctionTargetAttrs
- getFunctionFeatureMap
- getFunctionFeatureMap
- BuildSYCLKernelInfo
- registerSYCLEntryPointFunction
- getSYCLKernelInfo
- findSYCLKernelInfo
- getNewOMPTraitInfo
- operator<<
- mayExternalize
- shouldExternalize
- getCUIDHash
- baseForVTableAuthentication
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more