Bug Summary

File:tools/clang/lib/AST/ASTContext.cpp
Warning:line 8203, column 7
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ASTContext.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D CLANG_VENDOR="Debian " -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~svn373517/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include -I /build/llvm-toolchain-snapshot-10~svn373517/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-10~svn373517/build-llvm/include -I /build/llvm-toolchain-snapshot-10~svn373517/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~svn373517/build-llvm/tools/clang/lib/AST -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~svn373517=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-10-02-234743-9763-1 -x c++ /build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp

/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp

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/ASTMutationListener.h"
18#include "clang/AST/ASTTypeTraits.h"
19#include "clang/AST/Attr.h"
20#include "clang/AST/AttrIterator.h"
21#include "clang/AST/CharUnits.h"
22#include "clang/AST/Comment.h"
23#include "clang/AST/Decl.h"
24#include "clang/AST/DeclBase.h"
25#include "clang/AST/DeclCXX.h"
26#include "clang/AST/DeclContextInternals.h"
27#include "clang/AST/DeclObjC.h"
28#include "clang/AST/DeclOpenMP.h"
29#include "clang/AST/DeclTemplate.h"
30#include "clang/AST/DeclarationName.h"
31#include "clang/AST/Expr.h"
32#include "clang/AST/ExprCXX.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/Mangle.h"
35#include "clang/AST/MangleNumberingContext.h"
36#include "clang/AST/NestedNameSpecifier.h"
37#include "clang/AST/RawCommentList.h"
38#include "clang/AST/RecordLayout.h"
39#include "clang/AST/RecursiveASTVisitor.h"
40#include "clang/AST/Stmt.h"
41#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
45#include "clang/AST/UnresolvedSet.h"
46#include "clang/AST/VTableBuilder.h"
47#include "clang/Basic/AddressSpaces.h"
48#include "clang/Basic/Builtins.h"
49#include "clang/Basic/CommentOptions.h"
50#include "clang/Basic/ExceptionSpecificationType.h"
51#include "clang/Basic/FixedPoint.h"
52#include "clang/Basic/IdentifierTable.h"
53#include "clang/Basic/LLVM.h"
54#include "clang/Basic/LangOptions.h"
55#include "clang/Basic/Linkage.h"
56#include "clang/Basic/ObjCRuntime.h"
57#include "clang/Basic/SanitizerBlacklist.h"
58#include "clang/Basic/SourceLocation.h"
59#include "clang/Basic/SourceManager.h"
60#include "clang/Basic/Specifiers.h"
61#include "clang/Basic/TargetCXXABI.h"
62#include "clang/Basic/TargetInfo.h"
63#include "clang/Basic/XRayLists.h"
64#include "llvm/ADT/APInt.h"
65#include "llvm/ADT/APSInt.h"
66#include "llvm/ADT/ArrayRef.h"
67#include "llvm/ADT/DenseMap.h"
68#include "llvm/ADT/DenseSet.h"
69#include "llvm/ADT/FoldingSet.h"
70#include "llvm/ADT/None.h"
71#include "llvm/ADT/Optional.h"
72#include "llvm/ADT/PointerUnion.h"
73#include "llvm/ADT/STLExtras.h"
74#include "llvm/ADT/SmallPtrSet.h"
75#include "llvm/ADT/SmallVector.h"
76#include "llvm/ADT/StringExtras.h"
77#include "llvm/ADT/StringRef.h"
78#include "llvm/ADT/Triple.h"
79#include "llvm/Support/Capacity.h"
80#include "llvm/Support/Casting.h"
81#include "llvm/Support/Compiler.h"
82#include "llvm/Support/ErrorHandling.h"
83#include "llvm/Support/MathExtras.h"
84#include "llvm/Support/raw_ostream.h"
85#include <algorithm>
86#include <cassert>
87#include <cstddef>
88#include <cstdint>
89#include <cstdlib>
90#include <map>
91#include <memory>
92#include <string>
93#include <tuple>
94#include <utility>
95
96using namespace clang;
97
98enum FloatingRank {
99 Float16Rank, HalfRank, FloatRank, DoubleRank, LongDoubleRank, Float128Rank
100};
101
102/// \returns location that is relevant when searching for Doc comments related
103/// to \p D.
104static SourceLocation getDeclLocForCommentSearch(const Decl *D,
105 SourceManager &SourceMgr) {
106 assert(D)((D) ? static_cast<void> (0) : __assert_fail ("D", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 106, __PRETTY_FUNCTION__))
;
107
108 // User can not attach documentation to implicit declarations.
109 if (D->isImplicit())
110 return {};
111
112 // User can not attach documentation to implicit instantiations.
113 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
114 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
115 return {};
116 }
117
118 if (const auto *VD = dyn_cast<VarDecl>(D)) {
119 if (VD->isStaticDataMember() &&
120 VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
121 return {};
122 }
123
124 if (const auto *CRD = dyn_cast<CXXRecordDecl>(D)) {
125 if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
126 return {};
127 }
128
129 if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
130 TemplateSpecializationKind TSK = CTSD->getSpecializationKind();
131 if (TSK == TSK_ImplicitInstantiation ||
132 TSK == TSK_Undeclared)
133 return {};
134 }
135
136 if (const auto *ED = dyn_cast<EnumDecl>(D)) {
137 if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
138 return {};
139 }
140 if (const auto *TD = dyn_cast<TagDecl>(D)) {
141 // When tag declaration (but not definition!) is part of the
142 // decl-specifier-seq of some other declaration, it doesn't get comment
143 if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition())
144 return {};
145 }
146 // TODO: handle comments for function parameters properly.
147 if (isa<ParmVarDecl>(D))
148 return {};
149
150 // TODO: we could look up template parameter documentation in the template
151 // documentation.
152 if (isa<TemplateTypeParmDecl>(D) ||
153 isa<NonTypeTemplateParmDecl>(D) ||
154 isa<TemplateTemplateParmDecl>(D))
155 return {};
156
157 // Find declaration location.
158 // For Objective-C declarations we generally don't expect to have multiple
159 // declarators, thus use declaration starting location as the "declaration
160 // location".
161 // For all other declarations multiple declarators are used quite frequently,
162 // so we use the location of the identifier as the "declaration location".
163 if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) ||
164 isa<ObjCPropertyDecl>(D) ||
165 isa<RedeclarableTemplateDecl>(D) ||
166 isa<ClassTemplateSpecializationDecl>(D))
167 return D->getBeginLoc();
168 else {
169 const SourceLocation DeclLoc = D->getLocation();
170 if (DeclLoc.isMacroID()) {
171 if (isa<TypedefDecl>(D)) {
172 // If location of the typedef name is in a macro, it is because being
173 // declared via a macro. Try using declaration's starting location as
174 // the "declaration location".
175 return D->getBeginLoc();
176 } else if (const auto *TD = dyn_cast<TagDecl>(D)) {
177 // If location of the tag decl is inside a macro, but the spelling of
178 // the tag name comes from a macro argument, it looks like a special
179 // macro like NS_ENUM is being used to define the tag decl. In that
180 // case, adjust the source location to the expansion loc so that we can
181 // attach the comment to the tag decl.
182 if (SourceMgr.isMacroArgExpansion(DeclLoc) &&
183 TD->isCompleteDefinition())
184 return SourceMgr.getExpansionLoc(DeclLoc);
185 }
186 }
187 return DeclLoc;
188 }
189
190 return {};
191}
192
193RawComment *ASTContext::getRawCommentForDeclNoCacheImpl(
194 const Decl *D, const SourceLocation RepresentativeLocForDecl,
195 const std::map<unsigned, RawComment *> &CommentsInTheFile) const {
196 // If the declaration doesn't map directly to a location in a file, we
197 // can't find the comment.
198 if (RepresentativeLocForDecl.isInvalid() ||
199 !RepresentativeLocForDecl.isFileID())
200 return nullptr;
201
202 // If there are no comments anywhere, we won't find anything.
203 if (CommentsInTheFile.empty())
204 return nullptr;
205
206 // Decompose the location for the declaration and find the beginning of the
207 // file buffer.
208 const std::pair<FileID, unsigned> DeclLocDecomp =
209 SourceMgr.getDecomposedLoc(RepresentativeLocForDecl);
210
211 // Slow path.
212 auto OffsetCommentBehindDecl =
213 CommentsInTheFile.lower_bound(DeclLocDecomp.second);
214
215 // First check whether we have a trailing comment.
216 if (OffsetCommentBehindDecl != CommentsInTheFile.end()) {
217 RawComment *CommentBehindDecl = OffsetCommentBehindDecl->second;
218 if ((CommentBehindDecl->isDocumentation() ||
219 LangOpts.CommentOpts.ParseAllComments) &&
220 CommentBehindDecl->isTrailingComment() &&
221 (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D) ||
222 isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D))) {
223
224 // Check that Doxygen trailing comment comes after the declaration, starts
225 // on the same line and in the same file as the declaration.
226 if (SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) ==
227 Comments.getCommentBeginLine(CommentBehindDecl, DeclLocDecomp.first,
228 OffsetCommentBehindDecl->first)) {
229 return CommentBehindDecl;
230 }
231 }
232 }
233
234 // The comment just after the declaration was not a trailing comment.
235 // Let's look at the previous comment.
236 if (OffsetCommentBehindDecl == CommentsInTheFile.begin())
237 return nullptr;
238
239 auto OffsetCommentBeforeDecl = --OffsetCommentBehindDecl;
240 RawComment *CommentBeforeDecl = OffsetCommentBeforeDecl->second;
241
242 // Check that we actually have a non-member Doxygen comment.
243 if (!(CommentBeforeDecl->isDocumentation() ||
244 LangOpts.CommentOpts.ParseAllComments) ||
245 CommentBeforeDecl->isTrailingComment())
246 return nullptr;
247
248 // Decompose the end of the comment.
249 const unsigned CommentEndOffset =
250 Comments.getCommentEndOffset(CommentBeforeDecl);
251
252 // Get the corresponding buffer.
253 bool Invalid = false;
254 const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first,
255 &Invalid).data();
256 if (Invalid)
257 return nullptr;
258
259 // Extract text between the comment and declaration.
260 StringRef Text(Buffer + CommentEndOffset,
261 DeclLocDecomp.second - CommentEndOffset);
262
263 // There should be no other declarations or preprocessor directives between
264 // comment and declaration.
265 if (Text.find_first_of(";{}#@") != StringRef::npos)
266 return nullptr;
267
268 return CommentBeforeDecl;
269}
270
271RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
272 const SourceLocation DeclLoc = getDeclLocForCommentSearch(D, SourceMgr);
273
274 // If the declaration doesn't map directly to a location in a file, we
275 // can't find the comment.
276 if (DeclLoc.isInvalid() || !DeclLoc.isFileID())
277 return nullptr;
278
279 if (ExternalSource && !CommentsLoaded) {
280 ExternalSource->ReadComments();
281 CommentsLoaded = true;
282 }
283
284 if (Comments.empty())
285 return nullptr;
286
287 const FileID File = SourceMgr.getDecomposedLoc(DeclLoc).first;
288 const auto CommentsInThisFile = Comments.getCommentsInFile(File);
289 if (!CommentsInThisFile || CommentsInThisFile->empty())
290 return nullptr;
291
292 return getRawCommentForDeclNoCacheImpl(D, DeclLoc, *CommentsInThisFile);
293}
294
295/// If we have a 'templated' declaration for a template, adjust 'D' to
296/// refer to the actual template.
297/// If we have an implicit instantiation, adjust 'D' to refer to template.
298static const Decl &adjustDeclToTemplate(const Decl &D) {
299 if (const auto *FD = dyn_cast<FunctionDecl>(&D)) {
300 // Is this function declaration part of a function template?
301 if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate())
302 return *FTD;
303
304 // Nothing to do if function is not an implicit instantiation.
305 if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation)
306 return D;
307
308 // Function is an implicit instantiation of a function template?
309 if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate())
310 return *FTD;
311
312 // Function is instantiated from a member definition of a class template?
313 if (const FunctionDecl *MemberDecl =
314 FD->getInstantiatedFromMemberFunction())
315 return *MemberDecl;
316
317 return D;
318 }
319 if (const auto *VD = dyn_cast<VarDecl>(&D)) {
320 // Static data member is instantiated from a member definition of a class
321 // template?
322 if (VD->isStaticDataMember())
323 if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember())
324 return *MemberDecl;
325
326 return D;
327 }
328 if (const auto *CRD = dyn_cast<CXXRecordDecl>(&D)) {
329 // Is this class declaration part of a class template?
330 if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate())
331 return *CTD;
332
333 // Class is an implicit instantiation of a class template or partial
334 // specialization?
335 if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(CRD)) {
336 if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation)
337 return D;
338 llvm::PointerUnion<ClassTemplateDecl *,
339 ClassTemplatePartialSpecializationDecl *>
340 PU = CTSD->getSpecializedTemplateOrPartial();
341 return PU.is<ClassTemplateDecl *>()
342 ? *static_cast<const Decl *>(PU.get<ClassTemplateDecl *>())
343 : *static_cast<const Decl *>(
344 PU.get<ClassTemplatePartialSpecializationDecl *>());
345 }
346
347 // Class is instantiated from a member definition of a class template?
348 if (const MemberSpecializationInfo *Info =
349 CRD->getMemberSpecializationInfo())
350 return *Info->getInstantiatedFrom();
351
352 return D;
353 }
354 if (const auto *ED = dyn_cast<EnumDecl>(&D)) {
355 // Enum is instantiated from a member definition of a class template?
356 if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum())
357 return *MemberDecl;
358
359 return D;
360 }
361 // FIXME: Adjust alias templates?
362 return D;
363}
364
365const RawComment *ASTContext::getRawCommentForAnyRedecl(
366 const Decl *D,
367 const Decl **OriginalDecl) const {
368 if (!D) {
369 if (OriginalDecl)
370 OriginalDecl = nullptr;
371 return nullptr;
372 }
373
374 D = &adjustDeclToTemplate(*D);
375
376 // Any comment directly attached to D?
377 {
378 auto DeclComment = DeclRawComments.find(D);
379 if (DeclComment != DeclRawComments.end()) {
380 if (OriginalDecl)
381 *OriginalDecl = D;
382 return DeclComment->second;
383 }
384 }
385
386 // Any comment attached to any redeclaration of D?
387 const Decl *CanonicalD = D->getCanonicalDecl();
388 if (!CanonicalD)
389 return nullptr;
390
391 {
392 auto RedeclComment = RedeclChainComments.find(CanonicalD);
393 if (RedeclComment != RedeclChainComments.end()) {
394 if (OriginalDecl)
395 *OriginalDecl = RedeclComment->second;
396 auto CommentAtRedecl = DeclRawComments.find(RedeclComment->second);
397 assert(CommentAtRedecl != DeclRawComments.end() &&((CommentAtRedecl != DeclRawComments.end() && "This decl is supposed to have comment attached."
) ? static_cast<void> (0) : __assert_fail ("CommentAtRedecl != DeclRawComments.end() && \"This decl is supposed to have comment attached.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 398, __PRETTY_FUNCTION__))
398 "This decl is supposed to have comment attached.")((CommentAtRedecl != DeclRawComments.end() && "This decl is supposed to have comment attached."
) ? static_cast<void> (0) : __assert_fail ("CommentAtRedecl != DeclRawComments.end() && \"This decl is supposed to have comment attached.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 398, __PRETTY_FUNCTION__))
;
399 return CommentAtRedecl->second;
400 }
401 }
402
403 // Any redeclarations of D that we haven't checked for comments yet?
404 // We can't use DenseMap::iterator directly since it'd get invalid.
405 auto LastCheckedRedecl = [this, CanonicalD]() -> const Decl * {
406 auto LookupRes = CommentlessRedeclChains.find(CanonicalD);
407 if (LookupRes != CommentlessRedeclChains.end())
408 return LookupRes->second;
409 return nullptr;
410 }();
411
412 for (const auto Redecl : D->redecls()) {
413 assert(Redecl)((Redecl) ? static_cast<void> (0) : __assert_fail ("Redecl"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 413, __PRETTY_FUNCTION__))
;
414 // Skip all redeclarations that have been checked previously.
415 if (LastCheckedRedecl) {
416 if (LastCheckedRedecl == Redecl) {
417 LastCheckedRedecl = nullptr;
418 }
419 continue;
420 }
421 const RawComment *RedeclComment = getRawCommentForDeclNoCache(Redecl);
422 if (RedeclComment) {
423 cacheRawCommentForDecl(*Redecl, *RedeclComment);
424 if (OriginalDecl)
425 *OriginalDecl = Redecl;
426 return RedeclComment;
427 }
428 CommentlessRedeclChains[CanonicalD] = Redecl;
429 }
430
431 if (OriginalDecl)
432 *OriginalDecl = nullptr;
433 return nullptr;
434}
435
436void ASTContext::cacheRawCommentForDecl(const Decl &OriginalD,
437 const RawComment &Comment) const {
438 assert(Comment.isDocumentation() || LangOpts.CommentOpts.ParseAllComments)((Comment.isDocumentation() || LangOpts.CommentOpts.ParseAllComments
) ? static_cast<void> (0) : __assert_fail ("Comment.isDocumentation() || LangOpts.CommentOpts.ParseAllComments"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 438, __PRETTY_FUNCTION__))
;
439 DeclRawComments.try_emplace(&OriginalD, &Comment);
440 const Decl *const CanonicalDecl = OriginalD.getCanonicalDecl();
441 RedeclChainComments.try_emplace(CanonicalDecl, &OriginalD);
442 CommentlessRedeclChains.erase(CanonicalDecl);
443}
444
445static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod,
446 SmallVectorImpl<const NamedDecl *> &Redeclared) {
447 const DeclContext *DC = ObjCMethod->getDeclContext();
448 if (const auto *IMD = dyn_cast<ObjCImplDecl>(DC)) {
449 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
450 if (!ID)
451 return;
452 // Add redeclared method here.
453 for (const auto *Ext : ID->known_extensions()) {
454 if (ObjCMethodDecl *RedeclaredMethod =
455 Ext->getMethod(ObjCMethod->getSelector(),
456 ObjCMethod->isInstanceMethod()))
457 Redeclared.push_back(RedeclaredMethod);
458 }
459 }
460}
461
462void ASTContext::attachCommentsToJustParsedDecls(ArrayRef<Decl *> Decls,
463 const Preprocessor *PP) {
464 if (Comments.empty() || Decls.empty())
465 return;
466
467 // See if there are any new comments that are not attached to a decl.
468 // The location doesn't have to be precise - we care only about the file.
469 const FileID File =
470 SourceMgr.getDecomposedLoc((*Decls.begin())->getLocation()).first;
471 auto CommentsInThisFile = Comments.getCommentsInFile(File);
472 if (!CommentsInThisFile || CommentsInThisFile->empty() ||
473 CommentsInThisFile->rbegin()->second->isAttached())
474 return;
475
476 // There is at least one comment not attached to a decl.
477 // Maybe it should be attached to one of Decls?
478 //
479 // Note that this way we pick up not only comments that precede the
480 // declaration, but also comments that *follow* the declaration -- thanks to
481 // the lookahead in the lexer: we've consumed the semicolon and looked
482 // ahead through comments.
483
484 for (const Decl *D : Decls) {
485 assert(D)((D) ? static_cast<void> (0) : __assert_fail ("D", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 485, __PRETTY_FUNCTION__))
;
486 if (D->isInvalidDecl())
487 continue;
488
489 D = &adjustDeclToTemplate(*D);
490
491 const SourceLocation DeclLoc = getDeclLocForCommentSearch(D, SourceMgr);
492
493 if (DeclLoc.isInvalid() || !DeclLoc.isFileID())
494 continue;
495
496 if (DeclRawComments.count(D) > 0)
497 continue;
498
499 if (RawComment *const DocComment =
500 getRawCommentForDeclNoCacheImpl(D, DeclLoc, *CommentsInThisFile)) {
501 cacheRawCommentForDecl(*D, *DocComment);
502 comments::FullComment *FC = DocComment->parse(*this, PP, D);
503 ParsedComments[D->getCanonicalDecl()] = FC;
504 }
505 }
506}
507
508comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC,
509 const Decl *D) const {
510 auto *ThisDeclInfo = new (*this) comments::DeclInfo;
511 ThisDeclInfo->CommentDecl = D;
512 ThisDeclInfo->IsFilled = false;
513 ThisDeclInfo->fill();
514 ThisDeclInfo->CommentDecl = FC->getDecl();
515 if (!ThisDeclInfo->TemplateParameters)
516 ThisDeclInfo->TemplateParameters = FC->getDeclInfo()->TemplateParameters;
517 comments::FullComment *CFC =
518 new (*this) comments::FullComment(FC->getBlocks(),
519 ThisDeclInfo);
520 return CFC;
521}
522
523comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const {
524 const RawComment *RC = getRawCommentForDeclNoCache(D);
525 return RC ? RC->parse(*this, nullptr, D) : nullptr;
526}
527
528comments::FullComment *ASTContext::getCommentForDecl(
529 const Decl *D,
530 const Preprocessor *PP) const {
531 if (!D || D->isInvalidDecl())
532 return nullptr;
533 D = &adjustDeclToTemplate(*D);
534
535 const Decl *Canonical = D->getCanonicalDecl();
536 llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos =
537 ParsedComments.find(Canonical);
538
539 if (Pos != ParsedComments.end()) {
540 if (Canonical != D) {
541 comments::FullComment *FC = Pos->second;
542 comments::FullComment *CFC = cloneFullComment(FC, D);
543 return CFC;
544 }
545 return Pos->second;
546 }
547
548 const Decl *OriginalDecl = nullptr;
549
550 const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl);
551 if (!RC) {
552 if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) {
553 SmallVector<const NamedDecl*, 8> Overridden;
554 const auto *OMD = dyn_cast<ObjCMethodDecl>(D);
555 if (OMD && OMD->isPropertyAccessor())
556 if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl())
557 if (comments::FullComment *FC = getCommentForDecl(PDecl, PP))
558 return cloneFullComment(FC, D);
559 if (OMD)
560 addRedeclaredMethods(OMD, Overridden);
561 getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden);
562 for (unsigned i = 0, e = Overridden.size(); i < e; i++)
563 if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP))
564 return cloneFullComment(FC, D);
565 }
566 else if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
567 // Attach any tag type's documentation to its typedef if latter
568 // does not have one of its own.
569 QualType QT = TD->getUnderlyingType();
570 if (const auto *TT = QT->getAs<TagType>())
571 if (const Decl *TD = TT->getDecl())
572 if (comments::FullComment *FC = getCommentForDecl(TD, PP))
573 return cloneFullComment(FC, D);
574 }
575 else if (const auto *IC = dyn_cast<ObjCInterfaceDecl>(D)) {
576 while (IC->getSuperClass()) {
577 IC = IC->getSuperClass();
578 if (comments::FullComment *FC = getCommentForDecl(IC, PP))
579 return cloneFullComment(FC, D);
580 }
581 }
582 else if (const auto *CD = dyn_cast<ObjCCategoryDecl>(D)) {
583 if (const ObjCInterfaceDecl *IC = CD->getClassInterface())
584 if (comments::FullComment *FC = getCommentForDecl(IC, PP))
585 return cloneFullComment(FC, D);
586 }
587 else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) {
588 if (!(RD = RD->getDefinition()))
589 return nullptr;
590 // Check non-virtual bases.
591 for (const auto &I : RD->bases()) {
592 if (I.isVirtual() || (I.getAccessSpecifier() != AS_public))
593 continue;
594 QualType Ty = I.getType();
595 if (Ty.isNull())
596 continue;
597 if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) {
598 if (!(NonVirtualBase= NonVirtualBase->getDefinition()))
599 continue;
600
601 if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP))
602 return cloneFullComment(FC, D);
603 }
604 }
605 // Check virtual bases.
606 for (const auto &I : RD->vbases()) {
607 if (I.getAccessSpecifier() != AS_public)
608 continue;
609 QualType Ty = I.getType();
610 if (Ty.isNull())
611 continue;
612 if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) {
613 if (!(VirtualBase= VirtualBase->getDefinition()))
614 continue;
615 if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP))
616 return cloneFullComment(FC, D);
617 }
618 }
619 }
620 return nullptr;
621 }
622
623 // If the RawComment was attached to other redeclaration of this Decl, we
624 // should parse the comment in context of that other Decl. This is important
625 // because comments can contain references to parameter names which can be
626 // different across redeclarations.
627 if (D != OriginalDecl && OriginalDecl)
628 return getCommentForDecl(OriginalDecl, PP);
629
630 comments::FullComment *FC = RC->parse(*this, PP, D);
631 ParsedComments[Canonical] = FC;
632 return FC;
633}
634
635void
636ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
637 TemplateTemplateParmDecl *Parm) {
638 ID.AddInteger(Parm->getDepth());
639 ID.AddInteger(Parm->getPosition());
640 ID.AddBoolean(Parm->isParameterPack());
641
642 TemplateParameterList *Params = Parm->getTemplateParameters();
643 ID.AddInteger(Params->size());
644 for (TemplateParameterList::const_iterator P = Params->begin(),
645 PEnd = Params->end();
646 P != PEnd; ++P) {
647 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
648 ID.AddInteger(0);
649 ID.AddBoolean(TTP->isParameterPack());
650 continue;
651 }
652
653 if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
654 ID.AddInteger(1);
655 ID.AddBoolean(NTTP->isParameterPack());
656 ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr());
657 if (NTTP->isExpandedParameterPack()) {
658 ID.AddBoolean(true);
659 ID.AddInteger(NTTP->getNumExpansionTypes());
660 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
661 QualType T = NTTP->getExpansionType(I);
662 ID.AddPointer(T.getCanonicalType().getAsOpaquePtr());
663 }
664 } else
665 ID.AddBoolean(false);
666 continue;
667 }
668
669 auto *TTP = cast<TemplateTemplateParmDecl>(*P);
670 ID.AddInteger(2);
671 Profile(ID, TTP);
672 }
673}
674
675TemplateTemplateParmDecl *
676ASTContext::getCanonicalTemplateTemplateParmDecl(
677 TemplateTemplateParmDecl *TTP) const {
678 // Check if we already have a canonical template template parameter.
679 llvm::FoldingSetNodeID ID;
680 CanonicalTemplateTemplateParm::Profile(ID, TTP);
681 void *InsertPos = nullptr;
682 CanonicalTemplateTemplateParm *Canonical
683 = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
684 if (Canonical)
685 return Canonical->getParam();
686
687 // Build a canonical template parameter list.
688 TemplateParameterList *Params = TTP->getTemplateParameters();
689 SmallVector<NamedDecl *, 4> CanonParams;
690 CanonParams.reserve(Params->size());
691 for (TemplateParameterList::const_iterator P = Params->begin(),
692 PEnd = Params->end();
693 P != PEnd; ++P) {
694 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
695 CanonParams.push_back(
696 TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
697 SourceLocation(),
698 SourceLocation(),
699 TTP->getDepth(),
700 TTP->getIndex(), nullptr, false,
701 TTP->isParameterPack()));
702 else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
703 QualType T = getCanonicalType(NTTP->getType());
704 TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
705 NonTypeTemplateParmDecl *Param;
706 if (NTTP->isExpandedParameterPack()) {
707 SmallVector<QualType, 2> ExpandedTypes;
708 SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
709 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
710 ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
711 ExpandedTInfos.push_back(
712 getTrivialTypeSourceInfo(ExpandedTypes.back()));
713 }
714
715 Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
716 SourceLocation(),
717 SourceLocation(),
718 NTTP->getDepth(),
719 NTTP->getPosition(), nullptr,
720 T,
721 TInfo,
722 ExpandedTypes,
723 ExpandedTInfos);
724 } else {
725 Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
726 SourceLocation(),
727 SourceLocation(),
728 NTTP->getDepth(),
729 NTTP->getPosition(), nullptr,
730 T,
731 NTTP->isParameterPack(),
732 TInfo);
733 }
734 CanonParams.push_back(Param);
735
736 } else
737 CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
738 cast<TemplateTemplateParmDecl>(*P)));
739 }
740
741 assert(!TTP->getRequiresClause() &&((!TTP->getRequiresClause() && "Unexpected requires-clause on template template-parameter"
) ? static_cast<void> (0) : __assert_fail ("!TTP->getRequiresClause() && \"Unexpected requires-clause on template template-parameter\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 742, __PRETTY_FUNCTION__))
742 "Unexpected requires-clause on template template-parameter")((!TTP->getRequiresClause() && "Unexpected requires-clause on template template-parameter"
) ? static_cast<void> (0) : __assert_fail ("!TTP->getRequiresClause() && \"Unexpected requires-clause on template template-parameter\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 742, __PRETTY_FUNCTION__))
;
743 Expr *const CanonRequiresClause = nullptr;
744
745 TemplateTemplateParmDecl *CanonTTP
746 = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
747 SourceLocation(), TTP->getDepth(),
748 TTP->getPosition(),
749 TTP->isParameterPack(),
750 nullptr,
751 TemplateParameterList::Create(*this, SourceLocation(),
752 SourceLocation(),
753 CanonParams,
754 SourceLocation(),
755 CanonRequiresClause));
756
757 // Get the new insert position for the node we care about.
758 Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
759 assert(!Canonical && "Shouldn't be in the map!")((!Canonical && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!Canonical && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 759, __PRETTY_FUNCTION__))
;
760 (void)Canonical;
761
762 // Create the canonical template template parameter entry.
763 Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
764 CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
765 return CanonTTP;
766}
767
768CXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
769 if (!LangOpts.CPlusPlus) return nullptr;
770
771 switch (T.getCXXABI().getKind()) {
772 case TargetCXXABI::GenericARM: // Same as Itanium at this level
773 case TargetCXXABI::iOS:
774 case TargetCXXABI::iOS64:
775 case TargetCXXABI::WatchOS:
776 case TargetCXXABI::GenericAArch64:
777 case TargetCXXABI::GenericMIPS:
778 case TargetCXXABI::GenericItanium:
779 case TargetCXXABI::WebAssembly:
780 return CreateItaniumCXXABI(*this);
781 case TargetCXXABI::Microsoft:
782 return CreateMicrosoftCXXABI(*this);
783 }
784 llvm_unreachable("Invalid CXXABI type!")::llvm::llvm_unreachable_internal("Invalid CXXABI type!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 784)
;
785}
786
787interp::Context &ASTContext::getInterpContext() {
788 if (!InterpContext) {
789 InterpContext.reset(new interp::Context(*this));
790 }
791 return *InterpContext.get();
792}
793
794static const LangASMap *getAddressSpaceMap(const TargetInfo &T,
795 const LangOptions &LOpts) {
796 if (LOpts.FakeAddressSpaceMap) {
797 // The fake address space map must have a distinct entry for each
798 // language-specific address space.
799 static const unsigned FakeAddrSpaceMap[] = {
800 0, // Default
801 1, // opencl_global
802 3, // opencl_local
803 2, // opencl_constant
804 0, // opencl_private
805 4, // opencl_generic
806 5, // cuda_device
807 6, // cuda_constant
808 7 // cuda_shared
809 };
810 return &FakeAddrSpaceMap;
811 } else {
812 return &T.getAddressSpaceMap();
813 }
814}
815
816static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI,
817 const LangOptions &LangOpts) {
818 switch (LangOpts.getAddressSpaceMapMangling()) {
819 case LangOptions::ASMM_Target:
820 return TI.useAddressSpaceMapMangling();
821 case LangOptions::ASMM_On:
822 return true;
823 case LangOptions::ASMM_Off:
824 return false;
825 }
826 llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything.")::llvm::llvm_unreachable_internal("getAddressSpaceMapMangling() doesn't cover anything."
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 826)
;
827}
828
829ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
830 IdentifierTable &idents, SelectorTable &sels,
831 Builtin::Context &builtins)
832 : FunctionProtoTypes(this_()), TemplateSpecializationTypes(this_()),
833 DependentTemplateSpecializationTypes(this_()),
834 SubstTemplateTemplateParmPacks(this_()), SourceMgr(SM), LangOpts(LOpts),
835 SanitizerBL(new SanitizerBlacklist(LangOpts.SanitizerBlacklistFiles, SM)),
836 XRayFilter(new XRayFunctionFilter(LangOpts.XRayAlwaysInstrumentFiles,
837 LangOpts.XRayNeverInstrumentFiles,
838 LangOpts.XRayAttrListFiles, SM)),
839 PrintingPolicy(LOpts), Idents(idents), Selectors(sels),
840 BuiltinInfo(builtins), DeclarationNames(*this), Comments(SM),
841 CommentCommandTraits(BumpAlloc, LOpts.CommentOpts),
842 CompCategories(this_()), LastSDM(nullptr, 0) {
843 TUDecl = TranslationUnitDecl::Create(*this);
844 TraversalScope = {TUDecl};
845}
846
847ASTContext::~ASTContext() {
848 // Release the DenseMaps associated with DeclContext objects.
849 // FIXME: Is this the ideal solution?
850 ReleaseDeclContextMaps();
851
852 // Call all of the deallocation functions on all of their targets.
853 for (auto &Pair : Deallocations)
854 (Pair.first)(Pair.second);
855
856 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
857 // because they can contain DenseMaps.
858 for (llvm::DenseMap<const ObjCContainerDecl*,
859 const ASTRecordLayout*>::iterator
860 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
861 // Increment in loop to prevent using deallocated memory.
862 if (auto *R = const_cast<ASTRecordLayout *>((I++)->second))
863 R->Destroy(*this);
864
865 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
866 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
867 // Increment in loop to prevent using deallocated memory.
868 if (auto *R = const_cast<ASTRecordLayout *>((I++)->second))
869 R->Destroy(*this);
870 }
871
872 for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
873 AEnd = DeclAttrs.end();
874 A != AEnd; ++A)
875 A->second->~AttrVec();
876
877 for (std::pair<const MaterializeTemporaryExpr *, APValue *> &MTVPair :
878 MaterializedTemporaryValues)
879 MTVPair.second->~APValue();
880
881 for (const auto &Value : ModuleInitializers)
882 Value.second->~PerModuleInitializers();
883
884 for (APValue *Value : APValueCleanups)
885 Value->~APValue();
886}
887
888class ASTContext::ParentMap {
889 /// Contains parents of a node.
890 using ParentVector = llvm::SmallVector<ast_type_traits::DynTypedNode, 2>;
891
892 /// Maps from a node to its parents. This is used for nodes that have
893 /// pointer identity only, which are more common and we can save space by
894 /// only storing a unique pointer to them.
895 using ParentMapPointers = llvm::DenseMap<
896 const void *,
897 llvm::PointerUnion4<const Decl *, const Stmt *,
898 ast_type_traits::DynTypedNode *, ParentVector *>>;
899
900 /// Parent map for nodes without pointer identity. We store a full
901 /// DynTypedNode for all keys.
902 using ParentMapOtherNodes = llvm::DenseMap<
903 ast_type_traits::DynTypedNode,
904 llvm::PointerUnion4<const Decl *, const Stmt *,
905 ast_type_traits::DynTypedNode *, ParentVector *>>;
906
907 ParentMapPointers PointerParents;
908 ParentMapOtherNodes OtherParents;
909 class ASTVisitor;
910
911 static ast_type_traits::DynTypedNode
912 getSingleDynTypedNodeFromParentMap(ParentMapPointers::mapped_type U) {
913 if (const auto *D = U.dyn_cast<const Decl *>())
914 return ast_type_traits::DynTypedNode::create(*D);
915 if (const auto *S = U.dyn_cast<const Stmt *>())
916 return ast_type_traits::DynTypedNode::create(*S);
917 return *U.get<ast_type_traits::DynTypedNode *>();
918 }
919
920 template <typename NodeTy, typename MapTy>
921 static ASTContext::DynTypedNodeList getDynNodeFromMap(const NodeTy &Node,
922 const MapTy &Map) {
923 auto I = Map.find(Node);
924 if (I == Map.end()) {
925 return llvm::ArrayRef<ast_type_traits::DynTypedNode>();
926 }
927 if (const auto *V = I->second.template dyn_cast<ParentVector *>()) {
928 return llvm::makeArrayRef(*V);
929 }
930 return getSingleDynTypedNodeFromParentMap(I->second);
931 }
932
933public:
934 ParentMap(ASTContext &Ctx);
935 ~ParentMap() {
936 for (const auto &Entry : PointerParents) {
937 if (Entry.second.is<ast_type_traits::DynTypedNode *>()) {
938 delete Entry.second.get<ast_type_traits::DynTypedNode *>();
939 } else if (Entry.second.is<ParentVector *>()) {
940 delete Entry.second.get<ParentVector *>();
941 }
942 }
943 for (const auto &Entry : OtherParents) {
944 if (Entry.second.is<ast_type_traits::DynTypedNode *>()) {
945 delete Entry.second.get<ast_type_traits::DynTypedNode *>();
946 } else if (Entry.second.is<ParentVector *>()) {
947 delete Entry.second.get<ParentVector *>();
948 }
949 }
950 }
951
952 DynTypedNodeList getParents(const ast_type_traits::DynTypedNode &Node) {
953 if (Node.getNodeKind().hasPointerIdentity())
954 return getDynNodeFromMap(Node.getMemoizationData(), PointerParents);
955 return getDynNodeFromMap(Node, OtherParents);
956 }
957};
958
959void ASTContext::setTraversalScope(const std::vector<Decl *> &TopLevelDecls) {
960 TraversalScope = TopLevelDecls;
961 Parents.reset();
962}
963
964void ASTContext::AddDeallocation(void (*Callback)(void *), void *Data) const {
965 Deallocations.push_back({Callback, Data});
966}
967
968void
969ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) {
970 ExternalSource = std::move(Source);
971}
972
973void ASTContext::PrintStats() const {
974 llvm::errs() << "\n*** AST Context Stats:\n";
975 llvm::errs() << " " << Types.size() << " types total.\n";
976
977 unsigned counts[] = {
978#define TYPE(Name, Parent) 0,
979#define ABSTRACT_TYPE(Name, Parent)
980#include "clang/AST/TypeNodes.inc"
981 0 // Extra
982 };
983
984 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
985 Type *T = Types[i];
986 counts[(unsigned)T->getTypeClass()]++;
987 }
988
989 unsigned Idx = 0;
990 unsigned TotalBytes = 0;
991#define TYPE(Name, Parent) \
992 if (counts[Idx]) \
993 llvm::errs() << " " << counts[Idx] << " " << #Name \
994 << " types, " << sizeof(Name##Type) << " each " \
995 << "(" << counts[Idx] * sizeof(Name##Type) \
996 << " bytes)\n"; \
997 TotalBytes += counts[Idx] * sizeof(Name##Type); \
998 ++Idx;
999#define ABSTRACT_TYPE(Name, Parent)
1000#include "clang/AST/TypeNodes.inc"
1001
1002 llvm::errs() << "Total bytes = " << TotalBytes << "\n";
1003
1004 // Implicit special member functions.
1005 llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/"
1006 << NumImplicitDefaultConstructors
1007 << " implicit default constructors created\n";
1008 llvm::errs() << NumImplicitCopyConstructorsDeclared << "/"
1009 << NumImplicitCopyConstructors
1010 << " implicit copy constructors created\n";
1011 if (getLangOpts().CPlusPlus)
1012 llvm::errs() << NumImplicitMoveConstructorsDeclared << "/"
1013 << NumImplicitMoveConstructors
1014 << " implicit move constructors created\n";
1015 llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/"
1016 << NumImplicitCopyAssignmentOperators
1017 << " implicit copy assignment operators created\n";
1018 if (getLangOpts().CPlusPlus)
1019 llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/"
1020 << NumImplicitMoveAssignmentOperators
1021 << " implicit move assignment operators created\n";
1022 llvm::errs() << NumImplicitDestructorsDeclared << "/"
1023 << NumImplicitDestructors
1024 << " implicit destructors created\n";
1025
1026 if (ExternalSource) {
1027 llvm::errs() << "\n";
1028 ExternalSource->PrintStats();
1029 }
1030
1031 BumpAlloc.PrintStats();
1032}
1033
1034void ASTContext::mergeDefinitionIntoModule(NamedDecl *ND, Module *M,
1035 bool NotifyListeners) {
1036 if (NotifyListeners)
1037 if (auto *Listener = getASTMutationListener())
1038 Listener->RedefinedHiddenDefinition(ND, M);
1039
1040 MergedDefModules[cast<NamedDecl>(ND->getCanonicalDecl())].push_back(M);
1041}
1042
1043void ASTContext::deduplicateMergedDefinitonsFor(NamedDecl *ND) {
1044 auto It = MergedDefModules.find(cast<NamedDecl>(ND->getCanonicalDecl()));
1045 if (It == MergedDefModules.end())
1046 return;
1047
1048 auto &Merged = It->second;
1049 llvm::DenseSet<Module*> Found;
1050 for (Module *&M : Merged)
1051 if (!Found.insert(M).second)
1052 M = nullptr;
1053 Merged.erase(std::remove(Merged.begin(), Merged.end(), nullptr), Merged.end());
1054}
1055
1056void ASTContext::PerModuleInitializers::resolve(ASTContext &Ctx) {
1057 if (LazyInitializers.empty())
1058 return;
1059
1060 auto *Source = Ctx.getExternalSource();
1061 assert(Source && "lazy initializers but no external source")((Source && "lazy initializers but no external source"
) ? static_cast<void> (0) : __assert_fail ("Source && \"lazy initializers but no external source\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1061, __PRETTY_FUNCTION__))
;
1062
1063 auto LazyInits = std::move(LazyInitializers);
1064 LazyInitializers.clear();
1065
1066 for (auto ID : LazyInits)
1067 Initializers.push_back(Source->GetExternalDecl(ID));
1068
1069 assert(LazyInitializers.empty() &&((LazyInitializers.empty() && "GetExternalDecl for lazy module initializer added more inits"
) ? static_cast<void> (0) : __assert_fail ("LazyInitializers.empty() && \"GetExternalDecl for lazy module initializer added more inits\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1070, __PRETTY_FUNCTION__))
1070 "GetExternalDecl for lazy module initializer added more inits")((LazyInitializers.empty() && "GetExternalDecl for lazy module initializer added more inits"
) ? static_cast<void> (0) : __assert_fail ("LazyInitializers.empty() && \"GetExternalDecl for lazy module initializer added more inits\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1070, __PRETTY_FUNCTION__))
;
1071}
1072
1073void ASTContext::addModuleInitializer(Module *M, Decl *D) {
1074 // One special case: if we add a module initializer that imports another
1075 // module, and that module's only initializer is an ImportDecl, simplify.
1076 if (const auto *ID = dyn_cast<ImportDecl>(D)) {
1077 auto It = ModuleInitializers.find(ID->getImportedModule());
1078
1079 // Maybe the ImportDecl does nothing at all. (Common case.)
1080 if (It == ModuleInitializers.end())
1081 return;
1082
1083 // Maybe the ImportDecl only imports another ImportDecl.
1084 auto &Imported = *It->second;
1085 if (Imported.Initializers.size() + Imported.LazyInitializers.size() == 1) {
1086 Imported.resolve(*this);
1087 auto *OnlyDecl = Imported.Initializers.front();
1088 if (isa<ImportDecl>(OnlyDecl))
1089 D = OnlyDecl;
1090 }
1091 }
1092
1093 auto *&Inits = ModuleInitializers[M];
1094 if (!Inits)
1095 Inits = new (*this) PerModuleInitializers;
1096 Inits->Initializers.push_back(D);
1097}
1098
1099void ASTContext::addLazyModuleInitializers(Module *M, ArrayRef<uint32_t> IDs) {
1100 auto *&Inits = ModuleInitializers[M];
1101 if (!Inits)
1102 Inits = new (*this) PerModuleInitializers;
1103 Inits->LazyInitializers.insert(Inits->LazyInitializers.end(),
1104 IDs.begin(), IDs.end());
1105}
1106
1107ArrayRef<Decl *> ASTContext::getModuleInitializers(Module *M) {
1108 auto It = ModuleInitializers.find(M);
1109 if (It == ModuleInitializers.end())
1110 return None;
1111
1112 auto *Inits = It->second;
1113 Inits->resolve(*this);
1114 return Inits->Initializers;
1115}
1116
1117ExternCContextDecl *ASTContext::getExternCContextDecl() const {
1118 if (!ExternCContext)
1119 ExternCContext = ExternCContextDecl::Create(*this, getTranslationUnitDecl());
1120
1121 return ExternCContext;
1122}
1123
1124BuiltinTemplateDecl *
1125ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK,
1126 const IdentifierInfo *II) const {
1127 auto *BuiltinTemplate = BuiltinTemplateDecl::Create(*this, TUDecl, II, BTK);
1128 BuiltinTemplate->setImplicit();
1129 TUDecl->addDecl(BuiltinTemplate);
1130
1131 return BuiltinTemplate;
1132}
1133
1134BuiltinTemplateDecl *
1135ASTContext::getMakeIntegerSeqDecl() const {
1136 if (!MakeIntegerSeqDecl)
1137 MakeIntegerSeqDecl = buildBuiltinTemplateDecl(BTK__make_integer_seq,
1138 getMakeIntegerSeqName());
1139 return MakeIntegerSeqDecl;
1140}
1141
1142BuiltinTemplateDecl *
1143ASTContext::getTypePackElementDecl() const {
1144 if (!TypePackElementDecl)
1145 TypePackElementDecl = buildBuiltinTemplateDecl(BTK__type_pack_element,
1146 getTypePackElementName());
1147 return TypePackElementDecl;
1148}
1149
1150RecordDecl *ASTContext::buildImplicitRecord(StringRef Name,
1151 RecordDecl::TagKind TK) const {
1152 SourceLocation Loc;
1153 RecordDecl *NewDecl;
1154 if (getLangOpts().CPlusPlus)
1155 NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc,
1156 Loc, &Idents.get(Name));
1157 else
1158 NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc,
1159 &Idents.get(Name));
1160 NewDecl->setImplicit();
1161 NewDecl->addAttr(TypeVisibilityAttr::CreateImplicit(
1162 const_cast<ASTContext &>(*this), TypeVisibilityAttr::Default));
1163 return NewDecl;
1164}
1165
1166TypedefDecl *ASTContext::buildImplicitTypedef(QualType T,
1167 StringRef Name) const {
1168 TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
1169 TypedefDecl *NewDecl = TypedefDecl::Create(
1170 const_cast<ASTContext &>(*this), getTranslationUnitDecl(),
1171 SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo);
1172 NewDecl->setImplicit();
1173 return NewDecl;
1174}
1175
1176TypedefDecl *ASTContext::getInt128Decl() const {
1177 if (!Int128Decl)
1178 Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t");
1179 return Int128Decl;
1180}
1181
1182TypedefDecl *ASTContext::getUInt128Decl() const {
1183 if (!UInt128Decl)
1184 UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t");
1185 return UInt128Decl;
1186}
1187
1188void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
1189 auto *Ty = new (*this, TypeAlignment) BuiltinType(K);
1190 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
1191 Types.push_back(Ty);
1192}
1193
1194void ASTContext::InitBuiltinTypes(const TargetInfo &Target,
1195 const TargetInfo *AuxTarget) {
1196 assert((!this->Target || this->Target == &Target) &&(((!this->Target || this->Target == &Target) &&
"Incorrect target reinitialization") ? static_cast<void>
(0) : __assert_fail ("(!this->Target || this->Target == &Target) && \"Incorrect target reinitialization\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1197, __PRETTY_FUNCTION__))
1197 "Incorrect target reinitialization")(((!this->Target || this->Target == &Target) &&
"Incorrect target reinitialization") ? static_cast<void>
(0) : __assert_fail ("(!this->Target || this->Target == &Target) && \"Incorrect target reinitialization\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1197, __PRETTY_FUNCTION__))
;
1198 assert(VoidTy.isNull() && "Context reinitialized?")((VoidTy.isNull() && "Context reinitialized?") ? static_cast
<void> (0) : __assert_fail ("VoidTy.isNull() && \"Context reinitialized?\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1198, __PRETTY_FUNCTION__))
;
1199
1200 this->Target = &Target;
1201 this->AuxTarget = AuxTarget;
1202
1203 ABI.reset(createCXXABI(Target));
1204 AddrSpaceMap = getAddressSpaceMap(Target, LangOpts);
1205 AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(Target, LangOpts);
1206
1207 // C99 6.2.5p19.
1208 InitBuiltinType(VoidTy, BuiltinType::Void);
1209
1210 // C99 6.2.5p2.
1211 InitBuiltinType(BoolTy, BuiltinType::Bool);
1212 // C99 6.2.5p3.
1213 if (LangOpts.CharIsSigned)
1214 InitBuiltinType(CharTy, BuiltinType::Char_S);
1215 else
1216 InitBuiltinType(CharTy, BuiltinType::Char_U);
1217 // C99 6.2.5p4.
1218 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
1219 InitBuiltinType(ShortTy, BuiltinType::Short);
1220 InitBuiltinType(IntTy, BuiltinType::Int);
1221 InitBuiltinType(LongTy, BuiltinType::Long);
1222 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
1223
1224 // C99 6.2.5p6.
1225 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
1226 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
1227 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
1228 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
1229 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
1230
1231 // C99 6.2.5p10.
1232 InitBuiltinType(FloatTy, BuiltinType::Float);
1233 InitBuiltinType(DoubleTy, BuiltinType::Double);
1234 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
1235
1236 // GNU extension, __float128 for IEEE quadruple precision
1237 InitBuiltinType(Float128Ty, BuiltinType::Float128);
1238
1239 // C11 extension ISO/IEC TS 18661-3
1240 InitBuiltinType(Float16Ty, BuiltinType::Float16);
1241
1242 // ISO/IEC JTC1 SC22 WG14 N1169 Extension
1243 InitBuiltinType(ShortAccumTy, BuiltinType::ShortAccum);
1244 InitBuiltinType(AccumTy, BuiltinType::Accum);
1245 InitBuiltinType(LongAccumTy, BuiltinType::LongAccum);
1246 InitBuiltinType(UnsignedShortAccumTy, BuiltinType::UShortAccum);
1247 InitBuiltinType(UnsignedAccumTy, BuiltinType::UAccum);
1248 InitBuiltinType(UnsignedLongAccumTy, BuiltinType::ULongAccum);
1249 InitBuiltinType(ShortFractTy, BuiltinType::ShortFract);
1250 InitBuiltinType(FractTy, BuiltinType::Fract);
1251 InitBuiltinType(LongFractTy, BuiltinType::LongFract);
1252 InitBuiltinType(UnsignedShortFractTy, BuiltinType::UShortFract);
1253 InitBuiltinType(UnsignedFractTy, BuiltinType::UFract);
1254 InitBuiltinType(UnsignedLongFractTy, BuiltinType::ULongFract);
1255 InitBuiltinType(SatShortAccumTy, BuiltinType::SatShortAccum);
1256 InitBuiltinType(SatAccumTy, BuiltinType::SatAccum);
1257 InitBuiltinType(SatLongAccumTy, BuiltinType::SatLongAccum);
1258 InitBuiltinType(SatUnsignedShortAccumTy, BuiltinType::SatUShortAccum);
1259 InitBuiltinType(SatUnsignedAccumTy, BuiltinType::SatUAccum);
1260 InitBuiltinType(SatUnsignedLongAccumTy, BuiltinType::SatULongAccum);
1261 InitBuiltinType(SatShortFractTy, BuiltinType::SatShortFract);
1262 InitBuiltinType(SatFractTy, BuiltinType::SatFract);
1263 InitBuiltinType(SatLongFractTy, BuiltinType::SatLongFract);
1264 InitBuiltinType(SatUnsignedShortFractTy, BuiltinType::SatUShortFract);
1265 InitBuiltinType(SatUnsignedFractTy, BuiltinType::SatUFract);
1266 InitBuiltinType(SatUnsignedLongFractTy, BuiltinType::SatULongFract);
1267
1268 // GNU extension, 128-bit integers.
1269 InitBuiltinType(Int128Ty, BuiltinType::Int128);
1270 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
1271
1272 // C++ 3.9.1p5
1273 if (TargetInfo::isTypeSigned(Target.getWCharType()))
1274 InitBuiltinType(WCharTy, BuiltinType::WChar_S);
1275 else // -fshort-wchar makes wchar_t be unsigned.
1276 InitBuiltinType(WCharTy, BuiltinType::WChar_U);
1277 if (LangOpts.CPlusPlus && LangOpts.WChar)
1278 WideCharTy = WCharTy;
1279 else {
1280 // C99 (or C++ using -fno-wchar).
1281 WideCharTy = getFromTargetType(Target.getWCharType());
1282 }
1283
1284 WIntTy = getFromTargetType(Target.getWIntType());
1285
1286 // C++20 (proposed)
1287 InitBuiltinType(Char8Ty, BuiltinType::Char8);
1288
1289 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
1290 InitBuiltinType(Char16Ty, BuiltinType::Char16);
1291 else // C99
1292 Char16Ty = getFromTargetType(Target.getChar16Type());
1293
1294 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
1295 InitBuiltinType(Char32Ty, BuiltinType::Char32);
1296 else // C99
1297 Char32Ty = getFromTargetType(Target.getChar32Type());
1298
1299 // Placeholder type for type-dependent expressions whose type is
1300 // completely unknown. No code should ever check a type against
1301 // DependentTy and users should never see it; however, it is here to
1302 // help diagnose failures to properly check for type-dependent
1303 // expressions.
1304 InitBuiltinType(DependentTy, BuiltinType::Dependent);
1305
1306 // Placeholder type for functions.
1307 InitBuiltinType(OverloadTy, BuiltinType::Overload);
1308
1309 // Placeholder type for bound members.
1310 InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember);
1311
1312 // Placeholder type for pseudo-objects.
1313 InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject);
1314
1315 // "any" type; useful for debugger-like clients.
1316 InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny);
1317
1318 // Placeholder type for unbridged ARC casts.
1319 InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast);
1320
1321 // Placeholder type for builtin functions.
1322 InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn);
1323
1324 // Placeholder type for OMP array sections.
1325 if (LangOpts.OpenMP)
1326 InitBuiltinType(OMPArraySectionTy, BuiltinType::OMPArraySection);
1327
1328 // C99 6.2.5p11.
1329 FloatComplexTy = getComplexType(FloatTy);
1330 DoubleComplexTy = getComplexType(DoubleTy);
1331 LongDoubleComplexTy = getComplexType(LongDoubleTy);
1332 Float128ComplexTy = getComplexType(Float128Ty);
1333
1334 // Builtin types for 'id', 'Class', and 'SEL'.
1335 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
1336 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
1337 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
1338
1339 if (LangOpts.OpenCL) {
1340#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1341 InitBuiltinType(SingletonId, BuiltinType::Id);
1342#include "clang/Basic/OpenCLImageTypes.def"
1343
1344 InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler);
1345 InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent);
1346 InitBuiltinType(OCLClkEventTy, BuiltinType::OCLClkEvent);
1347 InitBuiltinType(OCLQueueTy, BuiltinType::OCLQueue);
1348 InitBuiltinType(OCLReserveIDTy, BuiltinType::OCLReserveID);
1349
1350#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
1351 InitBuiltinType(Id##Ty, BuiltinType::Id);
1352#include "clang/Basic/OpenCLExtensionTypes.def"
1353 }
1354
1355 if (Target.hasAArch64SVETypes()) {
1356#define SVE_TYPE(Name, Id, SingletonId) \
1357 InitBuiltinType(SingletonId, BuiltinType::Id);
1358#include "clang/Basic/AArch64SVEACLETypes.def"
1359 }
1360
1361 // Builtin type for __objc_yes and __objc_no
1362 ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ?
1363 SignedCharTy : BoolTy);
1364
1365 ObjCConstantStringType = QualType();
1366
1367 ObjCSuperType = QualType();
1368
1369 // void * type
1370 if (LangOpts.OpenCLVersion >= 200) {
1371 auto Q = VoidTy.getQualifiers();
1372 Q.setAddressSpace(LangAS::opencl_generic);
1373 VoidPtrTy = getPointerType(getCanonicalType(
1374 getQualifiedType(VoidTy.getUnqualifiedType(), Q)));
1375 } else {
1376 VoidPtrTy = getPointerType(VoidTy);
1377 }
1378
1379 // nullptr type (C++0x 2.14.7)
1380 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
1381
1382 // half type (OpenCL 6.1.1.1) / ARM NEON __fp16
1383 InitBuiltinType(HalfTy, BuiltinType::Half);
1384
1385 // Builtin type used to help define __builtin_va_list.
1386 VaListTagDecl = nullptr;
1387}
1388
1389DiagnosticsEngine &ASTContext::getDiagnostics() const {
1390 return SourceMgr.getDiagnostics();
1391}
1392
1393AttrVec& ASTContext::getDeclAttrs(const Decl *D) {
1394 AttrVec *&Result = DeclAttrs[D];
1395 if (!Result) {
1396 void *Mem = Allocate(sizeof(AttrVec));
1397 Result = new (Mem) AttrVec;
1398 }
1399
1400 return *Result;
1401}
1402
1403/// Erase the attributes corresponding to the given declaration.
1404void ASTContext::eraseDeclAttrs(const Decl *D) {
1405 llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
1406 if (Pos != DeclAttrs.end()) {
1407 Pos->second->~AttrVec();
1408 DeclAttrs.erase(Pos);
1409 }
1410}
1411
1412// FIXME: Remove ?
1413MemberSpecializationInfo *
1414ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
1415 assert(Var->isStaticDataMember() && "Not a static data member")((Var->isStaticDataMember() && "Not a static data member"
) ? static_cast<void> (0) : __assert_fail ("Var->isStaticDataMember() && \"Not a static data member\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1415, __PRETTY_FUNCTION__))
;
1416 return getTemplateOrSpecializationInfo(Var)
1417 .dyn_cast<MemberSpecializationInfo *>();
1418}
1419
1420ASTContext::TemplateOrSpecializationInfo
1421ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) {
1422 llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos =
1423 TemplateOrInstantiation.find(Var);
1424 if (Pos == TemplateOrInstantiation.end())
1425 return {};
1426
1427 return Pos->second;
1428}
1429
1430void
1431ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
1432 TemplateSpecializationKind TSK,
1433 SourceLocation PointOfInstantiation) {
1434 assert(Inst->isStaticDataMember() && "Not a static data member")((Inst->isStaticDataMember() && "Not a static data member"
) ? static_cast<void> (0) : __assert_fail ("Inst->isStaticDataMember() && \"Not a static data member\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1434, __PRETTY_FUNCTION__))
;
1435 assert(Tmpl->isStaticDataMember() && "Not a static data member")((Tmpl->isStaticDataMember() && "Not a static data member"
) ? static_cast<void> (0) : __assert_fail ("Tmpl->isStaticDataMember() && \"Not a static data member\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1435, __PRETTY_FUNCTION__))
;
1436 setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo(
1437 Tmpl, TSK, PointOfInstantiation));
1438}
1439
1440void
1441ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst,
1442 TemplateOrSpecializationInfo TSI) {
1443 assert(!TemplateOrInstantiation[Inst] &&((!TemplateOrInstantiation[Inst] && "Already noted what the variable was instantiated from"
) ? static_cast<void> (0) : __assert_fail ("!TemplateOrInstantiation[Inst] && \"Already noted what the variable was instantiated from\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1444, __PRETTY_FUNCTION__))
1444 "Already noted what the variable was instantiated from")((!TemplateOrInstantiation[Inst] && "Already noted what the variable was instantiated from"
) ? static_cast<void> (0) : __assert_fail ("!TemplateOrInstantiation[Inst] && \"Already noted what the variable was instantiated from\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1444, __PRETTY_FUNCTION__))
;
1445 TemplateOrInstantiation[Inst] = TSI;
1446}
1447
1448NamedDecl *
1449ASTContext::getInstantiatedFromUsingDecl(NamedDecl *UUD) {
1450 auto Pos = InstantiatedFromUsingDecl.find(UUD);
1451 if (Pos == InstantiatedFromUsingDecl.end())
1452 return nullptr;
1453
1454 return Pos->second;
1455}
1456
1457void
1458ASTContext::setInstantiatedFromUsingDecl(NamedDecl *Inst, NamedDecl *Pattern) {
1459 assert((isa<UsingDecl>(Pattern) ||(((isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl
>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern
)) && "pattern decl is not a using decl") ? static_cast
<void> (0) : __assert_fail ("(isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern)) && \"pattern decl is not a using decl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1462, __PRETTY_FUNCTION__))
1460 isa<UnresolvedUsingValueDecl>(Pattern) ||(((isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl
>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern
)) && "pattern decl is not a using decl") ? static_cast
<void> (0) : __assert_fail ("(isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern)) && \"pattern decl is not a using decl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1462, __PRETTY_FUNCTION__))
1461 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&(((isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl
>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern
)) && "pattern decl is not a using decl") ? static_cast
<void> (0) : __assert_fail ("(isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern)) && \"pattern decl is not a using decl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1462, __PRETTY_FUNCTION__))
1462 "pattern decl is not a using decl")(((isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl
>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern
)) && "pattern decl is not a using decl") ? static_cast
<void> (0) : __assert_fail ("(isa<UsingDecl>(Pattern) || isa<UnresolvedUsingValueDecl>(Pattern) || isa<UnresolvedUsingTypenameDecl>(Pattern)) && \"pattern decl is not a using decl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1462, __PRETTY_FUNCTION__))
;
1463 assert((isa<UsingDecl>(Inst) ||(((isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl
>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) &&
"instantiation did not produce a using decl") ? static_cast<
void> (0) : __assert_fail ("(isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) && \"instantiation did not produce a using decl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1466, __PRETTY_FUNCTION__))
1464 isa<UnresolvedUsingValueDecl>(Inst) ||(((isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl
>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) &&
"instantiation did not produce a using decl") ? static_cast<
void> (0) : __assert_fail ("(isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) && \"instantiation did not produce a using decl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1466, __PRETTY_FUNCTION__))
1465 isa<UnresolvedUsingTypenameDecl>(Inst)) &&(((isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl
>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) &&
"instantiation did not produce a using decl") ? static_cast<
void> (0) : __assert_fail ("(isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) && \"instantiation did not produce a using decl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1466, __PRETTY_FUNCTION__))
1466 "instantiation did not produce a using decl")(((isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl
>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) &&
"instantiation did not produce a using decl") ? static_cast<
void> (0) : __assert_fail ("(isa<UsingDecl>(Inst) || isa<UnresolvedUsingValueDecl>(Inst) || isa<UnresolvedUsingTypenameDecl>(Inst)) && \"instantiation did not produce a using decl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1466, __PRETTY_FUNCTION__))
;
1467 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists")((!InstantiatedFromUsingDecl[Inst] && "pattern already exists"
) ? static_cast<void> (0) : __assert_fail ("!InstantiatedFromUsingDecl[Inst] && \"pattern already exists\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1467, __PRETTY_FUNCTION__))
;
1468 InstantiatedFromUsingDecl[Inst] = Pattern;
1469}
1470
1471UsingShadowDecl *
1472ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
1473 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
1474 = InstantiatedFromUsingShadowDecl.find(Inst);
1475 if (Pos == InstantiatedFromUsingShadowDecl.end())
1476 return nullptr;
1477
1478 return Pos->second;
1479}
1480
1481void
1482ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
1483 UsingShadowDecl *Pattern) {
1484 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists")((!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"
) ? static_cast<void> (0) : __assert_fail ("!InstantiatedFromUsingShadowDecl[Inst] && \"pattern already exists\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1484, __PRETTY_FUNCTION__))
;
1485 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
1486}
1487
1488FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
1489 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
1490 = InstantiatedFromUnnamedFieldDecl.find(Field);
1491 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
1492 return nullptr;
1493
1494 return Pos->second;
1495}
1496
1497void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
1498 FieldDecl *Tmpl) {
1499 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed")((!Inst->getDeclName() && "Instantiated field decl is not unnamed"
) ? static_cast<void> (0) : __assert_fail ("!Inst->getDeclName() && \"Instantiated field decl is not unnamed\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1499, __PRETTY_FUNCTION__))
;
1500 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed")((!Tmpl->getDeclName() && "Template field decl is not unnamed"
) ? static_cast<void> (0) : __assert_fail ("!Tmpl->getDeclName() && \"Template field decl is not unnamed\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1500, __PRETTY_FUNCTION__))
;
1501 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&((!InstantiatedFromUnnamedFieldDecl[Inst] && "Already noted what unnamed field was instantiated from"
) ? static_cast<void> (0) : __assert_fail ("!InstantiatedFromUnnamedFieldDecl[Inst] && \"Already noted what unnamed field was instantiated from\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1502, __PRETTY_FUNCTION__))
1502 "Already noted what unnamed field was instantiated from")((!InstantiatedFromUnnamedFieldDecl[Inst] && "Already noted what unnamed field was instantiated from"
) ? static_cast<void> (0) : __assert_fail ("!InstantiatedFromUnnamedFieldDecl[Inst] && \"Already noted what unnamed field was instantiated from\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1502, __PRETTY_FUNCTION__))
;
1503
1504 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
1505}
1506
1507ASTContext::overridden_cxx_method_iterator
1508ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
1509 return overridden_methods(Method).begin();
1510}
1511
1512ASTContext::overridden_cxx_method_iterator
1513ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
1514 return overridden_methods(Method).end();
1515}
1516
1517unsigned
1518ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
1519 auto Range = overridden_methods(Method);
1520 return Range.end() - Range.begin();
1521}
1522
1523ASTContext::overridden_method_range
1524ASTContext::overridden_methods(const CXXMethodDecl *Method) const {
1525 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos =
1526 OverriddenMethods.find(Method->getCanonicalDecl());
1527 if (Pos == OverriddenMethods.end())
1528 return overridden_method_range(nullptr, nullptr);
1529 return overridden_method_range(Pos->second.begin(), Pos->second.end());
1530}
1531
1532void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
1533 const CXXMethodDecl *Overridden) {
1534 assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl())((Method->isCanonicalDecl() && Overridden->isCanonicalDecl
()) ? static_cast<void> (0) : __assert_fail ("Method->isCanonicalDecl() && Overridden->isCanonicalDecl()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1534, __PRETTY_FUNCTION__))
;
1535 OverriddenMethods[Method].push_back(Overridden);
1536}
1537
1538void ASTContext::getOverriddenMethods(
1539 const NamedDecl *D,
1540 SmallVectorImpl<const NamedDecl *> &Overridden) const {
1541 assert(D)((D) ? static_cast<void> (0) : __assert_fail ("D", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1541, __PRETTY_FUNCTION__))
;
1542
1543 if (const auto *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
1544 Overridden.append(overridden_methods_begin(CXXMethod),
1545 overridden_methods_end(CXXMethod));
1546 return;
1547 }
1548
1549 const auto *Method = dyn_cast<ObjCMethodDecl>(D);
1550 if (!Method)
1551 return;
1552
1553 SmallVector<const ObjCMethodDecl *, 8> OverDecls;
1554 Method->getOverriddenMethods(OverDecls);
1555 Overridden.append(OverDecls.begin(), OverDecls.end());
1556}
1557
1558void ASTContext::addedLocalImportDecl(ImportDecl *Import) {
1559 assert(!Import->NextLocalImport && "Import declaration already in the chain")((!Import->NextLocalImport && "Import declaration already in the chain"
) ? static_cast<void> (0) : __assert_fail ("!Import->NextLocalImport && \"Import declaration already in the chain\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1559, __PRETTY_FUNCTION__))
;
1560 assert(!Import->isFromASTFile() && "Non-local import declaration")((!Import->isFromASTFile() && "Non-local import declaration"
) ? static_cast<void> (0) : __assert_fail ("!Import->isFromASTFile() && \"Non-local import declaration\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1560, __PRETTY_FUNCTION__))
;
1561 if (!FirstLocalImport) {
1562 FirstLocalImport = Import;
1563 LastLocalImport = Import;
1564 return;
1565 }
1566
1567 LastLocalImport->NextLocalImport = Import;
1568 LastLocalImport = Import;
1569}
1570
1571//===----------------------------------------------------------------------===//
1572// Type Sizing and Analysis
1573//===----------------------------------------------------------------------===//
1574
1575/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
1576/// scalar floating point type.
1577const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
1578 const auto *BT = T->getAs<BuiltinType>();
1579 assert(BT && "Not a floating point type!")((BT && "Not a floating point type!") ? static_cast<
void> (0) : __assert_fail ("BT && \"Not a floating point type!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1579, __PRETTY_FUNCTION__))
;
1580 switch (BT->getKind()) {
1581 default: llvm_unreachable("Not a floating point type!")::llvm::llvm_unreachable_internal("Not a floating point type!"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1581)
;
1582 case BuiltinType::Float16:
1583 case BuiltinType::Half:
1584 return Target->getHalfFormat();
1585 case BuiltinType::Float: return Target->getFloatFormat();
1586 case BuiltinType::Double: return Target->getDoubleFormat();
1587 case BuiltinType::LongDouble:
1588 if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice)
1589 return AuxTarget->getLongDoubleFormat();
1590 return Target->getLongDoubleFormat();
1591 case BuiltinType::Float128:
1592 if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice)
1593 return AuxTarget->getFloat128Format();
1594 return Target->getFloat128Format();
1595 }
1596}
1597
1598CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const {
1599 unsigned Align = Target->getCharWidth();
1600
1601 bool UseAlignAttrOnly = false;
1602 if (unsigned AlignFromAttr = D->getMaxAlignment()) {
1603 Align = AlignFromAttr;
1604
1605 // __attribute__((aligned)) can increase or decrease alignment
1606 // *except* on a struct or struct member, where it only increases
1607 // alignment unless 'packed' is also specified.
1608 //
1609 // It is an error for alignas to decrease alignment, so we can
1610 // ignore that possibility; Sema should diagnose it.
1611 if (isa<FieldDecl>(D)) {
1612 UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
1613 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
1614 } else {
1615 UseAlignAttrOnly = true;
1616 }
1617 }
1618 else if (isa<FieldDecl>(D))
1619 UseAlignAttrOnly =
1620 D->hasAttr<PackedAttr>() ||
1621 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
1622
1623 // If we're using the align attribute only, just ignore everything
1624 // else about the declaration and its type.
1625 if (UseAlignAttrOnly) {
1626 // do nothing
1627 } else if (const auto *VD = dyn_cast<ValueDecl>(D)) {
1628 QualType T = VD->getType();
1629 if (const auto *RT = T->getAs<ReferenceType>()) {
1630 if (ForAlignof)
1631 T = RT->getPointeeType();
1632 else
1633 T = getPointerType(RT->getPointeeType());
1634 }
1635 QualType BaseT = getBaseElementType(T);
1636 if (T->isFunctionType())
1637 Align = getTypeInfoImpl(T.getTypePtr()).Align;
1638 else if (!BaseT->isIncompleteType()) {
1639 // Adjust alignments of declarations with array type by the
1640 // large-array alignment on the target.
1641 if (const ArrayType *arrayType = getAsArrayType(T)) {
1642 unsigned MinWidth = Target->getLargeArrayMinWidth();
1643 if (!ForAlignof && MinWidth) {
1644 if (isa<VariableArrayType>(arrayType))
1645 Align = std::max(Align, Target->getLargeArrayAlign());
1646 else if (isa<ConstantArrayType>(arrayType) &&
1647 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
1648 Align = std::max(Align, Target->getLargeArrayAlign());
1649 }
1650 }
1651 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
1652 if (BaseT.getQualifiers().hasUnaligned())
1653 Align = Target->getCharWidth();
1654 if (const auto *VD = dyn_cast<VarDecl>(D)) {
1655 if (VD->hasGlobalStorage() && !ForAlignof) {
1656 uint64_t TypeSize = getTypeSize(T.getTypePtr());
1657 Align = std::max(Align, getTargetInfo().getMinGlobalAlign(TypeSize));
1658 }
1659 }
1660 }
1661
1662 // Fields can be subject to extra alignment constraints, like if
1663 // the field is packed, the struct is packed, or the struct has a
1664 // a max-field-alignment constraint (#pragma pack). So calculate
1665 // the actual alignment of the field within the struct, and then
1666 // (as we're expected to) constrain that by the alignment of the type.
1667 if (const auto *Field = dyn_cast<FieldDecl>(VD)) {
1668 const RecordDecl *Parent = Field->getParent();
1669 // We can only produce a sensible answer if the record is valid.
1670 if (!Parent->isInvalidDecl()) {
1671 const ASTRecordLayout &Layout = getASTRecordLayout(Parent);
1672
1673 // Start with the record's overall alignment.
1674 unsigned FieldAlign = toBits(Layout.getAlignment());
1675
1676 // Use the GCD of that and the offset within the record.
1677 uint64_t Offset = Layout.getFieldOffset(Field->getFieldIndex());
1678 if (Offset > 0) {
1679 // Alignment is always a power of 2, so the GCD will be a power of 2,
1680 // which means we get to do this crazy thing instead of Euclid's.
1681 uint64_t LowBitOfOffset = Offset & (~Offset + 1);
1682 if (LowBitOfOffset < FieldAlign)
1683 FieldAlign = static_cast<unsigned>(LowBitOfOffset);
1684 }
1685
1686 Align = std::min(Align, FieldAlign);
1687 }
1688 }
1689 }
1690
1691 return toCharUnitsFromBits(Align);
1692}
1693
1694// getTypeInfoDataSizeInChars - Return the size of a type, in
1695// chars. If the type is a record, its data size is returned. This is
1696// the size of the memcpy that's performed when assigning this type
1697// using a trivial copy/move assignment operator.
1698std::pair<CharUnits, CharUnits>
1699ASTContext::getTypeInfoDataSizeInChars(QualType T) const {
1700 std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T);
1701
1702 // In C++, objects can sometimes be allocated into the tail padding
1703 // of a base-class subobject. We decide whether that's possible
1704 // during class layout, so here we can just trust the layout results.
1705 if (getLangOpts().CPlusPlus) {
1706 if (const auto *RT = T->getAs<RecordType>()) {
1707 const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl());
1708 sizeAndAlign.first = layout.getDataSize();
1709 }
1710 }
1711
1712 return sizeAndAlign;
1713}
1714
1715/// getConstantArrayInfoInChars - Performing the computation in CharUnits
1716/// instead of in bits prevents overflowing the uint64_t for some large arrays.
1717std::pair<CharUnits, CharUnits>
1718static getConstantArrayInfoInChars(const ASTContext &Context,
1719 const ConstantArrayType *CAT) {
1720 std::pair<CharUnits, CharUnits> EltInfo =
1721 Context.getTypeInfoInChars(CAT->getElementType());
1722 uint64_t Size = CAT->getSize().getZExtValue();
1723 assert((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <=(((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity
()) <= (uint64_t)(-1)/Size) && "Overflow in array type char size evaluation"
) ? static_cast<void> (0) : __assert_fail ("(Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <= (uint64_t)(-1)/Size) && \"Overflow in array type char size evaluation\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1725, __PRETTY_FUNCTION__))
1724 (uint64_t)(-1)/Size) &&(((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity
()) <= (uint64_t)(-1)/Size) && "Overflow in array type char size evaluation"
) ? static_cast<void> (0) : __assert_fail ("(Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <= (uint64_t)(-1)/Size) && \"Overflow in array type char size evaluation\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1725, __PRETTY_FUNCTION__))
1725 "Overflow in array type char size evaluation")(((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity
()) <= (uint64_t)(-1)/Size) && "Overflow in array type char size evaluation"
) ? static_cast<void> (0) : __assert_fail ("(Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <= (uint64_t)(-1)/Size) && \"Overflow in array type char size evaluation\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1725, __PRETTY_FUNCTION__))
;
1726 uint64_t Width = EltInfo.first.getQuantity() * Size;
1727 unsigned Align = EltInfo.second.getQuantity();
1728 if (!Context.getTargetInfo().getCXXABI().isMicrosoft() ||
1729 Context.getTargetInfo().getPointerWidth(0) == 64)
1730 Width = llvm::alignTo(Width, Align);
1731 return std::make_pair(CharUnits::fromQuantity(Width),
1732 CharUnits::fromQuantity(Align));
1733}
1734
1735std::pair<CharUnits, CharUnits>
1736ASTContext::getTypeInfoInChars(const Type *T) const {
1737 if (const auto *CAT = dyn_cast<ConstantArrayType>(T))
1738 return getConstantArrayInfoInChars(*this, CAT);
1739 TypeInfo Info = getTypeInfo(T);
1740 return std::make_pair(toCharUnitsFromBits(Info.Width),
1741 toCharUnitsFromBits(Info.Align));
1742}
1743
1744std::pair<CharUnits, CharUnits>
1745ASTContext::getTypeInfoInChars(QualType T) const {
1746 return getTypeInfoInChars(T.getTypePtr());
1747}
1748
1749bool ASTContext::isAlignmentRequired(const Type *T) const {
1750 return getTypeInfo(T).AlignIsRequired;
1751}
1752
1753bool ASTContext::isAlignmentRequired(QualType T) const {
1754 return isAlignmentRequired(T.getTypePtr());
1755}
1756
1757unsigned ASTContext::getTypeAlignIfKnown(QualType T) const {
1758 // An alignment on a typedef overrides anything else.
1759 if (const auto *TT = T->getAs<TypedefType>())
1760 if (unsigned Align = TT->getDecl()->getMaxAlignment())
1761 return Align;
1762
1763 // If we have an (array of) complete type, we're done.
1764 T = getBaseElementType(T);
1765 if (!T->isIncompleteType())
1766 return getTypeAlign(T);
1767
1768 // If we had an array type, its element type might be a typedef
1769 // type with an alignment attribute.
1770 if (const auto *TT = T->getAs<TypedefType>())
1771 if (unsigned Align = TT->getDecl()->getMaxAlignment())
1772 return Align;
1773
1774 // Otherwise, see if the declaration of the type had an attribute.
1775 if (const auto *TT = T->getAs<TagType>())
1776 return TT->getDecl()->getMaxAlignment();
1777
1778 return 0;
1779}
1780
1781TypeInfo ASTContext::getTypeInfo(const Type *T) const {
1782 TypeInfoMap::iterator I = MemoizedTypeInfo.find(T);
1783 if (I != MemoizedTypeInfo.end())
1784 return I->second;
1785
1786 // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup.
1787 TypeInfo TI = getTypeInfoImpl(T);
1788 MemoizedTypeInfo[T] = TI;
1789 return TI;
1790}
1791
1792/// getTypeInfoImpl - Return the size of the specified type, in bits. This
1793/// method does not work on incomplete types.
1794///
1795/// FIXME: Pointers into different addr spaces could have different sizes and
1796/// alignment requirements: getPointerInfo should take an AddrSpace, this
1797/// should take a QualType, &c.
1798TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
1799 uint64_t Width = 0;
1800 unsigned Align = 8;
1801 bool AlignIsRequired = false;
1802 unsigned AS = 0;
1803 switch (T->getTypeClass()) {
1804#define TYPE(Class, Base)
1805#define ABSTRACT_TYPE(Class, Base)
1806#define NON_CANONICAL_TYPE(Class, Base)
1807#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1808#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \
1809 case Type::Class: \
1810 assert(!T->isDependentType() && "should not see dependent types here")((!T->isDependentType() && "should not see dependent types here"
) ? static_cast<void> (0) : __assert_fail ("!T->isDependentType() && \"should not see dependent types here\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1810, __PRETTY_FUNCTION__))
; \
1811 return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr());
1812#include "clang/AST/TypeNodes.inc"
1813 llvm_unreachable("Should not see dependent types")::llvm::llvm_unreachable_internal("Should not see dependent types"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1813)
;
1814
1815 case Type::FunctionNoProto:
1816 case Type::FunctionProto:
1817 // GCC extension: alignof(function) = 32 bits
1818 Width = 0;
1819 Align = 32;
1820 break;
1821
1822 case Type::IncompleteArray:
1823 case Type::VariableArray:
1824 Width = 0;
1825 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
1826 break;
1827
1828 case Type::ConstantArray: {
1829 const auto *CAT = cast<ConstantArrayType>(T);
1830
1831 TypeInfo EltInfo = getTypeInfo(CAT->getElementType());
1832 uint64_t Size = CAT->getSize().getZExtValue();
1833 assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) &&(((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) &&
"Overflow in array type bit size evaluation") ? static_cast<
void> (0) : __assert_fail ("(Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && \"Overflow in array type bit size evaluation\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1834, __PRETTY_FUNCTION__))
1834 "Overflow in array type bit size evaluation")(((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) &&
"Overflow in array type bit size evaluation") ? static_cast<
void> (0) : __assert_fail ("(Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && \"Overflow in array type bit size evaluation\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1834, __PRETTY_FUNCTION__))
;
1835 Width = EltInfo.Width * Size;
1836 Align = EltInfo.Align;
1837 if (!getTargetInfo().getCXXABI().isMicrosoft() ||
1838 getTargetInfo().getPointerWidth(0) == 64)
1839 Width = llvm::alignTo(Width, Align);
1840 break;
1841 }
1842 case Type::ExtVector:
1843 case Type::Vector: {
1844 const auto *VT = cast<VectorType>(T);
1845 TypeInfo EltInfo = getTypeInfo(VT->getElementType());
1846 Width = EltInfo.Width * VT->getNumElements();
1847 Align = Width;
1848 // If the alignment is not a power of 2, round up to the next power of 2.
1849 // This happens for non-power-of-2 length vectors.
1850 if (Align & (Align-1)) {
1851 Align = llvm::NextPowerOf2(Align);
1852 Width = llvm::alignTo(Width, Align);
1853 }
1854 // Adjust the alignment based on the target max.
1855 uint64_t TargetVectorAlign = Target->getMaxVectorAlign();
1856 if (TargetVectorAlign && TargetVectorAlign < Align)
1857 Align = TargetVectorAlign;
1858 break;
1859 }
1860
1861 case Type::Builtin:
1862 switch (cast<BuiltinType>(T)->getKind()) {
1863 default: llvm_unreachable("Unknown builtin type!")::llvm::llvm_unreachable_internal("Unknown builtin type!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1863)
;
1864 case BuiltinType::Void:
1865 // GCC extension: alignof(void) = 8 bits.
1866 Width = 0;
1867 Align = 8;
1868 break;
1869 case BuiltinType::Bool:
1870 Width = Target->getBoolWidth();
1871 Align = Target->getBoolAlign();
1872 break;
1873 case BuiltinType::Char_S:
1874 case BuiltinType::Char_U:
1875 case BuiltinType::UChar:
1876 case BuiltinType::SChar:
1877 case BuiltinType::Char8:
1878 Width = Target->getCharWidth();
1879 Align = Target->getCharAlign();
1880 break;
1881 case BuiltinType::WChar_S:
1882 case BuiltinType::WChar_U:
1883 Width = Target->getWCharWidth();
1884 Align = Target->getWCharAlign();
1885 break;
1886 case BuiltinType::Char16:
1887 Width = Target->getChar16Width();
1888 Align = Target->getChar16Align();
1889 break;
1890 case BuiltinType::Char32:
1891 Width = Target->getChar32Width();
1892 Align = Target->getChar32Align();
1893 break;
1894 case BuiltinType::UShort:
1895 case BuiltinType::Short:
1896 Width = Target->getShortWidth();
1897 Align = Target->getShortAlign();
1898 break;
1899 case BuiltinType::UInt:
1900 case BuiltinType::Int:
1901 Width = Target->getIntWidth();
1902 Align = Target->getIntAlign();
1903 break;
1904 case BuiltinType::ULong:
1905 case BuiltinType::Long:
1906 Width = Target->getLongWidth();
1907 Align = Target->getLongAlign();
1908 break;
1909 case BuiltinType::ULongLong:
1910 case BuiltinType::LongLong:
1911 Width = Target->getLongLongWidth();
1912 Align = Target->getLongLongAlign();
1913 break;
1914 case BuiltinType::Int128:
1915 case BuiltinType::UInt128:
1916 Width = 128;
1917 Align = 128; // int128_t is 128-bit aligned on all targets.
1918 break;
1919 case BuiltinType::ShortAccum:
1920 case BuiltinType::UShortAccum:
1921 case BuiltinType::SatShortAccum:
1922 case BuiltinType::SatUShortAccum:
1923 Width = Target->getShortAccumWidth();
1924 Align = Target->getShortAccumAlign();
1925 break;
1926 case BuiltinType::Accum:
1927 case BuiltinType::UAccum:
1928 case BuiltinType::SatAccum:
1929 case BuiltinType::SatUAccum:
1930 Width = Target->getAccumWidth();
1931 Align = Target->getAccumAlign();
1932 break;
1933 case BuiltinType::LongAccum:
1934 case BuiltinType::ULongAccum:
1935 case BuiltinType::SatLongAccum:
1936 case BuiltinType::SatULongAccum:
1937 Width = Target->getLongAccumWidth();
1938 Align = Target->getLongAccumAlign();
1939 break;
1940 case BuiltinType::ShortFract:
1941 case BuiltinType::UShortFract:
1942 case BuiltinType::SatShortFract:
1943 case BuiltinType::SatUShortFract:
1944 Width = Target->getShortFractWidth();
1945 Align = Target->getShortFractAlign();
1946 break;
1947 case BuiltinType::Fract:
1948 case BuiltinType::UFract:
1949 case BuiltinType::SatFract:
1950 case BuiltinType::SatUFract:
1951 Width = Target->getFractWidth();
1952 Align = Target->getFractAlign();
1953 break;
1954 case BuiltinType::LongFract:
1955 case BuiltinType::ULongFract:
1956 case BuiltinType::SatLongFract:
1957 case BuiltinType::SatULongFract:
1958 Width = Target->getLongFractWidth();
1959 Align = Target->getLongFractAlign();
1960 break;
1961 case BuiltinType::Float16:
1962 case BuiltinType::Half:
1963 if (Target->hasFloat16Type() || !getLangOpts().OpenMP ||
1964 !getLangOpts().OpenMPIsDevice) {
1965 Width = Target->getHalfWidth();
1966 Align = Target->getHalfAlign();
1967 } else {
1968 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&((getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice
&& "Expected OpenMP device compilation.") ? static_cast
<void> (0) : __assert_fail ("getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && \"Expected OpenMP device compilation.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1969, __PRETTY_FUNCTION__))
1969 "Expected OpenMP device compilation.")((getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice
&& "Expected OpenMP device compilation.") ? static_cast
<void> (0) : __assert_fail ("getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && \"Expected OpenMP device compilation.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 1969, __PRETTY_FUNCTION__))
;
1970 Width = AuxTarget->getHalfWidth();
1971 Align = AuxTarget->getHalfAlign();
1972 }
1973 break;
1974 case BuiltinType::Float:
1975 Width = Target->getFloatWidth();
1976 Align = Target->getFloatAlign();
1977 break;
1978 case BuiltinType::Double:
1979 Width = Target->getDoubleWidth();
1980 Align = Target->getDoubleAlign();
1981 break;
1982 case BuiltinType::LongDouble:
1983 if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
1984 (Target->getLongDoubleWidth() != AuxTarget->getLongDoubleWidth() ||
1985 Target->getLongDoubleAlign() != AuxTarget->getLongDoubleAlign())) {
1986 Width = AuxTarget->getLongDoubleWidth();
1987 Align = AuxTarget->getLongDoubleAlign();
1988 } else {
1989 Width = Target->getLongDoubleWidth();
1990 Align = Target->getLongDoubleAlign();
1991 }
1992 break;
1993 case BuiltinType::Float128:
1994 if (Target->hasFloat128Type() || !getLangOpts().OpenMP ||
1995 !getLangOpts().OpenMPIsDevice) {
1996 Width = Target->getFloat128Width();
1997 Align = Target->getFloat128Align();
1998 } else {
1999 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&((getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice
&& "Expected OpenMP device compilation.") ? static_cast
<void> (0) : __assert_fail ("getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && \"Expected OpenMP device compilation.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2000, __PRETTY_FUNCTION__))
2000 "Expected OpenMP device compilation.")((getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice
&& "Expected OpenMP device compilation.") ? static_cast
<void> (0) : __assert_fail ("getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && \"Expected OpenMP device compilation.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2000, __PRETTY_FUNCTION__))
;
2001 Width = AuxTarget->getFloat128Width();
2002 Align = AuxTarget->getFloat128Align();
2003 }
2004 break;
2005 case BuiltinType::NullPtr:
2006 Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
2007 Align = Target->getPointerAlign(0); // == sizeof(void*)
2008 break;
2009 case BuiltinType::ObjCId:
2010 case BuiltinType::ObjCClass:
2011 case BuiltinType::ObjCSel:
2012 Width = Target->getPointerWidth(0);
2013 Align = Target->getPointerAlign(0);
2014 break;
2015 case BuiltinType::OCLSampler:
2016 case BuiltinType::OCLEvent:
2017 case BuiltinType::OCLClkEvent:
2018 case BuiltinType::OCLQueue:
2019 case BuiltinType::OCLReserveID:
2020#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
2021 case BuiltinType::Id:
2022#include "clang/Basic/OpenCLImageTypes.def"
2023#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
2024 case BuiltinType::Id:
2025#include "clang/Basic/OpenCLExtensionTypes.def"
2026 AS = getTargetAddressSpace(
2027 Target->getOpenCLTypeAddrSpace(getOpenCLTypeKind(T)));
2028 Width = Target->getPointerWidth(AS);
2029 Align = Target->getPointerAlign(AS);
2030 break;
2031 // The SVE types are effectively target-specific. The length of an
2032 // SVE_VECTOR_TYPE is only known at runtime, but it is always a multiple
2033 // of 128 bits. There is one predicate bit for each vector byte, so the
2034 // length of an SVE_PREDICATE_TYPE is always a multiple of 16 bits.
2035 //
2036 // Because the length is only known at runtime, we use a dummy value
2037 // of 0 for the static length. The alignment values are those defined
2038 // by the Procedure Call Standard for the Arm Architecture.
2039#define SVE_VECTOR_TYPE(Name, Id, SingletonId, ElKind, ElBits, IsSigned, IsFP)\
2040 case BuiltinType::Id: \
2041 Width = 0; \
2042 Align = 128; \
2043 break;
2044#define SVE_PREDICATE_TYPE(Name, Id, SingletonId, ElKind) \
2045 case BuiltinType::Id: \
2046 Width = 0; \
2047 Align = 16; \
2048 break;
2049#include "clang/Basic/AArch64SVEACLETypes.def"
2050 }
2051 break;
2052 case Type::ObjCObjectPointer:
2053 Width = Target->getPointerWidth(0);
2054 Align = Target->getPointerAlign(0);
2055 break;
2056 case Type::BlockPointer:
2057 AS = getTargetAddressSpace(cast<BlockPointerType>(T)->getPointeeType());
2058 Width = Target->getPointerWidth(AS);
2059 Align = Target->getPointerAlign(AS);
2060 break;
2061 case Type::LValueReference:
2062 case Type::RValueReference:
2063 // alignof and sizeof should never enter this code path here, so we go
2064 // the pointer route.
2065 AS = getTargetAddressSpace(cast<ReferenceType>(T)->getPointeeType());
2066 Width = Target->getPointerWidth(AS);
2067 Align = Target->getPointerAlign(AS);
2068 break;
2069 case Type::Pointer:
2070 AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
2071 Width = Target->getPointerWidth(AS);
2072 Align = Target->getPointerAlign(AS);
2073 break;
2074 case Type::MemberPointer: {
2075 const auto *MPT = cast<MemberPointerType>(T);
2076 CXXABI::MemberPointerInfo MPI = ABI->getMemberPointerInfo(MPT);
2077 Width = MPI.Width;
2078 Align = MPI.Align;
2079 break;
2080 }
2081 case Type::Complex: {
2082 // Complex types have the same alignment as their elements, but twice the
2083 // size.
2084 TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType());
2085 Width = EltInfo.Width * 2;
2086 Align = EltInfo.Align;
2087 break;
2088 }
2089 case Type::ObjCObject:
2090 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
2091 case Type::Adjusted:
2092 case Type::Decayed:
2093 return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr());
2094 case Type::ObjCInterface: {
2095 const auto *ObjCI = cast<ObjCInterfaceType>(T);
2096 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
2097 Width = toBits(Layout.getSize());
2098 Align = toBits(Layout.getAlignment());
2099 break;
2100 }
2101 case Type::Record:
2102 case Type::Enum: {
2103 const auto *TT = cast<TagType>(T);
2104
2105 if (TT->getDecl()->isInvalidDecl()) {
2106 Width = 8;
2107 Align = 8;
2108 break;
2109 }
2110
2111 if (const auto *ET = dyn_cast<EnumType>(TT)) {
2112 const EnumDecl *ED = ET->getDecl();
2113 TypeInfo Info =
2114 getTypeInfo(ED->getIntegerType()->getUnqualifiedDesugaredType());
2115 if (unsigned AttrAlign = ED->getMaxAlignment()) {
2116 Info.Align = AttrAlign;
2117 Info.AlignIsRequired = true;
2118 }
2119 return Info;
2120 }
2121
2122 const auto *RT = cast<RecordType>(TT);
2123 const RecordDecl *RD = RT->getDecl();
2124 const ASTRecordLayout &Layout = getASTRecordLayout(RD);
2125 Width = toBits(Layout.getSize());
2126 Align = toBits(Layout.getAlignment());
2127 AlignIsRequired = RD->hasAttr<AlignedAttr>();
2128 break;
2129 }
2130
2131 case Type::SubstTemplateTypeParm:
2132 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
2133 getReplacementType().getTypePtr());
2134
2135 case Type::Auto:
2136 case Type::DeducedTemplateSpecialization: {
2137 const auto *A = cast<DeducedType>(T);
2138 assert(!A->getDeducedType().isNull() &&((!A->getDeducedType().isNull() && "cannot request the size of an undeduced or dependent auto type"
) ? static_cast<void> (0) : __assert_fail ("!A->getDeducedType().isNull() && \"cannot request the size of an undeduced or dependent auto type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2139, __PRETTY_FUNCTION__))
2139 "cannot request the size of an undeduced or dependent auto type")((!A->getDeducedType().isNull() && "cannot request the size of an undeduced or dependent auto type"
) ? static_cast<void> (0) : __assert_fail ("!A->getDeducedType().isNull() && \"cannot request the size of an undeduced or dependent auto type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2139, __PRETTY_FUNCTION__))
;
2140 return getTypeInfo(A->getDeducedType().getTypePtr());
2141 }
2142
2143 case Type::Paren:
2144 return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
2145
2146 case Type::MacroQualified:
2147 return getTypeInfo(
2148 cast<MacroQualifiedType>(T)->getUnderlyingType().getTypePtr());
2149
2150 case Type::ObjCTypeParam:
2151 return getTypeInfo(cast<ObjCTypeParamType>(T)->desugar().getTypePtr());
2152
2153 case Type::Typedef: {
2154 const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
2155 TypeInfo Info = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
2156 // If the typedef has an aligned attribute on it, it overrides any computed
2157 // alignment we have. This violates the GCC documentation (which says that
2158 // attribute(aligned) can only round up) but matches its implementation.
2159 if (unsigned AttrAlign = Typedef->getMaxAlignment()) {
2160 Align = AttrAlign;
2161 AlignIsRequired = true;
2162 } else {
2163 Align = Info.Align;
2164 AlignIsRequired = Info.AlignIsRequired;
2165 }
2166 Width = Info.Width;
2167 break;
2168 }
2169
2170 case Type::Elaborated:
2171 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
2172
2173 case Type::Attributed:
2174 return getTypeInfo(
2175 cast<AttributedType>(T)->getEquivalentType().getTypePtr());
2176
2177 case Type::Atomic: {
2178 // Start with the base type information.
2179 TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType());
2180 Width = Info.Width;
2181 Align = Info.Align;
2182
2183 if (!Width) {
2184 // An otherwise zero-sized type should still generate an
2185 // atomic operation.
2186 Width = Target->getCharWidth();
2187 assert(Align)((Align) ? static_cast<void> (0) : __assert_fail ("Align"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2187, __PRETTY_FUNCTION__))
;
2188 } else if (Width <= Target->getMaxAtomicPromoteWidth()) {
2189 // If the size of the type doesn't exceed the platform's max
2190 // atomic promotion width, make the size and alignment more
2191 // favorable to atomic operations:
2192
2193 // Round the size up to a power of 2.
2194 if (!llvm::isPowerOf2_64(Width))
2195 Width = llvm::NextPowerOf2(Width);
2196
2197 // Set the alignment equal to the size.
2198 Align = static_cast<unsigned>(Width);
2199 }
2200 }
2201 break;
2202
2203 case Type::Pipe:
2204 Width = Target->getPointerWidth(getTargetAddressSpace(LangAS::opencl_global));
2205 Align = Target->getPointerAlign(getTargetAddressSpace(LangAS::opencl_global));
2206 break;
2207 }
2208
2209 assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2")((llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"
) ? static_cast<void> (0) : __assert_fail ("llvm::isPowerOf2_32(Align) && \"Alignment must be power of 2\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2209, __PRETTY_FUNCTION__))
;
2210 return TypeInfo(Width, Align, AlignIsRequired);
2211}
2212
2213unsigned ASTContext::getTypeUnadjustedAlign(const Type *T) const {
2214 UnadjustedAlignMap::iterator I = MemoizedUnadjustedAlign.find(T);
2215 if (I != MemoizedUnadjustedAlign.end())
2216 return I->second;
2217
2218 unsigned UnadjustedAlign;
2219 if (const auto *RT = T->getAs<RecordType>()) {
2220 const RecordDecl *RD = RT->getDecl();
2221 const ASTRecordLayout &Layout = getASTRecordLayout(RD);
2222 UnadjustedAlign = toBits(Layout.getUnadjustedAlignment());
2223 } else if (const auto *ObjCI = T->getAs<ObjCInterfaceType>()) {
2224 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
2225 UnadjustedAlign = toBits(Layout.getUnadjustedAlignment());
2226 } else {
2227 UnadjustedAlign = getTypeAlign(T->getUnqualifiedDesugaredType());
2228 }
2229
2230 MemoizedUnadjustedAlign[T] = UnadjustedAlign;
2231 return UnadjustedAlign;
2232}
2233
2234unsigned ASTContext::getOpenMPDefaultSimdAlign(QualType T) const {
2235 unsigned SimdAlign = getTargetInfo().getSimdDefaultAlign();
2236 // Target ppc64 with QPX: simd default alignment for pointer to double is 32.
2237 if ((getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64 ||
2238 getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64le) &&
2239 getTargetInfo().getABI() == "elfv1-qpx" &&
2240 T->isSpecificBuiltinType(BuiltinType::Double))
2241 SimdAlign = 256;
2242 return SimdAlign;
2243}
2244
2245/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
2246CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
2247 return CharUnits::fromQuantity(BitSize / getCharWidth());
2248}
2249
2250/// toBits - Convert a size in characters to a size in characters.
2251int64_t ASTContext::toBits(CharUnits CharSize) const {
2252 return CharSize.getQuantity() * getCharWidth();
2253}
2254
2255/// getTypeSizeInChars - Return the size of the specified type, in characters.
2256/// This method does not work on incomplete types.
2257CharUnits ASTContext::getTypeSizeInChars(QualType T) const {
2258 return getTypeInfoInChars(T).first;
2259}
2260CharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
2261 return getTypeInfoInChars(T).first;
2262}
2263
2264/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
2265/// characters. This method does not work on incomplete types.
2266CharUnits ASTContext::getTypeAlignInChars(QualType T) const {
2267 return toCharUnitsFromBits(getTypeAlign(T));
2268}
2269CharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
2270 return toCharUnitsFromBits(getTypeAlign(T));
2271}
2272
2273/// getTypeUnadjustedAlignInChars - Return the ABI-specified alignment of a
2274/// type, in characters, before alignment adustments. This method does
2275/// not work on incomplete types.
2276CharUnits ASTContext::getTypeUnadjustedAlignInChars(QualType T) const {
2277 return toCharUnitsFromBits(getTypeUnadjustedAlign(T));
2278}
2279CharUnits ASTContext::getTypeUnadjustedAlignInChars(const Type *T) const {
2280 return toCharUnitsFromBits(getTypeUnadjustedAlign(T));
2281}
2282
2283/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
2284/// type for the current target in bits. This can be different than the ABI
2285/// alignment in cases where it is beneficial for performance to overalign
2286/// a data type.
2287unsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
2288 TypeInfo TI = getTypeInfo(T);
2289 unsigned ABIAlign = TI.Align;
2290
2291 T = T->getBaseElementTypeUnsafe();
2292
2293 // The preferred alignment of member pointers is that of a pointer.
2294 if (T->isMemberPointerType())
2295 return getPreferredTypeAlign(getPointerDiffType().getTypePtr());
2296
2297 if (!Target->allowsLargerPreferedTypeAlignment())
2298 return ABIAlign;
2299
2300 // Double and long long should be naturally aligned if possible.
2301 if (const auto *CT = T->getAs<ComplexType>())
2302 T = CT->getElementType().getTypePtr();
2303 if (const auto *ET = T->getAs<EnumType>())
2304 T = ET->getDecl()->getIntegerType().getTypePtr();
2305 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
2306 T->isSpecificBuiltinType(BuiltinType::LongLong) ||
2307 T->isSpecificBuiltinType(BuiltinType::ULongLong))
2308 // Don't increase the alignment if an alignment attribute was specified on a
2309 // typedef declaration.
2310 if (!TI.AlignIsRequired)
2311 return std::max(ABIAlign, (unsigned)getTypeSize(T));
2312
2313 return ABIAlign;
2314}
2315
2316/// getTargetDefaultAlignForAttributeAligned - Return the default alignment
2317/// for __attribute__((aligned)) on this target, to be used if no alignment
2318/// value is specified.
2319unsigned ASTContext::getTargetDefaultAlignForAttributeAligned() const {
2320 return getTargetInfo().getDefaultAlignForAttributeAligned();
2321}
2322
2323/// getAlignOfGlobalVar - Return the alignment in bits that should be given
2324/// to a global variable of the specified type.
2325unsigned ASTContext::getAlignOfGlobalVar(QualType T) const {
2326 uint64_t TypeSize = getTypeSize(T.getTypePtr());
2327 return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign(TypeSize));
2328}
2329
2330/// getAlignOfGlobalVarInChars - Return the alignment in characters that
2331/// should be given to a global variable of the specified type.
2332CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const {
2333 return toCharUnitsFromBits(getAlignOfGlobalVar(T));
2334}
2335
2336CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const {
2337 CharUnits Offset = CharUnits::Zero();
2338 const ASTRecordLayout *Layout = &getASTRecordLayout(RD);
2339 while (const CXXRecordDecl *Base = Layout->getBaseSharingVBPtr()) {
2340 Offset += Layout->getBaseClassOffset(Base);
2341 Layout = &getASTRecordLayout(Base);
2342 }
2343 return Offset;
2344}
2345
2346/// DeepCollectObjCIvars -
2347/// This routine first collects all declared, but not synthesized, ivars in
2348/// super class and then collects all ivars, including those synthesized for
2349/// current class. This routine is used for implementation of current class
2350/// when all ivars, declared and synthesized are known.
2351void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
2352 bool leafClass,
2353 SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const {
2354 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
2355 DeepCollectObjCIvars(SuperClass, false, Ivars);
2356 if (!leafClass) {
2357 for (const auto *I : OI->ivars())
2358 Ivars.push_back(I);
2359 } else {
2360 auto *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
2361 for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
2362 Iv= Iv->getNextIvar())
2363 Ivars.push_back(Iv);
2364 }
2365}
2366
2367/// CollectInheritedProtocols - Collect all protocols in current class and
2368/// those inherited by it.
2369void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
2370 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
2371 if (const auto *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
2372 // We can use protocol_iterator here instead of
2373 // all_referenced_protocol_iterator since we are walking all categories.
2374 for (auto *Proto : OI->all_referenced_protocols()) {
2375 CollectInheritedProtocols(Proto, Protocols);
2376 }
2377
2378 // Categories of this Interface.
2379 for (const auto *Cat : OI->visible_categories())
2380 CollectInheritedProtocols(Cat, Protocols);
2381
2382 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
2383 while (SD) {
2384 CollectInheritedProtocols(SD, Protocols);
2385 SD = SD->getSuperClass();
2386 }
2387 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
2388 for (auto *Proto : OC->protocols()) {
2389 CollectInheritedProtocols(Proto, Protocols);
2390 }
2391 } else if (const auto *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
2392 // Insert the protocol.
2393 if (!Protocols.insert(
2394 const_cast<ObjCProtocolDecl *>(OP->getCanonicalDecl())).second)
2395 return;
2396
2397 for (auto *Proto : OP->protocols())
2398 CollectInheritedProtocols(Proto, Protocols);
2399 }
2400}
2401
2402static bool unionHasUniqueObjectRepresentations(const ASTContext &Context,
2403 const RecordDecl *RD) {
2404 assert(RD->isUnion() && "Must be union type")((RD->isUnion() && "Must be union type") ? static_cast
<void> (0) : __assert_fail ("RD->isUnion() && \"Must be union type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2404, __PRETTY_FUNCTION__))
;
2405 CharUnits UnionSize = Context.getTypeSizeInChars(RD->getTypeForDecl());
2406
2407 for (const auto *Field : RD->fields()) {
2408 if (!Context.hasUniqueObjectRepresentations(Field->getType()))
2409 return false;
2410 CharUnits FieldSize = Context.getTypeSizeInChars(Field->getType());
2411 if (FieldSize != UnionSize)
2412 return false;
2413 }
2414 return !RD->field_empty();
2415}
2416
2417static bool isStructEmpty(QualType Ty) {
2418 const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl();
2419
2420 if (!RD->field_empty())
2421 return false;
2422
2423 if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RD))
2424 return ClassDecl->isEmpty();
2425
2426 return true;
2427}
2428
2429static llvm::Optional<int64_t>
2430structHasUniqueObjectRepresentations(const ASTContext &Context,
2431 const RecordDecl *RD) {
2432 assert(!RD->isUnion() && "Must be struct/class type")((!RD->isUnion() && "Must be struct/class type") ?
static_cast<void> (0) : __assert_fail ("!RD->isUnion() && \"Must be struct/class type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2432, __PRETTY_FUNCTION__))
;
2433 const auto &Layout = Context.getASTRecordLayout(RD);
2434
2435 int64_t CurOffsetInBits = 0;
2436 if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RD)) {
2437 if (ClassDecl->isDynamicClass())
2438 return llvm::None;
2439
2440 SmallVector<std::pair<QualType, int64_t>, 4> Bases;
2441 for (const auto Base : ClassDecl->bases()) {
2442 // Empty types can be inherited from, and non-empty types can potentially
2443 // have tail padding, so just make sure there isn't an error.
2444 if (!isStructEmpty(Base.getType())) {
2445 llvm::Optional<int64_t> Size = structHasUniqueObjectRepresentations(
2446 Context, Base.getType()->castAs<RecordType>()->getDecl());
2447 if (!Size)
2448 return llvm::None;
2449 Bases.emplace_back(Base.getType(), Size.getValue());
2450 }
2451 }
2452
2453 llvm::sort(Bases, [&](const std::pair<QualType, int64_t> &L,
2454 const std::pair<QualType, int64_t> &R) {
2455 return Layout.getBaseClassOffset(L.first->getAsCXXRecordDecl()) <
2456 Layout.getBaseClassOffset(R.first->getAsCXXRecordDecl());
2457 });
2458
2459 for (const auto Base : Bases) {
2460 int64_t BaseOffset = Context.toBits(
2461 Layout.getBaseClassOffset(Base.first->getAsCXXRecordDecl()));
2462 int64_t BaseSize = Base.second;
2463 if (BaseOffset != CurOffsetInBits)
2464 return llvm::None;
2465 CurOffsetInBits = BaseOffset + BaseSize;
2466 }
2467 }
2468
2469 for (const auto *Field : RD->fields()) {
2470 if (!Field->getType()->isReferenceType() &&
2471 !Context.hasUniqueObjectRepresentations(Field->getType()))
2472 return llvm::None;
2473
2474 int64_t FieldSizeInBits =
2475 Context.toBits(Context.getTypeSizeInChars(Field->getType()));
2476 if (Field->isBitField()) {
2477 int64_t BitfieldSize = Field->getBitWidthValue(Context);
2478
2479 if (BitfieldSize > FieldSizeInBits)
2480 return llvm::None;
2481 FieldSizeInBits = BitfieldSize;
2482 }
2483
2484 int64_t FieldOffsetInBits = Context.getFieldOffset(Field);
2485
2486 if (FieldOffsetInBits != CurOffsetInBits)
2487 return llvm::None;
2488
2489 CurOffsetInBits = FieldSizeInBits + FieldOffsetInBits;
2490 }
2491
2492 return CurOffsetInBits;
2493}
2494
2495bool ASTContext::hasUniqueObjectRepresentations(QualType Ty) const {
2496 // C++17 [meta.unary.prop]:
2497 // The predicate condition for a template specialization
2498 // has_unique_object_representations<T> shall be
2499 // satisfied if and only if:
2500 // (9.1) - T is trivially copyable, and
2501 // (9.2) - any two objects of type T with the same value have the same
2502 // object representation, where two objects
2503 // of array or non-union class type are considered to have the same value
2504 // if their respective sequences of
2505 // direct subobjects have the same values, and two objects of union type
2506 // are considered to have the same
2507 // value if they have the same active member and the corresponding members
2508 // have the same value.
2509 // The set of scalar types for which this condition holds is
2510 // implementation-defined. [ Note: If a type has padding
2511 // bits, the condition does not hold; otherwise, the condition holds true
2512 // for unsigned integral types. -- end note ]
2513 assert(!Ty.isNull() && "Null QualType sent to unique object rep check")((!Ty.isNull() && "Null QualType sent to unique object rep check"
) ? static_cast<void> (0) : __assert_fail ("!Ty.isNull() && \"Null QualType sent to unique object rep check\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2513, __PRETTY_FUNCTION__))
;
2514
2515 // Arrays are unique only if their element type is unique.
2516 if (Ty->isArrayType())
2517 return hasUniqueObjectRepresentations(getBaseElementType(Ty));
2518
2519 // (9.1) - T is trivially copyable...
2520 if (!Ty.isTriviallyCopyableType(*this))
2521 return false;
2522
2523 // All integrals and enums are unique.
2524 if (Ty->isIntegralOrEnumerationType())
2525 return true;
2526
2527 // All other pointers are unique.
2528 if (Ty->isPointerType())
2529 return true;
2530
2531 if (Ty->isMemberPointerType()) {
2532 const auto *MPT = Ty->getAs<MemberPointerType>();
2533 return !ABI->getMemberPointerInfo(MPT).HasPadding;
2534 }
2535
2536 if (Ty->isRecordType()) {
2537 const RecordDecl *Record = Ty->castAs<RecordType>()->getDecl();
2538
2539 if (Record->isInvalidDecl())
2540 return false;
2541
2542 if (Record->isUnion())
2543 return unionHasUniqueObjectRepresentations(*this, Record);
2544
2545 Optional<int64_t> StructSize =
2546 structHasUniqueObjectRepresentations(*this, Record);
2547
2548 return StructSize &&
2549 StructSize.getValue() == static_cast<int64_t>(getTypeSize(Ty));
2550 }
2551
2552 // FIXME: More cases to handle here (list by rsmith):
2553 // vectors (careful about, eg, vector of 3 foo)
2554 // _Complex int and friends
2555 // _Atomic T
2556 // Obj-C block pointers
2557 // Obj-C object pointers
2558 // and perhaps OpenCL's various builtin types (pipe, sampler_t, event_t,
2559 // clk_event_t, queue_t, reserve_id_t)
2560 // There're also Obj-C class types and the Obj-C selector type, but I think it
2561 // makes sense for those to return false here.
2562
2563 return false;
2564}
2565
2566unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
2567 unsigned count = 0;
2568 // Count ivars declared in class extension.
2569 for (const auto *Ext : OI->known_extensions())
2570 count += Ext->ivar_size();
2571
2572 // Count ivar defined in this class's implementation. This
2573 // includes synthesized ivars.
2574 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
2575 count += ImplDecl->ivar_size();
2576
2577 return count;
2578}
2579
2580bool ASTContext::isSentinelNullExpr(const Expr *E) {
2581 if (!E)
2582 return false;
2583
2584 // nullptr_t is always treated as null.
2585 if (E->getType()->isNullPtrType()) return true;
2586
2587 if (E->getType()->isAnyPointerType() &&
2588 E->IgnoreParenCasts()->isNullPointerConstant(*this,
2589 Expr::NPC_ValueDependentIsNull))
2590 return true;
2591
2592 // Unfortunately, __null has type 'int'.
2593 if (isa<GNUNullExpr>(E)) return true;
2594
2595 return false;
2596}
2597
2598/// Get the implementation of ObjCInterfaceDecl, or nullptr if none
2599/// exists.
2600ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
2601 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
2602 I = ObjCImpls.find(D);
2603 if (I != ObjCImpls.end())
2604 return cast<ObjCImplementationDecl>(I->second);
2605 return nullptr;
2606}
2607
2608/// Get the implementation of ObjCCategoryDecl, or nullptr if none
2609/// exists.
2610ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
2611 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
2612 I = ObjCImpls.find(D);
2613 if (I != ObjCImpls.end())
2614 return cast<ObjCCategoryImplDecl>(I->second);
2615 return nullptr;
2616}
2617
2618/// Set the implementation of ObjCInterfaceDecl.
2619void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
2620 ObjCImplementationDecl *ImplD) {
2621 assert(IFaceD && ImplD && "Passed null params")((IFaceD && ImplD && "Passed null params") ? static_cast
<void> (0) : __assert_fail ("IFaceD && ImplD && \"Passed null params\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2621, __PRETTY_FUNCTION__))
;
2622 ObjCImpls[IFaceD] = ImplD;
2623}
2624
2625/// Set the implementation of ObjCCategoryDecl.
2626void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
2627 ObjCCategoryImplDecl *ImplD) {
2628 assert(CatD && ImplD && "Passed null params")((CatD && ImplD && "Passed null params") ? static_cast
<void> (0) : __assert_fail ("CatD && ImplD && \"Passed null params\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2628, __PRETTY_FUNCTION__))
;
2629 ObjCImpls[CatD] = ImplD;
2630}
2631
2632const ObjCMethodDecl *
2633ASTContext::getObjCMethodRedeclaration(const ObjCMethodDecl *MD) const {
2634 return ObjCMethodRedecls.lookup(MD);
2635}
2636
2637void ASTContext::setObjCMethodRedeclaration(const ObjCMethodDecl *MD,
2638 const ObjCMethodDecl *Redecl) {
2639 assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration")((!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration"
) ? static_cast<void> (0) : __assert_fail ("!getObjCMethodRedeclaration(MD) && \"MD already has a redeclaration\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2639, __PRETTY_FUNCTION__))
;
2640 ObjCMethodRedecls[MD] = Redecl;
2641}
2642
2643const ObjCInterfaceDecl *ASTContext::getObjContainingInterface(
2644 const NamedDecl *ND) const {
2645 if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext()))
2646 return ID;
2647 if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext()))
2648 return CD->getClassInterface();
2649 if (const auto *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext()))
2650 return IMD->getClassInterface();
2651
2652 return nullptr;
2653}
2654
2655/// Get the copy initialization expression of VarDecl, or nullptr if
2656/// none exists.
2657ASTContext::BlockVarCopyInit
2658ASTContext::getBlockVarCopyInit(const VarDecl*VD) const {
2659 assert(VD && "Passed null params")((VD && "Passed null params") ? static_cast<void>
(0) : __assert_fail ("VD && \"Passed null params\"",
"/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2659, __PRETTY_FUNCTION__))
;
2660 assert(VD->hasAttr<BlocksAttr>() &&((VD->hasAttr<BlocksAttr>() && "getBlockVarCopyInits - not __block var"
) ? static_cast<void> (0) : __assert_fail ("VD->hasAttr<BlocksAttr>() && \"getBlockVarCopyInits - not __block var\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2661, __PRETTY_FUNCTION__))
2661 "getBlockVarCopyInits - not __block var")((VD->hasAttr<BlocksAttr>() && "getBlockVarCopyInits - not __block var"
) ? static_cast<void> (0) : __assert_fail ("VD->hasAttr<BlocksAttr>() && \"getBlockVarCopyInits - not __block var\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2661, __PRETTY_FUNCTION__))
;
2662 auto I = BlockVarCopyInits.find(VD);
2663 if (I != BlockVarCopyInits.end())
2664 return I->second;
2665 return {nullptr, false};
2666}
2667
2668/// Set the copy initialization expression of a block var decl.
2669void ASTContext::setBlockVarCopyInit(const VarDecl*VD, Expr *CopyExpr,
2670 bool CanThrow) {
2671 assert(VD && CopyExpr && "Passed null params")((VD && CopyExpr && "Passed null params") ? static_cast
<void> (0) : __assert_fail ("VD && CopyExpr && \"Passed null params\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2671, __PRETTY_FUNCTION__))
;
2672 assert(VD->hasAttr<BlocksAttr>() &&((VD->hasAttr<BlocksAttr>() && "setBlockVarCopyInits - not __block var"
) ? static_cast<void> (0) : __assert_fail ("VD->hasAttr<BlocksAttr>() && \"setBlockVarCopyInits - not __block var\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2673, __PRETTY_FUNCTION__))
2673 "setBlockVarCopyInits - not __block var")((VD->hasAttr<BlocksAttr>() && "setBlockVarCopyInits - not __block var"
) ? static_cast<void> (0) : __assert_fail ("VD->hasAttr<BlocksAttr>() && \"setBlockVarCopyInits - not __block var\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2673, __PRETTY_FUNCTION__))
;
2674 BlockVarCopyInits[VD].setExprAndFlag(CopyExpr, CanThrow);
2675}
2676
2677TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
2678 unsigned DataSize) const {
2679 if (!DataSize)
2680 DataSize = TypeLoc::getFullDataSizeForType(T);
2681 else
2682 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&((DataSize == TypeLoc::getFullDataSizeForType(T) && "incorrect data size provided to CreateTypeSourceInfo!"
) ? static_cast<void> (0) : __assert_fail ("DataSize == TypeLoc::getFullDataSizeForType(T) && \"incorrect data size provided to CreateTypeSourceInfo!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2683, __PRETTY_FUNCTION__))
2683 "incorrect data size provided to CreateTypeSourceInfo!")((DataSize == TypeLoc::getFullDataSizeForType(T) && "incorrect data size provided to CreateTypeSourceInfo!"
) ? static_cast<void> (0) : __assert_fail ("DataSize == TypeLoc::getFullDataSizeForType(T) && \"incorrect data size provided to CreateTypeSourceInfo!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2683, __PRETTY_FUNCTION__))
;
2684
2685 auto *TInfo =
2686 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
2687 new (TInfo) TypeSourceInfo(T);
2688 return TInfo;
2689}
2690
2691TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
2692 SourceLocation L) const {
2693 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
2694 DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
2695 return DI;
2696}
2697
2698const ASTRecordLayout &
2699ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
2700 return getObjCLayout(D, nullptr);
2701}
2702
2703const ASTRecordLayout &
2704ASTContext::getASTObjCImplementationLayout(
2705 const ObjCImplementationDecl *D) const {
2706 return getObjCLayout(D->getClassInterface(), D);
2707}
2708
2709//===----------------------------------------------------------------------===//
2710// Type creation/memoization methods
2711//===----------------------------------------------------------------------===//
2712
2713QualType
2714ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
2715 unsigned fastQuals = quals.getFastQualifiers();
2716 quals.removeFastQualifiers();
2717
2718 // Check if we've already instantiated this type.
2719 llvm::FoldingSetNodeID ID;
2720 ExtQuals::Profile(ID, baseType, quals);
2721 void *insertPos = nullptr;
2722 if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
2723 assert(eq->getQualifiers() == quals)((eq->getQualifiers() == quals) ? static_cast<void> (
0) : __assert_fail ("eq->getQualifiers() == quals", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2723, __PRETTY_FUNCTION__))
;
2724 return QualType(eq, fastQuals);
2725 }
2726
2727 // If the base type is not canonical, make the appropriate canonical type.
2728 QualType canon;
2729 if (!baseType->isCanonicalUnqualified()) {
2730 SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
2731 canonSplit.Quals.addConsistentQualifiers(quals);
2732 canon = getExtQualType(canonSplit.Ty, canonSplit.Quals);
2733
2734 // Re-find the insert position.
2735 (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
2736 }
2737
2738 auto *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
2739 ExtQualNodes.InsertNode(eq, insertPos);
2740 return QualType(eq, fastQuals);
2741}
2742
2743QualType ASTContext::getAddrSpaceQualType(QualType T,
2744 LangAS AddressSpace) const {
2745 QualType CanT = getCanonicalType(T);
2746 if (CanT.getAddressSpace() == AddressSpace)
2747 return T;
2748
2749 // If we are composing extended qualifiers together, merge together
2750 // into one ExtQuals node.
2751 QualifierCollector Quals;
2752 const Type *TypeNode = Quals.strip(T);
2753
2754 // If this type already has an address space specified, it cannot get
2755 // another one.
2756 assert(!Quals.hasAddressSpace() &&((!Quals.hasAddressSpace() && "Type cannot be in multiple addr spaces!"
) ? static_cast<void> (0) : __assert_fail ("!Quals.hasAddressSpace() && \"Type cannot be in multiple addr spaces!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2757, __PRETTY_FUNCTION__))
2757 "Type cannot be in multiple addr spaces!")((!Quals.hasAddressSpace() && "Type cannot be in multiple addr spaces!"
) ? static_cast<void> (0) : __assert_fail ("!Quals.hasAddressSpace() && \"Type cannot be in multiple addr spaces!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2757, __PRETTY_FUNCTION__))
;
2758 Quals.addAddressSpace(AddressSpace);
2759
2760 return getExtQualType(TypeNode, Quals);
2761}
2762
2763QualType ASTContext::removeAddrSpaceQualType(QualType T) const {
2764 // If we are composing extended qualifiers together, merge together
2765 // into one ExtQuals node.
2766 QualifierCollector Quals;
2767 const Type *TypeNode = Quals.strip(T);
2768
2769 // If the qualifier doesn't have an address space just return it.
2770 if (!Quals.hasAddressSpace())
2771 return T;
2772
2773 Quals.removeAddressSpace();
2774
2775 // Removal of the address space can mean there are no longer any
2776 // non-fast qualifiers, so creating an ExtQualType isn't possible (asserts)
2777 // or required.
2778 if (Quals.hasNonFastQualifiers())
2779 return getExtQualType(TypeNode, Quals);
2780 else
2781 return QualType(TypeNode, Quals.getFastQualifiers());
2782}
2783
2784QualType ASTContext::getObjCGCQualType(QualType T,
2785 Qualifiers::GC GCAttr) const {
2786 QualType CanT = getCanonicalType(T);
2787 if (CanT.getObjCGCAttr() == GCAttr)
2788 return T;
2789
2790 if (const auto *ptr = T->getAs<PointerType>()) {
2791 QualType Pointee = ptr->getPointeeType();
2792 if (Pointee->isAnyPointerType()) {
2793 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
2794 return getPointerType(ResultType);
2795 }
2796 }
2797
2798 // If we are composing extended qualifiers together, merge together
2799 // into one ExtQuals node.
2800 QualifierCollector Quals;
2801 const Type *TypeNode = Quals.strip(T);
2802
2803 // If this type already has an ObjCGC specified, it cannot get
2804 // another one.
2805 assert(!Quals.hasObjCGCAttr() &&((!Quals.hasObjCGCAttr() && "Type cannot have multiple ObjCGCs!"
) ? static_cast<void> (0) : __assert_fail ("!Quals.hasObjCGCAttr() && \"Type cannot have multiple ObjCGCs!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2806, __PRETTY_FUNCTION__))
2806 "Type cannot have multiple ObjCGCs!")((!Quals.hasObjCGCAttr() && "Type cannot have multiple ObjCGCs!"
) ? static_cast<void> (0) : __assert_fail ("!Quals.hasObjCGCAttr() && \"Type cannot have multiple ObjCGCs!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2806, __PRETTY_FUNCTION__))
;
2807 Quals.addObjCGCAttr(GCAttr);
2808
2809 return getExtQualType(TypeNode, Quals);
2810}
2811
2812const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
2813 FunctionType::ExtInfo Info) {
2814 if (T->getExtInfo() == Info)
2815 return T;
2816
2817 QualType Result;
2818 if (const auto *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
2819 Result = getFunctionNoProtoType(FNPT->getReturnType(), Info);
2820 } else {
2821 const auto *FPT = cast<FunctionProtoType>(T);
2822 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
2823 EPI.ExtInfo = Info;
2824 Result = getFunctionType(FPT->getReturnType(), FPT->getParamTypes(), EPI);
2825 }
2826
2827 return cast<FunctionType>(Result.getTypePtr());
2828}
2829
2830void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD,
2831 QualType ResultType) {
2832 FD = FD->getMostRecentDecl();
2833 while (true) {
2834 const auto *FPT = FD->getType()->castAs<FunctionProtoType>();
2835 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
2836 FD->setType(getFunctionType(ResultType, FPT->getParamTypes(), EPI));
2837 if (FunctionDecl *Next = FD->getPreviousDecl())
2838 FD = Next;
2839 else
2840 break;
2841 }
2842 if (ASTMutationListener *L = getASTMutationListener())
2843 L->DeducedReturnType(FD, ResultType);
2844}
2845
2846/// Get a function type and produce the equivalent function type with the
2847/// specified exception specification. Type sugar that can be present on a
2848/// declaration of a function with an exception specification is permitted
2849/// and preserved. Other type sugar (for instance, typedefs) is not.
2850QualType ASTContext::getFunctionTypeWithExceptionSpec(
2851 QualType Orig, const FunctionProtoType::ExceptionSpecInfo &ESI) {
2852 // Might have some parens.
2853 if (const auto *PT = dyn_cast<ParenType>(Orig))
2854 return getParenType(
2855 getFunctionTypeWithExceptionSpec(PT->getInnerType(), ESI));
2856
2857 // Might be wrapped in a macro qualified type.
2858 if (const auto *MQT = dyn_cast<MacroQualifiedType>(Orig))
2859 return getMacroQualifiedType(
2860 getFunctionTypeWithExceptionSpec(MQT->getUnderlyingType(), ESI),
2861 MQT->getMacroIdentifier());
2862
2863 // Might have a calling-convention attribute.
2864 if (const auto *AT = dyn_cast<AttributedType>(Orig))
2865 return getAttributedType(
2866 AT->getAttrKind(),
2867 getFunctionTypeWithExceptionSpec(AT->getModifiedType(), ESI),
2868 getFunctionTypeWithExceptionSpec(AT->getEquivalentType(), ESI));
2869
2870 // Anything else must be a function type. Rebuild it with the new exception
2871 // specification.
2872 const auto *Proto = Orig->getAs<FunctionProtoType>();
2873 return getFunctionType(
2874 Proto->getReturnType(), Proto->getParamTypes(),
2875 Proto->getExtProtoInfo().withExceptionSpec(ESI));
2876}
2877
2878bool ASTContext::hasSameFunctionTypeIgnoringExceptionSpec(QualType T,
2879 QualType U) {
2880 return hasSameType(T, U) ||
2881 (getLangOpts().CPlusPlus17 &&
2882 hasSameType(getFunctionTypeWithExceptionSpec(T, EST_None),
2883 getFunctionTypeWithExceptionSpec(U, EST_None)));
2884}
2885
2886void ASTContext::adjustExceptionSpec(
2887 FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI,
2888 bool AsWritten) {
2889 // Update the type.
2890 QualType Updated =
2891 getFunctionTypeWithExceptionSpec(FD->getType(), ESI);
2892 FD->setType(Updated);
2893
2894 if (!AsWritten)
2895 return;
2896
2897 // Update the type in the type source information too.
2898 if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) {
2899 // If the type and the type-as-written differ, we may need to update
2900 // the type-as-written too.
2901 if (TSInfo->getType() != FD->getType())
2902 Updated = getFunctionTypeWithExceptionSpec(TSInfo->getType(), ESI);
2903
2904 // FIXME: When we get proper type location information for exceptions,
2905 // we'll also have to rebuild the TypeSourceInfo. For now, we just patch
2906 // up the TypeSourceInfo;
2907 assert(TypeLoc::getFullDataSizeForType(Updated) ==((TypeLoc::getFullDataSizeForType(Updated) == TypeLoc::getFullDataSizeForType
(TSInfo->getType()) && "TypeLoc size mismatch from updating exception specification"
) ? static_cast<void> (0) : __assert_fail ("TypeLoc::getFullDataSizeForType(Updated) == TypeLoc::getFullDataSizeForType(TSInfo->getType()) && \"TypeLoc size mismatch from updating exception specification\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2909, __PRETTY_FUNCTION__))
2908 TypeLoc::getFullDataSizeForType(TSInfo->getType()) &&((TypeLoc::getFullDataSizeForType(Updated) == TypeLoc::getFullDataSizeForType
(TSInfo->getType()) && "TypeLoc size mismatch from updating exception specification"
) ? static_cast<void> (0) : __assert_fail ("TypeLoc::getFullDataSizeForType(Updated) == TypeLoc::getFullDataSizeForType(TSInfo->getType()) && \"TypeLoc size mismatch from updating exception specification\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2909, __PRETTY_FUNCTION__))
2909 "TypeLoc size mismatch from updating exception specification")((TypeLoc::getFullDataSizeForType(Updated) == TypeLoc::getFullDataSizeForType
(TSInfo->getType()) && "TypeLoc size mismatch from updating exception specification"
) ? static_cast<void> (0) : __assert_fail ("TypeLoc::getFullDataSizeForType(Updated) == TypeLoc::getFullDataSizeForType(TSInfo->getType()) && \"TypeLoc size mismatch from updating exception specification\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2909, __PRETTY_FUNCTION__))
;
2910 TSInfo->overrideType(Updated);
2911 }
2912}
2913
2914/// getComplexType - Return the uniqued reference to the type for a complex
2915/// number with the specified element type.
2916QualType ASTContext::getComplexType(QualType T) const {
2917 // Unique pointers, to guarantee there is only one pointer of a particular
2918 // structure.
2919 llvm::FoldingSetNodeID ID;
2920 ComplexType::Profile(ID, T);
2921
2922 void *InsertPos = nullptr;
2923 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
2924 return QualType(CT, 0);
2925
2926 // If the pointee type isn't canonical, this won't be a canonical type either,
2927 // so fill in the canonical type field.
2928 QualType Canonical;
2929 if (!T.isCanonical()) {
2930 Canonical = getComplexType(getCanonicalType(T));
2931
2932 // Get the new insert position for the node we care about.
2933 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
2934 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2934, __PRETTY_FUNCTION__))
; (void)NewIP;
2935 }
2936 auto *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
2937 Types.push_back(New);
2938 ComplexTypes.InsertNode(New, InsertPos);
2939 return QualType(New, 0);
2940}
2941
2942/// getPointerType - Return the uniqued reference to the type for a pointer to
2943/// the specified type.
2944QualType ASTContext::getPointerType(QualType T) const {
2945 // Unique pointers, to guarantee there is only one pointer of a particular
2946 // structure.
2947 llvm::FoldingSetNodeID ID;
2948 PointerType::Profile(ID, T);
2949
2950 void *InsertPos = nullptr;
2951 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2952 return QualType(PT, 0);
2953
2954 // If the pointee type isn't canonical, this won't be a canonical type either,
2955 // so fill in the canonical type field.
2956 QualType Canonical;
2957 if (!T.isCanonical()) {
2958 Canonical = getPointerType(getCanonicalType(T));
2959
2960 // Get the new insert position for the node we care about.
2961 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2962 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2962, __PRETTY_FUNCTION__))
; (void)NewIP;
2963 }
2964 auto *New = new (*this, TypeAlignment) PointerType(T, Canonical);
2965 Types.push_back(New);
2966 PointerTypes.InsertNode(New, InsertPos);
2967 return QualType(New, 0);
2968}
2969
2970QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const {
2971 llvm::FoldingSetNodeID ID;
2972 AdjustedType::Profile(ID, Orig, New);
2973 void *InsertPos = nullptr;
2974 AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos);
2975 if (AT)
2976 return QualType(AT, 0);
2977
2978 QualType Canonical = getCanonicalType(New);
2979
2980 // Get the new insert position for the node we care about.
2981 AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos);
2982 assert(!AT && "Shouldn't be in the map!")((!AT && "Shouldn't be in the map!") ? static_cast<
void> (0) : __assert_fail ("!AT && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2982, __PRETTY_FUNCTION__))
;
2983
2984 AT = new (*this, TypeAlignment)
2985 AdjustedType(Type::Adjusted, Orig, New, Canonical);
2986 Types.push_back(AT);
2987 AdjustedTypes.InsertNode(AT, InsertPos);
2988 return QualType(AT, 0);
2989}
2990
2991QualType ASTContext::getDecayedType(QualType T) const {
2992 assert((T->isArrayType() || T->isFunctionType()) && "T does not decay")(((T->isArrayType() || T->isFunctionType()) && "T does not decay"
) ? static_cast<void> (0) : __assert_fail ("(T->isArrayType() || T->isFunctionType()) && \"T does not decay\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 2992, __PRETTY_FUNCTION__))
;
2993
2994 QualType Decayed;
2995
2996 // C99 6.7.5.3p7:
2997 // A declaration of a parameter as "array of type" shall be
2998 // adjusted to "qualified pointer to type", where the type
2999 // qualifiers (if any) are those specified within the [ and ] of
3000 // the array type derivation.
3001 if (T->isArrayType())
3002 Decayed = getArrayDecayedType(T);
3003
3004 // C99 6.7.5.3p8:
3005 // A declaration of a parameter as "function returning type"
3006 // shall be adjusted to "pointer to function returning type", as
3007 // in 6.3.2.1.
3008 if (T->isFunctionType())
3009 Decayed = getPointerType(T);
3010
3011 llvm::FoldingSetNodeID ID;
3012 AdjustedType::Profile(ID, T, Decayed);
3013 void *InsertPos = nullptr;
3014 AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos);
3015 if (AT)
3016 return QualType(AT, 0);
3017
3018 QualType Canonical = getCanonicalType(Decayed);
3019
3020 // Get the new insert position for the node we care about.
3021 AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos);
3022 assert(!AT && "Shouldn't be in the map!")((!AT && "Shouldn't be in the map!") ? static_cast<
void> (0) : __assert_fail ("!AT && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3022, __PRETTY_FUNCTION__))
;
3023
3024 AT = new (*this, TypeAlignment) DecayedType(T, Decayed, Canonical);
3025 Types.push_back(AT);
3026 AdjustedTypes.InsertNode(AT, InsertPos);
3027 return QualType(AT, 0);
3028}
3029
3030/// getBlockPointerType - Return the uniqued reference to the type for
3031/// a pointer to the specified block.
3032QualType ASTContext::getBlockPointerType(QualType T) const {
3033 assert(T->isFunctionType() && "block of function types only")((T->isFunctionType() && "block of function types only"
) ? static_cast<void> (0) : __assert_fail ("T->isFunctionType() && \"block of function types only\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3033, __PRETTY_FUNCTION__))
;
3034 // Unique pointers, to guarantee there is only one block of a particular
3035 // structure.
3036 llvm::FoldingSetNodeID ID;
3037 BlockPointerType::Profile(ID, T);
3038
3039 void *InsertPos = nullptr;
3040 if (BlockPointerType *PT =
3041 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
3042 return QualType(PT, 0);
3043
3044 // If the block pointee type isn't canonical, this won't be a canonical
3045 // type either so fill in the canonical type field.
3046 QualType Canonical;
3047 if (!T.isCanonical()) {
3048 Canonical = getBlockPointerType(getCanonicalType(T));
3049
3050 // Get the new insert position for the node we care about.
3051 BlockPointerType *NewIP =
3052 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
3053 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3053, __PRETTY_FUNCTION__))
; (void)NewIP;
3054 }
3055 auto *New = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
3056 Types.push_back(New);
3057 BlockPointerTypes.InsertNode(New, InsertPos);
3058 return QualType(New, 0);
3059}
3060
3061/// getLValueReferenceType - Return the uniqued reference to the type for an
3062/// lvalue reference to the specified type.
3063QualType
3064ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
3065 assert(getCanonicalType(T) != OverloadTy &&((getCanonicalType(T) != OverloadTy && "Unresolved overloaded function type"
) ? static_cast<void> (0) : __assert_fail ("getCanonicalType(T) != OverloadTy && \"Unresolved overloaded function type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3066, __PRETTY_FUNCTION__))
3066 "Unresolved overloaded function type")((getCanonicalType(T) != OverloadTy && "Unresolved overloaded function type"
) ? static_cast<void> (0) : __assert_fail ("getCanonicalType(T) != OverloadTy && \"Unresolved overloaded function type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3066, __PRETTY_FUNCTION__))
;
3067
3068 // Unique pointers, to guarantee there is only one pointer of a particular
3069 // structure.
3070 llvm::FoldingSetNodeID ID;
3071 ReferenceType::Profile(ID, T, SpelledAsLValue);
3072
3073 void *InsertPos = nullptr;
3074 if (LValueReferenceType *RT =
3075 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
3076 return QualType(RT, 0);
3077
3078 const auto *InnerRef = T->getAs<ReferenceType>();
3079
3080 // If the referencee type isn't canonical, this won't be a canonical type
3081 // either, so fill in the canonical type field.
3082 QualType Canonical;
3083 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
3084 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
3085 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
3086
3087 // Get the new insert position for the node we care about.
3088 LValueReferenceType *NewIP =
3089 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
3090 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3090, __PRETTY_FUNCTION__))
; (void)NewIP;
3091 }
3092
3093 auto *New = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
3094 SpelledAsLValue);
3095 Types.push_back(New);
3096 LValueReferenceTypes.InsertNode(New, InsertPos);
3097
3098 return QualType(New, 0);
3099}
3100
3101/// getRValueReferenceType - Return the uniqued reference to the type for an
3102/// rvalue reference to the specified type.
3103QualType ASTContext::getRValueReferenceType(QualType T) const {
3104 // Unique pointers, to guarantee there is only one pointer of a particular
3105 // structure.
3106 llvm::FoldingSetNodeID ID;
3107 ReferenceType::Profile(ID, T, false);
3108
3109 void *InsertPos = nullptr;
3110 if (RValueReferenceType *RT =
3111 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
3112 return QualType(RT, 0);
3113
3114 const auto *InnerRef = T->getAs<ReferenceType>();
3115
3116 // If the referencee type isn't canonical, this won't be a canonical type
3117 // either, so fill in the canonical type field.
3118 QualType Canonical;
3119 if (InnerRef || !T.isCanonical()) {
3120 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
3121 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
3122
3123 // Get the new insert position for the node we care about.
3124 RValueReferenceType *NewIP =
3125 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
3126 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3126, __PRETTY_FUNCTION__))
; (void)NewIP;
3127 }
3128
3129 auto *New = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
3130 Types.push_back(New);
3131 RValueReferenceTypes.InsertNode(New, InsertPos);
3132 return QualType(New, 0);
3133}
3134
3135/// getMemberPointerType - Return the uniqued reference to the type for a
3136/// member pointer to the specified type, in the specified class.
3137QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
3138 // Unique pointers, to guarantee there is only one pointer of a particular
3139 // structure.
3140 llvm::FoldingSetNodeID ID;
3141 MemberPointerType::Profile(ID, T, Cls);
3142
3143 void *InsertPos = nullptr;
3144 if (MemberPointerType *PT =
3145 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
3146 return QualType(PT, 0);
3147
3148 // If the pointee or class type isn't canonical, this won't be a canonical
3149 // type either, so fill in the canonical type field.
3150 QualType Canonical;
3151 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
3152 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
3153
3154 // Get the new insert position for the node we care about.
3155 MemberPointerType *NewIP =
3156 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
3157 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3157, __PRETTY_FUNCTION__))
; (void)NewIP;
3158 }
3159 auto *New = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
3160 Types.push_back(New);
3161 MemberPointerTypes.InsertNode(New, InsertPos);
3162 return QualType(New, 0);
3163}
3164
3165/// getConstantArrayType - Return the unique reference to the type for an
3166/// array of the specified element type.
3167QualType ASTContext::getConstantArrayType(QualType EltTy,
3168 const llvm::APInt &ArySizeIn,
3169 ArrayType::ArraySizeModifier ASM,
3170 unsigned IndexTypeQuals) const {
3171 assert((EltTy->isDependentType() ||(((EltTy->isDependentType() || EltTy->isIncompleteType(
) || EltTy->isConstantSizeType()) && "Constant array of VLAs is illegal!"
) ? static_cast<void> (0) : __assert_fail ("(EltTy->isDependentType() || EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && \"Constant array of VLAs is illegal!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3173, __PRETTY_FUNCTION__))
3172 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&(((EltTy->isDependentType() || EltTy->isIncompleteType(
) || EltTy->isConstantSizeType()) && "Constant array of VLAs is illegal!"
) ? static_cast<void> (0) : __assert_fail ("(EltTy->isDependentType() || EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && \"Constant array of VLAs is illegal!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3173, __PRETTY_FUNCTION__))
3173 "Constant array of VLAs is illegal!")(((EltTy->isDependentType() || EltTy->isIncompleteType(
) || EltTy->isConstantSizeType()) && "Constant array of VLAs is illegal!"
) ? static_cast<void> (0) : __assert_fail ("(EltTy->isDependentType() || EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && \"Constant array of VLAs is illegal!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3173, __PRETTY_FUNCTION__))
;
3174
3175 // Convert the array size into a canonical width matching the pointer size for
3176 // the target.
3177 llvm::APInt ArySize(ArySizeIn);
3178 ArySize = ArySize.zextOrTrunc(Target->getMaxPointerWidth());
3179
3180 llvm::FoldingSetNodeID ID;
3181 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
3182
3183 void *InsertPos = nullptr;
3184 if (ConstantArrayType *ATP =
3185 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
3186 return QualType(ATP, 0);
3187
3188 // If the element type isn't canonical or has qualifiers, this won't
3189 // be a canonical type either, so fill in the canonical type field.
3190 QualType Canon;
3191 if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
3192 SplitQualType canonSplit = getCanonicalType(EltTy).split();
3193 Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize,
3194 ASM, IndexTypeQuals);
3195 Canon = getQualifiedType(Canon, canonSplit.Quals);
3196
3197 // Get the new insert position for the node we care about.
3198 ConstantArrayType *NewIP =
3199 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
3200 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3200, __PRETTY_FUNCTION__))
; (void)NewIP;
3201 }
3202
3203 auto *New = new (*this,TypeAlignment)
3204 ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
3205 ConstantArrayTypes.InsertNode(New, InsertPos);
3206 Types.push_back(New);
3207 return QualType(New, 0);
3208}
3209
3210/// getVariableArrayDecayedType - Turns the given type, which may be
3211/// variably-modified, into the corresponding type with all the known
3212/// sizes replaced with [*].
3213QualType ASTContext::getVariableArrayDecayedType(QualType type) const {
3214 // Vastly most common case.
3215 if (!type->isVariablyModifiedType()) return type;
3216
3217 QualType result;
3218
3219 SplitQualType split = type.getSplitDesugaredType();
3220 const Type *ty = split.Ty;
3221 switch (ty->getTypeClass()) {
3222#define TYPE(Class, Base)
3223#define ABSTRACT_TYPE(Class, Base)
3224#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3225#include "clang/AST/TypeNodes.inc"
3226 llvm_unreachable("didn't desugar past all non-canonical types?")::llvm::llvm_unreachable_internal("didn't desugar past all non-canonical types?"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3226)
;
3227
3228 // These types should never be variably-modified.
3229 case Type::Builtin:
3230 case Type::Complex:
3231 case Type::Vector:
3232 case Type::DependentVector:
3233 case Type::ExtVector:
3234 case Type::DependentSizedExtVector:
3235 case Type::DependentAddressSpace:
3236 case Type::ObjCObject:
3237 case Type::ObjCInterface:
3238 case Type::ObjCObjectPointer:
3239 case Type::Record:
3240 case Type::Enum:
3241 case Type::UnresolvedUsing:
3242 case Type::TypeOfExpr:
3243 case Type::TypeOf:
3244 case Type::Decltype:
3245 case Type::UnaryTransform:
3246 case Type::DependentName:
3247 case Type::InjectedClassName:
3248 case Type::TemplateSpecialization:
3249 case Type::DependentTemplateSpecialization:
3250 case Type::TemplateTypeParm:
3251 case Type::SubstTemplateTypeParmPack:
3252 case Type::Auto:
3253 case Type::DeducedTemplateSpecialization:
3254 case Type::PackExpansion:
3255 llvm_unreachable("type should never be variably-modified")::llvm::llvm_unreachable_internal("type should never be variably-modified"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3255)
;
3256
3257 // These types can be variably-modified but should never need to
3258 // further decay.
3259 case Type::FunctionNoProto:
3260 case Type::FunctionProto:
3261 case Type::BlockPointer:
3262 case Type::MemberPointer:
3263 case Type::Pipe:
3264 return type;
3265
3266 // These types can be variably-modified. All these modifications
3267 // preserve structure except as noted by comments.
3268 // TODO: if we ever care about optimizing VLAs, there are no-op
3269 // optimizations available here.
3270 case Type::Pointer:
3271 result = getPointerType(getVariableArrayDecayedType(
3272 cast<PointerType>(ty)->getPointeeType()));
3273 break;
3274
3275 case Type::LValueReference: {
3276 const auto *lv = cast<LValueReferenceType>(ty);
3277 result = getLValueReferenceType(
3278 getVariableArrayDecayedType(lv->getPointeeType()),
3279 lv->isSpelledAsLValue());
3280 break;
3281 }
3282
3283 case Type::RValueReference: {
3284 const auto *lv = cast<RValueReferenceType>(ty);
3285 result = getRValueReferenceType(
3286 getVariableArrayDecayedType(lv->getPointeeType()));
3287 break;
3288 }
3289
3290 case Type::Atomic: {
3291 const auto *at = cast<AtomicType>(ty);
3292 result = getAtomicType(getVariableArrayDecayedType(at->getValueType()));
3293 break;
3294 }
3295
3296 case Type::ConstantArray: {
3297 const auto *cat = cast<ConstantArrayType>(ty);
3298 result = getConstantArrayType(
3299 getVariableArrayDecayedType(cat->getElementType()),
3300 cat->getSize(),
3301 cat->getSizeModifier(),
3302 cat->getIndexTypeCVRQualifiers());
3303 break;
3304 }
3305
3306 case Type::DependentSizedArray: {
3307 const auto *dat = cast<DependentSizedArrayType>(ty);
3308 result = getDependentSizedArrayType(
3309 getVariableArrayDecayedType(dat->getElementType()),
3310 dat->getSizeExpr(),
3311 dat->getSizeModifier(),
3312 dat->getIndexTypeCVRQualifiers(),
3313 dat->getBracketsRange());
3314 break;
3315 }
3316
3317 // Turn incomplete types into [*] types.
3318 case Type::IncompleteArray: {
3319 const auto *iat = cast<IncompleteArrayType>(ty);
3320 result = getVariableArrayType(
3321 getVariableArrayDecayedType(iat->getElementType()),
3322 /*size*/ nullptr,
3323 ArrayType::Normal,
3324 iat->getIndexTypeCVRQualifiers(),
3325 SourceRange());
3326 break;
3327 }
3328
3329 // Turn VLA types into [*] types.
3330 case Type::VariableArray: {
3331 const auto *vat = cast<VariableArrayType>(ty);
3332 result = getVariableArrayType(
3333 getVariableArrayDecayedType(vat->getElementType()),
3334 /*size*/ nullptr,
3335 ArrayType::Star,
3336 vat->getIndexTypeCVRQualifiers(),
3337 vat->getBracketsRange());
3338 break;
3339 }
3340 }
3341
3342 // Apply the top-level qualifiers from the original.
3343 return getQualifiedType(result, split.Quals);
3344}
3345
3346/// getVariableArrayType - Returns a non-unique reference to the type for a
3347/// variable array of the specified element type.
3348QualType ASTContext::getVariableArrayType(QualType EltTy,
3349 Expr *NumElts,
3350 ArrayType::ArraySizeModifier ASM,
3351 unsigned IndexTypeQuals,
3352 SourceRange Brackets) const {
3353 // Since we don't unique expressions, it isn't possible to unique VLA's
3354 // that have an expression provided for their size.
3355 QualType Canon;
3356
3357 // Be sure to pull qualifiers off the element type.
3358 if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
3359 SplitQualType canonSplit = getCanonicalType(EltTy).split();
3360 Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM,
3361 IndexTypeQuals, Brackets);
3362 Canon = getQualifiedType(Canon, canonSplit.Quals);
3363 }
3364
3365 auto *New = new (*this, TypeAlignment)
3366 VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
3367
3368 VariableArrayTypes.push_back(New);
3369 Types.push_back(New);
3370 return QualType(New, 0);
3371}
3372
3373/// getDependentSizedArrayType - Returns a non-unique reference to
3374/// the type for a dependently-sized array of the specified element
3375/// type.
3376QualType ASTContext::getDependentSizedArrayType(QualType elementType,
3377 Expr *numElements,
3378 ArrayType::ArraySizeModifier ASM,
3379 unsigned elementTypeQuals,
3380 SourceRange brackets) const {
3381 assert((!numElements || numElements->isTypeDependent() ||(((!numElements || numElements->isTypeDependent() || numElements
->isValueDependent()) && "Size must be type- or value-dependent!"
) ? static_cast<void> (0) : __assert_fail ("(!numElements || numElements->isTypeDependent() || numElements->isValueDependent()) && \"Size must be type- or value-dependent!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3383, __PRETTY_FUNCTION__))
3382 numElements->isValueDependent()) &&(((!numElements || numElements->isTypeDependent() || numElements
->isValueDependent()) && "Size must be type- or value-dependent!"
) ? static_cast<void> (0) : __assert_fail ("(!numElements || numElements->isTypeDependent() || numElements->isValueDependent()) && \"Size must be type- or value-dependent!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3383, __PRETTY_FUNCTION__))
3383 "Size must be type- or value-dependent!")(((!numElements || numElements->isTypeDependent() || numElements
->isValueDependent()) && "Size must be type- or value-dependent!"
) ? static_cast<void> (0) : __assert_fail ("(!numElements || numElements->isTypeDependent() || numElements->isValueDependent()) && \"Size must be type- or value-dependent!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3383, __PRETTY_FUNCTION__))
;
3384
3385 // Dependently-sized array types that do not have a specified number
3386 // of elements will have their sizes deduced from a dependent
3387 // initializer. We do no canonicalization here at all, which is okay
3388 // because they can't be used in most locations.
3389 if (!numElements) {
3390 auto *newType
3391 = new (*this, TypeAlignment)
3392 DependentSizedArrayType(*this, elementType, QualType(),
3393 numElements, ASM, elementTypeQuals,
3394 brackets);
3395 Types.push_back(newType);
3396 return QualType(newType, 0);
3397 }
3398
3399 // Otherwise, we actually build a new type every time, but we
3400 // also build a canonical type.
3401
3402 SplitQualType canonElementType = getCanonicalType(elementType).split();
3403
3404 void *insertPos = nullptr;
3405 llvm::FoldingSetNodeID ID;
3406 DependentSizedArrayType::Profile(ID, *this,
3407 QualType(canonElementType.Ty, 0),
3408 ASM, elementTypeQuals, numElements);
3409
3410 // Look for an existing type with these properties.
3411 DependentSizedArrayType *canonTy =
3412 DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
3413
3414 // If we don't have one, build one.
3415 if (!canonTy) {
3416 canonTy = new (*this, TypeAlignment)
3417 DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0),
3418 QualType(), numElements, ASM, elementTypeQuals,
3419 brackets);
3420 DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
3421 Types.push_back(canonTy);
3422 }
3423
3424 // Apply qualifiers from the element type to the array.
3425 QualType canon = getQualifiedType(QualType(canonTy,0),
3426 canonElementType.Quals);
3427
3428 // If we didn't need extra canonicalization for the element type or the size
3429 // expression, then just use that as our result.
3430 if (QualType(canonElementType.Ty, 0) == elementType &&
3431 canonTy->getSizeExpr() == numElements)
3432 return canon;
3433
3434 // Otherwise, we need to build a type which follows the spelling
3435 // of the element type.
3436 auto *sugaredType
3437 = new (*this, TypeAlignment)
3438 DependentSizedArrayType(*this, elementType, canon, numElements,
3439 ASM, elementTypeQuals, brackets);
3440 Types.push_back(sugaredType);
3441 return QualType(sugaredType, 0);
3442}
3443
3444QualType ASTContext::getIncompleteArrayType(QualType elementType,
3445 ArrayType::ArraySizeModifier ASM,
3446 unsigned elementTypeQuals) const {
3447 llvm::FoldingSetNodeID ID;
3448 IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
3449
3450 void *insertPos = nullptr;
3451 if (IncompleteArrayType *iat =
3452 IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
3453 return QualType(iat, 0);
3454
3455 // If the element type isn't canonical, this won't be a canonical type
3456 // either, so fill in the canonical type field. We also have to pull
3457 // qualifiers off the element type.
3458 QualType canon;
3459
3460 if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
3461 SplitQualType canonSplit = getCanonicalType(elementType).split();
3462 canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0),
3463 ASM, elementTypeQuals);
3464 canon = getQualifiedType(canon, canonSplit.Quals);
3465
3466 // Get the new insert position for the node we care about.
3467 IncompleteArrayType *existing =
3468 IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
3469 assert(!existing && "Shouldn't be in the map!")((!existing && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!existing && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3469, __PRETTY_FUNCTION__))
; (void) existing;
3470 }
3471
3472 auto *newType = new (*this, TypeAlignment)
3473 IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
3474
3475 IncompleteArrayTypes.InsertNode(newType, insertPos);
3476 Types.push_back(newType);
3477 return QualType(newType, 0);
3478}
3479
3480/// getVectorType - Return the unique reference to a vector type of
3481/// the specified element type and size. VectorType must be a built-in type.
3482QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
3483 VectorType::VectorKind VecKind) const {
3484 assert(vecType->isBuiltinType())((vecType->isBuiltinType()) ? static_cast<void> (0) :
__assert_fail ("vecType->isBuiltinType()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3484, __PRETTY_FUNCTION__))
;
3485
3486 // Check if we've already instantiated a vector of this type.
3487 llvm::FoldingSetNodeID ID;
3488 VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
3489
3490 void *InsertPos = nullptr;
3491 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
3492 return QualType(VTP, 0);
3493
3494 // If the element type isn't canonical, this won't be a canonical type either,
3495 // so fill in the canonical type field.
3496 QualType Canonical;
3497 if (!vecType.isCanonical()) {
3498 Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
3499
3500 // Get the new insert position for the node we care about.
3501 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
3502 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3502, __PRETTY_FUNCTION__))
; (void)NewIP;
3503 }
3504 auto *New = new (*this, TypeAlignment)
3505 VectorType(vecType, NumElts, Canonical, VecKind);
3506 VectorTypes.InsertNode(New, InsertPos);
3507 Types.push_back(New);
3508 return QualType(New, 0);
3509}
3510
3511QualType
3512ASTContext::getDependentVectorType(QualType VecType, Expr *SizeExpr,
3513 SourceLocation AttrLoc,
3514 VectorType::VectorKind VecKind) const {
3515 llvm::FoldingSetNodeID ID;
3516 DependentVectorType::Profile(ID, *this, getCanonicalType(VecType), SizeExpr,
3517 VecKind);
3518 void *InsertPos = nullptr;
3519 DependentVectorType *Canon =
3520 DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
3521 DependentVectorType *New;
3522
3523 if (Canon) {
3524 New = new (*this, TypeAlignment) DependentVectorType(
3525 *this, VecType, QualType(Canon, 0), SizeExpr, AttrLoc, VecKind);
3526 } else {
3527 QualType CanonVecTy = getCanonicalType(VecType);
3528 if (CanonVecTy == VecType) {
3529 New = new (*this, TypeAlignment) DependentVectorType(
3530 *this, VecType, QualType(), SizeExpr, AttrLoc, VecKind);
3531
3532 DependentVectorType *CanonCheck =
3533 DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
3534 assert(!CanonCheck &&((!CanonCheck && "Dependent-sized vector_size canonical type broken"
) ? static_cast<void> (0) : __assert_fail ("!CanonCheck && \"Dependent-sized vector_size canonical type broken\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3535, __PRETTY_FUNCTION__))
3535 "Dependent-sized vector_size canonical type broken")((!CanonCheck && "Dependent-sized vector_size canonical type broken"
) ? static_cast<void> (0) : __assert_fail ("!CanonCheck && \"Dependent-sized vector_size canonical type broken\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3535, __PRETTY_FUNCTION__))
;
3536 (void)CanonCheck;
3537 DependentVectorTypes.InsertNode(New, InsertPos);
3538 } else {
3539 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
3540 SourceLocation());
3541 New = new (*this, TypeAlignment) DependentVectorType(
3542 *this, VecType, Canon, SizeExpr, AttrLoc, VecKind);
3543 }
3544 }
3545
3546 Types.push_back(New);
3547 return QualType(New, 0);
3548}
3549
3550/// getExtVectorType - Return the unique reference to an extended vector type of
3551/// the specified element type and size. VectorType must be a built-in type.
3552QualType
3553ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
3554 assert(vecType->isBuiltinType() || vecType->isDependentType())((vecType->isBuiltinType() || vecType->isDependentType(
)) ? static_cast<void> (0) : __assert_fail ("vecType->isBuiltinType() || vecType->isDependentType()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3554, __PRETTY_FUNCTION__))
;
3555
3556 // Check if we've already instantiated a vector of this type.
3557 llvm::FoldingSetNodeID ID;
3558 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
3559 VectorType::GenericVector);
3560 void *InsertPos = nullptr;
3561 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
3562 return QualType(VTP, 0);
3563
3564 // If the element type isn't canonical, this won't be a canonical type either,
3565 // so fill in the canonical type field.
3566 QualType Canonical;
3567 if (!vecType.isCanonical()) {
3568 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
3569
3570 // Get the new insert position for the node we care about.
3571 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
3572 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3572, __PRETTY_FUNCTION__))
; (void)NewIP;
3573 }
3574 auto *New = new (*this, TypeAlignment)
3575 ExtVectorType(vecType, NumElts, Canonical);
3576 VectorTypes.InsertNode(New, InsertPos);
3577 Types.push_back(New);
3578 return QualType(New, 0);
3579}
3580
3581QualType
3582ASTContext::getDependentSizedExtVectorType(QualType vecType,
3583 Expr *SizeExpr,
3584 SourceLocation AttrLoc) const {
3585 llvm::FoldingSetNodeID ID;
3586 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
3587 SizeExpr);
3588
3589 void *InsertPos = nullptr;
3590 DependentSizedExtVectorType *Canon
3591 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
3592 DependentSizedExtVectorType *New;
3593 if (Canon) {
3594 // We already have a canonical version of this array type; use it as
3595 // the canonical type for a newly-built type.
3596 New = new (*this, TypeAlignment)
3597 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
3598 SizeExpr, AttrLoc);
3599 } else {
3600 QualType CanonVecTy = getCanonicalType(vecType);
3601 if (CanonVecTy == vecType) {
3602 New = new (*this, TypeAlignment)
3603 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
3604 AttrLoc);
3605
3606 DependentSizedExtVectorType *CanonCheck
3607 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
3608 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken")((!CanonCheck && "Dependent-sized ext_vector canonical type broken"
) ? static_cast<void> (0) : __assert_fail ("!CanonCheck && \"Dependent-sized ext_vector canonical type broken\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3608, __PRETTY_FUNCTION__))
;
3609 (void)CanonCheck;
3610 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
3611 } else {
3612 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
3613 SourceLocation());
3614 New = new (*this, TypeAlignment)
3615 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
3616 }
3617 }
3618
3619 Types.push_back(New);
3620 return QualType(New, 0);
3621}
3622
3623QualType ASTContext::getDependentAddressSpaceType(QualType PointeeType,
3624 Expr *AddrSpaceExpr,
3625 SourceLocation AttrLoc) const {
3626 assert(AddrSpaceExpr->isInstantiationDependent())((AddrSpaceExpr->isInstantiationDependent()) ? static_cast
<void> (0) : __assert_fail ("AddrSpaceExpr->isInstantiationDependent()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3626, __PRETTY_FUNCTION__))
;
3627
3628 QualType canonPointeeType = getCanonicalType(PointeeType);
3629
3630 void *insertPos = nullptr;
3631 llvm::FoldingSetNodeID ID;
3632 DependentAddressSpaceType::Profile(ID, *this, canonPointeeType,
3633 AddrSpaceExpr);
3634
3635 DependentAddressSpaceType *canonTy =
3636 DependentAddressSpaceTypes.FindNodeOrInsertPos(ID, insertPos);
3637
3638 if (!canonTy) {
3639 canonTy = new (*this, TypeAlignment)
3640 DependentAddressSpaceType(*this, canonPointeeType,
3641 QualType(), AddrSpaceExpr, AttrLoc);
3642 DependentAddressSpaceTypes.InsertNode(canonTy, insertPos);
3643 Types.push_back(canonTy);
3644 }
3645
3646 if (canonPointeeType == PointeeType &&
3647 canonTy->getAddrSpaceExpr() == AddrSpaceExpr)
3648 return QualType(canonTy, 0);
3649
3650 auto *sugaredType
3651 = new (*this, TypeAlignment)
3652 DependentAddressSpaceType(*this, PointeeType, QualType(canonTy, 0),
3653 AddrSpaceExpr, AttrLoc);
3654 Types.push_back(sugaredType);
3655 return QualType(sugaredType, 0);
3656}
3657
3658/// Determine whether \p T is canonical as the result type of a function.
3659static bool isCanonicalResultType(QualType T) {
3660 return T.isCanonical() &&
3661 (T.getObjCLifetime() == Qualifiers::OCL_None ||
3662 T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone);
3663}
3664
3665/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
3666QualType
3667ASTContext::getFunctionNoProtoType(QualType ResultTy,
3668 const FunctionType::ExtInfo &Info) const {
3669 // Unique functions, to guarantee there is only one function of a particular
3670 // structure.
3671 llvm::FoldingSetNodeID ID;
3672 FunctionNoProtoType::Profile(ID, ResultTy, Info);
3673
3674 void *InsertPos = nullptr;
3675 if (FunctionNoProtoType *FT =
3676 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
3677 return QualType(FT, 0);
3678
3679 QualType Canonical;
3680 if (!isCanonicalResultType(ResultTy)) {
3681 Canonical =
3682 getFunctionNoProtoType(getCanonicalFunctionResultType(ResultTy), Info);
3683
3684 // Get the new insert position for the node we care about.
3685 FunctionNoProtoType *NewIP =
3686 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
3687 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3687, __PRETTY_FUNCTION__))
; (void)NewIP;
3688 }
3689
3690 auto *New = new (*this, TypeAlignment)
3691 FunctionNoProtoType(ResultTy, Canonical, Info);
3692 Types.push_back(New);
3693 FunctionNoProtoTypes.InsertNode(New, InsertPos);
3694 return QualType(New, 0);
3695}
3696
3697CanQualType
3698ASTContext::getCanonicalFunctionResultType(QualType ResultType) const {
3699 CanQualType CanResultType = getCanonicalType(ResultType);
3700
3701 // Canonical result types do not have ARC lifetime qualifiers.
3702 if (CanResultType.getQualifiers().hasObjCLifetime()) {
3703 Qualifiers Qs = CanResultType.getQualifiers();
3704 Qs.removeObjCLifetime();
3705 return CanQualType::CreateUnsafe(
3706 getQualifiedType(CanResultType.getUnqualifiedType(), Qs));
3707 }
3708
3709 return CanResultType;
3710}
3711
3712static bool isCanonicalExceptionSpecification(
3713 const FunctionProtoType::ExceptionSpecInfo &ESI, bool NoexceptInType) {
3714 if (ESI.Type == EST_None)
3715 return true;
3716 if (!NoexceptInType)
3717 return false;
3718
3719 // C++17 onwards: exception specification is part of the type, as a simple
3720 // boolean "can this function type throw".
3721 if (ESI.Type == EST_BasicNoexcept)
3722 return true;
3723
3724 // A noexcept(expr) specification is (possibly) canonical if expr is
3725 // value-dependent.
3726 if (ESI.Type == EST_DependentNoexcept)
3727 return true;
3728
3729 // A dynamic exception specification is canonical if it only contains pack
3730 // expansions (so we can't tell whether it's non-throwing) and all its
3731 // contained types are canonical.
3732 if (ESI.Type == EST_Dynamic) {
3733 bool AnyPackExpansions = false;
3734 for (QualType ET : ESI.Exceptions) {
3735 if (!ET.isCanonical())
3736 return false;
3737 if (ET->getAs<PackExpansionType>())
3738 AnyPackExpansions = true;
3739 }
3740 return AnyPackExpansions;
3741 }
3742
3743 return false;
3744}
3745
3746QualType ASTContext::getFunctionTypeInternal(
3747 QualType ResultTy, ArrayRef<QualType> ArgArray,
3748 const FunctionProtoType::ExtProtoInfo &EPI, bool OnlyWantCanonical) const {
3749 size_t NumArgs = ArgArray.size();
3750
3751 // Unique functions, to guarantee there is only one function of a particular
3752 // structure.
3753 llvm::FoldingSetNodeID ID;
3754 FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI,
3755 *this, true);
3756
3757 QualType Canonical;
3758 bool Unique = false;
3759
3760 void *InsertPos = nullptr;
3761 if (FunctionProtoType *FPT =
3762 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) {
3763 QualType Existing = QualType(FPT, 0);
3764
3765 // If we find a pre-existing equivalent FunctionProtoType, we can just reuse
3766 // it so long as our exception specification doesn't contain a dependent
3767 // noexcept expression, or we're just looking for a canonical type.
3768 // Otherwise, we're going to need to create a type
3769 // sugar node to hold the concrete expression.
3770 if (OnlyWantCanonical || !isComputedNoexcept(EPI.ExceptionSpec.Type) ||
3771 EPI.ExceptionSpec.NoexceptExpr == FPT->getNoexceptExpr())
3772 return Existing;
3773
3774 // We need a new type sugar node for this one, to hold the new noexcept
3775 // expression. We do no canonicalization here, but that's OK since we don't
3776 // expect to see the same noexcept expression much more than once.
3777 Canonical = getCanonicalType(Existing);
3778 Unique = true;
3779 }
3780
3781 bool NoexceptInType = getLangOpts().CPlusPlus17;
3782 bool IsCanonicalExceptionSpec =
3783 isCanonicalExceptionSpecification(EPI.ExceptionSpec, NoexceptInType);
3784
3785 // Determine whether the type being created is already canonical or not.
3786 bool isCanonical = !Unique && IsCanonicalExceptionSpec &&
3787 isCanonicalResultType(ResultTy) && !EPI.HasTrailingReturn;
3788 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
3789 if (!ArgArray[i].isCanonicalAsParam())
3790 isCanonical = false;
3791
3792 if (OnlyWantCanonical)
3793 assert(isCanonical &&((isCanonical && "given non-canonical parameters constructing canonical type"
) ? static_cast<void> (0) : __assert_fail ("isCanonical && \"given non-canonical parameters constructing canonical type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3794, __PRETTY_FUNCTION__))
3794 "given non-canonical parameters constructing canonical type")((isCanonical && "given non-canonical parameters constructing canonical type"
) ? static_cast<void> (0) : __assert_fail ("isCanonical && \"given non-canonical parameters constructing canonical type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3794, __PRETTY_FUNCTION__))
;
3795
3796 // If this type isn't canonical, get the canonical version of it if we don't
3797 // already have it. The exception spec is only partially part of the
3798 // canonical type, and only in C++17 onwards.
3799 if (!isCanonical && Canonical.isNull()) {
3800 SmallVector<QualType, 16> CanonicalArgs;
3801 CanonicalArgs.reserve(NumArgs);
3802 for (unsigned i = 0; i != NumArgs; ++i)
3803 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
3804
3805 llvm::SmallVector<QualType, 8> ExceptionTypeStorage;
3806 FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
3807 CanonicalEPI.HasTrailingReturn = false;
3808
3809 if (IsCanonicalExceptionSpec) {
3810 // Exception spec is already OK.
3811 } else if (NoexceptInType) {
3812 switch (EPI.ExceptionSpec.Type) {
3813 case EST_Unparsed: case EST_Unevaluated: case EST_Uninstantiated:
3814 // We don't know yet. It shouldn't matter what we pick here; no-one
3815 // should ever look at this.
3816 LLVM_FALLTHROUGH[[gnu::fallthrough]];
3817 case EST_None: case EST_MSAny: case EST_NoexceptFalse:
3818 CanonicalEPI.ExceptionSpec.Type = EST_None;
3819 break;
3820
3821 // A dynamic exception specification is almost always "not noexcept",
3822 // with the exception that a pack expansion might expand to no types.
3823 case EST_Dynamic: {
3824 bool AnyPacks = false;
3825 for (QualType ET : EPI.ExceptionSpec.Exceptions) {
3826 if (ET->getAs<PackExpansionType>())
3827 AnyPacks = true;
3828 ExceptionTypeStorage.push_back(getCanonicalType(ET));
3829 }
3830 if (!AnyPacks)
3831 CanonicalEPI.ExceptionSpec.Type = EST_None;
3832 else {
3833 CanonicalEPI.ExceptionSpec.Type = EST_Dynamic;
3834 CanonicalEPI.ExceptionSpec.Exceptions = ExceptionTypeStorage;
3835 }
3836 break;
3837 }
3838
3839 case EST_DynamicNone:
3840 case EST_BasicNoexcept:
3841 case EST_NoexceptTrue:
3842 case EST_NoThrow:
3843 CanonicalEPI.ExceptionSpec.Type = EST_BasicNoexcept;
3844 break;
3845
3846 case EST_DependentNoexcept:
3847 llvm_unreachable("dependent noexcept is already canonical")::llvm::llvm_unreachable_internal("dependent noexcept is already canonical"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3847)
;
3848 }
3849 } else {
3850 CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo();
3851 }
3852
3853 // Adjust the canonical function result type.
3854 CanQualType CanResultTy = getCanonicalFunctionResultType(ResultTy);
3855 Canonical =
3856 getFunctionTypeInternal(CanResultTy, CanonicalArgs, CanonicalEPI, true);
3857
3858 // Get the new insert position for the node we care about.
3859 FunctionProtoType *NewIP =
3860 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
3861 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3861, __PRETTY_FUNCTION__))
; (void)NewIP;
3862 }
3863
3864 // Compute the needed size to hold this FunctionProtoType and the
3865 // various trailing objects.
3866 auto ESH = FunctionProtoType::getExceptionSpecSize(
3867 EPI.ExceptionSpec.Type, EPI.ExceptionSpec.Exceptions.size());
3868 size_t Size = FunctionProtoType::totalSizeToAlloc<
3869 QualType, FunctionType::FunctionTypeExtraBitfields,
3870 FunctionType::ExceptionType, Expr *, FunctionDecl *,
3871 FunctionProtoType::ExtParameterInfo, Qualifiers>(
3872 NumArgs, FunctionProtoType::hasExtraBitfields(EPI.ExceptionSpec.Type),
3873 ESH.NumExceptionType, ESH.NumExprPtr, ESH.NumFunctionDeclPtr,
3874 EPI.ExtParameterInfos ? NumArgs : 0,
3875 EPI.TypeQuals.hasNonFastQualifiers() ? 1 : 0);
3876
3877 auto *FTP = (FunctionProtoType *)Allocate(Size, TypeAlignment);
3878 FunctionProtoType::ExtProtoInfo newEPI = EPI;
3879 new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI);
3880 Types.push_back(FTP);
3881 if (!Unique)
3882 FunctionProtoTypes.InsertNode(FTP, InsertPos);
3883 return QualType(FTP, 0);
3884}
3885
3886QualType ASTContext::getPipeType(QualType T, bool ReadOnly) const {
3887 llvm::FoldingSetNodeID ID;
3888 PipeType::Profile(ID, T, ReadOnly);
3889
3890 void *InsertPos = nullptr;
3891 if (PipeType *PT = PipeTypes.FindNodeOrInsertPos(ID, InsertPos))
3892 return QualType(PT, 0);
3893
3894 // If the pipe element type isn't canonical, this won't be a canonical type
3895 // either, so fill in the canonical type field.
3896 QualType Canonical;
3897 if (!T.isCanonical()) {
3898 Canonical = getPipeType(getCanonicalType(T), ReadOnly);
3899
3900 // Get the new insert position for the node we care about.
3901 PipeType *NewIP = PipeTypes.FindNodeOrInsertPos(ID, InsertPos);
3902 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3902, __PRETTY_FUNCTION__))
;
3903 (void)NewIP;
3904 }
3905 auto *New = new (*this, TypeAlignment) PipeType(T, Canonical, ReadOnly);
3906 Types.push_back(New);
3907 PipeTypes.InsertNode(New, InsertPos);
3908 return QualType(New, 0);
3909}
3910
3911QualType ASTContext::adjustStringLiteralBaseType(QualType Ty) const {
3912 // OpenCL v1.1 s6.5.3: a string literal is in the constant address space.
3913 return LangOpts.OpenCL ? getAddrSpaceQualType(Ty, LangAS::opencl_constant)
3914 : Ty;
3915}
3916
3917QualType ASTContext::getReadPipeType(QualType T) const {
3918 return getPipeType(T, true);
3919}
3920
3921QualType ASTContext::getWritePipeType(QualType T) const {
3922 return getPipeType(T, false);
3923}
3924
3925#ifndef NDEBUG
3926static bool NeedsInjectedClassNameType(const RecordDecl *D) {
3927 if (!isa<CXXRecordDecl>(D)) return false;
3928 const auto *RD = cast<CXXRecordDecl>(D);
3929 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
3930 return true;
3931 if (RD->getDescribedClassTemplate() &&
3932 !isa<ClassTemplateSpecializationDecl>(RD))
3933 return true;
3934 return false;
3935}
3936#endif
3937
3938/// getInjectedClassNameType - Return the unique reference to the
3939/// injected class name type for the specified templated declaration.
3940QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
3941 QualType TST) const {
3942 assert(NeedsInjectedClassNameType(Decl))((NeedsInjectedClassNameType(Decl)) ? static_cast<void>
(0) : __assert_fail ("NeedsInjectedClassNameType(Decl)", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3942, __PRETTY_FUNCTION__))
;
3943 if (Decl->TypeForDecl) {
3944 assert(isa<InjectedClassNameType>(Decl->TypeForDecl))((isa<InjectedClassNameType>(Decl->TypeForDecl)) ? static_cast
<void> (0) : __assert_fail ("isa<InjectedClassNameType>(Decl->TypeForDecl)"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3944, __PRETTY_FUNCTION__))
;
3945 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) {
3946 assert(PrevDecl->TypeForDecl && "previous declaration has no type")((PrevDecl->TypeForDecl && "previous declaration has no type"
) ? static_cast<void> (0) : __assert_fail ("PrevDecl->TypeForDecl && \"previous declaration has no type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3946, __PRETTY_FUNCTION__))
;
3947 Decl->TypeForDecl = PrevDecl->TypeForDecl;
3948 assert(isa<InjectedClassNameType>(Decl->TypeForDecl))((isa<InjectedClassNameType>(Decl->TypeForDecl)) ? static_cast
<void> (0) : __assert_fail ("isa<InjectedClassNameType>(Decl->TypeForDecl)"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3948, __PRETTY_FUNCTION__))
;
3949 } else {
3950 Type *newType =
3951 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
3952 Decl->TypeForDecl = newType;
3953 Types.push_back(newType);
3954 }
3955 return QualType(Decl->TypeForDecl, 0);
3956}
3957
3958/// getTypeDeclType - Return the unique reference to the type for the
3959/// specified type declaration.
3960QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
3961 assert(Decl && "Passed null for Decl param")((Decl && "Passed null for Decl param") ? static_cast
<void> (0) : __assert_fail ("Decl && \"Passed null for Decl param\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3961, __PRETTY_FUNCTION__))
;
3962 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case")((!Decl->TypeForDecl && "TypeForDecl present in slow case"
) ? static_cast<void> (0) : __assert_fail ("!Decl->TypeForDecl && \"TypeForDecl present in slow case\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3962, __PRETTY_FUNCTION__))
;
3963
3964 if (const auto *Typedef = dyn_cast<TypedefNameDecl>(Decl))
3965 return getTypedefType(Typedef);
3966
3967 assert(!isa<TemplateTypeParmDecl>(Decl) &&((!isa<TemplateTypeParmDecl>(Decl) && "Template type parameter types are always available."
) ? static_cast<void> (0) : __assert_fail ("!isa<TemplateTypeParmDecl>(Decl) && \"Template type parameter types are always available.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3968, __PRETTY_FUNCTION__))
3968 "Template type parameter types are always available.")((!isa<TemplateTypeParmDecl>(Decl) && "Template type parameter types are always available."
) ? static_cast<void> (0) : __assert_fail ("!isa<TemplateTypeParmDecl>(Decl) && \"Template type parameter types are always available.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3968, __PRETTY_FUNCTION__))
;
3969
3970 if (const auto *Record = dyn_cast<RecordDecl>(Decl)) {
3971 assert(Record->isFirstDecl() && "struct/union has previous declaration")((Record->isFirstDecl() && "struct/union has previous declaration"
) ? static_cast<void> (0) : __assert_fail ("Record->isFirstDecl() && \"struct/union has previous declaration\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3971, __PRETTY_FUNCTION__))
;
3972 assert(!NeedsInjectedClassNameType(Record))((!NeedsInjectedClassNameType(Record)) ? static_cast<void>
(0) : __assert_fail ("!NeedsInjectedClassNameType(Record)", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3972, __PRETTY_FUNCTION__))
;
3973 return getRecordType(Record);
3974 } else if (const auto *Enum = dyn_cast<EnumDecl>(Decl)) {
3975 assert(Enum->isFirstDecl() && "enum has previous declaration")((Enum->isFirstDecl() && "enum has previous declaration"
) ? static_cast<void> (0) : __assert_fail ("Enum->isFirstDecl() && \"enum has previous declaration\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3975, __PRETTY_FUNCTION__))
;
3976 return getEnumType(Enum);
3977 } else if (const auto *Using = dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
3978 Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
3979 Decl->TypeForDecl = newType;
3980 Types.push_back(newType);
3981 } else
3982 llvm_unreachable("TypeDecl without a type?")::llvm::llvm_unreachable_internal("TypeDecl without a type?",
"/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 3982)
;
3983
3984 return QualType(Decl->TypeForDecl, 0);
3985}
3986
3987/// getTypedefType - Return the unique reference to the type for the
3988/// specified typedef name decl.
3989QualType
3990ASTContext::getTypedefType(const TypedefNameDecl *Decl,
3991 QualType Canonical) const {
3992 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
3993
3994 if (Canonical.isNull())
3995 Canonical = getCanonicalType(Decl->getUnderlyingType());
3996 auto *newType = new (*this, TypeAlignment)
3997 TypedefType(Type::Typedef, Decl, Canonical);
3998 Decl->TypeForDecl = newType;
3999 Types.push_back(newType);
4000 return QualType(newType, 0);
4001}
4002
4003QualType ASTContext::getRecordType(const RecordDecl *Decl) const {
4004 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
4005
4006 if (const RecordDecl *PrevDecl = Decl->getPreviousDecl())
4007 if (PrevDecl->TypeForDecl)
4008 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
4009
4010 auto *newType = new (*this, TypeAlignment) RecordType(Decl);
4011 Decl->TypeForDecl = newType;
4012 Types.push_back(newType);
4013 return QualType(newType, 0);
4014}
4015
4016QualType ASTContext::getEnumType(const EnumDecl *Decl) const {
4017 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
4018
4019 if (const EnumDecl *PrevDecl = Decl->getPreviousDecl())
4020 if (PrevDecl->TypeForDecl)
4021 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
4022
4023 auto *newType = new (*this, TypeAlignment) EnumType(Decl);
4024 Decl->TypeForDecl = newType;
4025 Types.push_back(newType);
4026 return QualType(newType, 0);
4027}
4028
4029QualType ASTContext::getAttributedType(attr::Kind attrKind,
4030 QualType modifiedType,
4031 QualType equivalentType) {
4032 llvm::FoldingSetNodeID id;
4033 AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
4034
4035 void *insertPos = nullptr;
4036 AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
4037 if (type) return QualType(type, 0);
4038
4039 QualType canon = getCanonicalType(equivalentType);
4040 type = new (*this, TypeAlignment)
4041 AttributedType(canon, attrKind, modifiedType, equivalentType);
4042
4043 Types.push_back(type);
4044 AttributedTypes.InsertNode(type, insertPos);
4045
4046 return QualType(type, 0);
4047}
4048
4049/// Retrieve a substitution-result type.
4050QualType
4051ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
4052 QualType Replacement) const {
4053 assert(Replacement.isCanonical()((Replacement.isCanonical() && "replacement types must always be canonical"
) ? static_cast<void> (0) : __assert_fail ("Replacement.isCanonical() && \"replacement types must always be canonical\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4054, __PRETTY_FUNCTION__))
4054 && "replacement types must always be canonical")((Replacement.isCanonical() && "replacement types must always be canonical"
) ? static_cast<void> (0) : __assert_fail ("Replacement.isCanonical() && \"replacement types must always be canonical\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4054, __PRETTY_FUNCTION__))
;
4055
4056 llvm::FoldingSetNodeID ID;
4057 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
4058 void *InsertPos = nullptr;
4059 SubstTemplateTypeParmType *SubstParm
4060 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
4061
4062 if (!SubstParm) {
4063 SubstParm = new (*this, TypeAlignment)
4064 SubstTemplateTypeParmType(Parm, Replacement);
4065 Types.push_back(SubstParm);
4066 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
4067 }
4068
4069 return QualType(SubstParm, 0);
4070}
4071
4072/// Retrieve a
4073QualType ASTContext::getSubstTemplateTypeParmPackType(
4074 const TemplateTypeParmType *Parm,
4075 const TemplateArgument &ArgPack) {
4076#ifndef NDEBUG
4077 for (const auto &P : ArgPack.pack_elements()) {
4078 assert(P.getKind() == TemplateArgument::Type &&"Pack contains a non-type")((P.getKind() == TemplateArgument::Type &&"Pack contains a non-type"
) ? static_cast<void> (0) : __assert_fail ("P.getKind() == TemplateArgument::Type &&\"Pack contains a non-type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4078, __PRETTY_FUNCTION__))
;
4079 assert(P.getAsType().isCanonical() && "Pack contains non-canonical type")((P.getAsType().isCanonical() && "Pack contains non-canonical type"
) ? static_cast<void> (0) : __assert_fail ("P.getAsType().isCanonical() && \"Pack contains non-canonical type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4079, __PRETTY_FUNCTION__))
;
4080 }
4081#endif
4082
4083 llvm::FoldingSetNodeID ID;
4084 SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
4085 void *InsertPos = nullptr;
4086 if (SubstTemplateTypeParmPackType *SubstParm
4087 = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
4088 return QualType(SubstParm, 0);
4089
4090 QualType Canon;
4091 if (!Parm->isCanonicalUnqualified()) {
4092 Canon = getCanonicalType(QualType(Parm, 0));
4093 Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
4094 ArgPack);
4095 SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
4096 }
4097
4098 auto *SubstParm
4099 = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
4100 ArgPack);
4101 Types.push_back(SubstParm);
4102 SubstTemplateTypeParmPackTypes.InsertNode(SubstParm, InsertPos);
4103 return QualType(SubstParm, 0);
4104}
4105
4106/// Retrieve the template type parameter type for a template
4107/// parameter or parameter pack with the given depth, index, and (optionally)
4108/// name.
4109QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
4110 bool ParameterPack,
4111 TemplateTypeParmDecl *TTPDecl) const {
4112 llvm::FoldingSetNodeID ID;
4113 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
4114 void *InsertPos = nullptr;
4115 TemplateTypeParmType *TypeParm
4116 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
4117
4118 if (TypeParm)
4119 return QualType(TypeParm, 0);
4120
4121 if (TTPDecl) {
4122 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
4123 TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
4124
4125 TemplateTypeParmType *TypeCheck
4126 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
4127 assert(!TypeCheck && "Template type parameter canonical type broken")((!TypeCheck && "Template type parameter canonical type broken"
) ? static_cast<void> (0) : __assert_fail ("!TypeCheck && \"Template type parameter canonical type broken\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4127, __PRETTY_FUNCTION__))
;
4128 (void)TypeCheck;
4129 } else
4130 TypeParm = new (*this, TypeAlignment)
4131 TemplateTypeParmType(Depth, Index, ParameterPack);
4132
4133 Types.push_back(TypeParm);
4134 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
4135
4136 return QualType(TypeParm, 0);
4137}
4138
4139TypeSourceInfo *
4140ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
4141 SourceLocation NameLoc,
4142 const TemplateArgumentListInfo &Args,
4143 QualType Underlying) const {
4144 assert(!Name.getAsDependentTemplateName() &&((!Name.getAsDependentTemplateName() && "No dependent template names here!"
) ? static_cast<void> (0) : __assert_fail ("!Name.getAsDependentTemplateName() && \"No dependent template names here!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4145, __PRETTY_FUNCTION__))
4145 "No dependent template names here!")((!Name.getAsDependentTemplateName() && "No dependent template names here!"
) ? static_cast<void> (0) : __assert_fail ("!Name.getAsDependentTemplateName() && \"No dependent template names here!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4145, __PRETTY_FUNCTION__))
;
4146 QualType TST = getTemplateSpecializationType(Name, Args, Underlying);
4147
4148 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
4149 TemplateSpecializationTypeLoc TL =
4150 DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>();
4151 TL.setTemplateKeywordLoc(SourceLocation());
4152 TL.setTemplateNameLoc(NameLoc);
4153 TL.setLAngleLoc(Args.getLAngleLoc());
4154 TL.setRAngleLoc(Args.getRAngleLoc());
4155 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4156 TL.setArgLocInfo(i, Args[i].getLocInfo());
4157 return DI;
4158}
4159
4160QualType
4161ASTContext::getTemplateSpecializationType(TemplateName Template,
4162 const TemplateArgumentListInfo &Args,
4163 QualType Underlying) const {
4164 assert(!Template.getAsDependentTemplateName() &&((!Template.getAsDependentTemplateName() && "No dependent template names here!"
) ? static_cast<void> (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4165, __PRETTY_FUNCTION__))
4165 "No dependent template names here!")((!Template.getAsDependentTemplateName() && "No dependent template names here!"
) ? static_cast<void> (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4165, __PRETTY_FUNCTION__))
;
4166
4167 SmallVector<TemplateArgument, 4> ArgVec;
4168 ArgVec.reserve(Args.size());
4169 for (const TemplateArgumentLoc &Arg : Args.arguments())
4170 ArgVec.push_back(Arg.getArgument());
4171
4172 return getTemplateSpecializationType(Template, ArgVec, Underlying);
4173}
4174
4175#ifndef NDEBUG
4176static bool hasAnyPackExpansions(ArrayRef<TemplateArgument> Args) {
4177 for (const TemplateArgument &Arg : Args)
4178 if (Arg.isPackExpansion())
4179 return true;
4180
4181 return true;
4182}
4183#endif
4184
4185QualType
4186ASTContext::getTemplateSpecializationType(TemplateName Template,
4187 ArrayRef<TemplateArgument> Args,
4188 QualType Underlying) const {
4189 assert(!Template.getAsDependentTemplateName() &&((!Template.getAsDependentTemplateName() && "No dependent template names here!"
) ? static_cast<void> (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4190, __PRETTY_FUNCTION__))
4190 "No dependent template names here!")((!Template.getAsDependentTemplateName() && "No dependent template names here!"
) ? static_cast<void> (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4190, __PRETTY_FUNCTION__))
;
4191 // Look through qualified template names.
4192 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
4193 Template = TemplateName(QTN->getTemplateDecl());
4194
4195 bool IsTypeAlias =
4196 Template.getAsTemplateDecl() &&
4197 isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
4198 QualType CanonType;
4199 if (!Underlying.isNull())
4200 CanonType = getCanonicalType(Underlying);
4201 else {
4202 // We can get here with an alias template when the specialization contains
4203 // a pack expansion that does not match up with a parameter pack.
4204 assert((!IsTypeAlias || hasAnyPackExpansions(Args)) &&(((!IsTypeAlias || hasAnyPackExpansions(Args)) && "Caller must compute aliased type"
) ? static_cast<void> (0) : __assert_fail ("(!IsTypeAlias || hasAnyPackExpansions(Args)) && \"Caller must compute aliased type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4205, __PRETTY_FUNCTION__))
4205 "Caller must compute aliased type")(((!IsTypeAlias || hasAnyPackExpansions(Args)) && "Caller must compute aliased type"
) ? static_cast<void> (0) : __assert_fail ("(!IsTypeAlias || hasAnyPackExpansions(Args)) && \"Caller must compute aliased type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4205, __PRETTY_FUNCTION__))
;
4206 IsTypeAlias = false;
4207 CanonType = getCanonicalTemplateSpecializationType(Template, Args);
4208 }
4209
4210 // Allocate the (non-canonical) template specialization type, but don't
4211 // try to unique it: these types typically have location information that
4212 // we don't unique and don't want to lose.
4213 void *Mem = Allocate(sizeof(TemplateSpecializationType) +
4214 sizeof(TemplateArgument) * Args.size() +
4215 (IsTypeAlias? sizeof(QualType) : 0),
4216 TypeAlignment);
4217 auto *Spec
4218 = new (Mem) TemplateSpecializationType(Template, Args, CanonType,
4219 IsTypeAlias ? Underlying : QualType());
4220
4221 Types.push_back(Spec);
4222 return QualType(Spec, 0);
4223}
4224
4225QualType ASTContext::getCanonicalTemplateSpecializationType(
4226 TemplateName Template, ArrayRef<TemplateArgument> Args) const {
4227 assert(!Template.getAsDependentTemplateName() &&((!Template.getAsDependentTemplateName() && "No dependent template names here!"
) ? static_cast<void> (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4228, __PRETTY_FUNCTION__))
4228 "No dependent template names here!")((!Template.getAsDependentTemplateName() && "No dependent template names here!"
) ? static_cast<void> (0) : __assert_fail ("!Template.getAsDependentTemplateName() && \"No dependent template names here!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4228, __PRETTY_FUNCTION__))
;
4229
4230 // Look through qualified template names.
4231 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
4232 Template = TemplateName(QTN->getTemplateDecl());
4233
4234 // Build the canonical template specialization type.
4235 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
4236 SmallVector<TemplateArgument, 4> CanonArgs;
4237 unsigned NumArgs = Args.size();
4238 CanonArgs.reserve(NumArgs);
4239 for (const TemplateArgument &Arg : Args)
4240 CanonArgs.push_back(getCanonicalTemplateArgument(Arg));
4241
4242 // Determine whether this canonical template specialization type already
4243 // exists.
4244 llvm::FoldingSetNodeID ID;
4245 TemplateSpecializationType::Profile(ID, CanonTemplate,
4246 CanonArgs, *this);
4247
4248 void *InsertPos = nullptr;
4249 TemplateSpecializationType *Spec
4250 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
4251
4252 if (!Spec) {
4253 // Allocate a new canonical template specialization type.
4254 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
4255 sizeof(TemplateArgument) * NumArgs),
4256 TypeAlignment);
4257 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
4258 CanonArgs,
4259 QualType(), QualType());
4260 Types.push_back(Spec);
4261 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
4262 }
4263
4264 assert(Spec->isDependentType() &&((Spec->isDependentType() && "Non-dependent template-id type must have a canonical type"
) ? static_cast<void> (0) : __assert_fail ("Spec->isDependentType() && \"Non-dependent template-id type must have a canonical type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4265, __PRETTY_FUNCTION__))
4265 "Non-dependent template-id type must have a canonical type")((Spec->isDependentType() && "Non-dependent template-id type must have a canonical type"
) ? static_cast<void> (0) : __assert_fail ("Spec->isDependentType() && \"Non-dependent template-id type must have a canonical type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4265, __PRETTY_FUNCTION__))
;
4266 return QualType(Spec, 0);
4267}
4268
4269QualType ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
4270 NestedNameSpecifier *NNS,
4271 QualType NamedType,
4272 TagDecl *OwnedTagDecl) const {
4273 llvm::FoldingSetNodeID ID;
4274 ElaboratedType::Profile(ID, Keyword, NNS, NamedType, OwnedTagDecl);
4275
4276 void *InsertPos = nullptr;
4277 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
4278 if (T)
4279 return QualType(T, 0);
4280
4281 QualType Canon = NamedType;
4282 if (!Canon.isCanonical()) {
4283 Canon = getCanonicalType(NamedType);
4284 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
4285 assert(!CheckT && "Elaborated canonical type broken")((!CheckT && "Elaborated canonical type broken") ? static_cast
<void> (0) : __assert_fail ("!CheckT && \"Elaborated canonical type broken\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4285, __PRETTY_FUNCTION__))
;
4286 (void)CheckT;
4287 }
4288
4289 void *Mem = Allocate(ElaboratedType::totalSizeToAlloc<TagDecl *>(!!OwnedTagDecl),
4290 TypeAlignment);
4291 T = new (Mem) ElaboratedType(Keyword, NNS, NamedType, Canon, OwnedTagDecl);
4292
4293 Types.push_back(T);
4294 ElaboratedTypes.InsertNode(T, InsertPos);
4295 return QualType(T, 0);
4296}
4297
4298QualType
4299ASTContext::getParenType(QualType InnerType) const {
4300 llvm::FoldingSetNodeID ID;
4301 ParenType::Profile(ID, InnerType);
4302
4303 void *InsertPos = nullptr;
4304 ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
4305 if (T)
4306 return QualType(T, 0);
4307
4308 QualType Canon = InnerType;
4309 if (!Canon.isCanonical()) {
4310 Canon = getCanonicalType(InnerType);
4311 ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
4312 assert(!CheckT && "Paren canonical type broken")((!CheckT && "Paren canonical type broken") ? static_cast
<void> (0) : __assert_fail ("!CheckT && \"Paren canonical type broken\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4312, __PRETTY_FUNCTION__))
;
4313 (void)CheckT;
4314 }
4315
4316 T = new (*this, TypeAlignment) ParenType(InnerType, Canon);
4317 Types.push_back(T);
4318 ParenTypes.InsertNode(T, InsertPos);
4319 return QualType(T, 0);
4320}
4321
4322QualType
4323ASTContext::getMacroQualifiedType(QualType UnderlyingTy,
4324 const IdentifierInfo *MacroII) const {
4325 QualType Canon = UnderlyingTy;
4326 if (!Canon.isCanonical())
4327 Canon = getCanonicalType(UnderlyingTy);
4328
4329 auto *newType = new (*this, TypeAlignment)
4330 MacroQualifiedType(UnderlyingTy, Canon, MacroII);
4331 Types.push_back(newType);
4332 return QualType(newType, 0);
4333}
4334
4335QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
4336 NestedNameSpecifier *NNS,
4337 const IdentifierInfo *Name,
4338 QualType Canon) const {
4339 if (Canon.isNull()) {
4340 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4341 if (CanonNNS != NNS)
4342 Canon = getDependentNameType(Keyword, CanonNNS, Name);
4343 }
4344
4345 llvm::FoldingSetNodeID ID;
4346 DependentNameType::Profile(ID, Keyword, NNS, Name);
4347
4348 void *InsertPos = nullptr;
4349 DependentNameType *T
4350 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
4351 if (T)
4352 return QualType(T, 0);
4353
4354 T = new (*this, TypeAlignment) DependentNameType(Keyword, NNS, Name, Canon);
4355 Types.push_back(T);
4356 DependentNameTypes.InsertNode(T, InsertPos);
4357 return QualType(T, 0);
4358}
4359
4360QualType
4361ASTContext::getDependentTemplateSpecializationType(
4362 ElaboratedTypeKeyword Keyword,
4363 NestedNameSpecifier *NNS,
4364 const IdentifierInfo *Name,
4365 const TemplateArgumentListInfo &Args) const {
4366 // TODO: avoid this copy
4367 SmallVector<TemplateArgument, 16> ArgCopy;
4368 for (unsigned I = 0, E = Args.size(); I != E; ++I)
4369 ArgCopy.push_back(Args[I].getArgument());
4370 return getDependentTemplateSpecializationType(Keyword, NNS, Name, ArgCopy);
4371}
4372
4373QualType
4374ASTContext::getDependentTemplateSpecializationType(
4375 ElaboratedTypeKeyword Keyword,
4376 NestedNameSpecifier *NNS,
4377 const IdentifierInfo *Name,
4378 ArrayRef<TemplateArgument> Args) const {
4379 assert((!NNS || NNS->isDependent()) &&(((!NNS || NNS->isDependent()) && "nested-name-specifier must be dependent"
) ? static_cast<void> (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"nested-name-specifier must be dependent\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4380, __PRETTY_FUNCTION__))
4380 "nested-name-specifier must be dependent")(((!NNS || NNS->isDependent()) && "nested-name-specifier must be dependent"
) ? static_cast<void> (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"nested-name-specifier must be dependent\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4380, __PRETTY_FUNCTION__))
;
4381
4382 llvm::FoldingSetNodeID ID;
4383 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
4384 Name, Args);
4385
4386 void *InsertPos = nullptr;
4387 DependentTemplateSpecializationType *T
4388 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
4389 if (T)
4390 return QualType(T, 0);
4391
4392 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4393
4394 ElaboratedTypeKeyword CanonKeyword = Keyword;
4395 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
4396
4397 bool AnyNonCanonArgs = false;
4398 unsigned NumArgs = Args.size();
4399 SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
4400 for (unsigned I = 0; I != NumArgs; ++I) {
4401 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
4402 if (!CanonArgs[I].structurallyEquals(Args[I]))
4403 AnyNonCanonArgs = true;
4404 }
4405
4406 QualType Canon;
4407 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
4408 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
4409 Name,
4410 CanonArgs);
4411
4412 // Find the insert position again.
4413 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
4414 }
4415
4416 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
4417 sizeof(TemplateArgument) * NumArgs),
4418 TypeAlignment);
4419 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
4420 Name, Args, Canon);
4421 Types.push_back(T);
4422 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
4423 return QualType(T, 0);
4424}
4425
4426TemplateArgument ASTContext::getInjectedTemplateArg(NamedDecl *Param) {
4427 TemplateArgument Arg;
4428 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4429 QualType ArgType = getTypeDeclType(TTP);
4430 if (TTP->isParameterPack())
4431 ArgType = getPackExpansionType(ArgType, None);
4432
4433 Arg = TemplateArgument(ArgType);
4434 } else if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4435 Expr *E = new (*this) DeclRefExpr(
4436 *this, NTTP, /*enclosing*/ false,
4437 NTTP->getType().getNonLValueExprType(*this),
4438 Expr::getValueKindForType(NTTP->getType()), NTTP->getLocation());
4439
4440 if (NTTP->isParameterPack())
4441 E = new (*this) PackExpansionExpr(DependentTy, E, NTTP->getLocation(),
4442 None);
4443 Arg = TemplateArgument(E);
4444 } else {
4445 auto *TTP = cast<TemplateTemplateParmDecl>(Param);
4446 if (TTP->isParameterPack())
4447 Arg = TemplateArgument(TemplateName(TTP), Optional<unsigned>());
4448 else
4449 Arg = TemplateArgument(TemplateName(TTP));
4450 }
4451
4452 if (Param->isTemplateParameterPack())
4453 Arg = TemplateArgument::CreatePackCopy(*this, Arg);
4454
4455 return Arg;
4456}
4457
4458void
4459ASTContext::getInjectedTemplateArgs(const TemplateParameterList *Params,
4460 SmallVectorImpl<TemplateArgument> &Args) {
4461 Args.reserve(Args.size() + Params->size());
4462
4463 for (NamedDecl *Param : *Params)
4464 Args.push_back(getInjectedTemplateArg(Param));
4465}
4466
4467QualType ASTContext::getPackExpansionType(QualType Pattern,
4468 Optional<unsigned> NumExpansions) {
4469 llvm::FoldingSetNodeID ID;
4470 PackExpansionType::Profile(ID, Pattern, NumExpansions);
4471
4472 // A deduced type can deduce to a pack, eg
4473 // auto ...x = some_pack;
4474 // That declaration isn't (yet) valid, but is created as part of building an
4475 // init-capture pack:
4476 // [...x = some_pack] {}
4477 assert((Pattern->containsUnexpandedParameterPack() ||(((Pattern->containsUnexpandedParameterPack() || Pattern->
getContainedDeducedType()) && "Pack expansions must expand one or more parameter packs"
) ? static_cast<void> (0) : __assert_fail ("(Pattern->containsUnexpandedParameterPack() || Pattern->getContainedDeducedType()) && \"Pack expansions must expand one or more parameter packs\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4479, __PRETTY_FUNCTION__))
4478 Pattern->getContainedDeducedType()) &&(((Pattern->containsUnexpandedParameterPack() || Pattern->
getContainedDeducedType()) && "Pack expansions must expand one or more parameter packs"
) ? static_cast<void> (0) : __assert_fail ("(Pattern->containsUnexpandedParameterPack() || Pattern->getContainedDeducedType()) && \"Pack expansions must expand one or more parameter packs\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4479, __PRETTY_FUNCTION__))
4479 "Pack expansions must expand one or more parameter packs")(((Pattern->containsUnexpandedParameterPack() || Pattern->
getContainedDeducedType()) && "Pack expansions must expand one or more parameter packs"
) ? static_cast<void> (0) : __assert_fail ("(Pattern->containsUnexpandedParameterPack() || Pattern->getContainedDeducedType()) && \"Pack expansions must expand one or more parameter packs\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4479, __PRETTY_FUNCTION__))
;
4480 void *InsertPos = nullptr;
4481 PackExpansionType *T
4482 = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
4483 if (T)
4484 return QualType(T, 0);
4485
4486 QualType Canon;
4487 if (!Pattern.isCanonical()) {
4488 Canon = getCanonicalType(Pattern);
4489 // The canonical type might not contain an unexpanded parameter pack, if it
4490 // contains an alias template specialization which ignores one of its
4491 // parameters.
4492 if (Canon->containsUnexpandedParameterPack()) {
4493 Canon = getPackExpansionType(Canon, NumExpansions);
4494
4495 // Find the insert position again, in case we inserted an element into
4496 // PackExpansionTypes and invalidated our insert position.
4497 PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
4498 }
4499 }
4500
4501 T = new (*this, TypeAlignment)
4502 PackExpansionType(Pattern, Canon, NumExpansions);
4503 Types.push_back(T);
4504 PackExpansionTypes.InsertNode(T, InsertPos);
4505 return QualType(T, 0);
4506}
4507
4508/// CmpProtocolNames - Comparison predicate for sorting protocols
4509/// alphabetically.
4510static int CmpProtocolNames(ObjCProtocolDecl *const *LHS,
4511 ObjCProtocolDecl *const *RHS) {
4512 return DeclarationName::compare((*LHS)->getDeclName(), (*RHS)->getDeclName());
4513}
4514
4515static bool areSortedAndUniqued(ArrayRef<ObjCProtocolDecl *> Protocols) {
4516 if (Protocols.empty()) return true;
4517
4518 if (Protocols[0]->getCanonicalDecl() != Protocols[0])
4519 return false;
4520
4521 for (unsigned i = 1; i != Protocols.size(); ++i)
4522 if (CmpProtocolNames(&Protocols[i - 1], &Protocols[i]) >= 0 ||
4523 Protocols[i]->getCanonicalDecl() != Protocols[i])
4524 return false;
4525 return true;
4526}
4527
4528static void
4529SortAndUniqueProtocols(SmallVectorImpl<ObjCProtocolDecl *> &Protocols) {
4530 // Sort protocols, keyed by name.
4531 llvm::array_pod_sort(Protocols.begin(), Protocols.end(), CmpProtocolNames);
4532
4533 // Canonicalize.
4534 for (ObjCProtocolDecl *&P : Protocols)
4535 P = P->getCanonicalDecl();
4536
4537 // Remove duplicates.
4538 auto ProtocolsEnd = std::unique(Protocols.begin(), Protocols.end());
4539 Protocols.erase(ProtocolsEnd, Protocols.end());
4540}
4541
4542QualType ASTContext::getObjCObjectType(QualType BaseType,
4543 ObjCProtocolDecl * const *Protocols,
4544 unsigned NumProtocols) const {
4545 return getObjCObjectType(BaseType, {},
4546 llvm::makeArrayRef(Protocols, NumProtocols),
4547 /*isKindOf=*/false);
4548}
4549
4550QualType ASTContext::getObjCObjectType(
4551 QualType baseType,
4552 ArrayRef<QualType> typeArgs,
4553 ArrayRef<ObjCProtocolDecl *> protocols,
4554 bool isKindOf) const {
4555 // If the base type is an interface and there aren't any protocols or
4556 // type arguments to add, then the interface type will do just fine.
4557 if (typeArgs.empty() && protocols.empty() && !isKindOf &&
4558 isa<ObjCInterfaceType>(baseType))
4559 return baseType;
4560
4561 // Look in the folding set for an existing type.
4562 llvm::FoldingSetNodeID ID;
4563 ObjCObjectTypeImpl::Profile(ID, baseType, typeArgs, protocols, isKindOf);
4564 void *InsertPos = nullptr;
4565 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
4566 return QualType(QT, 0);
4567
4568 // Determine the type arguments to be used for canonicalization,
4569 // which may be explicitly specified here or written on the base
4570 // type.
4571 ArrayRef<QualType> effectiveTypeArgs = typeArgs;
4572 if (effectiveTypeArgs.empty()) {
4573 if (const auto *baseObject = baseType->getAs<ObjCObjectType>())
4574 effectiveTypeArgs = baseObject->getTypeArgs();
4575 }
4576
4577 // Build the canonical type, which has the canonical base type and a
4578 // sorted-and-uniqued list of protocols and the type arguments
4579 // canonicalized.
4580 QualType canonical;
4581 bool typeArgsAreCanonical = std::all_of(effectiveTypeArgs.begin(),
4582 effectiveTypeArgs.end(),
4583 [&](QualType type) {
4584 return type.isCanonical();
4585 });
4586 bool protocolsSorted = areSortedAndUniqued(protocols);
4587 if (!typeArgsAreCanonical || !protocolsSorted || !baseType.isCanonical()) {
4588 // Determine the canonical type arguments.
4589 ArrayRef<QualType> canonTypeArgs;
4590 SmallVector<QualType, 4> canonTypeArgsVec;
4591 if (!typeArgsAreCanonical) {
4592 canonTypeArgsVec.reserve(effectiveTypeArgs.size());
4593 for (auto typeArg : effectiveTypeArgs)
4594 canonTypeArgsVec.push_back(getCanonicalType(typeArg));
4595 canonTypeArgs = canonTypeArgsVec;
4596 } else {
4597 canonTypeArgs = effectiveTypeArgs;
4598 }
4599
4600 ArrayRef<ObjCProtocolDecl *> canonProtocols;
4601 SmallVector<ObjCProtocolDecl*, 8> canonProtocolsVec;
4602 if (!protocolsSorted) {
4603 canonProtocolsVec.append(protocols.begin(), protocols.end());
4604 SortAndUniqueProtocols(canonProtocolsVec);
4605 canonProtocols = canonProtocolsVec;
4606 } else {
4607 canonProtocols = protocols;
4608 }
4609
4610 canonical = getObjCObjectType(getCanonicalType(baseType), canonTypeArgs,
4611 canonProtocols, isKindOf);
4612
4613 // Regenerate InsertPos.
4614 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
4615 }
4616
4617 unsigned size = sizeof(ObjCObjectTypeImpl);
4618 size += typeArgs.size() * sizeof(QualType);
4619 size += protocols.size() * sizeof(ObjCProtocolDecl *);
4620 void *mem = Allocate(size, TypeAlignment);
4621 auto *T =
4622 new (mem) ObjCObjectTypeImpl(canonical, baseType, typeArgs, protocols,
4623 isKindOf);
4624
4625 Types.push_back(T);
4626 ObjCObjectTypes.InsertNode(T, InsertPos);
4627 return QualType(T, 0);
4628}
4629
4630/// Apply Objective-C protocol qualifiers to the given type.
4631/// If this is for the canonical type of a type parameter, we can apply
4632/// protocol qualifiers on the ObjCObjectPointerType.
4633QualType
4634ASTContext::applyObjCProtocolQualifiers(QualType type,
4635 ArrayRef<ObjCProtocolDecl *> protocols, bool &hasError,
4636 bool allowOnPointerType) const {
4637 hasError = false;
4638
4639 if (const auto *objT = dyn_cast<ObjCTypeParamType>(type.getTypePtr())) {
4640 return getObjCTypeParamType(objT->getDecl(), protocols);
4641 }
4642
4643 // Apply protocol qualifiers to ObjCObjectPointerType.
4644 if (allowOnPointerType) {
4645 if (const auto *objPtr =
4646 dyn_cast<ObjCObjectPointerType>(type.getTypePtr())) {
4647 const ObjCObjectType *objT = objPtr->getObjectType();
4648 // Merge protocol lists and construct ObjCObjectType.
4649 SmallVector<ObjCProtocolDecl*, 8> protocolsVec;
4650 protocolsVec.append(objT->qual_begin(),
4651 objT->qual_end());
4652 protocolsVec.append(protocols.begin(), protocols.end());
4653 ArrayRef<ObjCProtocolDecl *> protocols = protocolsVec;
4654 type = getObjCObjectType(
4655 objT->getBaseType(),
4656 objT->getTypeArgsAsWritten(),
4657 protocols,
4658 objT->isKindOfTypeAsWritten());
4659 return getObjCObjectPointerType(type);
4660 }
4661 }
4662
4663 // Apply protocol qualifiers to ObjCObjectType.
4664 if (const auto *objT = dyn_cast<ObjCObjectType>(type.getTypePtr())){
4665 // FIXME: Check for protocols to which the class type is already
4666 // known to conform.
4667
4668 return getObjCObjectType(objT->getBaseType(),
4669 objT->getTypeArgsAsWritten(),
4670 protocols,
4671 objT->isKindOfTypeAsWritten());
4672 }
4673
4674 // If the canonical type is ObjCObjectType, ...
4675 if (type->isObjCObjectType()) {
4676 // Silently overwrite any existing protocol qualifiers.
4677 // TODO: determine whether that's the right thing to do.
4678
4679 // FIXME: Check for protocols to which the class type is already
4680 // known to conform.
4681 return getObjCObjectType(type, {}, protocols, false);
4682 }
4683
4684 // id<protocol-list>
4685 if (type->isObjCIdType()) {
4686 const auto *objPtr = type->castAs<ObjCObjectPointerType>();
4687 type = getObjCObjectType(ObjCBuiltinIdTy, {}, protocols,
4688 objPtr->isKindOfType());
4689 return getObjCObjectPointerType(type);
4690 }
4691
4692 // Class<protocol-list>
4693 if (type->isObjCClassType()) {
4694 const auto *objPtr = type->castAs<ObjCObjectPointerType>();
4695 type = getObjCObjectType(ObjCBuiltinClassTy, {}, protocols,
4696 objPtr->isKindOfType());
4697 return getObjCObjectPointerType(type);
4698 }
4699
4700 hasError = true;
4701 return type;
4702}
4703
4704QualType
4705ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl,
4706 ArrayRef<ObjCProtocolDecl *> protocols,
4707 QualType Canonical) const {
4708 // Look in the folding set for an existing type.
4709 llvm::FoldingSetNodeID ID;
4710 ObjCTypeParamType::Profile(ID, Decl, protocols);
4711 void *InsertPos = nullptr;
4712 if (ObjCTypeParamType *TypeParam =
4713 ObjCTypeParamTypes.FindNodeOrInsertPos(ID, InsertPos))
4714 return QualType(TypeParam, 0);
4715
4716 if (Canonical.isNull()) {
4717 // We canonicalize to the underlying type.
4718 Canonical = getCanonicalType(Decl->getUnderlyingType());
4719 if (!protocols.empty()) {
4720 // Apply the protocol qualifers.
4721 bool hasError;
4722 Canonical = getCanonicalType(applyObjCProtocolQualifiers(
4723 Canonical, protocols, hasError, true /*allowOnPointerType*/));
4724 assert(!hasError && "Error when apply protocol qualifier to bound type")((!hasError && "Error when apply protocol qualifier to bound type"
) ? static_cast<void> (0) : __assert_fail ("!hasError && \"Error when apply protocol qualifier to bound type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4724, __PRETTY_FUNCTION__))
;
4725 }
4726 }
4727
4728 unsigned size = sizeof(ObjCTypeParamType);
4729 size += protocols.size() * sizeof(ObjCProtocolDecl *);
4730 void *mem = Allocate(size, TypeAlignment);
4731 auto *newType = new (mem) ObjCTypeParamType(Decl, Canonical, protocols);
4732
4733 Types.push_back(newType);
4734 ObjCTypeParamTypes.InsertNode(newType, InsertPos);
4735 return QualType(newType, 0);
4736}
4737
4738/// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's
4739/// protocol list adopt all protocols in QT's qualified-id protocol
4740/// list.
4741bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT,
4742 ObjCInterfaceDecl *IC) {
4743 if (!QT->isObjCQualifiedIdType())
4744 return false;
4745
4746 if (const auto *OPT = QT->getAs<ObjCObjectPointerType>()) {
4747 // If both the right and left sides have qualifiers.
4748 for (auto *Proto : OPT->quals()) {
4749 if (!IC->ClassImplementsProtocol(Proto, false))
4750 return false;
4751 }
4752 return true;
4753 }
4754 return false;
4755}
4756
4757/// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in
4758/// QT's qualified-id protocol list adopt all protocols in IDecl's list
4759/// of protocols.
4760bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT,
4761 ObjCInterfaceDecl *IDecl) {
4762 if (!QT->isObjCQualifiedIdType())
4763 return false;
4764 const auto *OPT = QT->getAs<ObjCObjectPointerType>();
4765 if (!OPT)
4766 return false;
4767 if (!IDecl->hasDefinition())
4768 return false;
4769 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols;
4770 CollectInheritedProtocols(IDecl, InheritedProtocols);
4771 if (InheritedProtocols.empty())
4772 return false;
4773 // Check that if every protocol in list of id<plist> conforms to a protocol
4774 // of IDecl's, then bridge casting is ok.
4775 bool Conforms = false;
4776 for (auto *Proto : OPT->quals()) {
4777 Conforms = false;
4778 for (auto *PI : InheritedProtocols) {
4779 if (ProtocolCompatibleWithProtocol(Proto, PI)) {
4780 Conforms = true;
4781 break;
4782 }
4783 }
4784 if (!Conforms)
4785 break;
4786 }
4787 if (Conforms)
4788 return true;
4789
4790 for (auto *PI : InheritedProtocols) {
4791 // If both the right and left sides have qualifiers.
4792 bool Adopts = false;
4793 for (auto *Proto : OPT->quals()) {
4794 // return 'true' if 'PI' is in the inheritance hierarchy of Proto
4795 if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto)))
4796 break;
4797 }
4798 if (!Adopts)
4799 return false;
4800 }
4801 return true;
4802}
4803
4804/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
4805/// the given object type.
4806QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
4807 llvm::FoldingSetNodeID ID;
4808 ObjCObjectPointerType::Profile(ID, ObjectT);
4809
4810 void *InsertPos = nullptr;
4811 if (ObjCObjectPointerType *QT =
4812 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
4813 return QualType(QT, 0);
4814
4815 // Find the canonical object type.
4816 QualType Canonical;
4817 if (!ObjectT.isCanonical()) {
4818 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
4819
4820 // Regenerate InsertPos.
4821 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
4822 }
4823
4824 // No match.
4825 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
4826 auto *QType =
4827 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
4828
4829 Types.push_back(QType);
4830 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
4831 return QualType(QType, 0);
4832}
4833
4834/// getObjCInterfaceType - Return the unique reference to the type for the
4835/// specified ObjC interface decl. The list of protocols is optional.
4836QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
4837 ObjCInterfaceDecl *PrevDecl) const {
4838 if (Decl->TypeForDecl)
4839 return QualType(Decl->TypeForDecl, 0);
4840
4841 if (PrevDecl) {
4842 assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl")((PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"
) ? static_cast<void> (0) : __assert_fail ("PrevDecl->TypeForDecl && \"previous decl has no TypeForDecl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4842, __PRETTY_FUNCTION__))
;
4843 Decl->TypeForDecl = PrevDecl->TypeForDecl;
4844 return QualType(PrevDecl->TypeForDecl, 0);
4845 }
4846
4847 // Prefer the definition, if there is one.
4848 if (const ObjCInterfaceDecl *Def = Decl->getDefinition())
4849 Decl = Def;
4850
4851 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
4852 auto *T = new (Mem) ObjCInterfaceType(Decl);
4853 Decl->TypeForDecl = T;
4854 Types.push_back(T);
4855 return QualType(T, 0);
4856}
4857
4858/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
4859/// TypeOfExprType AST's (since expression's are never shared). For example,
4860/// multiple declarations that refer to "typeof(x)" all contain different
4861/// DeclRefExpr's. This doesn't effect the type checker, since it operates
4862/// on canonical type's (which are always unique).
4863QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
4864 TypeOfExprType *toe;
4865 if (tofExpr->isTypeDependent()) {
4866 llvm::FoldingSetNodeID ID;
4867 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
4868
4869 void *InsertPos = nullptr;
4870 DependentTypeOfExprType *Canon
4871 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
4872 if (Canon) {
4873 // We already have a "canonical" version of an identical, dependent
4874 // typeof(expr) type. Use that as our canonical type.
4875 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
4876 QualType((TypeOfExprType*)Canon, 0));
4877 } else {
4878 // Build a new, canonical typeof(expr) type.
4879 Canon
4880 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
4881 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
4882 toe = Canon;
4883 }
4884 } else {
4885 QualType Canonical = getCanonicalType(tofExpr->getType());
4886 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
4887 }
4888 Types.push_back(toe);
4889 return QualType(toe, 0);
4890}
4891
4892/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
4893/// TypeOfType nodes. The only motivation to unique these nodes would be
4894/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
4895/// an issue. This doesn't affect the type checker, since it operates
4896/// on canonical types (which are always unique).
4897QualType ASTContext::getTypeOfType(QualType tofType) const {
4898 QualType Canonical = getCanonicalType(tofType);
4899 auto *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
4900 Types.push_back(tot);
4901 return QualType(tot, 0);
4902}
4903
4904/// Unlike many "get<Type>" functions, we don't unique DecltypeType
4905/// nodes. This would never be helpful, since each such type has its own
4906/// expression, and would not give a significant memory saving, since there
4907/// is an Expr tree under each such type.
4908QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const {
4909 DecltypeType *dt;
4910
4911 // C++11 [temp.type]p2:
4912 // If an expression e involves a template parameter, decltype(e) denotes a
4913 // unique dependent type. Two such decltype-specifiers refer to the same
4914 // type only if their expressions are equivalent (14.5.6.1).
4915 if (e->isInstantiationDependent()) {
4916 llvm::FoldingSetNodeID ID;
4917 DependentDecltypeType::Profile(ID, *this, e);
4918
4919 void *InsertPos = nullptr;
4920 DependentDecltypeType *Canon
4921 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
4922 if (!Canon) {
4923 // Build a new, canonical decltype(expr) type.
4924 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
4925 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
4926 }
4927 dt = new (*this, TypeAlignment)
4928 DecltypeType(e, UnderlyingType, QualType((DecltypeType *)Canon, 0));
4929 } else {
4930 dt = new (*this, TypeAlignment)
4931 DecltypeType(e, UnderlyingType, getCanonicalType(UnderlyingType));
4932 }
4933 Types.push_back(dt);
4934 return QualType(dt, 0);
4935}
4936
4937/// getUnaryTransformationType - We don't unique these, since the memory
4938/// savings are minimal and these are rare.
4939QualType ASTContext::getUnaryTransformType(QualType BaseType,
4940 QualType UnderlyingType,
4941 UnaryTransformType::UTTKind Kind)
4942 const {
4943 UnaryTransformType *ut = nullptr;
4944
4945 if (BaseType->isDependentType()) {
4946 // Look in the folding set for an existing type.
4947 llvm::FoldingSetNodeID ID;
4948 DependentUnaryTransformType::Profile(ID, getCanonicalType(BaseType), Kind);
4949
4950 void *InsertPos = nullptr;
4951 DependentUnaryTransformType *Canon
4952 = DependentUnaryTransformTypes.FindNodeOrInsertPos(ID, InsertPos);
4953
4954 if (!Canon) {
4955 // Build a new, canonical __underlying_type(type) type.
4956 Canon = new (*this, TypeAlignment)
4957 DependentUnaryTransformType(*this, getCanonicalType(BaseType),
4958 Kind);
4959 DependentUnaryTransformTypes.InsertNode(Canon, InsertPos);
4960 }
4961 ut = new (*this, TypeAlignment) UnaryTransformType (BaseType,
4962 QualType(), Kind,
4963 QualType(Canon, 0));
4964 } else {
4965 QualType CanonType = getCanonicalType(UnderlyingType);
4966 ut = new (*this, TypeAlignment) UnaryTransformType (BaseType,
4967 UnderlyingType, Kind,
4968 CanonType);
4969 }
4970 Types.push_back(ut);
4971 return QualType(ut, 0);
4972}
4973
4974/// getAutoType - Return the uniqued reference to the 'auto' type which has been
4975/// deduced to the given type, or to the canonical undeduced 'auto' type, or the
4976/// canonical deduced-but-dependent 'auto' type.
4977QualType ASTContext::getAutoType(QualType DeducedType, AutoTypeKeyword Keyword,
4978 bool IsDependent, bool IsPack) const {
4979 assert((!IsPack || IsDependent) && "only use IsPack for a dependent pack")(((!IsPack || IsDependent) && "only use IsPack for a dependent pack"
) ? static_cast<void> (0) : __assert_fail ("(!IsPack || IsDependent) && \"only use IsPack for a dependent pack\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 4979, __PRETTY_FUNCTION__))
;
4980 if (DeducedType.isNull() && Keyword == AutoTypeKeyword::Auto && !IsDependent)
4981 return getAutoDeductType();
4982
4983 // Look in the folding set for an existing type.
4984 void *InsertPos = nullptr;
4985 llvm::FoldingSetNodeID ID;
4986 AutoType::Profile(ID, DeducedType, Keyword, IsDependent, IsPack);
4987 if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
4988 return QualType(AT, 0);
4989
4990 auto *AT = new (*this, TypeAlignment)
4991 AutoType(DeducedType, Keyword, IsDependent, IsPack);
4992 Types.push_back(AT);
4993 if (InsertPos)
4994 AutoTypes.InsertNode(AT, InsertPos);
4995 return QualType(AT, 0);
4996}
4997
4998/// Return the uniqued reference to the deduced template specialization type
4999/// which has been deduced to the given type, or to the canonical undeduced
5000/// such type, or the canonical deduced-but-dependent such type.
5001QualType ASTContext::getDeducedTemplateSpecializationType(
5002 TemplateName Template, QualType DeducedType, bool IsDependent) const {
5003 // Look in the folding set for an existing type.
5004 void *InsertPos = nullptr;
5005 llvm::FoldingSetNodeID ID;
5006 DeducedTemplateSpecializationType::Profile(ID, Template, DeducedType,
5007 IsDependent);
5008 if (DeducedTemplateSpecializationType *DTST =
5009 DeducedTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos))
5010 return QualType(DTST, 0);
5011
5012 auto *DTST = new (*this, TypeAlignment)
5013 DeducedTemplateSpecializationType(Template, DeducedType, IsDependent);
5014 Types.push_back(DTST);
5015 if (InsertPos)
5016 DeducedTemplateSpecializationTypes.InsertNode(DTST, InsertPos);
5017 return QualType(DTST, 0);
5018}
5019
5020/// getAtomicType - Return the uniqued reference to the atomic type for
5021/// the given value type.
5022QualType ASTContext::getAtomicType(QualType T) const {
5023 // Unique pointers, to guarantee there is only one pointer of a particular
5024 // structure.
5025 llvm::FoldingSetNodeID ID;
5026 AtomicType::Profile(ID, T);
5027
5028 void *InsertPos = nullptr;
5029 if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos))
5030 return QualType(AT, 0);
5031
5032 // If the atomic value type isn't canonical, this won't be a canonical type
5033 // either, so fill in the canonical type field.
5034 QualType Canonical;
5035 if (!T.isCanonical()) {
5036 Canonical = getAtomicType(getCanonicalType(T));
5037
5038 // Get the new insert position for the node we care about.
5039 AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos);
5040 assert(!NewIP && "Shouldn't be in the map!")((!NewIP && "Shouldn't be in the map!") ? static_cast
<void> (0) : __assert_fail ("!NewIP && \"Shouldn't be in the map!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5040, __PRETTY_FUNCTION__))
; (void)NewIP;
5041 }
5042 auto *New = new (*this, TypeAlignment) AtomicType(T, Canonical);
5043 Types.push_back(New);
5044 AtomicTypes.InsertNode(New, InsertPos);
5045 return QualType(New, 0);
5046}
5047
5048/// getAutoDeductType - Get type pattern for deducing against 'auto'.
5049QualType ASTContext::getAutoDeductType() const {
5050 if (AutoDeductTy.isNull())
5051 AutoDeductTy = QualType(
5052 new (*this, TypeAlignment) AutoType(QualType(), AutoTypeKeyword::Auto,
5053 /*dependent*/false, /*pack*/false),
5054 0);
5055 return AutoDeductTy;
5056}
5057
5058/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
5059QualType ASTContext::getAutoRRefDeductType() const {
5060 if (AutoRRefDeductTy.isNull())
5061 AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
5062 assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern")((!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"
) ? static_cast<void> (0) : __assert_fail ("!AutoRRefDeductTy.isNull() && \"can't build 'auto &&' pattern\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5062, __PRETTY_FUNCTION__))
;
5063 return AutoRRefDeductTy;
5064}
5065
5066/// getTagDeclType - Return the unique reference to the type for the
5067/// specified TagDecl (struct/union/class/enum) decl.
5068QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
5069 assert(Decl)((Decl) ? static_cast<void> (0) : __assert_fail ("Decl"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5069, __PRETTY_FUNCTION__))
;
5070 // FIXME: What is the design on getTagDeclType when it requires casting
5071 // away const? mutable?
5072 return getTypeDeclType(const_cast<TagDecl*>(Decl));
5073}
5074
5075/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
5076/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
5077/// needs to agree with the definition in <stddef.h>.
5078CanQualType ASTContext::getSizeType() const {
5079 return getFromTargetType(Target->getSizeType());
5080}
5081
5082/// Return the unique signed counterpart of the integer type
5083/// corresponding to size_t.
5084CanQualType ASTContext::getSignedSizeType() const {
5085 return getFromTargetType(Target->getSignedSizeType());
5086}
5087
5088/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
5089CanQualType ASTContext::getIntMaxType() const {
5090 return getFromTargetType(Target->getIntMaxType());
5091}
5092
5093/// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5).
5094CanQualType ASTContext::getUIntMaxType() const {
5095 return getFromTargetType(Target->getUIntMaxType());
5096}
5097
5098/// getSignedWCharType - Return the type of "signed wchar_t".
5099/// Used when in C++, as a GCC extension.
5100QualType ASTContext::getSignedWCharType() const {
5101 // FIXME: derive from "Target" ?
5102 return WCharTy;
5103}
5104
5105/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
5106/// Used when in C++, as a GCC extension.
5107QualType ASTContext::getUnsignedWCharType() const {
5108 // FIXME: derive from "Target" ?
5109 return UnsignedIntTy;
5110}
5111
5112QualType ASTContext::getIntPtrType() const {
5113 return getFromTargetType(Target->getIntPtrType());
5114}
5115
5116QualType ASTContext::getUIntPtrType() const {
5117 return getCorrespondingUnsignedType(getIntPtrType());
5118}
5119
5120/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
5121/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
5122QualType ASTContext::getPointerDiffType() const {
5123 return getFromTargetType(Target->getPtrDiffType(0));
5124}
5125
5126/// Return the unique unsigned counterpart of "ptrdiff_t"
5127/// integer type. The standard (C11 7.21.6.1p7) refers to this type
5128/// in the definition of %tu format specifier.
5129QualType ASTContext::getUnsignedPointerDiffType() const {
5130 return getFromTargetType(Target->getUnsignedPtrDiffType(0));
5131}
5132
5133/// Return the unique type for "pid_t" defined in
5134/// <sys/types.h>. We need this to compute the correct type for vfork().
5135QualType ASTContext::getProcessIDType() const {
5136 return getFromTargetType(Target->getProcessIDType());
5137}
5138
5139//===----------------------------------------------------------------------===//
5140// Type Operators
5141//===----------------------------------------------------------------------===//
5142
5143CanQualType ASTContext::getCanonicalParamType(QualType T) const {
5144 // Push qualifiers into arrays, and then discard any remaining
5145 // qualifiers.
5146 T = getCanonicalType(T);
5147 T = getVariableArrayDecayedType(T);
5148 const Type *Ty = T.getTypePtr();
5149 QualType Result;
5150 if (isa<ArrayType>(Ty)) {
5151 Result = getArrayDecayedType(QualType(Ty,0));
5152 } else if (isa<FunctionType>(Ty)) {
5153 Result = getPointerType(QualType(Ty, 0));
5154 } else {
5155 Result = QualType(Ty, 0);
5156 }
5157
5158 return CanQualType::CreateUnsafe(Result);
5159}
5160
5161QualType ASTContext::getUnqualifiedArrayType(QualType type,
5162 Qualifiers &quals) {
5163 SplitQualType splitType = type.getSplitUnqualifiedType();
5164
5165 // FIXME: getSplitUnqualifiedType() actually walks all the way to
5166 // the unqualified desugared type and then drops it on the floor.
5167 // We then have to strip that sugar back off with
5168 // getUnqualifiedDesugaredType(), which is silly.
5169 const auto *AT =
5170 dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType());
5171
5172 // If we don't have an array, just use the results in splitType.
5173 if (!AT) {
5174 quals = splitType.Quals;
5175 return QualType(splitType.Ty, 0);
5176 }
5177
5178 // Otherwise, recurse on the array's element type.
5179 QualType elementType = AT->getElementType();
5180 QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
5181
5182 // If that didn't change the element type, AT has no qualifiers, so we
5183 // can just use the results in splitType.
5184 if (elementType == unqualElementType) {
5185 assert(quals.empty())((quals.empty()) ? static_cast<void> (0) : __assert_fail
("quals.empty()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5185, __PRETTY_FUNCTION__))
; // from the recursive call
5186 quals = splitType.Quals;
5187 return QualType(splitType.Ty, 0);
5188 }
5189
5190 // Otherwise, add in the qualifiers from the outermost type, then
5191 // build the type back up.
5192 quals.addConsistentQualifiers(splitType.Quals);
5193
5194 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
5195 return getConstantArrayType(unqualElementType, CAT->getSize(),
5196 CAT->getSizeModifier(), 0);
5197 }
5198
5199 if (const auto *IAT = dyn_cast<IncompleteArrayType>(AT)) {
5200 return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
5201 }
5202
5203 if (const auto *VAT = dyn_cast<VariableArrayType>(AT)) {
5204 return getVariableArrayType(unqualElementType,
5205 VAT->getSizeExpr(),
5206 VAT->getSizeModifier(),
5207 VAT->getIndexTypeCVRQualifiers(),
5208 VAT->getBracketsRange());
5209 }
5210
5211 const auto *DSAT = cast<DependentSizedArrayType>(AT);
5212 return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
5213 DSAT->getSizeModifier(), 0,
5214 SourceRange());
5215}
5216
5217/// Attempt to unwrap two types that may both be array types with the same bound
5218/// (or both be array types of unknown bound) for the purpose of comparing the
5219/// cv-decomposition of two types per C++ [conv.qual].
5220bool ASTContext::UnwrapSimilarArrayTypes(QualType &T1, QualType &T2) {
5221 bool UnwrappedAny = false;
5222 while (true) {
5223 auto *AT1 = getAsArrayType(T1);
5224 if (!AT1) return UnwrappedAny;
5225
5226 auto *AT2 = getAsArrayType(T2);
5227 if (!AT2) return UnwrappedAny;
5228
5229 // If we don't have two array types with the same constant bound nor two
5230 // incomplete array types, we've unwrapped everything we can.
5231 if (auto *CAT1 = dyn_cast<ConstantArrayType>(AT1)) {
5232 auto *CAT2 = dyn_cast<ConstantArrayType>(AT2);
5233 if (!CAT2 || CAT1->getSize() != CAT2->getSize())
5234 return UnwrappedAny;
5235 } else if (!isa<IncompleteArrayType>(AT1) ||
5236 !isa<IncompleteArrayType>(AT2)) {
5237 return UnwrappedAny;
5238 }
5239
5240 T1 = AT1->getElementType();
5241 T2 = AT2->getElementType();
5242 UnwrappedAny = true;
5243 }
5244}
5245
5246/// Attempt to unwrap two types that may be similar (C++ [conv.qual]).
5247///
5248/// If T1 and T2 are both pointer types of the same kind, or both array types
5249/// with the same bound, unwraps layers from T1 and T2 until a pointer type is
5250/// unwrapped. Top-level qualifiers on T1 and T2 are ignored.
5251///
5252/// This function will typically be called in a loop that successively
5253/// "unwraps" pointer and pointer-to-member types to compare them at each
5254/// level.
5255///
5256/// \return \c true if a pointer type was unwrapped, \c false if we reached a
5257/// pair of types that can't be unwrapped further.
5258bool ASTContext::UnwrapSimilarTypes(QualType &T1, QualType &T2) {
5259 UnwrapSimilarArrayTypes(T1, T2);
5260
5261 const auto *T1PtrType = T1->getAs<PointerType>();
5262 const auto *T2PtrType = T2->getAs<PointerType>();
5263 if (T1PtrType && T2PtrType) {
5264 T1 = T1PtrType->getPointeeType();
5265 T2 = T2PtrType->getPointeeType();
5266 return true;
5267 }
5268
5269 const auto *T1MPType = T1->getAs<MemberPointerType>();
5270 const auto *T2MPType = T2->getAs<MemberPointerType>();
5271 if (T1MPType && T2MPType &&
5272 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
5273 QualType(T2MPType->getClass(), 0))) {
5274 T1 = T1MPType->getPointeeType();
5275 T2 = T2MPType->getPointeeType();
5276 return true;
5277 }
5278
5279 if (getLangOpts().ObjC) {
5280 const auto *T1OPType = T1->getAs<ObjCObjectPointerType>();
5281 const auto *T2OPType = T2->getAs<ObjCObjectPointerType>();
5282 if (T1OPType && T2OPType) {
5283 T1 = T1OPType->getPointeeType();
5284 T2 = T2OPType->getPointeeType();
5285 return true;
5286 }
5287 }
5288
5289 // FIXME: Block pointers, too?
5290
5291 return false;
5292}
5293
5294bool ASTContext::hasSimilarType(QualType T1, QualType T2) {
5295 while (true) {
5296 Qualifiers Quals;
5297 T1 = getUnqualifiedArrayType(T1, Quals);
5298 T2 = getUnqualifiedArrayType(T2, Quals);
5299 if (hasSameType(T1, T2))
5300 return true;
5301 if (!UnwrapSimilarTypes(T1, T2))
5302 return false;
5303 }
5304}
5305
5306bool ASTContext::hasCvrSimilarType(QualType T1, QualType T2) {
5307 while (true) {
5308 Qualifiers Quals1, Quals2;
5309 T1 = getUnqualifiedArrayType(T1, Quals1);
5310 T2 = getUnqualifiedArrayType(T2, Quals2);
5311
5312 Quals1.removeCVRQualifiers();
5313 Quals2.removeCVRQualifiers();
5314 if (Quals1 != Quals2)
5315 return false;
5316
5317 if (hasSameType(T1, T2))
5318 return true;
5319
5320 if (!UnwrapSimilarTypes(T1, T2))
5321 return false;
5322 }
5323}
5324
5325DeclarationNameInfo
5326ASTContext::getNameForTemplate(TemplateName Name,
5327 SourceLocation NameLoc) const {
5328 switch (Name.getKind()) {
5329 case TemplateName::QualifiedTemplate:
5330 case TemplateName::Template:
5331 // DNInfo work in progress: CHECKME: what about DNLoc?
5332 return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(),
5333 NameLoc);
5334
5335 case TemplateName::OverloadedTemplate: {
5336 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
5337 // DNInfo work in progress: CHECKME: what about DNLoc?
5338 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
5339 }
5340
5341 case TemplateName::AssumedTemplate: {
5342 AssumedTemplateStorage *Storage = Name.getAsAssumedTemplateName();
5343 return DeclarationNameInfo(Storage->getDeclName(), NameLoc);
5344 }
5345
5346 case TemplateName::DependentTemplate: {
5347 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
5348 DeclarationName DName;
5349 if (DTN->isIdentifier()) {
5350 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
5351 return DeclarationNameInfo(DName, NameLoc);
5352 } else {
5353 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
5354 // DNInfo work in progress: FIXME: source locations?
5355 DeclarationNameLoc DNLoc;
5356 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
5357 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
5358 return DeclarationNameInfo(DName, NameLoc, DNLoc);
5359 }
5360 }
5361
5362 case TemplateName::SubstTemplateTemplateParm: {
5363 SubstTemplateTemplateParmStorage *subst
5364 = Name.getAsSubstTemplateTemplateParm();
5365 return DeclarationNameInfo(subst->getParameter()->getDeclName(),
5366 NameLoc);
5367 }
5368
5369 case TemplateName::SubstTemplateTemplateParmPack: {
5370 SubstTemplateTemplateParmPackStorage *subst
5371 = Name.getAsSubstTemplateTemplateParmPack();
5372 return DeclarationNameInfo(subst->getParameterPack()->getDeclName(),
5373 NameLoc);
5374 }
5375 }
5376
5377 llvm_unreachable("bad template name kind!")::llvm::llvm_unreachable_internal("bad template name kind!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5377)
;
5378}
5379
5380TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
5381 switch (Name.getKind()) {
5382 case TemplateName::QualifiedTemplate:
5383 case TemplateName::Template: {
5384 TemplateDecl *Template = Name.getAsTemplateDecl();
5385 if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Template))
5386 Template = getCanonicalTemplateTemplateParmDecl(TTP);
5387
5388 // The canonical template name is the canonical template declaration.
5389 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
5390 }
5391
5392 case TemplateName::OverloadedTemplate:
5393 case TemplateName::AssumedTemplate:
5394 llvm_unreachable("cannot canonicalize unresolved template")::llvm::llvm_unreachable_internal("cannot canonicalize unresolved template"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5394)
;
5395
5396 case TemplateName::DependentTemplate: {
5397 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
5398 assert(DTN && "Non-dependent template names must refer to template decls.")((DTN && "Non-dependent template names must refer to template decls."
) ? static_cast<void> (0) : __assert_fail ("DTN && \"Non-dependent template names must refer to template decls.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5398, __PRETTY_FUNCTION__))
;
5399 return DTN->CanonicalTemplateName;
5400 }
5401
5402 case TemplateName::SubstTemplateTemplateParm: {
5403 SubstTemplateTemplateParmStorage *subst
5404 = Name.getAsSubstTemplateTemplateParm();
5405 return getCanonicalTemplateName(subst->getReplacement());
5406 }
5407
5408 case TemplateName::SubstTemplateTemplateParmPack: {
5409 SubstTemplateTemplateParmPackStorage *subst
5410 = Name.getAsSubstTemplateTemplateParmPack();
5411 TemplateTemplateParmDecl *canonParameter
5412 = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack());
5413 TemplateArgument canonArgPack
5414 = getCanonicalTemplateArgument(subst->getArgumentPack());
5415 return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack);
5416 }
5417 }
5418
5419 llvm_unreachable("bad template name!")::llvm::llvm_unreachable_internal("bad template name!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5419)
;
5420}
5421
5422bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
5423 X = getCanonicalTemplateName(X);
5424 Y = getCanonicalTemplateName(Y);
5425 return X.getAsVoidPointer() == Y.getAsVoidPointer();
5426}
5427
5428TemplateArgument
5429ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
5430 switch (Arg.getKind()) {
5431 case TemplateArgument::Null:
5432 return Arg;
5433
5434 case TemplateArgument::Expression:
5435 return Arg;
5436
5437 case TemplateArgument::Declaration: {
5438 auto *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl());
5439 return TemplateArgument(D, Arg.getParamTypeForDecl());
5440 }
5441
5442 case TemplateArgument::NullPtr:
5443 return TemplateArgument(getCanonicalType(Arg.getNullPtrType()),
5444 /*isNullPtr*/true);
5445
5446 case TemplateArgument::Template:
5447 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
5448
5449 case TemplateArgument::TemplateExpansion:
5450 return TemplateArgument(getCanonicalTemplateName(
5451 Arg.getAsTemplateOrTemplatePattern()),
5452 Arg.getNumTemplateExpansions());
5453
5454 case TemplateArgument::Integral:
5455 return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType()));
5456
5457 case TemplateArgument::Type:
5458 return TemplateArgument(getCanonicalType(Arg.getAsType()));
5459
5460 case TemplateArgument::Pack: {
5461 if (Arg.pack_size() == 0)
5462 return Arg;
5463
5464 auto *CanonArgs = new (*this) TemplateArgument[Arg.pack_size()];
5465 unsigned Idx = 0;
5466 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
5467 AEnd = Arg.pack_end();
5468 A != AEnd; (void)++A, ++Idx)
5469 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
5470
5471 return TemplateArgument(llvm::makeArrayRef(CanonArgs, Arg.pack_size()));
5472 }
5473 }
5474
5475 // Silence GCC warning
5476 llvm_unreachable("Unhandled template argument kind")::llvm::llvm_unreachable_internal("Unhandled template argument kind"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5476)
;
5477}
5478
5479NestedNameSpecifier *
5480ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
5481 if (!NNS)
5482 return nullptr;
5483
5484 switch (NNS->getKind()) {
5485 case NestedNameSpecifier::Identifier:
5486 // Canonicalize the prefix but keep the identifier the same.
5487 return NestedNameSpecifier::Create(*this,
5488 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
5489 NNS->getAsIdentifier());
5490
5491 case NestedNameSpecifier::Namespace:
5492 // A namespace is canonical; build a nested-name-specifier with
5493 // this namespace and no prefix.
5494 return NestedNameSpecifier::Create(*this, nullptr,
5495 NNS->getAsNamespace()->getOriginalNamespace());
5496
5497 case NestedNameSpecifier::NamespaceAlias:
5498 // A namespace is canonical; build a nested-name-specifier with
5499 // this namespace and no prefix.
5500 return NestedNameSpecifier::Create(*this, nullptr,
5501 NNS->getAsNamespaceAlias()->getNamespace()
5502 ->getOriginalNamespace());
5503
5504 case NestedNameSpecifier::TypeSpec:
5505 case NestedNameSpecifier::TypeSpecWithTemplate: {
5506 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
5507
5508 // If we have some kind of dependent-named type (e.g., "typename T::type"),
5509 // break it apart into its prefix and identifier, then reconsititute those
5510 // as the canonical nested-name-specifier. This is required to canonicalize
5511 // a dependent nested-name-specifier involving typedefs of dependent-name
5512 // types, e.g.,
5513 // typedef typename T::type T1;
5514 // typedef typename T1::type T2;
5515 if (const auto *DNT = T->getAs<DependentNameType>())
5516 return NestedNameSpecifier::Create(*this, DNT->getQualifier(),
5517 const_cast<IdentifierInfo *>(DNT->getIdentifier()));
5518
5519 // Otherwise, just canonicalize the type, and force it to be a TypeSpec.
5520 // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the
5521 // first place?
5522 return NestedNameSpecifier::Create(*this, nullptr, false,
5523 const_cast<Type *>(T.getTypePtr()));
5524 }
5525
5526 case NestedNameSpecifier::Global:
5527 case NestedNameSpecifier::Super:
5528 // The global specifier and __super specifer are canonical and unique.
5529 return NNS;
5530 }
5531
5532 llvm_unreachable("Invalid NestedNameSpecifier::Kind!")::llvm::llvm_unreachable_internal("Invalid NestedNameSpecifier::Kind!"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5532)
;
5533}
5534
5535const ArrayType *ASTContext::getAsArrayType(QualType T) const {
5536 // Handle the non-qualified case efficiently.
5537 if (!T.hasLocalQualifiers()) {
5538 // Handle the common positive case fast.
5539 if (const auto *AT = dyn_cast<ArrayType>(T))
5540 return AT;
5541 }
5542
5543 // Handle the common negative case fast.
5544 if (!isa<ArrayType>(T.getCanonicalType()))
5545 return nullptr;
5546
5547 // Apply any qualifiers from the array type to the element type. This
5548 // implements C99 6.7.3p8: "If the specification of an array type includes
5549 // any type qualifiers, the element type is so qualified, not the array type."
5550
5551 // If we get here, we either have type qualifiers on the type, or we have
5552 // sugar such as a typedef in the way. If we have type qualifiers on the type
5553 // we must propagate them down into the element type.
5554
5555 SplitQualType split = T.getSplitDesugaredType();
5556 Qualifiers qs = split.Quals;
5557
5558 // If we have a simple case, just return now.
5559 const auto *ATy = dyn_cast<ArrayType>(split.Ty);
5560 if (!ATy || qs.empty())
5561 return ATy;
5562
5563 // Otherwise, we have an array and we have qualifiers on it. Push the
5564 // qualifiers into the array element type and return a new array type.
5565 QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
5566
5567 if (const auto *CAT = dyn_cast<ConstantArrayType>(ATy))
5568 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
5569 CAT->getSizeModifier(),
5570 CAT->getIndexTypeCVRQualifiers()));
5571 if (const auto *IAT = dyn_cast<IncompleteArrayType>(ATy))
5572 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
5573 IAT->getSizeModifier(),
5574 IAT->getIndexTypeCVRQualifiers()));
5575
5576 if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(ATy))
5577 return cast<ArrayType>(
5578 getDependentSizedArrayType(NewEltTy,
5579 DSAT->getSizeExpr(),
5580 DSAT->getSizeModifier(),
5581 DSAT->getIndexTypeCVRQualifiers(),
5582 DSAT->getBracketsRange()));
5583
5584 const auto *VAT = cast<VariableArrayType>(ATy);
5585 return cast<ArrayType>(getVariableArrayType(NewEltTy,
5586 VAT->getSizeExpr(),
5587 VAT->getSizeModifier(),
5588 VAT->getIndexTypeCVRQualifiers(),
5589 VAT->getBracketsRange()));
5590}
5591
5592QualType ASTContext::getAdjustedParameterType(QualType T) const {
5593 if (T->isArrayType() || T->isFunctionType())
5594 return getDecayedType(T);
5595 return T;
5596}
5597
5598QualType ASTContext::getSignatureParameterType(QualType T) const {
5599 T = getVariableArrayDecayedType(T);
5600 T = getAdjustedParameterType(T);
5601 return T.getUnqualifiedType();
5602}
5603
5604QualType ASTContext::getExceptionObjectType(QualType T) const {
5605 // C++ [except.throw]p3:
5606 // A throw-expression initializes a temporary object, called the exception
5607 // object, the type of which is determined by removing any top-level
5608 // cv-qualifiers from the static type of the operand of throw and adjusting
5609 // the type from "array of T" or "function returning T" to "pointer to T"
5610 // or "pointer to function returning T", [...]
5611 T = getVariableArrayDecayedType(T);
5612 if (T->isArrayType() || T->isFunctionType())
5613 T = getDecayedType(T);
5614 return T.getUnqualifiedType();
5615}
5616
5617/// getArrayDecayedType - Return the properly qualified result of decaying the
5618/// specified array type to a pointer. This operation is non-trivial when
5619/// handling typedefs etc. The canonical type of "T" must be an array type,
5620/// this returns a pointer to a properly qualified element of the array.
5621///
5622/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
5623QualType ASTContext::getArrayDecayedType(QualType Ty) const {
5624 // Get the element type with 'getAsArrayType' so that we don't lose any
5625 // typedefs in the element type of the array. This also handles propagation
5626 // of type qualifiers from the array type into the element type if present
5627 // (C99 6.7.3p8).
5628 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
5629 assert(PrettyArrayType && "Not an array type!")((PrettyArrayType && "Not an array type!") ? static_cast
<void> (0) : __assert_fail ("PrettyArrayType && \"Not an array type!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5629, __PRETTY_FUNCTION__))
;
5630
5631 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
5632
5633 // int x[restrict 4] -> int *restrict
5634 QualType Result = getQualifiedType(PtrTy,
5635 PrettyArrayType->getIndexTypeQualifiers());
5636
5637 // int x[_Nullable] -> int * _Nullable
5638 if (auto Nullability = Ty->getNullability(*this)) {
5639 Result = const_cast<ASTContext *>(this)->getAttributedType(
5640 AttributedType::getNullabilityAttrKind(*Nullability), Result, Result);
5641 }
5642 return Result;
5643}
5644
5645QualType ASTContext::getBaseElementType(const ArrayType *array) const {
5646 return getBaseElementType(array->getElementType());
5647}
5648
5649QualType ASTContext::getBaseElementType(QualType type) const {
5650 Qualifiers qs;
5651 while (true) {
5652 SplitQualType split = type.getSplitDesugaredType();
5653 const ArrayType *array = split.Ty->getAsArrayTypeUnsafe();
5654 if (!array) break;
5655
5656 type = array->getElementType();
5657 qs.addConsistentQualifiers(split.Quals);
5658 }
5659
5660 return getQualifiedType(type, qs);
5661}
5662
5663/// getConstantArrayElementCount - Returns number of constant array elements.
5664uint64_t
5665ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
5666 uint64_t ElementCount = 1;
5667 do {
5668 ElementCount *= CA->getSize().getZExtValue();
5669 CA = dyn_cast_or_null<ConstantArrayType>(
5670 CA->getElementType()->getAsArrayTypeUnsafe());
5671 } while (CA);
5672 return ElementCount;
5673}
5674
5675/// getFloatingRank - Return a relative rank for floating point types.
5676/// This routine will assert if passed a built-in type that isn't a float.
5677static FloatingRank getFloatingRank(QualType T) {
5678 if (const auto *CT = T->getAs<ComplexType>())
5679 return getFloatingRank(CT->getElementType());
5680
5681 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type")((T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"
) ? static_cast<void> (0) : __assert_fail ("T->getAs<BuiltinType>() && \"getFloatingRank(): not a floating type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5681, __PRETTY_FUNCTION__))
;
5682 switch (T->castAs<BuiltinType>()->getKind()) {
5683 default: llvm_unreachable("getFloatingRank(): not a floating type")::llvm::llvm_unreachable_internal("getFloatingRank(): not a floating type"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5683)
;
5684 case BuiltinType::Float16: return Float16Rank;
5685 case BuiltinType::Half: return HalfRank;
5686 case BuiltinType::Float: return FloatRank;
5687 case BuiltinType::Double: return DoubleRank;
5688 case BuiltinType::LongDouble: return LongDoubleRank;
5689 case BuiltinType::Float128: return Float128Rank;
5690 }
5691}
5692
5693/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
5694/// point or a complex type (based on typeDomain/typeSize).
5695/// 'typeDomain' is a real floating point or complex type.
5696/// 'typeSize' is a real floating point or complex type.
5697QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
5698 QualType Domain) const {
5699 FloatingRank EltRank = getFloatingRank(Size);
5700 if (Domain->isComplexType()) {
5701 switch (EltRank) {
5702 case Float16Rank:
5703 case HalfRank: llvm_unreachable("Complex half is not supported")::llvm::llvm_unreachable_internal("Complex half is not supported"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5703)
;
5704 case FloatRank: return FloatComplexTy;
5705 case DoubleRank: return DoubleComplexTy;
5706 case LongDoubleRank: return LongDoubleComplexTy;
5707 case Float128Rank: return Float128ComplexTy;
5708 }
5709 }
5710
5711 assert(Domain->isRealFloatingType() && "Unknown domain!")((Domain->isRealFloatingType() && "Unknown domain!"
) ? static_cast<void> (0) : __assert_fail ("Domain->isRealFloatingType() && \"Unknown domain!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5711, __PRETTY_FUNCTION__))
;
5712 switch (EltRank) {
5713 case Float16Rank: return HalfTy;
5714 case HalfRank: return HalfTy;
5715 case FloatRank: return FloatTy;
5716 case DoubleRank: return DoubleTy;
5717 case LongDoubleRank: return LongDoubleTy;
5718 case Float128Rank: return Float128Ty;
5719 }
5720 llvm_unreachable("getFloatingRank(): illegal value for rank")::llvm::llvm_unreachable_internal("getFloatingRank(): illegal value for rank"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5720)
;
5721}
5722
5723/// getFloatingTypeOrder - Compare the rank of the two specified floating
5724/// point types, ignoring the domain of the type (i.e. 'double' ==
5725/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
5726/// LHS < RHS, return -1.
5727int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
5728 FloatingRank LHSR = getFloatingRank(LHS);
5729 FloatingRank RHSR = getFloatingRank(RHS);
5730
5731 if (LHSR == RHSR)
5732 return 0;
5733 if (LHSR > RHSR)
5734 return 1;
5735 return -1;
5736}
5737
5738int ASTContext::getFloatingTypeSemanticOrder(QualType LHS, QualType RHS) const {
5739 if (&getFloatTypeSemantics(LHS) == &getFloatTypeSemantics(RHS))
5740 return 0;
5741 return getFloatingTypeOrder(LHS, RHS);
5742}
5743
5744/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
5745/// routine will assert if passed a built-in type that isn't an integer or enum,
5746/// or if it is not canonicalized.
5747unsigned ASTContext::getIntegerRank(const Type *T) const {
5748 assert(T->isCanonicalUnqualified() && "T should be canonicalized")((T->isCanonicalUnqualified() && "T should be canonicalized"
) ? static_cast<void> (0) : __assert_fail ("T->isCanonicalUnqualified() && \"T should be canonicalized\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5748, __PRETTY_FUNCTION__))
;
5749
5750 switch (cast<BuiltinType>(T)->getKind()) {
5751 default: llvm_unreachable("getIntegerRank(): not a built-in integer")::llvm::llvm_unreachable_internal("getIntegerRank(): not a built-in integer"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5751)
;
5752 case BuiltinType::Bool:
5753 return 1 + (getIntWidth(BoolTy) << 3);
5754 case BuiltinType::Char_S:
5755 case BuiltinType::Char_U:
5756 case BuiltinType::SChar:
5757 case BuiltinType::UChar:
5758 return 2 + (getIntWidth(CharTy) << 3);
5759 case BuiltinType::Short:
5760 case BuiltinType::UShort:
5761 return 3 + (getIntWidth(ShortTy) << 3);
5762 case BuiltinType::Int:
5763 case BuiltinType::UInt:
5764 return 4 + (getIntWidth(IntTy) << 3);
5765 case BuiltinType::Long:
5766 case BuiltinType::ULong:
5767 return 5 + (getIntWidth(LongTy) << 3);
5768 case BuiltinType::LongLong:
5769 case BuiltinType::ULongLong:
5770 return 6 + (getIntWidth(LongLongTy) << 3);
5771 case BuiltinType::Int128:
5772 case BuiltinType::UInt128:
5773 return 7 + (getIntWidth(Int128Ty) << 3);
5774 }
5775}
5776
5777/// Whether this is a promotable bitfield reference according
5778/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
5779///
5780/// \returns the type this bit-field will promote to, or NULL if no
5781/// promotion occurs.
5782QualType ASTContext::isPromotableBitField(Expr *E) const {
5783 if (E->isTypeDependent() || E->isValueDependent())
5784 return {};
5785
5786 // C++ [conv.prom]p5:
5787 // If the bit-field has an enumerated type, it is treated as any other
5788 // value of that type for promotion purposes.
5789 if (getLangOpts().CPlusPlus && E->getType()->isEnumeralType())
5790 return {};
5791
5792 // FIXME: We should not do this unless E->refersToBitField() is true. This
5793 // matters in C where getSourceBitField() will find bit-fields for various
5794 // cases where the source expression is not a bit-field designator.
5795
5796 FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields?
5797 if (!Field)
5798 return {};
5799
5800 QualType FT = Field->getType();
5801
5802 uint64_t BitWidth = Field->getBitWidthValue(*this);
5803 uint64_t IntSize = getTypeSize(IntTy);
5804 // C++ [conv.prom]p5:
5805 // A prvalue for an integral bit-field can be converted to a prvalue of type
5806 // int if int can represent all the values of the bit-field; otherwise, it
5807 // can be converted to unsigned int if unsigned int can represent all the
5808 // values of the bit-field. If the bit-field is larger yet, no integral
5809 // promotion applies to it.
5810 // C11 6.3.1.1/2:
5811 // [For a bit-field of type _Bool, int, signed int, or unsigned int:]
5812 // If an int can represent all values of the original type (as restricted by
5813 // the width, for a bit-field), the value is converted to an int; otherwise,
5814 // it is converted to an unsigned int.
5815 //
5816 // FIXME: C does not permit promotion of a 'long : 3' bitfield to int.
5817 // We perform that promotion here to match GCC and C++.
5818 // FIXME: C does not permit promotion of an enum bit-field whose rank is
5819 // greater than that of 'int'. We perform that promotion to match GCC.
5820 if (BitWidth < IntSize)
5821 return IntTy;
5822
5823 if (BitWidth == IntSize)
5824 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
5825
5826 // Bit-fields wider than int are not subject to promotions, and therefore act
5827 // like the base type. GCC has some weird bugs in this area that we
5828 // deliberately do not follow (GCC follows a pre-standard resolution to
5829 // C's DR315 which treats bit-width as being part of the type, and this leaks
5830 // into their semantics in some cases).
5831 return {};
5832}
5833
5834/// getPromotedIntegerType - Returns the type that Promotable will
5835/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
5836/// integer type.
5837QualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
5838 assert(!Promotable.isNull())((!Promotable.isNull()) ? static_cast<void> (0) : __assert_fail
("!Promotable.isNull()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5838, __PRETTY_FUNCTION__))
;
5839 assert(Promotable->isPromotableIntegerType())((Promotable->isPromotableIntegerType()) ? static_cast<
void> (0) : __assert_fail ("Promotable->isPromotableIntegerType()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5839, __PRETTY_FUNCTION__))
;
5840 if (const auto *ET = Promotable->getAs<EnumType>())
5841 return ET->getDecl()->getPromotionType();
5842
5843 if (const auto *BT = Promotable->getAs<BuiltinType>()) {
5844 // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t
5845 // (3.9.1) can be converted to a prvalue of the first of the following
5846 // types that can represent all the values of its underlying type:
5847 // int, unsigned int, long int, unsigned long int, long long int, or
5848 // unsigned long long int [...]
5849 // FIXME: Is there some better way to compute this?
5850 if (BT->getKind() == BuiltinType::WChar_S ||
5851 BT->getKind() == BuiltinType::WChar_U ||
5852 BT->getKind() == BuiltinType::Char8 ||
5853 BT->getKind() == BuiltinType::Char16 ||
5854 BT->getKind() == BuiltinType::Char32) {
5855 bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S;
5856 uint64_t FromSize = getTypeSize(BT);
5857 QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy,
5858 LongLongTy, UnsignedLongLongTy };
5859 for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) {
5860 uint64_t ToSize = getTypeSize(PromoteTypes[Idx]);
5861 if (FromSize < ToSize ||
5862 (FromSize == ToSize &&
5863 FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType()))
5864 return PromoteTypes[Idx];
5865 }
5866 llvm_unreachable("char type should fit into long long")::llvm::llvm_unreachable_internal("char type should fit into long long"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5866)
;
5867 }
5868 }
5869
5870 // At this point, we should have a signed or unsigned integer type.
5871 if (Promotable->isSignedIntegerType())
5872 return IntTy;
5873 uint64_t PromotableSize = getIntWidth(Promotable);
5874 uint64_t IntSize = getIntWidth(IntTy);
5875 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize)((Promotable->isUnsignedIntegerType() && PromotableSize
<= IntSize) ? static_cast<void> (0) : __assert_fail
("Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5875, __PRETTY_FUNCTION__))
;
5876 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
5877}
5878
5879/// Recurses in pointer/array types until it finds an objc retainable
5880/// type and returns its ownership.
5881Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const {
5882 while (!T.isNull()) {
5883 if (T.getObjCLifetime() != Qualifiers::OCL_None)
5884 return T.getObjCLifetime();
5885 if (T->isArrayType())
5886 T = getBaseElementType(T);
5887 else if (const auto *PT = T->getAs<PointerType>())
5888 T = PT->getPointeeType();
5889 else if (const auto *RT = T->getAs<ReferenceType>())
5890 T = RT->getPointeeType();
5891 else
5892 break;
5893 }
5894
5895 return Qualifiers::OCL_None;
5896}
5897
5898static const Type *getIntegerTypeForEnum(const EnumType *ET) {
5899 // Incomplete enum types are not treated as integer types.
5900 // FIXME: In C++, enum types are never integer types.
5901 if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
5902 return ET->getDecl()->getIntegerType().getTypePtr();
5903 return nullptr;
5904}
5905
5906/// getIntegerTypeOrder - Returns the highest ranked integer type:
5907/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
5908/// LHS < RHS, return -1.
5909int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
5910 const Type *LHSC = getCanonicalType(LHS).getTypePtr();
5911 const Type *RHSC = getCanonicalType(RHS).getTypePtr();
5912
5913 // Unwrap enums to their underlying type.
5914 if (const auto *ET = dyn_cast<EnumType>(LHSC))
5915 LHSC = getIntegerTypeForEnum(ET);
5916 if (const auto *ET = dyn_cast<EnumType>(RHSC))
5917 RHSC = getIntegerTypeForEnum(ET);
5918
5919 if (LHSC == RHSC) return 0;
5920
5921 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
5922 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
5923
5924 unsigned LHSRank = getIntegerRank(LHSC);
5925 unsigned RHSRank = getIntegerRank(RHSC);
5926
5927 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
5928 if (LHSRank == RHSRank) return 0;
5929 return LHSRank > RHSRank ? 1 : -1;
5930 }
5931
5932 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
5933 if (LHSUnsigned) {
5934 // If the unsigned [LHS] type is larger, return it.
5935 if (LHSRank >= RHSRank)
5936 return 1;
5937
5938 // If the signed type can represent all values of the unsigned type, it
5939 // wins. Because we are dealing with 2's complement and types that are
5940 // powers of two larger than each other, this is always safe.
5941 return -1;
5942 }
5943
5944 // If the unsigned [RHS] type is larger, return it.
5945 if (RHSRank >= LHSRank)
5946 return -1;
5947
5948 // If the signed type can represent all values of the unsigned type, it
5949 // wins. Because we are dealing with 2's complement and types that are
5950 // powers of two larger than each other, this is always safe.
5951 return 1;
5952}
5953
5954TypedefDecl *ASTContext::getCFConstantStringDecl() const {
5955 if (CFConstantStringTypeDecl)
5956 return CFConstantStringTypeDecl;
5957
5958 assert(!CFConstantStringTagDecl &&((!CFConstantStringTagDecl && "tag and typedef should be initialized together"
) ? static_cast<void> (0) : __assert_fail ("!CFConstantStringTagDecl && \"tag and typedef should be initialized together\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5959, __PRETTY_FUNCTION__))
5959 "tag and typedef should be initialized together")((!CFConstantStringTagDecl && "tag and typedef should be initialized together"
) ? static_cast<void> (0) : __assert_fail ("!CFConstantStringTagDecl && \"tag and typedef should be initialized together\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 5959, __PRETTY_FUNCTION__))
;
5960 CFConstantStringTagDecl = buildImplicitRecord("__NSConstantString_tag");
5961 CFConstantStringTagDecl->startDefinition();
5962
5963 struct {
5964 QualType Type;
5965 const char *Name;
5966 } Fields[5];
5967 unsigned Count = 0;
5968
5969 /// Objective-C ABI
5970 ///
5971 /// typedef struct __NSConstantString_tag {
5972 /// const int *isa;
5973 /// int flags;
5974 /// const char *str;
5975 /// long length;
5976 /// } __NSConstantString;
5977 ///
5978 /// Swift ABI (4.1, 4.2)
5979 ///
5980 /// typedef struct __NSConstantString_tag {
5981 /// uintptr_t _cfisa;
5982 /// uintptr_t _swift_rc;
5983 /// _Atomic(uint64_t) _cfinfoa;
5984 /// const char *_ptr;
5985 /// uint32_t _length;
5986 /// } __NSConstantString;
5987 ///
5988 /// Swift ABI (5.0)
5989 ///
5990 /// typedef struct __NSConstantString_tag {
5991 /// uintptr_t _cfisa;
5992 /// uintptr_t _swift_rc;
5993 /// _Atomic(uint64_t) _cfinfoa;
5994 /// const char *_ptr;
5995 /// uintptr_t _length;
5996 /// } __NSConstantString;
5997
5998 const auto CFRuntime = getLangOpts().CFRuntime;
5999 if (static_cast<unsigned>(CFRuntime) <
6000 static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift)) {
6001 Fields[Count++] = { getPointerType(IntTy.withConst()), "isa" };
6002 Fields[Count++] = { IntTy, "flags" };
6003 Fields[Count++] = { getPointerType(CharTy.withConst()), "str" };
6004 Fields[Count++] = { LongTy, "length" };
6005 } else {
6006 Fields[Count++] = { getUIntPtrType(), "_cfisa" };
6007 Fields[Count++] = { getUIntPtrType(), "_swift_rc" };
6008 Fields[Count++] = { getFromTargetType(Target->getUInt64Type()), "_swift_rc" };
6009 Fields[Count++] = { getPointerType(CharTy.withConst()), "_ptr" };
6010 if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 ||
6011 CFRuntime == LangOptions::CoreFoundationABI::Swift4_2)
6012 Fields[Count++] = { IntTy, "_ptr" };
6013 else
6014 Fields[Count++] = { getUIntPtrType(), "_ptr" };
6015 }
6016
6017 // Create fields
6018 for (unsigned i = 0; i < Count; ++i) {
6019 FieldDecl *Field =
6020 FieldDecl::Create(*this, CFConstantStringTagDecl, SourceLocation(),
6021 SourceLocation(), &Idents.get(Fields[i].Name),
6022 Fields[i].Type, /*TInfo=*/nullptr,
6023 /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit);
6024 Field->setAccess(AS_public);
6025 CFConstantStringTagDecl->addDecl(Field);
6026 }
6027
6028 CFConstantStringTagDecl->completeDefinition();
6029 // This type is designed to be compatible with NSConstantString, but cannot
6030 // use the same name, since NSConstantString is an interface.
6031 auto tagType = getTagDeclType(CFConstantStringTagDecl);
6032 CFConstantStringTypeDecl =
6033 buildImplicitTypedef(tagType, "__NSConstantString");
6034
6035 return CFConstantStringTypeDecl;
6036}
6037
6038RecordDecl *ASTContext::getCFConstantStringTagDecl() const {
6039 if (!CFConstantStringTagDecl)
6040 getCFConstantStringDecl(); // Build the tag and the typedef.
6041 return CFConstantStringTagDecl;
6042}
6043
6044// getCFConstantStringType - Return the type used for constant CFStrings.
6045QualType ASTContext::getCFConstantStringType() const {
6046 return getTypedefType(getCFConstantStringDecl());
6047}
6048
6049QualType ASTContext::getObjCSuperType() const {
6050 if (ObjCSuperType.isNull()) {
6051 RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super");
6052 TUDecl->addDecl(ObjCSuperTypeDecl);
6053 ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl);
6054 }
6055 return ObjCSuperType;
6056}
6057
6058void ASTContext::setCFConstantStringType(QualType T) {
6059 const auto *TD = T->getAs<TypedefType>();
6060 assert(TD && "Invalid CFConstantStringType")((TD && "Invalid CFConstantStringType") ? static_cast
<void> (0) : __assert_fail ("TD && \"Invalid CFConstantStringType\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6060, __PRETTY_FUNCTION__))
;
6061 CFConstantStringTypeDecl = cast<TypedefDecl>(TD->getDecl());
6062 const auto *TagType =
6063 CFConstantStringTypeDecl->getUnderlyingType()->getAs<RecordType>();
6064 assert(TagType && "Invalid CFConstantStringType")((TagType && "Invalid CFConstantStringType") ? static_cast
<void> (0) : __assert_fail ("TagType && \"Invalid CFConstantStringType\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6064, __PRETTY_FUNCTION__))
;
6065 CFConstantStringTagDecl = TagType->getDecl();
6066}
6067
6068QualType ASTContext::getBlockDescriptorType() const {
6069 if (BlockDescriptorType)
6070 return getTagDeclType(BlockDescriptorType);
6071
6072 RecordDecl *RD;
6073 // FIXME: Needs the FlagAppleBlock bit.
6074 RD = buildImplicitRecord("__block_descriptor");
6075 RD->startDefinition();
6076
6077 QualType FieldTypes[] = {
6078 UnsignedLongTy,
6079 UnsignedLongTy,
6080 };
6081
6082 static const char *const FieldNames[] = {
6083 "reserved",
6084 "Size"
6085 };
6086
6087 for (size_t i = 0; i < 2; ++i) {
6088 FieldDecl *Field = FieldDecl::Create(
6089 *this, RD, SourceLocation(), SourceLocation(),
6090 &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr,
6091 /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit);
6092 Field->setAccess(AS_public);
6093 RD->addDecl(Field);
6094 }
6095
6096 RD->completeDefinition();
6097
6098 BlockDescriptorType = RD;
6099
6100 return getTagDeclType(BlockDescriptorType);
6101}
6102
6103QualType ASTContext::getBlockDescriptorExtendedType() const {
6104 if (BlockDescriptorExtendedType)
6105 return getTagDeclType(BlockDescriptorExtendedType);
6106
6107 RecordDecl *RD;
6108 // FIXME: Needs the FlagAppleBlock bit.
6109 RD = buildImplicitRecord("__block_descriptor_withcopydispose");
6110 RD->startDefinition();
6111
6112 QualType FieldTypes[] = {
6113 UnsignedLongTy,
6114 UnsignedLongTy,
6115 getPointerType(VoidPtrTy),
6116 getPointerType(VoidPtrTy)
6117 };
6118
6119 static const char *const FieldNames[] = {
6120 "reserved",
6121 "Size",
6122 "CopyFuncPtr",
6123 "DestroyFuncPtr"
6124 };
6125
6126 for (size_t i = 0; i < 4; ++i) {
6127 FieldDecl *Field = FieldDecl::Create(
6128 *this, RD, SourceLocation(), SourceLocation(),
6129 &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr,
6130 /*BitWidth=*/nullptr,
6131 /*Mutable=*/false, ICIS_NoInit);
6132 Field->setAccess(AS_public);
6133 RD->addDecl(Field);
6134 }
6135
6136 RD->completeDefinition();
6137
6138 BlockDescriptorExtendedType = RD;
6139 return getTagDeclType(BlockDescriptorExtendedType);
6140}
6141
6142TargetInfo::OpenCLTypeKind ASTContext::getOpenCLTypeKind(const Type *T) const {
6143 const auto *BT = dyn_cast<BuiltinType>(T);
6144
6145 if (!BT) {
6146 if (isa<PipeType>(T))
6147 return TargetInfo::OCLTK_Pipe;
6148
6149 return TargetInfo::OCLTK_Default;
6150 }
6151
6152 switch (BT->getKind()) {
6153#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6154 case BuiltinType::Id: \
6155 return TargetInfo::OCLTK_Image;
6156#include "clang/Basic/OpenCLImageTypes.def"
6157
6158 case BuiltinType::OCLClkEvent:
6159 return TargetInfo::OCLTK_ClkEvent;
6160
6161 case BuiltinType::OCLEvent:
6162 return TargetInfo::OCLTK_Event;
6163
6164 case BuiltinType::OCLQueue:
6165 return TargetInfo::OCLTK_Queue;
6166
6167 case BuiltinType::OCLReserveID:
6168 return TargetInfo::OCLTK_ReserveID;
6169
6170 case BuiltinType::OCLSampler:
6171 return TargetInfo::OCLTK_Sampler;
6172
6173 default:
6174 return TargetInfo::OCLTK_Default;
6175 }
6176}
6177
6178LangAS ASTContext::getOpenCLTypeAddrSpace(const Type *T) const {
6179 return Target->getOpenCLTypeAddrSpace(getOpenCLTypeKind(T));
6180}
6181
6182/// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty"
6183/// requires copy/dispose. Note that this must match the logic
6184/// in buildByrefHelpers.
6185bool ASTContext::BlockRequiresCopying(QualType Ty,
6186 const VarDecl *D) {
6187 if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) {
6188 const Expr *copyExpr = getBlockVarCopyInit(D).getCopyExpr();
6189 if (!copyExpr && record->hasTrivialDestructor()) return false;
6190
6191 return true;
6192 }
6193
6194 // The block needs copy/destroy helpers if Ty is non-trivial to destructively
6195 // move or destroy.
6196 if (Ty.isNonTrivialToPrimitiveDestructiveMove() || Ty.isDestructedType())
6197 return true;
6198
6199 if (!Ty->isObjCRetainableType()) return false;
6200
6201 Qualifiers qs = Ty.getQualifiers();
6202
6203 // If we have lifetime, that dominates.
6204 if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
6205 switch (lifetime) {
6206 case Qualifiers::OCL_None: llvm_unreachable("impossible")::llvm::llvm_unreachable_internal("impossible", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6206)
;
6207
6208 // These are just bits as far as the runtime is concerned.
6209 case Qualifiers::OCL_ExplicitNone:
6210 case Qualifiers::OCL_Autoreleasing:
6211 return false;
6212
6213 // These cases should have been taken care of when checking the type's
6214 // non-triviality.
6215 case Qualifiers::OCL_Weak:
6216 case Qualifiers::OCL_Strong:
6217 llvm_unreachable("impossible")::llvm::llvm_unreachable_internal("impossible", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6217)
;
6218 }
6219 llvm_unreachable("fell out of lifetime switch!")::llvm::llvm_unreachable_internal("fell out of lifetime switch!"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6219)
;
6220 }
6221 return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) ||
6222 Ty->isObjCObjectPointerType());
6223}
6224
6225bool ASTContext::getByrefLifetime(QualType Ty,
6226 Qualifiers::ObjCLifetime &LifeTime,
6227 bool &HasByrefExtendedLayout) const {
6228 if (!getLangOpts().ObjC ||
6229 getLangOpts().getGC() != LangOptions::NonGC)
6230 return false;
6231
6232 HasByrefExtendedLayout = false;
6233 if (Ty->isRecordType()) {
6234 HasByrefExtendedLayout = true;
6235 LifeTime = Qualifiers::OCL_None;
6236 } else if ((LifeTime = Ty.getObjCLifetime())) {
6237 // Honor the ARC qualifiers.
6238 } else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) {
6239 // The MRR rule.
6240 LifeTime = Qualifiers::OCL_ExplicitNone;
6241 } else {
6242 LifeTime = Qualifiers::OCL_None;
6243 }
6244 return true;
6245}
6246
6247TypedefDecl *ASTContext::getObjCInstanceTypeDecl() {
6248 if (!ObjCInstanceTypeDecl)
6249 ObjCInstanceTypeDecl =
6250 buildImplicitTypedef(getObjCIdType(), "instancetype");
6251 return ObjCInstanceTypeDecl;
6252}
6253
6254// This returns true if a type has been typedefed to BOOL:
6255// typedef <type> BOOL;
6256static bool isTypeTypedefedAsBOOL(QualType T) {
6257 if (const auto *TT = dyn_cast<TypedefType>(T))
6258 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
6259 return II->isStr("BOOL");
6260
6261 return false;
6262}
6263
6264/// getObjCEncodingTypeSize returns size of type for objective-c encoding
6265/// purpose.
6266CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
6267 if (!type->isIncompleteArrayType() && type->isIncompleteType())
6268 return CharUnits::Zero();
6269
6270 CharUnits sz = getTypeSizeInChars(type);
6271
6272 // Make all integer and enum types at least as large as an int
6273 if (sz.isPositive() && type->isIntegralOrEnumerationType())
6274 sz = std::max(sz, getTypeSizeInChars(IntTy));
6275 // Treat arrays as pointers, since that's how they're passed in.
6276 else if (type->isArrayType())
6277 sz = getTypeSizeInChars(VoidPtrTy);
6278 return sz;
6279}
6280
6281bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const {
6282 return getTargetInfo().getCXXABI().isMicrosoft() &&
6283 VD->isStaticDataMember() &&
6284 VD->getType()->isIntegralOrEnumerationType() &&
6285 !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit();
6286}
6287
6288ASTContext::InlineVariableDefinitionKind
6289ASTContext::getInlineVariableDefinitionKind(const VarDecl *VD) const {
6290 if (!VD->isInline())
6291 return InlineVariableDefinitionKind::None;
6292
6293 // In almost all cases, it's a weak definition.
6294 auto *First = VD->getFirstDecl();
6295 if (First->isInlineSpecified() || !First->isStaticDataMember())
6296 return InlineVariableDefinitionKind::Weak;
6297
6298 // If there's a file-context declaration in this translation unit, it's a
6299 // non-discardable definition.
6300 for (auto *D : VD->redecls())
6301 if (D->getLexicalDeclContext()->isFileContext() &&
6302 !D->isInlineSpecified() && (D->isConstexpr() || First->isConstexpr()))
6303 return InlineVariableDefinitionKind::Strong;
6304
6305 // If we've not seen one yet, we don't know.
6306 return InlineVariableDefinitionKind::WeakUnknown;
6307}
6308
6309static std::string charUnitsToString(const CharUnits &CU) {
6310 return llvm::itostr(CU.getQuantity());
6311}
6312
6313/// getObjCEncodingForBlock - Return the encoded type for this block
6314/// declaration.
6315std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
6316 std::string S;
6317
6318 const BlockDecl *Decl = Expr->getBlockDecl();
6319 QualType BlockTy =
6320 Expr->getType()->castAs<BlockPointerType>()->getPointeeType();
6321 QualType BlockReturnTy = BlockTy->castAs<FunctionType>()->getReturnType();
6322 // Encode result type.
6323 if (getLangOpts().EncodeExtendedBlockSig)
6324 getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, BlockReturnTy, S,
6325 true /*Extended*/);
6326 else
6327 getObjCEncodingForType(BlockReturnTy, S);
6328 // Compute size of all parameters.
6329 // Start with computing size of a pointer in number of bytes.
6330 // FIXME: There might(should) be a better way of doing this computation!
6331 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
6332 CharUnits ParmOffset = PtrSize;
6333 for (auto PI : Decl->parameters()) {
6334 QualType PType = PI->getType();
6335 CharUnits sz = getObjCEncodingTypeSize(PType);
6336 if (sz.isZero())
6337 continue;
6338 assert(sz.isPositive() && "BlockExpr - Incomplete param type")((sz.isPositive() && "BlockExpr - Incomplete param type"
) ? static_cast<void> (0) : __assert_fail ("sz.isPositive() && \"BlockExpr - Incomplete param type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6338, __PRETTY_FUNCTION__))
;
6339 ParmOffset += sz;
6340 }
6341 // Size of the argument frame
6342 S += charUnitsToString(ParmOffset);
6343 // Block pointer and offset.
6344 S += "@?0";
6345
6346 // Argument types.
6347 ParmOffset = PtrSize;
6348 for (auto PVDecl : Decl->parameters()) {
6349 QualType PType = PVDecl->getOriginalType();
6350 if (const auto *AT =
6351 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
6352 // Use array's original type only if it has known number of
6353 // elements.
6354 if (!isa<ConstantArrayType>(AT))
6355 PType = PVDecl->getType();
6356 } else if (PType->isFunctionType())
6357 PType = PVDecl->getType();
6358 if (getLangOpts().EncodeExtendedBlockSig)
6359 getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType,
6360 S, true /*Extended*/);
6361 else
6362 getObjCEncodingForType(PType, S);
6363 S += charUnitsToString(ParmOffset);
6364 ParmOffset += getObjCEncodingTypeSize(PType);
6365 }
6366
6367 return S;
6368}
6369
6370std::string
6371ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl) const {
6372 std::string S;
6373 // Encode result type.
6374 getObjCEncodingForType(Decl->getReturnType(), S);
6375 CharUnits ParmOffset;
6376 // Compute size of all parameters.
6377 for (auto PI : Decl->parameters()) {
6378 QualType PType = PI->getType();
6379 CharUnits sz = getObjCEncodingTypeSize(PType);
6380 if (sz.isZero())
6381 continue;
6382
6383 assert(sz.isPositive() &&((sz.isPositive() && "getObjCEncodingForFunctionDecl - Incomplete param type"
) ? static_cast<void> (0) : __assert_fail ("sz.isPositive() && \"getObjCEncodingForFunctionDecl - Incomplete param type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6384, __PRETTY_FUNCTION__))
6384 "getObjCEncodingForFunctionDecl - Incomplete param type")((sz.isPositive() && "getObjCEncodingForFunctionDecl - Incomplete param type"
) ? static_cast<void> (0) : __assert_fail ("sz.isPositive() && \"getObjCEncodingForFunctionDecl - Incomplete param type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6384, __PRETTY_FUNCTION__))
;
6385 ParmOffset += sz;
6386 }
6387 S += charUnitsToString(ParmOffset);
6388 ParmOffset = CharUnits::Zero();
6389
6390 // Argument types.
6391 for (auto PVDecl : Decl->parameters()) {
6392 QualType PType = PVDecl->getOriginalType();
6393 if (const auto *AT =
6394 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
6395 // Use array's original type only if it has known number of
6396 // elements.
6397 if (!isa<ConstantArrayType>(AT))
6398 PType = PVDecl->getType();
6399 } else if (PType->isFunctionType())
6400 PType = PVDecl->getType();
6401 getObjCEncodingForType(PType, S);
6402 S += charUnitsToString(ParmOffset);
6403 ParmOffset += getObjCEncodingTypeSize(PType);
6404 }
6405
6406 return S;
6407}
6408
6409/// getObjCEncodingForMethodParameter - Return the encoded type for a single
6410/// method parameter or return type. If Extended, include class names and
6411/// block object types.
6412void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT,
6413 QualType T, std::string& S,
6414 bool Extended) const {
6415 // Encode type qualifer, 'in', 'inout', etc. for the parameter.
6416 getObjCEncodingForTypeQualifier(QT, S);
6417 // Encode parameter type.
6418 ObjCEncOptions Options = ObjCEncOptions()
6419 .setExpandPointedToStructures()
6420 .setExpandStructures()
6421 .setIsOutermostType();
6422 if (Extended)
6423 Options.setEncodeBlockParameters().setEncodeClassNames();
6424 getObjCEncodingForTypeImpl(T, S, Options, /*Field=*/nullptr);
6425}
6426
6427/// getObjCEncodingForMethodDecl - Return the encoded type for this method
6428/// declaration.
6429std::string ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
6430 bool Extended) const {
6431 // FIXME: This is not very efficient.
6432 // Encode return type.
6433 std::string S;
6434 getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(),
6435 Decl->getReturnType(), S, Extended);
6436 // Compute size of all parameters.
6437 // Start with computing size of a pointer in number of bytes.
6438 // FIXME: There might(should) be a better way of doing this computation!
6439 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
6440 // The first two arguments (self and _cmd) are pointers; account for
6441 // their size.
6442 CharUnits ParmOffset = 2 * PtrSize;
6443 for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
6444 E = Decl->sel_param_end(); PI != E; ++PI) {
6445 QualType PType = (*PI)->getType();
6446 CharUnits sz = getObjCEncodingTypeSize(PType);
6447 if (sz.isZero())
6448 continue;
6449
6450 assert(sz.isPositive() &&((sz.isPositive() && "getObjCEncodingForMethodDecl - Incomplete param type"
) ? static_cast<void> (0) : __assert_fail ("sz.isPositive() && \"getObjCEncodingForMethodDecl - Incomplete param type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6451, __PRETTY_FUNCTION__))
6451 "getObjCEncodingForMethodDecl - Incomplete param type")((sz.isPositive() && "getObjCEncodingForMethodDecl - Incomplete param type"
) ? static_cast<void> (0) : __assert_fail ("sz.isPositive() && \"getObjCEncodingForMethodDecl - Incomplete param type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6451, __PRETTY_FUNCTION__))
;
6452 ParmOffset += sz;
6453 }
6454 S += charUnitsToString(ParmOffset);
6455 S += "@0:";
6456 S += charUnitsToString(PtrSize);
6457
6458 // Argument types.
6459 ParmOffset = 2 * PtrSize;
6460 for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
6461 E = Decl->sel_param_end(); PI != E; ++PI) {
6462 const ParmVarDecl *PVDecl = *PI;
6463 QualType PType = PVDecl->getOriginalType();
6464 if (const auto *AT =
6465 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
6466 // Use array's original type only if it has known number of
6467 // elements.
6468 if (!isa<ConstantArrayType>(AT))
6469 PType = PVDecl->getType();
6470 } else if (PType->isFunctionType())
6471 PType = PVDecl->getType();
6472 getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(),
6473 PType, S, Extended);
6474 S += charUnitsToString(ParmOffset);
6475 ParmOffset += getObjCEncodingTypeSize(PType);
6476 }
6477
6478 return S;
6479}
6480
6481ObjCPropertyImplDecl *
6482ASTContext::getObjCPropertyImplDeclForPropertyDecl(
6483 const ObjCPropertyDecl *PD,
6484 const Decl *Container) const {
6485 if (!Container)
6486 return nullptr;
6487 if (const auto *CID = dyn_cast<ObjCCategoryImplDecl>(Container)) {
6488 for (auto *PID : CID->property_impls())
6489 if (PID->getPropertyDecl() == PD)
6490 return PID;
6491 } else {
6492 const auto *OID = cast<ObjCImplementationDecl>(Container);
6493 for (auto *PID : OID->property_impls())
6494 if (PID->getPropertyDecl() == PD)
6495 return PID;
6496 }
6497 return nullptr;
6498}
6499
6500/// getObjCEncodingForPropertyDecl - Return the encoded type for this
6501/// property declaration. If non-NULL, Container must be either an
6502/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
6503/// NULL when getting encodings for protocol properties.
6504/// Property attributes are stored as a comma-delimited C string. The simple
6505/// attributes readonly and bycopy are encoded as single characters. The
6506/// parametrized attributes, getter=name, setter=name, and ivar=name, are
6507/// encoded as single characters, followed by an identifier. Property types
6508/// are also encoded as a parametrized attribute. The characters used to encode
6509/// these attributes are defined by the following enumeration:
6510/// @code
6511/// enum PropertyAttributes {
6512/// kPropertyReadOnly = 'R', // property is read-only.
6513/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
6514/// kPropertyByref = '&', // property is a reference to the value last assigned
6515/// kPropertyDynamic = 'D', // property is dynamic
6516/// kPropertyGetter = 'G', // followed by getter selector name
6517/// kPropertySetter = 'S', // followed by setter selector name
6518/// kPropertyInstanceVariable = 'V' // followed by instance variable name
6519/// kPropertyType = 'T' // followed by old-style type encoding.
6520/// kPropertyWeak = 'W' // 'weak' property
6521/// kPropertyStrong = 'P' // property GC'able
6522/// kPropertyNonAtomic = 'N' // property non-atomic
6523/// };
6524/// @endcode
6525std::string
6526ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
6527 const Decl *Container) const {
6528 // Collect information from the property implementation decl(s).
6529 bool Dynamic = false;
6530 ObjCPropertyImplDecl *SynthesizePID = nullptr;
6531
6532 if (ObjCPropertyImplDecl *PropertyImpDecl =
6533 getObjCPropertyImplDeclForPropertyDecl(PD, Container)) {
6534 if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
6535 Dynamic = true;
6536 else
6537 SynthesizePID = PropertyImpDecl;
6538 }
6539
6540 // FIXME: This is not very efficient.
6541 std::string S = "T";
6542
6543 // Encode result type.
6544 // GCC has some special rules regarding encoding of properties which
6545 // closely resembles encoding of ivars.
6546 getObjCEncodingForPropertyType(PD->getType(), S);
6547
6548 if (PD->isReadOnly()) {
6549 S += ",R";
6550 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy)
6551 S += ",C";
6552 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain)
6553 S += ",&";
6554 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
6555 S += ",W";
6556 } else {
6557 switch (PD->getSetterKind()) {
6558 case ObjCPropertyDecl::Assign: break;
6559 case ObjCPropertyDecl::Copy: S += ",C"; break;
6560 case ObjCPropertyDecl::Retain: S += ",&"; break;
6561 case ObjCPropertyDecl::Weak: S += ",W"; break;
6562 }
6563 }
6564
6565 // It really isn't clear at all what this means, since properties
6566 // are "dynamic by default".
6567 if (Dynamic)
6568 S += ",D";
6569
6570 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
6571 S += ",N";
6572
6573 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
6574 S += ",G";
6575 S += PD->getGetterName().getAsString();
6576 }
6577
6578 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
6579 S += ",S";
6580 S += PD->getSetterName().getAsString();
6581 }
6582
6583 if (SynthesizePID) {
6584 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
6585 S += ",V";
6586 S += OID->getNameAsString();
6587 }
6588
6589 // FIXME: OBJCGC: weak & strong
6590 return S;
6591}
6592
6593/// getLegacyIntegralTypeEncoding -
6594/// Another legacy compatibility encoding: 32-bit longs are encoded as
6595/// 'l' or 'L' , but not always. For typedefs, we need to use
6596/// 'i' or 'I' instead if encoding a struct field, or a pointer!
6597void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
6598 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
6599 if (const auto *BT = PointeeTy->getAs<BuiltinType>()) {
6600 if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
6601 PointeeTy = UnsignedIntTy;
6602 else
6603 if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
6604 PointeeTy = IntTy;
6605 }
6606 }
6607}
6608
6609void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
6610 const FieldDecl *Field,
6611 QualType *NotEncodedT) const {
6612 // We follow the behavior of gcc, expanding structures which are
6613 // directly pointed to, and expanding embedded structures. Note that
6614 // these rules are sufficient to prevent recursive encoding of the
6615 // same type.
6616 getObjCEncodingForTypeImpl(T, S,
6617 ObjCEncOptions()
6618 .setExpandPointedToStructures()
6619 .setExpandStructures()
6620 .setIsOutermostType(),
6621 Field, NotEncodedT);
6622}
6623
6624void ASTContext::getObjCEncodingForPropertyType(QualType T,
6625 std::string& S) const {
6626 // Encode result type.
6627 // GCC has some special rules regarding encoding of properties which
6628 // closely resembles encoding of ivars.
6629 getObjCEncodingForTypeImpl(T, S,
6630 ObjCEncOptions()
6631 .setExpandPointedToStructures()
6632 .setExpandStructures()
6633 .setIsOutermostType()
6634 .setEncodingProperty(),
6635 /*Field=*/nullptr);
6636}
6637
6638static char getObjCEncodingForPrimitiveType(const ASTContext *C,
6639 const BuiltinType *BT) {
6640 BuiltinType::Kind kind = BT->getKind();
6641 switch (kind) {
6642 case BuiltinType::Void: return 'v';
6643 case BuiltinType::Bool: return 'B';
6644 case BuiltinType::Char8:
6645 case BuiltinType::Char_U:
6646 case BuiltinType::UChar: return 'C';
6647 case BuiltinType::Char16:
6648 case BuiltinType::UShort: return 'S';
6649 case BuiltinType::Char32:
6650 case BuiltinType::UInt: return 'I';
6651 case BuiltinType::ULong:
6652 return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q';
6653 case BuiltinType::UInt128: return 'T';
6654 case BuiltinType::ULongLong: return 'Q';
6655 case BuiltinType::Char_S:
6656 case BuiltinType::SChar: return 'c';
6657 case BuiltinType::Short: return 's';
6658 case BuiltinType::WChar_S:
6659 case BuiltinType::WChar_U:
6660 case BuiltinType::Int: return 'i';
6661 case BuiltinType::Long:
6662 return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q';
6663 case BuiltinType::LongLong: return 'q';
6664 case BuiltinType::Int128: return 't';
6665 case BuiltinType::Float: return 'f';
6666 case BuiltinType::Double: return 'd';
6667 case BuiltinType::LongDouble: return 'D';
6668 case BuiltinType::NullPtr: return '*'; // like char*
6669
6670 case BuiltinType::Float16:
6671 case BuiltinType::Float128:
6672 case BuiltinType::Half:
6673 case BuiltinType::ShortAccum:
6674 case BuiltinType::Accum:
6675 case BuiltinType::LongAccum:
6676 case BuiltinType::UShortAccum:
6677 case BuiltinType::UAccum:
6678 case BuiltinType::ULongAccum:
6679 case BuiltinType::ShortFract:
6680 case BuiltinType::Fract:
6681 case BuiltinType::LongFract:
6682 case BuiltinType::UShortFract:
6683 case BuiltinType::UFract:
6684 case BuiltinType::ULongFract:
6685 case BuiltinType::SatShortAccum:
6686 case BuiltinType::SatAccum:
6687 case BuiltinType::SatLongAccum:
6688 case BuiltinType::SatUShortAccum:
6689 case BuiltinType::SatUAccum:
6690 case BuiltinType::SatULongAccum:
6691 case BuiltinType::SatShortFract:
6692 case BuiltinType::SatFract:
6693 case BuiltinType::SatLongFract:
6694 case BuiltinType::SatUShortFract:
6695 case BuiltinType::SatUFract:
6696 case BuiltinType::SatULongFract:
6697 // FIXME: potentially need @encodes for these!
6698 return ' ';
6699
6700#define SVE_TYPE(Name, Id, SingletonId) \
6701 case BuiltinType::Id:
6702#include "clang/Basic/AArch64SVEACLETypes.def"
6703 {
6704 DiagnosticsEngine &Diags = C->getDiagnostics();
6705 unsigned DiagID = Diags.getCustomDiagID(
6706 DiagnosticsEngine::Error, "cannot yet @encode type %0");
6707 Diags.Report(DiagID) << BT->getName(C->getPrintingPolicy());
6708 return ' ';
6709 }
6710
6711 case BuiltinType::ObjCId:
6712 case BuiltinType::ObjCClass:
6713 case BuiltinType::ObjCSel:
6714 llvm_unreachable("@encoding ObjC primitive type")::llvm::llvm_unreachable_internal("@encoding ObjC primitive type"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6714)
;
6715
6716 // OpenCL and placeholder types don't need @encodings.
6717#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6718 case BuiltinType::Id:
6719#include "clang/Basic/OpenCLImageTypes.def"
6720#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6721 case BuiltinType::Id:
6722#include "clang/Basic/OpenCLExtensionTypes.def"
6723 case BuiltinType::OCLEvent:
6724 case BuiltinType::OCLClkEvent:
6725 case BuiltinType::OCLQueue:
6726 case BuiltinType::OCLReserveID:
6727 case BuiltinType::OCLSampler:
6728 case BuiltinType::Dependent:
6729#define BUILTIN_TYPE(KIND, ID)
6730#define PLACEHOLDER_TYPE(KIND, ID) \
6731 case BuiltinType::KIND:
6732#include "clang/AST/BuiltinTypes.def"
6733 llvm_unreachable("invalid builtin type for @encode")::llvm::llvm_unreachable_internal("invalid builtin type for @encode"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6733)
;
6734 }
6735 llvm_unreachable("invalid BuiltinType::Kind value")::llvm::llvm_unreachable_internal("invalid BuiltinType::Kind value"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6735)
;
6736}
6737
6738static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) {
6739 EnumDecl *Enum = ET->getDecl();
6740
6741 // The encoding of an non-fixed enum type is always 'i', regardless of size.
6742 if (!Enum->isFixed())
6743 return 'i';
6744
6745 // The encoding of a fixed enum type matches its fixed underlying type.
6746 const auto *BT = Enum->getIntegerType()->castAs<BuiltinType>();
6747 return getObjCEncodingForPrimitiveType(C, BT);
6748}
6749
6750static void EncodeBitField(const ASTContext *Ctx, std::string& S,
6751 QualType T, const FieldDecl *FD) {
6752 assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl")((FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"
) ? static_cast<void> (0) : __assert_fail ("FD->isBitField() && \"not a bitfield - getObjCEncodingForTypeImpl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6752, __PRETTY_FUNCTION__))
;
6753 S += 'b';
6754 // The NeXT runtime encodes bit fields as b followed by the number of bits.
6755 // The GNU runtime requires more information; bitfields are encoded as b,
6756 // then the offset (in bits) of the first element, then the type of the
6757 // bitfield, then the size in bits. For example, in this structure:
6758 //
6759 // struct
6760 // {
6761 // int integer;
6762 // int flags:2;
6763 // };
6764 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
6765 // runtime, but b32i2 for the GNU runtime. The reason for this extra
6766 // information is not especially sensible, but we're stuck with it for
6767 // compatibility with GCC, although providing it breaks anything that
6768 // actually uses runtime introspection and wants to work on both runtimes...
6769 if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) {
6770 uint64_t Offset;
6771
6772 if (const auto *IVD = dyn_cast<ObjCIvarDecl>(FD)) {
6773 Offset = Ctx->lookupFieldBitOffset(IVD->getContainingInterface(), nullptr,
6774 IVD);
6775 } else {
6776 const RecordDecl *RD = FD->getParent();
6777 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
6778 Offset = RL.getFieldOffset(FD->getFieldIndex());
6779 }
6780
6781 S += llvm::utostr(Offset);
6782
6783 if (const auto *ET = T->getAs<EnumType>())
6784 S += ObjCEncodingForEnumType(Ctx, ET);
6785 else {
6786 const auto *BT = T->castAs<BuiltinType>();
6787 S += getObjCEncodingForPrimitiveType(Ctx, BT);
6788 }
6789 }
6790 S += llvm::utostr(FD->getBitWidthValue(*Ctx));
6791}
6792
6793// FIXME: Use SmallString for accumulating string.
6794void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string &S,
6795 const ObjCEncOptions Options,
6796 const FieldDecl *FD,
6797 QualType *NotEncodedT) const {
6798 CanQualType CT = getCanonicalType(T);
6799 switch (CT->getTypeClass()) {
6800 case Type::Builtin:
6801 case Type::Enum:
6802 if (FD && FD->isBitField())
6803 return EncodeBitField(this, S, T, FD);
6804 if (const auto *BT = dyn_cast<BuiltinType>(CT))
6805 S += getObjCEncodingForPrimitiveType(this, BT);
6806 else
6807 S += ObjCEncodingForEnumType(this, cast<EnumType>(CT));
6808 return;
6809
6810 case Type::Complex: {
6811 const auto *CT = T->castAs<ComplexType>();
6812 S += 'j';
6813 getObjCEncodingForTypeImpl(CT->getElementType(), S, ObjCEncOptions(),
6814 /*Field=*/nullptr);
6815 return;
6816 }
6817
6818 case Type::Atomic: {
6819 const auto *AT = T->castAs<AtomicType>();
6820 S += 'A';
6821 getObjCEncodingForTypeImpl(AT->getValueType(), S, ObjCEncOptions(),
6822 /*Field=*/nullptr);
6823 return;
6824 }
6825
6826 // encoding for pointer or reference types.
6827 case Type::Pointer:
6828 case Type::LValueReference:
6829 case Type::RValueReference: {
6830 QualType PointeeTy;
6831 if (isa<PointerType>(CT)) {
6832 const auto *PT = T->castAs<PointerType>();
6833 if (PT->isObjCSelType()) {
6834 S += ':';
6835 return;
6836 }
6837 PointeeTy = PT->getPointeeType();
6838 } else {
6839 PointeeTy = T->castAs<ReferenceType>()->getPointeeType();
6840 }
6841
6842 bool isReadOnly = false;
6843 // For historical/compatibility reasons, the read-only qualifier of the
6844 // pointee gets emitted _before_ the '^'. The read-only qualifier of
6845 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
6846 // Also, do not emit the 'r' for anything but the outermost type!
6847 if (isa<TypedefType>(T.getTypePtr())) {
6848 if (Options.IsOutermostType() && T.isConstQualified()) {
6849 isReadOnly = true;
6850 S += 'r';
6851 }
6852 } else if (Options.IsOutermostType()) {
6853 QualType P = PointeeTy;
6854 while (P->getAs<PointerType>())
6855 P = P->getAs<PointerType>()->getPointeeType();
6856 if (P.isConstQualified()) {
6857 isReadOnly = true;
6858 S += 'r';
6859 }
6860 }
6861 if (isReadOnly) {
6862 // Another legacy compatibility encoding. Some ObjC qualifier and type
6863 // combinations need to be rearranged.
6864 // Rewrite "in const" from "nr" to "rn"
6865 if (StringRef(S).endswith("nr"))
6866 S.replace(S.end()-2, S.end(), "rn");
6867 }
6868
6869 if (PointeeTy->isCharType()) {
6870 // char pointer types should be encoded as '*' unless it is a
6871 // type that has been typedef'd to 'BOOL'.
6872 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
6873 S += '*';
6874 return;
6875 }
6876 } else if (const auto *RTy = PointeeTy->getAs<RecordType>()) {
6877 // GCC binary compat: Need to convert "struct objc_class *" to "#".
6878 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
6879 S += '#';
6880 return;
6881 }
6882 // GCC binary compat: Need to convert "struct objc_object *" to "@".
6883 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
6884 S += '@';
6885 return;
6886 }
6887 // fall through...
6888 }
6889 S += '^';
6890 getLegacyIntegralTypeEncoding(PointeeTy);
6891
6892 ObjCEncOptions NewOptions;
6893 if (Options.ExpandPointedToStructures())
6894 NewOptions.setExpandStructures();
6895 getObjCEncodingForTypeImpl(PointeeTy, S, NewOptions,
6896 /*Field=*/nullptr, NotEncodedT);
6897 return;
6898 }
6899
6900 case Type::ConstantArray:
6901 case Type::IncompleteArray:
6902 case Type::VariableArray: {
6903 const auto *AT = cast<ArrayType>(CT);
6904
6905 if (isa<IncompleteArrayType>(AT) && !Options.IsStructField()) {
6906 // Incomplete arrays are encoded as a pointer to the array element.
6907 S += '^';
6908
6909 getObjCEncodingForTypeImpl(
6910 AT->getElementType(), S,
6911 Options.keepingOnly(ObjCEncOptions().setExpandStructures()), FD);
6912 } else {
6913 S += '[';
6914
6915 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
6916 S += llvm::utostr(CAT->getSize().getZExtValue());
6917 else {
6918 //Variable length arrays are encoded as a regular array with 0 elements.
6919 assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&(((isa<VariableArrayType>(AT) || isa<IncompleteArrayType
>(AT)) && "Unknown array type!") ? static_cast<
void> (0) : __assert_fail ("(isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && \"Unknown array type!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6920, __PRETTY_FUNCTION__))
6920 "Unknown array type!")(((isa<VariableArrayType>(AT) || isa<IncompleteArrayType
>(AT)) && "Unknown array type!") ? static_cast<
void> (0) : __assert_fail ("(isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && \"Unknown array type!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 6920, __PRETTY_FUNCTION__))
;
6921 S += '0';
6922 }
6923
6924 getObjCEncodingForTypeImpl(
6925 AT->getElementType(), S,
6926 Options.keepingOnly(ObjCEncOptions().setExpandStructures()), FD,
6927 NotEncodedT);
6928 S += ']';
6929 }
6930 return;
6931 }
6932
6933 case Type::FunctionNoProto:
6934 case Type::FunctionProto:
6935 S += '?';
6936 return;
6937
6938 case Type::Record: {
6939 RecordDecl *RDecl = cast<RecordType>(CT)->getDecl();
6940 S += RDecl->isUnion() ? '(' : '{';
6941 // Anonymous structures print as '?'
6942 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
6943 S += II->getName();
6944 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
6945 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
6946 llvm::raw_string_ostream OS(S);
6947 printTemplateArgumentList(OS, TemplateArgs.asArray(),
6948 getPrintingPolicy());
6949 }
6950 } else {
6951 S += '?';
6952 }
6953 if (Options.ExpandStructures()) {
6954 S += '=';
6955 if (!RDecl->isUnion()) {
6956 getObjCEncodingForStructureImpl(RDecl, S, FD, true, NotEncodedT);
6957 } else {
6958 for (const auto *Field : RDecl->fields()) {
6959 if (FD) {
6960 S += '"';
6961 S += Field->getNameAsString();
6962 S += '"';
6963 }
6964
6965 // Special case bit-fields.
6966 if (Field->isBitField()) {
6967 getObjCEncodingForTypeImpl(Field->getType(), S,
6968 ObjCEncOptions().setExpandStructures(),
6969 Field);
6970 } else {
6971 QualType qt = Field->getType();
6972 getLegacyIntegralTypeEncoding(qt);
6973 getObjCEncodingForTypeImpl(
6974 qt, S,
6975 ObjCEncOptions().setExpandStructures().setIsStructField(), FD,
6976 NotEncodedT);
6977 }
6978 }
6979 }
6980 }
6981 S += RDecl->isUnion() ? ')' : '}';
6982 return;
6983 }
6984
6985 case Type::BlockPointer: {
6986 const auto *BT = T->castAs<BlockPointerType>();
6987 S += "@?"; // Unlike a pointer-to-function, which is "^?".
6988 if (Options.EncodeBlockParameters()) {
6989 const auto *FT = BT->getPointeeType()->castAs<FunctionType>();
6990
6991 S += '<';
6992 // Block return type
6993 getObjCEncodingForTypeImpl(FT->getReturnType(), S,
6994 Options.forComponentType(), FD, NotEncodedT);
6995 // Block self
6996 S += "@?";
6997 // Block parameters
6998 if (const auto *FPT = dyn_cast<FunctionProtoType>(FT)) {
6999 for (const auto &I : FPT->param_types())
7000 getObjCEncodingForTypeImpl(I, S, Options.forComponentType(), FD,
7001 NotEncodedT);
7002 }
7003 S += '>';
7004 }
7005 return;
7006 }
7007
7008 case Type::ObjCObject: {
7009 // hack to match legacy encoding of *id and *Class
7010 QualType Ty = getObjCObjectPointerType(CT);
7011 if (Ty->isObjCIdType()) {
7012 S += "{objc_object=}";
7013 return;
7014 }
7015 else if (Ty->isObjCClassType()) {
7016 S += "{objc_class=}";
7017 return;
7018 }
7019 // TODO: Double check to make sure this intentionally falls through.
7020 LLVM_FALLTHROUGH[[gnu::fallthrough]];
7021 }
7022
7023 case Type::ObjCInterface: {
7024 // Ignore protocol qualifiers when mangling at this level.
7025 // @encode(class_name)
7026 ObjCInterfaceDecl *OI = T->castAs<ObjCObjectType>()->getInterface();
7027 S += '{';
7028 S += OI->getObjCRuntimeNameAsString();
7029 if (Options.ExpandStructures()) {
7030 S += '=';
7031 SmallVector<const ObjCIvarDecl*, 32> Ivars;
7032 DeepCollectObjCIvars(OI, true, Ivars);
7033 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
7034 const FieldDecl *Field = Ivars[i];
7035 if (Field->isBitField())
7036 getObjCEncodingForTypeImpl(Field->getType(), S,
7037 ObjCEncOptions().setExpandStructures(),
7038 Field);
7039 else
7040 getObjCEncodingForTypeImpl(Field->getType(), S,
7041 ObjCEncOptions().setExpandStructures(), FD,
7042 NotEncodedT);
7043 }
7044 }
7045 S += '}';
7046 return;
7047 }
7048
7049 case Type::ObjCObjectPointer: {
7050 const auto *OPT = T->castAs<ObjCObjectPointerType>();
7051 if (OPT->isObjCIdType()) {
7052 S += '@';
7053 return;
7054 }
7055
7056 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
7057 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
7058 // Since this is a binary compatibility issue, need to consult with
7059 // runtime folks. Fortunately, this is a *very* obscure construct.
7060 S += '#';
7061 return;
7062 }
7063
7064 if (OPT->isObjCQualifiedIdType()) {
7065 getObjCEncodingForTypeImpl(
7066 getObjCIdType(), S,
7067 Options.keepingOnly(ObjCEncOptions()
7068 .setExpandPointedToStructures()
7069 .setExpandStructures()),
7070 FD);
7071 if (FD || Options.EncodingProperty() || Options.EncodeClassNames()) {
7072 // Note that we do extended encoding of protocol qualifer list
7073 // Only when doing ivar or property encoding.
7074 S += '"';
7075 for (const auto *I : OPT->quals()) {
7076 S += '<';
7077 S += I->getObjCRuntimeNameAsString();
7078 S += '>';
7079 }
7080 S += '"';
7081 }
7082 return;
7083 }
7084
7085 S += '@';
7086 if (OPT->getInterfaceDecl() &&
7087 (FD || Options.EncodingProperty() || Options.EncodeClassNames())) {
7088 S += '"';
7089 S += OPT->getInterfaceDecl()->getObjCRuntimeNameAsString();
7090 for (const auto *I : OPT->quals()) {
7091 S += '<';
7092 S += I->getObjCRuntimeNameAsString();
7093 S += '>';
7094 }
7095 S += '"';
7096 }
7097 return;
7098 }
7099
7100 // gcc just blithely ignores member pointers.
7101 // FIXME: we should do better than that. 'M' is available.
7102 case Type::MemberPointer:
7103 // This matches gcc's encoding, even though technically it is insufficient.
7104 //FIXME. We should do a better job than gcc.
7105 case Type::Vector:
7106 case Type::ExtVector:
7107 // Until we have a coherent encoding of these three types, issue warning.
7108 if (NotEncodedT)
7109 *NotEncodedT = T;
7110 return;
7111
7112 // We could see an undeduced auto type here during error recovery.
7113 // Just ignore it.
7114 case Type::Auto:
7115 case Type::DeducedTemplateSpecialization:
7116 return;
7117
7118 case Type::Pipe:
7119#define ABSTRACT_TYPE(KIND, BASE)
7120#define TYPE(KIND, BASE)
7121#define DEPENDENT_TYPE(KIND, BASE) \
7122 case Type::KIND:
7123#define NON_CANONICAL_TYPE(KIND, BASE) \
7124 case Type::KIND:
7125#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \
7126 case Type::KIND:
7127#include "clang/AST/TypeNodes.inc"
7128 llvm_unreachable("@encode for dependent type!")::llvm::llvm_unreachable_internal("@encode for dependent type!"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7128)
;
7129 }
7130 llvm_unreachable("bad type kind!")::llvm::llvm_unreachable_internal("bad type kind!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7130)
;
7131}
7132
7133void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
7134 std::string &S,
7135 const FieldDecl *FD,
7136 bool includeVBases,
7137 QualType *NotEncodedT) const {
7138 assert(RDecl && "Expected non-null RecordDecl")((RDecl && "Expected non-null RecordDecl") ? static_cast
<void> (0) : __assert_fail ("RDecl && \"Expected non-null RecordDecl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7138, __PRETTY_FUNCTION__))
;
7139 assert(!RDecl->isUnion() && "Should not be called for unions")((!RDecl->isUnion() && "Should not be called for unions"
) ? static_cast<void> (0) : __assert_fail ("!RDecl->isUnion() && \"Should not be called for unions\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7139, __PRETTY_FUNCTION__))
;
7140 if (!RDecl->getDefinition() || RDecl->getDefinition()->isInvalidDecl())
7141 return;
7142
7143 const auto *CXXRec = dyn_cast<CXXRecordDecl>(RDecl);
7144 std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets;
7145 const ASTRecordLayout &layout = getASTRecordLayout(RDecl);
7146
7147 if (CXXRec) {
7148 for (const auto &BI : CXXRec->bases()) {
7149 if (!BI.isVirtual()) {
7150 CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl();
7151 if (base->isEmpty())
7152 continue;
7153 uint64_t offs = toBits(layout.getBaseClassOffset(base));
7154 FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
7155 std::make_pair(offs, base));
7156 }
7157 }
7158 }
7159
7160 unsigned i = 0;
7161 for (auto *Field : RDecl->fields()) {
7162 uint64_t offs = layout.getFieldOffset(i);
7163 FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
7164 std::make_pair(offs, Field));
7165 ++i;
7166 }
7167
7168 if (CXXRec && includeVBases) {
7169 for (const auto &BI : CXXRec->vbases()) {
7170 CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl();
7171 if (base->isEmpty())
7172 continue;
7173 uint64_t offs = toBits(layout.getVBaseClassOffset(base));
7174 if (offs >= uint64_t(toBits(layout.getNonVirtualSize())) &&
7175 FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end())
7176 FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(),
7177 std::make_pair(offs, base));
7178 }
7179 }
7180
7181 CharUnits size;
7182 if (CXXRec) {
7183 size = includeVBases ? layout.getSize() : layout.getNonVirtualSize();
7184 } else {
7185 size = layout.getSize();
7186 }
7187
7188#ifndef NDEBUG
7189 uint64_t CurOffs = 0;
7190#endif
7191 std::multimap<uint64_t, NamedDecl *>::iterator
7192 CurLayObj = FieldOrBaseOffsets.begin();
7193
7194 if (CXXRec && CXXRec->isDynamicClass() &&
7195 (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) {
7196 if (FD) {
7197 S += "\"_vptr$";
7198 std::string recname = CXXRec->getNameAsString();
7199 if (recname.empty()) recname = "?";
7200 S += recname;
7201 S += '"';
7202 }
7203 S += "^^?";
7204#ifndef NDEBUG
7205 CurOffs += getTypeSize(VoidPtrTy);
7206#endif
7207 }
7208
7209 if (!RDecl->hasFlexibleArrayMember()) {
7210 // Mark the end of the structure.
7211 uint64_t offs = toBits(size);
7212 FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
7213 std::make_pair(offs, nullptr));
7214 }
7215
7216 for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) {
7217#ifndef NDEBUG
7218 assert(CurOffs <= CurLayObj->first)((CurOffs <= CurLayObj->first) ? static_cast<void>
(0) : __assert_fail ("CurOffs <= CurLayObj->first", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7218, __PRETTY_FUNCTION__))
;
7219 if (CurOffs < CurLayObj->first) {
7220 uint64_t padding = CurLayObj->first - CurOffs;
7221 // FIXME: There doesn't seem to be a way to indicate in the encoding that
7222 // packing/alignment of members is different that normal, in which case
7223 // the encoding will be out-of-sync with the real layout.
7224 // If the runtime switches to just consider the size of types without
7225 // taking into account alignment, we could make padding explicit in the
7226 // encoding (e.g. using arrays of chars). The encoding strings would be
7227 // longer then though.
7228 CurOffs += padding;
7229 }
7230#endif
7231
7232 NamedDecl *dcl = CurLayObj->second;
7233 if (!dcl)
7234 break; // reached end of structure.
7235
7236 if (auto *base = dyn_cast<CXXRecordDecl>(dcl)) {
7237 // We expand the bases without their virtual bases since those are going
7238 // in the initial structure. Note that this differs from gcc which
7239 // expands virtual bases each time one is encountered in the hierarchy,
7240 // making the encoding type bigger than it really is.
7241 getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false,
7242 NotEncodedT);
7243 assert(!base->isEmpty())((!base->isEmpty()) ? static_cast<void> (0) : __assert_fail
("!base->isEmpty()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7243, __PRETTY_FUNCTION__))
;
7244#ifndef NDEBUG
7245 CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize());
7246#endif
7247 } else {
7248 const auto *field = cast<FieldDecl>(dcl);
7249 if (FD) {
7250 S += '"';
7251 S += field->getNameAsString();
7252 S += '"';
7253 }
7254
7255 if (field->isBitField()) {
7256 EncodeBitField(this, S, field->getType(), field);
7257#ifndef NDEBUG
7258 CurOffs += field->getBitWidthValue(*this);
7259#endif
7260 } else {
7261 QualType qt = field->getType();
7262 getLegacyIntegralTypeEncoding(qt);
7263 getObjCEncodingForTypeImpl(
7264 qt, S, ObjCEncOptions().setExpandStructures().setIsStructField(),
7265 FD, NotEncodedT);
7266#ifndef NDEBUG
7267 CurOffs += getTypeSize(field->getType());
7268#endif
7269 }
7270 }
7271 }
7272}
7273
7274void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
7275 std::string& S) const {
7276 if (QT & Decl::OBJC_TQ_In)
7277 S += 'n';
7278 if (QT & Decl::OBJC_TQ_Inout)
7279 S += 'N';
7280 if (QT & Decl::OBJC_TQ_Out)
7281 S += 'o';
7282 if (QT & Decl::OBJC_TQ_Bycopy)
7283 S += 'O';
7284 if (QT & Decl::OBJC_TQ_Byref)
7285 S += 'R';
7286 if (QT & Decl::OBJC_TQ_Oneway)
7287 S += 'V';
7288}
7289
7290TypedefDecl *ASTContext::getObjCIdDecl() const {
7291 if (!ObjCIdDecl) {
7292 QualType T = getObjCObjectType(ObjCBuiltinIdTy, {}, {});
7293 T = getObjCObjectPointerType(T);
7294 ObjCIdDecl = buildImplicitTypedef(T, "id");
7295 }
7296 return ObjCIdDecl;
7297}
7298
7299TypedefDecl *ASTContext::getObjCSelDecl() const {
7300 if (!ObjCSelDecl) {
7301 QualType T = getPointerType(ObjCBuiltinSelTy);
7302 ObjCSelDecl = buildImplicitTypedef(T, "SEL");
7303 }
7304 return ObjCSelDecl;
7305}
7306
7307TypedefDecl *ASTContext::getObjCClassDecl() const {
7308 if (!ObjCClassDecl) {
7309 QualType T = getObjCObjectType(ObjCBuiltinClassTy, {}, {});
7310 T = getObjCObjectPointerType(T);
7311 ObjCClassDecl = buildImplicitTypedef(T, "Class");
7312 }
7313 return ObjCClassDecl;
7314}
7315
7316ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const {
7317 if (!ObjCProtocolClassDecl) {
7318 ObjCProtocolClassDecl
7319 = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(),
7320 SourceLocation(),
7321 &Idents.get("Protocol"),
7322 /*typeParamList=*/nullptr,
7323 /*PrevDecl=*/nullptr,
7324 SourceLocation(), true);
7325 }
7326
7327 return ObjCProtocolClassDecl;
7328}
7329
7330//===----------------------------------------------------------------------===//
7331// __builtin_va_list Construction Functions
7332//===----------------------------------------------------------------------===//
7333
7334static TypedefDecl *CreateCharPtrNamedVaListDecl(const ASTContext *Context,
7335 StringRef Name) {
7336 // typedef char* __builtin[_ms]_va_list;
7337 QualType T = Context->getPointerType(Context->CharTy);
7338 return Context->buildImplicitTypedef(T, Name);
7339}
7340
7341static TypedefDecl *CreateMSVaListDecl(const ASTContext *Context) {
7342 return CreateCharPtrNamedVaListDecl(Context, "__builtin_ms_va_list");
7343}
7344
7345static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) {
7346 return CreateCharPtrNamedVaListDecl(Context, "__builtin_va_list");
7347}
7348
7349static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) {
7350 // typedef void* __builtin_va_list;
7351 QualType T = Context->getPointerType(Context->VoidTy);
7352 return Context->buildImplicitTypedef(T, "__builtin_va_list");
7353}
7354
7355static TypedefDecl *
7356CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) {
7357 // struct __va_list
7358 RecordDecl *VaListTagDecl = Context->buildImplicitRecord("__va_list");
7359 if (Context->getLangOpts().CPlusPlus) {
7360 // namespace std { struct __va_list {
7361 NamespaceDecl *NS;
7362 NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context),
7363 Context->getTranslationUnitDecl(),
7364 /*Inline*/ false, SourceLocation(),
7365 SourceLocation(), &Context->Idents.get("std"),
7366 /*PrevDecl*/ nullptr);
7367 NS->setImplicit();
7368 VaListTagDecl->setDeclContext(NS);
7369 }
7370
7371 VaListTagDecl->startDefinition();
7372
7373 const size_t NumFields = 5;
7374 QualType FieldTypes[NumFields];
7375 const char *FieldNames[NumFields];
7376
7377 // void *__stack;
7378 FieldTypes[0] = Context->getPointerType(Context->VoidTy);
7379 FieldNames[0] = "__stack";
7380
7381 // void *__gr_top;
7382 FieldTypes[1] = Context->getPointerType(Context->VoidTy);
7383 FieldNames[1] = "__gr_top";
7384
7385 // void *__vr_top;
7386 FieldTypes[2] = Context->getPointerType(Context->VoidTy);
7387 FieldNames[2] = "__vr_top";
7388
7389 // int __gr_offs;
7390 FieldTypes[3] = Context->IntTy;
7391 FieldNames[3] = "__gr_offs";
7392
7393 // int __vr_offs;
7394 FieldTypes[4] = Context->IntTy;
7395 FieldNames[4] = "__vr_offs";
7396
7397 // Create fields
7398 for (unsigned i = 0; i < NumFields; ++i) {
7399 FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
7400 VaListTagDecl,
7401 SourceLocation(),
7402 SourceLocation(),
7403 &Context->Idents.get(FieldNames[i]),
7404 FieldTypes[i], /*TInfo=*/nullptr,
7405 /*BitWidth=*/nullptr,
7406 /*Mutable=*/false,
7407 ICIS_NoInit);
7408 Field->setAccess(AS_public);
7409 VaListTagDecl->addDecl(Field);
7410 }
7411 VaListTagDecl->completeDefinition();
7412 Context->VaListTagDecl = VaListTagDecl;
7413 QualType VaListTagType = Context->getRecordType(VaListTagDecl);
7414
7415 // } __builtin_va_list;
7416 return Context->buildImplicitTypedef(VaListTagType, "__builtin_va_list");
7417}
7418
7419static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) {
7420 // typedef struct __va_list_tag {
7421 RecordDecl *VaListTagDecl;
7422
7423 VaListTagDecl = Context->buildImplicitRecord("__va_list_tag");
7424 VaListTagDecl->startDefinition();
7425
7426 const size_t NumFields = 5;
7427 QualType FieldTypes[NumFields];
7428 const char *FieldNames[NumFields];
7429
7430 // unsigned char gpr;
7431 FieldTypes[0] = Context->UnsignedCharTy;
7432 FieldNames[0] = "gpr";
7433
7434 // unsigned char fpr;
7435 FieldTypes[1] = Context->UnsignedCharTy;
7436 FieldNames[1] = "fpr";
7437
7438 // unsigned short reserved;
7439 FieldTypes[2] = Context->UnsignedShortTy;
7440 FieldNames[2] = "reserved";
7441
7442 // void* overflow_arg_area;
7443 FieldTypes[3] = Context->getPointerType(Context->VoidTy);
7444 FieldNames[3] = "overflow_arg_area";
7445
7446 // void* reg_save_area;
7447 FieldTypes[4] = Context->getPointerType(Context->VoidTy);
7448 FieldNames[4] = "reg_save_area";
7449
7450 // Create fields
7451 for (unsigned i = 0; i < NumFields; ++i) {
7452 FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl,
7453 SourceLocation(),
7454 SourceLocation(),
7455 &Context->Idents.get(FieldNames[i]),
7456 FieldTypes[i], /*TInfo=*/nullptr,
7457 /*BitWidth=*/nullptr,
7458 /*Mutable=*/false,
7459 ICIS_NoInit);
7460 Field->setAccess(AS_public);
7461 VaListTagDecl->addDecl(Field);
7462 }
7463 VaListTagDecl->completeDefinition();
7464 Context->VaListTagDecl = VaListTagDecl;
7465 QualType VaListTagType = Context->getRecordType(VaListTagDecl);
7466
7467 // } __va_list_tag;
7468 TypedefDecl *VaListTagTypedefDecl =
7469 Context->buildImplicitTypedef(VaListTagType, "__va_list_tag");
7470
7471 QualType VaListTagTypedefType =
7472 Context->getTypedefType(VaListTagTypedefDecl);
7473
7474 // typedef __va_list_tag __builtin_va_list[1];
7475 llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
7476 QualType VaListTagArrayType
7477 = Context->getConstantArrayType(VaListTagTypedefType,
7478 Size, ArrayType::Normal, 0);
7479 return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list");
7480}
7481
7482static TypedefDecl *
7483CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) {
7484 // struct __va_list_tag {
7485 RecordDecl *VaListTagDecl;
7486 VaListTagDecl = Context->buildImplicitRecord("__va_list_tag");
7487 VaListTagDecl->startDefinition();
7488
7489 const size_t NumFields = 4;
7490 QualType FieldTypes[NumFields];
7491 const char *FieldNames[NumFields];
7492
7493 // unsigned gp_offset;
7494 FieldTypes[0] = Context->UnsignedIntTy;
7495 FieldNames[0] = "gp_offset";
7496
7497 // unsigned fp_offset;
7498 FieldTypes[1] = Context->UnsignedIntTy;
7499 FieldNames[1] = "fp_offset";
7500
7501 // void* overflow_arg_area;
7502 FieldTypes[2] = Context->getPointerType(Context->VoidTy);
7503 FieldNames[2] = "overflow_arg_area";
7504
7505 // void* reg_save_area;
7506 FieldTypes[3] = Context->getPointerType(Context->VoidTy);
7507 FieldNames[3] = "reg_save_area";
7508
7509 // Create fields
7510 for (unsigned i = 0; i < NumFields; ++i) {
7511 FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
7512 VaListTagDecl,
7513 SourceLocation(),
7514 SourceLocation(),
7515 &Context->Idents.get(FieldNames[i]),
7516 FieldTypes[i], /*TInfo=*/nullptr,
7517 /*BitWidth=*/nullptr,
7518 /*Mutable=*/false,
7519 ICIS_NoInit);
7520 Field->setAccess(AS_public);
7521 VaListTagDecl->addDecl(Field);
7522 }
7523 VaListTagDecl->completeDefinition();
7524 Context->VaListTagDecl = VaListTagDecl;
7525 QualType VaListTagType = Context->getRecordType(VaListTagDecl);
7526
7527 // };
7528
7529 // typedef struct __va_list_tag __builtin_va_list[1];
7530 llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
7531 QualType VaListTagArrayType =
7532 Context->getConstantArrayType(VaListTagType, Size, ArrayType::Normal, 0);
7533 return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list");
7534}
7535
7536static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) {
7537 // typedef int __builtin_va_list[4];
7538 llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4);
7539 QualType IntArrayType =
7540 Context->getConstantArrayType(Context->IntTy, Size, ArrayType::Normal, 0);
7541 return Context->buildImplicitTypedef(IntArrayType, "__builtin_va_list");
7542}
7543
7544static TypedefDecl *
7545CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) {
7546 // struct __va_list
7547 RecordDecl *VaListDecl = Context->buildImplicitRecord("__va_list");
7548 if (Context->getLangOpts().CPlusPlus) {
7549 // namespace std { struct __va_list {
7550 NamespaceDecl *NS;
7551 NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context),
7552 Context->getTranslationUnitDecl(),
7553 /*Inline*/false, SourceLocation(),
7554 SourceLocation(), &Context->Idents.get("std"),
7555 /*PrevDecl*/ nullptr);
7556 NS->setImplicit();
7557 VaListDecl->setDeclContext(NS);
7558 }
7559
7560 VaListDecl->startDefinition();
7561
7562 // void * __ap;
7563 FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
7564 VaListDecl,
7565 SourceLocation(),
7566 SourceLocation(),
7567 &Context->Idents.get("__ap"),
7568 Context->getPointerType(Context->VoidTy),
7569 /*TInfo=*/nullptr,
7570 /*BitWidth=*/nullptr,
7571 /*Mutable=*/false,
7572 ICIS_NoInit);
7573 Field->setAccess(AS_public);
7574 VaListDecl->addDecl(Field);
7575
7576 // };
7577 VaListDecl->completeDefinition();
7578 Context->VaListTagDecl = VaListDecl;
7579
7580 // typedef struct __va_list __builtin_va_list;
7581 QualType T = Context->getRecordType(VaListDecl);
7582 return Context->buildImplicitTypedef(T, "__builtin_va_list");
7583}
7584
7585static TypedefDecl *
7586CreateSystemZBuiltinVaListDecl(const ASTContext *Context) {
7587 // struct __va_list_tag {
7588 RecordDecl *VaListTagDecl;
7589 VaListTagDecl = Context->buildImplicitRecord("__va_list_tag");
7590 VaListTagDecl->startDefinition();
7591
7592 const size_t NumFields = 4;
7593 QualType FieldTypes[NumFields];
7594 const char *FieldNames[NumFields];
7595
7596 // long __gpr;
7597 FieldTypes[0] = Context->LongTy;
7598 FieldNames[0] = "__gpr";
7599
7600 // long __fpr;
7601 FieldTypes[1] = Context->LongTy;
7602 FieldNames[1] = "__fpr";
7603
7604 // void *__overflow_arg_area;
7605 FieldTypes[2] = Context->getPointerType(Context->VoidTy);
7606 FieldNames[2] = "__overflow_arg_area";
7607
7608 // void *__reg_save_area;
7609 FieldTypes[3] = Context->getPointerType(Context->VoidTy);
7610 FieldNames[3] = "__reg_save_area";
7611
7612 // Create fields
7613 for (unsigned i = 0; i < NumFields; ++i) {
7614 FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
7615 VaListTagDecl,
7616 SourceLocation(),
7617 SourceLocation(),
7618 &Context->Idents.get(FieldNames[i]),
7619 FieldTypes[i], /*TInfo=*/nullptr,
7620 /*BitWidth=*/nullptr,
7621 /*Mutable=*/false,
7622 ICIS_NoInit);
7623 Field->setAccess(AS_public);
7624 VaListTagDecl->addDecl(Field);
7625 }
7626 VaListTagDecl->completeDefinition();
7627 Context->VaListTagDecl = VaListTagDecl;
7628 QualType VaListTagType = Context->getRecordType(VaListTagDecl);
7629
7630 // };
7631
7632 // typedef __va_list_tag __builtin_va_list[1];
7633 llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
7634 QualType VaListTagArrayType =
7635 Context->getConstantArrayType(VaListTagType, Size, ArrayType::Normal, 0);
7636
7637 return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list");
7638}
7639
7640static TypedefDecl *CreateVaListDecl(const ASTContext *Context,
7641 TargetInfo::BuiltinVaListKind Kind) {
7642 switch (Kind) {
7643 case TargetInfo::CharPtrBuiltinVaList:
7644 return CreateCharPtrBuiltinVaListDecl(Context);
7645 case TargetInfo::VoidPtrBuiltinVaList:
7646 return CreateVoidPtrBuiltinVaListDecl(Context);
7647 case TargetInfo::AArch64ABIBuiltinVaList:
7648 return CreateAArch64ABIBuiltinVaListDecl(Context);
7649 case TargetInfo::PowerABIBuiltinVaList:
7650 return CreatePowerABIBuiltinVaListDecl(Context);
7651 case TargetInfo::X86_64ABIBuiltinVaList:
7652 return CreateX86_64ABIBuiltinVaListDecl(Context);
7653 case TargetInfo::PNaClABIBuiltinVaList:
7654 return CreatePNaClABIBuiltinVaListDecl(Context);
7655 case TargetInfo::AAPCSABIBuiltinVaList:
7656 return CreateAAPCSABIBuiltinVaListDecl(Context);
7657 case TargetInfo::SystemZBuiltinVaList:
7658 return CreateSystemZBuiltinVaListDecl(Context);
7659 }
7660
7661 llvm_unreachable("Unhandled __builtin_va_list type kind")::llvm::llvm_unreachable_internal("Unhandled __builtin_va_list type kind"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7661)
;
7662}
7663
7664TypedefDecl *ASTContext::getBuiltinVaListDecl() const {
7665 if (!BuiltinVaListDecl) {
7666 BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind());
7667 assert(BuiltinVaListDecl->isImplicit())((BuiltinVaListDecl->isImplicit()) ? static_cast<void>
(0) : __assert_fail ("BuiltinVaListDecl->isImplicit()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7667, __PRETTY_FUNCTION__))
;
7668 }
7669
7670 return BuiltinVaListDecl;
7671}
7672
7673Decl *ASTContext::getVaListTagDecl() const {
7674 // Force the creation of VaListTagDecl by building the __builtin_va_list
7675 // declaration.
7676 if (!VaListTagDecl)
7677 (void)getBuiltinVaListDecl();
7678
7679 return VaListTagDecl;
7680}
7681
7682TypedefDecl *ASTContext::getBuiltinMSVaListDecl() const {
7683 if (!BuiltinMSVaListDecl)
7684 BuiltinMSVaListDecl = CreateMSVaListDecl(this);
7685
7686 return BuiltinMSVaListDecl;
7687}
7688
7689bool ASTContext::canBuiltinBeRedeclared(const FunctionDecl *FD) const {
7690 return BuiltinInfo.canBeRedeclared(FD->getBuiltinID());
7691}
7692
7693void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
7694 assert(ObjCConstantStringType.isNull() &&((ObjCConstantStringType.isNull() && "'NSConstantString' type already set!"
) ? static_cast<void> (0) : __assert_fail ("ObjCConstantStringType.isNull() && \"'NSConstantString' type already set!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7695, __PRETTY_FUNCTION__))
7695 "'NSConstantString' type already set!")((ObjCConstantStringType.isNull() && "'NSConstantString' type already set!"
) ? static_cast<void> (0) : __assert_fail ("ObjCConstantStringType.isNull() && \"'NSConstantString' type already set!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7695, __PRETTY_FUNCTION__))
;
7696
7697 ObjCConstantStringType = getObjCInterfaceType(Decl);
7698}
7699
7700/// Retrieve the template name that corresponds to a non-empty
7701/// lookup.
7702TemplateName
7703ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
7704 UnresolvedSetIterator End) const {
7705 unsigned size = End - Begin;
7706 assert(size > 1 && "set is not overloaded!")((size > 1 && "set is not overloaded!") ? static_cast
<void> (0) : __assert_fail ("size > 1 && \"set is not overloaded!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7706, __PRETTY_FUNCTION__))
;
7707
7708 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
7709 size * sizeof(FunctionTemplateDecl*));
7710 auto *OT = new (memory) OverloadedTemplateStorage(size);
7711
7712 NamedDecl **Storage = OT->getStorage();
7713 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
7714 NamedDecl *D = *I;
7715 assert(isa<FunctionTemplateDecl>(D) ||((isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl
>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl
>(D->getUnderlyingDecl()))) ? static_cast<void> (
0) : __assert_fail ("isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7718, __PRETTY_FUNCTION__))
7716 isa<UnresolvedUsingValueDecl>(D) ||((isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl
>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl
>(D->getUnderlyingDecl()))) ? static_cast<void> (
0) : __assert_fail ("isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7718, __PRETTY_FUNCTION__))
7717 (isa<UsingShadowDecl>(D) &&((isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl
>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl
>(D->getUnderlyingDecl()))) ? static_cast<void> (
0) : __assert_fail ("isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7718, __PRETTY_FUNCTION__))
7718 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())))((isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl
>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl
>(D->getUnderlyingDecl()))) ? static_cast<void> (
0) : __assert_fail ("isa<FunctionTemplateDecl>(D) || isa<UnresolvedUsingValueDecl>(D) || (isa<UsingShadowDecl>(D) && isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7718, __PRETTY_FUNCTION__))
;
7719 *Storage++ = D;
7720 }
7721
7722 return TemplateName(OT);
7723}
7724
7725/// Retrieve a template name representing an unqualified-id that has been
7726/// assumed to name a template for ADL purposes.
7727TemplateName ASTContext::getAssumedTemplateName(DeclarationName Name) const {
7728 auto *OT = new (*this) AssumedTemplateStorage(Name);
7729 return TemplateName(OT);
7730}
7731
7732/// Retrieve the template name that represents a qualified
7733/// template name such as \c std::vector.
7734TemplateName
7735ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
7736 bool TemplateKeyword,
7737 TemplateDecl *Template) const {
7738 assert(NNS && "Missing nested-name-specifier in qualified template name")((NNS && "Missing nested-name-specifier in qualified template name"
) ? static_cast<void> (0) : __assert_fail ("NNS && \"Missing nested-name-specifier in qualified template name\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7738, __PRETTY_FUNCTION__))
;
7739
7740 // FIXME: Canonicalization?
7741 llvm::FoldingSetNodeID ID;
7742 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
7743
7744 void *InsertPos = nullptr;
7745 QualifiedTemplateName *QTN =
7746 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
7747 if (!QTN) {
7748 QTN = new (*this, alignof(QualifiedTemplateName))
7749 QualifiedTemplateName(NNS, TemplateKeyword, Template);
7750 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
7751 }
7752
7753 return TemplateName(QTN);
7754}
7755
7756/// Retrieve the template name that represents a dependent
7757/// template name such as \c MetaFun::template apply.
7758TemplateName
7759ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
7760 const IdentifierInfo *Name) const {
7761 assert((!NNS || NNS->isDependent()) &&(((!NNS || NNS->isDependent()) && "Nested name specifier must be dependent"
) ? static_cast<void> (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"Nested name specifier must be dependent\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7762, __PRETTY_FUNCTION__))
7762 "Nested name specifier must be dependent")(((!NNS || NNS->isDependent()) && "Nested name specifier must be dependent"
) ? static_cast<void> (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"Nested name specifier must be dependent\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7762, __PRETTY_FUNCTION__))
;
7763
7764 llvm::FoldingSetNodeID ID;
7765 DependentTemplateName::Profile(ID, NNS, Name);
7766
7767 void *InsertPos = nullptr;
7768 DependentTemplateName *QTN =
7769 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
7770
7771 if (QTN)
7772 return TemplateName(QTN);
7773
7774 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
7775 if (CanonNNS == NNS) {
7776 QTN = new (*this, alignof(DependentTemplateName))
7777 DependentTemplateName(NNS, Name);
7778 } else {
7779 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
7780 QTN = new (*this, alignof(DependentTemplateName))
7781 DependentTemplateName(NNS, Name, Canon);
7782 DependentTemplateName *CheckQTN =
7783 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
7784 assert(!CheckQTN && "Dependent type name canonicalization broken")((!CheckQTN && "Dependent type name canonicalization broken"
) ? static_cast<void> (0) : __assert_fail ("!CheckQTN && \"Dependent type name canonicalization broken\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7784, __PRETTY_FUNCTION__))
;
7785 (void)CheckQTN;
7786 }
7787
7788 DependentTemplateNames.InsertNode(QTN, InsertPos);
7789 return TemplateName(QTN);
7790}
7791
7792/// Retrieve the template name that represents a dependent
7793/// template name such as \c MetaFun::template operator+.
7794TemplateName
7795ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
7796 OverloadedOperatorKind Operator) const {
7797 assert((!NNS || NNS->isDependent()) &&(((!NNS || NNS->isDependent()) && "Nested name specifier must be dependent"
) ? static_cast<void> (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"Nested name specifier must be dependent\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7798, __PRETTY_FUNCTION__))
7798 "Nested name specifier must be dependent")(((!NNS || NNS->isDependent()) && "Nested name specifier must be dependent"
) ? static_cast<void> (0) : __assert_fail ("(!NNS || NNS->isDependent()) && \"Nested name specifier must be dependent\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7798, __PRETTY_FUNCTION__))
;
7799
7800 llvm::FoldingSetNodeID ID;
7801 DependentTemplateName::Profile(ID, NNS, Operator);
7802
7803 void *InsertPos = nullptr;
7804 DependentTemplateName *QTN
7805 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
7806
7807 if (QTN)
7808 return TemplateName(QTN);
7809
7810 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
7811 if (CanonNNS == NNS) {
7812 QTN = new (*this, alignof(DependentTemplateName))
7813 DependentTemplateName(NNS, Operator);
7814 } else {
7815 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
7816 QTN = new (*this, alignof(DependentTemplateName))
7817 DependentTemplateName(NNS, Operator, Canon);
7818
7819 DependentTemplateName *CheckQTN
7820 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
7821 assert(!CheckQTN && "Dependent template name canonicalization broken")((!CheckQTN && "Dependent template name canonicalization broken"
) ? static_cast<void> (0) : __assert_fail ("!CheckQTN && \"Dependent template name canonicalization broken\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7821, __PRETTY_FUNCTION__))
;
7822 (void)CheckQTN;
7823 }
7824
7825 DependentTemplateNames.InsertNode(QTN, InsertPos);
7826 return TemplateName(QTN);
7827}
7828
7829TemplateName
7830ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param,
7831 TemplateName replacement) const {
7832 llvm::FoldingSetNodeID ID;
7833 SubstTemplateTemplateParmStorage::Profile(ID, param, replacement);
7834
7835 void *insertPos = nullptr;
7836 SubstTemplateTemplateParmStorage *subst
7837 = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
7838
7839 if (!subst) {
7840 subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement);
7841 SubstTemplateTemplateParms.InsertNode(subst, insertPos);
7842 }
7843
7844 return TemplateName(subst);
7845}
7846
7847TemplateName
7848ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
7849 const TemplateArgument &ArgPack) const {
7850 auto &Self = const_cast<ASTContext &>(*this);
7851 llvm::FoldingSetNodeID ID;
7852 SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
7853
7854 void *InsertPos = nullptr;
7855 SubstTemplateTemplateParmPackStorage *Subst
7856 = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
7857
7858 if (!Subst) {
7859 Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param,
7860 ArgPack.pack_size(),
7861 ArgPack.pack_begin());
7862 SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
7863 }
7864
7865 return TemplateName(Subst);
7866}
7867
7868/// getFromTargetType - Given one of the integer types provided by
7869/// TargetInfo, produce the corresponding type. The unsigned @p Type
7870/// is actually a value of type @c TargetInfo::IntType.
7871CanQualType ASTContext::getFromTargetType(unsigned Type) const {
7872 switch (Type) {
7873 case TargetInfo::NoInt: return {};
7874 case TargetInfo::SignedChar: return SignedCharTy;
7875 case TargetInfo::UnsignedChar: return UnsignedCharTy;
7876 case TargetInfo::SignedShort: return ShortTy;
7877 case TargetInfo::UnsignedShort: return UnsignedShortTy;
7878 case TargetInfo::SignedInt: return IntTy;
7879 case TargetInfo::UnsignedInt: return UnsignedIntTy;
7880 case TargetInfo::SignedLong: return LongTy;
7881 case TargetInfo::UnsignedLong: return UnsignedLongTy;
7882 case TargetInfo::SignedLongLong: return LongLongTy;
7883 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
7884 }
7885
7886 llvm_unreachable("Unhandled TargetInfo::IntType value")::llvm::llvm_unreachable_internal("Unhandled TargetInfo::IntType value"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7886)
;
7887}
7888
7889//===----------------------------------------------------------------------===//
7890// Type Predicates.
7891//===----------------------------------------------------------------------===//
7892
7893/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
7894/// garbage collection attribute.
7895///
7896Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
7897 if (getLangOpts().getGC() == LangOptions::NonGC)
7898 return Qualifiers::GCNone;
7899
7900 assert(getLangOpts().ObjC)((getLangOpts().ObjC) ? static_cast<void> (0) : __assert_fail
("getLangOpts().ObjC", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7900, __PRETTY_FUNCTION__))
;
7901 Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
7902
7903 // Default behaviour under objective-C's gc is for ObjC pointers
7904 // (or pointers to them) be treated as though they were declared
7905 // as __strong.
7906 if (GCAttrs == Qualifiers::GCNone) {
7907 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
7908 return Qualifiers::Strong;
7909 else if (Ty->isPointerType())
7910 return getObjCGCAttrKind(Ty->castAs<PointerType>()->getPointeeType());
7911 } else {
7912 // It's not valid to set GC attributes on anything that isn't a
7913 // pointer.
7914#ifndef NDEBUG
7915 QualType CT = Ty->getCanonicalTypeInternal();
7916 while (const auto *AT = dyn_cast<ArrayType>(CT))
7917 CT = AT->getElementType();
7918 assert(CT->isAnyPointerType() || CT->isBlockPointerType())((CT->isAnyPointerType() || CT->isBlockPointerType()) ?
static_cast<void> (0) : __assert_fail ("CT->isAnyPointerType() || CT->isBlockPointerType()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7918, __PRETTY_FUNCTION__))
;
7919#endif
7920 }
7921 return GCAttrs;
7922}
7923
7924//===----------------------------------------------------------------------===//
7925// Type Compatibility Testing
7926//===----------------------------------------------------------------------===//
7927
7928/// areCompatVectorTypes - Return true if the two specified vector types are
7929/// compatible.
7930static bool areCompatVectorTypes(const VectorType *LHS,
7931 const VectorType *RHS) {
7932 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified())((LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified
()) ? static_cast<void> (0) : __assert_fail ("LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7932, __PRETTY_FUNCTION__))
;
7933 return LHS->getElementType() == RHS->getElementType() &&
7934 LHS->getNumElements() == RHS->getNumElements();
7935}
7936
7937bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
7938 QualType SecondVec) {
7939 assert(FirstVec->isVectorType() && "FirstVec should be a vector type")((FirstVec->isVectorType() && "FirstVec should be a vector type"
) ? static_cast<void> (0) : __assert_fail ("FirstVec->isVectorType() && \"FirstVec should be a vector type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7939, __PRETTY_FUNCTION__))
;
7940 assert(SecondVec->isVectorType() && "SecondVec should be a vector type")((SecondVec->isVectorType() && "SecondVec should be a vector type"
) ? static_cast<void> (0) : __assert_fail ("SecondVec->isVectorType() && \"SecondVec should be a vector type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 7940, __PRETTY_FUNCTION__))
;
7941
7942 if (hasSameUnqualifiedType(FirstVec, SecondVec))
7943 return true;
7944
7945 // Treat Neon vector types and most AltiVec vector types as if they are the
7946 // equivalent GCC vector types.
7947 const auto *First = FirstVec->castAs<VectorType>();
7948 const auto *Second = SecondVec->castAs<VectorType>();
7949 if (First->getNumElements() == Second->getNumElements() &&
7950 hasSameType(First->getElementType(), Second->getElementType()) &&
7951 First->getVectorKind() != VectorType::AltiVecPixel &&
7952 First->getVectorKind() != VectorType::AltiVecBool &&
7953 Second->getVectorKind() != VectorType::AltiVecPixel &&
7954 Second->getVectorKind() != VectorType::AltiVecBool)
7955 return true;
7956
7957 return false;
7958}
7959
7960bool ASTContext::hasDirectOwnershipQualifier(QualType Ty) const {
7961 while (true) {
7962 // __strong id
7963 if (const AttributedType *Attr = dyn_cast<AttributedType>(Ty)) {
7964 if (Attr->getAttrKind() == attr::ObjCOwnership)
7965 return true;
7966
7967 Ty = Attr->getModifiedType();
7968
7969 // X *__strong (...)
7970 } else if (const ParenType *Paren = dyn_cast<ParenType>(Ty)) {
7971 Ty = Paren->getInnerType();
7972
7973 // We do not want to look through typedefs, typeof(expr),
7974 // typeof(type), or any other way that the type is somehow
7975 // abstracted.
7976 } else {
7977 return false;
7978 }
7979 }
7980}
7981
7982//===----------------------------------------------------------------------===//
7983// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
7984//===----------------------------------------------------------------------===//
7985
7986/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
7987/// inheritance hierarchy of 'rProto'.
7988bool
7989ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
7990 ObjCProtocolDecl *rProto) const {
7991 if (declaresSameEntity(lProto, rProto))
7992 return true;
7993 for (auto *PI : rProto->protocols())
7994 if (ProtocolCompatibleWithProtocol(lProto, PI))
7995 return true;
7996 return false;
7997}
7998
7999/// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and
8000/// Class<pr1, ...>.
8001bool ASTContext::ObjCQualifiedClassTypesAreCompatible(
8002 const ObjCObjectPointerType *lhs, const ObjCObjectPointerType *rhs) {
8003 for (auto *lhsProto : lhs->quals()) {
8004 bool match = false;
8005 for (auto *rhsProto : rhs->quals()) {
8006 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
8007 match = true;
8008 break;
8009 }
8010 }
8011 if (!match)
8012 return false;
8013 }
8014 return true;
8015}
8016
8017/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
8018/// ObjCQualifiedIDType.
8019bool ASTContext::ObjCQualifiedIdTypesAreCompatible(
8020 const ObjCObjectPointerType *lhs, const ObjCObjectPointerType *rhs,
8021 bool compare) {
8022 // Allow id<P..> and an 'id' or void* type in all cases.
8023 if (lhs->isVoidPointerType() ||
8024 lhs->isObjCIdType() || lhs->isObjCClassType())
8025 return true;
8026 else if (rhs->isVoidPointerType() ||
8027 rhs->isObjCIdType() || rhs->isObjCClassType())
8028 return true;
8029
8030 if (lhs->isObjCQualifiedIdType()) {
8031 if (rhs->qual_empty()) {
8032 // If the RHS is a unqualified interface pointer "NSString*",
8033 // make sure we check the class hierarchy.
8034 if (ObjCInterfaceDecl *rhsID = rhs->getInterfaceDecl()) {
8035 for (auto *I : lhs->quals()) {
8036 // when comparing an id<P> on lhs with a static type on rhs,
8037 // see if static class implements all of id's protocols, directly or
8038 // through its super class and categories.
8039 if (!rhsID->ClassImplementsProtocol(I, true))
8040 return false;
8041 }
8042 }
8043 // If there are no qualifiers and no interface, we have an 'id'.
8044 return true;
8045 }
8046 // Both the right and left sides have qualifiers.
8047 for (auto *lhsProto : lhs->quals()) {
8048 bool match = false;
8049
8050 // when comparing an id<P> on lhs with a static type on rhs,
8051 // see if static class implements all of id's protocols, directly or
8052 // through its super class and categories.
8053 for (auto *rhsProto : rhs->quals()) {
8054 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
8055 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
8056 match = true;
8057 break;
8058 }
8059 }
8060 // If the RHS is a qualified interface pointer "NSString<P>*",
8061 // make sure we check the class hierarchy.
8062 if (ObjCInterfaceDecl *rhsID = rhs->getInterfaceDecl()) {
8063 for (auto *I : lhs->quals()) {
8064 // when comparing an id<P> on lhs with a static type on rhs,
8065 // see if static class implements all of id's protocols, directly or
8066 // through its super class and categories.
8067 if (rhsID->ClassImplementsProtocol(I, true)) {
8068 match = true;
8069 break;
8070 }
8071 }
8072 }
8073 if (!match)
8074 return false;
8075 }
8076
8077 return true;
8078 }
8079
8080 assert(rhs->isObjCQualifiedIdType() && "One of the LHS/RHS should be id<x>")((rhs->isObjCQualifiedIdType() && "One of the LHS/RHS should be id<x>"
) ? static_cast<void> (0) : __assert_fail ("rhs->isObjCQualifiedIdType() && \"One of the LHS/RHS should be id<x>\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8080, __PRETTY_FUNCTION__))
;
8081
8082 if (lhs->getInterfaceType()) {
8083 // If both the right and left sides have qualifiers.
8084 for (auto *lhsProto : lhs->quals()) {
8085 bool match = false;
8086
8087 // when comparing an id<P> on rhs with a static type on lhs,
8088 // see if static class implements all of id's protocols, directly or
8089 // through its super class and categories.
8090 // First, lhs protocols in the qualifier list must be found, direct
8091 // or indirect in rhs's qualifier list or it is a mismatch.
8092 for (auto *rhsProto : rhs->quals()) {
8093 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
8094 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
8095 match = true;
8096 break;
8097 }
8098 }
8099 if (!match)
8100 return false;
8101 }
8102
8103 // Static class's protocols, or its super class or category protocols
8104 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
8105 if (ObjCInterfaceDecl *lhsID = lhs->getInterfaceDecl()) {
8106 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
8107 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
8108 // This is rather dubious but matches gcc's behavior. If lhs has
8109 // no type qualifier and its class has no static protocol(s)
8110 // assume that it is mismatch.
8111 if (LHSInheritedProtocols.empty() && lhs->qual_empty())
8112 return false;
8113 for (auto *lhsProto : LHSInheritedProtocols) {
8114 bool match = false;
8115 for (auto *rhsProto : rhs->quals()) {
8116 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
8117 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
8118 match = true;
8119 break;
8120 }
8121 }
8122 if (!match)
8123 return false;
8124 }
8125 }
8126 return true;
8127 }
8128 return false;
8129}
8130
8131/// canAssignObjCInterfaces - Return true if the two interface types are
8132/// compatible for assignment from RHS to LHS. This handles validation of any
8133/// protocol qualifiers on the LHS or RHS.
8134bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
8135 const ObjCObjectPointerType *RHSOPT) {
8136 const ObjCObjectType* LHS = LHSOPT->getObjectType();
8137 const ObjCObjectType* RHS = RHSOPT->getObjectType();
8138
8139 // If either type represents the built-in 'id' or 'Class' types, return true.
8140 if (LHS->isObjCUnqualifiedIdOrClass() ||
8141 RHS->isObjCUnqualifiedIdOrClass())
8142 return true;
8143
8144 // Function object that propagates a successful result or handles
8145 // __kindof types.
8146 auto finish = [&](bool succeeded) -> bool {
8147 if (succeeded)
8148 return true;
8149
8150 if (!RHS->isKindOfType())
8151 return false;
8152
8153 // Strip off __kindof and protocol qualifiers, then check whether
8154 // we can assign the other way.
8155 return canAssignObjCInterfaces(RHSOPT->stripObjCKindOfTypeAndQuals(*this),
8156 LHSOPT->stripObjCKindOfTypeAndQuals(*this));
8157 };
8158
8159 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) {
8160 return finish(ObjCQualifiedIdTypesAreCompatible(LHSOPT, RHSOPT, false));
8161 }
8162
8163 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) {
8164 return finish(ObjCQualifiedClassTypesAreCompatible(LHSOPT, RHSOPT));
8165 }
8166
8167 // If we have 2 user-defined types, fall into that path.
8168 if (LHS->getInterface() && RHS->getInterface()) {
8169 return finish(canAssignObjCInterfaces(LHS, RHS));
8170 }
8171
8172 return false;
8173}
8174
8175/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
8176/// for providing type-safety for objective-c pointers used to pass/return
8177/// arguments in block literals. When passed as arguments, passing 'A*' where
8178/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
8179/// not OK. For the return type, the opposite is not OK.
8180bool ASTContext::canAssignObjCInterfacesInBlockPointer(
8181 const ObjCObjectPointerType *LHSOPT,
8182 const ObjCObjectPointerType *RHSOPT,
8183 bool BlockReturnType) {
8184
8185 // Function object that propagates a successful result or handles
8186 // __kindof types.
8187 auto finish = [&](bool succeeded) -> bool {
8188 if (succeeded)
8189 return true;
8190
8191 const ObjCObjectPointerType *Expected = BlockReturnType ? RHSOPT : LHSOPT;
8192 if (!Expected->isKindOfType())
8193 return false;
8194
8195 // Strip off __kindof and protocol qualifiers, then check whether
8196 // we can assign the other way.
8197 return canAssignObjCInterfacesInBlockPointer(
8198 RHSOPT->stripObjCKindOfTypeAndQuals(*this),
8199 LHSOPT->stripObjCKindOfTypeAndQuals(*this),
8200 BlockReturnType);
8201 };
8202
8203 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
47
Called C++ object pointer is null
8204 return true;
8205
8206 if (LHSOPT->isObjCBuiltinType()) {
8207 return finish(RHSOPT->isObjCBuiltinType() ||
8208 RHSOPT->isObjCQualifiedIdType());
8209 }
8210
8211 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
8212 return finish(ObjCQualifiedIdTypesAreCompatible(
8213 (BlockReturnType ? LHSOPT : RHSOPT),
8214 (BlockReturnType ? RHSOPT : LHSOPT), false));
8215
8216 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
8217 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
8218 if (LHS && RHS) { // We have 2 user-defined types.
8219 if (LHS != RHS) {
8220 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
8221 return finish(BlockReturnType);
8222 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
8223 return finish(!BlockReturnType);
8224 }
8225 else
8226 return true;
8227 }
8228 return false;
8229}
8230
8231/// Comparison routine for Objective-C protocols to be used with
8232/// llvm::array_pod_sort.
8233static int compareObjCProtocolsByName(ObjCProtocolDecl * const *lhs,
8234 ObjCProtocolDecl * const *rhs) {
8235 return (*lhs)->getName().compare((*rhs)->getName());
8236}
8237
8238/// getIntersectionOfProtocols - This routine finds the intersection of set
8239/// of protocols inherited from two distinct objective-c pointer objects with
8240/// the given common base.
8241/// It is used to build composite qualifier list of the composite type of
8242/// the conditional expression involving two objective-c pointer objects.
8243static
8244void getIntersectionOfProtocols(ASTContext &Context,
8245 const ObjCInterfaceDecl *CommonBase,
8246 const ObjCObjectPointerType *LHSOPT,
8247 const ObjCObjectPointerType *RHSOPT,
8248 SmallVectorImpl<ObjCProtocolDecl *> &IntersectionSet) {
8249
8250 const ObjCObjectType* LHS = LHSOPT->getObjectType();
8251 const ObjCObjectType* RHS = RHSOPT->getObjectType();
8252 assert(LHS->getInterface() && "LHS must have an interface base")((LHS->getInterface() && "LHS must have an interface base"
) ? static_cast<void> (0) : __assert_fail ("LHS->getInterface() && \"LHS must have an interface base\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8252, __PRETTY_FUNCTION__))
;
8253 assert(RHS->getInterface() && "RHS must have an interface base")((RHS->getInterface() && "RHS must have an interface base"
) ? static_cast<void> (0) : __assert_fail ("RHS->getInterface() && \"RHS must have an interface base\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8253, __PRETTY_FUNCTION__))
;
8254
8255 // Add all of the protocols for the LHS.
8256 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSProtocolSet;
8257
8258 // Start with the protocol qualifiers.
8259 for (auto proto : LHS->quals()) {
8260 Context.CollectInheritedProtocols(proto, LHSProtocolSet);
8261 }
8262
8263 // Also add the protocols associated with the LHS interface.
8264 Context.CollectInheritedProtocols(LHS->getInterface(), LHSProtocolSet);
8265
8266 // Add all of the protocols for the RHS.
8267 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSProtocolSet;
8268
8269 // Start with the protocol qualifiers.
8270 for (auto proto : RHS->quals()) {
8271 Context.CollectInheritedProtocols(proto, RHSProtocolSet);
8272 }
8273
8274 // Also add the protocols associated with the RHS interface.
8275 Context.CollectInheritedProtocols(RHS->getInterface(), RHSProtocolSet);
8276
8277 // Compute the intersection of the collected protocol sets.
8278 for (auto proto : LHSProtocolSet) {
8279 if (RHSProtocolSet.count(proto))
8280 IntersectionSet.push_back(proto);
8281 }
8282
8283 // Compute the set of protocols that is implied by either the common type or
8284 // the protocols within the intersection.
8285 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ImpliedProtocols;
8286 Context.CollectInheritedProtocols(CommonBase, ImpliedProtocols);
8287
8288 // Remove any implied protocols from the list of inherited protocols.
8289 if (!ImpliedProtocols.empty()) {
8290 IntersectionSet.erase(
8291 std::remove_if(IntersectionSet.begin(),
8292 IntersectionSet.end(),
8293 [&](ObjCProtocolDecl *proto) -> bool {
8294 return ImpliedProtocols.count(proto) > 0;
8295 }),
8296 IntersectionSet.end());
8297 }
8298
8299 // Sort the remaining protocols by name.
8300 llvm::array_pod_sort(IntersectionSet.begin(), IntersectionSet.end(),
8301 compareObjCProtocolsByName);
8302}
8303
8304/// Determine whether the first type is a subtype of the second.
8305static bool canAssignObjCObjectTypes(ASTContext &ctx, QualType lhs,
8306 QualType rhs) {
8307 // Common case: two object pointers.
8308 const auto *lhsOPT = lhs->getAs<ObjCObjectPointerType>();
8309 const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
8310 if (lhsOPT && rhsOPT)
8311 return ctx.canAssignObjCInterfaces(lhsOPT, rhsOPT);
8312
8313 // Two block pointers.
8314 const auto *lhsBlock = lhs->getAs<BlockPointerType>();
8315 const auto *rhsBlock = rhs->getAs<BlockPointerType>();
8316 if (lhsBlock && rhsBlock)
8317 return ctx.typesAreBlockPointerCompatible(lhs, rhs);
8318
8319 // If either is an unqualified 'id' and the other is a block, it's
8320 // acceptable.
8321 if ((lhsOPT && lhsOPT->isObjCIdType() && rhsBlock) ||
8322 (rhsOPT && rhsOPT->isObjCIdType() && lhsBlock))
8323 return true;
8324
8325 return false;
8326}
8327
8328// Check that the given Objective-C type argument lists are equivalent.
8329static bool sameObjCTypeArgs(ASTContext &ctx,
8330 const ObjCInterfaceDecl *iface,
8331 ArrayRef<QualType> lhsArgs,
8332 ArrayRef<QualType> rhsArgs,
8333 bool stripKindOf) {
8334 if (lhsArgs.size() != rhsArgs.size())
8335 return false;
8336
8337 ObjCTypeParamList *typeParams = iface->getTypeParamList();
8338 for (unsigned i = 0, n = lhsArgs.size(); i != n; ++i) {
8339 if (ctx.hasSameType(lhsArgs[i], rhsArgs[i]))
8340 continue;
8341
8342 switch (typeParams->begin()[i]->getVariance()) {
8343 case ObjCTypeParamVariance::Invariant:
8344 if (!stripKindOf ||
8345 !ctx.hasSameType(lhsArgs[i].stripObjCKindOfType(ctx),
8346 rhsArgs[i].stripObjCKindOfType(ctx))) {
8347 return false;
8348 }
8349 break;
8350
8351 case ObjCTypeParamVariance::Covariant:
8352 if (!canAssignObjCObjectTypes(ctx, lhsArgs[i], rhsArgs[i]))
8353 return false;
8354 break;
8355
8356 case ObjCTypeParamVariance::Contravariant:
8357 if (!canAssignObjCObjectTypes(ctx, rhsArgs[i], lhsArgs[i]))
8358 return false;
8359 break;
8360 }
8361 }
8362
8363 return true;
8364}
8365
8366QualType ASTContext::areCommonBaseCompatible(
8367 const ObjCObjectPointerType *Lptr,
8368 const ObjCObjectPointerType *Rptr) {
8369 const ObjCObjectType *LHS = Lptr->getObjectType();
8370 const ObjCObjectType *RHS = Rptr->getObjectType();
8371 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
8372 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
8373
8374 if (!LDecl || !RDecl)
8375 return {};
8376
8377 // When either LHS or RHS is a kindof type, we should return a kindof type.
8378 // For example, for common base of kindof(ASub1) and kindof(ASub2), we return
8379 // kindof(A).
8380 bool anyKindOf = LHS->isKindOfType() || RHS->isKindOfType();
8381
8382 // Follow the left-hand side up the class hierarchy until we either hit a
8383 // root or find the RHS. Record the ancestors in case we don't find it.
8384 llvm::SmallDenseMap<const ObjCInterfaceDecl *, const ObjCObjectType *, 4>
8385 LHSAncestors;
8386 while (true) {
8387 // Record this ancestor. We'll need this if the common type isn't in the
8388 // path from the LHS to the root.
8389 LHSAncestors[LHS->getInterface()->getCanonicalDecl()] = LHS;
8390
8391 if (declaresSameEntity(LHS->getInterface(), RDecl)) {
8392 // Get the type arguments.
8393 ArrayRef<QualType> LHSTypeArgs = LHS->getTypeArgsAsWritten();
8394 bool anyChanges = false;
8395 if (LHS->isSpecialized() && RHS->isSpecialized()) {
8396 // Both have type arguments, compare them.
8397 if (!sameObjCTypeArgs(*this, LHS->getInterface(),
8398 LHS->getTypeArgs(), RHS->getTypeArgs(),
8399 /*stripKindOf=*/true))
8400 return {};
8401 } else if (LHS->isSpecialized() != RHS->isSpecialized()) {
8402 // If only one has type arguments, the result will not have type
8403 // arguments.
8404 LHSTypeArgs = {};
8405 anyChanges = true;
8406 }
8407
8408 // Compute the intersection of protocols.
8409 SmallVector<ObjCProtocolDecl *, 8> Protocols;
8410 getIntersectionOfProtocols(*this, LHS->getInterface(), Lptr, Rptr,
8411 Protocols);
8412 if (!Protocols.empty())
8413 anyChanges = true;
8414
8415 // If anything in the LHS will have changed, build a new result type.
8416 // If we need to return a kindof type but LHS is not a kindof type, we
8417 // build a new result type.
8418 if (anyChanges || LHS->isKindOfType() != anyKindOf) {
8419 QualType Result = getObjCInterfaceType(LHS->getInterface());
8420 Result = getObjCObjectType(Result, LHSTypeArgs, Protocols,
8421 anyKindOf || LHS->isKindOfType());
8422 return getObjCObjectPointerType(Result);
8423 }
8424
8425 return getObjCObjectPointerType(QualType(LHS, 0));
8426 }
8427
8428 // Find the superclass.
8429 QualType LHSSuperType = LHS->getSuperClassType();
8430 if (LHSSuperType.isNull())
8431 break;
8432
8433 LHS = LHSSuperType->castAs<ObjCObjectType>();
8434 }
8435
8436 // We didn't find anything by following the LHS to its root; now check
8437 // the RHS against the cached set of ancestors.
8438 while (true) {
8439 auto KnownLHS = LHSAncestors.find(RHS->getInterface()->getCanonicalDecl());
8440 if (KnownLHS != LHSAncestors.end()) {
8441 LHS = KnownLHS->second;
8442
8443 // Get the type arguments.
8444 ArrayRef<QualType> RHSTypeArgs = RHS->getTypeArgsAsWritten();
8445 bool anyChanges = false;
8446 if (LHS->isSpecialized() && RHS->isSpecialized()) {
8447 // Both have type arguments, compare them.
8448 if (!sameObjCTypeArgs(*this, LHS->getInterface(),
8449 LHS->getTypeArgs(), RHS->getTypeArgs(),
8450 /*stripKindOf=*/true))
8451 return {};
8452 } else if (LHS->isSpecialized() != RHS->isSpecialized()) {
8453 // If only one has type arguments, the result will not have type
8454 // arguments.
8455 RHSTypeArgs = {};
8456 anyChanges = true;
8457 }
8458
8459 // Compute the intersection of protocols.
8460 SmallVector<ObjCProtocolDecl *, 8> Protocols;
8461 getIntersectionOfProtocols(*this, RHS->getInterface(), Lptr, Rptr,
8462 Protocols);
8463 if (!Protocols.empty())
8464 anyChanges = true;
8465
8466 // If we need to return a kindof type but RHS is not a kindof type, we
8467 // build a new result type.
8468 if (anyChanges || RHS->isKindOfType() != anyKindOf) {
8469 QualType Result = getObjCInterfaceType(RHS->getInterface());
8470 Result = getObjCObjectType(Result, RHSTypeArgs, Protocols,
8471 anyKindOf || RHS->isKindOfType());
8472 return getObjCObjectPointerType(Result);
8473 }
8474
8475 return getObjCObjectPointerType(QualType(RHS, 0));
8476 }
8477
8478 // Find the superclass of the RHS.
8479 QualType RHSSuperType = RHS->getSuperClassType();
8480 if (RHSSuperType.isNull())
8481 break;
8482
8483 RHS = RHSSuperType->castAs<ObjCObjectType>();
8484 }
8485
8486 return {};
8487}
8488
8489bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
8490 const ObjCObjectType *RHS) {
8491 assert(LHS->getInterface() && "LHS is not an interface type")((LHS->getInterface() && "LHS is not an interface type"
) ? static_cast<void> (0) : __assert_fail ("LHS->getInterface() && \"LHS is not an interface type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8491, __PRETTY_FUNCTION__))
;
8492 assert(RHS->getInterface() && "RHS is not an interface type")((RHS->getInterface() && "RHS is not an interface type"
) ? static_cast<void> (0) : __assert_fail ("RHS->getInterface() && \"RHS is not an interface type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8492, __PRETTY_FUNCTION__))
;
8493
8494 // Verify that the base decls are compatible: the RHS must be a subclass of
8495 // the LHS.
8496 ObjCInterfaceDecl *LHSInterface = LHS->getInterface();
8497 bool IsSuperClass = LHSInterface->isSuperClassOf(RHS->getInterface());
8498 if (!IsSuperClass)
8499 return false;
8500
8501 // If the LHS has protocol qualifiers, determine whether all of them are
8502 // satisfied by the RHS (i.e., the RHS has a superset of the protocols in the
8503 // LHS).
8504 if (LHS->getNumProtocols() > 0) {
8505 // OK if conversion of LHS to SuperClass results in narrowing of types
8506 // ; i.e., SuperClass may implement at least one of the protocols
8507 // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
8508 // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
8509 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
8510 CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
8511 // Also, if RHS has explicit quelifiers, include them for comparing with LHS's
8512 // qualifiers.
8513 for (auto *RHSPI : RHS->quals())
8514 CollectInheritedProtocols(RHSPI, SuperClassInheritedProtocols);
8515 // If there is no protocols associated with RHS, it is not a match.
8516 if (SuperClassInheritedProtocols.empty())
8517 return false;
8518
8519 for (const auto *LHSProto : LHS->quals()) {
8520 bool SuperImplementsProtocol = false;
8521 for (auto *SuperClassProto : SuperClassInheritedProtocols)
8522 if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
8523 SuperImplementsProtocol = true;
8524 break;
8525 }
8526 if (!SuperImplementsProtocol)
8527 return false;
8528 }
8529 }
8530
8531 // If the LHS is specialized, we may need to check type arguments.
8532 if (LHS->isSpecialized()) {
8533 // Follow the superclass chain until we've matched the LHS class in the
8534 // hierarchy. This substitutes type arguments through.
8535 const ObjCObjectType *RHSSuper = RHS;
8536 while (!declaresSameEntity(RHSSuper->getInterface(), LHSInterface))
8537 RHSSuper = RHSSuper->getSuperClassType()->castAs<ObjCObjectType>();
8538
8539 // If the RHS is specializd, compare type arguments.
8540 if (RHSSuper->isSpecialized() &&
8541 !sameObjCTypeArgs(*this, LHS->getInterface(),
8542 LHS->getTypeArgs(), RHSSuper->getTypeArgs(),
8543 /*stripKindOf=*/true)) {
8544 return false;
8545 }
8546 }
8547
8548 return true;
8549}
8550
8551bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
8552 // get the "pointed to" types
8553 const auto *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
8554 const auto *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
8555
8556 if (!LHSOPT || !RHSOPT)
8557 return false;
8558
8559 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
8560 canAssignObjCInterfaces(RHSOPT, LHSOPT);
8561}
8562
8563bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
8564 return canAssignObjCInterfaces(
8565 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
8566 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
8567}
8568
8569/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
8570/// both shall have the identically qualified version of a compatible type.
8571/// C99 6.2.7p1: Two types have compatible types if their types are the
8572/// same. See 6.7.[2,3,5] for additional rules.
8573bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
8574 bool CompareUnqualified) {
8575 if (getLangOpts().CPlusPlus)
8576 return hasSameType(LHS, RHS);
8577
8578 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
8579}
8580
8581bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) {
8582 return typesAreCompatible(LHS, RHS);
8583}
8584
8585bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
8586 return !mergeTypes(LHS, RHS, true).isNull();
8587}
8588
8589/// mergeTransparentUnionType - if T is a transparent union type and a member
8590/// of T is compatible with SubType, return the merged type, else return
8591/// QualType()
8592QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
8593 bool OfBlockPointer,
8594 bool Unqualified) {
8595 if (const RecordType *UT = T->getAsUnionType()) {
8596 RecordDecl *UD = UT->getDecl();
8597 if (UD->hasAttr<TransparentUnionAttr>()) {
8598 for (const auto *I : UD->fields()) {
8599 QualType ET = I->getType().getUnqualifiedType();
8600 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
8601 if (!MT.isNull())
8602 return MT;
8603 }
8604 }
8605 }
8606
8607 return {};
8608}
8609
8610/// mergeFunctionParameterTypes - merge two types which appear as function
8611/// parameter types
8612QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs,
8613 bool OfBlockPointer,
8614 bool Unqualified) {
8615 // GNU extension: two types are compatible if they appear as a function
8616 // argument, one of the types is a transparent union type and the other
8617 // type is compatible with a union member
8618 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
8619 Unqualified);
8620 if (!lmerge.isNull())
8621 return lmerge;
8622
8623 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
8624 Unqualified);
8625 if (!rmerge.isNull())
8626 return rmerge;
8627
8628 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
8629}
8630
8631QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
8632 bool OfBlockPointer,
8633 bool Unqualified) {
8634 const auto *lbase = lhs->getAs<FunctionType>();
8635 const auto *rbase = rhs->getAs<FunctionType>();
8636 const auto *lproto = dyn_cast<FunctionProtoType>(lbase);
8637 const auto *rproto = dyn_cast<FunctionProtoType>(rbase);
8638 bool allLTypes = true;
8639 bool allRTypes = true;
8640
8641 // Check return type
8642 QualType retType;
8643 if (OfBlockPointer) {
8644 QualType RHS = rbase->getReturnType();
8645 QualType LHS = lbase->getReturnType();
8646 bool UnqualifiedResult = Unqualified;
8647 if (!UnqualifiedResult)
8648 UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
8649 retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
8650 }
8651 else
8652 retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), false,
8653 Unqualified);
8654 if (retType.isNull())
8655 return {};
8656
8657 if (Unqualified)
8658 retType = retType.getUnqualifiedType();
8659
8660 CanQualType LRetType = getCanonicalType(lbase->getReturnType());
8661 CanQualType RRetType = getCanonicalType(rbase->getReturnType());
8662 if (Unqualified) {
8663 LRetType = LRetType.getUnqualifiedType();
8664 RRetType = RRetType.getUnqualifiedType();
8665 }
8666
8667 if (getCanonicalType(retType) != LRetType)
8668 allLTypes = false;
8669 if (getCanonicalType(retType) != RRetType)
8670 allRTypes = false;
8671
8672 // FIXME: double check this
8673 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
8674 // rbase->getRegParmAttr() != 0 &&
8675 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
8676 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
8677 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
8678
8679 // Compatible functions must have compatible calling conventions
8680 if (lbaseInfo.getCC() != rbaseInfo.getCC())
8681 return {};
8682
8683 // Regparm is part of the calling convention.
8684 if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
8685 return {};
8686 if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
8687 return {};
8688
8689 if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult())
8690 return {};
8691 if (lbaseInfo.getNoCallerSavedRegs() != rbaseInfo.getNoCallerSavedRegs())
8692 return {};
8693 if (lbaseInfo.getNoCfCheck() != rbaseInfo.getNoCfCheck())
8694 return {};
8695
8696 // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
8697 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
8698
8699 if (lbaseInfo.getNoReturn() != NoReturn)
8700 allLTypes = false;
8701 if (rbaseInfo.getNoReturn() != NoReturn)
8702 allRTypes = false;
8703
8704 FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn);
8705
8706 if (lproto && rproto) { // two C99 style function prototypes
8707 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&((!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec
() && "C++ shouldn't be here") ? static_cast<void>
(0) : __assert_fail ("!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && \"C++ shouldn't be here\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8708, __PRETTY_FUNCTION__))
8708 "C++ shouldn't be here")((!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec
() && "C++ shouldn't be here") ? static_cast<void>
(0) : __assert_fail ("!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && \"C++ shouldn't be here\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8708, __PRETTY_FUNCTION__))
;
8709 // Compatible functions must have the same number of parameters
8710 if (lproto->getNumParams() != rproto->getNumParams())
8711 return {};
8712
8713 // Variadic and non-variadic functions aren't compatible
8714 if (lproto->isVariadic() != rproto->isVariadic())
8715 return {};
8716
8717 if (lproto->getMethodQuals() != rproto->getMethodQuals())
8718 return {};
8719
8720 SmallVector<FunctionProtoType::ExtParameterInfo, 4> newParamInfos;
8721 bool canUseLeft, canUseRight;
8722 if (!mergeExtParameterInfo(lproto, rproto, canUseLeft, canUseRight,
8723 newParamInfos))
8724 return {};
8725
8726 if (!canUseLeft)
8727 allLTypes = false;
8728 if (!canUseRight)
8729 allRTypes = false;
8730
8731 // Check parameter type compatibility
8732 SmallVector<QualType, 10> types;
8733 for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) {
8734 QualType lParamType = lproto->getParamType(i).getUnqualifiedType();
8735 QualType rParamType = rproto->getParamType(i).getUnqualifiedType();
8736 QualType paramType = mergeFunctionParameterTypes(
8737 lParamType, rParamType, OfBlockPointer, Unqualified);
8738 if (paramType.isNull())
8739 return {};
8740
8741 if (Unqualified)
8742 paramType = paramType.getUnqualifiedType();
8743
8744 types.push_back(paramType);
8745 if (Unqualified) {
8746 lParamType = lParamType.getUnqualifiedType();
8747 rParamType = rParamType.getUnqualifiedType();
8748 }
8749
8750 if (getCanonicalType(paramType) != getCanonicalType(lParamType))
8751 allLTypes = false;
8752 if (getCanonicalType(paramType) != getCanonicalType(rParamType))
8753 allRTypes = false;
8754 }
8755
8756 if (allLTypes) return lhs;
8757 if (allRTypes) return rhs;
8758
8759 FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
8760 EPI.ExtInfo = einfo;
8761 EPI.ExtParameterInfos =
8762 newParamInfos.empty() ? nullptr : newParamInfos.data();
8763 return getFunctionType(retType, types, EPI);
8764 }
8765
8766 if (lproto) allRTypes = false;
8767 if (rproto) allLTypes = false;
8768
8769 const FunctionProtoType *proto = lproto ? lproto : rproto;
8770 if (proto) {
8771 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here")((!proto->hasExceptionSpec() && "C++ shouldn't be here"
) ? static_cast<void> (0) : __assert_fail ("!proto->hasExceptionSpec() && \"C++ shouldn't be here\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8771, __PRETTY_FUNCTION__))
;
8772 if (proto->isVariadic())
8773 return {};
8774 // Check that the types are compatible with the types that
8775 // would result from default argument promotions (C99 6.7.5.3p15).
8776 // The only types actually affected are promotable integer
8777 // types and floats, which would be passed as a different
8778 // type depending on whether the prototype is visible.
8779 for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) {
8780 QualType paramTy = proto->getParamType(i);
8781
8782 // Look at the converted type of enum types, since that is the type used
8783 // to pass enum values.
8784 if (const auto *Enum = paramTy->getAs<EnumType>()) {
8785 paramTy = Enum->getDecl()->getIntegerType();
8786 if (paramTy.isNull())
8787 return {};
8788 }
8789
8790 if (paramTy->isPromotableIntegerType() ||
8791 getCanonicalType(paramTy).getUnqualifiedType() == FloatTy)
8792 return {};
8793 }
8794
8795 if (allLTypes) return lhs;
8796 if (allRTypes) return rhs;
8797
8798 FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
8799 EPI.ExtInfo = einfo;
8800 return getFunctionType(retType, proto->getParamTypes(), EPI);
8801 }
8802
8803 if (allLTypes) return lhs;
8804 if (allRTypes) return rhs;
8805 return getFunctionNoProtoType(retType, einfo);
8806}
8807
8808/// Given that we have an enum type and a non-enum type, try to merge them.
8809static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET,
8810 QualType other, bool isBlockReturnType) {
8811 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
8812 // a signed integer type, or an unsigned integer type.
8813 // Compatibility is based on the underlying type, not the promotion
8814 // type.
8815 QualType underlyingType = ET->getDecl()->getIntegerType();
8816 if (underlyingType.isNull())
8817 return {};
8818 if (Context.hasSameType(underlyingType, other))
8819 return other;
8820
8821 // In block return types, we're more permissive and accept any
8822 // integral type of the same size.
8823 if (isBlockReturnType && other->isIntegerType() &&
8824 Context.getTypeSize(underlyingType) == Context.getTypeSize(other))
8825 return other;
8826
8827 return {};
8828}
8829
8830QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
8831 bool OfBlockPointer,
8832 bool Unqualified, bool BlockReturnType) {
8833 // C++ [expr]: If an expression initially has the type "reference to T", the
8834 // type is adjusted to "T" prior to any further analysis, the expression
8835 // designates the object or function denoted by the reference, and the
8836 // expression is an lvalue unless the reference is an rvalue reference and
8837 // the expression is a function call (possibly inside parentheses).
8838 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?")((!LHS->getAs<ReferenceType>() && "LHS is a reference type?"
) ? static_cast<void> (0) : __assert_fail ("!LHS->getAs<ReferenceType>() && \"LHS is a reference type?\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8838, __PRETTY_FUNCTION__))
;
1
Assuming the object is not a 'ReferenceType'
2
'?' condition is true
8839 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?")((!RHS->getAs<ReferenceType>() && "RHS is a reference type?"
) ? static_cast<void> (0) : __assert_fail ("!RHS->getAs<ReferenceType>() && \"RHS is a reference type?\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8839, __PRETTY_FUNCTION__))
;
3
Assuming the object is not a 'ReferenceType'
4
'?' condition is true
8840
8841 if (Unqualified) {
5
Assuming 'Unqualified' is false
6
Taking false branch
8842 LHS = LHS.getUnqualifiedType();
8843 RHS = RHS.getUnqualifiedType();
8844 }
8845
8846 QualType LHSCan = getCanonicalType(LHS),
8847 RHSCan = getCanonicalType(RHS);
8848
8849 // If two types are identical, they are compatible.
8850 if (LHSCan == RHSCan)
7
Calling 'operator=='
13
Returning from 'operator=='
14
Taking false branch
8851 return LHS;
8852
8853 // If the qualifiers are different, the types aren't compatible... mostly.
8854 Qualifiers LQuals = LHSCan.getLocalQualifiers();
8855 Qualifiers RQuals = RHSCan.getLocalQualifiers();
8856 if (LQuals != RQuals) {
15
Calling 'Qualifiers::operator!='
18
Returning from 'Qualifiers::operator!='
19
Taking false branch
8857 // If any of these qualifiers are different, we have a type
8858 // mismatch.
8859 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
8860 LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
8861 LQuals.getObjCLifetime() != RQuals.getObjCLifetime() ||
8862 LQuals.hasUnaligned() != RQuals.hasUnaligned())
8863 return {};
8864
8865 // Exactly one GC qualifier difference is allowed: __strong is
8866 // okay if the other type has no GC qualifier but is an Objective
8867 // C object pointer (i.e. implicitly strong by default). We fix
8868 // this by pretending that the unqualified type was actually
8869 // qualified __strong.
8870 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
8871 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
8872 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements")(((GC_L != GC_R) && "unequal qualifier sets had only equal elements"
) ? static_cast<void> (0) : __assert_fail ("(GC_L != GC_R) && \"unequal qualifier sets had only equal elements\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8872, __PRETTY_FUNCTION__))
;
8873
8874 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
8875 return {};
8876
8877 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
8878 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
8879 }
8880 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
8881 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
8882 }
8883 return {};
8884 }
8885
8886 // Okay, qualifiers are equal.
8887
8888 Type::TypeClass LHSClass = LHSCan->getTypeClass();
8889 Type::TypeClass RHSClass = RHSCan->getTypeClass();
8890
8891 // We want to consider the two function types to be the same for these
8892 // comparisons, just force one to the other.
8893 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
20
Assuming 'LHSClass' is not equal to FunctionProto
21
Taking false branch
8894 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
22
Assuming 'RHSClass' is not equal to FunctionProto
23
Taking false branch
8895
8896 // Same as above for arrays
8897 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
24
Assuming 'LHSClass' is not equal to VariableArray
25
Assuming 'LHSClass' is not equal to IncompleteArray
26
Taking false branch
8898 LHSClass = Type::ConstantArray;
8899 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
27
Assuming 'RHSClass' is not equal to VariableArray
28
Assuming 'RHSClass' is not equal to IncompleteArray
29
Taking false branch
8900 RHSClass = Type::ConstantArray;
8901
8902 // ObjCInterfaces are just specialized ObjCObjects.
8903 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
30
Assuming 'LHSClass' is not equal to ObjCInterface
31
Taking false branch
8904 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
32
Assuming 'RHSClass' is not equal to ObjCInterface
33
Taking false branch
8905
8906 // Canonicalize ExtVector -> Vector.
8907 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
34
Assuming 'LHSClass' is not equal to ExtVector
35
Taking false branch
8908 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
36
Assuming 'RHSClass' is not equal to ExtVector
37
Taking false branch
8909
8910 // If the canonical type classes don't match.
8911 if (LHSClass != RHSClass) {
38
Assuming 'LHSClass' is equal to 'RHSClass'
39
Taking false branch
8912 // Note that we only have special rules for turning block enum
8913 // returns into block int returns, not vice-versa.
8914 if (const auto *ETy = LHS->getAs<EnumType>()) {
8915 return mergeEnumWithInteger(*this, ETy, RHS, false);
8916 }
8917 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
8918 return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType);
8919 }
8920 // allow block pointer type to match an 'id' type.
8921 if (OfBlockPointer && !BlockReturnType) {
8922 if (LHS->isObjCIdType() && RHS->isBlockPointerType())
8923 return LHS;
8924 if (RHS->isObjCIdType() && LHS->isBlockPointerType())
8925 return RHS;
8926 }
8927
8928 return {};
8929 }
8930
8931 // The canonical type classes match.
8932 switch (LHSClass) {
40
Control jumps to 'case ObjCObjectPointer:' at line 9132
8933#define TYPE(Class, Base)
8934#define ABSTRACT_TYPE(Class, Base)
8935#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
8936#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
8937#define DEPENDENT_TYPE(Class, Base) case Type::Class:
8938#include "clang/AST/TypeNodes.inc"
8939 llvm_unreachable("Non-canonical and dependent types shouldn't get here")::llvm::llvm_unreachable_internal("Non-canonical and dependent types shouldn't get here"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8939)
;
8940
8941 case Type::Auto:
8942 case Type::DeducedTemplateSpecialization:
8943 case Type::LValueReference:
8944 case Type::RValueReference:
8945 case Type::MemberPointer:
8946 llvm_unreachable("C++ should never be in mergeTypes")::llvm::llvm_unreachable_internal("C++ should never be in mergeTypes"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8946)
;
8947
8948 case Type::ObjCInterface:
8949 case Type::IncompleteArray:
8950 case Type::VariableArray:
8951 case Type::FunctionProto:
8952 case Type::ExtVector:
8953 llvm_unreachable("Types are eliminated above")::llvm::llvm_unreachable_internal("Types are eliminated above"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 8953)
;
8954
8955 case Type::Pointer:
8956 {
8957 // Merge two pointer types, while trying to preserve typedef info
8958 QualType LHSPointee = LHS->castAs<PointerType>()->getPointeeType();
8959 QualType RHSPointee = RHS->castAs<PointerType>()->getPointeeType();
8960 if (Unqualified) {
8961 LHSPointee = LHSPointee.getUnqualifiedType();
8962 RHSPointee = RHSPointee.getUnqualifiedType();
8963 }
8964 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
8965 Unqualified);
8966 if (ResultType.isNull())
8967 return {};
8968 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
8969 return LHS;
8970 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
8971 return RHS;
8972 return getPointerType(ResultType);
8973 }
8974 case Type::BlockPointer:
8975 {
8976 // Merge two block pointer types, while trying to preserve typedef info
8977 QualType LHSPointee = LHS->castAs<BlockPointerType>()->getPointeeType();
8978 QualType RHSPointee = RHS->castAs<BlockPointerType>()->getPointeeType();
8979 if (Unqualified) {
8980 LHSPointee = LHSPointee.getUnqualifiedType();
8981 RHSPointee = RHSPointee.getUnqualifiedType();
8982 }
8983 if (getLangOpts().OpenCL) {
8984 Qualifiers LHSPteeQual = LHSPointee.getQualifiers();
8985 Qualifiers RHSPteeQual = RHSPointee.getQualifiers();
8986 // Blocks can't be an expression in a ternary operator (OpenCL v2.0
8987 // 6.12.5) thus the following check is asymmetric.
8988 if (!LHSPteeQual.isAddressSpaceSupersetOf(RHSPteeQual))
8989 return {};
8990 LHSPteeQual.removeAddressSpace();
8991 RHSPteeQual.removeAddressSpace();
8992 LHSPointee =
8993 QualType(LHSPointee.getTypePtr(), LHSPteeQual.getAsOpaqueValue());
8994 RHSPointee =
8995 QualType(RHSPointee.getTypePtr(), RHSPteeQual.getAsOpaqueValue());
8996 }
8997 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
8998 Unqualified);
8999 if (ResultType.isNull())
9000 return {};
9001 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
9002 return LHS;
9003 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
9004 return RHS;
9005 return getBlockPointerType(ResultType);
9006 }
9007 case Type::Atomic:
9008 {
9009 // Merge two pointer types, while trying to preserve typedef info
9010 QualType LHSValue = LHS->castAs<AtomicType>()->getValueType();
9011 QualType RHSValue = RHS->castAs<AtomicType>()->getValueType();
9012 if (Unqualified) {
9013 LHSValue = LHSValue.getUnqualifiedType();
9014 RHSValue = RHSValue.getUnqualifiedType();
9015 }
9016 QualType ResultType = mergeTypes(LHSValue, RHSValue, false,
9017 Unqualified);
9018 if (ResultType.isNull())
9019 return {};
9020 if (getCanonicalType(LHSValue) == getCanonicalType(ResultType))
9021 return LHS;
9022 if (getCanonicalType(RHSValue) == getCanonicalType(ResultType))
9023 return RHS;
9024 return getAtomicType(ResultType);
9025 }
9026 case Type::ConstantArray:
9027 {
9028 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
9029 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
9030 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
9031 return {};
9032
9033 QualType LHSElem = getAsArrayType(LHS)->getElementType();
9034 QualType RHSElem = getAsArrayType(RHS)->getElementType();
9035 if (Unqualified) {
9036 LHSElem = LHSElem.getUnqualifiedType();
9037 RHSElem = RHSElem.getUnqualifiedType();
9038 }
9039
9040 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
9041 if (ResultType.isNull())
9042 return {};
9043
9044 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
9045 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
9046
9047 // If either side is a variable array, and both are complete, check whether
9048 // the current dimension is definite.
9049 if (LVAT || RVAT) {
9050 auto SizeFetch = [this](const VariableArrayType* VAT,
9051 const ConstantArrayType* CAT)
9052 -> std::pair<bool,llvm::APInt> {
9053 if (VAT) {
9054 llvm::APSInt TheInt;
9055 Expr *E = VAT->getSizeExpr();
9056 if (E && E->isIntegerConstantExpr(TheInt, *this))
9057 return std::make_pair(true, TheInt);
9058 else
9059 return std::make_pair(false, TheInt);
9060 } else if (CAT) {
9061 return std::make_pair(true, CAT->getSize());
9062 } else {
9063 return std::make_pair(false, llvm::APInt());
9064 }
9065 };
9066
9067 bool HaveLSize, HaveRSize;
9068 llvm::APInt LSize, RSize;
9069 std::tie(HaveLSize, LSize) = SizeFetch(LVAT, LCAT);
9070 std::tie(HaveRSize, RSize) = SizeFetch(RVAT, RCAT);
9071 if (HaveLSize && HaveRSize && !llvm::APInt::isSameValue(LSize, RSize))
9072 return {}; // Definite, but unequal, array dimension
9073 }
9074
9075 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
9076 return LHS;
9077 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
9078 return RHS;
9079 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
9080 ArrayType::ArraySizeModifier(), 0);
9081 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
9082 ArrayType::ArraySizeModifier(), 0);
9083 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
9084 return LHS;
9085 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
9086 return RHS;
9087 if (LVAT) {
9088 // FIXME: This isn't correct! But tricky to implement because
9089 // the array's size has to be the size of LHS, but the type
9090 // has to be different.
9091 return LHS;
9092 }
9093 if (RVAT) {
9094 // FIXME: This isn't correct! But tricky to implement because
9095 // the array's size has to be the size of RHS, but the type
9096 // has to be different.
9097 return RHS;
9098 }
9099 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
9100 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
9101 return getIncompleteArrayType(ResultType,
9102 ArrayType::ArraySizeModifier(), 0);
9103 }
9104 case Type::FunctionNoProto:
9105 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
9106 case Type::Record:
9107 case Type::Enum:
9108 return {};
9109 case Type::Builtin:
9110 // Only exactly equal builtin types are compatible, which is tested above.
9111 return {};
9112 case Type::Complex:
9113 // Distinct complex types are incompatible.
9114 return {};
9115 case Type::Vector:
9116 // FIXME: The merged type should be an ExtVector!
9117 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
9118 RHSCan->getAs<VectorType>()))
9119 return LHS;
9120 return {};
9121 case Type::ObjCObject: {
9122 // Check if the types are assignment compatible.
9123 // FIXME: This should be type compatibility, e.g. whether
9124 // "LHS x; RHS x;" at global scope is legal.
9125 const auto *LHSIface = LHS->getAs<ObjCObjectType>();
9126 const auto *RHSIface = RHS->getAs<ObjCObjectType>();
9127 if (canAssignObjCInterfaces(LHSIface, RHSIface))
9128 return LHS;
9129
9130 return {};
9131 }
9132 case Type::ObjCObjectPointer:
9133 if (OfBlockPointer) {
41
Assuming 'OfBlockPointer' is true
42
Taking true branch
9134 if (canAssignObjCInterfacesInBlockPointer(
46
Calling 'ASTContext::canAssignObjCInterfacesInBlockPointer'
9135 LHS->getAs<ObjCObjectPointerType>(),
43
Assuming the object is not a 'ObjCObjectPointerType'
9136 RHS->getAs<ObjCObjectPointerType>(),
44
Assuming the object is not a 'ObjCObjectPointerType'
45
Passing null pointer value via 2nd parameter 'RHSOPT'
9137 BlockReturnType))
9138 return LHS;
9139 return {};
9140 }
9141 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
9142 RHS->getAs<ObjCObjectPointerType>()))
9143 return LHS;
9144
9145 return {};
9146 case Type::Pipe:
9147 assert(LHS != RHS &&((LHS != RHS && "Equivalent pipe types should have already been handled!"
) ? static_cast<void> (0) : __assert_fail ("LHS != RHS && \"Equivalent pipe types should have already been handled!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9148, __PRETTY_FUNCTION__))
9148 "Equivalent pipe types should have already been handled!")((LHS != RHS && "Equivalent pipe types should have already been handled!"
) ? static_cast<void> (0) : __assert_fail ("LHS != RHS && \"Equivalent pipe types should have already been handled!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9148, __PRETTY_FUNCTION__))
;
9149 return {};
9150 }
9151
9152 llvm_unreachable("Invalid Type::Class!")::llvm::llvm_unreachable_internal("Invalid Type::Class!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9152)
;
9153}
9154
9155bool ASTContext::mergeExtParameterInfo(
9156 const FunctionProtoType *FirstFnType, const FunctionProtoType *SecondFnType,
9157 bool &CanUseFirst, bool &CanUseSecond,
9158 SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &NewParamInfos) {
9159 assert(NewParamInfos.empty() && "param info list not empty")((NewParamInfos.empty() && "param info list not empty"
) ? static_cast<void> (0) : __assert_fail ("NewParamInfos.empty() && \"param info list not empty\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9159, __PRETTY_FUNCTION__))
;
9160 CanUseFirst = CanUseSecond = true;
9161 bool FirstHasInfo = FirstFnType->hasExtParameterInfos();
9162 bool SecondHasInfo = SecondFnType->hasExtParameterInfos();
9163
9164 // Fast path: if the first type doesn't have ext parameter infos,
9165 // we match if and only if the second type also doesn't have them.
9166 if (!FirstHasInfo && !SecondHasInfo)
9167 return true;
9168
9169 bool NeedParamInfo = false;
9170 size_t E = FirstHasInfo ? FirstFnType->getExtParameterInfos().size()
9171 : SecondFnType->getExtParameterInfos().size();
9172
9173 for (size_t I = 0; I < E; ++I) {
9174 FunctionProtoType::ExtParameterInfo FirstParam, SecondParam;
9175 if (FirstHasInfo)
9176 FirstParam = FirstFnType->getExtParameterInfo(I);
9177 if (SecondHasInfo)
9178 SecondParam = SecondFnType->getExtParameterInfo(I);
9179
9180 // Cannot merge unless everything except the noescape flag matches.
9181 if (FirstParam.withIsNoEscape(false) != SecondParam.withIsNoEscape(false))
9182 return false;
9183
9184 bool FirstNoEscape = FirstParam.isNoEscape();
9185 bool SecondNoEscape = SecondParam.isNoEscape();
9186 bool IsNoEscape = FirstNoEscape && SecondNoEscape;
9187 NewParamInfos.push_back(FirstParam.withIsNoEscape(IsNoEscape));
9188 if (NewParamInfos.back().getOpaqueValue())
9189 NeedParamInfo = true;
9190 if (FirstNoEscape != IsNoEscape)
9191 CanUseFirst = false;
9192 if (SecondNoEscape != IsNoEscape)
9193 CanUseSecond = false;
9194 }
9195
9196 if (!NeedParamInfo)
9197 NewParamInfos.clear();
9198
9199 return true;
9200}
9201
9202void ASTContext::ResetObjCLayout(const ObjCContainerDecl *CD) {
9203 ObjCLayouts[CD] = nullptr;
9204}
9205
9206/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
9207/// 'RHS' attributes and returns the merged version; including for function
9208/// return types.
9209QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
9210 QualType LHSCan = getCanonicalType(LHS),
9211 RHSCan = getCanonicalType(RHS);
9212 // If two types are identical, they are compatible.
9213 if (LHSCan == RHSCan)
9214 return LHS;
9215 if (RHSCan->isFunctionType()) {
9216 if (!LHSCan->isFunctionType())
9217 return {};
9218 QualType OldReturnType =
9219 cast<FunctionType>(RHSCan.getTypePtr())->getReturnType();
9220 QualType NewReturnType =
9221 cast<FunctionType>(LHSCan.getTypePtr())->getReturnType();
9222 QualType ResReturnType =
9223 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
9224 if (ResReturnType.isNull())
9225 return {};
9226 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
9227 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
9228 // In either case, use OldReturnType to build the new function type.
9229 const auto *F = LHS->getAs<FunctionType>();
9230 if (const auto *FPT = cast<FunctionProtoType>(F)) {
9231 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
9232 EPI.ExtInfo = getFunctionExtInfo(LHS);
9233 QualType ResultType =
9234 getFunctionType(OldReturnType, FPT->getParamTypes(), EPI);
9235 return ResultType;
9236 }
9237 }
9238 return {};
9239 }
9240
9241 // If the qualifiers are different, the types can still be merged.
9242 Qualifiers LQuals = LHSCan.getLocalQualifiers();
9243 Qualifiers RQuals = RHSCan.getLocalQualifiers();
9244 if (LQuals != RQuals) {
9245 // If any of these qualifiers are different, we have a type mismatch.
9246 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
9247 LQuals.getAddressSpace() != RQuals.getAddressSpace())
9248 return {};
9249
9250 // Exactly one GC qualifier difference is allowed: __strong is
9251 // okay if the other type has no GC qualifier but is an Objective
9252 // C object pointer (i.e. implicitly strong by default). We fix
9253 // this by pretending that the unqualified type was actually
9254 // qualified __strong.
9255 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
9256 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
9257 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements")(((GC_L != GC_R) && "unequal qualifier sets had only equal elements"
) ? static_cast<void> (0) : __assert_fail ("(GC_L != GC_R) && \"unequal qualifier sets had only equal elements\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9257, __PRETTY_FUNCTION__))
;
9258
9259 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
9260 return {};
9261
9262 if (GC_L == Qualifiers::Strong)
9263 return LHS;
9264 if (GC_R == Qualifiers::Strong)
9265 return RHS;
9266 return {};
9267 }
9268
9269 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
9270 QualType LHSBaseQT = LHS->castAs<ObjCObjectPointerType>()->getPointeeType();
9271 QualType RHSBaseQT = RHS->castAs<ObjCObjectPointerType>()->getPointeeType();
9272 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
9273 if (ResQT == LHSBaseQT)
9274 return LHS;
9275 if (ResQT == RHSBaseQT)
9276 return RHS;
9277 }
9278 return {};
9279}
9280
9281//===----------------------------------------------------------------------===//
9282// Integer Predicates
9283//===----------------------------------------------------------------------===//
9284
9285unsigned ASTContext::getIntWidth(QualType T) const {
9286 if (const auto *ET = T->getAs<EnumType>())
9287 T = ET->getDecl()->getIntegerType();
9288 if (T->isBooleanType())
9289 return 1;
9290 // For builtin types, just use the standard type sizing method
9291 return (unsigned)getTypeSize(T);
9292}
9293
9294QualType ASTContext::getCorrespondingUnsignedType(QualType T) const {
9295 assert((T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) &&(((T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType
()) && "Unexpected type") ? static_cast<void> (
0) : __assert_fail ("(T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) && \"Unexpected type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9296, __PRETTY_FUNCTION__))
9296 "Unexpected type")(((T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType
()) && "Unexpected type") ? static_cast<void> (
0) : __assert_fail ("(T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) && \"Unexpected type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9296, __PRETTY_FUNCTION__))
;
9297
9298 // Turn <4 x signed int> -> <4 x unsigned int>
9299 if (const auto *VTy = T->getAs<VectorType>())
9300 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
9301 VTy->getNumElements(), VTy->getVectorKind());
9302
9303 // For enums, we return the unsigned version of the base type.
9304 if (const auto *ETy = T->getAs<EnumType>())
9305 T = ETy->getDecl()->getIntegerType();
9306
9307 const auto *BTy = T->getAs<BuiltinType>();
9308 assert(BTy && "Unexpected signed integer or fixed point type")((BTy && "Unexpected signed integer or fixed point type"
) ? static_cast<void> (0) : __assert_fail ("BTy && \"Unexpected signed integer or fixed point type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9308, __PRETTY_FUNCTION__))
;
9309 switch (BTy->getKind()) {
9310 case BuiltinType::Char_S:
9311 case BuiltinType::SChar:
9312 return UnsignedCharTy;
9313 case BuiltinType::Short:
9314 return UnsignedShortTy;
9315 case BuiltinType::Int:
9316 return UnsignedIntTy;
9317 case BuiltinType::Long:
9318 return UnsignedLongTy;
9319 case BuiltinType::LongLong:
9320 return UnsignedLongLongTy;
9321 case BuiltinType::Int128:
9322 return UnsignedInt128Ty;
9323
9324 case BuiltinType::ShortAccum:
9325 return UnsignedShortAccumTy;
9326 case BuiltinType::Accum:
9327 return UnsignedAccumTy;
9328 case BuiltinType::LongAccum:
9329 return UnsignedLongAccumTy;
9330 case BuiltinType::SatShortAccum:
9331 return SatUnsignedShortAccumTy;
9332 case BuiltinType::SatAccum:
9333 return SatUnsignedAccumTy;
9334 case BuiltinType::SatLongAccum:
9335 return SatUnsignedLongAccumTy;
9336 case BuiltinType::ShortFract:
9337 return UnsignedShortFractTy;
9338 case BuiltinType::Fract:
9339 return UnsignedFractTy;
9340 case BuiltinType::LongFract:
9341 return UnsignedLongFractTy;
9342 case BuiltinType::SatShortFract:
9343 return SatUnsignedShortFractTy;
9344 case BuiltinType::SatFract:
9345 return SatUnsignedFractTy;
9346 case BuiltinType::SatLongFract:
9347 return SatUnsignedLongFractTy;
9348 default:
9349 llvm_unreachable("Unexpected signed integer or fixed point type")::llvm::llvm_unreachable_internal("Unexpected signed integer or fixed point type"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9349)
;
9350 }
9351}
9352
9353ASTMutationListener::~ASTMutationListener() = default;
9354
9355void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD,
9356 QualType ReturnType) {}
9357
9358//===----------------------------------------------------------------------===//
9359// Builtin Type Computation
9360//===----------------------------------------------------------------------===//
9361
9362/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
9363/// pointer over the consumed characters. This returns the resultant type. If
9364/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
9365/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
9366/// a vector of "i*".
9367///
9368/// RequiresICE is filled in on return to indicate whether the value is required
9369/// to be an Integer Constant Expression.
9370static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
9371 ASTContext::GetBuiltinTypeError &Error,
9372 bool &RequiresICE,
9373 bool AllowTypeModifiers) {
9374 // Modifiers.
9375 int HowLong = 0;
9376 bool Signed = false, Unsigned = false;
9377 RequiresICE = false;
9378
9379 // Read the prefixed modifiers first.
9380 bool Done = false;
9381 #ifndef NDEBUG
9382 bool IsSpecial = false;
9383 #endif
9384 while (!Done) {
9385 switch (*Str++) {
9386 default: Done = true; --Str; break;
9387 case 'I':
9388 RequiresICE = true;
9389 break;
9390 case 'S':
9391 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!")((!Unsigned && "Can't use both 'S' and 'U' modifiers!"
) ? static_cast<void> (0) : __assert_fail ("!Unsigned && \"Can't use both 'S' and 'U' modifiers!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9391, __PRETTY_FUNCTION__))
;
9392 assert(!Signed && "Can't use 'S' modifier multiple times!")((!Signed && "Can't use 'S' modifier multiple times!"
) ? static_cast<void> (0) : __assert_fail ("!Signed && \"Can't use 'S' modifier multiple times!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9392, __PRETTY_FUNCTION__))
;
9393 Signed = true;
9394 break;
9395 case 'U':
9396 assert(!Signed && "Can't use both 'S' and 'U' modifiers!")((!Signed && "Can't use both 'S' and 'U' modifiers!")
? static_cast<void> (0) : __assert_fail ("!Signed && \"Can't use both 'S' and 'U' modifiers!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9396, __PRETTY_FUNCTION__))
;
9397 assert(!Unsigned && "Can't use 'U' modifier multiple times!")((!Unsigned && "Can't use 'U' modifier multiple times!"
) ? static_cast<void> (0) : __assert_fail ("!Unsigned && \"Can't use 'U' modifier multiple times!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9397, __PRETTY_FUNCTION__))
;
9398 Unsigned = true;
9399 break;
9400 case 'L':
9401 assert(!IsSpecial && "Can't use 'L' with 'W', 'N', 'Z' or 'O' modifiers")((!IsSpecial && "Can't use 'L' with 'W', 'N', 'Z' or 'O' modifiers"
) ? static_cast<void> (0) : __assert_fail ("!IsSpecial && \"Can't use 'L' with 'W', 'N', 'Z' or 'O' modifiers\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9401, __PRETTY_FUNCTION__))
;
9402 assert(HowLong <= 2 && "Can't have LLLL modifier")((HowLong <= 2 && "Can't have LLLL modifier") ? static_cast
<void> (0) : __assert_fail ("HowLong <= 2 && \"Can't have LLLL modifier\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9402, __PRETTY_FUNCTION__))
;
9403 ++HowLong;
9404 break;
9405 case 'N':
9406 // 'N' behaves like 'L' for all non LP64 targets and 'int' otherwise.
9407 assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!")((!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"
) ? static_cast<void> (0) : __assert_fail ("!IsSpecial && \"Can't use two 'N', 'W', 'Z' or 'O' modifiers!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9407, __PRETTY_FUNCTION__))
;
9408 assert(HowLong == 0 && "Can't use both 'L' and 'N' modifiers!")((HowLong == 0 && "Can't use both 'L' and 'N' modifiers!"
) ? static_cast<void> (0) : __assert_fail ("HowLong == 0 && \"Can't use both 'L' and 'N' modifiers!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9408, __PRETTY_FUNCTION__))
;
9409 #ifndef NDEBUG
9410 IsSpecial = true;
9411 #endif
9412 if (Context.getTargetInfo().getLongWidth() == 32)
9413 ++HowLong;
9414 break;
9415 case 'W':
9416 // This modifier represents int64 type.
9417 assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!")((!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"
) ? static_cast<void> (0) : __assert_fail ("!IsSpecial && \"Can't use two 'N', 'W', 'Z' or 'O' modifiers!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9417, __PRETTY_FUNCTION__))
;
9418 assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!")((HowLong == 0 && "Can't use both 'L' and 'W' modifiers!"
) ? static_cast<void> (0) : __assert_fail ("HowLong == 0 && \"Can't use both 'L' and 'W' modifiers!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9418, __PRETTY_FUNCTION__))
;
9419 #ifndef NDEBUG
9420 IsSpecial = true;
9421 #endif
9422 switch (Context.getTargetInfo().getInt64Type()) {
9423 default:
9424 llvm_unreachable("Unexpected integer type")::llvm::llvm_unreachable_internal("Unexpected integer type", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9424)
;
9425 case TargetInfo::SignedLong:
9426 HowLong = 1;
9427 break;
9428 case TargetInfo::SignedLongLong:
9429 HowLong = 2;
9430 break;
9431 }
9432 break;
9433 case 'Z':
9434 // This modifier represents int32 type.
9435 assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!")((!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"
) ? static_cast<void> (0) : __assert_fail ("!IsSpecial && \"Can't use two 'N', 'W', 'Z' or 'O' modifiers!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9435, __PRETTY_FUNCTION__))
;
9436 assert(HowLong == 0 && "Can't use both 'L' and 'Z' modifiers!")((HowLong == 0 && "Can't use both 'L' and 'Z' modifiers!"
) ? static_cast<void> (0) : __assert_fail ("HowLong == 0 && \"Can't use both 'L' and 'Z' modifiers!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9436, __PRETTY_FUNCTION__))
;
9437 #ifndef NDEBUG
9438 IsSpecial = true;
9439 #endif
9440 switch (Context.getTargetInfo().getIntTypeByWidth(32, true)) {
9441 default:
9442 llvm_unreachable("Unexpected integer type")::llvm::llvm_unreachable_internal("Unexpected integer type", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9442)
;
9443 case TargetInfo::SignedInt:
9444 HowLong = 0;
9445 break;
9446 case TargetInfo::SignedLong:
9447 HowLong = 1;
9448 break;
9449 case TargetInfo::SignedLongLong:
9450 HowLong = 2;
9451 break;
9452 }
9453 break;
9454 case 'O':
9455 assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!")((!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"
) ? static_cast<void> (0) : __assert_fail ("!IsSpecial && \"Can't use two 'N', 'W', 'Z' or 'O' modifiers!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9455, __PRETTY_FUNCTION__))
;
9456 assert(HowLong == 0 && "Can't use both 'L' and 'O' modifiers!")((HowLong == 0 && "Can't use both 'L' and 'O' modifiers!"
) ? static_cast<void> (0) : __assert_fail ("HowLong == 0 && \"Can't use both 'L' and 'O' modifiers!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9456, __PRETTY_FUNCTION__))
;
9457 #ifndef NDEBUG
9458 IsSpecial = true;
9459 #endif
9460 if (Context.getLangOpts().OpenCL)
9461 HowLong = 1;
9462 else
9463 HowLong = 2;
9464 break;
9465 }
9466 }
9467
9468 QualType Type;
9469
9470 // Read the base type.
9471 switch (*Str++) {
9472 default: llvm_unreachable("Unknown builtin type letter!")::llvm::llvm_unreachable_internal("Unknown builtin type letter!"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9472)
;
9473 case 'v':
9474 assert(HowLong == 0 && !Signed && !Unsigned &&((HowLong == 0 && !Signed && !Unsigned &&
"Bad modifiers used with 'v'!") ? static_cast<void> (0
) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'v'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9475, __PRETTY_FUNCTION__))
9475 "Bad modifiers used with 'v'!")((HowLong == 0 && !Signed && !Unsigned &&
"Bad modifiers used with 'v'!") ? static_cast<void> (0
) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'v'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9475, __PRETTY_FUNCTION__))
;
9476 Type = Context.VoidTy;
9477 break;
9478 case 'h':
9479 assert(HowLong == 0 && !Signed && !Unsigned &&((HowLong == 0 && !Signed && !Unsigned &&
"Bad modifiers used with 'h'!") ? static_cast<void> (0
) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'h'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9480, __PRETTY_FUNCTION__))
9480 "Bad modifiers used with 'h'!")((HowLong == 0 && !Signed && !Unsigned &&
"Bad modifiers used with 'h'!") ? static_cast<void> (0
) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'h'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9480, __PRETTY_FUNCTION__))
;
9481 Type = Context.HalfTy;
9482 break;
9483 case 'f':
9484 assert(HowLong == 0 && !Signed && !Unsigned &&((HowLong == 0 && !Signed && !Unsigned &&
"Bad modifiers used with 'f'!") ? static_cast<void> (0
) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'f'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9485, __PRETTY_FUNCTION__))
9485 "Bad modifiers used with 'f'!")((HowLong == 0 && !Signed && !Unsigned &&
"Bad modifiers used with 'f'!") ? static_cast<void> (0
) : __assert_fail ("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers used with 'f'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9485, __PRETTY_FUNCTION__))
;
9486 Type = Context.FloatTy;
9487 break;
9488 case 'd':
9489 assert(HowLong < 3 && !Signed && !Unsigned &&((HowLong < 3 && !Signed && !Unsigned &&
"Bad modifiers used with 'd'!") ? static_cast<void> (0
) : __assert_fail ("HowLong < 3 && !Signed && !Unsigned && \"Bad modifiers used with 'd'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9490, __PRETTY_FUNCTION__))
9490 "Bad modifiers used with 'd'!")((HowLong < 3 && !Signed && !Unsigned &&
"Bad modifiers used with 'd'!") ? static_cast<void> (0
) : __assert_fail ("HowLong < 3 && !Signed && !Unsigned && \"Bad modifiers used with 'd'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9490, __PRETTY_FUNCTION__))
;
9491 if (HowLong == 1)
9492 Type = Context.LongDoubleTy;
9493 else if (HowLong == 2)
9494 Type = Context.Float128Ty;
9495 else
9496 Type = Context.DoubleTy;
9497 break;
9498 case 's':
9499 assert(HowLong == 0 && "Bad modifiers used with 's'!")((HowLong == 0 && "Bad modifiers used with 's'!") ? static_cast
<void> (0) : __assert_fail ("HowLong == 0 && \"Bad modifiers used with 's'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9499, __PRETTY_FUNCTION__))
;
9500 if (Unsigned)
9501 Type = Context.UnsignedShortTy;
9502 else
9503 Type = Context.ShortTy;
9504 break;
9505 case 'i':
9506 if (HowLong == 3)
9507 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
9508 else if (HowLong == 2)
9509 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
9510 else if (HowLong == 1)
9511 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
9512 else
9513 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
9514 break;
9515 case 'c':
9516 assert(HowLong == 0 && "Bad modifiers used with 'c'!")((HowLong == 0 && "Bad modifiers used with 'c'!") ? static_cast
<void> (0) : __assert_fail ("HowLong == 0 && \"Bad modifiers used with 'c'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9516, __PRETTY_FUNCTION__))
;
9517 if (Signed)
9518 Type = Context.SignedCharTy;
9519 else if (Unsigned)
9520 Type = Context.UnsignedCharTy;
9521 else
9522 Type = Context.CharTy;
9523 break;
9524 case 'b': // boolean
9525 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!")((HowLong == 0 && !Signed && !Unsigned &&
"Bad modifiers for 'b'!") ? static_cast<void> (0) : __assert_fail
("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers for 'b'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9525, __PRETTY_FUNCTION__))
;
9526 Type = Context.BoolTy;
9527 break;
9528 case 'z': // size_t.
9529 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!")((HowLong == 0 && !Signed && !Unsigned &&
"Bad modifiers for 'z'!") ? static_cast<void> (0) : __assert_fail
("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers for 'z'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9529, __PRETTY_FUNCTION__))
;
9530 Type = Context.getSizeType();
9531 break;
9532 case 'w': // wchar_t.
9533 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'w'!")((HowLong == 0 && !Signed && !Unsigned &&
"Bad modifiers for 'w'!") ? static_cast<void> (0) : __assert_fail
("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers for 'w'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9533, __PRETTY_FUNCTION__))
;
9534 Type = Context.getWideCharType();
9535 break;
9536 case 'F':
9537 Type = Context.getCFConstantStringType();
9538 break;
9539 case 'G':
9540 Type = Context.getObjCIdType();
9541 break;
9542 case 'H':
9543 Type = Context.getObjCSelType();
9544 break;
9545 case 'M':
9546 Type = Context.getObjCSuperType();
9547 break;
9548 case 'a':
9549 Type = Context.getBuiltinVaListType();
9550 assert(!Type.isNull() && "builtin va list type not initialized!")((!Type.isNull() && "builtin va list type not initialized!"
) ? static_cast<void> (0) : __assert_fail ("!Type.isNull() && \"builtin va list type not initialized!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9550, __PRETTY_FUNCTION__))
;
9551 break;
9552 case 'A':
9553 // This is a "reference" to a va_list; however, what exactly
9554 // this means depends on how va_list is defined. There are two
9555 // different kinds of va_list: ones passed by value, and ones
9556 // passed by reference. An example of a by-value va_list is
9557 // x86, where va_list is a char*. An example of by-ref va_list
9558 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
9559 // we want this argument to be a char*&; for x86-64, we want
9560 // it to be a __va_list_tag*.
9561 Type = Context.getBuiltinVaListType();
9562 assert(!Type.isNull() && "builtin va list type not initialized!")((!Type.isNull() && "builtin va list type not initialized!"
) ? static_cast<void> (0) : __assert_fail ("!Type.isNull() && \"builtin va list type not initialized!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9562, __PRETTY_FUNCTION__))
;
9563 if (Type->isArrayType())
9564 Type = Context.getArrayDecayedType(Type);
9565 else
9566 Type = Context.getLValueReferenceType(Type);
9567 break;
9568 case 'V': {
9569 char *End;
9570 unsigned NumElements = strtoul(Str, &End, 10);
9571 assert(End != Str && "Missing vector size")((End != Str && "Missing vector size") ? static_cast<
void> (0) : __assert_fail ("End != Str && \"Missing vector size\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9571, __PRETTY_FUNCTION__))
;
9572 Str = End;
9573
9574 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
9575 RequiresICE, false);
9576 assert(!RequiresICE && "Can't require vector ICE")((!RequiresICE && "Can't require vector ICE") ? static_cast
<void> (0) : __assert_fail ("!RequiresICE && \"Can't require vector ICE\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9576, __PRETTY_FUNCTION__))
;
9577
9578 // TODO: No way to make AltiVec vectors in builtins yet.
9579 Type = Context.getVectorType(ElementType, NumElements,
9580 VectorType::GenericVector);
9581 break;
9582 }
9583 case 'E': {
9584 char *End;
9585
9586 unsigned NumElements = strtoul(Str, &End, 10);
9587 assert(End != Str && "Missing vector size")((End != Str && "Missing vector size") ? static_cast<
void> (0) : __assert_fail ("End != Str && \"Missing vector size\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9587, __PRETTY_FUNCTION__))
;
9588
9589 Str = End;
9590
9591 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
9592 false);
9593 Type = Context.getExtVectorType(ElementType, NumElements);
9594 break;
9595 }
9596 case 'X': {
9597 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
9598 false);
9599 assert(!RequiresICE && "Can't require complex ICE")((!RequiresICE && "Can't require complex ICE") ? static_cast
<void> (0) : __assert_fail ("!RequiresICE && \"Can't require complex ICE\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9599, __PRETTY_FUNCTION__))
;
9600 Type = Context.getComplexType(ElementType);
9601 break;
9602 }
9603 case 'Y':
9604 Type = Context.getPointerDiffType();
9605 break;
9606 case 'P':
9607 Type = Context.getFILEType();
9608 if (Type.isNull()) {
9609 Error = ASTContext::GE_Missing_stdio;
9610 return {};
9611 }
9612 break;
9613 case 'J':
9614 if (Signed)
9615 Type = Context.getsigjmp_bufType();
9616 else
9617 Type = Context.getjmp_bufType();
9618
9619 if (Type.isNull()) {
9620 Error = ASTContext::GE_Missing_setjmp;
9621 return {};
9622 }
9623 break;
9624 case 'K':
9625 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!")((HowLong == 0 && !Signed && !Unsigned &&
"Bad modifiers for 'K'!") ? static_cast<void> (0) : __assert_fail
("HowLong == 0 && !Signed && !Unsigned && \"Bad modifiers for 'K'!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9625, __PRETTY_FUNCTION__))
;
9626 Type = Context.getucontext_tType();
9627
9628 if (Type.isNull()) {
9629 Error = ASTContext::GE_Missing_ucontext;
9630 return {};
9631 }
9632 break;
9633 case 'p':
9634 Type = Context.getProcessIDType();
9635 break;
9636 }
9637
9638 // If there are modifiers and if we're allowed to parse them, go for it.
9639 Done = !AllowTypeModifiers;
9640 while (!Done) {
9641 switch (char c = *Str++) {
9642 default: Done = true; --Str; break;
9643 case '*':
9644 case '&': {
9645 // Both pointers and references can have their pointee types
9646 // qualified with an address space.
9647 char *End;
9648 unsigned AddrSpace = strtoul(Str, &End, 10);
9649 if (End != Str) {
9650 // Note AddrSpace == 0 is not the same as an unspecified address space.
9651 Type = Context.getAddrSpaceQualType(
9652 Type,
9653 Context.getLangASForBuiltinAddressSpace(AddrSpace));
9654 Str = End;
9655 }
9656 if (c == '*')
9657 Type = Context.getPointerType(Type);
9658 else
9659 Type = Context.getLValueReferenceType(Type);
9660 break;
9661 }
9662 // FIXME: There's no way to have a built-in with an rvalue ref arg.
9663 case 'C':
9664 Type = Type.withConst();
9665 break;
9666 case 'D':
9667 Type = Context.getVolatileType(Type);
9668 break;
9669 case 'R':
9670 Type = Type.withRestrict();
9671 break;
9672 }
9673 }
9674
9675 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&(((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
"Integer constant 'I' type must be an integer") ? static_cast
<void> (0) : __assert_fail ("(!RequiresICE || Type->isIntegralOrEnumerationType()) && \"Integer constant 'I' type must be an integer\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9676, __PRETTY_FUNCTION__))
9676 "Integer constant 'I' type must be an integer")(((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
"Integer constant 'I' type must be an integer") ? static_cast
<void> (0) : __assert_fail ("(!RequiresICE || Type->isIntegralOrEnumerationType()) && \"Integer constant 'I' type must be an integer\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9676, __PRETTY_FUNCTION__))
;
9677
9678 return Type;
9679}
9680
9681/// GetBuiltinType - Return the type for the specified builtin.
9682QualType ASTContext::GetBuiltinType(unsigned Id,
9683 GetBuiltinTypeError &Error,
9684 unsigned *IntegerConstantArgs) const {
9685 const char *TypeStr = BuiltinInfo.getTypeString(Id);
9686 if (TypeStr[0] == '\0') {
9687 Error = GE_Missing_type;
9688 return {};
9689 }
9690
9691 SmallVector<QualType, 8> ArgTypes;
9692
9693 bool RequiresICE = false;
9694 Error = GE_None;
9695 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
9696 RequiresICE, true);
9697 if (Error != GE_None)
9698 return {};
9699
9700 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE")((!RequiresICE && "Result of intrinsic cannot be required to be an ICE"
) ? static_cast<void> (0) : __assert_fail ("!RequiresICE && \"Result of intrinsic cannot be required to be an ICE\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9700, __PRETTY_FUNCTION__))
;
9701
9702 while (TypeStr[0] && TypeStr[0] != '.') {
9703 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
9704 if (Error != GE_None)
9705 return {};
9706
9707 // If this argument is required to be an IntegerConstantExpression and the
9708 // caller cares, fill in the bitmask we return.
9709 if (RequiresICE && IntegerConstantArgs)
9710 *IntegerConstantArgs |= 1 << ArgTypes.size();
9711
9712 // Do array -> pointer decay. The builtin should use the decayed type.
9713 if (Ty->isArrayType())
9714 Ty = getArrayDecayedType(Ty);
9715
9716 ArgTypes.push_back(Ty);
9717 }
9718
9719 if (Id == Builtin::BI__GetExceptionInfo)
9720 return {};
9721
9722 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&(((TypeStr[0] != '.' || TypeStr[1] == 0) && "'.' should only occur at end of builtin type list!"
) ? static_cast<void> (0) : __assert_fail ("(TypeStr[0] != '.' || TypeStr[1] == 0) && \"'.' should only occur at end of builtin type list!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9723, __PRETTY_FUNCTION__))
9723 "'.' should only occur at end of builtin type list!")(((TypeStr[0] != '.' || TypeStr[1] == 0) && "'.' should only occur at end of builtin type list!"
) ? static_cast<void> (0) : __assert_fail ("(TypeStr[0] != '.' || TypeStr[1] == 0) && \"'.' should only occur at end of builtin type list!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9723, __PRETTY_FUNCTION__))
;
9724
9725 bool Variadic = (TypeStr[0] == '.');
9726
9727 FunctionType::ExtInfo EI(getDefaultCallingConvention(
9728 Variadic, /*IsCXXMethod=*/false, /*IsBuiltin=*/true));
9729 if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
9730
9731
9732 // We really shouldn't be making a no-proto type here.
9733 if (ArgTypes.empty() && Variadic && !getLangOpts().CPlusPlus)
9734 return getFunctionNoProtoType(ResType, EI);
9735
9736 FunctionProtoType::ExtProtoInfo EPI;
9737 EPI.ExtInfo = EI;
9738 EPI.Variadic = Variadic;
9739 if (getLangOpts().CPlusPlus && BuiltinInfo.isNoThrow(Id))
9740 EPI.ExceptionSpec.Type =
9741 getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone;
9742
9743 return getFunctionType(ResType, ArgTypes, EPI);
9744}
9745
9746static GVALinkage basicGVALinkageForFunction(const ASTContext &Context,
9747 const FunctionDecl *FD) {
9748 if (!FD->isExternallyVisible())
9749 return GVA_Internal;
9750
9751 // Non-user-provided functions get emitted as weak definitions with every
9752 // use, no matter whether they've been explicitly instantiated etc.
9753 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
9754 if (!MD->isUserProvided())
9755 return GVA_DiscardableODR;
9756
9757 GVALinkage External;
9758 switch (FD->getTemplateSpecializationKind()) {
9759 case TSK_Undeclared:
9760 case TSK_ExplicitSpecialization:
9761 External = GVA_StrongExternal;
9762 break;
9763
9764 case TSK_ExplicitInstantiationDefinition:
9765 return GVA_StrongODR;
9766
9767 // C++11 [temp.explicit]p10:
9768 // [ Note: The intent is that an inline function that is the subject of
9769 // an explicit instantiation declaration will still be implicitly
9770 // instantiated when used so that the body can be considered for
9771 // inlining, but that no out-of-line copy of the inline function would be
9772 // generated in the translation unit. -- end note ]
9773 case TSK_ExplicitInstantiationDeclaration:
9774 return GVA_AvailableExternally;
9775
9776 case TSK_ImplicitInstantiation:
9777 External = GVA_DiscardableODR;
9778 break;
9779 }
9780
9781 if (!FD->isInlined())
9782 return External;
9783
9784 if ((!Context.getLangOpts().CPlusPlus &&
9785 !Context.getTargetInfo().getCXXABI().isMicrosoft() &&
9786 !FD->hasAttr<DLLExportAttr>()) ||
9787 FD->hasAttr<GNUInlineAttr>()) {
9788 // FIXME: This doesn't match gcc's behavior for dllexport inline functions.
9789
9790 // GNU or C99 inline semantics. Determine whether this symbol should be
9791 // externally visible.
9792 if (FD->isInlineDefinitionExternallyVisible())
9793 return External;
9794
9795 // C99 inline semantics, where the symbol is not externally visible.
9796 return GVA_AvailableExternally;
9797 }
9798
9799 // Functions specified with extern and inline in -fms-compatibility mode
9800 // forcibly get emitted. While the body of the function cannot be later
9801 // replaced, the function definition cannot be discarded.
9802 if (FD->isMSExternInline())
9803 return GVA_StrongODR;
9804
9805 return GVA_DiscardableODR;
9806}
9807
9808static GVALinkage adjustGVALinkageForAttributes(const ASTContext &Context,
9809 const Decl *D, GVALinkage L) {
9810 // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx
9811 // dllexport/dllimport on inline functions.
9812 if (D->hasAttr<DLLImportAttr>()) {
9813 if (L == GVA_DiscardableODR || L == GVA_StrongODR)
9814 return GVA_AvailableExternally;
9815 } else if (D->hasAttr<DLLExportAttr>()) {
9816 if (L == GVA_DiscardableODR)
9817 return GVA_StrongODR;
9818 } else if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice &&
9819 D->hasAttr<CUDAGlobalAttr>()) {
9820 // Device-side functions with __global__ attribute must always be
9821 // visible externally so they can be launched from host.
9822 if (L == GVA_DiscardableODR || L == GVA_Internal)
9823 return GVA_StrongODR;
9824 }
9825 return L;
9826}
9827
9828/// Adjust the GVALinkage for a declaration based on what an external AST source
9829/// knows about whether there can be other definitions of this declaration.
9830static GVALinkage
9831adjustGVALinkageForExternalDefinitionKind(const ASTContext &Ctx, const Decl *D,
9832 GVALinkage L) {
9833 ExternalASTSource *Source = Ctx.getExternalSource();
9834 if (!Source)
9835 return L;
9836
9837 switch (Source->hasExternalDefinitions(D)) {
9838 case ExternalASTSource::EK_Never:
9839 // Other translation units rely on us to provide the definition.
9840 if (L == GVA_DiscardableODR)
9841 return GVA_StrongODR;
9842 break;
9843
9844 case ExternalASTSource::EK_Always:
9845 return GVA_AvailableExternally;
9846
9847 case ExternalASTSource::EK_ReplyHazy:
9848 break;
9849 }
9850 return L;
9851}
9852
9853GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const {
9854 return adjustGVALinkageForExternalDefinitionKind(*this, FD,
9855 adjustGVALinkageForAttributes(*this, FD,
9856 basicGVALinkageForFunction(*this, FD)));
9857}
9858
9859static GVALinkage basicGVALinkageForVariable(const ASTContext &Context,
9860 const VarDecl *VD) {
9861 if (!VD->isExternallyVisible())
9862 return GVA_Internal;
9863
9864 if (VD->isStaticLocal()) {
9865 const DeclContext *LexicalContext = VD->getParentFunctionOrMethod();
9866 while (LexicalContext && !isa<FunctionDecl>(LexicalContext))
9867 LexicalContext = LexicalContext->getLexicalParent();
9868
9869 // ObjC Blocks can create local variables that don't have a FunctionDecl
9870 // LexicalContext.
9871 if (!LexicalContext)
9872 return GVA_DiscardableODR;
9873
9874 // Otherwise, let the static local variable inherit its linkage from the
9875 // nearest enclosing function.
9876 auto StaticLocalLinkage =
9877 Context.GetGVALinkageForFunction(cast<FunctionDecl>(LexicalContext));
9878
9879 // Itanium ABI 5.2.2: "Each COMDAT group [for a static local variable] must
9880 // be emitted in any object with references to the symbol for the object it
9881 // contains, whether inline or out-of-line."
9882 // Similar behavior is observed with MSVC. An alternative ABI could use
9883 // StrongODR/AvailableExternally to match the function, but none are
9884 // known/supported currently.
9885 if (StaticLocalLinkage == GVA_StrongODR ||
9886 StaticLocalLinkage == GVA_AvailableExternally)
9887 return GVA_DiscardableODR;
9888 return StaticLocalLinkage;
9889 }
9890
9891 // MSVC treats in-class initialized static data members as definitions.
9892 // By giving them non-strong linkage, out-of-line definitions won't
9893 // cause link errors.
9894 if (Context.isMSStaticDataMemberInlineDefinition(VD))
9895 return GVA_DiscardableODR;
9896
9897 // Most non-template variables have strong linkage; inline variables are
9898 // linkonce_odr or (occasionally, for compatibility) weak_odr.
9899 GVALinkage StrongLinkage;
9900 switch (Context.getInlineVariableDefinitionKind(VD)) {
9901 case ASTContext::InlineVariableDefinitionKind::None:
9902 StrongLinkage = GVA_StrongExternal;
9903 break;
9904 case ASTContext::InlineVariableDefinitionKind::Weak:
9905 case ASTContext::InlineVariableDefinitionKind::WeakUnknown:
9906 StrongLinkage = GVA_DiscardableODR;
9907 break;
9908 case ASTContext::InlineVariableDefinitionKind::Strong:
9909 StrongLinkage = GVA_StrongODR;
9910 break;
9911 }
9912
9913 switch (VD->getTemplateSpecializationKind()) {
9914 case TSK_Undeclared:
9915 return StrongLinkage;
9916
9917 case TSK_ExplicitSpecialization:
9918 return Context.getTargetInfo().getCXXABI().isMicrosoft() &&
9919 VD->isStaticDataMember()
9920 ? GVA_StrongODR
9921 : StrongLinkage;
9922
9923 case TSK_ExplicitInstantiationDefinition:
9924 return GVA_StrongODR;
9925
9926 case TSK_ExplicitInstantiationDeclaration:
9927 return GVA_AvailableExternally;
9928
9929 case TSK_ImplicitInstantiation:
9930 return GVA_DiscardableODR;
9931 }
9932
9933 llvm_unreachable("Invalid Linkage!")::llvm::llvm_unreachable_internal("Invalid Linkage!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9933)
;
9934}
9935
9936GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
9937 return adjustGVALinkageForExternalDefinitionKind(*this, VD,
9938 adjustGVALinkageForAttributes(*this, VD,
9939 basicGVALinkageForVariable(*this, VD)));
9940}
9941
9942bool ASTContext::DeclMustBeEmitted(const Decl *D) {
9943 if (const auto *VD = dyn_cast<VarDecl>(D)) {
9944 if (!VD->isFileVarDecl())
9945 return false;
9946 // Global named register variables (GNU extension) are never emitted.
9947 if (VD->getStorageClass() == SC_Register)
9948 return false;
9949 if (VD->getDescribedVarTemplate() ||
9950 isa<VarTemplatePartialSpecializationDecl>(VD))
9951 return false;
9952 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
9953 // We never need to emit an uninstantiated function template.
9954 if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
9955 return false;
9956 } else if (isa<PragmaCommentDecl>(D))
9957 return true;
9958 else if (isa<PragmaDetectMismatchDecl>(D))
9959 return true;
9960 else if (isa<OMPThreadPrivateDecl>(D))
9961 return !D->getDeclContext()->isDependentContext();
9962 else if (isa<OMPAllocateDecl>(D))
9963 return !D->getDeclContext()->isDependentContext();
9964 else if (isa<OMPDeclareReductionDecl>(D) || isa<OMPDeclareMapperDecl>(D))
9965 return !D->getDeclContext()->isDependentContext();
9966 else if (isa<ImportDecl>(D))
9967 return true;
9968 else
9969 return false;
9970
9971 if (D->isFromASTFile() && !LangOpts.BuildingPCHWithObjectFile) {
9972 assert(getExternalSource() && "It's from an AST file; must have a source.")((getExternalSource() && "It's from an AST file; must have a source."
) ? static_cast<void> (0) : __assert_fail ("getExternalSource() && \"It's from an AST file; must have a source.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 9972, __PRETTY_FUNCTION__))
;
9973 // On Windows, PCH files are built together with an object file. If this
9974 // declaration comes from such a PCH and DeclMustBeEmitted would return
9975 // true, it would have returned true and the decl would have been emitted
9976 // into that object file, so it doesn't need to be emitted here.
9977 // Note that decls are still emitted if they're referenced, as usual;
9978 // DeclMustBeEmitted is used to decide whether a decl must be emitted even
9979 // if it's not referenced.
9980 //
9981 // Explicit template instantiation definitions are tricky. If there was an
9982 // explicit template instantiation decl in the PCH before, it will look like
9983 // the definition comes from there, even if that was just the declaration.
9984 // (Explicit instantiation defs of variable templates always get emitted.)
9985 bool IsExpInstDef =
9986 isa<FunctionDecl>(D) &&
9987 cast<FunctionDecl>(D)->getTemplateSpecializationKind() ==
9988 TSK_ExplicitInstantiationDefinition;
9989
9990 // Implicit member function definitions, such as operator= might not be
9991 // marked as template specializations, since they're not coming from a
9992 // template but synthesized directly on the class.
9993 IsExpInstDef |=
9994 isa<CXXMethodDecl>(D) &&
9995 cast<CXXMethodDecl>(D)->getParent()->getTemplateSpecializationKind() ==
9996 TSK_ExplicitInstantiationDefinition;
9997
9998 if (getExternalSource()->DeclIsFromPCHWithObjectFile(D) && !IsExpInstDef)
9999 return false;
10000 }
10001
10002 // If this is a member of a class template, we do not need to emit it.
10003 if (D->getDeclContext()->isDependentContext())
10004 return false;
10005
10006 // Weak references don't produce any output by themselves.
10007 if (D->hasAttr<WeakRefAttr>())
10008 return false;
10009
10010 // Aliases and used decls are required.
10011 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
10012 return true;
10013
10014 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
10015 // Forward declarations aren't required.
10016 if (!FD->doesThisDeclarationHaveABody())
10017 return FD->doesDeclarationForceExternallyVisibleDefinition();
10018
10019 // Constructors and destructors are required.
10020 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
10021 return true;
10022
10023 // The key function for a class is required. This rule only comes
10024 // into play when inline functions can be key functions, though.
10025 if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) {
10026 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
10027 const CXXRecordDecl *RD = MD->getParent();
10028 if (MD->isOutOfLine() && RD->isDynamicClass()) {
10029 const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD);
10030 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
10031 return true;
10032 }
10033 }
10034 }
10035
10036 GVALinkage Linkage = GetGVALinkageForFunction(FD);
10037
10038 // static, static inline, always_inline, and extern inline functions can
10039 // always be deferred. Normal inline functions can be deferred in C99/C++.
10040 // Implicit template instantiations can also be deferred in C++.
10041 return !isDiscardableGVALinkage(Linkage);
10042 }
10043
10044 const auto *VD = cast<VarDecl>(D);
10045 assert(VD->isFileVarDecl() && "Expected file scoped var")((VD->isFileVarDecl() && "Expected file scoped var"
) ? static_cast<void> (0) : __assert_fail ("VD->isFileVarDecl() && \"Expected file scoped var\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10045, __PRETTY_FUNCTION__))
;
10046
10047 // If the decl is marked as `declare target to`, it should be emitted for the
10048 // host and for the device.
10049 if (LangOpts.OpenMP &&
10050 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
10051 return true;
10052
10053 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly &&
10054 !isMSStaticDataMemberInlineDefinition(VD))
10055 return false;
10056
10057 // Variables that can be needed in other TUs are required.
10058 auto Linkage = GetGVALinkageForVariable(VD);
10059 if (!isDiscardableGVALinkage(Linkage))
10060 return true;
10061
10062 // We never need to emit a variable that is available in another TU.
10063 if (Linkage == GVA_AvailableExternally)
10064 return false;
10065
10066 // Variables that have destruction with side-effects are required.
10067 if (VD->needsDestruction(*this))
10068 return true;
10069
10070 // Variables that have initialization with side-effects are required.
10071 if (VD->getInit() && VD->getInit()->HasSideEffects(*this) &&
10072 // We can get a value-dependent initializer during error recovery.
10073 (VD->getInit()->isValueDependent() || !VD->evaluateValue()))
10074 return true;
10075
10076 // Likewise, variables with tuple-like bindings are required if their
10077 // bindings have side-effects.
10078 if (const auto *DD = dyn_cast<DecompositionDecl>(VD))
10079 for (const auto *BD : DD->bindings())
10080 if (const auto *BindingVD = BD->getHoldingVar())
10081 if (DeclMustBeEmitted(BindingVD))
10082 return true;
10083
10084 return false;
10085}
10086
10087void ASTContext::forEachMultiversionedFunctionVersion(
10088 const FunctionDecl *FD,
10089 llvm::function_ref<void(FunctionDecl *)> Pred) const {
10090 assert(FD->isMultiVersion() && "Only valid for multiversioned functions")((FD->isMultiVersion() && "Only valid for multiversioned functions"
) ? static_cast<void> (0) : __assert_fail ("FD->isMultiVersion() && \"Only valid for multiversioned functions\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10090, __PRETTY_FUNCTION__))
;
10091 llvm::SmallDenseSet<const FunctionDecl*, 4> SeenDecls;
10092 FD = FD->getMostRecentDecl();
10093 for (auto *CurDecl :
10094 FD->getDeclContext()->getRedeclContext()->lookup(FD->getDeclName())) {
10095 FunctionDecl *CurFD = CurDecl->getAsFunction()->getMostRecentDecl();
10096 if (CurFD && hasSameType(CurFD->getType(), FD->getType()) &&
10097 std::end(SeenDecls) == llvm::find(SeenDecls, CurFD)) {
10098 SeenDecls.insert(CurFD);
10099 Pred(CurFD);
10100 }
10101 }
10102}
10103
10104CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic,
10105 bool IsCXXMethod,
10106 bool IsBuiltin) const {
10107 // Pass through to the C++ ABI object
10108 if (IsCXXMethod)
10109 return ABI->getDefaultMethodCallConv(IsVariadic);
10110
10111 // Builtins ignore user-specified default calling convention and remain the
10112 // Target's default calling convention.
10113 if (!IsBuiltin) {
10114 switch (LangOpts.getDefaultCallingConv()) {
10115 case LangOptions::DCC_None:
10116 break;
10117 case LangOptions::DCC_CDecl:
10118 return CC_C;
10119 case LangOptions::DCC_FastCall:
10120 if (getTargetInfo().hasFeature("sse2") && !IsVariadic)
10121 return CC_X86FastCall;
10122 break;
10123 case LangOptions::DCC_StdCall:
10124 if (!IsVariadic)
10125 return CC_X86StdCall;
10126 break;
10127 case LangOptions::DCC_VectorCall:
10128 // __vectorcall cannot be applied to variadic functions.
10129 if (!IsVariadic)
10130 return CC_X86VectorCall;
10131 break;
10132 case LangOptions::DCC_RegCall:
10133 // __regcall cannot be applied to variadic functions.
10134 if (!IsVariadic)
10135 return CC_X86RegCall;
10136 break;
10137 }
10138 }
10139 return Target->getDefaultCallingConv();
10140}
10141
10142bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
10143 // Pass through to the C++ ABI object
10144 return ABI->isNearlyEmpty(RD);
10145}
10146
10147VTableContextBase *ASTContext::getVTableContext() {
10148 if (!VTContext.get()) {
10149 if (Target->getCXXABI().isMicrosoft())
10150 VTContext.reset(new MicrosoftVTableContext(*this));
10151 else
10152 VTContext.reset(new ItaniumVTableContext(*this));
10153 }
10154 return VTContext.get();
10155}
10156
10157MangleContext *ASTContext::createMangleContext(const TargetInfo *T) {
10158 if (!T)
10159 T = Target;
10160 switch (T->getCXXABI().getKind()) {
10161 case TargetCXXABI::GenericAArch64:
10162 case TargetCXXABI::GenericItanium:
10163 case TargetCXXABI::GenericARM:
10164 case TargetCXXABI::GenericMIPS:
10165 case TargetCXXABI::iOS:
10166 case TargetCXXABI::iOS64:
10167 case TargetCXXABI::WebAssembly:
10168 case TargetCXXABI::WatchOS:
10169 return ItaniumMangleContext::create(*this, getDiagnostics());
10170 case TargetCXXABI::Microsoft:
10171 return MicrosoftMangleContext::create(*this, getDiagnostics());
10172 }
10173 llvm_unreachable("Unsupported ABI")::llvm::llvm_unreachable_internal("Unsupported ABI", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10173)
;
10174}
10175
10176CXXABI::~CXXABI() = default;
10177
10178size_t ASTContext::getSideTableAllocatedMemory() const {
10179 return ASTRecordLayouts.getMemorySize() +
10180 llvm::capacity_in_bytes(ObjCLayouts) +
10181 llvm::capacity_in_bytes(KeyFunctions) +
10182 llvm::capacity_in_bytes(ObjCImpls) +
10183 llvm::capacity_in_bytes(BlockVarCopyInits) +
10184 llvm::capacity_in_bytes(DeclAttrs) +
10185 llvm::capacity_in_bytes(TemplateOrInstantiation) +
10186 llvm::capacity_in_bytes(InstantiatedFromUsingDecl) +
10187 llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) +
10188 llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) +
10189 llvm::capacity_in_bytes(OverriddenMethods) +
10190 llvm::capacity_in_bytes(Types) +
10191 llvm::capacity_in_bytes(VariableArrayTypes);
10192}
10193
10194/// getIntTypeForBitwidth -
10195/// sets integer QualTy according to specified details:
10196/// bitwidth, signed/unsigned.
10197/// Returns empty type if there is no appropriate target types.
10198QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth,
10199 unsigned Signed) const {
10200 TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(DestWidth, Signed);
10201 CanQualType QualTy = getFromTargetType(Ty);
10202 if (!QualTy && DestWidth == 128)
10203 return Signed ? Int128Ty : UnsignedInt128Ty;
10204 return QualTy;
10205}
10206
10207/// getRealTypeForBitwidth -
10208/// sets floating point QualTy according to specified bitwidth.
10209/// Returns empty type if there is no appropriate target types.
10210QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth) const {
10211 TargetInfo::RealType Ty = getTargetInfo().getRealTypeByWidth(DestWidth);
10212 switch (Ty) {
10213 case TargetInfo::Float:
10214 return FloatTy;
10215 case TargetInfo::Double:
10216 return DoubleTy;
10217 case TargetInfo::LongDouble:
10218 return LongDoubleTy;
10219 case TargetInfo::Float128:
10220 return Float128Ty;
10221 case TargetInfo::NoFloat:
10222 return {};
10223 }
10224
10225 llvm_unreachable("Unhandled TargetInfo::RealType value")::llvm::llvm_unreachable_internal("Unhandled TargetInfo::RealType value"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10225)
;
10226}
10227
10228void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) {
10229 if (Number > 1)
10230 MangleNumbers[ND] = Number;
10231}
10232
10233unsigned ASTContext::getManglingNumber(const NamedDecl *ND) const {
10234 auto I = MangleNumbers.find(ND);
10235 return I != MangleNumbers.end() ? I->second : 1;
10236}
10237
10238void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) {
10239 if (Number > 1)
10240 StaticLocalNumbers[VD] = Number;
10241}
10242
10243unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const {
10244 auto I = StaticLocalNumbers.find(VD);
10245 return I != StaticLocalNumbers.end() ? I->second : 1;
10246}
10247
10248MangleNumberingContext &
10249ASTContext::getManglingNumberContext(const DeclContext *DC) {
10250 assert(LangOpts.CPlusPlus)((LangOpts.CPlusPlus) ? static_cast<void> (0) : __assert_fail
("LangOpts.CPlusPlus", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10250, __PRETTY_FUNCTION__))
; // We don't need mangling numbers for plain C.
10251 std::unique_ptr<MangleNumberingContext> &MCtx = MangleNumberingContexts[DC];
10252 if (!MCtx)
10253 MCtx = createMangleNumberingContext();
10254 return *MCtx;
10255}
10256
10257std::unique_ptr<MangleNumberingContext>
10258ASTContext::createMangleNumberingContext() const {
10259 return ABI->createMangleNumberingContext();
10260}
10261
10262const CXXConstructorDecl *
10263ASTContext::getCopyConstructorForExceptionObject(CXXRecordDecl *RD) {
10264 return ABI->getCopyConstructorForExceptionObject(
10265 cast<CXXRecordDecl>(RD->getFirstDecl()));
10266}
10267
10268void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
10269 CXXConstructorDecl *CD) {
10270 return ABI->addCopyConstructorForExceptionObject(
10271 cast<CXXRecordDecl>(RD->getFirstDecl()),
10272 cast<CXXConstructorDecl>(CD->getFirstDecl()));
10273}
10274
10275void ASTContext::addTypedefNameForUnnamedTagDecl(TagDecl *TD,
10276 TypedefNameDecl *DD) {
10277 return ABI->addTypedefNameForUnnamedTagDecl(TD, DD);
10278}
10279
10280TypedefNameDecl *
10281ASTContext::getTypedefNameForUnnamedTagDecl(const TagDecl *TD) {
10282 return ABI->getTypedefNameForUnnamedTagDecl(TD);
10283}
10284
10285void ASTContext::addDeclaratorForUnnamedTagDecl(TagDecl *TD,
10286 DeclaratorDecl *DD) {
10287 return ABI->addDeclaratorForUnnamedTagDecl(TD, DD);
10288}
10289
10290DeclaratorDecl *ASTContext::getDeclaratorForUnnamedTagDecl(const TagDecl *TD) {
10291 return ABI->getDeclaratorForUnnamedTagDecl(TD);
10292}
10293
10294void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) {
10295 ParamIndices[D] = index;
10296}
10297
10298unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const {
10299 ParameterIndexTable::const_iterator I = ParamIndices.find(D);
10300 assert(I != ParamIndices.end() &&((I != ParamIndices.end() && "ParmIndices lacks entry set by ParmVarDecl"
) ? static_cast<void> (0) : __assert_fail ("I != ParamIndices.end() && \"ParmIndices lacks entry set by ParmVarDecl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10301, __PRETTY_FUNCTION__))
10301 "ParmIndices lacks entry set by ParmVarDecl")((I != ParamIndices.end() && "ParmIndices lacks entry set by ParmVarDecl"
) ? static_cast<void> (0) : __assert_fail ("I != ParamIndices.end() && \"ParmIndices lacks entry set by ParmVarDecl\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10301, __PRETTY_FUNCTION__))
;
10302 return I->second;
10303}
10304
10305APValue *
10306ASTContext::getMaterializedTemporaryValue(const MaterializeTemporaryExpr *E,
10307 bool MayCreate) {
10308 assert(E && E->getStorageDuration() == SD_Static &&((E && E->getStorageDuration() == SD_Static &&
"don't need to cache the computed value for this temporary")
? static_cast<void> (0) : __assert_fail ("E && E->getStorageDuration() == SD_Static && \"don't need to cache the computed value for this temporary\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10309, __PRETTY_FUNCTION__))
10309 "don't need to cache the computed value for this temporary")((E && E->getStorageDuration() == SD_Static &&
"don't need to cache the computed value for this temporary")
? static_cast<void> (0) : __assert_fail ("E && E->getStorageDuration() == SD_Static && \"don't need to cache the computed value for this temporary\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10309, __PRETTY_FUNCTION__))
;
10310 if (MayCreate) {
10311 APValue *&MTVI = MaterializedTemporaryValues[E];
10312 if (!MTVI)
10313 MTVI = new (*this) APValue;
10314 return MTVI;
10315 }
10316
10317 return MaterializedTemporaryValues.lookup(E);
10318}
10319
10320QualType ASTContext::getStringLiteralArrayType(QualType EltTy,
10321 unsigned Length) const {
10322 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
10323 if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings)
10324 EltTy = EltTy.withConst();
10325
10326 EltTy = adjustStringLiteralBaseType(EltTy);
10327
10328 // Get an array type for the string, according to C99 6.4.5. This includes
10329 // the null terminator character.
10330 return getConstantArrayType(EltTy, llvm::APInt(32, Length + 1),
10331 ArrayType::Normal, /*IndexTypeQuals*/ 0);
10332}
10333
10334StringLiteral *
10335ASTContext::getPredefinedStringLiteralFromCache(StringRef Key) const {
10336 StringLiteral *&Result = StringLiteralCache[Key];
10337 if (!Result)
10338 Result = StringLiteral::Create(
10339 *this, Key, StringLiteral::Ascii,
10340 /*Pascal*/ false, getStringLiteralArrayType(CharTy, Key.size()),
10341 SourceLocation());
10342 return Result;
10343}
10344
10345bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const {
10346 const llvm::Triple &T = getTargetInfo().getTriple();
10347 if (!T.isOSDarwin())
10348 return false;
10349
10350 if (!(T.isiOS() && T.isOSVersionLT(7)) &&
10351 !(T.isMacOSX() && T.isOSVersionLT(10, 9)))
10352 return false;
10353
10354 QualType AtomicTy = E->getPtr()->getType()->getPointeeType();
10355 CharUnits sizeChars = getTypeSizeInChars(AtomicTy);
10356 uint64_t Size = sizeChars.getQuantity();
10357 CharUnits alignChars = getTypeAlignInChars(AtomicTy);
10358 unsigned Align = alignChars.getQuantity();
10359 unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth();
10360 return (Size != Align || toBits(sizeChars) > MaxInlineWidthInBits);
10361}
10362
10363/// Template specializations to abstract away from pointers and TypeLocs.
10364/// @{
10365template <typename T>
10366static ast_type_traits::DynTypedNode createDynTypedNode(const T &Node) {
10367 return ast_type_traits::DynTypedNode::create(*Node);
10368}
10369template <>
10370ast_type_traits::DynTypedNode createDynTypedNode(const TypeLoc &Node) {
10371 return ast_type_traits::DynTypedNode::create(Node);
10372}
10373template <>
10374ast_type_traits::DynTypedNode
10375createDynTypedNode(const NestedNameSpecifierLoc &Node) {
10376 return ast_type_traits::DynTypedNode::create(Node);
10377}
10378/// @}
10379
10380/// A \c RecursiveASTVisitor that builds a map from nodes to their
10381/// parents as defined by the \c RecursiveASTVisitor.
10382///
10383/// Note that the relationship described here is purely in terms of AST
10384/// traversal - there are other relationships (for example declaration context)
10385/// in the AST that are better modeled by special matchers.
10386///
10387/// FIXME: Currently only builds up the map using \c Stmt and \c Decl nodes.
10388class ASTContext::ParentMap::ASTVisitor
10389 : public RecursiveASTVisitor<ASTVisitor> {
10390public:
10391 ASTVisitor(ParentMap &Map) : Map(Map) {}
10392
10393private:
10394 friend class RecursiveASTVisitor<ASTVisitor>;
10395
10396 using VisitorBase = RecursiveASTVisitor<ASTVisitor>;
10397
10398 bool shouldVisitTemplateInstantiations() const { return true; }
10399
10400 bool shouldVisitImplicitCode() const { return true; }
10401
10402 template <typename T, typename MapNodeTy, typename BaseTraverseFn,
10403 typename MapTy>
10404 bool TraverseNode(T Node, MapNodeTy MapNode, BaseTraverseFn BaseTraverse,
10405 MapTy *Parents) {
10406 if (!Node)
10407 return true;
10408 if (ParentStack.size() > 0) {
10409 // FIXME: Currently we add the same parent multiple times, but only
10410 // when no memoization data is available for the type.
10411 // For example when we visit all subexpressions of template
10412 // instantiations; this is suboptimal, but benign: the only way to
10413 // visit those is with hasAncestor / hasParent, and those do not create
10414 // new matches.
10415 // The plan is to enable DynTypedNode to be storable in a map or hash
10416 // map. The main problem there is to implement hash functions /
10417 // comparison operators for all types that DynTypedNode supports that
10418 // do not have pointer identity.
10419 auto &NodeOrVector = (*Parents)[MapNode];
10420 if (NodeOrVector.isNull()) {
10421 if (const auto *D = ParentStack.back().get<Decl>())
10422 NodeOrVector = D;
10423 else if (const auto *S = ParentStack.back().get<Stmt>())
10424 NodeOrVector = S;
10425 else
10426 NodeOrVector = new ast_type_traits::DynTypedNode(ParentStack.back());
10427 } else {
10428 if (!NodeOrVector.template is<ParentVector *>()) {
10429 auto *Vector = new ParentVector(
10430 1, getSingleDynTypedNodeFromParentMap(NodeOrVector));
10431 delete NodeOrVector
10432 .template dyn_cast<ast_type_traits::DynTypedNode *>();
10433 NodeOrVector = Vector;
10434 }
10435
10436 auto *Vector = NodeOrVector.template get<ParentVector *>();
10437 // Skip duplicates for types that have memoization data.
10438 // We must check that the type has memoization data before calling
10439 // std::find() because DynTypedNode::operator== can't compare all
10440 // types.
10441 bool Found = ParentStack.back().getMemoizationData() &&
10442 std::find(Vector->begin(), Vector->end(),
10443 ParentStack.back()) != Vector->end();
10444 if (!Found)
10445 Vector->push_back(ParentStack.back());
10446 }
10447 }
10448 ParentStack.push_back(createDynTypedNode(Node));
10449 bool Result = BaseTraverse();
10450 ParentStack.pop_back();
10451 return Result;
10452 }
10453
10454 bool TraverseDecl(Decl *DeclNode) {
10455 return TraverseNode(
10456 DeclNode, DeclNode, [&] { return VisitorBase::TraverseDecl(DeclNode); },
10457 &Map.PointerParents);
10458 }
10459
10460 bool TraverseStmt(Stmt *StmtNode) {
10461 return TraverseNode(
10462 StmtNode, StmtNode, [&] { return VisitorBase::TraverseStmt(StmtNode); },
10463 &Map.PointerParents);
10464 }
10465
10466 bool TraverseTypeLoc(TypeLoc TypeLocNode) {
10467 return TraverseNode(
10468 TypeLocNode, ast_type_traits::DynTypedNode::create(TypeLocNode),
10469 [&] { return VisitorBase::TraverseTypeLoc(TypeLocNode); },
10470 &Map.OtherParents);
10471 }
10472
10473 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLocNode) {
10474 return TraverseNode(
10475 NNSLocNode, ast_type_traits::DynTypedNode::create(NNSLocNode),
10476 [&] { return VisitorBase::TraverseNestedNameSpecifierLoc(NNSLocNode); },
10477 &Map.OtherParents);
10478 }
10479
10480 ParentMap &Map;
10481 llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack;
10482};
10483
10484ASTContext::ParentMap::ParentMap(ASTContext &Ctx) {
10485 ASTVisitor(*this).TraverseAST(Ctx);
10486}
10487
10488ASTContext::DynTypedNodeList
10489ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) {
10490 if (!Parents)
10491 // We build the parent map for the traversal scope (usually whole TU), as
10492 // hasAncestor can escape any subtree.
10493 Parents = std::make_unique<ParentMap>(*this);
10494 return Parents->getParents(Node);
10495}
10496
10497bool
10498ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl,
10499 const ObjCMethodDecl *MethodImpl) {
10500 // No point trying to match an unavailable/deprecated mothod.
10501 if (MethodDecl->hasAttr<UnavailableAttr>()
10502 || MethodDecl->hasAttr<DeprecatedAttr>())
10503 return false;
10504 if (MethodDecl->getObjCDeclQualifier() !=
10505 MethodImpl->getObjCDeclQualifier())
10506 return false;
10507 if (!hasSameType(MethodDecl->getReturnType(), MethodImpl->getReturnType()))
10508 return false;
10509
10510 if (MethodDecl->param_size() != MethodImpl->param_size())
10511 return false;
10512
10513 for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(),
10514 IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(),
10515 EF = MethodDecl->param_end();
10516 IM != EM && IF != EF; ++IM, ++IF) {
10517 const ParmVarDecl *DeclVar = (*IF);
10518 const ParmVarDecl *ImplVar = (*IM);
10519 if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier())
10520 return false;
10521 if (!hasSameType(DeclVar->getType(), ImplVar->getType()))
10522 return false;
10523 }
10524
10525 return (MethodDecl->isVariadic() == MethodImpl->isVariadic());
10526}
10527
10528uint64_t ASTContext::getTargetNullPointerValue(QualType QT) const {
10529 LangAS AS;
10530 if (QT->getUnqualifiedDesugaredType()->isNullPtrType())
10531 AS = LangAS::Default;
10532 else
10533 AS = QT->getPointeeType().getAddressSpace();
10534
10535 return getTargetInfo().getNullPointerValue(AS);
10536}
10537
10538unsigned ASTContext::getTargetAddressSpace(LangAS AS) const {
10539 if (isTargetAddressSpace(AS))
10540 return toTargetAddressSpace(AS);
10541 else
10542 return (*AddrSpaceMap)[(unsigned)AS];
10543}
10544
10545QualType ASTContext::getCorrespondingSaturatedType(QualType Ty) const {
10546 assert(Ty->isFixedPointType())((Ty->isFixedPointType()) ? static_cast<void> (0) : __assert_fail
("Ty->isFixedPointType()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10546, __PRETTY_FUNCTION__))
;
10547
10548 if (Ty->isSaturatedFixedPointType()) return Ty;
10549
10550 switch (Ty->castAs<BuiltinType>()->getKind()) {
10551 default:
10552 llvm_unreachable("Not a fixed point type!")::llvm::llvm_unreachable_internal("Not a fixed point type!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10552)
;
10553 case BuiltinType::ShortAccum:
10554 return SatShortAccumTy;
10555 case BuiltinType::Accum:
10556 return SatAccumTy;
10557 case BuiltinType::LongAccum:
10558 return SatLongAccumTy;
10559 case BuiltinType::UShortAccum:
10560 return SatUnsignedShortAccumTy;
10561 case BuiltinType::UAccum:
10562 return SatUnsignedAccumTy;
10563 case BuiltinType::ULongAccum:
10564 return SatUnsignedLongAccumTy;
10565 case BuiltinType::ShortFract:
10566 return SatShortFractTy;
10567 case BuiltinType::Fract:
10568 return SatFractTy;
10569 case BuiltinType::LongFract:
10570 return SatLongFractTy;
10571 case BuiltinType::UShortFract:
10572 return SatUnsignedShortFractTy;
10573 case BuiltinType::UFract:
10574 return SatUnsignedFractTy;
10575 case BuiltinType::ULongFract:
10576 return SatUnsignedLongFractTy;
10577 }
10578}
10579
10580LangAS ASTContext::getLangASForBuiltinAddressSpace(unsigned AS) const {
10581 if (LangOpts.OpenCL)
10582 return getTargetInfo().getOpenCLBuiltinAddressSpace(AS);
10583
10584 if (LangOpts.CUDA)
10585 return getTargetInfo().getCUDABuiltinAddressSpace(AS);
10586
10587 return getLangASFromTargetAS(AS);
10588}
10589
10590// Explicitly instantiate this in case a Redeclarable<T> is used from a TU that
10591// doesn't include ASTContext.h
10592template
10593clang::LazyGenerationalUpdatePtr<
10594 const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType
10595clang::LazyGenerationalUpdatePtr<
10596 const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue(
10597 const clang::ASTContext &Ctx, Decl *Value);
10598
10599unsigned char ASTContext::getFixedPointScale(QualType Ty) const {
10600 assert(Ty->isFixedPointType())((Ty->isFixedPointType()) ? static_cast<void> (0) : __assert_fail
("Ty->isFixedPointType()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10600, __PRETTY_FUNCTION__))
;
10601
10602 const auto *BT = Ty->getAs<BuiltinType>();
10603 const TargetInfo &Target = getTargetInfo();
10604 switch (BT->getKind()) {
10605 default:
10606 llvm_unreachable("Not a fixed point type!")::llvm::llvm_unreachable_internal("Not a fixed point type!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10606)
;
10607 case BuiltinType::ShortAccum:
10608 case BuiltinType::SatShortAccum:
10609 return Target.getShortAccumScale();
10610 case BuiltinType::Accum:
10611 case BuiltinType::SatAccum:
10612 return Target.getAccumScale();
10613 case BuiltinType::LongAccum:
10614 case BuiltinType::SatLongAccum:
10615 return Target.getLongAccumScale();
10616 case BuiltinType::UShortAccum:
10617 case BuiltinType::SatUShortAccum:
10618 return Target.getUnsignedShortAccumScale();
10619 case BuiltinType::UAccum:
10620 case BuiltinType::SatUAccum:
10621 return Target.getUnsignedAccumScale();
10622 case BuiltinType::ULongAccum:
10623 case BuiltinType::SatULongAccum:
10624 return Target.getUnsignedLongAccumScale();
10625 case BuiltinType::ShortFract:
10626 case BuiltinType::SatShortFract:
10627 return Target.getShortFractScale();
10628 case BuiltinType::Fract:
10629 case BuiltinType::SatFract:
10630 return Target.getFractScale();
10631 case BuiltinType::LongFract:
10632 case BuiltinType::SatLongFract:
10633 return Target.getLongFractScale();
10634 case BuiltinType::UShortFract:
10635 case BuiltinType::SatUShortFract:
10636 return Target.getUnsignedShortFractScale();
10637 case BuiltinType::UFract:
10638 case BuiltinType::SatUFract:
10639 return Target.getUnsignedFractScale();
10640 case BuiltinType::ULongFract:
10641 case BuiltinType::SatULongFract:
10642 return Target.getUnsignedLongFractScale();
10643 }
10644}
10645
10646unsigned char ASTContext::getFixedPointIBits(QualType Ty) const {
10647 assert(Ty->isFixedPointType())((Ty->isFixedPointType()) ? static_cast<void> (0) : __assert_fail
("Ty->isFixedPointType()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10647, __PRETTY_FUNCTION__))
;
10648
10649 const auto *BT = Ty->getAs<BuiltinType>();
10650 const TargetInfo &Target = getTargetInfo();
10651 switch (BT->getKind()) {
10652 default:
10653 llvm_unreachable("Not a fixed point type!")::llvm::llvm_unreachable_internal("Not a fixed point type!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10653)
;
10654 case BuiltinType::ShortAccum:
10655 case BuiltinType::SatShortAccum:
10656 return Target.getShortAccumIBits();
10657 case BuiltinType::Accum:
10658 case BuiltinType::SatAccum:
10659 return Target.getAccumIBits();
10660 case BuiltinType::LongAccum:
10661 case BuiltinType::SatLongAccum:
10662 return Target.getLongAccumIBits();
10663 case BuiltinType::UShortAccum:
10664 case BuiltinType::SatUShortAccum:
10665 return Target.getUnsignedShortAccumIBits();
10666 case BuiltinType::UAccum:
10667 case BuiltinType::SatUAccum:
10668 return Target.getUnsignedAccumIBits();
10669 case BuiltinType::ULongAccum:
10670 case BuiltinType::SatULongAccum:
10671 return Target.getUnsignedLongAccumIBits();
10672 case BuiltinType::ShortFract:
10673 case BuiltinType::SatShortFract:
10674 case BuiltinType::Fract:
10675 case BuiltinType::SatFract:
10676 case BuiltinType::LongFract:
10677 case BuiltinType::SatLongFract:
10678 case BuiltinType::UShortFract:
10679 case BuiltinType::SatUShortFract:
10680 case BuiltinType::UFract:
10681 case BuiltinType::SatUFract:
10682 case BuiltinType::ULongFract:
10683 case BuiltinType::SatULongFract:
10684 return 0;
10685 }
10686}
10687
10688FixedPointSemantics ASTContext::getFixedPointSemantics(QualType Ty) const {
10689 assert((Ty->isFixedPointType() || Ty->isIntegerType()) &&(((Ty->isFixedPointType() || Ty->isIntegerType()) &&
"Can only get the fixed point semantics for a " "fixed point or integer type."
) ? static_cast<void> (0) : __assert_fail ("(Ty->isFixedPointType() || Ty->isIntegerType()) && \"Can only get the fixed point semantics for a \" \"fixed point or integer type.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10691, __PRETTY_FUNCTION__))
10690 "Can only get the fixed point semantics for a "(((Ty->isFixedPointType() || Ty->isIntegerType()) &&
"Can only get the fixed point semantics for a " "fixed point or integer type."
) ? static_cast<void> (0) : __assert_fail ("(Ty->isFixedPointType() || Ty->isIntegerType()) && \"Can only get the fixed point semantics for a \" \"fixed point or integer type.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10691, __PRETTY_FUNCTION__))
10691 "fixed point or integer type.")(((Ty->isFixedPointType() || Ty->isIntegerType()) &&
"Can only get the fixed point semantics for a " "fixed point or integer type."
) ? static_cast<void> (0) : __assert_fail ("(Ty->isFixedPointType() || Ty->isIntegerType()) && \"Can only get the fixed point semantics for a \" \"fixed point or integer type.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10691, __PRETTY_FUNCTION__))
;
10692 if (Ty->isIntegerType())
10693 return FixedPointSemantics::GetIntegerSemantics(getIntWidth(Ty),
10694 Ty->isSignedIntegerType());
10695
10696 bool isSigned = Ty->isSignedFixedPointType();
10697 return FixedPointSemantics(
10698 static_cast<unsigned>(getTypeSize(Ty)), getFixedPointScale(Ty), isSigned,
10699 Ty->isSaturatedFixedPointType(),
10700 !isSigned && getTargetInfo().doUnsignedFixedPointTypesHavePadding());
10701}
10702
10703APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const {
10704 assert(Ty->isFixedPointType())((Ty->isFixedPointType()) ? static_cast<void> (0) : __assert_fail
("Ty->isFixedPointType()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10704, __PRETTY_FUNCTION__))
;
10705 return APFixedPoint::getMax(getFixedPointSemantics(Ty));
10706}
10707
10708APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const {
10709 assert(Ty->isFixedPointType())((Ty->isFixedPointType()) ? static_cast<void> (0) : __assert_fail
("Ty->isFixedPointType()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10709, __PRETTY_FUNCTION__))
;
10710 return APFixedPoint::getMin(getFixedPointSemantics(Ty));
10711}
10712
10713QualType ASTContext::getCorrespondingSignedFixedPointType(QualType Ty) const {
10714 assert(Ty->isUnsignedFixedPointType() &&((Ty->isUnsignedFixedPointType() && "Expected unsigned fixed point type"
) ? static_cast<void> (0) : __assert_fail ("Ty->isUnsignedFixedPointType() && \"Expected unsigned fixed point type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10715, __PRETTY_FUNCTION__))
10715 "Expected unsigned fixed point type")((Ty->isUnsignedFixedPointType() && "Expected unsigned fixed point type"
) ? static_cast<void> (0) : __assert_fail ("Ty->isUnsignedFixedPointType() && \"Expected unsigned fixed point type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10715, __PRETTY_FUNCTION__))
;
10716 const auto *BTy = Ty->getAs<BuiltinType>();
10717
10718 switch (BTy->getKind()) {
10719 case BuiltinType::UShortAccum:
10720 return ShortAccumTy;
10721 case BuiltinType::UAccum:
10722 return AccumTy;
10723 case BuiltinType::ULongAccum:
10724 return LongAccumTy;
10725 case BuiltinType::SatUShortAccum:
10726 return SatShortAccumTy;
10727 case BuiltinType::SatUAccum:
10728 return SatAccumTy;
10729 case BuiltinType::SatULongAccum:
10730 return SatLongAccumTy;
10731 case BuiltinType::UShortFract:
10732 return ShortFractTy;
10733 case BuiltinType::UFract:
10734 return FractTy;
10735 case BuiltinType::ULongFract:
10736 return LongFractTy;
10737 case BuiltinType::SatUShortFract:
10738 return SatShortFractTy;
10739 case BuiltinType::SatUFract:
10740 return SatFractTy;
10741 case BuiltinType::SatULongFract:
10742 return SatLongFractTy;
10743 default:
10744 llvm_unreachable("Unexpected unsigned fixed point type")::llvm::llvm_unreachable_internal("Unexpected unsigned fixed point type"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/ASTContext.cpp"
, 10744)
;
10745 }
10746}

/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h

1//===- Type.h - C Language Family Type Representation -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// C Language Family Type Representation
11///
12/// This file defines the clang::Type interface and subclasses, used to
13/// represent types for languages in the C family.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CLANG_AST_TYPE_H
18#define LLVM_CLANG_AST_TYPE_H
19
20#include "clang/AST/NestedNameSpecifier.h"
21#include "clang/AST/TemplateName.h"
22#include "clang/Basic/AddressSpaces.h"
23#include "clang/Basic/AttrKinds.h"
24#include "clang/Basic/Diagnostic.h"
25#include "clang/Basic/ExceptionSpecificationType.h"
26#include "clang/Basic/LLVM.h"
27#include "clang/Basic/Linkage.h"
28#include "clang/Basic/PartialDiagnostic.h"
29#include "clang/Basic/SourceLocation.h"
30#include "clang/Basic/Specifiers.h"
31#include "clang/Basic/Visibility.h"
32#include "llvm/ADT/APInt.h"
33#include "llvm/ADT/APSInt.h"
34#include "llvm/ADT/ArrayRef.h"
35#include "llvm/ADT/FoldingSet.h"
36#include "llvm/ADT/None.h"
37#include "llvm/ADT/Optional.h"
38#include "llvm/ADT/PointerIntPair.h"
39#include "llvm/ADT/PointerUnion.h"
40#include "llvm/ADT/StringRef.h"
41#include "llvm/ADT/Twine.h"
42#include "llvm/ADT/iterator_range.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/Compiler.h"
45#include "llvm/Support/ErrorHandling.h"
46#include "llvm/Support/PointerLikeTypeTraits.h"
47#include "llvm/Support/type_traits.h"
48#include "llvm/Support/TrailingObjects.h"
49#include <cassert>
50#include <cstddef>
51#include <cstdint>
52#include <cstring>
53#include <string>
54#include <type_traits>
55#include <utility>
56
57namespace clang {
58
59class ExtQuals;
60class QualType;
61class TagDecl;
62class Type;
63
64enum {
65 TypeAlignmentInBits = 4,
66 TypeAlignment = 1 << TypeAlignmentInBits
67};
68
69} // namespace clang
70
71namespace llvm {
72
73 template <typename T>
74 struct PointerLikeTypeTraits;
75 template<>
76 struct PointerLikeTypeTraits< ::clang::Type*> {
77 static inline void *getAsVoidPointer(::clang::Type *P) { return P; }
78
79 static inline ::clang::Type *getFromVoidPointer(void *P) {
80 return static_cast< ::clang::Type*>(P);
81 }
82
83 enum { NumLowBitsAvailable = clang::TypeAlignmentInBits };
84 };
85
86 template<>
87 struct PointerLikeTypeTraits< ::clang::ExtQuals*> {
88 static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; }
89
90 static inline ::clang::ExtQuals *getFromVoidPointer(void *P) {
91 return static_cast< ::clang::ExtQuals*>(P);
92 }
93
94 enum { NumLowBitsAvailable = clang::TypeAlignmentInBits };
95 };
96
97} // namespace llvm
98
99namespace clang {
100
101class ASTContext;
102template <typename> class CanQual;
103class CXXRecordDecl;
104class DeclContext;
105class EnumDecl;
106class Expr;
107class ExtQualsTypeCommonBase;
108class FunctionDecl;
109class IdentifierInfo;
110class NamedDecl;
111class ObjCInterfaceDecl;
112class ObjCProtocolDecl;
113class ObjCTypeParamDecl;
114struct PrintingPolicy;
115class RecordDecl;
116class Stmt;
117class TagDecl;
118class TemplateArgument;
119class TemplateArgumentListInfo;
120class TemplateArgumentLoc;
121class TemplateTypeParmDecl;
122class TypedefNameDecl;
123class UnresolvedUsingTypenameDecl;
124
125using CanQualType = CanQual<Type>;
126
127// Provide forward declarations for all of the *Type classes.
128#define TYPE(Class, Base) class Class##Type;
129#include "clang/AST/TypeNodes.inc"
130
131/// The collection of all-type qualifiers we support.
132/// Clang supports five independent qualifiers:
133/// * C99: const, volatile, and restrict
134/// * MS: __unaligned
135/// * Embedded C (TR18037): address spaces
136/// * Objective C: the GC attributes (none, weak, or strong)
137class Qualifiers {
138public:
139 enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ.
140 Const = 0x1,
141 Restrict = 0x2,
142 Volatile = 0x4,
143 CVRMask = Const | Volatile | Restrict
144 };
145
146 enum GC {
147 GCNone = 0,
148 Weak,
149 Strong
150 };
151
152 enum ObjCLifetime {
153 /// There is no lifetime qualification on this type.
154 OCL_None,
155
156 /// This object can be modified without requiring retains or
157 /// releases.
158 OCL_ExplicitNone,
159
160 /// Assigning into this object requires the old value to be
161 /// released and the new value to be retained. The timing of the
162 /// release of the old value is inexact: it may be moved to
163 /// immediately after the last known point where the value is
164 /// live.
165 OCL_Strong,
166
167 /// Reading or writing from this object requires a barrier call.
168 OCL_Weak,
169
170 /// Assigning into this object requires a lifetime extension.
171 OCL_Autoreleasing
172 };
173
174 enum {
175 /// The maximum supported address space number.
176 /// 23 bits should be enough for anyone.
177 MaxAddressSpace = 0x7fffffu,
178
179 /// The width of the "fast" qualifier mask.
180 FastWidth = 3,
181
182 /// The fast qualifier mask.
183 FastMask = (1 << FastWidth) - 1
184 };
185
186 /// Returns the common set of qualifiers while removing them from
187 /// the given sets.
188 static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) {
189 // If both are only CVR-qualified, bit operations are sufficient.
190 if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) {
191 Qualifiers Q;
192 Q.Mask = L.Mask & R.Mask;
193 L.Mask &= ~Q.Mask;
194 R.Mask &= ~Q.Mask;
195 return Q;
196 }
197
198 Qualifiers Q;
199 unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers();
200 Q.addCVRQualifiers(CommonCRV);
201 L.removeCVRQualifiers(CommonCRV);
202 R.removeCVRQualifiers(CommonCRV);
203
204 if (L.getObjCGCAttr() == R.getObjCGCAttr()) {
205 Q.setObjCGCAttr(L.getObjCGCAttr());
206 L.removeObjCGCAttr();
207 R.removeObjCGCAttr();
208 }
209
210 if (L.getObjCLifetime() == R.getObjCLifetime()) {
211 Q.setObjCLifetime(L.getObjCLifetime());
212 L.removeObjCLifetime();
213 R.removeObjCLifetime();
214 }
215
216 if (L.getAddressSpace() == R.getAddressSpace()) {
217 Q.setAddressSpace(L.getAddressSpace());
218 L.removeAddressSpace();
219 R.removeAddressSpace();
220 }
221 return Q;
222 }
223
224 static Qualifiers fromFastMask(unsigned Mask) {
225 Qualifiers Qs;
226 Qs.addFastQualifiers(Mask);
227 return Qs;
228 }
229
230 static Qualifiers fromCVRMask(unsigned CVR) {
231 Qualifiers Qs;
232 Qs.addCVRQualifiers(CVR);
233 return Qs;
234 }
235
236 static Qualifiers fromCVRUMask(unsigned CVRU) {
237 Qualifiers Qs;
238 Qs.addCVRUQualifiers(CVRU);
239 return Qs;
240 }
241
242 // Deserialize qualifiers from an opaque representation.
243 static Qualifiers fromOpaqueValue(unsigned opaque) {
244 Qualifiers Qs;
245 Qs.Mask = opaque;
246 return Qs;
247 }
248
249 // Serialize these qualifiers into an opaque representation.
250 unsigned getAsOpaqueValue() const {
251 return Mask;
252 }
253
254 bool hasConst() const { return Mask & Const; }
255 bool hasOnlyConst() const { return Mask == Const; }
256 void removeConst() { Mask &= ~Const; }
257 void addConst() { Mask |= Const; }
258
259 bool hasVolatile() const { return Mask & Volatile; }
260 bool hasOnlyVolatile() const { return Mask == Volatile; }
261 void removeVolatile() { Mask &= ~Volatile; }
262 void addVolatile() { Mask |= Volatile; }
263
264 bool hasRestrict() const { return Mask & Restrict; }
265 bool hasOnlyRestrict() const { return Mask == Restrict; }
266 void removeRestrict() { Mask &= ~Restrict; }
267 void addRestrict() { Mask |= Restrict; }
268
269 bool hasCVRQualifiers() const { return getCVRQualifiers(); }
270 unsigned getCVRQualifiers() const { return Mask & CVRMask; }
271 unsigned getCVRUQualifiers() const { return Mask & (CVRMask | UMask); }
272
273 void setCVRQualifiers(unsigned mask) {
274 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")((!(mask & ~CVRMask) && "bitmask contains non-CVR bits"
) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 274, __PRETTY_FUNCTION__))
;
275 Mask = (Mask & ~CVRMask) | mask;
276 }
277 void removeCVRQualifiers(unsigned mask) {
278 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")((!(mask & ~CVRMask) && "bitmask contains non-CVR bits"
) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 278, __PRETTY_FUNCTION__))
;
279 Mask &= ~mask;
280 }
281 void removeCVRQualifiers() {
282 removeCVRQualifiers(CVRMask);
283 }
284 void addCVRQualifiers(unsigned mask) {
285 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")((!(mask & ~CVRMask) && "bitmask contains non-CVR bits"
) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask) && \"bitmask contains non-CVR bits\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 285, __PRETTY_FUNCTION__))
;
286 Mask |= mask;
287 }
288 void addCVRUQualifiers(unsigned mask) {
289 assert(!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits")((!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits"
) ? static_cast<void> (0) : __assert_fail ("!(mask & ~CVRMask & ~UMask) && \"bitmask contains non-CVRU bits\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 289, __PRETTY_FUNCTION__))
;
290 Mask |= mask;
291 }
292
293 bool hasUnaligned() const { return Mask & UMask; }
294 void setUnaligned(bool flag) {
295 Mask = (Mask & ~UMask) | (flag ? UMask : 0);
296 }
297 void removeUnaligned() { Mask &= ~UMask; }
298 void addUnaligned() { Mask |= UMask; }
299
300 bool hasObjCGCAttr() const { return Mask & GCAttrMask; }
301 GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); }
302 void setObjCGCAttr(GC type) {
303 Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift);
304 }
305 void removeObjCGCAttr() { setObjCGCAttr(GCNone); }
306 void addObjCGCAttr(GC type) {
307 assert(type)((type) ? static_cast<void> (0) : __assert_fail ("type"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 307, __PRETTY_FUNCTION__))
;
308 setObjCGCAttr(type);
309 }
310 Qualifiers withoutObjCGCAttr() const {
311 Qualifiers qs = *this;
312 qs.removeObjCGCAttr();
313 return qs;
314 }
315 Qualifiers withoutObjCLifetime() const {
316 Qualifiers qs = *this;
317 qs.removeObjCLifetime();
318 return qs;
319 }
320 Qualifiers withoutAddressSpace() const {
321 Qualifiers qs = *this;
322 qs.removeAddressSpace();
323 return qs;
324 }
325
326 bool hasObjCLifetime() const { return Mask & LifetimeMask; }
327 ObjCLifetime getObjCLifetime() const {
328 return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift);
329 }
330 void setObjCLifetime(ObjCLifetime type) {
331 Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift);
332 }
333 void removeObjCLifetime() { setObjCLifetime(OCL_None); }
334 void addObjCLifetime(ObjCLifetime type) {
335 assert(type)((type) ? static_cast<void> (0) : __assert_fail ("type"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 335, __PRETTY_FUNCTION__))
;
336 assert(!hasObjCLifetime())((!hasObjCLifetime()) ? static_cast<void> (0) : __assert_fail
("!hasObjCLifetime()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 336, __PRETTY_FUNCTION__))
;
337 Mask |= (type << LifetimeShift);
338 }
339
340 /// True if the lifetime is neither None or ExplicitNone.
341 bool hasNonTrivialObjCLifetime() const {
342 ObjCLifetime lifetime = getObjCLifetime();
343 return (lifetime > OCL_ExplicitNone);
344 }
345
346 /// True if the lifetime is either strong or weak.
347 bool hasStrongOrWeakObjCLifetime() const {
348 ObjCLifetime lifetime = getObjCLifetime();
349 return (lifetime == OCL_Strong || lifetime == OCL_Weak);
350 }
351
352 bool hasAddressSpace() const { return Mask & AddressSpaceMask; }
353 LangAS getAddressSpace() const {
354 return static_cast<LangAS>(Mask >> AddressSpaceShift);
355 }
356 bool hasTargetSpecificAddressSpace() const {
357 return isTargetAddressSpace(getAddressSpace());
358 }
359 /// Get the address space attribute value to be printed by diagnostics.
360 unsigned getAddressSpaceAttributePrintValue() const {
361 auto Addr = getAddressSpace();
362 // This function is not supposed to be used with language specific
363 // address spaces. If that happens, the diagnostic message should consider
364 // printing the QualType instead of the address space value.
365 assert(Addr == LangAS::Default || hasTargetSpecificAddressSpace())((Addr == LangAS::Default || hasTargetSpecificAddressSpace())
? static_cast<void> (0) : __assert_fail ("Addr == LangAS::Default || hasTargetSpecificAddressSpace()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 365, __PRETTY_FUNCTION__))
;
366 if (Addr != LangAS::Default)
367 return toTargetAddressSpace(Addr);
368 // TODO: The diagnostic messages where Addr may be 0 should be fixed
369 // since it cannot differentiate the situation where 0 denotes the default
370 // address space or user specified __attribute__((address_space(0))).
371 return 0;
372 }
373 void setAddressSpace(LangAS space) {
374 assert((unsigned)space <= MaxAddressSpace)(((unsigned)space <= MaxAddressSpace) ? static_cast<void
> (0) : __assert_fail ("(unsigned)space <= MaxAddressSpace"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 374, __PRETTY_FUNCTION__))
;
375 Mask = (Mask & ~AddressSpaceMask)
376 | (((uint32_t) space) << AddressSpaceShift);
377 }
378 void removeAddressSpace() { setAddressSpace(LangAS::Default); }
379 void addAddressSpace(LangAS space) {
380 assert(space != LangAS::Default)((space != LangAS::Default) ? static_cast<void> (0) : __assert_fail
("space != LangAS::Default", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 380, __PRETTY_FUNCTION__))
;
381 setAddressSpace(space);
382 }
383
384 // Fast qualifiers are those that can be allocated directly
385 // on a QualType object.
386 bool hasFastQualifiers() const { return getFastQualifiers(); }
387 unsigned getFastQualifiers() const { return Mask & FastMask; }
388 void setFastQualifiers(unsigned mask) {
389 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")((!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"
) ? static_cast<void> (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 389, __PRETTY_FUNCTION__))
;
390 Mask = (Mask & ~FastMask) | mask;
391 }
392 void removeFastQualifiers(unsigned mask) {
393 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")((!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"
) ? static_cast<void> (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 393, __PRETTY_FUNCTION__))
;
394 Mask &= ~mask;
395 }
396 void removeFastQualifiers() {
397 removeFastQualifiers(FastMask);
398 }
399 void addFastQualifiers(unsigned mask) {
400 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")((!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"
) ? static_cast<void> (0) : __assert_fail ("!(mask & ~FastMask) && \"bitmask contains non-fast qualifier bits\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 400, __PRETTY_FUNCTION__))
;
401 Mask |= mask;
402 }
403
404 /// Return true if the set contains any qualifiers which require an ExtQuals
405 /// node to be allocated.
406 bool hasNonFastQualifiers() const { return Mask & ~FastMask; }
407 Qualifiers getNonFastQualifiers() const {
408 Qualifiers Quals = *this;
409 Quals.setFastQualifiers(0);
410 return Quals;
411 }
412
413 /// Return true if the set contains any qualifiers.
414 bool hasQualifiers() const { return Mask; }
415 bool empty() const { return !Mask; }
416
417 /// Add the qualifiers from the given set to this set.
418 void addQualifiers(Qualifiers Q) {
419 // If the other set doesn't have any non-boolean qualifiers, just
420 // bit-or it in.
421 if (!(Q.Mask & ~CVRMask))
422 Mask |= Q.Mask;
423 else {
424 Mask |= (Q.Mask & CVRMask);
425 if (Q.hasAddressSpace())
426 addAddressSpace(Q.getAddressSpace());
427 if (Q.hasObjCGCAttr())
428 addObjCGCAttr(Q.getObjCGCAttr());
429 if (Q.hasObjCLifetime())
430 addObjCLifetime(Q.getObjCLifetime());
431 }
432 }
433
434 /// Remove the qualifiers from the given set from this set.
435 void removeQualifiers(Qualifiers Q) {
436 // If the other set doesn't have any non-boolean qualifiers, just
437 // bit-and the inverse in.
438 if (!(Q.Mask & ~CVRMask))
439 Mask &= ~Q.Mask;
440 else {
441 Mask &= ~(Q.Mask & CVRMask);
442 if (getObjCGCAttr() == Q.getObjCGCAttr())
443 removeObjCGCAttr();
444 if (getObjCLifetime() == Q.getObjCLifetime())
445 removeObjCLifetime();
446 if (getAddressSpace() == Q.getAddressSpace())
447 removeAddressSpace();
448 }
449 }
450
451 /// Add the qualifiers from the given set to this set, given that
452 /// they don't conflict.
453 void addConsistentQualifiers(Qualifiers qs) {
454 assert(getAddressSpace() == qs.getAddressSpace() ||((getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace
() || !qs.hasAddressSpace()) ? static_cast<void> (0) : __assert_fail
("getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace() || !qs.hasAddressSpace()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 455, __PRETTY_FUNCTION__))
455 !hasAddressSpace() || !qs.hasAddressSpace())((getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace
() || !qs.hasAddressSpace()) ? static_cast<void> (0) : __assert_fail
("getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace() || !qs.hasAddressSpace()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 455, __PRETTY_FUNCTION__))
;
456 assert(getObjCGCAttr() == qs.getObjCGCAttr() ||((getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() ||
!qs.hasObjCGCAttr()) ? static_cast<void> (0) : __assert_fail
("getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 457, __PRETTY_FUNCTION__))
457 !hasObjCGCAttr() || !qs.hasObjCGCAttr())((getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() ||
!qs.hasObjCGCAttr()) ? static_cast<void> (0) : __assert_fail
("getObjCGCAttr() == qs.getObjCGCAttr() || !hasObjCGCAttr() || !qs.hasObjCGCAttr()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 457, __PRETTY_FUNCTION__))
;
458 assert(getObjCLifetime() == qs.getObjCLifetime() ||((getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime
() || !qs.hasObjCLifetime()) ? static_cast<void> (0) : __assert_fail
("getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime() || !qs.hasObjCLifetime()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 459, __PRETTY_FUNCTION__))
459 !hasObjCLifetime() || !qs.hasObjCLifetime())((getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime
() || !qs.hasObjCLifetime()) ? static_cast<void> (0) : __assert_fail
("getObjCLifetime() == qs.getObjCLifetime() || !hasObjCLifetime() || !qs.hasObjCLifetime()"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 459, __PRETTY_FUNCTION__))
;
460 Mask |= qs.Mask;
461 }
462
463 /// Returns true if address space A is equal to or a superset of B.
464 /// OpenCL v2.0 defines conversion rules (OpenCLC v2.0 s6.5.5) and notion of
465 /// overlapping address spaces.
466 /// CL1.1 or CL1.2:
467 /// every address space is a superset of itself.
468 /// CL2.0 adds:
469 /// __generic is a superset of any address space except for __constant.
470 static bool isAddressSpaceSupersetOf(LangAS A, LangAS B) {
471 // Address spaces must match exactly.
472 return A == B ||
473 // Otherwise in OpenCLC v2.0 s6.5.5: every address space except
474 // for __constant can be used as __generic.
475 (A == LangAS::opencl_generic && B != LangAS::opencl_constant);
476 }
477
478 /// Returns true if the address space in these qualifiers is equal to or
479 /// a superset of the address space in the argument qualifiers.
480 bool isAddressSpaceSupersetOf(Qualifiers other) const {
481 return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace());
482 }
483
484 /// Determines if these qualifiers compatibly include another set.
485 /// Generally this answers the question of whether an object with the other
486 /// qualifiers can be safely used as an object with these qualifiers.
487 bool compatiblyIncludes(Qualifiers other) const {
488 return isAddressSpaceSupersetOf(other) &&
489 // ObjC GC qualifiers can match, be added, or be removed, but can't
490 // be changed.
491 (getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() ||
492 !other.hasObjCGCAttr()) &&
493 // ObjC lifetime qualifiers must match exactly.
494 getObjCLifetime() == other.getObjCLifetime() &&
495 // CVR qualifiers may subset.
496 (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)) &&
497 // U qualifier may superset.
498 (!other.hasUnaligned() || hasUnaligned());
499 }
500
501 /// Determines if these qualifiers compatibly include another set of
502 /// qualifiers from the narrow perspective of Objective-C ARC lifetime.
503 ///
504 /// One set of Objective-C lifetime qualifiers compatibly includes the other
505 /// if the lifetime qualifiers match, or if both are non-__weak and the
506 /// including set also contains the 'const' qualifier, or both are non-__weak
507 /// and one is None (which can only happen in non-ARC modes).
508 bool compatiblyIncludesObjCLifetime(Qualifiers other) const {
509 if (getObjCLifetime() == other.getObjCLifetime())
510 return true;
511
512 if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak)
513 return false;
514
515 if (getObjCLifetime() == OCL_None || other.getObjCLifetime() == OCL_None)
516 return true;
517
518 return hasConst();
519 }
520
521 /// Determine whether this set of qualifiers is a strict superset of
522 /// another set of qualifiers, not considering qualifier compatibility.
523 bool isStrictSupersetOf(Qualifiers Other) const;
524
525 bool operator==(Qualifiers Other) const { return Mask == Other.Mask; }
526 bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; }
16
Assuming 'Mask' is equal to 'Other.Mask'
17
Returning zero, which participates in a condition later
527
528 explicit operator bool() const { return hasQualifiers(); }
529
530 Qualifiers &operator+=(Qualifiers R) {
531 addQualifiers(R);
532 return *this;
533 }
534
535 // Union two qualifier sets. If an enumerated qualifier appears
536 // in both sets, use the one from the right.
537 friend Qualifiers operator+(Qualifiers L, Qualifiers R) {
538 L += R;
539 return L;
540 }
541
542 Qualifiers &operator-=(Qualifiers R) {
543 removeQualifiers(R);
544 return *this;
545 }
546
547 /// Compute the difference between two qualifier sets.
548 friend Qualifiers operator-(Qualifiers L, Qualifiers R) {
549 L -= R;
550 return L;
551 }
552
553 std::string getAsString() const;
554 std::string getAsString(const PrintingPolicy &Policy) const;
555
556 bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const;
557 void print(raw_ostream &OS, const PrintingPolicy &Policy,
558 bool appendSpaceIfNonEmpty = false) const;
559
560 void Profile(llvm::FoldingSetNodeID &ID) const {
561 ID.AddInteger(Mask);
562 }
563
564private:
565 // bits: |0 1 2|3|4 .. 5|6 .. 8|9 ... 31|
566 // |C R V|U|GCAttr|Lifetime|AddressSpace|
567 uint32_t Mask = 0;
568
569 static const uint32_t UMask = 0x8;
570 static const uint32_t UShift = 3;
571 static const uint32_t GCAttrMask = 0x30;
572 static const uint32_t GCAttrShift = 4;
573 static const uint32_t LifetimeMask = 0x1C0;
574 static const uint32_t LifetimeShift = 6;
575 static const uint32_t AddressSpaceMask =
576 ~(CVRMask | UMask | GCAttrMask | LifetimeMask);
577 static const uint32_t AddressSpaceShift = 9;
578};
579
580/// A std::pair-like structure for storing a qualified type split
581/// into its local qualifiers and its locally-unqualified type.
582struct SplitQualType {
583 /// The locally-unqualified type.
584 const Type *Ty = nullptr;
585
586 /// The local qualifiers.
587 Qualifiers Quals;
588
589 SplitQualType() = default;
590 SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {}
591
592 SplitQualType getSingleStepDesugaredType() const; // end of this file
593
594 // Make std::tie work.
595 std::pair<const Type *,Qualifiers> asPair() const {
596 return std::pair<const Type *, Qualifiers>(Ty, Quals);
597 }
598
599 friend bool operator==(SplitQualType a, SplitQualType b) {
600 return a.Ty == b.Ty && a.Quals == b.Quals;
601 }
602 friend bool operator!=(SplitQualType a, SplitQualType b) {
603 return a.Ty != b.Ty || a.Quals != b.Quals;
604 }
605};
606
607/// The kind of type we are substituting Objective-C type arguments into.
608///
609/// The kind of substitution affects the replacement of type parameters when
610/// no concrete type information is provided, e.g., when dealing with an
611/// unspecialized type.
612enum class ObjCSubstitutionContext {
613 /// An ordinary type.
614 Ordinary,
615
616 /// The result type of a method or function.
617 Result,
618
619 /// The parameter type of a method or function.
620 Parameter,
621
622 /// The type of a property.
623 Property,
624
625 /// The superclass of a type.
626 Superclass,
627};
628
629/// A (possibly-)qualified type.
630///
631/// For efficiency, we don't store CV-qualified types as nodes on their
632/// own: instead each reference to a type stores the qualifiers. This
633/// greatly reduces the number of nodes we need to allocate for types (for
634/// example we only need one for 'int', 'const int', 'volatile int',
635/// 'const volatile int', etc).
636///
637/// As an added efficiency bonus, instead of making this a pair, we
638/// just store the two bits we care about in the low bits of the
639/// pointer. To handle the packing/unpacking, we make QualType be a
640/// simple wrapper class that acts like a smart pointer. A third bit
641/// indicates whether there are extended qualifiers present, in which
642/// case the pointer points to a special structure.
643class QualType {
644 friend class QualifierCollector;
645
646 // Thankfully, these are efficiently composable.
647 llvm::PointerIntPair<llvm::PointerUnion<const Type *, const ExtQuals *>,
648 Qualifiers::FastWidth> Value;
649
650 const ExtQuals *getExtQualsUnsafe() const {
651 return Value.getPointer().get<const ExtQuals*>();
652 }
653
654 const Type *getTypePtrUnsafe() const {
655 return Value.getPointer().get<const Type*>();
656 }
657
658 const ExtQualsTypeCommonBase *getCommonPtr() const {
659 assert(!isNull() && "Cannot retrieve a NULL type pointer")((!isNull() && "Cannot retrieve a NULL type pointer")
? static_cast<void> (0) : __assert_fail ("!isNull() && \"Cannot retrieve a NULL type pointer\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 659, __PRETTY_FUNCTION__))
;
660 auto CommonPtrVal = reinterpret_cast<uintptr_t>(Value.getOpaqueValue());
661 CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1);
662 return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal);
663 }
664
665public:
666 QualType() = default;
667 QualType(const Type *Ptr, unsigned Quals) : Value(Ptr, Quals) {}
668 QualType(const ExtQuals *Ptr, unsigned Quals) : Value(Ptr, Quals) {}
669
670 unsigned getLocalFastQualifiers() const { return Value.getInt(); }
671 void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); }
672
673 /// Retrieves a pointer to the underlying (unqualified) type.
674 ///
675 /// This function requires that the type not be NULL. If the type might be
676 /// NULL, use the (slightly less efficient) \c getTypePtrOrNull().
677 const Type *getTypePtr() const;
678
679 const Type *getTypePtrOrNull() const;
680
681 /// Retrieves a pointer to the name of the base type.
682 const IdentifierInfo *getBaseTypeIdentifier() const;
683
684 /// Divides a QualType into its unqualified type and a set of local
685 /// qualifiers.
686 SplitQualType split() const;
687
688 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
689
690 static QualType getFromOpaquePtr(const void *Ptr) {
691 QualType T;
692 T.Value.setFromOpaqueValue(const_cast<void*>(Ptr));
693 return T;
694 }
695
696 const Type &operator*() const {
697 return *getTypePtr();
698 }
699
700 const Type *operator->() const {
701 return getTypePtr();
702 }
703
704 bool isCanonical() const;
705 bool isCanonicalAsParam() const;
706
707 /// Return true if this QualType doesn't point to a type yet.
708 bool isNull() const {
709 return Value.getPointer().isNull();
710 }
711
712 /// Determine whether this particular QualType instance has the
713 /// "const" qualifier set, without looking through typedefs that may have
714 /// added "const" at a different level.
715 bool isLocalConstQualified() const {
716 return (getLocalFastQualifiers() & Qualifiers::Const);
717 }
718
719 /// Determine whether this type is const-qualified.
720 bool isConstQualified() const;
721
722 /// Determine whether this particular QualType instance has the
723 /// "restrict" qualifier set, without looking through typedefs that may have
724 /// added "restrict" at a different level.
725 bool isLocalRestrictQualified() const {
726 return (getLocalFastQualifiers() & Qualifiers::Restrict);
727 }
728
729 /// Determine whether this type is restrict-qualified.
730 bool isRestrictQualified() const;
731
732 /// Determine whether this particular QualType instance has the
733 /// "volatile" qualifier set, without looking through typedefs that may have
734 /// added "volatile" at a different level.
735 bool isLocalVolatileQualified() const {
736 return (getLocalFastQualifiers() & Qualifiers::Volatile);
737 }
738
739 /// Determine whether this type is volatile-qualified.
740 bool isVolatileQualified() const;
741
742 /// Determine whether this particular QualType instance has any
743 /// qualifiers, without looking through any typedefs that might add
744 /// qualifiers at a different level.
745 bool hasLocalQualifiers() const {
746 return getLocalFastQualifiers() || hasLocalNonFastQualifiers();
747 }
748
749 /// Determine whether this type has any qualifiers.
750 bool hasQualifiers() const;
751
752 /// Determine whether this particular QualType instance has any
753 /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType
754 /// instance.
755 bool hasLocalNonFastQualifiers() const {
756 return Value.getPointer().is<const ExtQuals*>();
757 }
758
759 /// Retrieve the set of qualifiers local to this particular QualType
760 /// instance, not including any qualifiers acquired through typedefs or
761 /// other sugar.
762 Qualifiers getLocalQualifiers() const;
763
764 /// Retrieve the set of qualifiers applied to this type.
765 Qualifiers getQualifiers() const;
766
767 /// Retrieve the set of CVR (const-volatile-restrict) qualifiers
768 /// local to this particular QualType instance, not including any qualifiers
769 /// acquired through typedefs or other sugar.
770 unsigned getLocalCVRQualifiers() const {
771 return getLocalFastQualifiers();
772 }
773
774 /// Retrieve the set of CVR (const-volatile-restrict) qualifiers
775 /// applied to this type.
776 unsigned getCVRQualifiers() const;
777
778 bool isConstant(const ASTContext& Ctx) const {
779 return QualType::isConstant(*this, Ctx);
780 }
781
782 /// Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
783 bool isPODType(const ASTContext &Context) const;
784
785 /// Return true if this is a POD type according to the rules of the C++98
786 /// standard, regardless of the current compilation's language.
787 bool isCXX98PODType(const ASTContext &Context) const;
788
789 /// Return true if this is a POD type according to the more relaxed rules
790 /// of the C++11 standard, regardless of the current compilation's language.
791 /// (C++0x [basic.types]p9). Note that, unlike
792 /// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
793 bool isCXX11PODType(const ASTContext &Context) const;
794
795 /// Return true if this is a trivial type per (C++0x [basic.types]p9)
796 bool isTrivialType(const ASTContext &Context) const;
797
798 /// Return true if this is a trivially copyable type (C++0x [basic.types]p9)
799 bool isTriviallyCopyableType(const ASTContext &Context) const;
800
801
802 /// Returns true if it is a class and it might be dynamic.
803 bool mayBeDynamicClass() const;
804
805 /// Returns true if it is not a class or if the class might not be dynamic.
806 bool mayBeNotDynamicClass() const;
807
808 // Don't promise in the API that anything besides 'const' can be
809 // easily added.
810
811 /// Add the `const` type qualifier to this QualType.
812 void addConst() {
813 addFastQualifiers(Qualifiers::Const);
814 }
815 QualType withConst() const {
816 return withFastQualifiers(Qualifiers::Const);
817 }
818
819 /// Add the `volatile` type qualifier to this QualType.
820 void addVolatile() {
821 addFastQualifiers(Qualifiers::Volatile);
822 }
823 QualType withVolatile() const {
824 return withFastQualifiers(Qualifiers::Volatile);
825 }
826
827 /// Add the `restrict` qualifier to this QualType.
828 void addRestrict() {
829 addFastQualifiers(Qualifiers::Restrict);
830 }
831 QualType withRestrict() const {
832 return withFastQualifiers(Qualifiers::Restrict);
833 }
834
835 QualType withCVRQualifiers(unsigned CVR) const {
836 return withFastQualifiers(CVR);
837 }
838
839 void addFastQualifiers(unsigned TQs) {
840 assert(!(TQs & ~Qualifiers::FastMask)((!(TQs & ~Qualifiers::FastMask) && "non-fast qualifier bits set in mask!"
) ? static_cast<void> (0) : __assert_fail ("!(TQs & ~Qualifiers::FastMask) && \"non-fast qualifier bits set in mask!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 841, __PRETTY_FUNCTION__))
841 && "non-fast qualifier bits set in mask!")((!(TQs & ~Qualifiers::FastMask) && "non-fast qualifier bits set in mask!"
) ? static_cast<void> (0) : __assert_fail ("!(TQs & ~Qualifiers::FastMask) && \"non-fast qualifier bits set in mask!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 841, __PRETTY_FUNCTION__))
;
842 Value.setInt(Value.getInt() | TQs);
843 }
844
845 void removeLocalConst();
846 void removeLocalVolatile();
847 void removeLocalRestrict();
848 void removeLocalCVRQualifiers(unsigned Mask);
849
850 void removeLocalFastQualifiers() { Value.setInt(0); }
851 void removeLocalFastQualifiers(unsigned Mask) {
852 assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers")((!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"
) ? static_cast<void> (0) : __assert_fail ("!(Mask & ~Qualifiers::FastMask) && \"mask has non-fast qualifiers\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 852, __PRETTY_FUNCTION__))
;
853 Value.setInt(Value.getInt() & ~Mask);
854 }
855
856 // Creates a type with the given qualifiers in addition to any
857 // qualifiers already on this type.
858 QualType withFastQualifiers(unsigned TQs) const {
859 QualType T = *this;
860 T.addFastQualifiers(TQs);
861 return T;
862 }
863
864 // Creates a type with exactly the given fast qualifiers, removing
865 // any existing fast qualifiers.
866 QualType withExactLocalFastQualifiers(unsigned TQs) const {
867 return withoutLocalFastQualifiers().withFastQualifiers(TQs);
868 }
869
870 // Removes fast qualifiers, but leaves any extended qualifiers in place.
871 QualType withoutLocalFastQualifiers() const {
872 QualType T = *this;
873 T.removeLocalFastQualifiers();
874 return T;
875 }
876
877 QualType getCanonicalType() const;
878
879 /// Return this type with all of the instance-specific qualifiers
880 /// removed, but without removing any qualifiers that may have been applied
881 /// through typedefs.
882 QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); }
883
884 /// Retrieve the unqualified variant of the given type,
885 /// removing as little sugar as possible.
886 ///
887 /// This routine looks through various kinds of sugar to find the
888 /// least-desugared type that is unqualified. For example, given:
889 ///
890 /// \code
891 /// typedef int Integer;
892 /// typedef const Integer CInteger;
893 /// typedef CInteger DifferenceType;
894 /// \endcode
895 ///
896 /// Executing \c getUnqualifiedType() on the type \c DifferenceType will
897 /// desugar until we hit the type \c Integer, which has no qualifiers on it.
898 ///
899 /// The resulting type might still be qualified if it's sugar for an array
900 /// type. To strip qualifiers even from within a sugared array type, use
901 /// ASTContext::getUnqualifiedArrayType.
902 inline QualType getUnqualifiedType() const;
903
904 /// Retrieve the unqualified variant of the given type, removing as little
905 /// sugar as possible.
906 ///
907 /// Like getUnqualifiedType(), but also returns the set of
908 /// qualifiers that were built up.
909 ///
910 /// The resulting type might still be qualified if it's sugar for an array
911 /// type. To strip qualifiers even from within a sugared array type, use
912 /// ASTContext::getUnqualifiedArrayType.
913 inline SplitQualType getSplitUnqualifiedType() const;
914
915 /// Determine whether this type is more qualified than the other
916 /// given type, requiring exact equality for non-CVR qualifiers.
917 bool isMoreQualifiedThan(QualType Other) const;
918
919 /// Determine whether this type is at least as qualified as the other
920 /// given type, requiring exact equality for non-CVR qualifiers.
921 bool isAtLeastAsQualifiedAs(QualType Other) const;
922
923 QualType getNonReferenceType() const;
924
925 /// Determine the type of a (typically non-lvalue) expression with the
926 /// specified result type.
927 ///
928 /// This routine should be used for expressions for which the return type is
929 /// explicitly specified (e.g., in a cast or call) and isn't necessarily
930 /// an lvalue. It removes a top-level reference (since there are no
931 /// expressions of reference type) and deletes top-level cvr-qualifiers
932 /// from non-class types (in C++) or all types (in C).
933 QualType getNonLValueExprType(const ASTContext &Context) const;
934
935 /// Return the specified type with any "sugar" removed from
936 /// the type. This takes off typedefs, typeof's etc. If the outer level of
937 /// the type is already concrete, it returns it unmodified. This is similar
938 /// to getting the canonical type, but it doesn't remove *all* typedefs. For
939 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
940 /// concrete.
941 ///
942 /// Qualifiers are left in place.
943 QualType getDesugaredType(const ASTContext &Context) const {
944 return getDesugaredType(*this, Context);
945 }
946
947 SplitQualType getSplitDesugaredType() const {
948 return getSplitDesugaredType(*this);
949 }
950
951 /// Return the specified type with one level of "sugar" removed from
952 /// the type.
953 ///
954 /// This routine takes off the first typedef, typeof, etc. If the outer level
955 /// of the type is already concrete, it returns it unmodified.
956 QualType getSingleStepDesugaredType(const ASTContext &Context) const {
957 return getSingleStepDesugaredTypeImpl(*this, Context);
958 }
959
960 /// Returns the specified type after dropping any
961 /// outer-level parentheses.
962 QualType IgnoreParens() const {
963 if (isa<ParenType>(*this))
964 return QualType::IgnoreParens(*this);
965 return *this;
966 }
967
968 /// Indicate whether the specified types and qualifiers are identical.
969 friend bool operator==(const QualType &LHS, const QualType &RHS) {
970 return LHS.Value == RHS.Value;
8
Calling 'PointerIntPair::operator=='
11
Returning from 'PointerIntPair::operator=='
12
Returning zero, which participates in a condition later
971 }
972 friend bool operator!=(const QualType &LHS, const QualType &RHS) {
973 return LHS.Value != RHS.Value;
974 }
975 friend bool operator<(const QualType &LHS, const QualType &RHS) {
976 return LHS.Value < RHS.Value;
977 }
978
979 static std::string getAsString(SplitQualType split,
980 const PrintingPolicy &Policy) {
981 return getAsString(split.Ty, split.Quals, Policy);
982 }
983 static std::string getAsString(const Type *ty, Qualifiers qs,
984 const PrintingPolicy &Policy);
985
986 std::string getAsString() const;
987 std::string getAsString(const PrintingPolicy &Policy) const;
988
989 void print(raw_ostream &OS, const PrintingPolicy &Policy,
990 const Twine &PlaceHolder = Twine(),
991 unsigned Indentation = 0) const;
992
993 static void print(SplitQualType split, raw_ostream &OS,
994 const PrintingPolicy &policy, const Twine &PlaceHolder,
995 unsigned Indentation = 0) {
996 return print(split.Ty, split.Quals, OS, policy, PlaceHolder, Indentation);
997 }
998
999 static void print(const Type *ty, Qualifiers qs,
1000 raw_ostream &OS, const PrintingPolicy &policy,
1001 const Twine &PlaceHolder,
1002 unsigned Indentation = 0);
1003
1004 void getAsStringInternal(std::string &Str,
1005 const PrintingPolicy &Policy) const;
1006
1007 static void getAsStringInternal(SplitQualType split, std::string &out,
1008 const PrintingPolicy &policy) {
1009 return getAsStringInternal(split.Ty, split.Quals, out, policy);
1010 }
1011
1012 static void getAsStringInternal(const Type *ty, Qualifiers qs,
1013 std::string &out,
1014 const PrintingPolicy &policy);
1015
1016 class StreamedQualTypeHelper {
1017 const QualType &T;
1018 const PrintingPolicy &Policy;
1019 const Twine &PlaceHolder;
1020 unsigned Indentation;
1021
1022 public:
1023 StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy,
1024 const Twine &PlaceHolder, unsigned Indentation)
1025 : T(T), Policy(Policy), PlaceHolder(PlaceHolder),
1026 Indentation(Indentation) {}
1027
1028 friend raw_ostream &operator<<(raw_ostream &OS,
1029 const StreamedQualTypeHelper &SQT) {
1030 SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder, SQT.Indentation);
1031 return OS;
1032 }
1033 };
1034
1035 StreamedQualTypeHelper stream(const PrintingPolicy &Policy,
1036 const Twine &PlaceHolder = Twine(),
1037 unsigned Indentation = 0) const {
1038 return StreamedQualTypeHelper(*this, Policy, PlaceHolder, Indentation);
1039 }
1040
1041 void dump(const char *s) const;
1042 void dump() const;
1043 void dump(llvm::raw_ostream &OS) const;
1044
1045 void Profile(llvm::FoldingSetNodeID &ID) const {
1046 ID.AddPointer(getAsOpaquePtr());
1047 }
1048
1049 /// Return the address space of this type.
1050 inline LangAS getAddressSpace() const;
1051
1052 /// Returns gc attribute of this type.
1053 inline Qualifiers::GC getObjCGCAttr() const;
1054
1055 /// true when Type is objc's weak.
1056 bool isObjCGCWeak() const {
1057 return getObjCGCAttr() == Qualifiers::Weak;
1058 }
1059
1060 /// true when Type is objc's strong.
1061 bool isObjCGCStrong() const {
1062 return getObjCGCAttr() == Qualifiers::Strong;
1063 }
1064
1065 /// Returns lifetime attribute of this type.
1066 Qualifiers::ObjCLifetime getObjCLifetime() const {
1067 return getQualifiers().getObjCLifetime();
1068 }
1069
1070 bool hasNonTrivialObjCLifetime() const {
1071 return getQualifiers().hasNonTrivialObjCLifetime();
1072 }
1073
1074 bool hasStrongOrWeakObjCLifetime() const {
1075 return getQualifiers().hasStrongOrWeakObjCLifetime();
1076 }
1077
1078 // true when Type is objc's weak and weak is enabled but ARC isn't.
1079 bool isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const;
1080
1081 enum PrimitiveDefaultInitializeKind {
1082 /// The type does not fall into any of the following categories. Note that
1083 /// this case is zero-valued so that values of this enum can be used as a
1084 /// boolean condition for non-triviality.
1085 PDIK_Trivial,
1086
1087 /// The type is an Objective-C retainable pointer type that is qualified
1088 /// with the ARC __strong qualifier.
1089 PDIK_ARCStrong,
1090
1091 /// The type is an Objective-C retainable pointer type that is qualified
1092 /// with the ARC __weak qualifier.
1093 PDIK_ARCWeak,
1094
1095 /// The type is a struct containing a field whose type is not PCK_Trivial.
1096 PDIK_Struct
1097 };
1098
1099 /// Functions to query basic properties of non-trivial C struct types.
1100
1101 /// Check if this is a non-trivial type that would cause a C struct
1102 /// transitively containing this type to be non-trivial to default initialize
1103 /// and return the kind.
1104 PrimitiveDefaultInitializeKind
1105 isNonTrivialToPrimitiveDefaultInitialize() const;
1106
1107 enum PrimitiveCopyKind {
1108 /// The type does not fall into any of the following categories. Note that
1109 /// this case is zero-valued so that values of this enum can be used as a
1110 /// boolean condition for non-triviality.
1111 PCK_Trivial,
1112
1113 /// The type would be trivial except that it is volatile-qualified. Types
1114 /// that fall into one of the other non-trivial cases may additionally be
1115 /// volatile-qualified.
1116 PCK_VolatileTrivial,
1117
1118 /// The type is an Objective-C retainable pointer type that is qualified
1119 /// with the ARC __strong qualifier.
1120 PCK_ARCStrong,
1121
1122 /// The type is an Objective-C retainable pointer type that is qualified
1123 /// with the ARC __weak qualifier.
1124 PCK_ARCWeak,
1125
1126 /// The type is a struct containing a field whose type is neither
1127 /// PCK_Trivial nor PCK_VolatileTrivial.
1128 /// Note that a C++ struct type does not necessarily match this; C++ copying
1129 /// semantics are too complex to express here, in part because they depend
1130 /// on the exact constructor or assignment operator that is chosen by
1131 /// overload resolution to do the copy.
1132 PCK_Struct
1133 };
1134
1135 /// Check if this is a non-trivial type that would cause a C struct
1136 /// transitively containing this type to be non-trivial to copy and return the
1137 /// kind.
1138 PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const;
1139
1140 /// Check if this is a non-trivial type that would cause a C struct
1141 /// transitively containing this type to be non-trivial to destructively
1142 /// move and return the kind. Destructive move in this context is a C++-style
1143 /// move in which the source object is placed in a valid but unspecified state
1144 /// after it is moved, as opposed to a truly destructive move in which the
1145 /// source object is placed in an uninitialized state.
1146 PrimitiveCopyKind isNonTrivialToPrimitiveDestructiveMove() const;
1147
1148 enum DestructionKind {
1149 DK_none,
1150 DK_cxx_destructor,
1151 DK_objc_strong_lifetime,
1152 DK_objc_weak_lifetime,
1153 DK_nontrivial_c_struct
1154 };
1155
1156 /// Returns a nonzero value if objects of this type require
1157 /// non-trivial work to clean up after. Non-zero because it's
1158 /// conceivable that qualifiers (objc_gc(weak)?) could make
1159 /// something require destruction.
1160 DestructionKind isDestructedType() const {
1161 return isDestructedTypeImpl(*this);
1162 }
1163
1164 /// Check if this is or contains a C union that is non-trivial to
1165 /// default-initialize, which is a union that has a member that is non-trivial
1166 /// to default-initialize. If this returns true,
1167 /// isNonTrivialToPrimitiveDefaultInitialize returns PDIK_Struct.
1168 bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const;
1169
1170 /// Check if this is or contains a C union that is non-trivial to destruct,
1171 /// which is a union that has a member that is non-trivial to destruct. If
1172 /// this returns true, isDestructedType returns DK_nontrivial_c_struct.
1173 bool hasNonTrivialToPrimitiveDestructCUnion() const;
1174
1175 /// Check if this is or contains a C union that is non-trivial to copy, which
1176 /// is a union that has a member that is non-trivial to copy. If this returns
1177 /// true, isNonTrivialToPrimitiveCopy returns PCK_Struct.
1178 bool hasNonTrivialToPrimitiveCopyCUnion() const;
1179
1180 /// Determine whether expressions of the given type are forbidden
1181 /// from being lvalues in C.
1182 ///
1183 /// The expression types that are forbidden to be lvalues are:
1184 /// - 'void', but not qualified void
1185 /// - function types
1186 ///
1187 /// The exact rule here is C99 6.3.2.1:
1188 /// An lvalue is an expression with an object type or an incomplete
1189 /// type other than void.
1190 bool isCForbiddenLValueType() const;
1191
1192 /// Substitute type arguments for the Objective-C type parameters used in the
1193 /// subject type.
1194 ///
1195 /// \param ctx ASTContext in which the type exists.
1196 ///
1197 /// \param typeArgs The type arguments that will be substituted for the
1198 /// Objective-C type parameters in the subject type, which are generally
1199 /// computed via \c Type::getObjCSubstitutions. If empty, the type
1200 /// parameters will be replaced with their bounds or id/Class, as appropriate
1201 /// for the context.
1202 ///
1203 /// \param context The context in which the subject type was written.
1204 ///
1205 /// \returns the resulting type.
1206 QualType substObjCTypeArgs(ASTContext &ctx,
1207 ArrayRef<QualType> typeArgs,
1208 ObjCSubstitutionContext context) const;
1209
1210 /// Substitute type arguments from an object type for the Objective-C type
1211 /// parameters used in the subject type.
1212 ///
1213 /// This operation combines the computation of type arguments for
1214 /// substitution (\c Type::getObjCSubstitutions) with the actual process of
1215 /// substitution (\c QualType::substObjCTypeArgs) for the convenience of
1216 /// callers that need to perform a single substitution in isolation.
1217 ///
1218 /// \param objectType The type of the object whose member type we're
1219 /// substituting into. For example, this might be the receiver of a message
1220 /// or the base of a property access.
1221 ///
1222 /// \param dc The declaration context from which the subject type was
1223 /// retrieved, which indicates (for example) which type parameters should
1224 /// be substituted.
1225 ///
1226 /// \param context The context in which the subject type was written.
1227 ///
1228 /// \returns the subject type after replacing all of the Objective-C type
1229 /// parameters with their corresponding arguments.
1230 QualType substObjCMemberType(QualType objectType,
1231 const DeclContext *dc,
1232 ObjCSubstitutionContext context) const;
1233
1234 /// Strip Objective-C "__kindof" types from the given type.
1235 QualType stripObjCKindOfType(const ASTContext &ctx) const;
1236
1237 /// Remove all qualifiers including _Atomic.
1238 QualType getAtomicUnqualifiedType() const;
1239
1240private:
1241 // These methods are implemented in a separate translation unit;
1242 // "static"-ize them to avoid creating temporary QualTypes in the
1243 // caller.
1244 static bool isConstant(QualType T, const ASTContext& Ctx);
1245 static QualType getDesugaredType(QualType T, const ASTContext &Context);
1246 static SplitQualType getSplitDesugaredType(QualType T);
1247 static SplitQualType getSplitUnqualifiedTypeImpl(QualType type);
1248 static QualType getSingleStepDesugaredTypeImpl(QualType type,
1249 const ASTContext &C);
1250 static QualType IgnoreParens(QualType T);
1251 static DestructionKind isDestructedTypeImpl(QualType type);
1252
1253 /// Check if \param RD is or contains a non-trivial C union.
1254 static bool hasNonTrivialToPrimitiveDefaultInitializeCUnion(const RecordDecl *RD);
1255 static bool hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD);
1256 static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD);
1257};
1258
1259} // namespace clang
1260
1261namespace llvm {
1262
1263/// Implement simplify_type for QualType, so that we can dyn_cast from QualType
1264/// to a specific Type class.
1265template<> struct simplify_type< ::clang::QualType> {
1266 using SimpleType = const ::clang::Type *;
1267
1268 static SimpleType getSimplifiedValue(::clang::QualType Val) {
1269 return Val.getTypePtr();
1270 }
1271};
1272
1273// Teach SmallPtrSet that QualType is "basically a pointer".
1274template<>
1275struct PointerLikeTypeTraits<clang::QualType> {
1276 static inline void *getAsVoidPointer(clang::QualType P) {
1277 return P.getAsOpaquePtr();
1278 }
1279
1280 static inline clang::QualType getFromVoidPointer(void *P) {
1281 return clang::QualType::getFromOpaquePtr(P);
1282 }
1283
1284 // Various qualifiers go in low bits.
1285 enum { NumLowBitsAvailable = 0 };
1286};
1287
1288} // namespace llvm
1289
1290namespace clang {
1291
1292/// Base class that is common to both the \c ExtQuals and \c Type
1293/// classes, which allows \c QualType to access the common fields between the
1294/// two.
1295class ExtQualsTypeCommonBase {
1296 friend class ExtQuals;
1297 friend class QualType;
1298 friend class Type;
1299
1300 /// The "base" type of an extended qualifiers type (\c ExtQuals) or
1301 /// a self-referential pointer (for \c Type).
1302 ///
1303 /// This pointer allows an efficient mapping from a QualType to its
1304 /// underlying type pointer.
1305 const Type *const BaseType;
1306
1307 /// The canonical type of this type. A QualType.
1308 QualType CanonicalType;
1309
1310 ExtQualsTypeCommonBase(const Type *baseType, QualType canon)
1311 : BaseType(baseType), CanonicalType(canon) {}
1312};
1313
1314/// We can encode up to four bits in the low bits of a
1315/// type pointer, but there are many more type qualifiers that we want
1316/// to be able to apply to an arbitrary type. Therefore we have this
1317/// struct, intended to be heap-allocated and used by QualType to
1318/// store qualifiers.
1319///
1320/// The current design tags the 'const', 'restrict', and 'volatile' qualifiers
1321/// in three low bits on the QualType pointer; a fourth bit records whether
1322/// the pointer is an ExtQuals node. The extended qualifiers (address spaces,
1323/// Objective-C GC attributes) are much more rare.
1324class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode {
1325 // NOTE: changing the fast qualifiers should be straightforward as
1326 // long as you don't make 'const' non-fast.
1327 // 1. Qualifiers:
1328 // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ).
1329 // Fast qualifiers must occupy the low-order bits.
1330 // b) Update Qualifiers::FastWidth and FastMask.
1331 // 2. QualType:
1332 // a) Update is{Volatile,Restrict}Qualified(), defined inline.
1333 // b) Update remove{Volatile,Restrict}, defined near the end of
1334 // this header.
1335 // 3. ASTContext:
1336 // a) Update get{Volatile,Restrict}Type.
1337
1338 /// The immutable set of qualifiers applied by this node. Always contains
1339 /// extended qualifiers.
1340 Qualifiers Quals;
1341
1342 ExtQuals *this_() { return this; }
1343
1344public:
1345 ExtQuals(const Type *baseType, QualType canon, Qualifiers quals)
1346 : ExtQualsTypeCommonBase(baseType,
1347 canon.isNull() ? QualType(this_(), 0) : canon),
1348 Quals(quals) {
1349 assert(Quals.hasNonFastQualifiers()((Quals.hasNonFastQualifiers() && "ExtQuals created with no fast qualifiers"
) ? static_cast<void> (0) : __assert_fail ("Quals.hasNonFastQualifiers() && \"ExtQuals created with no fast qualifiers\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 1350, __PRETTY_FUNCTION__))
1350 && "ExtQuals created with no fast qualifiers")((Quals.hasNonFastQualifiers() && "ExtQuals created with no fast qualifiers"
) ? static_cast<void> (0) : __assert_fail ("Quals.hasNonFastQualifiers() && \"ExtQuals created with no fast qualifiers\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 1350, __PRETTY_FUNCTION__))
;
1351 assert(!Quals.hasFastQualifiers()((!Quals.hasFastQualifiers() && "ExtQuals created with fast qualifiers"
) ? static_cast<void> (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"ExtQuals created with fast qualifiers\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 1352, __PRETTY_FUNCTION__))
1352 && "ExtQuals created with fast qualifiers")((!Quals.hasFastQualifiers() && "ExtQuals created with fast qualifiers"
) ? static_cast<void> (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"ExtQuals created with fast qualifiers\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 1352, __PRETTY_FUNCTION__))
;
1353 }
1354
1355 Qualifiers getQualifiers() const { return Quals; }
1356
1357 bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); }
1358 Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); }
1359
1360 bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); }
1361 Qualifiers::ObjCLifetime getObjCLifetime() const {
1362 return Quals.getObjCLifetime();
1363 }
1364
1365 bool hasAddressSpace() const { return Quals.hasAddressSpace(); }
1366 LangAS getAddressSpace() const { return Quals.getAddressSpace(); }
1367
1368 const Type *getBaseType() const { return BaseType; }
1369
1370public:
1371 void Profile(llvm::FoldingSetNodeID &ID) const {
1372 Profile(ID, getBaseType(), Quals);
1373 }
1374
1375 static void Profile(llvm::FoldingSetNodeID &ID,
1376 const Type *BaseType,
1377 Qualifiers Quals) {
1378 assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!")((!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"
) ? static_cast<void> (0) : __assert_fail ("!Quals.hasFastQualifiers() && \"fast qualifiers in ExtQuals hash!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 1378, __PRETTY_FUNCTION__))
;
1379 ID.AddPointer(BaseType);
1380 Quals.Profile(ID);
1381 }
1382};
1383
1384/// The kind of C++11 ref-qualifier associated with a function type.
1385/// This determines whether a member function's "this" object can be an
1386/// lvalue, rvalue, or neither.
1387enum RefQualifierKind {
1388 /// No ref-qualifier was provided.
1389 RQ_None = 0,
1390
1391 /// An lvalue ref-qualifier was provided (\c &).
1392 RQ_LValue,
1393
1394 /// An rvalue ref-qualifier was provided (\c &&).
1395 RQ_RValue
1396};
1397
1398/// Which keyword(s) were used to create an AutoType.
1399enum class AutoTypeKeyword {
1400 /// auto
1401 Auto,
1402
1403 /// decltype(auto)
1404 DecltypeAuto,
1405
1406 /// __auto_type (GNU extension)
1407 GNUAutoType
1408};
1409
1410/// The base class of the type hierarchy.
1411///
1412/// A central concept with types is that each type always has a canonical
1413/// type. A canonical type is the type with any typedef names stripped out
1414/// of it or the types it references. For example, consider:
1415///
1416/// typedef int foo;
1417/// typedef foo* bar;
1418/// 'int *' 'foo *' 'bar'
1419///
1420/// There will be a Type object created for 'int'. Since int is canonical, its
1421/// CanonicalType pointer points to itself. There is also a Type for 'foo' (a
1422/// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next
1423/// there is a PointerType that represents 'int*', which, like 'int', is
1424/// canonical. Finally, there is a PointerType type for 'foo*' whose canonical
1425/// type is 'int*', and there is a TypedefType for 'bar', whose canonical type
1426/// is also 'int*'.
1427///
1428/// Non-canonical types are useful for emitting diagnostics, without losing
1429/// information about typedefs being used. Canonical types are useful for type
1430/// comparisons (they allow by-pointer equality tests) and useful for reasoning
1431/// about whether something has a particular form (e.g. is a function type),
1432/// because they implicitly, recursively, strip all typedefs out of a type.
1433///
1434/// Types, once created, are immutable.
1435///
1436class alignas(8) Type : public ExtQualsTypeCommonBase {
1437public:
1438 enum TypeClass {
1439#define TYPE(Class, Base) Class,
1440#define LAST_TYPE(Class) TypeLast = Class
1441#define ABSTRACT_TYPE(Class, Base)
1442#include "clang/AST/TypeNodes.inc"
1443 };
1444
1445private:
1446 /// Bitfields required by the Type class.
1447 class TypeBitfields {
1448 friend class Type;
1449 template <class T> friend class TypePropertyCache;
1450
1451 /// TypeClass bitfield - Enum that specifies what subclass this belongs to.
1452 unsigned TC : 8;
1453
1454 /// Whether this type is a dependent type (C++ [temp.dep.type]).
1455 unsigned Dependent : 1;
1456
1457 /// Whether this type somehow involves a template parameter, even
1458 /// if the resolution of the type does not depend on a template parameter.
1459 unsigned InstantiationDependent : 1;
1460
1461 /// Whether this type is a variably-modified type (C99 6.7.5).
1462 unsigned VariablyModified : 1;
1463
1464 /// Whether this type contains an unexpanded parameter pack
1465 /// (for C++11 variadic templates).
1466 unsigned ContainsUnexpandedParameterPack : 1;
1467
1468 /// True if the cache (i.e. the bitfields here starting with
1469 /// 'Cache') is valid.
1470 mutable unsigned CacheValid : 1;
1471
1472 /// Linkage of this type.
1473 mutable unsigned CachedLinkage : 3;
1474
1475 /// Whether this type involves and local or unnamed types.
1476 mutable unsigned CachedLocalOrUnnamed : 1;
1477
1478 /// Whether this type comes from an AST file.
1479 mutable unsigned FromAST : 1;
1480
1481 bool isCacheValid() const {
1482 return CacheValid;
1483 }
1484
1485 Linkage getLinkage() const {
1486 assert(isCacheValid() && "getting linkage from invalid cache")((isCacheValid() && "getting linkage from invalid cache"
) ? static_cast<void> (0) : __assert_fail ("isCacheValid() && \"getting linkage from invalid cache\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 1486, __PRETTY_FUNCTION__))
;
1487 return static_cast<Linkage>(CachedLinkage);
1488 }
1489
1490 bool hasLocalOrUnnamedType() const {
1491 assert(isCacheValid() && "getting linkage from invalid cache")((isCacheValid() && "getting linkage from invalid cache"
) ? static_cast<void> (0) : __assert_fail ("isCacheValid() && \"getting linkage from invalid cache\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 1491, __PRETTY_FUNCTION__))
;
1492 return CachedLocalOrUnnamed;
1493 }
1494 };
1495 enum { NumTypeBits = 18 };
1496
1497protected:
1498 // These classes allow subclasses to somewhat cleanly pack bitfields
1499 // into Type.
1500
1501 class ArrayTypeBitfields {
1502 friend class ArrayType;
1503
1504 unsigned : NumTypeBits;
1505
1506 /// CVR qualifiers from declarations like
1507 /// 'int X[static restrict 4]'. For function parameters only.
1508 unsigned IndexTypeQuals : 3;
1509
1510 /// Storage class qualifiers from declarations like
1511 /// 'int X[static restrict 4]'. For function parameters only.
1512 /// Actually an ArrayType::ArraySizeModifier.
1513 unsigned SizeModifier : 3;
1514 };
1515
1516 class BuiltinTypeBitfields {
1517 friend class BuiltinType;
1518
1519 unsigned : NumTypeBits;
1520
1521 /// The kind (BuiltinType::Kind) of builtin type this is.
1522 unsigned Kind : 8;
1523 };
1524
1525 /// FunctionTypeBitfields store various bits belonging to FunctionProtoType.
1526 /// Only common bits are stored here. Additional uncommon bits are stored
1527 /// in a trailing object after FunctionProtoType.
1528 class FunctionTypeBitfields {
1529 friend class FunctionProtoType;
1530 friend class FunctionType;
1531
1532 unsigned : NumTypeBits;
1533
1534 /// Extra information which affects how the function is called, like
1535 /// regparm and the calling convention.
1536 unsigned ExtInfo : 12;
1537
1538 /// The ref-qualifier associated with a \c FunctionProtoType.
1539 ///
1540 /// This is a value of type \c RefQualifierKind.
1541 unsigned RefQualifier : 2;
1542
1543 /// Used only by FunctionProtoType, put here to pack with the
1544 /// other bitfields.
1545 /// The qualifiers are part of FunctionProtoType because...
1546 ///
1547 /// C++ 8.3.5p4: The return type, the parameter type list and the
1548 /// cv-qualifier-seq, [...], are part of the function type.
1549 unsigned FastTypeQuals : Qualifiers::FastWidth;
1550 /// Whether this function has extended Qualifiers.
1551 unsigned HasExtQuals : 1;
1552
1553 /// The number of parameters this function has, not counting '...'.
1554 /// According to [implimits] 8 bits should be enough here but this is
1555 /// somewhat easy to exceed with metaprogramming and so we would like to
1556 /// keep NumParams as wide as reasonably possible.
1557 unsigned NumParams : 16;
1558
1559 /// The type of exception specification this function has.
1560 unsigned ExceptionSpecType : 4;
1561
1562 /// Whether this function has extended parameter information.
1563 unsigned HasExtParameterInfos : 1;
1564
1565 /// Whether the function is variadic.
1566 unsigned Variadic : 1;
1567
1568 /// Whether this function has a trailing return type.
1569 unsigned HasTrailingReturn : 1;
1570 };
1571
1572 class ObjCObjectTypeBitfields {
1573 friend class ObjCObjectType;
1574
1575 unsigned : NumTypeBits;
1576
1577 /// The number of type arguments stored directly on this object type.
1578 unsigned NumTypeArgs : 7;
1579
1580 /// The number of protocols stored directly on this object type.
1581 unsigned NumProtocols : 6;
1582
1583 /// Whether this is a "kindof" type.
1584 unsigned IsKindOf : 1;
1585 };
1586
1587 class ReferenceTypeBitfields {
1588 friend class ReferenceType;
1589
1590 unsigned : NumTypeBits;
1591
1592 /// True if the type was originally spelled with an lvalue sigil.
1593 /// This is never true of rvalue references but can also be false
1594 /// on lvalue references because of C++0x [dcl.typedef]p9,
1595 /// as follows:
1596 ///
1597 /// typedef int &ref; // lvalue, spelled lvalue
1598 /// typedef int &&rvref; // rvalue
1599 /// ref &a; // lvalue, inner ref, spelled lvalue
1600 /// ref &&a; // lvalue, inner ref
1601 /// rvref &a; // lvalue, inner ref, spelled lvalue
1602 /// rvref &&a; // rvalue, inner ref
1603 unsigned SpelledAsLValue : 1;
1604
1605 /// True if the inner type is a reference type. This only happens
1606 /// in non-canonical forms.
1607 unsigned InnerRef : 1;
1608 };
1609
1610 class TypeWithKeywordBitfields {
1611 friend class TypeWithKeyword;
1612
1613 unsigned : NumTypeBits;
1614
1615 /// An ElaboratedTypeKeyword. 8 bits for efficient access.
1616 unsigned Keyword : 8;
1617 };
1618
1619 enum { NumTypeWithKeywordBits = 8 };
1620
1621 class ElaboratedTypeBitfields {
1622 friend class ElaboratedType;
1623
1624 unsigned : NumTypeBits;
1625 unsigned : NumTypeWithKeywordBits;
1626
1627 /// Whether the ElaboratedType has a trailing OwnedTagDecl.
1628 unsigned HasOwnedTagDecl : 1;
1629 };
1630
1631 class VectorTypeBitfields {
1632 friend class VectorType;
1633 friend class DependentVectorType;
1634
1635 unsigned : NumTypeBits;
1636
1637 /// The kind of vector, either a generic vector type or some
1638 /// target-specific vector type such as for AltiVec or Neon.
1639 unsigned VecKind : 3;
1640
1641 /// The number of elements in the vector.
1642 unsigned NumElements : 29 - NumTypeBits;
1643
1644 enum { MaxNumElements = (1 << (29 - NumTypeBits)) - 1 };
1645 };
1646
1647 class AttributedTypeBitfields {
1648 friend class AttributedType;
1649
1650 unsigned : NumTypeBits;
1651
1652 /// An AttributedType::Kind
1653 unsigned AttrKind : 32 - NumTypeBits;
1654 };
1655
1656 class AutoTypeBitfields {
1657 friend class AutoType;
1658
1659 unsigned : NumTypeBits;
1660
1661 /// Was this placeholder type spelled as 'auto', 'decltype(auto)',
1662 /// or '__auto_type'? AutoTypeKeyword value.
1663 unsigned Keyword : 2;
1664 };
1665
1666 class SubstTemplateTypeParmPackTypeBitfields {
1667 friend class SubstTemplateTypeParmPackType;
1668
1669 unsigned : NumTypeBits;
1670
1671 /// The number of template arguments in \c Arguments, which is
1672 /// expected to be able to hold at least 1024 according to [implimits].
1673 /// However as this limit is somewhat easy to hit with template
1674 /// metaprogramming we'd prefer to keep it as large as possible.
1675 /// At the moment it has been left as a non-bitfield since this type
1676 /// safely fits in 64 bits as an unsigned, so there is no reason to
1677 /// introduce the performance impact of a bitfield.
1678 unsigned NumArgs;
1679 };
1680
1681 class TemplateSpecializationTypeBitfields {
1682 friend class TemplateSpecializationType;
1683
1684 unsigned : NumTypeBits;
1685
1686 /// Whether this template specialization type is a substituted type alias.
1687 unsigned TypeAlias : 1;
1688
1689 /// The number of template arguments named in this class template
1690 /// specialization, which is expected to be able to hold at least 1024
1691 /// according to [implimits]. However, as this limit is somewhat easy to
1692 /// hit with template metaprogramming we'd prefer to keep it as large
1693 /// as possible. At the moment it has been left as a non-bitfield since
1694 /// this type safely fits in 64 bits as an unsigned, so there is no reason
1695 /// to introduce the performance impact of a bitfield.
1696 unsigned NumArgs;
1697 };
1698
1699 class DependentTemplateSpecializationTypeBitfields {
1700 friend class DependentTemplateSpecializationType;
1701
1702 unsigned : NumTypeBits;
1703 unsigned : NumTypeWithKeywordBits;
1704
1705 /// The number of template arguments named in this class template
1706 /// specialization, which is expected to be able to hold at least 1024
1707 /// according to [implimits]. However, as this limit is somewhat easy to
1708 /// hit with template metaprogramming we'd prefer to keep it as large
1709 /// as possible. At the moment it has been left as a non-bitfield since
1710 /// this type safely fits in 64 bits as an unsigned, so there is no reason
1711 /// to introduce the performance impact of a bitfield.
1712 unsigned NumArgs;
1713 };
1714
1715 class PackExpansionTypeBitfields {
1716 friend class PackExpansionType;
1717
1718 unsigned : NumTypeBits;
1719
1720 /// The number of expansions that this pack expansion will
1721 /// generate when substituted (+1), which is expected to be able to
1722 /// hold at least 1024 according to [implimits]. However, as this limit
1723 /// is somewhat easy to hit with template metaprogramming we'd prefer to
1724 /// keep it as large as possible. At the moment it has been left as a
1725 /// non-bitfield since this type safely fits in 64 bits as an unsigned, so
1726 /// there is no reason to introduce the performance impact of a bitfield.
1727 ///
1728 /// This field will only have a non-zero value when some of the parameter
1729 /// packs that occur within the pattern have been substituted but others
1730 /// have not.
1731 unsigned NumExpansions;
1732 };
1733
1734 union {
1735 TypeBitfields TypeBits;
1736 ArrayTypeBitfields ArrayTypeBits;
1737 AttributedTypeBitfields AttributedTypeBits;
1738 AutoTypeBitfields AutoTypeBits;
1739 BuiltinTypeBitfields BuiltinTypeBits;
1740 FunctionTypeBitfields FunctionTypeBits;
1741 ObjCObjectTypeBitfields ObjCObjectTypeBits;
1742 ReferenceTypeBitfields ReferenceTypeBits;
1743 TypeWithKeywordBitfields TypeWithKeywordBits;
1744 ElaboratedTypeBitfields ElaboratedTypeBits;
1745 VectorTypeBitfields VectorTypeBits;
1746 SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits;
1747 TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits;
1748 DependentTemplateSpecializationTypeBitfields
1749 DependentTemplateSpecializationTypeBits;
1750 PackExpansionTypeBitfields PackExpansionTypeBits;
1751
1752 static_assert(sizeof(TypeBitfields) <= 8,
1753 "TypeBitfields is larger than 8 bytes!");
1754 static_assert(sizeof(ArrayTypeBitfields) <= 8,
1755 "ArrayTypeBitfields is larger than 8 bytes!");
1756 static_assert(sizeof(AttributedTypeBitfields) <= 8,
1757 "AttributedTypeBitfields is larger than 8 bytes!");
1758 static_assert(sizeof(AutoTypeBitfields) <= 8,
1759 "AutoTypeBitfields is larger than 8 bytes!");
1760 static_assert(sizeof(BuiltinTypeBitfields) <= 8,
1761 "BuiltinTypeBitfields is larger than 8 bytes!");
1762 static_assert(sizeof(FunctionTypeBitfields) <= 8,
1763 "FunctionTypeBitfields is larger than 8 bytes!");
1764 static_assert(sizeof(ObjCObjectTypeBitfields) <= 8,
1765 "ObjCObjectTypeBitfields is larger than 8 bytes!");
1766 static_assert(sizeof(ReferenceTypeBitfields) <= 8,
1767 "ReferenceTypeBitfields is larger than 8 bytes!");
1768 static_assert(sizeof(TypeWithKeywordBitfields) <= 8,
1769 "TypeWithKeywordBitfields is larger than 8 bytes!");
1770 static_assert(sizeof(ElaboratedTypeBitfields) <= 8,
1771 "ElaboratedTypeBitfields is larger than 8 bytes!");
1772 static_assert(sizeof(VectorTypeBitfields) <= 8,
1773 "VectorTypeBitfields is larger than 8 bytes!");
1774 static_assert(sizeof(SubstTemplateTypeParmPackTypeBitfields) <= 8,
1775 "SubstTemplateTypeParmPackTypeBitfields is larger"
1776 " than 8 bytes!");
1777 static_assert(sizeof(TemplateSpecializationTypeBitfields) <= 8,
1778 "TemplateSpecializationTypeBitfields is larger"
1779 " than 8 bytes!");
1780 static_assert(sizeof(DependentTemplateSpecializationTypeBitfields) <= 8,
1781 "DependentTemplateSpecializationTypeBitfields is larger"
1782 " than 8 bytes!");
1783 static_assert(sizeof(PackExpansionTypeBitfields) <= 8,
1784 "PackExpansionTypeBitfields is larger than 8 bytes");
1785 };
1786
1787private:
1788 template <class T> friend class TypePropertyCache;
1789
1790 /// Set whether this type comes from an AST file.
1791 void setFromAST(bool V = true) const {
1792 TypeBits.FromAST = V;
1793 }
1794
1795protected:
1796 friend class ASTContext;
1797
1798 Type(TypeClass tc, QualType canon, bool Dependent,
1799 bool InstantiationDependent, bool VariablyModified,
1800 bool ContainsUnexpandedParameterPack)
1801 : ExtQualsTypeCommonBase(this,
1802 canon.isNull() ? QualType(this_(), 0) : canon) {
1803 TypeBits.TC = tc;
1804 TypeBits.Dependent = Dependent;
1805 TypeBits.InstantiationDependent = Dependent || InstantiationDependent;
1806 TypeBits.VariablyModified = VariablyModified;
1807 TypeBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
1808 TypeBits.CacheValid = false;
1809 TypeBits.CachedLocalOrUnnamed = false;
1810 TypeBits.CachedLinkage = NoLinkage;
1811 TypeBits.FromAST = false;
1812 }
1813
1814 // silence VC++ warning C4355: 'this' : used in base member initializer list
1815 Type *this_() { return this; }
1816
1817 void setDependent(bool D = true) {
1818 TypeBits.Dependent = D;
1819 if (D)
1820 TypeBits.InstantiationDependent = true;
1821 }
1822
1823 void setInstantiationDependent(bool D = true) {
1824 TypeBits.InstantiationDependent = D; }
1825
1826 void setVariablyModified(bool VM = true) { TypeBits.VariablyModified = VM; }
1827
1828 void setContainsUnexpandedParameterPack(bool PP = true) {
1829 TypeBits.ContainsUnexpandedParameterPack = PP;
1830 }
1831
1832public:
1833 friend class ASTReader;
1834 friend class ASTWriter;
1835
1836 Type(const Type &) = delete;
1837 Type(Type &&) = delete;
1838 Type &operator=(const Type &) = delete;
1839 Type &operator=(Type &&) = delete;
1840
1841 TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); }
1842
1843 /// Whether this type comes from an AST file.
1844 bool isFromAST() const { return TypeBits.FromAST; }
1845
1846 /// Whether this type is or contains an unexpanded parameter
1847 /// pack, used to support C++0x variadic templates.
1848 ///
1849 /// A type that contains a parameter pack shall be expanded by the
1850 /// ellipsis operator at some point. For example, the typedef in the
1851 /// following example contains an unexpanded parameter pack 'T':
1852 ///
1853 /// \code
1854 /// template<typename ...T>
1855 /// struct X {
1856 /// typedef T* pointer_types; // ill-formed; T is a parameter pack.
1857 /// };
1858 /// \endcode
1859 ///
1860 /// Note that this routine does not specify which
1861 bool containsUnexpandedParameterPack() const {
1862 return TypeBits.ContainsUnexpandedParameterPack;
1863 }
1864
1865 /// Determines if this type would be canonical if it had no further
1866 /// qualification.
1867 bool isCanonicalUnqualified() const {
1868 return CanonicalType == QualType(this, 0);
1869 }
1870
1871 /// Pull a single level of sugar off of this locally-unqualified type.
1872 /// Users should generally prefer SplitQualType::getSingleStepDesugaredType()
1873 /// or QualType::getSingleStepDesugaredType(const ASTContext&).
1874 QualType getLocallyUnqualifiedSingleStepDesugaredType() const;
1875
1876 /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
1877 /// object types, function types, and incomplete types.
1878
1879 /// Return true if this is an incomplete type.
1880 /// A type that can describe objects, but which lacks information needed to
1881 /// determine its size (e.g. void, or a fwd declared struct). Clients of this
1882 /// routine will need to determine if the size is actually required.
1883 ///
1884 /// Def If non-null, and the type refers to some kind of declaration
1885 /// that can be completed (such as a C struct, C++ class, or Objective-C
1886 /// class), will be set to the declaration.
1887 bool isIncompleteType(NamedDecl **Def = nullptr) const;
1888
1889 /// Return true if this is an incomplete or object
1890 /// type, in other words, not a function type.
1891 bool isIncompleteOrObjectType() const {
1892 return !isFunctionType();
1893 }
1894
1895 /// Determine whether this type is an object type.
1896 bool isObjectType() const {
1897 // C++ [basic.types]p8:
1898 // An object type is a (possibly cv-qualified) type that is not a
1899 // function type, not a reference type, and not a void type.
1900 return !isReferenceType() && !isFunctionType() && !isVoidType();
1901 }
1902
1903 /// Return true if this is a literal type
1904 /// (C++11 [basic.types]p10)
1905 bool isLiteralType(const ASTContext &Ctx) const;
1906
1907 /// Test if this type is a standard-layout type.
1908 /// (C++0x [basic.type]p9)
1909 bool isStandardLayoutType() const;
1910
1911 /// Helper methods to distinguish type categories. All type predicates
1912 /// operate on the canonical type, ignoring typedefs and qualifiers.
1913
1914 /// Returns true if the type is a builtin type.
1915 bool isBuiltinType() const;
1916
1917 /// Test for a particular builtin type.
1918 bool isSpecificBuiltinType(unsigned K) const;
1919
1920 /// Test for a type which does not represent an actual type-system type but
1921 /// is instead used as a placeholder for various convenient purposes within
1922 /// Clang. All such types are BuiltinTypes.
1923 bool isPlaceholderType() const;
1924 const BuiltinType *getAsPlaceholderType() const;
1925
1926 /// Test for a specific placeholder type.
1927 bool isSpecificPlaceholderType(unsigned K) const;
1928
1929 /// Test for a placeholder type other than Overload; see
1930 /// BuiltinType::isNonOverloadPlaceholderType.
1931 bool isNonOverloadPlaceholderType() const;
1932
1933 /// isIntegerType() does *not* include complex integers (a GCC extension).
1934 /// isComplexIntegerType() can be used to test for complex integers.
1935 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum)
1936 bool isEnumeralType() const;
1937
1938 /// Determine whether this type is a scoped enumeration type.
1939 bool isScopedEnumeralType() const;
1940 bool isBooleanType() const;
1941 bool isCharType() const;
1942 bool isWideCharType() const;
1943 bool isChar8Type() const;
1944 bool isChar16Type() const;
1945 bool isChar32Type() const;
1946 bool isAnyCharacterType() const;
1947 bool isIntegralType(const ASTContext &Ctx) const;
1948
1949 /// Determine whether this type is an integral or enumeration type.
1950 bool isIntegralOrEnumerationType() const;
1951
1952 /// Determine whether this type is an integral or unscoped enumeration type.
1953 bool isIntegralOrUnscopedEnumerationType() const;
1954
1955 /// Floating point categories.
1956 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double)
1957 /// isComplexType() does *not* include complex integers (a GCC extension).
1958 /// isComplexIntegerType() can be used to test for complex integers.
1959 bool isComplexType() const; // C99 6.2.5p11 (complex)
1960 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int.
1961 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex)
1962 bool isHalfType() const; // OpenCL 6.1.1.1, NEON (IEEE 754-2008 half)
1963 bool isFloat16Type() const; // C11 extension ISO/IEC TS 18661
1964 bool isFloat128Type() const;
1965 bool isRealType() const; // C99 6.2.5p17 (real floating + integer)
1966 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating)
1967 bool isVoidType() const; // C99 6.2.5p19
1968 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers)
1969 bool isAggregateType() const;
1970 bool isFundamentalType() const;
1971 bool isCompoundType() const;
1972
1973 // Type Predicates: Check to see if this type is structurally the specified
1974 // type, ignoring typedefs and qualifiers.
1975 bool isFunctionType() const;
1976 bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); }
1977 bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); }
1978 bool isPointerType() const;
1979 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer
1980 bool isBlockPointerType() const;
1981 bool isVoidPointerType() const;
1982 bool isReferenceType() const;
1983 bool isLValueReferenceType() const;
1984 bool isRValueReferenceType() const;
1985 bool isFunctionPointerType() const;
1986 bool isFunctionReferenceType() const;
1987 bool isMemberPointerType() const;
1988 bool isMemberFunctionPointerType() const;
1989 bool isMemberDataPointerType() const;
1990 bool isArrayType() const;
1991 bool isConstantArrayType() const;
1992 bool isIncompleteArrayType() const;
1993 bool isVariableArrayType() const;
1994 bool isDependentSizedArrayType() const;
1995 bool isRecordType() const;
1996 bool isClassType() const;
1997 bool isStructureType() const;
1998 bool isObjCBoxableRecordType() const;
1999 bool isInterfaceType() const;
2000 bool isStructureOrClassType() const;
2001 bool isUnionType() const;
2002 bool isComplexIntegerType() const; // GCC _Complex integer type.
2003 bool isVectorType() const; // GCC vector type.
2004 bool isExtVectorType() const; // Extended vector type.
2005 bool isDependentAddressSpaceType() const; // value-dependent address space qualifier
2006 bool isObjCObjectPointerType() const; // pointer to ObjC object
2007 bool isObjCRetainableType() const; // ObjC object or block pointer
2008 bool isObjCLifetimeType() const; // (array of)* retainable type
2009 bool isObjCIndirectLifetimeType() const; // (pointer to)* lifetime type
2010 bool isObjCNSObjectType() const; // __attribute__((NSObject))
2011 bool isObjCIndependentClassType() const; // __attribute__((objc_independent_class))
2012 // FIXME: change this to 'raw' interface type, so we can used 'interface' type
2013 // for the common case.
2014 bool isObjCObjectType() const; // NSString or typeof(*(id)0)
2015 bool isObjCQualifiedInterfaceType() const; // NSString<foo>
2016 bool isObjCQualifiedIdType() const; // id<foo>
2017 bool isObjCQualifiedClassType() const; // Class<foo>
2018 bool isObjCObjectOrInterfaceType() const;
2019 bool isObjCIdType() const; // id
2020 bool isDecltypeType() const;
2021 /// Was this type written with the special inert-in-ARC __unsafe_unretained
2022 /// qualifier?
2023 ///
2024 /// This approximates the answer to the following question: if this
2025 /// translation unit were compiled in ARC, would this type be qualified
2026 /// with __unsafe_unretained?
2027 bool isObjCInertUnsafeUnretainedType() const {
2028 return hasAttr(attr::ObjCInertUnsafeUnretained);
2029 }
2030
2031 /// Whether the type is Objective-C 'id' or a __kindof type of an
2032 /// object type, e.g., __kindof NSView * or __kindof id
2033 /// <NSCopying>.
2034 ///
2035 /// \param bound Will be set to the bound on non-id subtype types,
2036 /// which will be (possibly specialized) Objective-C class type, or
2037 /// null for 'id.
2038 bool isObjCIdOrObjectKindOfType(const ASTContext &ctx,
2039 const ObjCObjectType *&bound) const;
2040
2041 bool isObjCClassType() const; // Class
2042
2043 /// Whether the type is Objective-C 'Class' or a __kindof type of an
2044 /// Class type, e.g., __kindof Class <NSCopying>.
2045 ///
2046 /// Unlike \c isObjCIdOrObjectKindOfType, there is no relevant bound
2047 /// here because Objective-C's type system cannot express "a class
2048 /// object for a subclass of NSFoo".
2049 bool isObjCClassOrClassKindOfType() const;
2050
2051 bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const;
2052 bool isObjCSelType() const; // Class
2053 bool isObjCBuiltinType() const; // 'id' or 'Class'
2054 bool isObjCARCBridgableType() const;
2055 bool isCARCBridgableType() const;
2056 bool isTemplateTypeParmType() const; // C++ template type parameter
2057 bool isNullPtrType() const; // C++11 std::nullptr_t
2058 bool isNothrowT() const; // C++ std::nothrow_t
2059 bool isAlignValT() const; // C++17 std::align_val_t
2060 bool isStdByteType() const; // C++17 std::byte
2061 bool isAtomicType() const; // C11 _Atomic()
2062
2063#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
2064 bool is##Id##Type() const;
2065#include "clang/Basic/OpenCLImageTypes.def"
2066
2067 bool isImageType() const; // Any OpenCL image type
2068
2069 bool isSamplerT() const; // OpenCL sampler_t
2070 bool isEventT() const; // OpenCL event_t
2071 bool isClkEventT() const; // OpenCL clk_event_t
2072 bool isQueueT() const; // OpenCL queue_t
2073 bool isReserveIDT() const; // OpenCL reserve_id_t
2074
2075#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
2076 bool is##Id##Type() const;
2077#include "clang/Basic/OpenCLExtensionTypes.def"
2078 // Type defined in cl_intel_device_side_avc_motion_estimation OpenCL extension
2079 bool isOCLIntelSubgroupAVCType() const;
2080 bool isOCLExtOpaqueType() const; // Any OpenCL extension type
2081
2082 bool isPipeType() const; // OpenCL pipe type
2083 bool isOpenCLSpecificType() const; // Any OpenCL specific type
2084
2085 /// Determines if this type, which must satisfy
2086 /// isObjCLifetimeType(), is implicitly __unsafe_unretained rather
2087 /// than implicitly __strong.
2088 bool isObjCARCImplicitlyUnretainedType() const;
2089
2090 /// Return the implicit lifetime for this type, which must not be dependent.
2091 Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const;
2092
2093 enum ScalarTypeKind {
2094 STK_CPointer,
2095 STK_BlockPointer,
2096 STK_ObjCObjectPointer,
2097 STK_MemberPointer,
2098 STK_Bool,
2099 STK_Integral,
2100 STK_Floating,
2101 STK_IntegralComplex,
2102 STK_FloatingComplex,
2103 STK_FixedPoint
2104 };
2105
2106 /// Given that this is a scalar type, classify it.
2107 ScalarTypeKind getScalarTypeKind() const;
2108
2109 /// Whether this type is a dependent type, meaning that its definition
2110 /// somehow depends on a template parameter (C++ [temp.dep.type]).
2111 bool isDependentType() const { return TypeBits.Dependent; }
2112
2113 /// Determine whether this type is an instantiation-dependent type,
2114 /// meaning that the type involves a template parameter (even if the
2115 /// definition does not actually depend on the type substituted for that
2116 /// template parameter).
2117 bool isInstantiationDependentType() const {
2118 return TypeBits.InstantiationDependent;
2119 }
2120
2121 /// Determine whether this type is an undeduced type, meaning that
2122 /// it somehow involves a C++11 'auto' type or similar which has not yet been
2123 /// deduced.
2124 bool isUndeducedType() const;
2125
2126 /// Whether this type is a variably-modified type (C99 6.7.5).
2127 bool isVariablyModifiedType() const { return TypeBits.VariablyModified; }
2128
2129 /// Whether this type involves a variable-length array type
2130 /// with a definite size.
2131 bool hasSizedVLAType() const;
2132
2133 /// Whether this type is or contains a local or unnamed type.
2134 bool hasUnnamedOrLocalType() const;
2135
2136 bool isOverloadableType() const;
2137
2138 /// Determine wither this type is a C++ elaborated-type-specifier.
2139 bool isElaboratedTypeSpecifier() const;
2140
2141 bool canDecayToPointerType() const;
2142
2143 /// Whether this type is represented natively as a pointer. This includes
2144 /// pointers, references, block pointers, and Objective-C interface,
2145 /// qualified id, and qualified interface types, as well as nullptr_t.
2146 bool hasPointerRepresentation() const;
2147
2148 /// Whether this type can represent an objective pointer type for the
2149 /// purpose of GC'ability
2150 bool hasObjCPointerRepresentation() const;
2151
2152 /// Determine whether this type has an integer representation
2153 /// of some sort, e.g., it is an integer type or a vector.
2154 bool hasIntegerRepresentation() const;
2155
2156 /// Determine whether this type has an signed integer representation
2157 /// of some sort, e.g., it is an signed integer type or a vector.
2158 bool hasSignedIntegerRepresentation() const;
2159
2160 /// Determine whether this type has an unsigned integer representation
2161 /// of some sort, e.g., it is an unsigned integer type or a vector.
2162 bool hasUnsignedIntegerRepresentation() const;
2163
2164 /// Determine whether this type has a floating-point representation
2165 /// of some sort, e.g., it is a floating-point type or a vector thereof.
2166 bool hasFloatingRepresentation() const;
2167
2168 // Type Checking Functions: Check to see if this type is structurally the
2169 // specified type, ignoring typedefs and qualifiers, and return a pointer to
2170 // the best type we can.
2171 const RecordType *getAsStructureType() const;
2172 /// NOTE: getAs*ArrayType are methods on ASTContext.
2173 const RecordType *getAsUnionType() const;
2174 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type.
2175 const ObjCObjectType *getAsObjCInterfaceType() const;
2176
2177 // The following is a convenience method that returns an ObjCObjectPointerType
2178 // for object declared using an interface.
2179 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const;
2180 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const;
2181 const ObjCObjectPointerType *getAsObjCQualifiedClassType() const;
2182 const ObjCObjectType *getAsObjCQualifiedInterfaceType() const;
2183
2184 /// Retrieves the CXXRecordDecl that this type refers to, either
2185 /// because the type is a RecordType or because it is the injected-class-name
2186 /// type of a class template or class template partial specialization.
2187 CXXRecordDecl *getAsCXXRecordDecl() const;
2188
2189 /// Retrieves the RecordDecl this type refers to.
2190 RecordDecl *getAsRecordDecl() const;
2191
2192 /// Retrieves the TagDecl that this type refers to, either
2193 /// because the type is a TagType or because it is the injected-class-name
2194 /// type of a class template or class template partial specialization.
2195 TagDecl *getAsTagDecl() const;
2196
2197 /// If this is a pointer or reference to a RecordType, return the
2198 /// CXXRecordDecl that the type refers to.
2199 ///
2200 /// If this is not a pointer or reference, or the type being pointed to does
2201 /// not refer to a CXXRecordDecl, returns NULL.
2202 const CXXRecordDecl *getPointeeCXXRecordDecl() const;
2203
2204 /// Get the DeducedType whose type will be deduced for a variable with
2205 /// an initializer of this type. This looks through declarators like pointer
2206 /// types, but not through decltype or typedefs.
2207 DeducedType *getContainedDeducedType() const;
2208
2209 /// Get the AutoType whose type will be deduced for a variable with
2210 /// an initializer of this type. This looks through declarators like pointer
2211 /// types, but not through decltype or typedefs.
2212 AutoType *getContainedAutoType() const {
2213 return dyn_cast_or_null<AutoType>(getContainedDeducedType());
2214 }
2215
2216 /// Determine whether this type was written with a leading 'auto'
2217 /// corresponding to a trailing return type (possibly for a nested
2218 /// function type within a pointer to function type or similar).
2219 bool hasAutoForTrailingReturnType() const;
2220
2221 /// Member-template getAs<specific type>'. Look through sugar for
2222 /// an instance of \<specific type>. This scheme will eventually
2223 /// replace the specific getAsXXXX methods above.
2224 ///
2225 /// There are some specializations of this member template listed
2226 /// immediately following this class.
2227 template <typename T> const T *getAs() const;
2228
2229 /// Member-template getAsAdjusted<specific type>. Look through specific kinds
2230 /// of sugar (parens, attributes, etc) for an instance of \<specific type>.
2231 /// This is used when you need to walk over sugar nodes that represent some
2232 /// kind of type adjustment from a type that was written as a \<specific type>
2233 /// to another type that is still canonically a \<specific type>.
2234 template <typename T> const T *getAsAdjusted() const;
2235
2236 /// A variant of getAs<> for array types which silently discards
2237 /// qualifiers from the outermost type.
2238 const ArrayType *getAsArrayTypeUnsafe() const;
2239
2240 /// Member-template castAs<specific type>. Look through sugar for
2241 /// the underlying instance of \<specific type>.
2242 ///
2243 /// This method has the same relationship to getAs<T> as cast<T> has
2244 /// to dyn_cast<T>; which is to say, the underlying type *must*
2245 /// have the intended type, and this method will never return null.
2246 template <typename T> const T *castAs() const;
2247
2248 /// A variant of castAs<> for array type which silently discards
2249 /// qualifiers from the outermost type.
2250 const ArrayType *castAsArrayTypeUnsafe() const;
2251
2252 /// Determine whether this type had the specified attribute applied to it
2253 /// (looking through top-level type sugar).
2254 bool hasAttr(attr::Kind AK) const;
2255
2256 /// Get the base element type of this type, potentially discarding type
2257 /// qualifiers. This should never be used when type qualifiers
2258 /// are meaningful.
2259 const Type *getBaseElementTypeUnsafe() const;
2260
2261 /// If this is an array type, return the element type of the array,
2262 /// potentially with type qualifiers missing.
2263 /// This should never be used when type qualifiers are meaningful.
2264 const Type *getArrayElementTypeNoTypeQual() const;
2265
2266 /// If this is a pointer type, return the pointee type.
2267 /// If this is an array type, return the array element type.
2268 /// This should never be used when type qualifiers are meaningful.
2269 const Type *getPointeeOrArrayElementType() const;
2270
2271 /// If this is a pointer, ObjC object pointer, or block
2272 /// pointer, this returns the respective pointee.
2273 QualType getPointeeType() const;
2274
2275 /// Return the specified type with any "sugar" removed from the type,
2276 /// removing any typedefs, typeofs, etc., as well as any qualifiers.
2277 const Type *getUnqualifiedDesugaredType() const;
2278
2279 /// More type predicates useful for type checking/promotion
2280 bool isPromotableIntegerType() const; // C99 6.3.1.1p2
2281
2282 /// Return true if this is an integer type that is
2283 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
2284 /// or an enum decl which has a signed representation.
2285 bool isSignedIntegerType() const;
2286
2287 /// Return true if this is an integer type that is
2288 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool],
2289 /// or an enum decl which has an unsigned representation.
2290 bool isUnsignedIntegerType() const;
2291
2292 /// Determines whether this is an integer type that is signed or an
2293 /// enumeration types whose underlying type is a signed integer type.
2294 bool isSignedIntegerOrEnumerationType() const;
2295
2296 /// Determines whether this is an integer type that is unsigned or an
2297 /// enumeration types whose underlying type is a unsigned integer type.
2298 bool isUnsignedIntegerOrEnumerationType() const;
2299
2300 /// Return true if this is a fixed point type according to
2301 /// ISO/IEC JTC1 SC22 WG14 N1169.
2302 bool isFixedPointType() const;
2303
2304 /// Return true if this is a fixed point or integer type.
2305 bool isFixedPointOrIntegerType() const;
2306
2307 /// Return true if this is a saturated fixed point type according to
2308 /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned.
2309 bool isSaturatedFixedPointType() const;
2310
2311 /// Return true if this is a saturated fixed point type according to
2312 /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned.
2313 bool isUnsaturatedFixedPointType() const;
2314
2315 /// Return true if this is a fixed point type that is signed according
2316 /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated.
2317 bool isSignedFixedPointType() const;
2318
2319 /// Return true if this is a fixed point type that is unsigned according
2320 /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated.
2321 bool isUnsignedFixedPointType() const;
2322
2323 /// Return true if this is not a variable sized type,
2324 /// according to the rules of C99 6.7.5p3. It is not legal to call this on
2325 /// incomplete types.
2326 bool isConstantSizeType() const;
2327
2328 /// Returns true if this type can be represented by some
2329 /// set of type specifiers.
2330 bool isSpecifierType() const;
2331
2332 /// Determine the linkage of this type.
2333 Linkage getLinkage() const;
2334
2335 /// Determine the visibility of this type.
2336 Visibility getVisibility() const {
2337 return getLinkageAndVisibility().getVisibility();
2338 }
2339
2340 /// Return true if the visibility was explicitly set is the code.
2341 bool isVisibilityExplicit() const {
2342 return getLinkageAndVisibility().isVisibilityExplicit();
2343 }
2344
2345 /// Determine the linkage and visibility of this type.
2346 LinkageInfo getLinkageAndVisibility() const;
2347
2348 /// True if the computed linkage is valid. Used for consistency
2349 /// checking. Should always return true.
2350 bool isLinkageValid() const;
2351
2352 /// Determine the nullability of the given type.
2353 ///
2354 /// Note that nullability is only captured as sugar within the type
2355 /// system, not as part of the canonical type, so nullability will
2356 /// be lost by canonicalization and desugaring.
2357 Optional<NullabilityKind> getNullability(const ASTContext &context) const;
2358
2359 /// Determine whether the given type can have a nullability
2360 /// specifier applied to it, i.e., if it is any kind of pointer type.
2361 ///
2362 /// \param ResultIfUnknown The value to return if we don't yet know whether
2363 /// this type can have nullability because it is dependent.
2364 bool canHaveNullability(bool ResultIfUnknown = true) const;
2365
2366 /// Retrieve the set of substitutions required when accessing a member
2367 /// of the Objective-C receiver type that is declared in the given context.
2368 ///
2369 /// \c *this is the type of the object we're operating on, e.g., the
2370 /// receiver for a message send or the base of a property access, and is
2371 /// expected to be of some object or object pointer type.
2372 ///
2373 /// \param dc The declaration context for which we are building up a
2374 /// substitution mapping, which should be an Objective-C class, extension,
2375 /// category, or method within.
2376 ///
2377 /// \returns an array of type arguments that can be substituted for
2378 /// the type parameters of the given declaration context in any type described
2379 /// within that context, or an empty optional to indicate that no
2380 /// substitution is required.
2381 Optional<ArrayRef<QualType>>
2382 getObjCSubstitutions(const DeclContext *dc) const;
2383
2384 /// Determines if this is an ObjC interface type that may accept type
2385 /// parameters.
2386 bool acceptsObjCTypeParams() const;
2387
2388 const char *getTypeClassName() const;
2389
2390 QualType getCanonicalTypeInternal() const {
2391 return CanonicalType;
2392 }
2393
2394 CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h
2395 void dump() const;
2396 void dump(llvm::raw_ostream &OS) const;
2397};
2398
2399/// This will check for a TypedefType by removing any existing sugar
2400/// until it reaches a TypedefType or a non-sugared type.
2401template <> const TypedefType *Type::getAs() const;
2402
2403/// This will check for a TemplateSpecializationType by removing any
2404/// existing sugar until it reaches a TemplateSpecializationType or a
2405/// non-sugared type.
2406template <> const TemplateSpecializationType *Type::getAs() const;
2407
2408/// This will check for an AttributedType by removing any existing sugar
2409/// until it reaches an AttributedType or a non-sugared type.
2410template <> const AttributedType *Type::getAs() const;
2411
2412// We can do canonical leaf types faster, because we don't have to
2413// worry about preserving child type decoration.
2414#define TYPE(Class, Base)
2415#define LEAF_TYPE(Class) \
2416template <> inline const Class##Type *Type::getAs() const { \
2417 return dyn_cast<Class##Type>(CanonicalType); \
2418} \
2419template <> inline const Class##Type *Type::castAs() const { \
2420 return cast<Class##Type>(CanonicalType); \
2421}
2422#include "clang/AST/TypeNodes.inc"
2423
2424/// This class is used for builtin types like 'int'. Builtin
2425/// types are always canonical and have a literal name field.
2426class BuiltinType : public Type {
2427public:
2428 enum Kind {
2429// OpenCL image types
2430#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) Id,
2431#include "clang/Basic/OpenCLImageTypes.def"
2432// OpenCL extension types
2433#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) Id,
2434#include "clang/Basic/OpenCLExtensionTypes.def"
2435// SVE Types
2436#define SVE_TYPE(Name, Id, SingletonId) Id,
2437#include "clang/Basic/AArch64SVEACLETypes.def"
2438// All other builtin types
2439#define BUILTIN_TYPE(Id, SingletonId) Id,
2440#define LAST_BUILTIN_TYPE(Id) LastKind = Id
2441#include "clang/AST/BuiltinTypes.def"
2442 };
2443
2444private:
2445 friend class ASTContext; // ASTContext creates these.
2446
2447 BuiltinType(Kind K)
2448 : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent),
2449 /*InstantiationDependent=*/(K == Dependent),
2450 /*VariablyModified=*/false,
2451 /*Unexpanded parameter pack=*/false) {
2452 BuiltinTypeBits.Kind = K;
2453 }
2454
2455public:
2456 Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); }
2457 StringRef getName(const PrintingPolicy &Policy) const;
2458
2459 const char *getNameAsCString(const PrintingPolicy &Policy) const {
2460 // The StringRef is null-terminated.
2461 StringRef str = getName(Policy);
2462 assert(!str.empty() && str.data()[str.size()] == '\0')((!str.empty() && str.data()[str.size()] == '\0') ? static_cast
<void> (0) : __assert_fail ("!str.empty() && str.data()[str.size()] == '\\0'"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 2462, __PRETTY_FUNCTION__))
;
2463 return str.data();
2464 }
2465
2466 bool isSugared() const { return false; }
2467 QualType desugar() const { return QualType(this, 0); }
2468
2469 bool isInteger() const {
2470 return getKind() >= Bool && getKind() <= Int128;
2471 }
2472
2473 bool isSignedInteger() const {
2474 return getKind() >= Char_S && getKind() <= Int128;
2475 }
2476
2477 bool isUnsignedInteger() const {
2478 return getKind() >= Bool && getKind() <= UInt128;
2479 }
2480
2481 bool isFloatingPoint() const {
2482 return getKind() >= Half && getKind() <= Float128;
2483 }
2484
2485 /// Determines whether the given kind corresponds to a placeholder type.
2486 static bool isPlaceholderTypeKind(Kind K) {
2487 return K >= Overload;
2488 }
2489
2490 /// Determines whether this type is a placeholder type, i.e. a type
2491 /// which cannot appear in arbitrary positions in a fully-formed
2492 /// expression.
2493 bool isPlaceholderType() const {
2494 return isPlaceholderTypeKind(getKind());
2495 }
2496
2497 /// Determines whether this type is a placeholder type other than
2498 /// Overload. Most placeholder types require only syntactic
2499 /// information about their context in order to be resolved (e.g.
2500 /// whether it is a call expression), which means they can (and
2501 /// should) be resolved in an earlier "phase" of analysis.
2502 /// Overload expressions sometimes pick up further information
2503 /// from their context, like whether the context expects a
2504 /// specific function-pointer type, and so frequently need
2505 /// special treatment.
2506 bool isNonOverloadPlaceholderType() const {
2507 return getKind() > Overload;
2508 }
2509
2510 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; }
2511};
2512
2513/// Complex values, per C99 6.2.5p11. This supports the C99 complex
2514/// types (_Complex float etc) as well as the GCC integer complex extensions.
2515class ComplexType : public Type, public llvm::FoldingSetNode {
2516 friend class ASTContext; // ASTContext creates these.
2517
2518 QualType ElementType;
2519
2520 ComplexType(QualType Element, QualType CanonicalPtr)
2521 : Type(Complex, CanonicalPtr, Element->isDependentType(),
2522 Element->isInstantiationDependentType(),
2523 Element->isVariablyModifiedType(),
2524 Element->containsUnexpandedParameterPack()),
2525 ElementType(Element) {}
2526
2527public:
2528 QualType getElementType() const { return ElementType; }
2529
2530 bool isSugared() const { return false; }
2531 QualType desugar() const { return QualType(this, 0); }
2532
2533 void Profile(llvm::FoldingSetNodeID &ID) {
2534 Profile(ID, getElementType());
2535 }
2536
2537 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) {
2538 ID.AddPointer(Element.getAsOpaquePtr());
2539 }
2540
2541 static bool classof(const Type *T) { return T->getTypeClass() == Complex; }
2542};
2543
2544/// Sugar for parentheses used when specifying types.
2545class ParenType : public Type, public llvm::FoldingSetNode {
2546 friend class ASTContext; // ASTContext creates these.
2547
2548 QualType Inner;
2549
2550 ParenType(QualType InnerType, QualType CanonType)
2551 : Type(Paren, CanonType, InnerType->isDependentType(),
2552 InnerType->isInstantiationDependentType(),
2553 InnerType->isVariablyModifiedType(),
2554 InnerType->containsUnexpandedParameterPack()),
2555 Inner(InnerType) {}
2556
2557public:
2558 QualType getInnerType() const { return Inner; }
2559
2560 bool isSugared() const { return true; }
2561 QualType desugar() const { return getInnerType(); }
2562
2563 void Profile(llvm::FoldingSetNodeID &ID) {
2564 Profile(ID, getInnerType());
2565 }
2566
2567 static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) {
2568 Inner.Profile(ID);
2569 }
2570
2571 static bool classof(const Type *T) { return T->getTypeClass() == Paren; }
2572};
2573
2574/// PointerType - C99 6.7.5.1 - Pointer Declarators.
2575class PointerType : public Type, public llvm::FoldingSetNode {
2576 friend class ASTContext; // ASTContext creates these.
2577
2578 QualType PointeeType;
2579
2580 PointerType(QualType Pointee, QualType CanonicalPtr)
2581 : Type(Pointer, CanonicalPtr, Pointee->isDependentType(),
2582 Pointee->isInstantiationDependentType(),
2583 Pointee->isVariablyModifiedType(),
2584 Pointee->containsUnexpandedParameterPack()),
2585 PointeeType(Pointee) {}
2586
2587public:
2588 QualType getPointeeType() const { return PointeeType; }
2589
2590 /// Returns true if address spaces of pointers overlap.
2591 /// OpenCL v2.0 defines conversion rules for pointers to different
2592 /// address spaces (OpenCLC v2.0 s6.5.5) and notion of overlapping
2593 /// address spaces.
2594 /// CL1.1 or CL1.2:
2595 /// address spaces overlap iff they are they same.
2596 /// CL2.0 adds:
2597 /// __generic overlaps with any address space except for __constant.
2598 bool isAddressSpaceOverlapping(const PointerType &other) const {
2599 Qualifiers thisQuals = PointeeType.getQualifiers();
2600 Qualifiers otherQuals = other.getPointeeType().getQualifiers();
2601 // Address spaces overlap if at least one of them is a superset of another
2602 return thisQuals.isAddressSpaceSupersetOf(otherQuals) ||
2603 otherQuals.isAddressSpaceSupersetOf(thisQuals);
2604 }
2605
2606 bool isSugared() const { return false; }
2607 QualType desugar() const { return QualType(this, 0); }
2608
2609 void Profile(llvm::FoldingSetNodeID &ID) {
2610 Profile(ID, getPointeeType());
2611 }
2612
2613 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
2614 ID.AddPointer(Pointee.getAsOpaquePtr());
2615 }
2616
2617 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
2618};
2619
2620/// Represents a type which was implicitly adjusted by the semantic
2621/// engine for arbitrary reasons. For example, array and function types can
2622/// decay, and function types can have their calling conventions adjusted.
2623class AdjustedType : public Type, public llvm::FoldingSetNode {
2624 QualType OriginalTy;
2625 QualType AdjustedTy;
2626
2627protected:
2628 friend class ASTContext; // ASTContext creates these.
2629
2630 AdjustedType(TypeClass TC, QualType OriginalTy, QualType AdjustedTy,
2631 QualType CanonicalPtr)
2632 : Type(TC, CanonicalPtr, OriginalTy->isDependentType(),
2633 OriginalTy->isInstantiationDependentType(),
2634 OriginalTy->isVariablyModifiedType(),
2635 OriginalTy->containsUnexpandedParameterPack()),
2636 OriginalTy(OriginalTy), AdjustedTy(AdjustedTy) {}
2637
2638public:
2639 QualType getOriginalType() const { return OriginalTy; }
2640 QualType getAdjustedType() const { return AdjustedTy; }
2641
2642 bool isSugared() const { return true; }
2643 QualType desugar() const { return AdjustedTy; }
2644
2645 void Profile(llvm::FoldingSetNodeID &ID) {
2646 Profile(ID, OriginalTy, AdjustedTy);
2647 }
2648
2649 static void Profile(llvm::FoldingSetNodeID &ID, QualType Orig, QualType New) {
2650 ID.AddPointer(Orig.getAsOpaquePtr());
2651 ID.AddPointer(New.getAsOpaquePtr());
2652 }
2653
2654 static bool classof(const Type *T) {
2655 return T->getTypeClass() == Adjusted || T->getTypeClass() == Decayed;
2656 }
2657};
2658
2659/// Represents a pointer type decayed from an array or function type.
2660class DecayedType : public AdjustedType {
2661 friend class ASTContext; // ASTContext creates these.
2662
2663 inline
2664 DecayedType(QualType OriginalType, QualType Decayed, QualType Canonical);
2665
2666public:
2667 QualType getDecayedType() const { return getAdjustedType(); }
2668
2669 inline QualType getPointeeType() const;
2670
2671 static bool classof(const Type *T) { return T->getTypeClass() == Decayed; }
2672};
2673
2674/// Pointer to a block type.
2675/// This type is to represent types syntactically represented as
2676/// "void (^)(int)", etc. Pointee is required to always be a function type.
2677class BlockPointerType : public Type, public llvm::FoldingSetNode {
2678 friend class ASTContext; // ASTContext creates these.
2679
2680 // Block is some kind of pointer type
2681 QualType PointeeType;
2682
2683 BlockPointerType(QualType Pointee, QualType CanonicalCls)
2684 : Type(BlockPointer, CanonicalCls, Pointee->isDependentType(),
2685 Pointee->isInstantiationDependentType(),
2686 Pointee->isVariablyModifiedType(),
2687 Pointee->containsUnexpandedParameterPack()),
2688 PointeeType(Pointee) {}
2689
2690public:
2691 // Get the pointee type. Pointee is required to always be a function type.
2692 QualType getPointeeType() const { return PointeeType; }
2693
2694 bool isSugared() const { return false; }
2695 QualType desugar() const { return QualType(this, 0); }
2696
2697 void Profile(llvm::FoldingSetNodeID &ID) {
2698 Profile(ID, getPointeeType());
2699 }
2700
2701 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
2702 ID.AddPointer(Pointee.getAsOpaquePtr());
2703 }
2704
2705 static bool classof(const Type *T) {
2706 return T->getTypeClass() == BlockPointer;
2707 }
2708};
2709
2710/// Base for LValueReferenceType and RValueReferenceType
2711class ReferenceType : public Type, public llvm::FoldingSetNode {
2712 QualType PointeeType;
2713
2714protected:
2715 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef,
2716 bool SpelledAsLValue)
2717 : Type(tc, CanonicalRef, Referencee->isDependentType(),
2718 Referencee->isInstantiationDependentType(),
2719 Referencee->isVariablyModifiedType(),
2720 Referencee->containsUnexpandedParameterPack()),
2721 PointeeType(Referencee) {
2722 ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue;
2723 ReferenceTypeBits.InnerRef = Referencee->isReferenceType();
2724 }
2725
2726public:
2727 bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; }
2728 bool isInnerRef() const { return ReferenceTypeBits.InnerRef; }
2729
2730 QualType getPointeeTypeAsWritten() const { return PointeeType; }
2731
2732 QualType getPointeeType() const {
2733 // FIXME: this might strip inner qualifiers; okay?
2734 const ReferenceType *T = this;
2735 while (T->isInnerRef())
2736 T = T->PointeeType->castAs<ReferenceType>();
2737 return T->PointeeType;
2738 }
2739
2740 void Profile(llvm::FoldingSetNodeID &ID) {
2741 Profile(ID, PointeeType, isSpelledAsLValue());
2742 }
2743
2744 static void Profile(llvm::FoldingSetNodeID &ID,
2745 QualType Referencee,
2746 bool SpelledAsLValue) {
2747 ID.AddPointer(Referencee.getAsOpaquePtr());
2748 ID.AddBoolean(SpelledAsLValue);
2749 }
2750
2751 static bool classof(const Type *T) {
2752 return T->getTypeClass() == LValueReference ||
2753 T->getTypeClass() == RValueReference;
2754 }
2755};
2756
2757/// An lvalue reference type, per C++11 [dcl.ref].
2758class LValueReferenceType : public ReferenceType {
2759 friend class ASTContext; // ASTContext creates these
2760
2761 LValueReferenceType(QualType Referencee, QualType CanonicalRef,
2762 bool SpelledAsLValue)
2763 : ReferenceType(LValueReference, Referencee, CanonicalRef,
2764 SpelledAsLValue) {}
2765
2766public:
2767 bool isSugared() const { return false; }
2768 QualType desugar() const { return QualType(this, 0); }
2769
2770 static bool classof(const Type *T) {
2771 return T->getTypeClass() == LValueReference;
2772 }
2773};
2774
2775/// An rvalue reference type, per C++11 [dcl.ref].
2776class RValueReferenceType : public ReferenceType {
2777 friend class ASTContext; // ASTContext creates these
2778
2779 RValueReferenceType(QualType Referencee, QualType CanonicalRef)
2780 : ReferenceType(RValueReference, Referencee, CanonicalRef, false) {}
2781
2782public:
2783 bool isSugared() const { return false; }
2784 QualType desugar() const { return QualType(this, 0); }
2785
2786 static bool classof(const Type *T) {
2787 return T->getTypeClass() == RValueReference;
2788 }
2789};
2790
2791/// A pointer to member type per C++ 8.3.3 - Pointers to members.
2792///
2793/// This includes both pointers to data members and pointer to member functions.
2794class MemberPointerType : public Type, public llvm::FoldingSetNode {
2795 friend class ASTContext; // ASTContext creates these.
2796
2797 QualType PointeeType;
2798
2799 /// The class of which the pointee is a member. Must ultimately be a
2800 /// RecordType, but could be a typedef or a template parameter too.
2801 const Type *Class;
2802
2803 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr)
2804 : Type(MemberPointer, CanonicalPtr,
2805 Cls->isDependentType() || Pointee->isDependentType(),
2806 (Cls->isInstantiationDependentType() ||
2807 Pointee->isInstantiationDependentType()),
2808 Pointee->isVariablyModifiedType(),
2809 (Cls->containsUnexpandedParameterPack() ||
2810 Pointee->containsUnexpandedParameterPack())),
2811 PointeeType(Pointee), Class(Cls) {}
2812
2813public:
2814 QualType getPointeeType() const { return PointeeType; }
2815
2816 /// Returns true if the member type (i.e. the pointee type) is a
2817 /// function type rather than a data-member type.
2818 bool isMemberFunctionPointer() const {
2819 return PointeeType->isFunctionProtoType();
2820 }
2821
2822 /// Returns true if the member type (i.e. the pointee type) is a
2823 /// data type rather than a function type.
2824 bool isMemberDataPointer() const {
2825 return !PointeeType->isFunctionProtoType();
2826 }
2827
2828 const Type *getClass() const { return Class; }
2829 CXXRecordDecl *getMostRecentCXXRecordDecl() const;
2830
2831 bool isSugared() const { return false; }
2832 QualType desugar() const { return QualType(this, 0); }
2833
2834 void Profile(llvm::FoldingSetNodeID &ID) {
2835 Profile(ID, getPointeeType(), getClass());
2836 }
2837
2838 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee,
2839 const Type *Class) {
2840 ID.AddPointer(Pointee.getAsOpaquePtr());
2841 ID.AddPointer(Class);
2842 }
2843
2844 static bool classof(const Type *T) {
2845 return T->getTypeClass() == MemberPointer;
2846 }
2847};
2848
2849/// Represents an array type, per C99 6.7.5.2 - Array Declarators.
2850class ArrayType : public Type, public llvm::FoldingSetNode {
2851public:
2852 /// Capture whether this is a normal array (e.g. int X[4])
2853 /// an array with a static size (e.g. int X[static 4]), or an array
2854 /// with a star size (e.g. int X[*]).
2855 /// 'static' is only allowed on function parameters.
2856 enum ArraySizeModifier {
2857 Normal, Static, Star
2858 };
2859
2860private:
2861 /// The element type of the array.
2862 QualType ElementType;
2863
2864protected:
2865 friend class ASTContext; // ASTContext creates these.
2866
2867 // C++ [temp.dep.type]p1:
2868 // A type is dependent if it is...
2869 // - an array type constructed from any dependent type or whose
2870 // size is specified by a constant expression that is
2871 // value-dependent,
2872 ArrayType(TypeClass tc, QualType et, QualType can,
2873 ArraySizeModifier sm, unsigned tq,
2874 bool ContainsUnexpandedParameterPack)
2875 : Type(tc, can, et->isDependentType() || tc == DependentSizedArray,
2876 et->isInstantiationDependentType() || tc == DependentSizedArray,
2877 (tc == VariableArray || et->isVariablyModifiedType()),
2878 ContainsUnexpandedParameterPack),
2879 ElementType(et) {
2880 ArrayTypeBits.IndexTypeQuals = tq;
2881 ArrayTypeBits.SizeModifier = sm;
2882 }
2883
2884public:
2885 QualType getElementType() const { return ElementType; }
2886
2887 ArraySizeModifier getSizeModifier() const {
2888 return ArraySizeModifier(ArrayTypeBits.SizeModifier);
2889 }
2890
2891 Qualifiers getIndexTypeQualifiers() const {
2892 return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers());
2893 }
2894
2895 unsigned getIndexTypeCVRQualifiers() const {
2896 return ArrayTypeBits.IndexTypeQuals;
2897 }
2898
2899 static bool classof(const Type *T) {
2900 return T->getTypeClass() == ConstantArray ||
2901 T->getTypeClass() == VariableArray ||
2902 T->getTypeClass() == IncompleteArray ||
2903 T->getTypeClass() == DependentSizedArray;
2904 }
2905};
2906
2907/// Represents the canonical version of C arrays with a specified constant size.
2908/// For example, the canonical type for 'int A[4 + 4*100]' is a
2909/// ConstantArrayType where the element type is 'int' and the size is 404.
2910class ConstantArrayType : public ArrayType {
2911 llvm::APInt Size; // Allows us to unique the type.
2912
2913 ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
2914 ArraySizeModifier sm, unsigned tq)
2915 : ArrayType(ConstantArray, et, can, sm, tq,
2916 et->containsUnexpandedParameterPack()),
2917 Size(size) {}
2918
2919protected:
2920 friend class ASTContext; // ASTContext creates these.
2921
2922 ConstantArrayType(TypeClass tc, QualType et, QualType can,
2923 const llvm::APInt &size, ArraySizeModifier sm, unsigned tq)
2924 : ArrayType(tc, et, can, sm, tq, et->containsUnexpandedParameterPack()),
2925 Size(size) {}
2926
2927public:
2928 const llvm::APInt &getSize() const { return Size; }
2929 bool isSugared() const { return false; }
2930 QualType desugar() const { return QualType(this, 0); }
2931
2932 /// Determine the number of bits required to address a member of
2933 // an array with the given element type and number of elements.
2934 static unsigned getNumAddressingBits(const ASTContext &Context,
2935 QualType ElementType,
2936 const llvm::APInt &NumElements);
2937
2938 /// Determine the maximum number of active bits that an array's size
2939 /// can require, which limits the maximum size of the array.
2940 static unsigned getMaxSizeBits(const ASTContext &Context);
2941
2942 void Profile(llvm::FoldingSetNodeID &ID) {
2943 Profile(ID, getElementType(), getSize(),
2944 getSizeModifier(), getIndexTypeCVRQualifiers());
2945 }
2946
2947 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
2948 const llvm::APInt &ArraySize, ArraySizeModifier SizeMod,
2949 unsigned TypeQuals) {
2950 ID.AddPointer(ET.getAsOpaquePtr());
2951 ID.AddInteger(ArraySize.getZExtValue());
2952 ID.AddInteger(SizeMod);
2953 ID.AddInteger(TypeQuals);
2954 }
2955
2956 static bool classof(const Type *T) {
2957 return T->getTypeClass() == ConstantArray;
2958 }
2959};
2960
2961/// Represents a C array with an unspecified size. For example 'int A[]' has
2962/// an IncompleteArrayType where the element type is 'int' and the size is
2963/// unspecified.
2964class IncompleteArrayType : public ArrayType {
2965 friend class ASTContext; // ASTContext creates these.
2966
2967 IncompleteArrayType(QualType et, QualType can,
2968 ArraySizeModifier sm, unsigned tq)
2969 : ArrayType(IncompleteArray, et, can, sm, tq,
2970 et->containsUnexpandedParameterPack()) {}
2971
2972public:
2973 friend class StmtIteratorBase;
2974
2975 bool isSugared() const { return false; }
2976 QualType desugar() const { return QualType(this, 0); }
2977
2978 static bool classof(const Type *T) {
2979 return T->getTypeClass() == IncompleteArray;
2980 }
2981
2982 void Profile(llvm::FoldingSetNodeID &ID) {
2983 Profile(ID, getElementType(), getSizeModifier(),
2984 getIndexTypeCVRQualifiers());
2985 }
2986
2987 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
2988 ArraySizeModifier SizeMod, unsigned TypeQuals) {
2989 ID.AddPointer(ET.getAsOpaquePtr());
2990 ID.AddInteger(SizeMod);
2991 ID.AddInteger(TypeQuals);
2992 }
2993};
2994
2995/// Represents a C array with a specified size that is not an
2996/// integer-constant-expression. For example, 'int s[x+foo()]'.
2997/// Since the size expression is an arbitrary expression, we store it as such.
2998///
2999/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and
3000/// should not be: two lexically equivalent variable array types could mean
3001/// different things, for example, these variables do not have the same type
3002/// dynamically:
3003///
3004/// void foo(int x) {
3005/// int Y[x];
3006/// ++x;
3007/// int Z[x];
3008/// }
3009class VariableArrayType : public ArrayType {
3010 friend class ASTContext; // ASTContext creates these.
3011
3012 /// An assignment-expression. VLA's are only permitted within
3013 /// a function block.
3014 Stmt *SizeExpr;
3015
3016 /// The range spanned by the left and right array brackets.
3017 SourceRange Brackets;
3018
3019 VariableArrayType(QualType et, QualType can, Expr *e,
3020 ArraySizeModifier sm, unsigned tq,
3021 SourceRange brackets)
3022 : ArrayType(VariableArray, et, can, sm, tq,
3023 et->containsUnexpandedParameterPack()),
3024 SizeExpr((Stmt*) e), Brackets(brackets) {}
3025
3026public:
3027 friend class StmtIteratorBase;
3028
3029 Expr *getSizeExpr() const {
3030 // We use C-style casts instead of cast<> here because we do not wish
3031 // to have a dependency of Type.h on Stmt.h/Expr.h.
3032 return (Expr*) SizeExpr;
3033 }
3034
3035 SourceRange getBracketsRange() const { return Brackets; }
3036 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
3037 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
3038
3039 bool isSugared() const { return false; }
3040 QualType desugar() const { return QualType(this, 0); }
3041
3042 static bool classof(const Type *T) {
3043 return T->getTypeClass() == VariableArray;
3044 }
3045
3046 void Profile(llvm::FoldingSetNodeID &ID) {
3047 llvm_unreachable("Cannot unique VariableArrayTypes.")::llvm::llvm_unreachable_internal("Cannot unique VariableArrayTypes."
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 3047)
;
3048 }
3049};
3050
3051/// Represents an array type in C++ whose size is a value-dependent expression.
3052///
3053/// For example:
3054/// \code
3055/// template<typename T, int Size>
3056/// class array {
3057/// T data[Size];
3058/// };
3059/// \endcode
3060///
3061/// For these types, we won't actually know what the array bound is
3062/// until template instantiation occurs, at which point this will
3063/// become either a ConstantArrayType or a VariableArrayType.
3064class DependentSizedArrayType : public ArrayType {
3065 friend class ASTContext; // ASTContext creates these.
3066
3067 const ASTContext &Context;
3068
3069 /// An assignment expression that will instantiate to the
3070 /// size of the array.
3071 ///
3072 /// The expression itself might be null, in which case the array
3073 /// type will have its size deduced from an initializer.
3074 Stmt *SizeExpr;
3075
3076 /// The range spanned by the left and right array brackets.
3077 SourceRange Brackets;
3078
3079 DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can,
3080 Expr *e, ArraySizeModifier sm, unsigned tq,
3081 SourceRange brackets);
3082
3083public:
3084 friend class StmtIteratorBase;
3085
3086 Expr *getSizeExpr() const {
3087 // We use C-style casts instead of cast<> here because we do not wish
3088 // to have a dependency of Type.h on Stmt.h/Expr.h.
3089 return (Expr*) SizeExpr;
3090 }
3091
3092 SourceRange getBracketsRange() const { return Brackets; }
3093 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
3094 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
3095
3096 bool isSugared() const { return false; }
3097 QualType desugar() const { return QualType(this, 0); }
3098
3099 static bool classof(const Type *T) {
3100 return T->getTypeClass() == DependentSizedArray;
3101 }
3102
3103 void Profile(llvm::FoldingSetNodeID &ID) {
3104 Profile(ID, Context, getElementType(),
3105 getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr());
3106 }
3107
3108 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3109 QualType ET, ArraySizeModifier SizeMod,
3110 unsigned TypeQuals, Expr *E);
3111};
3112
3113/// Represents an extended address space qualifier where the input address space
3114/// value is dependent. Non-dependent address spaces are not represented with a
3115/// special Type subclass; they are stored on an ExtQuals node as part of a QualType.
3116///
3117/// For example:
3118/// \code
3119/// template<typename T, int AddrSpace>
3120/// class AddressSpace {
3121/// typedef T __attribute__((address_space(AddrSpace))) type;
3122/// }
3123/// \endcode
3124class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode {
3125 friend class ASTContext;
3126
3127 const ASTContext &Context;
3128 Expr *AddrSpaceExpr;
3129 QualType PointeeType;
3130 SourceLocation loc;
3131
3132 DependentAddressSpaceType(const ASTContext &Context, QualType PointeeType,
3133 QualType can, Expr *AddrSpaceExpr,
3134 SourceLocation loc);
3135
3136public:
3137 Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; }
3138 QualType getPointeeType() const { return PointeeType; }
3139 SourceLocation getAttributeLoc() const { return loc; }
3140
3141 bool isSugared() const { return false; }
3142 QualType desugar() const { return QualType(this, 0); }
3143
3144 static bool classof(const Type *T) {
3145 return T->getTypeClass() == DependentAddressSpace;
3146 }
3147
3148 void Profile(llvm::FoldingSetNodeID &ID) {
3149 Profile(ID, Context, getPointeeType(), getAddrSpaceExpr());
3150 }
3151
3152 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3153 QualType PointeeType, Expr *AddrSpaceExpr);
3154};
3155
3156/// Represents an extended vector type where either the type or size is
3157/// dependent.
3158///
3159/// For example:
3160/// \code
3161/// template<typename T, int Size>
3162/// class vector {
3163/// typedef T __attribute__((ext_vector_type(Size))) type;
3164/// }
3165/// \endcode
3166class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode {
3167 friend class ASTContext;
3168
3169 const ASTContext &Context;
3170 Expr *SizeExpr;
3171
3172 /// The element type of the array.
3173 QualType ElementType;
3174
3175 SourceLocation loc;
3176
3177 DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType,
3178 QualType can, Expr *SizeExpr, SourceLocation loc);
3179
3180public:
3181 Expr *getSizeExpr() const { return SizeExpr; }
3182 QualType getElementType() const { return ElementType; }
3183 SourceLocation getAttributeLoc() const { return loc; }
3184
3185 bool isSugared() const { return false; }
3186 QualType desugar() const { return QualType(this, 0); }
3187
3188 static bool classof(const Type *T) {
3189 return T->getTypeClass() == DependentSizedExtVector;
3190 }
3191
3192 void Profile(llvm::FoldingSetNodeID &ID) {
3193 Profile(ID, Context, getElementType(), getSizeExpr());
3194 }
3195
3196 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3197 QualType ElementType, Expr *SizeExpr);
3198};
3199
3200
3201/// Represents a GCC generic vector type. This type is created using
3202/// __attribute__((vector_size(n)), where "n" specifies the vector size in
3203/// bytes; or from an Altivec __vector or vector declaration.
3204/// Since the constructor takes the number of vector elements, the
3205/// client is responsible for converting the size into the number of elements.
3206class VectorType : public Type, public llvm::FoldingSetNode {
3207public:
3208 enum VectorKind {
3209 /// not a target-specific vector type
3210 GenericVector,
3211
3212 /// is AltiVec vector
3213 AltiVecVector,
3214
3215 /// is AltiVec 'vector Pixel'
3216 AltiVecPixel,
3217
3218 /// is AltiVec 'vector bool ...'
3219 AltiVecBool,
3220
3221 /// is ARM Neon vector
3222 NeonVector,
3223
3224 /// is ARM Neon polynomial vector
3225 NeonPolyVector
3226 };
3227
3228protected:
3229 friend class ASTContext; // ASTContext creates these.
3230
3231 /// The element type of the vector.
3232 QualType ElementType;
3233
3234 VectorType(QualType vecType, unsigned nElements, QualType canonType,
3235 VectorKind vecKind);
3236
3237 VectorType(TypeClass tc, QualType vecType, unsigned nElements,
3238 QualType canonType, VectorKind vecKind);
3239
3240public:
3241 QualType getElementType() const { return ElementType; }
3242 unsigned getNumElements() const { return VectorTypeBits.NumElements; }
3243
3244 static bool isVectorSizeTooLarge(unsigned NumElements) {
3245 return NumElements > VectorTypeBitfields::MaxNumElements;
3246 }
3247
3248 bool isSugared() const { return false; }
3249 QualType desugar() const { return QualType(this, 0); }
3250
3251 VectorKind getVectorKind() const {
3252 return VectorKind(VectorTypeBits.VecKind);
3253 }
3254
3255 void Profile(llvm::FoldingSetNodeID &ID) {
3256 Profile(ID, getElementType(), getNumElements(),
3257 getTypeClass(), getVectorKind());
3258 }
3259
3260 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
3261 unsigned NumElements, TypeClass TypeClass,
3262 VectorKind VecKind) {
3263 ID.AddPointer(ElementType.getAsOpaquePtr());
3264 ID.AddInteger(NumElements);
3265 ID.AddInteger(TypeClass);
3266 ID.AddInteger(VecKind);
3267 }
3268
3269 static bool classof(const Type *T) {
3270 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector;
3271 }
3272};
3273
3274/// Represents a vector type where either the type or size is dependent.
3275////
3276/// For example:
3277/// \code
3278/// template<typename T, int Size>
3279/// class vector {
3280/// typedef T __attribute__((vector_size(Size))) type;
3281/// }
3282/// \endcode
3283class DependentVectorType : public Type, public llvm::FoldingSetNode {
3284 friend class ASTContext;
3285
3286 const ASTContext &Context;
3287 QualType ElementType;
3288 Expr *SizeExpr;
3289 SourceLocation Loc;
3290
3291 DependentVectorType(const ASTContext &Context, QualType ElementType,
3292 QualType CanonType, Expr *SizeExpr,
3293 SourceLocation Loc, VectorType::VectorKind vecKind);
3294
3295public:
3296 Expr *getSizeExpr() const { return SizeExpr; }
3297 QualType getElementType() const { return ElementType; }
3298 SourceLocation getAttributeLoc() const { return Loc; }
3299 VectorType::VectorKind getVectorKind() const {
3300 return VectorType::VectorKind(VectorTypeBits.VecKind);
3301 }
3302
3303 bool isSugared() const { return false; }
3304 QualType desugar() const { return QualType(this, 0); }
3305
3306 static bool classof(const Type *T) {
3307 return T->getTypeClass() == DependentVector;
3308 }
3309
3310 void Profile(llvm::FoldingSetNodeID &ID) {
3311 Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind());
3312 }
3313
3314 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3315 QualType ElementType, const Expr *SizeExpr,
3316 VectorType::VectorKind VecKind);
3317};
3318
3319/// ExtVectorType - Extended vector type. This type is created using
3320/// __attribute__((ext_vector_type(n)), where "n" is the number of elements.
3321/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This
3322/// class enables syntactic extensions, like Vector Components for accessing
3323/// points (as .xyzw), colors (as .rgba), and textures (modeled after OpenGL
3324/// Shading Language).
3325class ExtVectorType : public VectorType {
3326 friend class ASTContext; // ASTContext creates these.
3327
3328 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType)
3329 : VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {}
3330
3331public:
3332 static int getPointAccessorIdx(char c) {
3333 switch (c) {
3334 default: return -1;
3335 case 'x': case 'r': return 0;
3336 case 'y': case 'g': return 1;
3337 case 'z': case 'b': return 2;
3338 case 'w': case 'a': return 3;
3339 }
3340 }
3341
3342 static int getNumericAccessorIdx(char c) {
3343 switch (c) {
3344 default: return -1;
3345 case '0': return 0;
3346 case '1': return 1;
3347 case '2': return 2;
3348 case '3': return 3;
3349 case '4': return 4;
3350 case '5': return 5;
3351 case '6': return 6;
3352 case '7': return 7;
3353 case '8': return 8;
3354 case '9': return 9;
3355 case 'A':
3356 case 'a': return 10;
3357 case 'B':
3358 case 'b': return 11;
3359 case 'C':
3360 case 'c': return 12;
3361 case 'D':
3362 case 'd': return 13;
3363 case 'E':
3364 case 'e': return 14;
3365 case 'F':
3366 case 'f': return 15;
3367 }
3368 }
3369
3370 static int getAccessorIdx(char c, bool isNumericAccessor) {
3371 if (isNumericAccessor)
3372 return getNumericAccessorIdx(c);
3373 else
3374 return getPointAccessorIdx(c);
3375 }
3376
3377 bool isAccessorWithinNumElements(char c, bool isNumericAccessor) const {
3378 if (int idx = getAccessorIdx(c, isNumericAccessor)+1)
3379 return unsigned(idx-1) < getNumElements();
3380 return false;
3381 }
3382
3383 bool isSugared() const { return false; }
3384 QualType desugar() const { return QualType(this, 0); }
3385
3386 static bool classof(const Type *T) {
3387 return T->getTypeClass() == ExtVector;
3388 }
3389};
3390
3391/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base
3392/// class of FunctionNoProtoType and FunctionProtoType.
3393class FunctionType : public Type {
3394 // The type returned by the function.
3395 QualType ResultType;
3396
3397public:
3398 /// Interesting information about a specific parameter that can't simply
3399 /// be reflected in parameter's type. This is only used by FunctionProtoType
3400 /// but is in FunctionType to make this class available during the
3401 /// specification of the bases of FunctionProtoType.
3402 ///
3403 /// It makes sense to model language features this way when there's some
3404 /// sort of parameter-specific override (such as an attribute) that
3405 /// affects how the function is called. For example, the ARC ns_consumed
3406 /// attribute changes whether a parameter is passed at +0 (the default)
3407 /// or +1 (ns_consumed). This must be reflected in the function type,
3408 /// but isn't really a change to the parameter type.
3409 ///
3410 /// One serious disadvantage of modelling language features this way is
3411 /// that they generally do not work with language features that attempt
3412 /// to destructure types. For example, template argument deduction will
3413 /// not be able to match a parameter declared as
3414 /// T (*)(U)
3415 /// against an argument of type
3416 /// void (*)(__attribute__((ns_consumed)) id)
3417 /// because the substitution of T=void, U=id into the former will
3418 /// not produce the latter.
3419 class ExtParameterInfo {
3420 enum {
3421 ABIMask = 0x0F,
3422 IsConsumed = 0x10,
3423 HasPassObjSize = 0x20,
3424 IsNoEscape = 0x40,
3425 };
3426 unsigned char Data = 0;
3427
3428 public:
3429 ExtParameterInfo() = default;
3430
3431 /// Return the ABI treatment of this parameter.
3432 ParameterABI getABI() const { return ParameterABI(Data & ABIMask); }
3433 ExtParameterInfo withABI(ParameterABI kind) const {
3434 ExtParameterInfo copy = *this;
3435 copy.Data = (copy.Data & ~ABIMask) | unsigned(kind);
3436 return copy;
3437 }
3438
3439 /// Is this parameter considered "consumed" by Objective-C ARC?
3440 /// Consumed parameters must have retainable object type.
3441 bool isConsumed() const { return (Data & IsConsumed); }
3442 ExtParameterInfo withIsConsumed(bool consumed) const {
3443 ExtParameterInfo copy = *this;
3444 if (consumed)
3445 copy.Data |= IsConsumed;
3446 else
3447 copy.Data &= ~IsConsumed;
3448 return copy;
3449 }
3450
3451 bool hasPassObjectSize() const { return Data & HasPassObjSize; }
3452 ExtParameterInfo withHasPassObjectSize() const {
3453 ExtParameterInfo Copy = *this;
3454 Copy.Data |= HasPassObjSize;
3455 return Copy;
3456 }
3457
3458 bool isNoEscape() const { return Data & IsNoEscape; }
3459 ExtParameterInfo withIsNoEscape(bool NoEscape) const {
3460 ExtParameterInfo Copy = *this;
3461 if (NoEscape)
3462 Copy.Data |= IsNoEscape;
3463 else
3464 Copy.Data &= ~IsNoEscape;
3465 return Copy;
3466 }
3467
3468 unsigned char getOpaqueValue() const { return Data; }
3469 static ExtParameterInfo getFromOpaqueValue(unsigned char data) {
3470 ExtParameterInfo result;
3471 result.Data = data;
3472 return result;
3473 }
3474
3475 friend bool operator==(ExtParameterInfo lhs, ExtParameterInfo rhs) {
3476 return lhs.Data == rhs.Data;
3477 }
3478
3479 friend bool operator!=(ExtParameterInfo lhs, ExtParameterInfo rhs) {
3480 return lhs.Data != rhs.Data;
3481 }
3482 };
3483
3484 /// A class which abstracts out some details necessary for
3485 /// making a call.
3486 ///
3487 /// It is not actually used directly for storing this information in
3488 /// a FunctionType, although FunctionType does currently use the
3489 /// same bit-pattern.
3490 ///
3491 // If you add a field (say Foo), other than the obvious places (both,
3492 // constructors, compile failures), what you need to update is
3493 // * Operator==
3494 // * getFoo
3495 // * withFoo
3496 // * functionType. Add Foo, getFoo.
3497 // * ASTContext::getFooType
3498 // * ASTContext::mergeFunctionTypes
3499 // * FunctionNoProtoType::Profile
3500 // * FunctionProtoType::Profile
3501 // * TypePrinter::PrintFunctionProto
3502 // * AST read and write
3503 // * Codegen
3504 class ExtInfo {
3505 friend class FunctionType;
3506
3507 // Feel free to rearrange or add bits, but if you go over 12,
3508 // you'll need to adjust both the Bits field below and
3509 // Type::FunctionTypeBitfields.
3510
3511 // | CC |noreturn|produces|nocallersavedregs|regparm|nocfcheck|
3512 // |0 .. 4| 5 | 6 | 7 |8 .. 10| 11 |
3513 //
3514 // regparm is either 0 (no regparm attribute) or the regparm value+1.
3515 enum { CallConvMask = 0x1F };
3516 enum { NoReturnMask = 0x20 };
3517 enum { ProducesResultMask = 0x40 };
3518 enum { NoCallerSavedRegsMask = 0x80 };
3519 enum { NoCfCheckMask = 0x800 };
3520 enum {
3521 RegParmMask = ~(CallConvMask | NoReturnMask | ProducesResultMask |
3522 NoCallerSavedRegsMask | NoCfCheckMask),
3523 RegParmOffset = 8
3524 }; // Assumed to be the last field
3525 uint16_t Bits = CC_C;
3526
3527 ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {}
3528
3529 public:
3530 // Constructor with no defaults. Use this when you know that you
3531 // have all the elements (when reading an AST file for example).
3532 ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc,
3533 bool producesResult, bool noCallerSavedRegs, bool NoCfCheck) {
3534 assert((!hasRegParm || regParm < 7) && "Invalid regparm value")(((!hasRegParm || regParm < 7) && "Invalid regparm value"
) ? static_cast<void> (0) : __assert_fail ("(!hasRegParm || regParm < 7) && \"Invalid regparm value\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 3534, __PRETTY_FUNCTION__))
;
3535 Bits = ((unsigned)cc) | (noReturn ? NoReturnMask : 0) |
3536 (producesResult ? ProducesResultMask : 0) |
3537 (noCallerSavedRegs ? NoCallerSavedRegsMask : 0) |
3538 (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0) |
3539 (NoCfCheck ? NoCfCheckMask : 0);
3540 }
3541
3542 // Constructor with all defaults. Use when for example creating a
3543 // function known to use defaults.
3544 ExtInfo() = default;
3545
3546 // Constructor with just the calling convention, which is an important part
3547 // of the canonical type.
3548 ExtInfo(CallingConv CC) : Bits(CC) {}
3549
3550 bool getNoReturn() const { return Bits & NoReturnMask; }
3551 bool getProducesResult() const { return Bits & ProducesResultMask; }
3552 bool getNoCallerSavedRegs() const { return Bits & NoCallerSavedRegsMask; }
3553 bool getNoCfCheck() const { return Bits & NoCfCheckMask; }
3554 bool getHasRegParm() const { return (Bits >> RegParmOffset) != 0; }
3555
3556 unsigned getRegParm() const {
3557 unsigned RegParm = (Bits & RegParmMask) >> RegParmOffset;
3558 if (RegParm > 0)
3559 --RegParm;
3560 return RegParm;
3561 }
3562
3563 CallingConv getCC() const { return CallingConv(Bits & CallConvMask); }
3564
3565 bool operator==(ExtInfo Other) const {
3566 return Bits == Other.Bits;
3567 }
3568 bool operator!=(ExtInfo Other) const {
3569 return Bits != Other.Bits;
3570 }
3571
3572 // Note that we don't have setters. That is by design, use
3573 // the following with methods instead of mutating these objects.
3574
3575 ExtInfo withNoReturn(bool noReturn) const {
3576 if (noReturn)
3577 return ExtInfo(Bits | NoReturnMask);
3578 else
3579 return ExtInfo(Bits & ~NoReturnMask);
3580 }
3581
3582 ExtInfo withProducesResult(bool producesResult) const {
3583 if (producesResult)
3584 return ExtInfo(Bits | ProducesResultMask);
3585 else
3586 return ExtInfo(Bits & ~ProducesResultMask);
3587 }
3588
3589 ExtInfo withNoCallerSavedRegs(bool noCallerSavedRegs) const {
3590 if (noCallerSavedRegs)
3591 return ExtInfo(Bits | NoCallerSavedRegsMask);
3592 else
3593 return ExtInfo(Bits & ~NoCallerSavedRegsMask);
3594 }
3595
3596 ExtInfo withNoCfCheck(bool noCfCheck) const {
3597 if (noCfCheck)
3598 return ExtInfo(Bits | NoCfCheckMask);
3599 else
3600 return ExtInfo(Bits & ~NoCfCheckMask);
3601 }
3602
3603 ExtInfo withRegParm(unsigned RegParm) const {
3604 assert(RegParm < 7 && "Invalid regparm value")((RegParm < 7 && "Invalid regparm value") ? static_cast
<void> (0) : __assert_fail ("RegParm < 7 && \"Invalid regparm value\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 3604, __PRETTY_FUNCTION__))
;
3605 return ExtInfo((Bits & ~RegParmMask) |
3606 ((RegParm + 1) << RegParmOffset));
3607 }
3608
3609 ExtInfo withCallingConv(CallingConv cc) const {
3610 return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc);
3611 }
3612
3613 void Profile(llvm::FoldingSetNodeID &ID) const {
3614 ID.AddInteger(Bits);
3615 }
3616 };
3617
3618 /// A simple holder for a QualType representing a type in an
3619 /// exception specification. Unfortunately needed by FunctionProtoType
3620 /// because TrailingObjects cannot handle repeated types.
3621 struct ExceptionType { QualType Type; };
3622
3623 /// A simple holder for various uncommon bits which do not fit in
3624 /// FunctionTypeBitfields. Aligned to alignof(void *) to maintain the
3625 /// alignment of subsequent objects in TrailingObjects. You must update
3626 /// hasExtraBitfields in FunctionProtoType after adding extra data here.
3627 struct alignas(void *) FunctionTypeExtraBitfields {
3628 /// The number of types in the exception specification.
3629 /// A whole unsigned is not needed here and according to
3630 /// [implimits] 8 bits would be enough here.
3631 unsigned NumExceptionType;
3632 };
3633
3634protected:
3635 FunctionType(TypeClass tc, QualType res,
3636 QualType Canonical, bool Dependent,
3637 bool InstantiationDependent,
3638 bool VariablyModified, bool ContainsUnexpandedParameterPack,
3639 ExtInfo Info)
3640 : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified,
3641 ContainsUnexpandedParameterPack),
3642 ResultType(res) {
3643 FunctionTypeBits.ExtInfo = Info.Bits;
3644 }
3645
3646 Qualifiers getFastTypeQuals() const {
3647 return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals);
3648 }
3649
3650public:
3651 QualType getReturnType() const { return ResultType; }
3652
3653 bool getHasRegParm() const { return getExtInfo().getHasRegParm(); }
3654 unsigned getRegParmType() const { return getExtInfo().getRegParm(); }
3655
3656 /// Determine whether this function type includes the GNU noreturn
3657 /// attribute. The C++11 [[noreturn]] attribute does not affect the function
3658 /// type.
3659 bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); }
3660
3661 CallingConv getCallConv() const { return getExtInfo().getCC(); }
3662 ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); }
3663
3664 static_assert((~Qualifiers::FastMask & Qualifiers::CVRMask) == 0,
3665 "Const, volatile and restrict are assumed to be a subset of "
3666 "the fast qualifiers.");
3667
3668 bool isConst() const { return getFastTypeQuals().hasConst(); }
3669 bool isVolatile() const { return getFastTypeQuals().hasVolatile(); }
3670 bool isRestrict() const { return getFastTypeQuals().hasRestrict(); }
3671
3672 /// Determine the type of an expression that calls a function of
3673 /// this type.
3674 QualType getCallResultType(const ASTContext &Context) const {
3675 return getReturnType().getNonLValueExprType(Context);
3676 }
3677
3678 static StringRef getNameForCallConv(CallingConv CC);
3679
3680 static bool classof(const Type *T) {
3681 return T->getTypeClass() == FunctionNoProto ||
3682 T->getTypeClass() == FunctionProto;
3683 }
3684};
3685
3686/// Represents a K&R-style 'int foo()' function, which has
3687/// no information available about its arguments.
3688class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode {
3689 friend class ASTContext; // ASTContext creates these.
3690
3691 FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info)
3692 : FunctionType(FunctionNoProto, Result, Canonical,
3693 /*Dependent=*/false, /*InstantiationDependent=*/false,
3694 Result->isVariablyModifiedType(),
3695 /*ContainsUnexpandedParameterPack=*/false, Info) {}
3696
3697public:
3698 // No additional state past what FunctionType provides.
3699
3700 bool isSugared() const { return false; }
3701 QualType desugar() const { return QualType(this, 0); }
3702
3703 void Profile(llvm::FoldingSetNodeID &ID) {
3704 Profile(ID, getReturnType(), getExtInfo());
3705 }
3706
3707 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType,
3708 ExtInfo Info) {
3709 Info.Profile(ID);
3710 ID.AddPointer(ResultType.getAsOpaquePtr());
3711 }
3712
3713 static bool classof(const Type *T) {
3714 return T->getTypeClass() == FunctionNoProto;
3715 }
3716};
3717
3718/// Represents a prototype with parameter type info, e.g.
3719/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no
3720/// parameters, not as having a single void parameter. Such a type can have
3721/// an exception specification, but this specification is not part of the
3722/// canonical type. FunctionProtoType has several trailing objects, some of
3723/// which optional. For more information about the trailing objects see
3724/// the first comment inside FunctionProtoType.
3725class FunctionProtoType final
3726 : public FunctionType,
3727 public llvm::FoldingSetNode,
3728 private llvm::TrailingObjects<
3729 FunctionProtoType, QualType, FunctionType::FunctionTypeExtraBitfields,
3730 FunctionType::ExceptionType, Expr *, FunctionDecl *,
3731 FunctionType::ExtParameterInfo, Qualifiers> {
3732 friend class ASTContext; // ASTContext creates these.
3733 friend TrailingObjects;
3734
3735 // FunctionProtoType is followed by several trailing objects, some of
3736 // which optional. They are in order:
3737 //
3738 // * An array of getNumParams() QualType holding the parameter types.
3739 // Always present. Note that for the vast majority of FunctionProtoType,
3740 // these will be the only trailing objects.
3741 //
3742 // * Optionally if some extra data is stored in FunctionTypeExtraBitfields
3743 // (see FunctionTypeExtraBitfields and FunctionTypeBitfields):
3744 // a single FunctionTypeExtraBitfields. Present if and only if
3745 // hasExtraBitfields() is true.
3746 //
3747 // * Optionally exactly one of:
3748 // * an array of getNumExceptions() ExceptionType,
3749 // * a single Expr *,
3750 // * a pair of FunctionDecl *,
3751 // * a single FunctionDecl *
3752 // used to store information about the various types of exception
3753 // specification. See getExceptionSpecSize for the details.
3754 //
3755 // * Optionally an array of getNumParams() ExtParameterInfo holding
3756 // an ExtParameterInfo for each of the parameters. Present if and
3757 // only if hasExtParameterInfos() is true.
3758 //
3759 // * Optionally a Qualifiers object to represent extra qualifiers that can't
3760 // be represented by FunctionTypeBitfields.FastTypeQuals. Present if and only
3761 // if hasExtQualifiers() is true.
3762 //
3763 // The optional FunctionTypeExtraBitfields has to be before the data
3764 // related to the exception specification since it contains the number
3765 // of exception types.
3766 //
3767 // We put the ExtParameterInfos last. If all were equal, it would make
3768 // more sense to put these before the exception specification, because
3769 // it's much easier to skip past them compared to the elaborate switch
3770 // required to skip the exception specification. However, all is not
3771 // equal; ExtParameterInfos are used to model very uncommon features,
3772 // and it's better not to burden the more common paths.
3773
3774public:
3775 /// Holds information about the various types of exception specification.
3776 /// ExceptionSpecInfo is not stored as such in FunctionProtoType but is
3777 /// used to group together the various bits of information about the
3778 /// exception specification.
3779 struct ExceptionSpecInfo {
3780 /// The kind of exception specification this is.
3781 ExceptionSpecificationType Type = EST_None;
3782
3783 /// Explicitly-specified list of exception types.
3784 ArrayRef<QualType> Exceptions;
3785
3786 /// Noexcept expression, if this is a computed noexcept specification.
3787 Expr *NoexceptExpr = nullptr;
3788
3789 /// The function whose exception specification this is, for
3790 /// EST_Unevaluated and EST_Uninstantiated.
3791 FunctionDecl *SourceDecl = nullptr;
3792
3793 /// The function template whose exception specification this is instantiated
3794 /// from, for EST_Uninstantiated.
3795 FunctionDecl *SourceTemplate = nullptr;
3796
3797 ExceptionSpecInfo() = default;
3798
3799 ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {}
3800 };
3801
3802 /// Extra information about a function prototype. ExtProtoInfo is not
3803 /// stored as such in FunctionProtoType but is used to group together
3804 /// the various bits of extra information about a function prototype.
3805 struct ExtProtoInfo {
3806 FunctionType::ExtInfo ExtInfo;
3807 bool Variadic : 1;
3808 bool HasTrailingReturn : 1;
3809 Qualifiers TypeQuals;
3810 RefQualifierKind RefQualifier = RQ_None;
3811 ExceptionSpecInfo ExceptionSpec;
3812 const ExtParameterInfo *ExtParameterInfos = nullptr;
3813
3814 ExtProtoInfo() : Variadic(false), HasTrailingReturn(false) {}
3815
3816 ExtProtoInfo(CallingConv CC)
3817 : ExtInfo(CC), Variadic(false), HasTrailingReturn(false) {}
3818
3819 ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI) {
3820 ExtProtoInfo Result(*this);
3821 Result.ExceptionSpec = ESI;
3822 return Result;
3823 }
3824 };
3825
3826private:
3827 unsigned numTrailingObjects(OverloadToken<QualType>) const {
3828 return getNumParams();
3829 }
3830
3831 unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const {
3832 return hasExtraBitfields();
3833 }
3834
3835 unsigned numTrailingObjects(OverloadToken<ExceptionType>) const {
3836 return getExceptionSpecSize().NumExceptionType;
3837 }
3838
3839 unsigned numTrailingObjects(OverloadToken<Expr *>) const {
3840 return getExceptionSpecSize().NumExprPtr;
3841 }
3842
3843 unsigned numTrailingObjects(OverloadToken<FunctionDecl *>) const {
3844 return getExceptionSpecSize().NumFunctionDeclPtr;
3845 }
3846
3847 unsigned numTrailingObjects(OverloadToken<ExtParameterInfo>) const {
3848 return hasExtParameterInfos() ? getNumParams() : 0;
3849 }
3850
3851 /// Determine whether there are any argument types that
3852 /// contain an unexpanded parameter pack.
3853 static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray,
3854 unsigned numArgs) {
3855 for (unsigned Idx = 0; Idx < numArgs; ++Idx)
3856 if (ArgArray[Idx]->containsUnexpandedParameterPack())
3857 return true;
3858
3859 return false;
3860 }
3861
3862 FunctionProtoType(QualType result, ArrayRef<QualType> params,
3863 QualType canonical, const ExtProtoInfo &epi);
3864
3865 /// This struct is returned by getExceptionSpecSize and is used to
3866 /// translate an ExceptionSpecificationType to the number and kind
3867 /// of trailing objects related to the exception specification.
3868 struct ExceptionSpecSizeHolder {
3869 unsigned NumExceptionType;
3870 unsigned NumExprPtr;
3871 unsigned NumFunctionDeclPtr;
3872 };
3873
3874 /// Return the number and kind of trailing objects
3875 /// related to the exception specification.
3876 static ExceptionSpecSizeHolder
3877 getExceptionSpecSize(ExceptionSpecificationType EST, unsigned NumExceptions) {
3878 switch (EST) {
3879 case EST_None:
3880 case EST_DynamicNone:
3881 case EST_MSAny:
3882 case EST_BasicNoexcept:
3883 case EST_Unparsed:
3884 case EST_NoThrow:
3885 return {0, 0, 0};
3886
3887 case EST_Dynamic:
3888 return {NumExceptions, 0, 0};
3889
3890 case EST_DependentNoexcept:
3891 case EST_NoexceptFalse:
3892 case EST_NoexceptTrue:
3893 return {0, 1, 0};
3894
3895 case EST_Uninstantiated:
3896 return {0, 0, 2};
3897
3898 case EST_Unevaluated:
3899 return {0, 0, 1};
3900 }
3901 llvm_unreachable("bad exception specification kind")::llvm::llvm_unreachable_internal("bad exception specification kind"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 3901)
;
3902 }
3903
3904 /// Return the number and kind of trailing objects
3905 /// related to the exception specification.
3906 ExceptionSpecSizeHolder getExceptionSpecSize() const {
3907 return getExceptionSpecSize(getExceptionSpecType(), getNumExceptions());
3908 }
3909
3910 /// Whether the trailing FunctionTypeExtraBitfields is present.
3911 static bool hasExtraBitfields(ExceptionSpecificationType EST) {
3912 // If the exception spec type is EST_Dynamic then we have > 0 exception
3913 // types and the exact number is stored in FunctionTypeExtraBitfields.
3914 return EST == EST_Dynamic;
3915 }
3916
3917 /// Whether the trailing FunctionTypeExtraBitfields is present.
3918 bool hasExtraBitfields() const {
3919 return hasExtraBitfields(getExceptionSpecType());
3920 }
3921
3922 bool hasExtQualifiers() const {
3923 return FunctionTypeBits.HasExtQuals;
3924 }
3925
3926public:
3927 unsigned getNumParams() const { return FunctionTypeBits.NumParams; }
3928
3929 QualType getParamType(unsigned i) const {
3930 assert(i < getNumParams() && "invalid parameter index")((i < getNumParams() && "invalid parameter index")
? static_cast<void> (0) : __assert_fail ("i < getNumParams() && \"invalid parameter index\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 3930, __PRETTY_FUNCTION__))
;
3931 return param_type_begin()[i];
3932 }
3933
3934 ArrayRef<QualType> getParamTypes() const {
3935 return llvm::makeArrayRef(param_type_begin(), param_type_end());
3936 }
3937
3938 ExtProtoInfo getExtProtoInfo() const {
3939 ExtProtoInfo EPI;
3940 EPI.ExtInfo = getExtInfo();
3941 EPI.Variadic = isVariadic();
3942 EPI.HasTrailingReturn = hasTrailingReturn();
3943 EPI.ExceptionSpec.Type = getExceptionSpecType();
3944 EPI.TypeQuals = getMethodQuals();
3945 EPI.RefQualifier = getRefQualifier();
3946 if (EPI.ExceptionSpec.Type == EST_Dynamic) {
3947 EPI.ExceptionSpec.Exceptions = exceptions();
3948 } else if (isComputedNoexcept(EPI.ExceptionSpec.Type)) {
3949 EPI.ExceptionSpec.NoexceptExpr = getNoexceptExpr();
3950 } else if (EPI.ExceptionSpec.Type == EST_Uninstantiated) {
3951 EPI.ExceptionSpec.SourceDecl = getExceptionSpecDecl();
3952 EPI.ExceptionSpec.SourceTemplate = getExceptionSpecTemplate();
3953 } else if (EPI.ExceptionSpec.Type == EST_Unevaluated) {
3954 EPI.ExceptionSpec.SourceDecl = getExceptionSpecDecl();
3955 }
3956 EPI.ExtParameterInfos = getExtParameterInfosOrNull();
3957 return EPI;
3958 }
3959
3960 /// Get the kind of exception specification on this function.
3961 ExceptionSpecificationType getExceptionSpecType() const {
3962 return static_cast<ExceptionSpecificationType>(
3963 FunctionTypeBits.ExceptionSpecType);
3964 }
3965
3966 /// Return whether this function has any kind of exception spec.
3967 bool hasExceptionSpec() const { return getExceptionSpecType() != EST_None; }
3968
3969 /// Return whether this function has a dynamic (throw) exception spec.
3970 bool hasDynamicExceptionSpec() const {
3971 return isDynamicExceptionSpec(getExceptionSpecType());
3972 }
3973
3974 /// Return whether this function has a noexcept exception spec.
3975 bool hasNoexceptExceptionSpec() const {
3976 return isNoexceptExceptionSpec(getExceptionSpecType());
3977 }
3978
3979 /// Return whether this function has a dependent exception spec.
3980 bool hasDependentExceptionSpec() const;
3981
3982 /// Return whether this function has an instantiation-dependent exception
3983 /// spec.
3984 bool hasInstantiationDependentExceptionSpec() const;
3985
3986 /// Return the number of types in the exception specification.
3987 unsigned getNumExceptions() const {
3988 return getExceptionSpecType() == EST_Dynamic
3989 ? getTrailingObjects<FunctionTypeExtraBitfields>()
3990 ->NumExceptionType
3991 : 0;
3992 }
3993
3994 /// Return the ith exception type, where 0 <= i < getNumExceptions().
3995 QualType getExceptionType(unsigned i) const {
3996 assert(i < getNumExceptions() && "Invalid exception number!")((i < getNumExceptions() && "Invalid exception number!"
) ? static_cast<void> (0) : __assert_fail ("i < getNumExceptions() && \"Invalid exception number!\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 3996, __PRETTY_FUNCTION__))
;
3997 return exception_begin()[i];
3998 }
3999
4000 /// Return the expression inside noexcept(expression), or a null pointer
4001 /// if there is none (because the exception spec is not of this form).
4002 Expr *getNoexceptExpr() const {
4003 if (!isComputedNoexcept(getExceptionSpecType()))
4004 return nullptr;
4005 return *getTrailingObjects<Expr *>();
4006 }
4007
4008 /// If this function type has an exception specification which hasn't
4009 /// been determined yet (either because it has not been evaluated or because
4010 /// it has not been instantiated), this is the function whose exception
4011 /// specification is represented by this type.
4012 FunctionDecl *getExceptionSpecDecl() const {
4013 if (getExceptionSpecType() != EST_Uninstantiated &&
4014 getExceptionSpecType() != EST_Unevaluated)
4015 return nullptr;
4016 return getTrailingObjects<FunctionDecl *>()[0];
4017 }
4018
4019 /// If this function type has an uninstantiated exception
4020 /// specification, this is the function whose exception specification
4021 /// should be instantiated to find the exception specification for
4022 /// this type.
4023 FunctionDecl *getExceptionSpecTemplate() const {
4024 if (getExceptionSpecType() != EST_Uninstantiated)
4025 return nullptr;
4026 return getTrailingObjects<FunctionDecl *>()[1];
4027 }
4028
4029 /// Determine whether this function type has a non-throwing exception
4030 /// specification.
4031 CanThrowResult canThrow() const;
4032
4033 /// Determine whether this function type has a non-throwing exception
4034 /// specification. If this depends on template arguments, returns
4035 /// \c ResultIfDependent.
4036 bool isNothrow(bool ResultIfDependent = false) const {
4037 return ResultIfDependent ? canThrow() != CT_Can : canThrow() == CT_Cannot;
4038 }
4039
4040 /// Whether this function prototype is variadic.
4041 bool isVariadic() const { return FunctionTypeBits.Variadic; }
4042
4043 /// Determines whether this function prototype contains a
4044 /// parameter pack at the end.
4045 ///
4046 /// A function template whose last parameter is a parameter pack can be
4047 /// called with an arbitrary number of arguments, much like a variadic
4048 /// function.
4049 bool isTemplateVariadic() const;
4050
4051 /// Whether this function prototype has a trailing return type.
4052 bool hasTrailingReturn() const { return FunctionTypeBits.HasTrailingReturn; }
4053
4054 Qualifiers getMethodQuals() const {
4055 if (hasExtQualifiers())
4056 return *getTrailingObjects<Qualifiers>();
4057 else
4058 return getFastTypeQuals();
4059 }
4060
4061 /// Retrieve the ref-qualifier associated with this function type.
4062 RefQualifierKind getRefQualifier() const {
4063 return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier);
4064 }
4065
4066 using param_type_iterator = const QualType *;
4067 using param_type_range = llvm::iterator_range<param_type_iterator>;
4068
4069 param_type_range param_types() const {
4070 return param_type_range(param_type_begin(), param_type_end());
4071 }
4072
4073 param_type_iterator param_type_begin() const {
4074 return getTrailingObjects<QualType>();
4075 }
4076
4077 param_type_iterator param_type_end() const {
4078 return param_type_begin() + getNumParams();
4079 }
4080
4081 using exception_iterator = const QualType *;
4082
4083 ArrayRef<QualType> exceptions() const {
4084 return llvm::makeArrayRef(exception_begin(), exception_end());
4085 }
4086
4087 exception_iterator exception_begin() const {
4088 return reinterpret_cast<exception_iterator>(
4089 getTrailingObjects<ExceptionType>());
4090 }
4091
4092 exception_iterator exception_end() const {
4093 return exception_begin() + getNumExceptions();
4094 }
4095
4096 /// Is there any interesting extra information for any of the parameters
4097 /// of this function type?
4098 bool hasExtParameterInfos() const {
4099 return FunctionTypeBits.HasExtParameterInfos;
4100 }
4101
4102 ArrayRef<ExtParameterInfo> getExtParameterInfos() const {
4103 assert(hasExtParameterInfos())((hasExtParameterInfos()) ? static_cast<void> (0) : __assert_fail
("hasExtParameterInfos()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 4103, __PRETTY_FUNCTION__))
;
4104 return ArrayRef<ExtParameterInfo>(getTrailingObjects<ExtParameterInfo>(),
4105 getNumParams());
4106 }
4107
4108 /// Return a pointer to the beginning of the array of extra parameter
4109 /// information, if present, or else null if none of the parameters
4110 /// carry it. This is equivalent to getExtProtoInfo().ExtParameterInfos.
4111 const ExtParameterInfo *getExtParameterInfosOrNull() const {
4112 if (!hasExtParameterInfos())
4113 return nullptr;
4114 return getTrailingObjects<ExtParameterInfo>();
4115 }
4116
4117 ExtParameterInfo getExtParameterInfo(unsigned I) const {
4118 assert(I < getNumParams() && "parameter index out of range")((I < getNumParams() && "parameter index out of range"
) ? static_cast<void> (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 4118, __PRETTY_FUNCTION__))
;
4119 if (hasExtParameterInfos())
4120 return getTrailingObjects<ExtParameterInfo>()[I];
4121 return ExtParameterInfo();
4122 }
4123
4124 ParameterABI getParameterABI(unsigned I) const {
4125 assert(I < getNumParams() && "parameter index out of range")((I < getNumParams() && "parameter index out of range"
) ? static_cast<void> (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 4125, __PRETTY_FUNCTION__))
;
4126 if (hasExtParameterInfos())
4127 return getTrailingObjects<ExtParameterInfo>()[I].getABI();
4128 return ParameterABI::Ordinary;
4129 }
4130
4131 bool isParamConsumed(unsigned I) const {
4132 assert(I < getNumParams() && "parameter index out of range")((I < getNumParams() && "parameter index out of range"
) ? static_cast<void> (0) : __assert_fail ("I < getNumParams() && \"parameter index out of range\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 4132, __PRETTY_FUNCTION__))
;
4133 if (hasExtParameterInfos())
4134 return getTrailingObjects<ExtParameterInfo>()[I].isConsumed();
4135 return false;
4136 }
4137
4138 bool isSugared() const { return false; }
4139 QualType desugar() const { return QualType(this, 0); }
4140
4141 void printExceptionSpecification(raw_ostream &OS,
4142 const PrintingPolicy &Policy) const;
4143
4144 static bool classof(const Type *T) {
4145 return T->getTypeClass() == FunctionProto;
4146 }
4147
4148 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx);
4149 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
4150 param_type_iterator ArgTys, unsigned NumArgs,
4151 const ExtProtoInfo &EPI, const ASTContext &Context,
4152 bool Canonical);
4153};
4154
4155/// Represents the dependent type named by a dependently-scoped
4156/// typename using declaration, e.g.
4157/// using typename Base<T>::foo;
4158///
4159/// Template instantiation turns these into the underlying type.
4160class UnresolvedUsingType : public Type {
4161 friend class ASTContext; // ASTContext creates these.
4162
4163 UnresolvedUsingTypenameDecl *Decl;
4164
4165 UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D)
4166 : Type(UnresolvedUsing, QualType(), true, true, false,
4167 /*ContainsUnexpandedParameterPack=*/false),
4168 Decl(const_cast<UnresolvedUsingTypenameDecl*>(D)) {}
4169
4170public:
4171 UnresolvedUsingTypenameDecl *getDecl() const { return Decl; }
4172
4173 bool isSugared() const { return false; }
4174 QualType desugar() const { return QualType(this, 0); }
4175
4176 static bool classof(const Type *T) {
4177 return T->getTypeClass() == UnresolvedUsing;
4178 }
4179
4180 void Profile(llvm::FoldingSetNodeID &ID) {
4181 return Profile(ID, Decl);
4182 }
4183
4184 static void Profile(llvm::FoldingSetNodeID &ID,
4185 UnresolvedUsingTypenameDecl *D) {
4186 ID.AddPointer(D);
4187 }
4188};
4189
4190class TypedefType : public Type {
4191 TypedefNameDecl *Decl;
4192
4193protected:
4194 friend class ASTContext; // ASTContext creates these.
4195
4196 TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can)
4197 : Type(tc, can, can->isDependentType(),
4198 can->isInstantiationDependentType(),
4199 can->isVariablyModifiedType(),
4200 /*ContainsUnexpandedParameterPack=*/false),
4201 Decl(const_cast<TypedefNameDecl*>(D)) {
4202 assert(!isa<TypedefType>(can) && "Invalid canonical type")((!isa<TypedefType>(can) && "Invalid canonical type"
) ? static_cast<void> (0) : __assert_fail ("!isa<TypedefType>(can) && \"Invalid canonical type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 4202, __PRETTY_FUNCTION__))
;
4203 }
4204
4205public:
4206 TypedefNameDecl *getDecl() const { return Decl; }
4207
4208 bool isSugared() const { return true; }
4209 QualType desugar() const;
4210
4211 static bool classof(const Type *T) { return T->getTypeClass() == Typedef; }
4212};
4213
4214/// Sugar type that represents a type that was qualified by a qualifier written
4215/// as a macro invocation.
4216class MacroQualifiedType : public Type {
4217 friend class ASTContext; // ASTContext creates these.
4218
4219 QualType UnderlyingTy;
4220 const IdentifierInfo *MacroII;
4221
4222 MacroQualifiedType(QualType UnderlyingTy, QualType CanonTy,
4223 const IdentifierInfo *MacroII)
4224 : Type(MacroQualified, CanonTy, UnderlyingTy->isDependentType(),
4225 UnderlyingTy->isInstantiationDependentType(),
4226 UnderlyingTy->isVariablyModifiedType(),
4227 UnderlyingTy->containsUnexpandedParameterPack()),
4228 UnderlyingTy(UnderlyingTy), MacroII(MacroII) {
4229 assert(isa<AttributedType>(UnderlyingTy) &&((isa<AttributedType>(UnderlyingTy) && "Expected a macro qualified type to only wrap attributed types."
) ? static_cast<void> (0) : __assert_fail ("isa<AttributedType>(UnderlyingTy) && \"Expected a macro qualified type to only wrap attributed types.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 4230, __PRETTY_FUNCTION__))
4230 "Expected a macro qualified type to only wrap attributed types.")((isa<AttributedType>(UnderlyingTy) && "Expected a macro qualified type to only wrap attributed types."
) ? static_cast<void> (0) : __assert_fail ("isa<AttributedType>(UnderlyingTy) && \"Expected a macro qualified type to only wrap attributed types.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 4230, __PRETTY_FUNCTION__))
;
4231 }
4232
4233public:
4234 const IdentifierInfo *getMacroIdentifier() const { return MacroII; }
4235 QualType getUnderlyingType() const { return UnderlyingTy; }
4236
4237 /// Return this attributed type's modified type with no qualifiers attached to
4238 /// it.
4239 QualType getModifiedType() const;
4240
4241 bool isSugared() const { return true; }
4242 QualType desugar() const;
4243
4244 static bool classof(const Type *T) {
4245 return T->getTypeClass() == MacroQualified;
4246 }
4247};
4248
4249/// Represents a `typeof` (or __typeof__) expression (a GCC extension).
4250class TypeOfExprType : public Type {
4251 Expr *TOExpr;
4252
4253protected:
4254 friend class ASTContext; // ASTContext creates these.
4255
4256 TypeOfExprType(Expr *E, QualType can = QualType());
4257
4258public:
4259 Expr *getUnderlyingExpr() const { return TOExpr; }
4260
4261 /// Remove a single level of sugar.
4262 QualType desugar() const;
4263
4264 /// Returns whether this type directly provides sugar.
4265 bool isSugared() const;
4266
4267 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; }
4268};
4269
4270/// Internal representation of canonical, dependent
4271/// `typeof(expr)` types.
4272///
4273/// This class is used internally by the ASTContext to manage
4274/// canonical, dependent types, only. Clients will only see instances
4275/// of this class via TypeOfExprType nodes.
4276class DependentTypeOfExprType
4277 : public TypeOfExprType, public llvm::FoldingSetNode {
4278 const ASTContext &Context;
4279
4280public:
4281 DependentTypeOfExprType(const ASTContext &Context, Expr *E)
4282 : TypeOfExprType(E), Context(Context) {}
4283
4284 void Profile(llvm::FoldingSetNodeID &ID) {
4285 Profile(ID, Context, getUnderlyingExpr());
4286 }
4287
4288 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
4289 Expr *E);
4290};
4291
4292/// Represents `typeof(type)`, a GCC extension.
4293class TypeOfType : public Type {
4294 friend class ASTContext; // ASTContext creates these.
4295
4296 QualType TOType;
4297
4298 TypeOfType(QualType T, QualType can)
4299 : Type(TypeOf, can, T->isDependentType(),
4300 T->isInstantiationDependentType(),
4301 T->isVariablyModifiedType(),
4302 T->containsUnexpandedParameterPack()),
4303 TOType(T) {
4304 assert(!isa<TypedefType>(can) && "Invalid canonical type")((!isa<TypedefType>(can) && "Invalid canonical type"
) ? static_cast<void> (0) : __assert_fail ("!isa<TypedefType>(can) && \"Invalid canonical type\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 4304, __PRETTY_FUNCTION__))
;
4305 }
4306
4307public:
4308 QualType getUnderlyingType() const { return TOType; }
4309
4310 /// Remove a single level of sugar.
4311 QualType desugar() const { return getUnderlyingType(); }
4312
4313 /// Returns whether this type directly provides sugar.
4314 bool isSugared() const { return true; }
4315
4316 static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; }
4317};
4318
4319/// Represents the type `decltype(expr)` (C++11).
4320class DecltypeType : public Type {
4321 Expr *E;
4322 QualType UnderlyingType;
4323
4324protected:
4325 friend class ASTContext; // ASTContext creates these.
4326
4327 DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType());
4328
4329public:
4330 Expr *getUnderlyingExpr() const { return E; }
4331 QualType getUnderlyingType() const { return UnderlyingType; }
4332
4333 /// Remove a single level of sugar.
4334 QualType desugar() const;
4335
4336 /// Returns whether this type directly provides sugar.
4337 bool isSugared() const;
4338
4339 static bool classof(const Type *T) { return T->getTypeClass() == Decltype; }
4340};
4341
4342/// Internal representation of canonical, dependent
4343/// decltype(expr) types.
4344///
4345/// This class is used internally by the ASTContext to manage
4346/// canonical, dependent types, only. Clients will only see instances
4347/// of this class via DecltypeType nodes.
4348class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode {
4349 const ASTContext &Context;
4350
4351public:
4352 DependentDecltypeType(const ASTContext &Context, Expr *E);
4353
4354 void Profile(llvm::FoldingSetNodeID &ID) {
4355 Profile(ID, Context, getUnderlyingExpr());
4356 }
4357
4358 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
4359 Expr *E);
4360};
4361
4362/// A unary type transform, which is a type constructed from another.
4363class UnaryTransformType : public Type {
4364public:
4365 enum UTTKind {
4366 EnumUnderlyingType
4367 };
4368
4369private:
4370 /// The untransformed type.
4371 QualType BaseType;
4372
4373 /// The transformed type if not dependent, otherwise the same as BaseType.
4374 QualType UnderlyingType;
4375
4376 UTTKind UKind;
4377
4378protected:
4379 friend class ASTContext;
4380
4381 UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind,
4382 QualType CanonicalTy);
4383
4384public:
4385 bool isSugared() const { return !isDependentType(); }
4386 QualType desugar() const { return UnderlyingType; }
4387
4388 QualType getUnderlyingType() const { return UnderlyingType; }
4389 QualType getBaseType() const { return BaseType; }
4390
4391 UTTKind getUTTKind() const { return UKind; }
4392
4393 static bool classof(const Type *T) {
4394 return T->getTypeClass() == UnaryTransform;
4395 }
4396};
4397
4398/// Internal representation of canonical, dependent
4399/// __underlying_type(type) types.
4400///
4401/// This class is used internally by the ASTContext to manage
4402/// canonical, dependent types, only. Clients will only see instances
4403/// of this class via UnaryTransformType nodes.
4404class DependentUnaryTransformType : public UnaryTransformType,
4405 public llvm::FoldingSetNode {
4406public:
4407 DependentUnaryTransformType(const ASTContext &C, QualType BaseType,
4408 UTTKind UKind);
4409
4410 void Profile(llvm::FoldingSetNodeID &ID) {
4411 Profile(ID, getBaseType(), getUTTKind());
4412 }
4413
4414 static void Profile(llvm::FoldingSetNodeID &ID, QualType BaseType,
4415 UTTKind UKind) {
4416 ID.AddPointer(BaseType.getAsOpaquePtr());
4417 ID.AddInteger((unsigned)UKind);
4418 }
4419};
4420
4421class TagType : public Type {
4422 friend class ASTReader;
4423
4424 /// Stores the TagDecl associated with this type. The decl may point to any
4425 /// TagDecl that declares the entity.
4426 TagDecl *decl;
4427
4428protected:
4429 TagType(TypeClass TC, const TagDecl *D, QualType can);
4430
4431public:
4432 TagDecl *getDecl() const;
4433
4434 /// Determines whether this type is in the process of being defined.
4435 bool isBeingDefined() const;
4436
4437 static bool classof(const Type *T) {
4438 return T->getTypeClass() == Enum || T->getTypeClass() == Record;
4439 }
4440};
4441
4442/// A helper class that allows the use of isa/cast/dyncast
4443/// to detect TagType objects of structs/unions/classes.
4444class RecordType : public TagType {
4445protected:
4446 friend class ASTContext; // ASTContext creates these.
4447
4448 explicit RecordType(const RecordDecl *D)
4449 : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) {}
4450 explicit RecordType(TypeClass TC, RecordDecl *D)
4451 : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) {}
4452
4453public:
4454 RecordDecl *getDecl() const {
4455 return reinterpret_cast<RecordDecl*>(TagType::getDecl());
4456 }
4457
4458 /// Recursively check all fields in the record for const-ness. If any field
4459 /// is declared const, return true. Otherwise, return false.
4460 bool hasConstFields() const;
4461
4462 bool isSugared() const { return false; }
4463 QualType desugar() const { return QualType(this, 0); }
4464
4465 static bool classof(const Type *T) { return T->getTypeClass() == Record; }
4466};
4467
4468/// A helper class that allows the use of isa/cast/dyncast
4469/// to detect TagType objects of enums.
4470class EnumType : public TagType {
4471 friend class ASTContext; // ASTContext creates these.
4472
4473 explicit EnumType(const EnumDecl *D)
4474 : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) {}
4475
4476public:
4477 EnumDecl *getDecl() const {
4478 return reinterpret_cast<EnumDecl*>(TagType::getDecl());
4479 }
4480
4481 bool isSugared() const { return false; }
4482 QualType desugar() const { return QualType(this, 0); }
4483
4484 static bool classof(const Type *T) { return T->getTypeClass() == Enum; }
4485};
4486
4487/// An attributed type is a type to which a type attribute has been applied.
4488///
4489/// The "modified type" is the fully-sugared type to which the attributed
4490/// type was applied; generally it is not canonically equivalent to the
4491/// attributed type. The "equivalent type" is the minimally-desugared type
4492/// which the type is canonically equivalent to.
4493///
4494/// For example, in the following attributed type:
4495/// int32_t __attribute__((vector_size(16)))
4496/// - the modified type is the TypedefType for int32_t
4497/// - the equivalent type is VectorType(16, int32_t)
4498/// - the canonical type is VectorType(16, int)
4499class AttributedType : public Type, public llvm::FoldingSetNode {
4500public:
4501 using Kind = attr::Kind;
4502
4503private:
4504 friend class ASTContext; // ASTContext creates these
4505
4506 QualType ModifiedType;
4507 QualType EquivalentType;
4508
4509 AttributedType(QualType canon, attr::Kind attrKind, QualType modified,
4510 QualType equivalent)
4511 : Type(Attributed, canon, equivalent->isDependentType(),
4512 equivalent->isInstantiationDependentType(),
4513 equivalent->isVariablyModifiedType(),
4514 equivalent->containsUnexpandedParameterPack()),
4515 ModifiedType(modified), EquivalentType(equivalent) {
4516 AttributedTypeBits.AttrKind = attrKind;
4517 }
4518
4519public:
4520 Kind getAttrKind() const {
4521 return static_cast<Kind>(AttributedTypeBits.AttrKind);
4522 }
4523
4524 QualType getModifiedType() const { return ModifiedType; }
4525 QualType getEquivalentType() const { return EquivalentType; }
4526
4527 bool isSugared() const { return true; }
4528 QualType desugar() const { return getEquivalentType(); }
4529
4530 /// Does this attribute behave like a type qualifier?
4531 ///
4532 /// A type qualifier adjusts a type to provide specialized rules for
4533 /// a specific object, like the standard const and volatile qualifiers.
4534 /// This includes attributes controlling things like nullability,
4535 /// address spaces, and ARC ownership. The value of the object is still
4536 /// largely described by the modified type.
4537 ///
4538 /// In contrast, many type attributes "rewrite" their modified type to
4539 /// produce a fundamentally different type, not necessarily related in any
4540 /// formalizable way to the original type. For example, calling convention
4541 /// and vector attributes are not simple type qualifiers.
4542 ///
4543 /// Type qualifiers are often, but not always, reflected in the canonical
4544 /// type.
4545 bool isQualifier() const;
4546
4547 bool isMSTypeSpec() const;
4548
4549 bool isCallingConv() const;
4550
4551 llvm::Optional<NullabilityKind> getImmediateNullability() const;
4552
4553 /// Retrieve the attribute kind corresponding to the given
4554 /// nullability kind.
4555 static Kind getNullabilityAttrKind(NullabilityKind kind) {
4556 switch (kind) {
4557 case NullabilityKind::NonNull:
4558 return attr::TypeNonNull;
4559
4560 case NullabilityKind::Nullable:
4561 return attr::TypeNullable;
4562
4563 case NullabilityKind::Unspecified:
4564 return attr::TypeNullUnspecified;
4565 }
4566 llvm_unreachable("Unknown nullability kind.")::llvm::llvm_unreachable_internal("Unknown nullability kind."
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 4566)
;
4567 }
4568
4569 /// Strip off the top-level nullability annotation on the given
4570 /// type, if it's there.
4571 ///
4572 /// \param T The type to strip. If the type is exactly an
4573 /// AttributedType specifying nullability (without looking through
4574 /// type sugar), the nullability is returned and this type changed
4575 /// to the underlying modified type.
4576 ///
4577 /// \returns the top-level nullability, if present.
4578 static Optional<NullabilityKind> stripOuterNullability(QualType &T);
4579
4580 void Profile(llvm::FoldingSetNodeID &ID) {
4581 Profile(ID, getAttrKind(), ModifiedType, EquivalentType);
4582 }
4583
4584 static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind,
4585 QualType modified, QualType equivalent) {
4586 ID.AddInteger(attrKind);
4587 ID.AddPointer(modified.getAsOpaquePtr());
4588 ID.AddPointer(equivalent.getAsOpaquePtr());
4589 }
4590
4591 static bool classof(const Type *T) {
4592 return T->getTypeClass() == Attributed;
4593 }
4594};
4595
4596class TemplateTypeParmType : public Type, public llvm::FoldingSetNode {
4597 friend class ASTContext; // ASTContext creates these
4598
4599 // Helper data collector for canonical types.
4600 struct CanonicalTTPTInfo {
4601 unsigned Depth : 15;
4602 unsigned ParameterPack : 1;
4603 unsigned Index : 16;
4604 };
4605
4606 union {
4607 // Info for the canonical type.
4608 CanonicalTTPTInfo CanTTPTInfo;
4609
4610 // Info for the non-canonical type.
4611 TemplateTypeParmDecl *TTPDecl;
4612 };
4613
4614 /// Build a non-canonical type.
4615 TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon)
4616 : Type(TemplateTypeParm, Canon, /*Dependent=*/true,
4617 /*InstantiationDependent=*/true,
4618 /*VariablyModified=*/false,
4619 Canon->containsUnexpandedParameterPack()),
4620 TTPDecl(TTPDecl) {}
4621
4622 /// Build the canonical type.
4623 TemplateTypeParmType(unsigned D, unsigned I, bool PP)
4624 : Type(TemplateTypeParm, QualType(this, 0),
4625 /*Dependent=*/true,
4626 /*InstantiationDependent=*/true,
4627 /*VariablyModified=*/false, PP) {
4628 CanTTPTInfo.Depth = D;
4629 CanTTPTInfo.Index = I;
4630 CanTTPTInfo.ParameterPack = PP;
4631 }
4632
4633 const CanonicalTTPTInfo& getCanTTPTInfo() const {
4634 QualType Can = getCanonicalTypeInternal();
4635 return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo;
4636 }
4637
4638public:
4639 unsigned getDepth() const { return getCanTTPTInfo().Depth; }
4640 unsigned getIndex() const { return getCanTTPTInfo().Index; }
4641 bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; }
4642
4643 TemplateTypeParmDecl *getDecl() const {
4644 return isCanonicalUnqualified() ? nullptr : TTPDecl;
4645 }
4646
4647 IdentifierInfo *getIdentifier() const;
4648
4649 bool isSugared() const { return false; }
4650 QualType desugar() const { return QualType(this, 0); }
4651
4652 void Profile(llvm::FoldingSetNodeID &ID) {
4653 Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl());
4654 }
4655
4656 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth,
4657 unsigned Index, bool ParameterPack,
4658 TemplateTypeParmDecl *TTPDecl) {
4659 ID.AddInteger(Depth);
4660 ID.AddInteger(Index);
4661 ID.AddBoolean(ParameterPack);
4662 ID.AddPointer(TTPDecl);
4663 }
4664
4665 static bool classof(const Type *T) {
4666 return T->getTypeClass() == TemplateTypeParm;
4667 }
4668};
4669
4670/// Represents the result of substituting a type for a template
4671/// type parameter.
4672///
4673/// Within an instantiated template, all template type parameters have
4674/// been replaced with these. They are used solely to record that a
4675/// type was originally written as a template type parameter;
4676/// therefore they are never canonical.
4677class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode {
4678 friend class ASTContext;
4679
4680 // The original type parameter.
4681 const TemplateTypeParmType *Replaced;
4682
4683 SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon)
4684 : Type(SubstTemplateTypeParm, Canon, Canon->isDependentType(),
4685 Canon->isInstantiationDependentType(),
4686 Canon->isVariablyModifiedType(),
4687 Canon->containsUnexpandedParameterPack()),
4688 Replaced(Param) {}
4689
4690public:
4691 /// Gets the template parameter that was substituted for.
4692 const TemplateTypeParmType *getReplacedParameter() const {
4693 return Replaced;
4694 }
4695
4696 /// Gets the type that was substituted for the template
4697 /// parameter.
4698 QualType getReplacementType() const {
4699 return getCanonicalTypeInternal();
4700 }
4701
4702 bool isSugared() const { return true; }
4703 QualType desugar() const { return getReplacementType(); }
4704
4705 void Profile(llvm::FoldingSetNodeID &ID) {
4706 Profile(ID, getReplacedParameter(), getReplacementType());
4707 }
4708
4709 static void Profile(llvm::FoldingSetNodeID &ID,
4710 const TemplateTypeParmType *Replaced,
4711 QualType Replacement) {
4712 ID.AddPointer(Replaced);
4713 ID.AddPointer(Replacement.getAsOpaquePtr());
4714 }
4715
4716 static bool classof(const Type *T) {
4717 return T->getTypeClass() == SubstTemplateTypeParm;
4718 }
4719};
4720
4721/// Represents the result of substituting a set of types for a template
4722/// type parameter pack.
4723///
4724/// When a pack expansion in the source code contains multiple parameter packs
4725/// and those parameter packs correspond to different levels of template
4726/// parameter lists, this type node is used to represent a template type
4727/// parameter pack from an outer level, which has already had its argument pack
4728/// substituted but that still lives within a pack expansion that itself
4729/// could not be instantiated. When actually performing a substitution into
4730/// that pack expansion (e.g., when all template parameters have corresponding
4731/// arguments), this type will be replaced with the \c SubstTemplateTypeParmType
4732/// at the current pack substitution index.
4733class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode {
4734 friend class ASTContext;
4735
4736 /// The original type parameter.
4737 const TemplateTypeParmType *Replaced;
4738
4739 /// A pointer to the set of template arguments that this
4740 /// parameter pack is instantiated with.
4741 const TemplateArgument *Arguments;
4742
4743 SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param,
4744 QualType Canon,
4745 const TemplateArgument &ArgPack);
4746
4747public:
4748 IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); }
4749
4750 /// Gets the template parameter that was substituted for.
4751 const TemplateTypeParmType *getReplacedParameter() const {
4752 return Replaced;
4753 }
4754
4755 unsigned getNumArgs() const {
4756 return SubstTemplateTypeParmPackTypeBits.NumArgs;
4757 }
4758
4759 bool isSugared() const { return false; }
4760 QualType desugar() const { return QualType(this, 0); }
4761
4762 TemplateArgument getArgumentPack() const;
4763
4764 void Profile(llvm::FoldingSetNodeID &ID);
4765 static void Profile(llvm::FoldingSetNodeID &ID,
4766 const TemplateTypeParmType *Replaced,
4767 const TemplateArgument &ArgPack);
4768
4769 static bool classof(const Type *T) {
4770 return T->getTypeClass() == SubstTemplateTypeParmPack;
4771 }
4772};
4773
4774/// Common base class for placeholders for types that get replaced by
4775/// placeholder type deduction: C++11 auto, C++14 decltype(auto), C++17 deduced
4776/// class template types, and (eventually) constrained type names from the C++
4777/// Concepts TS.
4778///
4779/// These types are usually a placeholder for a deduced type. However, before
4780/// the initializer is attached, or (usually) if the initializer is
4781/// type-dependent, there is no deduced type and the type is canonical. In
4782/// the latter case, it is also a dependent type.
4783class DeducedType : public Type {
4784protected:
4785 DeducedType(TypeClass TC, QualType DeducedAsType, bool IsDependent,
4786 bool IsInstantiationDependent, bool ContainsParameterPack)
4787 : Type(TC,
4788 // FIXME: Retain the sugared deduced type?
4789 DeducedAsType.isNull() ? QualType(this, 0)
4790 : DeducedAsType.getCanonicalType(),
4791 IsDependent, IsInstantiationDependent,
4792 /*VariablyModified=*/false, ContainsParameterPack) {
4793 if (!DeducedAsType.isNull()) {
4794 if (DeducedAsType->isDependentType())
4795 setDependent();
4796 if (DeducedAsType->isInstantiationDependentType())
4797 setInstantiationDependent();
4798 if (DeducedAsType->containsUnexpandedParameterPack())
4799 setContainsUnexpandedParameterPack();
4800 }
4801 }
4802
4803public:
4804 bool isSugared() const { return !isCanonicalUnqualified(); }
4805 QualType desugar() const { return getCanonicalTypeInternal(); }
4806
4807 /// Get the type deduced for this placeholder type, or null if it's
4808 /// either not been deduced or was deduced to a dependent type.
4809 QualType getDeducedType() const {
4810 return !isCanonicalUnqualified() ? getCanonicalTypeInternal() : QualType();
4811 }
4812 bool isDeduced() const {
4813 return !isCanonicalUnqualified() || isDependentType();
4814 }
4815
4816 static bool classof(const Type *T) {
4817 return T->getTypeClass() == Auto ||
4818 T->getTypeClass() == DeducedTemplateSpecialization;
4819 }
4820};
4821
4822/// Represents a C++11 auto or C++14 decltype(auto) type.
4823class AutoType : public DeducedType, public llvm::FoldingSetNode {
4824 friend class ASTContext; // ASTContext creates these
4825
4826 AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword,
4827 bool IsDeducedAsDependent, bool IsDeducedAsPack)
4828 : DeducedType(Auto, DeducedAsType, IsDeducedAsDependent,
4829 IsDeducedAsDependent, IsDeducedAsPack) {
4830 AutoTypeBits.Keyword = (unsigned)Keyword;
4831 }
4832
4833public:
4834 bool isDecltypeAuto() const {
4835 return getKeyword() == AutoTypeKeyword::DecltypeAuto;
4836 }
4837
4838 AutoTypeKeyword getKeyword() const {
4839 return (AutoTypeKeyword)AutoTypeBits.Keyword;
4840 }
4841
4842 void Profile(llvm::FoldingSetNodeID &ID) {
4843 Profile(ID, getDeducedType(), getKeyword(), isDependentType(),
4844 containsUnexpandedParameterPack());
4845 }
4846
4847 static void Profile(llvm::FoldingSetNodeID &ID, QualType Deduced,
4848 AutoTypeKeyword Keyword, bool IsDependent, bool IsPack) {
4849 ID.AddPointer(Deduced.getAsOpaquePtr());
4850 ID.AddInteger((unsigned)Keyword);
4851 ID.AddBoolean(IsDependent);
4852 ID.AddBoolean(IsPack);
4853 }
4854
4855 static bool classof(const Type *T) {
4856 return T->getTypeClass() == Auto;
4857 }
4858};
4859
4860/// Represents a C++17 deduced template specialization type.
4861class DeducedTemplateSpecializationType : public DeducedType,
4862 public llvm::FoldingSetNode {
4863 friend class ASTContext; // ASTContext creates these
4864
4865 /// The name of the template whose arguments will be deduced.
4866 TemplateName Template;
4867
4868 DeducedTemplateSpecializationType(TemplateName Template,
4869 QualType DeducedAsType,
4870 bool IsDeducedAsDependent)
4871 : DeducedType(DeducedTemplateSpecialization, DeducedAsType,
4872 IsDeducedAsDependent || Template.isDependent(),
4873 IsDeducedAsDependent || Template.isInstantiationDependent(),
4874 Template.containsUnexpandedParameterPack()),
4875 Template(Template) {}
4876
4877public:
4878 /// Retrieve the name of the template that we are deducing.
4879 TemplateName getTemplateName() const { return Template;}
4880
4881 void Profile(llvm::FoldingSetNodeID &ID) {
4882 Profile(ID, getTemplateName(), getDeducedType(), isDependentType());
4883 }
4884
4885 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Template,
4886 QualType Deduced, bool IsDependent) {
4887 Template.Profile(ID);
4888 ID.AddPointer(Deduced.getAsOpaquePtr());
4889 ID.AddBoolean(IsDependent);
4890 }
4891
4892 static bool classof(const Type *T) {
4893 return T->getTypeClass() == DeducedTemplateSpecialization;
4894 }
4895};
4896
4897/// Represents a type template specialization; the template
4898/// must be a class template, a type alias template, or a template
4899/// template parameter. A template which cannot be resolved to one of
4900/// these, e.g. because it is written with a dependent scope
4901/// specifier, is instead represented as a
4902/// @c DependentTemplateSpecializationType.
4903///
4904/// A non-dependent template specialization type is always "sugar",
4905/// typically for a \c RecordType. For example, a class template
4906/// specialization type of \c vector<int> will refer to a tag type for
4907/// the instantiation \c std::vector<int, std::allocator<int>>
4908///
4909/// Template specializations are dependent if either the template or
4910/// any of the template arguments are dependent, in which case the
4911/// type may also be canonical.
4912///
4913/// Instances of this type are allocated with a trailing array of
4914/// TemplateArguments, followed by a QualType representing the
4915/// non-canonical aliased type when the template is a type alias
4916/// template.
4917class alignas(8) TemplateSpecializationType
4918 : public Type,
4919 public llvm::FoldingSetNode {
4920 friend class ASTContext; // ASTContext creates these
4921
4922 /// The name of the template being specialized. This is
4923 /// either a TemplateName::Template (in which case it is a
4924 /// ClassTemplateDecl*, a TemplateTemplateParmDecl*, or a
4925 /// TypeAliasTemplateDecl*), a
4926 /// TemplateName::SubstTemplateTemplateParmPack, or a
4927 /// TemplateName::SubstTemplateTemplateParm (in which case the
4928 /// replacement must, recursively, be one of these).
4929 TemplateName Template;
4930
4931 TemplateSpecializationType(TemplateName T,
4932 ArrayRef<TemplateArgument> Args,
4933 QualType Canon,
4934 QualType Aliased);
4935
4936public:
4937 /// Determine whether any of the given template arguments are dependent.
4938 static bool anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
4939 bool &InstantiationDependent);
4940
4941 static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &,
4942 bool &InstantiationDependent);
4943
4944 /// True if this template specialization type matches a current
4945 /// instantiation in the context in which it is found.
4946 bool isCurrentInstantiation() const {
4947 return isa<InjectedClassNameType>(getCanonicalTypeInternal());
4948 }
4949
4950 /// Determine if this template specialization type is for a type alias
4951 /// template that has been substituted.
4952 ///
4953 /// Nearly every template specialization type whose template is an alias
4954 /// template will be substituted. However, this is not the case when
4955 /// the specialization contains a pack expansion but the template alias
4956 /// does not have a corresponding parameter pack, e.g.,
4957 ///
4958 /// \code
4959 /// template<typename T, typename U, typename V> struct S;
4960 /// template<typename T, typename U> using A = S<T, int, U>;
4961 /// template<typename... Ts> struct X {
4962 /// typedef A<Ts...> type; // not a type alias
4963 /// };
4964 /// \endcode
4965 bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; }
4966
4967 /// Get the aliased type, if this is a specialization of a type alias
4968 /// template.
4969 QualType getAliasedType() const {
4970 assert(isTypeAlias() && "not a type alias template specialization")((isTypeAlias() && "not a type alias template specialization"
) ? static_cast<void> (0) : __assert_fail ("isTypeAlias() && \"not a type alias template specialization\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 4970, __PRETTY_FUNCTION__))
;
4971 return *reinterpret_cast<const QualType*>(end());
4972 }
4973
4974 using iterator = const TemplateArgument *;
4975
4976 iterator begin() const { return getArgs(); }
4977 iterator end() const; // defined inline in TemplateBase.h
4978
4979 /// Retrieve the name of the template that we are specializing.
4980 TemplateName getTemplateName() const { return Template; }
4981
4982 /// Retrieve the template arguments.
4983 const TemplateArgument *getArgs() const {
4984 return reinterpret_cast<const TemplateArgument *>(this + 1);
4985 }
4986
4987 /// Retrieve the number of template arguments.
4988 unsigned getNumArgs() const {
4989 return TemplateSpecializationTypeBits.NumArgs;
4990 }
4991
4992 /// Retrieve a specific template argument as a type.
4993 /// \pre \c isArgType(Arg)
4994 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h
4995
4996 ArrayRef<TemplateArgument> template_arguments() const {
4997 return {getArgs(), getNumArgs()};
4998 }
4999
5000 bool isSugared() const {
5001 return !isDependentType() || isCurrentInstantiation() || isTypeAlias();
5002 }
5003
5004 QualType desugar() const {
5005 return isTypeAlias() ? getAliasedType() : getCanonicalTypeInternal();
5006 }
5007
5008 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) {
5009 Profile(ID, Template, template_arguments(), Ctx);
5010 if (isTypeAlias())
5011 getAliasedType().Profile(ID);
5012 }
5013
5014 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T,
5015 ArrayRef<TemplateArgument> Args,
5016 const ASTContext &Context);
5017
5018 static bool classof(const Type *T) {
5019 return T->getTypeClass() == TemplateSpecialization;
5020 }
5021};
5022
5023/// Print a template argument list, including the '<' and '>'
5024/// enclosing the template arguments.
5025void printTemplateArgumentList(raw_ostream &OS,
5026 ArrayRef<TemplateArgument> Args,
5027 const PrintingPolicy &Policy);
5028
5029void printTemplateArgumentList(raw_ostream &OS,
5030 ArrayRef<TemplateArgumentLoc> Args,
5031 const PrintingPolicy &Policy);
5032
5033void printTemplateArgumentList(raw_ostream &OS,
5034 const TemplateArgumentListInfo &Args,
5035 const PrintingPolicy &Policy);
5036
5037/// The injected class name of a C++ class template or class
5038/// template partial specialization. Used to record that a type was
5039/// spelled with a bare identifier rather than as a template-id; the
5040/// equivalent for non-templated classes is just RecordType.
5041///
5042/// Injected class name types are always dependent. Template
5043/// instantiation turns these into RecordTypes.
5044///
5045/// Injected class name types are always canonical. This works
5046/// because it is impossible to compare an injected class name type
5047/// with the corresponding non-injected template type, for the same
5048/// reason that it is impossible to directly compare template
5049/// parameters from different dependent contexts: injected class name
5050/// types can only occur within the scope of a particular templated
5051/// declaration, and within that scope every template specialization
5052/// will canonicalize to the injected class name (when appropriate
5053/// according to the rules of the language).
5054class InjectedClassNameType : public Type {
5055 friend class ASTContext; // ASTContext creates these.
5056 friend class ASTNodeImporter;
5057 friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not
5058 // currently suitable for AST reading, too much
5059 // interdependencies.
5060
5061 CXXRecordDecl *Decl;
5062
5063 /// The template specialization which this type represents.
5064 /// For example, in
5065 /// template <class T> class A { ... };
5066 /// this is A<T>, whereas in
5067 /// template <class X, class Y> class A<B<X,Y> > { ... };
5068 /// this is A<B<X,Y> >.
5069 ///
5070 /// It is always unqualified, always a template specialization type,
5071 /// and always dependent.
5072 QualType InjectedType;
5073
5074 InjectedClassNameType(CXXRecordDecl *D, QualType TST)
5075 : Type(InjectedClassName, QualType(), /*Dependent=*/true,
5076 /*InstantiationDependent=*/true,
5077 /*VariablyModified=*/false,
5078 /*ContainsUnexpandedParameterPack=*/false),
5079 Decl(D), InjectedType(TST) {
5080 assert(isa<TemplateSpecializationType>(TST))((isa<TemplateSpecializationType>(TST)) ? static_cast<
void> (0) : __assert_fail ("isa<TemplateSpecializationType>(TST)"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 5080, __PRETTY_FUNCTION__))
;
5081 assert(!TST.hasQualifiers())((!TST.hasQualifiers()) ? static_cast<void> (0) : __assert_fail
("!TST.hasQualifiers()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 5081, __PRETTY_FUNCTION__))
;
5082 assert(TST->isDependentType())((TST->isDependentType()) ? static_cast<void> (0) : __assert_fail
("TST->isDependentType()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 5082, __PRETTY_FUNCTION__))
;
5083 }
5084
5085public:
5086 QualType getInjectedSpecializationType() const { return InjectedType; }
5087
5088 const TemplateSpecializationType *getInjectedTST() const {
5089 return cast<TemplateSpecializationType>(InjectedType.getTypePtr());
5090 }
5091
5092 TemplateName getTemplateName() const {
5093 return getInjectedTST()->getTemplateName();
5094 }
5095
5096 CXXRecordDecl *getDecl() const;
5097
5098 bool isSugared() const { return false; }
5099 QualType desugar() const { return QualType(this, 0); }
5100
5101 static bool classof(const Type *T) {
5102 return T->getTypeClass() == InjectedClassName;
5103 }
5104};
5105
5106/// The kind of a tag type.
5107enum TagTypeKind {
5108 /// The "struct" keyword.
5109 TTK_Struct,
5110
5111 /// The "__interface" keyword.
5112 TTK_Interface,
5113
5114 /// The "union" keyword.
5115 TTK_Union,
5116
5117 /// The "class" keyword.
5118 TTK_Class,
5119
5120 /// The "enum" keyword.
5121 TTK_Enum
5122};
5123
5124/// The elaboration keyword that precedes a qualified type name or
5125/// introduces an elaborated-type-specifier.
5126enum ElaboratedTypeKeyword {
5127 /// The "struct" keyword introduces the elaborated-type-specifier.
5128 ETK_Struct,
5129
5130 /// The "__interface" keyword introduces the elaborated-type-specifier.
5131 ETK_Interface,
5132
5133 /// The "union" keyword introduces the elaborated-type-specifier.
5134 ETK_Union,
5135
5136 /// The "class" keyword introduces the elaborated-type-specifier.
5137 ETK_Class,
5138
5139 /// The "enum" keyword introduces the elaborated-type-specifier.
5140 ETK_Enum,
5141
5142 /// The "typename" keyword precedes the qualified type name, e.g.,
5143 /// \c typename T::type.
5144 ETK_Typename,
5145
5146 /// No keyword precedes the qualified type name.
5147 ETK_None
5148};
5149
5150/// A helper class for Type nodes having an ElaboratedTypeKeyword.
5151/// The keyword in stored in the free bits of the base class.
5152/// Also provides a few static helpers for converting and printing
5153/// elaborated type keyword and tag type kind enumerations.
5154class TypeWithKeyword : public Type {
5155protected:
5156 TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc,
5157 QualType Canonical, bool Dependent,
5158 bool InstantiationDependent, bool VariablyModified,
5159 bool ContainsUnexpandedParameterPack)
5160 : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified,
5161 ContainsUnexpandedParameterPack) {
5162 TypeWithKeywordBits.Keyword = Keyword;
5163 }
5164
5165public:
5166 ElaboratedTypeKeyword getKeyword() const {
5167 return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword);
5168 }
5169
5170 /// Converts a type specifier (DeclSpec::TST) into an elaborated type keyword.
5171 static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec);
5172
5173 /// Converts a type specifier (DeclSpec::TST) into a tag type kind.
5174 /// It is an error to provide a type specifier which *isn't* a tag kind here.
5175 static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec);
5176
5177 /// Converts a TagTypeKind into an elaborated type keyword.
5178 static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag);
5179
5180 /// Converts an elaborated type keyword into a TagTypeKind.
5181 /// It is an error to provide an elaborated type keyword
5182 /// which *isn't* a tag kind here.
5183 static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword);
5184
5185 static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword);
5186
5187 static StringRef getKeywordName(ElaboratedTypeKeyword Keyword);
5188
5189 static StringRef getTagTypeKindName(TagTypeKind Kind) {
5190 return getKeywordName(getKeywordForTagTypeKind(Kind));
5191 }
5192
5193 class CannotCastToThisType {};
5194 static CannotCastToThisType classof(const Type *);
5195};
5196
5197/// Represents a type that was referred to using an elaborated type
5198/// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type,
5199/// or both.
5200///
5201/// This type is used to keep track of a type name as written in the
5202/// source code, including tag keywords and any nested-name-specifiers.
5203/// The type itself is always "sugar", used to express what was written
5204/// in the source code but containing no additional semantic information.
5205class ElaboratedType final
5206 : public TypeWithKeyword,
5207 public llvm::FoldingSetNode,
5208 private llvm::TrailingObjects<ElaboratedType, TagDecl *> {
5209 friend class ASTContext; // ASTContext creates these
5210 friend TrailingObjects;
5211
5212 /// The nested name specifier containing the qualifier.
5213 NestedNameSpecifier *NNS;
5214
5215 /// The type that this qualified name refers to.
5216 QualType NamedType;
5217
5218 /// The (re)declaration of this tag type owned by this occurrence is stored
5219 /// as a trailing object if there is one. Use getOwnedTagDecl to obtain
5220 /// it, or obtain a null pointer if there is none.
5221
5222 ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
5223 QualType NamedType, QualType CanonType, TagDecl *OwnedTagDecl)
5224 : TypeWithKeyword(Keyword, Elaborated, CanonType,
5225 NamedType->isDependentType(),
5226 NamedType->isInstantiationDependentType(),
5227 NamedType->isVariablyModifiedType(),
5228 NamedType->containsUnexpandedParameterPack()),
5229 NNS(NNS), NamedType(NamedType) {
5230 ElaboratedTypeBits.HasOwnedTagDecl = false;
5231 if (OwnedTagDecl) {
5232 ElaboratedTypeBits.HasOwnedTagDecl = true;
5233 *getTrailingObjects<TagDecl *>() = OwnedTagDecl;
5234 }
5235 assert(!(Keyword == ETK_None && NNS == nullptr) &&((!(Keyword == ETK_None && NNS == nullptr) &&
"ElaboratedType cannot have elaborated type keyword " "and name qualifier both null."
) ? static_cast<void> (0) : __assert_fail ("!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 5237, __PRETTY_FUNCTION__))
5236 "ElaboratedType cannot have elaborated type keyword "((!(Keyword == ETK_None && NNS == nullptr) &&
"ElaboratedType cannot have elaborated type keyword " "and name qualifier both null."
) ? static_cast<void> (0) : __assert_fail ("!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 5237, __PRETTY_FUNCTION__))
5237 "and name qualifier both null.")((!(Keyword == ETK_None && NNS == nullptr) &&
"ElaboratedType cannot have elaborated type keyword " "and name qualifier both null."
) ? static_cast<void> (0) : __assert_fail ("!(Keyword == ETK_None && NNS == nullptr) && \"ElaboratedType cannot have elaborated type keyword \" \"and name qualifier both null.\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 5237, __PRETTY_FUNCTION__))
;
5238 }
5239
5240public:
5241 /// Retrieve the qualification on this type.
5242 NestedNameSpecifier *getQualifier() const { return NNS; }
5243
5244 /// Retrieve the type named by the qualified-id.
5245 QualType getNamedType() const { return NamedType; }
5246
5247 /// Remove a single level of sugar.
5248 QualType desugar() const { return getNamedType(); }
5249
5250 /// Returns whether this type directly provides sugar.
5251 bool isSugared() const { return true; }
5252
5253 /// Return the (re)declaration of this type owned by this occurrence of this
5254 /// type, or nullptr if there is none.
5255 TagDecl *getOwnedTagDecl() const {
5256 return ElaboratedTypeBits.HasOwnedTagDecl ? *getTrailingObjects<TagDecl *>()
5257 : nullptr;
5258 }
5259
5260 void Profile(llvm::FoldingSetNodeID &ID) {
5261 Profile(ID, getKeyword(), NNS, NamedType, getOwnedTagDecl());
5262 }
5263
5264 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword,
5265 NestedNameSpecifier *NNS, QualType NamedType,
5266 TagDecl *OwnedTagDecl) {
5267 ID.AddInteger(Keyword);
5268 ID.AddPointer(NNS);
5269 NamedType.Profile(ID);
5270 ID.AddPointer(OwnedTagDecl);
5271 }
5272
5273 static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; }
5274};
5275
5276/// Represents a qualified type name for which the type name is
5277/// dependent.
5278///
5279/// DependentNameType represents a class of dependent types that involve a
5280/// possibly dependent nested-name-specifier (e.g., "T::") followed by a
5281/// name of a type. The DependentNameType may start with a "typename" (for a
5282/// typename-specifier), "class", "struct", "union", or "enum" (for a
5283/// dependent elaborated-type-specifier), or nothing (in contexts where we
5284/// know that we must be referring to a type, e.g., in a base class specifier).
5285/// Typically the nested-name-specifier is dependent, but in MSVC compatibility
5286/// mode, this type is used with non-dependent names to delay name lookup until
5287/// instantiation.
5288class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode {
5289 friend class ASTContext; // ASTContext creates these
5290
5291 /// The nested name specifier containing the qualifier.
5292 NestedNameSpecifier *NNS;
5293
5294 /// The type that this typename specifier refers to.
5295 const IdentifierInfo *Name;
5296
5297 DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
5298 const IdentifierInfo *Name, QualType CanonType)
5299 : TypeWithKeyword(Keyword, DependentName, CanonType, /*Dependent=*/true,
5300 /*InstantiationDependent=*/true,
5301 /*VariablyModified=*/false,
5302 NNS->containsUnexpandedParameterPack()),
5303 NNS(NNS), Name(Name) {}
5304
5305public:
5306 /// Retrieve the qualification on this type.
5307 NestedNameSpecifier *getQualifier() const { return NNS; }
5308
5309 /// Retrieve the type named by the typename specifier as an identifier.
5310 ///
5311 /// This routine will return a non-NULL identifier pointer when the
5312 /// form of the original typename was terminated by an identifier,
5313 /// e.g., "typename T::type".
5314 const IdentifierInfo *getIdentifier() const {
5315 return Name;
5316 }
5317
5318 bool isSugared() const { return false; }
5319 QualType desugar() const { return QualType(this, 0); }
5320
5321 void Profile(llvm::FoldingSetNodeID &ID) {
5322 Profile(ID, getKeyword(), NNS, Name);
5323 }
5324
5325 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword,
5326 NestedNameSpecifier *NNS, const IdentifierInfo *Name) {
5327 ID.AddInteger(Keyword);
5328 ID.AddPointer(NNS);
5329 ID.AddPointer(Name);
5330 }
5331
5332 static bool classof(const Type *T) {
5333 return T->getTypeClass() == DependentName;
5334 }
5335};
5336
5337/// Represents a template specialization type whose template cannot be
5338/// resolved, e.g.
5339/// A<T>::template B<T>
5340class alignas(8) DependentTemplateSpecializationType
5341 : public TypeWithKeyword,
5342 public llvm::FoldingSetNode {
5343 friend class ASTContext; // ASTContext creates these
5344
5345 /// The nested name specifier containing the qualifier.
5346 NestedNameSpecifier *NNS;
5347
5348 /// The identifier of the template.
5349 const IdentifierInfo *Name;
5350
5351 DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword,
5352 NestedNameSpecifier *NNS,
5353 const IdentifierInfo *Name,
5354 ArrayRef<TemplateArgument> Args,
5355 QualType Canon);
5356
5357 const TemplateArgument *getArgBuffer() const {
5358 return reinterpret_cast<const TemplateArgument*>(this+1);
5359 }
5360
5361 TemplateArgument *getArgBuffer() {
5362 return reinterpret_cast<TemplateArgument*>(this+1);
5363 }
5364
5365public:
5366 NestedNameSpecifier *getQualifier() const { return NNS; }
5367 const IdentifierInfo *getIdentifier() const { return Name; }
5368
5369 /// Retrieve the template arguments.
5370 const TemplateArgument *getArgs() const {
5371 return getArgBuffer();
5372 }
5373
5374 /// Retrieve the number of template arguments.
5375 unsigned getNumArgs() const {
5376 return DependentTemplateSpecializationTypeBits.NumArgs;
5377 }
5378
5379 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h
5380
5381 ArrayRef<TemplateArgument> template_arguments() const {
5382 return {getArgs(), getNumArgs()};
5383 }
5384
5385 using iterator = const TemplateArgument *;
5386
5387 iterator begin() const { return getArgs(); }
5388 iterator end() const; // inline in TemplateBase.h
5389
5390 bool isSugared() const { return false; }
5391 QualType desugar() const { return QualType(this, 0); }
5392
5393 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
5394 Profile(ID, Context, getKeyword(), NNS, Name, {getArgs(), getNumArgs()});
5395 }
5396
5397 static void Profile(llvm::FoldingSetNodeID &ID,
5398 const ASTContext &Context,
5399 ElaboratedTypeKeyword Keyword,
5400 NestedNameSpecifier *Qualifier,
5401 const IdentifierInfo *Name,
5402 ArrayRef<TemplateArgument> Args);
5403
5404 static bool classof(const Type *T) {
5405 return T->getTypeClass() == DependentTemplateSpecialization;
5406 }
5407};
5408
5409/// Represents a pack expansion of types.
5410///
5411/// Pack expansions are part of C++11 variadic templates. A pack
5412/// expansion contains a pattern, which itself contains one or more
5413/// "unexpanded" parameter packs. When instantiated, a pack expansion
5414/// produces a series of types, each instantiated from the pattern of
5415/// the expansion, where the Ith instantiation of the pattern uses the
5416/// Ith arguments bound to each of the unexpanded parameter packs. The
5417/// pack expansion is considered to "expand" these unexpanded
5418/// parameter packs.
5419///
5420/// \code
5421/// template<typename ...Types> struct tuple;
5422///
5423/// template<typename ...Types>
5424/// struct tuple_of_references {
5425/// typedef tuple<Types&...> type;
5426/// };
5427/// \endcode
5428///
5429/// Here, the pack expansion \c Types&... is represented via a
5430/// PackExpansionType whose pattern is Types&.
5431class PackExpansionType : public Type, public llvm::FoldingSetNode {
5432 friend class ASTContext; // ASTContext creates these
5433
5434 /// The pattern of the pack expansion.
5435 QualType Pattern;
5436
5437 PackExpansionType(QualType Pattern, QualType Canon,
5438 Optional<unsigned> NumExpansions)
5439 : Type(PackExpansion, Canon, /*Dependent=*/Pattern->isDependentType(),
5440 /*InstantiationDependent=*/true,
5441 /*VariablyModified=*/Pattern->isVariablyModifiedType(),
5442 /*ContainsUnexpandedParameterPack=*/false),
5443 Pattern(Pattern) {
5444 PackExpansionTypeBits.NumExpansions =
5445 NumExpansions ? *NumExpansions + 1 : 0;
5446 }
5447
5448public:
5449 /// Retrieve the pattern of this pack expansion, which is the
5450 /// type that will be repeatedly instantiated when instantiating the
5451 /// pack expansion itself.
5452 QualType getPattern() const { return Pattern; }
5453
5454 /// Retrieve the number of expansions that this pack expansion will
5455 /// generate, if known.
5456 Optional<unsigned> getNumExpansions() const {
5457 if (PackExpansionTypeBits.NumExpansions)
5458 return PackExpansionTypeBits.NumExpansions - 1;
5459 return None;
5460 }
5461
5462 bool isSugared() const { return !Pattern->isDependentType(); }
5463 QualType desugar() const { return isSugared() ? Pattern : QualType(this, 0); }
5464
5465 void Profile(llvm::FoldingSetNodeID &ID) {
5466 Profile(ID, getPattern(), getNumExpansions());
5467 }
5468
5469 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern,
5470 Optional<unsigned> NumExpansions) {
5471 ID.AddPointer(Pattern.getAsOpaquePtr());
5472 ID.AddBoolean(NumExpansions.hasValue());
5473 if (NumExpansions)
5474 ID.AddInteger(*NumExpansions);
5475 }
5476
5477 static bool classof(const Type *T) {
5478 return T->getTypeClass() == PackExpansion;
5479 }
5480};
5481
5482/// This class wraps the list of protocol qualifiers. For types that can
5483/// take ObjC protocol qualifers, they can subclass this class.
5484template <class T>
5485class ObjCProtocolQualifiers {
5486protected:
5487 ObjCProtocolQualifiers() = default;
5488
5489 ObjCProtocolDecl * const *getProtocolStorage() const {
5490 return const_cast<ObjCProtocolQualifiers*>(this)->getProtocolStorage();
5491 }
5492
5493 ObjCProtocolDecl **getProtocolStorage() {
5494 return static_cast<T*>(this)->getProtocolStorageImpl();
5495 }
5496
5497 void setNumProtocols(unsigned N) {
5498 static_cast<T*>(this)->setNumProtocolsImpl(N);
5499 }
5500
5501 void initialize(ArrayRef<ObjCProtocolDecl *> protocols) {
5502 setNumProtocols(protocols.size());
5503 assert(getNumProtocols() == protocols.size() &&((getNumProtocols() == protocols.size() && "bitfield overflow in protocol count"
) ? static_cast<void> (0) : __assert_fail ("getNumProtocols() == protocols.size() && \"bitfield overflow in protocol count\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 5504, __PRETTY_FUNCTION__))
5504 "bitfield overflow in protocol count")((getNumProtocols() == protocols.size() && "bitfield overflow in protocol count"
) ? static_cast<void> (0) : __assert_fail ("getNumProtocols() == protocols.size() && \"bitfield overflow in protocol count\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 5504, __PRETTY_FUNCTION__))
;
5505 if (!protocols.empty())
5506 memcpy(getProtocolStorage(), protocols.data(),
5507 protocols.size() * sizeof(ObjCProtocolDecl*));
5508 }
5509
5510public:
5511 using qual_iterator = ObjCProtocolDecl * const *;
5512 using qual_range = llvm::iterator_range<qual_iterator>;
5513
5514 qual_range quals() const { return qual_range(qual_begin(), qual_end()); }
5515 qual_iterator qual_begin() const { return getProtocolStorage(); }
5516 qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); }
5517
5518 bool qual_empty() const { return getNumProtocols() == 0; }
5519
5520 /// Return the number of qualifying protocols in this type, or 0 if
5521 /// there are none.
5522 unsigned getNumProtocols() const {
5523 return static_cast<const T*>(this)->getNumProtocolsImpl();
5524 }
5525
5526 /// Fetch a protocol by index.
5527 ObjCProtocolDecl *getProtocol(unsigned I) const {
5528 assert(I < getNumProtocols() && "Out-of-range protocol access")((I < getNumProtocols() && "Out-of-range protocol access"
) ? static_cast<void> (0) : __assert_fail ("I < getNumProtocols() && \"Out-of-range protocol access\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 5528, __PRETTY_FUNCTION__))
;
5529 return qual_begin()[I];
5530 }
5531
5532 /// Retrieve all of the protocol qualifiers.
5533 ArrayRef<ObjCProtocolDecl *> getProtocols() const {
5534 return ArrayRef<ObjCProtocolDecl *>(qual_begin(), getNumProtocols());
5535 }
5536};
5537
5538/// Represents a type parameter type in Objective C. It can take
5539/// a list of protocols.
5540class ObjCTypeParamType : public Type,
5541 public ObjCProtocolQualifiers<ObjCTypeParamType>,
5542 public llvm::FoldingSetNode {
5543 friend class ASTContext;
5544 friend class ObjCProtocolQualifiers<ObjCTypeParamType>;
5545
5546 /// The number of protocols stored on this type.
5547 unsigned NumProtocols : 6;
5548
5549 ObjCTypeParamDecl *OTPDecl;
5550
5551 /// The protocols are stored after the ObjCTypeParamType node. In the
5552 /// canonical type, the list of protocols are sorted alphabetically
5553 /// and uniqued.
5554 ObjCProtocolDecl **getProtocolStorageImpl();
5555
5556 /// Return the number of qualifying protocols in this interface type,
5557 /// or 0 if there are none.
5558 unsigned getNumProtocolsImpl() const {
5559 return NumProtocols;
5560 }
5561
5562 void setNumProtocolsImpl(unsigned N) {
5563 NumProtocols = N;
5564 }
5565
5566 ObjCTypeParamType(const ObjCTypeParamDecl *D,
5567 QualType can,
5568 ArrayRef<ObjCProtocolDecl *> protocols);
5569
5570public:
5571 bool isSugared() const { return true; }
5572 QualType desugar() const { return getCanonicalTypeInternal(); }
5573
5574 static bool classof(const Type *T) {
5575 return T->getTypeClass() == ObjCTypeParam;
5576 }
5577
5578 void Profile(llvm::FoldingSetNodeID &ID);
5579 static void Profile(llvm::FoldingSetNodeID &ID,
5580 const ObjCTypeParamDecl *OTPDecl,
5581 ArrayRef<ObjCProtocolDecl *> protocols);
5582
5583 ObjCTypeParamDecl *getDecl() const { return OTPDecl; }
5584};
5585
5586/// Represents a class type in Objective C.
5587///
5588/// Every Objective C type is a combination of a base type, a set of
5589/// type arguments (optional, for parameterized classes) and a list of
5590/// protocols.
5591///
5592/// Given the following declarations:
5593/// \code
5594/// \@class C<T>;
5595/// \@protocol P;
5596/// \endcode
5597///
5598/// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType
5599/// with base C and no protocols.
5600///
5601/// 'C<P>' is an unspecialized ObjCObjectType with base C and protocol list [P].
5602/// 'C<C*>' is a specialized ObjCObjectType with type arguments 'C*' and no
5603/// protocol list.
5604/// 'C<C*><P>' is a specialized ObjCObjectType with base C, type arguments 'C*',
5605/// and protocol list [P].
5606///
5607/// 'id' is a TypedefType which is sugar for an ObjCObjectPointerType whose
5608/// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType
5609/// and no protocols.
5610///
5611/// 'id<P>' is an ObjCObjectPointerType whose pointee is an ObjCObjectType
5612/// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually
5613/// this should get its own sugar class to better represent the source.
5614class ObjCObjectType : public Type,
5615 public ObjCProtocolQualifiers<ObjCObjectType> {
5616 friend class ObjCProtocolQualifiers<ObjCObjectType>;
5617
5618 // ObjCObjectType.NumTypeArgs - the number of type arguments stored
5619 // after the ObjCObjectPointerType node.
5620 // ObjCObjectType.NumProtocols - the number of protocols stored
5621 // after the type arguments of ObjCObjectPointerType node.
5622 //
5623 // These protocols are those written directly on the type. If
5624 // protocol qualifiers ever become additive, the iterators will need
5625 // to get kindof complicated.
5626 //
5627 // In the canonical object type, these are sorted alphabetically
5628 // and uniqued.
5629
5630 /// Either a BuiltinType or an InterfaceType or sugar for either.
5631 QualType BaseType;
5632
5633 /// Cached superclass type.
5634 mutable llvm::PointerIntPair<const ObjCObjectType *, 1, bool>
5635 CachedSuperClassType;
5636
5637 QualType *getTypeArgStorage();
5638 const QualType *getTypeArgStorage() const {
5639 return const_cast<ObjCObjectType *>(this)->getTypeArgStorage();
5640 }
5641
5642 ObjCProtocolDecl **getProtocolStorageImpl();
5643 /// Return the number of qualifying protocols in this interface type,
5644 /// or 0 if there are none.
5645 unsigned getNumProtocolsImpl() const {
5646 return ObjCObjectTypeBits.NumProtocols;
5647 }
5648 void setNumProtocolsImpl(unsigned N) {
5649 ObjCObjectTypeBits.NumProtocols = N;
5650 }
5651
5652protected:
5653 enum Nonce_ObjCInterface { Nonce_ObjCInterface };
5654
5655 ObjCObjectType(QualType Canonical, QualType Base,
5656 ArrayRef<QualType> typeArgs,
5657 ArrayRef<ObjCProtocolDecl *> protocols,
5658 bool isKindOf);
5659
5660 ObjCObjectType(enum Nonce_ObjCInterface)
5661 : Type(ObjCInterface, QualType(), false, false, false, false),
5662 BaseType(QualType(this_(), 0)) {
5663 ObjCObjectTypeBits.NumProtocols = 0;
5664 ObjCObjectTypeBits.NumTypeArgs = 0;
5665 ObjCObjectTypeBits.IsKindOf = 0;
5666 }
5667
5668 void computeSuperClassTypeSlow() const;
5669
5670public:
5671 /// Gets the base type of this object type. This is always (possibly
5672 /// sugar for) one of:
5673 /// - the 'id' builtin type (as opposed to the 'id' type visible to the
5674 /// user, which is a typedef for an ObjCObjectPointerType)
5675 /// - the 'Class' builtin type (same caveat)
5676 /// - an ObjCObjectType (currently always an ObjCInterfaceType)
5677 QualType getBaseType() const { return BaseType; }
5678
5679 bool isObjCId() const {
5680 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId);
5681 }
5682
5683 bool isObjCClass() const {
5684 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass);
5685 }
5686
5687 bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); }
5688 bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); }
5689 bool isObjCUnqualifiedIdOrClass() const {
5690 if (!qual_empty()) return false;
5691 if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>())
5692 return T->getKind() == BuiltinType::ObjCId ||
5693 T->getKind() == BuiltinType::ObjCClass;
5694 return false;
5695 }
5696 bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); }
5697 bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); }
5698
5699 /// Gets the interface declaration for this object type, if the base type
5700 /// really is an interface.
5701 ObjCInterfaceDecl *getInterface() const;
5702
5703 /// Determine whether this object type is "specialized", meaning
5704 /// that it has type arguments.
5705 bool isSpecialized() const;
5706
5707 /// Determine whether this object type was written with type arguments.
5708 bool isSpecializedAsWritten() const {
5709 return ObjCObjectTypeBits.NumTypeArgs > 0;
5710 }
5711
5712 /// Determine whether this object type is "unspecialized", meaning
5713 /// that it has no type arguments.
5714 bool isUnspecialized() const { return !isSpecialized(); }
5715
5716 /// Determine whether this object type is "unspecialized" as
5717 /// written, meaning that it has no type arguments.
5718 bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); }
5719
5720 /// Retrieve the type arguments of this object type (semantically).
5721 ArrayRef<QualType> getTypeArgs() const;
5722
5723 /// Retrieve the type arguments of this object type as they were
5724 /// written.
5725 ArrayRef<QualType> getTypeArgsAsWritten() const {
5726 return llvm::makeArrayRef(getTypeArgStorage(),
5727 ObjCObjectTypeBits.NumTypeArgs);
5728 }
5729
5730 /// Whether this is a "__kindof" type as written.
5731 bool isKindOfTypeAsWritten() const { return ObjCObjectTypeBits.IsKindOf; }
5732
5733 /// Whether this ia a "__kindof" type (semantically).
5734 bool isKindOfType() const;
5735
5736 /// Retrieve the type of the superclass of this object type.
5737 ///
5738 /// This operation substitutes any type arguments into the
5739 /// superclass of the current class type, potentially producing a
5740 /// specialization of the superclass type. Produces a null type if
5741 /// there is no superclass.
5742 QualType getSuperClassType() const {
5743 if (!CachedSuperClassType.getInt())
5744 computeSuperClassTypeSlow();
5745
5746 assert(CachedSuperClassType.getInt() && "Superclass not set?")((CachedSuperClassType.getInt() && "Superclass not set?"
) ? static_cast<void> (0) : __assert_fail ("CachedSuperClassType.getInt() && \"Superclass not set?\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 5746, __PRETTY_FUNCTION__))
;
5747 return QualType(CachedSuperClassType.getPointer(), 0);
5748 }
5749
5750 /// Strip off the Objective-C "kindof" type and (with it) any
5751 /// protocol qualifiers.
5752 QualType stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const;
5753
5754 bool isSugared() const { return false; }
5755 QualType desugar() const { return QualType(this, 0); }
5756
5757 static bool classof(const Type *T) {
5758 return T->getTypeClass() == ObjCObject ||
5759 T->getTypeClass() == ObjCInterface;
5760 }
5761};
5762
5763/// A class providing a concrete implementation
5764/// of ObjCObjectType, so as to not increase the footprint of
5765/// ObjCInterfaceType. Code outside of ASTContext and the core type
5766/// system should not reference this type.
5767class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode {
5768 friend class ASTContext;
5769
5770 // If anyone adds fields here, ObjCObjectType::getProtocolStorage()
5771 // will need to be modified.
5772
5773 ObjCObjectTypeImpl(QualType Canonical, QualType Base,
5774 ArrayRef<QualType> typeArgs,
5775 ArrayRef<ObjCProtocolDecl *> protocols,
5776 bool isKindOf)
5777 : ObjCObjectType(Canonical, Base, typeArgs, protocols, isKindOf) {}
5778
5779public:
5780 void Profile(llvm::FoldingSetNodeID &ID);
5781 static void Profile(llvm::FoldingSetNodeID &ID,
5782 QualType Base,
5783 ArrayRef<QualType> typeArgs,
5784 ArrayRef<ObjCProtocolDecl *> protocols,
5785 bool isKindOf);
5786};
5787
5788inline QualType *ObjCObjectType::getTypeArgStorage() {
5789 return reinterpret_cast<QualType *>(static_cast<ObjCObjectTypeImpl*>(this)+1);
5790}
5791
5792inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorageImpl() {
5793 return reinterpret_cast<ObjCProtocolDecl**>(
5794 getTypeArgStorage() + ObjCObjectTypeBits.NumTypeArgs);
5795}
5796
5797inline ObjCProtocolDecl **ObjCTypeParamType::getProtocolStorageImpl() {
5798 return reinterpret_cast<ObjCProtocolDecl**>(
5799 static_cast<ObjCTypeParamType*>(this)+1);
5800}
5801
5802/// Interfaces are the core concept in Objective-C for object oriented design.
5803/// They basically correspond to C++ classes. There are two kinds of interface
5804/// types: normal interfaces like `NSString`, and qualified interfaces, which
5805/// are qualified with a protocol list like `NSString<NSCopyable, NSAmazing>`.
5806///
5807/// ObjCInterfaceType guarantees the following properties when considered
5808/// as a subtype of its superclass, ObjCObjectType:
5809/// - There are no protocol qualifiers. To reinforce this, code which
5810/// tries to invoke the protocol methods via an ObjCInterfaceType will
5811/// fail to compile.
5812/// - It is its own base type. That is, if T is an ObjCInterfaceType*,
5813/// T->getBaseType() == QualType(T, 0).
5814class ObjCInterfaceType : public ObjCObjectType {
5815 friend class ASTContext; // ASTContext creates these.
5816 friend class ASTReader;
5817 friend class ObjCInterfaceDecl;
5818
5819 mutable ObjCInterfaceDecl *Decl;
5820
5821 ObjCInterfaceType(const ObjCInterfaceDecl *D)
5822 : ObjCObjectType(Nonce_ObjCInterface),
5823 Decl(const_cast<ObjCInterfaceDecl*>(D)) {}
5824
5825public:
5826 /// Get the declaration of this interface.
5827 ObjCInterfaceDecl *getDecl() const { return Decl; }
5828
5829 bool isSugared() const { return false; }
5830 QualType desugar() const { return QualType(this, 0); }
5831
5832 static bool classof(const Type *T) {
5833 return T->getTypeClass() == ObjCInterface;
5834 }
5835
5836 // Nonsense to "hide" certain members of ObjCObjectType within this
5837 // class. People asking for protocols on an ObjCInterfaceType are
5838 // not going to get what they want: ObjCInterfaceTypes are
5839 // guaranteed to have no protocols.
5840 enum {
5841 qual_iterator,
5842 qual_begin,
5843 qual_end,
5844 getNumProtocols,
5845 getProtocol
5846 };
5847};
5848
5849inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const {
5850 QualType baseType = getBaseType();
5851 while (const auto *ObjT = baseType->getAs<ObjCObjectType>()) {
5852 if (const auto *T = dyn_cast<ObjCInterfaceType>(ObjT))
5853 return T->getDecl();
5854
5855 baseType = ObjT->getBaseType();
5856 }
5857
5858 return nullptr;
5859}
5860
5861/// Represents a pointer to an Objective C object.
5862///
5863/// These are constructed from pointer declarators when the pointee type is
5864/// an ObjCObjectType (or sugar for one). In addition, the 'id' and 'Class'
5865/// types are typedefs for these, and the protocol-qualified types 'id<P>'
5866/// and 'Class<P>' are translated into these.
5867///
5868/// Pointers to pointers to Objective C objects are still PointerTypes;
5869/// only the first level of pointer gets it own type implementation.
5870class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode {
5871 friend class ASTContext; // ASTContext creates these.
5872
5873 QualType PointeeType;
5874
5875 ObjCObjectPointerType(QualType Canonical, QualType Pointee)
5876 : Type(ObjCObjectPointer, Canonical,
5877 Pointee->isDependentType(),
5878 Pointee->isInstantiationDependentType(),
5879 Pointee->isVariablyModifiedType(),
5880 Pointee->containsUnexpandedParameterPack()),
5881 PointeeType(Pointee) {}
5882
5883public:
5884 /// Gets the type pointed to by this ObjC pointer.
5885 /// The result will always be an ObjCObjectType or sugar thereof.
5886 QualType getPointeeType() const { return PointeeType; }
5887
5888 /// Gets the type pointed to by this ObjC pointer. Always returns non-null.
5889 ///
5890 /// This method is equivalent to getPointeeType() except that
5891 /// it discards any typedefs (or other sugar) between this
5892 /// type and the "outermost" object type. So for:
5893 /// \code
5894 /// \@class A; \@protocol P; \@protocol Q;
5895 /// typedef A<P> AP;
5896 /// typedef A A1;
5897 /// typedef A1<P> A1P;
5898 /// typedef A1P<Q> A1PQ;
5899 /// \endcode
5900 /// For 'A*', getObjectType() will return 'A'.
5901 /// For 'A<P>*', getObjectType() will return 'A<P>'.
5902 /// For 'AP*', getObjectType() will return 'A<P>'.
5903 /// For 'A1*', getObjectType() will return 'A'.
5904 /// For 'A1<P>*', getObjectType() will return 'A1<P>'.
5905 /// For 'A1P*', getObjectType() will return 'A1<P>'.
5906 /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because
5907 /// adding protocols to a protocol-qualified base discards the
5908 /// old qualifiers (for now). But if it didn't, getObjectType()
5909 /// would return 'A1P<Q>' (and we'd have to make iterating over
5910 /// qualifiers more complicated).
5911 const ObjCObjectType *getObjectType() const {
5912 return PointeeType->castAs<ObjCObjectType>();
5913 }
5914
5915 /// If this pointer points to an Objective C
5916 /// \@interface type, gets the type for that interface. Any protocol
5917 /// qualifiers on the interface are ignored.
5918 ///
5919 /// \return null if the base type for this pointer is 'id' or 'Class'
5920 const ObjCInterfaceType *getInterfaceType() const;
5921
5922 /// If this pointer points to an Objective \@interface
5923 /// type, gets the declaration for that interface.
5924 ///
5925 /// \return null if the base type for this pointer is 'id' or 'Class'
5926 ObjCInterfaceDecl *getInterfaceDecl() const {
5927 return getObjectType()->getInterface();
5928 }
5929
5930 /// True if this is equivalent to the 'id' type, i.e. if
5931 /// its object type is the primitive 'id' type with no protocols.
5932 bool isObjCIdType() const {
5933 return getObjectType()->isObjCUnqualifiedId();
5934 }
5935
5936 /// True if this is equivalent to the 'Class' type,
5937 /// i.e. if its object tive is the primitive 'Class' type with no protocols.
5938 bool isObjCClassType() const {
5939 return getObjectType()->isObjCUnqualifiedClass();
5940 }
5941
5942 /// True if this is equivalent to the 'id' or 'Class' type,
5943 bool isObjCIdOrClassType() const {
5944 return getObjectType()->isObjCUnqualifiedIdOrClass();
5945 }
5946
5947 /// True if this is equivalent to 'id<P>' for some non-empty set of
5948 /// protocols.
5949 bool isObjCQualifiedIdType() const {
5950 return getObjectType()->isObjCQualifiedId();
5951 }
5952
5953 /// True if this is equivalent to 'Class<P>' for some non-empty set of
5954 /// protocols.
5955 bool isObjCQualifiedClassType() const {
5956 return getObjectType()->isObjCQualifiedClass();
5957 }
5958
5959 /// Whether this is a "__kindof" type.
5960 bool isKindOfType() const { return getObjectType()->isKindOfType(); }
5961
5962 /// Whether this type is specialized, meaning that it has type arguments.
5963 bool isSpecialized() const { return getObjectType()->isSpecialized(); }
5964
5965 /// Whether this type is specialized, meaning that it has type arguments.
5966 bool isSpecializedAsWritten() const {
5967 return getObjectType()->isSpecializedAsWritten();
5968 }
5969
5970 /// Whether this type is unspecialized, meaning that is has no type arguments.
5971 bool isUnspecialized() const { return getObjectType()->isUnspecialized(); }
5972
5973 /// Determine whether this object type is "unspecialized" as
5974 /// written, meaning that it has no type arguments.
5975 bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); }
5976
5977 /// Retrieve the type arguments for this type.
5978 ArrayRef<QualType> getTypeArgs() const {
5979 return getObjectType()->getTypeArgs();
5980 }
5981
5982 /// Retrieve the type arguments for this type.
5983 ArrayRef<QualType> getTypeArgsAsWritten() const {
5984 return getObjectType()->getTypeArgsAsWritten();
5985 }
5986
5987 /// An iterator over the qualifiers on the object type. Provided
5988 /// for convenience. This will always iterate over the full set of
5989 /// protocols on a type, not just those provided directly.
5990 using qual_iterator = ObjCObjectType::qual_iterator;
5991 using qual_range = llvm::iterator_range<qual_iterator>;
5992
5993 qual_range quals() const { return qual_range(qual_begin(), qual_end()); }
5994
5995 qual_iterator qual_begin() const {
5996 return getObjectType()->qual_begin();
5997 }
5998
5999 qual_iterator qual_end() const {
6000 return getObjectType()->qual_end();
6001 }
6002
6003 bool qual_empty() const { return getObjectType()->qual_empty(); }
6004
6005 /// Return the number of qualifying protocols on the object type.
6006 unsigned getNumProtocols() const {
6007 return getObjectType()->getNumProtocols();
6008 }
6009
6010 /// Retrieve a qualifying protocol by index on the object type.
6011 ObjCProtocolDecl *getProtocol(unsigned I) const {
6012 return getObjectType()->getProtocol(I);
6013 }
6014
6015 bool isSugared() const { return false; }
6016 QualType desugar() const { return QualType(this, 0); }
6017
6018 /// Retrieve the type of the superclass of this object pointer type.
6019 ///
6020 /// This operation substitutes any type arguments into the
6021 /// superclass of the current class type, potentially producing a
6022 /// pointer to a specialization of the superclass type. Produces a
6023 /// null type if there is no superclass.
6024 QualType getSuperClassType() const;
6025
6026 /// Strip off the Objective-C "kindof" type and (with it) any
6027 /// protocol qualifiers.
6028 const ObjCObjectPointerType *stripObjCKindOfTypeAndQuals(
6029 const ASTContext &ctx) const;
6030
6031 void Profile(llvm::FoldingSetNodeID &ID) {
6032 Profile(ID, getPointeeType());
6033 }
6034
6035 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) {
6036 ID.AddPointer(T.getAsOpaquePtr());
6037 }
6038
6039 static bool classof(const Type *T) {
6040 return T->getTypeClass() == ObjCObjectPointer;
6041 }
6042};
6043
6044class AtomicType : public Type, public llvm::FoldingSetNode {
6045 friend class ASTContext; // ASTContext creates these.
6046
6047 QualType ValueType;
6048
6049 AtomicType(QualType ValTy, QualType Canonical)
6050 : Type(Atomic, Canonical, ValTy->isDependentType(),
6051 ValTy->isInstantiationDependentType(),
6052 ValTy->isVariablyModifiedType(),
6053 ValTy->containsUnexpandedParameterPack()),
6054 ValueType(ValTy) {}
6055
6056public:
6057 /// Gets the type contained by this atomic type, i.e.
6058 /// the type returned by performing an atomic load of this atomic type.
6059 QualType getValueType() const { return ValueType; }
6060
6061 bool isSugared() const { return false; }
6062 QualType desugar() const { return QualType(this, 0); }
6063
6064 void Profile(llvm::FoldingSetNodeID &ID) {
6065 Profile(ID, getValueType());
6066 }
6067
6068 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) {
6069 ID.AddPointer(T.getAsOpaquePtr());
6070 }
6071
6072 static bool classof(const Type *T) {
6073 return T->getTypeClass() == Atomic;
6074 }
6075};
6076
6077/// PipeType - OpenCL20.
6078class PipeType : public Type, public llvm::FoldingSetNode {
6079 friend class ASTContext; // ASTContext creates these.
6080
6081 QualType ElementType;
6082 bool isRead;
6083
6084 PipeType(QualType elemType, QualType CanonicalPtr, bool isRead)
6085 : Type(Pipe, CanonicalPtr, elemType->isDependentType(),
6086 elemType->isInstantiationDependentType(),
6087 elemType->isVariablyModifiedType(),
6088 elemType->containsUnexpandedParameterPack()),
6089 ElementType(elemType), isRead(isRead) {}
6090
6091public:
6092 QualType getElementType() const { return ElementType; }
6093
6094 bool isSugared() const { return false; }
6095
6096 QualType desugar() const { return QualType(this, 0); }
6097
6098 void Profile(llvm::FoldingSetNodeID &ID) {
6099 Profile(ID, getElementType(), isReadOnly());
6100 }
6101
6102 static void Profile(llvm::FoldingSetNodeID &ID, QualType T, bool isRead) {
6103 ID.AddPointer(T.getAsOpaquePtr());
6104 ID.AddBoolean(isRead);
6105 }
6106
6107 static bool classof(const Type *T) {
6108 return T->getTypeClass() == Pipe;
6109 }
6110
6111 bool isReadOnly() const { return isRead; }
6112};
6113
6114/// A qualifier set is used to build a set of qualifiers.
6115class QualifierCollector : public Qualifiers {
6116public:
6117 QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {}
6118
6119 /// Collect any qualifiers on the given type and return an
6120 /// unqualified type. The qualifiers are assumed to be consistent
6121 /// with those already in the type.
6122 const Type *strip(QualType type) {
6123 addFastQualifiers(type.getLocalFastQualifiers());
6124 if (!type.hasLocalNonFastQualifiers())
6125 return type.getTypePtrUnsafe();
6126
6127 const ExtQuals *extQuals = type.getExtQualsUnsafe();
6128 addConsistentQualifiers(extQuals->getQualifiers());
6129 return extQuals->getBaseType();
6130 }
6131
6132 /// Apply the collected qualifiers to the given type.
6133 QualType apply(const ASTContext &Context, QualType QT) const;
6134
6135 /// Apply the collected qualifiers to the given type.
6136 QualType apply(const ASTContext &Context, const Type* T) const;
6137};
6138
6139// Inline function definitions.
6140
6141inline SplitQualType SplitQualType::getSingleStepDesugaredType() const {
6142 SplitQualType desugar =
6143 Ty->getLocallyUnqualifiedSingleStepDesugaredType().split();
6144 desugar.Quals.addConsistentQualifiers(Quals);
6145 return desugar;
6146}
6147
6148inline const Type *QualType::getTypePtr() const {
6149 return getCommonPtr()->BaseType;
6150}
6151
6152inline const Type *QualType::getTypePtrOrNull() const {
6153 return (isNull() ? nullptr : getCommonPtr()->BaseType);
6154}
6155
6156inline SplitQualType QualType::split() const {
6157 if (!hasLocalNonFastQualifiers())
6158 return SplitQualType(getTypePtrUnsafe(),
6159 Qualifiers::fromFastMask(getLocalFastQualifiers()));
6160
6161 const ExtQuals *eq = getExtQualsUnsafe();
6162 Qualifiers qs = eq->getQualifiers();
6163 qs.addFastQualifiers(getLocalFastQualifiers());
6164 return SplitQualType(eq->getBaseType(), qs);
6165}
6166
6167inline Qualifiers QualType::getLocalQualifiers() const {
6168 Qualifiers Quals;
6169 if (hasLocalNonFastQualifiers())
6170 Quals = getExtQualsUnsafe()->getQualifiers();
6171 Quals.addFastQualifiers(getLocalFastQualifiers());
6172 return Quals;
6173}
6174
6175inline Qualifiers QualType::getQualifiers() const {
6176 Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers();
6177 quals.addFastQualifiers(getLocalFastQualifiers());
6178 return quals;
6179}
6180
6181inline unsigned QualType::getCVRQualifiers() const {
6182 unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers();
6183 cvr |= getLocalCVRQualifiers();
6184 return cvr;
6185}
6186
6187inline QualType QualType::getCanonicalType() const {
6188 QualType canon = getCommonPtr()->CanonicalType;
6189 return canon.withFastQualifiers(getLocalFastQualifiers());
6190}
6191
6192inline bool QualType::isCanonical() const {
6193 return getTypePtr()->isCanonicalUnqualified();
6194}
6195
6196inline bool QualType::isCanonicalAsParam() const {
6197 if (!isCanonical()) return false;
6198 if (hasLocalQualifiers()) return false;
6199
6200 const Type *T = getTypePtr();
6201 if (T->isVariablyModifiedType() && T->hasSizedVLAType())
6202 return false;
6203
6204 return !isa<FunctionType>(T) && !isa<ArrayType>(T);
6205}
6206
6207inline bool QualType::isConstQualified() const {
6208 return isLocalConstQualified() ||
6209 getCommonPtr()->CanonicalType.isLocalConstQualified();
6210}
6211
6212inline bool QualType::isRestrictQualified() const {
6213 return isLocalRestrictQualified() ||
6214 getCommonPtr()->CanonicalType.isLocalRestrictQualified();
6215}
6216
6217
6218inline bool QualType::isVolatileQualified() const {
6219 return isLocalVolatileQualified() ||
6220 getCommonPtr()->CanonicalType.isLocalVolatileQualified();
6221}
6222
6223inline bool QualType::hasQualifiers() const {
6224 return hasLocalQualifiers() ||
6225 getCommonPtr()->CanonicalType.hasLocalQualifiers();
6226}
6227
6228inline QualType QualType::getUnqualifiedType() const {
6229 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers())
6230 return QualType(getTypePtr(), 0);
6231
6232 return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0);
6233}
6234
6235inline SplitQualType QualType::getSplitUnqualifiedType() const {
6236 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers())
6237 return split();
6238
6239 return getSplitUnqualifiedTypeImpl(*this);
6240}
6241
6242inline void QualType::removeLocalConst() {
6243 removeLocalFastQualifiers(Qualifiers::Const);
6244}
6245
6246inline void QualType::removeLocalRestrict() {
6247 removeLocalFastQualifiers(Qualifiers::Restrict);
6248}
6249
6250inline void QualType::removeLocalVolatile() {
6251 removeLocalFastQualifiers(Qualifiers::Volatile);
6252}
6253
6254inline void QualType::removeLocalCVRQualifiers(unsigned Mask) {
6255 assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits")((!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"
) ? static_cast<void> (0) : __assert_fail ("!(Mask & ~Qualifiers::CVRMask) && \"mask has non-CVR bits\""
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 6255, __PRETTY_FUNCTION__))
;
6256 static_assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask,
6257 "Fast bits differ from CVR bits!");
6258
6259 // Fast path: we don't need to touch the slow qualifiers.
6260 removeLocalFastQualifiers(Mask);
6261}
6262
6263/// Return the address space of this type.
6264inline LangAS QualType::getAddressSpace() const {
6265 return getQualifiers().getAddressSpace();
6266}
6267
6268/// Return the gc attribute of this type.
6269inline Qualifiers::GC QualType::getObjCGCAttr() const {
6270 return getQualifiers().getObjCGCAttr();
6271}
6272
6273inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const {
6274 if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
6275 return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD);
6276 return false;
6277}
6278
6279inline bool QualType::hasNonTrivialToPrimitiveDestructCUnion() const {
6280 if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
6281 return hasNonTrivialToPrimitiveDestructCUnion(RD);
6282 return false;
6283}
6284
6285inline bool QualType::hasNonTrivialToPrimitiveCopyCUnion() const {
6286 if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
6287 return hasNonTrivialToPrimitiveCopyCUnion(RD);
6288 return false;
6289}
6290
6291inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) {
6292 if (const auto *PT = t.getAs<PointerType>()) {
6293 if (const auto *FT = PT->getPointeeType()->getAs<FunctionType>())
6294 return FT->getExtInfo();
6295 } else if (const auto *FT = t.getAs<FunctionType>())
6296 return FT->getExtInfo();
6297
6298 return FunctionType::ExtInfo();
6299}
6300
6301inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) {
6302 return getFunctionExtInfo(*t);
6303}
6304
6305/// Determine whether this type is more
6306/// qualified than the Other type. For example, "const volatile int"
6307/// is more qualified than "const int", "volatile int", and
6308/// "int". However, it is not more qualified than "const volatile
6309/// int".
6310inline bool QualType::isMoreQualifiedThan(QualType other) const {
6311 Qualifiers MyQuals = getQualifiers();
6312 Qualifiers OtherQuals = other.getQualifiers();
6313 return (MyQuals != OtherQuals && MyQuals.compatiblyIncludes(OtherQuals));
6314}
6315
6316/// Determine whether this type is at last
6317/// as qualified as the Other type. For example, "const volatile
6318/// int" is at least as qualified as "const int", "volatile int",
6319/// "int", and "const volatile int".
6320inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const {
6321 Qualifiers OtherQuals = other.getQualifiers();
6322
6323 // Ignore __unaligned qualifier if this type is a void.
6324 if (getUnqualifiedType()->isVoidType())
6325 OtherQuals.removeUnaligned();
6326
6327 return getQualifiers().compatiblyIncludes(OtherQuals);
6328}
6329
6330/// If Type is a reference type (e.g., const
6331/// int&), returns the type that the reference refers to ("const
6332/// int"). Otherwise, returns the type itself. This routine is used
6333/// throughout Sema to implement C++ 5p6:
6334///
6335/// If an expression initially has the type "reference to T" (8.3.2,
6336/// 8.5.3), the type is adjusted to "T" prior to any further
6337/// analysis, the expression designates the object or function
6338/// denoted by the reference, and the expression is an lvalue.
6339inline QualType QualType::getNonReferenceType() const {
6340 if (const auto *RefType = (*this)->getAs<ReferenceType>())
6341 return RefType->getPointeeType();
6342 else
6343 return *this;
6344}
6345
6346inline bool QualType::isCForbiddenLValueType() const {
6347 return ((getTypePtr()->isVoidType() && !hasQualifiers()) ||
6348 getTypePtr()->isFunctionType());
6349}
6350
6351/// Tests whether the type is categorized as a fundamental type.
6352///
6353/// \returns True for types specified in C++0x [basic.fundamental].
6354inline bool Type::isFundamentalType() const {
6355 return isVoidType() ||
6356 isNullPtrType() ||
6357 // FIXME: It's really annoying that we don't have an
6358 // 'isArithmeticType()' which agrees with the standard definition.
6359 (isArithmeticType() && !isEnumeralType());
6360}
6361
6362/// Tests whether the type is categorized as a compound type.
6363///
6364/// \returns True for types specified in C++0x [basic.compound].
6365inline bool Type::isCompoundType() const {
6366 // C++0x [basic.compound]p1:
6367 // Compound types can be constructed in the following ways:
6368 // -- arrays of objects of a given type [...];
6369 return isArrayType() ||
6370 // -- functions, which have parameters of given types [...];
6371 isFunctionType() ||
6372 // -- pointers to void or objects or functions [...];
6373 isPointerType() ||
6374 // -- references to objects or functions of a given type. [...]
6375 isReferenceType() ||
6376 // -- classes containing a sequence of objects of various types, [...];
6377 isRecordType() ||
6378 // -- unions, which are classes capable of containing objects of different
6379 // types at different times;
6380 isUnionType() ||
6381 // -- enumerations, which comprise a set of named constant values. [...];
6382 isEnumeralType() ||
6383 // -- pointers to non-static class members, [...].
6384 isMemberPointerType();
6385}
6386
6387inline bool Type::isFunctionType() const {
6388 return isa<FunctionType>(CanonicalType);
6389}
6390
6391inline bool Type::isPointerType() const {
6392 return isa<PointerType>(CanonicalType);
6393}
6394
6395inline bool Type::isAnyPointerType() const {
6396 return isPointerType() || isObjCObjectPointerType();
6397}
6398
6399inline bool Type::isBlockPointerType() const {
6400 return isa<BlockPointerType>(CanonicalType);
6401}
6402
6403inline bool Type::isReferenceType() const {
6404 return isa<ReferenceType>(CanonicalType);
6405}
6406
6407inline bool Type::isLValueReferenceType() const {
6408 return isa<LValueReferenceType>(CanonicalType);
6409}
6410
6411inline bool Type::isRValueReferenceType() const {
6412 return isa<RValueReferenceType>(CanonicalType);
6413}
6414
6415inline bool Type::isFunctionPointerType() const {
6416 if (const auto *T = getAs<PointerType>())
6417 return T->getPointeeType()->isFunctionType();
6418 else
6419 return false;
6420}
6421
6422inline bool Type::isFunctionReferenceType() const {
6423 if (const auto *T = getAs<ReferenceType>())
6424 return T->getPointeeType()->isFunctionType();
6425 else
6426 return false;
6427}
6428
6429inline bool Type::isMemberPointerType() const {
6430 return isa<MemberPointerType>(CanonicalType);
6431}
6432
6433inline bool Type::isMemberFunctionPointerType() const {
6434 if (const auto *T = getAs<MemberPointerType>())
6435 return T->isMemberFunctionPointer();
6436 else
6437 return false;
6438}
6439
6440inline bool Type::isMemberDataPointerType() const {
6441 if (const auto *T = getAs<MemberPointerType>())
6442 return T->isMemberDataPointer();
6443 else
6444 return false;
6445}
6446
6447inline bool Type::isArrayType() const {
6448 return isa<ArrayType>(CanonicalType);
6449}
6450
6451inline bool Type::isConstantArrayType() const {
6452 return isa<ConstantArrayType>(CanonicalType);
6453}
6454
6455inline bool Type::isIncompleteArrayType() const {
6456 return isa<IncompleteArrayType>(CanonicalType);
6457}
6458
6459inline bool Type::isVariableArrayType() const {
6460 return isa<VariableArrayType>(CanonicalType);
6461}
6462
6463inline bool Type::isDependentSizedArrayType() const {
6464 return isa<DependentSizedArrayType>(CanonicalType);
6465}
6466
6467inline bool Type::isBuiltinType() const {
6468 return isa<BuiltinType>(CanonicalType);
6469}
6470
6471inline bool Type::isRecordType() const {
6472 return isa<RecordType>(CanonicalType);
6473}
6474
6475inline bool Type::isEnumeralType() const {
6476 return isa<EnumType>(CanonicalType);
6477}
6478
6479inline bool Type::isAnyComplexType() const {
6480 return isa<ComplexType>(CanonicalType);
6481}
6482
6483inline bool Type::isVectorType() const {
6484 return isa<VectorType>(CanonicalType);
6485}
6486
6487inline bool Type::isExtVectorType() const {
6488 return isa<ExtVectorType>(CanonicalType);
6489}
6490
6491inline bool Type::isDependentAddressSpaceType() const {
6492 return isa<DependentAddressSpaceType>(CanonicalType);
6493}
6494
6495inline bool Type::isObjCObjectPointerType() const {
6496 return isa<ObjCObjectPointerType>(CanonicalType);
6497}
6498
6499inline bool Type::isObjCObjectType() const {
6500 return isa<ObjCObjectType>(CanonicalType);
6501}
6502
6503inline bool Type::isObjCObjectOrInterfaceType() const {
6504 return isa<ObjCInterfaceType>(CanonicalType) ||
6505 isa<ObjCObjectType>(CanonicalType);
6506}
6507
6508inline bool Type::isAtomicType() const {
6509 return isa<AtomicType>(CanonicalType);
6510}
6511
6512inline bool Type::isObjCQualifiedIdType() const {
6513 if (const auto *OPT = getAs<ObjCObjectPointerType>())
6514 return OPT->isObjCQualifiedIdType();
6515 return false;
6516}
6517
6518inline bool Type::isObjCQualifiedClassType() const {
6519 if (const auto *OPT = getAs<ObjCObjectPointerType>())
6520 return OPT->isObjCQualifiedClassType();
6521 return false;
6522}
6523
6524inline bool Type::isObjCIdType() const {
6525 if (const auto *OPT = getAs<ObjCObjectPointerType>())
6526 return OPT->isObjCIdType();
6527 return false;
6528}
6529
6530inline bool Type::isObjCClassType() const {
6531 if (const auto *OPT = getAs<ObjCObjectPointerType>())
6532 return OPT->isObjCClassType();
6533 return false;
6534}
6535
6536inline bool Type::isObjCSelType() const {
6537 if (const auto *OPT = getAs<PointerType>())
6538 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel);
6539 return false;
6540}
6541
6542inline bool Type::isObjCBuiltinType() const {
6543 return isObjCIdType() || isObjCClassType() || isObjCSelType();
6544}
6545
6546inline bool Type::isDecltypeType() const {
6547 return isa<DecltypeType>(this);
6548}
6549
6550#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6551 inline bool Type::is##Id##Type() const { \
6552 return isSpecificBuiltinType(BuiltinType::Id); \
6553 }
6554#include "clang/Basic/OpenCLImageTypes.def"
6555
6556inline bool Type::isSamplerT() const {
6557 return isSpecificBuiltinType(BuiltinType::OCLSampler);
6558}
6559
6560inline bool Type::isEventT() const {
6561 return isSpecificBuiltinType(BuiltinType::OCLEvent);
6562}
6563
6564inline bool Type::isClkEventT() const {
6565 return isSpecificBuiltinType(BuiltinType::OCLClkEvent);
6566}
6567
6568inline bool Type::isQueueT() const {
6569 return isSpecificBuiltinType(BuiltinType::OCLQueue);
6570}
6571
6572inline bool Type::isReserveIDT() const {
6573 return isSpecificBuiltinType(BuiltinType::OCLReserveID);
6574}
6575
6576inline bool Type::isImageType() const {
6577#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) is##Id##Type() ||
6578 return
6579#include "clang/Basic/OpenCLImageTypes.def"
6580 false; // end boolean or operation
6581}
6582
6583inline bool Type::isPipeType() const {
6584 return isa<PipeType>(CanonicalType);
6585}
6586
6587#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6588 inline bool Type::is##Id##Type() const { \
6589 return isSpecificBuiltinType(BuiltinType::Id); \
6590 }
6591#include "clang/Basic/OpenCLExtensionTypes.def"
6592
6593inline bool Type::isOCLIntelSubgroupAVCType() const {
6594#define INTEL_SUBGROUP_AVC_TYPE(ExtType, Id) \
6595 isOCLIntelSubgroupAVC##Id##Type() ||
6596 return
6597#include "clang/Basic/OpenCLExtensionTypes.def"
6598 false; // end of boolean or operation
6599}
6600
6601inline bool Type::isOCLExtOpaqueType() const {
6602#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) is##Id##Type() ||
6603 return
6604#include "clang/Basic/OpenCLExtensionTypes.def"
6605 false; // end of boolean or operation
6606}
6607
6608inline bool Type::isOpenCLSpecificType() const {
6609 return isSamplerT() || isEventT() || isImageType() || isClkEventT() ||
6610 isQueueT() || isReserveIDT() || isPipeType() || isOCLExtOpaqueType();
6611}
6612
6613inline bool Type::isTemplateTypeParmType() const {
6614 return isa<TemplateTypeParmType>(CanonicalType);
6615}
6616
6617inline bool Type::isSpecificBuiltinType(unsigned K) const {
6618 if (const BuiltinType *BT = getAs<BuiltinType>())
6619 if (BT->getKind() == (BuiltinType::Kind) K)
6620 return true;
6621 return false;
6622}
6623
6624inline bool Type::isPlaceholderType() const {
6625 if (const auto *BT = dyn_cast<BuiltinType>(this))
6626 return BT->isPlaceholderType();
6627 return false;
6628}
6629
6630inline const BuiltinType *Type::getAsPlaceholderType() const {
6631 if (const auto *BT = dyn_cast<BuiltinType>(this))
6632 if (BT->isPlaceholderType())
6633 return BT;
6634 return nullptr;
6635}
6636
6637inline bool Type::isSpecificPlaceholderType(unsigned K) const {
6638 assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K))((BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)) ?
static_cast<void> (0) : __assert_fail ("BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)"
, "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 6638, __PRETTY_FUNCTION__))
;
6639 if (const auto *BT = dyn_cast<BuiltinType>(this))
6640 return (BT->getKind() == (BuiltinType::Kind) K);
6641 return false;
6642}
6643
6644inline bool Type::isNonOverloadPlaceholderType() const {
6645 if (const auto *BT = dyn_cast<BuiltinType>(this))
6646 return BT->isNonOverloadPlaceholderType();
6647 return false;
6648}
6649
6650inline bool Type::isVoidType() const {
6651 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
6652 return BT->getKind() == BuiltinType::Void;
6653 return false;
6654}
6655
6656inline bool Type::isHalfType() const {
6657 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
6658 return BT->getKind() == BuiltinType::Half;
6659 // FIXME: Should we allow complex __fp16? Probably not.
6660 return false;
6661}
6662
6663inline bool Type::isFloat16Type() const {
6664 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
6665 return BT->getKind() == BuiltinType::Float16;
6666 return false;
6667}
6668
6669inline bool Type::isFloat128Type() const {
6670 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
6671 return BT->getKind() == BuiltinType::Float128;
6672 return false;
6673}
6674
6675inline bool Type::isNullPtrType() const {
6676 if (const auto *BT = getAs<BuiltinType>())
6677 return BT->getKind() == BuiltinType::NullPtr;
6678 return false;
6679}
6680
6681bool IsEnumDeclComplete(EnumDecl *);
6682bool IsEnumDeclScoped(EnumDecl *);
6683
6684inline bool Type::isIntegerType() const {
6685 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
6686 return BT->getKind() >= BuiltinType::Bool &&
6687 BT->getKind() <= BuiltinType::Int128;
6688 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
6689 // Incomplete enum types are not treated as integer types.
6690 // FIXME: In C++, enum types are never integer types.
6691 return IsEnumDeclComplete(ET->getDecl()) &&
6692 !IsEnumDeclScoped(ET->getDecl());
6693 }
6694 return false;
6695}
6696
6697inline bool Type::isFixedPointType() const {
6698 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) {
6699 return BT->getKind() >= BuiltinType::ShortAccum &&
6700 BT->getKind() <= BuiltinType::SatULongFract;
6701 }
6702 return false;
6703}
6704
6705inline bool Type::isFixedPointOrIntegerType() const {
6706 return isFixedPointType() || isIntegerType();
6707}
6708
6709inline bool Type::isSaturatedFixedPointType() const {
6710 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) {
6711 return BT->getKind() >= BuiltinType::SatShortAccum &&
6712 BT->getKind() <= BuiltinType::SatULongFract;
6713 }
6714 return false;
6715}
6716
6717inline bool Type::isUnsaturatedFixedPointType() const {
6718 return isFixedPointType() && !isSaturatedFixedPointType();
6719}
6720
6721inline bool Type::isSignedFixedPointType() const {
6722 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) {
6723 return ((BT->getKind() >= BuiltinType::ShortAccum &&
6724 BT->getKind() <= BuiltinType::LongAccum) ||
6725 (BT->getKind() >= BuiltinType::ShortFract &&
6726 BT->getKind() <= BuiltinType::LongFract) ||
6727 (BT->getKind() >= BuiltinType::SatShortAccum &&
6728 BT->getKind() <= BuiltinType::SatLongAccum) ||
6729 (BT->getKind() >= BuiltinType::SatShortFract &&
6730 BT->getKind() <= BuiltinType::SatLongFract));
6731 }
6732 return false;
6733}
6734
6735inline bool Type::isUnsignedFixedPointType() const {
6736 return isFixedPointType() && !isSignedFixedPointType();
6737}
6738
6739inline bool Type::isScalarType() const {
6740 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
6741 return BT->getKind() > BuiltinType::Void &&
6742 BT->getKind() <= BuiltinType::NullPtr;
6743 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
6744 // Enums are scalar types, but only if they are defined. Incomplete enums
6745 // are not treated as scalar types.
6746 return IsEnumDeclComplete(ET->getDecl());
6747 return isa<PointerType>(CanonicalType) ||
6748 isa<BlockPointerType>(CanonicalType) ||
6749 isa<MemberPointerType>(CanonicalType) ||
6750 isa<ComplexType>(CanonicalType) ||
6751 isa<ObjCObjectPointerType>(CanonicalType);
6752}
6753
6754inline bool Type::isIntegralOrEnumerationType() const {
6755 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
6756 return BT->getKind() >= BuiltinType::Bool &&
6757 BT->getKind() <= BuiltinType::Int128;
6758
6759 // Check for a complete enum type; incomplete enum types are not properly an
6760 // enumeration type in the sense required here.
6761 if (const auto *ET = dyn_cast<EnumType>(CanonicalType))
6762 return IsEnumDeclComplete(ET->getDecl());
6763
6764 return false;
6765}
6766
6767inline bool Type::isBooleanType() const {
6768 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
6769 return BT->getKind() == BuiltinType::Bool;
6770 return false;
6771}
6772
6773inline bool Type::isUndeducedType() const {
6774 auto *DT = getContainedDeducedType();
6775 return DT && !DT->isDeduced();
6776}
6777
6778/// Determines whether this is a type for which one can define
6779/// an overloaded operator.
6780inline bool Type::isOverloadableType() const {
6781 return isDependentType() || isRecordType() || isEnumeralType();
6782}
6783
6784/// Determines whether this type can decay to a pointer type.
6785inline bool Type::canDecayToPointerType() const {
6786 return isFunctionType() || isArrayType();
6787}
6788
6789inline bool Type::hasPointerRepresentation() const {
6790 return (isPointerType() || isReferenceType() || isBlockPointerType() ||
6791 isObjCObjectPointerType() || isNullPtrType());
6792}
6793
6794inline bool Type::hasObjCPointerRepresentation() const {
6795 return isObjCObjectPointerType();
6796}
6797
6798inline const Type *Type::getBaseElementTypeUnsafe() const {
6799 const Type *type = this;
6800 while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe())
6801 type = arrayType->getElementType().getTypePtr();
6802 return type;
6803}
6804
6805inline const Type *Type::getPointeeOrArrayElementType() const {
6806 const Type *type = this;
6807 if (type->isAnyPointerType())
6808 return type->getPointeeType().getTypePtr();
6809 else if (type->isArrayType())
6810 return type->getBaseElementTypeUnsafe();
6811 return type;
6812}
6813
6814/// Insertion operator for diagnostics. This allows sending Qualifiers into a
6815/// diagnostic with <<.
6816inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
6817 Qualifiers Q) {
6818 DB.AddTaggedVal(Q.getAsOpaqueValue(),
6819 DiagnosticsEngine::ArgumentKind::ak_qual);
6820 return DB;
6821}
6822
6823/// Insertion operator for partial diagnostics. This allows sending Qualifiers
6824/// into a diagnostic with <<.
6825inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
6826 Qualifiers Q) {
6827 PD.AddTaggedVal(Q.getAsOpaqueValue(),
6828 DiagnosticsEngine::ArgumentKind::ak_qual);
6829 return PD;
6830}
6831
6832/// Insertion operator for diagnostics. This allows sending QualType's into a
6833/// diagnostic with <<.
6834inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
6835 QualType T) {
6836 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
6837 DiagnosticsEngine::ak_qualtype);
6838 return DB;
6839}
6840
6841/// Insertion operator for partial diagnostics. This allows sending QualType's
6842/// into a diagnostic with <<.
6843inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
6844 QualType T) {
6845 PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
6846 DiagnosticsEngine::ak_qualtype);
6847 return PD;
6848}
6849
6850// Helper class template that is used by Type::getAs to ensure that one does
6851// not try to look through a qualified type to get to an array type.
6852template <typename T>
6853using TypeIsArrayType =
6854 std::integral_constant<bool, std::is_same<T, ArrayType>::value ||
6855 std::is_base_of<ArrayType, T>::value>;
6856
6857// Member-template getAs<specific type>'.
6858template <typename T> const T *Type::getAs() const {
6859 static_assert(!TypeIsArrayType<T>::value,
6860 "ArrayType cannot be used with getAs!");
6861
6862 // If this is directly a T type, return it.
6863 if (const auto *Ty = dyn_cast<T>(this))
6864 return Ty;
6865
6866 // If the canonical form of this type isn't the right kind, reject it.
6867 if (!isa<T>(CanonicalType))
6868 return nullptr;
6869
6870 // If this is a typedef for the type, strip the typedef off without
6871 // losing all typedef information.
6872 return cast<T>(getUnqualifiedDesugaredType());
6873}
6874
6875template <typename T> const T *Type::getAsAdjusted() const {
6876 static_assert(!TypeIsArrayType<T>::value, "ArrayType cannot be used with getAsAdjusted!");
6877
6878 // If this is directly a T type, return it.
6879 if (const auto *Ty = dyn_cast<T>(this))
6880 return Ty;
6881
6882 // If the canonical form of this type isn't the right kind, reject it.
6883 if (!isa<T>(CanonicalType))
6884 return nullptr;
6885
6886 // Strip off type adjustments that do not modify the underlying nature of the
6887 // type.
6888 const Type *Ty = this;
6889 while (Ty) {
6890 if (const auto *A = dyn_cast<AttributedType>(Ty))
6891 Ty = A->getModifiedType().getTypePtr();
6892 else if (const auto *E = dyn_cast<ElaboratedType>(Ty))
6893 Ty = E->desugar().getTypePtr();
6894 else if (const auto *P = dyn_cast<ParenType>(Ty))
6895 Ty = P->desugar().getTypePtr();
6896 else if (const auto *A = dyn_cast<AdjustedType>(Ty))
6897 Ty = A->desugar().getTypePtr();
6898 else if (const auto *M = dyn_cast<MacroQualifiedType>(Ty))
6899 Ty = M->desugar().getTypePtr();
6900 else
6901 break;
6902 }
6903
6904 // Just because the canonical type is correct does not mean we can use cast<>,
6905 // since we may not have stripped off all the sugar down to the base type.
6906 return dyn_cast<T>(Ty);
6907}
6908
6909inline const ArrayType *Type::getAsArrayTypeUnsafe() const {
6910 // If this is directly an array type, return it.
6911 if (const auto *arr = dyn_cast<ArrayType>(this))
6912 return arr;
6913
6914 // If the canonical form of this type isn't the right kind, reject it.
6915 if (!isa<ArrayType>(CanonicalType))
6916 return nullptr;
6917
6918 // If this is a typedef for the type, strip the typedef off without
6919 // losing all typedef information.
6920 return cast<ArrayType>(getUnqualifiedDesugaredType());
6921}
6922
6923template <typename T> const T *Type::castAs() const {
6924 static_assert(!TypeIsArrayType<T>::value,
6925 "ArrayType cannot be used with castAs!");
6926
6927 if (const auto *ty = dyn_cast<T>(this)) return ty;
6928 assert(isa<T>(CanonicalType))((isa<T>(CanonicalType)) ? static_cast<void> (0) :
__assert_fail ("isa<T>(CanonicalType)", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 6928, __PRETTY_FUNCTION__))
;
6929 return cast<T>(getUnqualifiedDesugaredType());
6930}
6931
6932inline const ArrayType *Type::castAsArrayTypeUnsafe() const {
6933 assert(isa<ArrayType>(CanonicalType))((isa<ArrayType>(CanonicalType)) ? static_cast<void>
(0) : __assert_fail ("isa<ArrayType>(CanonicalType)", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 6933, __PRETTY_FUNCTION__))
;
6934 if (const auto *arr = dyn_cast<ArrayType>(this)) return arr;
6935 return cast<ArrayType>(getUnqualifiedDesugaredType());
6936}
6937
6938DecayedType::DecayedType(QualType OriginalType, QualType DecayedPtr,
6939 QualType CanonicalPtr)
6940 : AdjustedType(Decayed, OriginalType, DecayedPtr, CanonicalPtr) {
6941#ifndef NDEBUG
6942 QualType Adjusted = getAdjustedType();
6943 (void)AttributedType::stripOuterNullability(Adjusted);
6944 assert(isa<PointerType>(Adjusted))((isa<PointerType>(Adjusted)) ? static_cast<void>
(0) : __assert_fail ("isa<PointerType>(Adjusted)", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include/clang/AST/Type.h"
, 6944, __PRETTY_FUNCTION__))
;
6945#endif
6946}
6947
6948QualType DecayedType::getPointeeType() const {
6949 QualType Decayed = getDecayedType();
6950 (void)AttributedType::stripOuterNullability(Decayed);
6951 return cast<PointerType>(Decayed)->getPointeeType();
6952}
6953
6954// Get the decimal string representation of a fixed point type, represented
6955// as a scaled integer.
6956// TODO: At some point, we should change the arguments to instead just accept an
6957// APFixedPoint instead of APSInt and scale.
6958void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val,
6959 unsigned Scale);
6960
6961} // namespace clang
6962
6963#endif // LLVM_CLANG_AST_TYPE_H

/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/ADT/PointerIntPair.h

1//===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- C++ -*-===//
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 defines the PointerIntPair class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ADT_POINTERINTPAIR_H
14#define LLVM_ADT_POINTERINTPAIR_H
15
16#include "llvm/Support/PointerLikeTypeTraits.h"
17#include "llvm/Support/type_traits.h"
18#include <cassert>
19#include <cstdint>
20#include <limits>
21
22namespace llvm {
23
24template <typename T> struct DenseMapInfo;
25template <typename PointerT, unsigned IntBits, typename PtrTraits>
26struct PointerIntPairInfo;
27
28/// PointerIntPair - This class implements a pair of a pointer and small
29/// integer. It is designed to represent this in the space required by one
30/// pointer by bitmangling the integer into the low part of the pointer. This
31/// can only be done for small integers: typically up to 3 bits, but it depends
32/// on the number of bits available according to PointerLikeTypeTraits for the
33/// type.
34///
35/// Note that PointerIntPair always puts the IntVal part in the highest bits
36/// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
37/// the bool into bit #2, not bit #0, which allows the low two bits to be used
38/// for something else. For example, this allows:
39/// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
40/// ... and the two bools will land in different bits.
41template <typename PointerTy, unsigned IntBits, typename IntType = unsigned,
42 typename PtrTraits = PointerLikeTypeTraits<PointerTy>,
43 typename Info = PointerIntPairInfo<PointerTy, IntBits, PtrTraits>>
44class PointerIntPair {
45 // Used by MSVC visualizer and generally helpful for debugging/visualizing.
46 using InfoTy = Info;
47 intptr_t Value = 0;
48
49public:
50 constexpr PointerIntPair() = default;
51
52 PointerIntPair(PointerTy PtrVal, IntType IntVal) {
53 setPointerAndInt(PtrVal, IntVal);
54 }
55
56 explicit PointerIntPair(PointerTy PtrVal) { initWithPointer(PtrVal); }
57
58 PointerTy getPointer() const { return Info::getPointer(Value); }
59
60 IntType getInt() const { return (IntType)Info::getInt(Value); }
61
62 void setPointer(PointerTy PtrVal) {
63 Value = Info::updatePointer(Value, PtrVal);
64 }
65
66 void setInt(IntType IntVal) {
67 Value = Info::updateInt(Value, static_cast<intptr_t>(IntVal));
68 }
69
70 void initWithPointer(PointerTy PtrVal) {
71 Value = Info::updatePointer(0, PtrVal);
72 }
73
74 void setPointerAndInt(PointerTy PtrVal, IntType IntVal) {
75 Value = Info::updateInt(Info::updatePointer(0, PtrVal),
76 static_cast<intptr_t>(IntVal));
77 }
78
79 PointerTy const *getAddrOfPointer() const {
80 return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
81 }
82
83 PointerTy *getAddrOfPointer() {
84 assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&((Value == reinterpret_cast<intptr_t>(getPointer()) &&
"Can only return the address if IntBits is cleared and " "PtrTraits doesn't change the pointer"
) ? static_cast<void> (0) : __assert_fail ("Value == reinterpret_cast<intptr_t>(getPointer()) && \"Can only return the address if IntBits is cleared and \" \"PtrTraits doesn't change the pointer\""
, "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/ADT/PointerIntPair.h"
, 86, __PRETTY_FUNCTION__))
85 "Can only return the address if IntBits is cleared and "((Value == reinterpret_cast<intptr_t>(getPointer()) &&
"Can only return the address if IntBits is cleared and " "PtrTraits doesn't change the pointer"
) ? static_cast<void> (0) : __assert_fail ("Value == reinterpret_cast<intptr_t>(getPointer()) && \"Can only return the address if IntBits is cleared and \" \"PtrTraits doesn't change the pointer\""
, "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/ADT/PointerIntPair.h"
, 86, __PRETTY_FUNCTION__))
86 "PtrTraits doesn't change the pointer")((Value == reinterpret_cast<intptr_t>(getPointer()) &&
"Can only return the address if IntBits is cleared and " "PtrTraits doesn't change the pointer"
) ? static_cast<void> (0) : __assert_fail ("Value == reinterpret_cast<intptr_t>(getPointer()) && \"Can only return the address if IntBits is cleared and \" \"PtrTraits doesn't change the pointer\""
, "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/ADT/PointerIntPair.h"
, 86, __PRETTY_FUNCTION__))
;
87 return reinterpret_cast<PointerTy *>(&Value);
88 }
89
90 void *getOpaqueValue() const { return reinterpret_cast<void *>(Value); }
91
92 void setFromOpaqueValue(void *Val) {
93 Value = reinterpret_cast<intptr_t>(Val);
94 }
95
96 static PointerIntPair getFromOpaqueValue(void *V) {
97 PointerIntPair P;
98 P.setFromOpaqueValue(V);
99 return P;
100 }
101
102 // Allow PointerIntPairs to be created from const void * if and only if the
103 // pointer type could be created from a const void *.
104 static PointerIntPair getFromOpaqueValue(const void *V) {
105 (void)PtrTraits::getFromVoidPointer(V);
106 return getFromOpaqueValue(const_cast<void *>(V));
107 }
108
109 bool operator==(const PointerIntPair &RHS) const {
110 return Value == RHS.Value;
9
Assuming 'Value' is not equal to 'RHS.Value'
10
Returning zero, which participates in a condition later
111 }
112
113 bool operator!=(const PointerIntPair &RHS) const {
114 return Value != RHS.Value;
115 }
116
117 bool operator<(const PointerIntPair &RHS) const { return Value < RHS.Value; }
118 bool operator>(const PointerIntPair &RHS) const { return Value > RHS.Value; }
119
120 bool operator<=(const PointerIntPair &RHS) const {
121 return Value <= RHS.Value;
122 }
123
124 bool operator>=(const PointerIntPair &RHS) const {
125 return Value >= RHS.Value;
126 }
127};
128
129// Specialize is_trivially_copyable to avoid limitation of llvm::is_trivially_copyable
130// when compiled with gcc 4.9.
131template <typename PointerTy, unsigned IntBits, typename IntType,
132 typename PtrTraits,
133 typename Info>
134struct is_trivially_copyable<PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>> : std::true_type {
135#ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE
136 static_assert(std::is_trivially_copyable<PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>::value,
137 "inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable");
138#endif
139};
140
141
142template <typename PointerT, unsigned IntBits, typename PtrTraits>
143struct PointerIntPairInfo {
144 static_assert(PtrTraits::NumLowBitsAvailable <
145 std::numeric_limits<uintptr_t>::digits,
146 "cannot use a pointer type that has all bits free");
147 static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
148 "PointerIntPair with integer size too large for pointer");
149 enum : uintptr_t {
150 /// PointerBitMask - The bits that come from the pointer.
151 PointerBitMask =
152 ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
153
154 /// IntShift - The number of low bits that we reserve for other uses, and
155 /// keep zero.
156 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
157
158 /// IntMask - This is the unshifted mask for valid bits of the int type.
159 IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1),
160
161 // ShiftedIntMask - This is the bits for the integer shifted in place.
162 ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
163 };
164
165 static PointerT getPointer(intptr_t Value) {
166 return PtrTraits::getFromVoidPointer(
167 reinterpret_cast<void *>(Value & PointerBitMask));
168 }
169
170 static intptr_t getInt(intptr_t Value) {
171 return (Value >> IntShift) & IntMask;
172 }
173
174 static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr) {
175 intptr_t PtrWord =
176 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
177 assert((PtrWord & ~PointerBitMask) == 0 &&(((PtrWord & ~PointerBitMask) == 0 && "Pointer is not sufficiently aligned"
) ? static_cast<void> (0) : __assert_fail ("(PtrWord & ~PointerBitMask) == 0 && \"Pointer is not sufficiently aligned\""
, "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/ADT/PointerIntPair.h"
, 178, __PRETTY_FUNCTION__))
178 "Pointer is not sufficiently aligned")(((PtrWord & ~PointerBitMask) == 0 && "Pointer is not sufficiently aligned"
) ? static_cast<void> (0) : __assert_fail ("(PtrWord & ~PointerBitMask) == 0 && \"Pointer is not sufficiently aligned\""
, "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/ADT/PointerIntPair.h"
, 178, __PRETTY_FUNCTION__))
;
179 // Preserve all low bits, just update the pointer.
180 return PtrWord | (OrigValue & ~PointerBitMask);
181 }
182
183 static intptr_t updateInt(intptr_t OrigValue, intptr_t Int) {
184 intptr_t IntWord = static_cast<intptr_t>(Int);
185 assert((IntWord & ~IntMask) == 0 && "Integer too large for field")(((IntWord & ~IntMask) == 0 && "Integer too large for field"
) ? static_cast<void> (0) : __assert_fail ("(IntWord & ~IntMask) == 0 && \"Integer too large for field\""
, "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/ADT/PointerIntPair.h"
, 185, __PRETTY_FUNCTION__))
;
186
187 // Preserve all bits other than the ones we are updating.
188 return (OrigValue & ~ShiftedIntMask) | IntWord << IntShift;
189 }
190};
191
192// Provide specialization of DenseMapInfo for PointerIntPair.
193template <typename PointerTy, unsigned IntBits, typename IntType>
194struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType>> {
195 using Ty = PointerIntPair<PointerTy, IntBits, IntType>;
196
197 static Ty getEmptyKey() {
198 uintptr_t Val = static_cast<uintptr_t>(-1);
199 Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
200 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
201 }
202
203 static Ty getTombstoneKey() {
204 uintptr_t Val = static_cast<uintptr_t>(-2);
205 Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
206 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
207 }
208
209 static unsigned getHashValue(Ty V) {
210 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
211 return unsigned(IV) ^ unsigned(IV >> 9);
212 }
213
214 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
215};
216
217// Teach SmallPtrSet that PointerIntPair is "basically a pointer".
218template <typename PointerTy, unsigned IntBits, typename IntType,
219 typename PtrTraits>
220struct PointerLikeTypeTraits<
221 PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
222 static inline void *
223 getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
224 return P.getOpaqueValue();
225 }
226
227 static inline PointerIntPair<PointerTy, IntBits, IntType>
228 getFromVoidPointer(void *P) {
229 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
230 }
231
232 static inline PointerIntPair<PointerTy, IntBits, IntType>
233 getFromVoidPointer(const void *P) {
234 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
235 }
236
237 enum { NumLowBitsAvailable = PtrTraits::NumLowBitsAvailable - IntBits };
238};
239
240} // end namespace llvm
241
242#endif // LLVM_ADT_POINTERINTPAIR_H