File: | build/source/clang/lib/AST/ASTContext.cpp |
Warning: | line 3141, column 12 1st function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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/TemplateBase.h" | |||
45 | #include "clang/AST/TemplateName.h" | |||
46 | #include "clang/AST/Type.h" | |||
47 | #include "clang/AST/TypeLoc.h" | |||
48 | #include "clang/AST/UnresolvedSet.h" | |||
49 | #include "clang/AST/VTableBuilder.h" | |||
50 | #include "clang/Basic/AddressSpaces.h" | |||
51 | #include "clang/Basic/Builtins.h" | |||
52 | #include "clang/Basic/CommentOptions.h" | |||
53 | #include "clang/Basic/ExceptionSpecificationType.h" | |||
54 | #include "clang/Basic/IdentifierTable.h" | |||
55 | #include "clang/Basic/LLVM.h" | |||
56 | #include "clang/Basic/LangOptions.h" | |||
57 | #include "clang/Basic/Linkage.h" | |||
58 | #include "clang/Basic/Module.h" | |||
59 | #include "clang/Basic/NoSanitizeList.h" | |||
60 | #include "clang/Basic/ObjCRuntime.h" | |||
61 | #include "clang/Basic/SourceLocation.h" | |||
62 | #include "clang/Basic/SourceManager.h" | |||
63 | #include "clang/Basic/Specifiers.h" | |||
64 | #include "clang/Basic/TargetCXXABI.h" | |||
65 | #include "clang/Basic/TargetInfo.h" | |||
66 | #include "clang/Basic/XRayLists.h" | |||
67 | #include "llvm/ADT/APFixedPoint.h" | |||
68 | #include "llvm/ADT/APInt.h" | |||
69 | #include "llvm/ADT/APSInt.h" | |||
70 | #include "llvm/ADT/ArrayRef.h" | |||
71 | #include "llvm/ADT/DenseMap.h" | |||
72 | #include "llvm/ADT/DenseSet.h" | |||
73 | #include "llvm/ADT/FoldingSet.h" | |||
74 | #include "llvm/ADT/PointerUnion.h" | |||
75 | #include "llvm/ADT/STLExtras.h" | |||
76 | #include "llvm/ADT/SmallPtrSet.h" | |||
77 | #include "llvm/ADT/SmallVector.h" | |||
78 | #include "llvm/ADT/StringExtras.h" | |||
79 | #include "llvm/ADT/StringRef.h" | |||
80 | #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" | |||
81 | #include "llvm/Support/Capacity.h" | |||
82 | #include "llvm/Support/Casting.h" | |||
83 | #include "llvm/Support/Compiler.h" | |||
84 | #include "llvm/Support/ErrorHandling.h" | |||
85 | #include "llvm/Support/MD5.h" | |||
86 | #include "llvm/Support/MathExtras.h" | |||
87 | #include "llvm/Support/raw_ostream.h" | |||
88 | #include "llvm/TargetParser/RISCVTargetParser.h" | |||
89 | #include "llvm/TargetParser/Triple.h" | |||
90 | #include <algorithm> | |||
91 | #include <cassert> | |||
92 | #include <cstddef> | |||
93 | #include <cstdint> | |||
94 | #include <cstdlib> | |||
95 | #include <map> | |||
96 | #include <memory> | |||
97 | #include <optional> | |||
98 | #include <string> | |||
99 | #include <tuple> | |||
100 | #include <utility> | |||
101 | ||||
102 | using namespace clang; | |||
103 | ||||
104 | enum FloatingRank { | |||
105 | BFloat16Rank, | |||
106 | Float16Rank, | |||
107 | HalfRank, | |||
108 | FloatRank, | |||
109 | DoubleRank, | |||
110 | LongDoubleRank, | |||
111 | Float128Rank, | |||
112 | Ibm128Rank | |||
113 | }; | |||
114 | ||||
115 | /// \returns location that is relevant when searching for Doc comments related | |||
116 | /// to \p D. | |||
117 | static SourceLocation getDeclLocForCommentSearch(const Decl *D, | |||
118 | SourceManager &SourceMgr) { | |||
119 | assert(D)(static_cast <bool> (D) ? void (0) : __assert_fail ("D" , "clang/lib/AST/ASTContext.cpp", 119, __extension__ __PRETTY_FUNCTION__ )); | |||
120 | ||||
121 | // User can not attach documentation to implicit declarations. | |||
122 | if (D->isImplicit()) | |||
123 | return {}; | |||
124 | ||||
125 | // User can not attach documentation to implicit instantiations. | |||
126 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { | |||
127 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) | |||
128 | return {}; | |||
129 | } | |||
130 | ||||
131 | if (const auto *VD = dyn_cast<VarDecl>(D)) { | |||
132 | if (VD->isStaticDataMember() && | |||
133 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) | |||
134 | return {}; | |||
135 | } | |||
136 | ||||
137 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(D)) { | |||
138 | if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) | |||
139 | return {}; | |||
140 | } | |||
141 | ||||
142 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) { | |||
143 | TemplateSpecializationKind TSK = CTSD->getSpecializationKind(); | |||
144 | if (TSK == TSK_ImplicitInstantiation || | |||
145 | TSK == TSK_Undeclared) | |||
146 | return {}; | |||
147 | } | |||
148 | ||||
149 | if (const auto *ED = dyn_cast<EnumDecl>(D)) { | |||
150 | if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) | |||
151 | return {}; | |||
152 | } | |||
153 | if (const auto *TD = dyn_cast<TagDecl>(D)) { | |||
154 | // When tag declaration (but not definition!) is part of the | |||
155 | // decl-specifier-seq of some other declaration, it doesn't get comment | |||
156 | if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition()) | |||
157 | return {}; | |||
158 | } | |||
159 | // TODO: handle comments for function parameters properly. | |||
160 | if (isa<ParmVarDecl>(D)) | |||
161 | return {}; | |||
162 | ||||
163 | // TODO: we could look up template parameter documentation in the template | |||
164 | // documentation. | |||
165 | if (isa<TemplateTypeParmDecl>(D) || | |||
166 | isa<NonTypeTemplateParmDecl>(D) || | |||
167 | isa<TemplateTemplateParmDecl>(D)) | |||
168 | return {}; | |||
169 | ||||
170 | // Find declaration location. | |||
171 | // For Objective-C declarations we generally don't expect to have multiple | |||
172 | // declarators, thus use declaration starting location as the "declaration | |||
173 | // location". | |||
174 | // For all other declarations multiple declarators are used quite frequently, | |||
175 | // so we use the location of the identifier as the "declaration location". | |||
176 | if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) || | |||
177 | isa<ObjCPropertyDecl>(D) || | |||
178 | isa<RedeclarableTemplateDecl>(D) || | |||
179 | isa<ClassTemplateSpecializationDecl>(D) || | |||
180 | // Allow association with Y across {} in `typedef struct X {} Y`. | |||
181 | isa<TypedefDecl>(D)) | |||
182 | return D->getBeginLoc(); | |||
183 | ||||
184 | const SourceLocation DeclLoc = D->getLocation(); | |||
185 | if (DeclLoc.isMacroID()) { | |||
186 | // There are (at least) three types of macros we care about here. | |||
187 | // | |||
188 | // 1. Macros that are used in the definition of a type outside the macro, | |||
189 | // with a comment attached at the macro call site. | |||
190 | // ``` | |||
191 | // #define MAKE_NAME(Foo) Name##Foo | |||
192 | // | |||
193 | // /// Comment is here, where we use the macro. | |||
194 | // struct MAKE_NAME(Foo) { | |||
195 | // int a; | |||
196 | // int b; | |||
197 | // }; | |||
198 | // ``` | |||
199 | // 2. Macros that define whole things along with the comment. | |||
200 | // ``` | |||
201 | // #define MAKE_METHOD(name) \ | |||
202 | // /** Comment is here, inside the macro. */ \ | |||
203 | // void name() {} | |||
204 | // | |||
205 | // struct S { | |||
206 | // MAKE_METHOD(f) | |||
207 | // } | |||
208 | // ``` | |||
209 | // 3. Macros that both declare a type and name a decl outside the macro. | |||
210 | // ``` | |||
211 | // /// Comment is here, where we use the macro. | |||
212 | // typedef NS_ENUM(NSInteger, Size) { | |||
213 | // SizeWidth, | |||
214 | // SizeHeight | |||
215 | // }; | |||
216 | // ``` | |||
217 | // In this case NS_ENUM declares am enum type, and uses the same name for | |||
218 | // the typedef declaration that appears outside the macro. The comment | |||
219 | // here should be applied to both declarations inside and outside the | |||
220 | // macro. | |||
221 | // | |||
222 | // We have found a Decl name that comes from inside a macro, but | |||
223 | // Decl::getLocation() returns the place where the macro is being called. | |||
224 | // If the declaration (and not just the name) resides inside the macro, | |||
225 | // then we want to map Decl::getLocation() into the macro to where the | |||
226 | // declaration and its attached comment (if any) were written. | |||
227 | // | |||
228 | // This mapping into the macro is done by mapping the location to its | |||
229 | // spelling location, however even if the declaration is inside a macro, | |||
230 | // the name's spelling can come from a macro argument (case 2 above). In | |||
231 | // this case mapping the location to the spelling location finds the | |||
232 | // argument's position (at `f` in MAKE_METHOD(`f`) above), which is not | |||
233 | // where the declaration and its comment are located. | |||
234 | // | |||
235 | // To avoid this issue, we make use of Decl::getBeginLocation() instead. | |||
236 | // While the declaration's position is where the name is written, the | |||
237 | // comment is always attached to the begining of the declaration, not to | |||
238 | // the name. | |||
239 | // | |||
240 | // In the first case, the begin location of the decl is outside the macro, | |||
241 | // at the location of `typedef`. This is where the comment is found as | |||
242 | // well. The begin location is not inside a macro, so it's spelling | |||
243 | // location is the same. | |||
244 | // | |||
245 | // In the second case, the begin location of the decl is the call to the | |||
246 | // macro, at `MAKE_METHOD`. However its spelling location is inside the | |||
247 | // the macro at the location of `void`. This is where the comment is found | |||
248 | // again. | |||
249 | // | |||
250 | // In the third case, there's no correct single behaviour. We want to use | |||
251 | // the comment outside the macro for the definition that's inside the macro. | |||
252 | // There is also a definition outside the macro, and we want the comment to | |||
253 | // apply to both. The cases we care about here is NS_ENUM() and | |||
254 | // NS_OPTIONS(). In general, if an enum is defined inside a macro, we should | |||
255 | // try to find the comment there. | |||
256 | ||||
257 | // This is handling case 3 for NS_ENUM() and NS_OPTIONS(), which define | |||
258 | // enum types inside the macro. | |||
259 | if (isa<EnumDecl>(D)) { | |||
260 | SourceLocation MacroCallLoc = SourceMgr.getExpansionLoc(DeclLoc); | |||
261 | if (auto BufferRef = | |||
262 | SourceMgr.getBufferOrNone(SourceMgr.getFileID(MacroCallLoc)); | |||
263 | BufferRef.has_value()) { | |||
264 | llvm::StringRef buffer = BufferRef->getBuffer().substr( | |||
265 | SourceMgr.getFileOffset(MacroCallLoc)); | |||
266 | if (buffer.starts_with("NS_ENUM(") || | |||
267 | buffer.starts_with("NS_OPTIONS(")) { | |||
268 | // We want to use the comment on the call to NS_ENUM and NS_OPTIONS | |||
269 | // macros for the types defined inside the macros, which is at the | |||
270 | // expansion location. | |||
271 | return MacroCallLoc; | |||
272 | } | |||
273 | } | |||
274 | } | |||
275 | return SourceMgr.getSpellingLoc(D->getBeginLoc()); | |||
276 | } | |||
277 | ||||
278 | return DeclLoc; | |||
279 | } | |||
280 | ||||
281 | RawComment *ASTContext::getRawCommentForDeclNoCacheImpl( | |||
282 | const Decl *D, const SourceLocation RepresentativeLocForDecl, | |||
283 | const std::map<unsigned, RawComment *> &CommentsInTheFile) const { | |||
284 | // If the declaration doesn't map directly to a location in a file, we | |||
285 | // can't find the comment. | |||
286 | if (RepresentativeLocForDecl.isInvalid() || | |||
287 | !RepresentativeLocForDecl.isFileID()) | |||
288 | return nullptr; | |||
289 | ||||
290 | // If there are no comments anywhere, we won't find anything. | |||
291 | if (CommentsInTheFile.empty()) | |||
292 | return nullptr; | |||
293 | ||||
294 | // Decompose the location for the declaration and find the beginning of the | |||
295 | // file buffer. | |||
296 | const std::pair<FileID, unsigned> DeclLocDecomp = | |||
297 | SourceMgr.getDecomposedLoc(RepresentativeLocForDecl); | |||
298 | ||||
299 | // Slow path. | |||
300 | auto OffsetCommentBehindDecl = | |||
301 | CommentsInTheFile.lower_bound(DeclLocDecomp.second); | |||
302 | ||||
303 | // First check whether we have a trailing comment. | |||
304 | if (OffsetCommentBehindDecl != CommentsInTheFile.end()) { | |||
305 | RawComment *CommentBehindDecl = OffsetCommentBehindDecl->second; | |||
306 | if ((CommentBehindDecl->isDocumentation() || | |||
307 | LangOpts.CommentOpts.ParseAllComments) && | |||
308 | CommentBehindDecl->isTrailingComment() && | |||
309 | (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D) || | |||
310 | isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D))) { | |||
311 | ||||
312 | // Check that Doxygen trailing comment comes after the declaration, starts | |||
313 | // on the same line and in the same file as the declaration. | |||
314 | if (SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) == | |||
315 | Comments.getCommentBeginLine(CommentBehindDecl, DeclLocDecomp.first, | |||
316 | OffsetCommentBehindDecl->first)) { | |||
317 | return CommentBehindDecl; | |||
318 | } | |||
319 | } | |||
320 | } | |||
321 | ||||
322 | // The comment just after the declaration was not a trailing comment. | |||
323 | // Let's look at the previous comment. | |||
324 | if (OffsetCommentBehindDecl == CommentsInTheFile.begin()) | |||
325 | return nullptr; | |||
326 | ||||
327 | auto OffsetCommentBeforeDecl = --OffsetCommentBehindDecl; | |||
328 | RawComment *CommentBeforeDecl = OffsetCommentBeforeDecl->second; | |||
329 | ||||
330 | // Check that we actually have a non-member Doxygen comment. | |||
331 | if (!(CommentBeforeDecl->isDocumentation() || | |||
332 | LangOpts.CommentOpts.ParseAllComments) || | |||
333 | CommentBeforeDecl->isTrailingComment()) | |||
334 | return nullptr; | |||
335 | ||||
336 | // Decompose the end of the comment. | |||
337 | const unsigned CommentEndOffset = | |||
338 | Comments.getCommentEndOffset(CommentBeforeDecl); | |||
339 | ||||
340 | // Get the corresponding buffer. | |||
341 | bool Invalid = false; | |||
342 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, | |||
343 | &Invalid).data(); | |||
344 | if (Invalid) | |||
345 | return nullptr; | |||
346 | ||||
347 | // Extract text between the comment and declaration. | |||
348 | StringRef Text(Buffer + CommentEndOffset, | |||
349 | DeclLocDecomp.second - CommentEndOffset); | |||
350 | ||||
351 | // There should be no other declarations or preprocessor directives between | |||
352 | // comment and declaration. | |||
353 | if (Text.find_last_of(";{}#@") != StringRef::npos) | |||
354 | return nullptr; | |||
355 | ||||
356 | return CommentBeforeDecl; | |||
357 | } | |||
358 | ||||
359 | RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { | |||
360 | const SourceLocation DeclLoc = getDeclLocForCommentSearch(D, SourceMgr); | |||
361 | ||||
362 | // If the declaration doesn't map directly to a location in a file, we | |||
363 | // can't find the comment. | |||
364 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) | |||
365 | return nullptr; | |||
366 | ||||
367 | if (ExternalSource && !CommentsLoaded) { | |||
368 | ExternalSource->ReadComments(); | |||
369 | CommentsLoaded = true; | |||
370 | } | |||
371 | ||||
372 | if (Comments.empty()) | |||
373 | return nullptr; | |||
374 | ||||
375 | const FileID File = SourceMgr.getDecomposedLoc(DeclLoc).first; | |||
376 | if (!File.isValid()) { | |||
377 | return nullptr; | |||
378 | } | |||
379 | const auto CommentsInThisFile = Comments.getCommentsInFile(File); | |||
380 | if (!CommentsInThisFile || CommentsInThisFile->empty()) | |||
381 | return nullptr; | |||
382 | ||||
383 | return getRawCommentForDeclNoCacheImpl(D, DeclLoc, *CommentsInThisFile); | |||
384 | } | |||
385 | ||||
386 | void ASTContext::addComment(const RawComment &RC) { | |||
387 | assert(LangOpts.RetainCommentsFromSystemHeaders ||(static_cast <bool> (LangOpts.RetainCommentsFromSystemHeaders || !SourceMgr.isInSystemHeader(RC.getSourceRange().getBegin( ))) ? void (0) : __assert_fail ("LangOpts.RetainCommentsFromSystemHeaders || !SourceMgr.isInSystemHeader(RC.getSourceRange().getBegin())" , "clang/lib/AST/ASTContext.cpp", 388, __extension__ __PRETTY_FUNCTION__ )) | |||
388 | !SourceMgr.isInSystemHeader(RC.getSourceRange().getBegin()))(static_cast <bool> (LangOpts.RetainCommentsFromSystemHeaders || !SourceMgr.isInSystemHeader(RC.getSourceRange().getBegin( ))) ? void (0) : __assert_fail ("LangOpts.RetainCommentsFromSystemHeaders || !SourceMgr.isInSystemHeader(RC.getSourceRange().getBegin())" , "clang/lib/AST/ASTContext.cpp", 388, __extension__ __PRETTY_FUNCTION__ )); | |||
389 | Comments.addComment(RC, LangOpts.CommentOpts, BumpAlloc); | |||
390 | } | |||
391 | ||||
392 | /// If we have a 'templated' declaration for a template, adjust 'D' to | |||
393 | /// refer to the actual template. | |||
394 | /// If we have an implicit instantiation, adjust 'D' to refer to template. | |||
395 | static const Decl &adjustDeclToTemplate(const Decl &D) { | |||
396 | if (const auto *FD = dyn_cast<FunctionDecl>(&D)) { | |||
397 | // Is this function declaration part of a function template? | |||
398 | if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) | |||
399 | return *FTD; | |||
400 | ||||
401 | // Nothing to do if function is not an implicit instantiation. | |||
402 | if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) | |||
403 | return D; | |||
404 | ||||
405 | // Function is an implicit instantiation of a function template? | |||
406 | if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) | |||
407 | return *FTD; | |||
408 | ||||
409 | // Function is instantiated from a member definition of a class template? | |||
410 | if (const FunctionDecl *MemberDecl = | |||
411 | FD->getInstantiatedFromMemberFunction()) | |||
412 | return *MemberDecl; | |||
413 | ||||
414 | return D; | |||
415 | } | |||
416 | if (const auto *VD = dyn_cast<VarDecl>(&D)) { | |||
417 | // Static data member is instantiated from a member definition of a class | |||
418 | // template? | |||
419 | if (VD->isStaticDataMember()) | |||
420 | if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember()) | |||
421 | return *MemberDecl; | |||
422 | ||||
423 | return D; | |||
424 | } | |||
425 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(&D)) { | |||
426 | // Is this class declaration part of a class template? | |||
427 | if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate()) | |||
428 | return *CTD; | |||
429 | ||||
430 | // Class is an implicit instantiation of a class template or partial | |||
431 | // specialization? | |||
432 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(CRD)) { | |||
433 | if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation) | |||
434 | return D; | |||
435 | llvm::PointerUnion<ClassTemplateDecl *, | |||
436 | ClassTemplatePartialSpecializationDecl *> | |||
437 | PU = CTSD->getSpecializedTemplateOrPartial(); | |||
438 | return PU.is<ClassTemplateDecl *>() | |||
439 | ? *static_cast<const Decl *>(PU.get<ClassTemplateDecl *>()) | |||
440 | : *static_cast<const Decl *>( | |||
441 | PU.get<ClassTemplatePartialSpecializationDecl *>()); | |||
442 | } | |||
443 | ||||
444 | // Class is instantiated from a member definition of a class template? | |||
445 | if (const MemberSpecializationInfo *Info = | |||
446 | CRD->getMemberSpecializationInfo()) | |||
447 | return *Info->getInstantiatedFrom(); | |||
448 | ||||
449 | return D; | |||
450 | } | |||
451 | if (const auto *ED = dyn_cast<EnumDecl>(&D)) { | |||
452 | // Enum is instantiated from a member definition of a class template? | |||
453 | if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum()) | |||
454 | return *MemberDecl; | |||
455 | ||||
456 | return D; | |||
457 | } | |||
458 | // FIXME: Adjust alias templates? | |||
459 | return D; | |||
460 | } | |||
461 | ||||
462 | const RawComment *ASTContext::getRawCommentForAnyRedecl( | |||
463 | const Decl *D, | |||
464 | const Decl **OriginalDecl) const { | |||
465 | if (!D) { | |||
466 | if (OriginalDecl) | |||
467 | OriginalDecl = nullptr; | |||
468 | return nullptr; | |||
469 | } | |||
470 | ||||
471 | D = &adjustDeclToTemplate(*D); | |||
472 | ||||
473 | // Any comment directly attached to D? | |||
474 | { | |||
475 | auto DeclComment = DeclRawComments.find(D); | |||
476 | if (DeclComment != DeclRawComments.end()) { | |||
477 | if (OriginalDecl) | |||
478 | *OriginalDecl = D; | |||
479 | return DeclComment->second; | |||
480 | } | |||
481 | } | |||
482 | ||||
483 | // Any comment attached to any redeclaration of D? | |||
484 | const Decl *CanonicalD = D->getCanonicalDecl(); | |||
485 | if (!CanonicalD) | |||
486 | return nullptr; | |||
487 | ||||
488 | { | |||
489 | auto RedeclComment = RedeclChainComments.find(CanonicalD); | |||
490 | if (RedeclComment != RedeclChainComments.end()) { | |||
491 | if (OriginalDecl) | |||
492 | *OriginalDecl = RedeclComment->second; | |||
493 | auto CommentAtRedecl = DeclRawComments.find(RedeclComment->second); | |||
494 | assert(CommentAtRedecl != DeclRawComments.end() &&(static_cast <bool> (CommentAtRedecl != DeclRawComments .end() && "This decl is supposed to have comment attached." ) ? void (0) : __assert_fail ("CommentAtRedecl != DeclRawComments.end() && \"This decl is supposed to have comment attached.\"" , "clang/lib/AST/ASTContext.cpp", 495, __extension__ __PRETTY_FUNCTION__ )) | |||
495 | "This decl is supposed to have comment attached.")(static_cast <bool> (CommentAtRedecl != DeclRawComments .end() && "This decl is supposed to have comment attached." ) ? void (0) : __assert_fail ("CommentAtRedecl != DeclRawComments.end() && \"This decl is supposed to have comment attached.\"" , "clang/lib/AST/ASTContext.cpp", 495, __extension__ __PRETTY_FUNCTION__ )); | |||
496 | return CommentAtRedecl->second; | |||
497 | } | |||
498 | } | |||
499 | ||||
500 | // Any redeclarations of D that we haven't checked for comments yet? | |||
501 | // We can't use DenseMap::iterator directly since it'd get invalid. | |||
502 | auto LastCheckedRedecl = [this, CanonicalD]() -> const Decl * { | |||
503 | auto LookupRes = CommentlessRedeclChains.find(CanonicalD); | |||
504 | if (LookupRes != CommentlessRedeclChains.end()) | |||
505 | return LookupRes->second; | |||
506 | return nullptr; | |||
507 | }(); | |||
508 | ||||
509 | for (const auto Redecl : D->redecls()) { | |||
510 | assert(Redecl)(static_cast <bool> (Redecl) ? void (0) : __assert_fail ("Redecl", "clang/lib/AST/ASTContext.cpp", 510, __extension__ __PRETTY_FUNCTION__)); | |||
511 | // Skip all redeclarations that have been checked previously. | |||
512 | if (LastCheckedRedecl) { | |||
513 | if (LastCheckedRedecl == Redecl) { | |||
514 | LastCheckedRedecl = nullptr; | |||
515 | } | |||
516 | continue; | |||
517 | } | |||
518 | const RawComment *RedeclComment = getRawCommentForDeclNoCache(Redecl); | |||
519 | if (RedeclComment) { | |||
520 | cacheRawCommentForDecl(*Redecl, *RedeclComment); | |||
521 | if (OriginalDecl) | |||
522 | *OriginalDecl = Redecl; | |||
523 | return RedeclComment; | |||
524 | } | |||
525 | CommentlessRedeclChains[CanonicalD] = Redecl; | |||
526 | } | |||
527 | ||||
528 | if (OriginalDecl) | |||
529 | *OriginalDecl = nullptr; | |||
530 | return nullptr; | |||
531 | } | |||
532 | ||||
533 | void ASTContext::cacheRawCommentForDecl(const Decl &OriginalD, | |||
534 | const RawComment &Comment) const { | |||
535 | assert(Comment.isDocumentation() || LangOpts.CommentOpts.ParseAllComments)(static_cast <bool> (Comment.isDocumentation() || LangOpts .CommentOpts.ParseAllComments) ? void (0) : __assert_fail ("Comment.isDocumentation() || LangOpts.CommentOpts.ParseAllComments" , "clang/lib/AST/ASTContext.cpp", 535, __extension__ __PRETTY_FUNCTION__ )); | |||
536 | DeclRawComments.try_emplace(&OriginalD, &Comment); | |||
537 | const Decl *const CanonicalDecl = OriginalD.getCanonicalDecl(); | |||
538 | RedeclChainComments.try_emplace(CanonicalDecl, &OriginalD); | |||
539 | CommentlessRedeclChains.erase(CanonicalDecl); | |||
540 | } | |||
541 | ||||
542 | static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod, | |||
543 | SmallVectorImpl<const NamedDecl *> &Redeclared) { | |||
544 | const DeclContext *DC = ObjCMethod->getDeclContext(); | |||
545 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(DC)) { | |||
546 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); | |||
547 | if (!ID) | |||
548 | return; | |||
549 | // Add redeclared method here. | |||
550 | for (const auto *Ext : ID->known_extensions()) { | |||
551 | if (ObjCMethodDecl *RedeclaredMethod = | |||
552 | Ext->getMethod(ObjCMethod->getSelector(), | |||
553 | ObjCMethod->isInstanceMethod())) | |||
554 | Redeclared.push_back(RedeclaredMethod); | |||
555 | } | |||
556 | } | |||
557 | } | |||
558 | ||||
559 | void ASTContext::attachCommentsToJustParsedDecls(ArrayRef<Decl *> Decls, | |||
560 | const Preprocessor *PP) { | |||
561 | if (Comments.empty() || Decls.empty()) | |||
562 | return; | |||
563 | ||||
564 | FileID File; | |||
565 | for (Decl *D : Decls) { | |||
566 | SourceLocation Loc = D->getLocation(); | |||
567 | if (Loc.isValid()) { | |||
568 | // See if there are any new comments that are not attached to a decl. | |||
569 | // The location doesn't have to be precise - we care only about the file. | |||
570 | File = SourceMgr.getDecomposedLoc(Loc).first; | |||
571 | break; | |||
572 | } | |||
573 | } | |||
574 | ||||
575 | if (File.isInvalid()) | |||
576 | return; | |||
577 | ||||
578 | auto CommentsInThisFile = Comments.getCommentsInFile(File); | |||
579 | if (!CommentsInThisFile || CommentsInThisFile->empty() || | |||
580 | CommentsInThisFile->rbegin()->second->isAttached()) | |||
581 | return; | |||
582 | ||||
583 | // There is at least one comment not attached to a decl. | |||
584 | // Maybe it should be attached to one of Decls? | |||
585 | // | |||
586 | // Note that this way we pick up not only comments that precede the | |||
587 | // declaration, but also comments that *follow* the declaration -- thanks to | |||
588 | // the lookahead in the lexer: we've consumed the semicolon and looked | |||
589 | // ahead through comments. | |||
590 | ||||
591 | for (const Decl *D : Decls) { | |||
592 | assert(D)(static_cast <bool> (D) ? void (0) : __assert_fail ("D" , "clang/lib/AST/ASTContext.cpp", 592, __extension__ __PRETTY_FUNCTION__ )); | |||
593 | if (D->isInvalidDecl()) | |||
594 | continue; | |||
595 | ||||
596 | D = &adjustDeclToTemplate(*D); | |||
597 | ||||
598 | const SourceLocation DeclLoc = getDeclLocForCommentSearch(D, SourceMgr); | |||
599 | ||||
600 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) | |||
601 | continue; | |||
602 | ||||
603 | if (DeclRawComments.count(D) > 0) | |||
604 | continue; | |||
605 | ||||
606 | if (RawComment *const DocComment = | |||
607 | getRawCommentForDeclNoCacheImpl(D, DeclLoc, *CommentsInThisFile)) { | |||
608 | cacheRawCommentForDecl(*D, *DocComment); | |||
609 | comments::FullComment *FC = DocComment->parse(*this, PP, D); | |||
610 | ParsedComments[D->getCanonicalDecl()] = FC; | |||
611 | } | |||
612 | } | |||
613 | } | |||
614 | ||||
615 | comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC, | |||
616 | const Decl *D) const { | |||
617 | auto *ThisDeclInfo = new (*this) comments::DeclInfo; | |||
618 | ThisDeclInfo->CommentDecl = D; | |||
619 | ThisDeclInfo->IsFilled = false; | |||
620 | ThisDeclInfo->fill(); | |||
621 | ThisDeclInfo->CommentDecl = FC->getDecl(); | |||
622 | if (!ThisDeclInfo->TemplateParameters) | |||
623 | ThisDeclInfo->TemplateParameters = FC->getDeclInfo()->TemplateParameters; | |||
624 | comments::FullComment *CFC = | |||
625 | new (*this) comments::FullComment(FC->getBlocks(), | |||
626 | ThisDeclInfo); | |||
627 | return CFC; | |||
628 | } | |||
629 | ||||
630 | comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const { | |||
631 | const RawComment *RC = getRawCommentForDeclNoCache(D); | |||
632 | return RC ? RC->parse(*this, nullptr, D) : nullptr; | |||
633 | } | |||
634 | ||||
635 | comments::FullComment *ASTContext::getCommentForDecl( | |||
636 | const Decl *D, | |||
637 | const Preprocessor *PP) const { | |||
638 | if (!D || D->isInvalidDecl()) | |||
639 | return nullptr; | |||
640 | D = &adjustDeclToTemplate(*D); | |||
641 | ||||
642 | const Decl *Canonical = D->getCanonicalDecl(); | |||
643 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = | |||
644 | ParsedComments.find(Canonical); | |||
645 | ||||
646 | if (Pos != ParsedComments.end()) { | |||
647 | if (Canonical != D) { | |||
648 | comments::FullComment *FC = Pos->second; | |||
649 | comments::FullComment *CFC = cloneFullComment(FC, D); | |||
650 | return CFC; | |||
651 | } | |||
652 | return Pos->second; | |||
653 | } | |||
654 | ||||
655 | const Decl *OriginalDecl = nullptr; | |||
656 | ||||
657 | const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl); | |||
658 | if (!RC) { | |||
659 | if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) { | |||
660 | SmallVector<const NamedDecl*, 8> Overridden; | |||
661 | const auto *OMD = dyn_cast<ObjCMethodDecl>(D); | |||
662 | if (OMD && OMD->isPropertyAccessor()) | |||
663 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) | |||
664 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) | |||
665 | return cloneFullComment(FC, D); | |||
666 | if (OMD) | |||
667 | addRedeclaredMethods(OMD, Overridden); | |||
668 | getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden); | |||
669 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) | |||
670 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) | |||
671 | return cloneFullComment(FC, D); | |||
672 | } | |||
673 | else if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { | |||
674 | // Attach any tag type's documentation to its typedef if latter | |||
675 | // does not have one of its own. | |||
676 | QualType QT = TD->getUnderlyingType(); | |||
677 | if (const auto *TT = QT->getAs<TagType>()) | |||
678 | if (const Decl *TD = TT->getDecl()) | |||
679 | if (comments::FullComment *FC = getCommentForDecl(TD, PP)) | |||
680 | return cloneFullComment(FC, D); | |||
681 | } | |||
682 | else if (const auto *IC = dyn_cast<ObjCInterfaceDecl>(D)) { | |||
683 | while (IC->getSuperClass()) { | |||
684 | IC = IC->getSuperClass(); | |||
685 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) | |||
686 | return cloneFullComment(FC, D); | |||
687 | } | |||
688 | } | |||
689 | else if (const auto *CD = dyn_cast<ObjCCategoryDecl>(D)) { | |||
690 | if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) | |||
691 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) | |||
692 | return cloneFullComment(FC, D); | |||
693 | } | |||
694 | else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { | |||
695 | if (!(RD = RD->getDefinition())) | |||
696 | return nullptr; | |||
697 | // Check non-virtual bases. | |||
698 | for (const auto &I : RD->bases()) { | |||
699 | if (I.isVirtual() || (I.getAccessSpecifier() != AS_public)) | |||
700 | continue; | |||
701 | QualType Ty = I.getType(); | |||
702 | if (Ty.isNull()) | |||
703 | continue; | |||
704 | if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) { | |||
705 | if (!(NonVirtualBase= NonVirtualBase->getDefinition())) | |||
706 | continue; | |||
707 | ||||
708 | if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) | |||
709 | return cloneFullComment(FC, D); | |||
710 | } | |||
711 | } | |||
712 | // Check virtual bases. | |||
713 | for (const auto &I : RD->vbases()) { | |||
714 | if (I.getAccessSpecifier() != AS_public) | |||
715 | continue; | |||
716 | QualType Ty = I.getType(); | |||
717 | if (Ty.isNull()) | |||
718 | continue; | |||
719 | if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { | |||
720 | if (!(VirtualBase= VirtualBase->getDefinition())) | |||
721 | continue; | |||
722 | if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) | |||
723 | return cloneFullComment(FC, D); | |||
724 | } | |||
725 | } | |||
726 | } | |||
727 | return nullptr; | |||
728 | } | |||
729 | ||||
730 | // If the RawComment was attached to other redeclaration of this Decl, we | |||
731 | // should parse the comment in context of that other Decl. This is important | |||
732 | // because comments can contain references to parameter names which can be | |||
733 | // different across redeclarations. | |||
734 | if (D != OriginalDecl && OriginalDecl) | |||
735 | return getCommentForDecl(OriginalDecl, PP); | |||
736 | ||||
737 | comments::FullComment *FC = RC->parse(*this, PP, D); | |||
738 | ParsedComments[Canonical] = FC; | |||
739 | return FC; | |||
740 | } | |||
741 | ||||
742 | void | |||
743 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, | |||
744 | const ASTContext &C, | |||
745 | TemplateTemplateParmDecl *Parm) { | |||
746 | ID.AddInteger(Parm->getDepth()); | |||
747 | ID.AddInteger(Parm->getPosition()); | |||
748 | ID.AddBoolean(Parm->isParameterPack()); | |||
749 | ||||
750 | TemplateParameterList *Params = Parm->getTemplateParameters(); | |||
751 | ID.AddInteger(Params->size()); | |||
752 | for (TemplateParameterList::const_iterator P = Params->begin(), | |||
753 | PEnd = Params->end(); | |||
754 | P != PEnd; ++P) { | |||
755 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { | |||
756 | ID.AddInteger(0); | |||
757 | ID.AddBoolean(TTP->isParameterPack()); | |||
758 | if (TTP->isExpandedParameterPack()) { | |||
759 | ID.AddBoolean(true); | |||
760 | ID.AddInteger(TTP->getNumExpansionParameters()); | |||
761 | } else | |||
762 | ID.AddBoolean(false); | |||
763 | continue; | |||
764 | } | |||
765 | ||||
766 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { | |||
767 | ID.AddInteger(1); | |||
768 | ID.AddBoolean(NTTP->isParameterPack()); | |||
769 | ID.AddPointer(C.getUnconstrainedType(C.getCanonicalType(NTTP->getType())) | |||
770 | .getAsOpaquePtr()); | |||
771 | if (NTTP->isExpandedParameterPack()) { | |||
772 | ID.AddBoolean(true); | |||
773 | ID.AddInteger(NTTP->getNumExpansionTypes()); | |||
774 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { | |||
775 | QualType T = NTTP->getExpansionType(I); | |||
776 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); | |||
777 | } | |||
778 | } else | |||
779 | ID.AddBoolean(false); | |||
780 | continue; | |||
781 | } | |||
782 | ||||
783 | auto *TTP = cast<TemplateTemplateParmDecl>(*P); | |||
784 | ID.AddInteger(2); | |||
785 | Profile(ID, C, TTP); | |||
786 | } | |||
787 | } | |||
788 | ||||
789 | TemplateTemplateParmDecl * | |||
790 | ASTContext::getCanonicalTemplateTemplateParmDecl( | |||
791 | TemplateTemplateParmDecl *TTP) const { | |||
792 | // Check if we already have a canonical template template parameter. | |||
793 | llvm::FoldingSetNodeID ID; | |||
794 | CanonicalTemplateTemplateParm::Profile(ID, *this, TTP); | |||
795 | void *InsertPos = nullptr; | |||
796 | CanonicalTemplateTemplateParm *Canonical | |||
797 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); | |||
798 | if (Canonical) | |||
799 | return Canonical->getParam(); | |||
800 | ||||
801 | // Build a canonical template parameter list. | |||
802 | TemplateParameterList *Params = TTP->getTemplateParameters(); | |||
803 | SmallVector<NamedDecl *, 4> CanonParams; | |||
804 | CanonParams.reserve(Params->size()); | |||
805 | for (TemplateParameterList::const_iterator P = Params->begin(), | |||
806 | PEnd = Params->end(); | |||
807 | P != PEnd; ++P) { | |||
808 | // Note that, per C++20 [temp.over.link]/6, when determining whether | |||
809 | // template-parameters are equivalent, constraints are ignored. | |||
810 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { | |||
811 | TemplateTypeParmDecl *NewTTP = TemplateTypeParmDecl::Create( | |||
812 | *this, getTranslationUnitDecl(), SourceLocation(), SourceLocation(), | |||
813 | TTP->getDepth(), TTP->getIndex(), nullptr, false, | |||
814 | TTP->isParameterPack(), /*HasTypeConstraint=*/false, | |||
815 | TTP->isExpandedParameterPack() | |||
816 | ? std::optional<unsigned>(TTP->getNumExpansionParameters()) | |||
817 | : std::nullopt); | |||
818 | CanonParams.push_back(NewTTP); | |||
819 | } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { | |||
820 | QualType T = getUnconstrainedType(getCanonicalType(NTTP->getType())); | |||
821 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); | |||
822 | NonTypeTemplateParmDecl *Param; | |||
823 | if (NTTP->isExpandedParameterPack()) { | |||
824 | SmallVector<QualType, 2> ExpandedTypes; | |||
825 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; | |||
826 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { | |||
827 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); | |||
828 | ExpandedTInfos.push_back( | |||
829 | getTrivialTypeSourceInfo(ExpandedTypes.back())); | |||
830 | } | |||
831 | ||||
832 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), | |||
833 | SourceLocation(), | |||
834 | SourceLocation(), | |||
835 | NTTP->getDepth(), | |||
836 | NTTP->getPosition(), nullptr, | |||
837 | T, | |||
838 | TInfo, | |||
839 | ExpandedTypes, | |||
840 | ExpandedTInfos); | |||
841 | } else { | |||
842 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), | |||
843 | SourceLocation(), | |||
844 | SourceLocation(), | |||
845 | NTTP->getDepth(), | |||
846 | NTTP->getPosition(), nullptr, | |||
847 | T, | |||
848 | NTTP->isParameterPack(), | |||
849 | TInfo); | |||
850 | } | |||
851 | CanonParams.push_back(Param); | |||
852 | } else | |||
853 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( | |||
854 | cast<TemplateTemplateParmDecl>(*P))); | |||
855 | } | |||
856 | ||||
857 | TemplateTemplateParmDecl *CanonTTP = TemplateTemplateParmDecl::Create( | |||
858 | *this, getTranslationUnitDecl(), SourceLocation(), TTP->getDepth(), | |||
859 | TTP->getPosition(), TTP->isParameterPack(), nullptr, | |||
860 | TemplateParameterList::Create(*this, SourceLocation(), SourceLocation(), | |||
861 | CanonParams, SourceLocation(), | |||
862 | /*RequiresClause=*/nullptr)); | |||
863 | ||||
864 | // Get the new insert position for the node we care about. | |||
865 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); | |||
866 | assert(!Canonical && "Shouldn't be in the map!")(static_cast <bool> (!Canonical && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!Canonical && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 866, __extension__ __PRETTY_FUNCTION__ )); | |||
867 | (void)Canonical; | |||
868 | ||||
869 | // Create the canonical template template parameter entry. | |||
870 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); | |||
871 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); | |||
872 | return CanonTTP; | |||
873 | } | |||
874 | ||||
875 | TargetCXXABI::Kind ASTContext::getCXXABIKind() const { | |||
876 | auto Kind = getTargetInfo().getCXXABI().getKind(); | |||
877 | return getLangOpts().CXXABI.value_or(Kind); | |||
878 | } | |||
879 | ||||
880 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { | |||
881 | if (!LangOpts.CPlusPlus) return nullptr; | |||
882 | ||||
883 | switch (getCXXABIKind()) { | |||
884 | case TargetCXXABI::AppleARM64: | |||
885 | case TargetCXXABI::Fuchsia: | |||
886 | case TargetCXXABI::GenericARM: // Same as Itanium at this level | |||
887 | case TargetCXXABI::iOS: | |||
888 | case TargetCXXABI::WatchOS: | |||
889 | case TargetCXXABI::GenericAArch64: | |||
890 | case TargetCXXABI::GenericMIPS: | |||
891 | case TargetCXXABI::GenericItanium: | |||
892 | case TargetCXXABI::WebAssembly: | |||
893 | case TargetCXXABI::XL: | |||
894 | return CreateItaniumCXXABI(*this); | |||
895 | case TargetCXXABI::Microsoft: | |||
896 | return CreateMicrosoftCXXABI(*this); | |||
897 | } | |||
898 | llvm_unreachable("Invalid CXXABI type!")::llvm::llvm_unreachable_internal("Invalid CXXABI type!", "clang/lib/AST/ASTContext.cpp" , 898); | |||
899 | } | |||
900 | ||||
901 | interp::Context &ASTContext::getInterpContext() { | |||
902 | if (!InterpContext) { | |||
903 | InterpContext.reset(new interp::Context(*this)); | |||
904 | } | |||
905 | return *InterpContext.get(); | |||
906 | } | |||
907 | ||||
908 | ParentMapContext &ASTContext::getParentMapContext() { | |||
909 | if (!ParentMapCtx) | |||
910 | ParentMapCtx.reset(new ParentMapContext(*this)); | |||
911 | return *ParentMapCtx.get(); | |||
912 | } | |||
913 | ||||
914 | static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI, | |||
915 | const LangOptions &LangOpts) { | |||
916 | switch (LangOpts.getAddressSpaceMapMangling()) { | |||
917 | case LangOptions::ASMM_Target: | |||
918 | return TI.useAddressSpaceMapMangling(); | |||
919 | case LangOptions::ASMM_On: | |||
920 | return true; | |||
921 | case LangOptions::ASMM_Off: | |||
922 | return false; | |||
923 | } | |||
924 | llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything.")::llvm::llvm_unreachable_internal("getAddressSpaceMapMangling() doesn't cover anything." , "clang/lib/AST/ASTContext.cpp", 924); | |||
925 | } | |||
926 | ||||
927 | ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM, | |||
928 | IdentifierTable &idents, SelectorTable &sels, | |||
929 | Builtin::Context &builtins, TranslationUnitKind TUKind) | |||
930 | : ConstantArrayTypes(this_(), ConstantArrayTypesLog2InitSize), | |||
931 | FunctionProtoTypes(this_(), FunctionProtoTypesLog2InitSize), | |||
932 | TemplateSpecializationTypes(this_()), | |||
933 | DependentTemplateSpecializationTypes(this_()), AutoTypes(this_()), | |||
934 | SubstTemplateTemplateParmPacks(this_()), | |||
935 | CanonTemplateTemplateParms(this_()), SourceMgr(SM), LangOpts(LOpts), | |||
936 | NoSanitizeL(new NoSanitizeList(LangOpts.NoSanitizeFiles, SM)), | |||
937 | XRayFilter(new XRayFunctionFilter(LangOpts.XRayAlwaysInstrumentFiles, | |||
938 | LangOpts.XRayNeverInstrumentFiles, | |||
939 | LangOpts.XRayAttrListFiles, SM)), | |||
940 | ProfList(new ProfileList(LangOpts.ProfileListFiles, SM)), | |||
941 | PrintingPolicy(LOpts), Idents(idents), Selectors(sels), | |||
942 | BuiltinInfo(builtins), TUKind(TUKind), DeclarationNames(*this), | |||
943 | Comments(SM), CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), | |||
944 | CompCategories(this_()), LastSDM(nullptr, 0) { | |||
945 | addTranslationUnitDecl(); | |||
946 | } | |||
947 | ||||
948 | void ASTContext::cleanup() { | |||
949 | // Release the DenseMaps associated with DeclContext objects. | |||
950 | // FIXME: Is this the ideal solution? | |||
951 | ReleaseDeclContextMaps(); | |||
952 | ||||
953 | // Call all of the deallocation functions on all of their targets. | |||
954 | for (auto &Pair : Deallocations) | |||
955 | (Pair.first)(Pair.second); | |||
956 | Deallocations.clear(); | |||
957 | ||||
958 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed | |||
959 | // because they can contain DenseMaps. | |||
960 | for (llvm::DenseMap<const ObjCContainerDecl*, | |||
961 | const ASTRecordLayout*>::iterator | |||
962 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) | |||
963 | // Increment in loop to prevent using deallocated memory. | |||
964 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) | |||
965 | R->Destroy(*this); | |||
966 | ObjCLayouts.clear(); | |||
967 | ||||
968 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator | |||
969 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { | |||
970 | // Increment in loop to prevent using deallocated memory. | |||
971 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) | |||
972 | R->Destroy(*this); | |||
973 | } | |||
974 | ASTRecordLayouts.clear(); | |||
975 | ||||
976 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), | |||
977 | AEnd = DeclAttrs.end(); | |||
978 | A != AEnd; ++A) | |||
979 | A->second->~AttrVec(); | |||
980 | DeclAttrs.clear(); | |||
981 | ||||
982 | for (const auto &Value : ModuleInitializers) | |||
983 | Value.second->~PerModuleInitializers(); | |||
984 | ModuleInitializers.clear(); | |||
985 | } | |||
986 | ||||
987 | ASTContext::~ASTContext() { cleanup(); } | |||
988 | ||||
989 | void ASTContext::setTraversalScope(const std::vector<Decl *> &TopLevelDecls) { | |||
990 | TraversalScope = TopLevelDecls; | |||
991 | getParentMapContext().clear(); | |||
992 | } | |||
993 | ||||
994 | void ASTContext::AddDeallocation(void (*Callback)(void *), void *Data) const { | |||
995 | Deallocations.push_back({Callback, Data}); | |||
996 | } | |||
997 | ||||
998 | void | |||
999 | ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) { | |||
1000 | ExternalSource = std::move(Source); | |||
1001 | } | |||
1002 | ||||
1003 | void ASTContext::PrintStats() const { | |||
1004 | llvm::errs() << "\n*** AST Context Stats:\n"; | |||
1005 | llvm::errs() << " " << Types.size() << " types total.\n"; | |||
1006 | ||||
1007 | unsigned counts[] = { | |||
1008 | #define TYPE(Name, Parent) 0, | |||
1009 | #define ABSTRACT_TYPE(Name, Parent) | |||
1010 | #include "clang/AST/TypeNodes.inc" | |||
1011 | 0 // Extra | |||
1012 | }; | |||
1013 | ||||
1014 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { | |||
1015 | Type *T = Types[i]; | |||
1016 | counts[(unsigned)T->getTypeClass()]++; | |||
1017 | } | |||
1018 | ||||
1019 | unsigned Idx = 0; | |||
1020 | unsigned TotalBytes = 0; | |||
1021 | #define TYPE(Name, Parent) \ | |||
1022 | if (counts[Idx]) \ | |||
1023 | llvm::errs() << " " << counts[Idx] << " " << #Name \ | |||
1024 | << " types, " << sizeof(Name##Type) << " each " \ | |||
1025 | << "(" << counts[Idx] * sizeof(Name##Type) \ | |||
1026 | << " bytes)\n"; \ | |||
1027 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ | |||
1028 | ++Idx; | |||
1029 | #define ABSTRACT_TYPE(Name, Parent) | |||
1030 | #include "clang/AST/TypeNodes.inc" | |||
1031 | ||||
1032 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; | |||
1033 | ||||
1034 | // Implicit special member functions. | |||
1035 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" | |||
1036 | << NumImplicitDefaultConstructors | |||
1037 | << " implicit default constructors created\n"; | |||
1038 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" | |||
1039 | << NumImplicitCopyConstructors | |||
1040 | << " implicit copy constructors created\n"; | |||
1041 | if (getLangOpts().CPlusPlus) | |||
1042 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" | |||
1043 | << NumImplicitMoveConstructors | |||
1044 | << " implicit move constructors created\n"; | |||
1045 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" | |||
1046 | << NumImplicitCopyAssignmentOperators | |||
1047 | << " implicit copy assignment operators created\n"; | |||
1048 | if (getLangOpts().CPlusPlus) | |||
1049 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" | |||
1050 | << NumImplicitMoveAssignmentOperators | |||
1051 | << " implicit move assignment operators created\n"; | |||
1052 | llvm::errs() << NumImplicitDestructorsDeclared << "/" | |||
1053 | << NumImplicitDestructors | |||
1054 | << " implicit destructors created\n"; | |||
1055 | ||||
1056 | if (ExternalSource) { | |||
1057 | llvm::errs() << "\n"; | |||
1058 | ExternalSource->PrintStats(); | |||
1059 | } | |||
1060 | ||||
1061 | BumpAlloc.PrintStats(); | |||
1062 | } | |||
1063 | ||||
1064 | void ASTContext::mergeDefinitionIntoModule(NamedDecl *ND, Module *M, | |||
1065 | bool NotifyListeners) { | |||
1066 | if (NotifyListeners) | |||
1067 | if (auto *Listener = getASTMutationListener()) | |||
1068 | Listener->RedefinedHiddenDefinition(ND, M); | |||
1069 | ||||
1070 | MergedDefModules[cast<NamedDecl>(ND->getCanonicalDecl())].push_back(M); | |||
1071 | } | |||
1072 | ||||
1073 | void ASTContext::deduplicateMergedDefinitonsFor(NamedDecl *ND) { | |||
1074 | auto It = MergedDefModules.find(cast<NamedDecl>(ND->getCanonicalDecl())); | |||
1075 | if (It == MergedDefModules.end()) | |||
1076 | return; | |||
1077 | ||||
1078 | auto &Merged = It->second; | |||
1079 | llvm::DenseSet<Module*> Found; | |||
1080 | for (Module *&M : Merged) | |||
1081 | if (!Found.insert(M).second) | |||
1082 | M = nullptr; | |||
1083 | llvm::erase_value(Merged, nullptr); | |||
1084 | } | |||
1085 | ||||
1086 | ArrayRef<Module *> | |||
1087 | ASTContext::getModulesWithMergedDefinition(const NamedDecl *Def) { | |||
1088 | auto MergedIt = | |||
1089 | MergedDefModules.find(cast<NamedDecl>(Def->getCanonicalDecl())); | |||
1090 | if (MergedIt == MergedDefModules.end()) | |||
1091 | return std::nullopt; | |||
1092 | return MergedIt->second; | |||
1093 | } | |||
1094 | ||||
1095 | void ASTContext::PerModuleInitializers::resolve(ASTContext &Ctx) { | |||
1096 | if (LazyInitializers.empty()) | |||
1097 | return; | |||
1098 | ||||
1099 | auto *Source = Ctx.getExternalSource(); | |||
1100 | assert(Source && "lazy initializers but no external source")(static_cast <bool> (Source && "lazy initializers but no external source" ) ? void (0) : __assert_fail ("Source && \"lazy initializers but no external source\"" , "clang/lib/AST/ASTContext.cpp", 1100, __extension__ __PRETTY_FUNCTION__ )); | |||
1101 | ||||
1102 | auto LazyInits = std::move(LazyInitializers); | |||
1103 | LazyInitializers.clear(); | |||
1104 | ||||
1105 | for (auto ID : LazyInits) | |||
1106 | Initializers.push_back(Source->GetExternalDecl(ID)); | |||
1107 | ||||
1108 | assert(LazyInitializers.empty() &&(static_cast <bool> (LazyInitializers.empty() && "GetExternalDecl for lazy module initializer added more inits" ) ? void (0) : __assert_fail ("LazyInitializers.empty() && \"GetExternalDecl for lazy module initializer added more inits\"" , "clang/lib/AST/ASTContext.cpp", 1109, __extension__ __PRETTY_FUNCTION__ )) | |||
1109 | "GetExternalDecl for lazy module initializer added more inits")(static_cast <bool> (LazyInitializers.empty() && "GetExternalDecl for lazy module initializer added more inits" ) ? void (0) : __assert_fail ("LazyInitializers.empty() && \"GetExternalDecl for lazy module initializer added more inits\"" , "clang/lib/AST/ASTContext.cpp", 1109, __extension__ __PRETTY_FUNCTION__ )); | |||
1110 | } | |||
1111 | ||||
1112 | void ASTContext::addModuleInitializer(Module *M, Decl *D) { | |||
1113 | // One special case: if we add a module initializer that imports another | |||
1114 | // module, and that module's only initializer is an ImportDecl, simplify. | |||
1115 | if (const auto *ID = dyn_cast<ImportDecl>(D)) { | |||
1116 | auto It = ModuleInitializers.find(ID->getImportedModule()); | |||
1117 | ||||
1118 | // Maybe the ImportDecl does nothing at all. (Common case.) | |||
1119 | if (It == ModuleInitializers.end()) | |||
1120 | return; | |||
1121 | ||||
1122 | // Maybe the ImportDecl only imports another ImportDecl. | |||
1123 | auto &Imported = *It->second; | |||
1124 | if (Imported.Initializers.size() + Imported.LazyInitializers.size() == 1) { | |||
1125 | Imported.resolve(*this); | |||
1126 | auto *OnlyDecl = Imported.Initializers.front(); | |||
1127 | if (isa<ImportDecl>(OnlyDecl)) | |||
1128 | D = OnlyDecl; | |||
1129 | } | |||
1130 | } | |||
1131 | ||||
1132 | auto *&Inits = ModuleInitializers[M]; | |||
1133 | if (!Inits) | |||
1134 | Inits = new (*this) PerModuleInitializers; | |||
1135 | Inits->Initializers.push_back(D); | |||
1136 | } | |||
1137 | ||||
1138 | void ASTContext::addLazyModuleInitializers(Module *M, ArrayRef<uint32_t> IDs) { | |||
1139 | auto *&Inits = ModuleInitializers[M]; | |||
1140 | if (!Inits) | |||
1141 | Inits = new (*this) PerModuleInitializers; | |||
1142 | Inits->LazyInitializers.insert(Inits->LazyInitializers.end(), | |||
1143 | IDs.begin(), IDs.end()); | |||
1144 | } | |||
1145 | ||||
1146 | ArrayRef<Decl *> ASTContext::getModuleInitializers(Module *M) { | |||
1147 | auto It = ModuleInitializers.find(M); | |||
1148 | if (It == ModuleInitializers.end()) | |||
1149 | return std::nullopt; | |||
1150 | ||||
1151 | auto *Inits = It->second; | |||
1152 | Inits->resolve(*this); | |||
1153 | return Inits->Initializers; | |||
1154 | } | |||
1155 | ||||
1156 | void ASTContext::setCurrentNamedModule(Module *M) { | |||
1157 | assert(M->isModulePurview())(static_cast <bool> (M->isModulePurview()) ? void (0 ) : __assert_fail ("M->isModulePurview()", "clang/lib/AST/ASTContext.cpp" , 1157, __extension__ __PRETTY_FUNCTION__)); | |||
1158 | assert(!CurrentCXXNamedModule &&(static_cast <bool> (!CurrentCXXNamedModule && "We should set named module for ASTContext for only once" ) ? void (0) : __assert_fail ("!CurrentCXXNamedModule && \"We should set named module for ASTContext for only once\"" , "clang/lib/AST/ASTContext.cpp", 1159, __extension__ __PRETTY_FUNCTION__ )) | |||
1159 | "We should set named module for ASTContext for only once")(static_cast <bool> (!CurrentCXXNamedModule && "We should set named module for ASTContext for only once" ) ? void (0) : __assert_fail ("!CurrentCXXNamedModule && \"We should set named module for ASTContext for only once\"" , "clang/lib/AST/ASTContext.cpp", 1159, __extension__ __PRETTY_FUNCTION__ )); | |||
1160 | CurrentCXXNamedModule = M; | |||
1161 | } | |||
1162 | ||||
1163 | ExternCContextDecl *ASTContext::getExternCContextDecl() const { | |||
1164 | if (!ExternCContext) | |||
1165 | ExternCContext = ExternCContextDecl::Create(*this, getTranslationUnitDecl()); | |||
1166 | ||||
1167 | return ExternCContext; | |||
1168 | } | |||
1169 | ||||
1170 | BuiltinTemplateDecl * | |||
1171 | ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK, | |||
1172 | const IdentifierInfo *II) const { | |||
1173 | auto *BuiltinTemplate = | |||
1174 | BuiltinTemplateDecl::Create(*this, getTranslationUnitDecl(), II, BTK); | |||
1175 | BuiltinTemplate->setImplicit(); | |||
1176 | getTranslationUnitDecl()->addDecl(BuiltinTemplate); | |||
1177 | ||||
1178 | return BuiltinTemplate; | |||
1179 | } | |||
1180 | ||||
1181 | BuiltinTemplateDecl * | |||
1182 | ASTContext::getMakeIntegerSeqDecl() const { | |||
1183 | if (!MakeIntegerSeqDecl) | |||
1184 | MakeIntegerSeqDecl = buildBuiltinTemplateDecl(BTK__make_integer_seq, | |||
1185 | getMakeIntegerSeqName()); | |||
1186 | return MakeIntegerSeqDecl; | |||
1187 | } | |||
1188 | ||||
1189 | BuiltinTemplateDecl * | |||
1190 | ASTContext::getTypePackElementDecl() const { | |||
1191 | if (!TypePackElementDecl) | |||
1192 | TypePackElementDecl = buildBuiltinTemplateDecl(BTK__type_pack_element, | |||
1193 | getTypePackElementName()); | |||
1194 | return TypePackElementDecl; | |||
1195 | } | |||
1196 | ||||
1197 | RecordDecl *ASTContext::buildImplicitRecord(StringRef Name, | |||
1198 | RecordDecl::TagKind TK) const { | |||
1199 | SourceLocation Loc; | |||
1200 | RecordDecl *NewDecl; | |||
1201 | if (getLangOpts().CPlusPlus) | |||
1202 | NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, | |||
1203 | Loc, &Idents.get(Name)); | |||
1204 | else | |||
1205 | NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc, | |||
1206 | &Idents.get(Name)); | |||
1207 | NewDecl->setImplicit(); | |||
1208 | NewDecl->addAttr(TypeVisibilityAttr::CreateImplicit( | |||
1209 | const_cast<ASTContext &>(*this), TypeVisibilityAttr::Default)); | |||
1210 | return NewDecl; | |||
1211 | } | |||
1212 | ||||
1213 | TypedefDecl *ASTContext::buildImplicitTypedef(QualType T, | |||
1214 | StringRef Name) const { | |||
1215 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); | |||
1216 | TypedefDecl *NewDecl = TypedefDecl::Create( | |||
1217 | const_cast<ASTContext &>(*this), getTranslationUnitDecl(), | |||
1218 | SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo); | |||
1219 | NewDecl->setImplicit(); | |||
1220 | return NewDecl; | |||
1221 | } | |||
1222 | ||||
1223 | TypedefDecl *ASTContext::getInt128Decl() const { | |||
1224 | if (!Int128Decl) | |||
1225 | Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t"); | |||
1226 | return Int128Decl; | |||
1227 | } | |||
1228 | ||||
1229 | TypedefDecl *ASTContext::getUInt128Decl() const { | |||
1230 | if (!UInt128Decl) | |||
1231 | UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t"); | |||
1232 | return UInt128Decl; | |||
1233 | } | |||
1234 | ||||
1235 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { | |||
1236 | auto *Ty = new (*this, TypeAlignment) BuiltinType(K); | |||
1237 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); | |||
1238 | Types.push_back(Ty); | |||
1239 | } | |||
1240 | ||||
1241 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target, | |||
1242 | const TargetInfo *AuxTarget) { | |||
1243 | assert((!this->Target || this->Target == &Target) &&(static_cast <bool> ((!this->Target || this->Target == &Target) && "Incorrect target reinitialization" ) ? void (0) : __assert_fail ("(!this->Target || this->Target == &Target) && \"Incorrect target reinitialization\"" , "clang/lib/AST/ASTContext.cpp", 1244, __extension__ __PRETTY_FUNCTION__ )) | |||
1244 | "Incorrect target reinitialization")(static_cast <bool> ((!this->Target || this->Target == &Target) && "Incorrect target reinitialization" ) ? void (0) : __assert_fail ("(!this->Target || this->Target == &Target) && \"Incorrect target reinitialization\"" , "clang/lib/AST/ASTContext.cpp", 1244, __extension__ __PRETTY_FUNCTION__ )); | |||
1245 | assert(VoidTy.isNull() && "Context reinitialized?")(static_cast <bool> (VoidTy.isNull() && "Context reinitialized?" ) ? void (0) : __assert_fail ("VoidTy.isNull() && \"Context reinitialized?\"" , "clang/lib/AST/ASTContext.cpp", 1245, __extension__ __PRETTY_FUNCTION__ )); | |||
1246 | ||||
1247 | this->Target = &Target; | |||
1248 | this->AuxTarget = AuxTarget; | |||
1249 | ||||
1250 | ABI.reset(createCXXABI(Target)); | |||
1251 | AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(Target, LangOpts); | |||
1252 | ||||
1253 | // C99 6.2.5p19. | |||
1254 | InitBuiltinType(VoidTy, BuiltinType::Void); | |||
1255 | ||||
1256 | // C99 6.2.5p2. | |||
1257 | InitBuiltinType(BoolTy, BuiltinType::Bool); | |||
1258 | // C99 6.2.5p3. | |||
1259 | if (LangOpts.CharIsSigned) | |||
1260 | InitBuiltinType(CharTy, BuiltinType::Char_S); | |||
1261 | else | |||
1262 | InitBuiltinType(CharTy, BuiltinType::Char_U); | |||
1263 | // C99 6.2.5p4. | |||
1264 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); | |||
1265 | InitBuiltinType(ShortTy, BuiltinType::Short); | |||
1266 | InitBuiltinType(IntTy, BuiltinType::Int); | |||
1267 | InitBuiltinType(LongTy, BuiltinType::Long); | |||
1268 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); | |||
1269 | ||||
1270 | // C99 6.2.5p6. | |||
1271 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); | |||
1272 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); | |||
1273 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); | |||
1274 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); | |||
1275 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); | |||
1276 | ||||
1277 | // C99 6.2.5p10. | |||
1278 | InitBuiltinType(FloatTy, BuiltinType::Float); | |||
1279 | InitBuiltinType(DoubleTy, BuiltinType::Double); | |||
1280 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); | |||
1281 | ||||
1282 | // GNU extension, __float128 for IEEE quadruple precision | |||
1283 | InitBuiltinType(Float128Ty, BuiltinType::Float128); | |||
1284 | ||||
1285 | // __ibm128 for IBM extended precision | |||
1286 | InitBuiltinType(Ibm128Ty, BuiltinType::Ibm128); | |||
1287 | ||||
1288 | // C11 extension ISO/IEC TS 18661-3 | |||
1289 | InitBuiltinType(Float16Ty, BuiltinType::Float16); | |||
1290 | ||||
1291 | // ISO/IEC JTC1 SC22 WG14 N1169 Extension | |||
1292 | InitBuiltinType(ShortAccumTy, BuiltinType::ShortAccum); | |||
1293 | InitBuiltinType(AccumTy, BuiltinType::Accum); | |||
1294 | InitBuiltinType(LongAccumTy, BuiltinType::LongAccum); | |||
1295 | InitBuiltinType(UnsignedShortAccumTy, BuiltinType::UShortAccum); | |||
1296 | InitBuiltinType(UnsignedAccumTy, BuiltinType::UAccum); | |||
1297 | InitBuiltinType(UnsignedLongAccumTy, BuiltinType::ULongAccum); | |||
1298 | InitBuiltinType(ShortFractTy, BuiltinType::ShortFract); | |||
1299 | InitBuiltinType(FractTy, BuiltinType::Fract); | |||
1300 | InitBuiltinType(LongFractTy, BuiltinType::LongFract); | |||
1301 | InitBuiltinType(UnsignedShortFractTy, BuiltinType::UShortFract); | |||
1302 | InitBuiltinType(UnsignedFractTy, BuiltinType::UFract); | |||
1303 | InitBuiltinType(UnsignedLongFractTy, BuiltinType::ULongFract); | |||
1304 | InitBuiltinType(SatShortAccumTy, BuiltinType::SatShortAccum); | |||
1305 | InitBuiltinType(SatAccumTy, BuiltinType::SatAccum); | |||
1306 | InitBuiltinType(SatLongAccumTy, BuiltinType::SatLongAccum); | |||
1307 | InitBuiltinType(SatUnsignedShortAccumTy, BuiltinType::SatUShortAccum); | |||
1308 | InitBuiltinType(SatUnsignedAccumTy, BuiltinType::SatUAccum); | |||
1309 | InitBuiltinType(SatUnsignedLongAccumTy, BuiltinType::SatULongAccum); | |||
1310 | InitBuiltinType(SatShortFractTy, BuiltinType::SatShortFract); | |||
1311 | InitBuiltinType(SatFractTy, BuiltinType::SatFract); | |||
1312 | InitBuiltinType(SatLongFractTy, BuiltinType::SatLongFract); | |||
1313 | InitBuiltinType(SatUnsignedShortFractTy, BuiltinType::SatUShortFract); | |||
1314 | InitBuiltinType(SatUnsignedFractTy, BuiltinType::SatUFract); | |||
1315 | InitBuiltinType(SatUnsignedLongFractTy, BuiltinType::SatULongFract); | |||
1316 | ||||
1317 | // GNU extension, 128-bit integers. | |||
1318 | InitBuiltinType(Int128Ty, BuiltinType::Int128); | |||
1319 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); | |||
1320 | ||||
1321 | // C++ 3.9.1p5 | |||
1322 | if (TargetInfo::isTypeSigned(Target.getWCharType())) | |||
1323 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); | |||
1324 | else // -fshort-wchar makes wchar_t be unsigned. | |||
1325 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); | |||
1326 | if (LangOpts.CPlusPlus && LangOpts.WChar) | |||
1327 | WideCharTy = WCharTy; | |||
1328 | else { | |||
1329 | // C99 (or C++ using -fno-wchar). | |||
1330 | WideCharTy = getFromTargetType(Target.getWCharType()); | |||
1331 | } | |||
1332 | ||||
1333 | WIntTy = getFromTargetType(Target.getWIntType()); | |||
1334 | ||||
1335 | // C++20 (proposed) | |||
1336 | InitBuiltinType(Char8Ty, BuiltinType::Char8); | |||
1337 | ||||
1338 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ | |||
1339 | InitBuiltinType(Char16Ty, BuiltinType::Char16); | |||
1340 | else // C99 | |||
1341 | Char16Ty = getFromTargetType(Target.getChar16Type()); | |||
1342 | ||||
1343 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ | |||
1344 | InitBuiltinType(Char32Ty, BuiltinType::Char32); | |||
1345 | else // C99 | |||
1346 | Char32Ty = getFromTargetType(Target.getChar32Type()); | |||
1347 | ||||
1348 | // Placeholder type for type-dependent expressions whose type is | |||
1349 | // completely unknown. No code should ever check a type against | |||
1350 | // DependentTy and users should never see it; however, it is here to | |||
1351 | // help diagnose failures to properly check for type-dependent | |||
1352 | // expressions. | |||
1353 | InitBuiltinType(DependentTy, BuiltinType::Dependent); | |||
1354 | ||||
1355 | // Placeholder type for functions. | |||
1356 | InitBuiltinType(OverloadTy, BuiltinType::Overload); | |||
1357 | ||||
1358 | // Placeholder type for bound members. | |||
1359 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); | |||
1360 | ||||
1361 | // Placeholder type for pseudo-objects. | |||
1362 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); | |||
1363 | ||||
1364 | // "any" type; useful for debugger-like clients. | |||
1365 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); | |||
1366 | ||||
1367 | // Placeholder type for unbridged ARC casts. | |||
1368 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); | |||
1369 | ||||
1370 | // Placeholder type for builtin functions. | |||
1371 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); | |||
1372 | ||||
1373 | // Placeholder type for OMP array sections. | |||
1374 | if (LangOpts.OpenMP) { | |||
1375 | InitBuiltinType(OMPArraySectionTy, BuiltinType::OMPArraySection); | |||
1376 | InitBuiltinType(OMPArrayShapingTy, BuiltinType::OMPArrayShaping); | |||
1377 | InitBuiltinType(OMPIteratorTy, BuiltinType::OMPIterator); | |||
1378 | } | |||
1379 | if (LangOpts.MatrixTypes) | |||
1380 | InitBuiltinType(IncompleteMatrixIdxTy, BuiltinType::IncompleteMatrixIdx); | |||
1381 | ||||
1382 | // Builtin types for 'id', 'Class', and 'SEL'. | |||
1383 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); | |||
1384 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); | |||
1385 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); | |||
1386 | ||||
1387 | if (LangOpts.OpenCL) { | |||
1388 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | |||
1389 | InitBuiltinType(SingletonId, BuiltinType::Id); | |||
1390 | #include "clang/Basic/OpenCLImageTypes.def" | |||
1391 | ||||
1392 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); | |||
1393 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); | |||
1394 | InitBuiltinType(OCLClkEventTy, BuiltinType::OCLClkEvent); | |||
1395 | InitBuiltinType(OCLQueueTy, BuiltinType::OCLQueue); | |||
1396 | InitBuiltinType(OCLReserveIDTy, BuiltinType::OCLReserveID); | |||
1397 | ||||
1398 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | |||
1399 | InitBuiltinType(Id##Ty, BuiltinType::Id); | |||
1400 | #include "clang/Basic/OpenCLExtensionTypes.def" | |||
1401 | } | |||
1402 | ||||
1403 | if (Target.hasAArch64SVETypes()) { | |||
1404 | #define SVE_TYPE(Name, Id, SingletonId) \ | |||
1405 | InitBuiltinType(SingletonId, BuiltinType::Id); | |||
1406 | #include "clang/Basic/AArch64SVEACLETypes.def" | |||
1407 | } | |||
1408 | ||||
1409 | if (Target.getTriple().isPPC64()) { | |||
1410 | #define PPC_VECTOR_MMA_TYPE(Name, Id, Size) \ | |||
1411 | InitBuiltinType(Id##Ty, BuiltinType::Id); | |||
1412 | #include "clang/Basic/PPCTypes.def" | |||
1413 | #define PPC_VECTOR_VSX_TYPE(Name, Id, Size) \ | |||
1414 | InitBuiltinType(Id##Ty, BuiltinType::Id); | |||
1415 | #include "clang/Basic/PPCTypes.def" | |||
1416 | } | |||
1417 | ||||
1418 | if (Target.hasRISCVVTypes()) { | |||
1419 | #define RVV_TYPE(Name, Id, SingletonId) \ | |||
1420 | InitBuiltinType(SingletonId, BuiltinType::Id); | |||
1421 | #include "clang/Basic/RISCVVTypes.def" | |||
1422 | } | |||
1423 | ||||
1424 | if (Target.getTriple().isWasm() && Target.hasFeature("reference-types")) { | |||
1425 | #define WASM_TYPE(Name, Id, SingletonId) \ | |||
1426 | InitBuiltinType(SingletonId, BuiltinType::Id); | |||
1427 | #include "clang/Basic/WebAssemblyReferenceTypes.def" | |||
1428 | } | |||
1429 | ||||
1430 | // Builtin type for __objc_yes and __objc_no | |||
1431 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? | |||
1432 | SignedCharTy : BoolTy); | |||
1433 | ||||
1434 | ObjCConstantStringType = QualType(); | |||
1435 | ||||
1436 | ObjCSuperType = QualType(); | |||
1437 | ||||
1438 | // void * type | |||
1439 | if (LangOpts.OpenCLGenericAddressSpace) { | |||
1440 | auto Q = VoidTy.getQualifiers(); | |||
1441 | Q.setAddressSpace(LangAS::opencl_generic); | |||
1442 | VoidPtrTy = getPointerType(getCanonicalType( | |||
1443 | getQualifiedType(VoidTy.getUnqualifiedType(), Q))); | |||
1444 | } else { | |||
1445 | VoidPtrTy = getPointerType(VoidTy); | |||
1446 | } | |||
1447 | ||||
1448 | // nullptr type (C++0x 2.14.7) | |||
1449 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); | |||
1450 | ||||
1451 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 | |||
1452 | InitBuiltinType(HalfTy, BuiltinType::Half); | |||
1453 | ||||
1454 | InitBuiltinType(BFloat16Ty, BuiltinType::BFloat16); | |||
1455 | ||||
1456 | // Builtin type used to help define __builtin_va_list. | |||
1457 | VaListTagDecl = nullptr; | |||
1458 | ||||
1459 | // MSVC predeclares struct _GUID, and we need it to create MSGuidDecls. | |||
1460 | if (LangOpts.MicrosoftExt || LangOpts.Borland) { | |||
1461 | MSGuidTagDecl = buildImplicitRecord("_GUID"); | |||
1462 | getTranslationUnitDecl()->addDecl(MSGuidTagDecl); | |||
1463 | } | |||
1464 | } | |||
1465 | ||||
1466 | DiagnosticsEngine &ASTContext::getDiagnostics() const { | |||
1467 | return SourceMgr.getDiagnostics(); | |||
1468 | } | |||
1469 | ||||
1470 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { | |||
1471 | AttrVec *&Result = DeclAttrs[D]; | |||
1472 | if (!Result) { | |||
1473 | void *Mem = Allocate(sizeof(AttrVec)); | |||
1474 | Result = new (Mem) AttrVec; | |||
1475 | } | |||
1476 | ||||
1477 | return *Result; | |||
1478 | } | |||
1479 | ||||
1480 | /// Erase the attributes corresponding to the given declaration. | |||
1481 | void ASTContext::eraseDeclAttrs(const Decl *D) { | |||
1482 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); | |||
1483 | if (Pos != DeclAttrs.end()) { | |||
1484 | Pos->second->~AttrVec(); | |||
1485 | DeclAttrs.erase(Pos); | |||
1486 | } | |||
1487 | } | |||
1488 | ||||
1489 | // FIXME: Remove ? | |||
1490 | MemberSpecializationInfo * | |||
1491 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { | |||
1492 | assert(Var->isStaticDataMember() && "Not a static data member")(static_cast <bool> (Var->isStaticDataMember() && "Not a static data member") ? void (0) : __assert_fail ("Var->isStaticDataMember() && \"Not a static data member\"" , "clang/lib/AST/ASTContext.cpp", 1492, __extension__ __PRETTY_FUNCTION__ )); | |||
1493 | return getTemplateOrSpecializationInfo(Var) | |||
1494 | .dyn_cast<MemberSpecializationInfo *>(); | |||
1495 | } | |||
1496 | ||||
1497 | ASTContext::TemplateOrSpecializationInfo | |||
1498 | ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) { | |||
1499 | llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos = | |||
1500 | TemplateOrInstantiation.find(Var); | |||
1501 | if (Pos == TemplateOrInstantiation.end()) | |||
1502 | return {}; | |||
1503 | ||||
1504 | return Pos->second; | |||
1505 | } | |||
1506 | ||||
1507 | void | |||
1508 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, | |||
1509 | TemplateSpecializationKind TSK, | |||
1510 | SourceLocation PointOfInstantiation) { | |||
1511 | assert(Inst->isStaticDataMember() && "Not a static data member")(static_cast <bool> (Inst->isStaticDataMember() && "Not a static data member") ? void (0) : __assert_fail ("Inst->isStaticDataMember() && \"Not a static data member\"" , "clang/lib/AST/ASTContext.cpp", 1511, __extension__ __PRETTY_FUNCTION__ )); | |||
1512 | assert(Tmpl->isStaticDataMember() && "Not a static data member")(static_cast <bool> (Tmpl->isStaticDataMember() && "Not a static data member") ? void (0) : __assert_fail ("Tmpl->isStaticDataMember() && \"Not a static data member\"" , "clang/lib/AST/ASTContext.cpp", 1512, __extension__ __PRETTY_FUNCTION__ )); | |||
1513 | setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo( | |||
1514 | Tmpl, TSK, PointOfInstantiation)); | |||
1515 | } | |||
1516 | ||||
1517 | void | |||
1518 | ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst, | |||
1519 | TemplateOrSpecializationInfo TSI) { | |||
1520 | assert(!TemplateOrInstantiation[Inst] &&(static_cast <bool> (!TemplateOrInstantiation[Inst] && "Already noted what the variable was instantiated from") ? void (0) : __assert_fail ("!TemplateOrInstantiation[Inst] && \"Already noted what the variable was instantiated from\"" , "clang/lib/AST/ASTContext.cpp", 1521, __extension__ __PRETTY_FUNCTION__ )) | |||
1521 | "Already noted what the variable was instantiated from")(static_cast <bool> (!TemplateOrInstantiation[Inst] && "Already noted what the variable was instantiated from") ? void (0) : __assert_fail ("!TemplateOrInstantiation[Inst] && \"Already noted what the variable was instantiated from\"" , "clang/lib/AST/ASTContext.cpp", 1521, __extension__ __PRETTY_FUNCTION__ )); | |||
1522 | TemplateOrInstantiation[Inst] = TSI; | |||
1523 | } | |||
1524 | ||||
1525 | NamedDecl * | |||
1526 | ASTContext::getInstantiatedFromUsingDecl(NamedDecl *UUD) { | |||
1527 | auto Pos = InstantiatedFromUsingDecl.find(UUD); | |||
1528 | if (Pos == InstantiatedFromUsingDecl.end()) | |||
1529 | return nullptr; | |||
1530 | ||||
1531 | return Pos->second; | |||
1532 | } | |||
1533 | ||||
1534 | void | |||
1535 | ASTContext::setInstantiatedFromUsingDecl(NamedDecl *Inst, NamedDecl *Pattern) { | |||
1536 | assert((isa<UsingDecl>(Pattern) ||(static_cast <bool> ((isa<UsingDecl>(Pattern) || isa <UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl >(Pattern)) && "pattern decl is not a using decl") ? void (0) : __assert_fail ("(isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern)) && \"pattern decl is not a using decl\"" , "clang/lib/AST/ASTContext.cpp", 1539, __extension__ __PRETTY_FUNCTION__ )) | |||
1537 | isa<UnresolvedUsingValueDecl>(Pattern) ||(static_cast <bool> ((isa<UsingDecl>(Pattern) || isa <UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl >(Pattern)) && "pattern decl is not a using decl") ? void (0) : __assert_fail ("(isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern)) && \"pattern decl is not a using decl\"" , "clang/lib/AST/ASTContext.cpp", 1539, __extension__ __PRETTY_FUNCTION__ )) | |||
1538 | isa<UnresolvedUsingTypenameDecl>(Pattern)) &&(static_cast <bool> ((isa<UsingDecl>(Pattern) || isa <UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl >(Pattern)) && "pattern decl is not a using decl") ? void (0) : __assert_fail ("(isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern)) && \"pattern decl is not a using decl\"" , "clang/lib/AST/ASTContext.cpp", 1539, __extension__ __PRETTY_FUNCTION__ )) | |||
1539 | "pattern decl is not a using decl")(static_cast <bool> ((isa<UsingDecl>(Pattern) || isa <UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl >(Pattern)) && "pattern decl is not a using decl") ? void (0) : __assert_fail ("(isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern)) && \"pattern decl is not a using decl\"" , "clang/lib/AST/ASTContext.cpp", 1539, __extension__ __PRETTY_FUNCTION__ )); | |||
1540 | assert((isa<UsingDecl>(Inst) ||(static_cast <bool> ((isa<UsingDecl>(Inst) || isa <UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl >(Inst)) && "instantiation did not produce a using decl" ) ? void (0) : __assert_fail ("(isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) && \"instantiation did not produce a using decl\"" , "clang/lib/AST/ASTContext.cpp", 1543, __extension__ __PRETTY_FUNCTION__ )) | |||
1541 | isa<UnresolvedUsingValueDecl>(Inst) ||(static_cast <bool> ((isa<UsingDecl>(Inst) || isa <UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl >(Inst)) && "instantiation did not produce a using decl" ) ? void (0) : __assert_fail ("(isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) && \"instantiation did not produce a using decl\"" , "clang/lib/AST/ASTContext.cpp", 1543, __extension__ __PRETTY_FUNCTION__ )) | |||
1542 | isa<UnresolvedUsingTypenameDecl>(Inst)) &&(static_cast <bool> ((isa<UsingDecl>(Inst) || isa <UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl >(Inst)) && "instantiation did not produce a using decl" ) ? void (0) : __assert_fail ("(isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) && \"instantiation did not produce a using decl\"" , "clang/lib/AST/ASTContext.cpp", 1543, __extension__ __PRETTY_FUNCTION__ )) | |||
1543 | "instantiation did not produce a using decl")(static_cast <bool> ((isa<UsingDecl>(Inst) || isa <UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl >(Inst)) && "instantiation did not produce a using decl" ) ? void (0) : __assert_fail ("(isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) && \"instantiation did not produce a using decl\"" , "clang/lib/AST/ASTContext.cpp", 1543, __extension__ __PRETTY_FUNCTION__ )); | |||
1544 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists")(static_cast <bool> (!InstantiatedFromUsingDecl[Inst] && "pattern already exists") ? void (0) : __assert_fail ("!InstantiatedFromUsingDecl[Inst] && \"pattern already exists\"" , "clang/lib/AST/ASTContext.cpp", 1544, __extension__ __PRETTY_FUNCTION__ )); | |||
1545 | InstantiatedFromUsingDecl[Inst] = Pattern; | |||
1546 | } | |||
1547 | ||||
1548 | UsingEnumDecl * | |||
1549 | ASTContext::getInstantiatedFromUsingEnumDecl(UsingEnumDecl *UUD) { | |||
1550 | auto Pos = InstantiatedFromUsingEnumDecl.find(UUD); | |||
1551 | if (Pos == InstantiatedFromUsingEnumDecl.end()) | |||
1552 | return nullptr; | |||
1553 | ||||
1554 | return Pos->second; | |||
1555 | } | |||
1556 | ||||
1557 | void ASTContext::setInstantiatedFromUsingEnumDecl(UsingEnumDecl *Inst, | |||
1558 | UsingEnumDecl *Pattern) { | |||
1559 | assert(!InstantiatedFromUsingEnumDecl[Inst] && "pattern already exists")(static_cast <bool> (!InstantiatedFromUsingEnumDecl[Inst ] && "pattern already exists") ? void (0) : __assert_fail ("!InstantiatedFromUsingEnumDecl[Inst] && \"pattern already exists\"" , "clang/lib/AST/ASTContext.cpp", 1559, __extension__ __PRETTY_FUNCTION__ )); | |||
1560 | InstantiatedFromUsingEnumDecl[Inst] = Pattern; | |||
1561 | } | |||
1562 | ||||
1563 | UsingShadowDecl * | |||
1564 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { | |||
1565 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos | |||
1566 | = InstantiatedFromUsingShadowDecl.find(Inst); | |||
1567 | if (Pos == InstantiatedFromUsingShadowDecl.end()) | |||
1568 | return nullptr; | |||
1569 | ||||
1570 | return Pos->second; | |||
1571 | } | |||
1572 | ||||
1573 | void | |||
1574 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, | |||
1575 | UsingShadowDecl *Pattern) { | |||
1576 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists")(static_cast <bool> (!InstantiatedFromUsingShadowDecl[Inst ] && "pattern already exists") ? void (0) : __assert_fail ("!InstantiatedFromUsingShadowDecl[Inst] && \"pattern already exists\"" , "clang/lib/AST/ASTContext.cpp", 1576, __extension__ __PRETTY_FUNCTION__ )); | |||
1577 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; | |||
1578 | } | |||
1579 | ||||
1580 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { | |||
1581 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos | |||
1582 | = InstantiatedFromUnnamedFieldDecl.find(Field); | |||
1583 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) | |||
1584 | return nullptr; | |||
1585 | ||||
1586 | return Pos->second; | |||
1587 | } | |||
1588 | ||||
1589 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, | |||
1590 | FieldDecl *Tmpl) { | |||
1591 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed")(static_cast <bool> (!Inst->getDeclName() && "Instantiated field decl is not unnamed") ? void (0) : __assert_fail ("!Inst->getDeclName() && \"Instantiated field decl is not unnamed\"" , "clang/lib/AST/ASTContext.cpp", 1591, __extension__ __PRETTY_FUNCTION__ )); | |||
1592 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed")(static_cast <bool> (!Tmpl->getDeclName() && "Template field decl is not unnamed") ? void (0) : __assert_fail ("!Tmpl->getDeclName() && \"Template field decl is not unnamed\"" , "clang/lib/AST/ASTContext.cpp", 1592, __extension__ __PRETTY_FUNCTION__ )); | |||
1593 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&(static_cast <bool> (!InstantiatedFromUnnamedFieldDecl[ Inst] && "Already noted what unnamed field was instantiated from" ) ? void (0) : __assert_fail ("!InstantiatedFromUnnamedFieldDecl[Inst] && \"Already noted what unnamed field was instantiated from\"" , "clang/lib/AST/ASTContext.cpp", 1594, __extension__ __PRETTY_FUNCTION__ )) | |||
1594 | "Already noted what unnamed field was instantiated from")(static_cast <bool> (!InstantiatedFromUnnamedFieldDecl[ Inst] && "Already noted what unnamed field was instantiated from" ) ? void (0) : __assert_fail ("!InstantiatedFromUnnamedFieldDecl[Inst] && \"Already noted what unnamed field was instantiated from\"" , "clang/lib/AST/ASTContext.cpp", 1594, __extension__ __PRETTY_FUNCTION__ )); | |||
1595 | ||||
1596 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; | |||
1597 | } | |||
1598 | ||||
1599 | ASTContext::overridden_cxx_method_iterator | |||
1600 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { | |||
1601 | return overridden_methods(Method).begin(); | |||
1602 | } | |||
1603 | ||||
1604 | ASTContext::overridden_cxx_method_iterator | |||
1605 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { | |||
1606 | return overridden_methods(Method).end(); | |||
1607 | } | |||
1608 | ||||
1609 | unsigned | |||
1610 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { | |||
1611 | auto Range = overridden_methods(Method); | |||
1612 | return Range.end() - Range.begin(); | |||
1613 | } | |||
1614 | ||||
1615 | ASTContext::overridden_method_range | |||
1616 | ASTContext::overridden_methods(const CXXMethodDecl *Method) const { | |||
1617 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos = | |||
1618 | OverriddenMethods.find(Method->getCanonicalDecl()); | |||
1619 | if (Pos == OverriddenMethods.end()) | |||
1620 | return overridden_method_range(nullptr, nullptr); | |||
1621 | return overridden_method_range(Pos->second.begin(), Pos->second.end()); | |||
1622 | } | |||
1623 | ||||
1624 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, | |||
1625 | const CXXMethodDecl *Overridden) { | |||
1626 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl())(static_cast <bool> (Method->isCanonicalDecl() && Overridden->isCanonicalDecl()) ? void (0) : __assert_fail ("Method->isCanonicalDecl() && Overridden->isCanonicalDecl()" , "clang/lib/AST/ASTContext.cpp", 1626, __extension__ __PRETTY_FUNCTION__ )); | |||
1627 | OverriddenMethods[Method].push_back(Overridden); | |||
1628 | } | |||
1629 | ||||
1630 | void ASTContext::getOverriddenMethods( | |||
1631 | const NamedDecl *D, | |||
1632 | SmallVectorImpl<const NamedDecl *> &Overridden) const { | |||
1633 | assert(D)(static_cast <bool> (D) ? void (0) : __assert_fail ("D" , "clang/lib/AST/ASTContext.cpp", 1633, __extension__ __PRETTY_FUNCTION__ )); | |||
1634 | ||||
1635 | if (const auto *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { | |||
1636 | Overridden.append(overridden_methods_begin(CXXMethod), | |||
1637 | overridden_methods_end(CXXMethod)); | |||
1638 | return; | |||
1639 | } | |||
1640 | ||||
1641 | const auto *Method = dyn_cast<ObjCMethodDecl>(D); | |||
1642 | if (!Method) | |||
1643 | return; | |||
1644 | ||||
1645 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; | |||
1646 | Method->getOverriddenMethods(OverDecls); | |||
1647 | Overridden.append(OverDecls.begin(), OverDecls.end()); | |||
1648 | } | |||
1649 | ||||
1650 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { | |||
1651 | assert(!Import->getNextLocalImport() &&(static_cast <bool> (!Import->getNextLocalImport() && "Import declaration already in the chain") ? void (0) : __assert_fail ("!Import->getNextLocalImport() && \"Import declaration already in the chain\"" , "clang/lib/AST/ASTContext.cpp", 1652, __extension__ __PRETTY_FUNCTION__ )) | |||
1652 | "Import declaration already in the chain")(static_cast <bool> (!Import->getNextLocalImport() && "Import declaration already in the chain") ? void (0) : __assert_fail ("!Import->getNextLocalImport() && \"Import declaration already in the chain\"" , "clang/lib/AST/ASTContext.cpp", 1652, __extension__ __PRETTY_FUNCTION__ )); | |||
1653 | assert(!Import->isFromASTFile() && "Non-local import declaration")(static_cast <bool> (!Import->isFromASTFile() && "Non-local import declaration") ? void (0) : __assert_fail ( "!Import->isFromASTFile() && \"Non-local import declaration\"" , "clang/lib/AST/ASTContext.cpp", 1653, __extension__ __PRETTY_FUNCTION__ )); | |||
1654 | if (!FirstLocalImport) { | |||
1655 | FirstLocalImport = Import; | |||
1656 | LastLocalImport = Import; | |||
1657 | return; | |||
1658 | } | |||
1659 | ||||
1660 | LastLocalImport->setNextLocalImport(Import); | |||
1661 | LastLocalImport = Import; | |||
1662 | } | |||
1663 | ||||
1664 | //===----------------------------------------------------------------------===// | |||
1665 | // Type Sizing and Analysis | |||
1666 | //===----------------------------------------------------------------------===// | |||
1667 | ||||
1668 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified | |||
1669 | /// scalar floating point type. | |||
1670 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { | |||
1671 | switch (T->castAs<BuiltinType>()->getKind()) { | |||
1672 | default: | |||
1673 | llvm_unreachable("Not a floating point type!")::llvm::llvm_unreachable_internal("Not a floating point type!" , "clang/lib/AST/ASTContext.cpp", 1673); | |||
1674 | case BuiltinType::BFloat16: | |||
1675 | return Target->getBFloat16Format(); | |||
1676 | case BuiltinType::Float16: | |||
1677 | return Target->getHalfFormat(); | |||
1678 | case BuiltinType::Half: | |||
1679 | // For HLSL, when the native half type is disabled, half will be treat as | |||
1680 | // float. | |||
1681 | if (getLangOpts().HLSL) | |||
1682 | if (getLangOpts().NativeHalfType) | |||
1683 | return Target->getHalfFormat(); | |||
1684 | else | |||
1685 | return Target->getFloatFormat(); | |||
1686 | else | |||
1687 | return Target->getHalfFormat(); | |||
1688 | case BuiltinType::Float: return Target->getFloatFormat(); | |||
1689 | case BuiltinType::Double: return Target->getDoubleFormat(); | |||
1690 | case BuiltinType::Ibm128: | |||
1691 | return Target->getIbm128Format(); | |||
1692 | case BuiltinType::LongDouble: | |||
1693 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice) | |||
1694 | return AuxTarget->getLongDoubleFormat(); | |||
1695 | return Target->getLongDoubleFormat(); | |||
1696 | case BuiltinType::Float128: | |||
1697 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice) | |||
1698 | return AuxTarget->getFloat128Format(); | |||
1699 | return Target->getFloat128Format(); | |||
1700 | } | |||
1701 | } | |||
1702 | ||||
1703 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const { | |||
1704 | unsigned Align = Target->getCharWidth(); | |||
1705 | ||||
1706 | bool UseAlignAttrOnly = false; | |||
1707 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { | |||
1708 | Align = AlignFromAttr; | |||
1709 | ||||
1710 | // __attribute__((aligned)) can increase or decrease alignment | |||
1711 | // *except* on a struct or struct member, where it only increases | |||
1712 | // alignment unless 'packed' is also specified. | |||
1713 | // | |||
1714 | // It is an error for alignas to decrease alignment, so we can | |||
1715 | // ignore that possibility; Sema should diagnose it. | |||
1716 | if (isa<FieldDecl>(D)) { | |||
1717 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || | |||
1718 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); | |||
1719 | } else { | |||
1720 | UseAlignAttrOnly = true; | |||
1721 | } | |||
1722 | } | |||
1723 | else if (isa<FieldDecl>(D)) | |||
1724 | UseAlignAttrOnly = | |||
1725 | D->hasAttr<PackedAttr>() || | |||
1726 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); | |||
1727 | ||||
1728 | // If we're using the align attribute only, just ignore everything | |||
1729 | // else about the declaration and its type. | |||
1730 | if (UseAlignAttrOnly) { | |||
1731 | // do nothing | |||
1732 | } else if (const auto *VD = dyn_cast<ValueDecl>(D)) { | |||
1733 | QualType T = VD->getType(); | |||
1734 | if (const auto *RT = T->getAs<ReferenceType>()) { | |||
1735 | if (ForAlignof) | |||
1736 | T = RT->getPointeeType(); | |||
1737 | else | |||
1738 | T = getPointerType(RT->getPointeeType()); | |||
1739 | } | |||
1740 | QualType BaseT = getBaseElementType(T); | |||
1741 | if (T->isFunctionType()) | |||
1742 | Align = getTypeInfoImpl(T.getTypePtr()).Align; | |||
1743 | else if (!BaseT->isIncompleteType()) { | |||
1744 | // Adjust alignments of declarations with array type by the | |||
1745 | // large-array alignment on the target. | |||
1746 | if (const ArrayType *arrayType = getAsArrayType(T)) { | |||
1747 | unsigned MinWidth = Target->getLargeArrayMinWidth(); | |||
1748 | if (!ForAlignof && MinWidth) { | |||
1749 | if (isa<VariableArrayType>(arrayType)) | |||
1750 | Align = std::max(Align, Target->getLargeArrayAlign()); | |||
1751 | else if (isa<ConstantArrayType>(arrayType) && | |||
1752 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) | |||
1753 | Align = std::max(Align, Target->getLargeArrayAlign()); | |||
1754 | } | |||
1755 | } | |||
1756 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); | |||
1757 | if (BaseT.getQualifiers().hasUnaligned()) | |||
1758 | Align = Target->getCharWidth(); | |||
1759 | if (const auto *VD = dyn_cast<VarDecl>(D)) { | |||
1760 | if (VD->hasGlobalStorage() && !ForAlignof) { | |||
1761 | uint64_t TypeSize = getTypeSize(T.getTypePtr()); | |||
1762 | Align = std::max(Align, getTargetInfo().getMinGlobalAlign(TypeSize)); | |||
1763 | } | |||
1764 | } | |||
1765 | } | |||
1766 | ||||
1767 | // Fields can be subject to extra alignment constraints, like if | |||
1768 | // the field is packed, the struct is packed, or the struct has a | |||
1769 | // a max-field-alignment constraint (#pragma pack). So calculate | |||
1770 | // the actual alignment of the field within the struct, and then | |||
1771 | // (as we're expected to) constrain that by the alignment of the type. | |||
1772 | if (const auto *Field = dyn_cast<FieldDecl>(VD)) { | |||
1773 | const RecordDecl *Parent = Field->getParent(); | |||
1774 | // We can only produce a sensible answer if the record is valid. | |||
1775 | if (!Parent->isInvalidDecl()) { | |||
1776 | const ASTRecordLayout &Layout = getASTRecordLayout(Parent); | |||
1777 | ||||
1778 | // Start with the record's overall alignment. | |||
1779 | unsigned FieldAlign = toBits(Layout.getAlignment()); | |||
1780 | ||||
1781 | // Use the GCD of that and the offset within the record. | |||
1782 | uint64_t Offset = Layout.getFieldOffset(Field->getFieldIndex()); | |||
1783 | if (Offset > 0) { | |||
1784 | // Alignment is always a power of 2, so the GCD will be a power of 2, | |||
1785 | // which means we get to do this crazy thing instead of Euclid's. | |||
1786 | uint64_t LowBitOfOffset = Offset & (~Offset + 1); | |||
1787 | if (LowBitOfOffset < FieldAlign) | |||
1788 | FieldAlign = static_cast<unsigned>(LowBitOfOffset); | |||
1789 | } | |||
1790 | ||||
1791 | Align = std::min(Align, FieldAlign); | |||
1792 | } | |||
1793 | } | |||
1794 | } | |||
1795 | ||||
1796 | // Some targets have hard limitation on the maximum requestable alignment in | |||
1797 | // aligned attribute for static variables. | |||
1798 | const unsigned MaxAlignedAttr = getTargetInfo().getMaxAlignedAttribute(); | |||
1799 | const auto *VD = dyn_cast<VarDecl>(D); | |||
1800 | if (MaxAlignedAttr && VD && VD->getStorageClass() == SC_Static) | |||
1801 | Align = std::min(Align, MaxAlignedAttr); | |||
1802 | ||||
1803 | return toCharUnitsFromBits(Align); | |||
1804 | } | |||
1805 | ||||
1806 | CharUnits ASTContext::getExnObjectAlignment() const { | |||
1807 | return toCharUnitsFromBits(Target->getExnObjectAlignment()); | |||
1808 | } | |||
1809 | ||||
1810 | // getTypeInfoDataSizeInChars - Return the size of a type, in | |||
1811 | // chars. If the type is a record, its data size is returned. This is | |||
1812 | // the size of the memcpy that's performed when assigning this type | |||
1813 | // using a trivial copy/move assignment operator. | |||
1814 | TypeInfoChars ASTContext::getTypeInfoDataSizeInChars(QualType T) const { | |||
1815 | TypeInfoChars Info = getTypeInfoInChars(T); | |||
1816 | ||||
1817 | // In C++, objects can sometimes be allocated into the tail padding | |||
1818 | // of a base-class subobject. We decide whether that's possible | |||
1819 | // during class layout, so here we can just trust the layout results. | |||
1820 | if (getLangOpts().CPlusPlus) { | |||
1821 | if (const auto *RT = T->getAs<RecordType>()) { | |||
1822 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); | |||
1823 | Info.Width = layout.getDataSize(); | |||
1824 | } | |||
1825 | } | |||
1826 | ||||
1827 | return Info; | |||
1828 | } | |||
1829 | ||||
1830 | /// getConstantArrayInfoInChars - Performing the computation in CharUnits | |||
1831 | /// instead of in bits prevents overflowing the uint64_t for some large arrays. | |||
1832 | TypeInfoChars | |||
1833 | static getConstantArrayInfoInChars(const ASTContext &Context, | |||
1834 | const ConstantArrayType *CAT) { | |||
1835 | TypeInfoChars EltInfo = Context.getTypeInfoInChars(CAT->getElementType()); | |||
1836 | uint64_t Size = CAT->getSize().getZExtValue(); | |||
1837 | assert((Size == 0 || static_cast<uint64_t>(EltInfo.Width.getQuantity()) <=(static_cast <bool> ((Size == 0 || static_cast<uint64_t >(EltInfo.Width.getQuantity()) <= (uint64_t)(-1)/Size) && "Overflow in array type char size evaluation") ? void (0) : __assert_fail ("(Size == 0 || static_cast<uint64_t>(EltInfo.Width.getQuantity()) <= (uint64_t)(-1)/Size) && \"Overflow in array type char size evaluation\"" , "clang/lib/AST/ASTContext.cpp", 1839, __extension__ __PRETTY_FUNCTION__ )) | |||
1838 | (uint64_t)(-1)/Size) &&(static_cast <bool> ((Size == 0 || static_cast<uint64_t >(EltInfo.Width.getQuantity()) <= (uint64_t)(-1)/Size) && "Overflow in array type char size evaluation") ? void (0) : __assert_fail ("(Size == 0 || static_cast<uint64_t>(EltInfo.Width.getQuantity()) <= (uint64_t)(-1)/Size) && \"Overflow in array type char size evaluation\"" , "clang/lib/AST/ASTContext.cpp", 1839, __extension__ __PRETTY_FUNCTION__ )) | |||
1839 | "Overflow in array type char size evaluation")(static_cast <bool> ((Size == 0 || static_cast<uint64_t >(EltInfo.Width.getQuantity()) <= (uint64_t)(-1)/Size) && "Overflow in array type char size evaluation") ? void (0) : __assert_fail ("(Size == 0 || static_cast<uint64_t>(EltInfo.Width.getQuantity()) <= (uint64_t)(-1)/Size) && \"Overflow in array type char size evaluation\"" , "clang/lib/AST/ASTContext.cpp", 1839, __extension__ __PRETTY_FUNCTION__ )); | |||
1840 | uint64_t Width = EltInfo.Width.getQuantity() * Size; | |||
1841 | unsigned Align = EltInfo.Align.getQuantity(); | |||
1842 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() || | |||
1843 | Context.getTargetInfo().getPointerWidth(LangAS::Default) == 64) | |||
1844 | Width = llvm::alignTo(Width, Align); | |||
1845 | return TypeInfoChars(CharUnits::fromQuantity(Width), | |||
1846 | CharUnits::fromQuantity(Align), | |||
1847 | EltInfo.AlignRequirement); | |||
1848 | } | |||
1849 | ||||
1850 | TypeInfoChars ASTContext::getTypeInfoInChars(const Type *T) const { | |||
1851 | if (const auto *CAT = dyn_cast<ConstantArrayType>(T)) | |||
1852 | return getConstantArrayInfoInChars(*this, CAT); | |||
1853 | TypeInfo Info = getTypeInfo(T); | |||
1854 | return TypeInfoChars(toCharUnitsFromBits(Info.Width), | |||
1855 | toCharUnitsFromBits(Info.Align), Info.AlignRequirement); | |||
1856 | } | |||
1857 | ||||
1858 | TypeInfoChars ASTContext::getTypeInfoInChars(QualType T) const { | |||
1859 | return getTypeInfoInChars(T.getTypePtr()); | |||
1860 | } | |||
1861 | ||||
1862 | bool ASTContext::isPromotableIntegerType(QualType T) const { | |||
1863 | // HLSL doesn't promote all small integer types to int, it | |||
1864 | // just uses the rank-based promotion rules for all types. | |||
1865 | if (getLangOpts().HLSL) | |||
1866 | return false; | |||
1867 | ||||
1868 | if (const auto *BT = T->getAs<BuiltinType>()) | |||
1869 | switch (BT->getKind()) { | |||
1870 | case BuiltinType::Bool: | |||
1871 | case BuiltinType::Char_S: | |||
1872 | case BuiltinType::Char_U: | |||
1873 | case BuiltinType::SChar: | |||
1874 | case BuiltinType::UChar: | |||
1875 | case BuiltinType::Short: | |||
1876 | case BuiltinType::UShort: | |||
1877 | case BuiltinType::WChar_S: | |||
1878 | case BuiltinType::WChar_U: | |||
1879 | case BuiltinType::Char8: | |||
1880 | case BuiltinType::Char16: | |||
1881 | case BuiltinType::Char32: | |||
1882 | return true; | |||
1883 | default: | |||
1884 | return false; | |||
1885 | } | |||
1886 | ||||
1887 | // Enumerated types are promotable to their compatible integer types | |||
1888 | // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2). | |||
1889 | if (const auto *ET = T->getAs<EnumType>()) { | |||
1890 | if (T->isDependentType() || ET->getDecl()->getPromotionType().isNull() || | |||
1891 | ET->getDecl()->isScoped()) | |||
1892 | return false; | |||
1893 | ||||
1894 | return true; | |||
1895 | } | |||
1896 | ||||
1897 | return false; | |||
1898 | } | |||
1899 | ||||
1900 | bool ASTContext::isAlignmentRequired(const Type *T) const { | |||
1901 | return getTypeInfo(T).AlignRequirement != AlignRequirementKind::None; | |||
1902 | } | |||
1903 | ||||
1904 | bool ASTContext::isAlignmentRequired(QualType T) const { | |||
1905 | return isAlignmentRequired(T.getTypePtr()); | |||
1906 | } | |||
1907 | ||||
1908 | unsigned ASTContext::getTypeAlignIfKnown(QualType T, | |||
1909 | bool NeedsPreferredAlignment) const { | |||
1910 | // An alignment on a typedef overrides anything else. | |||
1911 | if (const auto *TT = T->getAs<TypedefType>()) | |||
1912 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) | |||
1913 | return Align; | |||
1914 | ||||
1915 | // If we have an (array of) complete type, we're done. | |||
1916 | T = getBaseElementType(T); | |||
1917 | if (!T->isIncompleteType()) | |||
1918 | return NeedsPreferredAlignment ? getPreferredTypeAlign(T) : getTypeAlign(T); | |||
1919 | ||||
1920 | // If we had an array type, its element type might be a typedef | |||
1921 | // type with an alignment attribute. | |||
1922 | if (const auto *TT = T->getAs<TypedefType>()) | |||
1923 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) | |||
1924 | return Align; | |||
1925 | ||||
1926 | // Otherwise, see if the declaration of the type had an attribute. | |||
1927 | if (const auto *TT = T->getAs<TagType>()) | |||
1928 | return TT->getDecl()->getMaxAlignment(); | |||
1929 | ||||
1930 | return 0; | |||
1931 | } | |||
1932 | ||||
1933 | TypeInfo ASTContext::getTypeInfo(const Type *T) const { | |||
1934 | TypeInfoMap::iterator I = MemoizedTypeInfo.find(T); | |||
1935 | if (I != MemoizedTypeInfo.end()) | |||
1936 | return I->second; | |||
1937 | ||||
1938 | // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup. | |||
1939 | TypeInfo TI = getTypeInfoImpl(T); | |||
1940 | MemoizedTypeInfo[T] = TI; | |||
1941 | return TI; | |||
1942 | } | |||
1943 | ||||
1944 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This | |||
1945 | /// method does not work on incomplete types. | |||
1946 | /// | |||
1947 | /// FIXME: Pointers into different addr spaces could have different sizes and | |||
1948 | /// alignment requirements: getPointerInfo should take an AddrSpace, this | |||
1949 | /// should take a QualType, &c. | |||
1950 | TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { | |||
1951 | uint64_t Width = 0; | |||
1952 | unsigned Align = 8; | |||
1953 | AlignRequirementKind AlignRequirement = AlignRequirementKind::None; | |||
1954 | LangAS AS = LangAS::Default; | |||
1955 | switch (T->getTypeClass()) { | |||
1956 | #define TYPE(Class, Base) | |||
1957 | #define ABSTRACT_TYPE(Class, Base) | |||
1958 | #define NON_CANONICAL_TYPE(Class, Base) | |||
1959 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: | |||
1960 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \ | |||
1961 | case Type::Class: \ | |||
1962 | assert(!T->isDependentType() && "should not see dependent types here")(static_cast <bool> (!T->isDependentType() && "should not see dependent types here") ? void (0) : __assert_fail ("!T->isDependentType() && \"should not see dependent types here\"" , "clang/lib/AST/ASTContext.cpp", 1962, __extension__ __PRETTY_FUNCTION__ )); \ | |||
1963 | return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr()); | |||
1964 | #include "clang/AST/TypeNodes.inc" | |||
1965 | llvm_unreachable("Should not see dependent types")::llvm::llvm_unreachable_internal("Should not see dependent types" , "clang/lib/AST/ASTContext.cpp", 1965); | |||
1966 | ||||
1967 | case Type::FunctionNoProto: | |||
1968 | case Type::FunctionProto: | |||
1969 | // GCC extension: alignof(function) = 32 bits | |||
1970 | Width = 0; | |||
1971 | Align = 32; | |||
1972 | break; | |||
1973 | ||||
1974 | case Type::IncompleteArray: | |||
1975 | case Type::VariableArray: | |||
1976 | case Type::ConstantArray: { | |||
1977 | // Model non-constant sized arrays as size zero, but track the alignment. | |||
1978 | uint64_t Size = 0; | |||
1979 | if (const auto *CAT = dyn_cast<ConstantArrayType>(T)) | |||
1980 | Size = CAT->getSize().getZExtValue(); | |||
1981 | ||||
1982 | TypeInfo EltInfo = getTypeInfo(cast<ArrayType>(T)->getElementType()); | |||
1983 | assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) &&(static_cast <bool> ((Size == 0 || EltInfo.Width <= ( uint64_t)(-1) / Size) && "Overflow in array type bit size evaluation" ) ? void (0) : __assert_fail ("(Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && \"Overflow in array type bit size evaluation\"" , "clang/lib/AST/ASTContext.cpp", 1984, __extension__ __PRETTY_FUNCTION__ )) | |||
1984 | "Overflow in array type bit size evaluation")(static_cast <bool> ((Size == 0 || EltInfo.Width <= ( uint64_t)(-1) / Size) && "Overflow in array type bit size evaluation" ) ? void (0) : __assert_fail ("(Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && \"Overflow in array type bit size evaluation\"" , "clang/lib/AST/ASTContext.cpp", 1984, __extension__ __PRETTY_FUNCTION__ )); | |||
1985 | Width = EltInfo.Width * Size; | |||
1986 | Align = EltInfo.Align; | |||
1987 | AlignRequirement = EltInfo.AlignRequirement; | |||
1988 | if (!getTargetInfo().getCXXABI().isMicrosoft() || | |||
1989 | getTargetInfo().getPointerWidth(LangAS::Default) == 64) | |||
1990 | Width = llvm::alignTo(Width, Align); | |||
1991 | break; | |||
1992 | } | |||
1993 | ||||
1994 | case Type::ExtVector: | |||
1995 | case Type::Vector: { | |||
1996 | const auto *VT = cast<VectorType>(T); | |||
1997 | TypeInfo EltInfo = getTypeInfo(VT->getElementType()); | |||
1998 | Width = VT->isExtVectorBoolType() ? VT->getNumElements() | |||
1999 | : EltInfo.Width * VT->getNumElements(); | |||
2000 | // Enforce at least byte size and alignment. | |||
2001 | Width = std::max<unsigned>(8, Width); | |||
2002 | Align = std::max<unsigned>(8, Width); | |||
2003 | ||||
2004 | // If the alignment is not a power of 2, round up to the next power of 2. | |||
2005 | // This happens for non-power-of-2 length vectors. | |||
2006 | if (Align & (Align-1)) { | |||
2007 | Align = llvm::bit_ceil(Align); | |||
2008 | Width = llvm::alignTo(Width, Align); | |||
2009 | } | |||
2010 | // Adjust the alignment based on the target max. | |||
2011 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); | |||
2012 | if (TargetVectorAlign && TargetVectorAlign < Align) | |||
2013 | Align = TargetVectorAlign; | |||
2014 | if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector) | |||
2015 | // Adjust the alignment for fixed-length SVE vectors. This is important | |||
2016 | // for non-power-of-2 vector lengths. | |||
2017 | Align = 128; | |||
2018 | else if (VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector) | |||
2019 | // Adjust the alignment for fixed-length SVE predicates. | |||
2020 | Align = 16; | |||
2021 | else if (VT->getVectorKind() == VectorType::RVVFixedLengthDataVector) | |||
2022 | // Adjust the alignment for fixed-length RVV vectors. | |||
2023 | Align = 64; | |||
2024 | break; | |||
2025 | } | |||
2026 | ||||
2027 | case Type::ConstantMatrix: { | |||
2028 | const auto *MT = cast<ConstantMatrixType>(T); | |||
2029 | TypeInfo ElementInfo = getTypeInfo(MT->getElementType()); | |||
2030 | // The internal layout of a matrix value is implementation defined. | |||
2031 | // Initially be ABI compatible with arrays with respect to alignment and | |||
2032 | // size. | |||
2033 | Width = ElementInfo.Width * MT->getNumRows() * MT->getNumColumns(); | |||
2034 | Align = ElementInfo.Align; | |||
2035 | break; | |||
2036 | } | |||
2037 | ||||
2038 | case Type::Builtin: | |||
2039 | switch (cast<BuiltinType>(T)->getKind()) { | |||
2040 | default: llvm_unreachable("Unknown builtin type!")::llvm::llvm_unreachable_internal("Unknown builtin type!", "clang/lib/AST/ASTContext.cpp" , 2040); | |||
2041 | case BuiltinType::Void: | |||
2042 | // GCC extension: alignof(void) = 8 bits. | |||
2043 | Width = 0; | |||
2044 | Align = 8; | |||
2045 | break; | |||
2046 | case BuiltinType::Bool: | |||
2047 | Width = Target->getBoolWidth(); | |||
2048 | Align = Target->getBoolAlign(); | |||
2049 | break; | |||
2050 | case BuiltinType::Char_S: | |||
2051 | case BuiltinType::Char_U: | |||
2052 | case BuiltinType::UChar: | |||
2053 | case BuiltinType::SChar: | |||
2054 | case BuiltinType::Char8: | |||
2055 | Width = Target->getCharWidth(); | |||
2056 | Align = Target->getCharAlign(); | |||
2057 | break; | |||
2058 | case BuiltinType::WChar_S: | |||
2059 | case BuiltinType::WChar_U: | |||
2060 | Width = Target->getWCharWidth(); | |||
2061 | Align = Target->getWCharAlign(); | |||
2062 | break; | |||
2063 | case BuiltinType::Char16: | |||
2064 | Width = Target->getChar16Width(); | |||
2065 | Align = Target->getChar16Align(); | |||
2066 | break; | |||
2067 | case BuiltinType::Char32: | |||
2068 | Width = Target->getChar32Width(); | |||
2069 | Align = Target->getChar32Align(); | |||
2070 | break; | |||
2071 | case BuiltinType::UShort: | |||
2072 | case BuiltinType::Short: | |||
2073 | Width = Target->getShortWidth(); | |||
2074 | Align = Target->getShortAlign(); | |||
2075 | break; | |||
2076 | case BuiltinType::UInt: | |||
2077 | case BuiltinType::Int: | |||
2078 | Width = Target->getIntWidth(); | |||
2079 | Align = Target->getIntAlign(); | |||
2080 | break; | |||
2081 | case BuiltinType::ULong: | |||
2082 | case BuiltinType::Long: | |||
2083 | Width = Target->getLongWidth(); | |||
2084 | Align = Target->getLongAlign(); | |||
2085 | break; | |||
2086 | case BuiltinType::ULongLong: | |||
2087 | case BuiltinType::LongLong: | |||
2088 | Width = Target->getLongLongWidth(); | |||
2089 | Align = Target->getLongLongAlign(); | |||
2090 | break; | |||
2091 | case BuiltinType::Int128: | |||
2092 | case BuiltinType::UInt128: | |||
2093 | Width = 128; | |||
2094 | Align = Target->getInt128Align(); | |||
2095 | break; | |||
2096 | case BuiltinType::ShortAccum: | |||
2097 | case BuiltinType::UShortAccum: | |||
2098 | case BuiltinType::SatShortAccum: | |||
2099 | case BuiltinType::SatUShortAccum: | |||
2100 | Width = Target->getShortAccumWidth(); | |||
2101 | Align = Target->getShortAccumAlign(); | |||
2102 | break; | |||
2103 | case BuiltinType::Accum: | |||
2104 | case BuiltinType::UAccum: | |||
2105 | case BuiltinType::SatAccum: | |||
2106 | case BuiltinType::SatUAccum: | |||
2107 | Width = Target->getAccumWidth(); | |||
2108 | Align = Target->getAccumAlign(); | |||
2109 | break; | |||
2110 | case BuiltinType::LongAccum: | |||
2111 | case BuiltinType::ULongAccum: | |||
2112 | case BuiltinType::SatLongAccum: | |||
2113 | case BuiltinType::SatULongAccum: | |||
2114 | Width = Target->getLongAccumWidth(); | |||
2115 | Align = Target->getLongAccumAlign(); | |||
2116 | break; | |||
2117 | case BuiltinType::ShortFract: | |||
2118 | case BuiltinType::UShortFract: | |||
2119 | case BuiltinType::SatShortFract: | |||
2120 | case BuiltinType::SatUShortFract: | |||
2121 | Width = Target->getShortFractWidth(); | |||
2122 | Align = Target->getShortFractAlign(); | |||
2123 | break; | |||
2124 | case BuiltinType::Fract: | |||
2125 | case BuiltinType::UFract: | |||
2126 | case BuiltinType::SatFract: | |||
2127 | case BuiltinType::SatUFract: | |||
2128 | Width = Target->getFractWidth(); | |||
2129 | Align = Target->getFractAlign(); | |||
2130 | break; | |||
2131 | case BuiltinType::LongFract: | |||
2132 | case BuiltinType::ULongFract: | |||
2133 | case BuiltinType::SatLongFract: | |||
2134 | case BuiltinType::SatULongFract: | |||
2135 | Width = Target->getLongFractWidth(); | |||
2136 | Align = Target->getLongFractAlign(); | |||
2137 | break; | |||
2138 | case BuiltinType::BFloat16: | |||
2139 | if (Target->hasBFloat16Type()) { | |||
2140 | Width = Target->getBFloat16Width(); | |||
2141 | Align = Target->getBFloat16Align(); | |||
2142 | } else if ((getLangOpts().SYCLIsDevice || | |||
2143 | (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice)) && | |||
2144 | AuxTarget->hasBFloat16Type()) { | |||
2145 | Width = AuxTarget->getBFloat16Width(); | |||
2146 | Align = AuxTarget->getBFloat16Align(); | |||
2147 | } | |||
2148 | break; | |||
2149 | case BuiltinType::Float16: | |||
2150 | case BuiltinType::Half: | |||
2151 | if (Target->hasFloat16Type() || !getLangOpts().OpenMP || | |||
2152 | !getLangOpts().OpenMPIsDevice) { | |||
2153 | Width = Target->getHalfWidth(); | |||
2154 | Align = Target->getHalfAlign(); | |||
2155 | } else { | |||
2156 | assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&(static_cast <bool> (getLangOpts().OpenMP && getLangOpts ().OpenMPIsDevice && "Expected OpenMP device compilation." ) ? void (0) : __assert_fail ("getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && \"Expected OpenMP device compilation.\"" , "clang/lib/AST/ASTContext.cpp", 2157, __extension__ __PRETTY_FUNCTION__ )) | |||
2157 | "Expected OpenMP device compilation.")(static_cast <bool> (getLangOpts().OpenMP && getLangOpts ().OpenMPIsDevice && "Expected OpenMP device compilation." ) ? void (0) : __assert_fail ("getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && \"Expected OpenMP device compilation.\"" , "clang/lib/AST/ASTContext.cpp", 2157, __extension__ __PRETTY_FUNCTION__ )); | |||
2158 | Width = AuxTarget->getHalfWidth(); | |||
2159 | Align = AuxTarget->getHalfAlign(); | |||
2160 | } | |||
2161 | break; | |||
2162 | case BuiltinType::Float: | |||
2163 | Width = Target->getFloatWidth(); | |||
2164 | Align = Target->getFloatAlign(); | |||
2165 | break; | |||
2166 | case BuiltinType::Double: | |||
2167 | Width = Target->getDoubleWidth(); | |||
2168 | Align = Target->getDoubleAlign(); | |||
2169 | break; | |||
2170 | case BuiltinType::Ibm128: | |||
2171 | Width = Target->getIbm128Width(); | |||
2172 | Align = Target->getIbm128Align(); | |||
2173 | break; | |||
2174 | case BuiltinType::LongDouble: | |||
2175 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && | |||
2176 | (Target->getLongDoubleWidth() != AuxTarget->getLongDoubleWidth() || | |||
2177 | Target->getLongDoubleAlign() != AuxTarget->getLongDoubleAlign())) { | |||
2178 | Width = AuxTarget->getLongDoubleWidth(); | |||
2179 | Align = AuxTarget->getLongDoubleAlign(); | |||
2180 | } else { | |||
2181 | Width = Target->getLongDoubleWidth(); | |||
2182 | Align = Target->getLongDoubleAlign(); | |||
2183 | } | |||
2184 | break; | |||
2185 | case BuiltinType::Float128: | |||
2186 | if (Target->hasFloat128Type() || !getLangOpts().OpenMP || | |||
2187 | !getLangOpts().OpenMPIsDevice) { | |||
2188 | Width = Target->getFloat128Width(); | |||
2189 | Align = Target->getFloat128Align(); | |||
2190 | } else { | |||
2191 | assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&(static_cast <bool> (getLangOpts().OpenMP && getLangOpts ().OpenMPIsDevice && "Expected OpenMP device compilation." ) ? void (0) : __assert_fail ("getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && \"Expected OpenMP device compilation.\"" , "clang/lib/AST/ASTContext.cpp", 2192, __extension__ __PRETTY_FUNCTION__ )) | |||
2192 | "Expected OpenMP device compilation.")(static_cast <bool> (getLangOpts().OpenMP && getLangOpts ().OpenMPIsDevice && "Expected OpenMP device compilation." ) ? void (0) : __assert_fail ("getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && \"Expected OpenMP device compilation.\"" , "clang/lib/AST/ASTContext.cpp", 2192, __extension__ __PRETTY_FUNCTION__ )); | |||
2193 | Width = AuxTarget->getFloat128Width(); | |||
2194 | Align = AuxTarget->getFloat128Align(); | |||
2195 | } | |||
2196 | break; | |||
2197 | case BuiltinType::NullPtr: | |||
2198 | // C++ 3.9.1p11: sizeof(nullptr_t) == sizeof(void*) | |||
2199 | Width = Target->getPointerWidth(LangAS::Default); | |||
2200 | Align = Target->getPointerAlign(LangAS::Default); | |||
2201 | break; | |||
2202 | case BuiltinType::ObjCId: | |||
2203 | case BuiltinType::ObjCClass: | |||
2204 | case BuiltinType::ObjCSel: | |||
2205 | Width = Target->getPointerWidth(LangAS::Default); | |||
2206 | Align = Target->getPointerAlign(LangAS::Default); | |||
2207 | break; | |||
2208 | case BuiltinType::OCLSampler: | |||
2209 | case BuiltinType::OCLEvent: | |||
2210 | case BuiltinType::OCLClkEvent: | |||
2211 | case BuiltinType::OCLQueue: | |||
2212 | case BuiltinType::OCLReserveID: | |||
2213 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | |||
2214 | case BuiltinType::Id: | |||
2215 | #include "clang/Basic/OpenCLImageTypes.def" | |||
2216 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | |||
2217 | case BuiltinType::Id: | |||
2218 | #include "clang/Basic/OpenCLExtensionTypes.def" | |||
2219 | AS = Target->getOpenCLTypeAddrSpace(getOpenCLTypeKind(T)); | |||
2220 | Width = Target->getPointerWidth(AS); | |||
2221 | Align = Target->getPointerAlign(AS); | |||
2222 | break; | |||
2223 | // The SVE types are effectively target-specific. The length of an | |||
2224 | // SVE_VECTOR_TYPE is only known at runtime, but it is always a multiple | |||
2225 | // of 128 bits. There is one predicate bit for each vector byte, so the | |||
2226 | // length of an SVE_PREDICATE_TYPE is always a multiple of 16 bits. | |||
2227 | // | |||
2228 | // Because the length is only known at runtime, we use a dummy value | |||
2229 | // of 0 for the static length. The alignment values are those defined | |||
2230 | // by the Procedure Call Standard for the Arm Architecture. | |||
2231 | #define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId, NumEls, ElBits, \ | |||
2232 | IsSigned, IsFP, IsBF) \ | |||
2233 | case BuiltinType::Id: \ | |||
2234 | Width = 0; \ | |||
2235 | Align = 128; \ | |||
2236 | break; | |||
2237 | #define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId, NumEls) \ | |||
2238 | case BuiltinType::Id: \ | |||
2239 | Width = 0; \ | |||
2240 | Align = 16; \ | |||
2241 | break; | |||
2242 | #define SVE_OPAQUE_TYPE(Name, MangledName, Id, SingletonId) \ | |||
2243 | case BuiltinType::Id: \ | |||
2244 | Width = 0; \ | |||
2245 | Align = 16; \ | |||
2246 | break; | |||
2247 | #include "clang/Basic/AArch64SVEACLETypes.def" | |||
2248 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ | |||
2249 | case BuiltinType::Id: \ | |||
2250 | Width = Size; \ | |||
2251 | Align = Size; \ | |||
2252 | break; | |||
2253 | #include "clang/Basic/PPCTypes.def" | |||
2254 | #define RVV_VECTOR_TYPE(Name, Id, SingletonId, ElKind, ElBits, NF, IsSigned, \ | |||
2255 | IsFP) \ | |||
2256 | case BuiltinType::Id: \ | |||
2257 | Width = 0; \ | |||
2258 | Align = ElBits; \ | |||
2259 | break; | |||
2260 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, ElKind) \ | |||
2261 | case BuiltinType::Id: \ | |||
2262 | Width = 0; \ | |||
2263 | Align = 8; \ | |||
2264 | break; | |||
2265 | #include "clang/Basic/RISCVVTypes.def" | |||
2266 | #define WASM_TYPE(Name, Id, SingletonId) \ | |||
2267 | case BuiltinType::Id: \ | |||
2268 | Width = 0; \ | |||
2269 | Align = 8; \ | |||
2270 | break; | |||
2271 | #include "clang/Basic/WebAssemblyReferenceTypes.def" | |||
2272 | } | |||
2273 | break; | |||
2274 | case Type::ObjCObjectPointer: | |||
2275 | Width = Target->getPointerWidth(LangAS::Default); | |||
2276 | Align = Target->getPointerAlign(LangAS::Default); | |||
2277 | break; | |||
2278 | case Type::BlockPointer: | |||
2279 | AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); | |||
2280 | Width = Target->getPointerWidth(AS); | |||
2281 | Align = Target->getPointerAlign(AS); | |||
2282 | break; | |||
2283 | case Type::LValueReference: | |||
2284 | case Type::RValueReference: | |||
2285 | // alignof and sizeof should never enter this code path here, so we go | |||
2286 | // the pointer route. | |||
2287 | AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); | |||
2288 | Width = Target->getPointerWidth(AS); | |||
2289 | Align = Target->getPointerAlign(AS); | |||
2290 | break; | |||
2291 | case Type::Pointer: | |||
2292 | AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); | |||
2293 | Width = Target->getPointerWidth(AS); | |||
2294 | Align = Target->getPointerAlign(AS); | |||
2295 | break; | |||
2296 | case Type::MemberPointer: { | |||
2297 | const auto *MPT = cast<MemberPointerType>(T); | |||
2298 | CXXABI::MemberPointerInfo MPI = ABI->getMemberPointerInfo(MPT); | |||
2299 | Width = MPI.Width; | |||
2300 | Align = MPI.Align; | |||
2301 | break; | |||
2302 | } | |||
2303 | case Type::Complex: { | |||
2304 | // Complex types have the same alignment as their elements, but twice the | |||
2305 | // size. | |||
2306 | TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType()); | |||
2307 | Width = EltInfo.Width * 2; | |||
2308 | Align = EltInfo.Align; | |||
2309 | break; | |||
2310 | } | |||
2311 | case Type::ObjCObject: | |||
2312 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); | |||
2313 | case Type::Adjusted: | |||
2314 | case Type::Decayed: | |||
2315 | return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr()); | |||
2316 | case Type::ObjCInterface: { | |||
2317 | const auto *ObjCI = cast<ObjCInterfaceType>(T); | |||
2318 | if (ObjCI->getDecl()->isInvalidDecl()) { | |||
2319 | Width = 8; | |||
2320 | Align = 8; | |||
2321 | break; | |||
2322 | } | |||
2323 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); | |||
2324 | Width = toBits(Layout.getSize()); | |||
2325 | Align = toBits(Layout.getAlignment()); | |||
2326 | break; | |||
2327 | } | |||
2328 | case Type::BitInt: { | |||
2329 | const auto *EIT = cast<BitIntType>(T); | |||
2330 | Align = std::clamp<unsigned>(llvm::PowerOf2Ceil(EIT->getNumBits()), | |||
2331 | getCharWidth(), Target->getLongLongAlign()); | |||
2332 | Width = llvm::alignTo(EIT->getNumBits(), Align); | |||
2333 | break; | |||
2334 | } | |||
2335 | case Type::Record: | |||
2336 | case Type::Enum: { | |||
2337 | const auto *TT = cast<TagType>(T); | |||
2338 | ||||
2339 | if (TT->getDecl()->isInvalidDecl()) { | |||
2340 | Width = 8; | |||
2341 | Align = 8; | |||
2342 | break; | |||
2343 | } | |||
2344 | ||||
2345 | if (const auto *ET = dyn_cast<EnumType>(TT)) { | |||
2346 | const EnumDecl *ED = ET->getDecl(); | |||
2347 | TypeInfo Info = | |||
2348 | getTypeInfo(ED->getIntegerType()->getUnqualifiedDesugaredType()); | |||
2349 | if (unsigned AttrAlign = ED->getMaxAlignment()) { | |||
2350 | Info.Align = AttrAlign; | |||
2351 | Info.AlignRequirement = AlignRequirementKind::RequiredByEnum; | |||
2352 | } | |||
2353 | return Info; | |||
2354 | } | |||
2355 | ||||
2356 | const auto *RT = cast<RecordType>(TT); | |||
2357 | const RecordDecl *RD = RT->getDecl(); | |||
2358 | const ASTRecordLayout &Layout = getASTRecordLayout(RD); | |||
2359 | Width = toBits(Layout.getSize()); | |||
2360 | Align = toBits(Layout.getAlignment()); | |||
2361 | AlignRequirement = RD->hasAttr<AlignedAttr>() | |||
2362 | ? AlignRequirementKind::RequiredByRecord | |||
2363 | : AlignRequirementKind::None; | |||
2364 | break; | |||
2365 | } | |||
2366 | ||||
2367 | case Type::SubstTemplateTypeParm: | |||
2368 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> | |||
2369 | getReplacementType().getTypePtr()); | |||
2370 | ||||
2371 | case Type::Auto: | |||
2372 | case Type::DeducedTemplateSpecialization: { | |||
2373 | const auto *A = cast<DeducedType>(T); | |||
2374 | assert(!A->getDeducedType().isNull() &&(static_cast <bool> (!A->getDeducedType().isNull() && "cannot request the size of an undeduced or dependent auto type" ) ? void (0) : __assert_fail ("!A->getDeducedType().isNull() && \"cannot request the size of an undeduced or dependent auto type\"" , "clang/lib/AST/ASTContext.cpp", 2375, __extension__ __PRETTY_FUNCTION__ )) | |||
2375 | "cannot request the size of an undeduced or dependent auto type")(static_cast <bool> (!A->getDeducedType().isNull() && "cannot request the size of an undeduced or dependent auto type" ) ? void (0) : __assert_fail ("!A->getDeducedType().isNull() && \"cannot request the size of an undeduced or dependent auto type\"" , "clang/lib/AST/ASTContext.cpp", 2375, __extension__ __PRETTY_FUNCTION__ )); | |||
2376 | return getTypeInfo(A->getDeducedType().getTypePtr()); | |||
2377 | } | |||
2378 | ||||
2379 | case Type::Paren: | |||
2380 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); | |||
2381 | ||||
2382 | case Type::MacroQualified: | |||
2383 | return getTypeInfo( | |||
2384 | cast<MacroQualifiedType>(T)->getUnderlyingType().getTypePtr()); | |||
2385 | ||||
2386 | case Type::ObjCTypeParam: | |||
2387 | return getTypeInfo(cast<ObjCTypeParamType>(T)->desugar().getTypePtr()); | |||
2388 | ||||
2389 | case Type::Using: | |||
2390 | return getTypeInfo(cast<UsingType>(T)->desugar().getTypePtr()); | |||
2391 | ||||
2392 | case Type::Typedef: { | |||
2393 | const auto *TT = cast<TypedefType>(T); | |||
2394 | TypeInfo Info = getTypeInfo(TT->desugar().getTypePtr()); | |||
2395 | // If the typedef has an aligned attribute on it, it overrides any computed | |||
2396 | // alignment we have. This violates the GCC documentation (which says that | |||
2397 | // attribute(aligned) can only round up) but matches its implementation. | |||
2398 | if (unsigned AttrAlign = TT->getDecl()->getMaxAlignment()) { | |||
2399 | Align = AttrAlign; | |||
2400 | AlignRequirement = AlignRequirementKind::RequiredByTypedef; | |||
2401 | } else { | |||
2402 | Align = Info.Align; | |||
2403 | AlignRequirement = Info.AlignRequirement; | |||
2404 | } | |||
2405 | Width = Info.Width; | |||
2406 | break; | |||
2407 | } | |||
2408 | ||||
2409 | case Type::Elaborated: | |||
2410 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); | |||
2411 | ||||
2412 | case Type::Attributed: | |||
2413 | return getTypeInfo( | |||
2414 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); | |||
2415 | ||||
2416 | case Type::BTFTagAttributed: | |||
2417 | return getTypeInfo( | |||
2418 | cast<BTFTagAttributedType>(T)->getWrappedType().getTypePtr()); | |||
2419 | ||||
2420 | case Type::Atomic: { | |||
2421 | // Start with the base type information. | |||
2422 | TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType()); | |||
2423 | Width = Info.Width; | |||
2424 | Align = Info.Align; | |||
2425 | ||||
2426 | if (!Width) { | |||
2427 | // An otherwise zero-sized type should still generate an | |||
2428 | // atomic operation. | |||
2429 | Width = Target->getCharWidth(); | |||
2430 | assert(Align)(static_cast <bool> (Align) ? void (0) : __assert_fail ( "Align", "clang/lib/AST/ASTContext.cpp", 2430, __extension__ __PRETTY_FUNCTION__ )); | |||
2431 | } else if (Width <= Target->getMaxAtomicPromoteWidth()) { | |||
2432 | // If the size of the type doesn't exceed the platform's max | |||
2433 | // atomic promotion width, make the size and alignment more | |||
2434 | // favorable to atomic operations: | |||
2435 | ||||
2436 | // Round the size up to a power of 2. | |||
2437 | Width = llvm::bit_ceil(Width); | |||
2438 | ||||
2439 | // Set the alignment equal to the size. | |||
2440 | Align = static_cast<unsigned>(Width); | |||
2441 | } | |||
2442 | } | |||
2443 | break; | |||
2444 | ||||
2445 | case Type::Pipe: | |||
2446 | Width = Target->getPointerWidth(LangAS::opencl_global); | |||
2447 | Align = Target->getPointerAlign(LangAS::opencl_global); | |||
2448 | break; | |||
2449 | } | |||
2450 | ||||
2451 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2")(static_cast <bool> (llvm::isPowerOf2_32(Align) && "Alignment must be power of 2") ? void (0) : __assert_fail ( "llvm::isPowerOf2_32(Align) && \"Alignment must be power of 2\"" , "clang/lib/AST/ASTContext.cpp", 2451, __extension__ __PRETTY_FUNCTION__ )); | |||
2452 | return TypeInfo(Width, Align, AlignRequirement); | |||
2453 | } | |||
2454 | ||||
2455 | unsigned ASTContext::getTypeUnadjustedAlign(const Type *T) const { | |||
2456 | UnadjustedAlignMap::iterator I = MemoizedUnadjustedAlign.find(T); | |||
2457 | if (I != MemoizedUnadjustedAlign.end()) | |||
2458 | return I->second; | |||
2459 | ||||
2460 | unsigned UnadjustedAlign; | |||
2461 | if (const auto *RT = T->getAs<RecordType>()) { | |||
2462 | const RecordDecl *RD = RT->getDecl(); | |||
2463 | const ASTRecordLayout &Layout = getASTRecordLayout(RD); | |||
2464 | UnadjustedAlign = toBits(Layout.getUnadjustedAlignment()); | |||
2465 | } else if (const auto *ObjCI = T->getAs<ObjCInterfaceType>()) { | |||
2466 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); | |||
2467 | UnadjustedAlign = toBits(Layout.getUnadjustedAlignment()); | |||
2468 | } else { | |||
2469 | UnadjustedAlign = getTypeAlign(T->getUnqualifiedDesugaredType()); | |||
2470 | } | |||
2471 | ||||
2472 | MemoizedUnadjustedAlign[T] = UnadjustedAlign; | |||
2473 | return UnadjustedAlign; | |||
2474 | } | |||
2475 | ||||
2476 | unsigned ASTContext::getOpenMPDefaultSimdAlign(QualType T) const { | |||
2477 | unsigned SimdAlign = llvm::OpenMPIRBuilder::getOpenMPDefaultSimdAlign( | |||
2478 | getTargetInfo().getTriple(), Target->getTargetOpts().FeatureMap); | |||
2479 | return SimdAlign; | |||
2480 | } | |||
2481 | ||||
2482 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. | |||
2483 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { | |||
2484 | return CharUnits::fromQuantity(BitSize / getCharWidth()); | |||
2485 | } | |||
2486 | ||||
2487 | /// toBits - Convert a size in characters to a size in characters. | |||
2488 | int64_t ASTContext::toBits(CharUnits CharSize) const { | |||
2489 | return CharSize.getQuantity() * getCharWidth(); | |||
2490 | } | |||
2491 | ||||
2492 | /// getTypeSizeInChars - Return the size of the specified type, in characters. | |||
2493 | /// This method does not work on incomplete types. | |||
2494 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { | |||
2495 | return getTypeInfoInChars(T).Width; | |||
2496 | } | |||
2497 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { | |||
2498 | return getTypeInfoInChars(T).Width; | |||
2499 | } | |||
2500 | ||||
2501 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in | |||
2502 | /// characters. This method does not work on incomplete types. | |||
2503 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { | |||
2504 | return toCharUnitsFromBits(getTypeAlign(T)); | |||
2505 | } | |||
2506 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { | |||
2507 | return toCharUnitsFromBits(getTypeAlign(T)); | |||
2508 | } | |||
2509 | ||||
2510 | /// getTypeUnadjustedAlignInChars - Return the ABI-specified alignment of a | |||
2511 | /// type, in characters, before alignment adjustments. This method does | |||
2512 | /// not work on incomplete types. | |||
2513 | CharUnits ASTContext::getTypeUnadjustedAlignInChars(QualType T) const { | |||
2514 | return toCharUnitsFromBits(getTypeUnadjustedAlign(T)); | |||
2515 | } | |||
2516 | CharUnits ASTContext::getTypeUnadjustedAlignInChars(const Type *T) const { | |||
2517 | return toCharUnitsFromBits(getTypeUnadjustedAlign(T)); | |||
2518 | } | |||
2519 | ||||
2520 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified | |||
2521 | /// type for the current target in bits. This can be different than the ABI | |||
2522 | /// alignment in cases where it is beneficial for performance or backwards | |||
2523 | /// compatibility preserving to overalign a data type. (Note: despite the name, | |||
2524 | /// the preferred alignment is ABI-impacting, and not an optimization.) | |||
2525 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { | |||
2526 | TypeInfo TI = getTypeInfo(T); | |||
2527 | unsigned ABIAlign = TI.Align; | |||
2528 | ||||
2529 | T = T->getBaseElementTypeUnsafe(); | |||
2530 | ||||
2531 | // The preferred alignment of member pointers is that of a pointer. | |||
2532 | if (T->isMemberPointerType()) | |||
2533 | return getPreferredTypeAlign(getPointerDiffType().getTypePtr()); | |||
2534 | ||||
2535 | if (!Target->allowsLargerPreferedTypeAlignment()) | |||
2536 | return ABIAlign; | |||
2537 | ||||
2538 | if (const auto *RT = T->getAs<RecordType>()) { | |||
2539 | const RecordDecl *RD = RT->getDecl(); | |||
2540 | ||||
2541 | // When used as part of a typedef, or together with a 'packed' attribute, | |||
2542 | // the 'aligned' attribute can be used to decrease alignment. Note that the | |||
2543 | // 'packed' case is already taken into consideration when computing the | |||
2544 | // alignment, we only need to handle the typedef case here. | |||
2545 | if (TI.AlignRequirement == AlignRequirementKind::RequiredByTypedef || | |||
2546 | RD->isInvalidDecl()) | |||
2547 | return ABIAlign; | |||
2548 | ||||
2549 | unsigned PreferredAlign = static_cast<unsigned>( | |||
2550 | toBits(getASTRecordLayout(RD).PreferredAlignment)); | |||
2551 | assert(PreferredAlign >= ABIAlign &&(static_cast <bool> (PreferredAlign >= ABIAlign && "PreferredAlign should be at least as large as ABIAlign.") ? void (0) : __assert_fail ("PreferredAlign >= ABIAlign && \"PreferredAlign should be at least as large as ABIAlign.\"" , "clang/lib/AST/ASTContext.cpp", 2552, __extension__ __PRETTY_FUNCTION__ )) | |||
2552 | "PreferredAlign should be at least as large as ABIAlign.")(static_cast <bool> (PreferredAlign >= ABIAlign && "PreferredAlign should be at least as large as ABIAlign.") ? void (0) : __assert_fail ("PreferredAlign >= ABIAlign && \"PreferredAlign should be at least as large as ABIAlign.\"" , "clang/lib/AST/ASTContext.cpp", 2552, __extension__ __PRETTY_FUNCTION__ )); | |||
2553 | return PreferredAlign; | |||
2554 | } | |||
2555 | ||||
2556 | // Double (and, for targets supporting AIX `power` alignment, long double) and | |||
2557 | // long long should be naturally aligned (despite requiring less alignment) if | |||
2558 | // possible. | |||
2559 | if (const auto *CT = T->getAs<ComplexType>()) | |||
2560 | T = CT->getElementType().getTypePtr(); | |||
2561 | if (const auto *ET = T->getAs<EnumType>()) | |||
2562 | T = ET->getDecl()->getIntegerType().getTypePtr(); | |||
2563 | if (T->isSpecificBuiltinType(BuiltinType::Double) || | |||
2564 | T->isSpecificBuiltinType(BuiltinType::LongLong) || | |||
2565 | T->isSpecificBuiltinType(BuiltinType::ULongLong) || | |||
2566 | (T->isSpecificBuiltinType(BuiltinType::LongDouble) && | |||
2567 | Target->defaultsToAIXPowerAlignment())) | |||
2568 | // Don't increase the alignment if an alignment attribute was specified on a | |||
2569 | // typedef declaration. | |||
2570 | if (!TI.isAlignRequired()) | |||
2571 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); | |||
2572 | ||||
2573 | return ABIAlign; | |||
2574 | } | |||
2575 | ||||
2576 | /// getTargetDefaultAlignForAttributeAligned - Return the default alignment | |||
2577 | /// for __attribute__((aligned)) on this target, to be used if no alignment | |||
2578 | /// value is specified. | |||
2579 | unsigned ASTContext::getTargetDefaultAlignForAttributeAligned() const { | |||
2580 | return getTargetInfo().getDefaultAlignForAttributeAligned(); | |||
2581 | } | |||
2582 | ||||
2583 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given | |||
2584 | /// to a global variable of the specified type. | |||
2585 | unsigned ASTContext::getAlignOfGlobalVar(QualType T) const { | |||
2586 | uint64_t TypeSize = getTypeSize(T.getTypePtr()); | |||
2587 | return std::max(getPreferredTypeAlign(T), | |||
2588 | getTargetInfo().getMinGlobalAlign(TypeSize)); | |||
2589 | } | |||
2590 | ||||
2591 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that | |||
2592 | /// should be given to a global variable of the specified type. | |||
2593 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const { | |||
2594 | return toCharUnitsFromBits(getAlignOfGlobalVar(T)); | |||
2595 | } | |||
2596 | ||||
2597 | CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const { | |||
2598 | CharUnits Offset = CharUnits::Zero(); | |||
2599 | const ASTRecordLayout *Layout = &getASTRecordLayout(RD); | |||
2600 | while (const CXXRecordDecl *Base = Layout->getBaseSharingVBPtr()) { | |||
2601 | Offset += Layout->getBaseClassOffset(Base); | |||
2602 | Layout = &getASTRecordLayout(Base); | |||
2603 | } | |||
2604 | return Offset; | |||
2605 | } | |||
2606 | ||||
2607 | CharUnits ASTContext::getMemberPointerPathAdjustment(const APValue &MP) const { | |||
2608 | const ValueDecl *MPD = MP.getMemberPointerDecl(); | |||
2609 | CharUnits ThisAdjustment = CharUnits::Zero(); | |||
2610 | ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath(); | |||
2611 | bool DerivedMember = MP.isMemberPointerToDerivedMember(); | |||
2612 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext()); | |||
2613 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { | |||
2614 | const CXXRecordDecl *Base = RD; | |||
2615 | const CXXRecordDecl *Derived = Path[I]; | |||
2616 | if (DerivedMember) | |||
2617 | std::swap(Base, Derived); | |||
2618 | ThisAdjustment += getASTRecordLayout(Derived).getBaseClassOffset(Base); | |||
2619 | RD = Path[I]; | |||
2620 | } | |||
2621 | if (DerivedMember) | |||
2622 | ThisAdjustment = -ThisAdjustment; | |||
2623 | return ThisAdjustment; | |||
2624 | } | |||
2625 | ||||
2626 | /// DeepCollectObjCIvars - | |||
2627 | /// This routine first collects all declared, but not synthesized, ivars in | |||
2628 | /// super class and then collects all ivars, including those synthesized for | |||
2629 | /// current class. This routine is used for implementation of current class | |||
2630 | /// when all ivars, declared and synthesized are known. | |||
2631 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, | |||
2632 | bool leafClass, | |||
2633 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { | |||
2634 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) | |||
2635 | DeepCollectObjCIvars(SuperClass, false, Ivars); | |||
2636 | if (!leafClass) { | |||
2637 | llvm::append_range(Ivars, OI->ivars()); | |||
2638 | } else { | |||
2639 | auto *IDecl = const_cast<ObjCInterfaceDecl *>(OI); | |||
2640 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; | |||
2641 | Iv= Iv->getNextIvar()) | |||
2642 | Ivars.push_back(Iv); | |||
2643 | } | |||
2644 | } | |||
2645 | ||||
2646 | /// CollectInheritedProtocols - Collect all protocols in current class and | |||
2647 | /// those inherited by it. | |||
2648 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, | |||
2649 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { | |||
2650 | if (const auto *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { | |||
2651 | // We can use protocol_iterator here instead of | |||
2652 | // all_referenced_protocol_iterator since we are walking all categories. | |||
2653 | for (auto *Proto : OI->all_referenced_protocols()) { | |||
2654 | CollectInheritedProtocols(Proto, Protocols); | |||
2655 | } | |||
2656 | ||||
2657 | // Categories of this Interface. | |||
2658 | for (const auto *Cat : OI->visible_categories()) | |||
2659 | CollectInheritedProtocols(Cat, Protocols); | |||
2660 | ||||
2661 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) | |||
2662 | while (SD) { | |||
2663 | CollectInheritedProtocols(SD, Protocols); | |||
2664 | SD = SD->getSuperClass(); | |||
2665 | } | |||
2666 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { | |||
2667 | for (auto *Proto : OC->protocols()) { | |||
2668 | CollectInheritedProtocols(Proto, Protocols); | |||
2669 | } | |||
2670 | } else if (const auto *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { | |||
2671 | // Insert the protocol. | |||
2672 | if (!Protocols.insert( | |||
2673 | const_cast<ObjCProtocolDecl *>(OP->getCanonicalDecl())).second) | |||
2674 | return; | |||
2675 | ||||
2676 | for (auto *Proto : OP->protocols()) | |||
2677 | CollectInheritedProtocols(Proto, Protocols); | |||
2678 | } | |||
2679 | } | |||
2680 | ||||
2681 | static bool unionHasUniqueObjectRepresentations(const ASTContext &Context, | |||
2682 | const RecordDecl *RD, | |||
2683 | bool CheckIfTriviallyCopyable) { | |||
2684 | assert(RD->isUnion() && "Must be union type")(static_cast <bool> (RD->isUnion() && "Must be union type" ) ? void (0) : __assert_fail ("RD->isUnion() && \"Must be union type\"" , "clang/lib/AST/ASTContext.cpp", 2684, __extension__ __PRETTY_FUNCTION__ )); | |||
2685 | CharUnits UnionSize = Context.getTypeSizeInChars(RD->getTypeForDecl()); | |||
2686 | ||||
2687 | for (const auto *Field : RD->fields()) { | |||
2688 | if (!Context.hasUniqueObjectRepresentations(Field->getType(), | |||
2689 | CheckIfTriviallyCopyable)) | |||
2690 | return false; | |||
2691 | CharUnits FieldSize = Context.getTypeSizeInChars(Field->getType()); | |||
2692 | if (FieldSize != UnionSize) | |||
2693 | return false; | |||
2694 | } | |||
2695 | return !RD->field_empty(); | |||
2696 | } | |||
2697 | ||||
2698 | static int64_t getSubobjectOffset(const FieldDecl *Field, | |||
2699 | const ASTContext &Context, | |||
2700 | const clang::ASTRecordLayout & /*Layout*/) { | |||
2701 | return Context.getFieldOffset(Field); | |||
2702 | } | |||
2703 | ||||
2704 | static int64_t getSubobjectOffset(const CXXRecordDecl *RD, | |||
2705 | const ASTContext &Context, | |||
2706 | const clang::ASTRecordLayout &Layout) { | |||
2707 | return Context.toBits(Layout.getBaseClassOffset(RD)); | |||
2708 | } | |||
2709 | ||||
2710 | static std::optional<int64_t> | |||
2711 | structHasUniqueObjectRepresentations(const ASTContext &Context, | |||
2712 | const RecordDecl *RD, | |||
2713 | bool CheckIfTriviallyCopyable); | |||
2714 | ||||
2715 | static std::optional<int64_t> | |||
2716 | getSubobjectSizeInBits(const FieldDecl *Field, const ASTContext &Context, | |||
2717 | bool CheckIfTriviallyCopyable) { | |||
2718 | if (Field->getType()->isRecordType()) { | |||
2719 | const RecordDecl *RD = Field->getType()->getAsRecordDecl(); | |||
2720 | if (!RD->isUnion()) | |||
2721 | return structHasUniqueObjectRepresentations(Context, RD, | |||
2722 | CheckIfTriviallyCopyable); | |||
2723 | } | |||
2724 | ||||
2725 | // A _BitInt type may not be unique if it has padding bits | |||
2726 | // but if it is a bitfield the padding bits are not used. | |||
2727 | bool IsBitIntType = Field->getType()->isBitIntType(); | |||
2728 | if (!Field->getType()->isReferenceType() && !IsBitIntType && | |||
2729 | !Context.hasUniqueObjectRepresentations(Field->getType(), | |||
2730 | CheckIfTriviallyCopyable)) | |||
2731 | return std::nullopt; | |||
2732 | ||||
2733 | int64_t FieldSizeInBits = | |||
2734 | Context.toBits(Context.getTypeSizeInChars(Field->getType())); | |||
2735 | if (Field->isBitField()) { | |||
2736 | // If we have explicit padding bits, they don't contribute bits | |||
2737 | // to the actual object representation, so return 0. | |||
2738 | if (Field->isUnnamedBitfield()) | |||
2739 | return 0; | |||
2740 | ||||
2741 | int64_t BitfieldSize = Field->getBitWidthValue(Context); | |||
2742 | if (IsBitIntType) { | |||
2743 | if ((unsigned)BitfieldSize > | |||
2744 | cast<BitIntType>(Field->getType())->getNumBits()) | |||
2745 | return std::nullopt; | |||
2746 | } else if (BitfieldSize > FieldSizeInBits) { | |||
2747 | return std::nullopt; | |||
2748 | } | |||
2749 | FieldSizeInBits = BitfieldSize; | |||
2750 | } else if (IsBitIntType && !Context.hasUniqueObjectRepresentations( | |||
2751 | Field->getType(), CheckIfTriviallyCopyable)) { | |||
2752 | return std::nullopt; | |||
2753 | } | |||
2754 | return FieldSizeInBits; | |||
2755 | } | |||
2756 | ||||
2757 | static std::optional<int64_t> | |||
2758 | getSubobjectSizeInBits(const CXXRecordDecl *RD, const ASTContext &Context, | |||
2759 | bool CheckIfTriviallyCopyable) { | |||
2760 | return structHasUniqueObjectRepresentations(Context, RD, | |||
2761 | CheckIfTriviallyCopyable); | |||
2762 | } | |||
2763 | ||||
2764 | template <typename RangeT> | |||
2765 | static std::optional<int64_t> structSubobjectsHaveUniqueObjectRepresentations( | |||
2766 | const RangeT &Subobjects, int64_t CurOffsetInBits, | |||
2767 | const ASTContext &Context, const clang::ASTRecordLayout &Layout, | |||
2768 | bool CheckIfTriviallyCopyable) { | |||
2769 | for (const auto *Subobject : Subobjects) { | |||
2770 | std::optional<int64_t> SizeInBits = | |||
2771 | getSubobjectSizeInBits(Subobject, Context, CheckIfTriviallyCopyable); | |||
2772 | if (!SizeInBits) | |||
2773 | return std::nullopt; | |||
2774 | if (*SizeInBits != 0) { | |||
2775 | int64_t Offset = getSubobjectOffset(Subobject, Context, Layout); | |||
2776 | if (Offset != CurOffsetInBits) | |||
2777 | return std::nullopt; | |||
2778 | CurOffsetInBits += *SizeInBits; | |||
2779 | } | |||
2780 | } | |||
2781 | return CurOffsetInBits; | |||
2782 | } | |||
2783 | ||||
2784 | static std::optional<int64_t> | |||
2785 | structHasUniqueObjectRepresentations(const ASTContext &Context, | |||
2786 | const RecordDecl *RD, | |||
2787 | bool CheckIfTriviallyCopyable) { | |||
2788 | assert(!RD->isUnion() && "Must be struct/class type")(static_cast <bool> (!RD->isUnion() && "Must be struct/class type" ) ? void (0) : __assert_fail ("!RD->isUnion() && \"Must be struct/class type\"" , "clang/lib/AST/ASTContext.cpp", 2788, __extension__ __PRETTY_FUNCTION__ )); | |||
2789 | const auto &Layout = Context.getASTRecordLayout(RD); | |||
2790 | ||||
2791 | int64_t CurOffsetInBits = 0; | |||
2792 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RD)) { | |||
2793 | if (ClassDecl->isDynamicClass()) | |||
2794 | return std::nullopt; | |||
2795 | ||||
2796 | SmallVector<CXXRecordDecl *, 4> Bases; | |||
2797 | for (const auto &Base : ClassDecl->bases()) { | |||
2798 | // Empty types can be inherited from, and non-empty types can potentially | |||
2799 | // have tail padding, so just make sure there isn't an error. | |||
2800 | Bases.emplace_back(Base.getType()->getAsCXXRecordDecl()); | |||
2801 | } | |||
2802 | ||||
2803 | llvm::sort(Bases, [&](const CXXRecordDecl *L, const CXXRecordDecl *R) { | |||
2804 | return Layout.getBaseClassOffset(L) < Layout.getBaseClassOffset(R); | |||
2805 | }); | |||
2806 | ||||
2807 | std::optional<int64_t> OffsetAfterBases = | |||
2808 | structSubobjectsHaveUniqueObjectRepresentations( | |||
2809 | Bases, CurOffsetInBits, Context, Layout, CheckIfTriviallyCopyable); | |||
2810 | if (!OffsetAfterBases) | |||
2811 | return std::nullopt; | |||
2812 | CurOffsetInBits = *OffsetAfterBases; | |||
2813 | } | |||
2814 | ||||
2815 | std::optional<int64_t> OffsetAfterFields = | |||
2816 | structSubobjectsHaveUniqueObjectRepresentations( | |||
2817 | RD->fields(), CurOffsetInBits, Context, Layout, | |||
2818 | CheckIfTriviallyCopyable); | |||
2819 | if (!OffsetAfterFields) | |||
2820 | return std::nullopt; | |||
2821 | CurOffsetInBits = *OffsetAfterFields; | |||
2822 | ||||
2823 | return CurOffsetInBits; | |||
2824 | } | |||
2825 | ||||
2826 | bool ASTContext::hasUniqueObjectRepresentations( | |||
2827 | QualType Ty, bool CheckIfTriviallyCopyable) const { | |||
2828 | // C++17 [meta.unary.prop]: | |||
2829 | // The predicate condition for a template specialization | |||
2830 | // has_unique_object_representations<T> shall be | |||
2831 | // satisfied if and only if: | |||
2832 | // (9.1) - T is trivially copyable, and | |||
2833 | // (9.2) - any two objects of type T with the same value have the same | |||
2834 | // object representation, where two objects | |||
2835 | // of array or non-union class type are considered to have the same value | |||
2836 | // if their respective sequences of | |||
2837 | // direct subobjects have the same values, and two objects of union type | |||
2838 | // are considered to have the same | |||
2839 | // value if they have the same active member and the corresponding members | |||
2840 | // have the same value. | |||
2841 | // The set of scalar types for which this condition holds is | |||
2842 | // implementation-defined. [ Note: If a type has padding | |||
2843 | // bits, the condition does not hold; otherwise, the condition holds true | |||
2844 | // for unsigned integral types. -- end note ] | |||
2845 | assert(!Ty.isNull() && "Null QualType sent to unique object rep check")(static_cast <bool> (!Ty.isNull() && "Null QualType sent to unique object rep check" ) ? void (0) : __assert_fail ("!Ty.isNull() && \"Null QualType sent to unique object rep check\"" , "clang/lib/AST/ASTContext.cpp", 2845, __extension__ __PRETTY_FUNCTION__ )); | |||
2846 | ||||
2847 | // Arrays are unique only if their element type is unique. | |||
2848 | if (Ty->isArrayType()) | |||
2849 | return hasUniqueObjectRepresentations(getBaseElementType(Ty), | |||
2850 | CheckIfTriviallyCopyable); | |||
2851 | ||||
2852 | // (9.1) - T is trivially copyable... | |||
2853 | if (CheckIfTriviallyCopyable && !Ty.isTriviallyCopyableType(*this)) | |||
2854 | return false; | |||
2855 | ||||
2856 | // All integrals and enums are unique. | |||
2857 | if (Ty->isIntegralOrEnumerationType()) { | |||
2858 | // Except _BitInt types that have padding bits. | |||
2859 | if (const auto *BIT = Ty->getAs<BitIntType>()) | |||
2860 | return getTypeSize(BIT) == BIT->getNumBits(); | |||
2861 | ||||
2862 | return true; | |||
2863 | } | |||
2864 | ||||
2865 | // All other pointers are unique. | |||
2866 | if (Ty->isPointerType()) | |||
2867 | return true; | |||
2868 | ||||
2869 | if (const auto *MPT = Ty->getAs<MemberPointerType>()) | |||
2870 | return !ABI->getMemberPointerInfo(MPT).HasPadding; | |||
2871 | ||||
2872 | if (Ty->isRecordType()) { | |||
2873 | const RecordDecl *Record = Ty->castAs<RecordType>()->getDecl(); | |||
2874 | ||||
2875 | if (Record->isInvalidDecl()) | |||
2876 | return false; | |||
2877 | ||||
2878 | if (Record->isUnion()) | |||
2879 | return unionHasUniqueObjectRepresentations(*this, Record, | |||
2880 | CheckIfTriviallyCopyable); | |||
2881 | ||||
2882 | std::optional<int64_t> StructSize = structHasUniqueObjectRepresentations( | |||
2883 | *this, Record, CheckIfTriviallyCopyable); | |||
2884 | ||||
2885 | return StructSize && *StructSize == static_cast<int64_t>(getTypeSize(Ty)); | |||
2886 | } | |||
2887 | ||||
2888 | // FIXME: More cases to handle here (list by rsmith): | |||
2889 | // vectors (careful about, eg, vector of 3 foo) | |||
2890 | // _Complex int and friends | |||
2891 | // _Atomic T | |||
2892 | // Obj-C block pointers | |||
2893 | // Obj-C object pointers | |||
2894 | // and perhaps OpenCL's various builtin types (pipe, sampler_t, event_t, | |||
2895 | // clk_event_t, queue_t, reserve_id_t) | |||
2896 | // There're also Obj-C class types and the Obj-C selector type, but I think it | |||
2897 | // makes sense for those to return false here. | |||
2898 | ||||
2899 | return false; | |||
2900 | } | |||
2901 | ||||
2902 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { | |||
2903 | unsigned count = 0; | |||
2904 | // Count ivars declared in class extension. | |||
2905 | for (const auto *Ext : OI->known_extensions()) | |||
2906 | count += Ext->ivar_size(); | |||
2907 | ||||
2908 | // Count ivar defined in this class's implementation. This | |||
2909 | // includes synthesized ivars. | |||
2910 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) | |||
2911 | count += ImplDecl->ivar_size(); | |||
2912 | ||||
2913 | return count; | |||
2914 | } | |||
2915 | ||||
2916 | bool ASTContext::isSentinelNullExpr(const Expr *E) { | |||
2917 | if (!E) | |||
2918 | return false; | |||
2919 | ||||
2920 | // nullptr_t is always treated as null. | |||
2921 | if (E->getType()->isNullPtrType()) return true; | |||
2922 | ||||
2923 | if (E->getType()->isAnyPointerType() && | |||
2924 | E->IgnoreParenCasts()->isNullPointerConstant(*this, | |||
2925 | Expr::NPC_ValueDependentIsNull)) | |||
2926 | return true; | |||
2927 | ||||
2928 | // Unfortunately, __null has type 'int'. | |||
2929 | if (isa<GNUNullExpr>(E)) return true; | |||
2930 | ||||
2931 | return false; | |||
2932 | } | |||
2933 | ||||
2934 | /// Get the implementation of ObjCInterfaceDecl, or nullptr if none | |||
2935 | /// exists. | |||
2936 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { | |||
2937 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator | |||
2938 | I = ObjCImpls.find(D); | |||
2939 | if (I != ObjCImpls.end()) | |||
2940 | return cast<ObjCImplementationDecl>(I->second); | |||
2941 | return nullptr; | |||
2942 | } | |||
2943 | ||||
2944 | /// Get the implementation of ObjCCategoryDecl, or nullptr if none | |||
2945 | /// exists. | |||
2946 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { | |||
2947 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator | |||
2948 | I = ObjCImpls.find(D); | |||
2949 | if (I != ObjCImpls.end()) | |||
2950 | return cast<ObjCCategoryImplDecl>(I->second); | |||
2951 | return nullptr; | |||
2952 | } | |||
2953 | ||||
2954 | /// Set the implementation of ObjCInterfaceDecl. | |||
2955 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, | |||
2956 | ObjCImplementationDecl *ImplD) { | |||
2957 | assert(IFaceD && ImplD && "Passed null params")(static_cast <bool> (IFaceD && ImplD && "Passed null params") ? void (0) : __assert_fail ("IFaceD && ImplD && \"Passed null params\"" , "clang/lib/AST/ASTContext.cpp", 2957, __extension__ __PRETTY_FUNCTION__ )); | |||
2958 | ObjCImpls[IFaceD] = ImplD; | |||
2959 | } | |||
2960 | ||||
2961 | /// Set the implementation of ObjCCategoryDecl. | |||
2962 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, | |||
2963 | ObjCCategoryImplDecl *ImplD) { | |||
2964 | assert(CatD && ImplD && "Passed null params")(static_cast <bool> (CatD && ImplD && "Passed null params" ) ? void (0) : __assert_fail ("CatD && ImplD && \"Passed null params\"" , "clang/lib/AST/ASTContext.cpp", 2964, __extension__ __PRETTY_FUNCTION__ )); | |||
2965 | ObjCImpls[CatD] = ImplD; | |||
2966 | } | |||
2967 | ||||
2968 | const ObjCMethodDecl * | |||
2969 | ASTContext::getObjCMethodRedeclaration(const ObjCMethodDecl *MD) const { | |||
2970 | return ObjCMethodRedecls.lookup(MD); | |||
2971 | } | |||
2972 | ||||
2973 | void ASTContext::setObjCMethodRedeclaration(const ObjCMethodDecl *MD, | |||
2974 | const ObjCMethodDecl *Redecl) { | |||
2975 | assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration")(static_cast <bool> (!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration") ? void (0) : __assert_fail ("!getObjCMethodRedeclaration(MD) && \"MD already has a redeclaration\"" , "clang/lib/AST/ASTContext.cpp", 2975, __extension__ __PRETTY_FUNCTION__ )); | |||
2976 | ObjCMethodRedecls[MD] = Redecl; | |||
2977 | } | |||
2978 | ||||
2979 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( | |||
2980 | const NamedDecl *ND) const { | |||
2981 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) | |||
2982 | return ID; | |||
2983 | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) | |||
2984 | return CD->getClassInterface(); | |||
2985 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) | |||
2986 | return IMD->getClassInterface(); | |||
2987 | ||||
2988 | return nullptr; | |||
2989 | } | |||
2990 | ||||
2991 | /// Get the copy initialization expression of VarDecl, or nullptr if | |||
2992 | /// none exists. | |||
2993 | BlockVarCopyInit ASTContext::getBlockVarCopyInit(const VarDecl *VD) const { | |||
2994 | assert(VD && "Passed null params")(static_cast <bool> (VD && "Passed null params" ) ? void (0) : __assert_fail ("VD && \"Passed null params\"" , "clang/lib/AST/ASTContext.cpp", 2994, __extension__ __PRETTY_FUNCTION__ )); | |||
2995 | assert(VD->hasAttr<BlocksAttr>() &&(static_cast <bool> (VD->hasAttr<BlocksAttr>() && "getBlockVarCopyInits - not __block var") ? void ( 0) : __assert_fail ("VD->hasAttr<BlocksAttr>() && \"getBlockVarCopyInits - not __block var\"" , "clang/lib/AST/ASTContext.cpp", 2996, __extension__ __PRETTY_FUNCTION__ )) | |||
2996 | "getBlockVarCopyInits - not __block var")(static_cast <bool> (VD->hasAttr<BlocksAttr>() && "getBlockVarCopyInits - not __block var") ? void ( 0) : __assert_fail ("VD->hasAttr<BlocksAttr>() && \"getBlockVarCopyInits - not __block var\"" , "clang/lib/AST/ASTContext.cpp", 2996, __extension__ __PRETTY_FUNCTION__ )); | |||
2997 | auto I = BlockVarCopyInits.find(VD); | |||
2998 | if (I != BlockVarCopyInits.end()) | |||
2999 | return I->second; | |||
3000 | return {nullptr, false}; | |||
3001 | } | |||
3002 | ||||
3003 | /// Set the copy initialization expression of a block var decl. | |||
3004 | void ASTContext::setBlockVarCopyInit(const VarDecl*VD, Expr *CopyExpr, | |||
3005 | bool CanThrow) { | |||
3006 | assert(VD && CopyExpr && "Passed null params")(static_cast <bool> (VD && CopyExpr && "Passed null params" ) ? void (0) : __assert_fail ("VD && CopyExpr && \"Passed null params\"" , "clang/lib/AST/ASTContext.cpp", 3006, __extension__ __PRETTY_FUNCTION__ )); | |||
3007 | assert(VD->hasAttr<BlocksAttr>() &&(static_cast <bool> (VD->hasAttr<BlocksAttr>() && "setBlockVarCopyInits - not __block var") ? void ( 0) : __assert_fail ("VD->hasAttr<BlocksAttr>() && \"setBlockVarCopyInits - not __block var\"" , "clang/lib/AST/ASTContext.cpp", 3008, __extension__ __PRETTY_FUNCTION__ )) | |||
3008 | "setBlockVarCopyInits - not __block var")(static_cast <bool> (VD->hasAttr<BlocksAttr>() && "setBlockVarCopyInits - not __block var") ? void ( 0) : __assert_fail ("VD->hasAttr<BlocksAttr>() && \"setBlockVarCopyInits - not __block var\"" , "clang/lib/AST/ASTContext.cpp", 3008, __extension__ __PRETTY_FUNCTION__ )); | |||
3009 | BlockVarCopyInits[VD].setExprAndFlag(CopyExpr, CanThrow); | |||
3010 | } | |||
3011 | ||||
3012 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, | |||
3013 | unsigned DataSize) const { | |||
3014 | if (!DataSize) | |||
3015 | DataSize = TypeLoc::getFullDataSizeForType(T); | |||
3016 | else | |||
3017 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&(static_cast <bool> (DataSize == TypeLoc::getFullDataSizeForType (T) && "incorrect data size provided to CreateTypeSourceInfo!" ) ? void (0) : __assert_fail ("DataSize == TypeLoc::getFullDataSizeForType(T) && \"incorrect data size provided to CreateTypeSourceInfo!\"" , "clang/lib/AST/ASTContext.cpp", 3018, __extension__ __PRETTY_FUNCTION__ )) | |||
3018 | "incorrect data size provided to CreateTypeSourceInfo!")(static_cast <bool> (DataSize == TypeLoc::getFullDataSizeForType (T) && "incorrect data size provided to CreateTypeSourceInfo!" ) ? void (0) : __assert_fail ("DataSize == TypeLoc::getFullDataSizeForType(T) && \"incorrect data size provided to CreateTypeSourceInfo!\"" , "clang/lib/AST/ASTContext.cpp", 3018, __extension__ __PRETTY_FUNCTION__ )); | |||
3019 | ||||
3020 | auto *TInfo = | |||
3021 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); | |||
3022 | new (TInfo) TypeSourceInfo(T); | |||
3023 | return TInfo; | |||
3024 | } | |||
3025 | ||||
3026 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, | |||
3027 | SourceLocation L) const { | |||
3028 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); | |||
3029 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); | |||
3030 | return DI; | |||
3031 | } | |||
3032 | ||||
3033 | const ASTRecordLayout & | |||
3034 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { | |||
3035 | return getObjCLayout(D, nullptr); | |||
3036 | } | |||
3037 | ||||
3038 | const ASTRecordLayout & | |||
3039 | ASTContext::getASTObjCImplementationLayout( | |||
3040 | const ObjCImplementationDecl *D) const { | |||
3041 | return getObjCLayout(D->getClassInterface(), D); | |||
3042 | } | |||
3043 | ||||
3044 | static auto getCanonicalTemplateArguments(const ASTContext &C, | |||
3045 | ArrayRef<TemplateArgument> Args, | |||
3046 | bool &AnyNonCanonArgs) { | |||
3047 | SmallVector<TemplateArgument, 16> CanonArgs(Args); | |||
3048 | for (auto &Arg : CanonArgs) { | |||
3049 | TemplateArgument OrigArg = Arg; | |||
3050 | Arg = C.getCanonicalTemplateArgument(Arg); | |||
3051 | AnyNonCanonArgs |= !Arg.structurallyEquals(OrigArg); | |||
3052 | } | |||
3053 | return CanonArgs; | |||
3054 | } | |||
3055 | ||||
3056 | //===----------------------------------------------------------------------===// | |||
3057 | // Type creation/memoization methods | |||
3058 | //===----------------------------------------------------------------------===// | |||
3059 | ||||
3060 | QualType | |||
3061 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { | |||
3062 | unsigned fastQuals = quals.getFastQualifiers(); | |||
3063 | quals.removeFastQualifiers(); | |||
3064 | ||||
3065 | // Check if we've already instantiated this type. | |||
3066 | llvm::FoldingSetNodeID ID; | |||
3067 | ExtQuals::Profile(ID, baseType, quals); | |||
3068 | void *insertPos = nullptr; | |||
3069 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { | |||
3070 | assert(eq->getQualifiers() == quals)(static_cast <bool> (eq->getQualifiers() == quals) ? void (0) : __assert_fail ("eq->getQualifiers() == quals", "clang/lib/AST/ASTContext.cpp", 3070, __extension__ __PRETTY_FUNCTION__ )); | |||
3071 | return QualType(eq, fastQuals); | |||
3072 | } | |||
3073 | ||||
3074 | // If the base type is not canonical, make the appropriate canonical type. | |||
3075 | QualType canon; | |||
3076 | if (!baseType->isCanonicalUnqualified()) { | |||
3077 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); | |||
3078 | canonSplit.Quals.addConsistentQualifiers(quals); | |||
3079 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); | |||
3080 | ||||
3081 | // Re-find the insert position. | |||
3082 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); | |||
3083 | } | |||
3084 | ||||
3085 | auto *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); | |||
3086 | ExtQualNodes.InsertNode(eq, insertPos); | |||
3087 | return QualType(eq, fastQuals); | |||
3088 | } | |||
3089 | ||||
3090 | QualType ASTContext::getAddrSpaceQualType(QualType T, | |||
3091 | LangAS AddressSpace) const { | |||
3092 | QualType CanT = getCanonicalType(T); | |||
3093 | if (CanT.getAddressSpace() == AddressSpace) | |||
3094 | return T; | |||
3095 | ||||
3096 | // If we are composing extended qualifiers together, merge together | |||
3097 | // into one ExtQuals node. | |||
3098 | QualifierCollector Quals; | |||
3099 | const Type *TypeNode = Quals.strip(T); | |||
3100 | ||||
3101 | // If this type already has an address space specified, it cannot get | |||
3102 | // another one. | |||
3103 | assert(!Quals.hasAddressSpace() &&(static_cast <bool> (!Quals.hasAddressSpace() && "Type cannot be in multiple addr spaces!") ? void (0) : __assert_fail ("!Quals.hasAddressSpace() && \"Type cannot be in multiple addr spaces!\"" , "clang/lib/AST/ASTContext.cpp", 3104, __extension__ __PRETTY_FUNCTION__ )) | |||
3104 | "Type cannot be in multiple addr spaces!")(static_cast <bool> (!Quals.hasAddressSpace() && "Type cannot be in multiple addr spaces!") ? void (0) : __assert_fail ("!Quals.hasAddressSpace() && \"Type cannot be in multiple addr spaces!\"" , "clang/lib/AST/ASTContext.cpp", 3104, __extension__ __PRETTY_FUNCTION__ )); | |||
3105 | Quals.addAddressSpace(AddressSpace); | |||
3106 | ||||
3107 | return getExtQualType(TypeNode, Quals); | |||
3108 | } | |||
3109 | ||||
3110 | QualType ASTContext::removeAddrSpaceQualType(QualType T) const { | |||
3111 | // If the type is not qualified with an address space, just return it | |||
3112 | // immediately. | |||
3113 | if (!T.hasAddressSpace()) | |||
3114 | return T; | |||
3115 | ||||
3116 | // If we are composing extended qualifiers together, merge together | |||
3117 | // into one ExtQuals node. | |||
3118 | QualifierCollector Quals; | |||
3119 | const Type *TypeNode; | |||
3120 | ||||
3121 | while (T.hasAddressSpace()) { | |||
3122 | TypeNode = Quals.strip(T); | |||
3123 | ||||
3124 | // If the type no longer has an address space after stripping qualifiers, | |||
3125 | // jump out. | |||
3126 | if (!QualType(TypeNode, 0).hasAddressSpace()) | |||
3127 | break; | |||
3128 | ||||
3129 | // There might be sugar in the way. Strip it and try again. | |||
3130 | T = T.getSingleStepDesugaredType(*this); | |||
3131 | } | |||
3132 | ||||
3133 | Quals.removeAddressSpace(); | |||
3134 | ||||
3135 | // Removal of the address space can mean there are no longer any | |||
3136 | // non-fast qualifiers, so creating an ExtQualType isn't possible (asserts) | |||
3137 | // or required. | |||
3138 | if (Quals.hasNonFastQualifiers()) | |||
3139 | return getExtQualType(TypeNode, Quals); | |||
3140 | else | |||
3141 | return QualType(TypeNode, Quals.getFastQualifiers()); | |||
| ||||
3142 | } | |||
3143 | ||||
3144 | QualType ASTContext::getObjCGCQualType(QualType T, | |||
3145 | Qualifiers::GC GCAttr) const { | |||
3146 | QualType CanT = getCanonicalType(T); | |||
3147 | if (CanT.getObjCGCAttr() == GCAttr) | |||
3148 | return T; | |||
3149 | ||||
3150 | if (const auto *ptr = T->getAs<PointerType>()) { | |||
3151 | QualType Pointee = ptr->getPointeeType(); | |||
3152 | if (Pointee->isAnyPointerType()) { | |||
3153 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); | |||
3154 | return getPointerType(ResultType); | |||
3155 | } | |||
3156 | } | |||
3157 | ||||
3158 | // If we are composing extended qualifiers together, merge together | |||
3159 | // into one ExtQuals node. | |||
3160 | QualifierCollector Quals; | |||
3161 | const Type *TypeNode = Quals.strip(T); | |||
3162 | ||||
3163 | // If this type already has an ObjCGC specified, it cannot get | |||
3164 | // another one. | |||
3165 | assert(!Quals.hasObjCGCAttr() &&(static_cast <bool> (!Quals.hasObjCGCAttr() && "Type cannot have multiple ObjCGCs!" ) ? void (0) : __assert_fail ("!Quals.hasObjCGCAttr() && \"Type cannot have multiple ObjCGCs!\"" , "clang/lib/AST/ASTContext.cpp", 3166, __extension__ __PRETTY_FUNCTION__ )) | |||
3166 | "Type cannot have multiple ObjCGCs!")(static_cast <bool> (!Quals.hasObjCGCAttr() && "Type cannot have multiple ObjCGCs!" ) ? void (0) : __assert_fail ("!Quals.hasObjCGCAttr() && \"Type cannot have multiple ObjCGCs!\"" , "clang/lib/AST/ASTContext.cpp", 3166, __extension__ __PRETTY_FUNCTION__ )); | |||
3167 | Quals.addObjCGCAttr(GCAttr); | |||
3168 | ||||
3169 | return getExtQualType(TypeNode, Quals); | |||
3170 | } | |||
3171 | ||||
3172 | QualType ASTContext::removePtrSizeAddrSpace(QualType T) const { | |||
3173 | if (const PointerType *Ptr = T->getAs<PointerType>()) { | |||
3174 | QualType Pointee = Ptr->getPointeeType(); | |||
3175 | if (isPtrSizeAddressSpace(Pointee.getAddressSpace())) { | |||
3176 | return getPointerType(removeAddrSpaceQualType(Pointee)); | |||
3177 | } | |||
3178 | } | |||
3179 | return T; | |||
3180 | } | |||
3181 | ||||
3182 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, | |||
3183 | FunctionType::ExtInfo Info) { | |||
3184 | if (T->getExtInfo() == Info) | |||
3185 | return T; | |||
3186 | ||||
3187 | QualType Result; | |||
3188 | if (const auto *FNPT = dyn_cast<FunctionNoProtoType>(T)) { | |||
3189 | Result = getFunctionNoProtoType(FNPT->getReturnType(), Info); | |||
3190 | } else { | |||
3191 | const auto *FPT = cast<FunctionProtoType>(T); | |||
3192 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); | |||
3193 | EPI.ExtInfo = Info; | |||
3194 | Result = getFunctionType(FPT->getReturnType(), FPT->getParamTypes(), EPI); | |||
3195 | } | |||
3196 | ||||
3197 | return cast<FunctionType>(Result.getTypePtr()); | |||
3198 | } | |||
3199 | ||||
3200 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, | |||
3201 | QualType ResultType) { | |||
3202 | FD = FD->getMostRecentDecl(); | |||
3203 | while (true) { | |||
3204 | const auto *FPT = FD->getType()->castAs<FunctionProtoType>(); | |||
3205 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); | |||
3206 | FD->setType(getFunctionType(ResultType, FPT->getParamTypes(), EPI)); | |||
3207 | if (FunctionDecl *Next = FD->getPreviousDecl()) | |||
3208 | FD = Next; | |||
3209 | else | |||
3210 | break; | |||
3211 | } | |||
3212 | if (ASTMutationListener *L = getASTMutationListener()) | |||
3213 | L->DeducedReturnType(FD, ResultType); | |||
3214 | } | |||
3215 | ||||
3216 | /// Get a function type and produce the equivalent function type with the | |||
3217 | /// specified exception specification. Type sugar that can be present on a | |||
3218 | /// declaration of a function with an exception specification is permitted | |||
3219 | /// and preserved. Other type sugar (for instance, typedefs) is not. | |||
3220 | QualType ASTContext::getFunctionTypeWithExceptionSpec( | |||
3221 | QualType Orig, const FunctionProtoType::ExceptionSpecInfo &ESI) const { | |||
3222 | // Might have some parens. | |||
3223 | if (const auto *PT = dyn_cast<ParenType>(Orig)) | |||
3224 | return getParenType( | |||
3225 | getFunctionTypeWithExceptionSpec(PT->getInnerType(), ESI)); | |||
3226 | ||||
3227 | // Might be wrapped in a macro qualified type. | |||
3228 | if (const auto *MQT = dyn_cast<MacroQualifiedType>(Orig)) | |||
3229 | return getMacroQualifiedType( | |||
3230 | getFunctionTypeWithExceptionSpec(MQT->getUnderlyingType(), ESI), | |||
3231 | MQT->getMacroIdentifier()); | |||
3232 | ||||
3233 | // Might have a calling-convention attribute. | |||
3234 | if (const auto *AT = dyn_cast<AttributedType>(Orig)) | |||
3235 | return getAttributedType( | |||
3236 | AT->getAttrKind(), | |||
3237 | getFunctionTypeWithExceptionSpec(AT->getModifiedType(), ESI), | |||
3238 | getFunctionTypeWithExceptionSpec(AT->getEquivalentType(), ESI)); | |||
3239 | ||||
3240 | // Anything else must be a function type. Rebuild it with the new exception | |||
3241 | // specification. | |||
3242 | const auto *Proto = Orig->castAs<FunctionProtoType>(); | |||
3243 | return getFunctionType( | |||
3244 | Proto->getReturnType(), Proto->getParamTypes(), | |||
3245 | Proto->getExtProtoInfo().withExceptionSpec(ESI)); | |||
3246 | } | |||
3247 | ||||
3248 | bool ASTContext::hasSameFunctionTypeIgnoringExceptionSpec(QualType T, | |||
3249 | QualType U) const { | |||
3250 | return hasSameType(T, U) || | |||
3251 | (getLangOpts().CPlusPlus17 && | |||
3252 | hasSameType(getFunctionTypeWithExceptionSpec(T, EST_None), | |||
3253 | getFunctionTypeWithExceptionSpec(U, EST_None))); | |||
3254 | } | |||
3255 | ||||
3256 | QualType ASTContext::getFunctionTypeWithoutPtrSizes(QualType T) { | |||
3257 | if (const auto *Proto = T->getAs<FunctionProtoType>()) { | |||
3258 | QualType RetTy = removePtrSizeAddrSpace(Proto->getReturnType()); | |||
3259 | SmallVector<QualType, 16> Args(Proto->param_types().size()); | |||
3260 | for (unsigned i = 0, n = Args.size(); i != n; ++i) | |||
3261 | Args[i] = removePtrSizeAddrSpace(Proto->param_types()[i]); | |||
3262 | return getFunctionType(RetTy, Args, Proto->getExtProtoInfo()); | |||
3263 | } | |||
3264 | ||||
3265 | if (const FunctionNoProtoType *Proto = T->getAs<FunctionNoProtoType>()) { | |||
3266 | QualType RetTy = removePtrSizeAddrSpace(Proto->getReturnType()); | |||
3267 | return getFunctionNoProtoType(RetTy, Proto->getExtInfo()); | |||
3268 | } | |||
3269 | ||||
3270 | return T; | |||
3271 | } | |||
3272 | ||||
3273 | bool ASTContext::hasSameFunctionTypeIgnoringPtrSizes(QualType T, QualType U) { | |||
3274 | return hasSameType(T, U) || | |||
| ||||
3275 | hasSameType(getFunctionTypeWithoutPtrSizes(T), | |||
3276 | getFunctionTypeWithoutPtrSizes(U)); | |||
3277 | } | |||
3278 | ||||
3279 | void ASTContext::adjustExceptionSpec( | |||
3280 | FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI, | |||
3281 | bool AsWritten) { | |||
3282 | // Update the type. | |||
3283 | QualType Updated = | |||
3284 | getFunctionTypeWithExceptionSpec(FD->getType(), ESI); | |||
3285 | FD->setType(Updated); | |||
3286 | ||||
3287 | if (!AsWritten) | |||
3288 | return; | |||
3289 | ||||
3290 | // Update the type in the type source information too. | |||
3291 | if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) { | |||
3292 | // If the type and the type-as-written differ, we may need to update | |||
3293 | // the type-as-written too. | |||
3294 | if (TSInfo->getType() != FD->getType()) | |||
3295 | Updated = getFunctionTypeWithExceptionSpec(TSInfo->getType(), ESI); | |||
3296 | ||||
3297 | // FIXME: When we get proper type location information for exceptions, | |||
3298 | // we'll also have to rebuild the TypeSourceInfo. For now, we just patch | |||
3299 | // up the TypeSourceInfo; | |||
3300 | assert(TypeLoc::getFullDataSizeForType(Updated) ==(static_cast <bool> (TypeLoc::getFullDataSizeForType(Updated ) == TypeLoc::getFullDataSizeForType(TSInfo->getType()) && "TypeLoc size mismatch from updating exception specification" ) ? void (0) : __assert_fail ("TypeLoc::getFullDataSizeForType(Updated) == TypeLoc::getFullDataSizeForType(TSInfo->getType()) && \"TypeLoc size mismatch from updating exception specification\"" , "clang/lib/AST/ASTContext.cpp", 3302, __extension__ __PRETTY_FUNCTION__ )) | |||
3301 | TypeLoc::getFullDataSizeForType(TSInfo->getType()) &&(static_cast <bool> (TypeLoc::getFullDataSizeForType(Updated ) == TypeLoc::getFullDataSizeForType(TSInfo->getType()) && "TypeLoc size mismatch from updating exception specification" ) ? void (0) : __assert_fail ("TypeLoc::getFullDataSizeForType(Updated) == TypeLoc::getFullDataSizeForType(TSInfo->getType()) && \"TypeLoc size mismatch from updating exception specification\"" , "clang/lib/AST/ASTContext.cpp", 3302, __extension__ __PRETTY_FUNCTION__ )) | |||
3302 | "TypeLoc size mismatch from updating exception specification")(static_cast <bool> (TypeLoc::getFullDataSizeForType(Updated ) == TypeLoc::getFullDataSizeForType(TSInfo->getType()) && "TypeLoc size mismatch from updating exception specification" ) ? void (0) : __assert_fail ("TypeLoc::getFullDataSizeForType(Updated) == TypeLoc::getFullDataSizeForType(TSInfo->getType()) && \"TypeLoc size mismatch from updating exception specification\"" , "clang/lib/AST/ASTContext.cpp", 3302, __extension__ __PRETTY_FUNCTION__ )); | |||
3303 | TSInfo->overrideType(Updated); | |||
3304 | } | |||
3305 | } | |||
3306 | ||||
3307 | /// getComplexType - Return the uniqued reference to the type for a complex | |||
3308 | /// number with the specified element type. | |||
3309 | QualType ASTContext::getComplexType(QualType T) const { | |||
3310 | // Unique pointers, to guarantee there is only one pointer of a particular | |||
3311 | // structure. | |||
3312 | llvm::FoldingSetNodeID ID; | |||
3313 | ComplexType::Profile(ID, T); | |||
3314 | ||||
3315 | void *InsertPos = nullptr; | |||
3316 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
3317 | return QualType(CT, 0); | |||
3318 | ||||
3319 | // If the pointee type isn't canonical, this won't be a canonical type either, | |||
3320 | // so fill in the canonical type field. | |||
3321 | QualType Canonical; | |||
3322 | if (!T.isCanonical()) { | |||
3323 | Canonical = getComplexType(getCanonicalType(T)); | |||
3324 | ||||
3325 | // Get the new insert position for the node we care about. | |||
3326 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3327 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 3327, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
3328 | } | |||
3329 | auto *New = new (*this, TypeAlignment) ComplexType(T, Canonical); | |||
3330 | Types.push_back(New); | |||
3331 | ComplexTypes.InsertNode(New, InsertPos); | |||
3332 | return QualType(New, 0); | |||
3333 | } | |||
3334 | ||||
3335 | /// getPointerType - Return the uniqued reference to the type for a pointer to | |||
3336 | /// the specified type. | |||
3337 | QualType ASTContext::getPointerType(QualType T) const { | |||
3338 | // Unique pointers, to guarantee there is only one pointer of a particular | |||
3339 | // structure. | |||
3340 | llvm::FoldingSetNodeID ID; | |||
3341 | PointerType::Profile(ID, T); | |||
3342 | ||||
3343 | void *InsertPos = nullptr; | |||
3344 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
3345 | return QualType(PT, 0); | |||
3346 | ||||
3347 | // If the pointee type isn't canonical, this won't be a canonical type either, | |||
3348 | // so fill in the canonical type field. | |||
3349 | QualType Canonical; | |||
3350 | if (!T.isCanonical()) { | |||
3351 | Canonical = getPointerType(getCanonicalType(T)); | |||
3352 | ||||
3353 | // Get the new insert position for the node we care about. | |||
3354 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3355 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 3355, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
3356 | } | |||
3357 | auto *New = new (*this, TypeAlignment) PointerType(T, Canonical); | |||
3358 | Types.push_back(New); | |||
3359 | PointerTypes.InsertNode(New, InsertPos); | |||
3360 | return QualType(New, 0); | |||
3361 | } | |||
3362 | ||||
3363 | QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const { | |||
3364 | llvm::FoldingSetNodeID ID; | |||
3365 | AdjustedType::Profile(ID, Orig, New); | |||
3366 | void *InsertPos = nullptr; | |||
3367 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3368 | if (AT) | |||
3369 | return QualType(AT, 0); | |||
3370 | ||||
3371 | QualType Canonical = getCanonicalType(New); | |||
3372 | ||||
3373 | // Get the new insert position for the node we care about. | |||
3374 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3375 | assert(!AT && "Shouldn't be in the map!")(static_cast <bool> (!AT && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!AT && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 3375, __extension__ __PRETTY_FUNCTION__ )); | |||
3376 | ||||
3377 | AT = new (*this, TypeAlignment) | |||
3378 | AdjustedType(Type::Adjusted, Orig, New, Canonical); | |||
3379 | Types.push_back(AT); | |||
3380 | AdjustedTypes.InsertNode(AT, InsertPos); | |||
3381 | return QualType(AT, 0); | |||
3382 | } | |||
3383 | ||||
3384 | QualType ASTContext::getDecayedType(QualType Orig, QualType Decayed) const { | |||
3385 | llvm::FoldingSetNodeID ID; | |||
3386 | AdjustedType::Profile(ID, Orig, Decayed); | |||
3387 | void *InsertPos = nullptr; | |||
3388 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3389 | if (AT) | |||
3390 | return QualType(AT, 0); | |||
3391 | ||||
3392 | QualType Canonical = getCanonicalType(Decayed); | |||
3393 | ||||
3394 | // Get the new insert position for the node we care about. | |||
3395 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3396 | assert(!AT && "Shouldn't be in the map!")(static_cast <bool> (!AT && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!AT && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 3396, __extension__ __PRETTY_FUNCTION__ )); | |||
3397 | ||||
3398 | AT = new (*this, TypeAlignment) DecayedType(Orig, Decayed, Canonical); | |||
3399 | Types.push_back(AT); | |||
3400 | AdjustedTypes.InsertNode(AT, InsertPos); | |||
3401 | return QualType(AT, 0); | |||
3402 | } | |||
3403 | ||||
3404 | QualType ASTContext::getDecayedType(QualType T) const { | |||
3405 | assert((T->isArrayType() || T->isFunctionType()) && "T does not decay")(static_cast <bool> ((T->isArrayType() || T->isFunctionType ()) && "T does not decay") ? void (0) : __assert_fail ("(T->isArrayType() || T->isFunctionType()) && \"T does not decay\"" , "clang/lib/AST/ASTContext.cpp", 3405, __extension__ __PRETTY_FUNCTION__ )); | |||
3406 | ||||
3407 | QualType Decayed; | |||
3408 | ||||
3409 | // C99 6.7.5.3p7: | |||
3410 | // A declaration of a parameter as "array of type" shall be | |||
3411 | // adjusted to "qualified pointer to type", where the type | |||
3412 | // qualifiers (if any) are those specified within the [ and ] of | |||
3413 | // the array type derivation. | |||
3414 | if (T->isArrayType()) | |||
3415 | Decayed = getArrayDecayedType(T); | |||
3416 | ||||
3417 | // C99 6.7.5.3p8: | |||
3418 | // A declaration of a parameter as "function returning type" | |||
3419 | // shall be adjusted to "pointer to function returning type", as | |||
3420 | // in 6.3.2.1. | |||
3421 | if (T->isFunctionType()) | |||
3422 | Decayed = getPointerType(T); | |||
3423 | ||||
3424 | return getDecayedType(T, Decayed); | |||
3425 | } | |||
3426 | ||||
3427 | /// getBlockPointerType - Return the uniqued reference to the type for | |||
3428 | /// a pointer to the specified block. | |||
3429 | QualType ASTContext::getBlockPointerType(QualType T) const { | |||
3430 | assert(T->isFunctionType() && "block of function types only")(static_cast <bool> (T->isFunctionType() && "block of function types only" ) ? void (0) : __assert_fail ("T->isFunctionType() && \"block of function types only\"" , "clang/lib/AST/ASTContext.cpp", 3430, __extension__ __PRETTY_FUNCTION__ )); | |||
3431 | // Unique pointers, to guarantee there is only one block of a particular | |||
3432 | // structure. | |||
3433 | llvm::FoldingSetNodeID ID; | |||
3434 | BlockPointerType::Profile(ID, T); | |||
3435 | ||||
3436 | void *InsertPos = nullptr; | |||
3437 | if (BlockPointerType *PT = | |||
3438 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
3439 | return QualType(PT, 0); | |||
3440 | ||||
3441 | // If the block pointee type isn't canonical, this won't be a canonical | |||
3442 | // type either so fill in the canonical type field. | |||
3443 | QualType Canonical; | |||
3444 | if (!T.isCanonical()) { | |||
3445 | Canonical = getBlockPointerType(getCanonicalType(T)); | |||
3446 | ||||
3447 | // Get the new insert position for the node we care about. | |||
3448 | BlockPointerType *NewIP = | |||
3449 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3450 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 3450, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
3451 | } | |||
3452 | auto *New = new (*this, TypeAlignment) BlockPointerType(T, Canonical); | |||
3453 | Types.push_back(New); | |||
3454 | BlockPointerTypes.InsertNode(New, InsertPos); | |||
3455 | return QualType(New, 0); | |||
3456 | } | |||
3457 | ||||
3458 | /// getLValueReferenceType - Return the uniqued reference to the type for an | |||
3459 | /// lvalue reference to the specified type. | |||
3460 | QualType | |||
3461 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { | |||
3462 | assert((!T->isPlaceholderType() ||(static_cast <bool> ((!T->isPlaceholderType() || T-> isSpecificPlaceholderType(BuiltinType::UnknownAny)) && "Unresolved placeholder type") ? void (0) : __assert_fail ("(!T->isPlaceholderType() || T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) && \"Unresolved placeholder type\"" , "clang/lib/AST/ASTContext.cpp", 3464, __extension__ __PRETTY_FUNCTION__ )) | |||
3463 | T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) &&(static_cast <bool> ((!T->isPlaceholderType() || T-> isSpecificPlaceholderType(BuiltinType::UnknownAny)) && "Unresolved placeholder type") ? void (0) : __assert_fail ("(!T->isPlaceholderType() || T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) && \"Unresolved placeholder type\"" , "clang/lib/AST/ASTContext.cpp", 3464, __extension__ __PRETTY_FUNCTION__ )) | |||
3464 | "Unresolved placeholder type")(static_cast <bool> ((!T->isPlaceholderType() || T-> isSpecificPlaceholderType(BuiltinType::UnknownAny)) && "Unresolved placeholder type") ? void (0) : __assert_fail ("(!T->isPlaceholderType() || T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) && \"Unresolved placeholder type\"" , "clang/lib/AST/ASTContext.cpp", 3464, __extension__ __PRETTY_FUNCTION__ )); | |||
3465 | ||||
3466 | // Unique pointers, to guarantee there is only one pointer of a particular | |||
3467 | // structure. | |||
3468 | llvm::FoldingSetNodeID ID; | |||
3469 | ReferenceType::Profile(ID, T, SpelledAsLValue); | |||
3470 | ||||
3471 | void *InsertPos = nullptr; | |||
3472 | if (LValueReferenceType *RT = | |||
3473 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
3474 | return QualType(RT, 0); | |||
3475 | ||||
3476 | const auto *InnerRef = T->getAs<ReferenceType>(); | |||
3477 | ||||
3478 | // If the referencee type isn't canonical, this won't be a canonical type | |||
3479 | // either, so fill in the canonical type field. | |||
3480 | QualType Canonical; | |||
3481 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { | |||
3482 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); | |||
3483 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); | |||
3484 | ||||
3485 | // Get the new insert position for the node we care about. | |||
3486 | LValueReferenceType *NewIP = | |||
3487 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3488 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 3488, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
3489 | } | |||
3490 | ||||
3491 | auto *New = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, | |||
3492 | SpelledAsLValue); | |||
3493 | Types.push_back(New); | |||
3494 | LValueReferenceTypes.InsertNode(New, InsertPos); | |||
3495 | ||||
3496 | return QualType(New, 0); | |||
3497 | } | |||
3498 | ||||
3499 | /// getRValueReferenceType - Return the uniqued reference to the type for an | |||
3500 | /// rvalue reference to the specified type. | |||
3501 | QualType ASTContext::getRValueReferenceType(QualType T) const { | |||
3502 | assert((!T->isPlaceholderType() ||(static_cast <bool> ((!T->isPlaceholderType() || T-> isSpecificPlaceholderType(BuiltinType::UnknownAny)) && "Unresolved placeholder type") ? void (0) : __assert_fail ("(!T->isPlaceholderType() || T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) && \"Unresolved placeholder type\"" , "clang/lib/AST/ASTContext.cpp", 3504, __extension__ __PRETTY_FUNCTION__ )) | |||
3503 | T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) &&(static_cast <bool> ((!T->isPlaceholderType() || T-> isSpecificPlaceholderType(BuiltinType::UnknownAny)) && "Unresolved placeholder type") ? void (0) : __assert_fail ("(!T->isPlaceholderType() || T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) && \"Unresolved placeholder type\"" , "clang/lib/AST/ASTContext.cpp", 3504, __extension__ __PRETTY_FUNCTION__ )) | |||
3504 | "Unresolved placeholder type")(static_cast <bool> ((!T->isPlaceholderType() || T-> isSpecificPlaceholderType(BuiltinType::UnknownAny)) && "Unresolved placeholder type") ? void (0) : __assert_fail ("(!T->isPlaceholderType() || T->isSpecificPlaceholderType(BuiltinType::UnknownAny)) && \"Unresolved placeholder type\"" , "clang/lib/AST/ASTContext.cpp", 3504, __extension__ __PRETTY_FUNCTION__ )); | |||
3505 | ||||
3506 | // Unique pointers, to guarantee there is only one pointer of a particular | |||
3507 | // structure. | |||
3508 | llvm::FoldingSetNodeID ID; | |||
3509 | ReferenceType::Profile(ID, T, false); | |||
3510 | ||||
3511 | void *InsertPos = nullptr; | |||
3512 | if (RValueReferenceType *RT = | |||
3513 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
3514 | return QualType(RT, 0); | |||
3515 | ||||
3516 | const auto *InnerRef = T->getAs<ReferenceType>(); | |||
3517 | ||||
3518 | // If the referencee type isn't canonical, this won't be a canonical type | |||
3519 | // either, so fill in the canonical type field. | |||
3520 | QualType Canonical; | |||
3521 | if (InnerRef || !T.isCanonical()) { | |||
3522 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); | |||
3523 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); | |||
3524 | ||||
3525 | // Get the new insert position for the node we care about. | |||
3526 | RValueReferenceType *NewIP = | |||
3527 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3528 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 3528, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
3529 | } | |||
3530 | ||||
3531 | auto *New = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); | |||
3532 | Types.push_back(New); | |||
3533 | RValueReferenceTypes.InsertNode(New, InsertPos); | |||
3534 | return QualType(New, 0); | |||
3535 | } | |||
3536 | ||||
3537 | /// getMemberPointerType - Return the uniqued reference to the type for a | |||
3538 | /// member pointer to the specified type, in the specified class. | |||
3539 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { | |||
3540 | // Unique pointers, to guarantee there is only one pointer of a particular | |||
3541 | // structure. | |||
3542 | llvm::FoldingSetNodeID ID; | |||
3543 | MemberPointerType::Profile(ID, T, Cls); | |||
3544 | ||||
3545 | void *InsertPos = nullptr; | |||
3546 | if (MemberPointerType *PT = | |||
3547 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
3548 | return QualType(PT, 0); | |||
3549 | ||||
3550 | // If the pointee or class type isn't canonical, this won't be a canonical | |||
3551 | // type either, so fill in the canonical type field. | |||
3552 | QualType Canonical; | |||
3553 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { | |||
3554 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); | |||
3555 | ||||
3556 | // Get the new insert position for the node we care about. | |||
3557 | MemberPointerType *NewIP = | |||
3558 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3559 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 3559, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
3560 | } | |||
3561 | auto *New = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); | |||
3562 | Types.push_back(New); | |||
3563 | MemberPointerTypes.InsertNode(New, InsertPos); | |||
3564 | return QualType(New, 0); | |||
3565 | } | |||
3566 | ||||
3567 | /// getConstantArrayType - Return the unique reference to the type for an | |||
3568 | /// array of the specified element type. | |||
3569 | QualType ASTContext::getConstantArrayType(QualType EltTy, | |||
3570 | const llvm::APInt &ArySizeIn, | |||
3571 | const Expr *SizeExpr, | |||
3572 | ArrayType::ArraySizeModifier ASM, | |||
3573 | unsigned IndexTypeQuals) const { | |||
3574 | assert((EltTy->isDependentType() ||(static_cast <bool> ((EltTy->isDependentType() || EltTy ->isIncompleteType() || EltTy->isConstantSizeType()) && "Constant array of VLAs is illegal!") ? void (0) : __assert_fail ("(EltTy->isDependentType() || EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && \"Constant array of VLAs is illegal!\"" , "clang/lib/AST/ASTContext.cpp", 3576, __extension__ __PRETTY_FUNCTION__ )) | |||
3575 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&(static_cast <bool> ((EltTy->isDependentType() || EltTy ->isIncompleteType() || EltTy->isConstantSizeType()) && "Constant array of VLAs is illegal!") ? void (0) : __assert_fail ("(EltTy->isDependentType() || EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && \"Constant array of VLAs is illegal!\"" , "clang/lib/AST/ASTContext.cpp", 3576, __extension__ __PRETTY_FUNCTION__ )) | |||
3576 | "Constant array of VLAs is illegal!")(static_cast <bool> ((EltTy->isDependentType() || EltTy ->isIncompleteType() || EltTy->isConstantSizeType()) && "Constant array of VLAs is illegal!") ? void (0) : __assert_fail ("(EltTy->isDependentType() || EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && \"Constant array of VLAs is illegal!\"" , "clang/lib/AST/ASTContext.cpp", 3576, __extension__ __PRETTY_FUNCTION__ )); | |||
3577 | ||||
3578 | // We only need the size as part of the type if it's instantiation-dependent. | |||
3579 | if (SizeExpr && !SizeExpr->isInstantiationDependent()) | |||
3580 | SizeExpr = nullptr; | |||
3581 | ||||
3582 | // Convert the array size into a canonical width matching the pointer size for | |||
3583 | // the target. | |||
3584 | llvm::APInt ArySize(ArySizeIn); | |||
3585 | ArySize = ArySize.zextOrTrunc(Target->getMaxPointerWidth()); | |||
3586 | ||||
3587 | llvm::FoldingSetNodeID ID; | |||
3588 | ConstantArrayType::Profile(ID, *this, EltTy, ArySize, SizeExpr, ASM, | |||
3589 | IndexTypeQuals); | |||
3590 | ||||
3591 | void *InsertPos = nullptr; | |||
3592 | if (ConstantArrayType *ATP = | |||
3593 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
3594 | return QualType(ATP, 0); | |||
3595 | ||||
3596 | // If the element type isn't canonical or has qualifiers, or the array bound | |||
3597 | // is instantiation-dependent, this won't be a canonical type either, so fill | |||
3598 | // in the canonical type field. | |||
3599 | QualType Canon; | |||
3600 | // FIXME: Check below should look for qualifiers behind sugar. | |||
3601 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers() || SizeExpr) { | |||
3602 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); | |||
3603 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, nullptr, | |||
3604 | ASM, IndexTypeQuals); | |||
3605 | Canon = getQualifiedType(Canon, canonSplit.Quals); | |||
3606 | ||||
3607 | // Get the new insert position for the node we care about. | |||
3608 | ConstantArrayType *NewIP = | |||
3609 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
3610 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 3610, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
3611 | } | |||
3612 | ||||
3613 | void *Mem = Allocate( | |||
3614 | ConstantArrayType::totalSizeToAlloc<const Expr *>(SizeExpr ? 1 : 0), | |||
3615 | TypeAlignment); | |||
3616 | auto *New = new (Mem) | |||
3617 | ConstantArrayType(EltTy, Canon, ArySize, SizeExpr, ASM, IndexTypeQuals); | |||
3618 | ConstantArrayTypes.InsertNode(New, InsertPos); | |||
3619 | Types.push_back(New); | |||
3620 | return QualType(New, 0); | |||
3621 | } | |||
3622 | ||||
3623 | /// getVariableArrayDecayedType - Turns the given type, which may be | |||
3624 | /// variably-modified, into the corresponding type with all the known | |||
3625 | /// sizes replaced with [*]. | |||
3626 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { | |||
3627 | // Vastly most common case. | |||
3628 | if (!type->isVariablyModifiedType()) return type; | |||
3629 | ||||
3630 | QualType result; | |||
3631 | ||||
3632 | SplitQualType split = type.getSplitDesugaredType(); | |||
3633 | const Type *ty = split.Ty; | |||
3634 | switch (ty->getTypeClass()) { | |||
3635 | #define TYPE(Class, Base) | |||
3636 | #define ABSTRACT_TYPE(Class, Base) | |||
3637 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: | |||
3638 | #include "clang/AST/TypeNodes.inc" | |||
3639 | llvm_unreachable("didn't desugar past all non-canonical types?")::llvm::llvm_unreachable_internal("didn't desugar past all non-canonical types?" , "clang/lib/AST/ASTContext.cpp", 3639); | |||
3640 | ||||
3641 | // These types should never be variably-modified. | |||
3642 | case Type::Builtin: | |||
3643 | case Type::Complex: | |||
3644 | case Type::Vector: | |||
3645 | case Type::DependentVector: | |||
3646 | case Type::ExtVector: | |||
3647 | case Type::DependentSizedExtVector: | |||
3648 | case Type::ConstantMatrix: | |||
3649 | case Type::DependentSizedMatrix: | |||
3650 | case Type::DependentAddressSpace: | |||
3651 | case Type::ObjCObject: | |||
3652 | case Type::ObjCInterface: | |||
3653 | case Type::ObjCObjectPointer: | |||
3654 | case Type::Record: | |||
3655 | case Type::Enum: | |||
3656 | case Type::UnresolvedUsing: | |||
3657 | case Type::TypeOfExpr: | |||
3658 | case Type::TypeOf: | |||
3659 | case Type::Decltype: | |||
3660 | case Type::UnaryTransform: | |||
3661 | case Type::DependentName: | |||
3662 | case Type::InjectedClassName: | |||
3663 | case Type::TemplateSpecialization: | |||
3664 | case Type::DependentTemplateSpecialization: | |||
3665 | case Type::TemplateTypeParm: | |||
3666 | case Type::SubstTemplateTypeParmPack: | |||
3667 | case Type::Auto: | |||
3668 | case Type::DeducedTemplateSpecialization: | |||
3669 | case Type::PackExpansion: | |||
3670 | case Type::BitInt: | |||
3671 | case Type::DependentBitInt: | |||
3672 | llvm_unreachable("type should never be variably-modified")::llvm::llvm_unreachable_internal("type should never be variably-modified" , "clang/lib/AST/ASTContext.cpp", 3672); | |||
3673 | ||||
3674 | // These types can be variably-modified but should never need to | |||
3675 | // further decay. | |||
3676 | case Type::FunctionNoProto: | |||
3677 | case Type::FunctionProto: | |||
3678 | case Type::BlockPointer: | |||
3679 | case Type::MemberPointer: | |||
3680 | case Type::Pipe: | |||
3681 | return type; | |||
3682 | ||||
3683 | // These types can be variably-modified. All these modifications | |||
3684 | // preserve structure except as noted by comments. | |||
3685 | // TODO: if we ever care about optimizing VLAs, there are no-op | |||
3686 | // optimizations available here. | |||
3687 | case Type::Pointer: | |||
3688 | result = getPointerType(getVariableArrayDecayedType( | |||
3689 | cast<PointerType>(ty)->getPointeeType())); | |||
3690 | break; | |||
3691 | ||||
3692 | case Type::LValueReference: { | |||
3693 | const auto *lv = cast<LValueReferenceType>(ty); | |||
3694 | result = getLValueReferenceType( | |||
3695 | getVariableArrayDecayedType(lv->getPointeeType()), | |||
3696 | lv->isSpelledAsLValue()); | |||
3697 | break; | |||
3698 | } | |||
3699 | ||||
3700 | case Type::RValueReference: { | |||
3701 | const auto *lv = cast<RValueReferenceType>(ty); | |||
3702 | result = getRValueReferenceType( | |||
3703 | getVariableArrayDecayedType(lv->getPointeeType())); | |||
3704 | break; | |||
3705 | } | |||
3706 | ||||
3707 | case Type::Atomic: { | |||
3708 | const auto *at = cast<AtomicType>(ty); | |||
3709 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); | |||
3710 | break; | |||
3711 | } | |||
3712 | ||||
3713 | case Type::ConstantArray: { | |||
3714 | const auto *cat = cast<ConstantArrayType>(ty); | |||
3715 | result = getConstantArrayType( | |||
3716 | getVariableArrayDecayedType(cat->getElementType()), | |||
3717 | cat->getSize(), | |||
3718 | cat->getSizeExpr(), | |||
3719 | cat->getSizeModifier(), | |||
3720 | cat->getIndexTypeCVRQualifiers()); | |||
3721 | break; | |||
3722 | } | |||
3723 | ||||
3724 | case Type::DependentSizedArray: { | |||
3725 | const auto *dat = cast<DependentSizedArrayType>(ty); | |||
3726 | result = getDependentSizedArrayType( | |||
3727 | getVariableArrayDecayedType(dat->getElementType()), | |||
3728 | dat->getSizeExpr(), | |||
3729 | dat->getSizeModifier(), | |||
3730 | dat->getIndexTypeCVRQualifiers(), | |||
3731 | dat->getBracketsRange()); | |||
3732 | break; | |||
3733 | } | |||
3734 | ||||
3735 | // Turn incomplete types into [*] types. | |||
3736 | case Type::IncompleteArray: { | |||
3737 | const auto *iat = cast<IncompleteArrayType>(ty); | |||
3738 | result = getVariableArrayType( | |||
3739 | getVariableArrayDecayedType(iat->getElementType()), | |||
3740 | /*size*/ nullptr, | |||
3741 | ArrayType::Normal, | |||
3742 | iat->getIndexTypeCVRQualifiers(), | |||
3743 | SourceRange()); | |||
3744 | break; | |||
3745 | } | |||
3746 | ||||
3747 | // Turn VLA types into [*] types. | |||
3748 | case Type::VariableArray: { | |||
3749 | const auto *vat = cast<VariableArrayType>(ty); | |||
3750 | result = getVariableArrayType( | |||
3751 | getVariableArrayDecayedType(vat->getElementType()), | |||
3752 | /*size*/ nullptr, | |||
3753 | ArrayType::Star, | |||
3754 | vat->getIndexTypeCVRQualifiers(), | |||
3755 | vat->getBracketsRange()); | |||
3756 | break; | |||
3757 | } | |||
3758 | } | |||
3759 | ||||
3760 | // Apply the top-level qualifiers from the original. | |||
3761 | return getQualifiedType(result, split.Quals); | |||
3762 | } | |||
3763 | ||||
3764 | /// getVariableArrayType - Returns a non-unique reference to the type for a | |||
3765 | /// variable array of the specified element type. | |||
3766 | QualType ASTContext::getVariableArrayType(QualType EltTy, | |||
3767 | Expr *NumElts, | |||
3768 | ArrayType::ArraySizeModifier ASM, | |||
3769 | unsigned IndexTypeQuals, | |||
3770 | SourceRange Brackets) const { | |||
3771 | // Since we don't unique expressions, it isn't possible to unique VLA's | |||
3772 | // that have an expression provided for their size. | |||
3773 | QualType Canon; | |||
3774 | ||||
3775 | // Be sure to pull qualifiers off the element type. | |||
3776 | // FIXME: Check below should look for qualifiers behind sugar. | |||
3777 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { | |||
3778 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); | |||
3779 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, | |||
3780 | IndexTypeQuals, Brackets); | |||
3781 | Canon = getQualifiedType(Canon, canonSplit.Quals); | |||
3782 | } | |||
3783 | ||||
3784 | auto *New = new (*this, TypeAlignment) | |||
3785 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); | |||
3786 | ||||
3787 | VariableArrayTypes.push_back(New); | |||
3788 | Types.push_back(New); | |||
3789 | return QualType(New, 0); | |||
3790 | } | |||
3791 | ||||
3792 | /// getDependentSizedArrayType - Returns a non-unique reference to | |||
3793 | /// the type for a dependently-sized array of the specified element | |||
3794 | /// type. | |||
3795 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, | |||
3796 | Expr *numElements, | |||
3797 | ArrayType::ArraySizeModifier ASM, | |||
3798 | unsigned elementTypeQuals, | |||
3799 | SourceRange brackets) const { | |||
3800 | assert((!numElements || numElements->isTypeDependent() ||(static_cast <bool> ((!numElements || numElements->isTypeDependent () || numElements->isValueDependent()) && "Size must be type- or value-dependent!" ) ? void (0) : __assert_fail ("(!numElements || numElements->isTypeDependent() || numElements->isValueDependent()) && \"Size must be type- or value-dependent!\"" , "clang/lib/AST/ASTContext.cpp", 3802, __extension__ __PRETTY_FUNCTION__ )) | |||
3801 | numElements->isValueDependent()) &&(static_cast <bool> ((!numElements || numElements->isTypeDependent () || numElements->isValueDependent()) && "Size must be type- or value-dependent!" ) ? void (0) : __assert_fail ("(!numElements || numElements->isTypeDependent() || numElements->isValueDependent()) && \"Size must be type- or value-dependent!\"" , "clang/lib/AST/ASTContext.cpp", 3802, __extension__ __PRETTY_FUNCTION__ )) | |||
3802 | "Size must be type- or value-dependent!")(static_cast <bool> ((!numElements || numElements->isTypeDependent () || numElements->isValueDependent()) && "Size must be type- or value-dependent!" ) ? void (0) : __assert_fail ("(!numElements || numElements->isTypeDependent() || numElements->isValueDependent()) && \"Size must be type- or value-dependent!\"" , "clang/lib/AST/ASTContext.cpp", 3802, __extension__ __PRETTY_FUNCTION__ )); | |||
3803 | ||||
3804 | // Dependently-sized array types that do not have a specified number | |||
3805 | // of elements will have their sizes deduced from a dependent | |||
3806 | // initializer. We do no canonicalization here at all, which is okay | |||
3807 | // because they can't be used in most locations. | |||
3808 | if (!numElements) { | |||
3809 | auto *newType | |||
3810 | = new (*this, TypeAlignment) | |||
3811 | DependentSizedArrayType(*this, elementType, QualType(), | |||
3812 | numElements, ASM, elementTypeQuals, | |||
3813 | brackets); | |||
3814 | Types.push_back(newType); | |||
3815 | return QualType(newType, 0); | |||
3816 | } | |||
3817 | ||||
3818 | // Otherwise, we actually build a new type every time, but we | |||
3819 | // also build a canonical type. | |||
3820 | ||||
3821 | SplitQualType canonElementType = getCanonicalType(elementType).split(); | |||
3822 | ||||
3823 | void *insertPos = nullptr; | |||
3824 | llvm::FoldingSetNodeID ID; | |||
3825 | DependentSizedArrayType::Profile(ID, *this, | |||
3826 | QualType(canonElementType.Ty, 0), | |||
3827 | ASM, elementTypeQuals, numElements); | |||
3828 | ||||
3829 | // Look for an existing type with these properties. | |||
3830 | DependentSizedArrayType *canonTy = | |||
3831 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); | |||
3832 | ||||
3833 | // If we don't have one, build one. | |||
3834 | if (!canonTy) { | |||
3835 | canonTy = new (*this, TypeAlignment) | |||
3836 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), | |||
3837 | QualType(), numElements, ASM, elementTypeQuals, | |||
3838 | brackets); | |||
3839 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); | |||
3840 | Types.push_back(canonTy); | |||
3841 | } | |||
3842 | ||||
3843 | // Apply qualifiers from the element type to the array. | |||
3844 | QualType canon = getQualifiedType(QualType(canonTy,0), | |||
3845 | 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 | |||
3856 | = new (*this, TypeAlignment) | |||
3857 | DependentSizedArrayType(*this, elementType, canon, numElements, | |||
3858 | ASM, elementTypeQuals, brackets); | |||
3859 | Types.push_back(sugaredType); | |||
3860 | return QualType(sugaredType, 0); | |||
3861 | } | |||
3862 | ||||
3863 | QualType ASTContext::getIncompleteArrayType(QualType elementType, | |||
3864 | ArrayType::ArraySizeModifier ASM, | |||
3865 | unsigned elementTypeQuals) const { | |||
3866 | llvm::FoldingSetNodeID ID; | |||
3867 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); | |||
3868 | ||||
3869 | void *insertPos = nullptr; | |||
3870 | if (IncompleteArrayType *iat = | |||
3871 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) | |||
3872 | return QualType(iat, 0); | |||
3873 | ||||
3874 | // If the element type isn't canonical, this won't be a canonical type | |||
3875 | // either, so fill in the canonical type field. We also have to pull | |||
3876 | // qualifiers off the element type. | |||
3877 | QualType canon; | |||
3878 | ||||
3879 | // FIXME: Check below should look for qualifiers behind sugar. | |||
3880 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { | |||
3881 | SplitQualType canonSplit = getCanonicalType(elementType).split(); | |||
3882 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), | |||
3883 | ASM, elementTypeQuals); | |||
3884 | canon = getQualifiedType(canon, canonSplit.Quals); | |||
3885 | ||||
3886 | // Get the new insert position for the node we care about. | |||
3887 | IncompleteArrayType *existing = | |||
3888 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); | |||
3889 | assert(!existing && "Shouldn't be in the map!")(static_cast <bool> (!existing && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!existing && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 3889, __extension__ __PRETTY_FUNCTION__ )); (void) existing; | |||
3890 | } | |||
3891 | ||||
3892 | auto *newType = new (*this, TypeAlignment) | |||
3893 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); | |||
3894 | ||||
3895 | IncompleteArrayTypes.InsertNode(newType, insertPos); | |||
3896 | Types.push_back(newType); | |||
3897 | return QualType(newType, 0); | |||
3898 | } | |||
3899 | ||||
3900 | ASTContext::BuiltinVectorTypeInfo | |||
3901 | ASTContext::getBuiltinVectorTypeInfo(const BuiltinType *Ty) const { | |||
3902 | #define SVE_INT_ELTTY(BITS, ELTS, SIGNED, NUMVECTORS){getIntTypeForBitwidth(BITS, SIGNED), llvm::ElementCount::getScalable (ELTS), NUMVECTORS}; \ | |||
3903 | {getIntTypeForBitwidth(BITS, SIGNED), llvm::ElementCount::getScalable(ELTS), \ | |||
3904 | NUMVECTORS}; | |||
3905 | ||||
3906 | #define SVE_ELTTY(ELTTY, ELTS, NUMVECTORS){ELTTY, llvm::ElementCount::getScalable(ELTS), NUMVECTORS}; \ | |||
3907 | {ELTTY, llvm::ElementCount::getScalable(ELTS), NUMVECTORS}; | |||
3908 | ||||
3909 | switch (Ty->getKind()) { | |||
3910 | default: | |||
3911 | llvm_unreachable("Unsupported builtin vector type")::llvm::llvm_unreachable_internal("Unsupported builtin vector type" , "clang/lib/AST/ASTContext.cpp", 3911); | |||
3912 | case BuiltinType::SveInt8: | |||
3913 | return SVE_INT_ELTTY(8, 16, true, 1){getIntTypeForBitwidth(8, true), llvm::ElementCount::getScalable (16), 1};; | |||
3914 | case BuiltinType::SveUint8: | |||
3915 | return SVE_INT_ELTTY(8, 16, false, 1){getIntTypeForBitwidth(8, false), llvm::ElementCount::getScalable (16), 1};; | |||
3916 | case BuiltinType::SveInt8x2: | |||
3917 | return SVE_INT_ELTTY(8, 16, true, 2){getIntTypeForBitwidth(8, true), llvm::ElementCount::getScalable (16), 2};; | |||
3918 | case BuiltinType::SveUint8x2: | |||
3919 | return SVE_INT_ELTTY(8, 16, false, 2){getIntTypeForBitwidth(8, false), llvm::ElementCount::getScalable (16), 2};; | |||
3920 | case BuiltinType::SveInt8x3: | |||
3921 | return SVE_INT_ELTTY(8, 16, true, 3){getIntTypeForBitwidth(8, true), llvm::ElementCount::getScalable (16), 3};; | |||
3922 | case BuiltinType::SveUint8x3: | |||
3923 | return SVE_INT_ELTTY(8, 16, false, 3){getIntTypeForBitwidth(8, false), llvm::ElementCount::getScalable (16), 3};; | |||
3924 | case BuiltinType::SveInt8x4: | |||
3925 | return SVE_INT_ELTTY(8, 16, true, 4){getIntTypeForBitwidth(8, true), llvm::ElementCount::getScalable (16), 4};; | |||
3926 | case BuiltinType::SveUint8x4: | |||
3927 | return SVE_INT_ELTTY(8, 16, false, 4){getIntTypeForBitwidth(8, false), llvm::ElementCount::getScalable (16), 4};; | |||
3928 | case BuiltinType::SveInt16: | |||
3929 | return SVE_INT_ELTTY(16, 8, true, 1){getIntTypeForBitwidth(16, true), llvm::ElementCount::getScalable (8), 1};; | |||
3930 | case BuiltinType::SveUint16: | |||
3931 | return SVE_INT_ELTTY(16, 8, false, 1){getIntTypeForBitwidth(16, false), llvm::ElementCount::getScalable (8), 1};; | |||
3932 | case BuiltinType::SveInt16x2: | |||
3933 | return SVE_INT_ELTTY(16, 8, true, 2){getIntTypeForBitwidth(16, true), llvm::ElementCount::getScalable (8), 2};; | |||
3934 | case BuiltinType::SveUint16x2: | |||
3935 | return SVE_INT_ELTTY(16, 8, false, 2){getIntTypeForBitwidth(16, false), llvm::ElementCount::getScalable (8), 2};; | |||
3936 | case BuiltinType::SveInt16x3: | |||
3937 | return SVE_INT_ELTTY(16, 8, true, 3){getIntTypeForBitwidth(16, true), llvm::ElementCount::getScalable (8), 3};; | |||
3938 | case BuiltinType::SveUint16x3: | |||
3939 | return SVE_INT_ELTTY(16, 8, false, 3){getIntTypeForBitwidth(16, false), llvm::ElementCount::getScalable (8), 3};; | |||
3940 | case BuiltinType::SveInt16x4: | |||
3941 | return SVE_INT_ELTTY(16, 8, true, 4){getIntTypeForBitwidth(16, true), llvm::ElementCount::getScalable (8), 4};; | |||
3942 | case BuiltinType::SveUint16x4: | |||
3943 | return SVE_INT_ELTTY(16, 8, false, 4){getIntTypeForBitwidth(16, false), llvm::ElementCount::getScalable (8), 4};; | |||
3944 | case BuiltinType::SveInt32: | |||
3945 | return SVE_INT_ELTTY(32, 4, true, 1){getIntTypeForBitwidth(32, true), llvm::ElementCount::getScalable (4), 1};; | |||
3946 | case BuiltinType::SveUint32: | |||
3947 | return SVE_INT_ELTTY(32, 4, false, 1){getIntTypeForBitwidth(32, false), llvm::ElementCount::getScalable (4), 1};; | |||
3948 | case BuiltinType::SveInt32x2: | |||
3949 | return SVE_INT_ELTTY(32, 4, true, 2){getIntTypeForBitwidth(32, true), llvm::ElementCount::getScalable (4), 2};; | |||
3950 | case BuiltinType::SveUint32x2: | |||
3951 | return SVE_INT_ELTTY(32, 4, false, 2){getIntTypeForBitwidth(32, false), llvm::ElementCount::getScalable (4), 2};; | |||
3952 | case BuiltinType::SveInt32x3: | |||
3953 | return SVE_INT_ELTTY(32, 4, true, 3){getIntTypeForBitwidth(32, true), llvm::ElementCount::getScalable (4), 3};; | |||
3954 | case BuiltinType::SveUint32x3: | |||
3955 | return SVE_INT_ELTTY(32, 4, false, 3){getIntTypeForBitwidth(32, false), llvm::ElementCount::getScalable (4), 3};; | |||
3956 | case BuiltinType::SveInt32x4: | |||
3957 | return SVE_INT_ELTTY(32, 4, true, 4){getIntTypeForBitwidth(32, true), llvm::ElementCount::getScalable (4), 4};; | |||
3958 | case BuiltinType::SveUint32x4: | |||
3959 | return SVE_INT_ELTTY(32, 4, false, 4){getIntTypeForBitwidth(32, false), llvm::ElementCount::getScalable (4), 4};; | |||
3960 | case BuiltinType::SveInt64: | |||
3961 | return SVE_INT_ELTTY(64, 2, true, 1){getIntTypeForBitwidth(64, true), llvm::ElementCount::getScalable (2), 1};; | |||
3962 | case BuiltinType::SveUint64: | |||
3963 | return SVE_INT_ELTTY(64, 2, false, 1){getIntTypeForBitwidth(64, false), llvm::ElementCount::getScalable (2), 1};; | |||
3964 | case BuiltinType::SveInt64x2: | |||
3965 | return SVE_INT_ELTTY(64, 2, true, 2){getIntTypeForBitwidth(64, true), llvm::ElementCount::getScalable (2), 2};; | |||
3966 | case BuiltinType::SveUint64x2: | |||
3967 | return SVE_INT_ELTTY(64, 2, false, 2){getIntTypeForBitwidth(64, false), llvm::ElementCount::getScalable (2), 2};; | |||
3968 | case BuiltinType::SveInt64x3: | |||
3969 | return SVE_INT_ELTTY(64, 2, true, 3){getIntTypeForBitwidth(64, true), llvm::ElementCount::getScalable (2), 3};; | |||
3970 | case BuiltinType::SveUint64x3: | |||
3971 | return SVE_INT_ELTTY(64, 2, false, 3){getIntTypeForBitwidth(64, false), llvm::ElementCount::getScalable (2), 3};; | |||
3972 | case BuiltinType::SveInt64x4: | |||
3973 | return SVE_INT_ELTTY(64, 2, true, 4){getIntTypeForBitwidth(64, true), llvm::ElementCount::getScalable (2), 4};; | |||
3974 | case BuiltinType::SveUint64x4: | |||
3975 | return SVE_INT_ELTTY(64, 2, false, 4){getIntTypeForBitwidth(64, false), llvm::ElementCount::getScalable (2), 4};; | |||
3976 | case BuiltinType::SveBool: | |||
3977 | return SVE_ELTTY(BoolTy, 16, 1){BoolTy, llvm::ElementCount::getScalable(16), 1};; | |||
3978 | case BuiltinType::SveBoolx2: | |||
3979 | return SVE_ELTTY(BoolTy, 16, 2){BoolTy, llvm::ElementCount::getScalable(16), 2};; | |||
3980 | case BuiltinType::SveBoolx4: | |||
3981 | return SVE_ELTTY(BoolTy, 16, 4){BoolTy, llvm::ElementCount::getScalable(16), 4};; | |||
3982 | case BuiltinType::SveFloat16: | |||
3983 | return SVE_ELTTY(HalfTy, 8, 1){HalfTy, llvm::ElementCount::getScalable(8), 1};; | |||
3984 | case BuiltinType::SveFloat16x2: | |||
3985 | return SVE_ELTTY(HalfTy, 8, 2){HalfTy, llvm::ElementCount::getScalable(8), 2};; | |||
3986 | case BuiltinType::SveFloat16x3: | |||
3987 | return SVE_ELTTY(HalfTy, 8, 3){HalfTy, llvm::ElementCount::getScalable(8), 3};; | |||
3988 | case BuiltinType::SveFloat16x4: | |||
3989 | return SVE_ELTTY(HalfTy, 8, 4){HalfTy, llvm::ElementCount::getScalable(8), 4};; | |||
3990 | case BuiltinType::SveFloat32: | |||
3991 | return SVE_ELTTY(FloatTy, 4, 1){FloatTy, llvm::ElementCount::getScalable(4), 1};; | |||
3992 | case BuiltinType::SveFloat32x2: | |||
3993 | return SVE_ELTTY(FloatTy, 4, 2){FloatTy, llvm::ElementCount::getScalable(4), 2};; | |||
3994 | case BuiltinType::SveFloat32x3: | |||
3995 | return SVE_ELTTY(FloatTy, 4, 3){FloatTy, llvm::ElementCount::getScalable(4), 3};; | |||
3996 | case BuiltinType::SveFloat32x4: | |||
3997 | return SVE_ELTTY(FloatTy, 4, 4){FloatTy, llvm::ElementCount::getScalable(4), 4};; | |||
3998 | case BuiltinType::SveFloat64: | |||
3999 | return SVE_ELTTY(DoubleTy, 2, 1){DoubleTy, llvm::ElementCount::getScalable(2), 1};; | |||
4000 | case BuiltinType::SveFloat64x2: | |||
4001 | return SVE_ELTTY(DoubleTy, 2, 2){DoubleTy, llvm::ElementCount::getScalable(2), 2};; | |||
4002 | case BuiltinType::SveFloat64x3: | |||
4003 | return SVE_ELTTY(DoubleTy, 2, 3){DoubleTy, llvm::ElementCount::getScalable(2), 3};; | |||
4004 | case BuiltinType::SveFloat64x4: | |||
4005 | return SVE_ELTTY(DoubleTy, 2, 4){DoubleTy, llvm::ElementCount::getScalable(2), 4};; | |||
4006 | case BuiltinType::SveBFloat16: | |||
4007 | return SVE_ELTTY(BFloat16Ty, 8, 1){BFloat16Ty, llvm::ElementCount::getScalable(8), 1};; | |||
4008 | case BuiltinType::SveBFloat16x2: | |||
4009 | return SVE_ELTTY(BFloat16Ty, 8, 2){BFloat16Ty, llvm::ElementCount::getScalable(8), 2};; | |||
4010 | case BuiltinType::SveBFloat16x3: | |||
4011 | return SVE_ELTTY(BFloat16Ty, 8, 3){BFloat16Ty, llvm::ElementCount::getScalable(8), 3};; | |||
4012 | case BuiltinType::SveBFloat16x4: | |||
4013 | return SVE_ELTTY(BFloat16Ty, 8, 4){BFloat16Ty, llvm::ElementCount::getScalable(8), 4};; | |||
4014 | #define RVV_VECTOR_TYPE_INT(Name, Id, SingletonId, NumEls, ElBits, NF, \ | |||
4015 | IsSigned) \ | |||
4016 | case BuiltinType::Id: \ | |||
4017 | return {getIntTypeForBitwidth(ElBits, IsSigned), \ | |||
4018 | llvm::ElementCount::getScalable(NumEls), NF}; | |||
4019 | #define RVV_VECTOR_TYPE_FLOAT(Name, Id, SingletonId, NumEls, ElBits, NF) \ | |||
4020 | case BuiltinType::Id: \ | |||
4021 | return {ElBits == 16 ? Float16Ty : (ElBits == 32 ? FloatTy : DoubleTy), \ | |||
4022 | llvm::ElementCount::getScalable(NumEls), NF}; | |||
4023 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, NumEls) \ | |||
4024 | case BuiltinType::Id: \ | |||
4025 | return {BoolTy, llvm::ElementCount::getScalable(NumEls), 1}; | |||
4026 | #include "clang/Basic/RISCVVTypes.def" | |||
4027 | } | |||
4028 | } | |||
4029 | ||||
4030 | /// getExternrefType - Return a WebAssembly externref type, which represents an | |||
4031 | /// opaque reference to a host value. | |||
4032 | QualType ASTContext::getWebAssemblyExternrefType() const { | |||
4033 | if (Target->getTriple().isWasm() && Target->hasFeature("reference-types")) { | |||
4034 | #define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS) \ | |||
4035 | if (BuiltinType::Id == BuiltinType::WasmExternRef) \ | |||
4036 | return SingletonId; | |||
4037 | #include "clang/Basic/WebAssemblyReferenceTypes.def" | |||
4038 | } | |||
4039 | llvm_unreachable(::llvm::llvm_unreachable_internal("shouldn't try to generate type externref outside WebAssembly target" , "clang/lib/AST/ASTContext.cpp", 4040) | |||
4040 | "shouldn't try to generate type externref outside WebAssembly target")::llvm::llvm_unreachable_internal("shouldn't try to generate type externref outside WebAssembly target" , "clang/lib/AST/ASTContext.cpp", 4040); | |||
4041 | } | |||
4042 | ||||
4043 | /// getScalableVectorType - Return the unique reference to a scalable vector | |||
4044 | /// type of the specified element type and size. VectorType must be a built-in | |||
4045 | /// type. | |||
4046 | QualType ASTContext::getScalableVectorType(QualType EltTy, | |||
4047 | unsigned NumElts) const { | |||
4048 | if (Target->hasAArch64SVETypes()) { | |||
4049 | uint64_t EltTySize = getTypeSize(EltTy); | |||
4050 | #define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId, NumEls, ElBits, \ | |||
4051 | IsSigned, IsFP, IsBF) \ | |||
4052 | if (!EltTy->isBooleanType() && \ | |||
4053 | ((EltTy->hasIntegerRepresentation() && \ | |||
4054 | EltTy->hasSignedIntegerRepresentation() == IsSigned) || \ | |||
4055 | (EltTy->hasFloatingRepresentation() && !EltTy->isBFloat16Type() && \ | |||
4056 | IsFP && !IsBF) || \ | |||
4057 | (EltTy->hasFloatingRepresentation() && EltTy->isBFloat16Type() && \ | |||
4058 | IsBF && !IsFP)) && \ | |||
4059 | EltTySize == ElBits && NumElts == NumEls) { \ | |||
4060 | return SingletonId; \ | |||
4061 | } | |||
4062 | #define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId, NumEls) \ | |||
4063 | if (EltTy->isBooleanType() && NumElts == NumEls) \ | |||
4064 | return SingletonId; | |||
4065 | #define SVE_OPAQUE_TYPE(Name, MangledName, Id, SingleTonId) | |||
4066 | #include "clang/Basic/AArch64SVEACLETypes.def" | |||
4067 | } else if (Target->hasRISCVVTypes()) { | |||
4068 | uint64_t EltTySize = getTypeSize(EltTy); | |||
4069 | #define RVV_VECTOR_TYPE(Name, Id, SingletonId, NumEls, ElBits, NF, IsSigned, \ | |||
4070 | IsFP) \ | |||
4071 | if (!EltTy->isBooleanType() && \ | |||
4072 | ((EltTy->hasIntegerRepresentation() && \ | |||
4073 | EltTy->hasSignedIntegerRepresentation() == IsSigned) || \ | |||
4074 | (EltTy->hasFloatingRepresentation() && IsFP)) && \ | |||
4075 | EltTySize == ElBits && NumElts == NumEls) \ | |||
4076 | return SingletonId; | |||
4077 | #define RVV_PREDICATE_TYPE(Name, Id, SingletonId, NumEls) \ | |||
4078 | if (EltTy->isBooleanType() && NumElts == NumEls) \ | |||
4079 | return SingletonId; | |||
4080 | #include "clang/Basic/RISCVVTypes.def" | |||
4081 | } | |||
4082 | return QualType(); | |||
4083 | } | |||
4084 | ||||
4085 | /// getVectorType - Return the unique reference to a vector type of | |||
4086 | /// the specified element type and size. VectorType must be a built-in type. | |||
4087 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, | |||
4088 | VectorType::VectorKind VecKind) const { | |||
4089 | assert(vecType->isBuiltinType() ||(static_cast <bool> (vecType->isBuiltinType() || (vecType ->isBitIntType() && llvm::isPowerOf2_32(vecType-> getAs<BitIntType>()->getNumBits()) && vecType ->getAs<BitIntType>()->getNumBits() >= 8)) ? void (0) : __assert_fail ("vecType->isBuiltinType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) && vecType->getAs<BitIntType>()->getNumBits() >= 8)" , "clang/lib/AST/ASTContext.cpp", 4093, __extension__ __PRETTY_FUNCTION__ )) | |||
4090 | (vecType->isBitIntType() &&(static_cast <bool> (vecType->isBuiltinType() || (vecType ->isBitIntType() && llvm::isPowerOf2_32(vecType-> getAs<BitIntType>()->getNumBits()) && vecType ->getAs<BitIntType>()->getNumBits() >= 8)) ? void (0) : __assert_fail ("vecType->isBuiltinType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) && vecType->getAs<BitIntType>()->getNumBits() >= 8)" , "clang/lib/AST/ASTContext.cpp", 4093, __extension__ __PRETTY_FUNCTION__ )) | |||
4091 | // Only support _BitInt elements with byte-sized power of 2 NumBits.(static_cast <bool> (vecType->isBuiltinType() || (vecType ->isBitIntType() && llvm::isPowerOf2_32(vecType-> getAs<BitIntType>()->getNumBits()) && vecType ->getAs<BitIntType>()->getNumBits() >= 8)) ? void (0) : __assert_fail ("vecType->isBuiltinType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) && vecType->getAs<BitIntType>()->getNumBits() >= 8)" , "clang/lib/AST/ASTContext.cpp", 4093, __extension__ __PRETTY_FUNCTION__ )) | |||
4092 | llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) &&(static_cast <bool> (vecType->isBuiltinType() || (vecType ->isBitIntType() && llvm::isPowerOf2_32(vecType-> getAs<BitIntType>()->getNumBits()) && vecType ->getAs<BitIntType>()->getNumBits() >= 8)) ? void (0) : __assert_fail ("vecType->isBuiltinType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) && vecType->getAs<BitIntType>()->getNumBits() >= 8)" , "clang/lib/AST/ASTContext.cpp", 4093, __extension__ __PRETTY_FUNCTION__ )) | |||
4093 | vecType->getAs<BitIntType>()->getNumBits() >= 8))(static_cast <bool> (vecType->isBuiltinType() || (vecType ->isBitIntType() && llvm::isPowerOf2_32(vecType-> getAs<BitIntType>()->getNumBits()) && vecType ->getAs<BitIntType>()->getNumBits() >= 8)) ? void (0) : __assert_fail ("vecType->isBuiltinType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) && vecType->getAs<BitIntType>()->getNumBits() >= 8)" , "clang/lib/AST/ASTContext.cpp", 4093, __extension__ __PRETTY_FUNCTION__ )); | |||
4094 | ||||
4095 | // Check if we've already instantiated a vector of this type. | |||
4096 | llvm::FoldingSetNodeID ID; | |||
4097 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); | |||
4098 | ||||
4099 | void *InsertPos = nullptr; | |||
4100 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
4101 | return QualType(VTP, 0); | |||
4102 | ||||
4103 | // If the element type isn't canonical, this won't be a canonical type either, | |||
4104 | // so fill in the canonical type field. | |||
4105 | QualType Canonical; | |||
4106 | if (!vecType.isCanonical()) { | |||
4107 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); | |||
4108 | ||||
4109 | // Get the new insert position for the node we care about. | |||
4110 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4111 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 4111, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
4112 | } | |||
4113 | auto *New = new (*this, TypeAlignment) | |||
4114 | VectorType(vecType, NumElts, Canonical, VecKind); | |||
4115 | VectorTypes.InsertNode(New, InsertPos); | |||
4116 | Types.push_back(New); | |||
4117 | return QualType(New, 0); | |||
4118 | } | |||
4119 | ||||
4120 | QualType | |||
4121 | ASTContext::getDependentVectorType(QualType VecType, Expr *SizeExpr, | |||
4122 | SourceLocation AttrLoc, | |||
4123 | VectorType::VectorKind VecKind) const { | |||
4124 | llvm::FoldingSetNodeID ID; | |||
4125 | DependentVectorType::Profile(ID, *this, getCanonicalType(VecType), SizeExpr, | |||
4126 | VecKind); | |||
4127 | void *InsertPos = nullptr; | |||
4128 | DependentVectorType *Canon = | |||
4129 | DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4130 | DependentVectorType *New; | |||
4131 | ||||
4132 | if (Canon) { | |||
4133 | New = new (*this, TypeAlignment) DependentVectorType( | |||
4134 | *this, VecType, QualType(Canon, 0), SizeExpr, AttrLoc, VecKind); | |||
4135 | } else { | |||
4136 | QualType CanonVecTy = getCanonicalType(VecType); | |||
4137 | if (CanonVecTy == VecType) { | |||
4138 | New = new (*this, TypeAlignment) DependentVectorType( | |||
4139 | *this, VecType, QualType(), SizeExpr, AttrLoc, VecKind); | |||
4140 | ||||
4141 | DependentVectorType *CanonCheck = | |||
4142 | DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4143 | assert(!CanonCheck &&(static_cast <bool> (!CanonCheck && "Dependent-sized vector_size canonical type broken" ) ? void (0) : __assert_fail ("!CanonCheck && \"Dependent-sized vector_size canonical type broken\"" , "clang/lib/AST/ASTContext.cpp", 4144, __extension__ __PRETTY_FUNCTION__ )) | |||
4144 | "Dependent-sized vector_size canonical type broken")(static_cast <bool> (!CanonCheck && "Dependent-sized vector_size canonical type broken" ) ? void (0) : __assert_fail ("!CanonCheck && \"Dependent-sized vector_size canonical type broken\"" , "clang/lib/AST/ASTContext.cpp", 4144, __extension__ __PRETTY_FUNCTION__ )); | |||
4145 | (void)CanonCheck; | |||
4146 | DependentVectorTypes.InsertNode(New, InsertPos); | |||
4147 | } else { | |||
4148 | QualType CanonTy = getDependentVectorType(CanonVecTy, SizeExpr, | |||
4149 | SourceLocation(), VecKind); | |||
4150 | New = new (*this, TypeAlignment) DependentVectorType( | |||
4151 | *this, VecType, CanonTy, SizeExpr, AttrLoc, VecKind); | |||
4152 | } | |||
4153 | } | |||
4154 | ||||
4155 | Types.push_back(New); | |||
4156 | return QualType(New, 0); | |||
4157 | } | |||
4158 | ||||
4159 | /// getExtVectorType - Return the unique reference to an extended vector type of | |||
4160 | /// the specified element type and size. VectorType must be a built-in type. | |||
4161 | QualType ASTContext::getExtVectorType(QualType vecType, | |||
4162 | unsigned NumElts) const { | |||
4163 | assert(vecType->isBuiltinType() || vecType->isDependentType() ||(static_cast <bool> (vecType->isBuiltinType() || vecType ->isDependentType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()-> getNumBits()) && vecType->getAs<BitIntType>( )->getNumBits() >= 8)) ? void (0) : __assert_fail ("vecType->isBuiltinType() || vecType->isDependentType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) && vecType->getAs<BitIntType>()->getNumBits() >= 8)" , "clang/lib/AST/ASTContext.cpp", 4167, __extension__ __PRETTY_FUNCTION__ )) | |||
4164 | (vecType->isBitIntType() &&(static_cast <bool> (vecType->isBuiltinType() || vecType ->isDependentType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()-> getNumBits()) && vecType->getAs<BitIntType>( )->getNumBits() >= 8)) ? void (0) : __assert_fail ("vecType->isBuiltinType() || vecType->isDependentType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) && vecType->getAs<BitIntType>()->getNumBits() >= 8)" , "clang/lib/AST/ASTContext.cpp", 4167, __extension__ __PRETTY_FUNCTION__ )) | |||
4165 | // Only support _BitInt elements with byte-sized power of 2 NumBits.(static_cast <bool> (vecType->isBuiltinType() || vecType ->isDependentType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()-> getNumBits()) && vecType->getAs<BitIntType>( )->getNumBits() >= 8)) ? void (0) : __assert_fail ("vecType->isBuiltinType() || vecType->isDependentType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) && vecType->getAs<BitIntType>()->getNumBits() >= 8)" , "clang/lib/AST/ASTContext.cpp", 4167, __extension__ __PRETTY_FUNCTION__ )) | |||
4166 | llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) &&(static_cast <bool> (vecType->isBuiltinType() || vecType ->isDependentType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()-> getNumBits()) && vecType->getAs<BitIntType>( )->getNumBits() >= 8)) ? void (0) : __assert_fail ("vecType->isBuiltinType() || vecType->isDependentType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) && vecType->getAs<BitIntType>()->getNumBits() >= 8)" , "clang/lib/AST/ASTContext.cpp", 4167, __extension__ __PRETTY_FUNCTION__ )) | |||
4167 | vecType->getAs<BitIntType>()->getNumBits() >= 8))(static_cast <bool> (vecType->isBuiltinType() || vecType ->isDependentType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()-> getNumBits()) && vecType->getAs<BitIntType>( )->getNumBits() >= 8)) ? void (0) : __assert_fail ("vecType->isBuiltinType() || vecType->isDependentType() || (vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->getAs<BitIntType>()->getNumBits()) && vecType->getAs<BitIntType>()->getNumBits() >= 8)" , "clang/lib/AST/ASTContext.cpp", 4167, __extension__ __PRETTY_FUNCTION__ )); | |||
4168 | ||||
4169 | // Check if we've already instantiated a vector of this type. | |||
4170 | llvm::FoldingSetNodeID ID; | |||
4171 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, | |||
4172 | VectorType::GenericVector); | |||
4173 | void *InsertPos = nullptr; | |||
4174 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
4175 | return QualType(VTP, 0); | |||
4176 | ||||
4177 | // If the element type isn't canonical, this won't be a canonical type either, | |||
4178 | // so fill in the canonical type field. | |||
4179 | QualType Canonical; | |||
4180 | if (!vecType.isCanonical()) { | |||
4181 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); | |||
4182 | ||||
4183 | // Get the new insert position for the node we care about. | |||
4184 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4185 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 4185, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
4186 | } | |||
4187 | auto *New = new (*this, TypeAlignment) | |||
4188 | ExtVectorType(vecType, NumElts, Canonical); | |||
4189 | VectorTypes.InsertNode(New, InsertPos); | |||
4190 | Types.push_back(New); | |||
4191 | return QualType(New, 0); | |||
4192 | } | |||
4193 | ||||
4194 | QualType | |||
4195 | ASTContext::getDependentSizedExtVectorType(QualType vecType, | |||
4196 | Expr *SizeExpr, | |||
4197 | SourceLocation AttrLoc) const { | |||
4198 | llvm::FoldingSetNodeID ID; | |||
4199 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), | |||
4200 | SizeExpr); | |||
4201 | ||||
4202 | void *InsertPos = nullptr; | |||
4203 | DependentSizedExtVectorType *Canon | |||
4204 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4205 | DependentSizedExtVectorType *New; | |||
4206 | if (Canon) { | |||
4207 | // We already have a canonical version of this array type; use it as | |||
4208 | // the canonical type for a newly-built type. | |||
4209 | New = new (*this, TypeAlignment) | |||
4210 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), | |||
4211 | SizeExpr, AttrLoc); | |||
4212 | } else { | |||
4213 | QualType CanonVecTy = getCanonicalType(vecType); | |||
4214 | if (CanonVecTy == vecType) { | |||
4215 | New = new (*this, TypeAlignment) | |||
4216 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, | |||
4217 | AttrLoc); | |||
4218 | ||||
4219 | DependentSizedExtVectorType *CanonCheck | |||
4220 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4221 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken")(static_cast <bool> (!CanonCheck && "Dependent-sized ext_vector canonical type broken" ) ? void (0) : __assert_fail ("!CanonCheck && \"Dependent-sized ext_vector canonical type broken\"" , "clang/lib/AST/ASTContext.cpp", 4221, __extension__ __PRETTY_FUNCTION__ )); | |||
4222 | (void)CanonCheck; | |||
4223 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); | |||
4224 | } else { | |||
4225 | QualType CanonExtTy = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, | |||
4226 | SourceLocation()); | |||
4227 | New = new (*this, TypeAlignment) DependentSizedExtVectorType( | |||
4228 | *this, vecType, CanonExtTy, SizeExpr, AttrLoc); | |||
4229 | } | |||
4230 | } | |||
4231 | ||||
4232 | Types.push_back(New); | |||
4233 | return QualType(New, 0); | |||
4234 | } | |||
4235 | ||||
4236 | QualType ASTContext::getConstantMatrixType(QualType ElementTy, unsigned NumRows, | |||
4237 | unsigned NumColumns) const { | |||
4238 | llvm::FoldingSetNodeID ID; | |||
4239 | ConstantMatrixType::Profile(ID, ElementTy, NumRows, NumColumns, | |||
4240 | Type::ConstantMatrix); | |||
4241 | ||||
4242 | assert(MatrixType::isValidElementType(ElementTy) &&(static_cast <bool> (MatrixType::isValidElementType(ElementTy ) && "need a valid element type") ? void (0) : __assert_fail ("MatrixType::isValidElementType(ElementTy) && \"need a valid element type\"" , "clang/lib/AST/ASTContext.cpp", 4243, __extension__ __PRETTY_FUNCTION__ )) | |||
4243 | "need a valid element type")(static_cast <bool> (MatrixType::isValidElementType(ElementTy ) && "need a valid element type") ? void (0) : __assert_fail ("MatrixType::isValidElementType(ElementTy) && \"need a valid element type\"" , "clang/lib/AST/ASTContext.cpp", 4243, __extension__ __PRETTY_FUNCTION__ )); | |||
4244 | assert(ConstantMatrixType::isDimensionValid(NumRows) &&(static_cast <bool> (ConstantMatrixType::isDimensionValid (NumRows) && ConstantMatrixType::isDimensionValid(NumColumns ) && "need valid matrix dimensions") ? void (0) : __assert_fail ("ConstantMatrixType::isDimensionValid(NumRows) && ConstantMatrixType::isDimensionValid(NumColumns) && \"need valid matrix dimensions\"" , "clang/lib/AST/ASTContext.cpp", 4246, __extension__ __PRETTY_FUNCTION__ )) | |||
4245 | ConstantMatrixType::isDimensionValid(NumColumns) &&(static_cast <bool> (ConstantMatrixType::isDimensionValid (NumRows) && ConstantMatrixType::isDimensionValid(NumColumns ) && "need valid matrix dimensions") ? void (0) : __assert_fail ("ConstantMatrixType::isDimensionValid(NumRows) && ConstantMatrixType::isDimensionValid(NumColumns) && \"need valid matrix dimensions\"" , "clang/lib/AST/ASTContext.cpp", 4246, __extension__ __PRETTY_FUNCTION__ )) | |||
4246 | "need valid matrix dimensions")(static_cast <bool> (ConstantMatrixType::isDimensionValid (NumRows) && ConstantMatrixType::isDimensionValid(NumColumns ) && "need valid matrix dimensions") ? void (0) : __assert_fail ("ConstantMatrixType::isDimensionValid(NumRows) && ConstantMatrixType::isDimensionValid(NumColumns) && \"need valid matrix dimensions\"" , "clang/lib/AST/ASTContext.cpp", 4246, __extension__ __PRETTY_FUNCTION__ )); | |||
4247 | void *InsertPos = nullptr; | |||
4248 | if (ConstantMatrixType *MTP = MatrixTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
4249 | return QualType(MTP, 0); | |||
4250 | ||||
4251 | QualType Canonical; | |||
4252 | if (!ElementTy.isCanonical()) { | |||
4253 | Canonical = | |||
4254 | getConstantMatrixType(getCanonicalType(ElementTy), NumRows, NumColumns); | |||
4255 | ||||
4256 | ConstantMatrixType *NewIP = MatrixTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4257 | assert(!NewIP && "Matrix type shouldn't already exist in the map")(static_cast <bool> (!NewIP && "Matrix type shouldn't already exist in the map" ) ? void (0) : __assert_fail ("!NewIP && \"Matrix type shouldn't already exist in the map\"" , "clang/lib/AST/ASTContext.cpp", 4257, __extension__ __PRETTY_FUNCTION__ )); | |||
4258 | (void)NewIP; | |||
4259 | } | |||
4260 | ||||
4261 | auto *New = new (*this, TypeAlignment) | |||
4262 | ConstantMatrixType(ElementTy, NumRows, NumColumns, Canonical); | |||
4263 | MatrixTypes.InsertNode(New, InsertPos); | |||
4264 | Types.push_back(New); | |||
4265 | return QualType(New, 0); | |||
4266 | } | |||
4267 | ||||
4268 | QualType ASTContext::getDependentSizedMatrixType(QualType ElementTy, | |||
4269 | Expr *RowExpr, | |||
4270 | Expr *ColumnExpr, | |||
4271 | SourceLocation AttrLoc) const { | |||
4272 | QualType CanonElementTy = getCanonicalType(ElementTy); | |||
4273 | llvm::FoldingSetNodeID ID; | |||
4274 | DependentSizedMatrixType::Profile(ID, *this, CanonElementTy, RowExpr, | |||
4275 | ColumnExpr); | |||
4276 | ||||
4277 | void *InsertPos = nullptr; | |||
4278 | DependentSizedMatrixType *Canon = | |||
4279 | DependentSizedMatrixTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4280 | ||||
4281 | if (!Canon) { | |||
4282 | Canon = new (*this, TypeAlignment) DependentSizedMatrixType( | |||
4283 | *this, CanonElementTy, QualType(), RowExpr, ColumnExpr, AttrLoc); | |||
4284 | #ifndef NDEBUG | |||
4285 | DependentSizedMatrixType *CanonCheck = | |||
4286 | DependentSizedMatrixTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4287 | assert(!CanonCheck && "Dependent-sized matrix canonical type broken")(static_cast <bool> (!CanonCheck && "Dependent-sized matrix canonical type broken" ) ? void (0) : __assert_fail ("!CanonCheck && \"Dependent-sized matrix canonical type broken\"" , "clang/lib/AST/ASTContext.cpp", 4287, __extension__ __PRETTY_FUNCTION__ )); | |||
4288 | #endif | |||
4289 | DependentSizedMatrixTypes.InsertNode(Canon, InsertPos); | |||
4290 | Types.push_back(Canon); | |||
4291 | } | |||
4292 | ||||
4293 | // Already have a canonical version of the matrix type | |||
4294 | // | |||
4295 | // If it exactly matches the requested type, use it directly. | |||
4296 | if (Canon->getElementType() == ElementTy && Canon->getRowExpr() == RowExpr && | |||
4297 | Canon->getRowExpr() == ColumnExpr) | |||
4298 | return QualType(Canon, 0); | |||
4299 | ||||
4300 | // Use Canon as the canonical type for newly-built type. | |||
4301 | DependentSizedMatrixType *New = new (*this, TypeAlignment) | |||
4302 | DependentSizedMatrixType(*this, ElementTy, QualType(Canon, 0), RowExpr, | |||
4303 | ColumnExpr, AttrLoc); | |||
4304 | Types.push_back(New); | |||
4305 | return QualType(New, 0); | |||
4306 | } | |||
4307 | ||||
4308 | QualType ASTContext::getDependentAddressSpaceType(QualType PointeeType, | |||
4309 | Expr *AddrSpaceExpr, | |||
4310 | SourceLocation AttrLoc) const { | |||
4311 | assert(AddrSpaceExpr->isInstantiationDependent())(static_cast <bool> (AddrSpaceExpr->isInstantiationDependent ()) ? void (0) : __assert_fail ("AddrSpaceExpr->isInstantiationDependent()" , "clang/lib/AST/ASTContext.cpp", 4311, __extension__ __PRETTY_FUNCTION__ )); | |||
4312 | ||||
4313 | QualType canonPointeeType = getCanonicalType(PointeeType); | |||
4314 | ||||
4315 | void *insertPos = nullptr; | |||
4316 | llvm::FoldingSetNodeID ID; | |||
4317 | DependentAddressSpaceType::Profile(ID, *this, canonPointeeType, | |||
4318 | AddrSpaceExpr); | |||
4319 | ||||
4320 | DependentAddressSpaceType *canonTy = | |||
4321 | DependentAddressSpaceTypes.FindNodeOrInsertPos(ID, insertPos); | |||
4322 | ||||
4323 | if (!canonTy) { | |||
4324 | canonTy = new (*this, TypeAlignment) | |||
4325 | DependentAddressSpaceType(*this, canonPointeeType, | |||
4326 | QualType(), AddrSpaceExpr, AttrLoc); | |||
4327 | DependentAddressSpaceTypes.InsertNode(canonTy, insertPos); | |||
4328 | Types.push_back(canonTy); | |||
4329 | } | |||
4330 | ||||
4331 | if (canonPointeeType == PointeeType && | |||
4332 | canonTy->getAddrSpaceExpr() == AddrSpaceExpr) | |||
4333 | return QualType(canonTy, 0); | |||
4334 | ||||
4335 | auto *sugaredType | |||
4336 | = new (*this, TypeAlignment) | |||
4337 | DependentAddressSpaceType(*this, PointeeType, QualType(canonTy, 0), | |||
4338 | AddrSpaceExpr, AttrLoc); | |||
4339 | Types.push_back(sugaredType); | |||
4340 | return QualType(sugaredType, 0); | |||
4341 | } | |||
4342 | ||||
4343 | /// Determine whether \p T is canonical as the result type of a function. | |||
4344 | static bool isCanonicalResultType(QualType T) { | |||
4345 | return T.isCanonical() && | |||
4346 | (T.getObjCLifetime() == Qualifiers::OCL_None || | |||
4347 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); | |||
4348 | } | |||
4349 | ||||
4350 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. | |||
4351 | QualType | |||
4352 | ASTContext::getFunctionNoProtoType(QualType ResultTy, | |||
4353 | const FunctionType::ExtInfo &Info) const { | |||
4354 | // FIXME: This assertion cannot be enabled (yet) because the ObjC rewriter | |||
4355 | // functionality creates a function without a prototype regardless of | |||
4356 | // language mode (so it makes them even in C++). Once the rewriter has been | |||
4357 | // fixed, this assertion can be enabled again. | |||
4358 | //assert(!LangOpts.requiresStrictPrototypes() && | |||
4359 | // "strict prototypes are disabled"); | |||
4360 | ||||
4361 | // Unique functions, to guarantee there is only one function of a particular | |||
4362 | // structure. | |||
4363 | llvm::FoldingSetNodeID ID; | |||
4364 | FunctionNoProtoType::Profile(ID, ResultTy, Info); | |||
4365 | ||||
4366 | void *InsertPos = nullptr; | |||
4367 | if (FunctionNoProtoType *FT = | |||
4368 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
4369 | return QualType(FT, 0); | |||
4370 | ||||
4371 | QualType Canonical; | |||
4372 | if (!isCanonicalResultType(ResultTy)) { | |||
4373 | Canonical = | |||
4374 | getFunctionNoProtoType(getCanonicalFunctionResultType(ResultTy), Info); | |||
4375 | ||||
4376 | // Get the new insert position for the node we care about. | |||
4377 | FunctionNoProtoType *NewIP = | |||
4378 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4379 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 4379, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
4380 | } | |||
4381 | ||||
4382 | auto *New = new (*this, TypeAlignment) | |||
4383 | FunctionNoProtoType(ResultTy, Canonical, Info); | |||
4384 | Types.push_back(New); | |||
4385 | FunctionNoProtoTypes.InsertNode(New, InsertPos); | |||
4386 | return QualType(New, 0); | |||
4387 | } | |||
4388 | ||||
4389 | CanQualType | |||
4390 | ASTContext::getCanonicalFunctionResultType(QualType ResultType) const { | |||
4391 | CanQualType CanResultType = getCanonicalType(ResultType); | |||
4392 | ||||
4393 | // Canonical result types do not have ARC lifetime qualifiers. | |||
4394 | if (CanResultType.getQualifiers().hasObjCLifetime()) { | |||
4395 | Qualifiers Qs = CanResultType.getQualifiers(); | |||
4396 | Qs.removeObjCLifetime(); | |||
4397 | return CanQualType::CreateUnsafe( | |||
4398 | getQualifiedType(CanResultType.getUnqualifiedType(), Qs)); | |||
4399 | } | |||
4400 | ||||
4401 | return CanResultType; | |||
4402 | } | |||
4403 | ||||
4404 | static bool isCanonicalExceptionSpecification( | |||
4405 | const FunctionProtoType::ExceptionSpecInfo &ESI, bool NoexceptInType) { | |||
4406 | if (ESI.Type == EST_None) | |||
4407 | return true; | |||
4408 | if (!NoexceptInType) | |||
4409 | return false; | |||
4410 | ||||
4411 | // C++17 onwards: exception specification is part of the type, as a simple | |||
4412 | // boolean "can this function type throw". | |||
4413 | if (ESI.Type == EST_BasicNoexcept) | |||
4414 | return true; | |||
4415 | ||||
4416 | // A noexcept(expr) specification is (possibly) canonical if expr is | |||
4417 | // value-dependent. | |||
4418 | if (ESI.Type == EST_DependentNoexcept) | |||
4419 | return true; | |||
4420 | ||||
4421 | // A dynamic exception specification is canonical if it only contains pack | |||
4422 | // expansions (so we can't tell whether it's non-throwing) and all its | |||
4423 | // contained types are canonical. | |||
4424 | if (ESI.Type == EST_Dynamic) { | |||
4425 | bool AnyPackExpansions = false; | |||
4426 | for (QualType ET : ESI.Exceptions) { | |||
4427 | if (!ET.isCanonical()) | |||
4428 | return false; | |||
4429 | if (ET->getAs<PackExpansionType>()) | |||
4430 | AnyPackExpansions = true; | |||
4431 | } | |||
4432 | return AnyPackExpansions; | |||
4433 | } | |||
4434 | ||||
4435 | return false; | |||
4436 | } | |||
4437 | ||||
4438 | QualType ASTContext::getFunctionTypeInternal( | |||
4439 | QualType ResultTy, ArrayRef<QualType> ArgArray, | |||
4440 | const FunctionProtoType::ExtProtoInfo &EPI, bool OnlyWantCanonical) const { | |||
4441 | size_t NumArgs = ArgArray.size(); | |||
4442 | ||||
4443 | // Unique functions, to guarantee there is only one function of a particular | |||
4444 | // structure. | |||
4445 | llvm::FoldingSetNodeID ID; | |||
4446 | FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI, | |||
4447 | *this, true); | |||
4448 | ||||
4449 | QualType Canonical; | |||
4450 | bool Unique = false; | |||
4451 | ||||
4452 | void *InsertPos = nullptr; | |||
4453 | if (FunctionProtoType *FPT = | |||
4454 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) { | |||
4455 | QualType Existing = QualType(FPT, 0); | |||
4456 | ||||
4457 | // If we find a pre-existing equivalent FunctionProtoType, we can just reuse | |||
4458 | // it so long as our exception specification doesn't contain a dependent | |||
4459 | // noexcept expression, or we're just looking for a canonical type. | |||
4460 | // Otherwise, we're going to need to create a type | |||
4461 | // sugar node to hold the concrete expression. | |||
4462 | if (OnlyWantCanonical || !isComputedNoexcept(EPI.ExceptionSpec.Type) || | |||
4463 | EPI.ExceptionSpec.NoexceptExpr == FPT->getNoexceptExpr()) | |||
4464 | return Existing; | |||
4465 | ||||
4466 | // We need a new type sugar node for this one, to hold the new noexcept | |||
4467 | // expression. We do no canonicalization here, but that's OK since we don't | |||
4468 | // expect to see the same noexcept expression much more than once. | |||
4469 | Canonical = getCanonicalType(Existing); | |||
4470 | Unique = true; | |||
4471 | } | |||
4472 | ||||
4473 | bool NoexceptInType = getLangOpts().CPlusPlus17; | |||
4474 | bool IsCanonicalExceptionSpec = | |||
4475 | isCanonicalExceptionSpecification(EPI.ExceptionSpec, NoexceptInType); | |||
4476 | ||||
4477 | // Determine whether the type being created is already canonical or not. | |||
4478 | bool isCanonical = !Unique && IsCanonicalExceptionSpec && | |||
4479 | isCanonicalResultType(ResultTy) && !EPI.HasTrailingReturn; | |||
4480 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) | |||
4481 | if (!ArgArray[i].isCanonicalAsParam()) | |||
4482 | isCanonical = false; | |||
4483 | ||||
4484 | if (OnlyWantCanonical) | |||
4485 | assert(isCanonical &&(static_cast <bool> (isCanonical && "given non-canonical parameters constructing canonical type" ) ? void (0) : __assert_fail ("isCanonical && \"given non-canonical parameters constructing canonical type\"" , "clang/lib/AST/ASTContext.cpp", 4486, __extension__ __PRETTY_FUNCTION__ )) | |||
4486 | "given non-canonical parameters constructing canonical type")(static_cast <bool> (isCanonical && "given non-canonical parameters constructing canonical type" ) ? void (0) : __assert_fail ("isCanonical && \"given non-canonical parameters constructing canonical type\"" , "clang/lib/AST/ASTContext.cpp", 4486, __extension__ __PRETTY_FUNCTION__ )); | |||
4487 | ||||
4488 | // If this type isn't canonical, get the canonical version of it if we don't | |||
4489 | // already have it. The exception spec is only partially part of the | |||
4490 | // canonical type, and only in C++17 onwards. | |||
4491 | if (!isCanonical && Canonical.isNull()) { | |||
4492 | SmallVector<QualType, 16> CanonicalArgs; | |||
4493 | CanonicalArgs.reserve(NumArgs); | |||
4494 | for (unsigned i = 0; i != NumArgs; ++i) | |||
4495 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); | |||
4496 | ||||
4497 | llvm::SmallVector<QualType, 8> ExceptionTypeStorage; | |||
4498 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; | |||
4499 | CanonicalEPI.HasTrailingReturn = false; | |||
4500 | ||||
4501 | if (IsCanonicalExceptionSpec) { | |||
4502 | // Exception spec is already OK. | |||
4503 | } else if (NoexceptInType) { | |||
4504 | switch (EPI.ExceptionSpec.Type) { | |||
4505 | case EST_Unparsed: case EST_Unevaluated: case EST_Uninstantiated: | |||
4506 | // We don't know yet. It shouldn't matter what we pick here; no-one | |||
4507 | // should ever look at this. | |||
4508 | [[fallthrough]]; | |||
4509 | case EST_None: case EST_MSAny: case EST_NoexceptFalse: | |||
4510 | CanonicalEPI.ExceptionSpec.Type = EST_None; | |||
4511 | break; | |||
4512 | ||||
4513 | // A dynamic exception specification is almost always "not noexcept", | |||
4514 | // with the exception that a pack expansion might expand to no types. | |||
4515 | case EST_Dynamic: { | |||
4516 | bool AnyPacks = false; | |||
4517 | for (QualType ET : EPI.ExceptionSpec.Exceptions) { | |||
4518 | if (ET->getAs<PackExpansionType>()) | |||
4519 | AnyPacks = true; | |||
4520 | ExceptionTypeStorage.push_back(getCanonicalType(ET)); | |||
4521 | } | |||
4522 | if (!AnyPacks) | |||
4523 | CanonicalEPI.ExceptionSpec.Type = EST_None; | |||
4524 | else { | |||
4525 | CanonicalEPI.ExceptionSpec.Type = EST_Dynamic; | |||
4526 | CanonicalEPI.ExceptionSpec.Exceptions = ExceptionTypeStorage; | |||
4527 | } | |||
4528 | break; | |||
4529 | } | |||
4530 | ||||
4531 | case EST_DynamicNone: | |||
4532 | case EST_BasicNoexcept: | |||
4533 | case EST_NoexceptTrue: | |||
4534 | case EST_NoThrow: | |||
4535 | CanonicalEPI.ExceptionSpec.Type = EST_BasicNoexcept; | |||
4536 | break; | |||
4537 | ||||
4538 | case EST_DependentNoexcept: | |||
4539 | llvm_unreachable("dependent noexcept is already canonical")::llvm::llvm_unreachable_internal("dependent noexcept is already canonical" , "clang/lib/AST/ASTContext.cpp", 4539); | |||
4540 | } | |||
4541 | } else { | |||
4542 | CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo(); | |||
4543 | } | |||
4544 | ||||
4545 | // Adjust the canonical function result type. | |||
4546 | CanQualType CanResultTy = getCanonicalFunctionResultType(ResultTy); | |||
4547 | Canonical = | |||
4548 | getFunctionTypeInternal(CanResultTy, CanonicalArgs, CanonicalEPI, true); | |||
4549 | ||||
4550 | // Get the new insert position for the node we care about. | |||
4551 | FunctionProtoType *NewIP = | |||
4552 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4553 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 4553, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
4554 | } | |||
4555 | ||||
4556 | // Compute the needed size to hold this FunctionProtoType and the | |||
4557 | // various trailing objects. | |||
4558 | auto ESH = FunctionProtoType::getExceptionSpecSize( | |||
4559 | EPI.ExceptionSpec.Type, EPI.ExceptionSpec.Exceptions.size()); | |||
4560 | size_t Size = FunctionProtoType::totalSizeToAlloc< | |||
4561 | QualType, SourceLocation, FunctionType::FunctionTypeExtraBitfields, | |||
4562 | FunctionType::ExceptionType, Expr *, FunctionDecl *, | |||
4563 | FunctionProtoType::ExtParameterInfo, Qualifiers>( | |||
4564 | NumArgs, EPI.Variadic, EPI.requiresFunctionProtoTypeExtraBitfields(), | |||
4565 | ESH.NumExceptionType, ESH.NumExprPtr, ESH.NumFunctionDeclPtr, | |||
4566 | EPI.ExtParameterInfos ? NumArgs : 0, | |||
4567 | EPI.TypeQuals.hasNonFastQualifiers() ? 1 : 0); | |||
4568 | ||||
4569 | auto *FTP = (FunctionProtoType *)Allocate(Size, TypeAlignment); | |||
4570 | FunctionProtoType::ExtProtoInfo newEPI = EPI; | |||
4571 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); | |||
4572 | Types.push_back(FTP); | |||
4573 | if (!Unique) | |||
4574 | FunctionProtoTypes.InsertNode(FTP, InsertPos); | |||
4575 | return QualType(FTP, 0); | |||
4576 | } | |||
4577 | ||||
4578 | QualType ASTContext::getPipeType(QualType T, bool ReadOnly) const { | |||
4579 | llvm::FoldingSetNodeID ID; | |||
4580 | PipeType::Profile(ID, T, ReadOnly); | |||
4581 | ||||
4582 | void *InsertPos = nullptr; | |||
4583 | if (PipeType *PT = PipeTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
4584 | return QualType(PT, 0); | |||
4585 | ||||
4586 | // If the pipe element type isn't canonical, this won't be a canonical type | |||
4587 | // either, so fill in the canonical type field. | |||
4588 | QualType Canonical; | |||
4589 | if (!T.isCanonical()) { | |||
4590 | Canonical = getPipeType(getCanonicalType(T), ReadOnly); | |||
4591 | ||||
4592 | // Get the new insert position for the node we care about. | |||
4593 | PipeType *NewIP = PipeTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4594 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 4594, __extension__ __PRETTY_FUNCTION__ )); | |||
4595 | (void)NewIP; | |||
4596 | } | |||
4597 | auto *New = new (*this, TypeAlignment) PipeType(T, Canonical, ReadOnly); | |||
4598 | Types.push_back(New); | |||
4599 | PipeTypes.InsertNode(New, InsertPos); | |||
4600 | return QualType(New, 0); | |||
4601 | } | |||
4602 | ||||
4603 | QualType ASTContext::adjustStringLiteralBaseType(QualType Ty) const { | |||
4604 | // OpenCL v1.1 s6.5.3: a string literal is in the constant address space. | |||
4605 | return LangOpts.OpenCL ? getAddrSpaceQualType(Ty, LangAS::opencl_constant) | |||
4606 | : Ty; | |||
4607 | } | |||
4608 | ||||
4609 | QualType ASTContext::getReadPipeType(QualType T) const { | |||
4610 | return getPipeType(T, true); | |||
4611 | } | |||
4612 | ||||
4613 | QualType ASTContext::getWritePipeType(QualType T) const { | |||
4614 | return getPipeType(T, false); | |||
4615 | } | |||
4616 | ||||
4617 | QualType ASTContext::getBitIntType(bool IsUnsigned, unsigned NumBits) const { | |||
4618 | llvm::FoldingSetNodeID ID; | |||
4619 | BitIntType::Profile(ID, IsUnsigned, NumBits); | |||
4620 | ||||
4621 | void *InsertPos = nullptr; | |||
4622 | if (BitIntType *EIT = BitIntTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
4623 | return QualType(EIT, 0); | |||
4624 | ||||
4625 | auto *New = new (*this, TypeAlignment) BitIntType(IsUnsigned, NumBits); | |||
4626 | BitIntTypes.InsertNode(New, InsertPos); | |||
4627 | Types.push_back(New); | |||
4628 | return QualType(New, 0); | |||
4629 | } | |||
4630 | ||||
4631 | QualType ASTContext::getDependentBitIntType(bool IsUnsigned, | |||
4632 | Expr *NumBitsExpr) const { | |||
4633 | assert(NumBitsExpr->isInstantiationDependent() && "Only good for dependent")(static_cast <bool> (NumBitsExpr->isInstantiationDependent () && "Only good for dependent") ? void (0) : __assert_fail ("NumBitsExpr->isInstantiationDependent() && \"Only good for dependent\"" , "clang/lib/AST/ASTContext.cpp", 4633, __extension__ __PRETTY_FUNCTION__ )); | |||
4634 | llvm::FoldingSetNodeID ID; | |||
4635 | DependentBitIntType::Profile(ID, *this, IsUnsigned, NumBitsExpr); | |||
4636 | ||||
4637 | void *InsertPos = nullptr; | |||
4638 | if (DependentBitIntType *Existing = | |||
4639 | DependentBitIntTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
4640 | return QualType(Existing, 0); | |||
4641 | ||||
4642 | auto *New = new (*this, TypeAlignment) | |||
4643 | DependentBitIntType(*this, IsUnsigned, NumBitsExpr); | |||
4644 | DependentBitIntTypes.InsertNode(New, InsertPos); | |||
4645 | ||||
4646 | Types.push_back(New); | |||
4647 | return QualType(New, 0); | |||
4648 | } | |||
4649 | ||||
4650 | #ifndef NDEBUG | |||
4651 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { | |||
4652 | if (!isa<CXXRecordDecl>(D)) return false; | |||
4653 | const auto *RD = cast<CXXRecordDecl>(D); | |||
4654 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) | |||
4655 | return true; | |||
4656 | if (RD->getDescribedClassTemplate() && | |||
4657 | !isa<ClassTemplateSpecializationDecl>(RD)) | |||
4658 | return true; | |||
4659 | return false; | |||
4660 | } | |||
4661 | #endif | |||
4662 | ||||
4663 | /// getInjectedClassNameType - Return the unique reference to the | |||
4664 | /// injected class name type for the specified templated declaration. | |||
4665 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, | |||
4666 | QualType TST) const { | |||
4667 | assert(NeedsInjectedClassNameType(Decl))(static_cast <bool> (NeedsInjectedClassNameType(Decl)) ? void (0) : __assert_fail ("NeedsInjectedClassNameType(Decl)" , "clang/lib/AST/ASTContext.cpp", 4667, __extension__ __PRETTY_FUNCTION__ )); | |||
4668 | if (Decl->TypeForDecl) { | |||
4669 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl))(static_cast <bool> (isa<InjectedClassNameType>(Decl ->TypeForDecl)) ? void (0) : __assert_fail ("isa<InjectedClassNameType>(Decl->TypeForDecl)" , "clang/lib/AST/ASTContext.cpp", 4669, __extension__ __PRETTY_FUNCTION__ )); | |||
4670 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { | |||
4671 | assert(PrevDecl->TypeForDecl && "previous declaration has no type")(static_cast <bool> (PrevDecl->TypeForDecl && "previous declaration has no type") ? void (0) : __assert_fail ("PrevDecl->TypeForDecl && \"previous declaration has no type\"" , "clang/lib/AST/ASTContext.cpp", 4671, __extension__ __PRETTY_FUNCTION__ )); | |||
4672 | Decl->TypeForDecl = PrevDecl->TypeForDecl; | |||
4673 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl))(static_cast <bool> (isa<InjectedClassNameType>(Decl ->TypeForDecl)) ? void (0) : __assert_fail ("isa<InjectedClassNameType>(Decl->TypeForDecl)" , "clang/lib/AST/ASTContext.cpp", 4673, __extension__ __PRETTY_FUNCTION__ )); | |||
4674 | } else { | |||
4675 | Type *newType = | |||
4676 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); | |||
4677 | Decl->TypeForDecl = newType; | |||
4678 | Types.push_back(newType); | |||
4679 | } | |||
4680 | return QualType(Decl->TypeForDecl, 0); | |||
4681 | } | |||
4682 | ||||
4683 | /// getTypeDeclType - Return the unique reference to the type for the | |||
4684 | /// specified type declaration. | |||
4685 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { | |||
4686 | assert(Decl && "Passed null for Decl param")(static_cast <bool> (Decl && "Passed null for Decl param" ) ? void (0) : __assert_fail ("Decl && \"Passed null for Decl param\"" , "clang/lib/AST/ASTContext.cpp", 4686, __extension__ __PRETTY_FUNCTION__ )); | |||
4687 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case")(static_cast <bool> (!Decl->TypeForDecl && "TypeForDecl present in slow case" ) ? void (0) : __assert_fail ("!Decl->TypeForDecl && \"TypeForDecl present in slow case\"" , "clang/lib/AST/ASTContext.cpp", 4687, __extension__ __PRETTY_FUNCTION__ )); | |||
4688 | ||||
4689 | if (const auto *Typedef = dyn_cast<TypedefNameDecl>(Decl)) | |||
4690 | return getTypedefType(Typedef); | |||
4691 | ||||
4692 | assert(!isa<TemplateTypeParmDecl>(Decl) &&(static_cast <bool> (!isa<TemplateTypeParmDecl>(Decl ) && "Template type parameter types are always available." ) ? void (0) : __assert_fail ("!isa<TemplateTypeParmDecl>(Decl) && \"Template type parameter types are always available.\"" , "clang/lib/AST/ASTContext.cpp", 4693, __extension__ __PRETTY_FUNCTION__ )) | |||
4693 | "Template type parameter types are always available.")(static_cast <bool> (!isa<TemplateTypeParmDecl>(Decl ) && "Template type parameter types are always available." ) ? void (0) : __assert_fail ("!isa<TemplateTypeParmDecl>(Decl) && \"Template type parameter types are always available.\"" , "clang/lib/AST/ASTContext.cpp", 4693, __extension__ __PRETTY_FUNCTION__ )); | |||
4694 | ||||
4695 | if (const auto *Record = dyn_cast<RecordDecl>(Decl)) { | |||
4696 | assert(Record->isFirstDecl() && "struct/union has previous declaration")(static_cast <bool> (Record->isFirstDecl() && "struct/union has previous declaration") ? void (0) : __assert_fail ("Record->isFirstDecl() && \"struct/union has previous declaration\"" , "clang/lib/AST/ASTContext.cpp", 4696, __extension__ __PRETTY_FUNCTION__ )); | |||
4697 | assert(!NeedsInjectedClassNameType(Record))(static_cast <bool> (!NeedsInjectedClassNameType(Record )) ? void (0) : __assert_fail ("!NeedsInjectedClassNameType(Record)" , "clang/lib/AST/ASTContext.cpp", 4697, __extension__ __PRETTY_FUNCTION__ )); | |||
4698 | return getRecordType(Record); | |||
4699 | } else if (const auto *Enum = dyn_cast<EnumDecl>(Decl)) { | |||
4700 | assert(Enum->isFirstDecl() && "enum has previous declaration")(static_cast <bool> (Enum->isFirstDecl() && "enum has previous declaration" ) ? void (0) : __assert_fail ("Enum->isFirstDecl() && \"enum has previous declaration\"" , "clang/lib/AST/ASTContext.cpp", 4700, __extension__ __PRETTY_FUNCTION__ )); | |||
4701 | return getEnumType(Enum); | |||
4702 | } else if (const auto *Using = dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { | |||
4703 | return getUnresolvedUsingType(Using); | |||
4704 | } else | |||
4705 | llvm_unreachable("TypeDecl without a type?")::llvm::llvm_unreachable_internal("TypeDecl without a type?", "clang/lib/AST/ASTContext.cpp", 4705); | |||
4706 | ||||
4707 | return QualType(Decl->TypeForDecl, 0); | |||
4708 | } | |||
4709 | ||||
4710 | /// getTypedefType - Return the unique reference to the type for the | |||
4711 | /// specified typedef name decl. | |||
4712 | QualType ASTContext::getTypedefType(const TypedefNameDecl *Decl, | |||
4713 | QualType Underlying) const { | |||
4714 | if (!Decl->TypeForDecl) { | |||
4715 | if (Underlying.isNull()) | |||
4716 | Underlying = Decl->getUnderlyingType(); | |||
4717 | auto *NewType = new (*this, TypeAlignment) TypedefType( | |||
4718 | Type::Typedef, Decl, QualType(), getCanonicalType(Underlying)); | |||
4719 | Decl->TypeForDecl = NewType; | |||
4720 | Types.push_back(NewType); | |||
4721 | return QualType(NewType, 0); | |||
4722 | } | |||
4723 | if (Underlying.isNull() || Decl->getUnderlyingType() == Underlying) | |||
4724 | return QualType(Decl->TypeForDecl, 0); | |||
4725 | assert(hasSameType(Decl->getUnderlyingType(), Underlying))(static_cast <bool> (hasSameType(Decl->getUnderlyingType (), Underlying)) ? void (0) : __assert_fail ("hasSameType(Decl->getUnderlyingType(), Underlying)" , "clang/lib/AST/ASTContext.cpp", 4725, __extension__ __PRETTY_FUNCTION__ )); | |||
4726 | ||||
4727 | llvm::FoldingSetNodeID ID; | |||
4728 | TypedefType::Profile(ID, Decl, Underlying); | |||
4729 | ||||
4730 | void *InsertPos = nullptr; | |||
4731 | if (TypedefType *T = TypedefTypes.FindNodeOrInsertPos(ID, InsertPos)) { | |||
4732 | assert(!T->typeMatchesDecl() &&(static_cast <bool> (!T->typeMatchesDecl() && "non-divergent case should be handled with TypeDecl") ? void (0) : __assert_fail ("!T->typeMatchesDecl() && \"non-divergent case should be handled with TypeDecl\"" , "clang/lib/AST/ASTContext.cpp", 4733, __extension__ __PRETTY_FUNCTION__ )) | |||
4733 | "non-divergent case should be handled with TypeDecl")(static_cast <bool> (!T->typeMatchesDecl() && "non-divergent case should be handled with TypeDecl") ? void (0) : __assert_fail ("!T->typeMatchesDecl() && \"non-divergent case should be handled with TypeDecl\"" , "clang/lib/AST/ASTContext.cpp", 4733, __extension__ __PRETTY_FUNCTION__ )); | |||
4734 | return QualType(T, 0); | |||
4735 | } | |||
4736 | ||||
4737 | void *Mem = | |||
4738 | Allocate(TypedefType::totalSizeToAlloc<QualType>(true), TypeAlignment); | |||
4739 | auto *NewType = new (Mem) TypedefType(Type::Typedef, Decl, Underlying, | |||
4740 | getCanonicalType(Underlying)); | |||
4741 | TypedefTypes.InsertNode(NewType, InsertPos); | |||
4742 | Types.push_back(NewType); | |||
4743 | return QualType(NewType, 0); | |||
4744 | } | |||
4745 | ||||
4746 | QualType ASTContext::getUsingType(const UsingShadowDecl *Found, | |||
4747 | QualType Underlying) const { | |||
4748 | llvm::FoldingSetNodeID ID; | |||
4749 | UsingType::Profile(ID, Found, Underlying); | |||
4750 | ||||
4751 | void *InsertPos = nullptr; | |||
4752 | if (UsingType *T = UsingTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
4753 | return QualType(T, 0); | |||
4754 | ||||
4755 | const Type *TypeForDecl = | |||
4756 | cast<TypeDecl>(Found->getTargetDecl())->getTypeForDecl(); | |||
4757 | ||||
4758 | assert(!Underlying.hasLocalQualifiers())(static_cast <bool> (!Underlying.hasLocalQualifiers()) ? void (0) : __assert_fail ("!Underlying.hasLocalQualifiers()" , "clang/lib/AST/ASTContext.cpp", 4758, __extension__ __PRETTY_FUNCTION__ )); | |||
4759 | QualType Canon = Underlying->getCanonicalTypeInternal(); | |||
4760 | assert(TypeForDecl->getCanonicalTypeInternal() == Canon)(static_cast <bool> (TypeForDecl->getCanonicalTypeInternal () == Canon) ? void (0) : __assert_fail ("TypeForDecl->getCanonicalTypeInternal() == Canon" , "clang/lib/AST/ASTContext.cpp", 4760, __extension__ __PRETTY_FUNCTION__ )); | |||
4761 | ||||
4762 | if (Underlying.getTypePtr() == TypeForDecl) | |||
4763 | Underlying = QualType(); | |||
4764 | void *Mem = | |||
4765 | Allocate(UsingType::totalSizeToAlloc<QualType>(!Underlying.isNull()), | |||
4766 | TypeAlignment); | |||
4767 | UsingType *NewType = new (Mem) UsingType(Found, Underlying, Canon); | |||
4768 | Types.push_back(NewType); | |||
4769 | UsingTypes.InsertNode(NewType, InsertPos); | |||
4770 | return QualType(NewType, 0); | |||
4771 | } | |||
4772 | ||||
4773 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { | |||
4774 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); | |||
4775 | ||||
4776 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) | |||
4777 | if (PrevDecl->TypeForDecl) | |||
4778 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); | |||
4779 | ||||
4780 | auto *newType = new (*this, TypeAlignment) RecordType(Decl); | |||
4781 | Decl->TypeForDecl = newType; | |||
4782 | Types.push_back(newType); | |||
4783 | return QualType(newType, 0); | |||
4784 | } | |||
4785 | ||||
4786 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { | |||
4787 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); | |||
4788 | ||||
4789 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) | |||
4790 | if (PrevDecl->TypeForDecl) | |||
4791 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); | |||
4792 | ||||
4793 | auto *newType = new (*this, TypeAlignment) EnumType(Decl); | |||
4794 | Decl->TypeForDecl = newType; | |||
4795 | Types.push_back(newType); | |||
4796 | return QualType(newType, 0); | |||
4797 | } | |||
4798 | ||||
4799 | QualType ASTContext::getUnresolvedUsingType( | |||
4800 | const UnresolvedUsingTypenameDecl *Decl) const { | |||
4801 | if (Decl->TypeForDecl) | |||
4802 | return QualType(Decl->TypeForDecl, 0); | |||
4803 | ||||
4804 | if (const UnresolvedUsingTypenameDecl *CanonicalDecl = | |||
4805 | Decl->getCanonicalDecl()) | |||
4806 | if (CanonicalDecl->TypeForDecl) | |||
4807 | return QualType(Decl->TypeForDecl = CanonicalDecl->TypeForDecl, 0); | |||
4808 | ||||
4809 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Decl); | |||
4810 | Decl->TypeForDecl = newType; | |||
4811 | Types.push_back(newType); | |||
4812 | return QualType(newType, 0); | |||
4813 | } | |||
4814 | ||||
4815 | QualType ASTContext::getAttributedType(attr::Kind attrKind, | |||
4816 | QualType modifiedType, | |||
4817 | QualType equivalentType) const { | |||
4818 | llvm::FoldingSetNodeID id; | |||
4819 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); | |||
4820 | ||||
4821 | void *insertPos = nullptr; | |||
4822 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); | |||
4823 | if (type) return QualType(type, 0); | |||
4824 | ||||
4825 | QualType canon = getCanonicalType(equivalentType); | |||
4826 | type = new (*this, TypeAlignment) | |||
4827 | AttributedType(canon, attrKind, modifiedType, equivalentType); | |||
4828 | ||||
4829 | Types.push_back(type); | |||
4830 | AttributedTypes.InsertNode(type, insertPos); | |||
4831 | ||||
4832 | return QualType(type, 0); | |||
4833 | } | |||
4834 | ||||
4835 | QualType ASTContext::getBTFTagAttributedType(const BTFTypeTagAttr *BTFAttr, | |||
4836 | QualType Wrapped) { | |||
4837 | llvm::FoldingSetNodeID ID; | |||
4838 | BTFTagAttributedType::Profile(ID, Wrapped, BTFAttr); | |||
4839 | ||||
4840 | void *InsertPos = nullptr; | |||
4841 | BTFTagAttributedType *Ty = | |||
4842 | BTFTagAttributedTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4843 | if (Ty) | |||
4844 | return QualType(Ty, 0); | |||
4845 | ||||
4846 | QualType Canon = getCanonicalType(Wrapped); | |||
4847 | Ty = new (*this, TypeAlignment) BTFTagAttributedType(Canon, Wrapped, BTFAttr); | |||
4848 | ||||
4849 | Types.push_back(Ty); | |||
4850 | BTFTagAttributedTypes.InsertNode(Ty, InsertPos); | |||
4851 | ||||
4852 | return QualType(Ty, 0); | |||
4853 | } | |||
4854 | ||||
4855 | /// Retrieve a substitution-result type. | |||
4856 | QualType ASTContext::getSubstTemplateTypeParmType( | |||
4857 | QualType Replacement, Decl *AssociatedDecl, unsigned Index, | |||
4858 | std::optional<unsigned> PackIndex) const { | |||
4859 | llvm::FoldingSetNodeID ID; | |||
4860 | SubstTemplateTypeParmType::Profile(ID, Replacement, AssociatedDecl, Index, | |||
4861 | PackIndex); | |||
4862 | void *InsertPos = nullptr; | |||
4863 | SubstTemplateTypeParmType *SubstParm = | |||
4864 | SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4865 | ||||
4866 | if (!SubstParm) { | |||
4867 | void *Mem = Allocate(SubstTemplateTypeParmType::totalSizeToAlloc<QualType>( | |||
4868 | !Replacement.isCanonical()), | |||
4869 | TypeAlignment); | |||
4870 | SubstParm = new (Mem) SubstTemplateTypeParmType(Replacement, AssociatedDecl, | |||
4871 | Index, PackIndex); | |||
4872 | Types.push_back(SubstParm); | |||
4873 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); | |||
4874 | } | |||
4875 | ||||
4876 | return QualType(SubstParm, 0); | |||
4877 | } | |||
4878 | ||||
4879 | /// Retrieve a | |||
4880 | QualType | |||
4881 | ASTContext::getSubstTemplateTypeParmPackType(Decl *AssociatedDecl, | |||
4882 | unsigned Index, bool Final, | |||
4883 | const TemplateArgument &ArgPack) { | |||
4884 | #ifndef NDEBUG | |||
4885 | for (const auto &P : ArgPack.pack_elements()) | |||
4886 | assert(P.getKind() == TemplateArgument::Type && "Pack contains a non-type")(static_cast <bool> (P.getKind() == TemplateArgument::Type && "Pack contains a non-type") ? void (0) : __assert_fail ("P.getKind() == TemplateArgument::Type && \"Pack contains a non-type\"" , "clang/lib/AST/ASTContext.cpp", 4886, __extension__ __PRETTY_FUNCTION__ )); | |||
4887 | #endif | |||
4888 | ||||
4889 | llvm::FoldingSetNodeID ID; | |||
4890 | SubstTemplateTypeParmPackType::Profile(ID, AssociatedDecl, Index, Final, | |||
4891 | ArgPack); | |||
4892 | void *InsertPos = nullptr; | |||
4893 | if (SubstTemplateTypeParmPackType *SubstParm = | |||
4894 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
4895 | return QualType(SubstParm, 0); | |||
4896 | ||||
4897 | QualType Canon; | |||
4898 | { | |||
4899 | TemplateArgument CanonArgPack = getCanonicalTemplateArgument(ArgPack); | |||
4900 | if (!AssociatedDecl->isCanonicalDecl() || | |||
4901 | !CanonArgPack.structurallyEquals(ArgPack)) { | |||
4902 | Canon = getSubstTemplateTypeParmPackType( | |||
4903 | AssociatedDecl->getCanonicalDecl(), Index, Final, CanonArgPack); | |||
4904 | [[maybe_unused]] const auto *Nothing = | |||
4905 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4906 | assert(!Nothing)(static_cast <bool> (!Nothing) ? void (0) : __assert_fail ("!Nothing", "clang/lib/AST/ASTContext.cpp", 4906, __extension__ __PRETTY_FUNCTION__)); | |||
4907 | } | |||
4908 | } | |||
4909 | ||||
4910 | auto *SubstParm = new (*this, TypeAlignment) SubstTemplateTypeParmPackType( | |||
4911 | Canon, AssociatedDecl, Index, Final, ArgPack); | |||
4912 | Types.push_back(SubstParm); | |||
4913 | SubstTemplateTypeParmPackTypes.InsertNode(SubstParm, InsertPos); | |||
4914 | return QualType(SubstParm, 0); | |||
4915 | } | |||
4916 | ||||
4917 | /// Retrieve the template type parameter type for a template | |||
4918 | /// parameter or parameter pack with the given depth, index, and (optionally) | |||
4919 | /// name. | |||
4920 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, | |||
4921 | bool ParameterPack, | |||
4922 | TemplateTypeParmDecl *TTPDecl) const { | |||
4923 | llvm::FoldingSetNodeID ID; | |||
4924 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); | |||
4925 | void *InsertPos = nullptr; | |||
4926 | TemplateTypeParmType *TypeParm | |||
4927 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4928 | ||||
4929 | if (TypeParm) | |||
4930 | return QualType(TypeParm, 0); | |||
4931 | ||||
4932 | if (TTPDecl) { | |||
4933 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); | |||
4934 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); | |||
4935 | ||||
4936 | TemplateTypeParmType *TypeCheck | |||
4937 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
4938 | assert(!TypeCheck && "Template type parameter canonical type broken")(static_cast <bool> (!TypeCheck && "Template type parameter canonical type broken" ) ? void (0) : __assert_fail ("!TypeCheck && \"Template type parameter canonical type broken\"" , "clang/lib/AST/ASTContext.cpp", 4938, __extension__ __PRETTY_FUNCTION__ )); | |||
4939 | (void)TypeCheck; | |||
4940 | } else | |||
4941 | TypeParm = new (*this, TypeAlignment) | |||
4942 | TemplateTypeParmType(Depth, Index, ParameterPack); | |||
4943 | ||||
4944 | Types.push_back(TypeParm); | |||
4945 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); | |||
4946 | ||||
4947 | return QualType(TypeParm, 0); | |||
4948 | } | |||
4949 | ||||
4950 | TypeSourceInfo * | |||
4951 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, | |||
4952 | SourceLocation NameLoc, | |||
4953 | const TemplateArgumentListInfo &Args, | |||
4954 | QualType Underlying) const { | |||
4955 | assert(!Name.getAsDependentTemplateName() &&(static_cast <bool> (!Name.getAsDependentTemplateName() && "No dependent template names here!") ? void (0) : __assert_fail ("!Name.getAsDependentTemplateName() && \"No dependent template names here!\"" , "clang/lib/AST/ASTContext.cpp", 4956, __extension__ __PRETTY_FUNCTION__ )) | |||
4956 | "No dependent template names here!")(static_cast <bool> (!Name.getAsDependentTemplateName() && "No dependent template names here!") ? void (0) : __assert_fail ("!Name.getAsDependentTemplateName() && \"No dependent template names here!\"" , "clang/lib/AST/ASTContext.cpp", 4956, __extension__ __PRETTY_FUNCTION__ )); | |||
4957 | QualType TST = | |||
4958 | getTemplateSpecializationType(Name, Args.arguments(), Underlying); | |||
4959 | ||||
4960 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); | |||
4961 | TemplateSpecializationTypeLoc TL = | |||
4962 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); | |||
4963 | TL.setTemplateKeywordLoc(SourceLocation()); | |||
4964 | TL.setTemplateNameLoc(NameLoc); | |||
4965 | TL.setLAngleLoc(Args.getLAngleLoc()); | |||
4966 | TL.setRAngleLoc(Args.getRAngleLoc()); | |||
4967 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) | |||
4968 | TL.setArgLocInfo(i, Args[i].getLocInfo()); | |||
4969 | return DI; | |||
4970 | } | |||
4971 | ||||
4972 | QualType | |||
4973 | ASTContext::getTemplateSpecializationType(TemplateName Template, | |||
4974 | ArrayRef<TemplateArgumentLoc> Args, | |||
4975 | QualType Underlying) const { | |||
4976 | assert(!Template.getAsDependentTemplateName() &&(static_cast <bool> (!Template.getAsDependentTemplateName () && "No dependent template names here!") ? void (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\"" , "clang/lib/AST/ASTContext.cpp", 4977, __extension__ __PRETTY_FUNCTION__ )) | |||
4977 | "No dependent template names here!")(static_cast <bool> (!Template.getAsDependentTemplateName () && "No dependent template names here!") ? void (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\"" , "clang/lib/AST/ASTContext.cpp", 4977, __extension__ __PRETTY_FUNCTION__ )); | |||
4978 | ||||
4979 | SmallVector<TemplateArgument, 4> ArgVec; | |||
4980 | ArgVec.reserve(Args.size()); | |||
4981 | for (const TemplateArgumentLoc &Arg : Args) | |||
4982 | ArgVec.push_back(Arg.getArgument()); | |||
4983 | ||||
4984 | return getTemplateSpecializationType(Template, ArgVec, Underlying); | |||
4985 | } | |||
4986 | ||||
4987 | #ifndef NDEBUG | |||
4988 | static bool hasAnyPackExpansions(ArrayRef<TemplateArgument> Args) { | |||
4989 | for (const TemplateArgument &Arg : Args) | |||
4990 | if (Arg.isPackExpansion()) | |||
4991 | return true; | |||
4992 | ||||
4993 | return true; | |||
4994 | } | |||
4995 | #endif | |||
4996 | ||||
4997 | QualType | |||
4998 | ASTContext::getTemplateSpecializationType(TemplateName Template, | |||
4999 | ArrayRef<TemplateArgument> Args, | |||
5000 | QualType Underlying) const { | |||
5001 | assert(!Template.getAsDependentTemplateName() &&(static_cast <bool> (!Template.getAsDependentTemplateName () && "No dependent template names here!") ? void (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\"" , "clang/lib/AST/ASTContext.cpp", 5002, __extension__ __PRETTY_FUNCTION__ )) | |||
5002 | "No dependent template names here!")(static_cast <bool> (!Template.getAsDependentTemplateName () && "No dependent template names here!") ? void (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\"" , "clang/lib/AST/ASTContext.cpp", 5002, __extension__ __PRETTY_FUNCTION__ )); | |||
5003 | // Look through qualified template names. | |||
5004 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) | |||
5005 | Template = QTN->getUnderlyingTemplate(); | |||
5006 | ||||
5007 | const auto *TD = Template.getAsTemplateDecl(); | |||
5008 | bool IsTypeAlias = TD && TD->isTypeAlias(); | |||
5009 | QualType CanonType; | |||
5010 | if (!Underlying.isNull()) | |||
5011 | CanonType = getCanonicalType(Underlying); | |||
5012 | else { | |||
5013 | // We can get here with an alias template when the specialization contains | |||
5014 | // a pack expansion that does not match up with a parameter pack. | |||
5015 | assert((!IsTypeAlias || hasAnyPackExpansions(Args)) &&(static_cast <bool> ((!IsTypeAlias || hasAnyPackExpansions (Args)) && "Caller must compute aliased type") ? void (0) : __assert_fail ("(!IsTypeAlias || hasAnyPackExpansions(Args)) && \"Caller must compute aliased type\"" , "clang/lib/AST/ASTContext.cpp", 5016, __extension__ __PRETTY_FUNCTION__ )) | |||
5016 | "Caller must compute aliased type")(static_cast <bool> ((!IsTypeAlias || hasAnyPackExpansions (Args)) && "Caller must compute aliased type") ? void (0) : __assert_fail ("(!IsTypeAlias || hasAnyPackExpansions(Args)) && \"Caller must compute aliased type\"" , "clang/lib/AST/ASTContext.cpp", 5016, __extension__ __PRETTY_FUNCTION__ )); | |||
5017 | IsTypeAlias = false; | |||
5018 | CanonType = getCanonicalTemplateSpecializationType(Template, Args); | |||
5019 | } | |||
5020 | ||||
5021 | // Allocate the (non-canonical) template specialization type, but don't | |||
5022 | // try to unique it: these types typically have location information that | |||
5023 | // we don't unique and don't want to lose. | |||
5024 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + | |||
5025 | sizeof(TemplateArgument) * Args.size() + | |||
5026 | (IsTypeAlias? sizeof(QualType) : 0), | |||
5027 | TypeAlignment); | |||
5028 | auto *Spec | |||
5029 | = new (Mem) TemplateSpecializationType(Template, Args, CanonType, | |||
5030 | IsTypeAlias ? Underlying : QualType()); | |||
5031 | ||||
5032 | Types.push_back(Spec); | |||
5033 | return QualType(Spec, 0); | |||
5034 | } | |||
5035 | ||||
5036 | QualType ASTContext::getCanonicalTemplateSpecializationType( | |||
5037 | TemplateName Template, ArrayRef<TemplateArgument> Args) const { | |||
5038 | assert(!Template.getAsDependentTemplateName() &&(static_cast <bool> (!Template.getAsDependentTemplateName () && "No dependent template names here!") ? void (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\"" , "clang/lib/AST/ASTContext.cpp", 5039, __extension__ __PRETTY_FUNCTION__ )) | |||
5039 | "No dependent template names here!")(static_cast <bool> (!Template.getAsDependentTemplateName () && "No dependent template names here!") ? void (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\"" , "clang/lib/AST/ASTContext.cpp", 5039, __extension__ __PRETTY_FUNCTION__ )); | |||
5040 | ||||
5041 | // Look through qualified template names. | |||
5042 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) | |||
5043 | Template = TemplateName(QTN->getUnderlyingTemplate()); | |||
5044 | ||||
5045 | // Build the canonical template specialization type. | |||
5046 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); | |||
5047 | bool AnyNonCanonArgs = false; | |||
5048 | auto CanonArgs = | |||
5049 | ::getCanonicalTemplateArguments(*this, Args, AnyNonCanonArgs); | |||
5050 | ||||
5051 | // Determine whether this canonical template specialization type already | |||
5052 | // exists. | |||
5053 | llvm::FoldingSetNodeID ID; | |||
5054 | TemplateSpecializationType::Profile(ID, CanonTemplate, | |||
5055 | CanonArgs, *this); | |||
5056 | ||||
5057 | void *InsertPos = nullptr; | |||
5058 | TemplateSpecializationType *Spec | |||
5059 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5060 | ||||
5061 | if (!Spec) { | |||
5062 | // Allocate a new canonical template specialization type. | |||
5063 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + | |||
5064 | sizeof(TemplateArgument) * CanonArgs.size()), | |||
5065 | TypeAlignment); | |||
5066 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, | |||
5067 | CanonArgs, | |||
5068 | QualType(), QualType()); | |||
5069 | Types.push_back(Spec); | |||
5070 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); | |||
5071 | } | |||
5072 | ||||
5073 | assert(Spec->isDependentType() &&(static_cast <bool> (Spec->isDependentType() && "Non-dependent template-id type must have a canonical type") ? void (0) : __assert_fail ("Spec->isDependentType() && \"Non-dependent template-id type must have a canonical type\"" , "clang/lib/AST/ASTContext.cpp", 5074, __extension__ __PRETTY_FUNCTION__ )) | |||
5074 | "Non-dependent template-id type must have a canonical type")(static_cast <bool> (Spec->isDependentType() && "Non-dependent template-id type must have a canonical type") ? void (0) : __assert_fail ("Spec->isDependentType() && \"Non-dependent template-id type must have a canonical type\"" , "clang/lib/AST/ASTContext.cpp", 5074, __extension__ __PRETTY_FUNCTION__ )); | |||
5075 | return QualType(Spec, 0); | |||
5076 | } | |||
5077 | ||||
5078 | QualType ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, | |||
5079 | NestedNameSpecifier *NNS, | |||
5080 | QualType NamedType, | |||
5081 | TagDecl *OwnedTagDecl) const { | |||
5082 | llvm::FoldingSetNodeID ID; | |||
5083 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType, OwnedTagDecl); | |||
5084 | ||||
5085 | void *InsertPos = nullptr; | |||
5086 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5087 | if (T) | |||
5088 | return QualType(T, 0); | |||
5089 | ||||
5090 | QualType Canon = NamedType; | |||
5091 | if (!Canon.isCanonical()) { | |||
5092 | Canon = getCanonicalType(NamedType); | |||
5093 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5094 | assert(!CheckT && "Elaborated canonical type broken")(static_cast <bool> (!CheckT && "Elaborated canonical type broken" ) ? void (0) : __assert_fail ("!CheckT && \"Elaborated canonical type broken\"" , "clang/lib/AST/ASTContext.cpp", 5094, __extension__ __PRETTY_FUNCTION__ )); | |||
5095 | (void)CheckT; | |||
5096 | } | |||
5097 | ||||
5098 | void *Mem = Allocate(ElaboratedType::totalSizeToAlloc<TagDecl *>(!!OwnedTagDecl), | |||
5099 | TypeAlignment); | |||
5100 | T = new (Mem) ElaboratedType(Keyword, NNS, NamedType, Canon, OwnedTagDecl); | |||
5101 | ||||
5102 | Types.push_back(T); | |||
5103 | ElaboratedTypes.InsertNode(T, InsertPos); | |||
5104 | return QualType(T, 0); | |||
5105 | } | |||
5106 | ||||
5107 | QualType | |||
5108 | ASTContext::getParenType(QualType InnerType) const { | |||
5109 | llvm::FoldingSetNodeID ID; | |||
5110 | ParenType::Profile(ID, InnerType); | |||
5111 | ||||
5112 | void *InsertPos = nullptr; | |||
5113 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5114 | if (T) | |||
5115 | return QualType(T, 0); | |||
5116 | ||||
5117 | QualType Canon = InnerType; | |||
5118 | if (!Canon.isCanonical()) { | |||
5119 | Canon = getCanonicalType(InnerType); | |||
5120 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5121 | assert(!CheckT && "Paren canonical type broken")(static_cast <bool> (!CheckT && "Paren canonical type broken" ) ? void (0) : __assert_fail ("!CheckT && \"Paren canonical type broken\"" , "clang/lib/AST/ASTContext.cpp", 5121, __extension__ __PRETTY_FUNCTION__ )); | |||
5122 | (void)CheckT; | |||
5123 | } | |||
5124 | ||||
5125 | T = new (*this, TypeAlignment) ParenType(InnerType, Canon); | |||
5126 | Types.push_back(T); | |||
5127 | ParenTypes.InsertNode(T, InsertPos); | |||
5128 | return QualType(T, 0); | |||
5129 | } | |||
5130 | ||||
5131 | QualType | |||
5132 | ASTContext::getMacroQualifiedType(QualType UnderlyingTy, | |||
5133 | const IdentifierInfo *MacroII) const { | |||
5134 | QualType Canon = UnderlyingTy; | |||
5135 | if (!Canon.isCanonical()) | |||
5136 | Canon = getCanonicalType(UnderlyingTy); | |||
5137 | ||||
5138 | auto *newType = new (*this, TypeAlignment) | |||
5139 | MacroQualifiedType(UnderlyingTy, Canon, MacroII); | |||
5140 | Types.push_back(newType); | |||
5141 | return QualType(newType, 0); | |||
5142 | } | |||
5143 | ||||
5144 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, | |||
5145 | NestedNameSpecifier *NNS, | |||
5146 | const IdentifierInfo *Name, | |||
5147 | QualType Canon) const { | |||
5148 | if (Canon.isNull()) { | |||
5149 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); | |||
5150 | if (CanonNNS != NNS) | |||
5151 | Canon = getDependentNameType(Keyword, CanonNNS, Name); | |||
5152 | } | |||
5153 | ||||
5154 | llvm::FoldingSetNodeID ID; | |||
5155 | DependentNameType::Profile(ID, Keyword, NNS, Name); | |||
5156 | ||||
5157 | void *InsertPos = nullptr; | |||
5158 | DependentNameType *T | |||
5159 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5160 | if (T) | |||
5161 | return QualType(T, 0); | |||
5162 | ||||
5163 | T = new (*this, TypeAlignment) DependentNameType(Keyword, NNS, Name, Canon); | |||
5164 | Types.push_back(T); | |||
5165 | DependentNameTypes.InsertNode(T, InsertPos); | |||
5166 | return QualType(T, 0); | |||
5167 | } | |||
5168 | ||||
5169 | QualType ASTContext::getDependentTemplateSpecializationType( | |||
5170 | ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, | |||
5171 | const IdentifierInfo *Name, ArrayRef<TemplateArgumentLoc> Args) const { | |||
5172 | // TODO: avoid this copy | |||
5173 | SmallVector<TemplateArgument, 16> ArgCopy; | |||
5174 | for (unsigned I = 0, E = Args.size(); I != E; ++I) | |||
5175 | ArgCopy.push_back(Args[I].getArgument()); | |||
5176 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, ArgCopy); | |||
5177 | } | |||
5178 | ||||
5179 | QualType | |||
5180 | ASTContext::getDependentTemplateSpecializationType( | |||
5181 | ElaboratedTypeKeyword Keyword, | |||
5182 | NestedNameSpecifier *NNS, | |||
5183 | const IdentifierInfo *Name, | |||
5184 | ArrayRef<TemplateArgument> Args) const { | |||
5185 | assert((!NNS || NNS->isDependent()) &&(static_cast <bool> ((!NNS || NNS->isDependent()) && "nested-name-specifier must be dependent") ? void (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"nested-name-specifier must be dependent\"" , "clang/lib/AST/ASTContext.cpp", 5186, __extension__ __PRETTY_FUNCTION__ )) | |||
5186 | "nested-name-specifier must be dependent")(static_cast <bool> ((!NNS || NNS->isDependent()) && "nested-name-specifier must be dependent") ? void (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"nested-name-specifier must be dependent\"" , "clang/lib/AST/ASTContext.cpp", 5186, __extension__ __PRETTY_FUNCTION__ )); | |||
5187 | ||||
5188 | llvm::FoldingSetNodeID ID; | |||
5189 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, | |||
5190 | Name, Args); | |||
5191 | ||||
5192 | void *InsertPos = nullptr; | |||
5193 | DependentTemplateSpecializationType *T | |||
5194 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5195 | if (T) | |||
5196 | return QualType(T, 0); | |||
5197 | ||||
5198 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); | |||
5199 | ||||
5200 | ElaboratedTypeKeyword CanonKeyword = Keyword; | |||
5201 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; | |||
5202 | ||||
5203 | bool AnyNonCanonArgs = false; | |||
5204 | auto CanonArgs = | |||
5205 | ::getCanonicalTemplateArguments(*this, Args, AnyNonCanonArgs); | |||
5206 | ||||
5207 | QualType Canon; | |||
5208 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { | |||
5209 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, | |||
5210 | Name, | |||
5211 | CanonArgs); | |||
5212 | ||||
5213 | // Find the insert position again. | |||
5214 | [[maybe_unused]] auto *Nothing = | |||
5215 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5216 | assert(!Nothing && "canonical type broken")(static_cast <bool> (!Nothing && "canonical type broken" ) ? void (0) : __assert_fail ("!Nothing && \"canonical type broken\"" , "clang/lib/AST/ASTContext.cpp", 5216, __extension__ __PRETTY_FUNCTION__ )); | |||
5217 | } | |||
5218 | ||||
5219 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + | |||
5220 | sizeof(TemplateArgument) * Args.size()), | |||
5221 | TypeAlignment); | |||
5222 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, | |||
5223 | Name, Args, Canon); | |||
5224 | Types.push_back(T); | |||
5225 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); | |||
5226 | return QualType(T, 0); | |||
5227 | } | |||
5228 | ||||
5229 | TemplateArgument ASTContext::getInjectedTemplateArg(NamedDecl *Param) { | |||
5230 | TemplateArgument Arg; | |||
5231 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { | |||
5232 | QualType ArgType = getTypeDeclType(TTP); | |||
5233 | if (TTP->isParameterPack()) | |||
5234 | ArgType = getPackExpansionType(ArgType, std::nullopt); | |||
5235 | ||||
5236 | Arg = TemplateArgument(ArgType); | |||
5237 | } else if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { | |||
5238 | QualType T = | |||
5239 | NTTP->getType().getNonPackExpansionType().getNonLValueExprType(*this); | |||
5240 | // For class NTTPs, ensure we include the 'const' so the type matches that | |||
5241 | // of a real template argument. | |||
5242 | // FIXME: It would be more faithful to model this as something like an | |||
5243 | // lvalue-to-rvalue conversion applied to a const-qualified lvalue. | |||
5244 | if (T->isRecordType()) | |||
5245 | T.addConst(); | |||
5246 | Expr *E = new (*this) DeclRefExpr( | |||
5247 | *this, NTTP, /*RefersToEnclosingVariableOrCapture*/ false, T, | |||
5248 | Expr::getValueKindForType(NTTP->getType()), NTTP->getLocation()); | |||
5249 | ||||
5250 | if (NTTP->isParameterPack()) | |||
5251 | E = new (*this) | |||
5252 | PackExpansionExpr(DependentTy, E, NTTP->getLocation(), std::nullopt); | |||
5253 | Arg = TemplateArgument(E); | |||
5254 | } else { | |||
5255 | auto *TTP = cast<TemplateTemplateParmDecl>(Param); | |||
5256 | if (TTP->isParameterPack()) | |||
5257 | Arg = TemplateArgument(TemplateName(TTP), std::optional<unsigned>()); | |||
5258 | else | |||
5259 | Arg = TemplateArgument(TemplateName(TTP)); | |||
5260 | } | |||
5261 | ||||
5262 | if (Param->isTemplateParameterPack()) | |||
5263 | Arg = TemplateArgument::CreatePackCopy(*this, Arg); | |||
5264 | ||||
5265 | return Arg; | |||
5266 | } | |||
5267 | ||||
5268 | void | |||
5269 | ASTContext::getInjectedTemplateArgs(const TemplateParameterList *Params, | |||
5270 | SmallVectorImpl<TemplateArgument> &Args) { | |||
5271 | Args.reserve(Args.size() + Params->size()); | |||
5272 | ||||
5273 | for (NamedDecl *Param : *Params) | |||
5274 | Args.push_back(getInjectedTemplateArg(Param)); | |||
5275 | } | |||
5276 | ||||
5277 | QualType ASTContext::getPackExpansionType(QualType Pattern, | |||
5278 | std::optional<unsigned> NumExpansions, | |||
5279 | bool ExpectPackInType) { | |||
5280 | assert((!ExpectPackInType || Pattern->containsUnexpandedParameterPack()) &&(static_cast <bool> ((!ExpectPackInType || Pattern-> containsUnexpandedParameterPack()) && "Pack expansions must expand one or more parameter packs" ) ? void (0) : __assert_fail ("(!ExpectPackInType || Pattern->containsUnexpandedParameterPack()) && \"Pack expansions must expand one or more parameter packs\"" , "clang/lib/AST/ASTContext.cpp", 5281, __extension__ __PRETTY_FUNCTION__ )) | |||
5281 | "Pack expansions must expand one or more parameter packs")(static_cast <bool> ((!ExpectPackInType || Pattern-> containsUnexpandedParameterPack()) && "Pack expansions must expand one or more parameter packs" ) ? void (0) : __assert_fail ("(!ExpectPackInType || Pattern->containsUnexpandedParameterPack()) && \"Pack expansions must expand one or more parameter packs\"" , "clang/lib/AST/ASTContext.cpp", 5281, __extension__ __PRETTY_FUNCTION__ )); | |||
5282 | ||||
5283 | llvm::FoldingSetNodeID ID; | |||
5284 | PackExpansionType::Profile(ID, Pattern, NumExpansions); | |||
5285 | ||||
5286 | void *InsertPos = nullptr; | |||
5287 | PackExpansionType *T = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5288 | if (T) | |||
5289 | return QualType(T, 0); | |||
5290 | ||||
5291 | QualType Canon; | |||
5292 | if (!Pattern.isCanonical()) { | |||
5293 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions, | |||
5294 | /*ExpectPackInType=*/false); | |||
5295 | ||||
5296 | // Find the insert position again, in case we inserted an element into | |||
5297 | // PackExpansionTypes and invalidated our insert position. | |||
5298 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5299 | } | |||
5300 | ||||
5301 | T = new (*this, TypeAlignment) | |||
5302 | PackExpansionType(Pattern, Canon, NumExpansions); | |||
5303 | Types.push_back(T); | |||
5304 | PackExpansionTypes.InsertNode(T, InsertPos); | |||
5305 | return QualType(T, 0); | |||
5306 | } | |||
5307 | ||||
5308 | /// CmpProtocolNames - Comparison predicate for sorting protocols | |||
5309 | /// alphabetically. | |||
5310 | static int CmpProtocolNames(ObjCProtocolDecl *const *LHS, | |||
5311 | ObjCProtocolDecl *const *RHS) { | |||
5312 | return DeclarationName::compare((*LHS)->getDeclName(), (*RHS)->getDeclName()); | |||
5313 | } | |||
5314 | ||||
5315 | static bool areSortedAndUniqued(ArrayRef<ObjCProtocolDecl *> Protocols) { | |||
5316 | if (Protocols.empty()) return true; | |||
5317 | ||||
5318 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) | |||
5319 | return false; | |||
5320 | ||||
5321 | for (unsigned i = 1; i != Protocols.size(); ++i) | |||
5322 | if (CmpProtocolNames(&Protocols[i - 1], &Protocols[i]) >= 0 || | |||
5323 | Protocols[i]->getCanonicalDecl() != Protocols[i]) | |||
5324 | return false; | |||
5325 | return true; | |||
5326 | } | |||
5327 | ||||
5328 | static void | |||
5329 | SortAndUniqueProtocols(SmallVectorImpl<ObjCProtocolDecl *> &Protocols) { | |||
5330 | // Sort protocols, keyed by name. | |||
5331 | llvm::array_pod_sort(Protocols.begin(), Protocols.end(), CmpProtocolNames); | |||
5332 | ||||
5333 | // Canonicalize. | |||
5334 | for (ObjCProtocolDecl *&P : Protocols) | |||
5335 | P = P->getCanonicalDecl(); | |||
5336 | ||||
5337 | // Remove duplicates. | |||
5338 | auto ProtocolsEnd = std::unique(Protocols.begin(), Protocols.end()); | |||
5339 | Protocols.erase(ProtocolsEnd, Protocols.end()); | |||
5340 | } | |||
5341 | ||||
5342 | QualType ASTContext::getObjCObjectType(QualType BaseType, | |||
5343 | ObjCProtocolDecl * const *Protocols, | |||
5344 | unsigned NumProtocols) const { | |||
5345 | return getObjCObjectType(BaseType, {}, | |||
5346 | llvm::ArrayRef(Protocols, NumProtocols), | |||
5347 | /*isKindOf=*/false); | |||
5348 | } | |||
5349 | ||||
5350 | QualType ASTContext::getObjCObjectType( | |||
5351 | QualType baseType, | |||
5352 | ArrayRef<QualType> typeArgs, | |||
5353 | ArrayRef<ObjCProtocolDecl *> protocols, | |||
5354 | bool isKindOf) const { | |||
5355 | // If the base type is an interface and there aren't any protocols or | |||
5356 | // type arguments to add, then the interface type will do just fine. | |||
5357 | if (typeArgs.empty() && protocols.empty() && !isKindOf && | |||
5358 | isa<ObjCInterfaceType>(baseType)) | |||
5359 | return baseType; | |||
5360 | ||||
5361 | // Look in the folding set for an existing type. | |||
5362 | llvm::FoldingSetNodeID ID; | |||
5363 | ObjCObjectTypeImpl::Profile(ID, baseType, typeArgs, protocols, isKindOf); | |||
5364 | void *InsertPos = nullptr; | |||
5365 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
5366 | return QualType(QT, 0); | |||
5367 | ||||
5368 | // Determine the type arguments to be used for canonicalization, | |||
5369 | // which may be explicitly specified here or written on the base | |||
5370 | // type. | |||
5371 | ArrayRef<QualType> effectiveTypeArgs = typeArgs; | |||
5372 | if (effectiveTypeArgs.empty()) { | |||
5373 | if (const auto *baseObject = baseType->getAs<ObjCObjectType>()) | |||
5374 | effectiveTypeArgs = baseObject->getTypeArgs(); | |||
5375 | } | |||
5376 | ||||
5377 | // Build the canonical type, which has the canonical base type and a | |||
5378 | // sorted-and-uniqued list of protocols and the type arguments | |||
5379 | // canonicalized. | |||
5380 | QualType canonical; | |||
5381 | bool typeArgsAreCanonical = llvm::all_of( | |||
5382 | effectiveTypeArgs, [&](QualType type) { return type.isCanonical(); }); | |||
5383 | bool protocolsSorted = areSortedAndUniqued(protocols); | |||
5384 | if (!typeArgsAreCanonical || !protocolsSorted || !baseType.isCanonical()) { | |||
5385 | // Determine the canonical type arguments. | |||
5386 | ArrayRef<QualType> canonTypeArgs; | |||
5387 | SmallVector<QualType, 4> canonTypeArgsVec; | |||
5388 | if (!typeArgsAreCanonical) { | |||
5389 | canonTypeArgsVec.reserve(effectiveTypeArgs.size()); | |||
5390 | for (auto typeArg : effectiveTypeArgs) | |||
5391 | canonTypeArgsVec.push_back(getCanonicalType(typeArg)); | |||
5392 | canonTypeArgs = canonTypeArgsVec; | |||
5393 | } else { | |||
5394 | canonTypeArgs = effectiveTypeArgs; | |||
5395 | } | |||
5396 | ||||
5397 | ArrayRef<ObjCProtocolDecl *> canonProtocols; | |||
5398 | SmallVector<ObjCProtocolDecl*, 8> canonProtocolsVec; | |||
5399 | if (!protocolsSorted) { | |||
5400 | canonProtocolsVec.append(protocols.begin(), protocols.end()); | |||
5401 | SortAndUniqueProtocols(canonProtocolsVec); | |||
5402 | canonProtocols = canonProtocolsVec; | |||
5403 | } else { | |||
5404 | canonProtocols = protocols; | |||
5405 | } | |||
5406 | ||||
5407 | canonical = getObjCObjectType(getCanonicalType(baseType), canonTypeArgs, | |||
5408 | canonProtocols, isKindOf); | |||
5409 | ||||
5410 | // Regenerate InsertPos. | |||
5411 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5412 | } | |||
5413 | ||||
5414 | unsigned size = sizeof(ObjCObjectTypeImpl); | |||
5415 | size += typeArgs.size() * sizeof(QualType); | |||
5416 | size += protocols.size() * sizeof(ObjCProtocolDecl *); | |||
5417 | void *mem = Allocate(size, TypeAlignment); | |||
5418 | auto *T = | |||
5419 | new (mem) ObjCObjectTypeImpl(canonical, baseType, typeArgs, protocols, | |||
5420 | isKindOf); | |||
5421 | ||||
5422 | Types.push_back(T); | |||
5423 | ObjCObjectTypes.InsertNode(T, InsertPos); | |||
5424 | return QualType(T, 0); | |||
5425 | } | |||
5426 | ||||
5427 | /// Apply Objective-C protocol qualifiers to the given type. | |||
5428 | /// If this is for the canonical type of a type parameter, we can apply | |||
5429 | /// protocol qualifiers on the ObjCObjectPointerType. | |||
5430 | QualType | |||
5431 | ASTContext::applyObjCProtocolQualifiers(QualType type, | |||
5432 | ArrayRef<ObjCProtocolDecl *> protocols, bool &hasError, | |||
5433 | bool allowOnPointerType) const { | |||
5434 | hasError = false; | |||
5435 | ||||
5436 | if (const auto *objT = dyn_cast<ObjCTypeParamType>(type.getTypePtr())) { | |||
5437 | return getObjCTypeParamType(objT->getDecl(), protocols); | |||
5438 | } | |||
5439 | ||||
5440 | // Apply protocol qualifiers to ObjCObjectPointerType. | |||
5441 | if (allowOnPointerType) { | |||
5442 | if (const auto *objPtr = | |||
5443 | dyn_cast<ObjCObjectPointerType>(type.getTypePtr())) { | |||
5444 | const ObjCObjectType *objT = objPtr->getObjectType(); | |||
5445 | // Merge protocol lists and construct ObjCObjectType. | |||
5446 | SmallVector<ObjCProtocolDecl*, 8> protocolsVec; | |||
5447 | protocolsVec.append(objT->qual_begin(), | |||
5448 | objT->qual_end()); | |||
5449 | protocolsVec.append(protocols.begin(), protocols.end()); | |||
5450 | ArrayRef<ObjCProtocolDecl *> protocols = protocolsVec; | |||
5451 | type = getObjCObjectType( | |||
5452 | objT->getBaseType(), | |||
5453 | objT->getTypeArgsAsWritten(), | |||
5454 | protocols, | |||
5455 | objT->isKindOfTypeAsWritten()); | |||
5456 | return getObjCObjectPointerType(type); | |||
5457 | } | |||
5458 | } | |||
5459 | ||||
5460 | // Apply protocol qualifiers to ObjCObjectType. | |||
5461 | if (const auto *objT = dyn_cast<ObjCObjectType>(type.getTypePtr())){ | |||
5462 | // FIXME: Check for protocols to which the class type is already | |||
5463 | // known to conform. | |||
5464 | ||||
5465 | return getObjCObjectType(objT->getBaseType(), | |||
5466 | objT->getTypeArgsAsWritten(), | |||
5467 | protocols, | |||
5468 | objT->isKindOfTypeAsWritten()); | |||
5469 | } | |||
5470 | ||||
5471 | // If the canonical type is ObjCObjectType, ... | |||
5472 | if (type->isObjCObjectType()) { | |||
5473 | // Silently overwrite any existing protocol qualifiers. | |||
5474 | // TODO: determine whether that's the right thing to do. | |||
5475 | ||||
5476 | // FIXME: Check for protocols to which the class type is already | |||
5477 | // known to conform. | |||
5478 | return getObjCObjectType(type, {}, protocols, false); | |||
5479 | } | |||
5480 | ||||
5481 | // id<protocol-list> | |||
5482 | if (type->isObjCIdType()) { | |||
5483 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); | |||
5484 | type = getObjCObjectType(ObjCBuiltinIdTy, {}, protocols, | |||
5485 | objPtr->isKindOfType()); | |||
5486 | return getObjCObjectPointerType(type); | |||
5487 | } | |||
5488 | ||||
5489 | // Class<protocol-list> | |||
5490 | if (type->isObjCClassType()) { | |||
5491 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); | |||
5492 | type = getObjCObjectType(ObjCBuiltinClassTy, {}, protocols, | |||
5493 | objPtr->isKindOfType()); | |||
5494 | return getObjCObjectPointerType(type); | |||
5495 | } | |||
5496 | ||||
5497 | hasError = true; | |||
5498 | return type; | |||
5499 | } | |||
5500 | ||||
5501 | QualType | |||
5502 | ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl, | |||
5503 | ArrayRef<ObjCProtocolDecl *> protocols) const { | |||
5504 | // Look in the folding set for an existing type. | |||
5505 | llvm::FoldingSetNodeID ID; | |||
5506 | ObjCTypeParamType::Profile(ID, Decl, Decl->getUnderlyingType(), protocols); | |||
5507 | void *InsertPos = nullptr; | |||
5508 | if (ObjCTypeParamType *TypeParam = | |||
5509 | ObjCTypeParamTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
5510 | return QualType(TypeParam, 0); | |||
5511 | ||||
5512 | // We canonicalize to the underlying type. | |||
5513 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); | |||
5514 | if (!protocols.empty()) { | |||
5515 | // Apply the protocol qualifers. | |||
5516 | bool hasError; | |||
5517 | Canonical = getCanonicalType(applyObjCProtocolQualifiers( | |||
5518 | Canonical, protocols, hasError, true /*allowOnPointerType*/)); | |||
5519 | assert(!hasError && "Error when apply protocol qualifier to bound type")(static_cast <bool> (!hasError && "Error when apply protocol qualifier to bound type" ) ? void (0) : __assert_fail ("!hasError && \"Error when apply protocol qualifier to bound type\"" , "clang/lib/AST/ASTContext.cpp", 5519, __extension__ __PRETTY_FUNCTION__ )); | |||
5520 | } | |||
5521 | ||||
5522 | unsigned size = sizeof(ObjCTypeParamType); | |||
5523 | size += protocols.size() * sizeof(ObjCProtocolDecl *); | |||
5524 | void *mem = Allocate(size, TypeAlignment); | |||
5525 | auto *newType = new (mem) ObjCTypeParamType(Decl, Canonical, protocols); | |||
5526 | ||||
5527 | Types.push_back(newType); | |||
5528 | ObjCTypeParamTypes.InsertNode(newType, InsertPos); | |||
5529 | return QualType(newType, 0); | |||
5530 | } | |||
5531 | ||||
5532 | void ASTContext::adjustObjCTypeParamBoundType(const ObjCTypeParamDecl *Orig, | |||
5533 | ObjCTypeParamDecl *New) const { | |||
5534 | New->setTypeSourceInfo(getTrivialTypeSourceInfo(Orig->getUnderlyingType())); | |||
5535 | // Update TypeForDecl after updating TypeSourceInfo. | |||
5536 | auto NewTypeParamTy = cast<ObjCTypeParamType>(New->getTypeForDecl()); | |||
5537 | SmallVector<ObjCProtocolDecl *, 8> protocols; | |||
5538 | protocols.append(NewTypeParamTy->qual_begin(), NewTypeParamTy->qual_end()); | |||
5539 | QualType UpdatedTy = getObjCTypeParamType(New, protocols); | |||
5540 | New->setTypeForDecl(UpdatedTy.getTypePtr()); | |||
5541 | } | |||
5542 | ||||
5543 | /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's | |||
5544 | /// protocol list adopt all protocols in QT's qualified-id protocol | |||
5545 | /// list. | |||
5546 | bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT, | |||
5547 | ObjCInterfaceDecl *IC) { | |||
5548 | if (!QT->isObjCQualifiedIdType()) | |||
5549 | return false; | |||
5550 | ||||
5551 | if (const auto *OPT = QT->getAs<ObjCObjectPointerType>()) { | |||
5552 | // If both the right and left sides have qualifiers. | |||
5553 | for (auto *Proto : OPT->quals()) { | |||
5554 | if (!IC->ClassImplementsProtocol(Proto, false)) | |||
5555 | return false; | |||
5556 | } | |||
5557 | return true; | |||
5558 | } | |||
5559 | return false; | |||
5560 | } | |||
5561 | ||||
5562 | /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in | |||
5563 | /// QT's qualified-id protocol list adopt all protocols in IDecl's list | |||
5564 | /// of protocols. | |||
5565 | bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT, | |||
5566 | ObjCInterfaceDecl *IDecl) { | |||
5567 | if (!QT->isObjCQualifiedIdType()) | |||
5568 | return false; | |||
5569 | const auto *OPT = QT->getAs<ObjCObjectPointerType>(); | |||
5570 | if (!OPT) | |||
5571 | return false; | |||
5572 | if (!IDecl->hasDefinition()) | |||
5573 | return false; | |||
5574 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols; | |||
5575 | CollectInheritedProtocols(IDecl, InheritedProtocols); | |||
5576 | if (InheritedProtocols.empty()) | |||
5577 | return false; | |||
5578 | // Check that if every protocol in list of id<plist> conforms to a protocol | |||
5579 | // of IDecl's, then bridge casting is ok. | |||
5580 | bool Conforms = false; | |||
5581 | for (auto *Proto : OPT->quals()) { | |||
5582 | Conforms = false; | |||
5583 | for (auto *PI : InheritedProtocols) { | |||
5584 | if (ProtocolCompatibleWithProtocol(Proto, PI)) { | |||
5585 | Conforms = true; | |||
5586 | break; | |||
5587 | } | |||
5588 | } | |||
5589 | if (!Conforms) | |||
5590 | break; | |||
5591 | } | |||
5592 | if (Conforms) | |||
5593 | return true; | |||
5594 | ||||
5595 | for (auto *PI : InheritedProtocols) { | |||
5596 | // If both the right and left sides have qualifiers. | |||
5597 | bool Adopts = false; | |||
5598 | for (auto *Proto : OPT->quals()) { | |||
5599 | // return 'true' if 'PI' is in the inheritance hierarchy of Proto | |||
5600 | if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto))) | |||
5601 | break; | |||
5602 | } | |||
5603 | if (!Adopts) | |||
5604 | return false; | |||
5605 | } | |||
5606 | return true; | |||
5607 | } | |||
5608 | ||||
5609 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for | |||
5610 | /// the given object type. | |||
5611 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { | |||
5612 | llvm::FoldingSetNodeID ID; | |||
5613 | ObjCObjectPointerType::Profile(ID, ObjectT); | |||
5614 | ||||
5615 | void *InsertPos = nullptr; | |||
5616 | if (ObjCObjectPointerType *QT = | |||
5617 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
5618 | return QualType(QT, 0); | |||
5619 | ||||
5620 | // Find the canonical object type. | |||
5621 | QualType Canonical; | |||
5622 | if (!ObjectT.isCanonical()) { | |||
5623 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); | |||
5624 | ||||
5625 | // Regenerate InsertPos. | |||
5626 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5627 | } | |||
5628 | ||||
5629 | // No match. | |||
5630 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); | |||
5631 | auto *QType = | |||
5632 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); | |||
5633 | ||||
5634 | Types.push_back(QType); | |||
5635 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); | |||
5636 | return QualType(QType, 0); | |||
5637 | } | |||
5638 | ||||
5639 | /// getObjCInterfaceType - Return the unique reference to the type for the | |||
5640 | /// specified ObjC interface decl. The list of protocols is optional. | |||
5641 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, | |||
5642 | ObjCInterfaceDecl *PrevDecl) const { | |||
5643 | if (Decl->TypeForDecl) | |||
5644 | return QualType(Decl->TypeForDecl, 0); | |||
5645 | ||||
5646 | if (PrevDecl) { | |||
5647 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl")(static_cast <bool> (PrevDecl->TypeForDecl && "previous decl has no TypeForDecl") ? void (0) : __assert_fail ("PrevDecl->TypeForDecl && \"previous decl has no TypeForDecl\"" , "clang/lib/AST/ASTContext.cpp", 5647, __extension__ __PRETTY_FUNCTION__ )); | |||
5648 | Decl->TypeForDecl = PrevDecl->TypeForDecl; | |||
5649 | return QualType(PrevDecl->TypeForDecl, 0); | |||
5650 | } | |||
5651 | ||||
5652 | // Prefer the definition, if there is one. | |||
5653 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) | |||
5654 | Decl = Def; | |||
5655 | ||||
5656 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); | |||
5657 | auto *T = new (Mem) ObjCInterfaceType(Decl); | |||
5658 | Decl->TypeForDecl = T; | |||
5659 | Types.push_back(T); | |||
5660 | return QualType(T, 0); | |||
5661 | } | |||
5662 | ||||
5663 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique | |||
5664 | /// TypeOfExprType AST's (since expression's are never shared). For example, | |||
5665 | /// multiple declarations that refer to "typeof(x)" all contain different | |||
5666 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates | |||
5667 | /// on canonical type's (which are always unique). | |||
5668 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr, TypeOfKind Kind) const { | |||
5669 | TypeOfExprType *toe; | |||
5670 | if (tofExpr->isTypeDependent()) { | |||
5671 | llvm::FoldingSetNodeID ID; | |||
5672 | DependentTypeOfExprType::Profile(ID, *this, tofExpr, | |||
5673 | Kind == TypeOfKind::Unqualified); | |||
5674 | ||||
5675 | void *InsertPos = nullptr; | |||
5676 | DependentTypeOfExprType *Canon = | |||
5677 | DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5678 | if (Canon) { | |||
5679 | // We already have a "canonical" version of an identical, dependent | |||
5680 | // typeof(expr) type. Use that as our canonical type. | |||
5681 | toe = new (*this, TypeAlignment) | |||
5682 | TypeOfExprType(tofExpr, Kind, QualType((TypeOfExprType *)Canon, 0)); | |||
5683 | } else { | |||
5684 | // Build a new, canonical typeof(expr) type. | |||
5685 | Canon = new (*this, TypeAlignment) | |||
5686 | DependentTypeOfExprType(*this, tofExpr, Kind); | |||
5687 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); | |||
5688 | toe = Canon; | |||
5689 | } | |||
5690 | } else { | |||
5691 | QualType Canonical = getCanonicalType(tofExpr->getType()); | |||
5692 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Kind, Canonical); | |||
5693 | } | |||
5694 | Types.push_back(toe); | |||
5695 | return QualType(toe, 0); | |||
5696 | } | |||
5697 | ||||
5698 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique | |||
5699 | /// TypeOfType nodes. The only motivation to unique these nodes would be | |||
5700 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be | |||
5701 | /// an issue. This doesn't affect the type checker, since it operates | |||
5702 | /// on canonical types (which are always unique). | |||
5703 | QualType ASTContext::getTypeOfType(QualType tofType, TypeOfKind Kind) const { | |||
5704 | QualType Canonical = getCanonicalType(tofType); | |||
5705 | auto *tot = | |||
5706 | new (*this, TypeAlignment) TypeOfType(tofType, Canonical, Kind); | |||
5707 | Types.push_back(tot); | |||
5708 | return QualType(tot, 0); | |||
5709 | } | |||
5710 | ||||
5711 | /// getReferenceQualifiedType - Given an expr, will return the type for | |||
5712 | /// that expression, as in [dcl.type.simple]p4 but without taking id-expressions | |||
5713 | /// and class member access into account. | |||
5714 | QualType ASTContext::getReferenceQualifiedType(const Expr *E) const { | |||
5715 | // C++11 [dcl.type.simple]p4: | |||
5716 | // [...] | |||
5717 | QualType T = E->getType(); | |||
5718 | switch (E->getValueKind()) { | |||
5719 | // - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the | |||
5720 | // type of e; | |||
5721 | case VK_XValue: | |||
5722 | return getRValueReferenceType(T); | |||
5723 | // - otherwise, if e is an lvalue, decltype(e) is T&, where T is the | |||
5724 | // type of e; | |||
5725 | case VK_LValue: | |||
5726 | return getLValueReferenceType(T); | |||
5727 | // - otherwise, decltype(e) is the type of e. | |||
5728 | case VK_PRValue: | |||
5729 | return T; | |||
5730 | } | |||
5731 | llvm_unreachable("Unknown value kind")::llvm::llvm_unreachable_internal("Unknown value kind", "clang/lib/AST/ASTContext.cpp" , 5731); | |||
5732 | } | |||
5733 | ||||
5734 | /// Unlike many "get<Type>" functions, we don't unique DecltypeType | |||
5735 | /// nodes. This would never be helpful, since each such type has its own | |||
5736 | /// expression, and would not give a significant memory saving, since there | |||
5737 | /// is an Expr tree under each such type. | |||
5738 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { | |||
5739 | DecltypeType *dt; | |||
5740 | ||||
5741 | // C++11 [temp.type]p2: | |||
5742 | // If an expression e involves a template parameter, decltype(e) denotes a | |||
5743 | // unique dependent type. Two such decltype-specifiers refer to the same | |||
5744 | // type only if their expressions are equivalent (14.5.6.1). | |||
5745 | if (e->isInstantiationDependent()) { | |||
5746 | llvm::FoldingSetNodeID ID; | |||
5747 | DependentDecltypeType::Profile(ID, *this, e); | |||
5748 | ||||
5749 | void *InsertPos = nullptr; | |||
5750 | DependentDecltypeType *Canon | |||
5751 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5752 | if (!Canon) { | |||
5753 | // Build a new, canonical decltype(expr) type. | |||
5754 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); | |||
5755 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); | |||
5756 | } | |||
5757 | dt = new (*this, TypeAlignment) | |||
5758 | DecltypeType(e, UnderlyingType, QualType((DecltypeType *)Canon, 0)); | |||
5759 | } else { | |||
5760 | dt = new (*this, TypeAlignment) | |||
5761 | DecltypeType(e, UnderlyingType, getCanonicalType(UnderlyingType)); | |||
5762 | } | |||
5763 | Types.push_back(dt); | |||
5764 | return QualType(dt, 0); | |||
5765 | } | |||
5766 | ||||
5767 | /// getUnaryTransformationType - We don't unique these, since the memory | |||
5768 | /// savings are minimal and these are rare. | |||
5769 | QualType ASTContext::getUnaryTransformType(QualType BaseType, | |||
5770 | QualType UnderlyingType, | |||
5771 | UnaryTransformType::UTTKind Kind) | |||
5772 | const { | |||
5773 | UnaryTransformType *ut = nullptr; | |||
5774 | ||||
5775 | if (BaseType->isDependentType()) { | |||
5776 | // Look in the folding set for an existing type. | |||
5777 | llvm::FoldingSetNodeID ID; | |||
5778 | DependentUnaryTransformType::Profile(ID, getCanonicalType(BaseType), Kind); | |||
5779 | ||||
5780 | void *InsertPos = nullptr; | |||
5781 | DependentUnaryTransformType *Canon | |||
5782 | = DependentUnaryTransformTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5783 | ||||
5784 | if (!Canon) { | |||
5785 | // Build a new, canonical __underlying_type(type) type. | |||
5786 | Canon = new (*this, TypeAlignment) | |||
5787 | DependentUnaryTransformType(*this, getCanonicalType(BaseType), | |||
5788 | Kind); | |||
5789 | DependentUnaryTransformTypes.InsertNode(Canon, InsertPos); | |||
5790 | } | |||
5791 | ut = new (*this, TypeAlignment) UnaryTransformType (BaseType, | |||
5792 | QualType(), Kind, | |||
5793 | QualType(Canon, 0)); | |||
5794 | } else { | |||
5795 | QualType CanonType = getCanonicalType(UnderlyingType); | |||
5796 | ut = new (*this, TypeAlignment) UnaryTransformType (BaseType, | |||
5797 | UnderlyingType, Kind, | |||
5798 | CanonType); | |||
5799 | } | |||
5800 | Types.push_back(ut); | |||
5801 | return QualType(ut, 0); | |||
5802 | } | |||
5803 | ||||
5804 | QualType ASTContext::getAutoTypeInternal( | |||
5805 | QualType DeducedType, AutoTypeKeyword Keyword, bool IsDependent, | |||
5806 | bool IsPack, ConceptDecl *TypeConstraintConcept, | |||
5807 | ArrayRef<TemplateArgument> TypeConstraintArgs, bool IsCanon) const { | |||
5808 | if (DeducedType.isNull() && Keyword == AutoTypeKeyword::Auto && | |||
5809 | !TypeConstraintConcept && !IsDependent) | |||
5810 | return getAutoDeductType(); | |||
5811 | ||||
5812 | // Look in the folding set for an existing type. | |||
5813 | void *InsertPos = nullptr; | |||
5814 | llvm::FoldingSetNodeID ID; | |||
5815 | AutoType::Profile(ID, *this, DeducedType, Keyword, IsDependent, | |||
5816 | TypeConstraintConcept, TypeConstraintArgs); | |||
5817 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
5818 | return QualType(AT, 0); | |||
5819 | ||||
5820 | QualType Canon; | |||
5821 | if (!IsCanon) { | |||
5822 | if (!DeducedType.isNull()) { | |||
5823 | Canon = DeducedType.getCanonicalType(); | |||
5824 | } else if (TypeConstraintConcept) { | |||
5825 | bool AnyNonCanonArgs = false; | |||
5826 | ConceptDecl *CanonicalConcept = TypeConstraintConcept->getCanonicalDecl(); | |||
5827 | auto CanonicalConceptArgs = ::getCanonicalTemplateArguments( | |||
5828 | *this, TypeConstraintArgs, AnyNonCanonArgs); | |||
5829 | if (CanonicalConcept != TypeConstraintConcept || AnyNonCanonArgs) { | |||
5830 | Canon = | |||
5831 | getAutoTypeInternal(QualType(), Keyword, IsDependent, IsPack, | |||
5832 | CanonicalConcept, CanonicalConceptArgs, true); | |||
5833 | // Find the insert position again. | |||
5834 | [[maybe_unused]] auto *Nothing = | |||
5835 | AutoTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5836 | assert(!Nothing && "canonical type broken")(static_cast <bool> (!Nothing && "canonical type broken" ) ? void (0) : __assert_fail ("!Nothing && \"canonical type broken\"" , "clang/lib/AST/ASTContext.cpp", 5836, __extension__ __PRETTY_FUNCTION__ )); | |||
5837 | } | |||
5838 | } | |||
5839 | } | |||
5840 | ||||
5841 | void *Mem = Allocate(sizeof(AutoType) + | |||
5842 | sizeof(TemplateArgument) * TypeConstraintArgs.size(), | |||
5843 | TypeAlignment); | |||
5844 | auto *AT = new (Mem) AutoType( | |||
5845 | DeducedType, Keyword, | |||
5846 | (IsDependent ? TypeDependence::DependentInstantiation | |||
5847 | : TypeDependence::None) | | |||
5848 | (IsPack ? TypeDependence::UnexpandedPack : TypeDependence::None), | |||
5849 | Canon, TypeConstraintConcept, TypeConstraintArgs); | |||
5850 | Types.push_back(AT); | |||
5851 | AutoTypes.InsertNode(AT, InsertPos); | |||
5852 | return QualType(AT, 0); | |||
5853 | } | |||
5854 | ||||
5855 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been | |||
5856 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the | |||
5857 | /// canonical deduced-but-dependent 'auto' type. | |||
5858 | QualType | |||
5859 | ASTContext::getAutoType(QualType DeducedType, AutoTypeKeyword Keyword, | |||
5860 | bool IsDependent, bool IsPack, | |||
5861 | ConceptDecl *TypeConstraintConcept, | |||
5862 | ArrayRef<TemplateArgument> TypeConstraintArgs) const { | |||
5863 | assert((!IsPack || IsDependent) && "only use IsPack for a dependent pack")(static_cast <bool> ((!IsPack || IsDependent) && "only use IsPack for a dependent pack") ? void (0) : __assert_fail ("(!IsPack || IsDependent) && \"only use IsPack for a dependent pack\"" , "clang/lib/AST/ASTContext.cpp", 5863, __extension__ __PRETTY_FUNCTION__ )); | |||
5864 | assert((!IsDependent || DeducedType.isNull()) &&(static_cast <bool> ((!IsDependent || DeducedType.isNull ()) && "A dependent auto should be undeduced") ? void (0) : __assert_fail ("(!IsDependent || DeducedType.isNull()) && \"A dependent auto should be undeduced\"" , "clang/lib/AST/ASTContext.cpp", 5865, __extension__ __PRETTY_FUNCTION__ )) | |||
5865 | "A dependent auto should be undeduced")(static_cast <bool> ((!IsDependent || DeducedType.isNull ()) && "A dependent auto should be undeduced") ? void (0) : __assert_fail ("(!IsDependent || DeducedType.isNull()) && \"A dependent auto should be undeduced\"" , "clang/lib/AST/ASTContext.cpp", 5865, __extension__ __PRETTY_FUNCTION__ )); | |||
5866 | return getAutoTypeInternal(DeducedType, Keyword, IsDependent, IsPack, | |||
5867 | TypeConstraintConcept, TypeConstraintArgs); | |||
5868 | } | |||
5869 | ||||
5870 | QualType ASTContext::getUnconstrainedType(QualType T) const { | |||
5871 | QualType CanonT = T.getCanonicalType(); | |||
5872 | ||||
5873 | // Remove a type-constraint from a top-level auto or decltype(auto). | |||
5874 | if (auto *AT = CanonT->getAs<AutoType>()) { | |||
5875 | if (!AT->isConstrained()) | |||
5876 | return T; | |||
5877 | return getQualifiedType(getAutoType(QualType(), AT->getKeyword(), false, | |||
5878 | AT->containsUnexpandedParameterPack()), | |||
5879 | T.getQualifiers()); | |||
5880 | } | |||
5881 | ||||
5882 | // FIXME: We only support constrained auto at the top level in the type of a | |||
5883 | // non-type template parameter at the moment. Once we lift that restriction, | |||
5884 | // we'll need to recursively build types containing auto here. | |||
5885 | assert(!CanonT->getContainedAutoType() ||(static_cast <bool> (!CanonT->getContainedAutoType() || !CanonT->getContainedAutoType()->isConstrained()) ? void (0) : __assert_fail ("!CanonT->getContainedAutoType() || !CanonT->getContainedAutoType()->isConstrained()" , "clang/lib/AST/ASTContext.cpp", 5886, __extension__ __PRETTY_FUNCTION__ )) | |||
5886 | !CanonT->getContainedAutoType()->isConstrained())(static_cast <bool> (!CanonT->getContainedAutoType() || !CanonT->getContainedAutoType()->isConstrained()) ? void (0) : __assert_fail ("!CanonT->getContainedAutoType() || !CanonT->getContainedAutoType()->isConstrained()" , "clang/lib/AST/ASTContext.cpp", 5886, __extension__ __PRETTY_FUNCTION__ )); | |||
5887 | return T; | |||
5888 | } | |||
5889 | ||||
5890 | /// Return the uniqued reference to the deduced template specialization type | |||
5891 | /// which has been deduced to the given type, or to the canonical undeduced | |||
5892 | /// such type, or the canonical deduced-but-dependent such type. | |||
5893 | QualType ASTContext::getDeducedTemplateSpecializationType( | |||
5894 | TemplateName Template, QualType DeducedType, bool IsDependent) const { | |||
5895 | // Look in the folding set for an existing type. | |||
5896 | void *InsertPos = nullptr; | |||
5897 | llvm::FoldingSetNodeID ID; | |||
5898 | DeducedTemplateSpecializationType::Profile(ID, Template, DeducedType, | |||
5899 | IsDependent); | |||
5900 | if (DeducedTemplateSpecializationType *DTST = | |||
5901 | DeducedTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
5902 | return QualType(DTST, 0); | |||
5903 | ||||
5904 | auto *DTST = new (*this, TypeAlignment) | |||
5905 | DeducedTemplateSpecializationType(Template, DeducedType, IsDependent); | |||
5906 | llvm::FoldingSetNodeID TempID; | |||
5907 | DTST->Profile(TempID); | |||
5908 | assert(ID == TempID && "ID does not match")(static_cast <bool> (ID == TempID && "ID does not match" ) ? void (0) : __assert_fail ("ID == TempID && \"ID does not match\"" , "clang/lib/AST/ASTContext.cpp", 5908, __extension__ __PRETTY_FUNCTION__ )); | |||
5909 | Types.push_back(DTST); | |||
5910 | DeducedTemplateSpecializationTypes.InsertNode(DTST, InsertPos); | |||
5911 | return QualType(DTST, 0); | |||
5912 | } | |||
5913 | ||||
5914 | /// getAtomicType - Return the uniqued reference to the atomic type for | |||
5915 | /// the given value type. | |||
5916 | QualType ASTContext::getAtomicType(QualType T) const { | |||
5917 | // Unique pointers, to guarantee there is only one pointer of a particular | |||
5918 | // structure. | |||
5919 | llvm::FoldingSetNodeID ID; | |||
5920 | AtomicType::Profile(ID, T); | |||
5921 | ||||
5922 | void *InsertPos = nullptr; | |||
5923 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) | |||
5924 | return QualType(AT, 0); | |||
5925 | ||||
5926 | // If the atomic value type isn't canonical, this won't be a canonical type | |||
5927 | // either, so fill in the canonical type field. | |||
5928 | QualType Canonical; | |||
5929 | if (!T.isCanonical()) { | |||
5930 | Canonical = getAtomicType(getCanonicalType(T)); | |||
5931 | ||||
5932 | // Get the new insert position for the node we care about. | |||
5933 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); | |||
5934 | assert(!NewIP && "Shouldn't be in the map!")(static_cast <bool> (!NewIP && "Shouldn't be in the map!" ) ? void (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\"" , "clang/lib/AST/ASTContext.cpp", 5934, __extension__ __PRETTY_FUNCTION__ )); (void)NewIP; | |||
5935 | } | |||
5936 | auto *New = new (*this, TypeAlignment) AtomicType(T, Canonical); | |||
5937 | Types.push_back(New); | |||
5938 | AtomicTypes.InsertNode(New, InsertPos); | |||
5939 | return QualType(New, 0); | |||
5940 | } | |||
5941 | ||||
5942 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. | |||
5943 | QualType ASTContext::getAutoDeductType() const { | |||
5944 | if (AutoDeductTy.isNull()) | |||
5945 | AutoDeductTy = QualType(new (*this, TypeAlignment) | |||
5946 | AutoType(QualType(), AutoTypeKeyword::Auto, | |||
5947 | TypeDependence::None, QualType(), | |||
5948 | /*concept*/ nullptr, /*args*/ {}), | |||
5949 | 0); | |||
5950 | return AutoDeductTy; | |||
5951 | } | |||
5952 | ||||
5953 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. | |||
5954 | QualType ASTContext::getAutoRRefDeductType() const { | |||
5955 | if (AutoRRefDeductTy.isNull()) | |||
5956 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); | |||
5957 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern")(static_cast <bool> (!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern") ? void (0) : __assert_fail ("!AutoRRefDeductTy.isNull() && \"can't build 'auto &&' pattern\"" , "clang/lib/AST/ASTContext.cpp", 5957, __extension__ __PRETTY_FUNCTION__ )); | |||
5958 | return AutoRRefDeductTy; | |||
5959 | } | |||
5960 | ||||
5961 | /// getTagDeclType - Return the unique reference to the type for the | |||
5962 | /// specified TagDecl (struct/union/class/enum) decl. | |||
5963 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { | |||
5964 | assert(Decl)(static_cast <bool> (Decl) ? void (0) : __assert_fail ( "Decl", "clang/lib/AST/ASTContext.cpp", 5964, __extension__ __PRETTY_FUNCTION__ )); | |||
5965 | // FIXME: What is the design on getTagDeclType when it requires casting | |||
5966 | // away const? mutable? | |||
5967 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); | |||
5968 | } | |||
5969 | ||||
5970 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result | |||
5971 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and | |||
5972 | /// needs to agree with the definition in <stddef.h>. | |||
5973 | CanQualType ASTContext::getSizeType() const { | |||
5974 | return getFromTargetType(Target->getSizeType()); | |||
5975 | } | |||
5976 | ||||
5977 | /// Return the unique signed counterpart of the integer type | |||
5978 | /// corresponding to size_t. | |||
5979 | CanQualType ASTContext::getSignedSizeType() const { | |||
5980 | return getFromTargetType(Target->getSignedSizeType()); | |||
5981 | } | |||
5982 | ||||
5983 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). | |||
5984 | CanQualType ASTContext::getIntMaxType() const { | |||
5985 | return getFromTargetType(Target->getIntMaxType()); | |||
5986 | } | |||
5987 | ||||
5988 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). | |||
5989 | CanQualType ASTContext::getUIntMaxType() const { | |||
5990 | return getFromTargetType(Target->getUIntMaxType()); | |||
5991 | } | |||
5992 | ||||
5993 | /// getSignedWCharType - Return the type of "signed wchar_t". | |||
5994 | /// Used when in C++, as a GCC extension. | |||
5995 | QualType ASTContext::getSignedWCharType() const { | |||
5996 | // FIXME: derive from "Target" ? | |||
5997 | return WCharTy; | |||
5998 | } | |||
5999 | ||||
6000 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". | |||
6001 | /// Used when in C++, as a GCC extension. | |||
6002 | QualType ASTContext::getUnsignedWCharType() const { | |||
6003 | // FIXME: derive from "Target" ? | |||
6004 | return UnsignedIntTy; | |||
6005 | } | |||
6006 | ||||
6007 | QualType ASTContext::getIntPtrType() const { | |||
6008 | return getFromTargetType(Target->getIntPtrType()); | |||
6009 | } | |||
6010 | ||||
6011 | QualType ASTContext::getUIntPtrType() const { | |||
6012 | return getCorrespondingUnsignedType(getIntPtrType()); | |||
6013 | } | |||
6014 | ||||
6015 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) | |||
6016 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). | |||
6017 | QualType ASTContext::getPointerDiffType() const { | |||
6018 | return getFromTargetType(Target->getPtrDiffType(LangAS::Default)); | |||
6019 | } | |||
6020 | ||||
6021 | /// Return the unique unsigned counterpart of "ptrdiff_t" | |||
6022 | /// integer type. The standard (C11 7.21.6.1p7) refers to this type | |||
6023 | /// in the definition of %tu format specifier. | |||
6024 | QualType ASTContext::getUnsignedPointerDiffType() const { | |||
6025 | return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default)); | |||
6026 | } | |||
6027 | ||||
6028 | /// Return the unique type for "pid_t" defined in | |||
6029 | /// <sys/types.h>. We need this to compute the correct type for vfork(). | |||
6030 | QualType ASTContext::getProcessIDType() const { | |||
6031 | return getFromTargetType(Target->getProcessIDType()); | |||
6032 | } | |||
6033 | ||||
6034 | //===----------------------------------------------------------------------===// | |||
6035 | // Type Operators | |||
6036 | //===----------------------------------------------------------------------===// | |||
6037 | ||||
6038 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { | |||
6039 | // Push qualifiers into arrays, and then discard any remaining | |||
6040 | // qualifiers. | |||
6041 | T = getCanonicalType(T); | |||
6042 | T = getVariableArrayDecayedType(T); | |||
6043 | const Type *Ty = T.getTypePtr(); | |||
6044 | QualType Result; | |||
6045 | if (isa<ArrayType>(Ty)) { | |||
6046 | Result = getArrayDecayedType(QualType(Ty,0)); | |||
6047 | } else if (isa<FunctionType>(Ty)) { | |||
6048 | Result = getPointerType(QualType(Ty, 0)); | |||
6049 | } else { | |||
6050 | Result = QualType(Ty, 0); | |||
6051 | } | |||
6052 | ||||
6053 | return CanQualType::CreateUnsafe(Result); | |||
6054 | } | |||
6055 | ||||
6056 | QualType ASTContext::getUnqualifiedArrayType(QualType type, | |||
6057 | Qualifiers &quals) { | |||
6058 | SplitQualType splitType = type.getSplitUnqualifiedType(); | |||
6059 | ||||
6060 | // FIXME: getSplitUnqualifiedType() actually walks all the way to | |||
6061 | // the unqualified desugared type and then drops it on the floor. | |||
6062 | // We then have to strip that sugar back off with | |||
6063 | // getUnqualifiedDesugaredType(), which is silly. | |||
6064 | const auto *AT = | |||
6065 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); | |||
6066 | ||||
6067 | // If we don't have an array, just use the results in splitType. | |||
6068 | if (!AT) { | |||
6069 | quals = splitType.Quals; | |||
6070 | return QualType(splitType.Ty, 0); | |||
6071 | } | |||
6072 | ||||
6073 | // Otherwise, recurse on the array's element type. | |||
6074 | QualType elementType = AT->getElementType(); | |||
6075 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); | |||
6076 | ||||
6077 | // If that didn't change the element type, AT has no qualifiers, so we | |||
6078 | // can just use the results in splitType. | |||
6079 | if (elementType == unqualElementType) { | |||
6080 | assert(quals.empty())(static_cast <bool> (quals.empty()) ? void (0) : __assert_fail ("quals.empty()", "clang/lib/AST/ASTContext.cpp", 6080, __extension__ __PRETTY_FUNCTION__)); // from the recursive call | |||
6081 | quals = splitType.Quals; | |||
6082 | return QualType(splitType.Ty, 0); | |||
6083 | } | |||
6084 | ||||
6085 | // Otherwise, add in the qualifiers from the outermost type, then | |||
6086 | // build the type back up. | |||
6087 | quals.addConsistentQualifiers(splitType.Quals); | |||
6088 | ||||
6089 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) { | |||
6090 | return getConstantArrayType(unqualElementType, CAT->getSize(), | |||
6091 | CAT->getSizeExpr(), CAT->getSizeModifier(), 0); | |||
6092 | } | |||
6093 | ||||
6094 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(AT)) { | |||
6095 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); | |||
6096 | } | |||
6097 | ||||
6098 | if (const auto *VAT = dyn_cast<VariableArrayType>(AT)) { | |||
6099 | return getVariableArrayType(unqualElementType, | |||
6100 | VAT->getSizeExpr(), | |||
6101 | VAT->getSizeModifier(), | |||
6102 | VAT->getIndexTypeCVRQualifiers(), | |||
6103 | VAT->getBracketsRange()); | |||
6104 | } | |||
6105 | ||||
6106 | const auto *DSAT = cast<DependentSizedArrayType>(AT); | |||
6107 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), | |||
6108 | DSAT->getSizeModifier(), 0, | |||
6109 | SourceRange()); | |||
6110 | } | |||
6111 | ||||
6112 | /// Attempt to unwrap two types that may both be array types with the same bound | |||
6113 | /// (or both be array types of unknown bound) for the purpose of comparing the | |||
6114 | /// cv-decomposition of two types per C++ [conv.qual]. | |||
6115 | /// | |||
6116 | /// \param AllowPiMismatch Allow the Pi1 and Pi2 to differ as described in | |||
6117 | /// C++20 [conv.qual], if permitted by the current language mode. | |||
6118 | void ASTContext::UnwrapSimilarArrayTypes(QualType &T1, QualType &T2, | |||
6119 | bool AllowPiMismatch) { | |||
6120 | while (true) { | |||
6121 | auto *AT1 = getAsArrayType(T1); | |||
6122 | if (!AT1) | |||
6123 | return; | |||
6124 | ||||
6125 | auto *AT2 = getAsArrayType(T2); | |||
6126 | if (!AT2) | |||
6127 | return; | |||
6128 | ||||
6129 | // If we don't have two array types with the same constant bound nor two | |||
6130 | // incomplete array types, we've unwrapped everything we can. | |||
6131 | // C++20 also permits one type to be a constant array type and the other | |||
6132 | // to be an incomplete array type. | |||
6133 | // FIXME: Consider also unwrapping array of unknown bound and VLA. | |||
6134 | if (auto *CAT1 = dyn_cast<ConstantArrayType>(AT1)) { | |||
6135 | auto *CAT2 = dyn_cast<ConstantArrayType>(AT2); | |||
6136 | if (!((CAT2 && CAT1->getSize() == CAT2->getSize()) || | |||
6137 | (AllowPiMismatch && getLangOpts().CPlusPlus20 && | |||
6138 | isa<IncompleteArrayType>(AT2)))) | |||
6139 | return; | |||
6140 | } else if (isa<IncompleteArrayType>(AT1)) { | |||
6141 | if (!(isa<IncompleteArrayType>(AT2) || | |||
6142 | (AllowPiMismatch && getLangOpts().CPlusPlus20 && | |||
6143 | isa<ConstantArrayType>(AT2)))) | |||
6144 | return; | |||
6145 | } else { | |||
6146 | return; | |||
6147 | } | |||
6148 | ||||
6149 | T1 = AT1->getElementType(); | |||
6150 | T2 = AT2->getElementType(); | |||
6151 | } | |||
6152 | } | |||
6153 | ||||
6154 | /// Attempt to unwrap two types that may be similar (C++ [conv.qual]). | |||
6155 | /// | |||
6156 | /// If T1 and T2 are both pointer types of the same kind, or both array types | |||
6157 | /// with the same bound, unwraps layers from T1 and T2 until a pointer type is | |||
6158 | /// unwrapped. Top-level qualifiers on T1 and T2 are ignored. | |||
6159 | /// | |||
6160 | /// This function will typically be called in a loop that successively | |||
6161 | /// "unwraps" pointer and pointer-to-member types to compare them at each | |||
6162 | /// level. | |||
6163 | /// | |||
6164 | /// \param AllowPiMismatch Allow the Pi1 and Pi2 to differ as described in | |||
6165 | /// C++20 [conv.qual], if permitted by the current language mode. | |||
6166 | /// | |||
6167 | /// \return \c true if a pointer type was unwrapped, \c false if we reached a | |||
6168 | /// pair of types that can't be unwrapped further. | |||
6169 | bool ASTContext::UnwrapSimilarTypes(QualType &T1, QualType &T2, | |||
6170 | bool AllowPiMismatch) { | |||
6171 | UnwrapSimilarArrayTypes(T1, T2, AllowPiMismatch); | |||
6172 | ||||
6173 | const auto *T1PtrType = T1->getAs<PointerType>(); | |||
6174 | const auto *T2PtrType = T2->getAs<PointerType>(); | |||
6175 | if (T1PtrType && T2PtrType) { | |||
6176 | T1 = T1PtrType->getPointeeType(); | |||
6177 | T2 = T2PtrType->getPointeeType(); | |||
6178 | return true; | |||
6179 | } | |||
6180 | ||||
6181 | const auto *T1MPType = T1->getAs<MemberPointerType>(); | |||
6182 | const auto *T2MPType = T2->getAs<MemberPointerType>(); | |||
6183 | if (T1MPType && T2MPType && | |||
6184 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), | |||
6185 | QualType(T2MPType->getClass(), 0))) { | |||
6186 | T1 = T1MPType->getPointeeType(); | |||
6187 | T2 = T2MPType->getPointeeType(); | |||
6188 | return true; | |||
6189 | } | |||
6190 | ||||
6191 | if (getLangOpts().ObjC) { | |||
6192 | const auto *T1OPType = T1->getAs<ObjCObjectPointerType>(); | |||
6193 | const auto *T2OPType = T2->getAs<ObjCObjectPointerType>(); | |||
6194 | if (T1OPType && T2OPType) { | |||
6195 | T1 = T1OPType->getPointeeType(); | |||
6196 | T2 = T2OPType->getPointeeType(); | |||
6197 | return true; | |||
6198 | } | |||
6199 | } | |||
6200 | ||||
6201 | // FIXME: Block pointers, too? | |||
6202 | ||||
6203 | return false; | |||
6204 | } | |||
6205 | ||||
6206 | bool ASTContext::hasSimilarType(QualType T1, QualType T2) { | |||
6207 | while (true) { | |||
6208 | Qualifiers Quals; | |||
6209 | T1 = getUnqualifiedArrayType(T1, Quals); | |||
6210 | T2 = getUnqualifiedArrayType(T2, Quals); | |||
6211 | if (hasSameType(T1, T2)) | |||
6212 | return true; | |||
6213 | if (!UnwrapSimilarTypes(T1, T2)) | |||
6214 | return false; | |||
6215 | } | |||
6216 | } | |||
6217 | ||||
6218 | bool ASTContext::hasCvrSimilarType(QualType T1, QualType T2) { | |||
6219 | while (true) { | |||
6220 | Qualifiers Quals1, Quals2; | |||
6221 | T1 = getUnqualifiedArrayType(T1, Quals1); | |||
6222 | T2 = getUnqualifiedArrayType(T2, Quals2); | |||
6223 | ||||
6224 | Quals1.removeCVRQualifiers(); | |||
6225 | Quals2.removeCVRQualifiers(); | |||
6226 | if (Quals1 != Quals2) | |||
6227 | return false; | |||
6228 | ||||
6229 | if (hasSameType(T1, T2)) | |||
6230 | return true; | |||
6231 | ||||
6232 | if (!UnwrapSimilarTypes(T1, T2, /*AllowPiMismatch*/ false)) | |||
6233 | return false; | |||
6234 | } | |||
6235 | } | |||
6236 | ||||
6237 | DeclarationNameInfo | |||
6238 | ASTContext::getNameForTemplate(TemplateName Name, | |||
6239 | SourceLocation NameLoc) const { | |||
6240 | switch (Name.getKind()) { | |||
6241 | case TemplateName::QualifiedTemplate: | |||
6242 | case TemplateName::Template: | |||
6243 | // DNInfo work in progress: CHECKME: what about DNLoc? | |||
6244 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), | |||
6245 | NameLoc); | |||
6246 | ||||
6247 | case TemplateName::OverloadedTemplate: { | |||
6248 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); | |||
6249 | // DNInfo work in progress: CHECKME: what about DNLoc? | |||
6250 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); | |||
6251 | } | |||
6252 | ||||
6253 | case TemplateName::AssumedTemplate: { | |||
6254 | AssumedTemplateStorage *Storage = Name.getAsAssumedTemplateName(); | |||
6255 | return DeclarationNameInfo(Storage->getDeclName(), NameLoc); | |||
6256 | } | |||
6257 | ||||
6258 | case TemplateName::DependentTemplate: { | |||
6259 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); | |||
6260 | DeclarationName DName; | |||
6261 | if (DTN->isIdentifier()) { | |||
6262 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); | |||
6263 | return DeclarationNameInfo(DName, NameLoc); | |||
6264 | } else { | |||
6265 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); | |||
6266 | // DNInfo work in progress: FIXME: source locations? | |||
6267 | DeclarationNameLoc DNLoc = | |||
6268 | DeclarationNameLoc::makeCXXOperatorNameLoc(SourceRange()); | |||
6269 | return DeclarationNameInfo(DName, NameLoc, DNLoc); | |||
6270 | } | |||
6271 | } | |||
6272 | ||||
6273 | case TemplateName::SubstTemplateTemplateParm: { | |||
6274 | SubstTemplateTemplateParmStorage *subst | |||
6275 | = Name.getAsSubstTemplateTemplateParm(); | |||
6276 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), | |||
6277 | NameLoc); | |||
6278 | } | |||
6279 | ||||
6280 | case TemplateName::SubstTemplateTemplateParmPack: { | |||
6281 | SubstTemplateTemplateParmPackStorage *subst | |||
6282 | = Name.getAsSubstTemplateTemplateParmPack(); | |||
6283 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), | |||
6284 | NameLoc); | |||
6285 | } | |||
6286 | case TemplateName::UsingTemplate: | |||
6287 | return DeclarationNameInfo(Name.getAsUsingShadowDecl()->getDeclName(), | |||
6288 | NameLoc); | |||
6289 | } | |||
6290 | ||||
6291 | llvm_unreachable("bad template name kind!")::llvm::llvm_unreachable_internal("bad template name kind!", "clang/lib/AST/ASTContext.cpp" , 6291); | |||
6292 | } | |||
6293 | ||||
6294 | TemplateName | |||
6295 | ASTContext::getCanonicalTemplateName(const TemplateName &Name) const { | |||
6296 | switch (Name.getKind()) { | |||
6297 | case TemplateName::UsingTemplate: | |||
6298 | case TemplateName::QualifiedTemplate: | |||
6299 | case TemplateName::Template: { | |||
6300 | TemplateDecl *Template = Name.getAsTemplateDecl(); | |||
6301 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Template)) | |||
6302 | Template = getCanonicalTemplateTemplateParmDecl(TTP); | |||
6303 | ||||
6304 | // The canonical template name is the canonical template declaration. | |||
6305 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); | |||
6306 | } | |||
6307 | ||||
6308 | case TemplateName::OverloadedTemplate: | |||
6309 | case TemplateName::AssumedTemplate: | |||
6310 | llvm_unreachable("cannot canonicalize unresolved template")::llvm::llvm_unreachable_internal("cannot canonicalize unresolved template" , "clang/lib/AST/ASTContext.cpp", 6310); | |||
6311 | ||||
6312 | case TemplateName::DependentTemplate: { | |||
6313 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); | |||
6314 | assert(DTN && "Non-dependent template names must refer to template decls.")(static_cast <bool> (DTN && "Non-dependent template names must refer to template decls." ) ? void (0) : __assert_fail ("DTN && \"Non-dependent template names must refer to template decls.\"" , "clang/lib/AST/ASTContext.cpp", 6314, __extension__ __PRETTY_FUNCTION__ )); | |||
6315 | return DTN->CanonicalTemplateName; | |||
6316 | } | |||
6317 | ||||
6318 | case TemplateName::SubstTemplateTemplateParm: { | |||
6319 | SubstTemplateTemplateParmStorage *subst | |||
6320 | = Name.getAsSubstTemplateTemplateParm(); | |||
6321 | return getCanonicalTemplateName(subst->getReplacement()); | |||
6322 | } | |||
6323 | ||||
6324 | case TemplateName::SubstTemplateTemplateParmPack: { | |||
6325 | SubstTemplateTemplateParmPackStorage *subst = | |||
6326 | Name.getAsSubstTemplateTemplateParmPack(); | |||
6327 | TemplateArgument canonArgPack = | |||
6328 | getCanonicalTemplateArgument(subst->getArgumentPack()); | |||
6329 | return getSubstTemplateTemplateParmPack( | |||
6330 | canonArgPack, subst->getAssociatedDecl()->getCanonicalDecl(), | |||
6331 | subst->getFinal(), subst->getIndex()); | |||
6332 | } | |||
6333 | } | |||
6334 | ||||
6335 | llvm_unreachable("bad template name!")::llvm::llvm_unreachable_internal("bad template name!", "clang/lib/AST/ASTContext.cpp" , 6335); | |||
6336 | } | |||
6337 | ||||
6338 | bool ASTContext::hasSameTemplateName(const TemplateName &X, | |||
6339 | const TemplateName &Y) const { | |||
6340 | return getCanonicalTemplateName(X).getAsVoidPointer() == | |||
6341 | getCanonicalTemplateName(Y).getAsVoidPointer(); | |||
6342 | } | |||
6343 | ||||
6344 | bool ASTContext::isSameConstraintExpr(const Expr *XCE, const Expr *YCE) const { | |||
6345 | if (!XCE != !YCE) | |||
6346 | return false; | |||
6347 | ||||
6348 | if (!XCE) | |||
6349 | return true; | |||
6350 | ||||
6351 | llvm::FoldingSetNodeID XCEID, YCEID; | |||
6352 | XCE->Profile(XCEID, *this, /*Canonical=*/true); | |||
6353 | YCE->Profile(YCEID, *this, /*Canonical=*/true); | |||
6354 | return XCEID == YCEID; | |||
6355 | } | |||
6356 | ||||
6357 | bool ASTContext::isSameTypeConstraint(const TypeConstraint *XTC, | |||
6358 | const TypeConstraint *YTC) const { | |||
6359 | if (!XTC != !YTC) | |||
6360 | return false; | |||
6361 | ||||
6362 | if (!XTC) | |||
6363 | return true; | |||
6364 | ||||
6365 | auto *NCX = XTC->getNamedConcept(); | |||
6366 | auto *NCY = YTC->getNamedConcept(); | |||
6367 | if (!NCX || !NCY || !isSameEntity(NCX, NCY)) | |||
6368 | return false; | |||
6369 | if (XTC->hasExplicitTemplateArgs() != YTC->hasExplicitTemplateArgs()) | |||
6370 | return false; | |||
6371 | if (XTC->hasExplicitTemplateArgs()) | |||
6372 | if (XTC->getTemplateArgsAsWritten()->NumTemplateArgs != | |||
6373 | YTC->getTemplateArgsAsWritten()->NumTemplateArgs) | |||
6374 | return false; | |||
6375 | ||||
6376 | // Compare slowly by profiling. | |||
6377 | // | |||
6378 | // We couldn't compare the profiling result for the template | |||
6379 | // args here. Consider the following example in different modules: | |||
6380 | // | |||
6381 | // template <__integer_like _Tp, C<_Tp> Sentinel> | |||
6382 | // constexpr _Tp operator()(_Tp &&__t, Sentinel &&last) const { | |||
6383 | // return __t; | |||
6384 | // } | |||
6385 | // | |||
6386 | // When we compare the profiling result for `C<_Tp>` in different | |||
6387 | // modules, it will compare the type of `_Tp` in different modules. | |||
6388 | // However, the type of `_Tp` in different modules refer to different | |||
6389 | // types here naturally. So we couldn't compare the profiling result | |||
6390 | // for the template args directly. | |||
6391 | return isSameConstraintExpr(XTC->getImmediatelyDeclaredConstraint(), | |||
6392 | YTC->getImmediatelyDeclaredConstraint()); | |||
6393 | } | |||
6394 | ||||
6395 | bool ASTContext::isSameTemplateParameter(const NamedDecl *X, | |||
6396 | const NamedDecl *Y) const { | |||
6397 | if (X->getKind() != Y->getKind()) | |||
6398 | return false; | |||
6399 | ||||
6400 | if (auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) { | |||
6401 | auto *TY = cast<TemplateTypeParmDecl>(Y); | |||
6402 | if (TX->isParameterPack() != TY->isParameterPack()) | |||
6403 | return false; | |||
6404 | if (TX->hasTypeConstraint() != TY->hasTypeConstraint()) | |||
6405 | return false; | |||
6406 | return isSameTypeConstraint(TX->getTypeConstraint(), | |||
6407 | TY->getTypeConstraint()); | |||
6408 | } | |||
6409 | ||||
6410 | if (auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) { | |||
6411 | auto *TY = cast<NonTypeTemplateParmDecl>(Y); | |||
6412 | return TX->isParameterPack() == TY->isParameterPack() && | |||
6413 | TX->getASTContext().hasSameType(TX->getType(), TY->getType()) && | |||
6414 | isSameConstraintExpr(TX->getPlaceholderTypeConstraint(), | |||
6415 | TY->getPlaceholderTypeConstraint()); | |||
6416 | } | |||
6417 | ||||
6418 | auto *TX = cast<TemplateTemplateParmDecl>(X); | |||
6419 | auto *TY = cast<TemplateTemplateParmDecl>(Y); | |||
6420 | return TX->isParameterPack() == TY->isParameterPack() && | |||
6421 | isSameTemplateParameterList(TX->getTemplateParameters(), | |||
6422 | TY->getTemplateParameters()); | |||
6423 | } | |||
6424 | ||||
6425 | bool ASTContext::isSameTemplateParameterList( | |||
6426 | const TemplateParameterList *X, const TemplateParameterList *Y) const { | |||
6427 | if (X->size() != Y->size()) | |||
6428 | return false; | |||
6429 | ||||
6430 | for (unsigned I = 0, N = X->size(); I != N; ++I) | |||
6431 | if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I))) | |||
6432 | return false; | |||
6433 | ||||
6434 | return isSameConstraintExpr(X->getRequiresClause(), Y->getRequiresClause()); | |||
6435 | } | |||
6436 | ||||
6437 | bool ASTContext::isSameDefaultTemplateArgument(const NamedDecl *X, | |||
6438 | const NamedDecl *Y) const { | |||
6439 | // If the type parameter isn't the same already, we don't need to check the | |||
6440 | // default argument further. | |||
6441 | if (!isSameTemplateParameter(X, Y)) | |||
6442 | return false; | |||
6443 | ||||
6444 | if (auto *TTPX = dyn_cast<TemplateTypeParmDecl>(X)) { | |||
6445 | auto *TTPY = cast<TemplateTypeParmDecl>(Y); | |||
6446 | if (!TTPX->hasDefaultArgument() || !TTPY->hasDefaultArgument()) | |||
6447 | return false; | |||
6448 | ||||
6449 | return hasSameType(TTPX->getDefaultArgument(), TTPY->getDefaultArgument()); | |||
6450 | } | |||
6451 | ||||
6452 | if (auto *NTTPX = dyn_cast<NonTypeTemplateParmDecl>(X)) { | |||
6453 | auto *NTTPY = cast<NonTypeTemplateParmDecl>(Y); | |||
6454 | if (!NTTPX->hasDefaultArgument() || !NTTPY->hasDefaultArgument()) | |||
6455 | return false; | |||
6456 | ||||
6457 | Expr *DefaultArgumentX = NTTPX->getDefaultArgument()->IgnoreImpCasts(); | |||
6458 | Expr *DefaultArgumentY = NTTPY->getDefaultArgument()->IgnoreImpCasts(); | |||
6459 | llvm::FoldingSetNodeID XID, YID; | |||
6460 | DefaultArgumentX->Profile(XID, *this, /*Canonical=*/true); | |||
6461 | DefaultArgumentY->Profile(YID, *this, /*Canonical=*/true); | |||
6462 | return XID == YID; | |||
6463 | } | |||
6464 | ||||
6465 | auto *TTPX = cast<TemplateTemplateParmDecl>(X); | |||
6466 | auto *TTPY = cast<TemplateTemplateParmDecl>(Y); | |||
6467 | ||||
6468 | if (!TTPX->hasDefaultArgument() || !TTPY->hasDefaultArgument()) | |||
6469 | return false; | |||
6470 | ||||
6471 | const TemplateArgument &TAX = TTPX->getDefaultArgument().getArgument(); | |||
6472 | const TemplateArgument &TAY = TTPY->getDefaultArgument().getArgument(); | |||
6473 | return hasSameTemplateName(TAX.getAsTemplate(), TAY.getAsTemplate()); | |||
6474 | } | |||
6475 | ||||
6476 | static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) { | |||
6477 | if (auto *NS = X->getAsNamespace()) | |||
6478 | return NS; | |||
6479 | if (auto *NAS = X->getAsNamespaceAlias()) | |||
6480 | return NAS->getNamespace(); | |||
6481 | return nullptr; | |||
6482 | } | |||
6483 | ||||
6484 | static bool isSameQualifier(const NestedNameSpecifier *X, | |||
6485 | const NestedNameSpecifier *Y) { | |||
6486 | if (auto *NSX = getNamespace(X)) { | |||
6487 | auto *NSY = getNamespace(Y); | |||
6488 | if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl()) | |||
6489 | return false; | |||
6490 | } else if (X->getKind() != Y->getKind()) | |||
6491 | return false; | |||
6492 | ||||
6493 | // FIXME: For namespaces and types, we're permitted to check that the entity | |||
6494 | // is named via the same tokens. We should probably do so. | |||
6495 | switch (X->getKind()) { | |||
6496 | case NestedNameSpecifier::Identifier: | |||
6497 | if (X->getAsIdentifier() != Y->getAsIdentifier()) | |||
6498 | return false; | |||
6499 | break; | |||
6500 | case NestedNameSpecifier::Namespace: | |||
6501 | case NestedNameSpecifier::NamespaceAlias: | |||
6502 | // We've already checked that we named the same namespace. | |||
6503 | break; | |||
6504 | case NestedNameSpecifier::TypeSpec: | |||
6505 | case NestedNameSpecifier::TypeSpecWithTemplate: | |||
6506 | if (X->getAsType()->getCanonicalTypeInternal() != | |||
6507 | Y->getAsType()->getCanonicalTypeInternal()) | |||
6508 | return false; | |||
6509 | break; | |||
6510 | case NestedNameSpecifier::Global: | |||
6511 | case NestedNameSpecifier::Super: | |||
6512 | return true; | |||
6513 | } | |||
6514 | ||||
6515 | // Recurse into earlier portion of NNS, if any. | |||
6516 | auto *PX = X->getPrefix(); | |||
6517 | auto *PY = Y->getPrefix(); | |||
6518 | if (PX && PY) | |||
6519 | return isSameQualifier(PX, PY); | |||
6520 | return !PX && !PY; | |||
6521 | } | |||
6522 | ||||
6523 | /// Determine whether the attributes we can overload on are identical for A and | |||
6524 | /// B. Will ignore any overloadable attrs represented in the type of A and B. | |||
6525 | static bool hasSameOverloadableAttrs(const FunctionDecl *A, | |||
6526 | const FunctionDecl *B) { | |||
6527 | // Note that pass_object_size attributes are represented in the function's | |||
6528 | // ExtParameterInfo, so we don't need to check them here. | |||
6529 | ||||
6530 | llvm::FoldingSetNodeID Cand1ID, Cand2ID; | |||
6531 | auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>(); | |||
6532 | auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>(); | |||
6533 | ||||
6534 | for (auto Pair : zip_longest(AEnableIfAttrs, BEnableIfAttrs)) { | |||
6535 | std::optional<EnableIfAttr *> Cand1A = std::get<0>(Pair); | |||
6536 | std::optional<EnableIfAttr *> Cand2A = std::get<1>(Pair); | |||
6537 | ||||
6538 | // Return false if the number of enable_if attributes is different. | |||
6539 | if (!Cand1A || !Cand2A) | |||
6540 | return false; | |||
6541 | ||||
6542 | Cand1ID.clear(); | |||
6543 | Cand2ID.clear(); | |||
6544 | ||||
6545 | (*Cand1A)->getCond()->Profile(Cand1ID, A->getASTContext(), true); | |||
6546 | (*Cand2A)->getCond()->Profile(Cand2ID, B->getASTContext(), true); | |||
6547 | ||||
6548 | // Return false if any of the enable_if expressions of A and B are | |||
6549 | // different. | |||
6550 | if (Cand1ID != Cand2ID) | |||
6551 | return false; | |||
6552 | } | |||
6553 | return true; | |||
6554 | } | |||
6555 | ||||
6556 | bool ASTContext::isSameEntity(const NamedDecl *X, const NamedDecl *Y) const { | |||
6557 | // Caution: this function is called by the AST reader during deserialization, | |||
6558 | // so it cannot rely on AST invariants being met. Non-trivial accessors | |||
6559 | // should be avoided, along with any traversal of redeclaration chains. | |||
6560 | ||||
6561 | if (X == Y) | |||
6562 | return true; | |||
6563 | ||||
6564 | if (X->getDeclName() != Y->getDeclName()) | |||
6565 | return false; | |||
6566 | ||||
6567 | // Must be in the same context. | |||
6568 | // | |||
6569 | // Note that we can't use DeclContext::Equals here, because the DeclContexts | |||
6570 | // could be two different declarations of the same function. (We will fix the | |||
6571 | // semantic DC to refer to the primary definition after merging.) | |||
6572 | if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()), | |||
6573 | cast<Decl>(Y->getDeclContext()->getRedeclContext()))) | |||
6574 | return false; | |||
6575 | ||||
6576 | // Two typedefs refer to the same entity if they have the same underlying | |||
6577 | // type. | |||
6578 | if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X)) | |||
6579 | if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y)) | |||
6580 | return hasSameType(TypedefX->getUnderlyingType(), | |||
6581 | TypedefY->getUnderlyingType()); | |||
6582 | ||||
6583 | // Must have the same kind. | |||
6584 | if (X->getKind() != Y->getKind()) | |||
6585 | return false; | |||
6586 | ||||
6587 | // Objective-C classes and protocols with the same name always match. | |||
6588 | if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X)) | |||
6589 | return true; | |||
6590 | ||||
6591 | if (isa<ClassTemplateSpecializationDecl>(X)) { | |||
6592 | // No need to handle these here: we merge them when adding them to the | |||
6593 | // template. | |||
6594 | return false; | |||
6595 | } | |||
6596 | ||||
6597 | // Compatible tags match. | |||
6598 | if (const auto *TagX = dyn_cast<TagDecl>(X)) { | |||
6599 | const auto *TagY = cast<TagDecl>(Y); | |||
6600 | return (TagX->getTagKind() == TagY->getTagKind()) || | |||
6601 | ((TagX->getTagKind() == TTK_Struct || | |||
6602 | TagX->getTagKind() == TTK_Class || | |||
6603 | TagX->getTagKind() == TTK_Interface) && | |||
6604 | (TagY->getTagKind() == TTK_Struct || | |||
6605 | TagY->getTagKind() == TTK_Class || | |||
6606 | TagY->getTagKind() == TTK_Interface)); | |||
6607 | } | |||
6608 | ||||
6609 | // Functions with the same type and linkage match. | |||
6610 | // FIXME: This needs to cope with merging of prototyped/non-prototyped | |||
6611 | // functions, etc. | |||
6612 | if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) { | |||
6613 | const auto *FuncY = cast<FunctionDecl>(Y); | |||
6614 | if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) { | |||
6615 | const auto *CtorY = cast<CXXConstructorDecl>(Y); | |||
6616 | if (CtorX->getInheritedConstructor() && | |||
6617 | !isSameEntity(CtorX->getInheritedConstructor().getConstructor(), | |||
6618 | CtorY->getInheritedConstructor().getConstructor())) | |||
6619 | return false; | |||
6620 | } | |||
6621 | ||||
6622 | if (FuncX->isMultiVersion() != FuncY->isMultiVersion()) | |||
6623 | return false; | |||
6624 | ||||
6625 | // Multiversioned functions with different feature strings are represented | |||
6626 | // as separate declarations. | |||
6627 | if (FuncX->isMultiVersion()) { | |||
6628 | const auto *TAX = FuncX->getAttr<TargetAttr>(); | |||
6629 | const auto *TAY = FuncY->getAttr<TargetAttr>(); | |||
6630 | assert(TAX && TAY && "Multiversion Function without target attribute")(static_cast <bool> (TAX && TAY && "Multiversion Function without target attribute" ) ? void (0) : __assert_fail ("TAX && TAY && \"Multiversion Function without target attribute\"" , "clang/lib/AST/ASTContext.cpp", 6630, __extension__ __PRETTY_FUNCTION__ )); | |||
6631 | ||||
6632 | if (TAX->getFeaturesStr() != TAY->getFeaturesStr()) | |||
6633 | return false; | |||
6634 | } | |||
6635 | ||||
6636 | // Per C++20 [temp.over.link]/4, friends in different classes are sometimes | |||
6637 | // not the same entity if they are constrained. | |||
6638 | if ((FuncX->isMemberLikeConstrainedFriend() || | |||
6639 | FuncY->isMemberLikeConstrainedFriend()) && | |||
6640 | !FuncX->getLexicalDeclContext()->Equals( | |||
6641 | FuncY->getLexicalDeclContext())) { | |||
6642 | return false; | |||
6643 | } | |||
6644 | ||||
6645 | if (!isSameConstraintExpr(FuncX->getTrailingRequiresClause(), | |||
6646 | FuncY->getTrailingRequiresClause())) | |||
6647 | return false; | |||
6648 | ||||
6649 | auto GetTypeAsWritten = [](const FunctionDecl *FD) { | |||
6650 | // Map to the first declaration that we've already merged into this one. | |||
6651 | // The TSI of redeclarations might not match (due to calling conventions | |||
6652 | // being inherited onto the type but not the TSI), but the TSI type of | |||
6653 | // the first declaration of the function should match across modules. | |||
6654 | FD = FD->getCanonicalDecl(); | |||
6655 | return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType() | |||
6656 | : FD->getType(); | |||
6657 | }; | |||
6658 | QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY); | |||
6659 | if (!hasSameType(XT, YT)) { | |||
6660 | // We can get functions with different types on the redecl chain in C++17 | |||
6661 | // if they have differing exception specifications and at least one of | |||
6662 | // the excpetion specs is unresolved. | |||
6663 | auto *XFPT = XT->getAs<FunctionProtoType>(); | |||
6664 | auto *YFPT = YT->getAs<FunctionProtoType>(); | |||
6665 | if (getLangOpts().CPlusPlus17 && XFPT && YFPT && | |||
6666 | (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) || | |||
6667 | isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) && | |||
6668 | hasSameFunctionTypeIgnoringExceptionSpec(XT, YT)) | |||
6669 | return true; | |||
6670 | return false; | |||
6671 | } | |||
6672 | ||||
6673 | return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() && | |||
6674 | hasSameOverloadableAttrs(FuncX, FuncY); | |||
6675 | } | |||
6676 | ||||
6677 | // Variables with the same type and linkage match. | |||
6678 | if (const auto *VarX = dyn_cast<VarDecl>(X)) { | |||
6679 | const auto *VarY = cast<VarDecl>(Y); | |||
6680 | if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) { | |||
6681 | // During deserialization, we might compare variables before we load | |||
6682 | // their types. Assume the types will end up being the same. | |||
6683 | if (VarX->getType().isNull() || VarY->getType().isNull()) | |||
6684 | return true; | |||
6685 | ||||
6686 | if (hasSameType(VarX->getType(), VarY->getType())) | |||
6687 | return true; | |||
6688 | ||||
6689 | // We can get decls with different types on the redecl chain. Eg. | |||
6690 | // template <typename T> struct S { static T Var[]; }; // #1 | |||
6691 | // template <typename T> T S<T>::Var[sizeof(T)]; // #2 | |||
6692 | // Only? happens when completing an incomplete array type. In this case | |||
6693 | // when comparing #1 and #2 we should go through their element type. | |||
6694 | const ArrayType *VarXTy = getAsArrayType(VarX->getType()); | |||
6695 | const ArrayType *VarYTy = getAsArrayType(VarY->getType()); | |||
6696 | if (!VarXTy || !VarYTy) | |||
6697 | return false; | |||
6698 | if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType()) | |||
6699 | return hasSameType(VarXTy->getElementType(), VarYTy->getElementType()); | |||
6700 | } | |||
6701 | return false; | |||
6702 | } | |||
6703 | ||||
6704 | // Namespaces with the same name and inlinedness match. | |||
6705 | if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) { | |||
6706 | const auto *NamespaceY = cast<NamespaceDecl>(Y); | |||
6707 | return NamespaceX->isInline() == NamespaceY->isInline(); | |||
6708 | } | |||
6709 | ||||
6710 | // Identical template names and kinds match if their template parameter lists | |||
6711 | // and patterns match. | |||
6712 | if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) { | |||
6713 | const auto *TemplateY = cast<TemplateDecl>(Y); | |||
6714 | ||||
6715 | // ConceptDecl wouldn't be the same if their constraint expression differs. | |||
6716 | if (const auto *ConceptX = dyn_cast<ConceptDecl>(X)) { | |||
6717 | const auto *ConceptY = cast<ConceptDecl>(Y); | |||
6718 | const Expr *XCE = ConceptX->getConstraintExpr(); | |||
6719 | const Expr *YCE = ConceptY->getConstraintExpr(); | |||
6720 | assert(XCE && YCE && "ConceptDecl without constraint expression?")(static_cast <bool> (XCE && YCE && "ConceptDecl without constraint expression?" ) ? void (0) : __assert_fail ("XCE && YCE && \"ConceptDecl without constraint expression?\"" , "clang/lib/AST/ASTContext.cpp", 6720, __extension__ __PRETTY_FUNCTION__ )); | |||
6721 | llvm::FoldingSetNodeID XID, YID; | |||
6722 | XCE->Profile(XID, *this, /*Canonical=*/true); | |||
6723 | YCE->Profile(YID, *this, /*Canonical=*/true); | |||
6724 | if (XID != YID) | |||
6725 | return false; | |||
6726 | } | |||
6727 | ||||
6728 | return isSameEntity(TemplateX->getTemplatedDecl(), | |||
6729 | TemplateY->getTemplatedDecl()) && | |||
6730 | isSameTemplateParameterList(TemplateX->getTemplateParameters(), | |||
6731 | TemplateY->getTemplateParameters()); | |||
6732 | } | |||
6733 | ||||
6734 | // Fields with the same name and the same type match. | |||
6735 | if (const auto *FDX = dyn_cast<FieldDecl>(X)) { | |||
6736 | const auto *FDY = cast<FieldDecl>(Y); | |||
6737 | // FIXME: Also check the bitwidth is odr-equivalent, if any. | |||
6738 | return hasSameType(FDX->getType(), FDY->getType()); | |||
6739 | } | |||
6740 | ||||
6741 | // Indirect fields with the same target field match. | |||
6742 | if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) { | |||
6743 | const auto *IFDY = cast<IndirectFieldDecl>(Y); | |||
6744 | return IFDX->getAnonField()->getCanonicalDecl() == | |||
6745 | IFDY->getAnonField()->getCanonicalDecl(); | |||
6746 | } | |||
6747 | ||||
6748 | // Enumerators with the same name match. | |||
6749 | if (isa<EnumConstantDecl>(X)) | |||
6750 | // FIXME: Also check the value is odr-equivalent. | |||
6751 | return true; | |||
6752 | ||||
6753 | // Using shadow declarations with the same target match. | |||
6754 | if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) { | |||
6755 | const auto *USY = cast<UsingShadowDecl>(Y); | |||
6756 | return USX->getTargetDecl() == USY->getTargetDecl(); | |||
6757 | } | |||
6758 | ||||
6759 | // Using declarations with the same qualifier match. (We already know that | |||
6760 | // the name matches.) | |||
6761 | if (const auto *UX = dyn_cast<UsingDecl>(X)) { | |||
6762 | const auto *UY = cast<UsingDecl>(Y); | |||
6763 | return isSameQualifier(UX->getQualifier(), UY->getQualifier()) && | |||
6764 | UX->hasTypename() == UY->hasTypename() && | |||
6765 | UX->isAccessDeclaration() == UY->isAccessDeclaration(); | |||
6766 | } | |||
6767 | if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) { | |||
6768 | const auto *UY = cast<UnresolvedUsingValueDecl>(Y); | |||
6769 | return isSameQualifier(UX->getQualifier(), UY->getQualifier()) && | |||
6770 | UX->isAccessDeclaration() == UY->isAccessDeclaration(); | |||
6771 | } | |||
6772 | if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X)) { | |||
6773 | return isSameQualifier( | |||
6774 | UX->getQualifier(), | |||
6775 | cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier()); | |||
6776 | } | |||
6777 | ||||
6778 | // Using-pack declarations are only created by instantiation, and match if | |||
6779 | // they're instantiated from matching UnresolvedUsing...Decls. | |||
6780 | if (const auto *UX = dyn_cast<UsingPackDecl>(X)) { | |||
6781 | return declaresSameEntity( | |||
6782 | UX->getInstantiatedFromUsingDecl(), | |||
6783 | cast<UsingPackDecl>(Y)->getInstantiatedFromUsingDecl()); | |||
6784 | } | |||
6785 | ||||
6786 | // Namespace alias definitions with the same target match. | |||
6787 | if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) { | |||
6788 | const auto *NAY = cast<NamespaceAliasDecl>(Y); | |||
6789 | return NAX->getNamespace()->Equals(NAY->getNamespace()); | |||
6790 | } | |||
6791 | ||||
6792 | return false; | |||
6793 | } | |||
6794 | ||||
6795 | TemplateArgument | |||
6796 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { | |||
6797 | switch (Arg.getKind()) { | |||
6798 | case TemplateArgument::Null: | |||
6799 | return Arg; | |||
6800 | ||||
6801 | case TemplateArgument::Expression: | |||
6802 | return Arg; | |||
6803 | ||||
6804 | case TemplateArgument::Declaration: { | |||
6805 | auto *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); | |||
6806 | return TemplateArgument(D, getCanonicalType(Arg.getParamTypeForDecl()), | |||
6807 | Arg.getIsDefaulted()); | |||
6808 | } | |||
6809 | ||||
6810 | case TemplateArgument::NullPtr: | |||
6811 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), | |||
6812 | /*isNullPtr*/ true, Arg.getIsDefaulted()); | |||
6813 | ||||
6814 | case TemplateArgument::Template: | |||
6815 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()), | |||
6816 | Arg.getIsDefaulted()); | |||
6817 | ||||
6818 | case TemplateArgument::TemplateExpansion: | |||
6819 | return TemplateArgument( | |||
6820 | getCanonicalTemplateName(Arg.getAsTemplateOrTemplatePattern()), | |||
6821 | Arg.getNumTemplateExpansions(), Arg.getIsDefaulted()); | |||
6822 | ||||
6823 | case TemplateArgument::Integral: | |||
6824 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); | |||
6825 | ||||
6826 | case TemplateArgument::Type: | |||
6827 | return TemplateArgument(getCanonicalType(Arg.getAsType()), | |||
6828 | /*isNullPtr*/ false, Arg.getIsDefaulted()); | |||
6829 | ||||
6830 | case TemplateArgument::Pack: { | |||
6831 | bool AnyNonCanonArgs = false; | |||
6832 | auto CanonArgs = ::getCanonicalTemplateArguments( | |||
6833 | *this, Arg.pack_elements(), AnyNonCanonArgs); | |||
6834 | if (!AnyNonCanonArgs) | |||
6835 | return Arg; | |||
6836 | return TemplateArgument::CreatePackCopy(const_cast<ASTContext &>(*this), | |||
6837 | CanonArgs); | |||
6838 | } | |||
6839 | } | |||
6840 | ||||
6841 | // Silence GCC warning | |||
6842 | llvm_unreachable("Unhandled template argument kind")::llvm::llvm_unreachable_internal("Unhandled template argument kind" , "clang/lib/AST/ASTContext.cpp", 6842); | |||
6843 | } | |||
6844 | ||||
6845 | NestedNameSpecifier * | |||
6846 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { | |||
6847 | if (!NNS) | |||
6848 | return nullptr; | |||
6849 | ||||
6850 | switch (NNS->getKind()) { | |||
6851 | case NestedNameSpecifier::Identifier: | |||
6852 | // Canonicalize the prefix but keep the identifier the same. | |||
6853 | return NestedNameSpecifier::Create(*this, | |||
6854 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), | |||
6855 | NNS->getAsIdentifier()); | |||
6856 | ||||
6857 | case NestedNameSpecifier::Namespace: | |||
6858 | // A namespace is canonical; build a nested-name-specifier with | |||
6859 | // this namespace and no prefix. | |||
6860 | return NestedNameSpecifier::Create(*this, nullptr, | |||
6861 | NNS->getAsNamespace()->getOriginalNamespace()); | |||
6862 | ||||
6863 | case NestedNameSpecifier::NamespaceAlias: | |||
6864 | // A namespace is canonical; build a nested-name-specifier with | |||
6865 | // this namespace and no prefix. | |||
6866 | return NestedNameSpecifier::Create(*this, nullptr, | |||
6867 | NNS->getAsNamespaceAlias()->getNamespace() | |||
6868 | ->getOriginalNamespace()); | |||
6869 | ||||
6870 | // The difference between TypeSpec and TypeSpecWithTemplate is that the | |||
6871 | // latter will have the 'template' keyword when printed. | |||
6872 | case NestedNameSpecifier::TypeSpec: | |||
6873 | case NestedNameSpecifier::TypeSpecWithTemplate: { | |||
6874 | const Type *T = getCanonicalType(NNS->getAsType()); | |||
6875 | ||||
6876 | // If we have some kind of dependent-named type (e.g., "typename T::type"), | |||
6877 | // break it apart into its prefix and identifier, then reconsititute those | |||
6878 | // as the canonical nested-name-specifier. This is required to canonicalize | |||
6879 | // a dependent nested-name-specifier involving typedefs of dependent-name | |||
6880 | // types, e.g., | |||
6881 | // typedef typename T::type T1; | |||
6882 | // typedef typename T1::type T2; | |||
6883 | if (const auto *DNT = T->getAs<DependentNameType>()) | |||
6884 | return NestedNameSpecifier::Create( | |||
6885 | *this, DNT->getQualifier(), | |||
6886 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); | |||
6887 | if (const auto *DTST = T->getAs<DependentTemplateSpecializationType>()) | |||
6888 | return NestedNameSpecifier::Create(*this, DTST->getQualifier(), true, | |||
6889 | const_cast<Type *>(T)); | |||
6890 | ||||
6891 | // TODO: Set 'Template' parameter to true for other template types. | |||
6892 | return NestedNameSpecifier::Create(*this, nullptr, false, | |||
6893 | const_cast<Type *>(T)); | |||
6894 | } | |||
6895 | ||||
6896 | case NestedNameSpecifier::Global: | |||
6897 | case NestedNameSpecifier::Super: | |||
6898 | // The global specifier and __super specifer are canonical and unique. | |||
6899 | return NNS; | |||
6900 | } | |||
6901 | ||||
6902 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!")::llvm::llvm_unreachable_internal("Invalid NestedNameSpecifier::Kind!" , "clang/lib/AST/ASTContext.cpp", 6902); | |||
6903 | } | |||
6904 | ||||
6905 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { | |||
6906 | // Handle the non-qualified case efficiently. | |||
6907 | if (!T.hasLocalQualifiers()) { | |||
6908 | // Handle the common positive case fast. | |||
6909 | if (const auto *AT = dyn_cast<ArrayType>(T)) | |||
6910 | return AT; | |||
6911 | } | |||
6912 | ||||
6913 | // Handle the common negative case fast. | |||
6914 | if (!isa<ArrayType>(T.getCanonicalType())) | |||
6915 | return nullptr; | |||
6916 | ||||
6917 | // Apply any qualifiers from the array type to the element type. This | |||
6918 | // implements C99 6.7.3p8: "If the specification of an array type includes | |||
6919 | // any type qualifiers, the element type is so qualified, not the array type." | |||
6920 | ||||
6921 | // If we get here, we either have type qualifiers on the type, or we have | |||
6922 | // sugar such as a typedef in the way. If we have type qualifiers on the type | |||
6923 | // we must propagate them down into the element type. | |||
6924 | ||||
6925 | SplitQualType split = T.getSplitDesugaredType(); | |||
6926 | Qualifiers qs = split.Quals; | |||
6927 | ||||
6928 | // If we have a simple case, just return now. | |||
6929 | const auto *ATy = dyn_cast<ArrayType>(split.Ty); | |||
6930 | if (!ATy || qs.empty()) | |||
6931 | return ATy; | |||
6932 | ||||
6933 | // Otherwise, we have an array and we have qualifiers on it. Push the | |||
6934 | // qualifiers into the array element type and return a new array type. | |||
6935 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); | |||
6936 | ||||
6937 | if (const auto *CAT = dyn_cast<ConstantArrayType>(ATy)) | |||
6938 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), | |||
6939 | CAT->getSizeExpr(), | |||
6940 | CAT->getSizeModifier(), | |||
6941 | CAT->getIndexTypeCVRQualifiers())); | |||
6942 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(ATy)) | |||
6943 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, | |||
6944 | IAT->getSizeModifier(), | |||
6945 | IAT->getIndexTypeCVRQualifiers())); | |||
6946 | ||||
6947 | if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(ATy)) | |||
6948 | return cast<ArrayType>( | |||
6949 | getDependentSizedArrayType(NewEltTy, | |||
6950 | DSAT->getSizeExpr(), | |||
6951 | DSAT->getSizeModifier(), | |||
6952 | DSAT->getIndexTypeCVRQualifiers(), | |||
6953 | DSAT->getBracketsRange())); | |||
6954 | ||||
6955 | const auto *VAT = cast<VariableArrayType>(ATy); | |||
6956 | return cast<ArrayType>(getVariableArrayType(NewEltTy, | |||
6957 | VAT->getSizeExpr(), | |||
6958 | VAT->getSizeModifier(), | |||
6959 | VAT->getIndexTypeCVRQualifiers(), | |||
6960 | VAT->getBracketsRange())); | |||
6961 | } | |||
6962 | ||||
6963 | QualType ASTContext::getAdjustedParameterType(QualType T) const { | |||
6964 | if (T->isArrayType() || T->isFunctionType()) | |||
6965 | return getDecayedType(T); | |||
6966 | return T; | |||
6967 | } | |||
6968 | ||||
6969 | QualType ASTContext::getSignatureParameterType(QualType T) const { | |||
6970 | T = getVariableArrayDecayedType(T); | |||
6971 | T = getAdjustedParameterType(T); | |||
6972 | return T.getUnqualifiedType(); | |||
6973 | } | |||
6974 | ||||
6975 | QualType ASTContext::getExceptionObjectType(QualType T) const { | |||
6976 | // C++ [except.throw]p3: | |||
6977 | // A throw-expression initializes a temporary object, called the exception | |||
6978 | // object, the type of which is determined by removing any top-level | |||
6979 | // cv-qualifiers from the static type of the operand of throw and adjusting | |||
6980 | // the type from "array of T" or "function returning T" to "pointer to T" | |||
6981 | // or "pointer to function returning T", [...] | |||
6982 | T = getVariableArrayDecayedType(T); | |||
6983 | if (T->isArrayType() || T->isFunctionType()) | |||
6984 | T = getDecayedType(T); | |||
6985 | return T.getUnqualifiedType(); | |||
6986 | } | |||
6987 | ||||
6988 | /// getArrayDecayedType - Return the properly qualified result of decaying the | |||
6989 | /// specified array type to a pointer. This operation is non-trivial when | |||
6990 | /// handling typedefs etc. The canonical type of "T" must be an array type, | |||
6991 | /// this returns a pointer to a properly qualified element of the array. | |||
6992 | /// | |||
6993 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. | |||
6994 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { | |||
6995 | // Get the element type with 'getAsArrayType' so that we don't lose any | |||
6996 | // typedefs in the element type of the array. This also handles propagation | |||
6997 | // of type qualifiers from the array type into the element type if present | |||
6998 | // (C99 6.7.3p8). | |||
6999 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); | |||
7000 | assert(PrettyArrayType && "Not an array type!")(static_cast <bool> (PrettyArrayType && "Not an array type!" ) ? void (0) : __assert_fail ("PrettyArrayType && \"Not an array type!\"" , "clang/lib/AST/ASTContext.cpp", 7000, __extension__ __PRETTY_FUNCTION__ )); | |||
7001 | ||||
7002 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); | |||
7003 | ||||
7004 | // int x[restrict 4] -> int *restrict | |||
7005 | QualType Result = getQualifiedType(PtrTy, | |||
7006 | PrettyArrayType->getIndexTypeQualifiers()); | |||
7007 | ||||
7008 | // int x[_Nullable] -> int * _Nullable | |||
7009 | if (auto Nullability = Ty->getNullability()) { | |||
7010 | Result = const_cast<ASTContext *>(this)->getAttributedType( | |||
7011 | AttributedType::getNullabilityAttrKind(*Nullability), Result, Result); | |||
7012 | } | |||
7013 | return Result; | |||
7014 | } | |||
7015 | ||||
7016 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { | |||
7017 | return getBaseElementType(array->getElementType()); | |||
7018 | } | |||
7019 | ||||
7020 | QualType ASTContext::getBaseElementType(QualType type) const { | |||
7021 | Qualifiers qs; | |||
7022 | while (true) { | |||
7023 | SplitQualType split = type.getSplitDesugaredType(); | |||
7024 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); | |||
7025 | if (!array) break; | |||
7026 | ||||
7027 | type = array->getElementType(); | |||
7028 | qs.addConsistentQualifiers(split.Quals); | |||
7029 | } | |||
7030 | ||||
7031 | return getQualifiedType(type, qs); | |||
7032 | } | |||
7033 | ||||
7034 | /// getConstantArrayElementCount - Returns number of constant array elements. | |||
7035 | uint64_t | |||
7036 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { | |||
7037 | uint64_t ElementCount = 1; | |||
7038 | do { | |||
7039 | ElementCount *= CA->getSize().getZExtValue(); | |||
7040 | CA = dyn_cast_or_null<ConstantArrayType>( | |||
7041 | CA->getElementType()->getAsArrayTypeUnsafe()); | |||
7042 | } while (CA); | |||
7043 | return ElementCount; | |||
7044 | } | |||
7045 | ||||
7046 | uint64_t ASTContext::getArrayInitLoopExprElementCount( | |||
7047 | const ArrayInitLoopExpr *AILE) const { | |||
7048 | if (!AILE) | |||
7049 | return 0; | |||
7050 | ||||
7051 | uint64_t ElementCount = 1; | |||
7052 | ||||
7053 | do { | |||
7054 | ElementCount *= AILE->getArraySize().getZExtValue(); | |||
7055 | AILE = dyn_cast<ArrayInitLoopExpr>(AILE->getSubExpr()); | |||
7056 | } while (AILE); | |||
7057 | ||||
7058 | return ElementCount; | |||
7059 | } | |||
7060 | ||||
7061 | /// getFloatingRank - Return a relative rank for floating point types. | |||
7062 | /// This routine will assert if passed a built-in type that isn't a float. | |||
7063 | static FloatingRank getFloatingRank(QualType T) { | |||
7064 | if (const auto *CT = T->getAs<ComplexType>()) | |||
7065 | return getFloatingRank(CT->getElementType()); | |||
7066 | ||||
7067 | switch (T->castAs<BuiltinType>()->getKind()) { | |||
7068 | default: llvm_unreachable("getFloatingRank(): not a floating type")::llvm::llvm_unreachable_internal("getFloatingRank(): not a floating type" , "clang/lib/AST/ASTContext.cpp", 7068); | |||
7069 | case BuiltinType::Float16: return Float16Rank; | |||
7070 | case BuiltinType::Half: return HalfRank; | |||
7071 | case BuiltinType::Float: return FloatRank; | |||
7072 | case BuiltinType::Double: return DoubleRank; | |||
7073 | case BuiltinType::LongDouble: return LongDoubleRank; | |||
7074 | case BuiltinType::Float128: return Float128Rank; | |||
7075 | case BuiltinType::BFloat16: return BFloat16Rank; | |||
7076 | case BuiltinType::Ibm128: return Ibm128Rank; | |||
7077 | } | |||
7078 | } | |||
7079 | ||||
7080 | /// getFloatingTypeOrder - Compare the rank of the two specified floating | |||
7081 | /// point types, ignoring the domain of the type (i.e. 'double' == | |||
7082 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If | |||
7083 | /// LHS < RHS, return -1. | |||
7084 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { | |||
7085 | FloatingRank LHSR = getFloatingRank(LHS); | |||
7086 | FloatingRank RHSR = getFloatingRank(RHS); | |||
7087 | ||||
7088 | if (LHSR == RHSR) | |||
7089 | return 0; | |||
7090 | if (LHSR > RHSR) | |||
7091 | return 1; | |||
7092 | return -1; | |||
7093 | } | |||
7094 | ||||
7095 | int ASTContext::getFloatingTypeSemanticOrder(QualType LHS, QualType RHS) const { | |||
7096 | if (&getFloatTypeSemantics(LHS) == &getFloatTypeSemantics(RHS)) | |||
7097 | return 0; | |||
7098 | return getFloatingTypeOrder(LHS, RHS); | |||
7099 | } | |||
7100 | ||||
7101 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This | |||
7102 | /// routine will assert if passed a built-in type that isn't an integer or enum, | |||
7103 | /// or if it is not canonicalized. | |||
7104 | unsigned ASTContext::getIntegerRank(const Type *T) const { | |||
7105 | assert(T->isCanonicalUnqualified() && "T should be canonicalized")(static_cast <bool> (T->isCanonicalUnqualified() && "T should be canonicalized") ? void (0) : __assert_fail ("T->isCanonicalUnqualified() && \"T should be canonicalized\"" , "clang/lib/AST/ASTContext.cpp", 7105, __extension__ __PRETTY_FUNCTION__ )); | |||
7106 | ||||
7107 | // Results in this 'losing' to any type of the same size, but winning if | |||
7108 | // larger. | |||
7109 | if (const auto *EIT = dyn_cast<BitIntType>(T)) | |||
7110 | return 0 + (EIT->getNumBits() << 3); | |||
7111 | ||||
7112 | switch (cast<BuiltinType>(T)->getKind()) { | |||
7113 | default: llvm_unreachable("getIntegerRank(): not a built-in integer")::llvm::llvm_unreachable_internal("getIntegerRank(): not a built-in integer" , "clang/lib/AST/ASTContext.cpp", 7113); | |||
7114 | case BuiltinType::Bool: | |||
7115 | return 1 + (getIntWidth(BoolTy) << 3); | |||
7116 | case BuiltinType::Char_S: | |||
7117 | case BuiltinType::Char_U: | |||
7118 | case BuiltinType::SChar: | |||
7119 | case BuiltinType::UChar: | |||
7120 | return 2 + (getIntWidth(CharTy) << 3); | |||
7121 | case BuiltinType::Short: | |||
7122 | case BuiltinType::UShort: | |||
7123 | return 3 + (getIntWidth(ShortTy) << 3); | |||
7124 | case BuiltinType::Int: | |||
7125 | case BuiltinType::UInt: | |||
7126 | return 4 + (getIntWidth(IntTy) << 3); | |||
7127 | case BuiltinType::Long: | |||
7128 | case BuiltinType::ULong: | |||
7129 | return 5 + (getIntWidth(LongTy) << 3); | |||
7130 | case BuiltinType::LongLong: | |||
7131 | case BuiltinType::ULongLong: | |||
7132 | return 6 + (getIntWidth(LongLongTy) << 3); | |||
7133 | case BuiltinType::Int128: | |||
7134 | case BuiltinType::UInt128: | |||
7135 | return 7 + (getIntWidth(Int128Ty) << 3); | |||
7136 | ||||
7137 | // "The ranks of char8_t, char16_t, char32_t, and wchar_t equal the ranks of | |||
7138 | // their underlying types" [c++20 conv.rank] | |||
7139 | case BuiltinType::Char8: | |||
7140 | return getIntegerRank(UnsignedCharTy.getTypePtr()); | |||
7141 | case BuiltinType::Char16: | |||
7142 | return getIntegerRank( | |||
7143 | getFromTargetType(Target->getChar16Type()).getTypePtr()); | |||
7144 | case BuiltinType::Char32: | |||
7145 | return getIntegerRank( | |||
7146 | getFromTargetType(Target->getChar32Type()).getTypePtr()); | |||
7147 | case BuiltinType::WChar_S: | |||
7148 | case BuiltinType::WChar_U: | |||
7149 | return getIntegerRank( | |||
7150 | getFromTargetType(Target->getWCharType()).getTypePtr()); | |||
7151 | } | |||
7152 | } | |||
7153 | ||||
7154 | /// Whether this is a promotable bitfield reference according | |||
7155 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). | |||
7156 | /// | |||
7157 | /// \returns the type this bit-field will promote to, or NULL if no | |||
7158 | /// promotion occurs. | |||
7159 | QualType ASTContext::isPromotableBitField(Expr *E) const { | |||
7160 | if (E->isTypeDependent() || E->isValueDependent()) | |||
7161 | return {}; | |||
7162 | ||||
7163 | // C++ [conv.prom]p5: | |||
7164 | // If the bit-field has an enumerated type, it is treated as any other | |||
7165 | // value of that type for promotion purposes. | |||
7166 | if (getLangOpts().CPlusPlus && E->getType()->isEnumeralType()) | |||
7167 | return {}; | |||
7168 | ||||
7169 | // FIXME: We should not do this unless E->refersToBitField() is true. This | |||
7170 | // matters in C where getSourceBitField() will find bit-fields for various | |||
7171 | // cases where the source expression is not a bit-field designator. | |||
7172 | ||||
7173 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? | |||
7174 | if (!Field) | |||
7175 | return {}; | |||
7176 | ||||
7177 | QualType FT = Field->getType(); | |||
7178 | ||||
7179 | uint64_t BitWidth = Field->getBitWidthValue(*this); | |||
7180 | uint64_t IntSize = getTypeSize(IntTy); | |||
7181 | // C++ [conv.prom]p5: | |||
7182 | // A prvalue for an integral bit-field can be converted to a prvalue of type | |||
7183 | // int if int can represent all the values of the bit-field; otherwise, it | |||
7184 | // can be converted to unsigned int if unsigned int can represent all the | |||
7185 | // values of the bit-field. If the bit-field is larger yet, no integral | |||
7186 | // promotion applies to it. | |||
7187 | // C11 6.3.1.1/2: | |||
7188 | // [For a bit-field of type _Bool, int, signed int, or unsigned int:] | |||
7189 | // If an int can represent all values of the original type (as restricted by | |||
7190 | // the width, for a bit-field), the value is converted to an int; otherwise, | |||
7191 | // it is converted to an unsigned int. | |||
7192 | // | |||
7193 | // FIXME: C does not permit promotion of a 'long : 3' bitfield to int. | |||
7194 | // We perform that promotion here to match GCC and C++. | |||
7195 | // FIXME: C does not permit promotion of an enum bit-field whose rank is | |||
7196 | // greater than that of 'int'. We perform that promotion to match GCC. | |||
7197 | if (BitWidth < IntSize) | |||
7198 | return IntTy; | |||
7199 | ||||
7200 | if (BitWidth == IntSize) | |||
7201 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; | |||
7202 | ||||
7203 | // Bit-fields wider than int are not subject to promotions, and therefore act | |||
7204 | // like the base type. GCC has some weird bugs in this area that we | |||
7205 | // deliberately do not follow (GCC follows a pre-standard resolution to | |||
7206 | // C's DR315 which treats bit-width as being part of the type, and this leaks | |||
7207 | // into their semantics in some cases). | |||
7208 | return {}; | |||
7209 | } | |||
7210 | ||||
7211 | /// getPromotedIntegerType - Returns the type that Promotable will | |||
7212 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable | |||
7213 | /// integer type. | |||
7214 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { | |||
7215 | assert(!Promotable.isNull())(static_cast <bool> (!Promotable.isNull()) ? void (0) : __assert_fail ("!Promotable.isNull()", "clang/lib/AST/ASTContext.cpp" , 7215, __extension__ __PRETTY_FUNCTION__)); | |||
7216 | assert(isPromotableIntegerType(Promotable))(static_cast <bool> (isPromotableIntegerType(Promotable )) ? void (0) : __assert_fail ("isPromotableIntegerType(Promotable)" , "clang/lib/AST/ASTContext.cpp", 7216, __extension__ __PRETTY_FUNCTION__ )); | |||
7217 | if (const auto *ET = Promotable->getAs<EnumType>()) | |||
7218 | return ET->getDecl()->getPromotionType(); | |||
7219 | ||||
7220 | if (const auto *BT = Promotable->getAs<BuiltinType>()) { | |||
7221 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t | |||
7222 | // (3.9.1) can be converted to a prvalue of the first of the following | |||
7223 | // types that can represent all the values of its underlying type: | |||
7224 | // int, unsigned int, long int, unsigned long int, long long int, or | |||
7225 | // unsigned long long int [...] | |||
7226 | // FIXME: Is there some better way to compute this? | |||
7227 | if (BT->getKind() == BuiltinType::WChar_S || | |||
7228 | BT->getKind() == BuiltinType::WChar_U || | |||
7229 | BT->getKind() == BuiltinType::Char8 || | |||
7230 | BT->getKind() == BuiltinType::Char16 || | |||
7231 | BT->getKind() == BuiltinType::Char32) { | |||
7232 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; | |||
7233 | uint64_t FromSize = getTypeSize(BT); | |||
7234 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, | |||
7235 | LongLongTy, UnsignedLongLongTy }; | |||
7236 | for (const auto &PT : PromoteTypes) { | |||
7237 | uint64_t ToSize = getTypeSize(PT); | |||
7238 | if (FromSize < ToSize || | |||
7239 | (FromSize == ToSize && FromIsSigned == PT->isSignedIntegerType())) | |||
7240 | return PT; | |||
7241 | } | |||
7242 | llvm_unreachable("char type should fit into long long")::llvm::llvm_unreachable_internal("char type should fit into long long" , "clang/lib/AST/ASTContext.cpp", 7242); | |||
7243 | } | |||
7244 | } | |||
7245 | ||||
7246 | // At this point, we should have a signed or unsigned integer type. | |||
7247 | if (Promotable->isSignedIntegerType()) | |||
7248 | return IntTy; | |||
7249 | uint64_t PromotableSize = getIntWidth(Promotable); | |||
7250 | uint64_t IntSize = getIntWidth(IntTy); | |||
7251 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize)(static_cast <bool> (Promotable->isUnsignedIntegerType () && PromotableSize <= IntSize) ? void (0) : __assert_fail ("Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize" , "clang/lib/AST/ASTContext.cpp", 7251, __extension__ __PRETTY_FUNCTION__ )); | |||
7252 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; | |||
7253 | } | |||
7254 | ||||
7255 | /// Recurses in pointer/array types until it finds an objc retainable | |||
7256 | /// type and returns its ownership. | |||
7257 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { | |||
7258 | while (!T.isNull()) { | |||
7259 | if (T.getObjCLifetime() != Qualifiers::OCL_None) | |||
7260 | return T.getObjCLifetime(); | |||
7261 | if (T->isArrayType()) | |||
7262 | T = getBaseElementType(T); | |||
7263 | else if (const auto *PT = T->getAs<PointerType>()) | |||
7264 | T = PT->getPointeeType(); | |||
7265 | else if (const auto *RT = T->getAs<ReferenceType>()) | |||
7266 | T = RT->getPointeeType(); | |||
7267 | else | |||
7268 | break; | |||
7269 | } | |||
7270 | ||||
7271 | return Qualifiers::OCL_None; | |||
7272 | } | |||
7273 | ||||
7274 | static const Type *getIntegerTypeForEnum(const EnumType *ET) { | |||
7275 | // Incomplete enum types are not treated as integer types. | |||
7276 | // FIXME: In C++, enum types are never integer types. | |||
7277 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) | |||
7278 | return ET->getDecl()->getIntegerType().getTypePtr(); | |||
7279 | return nullptr; | |||
7280 | } | |||
7281 | ||||
7282 | /// getIntegerTypeOrder - Returns the highest ranked integer type: | |||
7283 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If | |||
7284 | /// LHS < RHS, return -1. | |||
7285 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { | |||
7286 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); | |||
7287 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); | |||
7288 | ||||
7289 | // Unwrap enums to their underlying type. | |||
7290 | if (const auto *ET = dyn_cast<EnumType>(LHSC)) | |||
7291 | LHSC = getIntegerTypeForEnum(ET); | |||
7292 | if (const auto *ET = dyn_cast<EnumType>(RHSC)) | |||
7293 | RHSC = getIntegerTypeForEnum(ET); | |||
7294 | ||||
7295 | if (LHSC == RHSC) return 0; | |||
7296 | ||||
7297 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); | |||
7298 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); | |||
7299 | ||||
7300 | unsigned LHSRank = getIntegerRank(LHSC); | |||
7301 | unsigned RHSRank = getIntegerRank(RHSC); | |||
7302 | ||||
7303 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. | |||
7304 | if (LHSRank == RHSRank) return 0; | |||
7305 | return LHSRank > RHSRank ? 1 : -1; | |||
7306 | } | |||
7307 | ||||
7308 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. | |||
7309 | if (LHSUnsigned) { | |||
7310 | // If the unsigned [LHS] type is larger, return it. | |||
7311 | if (LHSRank >= RHSRank) | |||
7312 | return 1; | |||
7313 | ||||
7314 | // If the signed type can represent all values of the unsigned type, it | |||
7315 | // wins. Because we are dealing with 2's complement and types that are | |||
7316 | // powers of two larger than each other, this is always safe. | |||
7317 | return -1; | |||
7318 | } | |||
7319 | ||||
7320 | // If the unsigned [RHS] type is larger, return it. | |||
7321 | if (RHSRank >= LHSRank) | |||
7322 | return -1; | |||
7323 | ||||
7324 | // If the signed type can represent all values of the unsigned type, it | |||
7325 | // wins. Because we are dealing with 2's complement and types that are | |||
7326 | // powers of two larger than each other, this is always safe. | |||
7327 | return 1; | |||
7328 | } | |||
7329 | ||||
7330 | TypedefDecl *ASTContext::getCFConstantStringDecl() const { | |||
7331 | if (CFConstantStringTypeDecl) | |||
7332 | return CFConstantStringTypeDecl; | |||
7333 | ||||
7334 | assert(!CFConstantStringTagDecl &&(static_cast <bool> (!CFConstantStringTagDecl && "tag and typedef should be initialized together") ? void (0) : __assert_fail ("!CFConstantStringTagDecl && \"tag and typedef should be initialized together\"" , "clang/lib/AST/ASTContext.cpp", 7335, __extension__ __PRETTY_FUNCTION__ )) | |||
7335 | "tag and typedef should be initialized together")(static_cast <bool> (!CFConstantStringTagDecl && "tag and typedef should be initialized together") ? void (0) : __assert_fail ("!CFConstantStringTagDecl && \"tag and typedef should be initialized together\"" , "clang/lib/AST/ASTContext.cpp", 7335, __extension__ __PRETTY_FUNCTION__ )); | |||
7336 | CFConstantStringTagDecl = buildImplicitRecord("__NSConstantString_tag"); | |||
7337 | CFConstantStringTagDecl->startDefinition(); | |||
7338 | ||||
7339 | struct { | |||
7340 | QualType Type; | |||
7341 | const char *Name; | |||
7342 | } Fields[5]; | |||
7343 | unsigned Count = 0; | |||
7344 | ||||
7345 | /// Objective-C ABI | |||
7346 | /// | |||
7347 | /// typedef struct __NSConstantString_tag { | |||
7348 | /// const int *isa; | |||
7349 | /// int flags; | |||
7350 | /// const char *str; | |||
7351 | /// long length; | |||
7352 | /// } __NSConstantString; | |||
7353 | /// | |||
7354 | /// Swift ABI (4.1, 4.2) | |||
7355 | /// | |||
7356 | /// typedef struct __NSConstantString_tag { | |||
7357 | /// uintptr_t _cfisa; | |||
7358 | /// uintptr_t _swift_rc; | |||
7359 | /// _Atomic(uint64_t) _cfinfoa; | |||
7360 | /// const char *_ptr; | |||
7361 | /// uint32_t _length; | |||
7362 | /// } __NSConstantString; | |||
7363 | /// | |||
7364 | /// Swift ABI (5.0) | |||
7365 | /// | |||
7366 | /// typedef struct __NSConstantString_tag { | |||
7367 | /// uintptr_t _cfisa; | |||
7368 | /// uintptr_t _swift_rc; | |||
7369 | /// _Atomic(uint64_t) _cfinfoa; | |||
7370 | /// const char *_ptr; | |||
7371 | /// uintptr_t _length; | |||
7372 | /// } __NSConstantString; | |||
7373 | ||||
7374 | const auto CFRuntime = getLangOpts().CFRuntime; | |||
7375 | if (static_cast<unsigned>(CFRuntime) < | |||
7376 | static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift)) { | |||
7377 | Fields[Count++] = { getPointerType(IntTy.withConst()), "isa" }; | |||
7378 | Fields[Count++] = { IntTy, "flags" }; | |||
7379 | Fields[Count++] = { getPointerType(CharTy.withConst()), "str" }; | |||
7380 | Fields[Count++] = { LongTy, "length" }; | |||
7381 | } else { | |||
7382 | Fields[Count++] = { getUIntPtrType(), "_cfisa" }; | |||
7383 | Fields[Count++] = { getUIntPtrType(), "_swift_rc" }; | |||
7384 | Fields[Count++] = { getFromTargetType(Target->getUInt64Type()), "_swift_rc" }; | |||
7385 | Fields[Count++] = { getPointerType(CharTy.withConst()), "_ptr" }; | |||
7386 | if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 || | |||
7387 | CFRuntime == LangOptions::CoreFoundationABI::Swift4_2) | |||
7388 | Fields[Count++] = { IntTy, "_ptr" }; | |||
7389 | else | |||
7390 | Fields[Count++] = { getUIntPtrType(), "_ptr" }; | |||
7391 | } | |||
7392 | ||||
7393 | // Create fields | |||
7394 | for (unsigned i = 0; i < Count; ++i) { | |||
7395 | FieldDecl *Field = | |||
7396 | FieldDecl::Create(*this, CFConstantStringTagDecl, SourceLocation(), | |||
7397 | SourceLocation(), &Idents.get(Fields[i].Name), | |||
7398 | Fields[i].Type, /*TInfo=*/nullptr, | |||
7399 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); | |||
7400 | Field->setAccess(AS_public); | |||
7401 | CFConstantStringTagDecl->addDecl(Field); | |||
7402 | } | |||
7403 | ||||
7404 | CFConstantStringTagDecl->completeDefinition(); | |||
7405 | // This type is designed to be compatible with NSConstantString, but cannot | |||
7406 | // use the same name, since NSConstantString is an interface. | |||
7407 | auto tagType = getTagDeclType(CFConstantStringTagDecl); | |||
7408 | CFConstantStringTypeDecl = | |||
7409 | buildImplicitTypedef(tagType, "__NSConstantString"); | |||
7410 | ||||
7411 | return CFConstantStringTypeDecl; | |||
7412 | } | |||
7413 | ||||
7414 | RecordDecl *ASTContext::getCFConstantStringTagDecl() const { | |||
7415 | if (!CFConstantStringTagDecl) | |||
7416 | getCFConstantStringDecl(); // Build the tag and the typedef. | |||
7417 | return CFConstantStringTagDecl; | |||
7418 | } | |||
7419 | ||||
7420 | // getCFConstantStringType - Return the type used for constant CFStrings. | |||
7421 | QualType ASTContext::getCFConstantStringType() const { | |||
7422 | return getTypedefType(getCFConstantStringDecl()); | |||
7423 | } | |||
7424 | ||||
7425 | QualType ASTContext::getObjCSuperType() const { | |||
7426 | if (ObjCSuperType.isNull()) { | |||
7427 | RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super"); | |||
7428 | getTranslationUnitDecl()->addDecl(ObjCSuperTypeDecl); | |||
7429 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); | |||
7430 | } | |||
7431 | return ObjCSuperType; | |||
7432 | } | |||
7433 | ||||
7434 | void ASTContext::setCFConstantStringType(QualType T) { | |||
7435 | const auto *TD = T->castAs<TypedefType>(); | |||
7436 | CFConstantStringTypeDecl = cast<TypedefDecl>(TD->getDecl()); | |||
7437 | const auto *TagType = | |||
7438 | CFConstantStringTypeDecl->getUnderlyingType()->castAs<RecordType>(); | |||
7439 | CFConstantStringTagDecl = TagType->getDecl(); | |||
7440 | } | |||
7441 | ||||
7442 | QualType ASTContext::getBlockDescriptorType() const { | |||
7443 | if (BlockDescriptorType) | |||
7444 | return getTagDeclType(BlockDescriptorType); | |||
7445 | ||||
7446 | RecordDecl *RD; | |||
7447 | // FIXME: Needs the FlagAppleBlock bit. | |||
7448 | RD = buildImplicitRecord("__block_descriptor"); | |||
7449 | RD->startDefinition(); | |||
7450 | ||||
7451 | QualType FieldTypes[] = { | |||
7452 | UnsignedLongTy, | |||
7453 | UnsignedLongTy, | |||
7454 | }; | |||
7455 | ||||
7456 | static const char *const FieldNames[] = { | |||
7457 | "reserved", | |||
7458 | "Size" | |||
7459 | }; | |||
7460 | ||||
7461 | for (size_t i = 0; i < 2; ++i) { | |||
7462 | FieldDecl *Field = FieldDecl::Create( | |||
7463 | *this, RD, SourceLocation(), SourceLocation(), | |||
7464 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, | |||
7465 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); | |||
7466 | Field->setAccess(AS_public); | |||
7467 | RD->addDecl(Field); | |||
7468 | } | |||
7469 | ||||
7470 | RD->completeDefinition(); | |||
7471 | ||||
7472 | BlockDescriptorType = RD; | |||
7473 | ||||
7474 | return getTagDeclType(BlockDescriptorType); | |||
7475 | } | |||
7476 | ||||
7477 | QualType ASTContext::getBlockDescriptorExtendedType() const { | |||
7478 | if (BlockDescriptorExtendedType) | |||
7479 | return getTagDeclType(BlockDescriptorExtendedType); | |||
7480 | ||||
7481 | RecordDecl *RD; | |||
7482 | // FIXME: Needs the FlagAppleBlock bit. | |||
7483 | RD = buildImplicitRecord("__block_descriptor_withcopydispose"); | |||
7484 | RD->startDefinition(); | |||
7485 | ||||
7486 | QualType FieldTypes[] = { | |||
7487 | UnsignedLongTy, | |||
7488 | UnsignedLongTy, | |||
7489 | getPointerType(VoidPtrTy), | |||
7490 | getPointerType(VoidPtrTy) | |||
7491 | }; | |||
7492 | ||||
7493 | static const char *const FieldNames[] = { | |||
7494 | "reserved", | |||
7495 | "Size", | |||
7496 | "CopyFuncPtr", | |||
7497 | "DestroyFuncPtr" | |||
7498 | }; | |||
7499 | ||||
7500 | for (size_t i = 0; i < 4; ++i) { | |||
7501 | FieldDecl *Field = FieldDecl::Create( | |||
7502 | *this, RD, SourceLocation(), SourceLocation(), | |||
7503 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, | |||
7504 | /*BitWidth=*/nullptr, | |||
7505 | /*Mutable=*/false, ICIS_NoInit); | |||
7506 | Field->setAccess(AS_public); | |||
7507 | RD->addDecl(Field); | |||
7508 | } | |||
7509 | ||||
7510 | RD->completeDefinition(); | |||
7511 | ||||
7512 | BlockDescriptorExtendedType = RD; | |||
7513 | return getTagDeclType(BlockDescriptorExtendedType); | |||
7514 | } | |||
7515 | ||||
7516 | OpenCLTypeKind ASTContext::getOpenCLTypeKind(const Type *T) const { | |||
7517 | const auto *BT = dyn_cast<BuiltinType>(T); | |||
7518 | ||||
7519 | if (!BT) { | |||
7520 | if (isa<PipeType>(T)) | |||
7521 | return OCLTK_Pipe; | |||
7522 | ||||
7523 | return OCLTK_Default; | |||
7524 | } | |||
7525 | ||||
7526 | switch (BT->getKind()) { | |||
7527 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | |||
7528 | case BuiltinType::Id: \ | |||
7529 | return OCLTK_Image; | |||
7530 | #include "clang/Basic/OpenCLImageTypes.def" | |||
7531 | ||||
7532 | case BuiltinType::OCLClkEvent: | |||
7533 | return OCLTK_ClkEvent; | |||
7534 | ||||
7535 | case BuiltinType::OCLEvent: | |||
7536 | return OCLTK_Event; | |||
7537 | ||||
7538 | case BuiltinType::OCLQueue: | |||
7539 | return OCLTK_Queue; | |||
7540 | ||||
7541 | case BuiltinType::OCLReserveID: | |||
7542 | return OCLTK_ReserveID; | |||
7543 | ||||
7544 | case BuiltinType::OCLSampler: | |||
7545 | return OCLTK_Sampler; | |||
7546 | ||||
7547 | default: | |||
7548 | return OCLTK_Default; | |||
7549 | } | |||
7550 | } | |||
7551 | ||||
7552 | LangAS ASTContext::getOpenCLTypeAddrSpace(const Type *T) const { | |||
7553 | return Target->getOpenCLTypeAddrSpace(getOpenCLTypeKind(T)); | |||
7554 | } | |||
7555 | ||||
7556 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" | |||
7557 | /// requires copy/dispose. Note that this must match the logic | |||
7558 | /// in buildByrefHelpers. | |||
7559 | bool ASTContext::BlockRequiresCopying(QualType Ty, | |||
7560 | const VarDecl *D) { | |||
7561 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { | |||
7562 | const Expr *copyExpr = getBlockVarCopyInit(D).getCopyExpr(); | |||
7563 | if (!copyExpr && record->hasTrivialDestructor()) return false; | |||
7564 | ||||
7565 | return true; | |||
7566 | } | |||
7567 | ||||
7568 | // The block needs copy/destroy helpers if Ty is non-trivial to destructively | |||
7569 | // move or destroy. | |||
7570 | if (Ty.isNonTrivialToPrimitiveDestructiveMove() || Ty.isDestructedType()) | |||
7571 | return true; | |||
7572 | ||||
7573 | if (!Ty->isObjCRetainableType()) return false; | |||
7574 | ||||
7575 | Qualifiers qs = Ty.getQualifiers(); | |||
7576 | ||||
7577 | // If we have lifetime, that dominates. | |||
7578 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { | |||
7579 | switch (lifetime) { | |||
7580 | case Qualifiers::OCL_None: llvm_unreachable("impossible")::llvm::llvm_unreachable_internal("impossible", "clang/lib/AST/ASTContext.cpp" , 7580); | |||
7581 | ||||
7582 | // These are just bits as far as the runtime is concerned. | |||
7583 | case Qualifiers::OCL_ExplicitNone: | |||
7584 | case Qualifiers::OCL_Autoreleasing: | |||
7585 | return false; | |||
7586 | ||||
7587 | // These cases should have been taken care of when checking the type's | |||
7588 | // non-triviality. | |||
7589 | case Qualifiers::OCL_Weak: | |||
7590 | case Qualifiers::OCL_Strong: | |||
7591 | llvm_unreachable("impossible")::llvm::llvm_unreachable_internal("impossible", "clang/lib/AST/ASTContext.cpp" , 7591); | |||
7592 | } | |||
7593 | llvm_unreachable("fell out of lifetime switch!")::llvm::llvm_unreachable_internal("fell out of lifetime switch!" , "clang/lib/AST/ASTContext.cpp", 7593); | |||
7594 | } | |||
7595 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || | |||
7596 | Ty->isObjCObjectPointerType()); | |||
7597 | } | |||
7598 | ||||
7599 | bool ASTContext::getByrefLifetime(QualType Ty, | |||
7600 | Qualifiers::ObjCLifetime &LifeTime, | |||
7601 | bool &HasByrefExtendedLayout) const { | |||
7602 | if (!getLangOpts().ObjC || | |||
7603 | getLangOpts().getGC() != LangOptions::NonGC) | |||
7604 | return false; | |||
7605 | ||||
7606 | HasByrefExtendedLayout = false; | |||
7607 | if (Ty->isRecordType()) { | |||
7608 | HasByrefExtendedLayout = true; | |||
7609 | LifeTime = Qualifiers::OCL_None; | |||
7610 | } else if ((LifeTime = Ty.getObjCLifetime())) { | |||
7611 | // Honor the ARC qualifiers. | |||
7612 | } else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) { | |||
7613 | // The MRR rule. | |||
7614 | LifeTime = Qualifiers::OCL_ExplicitNone; | |||
7615 | } else { | |||
7616 | LifeTime = Qualifiers::OCL_None; | |||
7617 | } | |||
7618 | return true; | |||
7619 | } | |||
7620 | ||||
7621 | CanQualType ASTContext::getNSUIntegerType() const { | |||
7622 | assert(Target && "Expected target to be initialized")(static_cast <bool> (Target && "Expected target to be initialized" ) ? void (0) : __assert_fail ("Target && \"Expected target to be initialized\"" , "clang/lib/AST/ASTContext.cpp", 7622, __extension__ __PRETTY_FUNCTION__ )); | |||
7623 | const llvm::Triple &T = Target->getTriple(); | |||
7624 | // Windows is LLP64 rather than LP64 | |||
7625 | if (T.isOSWindows() && T.isArch64Bit()) | |||
7626 | return UnsignedLongLongTy; | |||
7627 | return UnsignedLongTy; | |||
7628 | } | |||
7629 | ||||
7630 | CanQualType ASTContext::getNSIntegerType() const { | |||
7631 | assert(Target && "Expected target to be initialized")(static_cast <bool> (Target && "Expected target to be initialized" ) ? void (0) : __assert_fail ("Target && \"Expected target to be initialized\"" , "clang/lib/AST/ASTContext.cpp", 7631, __extension__ __PRETTY_FUNCTION__ )); | |||
7632 | const llvm::Triple &T = Target->getTriple(); | |||
7633 | // Windows is LLP64 rather than LP64 | |||
7634 | if (T.isOSWindows() && T.isArch64Bit()) | |||
7635 | return LongLongTy; | |||
7636 | return LongTy; | |||
7637 | } | |||
7638 | ||||
7639 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { | |||
7640 | if (!ObjCInstanceTypeDecl) | |||
7641 | ObjCInstanceTypeDecl = | |||
7642 | buildImplicitTypedef(getObjCIdType(), "instancetype"); | |||
7643 | return ObjCInstanceTypeDecl; | |||
7644 | } | |||
7645 | ||||
7646 | // This returns true if a type has been typedefed to BOOL: | |||
7647 | // typedef <type> BOOL; | |||
7648 | static bool isTypeTypedefedAsBOOL(QualType T) { | |||
7649 | if (const auto *TT = dyn_cast<TypedefType>(T)) | |||
7650 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) | |||
7651 | return II->isStr("BOOL"); | |||
7652 | ||||
7653 | return false; | |||
7654 | } | |||
7655 | ||||
7656 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding | |||
7657 | /// purpose. | |||
7658 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { | |||
7659 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) | |||
7660 | return CharUnits::Zero(); | |||
7661 | ||||
7662 | CharUnits sz = getTypeSizeInChars(type); | |||
7663 | ||||
7664 | // Make all integer and enum types at least as large as an int | |||
7665 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) | |||
7666 | sz = std::max(sz, getTypeSizeInChars(IntTy)); | |||
7667 | // Treat arrays as pointers, since that's how they're passed in. | |||
7668 | else if (type->isArrayType()) | |||
7669 | sz = getTypeSizeInChars(VoidPtrTy); | |||
7670 | return sz; | |||
7671 | } | |||
7672 | ||||
7673 | bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const { | |||
7674 | return getTargetInfo().getCXXABI().isMicrosoft() && | |||
7675 | VD->isStaticDataMember() && | |||
7676 | VD->getType()->isIntegralOrEnumerationType() && | |||
7677 | !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit(); | |||
7678 | } | |||
7679 | ||||
7680 | ASTContext::InlineVariableDefinitionKind | |||
7681 | ASTContext::getInlineVariableDefinitionKind(const VarDecl *VD) const { | |||
7682 | if (!VD->isInline()) | |||
7683 | return InlineVariableDefinitionKind::None; | |||
7684 | ||||
7685 | // In almost all cases, it's a weak definition. | |||
7686 | auto *First = VD->getFirstDecl(); | |||
7687 | if (First->isInlineSpecified() || !First->isStaticDataMember()) | |||
7688 | return InlineVariableDefinitionKind::Weak; | |||
7689 | ||||
7690 | // If there's a file-context declaration in this translation unit, it's a | |||
7691 | // non-discardable definition. | |||
7692 | for (auto *D : VD->redecls()) | |||
7693 | if (D->getLexicalDeclContext()->isFileContext() && | |||
7694 | !D->isInlineSpecified() && (D->isConstexpr() || First->isConstexpr())) | |||
7695 | return InlineVariableDefinitionKind::Strong; | |||
7696 | ||||
7697 | // If we've not seen one yet, we don't know. | |||
7698 | return InlineVariableDefinitionKind::WeakUnknown; | |||
7699 | } | |||
7700 | ||||
7701 | static std::string charUnitsToString(const CharUnits &CU) { | |||
7702 | return llvm::itostr(CU.getQuantity()); | |||
7703 | } | |||
7704 | ||||
7705 | /// getObjCEncodingForBlock - Return the encoded type for this block | |||
7706 | /// declaration. | |||
7707 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { | |||
7708 | std::string S; | |||
7709 | ||||
7710 | const BlockDecl *Decl = Expr->getBlockDecl(); | |||
7711 | QualType BlockTy = | |||
7712 | Expr->getType()->castAs<BlockPointerType>()->getPointeeType(); | |||
7713 | QualType BlockReturnTy = BlockTy->castAs<FunctionType>()->getReturnType(); | |||
7714 | // Encode result type. | |||
7715 | if (getLangOpts().EncodeExtendedBlockSig) | |||
7716 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, BlockReturnTy, S, | |||
7717 | true /*Extended*/); | |||
7718 | else | |||
7719 | getObjCEncodingForType(BlockReturnTy, S); | |||
7720 | // Compute size of all parameters. | |||
7721 | // Start with computing size of a pointer in number of bytes. | |||
7722 | // FIXME: There might(should) be a better way of doing this computation! | |||
7723 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); | |||
7724 | CharUnits ParmOffset = PtrSize; | |||
7725 | for (auto *PI : Decl->parameters()) { | |||
7726 | QualType PType = PI->getType(); | |||
7727 | CharUnits sz = getObjCEncodingTypeSize(PType); | |||
7728 | if (sz.isZero()) | |||
7729 | continue; | |||
7730 | assert(sz.isPositive() && "BlockExpr - Incomplete param type")(static_cast <bool> (sz.isPositive() && "BlockExpr - Incomplete param type" ) ? void (0) : __assert_fail ("sz.isPositive() && \"BlockExpr - Incomplete param type\"" , "clang/lib/AST/ASTContext.cpp", 7730, __extension__ __PRETTY_FUNCTION__ )); | |||
7731 | ParmOffset += sz; | |||
7732 | } | |||
7733 | // Size of the argument frame | |||
7734 | S += charUnitsToString(ParmOffset); | |||
7735 | // Block pointer and offset. | |||
7736 | S += "@?0"; | |||
7737 | ||||
7738 | // Argument types. | |||
7739 | ParmOffset = PtrSize; | |||
7740 | for (auto *PVDecl : Decl->parameters()) { | |||
7741 | QualType PType = PVDecl->getOriginalType(); | |||
7742 | if (const auto *AT = | |||
7743 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { | |||
7744 | // Use array's original type only if it has known number of | |||
7745 | // elements. | |||
7746 | if (!isa<ConstantArrayType>(AT)) | |||
7747 | PType = PVDecl->getType(); | |||
7748 | } else if (PType->isFunctionType()) | |||
7749 | PType = PVDecl->getType(); | |||
7750 | if (getLangOpts().EncodeExtendedBlockSig) | |||
7751 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, | |||
7752 | S, true /*Extended*/); | |||
7753 | else | |||
7754 | getObjCEncodingForType(PType, S); | |||
7755 | S += charUnitsToString(ParmOffset); | |||
7756 | ParmOffset += getObjCEncodingTypeSize(PType); | |||
7757 | } | |||
7758 | ||||
7759 | return S; | |||
7760 | } | |||
7761 | ||||
7762 | std::string | |||
7763 | ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl) const { | |||
7764 | std::string S; | |||
7765 | // Encode result type. | |||
7766 | getObjCEncodingForType(Decl->getReturnType(), S); | |||
7767 | CharUnits ParmOffset; | |||
7768 | // Compute size of all parameters. | |||
7769 | for (auto *PI : Decl->parameters()) { | |||
7770 | QualType PType = PI->getType(); | |||
7771 | CharUnits sz = getObjCEncodingTypeSize(PType); | |||
7772 | if (sz.isZero()) | |||
7773 | continue; | |||
7774 | ||||
7775 | assert(sz.isPositive() &&(static_cast <bool> (sz.isPositive() && "getObjCEncodingForFunctionDecl - Incomplete param type" ) ? void (0) : __assert_fail ("sz.isPositive() && \"getObjCEncodingForFunctionDecl - Incomplete param type\"" , "clang/lib/AST/ASTContext.cpp", 7776, __extension__ __PRETTY_FUNCTION__ )) | |||
7776 | "getObjCEncodingForFunctionDecl - Incomplete param type")(static_cast <bool> (sz.isPositive() && "getObjCEncodingForFunctionDecl - Incomplete param type" ) ? void (0) : __assert_fail ("sz.isPositive() && \"getObjCEncodingForFunctionDecl - Incomplete param type\"" , "clang/lib/AST/ASTContext.cpp", 7776, __extension__ __PRETTY_FUNCTION__ )); | |||
7777 | ParmOffset += sz; | |||
7778 | } | |||
7779 | S += charUnitsToString(ParmOffset); | |||
7780 | ParmOffset = CharUnits::Zero(); | |||
7781 | ||||
7782 | // Argument types. | |||
7783 | for (auto *PVDecl : Decl->parameters()) { | |||
7784 | QualType PType = PVDecl->getOriginalType(); | |||
7785 | if (const auto *AT = | |||
7786 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { | |||
7787 | // Use array's original type only if it has known number of | |||
7788 | // elements. | |||
7789 | if (!isa<ConstantArrayType>(AT)) | |||
7790 | PType = PVDecl->getType(); | |||
7791 | } else if (PType->isFunctionType()) | |||
7792 | PType = PVDecl->getType(); | |||
7793 | getObjCEncodingForType(PType, S); | |||
7794 | S += charUnitsToString(ParmOffset); | |||
7795 | ParmOffset += getObjCEncodingTypeSize(PType); | |||
7796 | } | |||
7797 | ||||
7798 | return S; | |||
7799 | } | |||
7800 | ||||
7801 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single | |||
7802 | /// method parameter or return type. If Extended, include class names and | |||
7803 | /// block object types. | |||
7804 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, | |||
7805 | QualType T, std::string& S, | |||
7806 | bool Extended) const { | |||
7807 | // Encode type qualifier, 'in', 'inout', etc. for the parameter. | |||
7808 | getObjCEncodingForTypeQualifier(QT, S); | |||
7809 | // Encode parameter type. | |||
7810 | ObjCEncOptions Options = ObjCEncOptions() | |||
7811 | .setExpandPointedToStructures() | |||
7812 | .setExpandStructures() | |||
7813 | .setIsOutermostType(); | |||
7814 | if (Extended) | |||
7815 | Options.setEncodeBlockParameters().setEncodeClassNames(); | |||
7816 | getObjCEncodingForTypeImpl(T, S, Options, /*Field=*/nullptr); | |||
7817 | } | |||
7818 | ||||
7819 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method | |||
7820 | /// declaration. | |||
7821 | std::string ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, | |||
7822 | bool Extended) const { | |||
7823 | // FIXME: This is not very efficient. | |||
7824 | // Encode return type. | |||
7825 | std::string S; | |||
7826 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), | |||
7827 | Decl->getReturnType(), S, Extended); | |||
7828 | // Compute size of all parameters. | |||
7829 | // Start with computing size of a pointer in number of bytes. | |||
7830 | // FIXME: There might(should) be a better way of doing this computation! | |||
7831 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); | |||
7832 | // The first two arguments (self and _cmd) are pointers; account for | |||
7833 | // their size. | |||
7834 | CharUnits ParmOffset = 2 * PtrSize; | |||
7835 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), | |||
7836 | E = Decl->sel_param_end(); PI != E; ++PI) { | |||
7837 | QualType PType = (*PI)->getType(); | |||
7838 | CharUnits sz = getObjCEncodingTypeSize(PType); | |||
7839 | if (sz.isZero()) | |||
7840 | continue; | |||
7841 | ||||
7842 | assert(sz.isPositive() &&(static_cast <bool> (sz.isPositive() && "getObjCEncodingForMethodDecl - Incomplete param type" ) ? void (0) : __assert_fail ("sz.isPositive() && \"getObjCEncodingForMethodDecl - Incomplete param type\"" , "clang/lib/AST/ASTContext.cpp", 7843, __extension__ __PRETTY_FUNCTION__ )) | |||
7843 | "getObjCEncodingForMethodDecl - Incomplete param type")(static_cast <bool> (sz.isPositive() && "getObjCEncodingForMethodDecl - Incomplete param type" ) ? void (0) : __assert_fail ("sz.isPositive() && \"getObjCEncodingForMethodDecl - Incomplete param type\"" , "clang/lib/AST/ASTContext.cpp", 7843, __extension__ __PRETTY_FUNCTION__ )); | |||
7844 | ParmOffset += sz; | |||
7845 | } | |||
7846 | S += charUnitsToString(ParmOffset); | |||
7847 | S += "@0:"; | |||
7848 | S += charUnitsToString(PtrSize); | |||
7849 | ||||
7850 | // Argument types. | |||
7851 | ParmOffset = 2 * PtrSize; | |||
7852 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), | |||
7853 | E = Decl->sel_param_end(); PI != E; ++PI) { | |||
7854 | const ParmVarDecl *PVDecl = *PI; | |||
7855 | QualType PType = PVDecl->getOriginalType(); | |||
7856 | if (const auto *AT = | |||
7857 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { | |||
7858 | // Use array's original type only if it has known number of | |||
7859 | // elements. | |||
7860 | if (!isa<ConstantArrayType>(AT)) | |||
7861 | PType = PVDecl->getType(); | |||
7862 | } else if (PType->isFunctionType()) | |||
7863 | PType = PVDecl->getType(); | |||
7864 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), | |||
7865 | PType, S, Extended); | |||
7866 | S += charUnitsToString(ParmOffset); | |||
7867 | ParmOffset += getObjCEncodingTypeSize(PType); | |||
7868 | } | |||
7869 | ||||
7870 | return S; | |||
7871 | } | |||
7872 | ||||
7873 | ObjCPropertyImplDecl * | |||
7874 | ASTContext::getObjCPropertyImplDeclForPropertyDecl( | |||
7875 | const ObjCPropertyDecl *PD, | |||
7876 | const Decl *Container) const { | |||
7877 | if (!Container) | |||
7878 | return nullptr; | |||
7879 | if (const auto *CID = dyn_cast<ObjCCategoryImplDecl>(Container)) { | |||
7880 | for (auto *PID : CID->property_impls()) | |||
7881 | if (PID->getPropertyDecl() == PD) | |||
7882 | return PID; | |||
7883 | } else { | |||
7884 | const auto *OID = cast<ObjCImplementationDecl>(Container); | |||
7885 | for (auto *PID : OID->property_impls()) | |||
7886 | if (PID->getPropertyDecl() == PD) | |||
7887 | return PID; | |||
7888 | } | |||
7889 | return nullptr; | |||
7890 | } | |||
7891 | ||||
7892 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this | |||
7893 | /// property declaration. If non-NULL, Container must be either an | |||
7894 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be | |||
7895 | /// NULL when getting encodings for protocol properties. | |||
7896 | /// Property attributes are stored as a comma-delimited C string. The simple | |||
7897 | /// attributes readonly and bycopy are encoded as single characters. The | |||
7898 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are | |||
7899 | /// encoded as single characters, followed by an identifier. Property types | |||
7900 | /// are also encoded as a parametrized attribute. The characters used to encode | |||
7901 | /// these attributes are defined by the following enumeration: | |||
7902 | /// @code | |||
7903 | /// enum PropertyAttributes { | |||
7904 | /// kPropertyReadOnly = 'R', // property is read-only. | |||
7905 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned | |||
7906 | /// kPropertyByref = '&', // property is a reference to the value last assigned | |||
7907 | /// kPropertyDynamic = 'D', // property is dynamic | |||
7908 | /// kPropertyGetter = 'G', // followed by getter selector name | |||
7909 | /// kPropertySetter = 'S', // followed by setter selector name | |||
7910 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name | |||
7911 | /// kPropertyType = 'T' // followed by old-style type encoding. | |||
7912 | /// kPropertyWeak = 'W' // 'weak' property | |||
7913 | /// kPropertyStrong = 'P' // property GC'able | |||
7914 | /// kPropertyNonAtomic = 'N' // property non-atomic | |||
7915 | /// }; | |||
7916 | /// @endcode | |||
7917 | std::string | |||
7918 | ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, | |||
7919 | const Decl *Container) const { | |||
7920 | // Collect information from the property implementation decl(s). | |||
7921 | bool Dynamic = false; | |||
7922 | ObjCPropertyImplDecl *SynthesizePID = nullptr; | |||
7923 | ||||
7924 | if (ObjCPropertyImplDecl *PropertyImpDecl = | |||
7925 | getObjCPropertyImplDeclForPropertyDecl(PD, Container)) { | |||
7926 | if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) | |||
7927 | Dynamic = true; | |||
7928 | else | |||
7929 | SynthesizePID = PropertyImpDecl; | |||
7930 | } | |||
7931 | ||||
7932 | // FIXME: This is not very efficient. | |||
7933 | std::string S = "T"; | |||
7934 | ||||
7935 | // Encode result type. | |||
7936 | // GCC has some special rules regarding encoding of properties which | |||
7937 | // closely resembles encoding of ivars. | |||
7938 | getObjCEncodingForPropertyType(PD->getType(), S); | |||
7939 | ||||
7940 | if (PD->isReadOnly()) { | |||
7941 | S += ",R"; | |||
7942 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy) | |||
7943 | S += ",C"; | |||
7944 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_retain) | |||
7945 | S += ",&"; | |||
7946 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak) | |||
7947 | S += ",W"; | |||
7948 | } else { | |||
7949 | switch (PD->getSetterKind()) { | |||
7950 | case ObjCPropertyDecl::Assign: break; | |||
7951 | case ObjCPropertyDecl::Copy: S += ",C"; break; | |||
7952 | case ObjCPropertyDecl::Retain: S += ",&"; break; | |||
7953 | case ObjCPropertyDecl::Weak: S += ",W"; break; | |||
7954 | } | |||
7955 | } | |||
7956 | ||||
7957 | // It really isn't clear at all what this means, since properties | |||
7958 | // are "dynamic by default". | |||
7959 | if (Dynamic) | |||
7960 | S += ",D"; | |||
7961 | ||||
7962 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_nonatomic) | |||
7963 | S += ",N"; | |||
7964 | ||||
7965 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_getter) { | |||
7966 | S += ",G"; | |||
7967 | S += PD->getGetterName().getAsString(); | |||
7968 | } | |||
7969 | ||||
7970 | if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_setter) { | |||
7971 | S += ",S"; | |||
7972 | S += PD->getSetterName().getAsString(); | |||
7973 | } | |||
7974 | ||||
7975 | if (SynthesizePID) { | |||
7976 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); | |||
7977 | S += ",V"; | |||
7978 | S += OID->getNameAsString(); | |||
7979 | } | |||
7980 | ||||
7981 | // FIXME: OBJCGC: weak & strong | |||
7982 | return S; | |||
7983 | } | |||
7984 | ||||
7985 | /// getLegacyIntegralTypeEncoding - | |||
7986 | /// Another legacy compatibility encoding: 32-bit longs are encoded as | |||
7987 | /// 'l' or 'L' , but not always. For typedefs, we need to use | |||
7988 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! | |||
7989 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { | |||
7990 | if (PointeeTy->getAs<TypedefType>()) { | |||
7991 | if (const auto *BT = PointeeTy->getAs<BuiltinType>()) { | |||
7992 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) | |||
7993 | PointeeTy = UnsignedIntTy; | |||
7994 | else | |||
7995 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) | |||
7996 | PointeeTy = IntTy; | |||
7997 | } | |||
7998 | } | |||
7999 | } | |||
8000 | ||||
8001 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, | |||
8002 | const FieldDecl *Field, | |||
8003 | QualType *NotEncodedT) const { | |||
8004 | // We follow the behavior of gcc, expanding structures which are | |||
8005 | // directly pointed to, and expanding embedded structures. Note that | |||
8006 | // these rules are sufficient to prevent recursive encoding of the | |||
8007 | // same type. | |||
8008 | getObjCEncodingForTypeImpl(T, S, | |||
8009 | ObjCEncOptions() | |||
8010 | .setExpandPointedToStructures() | |||
8011 | .setExpandStructures() | |||
8012 | .setIsOutermostType(), | |||
8013 | Field, NotEncodedT); | |||
8014 | } | |||
8015 | ||||
8016 | void ASTContext::getObjCEncodingForPropertyType(QualType T, | |||
8017 | std::string& S) const { | |||
8018 | // Encode result type. | |||
8019 | // GCC has some special rules regarding encoding of properties which | |||
8020 | // closely resembles encoding of ivars. | |||
8021 | getObjCEncodingForTypeImpl(T, S, | |||
8022 | ObjCEncOptions() | |||
8023 | .setExpandPointedToStructures() | |||
8024 | .setExpandStructures() | |||
8025 | .setIsOutermostType() | |||
8026 | .setEncodingProperty(), | |||
8027 | /*Field=*/nullptr); | |||
8028 | } | |||
8029 | ||||
8030 | static char getObjCEncodingForPrimitiveType(const ASTContext *C, | |||
8031 | const BuiltinType *BT) { | |||
8032 | BuiltinType::Kind kind = BT->getKind(); | |||
8033 | switch (kind) { | |||
8034 | case BuiltinType::Void: return 'v'; | |||
8035 | case BuiltinType::Bool: return 'B'; | |||
8036 | case BuiltinType::Char8: | |||
8037 | case BuiltinType::Char_U: | |||
8038 | case BuiltinType::UChar: return 'C'; | |||
8039 | case BuiltinType::Char16: | |||
8040 | case BuiltinType::UShort: return 'S'; | |||
8041 | case BuiltinType::Char32: | |||
8042 | case BuiltinType::UInt: return 'I'; | |||
8043 | case BuiltinType::ULong: | |||
8044 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; | |||
8045 | case BuiltinType::UInt128: return 'T'; | |||
8046 | case BuiltinType::ULongLong: return 'Q'; | |||
8047 | case BuiltinType::Char_S: | |||
8048 | case BuiltinType::SChar: return 'c'; | |||
8049 | case BuiltinType::Short: return 's'; | |||
8050 | case BuiltinType::WChar_S: | |||
8051 | case BuiltinType::WChar_U: | |||
8052 | case BuiltinType::Int: return 'i'; | |||
8053 | case BuiltinType::Long: | |||
8054 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; | |||
8055 | case BuiltinType::LongLong: return 'q'; | |||
8056 | case BuiltinType::Int128: return 't'; | |||
8057 | case BuiltinType::Float: return 'f'; | |||
8058 | case BuiltinType::Double: return 'd'; | |||
8059 | case BuiltinType::LongDouble: return 'D'; | |||
8060 | case BuiltinType::NullPtr: return '*'; // like char* | |||
8061 | ||||
8062 | case BuiltinType::BFloat16: | |||
8063 | case BuiltinType::Float16: | |||
8064 | case BuiltinType::Float128: | |||
8065 | case BuiltinType::Ibm128: | |||
8066 | case BuiltinType::Half: | |||
8067 | case BuiltinType::ShortAccum: | |||
8068 | case BuiltinType::Accum: | |||
8069 | case BuiltinType::LongAccum: | |||
8070 | case BuiltinType::UShortAccum: | |||
8071 | case BuiltinType::UAccum: | |||
8072 | case BuiltinType::ULongAccum: | |||
8073 | case BuiltinType::ShortFract: | |||
8074 | case BuiltinType::Fract: | |||
8075 | case BuiltinType::LongFract: | |||
8076 | case BuiltinType::UShortFract: | |||
8077 | case BuiltinType::UFract: | |||
8078 | case BuiltinType::ULongFract: | |||
8079 | case BuiltinType::SatShortAccum: | |||
8080 | case BuiltinType::SatAccum: | |||
8081 | case BuiltinType::SatLongAccum: | |||
8082 | case BuiltinType::SatUShortAccum: | |||
8083 | case BuiltinType::SatUAccum: | |||
8084 | case BuiltinType::SatULongAccum: | |||
8085 | case BuiltinType::SatShortFract: | |||
8086 | case BuiltinType::SatFract: | |||
8087 | case BuiltinType::SatLongFract: | |||
8088 | case BuiltinType::SatUShortFract: | |||
8089 | case BuiltinType::SatUFract: | |||
8090 | case BuiltinType::SatULongFract: | |||
8091 | // FIXME: potentially need @encodes for these! | |||
8092 | return ' '; | |||
8093 | ||||
8094 | #define SVE_TYPE(Name, Id, SingletonId) \ | |||
8095 | case BuiltinType::Id: | |||
8096 | #include "clang/Basic/AArch64SVEACLETypes.def" | |||
8097 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: | |||
8098 | #include "clang/Basic/RISCVVTypes.def" | |||
8099 | #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: | |||
8100 | #include "clang/Basic/WebAssemblyReferenceTypes.def" | |||
8101 | { | |||
8102 | DiagnosticsEngine &Diags = C->getDiagnostics(); | |||
8103 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, | |||
8104 | "cannot yet @encode type %0"); | |||
8105 | Diags.Report(DiagID) << BT->getName(C->getPrintingPolicy()); | |||
8106 | return ' '; | |||
8107 | } | |||
8108 | ||||
8109 | case BuiltinType::ObjCId: | |||
8110 | case BuiltinType::ObjCClass: | |||
8111 | case BuiltinType::ObjCSel: | |||
8112 | llvm_unreachable("@encoding ObjC primitive type")::llvm::llvm_unreachable_internal("@encoding ObjC primitive type" , "clang/lib/AST/ASTContext.cpp", 8112); | |||
8113 | ||||
8114 | // OpenCL and placeholder types don't need @encodings. | |||
8115 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | |||
8116 | case BuiltinType::Id: | |||
8117 | #include "clang/Basic/OpenCLImageTypes.def" | |||
8118 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | |||
8119 | case BuiltinType::Id: | |||
8120 | #include "clang/Basic/OpenCLExtensionTypes.def" | |||
8121 | case BuiltinType::OCLEvent: | |||
8122 | case BuiltinType::OCLClkEvent: | |||
8123 | case BuiltinType::OCLQueue: | |||
8124 | case BuiltinType::OCLReserveID: | |||
8125 | case BuiltinType::OCLSampler: | |||
8126 | case BuiltinType::Dependent: | |||
8127 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ | |||
8128 | case BuiltinType::Id: | |||
8129 | #include "clang/Basic/PPCTypes.def" | |||
8130 | #define BUILTIN_TYPE(KIND, ID) | |||
8131 | #define PLACEHOLDER_TYPE(KIND, ID) \ | |||
8132 | case BuiltinType::KIND: | |||
8133 | #include "clang/AST/BuiltinTypes.def" | |||
8134 | llvm_unreachable("invalid builtin type for @encode")::llvm::llvm_unreachable_internal("invalid builtin type for @encode" , "clang/lib/AST/ASTContext.cpp", 8134); | |||
8135 | } | |||
8136 | llvm_unreachable("invalid BuiltinType::Kind value")::llvm::llvm_unreachable_internal("invalid BuiltinType::Kind value" , "clang/lib/AST/ASTContext.cpp", 8136); | |||
8137 | } | |||
8138 | ||||
8139 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { | |||
8140 | EnumDecl *Enum = ET->getDecl(); | |||
8141 | ||||
8142 | // The encoding of an non-fixed enum type is always 'i', regardless of size. | |||
8143 | if (!Enum->isFixed()) | |||
8144 | return 'i'; | |||
8145 | ||||
8146 | // The encoding of a fixed enum type matches its fixed underlying type. | |||
8147 | const auto *BT = Enum->getIntegerType()->castAs<BuiltinType>(); | |||
8148 | return getObjCEncodingForPrimitiveType(C, BT); | |||
8149 | } | |||
8150 | ||||
8151 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, | |||
8152 | QualType T, const FieldDecl *FD) { | |||
8153 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl")(static_cast <bool> (FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl" ) ? void (0) : __assert_fail ("FD->isBitField() && \"not a bitfield - getObjCEncodingForTypeImpl\"" , "clang/lib/AST/ASTContext.cpp", 8153, __extension__ __PRETTY_FUNCTION__ )); | |||
8154 | S += 'b'; | |||
8155 | // The NeXT runtime encodes bit fields as b followed by the number of bits. | |||
8156 | // The GNU runtime requires more information; bitfields are encoded as b, | |||
8157 | // then the offset (in bits) of the first element, then the type of the | |||
8158 | // bitfield, then the size in bits. For example, in this structure: | |||
8159 | // | |||
8160 | // struct | |||
8161 | // { | |||
8162 | // int integer; | |||
8163 | // int flags:2; | |||
8164 | // }; | |||
8165 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT | |||
8166 | // runtime, but b32i2 for the GNU runtime. The reason for this extra | |||
8167 | // information is not especially sensible, but we're stuck with it for | |||
8168 | // compatibility with GCC, although providing it breaks anything that | |||
8169 | // actually uses runtime introspection and wants to work on both runtimes... | |||
8170 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { | |||
8171 | uint64_t Offset; | |||
8172 | ||||
8173 | if (const auto *IVD = dyn_cast<ObjCIvarDecl>(FD)) { | |||
8174 | Offset = Ctx->lookupFieldBitOffset(IVD->getContainingInterface(), nullptr, | |||
8175 | IVD); | |||
8176 | } else { | |||
8177 | const RecordDecl *RD = FD->getParent(); | |||
8178 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); | |||
8179 | Offset = RL.getFieldOffset(FD->getFieldIndex()); | |||
8180 | } | |||
8181 | ||||
8182 | S += llvm::utostr(Offset); | |||
8183 | ||||
8184 | if (const auto *ET = T->getAs<EnumType>()) | |||
8185 | S += ObjCEncodingForEnumType(Ctx, ET); | |||
8186 | else { | |||
8187 | const auto *BT = T->castAs<BuiltinType>(); | |||
8188 | S += getObjCEncodingForPrimitiveType(Ctx, BT); | |||
8189 | } | |||
8190 | } | |||
8191 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); | |||
8192 | } | |||
8193 | ||||
8194 | // Helper function for determining whether the encoded type string would include | |||
8195 | // a template specialization type. | |||
8196 | static bool hasTemplateSpecializationInEncodedString(const Type *T, | |||
8197 | bool VisitBasesAndFields) { | |||
8198 | T = T->getBaseElementTypeUnsafe(); | |||
8199 | ||||
8200 | if (auto *PT = T->getAs<PointerType>()) | |||
8201 | return hasTemplateSpecializationInEncodedString( | |||
8202 | PT->getPointeeType().getTypePtr(), false); | |||
8203 | ||||
8204 | auto *CXXRD = T->getAsCXXRecordDecl(); | |||
8205 | ||||
8206 | if (!CXXRD) | |||
8207 | return false; | |||
8208 | ||||
8209 | if (isa<ClassTemplateSpecializationDecl>(CXXRD)) | |||
8210 | return true; | |||
8211 | ||||
8212 | if (!CXXRD->hasDefinition() || !VisitBasesAndFields) | |||
8213 | return false; | |||
8214 | ||||
8215 | for (const auto &B : CXXRD->bases()) | |||
8216 | if (hasTemplateSpecializationInEncodedString(B.getType().getTypePtr(), | |||
8217 | true)) | |||
8218 | return true; | |||
8219 | ||||
8220 | for (auto *FD : CXXRD->fields()) | |||
8221 | if (hasTemplateSpecializationInEncodedString(FD->getType().getTypePtr(), | |||
8222 | true)) | |||
8223 | return true; | |||
8224 | ||||
8225 | return false; | |||
8226 | } | |||
8227 | ||||
8228 | // FIXME: Use SmallString for accumulating string. | |||
8229 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string &S, | |||
8230 | const ObjCEncOptions Options, | |||
8231 | const FieldDecl *FD, | |||
8232 | QualType *NotEncodedT) const { | |||
8233 | CanQualType CT = getCanonicalType(T); | |||
8234 | switch (CT->getTypeClass()) { | |||
8235 | case Type::Builtin: | |||
8236 | case Type::Enum: | |||
8237 | if (FD && FD->isBitField()) | |||
8238 | return EncodeBitField(this, S, T, FD); | |||
8239 | if (const auto *BT = dyn_cast<BuiltinType>(CT)) | |||
8240 | S += getObjCEncodingForPrimitiveType(this, BT); | |||
8241 | else | |||
8242 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); | |||
8243 | return; | |||
8244 | ||||
8245 | case Type::Complex: | |||
8246 | S += 'j'; | |||
8247 | getObjCEncodingForTypeImpl(T->castAs<ComplexType>()->getElementType(), S, | |||
8248 | ObjCEncOptions(), | |||
8249 | /*Field=*/nullptr); | |||
8250 | return; | |||
8251 | ||||
8252 | case Type::Atomic: | |||
8253 | S += 'A'; | |||
8254 | getObjCEncodingForTypeImpl(T->castAs<AtomicType>()->getValueType(), S, | |||
8255 | ObjCEncOptions(), | |||
8256 | /*Field=*/nullptr); | |||
8257 | return; | |||
8258 | ||||
8259 | // encoding for pointer or reference types. | |||
8260 | case Type::Pointer: | |||
8261 | case Type::LValueReference: | |||
8262 | case Type::RValueReference: { | |||
8263 | QualType PointeeTy; | |||
8264 | if (isa<PointerType>(CT)) { | |||
8265 | const auto *PT = T->castAs<PointerType>(); | |||
8266 | if (PT->isObjCSelType()) { | |||
8267 | S += ':'; | |||
8268 | return; | |||
8269 | } | |||
8270 | PointeeTy = PT->getPointeeType(); | |||
8271 | } else { | |||
8272 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); | |||
8273 | } | |||
8274 | ||||
8275 | bool isReadOnly = false; | |||
8276 | // For historical/compatibility reasons, the read-only qualifier of the | |||
8277 | // pointee gets emitted _before_ the '^'. The read-only qualifier of | |||
8278 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! | |||
8279 | // Also, do not emit the 'r' for anything but the outermost type! | |||
8280 | if (T->getAs<TypedefType>()) { | |||
8281 | if (Options.IsOutermostType() && T.isConstQualified()) { | |||
8282 | isReadOnly = true; | |||
8283 | S += 'r'; | |||
8284 | } | |||
8285 | } else if (Options.IsOutermostType()) { | |||
8286 | QualType P = PointeeTy; | |||
8287 | while (auto PT = P->getAs<PointerType>()) | |||
8288 | P = PT->getPointeeType(); | |||
8289 | if (P.isConstQualified()) { | |||
8290 | isReadOnly = true; | |||
8291 | S += 'r'; | |||
8292 | } | |||
8293 | } | |||
8294 | if (isReadOnly) { | |||
8295 | // Another legacy compatibility encoding. Some ObjC qualifier and type | |||
8296 | // combinations need to be rearranged. | |||
8297 | // Rewrite "in const" from "nr" to "rn" | |||
8298 | if (StringRef(S).endswith("nr")) | |||
8299 | S.replace(S.end()-2, S.end(), "rn"); | |||
8300 | } | |||
8301 | ||||
8302 | if (PointeeTy->isCharType()) { | |||
8303 | // char pointer types should be encoded as '*' unless it is a | |||
8304 | // type that has been typedef'd to 'BOOL'. | |||
8305 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { | |||
8306 | S += '*'; | |||
8307 | return; | |||
8308 | } | |||
8309 | } else if (const auto *RTy = PointeeTy->getAs<RecordType>()) { | |||
8310 | // GCC binary compat: Need to convert "struct objc_class *" to "#". | |||
8311 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { | |||
8312 | S += '#'; | |||
8313 | return; | |||
8314 | } | |||
8315 | // GCC binary compat: Need to convert "struct objc_object *" to "@". | |||
8316 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { | |||
8317 | S += '@'; | |||
8318 | return; | |||
8319 | } | |||
8320 | // If the encoded string for the class includes template names, just emit | |||
8321 | // "^v" for pointers to the class. | |||
8322 | if (getLangOpts().CPlusPlus && | |||
8323 | (!getLangOpts().EncodeCXXClassTemplateSpec && | |||
8324 | hasTemplateSpecializationInEncodedString( | |||
8325 | RTy, Options.ExpandPointedToStructures()))) { | |||
8326 | S += "^v"; | |||
8327 | return; | |||
8328 | } | |||
8329 | // fall through... | |||
8330 | } | |||
8331 | S += '^'; | |||
8332 | getLegacyIntegralTypeEncoding(PointeeTy); | |||
8333 | ||||
8334 | ObjCEncOptions NewOptions; | |||
8335 | if (Options.ExpandPointedToStructures()) | |||
8336 | NewOptions.setExpandStructures(); | |||
8337 | getObjCEncodingForTypeImpl(PointeeTy, S, NewOptions, | |||
8338 | /*Field=*/nullptr, NotEncodedT); | |||
8339 | return; | |||
8340 | } | |||
8341 | ||||
8342 | case Type::ConstantArray: | |||
8343 | case Type::IncompleteArray: | |||
8344 | case Type::VariableArray: { | |||
8345 | const auto *AT = cast<ArrayType>(CT); | |||
8346 | ||||
8347 | if (isa<IncompleteArrayType>(AT) && !Options.IsStructField()) { | |||
8348 | // Incomplete arrays are encoded as a pointer to the array element. | |||
8349 | S += '^'; | |||
8350 | ||||
8351 | getObjCEncodingForTypeImpl( | |||
8352 | AT->getElementType(), S, | |||
8353 | Options.keepingOnly(ObjCEncOptions().setExpandStructures()), FD); | |||
8354 | } else { | |||
8355 | S += '['; | |||
8356 | ||||
8357 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) | |||
8358 | S += llvm::utostr(CAT->getSize().getZExtValue()); | |||
8359 | else { | |||
8360 | //Variable length arrays are encoded as a regular array with 0 elements. | |||
8361 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&(static_cast <bool> ((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && "Unknown array type!" ) ? void (0) : __assert_fail ("(isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && \"Unknown array type!\"" , "clang/lib/AST/ASTContext.cpp", 8362, __extension__ __PRETTY_FUNCTION__ )) | |||
8362 | "Unknown array type!")(static_cast <bool> ((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && "Unknown array type!" ) ? void (0) : __assert_fail ("(isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && \"Unknown array type!\"" , "clang/lib/AST/ASTContext.cpp", 8362, __extension__ __PRETTY_FUNCTION__ )); | |||
8363 | S += '0'; | |||
8364 | } | |||
8365 | ||||
8366 | getObjCEncodingForTypeImpl( | |||
8367 | AT->getElementType(), S, | |||
8368 | Options.keepingOnly(ObjCEncOptions().setExpandStructures()), FD, | |||
8369 | NotEncodedT); | |||
8370 | S += ']'; | |||
8371 | } | |||
8372 | return; | |||
8373 | } | |||
8374 | ||||
8375 | case Type::FunctionNoProto: | |||
8376 | case Type::FunctionProto: | |||
8377 | S += '?'; | |||
8378 | return; | |||
8379 | ||||
8380 | case Type::Record: { | |||
8381 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); | |||
8382 | S += RDecl->isUnion() ? '(' : '{'; | |||
8383 | // Anonymous structures print as '?' | |||
8384 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { | |||
8385 | S += II->getName(); | |||
8386 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { | |||
8387 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); | |||
8388 | llvm::raw_string_ostream OS(S); | |||
8389 | printTemplateArgumentList(OS, TemplateArgs.asArray(), | |||
8390 | getPrintingPolicy()); | |||
8391 | } | |||
8392 | } else { | |||
8393 | S += '?'; | |||
8394 | } | |||
8395 | if (Options.ExpandStructures()) { | |||
8396 | S += '='; | |||
8397 | if (!RDecl->isUnion()) { | |||
8398 | getObjCEncodingForStructureImpl(RDecl, S, FD, true, NotEncodedT); | |||
8399 | } else { | |||
8400 | for (const auto *Field : RDecl->fields()) { | |||
8401 | if (FD) { | |||
8402 | S += '"'; | |||
8403 | S += Field->getNameAsString(); | |||
8404 | S += '"'; | |||
8405 | } | |||
8406 | ||||
8407 | // Special case bit-fields. | |||
8408 | if (Field->isBitField()) { | |||
8409 | getObjCEncodingForTypeImpl(Field->getType(), S, | |||
8410 | ObjCEncOptions().setExpandStructures(), | |||
8411 | Field); | |||
8412 | } else { | |||
8413 | QualType qt = Field->getType(); | |||
8414 | getLegacyIntegralTypeEncoding(qt); | |||
8415 | getObjCEncodingForTypeImpl( | |||
8416 | qt, S, | |||
8417 | ObjCEncOptions().setExpandStructures().setIsStructField(), FD, | |||
8418 | NotEncodedT); | |||
8419 | } | |||
8420 | } | |||
8421 | } | |||
8422 | } | |||
8423 | S += RDecl->isUnion() ? ')' : '}'; | |||
8424 | return; | |||
8425 | } | |||
8426 | ||||
8427 | case Type::BlockPointer: { | |||
8428 | const auto *BT = T->castAs<BlockPointerType>(); | |||
8429 | S += "@?"; // Unlike a pointer-to-function, which is "^?". | |||
8430 | if (Options.EncodeBlockParameters()) { | |||
8431 | const auto *FT = BT->getPointeeType()->castAs<FunctionType>(); | |||
8432 | ||||
8433 | S += '<'; | |||
8434 | // Block return type | |||
8435 | getObjCEncodingForTypeImpl(FT->getReturnType(), S, | |||
8436 | Options.forComponentType(), FD, NotEncodedT); | |||
8437 | // Block self | |||
8438 | S += "@?"; | |||
8439 | // Block parameters | |||
8440 | if (const auto *FPT = dyn_cast<FunctionProtoType>(FT)) { | |||
8441 | for (const auto &I : FPT->param_types()) | |||
8442 | getObjCEncodingForTypeImpl(I, S, Options.forComponentType(), FD, | |||
8443 | NotEncodedT); | |||
8444 | } | |||
8445 | S += '>'; | |||
8446 | } | |||
8447 | return; | |||
8448 | } | |||
8449 | ||||
8450 | case Type::ObjCObject: { | |||
8451 | // hack to match legacy encoding of *id and *Class | |||
8452 | QualType Ty = getObjCObjectPointerType(CT); | |||
8453 | if (Ty->isObjCIdType()) { | |||
8454 | S += "{objc_object=}"; | |||
8455 | return; | |||
8456 | } | |||
8457 | else if (Ty->isObjCClassType()) { | |||
8458 | S += "{objc_class=}"; | |||
8459 | return; | |||
8460 | } | |||
8461 | // TODO: Double check to make sure this intentionally falls through. | |||
8462 | [[fallthrough]]; | |||
8463 | } | |||
8464 | ||||
8465 | case Type::ObjCInterface: { | |||
8466 | // Ignore protocol qualifiers when mangling at this level. | |||
8467 | // @encode(class_name) | |||
8468 | ObjCInterfaceDecl *OI = T->castAs<ObjCObjectType>()->getInterface(); | |||
8469 | S += '{'; | |||
8470 | S += OI->getObjCRuntimeNameAsString(); | |||
8471 | if (Options.ExpandStructures()) { | |||
8472 | S += '='; | |||
8473 | SmallVector<const ObjCIvarDecl*, 32> Ivars; | |||
8474 | DeepCollectObjCIvars(OI, true, Ivars); | |||
8475 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { | |||
8476 | const FieldDecl *Field = Ivars[i]; | |||
8477 | if (Field->isBitField()) | |||
8478 | getObjCEncodingForTypeImpl(Field->getType(), S, | |||
8479 | ObjCEncOptions().setExpandStructures(), | |||
8480 | Field); | |||
8481 | else | |||
8482 | getObjCEncodingForTypeImpl(Field->getType(), S, | |||
8483 | ObjCEncOptions().setExpandStructures(), FD, | |||
8484 | NotEncodedT); | |||
8485 | } | |||
8486 | } | |||
8487 | S += '}'; | |||
8488 | return; | |||
8489 | } | |||
8490 | ||||
8491 | case Type::ObjCObjectPointer: { | |||
8492 | const auto *OPT = T->castAs<ObjCObjectPointerType>(); | |||
8493 | if (OPT->isObjCIdType()) { | |||
8494 | S += '@'; | |||
8495 | return; | |||
8496 | } | |||
8497 | ||||
8498 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { | |||
8499 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. | |||
8500 | // Since this is a binary compatibility issue, need to consult with | |||
8501 | // runtime folks. Fortunately, this is a *very* obscure construct. | |||
8502 | S += '#'; | |||
8503 | return; | |||
8504 | } | |||
8505 | ||||
8506 | if (OPT->isObjCQualifiedIdType()) { | |||
8507 | getObjCEncodingForTypeImpl( | |||
8508 | getObjCIdType(), S, | |||
8509 | Options.keepingOnly(ObjCEncOptions() | |||
8510 | .setExpandPointedToStructures() | |||
8511 | .setExpandStructures()), | |||
8512 | FD); | |||
8513 | if (FD || Options.EncodingProperty() || Options.EncodeClassNames()) { | |||
8514 | // Note that we do extended encoding of protocol qualifier list | |||
8515 | // Only when doing ivar or property encoding. | |||
8516 | S += '"'; | |||
8517 | for (const auto *I : OPT->quals()) { | |||
8518 | S += '<'; | |||
8519 | S += I->getObjCRuntimeNameAsString(); | |||
8520 | S += '>'; | |||
8521 | } | |||
8522 | S += '"'; | |||
8523 | } | |||
8524 | return; | |||
8525 | } | |||
8526 | ||||
8527 | S += '@'; | |||
8528 | if (OPT->getInterfaceDecl() && | |||
8529 | (FD || Options.EncodingProperty() || Options.EncodeClassNames())) { | |||
8530 | S += '"'; | |||
8531 | S += OPT->getInterfaceDecl()->getObjCRuntimeNameAsString(); | |||
8532 | for (const auto *I : OPT->quals()) { | |||
8533 | S += '<'; | |||
8534 | S += I->getObjCRuntimeNameAsString(); | |||
8535 | S += '>'; | |||
8536 | } | |||
8537 | S += '"'; | |||
8538 | } | |||
8539 | return; | |||
8540 | } | |||
8541 | ||||
8542 | // gcc just blithely ignores member pointers. | |||
8543 | // FIXME: we should do better than that. 'M' is available. | |||
8544 | case Type::MemberPointer: | |||
8545 | // This matches gcc's encoding, even though technically it is insufficient. | |||
8546 | //FIXME. We should do a better job than gcc. | |||
8547 | case Type::Vector: | |||
8548 | case Type::ExtVector: | |||
8549 | // Until we have a coherent encoding of these three types, issue warning. | |||
8550 | if (NotEncodedT) | |||
8551 | *NotEncodedT = T; | |||
8552 | return; | |||
8553 | ||||
8554 | case Type::ConstantMatrix: | |||
8555 | if (NotEncodedT) | |||
8556 | *NotEncodedT = T; | |||
8557 | return; | |||
8558 | ||||
8559 | case Type::BitInt: | |||
8560 | if (NotEncodedT) | |||
8561 | *NotEncodedT = T; | |||
8562 | return; | |||
8563 | ||||
8564 | // We could see an undeduced auto type here during error recovery. | |||
8565 | // Just ignore it. | |||
8566 | case Type::Auto: | |||
8567 | case Type::DeducedTemplateSpecialization: | |||
8568 | return; | |||
8569 | ||||
8570 | case Type::Pipe: | |||
8571 | #define ABSTRACT_TYPE(KIND, BASE) | |||
8572 | #define TYPE(KIND, BASE) | |||
8573 | #define DEPENDENT_TYPE(KIND, BASE) \ | |||
8574 | case Type::KIND: | |||
8575 | #define NON_CANONICAL_TYPE(KIND, BASE) \ | |||
8576 | case Type::KIND: | |||
8577 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ | |||
8578 | case Type::KIND: | |||
8579 | #include "clang/AST/TypeNodes.inc" | |||
8580 | llvm_unreachable("@encode for dependent type!")::llvm::llvm_unreachable_internal("@encode for dependent type!" , "clang/lib/AST/ASTContext.cpp", 8580); | |||
8581 | } | |||
8582 | llvm_unreachable("bad type kind!")::llvm::llvm_unreachable_internal("bad type kind!", "clang/lib/AST/ASTContext.cpp" , 8582); | |||
8583 | } | |||
8584 | ||||
8585 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, | |||
8586 | std::string &S, | |||
8587 | const FieldDecl *FD, | |||
8588 | bool includeVBases, | |||
8589 | QualType *NotEncodedT) const { | |||
8590 | assert(RDecl && "Expected non-null RecordDecl")(static_cast <bool> (RDecl && "Expected non-null RecordDecl" ) ? void (0) : __assert_fail ("RDecl && \"Expected non-null RecordDecl\"" , "clang/lib/AST/ASTContext.cpp", 8590, __extension__ __PRETTY_FUNCTION__ )); | |||
8591 | assert(!RDecl->isUnion() && "Should not be called for unions")(static_cast <bool> (!RDecl->isUnion() && "Should not be called for unions" ) ? void (0) : __assert_fail ("!RDecl->isUnion() && \"Should not be called for unions\"" , "clang/lib/AST/ASTContext.cpp", 8591, __extension__ __PRETTY_FUNCTION__ )); | |||
8592 | if (!RDecl->getDefinition() || RDecl->getDefinition()->isInvalidDecl()) | |||
8593 | return; | |||
8594 | ||||
8595 | const auto *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); | |||
8596 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; | |||
8597 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); | |||
8598 | ||||
8599 | if (CXXRec) { | |||
8600 | for (const auto &BI : CXXRec->bases()) { | |||
8601 | if (!BI.isVirtual()) { | |||
8602 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); | |||
8603 | if (base->isEmpty()) | |||
8604 | continue; | |||
8605 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); | |||
8606 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), | |||
8607 | std::make_pair(offs, base)); | |||
8608 | } | |||
8609 | } | |||
8610 | } | |||
8611 | ||||
8612 | unsigned i = 0; | |||
8613 | for (FieldDecl *Field : RDecl->fields()) { | |||
8614 | if (!Field->isZeroLengthBitField(*this) && Field->isZeroSize(*this)) | |||
8615 | continue; | |||
8616 | uint64_t offs = layout.getFieldOffset(i); | |||
8617 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), | |||
8618 | std::make_pair(offs, Field)); | |||
8619 | ++i; | |||
8620 | } | |||
8621 | ||||
8622 | if (CXXRec && includeVBases) { | |||
8623 | for (const auto &BI : CXXRec->vbases()) { | |||
8624 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); | |||
8625 | if (base->isEmpty()) | |||
8626 | continue; | |||
8627 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); | |||
8628 | if (offs >= uint64_t(toBits(layout.getNonVirtualSize())) && | |||
8629 | FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) | |||
8630 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), | |||
8631 | std::make_pair(offs, base)); | |||
8632 | } | |||
8633 | } | |||
8634 | ||||
8635 | CharUnits size; | |||
8636 | if (CXXRec) { | |||
8637 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); | |||
8638 | } else { | |||
8639 | size = layout.getSize(); | |||
8640 | } | |||
8641 | ||||
8642 | #ifndef NDEBUG | |||
8643 | uint64_t CurOffs = 0; | |||
8644 | #endif | |||
8645 | std::multimap<uint64_t, NamedDecl *>::iterator | |||
8646 | CurLayObj = FieldOrBaseOffsets.begin(); | |||
8647 | ||||
8648 | if (CXXRec && CXXRec->isDynamicClass() && | |||
8649 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { | |||
8650 | if (FD) { | |||
8651 | S += "\"_vptr$"; | |||
8652 | std::string recname = CXXRec->getNameAsString(); | |||
8653 | if (recname.empty()) recname = "?"; | |||
8654 | S += recname; | |||
8655 | S += '"'; | |||
8656 | } | |||
8657 | S += "^^?"; | |||
8658 | #ifndef NDEBUG | |||
8659 | CurOffs += getTypeSize(VoidPtrTy); | |||
8660 | #endif | |||
8661 | } | |||
8662 | ||||
8663 | if (!RDecl->hasFlexibleArrayMember()) { | |||
8664 | // Mark the end of the structure. | |||
8665 | uint64_t offs = toBits(size); | |||
8666 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), | |||
8667 | std::make_pair(offs, nullptr)); | |||
8668 | } | |||
8669 | ||||
8670 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { | |||
8671 | #ifndef NDEBUG | |||
8672 | assert(CurOffs <= CurLayObj->first)(static_cast <bool> (CurOffs <= CurLayObj->first) ? void (0) : __assert_fail ("CurOffs <= CurLayObj->first" , "clang/lib/AST/ASTContext.cpp", 8672, __extension__ __PRETTY_FUNCTION__ )); | |||
8673 | if (CurOffs < CurLayObj->first) { | |||
8674 | uint64_t padding = CurLayObj->first - CurOffs; | |||
8675 | // FIXME: There doesn't seem to be a way to indicate in the encoding that | |||
8676 | // packing/alignment of members is different that normal, in which case | |||
8677 | // the encoding will be out-of-sync with the real layout. | |||
8678 | // If the runtime switches to just consider the size of types without | |||
8679 | // taking into account alignment, we could make padding explicit in the | |||
8680 | // encoding (e.g. using arrays of chars). The encoding strings would be | |||
8681 | // longer then though. | |||
8682 | CurOffs += padding; | |||
8683 | } | |||
8684 | #endif | |||
8685 | ||||
8686 | NamedDecl *dcl = CurLayObj->second; | |||
8687 | if (!dcl) | |||
8688 | break; // reached end of structure. | |||
8689 | ||||
8690 | if (auto *base = dyn_cast<CXXRecordDecl>(dcl)) { | |||
8691 | // We expand the bases without their virtual bases since those are going | |||
8692 | // in the initial structure. Note that this differs from gcc which | |||
8693 | // expands virtual bases each time one is encountered in the hierarchy, | |||
8694 | // making the encoding type bigger than it really is. | |||
8695 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false, | |||
8696 | NotEncodedT); | |||
8697 | assert(!base->isEmpty())(static_cast <bool> (!base->isEmpty()) ? void (0) : __assert_fail ("!base->isEmpty()", "clang/lib/AST/ASTContext.cpp", 8697 , __extension__ __PRETTY_FUNCTION__)); | |||
8698 | #ifndef NDEBUG | |||
8699 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); | |||
8700 | #endif | |||
8701 | } else { | |||
8702 | const auto *field = cast<FieldDecl>(dcl); | |||
8703 | if (FD) { | |||
8704 | S += '"'; | |||
8705 | S += field->getNameAsString(); | |||
8706 | S += '"'; | |||
8707 | } | |||
8708 | ||||
8709 | if (field->isBitField()) { | |||
8710 | EncodeBitField(this, S, field->getType(), field); | |||
8711 | #ifndef NDEBUG | |||
8712 | CurOffs += field->getBitWidthValue(*this); | |||
8713 | #endif | |||
8714 | } else { | |||
8715 | QualType qt = field->getType(); | |||
8716 | getLegacyIntegralTypeEncoding(qt); | |||
8717 | getObjCEncodingForTypeImpl( | |||
8718 | qt, S, ObjCEncOptions().setExpandStructures().setIsStructField(), | |||
8719 | FD, NotEncodedT); | |||
8720 | #ifndef NDEBUG | |||
8721 | CurOffs += getTypeSize(field->getType()); | |||
8722 | #endif | |||
8723 | } | |||
8724 | } | |||
8725 | } | |||
8726 | } | |||
8727 | ||||
8728 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, | |||
8729 | std::string& S) const { | |||
8730 | if (QT & Decl::OBJC_TQ_In) | |||
8731 | S += 'n'; | |||
8732 | if (QT & Decl::OBJC_TQ_Inout) | |||
8733 | S += 'N'; | |||
8734 | if (QT & Decl::OBJC_TQ_Out) | |||
8735 | S += 'o'; | |||
8736 | if (QT & Decl::OBJC_TQ_Bycopy) | |||
8737 | S += 'O'; | |||
8738 | if (QT & Decl::OBJC_TQ_Byref) | |||
8739 | S += 'R'; | |||
8740 | if (QT & Decl::OBJC_TQ_Oneway) | |||
8741 | S += 'V'; | |||
8742 | } | |||
8743 | ||||
8744 | TypedefDecl *ASTContext::getObjCIdDecl() const { | |||
8745 | if (!ObjCIdDecl) { | |||
8746 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, {}, {}); | |||
8747 | T = getObjCObjectPointerType(T); | |||
8748 | ObjCIdDecl = buildImplicitTypedef(T, "id"); | |||
8749 | } | |||
8750 | return ObjCIdDecl; | |||
8751 | } | |||
8752 | ||||
8753 | TypedefDecl *ASTContext::getObjCSelDecl() const { | |||
8754 | if (!ObjCSelDecl) { | |||
8755 | QualType T = getPointerType(ObjCBuiltinSelTy); | |||
8756 | ObjCSelDecl = buildImplicitTypedef(T, "SEL"); | |||
8757 | } | |||
8758 | return ObjCSelDecl; | |||
8759 | } | |||
8760 | ||||
8761 | TypedefDecl *ASTContext::getObjCClassDecl() const { | |||
8762 | if (!ObjCClassDecl) { | |||
8763 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, {}, {}); | |||
8764 | T = getObjCObjectPointerType(T); | |||
8765 | ObjCClassDecl = buildImplicitTypedef(T, "Class"); | |||
8766 | } | |||
8767 | return ObjCClassDecl; | |||
8768 | } | |||
8769 | ||||
8770 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { | |||
8771 | if (!ObjCProtocolClassDecl) { | |||
8772 | ObjCProtocolClassDecl | |||
8773 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), | |||
8774 | SourceLocation(), | |||
8775 | &Idents.get("Protocol"), | |||
8776 | /*typeParamList=*/nullptr, | |||
8777 | /*PrevDecl=*/nullptr, | |||
8778 | SourceLocation(), true); | |||
8779 | } | |||
8780 | ||||
8781 | return ObjCProtocolClassDecl; | |||
8782 | } | |||
8783 | ||||
8784 | //===----------------------------------------------------------------------===// | |||
8785 | // __builtin_va_list Construction Functions | |||
8786 | //===----------------------------------------------------------------------===// | |||
8787 | ||||
8788 | static TypedefDecl *CreateCharPtrNamedVaListDecl(const ASTContext *Context, | |||
8789 | StringRef Name) { | |||
8790 | // typedef char* __builtin[_ms]_va_list; | |||
8791 | QualType T = Context->getPointerType(Context->CharTy); | |||
8792 | return Context->buildImplicitTypedef(T, Name); | |||
8793 | } | |||
8794 | ||||
8795 | static TypedefDecl *CreateMSVaListDecl(const ASTContext *Context) { | |||
8796 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_ms_va_list"); | |||
8797 | } | |||
8798 | ||||
8799 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { | |||
8800 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_va_list"); | |||
8801 | } | |||
8802 | ||||
8803 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { | |||
8804 | // typedef void* __builtin_va_list; | |||
8805 | QualType T = Context->getPointerType(Context->VoidTy); | |||
8806 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); | |||
8807 | } | |||
8808 | ||||
8809 | static TypedefDecl * | |||
8810 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { | |||
8811 | // struct __va_list | |||
8812 | RecordDecl *VaListTagDecl = Context->buildImplicitRecord("__va_list"); | |||
8813 | if (Context->getLangOpts().CPlusPlus) { | |||
8814 | // namespace std { struct __va_list { | |||
8815 | auto *NS = NamespaceDecl::Create( | |||
8816 | const_cast<ASTContext &>(*Context), Context->getTranslationUnitDecl(), | |||
8817 | /*Inline=*/false, SourceLocation(), SourceLocation(), | |||
8818 | &Context->Idents.get("std"), | |||
8819 | /*PrevDecl=*/nullptr, /*Nested=*/false); | |||
8820 | NS->setImplicit(); | |||
8821 | VaListTagDecl->setDeclContext(NS); | |||
8822 | } | |||
8823 | ||||
8824 | VaListTagDecl->startDefinition(); | |||
8825 | ||||
8826 | const size_t NumFields = 5; | |||
8827 | QualType FieldTypes[NumFields]; | |||
8828 | const char *FieldNames[NumFields]; | |||
8829 | ||||
8830 | // void *__stack; | |||
8831 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); | |||
8832 | FieldNames[0] = "__stack"; | |||
8833 | ||||
8834 | // void *__gr_top; | |||
8835 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); | |||
8836 | FieldNames[1] = "__gr_top"; | |||
8837 | ||||
8838 | // void *__vr_top; | |||
8839 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); | |||
8840 | FieldNames[2] = "__vr_top"; | |||
8841 | ||||
8842 | // int __gr_offs; | |||
8843 | FieldTypes[3] = Context->IntTy; | |||
8844 | FieldNames[3] = "__gr_offs"; | |||
8845 | ||||
8846 | // int __vr_offs; | |||
8847 | FieldTypes[4] = Context->IntTy; | |||
8848 | FieldNames[4] = "__vr_offs"; | |||
8849 | ||||
8850 | // Create fields | |||
8851 | for (unsigned i = 0; i < NumFields; ++i) { | |||
8852 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), | |||
8853 | VaListTagDecl, | |||
8854 | SourceLocation(), | |||
8855 | SourceLocation(), | |||
8856 | &Context->Idents.get(FieldNames[i]), | |||
8857 | FieldTypes[i], /*TInfo=*/nullptr, | |||
8858 | /*BitWidth=*/nullptr, | |||
8859 | /*Mutable=*/false, | |||
8860 | ICIS_NoInit); | |||
8861 | Field->setAccess(AS_public); | |||
8862 | VaListTagDecl->addDecl(Field); | |||
8863 | } | |||
8864 | VaListTagDecl->completeDefinition(); | |||
8865 | Context->VaListTagDecl = VaListTagDecl; | |||
8866 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); | |||
8867 | ||||
8868 | // } __builtin_va_list; | |||
8869 | return Context->buildImplicitTypedef(VaListTagType, "__builtin_va_list"); | |||
8870 | } | |||
8871 | ||||
8872 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { | |||
8873 | // typedef struct __va_list_tag { | |||
8874 | RecordDecl *VaListTagDecl; | |||
8875 | ||||
8876 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); | |||
8877 | VaListTagDecl->startDefinition(); | |||
8878 | ||||
8879 | const size_t NumFields = 5; | |||
8880 | QualType FieldTypes[NumFields]; | |||
8881 | const char *FieldNames[NumFields]; | |||
8882 | ||||
8883 | // unsigned char gpr; | |||
8884 | FieldTypes[0] = Context->UnsignedCharTy; | |||
8885 | FieldNames[0] = "gpr"; | |||
8886 | ||||
8887 | // unsigned char fpr; | |||
8888 | FieldTypes[1] = Context->UnsignedCharTy; | |||
8889 | FieldNames[1] = "fpr"; | |||
8890 | ||||
8891 | // unsigned short reserved; | |||
8892 | FieldTypes[2] = Context->UnsignedShortTy; | |||
8893 | FieldNames[2] = "reserved"; | |||
8894 | ||||
8895 | // void* overflow_arg_area; | |||
8896 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); | |||
8897 | FieldNames[3] = "overflow_arg_area"; | |||
8898 | ||||
8899 | // void* reg_save_area; | |||
8900 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); | |||
8901 | FieldNames[4] = "reg_save_area"; | |||
8902 | ||||
8903 | // Create fields | |||
8904 | for (unsigned i = 0; i < NumFields; ++i) { | |||
8905 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, | |||
8906 | SourceLocation(), | |||
8907 | SourceLocation(), | |||
8908 | &Context->Idents.get(FieldNames[i]), | |||
8909 | FieldTypes[i], /*TInfo=*/nullptr, | |||
8910 | /*BitWidth=*/nullptr, | |||
8911 | /*Mutable=*/false, | |||
8912 | ICIS_NoInit); | |||
8913 | Field->setAccess(AS_public); | |||
8914 | VaListTagDecl->addDecl(Field); | |||
8915 | } | |||
8916 | VaListTagDecl->completeDefinition(); | |||
8917 | Context->VaListTagDecl = VaListTagDecl; | |||
8918 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); | |||
8919 | ||||
8920 | // } __va_list_tag; | |||
8921 | TypedefDecl *VaListTagTypedefDecl = | |||
8922 | Context->buildImplicitTypedef(VaListTagType, "__va_list_tag"); | |||
8923 | ||||
8924 | QualType VaListTagTypedefType = | |||
8925 | Context->getTypedefType(VaListTagTypedefDecl); | |||
8926 | ||||
8927 | // typedef __va_list_tag __builtin_va_list[1]; | |||
8928 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); | |||
8929 | QualType VaListTagArrayType | |||
8930 | = Context->getConstantArrayType(VaListTagTypedefType, | |||
8931 | Size, nullptr, ArrayType::Normal, 0); | |||
8932 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); | |||
8933 | } | |||
8934 | ||||
8935 | static TypedefDecl * | |||
8936 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { | |||
8937 | // struct __va_list_tag { | |||
8938 | RecordDecl *VaListTagDecl; | |||
8939 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); | |||
8940 | VaListTagDecl->startDefinition(); | |||
8941 | ||||
8942 | const size_t NumFields = 4; | |||
8943 | QualType FieldTypes[NumFields]; | |||
8944 | const char *FieldNames[NumFields]; | |||
8945 | ||||
8946 | // unsigned gp_offset; | |||
8947 | FieldTypes[0] = Context->UnsignedIntTy; | |||
8948 | FieldNames[0] = "gp_offset"; | |||
8949 | ||||
8950 | // unsigned fp_offset; | |||
8951 | FieldTypes[1] = Context->UnsignedIntTy; | |||
8952 | FieldNames[1] = "fp_offset"; | |||
8953 | ||||
8954 | // void* overflow_arg_area; | |||
8955 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); | |||
8956 | FieldNames[2] = "overflow_arg_area"; | |||
8957 | ||||
8958 | // void* reg_save_area; | |||
8959 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); | |||
8960 | FieldNames[3] = "reg_save_area"; | |||
8961 | ||||
8962 | // Create fields | |||
8963 | for (unsigned i = 0; i < NumFields; ++i) { | |||
8964 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), | |||
8965 | VaListTagDecl, | |||
8966 | SourceLocation(), | |||
8967 | SourceLocation(), | |||
8968 | &Context->Idents.get(FieldNames[i]), | |||
8969 | FieldTypes[i], /*TInfo=*/nullptr, | |||
8970 | /*BitWidth=*/nullptr, | |||
8971 | /*Mutable=*/false, | |||
8972 | ICIS_NoInit); | |||
8973 | Field->setAccess(AS_public); | |||
8974 | VaListTagDecl->addDecl(Field); | |||
8975 | } | |||
8976 | VaListTagDecl->completeDefinition(); | |||
8977 | Context->VaListTagDecl = VaListTagDecl; | |||
8978 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); | |||
8979 | ||||
8980 | // }; | |||
8981 | ||||
8982 | // typedef struct __va_list_tag __builtin_va_list[1]; | |||
8983 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); | |||
8984 | QualType VaListTagArrayType = Context->getConstantArrayType( | |||
8985 | VaListTagType, Size, nullptr, ArrayType::Normal, 0); | |||
8986 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); | |||
8987 | } | |||
8988 | ||||
8989 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { | |||
8990 | // typedef int __builtin_va_list[4]; | |||
8991 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); | |||
8992 | QualType IntArrayType = Context->getConstantArrayType( | |||
8993 | Context->IntTy, Size, nullptr, ArrayType::Normal, 0); | |||
8994 | return Context->buildImplicitTypedef(IntArrayType, "__builtin_va_list"); | |||
8995 | } | |||
8996 | ||||
8997 | static TypedefDecl * | |||
8998 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { | |||
8999 | // struct __va_list | |||
9000 | RecordDecl *VaListDecl = Context->buildImplicitRecord("__va_list"); | |||
9001 | if (Context->getLangOpts().CPlusPlus) { | |||
9002 | // namespace std { struct __va_list { | |||
9003 | NamespaceDecl *NS; | |||
9004 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), | |||
9005 | Context->getTranslationUnitDecl(), | |||
9006 | /*Inline=*/false, SourceLocation(), | |||
9007 | SourceLocation(), &Context->Idents.get("std"), | |||
9008 | /*PrevDecl=*/nullptr, /*Nested=*/false); | |||
9009 | NS->setImplicit(); | |||
9010 | VaListDecl->setDeclContext(NS); | |||
9011 | } | |||
9012 | ||||
9013 | VaListDecl->startDefinition(); | |||
9014 | ||||
9015 | // void * __ap; | |||
9016 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), | |||
9017 | VaListDecl, | |||
9018 | SourceLocation(), | |||
9019 | SourceLocation(), | |||
9020 | &Context->Idents.get("__ap"), | |||
9021 | Context->getPointerType(Context->VoidTy), | |||
9022 | /*TInfo=*/nullptr, | |||
9023 | /*BitWidth=*/nullptr, | |||
9024 | /*Mutable=*/false, | |||
9025 | ICIS_NoInit); | |||
9026 | Field->setAccess(AS_public); | |||
9027 | VaListDecl->addDecl(Field); | |||
9028 | ||||
9029 | // }; | |||
9030 | VaListDecl->completeDefinition(); | |||
9031 | Context->VaListTagDecl = VaListDecl; | |||
9032 | ||||
9033 | // typedef struct __va_list __builtin_va_list; | |||
9034 | QualType T = Context->getRecordType(VaListDecl); | |||
9035 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); | |||
9036 | } | |||
9037 | ||||
9038 | static TypedefDecl * | |||
9039 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { | |||
9040 | // struct __va_list_tag { | |||
9041 | RecordDecl *VaListTagDecl; | |||
9042 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); | |||
9043 | VaListTagDecl->startDefinition(); | |||
9044 | ||||
9045 | const size_t NumFields = 4; | |||
9046 | QualType FieldTypes[NumFields]; | |||
9047 | const char *FieldNames[NumFields]; | |||
9048 | ||||
9049 | // long __gpr; | |||
9050 | FieldTypes[0] = Context->LongTy; | |||
9051 | FieldNames[0] = "__gpr"; | |||
9052 | ||||
9053 | // long __fpr; | |||
9054 | FieldTypes[1] = Context->LongTy; | |||
9055 | FieldNames[1] = "__fpr"; | |||
9056 | ||||
9057 | // void *__overflow_arg_area; | |||
9058 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); | |||
9059 | FieldNames[2] = "__overflow_arg_area"; | |||
9060 | ||||
9061 | // void *__reg_save_area; | |||
9062 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); | |||
9063 | FieldNames[3] = "__reg_save_area"; | |||
9064 | ||||
9065 | // Create fields | |||
9066 | for (unsigned i = 0; i < NumFields; ++i) { | |||
9067 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), | |||
9068 | VaListTagDecl, | |||
9069 | SourceLocation(), | |||
9070 | SourceLocation(), | |||
9071 | &Context->Idents.get(FieldNames[i]), | |||
9072 | FieldTypes[i], /*TInfo=*/nullptr, | |||
9073 | /*BitWidth=*/nullptr, | |||
9074 | /*Mutable=*/false, | |||
9075 | ICIS_NoInit); | |||
9076 | Field->setAccess(AS_public); | |||
9077 | VaListTagDecl->addDecl(Field); | |||
9078 | } | |||
9079 | VaListTagDecl->completeDefinition(); | |||
9080 | Context->VaListTagDecl = VaListTagDecl; | |||
9081 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); | |||
9082 | ||||
9083 | // }; | |||
9084 | ||||
9085 | // typedef __va_list_tag __builtin_va_list[1]; | |||
9086 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); | |||
9087 | QualType VaListTagArrayType = Context->getConstantArrayType( | |||
9088 | VaListTagType, Size, nullptr, ArrayType::Normal, 0); | |||
9089 | ||||
9090 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); | |||
9091 | } | |||
9092 | ||||
9093 | static TypedefDecl *CreateHexagonBuiltinVaListDecl(const ASTContext *Context) { | |||
9094 | // typedef struct __va_list_tag { | |||
9095 | RecordDecl *VaListTagDecl; | |||
9096 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); | |||
9097 | VaListTagDecl->startDefinition(); | |||
9098 | ||||
9099 | const size_t NumFields = 3; | |||
9100 | QualType FieldTypes[NumFields]; | |||
9101 | const char *FieldNames[NumFields]; | |||
9102 | ||||
9103 | // void *CurrentSavedRegisterArea; | |||
9104 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); | |||
9105 | FieldNames[0] = "__current_saved_reg_area_pointer"; | |||
9106 | ||||
9107 | // void *SavedRegAreaEnd; | |||
9108 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); | |||
9109 | FieldNames[1] = "__saved_reg_area_end_pointer"; | |||
9110 | ||||
9111 | // void *OverflowArea; | |||
9112 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); | |||
9113 | FieldNames[2] = "__overflow_area_pointer"; | |||
9114 | ||||
9115 | // Create fields | |||
9116 | for (unsigned i = 0; i < NumFields; ++i) { | |||
9117 | FieldDecl *Field = FieldDecl::Create( | |||
9118 | const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(), | |||
9119 | SourceLocation(), &Context->Idents.get(FieldNames[i]), FieldTypes[i], | |||
9120 | /*TInfo=*/nullptr, | |||
9121 | /*BitWidth=*/nullptr, | |||
9122 | /*Mutable=*/false, ICIS_NoInit); | |||
9123 | Field->setAccess(AS_public); | |||
9124 | VaListTagDecl->addDecl(Field); | |||
9125 | } | |||
9126 | VaListTagDecl->completeDefinition(); | |||
9127 | Context->VaListTagDecl = VaListTagDecl; | |||
9128 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); | |||
9129 | ||||
9130 | // } __va_list_tag; | |||
9131 | TypedefDecl *VaListTagTypedefDecl = | |||
9132 | Context->buildImplicitTypedef(VaListTagType, "__va_list_tag"); | |||
9133 | ||||
9134 | QualType VaListTagTypedefType = Context->getTypedefType(VaListTagTypedefDecl); | |||
9135 | ||||
9136 | // typedef __va_list_tag __builtin_va_list[1]; | |||
9137 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); | |||
9138 | QualType VaListTagArrayType = Context->getConstantArrayType( | |||
9139 | VaListTagTypedefType, Size, nullptr, ArrayType::Normal, 0); | |||
9140 | ||||
9141 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); | |||
9142 | } | |||
9143 | ||||
9144 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, | |||
9145 | TargetInfo::BuiltinVaListKind Kind) { | |||
9146 | switch (Kind) { | |||
9147 | case TargetInfo::CharPtrBuiltinVaList: | |||
9148 | return CreateCharPtrBuiltinVaListDecl(Context); | |||
9149 | case TargetInfo::VoidPtrBuiltinVaList: | |||
9150 | return CreateVoidPtrBuiltinVaListDecl(Context); | |||
9151 | case TargetInfo::AArch64ABIBuiltinVaList: | |||
9152 | return CreateAArch64ABIBuiltinVaListDecl(Context); | |||
9153 | case TargetInfo::PowerABIBuiltinVaList: | |||
9154 | return CreatePowerABIBuiltinVaListDecl(Context); | |||
9155 | case TargetInfo::X86_64ABIBuiltinVaList: | |||
9156 | return CreateX86_64ABIBuiltinVaListDecl(Context); | |||
9157 | case TargetInfo::PNaClABIBuiltinVaList: | |||
9158 | return CreatePNaClABIBuiltinVaListDecl(Context); | |||
9159 | case TargetInfo::AAPCSABIBuiltinVaList: | |||
9160 | return CreateAAPCSABIBuiltinVaListDecl(Context); | |||
9161 | case TargetInfo::SystemZBuiltinVaList: | |||
9162 | return CreateSystemZBuiltinVaListDecl(Context); | |||
9163 | case TargetInfo::HexagonBuiltinVaList: | |||
9164 | return CreateHexagonBuiltinVaListDecl(Context); | |||
9165 | } | |||
9166 | ||||
9167 | llvm_unreachable("Unhandled __builtin_va_list type kind")::llvm::llvm_unreachable_internal("Unhandled __builtin_va_list type kind" , "clang/lib/AST/ASTContext.cpp", 9167); | |||
9168 | } | |||
9169 | ||||
9170 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { | |||
9171 | if (!BuiltinVaListDecl) { | |||
9172 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); | |||
9173 | assert(BuiltinVaListDecl->isImplicit())(static_cast <bool> (BuiltinVaListDecl->isImplicit() ) ? void (0) : __assert_fail ("BuiltinVaListDecl->isImplicit()" , "clang/lib/AST/ASTContext.cpp", 9173, __extension__ __PRETTY_FUNCTION__ )); | |||
9174 | } | |||
9175 | ||||
9176 | return BuiltinVaListDecl; | |||
9177 | } | |||
9178 | ||||
9179 | Decl *ASTContext::getVaListTagDecl() const { | |||
9180 | // Force the creation of VaListTagDecl by building the __builtin_va_list | |||
9181 | // declaration. | |||
9182 | if (!VaListTagDecl) | |||
9183 | (void)getBuiltinVaListDecl(); | |||
9184 | ||||
9185 | return VaListTagDecl; | |||
9186 | } | |||
9187 | ||||
9188 | TypedefDecl *ASTContext::getBuiltinMSVaListDecl() const { | |||
9189 | if (!BuiltinMSVaListDecl) | |||
9190 | BuiltinMSVaListDecl = CreateMSVaListDecl(this); | |||
9191 | ||||
9192 | return BuiltinMSVaListDecl; | |||
9193 | } | |||
9194 | ||||
9195 | bool ASTContext::canBuiltinBeRedeclared(const FunctionDecl *FD) const { | |||
9196 | // Allow redecl custom type checking builtin for HLSL. | |||
9197 | if (LangOpts.HLSL && FD->getBuiltinID() != Builtin::NotBuiltin && | |||
9198 | BuiltinInfo.hasCustomTypechecking(FD->getBuiltinID())) | |||
9199 | return true; | |||
9200 | return BuiltinInfo.canBeRedeclared(FD->getBuiltinID()); | |||
9201 | } | |||
9202 | ||||
9203 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { | |||
9204 | assert(ObjCConstantStringType.isNull() &&(static_cast <bool> (ObjCConstantStringType.isNull() && "'NSConstantString' type already set!") ? void (0) : __assert_fail ("ObjCConstantStringType.isNull() && \"'NSConstantString' type already set!\"" , "clang/lib/AST/ASTContext.cpp", 9205, __extension__ __PRETTY_FUNCTION__ )) | |||
9205 | "'NSConstantString' type already set!")(static_cast <bool> (ObjCConstantStringType.isNull() && "'NSConstantString' type already set!") ? void (0) : __assert_fail ("ObjCConstantStringType.isNull() && \"'NSConstantString' type already set!\"" , "clang/lib/AST/ASTContext.cpp", 9205, __extension__ __PRETTY_FUNCTION__ )); | |||
9206 | ||||
9207 | ObjCConstantStringType = getObjCInterfaceType(Decl); | |||
9208 | } | |||
9209 | ||||
9210 | /// Retrieve the template name that corresponds to a non-empty | |||
9211 | /// lookup. | |||
9212 | TemplateName | |||
9213 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, | |||
9214 | UnresolvedSetIterator End) const { | |||
9215 | unsigned size = End - Begin; | |||
9216 | assert(size > 1 && "set is not overloaded!")(static_cast <bool> (size > 1 && "set is not overloaded!" ) ? void (0) : __assert_fail ("size > 1 && \"set is not overloaded!\"" , "clang/lib/AST/ASTContext.cpp", 9216, __extension__ __PRETTY_FUNCTION__ )); | |||
9217 | ||||
9218 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + | |||
9219 | size * sizeof(FunctionTemplateDecl*)); | |||
9220 | auto *OT = new (memory) OverloadedTemplateStorage(size); | |||
9221 | ||||
9222 | NamedDecl **Storage = OT->getStorage(); | |||
9223 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { | |||
9224 | NamedDecl *D = *I; | |||
9225 | assert(isa<FunctionTemplateDecl>(D) ||(static_cast <bool> (isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl >(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl ()))) ? void (0) : __assert_fail ("isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))" , "clang/lib/AST/ASTContext.cpp", 9228, __extension__ __PRETTY_FUNCTION__ )) | |||
9226 | isa<UnresolvedUsingValueDecl>(D) ||(static_cast <bool> (isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl >(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl ()))) ? void (0) : __assert_fail ("isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))" , "clang/lib/AST/ASTContext.cpp", 9228, __extension__ __PRETTY_FUNCTION__ )) | |||
9227 | (isa<UsingShadowDecl>(D) &&(static_cast <bool> (isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl >(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl ()))) ? void (0) : __assert_fail ("isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))" , "clang/lib/AST/ASTContext.cpp", 9228, __extension__ __PRETTY_FUNCTION__ )) | |||
9228 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl())))(static_cast <bool> (isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl >(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl ()))) ? void (0) : __assert_fail ("isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))" , "clang/lib/AST/ASTContext.cpp", 9228, __extension__ __PRETTY_FUNCTION__ )); | |||
9229 | *Storage++ = D; | |||
9230 | } | |||
9231 | ||||
9232 | return TemplateName(OT); | |||
9233 | } | |||
9234 | ||||
9235 | /// Retrieve a template name representing an unqualified-id that has been | |||
9236 | /// assumed to name a template for ADL purposes. | |||
9237 | TemplateName ASTContext::getAssumedTemplateName(DeclarationName Name) const { | |||
9238 | auto *OT = new (*this) AssumedTemplateStorage(Name); | |||
9239 | return TemplateName(OT); | |||
9240 | } | |||
9241 | ||||
9242 | /// Retrieve the template name that represents a qualified | |||
9243 | /// template name such as \c std::vector. | |||
9244 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, | |||
9245 | bool TemplateKeyword, | |||
9246 | TemplateName Template) const { | |||
9247 | assert(NNS && "Missing nested-name-specifier in qualified template name")(static_cast <bool> (NNS && "Missing nested-name-specifier in qualified template name" ) ? void (0) : __assert_fail ("NNS && \"Missing nested-name-specifier in qualified template name\"" , "clang/lib/AST/ASTContext.cpp", 9247, __extension__ __PRETTY_FUNCTION__ )); | |||
9248 | ||||
9249 | // FIXME: Canonicalization? | |||
9250 | llvm::FoldingSetNodeID ID; | |||
9251 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); | |||
9252 | ||||
9253 | void *InsertPos = nullptr; | |||
9254 | QualifiedTemplateName *QTN = | |||
9255 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); | |||
9256 | if (!QTN) { | |||
9257 | QTN = new (*this, alignof(QualifiedTemplateName)) | |||
9258 | QualifiedTemplateName(NNS, TemplateKeyword, Template); | |||
9259 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); | |||
9260 | } | |||
9261 | ||||
9262 | return TemplateName(QTN); | |||
9263 | } | |||
9264 | ||||
9265 | /// Retrieve the template name that represents a dependent | |||
9266 | /// template name such as \c MetaFun::template apply. | |||
9267 | TemplateName | |||
9268 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, | |||
9269 | const IdentifierInfo *Name) const { | |||
9270 | assert((!NNS || NNS->isDependent()) &&(static_cast <bool> ((!NNS || NNS->isDependent()) && "Nested name specifier must be dependent") ? void (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"Nested name specifier must be dependent\"" , "clang/lib/AST/ASTContext.cpp", 9271, __extension__ __PRETTY_FUNCTION__ )) | |||
9271 | "Nested name specifier must be dependent")(static_cast <bool> ((!NNS || NNS->isDependent()) && "Nested name specifier must be dependent") ? void (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"Nested name specifier must be dependent\"" , "clang/lib/AST/ASTContext.cpp", 9271, __extension__ __PRETTY_FUNCTION__ )); | |||
9272 | ||||
9273 | llvm::FoldingSetNodeID ID; | |||
9274 | DependentTemplateName::Profile(ID, NNS, Name); | |||
9275 | ||||
9276 | void *InsertPos = nullptr; | |||
9277 | DependentTemplateName *QTN = | |||
9278 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); | |||
9279 | ||||
9280 | if (QTN) | |||
9281 | return TemplateName(QTN); | |||
9282 | ||||
9283 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); | |||
9284 | if (CanonNNS == NNS) { | |||
9285 | QTN = new (*this, alignof(DependentTemplateName)) | |||
9286 | DependentTemplateName(NNS, Name); | |||
9287 | } else { | |||
9288 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); | |||
9289 | QTN = new (*this, alignof(DependentTemplateName)) | |||
9290 | DependentTemplateName(NNS, Name, Canon); | |||
9291 | DependentTemplateName *CheckQTN = | |||
9292 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); | |||
9293 | assert(!CheckQTN && "Dependent type name canonicalization broken")(static_cast <bool> (!CheckQTN && "Dependent type name canonicalization broken" ) ? void (0) : __assert_fail ("!CheckQTN && \"Dependent type name canonicalization broken\"" , "clang/lib/AST/ASTContext.cpp", 9293, __extension__ __PRETTY_FUNCTION__ )); | |||
9294 | (void)CheckQTN; | |||
9295 | } | |||
9296 | ||||
9297 | DependentTemplateNames.InsertNode(QTN, InsertPos); | |||
9298 | return TemplateName(QTN); | |||
9299 | } | |||
9300 | ||||
9301 | /// Retrieve the template name that represents a dependent | |||
9302 | /// template name such as \c MetaFun::template operator+. | |||
9303 | TemplateName | |||
9304 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, | |||
9305 | OverloadedOperatorKind Operator) const { | |||
9306 | assert((!NNS || NNS->isDependent()) &&(static_cast <bool> ((!NNS || NNS->isDependent()) && "Nested name specifier must be dependent") ? void (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"Nested name specifier must be dependent\"" , "clang/lib/AST/ASTContext.cpp", 9307, __extension__ __PRETTY_FUNCTION__ )) | |||
9307 | "Nested name specifier must be dependent")(static_cast <bool> ((!NNS || NNS->isDependent()) && "Nested name specifier must be dependent") ? void (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"Nested name specifier must be dependent\"" , "clang/lib/AST/ASTContext.cpp", 9307, __extension__ __PRETTY_FUNCTION__ )); | |||
9308 | ||||
9309 | llvm::FoldingSetNodeID ID; | |||
9310 | DependentTemplateName::Profile(ID, NNS, Operator); | |||
9311 | ||||
9312 | void *InsertPos = nullptr; | |||
9313 | DependentTemplateName *QTN | |||
9314 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); | |||
9315 | ||||
9316 | if (QTN) | |||
9317 | return TemplateName(QTN); | |||
9318 | ||||
9319 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); | |||
9320 | if (CanonNNS == NNS) { | |||
9321 | QTN = new (*this, alignof(DependentTemplateName)) | |||
9322 | DependentTemplateName(NNS, Operator); | |||
9323 | } else { | |||
9324 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); | |||
9325 | QTN = new (*this, alignof(DependentTemplateName)) | |||
9326 | DependentTemplateName(NNS, Operator, Canon); | |||
9327 | ||||
9328 | DependentTemplateName *CheckQTN | |||
9329 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); | |||
9330 | assert(!CheckQTN && "Dependent template name canonicalization broken")(static_cast <bool> (!CheckQTN && "Dependent template name canonicalization broken" ) ? void (0) : __assert_fail ("!CheckQTN && \"Dependent template name canonicalization broken\"" , "clang/lib/AST/ASTContext.cpp", 9330, __extension__ __PRETTY_FUNCTION__ )); | |||
9331 | (void)CheckQTN; | |||
9332 | } | |||
9333 | ||||
9334 | DependentTemplateNames.InsertNode(QTN, InsertPos); | |||
9335 | return TemplateName(QTN); | |||
9336 | } | |||
9337 | ||||
9338 | TemplateName ASTContext::getSubstTemplateTemplateParm( | |||
9339 | TemplateName Replacement, Decl *AssociatedDecl, unsigned Index, | |||
9340 | std::optional<unsigned> PackIndex) const { | |||
9341 | llvm::FoldingSetNodeID ID; | |||
9342 | SubstTemplateTemplateParmStorage::Profile(ID, Replacement, AssociatedDecl, | |||
9343 | Index, PackIndex); | |||
9344 | ||||
9345 | void *insertPos = nullptr; | |||
9346 | SubstTemplateTemplateParmStorage *subst | |||
9347 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); | |||
9348 | ||||
9349 | if (!subst) { | |||
9350 | subst = new (*this) SubstTemplateTemplateParmStorage( | |||
9351 | Replacement, AssociatedDecl, Index, PackIndex); | |||
9352 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); | |||
9353 | } | |||
9354 | ||||
9355 | return TemplateName(subst); | |||
9356 | } | |||
9357 | ||||
9358 | TemplateName | |||
9359 | ASTContext::getSubstTemplateTemplateParmPack(const TemplateArgument &ArgPack, | |||
9360 | Decl *AssociatedDecl, | |||
9361 | unsigned Index, bool Final) const { | |||
9362 | auto &Self = const_cast<ASTContext &>(*this); | |||
9363 | llvm::FoldingSetNodeID ID; | |||
9364 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, ArgPack, | |||
9365 | AssociatedDecl, Index, Final); | |||
9366 | ||||
9367 | void *InsertPos = nullptr; | |||
9368 | SubstTemplateTemplateParmPackStorage *Subst | |||
9369 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); | |||
9370 | ||||
9371 | if (!Subst) { | |||
9372 | Subst = new (*this) SubstTemplateTemplateParmPackStorage( | |||
9373 | ArgPack.pack_elements(), AssociatedDecl, Index, Final); | |||
9374 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); | |||
9375 | } | |||
9376 | ||||
9377 | return TemplateName(Subst); | |||
9378 | } | |||
9379 | ||||
9380 | /// getFromTargetType - Given one of the integer types provided by | |||
9381 | /// TargetInfo, produce the corresponding type. The unsigned @p Type | |||
9382 | /// is actually a value of type @c TargetInfo::IntType. | |||
9383 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { | |||
9384 | switch (Type) { | |||
9385 | case TargetInfo::NoInt: return {}; | |||
9386 | case TargetInfo::SignedChar: return SignedCharTy; | |||
9387 | case TargetInfo::UnsignedChar: return UnsignedCharTy; | |||
9388 | case TargetInfo::SignedShort: return ShortTy; | |||
9389 | case TargetInfo::UnsignedShort: return UnsignedShortTy; | |||
9390 | case TargetInfo::SignedInt: return IntTy; | |||
9391 | case TargetInfo::UnsignedInt: return UnsignedIntTy; | |||
9392 | case TargetInfo::SignedLong: return LongTy; | |||
9393 | case TargetInfo::UnsignedLong: return UnsignedLongTy; | |||
9394 | case TargetInfo::SignedLongLong: return LongLongTy; | |||
9395 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; | |||
9396 | } | |||
9397 | ||||
9398 | llvm_unreachable("Unhandled TargetInfo::IntType value")::llvm::llvm_unreachable_internal("Unhandled TargetInfo::IntType value" , "clang/lib/AST/ASTContext.cpp", 9398); | |||
9399 | } | |||
9400 | ||||
9401 | //===----------------------------------------------------------------------===// | |||
9402 | // Type Predicates. | |||
9403 | //===----------------------------------------------------------------------===// | |||
9404 | ||||
9405 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's | |||
9406 | /// garbage collection attribute. | |||
9407 | /// | |||
9408 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { | |||
9409 | if (getLangOpts().getGC() == LangOptions::NonGC) | |||
9410 | return Qualifiers::GCNone; | |||
9411 | ||||
9412 | assert(getLangOpts().ObjC)(static_cast <bool> (getLangOpts().ObjC) ? void (0) : __assert_fail ("getLangOpts().ObjC", "clang/lib/AST/ASTContext.cpp", 9412, __extension__ __PRETTY_FUNCTION__)); | |||
9413 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); | |||
9414 | ||||
9415 | // Default behaviour under objective-C's gc is for ObjC pointers | |||
9416 | // (or pointers to them) be treated as though they were declared | |||
9417 | // as __strong. | |||
9418 | if (GCAttrs == Qualifiers::GCNone) { | |||
9419 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) | |||
9420 | return Qualifiers::Strong; | |||
9421 | else if (Ty->isPointerType()) | |||
9422 | return getObjCGCAttrKind(Ty->castAs<PointerType>()->getPointeeType()); | |||
9423 | } else { | |||
9424 | // It's not valid to set GC attributes on anything that isn't a | |||
9425 | // pointer. | |||
9426 | #ifndef NDEBUG | |||
9427 | QualType CT = Ty->getCanonicalTypeInternal(); | |||
9428 | while (const auto *AT = dyn_cast<ArrayType>(CT)) | |||
9429 | CT = AT->getElementType(); | |||
9430 | assert(CT->isAnyPointerType() || CT->isBlockPointerType())(static_cast <bool> (CT->isAnyPointerType() || CT-> isBlockPointerType()) ? void (0) : __assert_fail ("CT->isAnyPointerType() || CT->isBlockPointerType()" , "clang/lib/AST/ASTContext.cpp", 9430, __extension__ __PRETTY_FUNCTION__ )); | |||
9431 | #endif | |||
9432 | } | |||
9433 | return GCAttrs; | |||
9434 | } | |||
9435 | ||||
9436 | //===----------------------------------------------------------------------===// | |||
9437 | // Type Compatibility Testing | |||
9438 | //===----------------------------------------------------------------------===// | |||
9439 | ||||
9440 | /// areCompatVectorTypes - Return true if the two specified vector types are | |||
9441 | /// compatible. | |||
9442 | static bool areCompatVectorTypes(const VectorType *LHS, | |||
9443 | const VectorType *RHS) { | |||
9444 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified())(static_cast <bool> (LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()) ? void (0) : __assert_fail ("LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()" , "clang/lib/AST/ASTContext.cpp", 9444, __extension__ __PRETTY_FUNCTION__ )); | |||
9445 | return LHS->getElementType() == RHS->getElementType() && | |||
9446 | LHS->getNumElements() == RHS->getNumElements(); | |||
9447 | } | |||
9448 | ||||
9449 | /// areCompatMatrixTypes - Return true if the two specified matrix types are | |||
9450 | /// compatible. | |||
9451 | static bool areCompatMatrixTypes(const ConstantMatrixType *LHS, | |||
9452 | const ConstantMatrixType *RHS) { | |||
9453 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified())(static_cast <bool> (LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()) ? void (0) : __assert_fail ("LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()" , "clang/lib/AST/ASTContext.cpp", 9453, __extension__ __PRETTY_FUNCTION__ )); | |||
9454 | return LHS->getElementType() == RHS->getElementType() && | |||
9455 | LHS->getNumRows() == RHS->getNumRows() && | |||
9456 | LHS->getNumColumns() == RHS->getNumColumns(); | |||
9457 | } | |||
9458 | ||||
9459 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, | |||
9460 | QualType SecondVec) { | |||
9461 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type")(static_cast <bool> (FirstVec->isVectorType() && "FirstVec should be a vector type") ? void (0) : __assert_fail ("FirstVec->isVectorType() && \"FirstVec should be a vector type\"" , "clang/lib/AST/ASTContext.cpp", 9461, __extension__ __PRETTY_FUNCTION__ )); | |||
9462 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type")(static_cast <bool> (SecondVec->isVectorType() && "SecondVec should be a vector type") ? void (0) : __assert_fail ("SecondVec->isVectorType() && \"SecondVec should be a vector type\"" , "clang/lib/AST/ASTContext.cpp", 9462, __extension__ __PRETTY_FUNCTION__ )); | |||
9463 | ||||
9464 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) | |||
9465 | return true; | |||
9466 | ||||
9467 | // Treat Neon vector types and most AltiVec vector types as if they are the | |||
9468 | // equivalent GCC vector types. | |||
9469 | const auto *First = FirstVec->castAs<VectorType>(); | |||
9470 | const auto *Second = SecondVec->castAs<VectorType>(); | |||
9471 | if (First->getNumElements() == Second->getNumElements() && | |||
9472 | hasSameType(First->getElementType(), Second->getElementType()) && | |||
9473 | First->getVectorKind() != VectorType::AltiVecPixel && | |||
9474 | First->getVectorKind() != VectorType::AltiVecBool && | |||
9475 | Second->getVectorKind() != VectorType::AltiVecPixel && | |||
9476 | Second->getVectorKind() != VectorType::AltiVecBool && | |||
9477 | First->getVectorKind() != VectorType::SveFixedLengthDataVector && | |||
9478 | First->getVectorKind() != VectorType::SveFixedLengthPredicateVector && | |||
9479 | Second->getVectorKind() != VectorType::SveFixedLengthDataVector && | |||
9480 | Second->getVectorKind() != VectorType::SveFixedLengthPredicateVector && | |||
9481 | First->getVectorKind() != VectorType::RVVFixedLengthDataVector && | |||
9482 | Second->getVectorKind() != VectorType::RVVFixedLengthDataVector) | |||
9483 | return true; | |||
9484 | ||||
9485 | return false; | |||
9486 | } | |||
9487 | ||||
9488 | /// getSVETypeSize - Return SVE vector or predicate register size. | |||
9489 | static uint64_t getSVETypeSize(ASTContext &Context, const BuiltinType *Ty) { | |||
9490 | assert(Ty->isVLSTBuiltinType() && "Invalid SVE Type")(static_cast <bool> (Ty->isVLSTBuiltinType() && "Invalid SVE Type") ? void (0) : __assert_fail ("Ty->isVLSTBuiltinType() && \"Invalid SVE Type\"" , "clang/lib/AST/ASTContext.cpp", 9490, __extension__ __PRETTY_FUNCTION__ )); | |||
9491 | if (Ty->getKind() == BuiltinType::SveBool || | |||
9492 | Ty->getKind() == BuiltinType::SveCount) | |||
9493 | return (Context.getLangOpts().VScaleMin * 128) / Context.getCharWidth(); | |||
9494 | return Context.getLangOpts().VScaleMin * 128; | |||
9495 | } | |||
9496 | ||||
9497 | bool ASTContext::areCompatibleSveTypes(QualType FirstType, | |||
9498 | QualType SecondType) { | |||
9499 | assert((static_cast <bool> (((FirstType->isSVESizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isSVESizelessBuiltinType ())) && "Expected SVE builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) && \"Expected SVE builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9502, __extension__ __PRETTY_FUNCTION__ )) | |||
9500 | ((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) ||(static_cast <bool> (((FirstType->isSVESizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isSVESizelessBuiltinType ())) && "Expected SVE builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) && \"Expected SVE builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9502, __extension__ __PRETTY_FUNCTION__ )) | |||
9501 | (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) &&(static_cast <bool> (((FirstType->isSVESizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isSVESizelessBuiltinType ())) && "Expected SVE builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) && \"Expected SVE builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9502, __extension__ __PRETTY_FUNCTION__ )) | |||
9502 | "Expected SVE builtin type and vector type!")(static_cast <bool> (((FirstType->isSVESizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isSVESizelessBuiltinType ())) && "Expected SVE builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) && \"Expected SVE builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9502, __extension__ __PRETTY_FUNCTION__ )); | |||
9503 | ||||
9504 | auto IsValidCast = [this](QualType FirstType, QualType SecondType) { | |||
9505 | if (const auto *BT = FirstType->getAs<BuiltinType>()) { | |||
9506 | if (const auto *VT = SecondType->getAs<VectorType>()) { | |||
9507 | // Predicates have the same representation as uint8 so we also have to | |||
9508 | // check the kind to make these types incompatible. | |||
9509 | if (VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector) | |||
9510 | return BT->getKind() == BuiltinType::SveBool; | |||
9511 | else if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector) | |||
9512 | return VT->getElementType().getCanonicalType() == | |||
9513 | FirstType->getSveEltType(*this); | |||
9514 | else if (VT->getVectorKind() == VectorType::GenericVector) | |||
9515 | return getTypeSize(SecondType) == getSVETypeSize(*this, BT) && | |||
9516 | hasSameType(VT->getElementType(), | |||
9517 | getBuiltinVectorTypeInfo(BT).ElementType); | |||
9518 | } | |||
9519 | } | |||
9520 | return false; | |||
9521 | }; | |||
9522 | ||||
9523 | return IsValidCast(FirstType, SecondType) || | |||
9524 | IsValidCast(SecondType, FirstType); | |||
9525 | } | |||
9526 | ||||
9527 | bool ASTContext::areLaxCompatibleSveTypes(QualType FirstType, | |||
9528 | QualType SecondType) { | |||
9529 | assert((static_cast <bool> (((FirstType->isSVESizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isSVESizelessBuiltinType ())) && "Expected SVE builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) && \"Expected SVE builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9532, __extension__ __PRETTY_FUNCTION__ )) | |||
9530 | ((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) ||(static_cast <bool> (((FirstType->isSVESizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isSVESizelessBuiltinType ())) && "Expected SVE builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) && \"Expected SVE builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9532, __extension__ __PRETTY_FUNCTION__ )) | |||
9531 | (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) &&(static_cast <bool> (((FirstType->isSVESizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isSVESizelessBuiltinType ())) && "Expected SVE builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) && \"Expected SVE builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9532, __extension__ __PRETTY_FUNCTION__ )) | |||
9532 | "Expected SVE builtin type and vector type!")(static_cast <bool> (((FirstType->isSVESizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isSVESizelessBuiltinType ())) && "Expected SVE builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isSVESizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isSVESizelessBuiltinType())) && \"Expected SVE builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9532, __extension__ __PRETTY_FUNCTION__ )); | |||
9533 | ||||
9534 | auto IsLaxCompatible = [this](QualType FirstType, QualType SecondType) { | |||
9535 | const auto *BT = FirstType->getAs<BuiltinType>(); | |||
9536 | if (!BT) | |||
9537 | return false; | |||
9538 | ||||
9539 | const auto *VecTy = SecondType->getAs<VectorType>(); | |||
9540 | if (VecTy && | |||
9541 | (VecTy->getVectorKind() == VectorType::SveFixedLengthDataVector || | |||
9542 | VecTy->getVectorKind() == VectorType::GenericVector)) { | |||
9543 | const LangOptions::LaxVectorConversionKind LVCKind = | |||
9544 | getLangOpts().getLaxVectorConversions(); | |||
9545 | ||||
9546 | // Can not convert between sve predicates and sve vectors because of | |||
9547 | // different size. | |||
9548 | if (BT->getKind() == BuiltinType::SveBool && | |||
9549 | VecTy->getVectorKind() == VectorType::SveFixedLengthDataVector) | |||
9550 | return false; | |||
9551 | ||||
9552 | // If __ARM_FEATURE_SVE_BITS != N do not allow GNU vector lax conversion. | |||
9553 | // "Whenever __ARM_FEATURE_SVE_BITS==N, GNUT implicitly | |||
9554 | // converts to VLAT and VLAT implicitly converts to GNUT." | |||
9555 | // ACLE Spec Version 00bet6, 3.7.3.2. Behavior common to vectors and | |||
9556 | // predicates. | |||
9557 | if (VecTy->getVectorKind() == VectorType::GenericVector && | |||
9558 | getTypeSize(SecondType) != getSVETypeSize(*this, BT)) | |||
9559 | return false; | |||
9560 | ||||
9561 | // If -flax-vector-conversions=all is specified, the types are | |||
9562 | // certainly compatible. | |||
9563 | if (LVCKind == LangOptions::LaxVectorConversionKind::All) | |||
9564 | return true; | |||
9565 | ||||
9566 | // If -flax-vector-conversions=integer is specified, the types are | |||
9567 | // compatible if the elements are integer types. | |||
9568 | if (LVCKind == LangOptions::LaxVectorConversionKind::Integer) | |||
9569 | return VecTy->getElementType().getCanonicalType()->isIntegerType() && | |||
9570 | FirstType->getSveEltType(*this)->isIntegerType(); | |||
9571 | } | |||
9572 | ||||
9573 | return false; | |||
9574 | }; | |||
9575 | ||||
9576 | return IsLaxCompatible(FirstType, SecondType) || | |||
9577 | IsLaxCompatible(SecondType, FirstType); | |||
9578 | } | |||
9579 | ||||
9580 | /// getRVVTypeSize - Return RVV vector register size. | |||
9581 | static uint64_t getRVVTypeSize(ASTContext &Context, const BuiltinType *Ty) { | |||
9582 | assert(Ty->isRVVVLSBuiltinType() && "Invalid RVV Type")(static_cast <bool> (Ty->isRVVVLSBuiltinType() && "Invalid RVV Type") ? void (0) : __assert_fail ("Ty->isRVVVLSBuiltinType() && \"Invalid RVV Type\"" , "clang/lib/AST/ASTContext.cpp", 9582, __extension__ __PRETTY_FUNCTION__ )); | |||
9583 | auto VScale = Context.getTargetInfo().getVScaleRange(Context.getLangOpts()); | |||
9584 | return VScale ? VScale->first * llvm::RISCV::RVVBitsPerBlock : 0; | |||
9585 | } | |||
9586 | ||||
9587 | bool ASTContext::areCompatibleRVVTypes(QualType FirstType, | |||
9588 | QualType SecondType) { | |||
9589 | assert((static_cast <bool> (((FirstType->isRVVSizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isRVVSizelessBuiltinType ())) && "Expected RVV builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && \"Expected RVV builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9592, __extension__ __PRETTY_FUNCTION__ )) | |||
9590 | ((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) ||(static_cast <bool> (((FirstType->isRVVSizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isRVVSizelessBuiltinType ())) && "Expected RVV builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && \"Expected RVV builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9592, __extension__ __PRETTY_FUNCTION__ )) | |||
9591 | (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) &&(static_cast <bool> (((FirstType->isRVVSizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isRVVSizelessBuiltinType ())) && "Expected RVV builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && \"Expected RVV builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9592, __extension__ __PRETTY_FUNCTION__ )) | |||
9592 | "Expected RVV builtin type and vector type!")(static_cast <bool> (((FirstType->isRVVSizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isRVVSizelessBuiltinType ())) && "Expected RVV builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && \"Expected RVV builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9592, __extension__ __PRETTY_FUNCTION__ )); | |||
9593 | ||||
9594 | auto IsValidCast = [this](QualType FirstType, QualType SecondType) { | |||
9595 | if (const auto *BT = FirstType->getAs<BuiltinType>()) { | |||
9596 | if (const auto *VT = SecondType->getAs<VectorType>()) { | |||
9597 | // Predicates have the same representation as uint8 so we also have to | |||
9598 | // check the kind to make these types incompatible. | |||
9599 | if (VT->getVectorKind() == VectorType::RVVFixedLengthDataVector) | |||
9600 | return FirstType->isRVVVLSBuiltinType() && | |||
9601 | VT->getElementType().getCanonicalType() == | |||
9602 | FirstType->getRVVEltType(*this); | |||
9603 | if (VT->getVectorKind() == VectorType::GenericVector) | |||
9604 | return getTypeSize(SecondType) == getRVVTypeSize(*this, BT) && | |||
9605 | hasSameType(VT->getElementType(), | |||
9606 | getBuiltinVectorTypeInfo(BT).ElementType); | |||
9607 | } | |||
9608 | } | |||
9609 | return false; | |||
9610 | }; | |||
9611 | ||||
9612 | return IsValidCast(FirstType, SecondType) || | |||
9613 | IsValidCast(SecondType, FirstType); | |||
9614 | } | |||
9615 | ||||
9616 | bool ASTContext::areLaxCompatibleRVVTypes(QualType FirstType, | |||
9617 | QualType SecondType) { | |||
9618 | assert((static_cast <bool> (((FirstType->isRVVSizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isRVVSizelessBuiltinType ())) && "Expected RVV builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && \"Expected RVV builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9621, __extension__ __PRETTY_FUNCTION__ )) | |||
9619 | ((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) ||(static_cast <bool> (((FirstType->isRVVSizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isRVVSizelessBuiltinType ())) && "Expected RVV builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && \"Expected RVV builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9621, __extension__ __PRETTY_FUNCTION__ )) | |||
9620 | (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) &&(static_cast <bool> (((FirstType->isRVVSizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isRVVSizelessBuiltinType ())) && "Expected RVV builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && \"Expected RVV builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9621, __extension__ __PRETTY_FUNCTION__ )) | |||
9621 | "Expected RVV builtin type and vector type!")(static_cast <bool> (((FirstType->isRVVSizelessBuiltinType () && SecondType->isVectorType()) || (FirstType-> isVectorType() && SecondType->isRVVSizelessBuiltinType ())) && "Expected RVV builtin type and vector type!") ? void (0) : __assert_fail ("((FirstType->isRVVSizelessBuiltinType() && SecondType->isVectorType()) || (FirstType->isVectorType() && SecondType->isRVVSizelessBuiltinType())) && \"Expected RVV builtin type and vector type!\"" , "clang/lib/AST/ASTContext.cpp", 9621, __extension__ __PRETTY_FUNCTION__ )); | |||
9622 | ||||
9623 | auto IsLaxCompatible = [this](QualType FirstType, QualType SecondType) { | |||
9624 | const auto *BT = FirstType->getAs<BuiltinType>(); | |||
9625 | if (!BT) | |||
9626 | return false; | |||
9627 | ||||
9628 | const auto *VecTy = SecondType->getAs<VectorType>(); | |||
9629 | if (VecTy && | |||
9630 | (VecTy->getVectorKind() == VectorType::RVVFixedLengthDataVector || | |||
9631 | VecTy->getVectorKind() == VectorType::GenericVector)) { | |||
9632 | const LangOptions::LaxVectorConversionKind LVCKind = | |||
9633 | getLangOpts().getLaxVectorConversions(); | |||
9634 | ||||
9635 | // If __riscv_v_fixed_vlen != N do not allow GNU vector lax conversion. | |||
9636 | if (VecTy->getVectorKind() == VectorType::GenericVector && | |||
9637 | getTypeSize(SecondType) != getRVVTypeSize(*this, BT)) | |||
9638 | return false; | |||
9639 | ||||
9640 | // If -flax-vector-conversions=all is specified, the types are | |||
9641 | // certainly compatible. | |||
9642 | if (LVCKind == LangOptions::LaxVectorConversionKind::All) | |||
9643 | return true; | |||
9644 | ||||
9645 | // If -flax-vector-conversions=integer is specified, the types are | |||
9646 | // compatible if the elements are integer types. | |||
9647 | if (LVCKind == LangOptions::LaxVectorConversionKind::Integer) | |||
9648 | return VecTy->getElementType().getCanonicalType()->isIntegerType() && | |||
9649 | FirstType->getRVVEltType(*this)->isIntegerType(); | |||
9650 | } | |||
9651 | ||||
9652 | return false; | |||
9653 | }; | |||
9654 | ||||
9655 | return IsLaxCompatible(FirstType, SecondType) || | |||
9656 | IsLaxCompatible(SecondType, FirstType); | |||
9657 | } | |||
9658 | ||||
9659 | bool ASTContext::hasDirectOwnershipQualifier(QualType Ty) const { | |||
9660 | while (true) { | |||
9661 | // __strong id | |||
9662 | if (const AttributedType *Attr = dyn_cast<AttributedType>(Ty)) { | |||
9663 | if (Attr->getAttrKind() == attr::ObjCOwnership) | |||
9664 | return true; | |||
9665 | ||||
9666 | Ty = Attr->getModifiedType(); | |||
9667 | ||||
9668 | // X *__strong (...) | |||
9669 | } else if (const ParenType *Paren = dyn_cast<ParenType>(Ty)) { | |||
9670 | Ty = Paren->getInnerType(); | |||
9671 | ||||
9672 | // We do not want to look through typedefs, typeof(expr), | |||
9673 | // typeof(type), or any other way that the type is somehow | |||
9674 | // abstracted. | |||
9675 | } else { | |||
9676 | return false; | |||
9677 | } | |||
9678 | } | |||
9679 | } | |||
9680 | ||||
9681 | //===----------------------------------------------------------------------===// | |||
9682 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. | |||
9683 | //===----------------------------------------------------------------------===// | |||
9684 | ||||
9685 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the | |||
9686 | /// inheritance hierarchy of 'rProto'. | |||
9687 | bool | |||
9688 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, | |||
9689 | ObjCProtocolDecl *rProto) const { | |||
9690 | if (declaresSameEntity(lProto, rProto)) | |||
9691 | return true; | |||
9692 | for (auto *PI : rProto->protocols()) | |||
9693 | if (ProtocolCompatibleWithProtocol(lProto, PI)) | |||
9694 | return true; | |||
9695 | return false; | |||
9696 | } | |||
9697 | ||||
9698 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and | |||
9699 | /// Class<pr1, ...>. | |||
9700 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible( | |||
9701 | const ObjCObjectPointerType *lhs, const ObjCObjectPointerType *rhs) { | |||
9702 | for (auto *lhsProto : lhs->quals()) { | |||
9703 | bool match = false; | |||
9704 | for (auto *rhsProto : rhs->quals()) { | |||
9705 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { | |||
9706 | match = true; | |||
9707 | break; | |||
9708 | } | |||
9709 | } | |||
9710 | if (!match) | |||
9711 | return false; | |||
9712 | } | |||
9713 | return true; | |||
9714 | } | |||
9715 | ||||
9716 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an | |||
9717 | /// ObjCQualifiedIDType. | |||
9718 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible( | |||
9719 | const ObjCObjectPointerType *lhs, const ObjCObjectPointerType *rhs, | |||
9720 | bool compare) { | |||
9721 | // Allow id<P..> and an 'id' in all cases. | |||
9722 | if (lhs->isObjCIdType() || rhs->isObjCIdType()) | |||
9723 | return true; | |||
9724 | ||||
9725 | // Don't allow id<P..> to convert to Class or Class<P..> in either direction. | |||
9726 | if (lhs->isObjCClassType() || lhs->isObjCQualifiedClassType() || | |||
9727 | rhs->isObjCClassType() || rhs->isObjCQualifiedClassType()) | |||
9728 | return false; | |||
9729 | ||||
9730 | if (lhs->isObjCQualifiedIdType()) { | |||
9731 | if (rhs->qual_empty()) { | |||
9732 | // If the RHS is a unqualified interface pointer "NSString*", | |||
9733 | // make sure we check the class hierarchy. | |||
9734 | if (ObjCInterfaceDecl *rhsID = rhs->getInterfaceDecl()) { | |||
9735 | for (auto *I : lhs->quals()) { | |||
9736 | // when comparing an id<P> on lhs with a static type on rhs, | |||
9737 | // see if static class implements all of id's protocols, directly or | |||
9738 | // through its super class and categories. | |||
9739 | if (!rhsID->ClassImplementsProtocol(I, true)) | |||
9740 | return false; | |||
9741 | } | |||
9742 | } | |||
9743 | // If there are no qualifiers and no interface, we have an 'id'. | |||
9744 | return true; | |||
9745 | } | |||
9746 | // Both the right and left sides have qualifiers. | |||
9747 | for (auto *lhsProto : lhs->quals()) { | |||
9748 | bool match = false; | |||
9749 | ||||
9750 | // when comparing an id<P> on lhs with a static type on rhs, | |||
9751 | // see if static class implements all of id's protocols, directly or | |||
9752 | // through its super class and categories. | |||
9753 | for (auto *rhsProto : rhs->quals()) { | |||
9754 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || | |||
9755 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { | |||
9756 | match = true; | |||
9757 | break; | |||
9758 | } | |||
9759 | } | |||
9760 | // If the RHS is a qualified interface pointer "NSString<P>*", | |||
9761 | // make sure we check the class hierarchy. | |||
9762 | if (ObjCInterfaceDecl *rhsID = rhs->getInterfaceDecl()) { | |||
9763 | for (auto *I : lhs->quals()) { | |||
9764 | // when comparing an id<P> on lhs with a static type on rhs, | |||
9765 | // see if static class implements all of id's protocols, directly or | |||
9766 | // through its super class and categories. | |||
9767 | if (rhsID->ClassImplementsProtocol(I, true)) { | |||
9768 | match = true; | |||
9769 | break; | |||
9770 | } | |||
9771 | } | |||
9772 | } | |||
9773 | if (!match) | |||
9774 | return false; | |||
9775 | } | |||
9776 | ||||
9777 | return true; | |||
9778 | } | |||
9779 | ||||
9780 | assert(rhs->isObjCQualifiedIdType() && "One of the LHS/RHS should be id<x>")(static_cast <bool> (rhs->isObjCQualifiedIdType() && "One of the LHS/RHS should be id<x>") ? void (0) : __assert_fail ("rhs->isObjCQualifiedIdType() && \"One of the LHS/RHS should be id<x>\"" , "clang/lib/AST/ASTContext.cpp", 9780, __extension__ __PRETTY_FUNCTION__ )); | |||
9781 | ||||
9782 | if (lhs->getInterfaceType()) { | |||
9783 | // If both the right and left sides have qualifiers. | |||
9784 | for (auto *lhsProto : lhs->quals()) { | |||
9785 | bool match = false; | |||
9786 | ||||
9787 | // when comparing an id<P> on rhs with a static type on lhs, | |||
9788 | // see if static class implements all of id's protocols, directly or | |||
9789 | // through its super class and categories. | |||
9790 | // First, lhs protocols in the qualifier list must be found, direct | |||
9791 | // or indirect in rhs's qualifier list or it is a mismatch. | |||
9792 | for (auto *rhsProto : rhs->quals()) { | |||
9793 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || | |||
9794 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { | |||
9795 | match = true; | |||
9796 | break; | |||
9797 | } | |||
9798 | } | |||
9799 | if (!match) | |||
9800 | return false; | |||
9801 | } | |||
9802 | ||||
9803 | // Static class's protocols, or its super class or category protocols | |||
9804 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. | |||
9805 | if (ObjCInterfaceDecl *lhsID = lhs->getInterfaceDecl()) { | |||
9806 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; | |||
9807 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); | |||
9808 | // This is rather dubious but matches gcc's behavior. If lhs has | |||
9809 | // no type qualifier and its class has no static protocol(s) | |||
9810 | // assume that it is mismatch. | |||
9811 | if (LHSInheritedProtocols.empty() && lhs->qual_empty()) | |||
9812 | return false; | |||
9813 | for (auto *lhsProto : LHSInheritedProtocols) { | |||
9814 | bool match = false; | |||
9815 | for (auto *rhsProto : rhs->quals()) { | |||
9816 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || | |||
9817 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { | |||
9818 | match = true; | |||
9819 | break; | |||
9820 | } | |||
9821 | } | |||
9822 | if (!match) | |||
9823 | return false; | |||
9824 | } | |||
9825 | } | |||
9826 | return true; | |||
9827 | } | |||
9828 | return false; | |||
9829 | } | |||
9830 | ||||
9831 | /// canAssignObjCInterfaces - Return true if the two interface types are | |||
9832 | /// compatible for assignment from RHS to LHS. This handles validation of any | |||
9833 | /// protocol qualifiers on the LHS or RHS. | |||
9834 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, | |||
9835 | const ObjCObjectPointerType *RHSOPT) { | |||
9836 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); | |||
9837 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); | |||
9838 | ||||
9839 | // If either type represents the built-in 'id' type, return true. | |||
9840 | if (LHS->isObjCUnqualifiedId() || RHS->isObjCUnqualifiedId()) | |||
9841 | return true; | |||
9842 | ||||
9843 | // Function object that propagates a successful result or handles | |||
9844 | // __kindof types. | |||
9845 | auto finish = [&](bool succeeded) -> bool { | |||
9846 | if (succeeded) | |||
9847 | return true; | |||
9848 | ||||
9849 | if (!RHS->isKindOfType()) | |||
9850 | return false; | |||
9851 | ||||
9852 | // Strip off __kindof and protocol qualifiers, then check whether | |||
9853 | // we can assign the other way. | |||
9854 | return canAssignObjCInterfaces(RHSOPT->stripObjCKindOfTypeAndQuals(*this), | |||
9855 | LHSOPT->stripObjCKindOfTypeAndQuals(*this)); | |||
9856 | }; | |||
9857 | ||||
9858 | // Casts from or to id<P> are allowed when the other side has compatible | |||
9859 | // protocols. | |||
9860 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) { | |||
9861 | return finish(ObjCQualifiedIdTypesAreCompatible(LHSOPT, RHSOPT, false)); | |||
9862 | } | |||
9863 | ||||
9864 | // Verify protocol compatibility for casts from Class<P1> to Class<P2>. | |||
9865 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) { | |||
9866 | return finish(ObjCQualifiedClassTypesAreCompatible(LHSOPT, RHSOPT)); | |||
9867 | } | |||
9868 | ||||
9869 | // Casts from Class to Class<Foo>, or vice-versa, are allowed. | |||
9870 | if (LHS->isObjCClass() && RHS->isObjCClass()) { | |||
9871 | return true; | |||
9872 | } | |||
9873 | ||||
9874 | // If we have 2 user-defined types, fall into that path. | |||
9875 | if (LHS->getInterface() && RHS->getInterface()) { | |||
9876 | return finish(canAssignObjCInterfaces(LHS, RHS)); | |||
9877 | } | |||
9878 | ||||
9879 | return false; | |||
9880 | } | |||
9881 | ||||
9882 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written | |||
9883 | /// for providing type-safety for objective-c pointers used to pass/return | |||
9884 | /// arguments in block literals. When passed as arguments, passing 'A*' where | |||
9885 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is | |||
9886 | /// not OK. For the return type, the opposite is not OK. | |||
9887 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( | |||
9888 | const ObjCObjectPointerType *LHSOPT, | |||
9889 | const ObjCObjectPointerType *RHSOPT, | |||
9890 | bool BlockReturnType) { | |||
9891 | ||||
9892 | // Function object that propagates a successful result or handles | |||
9893 | // __kindof types. | |||
9894 | auto finish = [&](bool succeeded) -> bool { | |||
9895 | if (succeeded) | |||
9896 | return true; | |||
9897 | ||||
9898 | const ObjCObjectPointerType *Expected = BlockReturnType ? RHSOPT : LHSOPT; | |||
9899 | if (!Expected->isKindOfType()) | |||
9900 | return false; | |||
9901 | ||||
9902 | // Strip off __kindof and protocol qualifiers, then check whether | |||
9903 | // we can assign the other way. | |||
9904 | return canAssignObjCInterfacesInBlockPointer( | |||
9905 | RHSOPT->stripObjCKindOfTypeAndQuals(*this), | |||
9906 | LHSOPT->stripObjCKindOfTypeAndQuals(*this), | |||
9907 | BlockReturnType); | |||
9908 | }; | |||
9909 | ||||
9910 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) | |||
9911 | return true; | |||
9912 | ||||
9913 | if (LHSOPT->isObjCBuiltinType()) { | |||
9914 | return finish(RHSOPT->isObjCBuiltinType() || | |||
9915 | RHSOPT->isObjCQualifiedIdType()); | |||
9916 | } | |||
9917 | ||||
9918 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) { | |||
9919 | if (getLangOpts().CompatibilityQualifiedIdBlockParamTypeChecking) | |||
9920 | // Use for block parameters previous type checking for compatibility. | |||
9921 | return finish(ObjCQualifiedIdTypesAreCompatible(LHSOPT, RHSOPT, false) || | |||
9922 | // Or corrected type checking as in non-compat mode. | |||
9923 | (!BlockReturnType && | |||
9924 | ObjCQualifiedIdTypesAreCompatible(RHSOPT, LHSOPT, false))); | |||
9925 | else | |||
9926 | return finish(ObjCQualifiedIdTypesAreCompatible( | |||
9927 | (BlockReturnType ? LHSOPT : RHSOPT), | |||
9928 | (BlockReturnType ? RHSOPT : LHSOPT), false)); | |||
9929 | } | |||
9930 | ||||
9931 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); | |||
9932 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); | |||
9933 | if (LHS && RHS) { // We have 2 user-defined types. | |||
9934 | if (LHS != RHS) { | |||
9935 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) | |||
9936 | return finish(BlockReturnType); | |||
9937 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) | |||
9938 | return finish(!BlockReturnType); | |||
9939 | } | |||
9940 | else | |||
9941 | return true; | |||
9942 | } | |||
9943 | return false; | |||
9944 | } | |||
9945 | ||||
9946 | /// Comparison routine for Objective-C protocols to be used with | |||
9947 | /// llvm::array_pod_sort. | |||
9948 | static int compareObjCProtocolsByName(ObjCProtocolDecl * const *lhs, | |||
9949 | ObjCProtocolDecl * const *rhs) { | |||
9950 | return (*lhs)->getName().compare((*rhs)->getName()); | |||
9951 | } | |||
9952 | ||||
9953 | /// getIntersectionOfProtocols - This routine finds the intersection of set | |||
9954 | /// of protocols inherited from two distinct objective-c pointer objects with | |||
9955 | /// the given common base. | |||
9956 | /// It is used to build composite qualifier list of the composite type of | |||
9957 | /// the conditional expression involving two objective-c pointer objects. | |||
9958 | static | |||
9959 | void getIntersectionOfProtocols(ASTContext &Context, | |||
9960 | const ObjCInterfaceDecl *CommonBase, | |||
9961 | const ObjCObjectPointerType *LHSOPT, | |||
9962 | const ObjCObjectPointerType *RHSOPT, | |||
9963 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionSet) { | |||
9964 | ||||
9965 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); | |||
9966 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); | |||
9967 | assert(LHS->getInterface() && "LHS must have an interface base")(static_cast <bool> (LHS->getInterface() && "LHS must have an interface base" ) ? void (0) : __assert_fail ("LHS->getInterface() && \"LHS must have an interface base\"" , "clang/lib/AST/ASTContext.cpp", 9967, __extension__ __PRETTY_FUNCTION__ )); | |||
9968 | assert(RHS->getInterface() && "RHS must have an interface base")(static_cast <bool> (RHS->getInterface() && "RHS must have an interface base" ) ? void (0) : __assert_fail ("RHS->getInterface() && \"RHS must have an interface base\"" , "clang/lib/AST/ASTContext.cpp", 9968, __extension__ __PRETTY_FUNCTION__ )); | |||
9969 | ||||
9970 | // Add all of the protocols for the LHS. | |||
9971 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSProtocolSet; | |||
9972 | ||||
9973 | // Start with the protocol qualifiers. | |||
9974 | for (auto *proto : LHS->quals()) { | |||
9975 | Context.CollectInheritedProtocols(proto, LHSProtocolSet); | |||
9976 | } | |||
9977 | ||||
9978 | // Also add the protocols associated with the LHS interface. | |||
9979 | Context.CollectInheritedProtocols(LHS->getInterface(), LHSProtocolSet); | |||
9980 | ||||
9981 | // Add all of the protocols for the RHS. | |||
9982 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSProtocolSet; | |||
9983 | ||||
9984 | // Start with the protocol qualifiers. | |||
9985 | for (auto *proto : RHS->quals()) { | |||
9986 | Context.CollectInheritedProtocols(proto, RHSProtocolSet); | |||
9987 | } | |||
9988 | ||||
9989 | // Also add the protocols associated with the RHS interface. | |||
9990 | Context.CollectInheritedProtocols(RHS->getInterface(), RHSProtocolSet); | |||
9991 | ||||
9992 | // Compute the intersection of the collected protocol sets. | |||
9993 | for (auto *proto : LHSProtocolSet) { | |||
9994 | if (RHSProtocolSet.count(proto)) | |||
9995 | IntersectionSet.push_back(proto); | |||
9996 | } | |||
9997 | ||||
9998 | // Compute the set of protocols that is implied by either the common type or | |||
9999 | // the protocols within the intersection. | |||
10000 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ImpliedProtocols; | |||
10001 | Context.CollectInheritedProtocols(CommonBase, ImpliedProtocols); | |||
10002 | ||||
10003 | // Remove any implied protocols from the list of inherited protocols. | |||
10004 | if (!ImpliedProtocols.empty()) { | |||
10005 | llvm::erase_if(IntersectionSet, [&](ObjCProtocolDecl *proto) -> bool { | |||
10006 | return ImpliedProtocols.contains(proto); | |||
10007 | }); | |||
10008 | } | |||
10009 | ||||
10010 | // Sort the remaining protocols by name. | |||
10011 | llvm::array_pod_sort(IntersectionSet.begin(), IntersectionSet.end(), | |||
10012 | compareObjCProtocolsByName); | |||
10013 | } | |||
10014 | ||||
10015 | /// Determine whether the first type is a subtype of the second. | |||
10016 | static bool canAssignObjCObjectTypes(ASTContext &ctx, QualType lhs, | |||
10017 | QualType rhs) { | |||
10018 | // Common case: two object pointers. | |||
10019 | const auto *lhsOPT = lhs->getAs<ObjCObjectPointerType>(); | |||
10020 | const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); | |||
10021 | if (lhsOPT && rhsOPT) | |||
10022 | return ctx.canAssignObjCInterfaces(lhsOPT, rhsOPT); | |||
10023 | ||||
10024 | // Two block pointers. | |||
10025 | const auto *lhsBlock = lhs->getAs<BlockPointerType>(); | |||
10026 | const auto *rhsBlock = rhs->getAs<BlockPointerType>(); | |||
10027 | if (lhsBlock && rhsBlock) | |||
10028 | return ctx.typesAreBlockPointerCompatible(lhs, rhs); | |||
10029 | ||||
10030 | // If either is an unqualified 'id' and the other is a block, it's | |||
10031 | // acceptable. | |||
10032 | if ((lhsOPT && lhsOPT->isObjCIdType() && rhsBlock) || | |||
10033 | (rhsOPT && rhsOPT->isObjCIdType() && lhsBlock)) | |||
10034 | return true; | |||
10035 | ||||
10036 | return false; | |||
10037 | } | |||
10038 | ||||
10039 | // Check that the given Objective-C type argument lists are equivalent. | |||
10040 | static bool sameObjCTypeArgs(ASTContext &ctx, | |||
10041 | const ObjCInterfaceDecl *iface, | |||
10042 | ArrayRef<QualType> lhsArgs, | |||
10043 | ArrayRef<QualType> rhsArgs, | |||
10044 | bool stripKindOf) { | |||
10045 | if (lhsArgs.size() != rhsArgs.size()) | |||
10046 | return false; | |||
10047 | ||||
10048 | ObjCTypeParamList *typeParams = iface->getTypeParamList(); | |||
10049 | for (unsigned i = 0, n = lhsArgs.size(); i != n; ++i) { | |||
10050 | if (ctx.hasSameType(lhsArgs[i], rhsArgs[i])) | |||
10051 | continue; | |||
10052 | ||||
10053 | switch (typeParams->begin()[i]->getVariance()) { | |||
10054 | case ObjCTypeParamVariance::Invariant: | |||
10055 | if (!stripKindOf || | |||
10056 | !ctx.hasSameType(lhsArgs[i].stripObjCKindOfType(ctx), | |||
10057 | rhsArgs[i].stripObjCKindOfType(ctx))) { | |||
10058 | return false; | |||
10059 | } | |||
10060 | break; | |||
10061 | ||||
10062 | case ObjCTypeParamVariance::Covariant: | |||
10063 | if (!canAssignObjCObjectTypes(ctx, lhsArgs[i], rhsArgs[i])) | |||
10064 | return false; | |||
10065 | break; | |||
10066 | ||||
10067 | case ObjCTypeParamVariance::Contravariant: | |||
10068 | if (!canAssignObjCObjectTypes(ctx, rhsArgs[i], lhsArgs[i])) | |||
10069 | return false; | |||
10070 | break; | |||
10071 | } | |||
10072 | } | |||
10073 | ||||
10074 | return true; | |||
10075 | } | |||
10076 | ||||
10077 | QualType ASTContext::areCommonBaseCompatible( | |||
10078 | const ObjCObjectPointerType *Lptr, | |||
10079 | const ObjCObjectPointerType *Rptr) { | |||
10080 | const ObjCObjectType *LHS = Lptr->getObjectType(); | |||
10081 | const ObjCObjectType *RHS = Rptr->getObjectType(); | |||
10082 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); | |||
10083 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); | |||
10084 | ||||
10085 | if (!LDecl || !RDecl) | |||
10086 | return {}; | |||
10087 | ||||
10088 | // When either LHS or RHS is a kindof type, we should return a kindof type. | |||
10089 | // For example, for common base of kindof(ASub1) and kindof(ASub2), we return | |||
10090 | // kindof(A). | |||
10091 | bool anyKindOf = LHS->isKindOfType() || RHS->isKindOfType(); | |||
10092 | ||||
10093 | // Follow the left-hand side up the class hierarchy until we either hit a | |||
10094 | // root or find the RHS. Record the ancestors in case we don't find it. | |||
10095 | llvm::SmallDenseMap<const ObjCInterfaceDecl *, const ObjCObjectType *, 4> | |||
10096 | LHSAncestors; | |||
10097 | while (true) { | |||
10098 | // Record this ancestor. We'll need this if the common type isn't in the | |||
10099 | // path from the LHS to the root. | |||
10100 | LHSAncestors[LHS->getInterface()->getCanonicalDecl()] = LHS; | |||
10101 | ||||
10102 | if (declaresSameEntity(LHS->getInterface(), RDecl)) { | |||
10103 | // Get the type arguments. | |||
10104 | ArrayRef<QualType> LHSTypeArgs = LHS->getTypeArgsAsWritten(); | |||
10105 | bool anyChanges = false; | |||
10106 | if (LHS->isSpecialized() && RHS->isSpecialized()) { | |||
10107 | // Both have type arguments, compare them. | |||
10108 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), | |||
10109 | LHS->getTypeArgs(), RHS->getTypeArgs(), | |||
10110 | /*stripKindOf=*/true)) | |||
10111 | return {}; | |||
10112 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { | |||
10113 | // If only one has type arguments, the result will not have type | |||
10114 | // arguments. | |||
10115 | LHSTypeArgs = {}; | |||
10116 | anyChanges = true; | |||
10117 | } | |||
10118 | ||||
10119 | // Compute the intersection of protocols. | |||
10120 | SmallVector<ObjCProtocolDecl *, 8> Protocols; | |||
10121 | getIntersectionOfProtocols(*this, LHS->getInterface(), Lptr, Rptr, | |||
10122 | Protocols); | |||
10123 | if (!Protocols.empty()) | |||
10124 | anyChanges = true; | |||
10125 | ||||
10126 | // If anything in the LHS will have changed, build a new result type. | |||
10127 | // If we need to return a kindof type but LHS is not a kindof type, we | |||
10128 | // build a new result type. | |||
10129 | if (anyChanges || LHS->isKindOfType() != anyKindOf) { | |||
10130 | QualType Result = getObjCInterfaceType(LHS->getInterface()); | |||
10131 | Result = getObjCObjectType(Result, LHSTypeArgs, Protocols, | |||
10132 | anyKindOf || LHS->isKindOfType()); | |||
10133 | return getObjCObjectPointerType(Result); | |||
10134 | } | |||
10135 | ||||
10136 | return getObjCObjectPointerType(QualType(LHS, 0)); | |||
10137 | } | |||
10138 | ||||
10139 | // Find the superclass. | |||
10140 | QualType LHSSuperType = LHS->getSuperClassType(); | |||
10141 | if (LHSSuperType.isNull()) | |||
10142 | break; | |||
10143 | ||||
10144 | LHS = LHSSuperType->castAs<ObjCObjectType>(); | |||
10145 | } | |||
10146 | ||||
10147 | // We didn't find anything by following the LHS to its root; now check | |||
10148 | // the RHS against the cached set of ancestors. | |||
10149 | while (true) { | |||
10150 | auto KnownLHS = LHSAncestors.find(RHS->getInterface()->getCanonicalDecl()); | |||
10151 | if (KnownLHS != LHSAncestors.end()) { | |||
10152 | LHS = KnownLHS->second; | |||
10153 | ||||
10154 | // Get the type arguments. | |||
10155 | ArrayRef<QualType> RHSTypeArgs = RHS->getTypeArgsAsWritten(); | |||
10156 | bool anyChanges = false; | |||
10157 | if (LHS->isSpecialized() && RHS->isSpecialized()) { | |||
10158 | // Both have type arguments, compare them. | |||
10159 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), | |||
10160 | LHS->getTypeArgs(), RHS->getTypeArgs(), | |||
10161 | /*stripKindOf=*/true)) | |||
10162 | return {}; | |||
10163 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { | |||
10164 | // If only one has type arguments, the result will not have type | |||
10165 | // arguments. | |||
10166 | RHSTypeArgs = {}; | |||
10167 | anyChanges = true; | |||
10168 | } | |||
10169 | ||||
10170 | // Compute the intersection of protocols. | |||
10171 | SmallVector<ObjCProtocolDecl *, 8> Protocols; | |||
10172 | getIntersectionOfProtocols(*this, RHS->getInterface(), Lptr, Rptr, | |||
10173 | Protocols); | |||
10174 | if (!Protocols.empty()) | |||
10175 | anyChanges = true; | |||
10176 | ||||
10177 | // If we need to return a kindof type but RHS is not a kindof type, we | |||
10178 | // build a new result type. | |||
10179 | if (anyChanges || RHS->isKindOfType() != anyKindOf) { | |||
10180 | QualType Result = getObjCInterfaceType(RHS->getInterface()); | |||
10181 | Result = getObjCObjectType(Result, RHSTypeArgs, Protocols, | |||
10182 | anyKindOf || RHS->isKindOfType()); | |||
10183 | return getObjCObjectPointerType(Result); | |||
10184 | } | |||
10185 | ||||
10186 | return getObjCObjectPointerType(QualType(RHS, 0)); | |||
10187 | } | |||
10188 | ||||
10189 | // Find the superclass of the RHS. | |||
10190 | QualType RHSSuperType = RHS->getSuperClassType(); | |||
10191 | if (RHSSuperType.isNull()) | |||
10192 | break; | |||
10193 | ||||
10194 | RHS = RHSSuperType->castAs<ObjCObjectType>(); | |||
10195 | } | |||
10196 | ||||
10197 | return {}; | |||
10198 | } | |||
10199 | ||||
10200 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, | |||
10201 | const ObjCObjectType *RHS) { | |||
10202 | assert(LHS->getInterface() && "LHS is not an interface type")(static_cast <bool> (LHS->getInterface() && "LHS is not an interface type" ) ? void (0) : __assert_fail ("LHS->getInterface() && \"LHS is not an interface type\"" , "clang/lib/AST/ASTContext.cpp", 10202, __extension__ __PRETTY_FUNCTION__ )); | |||
10203 | assert(RHS->getInterface() && "RHS is not an interface type")(static_cast <bool> (RHS->getInterface() && "RHS is not an interface type" ) ? void (0) : __assert_fail ("RHS->getInterface() && \"RHS is not an interface type\"" , "clang/lib/AST/ASTContext.cpp", 10203, __extension__ __PRETTY_FUNCTION__ )); | |||
10204 | ||||
10205 | // Verify that the base decls are compatible: the RHS must be a subclass of | |||
10206 | // the LHS. | |||
10207 | ObjCInterfaceDecl *LHSInterface = LHS->getInterface(); | |||
10208 | bool IsSuperClass = LHSInterface->isSuperClassOf(RHS->getInterface()); | |||
10209 | if (!IsSuperClass) | |||
10210 | return false; | |||
10211 | ||||
10212 | // If the LHS has protocol qualifiers, determine whether all of them are | |||
10213 | // satisfied by the RHS (i.e., the RHS has a superset of the protocols in the | |||
10214 | // LHS). | |||
10215 | if (LHS->getNumProtocols() > 0) { | |||
10216 | // OK if conversion of LHS to SuperClass results in narrowing of types | |||
10217 | // ; i.e., SuperClass may implement at least one of the protocols | |||
10218 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. | |||
10219 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. | |||
10220 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; | |||
10221 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); | |||
10222 | // Also, if RHS has explicit quelifiers, include them for comparing with LHS's | |||
10223 | // qualifiers. | |||
10224 | for (auto *RHSPI : RHS->quals()) | |||
10225 | CollectInheritedProtocols(RHSPI, SuperClassInheritedProtocols); | |||
10226 | // If there is no protocols associated with RHS, it is not a match. | |||
10227 | if (SuperClassInheritedProtocols.empty()) | |||
10228 | return false; | |||
10229 | ||||
10230 | for (const auto *LHSProto : LHS->quals()) { | |||
10231 | bool SuperImplementsProtocol = false; | |||
10232 | for (auto *SuperClassProto : SuperClassInheritedProtocols) | |||
10233 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { | |||
10234 | SuperImplementsProtocol = true; | |||
10235 | break; | |||
10236 | } | |||
10237 | if (!SuperImplementsProtocol) | |||
10238 | return false; | |||
10239 | } | |||
10240 | } | |||
10241 | ||||
10242 | // If the LHS is specialized, we may need to check type arguments. | |||
10243 | if (LHS->isSpecialized()) { | |||
10244 | // Follow the superclass chain until we've matched the LHS class in the | |||
10245 | // hierarchy. This substitutes type arguments through. | |||
10246 | const ObjCObjectType *RHSSuper = RHS; | |||
10247 | while (!declaresSameEntity(RHSSuper->getInterface(), LHSInterface)) | |||
10248 | RHSSuper = RHSSuper->getSuperClassType()->castAs<ObjCObjectType>(); | |||
10249 | ||||
10250 | // If the RHS is specializd, compare type arguments. | |||
10251 | if (RHSSuper->isSpecialized() && | |||
10252 | !sameObjCTypeArgs(*this, LHS->getInterface(), | |||
10253 | LHS->getTypeArgs(), RHSSuper->getTypeArgs(), | |||
10254 | /*stripKindOf=*/true)) { | |||
10255 | return false; | |||
10256 | } | |||
10257 | } | |||
10258 | ||||
10259 | return true; | |||
10260 | } | |||
10261 | ||||
10262 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { | |||
10263 | // get the "pointed to" types | |||
10264 | const auto *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); | |||
10265 | const auto *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); | |||
10266 | ||||
10267 | if (!LHSOPT || !RHSOPT) | |||
10268 | return false; | |||
10269 | ||||
10270 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || | |||
10271 | canAssignObjCInterfaces(RHSOPT, LHSOPT); | |||
10272 | } | |||
10273 | ||||
10274 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { | |||
10275 | return canAssignObjCInterfaces( | |||
10276 | getObjCObjectPointerType(To)->castAs<ObjCObjectPointerType>(), | |||
10277 | getObjCObjectPointerType(From)->castAs<ObjCObjectPointerType>()); | |||
10278 | } | |||
10279 | ||||
10280 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, | |||
10281 | /// both shall have the identically qualified version of a compatible type. | |||
10282 | /// C99 6.2.7p1: Two types have compatible types if their types are the | |||
10283 | /// same. See 6.7.[2,3,5] for additional rules. | |||
10284 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, | |||
10285 | bool CompareUnqualified) { | |||
10286 | if (getLangOpts().CPlusPlus) | |||
10287 | return hasSameType(LHS, RHS); | |||
10288 | ||||
10289 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); | |||
10290 | } | |||
10291 | ||||
10292 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { | |||
10293 | return typesAreCompatible(LHS, RHS); | |||
10294 | } | |||
10295 | ||||
10296 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { | |||
10297 | return !mergeTypes(LHS, RHS, true).isNull(); | |||
10298 | } | |||
10299 | ||||
10300 | /// mergeTransparentUnionType - if T is a transparent union type and a member | |||
10301 | /// of T is compatible with SubType, return the merged type, else return | |||
10302 | /// QualType() | |||
10303 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, | |||
10304 | bool OfBlockPointer, | |||
10305 | bool Unqualified) { | |||
10306 | if (const RecordType *UT = T->getAsUnionType()) { | |||
10307 | RecordDecl *UD = UT->getDecl(); | |||
10308 | if (UD->hasAttr<TransparentUnionAttr>()) { | |||
10309 | for (const auto *I : UD->fields()) { | |||
10310 | QualType ET = I->getType().getUnqualifiedType(); | |||
10311 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); | |||
10312 | if (!MT.isNull()) | |||
10313 | return MT; | |||
10314 | } | |||
10315 | } | |||
10316 | } | |||
10317 | ||||
10318 | return {}; | |||
10319 | } | |||
10320 | ||||
10321 | /// mergeFunctionParameterTypes - merge two types which appear as function | |||
10322 | /// parameter types | |||
10323 | QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs, | |||
10324 | bool OfBlockPointer, | |||
10325 | bool Unqualified) { | |||
10326 | // GNU extension: two types are compatible if they appear as a function | |||
10327 | // argument, one of the types is a transparent union type and the other | |||
10328 | // type is compatible with a union member | |||
10329 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, | |||
10330 | Unqualified); | |||
10331 | if (!lmerge.isNull()) | |||
10332 | return lmerge; | |||
10333 | ||||
10334 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, | |||
10335 | Unqualified); | |||
10336 | if (!rmerge.isNull()) | |||
10337 | return rmerge; | |||
10338 | ||||
10339 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); | |||
10340 | } | |||
10341 | ||||
10342 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, | |||
10343 | bool OfBlockPointer, bool Unqualified, | |||
10344 | bool AllowCXX, | |||
10345 | bool IsConditionalOperator) { | |||
10346 | const auto *lbase = lhs->castAs<FunctionType>(); | |||
10347 | const auto *rbase = rhs->castAs<FunctionType>(); | |||
10348 | const auto *lproto = dyn_cast<FunctionProtoType>(lbase); | |||
10349 | const auto *rproto = dyn_cast<FunctionProtoType>(rbase); | |||
10350 | bool allLTypes = true; | |||
10351 | bool allRTypes = true; | |||
10352 | ||||
10353 | // Check return type | |||
10354 | QualType retType; | |||
10355 | if (OfBlockPointer) { | |||
10356 | QualType RHS = rbase->getReturnType(); | |||
10357 | QualType LHS = lbase->getReturnType(); | |||
10358 | bool UnqualifiedResult = Unqualified; | |||
10359 | if (!UnqualifiedResult) | |||
10360 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); | |||
10361 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); | |||
10362 | } | |||
10363 | else | |||
10364 | retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), false, | |||
10365 | Unqualified); | |||
10366 | if (retType.isNull()) | |||
10367 | return {}; | |||
10368 | ||||
10369 | if (Unqualified) | |||
10370 | retType = retType.getUnqualifiedType(); | |||
10371 | ||||
10372 | CanQualType LRetType = getCanonicalType(lbase->getReturnType()); | |||
10373 | CanQualType RRetType = getCanonicalType(rbase->getReturnType()); | |||
10374 | if (Unqualified) { | |||
10375 | LRetType = LRetType.getUnqualifiedType(); | |||
10376 | RRetType = RRetType.getUnqualifiedType(); | |||
10377 | } | |||
10378 | ||||
10379 | if (getCanonicalType(retType) != LRetType) | |||
10380 | allLTypes = false; | |||
10381 | if (getCanonicalType(retType) != RRetType) | |||
10382 | allRTypes = false; | |||
10383 | ||||
10384 | // FIXME: double check this | |||
10385 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && | |||
10386 | // rbase->getRegParmAttr() != 0 && | |||
10387 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? | |||
10388 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); | |||
10389 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); | |||
10390 | ||||
10391 | // Compatible functions must have compatible calling conventions | |||
10392 | if (lbaseInfo.getCC() != rbaseInfo.getCC()) | |||
10393 | return {}; | |||
10394 | ||||
10395 | // Regparm is part of the calling convention. | |||
10396 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) | |||
10397 | return {}; | |||
10398 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) | |||
10399 | return {}; | |||
10400 | ||||
10401 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) | |||
10402 | return {}; | |||
10403 | if (lbaseInfo.getNoCallerSavedRegs() != rbaseInfo.getNoCallerSavedRegs()) | |||
10404 | return {}; | |||
10405 | if (lbaseInfo.getNoCfCheck() != rbaseInfo.getNoCfCheck()) | |||
10406 | return {}; | |||
10407 | ||||
10408 | // When merging declarations, it's common for supplemental information like | |||
10409 | // attributes to only be present in one of the declarations, and we generally | |||
10410 | // want type merging to preserve the union of information. So a merged | |||
10411 | // function type should be noreturn if it was noreturn in *either* operand | |||
10412 | // type. | |||
10413 | // | |||
10414 | // But for the conditional operator, this is backwards. The result of the | |||
10415 | // operator could be either operand, and its type should conservatively | |||
10416 | // reflect that. So a function type in a composite type is noreturn only | |||
10417 | // if it's noreturn in *both* operand types. | |||
10418 | // | |||
10419 | // Arguably, noreturn is a kind of subtype, and the conditional operator | |||
10420 | // ought to produce the most specific common supertype of its operand types. | |||
10421 | // That would differ from this rule in contravariant positions. However, | |||
10422 | // neither C nor C++ generally uses this kind of subtype reasoning. Also, | |||
10423 | // as a practical matter, it would only affect C code that does abstraction of | |||
10424 | // higher-order functions (taking noreturn callbacks!), which is uncommon to | |||
10425 | // say the least. So we use the simpler rule. | |||
10426 | bool NoReturn = IsConditionalOperator | |||
10427 | ? lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn() | |||
10428 | : lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); | |||
10429 | if (lbaseInfo.getNoReturn() != NoReturn) | |||
10430 | allLTypes = false; | |||
10431 | if (rbaseInfo.getNoReturn() != NoReturn) | |||
10432 | allRTypes = false; | |||
10433 | ||||
10434 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); | |||
10435 | ||||
10436 | if (lproto && rproto) { // two C99 style function prototypes | |||
10437 | assert((AllowCXX ||(static_cast <bool> ((AllowCXX || (!lproto->hasExceptionSpec () && !rproto->hasExceptionSpec())) && "C++ shouldn't be here" ) ? void (0) : __assert_fail ("(AllowCXX || (!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec())) && \"C++ shouldn't be here\"" , "clang/lib/AST/ASTContext.cpp", 10439, __extension__ __PRETTY_FUNCTION__ )) | |||
10438 | (!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec())) &&(static_cast <bool> ((AllowCXX || (!lproto->hasExceptionSpec () && !rproto->hasExceptionSpec())) && "C++ shouldn't be here" ) ? void (0) : __assert_fail ("(AllowCXX || (!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec())) && \"C++ shouldn't be here\"" , "clang/lib/AST/ASTContext.cpp", 10439, __extension__ __PRETTY_FUNCTION__ )) | |||
10439 | "C++ shouldn't be here")(static_cast <bool> ((AllowCXX || (!lproto->hasExceptionSpec () && !rproto->hasExceptionSpec())) && "C++ shouldn't be here" ) ? void (0) : __assert_fail ("(AllowCXX || (!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec())) && \"C++ shouldn't be here\"" , "clang/lib/AST/ASTContext.cpp", 10439, __extension__ __PRETTY_FUNCTION__ )); | |||
10440 | // Compatible functions must have the same number of parameters | |||
10441 | if (lproto->getNumParams() != rproto->getNumParams()) | |||
10442 | return {}; | |||
10443 | ||||
10444 | // Variadic and non-variadic functions aren't compatible | |||
10445 | if (lproto->isVariadic() != rproto->isVariadic()) | |||
10446 | return {}; | |||
10447 | ||||
10448 | if (lproto->getMethodQuals() != rproto->getMethodQuals()) | |||
10449 | return {}; | |||
10450 | ||||
10451 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> newParamInfos; | |||
10452 | bool canUseLeft, canUseRight; | |||
10453 | if (!mergeExtParameterInfo(lproto, rproto, canUseLeft, canUseRight, | |||
10454 | newParamInfos)) | |||
10455 | return {}; | |||
10456 | ||||
10457 | if (!canUseLeft) | |||
10458 | allLTypes = false; | |||
10459 | if (!canUseRight) | |||
10460 | allRTypes = false; | |||
10461 | ||||
10462 | // Check parameter type compatibility | |||
10463 | SmallVector<QualType, 10> types; | |||
10464 | for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) { | |||
10465 | QualType lParamType = lproto->getParamType(i).getUnqualifiedType(); | |||
10466 | QualType rParamType = rproto->getParamType(i).getUnqualifiedType(); | |||
10467 | QualType paramType = mergeFunctionParameterTypes( | |||
10468 | lParamType, rParamType, OfBlockPointer, Unqualified); | |||
10469 | if (paramType.isNull()) | |||
10470 | return {}; | |||
10471 | ||||
10472 | if (Unqualified) | |||
10473 | paramType = paramType.getUnqualifiedType(); | |||
10474 | ||||
10475 | types.push_back(paramType); | |||
10476 | if (Unqualified) { | |||
10477 | lParamType = lParamType.getUnqualifiedType(); | |||
10478 | rParamType = rParamType.getUnqualifiedType(); | |||
10479 | } | |||
10480 | ||||
10481 | if (getCanonicalType(paramType) != getCanonicalType(lParamType)) | |||
10482 | allLTypes = false; | |||
10483 | if (getCanonicalType(paramType) != getCanonicalType(rParamType)) | |||
10484 | allRTypes = false; | |||
10485 | } | |||
10486 | ||||
10487 | if (allLTypes) return lhs; | |||
10488 | if (allRTypes) return rhs; | |||
10489 | ||||
10490 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); | |||
10491 | EPI.ExtInfo = einfo; | |||
10492 | EPI.ExtParameterInfos = | |||
10493 | newParamInfos.empty() ? nullptr : newParamInfos.data(); | |||
10494 | return getFunctionType(retType, types, EPI); | |||
10495 | } | |||
10496 | ||||
10497 | if (lproto) allRTypes = false; | |||
10498 | if (rproto) allLTypes = false; | |||
10499 | ||||
10500 | const FunctionProtoType *proto = lproto ? lproto : rproto; | |||
10501 | if (proto) { | |||
10502 | assert((AllowCXX || !proto->hasExceptionSpec()) && "C++ shouldn't be here")(static_cast <bool> ((AllowCXX || !proto->hasExceptionSpec ()) && "C++ shouldn't be here") ? void (0) : __assert_fail ("(AllowCXX || !proto->hasExceptionSpec()) && \"C++ shouldn't be here\"" , "clang/lib/AST/ASTContext.cpp", 10502, __extension__ __PRETTY_FUNCTION__ )); | |||
10503 | if (proto->isVariadic()) | |||
10504 | return {}; | |||
10505 | // Check that the types are compatible with the types that | |||
10506 | // would result from default argument promotions (C99 6.7.5.3p15). | |||
10507 | // The only types actually affected are promotable integer | |||
10508 | // types and floats, which would be passed as a different | |||
10509 | // type depending on whether the prototype is visible. | |||
10510 | for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) { | |||
10511 | QualType paramTy = proto->getParamType(i); | |||
10512 | ||||
10513 | // Look at the converted type of enum types, since that is the type used | |||
10514 | // to pass enum values. | |||
10515 | if (const auto *Enum = paramTy->getAs<EnumType>()) { | |||
10516 | paramTy = Enum->getDecl()->getIntegerType(); | |||
10517 | if (paramTy.isNull()) | |||
10518 | return {}; | |||
10519 | } | |||
10520 | ||||
10521 | if (isPromotableIntegerType(paramTy) || | |||
10522 | getCanonicalType(paramTy).getUnqualifiedType() == FloatTy) | |||
10523 | return {}; | |||
10524 | } | |||
10525 | ||||
10526 | if (allLTypes) return lhs; | |||
10527 | if (allRTypes) return rhs; | |||
10528 | ||||
10529 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); | |||
10530 | EPI.ExtInfo = einfo; | |||
10531 | return getFunctionType(retType, proto->getParamTypes(), EPI); | |||
10532 | } | |||
10533 | ||||
10534 | if (allLTypes) return lhs; | |||
10535 | if (allRTypes) return rhs; | |||
10536 | return getFunctionNoProtoType(retType, einfo); | |||
10537 | } | |||
10538 | ||||
10539 | /// Given that we have an enum type and a non-enum type, try to merge them. | |||
10540 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, | |||
10541 | QualType other, bool isBlockReturnType) { | |||
10542 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, | |||
10543 | // a signed integer type, or an unsigned integer type. | |||
10544 | // Compatibility is based on the underlying type, not the promotion | |||
10545 | // type. | |||
10546 | QualType underlyingType = ET->getDecl()->getIntegerType(); | |||
10547 | if (underlyingType.isNull()) | |||
10548 | return {}; | |||
10549 | if (Context.hasSameType(underlyingType, other)) | |||
10550 | return other; | |||
10551 | ||||
10552 | // In block return types, we're more permissive and accept any | |||
10553 | // integral type of the same size. | |||
10554 | if (isBlockReturnType && other->isIntegerType() && | |||
10555 | Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) | |||
10556 | return other; | |||
10557 | ||||
10558 | return {}; | |||
10559 | } | |||
10560 | ||||
10561 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, bool OfBlockPointer, | |||
10562 | bool Unqualified, bool BlockReturnType, | |||
10563 | bool IsConditionalOperator) { | |||
10564 | // For C++ we will not reach this code with reference types (see below), | |||
10565 | // for OpenMP variant call overloading we might. | |||
10566 | // | |||
10567 | // C++ [expr]: If an expression initially has the type "reference to T", the | |||
10568 | // type is adjusted to "T" prior to any further analysis, the expression | |||
10569 | // designates the object or function denoted by the reference, and the | |||
10570 | // expression is an lvalue unless the reference is an rvalue reference and | |||
10571 | // the expression is a function call (possibly inside parentheses). | |||
10572 | auto *LHSRefTy = LHS->getAs<ReferenceType>(); | |||
10573 | auto *RHSRefTy = RHS->getAs<ReferenceType>(); | |||
10574 | if (LangOpts.OpenMP && LHSRefTy && RHSRefTy && | |||
10575 | LHS->getTypeClass() == RHS->getTypeClass()) | |||
10576 | return mergeTypes(LHSRefTy->getPointeeType(), RHSRefTy->getPointeeType(), | |||
10577 | OfBlockPointer, Unqualified, BlockReturnType); | |||
10578 | if (LHSRefTy || RHSRefTy) | |||
10579 | return {}; | |||
10580 | ||||
10581 | if (Unqualified) { | |||
10582 | LHS = LHS.getUnqualifiedType(); | |||
10583 | RHS = RHS.getUnqualifiedType(); | |||
10584 | } | |||
10585 | ||||
10586 | QualType LHSCan = getCanonicalType(LHS), | |||
10587 | RHSCan = getCanonicalType(RHS); | |||
10588 | ||||
10589 | // If two types are identical, they are compatible. | |||
10590 | if (LHSCan == RHSCan) | |||
10591 | return LHS; | |||
10592 | ||||
10593 | // If the qualifiers are different, the types aren't compatible... mostly. | |||
10594 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); | |||
10595 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); | |||
10596 | if (LQuals != RQuals) { | |||
10597 | // If any of these qualifiers are different, we have a type | |||
10598 | // mismatch. | |||
10599 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || | |||
10600 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || | |||
10601 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime() || | |||
10602 | LQuals.hasUnaligned() != RQuals.hasUnaligned()) | |||
10603 | return {}; | |||
10604 | ||||
10605 | // Exactly one GC qualifier difference is allowed: __strong is | |||
10606 | // okay if the other type has no GC qualifier but is an Objective | |||
10607 | // C object pointer (i.e. implicitly strong by default). We fix | |||
10608 | // this by pretending that the unqualified type was actually | |||
10609 | // qualified __strong. | |||
10610 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); | |||
10611 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); | |||
10612 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements")(static_cast <bool> ((GC_L != GC_R) && "unequal qualifier sets had only equal elements" ) ? void (0) : __assert_fail ("(GC_L != GC_R) && \"unequal qualifier sets had only equal elements\"" , "clang/lib/AST/ASTContext.cpp", 10612, __extension__ __PRETTY_FUNCTION__ )); | |||
10613 | ||||
10614 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) | |||
10615 | return {}; | |||
10616 | ||||
10617 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { | |||
10618 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); | |||
10619 | } | |||
10620 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { | |||
10621 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); | |||
10622 | } | |||
10623 | return {}; | |||
10624 | } | |||
10625 | ||||
10626 | // Okay, qualifiers are equal. | |||
10627 | ||||
10628 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); | |||
10629 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); | |||
10630 | ||||
10631 | // We want to consider the two function types to be the same for these | |||
10632 | // comparisons, just force one to the other. | |||
10633 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; | |||
10634 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; | |||
10635 | ||||
10636 | // Same as above for arrays | |||
10637 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) | |||
10638 | LHSClass = Type::ConstantArray; | |||
10639 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) | |||
10640 | RHSClass = Type::ConstantArray; | |||
10641 | ||||
10642 | // ObjCInterfaces are just specialized ObjCObjects. | |||
10643 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; | |||
10644 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; | |||
10645 | ||||
10646 | // Canonicalize ExtVector -> Vector. | |||
10647 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; | |||
10648 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; | |||
10649 | ||||
10650 | // If the canonical type classes don't match. | |||
10651 | if (LHSClass != RHSClass) { | |||
10652 | // Note that we only have special rules for turning block enum | |||
10653 | // returns into block int returns, not vice-versa. | |||
10654 | if (const auto *ETy = LHS->getAs<EnumType>()) { | |||
10655 | return mergeEnumWithInteger(*this, ETy, RHS, false); | |||
10656 | } | |||
10657 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { | |||
10658 | return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); | |||
10659 | } | |||
10660 | // allow block pointer type to match an 'id' type. | |||
10661 | if (OfBlockPointer && !BlockReturnType) { | |||
10662 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) | |||
10663 | return LHS; | |||
10664 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) | |||
10665 | return RHS; | |||
10666 | } | |||
10667 | // Allow __auto_type to match anything; it merges to the type with more | |||
10668 | // information. | |||
10669 | if (const auto *AT = LHS->getAs<AutoType>()) { | |||
10670 | if (!AT->isDeduced() && AT->isGNUAutoType()) | |||
10671 | return RHS; | |||
10672 | } | |||
10673 | if (const auto *AT = RHS->getAs<AutoType>()) { | |||
10674 | if (!AT->isDeduced() && AT->isGNUAutoType()) | |||
10675 | return LHS; | |||
10676 | } | |||
10677 | return {}; | |||
10678 | } | |||
10679 | ||||
10680 | // The canonical type classes match. | |||
10681 | switch (LHSClass) { | |||
10682 | #define TYPE(Class, Base) | |||
10683 | #define ABSTRACT_TYPE(Class, Base) | |||
10684 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: | |||
10685 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: | |||
10686 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: | |||
10687 | #include "clang/AST/TypeNodes.inc" | |||
10688 | llvm_unreachable("Non-canonical and dependent types shouldn't get here")::llvm::llvm_unreachable_internal("Non-canonical and dependent types shouldn't get here" , "clang/lib/AST/ASTContext.cpp", 10688); | |||
10689 | ||||
10690 | case Type::Auto: | |||
10691 | case Type::DeducedTemplateSpecialization: | |||
10692 | case Type::LValueReference: | |||
10693 | case Type::RValueReference: | |||
10694 | case Type::MemberPointer: | |||
10695 | llvm_unreachable("C++ should never be in mergeTypes")::llvm::llvm_unreachable_internal("C++ should never be in mergeTypes" , "clang/lib/AST/ASTContext.cpp", 10695); | |||
10696 | ||||
10697 | case Type::ObjCInterface: | |||
10698 | case Type::IncompleteArray: | |||
10699 | case Type::VariableArray: | |||
10700 | case Type::FunctionProto: | |||
10701 | case Type::ExtVector: | |||
10702 | llvm_unreachable("Types are eliminated above")::llvm::llvm_unreachable_internal("Types are eliminated above" , "clang/lib/AST/ASTContext.cpp", 10702); | |||
10703 | ||||
10704 | case Type::Pointer: | |||
10705 | { | |||
10706 | // Merge two pointer types, while trying to preserve typedef info | |||
10707 | QualType LHSPointee = LHS->castAs<PointerType>()->getPointeeType(); | |||
10708 | QualType RHSPointee = RHS->castAs<PointerType>()->getPointeeType(); | |||
10709 | if (Unqualified) { | |||
10710 | LHSPointee = LHSPointee.getUnqualifiedType(); | |||
10711 | RHSPointee = RHSPointee.getUnqualifiedType(); | |||
10712 | } | |||
10713 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, | |||
10714 | Unqualified); | |||
10715 | if (ResultType.isNull()) | |||
10716 | return {}; | |||
10717 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) | |||
10718 | return LHS; | |||
10719 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) | |||
10720 | return RHS; | |||
10721 | return getPointerType(ResultType); | |||
10722 | } | |||
10723 | case Type::BlockPointer: | |||
10724 | { | |||
10725 | // Merge two block pointer types, while trying to preserve typedef info | |||
10726 | QualType LHSPointee = LHS->castAs<BlockPointerType>()->getPointeeType(); | |||
10727 | QualType RHSPointee = RHS->castAs<BlockPointerType>()->getPointeeType(); | |||
10728 | if (Unqualified) { | |||
10729 | LHSPointee = LHSPointee.getUnqualifiedType(); | |||
10730 | RHSPointee = RHSPointee.getUnqualifiedType(); | |||
10731 | } | |||
10732 | if (getLangOpts().OpenCL) { | |||
10733 | Qualifiers LHSPteeQual = LHSPointee.getQualifiers(); | |||
10734 | Qualifiers RHSPteeQual = RHSPointee.getQualifiers(); | |||
10735 | // Blocks can't be an expression in a ternary operator (OpenCL v2.0 | |||
10736 | // 6.12.5) thus the following check is asymmetric. | |||
10737 | if (!LHSPteeQual.isAddressSpaceSupersetOf(RHSPteeQual)) | |||
10738 | return {}; | |||
10739 | LHSPteeQual.removeAddressSpace(); | |||
10740 | RHSPteeQual.removeAddressSpace(); | |||
10741 | LHSPointee = | |||
10742 | QualType(LHSPointee.getTypePtr(), LHSPteeQual.getAsOpaqueValue()); | |||
10743 | RHSPointee = | |||
10744 | QualType(RHSPointee.getTypePtr(), RHSPteeQual.getAsOpaqueValue()); | |||
10745 | } | |||
10746 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, | |||
10747 | Unqualified); | |||
10748 | if (ResultType.isNull()) | |||
10749 | return {}; | |||
10750 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) | |||
10751 | return LHS; | |||
10752 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) | |||
10753 | return RHS; | |||
10754 | return getBlockPointerType(ResultType); | |||
10755 | } | |||
10756 | case Type::Atomic: | |||
10757 | { | |||
10758 | // Merge two pointer types, while trying to preserve typedef info | |||
10759 | QualType LHSValue = LHS->castAs<AtomicType>()->getValueType(); | |||
10760 | QualType RHSValue = RHS->castAs<AtomicType>()->getValueType(); | |||
10761 | if (Unqualified) { | |||
10762 | LHSValue = LHSValue.getUnqualifiedType(); | |||
10763 | RHSValue = RHSValue.getUnqualifiedType(); | |||
10764 | } | |||
10765 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, | |||
10766 | Unqualified); | |||
10767 | if (ResultType.isNull()) | |||
10768 | return {}; | |||
10769 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) | |||
10770 | return LHS; | |||
10771 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) | |||
10772 | return RHS; | |||
10773 | return getAtomicType(ResultType); | |||
10774 | } | |||
10775 | case Type::ConstantArray: | |||
10776 | { | |||
10777 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); | |||
10778 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); | |||
10779 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) | |||
10780 | return {}; | |||
10781 | ||||
10782 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); | |||
10783 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); | |||
10784 | if (Unqualified) { | |||
10785 | LHSElem = LHSElem.getUnqualifiedType(); | |||
10786 | RHSElem = RHSElem.getUnqualifiedType(); | |||
10787 | } | |||
10788 | ||||
10789 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); | |||
10790 | if (ResultType.isNull()) | |||
10791 | return {}; | |||
10792 | ||||
10793 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); | |||
10794 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); | |||
10795 | ||||
10796 | // If either side is a variable array, and both are complete, check whether | |||
10797 | // the current dimension is definite. | |||
10798 | if (LVAT || RVAT) { | |||
10799 | auto SizeFetch = [this](const VariableArrayType* VAT, | |||
10800 | const ConstantArrayType* CAT) | |||
10801 | -> std::pair<bool,llvm::APInt> { | |||
10802 | if (VAT) { | |||
10803 | std::optional<llvm::APSInt> TheInt; | |||
10804 | Expr *E = VAT->getSizeExpr(); | |||
10805 | if (E && (TheInt = E->getIntegerConstantExpr(*this))) | |||
10806 | return std::make_pair(true, *TheInt); | |||
10807 | return std::make_pair(false, llvm::APSInt()); | |||
10808 | } | |||
10809 | if (CAT) | |||
10810 | return std::make_pair(true, CAT->getSize()); | |||
10811 | return std::make_pair(false, llvm::APInt()); | |||
10812 | }; | |||
10813 | ||||
10814 | bool HaveLSize, HaveRSize; | |||
10815 | llvm::APInt LSize, RSize; | |||
10816 | std::tie(HaveLSize, LSize) = SizeFetch(LVAT, LCAT); | |||
10817 | std::tie(HaveRSize, RSize) = SizeFetch(RVAT, RCAT); | |||
10818 | if (HaveLSize && HaveRSize && !llvm::APInt::isSameValue(LSize, RSize)) | |||
10819 | return {}; // Definite, but unequal, array dimension | |||
10820 | } | |||
10821 | ||||
10822 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) | |||
10823 | return LHS; | |||
10824 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) | |||
10825 | return RHS; | |||
10826 | if (LCAT) | |||
10827 | return getConstantArrayType(ResultType, LCAT->getSize(), | |||
10828 | LCAT->getSizeExpr(), | |||
10829 | ArrayType::ArraySizeModifier(), 0); | |||
10830 | if (RCAT) | |||
10831 | return getConstantArrayType(ResultType, RCAT->getSize(), | |||
10832 | RCAT->getSizeExpr(), | |||
10833 | ArrayType::ArraySizeModifier(), 0); | |||
10834 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) | |||
10835 | return LHS; | |||
10836 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) | |||
10837 | return RHS; | |||
10838 | if (LVAT) { | |||
10839 | // FIXME: This isn't correct! But tricky to implement because | |||
10840 | // the array's size has to be the size of LHS, but the type | |||
10841 | // has to be different. | |||
10842 | return LHS; | |||
10843 | } | |||
10844 | if (RVAT) { | |||
10845 | // FIXME: This isn't correct! But tricky to implement because | |||
10846 | // the array's size has to be the size of RHS, but the type | |||
10847 | // has to be different. | |||
10848 | return RHS; | |||
10849 | } | |||
10850 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; | |||
10851 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; | |||
10852 | return getIncompleteArrayType(ResultType, | |||
10853 | ArrayType::ArraySizeModifier(), 0); | |||
10854 | } | |||
10855 | case Type::FunctionNoProto: | |||
10856 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified, | |||
10857 | /*AllowCXX=*/false, IsConditionalOperator); | |||
10858 | case Type::Record: | |||
10859 | case Type::Enum: | |||
10860 | return {}; | |||
10861 | case Type::Builtin: | |||
10862 | // Only exactly equal builtin types are compatible, which is tested above. | |||
10863 | return {}; | |||
10864 | case Type::Complex: | |||
10865 | // Distinct complex types are incompatible. | |||
10866 | return {}; | |||
10867 | case Type::Vector: | |||
10868 | // FIXME: The merged type should be an ExtVector! | |||
10869 | if (areCompatVectorTypes(LHSCan->castAs<VectorType>(), | |||
10870 | RHSCan->castAs<VectorType>())) | |||
10871 | return LHS; | |||
10872 | return {}; | |||
10873 | case Type::ConstantMatrix: | |||
10874 | if (areCompatMatrixTypes(LHSCan->castAs<ConstantMatrixType>(), | |||
10875 | RHSCan->castAs<ConstantMatrixType>())) | |||
10876 | return LHS; | |||
10877 | return {}; | |||
10878 | case Type::ObjCObject: { | |||
10879 | // Check if the types are assignment compatible. | |||
10880 | // FIXME: This should be type compatibility, e.g. whether | |||
10881 | // "LHS x; RHS x;" at global scope is legal. | |||
10882 | if (canAssignObjCInterfaces(LHS->castAs<ObjCObjectType>(), | |||
10883 | RHS->castAs<ObjCObjectType>())) | |||
10884 | return LHS; | |||
10885 | return {}; | |||
10886 | } | |||
10887 | case Type::ObjCObjectPointer: | |||
10888 | if (OfBlockPointer) { | |||
10889 | if (canAssignObjCInterfacesInBlockPointer( | |||
10890 | LHS->castAs<ObjCObjectPointerType>(), | |||
10891 | RHS->castAs<ObjCObjectPointerType>(), BlockReturnType)) | |||
10892 | return LHS; | |||
10893 | return {}; | |||
10894 | } | |||
10895 | if (canAssignObjCInterfaces(LHS->castAs<ObjCObjectPointerType>(), | |||
10896 | RHS->castAs<ObjCObjectPointerType>())) | |||
10897 | return LHS; | |||
10898 | return {}; | |||
10899 | case Type::Pipe: | |||
10900 | assert(LHS != RHS &&(static_cast <bool> (LHS != RHS && "Equivalent pipe types should have already been handled!" ) ? void (0) : __assert_fail ("LHS != RHS && \"Equivalent pipe types should have already been handled!\"" , "clang/lib/AST/ASTContext.cpp", 10901, __extension__ __PRETTY_FUNCTION__ )) | |||
10901 | "Equivalent pipe types should have already been handled!")(static_cast <bool> (LHS != RHS && "Equivalent pipe types should have already been handled!" ) ? void (0) : __assert_fail ("LHS != RHS && \"Equivalent pipe types should have already been handled!\"" , "clang/lib/AST/ASTContext.cpp", 10901, __extension__ __PRETTY_FUNCTION__ )); | |||
10902 | return {}; | |||
10903 | case Type::BitInt: { | |||
10904 | // Merge two bit-precise int types, while trying to preserve typedef info. | |||
10905 | bool LHSUnsigned = LHS->castAs<BitIntType>()->isUnsigned(); | |||
10906 | bool RHSUnsigned = RHS->castAs<BitIntType>()->isUnsigned(); | |||
10907 | unsigned LHSBits = LHS->castAs<BitIntType>()->getNumBits(); | |||
10908 | unsigned RHSBits = RHS->castAs<BitIntType>()->getNumBits(); | |||
10909 | ||||
10910 | // Like unsigned/int, shouldn't have a type if they don't match. | |||
10911 | if (LHSUnsigned != RHSUnsigned) | |||
10912 | return {}; | |||
10913 | ||||
10914 | if (LHSBits != RHSBits) | |||
10915 | return {}; | |||
10916 | return LHS; | |||
10917 | } | |||
10918 | } | |||
10919 | ||||
10920 | llvm_unreachable("Invalid Type::Class!")::llvm::llvm_unreachable_internal("Invalid Type::Class!", "clang/lib/AST/ASTContext.cpp" , 10920); | |||
10921 | } | |||
10922 | ||||
10923 | bool ASTContext::mergeExtParameterInfo( | |||
10924 | const FunctionProtoType *FirstFnType, const FunctionProtoType *SecondFnType, | |||
10925 | bool &CanUseFirst, bool &CanUseSecond, | |||
10926 | SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &NewParamInfos) { | |||
10927 | assert(NewParamInfos.empty() && "param info list not empty")(static_cast <bool> (NewParamInfos.empty() && "param info list not empty" ) ? void (0) : __assert_fail ("NewParamInfos.empty() && \"param info list not empty\"" , "clang/lib/AST/ASTContext.cpp", 10927, __extension__ __PRETTY_FUNCTION__ )); | |||
10928 | CanUseFirst = CanUseSecond = true; | |||
10929 | bool FirstHasInfo = FirstFnType->hasExtParameterInfos(); | |||
10930 | bool SecondHasInfo = SecondFnType->hasExtParameterInfos(); | |||
10931 | ||||
10932 | // Fast path: if the first type doesn't have ext parameter infos, | |||
10933 | // we match if and only if the second type also doesn't have them. | |||
10934 | if (!FirstHasInfo && !SecondHasInfo) | |||
10935 | return true; | |||
10936 | ||||
10937 | bool NeedParamInfo = false; | |||
10938 | size_t E = FirstHasInfo ? FirstFnType->getExtParameterInfos().size() | |||
10939 | : SecondFnType->getExtParameterInfos().size(); | |||
10940 | ||||
10941 | for (size_t I = 0; I < E; ++I) { | |||
10942 | FunctionProtoType::ExtParameterInfo FirstParam, SecondParam; | |||
10943 | if (FirstHasInfo) | |||
10944 | FirstParam = FirstFnType->getExtParameterInfo(I); | |||
10945 | if (SecondHasInfo) | |||
10946 | SecondParam = SecondFnType->getExtParameterInfo(I); | |||
10947 | ||||
10948 | // Cannot merge unless everything except the noescape flag matches. | |||
10949 | if (FirstParam.withIsNoEscape(false) != SecondParam.withIsNoEscape(false)) | |||
10950 | return false; | |||
10951 | ||||
10952 | bool FirstNoEscape = FirstParam.isNoEscape(); | |||
10953 | bool SecondNoEscape = SecondParam.isNoEscape(); | |||
10954 | bool IsNoEscape = FirstNoEscape && SecondNoEscape; | |||
10955 | NewParamInfos.push_back(FirstParam.withIsNoEscape(IsNoEscape)); | |||
10956 | if (NewParamInfos.back().getOpaqueValue()) | |||
10957 | NeedParamInfo = true; | |||
10958 | if (FirstNoEscape != IsNoEscape) | |||
10959 | CanUseFirst = false; | |||
10960 | if (SecondNoEscape != IsNoEscape) | |||
10961 | CanUseSecond = false; | |||
10962 | } | |||
10963 | ||||
10964 | if (!NeedParamInfo) | |||
10965 | NewParamInfos.clear(); | |||
10966 | ||||
10967 | return true; | |||
10968 | } | |||
10969 | ||||
10970 | void ASTContext::ResetObjCLayout(const ObjCContainerDecl *CD) { | |||
10971 | ObjCLayouts[CD] = nullptr; | |||
10972 | } | |||
10973 | ||||
10974 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and | |||
10975 | /// 'RHS' attributes and returns the merged version; including for function | |||
10976 | /// return types. | |||
10977 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { | |||
10978 | QualType LHSCan = getCanonicalType(LHS), | |||
10979 | RHSCan = getCanonicalType(RHS); | |||
10980 | // If two types are identical, they are compatible. | |||
10981 | if (LHSCan == RHSCan) | |||
10982 | return LHS; | |||
10983 | if (RHSCan->isFunctionType()) { | |||
10984 | if (!LHSCan->isFunctionType()) | |||
10985 | return {}; | |||
10986 | QualType OldReturnType = | |||
10987 | cast<FunctionType>(RHSCan.getTypePtr())->getReturnType(); | |||
10988 | QualType NewReturnType = | |||
10989 | cast<FunctionType>(LHSCan.getTypePtr())->getReturnType(); | |||
10990 | QualType ResReturnType = | |||
10991 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); | |||
10992 | if (ResReturnType.isNull()) | |||
10993 | return {}; | |||
10994 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { | |||
10995 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); | |||
10996 | // In either case, use OldReturnType to build the new function type. | |||
10997 | const auto *F = LHS->castAs<FunctionType>(); | |||
10998 | if (const auto *FPT = cast<FunctionProtoType>(F)) { | |||
10999 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); | |||
11000 | EPI.ExtInfo = getFunctionExtInfo(LHS); | |||
11001 | QualType ResultType = | |||
11002 | getFunctionType(OldReturnType, FPT->getParamTypes(), EPI); | |||
11003 | return ResultType; | |||
11004 | } | |||
11005 | } | |||
11006 | return {}; | |||
11007 | } | |||
11008 | ||||
11009 | // If the qualifiers are different, the types can still be merged. | |||
11010 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); | |||
11011 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); | |||
11012 | if (LQuals != RQuals) { | |||
11013 | // If any of these qualifiers are different, we have a type mismatch. | |||
11014 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || | |||
11015 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) | |||
11016 | return {}; | |||
11017 | ||||
11018 | // Exactly one GC qualifier difference is allowed: __strong is | |||
11019 | // okay if the other type has no GC qualifier but is an Objective | |||
11020 | // C object pointer (i.e. implicitly strong by default). We fix | |||
11021 | // this by pretending that the unqualified type was actually | |||
11022 | // qualified __strong. | |||
11023 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); | |||
11024 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); | |||
11025 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements")(static_cast <bool> ((GC_L != GC_R) && "unequal qualifier sets had only equal elements" ) ? void (0) : __assert_fail ("(GC_L != GC_R) && \"unequal qualifier sets had only equal elements\"" , "clang/lib/AST/ASTContext.cpp", 11025, __extension__ __PRETTY_FUNCTION__ )); | |||
11026 | ||||
11027 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) | |||
11028 | return {}; | |||
11029 | ||||
11030 | if (GC_L == Qualifiers::Strong) | |||
11031 | return LHS; | |||
11032 | if (GC_R == Qualifiers::Strong) | |||
11033 | return RHS; | |||
11034 | return {}; | |||
11035 | } | |||
11036 | ||||
11037 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { | |||
11038 | QualType LHSBaseQT = LHS->castAs<ObjCObjectPointerType>()->getPointeeType(); | |||
11039 | QualType RHSBaseQT = RHS->castAs<ObjCObjectPointerType>()->getPointeeType(); | |||
11040 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); | |||
11041 | if (ResQT == LHSBaseQT) | |||
11042 | return LHS; | |||
11043 | if (ResQT == RHSBaseQT) | |||
11044 | return RHS; | |||
11045 | } | |||
11046 | return {}; | |||
11047 | } | |||
11048 | ||||
11049 | //===----------------------------------------------------------------------===// | |||
11050 | // Integer Predicates | |||
11051 | //===----------------------------------------------------------------------===// | |||
11052 | ||||
11053 | unsigned ASTContext::getIntWidth(QualType T) const { | |||
11054 | if (const auto *ET = T->getAs<EnumType>()) | |||
11055 | T = ET->getDecl()->getIntegerType(); | |||
11056 | if (T->isBooleanType()) | |||
11057 | return 1; | |||
11058 | if (const auto *EIT = T->getAs<BitIntType>()) | |||
11059 | return EIT->getNumBits(); | |||
11060 | // For builtin types, just use the standard type sizing method | |||
11061 | return (unsigned)getTypeSize(T); | |||
11062 | } | |||
11063 | ||||
11064 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { | |||
11065 | assert((T->hasIntegerRepresentation() || T->isEnumeralType() ||(static_cast <bool> ((T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && "Unexpected type") ? void (0) : __assert_fail ("(T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && \"Unexpected type\"" , "clang/lib/AST/ASTContext.cpp", 11067, __extension__ __PRETTY_FUNCTION__ )) | |||
11066 | T->isFixedPointType()) &&(static_cast <bool> ((T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && "Unexpected type") ? void (0) : __assert_fail ("(T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && \"Unexpected type\"" , "clang/lib/AST/ASTContext.cpp", 11067, __extension__ __PRETTY_FUNCTION__ )) | |||
11067 | "Unexpected type")(static_cast <bool> ((T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && "Unexpected type") ? void (0) : __assert_fail ("(T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && \"Unexpected type\"" , "clang/lib/AST/ASTContext.cpp", 11067, __extension__ __PRETTY_FUNCTION__ )); | |||
11068 | ||||
11069 | // Turn <4 x signed int> -> <4 x unsigned int> | |||
11070 | if (const auto *VTy = T->getAs<VectorType>()) | |||
11071 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), | |||
11072 | VTy->getNumElements(), VTy->getVectorKind()); | |||
11073 | ||||
11074 | // For _BitInt, return an unsigned _BitInt with same width. | |||
11075 | if (const auto *EITy = T->getAs<BitIntType>()) | |||
11076 | return getBitIntType(/*Unsigned=*/true, EITy->getNumBits()); | |||
11077 | ||||
11078 | // For enums, get the underlying integer type of the enum, and let the general | |||
11079 | // integer type signchanging code handle it. | |||
11080 | if (const auto *ETy = T->getAs<EnumType>()) | |||
11081 | T = ETy->getDecl()->getIntegerType(); | |||
11082 | ||||
11083 | switch (T->castAs<BuiltinType>()->getKind()) { | |||
11084 | case BuiltinType::Char_U: | |||
11085 | // Plain `char` is mapped to `unsigned char` even if it's already unsigned | |||
11086 | case BuiltinType::Char_S: | |||
11087 | case BuiltinType::SChar: | |||
11088 | case BuiltinType::Char8: | |||
11089 | return UnsignedCharTy; | |||
11090 | case BuiltinType::Short: | |||
11091 | return UnsignedShortTy; | |||
11092 | case BuiltinType::Int: | |||
11093 | return UnsignedIntTy; | |||
11094 | case BuiltinType::Long: | |||
11095 | return UnsignedLongTy; | |||
11096 | case BuiltinType::LongLong: | |||
11097 | return UnsignedLongLongTy; | |||
11098 | case BuiltinType::Int128: | |||
11099 | return UnsignedInt128Ty; | |||
11100 | // wchar_t is special. It is either signed or not, but when it's signed, | |||
11101 | // there's no matching "unsigned wchar_t". Therefore we return the unsigned | |||
11102 | // version of its underlying type instead. | |||
11103 | case BuiltinType::WChar_S: | |||
11104 | return getUnsignedWCharType(); | |||
11105 | ||||
11106 | case BuiltinType::ShortAccum: | |||
11107 | return UnsignedShortAccumTy; | |||
11108 | case BuiltinType::Accum: | |||
11109 | return UnsignedAccumTy; | |||
11110 | case BuiltinType::LongAccum: | |||
11111 | return UnsignedLongAccumTy; | |||
11112 | case BuiltinType::SatShortAccum: | |||
11113 | return SatUnsignedShortAccumTy; | |||
11114 | case BuiltinType::SatAccum: | |||
11115 | return SatUnsignedAccumTy; | |||
11116 | case BuiltinType::SatLongAccum: | |||
11117 | return SatUnsignedLongAccumTy; | |||
11118 | case BuiltinType::ShortFract: | |||
11119 | return UnsignedShortFractTy; | |||
11120 | case BuiltinType::Fract: | |||
11121 | return UnsignedFractTy; | |||
11122 | case BuiltinType::LongFract: | |||
11123 | return UnsignedLongFractTy; | |||
11124 | case BuiltinType::SatShortFract: | |||
11125 | return SatUnsignedShortFractTy; | |||
11126 | case BuiltinType::SatFract: | |||
11127 | return SatUnsignedFractTy; | |||
11128 | case BuiltinType::SatLongFract: | |||
11129 | return SatUnsignedLongFractTy; | |||
11130 | default: | |||
11131 | assert((T->hasUnsignedIntegerRepresentation() ||(static_cast <bool> ((T->hasUnsignedIntegerRepresentation () || T->isUnsignedFixedPointType()) && "Unexpected signed integer or fixed point type" ) ? void (0) : __assert_fail ("(T->hasUnsignedIntegerRepresentation() || T->isUnsignedFixedPointType()) && \"Unexpected signed integer or fixed point type\"" , "clang/lib/AST/ASTContext.cpp", 11133, __extension__ __PRETTY_FUNCTION__ )) | |||
11132 | T->isUnsignedFixedPointType()) &&(static_cast <bool> ((T->hasUnsignedIntegerRepresentation () || T->isUnsignedFixedPointType()) && "Unexpected signed integer or fixed point type" ) ? void (0) : __assert_fail ("(T->hasUnsignedIntegerRepresentation() || T->isUnsignedFixedPointType()) && \"Unexpected signed integer or fixed point type\"" , "clang/lib/AST/ASTContext.cpp", 11133, __extension__ __PRETTY_FUNCTION__ )) | |||
11133 | "Unexpected signed integer or fixed point type")(static_cast <bool> ((T->hasUnsignedIntegerRepresentation () || T->isUnsignedFixedPointType()) && "Unexpected signed integer or fixed point type" ) ? void (0) : __assert_fail ("(T->hasUnsignedIntegerRepresentation() || T->isUnsignedFixedPointType()) && \"Unexpected signed integer or fixed point type\"" , "clang/lib/AST/ASTContext.cpp", 11133, __extension__ __PRETTY_FUNCTION__ )); | |||
11134 | return T; | |||
11135 | } | |||
11136 | } | |||
11137 | ||||
11138 | QualType ASTContext::getCorrespondingSignedType(QualType T) const { | |||
11139 | assert((T->hasIntegerRepresentation() || T->isEnumeralType() ||(static_cast <bool> ((T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && "Unexpected type") ? void (0) : __assert_fail ("(T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && \"Unexpected type\"" , "clang/lib/AST/ASTContext.cpp", 11141, __extension__ __PRETTY_FUNCTION__ )) | |||
11140 | T->isFixedPointType()) &&(static_cast <bool> ((T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && "Unexpected type") ? void (0) : __assert_fail ("(T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && \"Unexpected type\"" , "clang/lib/AST/ASTContext.cpp", 11141, __extension__ __PRETTY_FUNCTION__ )) | |||
11141 | "Unexpected type")(static_cast <bool> ((T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && "Unexpected type") ? void (0) : __assert_fail ("(T->hasIntegerRepresentation() || T->isEnumeralType() || T->isFixedPointType()) && \"Unexpected type\"" , "clang/lib/AST/ASTContext.cpp", 11141, __extension__ __PRETTY_FUNCTION__ )); | |||
11142 | ||||
11143 | // Turn <4 x unsigned int> -> <4 x signed int> | |||
11144 | if (const auto *VTy = T->getAs<VectorType>()) | |||
11145 | return getVectorType(getCorrespondingSignedType(VTy->getElementType()), | |||
11146 | VTy->getNumElements(), VTy->getVectorKind()); | |||
11147 | ||||
11148 | // For _BitInt, return a signed _BitInt with same width. | |||
11149 | if (const auto *EITy = T->getAs<BitIntType>()) | |||
11150 | return getBitIntType(/*Unsigned=*/false, EITy->getNumBits()); | |||
11151 | ||||
11152 | // For enums, get the underlying integer type of the enum, and let the general | |||
11153 | // integer type signchanging code handle it. | |||
11154 | if (const auto *ETy = T->getAs<EnumType>()) | |||
11155 | T = ETy->getDecl()->getIntegerType(); | |||
11156 | ||||
11157 | switch (T->castAs<BuiltinType>()->getKind()) { | |||
11158 | case BuiltinType::Char_S: | |||
11159 | // Plain `char` is mapped to `signed char` even if it's already signed | |||
11160 | case BuiltinType::Char_U: | |||
11161 | case BuiltinType::UChar: | |||
11162 | case BuiltinType::Char8: | |||
11163 | return SignedCharTy; | |||
11164 | case BuiltinType::UShort: | |||
11165 | return ShortTy; | |||
11166 | case BuiltinType::UInt: | |||
11167 | return IntTy; | |||
11168 | case BuiltinType::ULong: | |||
11169 | return LongTy; | |||
11170 | case BuiltinType::ULongLong: | |||
11171 | return LongLongTy; | |||
11172 | case BuiltinType::UInt128: | |||
11173 | return Int128Ty; | |||
11174 | // wchar_t is special. It is either unsigned or not, but when it's unsigned, | |||
11175 | // there's no matching "signed wchar_t". Therefore we return the signed | |||
11176 | // version of its underlying type instead. | |||
11177 | case BuiltinType::WChar_U: | |||
11178 | return getSignedWCharType(); | |||
11179 | ||||
11180 | case BuiltinType::UShortAccum: | |||
11181 | return ShortAccumTy; | |||
11182 | case BuiltinType::UAccum: | |||
11183 | return AccumTy; | |||
11184 | case BuiltinType::ULongAccum: | |||
11185 | return LongAccumTy; | |||
11186 | case BuiltinType::SatUShortAccum: | |||
11187 | return SatShortAccumTy; | |||
11188 | case BuiltinType::SatUAccum: | |||
11189 | return SatAccumTy; | |||
11190 | case BuiltinType::SatULongAccum: | |||
11191 | return SatLongAccumTy; | |||
11192 | case BuiltinType::UShortFract: | |||
11193 | return ShortFractTy; | |||
11194 | case BuiltinType::UFract: | |||
11195 | return FractTy; | |||
11196 | case BuiltinType::ULongFract: | |||
11197 | return LongFractTy; | |||
11198 | case BuiltinType::SatUShortFract: | |||
11199 | return SatShortFractTy; | |||
11200 | case BuiltinType::SatUFract: | |||
11201 | return SatFractTy; | |||
11202 | case BuiltinType::SatULongFract: | |||
11203 | return SatLongFractTy; | |||
11204 | default: | |||
11205 | assert((static_cast <bool> ((T->hasSignedIntegerRepresentation () || T->isSignedFixedPointType()) && "Unexpected signed integer or fixed point type" ) ? void (0) : __assert_fail ("(T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) && \"Unexpected signed integer or fixed point type\"" , "clang/lib/AST/ASTContext.cpp", 11207, __extension__ __PRETTY_FUNCTION__ )) | |||
11206 | (T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) &&(static_cast <bool> ((T->hasSignedIntegerRepresentation () || T->isSignedFixedPointType()) && "Unexpected signed integer or fixed point type" ) ? void (0) : __assert_fail ("(T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) && \"Unexpected signed integer or fixed point type\"" , "clang/lib/AST/ASTContext.cpp", 11207, __extension__ __PRETTY_FUNCTION__ )) | |||
11207 | "Unexpected signed integer or fixed point type")(static_cast <bool> ((T->hasSignedIntegerRepresentation () || T->isSignedFixedPointType()) && "Unexpected signed integer or fixed point type" ) ? void (0) : __assert_fail ("(T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) && \"Unexpected signed integer or fixed point type\"" , "clang/lib/AST/ASTContext.cpp", 11207, __extension__ __PRETTY_FUNCTION__ )); | |||
11208 | return T; | |||
11209 | } | |||
11210 | } | |||
11211 | ||||
11212 | ASTMutationListener::~ASTMutationListener() = default; | |||
11213 | ||||
11214 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, | |||
11215 | QualType ReturnType) {} | |||
11216 | ||||
11217 | //===----------------------------------------------------------------------===// | |||
11218 | // Builtin Type Computation | |||
11219 | //===----------------------------------------------------------------------===// | |||
11220 | ||||
11221 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the | |||
11222 | /// pointer over the consumed characters. This returns the resultant type. If | |||
11223 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic | |||
11224 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of | |||
11225 | /// a vector of "i*". | |||
11226 | /// | |||
11227 | /// RequiresICE is filled in on return to indicate whether the value is required | |||
11228 | /// to be an Integer Constant Expression. | |||
11229 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, | |||
11230 | ASTContext::GetBuiltinTypeError &Error, | |||
11231 | bool &RequiresICE, | |||
11232 | bool AllowTypeModifiers) { | |||
11233 | // Modifiers. | |||
11234 | int HowLong = 0; | |||
11235 | bool Signed = false, Unsigned = false; | |||
11236 | RequiresICE = false; | |||
11237 | ||||
11238 | // Read the prefixed modifiers first. | |||
11239 | bool Done = false; | |||
11240 | #ifndef NDEBUG | |||
11241 | bool IsSpecial = false; | |||
11242 | #endif | |||
11243 | while (!Done) { | |||
11244 | switch (*Str++) { | |||
11245 | default: Done = true; --Str; break; | |||
11246 | case 'I': | |||
11247 | RequiresICE = true; | |||
11248 | break; | |||
11249 | case 'S': | |||
11250 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!")(static_cast <bool> (!Unsigned && "Can't use both 'S' and 'U' modifiers!" ) ? void (0) : __assert_fail ("!Unsigned && \"Can't use both 'S' and 'U' modifiers!\"" , "clang/lib/AST/ASTContext.cpp", 11250, __extension__ __PRETTY_FUNCTION__ )); | |||
11251 | assert(!Signed && "Can't use 'S' modifier multiple times!")(static_cast <bool> (!Signed && "Can't use 'S' modifier multiple times!" ) ? void (0) : __assert_fail ("!Signed && \"Can't use 'S' modifier multiple times!\"" , "clang/lib/AST/ASTContext.cpp", 11251, __extension__ __PRETTY_FUNCTION__ )); | |||
11252 | Signed = true; | |||
11253 | break; | |||
11254 | case 'U': | |||
11255 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!")(static_cast <bool> (!Signed && "Can't use both 'S' and 'U' modifiers!" ) ? void (0) : __assert_fail ("!Signed && \"Can't use both 'S' and 'U' modifiers!\"" , "clang/lib/AST/ASTContext.cpp", 11255, __extension__ __PRETTY_FUNCTION__ )); | |||
11256 | assert(!Unsigned && "Can't use 'U' modifier multiple times!")(static_cast <bool> (!Unsigned && "Can't use 'U' modifier multiple times!" ) ? void (0) : __assert_fail ("!Unsigned && \"Can't use 'U' modifier multiple times!\"" , "clang/lib/AST/ASTContext.cpp", 11256, __extension__ __PRETTY_FUNCTION__ )); | |||
11257 | Unsigned = true; | |||
11258 | break; | |||
11259 | case 'L': | |||
11260 | assert(!IsSpecial && "Can't use 'L' with 'W', 'N', 'Z' or 'O' modifiers")(static_cast <bool> (!IsSpecial && "Can't use 'L' with 'W', 'N', 'Z' or 'O' modifiers" ) ? void (0) : __assert_fail ("!IsSpecial && \"Can't use 'L' with 'W', 'N', 'Z' or 'O' modifiers\"" , "clang/lib/AST/ASTContext.cpp", 11260, __extension__ __PRETTY_FUNCTION__ )); | |||
11261 | assert(HowLong <= 2 && "Can't have LLLL modifier")(static_cast <bool> (HowLong <= 2 && "Can't have LLLL modifier" ) ? void (0) : __assert_fail ("HowLong <= 2 && \"Can't have LLLL modifier\"" , "clang/lib/AST/ASTContext.cpp", 11261, __extension__ __PRETTY_FUNCTION__ )); | |||
11262 | ++HowLong; | |||
11263 | break; | |||
11264 | case 'N': | |||
11265 | // 'N' behaves like 'L' for all non LP64 targets and 'int' otherwise. | |||
11266 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!")(static_cast <bool> (!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!" ) ? void (0) : __assert_fail ("!IsSpecial && \"Can't use two 'N', 'W', 'Z' or 'O' modifiers!\"" , "clang/lib/AST/ASTContext.cpp", 11266, __extension__ __PRETTY_FUNCTION__ )); | |||
11267 | assert(HowLong == 0 && "Can't use both 'L' and 'N' modifiers!")(static_cast <bool> (HowLong == 0 && "Can't use both 'L' and 'N' modifiers!" ) ? void (0) : __assert_fail ("HowLong == 0 && \"Can't use both 'L' and 'N' modifiers!\"" , "clang/lib/AST/ASTContext.cpp", 11267, __extension__ __PRETTY_FUNCTION__ )); | |||
11268 | #ifndef NDEBUG | |||
11269 | IsSpecial = true; | |||
11270 | #endif | |||
11271 | if (Context.getTargetInfo().getLongWidth() == 32) | |||
11272 | ++HowLong; | |||
11273 | break; | |||
11274 | case 'W': | |||
11275 | // This modifier represents int64 type. | |||
11276 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!")(static_cast <bool> (!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!" ) ? void (0) : __assert_fail ("!IsSpecial && \"Can't use two 'N', 'W', 'Z' or 'O' modifiers!\"" , "clang/lib/AST/ASTContext.cpp", 11276, __extension__ __PRETTY_FUNCTION__ )); | |||
11277 | assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!")(static_cast <bool> (HowLong == 0 && "Can't use both 'L' and 'W' modifiers!" ) ? void (0) : __assert_fail ("HowLong == 0 && \"Can't use both 'L' and 'W' modifiers!\"" , "clang/lib/AST/ASTContext.cpp", 11277, __extension__ __PRETTY_FUNCTION__ )); | |||
11278 | #ifndef NDEBUG | |||
11279 | IsSpecial = true; | |||
11280 | #endif | |||
11281 | switch (Context.getTargetInfo().getInt64Type()) { | |||
11282 | default: | |||
11283 | llvm_unreachable("Unexpected integer type")::llvm::llvm_unreachable_internal("Unexpected integer type", "clang/lib/AST/ASTContext.cpp" , 11283); | |||
11284 | case TargetInfo::SignedLong: | |||
11285 | HowLong = 1; | |||
11286 | break; | |||
11287 | case TargetInfo::SignedLongLong: | |||
11288 | HowLong = 2; | |||
11289 | break; | |||
11290 | } | |||
11291 | break; | |||
11292 | case 'Z': | |||
11293 | // This modifier represents int32 type. | |||
11294 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!")(static_cast <bool> (!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!" ) ? void (0) : __assert_fail ("!IsSpecial && \"Can't use two 'N', 'W', 'Z' or 'O' modifiers!\"" , "clang/lib/AST/ASTContext.cpp", 11294, __extension__ __PRETTY_FUNCTION__ )); | |||
11295 | assert(HowLong == 0 && "Can't use both 'L' and 'Z' modifiers!")(static_cast <bool> (HowLong == 0 && "Can't use both 'L' and 'Z' modifiers!" ) ? void (0) : __assert_fail ("HowLong == 0 && \"Can't use both 'L' and 'Z' modifiers!\"" , "clang/lib/AST/ASTContext.cpp", 11295, __extension__ __PRETTY_FUNCTION__ )); | |||
11296 | #ifndef NDEBUG | |||
11297 | IsSpecial = true; | |||
11298 | #endif | |||
11299 | switch (Context.getTargetInfo().getIntTypeByWidth(32, true)) { | |||
11300 | default: | |||
11301 | llvm_unreachable("Unexpected integer type")::llvm::llvm_unreachable_internal("Unexpected integer type", "clang/lib/AST/ASTContext.cpp" , 11301); | |||
11302 | case TargetInfo::SignedInt: | |||
11303 | HowLong = 0; | |||
11304 | break; | |||
11305 | case TargetInfo::SignedLong: | |||
11306 | HowLong = 1; | |||
11307 | break; | |||
11308 | case TargetInfo::SignedLongLong: | |||
11309 | HowLong = 2; | |||
11310 | break; | |||
11311 | } | |||
11312 | break; | |||
11313 | case 'O': | |||
11314 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!")(static_cast <bool> (!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!" ) ? void (0) : __assert_fail ("!IsSpecial && \"Can't use two 'N', 'W', 'Z' or 'O' modifiers!\"" , "clang/lib/AST/ASTContext.cpp", 11314, __extension__ __PRETTY_FUNCTION__ )); | |||
11315 | assert(HowLong == 0 && "Can't use both 'L' and 'O' modifiers!")(static_cast <bool> (HowLong == 0 && "Can't use both 'L' and 'O' modifiers!" ) ? void (0) : __assert_fail ("HowLong == 0 && \"Can't use both 'L' and 'O' modifiers!\"" , "clang/lib/AST/ASTContext.cpp", 11315, __extension__ __PRETTY_FUNCTION__ )); | |||
11316 | #ifndef NDEBUG | |||
11317 | IsSpecial = true; | |||
11318 | #endif | |||
11319 | if (Context.getLangOpts().OpenCL) | |||
11320 | HowLong = 1; | |||
11321 | else | |||
11322 | HowLong = 2; | |||
11323 | break; | |||
11324 | } | |||
11325 | } | |||
11326 | ||||
11327 | QualType Type; | |||
11328 | ||||
11329 | // Read the base type. | |||
11330 | switch (*Str++) { | |||
11331 | default: llvm_unreachable("Unknown builtin type letter!")::llvm::llvm_unreachable_internal("Unknown builtin type letter!" , "clang/lib/AST/ASTContext.cpp", 11331); | |||
11332 | case 'x': | |||
11333 | assert(HowLong == 0 && !Signed && !Unsigned &&(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers used with 'x'!") ? void ( 0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'x'!\"" , "clang/lib/AST/ASTContext.cpp", 11334, __extension__ __PRETTY_FUNCTION__ )) | |||
11334 | "Bad modifiers used with 'x'!")(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers used with 'x'!") ? void ( 0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'x'!\"" , "clang/lib/AST/ASTContext.cpp", 11334, __extension__ __PRETTY_FUNCTION__ )); | |||
11335 | Type = Context.Float16Ty; | |||
11336 | break; | |||
11337 | case 'y': | |||
11338 | assert(HowLong == 0 && !Signed && !Unsigned &&(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers used with 'y'!") ? void ( 0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'y'!\"" , "clang/lib/AST/ASTContext.cpp", 11339, __extension__ __PRETTY_FUNCTION__ )) | |||
11339 | "Bad modifiers used with 'y'!")(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers used with 'y'!") ? void ( 0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'y'!\"" , "clang/lib/AST/ASTContext.cpp", 11339, __extension__ __PRETTY_FUNCTION__ )); | |||
11340 | Type = Context.BFloat16Ty; | |||
11341 | break; | |||
11342 | case 'v': | |||
11343 | assert(HowLong == 0 && !Signed && !Unsigned &&(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers used with 'v'!") ? void ( 0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'v'!\"" , "clang/lib/AST/ASTContext.cpp", 11344, __extension__ __PRETTY_FUNCTION__ )) | |||
11344 | "Bad modifiers used with 'v'!")(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers used with 'v'!") ? void ( 0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'v'!\"" , "clang/lib/AST/ASTContext.cpp", 11344, __extension__ __PRETTY_FUNCTION__ )); | |||
11345 | Type = Context.VoidTy; | |||
11346 | break; | |||
11347 | case 'h': | |||
11348 | assert(HowLong == 0 && !Signed && !Unsigned &&(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers used with 'h'!") ? void ( 0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'h'!\"" , "clang/lib/AST/ASTContext.cpp", 11349, __extension__ __PRETTY_FUNCTION__ )) | |||
11349 | "Bad modifiers used with 'h'!")(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers used with 'h'!") ? void ( 0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'h'!\"" , "clang/lib/AST/ASTContext.cpp", 11349, __extension__ __PRETTY_FUNCTION__ )); | |||
11350 | Type = Context.HalfTy; | |||
11351 | break; | |||
11352 | case 'f': | |||
11353 | assert(HowLong == 0 && !Signed && !Unsigned &&(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers used with 'f'!") ? void ( 0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'f'!\"" , "clang/lib/AST/ASTContext.cpp", 11354, __extension__ __PRETTY_FUNCTION__ )) | |||
11354 | "Bad modifiers used with 'f'!")(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers used with 'f'!") ? void ( 0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'f'!\"" , "clang/lib/AST/ASTContext.cpp", 11354, __extension__ __PRETTY_FUNCTION__ )); | |||
11355 | Type = Context.FloatTy; | |||
11356 | break; | |||
11357 | case 'd': | |||
11358 | assert(HowLong < 3 && !Signed && !Unsigned &&(static_cast <bool> (HowLong < 3 && !Signed && !Unsigned && "Bad modifiers used with 'd'!") ? void ( 0) : __assert_fail ("HowLong < 3 && !Signed && !Unsigned && \"Bad modifiers used with 'd'!\"" , "clang/lib/AST/ASTContext.cpp", 11359, __extension__ __PRETTY_FUNCTION__ )) | |||
11359 | "Bad modifiers used with 'd'!")(static_cast <bool> (HowLong < 3 && !Signed && !Unsigned && "Bad modifiers used with 'd'!") ? void ( 0) : __assert_fail ("HowLong < 3 && !Signed && !Unsigned && \"Bad modifiers used with 'd'!\"" , "clang/lib/AST/ASTContext.cpp", 11359, __extension__ __PRETTY_FUNCTION__ )); | |||
11360 | if (HowLong == 1) | |||
11361 | Type = Context.LongDoubleTy; | |||
11362 | else if (HowLong == 2) | |||
11363 | Type = Context.Float128Ty; | |||
11364 | else | |||
11365 | Type = Context.DoubleTy; | |||
11366 | break; | |||
11367 | case 's': | |||
11368 | assert(HowLong == 0 && "Bad modifiers used with 's'!")(static_cast <bool> (HowLong == 0 && "Bad modifiers used with 's'!" ) ? void (0) : __assert_fail ("HowLong == 0 && \"Bad modifiers used with 's'!\"" , "clang/lib/AST/ASTContext.cpp", 11368, __extension__ __PRETTY_FUNCTION__ )); | |||
11369 | if (Unsigned) | |||
11370 | Type = Context.UnsignedShortTy; | |||
11371 | else | |||
11372 | Type = Context.ShortTy; | |||
11373 | break; | |||
11374 | case 'i': | |||
11375 | if (HowLong == 3) | |||
11376 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; | |||
11377 | else if (HowLong == 2) | |||
11378 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; | |||
11379 | else if (HowLong == 1) | |||
11380 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; | |||
11381 | else | |||
11382 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; | |||
11383 | break; | |||
11384 | case 'c': | |||
11385 | assert(HowLong == 0 && "Bad modifiers used with 'c'!")(static_cast <bool> (HowLong == 0 && "Bad modifiers used with 'c'!" ) ? void (0) : __assert_fail ("HowLong == 0 && \"Bad modifiers used with 'c'!\"" , "clang/lib/AST/ASTContext.cpp", 11385, __extension__ __PRETTY_FUNCTION__ )); | |||
11386 | if (Signed) | |||
11387 | Type = Context.SignedCharTy; | |||
11388 | else if (Unsigned) | |||
11389 | Type = Context.UnsignedCharTy; | |||
11390 | else | |||
11391 | Type = Context.CharTy; | |||
11392 | break; | |||
11393 | case 'b': // boolean | |||
11394 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!")(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!") ? void (0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers for 'b'!\"" , "clang/lib/AST/ASTContext.cpp", 11394, __extension__ __PRETTY_FUNCTION__ )); | |||
11395 | Type = Context.BoolTy; | |||
11396 | break; | |||
11397 | case 'z': // size_t. | |||
11398 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!")(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!") ? void (0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers for 'z'!\"" , "clang/lib/AST/ASTContext.cpp", 11398, __extension__ __PRETTY_FUNCTION__ )); | |||
11399 | Type = Context.getSizeType(); | |||
11400 | break; | |||
11401 | case 'w': // wchar_t. | |||
11402 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'w'!")(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'w'!") ? void (0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers for 'w'!\"" , "clang/lib/AST/ASTContext.cpp", 11402, __extension__ __PRETTY_FUNCTION__ )); | |||
11403 | Type = Context.getWideCharType(); | |||
11404 | break; | |||
11405 | case 'F': | |||
11406 | Type = Context.getCFConstantStringType(); | |||
11407 | break; | |||
11408 | case 'G': | |||
11409 | Type = Context.getObjCIdType(); | |||
11410 | break; | |||
11411 | case 'H': | |||
11412 | Type = Context.getObjCSelType(); | |||
11413 | break; | |||
11414 | case 'M': | |||
11415 | Type = Context.getObjCSuperType(); | |||
11416 | break; | |||
11417 | case 'a': | |||
11418 | Type = Context.getBuiltinVaListType(); | |||
11419 | assert(!Type.isNull() && "builtin va list type not initialized!")(static_cast <bool> (!Type.isNull() && "builtin va list type not initialized!" ) ? void (0) : __assert_fail ("!Type.isNull() && \"builtin va list type not initialized!\"" , "clang/lib/AST/ASTContext.cpp", 11419, __extension__ __PRETTY_FUNCTION__ )); | |||
11420 | break; | |||
11421 | case 'A': | |||
11422 | // This is a "reference" to a va_list; however, what exactly | |||
11423 | // this means depends on how va_list is defined. There are two | |||
11424 | // different kinds of va_list: ones passed by value, and ones | |||
11425 | // passed by reference. An example of a by-value va_list is | |||
11426 | // x86, where va_list is a char*. An example of by-ref va_list | |||
11427 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, | |||
11428 | // we want this argument to be a char*&; for x86-64, we want | |||
11429 | // it to be a __va_list_tag*. | |||
11430 | Type = Context.getBuiltinVaListType(); | |||
11431 | assert(!Type.isNull() && "builtin va list type not initialized!")(static_cast <bool> (!Type.isNull() && "builtin va list type not initialized!" ) ? void (0) : __assert_fail ("!Type.isNull() && \"builtin va list type not initialized!\"" , "clang/lib/AST/ASTContext.cpp", 11431, __extension__ __PRETTY_FUNCTION__ )); | |||
11432 | if (Type->isArrayType()) | |||
11433 | Type = Context.getArrayDecayedType(Type); | |||
11434 | else | |||
11435 | Type = Context.getLValueReferenceType(Type); | |||
11436 | break; | |||
11437 | case 'q': { | |||
11438 | char *End; | |||
11439 | unsigned NumElements = strtoul(Str, &End, 10); | |||
11440 | assert(End != Str && "Missing vector size")(static_cast <bool> (End != Str && "Missing vector size" ) ? void (0) : __assert_fail ("End != Str && \"Missing vector size\"" , "clang/lib/AST/ASTContext.cpp", 11440, __extension__ __PRETTY_FUNCTION__ )); | |||
11441 | Str = End; | |||
11442 | ||||
11443 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, | |||
11444 | RequiresICE, false); | |||
11445 | assert(!RequiresICE && "Can't require vector ICE")(static_cast <bool> (!RequiresICE && "Can't require vector ICE" ) ? void (0) : __assert_fail ("!RequiresICE && \"Can't require vector ICE\"" , "clang/lib/AST/ASTContext.cpp", 11445, __extension__ __PRETTY_FUNCTION__ )); | |||
11446 | ||||
11447 | Type = Context.getScalableVectorType(ElementType, NumElements); | |||
11448 | break; | |||
11449 | } | |||
11450 | case 'V': { | |||
11451 | char *End; | |||
11452 | unsigned NumElements = strtoul(Str, &End, 10); | |||
11453 | assert(End != Str && "Missing vector size")(static_cast <bool> (End != Str && "Missing vector size" ) ? void (0) : __assert_fail ("End != Str && \"Missing vector size\"" , "clang/lib/AST/ASTContext.cpp", 11453, __extension__ __PRETTY_FUNCTION__ )); | |||
11454 | Str = End; | |||
11455 | ||||
11456 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, | |||
11457 | RequiresICE, false); | |||
11458 | assert(!RequiresICE && "Can't require vector ICE")(static_cast <bool> (!RequiresICE && "Can't require vector ICE" ) ? void (0) : __assert_fail ("!RequiresICE && \"Can't require vector ICE\"" , "clang/lib/AST/ASTContext.cpp", 11458, __extension__ __PRETTY_FUNCTION__ )); | |||
11459 | ||||
11460 | // TODO: No way to make AltiVec vectors in builtins yet. | |||
11461 | Type = Context.getVectorType(ElementType, NumElements, | |||
11462 | VectorType::GenericVector); | |||
11463 | break; | |||
11464 | } | |||
11465 | case 'E': { | |||
11466 | char *End; | |||
11467 | ||||
11468 | unsigned NumElements = strtoul(Str, &End, 10); | |||
11469 | assert(End != Str && "Missing vector size")(static_cast <bool> (End != Str && "Missing vector size" ) ? void (0) : __assert_fail ("End != Str && \"Missing vector size\"" , "clang/lib/AST/ASTContext.cpp", 11469, __extension__ __PRETTY_FUNCTION__ )); | |||
11470 | ||||
11471 | Str = End; | |||
11472 | ||||
11473 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, | |||
11474 | false); | |||
11475 | Type = Context.getExtVectorType(ElementType, NumElements); | |||
11476 | break; | |||
11477 | } | |||
11478 | case 'X': { | |||
11479 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, | |||
11480 | false); | |||
11481 | assert(!RequiresICE && "Can't require complex ICE")(static_cast <bool> (!RequiresICE && "Can't require complex ICE" ) ? void (0) : __assert_fail ("!RequiresICE && \"Can't require complex ICE\"" , "clang/lib/AST/ASTContext.cpp", 11481, __extension__ __PRETTY_FUNCTION__ )); | |||
11482 | Type = Context.getComplexType(ElementType); | |||
11483 | break; | |||
11484 | } | |||
11485 | case 'Y': | |||
11486 | Type = Context.getPointerDiffType(); | |||
11487 | break; | |||
11488 | case 'P': | |||
11489 | Type = Context.getFILEType(); | |||
11490 | if (Type.isNull()) { | |||
11491 | Error = ASTContext::GE_Missing_stdio; | |||
11492 | return {}; | |||
11493 | } | |||
11494 | break; | |||
11495 | case 'J': | |||
11496 | if (Signed) | |||
11497 | Type = Context.getsigjmp_bufType(); | |||
11498 | else | |||
11499 | Type = Context.getjmp_bufType(); | |||
11500 | ||||
11501 | if (Type.isNull()) { | |||
11502 | Error = ASTContext::GE_Missing_setjmp; | |||
11503 | return {}; | |||
11504 | } | |||
11505 | break; | |||
11506 | case 'K': | |||
11507 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!")(static_cast <bool> (HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!") ? void (0) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers for 'K'!\"" , "clang/lib/AST/ASTContext.cpp", 11507, __extension__ __PRETTY_FUNCTION__ )); | |||
11508 | Type = Context.getucontext_tType(); | |||
11509 | ||||
11510 | if (Type.isNull()) { | |||
11511 | Error = ASTContext::GE_Missing_ucontext; | |||
11512 | return {}; | |||
11513 | } | |||
11514 | break; | |||
11515 | case 'p': | |||
11516 | Type = Context.getProcessIDType(); | |||
11517 | break; | |||
11518 | } | |||
11519 | ||||
11520 | // If there are modifiers and if we're allowed to parse them, go for it. | |||
11521 | Done = !AllowTypeModifiers; | |||
11522 | while (!Done) { | |||
11523 | switch (char c = *Str++) { | |||
11524 | default: Done = true; --Str; break; | |||
11525 | case '*': | |||
11526 | case '&': { | |||
11527 | // Both pointers and references can have their pointee types | |||
11528 | // qualified with an address space. | |||
11529 | char *End; | |||
11530 | unsigned AddrSpace = strtoul(Str, &End, 10); | |||
11531 | if (End != Str) { | |||
11532 | // Note AddrSpace == 0 is not the same as an unspecified address space. | |||
11533 | Type = Context.getAddrSpaceQualType( | |||
11534 | Type, | |||
11535 | Context.getLangASForBuiltinAddressSpace(AddrSpace)); | |||
11536 | Str = End; | |||
11537 | } | |||
11538 | if (c == '*') | |||
11539 | Type = Context.getPointerType(Type); | |||
11540 | else | |||
11541 | Type = Context.getLValueReferenceType(Type); | |||
11542 | break; | |||
11543 | } | |||
11544 | // FIXME: There's no way to have a built-in with an rvalue ref arg. | |||
11545 | case 'C': | |||
11546 | Type = Type.withConst(); | |||
11547 | break; | |||
11548 | case 'D': | |||
11549 | Type = Context.getVolatileType(Type); | |||
11550 | break; | |||
11551 | case 'R': | |||
11552 | Type = Type.withRestrict(); | |||
11553 | break; | |||
11554 | } | |||
11555 | } | |||
11556 | ||||
11557 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&(static_cast <bool> ((!RequiresICE || Type->isIntegralOrEnumerationType ()) && "Integer constant 'I' type must be an integer" ) ? void (0) : __assert_fail ("(!RequiresICE || Type->isIntegralOrEnumerationType()) && \"Integer constant 'I' type must be an integer\"" , "clang/lib/AST/ASTContext.cpp", 11558, __extension__ __PRETTY_FUNCTION__ )) | |||
11558 | "Integer constant 'I' type must be an integer")(static_cast <bool> ((!RequiresICE || Type->isIntegralOrEnumerationType ()) && "Integer constant 'I' type must be an integer" ) ? void (0) : __assert_fail ("(!RequiresICE || Type->isIntegralOrEnumerationType()) && \"Integer constant 'I' type must be an integer\"" , "clang/lib/AST/ASTContext.cpp", 11558, __extension__ __PRETTY_FUNCTION__ )); | |||
11559 | ||||
11560 | return Type; | |||
11561 | } | |||
11562 | ||||
11563 | // On some targets such as PowerPC, some of the builtins are defined with custom | |||
11564 | // type descriptors for target-dependent types. These descriptors are decoded in | |||
11565 | // other functions, but it may be useful to be able to fall back to default | |||
11566 | // descriptor decoding to define builtins mixing target-dependent and target- | |||
11567 | // independent types. This function allows decoding one type descriptor with | |||
11568 | // default decoding. | |||
11569 | QualType ASTContext::DecodeTypeStr(const char *&Str, const ASTContext &Context, | |||
11570 | GetBuiltinTypeError &Error, bool &RequireICE, | |||
11571 | bool AllowTypeModifiers) const { | |||
11572 | return DecodeTypeFromStr(Str, Context, Error, RequireICE, AllowTypeModifiers); | |||
11573 | } | |||
11574 | ||||
11575 | /// GetBuiltinType - Return the type for the specified builtin. | |||
11576 | QualType ASTContext::GetBuiltinType(unsigned Id, | |||
11577 | GetBuiltinTypeError &Error, | |||
11578 | unsigned *IntegerConstantArgs) const { | |||
11579 | const char *TypeStr = BuiltinInfo.getTypeString(Id); | |||
11580 | if (TypeStr[0] == '\0') { | |||
11581 | Error = GE_Missing_type; | |||
11582 | return {}; | |||
11583 | } | |||
11584 | ||||
11585 | SmallVector<QualType, 8> ArgTypes; | |||
11586 | ||||
11587 | bool RequiresICE = false; | |||
11588 | Error = GE_None; | |||
11589 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, | |||
11590 | RequiresICE, true); | |||
11591 | if (Error != GE_None) | |||
11592 | return {}; | |||
11593 | ||||
11594 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE")(static_cast <bool> (!RequiresICE && "Result of intrinsic cannot be required to be an ICE" ) ? void (0) : __assert_fail ("!RequiresICE && \"Result of intrinsic cannot be required to be an ICE\"" , "clang/lib/AST/ASTContext.cpp", 11594, __extension__ __PRETTY_FUNCTION__ )); | |||
11595 | ||||
11596 | while (TypeStr[0] && TypeStr[0] != '.') { | |||
11597 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); | |||
11598 | if (Error != GE_None) | |||
11599 | return {}; | |||
11600 | ||||
11601 | // If this argument is required to be an IntegerConstantExpression and the | |||
11602 | // caller cares, fill in the bitmask we return. | |||
11603 | if (RequiresICE && IntegerConstantArgs) | |||
11604 | *IntegerConstantArgs |= 1 << ArgTypes.size(); | |||
11605 | ||||
11606 | // Do array -> pointer decay. The builtin should use the decayed type. | |||
11607 | if (Ty->isArrayType()) | |||
11608 | Ty = getArrayDecayedType(Ty); | |||
11609 | ||||
11610 | ArgTypes.push_back(Ty); | |||
11611 | } | |||
11612 | ||||
11613 | if (Id == Builtin::BI__GetExceptionInfo) | |||
11614 | return {}; | |||
11615 | ||||
11616 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&(static_cast <bool> ((TypeStr[0] != '.' || TypeStr[1] == 0) && "'.' should only occur at end of builtin type list!" ) ? void (0) : __assert_fail ("(TypeStr[0] != '.' || TypeStr[1] == 0) && \"'.' should only occur at end of builtin type list!\"" , "clang/lib/AST/ASTContext.cpp", 11617, __extension__ __PRETTY_FUNCTION__ )) | |||
11617 | "'.' should only occur at end of builtin type list!")(static_cast <bool> ((TypeStr[0] != '.' || TypeStr[1] == 0) && "'.' should only occur at end of builtin type list!" ) ? void (0) : __assert_fail ("(TypeStr[0] != '.' || TypeStr[1] == 0) && \"'.' should only occur at end of builtin type list!\"" , "clang/lib/AST/ASTContext.cpp", 11617, __extension__ __PRETTY_FUNCTION__ )); | |||
11618 | ||||
11619 | bool Variadic = (TypeStr[0] == '.'); | |||
11620 | ||||
11621 | FunctionType::ExtInfo EI(getDefaultCallingConvention( | |||
11622 | Variadic, /*IsCXXMethod=*/false, /*IsBuiltin=*/true)); | |||
11623 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); | |||
11624 | ||||
11625 | ||||
11626 | // We really shouldn't be making a no-proto type here. | |||
11627 | if (ArgTypes.empty() && Variadic && !getLangOpts().requiresStrictPrototypes()) | |||
11628 | return getFunctionNoProtoType(ResType, EI); | |||
11629 | ||||
11630 | FunctionProtoType::ExtProtoInfo EPI; | |||
11631 | EPI.ExtInfo = EI; | |||
11632 | EPI.Variadic = Variadic; | |||
11633 | if (getLangOpts().CPlusPlus && BuiltinInfo.isNoThrow(Id)) | |||
11634 | EPI.ExceptionSpec.Type = | |||
11635 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; | |||
11636 | ||||
11637 | return getFunctionType(ResType, ArgTypes, EPI); | |||
11638 | } | |||
11639 | ||||
11640 | static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, | |||
11641 | const FunctionDecl *FD) { | |||
11642 | if (!FD->isExternallyVisible()) | |||
11643 | return GVA_Internal; | |||
11644 | ||||
11645 | // Non-user-provided functions get emitted as weak definitions with every | |||
11646 | // use, no matter whether they've been explicitly instantiated etc. | |||
11647 | if (!FD->isUserProvided()) | |||
11648 | return GVA_DiscardableODR; | |||
11649 | ||||
11650 | GVALinkage External; | |||
11651 | switch (FD->getTemplateSpecializationKind()) { | |||
11652 | case TSK_Undeclared: | |||
11653 | case TSK_ExplicitSpecialization: | |||
11654 | External = GVA_StrongExternal; | |||
11655 | break; | |||
11656 | ||||
11657 | case TSK_ExplicitInstantiationDefinition: | |||
11658 | return GVA_StrongODR; | |||
11659 | ||||
11660 | // C++11 [temp.explicit]p10: | |||
11661 | // [ Note: The intent is that an inline function that is the subject of | |||
11662 | // an explicit instantiation declaration will still be implicitly | |||
11663 | // instantiated when used so that the body can be considered for | |||
11664 | // inlining, but that no out-of-line copy of the inline function would be | |||
11665 | // generated in the translation unit. -- end note ] | |||
11666 | case TSK_ExplicitInstantiationDeclaration: | |||
11667 | return GVA_AvailableExternally; | |||
11668 | ||||
11669 | case TSK_ImplicitInstantiation: | |||
11670 | External = GVA_DiscardableODR; | |||
11671 | break; | |||
11672 | } | |||
11673 | ||||
11674 | if (!FD->isInlined()) | |||
11675 | return External; | |||
11676 | ||||
11677 | if ((!Context.getLangOpts().CPlusPlus && | |||
11678 | !Context.getTargetInfo().getCXXABI().isMicrosoft() && | |||
11679 | !FD->hasAttr<DLLExportAttr>()) || | |||
11680 | FD->hasAttr<GNUInlineAttr>()) { | |||
11681 | // FIXME: This doesn't match gcc's behavior for dllexport inline functions. | |||
11682 | ||||
11683 | // GNU or C99 inline semantics. Determine whether this symbol should be | |||
11684 | // externally visible. | |||
11685 | if (FD->isInlineDefinitionExternallyVisible()) | |||
11686 | return External; | |||
11687 | ||||
11688 | // C99 inline semantics, where the symbol is not externally visible. | |||
11689 | return GVA_AvailableExternally; | |||
11690 | } | |||
11691 | ||||
11692 | // Functions specified with extern and inline in -fms-compatibility mode | |||
11693 | // forcibly get emitted. While the body of the function cannot be later | |||
11694 | // replaced, the function definition cannot be discarded. | |||
11695 | if (FD->isMSExternInline()) | |||
11696 | return GVA_StrongODR; | |||
11697 | ||||
11698 | return GVA_DiscardableODR; | |||
11699 | } | |||
11700 | ||||
11701 | static GVALinkage adjustGVALinkageForAttributes(const ASTContext &Context, | |||
11702 | const Decl *D, GVALinkage L) { | |||
11703 | // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx | |||
11704 | // dllexport/dllimport on inline functions. | |||
11705 | if (D->hasAttr<DLLImportAttr>()) { | |||
11706 | if (L == GVA_DiscardableODR || L == GVA_StrongODR) | |||
11707 | return GVA_AvailableExternally; | |||
11708 | } else if (D->hasAttr<DLLExportAttr>()) { | |||
11709 | if (L == GVA_DiscardableODR) | |||
11710 | return GVA_StrongODR; | |||
11711 | } else if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice) { | |||
11712 | // Device-side functions with __global__ attribute must always be | |||
11713 | // visible externally so they can be launched from host. | |||
11714 | if (D->hasAttr<CUDAGlobalAttr>() && | |||
11715 | (L == GVA_DiscardableODR || L == GVA_Internal)) | |||
11716 | return GVA_StrongODR; | |||
11717 | // Single source offloading languages like CUDA/HIP need to be able to | |||
11718 | // access static device variables from host code of the same compilation | |||
11719 | // unit. This is done by externalizing the static variable with a shared | |||
11720 | // name between the host and device compilation which is the same for the | |||
11721 | // same compilation unit whereas different among different compilation | |||
11722 | // units. | |||
11723 | if (Context.shouldExternalize(D)) | |||
11724 | return GVA_StrongExternal; | |||
11725 | } | |||
11726 | return L; | |||
11727 | } | |||
11728 | ||||
11729 | /// Adjust the GVALinkage for a declaration based on what an external AST source | |||
11730 | /// knows about whether there can be other definitions of this declaration. | |||
11731 | static GVALinkage | |||
11732 | adjustGVALinkageForExternalDefinitionKind(const ASTContext &Ctx, const Decl *D, | |||
11733 | GVALinkage L) { | |||
11734 | ExternalASTSource *Source = Ctx.getExternalSource(); | |||
11735 | if (!Source) | |||
11736 | return L; | |||
11737 | ||||
11738 | switch (Source->hasExternalDefinitions(D)) { | |||
11739 | case ExternalASTSource::EK_Never: | |||
11740 | // Other translation units rely on us to provide the definition. | |||
11741 | if (L == GVA_DiscardableODR) | |||
11742 | return GVA_StrongODR; | |||
11743 | break; | |||
11744 | ||||
11745 | case ExternalASTSource::EK_Always: | |||
11746 | return GVA_AvailableExternally; | |||
11747 | ||||
11748 | case ExternalASTSource::EK_ReplyHazy: | |||
11749 | break; | |||
11750 | } | |||
11751 | return L; | |||
11752 | } | |||
11753 | ||||
11754 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { | |||
11755 | return adjustGVALinkageForExternalDefinitionKind(*this, FD, | |||
11756 | adjustGVALinkageForAttributes(*this, FD, | |||
11757 | basicGVALinkageForFunction(*this, FD))); | |||
11758 | } | |||
11759 | ||||
11760 | static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, | |||
11761 | const VarDecl *VD) { | |||
11762 | if (!VD->isExternallyVisible()) | |||
11763 | return GVA_Internal; | |||
11764 | ||||
11765 | if (VD->isStaticLocal()) { | |||
11766 | const DeclContext *LexicalContext = VD->getParentFunctionOrMethod(); | |||
11767 | while (LexicalContext && !isa<FunctionDecl>(LexicalContext)) | |||
11768 | LexicalContext = LexicalContext->getLexicalParent(); | |||
11769 | ||||
11770 | // ObjC Blocks can create local variables that don't have a FunctionDecl | |||
11771 | // LexicalContext. | |||
11772 | if (!LexicalContext) | |||
11773 | return GVA_DiscardableODR; | |||
11774 | ||||
11775 | // Otherwise, let the static local variable inherit its linkage from the | |||
11776 | // nearest enclosing function. | |||
11777 | auto StaticLocalLinkage = | |||
11778 | Context.GetGVALinkageForFunction(cast<FunctionDecl>(LexicalContext)); | |||
11779 | ||||
11780 | // Itanium ABI 5.2.2: "Each COMDAT group [for a static local variable] must | |||
11781 | // be emitted in any object with references to the symbol for the object it | |||
11782 | // contains, whether inline or out-of-line." | |||
11783 | // Similar behavior is observed with MSVC. An alternative ABI could use | |||
11784 | // StrongODR/AvailableExternally to match the function, but none are | |||
11785 | // known/supported currently. | |||
11786 | if (StaticLocalLinkage == GVA_StrongODR || | |||
11787 | StaticLocalLinkage == GVA_AvailableExternally) | |||
11788 | return GVA_DiscardableODR; | |||
11789 | return StaticLocalLinkage; | |||
11790 | } | |||
11791 | ||||
11792 | // MSVC treats in-class initialized static data members as definitions. | |||
11793 | // By giving them non-strong linkage, out-of-line definitions won't | |||
11794 | // cause link errors. | |||
11795 | if (Context.isMSStaticDataMemberInlineDefinition(VD)) | |||
11796 | return GVA_DiscardableODR; | |||
11797 | ||||
11798 | // Most non-template variables have strong linkage; inline variables are | |||
11799 | // linkonce_odr or (occasionally, for compatibility) weak_odr. | |||
11800 | GVALinkage StrongLinkage; | |||
11801 | switch (Context.getInlineVariableDefinitionKind(VD)) { | |||
11802 | case ASTContext::InlineVariableDefinitionKind::None: | |||
11803 | StrongLinkage = GVA_StrongExternal; | |||
11804 | break; | |||
11805 | case ASTContext::InlineVariableDefinitionKind::Weak: | |||
11806 | case ASTContext::InlineVariableDefinitionKind::WeakUnknown: | |||
11807 | StrongLinkage = GVA_DiscardableODR; | |||
11808 | break; | |||
11809 | case ASTContext::InlineVariableDefinitionKind::Strong: | |||
11810 | StrongLinkage = GVA_StrongODR; | |||
11811 | break; | |||
11812 | } | |||
11813 | ||||
11814 | switch (VD->getTemplateSpecializationKind()) { | |||
11815 | case TSK_Undeclared: | |||
11816 | return StrongLinkage; | |||
11817 | ||||
11818 | case TSK_ExplicitSpecialization: | |||
11819 | return Context.getTargetInfo().getCXXABI().isMicrosoft() && | |||
11820 | VD->isStaticDataMember() | |||
11821 | ? GVA_StrongODR | |||
11822 | : StrongLinkage; | |||
11823 | ||||
11824 | case TSK_ExplicitInstantiationDefinition: | |||
11825 | return GVA_StrongODR; | |||
11826 | ||||
11827 | case TSK_ExplicitInstantiationDeclaration: | |||
11828 | return GVA_AvailableExternally; | |||
11829 | ||||
11830 | case TSK_ImplicitInstantiation: | |||
11831 | return GVA_DiscardableODR; | |||
11832 | } | |||
11833 | ||||
11834 | llvm_unreachable("Invalid Linkage!")::llvm::llvm_unreachable_internal("Invalid Linkage!", "clang/lib/AST/ASTContext.cpp" , 11834); | |||
11835 | } | |||
11836 | ||||
11837 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) const { | |||
11838 | return adjustGVALinkageForExternalDefinitionKind(*this, VD, | |||
11839 | adjustGVALinkageForAttributes(*this, VD, | |||
11840 | basicGVALinkageForVariable(*this, VD))); | |||
11841 | } | |||
11842 | ||||
11843 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { | |||
11844 | if (const auto *VD = dyn_cast<VarDecl>(D)) { | |||
11845 | if (!VD->isFileVarDecl()) | |||
11846 | return false; | |||
11847 | // Global named register variables (GNU extension) are never emitted. | |||
11848 | if (VD->getStorageClass() == SC_Register) | |||
11849 | return false; | |||
11850 | if (VD->getDescribedVarTemplate() || | |||
11851 | isa<VarTemplatePartialSpecializationDecl>(VD)) | |||
11852 | return false; | |||
11853 | } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { | |||
11854 | // We never need to emit an uninstantiated function template. | |||
11855 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) | |||
11856 | return false; | |||
11857 | } else if (isa<PragmaCommentDecl>(D)) | |||
11858 | return true; | |||
11859 | else if (isa<PragmaDetectMismatchDecl>(D)) | |||
11860 | return true; | |||
11861 | else if (isa<OMPRequiresDecl>(D)) | |||
11862 | return true; | |||
11863 | else if (isa<OMPThreadPrivateDecl>(D)) | |||
11864 | return !D->getDeclContext()->isDependentContext(); | |||
11865 | else if (isa<OMPAllocateDecl>(D)) | |||
11866 | return !D->getDeclContext()->isDependentContext(); | |||
11867 | else if (isa<OMPDeclareReductionDecl>(D) || isa<OMPDeclareMapperDecl>(D)) | |||
11868 | return !D->getDeclContext()->isDependentContext(); | |||
11869 | else if (isa<ImportDecl>(D)) | |||
11870 | return true; | |||
11871 | else | |||
11872 | return false; | |||
11873 | ||||
11874 | // If this is a member of a class template, we do not need to emit it. | |||
11875 | if (D->getDeclContext()->isDependentContext()) | |||
11876 | return false; | |||
11877 | ||||
11878 | // Weak references don't produce any output by themselves. | |||
11879 | if (D->hasAttr<WeakRefAttr>()) | |||
11880 | return false; | |||
11881 | ||||
11882 | // Aliases and used decls are required. | |||
11883 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) | |||
11884 | return true; | |||
11885 | ||||
11886 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { | |||
11887 | // Forward declarations aren't required. | |||
11888 | if (!FD->doesThisDeclarationHaveABody()) | |||
11889 | return FD->doesDeclarationForceExternallyVisibleDefinition(); | |||
11890 | ||||
11891 | // Constructors and destructors are required. | |||
11892 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) | |||
11893 | return true; | |||
11894 | ||||
11895 | // The key function for a class is required. This rule only comes | |||
11896 | // into play when inline functions can be key functions, though. | |||
11897 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { | |||
11898 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { | |||
11899 | const CXXRecordDecl *RD = MD->getParent(); | |||
11900 | if (MD->isOutOfLine() && RD->isDynamicClass()) { | |||
11901 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); | |||
11902 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) | |||
11903 | return true; | |||
11904 | } | |||
11905 | } | |||
11906 | } | |||
11907 | ||||
11908 | GVALinkage Linkage = GetGVALinkageForFunction(FD); | |||
11909 | ||||
11910 | // static, static inline, always_inline, and extern inline functions can | |||
11911 | // always be deferred. Normal inline functions can be deferred in C99/C++. | |||
11912 | // Implicit template instantiations can also be deferred in C++. | |||
11913 | return !isDiscardableGVALinkage(Linkage); | |||
11914 | } | |||
11915 | ||||
11916 | const auto *VD = cast<VarDecl>(D); | |||
11917 | assert(VD->isFileVarDecl() && "Expected file scoped var")(static_cast <bool> (VD->isFileVarDecl() && "Expected file scoped var" ) ? void (0) : __assert_fail ("VD->isFileVarDecl() && \"Expected file scoped var\"" , "clang/lib/AST/ASTContext.cpp", 11917, __extension__ __PRETTY_FUNCTION__ )); | |||
11918 | ||||
11919 | // If the decl is marked as `declare target to`, it should be emitted for the | |||
11920 | // host and for the device. | |||
11921 | if (LangOpts.OpenMP && | |||
11922 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) | |||
11923 | return true; | |||
11924 | ||||
11925 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly && | |||
11926 | !isMSStaticDataMemberInlineDefinition(VD)) | |||
11927 | return false; | |||
11928 | ||||
11929 | // Variables in other module units shouldn't be forced to be emitted. | |||
11930 | if (VD->isInAnotherModuleUnit()) | |||
11931 | return false; | |||
11932 | ||||
11933 | // Variables that can be needed in other TUs are required. | |||
11934 | auto Linkage = GetGVALinkageForVariable(VD); | |||
11935 | if (!isDiscardableGVALinkage(Linkage)) | |||
11936 | return true; | |||
11937 | ||||
11938 | // We never need to emit a variable that is available in another TU. | |||
11939 | if (Linkage == GVA_AvailableExternally) | |||
11940 | return false; | |||
11941 | ||||
11942 | // Variables that have destruction with side-effects are required. | |||
11943 | if (VD->needsDestruction(*this)) | |||
11944 | return true; | |||
11945 | ||||
11946 | // Variables that have initialization with side-effects are required. | |||
11947 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this) && | |||
11948 | // We can get a value-dependent initializer during error recovery. | |||
11949 | (VD->getInit()->isValueDependent() || !VD->evaluateValue())) | |||
11950 | return true; | |||
11951 | ||||
11952 | // Likewise, variables with tuple-like bindings are required if their | |||
11953 | // bindings have side-effects. | |||
11954 | if (const auto *DD = dyn_cast<DecompositionDecl>(VD)) | |||
11955 | for (const auto *BD : DD->bindings()) | |||
11956 | if (const auto *BindingVD = BD->getHoldingVar()) | |||
11957 | if (DeclMustBeEmitted(BindingVD)) | |||
11958 | return true; | |||
11959 | ||||
11960 | return false; | |||
11961 | } | |||
11962 | ||||
11963 | void ASTContext::forEachMultiversionedFunctionVersion( | |||
11964 | const FunctionDecl *FD, | |||
11965 | llvm::function_ref<void(FunctionDecl *)> Pred) const { | |||
11966 | assert(FD->isMultiVersion() && "Only valid for multiversioned functions")(static_cast <bool> (FD->isMultiVersion() && "Only valid for multiversioned functions") ? void (0) : __assert_fail ("FD->isMultiVersion() && \"Only valid for multiversioned functions\"" , "clang/lib/AST/ASTContext.cpp", 11966, __extension__ __PRETTY_FUNCTION__ )); | |||
11967 | llvm::SmallDenseSet<const FunctionDecl*, 4> SeenDecls; | |||
11968 | FD = FD->getMostRecentDecl(); | |||
11969 | // FIXME: The order of traversal here matters and depends on the order of | |||
11970 | // lookup results, which happens to be (mostly) oldest-to-newest, but we | |||
11971 | // shouldn't rely on that. | |||
11972 | for (auto *CurDecl : | |||
11973 | FD->getDeclContext()->getRedeclContext()->lookup(FD->getDeclName())) { | |||
11974 | FunctionDecl *CurFD = CurDecl->getAsFunction()->getMostRecentDecl(); | |||
11975 | if (CurFD && hasSameType(CurFD->getType(), FD->getType()) && | |||
11976 | !SeenDecls.contains(CurFD)) { | |||
11977 | SeenDecls.insert(CurFD); | |||
11978 | Pred(CurFD); | |||
11979 | } | |||
11980 | } | |||
11981 | } | |||
11982 | ||||
11983 | CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic, | |||
11984 | bool IsCXXMethod, | |||
11985 | bool IsBuiltin) const { | |||
11986 | // Pass through to the C++ ABI object | |||
11987 | if (IsCXXMethod) | |||
11988 | return ABI->getDefaultMethodCallConv(IsVariadic); | |||
11989 | ||||
11990 | // Builtins ignore user-specified default calling convention and remain the | |||
11991 | // Target's default calling convention. | |||
11992 | if (!IsBuiltin) { | |||
11993 | switch (LangOpts.getDefaultCallingConv()) { | |||
11994 | case LangOptions::DCC_None: | |||
11995 | break; | |||
11996 | case LangOptions::DCC_CDecl: | |||
11997 | return CC_C; | |||
11998 | case LangOptions::DCC_FastCall: | |||
11999 | if (getTargetInfo().hasFeature("sse2") && !IsVariadic) | |||
12000 | return CC_X86FastCall; | |||
12001 | break; | |||
12002 | case LangOptions::DCC_StdCall: | |||
12003 | if (!IsVariadic) | |||
12004 | return CC_X86StdCall; | |||
12005 | break; | |||
12006 | case LangOptions::DCC_VectorCall: | |||
12007 | // __vectorcall cannot be applied to variadic functions. | |||
12008 | if (!IsVariadic) | |||
12009 | return CC_X86VectorCall; | |||
12010 | break; | |||
12011 | case LangOptions::DCC_RegCall: | |||
12012 | // __regcall cannot be applied to variadic functions. | |||
12013 | if (!IsVariadic) | |||
12014 | return CC_X86RegCall; | |||
12015 | break; | |||
12016 | } | |||
12017 | } | |||
12018 | return Target->getDefaultCallingConv(); | |||
12019 | } | |||
12020 | ||||
12021 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { | |||
12022 | // Pass through to the C++ ABI object | |||
12023 | return ABI->isNearlyEmpty(RD); | |||
12024 | } | |||
12025 | ||||
12026 | VTableContextBase *ASTContext::getVTableContext() { | |||
12027 | if (!VTContext.get()) { | |||
12028 | auto ABI = Target->getCXXABI(); | |||
12029 | if (ABI.isMicrosoft()) | |||
12030 | VTContext.reset(new MicrosoftVTableContext(*this)); | |||
12031 | else { | |||
12032 | auto ComponentLayout = getLangOpts().RelativeCXXABIVTables | |||
12033 | ? ItaniumVTableContext::Relative | |||
12034 | : ItaniumVTableContext::Pointer; | |||
12035 | VTContext.reset(new ItaniumVTableContext(*this, ComponentLayout)); | |||
12036 | } | |||
12037 | } | |||
12038 | return VTContext.get(); | |||
12039 | } | |||
12040 | ||||
12041 | MangleContext *ASTContext::createMangleContext(const TargetInfo *T) { | |||
12042 | if (!T) | |||
12043 | T = Target; | |||
12044 | switch (T->getCXXABI().getKind()) { | |||
12045 | case TargetCXXABI::AppleARM64: | |||
12046 | case TargetCXXABI::Fuchsia: | |||
12047 | case TargetCXXABI::GenericAArch64: | |||
12048 | case TargetCXXABI::GenericItanium: | |||
12049 | case TargetCXXABI::GenericARM: | |||
12050 | case TargetCXXABI::GenericMIPS: | |||
12051 | case TargetCXXABI::iOS: | |||
12052 | case TargetCXXABI::WebAssembly: | |||
12053 | case TargetCXXABI::WatchOS: | |||
12054 | case TargetCXXABI::XL: | |||
12055 | return ItaniumMangleContext::create(*this, getDiagnostics()); | |||
12056 | case TargetCXXABI::Microsoft: | |||
12057 | return MicrosoftMangleContext::create(*this, getDiagnostics()); | |||
12058 | } | |||
12059 | llvm_unreachable("Unsupported ABI")::llvm::llvm_unreachable_internal("Unsupported ABI", "clang/lib/AST/ASTContext.cpp" , 12059); | |||
12060 | } | |||
12061 | ||||
12062 | MangleContext *ASTContext::createDeviceMangleContext(const TargetInfo &T) { | |||
12063 | assert(T.getCXXABI().getKind() != TargetCXXABI::Microsoft &&(static_cast <bool> (T.getCXXABI().getKind() != TargetCXXABI ::Microsoft && "Device mangle context does not support Microsoft mangling." ) ? void (0) : __assert_fail ("T.getCXXABI().getKind() != TargetCXXABI::Microsoft && \"Device mangle context does not support Microsoft mangling.\"" , "clang/lib/AST/ASTContext.cpp", 12064, __extension__ __PRETTY_FUNCTION__ )) | |||
12064 | "Device mangle context does not support Microsoft mangling.")(static_cast <bool> (T.getCXXABI().getKind() != TargetCXXABI ::Microsoft && "Device mangle context does not support Microsoft mangling." ) ? void (0) : __assert_fail ("T.getCXXABI().getKind() != TargetCXXABI::Microsoft && \"Device mangle context does not support Microsoft mangling.\"" , "clang/lib/AST/ASTContext.cpp", 12064, __extension__ __PRETTY_FUNCTION__ )); | |||
12065 | switch (T.getCXXABI().getKind()) { | |||
12066 | case TargetCXXABI::AppleARM64: | |||
12067 | case TargetCXXABI::Fuchsia: | |||
12068 | case TargetCXXABI::GenericAArch64: | |||
12069 | case TargetCXXABI::GenericItanium: | |||
12070 | case TargetCXXABI::GenericARM: | |||
12071 | case TargetCXXABI::GenericMIPS: | |||
12072 | case TargetCXXABI::iOS: | |||
12073 | case TargetCXXABI::WebAssembly: | |||
12074 | case TargetCXXABI::WatchOS: | |||
12075 | case TargetCXXABI::XL: | |||
12076 | return ItaniumMangleContext::create( | |||
12077 | *this, getDiagnostics(), | |||
12078 | [](ASTContext &, const NamedDecl *ND) -> std::optional<unsigned> { | |||
12079 | if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) | |||
12080 | return RD->getDeviceLambdaManglingNumber(); | |||
12081 | return std::nullopt; | |||
12082 | }, | |||
12083 | /*IsAux=*/true); | |||
12084 | case TargetCXXABI::Microsoft: | |||
12085 | return MicrosoftMangleContext::create(*this, getDiagnostics(), | |||
12086 | /*IsAux=*/true); | |||
12087 | } | |||
12088 | llvm_unreachable("Unsupported ABI")::llvm::llvm_unreachable_internal("Unsupported ABI", "clang/lib/AST/ASTContext.cpp" , 12088); | |||
12089 | } | |||
12090 | ||||
12091 | CXXABI::~CXXABI() = default; | |||
12092 | ||||
12093 | size_t ASTContext::getSideTableAllocatedMemory() const { | |||
12094 | return ASTRecordLayouts.getMemorySize() + | |||
12095 | llvm::capacity_in_bytes(ObjCLayouts) + | |||
12096 | llvm::capacity_in_bytes(KeyFunctions) + | |||
12097 | llvm::capacity_in_bytes(ObjCImpls) + | |||
12098 | llvm::capacity_in_bytes(BlockVarCopyInits) + | |||
12099 | llvm::capacity_in_bytes(DeclAttrs) + | |||
12100 | llvm::capacity_in_bytes(TemplateOrInstantiation) + | |||
12101 | llvm::capacity_in_bytes(InstantiatedFromUsingDecl) + | |||
12102 | llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) + | |||
12103 | llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) + | |||
12104 | llvm::capacity_in_bytes(OverriddenMethods) + | |||
12105 | llvm::capacity_in_bytes(Types) + | |||
12106 | llvm::capacity_in_bytes(VariableArrayTypes); | |||
12107 | } | |||
12108 | ||||
12109 | /// getIntTypeForBitwidth - | |||
12110 | /// sets integer QualTy according to specified details: | |||
12111 | /// bitwidth, signed/unsigned. | |||
12112 | /// Returns empty type if there is no appropriate target types. | |||
12113 | QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth, | |||
12114 | unsigned Signed) const { | |||
12115 | TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(DestWidth, Signed); | |||
12116 | CanQualType QualTy = getFromTargetType(Ty); | |||
12117 | if (!QualTy && DestWidth == 128) | |||
12118 | return Signed ? Int128Ty : UnsignedInt128Ty; | |||
12119 | return QualTy; | |||
12120 | } | |||
12121 | ||||
12122 | /// getRealTypeForBitwidth - | |||
12123 | /// sets floating point QualTy according to specified bitwidth. | |||
12124 | /// Returns empty type if there is no appropriate target types. | |||
12125 | QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth, | |||
12126 | FloatModeKind ExplicitType) const { | |||
12127 | FloatModeKind Ty = | |||
12128 | getTargetInfo().getRealTypeByWidth(DestWidth, ExplicitType); | |||
12129 | switch (Ty) { | |||
12130 | case FloatModeKind::Half: | |||
12131 | return HalfTy; | |||
12132 | case FloatModeKind::Float: | |||
12133 | return FloatTy; | |||
12134 | case FloatModeKind::Double: | |||
12135 | return DoubleTy; | |||
12136 | case FloatModeKind::LongDouble: | |||
12137 | return LongDoubleTy; | |||
12138 | case FloatModeKind::Float128: | |||
12139 | return Float128Ty; | |||
12140 | case FloatModeKind::Ibm128: | |||
12141 | return Ibm128Ty; | |||
12142 | case FloatModeKind::NoFloat: | |||
12143 | return {}; | |||
12144 | } | |||
12145 | ||||
12146 | llvm_unreachable("Unhandled TargetInfo::RealType value")::llvm::llvm_unreachable_internal("Unhandled TargetInfo::RealType value" , "clang/lib/AST/ASTContext.cpp", 12146); | |||
12147 | } | |||
12148 | ||||
12149 | void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) { | |||
12150 | if (Number > 1) | |||
12151 | MangleNumbers[ND] = Number; | |||
12152 | } | |||
12153 | ||||
12154 | unsigned ASTContext::getManglingNumber(const NamedDecl *ND, | |||
12155 | bool ForAuxTarget) const { | |||
12156 | auto I = MangleNumbers.find(ND); | |||
12157 | unsigned Res = I != MangleNumbers.end() ? I->second : 1; | |||
12158 | // CUDA/HIP host compilation encodes host and device mangling numbers | |||
12159 | // as lower and upper half of 32 bit integer. | |||
12160 | if (LangOpts.CUDA && !LangOpts.CUDAIsDevice) { | |||
12161 | Res = ForAuxTarget ? Res >> 16 : Res & 0xFFFF; | |||
12162 | } else { | |||
12163 | assert(!ForAuxTarget && "Only CUDA/HIP host compilation supports mangling "(static_cast <bool> (!ForAuxTarget && "Only CUDA/HIP host compilation supports mangling " "number for aux target") ? void (0) : __assert_fail ("!ForAuxTarget && \"Only CUDA/HIP host compilation supports mangling \" \"number for aux target\"" , "clang/lib/AST/ASTContext.cpp", 12164, __extension__ __PRETTY_FUNCTION__ )) | |||
12164 | "number for aux target")(static_cast <bool> (!ForAuxTarget && "Only CUDA/HIP host compilation supports mangling " "number for aux target") ? void (0) : __assert_fail ("!ForAuxTarget && \"Only CUDA/HIP host compilation supports mangling \" \"number for aux target\"" , "clang/lib/AST/ASTContext.cpp", 12164, __extension__ __PRETTY_FUNCTION__ )); | |||
12165 | } | |||
12166 | return Res > 1 ? Res : 1; | |||
12167 | } | |||
12168 | ||||
12169 | void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) { | |||
12170 | if (Number > 1) | |||
12171 | StaticLocalNumbers[VD] = Number; | |||
12172 | } | |||
12173 | ||||
12174 | unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const { | |||
12175 | auto I = StaticLocalNumbers.find(VD); | |||
12176 | return I != StaticLocalNumbers.end() ? I->second : 1; | |||
12177 | } | |||
12178 | ||||
12179 | MangleNumberingContext & | |||
12180 | ASTContext::getManglingNumberContext(const DeclContext *DC) { | |||
12181 | assert(LangOpts.CPlusPlus)(static_cast <bool> (LangOpts.CPlusPlus) ? void (0) : __assert_fail ("LangOpts.CPlusPlus", "clang/lib/AST/ASTContext.cpp", 12181 , __extension__ __PRETTY_FUNCTION__)); // We don't need mangling numbers for plain C. | |||
12182 | std::unique_ptr<MangleNumberingContext> &MCtx = MangleNumberingContexts[DC]; | |||
12183 | if (!MCtx) | |||
12184 | MCtx = createMangleNumberingContext(); | |||
12185 | return *MCtx; | |||
12186 | } | |||
12187 | ||||
12188 | MangleNumberingContext & | |||
12189 | ASTContext::getManglingNumberContext(NeedExtraManglingDecl_t, const Decl *D) { | |||
12190 | assert(LangOpts.CPlusPlus)(static_cast <bool> (LangOpts.CPlusPlus) ? void (0) : __assert_fail ("LangOpts.CPlusPlus", "clang/lib/AST/ASTContext.cpp", 12190 , __extension__ __PRETTY_FUNCTION__)); // We don't need mangling numbers for plain C. | |||
12191 | std::unique_ptr<MangleNumberingContext> &MCtx = | |||
12192 | ExtraMangleNumberingContexts[D]; | |||
12193 | if (!MCtx) | |||
12194 | MCtx = createMangleNumberingContext(); | |||
12195 | return *MCtx; | |||
12196 | } | |||
12197 | ||||
12198 | std::unique_ptr<MangleNumberingContext> | |||
12199 | ASTContext::createMangleNumberingContext() const { | |||
12200 | return ABI->createMangleNumberingContext(); | |||
12201 | } | |||
12202 | ||||
12203 | const CXXConstructorDecl * | |||
12204 | ASTContext::getCopyConstructorForExceptionObject(CXXRecordDecl *RD) { | |||
12205 | return ABI->getCopyConstructorForExceptionObject( | |||
12206 | cast<CXXRecordDecl>(RD->getFirstDecl())); | |||
12207 | } | |||
12208 | ||||
12209 | void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD, | |||
12210 | CXXConstructorDecl *CD) { | |||
12211 | return ABI->addCopyConstructorForExceptionObject( | |||
12212 | cast<CXXRecordDecl>(RD->getFirstDecl()), | |||
12213 | cast<CXXConstructorDecl>(CD->getFirstDecl())); | |||
12214 | } | |||
12215 | ||||
12216 | void ASTContext::addTypedefNameForUnnamedTagDecl(TagDecl *TD, | |||
12217 | TypedefNameDecl *DD) { | |||
12218 | return ABI->addTypedefNameForUnnamedTagDecl(TD, DD); | |||
12219 | } | |||
12220 | ||||
12221 | TypedefNameDecl * | |||
12222 | ASTContext::getTypedefNameForUnnamedTagDecl(const TagDecl *TD) { | |||
12223 | return ABI->getTypedefNameForUnnamedTagDecl(TD); | |||
12224 | } | |||
12225 | ||||
12226 | void ASTContext::addDeclaratorForUnnamedTagDecl(TagDecl *TD, | |||
12227 | DeclaratorDecl *DD) { | |||
12228 | return ABI->addDeclaratorForUnnamedTagDecl(TD, DD); | |||
12229 | } | |||
12230 | ||||
12231 | DeclaratorDecl *ASTContext::getDeclaratorForUnnamedTagDecl(const TagDecl *TD) { | |||
12232 | return ABI->getDeclaratorForUnnamedTagDecl(TD); | |||
12233 | } | |||
12234 | ||||
12235 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { | |||
12236 | ParamIndices[D] = index; | |||
12237 | } | |||
12238 | ||||
12239 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { | |||
12240 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); | |||
12241 | assert(I != ParamIndices.end() &&(static_cast <bool> (I != ParamIndices.end() && "ParmIndices lacks entry set by ParmVarDecl") ? void (0) : __assert_fail ("I != ParamIndices.end() && \"ParmIndices lacks entry set by ParmVarDecl\"" , "clang/lib/AST/ASTContext.cpp", 12242, __extension__ __PRETTY_FUNCTION__ )) | |||
12242 | "ParmIndices lacks entry set by ParmVarDecl")(static_cast <bool> (I != ParamIndices.end() && "ParmIndices lacks entry set by ParmVarDecl") ? void (0) : __assert_fail ("I != ParamIndices.end() && \"ParmIndices lacks entry set by ParmVarDecl\"" , "clang/lib/AST/ASTContext.cpp", 12242, __extension__ __PRETTY_FUNCTION__ )); | |||
12243 | return I->second; | |||
12244 | } | |||
12245 | ||||
12246 | QualType ASTContext::getStringLiteralArrayType(QualType EltTy, | |||
12247 | unsigned Length) const { | |||
12248 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). | |||
12249 | if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings) | |||
12250 | EltTy = EltTy.withConst(); | |||
12251 | ||||
12252 | EltTy = adjustStringLiteralBaseType(EltTy); | |||
12253 | ||||
12254 | // Get an array type for the string, according to C99 6.4.5. This includes | |||
12255 | // the null terminator character. | |||
12256 | return getConstantArrayType(EltTy, llvm::APInt(32, Length + 1), nullptr, | |||
12257 | ArrayType::Normal, /*IndexTypeQuals*/ 0); | |||
12258 | } | |||
12259 | ||||
12260 | StringLiteral * | |||
12261 | ASTContext::getPredefinedStringLiteralFromCache(StringRef Key) const { | |||
12262 | StringLiteral *&Result = StringLiteralCache[Key]; | |||
12263 | if (!Result) | |||
12264 | Result = StringLiteral::Create( | |||
12265 | *this, Key, StringLiteral::Ordinary, | |||
12266 | /*Pascal*/ false, getStringLiteralArrayType(CharTy, Key.size()), | |||
12267 | SourceLocation()); | |||
12268 | return Result; | |||
12269 | } | |||
12270 | ||||
12271 | MSGuidDecl * | |||
12272 | ASTContext::getMSGuidDecl(MSGuidDecl::Parts Parts) const { | |||
12273 | assert(MSGuidTagDecl && "building MS GUID without MS extensions?")(static_cast <bool> (MSGuidTagDecl && "building MS GUID without MS extensions?" ) ? void (0) : __assert_fail ("MSGuidTagDecl && \"building MS GUID without MS extensions?\"" , "clang/lib/AST/ASTContext.cpp", 12273, __extension__ __PRETTY_FUNCTION__ )); | |||
12274 | ||||
12275 | llvm::FoldingSetNodeID ID; | |||
12276 | MSGuidDecl::Profile(ID, Parts); | |||
12277 | ||||
12278 | void *InsertPos; | |||
12279 | if (MSGuidDecl *Existing = MSGuidDecls.FindNodeOrInsertPos(ID, InsertPos)) | |||
12280 | return Existing; | |||
12281 | ||||
12282 | QualType GUIDType = getMSGuidType().withConst(); | |||
12283 | MSGuidDecl *New = MSGuidDecl::Create(*this, GUIDType, Parts); | |||
12284 | MSGuidDecls.InsertNode(New, InsertPos); | |||
12285 | return New; | |||
12286 | } | |||
12287 | ||||
12288 | UnnamedGlobalConstantDecl * | |||
12289 | ASTContext::getUnnamedGlobalConstantDecl(QualType Ty, | |||
12290 | const APValue &APVal) const { | |||
12291 | llvm::FoldingSetNodeID ID; | |||
12292 | UnnamedGlobalConstantDecl::Profile(ID, Ty, APVal); | |||
12293 | ||||
12294 | void *InsertPos; | |||
12295 | if (UnnamedGlobalConstantDecl *Existing = | |||
12296 | UnnamedGlobalConstantDecls.FindNodeOrInsertPos(ID, InsertPos)) | |||
12297 | return Existing; | |||
12298 | ||||
12299 | UnnamedGlobalConstantDecl *New = | |||
12300 | UnnamedGlobalConstantDecl::Create(*this, Ty, APVal); | |||
12301 | UnnamedGlobalConstantDecls.InsertNode(New, InsertPos); | |||
12302 | return New; | |||
12303 | } | |||
12304 | ||||
12305 | TemplateParamObjectDecl * | |||
12306 | ASTContext::getTemplateParamObjectDecl(QualType T, const APValue &V) const { | |||
12307 | assert(T->isRecordType() && "template param object of unexpected type")(static_cast <bool> (T->isRecordType() && "template param object of unexpected type" ) ? void (0) : __assert_fail ("T->isRecordType() && \"template param object of unexpected type\"" , "clang/lib/AST/ASTContext.cpp", 12307, __extension__ __PRETTY_FUNCTION__ )); | |||
12308 | ||||
12309 | // C++ [temp.param]p8: | |||
12310 | // [...] a static storage duration object of type 'const T' [...] | |||
12311 | T.addConst(); | |||
12312 | ||||
12313 | llvm::FoldingSetNodeID ID; | |||
12314 | TemplateParamObjectDecl::Profile(ID, T, V); | |||
12315 | ||||
12316 | void *InsertPos; | |||
12317 | if (TemplateParamObjectDecl *Existing = | |||
12318 | TemplateParamObjectDecls.FindNodeOrInsertPos(ID, InsertPos)) | |||
12319 | return Existing; | |||
12320 | ||||
12321 | TemplateParamObjectDecl *New = TemplateParamObjectDecl::Create(*this, T, V); | |||
12322 | TemplateParamObjectDecls.InsertNode(New, InsertPos); | |||
12323 | return New; | |||
12324 | } | |||
12325 | ||||
12326 | bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { | |||
12327 | const llvm::Triple &T = getTargetInfo().getTriple(); | |||
12328 | if (!T.isOSDarwin()) | |||
12329 | return false; | |||
12330 | ||||
12331 | if (!(T.isiOS() && T.isOSVersionLT(7)) && | |||
12332 | !(T.isMacOSX() && T.isOSVersionLT(10, 9))) | |||
12333 | return false; | |||
12334 | ||||
12335 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); | |||
12336 | CharUnits sizeChars = getTypeSizeInChars(AtomicTy); | |||
12337 | uint64_t Size = sizeChars.getQuantity(); | |||
12338 | CharUnits alignChars = getTypeAlignInChars(AtomicTy); | |||
12339 | unsigned Align = alignChars.getQuantity(); | |||
12340 | unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth(); | |||
12341 | return (Size != Align || toBits(sizeChars) > MaxInlineWidthInBits); | |||
12342 | } | |||
12343 | ||||
12344 | bool | |||
12345 | ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl, | |||
12346 | const ObjCMethodDecl *MethodImpl) { | |||
12347 | // No point trying to match an unavailable/deprecated mothod. | |||
12348 | if (MethodDecl->hasAttr<UnavailableAttr>() | |||
12349 | || MethodDecl->hasAttr<DeprecatedAttr>()) | |||
12350 | return false; | |||
12351 | if (MethodDecl->getObjCDeclQualifier() != | |||
12352 | MethodImpl->getObjCDeclQualifier()) | |||
12353 | return false; | |||
12354 | if (!hasSameType(MethodDecl->getReturnType(), MethodImpl->getReturnType())) | |||
12355 | return false; | |||
12356 | ||||
12357 | if (MethodDecl->param_size() != MethodImpl->param_size()) | |||
12358 | return false; | |||
12359 | ||||
12360 | for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(), | |||
12361 | IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(), | |||
12362 | EF = MethodDecl->param_end(); | |||
12363 | IM != EM && IF != EF; ++IM, ++IF) { | |||
12364 | const ParmVarDecl *DeclVar = (*IF); | |||
12365 | const ParmVarDecl *ImplVar = (*IM); | |||
12366 | if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier()) | |||
12367 | return false; | |||
12368 | if (!hasSameType(DeclVar->getType(), ImplVar->getType())) | |||
12369 | return false; | |||
12370 | } | |||
12371 | ||||
12372 | return (MethodDecl->isVariadic() == MethodImpl->isVariadic()); | |||
12373 | } | |||
12374 | ||||
12375 | uint64_t ASTContext::getTargetNullPointerValue(QualType QT) const { | |||
12376 | LangAS AS; | |||
12377 | if (QT->getUnqualifiedDesugaredType()->isNullPtrType()) | |||
12378 | AS = LangAS::Default; | |||
12379 | else | |||
12380 | AS = QT->getPointeeType().getAddressSpace(); | |||
12381 | ||||
12382 | return getTargetInfo().getNullPointerValue(AS); | |||
12383 | } | |||
12384 | ||||
12385 | unsigned ASTContext::getTargetAddressSpace(LangAS AS) const { | |||
12386 | return getTargetInfo().getTargetAddressSpace(AS); | |||
12387 | } | |||
12388 | ||||
12389 | bool ASTContext::hasSameExpr(const Expr *X, const Expr *Y) const { | |||
12390 | if (X == Y) | |||
12391 | return true; | |||
12392 | if (!X || !Y) | |||
12393 | return false; | |||
12394 | llvm::FoldingSetNodeID IDX, IDY; | |||
12395 | X->Profile(IDX, *this, /*Canonical=*/true); | |||
12396 | Y->Profile(IDY, *this, /*Canonical=*/true); | |||
12397 | return IDX == IDY; | |||
12398 | } | |||
12399 | ||||
12400 | // The getCommon* helpers return, for given 'same' X and Y entities given as | |||
12401 | // inputs, another entity which is also the 'same' as the inputs, but which | |||
12402 | // is closer to the canonical form of the inputs, each according to a given | |||
12403 | // criteria. | |||
12404 | // The getCommon*Checked variants are 'null inputs not-allowed' equivalents of | |||
12405 | // the regular ones. | |||
12406 | ||||
12407 | static Decl *getCommonDecl(Decl *X, Decl *Y) { | |||
12408 | if (!declaresSameEntity(X, Y)) | |||
12409 | return nullptr; | |||
12410 | for (const Decl *DX : X->redecls()) { | |||
12411 | // If we reach Y before reaching the first decl, that means X is older. | |||
12412 | if (DX == Y) | |||
12413 | return X; | |||
12414 | // If we reach the first decl, then Y is older. | |||
12415 | if (DX->isFirstDecl()) | |||
12416 | return Y; | |||
12417 | } | |||
12418 | llvm_unreachable("Corrupt redecls chain")::llvm::llvm_unreachable_internal("Corrupt redecls chain", "clang/lib/AST/ASTContext.cpp" , 12418); | |||
12419 | } | |||
12420 | ||||
12421 | template <class T, std::enable_if_t<std::is_base_of_v<Decl, T>, bool> = true> | |||
12422 | static T *getCommonDecl(T *X, T *Y) { | |||
12423 | return cast_or_null<T>( | |||
12424 | getCommonDecl(const_cast<Decl *>(cast_or_null<Decl>(X)), | |||
12425 | const_cast<Decl *>(cast_or_null<Decl>(Y)))); | |||
12426 | } | |||
12427 | ||||
12428 | template <class T, std::enable_if_t<std::is_base_of_v<Decl, T>, bool> = true> | |||
12429 | static T *getCommonDeclChecked(T *X, T *Y) { | |||
12430 | return cast<T>(getCommonDecl(const_cast<Decl *>(cast<Decl>(X)), | |||
12431 | const_cast<Decl *>(cast<Decl>(Y)))); | |||
12432 | } | |||
12433 | ||||
12434 | static TemplateName getCommonTemplateName(ASTContext &Ctx, TemplateName X, | |||
12435 | TemplateName Y) { | |||
12436 | if (X.getAsVoidPointer() == Y.getAsVoidPointer()) | |||
12437 | return X; | |||
12438 | // FIXME: There are cases here where we could find a common template name | |||
12439 | // with more sugar. For example one could be a SubstTemplateTemplate* | |||
12440 | // replacing the other. | |||
12441 | TemplateName CX = Ctx.getCanonicalTemplateName(X); | |||
12442 | if (CX.getAsVoidPointer() != | |||
12443 | Ctx.getCanonicalTemplateName(Y).getAsVoidPointer()) | |||
12444 | return TemplateName(); | |||
12445 | return CX; | |||
12446 | } | |||
12447 | ||||
12448 | static TemplateName | |||
12449 | getCommonTemplateNameChecked(ASTContext &Ctx, TemplateName X, TemplateName Y) { | |||
12450 | TemplateName R = getCommonTemplateName(Ctx, X, Y); | |||
12451 | assert(R.getAsVoidPointer() != nullptr)(static_cast <bool> (R.getAsVoidPointer() != nullptr) ? void (0) : __assert_fail ("R.getAsVoidPointer() != nullptr", "clang/lib/AST/ASTContext.cpp", 12451, __extension__ __PRETTY_FUNCTION__ )); | |||
12452 | return R; | |||
12453 | } | |||
12454 | ||||
12455 | static auto getCommonTypes(ASTContext &Ctx, ArrayRef<QualType> Xs, | |||
12456 | ArrayRef<QualType> Ys, bool Unqualified = false) { | |||
12457 | assert(Xs.size() == Ys.size())(static_cast <bool> (Xs.size() == Ys.size()) ? void (0) : __assert_fail ("Xs.size() == Ys.size()", "clang/lib/AST/ASTContext.cpp" , 12457, __extension__ __PRETTY_FUNCTION__)); | |||
12458 | SmallVector<QualType, 8> Rs(Xs.size()); | |||
12459 | for (size_t I = 0; I < Rs.size(); ++I) | |||
12460 | Rs[I] = Ctx.getCommonSugaredType(Xs[I], Ys[I], Unqualified); | |||
12461 | return Rs; | |||
12462 | } | |||
12463 | ||||
12464 | template <class T> | |||
12465 | static SourceLocation getCommonAttrLoc(const T *X, const T *Y) { | |||
12466 | return X->getAttributeLoc() == Y->getAttributeLoc() ? X->getAttributeLoc() | |||
12467 | : SourceLocation(); | |||
12468 | } | |||
12469 | ||||
12470 | static TemplateArgument getCommonTemplateArgument(ASTContext &Ctx, | |||
12471 | const TemplateArgument &X, | |||
12472 | const TemplateArgument &Y) { | |||
12473 | if (X.getKind() != Y.getKind()) | |||
12474 | return TemplateArgument(); | |||
12475 | ||||
12476 | switch (X.getKind()) { | |||
12477 | case TemplateArgument::ArgKind::Type: | |||
12478 | if (!Ctx.hasSameType(X.getAsType(), Y.getAsType())) | |||
12479 | return TemplateArgument(); | |||
12480 | return TemplateArgument( | |||
12481 | Ctx.getCommonSugaredType(X.getAsType(), Y.getAsType())); | |||
12482 | case TemplateArgument::ArgKind::NullPtr: | |||
12483 | if (!Ctx.hasSameType(X.getNullPtrType(), Y.getNullPtrType())) | |||
12484 | return TemplateArgument(); | |||
12485 | return TemplateArgument( | |||
12486 | Ctx.getCommonSugaredType(X.getNullPtrType(), Y.getNullPtrType()), | |||
12487 | /*Unqualified=*/true); | |||
12488 | case TemplateArgument::ArgKind::Expression: | |||
12489 | if (!Ctx.hasSameType(X.getAsExpr()->getType(), Y.getAsExpr()->getType())) | |||
12490 | return TemplateArgument(); | |||
12491 | // FIXME: Try to keep the common sugar. | |||
12492 | return X; | |||
12493 | case TemplateArgument::ArgKind::Template: { | |||
12494 | TemplateName TX = X.getAsTemplate(), TY = Y.getAsTemplate(); | |||
12495 | TemplateName CTN = ::getCommonTemplateName(Ctx, TX, TY); | |||
12496 | if (!CTN.getAsVoidPointer()) | |||
12497 | return TemplateArgument(); | |||
12498 | return TemplateArgument(CTN); | |||
12499 | } | |||
12500 | case TemplateArgument::ArgKind::TemplateExpansion: { | |||
12501 | TemplateName TX = X.getAsTemplateOrTemplatePattern(), | |||
12502 | TY = Y.getAsTemplateOrTemplatePattern(); | |||
12503 | TemplateName CTN = ::getCommonTemplateName(Ctx, TX, TY); | |||
12504 | if (!CTN.getAsVoidPointer()) | |||
12505 | return TemplateName(); | |||
12506 | auto NExpX = X.getNumTemplateExpansions(); | |||
12507 | assert(NExpX == Y.getNumTemplateExpansions())(static_cast <bool> (NExpX == Y.getNumTemplateExpansions ()) ? void (0) : __assert_fail ("NExpX == Y.getNumTemplateExpansions()" , "clang/lib/AST/ASTContext.cpp", 12507, __extension__ __PRETTY_FUNCTION__ )); | |||
12508 | return TemplateArgument(CTN, NExpX); | |||
12509 | } | |||
12510 | default: | |||
12511 | // FIXME: Handle the other argument kinds. | |||
12512 | return X; | |||
12513 | } | |||
12514 | } | |||
12515 | ||||
12516 | static bool getCommonTemplateArguments(ASTContext &Ctx, | |||
12517 | SmallVectorImpl<TemplateArgument> &R, | |||
12518 | ArrayRef<TemplateArgument> Xs, | |||
12519 | ArrayRef<TemplateArgument> Ys) { | |||
12520 | if (Xs.size() != Ys.size()) | |||
12521 | return true; | |||
12522 | R.resize(Xs.size()); | |||
12523 | for (size_t I = 0; I < R.size(); ++I) { | |||
12524 | R[I] = getCommonTemplateArgument(Ctx, Xs[I], Ys[I]); | |||
12525 | if (R[I].isNull()) | |||
12526 | return true; | |||
12527 | } | |||
12528 | return false; | |||
12529 | } | |||
12530 | ||||
12531 | static auto getCommonTemplateArguments(ASTContext &Ctx, | |||
12532 | ArrayRef<TemplateArgument> Xs, | |||
12533 | ArrayRef<TemplateArgument> Ys) { | |||
12534 | SmallVector<TemplateArgument, 8> R; | |||
12535 | bool Different = getCommonTemplateArguments(Ctx, R, Xs, Ys); | |||
12536 | assert(!Different)(static_cast <bool> (!Different) ? void (0) : __assert_fail ("!Different", "clang/lib/AST/ASTContext.cpp", 12536, __extension__ __PRETTY_FUNCTION__)); | |||
12537 | (void)Different; | |||
12538 | return R; | |||
12539 | } | |||
12540 | ||||
12541 | template <class T> | |||
12542 | static ElaboratedTypeKeyword getCommonTypeKeyword(const T *X, const T *Y) { | |||
12543 | return X->getKeyword() == Y->getKeyword() ? X->getKeyword() | |||
12544 | : ElaboratedTypeKeyword::ETK_None; | |||
12545 | } | |||
12546 | ||||
12547 | template <class T> | |||
12548 | static NestedNameSpecifier *getCommonNNS(ASTContext &Ctx, const T *X, | |||
12549 | const T *Y) { | |||
12550 | // FIXME: Try to keep the common NNS sugar. | |||
12551 | return X->getQualifier() == Y->getQualifier() | |||
12552 | ? X->getQualifier() | |||
12553 | : Ctx.getCanonicalNestedNameSpecifier(X->getQualifier()); | |||
12554 | } | |||
12555 | ||||
12556 | template <class T> | |||
12557 | static QualType getCommonElementType(ASTContext &Ctx, const T *X, const T *Y) { | |||
12558 | return Ctx.getCommonSugaredType(X->getElementType(), Y->getElementType()); | |||
12559 | } | |||
12560 | ||||
12561 | template <class T> | |||
12562 | static QualType getCommonArrayElementType(ASTContext &Ctx, const T *X, | |||
12563 | Qualifiers &QX, const T *Y, | |||
12564 | Qualifiers &QY) { | |||
12565 | QualType EX = X->getElementType(), EY = Y->getElementType(); | |||
12566 | QualType R = Ctx.getCommonSugaredType(EX, EY, | |||
12567 | /*Unqualified=*/true); | |||
12568 | Qualifiers RQ = R.getQualifiers(); | |||
12569 | QX += EX.getQualifiers() - RQ; | |||
12570 | QY += EY.getQualifiers() - RQ; | |||
12571 | return R; | |||
12572 | } | |||
12573 | ||||
12574 | template <class T> | |||
12575 | static QualType getCommonPointeeType(ASTContext &Ctx, const T *X, const T *Y) { | |||
12576 | return Ctx.getCommonSugaredType(X->getPointeeType(), Y->getPointeeType()); | |||
12577 | } | |||
12578 | ||||
12579 | template <class T> static auto *getCommonSizeExpr(ASTContext &Ctx, T *X, T *Y) { | |||
12580 | assert(Ctx.hasSameExpr(X->getSizeExpr(), Y->getSizeExpr()))(static_cast <bool> (Ctx.hasSameExpr(X->getSizeExpr( ), Y->getSizeExpr())) ? void (0) : __assert_fail ("Ctx.hasSameExpr(X->getSizeExpr(), Y->getSizeExpr())" , "clang/lib/AST/ASTContext.cpp", 12580, __extension__ __PRETTY_FUNCTION__ )); | |||
12581 | return X->getSizeExpr(); | |||
12582 | } | |||
12583 | ||||
12584 | static auto getCommonSizeModifier(const ArrayType *X, const ArrayType *Y) { | |||
12585 | assert(X->getSizeModifier() == Y->getSizeModifier())(static_cast <bool> (X->getSizeModifier() == Y->getSizeModifier ()) ? void (0) : __assert_fail ("X->getSizeModifier() == Y->getSizeModifier()" , "clang/lib/AST/ASTContext.cpp", 12585, __extension__ __PRETTY_FUNCTION__ )); | |||
12586 | return X->getSizeModifier(); | |||
12587 | } | |||
12588 | ||||
12589 | static auto getCommonIndexTypeCVRQualifiers(const ArrayType *X, | |||
12590 | const ArrayType *Y) { | |||
12591 | assert(X->getIndexTypeCVRQualifiers() == Y->getIndexTypeCVRQualifiers())(static_cast <bool> (X->getIndexTypeCVRQualifiers() == Y->getIndexTypeCVRQualifiers()) ? void (0) : __assert_fail ("X->getIndexTypeCVRQualifiers() == Y->getIndexTypeCVRQualifiers()" , "clang/lib/AST/ASTContext.cpp", 12591, __extension__ __PRETTY_FUNCTION__ )); | |||
12592 | return X->getIndexTypeCVRQualifiers(); | |||
12593 | } | |||
12594 | ||||
12595 | // Merges two type lists such that the resulting vector will contain | |||
12596 | // each type (in a canonical sense) only once, in the order they appear | |||
12597 | // from X to Y. If they occur in both X and Y, the result will contain | |||
12598 | // the common sugared type between them. | |||
12599 | static void mergeTypeLists(ASTContext &Ctx, SmallVectorImpl<QualType> &Out, | |||
12600 | ArrayRef<QualType> X, ArrayRef<QualType> Y) { | |||
12601 | llvm::DenseMap<QualType, unsigned> Found; | |||
12602 | for (auto Ts : {X, Y}) { | |||
12603 | for (QualType T : Ts) { | |||
12604 | auto Res = Found.try_emplace(Ctx.getCanonicalType(T), Out.size()); | |||
12605 | if (!Res.second) { | |||
12606 | QualType &U = Out[Res.first->second]; | |||
12607 | U = Ctx.getCommonSugaredType(U, T); | |||
12608 | } else { | |||
12609 | Out.emplace_back(T); | |||
12610 | } | |||
12611 | } | |||
12612 | } | |||
12613 | } | |||
12614 | ||||
12615 | FunctionProtoType::ExceptionSpecInfo | |||
12616 | ASTContext::mergeExceptionSpecs(FunctionProtoType::ExceptionSpecInfo ESI1, | |||
12617 | FunctionProtoType::ExceptionSpecInfo ESI2, | |||
12618 | SmallVectorImpl<QualType> &ExceptionTypeStorage, | |||
12619 | bool AcceptDependent) { | |||
12620 | ExceptionSpecificationType EST1 = ESI1.Type, EST2 = ESI2.Type; | |||
12621 | ||||
12622 | // If either of them can throw anything, that is the result. | |||
12623 | for (auto I : {EST_None, EST_MSAny, EST_NoexceptFalse}) { | |||
12624 | if (EST1 == I) | |||
12625 | return ESI1; | |||
12626 | if (EST2 == I) | |||
12627 | return ESI2; | |||
12628 | } | |||
12629 | ||||
12630 | // If either of them is non-throwing, the result is the other. | |||
12631 | for (auto I : | |||
12632 | {EST_NoThrow, EST_DynamicNone, EST_BasicNoexcept, EST_NoexceptTrue}) { | |||
12633 | if (EST1 == I) | |||
12634 | return ESI2; | |||
12635 | if (EST2 == I) | |||
12636 | return ESI1; | |||
12637 | } | |||
12638 | ||||
12639 | // If we're left with value-dependent computed noexcept expressions, we're | |||
12640 | // stuck. Before C++17, we can just drop the exception specification entirely, | |||
12641 | // since it's not actually part of the canonical type. And this should never | |||
12642 | // happen in C++17, because it would mean we were computing the composite | |||
12643 | // pointer type of dependent types, which should never happen. | |||
12644 | if (EST1 == EST_DependentNoexcept || EST2 == EST_DependentNoexcept) { | |||
12645 | assert(AcceptDependent &&(static_cast <bool> (AcceptDependent && "computing composite pointer type of dependent types" ) ? void (0) : __assert_fail ("AcceptDependent && \"computing composite pointer type of dependent types\"" , "clang/lib/AST/ASTContext.cpp", 12646, __extension__ __PRETTY_FUNCTION__ )) | |||
12646 | "computing composite pointer type of dependent types")(static_cast <bool> (AcceptDependent && "computing composite pointer type of dependent types" ) ? void (0) : __assert_fail ("AcceptDependent && \"computing composite pointer type of dependent types\"" , "clang/lib/AST/ASTContext.cpp", 12646, __extension__ __PRETTY_FUNCTION__ )); | |||
12647 | return FunctionProtoType::ExceptionSpecInfo(); | |||
12648 | } | |||
12649 | ||||
12650 | // Switch over the possibilities so that people adding new values know to | |||
12651 | // update this function. | |||
12652 | switch (EST1) { | |||
12653 | case EST_None: | |||
12654 | case EST_DynamicNone: | |||
12655 | case EST_MSAny: | |||
12656 | case EST_BasicNoexcept: | |||
12657 | case EST_DependentNoexcept: | |||
12658 | case EST_NoexceptFalse: | |||
12659 | case EST_NoexceptTrue: | |||
12660 | case EST_NoThrow: | |||
12661 | llvm_unreachable("These ESTs should be handled above")::llvm::llvm_unreachable_internal("These ESTs should be handled above" , "clang/lib/AST/ASTContext.cpp", 12661); | |||
12662 | ||||
12663 | case EST_Dynamic: { | |||
12664 | // This is the fun case: both exception specifications are dynamic. Form | |||
12665 | // the union of the two lists. | |||
12666 | assert(EST2 == EST_Dynamic && "other cases should already be handled")(static_cast <bool> (EST2 == EST_Dynamic && "other cases should already be handled" ) ? void (0) : __assert_fail ("EST2 == EST_Dynamic && \"other cases should already be handled\"" , "clang/lib/AST/ASTContext.cpp", 12666, __extension__ __PRETTY_FUNCTION__ )); | |||
12667 | mergeTypeLists(*this, ExceptionTypeStorage, ESI1.Exceptions, | |||
12668 | ESI2.Exceptions); | |||
12669 | FunctionProtoType::ExceptionSpecInfo Result(EST_Dynamic); | |||
12670 | Result.Exceptions = ExceptionTypeStorage; | |||
12671 | return Result; | |||
12672 | } | |||
12673 | ||||
12674 | case EST_Unevaluated: | |||
12675 | case EST_Uninstantiated: | |||
12676 | case EST_Unparsed: | |||
12677 | llvm_unreachable("shouldn't see unresolved exception specifications here")::llvm::llvm_unreachable_internal("shouldn't see unresolved exception specifications here" , "clang/lib/AST/ASTContext.cpp", 12677); | |||
12678 | } | |||
12679 | ||||
12680 | llvm_unreachable("invalid ExceptionSpecificationType")::llvm::llvm_unreachable_internal("invalid ExceptionSpecificationType" , "clang/lib/AST/ASTContext.cpp", 12680); | |||
12681 | } | |||
12682 | ||||
12683 | static QualType getCommonNonSugarTypeNode(ASTContext &Ctx, const Type *X, | |||
12684 | Qualifiers &QX, const Type *Y, | |||
12685 | Qualifiers &QY) { | |||
12686 | Type::TypeClass TC = X->getTypeClass(); | |||
12687 | assert(TC == Y->getTypeClass())(static_cast <bool> (TC == Y->getTypeClass()) ? void (0) : __assert_fail ("TC == Y->getTypeClass()", "clang/lib/AST/ASTContext.cpp" , 12687, __extension__ __PRETTY_FUNCTION__)); | |||
12688 | switch (TC) { | |||
12689 | #define UNEXPECTED_TYPE(Class, Kind) \ | |||
12690 | case Type::Class: \ | |||
12691 | llvm_unreachable("Unexpected " Kind ": " #Class)::llvm::llvm_unreachable_internal("Unexpected " Kind ": " #Class , "clang/lib/AST/ASTContext.cpp", 12691); | |||
12692 | ||||
12693 | #define NON_CANONICAL_TYPE(Class, Base) UNEXPECTED_TYPE(Class, "non-canonical") | |||
12694 | #define TYPE(Class, Base) | |||
12695 | #include "clang/AST/TypeNodes.inc" | |||
12696 | ||||
12697 | #define SUGAR_FREE_TYPE(Class) UNEXPECTED_TYPE(Class, "sugar-free") | |||
12698 | SUGAR_FREE_TYPE(Builtin) | |||
12699 | SUGAR_FREE_TYPE(Decltype) | |||
12700 | SUGAR_FREE_TYPE(DeducedTemplateSpecialization) | |||
12701 | SUGAR_FREE_TYPE(DependentBitInt) | |||
12702 | SUGAR_FREE_TYPE(Enum) | |||
12703 | SUGAR_FREE_TYPE(BitInt) | |||
12704 | SUGAR_FREE_TYPE(ObjCInterface) | |||
12705 | SUGAR_FREE_TYPE(Record) | |||
12706 | SUGAR_FREE_TYPE(SubstTemplateTypeParmPack) | |||
12707 | SUGAR_FREE_TYPE(UnresolvedUsing) | |||
12708 | #undef SUGAR_FREE_TYPE | |||
12709 | #define NON_UNIQUE_TYPE(Class) UNEXPECTED_TYPE(Class, "non-unique") | |||
12710 | NON_UNIQUE_TYPE(TypeOfExpr) | |||
12711 | NON_UNIQUE_TYPE(VariableArray) | |||
12712 | #undef NON_UNIQUE_TYPE | |||
12713 | ||||
12714 | UNEXPECTED_TYPE(TypeOf, "sugar") | |||
12715 | ||||
12716 | #undef UNEXPECTED_TYPE | |||
12717 | ||||
12718 | case Type::Auto: { | |||
12719 | const auto *AX = cast<AutoType>(X), *AY = cast<AutoType>(Y); | |||
12720 | assert(AX->getDeducedType().isNull())(static_cast <bool> (AX->getDeducedType().isNull()) ? void (0) : __assert_fail ("AX->getDeducedType().isNull()" , "clang/lib/AST/ASTContext.cpp", 12720, __extension__ __PRETTY_FUNCTION__ )); | |||
12721 | assert(AY->getDeducedType().isNull())(static_cast <bool> (AY->getDeducedType().isNull()) ? void (0) : __assert_fail ("AY->getDeducedType().isNull()" , "clang/lib/AST/ASTContext.cpp", 12721, __extension__ __PRETTY_FUNCTION__ )); | |||
12722 | assert(AX->getKeyword() == AY->getKeyword())(static_cast <bool> (AX->getKeyword() == AY->getKeyword ()) ? void (0) : __assert_fail ("AX->getKeyword() == AY->getKeyword()" , "clang/lib/AST/ASTContext.cpp", 12722, __extension__ __PRETTY_FUNCTION__ )); | |||
12723 | assert(AX->isInstantiationDependentType() ==(static_cast <bool> (AX->isInstantiationDependentType () == AY->isInstantiationDependentType()) ? void (0) : __assert_fail ("AX->isInstantiationDependentType() == AY->isInstantiationDependentType()" , "clang/lib/AST/ASTContext.cpp", 12724, __extension__ __PRETTY_FUNCTION__ )) | |||
12724 | AY->isInstantiationDependentType())(static_cast <bool> (AX->isInstantiationDependentType () == AY->isInstantiationDependentType()) ? void (0) : __assert_fail ("AX->isInstantiationDependentType() == AY->isInstantiationDependentType()" , "clang/lib/AST/ASTContext.cpp", 12724, __extension__ __PRETTY_FUNCTION__ )); | |||
12725 | auto As = getCommonTemplateArguments(Ctx, AX->getTypeConstraintArguments(), | |||
12726 | AY->getTypeConstraintArguments()); | |||
12727 | return Ctx.getAutoType(QualType(), AX->getKeyword(), | |||
12728 | AX->isInstantiationDependentType(), | |||
12729 | AX->containsUnexpandedParameterPack(), | |||
12730 | getCommonDeclChecked(AX->getTypeConstraintConcept(), | |||
12731 | AY->getTypeConstraintConcept()), | |||
12732 | As); | |||
12733 | } | |||
12734 | case Type::IncompleteArray: { | |||
12735 | const auto *AX = cast<IncompleteArrayType>(X), | |||
12736 | *AY = cast<IncompleteArrayType>(Y); | |||
12737 | return Ctx.getIncompleteArrayType( | |||
12738 | getCommonArrayElementType(Ctx, AX, QX, AY, QY), | |||
12739 | getCommonSizeModifier(AX, AY), getCommonIndexTypeCVRQualifiers(AX, AY)); | |||
12740 | } | |||
12741 | case Type::DependentSizedArray: { | |||
12742 | const auto *AX = cast<DependentSizedArrayType>(X), | |||
12743 | *AY = cast<DependentSizedArrayType>(Y); | |||
12744 | return Ctx.getDependentSizedArrayType( | |||
12745 | getCommonArrayElementType(Ctx, AX, QX, AY, QY), | |||
12746 | getCommonSizeExpr(Ctx, AX, AY), getCommonSizeModifier(AX, AY), | |||
12747 | getCommonIndexTypeCVRQualifiers(AX, AY), | |||
12748 | AX->getBracketsRange() == AY->getBracketsRange() | |||
12749 | ? AX->getBracketsRange() | |||
12750 | : SourceRange()); | |||
12751 | } | |||
12752 | case Type::ConstantArray: { | |||
12753 | const auto *AX = cast<ConstantArrayType>(X), | |||
12754 | *AY = cast<ConstantArrayType>(Y); | |||
12755 | assert(AX->getSize() == AY->getSize())(static_cast <bool> (AX->getSize() == AY->getSize ()) ? void (0) : __assert_fail ("AX->getSize() == AY->getSize()" , "clang/lib/AST/ASTContext.cpp", 12755, __extension__ __PRETTY_FUNCTION__ )); | |||
12756 | const Expr *SizeExpr = Ctx.hasSameExpr(AX->getSizeExpr(), AY->getSizeExpr()) | |||
12757 | ? AX->getSizeExpr() | |||
12758 | : nullptr; | |||
12759 | return Ctx.getConstantArrayType( | |||
12760 | getCommonArrayElementType(Ctx, AX, QX, AY, QY), AX->getSize(), SizeExpr, | |||
12761 | getCommonSizeModifier(AX, AY), getCommonIndexTypeCVRQualifiers(AX, AY)); | |||
12762 | } | |||
12763 | case Type::Atomic: { | |||
12764 | const auto *AX = cast<AtomicType>(X), *AY = cast<AtomicType>(Y); | |||
12765 | return Ctx.getAtomicType( | |||
12766 | Ctx.getCommonSugaredType(AX->getValueType(), AY->getValueType())); | |||
12767 | } | |||
12768 | case Type::Complex: { | |||
12769 | const auto *CX = cast<ComplexType>(X), *CY = cast<ComplexType>(Y); | |||
12770 | return Ctx.getComplexType(getCommonArrayElementType(Ctx, CX, QX, CY, QY)); | |||
12771 | } | |||
12772 | case Type::Pointer: { | |||
12773 | const auto *PX = cast<PointerType>(X), *PY = cast<PointerType>(Y); | |||
12774 | return Ctx.getPointerType(getCommonPointeeType(Ctx, PX, PY)); | |||
12775 | } | |||
12776 | case Type::BlockPointer: { | |||
12777 | const auto *PX = cast<BlockPointerType>(X), *PY = cast<BlockPointerType>(Y); | |||
12778 | return Ctx.getBlockPointerType(getCommonPointeeType(Ctx, PX, PY)); | |||
12779 | } | |||
12780 | case Type::ObjCObjectPointer: { | |||
12781 | const auto *PX = cast<ObjCObjectPointerType>(X), | |||
12782 | *PY = cast<ObjCObjectPointerType>(Y); | |||
12783 | return Ctx.getObjCObjectPointerType(getCommonPointeeType(Ctx, PX, PY)); | |||
12784 | } | |||
12785 | case Type::MemberPointer: { | |||
12786 | const auto *PX = cast<MemberPointerType>(X), | |||
12787 | *PY = cast<MemberPointerType>(Y); | |||
12788 | return Ctx.getMemberPointerType( | |||
12789 | getCommonPointeeType(Ctx, PX, PY), | |||
12790 | Ctx.getCommonSugaredType(QualType(PX->getClass(), 0), | |||
12791 | QualType(PY->getClass(), 0)) | |||
12792 | .getTypePtr()); | |||
12793 | } | |||
12794 | case Type::LValueReference: { | |||
12795 | const auto *PX = cast<LValueReferenceType>(X), | |||
12796 | *PY = cast<LValueReferenceType>(Y); | |||
12797 | // FIXME: Preserve PointeeTypeAsWritten. | |||
12798 | return Ctx.getLValueReferenceType(getCommonPointeeType(Ctx, PX, PY), | |||
12799 | PX->isSpelledAsLValue() || | |||
12800 | PY->isSpelledAsLValue()); | |||
12801 | } | |||
12802 | case Type::RValueReference: { | |||
12803 | const auto *PX = cast<RValueReferenceType>(X), | |||
12804 | *PY = cast<RValueReferenceType>(Y); | |||
12805 | // FIXME: Preserve PointeeTypeAsWritten. | |||
12806 | return Ctx.getRValueReferenceType(getCommonPointeeType(Ctx, PX, PY)); | |||
12807 | } | |||
12808 | case Type::DependentAddressSpace: { | |||
12809 | const auto *PX = cast<DependentAddressSpaceType>(X), | |||
12810 | *PY = cast<DependentAddressSpaceType>(Y); | |||
12811 | assert(Ctx.hasSameExpr(PX->getAddrSpaceExpr(), PY->getAddrSpaceExpr()))(static_cast <bool> (Ctx.hasSameExpr(PX->getAddrSpaceExpr (), PY->getAddrSpaceExpr())) ? void (0) : __assert_fail ("Ctx.hasSameExpr(PX->getAddrSpaceExpr(), PY->getAddrSpaceExpr())" , "clang/lib/AST/ASTContext.cpp", 12811, __extension__ __PRETTY_FUNCTION__ )); | |||
12812 | return Ctx.getDependentAddressSpaceType(getCommonPointeeType(Ctx, PX, PY), | |||
12813 | PX->getAddrSpaceExpr(), | |||
12814 | getCommonAttrLoc(PX, PY)); | |||
12815 | } | |||
12816 | case Type::FunctionNoProto: { | |||
12817 | const auto *FX = cast<FunctionNoProtoType>(X), | |||
12818 | *FY = cast<FunctionNoProtoType>(Y); | |||
12819 | assert(FX->getExtInfo() == FY->getExtInfo())(static_cast <bool> (FX->getExtInfo() == FY->getExtInfo ()) ? void (0) : __assert_fail ("FX->getExtInfo() == FY->getExtInfo()" , "clang/lib/AST/ASTContext.cpp", 12819, __extension__ __PRETTY_FUNCTION__ )); | |||
12820 | return Ctx.getFunctionNoProtoType( | |||
12821 | Ctx.getCommonSugaredType(FX->getReturnType(), FY->getReturnType()), | |||
12822 | FX->getExtInfo()); | |||
12823 | } | |||
12824 | case Type::FunctionProto: { | |||
12825 | const auto *FX = cast<FunctionProtoType>(X), | |||
12826 | *FY = cast<FunctionProtoType>(Y); | |||
12827 | FunctionProtoType::ExtProtoInfo EPIX = FX->getExtProtoInfo(), | |||
12828 | EPIY = FY->getExtProtoInfo(); | |||
12829 | assert(EPIX.ExtInfo == EPIY.ExtInfo)(static_cast <bool> (EPIX.ExtInfo == EPIY.ExtInfo) ? void (0) : __assert_fail ("EPIX.ExtInfo == EPIY.ExtInfo", "clang/lib/AST/ASTContext.cpp" , 12829, __extension__ __PRETTY_FUNCTION__)); | |||
12830 | assert(EPIX.ExtParameterInfos == EPIY.ExtParameterInfos)(static_cast <bool> (EPIX.ExtParameterInfos == EPIY.ExtParameterInfos ) ? void (0) : __assert_fail ("EPIX.ExtParameterInfos == EPIY.ExtParameterInfos" , "clang/lib/AST/ASTContext.cpp", 12830, __extension__ __PRETTY_FUNCTION__ )); | |||
12831 | assert(EPIX.RefQualifier == EPIY.RefQualifier)(static_cast <bool> (EPIX.RefQualifier == EPIY.RefQualifier ) ? void (0) : __assert_fail ("EPIX.RefQualifier == EPIY.RefQualifier" , "clang/lib/AST/ASTContext.cpp", 12831, __extension__ __PRETTY_FUNCTION__ )); | |||
12832 | assert(EPIX.TypeQuals == EPIY.TypeQuals)(static_cast <bool> (EPIX.TypeQuals == EPIY.TypeQuals) ? void (0) : __assert_fail ("EPIX.TypeQuals == EPIY.TypeQuals" , "clang/lib/AST/ASTContext.cpp", 12832, __extension__ __PRETTY_FUNCTION__ )); | |||
12833 | assert(EPIX.Variadic == EPIY.Variadic)(static_cast <bool> (EPIX.Variadic == EPIY.Variadic) ? void (0) : __assert_fail ("EPIX.Variadic == EPIY.Variadic", "clang/lib/AST/ASTContext.cpp" , 12833, __extension__ __PRETTY_FUNCTION__)); | |||
12834 | ||||
12835 | // FIXME: Can we handle an empty EllipsisLoc? | |||
12836 | // Use emtpy EllipsisLoc if X and Y differ. | |||
12837 | ||||
12838 | EPIX.HasTrailingReturn = EPIX.HasTrailingReturn && EPIY.HasTrailingReturn; | |||
12839 | ||||
12840 | QualType R = | |||
12841 | Ctx.getCommonSugaredType(FX->getReturnType(), FY->getReturnType()); | |||
12842 | auto P = getCommonTypes(Ctx, FX->param_types(), FY->param_types(), | |||
12843 | /*Unqualified=*/true); | |||
12844 | ||||
12845 | SmallVector<QualType, 8> Exceptions; | |||
12846 | EPIX.ExceptionSpec = Ctx.mergeExceptionSpecs( | |||
12847 | EPIX.ExceptionSpec, EPIY.ExceptionSpec, Exceptions, true); | |||
12848 | return Ctx.getFunctionType(R, P, EPIX); | |||
12849 | } | |||
12850 | case Type::ObjCObject: { | |||
12851 | const auto *OX = cast<ObjCObjectType>(X), *OY = cast<ObjCObjectType>(Y); | |||
12852 | assert((static_cast <bool> (std::equal(OX->getProtocols().begin (), OX->getProtocols().end(), OY->getProtocols().begin( ), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0 , const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl () == P1->getCanonicalDecl(); }) && "protocol lists must be the same" ) ? void (0) : __assert_fail ("std::equal(OX->getProtocols().begin(), OX->getProtocols().end(), OY->getProtocols().begin(), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0, const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl() == P1->getCanonicalDecl(); }) && \"protocol lists must be the same\"" , "clang/lib/AST/ASTContext.cpp", 12858, __extension__ __PRETTY_FUNCTION__ )) | |||
12853 | std::equal(OX->getProtocols().begin(), OX->getProtocols().end(),(static_cast <bool> (std::equal(OX->getProtocols().begin (), OX->getProtocols().end(), OY->getProtocols().begin( ), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0 , const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl () == P1->getCanonicalDecl(); }) && "protocol lists must be the same" ) ? void (0) : __assert_fail ("std::equal(OX->getProtocols().begin(), OX->getProtocols().end(), OY->getProtocols().begin(), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0, const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl() == P1->getCanonicalDecl(); }) && \"protocol lists must be the same\"" , "clang/lib/AST/ASTContext.cpp", 12858, __extension__ __PRETTY_FUNCTION__ )) | |||
12854 | OY->getProtocols().begin(), OY->getProtocols().end(),(static_cast <bool> (std::equal(OX->getProtocols().begin (), OX->getProtocols().end(), OY->getProtocols().begin( ), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0 , const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl () == P1->getCanonicalDecl(); }) && "protocol lists must be the same" ) ? void (0) : __assert_fail ("std::equal(OX->getProtocols().begin(), OX->getProtocols().end(), OY->getProtocols().begin(), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0, const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl() == P1->getCanonicalDecl(); }) && \"protocol lists must be the same\"" , "clang/lib/AST/ASTContext.cpp", 12858, __extension__ __PRETTY_FUNCTION__ )) | |||
12855 | [](const ObjCProtocolDecl *P0, const ObjCProtocolDecl *P1) {(static_cast <bool> (std::equal(OX->getProtocols().begin (), OX->getProtocols().end(), OY->getProtocols().begin( ), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0 , const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl () == P1->getCanonicalDecl(); }) && "protocol lists must be the same" ) ? void (0) : __assert_fail ("std::equal(OX->getProtocols().begin(), OX->getProtocols().end(), OY->getProtocols().begin(), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0, const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl() == P1->getCanonicalDecl(); }) && \"protocol lists must be the same\"" , "clang/lib/AST/ASTContext.cpp", 12858, __extension__ __PRETTY_FUNCTION__ )) | |||
12856 | return P0->getCanonicalDecl() == P1->getCanonicalDecl();(static_cast <bool> (std::equal(OX->getProtocols().begin (), OX->getProtocols().end(), OY->getProtocols().begin( ), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0 , const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl () == P1->getCanonicalDecl(); }) && "protocol lists must be the same" ) ? void (0) : __assert_fail ("std::equal(OX->getProtocols().begin(), OX->getProtocols().end(), OY->getProtocols().begin(), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0, const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl() == P1->getCanonicalDecl(); }) && \"protocol lists must be the same\"" , "clang/lib/AST/ASTContext.cpp", 12858, __extension__ __PRETTY_FUNCTION__ )) | |||
12857 | }) &&(static_cast <bool> (std::equal(OX->getProtocols().begin (), OX->getProtocols().end(), OY->getProtocols().begin( ), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0 , const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl () == P1->getCanonicalDecl(); }) && "protocol lists must be the same" ) ? void (0) : __assert_fail ("std::equal(OX->getProtocols().begin(), OX->getProtocols().end(), OY->getProtocols().begin(), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0, const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl() == P1->getCanonicalDecl(); }) && \"protocol lists must be the same\"" , "clang/lib/AST/ASTContext.cpp", 12858, __extension__ __PRETTY_FUNCTION__ )) | |||
12858 | "protocol lists must be the same")(static_cast <bool> (std::equal(OX->getProtocols().begin (), OX->getProtocols().end(), OY->getProtocols().begin( ), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0 , const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl () == P1->getCanonicalDecl(); }) && "protocol lists must be the same" ) ? void (0) : __assert_fail ("std::equal(OX->getProtocols().begin(), OX->getProtocols().end(), OY->getProtocols().begin(), OY->getProtocols().end(), [](const ObjCProtocolDecl *P0, const ObjCProtocolDecl *P1) { return P0->getCanonicalDecl() == P1->getCanonicalDecl(); }) && \"protocol lists must be the same\"" , "clang/lib/AST/ASTContext.cpp", 12858, __extension__ __PRETTY_FUNCTION__ )); | |||
12859 | auto TAs = getCommonTypes(Ctx, OX->getTypeArgsAsWritten(), | |||
12860 | OY->getTypeArgsAsWritten()); | |||
12861 | return Ctx.getObjCObjectType( | |||
12862 | Ctx.getCommonSugaredType(OX->getBaseType(), OY->getBaseType()), TAs, | |||
12863 | OX->getProtocols(), | |||
12864 | OX->isKindOfTypeAsWritten() && OY->isKindOfTypeAsWritten()); | |||
12865 | } | |||
12866 | case Type::ConstantMatrix: { | |||
12867 | const auto *MX = cast<ConstantMatrixType>(X), | |||
12868 | *MY = cast<ConstantMatrixType>(Y); | |||
12869 | assert(MX->getNumRows() == MY->getNumRows())(static_cast <bool> (MX->getNumRows() == MY->getNumRows ()) ? void (0) : __assert_fail ("MX->getNumRows() == MY->getNumRows()" , "clang/lib/AST/ASTContext.cpp", 12869, __extension__ __PRETTY_FUNCTION__ )); | |||
12870 | assert(MX->getNumColumns() == MY->getNumColumns())(static_cast <bool> (MX->getNumColumns() == MY->getNumColumns ()) ? void (0) : __assert_fail ("MX->getNumColumns() == MY->getNumColumns()" , "clang/lib/AST/ASTContext.cpp", 12870, __extension__ __PRETTY_FUNCTION__ )); | |||
12871 | return Ctx.getConstantMatrixType(getCommonElementType(Ctx, MX, MY), | |||
12872 | MX->getNumRows(), MX->getNumColumns()); | |||
12873 | } | |||
12874 | case Type::DependentSizedMatrix: { | |||
12875 | const auto *MX = cast<DependentSizedMatrixType>(X), | |||
12876 | *MY = cast<DependentSizedMatrixType>(Y); | |||
12877 | assert(Ctx.hasSameExpr(MX->getRowExpr(), MY->getRowExpr()))(static_cast <bool> (Ctx.hasSameExpr(MX->getRowExpr( ), MY->getRowExpr())) ? void (0) : __assert_fail ("Ctx.hasSameExpr(MX->getRowExpr(), MY->getRowExpr())" , "clang/lib/AST/ASTContext.cpp", 12877, __extension__ __PRETTY_FUNCTION__ )); | |||
12878 | assert(Ctx.hasSameExpr(MX->getColumnExpr(), MY->getColumnExpr()))(static_cast <bool> (Ctx.hasSameExpr(MX->getColumnExpr (), MY->getColumnExpr())) ? void (0) : __assert_fail ("Ctx.hasSameExpr(MX->getColumnExpr(), MY->getColumnExpr())" , "clang/lib/AST/ASTContext.cpp", 12878, __extension__ __PRETTY_FUNCTION__ )); | |||
12879 | return Ctx.getDependentSizedMatrixType( | |||
12880 | getCommonElementType(Ctx, MX, MY), MX->getRowExpr(), | |||
12881 | MX->getColumnExpr(), getCommonAttrLoc(MX, MY)); | |||
12882 | } | |||
12883 | case Type::Vector: { | |||
12884 | const auto *VX = cast<VectorType>(X), *VY = cast<VectorType>(Y); | |||
12885 | assert(VX->getNumElements() == VY->getNumElements())(static_cast <bool> (VX->getNumElements() == VY-> getNumElements()) ? void (0) : __assert_fail ("VX->getNumElements() == VY->getNumElements()" , "clang/lib/AST/ASTContext.cpp", 12885, __extension__ __PRETTY_FUNCTION__ )); | |||
12886 | assert(VX->getVectorKind() == VY->getVectorKind())(static_cast <bool> (VX->getVectorKind() == VY->getVectorKind ()) ? void (0) : __assert_fail ("VX->getVectorKind() == VY->getVectorKind()" , "clang/lib/AST/ASTContext.cpp", 12886, __extension__ __PRETTY_FUNCTION__ )); | |||
12887 | return Ctx.getVectorType(getCommonElementType(Ctx, VX, VY), | |||
12888 | VX->getNumElements(), VX->getVectorKind()); | |||
12889 | } | |||
12890 | case Type::ExtVector: { | |||
12891 | const auto *VX = cast<ExtVectorType>(X), *VY = cast<ExtVectorType>(Y); | |||
12892 | assert(VX->getNumElements() == VY->getNumElements())(static_cast <bool> (VX->getNumElements() == VY-> getNumElements()) ? void (0) : __assert_fail ("VX->getNumElements() == VY->getNumElements()" , "clang/lib/AST/ASTContext.cpp", 12892, __extension__ __PRETTY_FUNCTION__ )); | |||
12893 | return Ctx.getExtVectorType(getCommonElementType(Ctx, VX, VY), | |||
12894 | VX->getNumElements()); | |||
12895 | } | |||
12896 | case Type::DependentSizedExtVector: { | |||
12897 | const auto *VX = cast<DependentSizedExtVectorType>(X), | |||
12898 | *VY = cast<DependentSizedExtVectorType>(Y); | |||
12899 | return Ctx.getDependentSizedExtVectorType(getCommonElementType(Ctx, VX, VY), | |||
12900 | getCommonSizeExpr(Ctx, VX, VY), | |||
12901 | getCommonAttrLoc(VX, VY)); | |||
12902 | } | |||
12903 | case Type::DependentVector: { | |||
12904 | const auto *VX = cast<DependentVectorType>(X), | |||
12905 | *VY = cast<DependentVectorType>(Y); | |||
12906 | assert(VX->getVectorKind() == VY->getVectorKind())(static_cast <bool> (VX->getVectorKind() == VY->getVectorKind ()) ? void (0) : __assert_fail ("VX->getVectorKind() == VY->getVectorKind()" , "clang/lib/AST/ASTContext.cpp", 12906, __extension__ __PRETTY_FUNCTION__ )); | |||
12907 | return Ctx.getDependentVectorType( | |||
12908 | getCommonElementType(Ctx, VX, VY), getCommonSizeExpr(Ctx, VX, VY), | |||
12909 | getCommonAttrLoc(VX, VY), VX->getVectorKind()); | |||
12910 | } | |||
12911 | case Type::InjectedClassName: { | |||
12912 | const auto *IX = cast<InjectedClassNameType>(X), | |||
12913 | *IY = cast<InjectedClassNameType>(Y); | |||
12914 | return Ctx.getInjectedClassNameType( | |||
12915 | getCommonDeclChecked(IX->getDecl(), IY->getDecl()), | |||
12916 | Ctx.getCommonSugaredType(IX->getInjectedSpecializationType(), | |||
12917 | IY->getInjectedSpecializationType())); | |||
12918 | } | |||
12919 | case Type::TemplateSpecialization: { | |||
12920 | const auto *TX = cast<TemplateSpecializationType>(X), | |||
12921 | *TY = cast<TemplateSpecializationType>(Y); | |||
12922 | auto As = getCommonTemplateArguments(Ctx, TX->template_arguments(), | |||
12923 | TY->template_arguments()); | |||
12924 | return Ctx.getTemplateSpecializationType( | |||
12925 | ::getCommonTemplateNameChecked(Ctx, TX->getTemplateName(), | |||
12926 | TY->getTemplateName()), | |||
12927 | As, X->getCanonicalTypeInternal()); | |||
12928 | } | |||
12929 | case Type::DependentName: { | |||
12930 | const auto *NX = cast<DependentNameType>(X), | |||
12931 | *NY = cast<DependentNameType>(Y); | |||
12932 | assert(NX->getIdentifier() == NY->getIdentifier())(static_cast <bool> (NX->getIdentifier() == NY->getIdentifier ()) ? void (0) : __assert_fail ("NX->getIdentifier() == NY->getIdentifier()" , "clang/lib/AST/ASTContext.cpp", 12932, __extension__ __PRETTY_FUNCTION__ )); | |||
12933 | return Ctx.getDependentNameType( | |||
12934 | getCommonTypeKeyword(NX, NY), getCommonNNS(Ctx, NX, NY), | |||
12935 | NX->getIdentifier(), NX->getCanonicalTypeInternal()); | |||
12936 | } | |||
12937 | case Type::DependentTemplateSpecialization: { | |||
12938 | const auto *TX = cast<DependentTemplateSpecializationType>(X), | |||
12939 | *TY = cast<DependentTemplateSpecializationType>(Y); | |||
12940 | assert(TX->getIdentifier() == TY->getIdentifier())(static_cast <bool> (TX->getIdentifier() == TY->getIdentifier ()) ? void (0) : __assert_fail ("TX->getIdentifier() == TY->getIdentifier()" , "clang/lib/AST/ASTContext.cpp", 12940, __extension__ __PRETTY_FUNCTION__ )); | |||
12941 | auto As = getCommonTemplateArguments(Ctx, TX->template_arguments(), | |||
12942 | TY->template_arguments()); | |||
12943 | return Ctx.getDependentTemplateSpecializationType( | |||
12944 | getCommonTypeKeyword(TX, TY), getCommonNNS(Ctx, TX, TY), | |||
12945 | TX->getIdentifier(), As); | |||
12946 | } | |||
12947 | case Type::UnaryTransform: { | |||
12948 | const auto *TX = cast<UnaryTransformType>(X), | |||
12949 | *TY = cast<UnaryTransformType>(Y); | |||
12950 | assert(TX->getUTTKind() == TY->getUTTKind())(static_cast <bool> (TX->getUTTKind() == TY->getUTTKind ()) ? void (0) : __assert_fail ("TX->getUTTKind() == TY->getUTTKind()" , "clang/lib/AST/ASTContext.cpp", 12950, __extension__ __PRETTY_FUNCTION__ )); | |||
12951 | return Ctx.getUnaryTransformType( | |||
12952 | Ctx.getCommonSugaredType(TX->getBaseType(), TY->getBaseType()), | |||
12953 | Ctx.getCommonSugaredType(TX->getUnderlyingType(), | |||
12954 | TY->getUnderlyingType()), | |||
12955 | TX->getUTTKind()); | |||
12956 | } | |||
12957 | case Type::PackExpansion: { | |||
12958 | const auto *PX = cast<PackExpansionType>(X), | |||
12959 | *PY = cast<PackExpansionType>(Y); | |||
12960 | assert(PX->getNumExpansions() == PY->getNumExpansions())(static_cast <bool> (PX->getNumExpansions() == PY-> getNumExpansions()) ? void (0) : __assert_fail ("PX->getNumExpansions() == PY->getNumExpansions()" , "clang/lib/AST/ASTContext.cpp", 12960, __extension__ __PRETTY_FUNCTION__ )); | |||
12961 | return Ctx.getPackExpansionType( | |||
12962 | Ctx.getCommonSugaredType(PX->getPattern(), PY->getPattern()), | |||
12963 | PX->getNumExpansions(), false); | |||
12964 | } | |||
12965 | case Type::Pipe: { | |||
12966 | const auto *PX = cast<PipeType>(X), *PY = cast<PipeType>(Y); | |||
12967 | assert(PX->isReadOnly() == PY->isReadOnly())(static_cast <bool> (PX->isReadOnly() == PY->isReadOnly ()) ? void (0) : __assert_fail ("PX->isReadOnly() == PY->isReadOnly()" , "clang/lib/AST/ASTContext.cpp", 12967, __extension__ __PRETTY_FUNCTION__ )); | |||
12968 | auto MP = PX->isReadOnly() ? &ASTContext::getReadPipeType | |||
12969 | : &ASTContext::getWritePipeType; | |||
12970 | return (Ctx.*MP)(getCommonElementType(Ctx, PX, PY)); | |||
12971 | } | |||
12972 | case Type::TemplateTypeParm: { | |||
12973 | const auto *TX = cast<TemplateTypeParmType>(X), | |||
12974 | *TY = cast<TemplateTypeParmType>(Y); | |||
12975 | assert(TX->getDepth() == TY->getDepth())(static_cast <bool> (TX->getDepth() == TY->getDepth ()) ? void (0) : __assert_fail ("TX->getDepth() == TY->getDepth()" , "clang/lib/AST/ASTContext.cpp", 12975, __extension__ __PRETTY_FUNCTION__ )); | |||
12976 | assert(TX->getIndex() == TY->getIndex())(static_cast <bool> (TX->getIndex() == TY->getIndex ()) ? void (0) : __assert_fail ("TX->getIndex() == TY->getIndex()" , "clang/lib/AST/ASTContext.cpp", 12976, __extension__ __PRETTY_FUNCTION__ )); | |||
12977 | assert(TX->isParameterPack() == TY->isParameterPack())(static_cast <bool> (TX->isParameterPack() == TY-> isParameterPack()) ? void (0) : __assert_fail ("TX->isParameterPack() == TY->isParameterPack()" , "clang/lib/AST/ASTContext.cpp", 12977, __extension__ __PRETTY_FUNCTION__ )); | |||
12978 | return Ctx.getTemplateTypeParmType( | |||
12979 | TX->getDepth(), TX->getIndex(), TX->isParameterPack(), | |||
12980 | getCommonDecl(TX->getDecl(), TY->getDecl())); | |||
12981 | } | |||
12982 | } | |||
12983 | llvm_unreachable("Unknown Type Class")::llvm::llvm_unreachable_internal("Unknown Type Class", "clang/lib/AST/ASTContext.cpp" , 12983); | |||
12984 | } | |||
12985 | ||||
12986 | static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X, | |||
12987 | const Type *Y, | |||
12988 | SplitQualType Underlying) { | |||
12989 | Type::TypeClass TC = X->getTypeClass(); | |||
12990 | if (TC != Y->getTypeClass()) | |||
12991 | return QualType(); | |||
12992 | switch (TC) { | |||
12993 | #define UNEXPECTED_TYPE(Class, Kind) \ | |||
12994 | case Type::Class: \ | |||
12995 | llvm_unreachable("Unexpected " Kind ": " #Class)::llvm::llvm_unreachable_internal("Unexpected " Kind ": " #Class , "clang/lib/AST/ASTContext.cpp", 12995); | |||
12996 | #define TYPE(Class, Base) | |||
12997 | #define DEPENDENT_TYPE(Class, Base) UNEXPECTED_TYPE(Class, "dependent") | |||
12998 | #include "clang/AST/TypeNodes.inc" | |||
12999 | ||||
13000 | #define CANONICAL_TYPE(Class) UNEXPECTED_TYPE(Class, "canonical") | |||
13001 | CANONICAL_TYPE(Atomic) | |||
13002 | CANONICAL_TYPE(BitInt) | |||
13003 | CANONICAL_TYPE(BlockPointer) | |||
13004 | CANONICAL_TYPE(Builtin) | |||
13005 | CANONICAL_TYPE(Complex) | |||
13006 | CANONICAL_TYPE(ConstantArray) | |||
13007 | CANONICAL_TYPE(ConstantMatrix) | |||
13008 | CANONICAL_TYPE(Enum) | |||
13009 | CANONICAL_TYPE(ExtVector) | |||
13010 | CANONICAL_TYPE(FunctionNoProto) | |||
13011 | CANONICAL_TYPE(FunctionProto) | |||
13012 | CANONICAL_TYPE(IncompleteArray) | |||
13013 | CANONICAL_TYPE(LValueReference) | |||
13014 | CANONICAL_TYPE(MemberPointer) | |||
13015 | CANONICAL_TYPE(ObjCInterface) | |||
13016 | CANONICAL_TYPE(ObjCObject) | |||
13017 | CANONICAL_TYPE(ObjCObjectPointer) | |||
13018 | CANONICAL_TYPE(Pipe) | |||
13019 | CANONICAL_TYPE(Pointer) | |||
13020 | CANONICAL_TYPE(Record) | |||
13021 | CANONICAL_TYPE(RValueReference) | |||
13022 | CANONICAL_TYPE(VariableArray) | |||
13023 | CANONICAL_TYPE(Vector) | |||
13024 | #undef CANONICAL_TYPE | |||
13025 | ||||
13026 | #undef UNEXPECTED_TYPE | |||
13027 | ||||
13028 | case Type::Adjusted: { | |||
13029 | const auto *AX = cast<AdjustedType>(X), *AY = cast<AdjustedType>(Y); | |||
13030 | QualType OX = AX->getOriginalType(), OY = AY->getOriginalType(); | |||
13031 | if (!Ctx.hasSameType(OX, OY)) | |||
13032 | return QualType(); | |||
13033 | // FIXME: It's inefficient to have to unify the original types. | |||
13034 | return Ctx.getAdjustedType(Ctx.getCommonSugaredType(OX, OY), | |||
13035 | Ctx.getQualifiedType(Underlying)); | |||
13036 | } | |||
13037 | case Type::Decayed: { | |||
13038 | const auto *DX = cast<DecayedType>(X), *DY = cast<DecayedType>(Y); | |||
13039 | QualType OX = DX->getOriginalType(), OY = DY->getOriginalType(); | |||
13040 | if (!Ctx.hasSameType(OX, OY)) | |||
13041 | return QualType(); | |||
13042 | // FIXME: It's inefficient to have to unify the original types. | |||
13043 | return Ctx.getDecayedType(Ctx.getCommonSugaredType(OX, OY), | |||
13044 | Ctx.getQualifiedType(Underlying)); | |||
13045 | } | |||
13046 | case Type::Attributed: { | |||
13047 | const auto *AX = cast<AttributedType>(X), *AY = cast<AttributedType>(Y); | |||
13048 | AttributedType::Kind Kind = AX->getAttrKind(); | |||
13049 | if (Kind != AY->getAttrKind()) | |||
13050 | return QualType(); | |||
13051 | QualType MX = AX->getModifiedType(), MY = AY->getModifiedType(); | |||
13052 | if (!Ctx.hasSameType(MX, MY)) | |||
13053 | return QualType(); | |||
13054 | // FIXME: It's inefficient to have to unify the modified types. | |||
13055 | return Ctx.getAttributedType(Kind, Ctx.getCommonSugaredType(MX, MY), | |||
13056 | Ctx.getQualifiedType(Underlying)); | |||
13057 | } | |||
13058 | case Type::BTFTagAttributed: { | |||
13059 | const auto *BX = cast<BTFTagAttributedType>(X); | |||
13060 | const BTFTypeTagAttr *AX = BX->getAttr(); | |||
13061 | // The attribute is not uniqued, so just compare the tag. | |||
13062 | if (AX->getBTFTypeTag() != | |||
13063 | cast<BTFTagAttributedType>(Y)->getAttr()->getBTFTypeTag()) | |||
13064 | return QualType(); | |||
13065 | return Ctx.getBTFTagAttributedType(AX, Ctx.getQualifiedType(Underlying)); | |||
13066 | } | |||
13067 | case Type::Auto: { | |||
13068 | const auto *AX = cast<AutoType>(X), *AY = cast<AutoType>(Y); | |||
13069 | ||||
13070 | AutoTypeKeyword KW = AX->getKeyword(); | |||
13071 | if (KW != AY->getKeyword()) | |||
13072 | return QualType(); | |||
13073 | ||||
13074 | ConceptDecl *CD = ::getCommonDecl(AX->getTypeConstraintConcept(), | |||
13075 | AY->getTypeConstraintConcept()); | |||
13076 | SmallVector<TemplateArgument, 8> As; | |||
13077 | if (CD && | |||
13078 | getCommonTemplateArguments(Ctx, As, AX->getTypeConstraintArguments(), | |||
13079 | AY->getTypeConstraintArguments())) { | |||
13080 | CD = nullptr; // The arguments differ, so make it unconstrained. | |||
13081 | As.clear(); | |||
13082 | } | |||
13083 | ||||
13084 | // Both auto types can't be dependent, otherwise they wouldn't have been | |||
13085 | // sugar. This implies they can't contain unexpanded packs either. | |||
13086 | return Ctx.getAutoType(Ctx.getQualifiedType(Underlying), AX->getKeyword(), | |||
13087 | /*IsDependent=*/false, /*IsPack=*/false, CD, As); | |||
13088 | } | |||
13089 | case Type::Decltype: | |||
13090 | return QualType(); | |||
13091 | case Type::DeducedTemplateSpecialization: | |||
13092 | // FIXME: Try to merge these. | |||
13093 | return QualType(); | |||
13094 | ||||
13095 | case Type::Elaborated: { | |||
13096 | const auto *EX = cast<ElaboratedType>(X), *EY = cast<ElaboratedType>(Y); | |||
13097 | return Ctx.getElaboratedType( | |||
13098 | ::getCommonTypeKeyword(EX, EY), ::getCommonNNS(Ctx, EX, EY), | |||
13099 | Ctx.getQualifiedType(Underlying), | |||
13100 | ::getCommonDecl(EX->getOwnedTagDecl(), EY->getOwnedTagDecl())); | |||
13101 | } | |||
13102 | case Type::MacroQualified: { | |||
13103 | const auto *MX = cast<MacroQualifiedType>(X), | |||
13104 | *MY = cast<MacroQualifiedType>(Y); | |||
13105 | const IdentifierInfo *IX = MX->getMacroIdentifier(); | |||
13106 | if (IX != MY->getMacroIdentifier()) | |||
13107 | return QualType(); | |||
13108 | return Ctx.getMacroQualifiedType(Ctx.getQualifiedType(Underlying), IX); | |||
13109 | } | |||
13110 | case Type::SubstTemplateTypeParm: { | |||
13111 | const auto *SX = cast<SubstTemplateTypeParmType>(X), | |||
13112 | *SY = cast<SubstTemplateTypeParmType>(Y); | |||
13113 | Decl *CD = | |||
13114 | ::getCommonDecl(SX->getAssociatedDecl(), SY->getAssociatedDecl()); | |||
13115 | if (!CD) | |||
13116 | return QualType(); | |||
13117 | unsigned Index = SX->getIndex(); | |||
13118 | if (Index != SY->getIndex()) | |||
13119 | return QualType(); | |||
13120 | auto PackIndex = SX->getPackIndex(); | |||
13121 | if (PackIndex != SY->getPackIndex()) | |||
13122 | return QualType(); | |||
13123 | return Ctx.getSubstTemplateTypeParmType(Ctx.getQualifiedType(Underlying), | |||
13124 | CD, Index, PackIndex); | |||
13125 | } | |||
13126 | case Type::ObjCTypeParam: | |||
13127 | // FIXME: Try to merge these. | |||
13128 | return QualType(); | |||
13129 | case Type::Paren: | |||
13130 | return Ctx.getParenType(Ctx.getQualifiedType(Underlying)); | |||
13131 | ||||
13132 | case Type::TemplateSpecialization: { | |||
13133 | const auto *TX = cast<TemplateSpecializationType>(X), | |||
13134 | *TY = cast<TemplateSpecializationType>(Y); | |||
13135 | TemplateName CTN = ::getCommonTemplateName(Ctx, TX->getTemplateName(), | |||
13136 | TY->getTemplateName()); | |||
13137 | if (!CTN.getAsVoidPointer()) | |||
13138 | return QualType(); | |||
13139 | SmallVector<TemplateArgument, 8> Args; | |||
13140 | if (getCommonTemplateArguments(Ctx, Args, TX->template_arguments(), | |||
13141 | TY->template_arguments())) | |||
13142 | return QualType(); | |||
13143 | return Ctx.getTemplateSpecializationType(CTN, Args, | |||
13144 | Ctx.getQualifiedType(Underlying)); | |||
13145 | } | |||
13146 | case Type::Typedef: { | |||
13147 | const auto *TX = cast<TypedefType>(X), *TY = cast<TypedefType>(Y); | |||
13148 | const TypedefNameDecl *CD = ::getCommonDecl(TX->getDecl(), TY->getDecl()); | |||
13149 | if (!CD) | |||
13150 | return QualType(); | |||
13151 | return Ctx.getTypedefType(CD, Ctx.getQualifiedType(Underlying)); | |||
13152 | } | |||
13153 | case Type::TypeOf: { | |||
13154 | // The common sugar between two typeof expressions, where one is | |||
13155 | // potentially a typeof_unqual and the other is not, we unify to the | |||
13156 | // qualified type as that retains the most information along with the type. | |||
13157 | // We only return a typeof_unqual type when both types are unqual types. | |||
13158 | TypeOfKind Kind = TypeOfKind::Qualified; | |||
13159 | if (cast<TypeOfType>(X)->getKind() == cast<TypeOfType>(Y)->getKind() && | |||
13160 | cast<TypeOfType>(X)->getKind() == TypeOfKind::Unqualified) | |||
13161 | Kind = TypeOfKind::Unqualified; | |||
13162 | return Ctx.getTypeOfType(Ctx.getQualifiedType(Underlying), Kind); | |||
13163 | } | |||
13164 | case Type::TypeOfExpr: | |||
13165 | return QualType(); | |||
13166 | ||||
13167 | case Type::UnaryTransform: { | |||
13168 | const auto *UX = cast<UnaryTransformType>(X), | |||
13169 | *UY = cast<UnaryTransformType>(Y); | |||
13170 | UnaryTransformType::UTTKind KX = UX->getUTTKind(); | |||
13171 | if (KX != UY->getUTTKind()) | |||
13172 | return QualType(); | |||
13173 | QualType BX = UX->getBaseType(), BY = UY->getBaseType(); | |||
13174 | if (!Ctx.hasSameType(BX, BY)) | |||
13175 | return QualType(); | |||
13176 | // FIXME: It's inefficient to have to unify the base types. | |||
13177 | return Ctx.getUnaryTransformType(Ctx.getCommonSugaredType(BX, BY), | |||
13178 | Ctx.getQualifiedType(Underlying), KX); | |||
13179 | } | |||
13180 | case Type::Using: { | |||
13181 | const auto *UX = cast<UsingType>(X), *UY = cast<UsingType>(Y); | |||
13182 | const UsingShadowDecl *CD = | |||
13183 | ::getCommonDecl(UX->getFoundDecl(), UY->getFoundDecl()); | |||
13184 | if (!CD) | |||
13185 | return QualType(); | |||
13186 | return Ctx.getUsingType(CD, Ctx.getQualifiedType(Underlying)); | |||
13187 | } | |||
13188 | } | |||
13189 | llvm_unreachable("Unhandled Type Class")::llvm::llvm_unreachable_internal("Unhandled Type Class", "clang/lib/AST/ASTContext.cpp" , 13189); | |||
13190 | } | |||
13191 | ||||
13192 | static auto unwrapSugar(SplitQualType &T, Qualifiers &QTotal) { | |||
13193 | SmallVector<SplitQualType, 8> R; | |||
13194 | while (true) { | |||
13195 | QTotal.addConsistentQualifiers(T.Quals); | |||
13196 | QualType NT = T.Ty->getLocallyUnqualifiedSingleStepDesugaredType(); | |||
13197 | if (NT == QualType(T.Ty, 0)) | |||
13198 | break; | |||
13199 | R.push_back(T); | |||
13200 | T = NT.split(); | |||
13201 | } | |||
13202 | return R; | |||
13203 | } | |||
13204 | ||||
13205 | QualType ASTContext::getCommonSugaredType(QualType X, QualType Y, | |||
13206 | bool Unqualified) { | |||
13207 | assert(Unqualified ? hasSameUnqualifiedType(X, Y) : hasSameType(X, Y))(static_cast <bool> (Unqualified ? hasSameUnqualifiedType (X, Y) : hasSameType(X, Y)) ? void (0) : __assert_fail ("Unqualified ? hasSameUnqualifiedType(X, Y) : hasSameType(X, Y)" , "clang/lib/AST/ASTContext.cpp", 13207, __extension__ __PRETTY_FUNCTION__ )); | |||
13208 | if (X == Y) | |||
13209 | return X; | |||
13210 | if (!Unqualified) { | |||
13211 | if (X.isCanonical()) | |||
13212 | return X; | |||
13213 | if (Y.isCanonical()) | |||
13214 | return Y; | |||
13215 | } | |||
13216 | ||||
13217 | SplitQualType SX = X.split(), SY = Y.split(); | |||
13218 | Qualifiers QX, QY; | |||
13219 | // Desugar SX and SY, setting the sugar and qualifiers aside into Xs and Ys, | |||
13220 | // until we reach their underlying "canonical nodes". Note these are not | |||
13221 | // necessarily canonical types, as they may still have sugared properties. | |||
13222 | // QX and QY will store the sum of all qualifiers in Xs and Ys respectively. | |||
13223 | auto Xs = ::unwrapSugar(SX, QX), Ys = ::unwrapSugar(SY, QY); | |||
13224 | if (SX.Ty != SY.Ty) { | |||
13225 | // The canonical nodes differ. Build a common canonical node out of the two, | |||
13226 | // unifying their sugar. This may recurse back here. | |||
13227 | SX.Ty = | |||
13228 | ::getCommonNonSugarTypeNode(*this, SX.Ty, QX, SY.Ty, QY).getTypePtr(); | |||
13229 | } else { | |||
13230 | // The canonical nodes were identical: We may have desugared too much. | |||
13231 | // Add any common sugar back in. | |||
13232 | while (!Xs.empty() && !Ys.empty() && Xs.back().Ty == Ys.back().Ty) { | |||
13233 | QX -= SX.Quals; | |||
13234 | QY -= SY.Quals; | |||
13235 | SX = Xs.pop_back_val(); | |||
13236 | SY = Ys.pop_back_val(); | |||
13237 | } | |||
13238 | } | |||
13239 | if (Unqualified) | |||
13240 | QX = Qualifiers::removeCommonQualifiers(QX, QY); | |||
13241 | else | |||
13242 | assert(QX == QY)(static_cast <bool> (QX == QY) ? void (0) : __assert_fail ("QX == QY", "clang/lib/AST/ASTContext.cpp", 13242, __extension__ __PRETTY_FUNCTION__)); | |||
13243 | ||||
13244 | // Even though the remaining sugar nodes in Xs and Ys differ, some may be | |||
13245 | // related. Walk up these nodes, unifying them and adding the result. | |||
13246 | while (!Xs.empty() && !Ys.empty()) { | |||
13247 | auto Underlying = SplitQualType( | |||
13248 | SX.Ty, Qualifiers::removeCommonQualifiers(SX.Quals, SY.Quals)); | |||
13249 | SX = Xs.pop_back_val(); | |||
13250 | SY = Ys.pop_back_val(); | |||
13251 | SX.Ty = ::getCommonSugarTypeNode(*this, SX.Ty, SY.Ty, Underlying) | |||
13252 | .getTypePtrOrNull(); | |||
13253 | // Stop at the first pair which is unrelated. | |||
13254 | if (!SX.Ty) { | |||
13255 | SX.Ty = Underlying.Ty; | |||
13256 | break; | |||
13257 | } | |||
13258 | QX -= Underlying.Quals; | |||
13259 | }; | |||
13260 | ||||
13261 | // Add back the missing accumulated qualifiers, which were stripped off | |||
13262 | // with the sugar nodes we could not unify. | |||
13263 | QualType R = getQualifiedType(SX.Ty, QX); | |||
13264 | assert(Unqualified ? hasSameUnqualifiedType(R, X) : hasSameType(R, X))(static_cast <bool> (Unqualified ? hasSameUnqualifiedType (R, X) : hasSameType(R, X)) ? void (0) : __assert_fail ("Unqualified ? hasSameUnqualifiedType(R, X) : hasSameType(R, X)" , "clang/lib/AST/ASTContext.cpp", 13264, __extension__ __PRETTY_FUNCTION__ )); | |||
13265 | return R; | |||
13266 | } | |||
13267 | ||||
13268 | QualType ASTContext::getCorrespondingSaturatedType(QualType Ty) const { | |||
13269 | assert(Ty->isFixedPointType())(static_cast <bool> (Ty->isFixedPointType()) ? void ( 0) : __assert_fail ("Ty->isFixedPointType()", "clang/lib/AST/ASTContext.cpp" , 13269, __extension__ __PRETTY_FUNCTION__)); | |||
13270 | ||||
13271 | if (Ty->isSaturatedFixedPointType()) return Ty; | |||
13272 | ||||
13273 | switch (Ty->castAs<BuiltinType>()->getKind()) { | |||
13274 | default: | |||
13275 | llvm_unreachable("Not a fixed point type!")::llvm::llvm_unreachable_internal("Not a fixed point type!", "clang/lib/AST/ASTContext.cpp" , 13275); | |||
13276 | case BuiltinType::ShortAccum: | |||
13277 | return SatShortAccumTy; | |||
13278 | case BuiltinType::Accum: | |||
13279 | return SatAccumTy; | |||
13280 | case BuiltinType::LongAccum: | |||
13281 | return SatLongAccumTy; | |||
13282 | case BuiltinType::UShortAccum: | |||
13283 | return SatUnsignedShortAccumTy; | |||
13284 | case BuiltinType::UAccum: | |||
13285 | return SatUnsignedAccumTy; | |||
13286 | case BuiltinType::ULongAccum: | |||
13287 | return SatUnsignedLongAccumTy; | |||
13288 | case BuiltinType::ShortFract: | |||
13289 | return SatShortFractTy; | |||
13290 | case BuiltinType::Fract: | |||
13291 | return SatFractTy; | |||
13292 | case BuiltinType::LongFract: | |||
13293 | return SatLongFractTy; | |||
13294 | case BuiltinType::UShortFract: | |||
13295 | return SatUnsignedShortFractTy; | |||
13296 | case BuiltinType::UFract: | |||
13297 | return SatUnsignedFractTy; | |||
13298 | case BuiltinType::ULongFract: | |||
13299 | return SatUnsignedLongFractTy; | |||
13300 | } | |||
13301 | } | |||
13302 | ||||
13303 | LangAS ASTContext::getLangASForBuiltinAddressSpace(unsigned AS) const { | |||
13304 | if (LangOpts.OpenCL) | |||
13305 | return getTargetInfo().getOpenCLBuiltinAddressSpace(AS); | |||
13306 | ||||
13307 | if (LangOpts.CUDA) | |||
13308 | return getTargetInfo().getCUDABuiltinAddressSpace(AS); | |||
13309 | ||||
13310 | return getLangASFromTargetAS(AS); | |||
13311 | } | |||
13312 | ||||
13313 | // Explicitly instantiate this in case a Redeclarable<T> is used from a TU that | |||
13314 | // doesn't include ASTContext.h | |||
13315 | template | |||
13316 | clang::LazyGenerationalUpdatePtr< | |||
13317 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType | |||
13318 | clang::LazyGenerationalUpdatePtr< | |||
13319 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue( | |||
13320 | const clang::ASTContext &Ctx, Decl *Value); | |||
13321 | ||||
13322 | unsigned char ASTContext::getFixedPointScale(QualType Ty) const { | |||
13323 | assert(Ty->isFixedPointType())(static_cast <bool> (Ty->isFixedPointType()) ? void ( 0) : __assert_fail ("Ty->isFixedPointType()", "clang/lib/AST/ASTContext.cpp" , 13323, __extension__ __PRETTY_FUNCTION__)); | |||
13324 | ||||
13325 | const TargetInfo &Target = getTargetInfo(); | |||
13326 | switch (Ty->castAs<BuiltinType>()->getKind()) { | |||
13327 | default: | |||
13328 | llvm_unreachable("Not a fixed point type!")::llvm::llvm_unreachable_internal("Not a fixed point type!", "clang/lib/AST/ASTContext.cpp" , 13328); | |||
13329 | case BuiltinType::ShortAccum: | |||
13330 | case BuiltinType::SatShortAccum: | |||
13331 | return Target.getShortAccumScale(); | |||
13332 | case BuiltinType::Accum: | |||
13333 | case BuiltinType::SatAccum: | |||
13334 | return Target.getAccumScale(); | |||
13335 | case BuiltinType::LongAccum: | |||
13336 | case BuiltinType::SatLongAccum: | |||
13337 | return Target.getLongAccumScale(); | |||
13338 | case BuiltinType::UShortAccum: | |||
13339 | case BuiltinType::SatUShortAccum: | |||
13340 | return Target.getUnsignedShortAccumScale(); | |||
13341 | case BuiltinType::UAccum: | |||
13342 | case BuiltinType::SatUAccum: | |||
13343 | return Target.getUnsignedAccumScale(); | |||
13344 | case BuiltinType::ULongAccum: | |||
13345 | case BuiltinType::SatULongAccum: | |||
13346 | return Target.getUnsignedLongAccumScale(); | |||
13347 | case BuiltinType::ShortFract: | |||
13348 | case BuiltinType::SatShortFract: | |||
13349 | return Target.getShortFractScale(); | |||
13350 | case BuiltinType::Fract: | |||
13351 | case BuiltinType::SatFract: | |||
13352 | return Target.getFractScale(); | |||
13353 | case BuiltinType::LongFract: | |||
13354 | case BuiltinType::SatLongFract: | |||
13355 | return Target.getLongFractScale(); | |||
13356 | case BuiltinType::UShortFract: | |||
13357 | case BuiltinType::SatUShortFract: | |||
13358 | return Target.getUnsignedShortFractScale(); | |||
13359 | case BuiltinType::UFract: | |||
13360 | case BuiltinType::SatUFract: | |||
13361 | return Target.getUnsignedFractScale(); | |||
13362 | case BuiltinType::ULongFract: | |||
13363 | case BuiltinType::SatULongFract: | |||
13364 | return Target.getUnsignedLongFractScale(); | |||
13365 | } | |||
13366 | } | |||
13367 | ||||
13368 | unsigned char ASTContext::getFixedPointIBits(QualType Ty) const { | |||
13369 | assert(Ty->isFixedPointType())(static_cast <bool> (Ty->isFixedPointType()) ? void ( 0) : __assert_fail ("Ty->isFixedPointType()", "clang/lib/AST/ASTContext.cpp" , 13369, __extension__ __PRETTY_FUNCTION__)); | |||
13370 | ||||
13371 | const TargetInfo &Target = getTargetInfo(); | |||
13372 | switch (Ty->castAs<BuiltinType>()->getKind()) { | |||
13373 | default: | |||
13374 | llvm_unreachable("Not a fixed point type!")::llvm::llvm_unreachable_internal("Not a fixed point type!", "clang/lib/AST/ASTContext.cpp" , 13374); | |||
13375 | case BuiltinType::ShortAccum: | |||
13376 | case BuiltinType::SatShortAccum: | |||
13377 | return Target.getShortAccumIBits(); | |||
13378 | case BuiltinType::Accum: | |||
13379 | case BuiltinType::SatAccum: | |||
13380 | return Target.getAccumIBits(); | |||
13381 | case BuiltinType::LongAccum: | |||
13382 | case BuiltinType::SatLongAccum: | |||
13383 | return Target.getLongAccumIBits(); | |||
13384 | case BuiltinType::UShortAccum: | |||
13385 | case BuiltinType::SatUShortAccum: | |||
13386 | return Target.getUnsignedShortAccumIBits(); | |||
13387 | case BuiltinType::UAccum: | |||
13388 | case BuiltinType::SatUAccum: | |||
13389 | return Target.getUnsignedAccumIBits(); | |||
13390 | case BuiltinType::ULongAccum: | |||
13391 | case BuiltinType::SatULongAccum: | |||
13392 | return Target.getUnsignedLongAccumIBits(); | |||
13393 | case BuiltinType::ShortFract: | |||
13394 | case BuiltinType::SatShortFract: | |||
13395 | case BuiltinType::Fract: | |||
13396 | case BuiltinType::SatFract: | |||
13397 | case BuiltinType::LongFract: | |||
13398 | case BuiltinType::SatLongFract: | |||
13399 | case BuiltinType::UShortFract: | |||
13400 | case BuiltinType::SatUShortFract: | |||
13401 | case BuiltinType::UFract: | |||
13402 | case BuiltinType::SatUFract: | |||
13403 | case BuiltinType::ULongFract: | |||
13404 | case BuiltinType::SatULongFract: | |||
13405 | return 0; | |||
13406 | } | |||
13407 | } | |||
13408 | ||||
13409 | llvm::FixedPointSemantics | |||
13410 | ASTContext::getFixedPointSemantics(QualType Ty) const { | |||
13411 | assert((Ty->isFixedPointType() || Ty->isIntegerType()) &&(static_cast <bool> ((Ty->isFixedPointType() || Ty-> isIntegerType()) && "Can only get the fixed point semantics for a " "fixed point or integer type.") ? void (0) : __assert_fail ( "(Ty->isFixedPointType() || Ty->isIntegerType()) && \"Can only get the fixed point semantics for a \" \"fixed point or integer type.\"" , "clang/lib/AST/ASTContext.cpp", 13413, __extension__ __PRETTY_FUNCTION__ )) | |||
13412 | "Can only get the fixed point semantics for a "(static_cast <bool> ((Ty->isFixedPointType() || Ty-> isIntegerType()) && "Can only get the fixed point semantics for a " "fixed point or integer type.") ? void (0) : __assert_fail ( "(Ty->isFixedPointType() || Ty->isIntegerType()) && \"Can only get the fixed point semantics for a \" \"fixed point or integer type.\"" , "clang/lib/AST/ASTContext.cpp", 13413, __extension__ __PRETTY_FUNCTION__ )) | |||
13413 | "fixed point or integer type.")(static_cast <bool> ((Ty->isFixedPointType() || Ty-> isIntegerType()) && "Can only get the fixed point semantics for a " "fixed point or integer type.") ? void (0) : __assert_fail ( "(Ty->isFixedPointType() || Ty->isIntegerType()) && \"Can only get the fixed point semantics for a \" \"fixed point or integer type.\"" , "clang/lib/AST/ASTContext.cpp", 13413, __extension__ __PRETTY_FUNCTION__ )); | |||
13414 | if (Ty->isIntegerType()) | |||
13415 | return llvm::FixedPointSemantics::GetIntegerSemantics( | |||
13416 | getIntWidth(Ty), Ty->isSignedIntegerType()); | |||
13417 | ||||
13418 | bool isSigned = Ty->isSignedFixedPointType(); | |||
13419 | return llvm::FixedPointSemantics( | |||
13420 | static_cast<unsigned>(getTypeSize(Ty)), getFixedPointScale(Ty), isSigned, | |||
13421 | Ty->isSaturatedFixedPointType(), | |||
13422 | !isSigned && getTargetInfo().doUnsignedFixedPointTypesHavePadding()); | |||
13423 | } | |||
13424 | ||||
13425 | llvm::APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const { | |||
13426 | assert(Ty->isFixedPointType())(static_cast <bool> (Ty->isFixedPointType()) ? void ( 0) : __assert_fail ("Ty->isFixedPointType()", "clang/lib/AST/ASTContext.cpp" , 13426, __extension__ __PRETTY_FUNCTION__)); | |||
13427 | return llvm::APFixedPoint::getMax(getFixedPointSemantics(Ty)); | |||
13428 | } | |||
13429 | ||||
13430 | llvm::APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const { | |||
13431 | assert(Ty->isFixedPointType())(static_cast <bool> (Ty->isFixedPointType()) ? void ( 0) : __assert_fail ("Ty->isFixedPointType()", "clang/lib/AST/ASTContext.cpp" , 13431, __extension__ __PRETTY_FUNCTION__)); | |||
13432 | return llvm::APFixedPoint::getMin(getFixedPointSemantics(Ty)); | |||
13433 | } | |||
13434 | ||||
13435 | QualType ASTContext::getCorrespondingSignedFixedPointType(QualType Ty) const { | |||
13436 | assert(Ty->isUnsignedFixedPointType() &&(static_cast <bool> (Ty->isUnsignedFixedPointType() && "Expected unsigned fixed point type") ? void (0) : __assert_fail ("Ty->isUnsignedFixedPointType() && \"Expected unsigned fixed point type\"" , "clang/lib/AST/ASTContext.cpp", 13437, __extension__ __PRETTY_FUNCTION__ )) | |||
13437 | "Expected unsigned fixed point type")(static_cast <bool> (Ty->isUnsignedFixedPointType() && "Expected unsigned fixed point type") ? void (0) : __assert_fail ("Ty->isUnsignedFixedPointType() && \"Expected unsigned fixed point type\"" , "clang/lib/AST/ASTContext.cpp", 13437, __extension__ __PRETTY_FUNCTION__ )); | |||
13438 | ||||
13439 | switch (Ty->castAs<BuiltinType>()->getKind()) { | |||
13440 | case BuiltinType::UShortAccum: | |||
13441 | return ShortAccumTy; | |||
13442 | case BuiltinType::UAccum: | |||
13443 | return AccumTy; | |||
13444 | case BuiltinType::ULongAccum: | |||
13445 | return LongAccumTy; | |||
13446 | case BuiltinType::SatUShortAccum: | |||
13447 | return SatShortAccumTy; | |||
13448 | case BuiltinType::SatUAccum: | |||
13449 | return SatAccumTy; | |||
13450 | case BuiltinType::SatULongAccum: | |||
13451 | return SatLongAccumTy; | |||
13452 | case BuiltinType::UShortFract: | |||
13453 | return ShortFractTy; | |||
13454 | case BuiltinType::UFract: | |||
13455 | return FractTy; | |||
13456 | case BuiltinType::ULongFract: | |||
13457 | return LongFractTy; | |||
13458 | case BuiltinType::SatUShortFract: | |||
13459 | return SatShortFractTy; | |||
13460 | case BuiltinType::SatUFract: | |||
13461 | return SatFractTy; | |||
13462 | case BuiltinType::SatULongFract: | |||
13463 | return SatLongFractTy; | |||
13464 | default: | |||
13465 | llvm_unreachable("Unexpected unsigned fixed point type")::llvm::llvm_unreachable_internal("Unexpected unsigned fixed point type" , "clang/lib/AST/ASTContext.cpp", 13465); | |||
13466 | } | |||
13467 | } | |||
13468 | ||||
13469 | std::vector<std::string> ASTContext::filterFunctionTargetVersionAttrs( | |||
13470 | const TargetVersionAttr *TV) const { | |||
13471 | assert(TV != nullptr)(static_cast <bool> (TV != nullptr) ? void (0) : __assert_fail ("TV != nullptr", "clang/lib/AST/ASTContext.cpp", 13471, __extension__ __PRETTY_FUNCTION__)); | |||
13472 | llvm::SmallVector<StringRef, 8> Feats; | |||
13473 | std::vector<std::string> ResFeats; | |||
13474 | TV->getFeatures(Feats); | |||
13475 | for (auto &Feature : Feats) | |||
13476 | if (Target->validateCpuSupports(Feature.str())) | |||
13477 | // Use '?' to mark features that came from TargetVersion. | |||
13478 | ResFeats.push_back("?" + Feature.str()); | |||
13479 | return ResFeats; | |||
13480 | } | |||
13481 | ||||
13482 | ParsedTargetAttr | |||
13483 | ASTContext::filterFunctionTargetAttrs(const TargetAttr *TD) const { | |||
13484 | assert(TD != nullptr)(static_cast <bool> (TD != nullptr) ? void (0) : __assert_fail ("TD != nullptr", "clang/lib/AST/ASTContext.cpp", 13484, __extension__ __PRETTY_FUNCTION__)); | |||
13485 | ParsedTargetAttr ParsedAttr = Target->parseTargetAttr(TD->getFeaturesStr()); | |||
13486 | ||||
13487 | llvm::erase_if(ParsedAttr.Features, [&](const std::string &Feat) { | |||
13488 | return !Target->isValidFeatureName(StringRef{Feat}.substr(1)); | |||
13489 | }); | |||
13490 | return ParsedAttr; | |||
13491 | } | |||
13492 | ||||
13493 | void ASTContext::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, | |||
13494 | const FunctionDecl *FD) const { | |||
13495 | if (FD) | |||
13496 | getFunctionFeatureMap(FeatureMap, GlobalDecl().getWithDecl(FD)); | |||
13497 | else | |||
13498 | Target->initFeatureMap(FeatureMap, getDiagnostics(), | |||
13499 | Target->getTargetOpts().CPU, | |||
13500 | Target->getTargetOpts().Features); | |||
13501 | } | |||
13502 | ||||
13503 | // Fills in the supplied string map with the set of target features for the | |||
13504 | // passed in function. | |||
13505 | void ASTContext::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, | |||
13506 | GlobalDecl GD) const { | |||
13507 | StringRef TargetCPU = Target->getTargetOpts().CPU; | |||
13508 | const FunctionDecl *FD = GD.getDecl()->getAsFunction(); | |||
13509 | if (const auto *TD = FD->getAttr<TargetAttr>()) { | |||
13510 | ParsedTargetAttr ParsedAttr = filterFunctionTargetAttrs(TD); | |||
13511 | ||||
13512 | // Make a copy of the features as passed on the command line into the | |||
13513 | // beginning of the additional features from the function to override. | |||
13514 | ParsedAttr.Features.insert( | |||
13515 | ParsedAttr.Features.begin(), | |||
13516 | Target->getTargetOpts().FeaturesAsWritten.begin(), | |||
13517 | Target->getTargetOpts().FeaturesAsWritten.end()); | |||
13518 | ||||
13519 | if (ParsedAttr.CPU != "" && Target->isValidCPUName(ParsedAttr.CPU)) | |||
13520 | TargetCPU = ParsedAttr.CPU; | |||
13521 | ||||
13522 | // Now populate the feature map, first with the TargetCPU which is either | |||
13523 | // the default or a new one from the target attribute string. Then we'll use | |||
13524 | // the passed in features (FeaturesAsWritten) along with the new ones from | |||
13525 | // the attribute. | |||
13526 | Target->initFeatureMap(FeatureMap, getDiagnostics(), TargetCPU, | |||
13527 | ParsedAttr.Features); | |||
13528 | } else if (const auto *SD = FD->getAttr<CPUSpecificAttr>()) { | |||
13529 | llvm::SmallVector<StringRef, 32> FeaturesTmp; | |||
13530 | Target->getCPUSpecificCPUDispatchFeatures( | |||
13531 | SD->getCPUName(GD.getMultiVersionIndex())->getName(), FeaturesTmp); | |||
13532 | std::vector<std::string> Features(FeaturesTmp.begin(), FeaturesTmp.end()); | |||
13533 | Features.insert(Features.begin(), | |||
13534 | Target->getTargetOpts().FeaturesAsWritten.begin(), | |||
13535 | Target->getTargetOpts().FeaturesAsWritten.end()); | |||
13536 | Target->initFeatureMap(FeatureMap, getDiagnostics(), TargetCPU, Features); | |||
13537 | } else if (const auto *TC = FD->getAttr<TargetClonesAttr>()) { | |||
13538 | std::vector<std::string> Features; | |||
13539 | StringRef VersionStr = TC->getFeatureStr(GD.getMultiVersionIndex()); | |||
13540 | if (Target->getTriple().isAArch64()) { | |||
13541 | // TargetClones for AArch64 | |||
13542 | if (VersionStr != "default") { | |||
13543 | SmallVector<StringRef, 1> VersionFeatures; | |||
13544 | VersionStr.split(VersionFeatures, "+"); | |||
13545 | for (auto &VFeature : VersionFeatures) { | |||
13546 | VFeature = VFeature.trim(); | |||
13547 | // Use '?' to mark features that came from AArch64 TargetClones. | |||
13548 | Features.push_back((StringRef{"?"} + VFeature).str()); | |||
13549 | } | |||
13550 | } | |||
13551 | Features.insert(Features.begin(), | |||
13552 | Target->getTargetOpts().FeaturesAsWritten.begin(), | |||
13553 | Target->getTargetOpts().FeaturesAsWritten.end()); | |||
13554 | } else { | |||
13555 | if (VersionStr.startswith("arch=")) | |||
13556 | TargetCPU = VersionStr.drop_front(sizeof("arch=") - 1); | |||
13557 | else if (VersionStr != "default") | |||
13558 | Features.push_back((StringRef{"+"} + VersionStr).str()); | |||
13559 | } | |||
13560 | Target->initFeatureMap(FeatureMap, getDiagnostics(), TargetCPU, Features); | |||
13561 | } else if (const auto *TV = FD->getAttr<TargetVersionAttr>()) { | |||
13562 | std::vector<std::string> Feats = filterFunctionTargetVersionAttrs(TV); | |||
13563 | Feats.insert(Feats.begin(), | |||
13564 | Target->getTargetOpts().FeaturesAsWritten.begin(), | |||
13565 | Target->getTargetOpts().FeaturesAsWritten.end()); | |||
13566 | Target->initFeatureMap(FeatureMap, getDiagnostics(), TargetCPU, Feats); | |||
13567 | } else { | |||
13568 | FeatureMap = Target->getTargetOpts().FeatureMap; | |||
13569 | } | |||
13570 | } | |||
13571 | ||||
13572 | OMPTraitInfo &ASTContext::getNewOMPTraitInfo() { | |||
13573 | OMPTraitInfoVector.emplace_back(new OMPTraitInfo()); | |||
13574 | return *OMPTraitInfoVector.back(); | |||
13575 | } | |||
13576 | ||||
13577 | const StreamingDiagnostic &clang:: | |||
13578 | operator<<(const StreamingDiagnostic &DB, | |||
13579 | const ASTContext::SectionInfo &Section) { | |||
13580 | if (Section.Decl) | |||
13581 | return DB << Section.Decl; | |||
13582 | return DB << "a prior #pragma section"; | |||
13583 | } | |||
13584 | ||||
13585 | bool ASTContext::mayExternalize(const Decl *D) const { | |||
13586 | bool IsStaticVar = | |||
13587 | isa<VarDecl>(D) && cast<VarDecl>(D)->getStorageClass() == SC_Static; | |||
13588 | bool IsExplicitDeviceVar = (D->hasAttr<CUDADeviceAttr>() && | |||
13589 | !D->getAttr<CUDADeviceAttr>()->isImplicit()) || | |||
13590 | (D->hasAttr<CUDAConstantAttr>() && | |||
13591 | !D->getAttr<CUDAConstantAttr>()->isImplicit()); | |||
13592 | // CUDA/HIP: static managed variables need to be externalized since it is | |||
13593 | // a declaration in IR, therefore cannot have internal linkage. Kernels in | |||
13594 | // anonymous name space needs to be externalized to avoid duplicate symbols. | |||
13595 | return (IsStaticVar && | |||
13596 | (D->hasAttr<HIPManagedAttr>() || IsExplicitDeviceVar)) || | |||
13597 | (D->hasAttr<CUDAGlobalAttr>() && | |||
13598 | basicGVALinkageForFunction(*this, cast<FunctionDecl>(D)) == | |||
13599 | GVA_Internal); | |||
13600 | } | |||
13601 | ||||
13602 | bool ASTContext::shouldExternalize(const Decl *D) const { | |||
13603 | return mayExternalize(D) && | |||
13604 | (D->hasAttr<HIPManagedAttr>() || D->hasAttr<CUDAGlobalAttr>() || | |||
13605 | CUDADeviceVarODRUsedByHost.count(cast<VarDecl>(D))); | |||
13606 | } | |||
13607 | ||||
13608 | StringRef ASTContext::getCUIDHash() const { | |||
13609 | if (!CUIDHash.empty()) | |||
13610 | return CUIDHash; | |||
13611 | if (LangOpts.CUID.empty()) | |||
13612 | return StringRef(); | |||
13613 | CUIDHash = llvm::utohexstr(llvm::MD5Hash(LangOpts.CUID), /*LowerCase=*/true); | |||
13614 | return CUIDHash; | |||
13615 | } |