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 "CXXABI.h" |
15 | #include "Interp/Context.h" |
16 | #include "clang/AST/APValue.h" |
17 | #include "clang/AST/ASTConcept.h" |
18 | #include "clang/AST/ASTMutationListener.h" |
19 | #include "clang/AST/ASTTypeTraits.h" |
20 | #include "clang/AST/Attr.h" |
21 | #include "clang/AST/AttrIterator.h" |
22 | #include "clang/AST/CharUnits.h" |
23 | #include "clang/AST/Comment.h" |
24 | #include "clang/AST/Decl.h" |
25 | #include "clang/AST/DeclBase.h" |
26 | #include "clang/AST/DeclCXX.h" |
27 | #include "clang/AST/DeclContextInternals.h" |
28 | #include "clang/AST/DeclObjC.h" |
29 | #include "clang/AST/DeclOpenMP.h" |
30 | #include "clang/AST/DeclTemplate.h" |
31 | #include "clang/AST/DeclarationName.h" |
32 | #include "clang/AST/DependenceFlags.h" |
33 | #include "clang/AST/Expr.h" |
34 | #include "clang/AST/ExprCXX.h" |
35 | #include "clang/AST/ExprConcepts.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/StmtOpenACC.h" |
45 | #include "clang/AST/TemplateBase.h" |
46 | #include "clang/AST/TemplateName.h" |
47 | #include "clang/AST/Type.h" |
48 | #include "clang/AST/TypeLoc.h" |
49 | #include "clang/AST/UnresolvedSet.h" |
50 | #include "clang/AST/VTableBuilder.h" |
51 | #include "clang/Basic/AddressSpaces.h" |
52 | #include "clang/Basic/Builtins.h" |
53 | #include "clang/Basic/CommentOptions.h" |
54 | #include "clang/Basic/ExceptionSpecificationType.h" |
55 | #include "clang/Basic/IdentifierTable.h" |
56 | #include "clang/Basic/LLVM.h" |
57 | #include "clang/Basic/LangOptions.h" |
58 | #include "clang/Basic/Linkage.h" |
59 | #include "clang/Basic/Module.h" |
60 | #include "clang/Basic/NoSanitizeList.h" |
61 | #include "clang/Basic/ObjCRuntime.h" |
62 | #include "clang/Basic/ProfileList.h" |
63 | #include "clang/Basic/SourceLocation.h" |
64 | #include "clang/Basic/SourceManager.h" |
65 | #include "clang/Basic/Specifiers.h" |
66 | #include "clang/Basic/TargetCXXABI.h" |
67 | #include "clang/Basic/TargetInfo.h" |
68 | #include "clang/Basic/XRayLists.h" |
69 | #include "llvm/ADT/APFixedPoint.h" |
70 | #include "llvm/ADT/APInt.h" |
71 | #include "llvm/ADT/APSInt.h" |
72 | #include "llvm/ADT/ArrayRef.h" |
73 | #include "llvm/ADT/DenseMap.h" |
74 | #include "llvm/ADT/DenseSet.h" |
75 | #include "llvm/ADT/FoldingSet.h" |
76 | #include "llvm/ADT/PointerUnion.h" |
77 | #include "llvm/ADT/STLExtras.h" |
78 | #include "llvm/ADT/SmallPtrSet.h" |
79 | #include "llvm/ADT/SmallVector.h" |
80 | #include "llvm/ADT/StringExtras.h" |
81 | #include "llvm/ADT/StringRef.h" |
82 | #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" |
83 | #include "llvm/Support/Capacity.h" |
84 | #include "llvm/Support/Casting.h" |
85 | #include "llvm/Support/Compiler.h" |
86 | #include "llvm/Support/ErrorHandling.h" |
87 | #include "llvm/Support/MD5.h" |
88 | #include "llvm/Support/MathExtras.h" |
89 | #include "llvm/Support/raw_ostream.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 | /// \returns The locations that are relevant when searching for Doc comments |
117 | /// related to \p D. |
118 | static SmallVector<SourceLocation, 2> |
119 | (const Decl *D, SourceManager &SourceMgr) { |
120 | assert(D); |
121 | |
122 | // User can not attach documentation to implicit declarations. |
123 | if (D->isImplicit()) |
124 | return {}; |
125 | |
126 | // User can not attach documentation to implicit instantiations. |
127 | if (const auto *FD = dyn_cast<FunctionDecl>(Val: D)) { |
128 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
129 | return {}; |
130 | } |
131 | |
132 | if (const auto *VD = dyn_cast<VarDecl>(Val: D)) { |
133 | if (VD->isStaticDataMember() && |
134 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
135 | return {}; |
136 | } |
137 | |
138 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(Val: D)) { |
139 | if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
140 | return {}; |
141 | } |
142 | |
143 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) { |
144 | TemplateSpecializationKind TSK = CTSD->getSpecializationKind(); |
145 | if (TSK == TSK_ImplicitInstantiation || |
146 | TSK == TSK_Undeclared) |
147 | return {}; |
148 | } |
149 | |
150 | if (const auto *ED = dyn_cast<EnumDecl>(Val: D)) { |
151 | if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
152 | return {}; |
153 | } |
154 | if (const auto *TD = dyn_cast<TagDecl>(Val: D)) { |
155 | // When tag declaration (but not definition!) is part of the |
156 | // decl-specifier-seq of some other declaration, it doesn't get comment |
157 | if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition()) |
158 | return {}; |
159 | } |
160 | // TODO: handle comments for function parameters properly. |
161 | if (isa<ParmVarDecl>(Val: D)) |
162 | return {}; |
163 | |
164 | // TODO: we could look up template parameter documentation in the template |
165 | // documentation. |
166 | if (isa<TemplateTypeParmDecl>(Val: D) || |
167 | isa<NonTypeTemplateParmDecl>(Val: D) || |
168 | isa<TemplateTemplateParmDecl>(Val: D)) |
169 | return {}; |
170 | |
171 | SmallVector<SourceLocation, 2> Locations; |
172 | // Find declaration location. |
173 | // For Objective-C declarations we generally don't expect to have multiple |
174 | // declarators, thus use declaration starting location as the "declaration |
175 | // location". |
176 | // For all other declarations multiple declarators are used quite frequently, |
177 | // so we use the location of the identifier as the "declaration location". |
178 | SourceLocation BaseLocation; |
179 | if (isa<ObjCMethodDecl>(Val: D) || isa<ObjCContainerDecl>(Val: D) || |
180 | isa<ObjCPropertyDecl>(Val: D) || isa<RedeclarableTemplateDecl>(Val: D) || |
181 | isa<ClassTemplateSpecializationDecl>(Val: D) || |
182 | // Allow association with Y across {} in `typedef struct X {} Y`. |
183 | isa<TypedefDecl>(Val: D)) |
184 | BaseLocation = D->getBeginLoc(); |
185 | else |
186 | BaseLocation = D->getLocation(); |
187 | |
188 | if (!D->getLocation().isMacroID()) { |
189 | Locations.emplace_back(Args&: BaseLocation); |
190 | } else { |
191 | const auto *DeclCtx = D->getDeclContext(); |
192 | |
193 | // When encountering definitions generated from a macro (that are not |
194 | // contained by another declaration in the macro) we need to try and find |
195 | // the comment at the location of the expansion but if there is no comment |
196 | // there we should retry to see if there is a comment inside the macro as |
197 | // well. To this end we return first BaseLocation to first look at the |
198 | // expansion site, the second value is the spelling location of the |
199 | // beginning of the declaration defined inside the macro. |
200 | if (!(DeclCtx && |
201 | Decl::castFromDeclContext(DeclCtx)->getLocation().isMacroID())) { |
202 | Locations.emplace_back(Args: SourceMgr.getExpansionLoc(Loc: BaseLocation)); |
203 | } |
204 | |
205 | // We use Decl::getBeginLoc() and not just BaseLocation here to ensure that |
206 | // we don't refer to the macro argument location at the expansion site (this |
207 | // can happen if the name's spelling is provided via macro argument), and |
208 | // always to the declaration itself. |
209 | Locations.emplace_back(Args: SourceMgr.getSpellingLoc(Loc: D->getBeginLoc())); |
210 | } |
211 | |
212 | return Locations; |
213 | } |
214 | |
215 | RawComment *ASTContext::( |
216 | const Decl *D, const SourceLocation RepresentativeLocForDecl, |
217 | const std::map<unsigned, RawComment *> &) const { |
218 | // If the declaration doesn't map directly to a location in a file, we |
219 | // can't find the comment. |
220 | if (RepresentativeLocForDecl.isInvalid() || |
221 | !RepresentativeLocForDecl.isFileID()) |
222 | return nullptr; |
223 | |
224 | // If there are no comments anywhere, we won't find anything. |
225 | if (CommentsInTheFile.empty()) |
226 | return nullptr; |
227 | |
228 | // Decompose the location for the declaration and find the beginning of the |
229 | // file buffer. |
230 | const std::pair<FileID, unsigned> DeclLocDecomp = |
231 | SourceMgr.getDecomposedLoc(Loc: RepresentativeLocForDecl); |
232 | |
233 | // Slow path. |
234 | auto = |
235 | CommentsInTheFile.lower_bound(x: DeclLocDecomp.second); |
236 | |
237 | // First check whether we have a trailing comment. |
238 | if (OffsetCommentBehindDecl != CommentsInTheFile.end()) { |
239 | RawComment * = OffsetCommentBehindDecl->second; |
240 | if ((CommentBehindDecl->isDocumentation() || |
241 | LangOpts.CommentOpts.ParseAllComments) && |
242 | CommentBehindDecl->isTrailingComment() && |
243 | (isa<FieldDecl>(Val: D) || isa<EnumConstantDecl>(Val: D) || isa<VarDecl>(Val: D) || |
244 | isa<ObjCMethodDecl>(Val: D) || isa<ObjCPropertyDecl>(Val: D))) { |
245 | |
246 | // Check that Doxygen trailing comment comes after the declaration, starts |
247 | // on the same line and in the same file as the declaration. |
248 | if (SourceMgr.getLineNumber(FID: DeclLocDecomp.first, FilePos: DeclLocDecomp.second) == |
249 | Comments.getCommentBeginLine(C: CommentBehindDecl, File: DeclLocDecomp.first, |
250 | Offset: OffsetCommentBehindDecl->first)) { |
251 | return CommentBehindDecl; |
252 | } |
253 | } |
254 | } |
255 | |
256 | // The comment just after the declaration was not a trailing comment. |
257 | // Let's look at the previous comment. |
258 | if (OffsetCommentBehindDecl == CommentsInTheFile.begin()) |
259 | return nullptr; |
260 | |
261 | auto = --OffsetCommentBehindDecl; |
262 | RawComment * = OffsetCommentBeforeDecl->second; |
263 | |
264 | // Check that we actually have a non-member Doxygen comment. |
265 | if (!(CommentBeforeDecl->isDocumentation() || |
266 | LangOpts.CommentOpts.ParseAllComments) || |
267 | CommentBeforeDecl->isTrailingComment()) |
268 | return nullptr; |
269 | |
270 | // Decompose the end of the comment. |
271 | const unsigned = |
272 | Comments.getCommentEndOffset(C: CommentBeforeDecl); |
273 | |
274 | // Get the corresponding buffer. |
275 | bool Invalid = false; |
276 | const char *Buffer = SourceMgr.getBufferData(FID: DeclLocDecomp.first, |
277 | Invalid: &Invalid).data(); |
278 | if (Invalid) |
279 | return nullptr; |
280 | |
281 | // Extract text between the comment and declaration. |
282 | StringRef Text(Buffer + CommentEndOffset, |
283 | DeclLocDecomp.second - CommentEndOffset); |
284 | |
285 | // There should be no other declarations or preprocessor directives between |
286 | // comment and declaration. |
287 | if (Text.find_last_of(Chars: ";{}#@" ) != StringRef::npos) |
288 | return nullptr; |
289 | |
290 | return CommentBeforeDecl; |
291 | } |
292 | |
293 | RawComment *ASTContext::(const Decl *D) const { |
294 | const auto DeclLocs = getDeclLocsForCommentSearch(D, SourceMgr); |
295 | |
296 | for (const auto DeclLoc : DeclLocs) { |
297 | // If the declaration doesn't map directly to a location in a file, we |
298 | // can't find the comment. |
299 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
300 | continue; |
301 | |
302 | if (ExternalSource && !CommentsLoaded) { |
303 | ExternalSource->ReadComments(); |
304 | CommentsLoaded = true; |
305 | } |
306 | |
307 | if (Comments.empty()) |
308 | continue; |
309 | |
310 | const FileID File = SourceMgr.getDecomposedLoc(Loc: DeclLoc).first; |
311 | if (!File.isValid()) |
312 | continue; |
313 | |
314 | const auto = Comments.getCommentsInFile(File); |
315 | if (!CommentsInThisFile || CommentsInThisFile->empty()) |
316 | continue; |
317 | |
318 | if (RawComment * = |
319 | getRawCommentForDeclNoCacheImpl(D, RepresentativeLocForDecl: DeclLoc, CommentsInTheFile: *CommentsInThisFile)) |
320 | return Comment; |
321 | } |
322 | |
323 | return nullptr; |
324 | } |
325 | |
326 | void ASTContext::(const RawComment &RC) { |
327 | assert(LangOpts.RetainCommentsFromSystemHeaders || |
328 | !SourceMgr.isInSystemHeader(RC.getSourceRange().getBegin())); |
329 | Comments.addComment(RC, CommentOpts: LangOpts.CommentOpts, Allocator&: BumpAlloc); |
330 | } |
331 | |
332 | /// If we have a 'templated' declaration for a template, adjust 'D' to |
333 | /// refer to the actual template. |
334 | /// If we have an implicit instantiation, adjust 'D' to refer to template. |
335 | static const Decl &adjustDeclToTemplate(const Decl &D) { |
336 | if (const auto *FD = dyn_cast<FunctionDecl>(Val: &D)) { |
337 | // Is this function declaration part of a function template? |
338 | if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
339 | return *FTD; |
340 | |
341 | // Nothing to do if function is not an implicit instantiation. |
342 | if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
343 | return D; |
344 | |
345 | // Function is an implicit instantiation of a function template? |
346 | if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) |
347 | return *FTD; |
348 | |
349 | // Function is instantiated from a member definition of a class template? |
350 | if (const FunctionDecl *MemberDecl = |
351 | FD->getInstantiatedFromMemberFunction()) |
352 | return *MemberDecl; |
353 | |
354 | return D; |
355 | } |
356 | if (const auto *VD = dyn_cast<VarDecl>(Val: &D)) { |
357 | // Static data member is instantiated from a member definition of a class |
358 | // template? |
359 | if (VD->isStaticDataMember()) |
360 | if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember()) |
361 | return *MemberDecl; |
362 | |
363 | return D; |
364 | } |
365 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(Val: &D)) { |
366 | // Is this class declaration part of a class template? |
367 | if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate()) |
368 | return *CTD; |
369 | |
370 | // Class is an implicit instantiation of a class template or partial |
371 | // specialization? |
372 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: CRD)) { |
373 | if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation) |
374 | return D; |
375 | llvm::PointerUnion<ClassTemplateDecl *, |
376 | ClassTemplatePartialSpecializationDecl *> |
377 | PU = CTSD->getSpecializedTemplateOrPartial(); |
378 | return PU.is<ClassTemplateDecl *>() |
379 | ? *static_cast<const Decl *>(PU.get<ClassTemplateDecl *>()) |
380 | : *static_cast<const Decl *>( |
381 | PU.get<ClassTemplatePartialSpecializationDecl *>()); |
382 | } |
383 | |
384 | // Class is instantiated from a member definition of a class template? |
385 | if (const MemberSpecializationInfo *Info = |
386 | CRD->getMemberSpecializationInfo()) |
387 | return *Info->getInstantiatedFrom(); |
388 | |
389 | return D; |
390 | } |
391 | if (const auto *ED = dyn_cast<EnumDecl>(Val: &D)) { |
392 | // Enum is instantiated from a member definition of a class template? |
393 | if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum()) |
394 | return *MemberDecl; |
395 | |
396 | return D; |
397 | } |
398 | // FIXME: Adjust alias templates? |
399 | return D; |
400 | } |
401 | |
402 | const RawComment *ASTContext::( |
403 | const Decl *D, |
404 | const Decl **OriginalDecl) const { |
405 | if (!D) { |
406 | if (OriginalDecl) |
407 | OriginalDecl = nullptr; |
408 | return nullptr; |
409 | } |
410 | |
411 | D = &adjustDeclToTemplate(D: *D); |
412 | |
413 | // Any comment directly attached to D? |
414 | { |
415 | auto = DeclRawComments.find(Val: D); |
416 | if (DeclComment != DeclRawComments.end()) { |
417 | if (OriginalDecl) |
418 | *OriginalDecl = D; |
419 | return DeclComment->second; |
420 | } |
421 | } |
422 | |
423 | // Any comment attached to any redeclaration of D? |
424 | const Decl *CanonicalD = D->getCanonicalDecl(); |
425 | if (!CanonicalD) |
426 | return nullptr; |
427 | |
428 | { |
429 | auto = RedeclChainComments.find(Val: CanonicalD); |
430 | if (RedeclComment != RedeclChainComments.end()) { |
431 | if (OriginalDecl) |
432 | *OriginalDecl = RedeclComment->second; |
433 | auto = DeclRawComments.find(Val: RedeclComment->second); |
434 | assert(CommentAtRedecl != DeclRawComments.end() && |
435 | "This decl is supposed to have comment attached." ); |
436 | return CommentAtRedecl->second; |
437 | } |
438 | } |
439 | |
440 | // Any redeclarations of D that we haven't checked for comments yet? |
441 | // We can't use DenseMap::iterator directly since it'd get invalid. |
442 | auto LastCheckedRedecl = [this, CanonicalD]() -> const Decl * { |
443 | return CommentlessRedeclChains.lookup(Val: CanonicalD); |
444 | }(); |
445 | |
446 | for (const auto Redecl : D->redecls()) { |
447 | assert(Redecl); |
448 | // Skip all redeclarations that have been checked previously. |
449 | if (LastCheckedRedecl) { |
450 | if (LastCheckedRedecl == Redecl) { |
451 | LastCheckedRedecl = nullptr; |
452 | } |
453 | continue; |
454 | } |
455 | const RawComment * = getRawCommentForDeclNoCache(D: Redecl); |
456 | if (RedeclComment) { |
457 | cacheRawCommentForDecl(OriginalD: *Redecl, Comment: *RedeclComment); |
458 | if (OriginalDecl) |
459 | *OriginalDecl = Redecl; |
460 | return RedeclComment; |
461 | } |
462 | CommentlessRedeclChains[CanonicalD] = Redecl; |
463 | } |
464 | |
465 | if (OriginalDecl) |
466 | *OriginalDecl = nullptr; |
467 | return nullptr; |
468 | } |
469 | |
470 | void ASTContext::(const Decl &OriginalD, |
471 | const RawComment &) const { |
472 | assert(Comment.isDocumentation() || LangOpts.CommentOpts.ParseAllComments); |
473 | DeclRawComments.try_emplace(Key: &OriginalD, Args: &Comment); |
474 | const Decl *const CanonicalDecl = OriginalD.getCanonicalDecl(); |
475 | RedeclChainComments.try_emplace(Key: CanonicalDecl, Args: &OriginalD); |
476 | CommentlessRedeclChains.erase(Val: CanonicalDecl); |
477 | } |
478 | |
479 | static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod, |
480 | SmallVectorImpl<const NamedDecl *> &Redeclared) { |
481 | const DeclContext *DC = ObjCMethod->getDeclContext(); |
482 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(DC)) { |
483 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); |
484 | if (!ID) |
485 | return; |
486 | // Add redeclared method here. |
487 | for (const auto *Ext : ID->known_extensions()) { |
488 | if (ObjCMethodDecl *RedeclaredMethod = |
489 | Ext->getMethod(ObjCMethod->getSelector(), |
490 | ObjCMethod->isInstanceMethod())) |
491 | Redeclared.push_back(RedeclaredMethod); |
492 | } |
493 | } |
494 | } |
495 | |
496 | void ASTContext::(ArrayRef<Decl *> Decls, |
497 | const Preprocessor *PP) { |
498 | if (Comments.empty() || Decls.empty()) |
499 | return; |
500 | |
501 | FileID File; |
502 | for (const Decl *D : Decls) { |
503 | if (D->isInvalidDecl()) |
504 | continue; |
505 | |
506 | D = &adjustDeclToTemplate(D: *D); |
507 | SourceLocation Loc = D->getLocation(); |
508 | if (Loc.isValid()) { |
509 | // See if there are any new comments that are not attached to a decl. |
510 | // The location doesn't have to be precise - we care only about the file. |
511 | File = SourceMgr.getDecomposedLoc(Loc).first; |
512 | break; |
513 | } |
514 | } |
515 | |
516 | if (File.isInvalid()) |
517 | return; |
518 | |
519 | auto = Comments.getCommentsInFile(File); |
520 | if (!CommentsInThisFile || CommentsInThisFile->empty() || |
521 | CommentsInThisFile->rbegin()->second->isAttached()) |
522 | return; |
523 | |
524 | // There is at least one comment not attached to a decl. |
525 | // Maybe it should be attached to one of Decls? |
526 | // |
527 | // Note that this way we pick up not only comments that precede the |
528 | // declaration, but also comments that *follow* the declaration -- thanks to |
529 | // the lookahead in the lexer: we've consumed the semicolon and looked |
530 | // ahead through comments. |
531 | for (const Decl *D : Decls) { |
532 | assert(D); |
533 | if (D->isInvalidDecl()) |
534 | continue; |
535 | |
536 | D = &adjustDeclToTemplate(D: *D); |
537 | |
538 | if (DeclRawComments.count(Val: D) > 0) |
539 | continue; |
540 | |
541 | const auto DeclLocs = getDeclLocsForCommentSearch(D, SourceMgr); |
542 | |
543 | for (const auto DeclLoc : DeclLocs) { |
544 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
545 | continue; |
546 | |
547 | if (RawComment *const = getRawCommentForDeclNoCacheImpl( |
548 | D, RepresentativeLocForDecl: DeclLoc, CommentsInTheFile: *CommentsInThisFile)) { |
549 | cacheRawCommentForDecl(OriginalD: *D, Comment: *DocComment); |
550 | comments::FullComment *FC = DocComment->parse(Context: *this, PP, D); |
551 | ParsedComments[D->getCanonicalDecl()] = FC; |
552 | break; |
553 | } |
554 | } |
555 | } |
556 | } |
557 | |
558 | comments::FullComment *ASTContext::(comments::FullComment *FC, |
559 | const Decl *D) const { |
560 | auto *ThisDeclInfo = new (*this) comments::DeclInfo; |
561 | ThisDeclInfo->CommentDecl = D; |
562 | ThisDeclInfo->IsFilled = false; |
563 | ThisDeclInfo->fill(); |
564 | ThisDeclInfo->CommentDecl = FC->getDecl(); |
565 | if (!ThisDeclInfo->TemplateParameters) |
566 | ThisDeclInfo->TemplateParameters = FC->getDeclInfo()->TemplateParameters; |
567 | comments::FullComment *CFC = |
568 | new (*this) comments::FullComment(FC->getBlocks(), |
569 | ThisDeclInfo); |
570 | return CFC; |
571 | } |
572 | |
573 | comments::FullComment *ASTContext::(const Decl *D) const { |
574 | const RawComment *RC = getRawCommentForDeclNoCache(D); |
575 | return RC ? RC->parse(Context: *this, PP: nullptr, D) : nullptr; |
576 | } |
577 | |
578 | comments::FullComment *ASTContext::( |
579 | const Decl *D, |
580 | const Preprocessor *PP) const { |
581 | if (!D || D->isInvalidDecl()) |
582 | return nullptr; |
583 | D = &adjustDeclToTemplate(D: *D); |
584 | |
585 | const Decl *Canonical = D->getCanonicalDecl(); |
586 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = |
587 | ParsedComments.find(Val: Canonical); |
588 | |
589 | if (Pos != ParsedComments.end()) { |
590 | if (Canonical != D) { |
591 | comments::FullComment *FC = Pos->second; |
592 | comments::FullComment *CFC = cloneFullComment(FC, D); |
593 | return CFC; |
594 | } |
595 | return Pos->second; |
596 | } |
597 | |
598 | const Decl *OriginalDecl = nullptr; |
599 | |
600 | const RawComment *RC = getRawCommentForAnyRedecl(D, OriginalDecl: &OriginalDecl); |
601 | if (!RC) { |
602 | if (isa<ObjCMethodDecl>(Val: D) || isa<FunctionDecl>(Val: D)) { |
603 | SmallVector<const NamedDecl*, 8> Overridden; |
604 | const auto *OMD = dyn_cast<ObjCMethodDecl>(Val: D); |
605 | if (OMD && OMD->isPropertyAccessor()) |
606 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
607 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) |
608 | return cloneFullComment(FC, D); |
609 | if (OMD) |
610 | addRedeclaredMethods(ObjCMethod: OMD, Redeclared&: Overridden); |
611 | getOverriddenMethods(Method: dyn_cast<NamedDecl>(Val: D), Overridden); |
612 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) |
613 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) |
614 | return cloneFullComment(FC, D); |
615 | } |
616 | else if (const auto *TD = dyn_cast<TypedefNameDecl>(Val: D)) { |
617 | // Attach any tag type's documentation to its typedef if latter |
618 | // does not have one of its own. |
619 | QualType QT = TD->getUnderlyingType(); |
620 | if (const auto *TT = QT->getAs<TagType>()) |
621 | if (const Decl *TD = TT->getDecl()) |
622 | if (comments::FullComment *FC = getCommentForDecl(D: TD, PP)) |
623 | return cloneFullComment(FC, D); |
624 | } |
625 | else if (const auto *IC = dyn_cast<ObjCInterfaceDecl>(Val: D)) { |
626 | while (IC->getSuperClass()) { |
627 | IC = IC->getSuperClass(); |
628 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
629 | return cloneFullComment(FC, D); |
630 | } |
631 | } |
632 | else if (const auto *CD = dyn_cast<ObjCCategoryDecl>(Val: D)) { |
633 | if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) |
634 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
635 | return cloneFullComment(FC, D); |
636 | } |
637 | else if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: D)) { |
638 | if (!(RD = RD->getDefinition())) |
639 | return nullptr; |
640 | // Check non-virtual bases. |
641 | for (const auto &I : RD->bases()) { |
642 | if (I.isVirtual() || (I.getAccessSpecifier() != AS_public)) |
643 | continue; |
644 | QualType Ty = I.getType(); |
645 | if (Ty.isNull()) |
646 | continue; |
647 | if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) { |
648 | if (!(NonVirtualBase= NonVirtualBase->getDefinition())) |
649 | continue; |
650 | |
651 | if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) |
652 | return cloneFullComment(FC, D); |
653 | } |
654 | } |
655 | // Check virtual bases. |
656 | for (const auto &I : RD->vbases()) { |
657 | if (I.getAccessSpecifier() != AS_public) |
658 | continue; |
659 | QualType Ty = I.getType(); |
660 | if (Ty.isNull()) |
661 | continue; |
662 | if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { |
663 | if (!(VirtualBase= VirtualBase->getDefinition())) |
664 | continue; |
665 | if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) |
666 | return cloneFullComment(FC, D); |
667 | } |
668 | } |
669 | } |
670 | return nullptr; |
671 | } |
672 | |
673 | // If the RawComment was attached to other redeclaration of this Decl, we |
674 | // should parse the comment in context of that other Decl. This is important |
675 | // because comments can contain references to parameter names which can be |
676 | // different across redeclarations. |
677 | if (D != OriginalDecl && OriginalDecl) |
678 | return getCommentForDecl(D: OriginalDecl, PP); |
679 | |
680 | comments::FullComment *FC = RC->parse(Context: *this, PP, D); |
681 | ParsedComments[Canonical] = FC; |
682 | return FC; |
683 | } |
684 | |
685 | void |
686 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
687 | const ASTContext &C, |
688 | TemplateTemplateParmDecl *Parm) { |
689 | ID.AddInteger(Parm->getDepth()); |
690 | ID.AddInteger(Parm->getPosition()); |
691 | ID.AddBoolean(B: Parm->isParameterPack()); |
692 | |
693 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
694 | ID.AddInteger(I: Params->size()); |
695 | for (TemplateParameterList::const_iterator P = Params->begin(), |
696 | PEnd = Params->end(); |
697 | P != PEnd; ++P) { |
698 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
699 | ID.AddInteger(I: 0); |
700 | ID.AddBoolean(B: TTP->isParameterPack()); |
701 | if (TTP->isExpandedParameterPack()) { |
702 | ID.AddBoolean(B: true); |
703 | ID.AddInteger(TTP->getNumExpansionParameters()); |
704 | } else |
705 | ID.AddBoolean(B: false); |
706 | continue; |
707 | } |
708 | |
709 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
710 | ID.AddInteger(I: 1); |
711 | ID.AddBoolean(B: NTTP->isParameterPack()); |
712 | ID.AddPointer(Ptr: C.getUnconstrainedType(T: C.getCanonicalType(NTTP->getType())) |
713 | .getAsOpaquePtr()); |
714 | if (NTTP->isExpandedParameterPack()) { |
715 | ID.AddBoolean(B: true); |
716 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
717 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
718 | QualType T = NTTP->getExpansionType(I); |
719 | ID.AddPointer(Ptr: T.getCanonicalType().getAsOpaquePtr()); |
720 | } |
721 | } else |
722 | ID.AddBoolean(B: false); |
723 | continue; |
724 | } |
725 | |
726 | auto *TTP = cast<TemplateTemplateParmDecl>(Val: *P); |
727 | ID.AddInteger(I: 2); |
728 | Profile(ID, C, TTP); |
729 | } |
730 | } |
731 | |
732 | TemplateTemplateParmDecl * |
733 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
734 | TemplateTemplateParmDecl *TTP) const { |
735 | // Check if we already have a canonical template template parameter. |
736 | llvm::FoldingSetNodeID ID; |
737 | CanonicalTemplateTemplateParm::Profile(ID, C: *this, Parm: TTP); |
738 | void *InsertPos = nullptr; |
739 | CanonicalTemplateTemplateParm *Canonical |
740 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
741 | if (Canonical) |
742 | return Canonical->getParam(); |
743 | |
744 | // Build a canonical template parameter list. |
745 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
746 | SmallVector<NamedDecl *, 4> CanonParams; |
747 | CanonParams.reserve(N: Params->size()); |
748 | for (TemplateParameterList::const_iterator P = Params->begin(), |
749 | PEnd = Params->end(); |
750 | P != PEnd; ++P) { |
751 | // Note that, per C++20 [temp.over.link]/6, when determining whether |
752 | // template-parameters are equivalent, constraints are ignored. |
753 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
754 | TemplateTypeParmDecl *NewTTP = TemplateTypeParmDecl::Create( |
755 | C: *this, DC: getTranslationUnitDecl(), KeyLoc: SourceLocation(), NameLoc: SourceLocation(), |
756 | D: TTP->getDepth(), P: TTP->getIndex(), Id: nullptr, Typename: false, |
757 | ParameterPack: TTP->isParameterPack(), /*HasTypeConstraint=*/false, |
758 | NumExpanded: TTP->isExpandedParameterPack() |
759 | ? std::optional<unsigned>(TTP->getNumExpansionParameters()) |
760 | : std::nullopt); |
761 | CanonParams.push_back(NewTTP); |
762 | } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
763 | QualType T = getUnconstrainedType(T: getCanonicalType(NTTP->getType())); |
764 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
765 | NonTypeTemplateParmDecl *Param; |
766 | if (NTTP->isExpandedParameterPack()) { |
767 | SmallVector<QualType, 2> ExpandedTypes; |
768 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
769 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
770 | ExpandedTypes.push_back(Elt: getCanonicalType(NTTP->getExpansionType(I))); |
771 | ExpandedTInfos.push_back( |
772 | Elt: getTrivialTypeSourceInfo(T: ExpandedTypes.back())); |
773 | } |
774 | |
775 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
776 | SourceLocation(), |
777 | SourceLocation(), |
778 | NTTP->getDepth(), |
779 | NTTP->getPosition(), nullptr, |
780 | T, |
781 | TInfo, |
782 | ExpandedTypes, |
783 | ExpandedTInfos); |
784 | } else { |
785 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
786 | SourceLocation(), |
787 | SourceLocation(), |
788 | NTTP->getDepth(), |
789 | NTTP->getPosition(), nullptr, |
790 | T, |
791 | NTTP->isParameterPack(), |
792 | TInfo); |
793 | } |
794 | CanonParams.push_back(Param); |
795 | } else |
796 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
797 | TTP: cast<TemplateTemplateParmDecl>(Val: *P))); |
798 | } |
799 | |
800 | TemplateTemplateParmDecl *CanonTTP = TemplateTemplateParmDecl::Create( |
801 | *this, getTranslationUnitDecl(), SourceLocation(), TTP->getDepth(), |
802 | TTP->getPosition(), TTP->isParameterPack(), nullptr, /*Typename=*/false, |
803 | TemplateParameterList::Create(C: *this, TemplateLoc: SourceLocation(), LAngleLoc: SourceLocation(), |
804 | Params: CanonParams, RAngleLoc: SourceLocation(), |
805 | /*RequiresClause=*/nullptr)); |
806 | |
807 | // Get the new insert position for the node we care about. |
808 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
809 | assert(!Canonical && "Shouldn't be in the map!" ); |
810 | (void)Canonical; |
811 | |
812 | // Create the canonical template template parameter entry. |
813 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
814 | CanonTemplateTemplateParms.InsertNode(N: Canonical, InsertPos); |
815 | return CanonTTP; |
816 | } |
817 | |
818 | TargetCXXABI::Kind ASTContext::getCXXABIKind() const { |
819 | auto Kind = getTargetInfo().getCXXABI().getKind(); |
820 | return getLangOpts().CXXABI.value_or(u&: Kind); |
821 | } |
822 | |
823 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
824 | if (!LangOpts.CPlusPlus) return nullptr; |
825 | |
826 | switch (getCXXABIKind()) { |
827 | case TargetCXXABI::AppleARM64: |
828 | case TargetCXXABI::Fuchsia: |
829 | case TargetCXXABI::GenericARM: // Same as Itanium at this level |
830 | case TargetCXXABI::iOS: |
831 | case TargetCXXABI::WatchOS: |
832 | case TargetCXXABI::GenericAArch64: |
833 | case TargetCXXABI::GenericMIPS: |
834 | case TargetCXXABI::GenericItanium: |
835 | case TargetCXXABI::WebAssembly: |
836 | case TargetCXXABI::XL: |
837 | return CreateItaniumCXXABI(Ctx&: *this); |
838 | case TargetCXXABI::Microsoft: |
839 | return CreateMicrosoftCXXABI(Ctx&: *this); |
840 | } |
841 | llvm_unreachable("Invalid CXXABI type!" ); |
842 | } |
843 | |
844 | interp::Context &ASTContext::getInterpContext() { |
845 | if (!InterpContext) { |
846 | InterpContext.reset(p: new interp::Context(*this)); |
847 | } |
848 | return *InterpContext.get(); |
849 | } |
850 | |
851 | ParentMapContext &ASTContext::getParentMapContext() { |
852 | if (!ParentMapCtx) |
853 | ParentMapCtx.reset(p: new ParentMapContext(*this)); |
854 | return *ParentMapCtx.get(); |
855 | } |
856 | |
857 | static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI, |
858 | const LangOptions &LangOpts) { |
859 | switch (LangOpts.getAddressSpaceMapMangling()) { |
860 | case LangOptions::ASMM_Target: |
861 | return TI.useAddressSpaceMapMangling(); |
862 | case LangOptions::ASMM_On: |
863 | return true; |
864 | case LangOptions::ASMM_Off: |
865 | return false; |
866 | } |
867 | llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything." ); |
868 | } |
869 | |
870 | ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM, |
871 | IdentifierTable &idents, SelectorTable &sels, |
872 | Builtin::Context &builtins, TranslationUnitKind TUKind) |
873 | : ConstantArrayTypes(this_(), ConstantArrayTypesLog2InitSize), |
874 | DependentSizedArrayTypes(this_()), DependentSizedExtVectorTypes(this_()), |
875 | DependentAddressSpaceTypes(this_()), DependentVectorTypes(this_()), |
876 | DependentSizedMatrixTypes(this_()), |
877 | FunctionProtoTypes(this_(), FunctionProtoTypesLog2InitSize), |
878 | DependentTypeOfExprTypes(this_()), DependentDecltypeTypes(this_()), |
879 | TemplateSpecializationTypes(this_()), |
880 | DependentTemplateSpecializationTypes(this_()), AutoTypes(this_()), |
881 | DependentBitIntTypes(this_()), SubstTemplateTemplateParmPacks(this_()), |
882 | ArrayParameterTypes(this_()), CanonTemplateTemplateParms(this_()), |
883 | SourceMgr(SM), LangOpts(LOpts), |
884 | NoSanitizeL(new NoSanitizeList(LangOpts.NoSanitizeFiles, SM)), |
885 | XRayFilter(new XRayFunctionFilter(LangOpts.XRayAlwaysInstrumentFiles, |
886 | LangOpts.XRayNeverInstrumentFiles, |
887 | LangOpts.XRayAttrListFiles, SM)), |
888 | ProfList(new ProfileList(LangOpts.ProfileListFiles, SM)), |
889 | PrintingPolicy(LOpts), Idents(idents), Selectors(sels), |
890 | BuiltinInfo(builtins), TUKind(TUKind), DeclarationNames(*this), |
891 | Comments(SM), CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), |
892 | CompCategories(this_()), LastSDM(nullptr, 0) { |
893 | addTranslationUnitDecl(); |
894 | } |
895 | |
896 | void ASTContext::cleanup() { |
897 | // Release the DenseMaps associated with DeclContext objects. |
898 | // FIXME: Is this the ideal solution? |
899 | ReleaseDeclContextMaps(); |
900 | |
901 | // Call all of the deallocation functions on all of their targets. |
902 | for (auto &Pair : Deallocations) |
903 | (Pair.first)(Pair.second); |
904 | Deallocations.clear(); |
905 | |
906 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
907 | // because they can contain DenseMaps. |
908 | for (llvm::DenseMap<const ObjCContainerDecl*, |
909 | const ASTRecordLayout*>::iterator |
910 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
911 | // Increment in loop to prevent using deallocated memory. |
912 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) |
913 | R->Destroy(Ctx&: *this); |
914 | ObjCLayouts.clear(); |
915 | |
916 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
917 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
918 | // Increment in loop to prevent using deallocated memory. |
919 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) |
920 | R->Destroy(Ctx&: *this); |
921 | } |
922 | ASTRecordLayouts.clear(); |
923 | |
924 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
925 | AEnd = DeclAttrs.end(); |
926 | A != AEnd; ++A) |
927 | A->second->~AttrVec(); |
928 | DeclAttrs.clear(); |
929 | |
930 | for (const auto &Value : ModuleInitializers) |
931 | Value.second->~PerModuleInitializers(); |
932 | ModuleInitializers.clear(); |
933 | } |
934 | |
935 | ASTContext::~ASTContext() { cleanup(); } |
936 | |
937 | void ASTContext::setTraversalScope(const std::vector<Decl *> &TopLevelDecls) { |
938 | TraversalScope = TopLevelDecls; |
939 | getParentMapContext().clear(); |
940 | } |
941 | |
942 | void ASTContext::AddDeallocation(void (*Callback)(void *), void *Data) const { |
943 | Deallocations.push_back(Elt: {Callback, Data}); |
944 | } |
945 | |
946 | void |
947 | ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) { |
948 | ExternalSource = std::move(Source); |
949 | } |
950 | |
951 | void ASTContext::PrintStats() const { |
952 | llvm::errs() << "\n*** AST Context Stats:\n" ; |
953 | llvm::errs() << " " << Types.size() << " types total.\n" ; |
954 | |
955 | unsigned counts[] = { |
956 | #define TYPE(Name, Parent) 0, |
957 | #define ABSTRACT_TYPE(Name, Parent) |
958 | #include "clang/AST/TypeNodes.inc" |
959 | 0 // Extra |
960 | }; |
961 | |
962 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
963 | Type *T = Types[i]; |
964 | counts[(unsigned)T->getTypeClass()]++; |
965 | } |
966 | |
967 | unsigned Idx = 0; |
968 | unsigned TotalBytes = 0; |
969 | #define TYPE(Name, Parent) \ |
970 | if (counts[Idx]) \ |
971 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
972 | << " types, " << sizeof(Name##Type) << " each " \ |
973 | << "(" << counts[Idx] * sizeof(Name##Type) \ |
974 | << " bytes)\n"; \ |
975 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
976 | ++Idx; |
977 | #define ABSTRACT_TYPE(Name, Parent) |
978 | #include "clang/AST/TypeNodes.inc" |
979 | |
980 | llvm::errs() << "Total bytes = " << TotalBytes << "\n" ; |
981 | |
982 | // Implicit special member functions. |
983 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
984 | << NumImplicitDefaultConstructors |
985 | << " implicit default constructors created\n" ; |
986 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
987 | << NumImplicitCopyConstructors |
988 | << " implicit copy constructors created\n" ; |
989 | if (getLangOpts().CPlusPlus) |
990 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
991 | << NumImplicitMoveConstructors |
992 | << " implicit move constructors created\n" ; |
993 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
994 | << NumImplicitCopyAssignmentOperators |
995 | << " implicit copy assignment operators created\n" ; |
996 | if (getLangOpts().CPlusPlus) |
997 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
998 | << NumImplicitMoveAssignmentOperators |
999 | << " implicit move assignment operators created\n" ; |
1000 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
1001 | << NumImplicitDestructors |
1002 | << " implicit destructors created\n" ; |
1003 | |
1004 | if (ExternalSource) { |
1005 | llvm::errs() << "\n" ; |
1006 | ExternalSource->PrintStats(); |
1007 | } |
1008 | |
1009 | BumpAlloc.PrintStats(); |
1010 | } |
1011 | |
1012 | void ASTContext::mergeDefinitionIntoModule(NamedDecl *ND, Module *M, |
1013 | bool NotifyListeners) { |
1014 | if (NotifyListeners) |
1015 | if (auto *Listener = getASTMutationListener()) |
1016 | Listener->RedefinedHiddenDefinition(D: ND, M); |
1017 | |
1018 | MergedDefModules[cast<NamedDecl>(ND->getCanonicalDecl())].push_back(M); |
1019 | } |
1020 | |
1021 | void ASTContext::deduplicateMergedDefinitonsFor(NamedDecl *ND) { |
1022 | auto It = MergedDefModules.find(cast<NamedDecl>(ND->getCanonicalDecl())); |
1023 | if (It == MergedDefModules.end()) |
1024 | return; |
1025 | |
1026 | auto &Merged = It->second; |
1027 | llvm::DenseSet<Module*> Found; |
1028 | for (Module *&M : Merged) |
1029 | if (!Found.insert(M).second) |
1030 | M = nullptr; |
1031 | llvm::erase(Merged, nullptr); |
1032 | } |
1033 | |
1034 | ArrayRef<Module *> |
1035 | ASTContext::getModulesWithMergedDefinition(const NamedDecl *Def) { |
1036 | auto MergedIt = |
1037 | MergedDefModules.find(cast<NamedDecl>(Def->getCanonicalDecl())); |
1038 | if (MergedIt == MergedDefModules.end()) |
1039 | return std::nullopt; |
1040 | return MergedIt->second; |
1041 | } |
1042 | |
1043 | void ASTContext::PerModuleInitializers::resolve(ASTContext &Ctx) { |
1044 | if (LazyInitializers.empty()) |
1045 | return; |
1046 | |
1047 | auto *Source = Ctx.getExternalSource(); |
1048 | assert(Source && "lazy initializers but no external source" ); |
1049 | |
1050 | auto LazyInits = std::move(LazyInitializers); |
1051 | LazyInitializers.clear(); |
1052 | |
1053 | for (auto ID : LazyInits) |
1054 | Initializers.push_back(Elt: Source->GetExternalDecl(ID)); |
1055 | |
1056 | assert(LazyInitializers.empty() && |
1057 | "GetExternalDecl for lazy module initializer added more inits" ); |
1058 | } |
1059 | |
1060 | void ASTContext::addModuleInitializer(Module *M, Decl *D) { |
1061 | // One special case: if we add a module initializer that imports another |
1062 | // module, and that module's only initializer is an ImportDecl, simplify. |
1063 | if (const auto *ID = dyn_cast<ImportDecl>(Val: D)) { |
1064 | auto It = ModuleInitializers.find(Val: ID->getImportedModule()); |
1065 | |
1066 | // Maybe the ImportDecl does nothing at all. (Common case.) |
1067 | if (It == ModuleInitializers.end()) |
1068 | return; |
1069 | |
1070 | // Maybe the ImportDecl only imports another ImportDecl. |
1071 | auto &Imported = *It->second; |
1072 | if (Imported.Initializers.size() + Imported.LazyInitializers.size() == 1) { |
1073 | Imported.resolve(Ctx&: *this); |
1074 | auto *OnlyDecl = Imported.Initializers.front(); |
1075 | if (isa<ImportDecl>(Val: OnlyDecl)) |
1076 | D = OnlyDecl; |
1077 | } |
1078 | } |
1079 | |
1080 | auto *&Inits = ModuleInitializers[M]; |
1081 | if (!Inits) |
1082 | Inits = new (*this) PerModuleInitializers; |
1083 | Inits->Initializers.push_back(Elt: D); |
1084 | } |
1085 | |
1086 | void ASTContext::addLazyModuleInitializers(Module *M, |
1087 | ArrayRef<Decl::DeclID> IDs) { |
1088 | auto *&Inits = ModuleInitializers[M]; |
1089 | if (!Inits) |
1090 | Inits = new (*this) PerModuleInitializers; |
1091 | Inits->LazyInitializers.insert(I: Inits->LazyInitializers.end(), |
1092 | From: IDs.begin(), To: IDs.end()); |
1093 | } |
1094 | |
1095 | ArrayRef<Decl *> ASTContext::getModuleInitializers(Module *M) { |
1096 | auto It = ModuleInitializers.find(Val: M); |
1097 | if (It == ModuleInitializers.end()) |
1098 | return std::nullopt; |
1099 | |
1100 | auto *Inits = It->second; |
1101 | Inits->resolve(Ctx&: *this); |
1102 | return Inits->Initializers; |
1103 | } |
1104 | |
1105 | void ASTContext::setCurrentNamedModule(Module *M) { |
1106 | assert(M->isNamedModule()); |
1107 | assert(!CurrentCXXNamedModule && |
1108 | "We should set named module for ASTContext for only once" ); |
1109 | CurrentCXXNamedModule = M; |
1110 | } |
1111 | |
1112 | ExternCContextDecl *ASTContext::getExternCContextDecl() const { |
1113 | if (!ExternCContext) |
1114 | ExternCContext = ExternCContextDecl::Create(C: *this, TU: getTranslationUnitDecl()); |
1115 | |
1116 | return ExternCContext; |
1117 | } |
1118 | |
1119 | BuiltinTemplateDecl * |
1120 | ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK, |
1121 | const IdentifierInfo *II) const { |
1122 | auto *BuiltinTemplate = |
1123 | BuiltinTemplateDecl::Create(*this, getTranslationUnitDecl(), II, BTK); |
1124 | BuiltinTemplate->setImplicit(); |
1125 | getTranslationUnitDecl()->addDecl(D: BuiltinTemplate); |
1126 | |
1127 | return BuiltinTemplate; |
1128 | } |
1129 | |
1130 | BuiltinTemplateDecl * |
1131 | ASTContext::getMakeIntegerSeqDecl() const { |
1132 | if (!MakeIntegerSeqDecl) |
1133 | MakeIntegerSeqDecl = buildBuiltinTemplateDecl(BTK: BTK__make_integer_seq, |
1134 | II: getMakeIntegerSeqName()); |
1135 | return MakeIntegerSeqDecl; |
1136 | } |
1137 | |
1138 | BuiltinTemplateDecl * |
1139 | ASTContext::getTypePackElementDecl() const { |
1140 | if (!TypePackElementDecl) |
1141 | TypePackElementDecl = buildBuiltinTemplateDecl(BTK: BTK__type_pack_element, |
1142 | II: getTypePackElementName()); |
1143 | return TypePackElementDecl; |
1144 | } |
1145 | |
1146 | RecordDecl *ASTContext::buildImplicitRecord(StringRef Name, |
1147 | RecordDecl::TagKind TK) const { |
1148 | SourceLocation Loc; |
1149 | RecordDecl *NewDecl; |
1150 | if (getLangOpts().CPlusPlus) |
1151 | NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, |
1152 | Loc, &Idents.get(Name)); |
1153 | else |
1154 | NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc, |
1155 | &Idents.get(Name)); |
1156 | NewDecl->setImplicit(); |
1157 | NewDecl->addAttr(TypeVisibilityAttr::CreateImplicit( |
1158 | const_cast<ASTContext &>(*this), TypeVisibilityAttr::Default)); |
1159 | return NewDecl; |
1160 | } |
1161 | |
1162 | TypedefDecl *ASTContext::buildImplicitTypedef(QualType T, |
1163 | StringRef Name) const { |
1164 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
1165 | TypedefDecl *NewDecl = TypedefDecl::Create( |
1166 | const_cast<ASTContext &>(*this), getTranslationUnitDecl(), |
1167 | SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo); |
1168 | NewDecl->setImplicit(); |
1169 | return NewDecl; |
1170 | } |
1171 | |
1172 | TypedefDecl *ASTContext::getInt128Decl() const { |
1173 | if (!Int128Decl) |
1174 | Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t" ); |
1175 | return Int128Decl; |
1176 | } |
1177 | |
1178 | TypedefDecl *ASTContext::getUInt128Decl() const { |
1179 | if (!UInt128Decl) |
1180 | UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t" ); |
1181 | return UInt128Decl; |
1182 | } |
1183 | |
1184 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
1185 | auto *Ty = new (*this, alignof(BuiltinType)) BuiltinType(K); |
1186 | R = CanQualType::CreateUnsafe(Other: QualType(Ty, 0)); |
1187 | Types.push_back(Ty); |
1188 | } |
1189 | |
1190 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target, |
1191 | const TargetInfo *AuxTarget) { |
1192 | assert((!this->Target || this->Target == &Target) && |
1193 | "Incorrect target reinitialization" ); |
1194 | assert(VoidTy.isNull() && "Context reinitialized?" ); |
1195 | |
1196 | this->Target = &Target; |
1197 | this->AuxTarget = AuxTarget; |
1198 | |
1199 | ABI.reset(p: createCXXABI(T: Target)); |
1200 | AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(TI: Target, LangOpts); |
1201 | |
1202 | // C99 6.2.5p19. |
1203 | InitBuiltinType(VoidTy, BuiltinType::Void); |
1204 | |
1205 | // C99 6.2.5p2. |
1206 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
1207 | // C99 6.2.5p3. |
1208 | if (LangOpts.CharIsSigned) |
1209 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
1210 | else |
1211 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
1212 | // C99 6.2.5p4. |
1213 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
1214 | InitBuiltinType(ShortTy, BuiltinType::Short); |
1215 | InitBuiltinType(IntTy, BuiltinType::Int); |
1216 | InitBuiltinType(LongTy, BuiltinType::Long); |
1217 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
1218 | |
1219 | // C99 6.2.5p6. |
1220 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
1221 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
1222 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
1223 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
1224 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
1225 | |
1226 | // C99 6.2.5p10. |
1227 | InitBuiltinType(FloatTy, BuiltinType::Float); |
1228 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
1229 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
1230 | |
1231 | // GNU extension, __float128 for IEEE quadruple precision |
1232 | InitBuiltinType(Float128Ty, BuiltinType::Float128); |
1233 | |
1234 | // __ibm128 for IBM extended precision |
1235 | InitBuiltinType(Ibm128Ty, BuiltinType::Ibm128); |
1236 | |
1237 | // C11 extension ISO/IEC TS 18661-3 |
1238 | InitBuiltinType(Float16Ty, BuiltinType::Float16); |
1239 | |
1240 | // ISO/IEC JTC1 SC22 WG14 N1169 Extension |
1241 | InitBuiltinType(ShortAccumTy, BuiltinType::ShortAccum); |
1242 | InitBuiltinType(AccumTy, BuiltinType::Accum); |
1243 | InitBuiltinType(LongAccumTy, BuiltinType::LongAccum); |
1244 | InitBuiltinType(UnsignedShortAccumTy, BuiltinType::UShortAccum); |
1245 | InitBuiltinType(UnsignedAccumTy, BuiltinType::UAccum); |
1246 | InitBuiltinType(UnsignedLongAccumTy, BuiltinType::ULongAccum); |
1247 | InitBuiltinType(ShortFractTy, BuiltinType::ShortFract); |
1248 | InitBuiltinType(FractTy, BuiltinType::Fract); |
1249 | InitBuiltinType(LongFractTy, BuiltinType::LongFract); |
1250 | InitBuiltinType(UnsignedShortFractTy, BuiltinType::UShortFract); |
1251 | InitBuiltinType(UnsignedFractTy, BuiltinType::UFract); |
1252 | InitBuiltinType(UnsignedLongFractTy, BuiltinType::ULongFract); |
1253 | InitBuiltinType(SatShortAccumTy, BuiltinType::SatShortAccum); |
1254 | InitBuiltinType(SatAccumTy, BuiltinType::SatAccum); |
1255 | InitBuiltinType(SatLongAccumTy, BuiltinType::SatLongAccum); |
1256 | InitBuiltinType(SatUnsignedShortAccumTy, BuiltinType::SatUShortAccum); |
1257 | InitBuiltinType(SatUnsignedAccumTy, BuiltinType::SatUAccum); |
1258 | InitBuiltinType(SatUnsignedLongAccumTy, BuiltinType::SatULongAccum); |
1259 | InitBuiltinType(SatShortFractTy, BuiltinType::SatShortFract); |
1260 | InitBuiltinType(SatFractTy, BuiltinType::SatFract); |
1261 | InitBuiltinType(SatLongFractTy, BuiltinType::SatLongFract); |
1262 | InitBuiltinType(SatUnsignedShortFractTy, BuiltinType::SatUShortFract); |
1263 | InitBuiltinType(SatUnsignedFractTy, BuiltinType::SatUFract); |
1264 | InitBuiltinType(SatUnsignedLongFractTy, BuiltinType::SatULongFract); |
1265 | |
1266 | // GNU extension, 128-bit integers. |
1267 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
1268 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
1269 | |
1270 | // C++ 3.9.1p5 |
1271 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
1272 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
1273 | else // -fshort-wchar makes wchar_t be unsigned. |
1274 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
1275 | if (LangOpts.CPlusPlus && LangOpts.WChar) |
1276 | WideCharTy = WCharTy; |
1277 | else { |
1278 | // C99 (or C++ using -fno-wchar). |
1279 | WideCharTy = getFromTargetType(Target.getWCharType()); |
1280 | } |
1281 | |
1282 | WIntTy = getFromTargetType(Target.getWIntType()); |
1283 | |
1284 | // C++20 (proposed) |
1285 | InitBuiltinType(Char8Ty, BuiltinType::Char8); |
1286 | |
1287 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
1288 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
1289 | else // C99 |
1290 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
1291 | |
1292 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
1293 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
1294 | else // C99 |
1295 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
1296 | |
1297 | // Placeholder type for type-dependent expressions whose type is |
1298 | // completely unknown. No code should ever check a type against |
1299 | // DependentTy and users should never see it; however, it is here to |
1300 | // help diagnose failures to properly check for type-dependent |
1301 | // expressions. |
1302 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
1303 | |
1304 | // Placeholder type for functions. |
1305 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
1306 | |
1307 | // Placeholder type for bound members. |
1308 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
1309 | |
1310 | // Placeholder type for pseudo-objects. |
1311 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
1312 | |
1313 | // "any" type; useful for debugger-like clients. |
1314 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
1315 | |
1316 | // Placeholder type for unbridged ARC casts. |
1317 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
1318 | |
1319 | // Placeholder type for builtin functions. |
1320 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
1321 | |
1322 | // Placeholder type for OMP array sections. |
1323 | if (LangOpts.OpenMP) { |
1324 | InitBuiltinType(OMPArraySectionTy, BuiltinType::OMPArraySection); |
1325 | InitBuiltinType(OMPArrayShapingTy, BuiltinType::OMPArrayShaping); |
1326 | InitBuiltinType(OMPIteratorTy, BuiltinType::OMPIterator); |
1327 | } |
1328 | // Placeholder type for OpenACC array sections. |
1329 | if (LangOpts.OpenACC) { |
1330 | // FIXME: Once we implement OpenACC array sections in Sema, this will either |
1331 | // be combined with the OpenMP type, or given its own type. In the meantime, |
1332 | // just use the OpenMP type so that parsing can work. |
1333 | InitBuiltinType(OMPArraySectionTy, BuiltinType::OMPArraySection); |
1334 | } |
1335 | if (LangOpts.MatrixTypes) |
1336 | InitBuiltinType(IncompleteMatrixIdxTy, BuiltinType::IncompleteMatrixIdx); |
1337 | |
1338 | // Builtin types for 'id', 'Class', and 'SEL'. |
1339 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
1340 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
1341 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
1342 | |
1343 | if (LangOpts.OpenCL) { |
1344 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
1345 | InitBuiltinType(SingletonId, BuiltinType::Id); |
1346 | #include "clang/Basic/OpenCLImageTypes.def" |
1347 | |
1348 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
1349 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
1350 | InitBuiltinType(OCLClkEventTy, BuiltinType::OCLClkEvent); |
1351 | InitBuiltinType(OCLQueueTy, BuiltinType::OCLQueue); |
1352 | InitBuiltinType(OCLReserveIDTy, BuiltinType::OCLReserveID); |
1353 | |
1354 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
1355 | InitBuiltinType(Id##Ty, BuiltinType::Id); |
1356 | #include "clang/Basic/OpenCLExtensionTypes.def" |
1357 | } |
1358 | |
1359 | if (Target.hasAArch64SVETypes()) { |
1360 | #define SVE_TYPE(Name, Id, SingletonId) \ |
1361 | InitBuiltinType(SingletonId, BuiltinType::Id); |
1362 | #include "clang/Basic/AArch64SVEACLETypes.def" |
1363 | } |
1364 | |
1365 | if (Target.getTriple().isPPC64()) { |
1366 | #define PPC_VECTOR_MMA_TYPE(Name, Id, Size) \ |
1367 | InitBuiltinType(Id##Ty, BuiltinType::Id); |
1368 | #include "clang/Basic/PPCTypes.def" |
1369 | #define PPC_VECTOR_VSX_TYPE(Name, Id, Size) \ |
1370 | InitBuiltinType(Id##Ty, BuiltinType::Id); |
1371 | #include "clang/Basic/PPCTypes.def" |
1372 | } |
1373 | |
1374 | if (Target.hasRISCVVTypes()) { |
1375 | #define RVV_TYPE(Name, Id, SingletonId) \ |
1376 | InitBuiltinType(SingletonId, BuiltinType::Id); |
1377 | #include "clang/Basic/RISCVVTypes.def" |
1378 | } |
1379 | |
1380 | if (Target.getTriple().isWasm() && Target.hasFeature(Feature: "reference-types" )) { |
1381 | #define WASM_TYPE(Name, Id, SingletonId) \ |
1382 | InitBuiltinType(SingletonId, BuiltinType::Id); |
1383 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
1384 | } |
1385 | |
1386 | // Builtin type for __objc_yes and __objc_no |
1387 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
1388 | SignedCharTy : BoolTy); |
1389 | |
1390 | ObjCConstantStringType = QualType(); |
1391 | |
1392 | ObjCSuperType = QualType(); |
1393 | |
1394 | // void * type |
1395 | if (LangOpts.OpenCLGenericAddressSpace) { |
1396 | auto Q = VoidTy.getQualifiers(); |
1397 | Q.setAddressSpace(LangAS::opencl_generic); |
1398 | VoidPtrTy = getPointerType(getCanonicalType( |
1399 | getQualifiedType(VoidTy.getUnqualifiedType(), Q))); |
1400 | } else { |
1401 | VoidPtrTy = getPointerType(VoidTy); |
1402 | } |
1403 | |
1404 | // nullptr type (C++0x 2.14.7) |
1405 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
1406 | |
1407 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
1408 | InitBuiltinType(HalfTy, BuiltinType::Half); |
1409 | |
1410 | InitBuiltinType(BFloat16Ty, BuiltinType::BFloat16); |
1411 | |
1412 | // Builtin type used to help define __builtin_va_list. |
1413 | VaListTagDecl = nullptr; |
1414 | |
1415 | // MSVC predeclares struct _GUID, and we need it to create MSGuidDecls. |
1416 | if (LangOpts.MicrosoftExt || LangOpts.Borland) { |
1417 | MSGuidTagDecl = buildImplicitRecord(Name: "_GUID" ); |
1418 | getTranslationUnitDecl()->addDecl(MSGuidTagDecl); |
1419 | } |
1420 | } |
1421 | |
1422 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
1423 | return SourceMgr.getDiagnostics(); |
1424 | } |
1425 | |
1426 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
1427 | AttrVec *&Result = DeclAttrs[D]; |
1428 | if (!Result) { |
1429 | void *Mem = Allocate(Size: sizeof(AttrVec)); |
1430 | Result = new (Mem) AttrVec; |
1431 | } |
1432 | |
1433 | return *Result; |
1434 | } |
1435 | |
1436 | /// Erase the attributes corresponding to the given declaration. |
1437 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
1438 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(Val: D); |
1439 | if (Pos != DeclAttrs.end()) { |
1440 | Pos->second->~AttrVec(); |
1441 | DeclAttrs.erase(I: Pos); |
1442 | } |
1443 | } |
1444 | |
1445 | // FIXME: Remove ? |
1446 | MemberSpecializationInfo * |
1447 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
1448 | assert(Var->isStaticDataMember() && "Not a static data member" ); |
1449 | return getTemplateOrSpecializationInfo(Var) |
1450 | .dyn_cast<MemberSpecializationInfo *>(); |
1451 | } |
1452 | |
1453 | ASTContext::TemplateOrSpecializationInfo |
1454 | ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) { |
1455 | llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos = |
1456 | TemplateOrInstantiation.find(Val: Var); |
1457 | if (Pos == TemplateOrInstantiation.end()) |
1458 | return {}; |
1459 | |
1460 | return Pos->second; |
1461 | } |
1462 | |
1463 | void |
1464 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
1465 | TemplateSpecializationKind TSK, |
1466 | SourceLocation PointOfInstantiation) { |
1467 | assert(Inst->isStaticDataMember() && "Not a static data member" ); |
1468 | assert(Tmpl->isStaticDataMember() && "Not a static data member" ); |
1469 | setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo( |
1470 | Tmpl, TSK, PointOfInstantiation)); |
1471 | } |
1472 | |
1473 | void |
1474 | ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst, |
1475 | TemplateOrSpecializationInfo TSI) { |
1476 | assert(!TemplateOrInstantiation[Inst] && |
1477 | "Already noted what the variable was instantiated from" ); |
1478 | TemplateOrInstantiation[Inst] = TSI; |
1479 | } |
1480 | |
1481 | NamedDecl * |
1482 | ASTContext::getInstantiatedFromUsingDecl(NamedDecl *UUD) { |
1483 | return InstantiatedFromUsingDecl.lookup(Val: UUD); |
1484 | } |
1485 | |
1486 | void |
1487 | ASTContext::setInstantiatedFromUsingDecl(NamedDecl *Inst, NamedDecl *Pattern) { |
1488 | assert((isa<UsingDecl>(Pattern) || |
1489 | isa<UnresolvedUsingValueDecl>(Pattern) || |
1490 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
1491 | "pattern decl is not a using decl" ); |
1492 | assert((isa<UsingDecl>(Inst) || |
1493 | isa<UnresolvedUsingValueDecl>(Inst) || |
1494 | isa<UnresolvedUsingTypenameDecl>(Inst)) && |
1495 | "instantiation did not produce a using decl" ); |
1496 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists" ); |
1497 | InstantiatedFromUsingDecl[Inst] = Pattern; |
1498 | } |
1499 | |
1500 | UsingEnumDecl * |
1501 | ASTContext::getInstantiatedFromUsingEnumDecl(UsingEnumDecl *UUD) { |
1502 | return InstantiatedFromUsingEnumDecl.lookup(UUD); |
1503 | } |
1504 | |
1505 | void ASTContext::setInstantiatedFromUsingEnumDecl(UsingEnumDecl *Inst, |
1506 | UsingEnumDecl *Pattern) { |
1507 | assert(!InstantiatedFromUsingEnumDecl[Inst] && "pattern already exists" ); |
1508 | InstantiatedFromUsingEnumDecl[Inst] = Pattern; |
1509 | } |
1510 | |
1511 | UsingShadowDecl * |
1512 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
1513 | return InstantiatedFromUsingShadowDecl.lookup(Val: Inst); |
1514 | } |
1515 | |
1516 | void |
1517 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
1518 | UsingShadowDecl *Pattern) { |
1519 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists" ); |
1520 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
1521 | } |
1522 | |
1523 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
1524 | return InstantiatedFromUnnamedFieldDecl.lookup(Val: Field); |
1525 | } |
1526 | |
1527 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
1528 | FieldDecl *Tmpl) { |
1529 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed" ); |
1530 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed" ); |
1531 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
1532 | "Already noted what unnamed field was instantiated from" ); |
1533 | |
1534 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
1535 | } |
1536 | |
1537 | ASTContext::overridden_cxx_method_iterator |
1538 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
1539 | return overridden_methods(Method).begin(); |
1540 | } |
1541 | |
1542 | ASTContext::overridden_cxx_method_iterator |
1543 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
1544 | return overridden_methods(Method).end(); |
1545 | } |
1546 | |
1547 | unsigned |
1548 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
1549 | auto Range = overridden_methods(Method); |
1550 | return Range.end() - Range.begin(); |
1551 | } |
1552 | |
1553 | ASTContext::overridden_method_range |
1554 | ASTContext::overridden_methods(const CXXMethodDecl *Method) const { |
1555 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos = |
1556 | OverriddenMethods.find(Val: Method->getCanonicalDecl()); |
1557 | if (Pos == OverriddenMethods.end()) |
1558 | return overridden_method_range(nullptr, nullptr); |
1559 | return overridden_method_range(Pos->second.begin(), Pos->second.end()); |
1560 | } |
1561 | |
1562 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
1563 | const CXXMethodDecl *Overridden) { |
1564 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
1565 | OverriddenMethods[Method].push_back(NewVal: Overridden); |
1566 | } |
1567 | |
1568 | void ASTContext::getOverriddenMethods( |
1569 | const NamedDecl *D, |
1570 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
1571 | assert(D); |
1572 | |
1573 | if (const auto *CXXMethod = dyn_cast<CXXMethodDecl>(Val: D)) { |
1574 | Overridden.append(in_start: overridden_methods_begin(Method: CXXMethod), |
1575 | in_end: overridden_methods_end(Method: CXXMethod)); |
1576 | return; |
1577 | } |
1578 | |
1579 | const auto *Method = dyn_cast<ObjCMethodDecl>(Val: D); |
1580 | if (!Method) |
1581 | return; |
1582 | |
1583 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
1584 | Method->getOverriddenMethods(Overridden&: OverDecls); |
1585 | Overridden.append(in_start: OverDecls.begin(), in_end: OverDecls.end()); |
1586 | } |
1587 | |
1588 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
1589 | assert(!Import->getNextLocalImport() && |
1590 | "Import declaration already in the chain" ); |
1591 | assert(!Import->isFromASTFile() && "Non-local import declaration" ); |
1592 | if (!FirstLocalImport) { |
1593 | FirstLocalImport = Import; |
1594 | LastLocalImport = Import; |
1595 | return; |
1596 | } |
1597 | |
1598 | LastLocalImport->setNextLocalImport(Import); |
1599 | LastLocalImport = Import; |
1600 | } |
1601 | |
1602 | //===----------------------------------------------------------------------===// |
1603 | // Type Sizing and Analysis |
1604 | //===----------------------------------------------------------------------===// |
1605 | |
1606 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
1607 | /// scalar floating point type. |
1608 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
1609 | switch (T->castAs<BuiltinType>()->getKind()) { |
1610 | default: |
1611 | llvm_unreachable("Not a floating point type!" ); |
1612 | case BuiltinType::BFloat16: |
1613 | return Target->getBFloat16Format(); |
1614 | case BuiltinType::Float16: |
1615 | return Target->getHalfFormat(); |
1616 | case BuiltinType::Half: |
1617 | // For HLSL, when the native half type is disabled, half will be treat as |
1618 | // float. |
1619 | if (getLangOpts().HLSL) |
1620 | if (getLangOpts().NativeHalfType) |
1621 | return Target->getHalfFormat(); |
1622 | else |
1623 | return Target->getFloatFormat(); |
1624 | else |
1625 | return Target->getHalfFormat(); |
1626 | case BuiltinType::Float: return Target->getFloatFormat(); |
1627 | case BuiltinType::Double: return Target->getDoubleFormat(); |
1628 | case BuiltinType::Ibm128: |
1629 | return Target->getIbm128Format(); |
1630 | case BuiltinType::LongDouble: |
1631 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice) |
1632 | return AuxTarget->getLongDoubleFormat(); |
1633 | return Target->getLongDoubleFormat(); |
1634 | case BuiltinType::Float128: |
1635 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice) |
1636 | return AuxTarget->getFloat128Format(); |
1637 | return Target->getFloat128Format(); |
1638 | } |
1639 | } |
1640 | |
1641 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const { |
1642 | unsigned Align = Target->getCharWidth(); |
1643 | |
1644 | const unsigned AlignFromAttr = D->getMaxAlignment(); |
1645 | if (AlignFromAttr) |
1646 | Align = AlignFromAttr; |
1647 | |
1648 | // __attribute__((aligned)) can increase or decrease alignment |
1649 | // *except* on a struct or struct member, where it only increases |
1650 | // alignment unless 'packed' is also specified. |
1651 | // |
1652 | // It is an error for alignas to decrease alignment, so we can |
1653 | // ignore that possibility; Sema should diagnose it. |
1654 | bool UseAlignAttrOnly; |
1655 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: D)) |
1656 | UseAlignAttrOnly = |
1657 | FD->hasAttr<PackedAttr>() || FD->getParent()->hasAttr<PackedAttr>(); |
1658 | else |
1659 | UseAlignAttrOnly = AlignFromAttr != 0; |
1660 | // If we're using the align attribute only, just ignore everything |
1661 | // else about the declaration and its type. |
1662 | if (UseAlignAttrOnly) { |
1663 | // do nothing |
1664 | } else if (const auto *VD = dyn_cast<ValueDecl>(Val: D)) { |
1665 | QualType T = VD->getType(); |
1666 | if (const auto *RT = T->getAs<ReferenceType>()) { |
1667 | if (ForAlignof) |
1668 | T = RT->getPointeeType(); |
1669 | else |
1670 | T = getPointerType(T: RT->getPointeeType()); |
1671 | } |
1672 | QualType BaseT = getBaseElementType(QT: T); |
1673 | if (T->isFunctionType()) |
1674 | Align = getTypeInfoImpl(T: T.getTypePtr()).Align; |
1675 | else if (!BaseT->isIncompleteType()) { |
1676 | // Adjust alignments of declarations with array type by the |
1677 | // large-array alignment on the target. |
1678 | if (const ArrayType *arrayType = getAsArrayType(T)) { |
1679 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
1680 | if (!ForAlignof && MinWidth) { |
1681 | if (isa<VariableArrayType>(Val: arrayType)) |
1682 | Align = std::max(a: Align, b: Target->getLargeArrayAlign()); |
1683 | else if (isa<ConstantArrayType>(Val: arrayType) && |
1684 | MinWidth <= getTypeSize(cast<ConstantArrayType>(Val: arrayType))) |
1685 | Align = std::max(a: Align, b: Target->getLargeArrayAlign()); |
1686 | } |
1687 | } |
1688 | Align = std::max(a: Align, b: getPreferredTypeAlign(T: T.getTypePtr())); |
1689 | if (BaseT.getQualifiers().hasUnaligned()) |
1690 | Align = Target->getCharWidth(); |
1691 | } |
1692 | |
1693 | // Ensure miminum alignment for global variables. |
1694 | if (const auto *VD = dyn_cast<VarDecl>(Val: D)) |
1695 | if (VD->hasGlobalStorage() && !ForAlignof) { |
1696 | uint64_t TypeSize = |
1697 | !BaseT->isIncompleteType() ? getTypeSize(T: T.getTypePtr()) : 0; |
1698 | Align = std::max(a: Align, b: getMinGlobalAlignOfVar(Size: TypeSize, VD)); |
1699 | } |
1700 | |
1701 | // Fields can be subject to extra alignment constraints, like if |
1702 | // the field is packed, the struct is packed, or the struct has a |
1703 | // a max-field-alignment constraint (#pragma pack). So calculate |
1704 | // the actual alignment of the field within the struct, and then |
1705 | // (as we're expected to) constrain that by the alignment of the type. |
1706 | if (const auto *Field = dyn_cast<FieldDecl>(Val: VD)) { |
1707 | const RecordDecl *Parent = Field->getParent(); |
1708 | // We can only produce a sensible answer if the record is valid. |
1709 | if (!Parent->isInvalidDecl()) { |
1710 | const ASTRecordLayout &Layout = getASTRecordLayout(D: Parent); |
1711 | |
1712 | // Start with the record's overall alignment. |
1713 | unsigned FieldAlign = toBits(CharSize: Layout.getAlignment()); |
1714 | |
1715 | // Use the GCD of that and the offset within the record. |
1716 | uint64_t Offset = Layout.getFieldOffset(FieldNo: Field->getFieldIndex()); |
1717 | if (Offset > 0) { |
1718 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
1719 | // which means we get to do this crazy thing instead of Euclid's. |
1720 | uint64_t LowBitOfOffset = Offset & (~Offset + 1); |
1721 | if (LowBitOfOffset < FieldAlign) |
1722 | FieldAlign = static_cast<unsigned>(LowBitOfOffset); |
1723 | } |
1724 | |
1725 | Align = std::min(a: Align, b: FieldAlign); |
1726 | } |
1727 | } |
1728 | } |
1729 | |
1730 | // Some targets have hard limitation on the maximum requestable alignment in |
1731 | // aligned attribute for static variables. |
1732 | const unsigned MaxAlignedAttr = getTargetInfo().getMaxAlignedAttribute(); |
1733 | const auto *VD = dyn_cast<VarDecl>(Val: D); |
1734 | if (MaxAlignedAttr && VD && VD->getStorageClass() == SC_Static) |
1735 | Align = std::min(a: Align, b: MaxAlignedAttr); |
1736 | |
1737 | return toCharUnitsFromBits(BitSize: Align); |
1738 | } |
1739 | |
1740 | CharUnits ASTContext::getExnObjectAlignment() const { |
1741 | return toCharUnitsFromBits(BitSize: Target->getExnObjectAlignment()); |
1742 | } |
1743 | |
1744 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
1745 | // chars. If the type is a record, its data size is returned. This is |
1746 | // the size of the memcpy that's performed when assigning this type |
1747 | // using a trivial copy/move assignment operator. |
1748 | TypeInfoChars ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
1749 | TypeInfoChars Info = getTypeInfoInChars(T); |
1750 | |
1751 | // In C++, objects can sometimes be allocated into the tail padding |
1752 | // of a base-class subobject. We decide whether that's possible |
1753 | // during class layout, so here we can just trust the layout results. |
1754 | if (getLangOpts().CPlusPlus) { |
1755 | if (const auto *RT = T->getAs<RecordType>(); |
1756 | RT && !RT->getDecl()->isInvalidDecl()) { |
1757 | const ASTRecordLayout &layout = getASTRecordLayout(D: RT->getDecl()); |
1758 | Info.Width = layout.getDataSize(); |
1759 | } |
1760 | } |
1761 | |
1762 | return Info; |
1763 | } |
1764 | |
1765 | /// getConstantArrayInfoInChars - Performing the computation in CharUnits |
1766 | /// instead of in bits prevents overflowing the uint64_t for some large arrays. |
1767 | TypeInfoChars |
1768 | static getConstantArrayInfoInChars(const ASTContext &Context, |
1769 | const ConstantArrayType *CAT) { |
1770 | TypeInfoChars EltInfo = Context.getTypeInfoInChars(CAT->getElementType()); |
1771 | uint64_t Size = CAT->getZExtSize(); |
1772 | assert((Size == 0 || static_cast<uint64_t>(EltInfo.Width.getQuantity()) <= |
1773 | (uint64_t)(-1)/Size) && |
1774 | "Overflow in array type char size evaluation" ); |
1775 | uint64_t Width = EltInfo.Width.getQuantity() * Size; |
1776 | unsigned Align = EltInfo.Align.getQuantity(); |
1777 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() || |
1778 | Context.getTargetInfo().getPointerWidth(AddrSpace: LangAS::Default) == 64) |
1779 | Width = llvm::alignTo(Value: Width, Align); |
1780 | return TypeInfoChars(CharUnits::fromQuantity(Quantity: Width), |
1781 | CharUnits::fromQuantity(Quantity: Align), |
1782 | EltInfo.AlignRequirement); |
1783 | } |
1784 | |
1785 | TypeInfoChars ASTContext::getTypeInfoInChars(const Type *T) const { |
1786 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: T)) |
1787 | return getConstantArrayInfoInChars(Context: *this, CAT); |
1788 | TypeInfo Info = getTypeInfo(T); |
1789 | return TypeInfoChars(toCharUnitsFromBits(BitSize: Info.Width), |
1790 | toCharUnitsFromBits(BitSize: Info.Align), Info.AlignRequirement); |
1791 | } |
1792 | |
1793 | TypeInfoChars ASTContext::getTypeInfoInChars(QualType T) const { |
1794 | return getTypeInfoInChars(T: T.getTypePtr()); |
1795 | } |
1796 | |
1797 | bool ASTContext::isPromotableIntegerType(QualType T) const { |
1798 | // HLSL doesn't promote all small integer types to int, it |
1799 | // just uses the rank-based promotion rules for all types. |
1800 | if (getLangOpts().HLSL) |
1801 | return false; |
1802 | |
1803 | if (const auto *BT = T->getAs<BuiltinType>()) |
1804 | switch (BT->getKind()) { |
1805 | case BuiltinType::Bool: |
1806 | case BuiltinType::Char_S: |
1807 | case BuiltinType::Char_U: |
1808 | case BuiltinType::SChar: |
1809 | case BuiltinType::UChar: |
1810 | case BuiltinType::Short: |
1811 | case BuiltinType::UShort: |
1812 | case BuiltinType::WChar_S: |
1813 | case BuiltinType::WChar_U: |
1814 | case BuiltinType::Char8: |
1815 | case BuiltinType::Char16: |
1816 | case BuiltinType::Char32: |
1817 | return true; |
1818 | default: |
1819 | return false; |
1820 | } |
1821 | |
1822 | // Enumerated types are promotable to their compatible integer types |
1823 | // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2). |
1824 | if (const auto *ET = T->getAs<EnumType>()) { |
1825 | if (T->isDependentType() || ET->getDecl()->getPromotionType().isNull() || |
1826 | ET->getDecl()->isScoped()) |
1827 | return false; |
1828 | |
1829 | return true; |
1830 | } |
1831 | |
1832 | return false; |
1833 | } |
1834 | |
1835 | bool ASTContext::isAlignmentRequired(const Type *T) const { |
1836 | return getTypeInfo(T).AlignRequirement != AlignRequirementKind::None; |
1837 | } |
1838 | |
1839 | bool ASTContext::isAlignmentRequired(QualType T) const { |
1840 | return isAlignmentRequired(T: T.getTypePtr()); |
1841 | } |
1842 | |
1843 | unsigned ASTContext::getTypeAlignIfKnown(QualType T, |
1844 | bool NeedsPreferredAlignment) const { |
1845 | // An alignment on a typedef overrides anything else. |
1846 | if (const auto *TT = T->getAs<TypedefType>()) |
1847 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
1848 | return Align; |
1849 | |
1850 | // If we have an (array of) complete type, we're done. |
1851 | T = getBaseElementType(QT: T); |
1852 | if (!T->isIncompleteType()) |
1853 | return NeedsPreferredAlignment ? getPreferredTypeAlign(T) : getTypeAlign(T); |
1854 | |
1855 | // If we had an array type, its element type might be a typedef |
1856 | // type with an alignment attribute. |
1857 | if (const auto *TT = T->getAs<TypedefType>()) |
1858 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
1859 | return Align; |
1860 | |
1861 | // Otherwise, see if the declaration of the type had an attribute. |
1862 | if (const auto *TT = T->getAs<TagType>()) |
1863 | return TT->getDecl()->getMaxAlignment(); |
1864 | |
1865 | return 0; |
1866 | } |
1867 | |
1868 | TypeInfo ASTContext::getTypeInfo(const Type *T) const { |
1869 | TypeInfoMap::iterator I = MemoizedTypeInfo.find(Val: T); |
1870 | if (I != MemoizedTypeInfo.end()) |
1871 | return I->second; |
1872 | |
1873 | // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup. |
1874 | TypeInfo TI = getTypeInfoImpl(T); |
1875 | MemoizedTypeInfo[T] = TI; |
1876 | return TI; |
1877 | } |
1878 | |
1879 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
1880 | /// method does not work on incomplete types. |
1881 | /// |
1882 | /// FIXME: Pointers into different addr spaces could have different sizes and |
1883 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
1884 | /// should take a QualType, &c. |
1885 | TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { |
1886 | uint64_t Width = 0; |
1887 | unsigned Align = 8; |
1888 | AlignRequirementKind AlignRequirement = AlignRequirementKind::None; |
1889 | LangAS AS = LangAS::Default; |
1890 | switch (T->getTypeClass()) { |
1891 | #define TYPE(Class, Base) |
1892 | #define ABSTRACT_TYPE(Class, Base) |
1893 | #define NON_CANONICAL_TYPE(Class, Base) |
1894 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
1895 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \ |
1896 | case Type::Class: \ |
1897 | assert(!T->isDependentType() && "should not see dependent types here"); \ |
1898 | return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr()); |
1899 | #include "clang/AST/TypeNodes.inc" |
1900 | llvm_unreachable("Should not see dependent types" ); |
1901 | |
1902 | case Type::FunctionNoProto: |
1903 | case Type::FunctionProto: |
1904 | // GCC extension: alignof(function) = 32 bits |
1905 | Width = 0; |
1906 | Align = 32; |
1907 | break; |
1908 | |
1909 | case Type::IncompleteArray: |
1910 | case Type::VariableArray: |
1911 | case Type::ConstantArray: |
1912 | case Type::ArrayParameter: { |
1913 | // Model non-constant sized arrays as size zero, but track the alignment. |
1914 | uint64_t Size = 0; |
1915 | if (const auto *CAT = dyn_cast<ConstantArrayType>(T)) |
1916 | Size = CAT->getZExtSize(); |
1917 | |
1918 | TypeInfo EltInfo = getTypeInfo(cast<ArrayType>(T)->getElementType()); |
1919 | assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && |
1920 | "Overflow in array type bit size evaluation" ); |
1921 | Width = EltInfo.Width * Size; |
1922 | Align = EltInfo.Align; |
1923 | AlignRequirement = EltInfo.AlignRequirement; |
1924 | if (!getTargetInfo().getCXXABI().isMicrosoft() || |
1925 | getTargetInfo().getPointerWidth(AddrSpace: LangAS::Default) == 64) |
1926 | Width = llvm::alignTo(Value: Width, Align); |
1927 | break; |
1928 | } |
1929 | |
1930 | case Type::ExtVector: |
1931 | case Type::Vector: { |
1932 | const auto *VT = cast<VectorType>(T); |
1933 | TypeInfo EltInfo = getTypeInfo(VT->getElementType()); |
1934 | Width = VT->isExtVectorBoolType() ? VT->getNumElements() |
1935 | : EltInfo.Width * VT->getNumElements(); |
1936 | // Enforce at least byte size and alignment. |
1937 | Width = std::max<unsigned>(8, Width); |
1938 | Align = std::max<unsigned>(8, Width); |
1939 | |
1940 | // If the alignment is not a power of 2, round up to the next power of 2. |
1941 | // This happens for non-power-of-2 length vectors. |
1942 | if (Align & (Align-1)) { |
1943 | Align = llvm::bit_ceil(Align); |
1944 | Width = llvm::alignTo(Value: Width, Align); |
1945 | } |
1946 | // Adjust the alignment based on the target max. |
1947 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
1948 | if (TargetVectorAlign && TargetVectorAlign < Align) |
1949 | Align = TargetVectorAlign; |
1950 | if (VT->getVectorKind() == VectorKind::SveFixedLengthData) |
1951 | // Adjust the alignment for fixed-length SVE vectors. This is important |
1952 | // for non-power-of-2 vector lengths. |
1953 | Align = 128; |
1954 | else if (VT->getVectorKind() == VectorKind::SveFixedLengthPredicate) |
1955 | // Adjust the alignment for fixed-length SVE predicates. |
1956 | Align = 16; |
1957 | else if (VT->getVectorKind() == VectorKind::RVVFixedLengthData || |
1958 | VT->getVectorKind() == VectorKind::RVVFixedLengthMask) |
1959 | // Adjust the alignment for fixed-length RVV vectors. |
1960 | Align = std::min<unsigned>(64, Width); |
1961 | break; |
1962 | } |
1963 | |
1964 | case Type::ConstantMatrix: { |
1965 | const auto *MT = cast<ConstantMatrixType>(T); |
1966 | TypeInfo ElementInfo = getTypeInfo(MT->getElementType()); |
1967 | // The internal layout of a matrix value is implementation defined. |
1968 | // Initially be ABI compatible with arrays with respect to alignment and |
1969 | // size. |
1970 | Width = ElementInfo.Width * MT->getNumRows() * MT->getNumColumns(); |
1971 | Align = ElementInfo.Align; |
1972 | break; |
1973 | } |
1974 | |
1975 | case Type::Builtin: |
1976 | switch (cast<BuiltinType>(T)->getKind()) { |
1977 | default: llvm_unreachable("Unknown builtin type!" ); |
1978 | case BuiltinType::Void: |
1979 | // GCC extension: alignof(void) = 8 bits. |
1980 | Width = 0; |
1981 | Align = 8; |
1982 | break; |
1983 | case BuiltinType::Bool: |
1984 | Width = Target->getBoolWidth(); |
1985 | Align = Target->getBoolAlign(); |
1986 | break; |
1987 | case BuiltinType::Char_S: |
1988 | case BuiltinType::Char_U: |
1989 | case BuiltinType::UChar: |
1990 | case BuiltinType::SChar: |
1991 | case BuiltinType::Char8: |
1992 | Width = Target->getCharWidth(); |
1993 | Align = Target->getCharAlign(); |
1994 | break; |
1995 | case BuiltinType::WChar_S: |
1996 | case BuiltinType::WChar_U: |
1997 | Width = Target->getWCharWidth(); |
1998 | Align = Target->getWCharAlign(); |
1999 | break; |
2000 | case BuiltinType::Char16: |
2001 | Width = Target->getChar16Width(); |
2002 | Align = Target->getChar16Align(); |
2003 | break; |
2004 | case BuiltinType::Char32: |
2005 | Width = Target->getChar32Width(); |
2006 | Align = Target->getChar32Align(); |
2007 | break; |
2008 | case BuiltinType::UShort: |
2009 | case BuiltinType::Short: |
2010 | Width = Target->getShortWidth(); |
2011 | Align = Target->getShortAlign(); |
2012 | break; |
2013 | case BuiltinType::UInt: |
2014 | case BuiltinType::Int: |
2015 | Width = Target->getIntWidth(); |
2016 | Align = Target->getIntAlign(); |
2017 | break; |
2018 | case BuiltinType::ULong: |
2019 | case BuiltinType::Long: |
2020 | Width = Target->getLongWidth(); |
2021 | Align = Target->getLongAlign(); |
2022 | break; |
2023 | case BuiltinType::ULongLong: |
2024 | case BuiltinType::LongLong: |
2025 | Width = Target->getLongLongWidth(); |
2026 | Align = Target->getLongLongAlign(); |
2027 | break; |
2028 | case BuiltinType::Int128: |
2029 | case BuiltinType::UInt128: |
2030 | Width = 128; |
2031 | Align = Target->getInt128Align(); |
2032 | break; |
2033 | case BuiltinType::ShortAccum: |
2034 | case BuiltinType::UShortAccum: |
2035 | case BuiltinType::SatShortAccum: |
2036 | case BuiltinType::SatUShortAccum: |
2037 | Width = Target->getShortAccumWidth(); |
2038 | Align = Target->getShortAccumAlign(); |
2039 | break; |
2040 | case BuiltinType::Accum: |
2041 | case BuiltinType::UAccum: |
2042 | case BuiltinType::SatAccum: |
2043 | case BuiltinType::SatUAccum: |
2044 | Width = Target->getAccumWidth(); |
2045 | Align = Target->getAccumAlign(); |
2046 | break; |
2047 | case BuiltinType::LongAccum: |
2048 | case BuiltinType::ULongAccum: |
2049 | case BuiltinType::SatLongAccum: |
2050 | case BuiltinType::SatULongAccum: |
2051 | Width = Target->getLongAccumWidth(); |
2052 | Align = Target->getLongAccumAlign(); |
2053 | break; |
2054 | case BuiltinType::ShortFract: |
2055 | case BuiltinType::UShortFract: |
2056 | case BuiltinType::SatShortFract: |
2057 | case BuiltinType::SatUShortFract: |
2058 | Width = Target->getShortFractWidth(); |
2059 | Align = Target->getShortFractAlign(); |
2060 | break; |
2061 | case BuiltinType::Fract: |
2062 | case BuiltinType::UFract: |
2063 | case BuiltinType::SatFract: |
2064 | case BuiltinType::SatUFract: |
2065 | Width = Target->getFractWidth(); |
2066 | Align = Target->getFractAlign(); |
2067 | break; |
2068 | case BuiltinType::LongFract: |
2069 | case BuiltinType::ULongFract: |
2070 | case BuiltinType::SatLongFract: |
2071 | case BuiltinType::SatULongFract: |
2072 | Width = Target->getLongFractWidth(); |
2073 | Align = Target->getLongFractAlign(); |
2074 | break; |
2075 | case BuiltinType::BFloat16: |
2076 | if (Target->hasBFloat16Type()) { |
2077 | Width = Target->getBFloat16Width(); |
2078 | Align = Target->getBFloat16Align(); |
2079 | } else if ((getLangOpts().SYCLIsDevice || |
2080 | (getLangOpts().OpenMP && |
2081 | getLangOpts().OpenMPIsTargetDevice)) && |
2082 | AuxTarget->hasBFloat16Type()) { |
2083 | Width = AuxTarget->getBFloat16Width(); |
2084 | Align = AuxTarget->getBFloat16Align(); |
2085 | } |
2086 | break; |
2087 | case BuiltinType::Float16: |
2088 | case BuiltinType::Half: |
2089 | if (Target->hasFloat16Type() || !getLangOpts().OpenMP || |
2090 | !getLangOpts().OpenMPIsTargetDevice) { |
2091 | Width = Target->getHalfWidth(); |
2092 | Align = Target->getHalfAlign(); |
2093 | } else { |
2094 | assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice && |
2095 | "Expected OpenMP device compilation." ); |
2096 | Width = AuxTarget->getHalfWidth(); |
2097 | Align = AuxTarget->getHalfAlign(); |
2098 | } |
2099 | break; |
2100 | case BuiltinType::Float: |
2101 | Width = Target->getFloatWidth(); |
2102 | Align = Target->getFloatAlign(); |
2103 | break; |
2104 | case BuiltinType::Double: |
2105 | Width = Target->getDoubleWidth(); |
2106 | Align = Target->getDoubleAlign(); |
2107 | break; |
2108 | case BuiltinType::Ibm128: |
2109 | Width = Target->getIbm128Width(); |
2110 | Align = Target->getIbm128Align(); |
2111 | break; |
2112 | case BuiltinType::LongDouble: |
2113 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice && |
2114 | (Target->getLongDoubleWidth() != AuxTarget->getLongDoubleWidth() || |
2115 | Target->getLongDoubleAlign() != AuxTarget->getLongDoubleAlign())) { |
2116 | Width = AuxTarget->getLongDoubleWidth(); |
2117 | Align = AuxTarget->getLongDoubleAlign(); |
2118 | } else { |
2119 | Width = Target->getLongDoubleWidth(); |
2120 | Align = Target->getLongDoubleAlign(); |
2121 | } |
2122 | break; |
2123 | case BuiltinType::Float128: |
2124 | if (Target->hasFloat128Type() || !getLangOpts().OpenMP || |
2125 | !getLangOpts().OpenMPIsTargetDevice) { |
2126 | Width = Target->getFloat128Width(); |
2127 | Align = Target->getFloat128Align(); |
2128 | } else { |
2129 | assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice && |
2130 | "Expected OpenMP device compilation." ); |
2131 | Width = AuxTarget->getFloat128Width(); |
2132 | Align = AuxTarget->getFloat128Align(); |
2133 | } |
2134 | break; |
2135 | case BuiltinType::NullPtr: |
2136 | // C++ 3.9.1p11: sizeof(nullptr_t) == sizeof(void*) |
2137 | Width = Target->getPointerWidth(AddrSpace: LangAS::Default); |
2138 | Align = Target->getPointerAlign(AddrSpace: LangAS::Default); |
2139 | break; |
2140 | case BuiltinType::ObjCId: |
2141 | case BuiltinType::ObjCClass: |
2142 | case BuiltinType::ObjCSel: |
2143 | Width = Target->getPointerWidth(AddrSpace: LangAS::Default); |
2144 | Align = Target->getPointerAlign(AddrSpace: LangAS::Default); |
2145 | break; |
2146 | case BuiltinType::OCLSampler: |
2147 | case BuiltinType::OCLEvent: |
2148 | case BuiltinType::OCLClkEvent: |
2149 | case BuiltinType::OCLQueue: |
2150 | case BuiltinType::OCLReserveID: |
2151 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
2152 | case BuiltinType::Id: |
2153 | #include "clang/Basic/OpenCLImageTypes.def" |
2154 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
2155 | case BuiltinType::Id: |
2156 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2157 | AS = Target->getOpenCLTypeAddrSpace(TK: getOpenCLTypeKind(T)); |
2158 | Width = Target->getPointerWidth(AddrSpace: AS); |
2159 | Align = Target->getPointerAlign(AddrSpace: AS); |
2160 | break; |
2161 | // The SVE types are effectively target-specific. The length of an |
2162 | // SVE_VECTOR_TYPE is only known at runtime, but it is always a multiple |
2163 | // of 128 bits. There is one predicate bit for each vector byte, so the |
2164 | // length of an SVE_PREDICATE_TYPE is always a multiple of 16 bits. |
2165 | // |
2166 | // Because the length is only known at runtime, we use a dummy value |
2167 | // of 0 for the static length. The alignment values are those defined |
2168 | // by the Procedure Call Standard for the Arm Architecture. |
2169 | #define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId, NumEls, ElBits, \ |
2170 | IsSigned, IsFP, IsBF) \ |
2171 | case BuiltinType::Id: \ |
2172 | Width = 0; \ |
2173 | Align = 128; \ |
2174 | break; |
2175 | #define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId, NumEls) \ |
2176 | case BuiltinType::Id: \ |
2177 | Width = 0; \ |
2178 | Align = 16; \ |
2179 | break; |
2180 | #define SVE_OPAQUE_TYPE(Name, MangledName, Id, SingletonId) \ |
2181 | case BuiltinType::Id: \ |
2182 | Width = 0; \ |
2183 | Align = 16; \ |
2184 | break; |
2185 | #include "clang/Basic/AArch64SVEACLETypes.def" |
2186 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
2187 | case BuiltinType::Id: \ |
2188 | Width = Size; \ |
2189 | Align = Size; \ |
2190 | break; |
2191 | #include "clang/Basic/PPCTypes.def" |
2192 | #define RVV_VECTOR_TYPE(Name, Id, SingletonId, ElKind, ElBits, NF, IsSigned, \ |
2193 | IsFP, IsBF) \ |
2194 | case BuiltinType::Id: \ |
2195 | Width = 0; \ |
2196 | Align = ElBits; \ |
2197 | break; |
2198 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, ElKind) \ |
2199 | case BuiltinType::Id: \ |
2200 | Width = 0; \ |
2201 | Align = 8; \ |
2202 | break; |
2203 | #include "clang/Basic/RISCVVTypes.def" |
2204 | #define WASM_TYPE(Name, Id, SingletonId) \ |
2205 | case BuiltinType::Id: \ |
2206 | Width = 0; \ |
2207 | Align = 8; \ |
2208 | break; |
2209 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
2210 | } |
2211 | break; |
2212 | case Type::ObjCObjectPointer: |
2213 | Width = Target->getPointerWidth(AddrSpace: LangAS::Default); |
2214 | Align = Target->getPointerAlign(AddrSpace: LangAS::Default); |
2215 | break; |
2216 | case Type::BlockPointer: |
2217 | AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
2218 | Width = Target->getPointerWidth(AddrSpace: AS); |
2219 | Align = Target->getPointerAlign(AddrSpace: AS); |
2220 | break; |
2221 | case Type::LValueReference: |
2222 | case Type::RValueReference: |
2223 | // alignof and sizeof should never enter this code path here, so we go |
2224 | // the pointer route. |
2225 | AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
2226 | Width = Target->getPointerWidth(AddrSpace: AS); |
2227 | Align = Target->getPointerAlign(AddrSpace: AS); |
2228 | break; |
2229 | case Type::Pointer: |
2230 | AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
2231 | Width = Target->getPointerWidth(AddrSpace: AS); |
2232 | Align = Target->getPointerAlign(AddrSpace: AS); |
2233 | break; |
2234 | case Type::MemberPointer: { |
2235 | const auto *MPT = cast<MemberPointerType>(T); |
2236 | CXXABI::MemberPointerInfo MPI = ABI->getMemberPointerInfo(MPT: MPT); |
2237 | Width = MPI.Width; |
2238 | Align = MPI.Align; |
2239 | break; |
2240 | } |
2241 | case Type::Complex: { |
2242 | // Complex types have the same alignment as their elements, but twice the |
2243 | // size. |
2244 | TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType()); |
2245 | Width = EltInfo.Width * 2; |
2246 | Align = EltInfo.Align; |
2247 | break; |
2248 | } |
2249 | case Type::ObjCObject: |
2250 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
2251 | case Type::Adjusted: |
2252 | case Type::Decayed: |
2253 | return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr()); |
2254 | case Type::ObjCInterface: { |
2255 | const auto *ObjCI = cast<ObjCInterfaceType>(T); |
2256 | if (ObjCI->getDecl()->isInvalidDecl()) { |
2257 | Width = 8; |
2258 | Align = 8; |
2259 | break; |
2260 | } |
2261 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(D: ObjCI->getDecl()); |
2262 | Width = toBits(CharSize: Layout.getSize()); |
2263 | Align = toBits(CharSize: Layout.getAlignment()); |
2264 | break; |
2265 | } |
2266 | case Type::BitInt: { |
2267 | const auto *EIT = cast<BitIntType>(T); |
2268 | Align = std::clamp<unsigned>(llvm::PowerOf2Ceil(A: EIT->getNumBits()), |
2269 | getCharWidth(), Target->getLongLongAlign()); |
2270 | Width = llvm::alignTo(EIT->getNumBits(), Align); |
2271 | break; |
2272 | } |
2273 | case Type::Record: |
2274 | case Type::Enum: { |
2275 | const auto *TT = cast<TagType>(T); |
2276 | |
2277 | if (TT->getDecl()->isInvalidDecl()) { |
2278 | Width = 8; |
2279 | Align = 8; |
2280 | break; |
2281 | } |
2282 | |
2283 | if (const auto *ET = dyn_cast<EnumType>(TT)) { |
2284 | const EnumDecl *ED = ET->getDecl(); |
2285 | TypeInfo Info = |
2286 | getTypeInfo(T: ED->getIntegerType()->getUnqualifiedDesugaredType()); |
2287 | if (unsigned AttrAlign = ED->getMaxAlignment()) { |
2288 | Info.Align = AttrAlign; |
2289 | Info.AlignRequirement = AlignRequirementKind::RequiredByEnum; |
2290 | } |
2291 | return Info; |
2292 | } |
2293 | |
2294 | const auto *RT = cast<RecordType>(TT); |
2295 | const RecordDecl *RD = RT->getDecl(); |
2296 | const ASTRecordLayout &Layout = getASTRecordLayout(D: RD); |
2297 | Width = toBits(CharSize: Layout.getSize()); |
2298 | Align = toBits(CharSize: Layout.getAlignment()); |
2299 | AlignRequirement = RD->hasAttr<AlignedAttr>() |
2300 | ? AlignRequirementKind::RequiredByRecord |
2301 | : AlignRequirementKind::None; |
2302 | break; |
2303 | } |
2304 | |
2305 | case Type::SubstTemplateTypeParm: |
2306 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
2307 | getReplacementType().getTypePtr()); |
2308 | |
2309 | case Type::Auto: |
2310 | case Type::DeducedTemplateSpecialization: { |
2311 | const auto *A = cast<DeducedType>(T); |
2312 | assert(!A->getDeducedType().isNull() && |
2313 | "cannot request the size of an undeduced or dependent auto type" ); |
2314 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
2315 | } |
2316 | |
2317 | case Type::Paren: |
2318 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
2319 | |
2320 | case Type::MacroQualified: |
2321 | return getTypeInfo( |
2322 | cast<MacroQualifiedType>(T)->getUnderlyingType().getTypePtr()); |
2323 | |
2324 | case Type::ObjCTypeParam: |
2325 | return getTypeInfo(cast<ObjCTypeParamType>(T)->desugar().getTypePtr()); |
2326 | |
2327 | case Type::Using: |
2328 | return getTypeInfo(cast<UsingType>(T)->desugar().getTypePtr()); |
2329 | |
2330 | case Type::Typedef: { |
2331 | const auto *TT = cast<TypedefType>(T); |
2332 | TypeInfo Info = getTypeInfo(TT->desugar().getTypePtr()); |
2333 | // If the typedef has an aligned attribute on it, it overrides any computed |
2334 | // alignment we have. This violates the GCC documentation (which says that |
2335 | // attribute(aligned) can only round up) but matches its implementation. |
2336 | if (unsigned AttrAlign = TT->getDecl()->getMaxAlignment()) { |
2337 | Align = AttrAlign; |
2338 | AlignRequirement = AlignRequirementKind::RequiredByTypedef; |
2339 | } else { |
2340 | Align = Info.Align; |
2341 | AlignRequirement = Info.AlignRequirement; |
2342 | } |
2343 | Width = Info.Width; |
2344 | break; |
2345 | } |
2346 | |
2347 | case Type::Elaborated: |
2348 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
2349 | |
2350 | case Type::Attributed: |
2351 | return getTypeInfo( |
2352 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
2353 | |
2354 | case Type::CountAttributed: |
2355 | return getTypeInfo(cast<CountAttributedType>(T)->desugar().getTypePtr()); |
2356 | |
2357 | case Type::BTFTagAttributed: |
2358 | return getTypeInfo( |
2359 | cast<BTFTagAttributedType>(T)->getWrappedType().getTypePtr()); |
2360 | |
2361 | case Type::Atomic: { |
2362 | // Start with the base type information. |
2363 | TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
2364 | Width = Info.Width; |
2365 | Align = Info.Align; |
2366 | |
2367 | if (!Width) { |
2368 | // An otherwise zero-sized type should still generate an |
2369 | // atomic operation. |
2370 | Width = Target->getCharWidth(); |
2371 | assert(Align); |
2372 | } else if (Width <= Target->getMaxAtomicPromoteWidth()) { |
2373 | // If the size of the type doesn't exceed the platform's max |
2374 | // atomic promotion width, make the size and alignment more |
2375 | // favorable to atomic operations: |
2376 | |
2377 | // Round the size up to a power of 2. |
2378 | Width = llvm::bit_ceil(Width); |
2379 | |
2380 | // Set the alignment equal to the size. |
2381 | Align = static_cast<unsigned>(Width); |
2382 | } |
2383 | } |
2384 | break; |
2385 | |
2386 | case Type::Pipe: |
2387 | Width = Target->getPointerWidth(AddrSpace: LangAS::opencl_global); |
2388 | Align = Target->getPointerAlign(AddrSpace: LangAS::opencl_global); |
2389 | break; |
2390 | } |
2391 | |
2392 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2" ); |
2393 | return TypeInfo(Width, Align, AlignRequirement); |
2394 | } |
2395 | |
2396 | unsigned ASTContext::getTypeUnadjustedAlign(const Type *T) const { |
2397 | UnadjustedAlignMap::iterator I = MemoizedUnadjustedAlign.find(Val: T); |
2398 | if (I != MemoizedUnadjustedAlign.end()) |
2399 | return I->second; |
2400 | |
2401 | unsigned UnadjustedAlign; |
2402 | if (const auto *RT = T->getAs<RecordType>()) { |
2403 | const RecordDecl *RD = RT->getDecl(); |
2404 | const ASTRecordLayout &Layout = getASTRecordLayout(D: RD); |
2405 | UnadjustedAlign = toBits(CharSize: Layout.getUnadjustedAlignment()); |
2406 | } else if (const auto *ObjCI = T->getAs<ObjCInterfaceType>()) { |
2407 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(D: ObjCI->getDecl()); |
2408 | UnadjustedAlign = toBits(CharSize: Layout.getUnadjustedAlignment()); |
2409 | } else { |
2410 | UnadjustedAlign = getTypeAlign(T: T->getUnqualifiedDesugaredType()); |
2411 | } |
2412 | |
2413 | MemoizedUnadjustedAlign[T] = UnadjustedAlign; |
2414 | return UnadjustedAlign; |
2415 | } |
2416 | |
2417 | unsigned ASTContext::getOpenMPDefaultSimdAlign(QualType T) const { |
2418 | unsigned SimdAlign = llvm::OpenMPIRBuilder::getOpenMPDefaultSimdAlign( |
2419 | TargetTriple: getTargetInfo().getTriple(), Features: Target->getTargetOpts().FeatureMap); |
2420 | return SimdAlign; |
2421 | } |
2422 | |
2423 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
2424 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
2425 | return CharUnits::fromQuantity(Quantity: BitSize / getCharWidth()); |
2426 | } |
2427 | |
2428 | /// toBits - Convert a size in characters to a size in characters. |
2429 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
2430 | return CharSize.getQuantity() * getCharWidth(); |
2431 | } |
2432 | |
2433 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
2434 | /// This method does not work on incomplete types. |
2435 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
2436 | return getTypeInfoInChars(T).Width; |
2437 | } |
2438 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
2439 | return getTypeInfoInChars(T).Width; |
2440 | } |
2441 | |
2442 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
2443 | /// characters. This method does not work on incomplete types. |
2444 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
2445 | return toCharUnitsFromBits(BitSize: getTypeAlign(T)); |
2446 | } |
2447 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
2448 | return toCharUnitsFromBits(BitSize: getTypeAlign(T)); |
2449 | } |
2450 | |
2451 | /// getTypeUnadjustedAlignInChars - Return the ABI-specified alignment of a |
2452 | /// type, in characters, before alignment adjustments. This method does |
2453 | /// not work on incomplete types. |
2454 | CharUnits ASTContext::getTypeUnadjustedAlignInChars(QualType T) const { |
2455 | return toCharUnitsFromBits(BitSize: getTypeUnadjustedAlign(T)); |
2456 | } |
2457 | CharUnits ASTContext::getTypeUnadjustedAlignInChars(const Type *T) const { |
2458 | return toCharUnitsFromBits(BitSize: getTypeUnadjustedAlign(T)); |
2459 | } |
2460 | |
2461 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
2462 | /// type for the current target in bits. This can be different than the ABI |
2463 | /// alignment in cases where it is beneficial for performance or backwards |
2464 | /// compatibility preserving to overalign a data type. (Note: despite the name, |
2465 | /// the preferred alignment is ABI-impacting, and not an optimization.) |
2466 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
2467 | TypeInfo TI = getTypeInfo(T); |
2468 | unsigned ABIAlign = TI.Align; |
2469 | |
2470 | T = T->getBaseElementTypeUnsafe(); |
2471 | |
2472 | // The preferred alignment of member pointers is that of a pointer. |
2473 | if (T->isMemberPointerType()) |
2474 | return getPreferredTypeAlign(T: getPointerDiffType().getTypePtr()); |
2475 | |
2476 | if (!Target->allowsLargerPreferedTypeAlignment()) |
2477 | return ABIAlign; |
2478 | |
2479 | if (const auto *RT = T->getAs<RecordType>()) { |
2480 | const RecordDecl *RD = RT->getDecl(); |
2481 | |
2482 | // When used as part of a typedef, or together with a 'packed' attribute, |
2483 | // the 'aligned' attribute can be used to decrease alignment. Note that the |
2484 | // 'packed' case is already taken into consideration when computing the |
2485 | // alignment, we only need to handle the typedef case here. |
2486 | if (TI.AlignRequirement == AlignRequirementKind::RequiredByTypedef || |
2487 | RD->isInvalidDecl()) |
2488 | return ABIAlign; |
2489 | |
2490 | unsigned PreferredAlign = static_cast<unsigned>( |
2491 | toBits(CharSize: getASTRecordLayout(D: RD).PreferredAlignment)); |
2492 | assert(PreferredAlign >= ABIAlign && |
2493 | "PreferredAlign should be at least as large as ABIAlign." ); |
2494 | return PreferredAlign; |
2495 | } |
2496 | |
2497 | // Double (and, for targets supporting AIX `power` alignment, long double) and |
2498 | // long long should be naturally aligned (despite requiring less alignment) if |
2499 | // possible. |
2500 | if (const auto *CT = T->getAs<ComplexType>()) |
2501 | T = CT->getElementType().getTypePtr(); |
2502 | if (const auto *ET = T->getAs<EnumType>()) |
2503 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
2504 | if (T->isSpecificBuiltinType(K: BuiltinType::Double) || |
2505 | T->isSpecificBuiltinType(K: BuiltinType::LongLong) || |
2506 | T->isSpecificBuiltinType(K: BuiltinType::ULongLong) || |
2507 | (T->isSpecificBuiltinType(K: BuiltinType::LongDouble) && |
2508 | Target->defaultsToAIXPowerAlignment())) |
2509 | // Don't increase the alignment if an alignment attribute was specified on a |
2510 | // typedef declaration. |
2511 | if (!TI.isAlignRequired()) |
2512 | return std::max(a: ABIAlign, b: (unsigned)getTypeSize(T)); |
2513 | |
2514 | return ABIAlign; |
2515 | } |
2516 | |
2517 | /// getTargetDefaultAlignForAttributeAligned - Return the default alignment |
2518 | /// for __attribute__((aligned)) on this target, to be used if no alignment |
2519 | /// value is specified. |
2520 | unsigned ASTContext::getTargetDefaultAlignForAttributeAligned() const { |
2521 | return getTargetInfo().getDefaultAlignForAttributeAligned(); |
2522 | } |
2523 | |
2524 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
2525 | /// to a global variable of the specified type. |
2526 | unsigned ASTContext::getAlignOfGlobalVar(QualType T, const VarDecl *VD) const { |
2527 | uint64_t TypeSize = getTypeSize(T: T.getTypePtr()); |
2528 | return std::max(a: getPreferredTypeAlign(T), |
2529 | b: getMinGlobalAlignOfVar(Size: TypeSize, VD)); |
2530 | } |
2531 | |
2532 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
2533 | /// should be given to a global variable of the specified type. |
2534 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T, |
2535 | const VarDecl *VD) const { |
2536 | return toCharUnitsFromBits(BitSize: getAlignOfGlobalVar(T, VD)); |
2537 | } |
2538 | |
2539 | unsigned ASTContext::getMinGlobalAlignOfVar(uint64_t Size, |
2540 | const VarDecl *VD) const { |
2541 | // Make the default handling as that of a non-weak definition in the |
2542 | // current translation unit. |
2543 | bool HasNonWeakDef = !VD || (VD->hasDefinition() && !VD->isWeak()); |
2544 | return getTargetInfo().getMinGlobalAlign(Size, HasNonWeakDef); |
2545 | } |
2546 | |
2547 | CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const { |
2548 | CharUnits Offset = CharUnits::Zero(); |
2549 | const ASTRecordLayout *Layout = &getASTRecordLayout(RD); |
2550 | while (const CXXRecordDecl *Base = Layout->getBaseSharingVBPtr()) { |
2551 | Offset += Layout->getBaseClassOffset(Base); |
2552 | Layout = &getASTRecordLayout(Base); |
2553 | } |
2554 | return Offset; |
2555 | } |
2556 | |
2557 | CharUnits ASTContext::getMemberPointerPathAdjustment(const APValue &MP) const { |
2558 | const ValueDecl *MPD = MP.getMemberPointerDecl(); |
2559 | CharUnits ThisAdjustment = CharUnits::Zero(); |
2560 | ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath(); |
2561 | bool DerivedMember = MP.isMemberPointerToDerivedMember(); |
2562 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext()); |
2563 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
2564 | const CXXRecordDecl *Base = RD; |
2565 | const CXXRecordDecl *Derived = Path[I]; |
2566 | if (DerivedMember) |
2567 | std::swap(a&: Base, b&: Derived); |
2568 | ThisAdjustment += getASTRecordLayout(Derived).getBaseClassOffset(Base); |
2569 | RD = Path[I]; |
2570 | } |
2571 | if (DerivedMember) |
2572 | ThisAdjustment = -ThisAdjustment; |
2573 | return ThisAdjustment; |
2574 | } |
2575 | |
2576 | /// DeepCollectObjCIvars - |
2577 | /// This routine first collects all declared, but not synthesized, ivars in |
2578 | /// super class and then collects all ivars, including those synthesized for |
2579 | /// current class. This routine is used for implementation of current class |
2580 | /// when all ivars, declared and synthesized are known. |
2581 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
2582 | bool leafClass, |
2583 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
2584 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
2585 | DeepCollectObjCIvars(OI: SuperClass, leafClass: false, Ivars); |
2586 | if (!leafClass) { |
2587 | llvm::append_range(C&: Ivars, R: OI->ivars()); |
2588 | } else { |
2589 | auto *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
2590 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
2591 | Iv= Iv->getNextIvar()) |
2592 | Ivars.push_back(Elt: Iv); |
2593 | } |
2594 | } |
2595 | |
2596 | /// CollectInheritedProtocols - Collect all protocols in current class and |
2597 | /// those inherited by it. |
2598 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
2599 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
2600 | if (const auto *OI = dyn_cast<ObjCInterfaceDecl>(Val: CDecl)) { |
2601 | // We can use protocol_iterator here instead of |
2602 | // all_referenced_protocol_iterator since we are walking all categories. |
2603 | for (auto *Proto : OI->all_referenced_protocols()) { |
2604 | CollectInheritedProtocols(Proto, Protocols); |
2605 | } |
2606 | |
2607 | // Categories of this Interface. |
2608 | for (const auto *Cat : OI->visible_categories()) |
2609 | CollectInheritedProtocols(Cat, Protocols); |
2610 | |
2611 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
2612 | while (SD) { |
2613 | CollectInheritedProtocols(SD, Protocols); |
2614 | SD = SD->getSuperClass(); |
2615 | } |
2616 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(Val: CDecl)) { |
2617 | for (auto *Proto : OC->protocols()) { |
2618 | CollectInheritedProtocols(Proto, Protocols); |
2619 | } |
2620 | } else if (const auto *OP = dyn_cast<ObjCProtocolDecl>(Val: CDecl)) { |
2621 | // Insert the protocol. |
2622 | if (!Protocols.insert( |
2623 | Ptr: const_cast<ObjCProtocolDecl *>(OP->getCanonicalDecl())).second) |
2624 | return; |
2625 | |
2626 | for (auto *Proto : OP->protocols()) |
2627 | CollectInheritedProtocols(Proto, Protocols); |
2628 | } |
2629 | } |
2630 | |
2631 | static bool unionHasUniqueObjectRepresentations(const ASTContext &Context, |
2632 | const RecordDecl *RD, |
2633 | bool CheckIfTriviallyCopyable) { |
2634 | assert(RD->isUnion() && "Must be union type" ); |
2635 | CharUnits UnionSize = Context.getTypeSizeInChars(RD->getTypeForDecl()); |
2636 | |
2637 | for (const auto *Field : RD->fields()) { |
2638 | if (!Context.hasUniqueObjectRepresentations(Ty: Field->getType(), |
2639 | CheckIfTriviallyCopyable)) |
2640 | return false; |
2641 | CharUnits FieldSize = Context.getTypeSizeInChars(Field->getType()); |
2642 | if (FieldSize != UnionSize) |
2643 | return false; |
2644 | } |
2645 | return !RD->field_empty(); |
2646 | } |
2647 | |
2648 | static int64_t getSubobjectOffset(const FieldDecl *Field, |
2649 | const ASTContext &Context, |
2650 | const clang::ASTRecordLayout & /*Layout*/) { |
2651 | return Context.getFieldOffset(Field); |
2652 | } |
2653 | |
2654 | static int64_t getSubobjectOffset(const CXXRecordDecl *RD, |
2655 | const ASTContext &Context, |
2656 | const clang::ASTRecordLayout &Layout) { |
2657 | return Context.toBits(CharSize: Layout.getBaseClassOffset(Base: RD)); |
2658 | } |
2659 | |
2660 | static std::optional<int64_t> |
2661 | structHasUniqueObjectRepresentations(const ASTContext &Context, |
2662 | const RecordDecl *RD, |
2663 | bool CheckIfTriviallyCopyable); |
2664 | |
2665 | static std::optional<int64_t> |
2666 | getSubobjectSizeInBits(const FieldDecl *Field, const ASTContext &Context, |
2667 | bool CheckIfTriviallyCopyable) { |
2668 | if (Field->getType()->isRecordType()) { |
2669 | const RecordDecl *RD = Field->getType()->getAsRecordDecl(); |
2670 | if (!RD->isUnion()) |
2671 | return structHasUniqueObjectRepresentations(Context, RD, |
2672 | CheckIfTriviallyCopyable); |
2673 | } |
2674 | |
2675 | // A _BitInt type may not be unique if it has padding bits |
2676 | // but if it is a bitfield the padding bits are not used. |
2677 | bool IsBitIntType = Field->getType()->isBitIntType(); |
2678 | if (!Field->getType()->isReferenceType() && !IsBitIntType && |
2679 | !Context.hasUniqueObjectRepresentations(Ty: Field->getType(), |
2680 | CheckIfTriviallyCopyable)) |
2681 | return std::nullopt; |
2682 | |
2683 | int64_t FieldSizeInBits = |
2684 | Context.toBits(CharSize: Context.getTypeSizeInChars(Field->getType())); |
2685 | if (Field->isBitField()) { |
2686 | // If we have explicit padding bits, they don't contribute bits |
2687 | // to the actual object representation, so return 0. |
2688 | if (Field->isUnnamedBitField()) |
2689 | return 0; |
2690 | |
2691 | int64_t BitfieldSize = Field->getBitWidthValue(Ctx: Context); |
2692 | if (IsBitIntType) { |
2693 | if ((unsigned)BitfieldSize > |
2694 | cast<BitIntType>(Field->getType())->getNumBits()) |
2695 | return std::nullopt; |
2696 | } else if (BitfieldSize > FieldSizeInBits) { |
2697 | return std::nullopt; |
2698 | } |
2699 | FieldSizeInBits = BitfieldSize; |
2700 | } else if (IsBitIntType && !Context.hasUniqueObjectRepresentations( |
2701 | Ty: Field->getType(), CheckIfTriviallyCopyable)) { |
2702 | return std::nullopt; |
2703 | } |
2704 | return FieldSizeInBits; |
2705 | } |
2706 | |
2707 | static std::optional<int64_t> |
2708 | getSubobjectSizeInBits(const CXXRecordDecl *RD, const ASTContext &Context, |
2709 | bool CheckIfTriviallyCopyable) { |
2710 | return structHasUniqueObjectRepresentations(Context, RD, |
2711 | CheckIfTriviallyCopyable); |
2712 | } |
2713 | |
2714 | template <typename RangeT> |
2715 | static std::optional<int64_t> structSubobjectsHaveUniqueObjectRepresentations( |
2716 | const RangeT &Subobjects, int64_t CurOffsetInBits, |
2717 | const ASTContext &Context, const clang::ASTRecordLayout &Layout, |
2718 | bool CheckIfTriviallyCopyable) { |
2719 | for (const auto *Subobject : Subobjects) { |
2720 | std::optional<int64_t> SizeInBits = |
2721 | getSubobjectSizeInBits(Subobject, Context, CheckIfTriviallyCopyable); |
2722 | if (!SizeInBits) |
2723 | return std::nullopt; |
2724 | if (*SizeInBits != 0) { |
2725 | int64_t Offset = getSubobjectOffset(Subobject, Context, Layout); |
2726 | if (Offset != CurOffsetInBits) |
2727 | return std::nullopt; |
2728 | CurOffsetInBits += *SizeInBits; |
2729 | } |
2730 | } |
2731 | return CurOffsetInBits; |
2732 | } |
2733 | |
2734 | static std::optional<int64_t> |
2735 | structHasUniqueObjectRepresentations(const ASTContext &Context, |
2736 | const RecordDecl *RD, |
2737 | bool CheckIfTriviallyCopyable) { |
2738 | assert(!RD->isUnion() && "Must be struct/class type" ); |
2739 | const auto &Layout = Context.getASTRecordLayout(D: RD); |
2740 | |
2741 | int64_t CurOffsetInBits = 0; |
2742 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(Val: RD)) { |
2743 | if (ClassDecl->isDynamicClass()) |
2744 | return std::nullopt; |
2745 | |
2746 | SmallVector<CXXRecordDecl *, 4> Bases; |
2747 | for (const auto &Base : ClassDecl->bases()) { |
2748 | // Empty types can be inherited from, and non-empty types can potentially |
2749 | // have tail padding, so just make sure there isn't an error. |
2750 | Bases.emplace_back(Args: Base.getType()->getAsCXXRecordDecl()); |
2751 | } |
2752 | |
2753 | llvm::sort(C&: Bases, Comp: [&](const CXXRecordDecl *L, const CXXRecordDecl *R) { |
2754 | return Layout.getBaseClassOffset(Base: L) < Layout.getBaseClassOffset(Base: R); |
2755 | }); |
2756 | |
2757 | std::optional<int64_t> OffsetAfterBases = |
2758 | structSubobjectsHaveUniqueObjectRepresentations( |
2759 | Subobjects: Bases, CurOffsetInBits, Context, Layout, CheckIfTriviallyCopyable); |
2760 | if (!OffsetAfterBases) |
2761 | return std::nullopt; |
2762 | CurOffsetInBits = *OffsetAfterBases; |
2763 | } |
2764 | |
2765 | std::optional<int64_t> OffsetAfterFields = |
2766 | structSubobjectsHaveUniqueObjectRepresentations( |
2767 | Subobjects: RD->fields(), CurOffsetInBits, Context, Layout, |
2768 | CheckIfTriviallyCopyable); |
2769 | if (!OffsetAfterFields) |
2770 | return std::nullopt; |
2771 | CurOffsetInBits = *OffsetAfterFields; |
2772 | |
2773 | return CurOffsetInBits; |
2774 | } |
2775 | |
2776 | bool ASTContext::hasUniqueObjectRepresentations( |
2777 | QualType Ty, bool CheckIfTriviallyCopyable) const { |
2778 | // C++17 [meta.unary.prop]: |
2779 | // The predicate condition for a template specialization |
2780 | // has_unique_object_representations<T> shall be satisfied if and only if: |
2781 | // (9.1) - T is trivially copyable, and |
2782 | // (9.2) - any two objects of type T with the same value have the same |
2783 | // object representation, where: |
2784 | // - two objects of array or non-union class type are considered to have |
2785 | // the same value if their respective sequences of direct subobjects |
2786 | // have the same values, and |
2787 | // - two objects of union type are considered to have the same value if |
2788 | // they have the same active member and the corresponding members have |
2789 | // the same value. |
2790 | // The set of scalar types for which this condition holds is |
2791 | // implementation-defined. [ Note: If a type has padding bits, the condition |
2792 | // does not hold; otherwise, the condition holds true for unsigned integral |
2793 | // types. -- end note ] |
2794 | assert(!Ty.isNull() && "Null QualType sent to unique object rep check" ); |
2795 | |
2796 | // Arrays are unique only if their element type is unique. |
2797 | if (Ty->isArrayType()) |
2798 | return hasUniqueObjectRepresentations(Ty: getBaseElementType(QT: Ty), |
2799 | CheckIfTriviallyCopyable); |
2800 | |
2801 | // (9.1) - T is trivially copyable... |
2802 | if (CheckIfTriviallyCopyable && !Ty.isTriviallyCopyableType(Context: *this)) |
2803 | return false; |
2804 | |
2805 | // All integrals and enums are unique. |
2806 | if (Ty->isIntegralOrEnumerationType()) { |
2807 | // Except _BitInt types that have padding bits. |
2808 | if (const auto *BIT = Ty->getAs<BitIntType>()) |
2809 | return getTypeSize(BIT) == BIT->getNumBits(); |
2810 | |
2811 | return true; |
2812 | } |
2813 | |
2814 | // All other pointers are unique. |
2815 | if (Ty->isPointerType()) |
2816 | return true; |
2817 | |
2818 | if (const auto *MPT = Ty->getAs<MemberPointerType>()) |
2819 | return !ABI->getMemberPointerInfo(MPT).HasPadding; |
2820 | |
2821 | if (Ty->isRecordType()) { |
2822 | const RecordDecl *Record = Ty->castAs<RecordType>()->getDecl(); |
2823 | |
2824 | if (Record->isInvalidDecl()) |
2825 | return false; |
2826 | |
2827 | if (Record->isUnion()) |
2828 | return unionHasUniqueObjectRepresentations(Context: *this, RD: Record, |
2829 | CheckIfTriviallyCopyable); |
2830 | |
2831 | std::optional<int64_t> StructSize = structHasUniqueObjectRepresentations( |
2832 | Context: *this, RD: Record, CheckIfTriviallyCopyable); |
2833 | |
2834 | return StructSize && *StructSize == static_cast<int64_t>(getTypeSize(T: Ty)); |
2835 | } |
2836 | |
2837 | // FIXME: More cases to handle here (list by rsmith): |
2838 | // vectors (careful about, eg, vector of 3 foo) |
2839 | // _Complex int and friends |
2840 | // _Atomic T |
2841 | // Obj-C block pointers |
2842 | // Obj-C object pointers |
2843 | // and perhaps OpenCL's various builtin types (pipe, sampler_t, event_t, |
2844 | // clk_event_t, queue_t, reserve_id_t) |
2845 | // There're also Obj-C class types and the Obj-C selector type, but I think it |
2846 | // makes sense for those to return false here. |
2847 | |
2848 | return false; |
2849 | } |
2850 | |
2851 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
2852 | unsigned count = 0; |
2853 | // Count ivars declared in class extension. |
2854 | for (const auto *Ext : OI->known_extensions()) |
2855 | count += Ext->ivar_size(); |
2856 | |
2857 | // Count ivar defined in this class's implementation. This |
2858 | // includes synthesized ivars. |
2859 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
2860 | count += ImplDecl->ivar_size(); |
2861 | |
2862 | return count; |
2863 | } |
2864 | |
2865 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
2866 | if (!E) |
2867 | return false; |
2868 | |
2869 | // nullptr_t is always treated as null. |
2870 | if (E->getType()->isNullPtrType()) return true; |
2871 | |
2872 | if (E->getType()->isAnyPointerType() && |
2873 | E->IgnoreParenCasts()->isNullPointerConstant(Ctx&: *this, |
2874 | NPC: Expr::NPC_ValueDependentIsNull)) |
2875 | return true; |
2876 | |
2877 | // Unfortunately, __null has type 'int'. |
2878 | if (isa<GNUNullExpr>(Val: E)) return true; |
2879 | |
2880 | return false; |
2881 | } |
2882 | |
2883 | /// Get the implementation of ObjCInterfaceDecl, or nullptr if none |
2884 | /// exists. |
2885 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
2886 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
2887 | I = ObjCImpls.find(D); |
2888 | if (I != ObjCImpls.end()) |
2889 | return cast<ObjCImplementationDecl>(Val: I->second); |
2890 | return nullptr; |
2891 | } |
2892 | |
2893 | /// Get the implementation of ObjCCategoryDecl, or nullptr if none |
2894 | /// exists. |
2895 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
2896 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
2897 | I = ObjCImpls.find(D); |
2898 | if (I != ObjCImpls.end()) |
2899 | return cast<ObjCCategoryImplDecl>(Val: I->second); |
2900 | return nullptr; |
2901 | } |
2902 | |
2903 | /// Set the implementation of ObjCInterfaceDecl. |
2904 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
2905 | ObjCImplementationDecl *ImplD) { |
2906 | assert(IFaceD && ImplD && "Passed null params" ); |
2907 | ObjCImpls[IFaceD] = ImplD; |
2908 | } |
2909 | |
2910 | /// Set the implementation of ObjCCategoryDecl. |
2911 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
2912 | ObjCCategoryImplDecl *ImplD) { |
2913 | assert(CatD && ImplD && "Passed null params" ); |
2914 | ObjCImpls[CatD] = ImplD; |
2915 | } |
2916 | |
2917 | const ObjCMethodDecl * |
2918 | ASTContext::getObjCMethodRedeclaration(const ObjCMethodDecl *MD) const { |
2919 | return ObjCMethodRedecls.lookup(Val: MD); |
2920 | } |
2921 | |
2922 | void ASTContext::setObjCMethodRedeclaration(const ObjCMethodDecl *MD, |
2923 | const ObjCMethodDecl *Redecl) { |
2924 | assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration" ); |
2925 | ObjCMethodRedecls[MD] = Redecl; |
2926 | } |
2927 | |
2928 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
2929 | const NamedDecl *ND) const { |
2930 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
2931 | return ID; |
2932 | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
2933 | return CD->getClassInterface(); |
2934 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
2935 | return IMD->getClassInterface(); |
2936 | |
2937 | return nullptr; |
2938 | } |
2939 | |
2940 | /// Get the copy initialization expression of VarDecl, or nullptr if |
2941 | /// none exists. |
2942 | BlockVarCopyInit ASTContext::getBlockVarCopyInit(const VarDecl *VD) const { |
2943 | assert(VD && "Passed null params" ); |
2944 | assert(VD->hasAttr<BlocksAttr>() && |
2945 | "getBlockVarCopyInits - not __block var" ); |
2946 | auto I = BlockVarCopyInits.find(Val: VD); |
2947 | if (I != BlockVarCopyInits.end()) |
2948 | return I->second; |
2949 | return {nullptr, false}; |
2950 | } |
2951 | |
2952 | /// Set the copy initialization expression of a block var decl. |
2953 | void ASTContext::setBlockVarCopyInit(const VarDecl*VD, Expr *CopyExpr, |
2954 | bool CanThrow) { |
2955 | assert(VD && CopyExpr && "Passed null params" ); |
2956 | assert(VD->hasAttr<BlocksAttr>() && |
2957 | "setBlockVarCopyInits - not __block var" ); |
2958 | BlockVarCopyInits[VD].setExprAndFlag(CopyExpr, CanThrow); |
2959 | } |
2960 | |
2961 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
2962 | unsigned DataSize) const { |
2963 | if (!DataSize) |
2964 | DataSize = TypeLoc::getFullDataSizeForType(Ty: T); |
2965 | else |
2966 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
2967 | "incorrect data size provided to CreateTypeSourceInfo!" ); |
2968 | |
2969 | auto *TInfo = |
2970 | (TypeSourceInfo*)BumpAlloc.Allocate(Size: sizeof(TypeSourceInfo) + DataSize, Alignment: 8); |
2971 | new (TInfo) TypeSourceInfo(T, DataSize); |
2972 | return TInfo; |
2973 | } |
2974 | |
2975 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
2976 | SourceLocation L) const { |
2977 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
2978 | DI->getTypeLoc().initialize(Context&: const_cast<ASTContext &>(*this), Loc: L); |
2979 | return DI; |
2980 | } |
2981 | |
2982 | const ASTRecordLayout & |
2983 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
2984 | return getObjCLayout(D, Impl: nullptr); |
2985 | } |
2986 | |
2987 | const ASTRecordLayout & |
2988 | ASTContext::getASTObjCImplementationLayout( |
2989 | const ObjCImplementationDecl *D) const { |
2990 | return getObjCLayout(D: D->getClassInterface(), Impl: D); |
2991 | } |
2992 | |
2993 | static auto getCanonicalTemplateArguments(const ASTContext &C, |
2994 | ArrayRef<TemplateArgument> Args, |
2995 | bool &AnyNonCanonArgs) { |
2996 | SmallVector<TemplateArgument, 16> CanonArgs(Args); |
2997 | for (auto &Arg : CanonArgs) { |
2998 | TemplateArgument OrigArg = Arg; |
2999 | Arg = C.getCanonicalTemplateArgument(Arg); |
3000 | AnyNonCanonArgs |= !Arg.structurallyEquals(Other: OrigArg); |
3001 | } |
3002 | return CanonArgs; |
3003 | } |
3004 | |
3005 | //===----------------------------------------------------------------------===// |
3006 | // Type creation/memoization methods |
3007 | //===----------------------------------------------------------------------===// |
3008 | |
3009 | QualType |
3010 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
3011 | unsigned fastQuals = quals.getFastQualifiers(); |
3012 | quals.removeFastQualifiers(); |
3013 | |
3014 | // Check if we've already instantiated this type. |
3015 | llvm::FoldingSetNodeID ID; |
3016 | ExtQuals::Profile(ID, BaseType: baseType, Quals: quals); |
3017 | void *insertPos = nullptr; |
3018 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos&: insertPos)) { |
3019 | assert(eq->getQualifiers() == quals); |
3020 | return QualType(eq, fastQuals); |
3021 | } |
3022 | |
3023 | // If the base type is not canonical, make the appropriate canonical type. |
3024 | QualType canon; |
3025 | if (!baseType->isCanonicalUnqualified()) { |
3026 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
3027 | canonSplit.Quals.addConsistentQualifiers(qs: quals); |
3028 | canon = getExtQualType(baseType: canonSplit.Ty, quals: canonSplit.Quals); |
3029 | |
3030 | // Re-find the insert position. |
3031 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos&: insertPos); |
3032 | } |
3033 | |
3034 | auto *eq = new (*this, alignof(ExtQuals)) ExtQuals(baseType, canon, quals); |
3035 | ExtQualNodes.InsertNode(N: eq, InsertPos: insertPos); |
3036 | return QualType(eq, fastQuals); |
3037 | } |
3038 | |
3039 | QualType ASTContext::getAddrSpaceQualType(QualType T, |
3040 | LangAS AddressSpace) const { |
3041 | QualType CanT = getCanonicalType(T); |
3042 | if (CanT.getAddressSpace() == AddressSpace) |
3043 | return T; |
3044 | |
3045 | // If we are composing extended qualifiers together, merge together |
3046 | // into one ExtQuals node. |
3047 | QualifierCollector Quals; |
3048 | const Type *TypeNode = Quals.strip(type: T); |
3049 | |
3050 | // If this type already has an address space specified, it cannot get |
3051 | // another one. |
3052 | assert(!Quals.hasAddressSpace() && |
3053 | "Type cannot be in multiple addr spaces!" ); |
3054 | Quals.addAddressSpace(space: AddressSpace); |
3055 | |
3056 | return getExtQualType(baseType: TypeNode, quals: Quals); |
3057 | } |
3058 | |
3059 | QualType ASTContext::removeAddrSpaceQualType(QualType T) const { |
3060 | // If the type is not qualified with an address space, just return it |
3061 | // immediately. |
3062 | if (!T.hasAddressSpace()) |
3063 | return T; |
3064 | |
3065 | // If we are composing extended qualifiers together, merge together |
3066 | // into one ExtQuals node. |
3067 | QualifierCollector Quals; |
3068 | const Type *TypeNode; |
3069 | |
3070 | while (T.hasAddressSpace()) { |
3071 | TypeNode = Quals.strip(type: T); |
3072 | |
3073 | // If the type no longer has an address space after stripping qualifiers, |
3074 | // jump out. |
3075 | if (!QualType(TypeNode, 0).hasAddressSpace()) |
3076 | break; |
3077 | |
3078 | // There might be sugar in the way. Strip it and try again. |
3079 | T = T.getSingleStepDesugaredType(Context: *this); |
3080 | } |
3081 | |
3082 | Quals.removeAddressSpace(); |
3083 | |
3084 | // Removal of the address space can mean there are no longer any |
3085 | // non-fast qualifiers, so creating an ExtQualType isn't possible (asserts) |
3086 | // or required. |
3087 | if (Quals.hasNonFastQualifiers()) |
3088 | return getExtQualType(baseType: TypeNode, quals: Quals); |
3089 | else |
3090 | return QualType(TypeNode, Quals.getFastQualifiers()); |
3091 | } |
3092 | |
3093 | QualType ASTContext::getObjCGCQualType(QualType T, |
3094 | Qualifiers::GC GCAttr) const { |
3095 | QualType CanT = getCanonicalType(T); |
3096 | if (CanT.getObjCGCAttr() == GCAttr) |
3097 | return T; |
3098 | |
3099 | if (const auto *ptr = T->getAs<PointerType>()) { |
3100 | QualType Pointee = ptr->getPointeeType(); |
3101 | if (Pointee->isAnyPointerType()) { |
3102 | QualType ResultType = getObjCGCQualType(T: Pointee, GCAttr); |
3103 | return getPointerType(T: ResultType); |
3104 | } |
3105 | } |
3106 | |
3107 | // If we are composing extended qualifiers together, merge together |
3108 | // into one ExtQuals node. |
3109 | QualifierCollector Quals; |
3110 | const Type *TypeNode = Quals.strip(type: T); |
3111 | |
3112 | // If this type already has an ObjCGC specified, it cannot get |
3113 | // another one. |
3114 | assert(!Quals.hasObjCGCAttr() && |
3115 | "Type cannot have multiple ObjCGCs!" ); |
3116 | Quals.addObjCGCAttr(type: GCAttr); |
3117 | |
3118 | return getExtQualType(baseType: TypeNode, quals: Quals); |
3119 | } |
3120 | |
3121 | QualType ASTContext::removePtrSizeAddrSpace(QualType T) const { |
3122 | if (const PointerType *Ptr = T->getAs<PointerType>()) { |
3123 | QualType Pointee = Ptr->getPointeeType(); |
3124 | if (isPtrSizeAddressSpace(AS: Pointee.getAddressSpace())) { |
3125 | return getPointerType(T: removeAddrSpaceQualType(T: Pointee)); |
3126 | } |
3127 | } |
3128 | return T; |
3129 | } |
3130 | |
3131 | QualType ASTContext::getCountAttributedType( |
3132 | QualType WrappedTy, Expr *CountExpr, bool CountInBytes, bool OrNull, |
3133 | ArrayRef<TypeCoupledDeclRefInfo> DependentDecls) const { |
3134 | assert(WrappedTy->isPointerType() || WrappedTy->isArrayType()); |
3135 | |
3136 | llvm::FoldingSetNodeID ID; |
3137 | CountAttributedType::Profile(ID, WrappedTy, CountExpr, CountInBytes, Nullable: OrNull); |
3138 | |
3139 | void *InsertPos = nullptr; |
3140 | CountAttributedType *CATy = |
3141 | CountAttributedTypes.FindNodeOrInsertPos(ID, InsertPos); |
3142 | if (CATy) |
3143 | return QualType(CATy, 0); |
3144 | |
3145 | QualType CanonTy = getCanonicalType(T: WrappedTy); |
3146 | size_t Size = CountAttributedType::totalSizeToAlloc<TypeCoupledDeclRefInfo>( |
3147 | DependentDecls.size()); |
3148 | CATy = (CountAttributedType *)Allocate(Size, Align: TypeAlignment); |
3149 | new (CATy) CountAttributedType(WrappedTy, CanonTy, CountExpr, CountInBytes, |
3150 | OrNull, DependentDecls); |
3151 | Types.push_back(CATy); |
3152 | CountAttributedTypes.InsertNode(N: CATy, InsertPos); |
3153 | |
3154 | return QualType(CATy, 0); |
3155 | } |
3156 | |
3157 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
3158 | FunctionType::ExtInfo Info) { |
3159 | if (T->getExtInfo() == Info) |
3160 | return T; |
3161 | |
3162 | QualType Result; |
3163 | if (const auto *FNPT = dyn_cast<FunctionNoProtoType>(Val: T)) { |
3164 | Result = getFunctionNoProtoType(FNPT->getReturnType(), Info); |
3165 | } else { |
3166 | const auto *FPT = cast<FunctionProtoType>(Val: T); |
3167 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
3168 | EPI.ExtInfo = Info; |
3169 | Result = getFunctionType(ResultTy: FPT->getReturnType(), Args: FPT->getParamTypes(), EPI); |
3170 | } |
3171 | |
3172 | return cast<FunctionType>(Val: Result.getTypePtr()); |
3173 | } |
3174 | |
3175 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
3176 | QualType ResultType) { |
3177 | FD = FD->getMostRecentDecl(); |
3178 | while (true) { |
3179 | const auto *FPT = FD->getType()->castAs<FunctionProtoType>(); |
3180 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
3181 | FD->setType(getFunctionType(ResultTy: ResultType, Args: FPT->getParamTypes(), EPI)); |
3182 | if (FunctionDecl *Next = FD->getPreviousDecl()) |
3183 | FD = Next; |
3184 | else |
3185 | break; |
3186 | } |
3187 | if (ASTMutationListener *L = getASTMutationListener()) |
3188 | L->DeducedReturnType(FD, ReturnType: ResultType); |
3189 | } |
3190 | |
3191 | /// Get a function type and produce the equivalent function type with the |
3192 | /// specified exception specification. Type sugar that can be present on a |
3193 | /// declaration of a function with an exception specification is permitted |
3194 | /// and preserved. Other type sugar (for instance, typedefs) is not. |
3195 | QualType ASTContext::getFunctionTypeWithExceptionSpec( |
3196 | QualType Orig, const FunctionProtoType::ExceptionSpecInfo &ESI) const { |
3197 | // Might have some parens. |
3198 | if (const auto *PT = dyn_cast<ParenType>(Val&: Orig)) |
3199 | return getParenType( |
3200 | NamedType: getFunctionTypeWithExceptionSpec(Orig: PT->getInnerType(), ESI)); |
3201 | |
3202 | // Might be wrapped in a macro qualified type. |
3203 | if (const auto *MQT = dyn_cast<MacroQualifiedType>(Val&: Orig)) |
3204 | return getMacroQualifiedType( |
3205 | UnderlyingTy: getFunctionTypeWithExceptionSpec(Orig: MQT->getUnderlyingType(), ESI), |
3206 | MacroII: MQT->getMacroIdentifier()); |
3207 | |
3208 | // Might have a calling-convention attribute. |
3209 | if (const auto *AT = dyn_cast<AttributedType>(Val&: Orig)) |
3210 | return getAttributedType( |
3211 | attrKind: AT->getAttrKind(), |
3212 | modifiedType: getFunctionTypeWithExceptionSpec(Orig: AT->getModifiedType(), ESI), |
3213 | equivalentType: getFunctionTypeWithExceptionSpec(Orig: AT->getEquivalentType(), ESI)); |
3214 | |
3215 | // Anything else must be a function type. Rebuild it with the new exception |
3216 | // specification. |
3217 | const auto *Proto = Orig->castAs<FunctionProtoType>(); |
3218 | return getFunctionType( |
3219 | ResultTy: Proto->getReturnType(), Args: Proto->getParamTypes(), |
3220 | EPI: Proto->getExtProtoInfo().withExceptionSpec(ESI)); |
3221 | } |
3222 | |
3223 | bool ASTContext::hasSameFunctionTypeIgnoringExceptionSpec(QualType T, |
3224 | QualType U) const { |
3225 | return hasSameType(T1: T, T2: U) || |
3226 | (getLangOpts().CPlusPlus17 && |
3227 | hasSameType(T1: getFunctionTypeWithExceptionSpec(Orig: T, ESI: EST_None), |
3228 | T2: getFunctionTypeWithExceptionSpec(Orig: U, ESI: EST_None))); |
3229 | } |
3230 | |
3231 | QualType ASTContext::getFunctionTypeWithoutPtrSizes(QualType T) { |
3232 | if (const auto *Proto = T->getAs<FunctionProtoType>()) { |
3233 | QualType RetTy = removePtrSizeAddrSpace(T: Proto->getReturnType()); |
3234 | SmallVector<QualType, 16> Args(Proto->param_types().size()); |
3235 | for (unsigned i = 0, n = Args.size(); i != n; ++i) |
3236 | Args[i] = removePtrSizeAddrSpace(T: Proto->param_types()[i]); |
3237 | return getFunctionType(ResultTy: RetTy, Args, EPI: Proto->getExtProtoInfo()); |
3238 | } |
3239 | |
3240 | if (const FunctionNoProtoType *Proto = T->getAs<FunctionNoProtoType>()) { |
3241 | QualType RetTy = removePtrSizeAddrSpace(T: Proto->getReturnType()); |
3242 | return getFunctionNoProtoType(RetTy, Proto->getExtInfo()); |
3243 | } |
3244 | |
3245 | return T; |
3246 | } |
3247 | |
3248 | bool ASTContext::hasSameFunctionTypeIgnoringPtrSizes(QualType T, QualType U) { |
3249 | return hasSameType(T1: T, T2: U) || |
3250 | hasSameType(T1: getFunctionTypeWithoutPtrSizes(T), |
3251 | T2: getFunctionTypeWithoutPtrSizes(T: U)); |
3252 | } |
3253 | |
3254 | void ASTContext::adjustExceptionSpec( |
3255 | FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI, |
3256 | bool AsWritten) { |
3257 | // Update the type. |
3258 | QualType Updated = |
3259 | getFunctionTypeWithExceptionSpec(Orig: FD->getType(), ESI); |
3260 | FD->setType(Updated); |
3261 | |
3262 | if (!AsWritten) |
3263 | return; |
3264 | |
3265 | // Update the type in the type source information too. |
3266 | if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) { |
3267 | // If the type and the type-as-written differ, we may need to update |
3268 | // the type-as-written too. |
3269 | if (TSInfo->getType() != FD->getType()) |
3270 | Updated = getFunctionTypeWithExceptionSpec(Orig: TSInfo->getType(), ESI); |
3271 | |
3272 | // FIXME: When we get proper type location information for exceptions, |
3273 | // we'll also have to rebuild the TypeSourceInfo. For now, we just patch |
3274 | // up the TypeSourceInfo; |
3275 | assert(TypeLoc::getFullDataSizeForType(Updated) == |
3276 | TypeLoc::getFullDataSizeForType(TSInfo->getType()) && |
3277 | "TypeLoc size mismatch from updating exception specification" ); |
3278 | TSInfo->overrideType(T: Updated); |
3279 | } |
3280 | } |
3281 | |
3282 | /// getComplexType - Return the uniqued reference to the type for a complex |
3283 | /// number with the specified element type. |
3284 | QualType ASTContext::getComplexType(QualType T) const { |
3285 | // Unique pointers, to guarantee there is only one pointer of a particular |
3286 | // structure. |
3287 | llvm::FoldingSetNodeID ID; |
3288 | ComplexType::Profile(ID, Element: T); |
3289 | |
3290 | void *InsertPos = nullptr; |
3291 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
3292 | return QualType(CT, 0); |
3293 | |
3294 | // If the pointee type isn't canonical, this won't be a canonical type either, |
3295 | // so fill in the canonical type field. |
3296 | QualType Canonical; |
3297 | if (!T.isCanonical()) { |
3298 | Canonical = getComplexType(T: getCanonicalType(T)); |
3299 | |
3300 | // Get the new insert position for the node we care about. |
3301 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
3302 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
3303 | } |
3304 | auto *New = new (*this, alignof(ComplexType)) ComplexType(T, Canonical); |
3305 | Types.push_back(New); |
3306 | ComplexTypes.InsertNode(N: New, InsertPos); |
3307 | return QualType(New, 0); |
3308 | } |
3309 | |
3310 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
3311 | /// the specified type. |
3312 | QualType ASTContext::getPointerType(QualType T) const { |
3313 | // Unique pointers, to guarantee there is only one pointer of a particular |
3314 | // structure. |
3315 | llvm::FoldingSetNodeID ID; |
3316 | PointerType::Profile(ID, Pointee: T); |
3317 | |
3318 | void *InsertPos = nullptr; |
3319 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
3320 | return QualType(PT, 0); |
3321 | |
3322 | // If the pointee type isn't canonical, this won't be a canonical type either, |
3323 | // so fill in the canonical type field. |
3324 | QualType Canonical; |
3325 | if (!T.isCanonical()) { |
3326 | Canonical = getPointerType(T: getCanonicalType(T)); |
3327 | |
3328 | // Get the new insert position for the node we care about. |
3329 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
3330 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
3331 | } |
3332 | auto *New = new (*this, alignof(PointerType)) PointerType(T, Canonical); |
3333 | Types.push_back(New); |
3334 | PointerTypes.InsertNode(N: New, InsertPos); |
3335 | return QualType(New, 0); |
3336 | } |
3337 | |
3338 | QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const { |
3339 | llvm::FoldingSetNodeID ID; |
3340 | AdjustedType::Profile(ID, Orig, New); |
3341 | void *InsertPos = nullptr; |
3342 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
3343 | if (AT) |
3344 | return QualType(AT, 0); |
3345 | |
3346 | QualType Canonical = getCanonicalType(T: New); |
3347 | |
3348 | // Get the new insert position for the node we care about. |
3349 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
3350 | assert(!AT && "Shouldn't be in the map!" ); |
3351 | |
3352 | AT = new (*this, alignof(AdjustedType)) |
3353 | AdjustedType(Type::Adjusted, Orig, New, Canonical); |
3354 | Types.push_back(AT); |
3355 | AdjustedTypes.InsertNode(N: AT, InsertPos); |
3356 | return QualType(AT, 0); |
3357 | } |
3358 | |
3359 | QualType ASTContext::getDecayedType(QualType Orig, QualType Decayed) const { |
3360 | llvm::FoldingSetNodeID ID; |
3361 | AdjustedType::Profile(ID, Orig, New: Decayed); |
3362 | void *InsertPos = nullptr; |
3363 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
3364 | if (AT) |
3365 | return QualType(AT, 0); |
3366 | |
3367 | QualType Canonical = getCanonicalType(T: Decayed); |
3368 | |
3369 | // Get the new insert position for the node we care about. |
3370 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
3371 | assert(!AT && "Shouldn't be in the map!" ); |
3372 | |
3373 | AT = new (*this, alignof(DecayedType)) DecayedType(Orig, Decayed, Canonical); |
3374 | Types.push_back(AT); |
3375 | AdjustedTypes.InsertNode(N: AT, InsertPos); |
3376 | return QualType(AT, 0); |
3377 | } |
3378 | |
3379 | QualType ASTContext::getDecayedType(QualType T) const { |
3380 | assert((T->isArrayType() || T->isFunctionType()) && "T does not decay" ); |
3381 | |
3382 | QualType Decayed; |
3383 | |
3384 | // C99 6.7.5.3p7: |
3385 | // A declaration of a parameter as "array of type" shall be |
3386 | // adjusted to "qualified pointer to type", where the type |
3387 | // qualifiers (if any) are those specified within the [ and ] of |
3388 | // the array type derivation. |
3389 | if (T->isArrayType()) |
3390 | Decayed = getArrayDecayedType(T); |
3391 | |
3392 | // C99 6.7.5.3p8: |
3393 | // A declaration of a parameter as "function returning type" |
3394 | // shall be adjusted to "pointer to function returning type", as |
3395 | // in 6.3.2.1. |
3396 | if (T->isFunctionType()) |
3397 | Decayed = getPointerType(T); |
3398 | |
3399 | return getDecayedType(Orig: T, Decayed); |
3400 | } |
3401 | |
3402 | QualType ASTContext::getArrayParameterType(QualType Ty) const { |
3403 | if (Ty->isArrayParameterType()) |
3404 | return Ty; |
3405 | assert(Ty->isConstantArrayType() && "Ty must be an array type." ); |
3406 | const auto *ATy = cast<ConstantArrayType>(Val&: Ty); |
3407 | llvm::FoldingSetNodeID ID; |
3408 | ATy->Profile(ID, *this, ATy->getElementType(), ATy->getZExtSize(), |
3409 | ATy->getSizeExpr(), ATy->getSizeModifier(), |
3410 | ATy->getIndexTypeQualifiers().getAsOpaqueValue()); |
3411 | void *InsertPos = nullptr; |
3412 | ArrayParameterType *AT = |
3413 | ArrayParameterTypes.FindNodeOrInsertPos(ID, InsertPos); |
3414 | if (AT) |
3415 | return QualType(AT, 0); |
3416 | |
3417 | QualType Canonical; |
3418 | if (!Ty.isCanonical()) { |
3419 | Canonical = getArrayParameterType(Ty: getCanonicalType(T: Ty)); |
3420 | |
3421 | // Get the new insert position for the node we care about. |
3422 | AT = ArrayParameterTypes.FindNodeOrInsertPos(ID, InsertPos); |
3423 | assert(!AT && "Shouldn't be in the map!" ); |
3424 | } |
3425 | |
3426 | AT = new (*this, alignof(ArrayParameterType)) |
3427 | ArrayParameterType(ATy, Canonical); |
3428 | Types.push_back(AT); |
3429 | ArrayParameterTypes.InsertNode(N: AT, InsertPos); |
3430 | return QualType(AT, 0); |
3431 | } |
3432 | |
3433 | /// getBlockPointerType - Return the uniqued reference to the type for |
3434 | /// a pointer to the specified block. |
3435 | QualType ASTContext::getBlockPointerType(QualType T) const { |
3436 | assert(T->isFunctionType() && "block of function types only" ); |
3437 | // Unique pointers, to guarantee there is only one block of a particular |
3438 | // structure. |
3439 | llvm::FoldingSetNodeID ID; |
3440 | BlockPointerType::Profile(ID, Pointee: T); |
3441 | |
3442 | void *InsertPos = nullptr; |
3443 | if (BlockPointerType *PT = |
3444 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
3445 | return QualType(PT, 0); |
3446 | |
3447 | // If the block pointee type isn't canonical, this won't be a canonical |
3448 | // type either so fill in the canonical type field. |
3449 | QualType Canonical; |
3450 | if (!T.isCanonical()) { |
3451 | Canonical = getBlockPointerType(T: getCanonicalType(T)); |
3452 | |
3453 | // Get the new insert position for the node we care about. |
3454 | BlockPointerType *NewIP = |
3455 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
3456 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
3457 | } |
3458 | auto *New = |
3459 | new (*this, alignof(BlockPointerType)) BlockPointerType(T, Canonical); |
3460 | Types.push_back(New); |
3461 | BlockPointerTypes.InsertNode(N: New, InsertPos); |
3462 | return QualType(New, 0); |
3463 | } |
3464 | |
3465 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
3466 | /// lvalue reference to the specified type. |
3467 | QualType |
3468 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
3469 | assert((!T->isPlaceholderType() || |
3470 | T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) && |
3471 | "Unresolved placeholder type" ); |
3472 | |
3473 | // Unique pointers, to guarantee there is only one pointer of a particular |
3474 | // structure. |
3475 | llvm::FoldingSetNodeID ID; |
3476 | ReferenceType::Profile(ID, Referencee: T, SpelledAsLValue); |
3477 | |
3478 | void *InsertPos = nullptr; |
3479 | if (LValueReferenceType *RT = |
3480 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
3481 | return QualType(RT, 0); |
3482 | |
3483 | const auto *InnerRef = T->getAs<ReferenceType>(); |
3484 | |
3485 | // If the referencee type isn't canonical, this won't be a canonical type |
3486 | // either, so fill in the canonical type field. |
3487 | QualType Canonical; |
3488 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
3489 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
3490 | Canonical = getLValueReferenceType(T: getCanonicalType(T: PointeeType)); |
3491 | |
3492 | // Get the new insert position for the node we care about. |
3493 | LValueReferenceType *NewIP = |
3494 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
3495 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
3496 | } |
3497 | |
3498 | auto *New = new (*this, alignof(LValueReferenceType)) |
3499 | LValueReferenceType(T, Canonical, SpelledAsLValue); |
3500 | Types.push_back(New); |
3501 | LValueReferenceTypes.InsertNode(N: New, InsertPos); |
3502 | |
3503 | return QualType(New, 0); |
3504 | } |
3505 | |
3506 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
3507 | /// rvalue reference to the specified type. |
3508 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
3509 | assert((!T->isPlaceholderType() || |
3510 | T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) && |
3511 | "Unresolved placeholder type" ); |
3512 | |
3513 | // Unique pointers, to guarantee there is only one pointer of a particular |
3514 | // structure. |
3515 | llvm::FoldingSetNodeID ID; |
3516 | ReferenceType::Profile(ID, Referencee: T, SpelledAsLValue: false); |
3517 | |
3518 | void *InsertPos = nullptr; |
3519 | if (RValueReferenceType *RT = |
3520 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
3521 | return QualType(RT, 0); |
3522 | |
3523 | const auto *InnerRef = T->getAs<ReferenceType>(); |
3524 | |
3525 | // If the referencee type isn't canonical, this won't be a canonical type |
3526 | // either, so fill in the canonical type field. |
3527 | QualType Canonical; |
3528 | if (InnerRef || !T.isCanonical()) { |
3529 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
3530 | Canonical = getRValueReferenceType(T: getCanonicalType(T: PointeeType)); |
3531 | |
3532 | // Get the new insert position for the node we care about. |
3533 | RValueReferenceType *NewIP = |
3534 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
3535 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
3536 | } |
3537 | |
3538 | auto *New = new (*this, alignof(RValueReferenceType)) |
3539 | RValueReferenceType(T, Canonical); |
3540 | Types.push_back(New); |
3541 | RValueReferenceTypes.InsertNode(N: New, InsertPos); |
3542 | return QualType(New, 0); |
3543 | } |
3544 | |
3545 | /// getMemberPointerType - Return the uniqued reference to the type for a |
3546 | /// member pointer to the specified type, in the specified class. |
3547 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
3548 | // Unique pointers, to guarantee there is only one pointer of a particular |
3549 | // structure. |
3550 | llvm::FoldingSetNodeID ID; |
3551 | MemberPointerType::Profile(ID, Pointee: T, Class: Cls); |
3552 | |
3553 | void *InsertPos = nullptr; |
3554 | if (MemberPointerType *PT = |
3555 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
3556 | return QualType(PT, 0); |
3557 | |
3558 | // If the pointee or class type isn't canonical, this won't be a canonical |
3559 | // type either, so fill in the canonical type field. |
3560 | QualType Canonical; |
3561 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
3562 | Canonical = getMemberPointerType(T: getCanonicalType(T),Cls: getCanonicalType(T: Cls)); |
3563 | |
3564 | // Get the new insert position for the node we care about. |
3565 | MemberPointerType *NewIP = |
3566 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
3567 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
3568 | } |
3569 | auto *New = new (*this, alignof(MemberPointerType)) |
3570 | MemberPointerType(T, Cls, Canonical); |
3571 | Types.push_back(New); |
3572 | MemberPointerTypes.InsertNode(N: New, InsertPos); |
3573 | return QualType(New, 0); |
3574 | } |
3575 | |
3576 | /// getConstantArrayType - Return the unique reference to the type for an |
3577 | /// array of the specified element type. |
3578 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
3579 | const llvm::APInt &ArySizeIn, |
3580 | const Expr *SizeExpr, |
3581 | ArraySizeModifier ASM, |
3582 | unsigned IndexTypeQuals) const { |
3583 | assert((EltTy->isDependentType() || |
3584 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
3585 | "Constant array of VLAs is illegal!" ); |
3586 | |
3587 | // We only need the size as part of the type if it's instantiation-dependent. |
3588 | if (SizeExpr && !SizeExpr->isInstantiationDependent()) |
3589 | SizeExpr = nullptr; |
3590 | |
3591 | // Convert the array size into a canonical width matching the pointer size for |
3592 | // the target. |
3593 | llvm::APInt ArySize(ArySizeIn); |
3594 | ArySize = ArySize.zextOrTrunc(width: Target->getMaxPointerWidth()); |
3595 | |
3596 | llvm::FoldingSetNodeID ID; |
3597 | ConstantArrayType::Profile(ID, Ctx: *this, ET: EltTy, ArraySize: ArySize.getZExtValue(), SizeExpr, |
3598 | SizeMod: ASM, TypeQuals: IndexTypeQuals); |
3599 | |
3600 | void *InsertPos = nullptr; |
3601 | if (ConstantArrayType *ATP = |
3602 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
3603 | return QualType(ATP, 0); |
3604 | |
3605 | // If the element type isn't canonical or has qualifiers, or the array bound |
3606 | // is instantiation-dependent, this won't be a canonical type either, so fill |
3607 | // in the canonical type field. |
3608 | QualType Canon; |
3609 | // FIXME: Check below should look for qualifiers behind sugar. |
3610 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers() || SizeExpr) { |
3611 | SplitQualType canonSplit = getCanonicalType(T: EltTy).split(); |
3612 | Canon = getConstantArrayType(EltTy: QualType(canonSplit.Ty, 0), ArySizeIn: ArySize, SizeExpr: nullptr, |
3613 | ASM, IndexTypeQuals); |
3614 | Canon = getQualifiedType(T: Canon, Qs: canonSplit.Quals); |
3615 | |
3616 | // Get the new insert position for the node we care about. |
3617 | ConstantArrayType *NewIP = |
3618 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
3619 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
3620 | } |
3621 | |
3622 | auto *New = ConstantArrayType::Create(Ctx: *this, ET: EltTy, Can: Canon, Sz: ArySize, SzExpr: SizeExpr, |
3623 | SzMod: ASM, Qual: IndexTypeQuals); |
3624 | ConstantArrayTypes.InsertNode(N: New, InsertPos); |
3625 | Types.push_back(New); |
3626 | return QualType(New, 0); |
3627 | } |
3628 | |
3629 | /// getVariableArrayDecayedType - Turns the given type, which may be |
3630 | /// variably-modified, into the corresponding type with all the known |
3631 | /// sizes replaced with [*]. |
3632 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
3633 | // Vastly most common case. |
3634 | if (!type->isVariablyModifiedType()) return type; |
3635 | |
3636 | QualType result; |
3637 | |
3638 | SplitQualType split = type.getSplitDesugaredType(); |
3639 | const Type *ty = split.Ty; |
3640 | switch (ty->getTypeClass()) { |
3641 | #define TYPE(Class, Base) |
3642 | #define ABSTRACT_TYPE(Class, Base) |
3643 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
3644 | #include "clang/AST/TypeNodes.inc" |
3645 | llvm_unreachable("didn't desugar past all non-canonical types?" ); |
3646 | |
3647 | // These types should never be variably-modified. |
3648 | case Type::Builtin: |
3649 | case Type::Complex: |
3650 | case Type::Vector: |
3651 | case Type::DependentVector: |
3652 | case Type::ExtVector: |
3653 | case Type::DependentSizedExtVector: |
3654 | case Type::ConstantMatrix: |
3655 | case Type::DependentSizedMatrix: |
3656 | case Type::DependentAddressSpace: |
3657 | case Type::ObjCObject: |
3658 | case Type::ObjCInterface: |
3659 | case Type::ObjCObjectPointer: |
3660 | case Type::Record: |
3661 | case Type::Enum: |
3662 | case Type::UnresolvedUsing: |
3663 | case Type::TypeOfExpr: |
3664 | case Type::TypeOf: |
3665 | case Type::Decltype: |
3666 | case Type::UnaryTransform: |
3667 | case Type::DependentName: |
3668 | case Type::InjectedClassName: |
3669 | case Type::TemplateSpecialization: |
3670 | case Type::DependentTemplateSpecialization: |
3671 | case Type::TemplateTypeParm: |
3672 | case Type::SubstTemplateTypeParmPack: |
3673 | case Type::Auto: |
3674 | case Type::DeducedTemplateSpecialization: |
3675 | case Type::PackExpansion: |
3676 | case Type::PackIndexing: |
3677 | case Type::BitInt: |
3678 | case Type::DependentBitInt: |
3679 | case Type::ArrayParameter: |
3680 | llvm_unreachable("type should never be variably-modified" ); |
3681 | |
3682 | // These types can be variably-modified but should never need to |
3683 | // further decay. |
3684 | case Type::FunctionNoProto: |
3685 | case Type::FunctionProto: |
3686 | case Type::BlockPointer: |
3687 | case Type::MemberPointer: |
3688 | case Type::Pipe: |
3689 | return type; |
3690 | |
3691 | // These types can be variably-modified. All these modifications |
3692 | // preserve structure except as noted by comments. |
3693 | // TODO: if we ever care about optimizing VLAs, there are no-op |
3694 | // optimizations available here. |
3695 | case Type::Pointer: |
3696 | result = getPointerType(getVariableArrayDecayedType( |
3697 | type: cast<PointerType>(ty)->getPointeeType())); |
3698 | break; |
3699 | |
3700 | case Type::LValueReference: { |
3701 | const auto *lv = cast<LValueReferenceType>(ty); |
3702 | result = getLValueReferenceType( |
3703 | T: getVariableArrayDecayedType(type: lv->getPointeeType()), |
3704 | SpelledAsLValue: lv->isSpelledAsLValue()); |
3705 | break; |
3706 | } |
3707 | |
3708 | case Type::RValueReference: { |
3709 | const auto *lv = cast<RValueReferenceType>(ty); |
3710 | result = getRValueReferenceType( |
3711 | T: getVariableArrayDecayedType(type: lv->getPointeeType())); |
3712 | break; |
3713 | } |
3714 | |
3715 | case Type::Atomic: { |
3716 | const auto *at = cast<AtomicType>(ty); |
3717 | result = getAtomicType(T: getVariableArrayDecayedType(type: at->getValueType())); |
3718 | break; |
3719 | } |
3720 | |
3721 | case Type::ConstantArray: { |
3722 | const auto *cat = cast<ConstantArrayType>(ty); |
3723 | result = getConstantArrayType( |
3724 | EltTy: getVariableArrayDecayedType(type: cat->getElementType()), |
3725 | ArySizeIn: cat->getSize(), |
3726 | SizeExpr: cat->getSizeExpr(), |
3727 | ASM: cat->getSizeModifier(), |
3728 | IndexTypeQuals: cat->getIndexTypeCVRQualifiers()); |
3729 | break; |
3730 | } |
3731 | |
3732 | case Type::DependentSizedArray: { |
3733 | const auto *dat = cast<DependentSizedArrayType>(ty); |
3734 | result = getDependentSizedArrayType( |
3735 | EltTy: getVariableArrayDecayedType(type: dat->getElementType()), |
3736 | NumElts: dat->getSizeExpr(), |
3737 | ASM: dat->getSizeModifier(), |
3738 | IndexTypeQuals: dat->getIndexTypeCVRQualifiers(), |
3739 | Brackets: dat->getBracketsRange()); |
3740 | break; |
3741 | } |
3742 | |
3743 | // Turn incomplete types into [*] types. |
3744 | case Type::IncompleteArray: { |
3745 | const auto *iat = cast<IncompleteArrayType>(ty); |
3746 | result = |
3747 | getVariableArrayType(EltTy: getVariableArrayDecayedType(type: iat->getElementType()), |
3748 | /*size*/ NumElts: nullptr, ASM: ArraySizeModifier::Normal, |
3749 | IndexTypeQuals: iat->getIndexTypeCVRQualifiers(), Brackets: SourceRange()); |
3750 | break; |
3751 | } |
3752 | |
3753 | // Turn VLA types into [*] types. |
3754 | case Type::VariableArray: { |
3755 | const auto *vat = cast<VariableArrayType>(ty); |
3756 | result = getVariableArrayType( |
3757 | EltTy: getVariableArrayDecayedType(type: vat->getElementType()), |
3758 | /*size*/ NumElts: nullptr, ASM: ArraySizeModifier::Star, |
3759 | IndexTypeQuals: vat->getIndexTypeCVRQualifiers(), Brackets: vat->getBracketsRange()); |
3760 | break; |
3761 | } |
3762 | } |
3763 | |
3764 | // Apply the top-level qualifiers from the original. |
3765 | return getQualifiedType(T: result, Qs: split.Quals); |
3766 | } |
3767 | |
3768 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
3769 | /// variable array of the specified element type. |
3770 | QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, |
3771 | ArraySizeModifier ASM, |
3772 | unsigned IndexTypeQuals, |
3773 | SourceRange Brackets) const { |
3774 | // Since we don't unique expressions, it isn't possible to unique VLA's |
3775 | // that have an expression provided for their size. |
3776 | QualType Canon; |
3777 | |
3778 | // Be sure to pull qualifiers off the element type. |
3779 | // FIXME: Check below should look for qualifiers behind sugar. |
3780 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
3781 | SplitQualType canonSplit = getCanonicalType(T: EltTy).split(); |
3782 | Canon = getVariableArrayType(EltTy: QualType(canonSplit.Ty, 0), NumElts, ASM, |
3783 | IndexTypeQuals, Brackets); |
3784 | Canon = getQualifiedType(T: Canon, Qs: canonSplit.Quals); |
3785 | } |
3786 | |
3787 | auto *New = new (*this, alignof(VariableArrayType)) |
3788 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
3789 | |
3790 | VariableArrayTypes.push_back(x: New); |
3791 | Types.push_back(New); |
3792 | return QualType(New, 0); |
3793 | } |
3794 | |
3795 | /// getDependentSizedArrayType - Returns a non-unique reference to |
3796 | /// the type for a dependently-sized array of the specified element |
3797 | /// type. |
3798 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
3799 | Expr *numElements, |
3800 | ArraySizeModifier ASM, |
3801 | unsigned elementTypeQuals, |
3802 | SourceRange brackets) const { |
3803 | assert((!numElements || numElements->isTypeDependent() || |
3804 | numElements->isValueDependent()) && |
3805 | "Size must be type- or value-dependent!" ); |
3806 | |
3807 | // Dependently-sized array types that do not have a specified number |
3808 | // of elements will have their sizes deduced from a dependent |
3809 | // initializer. We do no canonicalization here at all, which is okay |
3810 | // because they can't be used in most locations. |
3811 | if (!numElements) { |
3812 | auto *newType = new (*this, alignof(DependentSizedArrayType)) |
3813 | DependentSizedArrayType(elementType, QualType(), numElements, ASM, |
3814 | elementTypeQuals, brackets); |
3815 | Types.push_back(newType); |
3816 | return QualType(newType, 0); |
3817 | } |
3818 | |
3819 | // Otherwise, we actually build a new type every time, but we |
3820 | // also build a canonical type. |
3821 | |
3822 | SplitQualType canonElementType = getCanonicalType(T: elementType).split(); |
3823 | |
3824 | void *insertPos = nullptr; |
3825 | llvm::FoldingSetNodeID ID; |
3826 | DependentSizedArrayType::Profile(ID, Context: *this, |
3827 | ET: QualType(canonElementType.Ty, 0), |
3828 | SizeMod: ASM, TypeQuals: elementTypeQuals, E: numElements); |
3829 | |
3830 | // Look for an existing type with these properties. |
3831 | DependentSizedArrayType *canonTy = |
3832 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos&: insertPos); |
3833 | |
3834 | // If we don't have one, build one. |
3835 | if (!canonTy) { |
3836 | canonTy = new (*this, alignof(DependentSizedArrayType)) |
3837 | DependentSizedArrayType(QualType(canonElementType.Ty, 0), QualType(), |
3838 | numElements, ASM, elementTypeQuals, brackets); |
3839 | DependentSizedArrayTypes.InsertNode(N: canonTy, InsertPos: insertPos); |
3840 | Types.push_back(canonTy); |
3841 | } |
3842 | |
3843 | // Apply qualifiers from the element type to the array. |
3844 | QualType canon = getQualifiedType(T: QualType(canonTy,0), |
3845 | Qs: canonElementType.Quals); |
3846 | |
3847 | // If we didn't need extra canonicalization for the element type or the size |
3848 | // expression, then just use that as our result. |
3849 | if (QualType(canonElementType.Ty, 0) == elementType && |
3850 | canonTy->getSizeExpr() == numElements) |
3851 | return canon; |
3852 | |
3853 | // Otherwise, we need to build a type which follows the spelling |
3854 | // of the element type. |
3855 | auto *sugaredType = new (*this, alignof(DependentSizedArrayType)) |
3856 | DependentSizedArrayType(elementType, canon, numElements, ASM, |
3857 | elementTypeQuals, brackets); |
3858 | Types.push_back(Elt: sugaredType); |
3859 | return QualType(sugaredType, 0); |
3860 | } |
3861 | |
3862 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
3863 | ArraySizeModifier ASM, |
3864 | unsigned elementTypeQuals) const { |
3865 | llvm::FoldingSetNodeID ID; |
3866 | IncompleteArrayType::Profile(ID, ET: elementType, SizeMod: ASM, TypeQuals: elementTypeQuals); |
3867 | |
3868 | void *insertPos = nullptr; |
3869 | if (IncompleteArrayType *iat = |
3870 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos&: insertPos)) |
3871 | return QualType(iat, 0); |
3872 | |
3873 | // If the element type isn't canonical, this won't be a canonical type |
3874 | // either, so fill in the canonical type field. We also have to pull |
3875 | // qualifiers off the element type. |
3876 | QualType canon; |
3877 | |
3878 | // FIXME: Check below should look for qualifiers behind sugar. |
3879 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
3880 | SplitQualType canonSplit = getCanonicalType(T: elementType).split(); |
3881 | canon = getIncompleteArrayType(elementType: QualType(canonSplit.Ty, 0), |
3882 | ASM, elementTypeQuals); |
3883 | canon = getQualifiedType(T: canon, Qs: canonSplit.Quals); |
3884 | |
3885 | // Get the new insert position for the node we care about. |
3886 | IncompleteArrayType *existing = |
3887 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos&: insertPos); |
3888 | assert(!existing && "Shouldn't be in the map!" ); (void) existing; |
3889 | } |
3890 | |
3891 | auto *newType = new (*this, alignof(IncompleteArrayType)) |
3892 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
3893 | |
3894 | IncompleteArrayTypes.InsertNode(N: newType, InsertPos: insertPos); |
3895 | Types.push_back(newType); |
3896 | return QualType(newType, 0); |
3897 | } |
3898 | |
3899 | ASTContext::BuiltinVectorTypeInfo |
3900 | ASTContext::getBuiltinVectorTypeInfo(const BuiltinType *Ty) const { |
3901 | #define SVE_INT_ELTTY(BITS, ELTS, SIGNED, NUMVECTORS) \ |
3902 | {getIntTypeForBitwidth(BITS, SIGNED), llvm::ElementCount::getScalable(ELTS), \ |
3903 | NUMVECTORS}; |
3904 | |
3905 | #define SVE_ELTTY(ELTTY, ELTS, NUMVECTORS) \ |
3906 | {ELTTY, llvm::ElementCount::getScalable(ELTS), NUMVECTORS}; |
3907 | |
3908 | switch (Ty->getKind()) { |
3909 | default: |
3910 | llvm_unreachable("Unsupported builtin vector type" ); |
3911 | case BuiltinType::SveInt8: |
3912 | return SVE_INT_ELTTY(8, 16, true, 1); |
3913 | case BuiltinType::SveUint8: |
3914 | return SVE_INT_ELTTY(8, 16, false, 1); |
3915 | case BuiltinType::SveInt8x2: |
3916 | return SVE_INT_ELTTY(8, 16, true, 2); |
3917 | case BuiltinType::SveUint8x2: |
3918 | return SVE_INT_ELTTY(8, 16, false, 2); |
3919 | case BuiltinType::SveInt8x3: |
3920 | return SVE_INT_ELTTY(8, 16, true, 3); |
3921 | case BuiltinType::SveUint8x3: |
3922 | return SVE_INT_ELTTY(8, 16, false, 3); |
3923 | case BuiltinType::SveInt8x4: |
3924 | return SVE_INT_ELTTY(8, 16, true, 4); |
3925 | case BuiltinType::SveUint8x4: |
3926 | return SVE_INT_ELTTY(8, 16, false, 4); |
3927 | case BuiltinType::SveInt16: |
3928 | return SVE_INT_ELTTY(16, 8, true, 1); |
3929 | case BuiltinType::SveUint16: |
3930 | return SVE_INT_ELTTY(16, 8, false, 1); |
3931 | case BuiltinType::SveInt16x2: |
3932 | return SVE_INT_ELTTY(16, 8, true, 2); |
3933 | case BuiltinType::SveUint16x2: |
3934 | return SVE_INT_ELTTY(16, 8, false, 2); |
3935 | case BuiltinType::SveInt16x3: |
3936 | return SVE_INT_ELTTY(16, 8, true, 3); |
3937 | case BuiltinType::SveUint16x3: |
3938 | return SVE_INT_ELTTY(16, 8, false, 3); |
3939 | case BuiltinType::SveInt16x4: |
3940 | return SVE_INT_ELTTY(16, 8, true, 4); |
3941 | case BuiltinType::SveUint16x4: |
3942 | return SVE_INT_ELTTY(16, 8, false, 4); |
3943 | case BuiltinType::SveInt32: |
3944 | return SVE_INT_ELTTY(32, 4, true, 1); |
3945 | case BuiltinType::SveUint32: |
3946 | return SVE_INT_ELTTY(32, 4, false, 1); |
3947 | case BuiltinType::SveInt32x2: |
3948 | return SVE_INT_ELTTY(32, 4, true, 2); |
3949 | case BuiltinType::SveUint32x2: |
3950 | return SVE_INT_ELTTY(32, 4, false, 2); |
3951 | case BuiltinType::SveInt32x3: |
3952 | return SVE_INT_ELTTY(32, 4, true, 3); |
3953 | case BuiltinType::SveUint32x3: |
3954 | return SVE_INT_ELTTY(32, 4, false, 3); |
3955 | case BuiltinType::SveInt32x4: |
3956 | return SVE_INT_ELTTY(32, 4, true, 4); |
3957 | case BuiltinType::SveUint32x4: |
3958 | return SVE_INT_ELTTY(32, 4, false, 4); |
3959 | case BuiltinType::SveInt64: |
3960 | return SVE_INT_ELTTY(64, 2, true, 1); |
3961 | case BuiltinType::SveUint64: |
3962 | return SVE_INT_ELTTY(64, 2, false, 1); |
3963 | case BuiltinType::SveInt64x2: |
3964 | return SVE_INT_ELTTY(64, 2, true, 2); |
3965 | case BuiltinType::SveUint64x2: |
3966 | return SVE_INT_ELTTY(64, 2, false, 2); |
3967 | case BuiltinType::SveInt64x3: |
3968 | return SVE_INT_ELTTY(64, 2, true, 3); |
3969 | case BuiltinType::SveUint64x3: |
3970 | return SVE_INT_ELTTY(64, 2, false, 3); |
3971 | case BuiltinType::SveInt64x4: |
3972 | return SVE_INT_ELTTY(64, 2, true, 4); |
3973 | case BuiltinType::SveUint64x4: |
3974 | return SVE_INT_ELTTY(64, 2, false, 4); |
3975 | case BuiltinType::SveBool: |
3976 | return SVE_ELTTY(BoolTy, 16, 1); |
3977 | case BuiltinType::SveBoolx2: |
3978 | return SVE_ELTTY(BoolTy, 16, 2); |
3979 | case BuiltinType::SveBoolx4: |
3980 | return SVE_ELTTY(BoolTy, 16, 4); |
3981 | case BuiltinType::SveFloat16: |
3982 | return SVE_ELTTY(HalfTy, 8, 1); |
3983 | case BuiltinType::SveFloat16x2: |
3984 | return SVE_ELTTY(HalfTy, 8, 2); |
3985 | case BuiltinType::SveFloat16x3: |
3986 | return SVE_ELTTY(HalfTy, 8, 3); |
3987 | case BuiltinType::SveFloat16x4: |
3988 | return SVE_ELTTY(HalfTy, 8, 4); |
3989 | case BuiltinType::SveFloat32: |
3990 | return SVE_ELTTY(FloatTy, 4, 1); |
3991 | case BuiltinType::SveFloat32x2: |
3992 | return SVE_ELTTY(FloatTy, 4, 2); |
3993 | case BuiltinType::SveFloat32x3: |
3994 | return SVE_ELTTY(FloatTy, 4, 3); |
3995 | case BuiltinType::SveFloat32x4: |
3996 | return SVE_ELTTY(FloatTy, 4, 4); |
3997 | case BuiltinType::SveFloat64: |
3998 | return SVE_ELTTY(DoubleTy, 2, 1); |
3999 | case BuiltinType::SveFloat64x2: |
4000 | return SVE_ELTTY(DoubleTy, 2, 2); |
4001 | case BuiltinType::SveFloat64x3: |
4002 | return SVE_ELTTY(DoubleTy, 2, 3); |
4003 | case BuiltinType::SveFloat64x4: |
4004 | return SVE_ELTTY(DoubleTy, 2, 4); |
4005 | case BuiltinType::SveBFloat16: |
4006 | return SVE_ELTTY(BFloat16Ty, 8, 1); |
4007 | case BuiltinType::SveBFloat16x2: |
4008 | return SVE_ELTTY(BFloat16Ty, 8, 2); |
4009 | case BuiltinType::SveBFloat16x3: |
4010 | return SVE_ELTTY(BFloat16Ty, 8, 3); |
4011 | case BuiltinType::SveBFloat16x4: |
4012 | return SVE_ELTTY(BFloat16Ty, 8, 4); |
4013 | #define RVV_VECTOR_TYPE_INT(Name, Id, SingletonId, NumEls, ElBits, NF, \ |
4014 | IsSigned) \ |
4015 | case BuiltinType::Id: \ |
4016 | return {getIntTypeForBitwidth(ElBits, IsSigned), \ |
4017 | llvm::ElementCount::getScalable(NumEls), NF}; |
4018 | #define RVV_VECTOR_TYPE_FLOAT(Name, Id, SingletonId, NumEls, ElBits, NF) \ |
4019 | case BuiltinType::Id: \ |
4020 | return {ElBits == 16 ? Float16Ty : (ElBits == 32 ? FloatTy : DoubleTy), \ |
4021 | llvm::ElementCount::getScalable(NumEls), NF}; |
4022 | #define RVV_VECTOR_TYPE_BFLOAT(Name, Id, SingletonId, NumEls, ElBits, NF) \ |
4023 | case BuiltinType::Id: \ |
4024 | return {BFloat16Ty, llvm::ElementCount::getScalable(NumEls), NF}; |
4025 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, NumEls) \ |
4026 | case BuiltinType::Id: \ |
4027 | return {BoolTy, llvm::ElementCount::getScalable(NumEls), 1}; |
4028 | #include "clang/Basic/RISCVVTypes.def" |
4029 | } |
4030 | } |
4031 | |
4032 | /// getExternrefType - Return a WebAssembly externref type, which represents an |
4033 | /// opaque reference to a host value. |
4034 | QualType ASTContext::getWebAssemblyExternrefType() const { |
4035 | if (Target->getTriple().isWasm() && Target->hasFeature(Feature: "reference-types" )) { |
4036 | #define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS) \ |
4037 | if (BuiltinType::Id == BuiltinType::WasmExternRef) \ |
4038 | return SingletonId; |
4039 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
4040 | } |
4041 | llvm_unreachable( |
4042 | "shouldn't try to generate type externref outside WebAssembly target" ); |
4043 | } |
4044 | |
4045 | /// getScalableVectorType - Return the unique reference to a scalable vector |
4046 | /// type of the specified element type and size. VectorType must be a built-in |
4047 | /// type. |
4048 | QualType ASTContext::getScalableVectorType(QualType EltTy, unsigned NumElts, |
4049 | unsigned NumFields) const { |
4050 | if (Target->hasAArch64SVETypes()) { |
4051 | uint64_t EltTySize = getTypeSize(T: EltTy); |
4052 | #define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId, NumEls, ElBits, \ |
4053 | IsSigned, IsFP, IsBF) \ |
4054 | if (!EltTy->isBooleanType() && \ |
4055 | ((EltTy->hasIntegerRepresentation() && \ |
4056 | EltTy->hasSignedIntegerRepresentation() == IsSigned) || \ |
4057 | (EltTy->hasFloatingRepresentation() && !EltTy->isBFloat16Type() && \ |
4058 | IsFP && !IsBF) || \ |
4059 | (EltTy->hasFloatingRepresentation() && EltTy->isBFloat16Type() && \ |
4060 | IsBF && !IsFP)) && \ |
4061 | EltTySize == ElBits && NumElts == NumEls) { \ |
4062 | return SingletonId; \ |
4063 | } |
4064 | #define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId, NumEls) \ |
4065 | if (EltTy->isBooleanType() && NumElts == NumEls) \ |
4066 | return SingletonId; |
4067 | #define SVE_OPAQUE_TYPE(Name, MangledName, Id, SingleTonId) |
4068 | #include "clang/Basic/AArch64SVEACLETypes.def" |
4069 | } else if (Target->hasRISCVVTypes()) { |
4070 | uint64_t EltTySize = getTypeSize(T: EltTy); |
4071 | #define RVV_VECTOR_TYPE(Name, Id, SingletonId, NumEls, ElBits, NF, IsSigned, \ |
4072 | IsFP, IsBF) \ |
4073 | if (!EltTy->isBooleanType() && \ |
4074 | ((EltTy->hasIntegerRepresentation() && \ |
4075 | EltTy->hasSignedIntegerRepresentation() == IsSigned) || \ |
4076 | (EltTy->hasFloatingRepresentation() && !EltTy->isBFloat16Type() && \ |
4077 | IsFP && !IsBF) || \ |
4078 | (EltTy->hasFloatingRepresentation() && EltTy->isBFloat16Type() && \ |
4079 | IsBF && !IsFP)) && \ |
4080 | EltTySize == ElBits && NumElts == NumEls && NumFields == NF) \ |
4081 | return SingletonId; |
4082 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, NumEls) \ |
4083 | if (EltTy->isBooleanType() && NumElts == NumEls) \ |
4084 | return SingletonId; |
4085 | #include "clang/Basic/RISCVVTypes.def" |
4086 | } |
4087 | return QualType(); |
4088 | } |
4089 | |
4090 | /// getVectorType - Return the unique reference to a vector type of |
4091 | /// the specified element type and size. VectorType must be a built-in type. |
4092 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
4093 | VectorKind VecKind) const { |
4094 | assert(vecType->isBuiltinType() || |
4095 | (vecType->isBitIntType() && |
4096 | // Only support _BitInt elements with byte-sized power of 2 NumBits. |
4097 | llvm::isPowerOf2_32(vecType->castAs<BitIntType>()->getNumBits()) && |
4098 | vecType->castAs<BitIntType>()->getNumBits() >= 8)); |
4099 | |
4100 | // Check if we've already instantiated a vector of this type. |
4101 | llvm::FoldingSetNodeID ID; |
4102 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
4103 | |
4104 | void *InsertPos = nullptr; |
4105 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4106 | return QualType(VTP, 0); |
4107 | |
4108 | // If the element type isn't canonical, this won't be a canonical type either, |
4109 | // so fill in the canonical type field. |
4110 | QualType Canonical; |
4111 | if (!vecType.isCanonical()) { |
4112 | Canonical = getVectorType(vecType: getCanonicalType(T: vecType), NumElts, VecKind); |
4113 | |
4114 | // Get the new insert position for the node we care about. |
4115 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4116 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
4117 | } |
4118 | auto *New = new (*this, alignof(VectorType)) |
4119 | VectorType(vecType, NumElts, Canonical, VecKind); |
4120 | VectorTypes.InsertNode(N: New, InsertPos); |
4121 | Types.push_back(New); |
4122 | return QualType(New, 0); |
4123 | } |
4124 | |
4125 | QualType ASTContext::getDependentVectorType(QualType VecType, Expr *SizeExpr, |
4126 | SourceLocation AttrLoc, |
4127 | VectorKind VecKind) const { |
4128 | llvm::FoldingSetNodeID ID; |
4129 | DependentVectorType::Profile(ID, Context: *this, ElementType: getCanonicalType(T: VecType), SizeExpr, |
4130 | VecKind); |
4131 | void *InsertPos = nullptr; |
4132 | DependentVectorType *Canon = |
4133 | DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4134 | DependentVectorType *New; |
4135 | |
4136 | if (Canon) { |
4137 | New = new (*this, alignof(DependentVectorType)) DependentVectorType( |
4138 | VecType, QualType(Canon, 0), SizeExpr, AttrLoc, VecKind); |
4139 | } else { |
4140 | QualType CanonVecTy = getCanonicalType(T: VecType); |
4141 | if (CanonVecTy == VecType) { |
4142 | New = new (*this, alignof(DependentVectorType)) |
4143 | DependentVectorType(VecType, QualType(), SizeExpr, AttrLoc, VecKind); |
4144 | |
4145 | DependentVectorType *CanonCheck = |
4146 | DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4147 | assert(!CanonCheck && |
4148 | "Dependent-sized vector_size canonical type broken" ); |
4149 | (void)CanonCheck; |
4150 | DependentVectorTypes.InsertNode(N: New, InsertPos); |
4151 | } else { |
4152 | QualType CanonTy = getDependentVectorType(VecType: CanonVecTy, SizeExpr, |
4153 | AttrLoc: SourceLocation(), VecKind); |
4154 | New = new (*this, alignof(DependentVectorType)) |
4155 | DependentVectorType(VecType, CanonTy, SizeExpr, AttrLoc, VecKind); |
4156 | } |
4157 | } |
4158 | |
4159 | Types.push_back(New); |
4160 | return QualType(New, 0); |
4161 | } |
4162 | |
4163 | /// getExtVectorType - Return the unique reference to an extended vector type of |
4164 | /// the specified element type and size. VectorType must be a built-in type. |
4165 | QualType ASTContext::getExtVectorType(QualType vecType, |
4166 | unsigned NumElts) const { |
4167 | assert(vecType->isBuiltinType() || vecType->isDependentType() || |
4168 | (vecType->isBitIntType() && |
4169 | // Only support _BitInt elements with byte-sized power of 2 NumBits. |
4170 | llvm::isPowerOf2_32(vecType->castAs<BitIntType>()->getNumBits()) && |
4171 | vecType->castAs<BitIntType>()->getNumBits() >= 8)); |
4172 | |
4173 | // Check if we've already instantiated a vector of this type. |
4174 | llvm::FoldingSetNodeID ID; |
4175 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
4176 | VectorKind::Generic); |
4177 | void *InsertPos = nullptr; |
4178 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4179 | return QualType(VTP, 0); |
4180 | |
4181 | // If the element type isn't canonical, this won't be a canonical type either, |
4182 | // so fill in the canonical type field. |
4183 | QualType Canonical; |
4184 | if (!vecType.isCanonical()) { |
4185 | Canonical = getExtVectorType(vecType: getCanonicalType(T: vecType), NumElts); |
4186 | |
4187 | // Get the new insert position for the node we care about. |
4188 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4189 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
4190 | } |
4191 | auto *New = new (*this, alignof(ExtVectorType)) |
4192 | ExtVectorType(vecType, NumElts, Canonical); |
4193 | VectorTypes.InsertNode(New, InsertPos); |
4194 | Types.push_back(New); |
4195 | return QualType(New, 0); |
4196 | } |
4197 | |
4198 | QualType |
4199 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
4200 | Expr *SizeExpr, |
4201 | SourceLocation AttrLoc) const { |
4202 | llvm::FoldingSetNodeID ID; |
4203 | DependentSizedExtVectorType::Profile(ID, Context: *this, ElementType: getCanonicalType(T: vecType), |
4204 | SizeExpr); |
4205 | |
4206 | void *InsertPos = nullptr; |
4207 | DependentSizedExtVectorType *Canon |
4208 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4209 | DependentSizedExtVectorType *New; |
4210 | if (Canon) { |
4211 | // We already have a canonical version of this array type; use it as |
4212 | // the canonical type for a newly-built type. |
4213 | New = new (*this, alignof(DependentSizedExtVectorType)) |
4214 | DependentSizedExtVectorType(vecType, QualType(Canon, 0), SizeExpr, |
4215 | AttrLoc); |
4216 | } else { |
4217 | QualType CanonVecTy = getCanonicalType(T: vecType); |
4218 | if (CanonVecTy == vecType) { |
4219 | New = new (*this, alignof(DependentSizedExtVectorType)) |
4220 | DependentSizedExtVectorType(vecType, QualType(), SizeExpr, AttrLoc); |
4221 | |
4222 | DependentSizedExtVectorType *CanonCheck |
4223 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
4224 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken" ); |
4225 | (void)CanonCheck; |
4226 | DependentSizedExtVectorTypes.InsertNode(N: New, InsertPos); |
4227 | } else { |
4228 | QualType CanonExtTy = getDependentSizedExtVectorType(vecType: CanonVecTy, SizeExpr, |
4229 | AttrLoc: SourceLocation()); |
4230 | New = new (*this, alignof(DependentSizedExtVectorType)) |
4231 | DependentSizedExtVectorType(vecType, CanonExtTy, SizeExpr, AttrLoc); |
4232 | } |
4233 | } |
4234 | |
4235 | Types.push_back(New); |
4236 | return QualType(New, 0); |
4237 | } |
4238 | |
4239 | QualType ASTContext::getConstantMatrixType(QualType ElementTy, unsigned NumRows, |
4240 | unsigned NumColumns) const { |
4241 | llvm::FoldingSetNodeID ID; |
4242 | ConstantMatrixType::Profile(ID, ElementTy, NumRows, NumColumns, |
4243 | Type::ConstantMatrix); |
4244 | |
4245 | assert(MatrixType::isValidElementType(ElementTy) && |
4246 | "need a valid element type" ); |
4247 | assert(ConstantMatrixType::isDimensionValid(NumRows) && |
4248 | ConstantMatrixType::isDimensionValid(NumColumns) && |
4249 | "need valid matrix dimensions" ); |
4250 | void *InsertPos = nullptr; |
4251 | if (ConstantMatrixType *MTP = MatrixTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4252 | return QualType(MTP, 0); |
4253 | |
4254 | QualType Canonical; |
4255 | if (!ElementTy.isCanonical()) { |
4256 | Canonical = |
4257 | getConstantMatrixType(ElementTy: getCanonicalType(T: ElementTy), NumRows, NumColumns); |
4258 | |
4259 | ConstantMatrixType *NewIP = MatrixTypes.FindNodeOrInsertPos(ID, InsertPos); |
4260 | assert(!NewIP && "Matrix type shouldn't already exist in the map" ); |
4261 | (void)NewIP; |
4262 | } |
4263 | |
4264 | auto *New = new (*this, alignof(ConstantMatrixType)) |
4265 | ConstantMatrixType(ElementTy, NumRows, NumColumns, Canonical); |
4266 | MatrixTypes.InsertNode(N: New, InsertPos); |
4267 | Types.push_back(New); |
4268 | return QualType(New, 0); |
4269 | } |
4270 | |
4271 | QualType ASTContext::getDependentSizedMatrixType(QualType ElementTy, |
4272 | Expr *RowExpr, |
4273 | Expr *ColumnExpr, |
4274 | SourceLocation AttrLoc) const { |
4275 | QualType CanonElementTy = getCanonicalType(T: ElementTy); |
4276 | llvm::FoldingSetNodeID ID; |
4277 | DependentSizedMatrixType::Profile(ID, Context: *this, ElementType: CanonElementTy, RowExpr, |
4278 | ColumnExpr); |
4279 | |
4280 | void *InsertPos = nullptr; |
4281 | DependentSizedMatrixType *Canon = |
4282 | DependentSizedMatrixTypes.FindNodeOrInsertPos(ID, InsertPos); |
4283 | |
4284 | if (!Canon) { |
4285 | Canon = new (*this, alignof(DependentSizedMatrixType)) |
4286 | DependentSizedMatrixType(CanonElementTy, QualType(), RowExpr, |
4287 | ColumnExpr, AttrLoc); |
4288 | #ifndef NDEBUG |
4289 | DependentSizedMatrixType *CanonCheck = |
4290 | DependentSizedMatrixTypes.FindNodeOrInsertPos(ID, InsertPos); |
4291 | assert(!CanonCheck && "Dependent-sized matrix canonical type broken" ); |
4292 | #endif |
4293 | DependentSizedMatrixTypes.InsertNode(N: Canon, InsertPos); |
4294 | Types.push_back(Canon); |
4295 | } |
4296 | |
4297 | // Already have a canonical version of the matrix type |
4298 | // |
4299 | // If it exactly matches the requested type, use it directly. |
4300 | if (Canon->getElementType() == ElementTy && Canon->getRowExpr() == RowExpr && |
4301 | Canon->getRowExpr() == ColumnExpr) |
4302 | return QualType(Canon, 0); |
4303 | |
4304 | // Use Canon as the canonical type for newly-built type. |
4305 | DependentSizedMatrixType *New = new (*this, alignof(DependentSizedMatrixType)) |
4306 | DependentSizedMatrixType(ElementTy, QualType(Canon, 0), RowExpr, |
4307 | ColumnExpr, AttrLoc); |
4308 | Types.push_back(New); |
4309 | return QualType(New, 0); |
4310 | } |
4311 | |
4312 | QualType ASTContext::getDependentAddressSpaceType(QualType PointeeType, |
4313 | Expr *AddrSpaceExpr, |
4314 | SourceLocation AttrLoc) const { |
4315 | assert(AddrSpaceExpr->isInstantiationDependent()); |
4316 | |
4317 | QualType canonPointeeType = getCanonicalType(T: PointeeType); |
4318 | |
4319 | void *insertPos = nullptr; |
4320 | llvm::FoldingSetNodeID ID; |
4321 | DependentAddressSpaceType::Profile(ID, Context: *this, PointeeType: canonPointeeType, |
4322 | AddrSpaceExpr); |
4323 | |
4324 | DependentAddressSpaceType *canonTy = |
4325 | DependentAddressSpaceTypes.FindNodeOrInsertPos(ID, InsertPos&: insertPos); |
4326 | |
4327 | if (!canonTy) { |
4328 | canonTy = new (*this, alignof(DependentAddressSpaceType)) |
4329 | DependentAddressSpaceType(canonPointeeType, QualType(), AddrSpaceExpr, |
4330 | AttrLoc); |
4331 | DependentAddressSpaceTypes.InsertNode(N: canonTy, InsertPos: insertPos); |
4332 | Types.push_back(canonTy); |
4333 | } |
4334 | |
4335 | if (canonPointeeType == PointeeType && |
4336 | canonTy->getAddrSpaceExpr() == AddrSpaceExpr) |
4337 | return QualType(canonTy, 0); |
4338 | |
4339 | auto *sugaredType = new (*this, alignof(DependentAddressSpaceType)) |
4340 | DependentAddressSpaceType(PointeeType, QualType(canonTy, 0), |
4341 | AddrSpaceExpr, AttrLoc); |
4342 | Types.push_back(Elt: sugaredType); |
4343 | return QualType(sugaredType, 0); |
4344 | } |
4345 | |
4346 | /// Determine whether \p T is canonical as the result type of a function. |
4347 | static bool isCanonicalResultType(QualType T) { |
4348 | return T.isCanonical() && |
4349 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
4350 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
4351 | } |
4352 | |
4353 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
4354 | QualType |
4355 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
4356 | const FunctionType::ExtInfo &Info) const { |
4357 | // FIXME: This assertion cannot be enabled (yet) because the ObjC rewriter |
4358 | // functionality creates a function without a prototype regardless of |
4359 | // language mode (so it makes them even in C++). Once the rewriter has been |
4360 | // fixed, this assertion can be enabled again. |
4361 | //assert(!LangOpts.requiresStrictPrototypes() && |
4362 | // "strict prototypes are disabled"); |
4363 | |
4364 | // Unique functions, to guarantee there is only one function of a particular |
4365 | // structure. |
4366 | llvm::FoldingSetNodeID ID; |
4367 | FunctionNoProtoType::Profile(ID, ResultType: ResultTy, Info); |
4368 | |
4369 | void *InsertPos = nullptr; |
4370 | if (FunctionNoProtoType *FT = |
4371 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4372 | return QualType(FT, 0); |
4373 | |
4374 | QualType Canonical; |
4375 | if (!isCanonicalResultType(T: ResultTy)) { |
4376 | Canonical = |
4377 | getFunctionNoProtoType(ResultTy: getCanonicalFunctionResultType(ResultType: ResultTy), Info); |
4378 | |
4379 | // Get the new insert position for the node we care about. |
4380 | FunctionNoProtoType *NewIP = |
4381 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
4382 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
4383 | } |
4384 | |
4385 | auto *New = new (*this, alignof(FunctionNoProtoType)) |
4386 | FunctionNoProtoType(ResultTy, Canonical, Info); |
4387 | Types.push_back(New); |
4388 | FunctionNoProtoTypes.InsertNode(N: New, InsertPos); |
4389 | return QualType(New, 0); |
4390 | } |
4391 | |
4392 | CanQualType |
4393 | ASTContext::getCanonicalFunctionResultType(QualType ResultType) const { |
4394 | CanQualType CanResultType = getCanonicalType(T: ResultType); |
4395 | |
4396 | // Canonical result types do not have ARC lifetime qualifiers. |
4397 | if (CanResultType.getQualifiers().hasObjCLifetime()) { |
4398 | Qualifiers Qs = CanResultType.getQualifiers(); |
4399 | Qs.removeObjCLifetime(); |
4400 | return CanQualType::CreateUnsafe( |
4401 | Other: getQualifiedType(T: CanResultType.getUnqualifiedType(), Qs)); |
4402 | } |
4403 | |
4404 | return CanResultType; |
4405 | } |
4406 | |
4407 | static bool isCanonicalExceptionSpecification( |
4408 | const FunctionProtoType::ExceptionSpecInfo &ESI, bool NoexceptInType) { |
4409 | if (ESI.Type == EST_None) |
4410 | return true; |
4411 | if (!NoexceptInType) |
4412 | return false; |
4413 | |
4414 | // C++17 onwards: exception specification is part of the type, as a simple |
4415 | // boolean "can this function type throw". |
4416 | if (ESI.Type == EST_BasicNoexcept) |
4417 | return true; |
4418 | |
4419 | // A noexcept(expr) specification is (possibly) canonical if expr is |
4420 | // value-dependent. |
4421 | if (ESI.Type == EST_DependentNoexcept) |
4422 | return true; |
4423 | |
4424 | // A dynamic exception specification is canonical if it only contains pack |
4425 | // expansions (so we can't tell whether it's non-throwing) and all its |
4426 | // contained types are canonical. |
4427 | if (ESI.Type == EST_Dynamic) { |
4428 | bool AnyPackExpansions = false; |
4429 | for (QualType ET : ESI.Exceptions) { |
4430 | if (!ET.isCanonical()) |
4431 | return false; |
4432 | if (ET->getAs<PackExpansionType>()) |
4433 | AnyPackExpansions = true; |
4434 | } |
4435 | return AnyPackExpansions; |
4436 | } |
4437 | |
4438 | return false; |
4439 | } |
4440 | |
4441 | QualType ASTContext::getFunctionTypeInternal( |
4442 | QualType ResultTy, ArrayRef<QualType> ArgArray, |
4443 | const FunctionProtoType::ExtProtoInfo &EPI, bool OnlyWantCanonical) const { |
4444 | size_t NumArgs = ArgArray.size(); |
4445 | |
4446 | // Unique functions, to guarantee there is only one function of a particular |
4447 | // structure. |
4448 | llvm::FoldingSetNodeID ID; |
4449 | FunctionProtoType::Profile(ID, Result: ResultTy, ArgTys: ArgArray.begin(), NumArgs, EPI, |
4450 | Context: *this, Canonical: true); |
4451 | |
4452 | QualType Canonical; |
4453 | bool Unique = false; |
4454 | |
4455 | void *InsertPos = nullptr; |
4456 | if (FunctionProtoType *FPT = |
4457 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) { |
4458 | QualType Existing = QualType(FPT, 0); |
4459 | |
4460 | // If we find a pre-existing equivalent FunctionProtoType, we can just reuse |
4461 | // it so long as our exception specification doesn't contain a dependent |
4462 | // noexcept expression, or we're just looking for a canonical type. |
4463 | // Otherwise, we're going to need to create a type |
4464 | // sugar node to hold the concrete expression. |
4465 | if (OnlyWantCanonical || !isComputedNoexcept(EPI.ExceptionSpec.Type) || |
4466 | EPI.ExceptionSpec.NoexceptExpr == FPT->getNoexceptExpr()) |
4467 | return Existing; |
4468 | |
4469 | // We need a new type sugar node for this one, to hold the new noexcept |
4470 | // expression. We do no canonicalization here, but that's OK since we don't |
4471 | // expect to see the same noexcept expression much more than once. |
4472 | Canonical = getCanonicalType(T: Existing); |
4473 | Unique = true; |
4474 | } |
4475 | |
4476 | bool NoexceptInType = getLangOpts().CPlusPlus17; |
4477 | bool IsCanonicalExceptionSpec = |
4478 | isCanonicalExceptionSpecification(EPI.ExceptionSpec, NoexceptInType); |
4479 | |
4480 | // Determine whether the type being created is already canonical or not. |
4481 | bool isCanonical = !Unique && IsCanonicalExceptionSpec && |
4482 | isCanonicalResultType(T: ResultTy) && !EPI.HasTrailingReturn; |
4483 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
4484 | if (!ArgArray[i].isCanonicalAsParam()) |
4485 | isCanonical = false; |
4486 | |
4487 | if (OnlyWantCanonical) |
4488 | assert(isCanonical && |
4489 | "given non-canonical parameters constructing canonical type" ); |
4490 | |
4491 | // If this type isn't canonical, get the canonical version of it if we don't |
4492 | // already have it. The exception spec is only partially part of the |
4493 | // canonical type, and only in C++17 onwards. |
4494 | if (!isCanonical && Canonical.isNull()) { |
4495 | SmallVector<QualType, 16> CanonicalArgs; |
4496 | CanonicalArgs.reserve(N: NumArgs); |
4497 | for (unsigned i = 0; i != NumArgs; ++i) |
4498 | CanonicalArgs.push_back(Elt: getCanonicalParamType(T: ArgArray[i])); |
4499 | |
4500 | llvm::SmallVector<QualType, 8> ExceptionTypeStorage; |
4501 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
4502 | CanonicalEPI.HasTrailingReturn = false; |
4503 | |
4504 | if (IsCanonicalExceptionSpec) { |
4505 | // Exception spec is already OK. |
4506 | } else if (NoexceptInType) { |
4507 | switch (EPI.ExceptionSpec.Type) { |
4508 | case EST_Unparsed: case EST_Unevaluated: case EST_Uninstantiated: |
4509 | // We don't know yet. It shouldn't matter what we pick here; no-one |
4510 | // should ever look at this. |
4511 | [[fallthrough]]; |
4512 | case EST_None: case EST_MSAny: case EST_NoexceptFalse: |
4513 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
4514 | break; |
4515 | |
4516 | // A dynamic exception specification is almost always "not noexcept", |
4517 | // with the exception that a pack expansion might expand to no types. |
4518 | case EST_Dynamic: { |
4519 | bool AnyPacks = false; |
4520 | for (QualType ET : EPI.ExceptionSpec.Exceptions) { |
4521 | if (ET->getAs<PackExpansionType>()) |
4522 | AnyPacks = true; |
4523 | ExceptionTypeStorage.push_back(getCanonicalType(ET)); |
4524 | } |
4525 | if (!AnyPacks) |
4526 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
4527 | else { |
4528 | CanonicalEPI.ExceptionSpec.Type = EST_Dynamic; |
4529 | CanonicalEPI.ExceptionSpec.Exceptions = ExceptionTypeStorage; |
4530 | } |
4531 | break; |
4532 | } |
4533 | |
4534 | case EST_DynamicNone: |
4535 | case EST_BasicNoexcept: |
4536 | case EST_NoexceptTrue: |
4537 | case EST_NoThrow: |
4538 | CanonicalEPI.ExceptionSpec.Type = EST_BasicNoexcept; |
4539 | break; |
4540 | |
4541 | case EST_DependentNoexcept: |
4542 | llvm_unreachable("dependent noexcept is already canonical" ); |
4543 | } |
4544 | } else { |
4545 | CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo(); |
4546 | } |
4547 | |
4548 | // Adjust the canonical function result type. |
4549 | CanQualType CanResultTy = getCanonicalFunctionResultType(ResultType: ResultTy); |
4550 | Canonical = |
4551 | getFunctionTypeInternal(ResultTy: CanResultTy, ArgArray: CanonicalArgs, EPI: CanonicalEPI, OnlyWantCanonical: true); |
4552 | |
4553 | // Get the new insert position for the node we care about. |
4554 | FunctionProtoType *NewIP = |
4555 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
4556 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
4557 | } |
4558 | |
4559 | // Compute the needed size to hold this FunctionProtoType and the |
4560 | // various trailing objects. |
4561 | auto ESH = FunctionProtoType::getExceptionSpecSize( |
4562 | EPI.ExceptionSpec.Type, EPI.ExceptionSpec.Exceptions.size()); |
4563 | size_t Size = FunctionProtoType::totalSizeToAlloc< |
4564 | QualType, SourceLocation, FunctionType::FunctionTypeExtraBitfields, |
4565 | FunctionType::FunctionTypeArmAttributes, FunctionType::ExceptionType, |
4566 | Expr *, FunctionDecl *, FunctionProtoType::ExtParameterInfo, Qualifiers>( |
4567 | NumArgs, EPI.Variadic, EPI.requiresFunctionProtoTypeExtraBitfields(), |
4568 | EPI.requiresFunctionProtoTypeArmAttributes(), ESH.NumExceptionType, |
4569 | ESH.NumExprPtr, ESH.NumFunctionDeclPtr, |
4570 | EPI.ExtParameterInfos ? NumArgs : 0, |
4571 | EPI.TypeQuals.hasNonFastQualifiers() ? 1 : 0); |
4572 | |
4573 | auto *FTP = (FunctionProtoType *)Allocate(Size, Align: alignof(FunctionProtoType)); |
4574 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
4575 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
4576 | Types.push_back(FTP); |
4577 | if (!Unique) |
4578 | FunctionProtoTypes.InsertNode(N: FTP, InsertPos); |
4579 | return QualType(FTP, 0); |
4580 | } |
4581 | |
4582 | QualType ASTContext::getPipeType(QualType T, bool ReadOnly) const { |
4583 | llvm::FoldingSetNodeID ID; |
4584 | PipeType::Profile(ID, T, isRead: ReadOnly); |
4585 | |
4586 | void *InsertPos = nullptr; |
4587 | if (PipeType *PT = PipeTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4588 | return QualType(PT, 0); |
4589 | |
4590 | // If the pipe element type isn't canonical, this won't be a canonical type |
4591 | // either, so fill in the canonical type field. |
4592 | QualType Canonical; |
4593 | if (!T.isCanonical()) { |
4594 | Canonical = getPipeType(T: getCanonicalType(T), ReadOnly); |
4595 | |
4596 | // Get the new insert position for the node we care about. |
4597 | PipeType *NewIP = PipeTypes.FindNodeOrInsertPos(ID, InsertPos); |
4598 | assert(!NewIP && "Shouldn't be in the map!" ); |
4599 | (void)NewIP; |
4600 | } |
4601 | auto *New = new (*this, alignof(PipeType)) PipeType(T, Canonical, ReadOnly); |
4602 | Types.push_back(New); |
4603 | PipeTypes.InsertNode(N: New, InsertPos); |
4604 | return QualType(New, 0); |
4605 | } |
4606 | |
4607 | QualType ASTContext::adjustStringLiteralBaseType(QualType Ty) const { |
4608 | // OpenCL v1.1 s6.5.3: a string literal is in the constant address space. |
4609 | return LangOpts.OpenCL ? getAddrSpaceQualType(T: Ty, AddressSpace: LangAS::opencl_constant) |
4610 | : Ty; |
4611 | } |
4612 | |
4613 | QualType ASTContext::getReadPipeType(QualType T) const { |
4614 | return getPipeType(T, ReadOnly: true); |
4615 | } |
4616 | |
4617 | QualType ASTContext::getWritePipeType(QualType T) const { |
4618 | return getPipeType(T, ReadOnly: false); |
4619 | } |
4620 | |
4621 | QualType ASTContext::getBitIntType(bool IsUnsigned, unsigned NumBits) const { |
4622 | llvm::FoldingSetNodeID ID; |
4623 | BitIntType::Profile(ID, IsUnsigned, NumBits); |
4624 | |
4625 | void *InsertPos = nullptr; |
4626 | if (BitIntType *EIT = BitIntTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4627 | return QualType(EIT, 0); |
4628 | |
4629 | auto *New = new (*this, alignof(BitIntType)) BitIntType(IsUnsigned, NumBits); |
4630 | BitIntTypes.InsertNode(N: New, InsertPos); |
4631 | Types.push_back(New); |
4632 | return QualType(New, 0); |
4633 | } |
4634 | |
4635 | QualType ASTContext::getDependentBitIntType(bool IsUnsigned, |
4636 | Expr *NumBitsExpr) const { |
4637 | assert(NumBitsExpr->isInstantiationDependent() && "Only good for dependent" ); |
4638 | llvm::FoldingSetNodeID ID; |
4639 | DependentBitIntType::Profile(ID, Context: *this, IsUnsigned, NumBitsExpr); |
4640 | |
4641 | void *InsertPos = nullptr; |
4642 | if (DependentBitIntType *Existing = |
4643 | DependentBitIntTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4644 | return QualType(Existing, 0); |
4645 | |
4646 | auto *New = new (*this, alignof(DependentBitIntType)) |
4647 | DependentBitIntType(IsUnsigned, NumBitsExpr); |
4648 | DependentBitIntTypes.InsertNode(N: New, InsertPos); |
4649 | |
4650 | Types.push_back(New); |
4651 | return QualType(New, 0); |
4652 | } |
4653 | |
4654 | #ifndef NDEBUG |
4655 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
4656 | if (!isa<CXXRecordDecl>(Val: D)) return false; |
4657 | const auto *RD = cast<CXXRecordDecl>(Val: D); |
4658 | if (isa<ClassTemplatePartialSpecializationDecl>(Val: RD)) |
4659 | return true; |
4660 | if (RD->getDescribedClassTemplate() && |
4661 | !isa<ClassTemplateSpecializationDecl>(Val: RD)) |
4662 | return true; |
4663 | return false; |
4664 | } |
4665 | #endif |
4666 | |
4667 | /// getInjectedClassNameType - Return the unique reference to the |
4668 | /// injected class name type for the specified templated declaration. |
4669 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
4670 | QualType TST) const { |
4671 | assert(NeedsInjectedClassNameType(Decl)); |
4672 | if (Decl->TypeForDecl) { |
4673 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
4674 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
4675 | assert(PrevDecl->TypeForDecl && "previous declaration has no type" ); |
4676 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
4677 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
4678 | } else { |
4679 | Type *newType = new (*this, alignof(InjectedClassNameType)) |
4680 | InjectedClassNameType(Decl, TST); |
4681 | Decl->TypeForDecl = newType; |
4682 | Types.push_back(Elt: newType); |
4683 | } |
4684 | return QualType(Decl->TypeForDecl, 0); |
4685 | } |
4686 | |
4687 | /// getTypeDeclType - Return the unique reference to the type for the |
4688 | /// specified type declaration. |
4689 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
4690 | assert(Decl && "Passed null for Decl param" ); |
4691 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case" ); |
4692 | |
4693 | if (const auto *Typedef = dyn_cast<TypedefNameDecl>(Val: Decl)) |
4694 | return getTypedefType(Decl: Typedef); |
4695 | |
4696 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
4697 | "Template type parameter types are always available." ); |
4698 | |
4699 | if (const auto *Record = dyn_cast<RecordDecl>(Val: Decl)) { |
4700 | assert(Record->isFirstDecl() && "struct/union has previous declaration" ); |
4701 | assert(!NeedsInjectedClassNameType(Record)); |
4702 | return getRecordType(Decl: Record); |
4703 | } else if (const auto *Enum = dyn_cast<EnumDecl>(Val: Decl)) { |
4704 | assert(Enum->isFirstDecl() && "enum has previous declaration" ); |
4705 | return getEnumType(Decl: Enum); |
4706 | } else if (const auto *Using = dyn_cast<UnresolvedUsingTypenameDecl>(Val: Decl)) { |
4707 | return getUnresolvedUsingType(Decl: Using); |
4708 | } else |
4709 | llvm_unreachable("TypeDecl without a type?" ); |
4710 | |
4711 | return QualType(Decl->TypeForDecl, 0); |
4712 | } |
4713 | |
4714 | /// getTypedefType - Return the unique reference to the type for the |
4715 | /// specified typedef name decl. |
4716 | QualType ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
4717 | QualType Underlying) const { |
4718 | if (!Decl->TypeForDecl) { |
4719 | if (Underlying.isNull()) |
4720 | Underlying = Decl->getUnderlyingType(); |
4721 | auto *NewType = new (*this, alignof(TypedefType)) TypedefType( |
4722 | Type::Typedef, Decl, QualType(), getCanonicalType(Underlying)); |
4723 | Decl->TypeForDecl = NewType; |
4724 | Types.push_back(Elt: NewType); |
4725 | return QualType(NewType, 0); |
4726 | } |
4727 | if (Underlying.isNull() || Decl->getUnderlyingType() == Underlying) |
4728 | return QualType(Decl->TypeForDecl, 0); |
4729 | assert(hasSameType(Decl->getUnderlyingType(), Underlying)); |
4730 | |
4731 | llvm::FoldingSetNodeID ID; |
4732 | TypedefType::Profile(ID, Decl, Underlying); |
4733 | |
4734 | void *InsertPos = nullptr; |
4735 | if (TypedefType *T = TypedefTypes.FindNodeOrInsertPos(ID, InsertPos)) { |
4736 | assert(!T->typeMatchesDecl() && |
4737 | "non-divergent case should be handled with TypeDecl" ); |
4738 | return QualType(T, 0); |
4739 | } |
4740 | |
4741 | void *Mem = Allocate(TypedefType::totalSizeToAlloc<QualType>(true), |
4742 | alignof(TypedefType)); |
4743 | auto *NewType = new (Mem) TypedefType(Type::Typedef, Decl, Underlying, |
4744 | getCanonicalType(Underlying)); |
4745 | TypedefTypes.InsertNode(NewType, InsertPos); |
4746 | Types.push_back(Elt: NewType); |
4747 | return QualType(NewType, 0); |
4748 | } |
4749 | |
4750 | QualType ASTContext::getUsingType(const UsingShadowDecl *Found, |
4751 | QualType Underlying) const { |
4752 | llvm::FoldingSetNodeID ID; |
4753 | UsingType::Profile(ID, Found, Underlying); |
4754 | |
4755 | void *InsertPos = nullptr; |
4756 | if (UsingType *T = UsingTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4757 | return QualType(T, 0); |
4758 | |
4759 | const Type *TypeForDecl = |
4760 | cast<TypeDecl>(Val: Found->getTargetDecl())->getTypeForDecl(); |
4761 | |
4762 | assert(!Underlying.hasLocalQualifiers()); |
4763 | QualType Canon = Underlying->getCanonicalTypeInternal(); |
4764 | assert(TypeForDecl->getCanonicalTypeInternal() == Canon); |
4765 | |
4766 | if (Underlying.getTypePtr() == TypeForDecl) |
4767 | Underlying = QualType(); |
4768 | void *Mem = |
4769 | Allocate(UsingType::totalSizeToAlloc<QualType>(!Underlying.isNull()), |
4770 | alignof(UsingType)); |
4771 | UsingType *NewType = new (Mem) UsingType(Found, Underlying, Canon); |
4772 | Types.push_back(NewType); |
4773 | UsingTypes.InsertNode(N: NewType, InsertPos); |
4774 | return QualType(NewType, 0); |
4775 | } |
4776 | |
4777 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
4778 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
4779 | |
4780 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
4781 | if (PrevDecl->TypeForDecl) |
4782 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
4783 | |
4784 | auto *newType = new (*this, alignof(RecordType)) RecordType(Decl); |
4785 | Decl->TypeForDecl = newType; |
4786 | Types.push_back(newType); |
4787 | return QualType(newType, 0); |
4788 | } |
4789 | |
4790 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
4791 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
4792 | |
4793 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
4794 | if (PrevDecl->TypeForDecl) |
4795 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
4796 | |
4797 | auto *newType = new (*this, alignof(EnumType)) EnumType(Decl); |
4798 | Decl->TypeForDecl = newType; |
4799 | Types.push_back(newType); |
4800 | return QualType(newType, 0); |
4801 | } |
4802 | |
4803 | QualType ASTContext::getUnresolvedUsingType( |
4804 | const UnresolvedUsingTypenameDecl *Decl) const { |
4805 | if (Decl->TypeForDecl) |
4806 | return QualType(Decl->TypeForDecl, 0); |
4807 | |
4808 | if (const UnresolvedUsingTypenameDecl *CanonicalDecl = |
4809 | Decl->getCanonicalDecl()) |
4810 | if (CanonicalDecl->TypeForDecl) |
4811 | return QualType(Decl->TypeForDecl = CanonicalDecl->TypeForDecl, 0); |
4812 | |
4813 | Type *newType = |
4814 | new (*this, alignof(UnresolvedUsingType)) UnresolvedUsingType(Decl); |
4815 | Decl->TypeForDecl = newType; |
4816 | Types.push_back(Elt: newType); |
4817 | return QualType(newType, 0); |
4818 | } |
4819 | |
4820 | QualType ASTContext::getAttributedType(attr::Kind attrKind, |
4821 | QualType modifiedType, |
4822 | QualType equivalentType) const { |
4823 | llvm::FoldingSetNodeID id; |
4824 | AttributedType::Profile(ID&: id, attrKind, modified: modifiedType, equivalent: equivalentType); |
4825 | |
4826 | void *insertPos = nullptr; |
4827 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(ID: id, InsertPos&: insertPos); |
4828 | if (type) return QualType(type, 0); |
4829 | |
4830 | QualType canon = getCanonicalType(T: equivalentType); |
4831 | type = new (*this, alignof(AttributedType)) |
4832 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
4833 | |
4834 | Types.push_back(type); |
4835 | AttributedTypes.InsertNode(N: type, InsertPos: insertPos); |
4836 | |
4837 | return QualType(type, 0); |
4838 | } |
4839 | |
4840 | QualType ASTContext::getBTFTagAttributedType(const BTFTypeTagAttr *BTFAttr, |
4841 | QualType Wrapped) { |
4842 | llvm::FoldingSetNodeID ID; |
4843 | BTFTagAttributedType::Profile(ID, Wrapped, BTFAttr); |
4844 | |
4845 | void *InsertPos = nullptr; |
4846 | BTFTagAttributedType *Ty = |
4847 | BTFTagAttributedTypes.FindNodeOrInsertPos(ID, InsertPos); |
4848 | if (Ty) |
4849 | return QualType(Ty, 0); |
4850 | |
4851 | QualType Canon = getCanonicalType(T: Wrapped); |
4852 | Ty = new (*this, alignof(BTFTagAttributedType)) |
4853 | BTFTagAttributedType(Canon, Wrapped, BTFAttr); |
4854 | |
4855 | Types.push_back(Ty); |
4856 | BTFTagAttributedTypes.InsertNode(N: Ty, InsertPos); |
4857 | |
4858 | return QualType(Ty, 0); |
4859 | } |
4860 | |
4861 | /// Retrieve a substitution-result type. |
4862 | QualType ASTContext::getSubstTemplateTypeParmType( |
4863 | QualType Replacement, Decl *AssociatedDecl, unsigned Index, |
4864 | std::optional<unsigned> PackIndex) const { |
4865 | llvm::FoldingSetNodeID ID; |
4866 | SubstTemplateTypeParmType::Profile(ID, Replacement, AssociatedDecl, Index, |
4867 | PackIndex); |
4868 | void *InsertPos = nullptr; |
4869 | SubstTemplateTypeParmType *SubstParm = |
4870 | SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
4871 | |
4872 | if (!SubstParm) { |
4873 | void *Mem = Allocate(SubstTemplateTypeParmType::totalSizeToAlloc<QualType>( |
4874 | !Replacement.isCanonical()), |
4875 | alignof(SubstTemplateTypeParmType)); |
4876 | SubstParm = new (Mem) SubstTemplateTypeParmType(Replacement, AssociatedDecl, |
4877 | Index, PackIndex); |
4878 | Types.push_back(SubstParm); |
4879 | SubstTemplateTypeParmTypes.InsertNode(N: SubstParm, InsertPos); |
4880 | } |
4881 | |
4882 | return QualType(SubstParm, 0); |
4883 | } |
4884 | |
4885 | /// Retrieve a |
4886 | QualType |
4887 | ASTContext::getSubstTemplateTypeParmPackType(Decl *AssociatedDecl, |
4888 | unsigned Index, bool Final, |
4889 | const TemplateArgument &ArgPack) { |
4890 | #ifndef NDEBUG |
4891 | for (const auto &P : ArgPack.pack_elements()) |
4892 | assert(P.getKind() == TemplateArgument::Type && "Pack contains a non-type" ); |
4893 | #endif |
4894 | |
4895 | llvm::FoldingSetNodeID ID; |
4896 | SubstTemplateTypeParmPackType::Profile(ID, AssociatedDecl, Index, Final, |
4897 | ArgPack); |
4898 | void *InsertPos = nullptr; |
4899 | if (SubstTemplateTypeParmPackType *SubstParm = |
4900 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
4901 | return QualType(SubstParm, 0); |
4902 | |
4903 | QualType Canon; |
4904 | { |
4905 | TemplateArgument CanonArgPack = getCanonicalTemplateArgument(Arg: ArgPack); |
4906 | if (!AssociatedDecl->isCanonicalDecl() || |
4907 | !CanonArgPack.structurallyEquals(Other: ArgPack)) { |
4908 | Canon = getSubstTemplateTypeParmPackType( |
4909 | AssociatedDecl: AssociatedDecl->getCanonicalDecl(), Index, Final, ArgPack: CanonArgPack); |
4910 | [[maybe_unused]] const auto *Nothing = |
4911 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
4912 | assert(!Nothing); |
4913 | } |
4914 | } |
4915 | |
4916 | auto *SubstParm = new (*this, alignof(SubstTemplateTypeParmPackType)) |
4917 | SubstTemplateTypeParmPackType(Canon, AssociatedDecl, Index, Final, |
4918 | ArgPack); |
4919 | Types.push_back(SubstParm); |
4920 | SubstTemplateTypeParmPackTypes.InsertNode(N: SubstParm, InsertPos); |
4921 | return QualType(SubstParm, 0); |
4922 | } |
4923 | |
4924 | /// Retrieve the template type parameter type for a template |
4925 | /// parameter or parameter pack with the given depth, index, and (optionally) |
4926 | /// name. |
4927 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
4928 | bool ParameterPack, |
4929 | TemplateTypeParmDecl *TTPDecl) const { |
4930 | llvm::FoldingSetNodeID ID; |
4931 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
4932 | void *InsertPos = nullptr; |
4933 | TemplateTypeParmType *TypeParm |
4934 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
4935 | |
4936 | if (TypeParm) |
4937 | return QualType(TypeParm, 0); |
4938 | |
4939 | if (TTPDecl) { |
4940 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
4941 | TypeParm = new (*this, alignof(TemplateTypeParmType)) |
4942 | TemplateTypeParmType(TTPDecl, Canon); |
4943 | |
4944 | TemplateTypeParmType *TypeCheck |
4945 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
4946 | assert(!TypeCheck && "Template type parameter canonical type broken" ); |
4947 | (void)TypeCheck; |
4948 | } else |
4949 | TypeParm = new (*this, alignof(TemplateTypeParmType)) |
4950 | TemplateTypeParmType(Depth, Index, ParameterPack); |
4951 | |
4952 | Types.push_back(TypeParm); |
4953 | TemplateTypeParmTypes.InsertNode(N: TypeParm, InsertPos); |
4954 | |
4955 | return QualType(TypeParm, 0); |
4956 | } |
4957 | |
4958 | TypeSourceInfo * |
4959 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
4960 | SourceLocation NameLoc, |
4961 | const TemplateArgumentListInfo &Args, |
4962 | QualType Underlying) const { |
4963 | assert(!Name.getAsDependentTemplateName() && |
4964 | "No dependent template names here!" ); |
4965 | QualType TST = |
4966 | getTemplateSpecializationType(T: Name, Args: Args.arguments(), Canon: Underlying); |
4967 | |
4968 | TypeSourceInfo *DI = CreateTypeSourceInfo(T: TST); |
4969 | TemplateSpecializationTypeLoc TL = |
4970 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
4971 | TL.setTemplateKeywordLoc(SourceLocation()); |
4972 | TL.setTemplateNameLoc(NameLoc); |
4973 | TL.setLAngleLoc(Args.getLAngleLoc()); |
4974 | TL.setRAngleLoc(Args.getRAngleLoc()); |
4975 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
4976 | TL.setArgLocInfo(i, AI: Args[i].getLocInfo()); |
4977 | return DI; |
4978 | } |
4979 | |
4980 | QualType |
4981 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
4982 | ArrayRef<TemplateArgumentLoc> Args, |
4983 | QualType Underlying) const { |
4984 | assert(!Template.getAsDependentTemplateName() && |
4985 | "No dependent template names here!" ); |
4986 | |
4987 | SmallVector<TemplateArgument, 4> ArgVec; |
4988 | ArgVec.reserve(N: Args.size()); |
4989 | for (const TemplateArgumentLoc &Arg : Args) |
4990 | ArgVec.push_back(Elt: Arg.getArgument()); |
4991 | |
4992 | return getTemplateSpecializationType(T: Template, Args: ArgVec, Canon: Underlying); |
4993 | } |
4994 | |
4995 | #ifndef NDEBUG |
4996 | static bool hasAnyPackExpansions(ArrayRef<TemplateArgument> Args) { |
4997 | for (const TemplateArgument &Arg : Args) |
4998 | if (Arg.isPackExpansion()) |
4999 | return true; |
5000 | |
5001 | return true; |
5002 | } |
5003 | #endif |
5004 | |
5005 | QualType |
5006 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
5007 | ArrayRef<TemplateArgument> Args, |
5008 | QualType Underlying) const { |
5009 | assert(!Template.getAsDependentTemplateName() && |
5010 | "No dependent template names here!" ); |
5011 | // Look through qualified template names. |
5012 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
5013 | Template = QTN->getUnderlyingTemplate(); |
5014 | |
5015 | const auto *TD = Template.getAsTemplateDecl(); |
5016 | bool IsTypeAlias = TD && TD->isTypeAlias(); |
5017 | QualType CanonType; |
5018 | if (!Underlying.isNull()) |
5019 | CanonType = getCanonicalType(T: Underlying); |
5020 | else { |
5021 | // We can get here with an alias template when the specialization contains |
5022 | // a pack expansion that does not match up with a parameter pack. |
5023 | assert((!IsTypeAlias || hasAnyPackExpansions(Args)) && |
5024 | "Caller must compute aliased type" ); |
5025 | IsTypeAlias = false; |
5026 | CanonType = getCanonicalTemplateSpecializationType(T: Template, Args); |
5027 | } |
5028 | |
5029 | // Allocate the (non-canonical) template specialization type, but don't |
5030 | // try to unique it: these types typically have location information that |
5031 | // we don't unique and don't want to lose. |
5032 | void *Mem = Allocate(Size: sizeof(TemplateSpecializationType) + |
5033 | sizeof(TemplateArgument) * Args.size() + |
5034 | (IsTypeAlias ? sizeof(QualType) : 0), |
5035 | Align: alignof(TemplateSpecializationType)); |
5036 | auto *Spec |
5037 | = new (Mem) TemplateSpecializationType(Template, Args, CanonType, |
5038 | IsTypeAlias ? Underlying : QualType()); |
5039 | |
5040 | Types.push_back(Spec); |
5041 | return QualType(Spec, 0); |
5042 | } |
5043 | |
5044 | QualType ASTContext::getCanonicalTemplateSpecializationType( |
5045 | TemplateName Template, ArrayRef<TemplateArgument> Args) const { |
5046 | assert(!Template.getAsDependentTemplateName() && |
5047 | "No dependent template names here!" ); |
5048 | |
5049 | // Look through qualified template names. |
5050 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
5051 | Template = TemplateName(QTN->getUnderlyingTemplate()); |
5052 | |
5053 | // Build the canonical template specialization type. |
5054 | TemplateName CanonTemplate = getCanonicalTemplateName(Name: Template); |
5055 | bool AnyNonCanonArgs = false; |
5056 | auto CanonArgs = |
5057 | ::getCanonicalTemplateArguments(C: *this, Args, AnyNonCanonArgs); |
5058 | |
5059 | // Determine whether this canonical template specialization type already |
5060 | // exists. |
5061 | llvm::FoldingSetNodeID ID; |
5062 | TemplateSpecializationType::Profile(ID, T: CanonTemplate, |
5063 | Args: CanonArgs, Context: *this); |
5064 | |
5065 | void *InsertPos = nullptr; |
5066 | TemplateSpecializationType *Spec |
5067 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
5068 | |
5069 | if (!Spec) { |
5070 | // Allocate a new canonical template specialization type. |
5071 | void *Mem = Allocate(Size: (sizeof(TemplateSpecializationType) + |
5072 | sizeof(TemplateArgument) * CanonArgs.size()), |
5073 | Align: alignof(TemplateSpecializationType)); |
5074 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
5075 | CanonArgs, |
5076 | QualType(), QualType()); |
5077 | Types.push_back(Spec); |
5078 | TemplateSpecializationTypes.InsertNode(N: Spec, InsertPos); |
5079 | } |
5080 | |
5081 | assert(Spec->isDependentType() && |
5082 | "Non-dependent template-id type must have a canonical type" ); |
5083 | return QualType(Spec, 0); |
5084 | } |
5085 | |
5086 | QualType ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
5087 | NestedNameSpecifier *NNS, |
5088 | QualType NamedType, |
5089 | TagDecl *OwnedTagDecl) const { |
5090 | llvm::FoldingSetNodeID ID; |
5091 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType, OwnedTagDecl); |
5092 | |
5093 | void *InsertPos = nullptr; |
5094 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
5095 | if (T) |
5096 | return QualType(T, 0); |
5097 | |
5098 | QualType Canon = NamedType; |
5099 | if (!Canon.isCanonical()) { |
5100 | Canon = getCanonicalType(T: NamedType); |
5101 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
5102 | assert(!CheckT && "Elaborated canonical type broken" ); |
5103 | (void)CheckT; |
5104 | } |
5105 | |
5106 | void *Mem = |
5107 | Allocate(ElaboratedType::totalSizeToAlloc<TagDecl *>(!!OwnedTagDecl), |
5108 | alignof(ElaboratedType)); |
5109 | T = new (Mem) ElaboratedType(Keyword, NNS, NamedType, Canon, OwnedTagDecl); |
5110 | |
5111 | Types.push_back(T); |
5112 | ElaboratedTypes.InsertNode(N: T, InsertPos); |
5113 | return QualType(T, 0); |
5114 | } |
5115 | |
5116 | QualType |
5117 | ASTContext::getParenType(QualType InnerType) const { |
5118 | llvm::FoldingSetNodeID ID; |
5119 | ParenType::Profile(ID, Inner: InnerType); |
5120 | |
5121 | void *InsertPos = nullptr; |
5122 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
5123 | if (T) |
5124 | return QualType(T, 0); |
5125 | |
5126 | QualType Canon = InnerType; |
5127 | if (!Canon.isCanonical()) { |
5128 | Canon = getCanonicalType(T: InnerType); |
5129 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
5130 | assert(!CheckT && "Paren canonical type broken" ); |
5131 | (void)CheckT; |
5132 | } |
5133 | |
5134 | T = new (*this, alignof(ParenType)) ParenType(InnerType, Canon); |
5135 | Types.push_back(T); |
5136 | ParenTypes.InsertNode(N: T, InsertPos); |
5137 | return QualType(T, 0); |
5138 | } |
5139 | |
5140 | QualType |
5141 | ASTContext::getMacroQualifiedType(QualType UnderlyingTy, |
5142 | const IdentifierInfo *MacroII) const { |
5143 | QualType Canon = UnderlyingTy; |
5144 | if (!Canon.isCanonical()) |
5145 | Canon = getCanonicalType(T: UnderlyingTy); |
5146 | |
5147 | auto *newType = new (*this, alignof(MacroQualifiedType)) |
5148 | MacroQualifiedType(UnderlyingTy, Canon, MacroII); |
5149 | Types.push_back(newType); |
5150 | return QualType(newType, 0); |
5151 | } |
5152 | |
5153 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
5154 | NestedNameSpecifier *NNS, |
5155 | const IdentifierInfo *Name, |
5156 | QualType Canon) const { |
5157 | if (Canon.isNull()) { |
5158 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
5159 | if (CanonNNS != NNS) |
5160 | Canon = getDependentNameType(Keyword, NNS: CanonNNS, Name); |
5161 | } |
5162 | |
5163 | llvm::FoldingSetNodeID ID; |
5164 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
5165 | |
5166 | void *InsertPos = nullptr; |
5167 | DependentNameType *T |
5168 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
5169 | if (T) |
5170 | return QualType(T, 0); |
5171 | |
5172 | T = new (*this, alignof(DependentNameType)) |
5173 | DependentNameType(Keyword, NNS, Name, Canon); |
5174 | Types.push_back(T); |
5175 | DependentNameTypes.InsertNode(N: T, InsertPos); |
5176 | return QualType(T, 0); |
5177 | } |
5178 | |
5179 | QualType ASTContext::getDependentTemplateSpecializationType( |
5180 | ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5181 | const IdentifierInfo *Name, ArrayRef<TemplateArgumentLoc> Args) const { |
5182 | // TODO: avoid this copy |
5183 | SmallVector<TemplateArgument, 16> ArgCopy; |
5184 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
5185 | ArgCopy.push_back(Elt: Args[I].getArgument()); |
5186 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, Args: ArgCopy); |
5187 | } |
5188 | |
5189 | QualType |
5190 | ASTContext::getDependentTemplateSpecializationType( |
5191 | ElaboratedTypeKeyword Keyword, |
5192 | NestedNameSpecifier *NNS, |
5193 | const IdentifierInfo *Name, |
5194 | ArrayRef<TemplateArgument> Args) const { |
5195 | assert((!NNS || NNS->isDependent()) && |
5196 | "nested-name-specifier must be dependent" ); |
5197 | |
5198 | llvm::FoldingSetNodeID ID; |
5199 | DependentTemplateSpecializationType::Profile(ID, Context: *this, Keyword, Qualifier: NNS, |
5200 | Name, Args); |
5201 | |
5202 | void *InsertPos = nullptr; |
5203 | DependentTemplateSpecializationType *T |
5204 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
5205 | if (T) |
5206 | return QualType(T, 0); |
5207 | |
5208 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
5209 | |
5210 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
5211 | if (Keyword == ElaboratedTypeKeyword::None) |
5212 | CanonKeyword = ElaboratedTypeKeyword::Typename; |
5213 | |
5214 | bool AnyNonCanonArgs = false; |
5215 | auto CanonArgs = |
5216 | ::getCanonicalTemplateArguments(C: *this, Args, AnyNonCanonArgs); |
5217 | |
5218 | QualType Canon; |
5219 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
5220 | Canon = getDependentTemplateSpecializationType(Keyword: CanonKeyword, NNS: CanonNNS, |
5221 | Name, |
5222 | Args: CanonArgs); |
5223 | |
5224 | // Find the insert position again. |
5225 | [[maybe_unused]] auto *Nothing = |
5226 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
5227 | assert(!Nothing && "canonical type broken" ); |
5228 | } |
5229 | |
5230 | void *Mem = Allocate(Size: (sizeof(DependentTemplateSpecializationType) + |
5231 | sizeof(TemplateArgument) * Args.size()), |
5232 | Align: alignof(DependentTemplateSpecializationType)); |
5233 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
5234 | Name, Args, Canon); |
5235 | Types.push_back(T); |
5236 | DependentTemplateSpecializationTypes.InsertNode(N: T, InsertPos); |
5237 | return QualType(T, 0); |
5238 | } |
5239 | |
5240 | TemplateArgument ASTContext::getInjectedTemplateArg(NamedDecl *Param) { |
5241 | TemplateArgument Arg; |
5242 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Val: Param)) { |
5243 | QualType ArgType = getTypeDeclType(TTP); |
5244 | if (TTP->isParameterPack()) |
5245 | ArgType = getPackExpansionType(Pattern: ArgType, NumExpansions: std::nullopt); |
5246 | |
5247 | Arg = TemplateArgument(ArgType); |
5248 | } else if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Val: Param)) { |
5249 | QualType T = |
5250 | NTTP->getType().getNonPackExpansionType().getNonLValueExprType(*this); |
5251 | // For class NTTPs, ensure we include the 'const' so the type matches that |
5252 | // of a real template argument. |
5253 | // FIXME: It would be more faithful to model this as something like an |
5254 | // lvalue-to-rvalue conversion applied to a const-qualified lvalue. |
5255 | if (T->isRecordType()) |
5256 | T.addConst(); |
5257 | Expr *E = new (*this) DeclRefExpr( |
5258 | *this, NTTP, /*RefersToEnclosingVariableOrCapture*/ false, T, |
5259 | Expr::getValueKindForType(T: NTTP->getType()), NTTP->getLocation()); |
5260 | |
5261 | if (NTTP->isParameterPack()) |
5262 | E = new (*this) |
5263 | PackExpansionExpr(DependentTy, E, NTTP->getLocation(), std::nullopt); |
5264 | Arg = TemplateArgument(E); |
5265 | } else { |
5266 | auto *TTP = cast<TemplateTemplateParmDecl>(Val: Param); |
5267 | if (TTP->isParameterPack()) |
5268 | Arg = TemplateArgument(TemplateName(TTP), std::optional<unsigned>()); |
5269 | else |
5270 | Arg = TemplateArgument(TemplateName(TTP)); |
5271 | } |
5272 | |
5273 | if (Param->isTemplateParameterPack()) |
5274 | Arg = TemplateArgument::CreatePackCopy(Context&: *this, Args: Arg); |
5275 | |
5276 | return Arg; |
5277 | } |
5278 | |
5279 | void |
5280 | ASTContext::getInjectedTemplateArgs(const TemplateParameterList *Params, |
5281 | SmallVectorImpl<TemplateArgument> &Args) { |
5282 | Args.reserve(N: Args.size() + Params->size()); |
5283 | |
5284 | for (NamedDecl *Param : *Params) |
5285 | Args.push_back(Elt: getInjectedTemplateArg(Param)); |
5286 | } |
5287 | |
5288 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
5289 | std::optional<unsigned> NumExpansions, |
5290 | bool ExpectPackInType) { |
5291 | assert((!ExpectPackInType || Pattern->containsUnexpandedParameterPack()) && |
5292 | "Pack expansions must expand one or more parameter packs" ); |
5293 | |
5294 | llvm::FoldingSetNodeID ID; |
5295 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
5296 | |
5297 | void *InsertPos = nullptr; |
5298 | PackExpansionType *T = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
5299 | if (T) |
5300 | return QualType(T, 0); |
5301 | |
5302 | QualType Canon; |
5303 | if (!Pattern.isCanonical()) { |
5304 | Canon = getPackExpansionType(Pattern: getCanonicalType(T: Pattern), NumExpansions, |
5305 | /*ExpectPackInType=*/false); |
5306 | |
5307 | // Find the insert position again, in case we inserted an element into |
5308 | // PackExpansionTypes and invalidated our insert position. |
5309 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
5310 | } |
5311 | |
5312 | T = new (*this, alignof(PackExpansionType)) |
5313 | PackExpansionType(Pattern, Canon, NumExpansions); |
5314 | Types.push_back(T); |
5315 | PackExpansionTypes.InsertNode(N: T, InsertPos); |
5316 | return QualType(T, 0); |
5317 | } |
5318 | |
5319 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
5320 | /// alphabetically. |
5321 | static int CmpProtocolNames(ObjCProtocolDecl *const *LHS, |
5322 | ObjCProtocolDecl *const *RHS) { |
5323 | return DeclarationName::compare(LHS: (*LHS)->getDeclName(), RHS: (*RHS)->getDeclName()); |
5324 | } |
5325 | |
5326 | static bool areSortedAndUniqued(ArrayRef<ObjCProtocolDecl *> Protocols) { |
5327 | if (Protocols.empty()) return true; |
5328 | |
5329 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
5330 | return false; |
5331 | |
5332 | for (unsigned i = 1; i != Protocols.size(); ++i) |
5333 | if (CmpProtocolNames(LHS: &Protocols[i - 1], RHS: &Protocols[i]) >= 0 || |
5334 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
5335 | return false; |
5336 | return true; |
5337 | } |
5338 | |
5339 | static void |
5340 | SortAndUniqueProtocols(SmallVectorImpl<ObjCProtocolDecl *> &Protocols) { |
5341 | // Sort protocols, keyed by name. |
5342 | llvm::array_pod_sort(Start: Protocols.begin(), End: Protocols.end(), Compare: CmpProtocolNames); |
5343 | |
5344 | // Canonicalize. |
5345 | for (ObjCProtocolDecl *&P : Protocols) |
5346 | P = P->getCanonicalDecl(); |
5347 | |
5348 | // Remove duplicates. |
5349 | auto ProtocolsEnd = std::unique(first: Protocols.begin(), last: Protocols.end()); |
5350 | Protocols.erase(CS: ProtocolsEnd, CE: Protocols.end()); |
5351 | } |
5352 | |
5353 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
5354 | ObjCProtocolDecl * const *Protocols, |
5355 | unsigned NumProtocols) const { |
5356 | return getObjCObjectType(Base: BaseType, typeArgs: {}, |
5357 | protocols: llvm::ArrayRef(Protocols, NumProtocols), |
5358 | /*isKindOf=*/false); |
5359 | } |
5360 | |
5361 | QualType ASTContext::getObjCObjectType( |
5362 | QualType baseType, |
5363 | ArrayRef<QualType> typeArgs, |
5364 | ArrayRef<ObjCProtocolDecl *> protocols, |
5365 | bool isKindOf) const { |
5366 | // If the base type is an interface and there aren't any protocols or |
5367 | // type arguments to add, then the interface type will do just fine. |
5368 | if (typeArgs.empty() && protocols.empty() && !isKindOf && |
5369 | isa<ObjCInterfaceType>(Val: baseType)) |
5370 | return baseType; |
5371 | |
5372 | // Look in the folding set for an existing type. |
5373 | llvm::FoldingSetNodeID ID; |
5374 | ObjCObjectTypeImpl::Profile(ID, Base: baseType, typeArgs, protocols, isKindOf); |
5375 | void *InsertPos = nullptr; |
5376 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5377 | return QualType(QT, 0); |
5378 | |
5379 | // Determine the type arguments to be used for canonicalization, |
5380 | // which may be explicitly specified here or written on the base |
5381 | // type. |
5382 | ArrayRef<QualType> effectiveTypeArgs = typeArgs; |
5383 | if (effectiveTypeArgs.empty()) { |
5384 | if (const auto *baseObject = baseType->getAs<ObjCObjectType>()) |
5385 | effectiveTypeArgs = baseObject->getTypeArgs(); |
5386 | } |
5387 | |
5388 | // Build the canonical type, which has the canonical base type and a |
5389 | // sorted-and-uniqued list of protocols and the type arguments |
5390 | // canonicalized. |
5391 | QualType canonical; |
5392 | bool typeArgsAreCanonical = llvm::all_of( |
5393 | Range&: effectiveTypeArgs, P: [&](QualType type) { return type.isCanonical(); }); |
5394 | bool protocolsSorted = areSortedAndUniqued(Protocols: protocols); |
5395 | if (!typeArgsAreCanonical || !protocolsSorted || !baseType.isCanonical()) { |
5396 | // Determine the canonical type arguments. |
5397 | ArrayRef<QualType> canonTypeArgs; |
5398 | SmallVector<QualType, 4> canonTypeArgsVec; |
5399 | if (!typeArgsAreCanonical) { |
5400 | canonTypeArgsVec.reserve(N: effectiveTypeArgs.size()); |
5401 | for (auto typeArg : effectiveTypeArgs) |
5402 | canonTypeArgsVec.push_back(Elt: getCanonicalType(T: typeArg)); |
5403 | canonTypeArgs = canonTypeArgsVec; |
5404 | } else { |
5405 | canonTypeArgs = effectiveTypeArgs; |
5406 | } |
5407 | |
5408 | ArrayRef<ObjCProtocolDecl *> canonProtocols; |
5409 | SmallVector<ObjCProtocolDecl*, 8> canonProtocolsVec; |
5410 | if (!protocolsSorted) { |
5411 | canonProtocolsVec.append(in_start: protocols.begin(), in_end: protocols.end()); |
5412 | SortAndUniqueProtocols(Protocols&: canonProtocolsVec); |
5413 | canonProtocols = canonProtocolsVec; |
5414 | } else { |
5415 | canonProtocols = protocols; |
5416 | } |
5417 | |
5418 | canonical = getObjCObjectType(baseType: getCanonicalType(T: baseType), typeArgs: canonTypeArgs, |
5419 | protocols: canonProtocols, isKindOf); |
5420 | |
5421 | // Regenerate InsertPos. |
5422 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
5423 | } |
5424 | |
5425 | unsigned size = sizeof(ObjCObjectTypeImpl); |
5426 | size += typeArgs.size() * sizeof(QualType); |
5427 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
5428 | void *mem = Allocate(Size: size, Align: alignof(ObjCObjectTypeImpl)); |
5429 | auto *T = |
5430 | new (mem) ObjCObjectTypeImpl(canonical, baseType, typeArgs, protocols, |
5431 | isKindOf); |
5432 | |
5433 | Types.push_back(T); |
5434 | ObjCObjectTypes.InsertNode(N: T, InsertPos); |
5435 | return QualType(T, 0); |
5436 | } |
5437 | |
5438 | /// Apply Objective-C protocol qualifiers to the given type. |
5439 | /// If this is for the canonical type of a type parameter, we can apply |
5440 | /// protocol qualifiers on the ObjCObjectPointerType. |
5441 | QualType |
5442 | ASTContext::applyObjCProtocolQualifiers(QualType type, |
5443 | ArrayRef<ObjCProtocolDecl *> protocols, bool &hasError, |
5444 | bool allowOnPointerType) const { |
5445 | hasError = false; |
5446 | |
5447 | if (const auto *objT = dyn_cast<ObjCTypeParamType>(Val: type.getTypePtr())) { |
5448 | return getObjCTypeParamType(Decl: objT->getDecl(), protocols); |
5449 | } |
5450 | |
5451 | // Apply protocol qualifiers to ObjCObjectPointerType. |
5452 | if (allowOnPointerType) { |
5453 | if (const auto *objPtr = |
5454 | dyn_cast<ObjCObjectPointerType>(Val: type.getTypePtr())) { |
5455 | const ObjCObjectType *objT = objPtr->getObjectType(); |
5456 | // Merge protocol lists and construct ObjCObjectType. |
5457 | SmallVector<ObjCProtocolDecl*, 8> protocolsVec; |
5458 | protocolsVec.append(objT->qual_begin(), |
5459 | objT->qual_end()); |
5460 | protocolsVec.append(in_start: protocols.begin(), in_end: protocols.end()); |
5461 | ArrayRef<ObjCProtocolDecl *> protocols = protocolsVec; |
5462 | type = getObjCObjectType( |
5463 | baseType: objT->getBaseType(), |
5464 | typeArgs: objT->getTypeArgsAsWritten(), |
5465 | protocols, |
5466 | isKindOf: objT->isKindOfTypeAsWritten()); |
5467 | return getObjCObjectPointerType(OIT: type); |
5468 | } |
5469 | } |
5470 | |
5471 | // Apply protocol qualifiers to ObjCObjectType. |
5472 | if (const auto *objT = dyn_cast<ObjCObjectType>(Val: type.getTypePtr())){ |
5473 | // FIXME: Check for protocols to which the class type is already |
5474 | // known to conform. |
5475 | |
5476 | return getObjCObjectType(baseType: objT->getBaseType(), |
5477 | typeArgs: objT->getTypeArgsAsWritten(), |
5478 | protocols, |
5479 | isKindOf: objT->isKindOfTypeAsWritten()); |
5480 | } |
5481 | |
5482 | // If the canonical type is ObjCObjectType, ... |
5483 | if (type->isObjCObjectType()) { |
5484 | // Silently overwrite any existing protocol qualifiers. |
5485 | // TODO: determine whether that's the right thing to do. |
5486 | |
5487 | // FIXME: Check for protocols to which the class type is already |
5488 | // known to conform. |
5489 | return getObjCObjectType(baseType: type, typeArgs: {}, protocols, isKindOf: false); |
5490 | } |
5491 | |
5492 | // id<protocol-list> |
5493 | if (type->isObjCIdType()) { |
5494 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); |
5495 | type = getObjCObjectType(ObjCBuiltinIdTy, {}, protocols, |
5496 | objPtr->isKindOfType()); |
5497 | return getObjCObjectPointerType(OIT: type); |
5498 | } |
5499 | |
5500 | // Class<protocol-list> |
5501 | if (type->isObjCClassType()) { |
5502 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); |
5503 | type = getObjCObjectType(ObjCBuiltinClassTy, {}, protocols, |
5504 | objPtr->isKindOfType()); |
5505 | return getObjCObjectPointerType(OIT: type); |
5506 | } |
5507 | |
5508 | hasError = true; |
5509 | return type; |
5510 | } |
5511 | |
5512 | QualType |
5513 | ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl, |
5514 | ArrayRef<ObjCProtocolDecl *> protocols) const { |
5515 | // Look in the folding set for an existing type. |
5516 | llvm::FoldingSetNodeID ID; |
5517 | ObjCTypeParamType::Profile(ID, Decl, Decl->getUnderlyingType(), protocols); |
5518 | void *InsertPos = nullptr; |
5519 | if (ObjCTypeParamType *TypeParam = |
5520 | ObjCTypeParamTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5521 | return QualType(TypeParam, 0); |
5522 | |
5523 | // We canonicalize to the underlying type. |
5524 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
5525 | if (!protocols.empty()) { |
5526 | // Apply the protocol qualifers. |
5527 | bool hasError; |
5528 | Canonical = getCanonicalType(T: applyObjCProtocolQualifiers( |
5529 | type: Canonical, protocols, hasError, allowOnPointerType: true /*allowOnPointerType*/)); |
5530 | assert(!hasError && "Error when apply protocol qualifier to bound type" ); |
5531 | } |
5532 | |
5533 | unsigned size = sizeof(ObjCTypeParamType); |
5534 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
5535 | void *mem = Allocate(Size: size, Align: alignof(ObjCTypeParamType)); |
5536 | auto *newType = new (mem) ObjCTypeParamType(Decl, Canonical, protocols); |
5537 | |
5538 | Types.push_back(Elt: newType); |
5539 | ObjCTypeParamTypes.InsertNode(newType, InsertPos); |
5540 | return QualType(newType, 0); |
5541 | } |
5542 | |
5543 | void ASTContext::adjustObjCTypeParamBoundType(const ObjCTypeParamDecl *Orig, |
5544 | ObjCTypeParamDecl *New) const { |
5545 | New->setTypeSourceInfo(getTrivialTypeSourceInfo(T: Orig->getUnderlyingType())); |
5546 | // Update TypeForDecl after updating TypeSourceInfo. |
5547 | auto NewTypeParamTy = cast<ObjCTypeParamType>(New->getTypeForDecl()); |
5548 | SmallVector<ObjCProtocolDecl *, 8> protocols; |
5549 | protocols.append(NewTypeParamTy->qual_begin(), NewTypeParamTy->qual_end()); |
5550 | QualType UpdatedTy = getObjCTypeParamType(Decl: New, protocols); |
5551 | New->setTypeForDecl(UpdatedTy.getTypePtr()); |
5552 | } |
5553 | |
5554 | /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's |
5555 | /// protocol list adopt all protocols in QT's qualified-id protocol |
5556 | /// list. |
5557 | bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT, |
5558 | ObjCInterfaceDecl *IC) { |
5559 | if (!QT->isObjCQualifiedIdType()) |
5560 | return false; |
5561 | |
5562 | if (const auto *OPT = QT->getAs<ObjCObjectPointerType>()) { |
5563 | // If both the right and left sides have qualifiers. |
5564 | for (auto *Proto : OPT->quals()) { |
5565 | if (!IC->ClassImplementsProtocol(Proto, false)) |
5566 | return false; |
5567 | } |
5568 | return true; |
5569 | } |
5570 | return false; |
5571 | } |
5572 | |
5573 | /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in |
5574 | /// QT's qualified-id protocol list adopt all protocols in IDecl's list |
5575 | /// of protocols. |
5576 | bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT, |
5577 | ObjCInterfaceDecl *IDecl) { |
5578 | if (!QT->isObjCQualifiedIdType()) |
5579 | return false; |
5580 | const auto *OPT = QT->getAs<ObjCObjectPointerType>(); |
5581 | if (!OPT) |
5582 | return false; |
5583 | if (!IDecl->hasDefinition()) |
5584 | return false; |
5585 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols; |
5586 | CollectInheritedProtocols(IDecl, InheritedProtocols); |
5587 | if (InheritedProtocols.empty()) |
5588 | return false; |
5589 | // Check that if every protocol in list of id<plist> conforms to a protocol |
5590 | // of IDecl's, then bridge casting is ok. |
5591 | bool Conforms = false; |
5592 | for (auto *Proto : OPT->quals()) { |
5593 | Conforms = false; |
5594 | for (auto *PI : InheritedProtocols) { |
5595 | if (ProtocolCompatibleWithProtocol(Proto, PI)) { |
5596 | Conforms = true; |
5597 | break; |
5598 | } |
5599 | } |
5600 | if (!Conforms) |
5601 | break; |
5602 | } |
5603 | if (Conforms) |
5604 | return true; |
5605 | |
5606 | for (auto *PI : InheritedProtocols) { |
5607 | // If both the right and left sides have qualifiers. |
5608 | bool Adopts = false; |
5609 | for (auto *Proto : OPT->quals()) { |
5610 | // return 'true' if 'PI' is in the inheritance hierarchy of Proto |
5611 | if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto))) |
5612 | break; |
5613 | } |
5614 | if (!Adopts) |
5615 | return false; |
5616 | } |
5617 | return true; |
5618 | } |
5619 | |
5620 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
5621 | /// the given object type. |
5622 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
5623 | llvm::FoldingSetNodeID ID; |
5624 | ObjCObjectPointerType::Profile(ID, T: ObjectT); |
5625 | |
5626 | void *InsertPos = nullptr; |
5627 | if (ObjCObjectPointerType *QT = |
5628 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5629 | return QualType(QT, 0); |
5630 | |
5631 | // Find the canonical object type. |
5632 | QualType Canonical; |
5633 | if (!ObjectT.isCanonical()) { |
5634 | Canonical = getObjCObjectPointerType(ObjectT: getCanonicalType(T: ObjectT)); |
5635 | |
5636 | // Regenerate InsertPos. |
5637 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
5638 | } |
5639 | |
5640 | // No match. |
5641 | void *Mem = |
5642 | Allocate(Size: sizeof(ObjCObjectPointerType), Align: alignof(ObjCObjectPointerType)); |
5643 | auto *QType = |
5644 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
5645 | |
5646 | Types.push_back(QType); |
5647 | ObjCObjectPointerTypes.InsertNode(N: QType, InsertPos); |
5648 | return QualType(QType, 0); |
5649 | } |
5650 | |
5651 | /// getObjCInterfaceType - Return the unique reference to the type for the |
5652 | /// specified ObjC interface decl. The list of protocols is optional. |
5653 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
5654 | ObjCInterfaceDecl *PrevDecl) const { |
5655 | if (Decl->TypeForDecl) |
5656 | return QualType(Decl->TypeForDecl, 0); |
5657 | |
5658 | if (PrevDecl) { |
5659 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl" ); |
5660 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
5661 | return QualType(PrevDecl->TypeForDecl, 0); |
5662 | } |
5663 | |
5664 | // Prefer the definition, if there is one. |
5665 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
5666 | Decl = Def; |
5667 | |
5668 | void *Mem = Allocate(Size: sizeof(ObjCInterfaceType), Align: alignof(ObjCInterfaceType)); |
5669 | auto *T = new (Mem) ObjCInterfaceType(Decl); |
5670 | Decl->TypeForDecl = T; |
5671 | Types.push_back(T); |
5672 | return QualType(T, 0); |
5673 | } |
5674 | |
5675 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
5676 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
5677 | /// multiple declarations that refer to "typeof(x)" all contain different |
5678 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
5679 | /// on canonical type's (which are always unique). |
5680 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr, TypeOfKind Kind) const { |
5681 | TypeOfExprType *toe; |
5682 | if (tofExpr->isTypeDependent()) { |
5683 | llvm::FoldingSetNodeID ID; |
5684 | DependentTypeOfExprType::Profile(ID, Context: *this, E: tofExpr, |
5685 | IsUnqual: Kind == TypeOfKind::Unqualified); |
5686 | |
5687 | void *InsertPos = nullptr; |
5688 | DependentTypeOfExprType *Canon = |
5689 | DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
5690 | if (Canon) { |
5691 | // We already have a "canonical" version of an identical, dependent |
5692 | // typeof(expr) type. Use that as our canonical type. |
5693 | toe = new (*this, alignof(TypeOfExprType)) |
5694 | TypeOfExprType(tofExpr, Kind, QualType((TypeOfExprType *)Canon, 0)); |
5695 | } else { |
5696 | // Build a new, canonical typeof(expr) type. |
5697 | Canon = new (*this, alignof(DependentTypeOfExprType)) |
5698 | DependentTypeOfExprType(tofExpr, Kind); |
5699 | DependentTypeOfExprTypes.InsertNode(N: Canon, InsertPos); |
5700 | toe = Canon; |
5701 | } |
5702 | } else { |
5703 | QualType Canonical = getCanonicalType(T: tofExpr->getType()); |
5704 | toe = new (*this, alignof(TypeOfExprType)) |
5705 | TypeOfExprType(tofExpr, Kind, Canonical); |
5706 | } |
5707 | Types.push_back(toe); |
5708 | return QualType(toe, 0); |
5709 | } |
5710 | |
5711 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
5712 | /// TypeOfType nodes. The only motivation to unique these nodes would be |
5713 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
5714 | /// an issue. This doesn't affect the type checker, since it operates |
5715 | /// on canonical types (which are always unique). |
5716 | QualType ASTContext::getTypeOfType(QualType tofType, TypeOfKind Kind) const { |
5717 | QualType Canonical = getCanonicalType(T: tofType); |
5718 | auto *tot = |
5719 | new (*this, alignof(TypeOfType)) TypeOfType(tofType, Canonical, Kind); |
5720 | Types.push_back(tot); |
5721 | return QualType(tot, 0); |
5722 | } |
5723 | |
5724 | /// getReferenceQualifiedType - Given an expr, will return the type for |
5725 | /// that expression, as in [dcl.type.simple]p4 but without taking id-expressions |
5726 | /// and class member access into account. |
5727 | QualType ASTContext::getReferenceQualifiedType(const Expr *E) const { |
5728 | // C++11 [dcl.type.simple]p4: |
5729 | // [...] |
5730 | QualType T = E->getType(); |
5731 | switch (E->getValueKind()) { |
5732 | // - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the |
5733 | // type of e; |
5734 | case VK_XValue: |
5735 | return getRValueReferenceType(T); |
5736 | // - otherwise, if e is an lvalue, decltype(e) is T&, where T is the |
5737 | // type of e; |
5738 | case VK_LValue: |
5739 | return getLValueReferenceType(T); |
5740 | // - otherwise, decltype(e) is the type of e. |
5741 | case VK_PRValue: |
5742 | return T; |
5743 | } |
5744 | llvm_unreachable("Unknown value kind" ); |
5745 | } |
5746 | |
5747 | /// Unlike many "get<Type>" functions, we don't unique DecltypeType |
5748 | /// nodes. This would never be helpful, since each such type has its own |
5749 | /// expression, and would not give a significant memory saving, since there |
5750 | /// is an Expr tree under each such type. |
5751 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
5752 | DecltypeType *dt; |
5753 | |
5754 | // C++11 [temp.type]p2: |
5755 | // If an expression e involves a template parameter, decltype(e) denotes a |
5756 | // unique dependent type. Two such decltype-specifiers refer to the same |
5757 | // type only if their expressions are equivalent (14.5.6.1). |
5758 | if (e->isInstantiationDependent()) { |
5759 | llvm::FoldingSetNodeID ID; |
5760 | DependentDecltypeType::Profile(ID, Context: *this, E: e); |
5761 | |
5762 | void *InsertPos = nullptr; |
5763 | DependentDecltypeType *Canon |
5764 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
5765 | if (!Canon) { |
5766 | // Build a new, canonical decltype(expr) type. |
5767 | Canon = new (*this, alignof(DependentDecltypeType)) |
5768 | DependentDecltypeType(e, DependentTy); |
5769 | DependentDecltypeTypes.InsertNode(N: Canon, InsertPos); |
5770 | } |
5771 | dt = new (*this, alignof(DecltypeType)) |
5772 | DecltypeType(e, UnderlyingType, QualType((DecltypeType *)Canon, 0)); |
5773 | } else { |
5774 | dt = new (*this, alignof(DecltypeType)) |
5775 | DecltypeType(e, UnderlyingType, getCanonicalType(T: UnderlyingType)); |
5776 | } |
5777 | Types.push_back(dt); |
5778 | return QualType(dt, 0); |
5779 | } |
5780 | |
5781 | QualType ASTContext::getPackIndexingType(QualType Pattern, Expr *IndexExpr, |
5782 | bool FullySubstituted, |
5783 | ArrayRef<QualType> Expansions, |
5784 | int Index) const { |
5785 | QualType Canonical; |
5786 | if (FullySubstituted && Index != -1) { |
5787 | Canonical = getCanonicalType(T: Expansions[Index]); |
5788 | } else { |
5789 | llvm::FoldingSetNodeID ID; |
5790 | PackIndexingType::Profile(ID, Context: *this, Pattern, E: IndexExpr); |
5791 | void *InsertPos = nullptr; |
5792 | PackIndexingType *Canon = |
5793 | DependentPackIndexingTypes.FindNodeOrInsertPos(ID, InsertPos); |
5794 | if (!Canon) { |
5795 | void *Mem = Allocate( |
5796 | PackIndexingType::totalSizeToAlloc<QualType>(Expansions.size()), |
5797 | TypeAlignment); |
5798 | Canon = new (Mem) |
5799 | PackIndexingType(*this, QualType(), Pattern, IndexExpr, Expansions); |
5800 | DependentPackIndexingTypes.InsertNode(N: Canon, InsertPos); |
5801 | } |
5802 | Canonical = QualType(Canon, 0); |
5803 | } |
5804 | |
5805 | void *Mem = |
5806 | Allocate(PackIndexingType::totalSizeToAlloc<QualType>(Expansions.size()), |
5807 | TypeAlignment); |
5808 | auto *T = new (Mem) |
5809 | PackIndexingType(*this, Canonical, Pattern, IndexExpr, Expansions); |
5810 | Types.push_back(T); |
5811 | return QualType(T, 0); |
5812 | } |
5813 | |
5814 | /// getUnaryTransformationType - We don't unique these, since the memory |
5815 | /// savings are minimal and these are rare. |
5816 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
5817 | QualType UnderlyingType, |
5818 | UnaryTransformType::UTTKind Kind) |
5819 | const { |
5820 | UnaryTransformType *ut = nullptr; |
5821 | |
5822 | if (BaseType->isDependentType()) { |
5823 | // Look in the folding set for an existing type. |
5824 | llvm::FoldingSetNodeID ID; |
5825 | DependentUnaryTransformType::Profile(ID, BaseType: getCanonicalType(T: BaseType), UKind: Kind); |
5826 | |
5827 | void *InsertPos = nullptr; |
5828 | DependentUnaryTransformType *Canon |
5829 | = DependentUnaryTransformTypes.FindNodeOrInsertPos(ID, InsertPos); |
5830 | |
5831 | if (!Canon) { |
5832 | // Build a new, canonical __underlying_type(type) type. |
5833 | Canon = new (*this, alignof(DependentUnaryTransformType)) |
5834 | DependentUnaryTransformType(*this, getCanonicalType(T: BaseType), Kind); |
5835 | DependentUnaryTransformTypes.InsertNode(N: Canon, InsertPos); |
5836 | } |
5837 | ut = new (*this, alignof(UnaryTransformType)) |
5838 | UnaryTransformType(BaseType, QualType(), Kind, QualType(Canon, 0)); |
5839 | } else { |
5840 | QualType CanonType = getCanonicalType(T: UnderlyingType); |
5841 | ut = new (*this, alignof(UnaryTransformType)) |
5842 | UnaryTransformType(BaseType, UnderlyingType, Kind, CanonType); |
5843 | } |
5844 | Types.push_back(ut); |
5845 | return QualType(ut, 0); |
5846 | } |
5847 | |
5848 | QualType ASTContext::getAutoTypeInternal( |
5849 | QualType DeducedType, AutoTypeKeyword Keyword, bool IsDependent, |
5850 | bool IsPack, ConceptDecl *TypeConstraintConcept, |
5851 | ArrayRef<TemplateArgument> TypeConstraintArgs, bool IsCanon) const { |
5852 | if (DeducedType.isNull() && Keyword == AutoTypeKeyword::Auto && |
5853 | !TypeConstraintConcept && !IsDependent) |
5854 | return getAutoDeductType(); |
5855 | |
5856 | // Look in the folding set for an existing type. |
5857 | void *InsertPos = nullptr; |
5858 | llvm::FoldingSetNodeID ID; |
5859 | AutoType::Profile(ID, Context: *this, Deduced: DeducedType, Keyword, IsDependent, |
5860 | CD: TypeConstraintConcept, Arguments: TypeConstraintArgs); |
5861 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5862 | return QualType(AT, 0); |
5863 | |
5864 | QualType Canon; |
5865 | if (!IsCanon) { |
5866 | if (!DeducedType.isNull()) { |
5867 | Canon = DeducedType.getCanonicalType(); |
5868 | } else if (TypeConstraintConcept) { |
5869 | bool AnyNonCanonArgs = false; |
5870 | ConceptDecl *CanonicalConcept = TypeConstraintConcept->getCanonicalDecl(); |
5871 | auto CanonicalConceptArgs = ::getCanonicalTemplateArguments( |
5872 | C: *this, Args: TypeConstraintArgs, AnyNonCanonArgs); |
5873 | if (CanonicalConcept != TypeConstraintConcept || AnyNonCanonArgs) { |
5874 | Canon = |
5875 | getAutoTypeInternal(DeducedType: QualType(), Keyword, IsDependent, IsPack, |
5876 | TypeConstraintConcept: CanonicalConcept, TypeConstraintArgs: CanonicalConceptArgs, IsCanon: true); |
5877 | // Find the insert position again. |
5878 | [[maybe_unused]] auto *Nothing = |
5879 | AutoTypes.FindNodeOrInsertPos(ID, InsertPos); |
5880 | assert(!Nothing && "canonical type broken" ); |
5881 | } |
5882 | } |
5883 | } |
5884 | |
5885 | void *Mem = Allocate(Size: sizeof(AutoType) + |
5886 | sizeof(TemplateArgument) * TypeConstraintArgs.size(), |
5887 | Align: alignof(AutoType)); |
5888 | auto *AT = new (Mem) AutoType( |
5889 | DeducedType, Keyword, |
5890 | (IsDependent ? TypeDependence::DependentInstantiation |
5891 | : TypeDependence::None) | |
5892 | (IsPack ? TypeDependence::UnexpandedPack : TypeDependence::None), |
5893 | Canon, TypeConstraintConcept, TypeConstraintArgs); |
5894 | Types.push_back(Elt: AT); |
5895 | AutoTypes.InsertNode(AT, InsertPos); |
5896 | return QualType(AT, 0); |
5897 | } |
5898 | |
5899 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
5900 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
5901 | /// canonical deduced-but-dependent 'auto' type. |
5902 | QualType |
5903 | ASTContext::getAutoType(QualType DeducedType, AutoTypeKeyword Keyword, |
5904 | bool IsDependent, bool IsPack, |
5905 | ConceptDecl *TypeConstraintConcept, |
5906 | ArrayRef<TemplateArgument> TypeConstraintArgs) const { |
5907 | assert((!IsPack || IsDependent) && "only use IsPack for a dependent pack" ); |
5908 | assert((!IsDependent || DeducedType.isNull()) && |
5909 | "A dependent auto should be undeduced" ); |
5910 | return getAutoTypeInternal(DeducedType, Keyword, IsDependent, IsPack, |
5911 | TypeConstraintConcept, TypeConstraintArgs); |
5912 | } |
5913 | |
5914 | QualType ASTContext::getUnconstrainedType(QualType T) const { |
5915 | QualType CanonT = T.getCanonicalType(); |
5916 | |
5917 | // Remove a type-constraint from a top-level auto or decltype(auto). |
5918 | if (auto *AT = CanonT->getAs<AutoType>()) { |
5919 | if (!AT->isConstrained()) |
5920 | return T; |
5921 | return getQualifiedType(getAutoType(DeducedType: QualType(), Keyword: AT->getKeyword(), IsDependent: false, |
5922 | IsPack: AT->containsUnexpandedParameterPack()), |
5923 | T.getQualifiers()); |
5924 | } |
5925 | |
5926 | // FIXME: We only support constrained auto at the top level in the type of a |
5927 | // non-type template parameter at the moment. Once we lift that restriction, |
5928 | // we'll need to recursively build types containing auto here. |
5929 | assert(!CanonT->getContainedAutoType() || |
5930 | !CanonT->getContainedAutoType()->isConstrained()); |
5931 | return T; |
5932 | } |
5933 | |
5934 | /// Return the uniqued reference to the deduced template specialization type |
5935 | /// which has been deduced to the given type, or to the canonical undeduced |
5936 | /// such type, or the canonical deduced-but-dependent such type. |
5937 | QualType ASTContext::getDeducedTemplateSpecializationType( |
5938 | TemplateName Template, QualType DeducedType, bool IsDependent) const { |
5939 | // Look in the folding set for an existing type. |
5940 | void *InsertPos = nullptr; |
5941 | llvm::FoldingSetNodeID ID; |
5942 | DeducedTemplateSpecializationType::Profile(ID, Template, Deduced: DeducedType, |
5943 | IsDependent); |
5944 | if (DeducedTemplateSpecializationType *DTST = |
5945 | DeducedTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5946 | return QualType(DTST, 0); |
5947 | |
5948 | auto *DTST = new (*this, alignof(DeducedTemplateSpecializationType)) |
5949 | DeducedTemplateSpecializationType(Template, DeducedType, IsDependent); |
5950 | llvm::FoldingSetNodeID TempID; |
5951 | DTST->Profile(ID&: TempID); |
5952 | assert(ID == TempID && "ID does not match" ); |
5953 | Types.push_back(DTST); |
5954 | DeducedTemplateSpecializationTypes.InsertNode(N: DTST, InsertPos); |
5955 | return QualType(DTST, 0); |
5956 | } |
5957 | |
5958 | /// getAtomicType - Return the uniqued reference to the atomic type for |
5959 | /// the given value type. |
5960 | QualType ASTContext::getAtomicType(QualType T) const { |
5961 | // Unique pointers, to guarantee there is only one pointer of a particular |
5962 | // structure. |
5963 | llvm::FoldingSetNodeID ID; |
5964 | AtomicType::Profile(ID, T); |
5965 | |
5966 | void *InsertPos = nullptr; |
5967 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
5968 | return QualType(AT, 0); |
5969 | |
5970 | // If the atomic value type isn't canonical, this won't be a canonical type |
5971 | // either, so fill in the canonical type field. |
5972 | QualType Canonical; |
5973 | if (!T.isCanonical()) { |
5974 | Canonical = getAtomicType(T: getCanonicalType(T)); |
5975 | |
5976 | // Get the new insert position for the node we care about. |
5977 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
5978 | assert(!NewIP && "Shouldn't be in the map!" ); (void)NewIP; |
5979 | } |
5980 | auto *New = new (*this, alignof(AtomicType)) AtomicType(T, Canonical); |
5981 | Types.push_back(New); |
5982 | AtomicTypes.InsertNode(N: New, InsertPos); |
5983 | return QualType(New, 0); |
5984 | } |
5985 | |
5986 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
5987 | QualType ASTContext::getAutoDeductType() const { |
5988 | if (AutoDeductTy.isNull()) |
5989 | AutoDeductTy = QualType(new (*this, alignof(AutoType)) |
5990 | AutoType(QualType(), AutoTypeKeyword::Auto, |
5991 | TypeDependence::None, QualType(), |
5992 | /*concept*/ nullptr, /*args*/ {}), |
5993 | 0); |
5994 | return AutoDeductTy; |
5995 | } |
5996 | |
5997 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
5998 | QualType ASTContext::getAutoRRefDeductType() const { |
5999 | if (AutoRRefDeductTy.isNull()) |
6000 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
6001 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern" ); |
6002 | return AutoRRefDeductTy; |
6003 | } |
6004 | |
6005 | /// getTagDeclType - Return the unique reference to the type for the |
6006 | /// specified TagDecl (struct/union/class/enum) decl. |
6007 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
6008 | assert(Decl); |
6009 | // FIXME: What is the design on getTagDeclType when it requires casting |
6010 | // away const? mutable? |
6011 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
6012 | } |
6013 | |
6014 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
6015 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
6016 | /// needs to agree with the definition in <stddef.h>. |
6017 | CanQualType ASTContext::getSizeType() const { |
6018 | return getFromTargetType(Type: Target->getSizeType()); |
6019 | } |
6020 | |
6021 | /// Return the unique signed counterpart of the integer type |
6022 | /// corresponding to size_t. |
6023 | CanQualType ASTContext::getSignedSizeType() const { |
6024 | return getFromTargetType(Type: Target->getSignedSizeType()); |
6025 | } |
6026 | |
6027 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
6028 | CanQualType ASTContext::getIntMaxType() const { |
6029 | return getFromTargetType(Type: Target->getIntMaxType()); |
6030 | } |
6031 | |
6032 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
6033 | CanQualType ASTContext::getUIntMaxType() const { |
6034 | return getFromTargetType(Type: Target->getUIntMaxType()); |
6035 | } |
6036 | |
6037 | /// getSignedWCharType - Return the type of "signed wchar_t". |
6038 | /// Used when in C++, as a GCC extension. |
6039 | QualType ASTContext::getSignedWCharType() const { |
6040 | // FIXME: derive from "Target" ? |
6041 | return WCharTy; |
6042 | } |
6043 | |
6044 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
6045 | /// Used when in C++, as a GCC extension. |
6046 | QualType ASTContext::getUnsignedWCharType() const { |
6047 | // FIXME: derive from "Target" ? |
6048 | return UnsignedIntTy; |
6049 | } |
6050 | |
6051 | QualType ASTContext::getIntPtrType() const { |
6052 | return getFromTargetType(Type: Target->getIntPtrType()); |
6053 | } |
6054 | |
6055 | QualType ASTContext::getUIntPtrType() const { |
6056 | return getCorrespondingUnsignedType(T: getIntPtrType()); |
6057 | } |
6058 | |
6059 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
6060 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
6061 | QualType ASTContext::getPointerDiffType() const { |
6062 | return getFromTargetType(Type: Target->getPtrDiffType(AddrSpace: LangAS::Default)); |
6063 | } |
6064 | |
6065 | /// Return the unique unsigned counterpart of "ptrdiff_t" |
6066 | /// integer type. The standard (C11 7.21.6.1p7) refers to this type |
6067 | /// in the definition of %tu format specifier. |
6068 | QualType ASTContext::getUnsignedPointerDiffType() const { |
6069 | return getFromTargetType(Type: Target->getUnsignedPtrDiffType(AddrSpace: LangAS::Default)); |
6070 | } |
6071 | |
6072 | /// Return the unique type for "pid_t" defined in |
6073 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
6074 | QualType ASTContext::getProcessIDType() const { |
6075 | return getFromTargetType(Type: Target->getProcessIDType()); |
6076 | } |
6077 | |
6078 | //===----------------------------------------------------------------------===// |
6079 | // Type Operators |
6080 | //===----------------------------------------------------------------------===// |
6081 | |
6082 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
6083 | // Push qualifiers into arrays, and then discard any remaining |
6084 | // qualifiers. |
6085 | T = getCanonicalType(T); |
6086 | T = getVariableArrayDecayedType(type: T); |
6087 | const Type *Ty = T.getTypePtr(); |
6088 | QualType Result; |
6089 | if (getLangOpts().HLSL && isa<ConstantArrayType>(Val: Ty)) { |
6090 | Result = getArrayParameterType(Ty: QualType(Ty, 0)); |
6091 | } else if (isa<ArrayType>(Val: Ty)) { |
6092 | Result = getArrayDecayedType(T: QualType(Ty,0)); |
6093 | } else if (isa<FunctionType>(Val: Ty)) { |
6094 | Result = getPointerType(T: QualType(Ty, 0)); |
6095 | } else { |
6096 | Result = QualType(Ty, 0); |
6097 | } |
6098 | |
6099 | return CanQualType::CreateUnsafe(Other: Result); |
6100 | } |
6101 | |
6102 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
6103 | Qualifiers &quals) { |
6104 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
6105 | |
6106 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
6107 | // the unqualified desugared type and then drops it on the floor. |
6108 | // We then have to strip that sugar back off with |
6109 | // getUnqualifiedDesugaredType(), which is silly. |
6110 | const auto *AT = |
6111 | dyn_cast<ArrayType>(Val: splitType.Ty->getUnqualifiedDesugaredType()); |
6112 | |
6113 | // If we don't have an array, just use the results in splitType. |
6114 | if (!AT) { |
6115 | quals = splitType.Quals; |
6116 | return QualType(splitType.Ty, 0); |
6117 | } |
6118 | |
6119 | // Otherwise, recurse on the array's element type. |
6120 | QualType elementType = AT->getElementType(); |
6121 | QualType unqualElementType = getUnqualifiedArrayType(type: elementType, quals); |
6122 | |
6123 | // If that didn't change the element type, AT has no qualifiers, so we |
6124 | // can just use the results in splitType. |
6125 | if (elementType == unqualElementType) { |
6126 | assert(quals.empty()); // from the recursive call |
6127 | quals = splitType.Quals; |
6128 | return QualType(splitType.Ty, 0); |
6129 | } |
6130 | |
6131 | // Otherwise, add in the qualifiers from the outermost type, then |
6132 | // build the type back up. |
6133 | quals.addConsistentQualifiers(qs: splitType.Quals); |
6134 | |
6135 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) { |
6136 | return getConstantArrayType(EltTy: unqualElementType, ArySizeIn: CAT->getSize(), |
6137 | SizeExpr: CAT->getSizeExpr(), ASM: CAT->getSizeModifier(), IndexTypeQuals: 0); |
6138 | } |
6139 | |
6140 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(Val: AT)) { |
6141 | return getIncompleteArrayType(elementType: unqualElementType, ASM: IAT->getSizeModifier(), elementTypeQuals: 0); |
6142 | } |
6143 | |
6144 | if (const auto *VAT = dyn_cast<VariableArrayType>(Val: AT)) { |
6145 | return getVariableArrayType(EltTy: unqualElementType, |
6146 | NumElts: VAT->getSizeExpr(), |
6147 | ASM: VAT->getSizeModifier(), |
6148 | IndexTypeQuals: VAT->getIndexTypeCVRQualifiers(), |
6149 | Brackets: VAT->getBracketsRange()); |
6150 | } |
6151 | |
6152 | const auto *DSAT = cast<DependentSizedArrayType>(Val: AT); |
6153 | return getDependentSizedArrayType(elementType: unqualElementType, numElements: DSAT->getSizeExpr(), |
6154 | ASM: DSAT->getSizeModifier(), elementTypeQuals: 0, |
6155 | brackets: SourceRange()); |
6156 | } |
6157 | |
6158 | /// Attempt to unwrap two types that may both be array types with the same bound |
6159 | /// (or both be array types of unknown bound) for the purpose of comparing the |
6160 | /// cv-decomposition of two types per C++ [conv.qual]. |
6161 | /// |
6162 | /// \param AllowPiMismatch Allow the Pi1 and Pi2 to differ as described in |
6163 | /// C++20 [conv.qual], if permitted by the current language mode. |
6164 | void ASTContext::UnwrapSimilarArrayTypes(QualType &T1, QualType &T2, |
6165 | bool AllowPiMismatch) { |
6166 | while (true) { |
6167 | auto *AT1 = getAsArrayType(T: T1); |
6168 | if (!AT1) |
6169 | return; |
6170 | |
6171 | auto *AT2 = getAsArrayType(T: T2); |
6172 | if (!AT2) |
6173 | return; |
6174 | |
6175 | // If we don't have two array types with the same constant bound nor two |
6176 | // incomplete array types, we've unwrapped everything we can. |
6177 | // C++20 also permits one type to be a constant array type and the other |
6178 | // to be an incomplete array type. |
6179 | // FIXME: Consider also unwrapping array of unknown bound and VLA. |
6180 | if (auto *CAT1 = dyn_cast<ConstantArrayType>(Val: AT1)) { |
6181 | auto *CAT2 = dyn_cast<ConstantArrayType>(Val: AT2); |
6182 | if (!((CAT2 && CAT1->getSize() == CAT2->getSize()) || |
6183 | (AllowPiMismatch && getLangOpts().CPlusPlus20 && |
6184 | isa<IncompleteArrayType>(Val: AT2)))) |
6185 | return; |
6186 | } else if (isa<IncompleteArrayType>(Val: AT1)) { |
6187 | if (!(isa<IncompleteArrayType>(Val: AT2) || |
6188 | (AllowPiMismatch && getLangOpts().CPlusPlus20 && |
6189 | isa<ConstantArrayType>(Val: AT2)))) |
6190 | return; |
6191 | } else { |
6192 | return; |
6193 | } |
6194 | |
6195 | T1 = AT1->getElementType(); |
6196 | T2 = AT2->getElementType(); |
6197 | } |
6198 | } |
6199 | |
6200 | /// Attempt to unwrap two types that may be similar (C++ [conv.qual]). |
6201 | /// |
6202 | /// If T1 and T2 are both pointer types of the same kind, or both array types |
6203 | /// with the same bound, unwraps layers from T1 and T2 until a pointer type is |
6204 | /// unwrapped. Top-level qualifiers on T1 and T2 are ignored. |
6205 | /// |
6206 | /// This function will typically be called in a loop that successively |
6207 | /// "unwraps" pointer and pointer-to-member types to compare them at each |
6208 | /// level. |
6209 | /// |
6210 | /// \param AllowPiMismatch Allow the Pi1 and Pi2 to differ as described in |
6211 | /// C++20 [conv.qual], if permitted by the current language mode. |
6212 | /// |
6213 | /// \return \c true if a pointer type was unwrapped, \c false if we reached a |
6214 | /// pair of types that can't be unwrapped further. |
6215 | bool ASTContext::UnwrapSimilarTypes(QualType &T1, QualType &T2, |
6216 | bool AllowPiMismatch) { |
6217 | UnwrapSimilarArrayTypes(T1, T2, AllowPiMismatch); |
6218 | |
6219 | const auto *T1PtrType = T1->getAs<PointerType>(); |
6220 | const auto *T2PtrType = T2->getAs<PointerType>(); |
6221 | if (T1PtrType && T2PtrType) { |
6222 | T1 = T1PtrType->getPointeeType(); |
6223 | T2 = T2PtrType->getPointeeType(); |
6224 | return true; |
6225 | } |
6226 | |
6227 | const auto *T1MPType = T1->getAs<MemberPointerType>(); |
6228 | const auto *T2MPType = T2->getAs<MemberPointerType>(); |
6229 | if (T1MPType && T2MPType && |
6230 | hasSameUnqualifiedType(T1: QualType(T1MPType->getClass(), 0), |
6231 | T2: QualType(T2MPType->getClass(), 0))) { |
6232 | T1 = T1MPType->getPointeeType(); |
6233 | T2 = T2MPType->getPointeeType(); |
6234 | return true; |
6235 | } |
6236 | |
6237 | if (getLangOpts().ObjC) { |
6238 | const auto *T1OPType = T1->getAs<ObjCObjectPointerType>(); |
6239 | const auto *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
6240 | if (T1OPType && T2OPType) { |
6241 | T1 = T1OPType->getPointeeType(); |
6242 | T2 = T2OPType->getPointeeType(); |
6243 | return true; |
6244 | } |
6245 | } |
6246 | |
6247 | // FIXME: Block pointers, too? |
6248 | |
6249 | return false; |
6250 | } |
6251 | |
6252 | bool ASTContext::hasSimilarType(QualType T1, QualType T2) { |
6253 | while (true) { |
6254 | Qualifiers Quals; |
6255 | T1 = getUnqualifiedArrayType(type: T1, quals&: Quals); |
6256 | T2 = getUnqualifiedArrayType(type: T2, quals&: Quals); |
6257 | if (hasSameType(T1, T2)) |
6258 | return true; |
6259 | if (!UnwrapSimilarTypes(T1, T2)) |
6260 | return false; |
6261 | } |
6262 | } |
6263 | |
6264 | bool ASTContext::hasCvrSimilarType(QualType T1, QualType T2) { |
6265 | while (true) { |
6266 | Qualifiers Quals1, Quals2; |
6267 | T1 = getUnqualifiedArrayType(type: T1, quals&: Quals1); |
6268 | T2 = getUnqualifiedArrayType(type: T2, quals&: Quals2); |
6269 | |
6270 | Quals1.removeCVRQualifiers(); |
6271 | Quals2.removeCVRQualifiers(); |
6272 | if (Quals1 != Quals2) |
6273 | return false; |
6274 | |
6275 | if (hasSameType(T1, T2)) |
6276 | return true; |
6277 | |
6278 | if (!UnwrapSimilarTypes(T1, T2, /*AllowPiMismatch*/ false)) |
6279 | return false; |
6280 | } |
6281 | } |
6282 | |
6283 | DeclarationNameInfo |
6284 | ASTContext::getNameForTemplate(TemplateName Name, |
6285 | SourceLocation NameLoc) const { |
6286 | switch (Name.getKind()) { |
6287 | case TemplateName::QualifiedTemplate: |
6288 | case TemplateName::Template: |
6289 | // DNInfo work in progress: CHECKME: what about DNLoc? |
6290 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
6291 | NameLoc); |
6292 | |
6293 | case TemplateName::OverloadedTemplate: { |
6294 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
6295 | // DNInfo work in progress: CHECKME: what about DNLoc? |
6296 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
6297 | } |
6298 | |
6299 | case TemplateName::AssumedTemplate: { |
6300 | AssumedTemplateStorage *Storage = Name.getAsAssumedTemplateName(); |
6301 | return DeclarationNameInfo(Storage->getDeclName(), NameLoc); |
6302 | } |
6303 | |
6304 | case TemplateName::DependentTemplate: { |
6305 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
6306 | DeclarationName DName; |
6307 | if (DTN->isIdentifier()) { |
6308 | DName = DeclarationNames.getIdentifier(ID: DTN->getIdentifier()); |
6309 | return DeclarationNameInfo(DName, NameLoc); |
6310 | } else { |
6311 | DName = DeclarationNames.getCXXOperatorName(Op: DTN->getOperator()); |
6312 | // DNInfo work in progress: FIXME: source locations? |
6313 | DeclarationNameLoc DNLoc = |
6314 | DeclarationNameLoc::makeCXXOperatorNameLoc(Range: SourceRange()); |
6315 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
6316 | } |
6317 | } |
6318 | |
6319 | case TemplateName::SubstTemplateTemplateParm: { |
6320 | SubstTemplateTemplateParmStorage *subst |
6321 | = Name.getAsSubstTemplateTemplateParm(); |
6322 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
6323 | NameLoc); |
6324 | } |
6325 | |
6326 | case TemplateName::SubstTemplateTemplateParmPack: { |
6327 | SubstTemplateTemplateParmPackStorage *subst |
6328 | = Name.getAsSubstTemplateTemplateParmPack(); |
6329 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
6330 | NameLoc); |
6331 | } |
6332 | case TemplateName::UsingTemplate: |
6333 | return DeclarationNameInfo(Name.getAsUsingShadowDecl()->getDeclName(), |
6334 | NameLoc); |
6335 | } |
6336 | |
6337 | llvm_unreachable("bad template name kind!" ); |
6338 | } |
6339 | |
6340 | TemplateName |
6341 | ASTContext::getCanonicalTemplateName(const TemplateName &Name) const { |
6342 | switch (Name.getKind()) { |
6343 | case TemplateName::UsingTemplate: |
6344 | case TemplateName::QualifiedTemplate: |
6345 | case TemplateName::Template: { |
6346 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
6347 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Val: Template)) |
6348 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
6349 | |
6350 | // The canonical template name is the canonical template declaration. |
6351 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
6352 | } |
6353 | |
6354 | case TemplateName::OverloadedTemplate: |
6355 | case TemplateName::AssumedTemplate: |
6356 | llvm_unreachable("cannot canonicalize unresolved template" ); |
6357 | |
6358 | case TemplateName::DependentTemplate: { |
6359 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
6360 | assert(DTN && "Non-dependent template names must refer to template decls." ); |
6361 | return DTN->CanonicalTemplateName; |
6362 | } |
6363 | |
6364 | case TemplateName::SubstTemplateTemplateParm: { |
6365 | SubstTemplateTemplateParmStorage *subst |
6366 | = Name.getAsSubstTemplateTemplateParm(); |
6367 | return getCanonicalTemplateName(Name: subst->getReplacement()); |
6368 | } |
6369 | |
6370 | case TemplateName::SubstTemplateTemplateParmPack: { |
6371 | SubstTemplateTemplateParmPackStorage *subst = |
6372 | Name.getAsSubstTemplateTemplateParmPack(); |
6373 | TemplateArgument canonArgPack = |
6374 | getCanonicalTemplateArgument(Arg: subst->getArgumentPack()); |
6375 | return getSubstTemplateTemplateParmPack( |
6376 | ArgPack: canonArgPack, AssociatedDecl: subst->getAssociatedDecl()->getCanonicalDecl(), |
6377 | Index: subst->getFinal(), Final: subst->getIndex()); |
6378 | } |
6379 | } |
6380 | |
6381 | llvm_unreachable("bad template name!" ); |
6382 | } |
6383 | |
6384 | bool ASTContext::hasSameTemplateName(const TemplateName &X, |
6385 | const TemplateName &Y) const { |
6386 | return getCanonicalTemplateName(Name: X).getAsVoidPointer() == |
6387 | getCanonicalTemplateName(Name: Y).getAsVoidPointer(); |
6388 | } |
6389 | |
6390 | bool ASTContext::isSameConstraintExpr(const Expr *XCE, const Expr *YCE) const { |
6391 | if (!XCE != !YCE) |
6392 | return false; |
6393 | |
6394 | if (!XCE) |
6395 | return true; |
6396 | |
6397 | llvm::FoldingSetNodeID XCEID, YCEID; |
6398 | XCE->Profile(XCEID, *this, /*Canonical=*/true, /*ProfileLambdaExpr=*/true); |
6399 | YCE->Profile(YCEID, *this, /*Canonical=*/true, /*ProfileLambdaExpr=*/true); |
6400 | return XCEID == YCEID; |
6401 | } |
6402 | |
6403 | bool ASTContext::isSameTypeConstraint(const TypeConstraint *XTC, |
6404 | const TypeConstraint *YTC) const { |
6405 | if (!XTC != !YTC) |
6406 | return false; |
6407 | |
6408 | if (!XTC) |
6409 | return true; |
6410 | |
6411 | auto *NCX = XTC->getNamedConcept(); |
6412 | auto *NCY = YTC->getNamedConcept(); |
6413 | if (!NCX || !NCY || !isSameEntity(NCX, NCY)) |
6414 | return false; |
6415 | if (XTC->getConceptReference()->hasExplicitTemplateArgs() != |
6416 | YTC->getConceptReference()->hasExplicitTemplateArgs()) |
6417 | return false; |
6418 | if (XTC->getConceptReference()->hasExplicitTemplateArgs()) |
6419 | if (XTC->getConceptReference() |
6420 | ->getTemplateArgsAsWritten() |
6421 | ->NumTemplateArgs != |
6422 | YTC->getConceptReference()->getTemplateArgsAsWritten()->NumTemplateArgs) |
6423 | return false; |
6424 | |
6425 | // Compare slowly by profiling. |
6426 | // |
6427 | // We couldn't compare the profiling result for the template |
6428 | // args here. Consider the following example in different modules: |
6429 | // |
6430 | // template <__integer_like _Tp, C<_Tp> Sentinel> |
6431 | // constexpr _Tp operator()(_Tp &&__t, Sentinel &&last) const { |
6432 | // return __t; |
6433 | // } |
6434 | // |
6435 | // When we compare the profiling result for `C<_Tp>` in different |
6436 | // modules, it will compare the type of `_Tp` in different modules. |
6437 | // However, the type of `_Tp` in different modules refer to different |
6438 | // types here naturally. So we couldn't compare the profiling result |
6439 | // for the template args directly. |
6440 | return isSameConstraintExpr(XCE: XTC->getImmediatelyDeclaredConstraint(), |
6441 | YCE: YTC->getImmediatelyDeclaredConstraint()); |
6442 | } |
6443 | |
6444 | bool ASTContext::isSameTemplateParameter(const NamedDecl *X, |
6445 | const NamedDecl *Y) const { |
6446 | if (X->getKind() != Y->getKind()) |
6447 | return false; |
6448 | |
6449 | if (auto *TX = dyn_cast<TemplateTypeParmDecl>(Val: X)) { |
6450 | auto *TY = cast<TemplateTypeParmDecl>(Val: Y); |
6451 | if (TX->isParameterPack() != TY->isParameterPack()) |
6452 | return false; |
6453 | if (TX->hasTypeConstraint() != TY->hasTypeConstraint()) |
6454 | return false; |
6455 | return isSameTypeConstraint(XTC: TX->getTypeConstraint(), |
6456 | YTC: TY->getTypeConstraint()); |
6457 | } |
6458 | |
6459 | if (auto *TX = dyn_cast<NonTypeTemplateParmDecl>(Val: X)) { |
6460 | auto *TY = cast<NonTypeTemplateParmDecl>(Val: Y); |
6461 | return TX->isParameterPack() == TY->isParameterPack() && |
6462 | TX->getASTContext().hasSameType(TX->getType(), TY->getType()) && |
6463 | isSameConstraintExpr(XCE: TX->getPlaceholderTypeConstraint(), |
6464 | YCE: TY->getPlaceholderTypeConstraint()); |
6465 | } |
6466 | |
6467 | auto *TX = cast<TemplateTemplateParmDecl>(Val: X); |
6468 | auto *TY = cast<TemplateTemplateParmDecl>(Val: Y); |
6469 | return TX->isParameterPack() == TY->isParameterPack() && |
6470 | isSameTemplateParameterList(X: TX->getTemplateParameters(), |
6471 | Y: TY->getTemplateParameters()); |
6472 | } |
6473 | |
6474 | bool ASTContext::isSameTemplateParameterList( |
6475 | const TemplateParameterList *X, const TemplateParameterList *Y) const { |
6476 | if (X->size() != Y->size()) |
6477 | return false; |
6478 | |
6479 | for (unsigned I = 0, N = X->size(); I != N; ++I) |
6480 | if (!isSameTemplateParameter(X: X->getParam(Idx: I), Y: Y->getParam(Idx: I))) |
6481 | return false; |
6482 | |
6483 | return isSameConstraintExpr(XCE: X->getRequiresClause(), YCE: Y->getRequiresClause()); |
6484 | } |
6485 | |
6486 | bool ASTContext::isSameDefaultTemplateArgument(const NamedDecl *X, |
6487 | const NamedDecl *Y) const { |
6488 | // If the type parameter isn't the same already, we don't need to check the |
6489 | // default argument further. |
6490 | if (!isSameTemplateParameter(X, Y)) |
6491 | return false; |
6492 | |
6493 | if (auto *TTPX = dyn_cast<TemplateTypeParmDecl>(Val: X)) { |
6494 | auto *TTPY = cast<TemplateTypeParmDecl>(Val: Y); |
6495 | if (!TTPX->hasDefaultArgument() || !TTPY->hasDefaultArgument()) |
6496 | return false; |
6497 | |
6498 | return hasSameType(T1: TTPX->getDefaultArgument(), T2: TTPY->getDefaultArgument()); |
6499 | } |
6500 | |
6501 | if (auto *NTTPX = dyn_cast<NonTypeTemplateParmDecl>(Val: X)) { |
6502 | auto *NTTPY = cast<NonTypeTemplateParmDecl>(Val: Y); |
6503 | if (!NTTPX->hasDefaultArgument() || !NTTPY->hasDefaultArgument()) |
6504 | return false; |
6505 | |
6506 | Expr *DefaultArgumentX = NTTPX->getDefaultArgument()->IgnoreImpCasts(); |
6507 | Expr *DefaultArgumentY = NTTPY->getDefaultArgument()->IgnoreImpCasts(); |
6508 | llvm::FoldingSetNodeID XID, YID; |
6509 | DefaultArgumentX->Profile(XID, *this, /*Canonical=*/true); |
6510 | DefaultArgumentY->Profile(YID, *this, /*Canonical=*/true); |
6511 | return XID == YID; |
6512 | } |
6513 | |
6514 | auto *TTPX = cast<TemplateTemplateParmDecl>(Val: X); |
6515 | auto *TTPY = cast<TemplateTemplateParmDecl>(Val: Y); |
6516 | |
6517 | if (!TTPX->hasDefaultArgument() || !TTPY->hasDefaultArgument()) |
6518 | return false; |
6519 | |
6520 | const TemplateArgument &TAX = TTPX->getDefaultArgument().getArgument(); |
6521 | const TemplateArgument &TAY = TTPY->getDefaultArgument().getArgument(); |
6522 | return hasSameTemplateName(X: TAX.getAsTemplate(), Y: TAY.getAsTemplate()); |
6523 | } |
6524 | |
6525 | static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) { |
6526 | if (auto *NS = X->getAsNamespace()) |
6527 | return NS; |
6528 | if (auto *NAS = X->getAsNamespaceAlias()) |
6529 | return NAS->getNamespace(); |
6530 | return nullptr; |
6531 | } |
6532 | |
6533 | static bool isSameQualifier(const NestedNameSpecifier *X, |
6534 | const NestedNameSpecifier *Y) { |
6535 | if (auto *NSX = getNamespace(X)) { |
6536 | auto *NSY = getNamespace(X: Y); |
6537 | if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl()) |
6538 | return false; |
6539 | } else if (X->getKind() != Y->getKind()) |
6540 | return false; |
6541 | |
6542 | // FIXME: For namespaces and types, we're permitted to check that the entity |
6543 | // is named via the same tokens. We should probably do so. |
6544 | switch (X->getKind()) { |
6545 | case NestedNameSpecifier::Identifier: |
6546 | if (X->getAsIdentifier() != Y->getAsIdentifier()) |
6547 | return false; |
6548 | break; |
6549 | case NestedNameSpecifier::Namespace: |
6550 | case NestedNameSpecifier::NamespaceAlias: |
6551 | // We've already checked that we named the same namespace. |
6552 | break; |
6553 | case NestedNameSpecifier::TypeSpec: |
6554 | case NestedNameSpecifier::TypeSpecWithTemplate: |
6555 | if (X->getAsType()->getCanonicalTypeInternal() != |
6556 | Y->getAsType()->getCanonicalTypeInternal()) |
6557 | return false; |
6558 | break; |
6559 | case NestedNameSpecifier::Global: |
6560 | case NestedNameSpecifier::Super: |
6561 | return true; |
6562 | } |
6563 | |
6564 | // Recurse into earlier portion of NNS, if any. |
6565 | auto *PX = X->getPrefix(); |
6566 | auto *PY = Y->getPrefix(); |
6567 | if (PX && PY) |
6568 | return isSameQualifier(X: PX, Y: PY); |
6569 | return !PX && !PY; |
6570 | } |
6571 | |
6572 | /// Determine whether the attributes we can overload on are identical for A and |
6573 | /// B. Will ignore any overloadable attrs represented in the type of A and B. |
6574 | static bool hasSameOverloadableAttrs(const FunctionDecl *A, |
6575 | const FunctionDecl *B) { |
6576 | // Note that pass_object_size attributes are represented in the function's |
6577 | // ExtParameterInfo, so we don't need to check them here. |
6578 | |
6579 | llvm::FoldingSetNodeID Cand1ID, Cand2ID; |
6580 | auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>(); |
6581 | auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>(); |
6582 | |
6583 | for (auto Pair : zip_longest(AEnableIfAttrs, BEnableIfAttrs)) { |
6584 | std::optional<EnableIfAttr *> Cand1A = std::get<0>(Pair); |
6585 | std::optional<EnableIfAttr *> Cand2A = std::get<1>(Pair); |
6586 | |
6587 | // Return false if the number of enable_if attributes is different. |
6588 | if (!Cand1A || !Cand2A) |
6589 | return false; |
6590 | |
6591 | Cand1ID.clear(); |
6592 | Cand2ID.clear(); |
6593 | |
6594 | (*Cand1A)->getCond()->Profile(Cand1ID, A->getASTContext(), true); |
6595 | (*Cand2A)->getCond()->Profile(Cand2ID, B->getASTContext(), true); |
6596 | |
6597 | // Return false if any of the enable_if expressions of A and B are |
6598 | // different. |
6599 | if (Cand1ID != Cand2ID) |
6600 | return false; |
6601 | } |
6602 | return true; |
6603 | } |
6604 | |
6605 | bool ASTContext::isSameEntity(const NamedDecl *X, const NamedDecl *Y) const { |
6606 | // Caution: this function is called by the AST reader during deserialization, |
6607 | // so it cannot rely on AST invariants being met. Non-trivial accessors |
6608 | // should be avoided, along with any traversal of redeclaration chains. |
6609 | |
6610 | if (X == Y) |
6611 | return true; |
6612 | |
6613 | if (X->getDeclName() != Y->getDeclName()) |
6614 | return false; |
6615 | |
6616 | // Must be in the same context. |
6617 | // |
6618 | // Note that we can't use DeclContext::Equals here, because the DeclContexts |
6619 | // could be two different declarations of the same function. (We will fix the |
6620 | // semantic DC to refer to the primary definition after merging.) |
6621 | if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()), |
6622 | cast<Decl>(Y->getDeclContext()->getRedeclContext()))) |
6623 | return false; |
6624 | |
6625 | // Two typedefs refer to the same entity if they have the same underlying |
6626 | // type. |
6627 | if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(Val: X)) |
6628 | if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Val: Y)) |
6629 | return hasSameType(T1: TypedefX->getUnderlyingType(), |
6630 | T2: TypedefY->getUnderlyingType()); |
6631 | |
6632 | // Must have the same kind. |
6633 | if (X->getKind() != Y->getKind()) |
6634 | return false; |
6635 | |
6636 | // Objective-C classes and protocols with the same name always match. |
6637 | if (isa<ObjCInterfaceDecl>(Val: X) || isa<ObjCProtocolDecl>(Val: X)) |
6638 | return true; |
6639 | |
6640 | if (isa<ClassTemplateSpecializationDecl>(Val: X)) { |
6641 | // No need to handle these here: we merge them when adding them to the |
6642 | // template. |
6643 | return false; |
6644 | } |
6645 | |
6646 | // Compatible tags match. |
6647 | if (const auto *TagX = dyn_cast<TagDecl>(Val: X)) { |
6648 | const auto *TagY = cast<TagDecl>(Val: Y); |
6649 | return (TagX->getTagKind() == TagY->getTagKind()) || |
6650 | ((TagX->getTagKind() == TagTypeKind::Struct || |
6651 | TagX->getTagKind() == TagTypeKind::Class || |
6652 | TagX->getTagKind() == TagTypeKind::Interface) && |
6653 | (TagY->getTagKind() == TagTypeKind::Struct || |
6654 | TagY->getTagKind() == TagTypeKind::Class || |
6655 | TagY->getTagKind() == TagTypeKind::Interface)); |
6656 | } |
6657 | |
6658 | // Functions with the same type and linkage match. |
6659 | // FIXME: This needs to cope with merging of prototyped/non-prototyped |
6660 | // functions, etc. |
6661 | if (const auto *FuncX = dyn_cast<FunctionDecl>(Val: X)) { |
6662 | const auto *FuncY = cast<FunctionDecl>(Val: Y); |
6663 | if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(Val: X)) { |
6664 | const auto *CtorY = cast<CXXConstructorDecl>(Val: Y); |
6665 | if (CtorX->getInheritedConstructor() && |
6666 | !isSameEntity(CtorX->getInheritedConstructor().getConstructor(), |
6667 | CtorY->getInheritedConstructor().getConstructor())) |
6668 | return false; |
6669 | } |
6670 | |
6671 | if (FuncX->isMultiVersion() != FuncY->isMultiVersion()) |
6672 | return false; |
6673 | |
6674 | // Multiversioned functions with different feature strings are represented |
6675 | // as separate declarations. |
6676 | if (FuncX->isMultiVersion()) { |
6677 | const auto *TAX = FuncX->getAttr<TargetAttr>(); |
6678 | const auto *TAY = FuncY->getAttr<TargetAttr>(); |
6679 | assert(TAX && TAY && "Multiversion Function without target attribute" ); |
6680 | |
6681 | if (TAX->getFeaturesStr() != TAY->getFeaturesStr()) |
6682 | return false; |
6683 | } |
6684 | |
6685 | // Per C++20 [temp.over.link]/4, friends in different classes are sometimes |
6686 | // not the same entity if they are constrained. |
6687 | if ((FuncX->isMemberLikeConstrainedFriend() || |
6688 | FuncY->isMemberLikeConstrainedFriend()) && |
6689 | !FuncX->getLexicalDeclContext()->Equals( |
6690 | FuncY->getLexicalDeclContext())) { |
6691 | return false; |
6692 | } |
6693 | |
6694 | if (!isSameConstraintExpr(XCE: FuncX->getTrailingRequiresClause(), |
6695 | YCE: FuncY->getTrailingRequiresClause())) |
6696 | return false; |
6697 | |
6698 | auto GetTypeAsWritten = [](const FunctionDecl *FD) { |
6699 | // Map to the first declaration that we've already merged into this one. |
6700 | // The TSI of redeclarations might not match (due to calling conventions |
6701 | // being inherited onto the type but not the TSI), but the TSI type of |
6702 | // the first declaration of the function should match across modules. |
6703 | FD = FD->getCanonicalDecl(); |
6704 | return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType() |
6705 | : FD->getType(); |
6706 | }; |
6707 | QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY); |
6708 | if (!hasSameType(T1: XT, T2: YT)) { |
6709 | // We can get functions with different types on the redecl chain in C++17 |
6710 | // if they have differing exception specifications and at least one of |
6711 | // the excpetion specs is unresolved. |
6712 | auto *XFPT = XT->getAs<FunctionProtoType>(); |
6713 | auto *YFPT = YT->getAs<FunctionProtoType>(); |
6714 | if (getLangOpts().CPlusPlus17 && XFPT && YFPT && |
6715 | (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) || |
6716 | isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) && |
6717 | hasSameFunctionTypeIgnoringExceptionSpec(T: XT, U: YT)) |
6718 | return true; |
6719 | return false; |
6720 | } |
6721 | |
6722 | return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() && |
6723 | hasSameOverloadableAttrs(A: FuncX, B: FuncY); |
6724 | } |
6725 | |
6726 | // Variables with the same type and linkage match. |
6727 | if (const auto *VarX = dyn_cast<VarDecl>(Val: X)) { |
6728 | const auto *VarY = cast<VarDecl>(Val: Y); |
6729 | if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) { |
6730 | // During deserialization, we might compare variables before we load |
6731 | // their types. Assume the types will end up being the same. |
6732 | if (VarX->getType().isNull() || VarY->getType().isNull()) |
6733 | return true; |
6734 | |
6735 | if (hasSameType(VarX->getType(), VarY->getType())) |
6736 | return true; |
6737 | |
6738 | // We can get decls with different types on the redecl chain. Eg. |
6739 | // template <typename T> struct S { static T Var[]; }; // #1 |
6740 | // template <typename T> T S<T>::Var[sizeof(T)]; // #2 |
6741 | // Only? happens when completing an incomplete array type. In this case |
6742 | // when comparing #1 and #2 we should go through their element type. |
6743 | const ArrayType *VarXTy = getAsArrayType(T: VarX->getType()); |
6744 | const ArrayType *VarYTy = getAsArrayType(T: VarY->getType()); |
6745 | if (!VarXTy || !VarYTy) |
6746 | return false; |
6747 | if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType()) |
6748 | return hasSameType(T1: VarXTy->getElementType(), T2: VarYTy->getElementType()); |
6749 | } |
6750 | return false; |
6751 | } |
6752 | |
6753 | // Namespaces with the same name and inlinedness match. |
6754 | if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(Val: X)) { |
6755 | const auto *NamespaceY = cast<NamespaceDecl>(Val: Y); |
6756 | return NamespaceX->isInline() == NamespaceY->isInline(); |
6757 | } |
6758 | |
6759 | // Identical template names and kinds match if their template parameter lists |
6760 | // and patterns match. |
6761 | if (const auto *TemplateX = dyn_cast<TemplateDecl>(Val: X)) { |
6762 | const auto *TemplateY = cast<TemplateDecl>(Val: Y); |
6763 | |
6764 | // ConceptDecl wouldn't be the same if their constraint expression differs. |
6765 | if (const auto *ConceptX = dyn_cast<ConceptDecl>(Val: X)) { |
6766 | const auto *ConceptY = cast<ConceptDecl>(Val: Y); |
6767 | if (!isSameConstraintExpr(XCE: ConceptX->getConstraintExpr(), |
6768 | YCE: ConceptY->getConstraintExpr())) |
6769 | return false; |
6770 | } |
6771 | |
6772 | return isSameEntity(X: TemplateX->getTemplatedDecl(), |
6773 | Y: TemplateY->getTemplatedDecl()) && |
6774 | isSameTemplateParameterList(X: TemplateX->getTemplateParameters(), |
6775 | Y: TemplateY->getTemplateParameters()); |
6776 | } |
6777 | |
6778 | // Fields with the same name and the same type match. |
6779 | if (const auto *FDX = dyn_cast<FieldDecl>(Val: X)) { |
6780 | const auto *FDY = cast<FieldDecl>(Val: Y); |
6781 | // FIXME: Also check the bitwidth is odr-equivalent, if any. |
6782 | return hasSameType(FDX->getType(), FDY->getType()); |
6783 | } |
6784 | |
6785 | // Indirect fields with the same target field match. |
6786 | if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(Val: X)) { |
6787 | const auto *IFDY = cast<IndirectFieldDecl>(Val: Y); |
6788 | return IFDX->getAnonField()->getCanonicalDecl() == |
6789 | IFDY->getAnonField()->getCanonicalDecl(); |
6790 | } |
6791 | |
6792 | // Enumerators with the same name match. |
6793 | if (isa<EnumConstantDecl>(Val: X)) |
6794 | // FIXME: Also check the value is odr-equivalent. |
6795 | return true; |
6796 | |
6797 | // Using shadow declarations with the same target match. |
6798 | if (const auto *USX = dyn_cast<UsingShadowDecl>(Val: X)) { |
6799 | const auto *USY = cast<UsingShadowDecl>(Val: Y); |
6800 | return USX->getTargetDecl() == USY->getTargetDecl(); |
6801 | } |
6802 | |
6803 | // Using declarations with the same qualifier match. (We already know that |
6804 | // the name matches.) |
6805 | if (const auto *UX = dyn_cast<UsingDecl>(Val: X)) { |
6806 | const auto *UY = cast<UsingDecl>(Val: Y); |
6807 | return isSameQualifier(X: UX->getQualifier(), Y: UY->getQualifier()) && |
6808 | UX->hasTypename() == UY->hasTypename() && |
6809 | UX->isAccessDeclaration() == UY->isAccessDeclaration(); |
6810 | } |
6811 | if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(Val: X)) { |
6812 | const auto *UY = cast<UnresolvedUsingValueDecl>(Val: Y); |
6813 | return isSameQualifier(X: UX->getQualifier(), Y: UY->getQualifier()) && |
6814 | UX->isAccessDeclaration() == UY->isAccessDeclaration(); |
6815 | } |
6816 | if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(Val: X)) { |
6817 | return isSameQualifier( |
6818 | X: UX->getQualifier(), |
6819 | Y: cast<UnresolvedUsingTypenameDecl>(Val: Y)->getQualifier()); |
6820 | } |
6821 | |
6822 | // Using-pack declarations are only created by instantiation, and match if |
6823 | // they're instantiated from matching UnresolvedUsing...Decls. |
6824 | if (const auto *UX = dyn_cast<UsingPackDecl>(Val: X)) { |
6825 | return declaresSameEntity( |
6826 | UX->getInstantiatedFromUsingDecl(), |
6827 | cast<UsingPackDecl>(Val: Y)->getInstantiatedFromUsingDecl()); |
6828 | } |
6829 | |
6830 | // Namespace alias definitions with the same target match. |
6831 | if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(Val: X)) { |
6832 | const auto *NAY = cast<NamespaceAliasDecl>(Val: Y); |
6833 | return NAX->getNamespace()->Equals(NAY->getNamespace()); |
6834 | } |
6835 | |
6836 | return false; |
6837 | } |
6838 | |
6839 | TemplateArgument |
6840 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
6841 | switch (Arg.getKind()) { |
6842 | case TemplateArgument::Null: |
6843 | return Arg; |
6844 | |
6845 | case TemplateArgument::Expression: |
6846 | return Arg; |
6847 | |
6848 | case TemplateArgument::Declaration: { |
6849 | auto *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
6850 | return TemplateArgument(D, getCanonicalType(T: Arg.getParamTypeForDecl()), |
6851 | Arg.getIsDefaulted()); |
6852 | } |
6853 | |
6854 | case TemplateArgument::NullPtr: |
6855 | return TemplateArgument(getCanonicalType(T: Arg.getNullPtrType()), |
6856 | /*isNullPtr*/ true, Arg.getIsDefaulted()); |
6857 | |
6858 | case TemplateArgument::Template: |
6859 | return TemplateArgument(getCanonicalTemplateName(Name: Arg.getAsTemplate()), |
6860 | Arg.getIsDefaulted()); |
6861 | |
6862 | case TemplateArgument::TemplateExpansion: |
6863 | return TemplateArgument( |
6864 | getCanonicalTemplateName(Name: Arg.getAsTemplateOrTemplatePattern()), |
6865 | Arg.getNumTemplateExpansions(), Arg.getIsDefaulted()); |
6866 | |
6867 | case TemplateArgument::Integral: |
6868 | return TemplateArgument(Arg, getCanonicalType(T: Arg.getIntegralType())); |
6869 | |
6870 | case TemplateArgument::StructuralValue: |
6871 | return TemplateArgument(*this, |
6872 | getCanonicalType(T: Arg.getStructuralValueType()), |
6873 | Arg.getAsStructuralValue()); |
6874 | |
6875 | case TemplateArgument::Type: |
6876 | return TemplateArgument(getCanonicalType(T: Arg.getAsType()), |
6877 | /*isNullPtr*/ false, Arg.getIsDefaulted()); |
6878 | |
6879 | case TemplateArgument::Pack: { |
6880 | bool AnyNonCanonArgs = false; |
6881 | auto CanonArgs = ::getCanonicalTemplateArguments( |
6882 | C: *this, Args: Arg.pack_elements(), AnyNonCanonArgs); |
6883 | if (!AnyNonCanonArgs) |
6884 | return Arg; |
6885 | return TemplateArgument::CreatePackCopy(Context&: const_cast<ASTContext &>(*this), |
6886 | Args: CanonArgs); |
6887 | } |
6888 | } |
6889 | |
6890 | // Silence GCC warning |
6891 | llvm_unreachable("Unhandled template argument kind" ); |
6892 | } |
6893 | |
6894 | NestedNameSpecifier * |
6895 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
6896 | if (!NNS) |
6897 | return nullptr; |
6898 | |
6899 | switch (NNS->getKind()) { |
6900 | case NestedNameSpecifier::Identifier: |
6901 | // Canonicalize the prefix but keep the identifier the same. |
6902 | return NestedNameSpecifier::Create(Context: *this, |
6903 | Prefix: getCanonicalNestedNameSpecifier(NNS: NNS->getPrefix()), |
6904 | II: NNS->getAsIdentifier()); |
6905 | |
6906 | case NestedNameSpecifier::Namespace: |
6907 | // A namespace is canonical; build a nested-name-specifier with |
6908 | // this namespace and no prefix. |
6909 | return NestedNameSpecifier::Create(Context: *this, Prefix: nullptr, |
6910 | NS: NNS->getAsNamespace()->getOriginalNamespace()); |
6911 | |
6912 | case NestedNameSpecifier::NamespaceAlias: |
6913 | // A namespace is canonical; build a nested-name-specifier with |
6914 | // this namespace and no prefix. |
6915 | return NestedNameSpecifier::Create(Context: *this, Prefix: nullptr, |
6916 | NS: NNS->getAsNamespaceAlias()->getNamespace() |
6917 | ->getOriginalNamespace()); |
6918 | |
6919 | // The difference between TypeSpec and TypeSpecWithTemplate is that the |
6920 | // latter will have the 'template' keyword when printed. |
6921 | case NestedNameSpecifier::TypeSpec: |
6922 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
6923 | const Type *T = getCanonicalType(T: NNS->getAsType()); |
6924 | |
6925 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
6926 | // break it apart into its prefix and identifier, then reconsititute those |
6927 | // as the canonical nested-name-specifier. This is required to canonicalize |
6928 | // a dependent nested-name-specifier involving typedefs of dependent-name |
6929 | // types, e.g., |
6930 | // typedef typename T::type T1; |
6931 | // typedef typename T1::type T2; |
6932 | if (const auto *DNT = T->getAs<DependentNameType>()) |
6933 | return NestedNameSpecifier::Create(Context: *this, Prefix: DNT->getQualifier(), |
6934 | II: DNT->getIdentifier()); |
6935 | if (const auto *DTST = T->getAs<DependentTemplateSpecializationType>()) |
6936 | return NestedNameSpecifier::Create(Context: *this, Prefix: DTST->getQualifier(), Template: true, T); |
6937 | |
6938 | // TODO: Set 'Template' parameter to true for other template types. |
6939 | return NestedNameSpecifier::Create(Context: *this, Prefix: nullptr, Template: false, T); |
6940 | } |
6941 | |
6942 | case NestedNameSpecifier::Global: |
6943 | case NestedNameSpecifier::Super: |
6944 | // The global specifier and __super specifer are canonical and unique. |
6945 | return NNS; |
6946 | } |
6947 | |
6948 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!" ); |
6949 | } |
6950 | |
6951 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
6952 | // Handle the non-qualified case efficiently. |
6953 | if (!T.hasLocalQualifiers()) { |
6954 | // Handle the common positive case fast. |
6955 | if (const auto *AT = dyn_cast<ArrayType>(Val&: T)) |
6956 | return AT; |
6957 | } |
6958 | |
6959 | // Handle the common negative case fast. |
6960 | if (!isa<ArrayType>(Val: T.getCanonicalType())) |
6961 | return nullptr; |
6962 | |
6963 | // Apply any qualifiers from the array type to the element type. This |
6964 | // implements C99 6.7.3p8: "If the specification of an array type includes |
6965 | // any type qualifiers, the element type is so qualified, not the array type." |
6966 | |
6967 | // If we get here, we either have type qualifiers on the type, or we have |
6968 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
6969 | // we must propagate them down into the element type. |
6970 | |
6971 | SplitQualType split = T.getSplitDesugaredType(); |
6972 | Qualifiers qs = split.Quals; |
6973 | |
6974 | // If we have a simple case, just return now. |
6975 | const auto *ATy = dyn_cast<ArrayType>(Val: split.Ty); |
6976 | if (!ATy || qs.empty()) |
6977 | return ATy; |
6978 | |
6979 | // Otherwise, we have an array and we have qualifiers on it. Push the |
6980 | // qualifiers into the array element type and return a new array type. |
6981 | QualType NewEltTy = getQualifiedType(T: ATy->getElementType(), Qs: qs); |
6982 | |
6983 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: ATy)) |
6984 | return cast<ArrayType>(getConstantArrayType(EltTy: NewEltTy, ArySizeIn: CAT->getSize(), |
6985 | SizeExpr: CAT->getSizeExpr(), |
6986 | ASM: CAT->getSizeModifier(), |
6987 | IndexTypeQuals: CAT->getIndexTypeCVRQualifiers())); |
6988 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(Val: ATy)) |
6989 | return cast<ArrayType>(getIncompleteArrayType(elementType: NewEltTy, |
6990 | ASM: IAT->getSizeModifier(), |
6991 | elementTypeQuals: IAT->getIndexTypeCVRQualifiers())); |
6992 | |
6993 | if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(Val: ATy)) |
6994 | return cast<ArrayType>( |
6995 | getDependentSizedArrayType(elementType: NewEltTy, |
6996 | numElements: DSAT->getSizeExpr(), |
6997 | ASM: DSAT->getSizeModifier(), |
6998 | elementTypeQuals: DSAT->getIndexTypeCVRQualifiers(), |
6999 | brackets: DSAT->getBracketsRange())); |
7000 | |
7001 | const auto *VAT = cast<VariableArrayType>(Val: ATy); |
7002 | return cast<ArrayType>(getVariableArrayType(EltTy: NewEltTy, |
7003 | NumElts: VAT->getSizeExpr(), |
7004 | ASM: VAT->getSizeModifier(), |
7005 | IndexTypeQuals: VAT->getIndexTypeCVRQualifiers(), |
7006 | Brackets: VAT->getBracketsRange())); |
7007 | } |
7008 | |
7009 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
7010 | if (getLangOpts().HLSL && T->isConstantArrayType()) |
7011 | return getArrayParameterType(Ty: T); |
7012 | if (T->isArrayType() || T->isFunctionType()) |
7013 | return getDecayedType(T); |
7014 | return T; |
7015 | } |
7016 | |
7017 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
7018 | T = getVariableArrayDecayedType(type: T); |
7019 | T = getAdjustedParameterType(T); |
7020 | return T.getUnqualifiedType(); |
7021 | } |
7022 | |
7023 | QualType ASTContext::getExceptionObjectType(QualType T) const { |
7024 | // C++ [except.throw]p3: |
7025 | // A throw-expression initializes a temporary object, called the exception |
7026 | // object, the type of which is determined by removing any top-level |
7027 | // cv-qualifiers from the static type of the operand of throw and adjusting |
7028 | // the type from "array of T" or "function returning T" to "pointer to T" |
7029 | // or "pointer to function returning T", [...] |
7030 | T = getVariableArrayDecayedType(type: T); |
7031 | if (T->isArrayType() || T->isFunctionType()) |
7032 | T = getDecayedType(T); |
7033 | return T.getUnqualifiedType(); |
7034 | } |
7035 | |
7036 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
7037 | /// specified array type to a pointer. This operation is non-trivial when |
7038 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
7039 | /// this returns a pointer to a properly qualified element of the array. |
7040 | /// |
7041 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
7042 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
7043 | // Get the element type with 'getAsArrayType' so that we don't lose any |
7044 | // typedefs in the element type of the array. This also handles propagation |
7045 | // of type qualifiers from the array type into the element type if present |
7046 | // (C99 6.7.3p8). |
7047 | const ArrayType *PrettyArrayType = getAsArrayType(T: Ty); |
7048 | assert(PrettyArrayType && "Not an array type!" ); |
7049 | |
7050 | QualType PtrTy = getPointerType(T: PrettyArrayType->getElementType()); |
7051 | |
7052 | // int x[restrict 4] -> int *restrict |
7053 | QualType Result = getQualifiedType(T: PtrTy, |
7054 | Qs: PrettyArrayType->getIndexTypeQualifiers()); |
7055 | |
7056 | // int x[_Nullable] -> int * _Nullable |
7057 | if (auto Nullability = Ty->getNullability()) { |
7058 | Result = const_cast<ASTContext *>(this)->getAttributedType( |
7059 | attrKind: AttributedType::getNullabilityAttrKind(kind: *Nullability), modifiedType: Result, equivalentType: Result); |
7060 | } |
7061 | return Result; |
7062 | } |
7063 | |
7064 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
7065 | return getBaseElementType(QT: array->getElementType()); |
7066 | } |
7067 | |
7068 | QualType ASTContext::getBaseElementType(QualType type) const { |
7069 | Qualifiers qs; |
7070 | while (true) { |
7071 | SplitQualType split = type.getSplitDesugaredType(); |
7072 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
7073 | if (!array) break; |
7074 | |
7075 | type = array->getElementType(); |
7076 | qs.addConsistentQualifiers(qs: split.Quals); |
7077 | } |
7078 | |
7079 | return getQualifiedType(T: type, Qs: qs); |
7080 | } |
7081 | |
7082 | /// getConstantArrayElementCount - Returns number of constant array elements. |
7083 | uint64_t |
7084 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
7085 | uint64_t ElementCount = 1; |
7086 | do { |
7087 | ElementCount *= CA->getZExtSize(); |
7088 | CA = dyn_cast_or_null<ConstantArrayType>( |
7089 | CA->getElementType()->getAsArrayTypeUnsafe()); |
7090 | } while (CA); |
7091 | return ElementCount; |
7092 | } |
7093 | |
7094 | uint64_t ASTContext::getArrayInitLoopExprElementCount( |
7095 | const ArrayInitLoopExpr *AILE) const { |
7096 | if (!AILE) |
7097 | return 0; |
7098 | |
7099 | uint64_t ElementCount = 1; |
7100 | |
7101 | do { |
7102 | ElementCount *= AILE->getArraySize().getZExtValue(); |
7103 | AILE = dyn_cast<ArrayInitLoopExpr>(Val: AILE->getSubExpr()); |
7104 | } while (AILE); |
7105 | |
7106 | return ElementCount; |
7107 | } |
7108 | |
7109 | /// getFloatingRank - Return a relative rank for floating point types. |
7110 | /// This routine will assert if passed a built-in type that isn't a float. |
7111 | static FloatingRank getFloatingRank(QualType T) { |
7112 | if (const auto *CT = T->getAs<ComplexType>()) |
7113 | return getFloatingRank(T: CT->getElementType()); |
7114 | |
7115 | switch (T->castAs<BuiltinType>()->getKind()) { |
7116 | default: llvm_unreachable("getFloatingRank(): not a floating type" ); |
7117 | case BuiltinType::Float16: return Float16Rank; |
7118 | case BuiltinType::Half: return HalfRank; |
7119 | case BuiltinType::Float: return FloatRank; |
7120 | case BuiltinType::Double: return DoubleRank; |
7121 | case BuiltinType::LongDouble: return LongDoubleRank; |
7122 | case BuiltinType::Float128: return Float128Rank; |
7123 | case BuiltinType::BFloat16: return BFloat16Rank; |
7124 | case BuiltinType::Ibm128: return Ibm128Rank; |
7125 | } |
7126 | } |
7127 | |
7128 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
7129 | /// point types, ignoring the domain of the type (i.e. 'double' == |
7130 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
7131 | /// LHS < RHS, return -1. |
7132 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
7133 | FloatingRank LHSR = getFloatingRank(T: LHS); |
7134 | FloatingRank RHSR = getFloatingRank(T: RHS); |
7135 | |
7136 | if (LHSR == RHSR) |
7137 | return 0; |
7138 | if (LHSR > RHSR) |
7139 | return 1; |
7140 | return -1; |
7141 | } |
7142 | |
7143 | int ASTContext::getFloatingTypeSemanticOrder(QualType LHS, QualType RHS) const { |
7144 | if (&getFloatTypeSemantics(T: LHS) == &getFloatTypeSemantics(T: RHS)) |
7145 | return 0; |
7146 | return getFloatingTypeOrder(LHS, RHS); |
7147 | } |
7148 | |
7149 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
7150 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
7151 | /// or if it is not canonicalized. |
7152 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
7153 | assert(T->isCanonicalUnqualified() && "T should be canonicalized" ); |
7154 | |
7155 | // Results in this 'losing' to any type of the same size, but winning if |
7156 | // larger. |
7157 | if (const auto *EIT = dyn_cast<BitIntType>(Val: T)) |
7158 | return 0 + (EIT->getNumBits() << 3); |
7159 | |
7160 | switch (cast<BuiltinType>(Val: T)->getKind()) { |
7161 | default: llvm_unreachable("getIntegerRank(): not a built-in integer" ); |
7162 | case BuiltinType::Bool: |
7163 | return 1 + (getIntWidth(BoolTy) << 3); |
7164 | case BuiltinType::Char_S: |
7165 | case BuiltinType::Char_U: |
7166 | case BuiltinType::SChar: |
7167 | case BuiltinType::UChar: |
7168 | return 2 + (getIntWidth(CharTy) << 3); |
7169 | case BuiltinType::Short: |
7170 | case BuiltinType::UShort: |
7171 | return 3 + (getIntWidth(ShortTy) << 3); |
7172 | case BuiltinType::Int: |
7173 | case BuiltinType::UInt: |
7174 | return 4 + (getIntWidth(IntTy) << 3); |
7175 | case BuiltinType::Long: |
7176 | case BuiltinType::ULong: |
7177 | return 5 + (getIntWidth(LongTy) << 3); |
7178 | case BuiltinType::LongLong: |
7179 | case BuiltinType::ULongLong: |
7180 | return 6 + (getIntWidth(LongLongTy) << 3); |
7181 | case BuiltinType::Int128: |
7182 | case BuiltinType::UInt128: |
7183 | return 7 + (getIntWidth(Int128Ty) << 3); |
7184 | |
7185 | // "The ranks of char8_t, char16_t, char32_t, and wchar_t equal the ranks of |
7186 | // their underlying types" [c++20 conv.rank] |
7187 | case BuiltinType::Char8: |
7188 | return getIntegerRank(UnsignedCharTy.getTypePtr()); |
7189 | case BuiltinType::Char16: |
7190 | return getIntegerRank( |
7191 | T: getFromTargetType(Type: Target->getChar16Type()).getTypePtr()); |
7192 | case BuiltinType::Char32: |
7193 | return getIntegerRank( |
7194 | T: getFromTargetType(Type: Target->getChar32Type()).getTypePtr()); |
7195 | case BuiltinType::WChar_S: |
7196 | case BuiltinType::WChar_U: |
7197 | return getIntegerRank( |
7198 | T: getFromTargetType(Type: Target->getWCharType()).getTypePtr()); |
7199 | } |
7200 | } |
7201 | |
7202 | /// Whether this is a promotable bitfield reference according |
7203 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
7204 | /// |
7205 | /// \returns the type this bit-field will promote to, or NULL if no |
7206 | /// promotion occurs. |
7207 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
7208 | if (E->isTypeDependent() || E->isValueDependent()) |
7209 | return {}; |
7210 | |
7211 | // C++ [conv.prom]p5: |
7212 | // If the bit-field has an enumerated type, it is treated as any other |
7213 | // value of that type for promotion purposes. |
7214 | if (getLangOpts().CPlusPlus && E->getType()->isEnumeralType()) |
7215 | return {}; |
7216 | |
7217 | // FIXME: We should not do this unless E->refersToBitField() is true. This |
7218 | // matters in C where getSourceBitField() will find bit-fields for various |
7219 | // cases where the source expression is not a bit-field designator. |
7220 | |
7221 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? |
7222 | if (!Field) |
7223 | return {}; |
7224 | |
7225 | QualType FT = Field->getType(); |
7226 | |
7227 | uint64_t BitWidth = Field->getBitWidthValue(Ctx: *this); |
7228 | uint64_t IntSize = getTypeSize(IntTy); |
7229 | // C++ [conv.prom]p5: |
7230 | // A prvalue for an integral bit-field can be converted to a prvalue of type |
7231 | // int if int can represent all the values of the bit-field; otherwise, it |
7232 | // can be converted to unsigned int if unsigned int can represent all the |
7233 | // values of the bit-field. If the bit-field is larger yet, no integral |
7234 | // promotion applies to it. |
7235 | // C11 6.3.1.1/2: |
7236 | // [For a bit-field of type _Bool, int, signed int, or unsigned int:] |
7237 | // If an int can represent all values of the original type (as restricted by |
7238 | // the width, for a bit-field), the value is converted to an int; otherwise, |
7239 | // it is converted to an unsigned int. |
7240 | // |
7241 | // FIXME: C does not permit promotion of a 'long : 3' bitfield to int. |
7242 | // We perform that promotion here to match GCC and C++. |
7243 | // FIXME: C does not permit promotion of an enum bit-field whose rank is |
7244 | // greater than that of 'int'. We perform that promotion to match GCC. |
7245 | // |
7246 | // C23 6.3.1.1p2: |
7247 | // The value from a bit-field of a bit-precise integer type is converted to |
7248 | // the corresponding bit-precise integer type. (The rest is the same as in |
7249 | // C11.) |
7250 | if (QualType QT = Field->getType(); QT->isBitIntType()) |
7251 | return QT; |
7252 | |
7253 | if (BitWidth < IntSize) |
7254 | return IntTy; |
7255 | |
7256 | if (BitWidth == IntSize) |
7257 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
7258 | |
7259 | // Bit-fields wider than int are not subject to promotions, and therefore act |
7260 | // like the base type. GCC has some weird bugs in this area that we |
7261 | // deliberately do not follow (GCC follows a pre-standard resolution to |
7262 | // C's DR315 which treats bit-width as being part of the type, and this leaks |
7263 | // into their semantics in some cases). |
7264 | return {}; |
7265 | } |
7266 | |
7267 | /// getPromotedIntegerType - Returns the type that Promotable will |
7268 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
7269 | /// integer type. |
7270 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
7271 | assert(!Promotable.isNull()); |
7272 | assert(isPromotableIntegerType(Promotable)); |
7273 | if (const auto *ET = Promotable->getAs<EnumType>()) |
7274 | return ET->getDecl()->getPromotionType(); |
7275 | |
7276 | if (const auto *BT = Promotable->getAs<BuiltinType>()) { |
7277 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
7278 | // (3.9.1) can be converted to a prvalue of the first of the following |
7279 | // types that can represent all the values of its underlying type: |
7280 | // int, unsigned int, long int, unsigned long int, long long int, or |
7281 | // unsigned long long int [...] |
7282 | // FIXME: Is there some better way to compute this? |
7283 | if (BT->getKind() == BuiltinType::WChar_S || |
7284 | BT->getKind() == BuiltinType::WChar_U || |
7285 | BT->getKind() == BuiltinType::Char8 || |
7286 | BT->getKind() == BuiltinType::Char16 || |
7287 | BT->getKind() == BuiltinType::Char32) { |
7288 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
7289 | uint64_t FromSize = getTypeSize(BT); |
7290 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
7291 | LongLongTy, UnsignedLongLongTy }; |
7292 | for (const auto &PT : PromoteTypes) { |
7293 | uint64_t ToSize = getTypeSize(PT); |
7294 | if (FromSize < ToSize || |
7295 | (FromSize == ToSize && FromIsSigned == PT->isSignedIntegerType())) |
7296 | return PT; |
7297 | } |
7298 | llvm_unreachable("char type should fit into long long" ); |
7299 | } |
7300 | } |
7301 | |
7302 | // At this point, we should have a signed or unsigned integer type. |
7303 | if (Promotable->isSignedIntegerType()) |
7304 | return IntTy; |
7305 | uint64_t PromotableSize = getIntWidth(T: Promotable); |
7306 | uint64_t IntSize = getIntWidth(IntTy); |
7307 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
7308 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
7309 | } |
7310 | |
7311 | /// Recurses in pointer/array types until it finds an objc retainable |
7312 | /// type and returns its ownership. |
7313 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
7314 | while (!T.isNull()) { |
7315 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
7316 | return T.getObjCLifetime(); |
7317 | if (T->isArrayType()) |
7318 | T = getBaseElementType(type: T); |
7319 | else if (const auto *PT = T->getAs<PointerType>()) |
7320 | T = PT->getPointeeType(); |
7321 | else if (const auto *RT = T->getAs<ReferenceType>()) |
7322 | T = RT->getPointeeType(); |
7323 | else |
7324 | break; |
7325 | } |
7326 | |
7327 | return Qualifiers::OCL_None; |
7328 | } |
7329 | |
7330 | static const Type *getIntegerTypeForEnum(const EnumType *ET) { |
7331 | // Incomplete enum types are not treated as integer types. |
7332 | // FIXME: In C++, enum types are never integer types. |
7333 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) |
7334 | return ET->getDecl()->getIntegerType().getTypePtr(); |
7335 | return nullptr; |
7336 | } |
7337 | |
7338 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
7339 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
7340 | /// LHS < RHS, return -1. |
7341 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
7342 | const Type *LHSC = getCanonicalType(T: LHS).getTypePtr(); |
7343 | const Type *RHSC = getCanonicalType(T: RHS).getTypePtr(); |
7344 | |
7345 | // Unwrap enums to their underlying type. |
7346 | if (const auto *ET = dyn_cast<EnumType>(Val: LHSC)) |
7347 | LHSC = getIntegerTypeForEnum(ET); |
7348 | if (const auto *ET = dyn_cast<EnumType>(Val: RHSC)) |
7349 | RHSC = getIntegerTypeForEnum(ET); |
7350 | |
7351 | if (LHSC == RHSC) return 0; |
7352 | |
7353 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
7354 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
7355 | |
7356 | unsigned LHSRank = getIntegerRank(T: LHSC); |
7357 | unsigned RHSRank = getIntegerRank(T: RHSC); |
7358 | |
7359 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
7360 | if (LHSRank == RHSRank) return 0; |
7361 | return LHSRank > RHSRank ? 1 : -1; |
7362 | } |
7363 | |
7364 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
7365 | if (LHSUnsigned) { |
7366 | // If the unsigned [LHS] type is larger, return it. |
7367 | if (LHSRank >= RHSRank) |
7368 | return 1; |
7369 | |
7370 | // If the signed type can represent all values of the unsigned type, it |
7371 | // wins. Because we are dealing with 2's complement and types that are |
7372 | // powers of two larger than each other, this is always safe. |
7373 | return -1; |
7374 | } |
7375 | |
7376 | // If the unsigned [RHS] type is larger, return it. |
7377 | if (RHSRank >= LHSRank) |
7378 | return -1; |
7379 | |
7380 | // If the signed type can represent all values of the unsigned type, it |
7381 | // wins. Because we are dealing with 2's complement and types that are |
7382 | // powers of two larger than each other, this is always safe. |
7383 | return 1; |
7384 | } |
7385 | |
7386 | TypedefDecl *ASTContext::getCFConstantStringDecl() const { |
7387 | if (CFConstantStringTypeDecl) |
7388 | return CFConstantStringTypeDecl; |
7389 | |
7390 | assert(!CFConstantStringTagDecl && |
7391 | "tag and typedef should be initialized together" ); |
7392 | CFConstantStringTagDecl = buildImplicitRecord(Name: "__NSConstantString_tag" ); |
7393 | CFConstantStringTagDecl->startDefinition(); |
7394 | |
7395 | struct { |
7396 | QualType Type; |
7397 | const char *Name; |
7398 | } Fields[5]; |
7399 | unsigned Count = 0; |
7400 | |
7401 | /// Objective-C ABI |
7402 | /// |
7403 | /// typedef struct __NSConstantString_tag { |
7404 | /// const int *isa; |
7405 | /// int flags; |
7406 | /// const char *str; |
7407 | /// long length; |
7408 | /// } __NSConstantString; |
7409 | /// |
7410 | /// Swift ABI (4.1, 4.2) |
7411 | /// |
7412 | /// typedef struct __NSConstantString_tag { |
7413 | /// uintptr_t _cfisa; |
7414 | /// uintptr_t _swift_rc; |
7415 | /// _Atomic(uint64_t) _cfinfoa; |
7416 | /// const char *_ptr; |
7417 | /// uint32_t _length; |
7418 | /// } __NSConstantString; |
7419 | /// |
7420 | /// Swift ABI (5.0) |
7421 | /// |
7422 | /// typedef struct __NSConstantString_tag { |
7423 | /// uintptr_t _cfisa; |
7424 | /// uintptr_t _swift_rc; |
7425 | /// _Atomic(uint64_t) _cfinfoa; |
7426 | /// const char *_ptr; |
7427 | /// uintptr_t _length; |
7428 | /// } __NSConstantString; |
7429 | |
7430 | const auto CFRuntime = getLangOpts().CFRuntime; |
7431 | if (static_cast<unsigned>(CFRuntime) < |
7432 | static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift)) { |
7433 | Fields[Count++] = { getPointerType(IntTy.withConst()), "isa" }; |
7434 | Fields[Count++] = { IntTy, "flags" }; |
7435 | Fields[Count++] = { getPointerType(CharTy.withConst()), "str" }; |
7436 | Fields[Count++] = { LongTy, "length" }; |
7437 | } else { |
7438 | Fields[Count++] = { getUIntPtrType(), "_cfisa" }; |
7439 | Fields[Count++] = { getUIntPtrType(), "_swift_rc" }; |
7440 | Fields[Count++] = { getFromTargetType(Type: Target->getUInt64Type()), "_swift_rc" }; |
7441 | Fields[Count++] = { getPointerType(CharTy.withConst()), "_ptr" }; |
7442 | if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 || |
7443 | CFRuntime == LangOptions::CoreFoundationABI::Swift4_2) |
7444 | Fields[Count++] = { IntTy, "_ptr" }; |
7445 | else |
7446 | Fields[Count++] = { getUIntPtrType(), "_ptr" }; |
7447 | } |
7448 | |
7449 | // Create fields |
7450 | for (unsigned i = 0; i < Count; ++i) { |
7451 | FieldDecl *Field = |
7452 | FieldDecl::Create(C: *this, DC: CFConstantStringTagDecl, StartLoc: SourceLocation(), |
7453 | IdLoc: SourceLocation(), Id: &Idents.get(Name: Fields[i].Name), |
7454 | T: Fields[i].Type, /*TInfo=*/nullptr, |
7455 | /*BitWidth=*/BW: nullptr, /*Mutable=*/false, InitStyle: ICIS_NoInit); |
7456 | Field->setAccess(AS_public); |
7457 | CFConstantStringTagDecl->addDecl(Field); |
7458 | } |
7459 | |
7460 | CFConstantStringTagDecl->completeDefinition(); |
7461 | // This type is designed to be compatible with NSConstantString, but cannot |
7462 | // use the same name, since NSConstantString is an interface. |
7463 | auto tagType = getTagDeclType(CFConstantStringTagDecl); |
7464 | CFConstantStringTypeDecl = |
7465 | buildImplicitTypedef(T: tagType, Name: "__NSConstantString" ); |
7466 | |
7467 | return CFConstantStringTypeDecl; |
7468 | } |
7469 | |
7470 | RecordDecl *ASTContext::getCFConstantStringTagDecl() const { |
7471 | if (!CFConstantStringTagDecl) |
7472 | getCFConstantStringDecl(); // Build the tag and the typedef. |
7473 | return CFConstantStringTagDecl; |
7474 | } |
7475 | |
7476 | // getCFConstantStringType - Return the type used for constant CFStrings. |
7477 | QualType ASTContext::getCFConstantStringType() const { |
7478 | return getTypedefType(getCFConstantStringDecl()); |
7479 | } |
7480 | |
7481 | QualType ASTContext::getObjCSuperType() const { |
7482 | if (ObjCSuperType.isNull()) { |
7483 | RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord(Name: "objc_super" ); |
7484 | getTranslationUnitDecl()->addDecl(ObjCSuperTypeDecl); |
7485 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
7486 | } |
7487 | return ObjCSuperType; |
7488 | } |
7489 | |
7490 | void ASTContext::setCFConstantStringType(QualType T) { |
7491 | const auto *TD = T->castAs<TypedefType>(); |
7492 | CFConstantStringTypeDecl = cast<TypedefDecl>(Val: TD->getDecl()); |
7493 | const auto *TagType = |
7494 | CFConstantStringTypeDecl->getUnderlyingType()->castAs<RecordType>(); |
7495 | CFConstantStringTagDecl = TagType->getDecl(); |
7496 | } |
7497 | |
7498 | QualType ASTContext::getBlockDescriptorType() const { |
7499 | if (BlockDescriptorType) |
7500 | return getTagDeclType(BlockDescriptorType); |
7501 | |
7502 | RecordDecl *RD; |
7503 | // FIXME: Needs the FlagAppleBlock bit. |
7504 | RD = buildImplicitRecord(Name: "__block_descriptor" ); |
7505 | RD->startDefinition(); |
7506 | |
7507 | QualType FieldTypes[] = { |
7508 | UnsignedLongTy, |
7509 | UnsignedLongTy, |
7510 | }; |
7511 | |
7512 | static const char *const FieldNames[] = { |
7513 | "reserved" , |
7514 | "Size" |
7515 | }; |
7516 | |
7517 | for (size_t i = 0; i < 2; ++i) { |
7518 | FieldDecl *Field = FieldDecl::Create( |
7519 | *this, RD, SourceLocation(), SourceLocation(), |
7520 | &Idents.get(Name: FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
7521 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); |
7522 | Field->setAccess(AS_public); |
7523 | RD->addDecl(Field); |
7524 | } |
7525 | |
7526 | RD->completeDefinition(); |
7527 | |
7528 | BlockDescriptorType = RD; |
7529 | |
7530 | return getTagDeclType(BlockDescriptorType); |
7531 | } |
7532 | |
7533 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
7534 | if (BlockDescriptorExtendedType) |
7535 | return getTagDeclType(BlockDescriptorExtendedType); |
7536 | |
7537 | RecordDecl *RD; |
7538 | // FIXME: Needs the FlagAppleBlock bit. |
7539 | RD = buildImplicitRecord(Name: "__block_descriptor_withcopydispose" ); |
7540 | RD->startDefinition(); |
7541 | |
7542 | QualType FieldTypes[] = { |
7543 | UnsignedLongTy, |
7544 | UnsignedLongTy, |
7545 | getPointerType(VoidPtrTy), |
7546 | getPointerType(VoidPtrTy) |
7547 | }; |
7548 | |
7549 | static const char *const FieldNames[] = { |
7550 | "reserved" , |
7551 | "Size" , |
7552 | "CopyFuncPtr" , |
7553 | "DestroyFuncPtr" |
7554 | }; |
7555 | |
7556 | for (size_t i = 0; i < 4; ++i) { |
7557 | FieldDecl *Field = FieldDecl::Create( |
7558 | *this, RD, SourceLocation(), SourceLocation(), |
7559 | &Idents.get(Name: FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
7560 | /*BitWidth=*/nullptr, |
7561 | /*Mutable=*/false, ICIS_NoInit); |
7562 | Field->setAccess(AS_public); |
7563 | RD->addDecl(Field); |
7564 | } |
7565 | |
7566 | RD->completeDefinition(); |
7567 | |
7568 | BlockDescriptorExtendedType = RD; |
7569 | return getTagDeclType(BlockDescriptorExtendedType); |
7570 | } |
7571 | |
7572 | OpenCLTypeKind ASTContext::getOpenCLTypeKind(const Type *T) const { |
7573 | const auto *BT = dyn_cast<BuiltinType>(Val: T); |
7574 | |
7575 | if (!BT) { |
7576 | if (isa<PipeType>(Val: T)) |
7577 | return OCLTK_Pipe; |
7578 | |
7579 | return OCLTK_Default; |
7580 | } |
7581 | |
7582 | switch (BT->getKind()) { |
7583 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
7584 | case BuiltinType::Id: \ |
7585 | return OCLTK_Image; |
7586 | #include "clang/Basic/OpenCLImageTypes.def" |
7587 | |
7588 | case BuiltinType::OCLClkEvent: |
7589 | return OCLTK_ClkEvent; |
7590 | |
7591 | case BuiltinType::OCLEvent: |
7592 | return OCLTK_Event; |
7593 | |
7594 | case BuiltinType::OCLQueue: |
7595 | return OCLTK_Queue; |
7596 | |
7597 | case BuiltinType::OCLReserveID: |
7598 | return OCLTK_ReserveID; |
7599 | |
7600 | case BuiltinType::OCLSampler: |
7601 | return OCLTK_Sampler; |
7602 | |
7603 | default: |
7604 | return OCLTK_Default; |
7605 | } |
7606 | } |
7607 | |
7608 | LangAS ASTContext::getOpenCLTypeAddrSpace(const Type *T) const { |
7609 | return Target->getOpenCLTypeAddrSpace(TK: getOpenCLTypeKind(T)); |
7610 | } |
7611 | |
7612 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
7613 | /// requires copy/dispose. Note that this must match the logic |
7614 | /// in buildByrefHelpers. |
7615 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
7616 | const VarDecl *D) { |
7617 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
7618 | const Expr *copyExpr = getBlockVarCopyInit(VD: D).getCopyExpr(); |
7619 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
7620 | |
7621 | return true; |
7622 | } |
7623 | |
7624 | // The block needs copy/destroy helpers if Ty is non-trivial to destructively |
7625 | // move or destroy. |
7626 | if (Ty.isNonTrivialToPrimitiveDestructiveMove() || Ty.isDestructedType()) |
7627 | return true; |
7628 | |
7629 | if (!Ty->isObjCRetainableType()) return false; |
7630 | |
7631 | Qualifiers qs = Ty.getQualifiers(); |
7632 | |
7633 | // If we have lifetime, that dominates. |
7634 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
7635 | switch (lifetime) { |
7636 | case Qualifiers::OCL_None: llvm_unreachable("impossible" ); |
7637 | |
7638 | // These are just bits as far as the runtime is concerned. |
7639 | case Qualifiers::OCL_ExplicitNone: |
7640 | case Qualifiers::OCL_Autoreleasing: |
7641 | return false; |
7642 | |
7643 | // These cases should have been taken care of when checking the type's |
7644 | // non-triviality. |
7645 | case Qualifiers::OCL_Weak: |
7646 | case Qualifiers::OCL_Strong: |
7647 | llvm_unreachable("impossible" ); |
7648 | } |
7649 | llvm_unreachable("fell out of lifetime switch!" ); |
7650 | } |
7651 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
7652 | Ty->isObjCObjectPointerType()); |
7653 | } |
7654 | |
7655 | bool ASTContext::getByrefLifetime(QualType Ty, |
7656 | Qualifiers::ObjCLifetime &LifeTime, |
7657 | bool &HasByrefExtendedLayout) const { |
7658 | if (!getLangOpts().ObjC || |
7659 | getLangOpts().getGC() != LangOptions::NonGC) |
7660 | return false; |
7661 | |
7662 | HasByrefExtendedLayout = false; |
7663 | if (Ty->isRecordType()) { |
7664 | HasByrefExtendedLayout = true; |
7665 | LifeTime = Qualifiers::OCL_None; |
7666 | } else if ((LifeTime = Ty.getObjCLifetime())) { |
7667 | // Honor the ARC qualifiers. |
7668 | } else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) { |
7669 | // The MRR rule. |
7670 | LifeTime = Qualifiers::OCL_ExplicitNone; |
7671 | } else { |
7672 | LifeTime = Qualifiers::OCL_None; |
7673 | } |
7674 | return true; |
7675 | } |
7676 | |
7677 | CanQualType ASTContext::getNSUIntegerType() const { |
7678 | assert(Target && "Expected target to be initialized" ); |
7679 | const llvm::Triple &T = Target->getTriple(); |
7680 | // Windows is LLP64 rather than LP64 |
7681 | if (T.isOSWindows() && T.isArch64Bit()) |
7682 | return UnsignedLongLongTy; |
7683 | return UnsignedLongTy; |
7684 | } |
7685 | |
7686 | CanQualType ASTContext::getNSIntegerType() const { |
7687 | assert(Target && "Expected target to be initialized" ); |
7688 | const llvm::Triple &T = Target->getTriple(); |
7689 | // Windows is LLP64 rather than LP64 |
7690 | if (T.isOSWindows() && T.isArch64Bit()) |
7691 | return LongLongTy; |
7692 | return LongTy; |
7693 | } |
7694 | |
7695 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
7696 | if (!ObjCInstanceTypeDecl) |
7697 | ObjCInstanceTypeDecl = |
7698 | buildImplicitTypedef(T: getObjCIdType(), Name: "instancetype" ); |
7699 | return ObjCInstanceTypeDecl; |
7700 | } |
7701 | |
7702 | // This returns true if a type has been typedefed to BOOL: |
7703 | // typedef <type> BOOL; |
7704 | static bool isTypeTypedefedAsBOOL(QualType T) { |
7705 | if (const auto *TT = dyn_cast<TypedefType>(Val&: T)) |
7706 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
7707 | return II->isStr(Str: "BOOL" ); |
7708 | |
7709 | return false; |
7710 | } |
7711 | |
7712 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
7713 | /// purpose. |
7714 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
7715 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
7716 | return CharUnits::Zero(); |
7717 | |
7718 | CharUnits sz = getTypeSizeInChars(T: type); |
7719 | |
7720 | // Make all integer and enum types at least as large as an int |
7721 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
7722 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
7723 | // Treat arrays as pointers, since that's how they're passed in. |
7724 | else if (type->isArrayType()) |
7725 | sz = getTypeSizeInChars(VoidPtrTy); |
7726 | return sz; |
7727 | } |
7728 | |
7729 | bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const { |
7730 | return getTargetInfo().getCXXABI().isMicrosoft() && |
7731 | VD->isStaticDataMember() && |
7732 | VD->getType()->isIntegralOrEnumerationType() && |
7733 | !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit(); |
7734 | } |
7735 | |
7736 | ASTContext::InlineVariableDefinitionKind |
7737 | ASTContext::getInlineVariableDefinitionKind(const VarDecl *VD) const { |
7738 | if (!VD->isInline()) |
7739 | return InlineVariableDefinitionKind::None; |
7740 | |
7741 | // In almost all cases, it's a weak definition. |
7742 | auto *First = VD->getFirstDecl(); |
7743 | if (First->isInlineSpecified() || !First->isStaticDataMember()) |
7744 | return InlineVariableDefinitionKind::Weak; |
7745 | |
7746 | // If there's a file-context declaration in this translation unit, it's a |
7747 | // non-discardable definition. |
7748 | for (auto *D : VD->redecls()) |
7749 | if (D->getLexicalDeclContext()->isFileContext() && |
7750 | !D->isInlineSpecified() && (D->isConstexpr() || First->isConstexpr())) |
7751 | return InlineVariableDefinitionKind::Strong; |
7752 | |
7753 | // If we've not seen one yet, we don't know. |
7754 | return InlineVariableDefinitionKind::WeakUnknown; |
7755 | } |
7756 | |
7757 | static std::string charUnitsToString(const CharUnits &CU) { |
7758 | return llvm::itostr(X: CU.getQuantity()); |
7759 | } |
7760 | |
7761 | /// getObjCEncodingForBlock - Return the encoded type for this block |
7762 | /// declaration. |
7763 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
7764 | std::string S; |
7765 | |
7766 | const BlockDecl *Decl = Expr->getBlockDecl(); |
7767 | QualType BlockTy = |
7768 | Expr->getType()->castAs<BlockPointerType>()->getPointeeType(); |
7769 | QualType BlockReturnTy = BlockTy->castAs<FunctionType>()->getReturnType(); |
7770 | // Encode result type. |
7771 | if (getLangOpts().EncodeExtendedBlockSig) |
7772 | getObjCEncodingForMethodParameter(QT: Decl::OBJC_TQ_None, T: BlockReturnTy, S, |
7773 | Extended: true /*Extended*/); |
7774 | else |
7775 | getObjCEncodingForType(T: BlockReturnTy, S); |
7776 | // Compute size of all parameters. |
7777 | // Start with computing size of a pointer in number of bytes. |
7778 | // FIXME: There might(should) be a better way of doing this computation! |
7779 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
7780 | CharUnits ParmOffset = PtrSize; |
7781 | for (auto *PI : Decl->parameters()) { |
7782 | QualType PType = PI->getType(); |
7783 | CharUnits sz = getObjCEncodingTypeSize(type: PType); |
7784 | if (sz.isZero()) |
7785 | continue; |
7786 | assert(sz.isPositive() && "BlockExpr - Incomplete param type" ); |
7787 | ParmOffset += sz; |
7788 | } |
7789 | // Size of the argument frame |
7790 | S += charUnitsToString(CU: ParmOffset); |
7791 | // Block pointer and offset. |
7792 | S += "@?0" ; |
7793 | |
7794 | // Argument types. |
7795 | ParmOffset = PtrSize; |
7796 | for (auto *PVDecl : Decl->parameters()) { |
7797 | QualType PType = PVDecl->getOriginalType(); |
7798 | if (const auto *AT = |
7799 | dyn_cast<ArrayType>(Val: PType->getCanonicalTypeInternal())) { |
7800 | // Use array's original type only if it has known number of |
7801 | // elements. |
7802 | if (!isa<ConstantArrayType>(Val: AT)) |
7803 | PType = PVDecl->getType(); |
7804 | } else if (PType->isFunctionType()) |
7805 | PType = PVDecl->getType(); |
7806 | if (getLangOpts().EncodeExtendedBlockSig) |
7807 | getObjCEncodingForMethodParameter(QT: Decl::OBJC_TQ_None, T: PType, |
7808 | S, Extended: true /*Extended*/); |
7809 | else |
7810 | getObjCEncodingForType(T: PType, S); |
7811 | S += charUnitsToString(CU: ParmOffset); |
7812 | ParmOffset += getObjCEncodingTypeSize(type: PType); |
7813 | } |
7814 | |
7815 | return S; |
7816 | } |
7817 | |
7818 | std::string |
7819 | ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl) const { |
7820 | std::string S; |
7821 | // Encode result type. |
7822 | getObjCEncodingForType(T: Decl->getReturnType(), S); |
7823 | CharUnits ParmOffset; |
7824 | // Compute size of all parameters. |
7825 | for (auto *PI : Decl->parameters()) { |
7826 | QualType PType = PI->getType(); |
7827 | CharUnits sz = getObjCEncodingTypeSize(type: PType); |
7828 | if (sz.isZero()) |
7829 | continue; |
7830 | |
7831 | assert(sz.isPositive() && |
7832 | "getObjCEncodingForFunctionDecl - Incomplete param type" ); |
7833 | ParmOffset += sz; |
7834 | } |
7835 | S += charUnitsToString(CU: ParmOffset); |
7836 | ParmOffset = CharUnits::Zero(); |
7837 | |
7838 | // Argument types. |
7839 | for (auto *PVDecl : Decl->parameters()) { |
7840 | QualType PType = PVDecl->getOriginalType(); |
7841 | if (const auto *AT = |
7842 | dyn_cast<ArrayType>(Val: PType->getCanonicalTypeInternal())) { |
7843 | // Use array's original type only if it has known number of |
7844 | // elements. |
7845 | if (!isa<ConstantArrayType>(Val: AT)) |
7846 | PType = PVDecl->getType(); |
7847 | } else if (PType->isFunctionType()) |
7848 | PType = PVDecl->getType(); |
7849 | getObjCEncodingForType(T: PType, S); |
7850 | S += charUnitsToString(CU: ParmOffset); |
7851 | ParmOffset += getObjCEncodingTypeSize(type: PType); |
7852 | } |
7853 | |
7854 | return S; |
7855 | } |
7856 | |
7857 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
7858 | /// method parameter or return type. If Extended, include class names and |
7859 | /// block object types. |
7860 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
7861 | QualType T, std::string& S, |
7862 | bool Extended) const { |
7863 | // Encode type qualifier, 'in', 'inout', etc. for the parameter. |
7864 | getObjCEncodingForTypeQualifier(QT, S); |
7865 | // Encode parameter type. |
7866 | ObjCEncOptions Options = ObjCEncOptions() |
7867 | .setExpandPointedToStructures() |
7868 | .setExpandStructures() |
7869 | .setIsOutermostType(); |
7870 | if (Extended) |
7871 | Options.setEncodeBlockParameters().setEncodeClassNames(); |
7872 | getObjCEncodingForTypeImpl(t: T, S, Options, /*Field=*/nullptr); |
7873 | } |
7874 | |
7875 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
7876 | /// declaration. |
7877 | std::string ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
7878 | bool Extended) const { |
7879 | // FIXME: This is not very efficient. |
7880 | // Encode return type. |
7881 | std::string S; |
7882 | getObjCEncodingForMethodParameter(QT: Decl->getObjCDeclQualifier(), |
7883 | T: Decl->getReturnType(), S, Extended); |
7884 | // Compute size of all parameters. |
7885 | // Start with computing size of a pointer in number of bytes. |
7886 | // FIXME: There might(should) be a better way of doing this computation! |
7887 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
7888 | // The first two arguments (self and _cmd) are pointers; account for |
7889 | // their size. |
7890 | CharUnits ParmOffset = 2 * PtrSize; |
7891 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
7892 | E = Decl->sel_param_end(); PI != E; ++PI) { |
7893 | QualType PType = (*PI)->getType(); |
7894 | CharUnits sz = getObjCEncodingTypeSize(type: PType); |
7895 | if (sz.isZero()) |
7896 | continue; |
7897 | |
7898 | assert(sz.isPositive() && |
7899 | "getObjCEncodingForMethodDecl - Incomplete param type" ); |
7900 | ParmOffset += sz; |
7901 | } |
7902 | S += charUnitsToString(CU: ParmOffset); |
7903 | S += "@0:" ; |
7904 | S += charUnitsToString(CU: PtrSize); |
7905 | |
7906 | // Argument types. |
7907 | ParmOffset = 2 * PtrSize; |
7908 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
7909 | E = Decl->sel_param_end(); PI != E; ++PI) { |
7910 | const ParmVarDecl *PVDecl = *PI; |
7911 | QualType PType = PVDecl->getOriginalType(); |
7912 | if (const auto *AT = |
7913 | dyn_cast<ArrayType>(Val: PType->getCanonicalTypeInternal())) { |
7914 | // Use array's original type only if it has known number of |
7915 | // elements. |
7916 | if (!isa<ConstantArrayType>(Val: AT)) |
7917 | PType = PVDecl->getType(); |
7918 | } else if (PType->isFunctionType()) |
7919 | PType = PVDecl->getType(); |
7920 | getObjCEncodingForMethodParameter(QT: PVDecl->getObjCDeclQualifier(), |
7921 | T: PType, S, Extended); |
7922 | S += charUnitsToString(CU: ParmOffset); |
7923 | ParmOffset += getObjCEncodingTypeSize(type: PType); |
7924 | } |
7925 | |
7926 | return S; |
7927 | } |
7928 | |
7929 | ObjCPropertyImplDecl * |
7930 | ASTContext::getObjCPropertyImplDeclForPropertyDecl( |
7931 | const ObjCPropertyDecl *PD, |
7932 | const Decl *Container) const { |
7933 | if (!Container) |
7934 | return nullptr; |
7935 | if (const auto *CID = dyn_cast<ObjCCategoryImplDecl>(Val: Container)) { |
7936 | for (auto *PID : CID->property_impls()) |
7937 | if (PID->getPropertyDecl() == PD) |
7938 | return PID; |
7939 | } else { |
7940 | const auto *OID = cast<ObjCImplementationDecl>(Val: Container); |
7941 | for (auto *PID : OID->property_impls()) |
7942 | if (PID->getPropertyDecl() == PD) |
7943 | return PID; |
7944 | } |
7945 | return nullptr; |
7946 | } |
7947 | |
7948 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
7949 | /// property declaration. If non-NULL, Container must be either an |
7950 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
7951 | /// NULL when getting encodings for protocol properties. |
7952 | /// Property attributes are stored as a comma-delimited C string. The simple |
7953 | /// attributes readonly and bycopy are encoded as single characters. The |
7954 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
7955 | /// encoded as single characters, followed by an identifier. Property types |
7956 | /// are also encoded as a parametrized attribute. The characters used to encode |
7957 | /// these attributes are defined by the following enumeration: |
7958 | /// @code |
7959 | /// enum PropertyAttributes { |
7960 | /// kPropertyReadOnly = 'R', // property is read-only. |
7961 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
7962 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
7963 | /// kPropertyDynamic = 'D', // property is dynamic |
7964 | /// kPropertyGetter = 'G', // followed by getter selector name |
7965 | /// kPropertySetter = 'S', // followed by setter selector name |
7966 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
7967 | /// kPropertyType = 'T' // followed by old-style type encoding. |
7968 | /// kPropertyWeak = 'W' // 'weak' property |
7969 | /// kPropertyStrong = 'P' // property GC'able |
7970 | /// kPropertyNonAtomic = 'N' // property non-atomic |
7971 | /// kPropertyOptional = '?' // property optional |
7972 | /// }; |
7973 | /// @endcode |
7974 | std::string |
7975 | ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
7976 | const Decl *Container) const { |
7977 | // Collect information from the property implementation decl(s). |
7978 | bool Dynamic = false; |
7979 | ObjCPropertyImplDecl *SynthesizePID = nullptr; |
7980 | |
7981 | if (ObjCPropertyImplDecl *PropertyImpDecl = |
7982 | getObjCPropertyImplDeclForPropertyDecl(PD, Container)) { |
7983 | if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
7984 | Dynamic = true; |
7985 | else |
7986 | SynthesizePID = PropertyImpDecl; |
7987 | } |
7988 | |
7989 | // FIXME: This is not very efficient. |
7990 | std::string S = "T" ; |
7991 | |
7992 | // Encode result type. |
7993 | // GCC has some special rules regarding encoding of properties which |
7994 | // closely resembles encoding of ivars. |
7995 | getObjCEncodingForPropertyType(T: PD->getType(), S); |
7996 | |
7997 | if (PD->isOptional()) |
7998 | S += ",?" ; |
7999 | |
8000 | if (PD->isReadOnly()) { |
8001 | S += ",R" ; |
8002 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy) |
8003 | S += ",C" ; |
8004 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_retain) |
8005 | S += ",&" ; |
8006 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak) |
8007 | S += ",W" ; |
8008 | } else { |
8009 | switch (PD->getSetterKind()) { |
8010 | case ObjCPropertyDecl::Assign: break; |
8011 | case ObjCPropertyDecl::Copy: S += ",C" ; break; |
8012 | case ObjCPropertyDecl::Retain: S += ",&" ; break; |
8013 | case ObjCPropertyDecl::Weak: S += ",W" ; break; |
8014 | } |
8015 | } |
8016 | |
8017 | // It really isn't clear at all what this means, since properties |
8018 | // are "dynamic by default". |
8019 | if (Dynamic) |
8020 | S += ",D" ; |
8021 | |
8022 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_nonatomic) |
8023 | S += ",N" ; |
8024 | |
8025 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_getter) { |
8026 | S += ",G" ; |
8027 | S += PD->getGetterName().getAsString(); |
8028 | } |
8029 | |
8030 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_setter) { |
8031 | S += ",S" ; |
8032 | S += PD->getSetterName().getAsString(); |
8033 | } |
8034 | |
8035 | if (SynthesizePID) { |
8036 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
8037 | S += ",V" ; |
8038 | S += OID->getNameAsString(); |
8039 | } |
8040 | |
8041 | // FIXME: OBJCGC: weak & strong |
8042 | return S; |
8043 | } |
8044 | |
8045 | /// getLegacyIntegralTypeEncoding - |
8046 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
8047 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
8048 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
8049 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
8050 | if (PointeeTy->getAs<TypedefType>()) { |
8051 | if (const auto *BT = PointeeTy->getAs<BuiltinType>()) { |
8052 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
8053 | PointeeTy = UnsignedIntTy; |
8054 | else |
8055 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
8056 | PointeeTy = IntTy; |
8057 | } |
8058 | } |
8059 | } |
8060 | |
8061 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
8062 | const FieldDecl *Field, |
8063 | QualType *NotEncodedT) const { |
8064 | // We follow the behavior of gcc, expanding structures which are |
8065 | // directly pointed to, and expanding embedded structures. Note that |
8066 | // these rules are sufficient to prevent recursive encoding of the |
8067 | // same type. |
8068 | getObjCEncodingForTypeImpl(t: T, S, |
8069 | Options: ObjCEncOptions() |
8070 | .setExpandPointedToStructures() |
8071 | .setExpandStructures() |
8072 | .setIsOutermostType(), |
8073 | Field, NotEncodedT); |
8074 | } |
8075 | |
8076 | void ASTContext::getObjCEncodingForPropertyType(QualType T, |
8077 | std::string& S) const { |
8078 | // Encode result type. |
8079 | // GCC has some special rules regarding encoding of properties which |
8080 | // closely resembles encoding of ivars. |
8081 | getObjCEncodingForTypeImpl(t: T, S, |
8082 | Options: ObjCEncOptions() |
8083 | .setExpandPointedToStructures() |
8084 | .setExpandStructures() |
8085 | .setIsOutermostType() |
8086 | .setEncodingProperty(), |
8087 | /*Field=*/nullptr); |
8088 | } |
8089 | |
8090 | static char getObjCEncodingForPrimitiveType(const ASTContext *C, |
8091 | const BuiltinType *BT) { |
8092 | BuiltinType::Kind kind = BT->getKind(); |
8093 | switch (kind) { |
8094 | case BuiltinType::Void: return 'v'; |
8095 | case BuiltinType::Bool: return 'B'; |
8096 | case BuiltinType::Char8: |
8097 | case BuiltinType::Char_U: |
8098 | case BuiltinType::UChar: return 'C'; |
8099 | case BuiltinType::Char16: |
8100 | case BuiltinType::UShort: return 'S'; |
8101 | case BuiltinType::Char32: |
8102 | case BuiltinType::UInt: return 'I'; |
8103 | case BuiltinType::ULong: |
8104 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
8105 | case BuiltinType::UInt128: return 'T'; |
8106 | case BuiltinType::ULongLong: return 'Q'; |
8107 | case BuiltinType::Char_S: |
8108 | case BuiltinType::SChar: return 'c'; |
8109 | case BuiltinType::Short: return 's'; |
8110 | case BuiltinType::WChar_S: |
8111 | case BuiltinType::WChar_U: |
8112 | case BuiltinType::Int: return 'i'; |
8113 | case BuiltinType::Long: |
8114 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
8115 | case BuiltinType::LongLong: return 'q'; |
8116 | case BuiltinType::Int128: return 't'; |
8117 | case BuiltinType::Float: return 'f'; |
8118 | case BuiltinType::Double: return 'd'; |
8119 | case BuiltinType::LongDouble: return 'D'; |
8120 | case BuiltinType::NullPtr: return '*'; // like char* |
8121 | |
8122 | case BuiltinType::BFloat16: |
8123 | case BuiltinType::Float16: |
8124 | case BuiltinType::Float128: |
8125 | case BuiltinType::Ibm128: |
8126 | case BuiltinType::Half: |
8127 | case BuiltinType::ShortAccum: |
8128 | case BuiltinType::Accum: |
8129 | case BuiltinType::LongAccum: |
8130 | case BuiltinType::UShortAccum: |
8131 | case BuiltinType::UAccum: |
8132 | case BuiltinType::ULongAccum: |
8133 | case BuiltinType::ShortFract: |
8134 | case BuiltinType::Fract: |
8135 | case BuiltinType::LongFract: |
8136 | case BuiltinType::UShortFract: |
8137 | case BuiltinType::UFract: |
8138 | case BuiltinType::ULongFract: |
8139 | case BuiltinType::SatShortAccum: |
8140 | case BuiltinType::SatAccum: |
8141 | case BuiltinType::SatLongAccum: |
8142 | case BuiltinType::SatUShortAccum: |
8143 | case BuiltinType::SatUAccum: |
8144 | case BuiltinType::SatULongAccum: |
8145 | case BuiltinType::SatShortFract: |
8146 | case BuiltinType::SatFract: |
8147 | case BuiltinType::SatLongFract: |
8148 | case BuiltinType::SatUShortFract: |
8149 | case BuiltinType::SatUFract: |
8150 | case BuiltinType::SatULongFract: |
8151 | // FIXME: potentially need @encodes for these! |
8152 | return ' '; |
8153 | |
8154 | #define SVE_TYPE(Name, Id, SingletonId) \ |
8155 | case BuiltinType::Id: |
8156 | #include "clang/Basic/AArch64SVEACLETypes.def" |
8157 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
8158 | #include "clang/Basic/RISCVVTypes.def" |
8159 | #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
8160 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
8161 | { |
8162 | DiagnosticsEngine &Diags = C->getDiagnostics(); |
8163 | unsigned DiagID = Diags.getCustomDiagID(L: DiagnosticsEngine::Error, |
8164 | FormatString: "cannot yet @encode type %0" ); |
8165 | Diags.Report(DiagID) << BT->getName(Policy: C->getPrintingPolicy()); |
8166 | return ' '; |
8167 | } |
8168 | |
8169 | case BuiltinType::ObjCId: |
8170 | case BuiltinType::ObjCClass: |
8171 | case BuiltinType::ObjCSel: |
8172 | llvm_unreachable("@encoding ObjC primitive type" ); |
8173 | |
8174 | // OpenCL and placeholder types don't need @encodings. |
8175 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
8176 | case BuiltinType::Id: |
8177 | #include "clang/Basic/OpenCLImageTypes.def" |
8178 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
8179 | case BuiltinType::Id: |
8180 | #include "clang/Basic/OpenCLExtensionTypes.def" |
8181 | case BuiltinType::OCLEvent: |
8182 | case BuiltinType::OCLClkEvent: |
8183 | case BuiltinType::OCLQueue: |
8184 | case BuiltinType::OCLReserveID: |
8185 | case BuiltinType::OCLSampler: |
8186 | case BuiltinType::Dependent: |
8187 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
8188 | case BuiltinType::Id: |
8189 | #include "clang/Basic/PPCTypes.def" |
8190 | #define BUILTIN_TYPE(KIND, ID) |
8191 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
8192 | case BuiltinType::KIND: |
8193 | #include "clang/AST/BuiltinTypes.def" |
8194 | llvm_unreachable("invalid builtin type for @encode" ); |
8195 | } |
8196 | llvm_unreachable("invalid BuiltinType::Kind value" ); |
8197 | } |
8198 | |
8199 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
8200 | EnumDecl *Enum = ET->getDecl(); |
8201 | |
8202 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
8203 | if (!Enum->isFixed()) |
8204 | return 'i'; |
8205 | |
8206 | // The encoding of a fixed enum type matches its fixed underlying type. |
8207 | const auto *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
8208 | return getObjCEncodingForPrimitiveType(C, BT); |
8209 | } |
8210 | |
8211 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
8212 | QualType T, const FieldDecl *FD) { |
8213 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl" ); |
8214 | S += 'b'; |
8215 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
8216 | // The GNU runtime requires more information; bitfields are encoded as b, |
8217 | // then the offset (in bits) of the first element, then the type of the |
8218 | // bitfield, then the size in bits. For example, in this structure: |
8219 | // |
8220 | // struct |
8221 | // { |
8222 | // int integer; |
8223 | // int flags:2; |
8224 | // }; |
8225 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
8226 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
8227 | // information is not especially sensible, but we're stuck with it for |
8228 | // compatibility with GCC, although providing it breaks anything that |
8229 | // actually uses runtime introspection and wants to work on both runtimes... |
8230 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
8231 | uint64_t Offset; |
8232 | |
8233 | if (const auto *IVD = dyn_cast<ObjCIvarDecl>(Val: FD)) { |
8234 | Offset = Ctx->lookupFieldBitOffset(OID: IVD->getContainingInterface(), ID: nullptr, |
8235 | Ivar: IVD); |
8236 | } else { |
8237 | const RecordDecl *RD = FD->getParent(); |
8238 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(D: RD); |
8239 | Offset = RL.getFieldOffset(FieldNo: FD->getFieldIndex()); |
8240 | } |
8241 | |
8242 | S += llvm::utostr(X: Offset); |
8243 | |
8244 | if (const auto *ET = T->getAs<EnumType>()) |
8245 | S += ObjCEncodingForEnumType(C: Ctx, ET); |
8246 | else { |
8247 | const auto *BT = T->castAs<BuiltinType>(); |
8248 | S += getObjCEncodingForPrimitiveType(C: Ctx, BT); |
8249 | } |
8250 | } |
8251 | S += llvm::utostr(X: FD->getBitWidthValue(Ctx: *Ctx)); |
8252 | } |
8253 | |
8254 | // Helper function for determining whether the encoded type string would include |
8255 | // a template specialization type. |
8256 | static bool hasTemplateSpecializationInEncodedString(const Type *T, |
8257 | bool VisitBasesAndFields) { |
8258 | T = T->getBaseElementTypeUnsafe(); |
8259 | |
8260 | if (auto *PT = T->getAs<PointerType>()) |
8261 | return hasTemplateSpecializationInEncodedString( |
8262 | T: PT->getPointeeType().getTypePtr(), VisitBasesAndFields: false); |
8263 | |
8264 | auto *CXXRD = T->getAsCXXRecordDecl(); |
8265 | |
8266 | if (!CXXRD) |
8267 | return false; |
8268 | |
8269 | if (isa<ClassTemplateSpecializationDecl>(Val: CXXRD)) |
8270 | return true; |
8271 | |
8272 | if (!CXXRD->hasDefinition() || !VisitBasesAndFields) |
8273 | return false; |
8274 | |
8275 | for (const auto &B : CXXRD->bases()) |
8276 | if (hasTemplateSpecializationInEncodedString(T: B.getType().getTypePtr(), |
8277 | VisitBasesAndFields: true)) |
8278 | return true; |
8279 | |
8280 | for (auto *FD : CXXRD->fields()) |
8281 | if (hasTemplateSpecializationInEncodedString(FD->getType().getTypePtr(), |
8282 | true)) |
8283 | return true; |
8284 | |
8285 | return false; |
8286 | } |
8287 | |
8288 | // FIXME: Use SmallString for accumulating string. |
8289 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string &S, |
8290 | const ObjCEncOptions Options, |
8291 | const FieldDecl *FD, |
8292 | QualType *NotEncodedT) const { |
8293 | CanQualType CT = getCanonicalType(T); |
8294 | switch (CT->getTypeClass()) { |
8295 | case Type::Builtin: |
8296 | case Type::Enum: |
8297 | if (FD && FD->isBitField()) |
8298 | return EncodeBitField(Ctx: this, S, T, FD); |
8299 | if (const auto *BT = dyn_cast<BuiltinType>(Val&: CT)) |
8300 | S += getObjCEncodingForPrimitiveType(C: this, BT); |
8301 | else |
8302 | S += ObjCEncodingForEnumType(C: this, ET: cast<EnumType>(Val&: CT)); |
8303 | return; |
8304 | |
8305 | case Type::Complex: |
8306 | S += 'j'; |
8307 | getObjCEncodingForTypeImpl(T: T->castAs<ComplexType>()->getElementType(), S, |
8308 | Options: ObjCEncOptions(), |
8309 | /*Field=*/FD: nullptr); |
8310 | return; |
8311 | |
8312 | case Type::Atomic: |
8313 | S += 'A'; |
8314 | getObjCEncodingForTypeImpl(T: T->castAs<AtomicType>()->getValueType(), S, |
8315 | Options: ObjCEncOptions(), |
8316 | /*Field=*/FD: nullptr); |
8317 | return; |
8318 | |
8319 | // encoding for pointer or reference types. |
8320 | case Type::Pointer: |
8321 | case Type::LValueReference: |
8322 | case Type::RValueReference: { |
8323 | QualType PointeeTy; |
8324 | if (isa<PointerType>(Val: CT)) { |
8325 | const auto *PT = T->castAs<PointerType>(); |
8326 | if (PT->isObjCSelType()) { |
8327 | S += ':'; |
8328 | return; |
8329 | } |
8330 | PointeeTy = PT->getPointeeType(); |
8331 | } else { |
8332 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
8333 | } |
8334 | |
8335 | bool isReadOnly = false; |
8336 | // For historical/compatibility reasons, the read-only qualifier of the |
8337 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
8338 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
8339 | // Also, do not emit the 'r' for anything but the outermost type! |
8340 | if (T->getAs<TypedefType>()) { |
8341 | if (Options.IsOutermostType() && T.isConstQualified()) { |
8342 | isReadOnly = true; |
8343 | S += 'r'; |
8344 | } |
8345 | } else if (Options.IsOutermostType()) { |
8346 | QualType P = PointeeTy; |
8347 | while (auto PT = P->getAs<PointerType>()) |
8348 | P = PT->getPointeeType(); |
8349 | if (P.isConstQualified()) { |
8350 | isReadOnly = true; |
8351 | S += 'r'; |
8352 | } |
8353 | } |
8354 | if (isReadOnly) { |
8355 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
8356 | // combinations need to be rearranged. |
8357 | // Rewrite "in const" from "nr" to "rn" |
8358 | if (StringRef(S).ends_with(Suffix: "nr" )) |
8359 | S.replace(i1: S.end()-2, i2: S.end(), s: "rn" ); |
8360 | } |
8361 | |
8362 | if (PointeeTy->isCharType()) { |
8363 | // char pointer types should be encoded as '*' unless it is a |
8364 | // type that has been typedef'd to 'BOOL'. |
8365 | if (!isTypeTypedefedAsBOOL(T: PointeeTy)) { |
8366 | S += '*'; |
8367 | return; |
8368 | } |
8369 | } else if (const auto *RTy = PointeeTy->getAs<RecordType>()) { |
8370 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
8371 | if (RTy->getDecl()->getIdentifier() == &Idents.get(Name: "objc_class" )) { |
8372 | S += '#'; |
8373 | return; |
8374 | } |
8375 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
8376 | if (RTy->getDecl()->getIdentifier() == &Idents.get(Name: "objc_object" )) { |
8377 | S += '@'; |
8378 | return; |
8379 | } |
8380 | // If the encoded string for the class includes template names, just emit |
8381 | // "^v" for pointers to the class. |
8382 | if (getLangOpts().CPlusPlus && |
8383 | (!getLangOpts().EncodeCXXClassTemplateSpec && |
8384 | hasTemplateSpecializationInEncodedString( |
8385 | RTy, Options.ExpandPointedToStructures()))) { |
8386 | S += "^v" ; |
8387 | return; |
8388 | } |
8389 | // fall through... |
8390 | } |
8391 | S += '^'; |
8392 | getLegacyIntegralTypeEncoding(PointeeTy); |
8393 | |
8394 | ObjCEncOptions NewOptions; |
8395 | if (Options.ExpandPointedToStructures()) |
8396 | NewOptions.setExpandStructures(); |
8397 | getObjCEncodingForTypeImpl(T: PointeeTy, S, Options: NewOptions, |
8398 | /*Field=*/FD: nullptr, NotEncodedT); |
8399 | return; |
8400 | } |
8401 | |
8402 | case Type::ConstantArray: |
8403 | case Type::IncompleteArray: |
8404 | case Type::VariableArray: { |
8405 | const auto *AT = cast<ArrayType>(Val&: CT); |
8406 | |
8407 | if (isa<IncompleteArrayType>(Val: AT) && !Options.IsStructField()) { |
8408 | // Incomplete arrays are encoded as a pointer to the array element. |
8409 | S += '^'; |
8410 | |
8411 | getObjCEncodingForTypeImpl( |
8412 | T: AT->getElementType(), S, |
8413 | Options: Options.keepingOnly(Mask: ObjCEncOptions().setExpandStructures()), FD); |
8414 | } else { |
8415 | S += '['; |
8416 | |
8417 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) |
8418 | S += llvm::utostr(X: CAT->getZExtSize()); |
8419 | else { |
8420 | //Variable length arrays are encoded as a regular array with 0 elements. |
8421 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
8422 | "Unknown array type!" ); |
8423 | S += '0'; |
8424 | } |
8425 | |
8426 | getObjCEncodingForTypeImpl( |
8427 | T: AT->getElementType(), S, |
8428 | Options: Options.keepingOnly(Mask: ObjCEncOptions().setExpandStructures()), FD, |
8429 | NotEncodedT); |
8430 | S += ']'; |
8431 | } |
8432 | return; |
8433 | } |
8434 | |
8435 | case Type::FunctionNoProto: |
8436 | case Type::FunctionProto: |
8437 | S += '?'; |
8438 | return; |
8439 | |
8440 | case Type::Record: { |
8441 | RecordDecl *RDecl = cast<RecordType>(Val&: CT)->getDecl(); |
8442 | S += RDecl->isUnion() ? '(' : '{'; |
8443 | // Anonymous structures print as '?' |
8444 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
8445 | S += II->getName(); |
8446 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(Val: RDecl)) { |
8447 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
8448 | llvm::raw_string_ostream OS(S); |
8449 | printTemplateArgumentList(OS, Args: TemplateArgs.asArray(), |
8450 | Policy: getPrintingPolicy()); |
8451 | } |
8452 | } else { |
8453 | S += '?'; |
8454 | } |
8455 | if (Options.ExpandStructures()) { |
8456 | S += '='; |
8457 | if (!RDecl->isUnion()) { |
8458 | getObjCEncodingForStructureImpl(RD: RDecl, S, Field: FD, includeVBases: true, NotEncodedT); |
8459 | } else { |
8460 | for (const auto *Field : RDecl->fields()) { |
8461 | if (FD) { |
8462 | S += '"'; |
8463 | S += Field->getNameAsString(); |
8464 | S += '"'; |
8465 | } |
8466 | |
8467 | // Special case bit-fields. |
8468 | if (Field->isBitField()) { |
8469 | getObjCEncodingForTypeImpl(T: Field->getType(), S, |
8470 | Options: ObjCEncOptions().setExpandStructures(), |
8471 | FD: Field); |
8472 | } else { |
8473 | QualType qt = Field->getType(); |
8474 | getLegacyIntegralTypeEncoding(PointeeTy&: qt); |
8475 | getObjCEncodingForTypeImpl( |
8476 | T: qt, S, |
8477 | Options: ObjCEncOptions().setExpandStructures().setIsStructField(), FD, |
8478 | NotEncodedT); |
8479 | } |
8480 | } |
8481 | } |
8482 | } |
8483 | S += RDecl->isUnion() ? ')' : '}'; |
8484 | return; |
8485 | } |
8486 | |
8487 | case Type::BlockPointer: { |
8488 | const auto *BT = T->castAs<BlockPointerType>(); |
8489 | S += "@?" ; // Unlike a pointer-to-function, which is "^?". |
8490 | if (Options.EncodeBlockParameters()) { |
8491 | const auto *FT = BT->getPointeeType()->castAs<FunctionType>(); |
8492 | |
8493 | S += '<'; |
8494 | // Block return type |
8495 | getObjCEncodingForTypeImpl(T: FT->getReturnType(), S, |
8496 | Options: Options.forComponentType(), FD, NotEncodedT); |
8497 | // Block self |
8498 | S += "@?" ; |
8499 | // Block parameters |
8500 | if (const auto *FPT = dyn_cast<FunctionProtoType>(Val: FT)) { |
8501 | for (const auto &I : FPT->param_types()) |
8502 | getObjCEncodingForTypeImpl(T: I, S, Options: Options.forComponentType(), FD, |
8503 | NotEncodedT); |
8504 | } |
8505 | S += '>'; |
8506 | } |
8507 | return; |
8508 | } |
8509 | |
8510 | case Type::ObjCObject: { |
8511 | // hack to match legacy encoding of *id and *Class |
8512 | QualType Ty = getObjCObjectPointerType(ObjectT: CT); |
8513 | if (Ty->isObjCIdType()) { |
8514 | S += "{objc_object=}" ; |
8515 | return; |
8516 | } |
8517 | else if (Ty->isObjCClassType()) { |
8518 | S += "{objc_class=}" ; |
8519 | return; |
8520 | } |
8521 | // TODO: Double check to make sure this intentionally falls through. |
8522 | [[fallthrough]]; |
8523 | } |
8524 | |
8525 | case Type::ObjCInterface: { |
8526 | // Ignore protocol qualifiers when mangling at this level. |
8527 | // @encode(class_name) |
8528 | ObjCInterfaceDecl *OI = T->castAs<ObjCObjectType>()->getInterface(); |
8529 | S += '{'; |
8530 | S += OI->getObjCRuntimeNameAsString(); |
8531 | if (Options.ExpandStructures()) { |
8532 | S += '='; |
8533 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
8534 | DeepCollectObjCIvars(OI, leafClass: true, Ivars); |
8535 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
8536 | const FieldDecl *Field = Ivars[i]; |
8537 | if (Field->isBitField()) |
8538 | getObjCEncodingForTypeImpl(T: Field->getType(), S, |
8539 | Options: ObjCEncOptions().setExpandStructures(), |
8540 | FD: Field); |
8541 | else |
8542 | getObjCEncodingForTypeImpl(T: Field->getType(), S, |
8543 | Options: ObjCEncOptions().setExpandStructures(), FD, |
8544 | NotEncodedT); |
8545 | } |
8546 | } |
8547 | S += '}'; |
8548 | return; |
8549 | } |
8550 | |
8551 | case Type::ObjCObjectPointer: { |
8552 | const auto *OPT = T->castAs<ObjCObjectPointerType>(); |
8553 | if (OPT->isObjCIdType()) { |
8554 | S += '@'; |
8555 | return; |
8556 | } |
8557 | |
8558 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
8559 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
8560 | // Since this is a binary compatibility issue, need to consult with |
8561 | // runtime folks. Fortunately, this is a *very* obscure construct. |
8562 | S += '#'; |
8563 | return; |
8564 | } |
8565 | |
8566 | if (OPT->isObjCQualifiedIdType()) { |
8567 | getObjCEncodingForTypeImpl( |
8568 | T: getObjCIdType(), S, |
8569 | Options: Options.keepingOnly(Mask: ObjCEncOptions() |
8570 | .setExpandPointedToStructures() |
8571 | .setExpandStructures()), |
8572 | FD); |
8573 | if (FD || Options.EncodingProperty() || Options.EncodeClassNames()) { |
8574 | // Note that we do extended encoding of protocol qualifier list |
8575 | // Only when doing ivar or property encoding. |
8576 | S += '"'; |
8577 | for (const auto *I : OPT->quals()) { |
8578 | S += '<'; |
8579 | S += I->getObjCRuntimeNameAsString(); |
8580 | S += '>'; |
8581 | } |
8582 | S += '"'; |
8583 | } |
8584 | return; |
8585 | } |
8586 | |
8587 | S += '@'; |
8588 | if (OPT->getInterfaceDecl() && |
8589 | (FD || Options.EncodingProperty() || Options.EncodeClassNames())) { |
8590 | S += '"'; |
8591 | S += OPT->getInterfaceDecl()->getObjCRuntimeNameAsString(); |
8592 | for (const auto *I : OPT->quals()) { |
8593 | S += '<'; |
8594 | S += I->getObjCRuntimeNameAsString(); |
8595 | S += '>'; |
8596 | } |
8597 | S += '"'; |
8598 | } |
8599 | return; |
8600 | } |
8601 | |
8602 | // gcc just blithely ignores member pointers. |
8603 | // FIXME: we should do better than that. 'M' is available. |
8604 | case Type::MemberPointer: |
8605 | // This matches gcc's encoding, even though technically it is insufficient. |
8606 | //FIXME. We should do a better job than gcc. |
8607 | case Type::Vector: |
8608 | case Type::ExtVector: |
8609 | // Until we have a coherent encoding of these three types, issue warning. |
8610 | if (NotEncodedT) |
8611 | *NotEncodedT = T; |
8612 | return; |
8613 | |
8614 | case Type::ConstantMatrix: |
8615 | if (NotEncodedT) |
8616 | *NotEncodedT = T; |
8617 | return; |
8618 | |
8619 | case Type::BitInt: |
8620 | if (NotEncodedT) |
8621 | *NotEncodedT = T; |
8622 | return; |
8623 | |
8624 | // We could see an undeduced auto type here during error recovery. |
8625 | // Just ignore it. |
8626 | case Type::Auto: |
8627 | case Type::DeducedTemplateSpecialization: |
8628 | return; |
8629 | |
8630 | case Type::ArrayParameter: |
8631 | case Type::Pipe: |
8632 | #define ABSTRACT_TYPE(KIND, BASE) |
8633 | #define TYPE(KIND, BASE) |
8634 | #define DEPENDENT_TYPE(KIND, BASE) \ |
8635 | case Type::KIND: |
8636 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
8637 | case Type::KIND: |
8638 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
8639 | case Type::KIND: |
8640 | #include "clang/AST/TypeNodes.inc" |
8641 | llvm_unreachable("@encode for dependent type!" ); |
8642 | } |
8643 | llvm_unreachable("bad type kind!" ); |
8644 | } |
8645 | |
8646 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
8647 | std::string &S, |
8648 | const FieldDecl *FD, |
8649 | bool includeVBases, |
8650 | QualType *NotEncodedT) const { |
8651 | assert(RDecl && "Expected non-null RecordDecl" ); |
8652 | assert(!RDecl->isUnion() && "Should not be called for unions" ); |
8653 | if (!RDecl->getDefinition() || RDecl->getDefinition()->isInvalidDecl()) |
8654 | return; |
8655 | |
8656 | const auto *CXXRec = dyn_cast<CXXRecordDecl>(Val: RDecl); |
8657 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
8658 | const ASTRecordLayout &layout = getASTRecordLayout(D: RDecl); |
8659 | |
8660 | if (CXXRec) { |
8661 | for (const auto &BI : CXXRec->bases()) { |
8662 | if (!BI.isVirtual()) { |
8663 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
8664 | if (base->isEmpty()) |
8665 | continue; |
8666 | uint64_t offs = toBits(CharSize: layout.getBaseClassOffset(Base: base)); |
8667 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(x: offs), |
8668 | std::make_pair(x&: offs, y&: base)); |
8669 | } |
8670 | } |
8671 | } |
8672 | |
8673 | for (FieldDecl *Field : RDecl->fields()) { |
8674 | if (!Field->isZeroLengthBitField(Ctx: *this) && Field->isZeroSize(Ctx: *this)) |
8675 | continue; |
8676 | uint64_t offs = layout.getFieldOffset(FieldNo: Field->getFieldIndex()); |
8677 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(x: offs), |
8678 | std::make_pair(x&: offs, y&: Field)); |
8679 | } |
8680 | |
8681 | if (CXXRec && includeVBases) { |
8682 | for (const auto &BI : CXXRec->vbases()) { |
8683 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
8684 | if (base->isEmpty()) |
8685 | continue; |
8686 | uint64_t offs = toBits(CharSize: layout.getVBaseClassOffset(VBase: base)); |
8687 | if (offs >= uint64_t(toBits(CharSize: layout.getNonVirtualSize())) && |
8688 | FieldOrBaseOffsets.find(x: offs) == FieldOrBaseOffsets.end()) |
8689 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
8690 | std::make_pair(x&: offs, y&: base)); |
8691 | } |
8692 | } |
8693 | |
8694 | CharUnits size; |
8695 | if (CXXRec) { |
8696 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
8697 | } else { |
8698 | size = layout.getSize(); |
8699 | } |
8700 | |
8701 | #ifndef NDEBUG |
8702 | uint64_t CurOffs = 0; |
8703 | #endif |
8704 | std::multimap<uint64_t, NamedDecl *>::iterator |
8705 | CurLayObj = FieldOrBaseOffsets.begin(); |
8706 | |
8707 | if (CXXRec && CXXRec->isDynamicClass() && |
8708 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
8709 | if (FD) { |
8710 | S += "\"_vptr$" ; |
8711 | std::string recname = CXXRec->getNameAsString(); |
8712 | if (recname.empty()) recname = "?" ; |
8713 | S += recname; |
8714 | S += '"'; |
8715 | } |
8716 | S += "^^?" ; |
8717 | #ifndef NDEBUG |
8718 | CurOffs += getTypeSize(VoidPtrTy); |
8719 | #endif |
8720 | } |
8721 | |
8722 | if (!RDecl->hasFlexibleArrayMember()) { |
8723 | // Mark the end of the structure. |
8724 | uint64_t offs = toBits(CharSize: size); |
8725 | FieldOrBaseOffsets.insert(position: FieldOrBaseOffsets.upper_bound(x: offs), |
8726 | x: std::make_pair(x&: offs, y: nullptr)); |
8727 | } |
8728 | |
8729 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
8730 | #ifndef NDEBUG |
8731 | assert(CurOffs <= CurLayObj->first); |
8732 | if (CurOffs < CurLayObj->first) { |
8733 | uint64_t padding = CurLayObj->first - CurOffs; |
8734 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
8735 | // packing/alignment of members is different that normal, in which case |
8736 | // the encoding will be out-of-sync with the real layout. |
8737 | // If the runtime switches to just consider the size of types without |
8738 | // taking into account alignment, we could make padding explicit in the |
8739 | // encoding (e.g. using arrays of chars). The encoding strings would be |
8740 | // longer then though. |
8741 | CurOffs += padding; |
8742 | } |
8743 | #endif |
8744 | |
8745 | NamedDecl *dcl = CurLayObj->second; |
8746 | if (!dcl) |
8747 | break; // reached end of structure. |
8748 | |
8749 | if (auto *base = dyn_cast<CXXRecordDecl>(Val: dcl)) { |
8750 | // We expand the bases without their virtual bases since those are going |
8751 | // in the initial structure. Note that this differs from gcc which |
8752 | // expands virtual bases each time one is encountered in the hierarchy, |
8753 | // making the encoding type bigger than it really is. |
8754 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false, |
8755 | NotEncodedT); |
8756 | assert(!base->isEmpty()); |
8757 | #ifndef NDEBUG |
8758 | CurOffs += toBits(CharSize: getASTRecordLayout(base).getNonVirtualSize()); |
8759 | #endif |
8760 | } else { |
8761 | const auto *field = cast<FieldDecl>(Val: dcl); |
8762 | if (FD) { |
8763 | S += '"'; |
8764 | S += field->getNameAsString(); |
8765 | S += '"'; |
8766 | } |
8767 | |
8768 | if (field->isBitField()) { |
8769 | EncodeBitField(this, S, field->getType(), field); |
8770 | #ifndef NDEBUG |
8771 | CurOffs += field->getBitWidthValue(Ctx: *this); |
8772 | #endif |
8773 | } else { |
8774 | QualType qt = field->getType(); |
8775 | getLegacyIntegralTypeEncoding(PointeeTy&: qt); |
8776 | getObjCEncodingForTypeImpl( |
8777 | T: qt, S, Options: ObjCEncOptions().setExpandStructures().setIsStructField(), |
8778 | FD, NotEncodedT); |
8779 | #ifndef NDEBUG |
8780 | CurOffs += getTypeSize(field->getType()); |
8781 | #endif |
8782 | } |
8783 | } |
8784 | } |
8785 | } |
8786 | |
8787 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
8788 | std::string& S) const { |
8789 | if (QT & Decl::OBJC_TQ_In) |
8790 | S += 'n'; |
8791 | if (QT & Decl::OBJC_TQ_Inout) |
8792 | S += 'N'; |
8793 | if (QT & Decl::OBJC_TQ_Out) |
8794 | S += 'o'; |
8795 | if (QT & Decl::OBJC_TQ_Bycopy) |
8796 | S += 'O'; |
8797 | if (QT & Decl::OBJC_TQ_Byref) |
8798 | S += 'R'; |
8799 | if (QT & Decl::OBJC_TQ_Oneway) |
8800 | S += 'V'; |
8801 | } |
8802 | |
8803 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
8804 | if (!ObjCIdDecl) { |
8805 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, {}, {}); |
8806 | T = getObjCObjectPointerType(ObjectT: T); |
8807 | ObjCIdDecl = buildImplicitTypedef(T, Name: "id" ); |
8808 | } |
8809 | return ObjCIdDecl; |
8810 | } |
8811 | |
8812 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
8813 | if (!ObjCSelDecl) { |
8814 | QualType T = getPointerType(ObjCBuiltinSelTy); |
8815 | ObjCSelDecl = buildImplicitTypedef(T, Name: "SEL" ); |
8816 | } |
8817 | return ObjCSelDecl; |
8818 | } |
8819 | |
8820 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
8821 | if (!ObjCClassDecl) { |
8822 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, {}, {}); |
8823 | T = getObjCObjectPointerType(ObjectT: T); |
8824 | ObjCClassDecl = buildImplicitTypedef(T, Name: "Class" ); |
8825 | } |
8826 | return ObjCClassDecl; |
8827 | } |
8828 | |
8829 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
8830 | if (!ObjCProtocolClassDecl) { |
8831 | ObjCProtocolClassDecl |
8832 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
8833 | SourceLocation(), |
8834 | &Idents.get(Name: "Protocol" ), |
8835 | /*typeParamList=*/nullptr, |
8836 | /*PrevDecl=*/nullptr, |
8837 | SourceLocation(), true); |
8838 | } |
8839 | |
8840 | return ObjCProtocolClassDecl; |
8841 | } |
8842 | |
8843 | //===----------------------------------------------------------------------===// |
8844 | // __builtin_va_list Construction Functions |
8845 | //===----------------------------------------------------------------------===// |
8846 | |
8847 | static TypedefDecl *CreateCharPtrNamedVaListDecl(const ASTContext *Context, |
8848 | StringRef Name) { |
8849 | // typedef char* __builtin[_ms]_va_list; |
8850 | QualType T = Context->getPointerType(Context->CharTy); |
8851 | return Context->buildImplicitTypedef(T, Name); |
8852 | } |
8853 | |
8854 | static TypedefDecl *CreateMSVaListDecl(const ASTContext *Context) { |
8855 | return CreateCharPtrNamedVaListDecl(Context, Name: "__builtin_ms_va_list" ); |
8856 | } |
8857 | |
8858 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
8859 | return CreateCharPtrNamedVaListDecl(Context, Name: "__builtin_va_list" ); |
8860 | } |
8861 | |
8862 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
8863 | // typedef void* __builtin_va_list; |
8864 | QualType T = Context->getPointerType(Context->VoidTy); |
8865 | return Context->buildImplicitTypedef(T, Name: "__builtin_va_list" ); |
8866 | } |
8867 | |
8868 | static TypedefDecl * |
8869 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
8870 | // struct __va_list |
8871 | RecordDecl *VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list" ); |
8872 | if (Context->getLangOpts().CPlusPlus) { |
8873 | // namespace std { struct __va_list { |
8874 | auto *NS = NamespaceDecl::Create( |
8875 | const_cast<ASTContext &>(*Context), Context->getTranslationUnitDecl(), |
8876 | /*Inline=*/false, SourceLocation(), SourceLocation(), |
8877 | &Context->Idents.get(Name: "std" ), |
8878 | /*PrevDecl=*/nullptr, /*Nested=*/false); |
8879 | NS->setImplicit(); |
8880 | VaListTagDecl->setDeclContext(NS); |
8881 | } |
8882 | |
8883 | VaListTagDecl->startDefinition(); |
8884 | |
8885 | const size_t NumFields = 5; |
8886 | QualType FieldTypes[NumFields]; |
8887 | const char *FieldNames[NumFields]; |
8888 | |
8889 | // void *__stack; |
8890 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
8891 | FieldNames[0] = "__stack" ; |
8892 | |
8893 | // void *__gr_top; |
8894 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
8895 | FieldNames[1] = "__gr_top" ; |
8896 | |
8897 | // void *__vr_top; |
8898 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
8899 | FieldNames[2] = "__vr_top" ; |
8900 | |
8901 | // int __gr_offs; |
8902 | FieldTypes[3] = Context->IntTy; |
8903 | FieldNames[3] = "__gr_offs" ; |
8904 | |
8905 | // int __vr_offs; |
8906 | FieldTypes[4] = Context->IntTy; |
8907 | FieldNames[4] = "__vr_offs" ; |
8908 | |
8909 | // Create fields |
8910 | for (unsigned i = 0; i < NumFields; ++i) { |
8911 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
8912 | VaListTagDecl, |
8913 | SourceLocation(), |
8914 | SourceLocation(), |
8915 | &Context->Idents.get(Name: FieldNames[i]), |
8916 | FieldTypes[i], /*TInfo=*/nullptr, |
8917 | /*BitWidth=*/nullptr, |
8918 | /*Mutable=*/false, |
8919 | ICIS_NoInit); |
8920 | Field->setAccess(AS_public); |
8921 | VaListTagDecl->addDecl(Field); |
8922 | } |
8923 | VaListTagDecl->completeDefinition(); |
8924 | Context->VaListTagDecl = VaListTagDecl; |
8925 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
8926 | |
8927 | // } __builtin_va_list; |
8928 | return Context->buildImplicitTypedef(T: VaListTagType, Name: "__builtin_va_list" ); |
8929 | } |
8930 | |
8931 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
8932 | // typedef struct __va_list_tag { |
8933 | RecordDecl *VaListTagDecl; |
8934 | |
8935 | VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list_tag" ); |
8936 | VaListTagDecl->startDefinition(); |
8937 | |
8938 | const size_t NumFields = 5; |
8939 | QualType FieldTypes[NumFields]; |
8940 | const char *FieldNames[NumFields]; |
8941 | |
8942 | // unsigned char gpr; |
8943 | FieldTypes[0] = Context->UnsignedCharTy; |
8944 | FieldNames[0] = "gpr" ; |
8945 | |
8946 | // unsigned char fpr; |
8947 | FieldTypes[1] = Context->UnsignedCharTy; |
8948 | FieldNames[1] = "fpr" ; |
8949 | |
8950 | // unsigned short reserved; |
8951 | FieldTypes[2] = Context->UnsignedShortTy; |
8952 | FieldNames[2] = "reserved" ; |
8953 | |
8954 | // void* overflow_arg_area; |
8955 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
8956 | FieldNames[3] = "overflow_arg_area" ; |
8957 | |
8958 | // void* reg_save_area; |
8959 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
8960 | FieldNames[4] = "reg_save_area" ; |
8961 | |
8962 | // Create fields |
8963 | for (unsigned i = 0; i < NumFields; ++i) { |
8964 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
8965 | SourceLocation(), |
8966 | SourceLocation(), |
8967 | &Context->Idents.get(Name: FieldNames[i]), |
8968 | FieldTypes[i], /*TInfo=*/nullptr, |
8969 | /*BitWidth=*/nullptr, |
8970 | /*Mutable=*/false, |
8971 | ICIS_NoInit); |
8972 | Field->setAccess(AS_public); |
8973 | VaListTagDecl->addDecl(Field); |
8974 | } |
8975 | VaListTagDecl->completeDefinition(); |
8976 | Context->VaListTagDecl = VaListTagDecl; |
8977 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
8978 | |
8979 | // } __va_list_tag; |
8980 | TypedefDecl *VaListTagTypedefDecl = |
8981 | Context->buildImplicitTypedef(T: VaListTagType, Name: "__va_list_tag" ); |
8982 | |
8983 | QualType VaListTagTypedefType = |
8984 | Context->getTypedefType(VaListTagTypedefDecl); |
8985 | |
8986 | // typedef __va_list_tag __builtin_va_list[1]; |
8987 | llvm::APInt Size(Context->getTypeSize(T: Context->getSizeType()), 1); |
8988 | QualType VaListTagArrayType = Context->getConstantArrayType( |
8989 | EltTy: VaListTagTypedefType, ArySizeIn: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
8990 | return Context->buildImplicitTypedef(T: VaListTagArrayType, Name: "__builtin_va_list" ); |
8991 | } |
8992 | |
8993 | static TypedefDecl * |
8994 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
8995 | // struct __va_list_tag { |
8996 | RecordDecl *VaListTagDecl; |
8997 | VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list_tag" ); |
8998 | VaListTagDecl->startDefinition(); |
8999 | |
9000 | const size_t NumFields = 4; |
9001 | QualType FieldTypes[NumFields]; |
9002 | const char *FieldNames[NumFields]; |
9003 | |
9004 | // unsigned gp_offset; |
9005 | FieldTypes[0] = Context->UnsignedIntTy; |
9006 | FieldNames[0] = "gp_offset" ; |
9007 | |
9008 | // unsigned fp_offset; |
9009 | FieldTypes[1] = Context->UnsignedIntTy; |
9010 | FieldNames[1] = "fp_offset" ; |
9011 | |
9012 | // void* overflow_arg_area; |
9013 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
9014 | FieldNames[2] = "overflow_arg_area" ; |
9015 | |
9016 | // void* reg_save_area; |
9017 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
9018 | FieldNames[3] = "reg_save_area" ; |
9019 | |
9020 | // Create fields |
9021 | for (unsigned i = 0; i < NumFields; ++i) { |
9022 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
9023 | VaListTagDecl, |
9024 | SourceLocation(), |
9025 | SourceLocation(), |
9026 | &Context->Idents.get(Name: FieldNames[i]), |
9027 | FieldTypes[i], /*TInfo=*/nullptr, |
9028 | /*BitWidth=*/nullptr, |
9029 | /*Mutable=*/false, |
9030 | ICIS_NoInit); |
9031 | Field->setAccess(AS_public); |
9032 | VaListTagDecl->addDecl(Field); |
9033 | } |
9034 | VaListTagDecl->completeDefinition(); |
9035 | Context->VaListTagDecl = VaListTagDecl; |
9036 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
9037 | |
9038 | // }; |
9039 | |
9040 | // typedef struct __va_list_tag __builtin_va_list[1]; |
9041 | llvm::APInt Size(Context->getTypeSize(T: Context->getSizeType()), 1); |
9042 | QualType VaListTagArrayType = Context->getConstantArrayType( |
9043 | EltTy: VaListTagType, ArySizeIn: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9044 | return Context->buildImplicitTypedef(T: VaListTagArrayType, Name: "__builtin_va_list" ); |
9045 | } |
9046 | |
9047 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
9048 | // typedef int __builtin_va_list[4]; |
9049 | llvm::APInt Size(Context->getTypeSize(T: Context->getSizeType()), 4); |
9050 | QualType IntArrayType = Context->getConstantArrayType( |
9051 | EltTy: Context->IntTy, ArySizeIn: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9052 | return Context->buildImplicitTypedef(T: IntArrayType, Name: "__builtin_va_list" ); |
9053 | } |
9054 | |
9055 | static TypedefDecl * |
9056 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
9057 | // struct __va_list |
9058 | RecordDecl *VaListDecl = Context->buildImplicitRecord(Name: "__va_list" ); |
9059 | if (Context->getLangOpts().CPlusPlus) { |
9060 | // namespace std { struct __va_list { |
9061 | NamespaceDecl *NS; |
9062 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
9063 | Context->getTranslationUnitDecl(), |
9064 | /*Inline=*/false, SourceLocation(), |
9065 | SourceLocation(), &Context->Idents.get(Name: "std" ), |
9066 | /*PrevDecl=*/nullptr, /*Nested=*/false); |
9067 | NS->setImplicit(); |
9068 | VaListDecl->setDeclContext(NS); |
9069 | } |
9070 | |
9071 | VaListDecl->startDefinition(); |
9072 | |
9073 | // void * __ap; |
9074 | FieldDecl *Field = FieldDecl::Create(C: const_cast<ASTContext &>(*Context), |
9075 | DC: VaListDecl, |
9076 | StartLoc: SourceLocation(), |
9077 | IdLoc: SourceLocation(), |
9078 | Id: &Context->Idents.get(Name: "__ap" ), |
9079 | T: Context->getPointerType(Context->VoidTy), |
9080 | /*TInfo=*/nullptr, |
9081 | /*BitWidth=*/BW: nullptr, |
9082 | /*Mutable=*/false, |
9083 | InitStyle: ICIS_NoInit); |
9084 | Field->setAccess(AS_public); |
9085 | VaListDecl->addDecl(Field); |
9086 | |
9087 | // }; |
9088 | VaListDecl->completeDefinition(); |
9089 | Context->VaListTagDecl = VaListDecl; |
9090 | |
9091 | // typedef struct __va_list __builtin_va_list; |
9092 | QualType T = Context->getRecordType(Decl: VaListDecl); |
9093 | return Context->buildImplicitTypedef(T, Name: "__builtin_va_list" ); |
9094 | } |
9095 | |
9096 | static TypedefDecl * |
9097 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { |
9098 | // struct __va_list_tag { |
9099 | RecordDecl *VaListTagDecl; |
9100 | VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list_tag" ); |
9101 | VaListTagDecl->startDefinition(); |
9102 | |
9103 | const size_t NumFields = 4; |
9104 | QualType FieldTypes[NumFields]; |
9105 | const char *FieldNames[NumFields]; |
9106 | |
9107 | // long __gpr; |
9108 | FieldTypes[0] = Context->LongTy; |
9109 | FieldNames[0] = "__gpr" ; |
9110 | |
9111 | // long __fpr; |
9112 | FieldTypes[1] = Context->LongTy; |
9113 | FieldNames[1] = "__fpr" ; |
9114 | |
9115 | // void *__overflow_arg_area; |
9116 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
9117 | FieldNames[2] = "__overflow_arg_area" ; |
9118 | |
9119 | // void *__reg_save_area; |
9120 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
9121 | FieldNames[3] = "__reg_save_area" ; |
9122 | |
9123 | // Create fields |
9124 | for (unsigned i = 0; i < NumFields; ++i) { |
9125 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
9126 | VaListTagDecl, |
9127 | SourceLocation(), |
9128 | SourceLocation(), |
9129 | &Context->Idents.get(Name: FieldNames[i]), |
9130 | FieldTypes[i], /*TInfo=*/nullptr, |
9131 | /*BitWidth=*/nullptr, |
9132 | /*Mutable=*/false, |
9133 | ICIS_NoInit); |
9134 | Field->setAccess(AS_public); |
9135 | VaListTagDecl->addDecl(Field); |
9136 | } |
9137 | VaListTagDecl->completeDefinition(); |
9138 | Context->VaListTagDecl = VaListTagDecl; |
9139 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
9140 | |
9141 | // }; |
9142 | |
9143 | // typedef __va_list_tag __builtin_va_list[1]; |
9144 | llvm::APInt Size(Context->getTypeSize(T: Context->getSizeType()), 1); |
9145 | QualType VaListTagArrayType = Context->getConstantArrayType( |
9146 | EltTy: VaListTagType, ArySizeIn: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9147 | |
9148 | return Context->buildImplicitTypedef(T: VaListTagArrayType, Name: "__builtin_va_list" ); |
9149 | } |
9150 | |
9151 | static TypedefDecl *CreateHexagonBuiltinVaListDecl(const ASTContext *Context) { |
9152 | // typedef struct __va_list_tag { |
9153 | RecordDecl *VaListTagDecl; |
9154 | VaListTagDecl = Context->buildImplicitRecord(Name: "__va_list_tag" ); |
9155 | VaListTagDecl->startDefinition(); |
9156 | |
9157 | const size_t NumFields = 3; |
9158 | QualType FieldTypes[NumFields]; |
9159 | const char *FieldNames[NumFields]; |
9160 | |
9161 | // void *CurrentSavedRegisterArea; |
9162 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
9163 | FieldNames[0] = "__current_saved_reg_area_pointer" ; |
9164 | |
9165 | // void *SavedRegAreaEnd; |
9166 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
9167 | FieldNames[1] = "__saved_reg_area_end_pointer" ; |
9168 | |
9169 | // void *OverflowArea; |
9170 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
9171 | FieldNames[2] = "__overflow_area_pointer" ; |
9172 | |
9173 | // Create fields |
9174 | for (unsigned i = 0; i < NumFields; ++i) { |
9175 | FieldDecl *Field = FieldDecl::Create( |
9176 | const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(), |
9177 | SourceLocation(), &Context->Idents.get(Name: FieldNames[i]), FieldTypes[i], |
9178 | /*TInfo=*/nullptr, |
9179 | /*BitWidth=*/nullptr, |
9180 | /*Mutable=*/false, ICIS_NoInit); |
9181 | Field->setAccess(AS_public); |
9182 | VaListTagDecl->addDecl(Field); |
9183 | } |
9184 | VaListTagDecl->completeDefinition(); |
9185 | Context->VaListTagDecl = VaListTagDecl; |
9186 | QualType VaListTagType = Context->getRecordType(Decl: VaListTagDecl); |
9187 | |
9188 | // } __va_list_tag; |
9189 | TypedefDecl *VaListTagTypedefDecl = |
9190 | Context->buildImplicitTypedef(T: VaListTagType, Name: "__va_list_tag" ); |
9191 | |
9192 | QualType VaListTagTypedefType = Context->getTypedefType(VaListTagTypedefDecl); |
9193 | |
9194 | // typedef __va_list_tag __builtin_va_list[1]; |
9195 | llvm::APInt Size(Context->getTypeSize(T: Context->getSizeType()), 1); |
9196 | QualType VaListTagArrayType = Context->getConstantArrayType( |
9197 | EltTy: VaListTagTypedefType, ArySizeIn: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9198 | |
9199 | return Context->buildImplicitTypedef(T: VaListTagArrayType, Name: "__builtin_va_list" ); |
9200 | } |
9201 | |
9202 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
9203 | TargetInfo::BuiltinVaListKind Kind) { |
9204 | switch (Kind) { |
9205 | case TargetInfo::CharPtrBuiltinVaList: |
9206 | return CreateCharPtrBuiltinVaListDecl(Context); |
9207 | case TargetInfo::VoidPtrBuiltinVaList: |
9208 | return CreateVoidPtrBuiltinVaListDecl(Context); |
9209 | case TargetInfo::AArch64ABIBuiltinVaList: |
9210 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
9211 | case TargetInfo::PowerABIBuiltinVaList: |
9212 | return CreatePowerABIBuiltinVaListDecl(Context); |
9213 | case TargetInfo::X86_64ABIBuiltinVaList: |
9214 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
9215 | case TargetInfo::PNaClABIBuiltinVaList: |
9216 | return CreatePNaClABIBuiltinVaListDecl(Context); |
9217 | case TargetInfo::AAPCSABIBuiltinVaList: |
9218 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
9219 | case TargetInfo::SystemZBuiltinVaList: |
9220 | return CreateSystemZBuiltinVaListDecl(Context); |
9221 | case TargetInfo::HexagonBuiltinVaList: |
9222 | return CreateHexagonBuiltinVaListDecl(Context); |
9223 | } |
9224 | |
9225 | llvm_unreachable("Unhandled __builtin_va_list type kind" ); |
9226 | } |
9227 | |
9228 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
9229 | if (!BuiltinVaListDecl) { |
9230 | BuiltinVaListDecl = CreateVaListDecl(Context: this, Kind: Target->getBuiltinVaListKind()); |
9231 | assert(BuiltinVaListDecl->isImplicit()); |
9232 | } |
9233 | |
9234 | return BuiltinVaListDecl; |
9235 | } |
9236 | |
9237 | Decl *ASTContext::getVaListTagDecl() const { |
9238 | // Force the creation of VaListTagDecl by building the __builtin_va_list |
9239 | // declaration. |
9240 | if (!VaListTagDecl) |
9241 | (void)getBuiltinVaListDecl(); |
9242 | |
9243 | return VaListTagDecl; |
9244 | } |
9245 | |
9246 | TypedefDecl *ASTContext::getBuiltinMSVaListDecl() const { |
9247 | if (!BuiltinMSVaListDecl) |
9248 | BuiltinMSVaListDecl = CreateMSVaListDecl(Context: this); |
9249 | |
9250 | return BuiltinMSVaListDecl; |
9251 | } |
9252 | |
9253 | bool ASTContext::canBuiltinBeRedeclared(const FunctionDecl *FD) const { |
9254 | // Allow redecl custom type checking builtin for HLSL. |
9255 | if (LangOpts.HLSL && FD->getBuiltinID() != Builtin::NotBuiltin && |
9256 | BuiltinInfo.hasCustomTypechecking(ID: FD->getBuiltinID())) |
9257 | return true; |
9258 | return BuiltinInfo.canBeRedeclared(ID: FD->getBuiltinID()); |
9259 | } |
9260 | |
9261 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
9262 | assert(ObjCConstantStringType.isNull() && |
9263 | "'NSConstantString' type already set!" ); |
9264 | |
9265 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
9266 | } |
9267 | |
9268 | /// Retrieve the template name that corresponds to a non-empty |
9269 | /// lookup. |
9270 | TemplateName |
9271 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
9272 | UnresolvedSetIterator End) const { |
9273 | unsigned size = End - Begin; |
9274 | assert(size > 1 && "set is not overloaded!" ); |
9275 | |
9276 | void *memory = Allocate(Size: sizeof(OverloadedTemplateStorage) + |
9277 | size * sizeof(FunctionTemplateDecl*)); |
9278 | auto *OT = new (memory) OverloadedTemplateStorage(size); |
9279 | |
9280 | NamedDecl **Storage = OT->getStorage(); |
9281 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
9282 | NamedDecl *D = *I; |
9283 | assert(isa<FunctionTemplateDecl>(D) || |
9284 | isa<UnresolvedUsingValueDecl>(D) || |
9285 | (isa<UsingShadowDecl>(D) && |
9286 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
9287 | *Storage++ = D; |
9288 | } |
9289 | |
9290 | return TemplateName(OT); |
9291 | } |
9292 | |
9293 | /// Retrieve a template name representing an unqualified-id that has been |
9294 | /// assumed to name a template for ADL purposes. |
9295 | TemplateName ASTContext::getAssumedTemplateName(DeclarationName Name) const { |
9296 | auto *OT = new (*this) AssumedTemplateStorage(Name); |
9297 | return TemplateName(OT); |
9298 | } |
9299 | |
9300 | /// Retrieve the template name that represents a qualified |
9301 | /// template name such as \c std::vector. |
9302 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
9303 | bool TemplateKeyword, |
9304 | TemplateName Template) const { |
9305 | assert(NNS && "Missing nested-name-specifier in qualified template name" ); |
9306 | |
9307 | // FIXME: Canonicalization? |
9308 | llvm::FoldingSetNodeID ID; |
9309 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, TN: Template); |
9310 | |
9311 | void *InsertPos = nullptr; |
9312 | QualifiedTemplateName *QTN = |
9313 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
9314 | if (!QTN) { |
9315 | QTN = new (*this, alignof(QualifiedTemplateName)) |
9316 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
9317 | QualifiedTemplateNames.InsertNode(N: QTN, InsertPos); |
9318 | } |
9319 | |
9320 | return TemplateName(QTN); |
9321 | } |
9322 | |
9323 | /// Retrieve the template name that represents a dependent |
9324 | /// template name such as \c MetaFun::template apply. |
9325 | TemplateName |
9326 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
9327 | const IdentifierInfo *Name) const { |
9328 | assert((!NNS || NNS->isDependent()) && |
9329 | "Nested name specifier must be dependent" ); |
9330 | |
9331 | llvm::FoldingSetNodeID ID; |
9332 | DependentTemplateName::Profile(ID, NNS, Identifier: Name); |
9333 | |
9334 | void *InsertPos = nullptr; |
9335 | DependentTemplateName *QTN = |
9336 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
9337 | |
9338 | if (QTN) |
9339 | return TemplateName(QTN); |
9340 | |
9341 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
9342 | if (CanonNNS == NNS) { |
9343 | QTN = new (*this, alignof(DependentTemplateName)) |
9344 | DependentTemplateName(NNS, Name); |
9345 | } else { |
9346 | TemplateName Canon = getDependentTemplateName(NNS: CanonNNS, Name); |
9347 | QTN = new (*this, alignof(DependentTemplateName)) |
9348 | DependentTemplateName(NNS, Name, Canon); |
9349 | DependentTemplateName *CheckQTN = |
9350 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
9351 | assert(!CheckQTN && "Dependent type name canonicalization broken" ); |
9352 | (void)CheckQTN; |
9353 | } |
9354 | |
9355 | DependentTemplateNames.InsertNode(N: QTN, InsertPos); |
9356 | return TemplateName(QTN); |
9357 | } |
9358 | |
9359 | /// Retrieve the template name that represents a dependent |
9360 | /// template name such as \c MetaFun::template operator+. |
9361 | TemplateName |
9362 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
9363 | OverloadedOperatorKind Operator) const { |
9364 | assert((!NNS || NNS->isDependent()) && |
9365 | "Nested name specifier must be dependent" ); |
9366 | |
9367 | llvm::FoldingSetNodeID ID; |
9368 | DependentTemplateName::Profile(ID, NNS, Operator); |
9369 | |
9370 | void *InsertPos = nullptr; |
9371 | DependentTemplateName *QTN |
9372 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
9373 | |
9374 | if (QTN) |
9375 | return TemplateName(QTN); |
9376 | |
9377 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
9378 | if (CanonNNS == NNS) { |
9379 | QTN = new (*this, alignof(DependentTemplateName)) |
9380 | DependentTemplateName(NNS, Operator); |
9381 | } else { |
9382 | TemplateName Canon = getDependentTemplateName(NNS: CanonNNS, Operator); |
9383 | QTN = new (*this, alignof(DependentTemplateName)) |
9384 | DependentTemplateName(NNS, Operator, Canon); |
9385 | |
9386 | DependentTemplateName *CheckQTN |
9387 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
9388 | assert(!CheckQTN && "Dependent template name canonicalization broken" ); |
9389 | (void)CheckQTN; |
9390 | } |
9391 | |
9392 | DependentTemplateNames.InsertNode(N: QTN, InsertPos); |
9393 | return TemplateName(QTN); |
9394 | } |
9395 | |
9396 | TemplateName ASTContext::getSubstTemplateTemplateParm( |
9397 | TemplateName Replacement, Decl *AssociatedDecl, unsigned Index, |
9398 | std::optional<unsigned> PackIndex) const { |
9399 | llvm::FoldingSetNodeID ID; |
9400 | SubstTemplateTemplateParmStorage::Profile(ID, Replacement, AssociatedDecl, |
9401 | Index, PackIndex); |
9402 | |
9403 | void *insertPos = nullptr; |
9404 | SubstTemplateTemplateParmStorage *subst |
9405 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos&: insertPos); |
9406 | |
9407 | if (!subst) { |
9408 | subst = new (*this) SubstTemplateTemplateParmStorage( |
9409 | Replacement, AssociatedDecl, Index, PackIndex); |
9410 | SubstTemplateTemplateParms.InsertNode(N: subst, InsertPos: insertPos); |
9411 | } |
9412 | |
9413 | return TemplateName(subst); |
9414 | } |
9415 | |
9416 | TemplateName |
9417 | ASTContext::getSubstTemplateTemplateParmPack(const TemplateArgument &ArgPack, |
9418 | Decl *AssociatedDecl, |
9419 | unsigned Index, bool Final) const { |
9420 | auto &Self = const_cast<ASTContext &>(*this); |
9421 | llvm::FoldingSetNodeID ID; |
9422 | SubstTemplateTemplateParmPackStorage::Profile(ID, Context&: Self, ArgPack, |
9423 | AssociatedDecl, Index, Final); |
9424 | |
9425 | void *InsertPos = nullptr; |
9426 | SubstTemplateTemplateParmPackStorage *Subst |
9427 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
9428 | |
9429 | if (!Subst) { |
9430 | Subst = new (*this) SubstTemplateTemplateParmPackStorage( |
9431 | ArgPack.pack_elements(), AssociatedDecl, Index, Final); |
9432 | SubstTemplateTemplateParmPacks.InsertNode(N: Subst, InsertPos); |
9433 | } |
9434 | |
9435 | return TemplateName(Subst); |
9436 | } |
9437 | |
9438 | /// getFromTargetType - Given one of the integer types provided by |
9439 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
9440 | /// is actually a value of type @c TargetInfo::IntType. |
9441 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
9442 | switch (Type) { |
9443 | case TargetInfo::NoInt: return {}; |
9444 | case TargetInfo::SignedChar: return SignedCharTy; |
9445 | case TargetInfo::UnsignedChar: return UnsignedCharTy; |
9446 | case TargetInfo::SignedShort: return ShortTy; |
9447 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
9448 | case TargetInfo::SignedInt: return IntTy; |
9449 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
9450 | case TargetInfo::SignedLong: return LongTy; |
9451 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
9452 | case TargetInfo::SignedLongLong: return LongLongTy; |
9453 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
9454 | } |
9455 | |
9456 | llvm_unreachable("Unhandled TargetInfo::IntType value" ); |
9457 | } |
9458 | |
9459 | //===----------------------------------------------------------------------===// |
9460 | // Type Predicates. |
9461 | //===----------------------------------------------------------------------===// |
9462 | |
9463 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
9464 | /// garbage collection attribute. |
9465 | /// |
9466 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
9467 | if (getLangOpts().getGC() == LangOptions::NonGC) |
9468 | return Qualifiers::GCNone; |
9469 | |
9470 | assert(getLangOpts().ObjC); |
9471 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
9472 | |
9473 | // Default behaviour under objective-C's gc is for ObjC pointers |
9474 | // (or pointers to them) be treated as though they were declared |
9475 | // as __strong. |
9476 | if (GCAttrs == Qualifiers::GCNone) { |
9477 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
9478 | return Qualifiers::Strong; |
9479 | else if (Ty->isPointerType()) |
9480 | return getObjCGCAttrKind(Ty: Ty->castAs<PointerType>()->getPointeeType()); |
9481 | } else { |
9482 | // It's not valid to set GC attributes on anything that isn't a |
9483 | // pointer. |
9484 | #ifndef NDEBUG |
9485 | QualType CT = Ty->getCanonicalTypeInternal(); |
9486 | while (const auto *AT = dyn_cast<ArrayType>(Val&: CT)) |
9487 | CT = AT->getElementType(); |
9488 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
9489 | #endif |
9490 | } |
9491 | return GCAttrs; |
9492 | } |
9493 | |
9494 | //===----------------------------------------------------------------------===// |
9495 | // Type Compatibility Testing |
9496 | //===----------------------------------------------------------------------===// |
9497 | |
9498 | /// areCompatVectorTypes - Return true if the two specified vector types are |
9499 | /// compatible. |
9500 | static bool areCompatVectorTypes(const VectorType *LHS, |
9501 | const VectorType *RHS) { |
9502 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
9503 | return LHS->getElementType() == RHS->getElementType() && |
9504 | LHS->getNumElements() == RHS->getNumElements(); |
9505 | } |
9506 | |
9507 | /// areCompatMatrixTypes - Return true if the two specified matrix types are |
9508 | /// compatible. |
9509 | static bool areCompatMatrixTypes(const ConstantMatrixType *LHS, |
9510 | const ConstantMatrixType *RHS) { |
9511 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
9512 | return LHS->getElementType() == RHS->getElementType() && |
9513 | LHS->getNumRows() == RHS->getNumRows() && |
9514 | LHS->getNumColumns() == RHS->getNumColumns(); |
9515 | } |
9516 | |
9517 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
9518 | QualType SecondVec) { |
9519 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type" ); |
9520 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type" ); |
9521 | |
9522 | if (hasSameUnqualifiedType(T1: FirstVec, T2: SecondVec)) |
9523 | return true; |
9524 | |
9525 | // Treat Neon vector types and most AltiVec vector types as if they are the |
9526 | // equivalent GCC vector types. |
9527 | const auto *First = FirstVec->castAs<VectorType>(); |
9528 | const auto *Second = SecondVec->castAs<VectorType>(); |
9529 | if (First->getNumElements() == Second->getNumElements() && |
9530 | hasSameType(T1: First->getElementType(), T2: Second->getElementType()) && |
9531 | First->getVectorKind() != VectorKind::AltiVecPixel && |
9532 | First->getVectorKind() != VectorKind::AltiVecBool && |
9533 | Second->getVectorKind() != VectorKind::AltiVecPixel && |
9534 | Second->getVectorKind() != VectorKind::AltiVecBool && |
9535 | First->getVectorKind() != VectorKind::SveFixedLengthData && |
9536 | First->getVectorKind() != VectorKind::SveFixedLengthPredicate && |
9537 | Second->getVectorKind() != VectorKind::SveFixedLengthData && |
9538 | Second->getVectorKind() != VectorKind::SveFixedLengthPredicate && |
9539 | First->getVectorKind() != VectorKind::RVVFixedLengthData && |
9540 | Second->getVectorKind() != VectorKind::RVVFixedLengthData && |
9541 | First->getVectorKind() != VectorKind::RVVFixedLengthMask && |
9542 | Second->getVectorKind() != VectorKind::RVVFixedLengthMask) |
9543 | return true; |
9544 | |
9545 | return false; |
9546 | } |
9547 | |
9548 | /// getSVETypeSize - Return SVE vector or predicate register size. |
9549 | static uint64_t getSVETypeSize(ASTContext &Context, const BuiltinType *Ty) { |
9550 | assert(Ty->isSveVLSBuiltinType() && "Invalid SVE Type" ); |
9551 | if (Ty->getKind() == BuiltinType::SveBool || |
9552 | Ty->getKind() == BuiltinType::SveCount) |
9553 | return (Context.getLangOpts().VScaleMin * 128) / Context.getCharWidth(); |
9554 | return Context.getLangOpts().VScaleMin * 128; |
9555 | } |
9556 | |
9557 | bool ASTContext::areCompatibleSveTypes(QualType FirstType, |
9558 | QualType SecondType) { |
9559 | assert( |
9560 | ((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) || |
9561 | (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) && |
9562 | "Expected SVE builtin type and vector type!" ); |
9563 | |
9564 | auto IsValidCast = [this](QualType FirstType, QualType SecondType) { |
9565 | if (const auto *BT = FirstType->getAs<BuiltinType>()) { |
9566 | if (const auto *VT = SecondType->getAs<VectorType>()) { |
9567 | // Predicates have the same representation as uint8 so we also have to |
9568 | // check the kind to make these types incompatible. |
9569 | if (VT->getVectorKind() == VectorKind::SveFixedLengthPredicate) |
9570 | return BT->getKind() == BuiltinType::SveBool; |
9571 | else if (VT->getVectorKind() == VectorKind::SveFixedLengthData) |
9572 | return VT->getElementType().getCanonicalType() == |
9573 | FirstType->getSveEltType(Ctx: *this); |
9574 | else if (VT->getVectorKind() == VectorKind::Generic) |
9575 | return getTypeSize(SecondType) == getSVETypeSize(*this, BT) && |
9576 | hasSameType(VT->getElementType(), |
9577 | getBuiltinVectorTypeInfo(BT).ElementType); |
9578 | } |
9579 | } |
9580 | return false; |
9581 | }; |
9582 | |
9583 | return IsValidCast(FirstType, SecondType) || |
9584 | IsValidCast(SecondType, FirstType); |
9585 | } |
9586 | |
9587 | bool ASTContext::areLaxCompatibleSveTypes(QualType FirstType, |
9588 | QualType SecondType) { |
9589 | assert( |
9590 | ((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) || |
9591 | (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) && |
9592 | "Expected SVE builtin type and vector type!" ); |
9593 | |
9594 | auto IsLaxCompatible = [this](QualType FirstType, QualType SecondType) { |
9595 | const auto *BT = FirstType->getAs<BuiltinType>(); |
9596 | if (!BT) |
9597 | return false; |
9598 | |
9599 | const auto *VecTy = SecondType->getAs<VectorType>(); |
9600 | if (VecTy && (VecTy->getVectorKind() == VectorKind::SveFixedLengthData || |
9601 | VecTy->getVectorKind() == VectorKind::Generic)) { |
9602 | const LangOptions::LaxVectorConversionKind LVCKind = |
9603 | getLangOpts().getLaxVectorConversions(); |
9604 | |
9605 | // Can not convert between sve predicates and sve vectors because of |
9606 | // different size. |
9607 | if (BT->getKind() == BuiltinType::SveBool && |
9608 | VecTy->getVectorKind() == VectorKind::SveFixedLengthData) |
9609 | return false; |
9610 | |
9611 | // If __ARM_FEATURE_SVE_BITS != N do not allow GNU vector lax conversion. |
9612 | // "Whenever __ARM_FEATURE_SVE_BITS==N, GNUT implicitly |
9613 | // converts to VLAT and VLAT implicitly converts to GNUT." |
9614 | // ACLE Spec Version 00bet6, 3.7.3.2. Behavior common to vectors and |
9615 | // predicates. |
9616 | if (VecTy->getVectorKind() == VectorKind::Generic && |
9617 | getTypeSize(T: SecondType) != getSVETypeSize(Context&: *this, Ty: BT)) |
9618 | return false; |
9619 | |
9620 | // If -flax-vector-conversions=all is specified, the types are |
9621 | // certainly compatible. |
9622 | if (LVCKind == LangOptions::LaxVectorConversionKind::All) |
9623 | return true; |
9624 | |
9625 | // If -flax-vector-conversions=integer is specified, the types are |
9626 | // compatible if the elements are integer types. |
9627 | if (LVCKind == LangOptions::LaxVectorConversionKind::Integer) |
9628 | return VecTy->getElementType().getCanonicalType()->isIntegerType() && |
9629 | FirstType->getSveEltType(Ctx: *this)->isIntegerType(); |
9630 | } |
9631 | |
9632 | return false; |
9633 | }; |
9634 | |
9635 | return IsLaxCompatible(FirstType, SecondType) || |
9636 | IsLaxCompatible(SecondType, FirstType); |
9637 | } |
9638 | |
9639 | /// getRVVTypeSize - Return RVV vector register size. |
9640 | static uint64_t getRVVTypeSize(ASTContext &Context, const BuiltinType *Ty) { |
9641 | assert(Ty->isRVVVLSBuiltinType() && "Invalid RVV Type" ); |
9642 | auto VScale = Context.getTargetInfo().getVScaleRange(LangOpts: Context.getLangOpts()); |
9643 | if (!VScale) |
9644 | return 0; |
9645 | |
9646 | ASTContext::BuiltinVectorTypeInfo Info = Context.getBuiltinVectorTypeInfo(Ty); |
9647 | |
9648 | uint64_t EltSize = Context.getTypeSize(Info.ElementType); |
9649 | if (Info.ElementType == Context.BoolTy) |
9650 | EltSize = 1; |
9651 | |
9652 | uint64_t MinElts = Info.EC.getKnownMinValue(); |
9653 | return VScale->first * MinElts * EltSize; |
9654 | } |
9655 | |
9656 | bool ASTContext::areCompatibleRVVTypes(QualType FirstType, |
9657 | QualType SecondType) { |
9658 | assert( |
9659 | ((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || |
9660 | (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && |
9661 | "Expected RVV builtin type and vector type!" ); |
9662 | |
9663 | auto IsValidCast = [this](QualType FirstType, QualType SecondType) { |
9664 | if (const auto *BT = FirstType->getAs<BuiltinType>()) { |
9665 | if (const auto *VT = SecondType->getAs<VectorType>()) { |
9666 | if (VT->getVectorKind() == VectorKind::RVVFixedLengthMask) { |
9667 | BuiltinVectorTypeInfo Info = getBuiltinVectorTypeInfo(Ty: BT); |
9668 | return FirstType->isRVVVLSBuiltinType() && |
9669 | Info.ElementType == BoolTy && |
9670 | getTypeSize(SecondType) == getRVVTypeSize(*this, BT); |
9671 | } |
9672 | if (VT->getVectorKind() == VectorKind::RVVFixedLengthData || |
9673 | VT->getVectorKind() == VectorKind::Generic) |
9674 | return FirstType->isRVVVLSBuiltinType() && |
9675 | getTypeSize(SecondType) == getRVVTypeSize(*this, BT) && |
9676 | hasSameType(VT->getElementType(), |
9677 | getBuiltinVectorTypeInfo(BT).ElementType); |
9678 | } |
9679 | } |
9680 | return false; |
9681 | }; |
9682 | |
9683 | return IsValidCast(FirstType, SecondType) || |
9684 | IsValidCast(SecondType, FirstType); |
9685 | } |
9686 | |
9687 | bool ASTContext::areLaxCompatibleRVVTypes(QualType FirstType, |
9688 | QualType SecondType) { |
9689 | assert( |
9690 | ((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || |
9691 | (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && |
9692 | "Expected RVV builtin type and vector type!" ); |
9693 | |
9694 | auto IsLaxCompatible = [this](QualType FirstType, QualType SecondType) { |
9695 | const auto *BT = FirstType->getAs<BuiltinType>(); |
9696 | if (!BT) |
9697 | return false; |
9698 | |
9699 | if (!BT->isRVVVLSBuiltinType()) |
9700 | return false; |
9701 | |
9702 | const auto *VecTy = SecondType->getAs<VectorType>(); |
9703 | if (VecTy && VecTy->getVectorKind() == VectorKind::Generic) { |
9704 | const LangOptions::LaxVectorConversionKind LVCKind = |
9705 | getLangOpts().getLaxVectorConversions(); |
9706 | |
9707 | // If __riscv_v_fixed_vlen != N do not allow vector lax conversion. |
9708 | if (getTypeSize(T: SecondType) != getRVVTypeSize(Context&: *this, Ty: BT)) |
9709 | return false; |
9710 | |
9711 | // If -flax-vector-conversions=all is specified, the types are |
9712 | // certainly compatible. |
9713 | if (LVCKind == LangOptions::LaxVectorConversionKind::All) |
9714 | return true; |
9715 | |
9716 | // If -flax-vector-conversions=integer is specified, the types are |
9717 | // compatible if the elements are integer types. |
9718 | if (LVCKind == LangOptions::LaxVectorConversionKind::Integer) |
9719 | return VecTy->getElementType().getCanonicalType()->isIntegerType() && |
9720 | FirstType->getRVVEltType(Ctx: *this)->isIntegerType(); |
9721 | } |
9722 | |
9723 | return false; |
9724 | }; |
9725 | |
9726 | return IsLaxCompatible(FirstType, SecondType) || |
9727 | IsLaxCompatible(SecondType, FirstType); |
9728 | } |
9729 | |
9730 | bool ASTContext::hasDirectOwnershipQualifier(QualType Ty) const { |
9731 | while (true) { |
9732 | // __strong id |
9733 | if (const AttributedType *Attr = dyn_cast<AttributedType>(Val&: Ty)) { |
9734 | if (Attr->getAttrKind() == attr::ObjCOwnership) |
9735 | return true; |
9736 | |
9737 | Ty = Attr->getModifiedType(); |
9738 | |
9739 | // X *__strong (...) |
9740 | } else if (const ParenType *Paren = dyn_cast<ParenType>(Val&: Ty)) { |
9741 | Ty = Paren->getInnerType(); |
9742 | |
9743 | // We do not want to look through typedefs, typeof(expr), |
9744 | // typeof(type), or any other way that the type is somehow |
9745 | // abstracted. |
9746 | } else { |
9747 | return false; |
9748 | } |
9749 | } |
9750 | } |
9751 | |
9752 | //===----------------------------------------------------------------------===// |
9753 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
9754 | //===----------------------------------------------------------------------===// |
9755 | |
9756 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
9757 | /// inheritance hierarchy of 'rProto'. |
9758 | bool |
9759 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
9760 | ObjCProtocolDecl *rProto) const { |
9761 | if (declaresSameEntity(lProto, rProto)) |
9762 | return true; |
9763 | for (auto *PI : rProto->protocols()) |
9764 | if (ProtocolCompatibleWithProtocol(lProto, rProto: PI)) |
9765 | return true; |
9766 | return false; |
9767 | } |
9768 | |
9769 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
9770 | /// Class<pr1, ...>. |
9771 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible( |
9772 | const ObjCObjectPointerType *lhs, const ObjCObjectPointerType *rhs) { |
9773 | for (auto *lhsProto : lhs->quals()) { |
9774 | bool match = false; |
9775 | for (auto *rhsProto : rhs->quals()) { |
9776 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
9777 | match = true; |
9778 | break; |
9779 | } |
9780 | } |
9781 | if (!match) |
9782 | return false; |
9783 | } |
9784 | return true; |
9785 | } |
9786 | |
9787 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
9788 | /// ObjCQualifiedIDType. |
9789 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible( |
9790 | const ObjCObjectPointerType *lhs, const ObjCObjectPointerType *rhs, |
9791 | bool compare) { |
9792 | // Allow id<P..> and an 'id' in all cases. |
9793 | if (lhs->isObjCIdType() || rhs->isObjCIdType()) |
9794 | return true; |
9795 | |
9796 | // Don't allow id<P..> to convert to Class or Class<P..> in either direction. |
9797 | if (lhs->isObjCClassType() || lhs->isObjCQualifiedClassType() || |
9798 | rhs->isObjCClassType() || rhs->isObjCQualifiedClassType()) |
9799 | return false; |
9800 | |
9801 | if (lhs->isObjCQualifiedIdType()) { |
9802 | if (rhs->qual_empty()) { |
9803 | // If the RHS is a unqualified interface pointer "NSString*", |
9804 | // make sure we check the class hierarchy. |
9805 | if (ObjCInterfaceDecl *rhsID = rhs->getInterfaceDecl()) { |
9806 | for (auto *I : lhs->quals()) { |
9807 | // when comparing an id<P> on lhs with a static type on rhs, |
9808 | // see if static class implements all of id's protocols, directly or |
9809 | // through its super class and categories. |
9810 | if (!rhsID->ClassImplementsProtocol(I, true)) |
9811 | return false; |
9812 | } |
9813 | } |
9814 | // If there are no qualifiers and no interface, we have an 'id'. |
9815 | return true; |
9816 | } |
9817 | // Both the right and left sides have qualifiers. |
9818 | for (auto *lhsProto : lhs->quals()) { |
9819 | bool match = false; |
9820 | |
9821 | // when comparing an id<P> on lhs with a static type on rhs, |
9822 | // see if static class implements all of id's protocols, directly or |
9823 | // through its super class and categories. |
9824 | for (auto *rhsProto : rhs->quals()) { |
9825 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
9826 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
9827 | match = true; |
9828 | break; |
9829 | } |
9830 | } |
9831 | // If the RHS is a qualified interface pointer "NSString<P>*", |
9832 | // make sure we check the class hierarchy. |
9833 | if (ObjCInterfaceDecl *rhsID = rhs->getInterfaceDecl()) { |
9834 | for (auto *I : lhs->quals()) { |
9835 | // when comparing an id<P> on lhs with a static type on rhs, |
9836 | // see if static class implements all of id's protocols, directly or |
9837 | // through its super class and categories. |
9838 | if (rhsID->ClassImplementsProtocol(I, true)) { |
9839 | match = true; |
9840 | break; |
9841 | } |
9842 | } |
9843 | } |
9844 | if (!match) |
9845 | return false; |
9846 | } |
9847 | |
9848 | return true; |
9849 | } |
9850 | |
9851 | assert(rhs->isObjCQualifiedIdType() && "One of the LHS/RHS should be id<x>" ); |
9852 | |
9853 | if (lhs->getInterfaceType()) { |
9854 | // If both the right and left sides have qualifiers. |
9855 | for (auto *lhsProto : lhs->quals()) { |
9856 | bool match = false; |
9857 | |
9858 | // when comparing an id<P> on rhs with a static type on lhs, |
9859 | // see if static class implements all of id's protocols, directly or |
9860 | // through its super class and categories. |
9861 | // First, lhs protocols in the qualifier list must be found, direct |
9862 | // or indirect in rhs's qualifier list or it is a mismatch. |
9863 | for (auto *rhsProto : rhs->quals()) { |
9864 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
9865 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
9866 | match = true; |
9867 | break; |
9868 | } |
9869 | } |
9870 | if (!match) |
9871 | return false; |
9872 | } |
9873 | |
9874 | // Static class's protocols, or its super class or category protocols |
9875 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
9876 | if (ObjCInterfaceDecl *lhsID = lhs->getInterfaceDecl()) { |
9877 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
9878 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
9879 | // This is rather dubious but matches gcc's behavior. If lhs has |
9880 | // no type qualifier and its class has no static protocol(s) |
9881 | // assume that it is mismatch. |
9882 | if (LHSInheritedProtocols.empty() && lhs->qual_empty()) |
9883 | return false; |
9884 | for (auto *lhsProto : LHSInheritedProtocols) { |
9885 | bool match = false; |
9886 | for (auto *rhsProto : rhs->quals()) { |
9887 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
9888 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
9889 | match = true; |
9890 | break; |
9891 | } |
9892 | } |
9893 | if (!match) |
9894 | return false; |
9895 | } |
9896 | } |
9897 | return true; |
9898 | } |
9899 | return false; |
9900 | } |
9901 | |
9902 | /// canAssignObjCInterfaces - Return true if the two interface types are |
9903 | /// compatible for assignment from RHS to LHS. This handles validation of any |
9904 | /// protocol qualifiers on the LHS or RHS. |
9905 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
9906 | const ObjCObjectPointerType *RHSOPT) { |
9907 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
9908 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
9909 | |
9910 | // If either type represents the built-in 'id' type, return true. |
9911 | if (LHS->isObjCUnqualifiedId() || RHS->isObjCUnqualifiedId()) |
9912 | return true; |
9913 | |
9914 | // Function object that propagates a successful result or handles |
9915 | // __kindof types. |
9916 | auto finish = [&](bool succeeded) -> bool { |
9917 | if (succeeded) |
9918 | return true; |
9919 | |
9920 | if (!RHS->isKindOfType()) |
9921 | return false; |
9922 | |
9923 | // Strip off __kindof and protocol qualifiers, then check whether |
9924 | // we can assign the other way. |
9925 | return canAssignObjCInterfaces(LHSOPT: RHSOPT->stripObjCKindOfTypeAndQuals(ctx: *this), |
9926 | RHSOPT: LHSOPT->stripObjCKindOfTypeAndQuals(ctx: *this)); |
9927 | }; |
9928 | |
9929 | // Casts from or to id<P> are allowed when the other side has compatible |
9930 | // protocols. |
9931 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) { |
9932 | return finish(ObjCQualifiedIdTypesAreCompatible(lhs: LHSOPT, rhs: RHSOPT, compare: false)); |
9933 | } |
9934 | |
9935 | // Verify protocol compatibility for casts from Class<P1> to Class<P2>. |
9936 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) { |
9937 | return finish(ObjCQualifiedClassTypesAreCompatible(lhs: LHSOPT, rhs: RHSOPT)); |
9938 | } |
9939 | |
9940 | // Casts from Class to Class<Foo>, or vice-versa, are allowed. |
9941 | if (LHS->isObjCClass() && RHS->isObjCClass()) { |
9942 | return true; |
9943 | } |
9944 | |
9945 | // If we have 2 user-defined types, fall into that path. |
9946 | if (LHS->getInterface() && RHS->getInterface()) { |
9947 | return finish(canAssignObjCInterfaces(LHS, RHS)); |
9948 | } |
9949 | |
9950 | return false; |
9951 | } |
9952 | |
9953 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
9954 | /// for providing type-safety for objective-c pointers used to pass/return |
9955 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
9956 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
9957 | /// not OK. For the return type, the opposite is not OK. |
9958 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
9959 | const ObjCObjectPointerType *LHSOPT, |
9960 | const ObjCObjectPointerType *RHSOPT, |
9961 | bool BlockReturnType) { |
9962 | |
9963 | // Function object that propagates a successful result or handles |
9964 | // __kindof types. |
9965 | auto finish = [&](bool succeeded) -> bool { |
9966 | if (succeeded) |
9967 | return true; |
9968 | |
9969 | const ObjCObjectPointerType *Expected = BlockReturnType ? RHSOPT : LHSOPT; |
9970 | if (!Expected->isKindOfType()) |
9971 | return false; |
9972 | |
9973 | // Strip off __kindof and protocol qualifiers, then check whether |
9974 | // we can assign the other way. |
9975 | return canAssignObjCInterfacesInBlockPointer( |
9976 | LHSOPT: RHSOPT->stripObjCKindOfTypeAndQuals(ctx: *this), |
9977 | RHSOPT: LHSOPT->stripObjCKindOfTypeAndQuals(ctx: *this), |
9978 | BlockReturnType); |
9979 | }; |
9980 | |
9981 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
9982 | return true; |
9983 | |
9984 | if (LHSOPT->isObjCBuiltinType()) { |
9985 | return finish(RHSOPT->isObjCBuiltinType() || |
9986 | RHSOPT->isObjCQualifiedIdType()); |
9987 | } |
9988 | |
9989 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) { |
9990 | if (getLangOpts().CompatibilityQualifiedIdBlockParamTypeChecking) |
9991 | // Use for block parameters previous type checking for compatibility. |
9992 | return finish(ObjCQualifiedIdTypesAreCompatible(lhs: LHSOPT, rhs: RHSOPT, compare: false) || |
9993 | // Or corrected type checking as in non-compat mode. |
9994 | (!BlockReturnType && |
9995 | ObjCQualifiedIdTypesAreCompatible(lhs: RHSOPT, rhs: LHSOPT, compare: false))); |
9996 | else |
9997 | return finish(ObjCQualifiedIdTypesAreCompatible( |
9998 | lhs: (BlockReturnType ? LHSOPT : RHSOPT), |
9999 | rhs: (BlockReturnType ? RHSOPT : LHSOPT), compare: false)); |
10000 | } |
10001 | |
10002 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
10003 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
10004 | if (LHS && RHS) { // We have 2 user-defined types. |
10005 | if (LHS != RHS) { |
10006 | if (LHS->getDecl()->isSuperClassOf(I: RHS->getDecl())) |
10007 | return finish(BlockReturnType); |
10008 | if (RHS->getDecl()->isSuperClassOf(I: LHS->getDecl())) |
10009 | return finish(!BlockReturnType); |
10010 | } |
10011 | else |
10012 | return true; |
10013 | } |
10014 | return false; |
10015 | } |
10016 | |
10017 | /// Comparison routine for Objective-C protocols to be used with |
10018 | /// llvm::array_pod_sort. |
10019 | static int compareObjCProtocolsByName(ObjCProtocolDecl * const *lhs, |
10020 | ObjCProtocolDecl * const *rhs) { |
10021 | return (*lhs)->getName().compare((*rhs)->getName()); |
10022 | } |
10023 | |
10024 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
10025 | /// of protocols inherited from two distinct objective-c pointer objects with |
10026 | /// the given common base. |
10027 | /// It is used to build composite qualifier list of the composite type of |
10028 | /// the conditional expression involving two objective-c pointer objects. |
10029 | static |
10030 | void getIntersectionOfProtocols(ASTContext &Context, |
10031 | const ObjCInterfaceDecl *CommonBase, |
10032 | const ObjCObjectPointerType *LHSOPT, |
10033 | const ObjCObjectPointerType *RHSOPT, |
10034 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionSet) { |
10035 | |
10036 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
10037 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
10038 | assert(LHS->getInterface() && "LHS must have an interface base" ); |
10039 | assert(RHS->getInterface() && "RHS must have an interface base" ); |
10040 | |
10041 | // Add all of the protocols for the LHS. |
10042 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSProtocolSet; |
10043 | |
10044 | // Start with the protocol qualifiers. |
10045 | for (auto *proto : LHS->quals()) { |
10046 | Context.CollectInheritedProtocols(proto, LHSProtocolSet); |
10047 | } |
10048 | |
10049 | // Also add the protocols associated with the LHS interface. |
10050 | Context.CollectInheritedProtocols(LHS->getInterface(), LHSProtocolSet); |
10051 | |
10052 | // Add all of the protocols for the RHS. |
10053 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSProtocolSet; |
10054 | |
10055 | // Start with the protocol qualifiers. |
10056 | for (auto *proto : RHS->quals()) { |
10057 | Context.CollectInheritedProtocols(proto, RHSProtocolSet); |
10058 | } |
10059 | |
10060 | // Also add the protocols associated with the RHS interface. |
10061 | Context.CollectInheritedProtocols(RHS->getInterface(), RHSProtocolSet); |
10062 | |
10063 | // Compute the intersection of the collected protocol sets. |
10064 | for (auto *proto : LHSProtocolSet) { |
10065 | if (RHSProtocolSet.count(Ptr: proto)) |
10066 | IntersectionSet.push_back(Elt: proto); |
10067 | } |
10068 | |
10069 | // Compute the set of protocols that is implied by either the common type or |
10070 | // the protocols within the intersection. |
10071 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ImpliedProtocols; |
10072 | Context.CollectInheritedProtocols(CommonBase, ImpliedProtocols); |
10073 | |
10074 | // Remove any implied protocols from the list of inherited protocols. |
10075 | if (!ImpliedProtocols.empty()) { |
10076 | llvm::erase_if(C&: IntersectionSet, P: [&](ObjCProtocolDecl *proto) -> bool { |
10077 | return ImpliedProtocols.contains(Ptr: proto); |
10078 | }); |
10079 | } |
10080 | |
10081 | // Sort the remaining protocols by name. |
10082 | llvm::array_pod_sort(Start: IntersectionSet.begin(), End: IntersectionSet.end(), |
10083 | Compare: compareObjCProtocolsByName); |
10084 | } |
10085 | |
10086 | /// Determine whether the first type is a subtype of the second. |
10087 | static bool canAssignObjCObjectTypes(ASTContext &ctx, QualType lhs, |
10088 | QualType rhs) { |
10089 | // Common case: two object pointers. |
10090 | const auto *lhsOPT = lhs->getAs<ObjCObjectPointerType>(); |
10091 | const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
10092 | if (lhsOPT && rhsOPT) |
10093 | return ctx.canAssignObjCInterfaces(LHSOPT: lhsOPT, RHSOPT: rhsOPT); |
10094 | |
10095 | // Two block pointers. |
10096 | const auto *lhsBlock = lhs->getAs<BlockPointerType>(); |
10097 | const auto *rhsBlock = rhs->getAs<BlockPointerType>(); |
10098 | if (lhsBlock && rhsBlock) |
10099 | return ctx.typesAreBlockPointerCompatible(lhs, rhs); |
10100 | |
10101 | // If either is an unqualified 'id' and the other is a block, it's |
10102 | // acceptable. |
10103 | if ((lhsOPT && lhsOPT->isObjCIdType() && rhsBlock) || |
10104 | (rhsOPT && rhsOPT->isObjCIdType() && lhsBlock)) |
10105 | return true; |
10106 | |
10107 | return false; |
10108 | } |
10109 | |
10110 | // Check that the given Objective-C type argument lists are equivalent. |
10111 | static bool sameObjCTypeArgs(ASTContext &ctx, |
10112 | const ObjCInterfaceDecl *iface, |
10113 | ArrayRef<QualType> lhsArgs, |
10114 | ArrayRef<QualType> rhsArgs, |
10115 | bool stripKindOf) { |
10116 | if (lhsArgs.size() != rhsArgs.size()) |
10117 | return false; |
10118 | |
10119 | ObjCTypeParamList *typeParams = iface->getTypeParamList(); |
10120 | if (!typeParams) |
10121 | return false; |
10122 | |
10123 | for (unsigned i = 0, n = lhsArgs.size(); i != n; ++i) { |
10124 | if (ctx.hasSameType(T1: lhsArgs[i], T2: rhsArgs[i])) |
10125 | continue; |
10126 | |
10127 | switch (typeParams->begin()[i]->getVariance()) { |
10128 | case ObjCTypeParamVariance::Invariant: |
10129 | if (!stripKindOf || |
10130 | !ctx.hasSameType(T1: lhsArgs[i].stripObjCKindOfType(ctx), |
10131 | T2: rhsArgs[i].stripObjCKindOfType(ctx))) { |
10132 | return false; |
10133 | } |
10134 | break; |
10135 | |
10136 | case ObjCTypeParamVariance::Covariant: |
10137 | if (!canAssignObjCObjectTypes(ctx, lhs: lhsArgs[i], rhs: rhsArgs[i])) |
10138 | return false; |
10139 | break; |
10140 | |
10141 | case ObjCTypeParamVariance::Contravariant: |
10142 | if (!canAssignObjCObjectTypes(ctx, lhs: rhsArgs[i], rhs: lhsArgs[i])) |
10143 | return false; |
10144 | break; |
10145 | } |
10146 | } |
10147 | |
10148 | return true; |
10149 | } |
10150 | |
10151 | QualType ASTContext::areCommonBaseCompatible( |
10152 | const ObjCObjectPointerType *Lptr, |
10153 | const ObjCObjectPointerType *Rptr) { |
10154 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
10155 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
10156 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
10157 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
10158 | |
10159 | if (!LDecl || !RDecl) |
10160 | return {}; |
10161 | |
10162 | // When either LHS or RHS is a kindof type, we should return a kindof type. |
10163 | // For example, for common base of kindof(ASub1) and kindof(ASub2), we return |
10164 | // kindof(A). |
10165 | bool anyKindOf = LHS->isKindOfType() || RHS->isKindOfType(); |
10166 | |
10167 | // Follow the left-hand side up the class hierarchy until we either hit a |
10168 | // root or find the RHS. Record the ancestors in case we don't find it. |
10169 | llvm::SmallDenseMap<const ObjCInterfaceDecl *, const ObjCObjectType *, 4> |
10170 | LHSAncestors; |
10171 | while (true) { |
10172 | // Record this ancestor. We'll need this if the common type isn't in the |
10173 | // path from the LHS to the root. |
10174 | LHSAncestors[LHS->getInterface()->getCanonicalDecl()] = LHS; |
10175 | |
10176 | if (declaresSameEntity(LHS->getInterface(), RDecl)) { |
10177 | // Get the type arguments. |
10178 | ArrayRef<QualType> LHSTypeArgs = LHS->getTypeArgsAsWritten(); |
10179 | bool anyChanges = false; |
10180 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
10181 | // Both have type arguments, compare them. |
10182 | if (!sameObjCTypeArgs(ctx&: *this, iface: LHS->getInterface(), |
10183 | lhsArgs: LHS->getTypeArgs(), rhsArgs: RHS->getTypeArgs(), |
10184 | /*stripKindOf=*/true)) |
10185 | return {}; |
10186 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
10187 | // If only one has type arguments, the result will not have type |
10188 | // arguments. |
10189 | LHSTypeArgs = {}; |
10190 | anyChanges = true; |
10191 | } |
10192 | |
10193 | // Compute the intersection of protocols. |
10194 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
10195 | getIntersectionOfProtocols(Context&: *this, CommonBase: LHS->getInterface(), LHSOPT: Lptr, RHSOPT: Rptr, |
10196 | IntersectionSet&: Protocols); |
10197 | if (!Protocols.empty()) |
10198 | anyChanges = true; |
10199 | |
10200 | // If anything in the LHS will have changed, build a new result type. |
10201 | // If we need to return a kindof type but LHS is not a kindof type, we |
10202 | // build a new result type. |
10203 | if (anyChanges || LHS->isKindOfType() != anyKindOf) { |
10204 | QualType Result = getObjCInterfaceType(Decl: LHS->getInterface()); |
10205 | Result = getObjCObjectType(baseType: Result, typeArgs: LHSTypeArgs, protocols: Protocols, |
10206 | isKindOf: anyKindOf || LHS->isKindOfType()); |
10207 | return getObjCObjectPointerType(ObjectT: Result); |
10208 | } |
10209 | |
10210 | return getObjCObjectPointerType(ObjectT: QualType(LHS, 0)); |
10211 | } |
10212 | |
10213 | // Find the superclass. |
10214 | QualType LHSSuperType = LHS->getSuperClassType(); |
10215 | if (LHSSuperType.isNull()) |
10216 | break; |
10217 | |
10218 | LHS = LHSSuperType->castAs<ObjCObjectType>(); |
10219 | } |
10220 | |
10221 | // We didn't find anything by following the LHS to its root; now check |
10222 | // the RHS against the cached set of ancestors. |
10223 | while (true) { |
10224 | auto KnownLHS = LHSAncestors.find(Val: RHS->getInterface()->getCanonicalDecl()); |
10225 | if (KnownLHS != LHSAncestors.end()) { |
10226 | LHS = KnownLHS->second; |
10227 | |
10228 | // Get the type arguments. |
10229 | ArrayRef<QualType> RHSTypeArgs = RHS->getTypeArgsAsWritten(); |
10230 | bool anyChanges = false; |
10231 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
10232 | // Both have type arguments, compare them. |
10233 | if (!sameObjCTypeArgs(ctx&: *this, iface: LHS->getInterface(), |
10234 | lhsArgs: LHS->getTypeArgs(), rhsArgs: RHS->getTypeArgs(), |
10235 | /*stripKindOf=*/true)) |
10236 | return {}; |
10237 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
10238 | // If only one has type arguments, the result will not have type |
10239 | // arguments. |
10240 | RHSTypeArgs = {}; |
10241 | anyChanges = true; |
10242 | } |
10243 | |
10244 | // Compute the intersection of protocols. |
10245 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
10246 | getIntersectionOfProtocols(Context&: *this, CommonBase: RHS->getInterface(), LHSOPT: Lptr, RHSOPT: Rptr, |
10247 | IntersectionSet&: Protocols); |
10248 | if (!Protocols.empty()) |
10249 | anyChanges = true; |
10250 | |
10251 | // If we need to return a kindof type but RHS is not a kindof type, we |
10252 | // build a new result type. |
10253 | if (anyChanges || RHS->isKindOfType() != anyKindOf) { |
10254 | QualType Result = getObjCInterfaceType(Decl: RHS->getInterface()); |
10255 | Result = getObjCObjectType(baseType: Result, typeArgs: RHSTypeArgs, protocols: Protocols, |
10256 | isKindOf: anyKindOf || RHS->isKindOfType()); |
10257 | return getObjCObjectPointerType(ObjectT: Result); |
10258 | } |
10259 | |
10260 | return getObjCObjectPointerType(ObjectT: QualType(RHS, 0)); |
10261 | } |
10262 | |
10263 | // Find the superclass of the RHS. |
10264 | QualType RHSSuperType = RHS->getSuperClassType(); |
10265 | if (RHSSuperType.isNull()) |
10266 | break; |
10267 | |
10268 | RHS = RHSSuperType->castAs<ObjCObjectType>(); |
10269 | } |
10270 | |
10271 | return {}; |
10272 | } |
10273 | |
10274 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
10275 | const ObjCObjectType *RHS) { |
10276 | assert(LHS->getInterface() && "LHS is not an interface type" ); |
10277 | assert(RHS->getInterface() && "RHS is not an interface type" ); |
10278 | |
10279 | // Verify that the base decls are compatible: the RHS must be a subclass of |
10280 | // the LHS. |
10281 | ObjCInterfaceDecl *LHSInterface = LHS->getInterface(); |
10282 | bool IsSuperClass = LHSInterface->isSuperClassOf(I: RHS->getInterface()); |
10283 | if (!IsSuperClass) |
10284 | return false; |
10285 | |
10286 | // If the LHS has protocol qualifiers, determine whether all of them are |
10287 | // satisfied by the RHS (i.e., the RHS has a superset of the protocols in the |
10288 | // LHS). |
10289 | if (LHS->getNumProtocols() > 0) { |
10290 | // OK if conversion of LHS to SuperClass results in narrowing of types |
10291 | // ; i.e., SuperClass may implement at least one of the protocols |
10292 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
10293 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
10294 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
10295 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
10296 | // Also, if RHS has explicit quelifiers, include them for comparing with LHS's |
10297 | // qualifiers. |
10298 | for (auto *RHSPI : RHS->quals()) |
10299 | CollectInheritedProtocols(RHSPI, SuperClassInheritedProtocols); |
10300 | // If there is no protocols associated with RHS, it is not a match. |
10301 | if (SuperClassInheritedProtocols.empty()) |
10302 | return false; |
10303 | |
10304 | for (const auto *LHSProto : LHS->quals()) { |
10305 | bool SuperImplementsProtocol = false; |
10306 | for (auto *SuperClassProto : SuperClassInheritedProtocols) |
10307 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
10308 | SuperImplementsProtocol = true; |
10309 | break; |
10310 | } |
10311 | if (!SuperImplementsProtocol) |
10312 | return false; |
10313 | } |
10314 | } |
10315 | |
10316 | // If the LHS is specialized, we may need to check type arguments. |
10317 | if (LHS->isSpecialized()) { |
10318 | // Follow the superclass chain until we've matched the LHS class in the |
10319 | // hierarchy. This substitutes type arguments through. |
10320 | const ObjCObjectType *RHSSuper = RHS; |
10321 | while (!declaresSameEntity(RHSSuper->getInterface(), LHSInterface)) |
10322 | RHSSuper = RHSSuper->getSuperClassType()->castAs<ObjCObjectType>(); |
10323 | |
10324 | // If the RHS is specializd, compare type arguments. |
10325 | if (RHSSuper->isSpecialized() && |
10326 | !sameObjCTypeArgs(ctx&: *this, iface: LHS->getInterface(), |
10327 | lhsArgs: LHS->getTypeArgs(), rhsArgs: RHSSuper->getTypeArgs(), |
10328 | /*stripKindOf=*/true)) { |
10329 | return false; |
10330 | } |
10331 | } |
10332 | |
10333 | return true; |
10334 | } |
10335 | |
10336 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
10337 | // get the "pointed to" types |
10338 | const auto *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
10339 | const auto *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
10340 | |
10341 | if (!LHSOPT || !RHSOPT) |
10342 | return false; |
10343 | |
10344 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
10345 | canAssignObjCInterfaces(LHSOPT: RHSOPT, RHSOPT: LHSOPT); |
10346 | } |
10347 | |
10348 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
10349 | return canAssignObjCInterfaces( |
10350 | LHSOPT: getObjCObjectPointerType(ObjectT: To)->castAs<ObjCObjectPointerType>(), |
10351 | RHSOPT: getObjCObjectPointerType(ObjectT: From)->castAs<ObjCObjectPointerType>()); |
10352 | } |
10353 | |
10354 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
10355 | /// both shall have the identically qualified version of a compatible type. |
10356 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
10357 | /// same. See 6.7.[2,3,5] for additional rules. |
10358 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
10359 | bool CompareUnqualified) { |
10360 | if (getLangOpts().CPlusPlus) |
10361 | return hasSameType(T1: LHS, T2: RHS); |
10362 | |
10363 | return !mergeTypes(LHS, RHS, OfBlockPointer: false, Unqualified: CompareUnqualified).isNull(); |
10364 | } |
10365 | |
10366 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
10367 | return typesAreCompatible(LHS, RHS); |
10368 | } |
10369 | |
10370 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
10371 | return !mergeTypes(LHS, RHS, OfBlockPointer: true).isNull(); |
10372 | } |
10373 | |
10374 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
10375 | /// of T is compatible with SubType, return the merged type, else return |
10376 | /// QualType() |
10377 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
10378 | bool OfBlockPointer, |
10379 | bool Unqualified) { |
10380 | if (const RecordType *UT = T->getAsUnionType()) { |
10381 | RecordDecl *UD = UT->getDecl(); |
10382 | if (UD->hasAttr<TransparentUnionAttr>()) { |
10383 | for (const auto *I : UD->fields()) { |
10384 | QualType ET = I->getType().getUnqualifiedType(); |
10385 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
10386 | if (!MT.isNull()) |
10387 | return MT; |
10388 | } |
10389 | } |
10390 | } |
10391 | |
10392 | return {}; |
10393 | } |
10394 | |
10395 | /// mergeFunctionParameterTypes - merge two types which appear as function |
10396 | /// parameter types |
10397 | QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs, |
10398 | bool OfBlockPointer, |
10399 | bool Unqualified) { |
10400 | // GNU extension: two types are compatible if they appear as a function |
10401 | // argument, one of the types is a transparent union type and the other |
10402 | // type is compatible with a union member |
10403 | QualType lmerge = mergeTransparentUnionType(T: lhs, SubType: rhs, OfBlockPointer, |
10404 | Unqualified); |
10405 | if (!lmerge.isNull()) |
10406 | return lmerge; |
10407 | |
10408 | QualType rmerge = mergeTransparentUnionType(T: rhs, SubType: lhs, OfBlockPointer, |
10409 | Unqualified); |
10410 | if (!rmerge.isNull()) |
10411 | return rmerge; |
10412 | |
10413 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
10414 | } |
10415 | |
10416 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
10417 | bool OfBlockPointer, bool Unqualified, |
10418 | bool AllowCXX, |
10419 | bool IsConditionalOperator) { |
10420 | const auto *lbase = lhs->castAs<FunctionType>(); |
10421 | const auto *rbase = rhs->castAs<FunctionType>(); |
10422 | const auto *lproto = dyn_cast<FunctionProtoType>(Val: lbase); |
10423 | const auto *rproto = dyn_cast<FunctionProtoType>(Val: rbase); |
10424 | bool allLTypes = true; |
10425 | bool allRTypes = true; |
10426 | |
10427 | // Check return type |
10428 | QualType retType; |
10429 | if (OfBlockPointer) { |
10430 | QualType RHS = rbase->getReturnType(); |
10431 | QualType LHS = lbase->getReturnType(); |
10432 | bool UnqualifiedResult = Unqualified; |
10433 | if (!UnqualifiedResult) |
10434 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
10435 | retType = mergeTypes(LHS, RHS, OfBlockPointer: true, Unqualified: UnqualifiedResult, BlockReturnType: true); |
10436 | } |
10437 | else |
10438 | retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), OfBlockPointer: false, |
10439 | Unqualified); |
10440 | if (retType.isNull()) |
10441 | return {}; |
10442 | |
10443 | if (Unqualified) |
10444 | retType = retType.getUnqualifiedType(); |
10445 | |
10446 | CanQualType LRetType = getCanonicalType(T: lbase->getReturnType()); |
10447 | CanQualType RRetType = getCanonicalType(T: rbase->getReturnType()); |
10448 | if (Unqualified) { |
10449 | LRetType = LRetType.getUnqualifiedType(); |
10450 | RRetType = RRetType.getUnqualifiedType(); |
10451 | } |
10452 | |
10453 | if (getCanonicalType(T: retType) != LRetType) |
10454 | allLTypes = false; |
10455 | if (getCanonicalType(T: retType) != RRetType) |
10456 | allRTypes = false; |
10457 | |
10458 | // FIXME: double check this |
10459 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
10460 | // rbase->getRegParmAttr() != 0 && |
10461 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
10462 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
10463 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
10464 | |
10465 | // Compatible functions must have compatible calling conventions |
10466 | if (lbaseInfo.getCC() != rbaseInfo.getCC()) |
10467 | return {}; |
10468 | |
10469 | // Regparm is part of the calling convention. |
10470 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
10471 | return {}; |
10472 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
10473 | return {}; |
10474 | |
10475 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
10476 | return {}; |
10477 | if (lbaseInfo.getNoCallerSavedRegs() != rbaseInfo.getNoCallerSavedRegs()) |
10478 | return {}; |
10479 | if (lbaseInfo.getNoCfCheck() != rbaseInfo.getNoCfCheck()) |
10480 | return {}; |
10481 | |
10482 | // When merging declarations, it's common for supplemental information like |
10483 | // attributes to only be present in one of the declarations, and we generally |
10484 | // want type merging to preserve the union of information. So a merged |
10485 | // function type should be noreturn if it was noreturn in *either* operand |
10486 | // type. |
10487 | // |
10488 | // But for the conditional operator, this is backwards. The result of the |
10489 | // operator could be either operand, and its type should conservatively |
10490 | // reflect that. So a function type in a composite type is noreturn only |
10491 | // if it's noreturn in *both* operand types. |
10492 | // |
10493 | // Arguably, noreturn is a kind of subtype, and the conditional operator |
10494 | // ought to produce the most specific common supertype of its operand types. |
10495 | // That would differ from this rule in contravariant positions. However, |
10496 | // neither C nor C++ generally uses this kind of subtype reasoning. Also, |
10497 | // as a practical matter, it would only affect C code that does abstraction of |
10498 | // higher-order functions (taking noreturn callbacks!), which is uncommon to |
10499 | // say the least. So we use the simpler rule. |
10500 | bool NoReturn = IsConditionalOperator |
10501 | ? lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn() |
10502 | : lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
10503 | if (lbaseInfo.getNoReturn() != NoReturn) |
10504 | allLTypes = false; |
10505 | if (rbaseInfo.getNoReturn() != NoReturn) |
10506 | allRTypes = false; |
10507 | |
10508 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(noReturn: NoReturn); |
10509 | |
10510 | if (lproto && rproto) { // two C99 style function prototypes |
10511 | assert((AllowCXX || |
10512 | (!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec())) && |
10513 | "C++ shouldn't be here" ); |
10514 | // Compatible functions must have the same number of parameters |
10515 | if (lproto->getNumParams() != rproto->getNumParams()) |
10516 | return {}; |
10517 | |
10518 | // Variadic and non-variadic functions aren't compatible |
10519 | if (lproto->isVariadic() != rproto->isVariadic()) |
10520 | return {}; |
10521 | |
10522 | if (lproto->getMethodQuals() != rproto->getMethodQuals()) |
10523 | return {}; |
10524 | |
10525 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> newParamInfos; |
10526 | bool canUseLeft, canUseRight; |
10527 | if (!mergeExtParameterInfo(FirstFnType: lproto, SecondFnType: rproto, CanUseFirst&: canUseLeft, CanUseSecond&: canUseRight, |
10528 | NewParamInfos&: newParamInfos)) |
10529 | return {}; |
10530 | |
10531 | if (!canUseLeft) |
10532 | allLTypes = false; |
10533 | if (!canUseRight) |
10534 | allRTypes = false; |
10535 | |
10536 | // Check parameter type compatibility |
10537 | SmallVector<QualType, 10> types; |
10538 | for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) { |
10539 | QualType lParamType = lproto->getParamType(i).getUnqualifiedType(); |
10540 | QualType rParamType = rproto->getParamType(i).getUnqualifiedType(); |
10541 | QualType paramType = mergeFunctionParameterTypes( |
10542 | lhs: lParamType, rhs: rParamType, OfBlockPointer, Unqualified); |
10543 | if (paramType.isNull()) |
10544 | return {}; |
10545 | |
10546 | if (Unqualified) |
10547 | paramType = paramType.getUnqualifiedType(); |
10548 | |
10549 | types.push_back(Elt: paramType); |
10550 | if (Unqualified) { |
10551 | lParamType = lParamType.getUnqualifiedType(); |
10552 | rParamType = rParamType.getUnqualifiedType(); |
10553 | } |
10554 | |
10555 | if (getCanonicalType(T: paramType) != getCanonicalType(T: lParamType)) |
10556 | allLTypes = false; |
10557 | if (getCanonicalType(T: paramType) != getCanonicalType(T: rParamType)) |
10558 | allRTypes = false; |
10559 | } |
10560 | |
10561 | if (allLTypes) return lhs; |
10562 | if (allRTypes) return rhs; |
10563 | |
10564 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
10565 | EPI.ExtInfo = einfo; |
10566 | EPI.ExtParameterInfos = |
10567 | newParamInfos.empty() ? nullptr : newParamInfos.data(); |
10568 | return getFunctionType(ResultTy: retType, Args: types, EPI); |
10569 | } |
10570 | |
10571 | if (lproto) allRTypes = false; |
10572 | if (rproto) allLTypes = false; |
10573 | |
10574 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
10575 | if (proto) { |
10576 | assert((AllowCXX || !proto->hasExceptionSpec()) && "C++ shouldn't be here" ); |
10577 | if (proto->isVariadic()) |
10578 | return {}; |
10579 | // Check that the types are compatible with the types that |
10580 | // would result from default argument promotions (C99 6.7.5.3p15). |
10581 | // The only types actually affected are promotable integer |
10582 | // types and floats, which would be passed as a different |
10583 | // type depending on whether the prototype is visible. |
10584 | for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) { |
10585 | QualType paramTy = proto->getParamType(i); |
10586 | |
10587 | // Look at the converted type of enum types, since that is the type used |
10588 | // to pass enum values. |
10589 | if (const auto *Enum = paramTy->getAs<EnumType>()) { |
10590 | paramTy = Enum->getDecl()->getIntegerType(); |
10591 | if (paramTy.isNull()) |
10592 | return {}; |
10593 | } |
10594 | |
10595 | if (isPromotableIntegerType(paramTy) || |
10596 | getCanonicalType(paramTy).getUnqualifiedType() == FloatTy) |
10597 | return {}; |
10598 | } |
10599 | |
10600 | if (allLTypes) return lhs; |
10601 | if (allRTypes) return rhs; |
10602 | |
10603 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
10604 | EPI.ExtInfo = einfo; |
10605 | return getFunctionType(ResultTy: retType, Args: proto->getParamTypes(), EPI); |
10606 | } |
10607 | |
10608 | if (allLTypes) return lhs; |
10609 | if (allRTypes) return rhs; |
10610 | return getFunctionNoProtoType(ResultTy: retType, Info: einfo); |
10611 | } |
10612 | |
10613 | /// Given that we have an enum type and a non-enum type, try to merge them. |
10614 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
10615 | QualType other, bool isBlockReturnType) { |
10616 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
10617 | // a signed integer type, or an unsigned integer type. |
10618 | // Compatibility is based on the underlying type, not the promotion |
10619 | // type. |
10620 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
10621 | if (underlyingType.isNull()) |
10622 | return {}; |
10623 | if (Context.hasSameType(T1: underlyingType, T2: other)) |
10624 | return other; |
10625 | |
10626 | // In block return types, we're more permissive and accept any |
10627 | // integral type of the same size. |
10628 | if (isBlockReturnType && other->isIntegerType() && |
10629 | Context.getTypeSize(T: underlyingType) == Context.getTypeSize(T: other)) |
10630 | return other; |
10631 | |
10632 | return {}; |
10633 | } |
10634 | |
10635 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, bool OfBlockPointer, |
10636 | bool Unqualified, bool BlockReturnType, |
10637 | bool IsConditionalOperator) { |
10638 | // For C++ we will not reach this code with reference types (see below), |
10639 | // for OpenMP variant call overloading we might. |
10640 | // |
10641 | // C++ [expr]: If an expression initially has the type "reference to T", the |
10642 | // type is adjusted to "T" prior to any further analysis, the expression |
10643 | // designates the object or function denoted by the reference, and the |
10644 | // expression is an lvalue unless the reference is an rvalue reference and |
10645 | // the expression is a function call (possibly inside parentheses). |
10646 | auto *LHSRefTy = LHS->getAs<ReferenceType>(); |
10647 | auto *RHSRefTy = RHS->getAs<ReferenceType>(); |
10648 | if (LangOpts.OpenMP && LHSRefTy && RHSRefTy && |
10649 | LHS->getTypeClass() == RHS->getTypeClass()) |
10650 | return mergeTypes(LHS: LHSRefTy->getPointeeType(), RHS: RHSRefTy->getPointeeType(), |
10651 | OfBlockPointer, Unqualified, BlockReturnType); |
10652 | if (LHSRefTy || RHSRefTy) |
10653 | return {}; |
10654 | |
10655 | if (Unqualified) { |
10656 | LHS = LHS.getUnqualifiedType(); |
10657 | RHS = RHS.getUnqualifiedType(); |
10658 | } |
10659 | |
10660 | QualType LHSCan = getCanonicalType(T: LHS), |
10661 | RHSCan = getCanonicalType(T: RHS); |
10662 | |
10663 | // If two types are identical, they are compatible. |
10664 | if (LHSCan == RHSCan) |
10665 | return LHS; |
10666 | |
10667 | // If the qualifiers are different, the types aren't compatible... mostly. |
10668 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
10669 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
10670 | if (LQuals != RQuals) { |
10671 | // If any of these qualifiers are different, we have a type |
10672 | // mismatch. |
10673 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
10674 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
10675 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime() || |
10676 | LQuals.hasUnaligned() != RQuals.hasUnaligned()) |
10677 | return {}; |
10678 | |
10679 | // Exactly one GC qualifier difference is allowed: __strong is |
10680 | // okay if the other type has no GC qualifier but is an Objective |
10681 | // C object pointer (i.e. implicitly strong by default). We fix |
10682 | // this by pretending that the unqualified type was actually |
10683 | // qualified __strong. |
10684 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
10685 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
10686 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements" ); |
10687 | |
10688 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
10689 | return {}; |
10690 | |
10691 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
10692 | return mergeTypes(LHS, RHS: getObjCGCQualType(T: RHS, GCAttr: Qualifiers::Strong)); |
10693 | } |
10694 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
10695 | return mergeTypes(LHS: getObjCGCQualType(T: LHS, GCAttr: Qualifiers::Strong), RHS); |
10696 | } |
10697 | return {}; |
10698 | } |
10699 | |
10700 | // Okay, qualifiers are equal. |
10701 | |
10702 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
10703 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
10704 | |
10705 | // We want to consider the two function types to be the same for these |
10706 | // comparisons, just force one to the other. |
10707 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
10708 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
10709 | |
10710 | // Same as above for arrays |
10711 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
10712 | LHSClass = Type::ConstantArray; |
10713 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
10714 | RHSClass = Type::ConstantArray; |
10715 | |
10716 | // ObjCInterfaces are just specialized ObjCObjects. |
10717 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
10718 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
10719 | |
10720 | // Canonicalize ExtVector -> Vector. |
10721 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
10722 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
10723 | |
10724 | // If the canonical type classes don't match. |
10725 | if (LHSClass != RHSClass) { |
10726 | // Note that we only have special rules for turning block enum |
10727 | // returns into block int returns, not vice-versa. |
10728 | if (const auto *ETy = LHS->getAs<EnumType>()) { |
10729 | return mergeEnumWithInteger(Context&: *this, ET: ETy, other: RHS, isBlockReturnType: false); |
10730 | } |
10731 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
10732 | return mergeEnumWithInteger(Context&: *this, ET: ETy, other: LHS, isBlockReturnType: BlockReturnType); |
10733 | } |
10734 | // allow block pointer type to match an 'id' type. |
10735 | if (OfBlockPointer && !BlockReturnType) { |
10736 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
10737 | return LHS; |
10738 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
10739 | return RHS; |
10740 | } |
10741 | // Allow __auto_type to match anything; it merges to the type with more |
10742 | // information. |
10743 | if (const auto *AT = LHS->getAs<AutoType>()) { |
10744 | if (!AT->isDeduced() && AT->isGNUAutoType()) |
10745 | return RHS; |
10746 | } |
10747 | if (const auto *AT = RHS->getAs<AutoType>()) { |
10748 | if (!AT->isDeduced() && AT->isGNUAutoType()) |
10749 | return LHS; |
10750 | } |
10751 | return {}; |
10752 | } |
10753 | |
10754 | // The canonical type classes match. |
10755 | switch (LHSClass) { |
10756 | #define TYPE(Class, Base) |
10757 | #define ABSTRACT_TYPE(Class, Base) |
10758 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
10759 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
10760 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
10761 | #include "clang/AST/TypeNodes.inc" |
10762 | llvm_unreachable("Non-canonical and dependent types shouldn't get here" ); |
10763 | |
10764 | case Type::Auto: |
10765 | case Type::DeducedTemplateSpecialization: |
10766 | case Type::LValueReference: |
10767 | case Type::RValueReference: |
10768 | case Type::MemberPointer: |
10769 | llvm_unreachable("C++ should never be in mergeTypes" ); |
10770 | |
10771 | case Type::ObjCInterface: |
10772 | case Type::IncompleteArray: |
10773 | case Type::VariableArray: |
10774 | case Type::FunctionProto: |
10775 | case Type::ExtVector: |
10776 | llvm_unreachable("Types are eliminated above" ); |
10777 | |
10778 | case Type::Pointer: |
10779 | { |
10780 | // Merge two pointer types, while trying to preserve typedef info |
10781 | QualType LHSPointee = LHS->castAs<PointerType>()->getPointeeType(); |
10782 | QualType RHSPointee = RHS->castAs<PointerType>()->getPointeeType(); |
10783 | if (Unqualified) { |
10784 | LHSPointee = LHSPointee.getUnqualifiedType(); |
10785 | RHSPointee = RHSPointee.getUnqualifiedType(); |
10786 | } |
10787 | QualType ResultType = mergeTypes(LHS: LHSPointee, RHS: RHSPointee, OfBlockPointer: false, |
10788 | Unqualified); |
10789 | if (ResultType.isNull()) |
10790 | return {}; |
10791 | if (getCanonicalType(T: LHSPointee) == getCanonicalType(T: ResultType)) |
10792 | return LHS; |
10793 | if (getCanonicalType(T: RHSPointee) == getCanonicalType(T: ResultType)) |
10794 | return RHS; |
10795 | return getPointerType(T: ResultType); |
10796 | } |
10797 | case Type::BlockPointer: |
10798 | { |
10799 | // Merge two block pointer types, while trying to preserve typedef info |
10800 | QualType LHSPointee = LHS->castAs<BlockPointerType>()->getPointeeType(); |
10801 | QualType RHSPointee = RHS->castAs<BlockPointerType>()->getPointeeType(); |
10802 | if (Unqualified) { |
10803 | LHSPointee = LHSPointee.getUnqualifiedType(); |
10804 | RHSPointee = RHSPointee.getUnqualifiedType(); |
10805 | } |
10806 | if (getLangOpts().OpenCL) { |
10807 | Qualifiers LHSPteeQual = LHSPointee.getQualifiers(); |
10808 | Qualifiers RHSPteeQual = RHSPointee.getQualifiers(); |
10809 | // Blocks can't be an expression in a ternary operator (OpenCL v2.0 |
10810 | // 6.12.5) thus the following check is asymmetric. |
10811 | if (!LHSPteeQual.isAddressSpaceSupersetOf(other: RHSPteeQual)) |
10812 | return {}; |
10813 | LHSPteeQual.removeAddressSpace(); |
10814 | RHSPteeQual.removeAddressSpace(); |
10815 | LHSPointee = |
10816 | QualType(LHSPointee.getTypePtr(), LHSPteeQual.getAsOpaqueValue()); |
10817 | RHSPointee = |
10818 | QualType(RHSPointee.getTypePtr(), RHSPteeQual.getAsOpaqueValue()); |
10819 | } |
10820 | QualType ResultType = mergeTypes(LHS: LHSPointee, RHS: RHSPointee, OfBlockPointer, |
10821 | Unqualified); |
10822 | if (ResultType.isNull()) |
10823 | return {}; |
10824 | if (getCanonicalType(T: LHSPointee) == getCanonicalType(T: ResultType)) |
10825 | return LHS; |
10826 | if (getCanonicalType(T: RHSPointee) == getCanonicalType(T: ResultType)) |
10827 | return RHS; |
10828 | return getBlockPointerType(T: ResultType); |
10829 | } |
10830 | case Type::Atomic: |
10831 | { |
10832 | // Merge two pointer types, while trying to preserve typedef info |
10833 | QualType LHSValue = LHS->castAs<AtomicType>()->getValueType(); |
10834 | QualType RHSValue = RHS->castAs<AtomicType>()->getValueType(); |
10835 | if (Unqualified) { |
10836 | LHSValue = LHSValue.getUnqualifiedType(); |
10837 | RHSValue = RHSValue.getUnqualifiedType(); |
10838 | } |
10839 | QualType ResultType = mergeTypes(LHS: LHSValue, RHS: RHSValue, OfBlockPointer: false, |
10840 | Unqualified); |
10841 | if (ResultType.isNull()) |
10842 | return {}; |
10843 | if (getCanonicalType(T: LHSValue) == getCanonicalType(T: ResultType)) |
10844 | return LHS; |
10845 | if (getCanonicalType(T: RHSValue) == getCanonicalType(T: ResultType)) |
10846 | return RHS; |
10847 | return getAtomicType(T: ResultType); |
10848 | } |
10849 | case Type::ConstantArray: |
10850 | { |
10851 | const ConstantArrayType* LCAT = getAsConstantArrayType(T: LHS); |
10852 | const ConstantArrayType* RCAT = getAsConstantArrayType(T: RHS); |
10853 | if (LCAT && RCAT && RCAT->getZExtSize() != LCAT->getZExtSize()) |
10854 | return {}; |
10855 | |
10856 | QualType LHSElem = getAsArrayType(T: LHS)->getElementType(); |
10857 | QualType RHSElem = getAsArrayType(T: RHS)->getElementType(); |
10858 | if (Unqualified) { |
10859 | LHSElem = LHSElem.getUnqualifiedType(); |
10860 | RHSElem = RHSElem.getUnqualifiedType(); |
10861 | } |
10862 | |
10863 | QualType ResultType = mergeTypes(LHS: LHSElem, RHS: RHSElem, OfBlockPointer: false, Unqualified); |
10864 | if (ResultType.isNull()) |
10865 | return {}; |
10866 | |
10867 | const VariableArrayType* LVAT = getAsVariableArrayType(T: LHS); |
10868 | const VariableArrayType* RVAT = getAsVariableArrayType(T: RHS); |
10869 | |
10870 | // If either side is a variable array, and both are complete, check whether |
10871 | // the current dimension is definite. |
10872 | if (LVAT || RVAT) { |
10873 | auto SizeFetch = [this](const VariableArrayType* VAT, |
10874 | const ConstantArrayType* CAT) |
10875 | -> std::pair<bool,llvm::APInt> { |
10876 | if (VAT) { |
10877 | std::optional<llvm::APSInt> TheInt; |
10878 | Expr *E = VAT->getSizeExpr(); |
10879 | if (E && (TheInt = E->getIntegerConstantExpr(*this))) |
10880 | return std::make_pair(true, *TheInt); |
10881 | return std::make_pair(false, llvm::APSInt()); |
10882 | } |
10883 | if (CAT) |
10884 | return std::make_pair(true, CAT->getSize()); |
10885 | return std::make_pair(false, llvm::APInt()); |
10886 | }; |
10887 | |
10888 | bool HaveLSize, HaveRSize; |
10889 | llvm::APInt LSize, RSize; |
10890 | std::tie(HaveLSize, LSize) = SizeFetch(LVAT, LCAT); |
10891 | std::tie(HaveRSize, RSize) = SizeFetch(RVAT, RCAT); |
10892 | if (HaveLSize && HaveRSize && !llvm::APInt::isSameValue(I1: LSize, I2: RSize)) |
10893 | return {}; // Definite, but unequal, array dimension |
10894 | } |
10895 | |
10896 | if (LCAT && getCanonicalType(T: LHSElem) == getCanonicalType(T: ResultType)) |
10897 | return LHS; |
10898 | if (RCAT && getCanonicalType(T: RHSElem) == getCanonicalType(T: ResultType)) |
10899 | return RHS; |
10900 | if (LCAT) |
10901 | return getConstantArrayType(EltTy: ResultType, ArySizeIn: LCAT->getSize(), |
10902 | SizeExpr: LCAT->getSizeExpr(), ASM: ArraySizeModifier(), IndexTypeQuals: 0); |
10903 | if (RCAT) |
10904 | return getConstantArrayType(EltTy: ResultType, ArySizeIn: RCAT->getSize(), |
10905 | SizeExpr: RCAT->getSizeExpr(), ASM: ArraySizeModifier(), IndexTypeQuals: 0); |
10906 | if (LVAT && getCanonicalType(T: LHSElem) == getCanonicalType(T: ResultType)) |
10907 | return LHS; |
10908 | if (RVAT && getCanonicalType(T: RHSElem) == getCanonicalType(T: ResultType)) |
10909 | return RHS; |
10910 | if (LVAT) { |
10911 | // FIXME: This isn't correct! But tricky to implement because |
10912 | // the array's size has to be the size of LHS, but the type |
10913 | // has to be different. |
10914 | return LHS; |
10915 | } |
10916 | if (RVAT) { |
10917 | // FIXME: This isn't correct! But tricky to implement because |
10918 | // the array's size has to be the size of RHS, but the type |
10919 | // has to be different. |
10920 | return RHS; |
10921 | } |
10922 | if (getCanonicalType(T: LHSElem) == getCanonicalType(T: ResultType)) return LHS; |
10923 | if (getCanonicalType(T: RHSElem) == getCanonicalType(T: ResultType)) return RHS; |
10924 | return getIncompleteArrayType(elementType: ResultType, ASM: ArraySizeModifier(), elementTypeQuals: 0); |
10925 | } |
10926 | case Type::FunctionNoProto: |
10927 | return mergeFunctionTypes(lhs: LHS, rhs: RHS, OfBlockPointer, Unqualified, |
10928 | /*AllowCXX=*/false, IsConditionalOperator); |
10929 | case Type::Record: |
10930 | case Type::Enum: |
10931 | return {}; |
10932 | case Type::Builtin: |
10933 | // Only exactly equal builtin types are compatible, which is tested above. |
10934 | return {}; |
10935 | case Type::Complex: |
10936 | // Distinct complex types are incompatible. |
10937 | return {}; |
10938 | case Type::Vector: |
10939 | // FIXME: The merged type should be an ExtVector! |
10940 | if (areCompatVectorTypes(LHSCan->castAs<VectorType>(), |
10941 | RHSCan->castAs<VectorType>())) |
10942 | return LHS; |
10943 | return {}; |
10944 | case Type::ConstantMatrix: |
10945 | if (areCompatMatrixTypes(LHSCan->castAs<ConstantMatrixType>(), |
10946 | RHSCan->castAs<ConstantMatrixType>())) |
10947 | return LHS; |
10948 | return {}; |
10949 | case Type::ObjCObject: { |
10950 | // Check if the types are assignment compatible. |
10951 | // FIXME: This should be type compatibility, e.g. whether |
10952 | // "LHS x; RHS x;" at global scope is legal. |
10953 | if (canAssignObjCInterfaces(LHS->castAs<ObjCObjectType>(), |
10954 | RHS->castAs<ObjCObjectType>())) |
10955 | return LHS; |
10956 | return {}; |
10957 | } |
10958 | case Type::ObjCObjectPointer: |
10959 | if (OfBlockPointer) { |
10960 | if (canAssignObjCInterfacesInBlockPointer( |
10961 | LHSOPT: LHS->castAs<ObjCObjectPointerType>(), |
10962 | RHSOPT: RHS->castAs<ObjCObjectPointerType>(), BlockReturnType)) |
10963 | return LHS; |
10964 | return {}; |
10965 | } |
10966 | if (canAssignObjCInterfaces(LHS->castAs<ObjCObjectPointerType>(), |
10967 | RHS->castAs<ObjCObjectPointerType>())) |
10968 | return LHS; |
10969 | return {}; |
10970 | case Type::Pipe: |
10971 | assert(LHS != RHS && |
10972 | "Equivalent pipe types should have already been handled!" ); |
10973 | return {}; |
10974 | case Type::ArrayParameter: |
10975 | assert(LHS != RHS && |
10976 | "Equivalent ArrayParameter types should have already been handled!" ); |
10977 | return {}; |
10978 | case Type::BitInt: { |
10979 | // Merge two bit-precise int types, while trying to preserve typedef info. |
10980 | bool LHSUnsigned = LHS->castAs<BitIntType>()->isUnsigned(); |
10981 | bool RHSUnsigned = RHS->castAs<BitIntType>()->isUnsigned(); |
10982 | unsigned LHSBits = LHS->castAs<BitIntType>()->getNumBits(); |
10983 | unsigned RHSBits = RHS->castAs<BitIntType>()->getNumBits(); |
10984 | |
10985 | // Like unsigned/int, shouldn't have a type if they don't match. |
10986 | if (LHSUnsigned != RHSUnsigned) |
10987 | return {}; |
10988 | |
10989 | if (LHSBits != RHSBits) |
10990 | return {}; |
10991 | return LHS; |
10992 | } |
10993 | } |
10994 | |
10995 | llvm_unreachable("Invalid Type::Class!" ); |
10996 | } |
10997 | |
10998 | bool ASTContext::mergeExtParameterInfo( |
10999 | const FunctionProtoType *FirstFnType, const FunctionProtoType *SecondFnType, |
11000 | bool &CanUseFirst, bool &CanUseSecond, |
11001 | SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &NewParamInfos) { |
11002 | assert(NewParamInfos.empty() && "param info list not empty" ); |
11003 | CanUseFirst = CanUseSecond = true; |
11004 | bool FirstHasInfo = FirstFnType->hasExtParameterInfos(); |
11005 | bool SecondHasInfo = SecondFnType->hasExtParameterInfos(); |
11006 | |
11007 | // Fast path: if the first type doesn't have ext parameter infos, |
11008 | // we match if and only if the second type also doesn't have them. |
11009 | if (!FirstHasInfo && !SecondHasInfo) |
11010 | return true; |
11011 | |
11012 | bool NeedParamInfo = false; |
11013 | size_t E = FirstHasInfo ? FirstFnType->getExtParameterInfos().size() |
11014 | : SecondFnType->getExtParameterInfos().size(); |
11015 | |
11016 | for (size_t I = 0; I < E; ++I) { |
11017 | FunctionProtoType::ExtParameterInfo FirstParam, SecondParam; |
11018 | if (FirstHasInfo) |
11019 | FirstParam = FirstFnType->getExtParameterInfo(I); |
11020 | if (SecondHasInfo) |
11021 | SecondParam = SecondFnType->getExtParameterInfo(I); |
11022 | |
11023 | // Cannot merge unless everything except the noescape flag matches. |
11024 | if (FirstParam.withIsNoEscape(NoEscape: false) != SecondParam.withIsNoEscape(NoEscape: false)) |
11025 | return false; |
11026 | |
11027 | bool FirstNoEscape = FirstParam.isNoEscape(); |
11028 | bool SecondNoEscape = SecondParam.isNoEscape(); |
11029 | bool IsNoEscape = FirstNoEscape && SecondNoEscape; |
11030 | NewParamInfos.push_back(Elt: FirstParam.withIsNoEscape(NoEscape: IsNoEscape)); |
11031 | if (NewParamInfos.back().getOpaqueValue()) |
11032 | NeedParamInfo = true; |
11033 | if (FirstNoEscape != IsNoEscape) |
11034 | CanUseFirst = false; |
11035 | if (SecondNoEscape != IsNoEscape) |
11036 | CanUseSecond = false; |
11037 | } |
11038 | |
11039 | if (!NeedParamInfo) |
11040 | NewParamInfos.clear(); |
11041 | |
11042 | return true; |
11043 | } |
11044 | |
11045 | void ASTContext::ResetObjCLayout(const ObjCContainerDecl *CD) { |
11046 | ObjCLayouts[CD] = nullptr; |
11047 | } |
11048 | |
11049 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
11050 | /// 'RHS' attributes and returns the merged version; including for function |
11051 | /// return types. |
11052 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
11053 | QualType LHSCan = getCanonicalType(T: LHS), |
11054 | RHSCan = getCanonicalType(T: RHS); |
11055 | // If two types are identical, they are compatible. |
11056 | if (LHSCan == RHSCan) |
11057 | return LHS; |
11058 | if (RHSCan->isFunctionType()) { |
11059 | if (!LHSCan->isFunctionType()) |
11060 | return {}; |
11061 | QualType OldReturnType = |
11062 | cast<FunctionType>(Val: RHSCan.getTypePtr())->getReturnType(); |
11063 | QualType NewReturnType = |
11064 | cast<FunctionType>(Val: LHSCan.getTypePtr())->getReturnType(); |
11065 | QualType ResReturnType = |
11066 | mergeObjCGCQualifiers(LHS: NewReturnType, RHS: OldReturnType); |
11067 | if (ResReturnType.isNull()) |
11068 | return {}; |
11069 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
11070 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
11071 | // In either case, use OldReturnType to build the new function type. |
11072 | const auto *F = LHS->castAs<FunctionType>(); |
11073 | if (const auto *FPT = cast<FunctionProtoType>(Val: F)) { |
11074 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
11075 | EPI.ExtInfo = getFunctionExtInfo(t: LHS); |
11076 | QualType ResultType = |
11077 | getFunctionType(ResultTy: OldReturnType, Args: FPT->getParamTypes(), EPI); |
11078 | return ResultType; |
11079 | } |
11080 | } |
11081 | return {}; |
11082 | } |
11083 | |
11084 | // If the qualifiers are different, the types can still be merged. |
11085 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
11086 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
11087 | if (LQuals != RQuals) { |
11088 | // If any of these qualifiers are different, we have a type mismatch. |
11089 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
11090 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
11091 | return {}; |
11092 | |
11093 | // Exactly one GC qualifier difference is allowed: __strong is |
11094 | // okay if the other type has no GC qualifier but is an Objective |
11095 | // C object pointer (i.e. implicitly strong by default). We fix |
11096 | // this by pretending that the unqualified type was actually |
11097 | // qualified __strong. |
11098 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
11099 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
11100 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements" ); |
11101 | |
11102 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
11103 | return {}; |
11104 | |
11105 | if (GC_L == Qualifiers::Strong) |
11106 | return LHS; |
11107 | if (GC_R == Qualifiers::Strong) |
11108 | return RHS; |
11109 | return {}; |
11110 | } |
11111 | |
11112 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
11113 | QualType LHSBaseQT = LHS->castAs<ObjCObjectPointerType>()->getPointeeType(); |
11114 | QualType RHSBaseQT = RHS->castAs<ObjCObjectPointerType>()->getPointeeType(); |
11115 | QualType ResQT = mergeObjCGCQualifiers(LHS: LHSBaseQT, RHS: RHSBaseQT); |
11116 | if (ResQT == LHSBaseQT) |
11117 | return LHS; |
11118 | if (ResQT == RHSBaseQT) |
11119 | return RHS; |
11120 | } |
11121 | return {}; |
11122 | } |
11123 | |
11124 | //===----------------------------------------------------------------------===// |
11125 | // Integer Predicates |
11126 | //===----------------------------------------------------------------------===// |
11127 | |
11128 | unsigned ASTContext::getIntWidth(QualType T) const { |
11129 | if (const auto *ET = T->getAs<EnumType>()) |
11130 | T = ET->getDecl()->getIntegerType(); |
11131 | if (T->isBooleanType()) |
11132 | return 1; |
11133 | if (const auto *EIT = T->getAs<BitIntType>()) |
11134 | return EIT->getNumBits(); |
11135 | // For builtin types, just use the standard type sizing method |
11136 | return (unsigned)getTypeSize(T); |
11137 | } |
11138 | |
11139 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
11140 | assert((T->hasIntegerRepresentation() || T->isEnumeralType() || |
11141 | T->isFixedPointType()) && |
11142 | "Unexpected type" ); |
11143 | |
11144 | // Turn <4 x signed int> -> <4 x unsigned int> |
11145 | if (const auto *VTy = T->getAs<VectorType>()) |
11146 | return getVectorType(vecType: getCorrespondingUnsignedType(T: VTy->getElementType()), |
11147 | NumElts: VTy->getNumElements(), VecKind: VTy->getVectorKind()); |
11148 | |
11149 | // For _BitInt, return an unsigned _BitInt with same width. |
11150 | if (const auto *EITy = T->getAs<BitIntType>()) |
11151 | return getBitIntType(/*Unsigned=*/IsUnsigned: true, NumBits: EITy->getNumBits()); |
11152 | |
11153 | // For enums, get the underlying integer type of the enum, and let the general |
11154 | // integer type signchanging code handle it. |
11155 | if (const auto *ETy = T->getAs<EnumType>()) |
11156 | T = ETy->getDecl()->getIntegerType(); |
11157 | |
11158 | switch (T->castAs<BuiltinType>()->getKind()) { |
11159 | case BuiltinType::Char_U: |
11160 | // Plain `char` is mapped to `unsigned char` even if it's already unsigned |
11161 | case BuiltinType::Char_S: |
11162 | case BuiltinType::SChar: |
11163 | case BuiltinType::Char8: |
11164 | return UnsignedCharTy; |
11165 | case BuiltinType::Short: |
11166 | return UnsignedShortTy; |
11167 | case BuiltinType::Int: |
11168 | return UnsignedIntTy; |
11169 | case BuiltinType::Long: |
11170 | return UnsignedLongTy; |
11171 | case BuiltinType::LongLong: |
11172 | return UnsignedLongLongTy; |
11173 | case BuiltinType::Int128: |
11174 | return UnsignedInt128Ty; |
11175 | // wchar_t is special. It is either signed or not, but when it's signed, |
11176 | // there's no matching "unsigned wchar_t". Therefore we return the unsigned |
11177 | // version of its underlying type instead. |
11178 | case BuiltinType::WChar_S: |
11179 | return getUnsignedWCharType(); |
11180 | |
11181 | case BuiltinType::ShortAccum: |
11182 | return UnsignedShortAccumTy; |
11183 | case BuiltinType::Accum: |
11184 | return UnsignedAccumTy; |
11185 | case BuiltinType::LongAccum: |
11186 | return UnsignedLongAccumTy; |
11187 | case BuiltinType::SatShortAccum: |
11188 | return SatUnsignedShortAccumTy; |
11189 | case BuiltinType::SatAccum: |
11190 | return SatUnsignedAccumTy; |
11191 | case BuiltinType::SatLongAccum: |
11192 | return SatUnsignedLongAccumTy; |
11193 | case BuiltinType::ShortFract: |
11194 | return UnsignedShortFractTy; |
11195 | case BuiltinType::Fract: |
11196 | return UnsignedFractTy; |
11197 | case BuiltinType::LongFract: |
11198 | return UnsignedLongFractTy; |
11199 | case BuiltinType::SatShortFract: |
11200 | return SatUnsignedShortFractTy; |
11201 | case BuiltinType::SatFract: |
11202 | return SatUnsignedFractTy; |
11203 | case BuiltinType::SatLongFract: |
11204 | return SatUnsignedLongFractTy; |
11205 | default: |
11206 | assert((T->hasUnsignedIntegerRepresentation() || |
11207 | T->isUnsignedFixedPointType()) && |
11208 | "Unexpected signed integer or fixed point type" ); |
11209 | return T; |
11210 | } |
11211 | } |
11212 | |
11213 | QualType ASTContext::getCorrespondingSignedType(QualType T) const { |
11214 | assert((T->hasIntegerRepresentation() || T->isEnumeralType() || |
11215 | T->isFixedPointType()) && |
11216 | "Unexpected type" ); |
11217 | |
11218 | // Turn <4 x unsigned int> -> <4 x signed int> |
11219 | if (const auto *VTy = T->getAs<VectorType>()) |
11220 | return getVectorType(vecType: getCorrespondingSignedType(T: VTy->getElementType()), |
11221 | NumElts: VTy->getNumElements(), VecKind: VTy->getVectorKind()); |
11222 | |
11223 | // For _BitInt, return a signed _BitInt with same width. |
11224 | if (const auto *EITy = T->getAs<BitIntType>()) |
11225 | return getBitIntType(/*Unsigned=*/IsUnsigned: false, NumBits: EITy->getNumBits()); |
11226 | |
11227 | // For enums, get the underlying integer type of the enum, and let the general |
11228 | // integer type signchanging code handle it. |
11229 | if (const auto *ETy = T->getAs<EnumType>()) |
11230 | T = ETy->getDecl()->getIntegerType(); |
11231 | |
11232 | switch (T->castAs<BuiltinType>()->getKind()) { |
11233 | case BuiltinType::Char_S: |
11234 | // Plain `char` is mapped to `signed char` even if it's already signed |
11235 | case BuiltinType::Char_U: |
11236 | case BuiltinType::UChar: |
11237 | case BuiltinType::Char8: |
11238 | return SignedCharTy; |
11239 | case BuiltinType::UShort: |
11240 | return ShortTy; |
11241 | case BuiltinType::UInt: |
11242 | return IntTy; |
11243 | case BuiltinType::ULong: |
11244 | return LongTy; |
11245 | case BuiltinType::ULongLong: |
11246 | return LongLongTy; |
11247 | case BuiltinType::UInt128: |
11248 | return Int128Ty; |
11249 | // wchar_t is special. It is either unsigned or not, but when it's unsigned, |
11250 | // there's no matching "signed wchar_t". Therefore we return the signed |
11251 | // version of its underlying type instead. |
11252 | case BuiltinType::WChar_U: |
11253 | return getSignedWCharType(); |
11254 | |
11255 | case BuiltinType::UShortAccum: |
11256 | return ShortAccumTy; |
11257 | case BuiltinType::UAccum: |
11258 | return AccumTy; |
11259 | case BuiltinType::ULongAccum: |
11260 | return LongAccumTy; |
11261 | case BuiltinType::SatUShortAccum: |
11262 | return SatShortAccumTy; |
11263 | case BuiltinType::SatUAccum: |
11264 | return SatAccumTy; |
11265 | case BuiltinType::SatULongAccum: |
11266 | return SatLongAccumTy; |
11267 | case BuiltinType::UShortFract: |
11268 | return ShortFractTy; |
11269 | case BuiltinType::UFract: |
11270 | return FractTy; |
11271 | case BuiltinType::ULongFract: |
11272 | return LongFractTy; |
11273 | case BuiltinType::SatUShortFract: |
11274 | return SatShortFractTy; |
11275 | case BuiltinType::SatUFract: |
11276 | return SatFractTy; |
11277 | case BuiltinType::SatULongFract: |
11278 | return SatLongFractTy; |
11279 | default: |
11280 | assert( |
11281 | (T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) && |
11282 | "Unexpected signed integer or fixed point type" ); |
11283 | return T; |
11284 | } |
11285 | } |
11286 | |
11287 | ASTMutationListener::~ASTMutationListener() = default; |
11288 | |
11289 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, |
11290 | QualType ReturnType) {} |
11291 | |
11292 | //===----------------------------------------------------------------------===// |
11293 | // Builtin Type Computation |
11294 | //===----------------------------------------------------------------------===// |
11295 | |
11296 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
11297 | /// pointer over the consumed characters. This returns the resultant type. If |
11298 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
11299 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
11300 | /// a vector of "i*". |
11301 | /// |
11302 | /// RequiresICE is filled in on return to indicate whether the value is required |
11303 | /// to be an Integer Constant Expression. |
11304 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
11305 | ASTContext::GetBuiltinTypeError &Error, |
11306 | bool &RequiresICE, |
11307 | bool AllowTypeModifiers) { |
11308 | // Modifiers. |
11309 | int HowLong = 0; |
11310 | bool Signed = false, Unsigned = false; |
11311 | RequiresICE = false; |
11312 | |
11313 | // Read the prefixed modifiers first. |
11314 | bool Done = false; |
11315 | #ifndef NDEBUG |
11316 | bool IsSpecial = false; |
11317 | #endif |
11318 | while (!Done) { |
11319 | switch (*Str++) { |
11320 | default: Done = true; --Str; break; |
11321 | case 'I': |
11322 | RequiresICE = true; |
11323 | break; |
11324 | case 'S': |
11325 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!" ); |
11326 | assert(!Signed && "Can't use 'S' modifier multiple times!" ); |
11327 | Signed = true; |
11328 | break; |
11329 | case 'U': |
11330 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!" ); |
11331 | assert(!Unsigned && "Can't use 'U' modifier multiple times!" ); |
11332 | Unsigned = true; |
11333 | break; |
11334 | case 'L': |
11335 | assert(!IsSpecial && "Can't use 'L' with 'W', 'N', 'Z' or 'O' modifiers" ); |
11336 | assert(HowLong <= 2 && "Can't have LLLL modifier" ); |
11337 | ++HowLong; |
11338 | break; |
11339 | case 'N': |
11340 | // 'N' behaves like 'L' for all non LP64 targets and 'int' otherwise. |
11341 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!" ); |
11342 | assert(HowLong == 0 && "Can't use both 'L' and 'N' modifiers!" ); |
11343 | #ifndef NDEBUG |
11344 | IsSpecial = true; |
11345 | #endif |
11346 | if (Context.getTargetInfo().getLongWidth() == 32) |
11347 | ++HowLong; |
11348 | break; |
11349 | case 'W': |
11350 | // This modifier represents int64 type. |
11351 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!" ); |
11352 | assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!" ); |
11353 | #ifndef NDEBUG |
11354 | IsSpecial = true; |
11355 | #endif |
11356 | switch (Context.getTargetInfo().getInt64Type()) { |
11357 | default: |
11358 | llvm_unreachable("Unexpected integer type" ); |
11359 | case TargetInfo::SignedLong: |
11360 | HowLong = 1; |
11361 | break; |
11362 | case TargetInfo::SignedLongLong: |
11363 | HowLong = 2; |
11364 | break; |
11365 | } |
11366 | break; |
11367 | case 'Z': |
11368 | // This modifier represents int32 type. |
11369 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!" ); |
11370 | assert(HowLong == 0 && "Can't use both 'L' and 'Z' modifiers!" ); |
11371 | #ifndef NDEBUG |
11372 | IsSpecial = true; |
11373 | #endif |
11374 | switch (Context.getTargetInfo().getIntTypeByWidth(BitWidth: 32, IsSigned: true)) { |
11375 | default: |
11376 | llvm_unreachable("Unexpected integer type" ); |
11377 | case TargetInfo::SignedInt: |
11378 | HowLong = 0; |
11379 | break; |
11380 | case TargetInfo::SignedLong: |
11381 | HowLong = 1; |
11382 | break; |
11383 | case TargetInfo::SignedLongLong: |
11384 | HowLong = 2; |
11385 | break; |
11386 | } |
11387 | break; |
11388 | case 'O': |
11389 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!" ); |
11390 | assert(HowLong == 0 && "Can't use both 'L' and 'O' modifiers!" ); |
11391 | #ifndef NDEBUG |
11392 | IsSpecial = true; |
11393 | #endif |
11394 | if (Context.getLangOpts().OpenCL) |
11395 | HowLong = 1; |
11396 | else |
11397 | HowLong = 2; |
11398 | break; |
11399 | } |
11400 | } |
11401 | |
11402 | QualType Type; |
11403 | |
11404 | // Read the base type. |
11405 | switch (*Str++) { |
11406 | default: llvm_unreachable("Unknown builtin type letter!" ); |
11407 | case 'x': |
11408 | assert(HowLong == 0 && !Signed && !Unsigned && |
11409 | "Bad modifiers used with 'x'!" ); |
11410 | Type = Context.Float16Ty; |
11411 | break; |
11412 | case 'y': |
11413 | assert(HowLong == 0 && !Signed && !Unsigned && |
11414 | "Bad modifiers used with 'y'!" ); |
11415 | Type = Context.BFloat16Ty; |
11416 | break; |
11417 | case 'v': |
11418 | assert(HowLong == 0 && !Signed && !Unsigned && |
11419 | "Bad modifiers used with 'v'!" ); |
11420 | Type = Context.VoidTy; |
11421 | break; |
11422 | case 'h': |
11423 | assert(HowLong == 0 && !Signed && !Unsigned && |
11424 | "Bad modifiers used with 'h'!" ); |
11425 | Type = Context.HalfTy; |
11426 | break; |
11427 | case 'f': |
11428 | assert(HowLong == 0 && !Signed && !Unsigned && |
11429 | "Bad modifiers used with 'f'!" ); |
11430 | Type = Context.FloatTy; |
11431 | break; |
11432 | case 'd': |
11433 | assert(HowLong < 3 && !Signed && !Unsigned && |
11434 | "Bad modifiers used with 'd'!" ); |
11435 | if (HowLong == 1) |
11436 | Type = Context.LongDoubleTy; |
11437 | else if (HowLong == 2) |
11438 | Type = Context.Float128Ty; |
11439 | else |
11440 | Type = Context.DoubleTy; |
11441 | break; |
11442 | case 's': |
11443 | assert(HowLong == 0 && "Bad modifiers used with 's'!" ); |
11444 | if (Unsigned) |
11445 | Type = Context.UnsignedShortTy; |
11446 | else |
11447 | Type = Context.ShortTy; |
11448 | break; |
11449 | case 'i': |
11450 | if (HowLong == 3) |
11451 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
11452 | else if (HowLong == 2) |
11453 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
11454 | else if (HowLong == 1) |
11455 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
11456 | else |
11457 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
11458 | break; |
11459 | case 'c': |
11460 | assert(HowLong == 0 && "Bad modifiers used with 'c'!" ); |
11461 | if (Signed) |
11462 | Type = Context.SignedCharTy; |
11463 | else if (Unsigned) |
11464 | Type = Context.UnsignedCharTy; |
11465 | else |
11466 | Type = Context.CharTy; |
11467 | break; |
11468 | case 'b': // boolean |
11469 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!" ); |
11470 | Type = Context.BoolTy; |
11471 | break; |
11472 | case 'z': // size_t. |
11473 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!" ); |
11474 | Type = Context.getSizeType(); |
11475 | break; |
11476 | case 'w': // wchar_t. |
11477 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'w'!" ); |
11478 | Type = Context.getWideCharType(); |
11479 | break; |
11480 | case 'F': |
11481 | Type = Context.getCFConstantStringType(); |
11482 | break; |
11483 | case 'G': |
11484 | Type = Context.getObjCIdType(); |
11485 | break; |
11486 | case 'H': |
11487 | Type = Context.getObjCSelType(); |
11488 | break; |
11489 | case 'M': |
11490 | Type = Context.getObjCSuperType(); |
11491 | break; |
11492 | case 'a': |
11493 | Type = Context.getBuiltinVaListType(); |
11494 | assert(!Type.isNull() && "builtin va list type not initialized!" ); |
11495 | break; |
11496 | case 'A': |
11497 | // This is a "reference" to a va_list; however, what exactly |
11498 | // this means depends on how va_list is defined. There are two |
11499 | // different kinds of va_list: ones passed by value, and ones |
11500 | // passed by reference. An example of a by-value va_list is |
11501 | // x86, where va_list is a char*. An example of by-ref va_list |
11502 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
11503 | // we want this argument to be a char*&; for x86-64, we want |
11504 | // it to be a __va_list_tag*. |
11505 | Type = Context.getBuiltinVaListType(); |
11506 | assert(!Type.isNull() && "builtin va list type not initialized!" ); |
11507 | if (Type->isArrayType()) |
11508 | Type = Context.getArrayDecayedType(Ty: Type); |
11509 | else |
11510 | Type = Context.getLValueReferenceType(T: Type); |
11511 | break; |
11512 | case 'q': { |
11513 | char *End; |
11514 | unsigned NumElements = strtoul(nptr: Str, endptr: &End, base: 10); |
11515 | assert(End != Str && "Missing vector size" ); |
11516 | Str = End; |
11517 | |
11518 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
11519 | RequiresICE, AllowTypeModifiers: false); |
11520 | assert(!RequiresICE && "Can't require vector ICE" ); |
11521 | |
11522 | Type = Context.getScalableVectorType(EltTy: ElementType, NumElts: NumElements); |
11523 | break; |
11524 | } |
11525 | case 'Q': { |
11526 | switch (*Str++) { |
11527 | case 'a': { |
11528 | Type = Context.SveCountTy; |
11529 | break; |
11530 | } |
11531 | default: |
11532 | llvm_unreachable("Unexpected target builtin type" ); |
11533 | } |
11534 | break; |
11535 | } |
11536 | case 'V': { |
11537 | char *End; |
11538 | unsigned NumElements = strtoul(nptr: Str, endptr: &End, base: 10); |
11539 | assert(End != Str && "Missing vector size" ); |
11540 | Str = End; |
11541 | |
11542 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
11543 | RequiresICE, AllowTypeModifiers: false); |
11544 | assert(!RequiresICE && "Can't require vector ICE" ); |
11545 | |
11546 | // TODO: No way to make AltiVec vectors in builtins yet. |
11547 | Type = Context.getVectorType(vecType: ElementType, NumElts: NumElements, VecKind: VectorKind::Generic); |
11548 | break; |
11549 | } |
11550 | case 'E': { |
11551 | char *End; |
11552 | |
11553 | unsigned NumElements = strtoul(nptr: Str, endptr: &End, base: 10); |
11554 | assert(End != Str && "Missing vector size" ); |
11555 | |
11556 | Str = End; |
11557 | |
11558 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
11559 | AllowTypeModifiers: false); |
11560 | Type = Context.getExtVectorType(vecType: ElementType, NumElts: NumElements); |
11561 | break; |
11562 | } |
11563 | case 'X': { |
11564 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
11565 | AllowTypeModifiers: false); |
11566 | assert(!RequiresICE && "Can't require complex ICE" ); |
11567 | Type = Context.getComplexType(T: ElementType); |
11568 | break; |
11569 | } |
11570 | case 'Y': |
11571 | Type = Context.getPointerDiffType(); |
11572 | break; |
11573 | case 'P': |
11574 | Type = Context.getFILEType(); |
11575 | if (Type.isNull()) { |
11576 | Error = ASTContext::GE_Missing_stdio; |
11577 | return {}; |
11578 | } |
11579 | break; |
11580 | case 'J': |
11581 | if (Signed) |
11582 | Type = Context.getsigjmp_bufType(); |
11583 | else |
11584 | Type = Context.getjmp_bufType(); |
11585 | |
11586 | if (Type.isNull()) { |
11587 | Error = ASTContext::GE_Missing_setjmp; |
11588 | return {}; |
11589 | } |
11590 | break; |
11591 | case 'K': |
11592 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!" ); |
11593 | Type = Context.getucontext_tType(); |
11594 | |
11595 | if (Type.isNull()) { |
11596 | Error = ASTContext::GE_Missing_ucontext; |
11597 | return {}; |
11598 | } |
11599 | break; |
11600 | case 'p': |
11601 | Type = Context.getProcessIDType(); |
11602 | break; |
11603 | } |
11604 | |
11605 | // If there are modifiers and if we're allowed to parse them, go for it. |
11606 | Done = !AllowTypeModifiers; |
11607 | while (!Done) { |
11608 | switch (char c = *Str++) { |
11609 | default: Done = true; --Str; break; |
11610 | case '*': |
11611 | case '&': { |
11612 | // Both pointers and references can have their pointee types |
11613 | // qualified with an address space. |
11614 | char *End; |
11615 | unsigned AddrSpace = strtoul(nptr: Str, endptr: &End, base: 10); |
11616 | if (End != Str) { |
11617 | // Note AddrSpace == 0 is not the same as an unspecified address space. |
11618 | Type = Context.getAddrSpaceQualType( |
11619 | T: Type, |
11620 | AddressSpace: Context.getLangASForBuiltinAddressSpace(AS: AddrSpace)); |
11621 | Str = End; |
11622 | } |
11623 | if (c == '*') |
11624 | Type = Context.getPointerType(T: Type); |
11625 | else |
11626 | Type = Context.getLValueReferenceType(T: Type); |
11627 | break; |
11628 | } |
11629 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
11630 | case 'C': |
11631 | Type = Type.withConst(); |
11632 | break; |
11633 | case 'D': |
11634 | Type = Context.getVolatileType(T: Type); |
11635 | break; |
11636 | case 'R': |
11637 | Type = Type.withRestrict(); |
11638 | break; |
11639 | } |
11640 | } |
11641 | |
11642 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
11643 | "Integer constant 'I' type must be an integer" ); |
11644 | |
11645 | return Type; |
11646 | } |
11647 | |
11648 | // On some targets such as PowerPC, some of the builtins are defined with custom |
11649 | // type descriptors for target-dependent types. These descriptors are decoded in |
11650 | // other functions, but it may be useful to be able to fall back to default |
11651 | // descriptor decoding to define builtins mixing target-dependent and target- |
11652 | // independent types. This function allows decoding one type descriptor with |
11653 | // default decoding. |
11654 | QualType ASTContext::DecodeTypeStr(const char *&Str, const ASTContext &Context, |
11655 | GetBuiltinTypeError &Error, bool &RequireICE, |
11656 | bool AllowTypeModifiers) const { |
11657 | return DecodeTypeFromStr(Str, Context, Error, RequiresICE&: RequireICE, AllowTypeModifiers); |
11658 | } |
11659 | |
11660 | /// GetBuiltinType - Return the type for the specified builtin. |
11661 | QualType ASTContext::GetBuiltinType(unsigned Id, |
11662 | GetBuiltinTypeError &Error, |
11663 | unsigned *IntegerConstantArgs) const { |
11664 | const char *TypeStr = BuiltinInfo.getTypeString(ID: Id); |
11665 | if (TypeStr[0] == '\0') { |
11666 | Error = GE_Missing_type; |
11667 | return {}; |
11668 | } |
11669 | |
11670 | SmallVector<QualType, 8> ArgTypes; |
11671 | |
11672 | bool RequiresICE = false; |
11673 | Error = GE_None; |
11674 | QualType ResType = DecodeTypeFromStr(Str&: TypeStr, Context: *this, Error, |
11675 | RequiresICE, AllowTypeModifiers: true); |
11676 | if (Error != GE_None) |
11677 | return {}; |
11678 | |
11679 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE" ); |
11680 | |
11681 | while (TypeStr[0] && TypeStr[0] != '.') { |
11682 | QualType Ty = DecodeTypeFromStr(Str&: TypeStr, Context: *this, Error, RequiresICE, AllowTypeModifiers: true); |
11683 | if (Error != GE_None) |
11684 | return {}; |
11685 | |
11686 | // If this argument is required to be an IntegerConstantExpression and the |
11687 | // caller cares, fill in the bitmask we return. |
11688 | if (RequiresICE && IntegerConstantArgs) |
11689 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
11690 | |
11691 | // Do array -> pointer decay. The builtin should use the decayed type. |
11692 | if (Ty->isArrayType()) |
11693 | Ty = getArrayDecayedType(Ty); |
11694 | |
11695 | ArgTypes.push_back(Elt: Ty); |
11696 | } |
11697 | |
11698 | if (Id == Builtin::BI__GetExceptionInfo) |
11699 | return {}; |
11700 | |
11701 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
11702 | "'.' should only occur at end of builtin type list!" ); |
11703 | |
11704 | bool Variadic = (TypeStr[0] == '.'); |
11705 | |
11706 | FunctionType::ExtInfo EI(getDefaultCallingConvention( |
11707 | IsVariadic: Variadic, /*IsCXXMethod=*/false, /*IsBuiltin=*/true)); |
11708 | if (BuiltinInfo.isNoReturn(ID: Id)) EI = EI.withNoReturn(noReturn: true); |
11709 | |
11710 | |
11711 | // We really shouldn't be making a no-proto type here. |
11712 | if (ArgTypes.empty() && Variadic && !getLangOpts().requiresStrictPrototypes()) |
11713 | return getFunctionNoProtoType(ResultTy: ResType, Info: EI); |
11714 | |
11715 | FunctionProtoType::ExtProtoInfo EPI; |
11716 | EPI.ExtInfo = EI; |
11717 | EPI.Variadic = Variadic; |
11718 | if (getLangOpts().CPlusPlus && BuiltinInfo.isNoThrow(ID: Id)) |
11719 | EPI.ExceptionSpec.Type = |
11720 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
11721 | |
11722 | return getFunctionType(ResultTy: ResType, Args: ArgTypes, EPI); |
11723 | } |
11724 | |
11725 | static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, |
11726 | const FunctionDecl *FD) { |
11727 | if (!FD->isExternallyVisible()) |
11728 | return GVA_Internal; |
11729 | |
11730 | // Non-user-provided functions get emitted as weak definitions with every |
11731 | // use, no matter whether they've been explicitly instantiated etc. |
11732 | if (!FD->isUserProvided()) |
11733 | return GVA_DiscardableODR; |
11734 | |
11735 | GVALinkage External; |
11736 | switch (FD->getTemplateSpecializationKind()) { |
11737 | case TSK_Undeclared: |
11738 | case TSK_ExplicitSpecialization: |
11739 | External = GVA_StrongExternal; |
11740 | break; |
11741 | |
11742 | case TSK_ExplicitInstantiationDefinition: |
11743 | return GVA_StrongODR; |
11744 | |
11745 | // C++11 [temp.explicit]p10: |
11746 | // [ Note: The intent is that an inline function that is the subject of |
11747 | // an explicit instantiation declaration will still be implicitly |
11748 | // instantiated when used so that the body can be considered for |
11749 | // inlining, but that no out-of-line copy of the inline function would be |
11750 | // generated in the translation unit. -- end note ] |
11751 | case TSK_ExplicitInstantiationDeclaration: |
11752 | return GVA_AvailableExternally; |
11753 | |
11754 | case TSK_ImplicitInstantiation: |
11755 | External = GVA_DiscardableODR; |
11756 | break; |
11757 | } |
11758 | |
11759 | if (!FD->isInlined()) |
11760 | return External; |
11761 | |
11762 | if ((!Context.getLangOpts().CPlusPlus && |
11763 | !Context.getTargetInfo().getCXXABI().isMicrosoft() && |
11764 | !FD->hasAttr<DLLExportAttr>()) || |
11765 | FD->hasAttr<GNUInlineAttr>()) { |
11766 | // FIXME: This doesn't match gcc's behavior for dllexport inline functions. |
11767 | |
11768 | // GNU or C99 inline semantics. Determine whether this symbol should be |
11769 | // externally visible. |
11770 | if (FD->isInlineDefinitionExternallyVisible()) |
11771 | return External; |
11772 | |
11773 | // C99 inline semantics, where the symbol is not externally visible. |
11774 | return GVA_AvailableExternally; |
11775 | } |
11776 | |
11777 | // Functions specified with extern and inline in -fms-compatibility mode |
11778 | // forcibly get emitted. While the body of the function cannot be later |
11779 | // replaced, the function definition cannot be discarded. |
11780 | if (FD->isMSExternInline()) |
11781 | return GVA_StrongODR; |
11782 | |
11783 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
11784 | isa<CXXConstructorDecl>(Val: FD) && |
11785 | cast<CXXConstructorDecl>(Val: FD)->isInheritingConstructor()) |
11786 | // Our approach to inheriting constructors is fundamentally different from |
11787 | // that used by the MS ABI, so keep our inheriting constructor thunks |
11788 | // internal rather than trying to pick an unambiguous mangling for them. |
11789 | return GVA_Internal; |
11790 | |
11791 | return GVA_DiscardableODR; |
11792 | } |
11793 | |
11794 | static GVALinkage adjustGVALinkageForAttributes(const ASTContext &Context, |
11795 | const Decl *D, GVALinkage L) { |
11796 | // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx |
11797 | // dllexport/dllimport on inline functions. |
11798 | if (D->hasAttr<DLLImportAttr>()) { |
11799 | if (L == GVA_DiscardableODR || L == GVA_StrongODR) |
11800 | return GVA_AvailableExternally; |
11801 | } else if (D->hasAttr<DLLExportAttr>()) { |
11802 | if (L == GVA_DiscardableODR) |
11803 | return GVA_StrongODR; |
11804 | } else if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice) { |
11805 | // Device-side functions with __global__ attribute must always be |
11806 | // visible externally so they can be launched from host. |
11807 | if (D->hasAttr<CUDAGlobalAttr>() && |
11808 | (L == GVA_DiscardableODR || L == GVA_Internal)) |
11809 | return GVA_StrongODR; |
11810 | // Single source offloading languages like CUDA/HIP need to be able to |
11811 | // access static device variables from host code of the same compilation |
11812 | // unit. This is done by externalizing the static variable with a shared |
11813 | // name between the host and device compilation which is the same for the |
11814 | // same compilation unit whereas different among different compilation |
11815 | // units. |
11816 | if (Context.shouldExternalize(D)) |
11817 | return GVA_StrongExternal; |
11818 | } |
11819 | return L; |
11820 | } |
11821 | |
11822 | /// Adjust the GVALinkage for a declaration based on what an external AST source |
11823 | /// knows about whether there can be other definitions of this declaration. |
11824 | static GVALinkage |
11825 | adjustGVALinkageForExternalDefinitionKind(const ASTContext &Ctx, const Decl *D, |
11826 | GVALinkage L) { |
11827 | ExternalASTSource *Source = Ctx.getExternalSource(); |
11828 | if (!Source) |
11829 | return L; |
11830 | |
11831 | switch (Source->hasExternalDefinitions(D)) { |
11832 | case ExternalASTSource::EK_Never: |
11833 | // Other translation units rely on us to provide the definition. |
11834 | if (L == GVA_DiscardableODR) |
11835 | return GVA_StrongODR; |
11836 | break; |
11837 | |
11838 | case ExternalASTSource::EK_Always: |
11839 | return GVA_AvailableExternally; |
11840 | |
11841 | case ExternalASTSource::EK_ReplyHazy: |
11842 | break; |
11843 | } |
11844 | return L; |
11845 | } |
11846 | |
11847 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { |
11848 | return adjustGVALinkageForExternalDefinitionKind(*this, FD, |
11849 | adjustGVALinkageForAttributes(*this, FD, |
11850 | basicGVALinkageForFunction(Context: *this, FD))); |
11851 | } |
11852 | |
11853 | static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, |
11854 | const VarDecl *VD) { |
11855 | // As an extension for interactive REPLs, make sure constant variables are |
11856 | // only emitted once instead of LinkageComputer::getLVForNamespaceScopeDecl |
11857 | // marking them as internal. |
11858 | if (Context.getLangOpts().CPlusPlus && |
11859 | Context.getLangOpts().IncrementalExtensions && |
11860 | VD->getType().isConstQualified() && |
11861 | !VD->getType().isVolatileQualified() && !VD->isInline() && |
11862 | !isa<VarTemplateSpecializationDecl>(Val: VD) && !VD->getDescribedVarTemplate()) |
11863 | return GVA_DiscardableODR; |
11864 | |
11865 | if (!VD->isExternallyVisible()) |
11866 | return GVA_Internal; |
11867 | |
11868 | if (VD->isStaticLocal()) { |
11869 | const DeclContext *LexicalContext = VD->getParentFunctionOrMethod(); |
11870 | while (LexicalContext && !isa<FunctionDecl>(Val: LexicalContext)) |
11871 | LexicalContext = LexicalContext->getLexicalParent(); |
11872 | |
11873 | // ObjC Blocks can create local variables that don't have a FunctionDecl |
11874 | // LexicalContext. |
11875 | if (!LexicalContext) |
11876 | return GVA_DiscardableODR; |
11877 | |
11878 | // Otherwise, let the static local variable inherit its linkage from the |
11879 | // nearest enclosing function. |
11880 | auto StaticLocalLinkage = |
11881 | Context.GetGVALinkageForFunction(FD: cast<FunctionDecl>(Val: LexicalContext)); |
11882 | |
11883 | // Itanium ABI 5.2.2: "Each COMDAT group [for a static local variable] must |
11884 | // be emitted in any object with references to the symbol for the object it |
11885 | // contains, whether inline or out-of-line." |
11886 | // Similar behavior is observed with MSVC. An alternative ABI could use |
11887 | // StrongODR/AvailableExternally to match the function, but none are |
11888 | // known/supported currently. |
11889 | if (StaticLocalLinkage == GVA_StrongODR || |
11890 | StaticLocalLinkage == GVA_AvailableExternally) |
11891 | return GVA_DiscardableODR; |
11892 | return StaticLocalLinkage; |
11893 | } |
11894 | |
11895 | // MSVC treats in-class initialized static data members as definitions. |
11896 | // By giving them non-strong linkage, out-of-line definitions won't |
11897 | // cause link errors. |
11898 | if (Context.isMSStaticDataMemberInlineDefinition(VD)) |
11899 | return GVA_DiscardableODR; |
11900 | |
11901 | // Most non-template variables have strong linkage; inline variables are |
11902 | // linkonce_odr or (occasionally, for compatibility) weak_odr. |
11903 | GVALinkage StrongLinkage; |
11904 | switch (Context.getInlineVariableDefinitionKind(VD)) { |
11905 | case ASTContext::InlineVariableDefinitionKind::None: |
11906 | StrongLinkage = GVA_StrongExternal; |
11907 | break; |
11908 | case ASTContext::InlineVariableDefinitionKind::Weak: |
11909 | case ASTContext::InlineVariableDefinitionKind::WeakUnknown: |
11910 | StrongLinkage = GVA_DiscardableODR; |
11911 | break; |
11912 | case ASTContext::InlineVariableDefinitionKind::Strong: |
11913 | StrongLinkage = GVA_StrongODR; |
11914 | break; |
11915 | } |
11916 | |
11917 | switch (VD->getTemplateSpecializationKind()) { |
11918 | case TSK_Undeclared: |
11919 | return StrongLinkage; |
11920 | |
11921 | case TSK_ExplicitSpecialization: |
11922 | return Context.getTargetInfo().getCXXABI().isMicrosoft() && |
11923 | VD->isStaticDataMember() |
11924 | ? GVA_StrongODR |
11925 | : StrongLinkage; |
11926 | |
11927 | case TSK_ExplicitInstantiationDefinition: |
11928 | return GVA_StrongODR; |
11929 | |
11930 | case TSK_ExplicitInstantiationDeclaration: |
11931 | return GVA_AvailableExternally; |
11932 | |
11933 | case TSK_ImplicitInstantiation: |
11934 | return GVA_DiscardableODR; |
11935 | } |
11936 | |
11937 | llvm_unreachable("Invalid Linkage!" ); |
11938 | } |
11939 | |
11940 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) const { |
11941 | return adjustGVALinkageForExternalDefinitionKind(*this, VD, |
11942 | adjustGVALinkageForAttributes(*this, VD, |
11943 | basicGVALinkageForVariable(Context: *this, VD))); |
11944 | } |
11945 | |
11946 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
11947 | if (const auto *VD = dyn_cast<VarDecl>(Val: D)) { |
11948 | if (!VD->isFileVarDecl()) |
11949 | return false; |
11950 | // Global named register variables (GNU extension) are never emitted. |
11951 | if (VD->getStorageClass() == SC_Register) |
11952 | return false; |
11953 | if (VD->getDescribedVarTemplate() || |
11954 | isa<VarTemplatePartialSpecializationDecl>(Val: VD)) |
11955 | return false; |
11956 | } else if (const auto *FD = dyn_cast<FunctionDecl>(Val: D)) { |
11957 | // We never need to emit an uninstantiated function template. |
11958 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
11959 | return false; |
11960 | } else if (isa<PragmaCommentDecl>(Val: D)) |
11961 | return true; |
11962 | else if (isa<PragmaDetectMismatchDecl>(Val: D)) |
11963 | return true; |
11964 | else if (isa<OMPRequiresDecl>(Val: D)) |
11965 | return true; |
11966 | else if (isa<OMPThreadPrivateDecl>(Val: D)) |
11967 | return !D->getDeclContext()->isDependentContext(); |
11968 | else if (isa<OMPAllocateDecl>(Val: D)) |
11969 | return !D->getDeclContext()->isDependentContext(); |
11970 | else if (isa<OMPDeclareReductionDecl>(Val: D) || isa<OMPDeclareMapperDecl>(Val: D)) |
11971 | return !D->getDeclContext()->isDependentContext(); |
11972 | else if (isa<ImportDecl>(Val: D)) |
11973 | return true; |
11974 | else |
11975 | return false; |
11976 | |
11977 | // If this is a member of a class template, we do not need to emit it. |
11978 | if (D->getDeclContext()->isDependentContext()) |
11979 | return false; |
11980 | |
11981 | // Weak references don't produce any output by themselves. |
11982 | if (D->hasAttr<WeakRefAttr>()) |
11983 | return false; |
11984 | |
11985 | // Aliases and used decls are required. |
11986 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
11987 | return true; |
11988 | |
11989 | if (const auto *FD = dyn_cast<FunctionDecl>(Val: D)) { |
11990 | // Forward declarations aren't required. |
11991 | if (!FD->doesThisDeclarationHaveABody()) |
11992 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
11993 | |
11994 | // Constructors and destructors are required. |
11995 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
11996 | return true; |
11997 | |
11998 | // The key function for a class is required. This rule only comes |
11999 | // into play when inline functions can be key functions, though. |
12000 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
12001 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FD)) { |
12002 | const CXXRecordDecl *RD = MD->getParent(); |
12003 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
12004 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
12005 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
12006 | return true; |
12007 | } |
12008 | } |
12009 | } |
12010 | |
12011 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
12012 | |
12013 | // static, static inline, always_inline, and extern inline functions can |
12014 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
12015 | // Implicit template instantiations can also be deferred in C++. |
12016 | return !isDiscardableGVALinkage(L: Linkage); |
12017 | } |
12018 | |
12019 | const auto *VD = cast<VarDecl>(Val: D); |
12020 | assert(VD->isFileVarDecl() && "Expected file scoped var" ); |
12021 | |
12022 | // If the decl is marked as `declare target to`, it should be emitted for the |
12023 | // host and for the device. |
12024 | if (LangOpts.OpenMP && |
12025 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) |
12026 | return true; |
12027 | |
12028 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly && |
12029 | !isMSStaticDataMemberInlineDefinition(VD)) |
12030 | return false; |
12031 | |
12032 | // Variables in other module units shouldn't be forced to be emitted. |
12033 | if (VD->isInAnotherModuleUnit()) |
12034 | return false; |
12035 | |
12036 | // Variables that can be needed in other TUs are required. |
12037 | auto Linkage = GetGVALinkageForVariable(VD); |
12038 | if (!isDiscardableGVALinkage(L: Linkage)) |
12039 | return true; |
12040 | |
12041 | // We never need to emit a variable that is available in another TU. |
12042 | if (Linkage == GVA_AvailableExternally) |
12043 | return false; |
12044 | |
12045 | // Variables that have destruction with side-effects are required. |
12046 | if (VD->needsDestruction(Ctx: *this)) |
12047 | return true; |
12048 | |
12049 | // Variables that have initialization with side-effects are required. |
12050 | if (VD->getInit() && VD->getInit()->HasSideEffects(Ctx: *this) && |
12051 | // We can get a value-dependent initializer during error recovery. |
12052 | (VD->getInit()->isValueDependent() || !VD->evaluateValue())) |
12053 | return true; |
12054 | |
12055 | // Likewise, variables with tuple-like bindings are required if their |
12056 | // bindings have side-effects. |
12057 | if (const auto *DD = dyn_cast<DecompositionDecl>(Val: VD)) |
12058 | for (const auto *BD : DD->bindings()) |
12059 | if (const auto *BindingVD = BD->getHoldingVar()) |
12060 | if (DeclMustBeEmitted(BindingVD)) |
12061 | return true; |
12062 | |
12063 | return false; |
12064 | } |
12065 | |
12066 | void ASTContext::forEachMultiversionedFunctionVersion( |
12067 | const FunctionDecl *FD, |
12068 | llvm::function_ref<void(FunctionDecl *)> Pred) const { |
12069 | assert(FD->isMultiVersion() && "Only valid for multiversioned functions" ); |
12070 | llvm::SmallDenseSet<const FunctionDecl*, 4> SeenDecls; |
12071 | FD = FD->getMostRecentDecl(); |
12072 | // FIXME: The order of traversal here matters and depends on the order of |
12073 | // lookup results, which happens to be (mostly) oldest-to-newest, but we |
12074 | // shouldn't rely on that. |
12075 | for (auto *CurDecl : |
12076 | FD->getDeclContext()->getRedeclContext()->lookup(FD->getDeclName())) { |
12077 | FunctionDecl *CurFD = CurDecl->getAsFunction()->getMostRecentDecl(); |
12078 | if (CurFD && hasSameType(CurFD->getType(), FD->getType()) && |
12079 | !SeenDecls.contains(CurFD)) { |
12080 | SeenDecls.insert(CurFD); |
12081 | Pred(CurFD); |
12082 | } |
12083 | } |
12084 | } |
12085 | |
12086 | CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic, |
12087 | bool IsCXXMethod, |
12088 | bool IsBuiltin) const { |
12089 | // Pass through to the C++ ABI object |
12090 | if (IsCXXMethod) |
12091 | return ABI->getDefaultMethodCallConv(isVariadic: IsVariadic); |
12092 | |
12093 | // Builtins ignore user-specified default calling convention and remain the |
12094 | // Target's default calling convention. |
12095 | if (!IsBuiltin) { |
12096 | switch (LangOpts.getDefaultCallingConv()) { |
12097 | case LangOptions::DCC_None: |
12098 | break; |
12099 | case LangOptions::DCC_CDecl: |
12100 | return CC_C; |
12101 | case LangOptions::DCC_FastCall: |
12102 | if (getTargetInfo().hasFeature(Feature: "sse2" ) && !IsVariadic) |
12103 | return CC_X86FastCall; |
12104 | break; |
12105 | case LangOptions::DCC_StdCall: |
12106 | if (!IsVariadic) |
12107 | return CC_X86StdCall; |
12108 | break; |
12109 | case LangOptions::DCC_VectorCall: |
12110 | // __vectorcall cannot be applied to variadic functions. |
12111 | if (!IsVariadic) |
12112 | return CC_X86VectorCall; |
12113 | break; |
12114 | case LangOptions::DCC_RegCall: |
12115 | // __regcall cannot be applied to variadic functions. |
12116 | if (!IsVariadic) |
12117 | return CC_X86RegCall; |
12118 | break; |
12119 | case LangOptions::DCC_RtdCall: |
12120 | if (!IsVariadic) |
12121 | return CC_M68kRTD; |
12122 | break; |
12123 | } |
12124 | } |
12125 | return Target->getDefaultCallingConv(); |
12126 | } |
12127 | |
12128 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
12129 | // Pass through to the C++ ABI object |
12130 | return ABI->isNearlyEmpty(RD); |
12131 | } |
12132 | |
12133 | VTableContextBase *ASTContext::getVTableContext() { |
12134 | if (!VTContext.get()) { |
12135 | auto ABI = Target->getCXXABI(); |
12136 | if (ABI.isMicrosoft()) |
12137 | VTContext.reset(p: new MicrosoftVTableContext(*this)); |
12138 | else { |
12139 | auto ComponentLayout = getLangOpts().RelativeCXXABIVTables |
12140 | ? ItaniumVTableContext::Relative |
12141 | : ItaniumVTableContext::Pointer; |
12142 | VTContext.reset(p: new ItaniumVTableContext(*this, ComponentLayout)); |
12143 | } |
12144 | } |
12145 | return VTContext.get(); |
12146 | } |
12147 | |
12148 | MangleContext *ASTContext::createMangleContext(const TargetInfo *T) { |
12149 | if (!T) |
12150 | T = Target; |
12151 | switch (T->getCXXABI().getKind()) { |
12152 | case TargetCXXABI::AppleARM64: |
12153 | case TargetCXXABI::Fuchsia: |
12154 | case TargetCXXABI::GenericAArch64: |
12155 | case TargetCXXABI::GenericItanium: |
12156 | case TargetCXXABI::GenericARM: |
12157 | case TargetCXXABI::GenericMIPS: |
12158 | case TargetCXXABI::iOS: |
12159 | case TargetCXXABI::WebAssembly: |
12160 | case TargetCXXABI::WatchOS: |
12161 | case TargetCXXABI::XL: |
12162 | return ItaniumMangleContext::create(Context&: *this, Diags&: getDiagnostics()); |
12163 | case TargetCXXABI::Microsoft: |
12164 | return MicrosoftMangleContext::create(Context&: *this, Diags&: getDiagnostics()); |
12165 | } |
12166 | llvm_unreachable("Unsupported ABI" ); |
12167 | } |
12168 | |
12169 | MangleContext *ASTContext::createDeviceMangleContext(const TargetInfo &T) { |
12170 | assert(T.getCXXABI().getKind() != TargetCXXABI::Microsoft && |
12171 | "Device mangle context does not support Microsoft mangling." ); |
12172 | switch (T.getCXXABI().getKind()) { |
12173 | case TargetCXXABI::AppleARM64: |
12174 | case TargetCXXABI::Fuchsia: |
12175 | case TargetCXXABI::GenericAArch64: |
12176 | case TargetCXXABI::GenericItanium: |
12177 | case TargetCXXABI::GenericARM: |
12178 | case TargetCXXABI::GenericMIPS: |
12179 | case TargetCXXABI::iOS: |
12180 | case TargetCXXABI::WebAssembly: |
12181 | case TargetCXXABI::WatchOS: |
12182 | case TargetCXXABI::XL: |
12183 | return ItaniumMangleContext::create( |
12184 | Context&: *this, Diags&: getDiagnostics(), |
12185 | Discriminator: [](ASTContext &, const NamedDecl *ND) -> std::optional<unsigned> { |
12186 | if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: ND)) |
12187 | return RD->getDeviceLambdaManglingNumber(); |
12188 | return std::nullopt; |
12189 | }, |
12190 | /*IsAux=*/true); |
12191 | case TargetCXXABI::Microsoft: |
12192 | return MicrosoftMangleContext::create(Context&: *this, Diags&: getDiagnostics(), |
12193 | /*IsAux=*/true); |
12194 | } |
12195 | llvm_unreachable("Unsupported ABI" ); |
12196 | } |
12197 | |
12198 | CXXABI::~CXXABI() = default; |
12199 | |
12200 | size_t ASTContext::getSideTableAllocatedMemory() const { |
12201 | return ASTRecordLayouts.getMemorySize() + |
12202 | llvm::capacity_in_bytes(X: ObjCLayouts) + |
12203 | llvm::capacity_in_bytes(X: KeyFunctions) + |
12204 | llvm::capacity_in_bytes(X: ObjCImpls) + |
12205 | llvm::capacity_in_bytes(X: BlockVarCopyInits) + |
12206 | llvm::capacity_in_bytes(X: DeclAttrs) + |
12207 | llvm::capacity_in_bytes(X: TemplateOrInstantiation) + |
12208 | llvm::capacity_in_bytes(X: InstantiatedFromUsingDecl) + |
12209 | llvm::capacity_in_bytes(X: InstantiatedFromUsingShadowDecl) + |
12210 | llvm::capacity_in_bytes(X: InstantiatedFromUnnamedFieldDecl) + |
12211 | llvm::capacity_in_bytes(X: OverriddenMethods) + |
12212 | llvm::capacity_in_bytes(X: Types) + |
12213 | llvm::capacity_in_bytes(x: VariableArrayTypes); |
12214 | } |
12215 | |
12216 | /// getIntTypeForBitwidth - |
12217 | /// sets integer QualTy according to specified details: |
12218 | /// bitwidth, signed/unsigned. |
12219 | /// Returns empty type if there is no appropriate target types. |
12220 | QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth, |
12221 | unsigned Signed) const { |
12222 | TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(BitWidth: DestWidth, IsSigned: Signed); |
12223 | CanQualType QualTy = getFromTargetType(Type: Ty); |
12224 | if (!QualTy && DestWidth == 128) |
12225 | return Signed ? Int128Ty : UnsignedInt128Ty; |
12226 | return QualTy; |
12227 | } |
12228 | |
12229 | /// getRealTypeForBitwidth - |
12230 | /// sets floating point QualTy according to specified bitwidth. |
12231 | /// Returns empty type if there is no appropriate target types. |
12232 | QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth, |
12233 | FloatModeKind ExplicitType) const { |
12234 | FloatModeKind Ty = |
12235 | getTargetInfo().getRealTypeByWidth(BitWidth: DestWidth, ExplicitType); |
12236 | switch (Ty) { |
12237 | case FloatModeKind::Half: |
12238 | return HalfTy; |
12239 | case FloatModeKind::Float: |
12240 | return FloatTy; |
12241 | case FloatModeKind::Double: |
12242 | return DoubleTy; |
12243 | case FloatModeKind::LongDouble: |
12244 | return LongDoubleTy; |
12245 | case FloatModeKind::Float128: |
12246 | return Float128Ty; |
12247 | case FloatModeKind::Ibm128: |
12248 | return Ibm128Ty; |
12249 | case FloatModeKind::NoFloat: |
12250 | return {}; |
12251 | } |
12252 | |
12253 | llvm_unreachable("Unhandled TargetInfo::RealType value" ); |
12254 | } |
12255 | |
12256 | void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) { |
12257 | if (Number <= 1) |
12258 | return; |
12259 | |
12260 | MangleNumbers[ND] = Number; |
12261 | |
12262 | if (Listener) |
12263 | Listener->AddedManglingNumber(ND, Number); |
12264 | } |
12265 | |
12266 | unsigned ASTContext::getManglingNumber(const NamedDecl *ND, |
12267 | bool ForAuxTarget) const { |
12268 | auto I = MangleNumbers.find(Key: ND); |
12269 | unsigned Res = I != MangleNumbers.end() ? I->second : 1; |
12270 | // CUDA/HIP host compilation encodes host and device mangling numbers |
12271 | // as lower and upper half of 32 bit integer. |
12272 | if (LangOpts.CUDA && !LangOpts.CUDAIsDevice) { |
12273 | Res = ForAuxTarget ? Res >> 16 : Res & 0xFFFF; |
12274 | } else { |
12275 | assert(!ForAuxTarget && "Only CUDA/HIP host compilation supports mangling " |
12276 | "number for aux target" ); |
12277 | } |
12278 | return Res > 1 ? Res : 1; |
12279 | } |
12280 | |
12281 | void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) { |
12282 | if (Number <= 1) |
12283 | return; |
12284 | |
12285 | StaticLocalNumbers[VD] = Number; |
12286 | |
12287 | if (Listener) |
12288 | Listener->AddedStaticLocalNumbers(VD, Number); |
12289 | } |
12290 | |
12291 | unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const { |
12292 | auto I = StaticLocalNumbers.find(Key: VD); |
12293 | return I != StaticLocalNumbers.end() ? I->second : 1; |
12294 | } |
12295 | |
12296 | MangleNumberingContext & |
12297 | ASTContext::getManglingNumberContext(const DeclContext *DC) { |
12298 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
12299 | std::unique_ptr<MangleNumberingContext> &MCtx = MangleNumberingContexts[DC]; |
12300 | if (!MCtx) |
12301 | MCtx = createMangleNumberingContext(); |
12302 | return *MCtx; |
12303 | } |
12304 | |
12305 | MangleNumberingContext & |
12306 | ASTContext::(NeedExtraManglingDecl_t, const Decl *D) { |
12307 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
12308 | std::unique_ptr<MangleNumberingContext> &MCtx = |
12309 | ExtraMangleNumberingContexts[D]; |
12310 | if (!MCtx) |
12311 | MCtx = createMangleNumberingContext(); |
12312 | return *MCtx; |
12313 | } |
12314 | |
12315 | std::unique_ptr<MangleNumberingContext> |
12316 | ASTContext::createMangleNumberingContext() const { |
12317 | return ABI->createMangleNumberingContext(); |
12318 | } |
12319 | |
12320 | const CXXConstructorDecl * |
12321 | ASTContext::getCopyConstructorForExceptionObject(CXXRecordDecl *RD) { |
12322 | return ABI->getCopyConstructorForExceptionObject( |
12323 | cast<CXXRecordDecl>(RD->getFirstDecl())); |
12324 | } |
12325 | |
12326 | void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD, |
12327 | CXXConstructorDecl *CD) { |
12328 | return ABI->addCopyConstructorForExceptionObject( |
12329 | cast<CXXRecordDecl>(RD->getFirstDecl()), |
12330 | cast<CXXConstructorDecl>(CD->getFirstDecl())); |
12331 | } |
12332 | |
12333 | void ASTContext::addTypedefNameForUnnamedTagDecl(TagDecl *TD, |
12334 | TypedefNameDecl *DD) { |
12335 | return ABI->addTypedefNameForUnnamedTagDecl(TD, DD); |
12336 | } |
12337 | |
12338 | TypedefNameDecl * |
12339 | ASTContext::getTypedefNameForUnnamedTagDecl(const TagDecl *TD) { |
12340 | return ABI->getTypedefNameForUnnamedTagDecl(TD); |
12341 | } |
12342 | |
12343 | void ASTContext::addDeclaratorForUnnamedTagDecl(TagDecl *TD, |
12344 | DeclaratorDecl *DD) { |
12345 | return ABI->addDeclaratorForUnnamedTagDecl(TD, DD); |
12346 | } |
12347 | |
12348 | DeclaratorDecl *ASTContext::getDeclaratorForUnnamedTagDecl(const TagDecl *TD) { |
12349 | return ABI->getDeclaratorForUnnamedTagDecl(TD); |
12350 | } |
12351 | |
12352 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
12353 | ParamIndices[D] = index; |
12354 | } |
12355 | |
12356 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
12357 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
12358 | assert(I != ParamIndices.end() && |
12359 | "ParmIndices lacks entry set by ParmVarDecl" ); |
12360 | return I->second; |
12361 | } |
12362 | |
12363 | QualType ASTContext::getStringLiteralArrayType(QualType EltTy, |
12364 | unsigned Length) const { |
12365 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
12366 | if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings) |
12367 | EltTy = EltTy.withConst(); |
12368 | |
12369 | EltTy = adjustStringLiteralBaseType(Ty: EltTy); |
12370 | |
12371 | // Get an array type for the string, according to C99 6.4.5. This includes |
12372 | // the null terminator character. |
12373 | return getConstantArrayType(EltTy, ArySizeIn: llvm::APInt(32, Length + 1), SizeExpr: nullptr, |
12374 | ASM: ArraySizeModifier::Normal, /*IndexTypeQuals*/ 0); |
12375 | } |
12376 | |
12377 | StringLiteral * |
12378 | ASTContext::getPredefinedStringLiteralFromCache(StringRef Key) const { |
12379 | StringLiteral *&Result = StringLiteralCache[Key]; |
12380 | if (!Result) |
12381 | Result = StringLiteral::Create( |
12382 | *this, Key, StringLiteralKind::Ordinary, |
12383 | /*Pascal*/ false, getStringLiteralArrayType(CharTy, Key.size()), |
12384 | SourceLocation()); |
12385 | return Result; |
12386 | } |
12387 | |
12388 | MSGuidDecl * |
12389 | ASTContext::getMSGuidDecl(MSGuidDecl::Parts Parts) const { |
12390 | assert(MSGuidTagDecl && "building MS GUID without MS extensions?" ); |
12391 | |
12392 | llvm::FoldingSetNodeID ID; |
12393 | MSGuidDecl::Profile(ID, P: Parts); |
12394 | |
12395 | void *InsertPos; |
12396 | if (MSGuidDecl *Existing = MSGuidDecls.FindNodeOrInsertPos(ID, InsertPos)) |
12397 | return Existing; |
12398 | |
12399 | QualType GUIDType = getMSGuidType().withConst(); |
12400 | MSGuidDecl *New = MSGuidDecl::Create(C: *this, T: GUIDType, P: Parts); |
12401 | MSGuidDecls.InsertNode(New, InsertPos); |
12402 | return New; |
12403 | } |
12404 | |
12405 | UnnamedGlobalConstantDecl * |
12406 | ASTContext::getUnnamedGlobalConstantDecl(QualType Ty, |
12407 | const APValue &APVal) const { |
12408 | llvm::FoldingSetNodeID ID; |
12409 | UnnamedGlobalConstantDecl::Profile(ID, Ty, APVal); |
12410 | |
12411 | void *InsertPos; |
12412 | if (UnnamedGlobalConstantDecl *Existing = |
12413 | UnnamedGlobalConstantDecls.FindNodeOrInsertPos(ID, InsertPos)) |
12414 | return Existing; |
12415 | |
12416 | UnnamedGlobalConstantDecl *New = |
12417 | UnnamedGlobalConstantDecl::Create(C: *this, T: Ty, APVal); |
12418 | UnnamedGlobalConstantDecls.InsertNode(New, InsertPos); |
12419 | return New; |
12420 | } |
12421 | |
12422 | TemplateParamObjectDecl * |
12423 | ASTContext::getTemplateParamObjectDecl(QualType T, const APValue &V) const { |
12424 | assert(T->isRecordType() && "template param object of unexpected type" ); |
12425 | |
12426 | // C++ [temp.param]p8: |
12427 | // [...] a static storage duration object of type 'const T' [...] |
12428 | T.addConst(); |
12429 | |
12430 | llvm::FoldingSetNodeID ID; |
12431 | TemplateParamObjectDecl::Profile(ID, T, V); |
12432 | |
12433 | void *InsertPos; |
12434 | if (TemplateParamObjectDecl *Existing = |
12435 | TemplateParamObjectDecls.FindNodeOrInsertPos(ID, InsertPos)) |
12436 | return Existing; |
12437 | |
12438 | TemplateParamObjectDecl *New = TemplateParamObjectDecl::Create(C: *this, T, V); |
12439 | TemplateParamObjectDecls.InsertNode(New, InsertPos); |
12440 | return New; |
12441 | } |
12442 | |
12443 | bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { |
12444 | const llvm::Triple &T = getTargetInfo().getTriple(); |
12445 | if (!T.isOSDarwin()) |
12446 | return false; |
12447 | |
12448 | if (!(T.isiOS() && T.isOSVersionLT(Major: 7)) && |
12449 | !(T.isMacOSX() && T.isOSVersionLT(Major: 10, Minor: 9))) |
12450 | return false; |
12451 | |
12452 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
12453 | CharUnits sizeChars = getTypeSizeInChars(T: AtomicTy); |
12454 | uint64_t Size = sizeChars.getQuantity(); |
12455 | CharUnits alignChars = getTypeAlignInChars(T: AtomicTy); |
12456 | unsigned Align = alignChars.getQuantity(); |
12457 | unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth(); |
12458 | return (Size != Align || toBits(CharSize: sizeChars) > MaxInlineWidthInBits); |
12459 | } |
12460 | |
12461 | bool |
12462 | ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl, |
12463 | const ObjCMethodDecl *MethodImpl) { |
12464 | // No point trying to match an unavailable/deprecated mothod. |
12465 | if (MethodDecl->hasAttr<UnavailableAttr>() |
12466 | || MethodDecl->hasAttr<DeprecatedAttr>()) |
12467 | return false; |
12468 | if (MethodDecl->getObjCDeclQualifier() != |
12469 | MethodImpl->getObjCDeclQualifier()) |
12470 | return false; |
12471 | if (!hasSameType(T1: MethodDecl->getReturnType(), T2: MethodImpl->getReturnType())) |
12472 | return false; |
12473 | |
12474 | if (MethodDecl->param_size() != MethodImpl->param_size()) |
12475 | return false; |
12476 | |
12477 | for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(), |
12478 | IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(), |
12479 | EF = MethodDecl->param_end(); |
12480 | IM != EM && IF != EF; ++IM, ++IF) { |
12481 | const ParmVarDecl *DeclVar = (*IF); |
12482 | const ParmVarDecl *ImplVar = (*IM); |
12483 | if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier()) |
12484 | return false; |
12485 | if (!hasSameType(DeclVar->getType(), ImplVar->getType())) |
12486 | return false; |
12487 | } |
12488 | |
12489 | return (MethodDecl->isVariadic() == MethodImpl->isVariadic()); |
12490 | } |
12491 | |
12492 | uint64_t ASTContext::getTargetNullPointerValue(QualType QT) const { |
12493 | LangAS AS; |
12494 | if (QT->getUnqualifiedDesugaredType()->isNullPtrType()) |
12495 | AS = LangAS::Default; |
12496 | else |
12497 | AS = QT->getPointeeType().getAddressSpace(); |
12498 | |
12499 | return getTargetInfo().getNullPointerValue(AddrSpace: AS); |
12500 | } |
12501 | |
12502 | unsigned ASTContext::getTargetAddressSpace(LangAS AS) const { |
12503 | return getTargetInfo().getTargetAddressSpace(AS); |
12504 | } |
12505 | |
12506 | bool ASTContext::hasSameExpr(const Expr *X, const Expr *Y) const { |
12507 | if (X == Y) |
12508 | return true; |
12509 | if (!X || !Y) |
12510 | return false; |
12511 | llvm::FoldingSetNodeID IDX, IDY; |
12512 | X->Profile(IDX, *this, /*Canonical=*/true); |
12513 | Y->Profile(IDY, *this, /*Canonical=*/true); |
12514 | return IDX == IDY; |
12515 | } |
12516 | |
12517 | // The getCommon* helpers return, for given 'same' X and Y entities given as |
12518 | // inputs, another entity which is also the 'same' as the inputs, but which |
12519 | // is closer to the canonical form of the inputs, each according to a given |
12520 | // criteria. |
12521 | // The getCommon*Checked variants are 'null inputs not-allowed' equivalents of |
12522 | // the regular ones. |
12523 | |
12524 | static Decl *getCommonDecl(Decl *X, Decl *Y) { |
12525 | if (!declaresSameEntity(D1: X, D2: Y)) |
12526 | return nullptr; |
12527 | for (const Decl *DX : X->redecls()) { |
12528 | // If we reach Y before reaching the first decl, that means X is older. |
12529 | if (DX == Y) |
12530 | return X; |
12531 | // If we reach the first decl, then Y is older. |
12532 | if (DX->isFirstDecl()) |
12533 | return Y; |
12534 | } |
12535 | llvm_unreachable("Corrupt redecls chain" ); |
12536 | } |
12537 | |
12538 | template <class T, std::enable_if_t<std::is_base_of_v<Decl, T>, bool> = true> |
12539 | static T *getCommonDecl(T *X, T *Y) { |
12540 | return cast_or_null<T>( |
12541 | getCommonDecl(X: const_cast<Decl *>(cast_or_null<Decl>(X)), |
12542 | Y: const_cast<Decl *>(cast_or_null<Decl>(Y)))); |
12543 | } |
12544 | |
12545 | template <class T, std::enable_if_t<std::is_base_of_v<Decl, T>, bool> = true> |
12546 | static T *getCommonDeclChecked(T *X, T *Y) { |
12547 | return cast<T>(getCommonDecl(X: const_cast<Decl *>(cast<Decl>(X)), |
12548 | Y: const_cast<Decl *>(cast<Decl>(Y)))); |
12549 | } |
12550 | |
12551 | static TemplateName getCommonTemplateName(ASTContext &Ctx, TemplateName X, |
12552 | TemplateName Y) { |
12553 | if (X.getAsVoidPointer() == Y.getAsVoidPointer()) |
12554 | return X; |
12555 | // FIXME: There are cases here where we could find a common template name |
12556 | // with more sugar. For example one could be a SubstTemplateTemplate* |
12557 | // replacing the other. |
12558 | TemplateName CX = Ctx.getCanonicalTemplateName(Name: X); |
12559 | if (CX.getAsVoidPointer() != |
12560 | Ctx.getCanonicalTemplateName(Name: Y).getAsVoidPointer()) |
12561 | return TemplateName(); |
12562 | return CX; |
12563 | } |
12564 | |
12565 | static TemplateName |
12566 | getCommonTemplateNameChecked(ASTContext &Ctx, TemplateName X, TemplateName Y) { |
12567 | TemplateName R = getCommonTemplateName(Ctx, X, Y); |
12568 | assert(R.getAsVoidPointer() != nullptr); |
12569 | return R; |
12570 | } |
12571 | |
12572 | static auto getCommonTypes(ASTContext &Ctx, ArrayRef<QualType> Xs, |
12573 | ArrayRef<QualType> Ys, bool Unqualified = false) { |
12574 | assert(Xs.size() == Ys.size()); |
12575 | SmallVector<QualType, 8> Rs(Xs.size()); |
12576 | for (size_t I = 0; I < Rs.size(); ++I) |
12577 | Rs[I] = Ctx.getCommonSugaredType(X: Xs[I], Y: Ys[I], Unqualified); |
12578 | return Rs; |
12579 | } |
12580 | |
12581 | template <class T> |
12582 | static SourceLocation getCommonAttrLoc(const T *X, const T *Y) { |
12583 | return X->getAttributeLoc() == Y->getAttributeLoc() ? X->getAttributeLoc() |
12584 | : SourceLocation(); |
12585 | } |
12586 | |
12587 | static TemplateArgument getCommonTemplateArgument(ASTContext &Ctx, |
12588 | const TemplateArgument &X, |
12589 | const TemplateArgument &Y) { |
12590 | if (X.getKind() != Y.getKind()) |
12591 | return TemplateArgument(); |
12592 | |
12593 | switch (X.getKind()) { |
12594 | case TemplateArgument::ArgKind::Type: |
12595 | if (!Ctx.hasSameType(T1: X.getAsType(), T2: Y.getAsType())) |
12596 | return TemplateArgument(); |
12597 | return TemplateArgument( |
12598 | Ctx.getCommonSugaredType(X: X.getAsType(), Y: Y.getAsType())); |
12599 | case TemplateArgument::ArgKind::NullPtr: |
12600 | if (!Ctx.hasSameType(T1: X.getNullPtrType(), T2: Y.getNullPtrType())) |
12601 | return TemplateArgument(); |
12602 | return TemplateArgument( |
12603 | Ctx.getCommonSugaredType(X: X.getNullPtrType(), Y: Y.getNullPtrType()), |
12604 | /*Unqualified=*/true); |
12605 | case TemplateArgument::ArgKind::Expression: |
12606 | if (!Ctx.hasSameType(T1: X.getAsExpr()->getType(), T2: Y.getAsExpr()->getType())) |
12607 | return TemplateArgument(); |
12608 | // FIXME: Try to keep the common sugar. |
12609 | return X; |
12610 | case TemplateArgument::ArgKind::Template: { |
12611 | TemplateName TX = X.getAsTemplate(), TY = Y.getAsTemplate(); |
12612 | TemplateName CTN = ::getCommonTemplateName(Ctx, X: TX, Y: TY); |
12613 | if (!CTN.getAsVoidPointer()) |
12614 | return TemplateArgument(); |
12615 | return TemplateArgument(CTN); |
12616 | } |
12617 | case TemplateArgument::ArgKind::TemplateExpansion: { |
12618 | TemplateName TX = X.getAsTemplateOrTemplatePattern(), |
12619 | TY = Y.getAsTemplateOrTemplatePattern(); |
12620 | TemplateName CTN = ::getCommonTemplateName(Ctx, X: TX, Y: TY); |
12621 | if (!CTN.getAsVoidPointer()) |
12622 | return TemplateName(); |
12623 | auto NExpX = X.getNumTemplateExpansions(); |
12624 | assert(NExpX == Y.getNumTemplateExpansions()); |
12625 | return TemplateArgument(CTN, NExpX); |
12626 | } |
12627 | default: |
12628 | // FIXME: Handle the other argument kinds. |
12629 | return X; |
12630 | } |
12631 | } |
12632 | |
12633 | static bool getCommonTemplateArguments(ASTContext &Ctx, |
12634 | SmallVectorImpl<TemplateArgument> &R, |
12635 | ArrayRef<TemplateArgument> Xs, |
12636 | ArrayRef<TemplateArgument> Ys) { |
12637 | if (Xs.size() != Ys.size()) |
12638 | return true; |
12639 | R.resize(N: Xs.size()); |
12640 | for (size_t I = 0; I < R.size(); ++I) { |
12641 | R[I] = getCommonTemplateArgument(Ctx, X: Xs[I], Y: Ys[I]); |
12642 | if (R[I].isNull()) |
12643 | return true; |
12644 | } |
12645 | return false; |
12646 | } |
12647 | |
12648 | static auto getCommonTemplateArguments(ASTContext &Ctx, |
12649 | ArrayRef<TemplateArgument> Xs, |
12650 | ArrayRef<TemplateArgument> Ys) { |
12651 | SmallVector<TemplateArgument, 8> R; |
12652 | bool Different = getCommonTemplateArguments(Ctx, R, Xs, Ys); |
12653 | assert(!Different); |
12654 | (void)Different; |
12655 | return R; |
12656 | } |
12657 | |
12658 | template <class T> |
12659 | static ElaboratedTypeKeyword getCommonTypeKeyword(const T *X, const T *Y) { |
12660 | return X->getKeyword() == Y->getKeyword() ? X->getKeyword() |
12661 | : ElaboratedTypeKeyword::None; |
12662 | } |
12663 | |
12664 | template <class T> |
12665 | static NestedNameSpecifier *getCommonNNS(ASTContext &Ctx, const T *X, |
12666 | const T *Y) { |
12667 | // FIXME: Try to keep the common NNS sugar. |
12668 | return X->getQualifier() == Y->getQualifier() |
12669 | ? X->getQualifier() |
12670 | : Ctx.getCanonicalNestedNameSpecifier(NNS: X->getQualifier()); |
12671 | } |
12672 | |
12673 | template <class T> |
12674 | static QualType getCommonElementType(ASTContext &Ctx, const T *X, const T *Y) { |
12675 | return Ctx.getCommonSugaredType(X: X->getElementType(), Y: Y->getElementType()); |
12676 | } |
12677 | |
12678 | template <class T> |
12679 | static QualType getCommonArrayElementType(ASTContext &Ctx, const T *X, |
12680 | Qualifiers &QX, const T *Y, |
12681 | Qualifiers &QY) { |
12682 | QualType EX = X->getElementType(), EY = Y->getElementType(); |
12683 | QualType R = Ctx.getCommonSugaredType(X: EX, Y: EY, |
12684 | /*Unqualified=*/true); |
12685 | Qualifiers RQ = R.getQualifiers(); |
12686 | QX += EX.getQualifiers() - RQ; |
12687 | QY += EY.getQualifiers() - RQ; |
12688 | return R; |
12689 | } |
12690 | |
12691 | template <class T> |
12692 | static QualType getCommonPointeeType(ASTContext &Ctx, const T *X, const T *Y) { |
12693 | return Ctx.getCommonSugaredType(X: X->getPointeeType(), Y: Y->getPointeeType()); |
12694 | } |
12695 | |
12696 | template <class T> static auto *getCommonSizeExpr(ASTContext &Ctx, T *X, T *Y) { |
12697 | assert(Ctx.hasSameExpr(X->getSizeExpr(), Y->getSizeExpr())); |
12698 | return X->getSizeExpr(); |
12699 | } |
12700 | |
12701 | static auto getCommonSizeModifier(const ArrayType *X, const ArrayType *Y) { |
12702 | assert(X->getSizeModifier() == Y->getSizeModifier()); |
12703 | return X->getSizeModifier(); |
12704 | } |
12705 | |
12706 | static auto getCommonIndexTypeCVRQualifiers(const ArrayType *X, |
12707 | const ArrayType *Y) { |
12708 | assert(X->getIndexTypeCVRQualifiers() == Y->getIndexTypeCVRQualifiers()); |
12709 | return X->getIndexTypeCVRQualifiers(); |
12710 | } |
12711 | |
12712 | // Merges two type lists such that the resulting vector will contain |
12713 | // each type (in a canonical sense) only once, in the order they appear |
12714 | // from X to Y. If they occur in both X and Y, the result will contain |
12715 | // the common sugared type between them. |
12716 | static void mergeTypeLists(ASTContext &Ctx, SmallVectorImpl<QualType> &Out, |
12717 | ArrayRef<QualType> X, ArrayRef<QualType> Y) { |
12718 | llvm::DenseMap<QualType, unsigned> Found; |
12719 | for (auto Ts : {X, Y}) { |
12720 | for (QualType T : Ts) { |
12721 | auto Res = Found.try_emplace(Ctx.getCanonicalType(T), Out.size()); |
12722 | if (!Res.second) { |
12723 | QualType &U = Out[Res.first->second]; |
12724 | U = Ctx.getCommonSugaredType(X: U, Y: T); |
12725 | } else { |
12726 | Out.emplace_back(Args&: T); |
12727 | } |
12728 | } |
12729 | } |
12730 | } |
12731 | |
12732 | FunctionProtoType::ExceptionSpecInfo |
12733 | ASTContext::mergeExceptionSpecs(FunctionProtoType::ExceptionSpecInfo ESI1, |
12734 | FunctionProtoType::ExceptionSpecInfo ESI2, |
12735 | SmallVectorImpl<QualType> &ExceptionTypeStorage, |
12736 | bool AcceptDependent) { |
12737 | ExceptionSpecificationType EST1 = ESI1.Type, EST2 = ESI2.Type; |
12738 | |
12739 | // If either of them can throw anything, that is the result. |
12740 | for (auto I : {EST_None, EST_MSAny, EST_NoexceptFalse}) { |
12741 | if (EST1 == I) |
12742 | return ESI1; |
12743 | if (EST2 == I) |
12744 | return ESI2; |
12745 | } |
12746 | |
12747 | // If either of them is non-throwing, the result is the other. |
12748 | for (auto I : |
12749 | {EST_NoThrow, EST_DynamicNone, EST_BasicNoexcept, EST_NoexceptTrue}) { |
12750 | if (EST1 == I) |
12751 | return ESI2; |
12752 | if (EST2 == I) |
12753 | return ESI1; |
12754 | } |
12755 | |
12756 | // If we're left with value-dependent computed noexcept expressions, we're |
12757 | // stuck. Before C++17, we can just drop the exception specification entirely, |
12758 | // since it's not actually part of the canonical type. And this should never |
12759 | // happen in C++17, because it would mean we were computing the composite |
12760 | // pointer type of dependent types, which should never happen. |
12761 | if (EST1 == EST_DependentNoexcept || EST2 == EST_DependentNoexcept) { |
12762 | assert(AcceptDependent && |
12763 | "computing composite pointer type of dependent types" ); |
12764 | return FunctionProtoType::ExceptionSpecInfo(); |
12765 | } |
12766 | |
12767 | // Switch over the possibilities so that people adding new values know to |
12768 | // update this function. |
12769 | switch (EST1) { |
12770 | case EST_None: |
12771 | case EST_DynamicNone: |
12772 | case EST_MSAny: |
12773 | case EST_BasicNoexcept: |
12774 | case EST_DependentNoexcept: |
12775 | case EST_NoexceptFalse: |
12776 | case EST_NoexceptTrue: |
12777 | case EST_NoThrow: |
12778 | llvm_unreachable("These ESTs should be handled above" ); |
12779 | |
12780 | case EST_Dynamic: { |
12781 | // This is the fun case: both exception specifications are dynamic. Form |
12782 | // the union of the two lists. |
12783 | assert(EST2 == EST_Dynamic && "other cases should already be handled" ); |
12784 | mergeTypeLists(*this, ExceptionTypeStorage, ESI1.Exceptions, |
12785 | ESI2.Exceptions); |
12786 | FunctionProtoType::ExceptionSpecInfo Result(EST_Dynamic); |
12787 | Result.Exceptions = ExceptionTypeStorage; |
12788 | return Result; |
12789 | } |
12790 | |
12791 | case EST_Unevaluated: |
12792 | case EST_Uninstantiated: |
12793 | case EST_Unparsed: |
12794 | llvm_unreachable("shouldn't see unresolved exception specifications here" ); |
12795 | } |
12796 | |
12797 | llvm_unreachable("invalid ExceptionSpecificationType" ); |
12798 | } |
12799 | |
12800 | static QualType getCommonNonSugarTypeNode(ASTContext &Ctx, const Type *X, |
12801 | Qualifiers &QX, const Type *Y, |
12802 | Qualifiers &QY) { |
12803 | Type::TypeClass TC = X->getTypeClass(); |
12804 | assert(TC == Y->getTypeClass()); |
12805 | switch (TC) { |
12806 | #define UNEXPECTED_TYPE(Class, Kind) \ |
12807 | case Type::Class: \ |
12808 | llvm_unreachable("Unexpected " Kind ": " #Class); |
12809 | |
12810 | #define NON_CANONICAL_TYPE(Class, Base) UNEXPECTED_TYPE(Class, "non-canonical") |
12811 | #define TYPE(Class, Base) |
12812 | #include "clang/AST/TypeNodes.inc" |
12813 | |
12814 | #define SUGAR_FREE_TYPE(Class) UNEXPECTED_TYPE(Class, "sugar-free") |
12815 | SUGAR_FREE_TYPE(Builtin) |
12816 | SUGAR_FREE_TYPE(DeducedTemplateSpecialization) |
12817 | SUGAR_FREE_TYPE(DependentBitInt) |
12818 | SUGAR_FREE_TYPE(Enum) |
12819 | SUGAR_FREE_TYPE(BitInt) |
12820 | SUGAR_FREE_TYPE(ObjCInterface) |
12821 | SUGAR_FREE_TYPE(Record) |
12822 | SUGAR_FREE_TYPE(SubstTemplateTypeParmPack) |
12823 | SUGAR_FREE_TYPE(UnresolvedUsing) |
12824 | #undef SUGAR_FREE_TYPE |
12825 | #define NON_UNIQUE_TYPE(Class) UNEXPECTED_TYPE(Class, "non-unique") |
12826 | NON_UNIQUE_TYPE(TypeOfExpr) |
12827 | NON_UNIQUE_TYPE(VariableArray) |
12828 | #undef NON_UNIQUE_TYPE |
12829 | |
12830 | UNEXPECTED_TYPE(TypeOf, "sugar" ) |
12831 | |
12832 | #undef UNEXPECTED_TYPE |
12833 | |
12834 | case Type::Auto: { |
12835 | const auto *AX = cast<AutoType>(X), *AY = cast<AutoType>(Y); |
12836 | assert(AX->getDeducedType().isNull()); |
12837 | assert(AY->getDeducedType().isNull()); |
12838 | assert(AX->getKeyword() == AY->getKeyword()); |
12839 | assert(AX->isInstantiationDependentType() == |
12840 | AY->isInstantiationDependentType()); |
12841 | auto As = getCommonTemplateArguments(Ctx, AX->getTypeConstraintArguments(), |
12842 | AY->getTypeConstraintArguments()); |
12843 | return Ctx.getAutoType(DeducedType: QualType(), Keyword: AX->getKeyword(), |
12844 | IsDependent: AX->isInstantiationDependentType(), |
12845 | IsPack: AX->containsUnexpandedParameterPack(), |
12846 | TypeConstraintConcept: getCommonDeclChecked(AX->getTypeConstraintConcept(), |
12847 | AY->getTypeConstraintConcept()), |
12848 | TypeConstraintArgs: As); |
12849 | } |
12850 | case Type::IncompleteArray: { |
12851 | const auto *AX = cast<IncompleteArrayType>(X), |
12852 | *AY = cast<IncompleteArrayType>(Y); |
12853 | return Ctx.getIncompleteArrayType( |
12854 | elementType: getCommonArrayElementType(Ctx, AX, QX, AY, QY), |
12855 | ASM: getCommonSizeModifier(AX, AY), elementTypeQuals: getCommonIndexTypeCVRQualifiers(AX, AY)); |
12856 | } |
12857 | case Type::DependentSizedArray: { |
12858 | const auto *AX = cast<DependentSizedArrayType>(X), |
12859 | *AY = cast<DependentSizedArrayType>(Y); |
12860 | return Ctx.getDependentSizedArrayType( |
12861 | elementType: getCommonArrayElementType(Ctx, AX, QX, AY, QY), |
12862 | numElements: getCommonSizeExpr(Ctx, AX, AY), ASM: getCommonSizeModifier(AX, AY), |
12863 | elementTypeQuals: getCommonIndexTypeCVRQualifiers(AX, AY), |
12864 | brackets: AX->getBracketsRange() == AY->getBracketsRange() |
12865 | ? AX->getBracketsRange() |
12866 | : SourceRange()); |
12867 | } |
12868 | case Type::ConstantArray: { |
12869 | const auto *AX = cast<ConstantArrayType>(X), |
12870 | *AY = cast<ConstantArrayType>(Y); |
12871 | assert(AX->getSize() == AY->getSize()); |
12872 | const Expr *SizeExpr = Ctx.hasSameExpr(X: AX->getSizeExpr(), Y: AY->getSizeExpr()) |
12873 | ? AX->getSizeExpr() |
12874 | : nullptr; |
12875 | return Ctx.getConstantArrayType( |
12876 | EltTy: getCommonArrayElementType(Ctx, AX, QX, AY, QY), ArySizeIn: AX->getSize(), SizeExpr, |
12877 | ASM: getCommonSizeModifier(AX, AY), IndexTypeQuals: getCommonIndexTypeCVRQualifiers(AX, AY)); |
12878 | } |
12879 | case Type::ArrayParameter: { |
12880 | const auto *AX = cast<ArrayParameterType>(X), |
12881 | *AY = cast<ArrayParameterType>(Y); |
12882 | assert(AX->getSize() == AY->getSize()); |
12883 | const Expr *SizeExpr = Ctx.hasSameExpr(X: AX->getSizeExpr(), Y: AY->getSizeExpr()) |
12884 | ? AX->getSizeExpr() |
12885 | : nullptr; |
12886 | auto ArrayTy = Ctx.getConstantArrayType( |
12887 | EltTy: getCommonArrayElementType(Ctx, AX, QX, AY, QY), ArySizeIn: AX->getSize(), SizeExpr, |
12888 | ASM: getCommonSizeModifier(AX, AY), IndexTypeQuals: getCommonIndexTypeCVRQualifiers(AX, AY)); |
12889 | return Ctx.getArrayParameterType(Ty: ArrayTy); |
12890 | } |
12891 | case Type::Atomic: { |
12892 | const auto *AX = cast<AtomicType>(X), *AY = cast<AtomicType>(Y); |
12893 | return Ctx.getAtomicType( |
12894 | T: Ctx.getCommonSugaredType(X: AX->getValueType(), Y: AY->getValueType())); |
12895 | } |
12896 | case Type::Complex: { |
12897 | const auto *CX = cast<ComplexType>(X), *CY = cast<ComplexType>(Y); |
12898 | return Ctx.getComplexType(getCommonArrayElementType(Ctx, CX, QX, CY, QY)); |
12899 | } |
12900 | case Type::Pointer: { |
12901 | const auto *PX = cast<PointerType>(X), *PY = cast<PointerType>(Y); |
12902 | return Ctx.getPointerType(getCommonPointeeType(Ctx, PX, PY)); |
12903 | } |
12904 | case Type::BlockPointer: { |
12905 | const auto *PX = cast<BlockPointerType>(X), *PY = cast<BlockPointerType>(Y); |
12906 | return Ctx.getBlockPointerType(T: getCommonPointeeType(Ctx, PX, PY)); |
12907 | } |
12908 | case Type::ObjCObjectPointer: { |
12909 | const auto *PX = cast<ObjCObjectPointerType>(X), |
12910 | *PY = cast<ObjCObjectPointerType>(Y); |
12911 | return Ctx.getObjCObjectPointerType(ObjectT: getCommonPointeeType(Ctx, PX, PY)); |
12912 | } |
12913 | case Type::MemberPointer: { |
12914 | const auto *PX = cast<MemberPointerType>(X), |
12915 | *PY = cast<MemberPointerType>(Y); |
12916 | return Ctx.getMemberPointerType( |
12917 | T: getCommonPointeeType(Ctx, PX, PY), |
12918 | Cls: Ctx.getCommonSugaredType(X: QualType(PX->getClass(), 0), |
12919 | Y: QualType(PY->getClass(), 0)) |
12920 | .getTypePtr()); |
12921 | } |
12922 | case Type::LValueReference: { |
12923 | const auto *PX = cast<LValueReferenceType>(X), |
12924 | *PY = cast<LValueReferenceType>(Y); |
12925 | // FIXME: Preserve PointeeTypeAsWritten. |
12926 | return Ctx.getLValueReferenceType(T: getCommonPointeeType(Ctx, PX, PY), |
12927 | SpelledAsLValue: PX->isSpelledAsLValue() || |
12928 | PY->isSpelledAsLValue()); |
12929 | } |
12930 | case Type::RValueReference: { |
12931 | const auto *PX = cast<RValueReferenceType>(X), |
12932 | *PY = cast<RValueReferenceType>(Y); |
12933 | // FIXME: Preserve PointeeTypeAsWritten. |
12934 | return Ctx.getRValueReferenceType(T: getCommonPointeeType(Ctx, PX, PY)); |
12935 | } |
12936 | case Type::DependentAddressSpace: { |
12937 | const auto *PX = cast<DependentAddressSpaceType>(X), |
12938 | *PY = cast<DependentAddressSpaceType>(Y); |
12939 | assert(Ctx.hasSameExpr(PX->getAddrSpaceExpr(), PY->getAddrSpaceExpr())); |
12940 | return Ctx.getDependentAddressSpaceType(PointeeType: getCommonPointeeType(Ctx, PX, PY), |
12941 | AddrSpaceExpr: PX->getAddrSpaceExpr(), |
12942 | AttrLoc: getCommonAttrLoc(PX, PY)); |
12943 | } |
12944 | case Type::FunctionNoProto: { |
12945 | const auto *FX = cast<FunctionNoProtoType>(X), |
12946 | *FY = cast<FunctionNoProtoType>(Y); |
12947 | assert(FX->getExtInfo() == FY->getExtInfo()); |
12948 | return Ctx.getFunctionNoProtoType( |
12949 | Ctx.getCommonSugaredType(X: FX->getReturnType(), Y: FY->getReturnType()), |
12950 | FX->getExtInfo()); |
12951 | } |
12952 | case Type::FunctionProto: { |
12953 | const auto *FX = cast<FunctionProtoType>(X), |
12954 | *FY = cast<FunctionProtoType>(Y); |
12955 | FunctionProtoType::ExtProtoInfo EPIX = FX->getExtProtoInfo(), |
12956 | EPIY = FY->getExtProtoInfo(); |
12957 | assert(EPIX.ExtInfo == EPIY.ExtInfo); |
12958 | assert(EPIX.ExtParameterInfos == EPIY.ExtParameterInfos); |
12959 | assert(EPIX.RefQualifier == EPIY.RefQualifier); |
12960 | assert(EPIX.TypeQuals == EPIY.TypeQuals); |
12961 | assert(EPIX.Variadic == EPIY.Variadic); |
12962 | |
12963 | // FIXME: Can we handle an empty EllipsisLoc? |
12964 | // Use emtpy EllipsisLoc if X and Y differ. |
12965 | |
12966 | EPIX.HasTrailingReturn = EPIX.HasTrailingReturn && EPIY.HasTrailingReturn; |
12967 | |
12968 | QualType R = |
12969 | Ctx.getCommonSugaredType(X: FX->getReturnType(), Y: FY->getReturnType()); |
12970 | auto P = getCommonTypes(Ctx, FX->param_types(), FY->param_types(), |
12971 | /*Unqualified=*/true); |
12972 | |
12973 | SmallVector<QualType, 8> Exceptions; |
12974 | EPIX.ExceptionSpec = Ctx.mergeExceptionSpecs( |
12975 | ESI1: EPIX.ExceptionSpec, ESI2: EPIY.ExceptionSpec, ExceptionTypeStorage&: Exceptions, AcceptDependent: true); |
12976 | return Ctx.getFunctionType(ResultTy: R, Args: P, EPI: EPIX); |
12977 | } |
12978 | case Type::ObjCObject: { |
12979 | const auto *OX = cast<ObjCObjectType>(X), *OY = cast<ObjCObjectType>(Y); |
12980 | assert( |
12981 | std::equal(OX->getProtocols().begin(), OX->getProtocols().end(), |
12982 | OY->getProtocols().begin(), OY->getProtocols().end(), |
12983 | [](const ObjCProtocolDecl *P0, const ObjCProtocolDecl *P1) { |
12984 | return P0->getCanonicalDecl() == P1->getCanonicalDecl(); |
12985 | }) && |
12986 | "protocol lists must be the same" ); |
12987 | auto TAs = getCommonTypes(Ctx, OX->getTypeArgsAsWritten(), |
12988 | OY->getTypeArgsAsWritten()); |
12989 | return Ctx.getObjCObjectType( |
12990 | Ctx.getCommonSugaredType(X: OX->getBaseType(), Y: OY->getBaseType()), TAs, |
12991 | OX->getProtocols(), |
12992 | OX->isKindOfTypeAsWritten() && OY->isKindOfTypeAsWritten()); |
12993 | } |
12994 | case Type::ConstantMatrix: { |
12995 | const auto *MX = cast<ConstantMatrixType>(X), |
12996 | *MY = cast<ConstantMatrixType>(Y); |
12997 | assert(MX->getNumRows() == MY->getNumRows()); |
12998 | assert(MX->getNumColumns() == MY->getNumColumns()); |
12999 | return Ctx.getConstantMatrixType(ElementTy: getCommonElementType(Ctx, MX, MY), |
13000 | NumRows: MX->getNumRows(), NumColumns: MX->getNumColumns()); |
13001 | } |
13002 | case Type::DependentSizedMatrix: { |
13003 | const auto *MX = cast<DependentSizedMatrixType>(X), |
13004 | *MY = cast<DependentSizedMatrixType>(Y); |
13005 | assert(Ctx.hasSameExpr(MX->getRowExpr(), MY->getRowExpr())); |
13006 | assert(Ctx.hasSameExpr(MX->getColumnExpr(), MY->getColumnExpr())); |
13007 | return Ctx.getDependentSizedMatrixType( |
13008 | ElementTy: getCommonElementType(Ctx, MX, MY), RowExpr: MX->getRowExpr(), |
13009 | ColumnExpr: MX->getColumnExpr(), AttrLoc: getCommonAttrLoc(MX, MY)); |
13010 | } |
13011 | case Type::Vector: { |
13012 | const auto *VX = cast<VectorType>(X), *VY = cast<VectorType>(Y); |
13013 | assert(VX->getNumElements() == VY->getNumElements()); |
13014 | assert(VX->getVectorKind() == VY->getVectorKind()); |
13015 | return Ctx.getVectorType(vecType: getCommonElementType(Ctx, VX, VY), |
13016 | NumElts: VX->getNumElements(), VecKind: VX->getVectorKind()); |
13017 | } |
13018 | case Type::ExtVector: { |
13019 | const auto *VX = cast<ExtVectorType>(X), *VY = cast<ExtVectorType>(Y); |
13020 | assert(VX->getNumElements() == VY->getNumElements()); |
13021 | return Ctx.getExtVectorType(vecType: getCommonElementType(Ctx, VX, VY), |
13022 | NumElts: VX->getNumElements()); |
13023 | } |
13024 | case Type::DependentSizedExtVector: { |
13025 | const auto *VX = cast<DependentSizedExtVectorType>(X), |
13026 | *VY = cast<DependentSizedExtVectorType>(Y); |
13027 | return Ctx.getDependentSizedExtVectorType(vecType: getCommonElementType(Ctx, VX, VY), |
13028 | SizeExpr: getCommonSizeExpr(Ctx, VX, VY), |
13029 | AttrLoc: getCommonAttrLoc(VX, VY)); |
13030 | } |
13031 | case Type::DependentVector: { |
13032 | const auto *VX = cast<DependentVectorType>(X), |
13033 | *VY = cast<DependentVectorType>(Y); |
13034 | assert(VX->getVectorKind() == VY->getVectorKind()); |
13035 | return Ctx.getDependentVectorType( |
13036 | VecType: getCommonElementType(Ctx, VX, VY), SizeExpr: getCommonSizeExpr(Ctx, VX, VY), |
13037 | AttrLoc: getCommonAttrLoc(VX, VY), VecKind: VX->getVectorKind()); |
13038 | } |
13039 | case Type::InjectedClassName: { |
13040 | const auto *IX = cast<InjectedClassNameType>(X), |
13041 | *IY = cast<InjectedClassNameType>(Y); |
13042 | return Ctx.getInjectedClassNameType( |
13043 | Decl: getCommonDeclChecked(IX->getDecl(), IY->getDecl()), |
13044 | TST: Ctx.getCommonSugaredType(X: IX->getInjectedSpecializationType(), |
13045 | Y: IY->getInjectedSpecializationType())); |
13046 | } |
13047 | case Type::TemplateSpecialization: { |
13048 | const auto *TX = cast<TemplateSpecializationType>(X), |
13049 | *TY = cast<TemplateSpecializationType>(Y); |
13050 | auto As = getCommonTemplateArguments(Ctx, TX->template_arguments(), |
13051 | TY->template_arguments()); |
13052 | return Ctx.getTemplateSpecializationType( |
13053 | ::getCommonTemplateNameChecked(Ctx, X: TX->getTemplateName(), |
13054 | Y: TY->getTemplateName()), |
13055 | As, X->getCanonicalTypeInternal()); |
13056 | } |
13057 | case Type::Decltype: { |
13058 | const auto *DX = cast<DecltypeType>(X); |
13059 | [[maybe_unused]] const auto *DY = cast<DecltypeType>(Y); |
13060 | assert(DX->isDependentType()); |
13061 | assert(DY->isDependentType()); |
13062 | assert(Ctx.hasSameExpr(DX->getUnderlyingExpr(), DY->getUnderlyingExpr())); |
13063 | // As Decltype is not uniqued, building a common type would be wasteful. |
13064 | return QualType(DX, 0); |
13065 | } |
13066 | case Type::PackIndexing: { |
13067 | const auto *DX = cast<PackIndexingType>(X); |
13068 | [[maybe_unused]] const auto *DY = cast<PackIndexingType>(Y); |
13069 | assert(DX->isDependentType()); |
13070 | assert(DY->isDependentType()); |
13071 | assert(Ctx.hasSameExpr(DX->getIndexExpr(), DY->getIndexExpr())); |
13072 | return QualType(DX, 0); |
13073 | } |
13074 | case Type::DependentName: { |
13075 | const auto *NX = cast<DependentNameType>(X), |
13076 | *NY = cast<DependentNameType>(Y); |
13077 | assert(NX->getIdentifier() == NY->getIdentifier()); |
13078 | return Ctx.getDependentNameType( |
13079 | Keyword: getCommonTypeKeyword(NX, NY), NNS: getCommonNNS(Ctx, NX, NY), |
13080 | Name: NX->getIdentifier(), Canon: NX->getCanonicalTypeInternal()); |
13081 | } |
13082 | case Type::DependentTemplateSpecialization: { |
13083 | const auto *TX = cast<DependentTemplateSpecializationType>(X), |
13084 | *TY = cast<DependentTemplateSpecializationType>(Y); |
13085 | assert(TX->getIdentifier() == TY->getIdentifier()); |
13086 | auto As = getCommonTemplateArguments(Ctx, TX->template_arguments(), |
13087 | TY->template_arguments()); |
13088 | return Ctx.getDependentTemplateSpecializationType( |
13089 | getCommonTypeKeyword(TX, TY), getCommonNNS(Ctx, TX, TY), |
13090 | TX->getIdentifier(), As); |
13091 | } |
13092 | case Type::UnaryTransform: { |
13093 | const auto *TX = cast<UnaryTransformType>(X), |
13094 | *TY = cast<UnaryTransformType>(Y); |
13095 | assert(TX->getUTTKind() == TY->getUTTKind()); |
13096 | return Ctx.getUnaryTransformType( |
13097 | BaseType: Ctx.getCommonSugaredType(X: TX->getBaseType(), Y: TY->getBaseType()), |
13098 | UnderlyingType: Ctx.getCommonSugaredType(X: TX->getUnderlyingType(), |
13099 | Y: TY->getUnderlyingType()), |
13100 | Kind: TX->getUTTKind()); |
13101 | } |
13102 | case Type::PackExpansion: { |
13103 | const auto *PX = cast<PackExpansionType>(X), |
13104 | *PY = cast<PackExpansionType>(Y); |
13105 | assert(PX->getNumExpansions() == PY->getNumExpansions()); |
13106 | return Ctx.getPackExpansionType( |
13107 | Pattern: Ctx.getCommonSugaredType(X: PX->getPattern(), Y: PY->getPattern()), |
13108 | NumExpansions: PX->getNumExpansions(), ExpectPackInType: false); |
13109 | } |
13110 | case Type::Pipe: { |
13111 | const auto *PX = cast<PipeType>(X), *PY = cast<PipeType>(Y); |
13112 | assert(PX->isReadOnly() == PY->isReadOnly()); |
13113 | auto MP = PX->isReadOnly() ? &ASTContext::getReadPipeType |
13114 | : &ASTContext::getWritePipeType; |
13115 | return (Ctx.*MP)(getCommonElementType(Ctx, PX, PY)); |
13116 | } |
13117 | case Type::TemplateTypeParm: { |
13118 | const auto *TX = cast<TemplateTypeParmType>(X), |
13119 | *TY = cast<TemplateTypeParmType>(Y); |
13120 | assert(TX->getDepth() == TY->getDepth()); |
13121 | assert(TX->getIndex() == TY->getIndex()); |
13122 | assert(TX->isParameterPack() == TY->isParameterPack()); |
13123 | return Ctx.getTemplateTypeParmType( |
13124 | Depth: TX->getDepth(), Index: TX->getIndex(), ParameterPack: TX->isParameterPack(), |
13125 | TTPDecl: getCommonDecl(TX->getDecl(), TY->getDecl())); |
13126 | } |
13127 | } |
13128 | llvm_unreachable("Unknown Type Class" ); |
13129 | } |
13130 | |
13131 | static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X, |
13132 | const Type *Y, |
13133 | SplitQualType Underlying) { |
13134 | Type::TypeClass TC = X->getTypeClass(); |
13135 | if (TC != Y->getTypeClass()) |
13136 | return QualType(); |
13137 | switch (TC) { |
13138 | #define UNEXPECTED_TYPE(Class, Kind) \ |
13139 | case Type::Class: \ |
13140 | llvm_unreachable("Unexpected " Kind ": " #Class); |
13141 | #define TYPE(Class, Base) |
13142 | #define DEPENDENT_TYPE(Class, Base) UNEXPECTED_TYPE(Class, "dependent") |
13143 | #include "clang/AST/TypeNodes.inc" |
13144 | |
13145 | #define CANONICAL_TYPE(Class) UNEXPECTED_TYPE(Class, "canonical") |
13146 | CANONICAL_TYPE(Atomic) |
13147 | CANONICAL_TYPE(BitInt) |
13148 | CANONICAL_TYPE(BlockPointer) |
13149 | CANONICAL_TYPE(Builtin) |
13150 | CANONICAL_TYPE(Complex) |
13151 | CANONICAL_TYPE(ConstantArray) |
13152 | CANONICAL_TYPE(ArrayParameter) |
13153 | CANONICAL_TYPE(ConstantMatrix) |
13154 | CANONICAL_TYPE(Enum) |
13155 | CANONICAL_TYPE(ExtVector) |
13156 | CANONICAL_TYPE(FunctionNoProto) |
13157 | CANONICAL_TYPE(FunctionProto) |
13158 | CANONICAL_TYPE(IncompleteArray) |
13159 | CANONICAL_TYPE(LValueReference) |
13160 | CANONICAL_TYPE(MemberPointer) |
13161 | CANONICAL_TYPE(ObjCInterface) |
13162 | CANONICAL_TYPE(ObjCObject) |
13163 | CANONICAL_TYPE(ObjCObjectPointer) |
13164 | CANONICAL_TYPE(Pipe) |
13165 | CANONICAL_TYPE(Pointer) |
13166 | CANONICAL_TYPE(Record) |
13167 | CANONICAL_TYPE(RValueReference) |
13168 | CANONICAL_TYPE(VariableArray) |
13169 | CANONICAL_TYPE(Vector) |
13170 | #undef CANONICAL_TYPE |
13171 | |
13172 | #undef UNEXPECTED_TYPE |
13173 | |
13174 | case Type::Adjusted: { |
13175 | const auto *AX = cast<AdjustedType>(X), *AY = cast<AdjustedType>(Y); |
13176 | QualType OX = AX->getOriginalType(), OY = AY->getOriginalType(); |
13177 | if (!Ctx.hasSameType(T1: OX, T2: OY)) |
13178 | return QualType(); |
13179 | // FIXME: It's inefficient to have to unify the original types. |
13180 | return Ctx.getAdjustedType(Orig: Ctx.getCommonSugaredType(X: OX, Y: OY), |
13181 | New: Ctx.getQualifiedType(split: Underlying)); |
13182 | } |
13183 | case Type::Decayed: { |
13184 | const auto *DX = cast<DecayedType>(X), *DY = cast<DecayedType>(Y); |
13185 | QualType OX = DX->getOriginalType(), OY = DY->getOriginalType(); |
13186 | if (!Ctx.hasSameType(T1: OX, T2: OY)) |
13187 | return QualType(); |
13188 | // FIXME: It's inefficient to have to unify the original types. |
13189 | return Ctx.getDecayedType(Orig: Ctx.getCommonSugaredType(X: OX, Y: OY), |
13190 | Decayed: Ctx.getQualifiedType(split: Underlying)); |
13191 | } |
13192 | case Type::Attributed: { |
13193 | const auto *AX = cast<AttributedType>(X), *AY = cast<AttributedType>(Y); |
13194 | AttributedType::Kind Kind = AX->getAttrKind(); |
13195 | if (Kind != AY->getAttrKind()) |
13196 | return QualType(); |
13197 | QualType MX = AX->getModifiedType(), MY = AY->getModifiedType(); |
13198 | if (!Ctx.hasSameType(T1: MX, T2: MY)) |
13199 | return QualType(); |
13200 | // FIXME: It's inefficient to have to unify the modified types. |
13201 | return Ctx.getAttributedType(attrKind: Kind, modifiedType: Ctx.getCommonSugaredType(X: MX, Y: MY), |
13202 | equivalentType: Ctx.getQualifiedType(split: Underlying)); |
13203 | } |
13204 | case Type::BTFTagAttributed: { |
13205 | const auto *BX = cast<BTFTagAttributedType>(X); |
13206 | const BTFTypeTagAttr *AX = BX->getAttr(); |
13207 | // The attribute is not uniqued, so just compare the tag. |
13208 | if (AX->getBTFTypeTag() != |
13209 | cast<BTFTagAttributedType>(Y)->getAttr()->getBTFTypeTag()) |
13210 | return QualType(); |
13211 | return Ctx.getBTFTagAttributedType(BTFAttr: AX, Wrapped: Ctx.getQualifiedType(split: Underlying)); |
13212 | } |
13213 | case Type::Auto: { |
13214 | const auto *AX = cast<AutoType>(X), *AY = cast<AutoType>(Y); |
13215 | |
13216 | AutoTypeKeyword KW = AX->getKeyword(); |
13217 | if (KW != AY->getKeyword()) |
13218 | return QualType(); |
13219 | |
13220 | ConceptDecl *CD = ::getCommonDecl(AX->getTypeConstraintConcept(), |
13221 | AY->getTypeConstraintConcept()); |
13222 | SmallVector<TemplateArgument, 8> As; |
13223 | if (CD && |
13224 | getCommonTemplateArguments(Ctx, As, AX->getTypeConstraintArguments(), |
13225 | AY->getTypeConstraintArguments())) { |
13226 | CD = nullptr; // The arguments differ, so make it unconstrained. |
13227 | As.clear(); |
13228 | } |
13229 | |
13230 | // Both auto types can't be dependent, otherwise they wouldn't have been |
13231 | // sugar. This implies they can't contain unexpanded packs either. |
13232 | return Ctx.getAutoType(DeducedType: Ctx.getQualifiedType(split: Underlying), Keyword: AX->getKeyword(), |
13233 | /*IsDependent=*/false, /*IsPack=*/false, TypeConstraintConcept: CD, TypeConstraintArgs: As); |
13234 | } |
13235 | case Type::PackIndexing: |
13236 | case Type::Decltype: |
13237 | return QualType(); |
13238 | case Type::DeducedTemplateSpecialization: |
13239 | // FIXME: Try to merge these. |
13240 | return QualType(); |
13241 | |
13242 | case Type::Elaborated: { |
13243 | const auto *EX = cast<ElaboratedType>(X), *EY = cast<ElaboratedType>(Y); |
13244 | return Ctx.getElaboratedType( |
13245 | Keyword: ::getCommonTypeKeyword(EX, EY), NNS: ::getCommonNNS(Ctx, EX, EY), |
13246 | NamedType: Ctx.getQualifiedType(split: Underlying), |
13247 | OwnedTagDecl: ::getCommonDecl(EX->getOwnedTagDecl(), EY->getOwnedTagDecl())); |
13248 | } |
13249 | case Type::MacroQualified: { |
13250 | const auto *MX = cast<MacroQualifiedType>(X), |
13251 | *MY = cast<MacroQualifiedType>(Y); |
13252 | const IdentifierInfo *IX = MX->getMacroIdentifier(); |
13253 | if (IX != MY->getMacroIdentifier()) |
13254 | return QualType(); |
13255 | return Ctx.getMacroQualifiedType(UnderlyingTy: Ctx.getQualifiedType(split: Underlying), MacroII: IX); |
13256 | } |
13257 | case Type::SubstTemplateTypeParm: { |
13258 | const auto *SX = cast<SubstTemplateTypeParmType>(X), |
13259 | *SY = cast<SubstTemplateTypeParmType>(Y); |
13260 | Decl *CD = |
13261 | ::getCommonDecl(SX->getAssociatedDecl(), SY->getAssociatedDecl()); |
13262 | if (!CD) |
13263 | return QualType(); |
13264 | unsigned Index = SX->getIndex(); |
13265 | if (Index != SY->getIndex()) |
13266 | return QualType(); |
13267 | auto PackIndex = SX->getPackIndex(); |
13268 | if (PackIndex != SY->getPackIndex()) |
13269 | return QualType(); |
13270 | return Ctx.getSubstTemplateTypeParmType(Replacement: Ctx.getQualifiedType(split: Underlying), |
13271 | AssociatedDecl: CD, Index, PackIndex: PackIndex); |
13272 | } |
13273 | case Type::ObjCTypeParam: |
13274 | // FIXME: Try to merge these. |
13275 | return QualType(); |
13276 | case Type::Paren: |
13277 | return Ctx.getParenType(InnerType: Ctx.getQualifiedType(split: Underlying)); |
13278 | |
13279 | case Type::TemplateSpecialization: { |
13280 | const auto *TX = cast<TemplateSpecializationType>(X), |
13281 | *TY = cast<TemplateSpecializationType>(Y); |
13282 | TemplateName CTN = ::getCommonTemplateName(Ctx, X: TX->getTemplateName(), |
13283 | Y: TY->getTemplateName()); |
13284 | if (!CTN.getAsVoidPointer()) |
13285 | return QualType(); |
13286 | SmallVector<TemplateArgument, 8> Args; |
13287 | if (getCommonTemplateArguments(Ctx, Args, TX->template_arguments(), |
13288 | TY->template_arguments())) |
13289 | return QualType(); |
13290 | return Ctx.getTemplateSpecializationType(CTN, Args, |
13291 | Ctx.getQualifiedType(split: Underlying)); |
13292 | } |
13293 | case Type::Typedef: { |
13294 | const auto *TX = cast<TypedefType>(X), *TY = cast<TypedefType>(Y); |
13295 | const TypedefNameDecl *CD = ::getCommonDecl(TX->getDecl(), TY->getDecl()); |
13296 | if (!CD) |
13297 | return QualType(); |
13298 | return Ctx.getTypedefType(Decl: CD, Underlying: Ctx.getQualifiedType(split: Underlying)); |
13299 | } |
13300 | case Type::TypeOf: { |
13301 | // The common sugar between two typeof expressions, where one is |
13302 | // potentially a typeof_unqual and the other is not, we unify to the |
13303 | // qualified type as that retains the most information along with the type. |
13304 | // We only return a typeof_unqual type when both types are unqual types. |
13305 | TypeOfKind Kind = TypeOfKind::Qualified; |
13306 | if (cast<TypeOfType>(X)->getKind() == cast<TypeOfType>(Y)->getKind() && |
13307 | cast<TypeOfType>(X)->getKind() == TypeOfKind::Unqualified) |
13308 | Kind = TypeOfKind::Unqualified; |
13309 | return Ctx.getTypeOfType(tofType: Ctx.getQualifiedType(split: Underlying), Kind); |
13310 | } |
13311 | case Type::TypeOfExpr: |
13312 | return QualType(); |
13313 | |
13314 | case Type::UnaryTransform: { |
13315 | const auto *UX = cast<UnaryTransformType>(X), |
13316 | *UY = cast<UnaryTransformType>(Y); |
13317 | UnaryTransformType::UTTKind KX = UX->getUTTKind(); |
13318 | if (KX != UY->getUTTKind()) |
13319 | return QualType(); |
13320 | QualType BX = UX->getBaseType(), BY = UY->getBaseType(); |
13321 | if (!Ctx.hasSameType(T1: BX, T2: BY)) |
13322 | return QualType(); |
13323 | // FIXME: It's inefficient to have to unify the base types. |
13324 | return Ctx.getUnaryTransformType(BaseType: Ctx.getCommonSugaredType(X: BX, Y: BY), |
13325 | UnderlyingType: Ctx.getQualifiedType(split: Underlying), Kind: KX); |
13326 | } |
13327 | case Type::Using: { |
13328 | const auto *UX = cast<UsingType>(X), *UY = cast<UsingType>(Y); |
13329 | const UsingShadowDecl *CD = |
13330 | ::getCommonDecl(UX->getFoundDecl(), UY->getFoundDecl()); |
13331 | if (!CD) |
13332 | return QualType(); |
13333 | return Ctx.getUsingType(Found: CD, Underlying: Ctx.getQualifiedType(split: Underlying)); |
13334 | } |
13335 | case Type::CountAttributed: { |
13336 | const auto *DX = cast<CountAttributedType>(X), |
13337 | *DY = cast<CountAttributedType>(Y); |
13338 | if (DX->isCountInBytes() != DY->isCountInBytes()) |
13339 | return QualType(); |
13340 | if (DX->isOrNull() != DY->isOrNull()) |
13341 | return QualType(); |
13342 | Expr *CEX = DX->getCountExpr(); |
13343 | Expr *CEY = DY->getCountExpr(); |
13344 | llvm::ArrayRef<clang::TypeCoupledDeclRefInfo> CDX = DX->getCoupledDecls(); |
13345 | if (Ctx.hasSameExpr(X: CEX, Y: CEY)) |
13346 | return Ctx.getCountAttributedType(WrappedTy: Ctx.getQualifiedType(split: Underlying), CountExpr: CEX, |
13347 | CountInBytes: DX->isCountInBytes(), OrNull: DX->isOrNull(), |
13348 | DependentDecls: CDX); |
13349 | if (!CEX->isIntegerConstantExpr(Ctx) || !CEY->isIntegerConstantExpr(Ctx)) |
13350 | return QualType(); |
13351 | // Two declarations with the same integer constant may still differ in their |
13352 | // expression pointers, so we need to evaluate them. |
13353 | llvm::APSInt VX = *CEX->getIntegerConstantExpr(Ctx); |
13354 | llvm::APSInt VY = *CEY->getIntegerConstantExpr(Ctx); |
13355 | if (VX != VY) |
13356 | return QualType(); |
13357 | return Ctx.getCountAttributedType(WrappedTy: Ctx.getQualifiedType(split: Underlying), CountExpr: CEX, |
13358 | CountInBytes: DX->isCountInBytes(), OrNull: DX->isOrNull(), |
13359 | DependentDecls: CDX); |
13360 | } |
13361 | } |
13362 | llvm_unreachable("Unhandled Type Class" ); |
13363 | } |
13364 | |
13365 | static auto unwrapSugar(SplitQualType &T, Qualifiers &QTotal) { |
13366 | SmallVector<SplitQualType, 8> R; |
13367 | while (true) { |
13368 | QTotal.addConsistentQualifiers(qs: T.Quals); |
13369 | QualType NT = T.Ty->getLocallyUnqualifiedSingleStepDesugaredType(); |
13370 | if (NT == QualType(T.Ty, 0)) |
13371 | break; |
13372 | R.push_back(Elt: T); |
13373 | T = NT.split(); |
13374 | } |
13375 | return R; |
13376 | } |
13377 | |
13378 | QualType ASTContext::getCommonSugaredType(QualType X, QualType Y, |
13379 | bool Unqualified) { |
13380 | assert(Unqualified ? hasSameUnqualifiedType(X, Y) : hasSameType(X, Y)); |
13381 | if (X == Y) |
13382 | return X; |
13383 | if (!Unqualified) { |
13384 | if (X.isCanonical()) |
13385 | return X; |
13386 | if (Y.isCanonical()) |
13387 | return Y; |
13388 | } |
13389 | |
13390 | SplitQualType SX = X.split(), SY = Y.split(); |
13391 | Qualifiers QX, QY; |
13392 | // Desugar SX and SY, setting the sugar and qualifiers aside into Xs and Ys, |
13393 | // until we reach their underlying "canonical nodes". Note these are not |
13394 | // necessarily canonical types, as they may still have sugared properties. |
13395 | // QX and QY will store the sum of all qualifiers in Xs and Ys respectively. |
13396 | auto Xs = ::unwrapSugar(T&: SX, QTotal&: QX), Ys = ::unwrapSugar(T&: SY, QTotal&: QY); |
13397 | if (SX.Ty != SY.Ty) { |
13398 | // The canonical nodes differ. Build a common canonical node out of the two, |
13399 | // unifying their sugar. This may recurse back here. |
13400 | SX.Ty = |
13401 | ::getCommonNonSugarTypeNode(Ctx&: *this, X: SX.Ty, QX, Y: SY.Ty, QY).getTypePtr(); |
13402 | } else { |
13403 | // The canonical nodes were identical: We may have desugared too much. |
13404 | // Add any common sugar back in. |
13405 | while (!Xs.empty() && !Ys.empty() && Xs.back().Ty == Ys.back().Ty) { |
13406 | QX -= SX.Quals; |
13407 | QY -= SY.Quals; |
13408 | SX = Xs.pop_back_val(); |
13409 | SY = Ys.pop_back_val(); |
13410 | } |
13411 | } |
13412 | if (Unqualified) |
13413 | QX = Qualifiers::removeCommonQualifiers(L&: QX, R&: QY); |
13414 | else |
13415 | assert(QX == QY); |
13416 | |
13417 | // Even though the remaining sugar nodes in Xs and Ys differ, some may be |
13418 | // related. Walk up these nodes, unifying them and adding the result. |
13419 | while (!Xs.empty() && !Ys.empty()) { |
13420 | auto Underlying = SplitQualType( |
13421 | SX.Ty, Qualifiers::removeCommonQualifiers(L&: SX.Quals, R&: SY.Quals)); |
13422 | SX = Xs.pop_back_val(); |
13423 | SY = Ys.pop_back_val(); |
13424 | SX.Ty = ::getCommonSugarTypeNode(Ctx&: *this, X: SX.Ty, Y: SY.Ty, Underlying) |
13425 | .getTypePtrOrNull(); |
13426 | // Stop at the first pair which is unrelated. |
13427 | if (!SX.Ty) { |
13428 | SX.Ty = Underlying.Ty; |
13429 | break; |
13430 | } |
13431 | QX -= Underlying.Quals; |
13432 | }; |
13433 | |
13434 | // Add back the missing accumulated qualifiers, which were stripped off |
13435 | // with the sugar nodes we could not unify. |
13436 | QualType R = getQualifiedType(T: SX.Ty, Qs: QX); |
13437 | assert(Unqualified ? hasSameUnqualifiedType(R, X) : hasSameType(R, X)); |
13438 | return R; |
13439 | } |
13440 | |
13441 | QualType ASTContext::getCorrespondingUnsaturatedType(QualType Ty) const { |
13442 | assert(Ty->isFixedPointType()); |
13443 | |
13444 | if (Ty->isUnsaturatedFixedPointType()) |
13445 | return Ty; |
13446 | |
13447 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
13448 | default: |
13449 | llvm_unreachable("Not a saturated fixed point type!" ); |
13450 | case BuiltinType::SatShortAccum: |
13451 | return ShortAccumTy; |
13452 | case BuiltinType::SatAccum: |
13453 | return AccumTy; |
13454 | case BuiltinType::SatLongAccum: |
13455 | return LongAccumTy; |
13456 | case BuiltinType::SatUShortAccum: |
13457 | return UnsignedShortAccumTy; |
13458 | case BuiltinType::SatUAccum: |
13459 | return UnsignedAccumTy; |
13460 | case BuiltinType::SatULongAccum: |
13461 | return UnsignedLongAccumTy; |
13462 | case BuiltinType::SatShortFract: |
13463 | return ShortFractTy; |
13464 | case BuiltinType::SatFract: |
13465 | return FractTy; |
13466 | case BuiltinType::SatLongFract: |
13467 | return LongFractTy; |
13468 | case BuiltinType::SatUShortFract: |
13469 | return UnsignedShortFractTy; |
13470 | case BuiltinType::SatUFract: |
13471 | return UnsignedFractTy; |
13472 | case BuiltinType::SatULongFract: |
13473 | return UnsignedLongFractTy; |
13474 | } |
13475 | } |
13476 | |
13477 | QualType ASTContext::getCorrespondingSaturatedType(QualType Ty) const { |
13478 | assert(Ty->isFixedPointType()); |
13479 | |
13480 | if (Ty->isSaturatedFixedPointType()) return Ty; |
13481 | |
13482 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
13483 | default: |
13484 | llvm_unreachable("Not a fixed point type!" ); |
13485 | case BuiltinType::ShortAccum: |
13486 | return SatShortAccumTy; |
13487 | case BuiltinType::Accum: |
13488 | return SatAccumTy; |
13489 | case BuiltinType::LongAccum: |
13490 | return SatLongAccumTy; |
13491 | case BuiltinType::UShortAccum: |
13492 | return SatUnsignedShortAccumTy; |
13493 | case BuiltinType::UAccum: |
13494 | return SatUnsignedAccumTy; |
13495 | case BuiltinType::ULongAccum: |
13496 | return SatUnsignedLongAccumTy; |
13497 | case BuiltinType::ShortFract: |
13498 | return SatShortFractTy; |
13499 | case BuiltinType::Fract: |
13500 | return SatFractTy; |
13501 | case BuiltinType::LongFract: |
13502 | return SatLongFractTy; |
13503 | case BuiltinType::UShortFract: |
13504 | return SatUnsignedShortFractTy; |
13505 | case BuiltinType::UFract: |
13506 | return SatUnsignedFractTy; |
13507 | case BuiltinType::ULongFract: |
13508 | return SatUnsignedLongFractTy; |
13509 | } |
13510 | } |
13511 | |
13512 | LangAS ASTContext::getLangASForBuiltinAddressSpace(unsigned AS) const { |
13513 | if (LangOpts.OpenCL) |
13514 | return getTargetInfo().getOpenCLBuiltinAddressSpace(AS); |
13515 | |
13516 | if (LangOpts.CUDA) |
13517 | return getTargetInfo().getCUDABuiltinAddressSpace(AS); |
13518 | |
13519 | return getLangASFromTargetAS(TargetAS: AS); |
13520 | } |
13521 | |
13522 | // Explicitly instantiate this in case a Redeclarable<T> is used from a TU that |
13523 | // doesn't include ASTContext.h |
13524 | template |
13525 | clang::LazyGenerationalUpdatePtr< |
13526 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType |
13527 | clang::LazyGenerationalUpdatePtr< |
13528 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue( |
13529 | const clang::ASTContext &Ctx, Decl *Value); |
13530 | |
13531 | unsigned char ASTContext::getFixedPointScale(QualType Ty) const { |
13532 | assert(Ty->isFixedPointType()); |
13533 | |
13534 | const TargetInfo &Target = getTargetInfo(); |
13535 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
13536 | default: |
13537 | llvm_unreachable("Not a fixed point type!" ); |
13538 | case BuiltinType::ShortAccum: |
13539 | case BuiltinType::SatShortAccum: |
13540 | return Target.getShortAccumScale(); |
13541 | case BuiltinType::Accum: |
13542 | case BuiltinType::SatAccum: |
13543 | return Target.getAccumScale(); |
13544 | case BuiltinType::LongAccum: |
13545 | case BuiltinType::SatLongAccum: |
13546 | return Target.getLongAccumScale(); |
13547 | case BuiltinType::UShortAccum: |
13548 | case BuiltinType::SatUShortAccum: |
13549 | return Target.getUnsignedShortAccumScale(); |
13550 | case BuiltinType::UAccum: |
13551 | case BuiltinType::SatUAccum: |
13552 | return Target.getUnsignedAccumScale(); |
13553 | case BuiltinType::ULongAccum: |
13554 | case BuiltinType::SatULongAccum: |
13555 | return Target.getUnsignedLongAccumScale(); |
13556 | case BuiltinType::ShortFract: |
13557 | case BuiltinType::SatShortFract: |
13558 | return Target.getShortFractScale(); |
13559 | case BuiltinType::Fract: |
13560 | case BuiltinType::SatFract: |
13561 | return Target.getFractScale(); |
13562 | case BuiltinType::LongFract: |
13563 | case BuiltinType::SatLongFract: |
13564 | return Target.getLongFractScale(); |
13565 | case BuiltinType::UShortFract: |
13566 | case BuiltinType::SatUShortFract: |
13567 | return Target.getUnsignedShortFractScale(); |
13568 | case BuiltinType::UFract: |
13569 | case BuiltinType::SatUFract: |
13570 | return Target.getUnsignedFractScale(); |
13571 | case BuiltinType::ULongFract: |
13572 | case BuiltinType::SatULongFract: |
13573 | return Target.getUnsignedLongFractScale(); |
13574 | } |
13575 | } |
13576 | |
13577 | unsigned char ASTContext::getFixedPointIBits(QualType Ty) const { |
13578 | assert(Ty->isFixedPointType()); |
13579 | |
13580 | const TargetInfo &Target = getTargetInfo(); |
13581 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
13582 | default: |
13583 | llvm_unreachable("Not a fixed point type!" ); |
13584 | case BuiltinType::ShortAccum: |
13585 | case BuiltinType::SatShortAccum: |
13586 | return Target.getShortAccumIBits(); |
13587 | case BuiltinType::Accum: |
13588 | case BuiltinType::SatAccum: |
13589 | return Target.getAccumIBits(); |
13590 | case BuiltinType::LongAccum: |
13591 | case BuiltinType::SatLongAccum: |
13592 | return Target.getLongAccumIBits(); |
13593 | case BuiltinType::UShortAccum: |
13594 | case BuiltinType::SatUShortAccum: |
13595 | return Target.getUnsignedShortAccumIBits(); |
13596 | case BuiltinType::UAccum: |
13597 | case BuiltinType::SatUAccum: |
13598 | return Target.getUnsignedAccumIBits(); |
13599 | case BuiltinType::ULongAccum: |
13600 | case BuiltinType::SatULongAccum: |
13601 | return Target.getUnsignedLongAccumIBits(); |
13602 | case BuiltinType::ShortFract: |
13603 | case BuiltinType::SatShortFract: |
13604 | case BuiltinType::Fract: |
13605 | case BuiltinType::SatFract: |
13606 | case BuiltinType::LongFract: |
13607 | case BuiltinType::SatLongFract: |
13608 | case BuiltinType::UShortFract: |
13609 | case BuiltinType::SatUShortFract: |
13610 | case BuiltinType::UFract: |
13611 | case BuiltinType::SatUFract: |
13612 | case BuiltinType::ULongFract: |
13613 | case BuiltinType::SatULongFract: |
13614 | return 0; |
13615 | } |
13616 | } |
13617 | |
13618 | llvm::FixedPointSemantics |
13619 | ASTContext::getFixedPointSemantics(QualType Ty) const { |
13620 | assert((Ty->isFixedPointType() || Ty->isIntegerType()) && |
13621 | "Can only get the fixed point semantics for a " |
13622 | "fixed point or integer type." ); |
13623 | if (Ty->isIntegerType()) |
13624 | return llvm::FixedPointSemantics::GetIntegerSemantics( |
13625 | Width: getIntWidth(T: Ty), IsSigned: Ty->isSignedIntegerType()); |
13626 | |
13627 | bool isSigned = Ty->isSignedFixedPointType(); |
13628 | return llvm::FixedPointSemantics( |
13629 | static_cast<unsigned>(getTypeSize(T: Ty)), getFixedPointScale(Ty), isSigned, |
13630 | Ty->isSaturatedFixedPointType(), |
13631 | !isSigned && getTargetInfo().doUnsignedFixedPointTypesHavePadding()); |
13632 | } |
13633 | |
13634 | llvm::APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const { |
13635 | assert(Ty->isFixedPointType()); |
13636 | return llvm::APFixedPoint::getMax(Sema: getFixedPointSemantics(Ty)); |
13637 | } |
13638 | |
13639 | llvm::APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const { |
13640 | assert(Ty->isFixedPointType()); |
13641 | return llvm::APFixedPoint::getMin(Sema: getFixedPointSemantics(Ty)); |
13642 | } |
13643 | |
13644 | QualType ASTContext::getCorrespondingSignedFixedPointType(QualType Ty) const { |
13645 | assert(Ty->isUnsignedFixedPointType() && |
13646 | "Expected unsigned fixed point type" ); |
13647 | |
13648 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
13649 | case BuiltinType::UShortAccum: |
13650 | return ShortAccumTy; |
13651 | case BuiltinType::UAccum: |
13652 | return AccumTy; |
13653 | case BuiltinType::ULongAccum: |
13654 | return LongAccumTy; |
13655 | case BuiltinType::SatUShortAccum: |
13656 | return SatShortAccumTy; |
13657 | case BuiltinType::SatUAccum: |
13658 | return SatAccumTy; |
13659 | case BuiltinType::SatULongAccum: |
13660 | return SatLongAccumTy; |
13661 | case BuiltinType::UShortFract: |
13662 | return ShortFractTy; |
13663 | case BuiltinType::UFract: |
13664 | return FractTy; |
13665 | case BuiltinType::ULongFract: |
13666 | return LongFractTy; |
13667 | case BuiltinType::SatUShortFract: |
13668 | return SatShortFractTy; |
13669 | case BuiltinType::SatUFract: |
13670 | return SatFractTy; |
13671 | case BuiltinType::SatULongFract: |
13672 | return SatLongFractTy; |
13673 | default: |
13674 | llvm_unreachable("Unexpected unsigned fixed point type" ); |
13675 | } |
13676 | } |
13677 | |
13678 | std::vector<std::string> ASTContext::filterFunctionTargetVersionAttrs( |
13679 | const TargetVersionAttr *TV) const { |
13680 | assert(TV != nullptr); |
13681 | llvm::SmallVector<StringRef, 8> Feats; |
13682 | std::vector<std::string> ResFeats; |
13683 | TV->getFeatures(Feats); |
13684 | for (auto &Feature : Feats) |
13685 | if (Target->validateCpuSupports(Name: Feature.str())) |
13686 | // Use '?' to mark features that came from TargetVersion. |
13687 | ResFeats.push_back(x: "?" + Feature.str()); |
13688 | return ResFeats; |
13689 | } |
13690 | |
13691 | ParsedTargetAttr |
13692 | ASTContext::filterFunctionTargetAttrs(const TargetAttr *TD) const { |
13693 | assert(TD != nullptr); |
13694 | ParsedTargetAttr ParsedAttr = Target->parseTargetAttr(Str: TD->getFeaturesStr()); |
13695 | |
13696 | llvm::erase_if(C&: ParsedAttr.Features, P: [&](const std::string &Feat) { |
13697 | return !Target->isValidFeatureName(Feature: StringRef{Feat}.substr(Start: 1)); |
13698 | }); |
13699 | return ParsedAttr; |
13700 | } |
13701 | |
13702 | void ASTContext::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, |
13703 | const FunctionDecl *FD) const { |
13704 | if (FD) |
13705 | getFunctionFeatureMap(FeatureMap, GD: GlobalDecl().getWithDecl(FD)); |
13706 | else |
13707 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), |
13708 | CPU: Target->getTargetOpts().CPU, |
13709 | FeatureVec: Target->getTargetOpts().Features); |
13710 | } |
13711 | |
13712 | // Fills in the supplied string map with the set of target features for the |
13713 | // passed in function. |
13714 | void ASTContext::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, |
13715 | GlobalDecl GD) const { |
13716 | StringRef TargetCPU = Target->getTargetOpts().CPU; |
13717 | const FunctionDecl *FD = GD.getDecl()->getAsFunction(); |
13718 | if (const auto *TD = FD->getAttr<TargetAttr>()) { |
13719 | ParsedTargetAttr ParsedAttr = filterFunctionTargetAttrs(TD: TD); |
13720 | |
13721 | // Make a copy of the features as passed on the command line into the |
13722 | // beginning of the additional features from the function to override. |
13723 | ParsedAttr.Features.insert( |
13724 | position: ParsedAttr.Features.begin(), |
13725 | first: Target->getTargetOpts().FeaturesAsWritten.begin(), |
13726 | last: Target->getTargetOpts().FeaturesAsWritten.end()); |
13727 | |
13728 | if (ParsedAttr.CPU != "" && Target->isValidCPUName(Name: ParsedAttr.CPU)) |
13729 | TargetCPU = ParsedAttr.CPU; |
13730 | |
13731 | // Now populate the feature map, first with the TargetCPU which is either |
13732 | // the default or a new one from the target attribute string. Then we'll use |
13733 | // the passed in features (FeaturesAsWritten) along with the new ones from |
13734 | // the attribute. |
13735 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), CPU: TargetCPU, |
13736 | FeatureVec: ParsedAttr.Features); |
13737 | } else if (const auto *SD = FD->getAttr<CPUSpecificAttr>()) { |
13738 | llvm::SmallVector<StringRef, 32> FeaturesTmp; |
13739 | Target->getCPUSpecificCPUDispatchFeatures( |
13740 | Name: SD->getCPUName(GD.getMultiVersionIndex())->getName(), Features&: FeaturesTmp); |
13741 | std::vector<std::string> Features(FeaturesTmp.begin(), FeaturesTmp.end()); |
13742 | Features.insert(position: Features.begin(), |
13743 | first: Target->getTargetOpts().FeaturesAsWritten.begin(), |
13744 | last: Target->getTargetOpts().FeaturesAsWritten.end()); |
13745 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), CPU: TargetCPU, FeatureVec: Features); |
13746 | } else if (const auto *TC = FD->getAttr<TargetClonesAttr>()) { |
13747 | std::vector<std::string> Features; |
13748 | if (Target->getTriple().isAArch64()) { |
13749 | // TargetClones for AArch64 |
13750 | llvm::SmallVector<StringRef, 8> Feats; |
13751 | TC->getFeatures(Feats, GD.getMultiVersionIndex()); |
13752 | for (StringRef Feat : Feats) |
13753 | if (Target->validateCpuSupports(Name: Feat.str())) |
13754 | // Use '?' to mark features that came from AArch64 TargetClones. |
13755 | Features.push_back(x: "?" + Feat.str()); |
13756 | Features.insert(position: Features.begin(), |
13757 | first: Target->getTargetOpts().FeaturesAsWritten.begin(), |
13758 | last: Target->getTargetOpts().FeaturesAsWritten.end()); |
13759 | } else { |
13760 | StringRef VersionStr = TC->getFeatureStr(GD.getMultiVersionIndex()); |
13761 | if (VersionStr.starts_with(Prefix: "arch=" )) |
13762 | TargetCPU = VersionStr.drop_front(N: sizeof("arch=" ) - 1); |
13763 | else if (VersionStr != "default" ) |
13764 | Features.push_back(x: (StringRef{"+" } + VersionStr).str()); |
13765 | } |
13766 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), CPU: TargetCPU, FeatureVec: Features); |
13767 | } else if (const auto *TV = FD->getAttr<TargetVersionAttr>()) { |
13768 | std::vector<std::string> Feats = filterFunctionTargetVersionAttrs(TV); |
13769 | Feats.insert(position: Feats.begin(), |
13770 | first: Target->getTargetOpts().FeaturesAsWritten.begin(), |
13771 | last: Target->getTargetOpts().FeaturesAsWritten.end()); |
13772 | Target->initFeatureMap(Features&: FeatureMap, Diags&: getDiagnostics(), CPU: TargetCPU, FeatureVec: Feats); |
13773 | } else { |
13774 | FeatureMap = Target->getTargetOpts().FeatureMap; |
13775 | } |
13776 | } |
13777 | |
13778 | OMPTraitInfo &ASTContext::getNewOMPTraitInfo() { |
13779 | OMPTraitInfoVector.emplace_back(Args: new OMPTraitInfo()); |
13780 | return *OMPTraitInfoVector.back(); |
13781 | } |
13782 | |
13783 | const StreamingDiagnostic &clang:: |
13784 | operator<<(const StreamingDiagnostic &DB, |
13785 | const ASTContext::SectionInfo &Section) { |
13786 | if (Section.Decl) |
13787 | return DB << Section.Decl; |
13788 | return DB << "a prior #pragma section" ; |
13789 | } |
13790 | |
13791 | bool ASTContext::mayExternalize(const Decl *D) const { |
13792 | bool IsInternalVar = |
13793 | isa<VarDecl>(Val: D) && |
13794 | basicGVALinkageForVariable(Context: *this, VD: cast<VarDecl>(Val: D)) == GVA_Internal; |
13795 | bool IsExplicitDeviceVar = (D->hasAttr<CUDADeviceAttr>() && |
13796 | !D->getAttr<CUDADeviceAttr>()->isImplicit()) || |
13797 | (D->hasAttr<CUDAConstantAttr>() && |
13798 | !D->getAttr<CUDAConstantAttr>()->isImplicit()); |
13799 | // CUDA/HIP: managed variables need to be externalized since it is |
13800 | // a declaration in IR, therefore cannot have internal linkage. Kernels in |
13801 | // anonymous name space needs to be externalized to avoid duplicate symbols. |
13802 | return (IsInternalVar && |
13803 | (D->hasAttr<HIPManagedAttr>() || IsExplicitDeviceVar)) || |
13804 | (D->hasAttr<CUDAGlobalAttr>() && |
13805 | basicGVALinkageForFunction(*this, cast<FunctionDecl>(D)) == |
13806 | GVA_Internal); |
13807 | } |
13808 | |
13809 | bool ASTContext::shouldExternalize(const Decl *D) const { |
13810 | return mayExternalize(D) && |
13811 | (D->hasAttr<HIPManagedAttr>() || D->hasAttr<CUDAGlobalAttr>() || |
13812 | CUDADeviceVarODRUsedByHost.count(cast<VarDecl>(D))); |
13813 | } |
13814 | |
13815 | StringRef ASTContext::getCUIDHash() const { |
13816 | if (!CUIDHash.empty()) |
13817 | return CUIDHash; |
13818 | if (LangOpts.CUID.empty()) |
13819 | return StringRef(); |
13820 | CUIDHash = llvm::utohexstr(X: llvm::MD5Hash(Str: LangOpts.CUID), /*LowerCase=*/true); |
13821 | return CUIDHash; |
13822 | } |
13823 | |