1f4a2713aSLionel Sambuc //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the ASTContext interface.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
15f4a2713aSLionel Sambuc #include "CXXABI.h"
16f4a2713aSLionel Sambuc #include "clang/AST/ASTMutationListener.h"
17f4a2713aSLionel Sambuc #include "clang/AST/Attr.h"
18f4a2713aSLionel Sambuc #include "clang/AST/CharUnits.h"
19f4a2713aSLionel Sambuc #include "clang/AST/Comment.h"
20f4a2713aSLionel Sambuc #include "clang/AST/CommentCommandTraits.h"
21f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
22f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
23f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
24f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
25f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
26f4a2713aSLionel Sambuc #include "clang/AST/ExternalASTSource.h"
27f4a2713aSLionel Sambuc #include "clang/AST/Mangle.h"
28f4a2713aSLionel Sambuc #include "clang/AST/MangleNumberingContext.h"
29f4a2713aSLionel Sambuc #include "clang/AST/RecordLayout.h"
30f4a2713aSLionel Sambuc #include "clang/AST/RecursiveASTVisitor.h"
31f4a2713aSLionel Sambuc #include "clang/AST/TypeLoc.h"
32*0a6a1f1dSLionel Sambuc #include "clang/AST/VTableBuilder.h"
33f4a2713aSLionel Sambuc #include "clang/Basic/Builtins.h"
34f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
35f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
36f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
37f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
38f4a2713aSLionel Sambuc #include "llvm/ADT/Triple.h"
39f4a2713aSLionel Sambuc #include "llvm/Support/Capacity.h"
40f4a2713aSLionel Sambuc #include "llvm/Support/MathExtras.h"
41f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
42f4a2713aSLionel Sambuc #include <map>
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc using namespace clang;
45f4a2713aSLionel Sambuc
46f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitDefaultConstructors;
47f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
48f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitCopyConstructors;
49f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitCopyConstructorsDeclared;
50f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitMoveConstructors;
51f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitMoveConstructorsDeclared;
52f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitCopyAssignmentOperators;
53f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
54f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitMoveAssignmentOperators;
55f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared;
56f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitDestructors;
57f4a2713aSLionel Sambuc unsigned ASTContext::NumImplicitDestructorsDeclared;
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc enum FloatingRank {
60f4a2713aSLionel Sambuc HalfRank, FloatRank, DoubleRank, LongDoubleRank
61f4a2713aSLionel Sambuc };
62f4a2713aSLionel Sambuc
getRawCommentForDeclNoCache(const Decl * D) const63f4a2713aSLionel Sambuc RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
64f4a2713aSLionel Sambuc if (!CommentsLoaded && ExternalSource) {
65f4a2713aSLionel Sambuc ExternalSource->ReadComments();
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
68*0a6a1f1dSLionel Sambuc ArrayRef<RawComment *> RawComments = Comments.getComments();
69*0a6a1f1dSLionel Sambuc assert(std::is_sorted(RawComments.begin(), RawComments.end(),
70*0a6a1f1dSLionel Sambuc BeforeThanCompare<RawComment>(SourceMgr)));
71*0a6a1f1dSLionel Sambuc #endif
72*0a6a1f1dSLionel Sambuc
73f4a2713aSLionel Sambuc CommentsLoaded = true;
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc
76f4a2713aSLionel Sambuc assert(D);
77f4a2713aSLionel Sambuc
78f4a2713aSLionel Sambuc // User can not attach documentation to implicit declarations.
79f4a2713aSLionel Sambuc if (D->isImplicit())
80*0a6a1f1dSLionel Sambuc return nullptr;
81f4a2713aSLionel Sambuc
82f4a2713aSLionel Sambuc // User can not attach documentation to implicit instantiations.
83f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
84f4a2713aSLionel Sambuc if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
85*0a6a1f1dSLionel Sambuc return nullptr;
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
89f4a2713aSLionel Sambuc if (VD->isStaticDataMember() &&
90f4a2713aSLionel Sambuc VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
91*0a6a1f1dSLionel Sambuc return nullptr;
92f4a2713aSLionel Sambuc }
93f4a2713aSLionel Sambuc
94f4a2713aSLionel Sambuc if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
95f4a2713aSLionel Sambuc if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
96*0a6a1f1dSLionel Sambuc return nullptr;
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc
99f4a2713aSLionel Sambuc if (const ClassTemplateSpecializationDecl *CTSD =
100f4a2713aSLionel Sambuc dyn_cast<ClassTemplateSpecializationDecl>(D)) {
101f4a2713aSLionel Sambuc TemplateSpecializationKind TSK = CTSD->getSpecializationKind();
102f4a2713aSLionel Sambuc if (TSK == TSK_ImplicitInstantiation ||
103f4a2713aSLionel Sambuc TSK == TSK_Undeclared)
104*0a6a1f1dSLionel Sambuc return nullptr;
105f4a2713aSLionel Sambuc }
106f4a2713aSLionel Sambuc
107f4a2713aSLionel Sambuc if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
108f4a2713aSLionel Sambuc if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
109*0a6a1f1dSLionel Sambuc return nullptr;
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
112f4a2713aSLionel Sambuc // When tag declaration (but not definition!) is part of the
113f4a2713aSLionel Sambuc // decl-specifier-seq of some other declaration, it doesn't get comment
114f4a2713aSLionel Sambuc if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition())
115*0a6a1f1dSLionel Sambuc return nullptr;
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc // TODO: handle comments for function parameters properly.
118f4a2713aSLionel Sambuc if (isa<ParmVarDecl>(D))
119*0a6a1f1dSLionel Sambuc return nullptr;
120f4a2713aSLionel Sambuc
121f4a2713aSLionel Sambuc // TODO: we could look up template parameter documentation in the template
122f4a2713aSLionel Sambuc // documentation.
123f4a2713aSLionel Sambuc if (isa<TemplateTypeParmDecl>(D) ||
124f4a2713aSLionel Sambuc isa<NonTypeTemplateParmDecl>(D) ||
125f4a2713aSLionel Sambuc isa<TemplateTemplateParmDecl>(D))
126*0a6a1f1dSLionel Sambuc return nullptr;
127f4a2713aSLionel Sambuc
128f4a2713aSLionel Sambuc ArrayRef<RawComment *> RawComments = Comments.getComments();
129f4a2713aSLionel Sambuc
130f4a2713aSLionel Sambuc // If there are no comments anywhere, we won't find anything.
131f4a2713aSLionel Sambuc if (RawComments.empty())
132*0a6a1f1dSLionel Sambuc return nullptr;
133f4a2713aSLionel Sambuc
134f4a2713aSLionel Sambuc // Find declaration location.
135f4a2713aSLionel Sambuc // For Objective-C declarations we generally don't expect to have multiple
136f4a2713aSLionel Sambuc // declarators, thus use declaration starting location as the "declaration
137f4a2713aSLionel Sambuc // location".
138f4a2713aSLionel Sambuc // For all other declarations multiple declarators are used quite frequently,
139f4a2713aSLionel Sambuc // so we use the location of the identifier as the "declaration location".
140f4a2713aSLionel Sambuc SourceLocation DeclLoc;
141f4a2713aSLionel Sambuc if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) ||
142f4a2713aSLionel Sambuc isa<ObjCPropertyDecl>(D) ||
143f4a2713aSLionel Sambuc isa<RedeclarableTemplateDecl>(D) ||
144f4a2713aSLionel Sambuc isa<ClassTemplateSpecializationDecl>(D))
145f4a2713aSLionel Sambuc DeclLoc = D->getLocStart();
146f4a2713aSLionel Sambuc else {
147f4a2713aSLionel Sambuc DeclLoc = D->getLocation();
148*0a6a1f1dSLionel Sambuc if (DeclLoc.isMacroID()) {
149*0a6a1f1dSLionel Sambuc if (isa<TypedefDecl>(D)) {
150f4a2713aSLionel Sambuc // If location of the typedef name is in a macro, it is because being
151*0a6a1f1dSLionel Sambuc // declared via a macro. Try using declaration's starting location as
152*0a6a1f1dSLionel Sambuc // the "declaration location".
153f4a2713aSLionel Sambuc DeclLoc = D->getLocStart();
154*0a6a1f1dSLionel Sambuc } else if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
155*0a6a1f1dSLionel Sambuc // If location of the tag decl is inside a macro, but the spelling of
156*0a6a1f1dSLionel Sambuc // the tag name comes from a macro argument, it looks like a special
157*0a6a1f1dSLionel Sambuc // macro like NS_ENUM is being used to define the tag decl. In that
158*0a6a1f1dSLionel Sambuc // case, adjust the source location to the expansion loc so that we can
159*0a6a1f1dSLionel Sambuc // attach the comment to the tag decl.
160*0a6a1f1dSLionel Sambuc if (SourceMgr.isMacroArgExpansion(DeclLoc) &&
161*0a6a1f1dSLionel Sambuc TD->isCompleteDefinition())
162*0a6a1f1dSLionel Sambuc DeclLoc = SourceMgr.getExpansionLoc(DeclLoc);
163*0a6a1f1dSLionel Sambuc }
164*0a6a1f1dSLionel Sambuc }
165f4a2713aSLionel Sambuc }
166f4a2713aSLionel Sambuc
167f4a2713aSLionel Sambuc // If the declaration doesn't map directly to a location in a file, we
168f4a2713aSLionel Sambuc // can't find the comment.
169f4a2713aSLionel Sambuc if (DeclLoc.isInvalid() || !DeclLoc.isFileID())
170*0a6a1f1dSLionel Sambuc return nullptr;
171f4a2713aSLionel Sambuc
172f4a2713aSLionel Sambuc // Find the comment that occurs just after this declaration.
173f4a2713aSLionel Sambuc ArrayRef<RawComment *>::iterator Comment;
174f4a2713aSLionel Sambuc {
175f4a2713aSLionel Sambuc // When searching for comments during parsing, the comment we are looking
176f4a2713aSLionel Sambuc // for is usually among the last two comments we parsed -- check them
177f4a2713aSLionel Sambuc // first.
178f4a2713aSLionel Sambuc RawComment CommentAtDeclLoc(
179f4a2713aSLionel Sambuc SourceMgr, SourceRange(DeclLoc), false,
180f4a2713aSLionel Sambuc LangOpts.CommentOpts.ParseAllComments);
181f4a2713aSLionel Sambuc BeforeThanCompare<RawComment> Compare(SourceMgr);
182f4a2713aSLionel Sambuc ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1;
183f4a2713aSLionel Sambuc bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
184f4a2713aSLionel Sambuc if (!Found && RawComments.size() >= 2) {
185f4a2713aSLionel Sambuc MaybeBeforeDecl--;
186f4a2713aSLionel Sambuc Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
187f4a2713aSLionel Sambuc }
188f4a2713aSLionel Sambuc
189f4a2713aSLionel Sambuc if (Found) {
190f4a2713aSLionel Sambuc Comment = MaybeBeforeDecl + 1;
191f4a2713aSLionel Sambuc assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(),
192f4a2713aSLionel Sambuc &CommentAtDeclLoc, Compare));
193f4a2713aSLionel Sambuc } else {
194f4a2713aSLionel Sambuc // Slow path.
195f4a2713aSLionel Sambuc Comment = std::lower_bound(RawComments.begin(), RawComments.end(),
196f4a2713aSLionel Sambuc &CommentAtDeclLoc, Compare);
197f4a2713aSLionel Sambuc }
198f4a2713aSLionel Sambuc }
199f4a2713aSLionel Sambuc
200f4a2713aSLionel Sambuc // Decompose the location for the declaration and find the beginning of the
201f4a2713aSLionel Sambuc // file buffer.
202f4a2713aSLionel Sambuc std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc);
203f4a2713aSLionel Sambuc
204f4a2713aSLionel Sambuc // First check whether we have a trailing comment.
205f4a2713aSLionel Sambuc if (Comment != RawComments.end() &&
206f4a2713aSLionel Sambuc (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() &&
207f4a2713aSLionel Sambuc (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D) ||
208f4a2713aSLionel Sambuc isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D))) {
209f4a2713aSLionel Sambuc std::pair<FileID, unsigned> CommentBeginDecomp
210f4a2713aSLionel Sambuc = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin());
211f4a2713aSLionel Sambuc // Check that Doxygen trailing comment comes after the declaration, starts
212f4a2713aSLionel Sambuc // on the same line and in the same file as the declaration.
213f4a2713aSLionel Sambuc if (DeclLocDecomp.first == CommentBeginDecomp.first &&
214f4a2713aSLionel Sambuc SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second)
215f4a2713aSLionel Sambuc == SourceMgr.getLineNumber(CommentBeginDecomp.first,
216f4a2713aSLionel Sambuc CommentBeginDecomp.second)) {
217f4a2713aSLionel Sambuc return *Comment;
218f4a2713aSLionel Sambuc }
219f4a2713aSLionel Sambuc }
220f4a2713aSLionel Sambuc
221f4a2713aSLionel Sambuc // The comment just after the declaration was not a trailing comment.
222f4a2713aSLionel Sambuc // Let's look at the previous comment.
223f4a2713aSLionel Sambuc if (Comment == RawComments.begin())
224*0a6a1f1dSLionel Sambuc return nullptr;
225f4a2713aSLionel Sambuc --Comment;
226f4a2713aSLionel Sambuc
227f4a2713aSLionel Sambuc // Check that we actually have a non-member Doxygen comment.
228f4a2713aSLionel Sambuc if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment())
229*0a6a1f1dSLionel Sambuc return nullptr;
230f4a2713aSLionel Sambuc
231f4a2713aSLionel Sambuc // Decompose the end of the comment.
232f4a2713aSLionel Sambuc std::pair<FileID, unsigned> CommentEndDecomp
233f4a2713aSLionel Sambuc = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd());
234f4a2713aSLionel Sambuc
235f4a2713aSLionel Sambuc // If the comment and the declaration aren't in the same file, then they
236f4a2713aSLionel Sambuc // aren't related.
237f4a2713aSLionel Sambuc if (DeclLocDecomp.first != CommentEndDecomp.first)
238*0a6a1f1dSLionel Sambuc return nullptr;
239f4a2713aSLionel Sambuc
240f4a2713aSLionel Sambuc // Get the corresponding buffer.
241f4a2713aSLionel Sambuc bool Invalid = false;
242f4a2713aSLionel Sambuc const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first,
243f4a2713aSLionel Sambuc &Invalid).data();
244f4a2713aSLionel Sambuc if (Invalid)
245*0a6a1f1dSLionel Sambuc return nullptr;
246f4a2713aSLionel Sambuc
247f4a2713aSLionel Sambuc // Extract text between the comment and declaration.
248f4a2713aSLionel Sambuc StringRef Text(Buffer + CommentEndDecomp.second,
249f4a2713aSLionel Sambuc DeclLocDecomp.second - CommentEndDecomp.second);
250f4a2713aSLionel Sambuc
251f4a2713aSLionel Sambuc // There should be no other declarations or preprocessor directives between
252f4a2713aSLionel Sambuc // comment and declaration.
253f4a2713aSLionel Sambuc if (Text.find_first_of(";{}#@") != StringRef::npos)
254*0a6a1f1dSLionel Sambuc return nullptr;
255f4a2713aSLionel Sambuc
256f4a2713aSLionel Sambuc return *Comment;
257f4a2713aSLionel Sambuc }
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc namespace {
260f4a2713aSLionel Sambuc /// If we have a 'templated' declaration for a template, adjust 'D' to
261f4a2713aSLionel Sambuc /// refer to the actual template.
262f4a2713aSLionel Sambuc /// If we have an implicit instantiation, adjust 'D' to refer to template.
adjustDeclToTemplate(const Decl * D)263f4a2713aSLionel Sambuc const Decl *adjustDeclToTemplate(const Decl *D) {
264f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
265f4a2713aSLionel Sambuc // Is this function declaration part of a function template?
266f4a2713aSLionel Sambuc if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate())
267f4a2713aSLionel Sambuc return FTD;
268f4a2713aSLionel Sambuc
269f4a2713aSLionel Sambuc // Nothing to do if function is not an implicit instantiation.
270f4a2713aSLionel Sambuc if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation)
271f4a2713aSLionel Sambuc return D;
272f4a2713aSLionel Sambuc
273f4a2713aSLionel Sambuc // Function is an implicit instantiation of a function template?
274f4a2713aSLionel Sambuc if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate())
275f4a2713aSLionel Sambuc return FTD;
276f4a2713aSLionel Sambuc
277f4a2713aSLionel Sambuc // Function is instantiated from a member definition of a class template?
278f4a2713aSLionel Sambuc if (const FunctionDecl *MemberDecl =
279f4a2713aSLionel Sambuc FD->getInstantiatedFromMemberFunction())
280f4a2713aSLionel Sambuc return MemberDecl;
281f4a2713aSLionel Sambuc
282f4a2713aSLionel Sambuc return D;
283f4a2713aSLionel Sambuc }
284f4a2713aSLionel Sambuc if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
285f4a2713aSLionel Sambuc // Static data member is instantiated from a member definition of a class
286f4a2713aSLionel Sambuc // template?
287f4a2713aSLionel Sambuc if (VD->isStaticDataMember())
288f4a2713aSLionel Sambuc if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember())
289f4a2713aSLionel Sambuc return MemberDecl;
290f4a2713aSLionel Sambuc
291f4a2713aSLionel Sambuc return D;
292f4a2713aSLionel Sambuc }
293f4a2713aSLionel Sambuc if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
294f4a2713aSLionel Sambuc // Is this class declaration part of a class template?
295f4a2713aSLionel Sambuc if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate())
296f4a2713aSLionel Sambuc return CTD;
297f4a2713aSLionel Sambuc
298f4a2713aSLionel Sambuc // Class is an implicit instantiation of a class template or partial
299f4a2713aSLionel Sambuc // specialization?
300f4a2713aSLionel Sambuc if (const ClassTemplateSpecializationDecl *CTSD =
301f4a2713aSLionel Sambuc dyn_cast<ClassTemplateSpecializationDecl>(CRD)) {
302f4a2713aSLionel Sambuc if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation)
303f4a2713aSLionel Sambuc return D;
304f4a2713aSLionel Sambuc llvm::PointerUnion<ClassTemplateDecl *,
305f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *>
306f4a2713aSLionel Sambuc PU = CTSD->getSpecializedTemplateOrPartial();
307f4a2713aSLionel Sambuc return PU.is<ClassTemplateDecl*>() ?
308f4a2713aSLionel Sambuc static_cast<const Decl*>(PU.get<ClassTemplateDecl *>()) :
309f4a2713aSLionel Sambuc static_cast<const Decl*>(
310f4a2713aSLionel Sambuc PU.get<ClassTemplatePartialSpecializationDecl *>());
311f4a2713aSLionel Sambuc }
312f4a2713aSLionel Sambuc
313f4a2713aSLionel Sambuc // Class is instantiated from a member definition of a class template?
314f4a2713aSLionel Sambuc if (const MemberSpecializationInfo *Info =
315f4a2713aSLionel Sambuc CRD->getMemberSpecializationInfo())
316f4a2713aSLionel Sambuc return Info->getInstantiatedFrom();
317f4a2713aSLionel Sambuc
318f4a2713aSLionel Sambuc return D;
319f4a2713aSLionel Sambuc }
320f4a2713aSLionel Sambuc if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
321f4a2713aSLionel Sambuc // Enum is instantiated from a member definition of a class template?
322f4a2713aSLionel Sambuc if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum())
323f4a2713aSLionel Sambuc return MemberDecl;
324f4a2713aSLionel Sambuc
325f4a2713aSLionel Sambuc return D;
326f4a2713aSLionel Sambuc }
327f4a2713aSLionel Sambuc // FIXME: Adjust alias templates?
328f4a2713aSLionel Sambuc return D;
329f4a2713aSLionel Sambuc }
330f4a2713aSLionel Sambuc } // unnamed namespace
331f4a2713aSLionel Sambuc
getRawCommentForAnyRedecl(const Decl * D,const Decl ** OriginalDecl) const332f4a2713aSLionel Sambuc const RawComment *ASTContext::getRawCommentForAnyRedecl(
333f4a2713aSLionel Sambuc const Decl *D,
334f4a2713aSLionel Sambuc const Decl **OriginalDecl) const {
335f4a2713aSLionel Sambuc D = adjustDeclToTemplate(D);
336f4a2713aSLionel Sambuc
337f4a2713aSLionel Sambuc // Check whether we have cached a comment for this declaration already.
338f4a2713aSLionel Sambuc {
339f4a2713aSLionel Sambuc llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
340f4a2713aSLionel Sambuc RedeclComments.find(D);
341f4a2713aSLionel Sambuc if (Pos != RedeclComments.end()) {
342f4a2713aSLionel Sambuc const RawCommentAndCacheFlags &Raw = Pos->second;
343f4a2713aSLionel Sambuc if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) {
344f4a2713aSLionel Sambuc if (OriginalDecl)
345f4a2713aSLionel Sambuc *OriginalDecl = Raw.getOriginalDecl();
346f4a2713aSLionel Sambuc return Raw.getRaw();
347f4a2713aSLionel Sambuc }
348f4a2713aSLionel Sambuc }
349f4a2713aSLionel Sambuc }
350f4a2713aSLionel Sambuc
351f4a2713aSLionel Sambuc // Search for comments attached to declarations in the redeclaration chain.
352*0a6a1f1dSLionel Sambuc const RawComment *RC = nullptr;
353*0a6a1f1dSLionel Sambuc const Decl *OriginalDeclForRC = nullptr;
354*0a6a1f1dSLionel Sambuc for (auto I : D->redecls()) {
355f4a2713aSLionel Sambuc llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
356*0a6a1f1dSLionel Sambuc RedeclComments.find(I);
357f4a2713aSLionel Sambuc if (Pos != RedeclComments.end()) {
358f4a2713aSLionel Sambuc const RawCommentAndCacheFlags &Raw = Pos->second;
359f4a2713aSLionel Sambuc if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) {
360f4a2713aSLionel Sambuc RC = Raw.getRaw();
361f4a2713aSLionel Sambuc OriginalDeclForRC = Raw.getOriginalDecl();
362f4a2713aSLionel Sambuc break;
363f4a2713aSLionel Sambuc }
364f4a2713aSLionel Sambuc } else {
365*0a6a1f1dSLionel Sambuc RC = getRawCommentForDeclNoCache(I);
366*0a6a1f1dSLionel Sambuc OriginalDeclForRC = I;
367f4a2713aSLionel Sambuc RawCommentAndCacheFlags Raw;
368f4a2713aSLionel Sambuc if (RC) {
369f4a2713aSLionel Sambuc Raw.setRaw(RC);
370f4a2713aSLionel Sambuc Raw.setKind(RawCommentAndCacheFlags::FromDecl);
371f4a2713aSLionel Sambuc } else
372f4a2713aSLionel Sambuc Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl);
373*0a6a1f1dSLionel Sambuc Raw.setOriginalDecl(I);
374*0a6a1f1dSLionel Sambuc RedeclComments[I] = Raw;
375f4a2713aSLionel Sambuc if (RC)
376f4a2713aSLionel Sambuc break;
377f4a2713aSLionel Sambuc }
378f4a2713aSLionel Sambuc }
379f4a2713aSLionel Sambuc
380f4a2713aSLionel Sambuc // If we found a comment, it should be a documentation comment.
381f4a2713aSLionel Sambuc assert(!RC || RC->isDocumentation());
382f4a2713aSLionel Sambuc
383f4a2713aSLionel Sambuc if (OriginalDecl)
384f4a2713aSLionel Sambuc *OriginalDecl = OriginalDeclForRC;
385f4a2713aSLionel Sambuc
386f4a2713aSLionel Sambuc // Update cache for every declaration in the redeclaration chain.
387f4a2713aSLionel Sambuc RawCommentAndCacheFlags Raw;
388f4a2713aSLionel Sambuc Raw.setRaw(RC);
389f4a2713aSLionel Sambuc Raw.setKind(RawCommentAndCacheFlags::FromRedecl);
390f4a2713aSLionel Sambuc Raw.setOriginalDecl(OriginalDeclForRC);
391f4a2713aSLionel Sambuc
392*0a6a1f1dSLionel Sambuc for (auto I : D->redecls()) {
393*0a6a1f1dSLionel Sambuc RawCommentAndCacheFlags &R = RedeclComments[I];
394f4a2713aSLionel Sambuc if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl)
395f4a2713aSLionel Sambuc R = Raw;
396f4a2713aSLionel Sambuc }
397f4a2713aSLionel Sambuc
398f4a2713aSLionel Sambuc return RC;
399f4a2713aSLionel Sambuc }
400f4a2713aSLionel Sambuc
addRedeclaredMethods(const ObjCMethodDecl * ObjCMethod,SmallVectorImpl<const NamedDecl * > & Redeclared)401f4a2713aSLionel Sambuc static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod,
402f4a2713aSLionel Sambuc SmallVectorImpl<const NamedDecl *> &Redeclared) {
403f4a2713aSLionel Sambuc const DeclContext *DC = ObjCMethod->getDeclContext();
404f4a2713aSLionel Sambuc if (const ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(DC)) {
405f4a2713aSLionel Sambuc const ObjCInterfaceDecl *ID = IMD->getClassInterface();
406f4a2713aSLionel Sambuc if (!ID)
407f4a2713aSLionel Sambuc return;
408f4a2713aSLionel Sambuc // Add redeclared method here.
409*0a6a1f1dSLionel Sambuc for (const auto *Ext : ID->known_extensions()) {
410f4a2713aSLionel Sambuc if (ObjCMethodDecl *RedeclaredMethod =
411f4a2713aSLionel Sambuc Ext->getMethod(ObjCMethod->getSelector(),
412f4a2713aSLionel Sambuc ObjCMethod->isInstanceMethod()))
413f4a2713aSLionel Sambuc Redeclared.push_back(RedeclaredMethod);
414f4a2713aSLionel Sambuc }
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc }
417f4a2713aSLionel Sambuc
cloneFullComment(comments::FullComment * FC,const Decl * D) const418f4a2713aSLionel Sambuc comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC,
419f4a2713aSLionel Sambuc const Decl *D) const {
420f4a2713aSLionel Sambuc comments::DeclInfo *ThisDeclInfo = new (*this) comments::DeclInfo;
421f4a2713aSLionel Sambuc ThisDeclInfo->CommentDecl = D;
422f4a2713aSLionel Sambuc ThisDeclInfo->IsFilled = false;
423f4a2713aSLionel Sambuc ThisDeclInfo->fill();
424f4a2713aSLionel Sambuc ThisDeclInfo->CommentDecl = FC->getDecl();
425*0a6a1f1dSLionel Sambuc if (!ThisDeclInfo->TemplateParameters)
426*0a6a1f1dSLionel Sambuc ThisDeclInfo->TemplateParameters = FC->getDeclInfo()->TemplateParameters;
427f4a2713aSLionel Sambuc comments::FullComment *CFC =
428f4a2713aSLionel Sambuc new (*this) comments::FullComment(FC->getBlocks(),
429f4a2713aSLionel Sambuc ThisDeclInfo);
430f4a2713aSLionel Sambuc return CFC;
431f4a2713aSLionel Sambuc
432f4a2713aSLionel Sambuc }
433f4a2713aSLionel Sambuc
getLocalCommentForDeclUncached(const Decl * D) const434f4a2713aSLionel Sambuc comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const {
435f4a2713aSLionel Sambuc const RawComment *RC = getRawCommentForDeclNoCache(D);
436*0a6a1f1dSLionel Sambuc return RC ? RC->parse(*this, nullptr, D) : nullptr;
437f4a2713aSLionel Sambuc }
438f4a2713aSLionel Sambuc
getCommentForDecl(const Decl * D,const Preprocessor * PP) const439f4a2713aSLionel Sambuc comments::FullComment *ASTContext::getCommentForDecl(
440f4a2713aSLionel Sambuc const Decl *D,
441f4a2713aSLionel Sambuc const Preprocessor *PP) const {
442f4a2713aSLionel Sambuc if (D->isInvalidDecl())
443*0a6a1f1dSLionel Sambuc return nullptr;
444f4a2713aSLionel Sambuc D = adjustDeclToTemplate(D);
445f4a2713aSLionel Sambuc
446f4a2713aSLionel Sambuc const Decl *Canonical = D->getCanonicalDecl();
447f4a2713aSLionel Sambuc llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos =
448f4a2713aSLionel Sambuc ParsedComments.find(Canonical);
449f4a2713aSLionel Sambuc
450f4a2713aSLionel Sambuc if (Pos != ParsedComments.end()) {
451f4a2713aSLionel Sambuc if (Canonical != D) {
452f4a2713aSLionel Sambuc comments::FullComment *FC = Pos->second;
453f4a2713aSLionel Sambuc comments::FullComment *CFC = cloneFullComment(FC, D);
454f4a2713aSLionel Sambuc return CFC;
455f4a2713aSLionel Sambuc }
456f4a2713aSLionel Sambuc return Pos->second;
457f4a2713aSLionel Sambuc }
458f4a2713aSLionel Sambuc
459f4a2713aSLionel Sambuc const Decl *OriginalDecl;
460f4a2713aSLionel Sambuc
461f4a2713aSLionel Sambuc const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl);
462f4a2713aSLionel Sambuc if (!RC) {
463f4a2713aSLionel Sambuc if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) {
464f4a2713aSLionel Sambuc SmallVector<const NamedDecl*, 8> Overridden;
465f4a2713aSLionel Sambuc const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D);
466f4a2713aSLionel Sambuc if (OMD && OMD->isPropertyAccessor())
467f4a2713aSLionel Sambuc if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl())
468f4a2713aSLionel Sambuc if (comments::FullComment *FC = getCommentForDecl(PDecl, PP))
469f4a2713aSLionel Sambuc return cloneFullComment(FC, D);
470f4a2713aSLionel Sambuc if (OMD)
471f4a2713aSLionel Sambuc addRedeclaredMethods(OMD, Overridden);
472f4a2713aSLionel Sambuc getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden);
473f4a2713aSLionel Sambuc for (unsigned i = 0, e = Overridden.size(); i < e; i++)
474f4a2713aSLionel Sambuc if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP))
475f4a2713aSLionel Sambuc return cloneFullComment(FC, D);
476f4a2713aSLionel Sambuc }
477f4a2713aSLionel Sambuc else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
478f4a2713aSLionel Sambuc // Attach any tag type's documentation to its typedef if latter
479f4a2713aSLionel Sambuc // does not have one of its own.
480f4a2713aSLionel Sambuc QualType QT = TD->getUnderlyingType();
481f4a2713aSLionel Sambuc if (const TagType *TT = QT->getAs<TagType>())
482f4a2713aSLionel Sambuc if (const Decl *TD = TT->getDecl())
483f4a2713aSLionel Sambuc if (comments::FullComment *FC = getCommentForDecl(TD, PP))
484f4a2713aSLionel Sambuc return cloneFullComment(FC, D);
485f4a2713aSLionel Sambuc }
486f4a2713aSLionel Sambuc else if (const ObjCInterfaceDecl *IC = dyn_cast<ObjCInterfaceDecl>(D)) {
487f4a2713aSLionel Sambuc while (IC->getSuperClass()) {
488f4a2713aSLionel Sambuc IC = IC->getSuperClass();
489f4a2713aSLionel Sambuc if (comments::FullComment *FC = getCommentForDecl(IC, PP))
490f4a2713aSLionel Sambuc return cloneFullComment(FC, D);
491f4a2713aSLionel Sambuc }
492f4a2713aSLionel Sambuc }
493f4a2713aSLionel Sambuc else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
494f4a2713aSLionel Sambuc if (const ObjCInterfaceDecl *IC = CD->getClassInterface())
495f4a2713aSLionel Sambuc if (comments::FullComment *FC = getCommentForDecl(IC, PP))
496f4a2713aSLionel Sambuc return cloneFullComment(FC, D);
497f4a2713aSLionel Sambuc }
498f4a2713aSLionel Sambuc else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
499f4a2713aSLionel Sambuc if (!(RD = RD->getDefinition()))
500*0a6a1f1dSLionel Sambuc return nullptr;
501f4a2713aSLionel Sambuc // Check non-virtual bases.
502*0a6a1f1dSLionel Sambuc for (const auto &I : RD->bases()) {
503*0a6a1f1dSLionel Sambuc if (I.isVirtual() || (I.getAccessSpecifier() != AS_public))
504f4a2713aSLionel Sambuc continue;
505*0a6a1f1dSLionel Sambuc QualType Ty = I.getType();
506f4a2713aSLionel Sambuc if (Ty.isNull())
507f4a2713aSLionel Sambuc continue;
508f4a2713aSLionel Sambuc if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) {
509f4a2713aSLionel Sambuc if (!(NonVirtualBase= NonVirtualBase->getDefinition()))
510f4a2713aSLionel Sambuc continue;
511f4a2713aSLionel Sambuc
512f4a2713aSLionel Sambuc if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP))
513f4a2713aSLionel Sambuc return cloneFullComment(FC, D);
514f4a2713aSLionel Sambuc }
515f4a2713aSLionel Sambuc }
516f4a2713aSLionel Sambuc // Check virtual bases.
517*0a6a1f1dSLionel Sambuc for (const auto &I : RD->vbases()) {
518*0a6a1f1dSLionel Sambuc if (I.getAccessSpecifier() != AS_public)
519f4a2713aSLionel Sambuc continue;
520*0a6a1f1dSLionel Sambuc QualType Ty = I.getType();
521f4a2713aSLionel Sambuc if (Ty.isNull())
522f4a2713aSLionel Sambuc continue;
523f4a2713aSLionel Sambuc if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) {
524f4a2713aSLionel Sambuc if (!(VirtualBase= VirtualBase->getDefinition()))
525f4a2713aSLionel Sambuc continue;
526f4a2713aSLionel Sambuc if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP))
527f4a2713aSLionel Sambuc return cloneFullComment(FC, D);
528f4a2713aSLionel Sambuc }
529f4a2713aSLionel Sambuc }
530f4a2713aSLionel Sambuc }
531*0a6a1f1dSLionel Sambuc return nullptr;
532f4a2713aSLionel Sambuc }
533f4a2713aSLionel Sambuc
534f4a2713aSLionel Sambuc // If the RawComment was attached to other redeclaration of this Decl, we
535f4a2713aSLionel Sambuc // should parse the comment in context of that other Decl. This is important
536f4a2713aSLionel Sambuc // because comments can contain references to parameter names which can be
537f4a2713aSLionel Sambuc // different across redeclarations.
538f4a2713aSLionel Sambuc if (D != OriginalDecl)
539f4a2713aSLionel Sambuc return getCommentForDecl(OriginalDecl, PP);
540f4a2713aSLionel Sambuc
541f4a2713aSLionel Sambuc comments::FullComment *FC = RC->parse(*this, PP, D);
542f4a2713aSLionel Sambuc ParsedComments[Canonical] = FC;
543f4a2713aSLionel Sambuc return FC;
544f4a2713aSLionel Sambuc }
545f4a2713aSLionel Sambuc
546f4a2713aSLionel Sambuc void
Profile(llvm::FoldingSetNodeID & ID,TemplateTemplateParmDecl * Parm)547f4a2713aSLionel Sambuc ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
548f4a2713aSLionel Sambuc TemplateTemplateParmDecl *Parm) {
549f4a2713aSLionel Sambuc ID.AddInteger(Parm->getDepth());
550f4a2713aSLionel Sambuc ID.AddInteger(Parm->getPosition());
551f4a2713aSLionel Sambuc ID.AddBoolean(Parm->isParameterPack());
552f4a2713aSLionel Sambuc
553f4a2713aSLionel Sambuc TemplateParameterList *Params = Parm->getTemplateParameters();
554f4a2713aSLionel Sambuc ID.AddInteger(Params->size());
555f4a2713aSLionel Sambuc for (TemplateParameterList::const_iterator P = Params->begin(),
556f4a2713aSLionel Sambuc PEnd = Params->end();
557f4a2713aSLionel Sambuc P != PEnd; ++P) {
558f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
559f4a2713aSLionel Sambuc ID.AddInteger(0);
560f4a2713aSLionel Sambuc ID.AddBoolean(TTP->isParameterPack());
561f4a2713aSLionel Sambuc continue;
562f4a2713aSLionel Sambuc }
563f4a2713aSLionel Sambuc
564f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
565f4a2713aSLionel Sambuc ID.AddInteger(1);
566f4a2713aSLionel Sambuc ID.AddBoolean(NTTP->isParameterPack());
567f4a2713aSLionel Sambuc ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr());
568f4a2713aSLionel Sambuc if (NTTP->isExpandedParameterPack()) {
569f4a2713aSLionel Sambuc ID.AddBoolean(true);
570f4a2713aSLionel Sambuc ID.AddInteger(NTTP->getNumExpansionTypes());
571f4a2713aSLionel Sambuc for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
572f4a2713aSLionel Sambuc QualType T = NTTP->getExpansionType(I);
573f4a2713aSLionel Sambuc ID.AddPointer(T.getCanonicalType().getAsOpaquePtr());
574f4a2713aSLionel Sambuc }
575f4a2713aSLionel Sambuc } else
576f4a2713aSLionel Sambuc ID.AddBoolean(false);
577f4a2713aSLionel Sambuc continue;
578f4a2713aSLionel Sambuc }
579f4a2713aSLionel Sambuc
580f4a2713aSLionel Sambuc TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
581f4a2713aSLionel Sambuc ID.AddInteger(2);
582f4a2713aSLionel Sambuc Profile(ID, TTP);
583f4a2713aSLionel Sambuc }
584f4a2713aSLionel Sambuc }
585f4a2713aSLionel Sambuc
586f4a2713aSLionel Sambuc TemplateTemplateParmDecl *
getCanonicalTemplateTemplateParmDecl(TemplateTemplateParmDecl * TTP) const587f4a2713aSLionel Sambuc ASTContext::getCanonicalTemplateTemplateParmDecl(
588f4a2713aSLionel Sambuc TemplateTemplateParmDecl *TTP) const {
589f4a2713aSLionel Sambuc // Check if we already have a canonical template template parameter.
590f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
591f4a2713aSLionel Sambuc CanonicalTemplateTemplateParm::Profile(ID, TTP);
592*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
593f4a2713aSLionel Sambuc CanonicalTemplateTemplateParm *Canonical
594f4a2713aSLionel Sambuc = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
595f4a2713aSLionel Sambuc if (Canonical)
596f4a2713aSLionel Sambuc return Canonical->getParam();
597f4a2713aSLionel Sambuc
598f4a2713aSLionel Sambuc // Build a canonical template parameter list.
599f4a2713aSLionel Sambuc TemplateParameterList *Params = TTP->getTemplateParameters();
600f4a2713aSLionel Sambuc SmallVector<NamedDecl *, 4> CanonParams;
601f4a2713aSLionel Sambuc CanonParams.reserve(Params->size());
602f4a2713aSLionel Sambuc for (TemplateParameterList::const_iterator P = Params->begin(),
603f4a2713aSLionel Sambuc PEnd = Params->end();
604f4a2713aSLionel Sambuc P != PEnd; ++P) {
605f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
606f4a2713aSLionel Sambuc CanonParams.push_back(
607f4a2713aSLionel Sambuc TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
608f4a2713aSLionel Sambuc SourceLocation(),
609f4a2713aSLionel Sambuc SourceLocation(),
610f4a2713aSLionel Sambuc TTP->getDepth(),
611*0a6a1f1dSLionel Sambuc TTP->getIndex(), nullptr, false,
612f4a2713aSLionel Sambuc TTP->isParameterPack()));
613f4a2713aSLionel Sambuc else if (NonTypeTemplateParmDecl *NTTP
614f4a2713aSLionel Sambuc = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
615f4a2713aSLionel Sambuc QualType T = getCanonicalType(NTTP->getType());
616f4a2713aSLionel Sambuc TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
617f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *Param;
618f4a2713aSLionel Sambuc if (NTTP->isExpandedParameterPack()) {
619f4a2713aSLionel Sambuc SmallVector<QualType, 2> ExpandedTypes;
620f4a2713aSLionel Sambuc SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
621f4a2713aSLionel Sambuc for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
622f4a2713aSLionel Sambuc ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
623f4a2713aSLionel Sambuc ExpandedTInfos.push_back(
624f4a2713aSLionel Sambuc getTrivialTypeSourceInfo(ExpandedTypes.back()));
625f4a2713aSLionel Sambuc }
626f4a2713aSLionel Sambuc
627f4a2713aSLionel Sambuc Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
628f4a2713aSLionel Sambuc SourceLocation(),
629f4a2713aSLionel Sambuc SourceLocation(),
630f4a2713aSLionel Sambuc NTTP->getDepth(),
631*0a6a1f1dSLionel Sambuc NTTP->getPosition(), nullptr,
632f4a2713aSLionel Sambuc T,
633f4a2713aSLionel Sambuc TInfo,
634f4a2713aSLionel Sambuc ExpandedTypes.data(),
635f4a2713aSLionel Sambuc ExpandedTypes.size(),
636f4a2713aSLionel Sambuc ExpandedTInfos.data());
637f4a2713aSLionel Sambuc } else {
638f4a2713aSLionel Sambuc Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
639f4a2713aSLionel Sambuc SourceLocation(),
640f4a2713aSLionel Sambuc SourceLocation(),
641f4a2713aSLionel Sambuc NTTP->getDepth(),
642*0a6a1f1dSLionel Sambuc NTTP->getPosition(), nullptr,
643f4a2713aSLionel Sambuc T,
644f4a2713aSLionel Sambuc NTTP->isParameterPack(),
645f4a2713aSLionel Sambuc TInfo);
646f4a2713aSLionel Sambuc }
647f4a2713aSLionel Sambuc CanonParams.push_back(Param);
648f4a2713aSLionel Sambuc
649f4a2713aSLionel Sambuc } else
650f4a2713aSLionel Sambuc CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
651f4a2713aSLionel Sambuc cast<TemplateTemplateParmDecl>(*P)));
652f4a2713aSLionel Sambuc }
653f4a2713aSLionel Sambuc
654f4a2713aSLionel Sambuc TemplateTemplateParmDecl *CanonTTP
655f4a2713aSLionel Sambuc = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
656f4a2713aSLionel Sambuc SourceLocation(), TTP->getDepth(),
657f4a2713aSLionel Sambuc TTP->getPosition(),
658f4a2713aSLionel Sambuc TTP->isParameterPack(),
659*0a6a1f1dSLionel Sambuc nullptr,
660f4a2713aSLionel Sambuc TemplateParameterList::Create(*this, SourceLocation(),
661f4a2713aSLionel Sambuc SourceLocation(),
662f4a2713aSLionel Sambuc CanonParams.data(),
663f4a2713aSLionel Sambuc CanonParams.size(),
664f4a2713aSLionel Sambuc SourceLocation()));
665f4a2713aSLionel Sambuc
666f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
667f4a2713aSLionel Sambuc Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
668*0a6a1f1dSLionel Sambuc assert(!Canonical && "Shouldn't be in the map!");
669f4a2713aSLionel Sambuc (void)Canonical;
670f4a2713aSLionel Sambuc
671f4a2713aSLionel Sambuc // Create the canonical template template parameter entry.
672f4a2713aSLionel Sambuc Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
673f4a2713aSLionel Sambuc CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
674f4a2713aSLionel Sambuc return CanonTTP;
675f4a2713aSLionel Sambuc }
676f4a2713aSLionel Sambuc
createCXXABI(const TargetInfo & T)677f4a2713aSLionel Sambuc CXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
678*0a6a1f1dSLionel Sambuc if (!LangOpts.CPlusPlus) return nullptr;
679f4a2713aSLionel Sambuc
680f4a2713aSLionel Sambuc switch (T.getCXXABI().getKind()) {
681*0a6a1f1dSLionel Sambuc case TargetCXXABI::GenericARM: // Same as Itanium at this level
682f4a2713aSLionel Sambuc case TargetCXXABI::iOS:
683*0a6a1f1dSLionel Sambuc case TargetCXXABI::iOS64:
684*0a6a1f1dSLionel Sambuc case TargetCXXABI::GenericAArch64:
685*0a6a1f1dSLionel Sambuc case TargetCXXABI::GenericMIPS:
686f4a2713aSLionel Sambuc case TargetCXXABI::GenericItanium:
687f4a2713aSLionel Sambuc return CreateItaniumCXXABI(*this);
688f4a2713aSLionel Sambuc case TargetCXXABI::Microsoft:
689f4a2713aSLionel Sambuc return CreateMicrosoftCXXABI(*this);
690f4a2713aSLionel Sambuc }
691f4a2713aSLionel Sambuc llvm_unreachable("Invalid CXXABI type!");
692f4a2713aSLionel Sambuc }
693f4a2713aSLionel Sambuc
getAddressSpaceMap(const TargetInfo & T,const LangOptions & LOpts)694f4a2713aSLionel Sambuc static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T,
695f4a2713aSLionel Sambuc const LangOptions &LOpts) {
696f4a2713aSLionel Sambuc if (LOpts.FakeAddressSpaceMap) {
697f4a2713aSLionel Sambuc // The fake address space map must have a distinct entry for each
698f4a2713aSLionel Sambuc // language-specific address space.
699f4a2713aSLionel Sambuc static const unsigned FakeAddrSpaceMap[] = {
700f4a2713aSLionel Sambuc 1, // opencl_global
701f4a2713aSLionel Sambuc 2, // opencl_local
702f4a2713aSLionel Sambuc 3, // opencl_constant
703*0a6a1f1dSLionel Sambuc 4, // opencl_generic
704*0a6a1f1dSLionel Sambuc 5, // cuda_device
705*0a6a1f1dSLionel Sambuc 6, // cuda_constant
706*0a6a1f1dSLionel Sambuc 7 // cuda_shared
707f4a2713aSLionel Sambuc };
708f4a2713aSLionel Sambuc return &FakeAddrSpaceMap;
709f4a2713aSLionel Sambuc } else {
710f4a2713aSLionel Sambuc return &T.getAddressSpaceMap();
711f4a2713aSLionel Sambuc }
712f4a2713aSLionel Sambuc }
713f4a2713aSLionel Sambuc
isAddrSpaceMapManglingEnabled(const TargetInfo & TI,const LangOptions & LangOpts)714f4a2713aSLionel Sambuc static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI,
715f4a2713aSLionel Sambuc const LangOptions &LangOpts) {
716f4a2713aSLionel Sambuc switch (LangOpts.getAddressSpaceMapMangling()) {
717f4a2713aSLionel Sambuc case LangOptions::ASMM_Target:
718f4a2713aSLionel Sambuc return TI.useAddressSpaceMapMangling();
719f4a2713aSLionel Sambuc case LangOptions::ASMM_On:
720f4a2713aSLionel Sambuc return true;
721f4a2713aSLionel Sambuc case LangOptions::ASMM_Off:
722f4a2713aSLionel Sambuc return false;
723f4a2713aSLionel Sambuc }
724f4a2713aSLionel Sambuc llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything.");
725f4a2713aSLionel Sambuc }
726f4a2713aSLionel Sambuc
ASTContext(LangOptions & LOpts,SourceManager & SM,IdentifierTable & idents,SelectorTable & sels,Builtin::Context & builtins)727f4a2713aSLionel Sambuc ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
728f4a2713aSLionel Sambuc IdentifierTable &idents, SelectorTable &sels,
729*0a6a1f1dSLionel Sambuc Builtin::Context &builtins)
730*0a6a1f1dSLionel Sambuc : FunctionProtoTypes(this_()), TemplateSpecializationTypes(this_()),
731f4a2713aSLionel Sambuc DependentTemplateSpecializationTypes(this_()),
732f4a2713aSLionel Sambuc SubstTemplateTemplateParmPacks(this_()),
733*0a6a1f1dSLionel Sambuc GlobalNestedNameSpecifier(nullptr), Int128Decl(nullptr),
734*0a6a1f1dSLionel Sambuc UInt128Decl(nullptr), Float128StubDecl(nullptr),
735*0a6a1f1dSLionel Sambuc BuiltinVaListDecl(nullptr), ObjCIdDecl(nullptr), ObjCSelDecl(nullptr),
736*0a6a1f1dSLionel Sambuc ObjCClassDecl(nullptr), ObjCProtocolClassDecl(nullptr), BOOLDecl(nullptr),
737*0a6a1f1dSLionel Sambuc CFConstantStringTypeDecl(nullptr), ObjCInstanceTypeDecl(nullptr),
738*0a6a1f1dSLionel Sambuc FILEDecl(nullptr), jmp_bufDecl(nullptr), sigjmp_bufDecl(nullptr),
739*0a6a1f1dSLionel Sambuc ucontext_tDecl(nullptr), BlockDescriptorType(nullptr),
740*0a6a1f1dSLionel Sambuc BlockDescriptorExtendedType(nullptr), cudaConfigureCallDecl(nullptr),
741f4a2713aSLionel Sambuc FirstLocalImport(), LastLocalImport(),
742f4a2713aSLionel Sambuc SourceMgr(SM), LangOpts(LOpts),
743*0a6a1f1dSLionel Sambuc SanitizerBL(new SanitizerBlacklist(LangOpts.SanitizerBlacklistFile, SM)),
744*0a6a1f1dSLionel Sambuc AddrSpaceMap(nullptr), Target(nullptr), PrintingPolicy(LOpts),
745*0a6a1f1dSLionel Sambuc Idents(idents), Selectors(sels), BuiltinInfo(builtins),
746*0a6a1f1dSLionel Sambuc DeclarationNames(*this), ExternalSource(nullptr), Listener(nullptr),
747f4a2713aSLionel Sambuc Comments(SM), CommentsLoaded(false),
748*0a6a1f1dSLionel Sambuc CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), LastSDM(nullptr, 0) {
749f4a2713aSLionel Sambuc TUDecl = TranslationUnitDecl::Create(*this);
750f4a2713aSLionel Sambuc }
751f4a2713aSLionel Sambuc
~ASTContext()752f4a2713aSLionel Sambuc ASTContext::~ASTContext() {
753*0a6a1f1dSLionel Sambuc ReleaseParentMapEntries();
754*0a6a1f1dSLionel Sambuc
755f4a2713aSLionel Sambuc // Release the DenseMaps associated with DeclContext objects.
756f4a2713aSLionel Sambuc // FIXME: Is this the ideal solution?
757f4a2713aSLionel Sambuc ReleaseDeclContextMaps();
758f4a2713aSLionel Sambuc
759f4a2713aSLionel Sambuc // Call all of the deallocation functions on all of their targets.
760f4a2713aSLionel Sambuc for (DeallocationMap::const_iterator I = Deallocations.begin(),
761f4a2713aSLionel Sambuc E = Deallocations.end(); I != E; ++I)
762f4a2713aSLionel Sambuc for (unsigned J = 0, N = I->second.size(); J != N; ++J)
763f4a2713aSLionel Sambuc (I->first)((I->second)[J]);
764f4a2713aSLionel Sambuc
765f4a2713aSLionel Sambuc // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
766f4a2713aSLionel Sambuc // because they can contain DenseMaps.
767f4a2713aSLionel Sambuc for (llvm::DenseMap<const ObjCContainerDecl*,
768f4a2713aSLionel Sambuc const ASTRecordLayout*>::iterator
769f4a2713aSLionel Sambuc I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
770f4a2713aSLionel Sambuc // Increment in loop to prevent using deallocated memory.
771f4a2713aSLionel Sambuc if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
772f4a2713aSLionel Sambuc R->Destroy(*this);
773f4a2713aSLionel Sambuc
774f4a2713aSLionel Sambuc for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
775f4a2713aSLionel Sambuc I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
776f4a2713aSLionel Sambuc // Increment in loop to prevent using deallocated memory.
777f4a2713aSLionel Sambuc if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
778f4a2713aSLionel Sambuc R->Destroy(*this);
779f4a2713aSLionel Sambuc }
780f4a2713aSLionel Sambuc
781f4a2713aSLionel Sambuc for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
782f4a2713aSLionel Sambuc AEnd = DeclAttrs.end();
783f4a2713aSLionel Sambuc A != AEnd; ++A)
784f4a2713aSLionel Sambuc A->second->~AttrVec();
785f4a2713aSLionel Sambuc
786*0a6a1f1dSLionel Sambuc llvm::DeleteContainerSeconds(MangleNumberingContexts);
787*0a6a1f1dSLionel Sambuc }
788*0a6a1f1dSLionel Sambuc
ReleaseParentMapEntries()789*0a6a1f1dSLionel Sambuc void ASTContext::ReleaseParentMapEntries() {
790*0a6a1f1dSLionel Sambuc if (!AllParents) return;
791*0a6a1f1dSLionel Sambuc for (const auto &Entry : *AllParents) {
792*0a6a1f1dSLionel Sambuc if (Entry.second.is<ast_type_traits::DynTypedNode *>()) {
793*0a6a1f1dSLionel Sambuc delete Entry.second.get<ast_type_traits::DynTypedNode *>();
794*0a6a1f1dSLionel Sambuc } else {
795*0a6a1f1dSLionel Sambuc assert(Entry.second.is<ParentVector *>());
796*0a6a1f1dSLionel Sambuc delete Entry.second.get<ParentVector *>();
797*0a6a1f1dSLionel Sambuc }
798*0a6a1f1dSLionel Sambuc }
799f4a2713aSLionel Sambuc }
800f4a2713aSLionel Sambuc
AddDeallocation(void (* Callback)(void *),void * Data)801f4a2713aSLionel Sambuc void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
802f4a2713aSLionel Sambuc Deallocations[Callback].push_back(Data);
803f4a2713aSLionel Sambuc }
804f4a2713aSLionel Sambuc
805f4a2713aSLionel Sambuc void
setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source)806*0a6a1f1dSLionel Sambuc ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) {
807*0a6a1f1dSLionel Sambuc ExternalSource = Source;
808f4a2713aSLionel Sambuc }
809f4a2713aSLionel Sambuc
PrintStats() const810f4a2713aSLionel Sambuc void ASTContext::PrintStats() const {
811f4a2713aSLionel Sambuc llvm::errs() << "\n*** AST Context Stats:\n";
812f4a2713aSLionel Sambuc llvm::errs() << " " << Types.size() << " types total.\n";
813f4a2713aSLionel Sambuc
814f4a2713aSLionel Sambuc unsigned counts[] = {
815f4a2713aSLionel Sambuc #define TYPE(Name, Parent) 0,
816f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Name, Parent)
817f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
818f4a2713aSLionel Sambuc 0 // Extra
819f4a2713aSLionel Sambuc };
820f4a2713aSLionel Sambuc
821f4a2713aSLionel Sambuc for (unsigned i = 0, e = Types.size(); i != e; ++i) {
822f4a2713aSLionel Sambuc Type *T = Types[i];
823f4a2713aSLionel Sambuc counts[(unsigned)T->getTypeClass()]++;
824f4a2713aSLionel Sambuc }
825f4a2713aSLionel Sambuc
826f4a2713aSLionel Sambuc unsigned Idx = 0;
827f4a2713aSLionel Sambuc unsigned TotalBytes = 0;
828f4a2713aSLionel Sambuc #define TYPE(Name, Parent) \
829f4a2713aSLionel Sambuc if (counts[Idx]) \
830f4a2713aSLionel Sambuc llvm::errs() << " " << counts[Idx] << " " << #Name \
831f4a2713aSLionel Sambuc << " types\n"; \
832f4a2713aSLionel Sambuc TotalBytes += counts[Idx] * sizeof(Name##Type); \
833f4a2713aSLionel Sambuc ++Idx;
834f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Name, Parent)
835f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
836f4a2713aSLionel Sambuc
837f4a2713aSLionel Sambuc llvm::errs() << "Total bytes = " << TotalBytes << "\n";
838f4a2713aSLionel Sambuc
839f4a2713aSLionel Sambuc // Implicit special member functions.
840f4a2713aSLionel Sambuc llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/"
841f4a2713aSLionel Sambuc << NumImplicitDefaultConstructors
842f4a2713aSLionel Sambuc << " implicit default constructors created\n";
843f4a2713aSLionel Sambuc llvm::errs() << NumImplicitCopyConstructorsDeclared << "/"
844f4a2713aSLionel Sambuc << NumImplicitCopyConstructors
845f4a2713aSLionel Sambuc << " implicit copy constructors created\n";
846f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus)
847f4a2713aSLionel Sambuc llvm::errs() << NumImplicitMoveConstructorsDeclared << "/"
848f4a2713aSLionel Sambuc << NumImplicitMoveConstructors
849f4a2713aSLionel Sambuc << " implicit move constructors created\n";
850f4a2713aSLionel Sambuc llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/"
851f4a2713aSLionel Sambuc << NumImplicitCopyAssignmentOperators
852f4a2713aSLionel Sambuc << " implicit copy assignment operators created\n";
853f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus)
854f4a2713aSLionel Sambuc llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/"
855f4a2713aSLionel Sambuc << NumImplicitMoveAssignmentOperators
856f4a2713aSLionel Sambuc << " implicit move assignment operators created\n";
857f4a2713aSLionel Sambuc llvm::errs() << NumImplicitDestructorsDeclared << "/"
858f4a2713aSLionel Sambuc << NumImplicitDestructors
859f4a2713aSLionel Sambuc << " implicit destructors created\n";
860f4a2713aSLionel Sambuc
861*0a6a1f1dSLionel Sambuc if (ExternalSource) {
862f4a2713aSLionel Sambuc llvm::errs() << "\n";
863f4a2713aSLionel Sambuc ExternalSource->PrintStats();
864f4a2713aSLionel Sambuc }
865f4a2713aSLionel Sambuc
866f4a2713aSLionel Sambuc BumpAlloc.PrintStats();
867f4a2713aSLionel Sambuc }
868f4a2713aSLionel Sambuc
buildImplicitRecord(StringRef Name,RecordDecl::TagKind TK) const869*0a6a1f1dSLionel Sambuc RecordDecl *ASTContext::buildImplicitRecord(StringRef Name,
870*0a6a1f1dSLionel Sambuc RecordDecl::TagKind TK) const {
871*0a6a1f1dSLionel Sambuc SourceLocation Loc;
872*0a6a1f1dSLionel Sambuc RecordDecl *NewDecl;
873*0a6a1f1dSLionel Sambuc if (getLangOpts().CPlusPlus)
874*0a6a1f1dSLionel Sambuc NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc,
875*0a6a1f1dSLionel Sambuc Loc, &Idents.get(Name));
876*0a6a1f1dSLionel Sambuc else
877*0a6a1f1dSLionel Sambuc NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc,
878*0a6a1f1dSLionel Sambuc &Idents.get(Name));
879*0a6a1f1dSLionel Sambuc NewDecl->setImplicit();
880*0a6a1f1dSLionel Sambuc return NewDecl;
881f4a2713aSLionel Sambuc }
882f4a2713aSLionel Sambuc
buildImplicitTypedef(QualType T,StringRef Name) const883*0a6a1f1dSLionel Sambuc TypedefDecl *ASTContext::buildImplicitTypedef(QualType T,
884*0a6a1f1dSLionel Sambuc StringRef Name) const {
885*0a6a1f1dSLionel Sambuc TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
886*0a6a1f1dSLionel Sambuc TypedefDecl *NewDecl = TypedefDecl::Create(
887*0a6a1f1dSLionel Sambuc const_cast<ASTContext &>(*this), getTranslationUnitDecl(),
888*0a6a1f1dSLionel Sambuc SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo);
889*0a6a1f1dSLionel Sambuc NewDecl->setImplicit();
890*0a6a1f1dSLionel Sambuc return NewDecl;
891*0a6a1f1dSLionel Sambuc }
892*0a6a1f1dSLionel Sambuc
getInt128Decl() const893*0a6a1f1dSLionel Sambuc TypedefDecl *ASTContext::getInt128Decl() const {
894*0a6a1f1dSLionel Sambuc if (!Int128Decl)
895*0a6a1f1dSLionel Sambuc Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t");
896f4a2713aSLionel Sambuc return Int128Decl;
897f4a2713aSLionel Sambuc }
898f4a2713aSLionel Sambuc
getUInt128Decl() const899f4a2713aSLionel Sambuc TypedefDecl *ASTContext::getUInt128Decl() const {
900*0a6a1f1dSLionel Sambuc if (!UInt128Decl)
901*0a6a1f1dSLionel Sambuc UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t");
902f4a2713aSLionel Sambuc return UInt128Decl;
903f4a2713aSLionel Sambuc }
904f4a2713aSLionel Sambuc
getFloat128StubType() const905f4a2713aSLionel Sambuc TypeDecl *ASTContext::getFloat128StubType() const {
906f4a2713aSLionel Sambuc assert(LangOpts.CPlusPlus && "should only be called for c++");
907*0a6a1f1dSLionel Sambuc if (!Float128StubDecl)
908*0a6a1f1dSLionel Sambuc Float128StubDecl = buildImplicitRecord("__float128");
909f4a2713aSLionel Sambuc
910f4a2713aSLionel Sambuc return Float128StubDecl;
911f4a2713aSLionel Sambuc }
912f4a2713aSLionel Sambuc
InitBuiltinType(CanQualType & R,BuiltinType::Kind K)913f4a2713aSLionel Sambuc void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
914f4a2713aSLionel Sambuc BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
915f4a2713aSLionel Sambuc R = CanQualType::CreateUnsafe(QualType(Ty, 0));
916f4a2713aSLionel Sambuc Types.push_back(Ty);
917f4a2713aSLionel Sambuc }
918f4a2713aSLionel Sambuc
InitBuiltinTypes(const TargetInfo & Target)919f4a2713aSLionel Sambuc void ASTContext::InitBuiltinTypes(const TargetInfo &Target) {
920f4a2713aSLionel Sambuc assert((!this->Target || this->Target == &Target) &&
921f4a2713aSLionel Sambuc "Incorrect target reinitialization");
922f4a2713aSLionel Sambuc assert(VoidTy.isNull() && "Context reinitialized?");
923f4a2713aSLionel Sambuc
924f4a2713aSLionel Sambuc this->Target = &Target;
925f4a2713aSLionel Sambuc
926f4a2713aSLionel Sambuc ABI.reset(createCXXABI(Target));
927f4a2713aSLionel Sambuc AddrSpaceMap = getAddressSpaceMap(Target, LangOpts);
928f4a2713aSLionel Sambuc AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(Target, LangOpts);
929f4a2713aSLionel Sambuc
930f4a2713aSLionel Sambuc // C99 6.2.5p19.
931f4a2713aSLionel Sambuc InitBuiltinType(VoidTy, BuiltinType::Void);
932f4a2713aSLionel Sambuc
933f4a2713aSLionel Sambuc // C99 6.2.5p2.
934f4a2713aSLionel Sambuc InitBuiltinType(BoolTy, BuiltinType::Bool);
935f4a2713aSLionel Sambuc // C99 6.2.5p3.
936f4a2713aSLionel Sambuc if (LangOpts.CharIsSigned)
937f4a2713aSLionel Sambuc InitBuiltinType(CharTy, BuiltinType::Char_S);
938f4a2713aSLionel Sambuc else
939f4a2713aSLionel Sambuc InitBuiltinType(CharTy, BuiltinType::Char_U);
940f4a2713aSLionel Sambuc // C99 6.2.5p4.
941f4a2713aSLionel Sambuc InitBuiltinType(SignedCharTy, BuiltinType::SChar);
942f4a2713aSLionel Sambuc InitBuiltinType(ShortTy, BuiltinType::Short);
943f4a2713aSLionel Sambuc InitBuiltinType(IntTy, BuiltinType::Int);
944f4a2713aSLionel Sambuc InitBuiltinType(LongTy, BuiltinType::Long);
945f4a2713aSLionel Sambuc InitBuiltinType(LongLongTy, BuiltinType::LongLong);
946f4a2713aSLionel Sambuc
947f4a2713aSLionel Sambuc // C99 6.2.5p6.
948f4a2713aSLionel Sambuc InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
949f4a2713aSLionel Sambuc InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
950f4a2713aSLionel Sambuc InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
951f4a2713aSLionel Sambuc InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
952f4a2713aSLionel Sambuc InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
953f4a2713aSLionel Sambuc
954f4a2713aSLionel Sambuc // C99 6.2.5p10.
955f4a2713aSLionel Sambuc InitBuiltinType(FloatTy, BuiltinType::Float);
956f4a2713aSLionel Sambuc InitBuiltinType(DoubleTy, BuiltinType::Double);
957f4a2713aSLionel Sambuc InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
958f4a2713aSLionel Sambuc
959f4a2713aSLionel Sambuc // GNU extension, 128-bit integers.
960f4a2713aSLionel Sambuc InitBuiltinType(Int128Ty, BuiltinType::Int128);
961f4a2713aSLionel Sambuc InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
962f4a2713aSLionel Sambuc
963f4a2713aSLionel Sambuc // C++ 3.9.1p5
964f4a2713aSLionel Sambuc if (TargetInfo::isTypeSigned(Target.getWCharType()))
965f4a2713aSLionel Sambuc InitBuiltinType(WCharTy, BuiltinType::WChar_S);
966f4a2713aSLionel Sambuc else // -fshort-wchar makes wchar_t be unsigned.
967f4a2713aSLionel Sambuc InitBuiltinType(WCharTy, BuiltinType::WChar_U);
968f4a2713aSLionel Sambuc if (LangOpts.CPlusPlus && LangOpts.WChar)
969f4a2713aSLionel Sambuc WideCharTy = WCharTy;
970f4a2713aSLionel Sambuc else {
971f4a2713aSLionel Sambuc // C99 (or C++ using -fno-wchar).
972f4a2713aSLionel Sambuc WideCharTy = getFromTargetType(Target.getWCharType());
973f4a2713aSLionel Sambuc }
974f4a2713aSLionel Sambuc
975f4a2713aSLionel Sambuc WIntTy = getFromTargetType(Target.getWIntType());
976f4a2713aSLionel Sambuc
977f4a2713aSLionel Sambuc if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
978f4a2713aSLionel Sambuc InitBuiltinType(Char16Ty, BuiltinType::Char16);
979f4a2713aSLionel Sambuc else // C99
980f4a2713aSLionel Sambuc Char16Ty = getFromTargetType(Target.getChar16Type());
981f4a2713aSLionel Sambuc
982f4a2713aSLionel Sambuc if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
983f4a2713aSLionel Sambuc InitBuiltinType(Char32Ty, BuiltinType::Char32);
984f4a2713aSLionel Sambuc else // C99
985f4a2713aSLionel Sambuc Char32Ty = getFromTargetType(Target.getChar32Type());
986f4a2713aSLionel Sambuc
987f4a2713aSLionel Sambuc // Placeholder type for type-dependent expressions whose type is
988f4a2713aSLionel Sambuc // completely unknown. No code should ever check a type against
989f4a2713aSLionel Sambuc // DependentTy and users should never see it; however, it is here to
990f4a2713aSLionel Sambuc // help diagnose failures to properly check for type-dependent
991f4a2713aSLionel Sambuc // expressions.
992f4a2713aSLionel Sambuc InitBuiltinType(DependentTy, BuiltinType::Dependent);
993f4a2713aSLionel Sambuc
994f4a2713aSLionel Sambuc // Placeholder type for functions.
995f4a2713aSLionel Sambuc InitBuiltinType(OverloadTy, BuiltinType::Overload);
996f4a2713aSLionel Sambuc
997f4a2713aSLionel Sambuc // Placeholder type for bound members.
998f4a2713aSLionel Sambuc InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember);
999f4a2713aSLionel Sambuc
1000f4a2713aSLionel Sambuc // Placeholder type for pseudo-objects.
1001f4a2713aSLionel Sambuc InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject);
1002f4a2713aSLionel Sambuc
1003f4a2713aSLionel Sambuc // "any" type; useful for debugger-like clients.
1004f4a2713aSLionel Sambuc InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny);
1005f4a2713aSLionel Sambuc
1006f4a2713aSLionel Sambuc // Placeholder type for unbridged ARC casts.
1007f4a2713aSLionel Sambuc InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast);
1008f4a2713aSLionel Sambuc
1009f4a2713aSLionel Sambuc // Placeholder type for builtin functions.
1010f4a2713aSLionel Sambuc InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn);
1011f4a2713aSLionel Sambuc
1012f4a2713aSLionel Sambuc // C99 6.2.5p11.
1013f4a2713aSLionel Sambuc FloatComplexTy = getComplexType(FloatTy);
1014f4a2713aSLionel Sambuc DoubleComplexTy = getComplexType(DoubleTy);
1015f4a2713aSLionel Sambuc LongDoubleComplexTy = getComplexType(LongDoubleTy);
1016f4a2713aSLionel Sambuc
1017f4a2713aSLionel Sambuc // Builtin types for 'id', 'Class', and 'SEL'.
1018f4a2713aSLionel Sambuc InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
1019f4a2713aSLionel Sambuc InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
1020f4a2713aSLionel Sambuc InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
1021f4a2713aSLionel Sambuc
1022f4a2713aSLionel Sambuc if (LangOpts.OpenCL) {
1023f4a2713aSLionel Sambuc InitBuiltinType(OCLImage1dTy, BuiltinType::OCLImage1d);
1024f4a2713aSLionel Sambuc InitBuiltinType(OCLImage1dArrayTy, BuiltinType::OCLImage1dArray);
1025f4a2713aSLionel Sambuc InitBuiltinType(OCLImage1dBufferTy, BuiltinType::OCLImage1dBuffer);
1026f4a2713aSLionel Sambuc InitBuiltinType(OCLImage2dTy, BuiltinType::OCLImage2d);
1027f4a2713aSLionel Sambuc InitBuiltinType(OCLImage2dArrayTy, BuiltinType::OCLImage2dArray);
1028f4a2713aSLionel Sambuc InitBuiltinType(OCLImage3dTy, BuiltinType::OCLImage3d);
1029f4a2713aSLionel Sambuc
1030f4a2713aSLionel Sambuc InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler);
1031f4a2713aSLionel Sambuc InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent);
1032f4a2713aSLionel Sambuc }
1033f4a2713aSLionel Sambuc
1034f4a2713aSLionel Sambuc // Builtin type for __objc_yes and __objc_no
1035f4a2713aSLionel Sambuc ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ?
1036f4a2713aSLionel Sambuc SignedCharTy : BoolTy);
1037f4a2713aSLionel Sambuc
1038f4a2713aSLionel Sambuc ObjCConstantStringType = QualType();
1039f4a2713aSLionel Sambuc
1040f4a2713aSLionel Sambuc ObjCSuperType = QualType();
1041f4a2713aSLionel Sambuc
1042f4a2713aSLionel Sambuc // void * type
1043f4a2713aSLionel Sambuc VoidPtrTy = getPointerType(VoidTy);
1044f4a2713aSLionel Sambuc
1045f4a2713aSLionel Sambuc // nullptr type (C++0x 2.14.7)
1046f4a2713aSLionel Sambuc InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
1047f4a2713aSLionel Sambuc
1048f4a2713aSLionel Sambuc // half type (OpenCL 6.1.1.1) / ARM NEON __fp16
1049f4a2713aSLionel Sambuc InitBuiltinType(HalfTy, BuiltinType::Half);
1050f4a2713aSLionel Sambuc
1051f4a2713aSLionel Sambuc // Builtin type used to help define __builtin_va_list.
1052f4a2713aSLionel Sambuc VaListTagTy = QualType();
1053f4a2713aSLionel Sambuc }
1054f4a2713aSLionel Sambuc
getDiagnostics() const1055f4a2713aSLionel Sambuc DiagnosticsEngine &ASTContext::getDiagnostics() const {
1056f4a2713aSLionel Sambuc return SourceMgr.getDiagnostics();
1057f4a2713aSLionel Sambuc }
1058f4a2713aSLionel Sambuc
getDeclAttrs(const Decl * D)1059f4a2713aSLionel Sambuc AttrVec& ASTContext::getDeclAttrs(const Decl *D) {
1060f4a2713aSLionel Sambuc AttrVec *&Result = DeclAttrs[D];
1061f4a2713aSLionel Sambuc if (!Result) {
1062f4a2713aSLionel Sambuc void *Mem = Allocate(sizeof(AttrVec));
1063f4a2713aSLionel Sambuc Result = new (Mem) AttrVec;
1064f4a2713aSLionel Sambuc }
1065f4a2713aSLionel Sambuc
1066f4a2713aSLionel Sambuc return *Result;
1067f4a2713aSLionel Sambuc }
1068f4a2713aSLionel Sambuc
1069f4a2713aSLionel Sambuc /// \brief Erase the attributes corresponding to the given declaration.
eraseDeclAttrs(const Decl * D)1070f4a2713aSLionel Sambuc void ASTContext::eraseDeclAttrs(const Decl *D) {
1071f4a2713aSLionel Sambuc llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
1072f4a2713aSLionel Sambuc if (Pos != DeclAttrs.end()) {
1073f4a2713aSLionel Sambuc Pos->second->~AttrVec();
1074f4a2713aSLionel Sambuc DeclAttrs.erase(Pos);
1075f4a2713aSLionel Sambuc }
1076f4a2713aSLionel Sambuc }
1077f4a2713aSLionel Sambuc
1078f4a2713aSLionel Sambuc // FIXME: Remove ?
1079f4a2713aSLionel Sambuc MemberSpecializationInfo *
getInstantiatedFromStaticDataMember(const VarDecl * Var)1080f4a2713aSLionel Sambuc ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
1081f4a2713aSLionel Sambuc assert(Var->isStaticDataMember() && "Not a static data member");
1082f4a2713aSLionel Sambuc return getTemplateOrSpecializationInfo(Var)
1083f4a2713aSLionel Sambuc .dyn_cast<MemberSpecializationInfo *>();
1084f4a2713aSLionel Sambuc }
1085f4a2713aSLionel Sambuc
1086f4a2713aSLionel Sambuc ASTContext::TemplateOrSpecializationInfo
getTemplateOrSpecializationInfo(const VarDecl * Var)1087f4a2713aSLionel Sambuc ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) {
1088f4a2713aSLionel Sambuc llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos =
1089f4a2713aSLionel Sambuc TemplateOrInstantiation.find(Var);
1090f4a2713aSLionel Sambuc if (Pos == TemplateOrInstantiation.end())
1091f4a2713aSLionel Sambuc return TemplateOrSpecializationInfo();
1092f4a2713aSLionel Sambuc
1093f4a2713aSLionel Sambuc return Pos->second;
1094f4a2713aSLionel Sambuc }
1095f4a2713aSLionel Sambuc
1096f4a2713aSLionel Sambuc void
setInstantiatedFromStaticDataMember(VarDecl * Inst,VarDecl * Tmpl,TemplateSpecializationKind TSK,SourceLocation PointOfInstantiation)1097f4a2713aSLionel Sambuc ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
1098f4a2713aSLionel Sambuc TemplateSpecializationKind TSK,
1099f4a2713aSLionel Sambuc SourceLocation PointOfInstantiation) {
1100f4a2713aSLionel Sambuc assert(Inst->isStaticDataMember() && "Not a static data member");
1101f4a2713aSLionel Sambuc assert(Tmpl->isStaticDataMember() && "Not a static data member");
1102f4a2713aSLionel Sambuc setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo(
1103f4a2713aSLionel Sambuc Tmpl, TSK, PointOfInstantiation));
1104f4a2713aSLionel Sambuc }
1105f4a2713aSLionel Sambuc
1106f4a2713aSLionel Sambuc void
setTemplateOrSpecializationInfo(VarDecl * Inst,TemplateOrSpecializationInfo TSI)1107f4a2713aSLionel Sambuc ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst,
1108f4a2713aSLionel Sambuc TemplateOrSpecializationInfo TSI) {
1109f4a2713aSLionel Sambuc assert(!TemplateOrInstantiation[Inst] &&
1110f4a2713aSLionel Sambuc "Already noted what the variable was instantiated from");
1111f4a2713aSLionel Sambuc TemplateOrInstantiation[Inst] = TSI;
1112f4a2713aSLionel Sambuc }
1113f4a2713aSLionel Sambuc
getClassScopeSpecializationPattern(const FunctionDecl * FD)1114f4a2713aSLionel Sambuc FunctionDecl *ASTContext::getClassScopeSpecializationPattern(
1115f4a2713aSLionel Sambuc const FunctionDecl *FD){
1116f4a2713aSLionel Sambuc assert(FD && "Specialization is 0");
1117f4a2713aSLionel Sambuc llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos
1118f4a2713aSLionel Sambuc = ClassScopeSpecializationPattern.find(FD);
1119f4a2713aSLionel Sambuc if (Pos == ClassScopeSpecializationPattern.end())
1120*0a6a1f1dSLionel Sambuc return nullptr;
1121f4a2713aSLionel Sambuc
1122f4a2713aSLionel Sambuc return Pos->second;
1123f4a2713aSLionel Sambuc }
1124f4a2713aSLionel Sambuc
setClassScopeSpecializationPattern(FunctionDecl * FD,FunctionDecl * Pattern)1125f4a2713aSLionel Sambuc void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD,
1126f4a2713aSLionel Sambuc FunctionDecl *Pattern) {
1127f4a2713aSLionel Sambuc assert(FD && "Specialization is 0");
1128f4a2713aSLionel Sambuc assert(Pattern && "Class scope specialization pattern is 0");
1129f4a2713aSLionel Sambuc ClassScopeSpecializationPattern[FD] = Pattern;
1130f4a2713aSLionel Sambuc }
1131f4a2713aSLionel Sambuc
1132f4a2713aSLionel Sambuc NamedDecl *
getInstantiatedFromUsingDecl(UsingDecl * UUD)1133f4a2713aSLionel Sambuc ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
1134f4a2713aSLionel Sambuc llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
1135f4a2713aSLionel Sambuc = InstantiatedFromUsingDecl.find(UUD);
1136f4a2713aSLionel Sambuc if (Pos == InstantiatedFromUsingDecl.end())
1137*0a6a1f1dSLionel Sambuc return nullptr;
1138f4a2713aSLionel Sambuc
1139f4a2713aSLionel Sambuc return Pos->second;
1140f4a2713aSLionel Sambuc }
1141f4a2713aSLionel Sambuc
1142f4a2713aSLionel Sambuc void
setInstantiatedFromUsingDecl(UsingDecl * Inst,NamedDecl * Pattern)1143f4a2713aSLionel Sambuc ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
1144f4a2713aSLionel Sambuc assert((isa<UsingDecl>(Pattern) ||
1145f4a2713aSLionel Sambuc isa<UnresolvedUsingValueDecl>(Pattern) ||
1146f4a2713aSLionel Sambuc isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
1147f4a2713aSLionel Sambuc "pattern decl is not a using decl");
1148f4a2713aSLionel Sambuc assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
1149f4a2713aSLionel Sambuc InstantiatedFromUsingDecl[Inst] = Pattern;
1150f4a2713aSLionel Sambuc }
1151f4a2713aSLionel Sambuc
1152f4a2713aSLionel Sambuc UsingShadowDecl *
getInstantiatedFromUsingShadowDecl(UsingShadowDecl * Inst)1153f4a2713aSLionel Sambuc ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
1154f4a2713aSLionel Sambuc llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
1155f4a2713aSLionel Sambuc = InstantiatedFromUsingShadowDecl.find(Inst);
1156f4a2713aSLionel Sambuc if (Pos == InstantiatedFromUsingShadowDecl.end())
1157*0a6a1f1dSLionel Sambuc return nullptr;
1158f4a2713aSLionel Sambuc
1159f4a2713aSLionel Sambuc return Pos->second;
1160f4a2713aSLionel Sambuc }
1161f4a2713aSLionel Sambuc
1162f4a2713aSLionel Sambuc void
setInstantiatedFromUsingShadowDecl(UsingShadowDecl * Inst,UsingShadowDecl * Pattern)1163f4a2713aSLionel Sambuc ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
1164f4a2713aSLionel Sambuc UsingShadowDecl *Pattern) {
1165f4a2713aSLionel Sambuc assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
1166f4a2713aSLionel Sambuc InstantiatedFromUsingShadowDecl[Inst] = Pattern;
1167f4a2713aSLionel Sambuc }
1168f4a2713aSLionel Sambuc
getInstantiatedFromUnnamedFieldDecl(FieldDecl * Field)1169f4a2713aSLionel Sambuc FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
1170f4a2713aSLionel Sambuc llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
1171f4a2713aSLionel Sambuc = InstantiatedFromUnnamedFieldDecl.find(Field);
1172f4a2713aSLionel Sambuc if (Pos == InstantiatedFromUnnamedFieldDecl.end())
1173*0a6a1f1dSLionel Sambuc return nullptr;
1174f4a2713aSLionel Sambuc
1175f4a2713aSLionel Sambuc return Pos->second;
1176f4a2713aSLionel Sambuc }
1177f4a2713aSLionel Sambuc
setInstantiatedFromUnnamedFieldDecl(FieldDecl * Inst,FieldDecl * Tmpl)1178f4a2713aSLionel Sambuc void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
1179f4a2713aSLionel Sambuc FieldDecl *Tmpl) {
1180f4a2713aSLionel Sambuc assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
1181f4a2713aSLionel Sambuc assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
1182f4a2713aSLionel Sambuc assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
1183f4a2713aSLionel Sambuc "Already noted what unnamed field was instantiated from");
1184f4a2713aSLionel Sambuc
1185f4a2713aSLionel Sambuc InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
1186f4a2713aSLionel Sambuc }
1187f4a2713aSLionel Sambuc
1188f4a2713aSLionel Sambuc ASTContext::overridden_cxx_method_iterator
overridden_methods_begin(const CXXMethodDecl * Method) const1189f4a2713aSLionel Sambuc ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
1190f4a2713aSLionel Sambuc llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
1191f4a2713aSLionel Sambuc = OverriddenMethods.find(Method->getCanonicalDecl());
1192f4a2713aSLionel Sambuc if (Pos == OverriddenMethods.end())
1193*0a6a1f1dSLionel Sambuc return nullptr;
1194f4a2713aSLionel Sambuc
1195f4a2713aSLionel Sambuc return Pos->second.begin();
1196f4a2713aSLionel Sambuc }
1197f4a2713aSLionel Sambuc
1198f4a2713aSLionel Sambuc ASTContext::overridden_cxx_method_iterator
overridden_methods_end(const CXXMethodDecl * Method) const1199f4a2713aSLionel Sambuc ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
1200f4a2713aSLionel Sambuc llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
1201f4a2713aSLionel Sambuc = OverriddenMethods.find(Method->getCanonicalDecl());
1202f4a2713aSLionel Sambuc if (Pos == OverriddenMethods.end())
1203*0a6a1f1dSLionel Sambuc return nullptr;
1204f4a2713aSLionel Sambuc
1205f4a2713aSLionel Sambuc return Pos->second.end();
1206f4a2713aSLionel Sambuc }
1207f4a2713aSLionel Sambuc
1208f4a2713aSLionel Sambuc unsigned
overridden_methods_size(const CXXMethodDecl * Method) const1209f4a2713aSLionel Sambuc ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
1210f4a2713aSLionel Sambuc llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
1211f4a2713aSLionel Sambuc = OverriddenMethods.find(Method->getCanonicalDecl());
1212f4a2713aSLionel Sambuc if (Pos == OverriddenMethods.end())
1213f4a2713aSLionel Sambuc return 0;
1214f4a2713aSLionel Sambuc
1215f4a2713aSLionel Sambuc return Pos->second.size();
1216f4a2713aSLionel Sambuc }
1217f4a2713aSLionel Sambuc
addOverriddenMethod(const CXXMethodDecl * Method,const CXXMethodDecl * Overridden)1218f4a2713aSLionel Sambuc void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
1219f4a2713aSLionel Sambuc const CXXMethodDecl *Overridden) {
1220f4a2713aSLionel Sambuc assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl());
1221f4a2713aSLionel Sambuc OverriddenMethods[Method].push_back(Overridden);
1222f4a2713aSLionel Sambuc }
1223f4a2713aSLionel Sambuc
getOverriddenMethods(const NamedDecl * D,SmallVectorImpl<const NamedDecl * > & Overridden) const1224f4a2713aSLionel Sambuc void ASTContext::getOverriddenMethods(
1225f4a2713aSLionel Sambuc const NamedDecl *D,
1226f4a2713aSLionel Sambuc SmallVectorImpl<const NamedDecl *> &Overridden) const {
1227f4a2713aSLionel Sambuc assert(D);
1228f4a2713aSLionel Sambuc
1229f4a2713aSLionel Sambuc if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
1230f4a2713aSLionel Sambuc Overridden.append(overridden_methods_begin(CXXMethod),
1231f4a2713aSLionel Sambuc overridden_methods_end(CXXMethod));
1232f4a2713aSLionel Sambuc return;
1233f4a2713aSLionel Sambuc }
1234f4a2713aSLionel Sambuc
1235f4a2713aSLionel Sambuc const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
1236f4a2713aSLionel Sambuc if (!Method)
1237f4a2713aSLionel Sambuc return;
1238f4a2713aSLionel Sambuc
1239f4a2713aSLionel Sambuc SmallVector<const ObjCMethodDecl *, 8> OverDecls;
1240f4a2713aSLionel Sambuc Method->getOverriddenMethods(OverDecls);
1241f4a2713aSLionel Sambuc Overridden.append(OverDecls.begin(), OverDecls.end());
1242f4a2713aSLionel Sambuc }
1243f4a2713aSLionel Sambuc
addedLocalImportDecl(ImportDecl * Import)1244f4a2713aSLionel Sambuc void ASTContext::addedLocalImportDecl(ImportDecl *Import) {
1245f4a2713aSLionel Sambuc assert(!Import->NextLocalImport && "Import declaration already in the chain");
1246f4a2713aSLionel Sambuc assert(!Import->isFromASTFile() && "Non-local import declaration");
1247f4a2713aSLionel Sambuc if (!FirstLocalImport) {
1248f4a2713aSLionel Sambuc FirstLocalImport = Import;
1249f4a2713aSLionel Sambuc LastLocalImport = Import;
1250f4a2713aSLionel Sambuc return;
1251f4a2713aSLionel Sambuc }
1252f4a2713aSLionel Sambuc
1253f4a2713aSLionel Sambuc LastLocalImport->NextLocalImport = Import;
1254f4a2713aSLionel Sambuc LastLocalImport = Import;
1255f4a2713aSLionel Sambuc }
1256f4a2713aSLionel Sambuc
1257f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1258f4a2713aSLionel Sambuc // Type Sizing and Analysis
1259f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1260f4a2713aSLionel Sambuc
1261f4a2713aSLionel Sambuc /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
1262f4a2713aSLionel Sambuc /// scalar floating point type.
getFloatTypeSemantics(QualType T) const1263f4a2713aSLionel Sambuc const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
1264f4a2713aSLionel Sambuc const BuiltinType *BT = T->getAs<BuiltinType>();
1265f4a2713aSLionel Sambuc assert(BT && "Not a floating point type!");
1266f4a2713aSLionel Sambuc switch (BT->getKind()) {
1267f4a2713aSLionel Sambuc default: llvm_unreachable("Not a floating point type!");
1268f4a2713aSLionel Sambuc case BuiltinType::Half: return Target->getHalfFormat();
1269f4a2713aSLionel Sambuc case BuiltinType::Float: return Target->getFloatFormat();
1270f4a2713aSLionel Sambuc case BuiltinType::Double: return Target->getDoubleFormat();
1271f4a2713aSLionel Sambuc case BuiltinType::LongDouble: return Target->getLongDoubleFormat();
1272f4a2713aSLionel Sambuc }
1273f4a2713aSLionel Sambuc }
1274f4a2713aSLionel Sambuc
getDeclAlign(const Decl * D,bool ForAlignof) const1275f4a2713aSLionel Sambuc CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const {
1276f4a2713aSLionel Sambuc unsigned Align = Target->getCharWidth();
1277f4a2713aSLionel Sambuc
1278f4a2713aSLionel Sambuc bool UseAlignAttrOnly = false;
1279f4a2713aSLionel Sambuc if (unsigned AlignFromAttr = D->getMaxAlignment()) {
1280f4a2713aSLionel Sambuc Align = AlignFromAttr;
1281f4a2713aSLionel Sambuc
1282f4a2713aSLionel Sambuc // __attribute__((aligned)) can increase or decrease alignment
1283f4a2713aSLionel Sambuc // *except* on a struct or struct member, where it only increases
1284f4a2713aSLionel Sambuc // alignment unless 'packed' is also specified.
1285f4a2713aSLionel Sambuc //
1286f4a2713aSLionel Sambuc // It is an error for alignas to decrease alignment, so we can
1287f4a2713aSLionel Sambuc // ignore that possibility; Sema should diagnose it.
1288f4a2713aSLionel Sambuc if (isa<FieldDecl>(D)) {
1289f4a2713aSLionel Sambuc UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
1290f4a2713aSLionel Sambuc cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
1291f4a2713aSLionel Sambuc } else {
1292f4a2713aSLionel Sambuc UseAlignAttrOnly = true;
1293f4a2713aSLionel Sambuc }
1294f4a2713aSLionel Sambuc }
1295f4a2713aSLionel Sambuc else if (isa<FieldDecl>(D))
1296f4a2713aSLionel Sambuc UseAlignAttrOnly =
1297f4a2713aSLionel Sambuc D->hasAttr<PackedAttr>() ||
1298f4a2713aSLionel Sambuc cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
1299f4a2713aSLionel Sambuc
1300f4a2713aSLionel Sambuc // If we're using the align attribute only, just ignore everything
1301f4a2713aSLionel Sambuc // else about the declaration and its type.
1302f4a2713aSLionel Sambuc if (UseAlignAttrOnly) {
1303f4a2713aSLionel Sambuc // do nothing
1304f4a2713aSLionel Sambuc
1305f4a2713aSLionel Sambuc } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
1306f4a2713aSLionel Sambuc QualType T = VD->getType();
1307f4a2713aSLionel Sambuc if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
1308f4a2713aSLionel Sambuc if (ForAlignof)
1309f4a2713aSLionel Sambuc T = RT->getPointeeType();
1310f4a2713aSLionel Sambuc else
1311f4a2713aSLionel Sambuc T = getPointerType(RT->getPointeeType());
1312f4a2713aSLionel Sambuc }
1313*0a6a1f1dSLionel Sambuc QualType BaseT = getBaseElementType(T);
1314*0a6a1f1dSLionel Sambuc if (!BaseT->isIncompleteType() && !T->isFunctionType()) {
1315f4a2713aSLionel Sambuc // Adjust alignments of declarations with array type by the
1316f4a2713aSLionel Sambuc // large-array alignment on the target.
1317f4a2713aSLionel Sambuc if (const ArrayType *arrayType = getAsArrayType(T)) {
1318f4a2713aSLionel Sambuc unsigned MinWidth = Target->getLargeArrayMinWidth();
1319f4a2713aSLionel Sambuc if (!ForAlignof && MinWidth) {
1320f4a2713aSLionel Sambuc if (isa<VariableArrayType>(arrayType))
1321f4a2713aSLionel Sambuc Align = std::max(Align, Target->getLargeArrayAlign());
1322f4a2713aSLionel Sambuc else if (isa<ConstantArrayType>(arrayType) &&
1323f4a2713aSLionel Sambuc MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
1324f4a2713aSLionel Sambuc Align = std::max(Align, Target->getLargeArrayAlign());
1325f4a2713aSLionel Sambuc }
1326f4a2713aSLionel Sambuc }
1327f4a2713aSLionel Sambuc Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
1328f4a2713aSLionel Sambuc if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1329f4a2713aSLionel Sambuc if (VD->hasGlobalStorage())
1330f4a2713aSLionel Sambuc Align = std::max(Align, getTargetInfo().getMinGlobalAlign());
1331f4a2713aSLionel Sambuc }
1332f4a2713aSLionel Sambuc }
1333f4a2713aSLionel Sambuc
1334f4a2713aSLionel Sambuc // Fields can be subject to extra alignment constraints, like if
1335f4a2713aSLionel Sambuc // the field is packed, the struct is packed, or the struct has a
1336f4a2713aSLionel Sambuc // a max-field-alignment constraint (#pragma pack). So calculate
1337f4a2713aSLionel Sambuc // the actual alignment of the field within the struct, and then
1338f4a2713aSLionel Sambuc // (as we're expected to) constrain that by the alignment of the type.
1339f4a2713aSLionel Sambuc if (const FieldDecl *Field = dyn_cast<FieldDecl>(VD)) {
1340f4a2713aSLionel Sambuc const RecordDecl *Parent = Field->getParent();
1341f4a2713aSLionel Sambuc // We can only produce a sensible answer if the record is valid.
1342f4a2713aSLionel Sambuc if (!Parent->isInvalidDecl()) {
1343f4a2713aSLionel Sambuc const ASTRecordLayout &Layout = getASTRecordLayout(Parent);
1344f4a2713aSLionel Sambuc
1345f4a2713aSLionel Sambuc // Start with the record's overall alignment.
1346f4a2713aSLionel Sambuc unsigned FieldAlign = toBits(Layout.getAlignment());
1347f4a2713aSLionel Sambuc
1348f4a2713aSLionel Sambuc // Use the GCD of that and the offset within the record.
1349f4a2713aSLionel Sambuc uint64_t Offset = Layout.getFieldOffset(Field->getFieldIndex());
1350f4a2713aSLionel Sambuc if (Offset > 0) {
1351f4a2713aSLionel Sambuc // Alignment is always a power of 2, so the GCD will be a power of 2,
1352f4a2713aSLionel Sambuc // which means we get to do this crazy thing instead of Euclid's.
1353f4a2713aSLionel Sambuc uint64_t LowBitOfOffset = Offset & (~Offset + 1);
1354f4a2713aSLionel Sambuc if (LowBitOfOffset < FieldAlign)
1355f4a2713aSLionel Sambuc FieldAlign = static_cast<unsigned>(LowBitOfOffset);
1356f4a2713aSLionel Sambuc }
1357f4a2713aSLionel Sambuc
1358f4a2713aSLionel Sambuc Align = std::min(Align, FieldAlign);
1359f4a2713aSLionel Sambuc }
1360f4a2713aSLionel Sambuc }
1361f4a2713aSLionel Sambuc }
1362f4a2713aSLionel Sambuc
1363f4a2713aSLionel Sambuc return toCharUnitsFromBits(Align);
1364f4a2713aSLionel Sambuc }
1365f4a2713aSLionel Sambuc
1366f4a2713aSLionel Sambuc // getTypeInfoDataSizeInChars - Return the size of a type, in
1367f4a2713aSLionel Sambuc // chars. If the type is a record, its data size is returned. This is
1368f4a2713aSLionel Sambuc // the size of the memcpy that's performed when assigning this type
1369f4a2713aSLionel Sambuc // using a trivial copy/move assignment operator.
1370f4a2713aSLionel Sambuc std::pair<CharUnits, CharUnits>
getTypeInfoDataSizeInChars(QualType T) const1371f4a2713aSLionel Sambuc ASTContext::getTypeInfoDataSizeInChars(QualType T) const {
1372f4a2713aSLionel Sambuc std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T);
1373f4a2713aSLionel Sambuc
1374f4a2713aSLionel Sambuc // In C++, objects can sometimes be allocated into the tail padding
1375f4a2713aSLionel Sambuc // of a base-class subobject. We decide whether that's possible
1376f4a2713aSLionel Sambuc // during class layout, so here we can just trust the layout results.
1377f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus) {
1378f4a2713aSLionel Sambuc if (const RecordType *RT = T->getAs<RecordType>()) {
1379f4a2713aSLionel Sambuc const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl());
1380f4a2713aSLionel Sambuc sizeAndAlign.first = layout.getDataSize();
1381f4a2713aSLionel Sambuc }
1382f4a2713aSLionel Sambuc }
1383f4a2713aSLionel Sambuc
1384f4a2713aSLionel Sambuc return sizeAndAlign;
1385f4a2713aSLionel Sambuc }
1386f4a2713aSLionel Sambuc
1387f4a2713aSLionel Sambuc /// getConstantArrayInfoInChars - Performing the computation in CharUnits
1388f4a2713aSLionel Sambuc /// instead of in bits prevents overflowing the uint64_t for some large arrays.
1389f4a2713aSLionel Sambuc std::pair<CharUnits, CharUnits>
getConstantArrayInfoInChars(const ASTContext & Context,const ConstantArrayType * CAT)1390f4a2713aSLionel Sambuc static getConstantArrayInfoInChars(const ASTContext &Context,
1391f4a2713aSLionel Sambuc const ConstantArrayType *CAT) {
1392f4a2713aSLionel Sambuc std::pair<CharUnits, CharUnits> EltInfo =
1393f4a2713aSLionel Sambuc Context.getTypeInfoInChars(CAT->getElementType());
1394f4a2713aSLionel Sambuc uint64_t Size = CAT->getSize().getZExtValue();
1395f4a2713aSLionel Sambuc assert((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <=
1396f4a2713aSLionel Sambuc (uint64_t)(-1)/Size) &&
1397f4a2713aSLionel Sambuc "Overflow in array type char size evaluation");
1398f4a2713aSLionel Sambuc uint64_t Width = EltInfo.first.getQuantity() * Size;
1399f4a2713aSLionel Sambuc unsigned Align = EltInfo.second.getQuantity();
1400f4a2713aSLionel Sambuc if (!Context.getTargetInfo().getCXXABI().isMicrosoft() ||
1401f4a2713aSLionel Sambuc Context.getTargetInfo().getPointerWidth(0) == 64)
1402f4a2713aSLionel Sambuc Width = llvm::RoundUpToAlignment(Width, Align);
1403f4a2713aSLionel Sambuc return std::make_pair(CharUnits::fromQuantity(Width),
1404f4a2713aSLionel Sambuc CharUnits::fromQuantity(Align));
1405f4a2713aSLionel Sambuc }
1406f4a2713aSLionel Sambuc
1407f4a2713aSLionel Sambuc std::pair<CharUnits, CharUnits>
getTypeInfoInChars(const Type * T) const1408f4a2713aSLionel Sambuc ASTContext::getTypeInfoInChars(const Type *T) const {
1409f4a2713aSLionel Sambuc if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T))
1410f4a2713aSLionel Sambuc return getConstantArrayInfoInChars(*this, CAT);
1411*0a6a1f1dSLionel Sambuc TypeInfo Info = getTypeInfo(T);
1412*0a6a1f1dSLionel Sambuc return std::make_pair(toCharUnitsFromBits(Info.Width),
1413*0a6a1f1dSLionel Sambuc toCharUnitsFromBits(Info.Align));
1414f4a2713aSLionel Sambuc }
1415f4a2713aSLionel Sambuc
1416f4a2713aSLionel Sambuc std::pair<CharUnits, CharUnits>
getTypeInfoInChars(QualType T) const1417f4a2713aSLionel Sambuc ASTContext::getTypeInfoInChars(QualType T) const {
1418f4a2713aSLionel Sambuc return getTypeInfoInChars(T.getTypePtr());
1419f4a2713aSLionel Sambuc }
1420f4a2713aSLionel Sambuc
isAlignmentRequired(const Type * T) const1421*0a6a1f1dSLionel Sambuc bool ASTContext::isAlignmentRequired(const Type *T) const {
1422*0a6a1f1dSLionel Sambuc return getTypeInfo(T).AlignIsRequired;
1423*0a6a1f1dSLionel Sambuc }
1424f4a2713aSLionel Sambuc
isAlignmentRequired(QualType T) const1425*0a6a1f1dSLionel Sambuc bool ASTContext::isAlignmentRequired(QualType T) const {
1426*0a6a1f1dSLionel Sambuc return isAlignmentRequired(T.getTypePtr());
1427*0a6a1f1dSLionel Sambuc }
1428*0a6a1f1dSLionel Sambuc
getTypeInfo(const Type * T) const1429*0a6a1f1dSLionel Sambuc TypeInfo ASTContext::getTypeInfo(const Type *T) const {
1430*0a6a1f1dSLionel Sambuc TypeInfoMap::iterator I = MemoizedTypeInfo.find(T);
1431*0a6a1f1dSLionel Sambuc if (I != MemoizedTypeInfo.end())
1432*0a6a1f1dSLionel Sambuc return I->second;
1433*0a6a1f1dSLionel Sambuc
1434*0a6a1f1dSLionel Sambuc // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup.
1435*0a6a1f1dSLionel Sambuc TypeInfo TI = getTypeInfoImpl(T);
1436*0a6a1f1dSLionel Sambuc MemoizedTypeInfo[T] = TI;
1437*0a6a1f1dSLionel Sambuc return TI;
1438f4a2713aSLionel Sambuc }
1439f4a2713aSLionel Sambuc
1440f4a2713aSLionel Sambuc /// getTypeInfoImpl - Return the size of the specified type, in bits. This
1441f4a2713aSLionel Sambuc /// method does not work on incomplete types.
1442f4a2713aSLionel Sambuc ///
1443f4a2713aSLionel Sambuc /// FIXME: Pointers into different addr spaces could have different sizes and
1444f4a2713aSLionel Sambuc /// alignment requirements: getPointerInfo should take an AddrSpace, this
1445f4a2713aSLionel Sambuc /// should take a QualType, &c.
getTypeInfoImpl(const Type * T) const1446*0a6a1f1dSLionel Sambuc TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
1447f4a2713aSLionel Sambuc uint64_t Width = 0;
1448f4a2713aSLionel Sambuc unsigned Align = 8;
1449*0a6a1f1dSLionel Sambuc bool AlignIsRequired = false;
1450f4a2713aSLionel Sambuc switch (T->getTypeClass()) {
1451f4a2713aSLionel Sambuc #define TYPE(Class, Base)
1452f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Class, Base)
1453f4a2713aSLionel Sambuc #define NON_CANONICAL_TYPE(Class, Base)
1454f4a2713aSLionel Sambuc #define DEPENDENT_TYPE(Class, Base) case Type::Class:
1455f4a2713aSLionel Sambuc #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \
1456f4a2713aSLionel Sambuc case Type::Class: \
1457f4a2713aSLionel Sambuc assert(!T->isDependentType() && "should not see dependent types here"); \
1458f4a2713aSLionel Sambuc return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr());
1459f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
1460f4a2713aSLionel Sambuc llvm_unreachable("Should not see dependent types");
1461f4a2713aSLionel Sambuc
1462f4a2713aSLionel Sambuc case Type::FunctionNoProto:
1463f4a2713aSLionel Sambuc case Type::FunctionProto:
1464f4a2713aSLionel Sambuc // GCC extension: alignof(function) = 32 bits
1465f4a2713aSLionel Sambuc Width = 0;
1466f4a2713aSLionel Sambuc Align = 32;
1467f4a2713aSLionel Sambuc break;
1468f4a2713aSLionel Sambuc
1469f4a2713aSLionel Sambuc case Type::IncompleteArray:
1470f4a2713aSLionel Sambuc case Type::VariableArray:
1471f4a2713aSLionel Sambuc Width = 0;
1472f4a2713aSLionel Sambuc Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
1473f4a2713aSLionel Sambuc break;
1474f4a2713aSLionel Sambuc
1475f4a2713aSLionel Sambuc case Type::ConstantArray: {
1476f4a2713aSLionel Sambuc const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
1477f4a2713aSLionel Sambuc
1478*0a6a1f1dSLionel Sambuc TypeInfo EltInfo = getTypeInfo(CAT->getElementType());
1479f4a2713aSLionel Sambuc uint64_t Size = CAT->getSize().getZExtValue();
1480*0a6a1f1dSLionel Sambuc assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) &&
1481f4a2713aSLionel Sambuc "Overflow in array type bit size evaluation");
1482*0a6a1f1dSLionel Sambuc Width = EltInfo.Width * Size;
1483*0a6a1f1dSLionel Sambuc Align = EltInfo.Align;
1484f4a2713aSLionel Sambuc if (!getTargetInfo().getCXXABI().isMicrosoft() ||
1485f4a2713aSLionel Sambuc getTargetInfo().getPointerWidth(0) == 64)
1486f4a2713aSLionel Sambuc Width = llvm::RoundUpToAlignment(Width, Align);
1487f4a2713aSLionel Sambuc break;
1488f4a2713aSLionel Sambuc }
1489f4a2713aSLionel Sambuc case Type::ExtVector:
1490f4a2713aSLionel Sambuc case Type::Vector: {
1491f4a2713aSLionel Sambuc const VectorType *VT = cast<VectorType>(T);
1492*0a6a1f1dSLionel Sambuc TypeInfo EltInfo = getTypeInfo(VT->getElementType());
1493*0a6a1f1dSLionel Sambuc Width = EltInfo.Width * VT->getNumElements();
1494f4a2713aSLionel Sambuc Align = Width;
1495f4a2713aSLionel Sambuc // If the alignment is not a power of 2, round up to the next power of 2.
1496f4a2713aSLionel Sambuc // This happens for non-power-of-2 length vectors.
1497f4a2713aSLionel Sambuc if (Align & (Align-1)) {
1498f4a2713aSLionel Sambuc Align = llvm::NextPowerOf2(Align);
1499f4a2713aSLionel Sambuc Width = llvm::RoundUpToAlignment(Width, Align);
1500f4a2713aSLionel Sambuc }
1501f4a2713aSLionel Sambuc // Adjust the alignment based on the target max.
1502f4a2713aSLionel Sambuc uint64_t TargetVectorAlign = Target->getMaxVectorAlign();
1503f4a2713aSLionel Sambuc if (TargetVectorAlign && TargetVectorAlign < Align)
1504f4a2713aSLionel Sambuc Align = TargetVectorAlign;
1505f4a2713aSLionel Sambuc break;
1506f4a2713aSLionel Sambuc }
1507f4a2713aSLionel Sambuc
1508f4a2713aSLionel Sambuc case Type::Builtin:
1509f4a2713aSLionel Sambuc switch (cast<BuiltinType>(T)->getKind()) {
1510f4a2713aSLionel Sambuc default: llvm_unreachable("Unknown builtin type!");
1511f4a2713aSLionel Sambuc case BuiltinType::Void:
1512f4a2713aSLionel Sambuc // GCC extension: alignof(void) = 8 bits.
1513f4a2713aSLionel Sambuc Width = 0;
1514f4a2713aSLionel Sambuc Align = 8;
1515f4a2713aSLionel Sambuc break;
1516f4a2713aSLionel Sambuc
1517f4a2713aSLionel Sambuc case BuiltinType::Bool:
1518f4a2713aSLionel Sambuc Width = Target->getBoolWidth();
1519f4a2713aSLionel Sambuc Align = Target->getBoolAlign();
1520f4a2713aSLionel Sambuc break;
1521f4a2713aSLionel Sambuc case BuiltinType::Char_S:
1522f4a2713aSLionel Sambuc case BuiltinType::Char_U:
1523f4a2713aSLionel Sambuc case BuiltinType::UChar:
1524f4a2713aSLionel Sambuc case BuiltinType::SChar:
1525f4a2713aSLionel Sambuc Width = Target->getCharWidth();
1526f4a2713aSLionel Sambuc Align = Target->getCharAlign();
1527f4a2713aSLionel Sambuc break;
1528f4a2713aSLionel Sambuc case BuiltinType::WChar_S:
1529f4a2713aSLionel Sambuc case BuiltinType::WChar_U:
1530f4a2713aSLionel Sambuc Width = Target->getWCharWidth();
1531f4a2713aSLionel Sambuc Align = Target->getWCharAlign();
1532f4a2713aSLionel Sambuc break;
1533f4a2713aSLionel Sambuc case BuiltinType::Char16:
1534f4a2713aSLionel Sambuc Width = Target->getChar16Width();
1535f4a2713aSLionel Sambuc Align = Target->getChar16Align();
1536f4a2713aSLionel Sambuc break;
1537f4a2713aSLionel Sambuc case BuiltinType::Char32:
1538f4a2713aSLionel Sambuc Width = Target->getChar32Width();
1539f4a2713aSLionel Sambuc Align = Target->getChar32Align();
1540f4a2713aSLionel Sambuc break;
1541f4a2713aSLionel Sambuc case BuiltinType::UShort:
1542f4a2713aSLionel Sambuc case BuiltinType::Short:
1543f4a2713aSLionel Sambuc Width = Target->getShortWidth();
1544f4a2713aSLionel Sambuc Align = Target->getShortAlign();
1545f4a2713aSLionel Sambuc break;
1546f4a2713aSLionel Sambuc case BuiltinType::UInt:
1547f4a2713aSLionel Sambuc case BuiltinType::Int:
1548f4a2713aSLionel Sambuc Width = Target->getIntWidth();
1549f4a2713aSLionel Sambuc Align = Target->getIntAlign();
1550f4a2713aSLionel Sambuc break;
1551f4a2713aSLionel Sambuc case BuiltinType::ULong:
1552f4a2713aSLionel Sambuc case BuiltinType::Long:
1553f4a2713aSLionel Sambuc Width = Target->getLongWidth();
1554f4a2713aSLionel Sambuc Align = Target->getLongAlign();
1555f4a2713aSLionel Sambuc break;
1556f4a2713aSLionel Sambuc case BuiltinType::ULongLong:
1557f4a2713aSLionel Sambuc case BuiltinType::LongLong:
1558f4a2713aSLionel Sambuc Width = Target->getLongLongWidth();
1559f4a2713aSLionel Sambuc Align = Target->getLongLongAlign();
1560f4a2713aSLionel Sambuc break;
1561f4a2713aSLionel Sambuc case BuiltinType::Int128:
1562f4a2713aSLionel Sambuc case BuiltinType::UInt128:
1563f4a2713aSLionel Sambuc Width = 128;
1564f4a2713aSLionel Sambuc Align = 128; // int128_t is 128-bit aligned on all targets.
1565f4a2713aSLionel Sambuc break;
1566f4a2713aSLionel Sambuc case BuiltinType::Half:
1567f4a2713aSLionel Sambuc Width = Target->getHalfWidth();
1568f4a2713aSLionel Sambuc Align = Target->getHalfAlign();
1569f4a2713aSLionel Sambuc break;
1570f4a2713aSLionel Sambuc case BuiltinType::Float:
1571f4a2713aSLionel Sambuc Width = Target->getFloatWidth();
1572f4a2713aSLionel Sambuc Align = Target->getFloatAlign();
1573f4a2713aSLionel Sambuc break;
1574f4a2713aSLionel Sambuc case BuiltinType::Double:
1575f4a2713aSLionel Sambuc Width = Target->getDoubleWidth();
1576f4a2713aSLionel Sambuc Align = Target->getDoubleAlign();
1577f4a2713aSLionel Sambuc break;
1578f4a2713aSLionel Sambuc case BuiltinType::LongDouble:
1579f4a2713aSLionel Sambuc Width = Target->getLongDoubleWidth();
1580f4a2713aSLionel Sambuc Align = Target->getLongDoubleAlign();
1581f4a2713aSLionel Sambuc break;
1582f4a2713aSLionel Sambuc case BuiltinType::NullPtr:
1583f4a2713aSLionel Sambuc Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
1584f4a2713aSLionel Sambuc Align = Target->getPointerAlign(0); // == sizeof(void*)
1585f4a2713aSLionel Sambuc break;
1586f4a2713aSLionel Sambuc case BuiltinType::ObjCId:
1587f4a2713aSLionel Sambuc case BuiltinType::ObjCClass:
1588f4a2713aSLionel Sambuc case BuiltinType::ObjCSel:
1589f4a2713aSLionel Sambuc Width = Target->getPointerWidth(0);
1590f4a2713aSLionel Sambuc Align = Target->getPointerAlign(0);
1591f4a2713aSLionel Sambuc break;
1592f4a2713aSLionel Sambuc case BuiltinType::OCLSampler:
1593f4a2713aSLionel Sambuc // Samplers are modeled as integers.
1594f4a2713aSLionel Sambuc Width = Target->getIntWidth();
1595f4a2713aSLionel Sambuc Align = Target->getIntAlign();
1596f4a2713aSLionel Sambuc break;
1597f4a2713aSLionel Sambuc case BuiltinType::OCLEvent:
1598f4a2713aSLionel Sambuc case BuiltinType::OCLImage1d:
1599f4a2713aSLionel Sambuc case BuiltinType::OCLImage1dArray:
1600f4a2713aSLionel Sambuc case BuiltinType::OCLImage1dBuffer:
1601f4a2713aSLionel Sambuc case BuiltinType::OCLImage2d:
1602f4a2713aSLionel Sambuc case BuiltinType::OCLImage2dArray:
1603f4a2713aSLionel Sambuc case BuiltinType::OCLImage3d:
1604f4a2713aSLionel Sambuc // Currently these types are pointers to opaque types.
1605f4a2713aSLionel Sambuc Width = Target->getPointerWidth(0);
1606f4a2713aSLionel Sambuc Align = Target->getPointerAlign(0);
1607f4a2713aSLionel Sambuc break;
1608f4a2713aSLionel Sambuc }
1609f4a2713aSLionel Sambuc break;
1610f4a2713aSLionel Sambuc case Type::ObjCObjectPointer:
1611f4a2713aSLionel Sambuc Width = Target->getPointerWidth(0);
1612f4a2713aSLionel Sambuc Align = Target->getPointerAlign(0);
1613f4a2713aSLionel Sambuc break;
1614f4a2713aSLionel Sambuc case Type::BlockPointer: {
1615f4a2713aSLionel Sambuc unsigned AS = getTargetAddressSpace(
1616f4a2713aSLionel Sambuc cast<BlockPointerType>(T)->getPointeeType());
1617f4a2713aSLionel Sambuc Width = Target->getPointerWidth(AS);
1618f4a2713aSLionel Sambuc Align = Target->getPointerAlign(AS);
1619f4a2713aSLionel Sambuc break;
1620f4a2713aSLionel Sambuc }
1621f4a2713aSLionel Sambuc case Type::LValueReference:
1622f4a2713aSLionel Sambuc case Type::RValueReference: {
1623f4a2713aSLionel Sambuc // alignof and sizeof should never enter this code path here, so we go
1624f4a2713aSLionel Sambuc // the pointer route.
1625f4a2713aSLionel Sambuc unsigned AS = getTargetAddressSpace(
1626f4a2713aSLionel Sambuc cast<ReferenceType>(T)->getPointeeType());
1627f4a2713aSLionel Sambuc Width = Target->getPointerWidth(AS);
1628f4a2713aSLionel Sambuc Align = Target->getPointerAlign(AS);
1629f4a2713aSLionel Sambuc break;
1630f4a2713aSLionel Sambuc }
1631f4a2713aSLionel Sambuc case Type::Pointer: {
1632f4a2713aSLionel Sambuc unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
1633f4a2713aSLionel Sambuc Width = Target->getPointerWidth(AS);
1634f4a2713aSLionel Sambuc Align = Target->getPointerAlign(AS);
1635f4a2713aSLionel Sambuc break;
1636f4a2713aSLionel Sambuc }
1637f4a2713aSLionel Sambuc case Type::MemberPointer: {
1638f4a2713aSLionel Sambuc const MemberPointerType *MPT = cast<MemberPointerType>(T);
1639*0a6a1f1dSLionel Sambuc std::tie(Width, Align) = ABI->getMemberPointerWidthAndAlign(MPT);
1640f4a2713aSLionel Sambuc break;
1641f4a2713aSLionel Sambuc }
1642f4a2713aSLionel Sambuc case Type::Complex: {
1643f4a2713aSLionel Sambuc // Complex types have the same alignment as their elements, but twice the
1644f4a2713aSLionel Sambuc // size.
1645*0a6a1f1dSLionel Sambuc TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType());
1646*0a6a1f1dSLionel Sambuc Width = EltInfo.Width * 2;
1647*0a6a1f1dSLionel Sambuc Align = EltInfo.Align;
1648f4a2713aSLionel Sambuc break;
1649f4a2713aSLionel Sambuc }
1650f4a2713aSLionel Sambuc case Type::ObjCObject:
1651f4a2713aSLionel Sambuc return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
1652*0a6a1f1dSLionel Sambuc case Type::Adjusted:
1653f4a2713aSLionel Sambuc case Type::Decayed:
1654*0a6a1f1dSLionel Sambuc return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr());
1655f4a2713aSLionel Sambuc case Type::ObjCInterface: {
1656f4a2713aSLionel Sambuc const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
1657f4a2713aSLionel Sambuc const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
1658f4a2713aSLionel Sambuc Width = toBits(Layout.getSize());
1659f4a2713aSLionel Sambuc Align = toBits(Layout.getAlignment());
1660f4a2713aSLionel Sambuc break;
1661f4a2713aSLionel Sambuc }
1662f4a2713aSLionel Sambuc case Type::Record:
1663f4a2713aSLionel Sambuc case Type::Enum: {
1664f4a2713aSLionel Sambuc const TagType *TT = cast<TagType>(T);
1665f4a2713aSLionel Sambuc
1666f4a2713aSLionel Sambuc if (TT->getDecl()->isInvalidDecl()) {
1667f4a2713aSLionel Sambuc Width = 8;
1668f4a2713aSLionel Sambuc Align = 8;
1669f4a2713aSLionel Sambuc break;
1670f4a2713aSLionel Sambuc }
1671f4a2713aSLionel Sambuc
1672f4a2713aSLionel Sambuc if (const EnumType *ET = dyn_cast<EnumType>(TT))
1673f4a2713aSLionel Sambuc return getTypeInfo(ET->getDecl()->getIntegerType());
1674f4a2713aSLionel Sambuc
1675f4a2713aSLionel Sambuc const RecordType *RT = cast<RecordType>(TT);
1676f4a2713aSLionel Sambuc const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
1677f4a2713aSLionel Sambuc Width = toBits(Layout.getSize());
1678f4a2713aSLionel Sambuc Align = toBits(Layout.getAlignment());
1679f4a2713aSLionel Sambuc break;
1680f4a2713aSLionel Sambuc }
1681f4a2713aSLionel Sambuc
1682f4a2713aSLionel Sambuc case Type::SubstTemplateTypeParm:
1683f4a2713aSLionel Sambuc return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
1684f4a2713aSLionel Sambuc getReplacementType().getTypePtr());
1685f4a2713aSLionel Sambuc
1686f4a2713aSLionel Sambuc case Type::Auto: {
1687f4a2713aSLionel Sambuc const AutoType *A = cast<AutoType>(T);
1688f4a2713aSLionel Sambuc assert(!A->getDeducedType().isNull() &&
1689f4a2713aSLionel Sambuc "cannot request the size of an undeduced or dependent auto type");
1690f4a2713aSLionel Sambuc return getTypeInfo(A->getDeducedType().getTypePtr());
1691f4a2713aSLionel Sambuc }
1692f4a2713aSLionel Sambuc
1693f4a2713aSLionel Sambuc case Type::Paren:
1694f4a2713aSLionel Sambuc return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
1695f4a2713aSLionel Sambuc
1696f4a2713aSLionel Sambuc case Type::Typedef: {
1697f4a2713aSLionel Sambuc const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
1698*0a6a1f1dSLionel Sambuc TypeInfo Info = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
1699f4a2713aSLionel Sambuc // If the typedef has an aligned attribute on it, it overrides any computed
1700f4a2713aSLionel Sambuc // alignment we have. This violates the GCC documentation (which says that
1701f4a2713aSLionel Sambuc // attribute(aligned) can only round up) but matches its implementation.
1702*0a6a1f1dSLionel Sambuc if (unsigned AttrAlign = Typedef->getMaxAlignment()) {
1703f4a2713aSLionel Sambuc Align = AttrAlign;
1704*0a6a1f1dSLionel Sambuc AlignIsRequired = true;
1705*0a6a1f1dSLionel Sambuc } else {
1706*0a6a1f1dSLionel Sambuc Align = Info.Align;
1707*0a6a1f1dSLionel Sambuc AlignIsRequired = Info.AlignIsRequired;
1708*0a6a1f1dSLionel Sambuc }
1709*0a6a1f1dSLionel Sambuc Width = Info.Width;
1710f4a2713aSLionel Sambuc break;
1711f4a2713aSLionel Sambuc }
1712f4a2713aSLionel Sambuc
1713f4a2713aSLionel Sambuc case Type::Elaborated:
1714f4a2713aSLionel Sambuc return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
1715f4a2713aSLionel Sambuc
1716f4a2713aSLionel Sambuc case Type::Attributed:
1717f4a2713aSLionel Sambuc return getTypeInfo(
1718f4a2713aSLionel Sambuc cast<AttributedType>(T)->getEquivalentType().getTypePtr());
1719f4a2713aSLionel Sambuc
1720f4a2713aSLionel Sambuc case Type::Atomic: {
1721f4a2713aSLionel Sambuc // Start with the base type information.
1722*0a6a1f1dSLionel Sambuc TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType());
1723*0a6a1f1dSLionel Sambuc Width = Info.Width;
1724*0a6a1f1dSLionel Sambuc Align = Info.Align;
1725f4a2713aSLionel Sambuc
1726f4a2713aSLionel Sambuc // If the size of the type doesn't exceed the platform's max
1727f4a2713aSLionel Sambuc // atomic promotion width, make the size and alignment more
1728f4a2713aSLionel Sambuc // favorable to atomic operations:
1729f4a2713aSLionel Sambuc if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth()) {
1730f4a2713aSLionel Sambuc // Round the size up to a power of 2.
1731f4a2713aSLionel Sambuc if (!llvm::isPowerOf2_64(Width))
1732f4a2713aSLionel Sambuc Width = llvm::NextPowerOf2(Width);
1733f4a2713aSLionel Sambuc
1734f4a2713aSLionel Sambuc // Set the alignment equal to the size.
1735f4a2713aSLionel Sambuc Align = static_cast<unsigned>(Width);
1736f4a2713aSLionel Sambuc }
1737f4a2713aSLionel Sambuc }
1738f4a2713aSLionel Sambuc
1739f4a2713aSLionel Sambuc }
1740f4a2713aSLionel Sambuc
1741f4a2713aSLionel Sambuc assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2");
1742*0a6a1f1dSLionel Sambuc return TypeInfo(Width, Align, AlignIsRequired);
1743f4a2713aSLionel Sambuc }
1744f4a2713aSLionel Sambuc
1745f4a2713aSLionel Sambuc /// toCharUnitsFromBits - Convert a size in bits to a size in characters.
toCharUnitsFromBits(int64_t BitSize) const1746f4a2713aSLionel Sambuc CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
1747f4a2713aSLionel Sambuc return CharUnits::fromQuantity(BitSize / getCharWidth());
1748f4a2713aSLionel Sambuc }
1749f4a2713aSLionel Sambuc
1750f4a2713aSLionel Sambuc /// toBits - Convert a size in characters to a size in characters.
toBits(CharUnits CharSize) const1751f4a2713aSLionel Sambuc int64_t ASTContext::toBits(CharUnits CharSize) const {
1752f4a2713aSLionel Sambuc return CharSize.getQuantity() * getCharWidth();
1753f4a2713aSLionel Sambuc }
1754f4a2713aSLionel Sambuc
1755f4a2713aSLionel Sambuc /// getTypeSizeInChars - Return the size of the specified type, in characters.
1756f4a2713aSLionel Sambuc /// This method does not work on incomplete types.
getTypeSizeInChars(QualType T) const1757f4a2713aSLionel Sambuc CharUnits ASTContext::getTypeSizeInChars(QualType T) const {
1758f4a2713aSLionel Sambuc return getTypeInfoInChars(T).first;
1759f4a2713aSLionel Sambuc }
getTypeSizeInChars(const Type * T) const1760f4a2713aSLionel Sambuc CharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
1761f4a2713aSLionel Sambuc return getTypeInfoInChars(T).first;
1762f4a2713aSLionel Sambuc }
1763f4a2713aSLionel Sambuc
1764f4a2713aSLionel Sambuc /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
1765f4a2713aSLionel Sambuc /// characters. This method does not work on incomplete types.
getTypeAlignInChars(QualType T) const1766f4a2713aSLionel Sambuc CharUnits ASTContext::getTypeAlignInChars(QualType T) const {
1767f4a2713aSLionel Sambuc return toCharUnitsFromBits(getTypeAlign(T));
1768f4a2713aSLionel Sambuc }
getTypeAlignInChars(const Type * T) const1769f4a2713aSLionel Sambuc CharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
1770f4a2713aSLionel Sambuc return toCharUnitsFromBits(getTypeAlign(T));
1771f4a2713aSLionel Sambuc }
1772f4a2713aSLionel Sambuc
1773f4a2713aSLionel Sambuc /// getPreferredTypeAlign - Return the "preferred" alignment of the specified
1774f4a2713aSLionel Sambuc /// type for the current target in bits. This can be different than the ABI
1775f4a2713aSLionel Sambuc /// alignment in cases where it is beneficial for performance to overalign
1776f4a2713aSLionel Sambuc /// a data type.
getPreferredTypeAlign(const Type * T) const1777f4a2713aSLionel Sambuc unsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
1778*0a6a1f1dSLionel Sambuc TypeInfo TI = getTypeInfo(T);
1779*0a6a1f1dSLionel Sambuc unsigned ABIAlign = TI.Align;
1780f4a2713aSLionel Sambuc
1781f4a2713aSLionel Sambuc if (Target->getTriple().getArch() == llvm::Triple::xcore)
1782f4a2713aSLionel Sambuc return ABIAlign; // Never overalign on XCore.
1783f4a2713aSLionel Sambuc
1784f4a2713aSLionel Sambuc // Double and long long should be naturally aligned if possible.
1785*0a6a1f1dSLionel Sambuc T = T->getBaseElementTypeUnsafe();
1786f4a2713aSLionel Sambuc if (const ComplexType *CT = T->getAs<ComplexType>())
1787f4a2713aSLionel Sambuc T = CT->getElementType().getTypePtr();
1788f4a2713aSLionel Sambuc if (T->isSpecificBuiltinType(BuiltinType::Double) ||
1789f4a2713aSLionel Sambuc T->isSpecificBuiltinType(BuiltinType::LongLong) ||
1790f4a2713aSLionel Sambuc T->isSpecificBuiltinType(BuiltinType::ULongLong))
1791*0a6a1f1dSLionel Sambuc // Don't increase the alignment if an alignment attribute was specified on a
1792*0a6a1f1dSLionel Sambuc // typedef declaration.
1793*0a6a1f1dSLionel Sambuc if (!TI.AlignIsRequired)
1794f4a2713aSLionel Sambuc return std::max(ABIAlign, (unsigned)getTypeSize(T));
1795f4a2713aSLionel Sambuc
1796f4a2713aSLionel Sambuc return ABIAlign;
1797f4a2713aSLionel Sambuc }
1798f4a2713aSLionel Sambuc
1799f4a2713aSLionel Sambuc /// getAlignOfGlobalVar - Return the alignment in bits that should be given
1800f4a2713aSLionel Sambuc /// to a global variable of the specified type.
getAlignOfGlobalVar(QualType T) const1801f4a2713aSLionel Sambuc unsigned ASTContext::getAlignOfGlobalVar(QualType T) const {
1802f4a2713aSLionel Sambuc return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign());
1803f4a2713aSLionel Sambuc }
1804f4a2713aSLionel Sambuc
1805f4a2713aSLionel Sambuc /// getAlignOfGlobalVarInChars - Return the alignment in characters that
1806f4a2713aSLionel Sambuc /// should be given to a global variable of the specified type.
getAlignOfGlobalVarInChars(QualType T) const1807f4a2713aSLionel Sambuc CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const {
1808f4a2713aSLionel Sambuc return toCharUnitsFromBits(getAlignOfGlobalVar(T));
1809f4a2713aSLionel Sambuc }
1810f4a2713aSLionel Sambuc
1811f4a2713aSLionel Sambuc /// DeepCollectObjCIvars -
1812f4a2713aSLionel Sambuc /// This routine first collects all declared, but not synthesized, ivars in
1813f4a2713aSLionel Sambuc /// super class and then collects all ivars, including those synthesized for
1814f4a2713aSLionel Sambuc /// current class. This routine is used for implementation of current class
1815f4a2713aSLionel Sambuc /// when all ivars, declared and synthesized are known.
1816f4a2713aSLionel Sambuc ///
DeepCollectObjCIvars(const ObjCInterfaceDecl * OI,bool leafClass,SmallVectorImpl<const ObjCIvarDecl * > & Ivars) const1817f4a2713aSLionel Sambuc void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
1818f4a2713aSLionel Sambuc bool leafClass,
1819f4a2713aSLionel Sambuc SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const {
1820f4a2713aSLionel Sambuc if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
1821f4a2713aSLionel Sambuc DeepCollectObjCIvars(SuperClass, false, Ivars);
1822f4a2713aSLionel Sambuc if (!leafClass) {
1823*0a6a1f1dSLionel Sambuc for (const auto *I : OI->ivars())
1824*0a6a1f1dSLionel Sambuc Ivars.push_back(I);
1825f4a2713aSLionel Sambuc } else {
1826f4a2713aSLionel Sambuc ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1827f4a2713aSLionel Sambuc for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1828f4a2713aSLionel Sambuc Iv= Iv->getNextIvar())
1829f4a2713aSLionel Sambuc Ivars.push_back(Iv);
1830f4a2713aSLionel Sambuc }
1831f4a2713aSLionel Sambuc }
1832f4a2713aSLionel Sambuc
1833f4a2713aSLionel Sambuc /// CollectInheritedProtocols - Collect all protocols in current class and
1834f4a2713aSLionel Sambuc /// those inherited by it.
CollectInheritedProtocols(const Decl * CDecl,llvm::SmallPtrSet<ObjCProtocolDecl *,8> & Protocols)1835f4a2713aSLionel Sambuc void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
1836f4a2713aSLionel Sambuc llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
1837f4a2713aSLionel Sambuc if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1838f4a2713aSLionel Sambuc // We can use protocol_iterator here instead of
1839f4a2713aSLionel Sambuc // all_referenced_protocol_iterator since we are walking all categories.
1840*0a6a1f1dSLionel Sambuc for (auto *Proto : OI->all_referenced_protocols()) {
1841f4a2713aSLionel Sambuc Protocols.insert(Proto->getCanonicalDecl());
1842*0a6a1f1dSLionel Sambuc for (auto *P : Proto->protocols()) {
1843*0a6a1f1dSLionel Sambuc Protocols.insert(P->getCanonicalDecl());
1844*0a6a1f1dSLionel Sambuc CollectInheritedProtocols(P, Protocols);
1845f4a2713aSLionel Sambuc }
1846f4a2713aSLionel Sambuc }
1847f4a2713aSLionel Sambuc
1848f4a2713aSLionel Sambuc // Categories of this Interface.
1849*0a6a1f1dSLionel Sambuc for (const auto *Cat : OI->visible_categories())
1850*0a6a1f1dSLionel Sambuc CollectInheritedProtocols(Cat, Protocols);
1851f4a2713aSLionel Sambuc
1852f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1853f4a2713aSLionel Sambuc while (SD) {
1854f4a2713aSLionel Sambuc CollectInheritedProtocols(SD, Protocols);
1855f4a2713aSLionel Sambuc SD = SD->getSuperClass();
1856f4a2713aSLionel Sambuc }
1857f4a2713aSLionel Sambuc } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1858*0a6a1f1dSLionel Sambuc for (auto *Proto : OC->protocols()) {
1859f4a2713aSLionel Sambuc Protocols.insert(Proto->getCanonicalDecl());
1860*0a6a1f1dSLionel Sambuc for (const auto *P : Proto->protocols())
1861*0a6a1f1dSLionel Sambuc CollectInheritedProtocols(P, Protocols);
1862f4a2713aSLionel Sambuc }
1863f4a2713aSLionel Sambuc } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1864*0a6a1f1dSLionel Sambuc for (auto *Proto : OP->protocols()) {
1865f4a2713aSLionel Sambuc Protocols.insert(Proto->getCanonicalDecl());
1866*0a6a1f1dSLionel Sambuc for (const auto *P : Proto->protocols())
1867*0a6a1f1dSLionel Sambuc CollectInheritedProtocols(P, Protocols);
1868f4a2713aSLionel Sambuc }
1869f4a2713aSLionel Sambuc }
1870f4a2713aSLionel Sambuc }
1871f4a2713aSLionel Sambuc
CountNonClassIvars(const ObjCInterfaceDecl * OI) const1872f4a2713aSLionel Sambuc unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
1873f4a2713aSLionel Sambuc unsigned count = 0;
1874f4a2713aSLionel Sambuc // Count ivars declared in class extension.
1875*0a6a1f1dSLionel Sambuc for (const auto *Ext : OI->known_extensions())
1876f4a2713aSLionel Sambuc count += Ext->ivar_size();
1877f4a2713aSLionel Sambuc
1878f4a2713aSLionel Sambuc // Count ivar defined in this class's implementation. This
1879f4a2713aSLionel Sambuc // includes synthesized ivars.
1880f4a2713aSLionel Sambuc if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
1881f4a2713aSLionel Sambuc count += ImplDecl->ivar_size();
1882f4a2713aSLionel Sambuc
1883f4a2713aSLionel Sambuc return count;
1884f4a2713aSLionel Sambuc }
1885f4a2713aSLionel Sambuc
isSentinelNullExpr(const Expr * E)1886f4a2713aSLionel Sambuc bool ASTContext::isSentinelNullExpr(const Expr *E) {
1887f4a2713aSLionel Sambuc if (!E)
1888f4a2713aSLionel Sambuc return false;
1889f4a2713aSLionel Sambuc
1890f4a2713aSLionel Sambuc // nullptr_t is always treated as null.
1891f4a2713aSLionel Sambuc if (E->getType()->isNullPtrType()) return true;
1892f4a2713aSLionel Sambuc
1893f4a2713aSLionel Sambuc if (E->getType()->isAnyPointerType() &&
1894f4a2713aSLionel Sambuc E->IgnoreParenCasts()->isNullPointerConstant(*this,
1895f4a2713aSLionel Sambuc Expr::NPC_ValueDependentIsNull))
1896f4a2713aSLionel Sambuc return true;
1897f4a2713aSLionel Sambuc
1898f4a2713aSLionel Sambuc // Unfortunately, __null has type 'int'.
1899f4a2713aSLionel Sambuc if (isa<GNUNullExpr>(E)) return true;
1900f4a2713aSLionel Sambuc
1901f4a2713aSLionel Sambuc return false;
1902f4a2713aSLionel Sambuc }
1903f4a2713aSLionel Sambuc
1904f4a2713aSLionel Sambuc /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
getObjCImplementation(ObjCInterfaceDecl * D)1905f4a2713aSLionel Sambuc ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
1906f4a2713aSLionel Sambuc llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1907f4a2713aSLionel Sambuc I = ObjCImpls.find(D);
1908f4a2713aSLionel Sambuc if (I != ObjCImpls.end())
1909f4a2713aSLionel Sambuc return cast<ObjCImplementationDecl>(I->second);
1910*0a6a1f1dSLionel Sambuc return nullptr;
1911f4a2713aSLionel Sambuc }
1912f4a2713aSLionel Sambuc /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
getObjCImplementation(ObjCCategoryDecl * D)1913f4a2713aSLionel Sambuc ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
1914f4a2713aSLionel Sambuc llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1915f4a2713aSLionel Sambuc I = ObjCImpls.find(D);
1916f4a2713aSLionel Sambuc if (I != ObjCImpls.end())
1917f4a2713aSLionel Sambuc return cast<ObjCCategoryImplDecl>(I->second);
1918*0a6a1f1dSLionel Sambuc return nullptr;
1919f4a2713aSLionel Sambuc }
1920f4a2713aSLionel Sambuc
1921f4a2713aSLionel Sambuc /// \brief Set the implementation of ObjCInterfaceDecl.
setObjCImplementation(ObjCInterfaceDecl * IFaceD,ObjCImplementationDecl * ImplD)1922f4a2713aSLionel Sambuc void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1923f4a2713aSLionel Sambuc ObjCImplementationDecl *ImplD) {
1924f4a2713aSLionel Sambuc assert(IFaceD && ImplD && "Passed null params");
1925f4a2713aSLionel Sambuc ObjCImpls[IFaceD] = ImplD;
1926f4a2713aSLionel Sambuc }
1927f4a2713aSLionel Sambuc /// \brief Set the implementation of ObjCCategoryDecl.
setObjCImplementation(ObjCCategoryDecl * CatD,ObjCCategoryImplDecl * ImplD)1928f4a2713aSLionel Sambuc void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1929f4a2713aSLionel Sambuc ObjCCategoryImplDecl *ImplD) {
1930f4a2713aSLionel Sambuc assert(CatD && ImplD && "Passed null params");
1931f4a2713aSLionel Sambuc ObjCImpls[CatD] = ImplD;
1932f4a2713aSLionel Sambuc }
1933f4a2713aSLionel Sambuc
getObjContainingInterface(const NamedDecl * ND) const1934f4a2713aSLionel Sambuc const ObjCInterfaceDecl *ASTContext::getObjContainingInterface(
1935f4a2713aSLionel Sambuc const NamedDecl *ND) const {
1936f4a2713aSLionel Sambuc if (const ObjCInterfaceDecl *ID =
1937f4a2713aSLionel Sambuc dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext()))
1938f4a2713aSLionel Sambuc return ID;
1939f4a2713aSLionel Sambuc if (const ObjCCategoryDecl *CD =
1940f4a2713aSLionel Sambuc dyn_cast<ObjCCategoryDecl>(ND->getDeclContext()))
1941f4a2713aSLionel Sambuc return CD->getClassInterface();
1942f4a2713aSLionel Sambuc if (const ObjCImplDecl *IMD =
1943f4a2713aSLionel Sambuc dyn_cast<ObjCImplDecl>(ND->getDeclContext()))
1944f4a2713aSLionel Sambuc return IMD->getClassInterface();
1945f4a2713aSLionel Sambuc
1946*0a6a1f1dSLionel Sambuc return nullptr;
1947f4a2713aSLionel Sambuc }
1948f4a2713aSLionel Sambuc
1949f4a2713aSLionel Sambuc /// \brief Get the copy initialization expression of VarDecl,or NULL if
1950f4a2713aSLionel Sambuc /// none exists.
getBlockVarCopyInits(const VarDecl * VD)1951f4a2713aSLionel Sambuc Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
1952f4a2713aSLionel Sambuc assert(VD && "Passed null params");
1953f4a2713aSLionel Sambuc assert(VD->hasAttr<BlocksAttr>() &&
1954f4a2713aSLionel Sambuc "getBlockVarCopyInits - not __block var");
1955f4a2713aSLionel Sambuc llvm::DenseMap<const VarDecl*, Expr*>::iterator
1956f4a2713aSLionel Sambuc I = BlockVarCopyInits.find(VD);
1957*0a6a1f1dSLionel Sambuc return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : nullptr;
1958f4a2713aSLionel Sambuc }
1959f4a2713aSLionel Sambuc
1960f4a2713aSLionel Sambuc /// \brief Set the copy inialization expression of a block var decl.
setBlockVarCopyInits(VarDecl * VD,Expr * Init)1961f4a2713aSLionel Sambuc void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
1962f4a2713aSLionel Sambuc assert(VD && Init && "Passed null params");
1963f4a2713aSLionel Sambuc assert(VD->hasAttr<BlocksAttr>() &&
1964f4a2713aSLionel Sambuc "setBlockVarCopyInits - not __block var");
1965f4a2713aSLionel Sambuc BlockVarCopyInits[VD] = Init;
1966f4a2713aSLionel Sambuc }
1967f4a2713aSLionel Sambuc
CreateTypeSourceInfo(QualType T,unsigned DataSize) const1968f4a2713aSLionel Sambuc TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
1969f4a2713aSLionel Sambuc unsigned DataSize) const {
1970f4a2713aSLionel Sambuc if (!DataSize)
1971f4a2713aSLionel Sambuc DataSize = TypeLoc::getFullDataSizeForType(T);
1972f4a2713aSLionel Sambuc else
1973f4a2713aSLionel Sambuc assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
1974f4a2713aSLionel Sambuc "incorrect data size provided to CreateTypeSourceInfo!");
1975f4a2713aSLionel Sambuc
1976f4a2713aSLionel Sambuc TypeSourceInfo *TInfo =
1977f4a2713aSLionel Sambuc (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1978f4a2713aSLionel Sambuc new (TInfo) TypeSourceInfo(T);
1979f4a2713aSLionel Sambuc return TInfo;
1980f4a2713aSLionel Sambuc }
1981f4a2713aSLionel Sambuc
getTrivialTypeSourceInfo(QualType T,SourceLocation L) const1982f4a2713aSLionel Sambuc TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
1983f4a2713aSLionel Sambuc SourceLocation L) const {
1984f4a2713aSLionel Sambuc TypeSourceInfo *DI = CreateTypeSourceInfo(T);
1985f4a2713aSLionel Sambuc DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
1986f4a2713aSLionel Sambuc return DI;
1987f4a2713aSLionel Sambuc }
1988f4a2713aSLionel Sambuc
1989f4a2713aSLionel Sambuc const ASTRecordLayout &
getASTObjCInterfaceLayout(const ObjCInterfaceDecl * D) const1990f4a2713aSLionel Sambuc ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
1991*0a6a1f1dSLionel Sambuc return getObjCLayout(D, nullptr);
1992f4a2713aSLionel Sambuc }
1993f4a2713aSLionel Sambuc
1994f4a2713aSLionel Sambuc const ASTRecordLayout &
getASTObjCImplementationLayout(const ObjCImplementationDecl * D) const1995f4a2713aSLionel Sambuc ASTContext::getASTObjCImplementationLayout(
1996f4a2713aSLionel Sambuc const ObjCImplementationDecl *D) const {
1997f4a2713aSLionel Sambuc return getObjCLayout(D->getClassInterface(), D);
1998f4a2713aSLionel Sambuc }
1999f4a2713aSLionel Sambuc
2000f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2001f4a2713aSLionel Sambuc // Type creation/memoization methods
2002f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2003f4a2713aSLionel Sambuc
2004f4a2713aSLionel Sambuc QualType
getExtQualType(const Type * baseType,Qualifiers quals) const2005f4a2713aSLionel Sambuc ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
2006f4a2713aSLionel Sambuc unsigned fastQuals = quals.getFastQualifiers();
2007f4a2713aSLionel Sambuc quals.removeFastQualifiers();
2008f4a2713aSLionel Sambuc
2009f4a2713aSLionel Sambuc // Check if we've already instantiated this type.
2010f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2011f4a2713aSLionel Sambuc ExtQuals::Profile(ID, baseType, quals);
2012*0a6a1f1dSLionel Sambuc void *insertPos = nullptr;
2013f4a2713aSLionel Sambuc if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
2014f4a2713aSLionel Sambuc assert(eq->getQualifiers() == quals);
2015f4a2713aSLionel Sambuc return QualType(eq, fastQuals);
2016f4a2713aSLionel Sambuc }
2017f4a2713aSLionel Sambuc
2018f4a2713aSLionel Sambuc // If the base type is not canonical, make the appropriate canonical type.
2019f4a2713aSLionel Sambuc QualType canon;
2020f4a2713aSLionel Sambuc if (!baseType->isCanonicalUnqualified()) {
2021f4a2713aSLionel Sambuc SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
2022f4a2713aSLionel Sambuc canonSplit.Quals.addConsistentQualifiers(quals);
2023f4a2713aSLionel Sambuc canon = getExtQualType(canonSplit.Ty, canonSplit.Quals);
2024f4a2713aSLionel Sambuc
2025f4a2713aSLionel Sambuc // Re-find the insert position.
2026f4a2713aSLionel Sambuc (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
2027f4a2713aSLionel Sambuc }
2028f4a2713aSLionel Sambuc
2029f4a2713aSLionel Sambuc ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
2030f4a2713aSLionel Sambuc ExtQualNodes.InsertNode(eq, insertPos);
2031f4a2713aSLionel Sambuc return QualType(eq, fastQuals);
2032f4a2713aSLionel Sambuc }
2033f4a2713aSLionel Sambuc
2034f4a2713aSLionel Sambuc QualType
getAddrSpaceQualType(QualType T,unsigned AddressSpace) const2035f4a2713aSLionel Sambuc ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
2036f4a2713aSLionel Sambuc QualType CanT = getCanonicalType(T);
2037f4a2713aSLionel Sambuc if (CanT.getAddressSpace() == AddressSpace)
2038f4a2713aSLionel Sambuc return T;
2039f4a2713aSLionel Sambuc
2040f4a2713aSLionel Sambuc // If we are composing extended qualifiers together, merge together
2041f4a2713aSLionel Sambuc // into one ExtQuals node.
2042f4a2713aSLionel Sambuc QualifierCollector Quals;
2043f4a2713aSLionel Sambuc const Type *TypeNode = Quals.strip(T);
2044f4a2713aSLionel Sambuc
2045f4a2713aSLionel Sambuc // If this type already has an address space specified, it cannot get
2046f4a2713aSLionel Sambuc // another one.
2047f4a2713aSLionel Sambuc assert(!Quals.hasAddressSpace() &&
2048f4a2713aSLionel Sambuc "Type cannot be in multiple addr spaces!");
2049f4a2713aSLionel Sambuc Quals.addAddressSpace(AddressSpace);
2050f4a2713aSLionel Sambuc
2051f4a2713aSLionel Sambuc return getExtQualType(TypeNode, Quals);
2052f4a2713aSLionel Sambuc }
2053f4a2713aSLionel Sambuc
getObjCGCQualType(QualType T,Qualifiers::GC GCAttr) const2054f4a2713aSLionel Sambuc QualType ASTContext::getObjCGCQualType(QualType T,
2055f4a2713aSLionel Sambuc Qualifiers::GC GCAttr) const {
2056f4a2713aSLionel Sambuc QualType CanT = getCanonicalType(T);
2057f4a2713aSLionel Sambuc if (CanT.getObjCGCAttr() == GCAttr)
2058f4a2713aSLionel Sambuc return T;
2059f4a2713aSLionel Sambuc
2060f4a2713aSLionel Sambuc if (const PointerType *ptr = T->getAs<PointerType>()) {
2061f4a2713aSLionel Sambuc QualType Pointee = ptr->getPointeeType();
2062f4a2713aSLionel Sambuc if (Pointee->isAnyPointerType()) {
2063f4a2713aSLionel Sambuc QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
2064f4a2713aSLionel Sambuc return getPointerType(ResultType);
2065f4a2713aSLionel Sambuc }
2066f4a2713aSLionel Sambuc }
2067f4a2713aSLionel Sambuc
2068f4a2713aSLionel Sambuc // If we are composing extended qualifiers together, merge together
2069f4a2713aSLionel Sambuc // into one ExtQuals node.
2070f4a2713aSLionel Sambuc QualifierCollector Quals;
2071f4a2713aSLionel Sambuc const Type *TypeNode = Quals.strip(T);
2072f4a2713aSLionel Sambuc
2073f4a2713aSLionel Sambuc // If this type already has an ObjCGC specified, it cannot get
2074f4a2713aSLionel Sambuc // another one.
2075f4a2713aSLionel Sambuc assert(!Quals.hasObjCGCAttr() &&
2076f4a2713aSLionel Sambuc "Type cannot have multiple ObjCGCs!");
2077f4a2713aSLionel Sambuc Quals.addObjCGCAttr(GCAttr);
2078f4a2713aSLionel Sambuc
2079f4a2713aSLionel Sambuc return getExtQualType(TypeNode, Quals);
2080f4a2713aSLionel Sambuc }
2081f4a2713aSLionel Sambuc
adjustFunctionType(const FunctionType * T,FunctionType::ExtInfo Info)2082f4a2713aSLionel Sambuc const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
2083f4a2713aSLionel Sambuc FunctionType::ExtInfo Info) {
2084f4a2713aSLionel Sambuc if (T->getExtInfo() == Info)
2085f4a2713aSLionel Sambuc return T;
2086f4a2713aSLionel Sambuc
2087f4a2713aSLionel Sambuc QualType Result;
2088f4a2713aSLionel Sambuc if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
2089*0a6a1f1dSLionel Sambuc Result = getFunctionNoProtoType(FNPT->getReturnType(), Info);
2090f4a2713aSLionel Sambuc } else {
2091f4a2713aSLionel Sambuc const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
2092f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
2093f4a2713aSLionel Sambuc EPI.ExtInfo = Info;
2094*0a6a1f1dSLionel Sambuc Result = getFunctionType(FPT->getReturnType(), FPT->getParamTypes(), EPI);
2095f4a2713aSLionel Sambuc }
2096f4a2713aSLionel Sambuc
2097f4a2713aSLionel Sambuc return cast<FunctionType>(Result.getTypePtr());
2098f4a2713aSLionel Sambuc }
2099f4a2713aSLionel Sambuc
adjustDeducedFunctionResultType(FunctionDecl * FD,QualType ResultType)2100f4a2713aSLionel Sambuc void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD,
2101f4a2713aSLionel Sambuc QualType ResultType) {
2102f4a2713aSLionel Sambuc FD = FD->getMostRecentDecl();
2103f4a2713aSLionel Sambuc while (true) {
2104f4a2713aSLionel Sambuc const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>();
2105f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
2106*0a6a1f1dSLionel Sambuc FD->setType(getFunctionType(ResultType, FPT->getParamTypes(), EPI));
2107f4a2713aSLionel Sambuc if (FunctionDecl *Next = FD->getPreviousDecl())
2108f4a2713aSLionel Sambuc FD = Next;
2109f4a2713aSLionel Sambuc else
2110f4a2713aSLionel Sambuc break;
2111f4a2713aSLionel Sambuc }
2112f4a2713aSLionel Sambuc if (ASTMutationListener *L = getASTMutationListener())
2113f4a2713aSLionel Sambuc L->DeducedReturnType(FD, ResultType);
2114f4a2713aSLionel Sambuc }
2115f4a2713aSLionel Sambuc
2116*0a6a1f1dSLionel Sambuc /// Get a function type and produce the equivalent function type with the
2117*0a6a1f1dSLionel Sambuc /// specified exception specification. Type sugar that can be present on a
2118*0a6a1f1dSLionel Sambuc /// declaration of a function with an exception specification is permitted
2119*0a6a1f1dSLionel Sambuc /// and preserved. Other type sugar (for instance, typedefs) is not.
getFunctionTypeWithExceptionSpec(ASTContext & Context,QualType Orig,const FunctionProtoType::ExceptionSpecInfo & ESI)2120*0a6a1f1dSLionel Sambuc static QualType getFunctionTypeWithExceptionSpec(
2121*0a6a1f1dSLionel Sambuc ASTContext &Context, QualType Orig,
2122*0a6a1f1dSLionel Sambuc const FunctionProtoType::ExceptionSpecInfo &ESI) {
2123*0a6a1f1dSLionel Sambuc // Might have some parens.
2124*0a6a1f1dSLionel Sambuc if (auto *PT = dyn_cast<ParenType>(Orig))
2125*0a6a1f1dSLionel Sambuc return Context.getParenType(
2126*0a6a1f1dSLionel Sambuc getFunctionTypeWithExceptionSpec(Context, PT->getInnerType(), ESI));
2127*0a6a1f1dSLionel Sambuc
2128*0a6a1f1dSLionel Sambuc // Might have a calling-convention attribute.
2129*0a6a1f1dSLionel Sambuc if (auto *AT = dyn_cast<AttributedType>(Orig))
2130*0a6a1f1dSLionel Sambuc return Context.getAttributedType(
2131*0a6a1f1dSLionel Sambuc AT->getAttrKind(),
2132*0a6a1f1dSLionel Sambuc getFunctionTypeWithExceptionSpec(Context, AT->getModifiedType(), ESI),
2133*0a6a1f1dSLionel Sambuc getFunctionTypeWithExceptionSpec(Context, AT->getEquivalentType(),
2134*0a6a1f1dSLionel Sambuc ESI));
2135*0a6a1f1dSLionel Sambuc
2136*0a6a1f1dSLionel Sambuc // Anything else must be a function type. Rebuild it with the new exception
2137*0a6a1f1dSLionel Sambuc // specification.
2138*0a6a1f1dSLionel Sambuc const FunctionProtoType *Proto = cast<FunctionProtoType>(Orig);
2139*0a6a1f1dSLionel Sambuc return Context.getFunctionType(
2140*0a6a1f1dSLionel Sambuc Proto->getReturnType(), Proto->getParamTypes(),
2141*0a6a1f1dSLionel Sambuc Proto->getExtProtoInfo().withExceptionSpec(ESI));
2142*0a6a1f1dSLionel Sambuc }
2143*0a6a1f1dSLionel Sambuc
adjustExceptionSpec(FunctionDecl * FD,const FunctionProtoType::ExceptionSpecInfo & ESI,bool AsWritten)2144*0a6a1f1dSLionel Sambuc void ASTContext::adjustExceptionSpec(
2145*0a6a1f1dSLionel Sambuc FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI,
2146*0a6a1f1dSLionel Sambuc bool AsWritten) {
2147*0a6a1f1dSLionel Sambuc // Update the type.
2148*0a6a1f1dSLionel Sambuc QualType Updated =
2149*0a6a1f1dSLionel Sambuc getFunctionTypeWithExceptionSpec(*this, FD->getType(), ESI);
2150*0a6a1f1dSLionel Sambuc FD->setType(Updated);
2151*0a6a1f1dSLionel Sambuc
2152*0a6a1f1dSLionel Sambuc if (!AsWritten)
2153*0a6a1f1dSLionel Sambuc return;
2154*0a6a1f1dSLionel Sambuc
2155*0a6a1f1dSLionel Sambuc // Update the type in the type source information too.
2156*0a6a1f1dSLionel Sambuc if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) {
2157*0a6a1f1dSLionel Sambuc // If the type and the type-as-written differ, we may need to update
2158*0a6a1f1dSLionel Sambuc // the type-as-written too.
2159*0a6a1f1dSLionel Sambuc if (TSInfo->getType() != FD->getType())
2160*0a6a1f1dSLionel Sambuc Updated = getFunctionTypeWithExceptionSpec(*this, TSInfo->getType(), ESI);
2161*0a6a1f1dSLionel Sambuc
2162*0a6a1f1dSLionel Sambuc // FIXME: When we get proper type location information for exceptions,
2163*0a6a1f1dSLionel Sambuc // we'll also have to rebuild the TypeSourceInfo. For now, we just patch
2164*0a6a1f1dSLionel Sambuc // up the TypeSourceInfo;
2165*0a6a1f1dSLionel Sambuc assert(TypeLoc::getFullDataSizeForType(Updated) ==
2166*0a6a1f1dSLionel Sambuc TypeLoc::getFullDataSizeForType(TSInfo->getType()) &&
2167*0a6a1f1dSLionel Sambuc "TypeLoc size mismatch from updating exception specification");
2168*0a6a1f1dSLionel Sambuc TSInfo->overrideType(Updated);
2169*0a6a1f1dSLionel Sambuc }
2170*0a6a1f1dSLionel Sambuc }
2171*0a6a1f1dSLionel Sambuc
2172f4a2713aSLionel Sambuc /// getComplexType - Return the uniqued reference to the type for a complex
2173f4a2713aSLionel Sambuc /// number with the specified element type.
getComplexType(QualType T) const2174f4a2713aSLionel Sambuc QualType ASTContext::getComplexType(QualType T) const {
2175f4a2713aSLionel Sambuc // Unique pointers, to guarantee there is only one pointer of a particular
2176f4a2713aSLionel Sambuc // structure.
2177f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2178f4a2713aSLionel Sambuc ComplexType::Profile(ID, T);
2179f4a2713aSLionel Sambuc
2180*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2181f4a2713aSLionel Sambuc if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
2182f4a2713aSLionel Sambuc return QualType(CT, 0);
2183f4a2713aSLionel Sambuc
2184f4a2713aSLionel Sambuc // If the pointee type isn't canonical, this won't be a canonical type either,
2185f4a2713aSLionel Sambuc // so fill in the canonical type field.
2186f4a2713aSLionel Sambuc QualType Canonical;
2187f4a2713aSLionel Sambuc if (!T.isCanonical()) {
2188f4a2713aSLionel Sambuc Canonical = getComplexType(getCanonicalType(T));
2189f4a2713aSLionel Sambuc
2190f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2191f4a2713aSLionel Sambuc ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
2192*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2193f4a2713aSLionel Sambuc }
2194f4a2713aSLionel Sambuc ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
2195f4a2713aSLionel Sambuc Types.push_back(New);
2196f4a2713aSLionel Sambuc ComplexTypes.InsertNode(New, InsertPos);
2197f4a2713aSLionel Sambuc return QualType(New, 0);
2198f4a2713aSLionel Sambuc }
2199f4a2713aSLionel Sambuc
2200f4a2713aSLionel Sambuc /// getPointerType - Return the uniqued reference to the type for a pointer to
2201f4a2713aSLionel Sambuc /// the specified type.
getPointerType(QualType T) const2202f4a2713aSLionel Sambuc QualType ASTContext::getPointerType(QualType T) const {
2203f4a2713aSLionel Sambuc // Unique pointers, to guarantee there is only one pointer of a particular
2204f4a2713aSLionel Sambuc // structure.
2205f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2206f4a2713aSLionel Sambuc PointerType::Profile(ID, T);
2207f4a2713aSLionel Sambuc
2208*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2209f4a2713aSLionel Sambuc if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2210f4a2713aSLionel Sambuc return QualType(PT, 0);
2211f4a2713aSLionel Sambuc
2212f4a2713aSLionel Sambuc // If the pointee type isn't canonical, this won't be a canonical type either,
2213f4a2713aSLionel Sambuc // so fill in the canonical type field.
2214f4a2713aSLionel Sambuc QualType Canonical;
2215f4a2713aSLionel Sambuc if (!T.isCanonical()) {
2216f4a2713aSLionel Sambuc Canonical = getPointerType(getCanonicalType(T));
2217f4a2713aSLionel Sambuc
2218f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2219f4a2713aSLionel Sambuc PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2220*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2221f4a2713aSLionel Sambuc }
2222f4a2713aSLionel Sambuc PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
2223f4a2713aSLionel Sambuc Types.push_back(New);
2224f4a2713aSLionel Sambuc PointerTypes.InsertNode(New, InsertPos);
2225f4a2713aSLionel Sambuc return QualType(New, 0);
2226f4a2713aSLionel Sambuc }
2227f4a2713aSLionel Sambuc
getAdjustedType(QualType Orig,QualType New) const2228*0a6a1f1dSLionel Sambuc QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const {
2229*0a6a1f1dSLionel Sambuc llvm::FoldingSetNodeID ID;
2230*0a6a1f1dSLionel Sambuc AdjustedType::Profile(ID, Orig, New);
2231*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2232*0a6a1f1dSLionel Sambuc AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos);
2233*0a6a1f1dSLionel Sambuc if (AT)
2234*0a6a1f1dSLionel Sambuc return QualType(AT, 0);
2235*0a6a1f1dSLionel Sambuc
2236*0a6a1f1dSLionel Sambuc QualType Canonical = getCanonicalType(New);
2237*0a6a1f1dSLionel Sambuc
2238*0a6a1f1dSLionel Sambuc // Get the new insert position for the node we care about.
2239*0a6a1f1dSLionel Sambuc AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos);
2240*0a6a1f1dSLionel Sambuc assert(!AT && "Shouldn't be in the map!");
2241*0a6a1f1dSLionel Sambuc
2242*0a6a1f1dSLionel Sambuc AT = new (*this, TypeAlignment)
2243*0a6a1f1dSLionel Sambuc AdjustedType(Type::Adjusted, Orig, New, Canonical);
2244*0a6a1f1dSLionel Sambuc Types.push_back(AT);
2245*0a6a1f1dSLionel Sambuc AdjustedTypes.InsertNode(AT, InsertPos);
2246*0a6a1f1dSLionel Sambuc return QualType(AT, 0);
2247*0a6a1f1dSLionel Sambuc }
2248*0a6a1f1dSLionel Sambuc
getDecayedType(QualType T) const2249f4a2713aSLionel Sambuc QualType ASTContext::getDecayedType(QualType T) const {
2250f4a2713aSLionel Sambuc assert((T->isArrayType() || T->isFunctionType()) && "T does not decay");
2251f4a2713aSLionel Sambuc
2252f4a2713aSLionel Sambuc QualType Decayed;
2253f4a2713aSLionel Sambuc
2254f4a2713aSLionel Sambuc // C99 6.7.5.3p7:
2255f4a2713aSLionel Sambuc // A declaration of a parameter as "array of type" shall be
2256f4a2713aSLionel Sambuc // adjusted to "qualified pointer to type", where the type
2257f4a2713aSLionel Sambuc // qualifiers (if any) are those specified within the [ and ] of
2258f4a2713aSLionel Sambuc // the array type derivation.
2259f4a2713aSLionel Sambuc if (T->isArrayType())
2260f4a2713aSLionel Sambuc Decayed = getArrayDecayedType(T);
2261f4a2713aSLionel Sambuc
2262f4a2713aSLionel Sambuc // C99 6.7.5.3p8:
2263f4a2713aSLionel Sambuc // A declaration of a parameter as "function returning type"
2264f4a2713aSLionel Sambuc // shall be adjusted to "pointer to function returning type", as
2265f4a2713aSLionel Sambuc // in 6.3.2.1.
2266f4a2713aSLionel Sambuc if (T->isFunctionType())
2267f4a2713aSLionel Sambuc Decayed = getPointerType(T);
2268f4a2713aSLionel Sambuc
2269*0a6a1f1dSLionel Sambuc llvm::FoldingSetNodeID ID;
2270*0a6a1f1dSLionel Sambuc AdjustedType::Profile(ID, T, Decayed);
2271*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2272*0a6a1f1dSLionel Sambuc AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos);
2273*0a6a1f1dSLionel Sambuc if (AT)
2274*0a6a1f1dSLionel Sambuc return QualType(AT, 0);
2275*0a6a1f1dSLionel Sambuc
2276f4a2713aSLionel Sambuc QualType Canonical = getCanonicalType(Decayed);
2277f4a2713aSLionel Sambuc
2278f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2279*0a6a1f1dSLionel Sambuc AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos);
2280*0a6a1f1dSLionel Sambuc assert(!AT && "Shouldn't be in the map!");
2281f4a2713aSLionel Sambuc
2282*0a6a1f1dSLionel Sambuc AT = new (*this, TypeAlignment) DecayedType(T, Decayed, Canonical);
2283*0a6a1f1dSLionel Sambuc Types.push_back(AT);
2284*0a6a1f1dSLionel Sambuc AdjustedTypes.InsertNode(AT, InsertPos);
2285*0a6a1f1dSLionel Sambuc return QualType(AT, 0);
2286f4a2713aSLionel Sambuc }
2287f4a2713aSLionel Sambuc
2288f4a2713aSLionel Sambuc /// getBlockPointerType - Return the uniqued reference to the type for
2289f4a2713aSLionel Sambuc /// a pointer to the specified block.
getBlockPointerType(QualType T) const2290f4a2713aSLionel Sambuc QualType ASTContext::getBlockPointerType(QualType T) const {
2291f4a2713aSLionel Sambuc assert(T->isFunctionType() && "block of function types only");
2292f4a2713aSLionel Sambuc // Unique pointers, to guarantee there is only one block of a particular
2293f4a2713aSLionel Sambuc // structure.
2294f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2295f4a2713aSLionel Sambuc BlockPointerType::Profile(ID, T);
2296f4a2713aSLionel Sambuc
2297*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2298f4a2713aSLionel Sambuc if (BlockPointerType *PT =
2299f4a2713aSLionel Sambuc BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2300f4a2713aSLionel Sambuc return QualType(PT, 0);
2301f4a2713aSLionel Sambuc
2302f4a2713aSLionel Sambuc // If the block pointee type isn't canonical, this won't be a canonical
2303f4a2713aSLionel Sambuc // type either so fill in the canonical type field.
2304f4a2713aSLionel Sambuc QualType Canonical;
2305f4a2713aSLionel Sambuc if (!T.isCanonical()) {
2306f4a2713aSLionel Sambuc Canonical = getBlockPointerType(getCanonicalType(T));
2307f4a2713aSLionel Sambuc
2308f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2309f4a2713aSLionel Sambuc BlockPointerType *NewIP =
2310f4a2713aSLionel Sambuc BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2311*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2312f4a2713aSLionel Sambuc }
2313f4a2713aSLionel Sambuc BlockPointerType *New
2314f4a2713aSLionel Sambuc = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
2315f4a2713aSLionel Sambuc Types.push_back(New);
2316f4a2713aSLionel Sambuc BlockPointerTypes.InsertNode(New, InsertPos);
2317f4a2713aSLionel Sambuc return QualType(New, 0);
2318f4a2713aSLionel Sambuc }
2319f4a2713aSLionel Sambuc
2320f4a2713aSLionel Sambuc /// getLValueReferenceType - Return the uniqued reference to the type for an
2321f4a2713aSLionel Sambuc /// lvalue reference to the specified type.
2322f4a2713aSLionel Sambuc QualType
getLValueReferenceType(QualType T,bool SpelledAsLValue) const2323f4a2713aSLionel Sambuc ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
2324f4a2713aSLionel Sambuc assert(getCanonicalType(T) != OverloadTy &&
2325f4a2713aSLionel Sambuc "Unresolved overloaded function type");
2326f4a2713aSLionel Sambuc
2327f4a2713aSLionel Sambuc // Unique pointers, to guarantee there is only one pointer of a particular
2328f4a2713aSLionel Sambuc // structure.
2329f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2330f4a2713aSLionel Sambuc ReferenceType::Profile(ID, T, SpelledAsLValue);
2331f4a2713aSLionel Sambuc
2332*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2333f4a2713aSLionel Sambuc if (LValueReferenceType *RT =
2334f4a2713aSLionel Sambuc LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
2335f4a2713aSLionel Sambuc return QualType(RT, 0);
2336f4a2713aSLionel Sambuc
2337f4a2713aSLionel Sambuc const ReferenceType *InnerRef = T->getAs<ReferenceType>();
2338f4a2713aSLionel Sambuc
2339f4a2713aSLionel Sambuc // If the referencee type isn't canonical, this won't be a canonical type
2340f4a2713aSLionel Sambuc // either, so fill in the canonical type field.
2341f4a2713aSLionel Sambuc QualType Canonical;
2342f4a2713aSLionel Sambuc if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
2343f4a2713aSLionel Sambuc QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
2344f4a2713aSLionel Sambuc Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
2345f4a2713aSLionel Sambuc
2346f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2347f4a2713aSLionel Sambuc LValueReferenceType *NewIP =
2348f4a2713aSLionel Sambuc LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
2349*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2350f4a2713aSLionel Sambuc }
2351f4a2713aSLionel Sambuc
2352f4a2713aSLionel Sambuc LValueReferenceType *New
2353f4a2713aSLionel Sambuc = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
2354f4a2713aSLionel Sambuc SpelledAsLValue);
2355f4a2713aSLionel Sambuc Types.push_back(New);
2356f4a2713aSLionel Sambuc LValueReferenceTypes.InsertNode(New, InsertPos);
2357f4a2713aSLionel Sambuc
2358f4a2713aSLionel Sambuc return QualType(New, 0);
2359f4a2713aSLionel Sambuc }
2360f4a2713aSLionel Sambuc
2361f4a2713aSLionel Sambuc /// getRValueReferenceType - Return the uniqued reference to the type for an
2362f4a2713aSLionel Sambuc /// rvalue reference to the specified type.
getRValueReferenceType(QualType T) const2363f4a2713aSLionel Sambuc QualType ASTContext::getRValueReferenceType(QualType T) const {
2364f4a2713aSLionel Sambuc // Unique pointers, to guarantee there is only one pointer of a particular
2365f4a2713aSLionel Sambuc // structure.
2366f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2367f4a2713aSLionel Sambuc ReferenceType::Profile(ID, T, false);
2368f4a2713aSLionel Sambuc
2369*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2370f4a2713aSLionel Sambuc if (RValueReferenceType *RT =
2371f4a2713aSLionel Sambuc RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
2372f4a2713aSLionel Sambuc return QualType(RT, 0);
2373f4a2713aSLionel Sambuc
2374f4a2713aSLionel Sambuc const ReferenceType *InnerRef = T->getAs<ReferenceType>();
2375f4a2713aSLionel Sambuc
2376f4a2713aSLionel Sambuc // If the referencee type isn't canonical, this won't be a canonical type
2377f4a2713aSLionel Sambuc // either, so fill in the canonical type field.
2378f4a2713aSLionel Sambuc QualType Canonical;
2379f4a2713aSLionel Sambuc if (InnerRef || !T.isCanonical()) {
2380f4a2713aSLionel Sambuc QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
2381f4a2713aSLionel Sambuc Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
2382f4a2713aSLionel Sambuc
2383f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2384f4a2713aSLionel Sambuc RValueReferenceType *NewIP =
2385f4a2713aSLionel Sambuc RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
2386*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2387f4a2713aSLionel Sambuc }
2388f4a2713aSLionel Sambuc
2389f4a2713aSLionel Sambuc RValueReferenceType *New
2390f4a2713aSLionel Sambuc = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
2391f4a2713aSLionel Sambuc Types.push_back(New);
2392f4a2713aSLionel Sambuc RValueReferenceTypes.InsertNode(New, InsertPos);
2393f4a2713aSLionel Sambuc return QualType(New, 0);
2394f4a2713aSLionel Sambuc }
2395f4a2713aSLionel Sambuc
2396f4a2713aSLionel Sambuc /// getMemberPointerType - Return the uniqued reference to the type for a
2397f4a2713aSLionel Sambuc /// member pointer to the specified type, in the specified class.
getMemberPointerType(QualType T,const Type * Cls) const2398f4a2713aSLionel Sambuc QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
2399f4a2713aSLionel Sambuc // Unique pointers, to guarantee there is only one pointer of a particular
2400f4a2713aSLionel Sambuc // structure.
2401f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2402f4a2713aSLionel Sambuc MemberPointerType::Profile(ID, T, Cls);
2403f4a2713aSLionel Sambuc
2404*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2405f4a2713aSLionel Sambuc if (MemberPointerType *PT =
2406f4a2713aSLionel Sambuc MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2407f4a2713aSLionel Sambuc return QualType(PT, 0);
2408f4a2713aSLionel Sambuc
2409f4a2713aSLionel Sambuc // If the pointee or class type isn't canonical, this won't be a canonical
2410f4a2713aSLionel Sambuc // type either, so fill in the canonical type field.
2411f4a2713aSLionel Sambuc QualType Canonical;
2412f4a2713aSLionel Sambuc if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
2413f4a2713aSLionel Sambuc Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
2414f4a2713aSLionel Sambuc
2415f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2416f4a2713aSLionel Sambuc MemberPointerType *NewIP =
2417f4a2713aSLionel Sambuc MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2418*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2419f4a2713aSLionel Sambuc }
2420f4a2713aSLionel Sambuc MemberPointerType *New
2421f4a2713aSLionel Sambuc = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
2422f4a2713aSLionel Sambuc Types.push_back(New);
2423f4a2713aSLionel Sambuc MemberPointerTypes.InsertNode(New, InsertPos);
2424f4a2713aSLionel Sambuc return QualType(New, 0);
2425f4a2713aSLionel Sambuc }
2426f4a2713aSLionel Sambuc
2427f4a2713aSLionel Sambuc /// getConstantArrayType - Return the unique reference to the type for an
2428f4a2713aSLionel Sambuc /// array of the specified element type.
getConstantArrayType(QualType EltTy,const llvm::APInt & ArySizeIn,ArrayType::ArraySizeModifier ASM,unsigned IndexTypeQuals) const2429f4a2713aSLionel Sambuc QualType ASTContext::getConstantArrayType(QualType EltTy,
2430f4a2713aSLionel Sambuc const llvm::APInt &ArySizeIn,
2431f4a2713aSLionel Sambuc ArrayType::ArraySizeModifier ASM,
2432f4a2713aSLionel Sambuc unsigned IndexTypeQuals) const {
2433f4a2713aSLionel Sambuc assert((EltTy->isDependentType() ||
2434f4a2713aSLionel Sambuc EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
2435f4a2713aSLionel Sambuc "Constant array of VLAs is illegal!");
2436f4a2713aSLionel Sambuc
2437f4a2713aSLionel Sambuc // Convert the array size into a canonical width matching the pointer size for
2438f4a2713aSLionel Sambuc // the target.
2439f4a2713aSLionel Sambuc llvm::APInt ArySize(ArySizeIn);
2440f4a2713aSLionel Sambuc ArySize =
2441f4a2713aSLionel Sambuc ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy)));
2442f4a2713aSLionel Sambuc
2443f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2444f4a2713aSLionel Sambuc ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
2445f4a2713aSLionel Sambuc
2446*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2447f4a2713aSLionel Sambuc if (ConstantArrayType *ATP =
2448f4a2713aSLionel Sambuc ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
2449f4a2713aSLionel Sambuc return QualType(ATP, 0);
2450f4a2713aSLionel Sambuc
2451f4a2713aSLionel Sambuc // If the element type isn't canonical or has qualifiers, this won't
2452f4a2713aSLionel Sambuc // be a canonical type either, so fill in the canonical type field.
2453f4a2713aSLionel Sambuc QualType Canon;
2454f4a2713aSLionel Sambuc if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
2455f4a2713aSLionel Sambuc SplitQualType canonSplit = getCanonicalType(EltTy).split();
2456f4a2713aSLionel Sambuc Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize,
2457f4a2713aSLionel Sambuc ASM, IndexTypeQuals);
2458f4a2713aSLionel Sambuc Canon = getQualifiedType(Canon, canonSplit.Quals);
2459f4a2713aSLionel Sambuc
2460f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2461f4a2713aSLionel Sambuc ConstantArrayType *NewIP =
2462f4a2713aSLionel Sambuc ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
2463*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2464f4a2713aSLionel Sambuc }
2465f4a2713aSLionel Sambuc
2466f4a2713aSLionel Sambuc ConstantArrayType *New = new(*this,TypeAlignment)
2467f4a2713aSLionel Sambuc ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
2468f4a2713aSLionel Sambuc ConstantArrayTypes.InsertNode(New, InsertPos);
2469f4a2713aSLionel Sambuc Types.push_back(New);
2470f4a2713aSLionel Sambuc return QualType(New, 0);
2471f4a2713aSLionel Sambuc }
2472f4a2713aSLionel Sambuc
2473f4a2713aSLionel Sambuc /// getVariableArrayDecayedType - Turns the given type, which may be
2474f4a2713aSLionel Sambuc /// variably-modified, into the corresponding type with all the known
2475f4a2713aSLionel Sambuc /// sizes replaced with [*].
getVariableArrayDecayedType(QualType type) const2476f4a2713aSLionel Sambuc QualType ASTContext::getVariableArrayDecayedType(QualType type) const {
2477f4a2713aSLionel Sambuc // Vastly most common case.
2478f4a2713aSLionel Sambuc if (!type->isVariablyModifiedType()) return type;
2479f4a2713aSLionel Sambuc
2480f4a2713aSLionel Sambuc QualType result;
2481f4a2713aSLionel Sambuc
2482f4a2713aSLionel Sambuc SplitQualType split = type.getSplitDesugaredType();
2483f4a2713aSLionel Sambuc const Type *ty = split.Ty;
2484f4a2713aSLionel Sambuc switch (ty->getTypeClass()) {
2485f4a2713aSLionel Sambuc #define TYPE(Class, Base)
2486f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Class, Base)
2487f4a2713aSLionel Sambuc #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2488f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
2489f4a2713aSLionel Sambuc llvm_unreachable("didn't desugar past all non-canonical types?");
2490f4a2713aSLionel Sambuc
2491f4a2713aSLionel Sambuc // These types should never be variably-modified.
2492f4a2713aSLionel Sambuc case Type::Builtin:
2493f4a2713aSLionel Sambuc case Type::Complex:
2494f4a2713aSLionel Sambuc case Type::Vector:
2495f4a2713aSLionel Sambuc case Type::ExtVector:
2496f4a2713aSLionel Sambuc case Type::DependentSizedExtVector:
2497f4a2713aSLionel Sambuc case Type::ObjCObject:
2498f4a2713aSLionel Sambuc case Type::ObjCInterface:
2499f4a2713aSLionel Sambuc case Type::ObjCObjectPointer:
2500f4a2713aSLionel Sambuc case Type::Record:
2501f4a2713aSLionel Sambuc case Type::Enum:
2502f4a2713aSLionel Sambuc case Type::UnresolvedUsing:
2503f4a2713aSLionel Sambuc case Type::TypeOfExpr:
2504f4a2713aSLionel Sambuc case Type::TypeOf:
2505f4a2713aSLionel Sambuc case Type::Decltype:
2506f4a2713aSLionel Sambuc case Type::UnaryTransform:
2507f4a2713aSLionel Sambuc case Type::DependentName:
2508f4a2713aSLionel Sambuc case Type::InjectedClassName:
2509f4a2713aSLionel Sambuc case Type::TemplateSpecialization:
2510f4a2713aSLionel Sambuc case Type::DependentTemplateSpecialization:
2511f4a2713aSLionel Sambuc case Type::TemplateTypeParm:
2512f4a2713aSLionel Sambuc case Type::SubstTemplateTypeParmPack:
2513f4a2713aSLionel Sambuc case Type::Auto:
2514f4a2713aSLionel Sambuc case Type::PackExpansion:
2515f4a2713aSLionel Sambuc llvm_unreachable("type should never be variably-modified");
2516f4a2713aSLionel Sambuc
2517f4a2713aSLionel Sambuc // These types can be variably-modified but should never need to
2518f4a2713aSLionel Sambuc // further decay.
2519f4a2713aSLionel Sambuc case Type::FunctionNoProto:
2520f4a2713aSLionel Sambuc case Type::FunctionProto:
2521f4a2713aSLionel Sambuc case Type::BlockPointer:
2522f4a2713aSLionel Sambuc case Type::MemberPointer:
2523f4a2713aSLionel Sambuc return type;
2524f4a2713aSLionel Sambuc
2525f4a2713aSLionel Sambuc // These types can be variably-modified. All these modifications
2526f4a2713aSLionel Sambuc // preserve structure except as noted by comments.
2527f4a2713aSLionel Sambuc // TODO: if we ever care about optimizing VLAs, there are no-op
2528f4a2713aSLionel Sambuc // optimizations available here.
2529f4a2713aSLionel Sambuc case Type::Pointer:
2530f4a2713aSLionel Sambuc result = getPointerType(getVariableArrayDecayedType(
2531f4a2713aSLionel Sambuc cast<PointerType>(ty)->getPointeeType()));
2532f4a2713aSLionel Sambuc break;
2533f4a2713aSLionel Sambuc
2534f4a2713aSLionel Sambuc case Type::LValueReference: {
2535f4a2713aSLionel Sambuc const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
2536f4a2713aSLionel Sambuc result = getLValueReferenceType(
2537f4a2713aSLionel Sambuc getVariableArrayDecayedType(lv->getPointeeType()),
2538f4a2713aSLionel Sambuc lv->isSpelledAsLValue());
2539f4a2713aSLionel Sambuc break;
2540f4a2713aSLionel Sambuc }
2541f4a2713aSLionel Sambuc
2542f4a2713aSLionel Sambuc case Type::RValueReference: {
2543f4a2713aSLionel Sambuc const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
2544f4a2713aSLionel Sambuc result = getRValueReferenceType(
2545f4a2713aSLionel Sambuc getVariableArrayDecayedType(lv->getPointeeType()));
2546f4a2713aSLionel Sambuc break;
2547f4a2713aSLionel Sambuc }
2548f4a2713aSLionel Sambuc
2549f4a2713aSLionel Sambuc case Type::Atomic: {
2550f4a2713aSLionel Sambuc const AtomicType *at = cast<AtomicType>(ty);
2551f4a2713aSLionel Sambuc result = getAtomicType(getVariableArrayDecayedType(at->getValueType()));
2552f4a2713aSLionel Sambuc break;
2553f4a2713aSLionel Sambuc }
2554f4a2713aSLionel Sambuc
2555f4a2713aSLionel Sambuc case Type::ConstantArray: {
2556f4a2713aSLionel Sambuc const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
2557f4a2713aSLionel Sambuc result = getConstantArrayType(
2558f4a2713aSLionel Sambuc getVariableArrayDecayedType(cat->getElementType()),
2559f4a2713aSLionel Sambuc cat->getSize(),
2560f4a2713aSLionel Sambuc cat->getSizeModifier(),
2561f4a2713aSLionel Sambuc cat->getIndexTypeCVRQualifiers());
2562f4a2713aSLionel Sambuc break;
2563f4a2713aSLionel Sambuc }
2564f4a2713aSLionel Sambuc
2565f4a2713aSLionel Sambuc case Type::DependentSizedArray: {
2566f4a2713aSLionel Sambuc const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
2567f4a2713aSLionel Sambuc result = getDependentSizedArrayType(
2568f4a2713aSLionel Sambuc getVariableArrayDecayedType(dat->getElementType()),
2569f4a2713aSLionel Sambuc dat->getSizeExpr(),
2570f4a2713aSLionel Sambuc dat->getSizeModifier(),
2571f4a2713aSLionel Sambuc dat->getIndexTypeCVRQualifiers(),
2572f4a2713aSLionel Sambuc dat->getBracketsRange());
2573f4a2713aSLionel Sambuc break;
2574f4a2713aSLionel Sambuc }
2575f4a2713aSLionel Sambuc
2576f4a2713aSLionel Sambuc // Turn incomplete types into [*] types.
2577f4a2713aSLionel Sambuc case Type::IncompleteArray: {
2578f4a2713aSLionel Sambuc const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
2579f4a2713aSLionel Sambuc result = getVariableArrayType(
2580f4a2713aSLionel Sambuc getVariableArrayDecayedType(iat->getElementType()),
2581*0a6a1f1dSLionel Sambuc /*size*/ nullptr,
2582f4a2713aSLionel Sambuc ArrayType::Normal,
2583f4a2713aSLionel Sambuc iat->getIndexTypeCVRQualifiers(),
2584f4a2713aSLionel Sambuc SourceRange());
2585f4a2713aSLionel Sambuc break;
2586f4a2713aSLionel Sambuc }
2587f4a2713aSLionel Sambuc
2588f4a2713aSLionel Sambuc // Turn VLA types into [*] types.
2589f4a2713aSLionel Sambuc case Type::VariableArray: {
2590f4a2713aSLionel Sambuc const VariableArrayType *vat = cast<VariableArrayType>(ty);
2591f4a2713aSLionel Sambuc result = getVariableArrayType(
2592f4a2713aSLionel Sambuc getVariableArrayDecayedType(vat->getElementType()),
2593*0a6a1f1dSLionel Sambuc /*size*/ nullptr,
2594f4a2713aSLionel Sambuc ArrayType::Star,
2595f4a2713aSLionel Sambuc vat->getIndexTypeCVRQualifiers(),
2596f4a2713aSLionel Sambuc vat->getBracketsRange());
2597f4a2713aSLionel Sambuc break;
2598f4a2713aSLionel Sambuc }
2599f4a2713aSLionel Sambuc }
2600f4a2713aSLionel Sambuc
2601f4a2713aSLionel Sambuc // Apply the top-level qualifiers from the original.
2602f4a2713aSLionel Sambuc return getQualifiedType(result, split.Quals);
2603f4a2713aSLionel Sambuc }
2604f4a2713aSLionel Sambuc
2605f4a2713aSLionel Sambuc /// getVariableArrayType - Returns a non-unique reference to the type for a
2606f4a2713aSLionel Sambuc /// variable array of the specified element type.
getVariableArrayType(QualType EltTy,Expr * NumElts,ArrayType::ArraySizeModifier ASM,unsigned IndexTypeQuals,SourceRange Brackets) const2607f4a2713aSLionel Sambuc QualType ASTContext::getVariableArrayType(QualType EltTy,
2608f4a2713aSLionel Sambuc Expr *NumElts,
2609f4a2713aSLionel Sambuc ArrayType::ArraySizeModifier ASM,
2610f4a2713aSLionel Sambuc unsigned IndexTypeQuals,
2611f4a2713aSLionel Sambuc SourceRange Brackets) const {
2612f4a2713aSLionel Sambuc // Since we don't unique expressions, it isn't possible to unique VLA's
2613f4a2713aSLionel Sambuc // that have an expression provided for their size.
2614f4a2713aSLionel Sambuc QualType Canon;
2615f4a2713aSLionel Sambuc
2616f4a2713aSLionel Sambuc // Be sure to pull qualifiers off the element type.
2617f4a2713aSLionel Sambuc if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
2618f4a2713aSLionel Sambuc SplitQualType canonSplit = getCanonicalType(EltTy).split();
2619f4a2713aSLionel Sambuc Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM,
2620f4a2713aSLionel Sambuc IndexTypeQuals, Brackets);
2621f4a2713aSLionel Sambuc Canon = getQualifiedType(Canon, canonSplit.Quals);
2622f4a2713aSLionel Sambuc }
2623f4a2713aSLionel Sambuc
2624f4a2713aSLionel Sambuc VariableArrayType *New = new(*this, TypeAlignment)
2625f4a2713aSLionel Sambuc VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
2626f4a2713aSLionel Sambuc
2627f4a2713aSLionel Sambuc VariableArrayTypes.push_back(New);
2628f4a2713aSLionel Sambuc Types.push_back(New);
2629f4a2713aSLionel Sambuc return QualType(New, 0);
2630f4a2713aSLionel Sambuc }
2631f4a2713aSLionel Sambuc
2632f4a2713aSLionel Sambuc /// getDependentSizedArrayType - Returns a non-unique reference to
2633f4a2713aSLionel Sambuc /// the type for a dependently-sized array of the specified element
2634f4a2713aSLionel Sambuc /// type.
getDependentSizedArrayType(QualType elementType,Expr * numElements,ArrayType::ArraySizeModifier ASM,unsigned elementTypeQuals,SourceRange brackets) const2635f4a2713aSLionel Sambuc QualType ASTContext::getDependentSizedArrayType(QualType elementType,
2636f4a2713aSLionel Sambuc Expr *numElements,
2637f4a2713aSLionel Sambuc ArrayType::ArraySizeModifier ASM,
2638f4a2713aSLionel Sambuc unsigned elementTypeQuals,
2639f4a2713aSLionel Sambuc SourceRange brackets) const {
2640f4a2713aSLionel Sambuc assert((!numElements || numElements->isTypeDependent() ||
2641f4a2713aSLionel Sambuc numElements->isValueDependent()) &&
2642f4a2713aSLionel Sambuc "Size must be type- or value-dependent!");
2643f4a2713aSLionel Sambuc
2644f4a2713aSLionel Sambuc // Dependently-sized array types that do not have a specified number
2645f4a2713aSLionel Sambuc // of elements will have their sizes deduced from a dependent
2646f4a2713aSLionel Sambuc // initializer. We do no canonicalization here at all, which is okay
2647f4a2713aSLionel Sambuc // because they can't be used in most locations.
2648f4a2713aSLionel Sambuc if (!numElements) {
2649f4a2713aSLionel Sambuc DependentSizedArrayType *newType
2650f4a2713aSLionel Sambuc = new (*this, TypeAlignment)
2651f4a2713aSLionel Sambuc DependentSizedArrayType(*this, elementType, QualType(),
2652f4a2713aSLionel Sambuc numElements, ASM, elementTypeQuals,
2653f4a2713aSLionel Sambuc brackets);
2654f4a2713aSLionel Sambuc Types.push_back(newType);
2655f4a2713aSLionel Sambuc return QualType(newType, 0);
2656f4a2713aSLionel Sambuc }
2657f4a2713aSLionel Sambuc
2658f4a2713aSLionel Sambuc // Otherwise, we actually build a new type every time, but we
2659f4a2713aSLionel Sambuc // also build a canonical type.
2660f4a2713aSLionel Sambuc
2661f4a2713aSLionel Sambuc SplitQualType canonElementType = getCanonicalType(elementType).split();
2662f4a2713aSLionel Sambuc
2663*0a6a1f1dSLionel Sambuc void *insertPos = nullptr;
2664f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2665f4a2713aSLionel Sambuc DependentSizedArrayType::Profile(ID, *this,
2666f4a2713aSLionel Sambuc QualType(canonElementType.Ty, 0),
2667f4a2713aSLionel Sambuc ASM, elementTypeQuals, numElements);
2668f4a2713aSLionel Sambuc
2669f4a2713aSLionel Sambuc // Look for an existing type with these properties.
2670f4a2713aSLionel Sambuc DependentSizedArrayType *canonTy =
2671f4a2713aSLionel Sambuc DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
2672f4a2713aSLionel Sambuc
2673f4a2713aSLionel Sambuc // If we don't have one, build one.
2674f4a2713aSLionel Sambuc if (!canonTy) {
2675f4a2713aSLionel Sambuc canonTy = new (*this, TypeAlignment)
2676f4a2713aSLionel Sambuc DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0),
2677f4a2713aSLionel Sambuc QualType(), numElements, ASM, elementTypeQuals,
2678f4a2713aSLionel Sambuc brackets);
2679f4a2713aSLionel Sambuc DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
2680f4a2713aSLionel Sambuc Types.push_back(canonTy);
2681f4a2713aSLionel Sambuc }
2682f4a2713aSLionel Sambuc
2683f4a2713aSLionel Sambuc // Apply qualifiers from the element type to the array.
2684f4a2713aSLionel Sambuc QualType canon = getQualifiedType(QualType(canonTy,0),
2685f4a2713aSLionel Sambuc canonElementType.Quals);
2686f4a2713aSLionel Sambuc
2687f4a2713aSLionel Sambuc // If we didn't need extra canonicalization for the element type,
2688f4a2713aSLionel Sambuc // then just use that as our result.
2689f4a2713aSLionel Sambuc if (QualType(canonElementType.Ty, 0) == elementType)
2690f4a2713aSLionel Sambuc return canon;
2691f4a2713aSLionel Sambuc
2692f4a2713aSLionel Sambuc // Otherwise, we need to build a type which follows the spelling
2693f4a2713aSLionel Sambuc // of the element type.
2694f4a2713aSLionel Sambuc DependentSizedArrayType *sugaredType
2695f4a2713aSLionel Sambuc = new (*this, TypeAlignment)
2696f4a2713aSLionel Sambuc DependentSizedArrayType(*this, elementType, canon, numElements,
2697f4a2713aSLionel Sambuc ASM, elementTypeQuals, brackets);
2698f4a2713aSLionel Sambuc Types.push_back(sugaredType);
2699f4a2713aSLionel Sambuc return QualType(sugaredType, 0);
2700f4a2713aSLionel Sambuc }
2701f4a2713aSLionel Sambuc
getIncompleteArrayType(QualType elementType,ArrayType::ArraySizeModifier ASM,unsigned elementTypeQuals) const2702f4a2713aSLionel Sambuc QualType ASTContext::getIncompleteArrayType(QualType elementType,
2703f4a2713aSLionel Sambuc ArrayType::ArraySizeModifier ASM,
2704f4a2713aSLionel Sambuc unsigned elementTypeQuals) const {
2705f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2706f4a2713aSLionel Sambuc IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
2707f4a2713aSLionel Sambuc
2708*0a6a1f1dSLionel Sambuc void *insertPos = nullptr;
2709f4a2713aSLionel Sambuc if (IncompleteArrayType *iat =
2710f4a2713aSLionel Sambuc IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
2711f4a2713aSLionel Sambuc return QualType(iat, 0);
2712f4a2713aSLionel Sambuc
2713f4a2713aSLionel Sambuc // If the element type isn't canonical, this won't be a canonical type
2714f4a2713aSLionel Sambuc // either, so fill in the canonical type field. We also have to pull
2715f4a2713aSLionel Sambuc // qualifiers off the element type.
2716f4a2713aSLionel Sambuc QualType canon;
2717f4a2713aSLionel Sambuc
2718f4a2713aSLionel Sambuc if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
2719f4a2713aSLionel Sambuc SplitQualType canonSplit = getCanonicalType(elementType).split();
2720f4a2713aSLionel Sambuc canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0),
2721f4a2713aSLionel Sambuc ASM, elementTypeQuals);
2722f4a2713aSLionel Sambuc canon = getQualifiedType(canon, canonSplit.Quals);
2723f4a2713aSLionel Sambuc
2724f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2725f4a2713aSLionel Sambuc IncompleteArrayType *existing =
2726f4a2713aSLionel Sambuc IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
2727f4a2713aSLionel Sambuc assert(!existing && "Shouldn't be in the map!"); (void) existing;
2728f4a2713aSLionel Sambuc }
2729f4a2713aSLionel Sambuc
2730f4a2713aSLionel Sambuc IncompleteArrayType *newType = new (*this, TypeAlignment)
2731f4a2713aSLionel Sambuc IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
2732f4a2713aSLionel Sambuc
2733f4a2713aSLionel Sambuc IncompleteArrayTypes.InsertNode(newType, insertPos);
2734f4a2713aSLionel Sambuc Types.push_back(newType);
2735f4a2713aSLionel Sambuc return QualType(newType, 0);
2736f4a2713aSLionel Sambuc }
2737f4a2713aSLionel Sambuc
2738f4a2713aSLionel Sambuc /// getVectorType - Return the unique reference to a vector type of
2739f4a2713aSLionel Sambuc /// the specified element type and size. VectorType must be a built-in type.
getVectorType(QualType vecType,unsigned NumElts,VectorType::VectorKind VecKind) const2740f4a2713aSLionel Sambuc QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
2741f4a2713aSLionel Sambuc VectorType::VectorKind VecKind) const {
2742f4a2713aSLionel Sambuc assert(vecType->isBuiltinType());
2743f4a2713aSLionel Sambuc
2744f4a2713aSLionel Sambuc // Check if we've already instantiated a vector of this type.
2745f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2746f4a2713aSLionel Sambuc VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
2747f4a2713aSLionel Sambuc
2748*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2749f4a2713aSLionel Sambuc if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
2750f4a2713aSLionel Sambuc return QualType(VTP, 0);
2751f4a2713aSLionel Sambuc
2752f4a2713aSLionel Sambuc // If the element type isn't canonical, this won't be a canonical type either,
2753f4a2713aSLionel Sambuc // so fill in the canonical type field.
2754f4a2713aSLionel Sambuc QualType Canonical;
2755f4a2713aSLionel Sambuc if (!vecType.isCanonical()) {
2756f4a2713aSLionel Sambuc Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
2757f4a2713aSLionel Sambuc
2758f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2759f4a2713aSLionel Sambuc VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2760*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2761f4a2713aSLionel Sambuc }
2762f4a2713aSLionel Sambuc VectorType *New = new (*this, TypeAlignment)
2763f4a2713aSLionel Sambuc VectorType(vecType, NumElts, Canonical, VecKind);
2764f4a2713aSLionel Sambuc VectorTypes.InsertNode(New, InsertPos);
2765f4a2713aSLionel Sambuc Types.push_back(New);
2766f4a2713aSLionel Sambuc return QualType(New, 0);
2767f4a2713aSLionel Sambuc }
2768f4a2713aSLionel Sambuc
2769f4a2713aSLionel Sambuc /// getExtVectorType - Return the unique reference to an extended vector type of
2770f4a2713aSLionel Sambuc /// the specified element type and size. VectorType must be a built-in type.
2771f4a2713aSLionel Sambuc QualType
getExtVectorType(QualType vecType,unsigned NumElts) const2772f4a2713aSLionel Sambuc ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
2773f4a2713aSLionel Sambuc assert(vecType->isBuiltinType() || vecType->isDependentType());
2774f4a2713aSLionel Sambuc
2775f4a2713aSLionel Sambuc // Check if we've already instantiated a vector of this type.
2776f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2777f4a2713aSLionel Sambuc VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
2778f4a2713aSLionel Sambuc VectorType::GenericVector);
2779*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2780f4a2713aSLionel Sambuc if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
2781f4a2713aSLionel Sambuc return QualType(VTP, 0);
2782f4a2713aSLionel Sambuc
2783f4a2713aSLionel Sambuc // If the element type isn't canonical, this won't be a canonical type either,
2784f4a2713aSLionel Sambuc // so fill in the canonical type field.
2785f4a2713aSLionel Sambuc QualType Canonical;
2786f4a2713aSLionel Sambuc if (!vecType.isCanonical()) {
2787f4a2713aSLionel Sambuc Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
2788f4a2713aSLionel Sambuc
2789f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2790f4a2713aSLionel Sambuc VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2791*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2792f4a2713aSLionel Sambuc }
2793f4a2713aSLionel Sambuc ExtVectorType *New = new (*this, TypeAlignment)
2794f4a2713aSLionel Sambuc ExtVectorType(vecType, NumElts, Canonical);
2795f4a2713aSLionel Sambuc VectorTypes.InsertNode(New, InsertPos);
2796f4a2713aSLionel Sambuc Types.push_back(New);
2797f4a2713aSLionel Sambuc return QualType(New, 0);
2798f4a2713aSLionel Sambuc }
2799f4a2713aSLionel Sambuc
2800f4a2713aSLionel Sambuc QualType
getDependentSizedExtVectorType(QualType vecType,Expr * SizeExpr,SourceLocation AttrLoc) const2801f4a2713aSLionel Sambuc ASTContext::getDependentSizedExtVectorType(QualType vecType,
2802f4a2713aSLionel Sambuc Expr *SizeExpr,
2803f4a2713aSLionel Sambuc SourceLocation AttrLoc) const {
2804f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2805f4a2713aSLionel Sambuc DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
2806f4a2713aSLionel Sambuc SizeExpr);
2807f4a2713aSLionel Sambuc
2808*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2809f4a2713aSLionel Sambuc DependentSizedExtVectorType *Canon
2810f4a2713aSLionel Sambuc = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2811f4a2713aSLionel Sambuc DependentSizedExtVectorType *New;
2812f4a2713aSLionel Sambuc if (Canon) {
2813f4a2713aSLionel Sambuc // We already have a canonical version of this array type; use it as
2814f4a2713aSLionel Sambuc // the canonical type for a newly-built type.
2815f4a2713aSLionel Sambuc New = new (*this, TypeAlignment)
2816f4a2713aSLionel Sambuc DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
2817f4a2713aSLionel Sambuc SizeExpr, AttrLoc);
2818f4a2713aSLionel Sambuc } else {
2819f4a2713aSLionel Sambuc QualType CanonVecTy = getCanonicalType(vecType);
2820f4a2713aSLionel Sambuc if (CanonVecTy == vecType) {
2821f4a2713aSLionel Sambuc New = new (*this, TypeAlignment)
2822f4a2713aSLionel Sambuc DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
2823f4a2713aSLionel Sambuc AttrLoc);
2824f4a2713aSLionel Sambuc
2825f4a2713aSLionel Sambuc DependentSizedExtVectorType *CanonCheck
2826f4a2713aSLionel Sambuc = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2827f4a2713aSLionel Sambuc assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
2828f4a2713aSLionel Sambuc (void)CanonCheck;
2829f4a2713aSLionel Sambuc DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
2830f4a2713aSLionel Sambuc } else {
2831f4a2713aSLionel Sambuc QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
2832f4a2713aSLionel Sambuc SourceLocation());
2833f4a2713aSLionel Sambuc New = new (*this, TypeAlignment)
2834f4a2713aSLionel Sambuc DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
2835f4a2713aSLionel Sambuc }
2836f4a2713aSLionel Sambuc }
2837f4a2713aSLionel Sambuc
2838f4a2713aSLionel Sambuc Types.push_back(New);
2839f4a2713aSLionel Sambuc return QualType(New, 0);
2840f4a2713aSLionel Sambuc }
2841f4a2713aSLionel Sambuc
2842f4a2713aSLionel Sambuc /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
2843f4a2713aSLionel Sambuc ///
2844f4a2713aSLionel Sambuc QualType
getFunctionNoProtoType(QualType ResultTy,const FunctionType::ExtInfo & Info) const2845f4a2713aSLionel Sambuc ASTContext::getFunctionNoProtoType(QualType ResultTy,
2846f4a2713aSLionel Sambuc const FunctionType::ExtInfo &Info) const {
2847f4a2713aSLionel Sambuc const CallingConv CallConv = Info.getCC();
2848f4a2713aSLionel Sambuc
2849f4a2713aSLionel Sambuc // Unique functions, to guarantee there is only one function of a particular
2850f4a2713aSLionel Sambuc // structure.
2851f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2852f4a2713aSLionel Sambuc FunctionNoProtoType::Profile(ID, ResultTy, Info);
2853f4a2713aSLionel Sambuc
2854*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2855f4a2713aSLionel Sambuc if (FunctionNoProtoType *FT =
2856f4a2713aSLionel Sambuc FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
2857f4a2713aSLionel Sambuc return QualType(FT, 0);
2858f4a2713aSLionel Sambuc
2859f4a2713aSLionel Sambuc QualType Canonical;
2860f4a2713aSLionel Sambuc if (!ResultTy.isCanonical()) {
2861f4a2713aSLionel Sambuc Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), Info);
2862f4a2713aSLionel Sambuc
2863f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2864f4a2713aSLionel Sambuc FunctionNoProtoType *NewIP =
2865f4a2713aSLionel Sambuc FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2866*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2867f4a2713aSLionel Sambuc }
2868f4a2713aSLionel Sambuc
2869f4a2713aSLionel Sambuc FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
2870f4a2713aSLionel Sambuc FunctionNoProtoType *New = new (*this, TypeAlignment)
2871f4a2713aSLionel Sambuc FunctionNoProtoType(ResultTy, Canonical, newInfo);
2872f4a2713aSLionel Sambuc Types.push_back(New);
2873f4a2713aSLionel Sambuc FunctionNoProtoTypes.InsertNode(New, InsertPos);
2874f4a2713aSLionel Sambuc return QualType(New, 0);
2875f4a2713aSLionel Sambuc }
2876f4a2713aSLionel Sambuc
2877f4a2713aSLionel Sambuc /// \brief Determine whether \p T is canonical as the result type of a function.
isCanonicalResultType(QualType T)2878f4a2713aSLionel Sambuc static bool isCanonicalResultType(QualType T) {
2879f4a2713aSLionel Sambuc return T.isCanonical() &&
2880f4a2713aSLionel Sambuc (T.getObjCLifetime() == Qualifiers::OCL_None ||
2881f4a2713aSLionel Sambuc T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone);
2882f4a2713aSLionel Sambuc }
2883f4a2713aSLionel Sambuc
2884f4a2713aSLionel Sambuc QualType
getFunctionType(QualType ResultTy,ArrayRef<QualType> ArgArray,const FunctionProtoType::ExtProtoInfo & EPI) const2885f4a2713aSLionel Sambuc ASTContext::getFunctionType(QualType ResultTy, ArrayRef<QualType> ArgArray,
2886f4a2713aSLionel Sambuc const FunctionProtoType::ExtProtoInfo &EPI) const {
2887f4a2713aSLionel Sambuc size_t NumArgs = ArgArray.size();
2888f4a2713aSLionel Sambuc
2889f4a2713aSLionel Sambuc // Unique functions, to guarantee there is only one function of a particular
2890f4a2713aSLionel Sambuc // structure.
2891f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
2892f4a2713aSLionel Sambuc FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI,
2893f4a2713aSLionel Sambuc *this);
2894f4a2713aSLionel Sambuc
2895*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2896f4a2713aSLionel Sambuc if (FunctionProtoType *FTP =
2897f4a2713aSLionel Sambuc FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
2898f4a2713aSLionel Sambuc return QualType(FTP, 0);
2899f4a2713aSLionel Sambuc
2900f4a2713aSLionel Sambuc // Determine whether the type being created is already canonical or not.
2901f4a2713aSLionel Sambuc bool isCanonical =
2902*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec.Type == EST_None && isCanonicalResultType(ResultTy) &&
2903f4a2713aSLionel Sambuc !EPI.HasTrailingReturn;
2904f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
2905f4a2713aSLionel Sambuc if (!ArgArray[i].isCanonicalAsParam())
2906f4a2713aSLionel Sambuc isCanonical = false;
2907f4a2713aSLionel Sambuc
2908f4a2713aSLionel Sambuc // If this type isn't canonical, get the canonical version of it.
2909f4a2713aSLionel Sambuc // The exception spec is not part of the canonical type.
2910f4a2713aSLionel Sambuc QualType Canonical;
2911f4a2713aSLionel Sambuc if (!isCanonical) {
2912f4a2713aSLionel Sambuc SmallVector<QualType, 16> CanonicalArgs;
2913f4a2713aSLionel Sambuc CanonicalArgs.reserve(NumArgs);
2914f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumArgs; ++i)
2915f4a2713aSLionel Sambuc CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
2916f4a2713aSLionel Sambuc
2917f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
2918f4a2713aSLionel Sambuc CanonicalEPI.HasTrailingReturn = false;
2919*0a6a1f1dSLionel Sambuc CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo();
2920f4a2713aSLionel Sambuc
2921f4a2713aSLionel Sambuc // Result types do not have ARC lifetime qualifiers.
2922f4a2713aSLionel Sambuc QualType CanResultTy = getCanonicalType(ResultTy);
2923f4a2713aSLionel Sambuc if (ResultTy.getQualifiers().hasObjCLifetime()) {
2924f4a2713aSLionel Sambuc Qualifiers Qs = CanResultTy.getQualifiers();
2925f4a2713aSLionel Sambuc Qs.removeObjCLifetime();
2926f4a2713aSLionel Sambuc CanResultTy = getQualifiedType(CanResultTy.getUnqualifiedType(), Qs);
2927f4a2713aSLionel Sambuc }
2928f4a2713aSLionel Sambuc
2929f4a2713aSLionel Sambuc Canonical = getFunctionType(CanResultTy, CanonicalArgs, CanonicalEPI);
2930f4a2713aSLionel Sambuc
2931f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
2932f4a2713aSLionel Sambuc FunctionProtoType *NewIP =
2933f4a2713aSLionel Sambuc FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2934*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
2935f4a2713aSLionel Sambuc }
2936f4a2713aSLionel Sambuc
2937f4a2713aSLionel Sambuc // FunctionProtoType objects are allocated with extra bytes after
2938f4a2713aSLionel Sambuc // them for three variable size arrays at the end:
2939f4a2713aSLionel Sambuc // - parameter types
2940f4a2713aSLionel Sambuc // - exception types
2941f4a2713aSLionel Sambuc // - consumed-arguments flags
2942f4a2713aSLionel Sambuc // Instead of the exception types, there could be a noexcept
2943f4a2713aSLionel Sambuc // expression, or information used to resolve the exception
2944f4a2713aSLionel Sambuc // specification.
2945f4a2713aSLionel Sambuc size_t Size = sizeof(FunctionProtoType) +
2946f4a2713aSLionel Sambuc NumArgs * sizeof(QualType);
2947*0a6a1f1dSLionel Sambuc if (EPI.ExceptionSpec.Type == EST_Dynamic) {
2948*0a6a1f1dSLionel Sambuc Size += EPI.ExceptionSpec.Exceptions.size() * sizeof(QualType);
2949*0a6a1f1dSLionel Sambuc } else if (EPI.ExceptionSpec.Type == EST_ComputedNoexcept) {
2950f4a2713aSLionel Sambuc Size += sizeof(Expr*);
2951*0a6a1f1dSLionel Sambuc } else if (EPI.ExceptionSpec.Type == EST_Uninstantiated) {
2952f4a2713aSLionel Sambuc Size += 2 * sizeof(FunctionDecl*);
2953*0a6a1f1dSLionel Sambuc } else if (EPI.ExceptionSpec.Type == EST_Unevaluated) {
2954f4a2713aSLionel Sambuc Size += sizeof(FunctionDecl*);
2955f4a2713aSLionel Sambuc }
2956*0a6a1f1dSLionel Sambuc if (EPI.ConsumedParameters)
2957f4a2713aSLionel Sambuc Size += NumArgs * sizeof(bool);
2958f4a2713aSLionel Sambuc
2959f4a2713aSLionel Sambuc FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
2960f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo newEPI = EPI;
2961f4a2713aSLionel Sambuc new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI);
2962f4a2713aSLionel Sambuc Types.push_back(FTP);
2963f4a2713aSLionel Sambuc FunctionProtoTypes.InsertNode(FTP, InsertPos);
2964f4a2713aSLionel Sambuc return QualType(FTP, 0);
2965f4a2713aSLionel Sambuc }
2966f4a2713aSLionel Sambuc
2967f4a2713aSLionel Sambuc #ifndef NDEBUG
NeedsInjectedClassNameType(const RecordDecl * D)2968f4a2713aSLionel Sambuc static bool NeedsInjectedClassNameType(const RecordDecl *D) {
2969f4a2713aSLionel Sambuc if (!isa<CXXRecordDecl>(D)) return false;
2970f4a2713aSLionel Sambuc const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
2971f4a2713aSLionel Sambuc if (isa<ClassTemplatePartialSpecializationDecl>(RD))
2972f4a2713aSLionel Sambuc return true;
2973f4a2713aSLionel Sambuc if (RD->getDescribedClassTemplate() &&
2974f4a2713aSLionel Sambuc !isa<ClassTemplateSpecializationDecl>(RD))
2975f4a2713aSLionel Sambuc return true;
2976f4a2713aSLionel Sambuc return false;
2977f4a2713aSLionel Sambuc }
2978f4a2713aSLionel Sambuc #endif
2979f4a2713aSLionel Sambuc
2980f4a2713aSLionel Sambuc /// getInjectedClassNameType - Return the unique reference to the
2981f4a2713aSLionel Sambuc /// injected class name type for the specified templated declaration.
getInjectedClassNameType(CXXRecordDecl * Decl,QualType TST) const2982f4a2713aSLionel Sambuc QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
2983f4a2713aSLionel Sambuc QualType TST) const {
2984f4a2713aSLionel Sambuc assert(NeedsInjectedClassNameType(Decl));
2985f4a2713aSLionel Sambuc if (Decl->TypeForDecl) {
2986f4a2713aSLionel Sambuc assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
2987f4a2713aSLionel Sambuc } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) {
2988f4a2713aSLionel Sambuc assert(PrevDecl->TypeForDecl && "previous declaration has no type");
2989f4a2713aSLionel Sambuc Decl->TypeForDecl = PrevDecl->TypeForDecl;
2990f4a2713aSLionel Sambuc assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
2991f4a2713aSLionel Sambuc } else {
2992f4a2713aSLionel Sambuc Type *newType =
2993f4a2713aSLionel Sambuc new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
2994f4a2713aSLionel Sambuc Decl->TypeForDecl = newType;
2995f4a2713aSLionel Sambuc Types.push_back(newType);
2996f4a2713aSLionel Sambuc }
2997f4a2713aSLionel Sambuc return QualType(Decl->TypeForDecl, 0);
2998f4a2713aSLionel Sambuc }
2999f4a2713aSLionel Sambuc
3000f4a2713aSLionel Sambuc /// getTypeDeclType - Return the unique reference to the type for the
3001f4a2713aSLionel Sambuc /// specified type declaration.
getTypeDeclTypeSlow(const TypeDecl * Decl) const3002f4a2713aSLionel Sambuc QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
3003f4a2713aSLionel Sambuc assert(Decl && "Passed null for Decl param");
3004f4a2713aSLionel Sambuc assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
3005f4a2713aSLionel Sambuc
3006f4a2713aSLionel Sambuc if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
3007f4a2713aSLionel Sambuc return getTypedefType(Typedef);
3008f4a2713aSLionel Sambuc
3009f4a2713aSLionel Sambuc assert(!isa<TemplateTypeParmDecl>(Decl) &&
3010f4a2713aSLionel Sambuc "Template type parameter types are always available.");
3011f4a2713aSLionel Sambuc
3012f4a2713aSLionel Sambuc if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
3013f4a2713aSLionel Sambuc assert(Record->isFirstDecl() && "struct/union has previous declaration");
3014f4a2713aSLionel Sambuc assert(!NeedsInjectedClassNameType(Record));
3015f4a2713aSLionel Sambuc return getRecordType(Record);
3016f4a2713aSLionel Sambuc } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
3017f4a2713aSLionel Sambuc assert(Enum->isFirstDecl() && "enum has previous declaration");
3018f4a2713aSLionel Sambuc return getEnumType(Enum);
3019f4a2713aSLionel Sambuc } else if (const UnresolvedUsingTypenameDecl *Using =
3020f4a2713aSLionel Sambuc dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
3021f4a2713aSLionel Sambuc Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
3022f4a2713aSLionel Sambuc Decl->TypeForDecl = newType;
3023f4a2713aSLionel Sambuc Types.push_back(newType);
3024f4a2713aSLionel Sambuc } else
3025f4a2713aSLionel Sambuc llvm_unreachable("TypeDecl without a type?");
3026f4a2713aSLionel Sambuc
3027f4a2713aSLionel Sambuc return QualType(Decl->TypeForDecl, 0);
3028f4a2713aSLionel Sambuc }
3029f4a2713aSLionel Sambuc
3030f4a2713aSLionel Sambuc /// getTypedefType - Return the unique reference to the type for the
3031f4a2713aSLionel Sambuc /// specified typedef name decl.
3032f4a2713aSLionel Sambuc QualType
getTypedefType(const TypedefNameDecl * Decl,QualType Canonical) const3033f4a2713aSLionel Sambuc ASTContext::getTypedefType(const TypedefNameDecl *Decl,
3034f4a2713aSLionel Sambuc QualType Canonical) const {
3035f4a2713aSLionel Sambuc if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
3036f4a2713aSLionel Sambuc
3037f4a2713aSLionel Sambuc if (Canonical.isNull())
3038f4a2713aSLionel Sambuc Canonical = getCanonicalType(Decl->getUnderlyingType());
3039f4a2713aSLionel Sambuc TypedefType *newType = new(*this, TypeAlignment)
3040f4a2713aSLionel Sambuc TypedefType(Type::Typedef, Decl, Canonical);
3041f4a2713aSLionel Sambuc Decl->TypeForDecl = newType;
3042f4a2713aSLionel Sambuc Types.push_back(newType);
3043f4a2713aSLionel Sambuc return QualType(newType, 0);
3044f4a2713aSLionel Sambuc }
3045f4a2713aSLionel Sambuc
getRecordType(const RecordDecl * Decl) const3046f4a2713aSLionel Sambuc QualType ASTContext::getRecordType(const RecordDecl *Decl) const {
3047f4a2713aSLionel Sambuc if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
3048f4a2713aSLionel Sambuc
3049f4a2713aSLionel Sambuc if (const RecordDecl *PrevDecl = Decl->getPreviousDecl())
3050f4a2713aSLionel Sambuc if (PrevDecl->TypeForDecl)
3051f4a2713aSLionel Sambuc return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
3052f4a2713aSLionel Sambuc
3053f4a2713aSLionel Sambuc RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
3054f4a2713aSLionel Sambuc Decl->TypeForDecl = newType;
3055f4a2713aSLionel Sambuc Types.push_back(newType);
3056f4a2713aSLionel Sambuc return QualType(newType, 0);
3057f4a2713aSLionel Sambuc }
3058f4a2713aSLionel Sambuc
getEnumType(const EnumDecl * Decl) const3059f4a2713aSLionel Sambuc QualType ASTContext::getEnumType(const EnumDecl *Decl) const {
3060f4a2713aSLionel Sambuc if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
3061f4a2713aSLionel Sambuc
3062f4a2713aSLionel Sambuc if (const EnumDecl *PrevDecl = Decl->getPreviousDecl())
3063f4a2713aSLionel Sambuc if (PrevDecl->TypeForDecl)
3064f4a2713aSLionel Sambuc return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
3065f4a2713aSLionel Sambuc
3066f4a2713aSLionel Sambuc EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
3067f4a2713aSLionel Sambuc Decl->TypeForDecl = newType;
3068f4a2713aSLionel Sambuc Types.push_back(newType);
3069f4a2713aSLionel Sambuc return QualType(newType, 0);
3070f4a2713aSLionel Sambuc }
3071f4a2713aSLionel Sambuc
getAttributedType(AttributedType::Kind attrKind,QualType modifiedType,QualType equivalentType)3072f4a2713aSLionel Sambuc QualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
3073f4a2713aSLionel Sambuc QualType modifiedType,
3074f4a2713aSLionel Sambuc QualType equivalentType) {
3075f4a2713aSLionel Sambuc llvm::FoldingSetNodeID id;
3076f4a2713aSLionel Sambuc AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
3077f4a2713aSLionel Sambuc
3078*0a6a1f1dSLionel Sambuc void *insertPos = nullptr;
3079f4a2713aSLionel Sambuc AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
3080f4a2713aSLionel Sambuc if (type) return QualType(type, 0);
3081f4a2713aSLionel Sambuc
3082f4a2713aSLionel Sambuc QualType canon = getCanonicalType(equivalentType);
3083f4a2713aSLionel Sambuc type = new (*this, TypeAlignment)
3084f4a2713aSLionel Sambuc AttributedType(canon, attrKind, modifiedType, equivalentType);
3085f4a2713aSLionel Sambuc
3086f4a2713aSLionel Sambuc Types.push_back(type);
3087f4a2713aSLionel Sambuc AttributedTypes.InsertNode(type, insertPos);
3088f4a2713aSLionel Sambuc
3089f4a2713aSLionel Sambuc return QualType(type, 0);
3090f4a2713aSLionel Sambuc }
3091f4a2713aSLionel Sambuc
3092f4a2713aSLionel Sambuc
3093f4a2713aSLionel Sambuc /// \brief Retrieve a substitution-result type.
3094f4a2713aSLionel Sambuc QualType
getSubstTemplateTypeParmType(const TemplateTypeParmType * Parm,QualType Replacement) const3095f4a2713aSLionel Sambuc ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
3096f4a2713aSLionel Sambuc QualType Replacement) const {
3097f4a2713aSLionel Sambuc assert(Replacement.isCanonical()
3098f4a2713aSLionel Sambuc && "replacement types must always be canonical");
3099f4a2713aSLionel Sambuc
3100f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3101f4a2713aSLionel Sambuc SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
3102*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3103f4a2713aSLionel Sambuc SubstTemplateTypeParmType *SubstParm
3104f4a2713aSLionel Sambuc = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
3105f4a2713aSLionel Sambuc
3106f4a2713aSLionel Sambuc if (!SubstParm) {
3107f4a2713aSLionel Sambuc SubstParm = new (*this, TypeAlignment)
3108f4a2713aSLionel Sambuc SubstTemplateTypeParmType(Parm, Replacement);
3109f4a2713aSLionel Sambuc Types.push_back(SubstParm);
3110f4a2713aSLionel Sambuc SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
3111f4a2713aSLionel Sambuc }
3112f4a2713aSLionel Sambuc
3113f4a2713aSLionel Sambuc return QualType(SubstParm, 0);
3114f4a2713aSLionel Sambuc }
3115f4a2713aSLionel Sambuc
3116f4a2713aSLionel Sambuc /// \brief Retrieve a
getSubstTemplateTypeParmPackType(const TemplateTypeParmType * Parm,const TemplateArgument & ArgPack)3117f4a2713aSLionel Sambuc QualType ASTContext::getSubstTemplateTypeParmPackType(
3118f4a2713aSLionel Sambuc const TemplateTypeParmType *Parm,
3119f4a2713aSLionel Sambuc const TemplateArgument &ArgPack) {
3120f4a2713aSLionel Sambuc #ifndef NDEBUG
3121*0a6a1f1dSLionel Sambuc for (const auto &P : ArgPack.pack_elements()) {
3122*0a6a1f1dSLionel Sambuc assert(P.getKind() == TemplateArgument::Type &&"Pack contains a non-type");
3123*0a6a1f1dSLionel Sambuc assert(P.getAsType().isCanonical() && "Pack contains non-canonical type");
3124f4a2713aSLionel Sambuc }
3125f4a2713aSLionel Sambuc #endif
3126f4a2713aSLionel Sambuc
3127f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3128f4a2713aSLionel Sambuc SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
3129*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3130f4a2713aSLionel Sambuc if (SubstTemplateTypeParmPackType *SubstParm
3131f4a2713aSLionel Sambuc = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
3132f4a2713aSLionel Sambuc return QualType(SubstParm, 0);
3133f4a2713aSLionel Sambuc
3134f4a2713aSLionel Sambuc QualType Canon;
3135f4a2713aSLionel Sambuc if (!Parm->isCanonicalUnqualified()) {
3136f4a2713aSLionel Sambuc Canon = getCanonicalType(QualType(Parm, 0));
3137f4a2713aSLionel Sambuc Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
3138f4a2713aSLionel Sambuc ArgPack);
3139f4a2713aSLionel Sambuc SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
3140f4a2713aSLionel Sambuc }
3141f4a2713aSLionel Sambuc
3142f4a2713aSLionel Sambuc SubstTemplateTypeParmPackType *SubstParm
3143f4a2713aSLionel Sambuc = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
3144f4a2713aSLionel Sambuc ArgPack);
3145f4a2713aSLionel Sambuc Types.push_back(SubstParm);
3146f4a2713aSLionel Sambuc SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
3147f4a2713aSLionel Sambuc return QualType(SubstParm, 0);
3148f4a2713aSLionel Sambuc }
3149f4a2713aSLionel Sambuc
3150f4a2713aSLionel Sambuc /// \brief Retrieve the template type parameter type for a template
3151f4a2713aSLionel Sambuc /// parameter or parameter pack with the given depth, index, and (optionally)
3152f4a2713aSLionel Sambuc /// name.
getTemplateTypeParmType(unsigned Depth,unsigned Index,bool ParameterPack,TemplateTypeParmDecl * TTPDecl) const3153f4a2713aSLionel Sambuc QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
3154f4a2713aSLionel Sambuc bool ParameterPack,
3155f4a2713aSLionel Sambuc TemplateTypeParmDecl *TTPDecl) const {
3156f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3157f4a2713aSLionel Sambuc TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
3158*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3159f4a2713aSLionel Sambuc TemplateTypeParmType *TypeParm
3160f4a2713aSLionel Sambuc = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
3161f4a2713aSLionel Sambuc
3162f4a2713aSLionel Sambuc if (TypeParm)
3163f4a2713aSLionel Sambuc return QualType(TypeParm, 0);
3164f4a2713aSLionel Sambuc
3165f4a2713aSLionel Sambuc if (TTPDecl) {
3166f4a2713aSLionel Sambuc QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
3167f4a2713aSLionel Sambuc TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
3168f4a2713aSLionel Sambuc
3169f4a2713aSLionel Sambuc TemplateTypeParmType *TypeCheck
3170f4a2713aSLionel Sambuc = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
3171f4a2713aSLionel Sambuc assert(!TypeCheck && "Template type parameter canonical type broken");
3172f4a2713aSLionel Sambuc (void)TypeCheck;
3173f4a2713aSLionel Sambuc } else
3174f4a2713aSLionel Sambuc TypeParm = new (*this, TypeAlignment)
3175f4a2713aSLionel Sambuc TemplateTypeParmType(Depth, Index, ParameterPack);
3176f4a2713aSLionel Sambuc
3177f4a2713aSLionel Sambuc Types.push_back(TypeParm);
3178f4a2713aSLionel Sambuc TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
3179f4a2713aSLionel Sambuc
3180f4a2713aSLionel Sambuc return QualType(TypeParm, 0);
3181f4a2713aSLionel Sambuc }
3182f4a2713aSLionel Sambuc
3183f4a2713aSLionel Sambuc TypeSourceInfo *
getTemplateSpecializationTypeInfo(TemplateName Name,SourceLocation NameLoc,const TemplateArgumentListInfo & Args,QualType Underlying) const3184f4a2713aSLionel Sambuc ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
3185f4a2713aSLionel Sambuc SourceLocation NameLoc,
3186f4a2713aSLionel Sambuc const TemplateArgumentListInfo &Args,
3187f4a2713aSLionel Sambuc QualType Underlying) const {
3188f4a2713aSLionel Sambuc assert(!Name.getAsDependentTemplateName() &&
3189f4a2713aSLionel Sambuc "No dependent template names here!");
3190f4a2713aSLionel Sambuc QualType TST = getTemplateSpecializationType(Name, Args, Underlying);
3191f4a2713aSLionel Sambuc
3192f4a2713aSLionel Sambuc TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
3193f4a2713aSLionel Sambuc TemplateSpecializationTypeLoc TL =
3194f4a2713aSLionel Sambuc DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>();
3195f4a2713aSLionel Sambuc TL.setTemplateKeywordLoc(SourceLocation());
3196f4a2713aSLionel Sambuc TL.setTemplateNameLoc(NameLoc);
3197f4a2713aSLionel Sambuc TL.setLAngleLoc(Args.getLAngleLoc());
3198f4a2713aSLionel Sambuc TL.setRAngleLoc(Args.getRAngleLoc());
3199f4a2713aSLionel Sambuc for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3200f4a2713aSLionel Sambuc TL.setArgLocInfo(i, Args[i].getLocInfo());
3201f4a2713aSLionel Sambuc return DI;
3202f4a2713aSLionel Sambuc }
3203f4a2713aSLionel Sambuc
3204f4a2713aSLionel Sambuc QualType
getTemplateSpecializationType(TemplateName Template,const TemplateArgumentListInfo & Args,QualType Underlying) const3205f4a2713aSLionel Sambuc ASTContext::getTemplateSpecializationType(TemplateName Template,
3206f4a2713aSLionel Sambuc const TemplateArgumentListInfo &Args,
3207f4a2713aSLionel Sambuc QualType Underlying) const {
3208f4a2713aSLionel Sambuc assert(!Template.getAsDependentTemplateName() &&
3209f4a2713aSLionel Sambuc "No dependent template names here!");
3210f4a2713aSLionel Sambuc
3211f4a2713aSLionel Sambuc unsigned NumArgs = Args.size();
3212f4a2713aSLionel Sambuc
3213f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 4> ArgVec;
3214f4a2713aSLionel Sambuc ArgVec.reserve(NumArgs);
3215f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumArgs; ++i)
3216f4a2713aSLionel Sambuc ArgVec.push_back(Args[i].getArgument());
3217f4a2713aSLionel Sambuc
3218f4a2713aSLionel Sambuc return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
3219f4a2713aSLionel Sambuc Underlying);
3220f4a2713aSLionel Sambuc }
3221f4a2713aSLionel Sambuc
3222f4a2713aSLionel Sambuc #ifndef NDEBUG
hasAnyPackExpansions(const TemplateArgument * Args,unsigned NumArgs)3223f4a2713aSLionel Sambuc static bool hasAnyPackExpansions(const TemplateArgument *Args,
3224f4a2713aSLionel Sambuc unsigned NumArgs) {
3225f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumArgs; ++I)
3226f4a2713aSLionel Sambuc if (Args[I].isPackExpansion())
3227f4a2713aSLionel Sambuc return true;
3228f4a2713aSLionel Sambuc
3229f4a2713aSLionel Sambuc return true;
3230f4a2713aSLionel Sambuc }
3231f4a2713aSLionel Sambuc #endif
3232f4a2713aSLionel Sambuc
3233f4a2713aSLionel Sambuc QualType
getTemplateSpecializationType(TemplateName Template,const TemplateArgument * Args,unsigned NumArgs,QualType Underlying) const3234f4a2713aSLionel Sambuc ASTContext::getTemplateSpecializationType(TemplateName Template,
3235f4a2713aSLionel Sambuc const TemplateArgument *Args,
3236f4a2713aSLionel Sambuc unsigned NumArgs,
3237f4a2713aSLionel Sambuc QualType Underlying) const {
3238f4a2713aSLionel Sambuc assert(!Template.getAsDependentTemplateName() &&
3239f4a2713aSLionel Sambuc "No dependent template names here!");
3240f4a2713aSLionel Sambuc // Look through qualified template names.
3241f4a2713aSLionel Sambuc if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
3242f4a2713aSLionel Sambuc Template = TemplateName(QTN->getTemplateDecl());
3243f4a2713aSLionel Sambuc
3244f4a2713aSLionel Sambuc bool IsTypeAlias =
3245f4a2713aSLionel Sambuc Template.getAsTemplateDecl() &&
3246f4a2713aSLionel Sambuc isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
3247f4a2713aSLionel Sambuc QualType CanonType;
3248f4a2713aSLionel Sambuc if (!Underlying.isNull())
3249f4a2713aSLionel Sambuc CanonType = getCanonicalType(Underlying);
3250f4a2713aSLionel Sambuc else {
3251f4a2713aSLionel Sambuc // We can get here with an alias template when the specialization contains
3252f4a2713aSLionel Sambuc // a pack expansion that does not match up with a parameter pack.
3253f4a2713aSLionel Sambuc assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) &&
3254f4a2713aSLionel Sambuc "Caller must compute aliased type");
3255f4a2713aSLionel Sambuc IsTypeAlias = false;
3256f4a2713aSLionel Sambuc CanonType = getCanonicalTemplateSpecializationType(Template, Args,
3257f4a2713aSLionel Sambuc NumArgs);
3258f4a2713aSLionel Sambuc }
3259f4a2713aSLionel Sambuc
3260f4a2713aSLionel Sambuc // Allocate the (non-canonical) template specialization type, but don't
3261f4a2713aSLionel Sambuc // try to unique it: these types typically have location information that
3262f4a2713aSLionel Sambuc // we don't unique and don't want to lose.
3263f4a2713aSLionel Sambuc void *Mem = Allocate(sizeof(TemplateSpecializationType) +
3264f4a2713aSLionel Sambuc sizeof(TemplateArgument) * NumArgs +
3265f4a2713aSLionel Sambuc (IsTypeAlias? sizeof(QualType) : 0),
3266f4a2713aSLionel Sambuc TypeAlignment);
3267f4a2713aSLionel Sambuc TemplateSpecializationType *Spec
3268f4a2713aSLionel Sambuc = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType,
3269f4a2713aSLionel Sambuc IsTypeAlias ? Underlying : QualType());
3270f4a2713aSLionel Sambuc
3271f4a2713aSLionel Sambuc Types.push_back(Spec);
3272f4a2713aSLionel Sambuc return QualType(Spec, 0);
3273f4a2713aSLionel Sambuc }
3274f4a2713aSLionel Sambuc
3275f4a2713aSLionel Sambuc QualType
getCanonicalTemplateSpecializationType(TemplateName Template,const TemplateArgument * Args,unsigned NumArgs) const3276f4a2713aSLionel Sambuc ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
3277f4a2713aSLionel Sambuc const TemplateArgument *Args,
3278f4a2713aSLionel Sambuc unsigned NumArgs) const {
3279f4a2713aSLionel Sambuc assert(!Template.getAsDependentTemplateName() &&
3280f4a2713aSLionel Sambuc "No dependent template names here!");
3281f4a2713aSLionel Sambuc
3282f4a2713aSLionel Sambuc // Look through qualified template names.
3283f4a2713aSLionel Sambuc if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
3284f4a2713aSLionel Sambuc Template = TemplateName(QTN->getTemplateDecl());
3285f4a2713aSLionel Sambuc
3286f4a2713aSLionel Sambuc // Build the canonical template specialization type.
3287f4a2713aSLionel Sambuc TemplateName CanonTemplate = getCanonicalTemplateName(Template);
3288f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 4> CanonArgs;
3289f4a2713aSLionel Sambuc CanonArgs.reserve(NumArgs);
3290f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumArgs; ++I)
3291f4a2713aSLionel Sambuc CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
3292f4a2713aSLionel Sambuc
3293f4a2713aSLionel Sambuc // Determine whether this canonical template specialization type already
3294f4a2713aSLionel Sambuc // exists.
3295f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3296f4a2713aSLionel Sambuc TemplateSpecializationType::Profile(ID, CanonTemplate,
3297f4a2713aSLionel Sambuc CanonArgs.data(), NumArgs, *this);
3298f4a2713aSLionel Sambuc
3299*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3300f4a2713aSLionel Sambuc TemplateSpecializationType *Spec
3301f4a2713aSLionel Sambuc = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
3302f4a2713aSLionel Sambuc
3303f4a2713aSLionel Sambuc if (!Spec) {
3304f4a2713aSLionel Sambuc // Allocate a new canonical template specialization type.
3305f4a2713aSLionel Sambuc void *Mem = Allocate((sizeof(TemplateSpecializationType) +
3306f4a2713aSLionel Sambuc sizeof(TemplateArgument) * NumArgs),
3307f4a2713aSLionel Sambuc TypeAlignment);
3308f4a2713aSLionel Sambuc Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
3309f4a2713aSLionel Sambuc CanonArgs.data(), NumArgs,
3310f4a2713aSLionel Sambuc QualType(), QualType());
3311f4a2713aSLionel Sambuc Types.push_back(Spec);
3312f4a2713aSLionel Sambuc TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
3313f4a2713aSLionel Sambuc }
3314f4a2713aSLionel Sambuc
3315f4a2713aSLionel Sambuc assert(Spec->isDependentType() &&
3316f4a2713aSLionel Sambuc "Non-dependent template-id type must have a canonical type");
3317f4a2713aSLionel Sambuc return QualType(Spec, 0);
3318f4a2713aSLionel Sambuc }
3319f4a2713aSLionel Sambuc
3320f4a2713aSLionel Sambuc QualType
getElaboratedType(ElaboratedTypeKeyword Keyword,NestedNameSpecifier * NNS,QualType NamedType) const3321f4a2713aSLionel Sambuc ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
3322f4a2713aSLionel Sambuc NestedNameSpecifier *NNS,
3323f4a2713aSLionel Sambuc QualType NamedType) const {
3324f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3325f4a2713aSLionel Sambuc ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
3326f4a2713aSLionel Sambuc
3327*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3328f4a2713aSLionel Sambuc ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
3329f4a2713aSLionel Sambuc if (T)
3330f4a2713aSLionel Sambuc return QualType(T, 0);
3331f4a2713aSLionel Sambuc
3332f4a2713aSLionel Sambuc QualType Canon = NamedType;
3333f4a2713aSLionel Sambuc if (!Canon.isCanonical()) {
3334f4a2713aSLionel Sambuc Canon = getCanonicalType(NamedType);
3335f4a2713aSLionel Sambuc ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
3336f4a2713aSLionel Sambuc assert(!CheckT && "Elaborated canonical type broken");
3337f4a2713aSLionel Sambuc (void)CheckT;
3338f4a2713aSLionel Sambuc }
3339f4a2713aSLionel Sambuc
3340f4a2713aSLionel Sambuc T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
3341f4a2713aSLionel Sambuc Types.push_back(T);
3342f4a2713aSLionel Sambuc ElaboratedTypes.InsertNode(T, InsertPos);
3343f4a2713aSLionel Sambuc return QualType(T, 0);
3344f4a2713aSLionel Sambuc }
3345f4a2713aSLionel Sambuc
3346f4a2713aSLionel Sambuc QualType
getParenType(QualType InnerType) const3347f4a2713aSLionel Sambuc ASTContext::getParenType(QualType InnerType) const {
3348f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3349f4a2713aSLionel Sambuc ParenType::Profile(ID, InnerType);
3350f4a2713aSLionel Sambuc
3351*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3352f4a2713aSLionel Sambuc ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
3353f4a2713aSLionel Sambuc if (T)
3354f4a2713aSLionel Sambuc return QualType(T, 0);
3355f4a2713aSLionel Sambuc
3356f4a2713aSLionel Sambuc QualType Canon = InnerType;
3357f4a2713aSLionel Sambuc if (!Canon.isCanonical()) {
3358f4a2713aSLionel Sambuc Canon = getCanonicalType(InnerType);
3359f4a2713aSLionel Sambuc ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
3360f4a2713aSLionel Sambuc assert(!CheckT && "Paren canonical type broken");
3361f4a2713aSLionel Sambuc (void)CheckT;
3362f4a2713aSLionel Sambuc }
3363f4a2713aSLionel Sambuc
3364f4a2713aSLionel Sambuc T = new (*this) ParenType(InnerType, Canon);
3365f4a2713aSLionel Sambuc Types.push_back(T);
3366f4a2713aSLionel Sambuc ParenTypes.InsertNode(T, InsertPos);
3367f4a2713aSLionel Sambuc return QualType(T, 0);
3368f4a2713aSLionel Sambuc }
3369f4a2713aSLionel Sambuc
getDependentNameType(ElaboratedTypeKeyword Keyword,NestedNameSpecifier * NNS,const IdentifierInfo * Name,QualType Canon) const3370f4a2713aSLionel Sambuc QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
3371f4a2713aSLionel Sambuc NestedNameSpecifier *NNS,
3372f4a2713aSLionel Sambuc const IdentifierInfo *Name,
3373f4a2713aSLionel Sambuc QualType Canon) const {
3374f4a2713aSLionel Sambuc if (Canon.isNull()) {
3375f4a2713aSLionel Sambuc NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3376f4a2713aSLionel Sambuc ElaboratedTypeKeyword CanonKeyword = Keyword;
3377f4a2713aSLionel Sambuc if (Keyword == ETK_None)
3378f4a2713aSLionel Sambuc CanonKeyword = ETK_Typename;
3379f4a2713aSLionel Sambuc
3380f4a2713aSLionel Sambuc if (CanonNNS != NNS || CanonKeyword != Keyword)
3381f4a2713aSLionel Sambuc Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
3382f4a2713aSLionel Sambuc }
3383f4a2713aSLionel Sambuc
3384f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3385f4a2713aSLionel Sambuc DependentNameType::Profile(ID, Keyword, NNS, Name);
3386f4a2713aSLionel Sambuc
3387*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3388f4a2713aSLionel Sambuc DependentNameType *T
3389f4a2713aSLionel Sambuc = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
3390f4a2713aSLionel Sambuc if (T)
3391f4a2713aSLionel Sambuc return QualType(T, 0);
3392f4a2713aSLionel Sambuc
3393f4a2713aSLionel Sambuc T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
3394f4a2713aSLionel Sambuc Types.push_back(T);
3395f4a2713aSLionel Sambuc DependentNameTypes.InsertNode(T, InsertPos);
3396f4a2713aSLionel Sambuc return QualType(T, 0);
3397f4a2713aSLionel Sambuc }
3398f4a2713aSLionel Sambuc
3399f4a2713aSLionel Sambuc QualType
getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword,NestedNameSpecifier * NNS,const IdentifierInfo * Name,const TemplateArgumentListInfo & Args) const3400f4a2713aSLionel Sambuc ASTContext::getDependentTemplateSpecializationType(
3401f4a2713aSLionel Sambuc ElaboratedTypeKeyword Keyword,
3402f4a2713aSLionel Sambuc NestedNameSpecifier *NNS,
3403f4a2713aSLionel Sambuc const IdentifierInfo *Name,
3404f4a2713aSLionel Sambuc const TemplateArgumentListInfo &Args) const {
3405f4a2713aSLionel Sambuc // TODO: avoid this copy
3406f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 16> ArgCopy;
3407f4a2713aSLionel Sambuc for (unsigned I = 0, E = Args.size(); I != E; ++I)
3408f4a2713aSLionel Sambuc ArgCopy.push_back(Args[I].getArgument());
3409f4a2713aSLionel Sambuc return getDependentTemplateSpecializationType(Keyword, NNS, Name,
3410f4a2713aSLionel Sambuc ArgCopy.size(),
3411f4a2713aSLionel Sambuc ArgCopy.data());
3412f4a2713aSLionel Sambuc }
3413f4a2713aSLionel Sambuc
3414f4a2713aSLionel Sambuc QualType
getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword,NestedNameSpecifier * NNS,const IdentifierInfo * Name,unsigned NumArgs,const TemplateArgument * Args) const3415f4a2713aSLionel Sambuc ASTContext::getDependentTemplateSpecializationType(
3416f4a2713aSLionel Sambuc ElaboratedTypeKeyword Keyword,
3417f4a2713aSLionel Sambuc NestedNameSpecifier *NNS,
3418f4a2713aSLionel Sambuc const IdentifierInfo *Name,
3419f4a2713aSLionel Sambuc unsigned NumArgs,
3420f4a2713aSLionel Sambuc const TemplateArgument *Args) const {
3421f4a2713aSLionel Sambuc assert((!NNS || NNS->isDependent()) &&
3422f4a2713aSLionel Sambuc "nested-name-specifier must be dependent");
3423f4a2713aSLionel Sambuc
3424f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3425f4a2713aSLionel Sambuc DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
3426f4a2713aSLionel Sambuc Name, NumArgs, Args);
3427f4a2713aSLionel Sambuc
3428*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3429f4a2713aSLionel Sambuc DependentTemplateSpecializationType *T
3430f4a2713aSLionel Sambuc = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
3431f4a2713aSLionel Sambuc if (T)
3432f4a2713aSLionel Sambuc return QualType(T, 0);
3433f4a2713aSLionel Sambuc
3434f4a2713aSLionel Sambuc NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3435f4a2713aSLionel Sambuc
3436f4a2713aSLionel Sambuc ElaboratedTypeKeyword CanonKeyword = Keyword;
3437f4a2713aSLionel Sambuc if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
3438f4a2713aSLionel Sambuc
3439f4a2713aSLionel Sambuc bool AnyNonCanonArgs = false;
3440f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
3441f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumArgs; ++I) {
3442f4a2713aSLionel Sambuc CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
3443f4a2713aSLionel Sambuc if (!CanonArgs[I].structurallyEquals(Args[I]))
3444f4a2713aSLionel Sambuc AnyNonCanonArgs = true;
3445f4a2713aSLionel Sambuc }
3446f4a2713aSLionel Sambuc
3447f4a2713aSLionel Sambuc QualType Canon;
3448f4a2713aSLionel Sambuc if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
3449f4a2713aSLionel Sambuc Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
3450f4a2713aSLionel Sambuc Name, NumArgs,
3451f4a2713aSLionel Sambuc CanonArgs.data());
3452f4a2713aSLionel Sambuc
3453f4a2713aSLionel Sambuc // Find the insert position again.
3454f4a2713aSLionel Sambuc DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
3455f4a2713aSLionel Sambuc }
3456f4a2713aSLionel Sambuc
3457f4a2713aSLionel Sambuc void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
3458f4a2713aSLionel Sambuc sizeof(TemplateArgument) * NumArgs),
3459f4a2713aSLionel Sambuc TypeAlignment);
3460f4a2713aSLionel Sambuc T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
3461f4a2713aSLionel Sambuc Name, NumArgs, Args, Canon);
3462f4a2713aSLionel Sambuc Types.push_back(T);
3463f4a2713aSLionel Sambuc DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
3464f4a2713aSLionel Sambuc return QualType(T, 0);
3465f4a2713aSLionel Sambuc }
3466f4a2713aSLionel Sambuc
getPackExpansionType(QualType Pattern,Optional<unsigned> NumExpansions)3467f4a2713aSLionel Sambuc QualType ASTContext::getPackExpansionType(QualType Pattern,
3468f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions) {
3469f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3470f4a2713aSLionel Sambuc PackExpansionType::Profile(ID, Pattern, NumExpansions);
3471f4a2713aSLionel Sambuc
3472f4a2713aSLionel Sambuc assert(Pattern->containsUnexpandedParameterPack() &&
3473f4a2713aSLionel Sambuc "Pack expansions must expand one or more parameter packs");
3474*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3475f4a2713aSLionel Sambuc PackExpansionType *T
3476f4a2713aSLionel Sambuc = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
3477f4a2713aSLionel Sambuc if (T)
3478f4a2713aSLionel Sambuc return QualType(T, 0);
3479f4a2713aSLionel Sambuc
3480f4a2713aSLionel Sambuc QualType Canon;
3481f4a2713aSLionel Sambuc if (!Pattern.isCanonical()) {
3482f4a2713aSLionel Sambuc Canon = getCanonicalType(Pattern);
3483f4a2713aSLionel Sambuc // The canonical type might not contain an unexpanded parameter pack, if it
3484f4a2713aSLionel Sambuc // contains an alias template specialization which ignores one of its
3485f4a2713aSLionel Sambuc // parameters.
3486f4a2713aSLionel Sambuc if (Canon->containsUnexpandedParameterPack()) {
3487*0a6a1f1dSLionel Sambuc Canon = getPackExpansionType(Canon, NumExpansions);
3488f4a2713aSLionel Sambuc
3489f4a2713aSLionel Sambuc // Find the insert position again, in case we inserted an element into
3490f4a2713aSLionel Sambuc // PackExpansionTypes and invalidated our insert position.
3491f4a2713aSLionel Sambuc PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
3492f4a2713aSLionel Sambuc }
3493f4a2713aSLionel Sambuc }
3494f4a2713aSLionel Sambuc
3495f4a2713aSLionel Sambuc T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
3496f4a2713aSLionel Sambuc Types.push_back(T);
3497f4a2713aSLionel Sambuc PackExpansionTypes.InsertNode(T, InsertPos);
3498f4a2713aSLionel Sambuc return QualType(T, 0);
3499f4a2713aSLionel Sambuc }
3500f4a2713aSLionel Sambuc
3501f4a2713aSLionel Sambuc /// CmpProtocolNames - Comparison predicate for sorting protocols
3502f4a2713aSLionel Sambuc /// alphabetically.
CmpProtocolNames(const ObjCProtocolDecl * LHS,const ObjCProtocolDecl * RHS)3503f4a2713aSLionel Sambuc static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
3504f4a2713aSLionel Sambuc const ObjCProtocolDecl *RHS) {
3505f4a2713aSLionel Sambuc return LHS->getDeclName() < RHS->getDeclName();
3506f4a2713aSLionel Sambuc }
3507f4a2713aSLionel Sambuc
areSortedAndUniqued(ObjCProtocolDecl * const * Protocols,unsigned NumProtocols)3508f4a2713aSLionel Sambuc static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
3509f4a2713aSLionel Sambuc unsigned NumProtocols) {
3510f4a2713aSLionel Sambuc if (NumProtocols == 0) return true;
3511f4a2713aSLionel Sambuc
3512f4a2713aSLionel Sambuc if (Protocols[0]->getCanonicalDecl() != Protocols[0])
3513f4a2713aSLionel Sambuc return false;
3514f4a2713aSLionel Sambuc
3515f4a2713aSLionel Sambuc for (unsigned i = 1; i != NumProtocols; ++i)
3516f4a2713aSLionel Sambuc if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) ||
3517f4a2713aSLionel Sambuc Protocols[i]->getCanonicalDecl() != Protocols[i])
3518f4a2713aSLionel Sambuc return false;
3519f4a2713aSLionel Sambuc return true;
3520f4a2713aSLionel Sambuc }
3521f4a2713aSLionel Sambuc
SortAndUniqueProtocols(ObjCProtocolDecl ** Protocols,unsigned & NumProtocols)3522f4a2713aSLionel Sambuc static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
3523f4a2713aSLionel Sambuc unsigned &NumProtocols) {
3524f4a2713aSLionel Sambuc ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
3525f4a2713aSLionel Sambuc
3526f4a2713aSLionel Sambuc // Sort protocols, keyed by name.
3527f4a2713aSLionel Sambuc std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
3528f4a2713aSLionel Sambuc
3529f4a2713aSLionel Sambuc // Canonicalize.
3530f4a2713aSLionel Sambuc for (unsigned I = 0, N = NumProtocols; I != N; ++I)
3531f4a2713aSLionel Sambuc Protocols[I] = Protocols[I]->getCanonicalDecl();
3532f4a2713aSLionel Sambuc
3533f4a2713aSLionel Sambuc // Remove duplicates.
3534f4a2713aSLionel Sambuc ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
3535f4a2713aSLionel Sambuc NumProtocols = ProtocolsEnd-Protocols;
3536f4a2713aSLionel Sambuc }
3537f4a2713aSLionel Sambuc
getObjCObjectType(QualType BaseType,ObjCProtocolDecl * const * Protocols,unsigned NumProtocols) const3538f4a2713aSLionel Sambuc QualType ASTContext::getObjCObjectType(QualType BaseType,
3539f4a2713aSLionel Sambuc ObjCProtocolDecl * const *Protocols,
3540f4a2713aSLionel Sambuc unsigned NumProtocols) const {
3541f4a2713aSLionel Sambuc // If the base type is an interface and there aren't any protocols
3542f4a2713aSLionel Sambuc // to add, then the interface type will do just fine.
3543f4a2713aSLionel Sambuc if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
3544f4a2713aSLionel Sambuc return BaseType;
3545f4a2713aSLionel Sambuc
3546f4a2713aSLionel Sambuc // Look in the folding set for an existing type.
3547f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3548f4a2713aSLionel Sambuc ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
3549*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3550f4a2713aSLionel Sambuc if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
3551f4a2713aSLionel Sambuc return QualType(QT, 0);
3552f4a2713aSLionel Sambuc
3553f4a2713aSLionel Sambuc // Build the canonical type, which has the canonical base type and
3554f4a2713aSLionel Sambuc // a sorted-and-uniqued list of protocols.
3555f4a2713aSLionel Sambuc QualType Canonical;
3556f4a2713aSLionel Sambuc bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
3557f4a2713aSLionel Sambuc if (!ProtocolsSorted || !BaseType.isCanonical()) {
3558f4a2713aSLionel Sambuc if (!ProtocolsSorted) {
3559f4a2713aSLionel Sambuc SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
3560f4a2713aSLionel Sambuc Protocols + NumProtocols);
3561f4a2713aSLionel Sambuc unsigned UniqueCount = NumProtocols;
3562f4a2713aSLionel Sambuc
3563f4a2713aSLionel Sambuc SortAndUniqueProtocols(&Sorted[0], UniqueCount);
3564f4a2713aSLionel Sambuc Canonical = getObjCObjectType(getCanonicalType(BaseType),
3565f4a2713aSLionel Sambuc &Sorted[0], UniqueCount);
3566f4a2713aSLionel Sambuc } else {
3567f4a2713aSLionel Sambuc Canonical = getObjCObjectType(getCanonicalType(BaseType),
3568f4a2713aSLionel Sambuc Protocols, NumProtocols);
3569f4a2713aSLionel Sambuc }
3570f4a2713aSLionel Sambuc
3571f4a2713aSLionel Sambuc // Regenerate InsertPos.
3572f4a2713aSLionel Sambuc ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
3573f4a2713aSLionel Sambuc }
3574f4a2713aSLionel Sambuc
3575f4a2713aSLionel Sambuc unsigned Size = sizeof(ObjCObjectTypeImpl);
3576f4a2713aSLionel Sambuc Size += NumProtocols * sizeof(ObjCProtocolDecl *);
3577f4a2713aSLionel Sambuc void *Mem = Allocate(Size, TypeAlignment);
3578f4a2713aSLionel Sambuc ObjCObjectTypeImpl *T =
3579f4a2713aSLionel Sambuc new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
3580f4a2713aSLionel Sambuc
3581f4a2713aSLionel Sambuc Types.push_back(T);
3582f4a2713aSLionel Sambuc ObjCObjectTypes.InsertNode(T, InsertPos);
3583f4a2713aSLionel Sambuc return QualType(T, 0);
3584f4a2713aSLionel Sambuc }
3585f4a2713aSLionel Sambuc
3586*0a6a1f1dSLionel Sambuc /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's
3587*0a6a1f1dSLionel Sambuc /// protocol list adopt all protocols in QT's qualified-id protocol
3588*0a6a1f1dSLionel Sambuc /// list.
ObjCObjectAdoptsQTypeProtocols(QualType QT,ObjCInterfaceDecl * IC)3589*0a6a1f1dSLionel Sambuc bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT,
3590*0a6a1f1dSLionel Sambuc ObjCInterfaceDecl *IC) {
3591*0a6a1f1dSLionel Sambuc if (!QT->isObjCQualifiedIdType())
3592*0a6a1f1dSLionel Sambuc return false;
3593*0a6a1f1dSLionel Sambuc
3594*0a6a1f1dSLionel Sambuc if (const ObjCObjectPointerType *OPT = QT->getAs<ObjCObjectPointerType>()) {
3595*0a6a1f1dSLionel Sambuc // If both the right and left sides have qualifiers.
3596*0a6a1f1dSLionel Sambuc for (auto *Proto : OPT->quals()) {
3597*0a6a1f1dSLionel Sambuc if (!IC->ClassImplementsProtocol(Proto, false))
3598*0a6a1f1dSLionel Sambuc return false;
3599*0a6a1f1dSLionel Sambuc }
3600*0a6a1f1dSLionel Sambuc return true;
3601*0a6a1f1dSLionel Sambuc }
3602*0a6a1f1dSLionel Sambuc return false;
3603*0a6a1f1dSLionel Sambuc }
3604*0a6a1f1dSLionel Sambuc
3605*0a6a1f1dSLionel Sambuc /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in
3606*0a6a1f1dSLionel Sambuc /// QT's qualified-id protocol list adopt all protocols in IDecl's list
3607*0a6a1f1dSLionel Sambuc /// of protocols.
QIdProtocolsAdoptObjCObjectProtocols(QualType QT,ObjCInterfaceDecl * IDecl)3608*0a6a1f1dSLionel Sambuc bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT,
3609*0a6a1f1dSLionel Sambuc ObjCInterfaceDecl *IDecl) {
3610*0a6a1f1dSLionel Sambuc if (!QT->isObjCQualifiedIdType())
3611*0a6a1f1dSLionel Sambuc return false;
3612*0a6a1f1dSLionel Sambuc const ObjCObjectPointerType *OPT = QT->getAs<ObjCObjectPointerType>();
3613*0a6a1f1dSLionel Sambuc if (!OPT)
3614*0a6a1f1dSLionel Sambuc return false;
3615*0a6a1f1dSLionel Sambuc if (!IDecl->hasDefinition())
3616*0a6a1f1dSLionel Sambuc return false;
3617*0a6a1f1dSLionel Sambuc llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols;
3618*0a6a1f1dSLionel Sambuc CollectInheritedProtocols(IDecl, InheritedProtocols);
3619*0a6a1f1dSLionel Sambuc if (InheritedProtocols.empty())
3620*0a6a1f1dSLionel Sambuc return false;
3621*0a6a1f1dSLionel Sambuc // Check that if every protocol in list of id<plist> conforms to a protcol
3622*0a6a1f1dSLionel Sambuc // of IDecl's, then bridge casting is ok.
3623*0a6a1f1dSLionel Sambuc bool Conforms = false;
3624*0a6a1f1dSLionel Sambuc for (auto *Proto : OPT->quals()) {
3625*0a6a1f1dSLionel Sambuc Conforms = false;
3626*0a6a1f1dSLionel Sambuc for (auto *PI : InheritedProtocols) {
3627*0a6a1f1dSLionel Sambuc if (ProtocolCompatibleWithProtocol(Proto, PI)) {
3628*0a6a1f1dSLionel Sambuc Conforms = true;
3629*0a6a1f1dSLionel Sambuc break;
3630*0a6a1f1dSLionel Sambuc }
3631*0a6a1f1dSLionel Sambuc }
3632*0a6a1f1dSLionel Sambuc if (!Conforms)
3633*0a6a1f1dSLionel Sambuc break;
3634*0a6a1f1dSLionel Sambuc }
3635*0a6a1f1dSLionel Sambuc if (Conforms)
3636*0a6a1f1dSLionel Sambuc return true;
3637*0a6a1f1dSLionel Sambuc
3638*0a6a1f1dSLionel Sambuc for (auto *PI : InheritedProtocols) {
3639*0a6a1f1dSLionel Sambuc // If both the right and left sides have qualifiers.
3640*0a6a1f1dSLionel Sambuc bool Adopts = false;
3641*0a6a1f1dSLionel Sambuc for (auto *Proto : OPT->quals()) {
3642*0a6a1f1dSLionel Sambuc // return 'true' if 'PI' is in the inheritance hierarchy of Proto
3643*0a6a1f1dSLionel Sambuc if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto)))
3644*0a6a1f1dSLionel Sambuc break;
3645*0a6a1f1dSLionel Sambuc }
3646*0a6a1f1dSLionel Sambuc if (!Adopts)
3647*0a6a1f1dSLionel Sambuc return false;
3648*0a6a1f1dSLionel Sambuc }
3649*0a6a1f1dSLionel Sambuc return true;
3650*0a6a1f1dSLionel Sambuc }
3651*0a6a1f1dSLionel Sambuc
3652f4a2713aSLionel Sambuc /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
3653f4a2713aSLionel Sambuc /// the given object type.
getObjCObjectPointerType(QualType ObjectT) const3654f4a2713aSLionel Sambuc QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
3655f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3656f4a2713aSLionel Sambuc ObjCObjectPointerType::Profile(ID, ObjectT);
3657f4a2713aSLionel Sambuc
3658*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3659f4a2713aSLionel Sambuc if (ObjCObjectPointerType *QT =
3660f4a2713aSLionel Sambuc ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
3661f4a2713aSLionel Sambuc return QualType(QT, 0);
3662f4a2713aSLionel Sambuc
3663f4a2713aSLionel Sambuc // Find the canonical object type.
3664f4a2713aSLionel Sambuc QualType Canonical;
3665f4a2713aSLionel Sambuc if (!ObjectT.isCanonical()) {
3666f4a2713aSLionel Sambuc Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
3667f4a2713aSLionel Sambuc
3668f4a2713aSLionel Sambuc // Regenerate InsertPos.
3669f4a2713aSLionel Sambuc ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
3670f4a2713aSLionel Sambuc }
3671f4a2713aSLionel Sambuc
3672f4a2713aSLionel Sambuc // No match.
3673f4a2713aSLionel Sambuc void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
3674f4a2713aSLionel Sambuc ObjCObjectPointerType *QType =
3675f4a2713aSLionel Sambuc new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
3676f4a2713aSLionel Sambuc
3677f4a2713aSLionel Sambuc Types.push_back(QType);
3678f4a2713aSLionel Sambuc ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
3679f4a2713aSLionel Sambuc return QualType(QType, 0);
3680f4a2713aSLionel Sambuc }
3681f4a2713aSLionel Sambuc
3682f4a2713aSLionel Sambuc /// getObjCInterfaceType - Return the unique reference to the type for the
3683f4a2713aSLionel Sambuc /// specified ObjC interface decl. The list of protocols is optional.
getObjCInterfaceType(const ObjCInterfaceDecl * Decl,ObjCInterfaceDecl * PrevDecl) const3684f4a2713aSLionel Sambuc QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
3685f4a2713aSLionel Sambuc ObjCInterfaceDecl *PrevDecl) const {
3686f4a2713aSLionel Sambuc if (Decl->TypeForDecl)
3687f4a2713aSLionel Sambuc return QualType(Decl->TypeForDecl, 0);
3688f4a2713aSLionel Sambuc
3689f4a2713aSLionel Sambuc if (PrevDecl) {
3690f4a2713aSLionel Sambuc assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl");
3691f4a2713aSLionel Sambuc Decl->TypeForDecl = PrevDecl->TypeForDecl;
3692f4a2713aSLionel Sambuc return QualType(PrevDecl->TypeForDecl, 0);
3693f4a2713aSLionel Sambuc }
3694f4a2713aSLionel Sambuc
3695f4a2713aSLionel Sambuc // Prefer the definition, if there is one.
3696f4a2713aSLionel Sambuc if (const ObjCInterfaceDecl *Def = Decl->getDefinition())
3697f4a2713aSLionel Sambuc Decl = Def;
3698f4a2713aSLionel Sambuc
3699f4a2713aSLionel Sambuc void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
3700f4a2713aSLionel Sambuc ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
3701f4a2713aSLionel Sambuc Decl->TypeForDecl = T;
3702f4a2713aSLionel Sambuc Types.push_back(T);
3703f4a2713aSLionel Sambuc return QualType(T, 0);
3704f4a2713aSLionel Sambuc }
3705f4a2713aSLionel Sambuc
3706f4a2713aSLionel Sambuc /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
3707f4a2713aSLionel Sambuc /// TypeOfExprType AST's (since expression's are never shared). For example,
3708f4a2713aSLionel Sambuc /// multiple declarations that refer to "typeof(x)" all contain different
3709f4a2713aSLionel Sambuc /// DeclRefExpr's. This doesn't effect the type checker, since it operates
3710f4a2713aSLionel Sambuc /// on canonical type's (which are always unique).
getTypeOfExprType(Expr * tofExpr) const3711f4a2713aSLionel Sambuc QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
3712f4a2713aSLionel Sambuc TypeOfExprType *toe;
3713f4a2713aSLionel Sambuc if (tofExpr->isTypeDependent()) {
3714f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3715f4a2713aSLionel Sambuc DependentTypeOfExprType::Profile(ID, *this, tofExpr);
3716f4a2713aSLionel Sambuc
3717*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3718f4a2713aSLionel Sambuc DependentTypeOfExprType *Canon
3719f4a2713aSLionel Sambuc = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
3720f4a2713aSLionel Sambuc if (Canon) {
3721f4a2713aSLionel Sambuc // We already have a "canonical" version of an identical, dependent
3722f4a2713aSLionel Sambuc // typeof(expr) type. Use that as our canonical type.
3723f4a2713aSLionel Sambuc toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
3724f4a2713aSLionel Sambuc QualType((TypeOfExprType*)Canon, 0));
3725f4a2713aSLionel Sambuc } else {
3726f4a2713aSLionel Sambuc // Build a new, canonical typeof(expr) type.
3727f4a2713aSLionel Sambuc Canon
3728f4a2713aSLionel Sambuc = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
3729f4a2713aSLionel Sambuc DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
3730f4a2713aSLionel Sambuc toe = Canon;
3731f4a2713aSLionel Sambuc }
3732f4a2713aSLionel Sambuc } else {
3733f4a2713aSLionel Sambuc QualType Canonical = getCanonicalType(tofExpr->getType());
3734f4a2713aSLionel Sambuc toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
3735f4a2713aSLionel Sambuc }
3736f4a2713aSLionel Sambuc Types.push_back(toe);
3737f4a2713aSLionel Sambuc return QualType(toe, 0);
3738f4a2713aSLionel Sambuc }
3739f4a2713aSLionel Sambuc
3740f4a2713aSLionel Sambuc /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
3741*0a6a1f1dSLionel Sambuc /// TypeOfType nodes. The only motivation to unique these nodes would be
3742f4a2713aSLionel Sambuc /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
3743*0a6a1f1dSLionel Sambuc /// an issue. This doesn't affect the type checker, since it operates
3744*0a6a1f1dSLionel Sambuc /// on canonical types (which are always unique).
getTypeOfType(QualType tofType) const3745f4a2713aSLionel Sambuc QualType ASTContext::getTypeOfType(QualType tofType) const {
3746f4a2713aSLionel Sambuc QualType Canonical = getCanonicalType(tofType);
3747f4a2713aSLionel Sambuc TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
3748f4a2713aSLionel Sambuc Types.push_back(tot);
3749f4a2713aSLionel Sambuc return QualType(tot, 0);
3750f4a2713aSLionel Sambuc }
3751f4a2713aSLionel Sambuc
3752f4a2713aSLionel Sambuc
3753*0a6a1f1dSLionel Sambuc /// \brief Unlike many "get<Type>" functions, we don't unique DecltypeType
3754*0a6a1f1dSLionel Sambuc /// nodes. This would never be helpful, since each such type has its own
3755*0a6a1f1dSLionel Sambuc /// expression, and would not give a significant memory saving, since there
3756*0a6a1f1dSLionel Sambuc /// is an Expr tree under each such type.
getDecltypeType(Expr * e,QualType UnderlyingType) const3757f4a2713aSLionel Sambuc QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const {
3758f4a2713aSLionel Sambuc DecltypeType *dt;
3759f4a2713aSLionel Sambuc
3760*0a6a1f1dSLionel Sambuc // C++11 [temp.type]p2:
3761f4a2713aSLionel Sambuc // If an expression e involves a template parameter, decltype(e) denotes a
3762f4a2713aSLionel Sambuc // unique dependent type. Two such decltype-specifiers refer to the same
3763f4a2713aSLionel Sambuc // type only if their expressions are equivalent (14.5.6.1).
3764f4a2713aSLionel Sambuc if (e->isInstantiationDependent()) {
3765f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3766f4a2713aSLionel Sambuc DependentDecltypeType::Profile(ID, *this, e);
3767f4a2713aSLionel Sambuc
3768*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3769f4a2713aSLionel Sambuc DependentDecltypeType *Canon
3770f4a2713aSLionel Sambuc = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
3771*0a6a1f1dSLionel Sambuc if (!Canon) {
3772f4a2713aSLionel Sambuc // Build a new, canonical typeof(expr) type.
3773f4a2713aSLionel Sambuc Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
3774f4a2713aSLionel Sambuc DependentDecltypeTypes.InsertNode(Canon, InsertPos);
3775f4a2713aSLionel Sambuc }
3776*0a6a1f1dSLionel Sambuc dt = new (*this, TypeAlignment)
3777*0a6a1f1dSLionel Sambuc DecltypeType(e, UnderlyingType, QualType((DecltypeType *)Canon, 0));
3778f4a2713aSLionel Sambuc } else {
3779*0a6a1f1dSLionel Sambuc dt = new (*this, TypeAlignment)
3780*0a6a1f1dSLionel Sambuc DecltypeType(e, UnderlyingType, getCanonicalType(UnderlyingType));
3781f4a2713aSLionel Sambuc }
3782f4a2713aSLionel Sambuc Types.push_back(dt);
3783f4a2713aSLionel Sambuc return QualType(dt, 0);
3784f4a2713aSLionel Sambuc }
3785f4a2713aSLionel Sambuc
3786f4a2713aSLionel Sambuc /// getUnaryTransformationType - We don't unique these, since the memory
3787f4a2713aSLionel Sambuc /// savings are minimal and these are rare.
getUnaryTransformType(QualType BaseType,QualType UnderlyingType,UnaryTransformType::UTTKind Kind) const3788f4a2713aSLionel Sambuc QualType ASTContext::getUnaryTransformType(QualType BaseType,
3789f4a2713aSLionel Sambuc QualType UnderlyingType,
3790f4a2713aSLionel Sambuc UnaryTransformType::UTTKind Kind)
3791f4a2713aSLionel Sambuc const {
3792f4a2713aSLionel Sambuc UnaryTransformType *Ty =
3793f4a2713aSLionel Sambuc new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType,
3794f4a2713aSLionel Sambuc Kind,
3795f4a2713aSLionel Sambuc UnderlyingType->isDependentType() ?
3796f4a2713aSLionel Sambuc QualType() : getCanonicalType(UnderlyingType));
3797f4a2713aSLionel Sambuc Types.push_back(Ty);
3798f4a2713aSLionel Sambuc return QualType(Ty, 0);
3799f4a2713aSLionel Sambuc }
3800f4a2713aSLionel Sambuc
3801f4a2713aSLionel Sambuc /// getAutoType - Return the uniqued reference to the 'auto' type which has been
3802f4a2713aSLionel Sambuc /// deduced to the given type, or to the canonical undeduced 'auto' type, or the
3803f4a2713aSLionel Sambuc /// canonical deduced-but-dependent 'auto' type.
getAutoType(QualType DeducedType,bool IsDecltypeAuto,bool IsDependent) const3804f4a2713aSLionel Sambuc QualType ASTContext::getAutoType(QualType DeducedType, bool IsDecltypeAuto,
3805f4a2713aSLionel Sambuc bool IsDependent) const {
3806f4a2713aSLionel Sambuc if (DeducedType.isNull() && !IsDecltypeAuto && !IsDependent)
3807f4a2713aSLionel Sambuc return getAutoDeductType();
3808f4a2713aSLionel Sambuc
3809f4a2713aSLionel Sambuc // Look in the folding set for an existing type.
3810*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3811f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3812f4a2713aSLionel Sambuc AutoType::Profile(ID, DeducedType, IsDecltypeAuto, IsDependent);
3813f4a2713aSLionel Sambuc if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
3814f4a2713aSLionel Sambuc return QualType(AT, 0);
3815f4a2713aSLionel Sambuc
3816f4a2713aSLionel Sambuc AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType,
3817f4a2713aSLionel Sambuc IsDecltypeAuto,
3818f4a2713aSLionel Sambuc IsDependent);
3819f4a2713aSLionel Sambuc Types.push_back(AT);
3820f4a2713aSLionel Sambuc if (InsertPos)
3821f4a2713aSLionel Sambuc AutoTypes.InsertNode(AT, InsertPos);
3822f4a2713aSLionel Sambuc return QualType(AT, 0);
3823f4a2713aSLionel Sambuc }
3824f4a2713aSLionel Sambuc
3825f4a2713aSLionel Sambuc /// getAtomicType - Return the uniqued reference to the atomic type for
3826f4a2713aSLionel Sambuc /// the given value type.
getAtomicType(QualType T) const3827f4a2713aSLionel Sambuc QualType ASTContext::getAtomicType(QualType T) const {
3828f4a2713aSLionel Sambuc // Unique pointers, to guarantee there is only one pointer of a particular
3829f4a2713aSLionel Sambuc // structure.
3830f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
3831f4a2713aSLionel Sambuc AtomicType::Profile(ID, T);
3832f4a2713aSLionel Sambuc
3833*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
3834f4a2713aSLionel Sambuc if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos))
3835f4a2713aSLionel Sambuc return QualType(AT, 0);
3836f4a2713aSLionel Sambuc
3837f4a2713aSLionel Sambuc // If the atomic value type isn't canonical, this won't be a canonical type
3838f4a2713aSLionel Sambuc // either, so fill in the canonical type field.
3839f4a2713aSLionel Sambuc QualType Canonical;
3840f4a2713aSLionel Sambuc if (!T.isCanonical()) {
3841f4a2713aSLionel Sambuc Canonical = getAtomicType(getCanonicalType(T));
3842f4a2713aSLionel Sambuc
3843f4a2713aSLionel Sambuc // Get the new insert position for the node we care about.
3844f4a2713aSLionel Sambuc AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos);
3845*0a6a1f1dSLionel Sambuc assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP;
3846f4a2713aSLionel Sambuc }
3847f4a2713aSLionel Sambuc AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical);
3848f4a2713aSLionel Sambuc Types.push_back(New);
3849f4a2713aSLionel Sambuc AtomicTypes.InsertNode(New, InsertPos);
3850f4a2713aSLionel Sambuc return QualType(New, 0);
3851f4a2713aSLionel Sambuc }
3852f4a2713aSLionel Sambuc
3853f4a2713aSLionel Sambuc /// getAutoDeductType - Get type pattern for deducing against 'auto'.
getAutoDeductType() const3854f4a2713aSLionel Sambuc QualType ASTContext::getAutoDeductType() const {
3855f4a2713aSLionel Sambuc if (AutoDeductTy.isNull())
3856f4a2713aSLionel Sambuc AutoDeductTy = QualType(
3857f4a2713aSLionel Sambuc new (*this, TypeAlignment) AutoType(QualType(), /*decltype(auto)*/false,
3858f4a2713aSLionel Sambuc /*dependent*/false),
3859f4a2713aSLionel Sambuc 0);
3860f4a2713aSLionel Sambuc return AutoDeductTy;
3861f4a2713aSLionel Sambuc }
3862f4a2713aSLionel Sambuc
3863f4a2713aSLionel Sambuc /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
getAutoRRefDeductType() const3864f4a2713aSLionel Sambuc QualType ASTContext::getAutoRRefDeductType() const {
3865f4a2713aSLionel Sambuc if (AutoRRefDeductTy.isNull())
3866f4a2713aSLionel Sambuc AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
3867f4a2713aSLionel Sambuc assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
3868f4a2713aSLionel Sambuc return AutoRRefDeductTy;
3869f4a2713aSLionel Sambuc }
3870f4a2713aSLionel Sambuc
3871f4a2713aSLionel Sambuc /// getTagDeclType - Return the unique reference to the type for the
3872f4a2713aSLionel Sambuc /// specified TagDecl (struct/union/class/enum) decl.
getTagDeclType(const TagDecl * Decl) const3873f4a2713aSLionel Sambuc QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
3874f4a2713aSLionel Sambuc assert (Decl);
3875f4a2713aSLionel Sambuc // FIXME: What is the design on getTagDeclType when it requires casting
3876f4a2713aSLionel Sambuc // away const? mutable?
3877f4a2713aSLionel Sambuc return getTypeDeclType(const_cast<TagDecl*>(Decl));
3878f4a2713aSLionel Sambuc }
3879f4a2713aSLionel Sambuc
3880f4a2713aSLionel Sambuc /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
3881f4a2713aSLionel Sambuc /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
3882f4a2713aSLionel Sambuc /// needs to agree with the definition in <stddef.h>.
getSizeType() const3883f4a2713aSLionel Sambuc CanQualType ASTContext::getSizeType() const {
3884f4a2713aSLionel Sambuc return getFromTargetType(Target->getSizeType());
3885f4a2713aSLionel Sambuc }
3886f4a2713aSLionel Sambuc
3887f4a2713aSLionel Sambuc /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
getIntMaxType() const3888f4a2713aSLionel Sambuc CanQualType ASTContext::getIntMaxType() const {
3889f4a2713aSLionel Sambuc return getFromTargetType(Target->getIntMaxType());
3890f4a2713aSLionel Sambuc }
3891f4a2713aSLionel Sambuc
3892f4a2713aSLionel Sambuc /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5).
getUIntMaxType() const3893f4a2713aSLionel Sambuc CanQualType ASTContext::getUIntMaxType() const {
3894f4a2713aSLionel Sambuc return getFromTargetType(Target->getUIntMaxType());
3895f4a2713aSLionel Sambuc }
3896f4a2713aSLionel Sambuc
3897f4a2713aSLionel Sambuc /// getSignedWCharType - Return the type of "signed wchar_t".
3898f4a2713aSLionel Sambuc /// Used when in C++, as a GCC extension.
getSignedWCharType() const3899f4a2713aSLionel Sambuc QualType ASTContext::getSignedWCharType() const {
3900f4a2713aSLionel Sambuc // FIXME: derive from "Target" ?
3901f4a2713aSLionel Sambuc return WCharTy;
3902f4a2713aSLionel Sambuc }
3903f4a2713aSLionel Sambuc
3904f4a2713aSLionel Sambuc /// getUnsignedWCharType - Return the type of "unsigned wchar_t".
3905f4a2713aSLionel Sambuc /// Used when in C++, as a GCC extension.
getUnsignedWCharType() const3906f4a2713aSLionel Sambuc QualType ASTContext::getUnsignedWCharType() const {
3907f4a2713aSLionel Sambuc // FIXME: derive from "Target" ?
3908f4a2713aSLionel Sambuc return UnsignedIntTy;
3909f4a2713aSLionel Sambuc }
3910f4a2713aSLionel Sambuc
getIntPtrType() const3911f4a2713aSLionel Sambuc QualType ASTContext::getIntPtrType() const {
3912f4a2713aSLionel Sambuc return getFromTargetType(Target->getIntPtrType());
3913f4a2713aSLionel Sambuc }
3914f4a2713aSLionel Sambuc
getUIntPtrType() const3915f4a2713aSLionel Sambuc QualType ASTContext::getUIntPtrType() const {
3916f4a2713aSLionel Sambuc return getCorrespondingUnsignedType(getIntPtrType());
3917f4a2713aSLionel Sambuc }
3918f4a2713aSLionel Sambuc
3919f4a2713aSLionel Sambuc /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
3920f4a2713aSLionel Sambuc /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
getPointerDiffType() const3921f4a2713aSLionel Sambuc QualType ASTContext::getPointerDiffType() const {
3922f4a2713aSLionel Sambuc return getFromTargetType(Target->getPtrDiffType(0));
3923f4a2713aSLionel Sambuc }
3924f4a2713aSLionel Sambuc
3925f4a2713aSLionel Sambuc /// \brief Return the unique type for "pid_t" defined in
3926f4a2713aSLionel Sambuc /// <sys/types.h>. We need this to compute the correct type for vfork().
getProcessIDType() const3927f4a2713aSLionel Sambuc QualType ASTContext::getProcessIDType() const {
3928f4a2713aSLionel Sambuc return getFromTargetType(Target->getProcessIDType());
3929f4a2713aSLionel Sambuc }
3930f4a2713aSLionel Sambuc
3931f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3932f4a2713aSLionel Sambuc // Type Operators
3933f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3934f4a2713aSLionel Sambuc
getCanonicalParamType(QualType T) const3935f4a2713aSLionel Sambuc CanQualType ASTContext::getCanonicalParamType(QualType T) const {
3936f4a2713aSLionel Sambuc // Push qualifiers into arrays, and then discard any remaining
3937f4a2713aSLionel Sambuc // qualifiers.
3938f4a2713aSLionel Sambuc T = getCanonicalType(T);
3939f4a2713aSLionel Sambuc T = getVariableArrayDecayedType(T);
3940f4a2713aSLionel Sambuc const Type *Ty = T.getTypePtr();
3941f4a2713aSLionel Sambuc QualType Result;
3942f4a2713aSLionel Sambuc if (isa<ArrayType>(Ty)) {
3943f4a2713aSLionel Sambuc Result = getArrayDecayedType(QualType(Ty,0));
3944f4a2713aSLionel Sambuc } else if (isa<FunctionType>(Ty)) {
3945f4a2713aSLionel Sambuc Result = getPointerType(QualType(Ty, 0));
3946f4a2713aSLionel Sambuc } else {
3947f4a2713aSLionel Sambuc Result = QualType(Ty, 0);
3948f4a2713aSLionel Sambuc }
3949f4a2713aSLionel Sambuc
3950f4a2713aSLionel Sambuc return CanQualType::CreateUnsafe(Result);
3951f4a2713aSLionel Sambuc }
3952f4a2713aSLionel Sambuc
getUnqualifiedArrayType(QualType type,Qualifiers & quals)3953f4a2713aSLionel Sambuc QualType ASTContext::getUnqualifiedArrayType(QualType type,
3954f4a2713aSLionel Sambuc Qualifiers &quals) {
3955f4a2713aSLionel Sambuc SplitQualType splitType = type.getSplitUnqualifiedType();
3956f4a2713aSLionel Sambuc
3957f4a2713aSLionel Sambuc // FIXME: getSplitUnqualifiedType() actually walks all the way to
3958f4a2713aSLionel Sambuc // the unqualified desugared type and then drops it on the floor.
3959f4a2713aSLionel Sambuc // We then have to strip that sugar back off with
3960f4a2713aSLionel Sambuc // getUnqualifiedDesugaredType(), which is silly.
3961f4a2713aSLionel Sambuc const ArrayType *AT =
3962f4a2713aSLionel Sambuc dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType());
3963f4a2713aSLionel Sambuc
3964f4a2713aSLionel Sambuc // If we don't have an array, just use the results in splitType.
3965f4a2713aSLionel Sambuc if (!AT) {
3966f4a2713aSLionel Sambuc quals = splitType.Quals;
3967f4a2713aSLionel Sambuc return QualType(splitType.Ty, 0);
3968f4a2713aSLionel Sambuc }
3969f4a2713aSLionel Sambuc
3970f4a2713aSLionel Sambuc // Otherwise, recurse on the array's element type.
3971f4a2713aSLionel Sambuc QualType elementType = AT->getElementType();
3972f4a2713aSLionel Sambuc QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
3973f4a2713aSLionel Sambuc
3974f4a2713aSLionel Sambuc // If that didn't change the element type, AT has no qualifiers, so we
3975f4a2713aSLionel Sambuc // can just use the results in splitType.
3976f4a2713aSLionel Sambuc if (elementType == unqualElementType) {
3977f4a2713aSLionel Sambuc assert(quals.empty()); // from the recursive call
3978f4a2713aSLionel Sambuc quals = splitType.Quals;
3979f4a2713aSLionel Sambuc return QualType(splitType.Ty, 0);
3980f4a2713aSLionel Sambuc }
3981f4a2713aSLionel Sambuc
3982f4a2713aSLionel Sambuc // Otherwise, add in the qualifiers from the outermost type, then
3983f4a2713aSLionel Sambuc // build the type back up.
3984f4a2713aSLionel Sambuc quals.addConsistentQualifiers(splitType.Quals);
3985f4a2713aSLionel Sambuc
3986f4a2713aSLionel Sambuc if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
3987f4a2713aSLionel Sambuc return getConstantArrayType(unqualElementType, CAT->getSize(),
3988f4a2713aSLionel Sambuc CAT->getSizeModifier(), 0);
3989f4a2713aSLionel Sambuc }
3990f4a2713aSLionel Sambuc
3991f4a2713aSLionel Sambuc if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
3992f4a2713aSLionel Sambuc return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
3993f4a2713aSLionel Sambuc }
3994f4a2713aSLionel Sambuc
3995f4a2713aSLionel Sambuc if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
3996f4a2713aSLionel Sambuc return getVariableArrayType(unqualElementType,
3997f4a2713aSLionel Sambuc VAT->getSizeExpr(),
3998f4a2713aSLionel Sambuc VAT->getSizeModifier(),
3999f4a2713aSLionel Sambuc VAT->getIndexTypeCVRQualifiers(),
4000f4a2713aSLionel Sambuc VAT->getBracketsRange());
4001f4a2713aSLionel Sambuc }
4002f4a2713aSLionel Sambuc
4003f4a2713aSLionel Sambuc const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
4004f4a2713aSLionel Sambuc return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
4005f4a2713aSLionel Sambuc DSAT->getSizeModifier(), 0,
4006f4a2713aSLionel Sambuc SourceRange());
4007f4a2713aSLionel Sambuc }
4008f4a2713aSLionel Sambuc
4009f4a2713aSLionel Sambuc /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
4010f4a2713aSLionel Sambuc /// may be similar (C++ 4.4), replaces T1 and T2 with the type that
4011f4a2713aSLionel Sambuc /// they point to and return true. If T1 and T2 aren't pointer types
4012f4a2713aSLionel Sambuc /// or pointer-to-member types, or if they are not similar at this
4013f4a2713aSLionel Sambuc /// level, returns false and leaves T1 and T2 unchanged. Top-level
4014f4a2713aSLionel Sambuc /// qualifiers on T1 and T2 are ignored. This function will typically
4015f4a2713aSLionel Sambuc /// be called in a loop that successively "unwraps" pointer and
4016f4a2713aSLionel Sambuc /// pointer-to-member types to compare them at each level.
UnwrapSimilarPointerTypes(QualType & T1,QualType & T2)4017f4a2713aSLionel Sambuc bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
4018f4a2713aSLionel Sambuc const PointerType *T1PtrType = T1->getAs<PointerType>(),
4019f4a2713aSLionel Sambuc *T2PtrType = T2->getAs<PointerType>();
4020f4a2713aSLionel Sambuc if (T1PtrType && T2PtrType) {
4021f4a2713aSLionel Sambuc T1 = T1PtrType->getPointeeType();
4022f4a2713aSLionel Sambuc T2 = T2PtrType->getPointeeType();
4023f4a2713aSLionel Sambuc return true;
4024f4a2713aSLionel Sambuc }
4025f4a2713aSLionel Sambuc
4026f4a2713aSLionel Sambuc const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
4027f4a2713aSLionel Sambuc *T2MPType = T2->getAs<MemberPointerType>();
4028f4a2713aSLionel Sambuc if (T1MPType && T2MPType &&
4029f4a2713aSLionel Sambuc hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
4030f4a2713aSLionel Sambuc QualType(T2MPType->getClass(), 0))) {
4031f4a2713aSLionel Sambuc T1 = T1MPType->getPointeeType();
4032f4a2713aSLionel Sambuc T2 = T2MPType->getPointeeType();
4033f4a2713aSLionel Sambuc return true;
4034f4a2713aSLionel Sambuc }
4035f4a2713aSLionel Sambuc
4036f4a2713aSLionel Sambuc if (getLangOpts().ObjC1) {
4037f4a2713aSLionel Sambuc const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
4038f4a2713aSLionel Sambuc *T2OPType = T2->getAs<ObjCObjectPointerType>();
4039f4a2713aSLionel Sambuc if (T1OPType && T2OPType) {
4040f4a2713aSLionel Sambuc T1 = T1OPType->getPointeeType();
4041f4a2713aSLionel Sambuc T2 = T2OPType->getPointeeType();
4042f4a2713aSLionel Sambuc return true;
4043f4a2713aSLionel Sambuc }
4044f4a2713aSLionel Sambuc }
4045f4a2713aSLionel Sambuc
4046f4a2713aSLionel Sambuc // FIXME: Block pointers, too?
4047f4a2713aSLionel Sambuc
4048f4a2713aSLionel Sambuc return false;
4049f4a2713aSLionel Sambuc }
4050f4a2713aSLionel Sambuc
4051f4a2713aSLionel Sambuc DeclarationNameInfo
getNameForTemplate(TemplateName Name,SourceLocation NameLoc) const4052f4a2713aSLionel Sambuc ASTContext::getNameForTemplate(TemplateName Name,
4053f4a2713aSLionel Sambuc SourceLocation NameLoc) const {
4054f4a2713aSLionel Sambuc switch (Name.getKind()) {
4055f4a2713aSLionel Sambuc case TemplateName::QualifiedTemplate:
4056f4a2713aSLionel Sambuc case TemplateName::Template:
4057f4a2713aSLionel Sambuc // DNInfo work in progress: CHECKME: what about DNLoc?
4058f4a2713aSLionel Sambuc return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(),
4059f4a2713aSLionel Sambuc NameLoc);
4060f4a2713aSLionel Sambuc
4061f4a2713aSLionel Sambuc case TemplateName::OverloadedTemplate: {
4062f4a2713aSLionel Sambuc OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
4063f4a2713aSLionel Sambuc // DNInfo work in progress: CHECKME: what about DNLoc?
4064f4a2713aSLionel Sambuc return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
4065f4a2713aSLionel Sambuc }
4066f4a2713aSLionel Sambuc
4067f4a2713aSLionel Sambuc case TemplateName::DependentTemplate: {
4068f4a2713aSLionel Sambuc DependentTemplateName *DTN = Name.getAsDependentTemplateName();
4069f4a2713aSLionel Sambuc DeclarationName DName;
4070f4a2713aSLionel Sambuc if (DTN->isIdentifier()) {
4071f4a2713aSLionel Sambuc DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
4072f4a2713aSLionel Sambuc return DeclarationNameInfo(DName, NameLoc);
4073f4a2713aSLionel Sambuc } else {
4074f4a2713aSLionel Sambuc DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
4075f4a2713aSLionel Sambuc // DNInfo work in progress: FIXME: source locations?
4076f4a2713aSLionel Sambuc DeclarationNameLoc DNLoc;
4077f4a2713aSLionel Sambuc DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
4078f4a2713aSLionel Sambuc DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
4079f4a2713aSLionel Sambuc return DeclarationNameInfo(DName, NameLoc, DNLoc);
4080f4a2713aSLionel Sambuc }
4081f4a2713aSLionel Sambuc }
4082f4a2713aSLionel Sambuc
4083f4a2713aSLionel Sambuc case TemplateName::SubstTemplateTemplateParm: {
4084f4a2713aSLionel Sambuc SubstTemplateTemplateParmStorage *subst
4085f4a2713aSLionel Sambuc = Name.getAsSubstTemplateTemplateParm();
4086f4a2713aSLionel Sambuc return DeclarationNameInfo(subst->getParameter()->getDeclName(),
4087f4a2713aSLionel Sambuc NameLoc);
4088f4a2713aSLionel Sambuc }
4089f4a2713aSLionel Sambuc
4090f4a2713aSLionel Sambuc case TemplateName::SubstTemplateTemplateParmPack: {
4091f4a2713aSLionel Sambuc SubstTemplateTemplateParmPackStorage *subst
4092f4a2713aSLionel Sambuc = Name.getAsSubstTemplateTemplateParmPack();
4093f4a2713aSLionel Sambuc return DeclarationNameInfo(subst->getParameterPack()->getDeclName(),
4094f4a2713aSLionel Sambuc NameLoc);
4095f4a2713aSLionel Sambuc }
4096f4a2713aSLionel Sambuc }
4097f4a2713aSLionel Sambuc
4098f4a2713aSLionel Sambuc llvm_unreachable("bad template name kind!");
4099f4a2713aSLionel Sambuc }
4100f4a2713aSLionel Sambuc
getCanonicalTemplateName(TemplateName Name) const4101f4a2713aSLionel Sambuc TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
4102f4a2713aSLionel Sambuc switch (Name.getKind()) {
4103f4a2713aSLionel Sambuc case TemplateName::QualifiedTemplate:
4104f4a2713aSLionel Sambuc case TemplateName::Template: {
4105f4a2713aSLionel Sambuc TemplateDecl *Template = Name.getAsTemplateDecl();
4106f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *TTP
4107f4a2713aSLionel Sambuc = dyn_cast<TemplateTemplateParmDecl>(Template))
4108f4a2713aSLionel Sambuc Template = getCanonicalTemplateTemplateParmDecl(TTP);
4109f4a2713aSLionel Sambuc
4110f4a2713aSLionel Sambuc // The canonical template name is the canonical template declaration.
4111f4a2713aSLionel Sambuc return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
4112f4a2713aSLionel Sambuc }
4113f4a2713aSLionel Sambuc
4114f4a2713aSLionel Sambuc case TemplateName::OverloadedTemplate:
4115f4a2713aSLionel Sambuc llvm_unreachable("cannot canonicalize overloaded template");
4116f4a2713aSLionel Sambuc
4117f4a2713aSLionel Sambuc case TemplateName::DependentTemplate: {
4118f4a2713aSLionel Sambuc DependentTemplateName *DTN = Name.getAsDependentTemplateName();
4119f4a2713aSLionel Sambuc assert(DTN && "Non-dependent template names must refer to template decls.");
4120f4a2713aSLionel Sambuc return DTN->CanonicalTemplateName;
4121f4a2713aSLionel Sambuc }
4122f4a2713aSLionel Sambuc
4123f4a2713aSLionel Sambuc case TemplateName::SubstTemplateTemplateParm: {
4124f4a2713aSLionel Sambuc SubstTemplateTemplateParmStorage *subst
4125f4a2713aSLionel Sambuc = Name.getAsSubstTemplateTemplateParm();
4126f4a2713aSLionel Sambuc return getCanonicalTemplateName(subst->getReplacement());
4127f4a2713aSLionel Sambuc }
4128f4a2713aSLionel Sambuc
4129f4a2713aSLionel Sambuc case TemplateName::SubstTemplateTemplateParmPack: {
4130f4a2713aSLionel Sambuc SubstTemplateTemplateParmPackStorage *subst
4131f4a2713aSLionel Sambuc = Name.getAsSubstTemplateTemplateParmPack();
4132f4a2713aSLionel Sambuc TemplateTemplateParmDecl *canonParameter
4133f4a2713aSLionel Sambuc = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack());
4134f4a2713aSLionel Sambuc TemplateArgument canonArgPack
4135f4a2713aSLionel Sambuc = getCanonicalTemplateArgument(subst->getArgumentPack());
4136f4a2713aSLionel Sambuc return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack);
4137f4a2713aSLionel Sambuc }
4138f4a2713aSLionel Sambuc }
4139f4a2713aSLionel Sambuc
4140f4a2713aSLionel Sambuc llvm_unreachable("bad template name!");
4141f4a2713aSLionel Sambuc }
4142f4a2713aSLionel Sambuc
hasSameTemplateName(TemplateName X,TemplateName Y)4143f4a2713aSLionel Sambuc bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
4144f4a2713aSLionel Sambuc X = getCanonicalTemplateName(X);
4145f4a2713aSLionel Sambuc Y = getCanonicalTemplateName(Y);
4146f4a2713aSLionel Sambuc return X.getAsVoidPointer() == Y.getAsVoidPointer();
4147f4a2713aSLionel Sambuc }
4148f4a2713aSLionel Sambuc
4149f4a2713aSLionel Sambuc TemplateArgument
getCanonicalTemplateArgument(const TemplateArgument & Arg) const4150f4a2713aSLionel Sambuc ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
4151f4a2713aSLionel Sambuc switch (Arg.getKind()) {
4152f4a2713aSLionel Sambuc case TemplateArgument::Null:
4153f4a2713aSLionel Sambuc return Arg;
4154f4a2713aSLionel Sambuc
4155f4a2713aSLionel Sambuc case TemplateArgument::Expression:
4156f4a2713aSLionel Sambuc return Arg;
4157f4a2713aSLionel Sambuc
4158f4a2713aSLionel Sambuc case TemplateArgument::Declaration: {
4159f4a2713aSLionel Sambuc ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl());
4160*0a6a1f1dSLionel Sambuc return TemplateArgument(D, Arg.getParamTypeForDecl());
4161f4a2713aSLionel Sambuc }
4162f4a2713aSLionel Sambuc
4163f4a2713aSLionel Sambuc case TemplateArgument::NullPtr:
4164f4a2713aSLionel Sambuc return TemplateArgument(getCanonicalType(Arg.getNullPtrType()),
4165f4a2713aSLionel Sambuc /*isNullPtr*/true);
4166f4a2713aSLionel Sambuc
4167f4a2713aSLionel Sambuc case TemplateArgument::Template:
4168f4a2713aSLionel Sambuc return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
4169f4a2713aSLionel Sambuc
4170f4a2713aSLionel Sambuc case TemplateArgument::TemplateExpansion:
4171f4a2713aSLionel Sambuc return TemplateArgument(getCanonicalTemplateName(
4172f4a2713aSLionel Sambuc Arg.getAsTemplateOrTemplatePattern()),
4173f4a2713aSLionel Sambuc Arg.getNumTemplateExpansions());
4174f4a2713aSLionel Sambuc
4175f4a2713aSLionel Sambuc case TemplateArgument::Integral:
4176f4a2713aSLionel Sambuc return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType()));
4177f4a2713aSLionel Sambuc
4178f4a2713aSLionel Sambuc case TemplateArgument::Type:
4179f4a2713aSLionel Sambuc return TemplateArgument(getCanonicalType(Arg.getAsType()));
4180f4a2713aSLionel Sambuc
4181f4a2713aSLionel Sambuc case TemplateArgument::Pack: {
4182f4a2713aSLionel Sambuc if (Arg.pack_size() == 0)
4183f4a2713aSLionel Sambuc return Arg;
4184f4a2713aSLionel Sambuc
4185f4a2713aSLionel Sambuc TemplateArgument *CanonArgs
4186f4a2713aSLionel Sambuc = new (*this) TemplateArgument[Arg.pack_size()];
4187f4a2713aSLionel Sambuc unsigned Idx = 0;
4188f4a2713aSLionel Sambuc for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
4189f4a2713aSLionel Sambuc AEnd = Arg.pack_end();
4190f4a2713aSLionel Sambuc A != AEnd; (void)++A, ++Idx)
4191f4a2713aSLionel Sambuc CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
4192f4a2713aSLionel Sambuc
4193f4a2713aSLionel Sambuc return TemplateArgument(CanonArgs, Arg.pack_size());
4194f4a2713aSLionel Sambuc }
4195f4a2713aSLionel Sambuc }
4196f4a2713aSLionel Sambuc
4197f4a2713aSLionel Sambuc // Silence GCC warning
4198f4a2713aSLionel Sambuc llvm_unreachable("Unhandled template argument kind");
4199f4a2713aSLionel Sambuc }
4200f4a2713aSLionel Sambuc
4201f4a2713aSLionel Sambuc NestedNameSpecifier *
getCanonicalNestedNameSpecifier(NestedNameSpecifier * NNS) const4202f4a2713aSLionel Sambuc ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
4203f4a2713aSLionel Sambuc if (!NNS)
4204*0a6a1f1dSLionel Sambuc return nullptr;
4205f4a2713aSLionel Sambuc
4206f4a2713aSLionel Sambuc switch (NNS->getKind()) {
4207f4a2713aSLionel Sambuc case NestedNameSpecifier::Identifier:
4208f4a2713aSLionel Sambuc // Canonicalize the prefix but keep the identifier the same.
4209f4a2713aSLionel Sambuc return NestedNameSpecifier::Create(*this,
4210f4a2713aSLionel Sambuc getCanonicalNestedNameSpecifier(NNS->getPrefix()),
4211f4a2713aSLionel Sambuc NNS->getAsIdentifier());
4212f4a2713aSLionel Sambuc
4213f4a2713aSLionel Sambuc case NestedNameSpecifier::Namespace:
4214f4a2713aSLionel Sambuc // A namespace is canonical; build a nested-name-specifier with
4215f4a2713aSLionel Sambuc // this namespace and no prefix.
4216*0a6a1f1dSLionel Sambuc return NestedNameSpecifier::Create(*this, nullptr,
4217f4a2713aSLionel Sambuc NNS->getAsNamespace()->getOriginalNamespace());
4218f4a2713aSLionel Sambuc
4219f4a2713aSLionel Sambuc case NestedNameSpecifier::NamespaceAlias:
4220f4a2713aSLionel Sambuc // A namespace is canonical; build a nested-name-specifier with
4221f4a2713aSLionel Sambuc // this namespace and no prefix.
4222*0a6a1f1dSLionel Sambuc return NestedNameSpecifier::Create(*this, nullptr,
4223f4a2713aSLionel Sambuc NNS->getAsNamespaceAlias()->getNamespace()
4224f4a2713aSLionel Sambuc ->getOriginalNamespace());
4225f4a2713aSLionel Sambuc
4226f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpec:
4227f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpecWithTemplate: {
4228f4a2713aSLionel Sambuc QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
4229f4a2713aSLionel Sambuc
4230f4a2713aSLionel Sambuc // If we have some kind of dependent-named type (e.g., "typename T::type"),
4231f4a2713aSLionel Sambuc // break it apart into its prefix and identifier, then reconsititute those
4232f4a2713aSLionel Sambuc // as the canonical nested-name-specifier. This is required to canonicalize
4233f4a2713aSLionel Sambuc // a dependent nested-name-specifier involving typedefs of dependent-name
4234f4a2713aSLionel Sambuc // types, e.g.,
4235f4a2713aSLionel Sambuc // typedef typename T::type T1;
4236f4a2713aSLionel Sambuc // typedef typename T1::type T2;
4237f4a2713aSLionel Sambuc if (const DependentNameType *DNT = T->getAs<DependentNameType>())
4238f4a2713aSLionel Sambuc return NestedNameSpecifier::Create(*this, DNT->getQualifier(),
4239f4a2713aSLionel Sambuc const_cast<IdentifierInfo *>(DNT->getIdentifier()));
4240f4a2713aSLionel Sambuc
4241f4a2713aSLionel Sambuc // Otherwise, just canonicalize the type, and force it to be a TypeSpec.
4242f4a2713aSLionel Sambuc // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the
4243f4a2713aSLionel Sambuc // first place?
4244*0a6a1f1dSLionel Sambuc return NestedNameSpecifier::Create(*this, nullptr, false,
4245f4a2713aSLionel Sambuc const_cast<Type *>(T.getTypePtr()));
4246f4a2713aSLionel Sambuc }
4247f4a2713aSLionel Sambuc
4248f4a2713aSLionel Sambuc case NestedNameSpecifier::Global:
4249*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Super:
4250*0a6a1f1dSLionel Sambuc // The global specifier and __super specifer are canonical and unique.
4251f4a2713aSLionel Sambuc return NNS;
4252f4a2713aSLionel Sambuc }
4253f4a2713aSLionel Sambuc
4254f4a2713aSLionel Sambuc llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
4255f4a2713aSLionel Sambuc }
4256f4a2713aSLionel Sambuc
4257f4a2713aSLionel Sambuc
getAsArrayType(QualType T) const4258f4a2713aSLionel Sambuc const ArrayType *ASTContext::getAsArrayType(QualType T) const {
4259f4a2713aSLionel Sambuc // Handle the non-qualified case efficiently.
4260f4a2713aSLionel Sambuc if (!T.hasLocalQualifiers()) {
4261f4a2713aSLionel Sambuc // Handle the common positive case fast.
4262f4a2713aSLionel Sambuc if (const ArrayType *AT = dyn_cast<ArrayType>(T))
4263f4a2713aSLionel Sambuc return AT;
4264f4a2713aSLionel Sambuc }
4265f4a2713aSLionel Sambuc
4266f4a2713aSLionel Sambuc // Handle the common negative case fast.
4267f4a2713aSLionel Sambuc if (!isa<ArrayType>(T.getCanonicalType()))
4268*0a6a1f1dSLionel Sambuc return nullptr;
4269f4a2713aSLionel Sambuc
4270f4a2713aSLionel Sambuc // Apply any qualifiers from the array type to the element type. This
4271f4a2713aSLionel Sambuc // implements C99 6.7.3p8: "If the specification of an array type includes
4272f4a2713aSLionel Sambuc // any type qualifiers, the element type is so qualified, not the array type."
4273f4a2713aSLionel Sambuc
4274f4a2713aSLionel Sambuc // If we get here, we either have type qualifiers on the type, or we have
4275f4a2713aSLionel Sambuc // sugar such as a typedef in the way. If we have type qualifiers on the type
4276f4a2713aSLionel Sambuc // we must propagate them down into the element type.
4277f4a2713aSLionel Sambuc
4278f4a2713aSLionel Sambuc SplitQualType split = T.getSplitDesugaredType();
4279f4a2713aSLionel Sambuc Qualifiers qs = split.Quals;
4280f4a2713aSLionel Sambuc
4281f4a2713aSLionel Sambuc // If we have a simple case, just return now.
4282f4a2713aSLionel Sambuc const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty);
4283*0a6a1f1dSLionel Sambuc if (!ATy || qs.empty())
4284f4a2713aSLionel Sambuc return ATy;
4285f4a2713aSLionel Sambuc
4286f4a2713aSLionel Sambuc // Otherwise, we have an array and we have qualifiers on it. Push the
4287f4a2713aSLionel Sambuc // qualifiers into the array element type and return a new array type.
4288f4a2713aSLionel Sambuc QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
4289f4a2713aSLionel Sambuc
4290f4a2713aSLionel Sambuc if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
4291f4a2713aSLionel Sambuc return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
4292f4a2713aSLionel Sambuc CAT->getSizeModifier(),
4293f4a2713aSLionel Sambuc CAT->getIndexTypeCVRQualifiers()));
4294f4a2713aSLionel Sambuc if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
4295f4a2713aSLionel Sambuc return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
4296f4a2713aSLionel Sambuc IAT->getSizeModifier(),
4297f4a2713aSLionel Sambuc IAT->getIndexTypeCVRQualifiers()));
4298f4a2713aSLionel Sambuc
4299f4a2713aSLionel Sambuc if (const DependentSizedArrayType *DSAT
4300f4a2713aSLionel Sambuc = dyn_cast<DependentSizedArrayType>(ATy))
4301f4a2713aSLionel Sambuc return cast<ArrayType>(
4302f4a2713aSLionel Sambuc getDependentSizedArrayType(NewEltTy,
4303f4a2713aSLionel Sambuc DSAT->getSizeExpr(),
4304f4a2713aSLionel Sambuc DSAT->getSizeModifier(),
4305f4a2713aSLionel Sambuc DSAT->getIndexTypeCVRQualifiers(),
4306f4a2713aSLionel Sambuc DSAT->getBracketsRange()));
4307f4a2713aSLionel Sambuc
4308f4a2713aSLionel Sambuc const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
4309f4a2713aSLionel Sambuc return cast<ArrayType>(getVariableArrayType(NewEltTy,
4310f4a2713aSLionel Sambuc VAT->getSizeExpr(),
4311f4a2713aSLionel Sambuc VAT->getSizeModifier(),
4312f4a2713aSLionel Sambuc VAT->getIndexTypeCVRQualifiers(),
4313f4a2713aSLionel Sambuc VAT->getBracketsRange()));
4314f4a2713aSLionel Sambuc }
4315f4a2713aSLionel Sambuc
getAdjustedParameterType(QualType T) const4316f4a2713aSLionel Sambuc QualType ASTContext::getAdjustedParameterType(QualType T) const {
4317f4a2713aSLionel Sambuc if (T->isArrayType() || T->isFunctionType())
4318f4a2713aSLionel Sambuc return getDecayedType(T);
4319f4a2713aSLionel Sambuc return T;
4320f4a2713aSLionel Sambuc }
4321f4a2713aSLionel Sambuc
getSignatureParameterType(QualType T) const4322f4a2713aSLionel Sambuc QualType ASTContext::getSignatureParameterType(QualType T) const {
4323f4a2713aSLionel Sambuc T = getVariableArrayDecayedType(T);
4324f4a2713aSLionel Sambuc T = getAdjustedParameterType(T);
4325f4a2713aSLionel Sambuc return T.getUnqualifiedType();
4326f4a2713aSLionel Sambuc }
4327f4a2713aSLionel Sambuc
4328f4a2713aSLionel Sambuc /// getArrayDecayedType - Return the properly qualified result of decaying the
4329f4a2713aSLionel Sambuc /// specified array type to a pointer. This operation is non-trivial when
4330f4a2713aSLionel Sambuc /// handling typedefs etc. The canonical type of "T" must be an array type,
4331f4a2713aSLionel Sambuc /// this returns a pointer to a properly qualified element of the array.
4332f4a2713aSLionel Sambuc ///
4333f4a2713aSLionel Sambuc /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
getArrayDecayedType(QualType Ty) const4334f4a2713aSLionel Sambuc QualType ASTContext::getArrayDecayedType(QualType Ty) const {
4335f4a2713aSLionel Sambuc // Get the element type with 'getAsArrayType' so that we don't lose any
4336f4a2713aSLionel Sambuc // typedefs in the element type of the array. This also handles propagation
4337f4a2713aSLionel Sambuc // of type qualifiers from the array type into the element type if present
4338f4a2713aSLionel Sambuc // (C99 6.7.3p8).
4339f4a2713aSLionel Sambuc const ArrayType *PrettyArrayType = getAsArrayType(Ty);
4340f4a2713aSLionel Sambuc assert(PrettyArrayType && "Not an array type!");
4341f4a2713aSLionel Sambuc
4342f4a2713aSLionel Sambuc QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
4343f4a2713aSLionel Sambuc
4344f4a2713aSLionel Sambuc // int x[restrict 4] -> int *restrict
4345f4a2713aSLionel Sambuc return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
4346f4a2713aSLionel Sambuc }
4347f4a2713aSLionel Sambuc
getBaseElementType(const ArrayType * array) const4348f4a2713aSLionel Sambuc QualType ASTContext::getBaseElementType(const ArrayType *array) const {
4349f4a2713aSLionel Sambuc return getBaseElementType(array->getElementType());
4350f4a2713aSLionel Sambuc }
4351f4a2713aSLionel Sambuc
getBaseElementType(QualType type) const4352f4a2713aSLionel Sambuc QualType ASTContext::getBaseElementType(QualType type) const {
4353f4a2713aSLionel Sambuc Qualifiers qs;
4354f4a2713aSLionel Sambuc while (true) {
4355f4a2713aSLionel Sambuc SplitQualType split = type.getSplitDesugaredType();
4356f4a2713aSLionel Sambuc const ArrayType *array = split.Ty->getAsArrayTypeUnsafe();
4357f4a2713aSLionel Sambuc if (!array) break;
4358f4a2713aSLionel Sambuc
4359f4a2713aSLionel Sambuc type = array->getElementType();
4360f4a2713aSLionel Sambuc qs.addConsistentQualifiers(split.Quals);
4361f4a2713aSLionel Sambuc }
4362f4a2713aSLionel Sambuc
4363f4a2713aSLionel Sambuc return getQualifiedType(type, qs);
4364f4a2713aSLionel Sambuc }
4365f4a2713aSLionel Sambuc
4366f4a2713aSLionel Sambuc /// getConstantArrayElementCount - Returns number of constant array elements.
4367f4a2713aSLionel Sambuc uint64_t
getConstantArrayElementCount(const ConstantArrayType * CA) const4368f4a2713aSLionel Sambuc ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
4369f4a2713aSLionel Sambuc uint64_t ElementCount = 1;
4370f4a2713aSLionel Sambuc do {
4371f4a2713aSLionel Sambuc ElementCount *= CA->getSize().getZExtValue();
4372f4a2713aSLionel Sambuc CA = dyn_cast_or_null<ConstantArrayType>(
4373f4a2713aSLionel Sambuc CA->getElementType()->getAsArrayTypeUnsafe());
4374f4a2713aSLionel Sambuc } while (CA);
4375f4a2713aSLionel Sambuc return ElementCount;
4376f4a2713aSLionel Sambuc }
4377f4a2713aSLionel Sambuc
4378f4a2713aSLionel Sambuc /// getFloatingRank - Return a relative rank for floating point types.
4379f4a2713aSLionel Sambuc /// This routine will assert if passed a built-in type that isn't a float.
getFloatingRank(QualType T)4380f4a2713aSLionel Sambuc static FloatingRank getFloatingRank(QualType T) {
4381f4a2713aSLionel Sambuc if (const ComplexType *CT = T->getAs<ComplexType>())
4382f4a2713aSLionel Sambuc return getFloatingRank(CT->getElementType());
4383f4a2713aSLionel Sambuc
4384f4a2713aSLionel Sambuc assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
4385f4a2713aSLionel Sambuc switch (T->getAs<BuiltinType>()->getKind()) {
4386f4a2713aSLionel Sambuc default: llvm_unreachable("getFloatingRank(): not a floating type");
4387f4a2713aSLionel Sambuc case BuiltinType::Half: return HalfRank;
4388f4a2713aSLionel Sambuc case BuiltinType::Float: return FloatRank;
4389f4a2713aSLionel Sambuc case BuiltinType::Double: return DoubleRank;
4390f4a2713aSLionel Sambuc case BuiltinType::LongDouble: return LongDoubleRank;
4391f4a2713aSLionel Sambuc }
4392f4a2713aSLionel Sambuc }
4393f4a2713aSLionel Sambuc
4394f4a2713aSLionel Sambuc /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
4395f4a2713aSLionel Sambuc /// point or a complex type (based on typeDomain/typeSize).
4396f4a2713aSLionel Sambuc /// 'typeDomain' is a real floating point or complex type.
4397f4a2713aSLionel Sambuc /// 'typeSize' is a real floating point or complex type.
getFloatingTypeOfSizeWithinDomain(QualType Size,QualType Domain) const4398f4a2713aSLionel Sambuc QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
4399f4a2713aSLionel Sambuc QualType Domain) const {
4400f4a2713aSLionel Sambuc FloatingRank EltRank = getFloatingRank(Size);
4401f4a2713aSLionel Sambuc if (Domain->isComplexType()) {
4402f4a2713aSLionel Sambuc switch (EltRank) {
4403f4a2713aSLionel Sambuc case HalfRank: llvm_unreachable("Complex half is not supported");
4404f4a2713aSLionel Sambuc case FloatRank: return FloatComplexTy;
4405f4a2713aSLionel Sambuc case DoubleRank: return DoubleComplexTy;
4406f4a2713aSLionel Sambuc case LongDoubleRank: return LongDoubleComplexTy;
4407f4a2713aSLionel Sambuc }
4408f4a2713aSLionel Sambuc }
4409f4a2713aSLionel Sambuc
4410f4a2713aSLionel Sambuc assert(Domain->isRealFloatingType() && "Unknown domain!");
4411f4a2713aSLionel Sambuc switch (EltRank) {
4412f4a2713aSLionel Sambuc case HalfRank: return HalfTy;
4413f4a2713aSLionel Sambuc case FloatRank: return FloatTy;
4414f4a2713aSLionel Sambuc case DoubleRank: return DoubleTy;
4415f4a2713aSLionel Sambuc case LongDoubleRank: return LongDoubleTy;
4416f4a2713aSLionel Sambuc }
4417f4a2713aSLionel Sambuc llvm_unreachable("getFloatingRank(): illegal value for rank");
4418f4a2713aSLionel Sambuc }
4419f4a2713aSLionel Sambuc
4420f4a2713aSLionel Sambuc /// getFloatingTypeOrder - Compare the rank of the two specified floating
4421f4a2713aSLionel Sambuc /// point types, ignoring the domain of the type (i.e. 'double' ==
4422f4a2713aSLionel Sambuc /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
4423f4a2713aSLionel Sambuc /// LHS < RHS, return -1.
getFloatingTypeOrder(QualType LHS,QualType RHS) const4424f4a2713aSLionel Sambuc int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
4425f4a2713aSLionel Sambuc FloatingRank LHSR = getFloatingRank(LHS);
4426f4a2713aSLionel Sambuc FloatingRank RHSR = getFloatingRank(RHS);
4427f4a2713aSLionel Sambuc
4428f4a2713aSLionel Sambuc if (LHSR == RHSR)
4429f4a2713aSLionel Sambuc return 0;
4430f4a2713aSLionel Sambuc if (LHSR > RHSR)
4431f4a2713aSLionel Sambuc return 1;
4432f4a2713aSLionel Sambuc return -1;
4433f4a2713aSLionel Sambuc }
4434f4a2713aSLionel Sambuc
4435f4a2713aSLionel Sambuc /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
4436f4a2713aSLionel Sambuc /// routine will assert if passed a built-in type that isn't an integer or enum,
4437f4a2713aSLionel Sambuc /// or if it is not canonicalized.
getIntegerRank(const Type * T) const4438f4a2713aSLionel Sambuc unsigned ASTContext::getIntegerRank(const Type *T) const {
4439f4a2713aSLionel Sambuc assert(T->isCanonicalUnqualified() && "T should be canonicalized");
4440f4a2713aSLionel Sambuc
4441f4a2713aSLionel Sambuc switch (cast<BuiltinType>(T)->getKind()) {
4442f4a2713aSLionel Sambuc default: llvm_unreachable("getIntegerRank(): not a built-in integer");
4443f4a2713aSLionel Sambuc case BuiltinType::Bool:
4444f4a2713aSLionel Sambuc return 1 + (getIntWidth(BoolTy) << 3);
4445f4a2713aSLionel Sambuc case BuiltinType::Char_S:
4446f4a2713aSLionel Sambuc case BuiltinType::Char_U:
4447f4a2713aSLionel Sambuc case BuiltinType::SChar:
4448f4a2713aSLionel Sambuc case BuiltinType::UChar:
4449f4a2713aSLionel Sambuc return 2 + (getIntWidth(CharTy) << 3);
4450f4a2713aSLionel Sambuc case BuiltinType::Short:
4451f4a2713aSLionel Sambuc case BuiltinType::UShort:
4452f4a2713aSLionel Sambuc return 3 + (getIntWidth(ShortTy) << 3);
4453f4a2713aSLionel Sambuc case BuiltinType::Int:
4454f4a2713aSLionel Sambuc case BuiltinType::UInt:
4455f4a2713aSLionel Sambuc return 4 + (getIntWidth(IntTy) << 3);
4456f4a2713aSLionel Sambuc case BuiltinType::Long:
4457f4a2713aSLionel Sambuc case BuiltinType::ULong:
4458f4a2713aSLionel Sambuc return 5 + (getIntWidth(LongTy) << 3);
4459f4a2713aSLionel Sambuc case BuiltinType::LongLong:
4460f4a2713aSLionel Sambuc case BuiltinType::ULongLong:
4461f4a2713aSLionel Sambuc return 6 + (getIntWidth(LongLongTy) << 3);
4462f4a2713aSLionel Sambuc case BuiltinType::Int128:
4463f4a2713aSLionel Sambuc case BuiltinType::UInt128:
4464f4a2713aSLionel Sambuc return 7 + (getIntWidth(Int128Ty) << 3);
4465f4a2713aSLionel Sambuc }
4466f4a2713aSLionel Sambuc }
4467f4a2713aSLionel Sambuc
4468f4a2713aSLionel Sambuc /// \brief Whether this is a promotable bitfield reference according
4469f4a2713aSLionel Sambuc /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
4470f4a2713aSLionel Sambuc ///
4471f4a2713aSLionel Sambuc /// \returns the type this bit-field will promote to, or NULL if no
4472f4a2713aSLionel Sambuc /// promotion occurs.
isPromotableBitField(Expr * E) const4473f4a2713aSLionel Sambuc QualType ASTContext::isPromotableBitField(Expr *E) const {
4474f4a2713aSLionel Sambuc if (E->isTypeDependent() || E->isValueDependent())
4475f4a2713aSLionel Sambuc return QualType();
4476f4a2713aSLionel Sambuc
4477*0a6a1f1dSLionel Sambuc // FIXME: We should not do this unless E->refersToBitField() is true. This
4478*0a6a1f1dSLionel Sambuc // matters in C where getSourceBitField() will find bit-fields for various
4479*0a6a1f1dSLionel Sambuc // cases where the source expression is not a bit-field designator.
4480*0a6a1f1dSLionel Sambuc
4481f4a2713aSLionel Sambuc FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields?
4482f4a2713aSLionel Sambuc if (!Field)
4483f4a2713aSLionel Sambuc return QualType();
4484f4a2713aSLionel Sambuc
4485f4a2713aSLionel Sambuc QualType FT = Field->getType();
4486f4a2713aSLionel Sambuc
4487f4a2713aSLionel Sambuc uint64_t BitWidth = Field->getBitWidthValue(*this);
4488f4a2713aSLionel Sambuc uint64_t IntSize = getTypeSize(IntTy);
4489*0a6a1f1dSLionel Sambuc // C++ [conv.prom]p5:
4490*0a6a1f1dSLionel Sambuc // A prvalue for an integral bit-field can be converted to a prvalue of type
4491*0a6a1f1dSLionel Sambuc // int if int can represent all the values of the bit-field; otherwise, it
4492*0a6a1f1dSLionel Sambuc // can be converted to unsigned int if unsigned int can represent all the
4493*0a6a1f1dSLionel Sambuc // values of the bit-field. If the bit-field is larger yet, no integral
4494*0a6a1f1dSLionel Sambuc // promotion applies to it.
4495*0a6a1f1dSLionel Sambuc // C11 6.3.1.1/2:
4496*0a6a1f1dSLionel Sambuc // [For a bit-field of type _Bool, int, signed int, or unsigned int:]
4497*0a6a1f1dSLionel Sambuc // If an int can represent all values of the original type (as restricted by
4498*0a6a1f1dSLionel Sambuc // the width, for a bit-field), the value is converted to an int; otherwise,
4499*0a6a1f1dSLionel Sambuc // it is converted to an unsigned int.
4500*0a6a1f1dSLionel Sambuc //
4501*0a6a1f1dSLionel Sambuc // FIXME: C does not permit promotion of a 'long : 3' bitfield to int.
4502*0a6a1f1dSLionel Sambuc // We perform that promotion here to match GCC and C++.
4503f4a2713aSLionel Sambuc if (BitWidth < IntSize)
4504f4a2713aSLionel Sambuc return IntTy;
4505f4a2713aSLionel Sambuc
4506f4a2713aSLionel Sambuc if (BitWidth == IntSize)
4507f4a2713aSLionel Sambuc return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
4508f4a2713aSLionel Sambuc
4509f4a2713aSLionel Sambuc // Types bigger than int are not subject to promotions, and therefore act
4510*0a6a1f1dSLionel Sambuc // like the base type. GCC has some weird bugs in this area that we
4511*0a6a1f1dSLionel Sambuc // deliberately do not follow (GCC follows a pre-standard resolution to
4512*0a6a1f1dSLionel Sambuc // C's DR315 which treats bit-width as being part of the type, and this leaks
4513*0a6a1f1dSLionel Sambuc // into their semantics in some cases).
4514f4a2713aSLionel Sambuc return QualType();
4515f4a2713aSLionel Sambuc }
4516f4a2713aSLionel Sambuc
4517f4a2713aSLionel Sambuc /// getPromotedIntegerType - Returns the type that Promotable will
4518f4a2713aSLionel Sambuc /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
4519f4a2713aSLionel Sambuc /// integer type.
getPromotedIntegerType(QualType Promotable) const4520f4a2713aSLionel Sambuc QualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
4521f4a2713aSLionel Sambuc assert(!Promotable.isNull());
4522f4a2713aSLionel Sambuc assert(Promotable->isPromotableIntegerType());
4523f4a2713aSLionel Sambuc if (const EnumType *ET = Promotable->getAs<EnumType>())
4524f4a2713aSLionel Sambuc return ET->getDecl()->getPromotionType();
4525f4a2713aSLionel Sambuc
4526f4a2713aSLionel Sambuc if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) {
4527f4a2713aSLionel Sambuc // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t
4528f4a2713aSLionel Sambuc // (3.9.1) can be converted to a prvalue of the first of the following
4529f4a2713aSLionel Sambuc // types that can represent all the values of its underlying type:
4530f4a2713aSLionel Sambuc // int, unsigned int, long int, unsigned long int, long long int, or
4531f4a2713aSLionel Sambuc // unsigned long long int [...]
4532f4a2713aSLionel Sambuc // FIXME: Is there some better way to compute this?
4533f4a2713aSLionel Sambuc if (BT->getKind() == BuiltinType::WChar_S ||
4534f4a2713aSLionel Sambuc BT->getKind() == BuiltinType::WChar_U ||
4535f4a2713aSLionel Sambuc BT->getKind() == BuiltinType::Char16 ||
4536f4a2713aSLionel Sambuc BT->getKind() == BuiltinType::Char32) {
4537f4a2713aSLionel Sambuc bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S;
4538f4a2713aSLionel Sambuc uint64_t FromSize = getTypeSize(BT);
4539f4a2713aSLionel Sambuc QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy,
4540f4a2713aSLionel Sambuc LongLongTy, UnsignedLongLongTy };
4541f4a2713aSLionel Sambuc for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) {
4542f4a2713aSLionel Sambuc uint64_t ToSize = getTypeSize(PromoteTypes[Idx]);
4543f4a2713aSLionel Sambuc if (FromSize < ToSize ||
4544f4a2713aSLionel Sambuc (FromSize == ToSize &&
4545f4a2713aSLionel Sambuc FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType()))
4546f4a2713aSLionel Sambuc return PromoteTypes[Idx];
4547f4a2713aSLionel Sambuc }
4548f4a2713aSLionel Sambuc llvm_unreachable("char type should fit into long long");
4549f4a2713aSLionel Sambuc }
4550f4a2713aSLionel Sambuc }
4551f4a2713aSLionel Sambuc
4552f4a2713aSLionel Sambuc // At this point, we should have a signed or unsigned integer type.
4553f4a2713aSLionel Sambuc if (Promotable->isSignedIntegerType())
4554f4a2713aSLionel Sambuc return IntTy;
4555f4a2713aSLionel Sambuc uint64_t PromotableSize = getIntWidth(Promotable);
4556f4a2713aSLionel Sambuc uint64_t IntSize = getIntWidth(IntTy);
4557f4a2713aSLionel Sambuc assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
4558f4a2713aSLionel Sambuc return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
4559f4a2713aSLionel Sambuc }
4560f4a2713aSLionel Sambuc
4561f4a2713aSLionel Sambuc /// \brief Recurses in pointer/array types until it finds an objc retainable
4562f4a2713aSLionel Sambuc /// type and returns its ownership.
getInnerObjCOwnership(QualType T) const4563f4a2713aSLionel Sambuc Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const {
4564f4a2713aSLionel Sambuc while (!T.isNull()) {
4565f4a2713aSLionel Sambuc if (T.getObjCLifetime() != Qualifiers::OCL_None)
4566f4a2713aSLionel Sambuc return T.getObjCLifetime();
4567f4a2713aSLionel Sambuc if (T->isArrayType())
4568f4a2713aSLionel Sambuc T = getBaseElementType(T);
4569f4a2713aSLionel Sambuc else if (const PointerType *PT = T->getAs<PointerType>())
4570f4a2713aSLionel Sambuc T = PT->getPointeeType();
4571f4a2713aSLionel Sambuc else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4572f4a2713aSLionel Sambuc T = RT->getPointeeType();
4573f4a2713aSLionel Sambuc else
4574f4a2713aSLionel Sambuc break;
4575f4a2713aSLionel Sambuc }
4576f4a2713aSLionel Sambuc
4577f4a2713aSLionel Sambuc return Qualifiers::OCL_None;
4578f4a2713aSLionel Sambuc }
4579f4a2713aSLionel Sambuc
getIntegerTypeForEnum(const EnumType * ET)4580f4a2713aSLionel Sambuc static const Type *getIntegerTypeForEnum(const EnumType *ET) {
4581f4a2713aSLionel Sambuc // Incomplete enum types are not treated as integer types.
4582f4a2713aSLionel Sambuc // FIXME: In C++, enum types are never integer types.
4583f4a2713aSLionel Sambuc if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
4584f4a2713aSLionel Sambuc return ET->getDecl()->getIntegerType().getTypePtr();
4585*0a6a1f1dSLionel Sambuc return nullptr;
4586f4a2713aSLionel Sambuc }
4587f4a2713aSLionel Sambuc
4588f4a2713aSLionel Sambuc /// getIntegerTypeOrder - Returns the highest ranked integer type:
4589f4a2713aSLionel Sambuc /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
4590f4a2713aSLionel Sambuc /// LHS < RHS, return -1.
getIntegerTypeOrder(QualType LHS,QualType RHS) const4591f4a2713aSLionel Sambuc int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
4592f4a2713aSLionel Sambuc const Type *LHSC = getCanonicalType(LHS).getTypePtr();
4593f4a2713aSLionel Sambuc const Type *RHSC = getCanonicalType(RHS).getTypePtr();
4594f4a2713aSLionel Sambuc
4595f4a2713aSLionel Sambuc // Unwrap enums to their underlying type.
4596f4a2713aSLionel Sambuc if (const EnumType *ET = dyn_cast<EnumType>(LHSC))
4597f4a2713aSLionel Sambuc LHSC = getIntegerTypeForEnum(ET);
4598f4a2713aSLionel Sambuc if (const EnumType *ET = dyn_cast<EnumType>(RHSC))
4599f4a2713aSLionel Sambuc RHSC = getIntegerTypeForEnum(ET);
4600f4a2713aSLionel Sambuc
4601f4a2713aSLionel Sambuc if (LHSC == RHSC) return 0;
4602f4a2713aSLionel Sambuc
4603f4a2713aSLionel Sambuc bool LHSUnsigned = LHSC->isUnsignedIntegerType();
4604f4a2713aSLionel Sambuc bool RHSUnsigned = RHSC->isUnsignedIntegerType();
4605f4a2713aSLionel Sambuc
4606f4a2713aSLionel Sambuc unsigned LHSRank = getIntegerRank(LHSC);
4607f4a2713aSLionel Sambuc unsigned RHSRank = getIntegerRank(RHSC);
4608f4a2713aSLionel Sambuc
4609f4a2713aSLionel Sambuc if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
4610f4a2713aSLionel Sambuc if (LHSRank == RHSRank) return 0;
4611f4a2713aSLionel Sambuc return LHSRank > RHSRank ? 1 : -1;
4612f4a2713aSLionel Sambuc }
4613f4a2713aSLionel Sambuc
4614f4a2713aSLionel Sambuc // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
4615f4a2713aSLionel Sambuc if (LHSUnsigned) {
4616f4a2713aSLionel Sambuc // If the unsigned [LHS] type is larger, return it.
4617f4a2713aSLionel Sambuc if (LHSRank >= RHSRank)
4618f4a2713aSLionel Sambuc return 1;
4619f4a2713aSLionel Sambuc
4620f4a2713aSLionel Sambuc // If the signed type can represent all values of the unsigned type, it
4621f4a2713aSLionel Sambuc // wins. Because we are dealing with 2's complement and types that are
4622f4a2713aSLionel Sambuc // powers of two larger than each other, this is always safe.
4623f4a2713aSLionel Sambuc return -1;
4624f4a2713aSLionel Sambuc }
4625f4a2713aSLionel Sambuc
4626f4a2713aSLionel Sambuc // If the unsigned [RHS] type is larger, return it.
4627f4a2713aSLionel Sambuc if (RHSRank >= LHSRank)
4628f4a2713aSLionel Sambuc return -1;
4629f4a2713aSLionel Sambuc
4630f4a2713aSLionel Sambuc // If the signed type can represent all values of the unsigned type, it
4631f4a2713aSLionel Sambuc // wins. Because we are dealing with 2's complement and types that are
4632f4a2713aSLionel Sambuc // powers of two larger than each other, this is always safe.
4633f4a2713aSLionel Sambuc return 1;
4634f4a2713aSLionel Sambuc }
4635f4a2713aSLionel Sambuc
4636f4a2713aSLionel Sambuc // getCFConstantStringType - Return the type used for constant CFStrings.
getCFConstantStringType() const4637f4a2713aSLionel Sambuc QualType ASTContext::getCFConstantStringType() const {
4638f4a2713aSLionel Sambuc if (!CFConstantStringTypeDecl) {
4639*0a6a1f1dSLionel Sambuc CFConstantStringTypeDecl = buildImplicitRecord("NSConstantString");
4640f4a2713aSLionel Sambuc CFConstantStringTypeDecl->startDefinition();
4641f4a2713aSLionel Sambuc
4642f4a2713aSLionel Sambuc QualType FieldTypes[4];
4643f4a2713aSLionel Sambuc
4644f4a2713aSLionel Sambuc // const int *isa;
4645f4a2713aSLionel Sambuc FieldTypes[0] = getPointerType(IntTy.withConst());
4646f4a2713aSLionel Sambuc // int flags;
4647f4a2713aSLionel Sambuc FieldTypes[1] = IntTy;
4648f4a2713aSLionel Sambuc // const char *str;
4649f4a2713aSLionel Sambuc FieldTypes[2] = getPointerType(CharTy.withConst());
4650f4a2713aSLionel Sambuc // long length;
4651f4a2713aSLionel Sambuc FieldTypes[3] = LongTy;
4652f4a2713aSLionel Sambuc
4653f4a2713aSLionel Sambuc // Create fields
4654f4a2713aSLionel Sambuc for (unsigned i = 0; i < 4; ++i) {
4655f4a2713aSLionel Sambuc FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
4656f4a2713aSLionel Sambuc SourceLocation(),
4657*0a6a1f1dSLionel Sambuc SourceLocation(), nullptr,
4658*0a6a1f1dSLionel Sambuc FieldTypes[i], /*TInfo=*/nullptr,
4659*0a6a1f1dSLionel Sambuc /*BitWidth=*/nullptr,
4660f4a2713aSLionel Sambuc /*Mutable=*/false,
4661f4a2713aSLionel Sambuc ICIS_NoInit);
4662f4a2713aSLionel Sambuc Field->setAccess(AS_public);
4663f4a2713aSLionel Sambuc CFConstantStringTypeDecl->addDecl(Field);
4664f4a2713aSLionel Sambuc }
4665f4a2713aSLionel Sambuc
4666f4a2713aSLionel Sambuc CFConstantStringTypeDecl->completeDefinition();
4667f4a2713aSLionel Sambuc }
4668f4a2713aSLionel Sambuc
4669f4a2713aSLionel Sambuc return getTagDeclType(CFConstantStringTypeDecl);
4670f4a2713aSLionel Sambuc }
4671f4a2713aSLionel Sambuc
getObjCSuperType() const4672f4a2713aSLionel Sambuc QualType ASTContext::getObjCSuperType() const {
4673f4a2713aSLionel Sambuc if (ObjCSuperType.isNull()) {
4674*0a6a1f1dSLionel Sambuc RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super");
4675f4a2713aSLionel Sambuc TUDecl->addDecl(ObjCSuperTypeDecl);
4676f4a2713aSLionel Sambuc ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl);
4677f4a2713aSLionel Sambuc }
4678f4a2713aSLionel Sambuc return ObjCSuperType;
4679f4a2713aSLionel Sambuc }
4680f4a2713aSLionel Sambuc
setCFConstantStringType(QualType T)4681f4a2713aSLionel Sambuc void ASTContext::setCFConstantStringType(QualType T) {
4682f4a2713aSLionel Sambuc const RecordType *Rec = T->getAs<RecordType>();
4683f4a2713aSLionel Sambuc assert(Rec && "Invalid CFConstantStringType");
4684f4a2713aSLionel Sambuc CFConstantStringTypeDecl = Rec->getDecl();
4685f4a2713aSLionel Sambuc }
4686f4a2713aSLionel Sambuc
getBlockDescriptorType() const4687f4a2713aSLionel Sambuc QualType ASTContext::getBlockDescriptorType() const {
4688f4a2713aSLionel Sambuc if (BlockDescriptorType)
4689f4a2713aSLionel Sambuc return getTagDeclType(BlockDescriptorType);
4690f4a2713aSLionel Sambuc
4691*0a6a1f1dSLionel Sambuc RecordDecl *RD;
4692f4a2713aSLionel Sambuc // FIXME: Needs the FlagAppleBlock bit.
4693*0a6a1f1dSLionel Sambuc RD = buildImplicitRecord("__block_descriptor");
4694*0a6a1f1dSLionel Sambuc RD->startDefinition();
4695f4a2713aSLionel Sambuc
4696f4a2713aSLionel Sambuc QualType FieldTypes[] = {
4697f4a2713aSLionel Sambuc UnsignedLongTy,
4698f4a2713aSLionel Sambuc UnsignedLongTy,
4699f4a2713aSLionel Sambuc };
4700f4a2713aSLionel Sambuc
4701f4a2713aSLionel Sambuc static const char *const FieldNames[] = {
4702f4a2713aSLionel Sambuc "reserved",
4703f4a2713aSLionel Sambuc "Size"
4704f4a2713aSLionel Sambuc };
4705f4a2713aSLionel Sambuc
4706f4a2713aSLionel Sambuc for (size_t i = 0; i < 2; ++i) {
4707*0a6a1f1dSLionel Sambuc FieldDecl *Field = FieldDecl::Create(
4708*0a6a1f1dSLionel Sambuc *this, RD, SourceLocation(), SourceLocation(),
4709*0a6a1f1dSLionel Sambuc &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr,
4710*0a6a1f1dSLionel Sambuc /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit);
4711f4a2713aSLionel Sambuc Field->setAccess(AS_public);
4712*0a6a1f1dSLionel Sambuc RD->addDecl(Field);
4713f4a2713aSLionel Sambuc }
4714f4a2713aSLionel Sambuc
4715*0a6a1f1dSLionel Sambuc RD->completeDefinition();
4716f4a2713aSLionel Sambuc
4717*0a6a1f1dSLionel Sambuc BlockDescriptorType = RD;
4718f4a2713aSLionel Sambuc
4719f4a2713aSLionel Sambuc return getTagDeclType(BlockDescriptorType);
4720f4a2713aSLionel Sambuc }
4721f4a2713aSLionel Sambuc
getBlockDescriptorExtendedType() const4722f4a2713aSLionel Sambuc QualType ASTContext::getBlockDescriptorExtendedType() const {
4723f4a2713aSLionel Sambuc if (BlockDescriptorExtendedType)
4724f4a2713aSLionel Sambuc return getTagDeclType(BlockDescriptorExtendedType);
4725f4a2713aSLionel Sambuc
4726*0a6a1f1dSLionel Sambuc RecordDecl *RD;
4727f4a2713aSLionel Sambuc // FIXME: Needs the FlagAppleBlock bit.
4728*0a6a1f1dSLionel Sambuc RD = buildImplicitRecord("__block_descriptor_withcopydispose");
4729*0a6a1f1dSLionel Sambuc RD->startDefinition();
4730f4a2713aSLionel Sambuc
4731f4a2713aSLionel Sambuc QualType FieldTypes[] = {
4732f4a2713aSLionel Sambuc UnsignedLongTy,
4733f4a2713aSLionel Sambuc UnsignedLongTy,
4734f4a2713aSLionel Sambuc getPointerType(VoidPtrTy),
4735f4a2713aSLionel Sambuc getPointerType(VoidPtrTy)
4736f4a2713aSLionel Sambuc };
4737f4a2713aSLionel Sambuc
4738f4a2713aSLionel Sambuc static const char *const FieldNames[] = {
4739f4a2713aSLionel Sambuc "reserved",
4740f4a2713aSLionel Sambuc "Size",
4741f4a2713aSLionel Sambuc "CopyFuncPtr",
4742f4a2713aSLionel Sambuc "DestroyFuncPtr"
4743f4a2713aSLionel Sambuc };
4744f4a2713aSLionel Sambuc
4745f4a2713aSLionel Sambuc for (size_t i = 0; i < 4; ++i) {
4746*0a6a1f1dSLionel Sambuc FieldDecl *Field = FieldDecl::Create(
4747*0a6a1f1dSLionel Sambuc *this, RD, SourceLocation(), SourceLocation(),
4748*0a6a1f1dSLionel Sambuc &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr,
4749*0a6a1f1dSLionel Sambuc /*BitWidth=*/nullptr,
4750*0a6a1f1dSLionel Sambuc /*Mutable=*/false, ICIS_NoInit);
4751f4a2713aSLionel Sambuc Field->setAccess(AS_public);
4752*0a6a1f1dSLionel Sambuc RD->addDecl(Field);
4753f4a2713aSLionel Sambuc }
4754f4a2713aSLionel Sambuc
4755*0a6a1f1dSLionel Sambuc RD->completeDefinition();
4756f4a2713aSLionel Sambuc
4757*0a6a1f1dSLionel Sambuc BlockDescriptorExtendedType = RD;
4758f4a2713aSLionel Sambuc return getTagDeclType(BlockDescriptorExtendedType);
4759f4a2713aSLionel Sambuc }
4760f4a2713aSLionel Sambuc
4761f4a2713aSLionel Sambuc /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty"
4762f4a2713aSLionel Sambuc /// requires copy/dispose. Note that this must match the logic
4763f4a2713aSLionel Sambuc /// in buildByrefHelpers.
BlockRequiresCopying(QualType Ty,const VarDecl * D)4764f4a2713aSLionel Sambuc bool ASTContext::BlockRequiresCopying(QualType Ty,
4765f4a2713aSLionel Sambuc const VarDecl *D) {
4766f4a2713aSLionel Sambuc if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) {
4767f4a2713aSLionel Sambuc const Expr *copyExpr = getBlockVarCopyInits(D);
4768f4a2713aSLionel Sambuc if (!copyExpr && record->hasTrivialDestructor()) return false;
4769f4a2713aSLionel Sambuc
4770f4a2713aSLionel Sambuc return true;
4771f4a2713aSLionel Sambuc }
4772f4a2713aSLionel Sambuc
4773f4a2713aSLionel Sambuc if (!Ty->isObjCRetainableType()) return false;
4774f4a2713aSLionel Sambuc
4775f4a2713aSLionel Sambuc Qualifiers qs = Ty.getQualifiers();
4776f4a2713aSLionel Sambuc
4777f4a2713aSLionel Sambuc // If we have lifetime, that dominates.
4778f4a2713aSLionel Sambuc if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
4779f4a2713aSLionel Sambuc assert(getLangOpts().ObjCAutoRefCount);
4780f4a2713aSLionel Sambuc
4781f4a2713aSLionel Sambuc switch (lifetime) {
4782f4a2713aSLionel Sambuc case Qualifiers::OCL_None: llvm_unreachable("impossible");
4783f4a2713aSLionel Sambuc
4784f4a2713aSLionel Sambuc // These are just bits as far as the runtime is concerned.
4785f4a2713aSLionel Sambuc case Qualifiers::OCL_ExplicitNone:
4786f4a2713aSLionel Sambuc case Qualifiers::OCL_Autoreleasing:
4787f4a2713aSLionel Sambuc return false;
4788f4a2713aSLionel Sambuc
4789f4a2713aSLionel Sambuc // Tell the runtime that this is ARC __weak, called by the
4790f4a2713aSLionel Sambuc // byref routines.
4791f4a2713aSLionel Sambuc case Qualifiers::OCL_Weak:
4792f4a2713aSLionel Sambuc // ARC __strong __block variables need to be retained.
4793f4a2713aSLionel Sambuc case Qualifiers::OCL_Strong:
4794f4a2713aSLionel Sambuc return true;
4795f4a2713aSLionel Sambuc }
4796f4a2713aSLionel Sambuc llvm_unreachable("fell out of lifetime switch!");
4797f4a2713aSLionel Sambuc }
4798f4a2713aSLionel Sambuc return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) ||
4799f4a2713aSLionel Sambuc Ty->isObjCObjectPointerType());
4800f4a2713aSLionel Sambuc }
4801f4a2713aSLionel Sambuc
getByrefLifetime(QualType Ty,Qualifiers::ObjCLifetime & LifeTime,bool & HasByrefExtendedLayout) const4802f4a2713aSLionel Sambuc bool ASTContext::getByrefLifetime(QualType Ty,
4803f4a2713aSLionel Sambuc Qualifiers::ObjCLifetime &LifeTime,
4804f4a2713aSLionel Sambuc bool &HasByrefExtendedLayout) const {
4805f4a2713aSLionel Sambuc
4806f4a2713aSLionel Sambuc if (!getLangOpts().ObjC1 ||
4807f4a2713aSLionel Sambuc getLangOpts().getGC() != LangOptions::NonGC)
4808f4a2713aSLionel Sambuc return false;
4809f4a2713aSLionel Sambuc
4810f4a2713aSLionel Sambuc HasByrefExtendedLayout = false;
4811f4a2713aSLionel Sambuc if (Ty->isRecordType()) {
4812f4a2713aSLionel Sambuc HasByrefExtendedLayout = true;
4813f4a2713aSLionel Sambuc LifeTime = Qualifiers::OCL_None;
4814f4a2713aSLionel Sambuc }
4815f4a2713aSLionel Sambuc else if (getLangOpts().ObjCAutoRefCount)
4816f4a2713aSLionel Sambuc LifeTime = Ty.getObjCLifetime();
4817f4a2713aSLionel Sambuc // MRR.
4818f4a2713aSLionel Sambuc else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
4819f4a2713aSLionel Sambuc LifeTime = Qualifiers::OCL_ExplicitNone;
4820f4a2713aSLionel Sambuc else
4821f4a2713aSLionel Sambuc LifeTime = Qualifiers::OCL_None;
4822f4a2713aSLionel Sambuc return true;
4823f4a2713aSLionel Sambuc }
4824f4a2713aSLionel Sambuc
getObjCInstanceTypeDecl()4825f4a2713aSLionel Sambuc TypedefDecl *ASTContext::getObjCInstanceTypeDecl() {
4826f4a2713aSLionel Sambuc if (!ObjCInstanceTypeDecl)
4827*0a6a1f1dSLionel Sambuc ObjCInstanceTypeDecl =
4828*0a6a1f1dSLionel Sambuc buildImplicitTypedef(getObjCIdType(), "instancetype");
4829f4a2713aSLionel Sambuc return ObjCInstanceTypeDecl;
4830f4a2713aSLionel Sambuc }
4831f4a2713aSLionel Sambuc
4832f4a2713aSLionel Sambuc // This returns true if a type has been typedefed to BOOL:
4833f4a2713aSLionel Sambuc // typedef <type> BOOL;
isTypeTypedefedAsBOOL(QualType T)4834f4a2713aSLionel Sambuc static bool isTypeTypedefedAsBOOL(QualType T) {
4835f4a2713aSLionel Sambuc if (const TypedefType *TT = dyn_cast<TypedefType>(T))
4836f4a2713aSLionel Sambuc if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
4837f4a2713aSLionel Sambuc return II->isStr("BOOL");
4838f4a2713aSLionel Sambuc
4839f4a2713aSLionel Sambuc return false;
4840f4a2713aSLionel Sambuc }
4841f4a2713aSLionel Sambuc
4842f4a2713aSLionel Sambuc /// getObjCEncodingTypeSize returns size of type for objective-c encoding
4843f4a2713aSLionel Sambuc /// purpose.
getObjCEncodingTypeSize(QualType type) const4844f4a2713aSLionel Sambuc CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
4845f4a2713aSLionel Sambuc if (!type->isIncompleteArrayType() && type->isIncompleteType())
4846f4a2713aSLionel Sambuc return CharUnits::Zero();
4847f4a2713aSLionel Sambuc
4848f4a2713aSLionel Sambuc CharUnits sz = getTypeSizeInChars(type);
4849f4a2713aSLionel Sambuc
4850f4a2713aSLionel Sambuc // Make all integer and enum types at least as large as an int
4851f4a2713aSLionel Sambuc if (sz.isPositive() && type->isIntegralOrEnumerationType())
4852f4a2713aSLionel Sambuc sz = std::max(sz, getTypeSizeInChars(IntTy));
4853f4a2713aSLionel Sambuc // Treat arrays as pointers, since that's how they're passed in.
4854f4a2713aSLionel Sambuc else if (type->isArrayType())
4855f4a2713aSLionel Sambuc sz = getTypeSizeInChars(VoidPtrTy);
4856f4a2713aSLionel Sambuc return sz;
4857f4a2713aSLionel Sambuc }
4858f4a2713aSLionel Sambuc
isMSStaticDataMemberInlineDefinition(const VarDecl * VD) const4859*0a6a1f1dSLionel Sambuc bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const {
4860*0a6a1f1dSLionel Sambuc return getLangOpts().MSVCCompat && VD->isStaticDataMember() &&
4861*0a6a1f1dSLionel Sambuc VD->getType()->isIntegralOrEnumerationType() &&
4862*0a6a1f1dSLionel Sambuc !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit();
4863*0a6a1f1dSLionel Sambuc }
4864*0a6a1f1dSLionel Sambuc
4865f4a2713aSLionel Sambuc static inline
charUnitsToString(const CharUnits & CU)4866f4a2713aSLionel Sambuc std::string charUnitsToString(const CharUnits &CU) {
4867f4a2713aSLionel Sambuc return llvm::itostr(CU.getQuantity());
4868f4a2713aSLionel Sambuc }
4869f4a2713aSLionel Sambuc
4870f4a2713aSLionel Sambuc /// getObjCEncodingForBlock - Return the encoded type for this block
4871f4a2713aSLionel Sambuc /// declaration.
getObjCEncodingForBlock(const BlockExpr * Expr) const4872f4a2713aSLionel Sambuc std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
4873f4a2713aSLionel Sambuc std::string S;
4874f4a2713aSLionel Sambuc
4875f4a2713aSLionel Sambuc const BlockDecl *Decl = Expr->getBlockDecl();
4876f4a2713aSLionel Sambuc QualType BlockTy =
4877f4a2713aSLionel Sambuc Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
4878f4a2713aSLionel Sambuc // Encode result type.
4879f4a2713aSLionel Sambuc if (getLangOpts().EncodeExtendedBlockSig)
4880*0a6a1f1dSLionel Sambuc getObjCEncodingForMethodParameter(
4881*0a6a1f1dSLionel Sambuc Decl::OBJC_TQ_None, BlockTy->getAs<FunctionType>()->getReturnType(), S,
4882*0a6a1f1dSLionel Sambuc true /*Extended*/);
4883f4a2713aSLionel Sambuc else
4884*0a6a1f1dSLionel Sambuc getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getReturnType(), S);
4885f4a2713aSLionel Sambuc // Compute size of all parameters.
4886f4a2713aSLionel Sambuc // Start with computing size of a pointer in number of bytes.
4887f4a2713aSLionel Sambuc // FIXME: There might(should) be a better way of doing this computation!
4888f4a2713aSLionel Sambuc SourceLocation Loc;
4889f4a2713aSLionel Sambuc CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
4890f4a2713aSLionel Sambuc CharUnits ParmOffset = PtrSize;
4891*0a6a1f1dSLionel Sambuc for (auto PI : Decl->params()) {
4892*0a6a1f1dSLionel Sambuc QualType PType = PI->getType();
4893f4a2713aSLionel Sambuc CharUnits sz = getObjCEncodingTypeSize(PType);
4894f4a2713aSLionel Sambuc if (sz.isZero())
4895f4a2713aSLionel Sambuc continue;
4896f4a2713aSLionel Sambuc assert (sz.isPositive() && "BlockExpr - Incomplete param type");
4897f4a2713aSLionel Sambuc ParmOffset += sz;
4898f4a2713aSLionel Sambuc }
4899f4a2713aSLionel Sambuc // Size of the argument frame
4900f4a2713aSLionel Sambuc S += charUnitsToString(ParmOffset);
4901f4a2713aSLionel Sambuc // Block pointer and offset.
4902f4a2713aSLionel Sambuc S += "@?0";
4903f4a2713aSLionel Sambuc
4904f4a2713aSLionel Sambuc // Argument types.
4905f4a2713aSLionel Sambuc ParmOffset = PtrSize;
4906*0a6a1f1dSLionel Sambuc for (auto PVDecl : Decl->params()) {
4907f4a2713aSLionel Sambuc QualType PType = PVDecl->getOriginalType();
4908f4a2713aSLionel Sambuc if (const ArrayType *AT =
4909f4a2713aSLionel Sambuc dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
4910f4a2713aSLionel Sambuc // Use array's original type only if it has known number of
4911f4a2713aSLionel Sambuc // elements.
4912f4a2713aSLionel Sambuc if (!isa<ConstantArrayType>(AT))
4913f4a2713aSLionel Sambuc PType = PVDecl->getType();
4914f4a2713aSLionel Sambuc } else if (PType->isFunctionType())
4915f4a2713aSLionel Sambuc PType = PVDecl->getType();
4916f4a2713aSLionel Sambuc if (getLangOpts().EncodeExtendedBlockSig)
4917f4a2713aSLionel Sambuc getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType,
4918f4a2713aSLionel Sambuc S, true /*Extended*/);
4919f4a2713aSLionel Sambuc else
4920f4a2713aSLionel Sambuc getObjCEncodingForType(PType, S);
4921f4a2713aSLionel Sambuc S += charUnitsToString(ParmOffset);
4922f4a2713aSLionel Sambuc ParmOffset += getObjCEncodingTypeSize(PType);
4923f4a2713aSLionel Sambuc }
4924f4a2713aSLionel Sambuc
4925f4a2713aSLionel Sambuc return S;
4926f4a2713aSLionel Sambuc }
4927f4a2713aSLionel Sambuc
getObjCEncodingForFunctionDecl(const FunctionDecl * Decl,std::string & S)4928f4a2713aSLionel Sambuc bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
4929f4a2713aSLionel Sambuc std::string& S) {
4930f4a2713aSLionel Sambuc // Encode result type.
4931*0a6a1f1dSLionel Sambuc getObjCEncodingForType(Decl->getReturnType(), S);
4932f4a2713aSLionel Sambuc CharUnits ParmOffset;
4933f4a2713aSLionel Sambuc // Compute size of all parameters.
4934*0a6a1f1dSLionel Sambuc for (auto PI : Decl->params()) {
4935*0a6a1f1dSLionel Sambuc QualType PType = PI->getType();
4936f4a2713aSLionel Sambuc CharUnits sz = getObjCEncodingTypeSize(PType);
4937f4a2713aSLionel Sambuc if (sz.isZero())
4938f4a2713aSLionel Sambuc continue;
4939f4a2713aSLionel Sambuc
4940f4a2713aSLionel Sambuc assert (sz.isPositive() &&
4941f4a2713aSLionel Sambuc "getObjCEncodingForFunctionDecl - Incomplete param type");
4942f4a2713aSLionel Sambuc ParmOffset += sz;
4943f4a2713aSLionel Sambuc }
4944f4a2713aSLionel Sambuc S += charUnitsToString(ParmOffset);
4945f4a2713aSLionel Sambuc ParmOffset = CharUnits::Zero();
4946f4a2713aSLionel Sambuc
4947f4a2713aSLionel Sambuc // Argument types.
4948*0a6a1f1dSLionel Sambuc for (auto PVDecl : Decl->params()) {
4949f4a2713aSLionel Sambuc QualType PType = PVDecl->getOriginalType();
4950f4a2713aSLionel Sambuc if (const ArrayType *AT =
4951f4a2713aSLionel Sambuc dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
4952f4a2713aSLionel Sambuc // Use array's original type only if it has known number of
4953f4a2713aSLionel Sambuc // elements.
4954f4a2713aSLionel Sambuc if (!isa<ConstantArrayType>(AT))
4955f4a2713aSLionel Sambuc PType = PVDecl->getType();
4956f4a2713aSLionel Sambuc } else if (PType->isFunctionType())
4957f4a2713aSLionel Sambuc PType = PVDecl->getType();
4958f4a2713aSLionel Sambuc getObjCEncodingForType(PType, S);
4959f4a2713aSLionel Sambuc S += charUnitsToString(ParmOffset);
4960f4a2713aSLionel Sambuc ParmOffset += getObjCEncodingTypeSize(PType);
4961f4a2713aSLionel Sambuc }
4962f4a2713aSLionel Sambuc
4963f4a2713aSLionel Sambuc return false;
4964f4a2713aSLionel Sambuc }
4965f4a2713aSLionel Sambuc
4966f4a2713aSLionel Sambuc /// getObjCEncodingForMethodParameter - Return the encoded type for a single
4967f4a2713aSLionel Sambuc /// method parameter or return type. If Extended, include class names and
4968f4a2713aSLionel Sambuc /// block object types.
getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT,QualType T,std::string & S,bool Extended) const4969f4a2713aSLionel Sambuc void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT,
4970f4a2713aSLionel Sambuc QualType T, std::string& S,
4971f4a2713aSLionel Sambuc bool Extended) const {
4972f4a2713aSLionel Sambuc // Encode type qualifer, 'in', 'inout', etc. for the parameter.
4973f4a2713aSLionel Sambuc getObjCEncodingForTypeQualifier(QT, S);
4974f4a2713aSLionel Sambuc // Encode parameter type.
4975*0a6a1f1dSLionel Sambuc getObjCEncodingForTypeImpl(T, S, true, true, nullptr,
4976f4a2713aSLionel Sambuc true /*OutermostType*/,
4977f4a2713aSLionel Sambuc false /*EncodingProperty*/,
4978f4a2713aSLionel Sambuc false /*StructField*/,
4979f4a2713aSLionel Sambuc Extended /*EncodeBlockParameters*/,
4980f4a2713aSLionel Sambuc Extended /*EncodeClassNames*/);
4981f4a2713aSLionel Sambuc }
4982f4a2713aSLionel Sambuc
4983f4a2713aSLionel Sambuc /// getObjCEncodingForMethodDecl - Return the encoded type for this method
4984f4a2713aSLionel Sambuc /// declaration.
getObjCEncodingForMethodDecl(const ObjCMethodDecl * Decl,std::string & S,bool Extended) const4985f4a2713aSLionel Sambuc bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
4986f4a2713aSLionel Sambuc std::string& S,
4987f4a2713aSLionel Sambuc bool Extended) const {
4988f4a2713aSLionel Sambuc // FIXME: This is not very efficient.
4989f4a2713aSLionel Sambuc // Encode return type.
4990f4a2713aSLionel Sambuc getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(),
4991*0a6a1f1dSLionel Sambuc Decl->getReturnType(), S, Extended);
4992f4a2713aSLionel Sambuc // Compute size of all parameters.
4993f4a2713aSLionel Sambuc // Start with computing size of a pointer in number of bytes.
4994f4a2713aSLionel Sambuc // FIXME: There might(should) be a better way of doing this computation!
4995f4a2713aSLionel Sambuc SourceLocation Loc;
4996f4a2713aSLionel Sambuc CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
4997f4a2713aSLionel Sambuc // The first two arguments (self and _cmd) are pointers; account for
4998f4a2713aSLionel Sambuc // their size.
4999f4a2713aSLionel Sambuc CharUnits ParmOffset = 2 * PtrSize;
5000f4a2713aSLionel Sambuc for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
5001f4a2713aSLionel Sambuc E = Decl->sel_param_end(); PI != E; ++PI) {
5002f4a2713aSLionel Sambuc QualType PType = (*PI)->getType();
5003f4a2713aSLionel Sambuc CharUnits sz = getObjCEncodingTypeSize(PType);
5004f4a2713aSLionel Sambuc if (sz.isZero())
5005f4a2713aSLionel Sambuc continue;
5006f4a2713aSLionel Sambuc
5007f4a2713aSLionel Sambuc assert (sz.isPositive() &&
5008f4a2713aSLionel Sambuc "getObjCEncodingForMethodDecl - Incomplete param type");
5009f4a2713aSLionel Sambuc ParmOffset += sz;
5010f4a2713aSLionel Sambuc }
5011f4a2713aSLionel Sambuc S += charUnitsToString(ParmOffset);
5012f4a2713aSLionel Sambuc S += "@0:";
5013f4a2713aSLionel Sambuc S += charUnitsToString(PtrSize);
5014f4a2713aSLionel Sambuc
5015f4a2713aSLionel Sambuc // Argument types.
5016f4a2713aSLionel Sambuc ParmOffset = 2 * PtrSize;
5017f4a2713aSLionel Sambuc for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
5018f4a2713aSLionel Sambuc E = Decl->sel_param_end(); PI != E; ++PI) {
5019f4a2713aSLionel Sambuc const ParmVarDecl *PVDecl = *PI;
5020f4a2713aSLionel Sambuc QualType PType = PVDecl->getOriginalType();
5021f4a2713aSLionel Sambuc if (const ArrayType *AT =
5022f4a2713aSLionel Sambuc dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
5023f4a2713aSLionel Sambuc // Use array's original type only if it has known number of
5024f4a2713aSLionel Sambuc // elements.
5025f4a2713aSLionel Sambuc if (!isa<ConstantArrayType>(AT))
5026f4a2713aSLionel Sambuc PType = PVDecl->getType();
5027f4a2713aSLionel Sambuc } else if (PType->isFunctionType())
5028f4a2713aSLionel Sambuc PType = PVDecl->getType();
5029f4a2713aSLionel Sambuc getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(),
5030f4a2713aSLionel Sambuc PType, S, Extended);
5031f4a2713aSLionel Sambuc S += charUnitsToString(ParmOffset);
5032f4a2713aSLionel Sambuc ParmOffset += getObjCEncodingTypeSize(PType);
5033f4a2713aSLionel Sambuc }
5034f4a2713aSLionel Sambuc
5035f4a2713aSLionel Sambuc return false;
5036f4a2713aSLionel Sambuc }
5037f4a2713aSLionel Sambuc
5038*0a6a1f1dSLionel Sambuc ObjCPropertyImplDecl *
getObjCPropertyImplDeclForPropertyDecl(const ObjCPropertyDecl * PD,const Decl * Container) const5039*0a6a1f1dSLionel Sambuc ASTContext::getObjCPropertyImplDeclForPropertyDecl(
5040*0a6a1f1dSLionel Sambuc const ObjCPropertyDecl *PD,
5041*0a6a1f1dSLionel Sambuc const Decl *Container) const {
5042*0a6a1f1dSLionel Sambuc if (!Container)
5043*0a6a1f1dSLionel Sambuc return nullptr;
5044*0a6a1f1dSLionel Sambuc if (const ObjCCategoryImplDecl *CID =
5045*0a6a1f1dSLionel Sambuc dyn_cast<ObjCCategoryImplDecl>(Container)) {
5046*0a6a1f1dSLionel Sambuc for (auto *PID : CID->property_impls())
5047*0a6a1f1dSLionel Sambuc if (PID->getPropertyDecl() == PD)
5048*0a6a1f1dSLionel Sambuc return PID;
5049*0a6a1f1dSLionel Sambuc } else {
5050*0a6a1f1dSLionel Sambuc const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
5051*0a6a1f1dSLionel Sambuc for (auto *PID : OID->property_impls())
5052*0a6a1f1dSLionel Sambuc if (PID->getPropertyDecl() == PD)
5053*0a6a1f1dSLionel Sambuc return PID;
5054*0a6a1f1dSLionel Sambuc }
5055*0a6a1f1dSLionel Sambuc return nullptr;
5056*0a6a1f1dSLionel Sambuc }
5057*0a6a1f1dSLionel Sambuc
5058f4a2713aSLionel Sambuc /// getObjCEncodingForPropertyDecl - Return the encoded type for this
5059f4a2713aSLionel Sambuc /// property declaration. If non-NULL, Container must be either an
5060f4a2713aSLionel Sambuc /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
5061f4a2713aSLionel Sambuc /// NULL when getting encodings for protocol properties.
5062f4a2713aSLionel Sambuc /// Property attributes are stored as a comma-delimited C string. The simple
5063f4a2713aSLionel Sambuc /// attributes readonly and bycopy are encoded as single characters. The
5064f4a2713aSLionel Sambuc /// parametrized attributes, getter=name, setter=name, and ivar=name, are
5065f4a2713aSLionel Sambuc /// encoded as single characters, followed by an identifier. Property types
5066f4a2713aSLionel Sambuc /// are also encoded as a parametrized attribute. The characters used to encode
5067f4a2713aSLionel Sambuc /// these attributes are defined by the following enumeration:
5068f4a2713aSLionel Sambuc /// @code
5069f4a2713aSLionel Sambuc /// enum PropertyAttributes {
5070f4a2713aSLionel Sambuc /// kPropertyReadOnly = 'R', // property is read-only.
5071f4a2713aSLionel Sambuc /// kPropertyBycopy = 'C', // property is a copy of the value last assigned
5072f4a2713aSLionel Sambuc /// kPropertyByref = '&', // property is a reference to the value last assigned
5073f4a2713aSLionel Sambuc /// kPropertyDynamic = 'D', // property is dynamic
5074f4a2713aSLionel Sambuc /// kPropertyGetter = 'G', // followed by getter selector name
5075f4a2713aSLionel Sambuc /// kPropertySetter = 'S', // followed by setter selector name
5076f4a2713aSLionel Sambuc /// kPropertyInstanceVariable = 'V' // followed by instance variable name
5077f4a2713aSLionel Sambuc /// kPropertyType = 'T' // followed by old-style type encoding.
5078f4a2713aSLionel Sambuc /// kPropertyWeak = 'W' // 'weak' property
5079f4a2713aSLionel Sambuc /// kPropertyStrong = 'P' // property GC'able
5080f4a2713aSLionel Sambuc /// kPropertyNonAtomic = 'N' // property non-atomic
5081f4a2713aSLionel Sambuc /// };
5082f4a2713aSLionel Sambuc /// @endcode
getObjCEncodingForPropertyDecl(const ObjCPropertyDecl * PD,const Decl * Container,std::string & S) const5083f4a2713aSLionel Sambuc void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
5084f4a2713aSLionel Sambuc const Decl *Container,
5085f4a2713aSLionel Sambuc std::string& S) const {
5086f4a2713aSLionel Sambuc // Collect information from the property implementation decl(s).
5087f4a2713aSLionel Sambuc bool Dynamic = false;
5088*0a6a1f1dSLionel Sambuc ObjCPropertyImplDecl *SynthesizePID = nullptr;
5089f4a2713aSLionel Sambuc
5090*0a6a1f1dSLionel Sambuc if (ObjCPropertyImplDecl *PropertyImpDecl =
5091*0a6a1f1dSLionel Sambuc getObjCPropertyImplDeclForPropertyDecl(PD, Container)) {
5092*0a6a1f1dSLionel Sambuc if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
5093f4a2713aSLionel Sambuc Dynamic = true;
5094*0a6a1f1dSLionel Sambuc else
5095*0a6a1f1dSLionel Sambuc SynthesizePID = PropertyImpDecl;
5096f4a2713aSLionel Sambuc }
5097f4a2713aSLionel Sambuc
5098f4a2713aSLionel Sambuc // FIXME: This is not very efficient.
5099f4a2713aSLionel Sambuc S = "T";
5100f4a2713aSLionel Sambuc
5101f4a2713aSLionel Sambuc // Encode result type.
5102f4a2713aSLionel Sambuc // GCC has some special rules regarding encoding of properties which
5103f4a2713aSLionel Sambuc // closely resembles encoding of ivars.
5104*0a6a1f1dSLionel Sambuc getObjCEncodingForPropertyType(PD->getType(), S);
5105f4a2713aSLionel Sambuc
5106f4a2713aSLionel Sambuc if (PD->isReadOnly()) {
5107f4a2713aSLionel Sambuc S += ",R";
5108f4a2713aSLionel Sambuc if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy)
5109f4a2713aSLionel Sambuc S += ",C";
5110f4a2713aSLionel Sambuc if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain)
5111f4a2713aSLionel Sambuc S += ",&";
5112*0a6a1f1dSLionel Sambuc if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
5113*0a6a1f1dSLionel Sambuc S += ",W";
5114f4a2713aSLionel Sambuc } else {
5115f4a2713aSLionel Sambuc switch (PD->getSetterKind()) {
5116f4a2713aSLionel Sambuc case ObjCPropertyDecl::Assign: break;
5117f4a2713aSLionel Sambuc case ObjCPropertyDecl::Copy: S += ",C"; break;
5118f4a2713aSLionel Sambuc case ObjCPropertyDecl::Retain: S += ",&"; break;
5119f4a2713aSLionel Sambuc case ObjCPropertyDecl::Weak: S += ",W"; break;
5120f4a2713aSLionel Sambuc }
5121f4a2713aSLionel Sambuc }
5122f4a2713aSLionel Sambuc
5123f4a2713aSLionel Sambuc // It really isn't clear at all what this means, since properties
5124f4a2713aSLionel Sambuc // are "dynamic by default".
5125f4a2713aSLionel Sambuc if (Dynamic)
5126f4a2713aSLionel Sambuc S += ",D";
5127f4a2713aSLionel Sambuc
5128f4a2713aSLionel Sambuc if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
5129f4a2713aSLionel Sambuc S += ",N";
5130f4a2713aSLionel Sambuc
5131f4a2713aSLionel Sambuc if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
5132f4a2713aSLionel Sambuc S += ",G";
5133f4a2713aSLionel Sambuc S += PD->getGetterName().getAsString();
5134f4a2713aSLionel Sambuc }
5135f4a2713aSLionel Sambuc
5136f4a2713aSLionel Sambuc if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
5137f4a2713aSLionel Sambuc S += ",S";
5138f4a2713aSLionel Sambuc S += PD->getSetterName().getAsString();
5139f4a2713aSLionel Sambuc }
5140f4a2713aSLionel Sambuc
5141f4a2713aSLionel Sambuc if (SynthesizePID) {
5142f4a2713aSLionel Sambuc const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
5143f4a2713aSLionel Sambuc S += ",V";
5144f4a2713aSLionel Sambuc S += OID->getNameAsString();
5145f4a2713aSLionel Sambuc }
5146f4a2713aSLionel Sambuc
5147f4a2713aSLionel Sambuc // FIXME: OBJCGC: weak & strong
5148f4a2713aSLionel Sambuc }
5149f4a2713aSLionel Sambuc
5150f4a2713aSLionel Sambuc /// getLegacyIntegralTypeEncoding -
5151f4a2713aSLionel Sambuc /// Another legacy compatibility encoding: 32-bit longs are encoded as
5152f4a2713aSLionel Sambuc /// 'l' or 'L' , but not always. For typedefs, we need to use
5153f4a2713aSLionel Sambuc /// 'i' or 'I' instead if encoding a struct field, or a pointer!
5154f4a2713aSLionel Sambuc ///
getLegacyIntegralTypeEncoding(QualType & PointeeTy) const5155f4a2713aSLionel Sambuc void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
5156f4a2713aSLionel Sambuc if (isa<TypedefType>(PointeeTy.getTypePtr())) {
5157f4a2713aSLionel Sambuc if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
5158f4a2713aSLionel Sambuc if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
5159f4a2713aSLionel Sambuc PointeeTy = UnsignedIntTy;
5160f4a2713aSLionel Sambuc else
5161f4a2713aSLionel Sambuc if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
5162f4a2713aSLionel Sambuc PointeeTy = IntTy;
5163f4a2713aSLionel Sambuc }
5164f4a2713aSLionel Sambuc }
5165f4a2713aSLionel Sambuc }
5166f4a2713aSLionel Sambuc
getObjCEncodingForType(QualType T,std::string & S,const FieldDecl * Field,QualType * NotEncodedT) const5167f4a2713aSLionel Sambuc void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
5168*0a6a1f1dSLionel Sambuc const FieldDecl *Field,
5169*0a6a1f1dSLionel Sambuc QualType *NotEncodedT) const {
5170f4a2713aSLionel Sambuc // We follow the behavior of gcc, expanding structures which are
5171f4a2713aSLionel Sambuc // directly pointed to, and expanding embedded structures. Note that
5172f4a2713aSLionel Sambuc // these rules are sufficient to prevent recursive encoding of the
5173f4a2713aSLionel Sambuc // same type.
5174f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(T, S, true, true, Field,
5175*0a6a1f1dSLionel Sambuc true /* outermost type */, false, false,
5176*0a6a1f1dSLionel Sambuc false, false, false, NotEncodedT);
5177*0a6a1f1dSLionel Sambuc }
5178*0a6a1f1dSLionel Sambuc
getObjCEncodingForPropertyType(QualType T,std::string & S) const5179*0a6a1f1dSLionel Sambuc void ASTContext::getObjCEncodingForPropertyType(QualType T,
5180*0a6a1f1dSLionel Sambuc std::string& S) const {
5181*0a6a1f1dSLionel Sambuc // Encode result type.
5182*0a6a1f1dSLionel Sambuc // GCC has some special rules regarding encoding of properties which
5183*0a6a1f1dSLionel Sambuc // closely resembles encoding of ivars.
5184*0a6a1f1dSLionel Sambuc getObjCEncodingForTypeImpl(T, S, true, true, nullptr,
5185*0a6a1f1dSLionel Sambuc true /* outermost type */,
5186*0a6a1f1dSLionel Sambuc true /* encoding property */);
5187f4a2713aSLionel Sambuc }
5188f4a2713aSLionel Sambuc
getObjCEncodingForPrimitiveKind(const ASTContext * C,BuiltinType::Kind kind)5189f4a2713aSLionel Sambuc static char getObjCEncodingForPrimitiveKind(const ASTContext *C,
5190f4a2713aSLionel Sambuc BuiltinType::Kind kind) {
5191f4a2713aSLionel Sambuc switch (kind) {
5192f4a2713aSLionel Sambuc case BuiltinType::Void: return 'v';
5193f4a2713aSLionel Sambuc case BuiltinType::Bool: return 'B';
5194f4a2713aSLionel Sambuc case BuiltinType::Char_U:
5195f4a2713aSLionel Sambuc case BuiltinType::UChar: return 'C';
5196f4a2713aSLionel Sambuc case BuiltinType::Char16:
5197f4a2713aSLionel Sambuc case BuiltinType::UShort: return 'S';
5198f4a2713aSLionel Sambuc case BuiltinType::Char32:
5199f4a2713aSLionel Sambuc case BuiltinType::UInt: return 'I';
5200f4a2713aSLionel Sambuc case BuiltinType::ULong:
5201f4a2713aSLionel Sambuc return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q';
5202f4a2713aSLionel Sambuc case BuiltinType::UInt128: return 'T';
5203f4a2713aSLionel Sambuc case BuiltinType::ULongLong: return 'Q';
5204f4a2713aSLionel Sambuc case BuiltinType::Char_S:
5205f4a2713aSLionel Sambuc case BuiltinType::SChar: return 'c';
5206f4a2713aSLionel Sambuc case BuiltinType::Short: return 's';
5207f4a2713aSLionel Sambuc case BuiltinType::WChar_S:
5208f4a2713aSLionel Sambuc case BuiltinType::WChar_U:
5209f4a2713aSLionel Sambuc case BuiltinType::Int: return 'i';
5210f4a2713aSLionel Sambuc case BuiltinType::Long:
5211f4a2713aSLionel Sambuc return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q';
5212f4a2713aSLionel Sambuc case BuiltinType::LongLong: return 'q';
5213f4a2713aSLionel Sambuc case BuiltinType::Int128: return 't';
5214f4a2713aSLionel Sambuc case BuiltinType::Float: return 'f';
5215f4a2713aSLionel Sambuc case BuiltinType::Double: return 'd';
5216f4a2713aSLionel Sambuc case BuiltinType::LongDouble: return 'D';
5217f4a2713aSLionel Sambuc case BuiltinType::NullPtr: return '*'; // like char*
5218f4a2713aSLionel Sambuc
5219f4a2713aSLionel Sambuc case BuiltinType::Half:
5220f4a2713aSLionel Sambuc // FIXME: potentially need @encodes for these!
5221f4a2713aSLionel Sambuc return ' ';
5222f4a2713aSLionel Sambuc
5223f4a2713aSLionel Sambuc case BuiltinType::ObjCId:
5224f4a2713aSLionel Sambuc case BuiltinType::ObjCClass:
5225f4a2713aSLionel Sambuc case BuiltinType::ObjCSel:
5226f4a2713aSLionel Sambuc llvm_unreachable("@encoding ObjC primitive type");
5227f4a2713aSLionel Sambuc
5228f4a2713aSLionel Sambuc // OpenCL and placeholder types don't need @encodings.
5229f4a2713aSLionel Sambuc case BuiltinType::OCLImage1d:
5230f4a2713aSLionel Sambuc case BuiltinType::OCLImage1dArray:
5231f4a2713aSLionel Sambuc case BuiltinType::OCLImage1dBuffer:
5232f4a2713aSLionel Sambuc case BuiltinType::OCLImage2d:
5233f4a2713aSLionel Sambuc case BuiltinType::OCLImage2dArray:
5234f4a2713aSLionel Sambuc case BuiltinType::OCLImage3d:
5235f4a2713aSLionel Sambuc case BuiltinType::OCLEvent:
5236f4a2713aSLionel Sambuc case BuiltinType::OCLSampler:
5237f4a2713aSLionel Sambuc case BuiltinType::Dependent:
5238f4a2713aSLionel Sambuc #define BUILTIN_TYPE(KIND, ID)
5239f4a2713aSLionel Sambuc #define PLACEHOLDER_TYPE(KIND, ID) \
5240f4a2713aSLionel Sambuc case BuiltinType::KIND:
5241f4a2713aSLionel Sambuc #include "clang/AST/BuiltinTypes.def"
5242f4a2713aSLionel Sambuc llvm_unreachable("invalid builtin type for @encode");
5243f4a2713aSLionel Sambuc }
5244f4a2713aSLionel Sambuc llvm_unreachable("invalid BuiltinType::Kind value");
5245f4a2713aSLionel Sambuc }
5246f4a2713aSLionel Sambuc
ObjCEncodingForEnumType(const ASTContext * C,const EnumType * ET)5247f4a2713aSLionel Sambuc static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) {
5248f4a2713aSLionel Sambuc EnumDecl *Enum = ET->getDecl();
5249f4a2713aSLionel Sambuc
5250f4a2713aSLionel Sambuc // The encoding of an non-fixed enum type is always 'i', regardless of size.
5251f4a2713aSLionel Sambuc if (!Enum->isFixed())
5252f4a2713aSLionel Sambuc return 'i';
5253f4a2713aSLionel Sambuc
5254f4a2713aSLionel Sambuc // The encoding of a fixed enum type matches its fixed underlying type.
5255f4a2713aSLionel Sambuc const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>();
5256f4a2713aSLionel Sambuc return getObjCEncodingForPrimitiveKind(C, BT->getKind());
5257f4a2713aSLionel Sambuc }
5258f4a2713aSLionel Sambuc
EncodeBitField(const ASTContext * Ctx,std::string & S,QualType T,const FieldDecl * FD)5259f4a2713aSLionel Sambuc static void EncodeBitField(const ASTContext *Ctx, std::string& S,
5260f4a2713aSLionel Sambuc QualType T, const FieldDecl *FD) {
5261f4a2713aSLionel Sambuc assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl");
5262f4a2713aSLionel Sambuc S += 'b';
5263f4a2713aSLionel Sambuc // The NeXT runtime encodes bit fields as b followed by the number of bits.
5264f4a2713aSLionel Sambuc // The GNU runtime requires more information; bitfields are encoded as b,
5265f4a2713aSLionel Sambuc // then the offset (in bits) of the first element, then the type of the
5266f4a2713aSLionel Sambuc // bitfield, then the size in bits. For example, in this structure:
5267f4a2713aSLionel Sambuc //
5268f4a2713aSLionel Sambuc // struct
5269f4a2713aSLionel Sambuc // {
5270f4a2713aSLionel Sambuc // int integer;
5271f4a2713aSLionel Sambuc // int flags:2;
5272f4a2713aSLionel Sambuc // };
5273f4a2713aSLionel Sambuc // On a 32-bit system, the encoding for flags would be b2 for the NeXT
5274f4a2713aSLionel Sambuc // runtime, but b32i2 for the GNU runtime. The reason for this extra
5275f4a2713aSLionel Sambuc // information is not especially sensible, but we're stuck with it for
5276f4a2713aSLionel Sambuc // compatibility with GCC, although providing it breaks anything that
5277f4a2713aSLionel Sambuc // actually uses runtime introspection and wants to work on both runtimes...
5278f4a2713aSLionel Sambuc if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) {
5279f4a2713aSLionel Sambuc const RecordDecl *RD = FD->getParent();
5280f4a2713aSLionel Sambuc const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
5281f4a2713aSLionel Sambuc S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex()));
5282f4a2713aSLionel Sambuc if (const EnumType *ET = T->getAs<EnumType>())
5283f4a2713aSLionel Sambuc S += ObjCEncodingForEnumType(Ctx, ET);
5284f4a2713aSLionel Sambuc else {
5285f4a2713aSLionel Sambuc const BuiltinType *BT = T->castAs<BuiltinType>();
5286f4a2713aSLionel Sambuc S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind());
5287f4a2713aSLionel Sambuc }
5288f4a2713aSLionel Sambuc }
5289f4a2713aSLionel Sambuc S += llvm::utostr(FD->getBitWidthValue(*Ctx));
5290f4a2713aSLionel Sambuc }
5291f4a2713aSLionel Sambuc
5292f4a2713aSLionel Sambuc // FIXME: Use SmallString for accumulating string.
getObjCEncodingForTypeImpl(QualType T,std::string & S,bool ExpandPointedToStructures,bool ExpandStructures,const FieldDecl * FD,bool OutermostType,bool EncodingProperty,bool StructField,bool EncodeBlockParameters,bool EncodeClassNames,bool EncodePointerToObjCTypedef,QualType * NotEncodedT) const5293f4a2713aSLionel Sambuc void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
5294f4a2713aSLionel Sambuc bool ExpandPointedToStructures,
5295f4a2713aSLionel Sambuc bool ExpandStructures,
5296f4a2713aSLionel Sambuc const FieldDecl *FD,
5297f4a2713aSLionel Sambuc bool OutermostType,
5298f4a2713aSLionel Sambuc bool EncodingProperty,
5299f4a2713aSLionel Sambuc bool StructField,
5300f4a2713aSLionel Sambuc bool EncodeBlockParameters,
5301f4a2713aSLionel Sambuc bool EncodeClassNames,
5302*0a6a1f1dSLionel Sambuc bool EncodePointerToObjCTypedef,
5303*0a6a1f1dSLionel Sambuc QualType *NotEncodedT) const {
5304f4a2713aSLionel Sambuc CanQualType CT = getCanonicalType(T);
5305f4a2713aSLionel Sambuc switch (CT->getTypeClass()) {
5306f4a2713aSLionel Sambuc case Type::Builtin:
5307f4a2713aSLionel Sambuc case Type::Enum:
5308f4a2713aSLionel Sambuc if (FD && FD->isBitField())
5309f4a2713aSLionel Sambuc return EncodeBitField(this, S, T, FD);
5310f4a2713aSLionel Sambuc if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT))
5311f4a2713aSLionel Sambuc S += getObjCEncodingForPrimitiveKind(this, BT->getKind());
5312f4a2713aSLionel Sambuc else
5313f4a2713aSLionel Sambuc S += ObjCEncodingForEnumType(this, cast<EnumType>(CT));
5314f4a2713aSLionel Sambuc return;
5315f4a2713aSLionel Sambuc
5316f4a2713aSLionel Sambuc case Type::Complex: {
5317f4a2713aSLionel Sambuc const ComplexType *CT = T->castAs<ComplexType>();
5318f4a2713aSLionel Sambuc S += 'j';
5319*0a6a1f1dSLionel Sambuc getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, nullptr);
5320f4a2713aSLionel Sambuc return;
5321f4a2713aSLionel Sambuc }
5322f4a2713aSLionel Sambuc
5323f4a2713aSLionel Sambuc case Type::Atomic: {
5324f4a2713aSLionel Sambuc const AtomicType *AT = T->castAs<AtomicType>();
5325f4a2713aSLionel Sambuc S += 'A';
5326*0a6a1f1dSLionel Sambuc getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, nullptr);
5327f4a2713aSLionel Sambuc return;
5328f4a2713aSLionel Sambuc }
5329f4a2713aSLionel Sambuc
5330f4a2713aSLionel Sambuc // encoding for pointer or reference types.
5331f4a2713aSLionel Sambuc case Type::Pointer:
5332f4a2713aSLionel Sambuc case Type::LValueReference:
5333f4a2713aSLionel Sambuc case Type::RValueReference: {
5334f4a2713aSLionel Sambuc QualType PointeeTy;
5335f4a2713aSLionel Sambuc if (isa<PointerType>(CT)) {
5336f4a2713aSLionel Sambuc const PointerType *PT = T->castAs<PointerType>();
5337f4a2713aSLionel Sambuc if (PT->isObjCSelType()) {
5338f4a2713aSLionel Sambuc S += ':';
5339f4a2713aSLionel Sambuc return;
5340f4a2713aSLionel Sambuc }
5341f4a2713aSLionel Sambuc PointeeTy = PT->getPointeeType();
5342f4a2713aSLionel Sambuc } else {
5343f4a2713aSLionel Sambuc PointeeTy = T->castAs<ReferenceType>()->getPointeeType();
5344f4a2713aSLionel Sambuc }
5345f4a2713aSLionel Sambuc
5346f4a2713aSLionel Sambuc bool isReadOnly = false;
5347f4a2713aSLionel Sambuc // For historical/compatibility reasons, the read-only qualifier of the
5348f4a2713aSLionel Sambuc // pointee gets emitted _before_ the '^'. The read-only qualifier of
5349f4a2713aSLionel Sambuc // the pointer itself gets ignored, _unless_ we are looking at a typedef!
5350f4a2713aSLionel Sambuc // Also, do not emit the 'r' for anything but the outermost type!
5351f4a2713aSLionel Sambuc if (isa<TypedefType>(T.getTypePtr())) {
5352f4a2713aSLionel Sambuc if (OutermostType && T.isConstQualified()) {
5353f4a2713aSLionel Sambuc isReadOnly = true;
5354f4a2713aSLionel Sambuc S += 'r';
5355f4a2713aSLionel Sambuc }
5356f4a2713aSLionel Sambuc } else if (OutermostType) {
5357f4a2713aSLionel Sambuc QualType P = PointeeTy;
5358f4a2713aSLionel Sambuc while (P->getAs<PointerType>())
5359f4a2713aSLionel Sambuc P = P->getAs<PointerType>()->getPointeeType();
5360f4a2713aSLionel Sambuc if (P.isConstQualified()) {
5361f4a2713aSLionel Sambuc isReadOnly = true;
5362f4a2713aSLionel Sambuc S += 'r';
5363f4a2713aSLionel Sambuc }
5364f4a2713aSLionel Sambuc }
5365f4a2713aSLionel Sambuc if (isReadOnly) {
5366f4a2713aSLionel Sambuc // Another legacy compatibility encoding. Some ObjC qualifier and type
5367f4a2713aSLionel Sambuc // combinations need to be rearranged.
5368f4a2713aSLionel Sambuc // Rewrite "in const" from "nr" to "rn"
5369f4a2713aSLionel Sambuc if (StringRef(S).endswith("nr"))
5370f4a2713aSLionel Sambuc S.replace(S.end()-2, S.end(), "rn");
5371f4a2713aSLionel Sambuc }
5372f4a2713aSLionel Sambuc
5373f4a2713aSLionel Sambuc if (PointeeTy->isCharType()) {
5374f4a2713aSLionel Sambuc // char pointer types should be encoded as '*' unless it is a
5375f4a2713aSLionel Sambuc // type that has been typedef'd to 'BOOL'.
5376f4a2713aSLionel Sambuc if (!isTypeTypedefedAsBOOL(PointeeTy)) {
5377f4a2713aSLionel Sambuc S += '*';
5378f4a2713aSLionel Sambuc return;
5379f4a2713aSLionel Sambuc }
5380f4a2713aSLionel Sambuc } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
5381f4a2713aSLionel Sambuc // GCC binary compat: Need to convert "struct objc_class *" to "#".
5382f4a2713aSLionel Sambuc if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
5383f4a2713aSLionel Sambuc S += '#';
5384f4a2713aSLionel Sambuc return;
5385f4a2713aSLionel Sambuc }
5386f4a2713aSLionel Sambuc // GCC binary compat: Need to convert "struct objc_object *" to "@".
5387f4a2713aSLionel Sambuc if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
5388f4a2713aSLionel Sambuc S += '@';
5389f4a2713aSLionel Sambuc return;
5390f4a2713aSLionel Sambuc }
5391f4a2713aSLionel Sambuc // fall through...
5392f4a2713aSLionel Sambuc }
5393f4a2713aSLionel Sambuc S += '^';
5394f4a2713aSLionel Sambuc getLegacyIntegralTypeEncoding(PointeeTy);
5395f4a2713aSLionel Sambuc
5396f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
5397*0a6a1f1dSLionel Sambuc nullptr, false, false, false, false, false, false,
5398*0a6a1f1dSLionel Sambuc NotEncodedT);
5399f4a2713aSLionel Sambuc return;
5400f4a2713aSLionel Sambuc }
5401f4a2713aSLionel Sambuc
5402f4a2713aSLionel Sambuc case Type::ConstantArray:
5403f4a2713aSLionel Sambuc case Type::IncompleteArray:
5404f4a2713aSLionel Sambuc case Type::VariableArray: {
5405f4a2713aSLionel Sambuc const ArrayType *AT = cast<ArrayType>(CT);
5406f4a2713aSLionel Sambuc
5407f4a2713aSLionel Sambuc if (isa<IncompleteArrayType>(AT) && !StructField) {
5408f4a2713aSLionel Sambuc // Incomplete arrays are encoded as a pointer to the array element.
5409f4a2713aSLionel Sambuc S += '^';
5410f4a2713aSLionel Sambuc
5411f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(AT->getElementType(), S,
5412f4a2713aSLionel Sambuc false, ExpandStructures, FD);
5413f4a2713aSLionel Sambuc } else {
5414f4a2713aSLionel Sambuc S += '[';
5415f4a2713aSLionel Sambuc
5416f4a2713aSLionel Sambuc if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
5417f4a2713aSLionel Sambuc S += llvm::utostr(CAT->getSize().getZExtValue());
5418f4a2713aSLionel Sambuc else {
5419f4a2713aSLionel Sambuc //Variable length arrays are encoded as a regular array with 0 elements.
5420f4a2713aSLionel Sambuc assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
5421f4a2713aSLionel Sambuc "Unknown array type!");
5422f4a2713aSLionel Sambuc S += '0';
5423f4a2713aSLionel Sambuc }
5424f4a2713aSLionel Sambuc
5425f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(AT->getElementType(), S,
5426*0a6a1f1dSLionel Sambuc false, ExpandStructures, FD,
5427*0a6a1f1dSLionel Sambuc false, false, false, false, false, false,
5428*0a6a1f1dSLionel Sambuc NotEncodedT);
5429f4a2713aSLionel Sambuc S += ']';
5430f4a2713aSLionel Sambuc }
5431f4a2713aSLionel Sambuc return;
5432f4a2713aSLionel Sambuc }
5433f4a2713aSLionel Sambuc
5434f4a2713aSLionel Sambuc case Type::FunctionNoProto:
5435f4a2713aSLionel Sambuc case Type::FunctionProto:
5436f4a2713aSLionel Sambuc S += '?';
5437f4a2713aSLionel Sambuc return;
5438f4a2713aSLionel Sambuc
5439f4a2713aSLionel Sambuc case Type::Record: {
5440f4a2713aSLionel Sambuc RecordDecl *RDecl = cast<RecordType>(CT)->getDecl();
5441f4a2713aSLionel Sambuc S += RDecl->isUnion() ? '(' : '{';
5442f4a2713aSLionel Sambuc // Anonymous structures print as '?'
5443f4a2713aSLionel Sambuc if (const IdentifierInfo *II = RDecl->getIdentifier()) {
5444f4a2713aSLionel Sambuc S += II->getName();
5445f4a2713aSLionel Sambuc if (ClassTemplateSpecializationDecl *Spec
5446f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
5447f4a2713aSLionel Sambuc const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
5448f4a2713aSLionel Sambuc llvm::raw_string_ostream OS(S);
5449f4a2713aSLionel Sambuc TemplateSpecializationType::PrintTemplateArgumentList(OS,
5450f4a2713aSLionel Sambuc TemplateArgs.data(),
5451f4a2713aSLionel Sambuc TemplateArgs.size(),
5452f4a2713aSLionel Sambuc (*this).getPrintingPolicy());
5453f4a2713aSLionel Sambuc }
5454f4a2713aSLionel Sambuc } else {
5455f4a2713aSLionel Sambuc S += '?';
5456f4a2713aSLionel Sambuc }
5457f4a2713aSLionel Sambuc if (ExpandStructures) {
5458f4a2713aSLionel Sambuc S += '=';
5459f4a2713aSLionel Sambuc if (!RDecl->isUnion()) {
5460*0a6a1f1dSLionel Sambuc getObjCEncodingForStructureImpl(RDecl, S, FD, true, NotEncodedT);
5461f4a2713aSLionel Sambuc } else {
5462*0a6a1f1dSLionel Sambuc for (const auto *Field : RDecl->fields()) {
5463f4a2713aSLionel Sambuc if (FD) {
5464f4a2713aSLionel Sambuc S += '"';
5465f4a2713aSLionel Sambuc S += Field->getNameAsString();
5466f4a2713aSLionel Sambuc S += '"';
5467f4a2713aSLionel Sambuc }
5468f4a2713aSLionel Sambuc
5469f4a2713aSLionel Sambuc // Special case bit-fields.
5470f4a2713aSLionel Sambuc if (Field->isBitField()) {
5471f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
5472*0a6a1f1dSLionel Sambuc Field);
5473f4a2713aSLionel Sambuc } else {
5474f4a2713aSLionel Sambuc QualType qt = Field->getType();
5475f4a2713aSLionel Sambuc getLegacyIntegralTypeEncoding(qt);
5476f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(qt, S, false, true,
5477f4a2713aSLionel Sambuc FD, /*OutermostType*/false,
5478f4a2713aSLionel Sambuc /*EncodingProperty*/false,
5479*0a6a1f1dSLionel Sambuc /*StructField*/true,
5480*0a6a1f1dSLionel Sambuc false, false, false, NotEncodedT);
5481f4a2713aSLionel Sambuc }
5482f4a2713aSLionel Sambuc }
5483f4a2713aSLionel Sambuc }
5484f4a2713aSLionel Sambuc }
5485f4a2713aSLionel Sambuc S += RDecl->isUnion() ? ')' : '}';
5486f4a2713aSLionel Sambuc return;
5487f4a2713aSLionel Sambuc }
5488f4a2713aSLionel Sambuc
5489f4a2713aSLionel Sambuc case Type::BlockPointer: {
5490f4a2713aSLionel Sambuc const BlockPointerType *BT = T->castAs<BlockPointerType>();
5491f4a2713aSLionel Sambuc S += "@?"; // Unlike a pointer-to-function, which is "^?".
5492f4a2713aSLionel Sambuc if (EncodeBlockParameters) {
5493f4a2713aSLionel Sambuc const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>();
5494f4a2713aSLionel Sambuc
5495f4a2713aSLionel Sambuc S += '<';
5496f4a2713aSLionel Sambuc // Block return type
5497*0a6a1f1dSLionel Sambuc getObjCEncodingForTypeImpl(
5498*0a6a1f1dSLionel Sambuc FT->getReturnType(), S, ExpandPointedToStructures, ExpandStructures,
5499*0a6a1f1dSLionel Sambuc FD, false /* OutermostType */, EncodingProperty,
5500*0a6a1f1dSLionel Sambuc false /* StructField */, EncodeBlockParameters, EncodeClassNames, false,
5501*0a6a1f1dSLionel Sambuc NotEncodedT);
5502f4a2713aSLionel Sambuc // Block self
5503f4a2713aSLionel Sambuc S += "@?";
5504f4a2713aSLionel Sambuc // Block parameters
5505f4a2713aSLionel Sambuc if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
5506*0a6a1f1dSLionel Sambuc for (const auto &I : FPT->param_types())
5507*0a6a1f1dSLionel Sambuc getObjCEncodingForTypeImpl(
5508*0a6a1f1dSLionel Sambuc I, S, ExpandPointedToStructures, ExpandStructures, FD,
5509*0a6a1f1dSLionel Sambuc false /* OutermostType */, EncodingProperty,
5510*0a6a1f1dSLionel Sambuc false /* StructField */, EncodeBlockParameters, EncodeClassNames,
5511*0a6a1f1dSLionel Sambuc false, NotEncodedT);
5512f4a2713aSLionel Sambuc }
5513f4a2713aSLionel Sambuc S += '>';
5514f4a2713aSLionel Sambuc }
5515f4a2713aSLionel Sambuc return;
5516f4a2713aSLionel Sambuc }
5517f4a2713aSLionel Sambuc
5518*0a6a1f1dSLionel Sambuc case Type::ObjCObject: {
5519*0a6a1f1dSLionel Sambuc // hack to match legacy encoding of *id and *Class
5520*0a6a1f1dSLionel Sambuc QualType Ty = getObjCObjectPointerType(CT);
5521*0a6a1f1dSLionel Sambuc if (Ty->isObjCIdType()) {
5522*0a6a1f1dSLionel Sambuc S += "{objc_object=}";
5523*0a6a1f1dSLionel Sambuc return;
5524*0a6a1f1dSLionel Sambuc }
5525*0a6a1f1dSLionel Sambuc else if (Ty->isObjCClassType()) {
5526*0a6a1f1dSLionel Sambuc S += "{objc_class=}";
5527*0a6a1f1dSLionel Sambuc return;
5528*0a6a1f1dSLionel Sambuc }
5529*0a6a1f1dSLionel Sambuc }
5530*0a6a1f1dSLionel Sambuc
5531f4a2713aSLionel Sambuc case Type::ObjCInterface: {
5532f4a2713aSLionel Sambuc // Ignore protocol qualifiers when mangling at this level.
5533f4a2713aSLionel Sambuc T = T->castAs<ObjCObjectType>()->getBaseType();
5534f4a2713aSLionel Sambuc
5535f4a2713aSLionel Sambuc // The assumption seems to be that this assert will succeed
5536f4a2713aSLionel Sambuc // because nested levels will have filtered out 'id' and 'Class'.
5537f4a2713aSLionel Sambuc const ObjCInterfaceType *OIT = T->castAs<ObjCInterfaceType>();
5538f4a2713aSLionel Sambuc // @encode(class_name)
5539f4a2713aSLionel Sambuc ObjCInterfaceDecl *OI = OIT->getDecl();
5540f4a2713aSLionel Sambuc S += '{';
5541f4a2713aSLionel Sambuc const IdentifierInfo *II = OI->getIdentifier();
5542f4a2713aSLionel Sambuc S += II->getName();
5543f4a2713aSLionel Sambuc S += '=';
5544f4a2713aSLionel Sambuc SmallVector<const ObjCIvarDecl*, 32> Ivars;
5545f4a2713aSLionel Sambuc DeepCollectObjCIvars(OI, true, Ivars);
5546f4a2713aSLionel Sambuc for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
5547f4a2713aSLionel Sambuc const FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
5548f4a2713aSLionel Sambuc if (Field->isBitField())
5549f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
5550f4a2713aSLionel Sambuc else
5551f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD,
5552f4a2713aSLionel Sambuc false, false, false, false, false,
5553*0a6a1f1dSLionel Sambuc EncodePointerToObjCTypedef,
5554*0a6a1f1dSLionel Sambuc NotEncodedT);
5555f4a2713aSLionel Sambuc }
5556f4a2713aSLionel Sambuc S += '}';
5557f4a2713aSLionel Sambuc return;
5558f4a2713aSLionel Sambuc }
5559f4a2713aSLionel Sambuc
5560f4a2713aSLionel Sambuc case Type::ObjCObjectPointer: {
5561f4a2713aSLionel Sambuc const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>();
5562f4a2713aSLionel Sambuc if (OPT->isObjCIdType()) {
5563f4a2713aSLionel Sambuc S += '@';
5564f4a2713aSLionel Sambuc return;
5565f4a2713aSLionel Sambuc }
5566f4a2713aSLionel Sambuc
5567f4a2713aSLionel Sambuc if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
5568f4a2713aSLionel Sambuc // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
5569f4a2713aSLionel Sambuc // Since this is a binary compatibility issue, need to consult with runtime
5570f4a2713aSLionel Sambuc // folks. Fortunately, this is a *very* obsure construct.
5571f4a2713aSLionel Sambuc S += '#';
5572f4a2713aSLionel Sambuc return;
5573f4a2713aSLionel Sambuc }
5574f4a2713aSLionel Sambuc
5575f4a2713aSLionel Sambuc if (OPT->isObjCQualifiedIdType()) {
5576f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(getObjCIdType(), S,
5577f4a2713aSLionel Sambuc ExpandPointedToStructures,
5578f4a2713aSLionel Sambuc ExpandStructures, FD);
5579f4a2713aSLionel Sambuc if (FD || EncodingProperty || EncodeClassNames) {
5580f4a2713aSLionel Sambuc // Note that we do extended encoding of protocol qualifer list
5581f4a2713aSLionel Sambuc // Only when doing ivar or property encoding.
5582f4a2713aSLionel Sambuc S += '"';
5583*0a6a1f1dSLionel Sambuc for (const auto *I : OPT->quals()) {
5584f4a2713aSLionel Sambuc S += '<';
5585*0a6a1f1dSLionel Sambuc S += I->getNameAsString();
5586f4a2713aSLionel Sambuc S += '>';
5587f4a2713aSLionel Sambuc }
5588f4a2713aSLionel Sambuc S += '"';
5589f4a2713aSLionel Sambuc }
5590f4a2713aSLionel Sambuc return;
5591f4a2713aSLionel Sambuc }
5592f4a2713aSLionel Sambuc
5593f4a2713aSLionel Sambuc QualType PointeeTy = OPT->getPointeeType();
5594f4a2713aSLionel Sambuc if (!EncodingProperty &&
5595f4a2713aSLionel Sambuc isa<TypedefType>(PointeeTy.getTypePtr()) &&
5596f4a2713aSLionel Sambuc !EncodePointerToObjCTypedef) {
5597f4a2713aSLionel Sambuc // Another historical/compatibility reason.
5598f4a2713aSLionel Sambuc // We encode the underlying type which comes out as
5599f4a2713aSLionel Sambuc // {...};
5600f4a2713aSLionel Sambuc S += '^';
5601f4a2713aSLionel Sambuc if (FD && OPT->getInterfaceDecl()) {
5602f4a2713aSLionel Sambuc // Prevent recursive encoding of fields in some rare cases.
5603f4a2713aSLionel Sambuc ObjCInterfaceDecl *OI = OPT->getInterfaceDecl();
5604f4a2713aSLionel Sambuc SmallVector<const ObjCIvarDecl*, 32> Ivars;
5605f4a2713aSLionel Sambuc DeepCollectObjCIvars(OI, true, Ivars);
5606f4a2713aSLionel Sambuc for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
5607f4a2713aSLionel Sambuc if (cast<FieldDecl>(Ivars[i]) == FD) {
5608f4a2713aSLionel Sambuc S += '{';
5609f4a2713aSLionel Sambuc S += OI->getIdentifier()->getName();
5610f4a2713aSLionel Sambuc S += '}';
5611f4a2713aSLionel Sambuc return;
5612f4a2713aSLionel Sambuc }
5613f4a2713aSLionel Sambuc }
5614f4a2713aSLionel Sambuc }
5615f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(PointeeTy, S,
5616f4a2713aSLionel Sambuc false, ExpandPointedToStructures,
5617*0a6a1f1dSLionel Sambuc nullptr,
5618f4a2713aSLionel Sambuc false, false, false, false, false,
5619f4a2713aSLionel Sambuc /*EncodePointerToObjCTypedef*/true);
5620f4a2713aSLionel Sambuc return;
5621f4a2713aSLionel Sambuc }
5622f4a2713aSLionel Sambuc
5623f4a2713aSLionel Sambuc S += '@';
5624f4a2713aSLionel Sambuc if (OPT->getInterfaceDecl() &&
5625f4a2713aSLionel Sambuc (FD || EncodingProperty || EncodeClassNames)) {
5626f4a2713aSLionel Sambuc S += '"';
5627f4a2713aSLionel Sambuc S += OPT->getInterfaceDecl()->getIdentifier()->getName();
5628*0a6a1f1dSLionel Sambuc for (const auto *I : OPT->quals()) {
5629f4a2713aSLionel Sambuc S += '<';
5630*0a6a1f1dSLionel Sambuc S += I->getNameAsString();
5631f4a2713aSLionel Sambuc S += '>';
5632f4a2713aSLionel Sambuc }
5633f4a2713aSLionel Sambuc S += '"';
5634f4a2713aSLionel Sambuc }
5635f4a2713aSLionel Sambuc return;
5636f4a2713aSLionel Sambuc }
5637f4a2713aSLionel Sambuc
5638f4a2713aSLionel Sambuc // gcc just blithely ignores member pointers.
5639f4a2713aSLionel Sambuc // FIXME: we shoul do better than that. 'M' is available.
5640f4a2713aSLionel Sambuc case Type::MemberPointer:
5641*0a6a1f1dSLionel Sambuc // This matches gcc's encoding, even though technically it is insufficient.
5642*0a6a1f1dSLionel Sambuc //FIXME. We should do a better job than gcc.
5643f4a2713aSLionel Sambuc case Type::Vector:
5644f4a2713aSLionel Sambuc case Type::ExtVector:
5645*0a6a1f1dSLionel Sambuc // Until we have a coherent encoding of these three types, issue warning.
5646*0a6a1f1dSLionel Sambuc { if (NotEncodedT)
5647*0a6a1f1dSLionel Sambuc *NotEncodedT = T;
5648f4a2713aSLionel Sambuc return;
5649*0a6a1f1dSLionel Sambuc }
5650f4a2713aSLionel Sambuc
5651f4a2713aSLionel Sambuc // We could see an undeduced auto type here during error recovery.
5652f4a2713aSLionel Sambuc // Just ignore it.
5653*0a6a1f1dSLionel Sambuc case Type::Auto:
5654f4a2713aSLionel Sambuc return;
5655f4a2713aSLionel Sambuc
5656*0a6a1f1dSLionel Sambuc
5657f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(KIND, BASE)
5658f4a2713aSLionel Sambuc #define TYPE(KIND, BASE)
5659f4a2713aSLionel Sambuc #define DEPENDENT_TYPE(KIND, BASE) \
5660f4a2713aSLionel Sambuc case Type::KIND:
5661f4a2713aSLionel Sambuc #define NON_CANONICAL_TYPE(KIND, BASE) \
5662f4a2713aSLionel Sambuc case Type::KIND:
5663f4a2713aSLionel Sambuc #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \
5664f4a2713aSLionel Sambuc case Type::KIND:
5665f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
5666f4a2713aSLionel Sambuc llvm_unreachable("@encode for dependent type!");
5667f4a2713aSLionel Sambuc }
5668f4a2713aSLionel Sambuc llvm_unreachable("bad type kind!");
5669f4a2713aSLionel Sambuc }
5670f4a2713aSLionel Sambuc
getObjCEncodingForStructureImpl(RecordDecl * RDecl,std::string & S,const FieldDecl * FD,bool includeVBases,QualType * NotEncodedT) const5671f4a2713aSLionel Sambuc void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
5672f4a2713aSLionel Sambuc std::string &S,
5673f4a2713aSLionel Sambuc const FieldDecl *FD,
5674*0a6a1f1dSLionel Sambuc bool includeVBases,
5675*0a6a1f1dSLionel Sambuc QualType *NotEncodedT) const {
5676f4a2713aSLionel Sambuc assert(RDecl && "Expected non-null RecordDecl");
5677f4a2713aSLionel Sambuc assert(!RDecl->isUnion() && "Should not be called for unions");
5678f4a2713aSLionel Sambuc if (!RDecl->getDefinition())
5679f4a2713aSLionel Sambuc return;
5680f4a2713aSLionel Sambuc
5681f4a2713aSLionel Sambuc CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl);
5682f4a2713aSLionel Sambuc std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets;
5683f4a2713aSLionel Sambuc const ASTRecordLayout &layout = getASTRecordLayout(RDecl);
5684f4a2713aSLionel Sambuc
5685f4a2713aSLionel Sambuc if (CXXRec) {
5686*0a6a1f1dSLionel Sambuc for (const auto &BI : CXXRec->bases()) {
5687*0a6a1f1dSLionel Sambuc if (!BI.isVirtual()) {
5688*0a6a1f1dSLionel Sambuc CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl();
5689f4a2713aSLionel Sambuc if (base->isEmpty())
5690f4a2713aSLionel Sambuc continue;
5691f4a2713aSLionel Sambuc uint64_t offs = toBits(layout.getBaseClassOffset(base));
5692f4a2713aSLionel Sambuc FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
5693f4a2713aSLionel Sambuc std::make_pair(offs, base));
5694f4a2713aSLionel Sambuc }
5695f4a2713aSLionel Sambuc }
5696f4a2713aSLionel Sambuc }
5697f4a2713aSLionel Sambuc
5698f4a2713aSLionel Sambuc unsigned i = 0;
5699*0a6a1f1dSLionel Sambuc for (auto *Field : RDecl->fields()) {
5700f4a2713aSLionel Sambuc uint64_t offs = layout.getFieldOffset(i);
5701f4a2713aSLionel Sambuc FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
5702*0a6a1f1dSLionel Sambuc std::make_pair(offs, Field));
5703*0a6a1f1dSLionel Sambuc ++i;
5704f4a2713aSLionel Sambuc }
5705f4a2713aSLionel Sambuc
5706f4a2713aSLionel Sambuc if (CXXRec && includeVBases) {
5707*0a6a1f1dSLionel Sambuc for (const auto &BI : CXXRec->vbases()) {
5708*0a6a1f1dSLionel Sambuc CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl();
5709f4a2713aSLionel Sambuc if (base->isEmpty())
5710f4a2713aSLionel Sambuc continue;
5711f4a2713aSLionel Sambuc uint64_t offs = toBits(layout.getVBaseClassOffset(base));
5712f4a2713aSLionel Sambuc if (offs >= uint64_t(toBits(layout.getNonVirtualSize())) &&
5713f4a2713aSLionel Sambuc FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end())
5714f4a2713aSLionel Sambuc FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(),
5715f4a2713aSLionel Sambuc std::make_pair(offs, base));
5716f4a2713aSLionel Sambuc }
5717f4a2713aSLionel Sambuc }
5718f4a2713aSLionel Sambuc
5719f4a2713aSLionel Sambuc CharUnits size;
5720f4a2713aSLionel Sambuc if (CXXRec) {
5721f4a2713aSLionel Sambuc size = includeVBases ? layout.getSize() : layout.getNonVirtualSize();
5722f4a2713aSLionel Sambuc } else {
5723f4a2713aSLionel Sambuc size = layout.getSize();
5724f4a2713aSLionel Sambuc }
5725f4a2713aSLionel Sambuc
5726*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
5727f4a2713aSLionel Sambuc uint64_t CurOffs = 0;
5728*0a6a1f1dSLionel Sambuc #endif
5729f4a2713aSLionel Sambuc std::multimap<uint64_t, NamedDecl *>::iterator
5730f4a2713aSLionel Sambuc CurLayObj = FieldOrBaseOffsets.begin();
5731f4a2713aSLionel Sambuc
5732f4a2713aSLionel Sambuc if (CXXRec && CXXRec->isDynamicClass() &&
5733f4a2713aSLionel Sambuc (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) {
5734f4a2713aSLionel Sambuc if (FD) {
5735f4a2713aSLionel Sambuc S += "\"_vptr$";
5736f4a2713aSLionel Sambuc std::string recname = CXXRec->getNameAsString();
5737f4a2713aSLionel Sambuc if (recname.empty()) recname = "?";
5738f4a2713aSLionel Sambuc S += recname;
5739f4a2713aSLionel Sambuc S += '"';
5740f4a2713aSLionel Sambuc }
5741f4a2713aSLionel Sambuc S += "^^?";
5742*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
5743f4a2713aSLionel Sambuc CurOffs += getTypeSize(VoidPtrTy);
5744*0a6a1f1dSLionel Sambuc #endif
5745f4a2713aSLionel Sambuc }
5746f4a2713aSLionel Sambuc
5747f4a2713aSLionel Sambuc if (!RDecl->hasFlexibleArrayMember()) {
5748f4a2713aSLionel Sambuc // Mark the end of the structure.
5749f4a2713aSLionel Sambuc uint64_t offs = toBits(size);
5750f4a2713aSLionel Sambuc FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
5751*0a6a1f1dSLionel Sambuc std::make_pair(offs, nullptr));
5752f4a2713aSLionel Sambuc }
5753f4a2713aSLionel Sambuc
5754f4a2713aSLionel Sambuc for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) {
5755*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
5756f4a2713aSLionel Sambuc assert(CurOffs <= CurLayObj->first);
5757f4a2713aSLionel Sambuc if (CurOffs < CurLayObj->first) {
5758f4a2713aSLionel Sambuc uint64_t padding = CurLayObj->first - CurOffs;
5759f4a2713aSLionel Sambuc // FIXME: There doesn't seem to be a way to indicate in the encoding that
5760f4a2713aSLionel Sambuc // packing/alignment of members is different that normal, in which case
5761f4a2713aSLionel Sambuc // the encoding will be out-of-sync with the real layout.
5762f4a2713aSLionel Sambuc // If the runtime switches to just consider the size of types without
5763f4a2713aSLionel Sambuc // taking into account alignment, we could make padding explicit in the
5764f4a2713aSLionel Sambuc // encoding (e.g. using arrays of chars). The encoding strings would be
5765f4a2713aSLionel Sambuc // longer then though.
5766f4a2713aSLionel Sambuc CurOffs += padding;
5767f4a2713aSLionel Sambuc }
5768*0a6a1f1dSLionel Sambuc #endif
5769f4a2713aSLionel Sambuc
5770f4a2713aSLionel Sambuc NamedDecl *dcl = CurLayObj->second;
5771*0a6a1f1dSLionel Sambuc if (!dcl)
5772f4a2713aSLionel Sambuc break; // reached end of structure.
5773f4a2713aSLionel Sambuc
5774f4a2713aSLionel Sambuc if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) {
5775f4a2713aSLionel Sambuc // We expand the bases without their virtual bases since those are going
5776f4a2713aSLionel Sambuc // in the initial structure. Note that this differs from gcc which
5777f4a2713aSLionel Sambuc // expands virtual bases each time one is encountered in the hierarchy,
5778f4a2713aSLionel Sambuc // making the encoding type bigger than it really is.
5779*0a6a1f1dSLionel Sambuc getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false,
5780*0a6a1f1dSLionel Sambuc NotEncodedT);
5781f4a2713aSLionel Sambuc assert(!base->isEmpty());
5782*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
5783f4a2713aSLionel Sambuc CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize());
5784*0a6a1f1dSLionel Sambuc #endif
5785f4a2713aSLionel Sambuc } else {
5786f4a2713aSLionel Sambuc FieldDecl *field = cast<FieldDecl>(dcl);
5787f4a2713aSLionel Sambuc if (FD) {
5788f4a2713aSLionel Sambuc S += '"';
5789f4a2713aSLionel Sambuc S += field->getNameAsString();
5790f4a2713aSLionel Sambuc S += '"';
5791f4a2713aSLionel Sambuc }
5792f4a2713aSLionel Sambuc
5793f4a2713aSLionel Sambuc if (field->isBitField()) {
5794f4a2713aSLionel Sambuc EncodeBitField(this, S, field->getType(), field);
5795*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
5796f4a2713aSLionel Sambuc CurOffs += field->getBitWidthValue(*this);
5797*0a6a1f1dSLionel Sambuc #endif
5798f4a2713aSLionel Sambuc } else {
5799f4a2713aSLionel Sambuc QualType qt = field->getType();
5800f4a2713aSLionel Sambuc getLegacyIntegralTypeEncoding(qt);
5801f4a2713aSLionel Sambuc getObjCEncodingForTypeImpl(qt, S, false, true, FD,
5802f4a2713aSLionel Sambuc /*OutermostType*/false,
5803f4a2713aSLionel Sambuc /*EncodingProperty*/false,
5804*0a6a1f1dSLionel Sambuc /*StructField*/true,
5805*0a6a1f1dSLionel Sambuc false, false, false, NotEncodedT);
5806*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
5807f4a2713aSLionel Sambuc CurOffs += getTypeSize(field->getType());
5808*0a6a1f1dSLionel Sambuc #endif
5809f4a2713aSLionel Sambuc }
5810f4a2713aSLionel Sambuc }
5811f4a2713aSLionel Sambuc }
5812f4a2713aSLionel Sambuc }
5813f4a2713aSLionel Sambuc
getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,std::string & S) const5814f4a2713aSLionel Sambuc void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
5815f4a2713aSLionel Sambuc std::string& S) const {
5816f4a2713aSLionel Sambuc if (QT & Decl::OBJC_TQ_In)
5817f4a2713aSLionel Sambuc S += 'n';
5818f4a2713aSLionel Sambuc if (QT & Decl::OBJC_TQ_Inout)
5819f4a2713aSLionel Sambuc S += 'N';
5820f4a2713aSLionel Sambuc if (QT & Decl::OBJC_TQ_Out)
5821f4a2713aSLionel Sambuc S += 'o';
5822f4a2713aSLionel Sambuc if (QT & Decl::OBJC_TQ_Bycopy)
5823f4a2713aSLionel Sambuc S += 'O';
5824f4a2713aSLionel Sambuc if (QT & Decl::OBJC_TQ_Byref)
5825f4a2713aSLionel Sambuc S += 'R';
5826f4a2713aSLionel Sambuc if (QT & Decl::OBJC_TQ_Oneway)
5827f4a2713aSLionel Sambuc S += 'V';
5828f4a2713aSLionel Sambuc }
5829f4a2713aSLionel Sambuc
getObjCIdDecl() const5830f4a2713aSLionel Sambuc TypedefDecl *ASTContext::getObjCIdDecl() const {
5831f4a2713aSLionel Sambuc if (!ObjCIdDecl) {
5832*0a6a1f1dSLionel Sambuc QualType T = getObjCObjectType(ObjCBuiltinIdTy, nullptr, 0);
5833f4a2713aSLionel Sambuc T = getObjCObjectPointerType(T);
5834*0a6a1f1dSLionel Sambuc ObjCIdDecl = buildImplicitTypedef(T, "id");
5835f4a2713aSLionel Sambuc }
5836f4a2713aSLionel Sambuc return ObjCIdDecl;
5837f4a2713aSLionel Sambuc }
5838f4a2713aSLionel Sambuc
getObjCSelDecl() const5839f4a2713aSLionel Sambuc TypedefDecl *ASTContext::getObjCSelDecl() const {
5840f4a2713aSLionel Sambuc if (!ObjCSelDecl) {
5841*0a6a1f1dSLionel Sambuc QualType T = getPointerType(ObjCBuiltinSelTy);
5842*0a6a1f1dSLionel Sambuc ObjCSelDecl = buildImplicitTypedef(T, "SEL");
5843f4a2713aSLionel Sambuc }
5844f4a2713aSLionel Sambuc return ObjCSelDecl;
5845f4a2713aSLionel Sambuc }
5846f4a2713aSLionel Sambuc
getObjCClassDecl() const5847f4a2713aSLionel Sambuc TypedefDecl *ASTContext::getObjCClassDecl() const {
5848f4a2713aSLionel Sambuc if (!ObjCClassDecl) {
5849*0a6a1f1dSLionel Sambuc QualType T = getObjCObjectType(ObjCBuiltinClassTy, nullptr, 0);
5850f4a2713aSLionel Sambuc T = getObjCObjectPointerType(T);
5851*0a6a1f1dSLionel Sambuc ObjCClassDecl = buildImplicitTypedef(T, "Class");
5852f4a2713aSLionel Sambuc }
5853f4a2713aSLionel Sambuc return ObjCClassDecl;
5854f4a2713aSLionel Sambuc }
5855f4a2713aSLionel Sambuc
getObjCProtocolDecl() const5856f4a2713aSLionel Sambuc ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const {
5857f4a2713aSLionel Sambuc if (!ObjCProtocolClassDecl) {
5858f4a2713aSLionel Sambuc ObjCProtocolClassDecl
5859f4a2713aSLionel Sambuc = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(),
5860f4a2713aSLionel Sambuc SourceLocation(),
5861f4a2713aSLionel Sambuc &Idents.get("Protocol"),
5862*0a6a1f1dSLionel Sambuc /*PrevDecl=*/nullptr,
5863f4a2713aSLionel Sambuc SourceLocation(), true);
5864f4a2713aSLionel Sambuc }
5865f4a2713aSLionel Sambuc
5866f4a2713aSLionel Sambuc return ObjCProtocolClassDecl;
5867f4a2713aSLionel Sambuc }
5868f4a2713aSLionel Sambuc
5869f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
5870f4a2713aSLionel Sambuc // __builtin_va_list Construction Functions
5871f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
5872f4a2713aSLionel Sambuc
CreateCharPtrBuiltinVaListDecl(const ASTContext * Context)5873f4a2713aSLionel Sambuc static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) {
5874f4a2713aSLionel Sambuc // typedef char* __builtin_va_list;
5875*0a6a1f1dSLionel Sambuc QualType T = Context->getPointerType(Context->CharTy);
5876*0a6a1f1dSLionel Sambuc return Context->buildImplicitTypedef(T, "__builtin_va_list");
5877f4a2713aSLionel Sambuc }
5878f4a2713aSLionel Sambuc
CreateVoidPtrBuiltinVaListDecl(const ASTContext * Context)5879f4a2713aSLionel Sambuc static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) {
5880f4a2713aSLionel Sambuc // typedef void* __builtin_va_list;
5881*0a6a1f1dSLionel Sambuc QualType T = Context->getPointerType(Context->VoidTy);
5882*0a6a1f1dSLionel Sambuc return Context->buildImplicitTypedef(T, "__builtin_va_list");
5883f4a2713aSLionel Sambuc }
5884f4a2713aSLionel Sambuc
5885f4a2713aSLionel Sambuc static TypedefDecl *
CreateAArch64ABIBuiltinVaListDecl(const ASTContext * Context)5886f4a2713aSLionel Sambuc CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) {
5887*0a6a1f1dSLionel Sambuc // struct __va_list
5888*0a6a1f1dSLionel Sambuc RecordDecl *VaListTagDecl = Context->buildImplicitRecord("__va_list");
5889f4a2713aSLionel Sambuc if (Context->getLangOpts().CPlusPlus) {
5890f4a2713aSLionel Sambuc // namespace std { struct __va_list {
5891f4a2713aSLionel Sambuc NamespaceDecl *NS;
5892f4a2713aSLionel Sambuc NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context),
5893f4a2713aSLionel Sambuc Context->getTranslationUnitDecl(),
5894f4a2713aSLionel Sambuc /*Inline*/ false, SourceLocation(),
5895f4a2713aSLionel Sambuc SourceLocation(), &Context->Idents.get("std"),
5896*0a6a1f1dSLionel Sambuc /*PrevDecl*/ nullptr);
5897*0a6a1f1dSLionel Sambuc NS->setImplicit();
5898f4a2713aSLionel Sambuc VaListTagDecl->setDeclContext(NS);
5899f4a2713aSLionel Sambuc }
5900f4a2713aSLionel Sambuc
5901f4a2713aSLionel Sambuc VaListTagDecl->startDefinition();
5902f4a2713aSLionel Sambuc
5903f4a2713aSLionel Sambuc const size_t NumFields = 5;
5904f4a2713aSLionel Sambuc QualType FieldTypes[NumFields];
5905f4a2713aSLionel Sambuc const char *FieldNames[NumFields];
5906f4a2713aSLionel Sambuc
5907f4a2713aSLionel Sambuc // void *__stack;
5908f4a2713aSLionel Sambuc FieldTypes[0] = Context->getPointerType(Context->VoidTy);
5909f4a2713aSLionel Sambuc FieldNames[0] = "__stack";
5910f4a2713aSLionel Sambuc
5911f4a2713aSLionel Sambuc // void *__gr_top;
5912f4a2713aSLionel Sambuc FieldTypes[1] = Context->getPointerType(Context->VoidTy);
5913f4a2713aSLionel Sambuc FieldNames[1] = "__gr_top";
5914f4a2713aSLionel Sambuc
5915f4a2713aSLionel Sambuc // void *__vr_top;
5916f4a2713aSLionel Sambuc FieldTypes[2] = Context->getPointerType(Context->VoidTy);
5917f4a2713aSLionel Sambuc FieldNames[2] = "__vr_top";
5918f4a2713aSLionel Sambuc
5919f4a2713aSLionel Sambuc // int __gr_offs;
5920f4a2713aSLionel Sambuc FieldTypes[3] = Context->IntTy;
5921f4a2713aSLionel Sambuc FieldNames[3] = "__gr_offs";
5922f4a2713aSLionel Sambuc
5923f4a2713aSLionel Sambuc // int __vr_offs;
5924f4a2713aSLionel Sambuc FieldTypes[4] = Context->IntTy;
5925f4a2713aSLionel Sambuc FieldNames[4] = "__vr_offs";
5926f4a2713aSLionel Sambuc
5927f4a2713aSLionel Sambuc // Create fields
5928f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumFields; ++i) {
5929f4a2713aSLionel Sambuc FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
5930f4a2713aSLionel Sambuc VaListTagDecl,
5931f4a2713aSLionel Sambuc SourceLocation(),
5932f4a2713aSLionel Sambuc SourceLocation(),
5933f4a2713aSLionel Sambuc &Context->Idents.get(FieldNames[i]),
5934*0a6a1f1dSLionel Sambuc FieldTypes[i], /*TInfo=*/nullptr,
5935*0a6a1f1dSLionel Sambuc /*BitWidth=*/nullptr,
5936f4a2713aSLionel Sambuc /*Mutable=*/false,
5937f4a2713aSLionel Sambuc ICIS_NoInit);
5938f4a2713aSLionel Sambuc Field->setAccess(AS_public);
5939f4a2713aSLionel Sambuc VaListTagDecl->addDecl(Field);
5940f4a2713aSLionel Sambuc }
5941f4a2713aSLionel Sambuc VaListTagDecl->completeDefinition();
5942f4a2713aSLionel Sambuc QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5943f4a2713aSLionel Sambuc Context->VaListTagTy = VaListTagType;
5944f4a2713aSLionel Sambuc
5945f4a2713aSLionel Sambuc // } __builtin_va_list;
5946*0a6a1f1dSLionel Sambuc return Context->buildImplicitTypedef(VaListTagType, "__builtin_va_list");
5947f4a2713aSLionel Sambuc }
5948f4a2713aSLionel Sambuc
CreatePowerABIBuiltinVaListDecl(const ASTContext * Context)5949f4a2713aSLionel Sambuc static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) {
5950f4a2713aSLionel Sambuc // typedef struct __va_list_tag {
5951f4a2713aSLionel Sambuc RecordDecl *VaListTagDecl;
5952f4a2713aSLionel Sambuc
5953*0a6a1f1dSLionel Sambuc VaListTagDecl = Context->buildImplicitRecord("__va_list_tag");
5954f4a2713aSLionel Sambuc VaListTagDecl->startDefinition();
5955f4a2713aSLionel Sambuc
5956f4a2713aSLionel Sambuc const size_t NumFields = 5;
5957f4a2713aSLionel Sambuc QualType FieldTypes[NumFields];
5958f4a2713aSLionel Sambuc const char *FieldNames[NumFields];
5959f4a2713aSLionel Sambuc
5960f4a2713aSLionel Sambuc // unsigned char gpr;
5961f4a2713aSLionel Sambuc FieldTypes[0] = Context->UnsignedCharTy;
5962f4a2713aSLionel Sambuc FieldNames[0] = "gpr";
5963f4a2713aSLionel Sambuc
5964f4a2713aSLionel Sambuc // unsigned char fpr;
5965f4a2713aSLionel Sambuc FieldTypes[1] = Context->UnsignedCharTy;
5966f4a2713aSLionel Sambuc FieldNames[1] = "fpr";
5967f4a2713aSLionel Sambuc
5968f4a2713aSLionel Sambuc // unsigned short reserved;
5969f4a2713aSLionel Sambuc FieldTypes[2] = Context->UnsignedShortTy;
5970f4a2713aSLionel Sambuc FieldNames[2] = "reserved";
5971f4a2713aSLionel Sambuc
5972f4a2713aSLionel Sambuc // void* overflow_arg_area;
5973f4a2713aSLionel Sambuc FieldTypes[3] = Context->getPointerType(Context->VoidTy);
5974f4a2713aSLionel Sambuc FieldNames[3] = "overflow_arg_area";
5975f4a2713aSLionel Sambuc
5976f4a2713aSLionel Sambuc // void* reg_save_area;
5977f4a2713aSLionel Sambuc FieldTypes[4] = Context->getPointerType(Context->VoidTy);
5978f4a2713aSLionel Sambuc FieldNames[4] = "reg_save_area";
5979f4a2713aSLionel Sambuc
5980f4a2713aSLionel Sambuc // Create fields
5981f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumFields; ++i) {
5982f4a2713aSLionel Sambuc FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl,
5983f4a2713aSLionel Sambuc SourceLocation(),
5984f4a2713aSLionel Sambuc SourceLocation(),
5985f4a2713aSLionel Sambuc &Context->Idents.get(FieldNames[i]),
5986*0a6a1f1dSLionel Sambuc FieldTypes[i], /*TInfo=*/nullptr,
5987*0a6a1f1dSLionel Sambuc /*BitWidth=*/nullptr,
5988f4a2713aSLionel Sambuc /*Mutable=*/false,
5989f4a2713aSLionel Sambuc ICIS_NoInit);
5990f4a2713aSLionel Sambuc Field->setAccess(AS_public);
5991f4a2713aSLionel Sambuc VaListTagDecl->addDecl(Field);
5992f4a2713aSLionel Sambuc }
5993f4a2713aSLionel Sambuc VaListTagDecl->completeDefinition();
5994f4a2713aSLionel Sambuc QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5995f4a2713aSLionel Sambuc Context->VaListTagTy = VaListTagType;
5996f4a2713aSLionel Sambuc
5997f4a2713aSLionel Sambuc // } __va_list_tag;
5998*0a6a1f1dSLionel Sambuc TypedefDecl *VaListTagTypedefDecl =
5999*0a6a1f1dSLionel Sambuc Context->buildImplicitTypedef(VaListTagType, "__va_list_tag");
6000*0a6a1f1dSLionel Sambuc
6001f4a2713aSLionel Sambuc QualType VaListTagTypedefType =
6002f4a2713aSLionel Sambuc Context->getTypedefType(VaListTagTypedefDecl);
6003f4a2713aSLionel Sambuc
6004f4a2713aSLionel Sambuc // typedef __va_list_tag __builtin_va_list[1];
6005f4a2713aSLionel Sambuc llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
6006f4a2713aSLionel Sambuc QualType VaListTagArrayType
6007f4a2713aSLionel Sambuc = Context->getConstantArrayType(VaListTagTypedefType,
6008f4a2713aSLionel Sambuc Size, ArrayType::Normal, 0);
6009*0a6a1f1dSLionel Sambuc return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list");
6010f4a2713aSLionel Sambuc }
6011f4a2713aSLionel Sambuc
6012f4a2713aSLionel Sambuc static TypedefDecl *
CreateX86_64ABIBuiltinVaListDecl(const ASTContext * Context)6013f4a2713aSLionel Sambuc CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) {
6014f4a2713aSLionel Sambuc // typedef struct __va_list_tag {
6015f4a2713aSLionel Sambuc RecordDecl *VaListTagDecl;
6016*0a6a1f1dSLionel Sambuc VaListTagDecl = Context->buildImplicitRecord("__va_list_tag");
6017f4a2713aSLionel Sambuc VaListTagDecl->startDefinition();
6018f4a2713aSLionel Sambuc
6019f4a2713aSLionel Sambuc const size_t NumFields = 4;
6020f4a2713aSLionel Sambuc QualType FieldTypes[NumFields];
6021f4a2713aSLionel Sambuc const char *FieldNames[NumFields];
6022f4a2713aSLionel Sambuc
6023f4a2713aSLionel Sambuc // unsigned gp_offset;
6024f4a2713aSLionel Sambuc FieldTypes[0] = Context->UnsignedIntTy;
6025f4a2713aSLionel Sambuc FieldNames[0] = "gp_offset";
6026f4a2713aSLionel Sambuc
6027f4a2713aSLionel Sambuc // unsigned fp_offset;
6028f4a2713aSLionel Sambuc FieldTypes[1] = Context->UnsignedIntTy;
6029f4a2713aSLionel Sambuc FieldNames[1] = "fp_offset";
6030f4a2713aSLionel Sambuc
6031f4a2713aSLionel Sambuc // void* overflow_arg_area;
6032f4a2713aSLionel Sambuc FieldTypes[2] = Context->getPointerType(Context->VoidTy);
6033f4a2713aSLionel Sambuc FieldNames[2] = "overflow_arg_area";
6034f4a2713aSLionel Sambuc
6035f4a2713aSLionel Sambuc // void* reg_save_area;
6036f4a2713aSLionel Sambuc FieldTypes[3] = Context->getPointerType(Context->VoidTy);
6037f4a2713aSLionel Sambuc FieldNames[3] = "reg_save_area";
6038f4a2713aSLionel Sambuc
6039f4a2713aSLionel Sambuc // Create fields
6040f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumFields; ++i) {
6041f4a2713aSLionel Sambuc FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
6042f4a2713aSLionel Sambuc VaListTagDecl,
6043f4a2713aSLionel Sambuc SourceLocation(),
6044f4a2713aSLionel Sambuc SourceLocation(),
6045f4a2713aSLionel Sambuc &Context->Idents.get(FieldNames[i]),
6046*0a6a1f1dSLionel Sambuc FieldTypes[i], /*TInfo=*/nullptr,
6047*0a6a1f1dSLionel Sambuc /*BitWidth=*/nullptr,
6048f4a2713aSLionel Sambuc /*Mutable=*/false,
6049f4a2713aSLionel Sambuc ICIS_NoInit);
6050f4a2713aSLionel Sambuc Field->setAccess(AS_public);
6051f4a2713aSLionel Sambuc VaListTagDecl->addDecl(Field);
6052f4a2713aSLionel Sambuc }
6053f4a2713aSLionel Sambuc VaListTagDecl->completeDefinition();
6054f4a2713aSLionel Sambuc QualType VaListTagType = Context->getRecordType(VaListTagDecl);
6055f4a2713aSLionel Sambuc Context->VaListTagTy = VaListTagType;
6056f4a2713aSLionel Sambuc
6057f4a2713aSLionel Sambuc // } __va_list_tag;
6058*0a6a1f1dSLionel Sambuc TypedefDecl *VaListTagTypedefDecl =
6059*0a6a1f1dSLionel Sambuc Context->buildImplicitTypedef(VaListTagType, "__va_list_tag");
6060*0a6a1f1dSLionel Sambuc
6061f4a2713aSLionel Sambuc QualType VaListTagTypedefType =
6062f4a2713aSLionel Sambuc Context->getTypedefType(VaListTagTypedefDecl);
6063f4a2713aSLionel Sambuc
6064f4a2713aSLionel Sambuc // typedef __va_list_tag __builtin_va_list[1];
6065f4a2713aSLionel Sambuc llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
6066f4a2713aSLionel Sambuc QualType VaListTagArrayType
6067f4a2713aSLionel Sambuc = Context->getConstantArrayType(VaListTagTypedefType,
6068f4a2713aSLionel Sambuc Size, ArrayType::Normal,0);
6069*0a6a1f1dSLionel Sambuc return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list");
6070f4a2713aSLionel Sambuc }
6071f4a2713aSLionel Sambuc
CreatePNaClABIBuiltinVaListDecl(const ASTContext * Context)6072f4a2713aSLionel Sambuc static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) {
6073f4a2713aSLionel Sambuc // typedef int __builtin_va_list[4];
6074f4a2713aSLionel Sambuc llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4);
6075f4a2713aSLionel Sambuc QualType IntArrayType
6076f4a2713aSLionel Sambuc = Context->getConstantArrayType(Context->IntTy,
6077f4a2713aSLionel Sambuc Size, ArrayType::Normal, 0);
6078*0a6a1f1dSLionel Sambuc return Context->buildImplicitTypedef(IntArrayType, "__builtin_va_list");
6079f4a2713aSLionel Sambuc }
6080f4a2713aSLionel Sambuc
6081f4a2713aSLionel Sambuc static TypedefDecl *
CreateAAPCSABIBuiltinVaListDecl(const ASTContext * Context)6082f4a2713aSLionel Sambuc CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) {
6083*0a6a1f1dSLionel Sambuc // struct __va_list
6084*0a6a1f1dSLionel Sambuc RecordDecl *VaListDecl = Context->buildImplicitRecord("__va_list");
6085f4a2713aSLionel Sambuc if (Context->getLangOpts().CPlusPlus) {
6086f4a2713aSLionel Sambuc // namespace std { struct __va_list {
6087f4a2713aSLionel Sambuc NamespaceDecl *NS;
6088f4a2713aSLionel Sambuc NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context),
6089f4a2713aSLionel Sambuc Context->getTranslationUnitDecl(),
6090f4a2713aSLionel Sambuc /*Inline*/false, SourceLocation(),
6091f4a2713aSLionel Sambuc SourceLocation(), &Context->Idents.get("std"),
6092*0a6a1f1dSLionel Sambuc /*PrevDecl*/ nullptr);
6093*0a6a1f1dSLionel Sambuc NS->setImplicit();
6094f4a2713aSLionel Sambuc VaListDecl->setDeclContext(NS);
6095f4a2713aSLionel Sambuc }
6096f4a2713aSLionel Sambuc
6097f4a2713aSLionel Sambuc VaListDecl->startDefinition();
6098f4a2713aSLionel Sambuc
6099f4a2713aSLionel Sambuc // void * __ap;
6100f4a2713aSLionel Sambuc FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
6101f4a2713aSLionel Sambuc VaListDecl,
6102f4a2713aSLionel Sambuc SourceLocation(),
6103f4a2713aSLionel Sambuc SourceLocation(),
6104f4a2713aSLionel Sambuc &Context->Idents.get("__ap"),
6105f4a2713aSLionel Sambuc Context->getPointerType(Context->VoidTy),
6106*0a6a1f1dSLionel Sambuc /*TInfo=*/nullptr,
6107*0a6a1f1dSLionel Sambuc /*BitWidth=*/nullptr,
6108f4a2713aSLionel Sambuc /*Mutable=*/false,
6109f4a2713aSLionel Sambuc ICIS_NoInit);
6110f4a2713aSLionel Sambuc Field->setAccess(AS_public);
6111f4a2713aSLionel Sambuc VaListDecl->addDecl(Field);
6112f4a2713aSLionel Sambuc
6113f4a2713aSLionel Sambuc // };
6114f4a2713aSLionel Sambuc VaListDecl->completeDefinition();
6115f4a2713aSLionel Sambuc
6116f4a2713aSLionel Sambuc // typedef struct __va_list __builtin_va_list;
6117*0a6a1f1dSLionel Sambuc QualType T = Context->getRecordType(VaListDecl);
6118*0a6a1f1dSLionel Sambuc return Context->buildImplicitTypedef(T, "__builtin_va_list");
6119f4a2713aSLionel Sambuc }
6120f4a2713aSLionel Sambuc
6121f4a2713aSLionel Sambuc static TypedefDecl *
CreateSystemZBuiltinVaListDecl(const ASTContext * Context)6122f4a2713aSLionel Sambuc CreateSystemZBuiltinVaListDecl(const ASTContext *Context) {
6123f4a2713aSLionel Sambuc // typedef struct __va_list_tag {
6124f4a2713aSLionel Sambuc RecordDecl *VaListTagDecl;
6125*0a6a1f1dSLionel Sambuc VaListTagDecl = Context->buildImplicitRecord("__va_list_tag");
6126f4a2713aSLionel Sambuc VaListTagDecl->startDefinition();
6127f4a2713aSLionel Sambuc
6128f4a2713aSLionel Sambuc const size_t NumFields = 4;
6129f4a2713aSLionel Sambuc QualType FieldTypes[NumFields];
6130f4a2713aSLionel Sambuc const char *FieldNames[NumFields];
6131f4a2713aSLionel Sambuc
6132f4a2713aSLionel Sambuc // long __gpr;
6133f4a2713aSLionel Sambuc FieldTypes[0] = Context->LongTy;
6134f4a2713aSLionel Sambuc FieldNames[0] = "__gpr";
6135f4a2713aSLionel Sambuc
6136f4a2713aSLionel Sambuc // long __fpr;
6137f4a2713aSLionel Sambuc FieldTypes[1] = Context->LongTy;
6138f4a2713aSLionel Sambuc FieldNames[1] = "__fpr";
6139f4a2713aSLionel Sambuc
6140f4a2713aSLionel Sambuc // void *__overflow_arg_area;
6141f4a2713aSLionel Sambuc FieldTypes[2] = Context->getPointerType(Context->VoidTy);
6142f4a2713aSLionel Sambuc FieldNames[2] = "__overflow_arg_area";
6143f4a2713aSLionel Sambuc
6144f4a2713aSLionel Sambuc // void *__reg_save_area;
6145f4a2713aSLionel Sambuc FieldTypes[3] = Context->getPointerType(Context->VoidTy);
6146f4a2713aSLionel Sambuc FieldNames[3] = "__reg_save_area";
6147f4a2713aSLionel Sambuc
6148f4a2713aSLionel Sambuc // Create fields
6149f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumFields; ++i) {
6150f4a2713aSLionel Sambuc FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
6151f4a2713aSLionel Sambuc VaListTagDecl,
6152f4a2713aSLionel Sambuc SourceLocation(),
6153f4a2713aSLionel Sambuc SourceLocation(),
6154f4a2713aSLionel Sambuc &Context->Idents.get(FieldNames[i]),
6155*0a6a1f1dSLionel Sambuc FieldTypes[i], /*TInfo=*/nullptr,
6156*0a6a1f1dSLionel Sambuc /*BitWidth=*/nullptr,
6157f4a2713aSLionel Sambuc /*Mutable=*/false,
6158f4a2713aSLionel Sambuc ICIS_NoInit);
6159f4a2713aSLionel Sambuc Field->setAccess(AS_public);
6160f4a2713aSLionel Sambuc VaListTagDecl->addDecl(Field);
6161f4a2713aSLionel Sambuc }
6162f4a2713aSLionel Sambuc VaListTagDecl->completeDefinition();
6163f4a2713aSLionel Sambuc QualType VaListTagType = Context->getRecordType(VaListTagDecl);
6164f4a2713aSLionel Sambuc Context->VaListTagTy = VaListTagType;
6165f4a2713aSLionel Sambuc
6166f4a2713aSLionel Sambuc // } __va_list_tag;
6167*0a6a1f1dSLionel Sambuc TypedefDecl *VaListTagTypedefDecl =
6168*0a6a1f1dSLionel Sambuc Context->buildImplicitTypedef(VaListTagType, "__va_list_tag");
6169f4a2713aSLionel Sambuc QualType VaListTagTypedefType =
6170f4a2713aSLionel Sambuc Context->getTypedefType(VaListTagTypedefDecl);
6171f4a2713aSLionel Sambuc
6172f4a2713aSLionel Sambuc // typedef __va_list_tag __builtin_va_list[1];
6173f4a2713aSLionel Sambuc llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
6174f4a2713aSLionel Sambuc QualType VaListTagArrayType
6175f4a2713aSLionel Sambuc = Context->getConstantArrayType(VaListTagTypedefType,
6176f4a2713aSLionel Sambuc Size, ArrayType::Normal,0);
6177f4a2713aSLionel Sambuc
6178*0a6a1f1dSLionel Sambuc return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list");
6179f4a2713aSLionel Sambuc }
6180f4a2713aSLionel Sambuc
CreateVaListDecl(const ASTContext * Context,TargetInfo::BuiltinVaListKind Kind)6181f4a2713aSLionel Sambuc static TypedefDecl *CreateVaListDecl(const ASTContext *Context,
6182f4a2713aSLionel Sambuc TargetInfo::BuiltinVaListKind Kind) {
6183f4a2713aSLionel Sambuc switch (Kind) {
6184f4a2713aSLionel Sambuc case TargetInfo::CharPtrBuiltinVaList:
6185f4a2713aSLionel Sambuc return CreateCharPtrBuiltinVaListDecl(Context);
6186f4a2713aSLionel Sambuc case TargetInfo::VoidPtrBuiltinVaList:
6187f4a2713aSLionel Sambuc return CreateVoidPtrBuiltinVaListDecl(Context);
6188f4a2713aSLionel Sambuc case TargetInfo::AArch64ABIBuiltinVaList:
6189f4a2713aSLionel Sambuc return CreateAArch64ABIBuiltinVaListDecl(Context);
6190f4a2713aSLionel Sambuc case TargetInfo::PowerABIBuiltinVaList:
6191f4a2713aSLionel Sambuc return CreatePowerABIBuiltinVaListDecl(Context);
6192f4a2713aSLionel Sambuc case TargetInfo::X86_64ABIBuiltinVaList:
6193f4a2713aSLionel Sambuc return CreateX86_64ABIBuiltinVaListDecl(Context);
6194f4a2713aSLionel Sambuc case TargetInfo::PNaClABIBuiltinVaList:
6195f4a2713aSLionel Sambuc return CreatePNaClABIBuiltinVaListDecl(Context);
6196f4a2713aSLionel Sambuc case TargetInfo::AAPCSABIBuiltinVaList:
6197f4a2713aSLionel Sambuc return CreateAAPCSABIBuiltinVaListDecl(Context);
6198f4a2713aSLionel Sambuc case TargetInfo::SystemZBuiltinVaList:
6199f4a2713aSLionel Sambuc return CreateSystemZBuiltinVaListDecl(Context);
6200f4a2713aSLionel Sambuc }
6201f4a2713aSLionel Sambuc
6202f4a2713aSLionel Sambuc llvm_unreachable("Unhandled __builtin_va_list type kind");
6203f4a2713aSLionel Sambuc }
6204f4a2713aSLionel Sambuc
getBuiltinVaListDecl() const6205f4a2713aSLionel Sambuc TypedefDecl *ASTContext::getBuiltinVaListDecl() const {
6206*0a6a1f1dSLionel Sambuc if (!BuiltinVaListDecl) {
6207f4a2713aSLionel Sambuc BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind());
6208*0a6a1f1dSLionel Sambuc assert(BuiltinVaListDecl->isImplicit());
6209*0a6a1f1dSLionel Sambuc }
6210f4a2713aSLionel Sambuc
6211f4a2713aSLionel Sambuc return BuiltinVaListDecl;
6212f4a2713aSLionel Sambuc }
6213f4a2713aSLionel Sambuc
getVaListTagType() const6214f4a2713aSLionel Sambuc QualType ASTContext::getVaListTagType() const {
6215f4a2713aSLionel Sambuc // Force the creation of VaListTagTy by building the __builtin_va_list
6216f4a2713aSLionel Sambuc // declaration.
6217f4a2713aSLionel Sambuc if (VaListTagTy.isNull())
6218f4a2713aSLionel Sambuc (void) getBuiltinVaListDecl();
6219f4a2713aSLionel Sambuc
6220f4a2713aSLionel Sambuc return VaListTagTy;
6221f4a2713aSLionel Sambuc }
6222f4a2713aSLionel Sambuc
setObjCConstantStringInterface(ObjCInterfaceDecl * Decl)6223f4a2713aSLionel Sambuc void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
6224f4a2713aSLionel Sambuc assert(ObjCConstantStringType.isNull() &&
6225f4a2713aSLionel Sambuc "'NSConstantString' type already set!");
6226f4a2713aSLionel Sambuc
6227f4a2713aSLionel Sambuc ObjCConstantStringType = getObjCInterfaceType(Decl);
6228f4a2713aSLionel Sambuc }
6229f4a2713aSLionel Sambuc
6230f4a2713aSLionel Sambuc /// \brief Retrieve the template name that corresponds to a non-empty
6231f4a2713aSLionel Sambuc /// lookup.
6232f4a2713aSLionel Sambuc TemplateName
getOverloadedTemplateName(UnresolvedSetIterator Begin,UnresolvedSetIterator End) const6233f4a2713aSLionel Sambuc ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
6234f4a2713aSLionel Sambuc UnresolvedSetIterator End) const {
6235f4a2713aSLionel Sambuc unsigned size = End - Begin;
6236f4a2713aSLionel Sambuc assert(size > 1 && "set is not overloaded!");
6237f4a2713aSLionel Sambuc
6238f4a2713aSLionel Sambuc void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
6239f4a2713aSLionel Sambuc size * sizeof(FunctionTemplateDecl*));
6240f4a2713aSLionel Sambuc OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
6241f4a2713aSLionel Sambuc
6242f4a2713aSLionel Sambuc NamedDecl **Storage = OT->getStorage();
6243f4a2713aSLionel Sambuc for (UnresolvedSetIterator I = Begin; I != End; ++I) {
6244f4a2713aSLionel Sambuc NamedDecl *D = *I;
6245f4a2713aSLionel Sambuc assert(isa<FunctionTemplateDecl>(D) ||
6246f4a2713aSLionel Sambuc (isa<UsingShadowDecl>(D) &&
6247f4a2713aSLionel Sambuc isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
6248f4a2713aSLionel Sambuc *Storage++ = D;
6249f4a2713aSLionel Sambuc }
6250f4a2713aSLionel Sambuc
6251f4a2713aSLionel Sambuc return TemplateName(OT);
6252f4a2713aSLionel Sambuc }
6253f4a2713aSLionel Sambuc
6254f4a2713aSLionel Sambuc /// \brief Retrieve the template name that represents a qualified
6255f4a2713aSLionel Sambuc /// template name such as \c std::vector.
6256f4a2713aSLionel Sambuc TemplateName
getQualifiedTemplateName(NestedNameSpecifier * NNS,bool TemplateKeyword,TemplateDecl * Template) const6257f4a2713aSLionel Sambuc ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
6258f4a2713aSLionel Sambuc bool TemplateKeyword,
6259f4a2713aSLionel Sambuc TemplateDecl *Template) const {
6260f4a2713aSLionel Sambuc assert(NNS && "Missing nested-name-specifier in qualified template name");
6261f4a2713aSLionel Sambuc
6262f4a2713aSLionel Sambuc // FIXME: Canonicalization?
6263f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
6264f4a2713aSLionel Sambuc QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
6265f4a2713aSLionel Sambuc
6266*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
6267f4a2713aSLionel Sambuc QualifiedTemplateName *QTN =
6268f4a2713aSLionel Sambuc QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6269f4a2713aSLionel Sambuc if (!QTN) {
6270f4a2713aSLionel Sambuc QTN = new (*this, llvm::alignOf<QualifiedTemplateName>())
6271f4a2713aSLionel Sambuc QualifiedTemplateName(NNS, TemplateKeyword, Template);
6272f4a2713aSLionel Sambuc QualifiedTemplateNames.InsertNode(QTN, InsertPos);
6273f4a2713aSLionel Sambuc }
6274f4a2713aSLionel Sambuc
6275f4a2713aSLionel Sambuc return TemplateName(QTN);
6276f4a2713aSLionel Sambuc }
6277f4a2713aSLionel Sambuc
6278f4a2713aSLionel Sambuc /// \brief Retrieve the template name that represents a dependent
6279f4a2713aSLionel Sambuc /// template name such as \c MetaFun::template apply.
6280f4a2713aSLionel Sambuc TemplateName
getDependentTemplateName(NestedNameSpecifier * NNS,const IdentifierInfo * Name) const6281f4a2713aSLionel Sambuc ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
6282f4a2713aSLionel Sambuc const IdentifierInfo *Name) const {
6283f4a2713aSLionel Sambuc assert((!NNS || NNS->isDependent()) &&
6284f4a2713aSLionel Sambuc "Nested name specifier must be dependent");
6285f4a2713aSLionel Sambuc
6286f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
6287f4a2713aSLionel Sambuc DependentTemplateName::Profile(ID, NNS, Name);
6288f4a2713aSLionel Sambuc
6289*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
6290f4a2713aSLionel Sambuc DependentTemplateName *QTN =
6291f4a2713aSLionel Sambuc DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6292f4a2713aSLionel Sambuc
6293f4a2713aSLionel Sambuc if (QTN)
6294f4a2713aSLionel Sambuc return TemplateName(QTN);
6295f4a2713aSLionel Sambuc
6296f4a2713aSLionel Sambuc NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
6297f4a2713aSLionel Sambuc if (CanonNNS == NNS) {
6298f4a2713aSLionel Sambuc QTN = new (*this, llvm::alignOf<DependentTemplateName>())
6299f4a2713aSLionel Sambuc DependentTemplateName(NNS, Name);
6300f4a2713aSLionel Sambuc } else {
6301f4a2713aSLionel Sambuc TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
6302f4a2713aSLionel Sambuc QTN = new (*this, llvm::alignOf<DependentTemplateName>())
6303f4a2713aSLionel Sambuc DependentTemplateName(NNS, Name, Canon);
6304f4a2713aSLionel Sambuc DependentTemplateName *CheckQTN =
6305f4a2713aSLionel Sambuc DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6306f4a2713aSLionel Sambuc assert(!CheckQTN && "Dependent type name canonicalization broken");
6307f4a2713aSLionel Sambuc (void)CheckQTN;
6308f4a2713aSLionel Sambuc }
6309f4a2713aSLionel Sambuc
6310f4a2713aSLionel Sambuc DependentTemplateNames.InsertNode(QTN, InsertPos);
6311f4a2713aSLionel Sambuc return TemplateName(QTN);
6312f4a2713aSLionel Sambuc }
6313f4a2713aSLionel Sambuc
6314f4a2713aSLionel Sambuc /// \brief Retrieve the template name that represents a dependent
6315f4a2713aSLionel Sambuc /// template name such as \c MetaFun::template operator+.
6316f4a2713aSLionel Sambuc TemplateName
getDependentTemplateName(NestedNameSpecifier * NNS,OverloadedOperatorKind Operator) const6317f4a2713aSLionel Sambuc ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
6318f4a2713aSLionel Sambuc OverloadedOperatorKind Operator) const {
6319f4a2713aSLionel Sambuc assert((!NNS || NNS->isDependent()) &&
6320f4a2713aSLionel Sambuc "Nested name specifier must be dependent");
6321f4a2713aSLionel Sambuc
6322f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
6323f4a2713aSLionel Sambuc DependentTemplateName::Profile(ID, NNS, Operator);
6324f4a2713aSLionel Sambuc
6325*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
6326f4a2713aSLionel Sambuc DependentTemplateName *QTN
6327f4a2713aSLionel Sambuc = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6328f4a2713aSLionel Sambuc
6329f4a2713aSLionel Sambuc if (QTN)
6330f4a2713aSLionel Sambuc return TemplateName(QTN);
6331f4a2713aSLionel Sambuc
6332f4a2713aSLionel Sambuc NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
6333f4a2713aSLionel Sambuc if (CanonNNS == NNS) {
6334f4a2713aSLionel Sambuc QTN = new (*this, llvm::alignOf<DependentTemplateName>())
6335f4a2713aSLionel Sambuc DependentTemplateName(NNS, Operator);
6336f4a2713aSLionel Sambuc } else {
6337f4a2713aSLionel Sambuc TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
6338f4a2713aSLionel Sambuc QTN = new (*this, llvm::alignOf<DependentTemplateName>())
6339f4a2713aSLionel Sambuc DependentTemplateName(NNS, Operator, Canon);
6340f4a2713aSLionel Sambuc
6341f4a2713aSLionel Sambuc DependentTemplateName *CheckQTN
6342f4a2713aSLionel Sambuc = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6343f4a2713aSLionel Sambuc assert(!CheckQTN && "Dependent template name canonicalization broken");
6344f4a2713aSLionel Sambuc (void)CheckQTN;
6345f4a2713aSLionel Sambuc }
6346f4a2713aSLionel Sambuc
6347f4a2713aSLionel Sambuc DependentTemplateNames.InsertNode(QTN, InsertPos);
6348f4a2713aSLionel Sambuc return TemplateName(QTN);
6349f4a2713aSLionel Sambuc }
6350f4a2713aSLionel Sambuc
6351f4a2713aSLionel Sambuc TemplateName
getSubstTemplateTemplateParm(TemplateTemplateParmDecl * param,TemplateName replacement) const6352f4a2713aSLionel Sambuc ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param,
6353f4a2713aSLionel Sambuc TemplateName replacement) const {
6354f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
6355f4a2713aSLionel Sambuc SubstTemplateTemplateParmStorage::Profile(ID, param, replacement);
6356f4a2713aSLionel Sambuc
6357*0a6a1f1dSLionel Sambuc void *insertPos = nullptr;
6358f4a2713aSLionel Sambuc SubstTemplateTemplateParmStorage *subst
6359f4a2713aSLionel Sambuc = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
6360f4a2713aSLionel Sambuc
6361f4a2713aSLionel Sambuc if (!subst) {
6362f4a2713aSLionel Sambuc subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement);
6363f4a2713aSLionel Sambuc SubstTemplateTemplateParms.InsertNode(subst, insertPos);
6364f4a2713aSLionel Sambuc }
6365f4a2713aSLionel Sambuc
6366f4a2713aSLionel Sambuc return TemplateName(subst);
6367f4a2713aSLionel Sambuc }
6368f4a2713aSLionel Sambuc
6369f4a2713aSLionel Sambuc TemplateName
getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl * Param,const TemplateArgument & ArgPack) const6370f4a2713aSLionel Sambuc ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
6371f4a2713aSLionel Sambuc const TemplateArgument &ArgPack) const {
6372f4a2713aSLionel Sambuc ASTContext &Self = const_cast<ASTContext &>(*this);
6373f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
6374f4a2713aSLionel Sambuc SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
6375f4a2713aSLionel Sambuc
6376*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
6377f4a2713aSLionel Sambuc SubstTemplateTemplateParmPackStorage *Subst
6378f4a2713aSLionel Sambuc = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
6379f4a2713aSLionel Sambuc
6380f4a2713aSLionel Sambuc if (!Subst) {
6381f4a2713aSLionel Sambuc Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param,
6382f4a2713aSLionel Sambuc ArgPack.pack_size(),
6383f4a2713aSLionel Sambuc ArgPack.pack_begin());
6384f4a2713aSLionel Sambuc SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
6385f4a2713aSLionel Sambuc }
6386f4a2713aSLionel Sambuc
6387f4a2713aSLionel Sambuc return TemplateName(Subst);
6388f4a2713aSLionel Sambuc }
6389f4a2713aSLionel Sambuc
6390f4a2713aSLionel Sambuc /// getFromTargetType - Given one of the integer types provided by
6391f4a2713aSLionel Sambuc /// TargetInfo, produce the corresponding type. The unsigned @p Type
6392f4a2713aSLionel Sambuc /// is actually a value of type @c TargetInfo::IntType.
getFromTargetType(unsigned Type) const6393f4a2713aSLionel Sambuc CanQualType ASTContext::getFromTargetType(unsigned Type) const {
6394f4a2713aSLionel Sambuc switch (Type) {
6395f4a2713aSLionel Sambuc case TargetInfo::NoInt: return CanQualType();
6396f4a2713aSLionel Sambuc case TargetInfo::SignedChar: return SignedCharTy;
6397f4a2713aSLionel Sambuc case TargetInfo::UnsignedChar: return UnsignedCharTy;
6398f4a2713aSLionel Sambuc case TargetInfo::SignedShort: return ShortTy;
6399f4a2713aSLionel Sambuc case TargetInfo::UnsignedShort: return UnsignedShortTy;
6400f4a2713aSLionel Sambuc case TargetInfo::SignedInt: return IntTy;
6401f4a2713aSLionel Sambuc case TargetInfo::UnsignedInt: return UnsignedIntTy;
6402f4a2713aSLionel Sambuc case TargetInfo::SignedLong: return LongTy;
6403f4a2713aSLionel Sambuc case TargetInfo::UnsignedLong: return UnsignedLongTy;
6404f4a2713aSLionel Sambuc case TargetInfo::SignedLongLong: return LongLongTy;
6405f4a2713aSLionel Sambuc case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
6406f4a2713aSLionel Sambuc }
6407f4a2713aSLionel Sambuc
6408f4a2713aSLionel Sambuc llvm_unreachable("Unhandled TargetInfo::IntType value");
6409f4a2713aSLionel Sambuc }
6410f4a2713aSLionel Sambuc
6411f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
6412f4a2713aSLionel Sambuc // Type Predicates.
6413f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
6414f4a2713aSLionel Sambuc
6415f4a2713aSLionel Sambuc /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
6416f4a2713aSLionel Sambuc /// garbage collection attribute.
6417f4a2713aSLionel Sambuc ///
getObjCGCAttrKind(QualType Ty) const6418f4a2713aSLionel Sambuc Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
6419f4a2713aSLionel Sambuc if (getLangOpts().getGC() == LangOptions::NonGC)
6420f4a2713aSLionel Sambuc return Qualifiers::GCNone;
6421f4a2713aSLionel Sambuc
6422f4a2713aSLionel Sambuc assert(getLangOpts().ObjC1);
6423f4a2713aSLionel Sambuc Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
6424f4a2713aSLionel Sambuc
6425f4a2713aSLionel Sambuc // Default behaviour under objective-C's gc is for ObjC pointers
6426f4a2713aSLionel Sambuc // (or pointers to them) be treated as though they were declared
6427f4a2713aSLionel Sambuc // as __strong.
6428f4a2713aSLionel Sambuc if (GCAttrs == Qualifiers::GCNone) {
6429f4a2713aSLionel Sambuc if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
6430f4a2713aSLionel Sambuc return Qualifiers::Strong;
6431f4a2713aSLionel Sambuc else if (Ty->isPointerType())
6432f4a2713aSLionel Sambuc return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
6433f4a2713aSLionel Sambuc } else {
6434f4a2713aSLionel Sambuc // It's not valid to set GC attributes on anything that isn't a
6435f4a2713aSLionel Sambuc // pointer.
6436f4a2713aSLionel Sambuc #ifndef NDEBUG
6437f4a2713aSLionel Sambuc QualType CT = Ty->getCanonicalTypeInternal();
6438f4a2713aSLionel Sambuc while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
6439f4a2713aSLionel Sambuc CT = AT->getElementType();
6440f4a2713aSLionel Sambuc assert(CT->isAnyPointerType() || CT->isBlockPointerType());
6441f4a2713aSLionel Sambuc #endif
6442f4a2713aSLionel Sambuc }
6443f4a2713aSLionel Sambuc return GCAttrs;
6444f4a2713aSLionel Sambuc }
6445f4a2713aSLionel Sambuc
6446f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
6447f4a2713aSLionel Sambuc // Type Compatibility Testing
6448f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
6449f4a2713aSLionel Sambuc
6450f4a2713aSLionel Sambuc /// areCompatVectorTypes - Return true if the two specified vector types are
6451f4a2713aSLionel Sambuc /// compatible.
areCompatVectorTypes(const VectorType * LHS,const VectorType * RHS)6452f4a2713aSLionel Sambuc static bool areCompatVectorTypes(const VectorType *LHS,
6453f4a2713aSLionel Sambuc const VectorType *RHS) {
6454f4a2713aSLionel Sambuc assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
6455f4a2713aSLionel Sambuc return LHS->getElementType() == RHS->getElementType() &&
6456f4a2713aSLionel Sambuc LHS->getNumElements() == RHS->getNumElements();
6457f4a2713aSLionel Sambuc }
6458f4a2713aSLionel Sambuc
areCompatibleVectorTypes(QualType FirstVec,QualType SecondVec)6459f4a2713aSLionel Sambuc bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
6460f4a2713aSLionel Sambuc QualType SecondVec) {
6461f4a2713aSLionel Sambuc assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
6462f4a2713aSLionel Sambuc assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
6463f4a2713aSLionel Sambuc
6464f4a2713aSLionel Sambuc if (hasSameUnqualifiedType(FirstVec, SecondVec))
6465f4a2713aSLionel Sambuc return true;
6466f4a2713aSLionel Sambuc
6467f4a2713aSLionel Sambuc // Treat Neon vector types and most AltiVec vector types as if they are the
6468f4a2713aSLionel Sambuc // equivalent GCC vector types.
6469f4a2713aSLionel Sambuc const VectorType *First = FirstVec->getAs<VectorType>();
6470f4a2713aSLionel Sambuc const VectorType *Second = SecondVec->getAs<VectorType>();
6471f4a2713aSLionel Sambuc if (First->getNumElements() == Second->getNumElements() &&
6472f4a2713aSLionel Sambuc hasSameType(First->getElementType(), Second->getElementType()) &&
6473f4a2713aSLionel Sambuc First->getVectorKind() != VectorType::AltiVecPixel &&
6474f4a2713aSLionel Sambuc First->getVectorKind() != VectorType::AltiVecBool &&
6475f4a2713aSLionel Sambuc Second->getVectorKind() != VectorType::AltiVecPixel &&
6476f4a2713aSLionel Sambuc Second->getVectorKind() != VectorType::AltiVecBool)
6477f4a2713aSLionel Sambuc return true;
6478f4a2713aSLionel Sambuc
6479f4a2713aSLionel Sambuc return false;
6480f4a2713aSLionel Sambuc }
6481f4a2713aSLionel Sambuc
6482f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
6483f4a2713aSLionel Sambuc // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
6484f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
6485f4a2713aSLionel Sambuc
6486f4a2713aSLionel Sambuc /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
6487f4a2713aSLionel Sambuc /// inheritance hierarchy of 'rProto'.
6488f4a2713aSLionel Sambuc bool
ProtocolCompatibleWithProtocol(ObjCProtocolDecl * lProto,ObjCProtocolDecl * rProto) const6489f4a2713aSLionel Sambuc ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
6490f4a2713aSLionel Sambuc ObjCProtocolDecl *rProto) const {
6491f4a2713aSLionel Sambuc if (declaresSameEntity(lProto, rProto))
6492f4a2713aSLionel Sambuc return true;
6493*0a6a1f1dSLionel Sambuc for (auto *PI : rProto->protocols())
6494*0a6a1f1dSLionel Sambuc if (ProtocolCompatibleWithProtocol(lProto, PI))
6495f4a2713aSLionel Sambuc return true;
6496f4a2713aSLionel Sambuc return false;
6497f4a2713aSLionel Sambuc }
6498f4a2713aSLionel Sambuc
6499f4a2713aSLionel Sambuc /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and
6500f4a2713aSLionel Sambuc /// Class<pr1, ...>.
ObjCQualifiedClassTypesAreCompatible(QualType lhs,QualType rhs)6501f4a2713aSLionel Sambuc bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
6502f4a2713aSLionel Sambuc QualType rhs) {
6503f4a2713aSLionel Sambuc const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
6504f4a2713aSLionel Sambuc const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
6505f4a2713aSLionel Sambuc assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
6506f4a2713aSLionel Sambuc
6507*0a6a1f1dSLionel Sambuc for (auto *lhsProto : lhsQID->quals()) {
6508f4a2713aSLionel Sambuc bool match = false;
6509*0a6a1f1dSLionel Sambuc for (auto *rhsProto : rhsOPT->quals()) {
6510f4a2713aSLionel Sambuc if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
6511f4a2713aSLionel Sambuc match = true;
6512f4a2713aSLionel Sambuc break;
6513f4a2713aSLionel Sambuc }
6514f4a2713aSLionel Sambuc }
6515f4a2713aSLionel Sambuc if (!match)
6516f4a2713aSLionel Sambuc return false;
6517f4a2713aSLionel Sambuc }
6518f4a2713aSLionel Sambuc return true;
6519f4a2713aSLionel Sambuc }
6520f4a2713aSLionel Sambuc
6521f4a2713aSLionel Sambuc /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
6522f4a2713aSLionel Sambuc /// ObjCQualifiedIDType.
ObjCQualifiedIdTypesAreCompatible(QualType lhs,QualType rhs,bool compare)6523f4a2713aSLionel Sambuc bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
6524f4a2713aSLionel Sambuc bool compare) {
6525f4a2713aSLionel Sambuc // Allow id<P..> and an 'id' or void* type in all cases.
6526f4a2713aSLionel Sambuc if (lhs->isVoidPointerType() ||
6527f4a2713aSLionel Sambuc lhs->isObjCIdType() || lhs->isObjCClassType())
6528f4a2713aSLionel Sambuc return true;
6529f4a2713aSLionel Sambuc else if (rhs->isVoidPointerType() ||
6530f4a2713aSLionel Sambuc rhs->isObjCIdType() || rhs->isObjCClassType())
6531f4a2713aSLionel Sambuc return true;
6532f4a2713aSLionel Sambuc
6533f4a2713aSLionel Sambuc if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
6534f4a2713aSLionel Sambuc const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
6535f4a2713aSLionel Sambuc
6536f4a2713aSLionel Sambuc if (!rhsOPT) return false;
6537f4a2713aSLionel Sambuc
6538f4a2713aSLionel Sambuc if (rhsOPT->qual_empty()) {
6539f4a2713aSLionel Sambuc // If the RHS is a unqualified interface pointer "NSString*",
6540f4a2713aSLionel Sambuc // make sure we check the class hierarchy.
6541f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
6542*0a6a1f1dSLionel Sambuc for (auto *I : lhsQID->quals()) {
6543f4a2713aSLionel Sambuc // when comparing an id<P> on lhs with a static type on rhs,
6544f4a2713aSLionel Sambuc // see if static class implements all of id's protocols, directly or
6545f4a2713aSLionel Sambuc // through its super class and categories.
6546*0a6a1f1dSLionel Sambuc if (!rhsID->ClassImplementsProtocol(I, true))
6547f4a2713aSLionel Sambuc return false;
6548f4a2713aSLionel Sambuc }
6549f4a2713aSLionel Sambuc }
6550f4a2713aSLionel Sambuc // If there are no qualifiers and no interface, we have an 'id'.
6551f4a2713aSLionel Sambuc return true;
6552f4a2713aSLionel Sambuc }
6553f4a2713aSLionel Sambuc // Both the right and left sides have qualifiers.
6554*0a6a1f1dSLionel Sambuc for (auto *lhsProto : lhsQID->quals()) {
6555f4a2713aSLionel Sambuc bool match = false;
6556f4a2713aSLionel Sambuc
6557f4a2713aSLionel Sambuc // when comparing an id<P> on lhs with a static type on rhs,
6558f4a2713aSLionel Sambuc // see if static class implements all of id's protocols, directly or
6559f4a2713aSLionel Sambuc // through its super class and categories.
6560*0a6a1f1dSLionel Sambuc for (auto *rhsProto : rhsOPT->quals()) {
6561f4a2713aSLionel Sambuc if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
6562f4a2713aSLionel Sambuc (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
6563f4a2713aSLionel Sambuc match = true;
6564f4a2713aSLionel Sambuc break;
6565f4a2713aSLionel Sambuc }
6566f4a2713aSLionel Sambuc }
6567f4a2713aSLionel Sambuc // If the RHS is a qualified interface pointer "NSString<P>*",
6568f4a2713aSLionel Sambuc // make sure we check the class hierarchy.
6569f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
6570*0a6a1f1dSLionel Sambuc for (auto *I : lhsQID->quals()) {
6571f4a2713aSLionel Sambuc // when comparing an id<P> on lhs with a static type on rhs,
6572f4a2713aSLionel Sambuc // see if static class implements all of id's protocols, directly or
6573f4a2713aSLionel Sambuc // through its super class and categories.
6574*0a6a1f1dSLionel Sambuc if (rhsID->ClassImplementsProtocol(I, true)) {
6575f4a2713aSLionel Sambuc match = true;
6576f4a2713aSLionel Sambuc break;
6577f4a2713aSLionel Sambuc }
6578f4a2713aSLionel Sambuc }
6579f4a2713aSLionel Sambuc }
6580f4a2713aSLionel Sambuc if (!match)
6581f4a2713aSLionel Sambuc return false;
6582f4a2713aSLionel Sambuc }
6583f4a2713aSLionel Sambuc
6584f4a2713aSLionel Sambuc return true;
6585f4a2713aSLionel Sambuc }
6586f4a2713aSLionel Sambuc
6587f4a2713aSLionel Sambuc const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
6588f4a2713aSLionel Sambuc assert(rhsQID && "One of the LHS/RHS should be id<x>");
6589f4a2713aSLionel Sambuc
6590f4a2713aSLionel Sambuc if (const ObjCObjectPointerType *lhsOPT =
6591f4a2713aSLionel Sambuc lhs->getAsObjCInterfacePointerType()) {
6592f4a2713aSLionel Sambuc // If both the right and left sides have qualifiers.
6593*0a6a1f1dSLionel Sambuc for (auto *lhsProto : lhsOPT->quals()) {
6594f4a2713aSLionel Sambuc bool match = false;
6595f4a2713aSLionel Sambuc
6596f4a2713aSLionel Sambuc // when comparing an id<P> on rhs with a static type on lhs,
6597f4a2713aSLionel Sambuc // see if static class implements all of id's protocols, directly or
6598f4a2713aSLionel Sambuc // through its super class and categories.
6599f4a2713aSLionel Sambuc // First, lhs protocols in the qualifier list must be found, direct
6600f4a2713aSLionel Sambuc // or indirect in rhs's qualifier list or it is a mismatch.
6601*0a6a1f1dSLionel Sambuc for (auto *rhsProto : rhsQID->quals()) {
6602f4a2713aSLionel Sambuc if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
6603f4a2713aSLionel Sambuc (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
6604f4a2713aSLionel Sambuc match = true;
6605f4a2713aSLionel Sambuc break;
6606f4a2713aSLionel Sambuc }
6607f4a2713aSLionel Sambuc }
6608f4a2713aSLionel Sambuc if (!match)
6609f4a2713aSLionel Sambuc return false;
6610f4a2713aSLionel Sambuc }
6611f4a2713aSLionel Sambuc
6612f4a2713aSLionel Sambuc // Static class's protocols, or its super class or category protocols
6613f4a2713aSLionel Sambuc // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
6614f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
6615f4a2713aSLionel Sambuc llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
6616f4a2713aSLionel Sambuc CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
6617f4a2713aSLionel Sambuc // This is rather dubious but matches gcc's behavior. If lhs has
6618f4a2713aSLionel Sambuc // no type qualifier and its class has no static protocol(s)
6619f4a2713aSLionel Sambuc // assume that it is mismatch.
6620f4a2713aSLionel Sambuc if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
6621f4a2713aSLionel Sambuc return false;
6622*0a6a1f1dSLionel Sambuc for (auto *lhsProto : LHSInheritedProtocols) {
6623f4a2713aSLionel Sambuc bool match = false;
6624*0a6a1f1dSLionel Sambuc for (auto *rhsProto : rhsQID->quals()) {
6625f4a2713aSLionel Sambuc if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
6626f4a2713aSLionel Sambuc (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
6627f4a2713aSLionel Sambuc match = true;
6628f4a2713aSLionel Sambuc break;
6629f4a2713aSLionel Sambuc }
6630f4a2713aSLionel Sambuc }
6631f4a2713aSLionel Sambuc if (!match)
6632f4a2713aSLionel Sambuc return false;
6633f4a2713aSLionel Sambuc }
6634f4a2713aSLionel Sambuc }
6635f4a2713aSLionel Sambuc return true;
6636f4a2713aSLionel Sambuc }
6637f4a2713aSLionel Sambuc return false;
6638f4a2713aSLionel Sambuc }
6639f4a2713aSLionel Sambuc
6640f4a2713aSLionel Sambuc /// canAssignObjCInterfaces - Return true if the two interface types are
6641f4a2713aSLionel Sambuc /// compatible for assignment from RHS to LHS. This handles validation of any
6642f4a2713aSLionel Sambuc /// protocol qualifiers on the LHS or RHS.
6643f4a2713aSLionel Sambuc ///
canAssignObjCInterfaces(const ObjCObjectPointerType * LHSOPT,const ObjCObjectPointerType * RHSOPT)6644f4a2713aSLionel Sambuc bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
6645f4a2713aSLionel Sambuc const ObjCObjectPointerType *RHSOPT) {
6646f4a2713aSLionel Sambuc const ObjCObjectType* LHS = LHSOPT->getObjectType();
6647f4a2713aSLionel Sambuc const ObjCObjectType* RHS = RHSOPT->getObjectType();
6648f4a2713aSLionel Sambuc
6649f4a2713aSLionel Sambuc // If either type represents the built-in 'id' or 'Class' types, return true.
6650f4a2713aSLionel Sambuc if (LHS->isObjCUnqualifiedIdOrClass() ||
6651f4a2713aSLionel Sambuc RHS->isObjCUnqualifiedIdOrClass())
6652f4a2713aSLionel Sambuc return true;
6653f4a2713aSLionel Sambuc
6654f4a2713aSLionel Sambuc if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
6655f4a2713aSLionel Sambuc return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
6656f4a2713aSLionel Sambuc QualType(RHSOPT,0),
6657f4a2713aSLionel Sambuc false);
6658f4a2713aSLionel Sambuc
6659f4a2713aSLionel Sambuc if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
6660f4a2713aSLionel Sambuc return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
6661f4a2713aSLionel Sambuc QualType(RHSOPT,0));
6662f4a2713aSLionel Sambuc
6663f4a2713aSLionel Sambuc // If we have 2 user-defined types, fall into that path.
6664f4a2713aSLionel Sambuc if (LHS->getInterface() && RHS->getInterface())
6665f4a2713aSLionel Sambuc return canAssignObjCInterfaces(LHS, RHS);
6666f4a2713aSLionel Sambuc
6667f4a2713aSLionel Sambuc return false;
6668f4a2713aSLionel Sambuc }
6669f4a2713aSLionel Sambuc
6670f4a2713aSLionel Sambuc /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
6671f4a2713aSLionel Sambuc /// for providing type-safety for objective-c pointers used to pass/return
6672f4a2713aSLionel Sambuc /// arguments in block literals. When passed as arguments, passing 'A*' where
6673f4a2713aSLionel Sambuc /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
6674f4a2713aSLionel Sambuc /// not OK. For the return type, the opposite is not OK.
canAssignObjCInterfacesInBlockPointer(const ObjCObjectPointerType * LHSOPT,const ObjCObjectPointerType * RHSOPT,bool BlockReturnType)6675f4a2713aSLionel Sambuc bool ASTContext::canAssignObjCInterfacesInBlockPointer(
6676f4a2713aSLionel Sambuc const ObjCObjectPointerType *LHSOPT,
6677f4a2713aSLionel Sambuc const ObjCObjectPointerType *RHSOPT,
6678f4a2713aSLionel Sambuc bool BlockReturnType) {
6679f4a2713aSLionel Sambuc if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
6680f4a2713aSLionel Sambuc return true;
6681f4a2713aSLionel Sambuc
6682f4a2713aSLionel Sambuc if (LHSOPT->isObjCBuiltinType()) {
6683f4a2713aSLionel Sambuc return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
6684f4a2713aSLionel Sambuc }
6685f4a2713aSLionel Sambuc
6686f4a2713aSLionel Sambuc if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
6687f4a2713aSLionel Sambuc return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
6688f4a2713aSLionel Sambuc QualType(RHSOPT,0),
6689f4a2713aSLionel Sambuc false);
6690f4a2713aSLionel Sambuc
6691f4a2713aSLionel Sambuc const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
6692f4a2713aSLionel Sambuc const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
6693f4a2713aSLionel Sambuc if (LHS && RHS) { // We have 2 user-defined types.
6694f4a2713aSLionel Sambuc if (LHS != RHS) {
6695f4a2713aSLionel Sambuc if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
6696f4a2713aSLionel Sambuc return BlockReturnType;
6697f4a2713aSLionel Sambuc if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
6698f4a2713aSLionel Sambuc return !BlockReturnType;
6699f4a2713aSLionel Sambuc }
6700f4a2713aSLionel Sambuc else
6701f4a2713aSLionel Sambuc return true;
6702f4a2713aSLionel Sambuc }
6703f4a2713aSLionel Sambuc return false;
6704f4a2713aSLionel Sambuc }
6705f4a2713aSLionel Sambuc
6706f4a2713aSLionel Sambuc /// getIntersectionOfProtocols - This routine finds the intersection of set
6707f4a2713aSLionel Sambuc /// of protocols inherited from two distinct objective-c pointer objects.
6708f4a2713aSLionel Sambuc /// It is used to build composite qualifier list of the composite type of
6709f4a2713aSLionel Sambuc /// the conditional expression involving two objective-c pointer objects.
6710f4a2713aSLionel Sambuc static
getIntersectionOfProtocols(ASTContext & Context,const ObjCObjectPointerType * LHSOPT,const ObjCObjectPointerType * RHSOPT,SmallVectorImpl<ObjCProtocolDecl * > & IntersectionOfProtocols)6711f4a2713aSLionel Sambuc void getIntersectionOfProtocols(ASTContext &Context,
6712f4a2713aSLionel Sambuc const ObjCObjectPointerType *LHSOPT,
6713f4a2713aSLionel Sambuc const ObjCObjectPointerType *RHSOPT,
6714f4a2713aSLionel Sambuc SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
6715f4a2713aSLionel Sambuc
6716f4a2713aSLionel Sambuc const ObjCObjectType* LHS = LHSOPT->getObjectType();
6717f4a2713aSLionel Sambuc const ObjCObjectType* RHS = RHSOPT->getObjectType();
6718f4a2713aSLionel Sambuc assert(LHS->getInterface() && "LHS must have an interface base");
6719f4a2713aSLionel Sambuc assert(RHS->getInterface() && "RHS must have an interface base");
6720f4a2713aSLionel Sambuc
6721f4a2713aSLionel Sambuc llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
6722f4a2713aSLionel Sambuc unsigned LHSNumProtocols = LHS->getNumProtocols();
6723f4a2713aSLionel Sambuc if (LHSNumProtocols > 0)
6724f4a2713aSLionel Sambuc InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
6725f4a2713aSLionel Sambuc else {
6726f4a2713aSLionel Sambuc llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
6727f4a2713aSLionel Sambuc Context.CollectInheritedProtocols(LHS->getInterface(),
6728f4a2713aSLionel Sambuc LHSInheritedProtocols);
6729f4a2713aSLionel Sambuc InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
6730f4a2713aSLionel Sambuc LHSInheritedProtocols.end());
6731f4a2713aSLionel Sambuc }
6732f4a2713aSLionel Sambuc
6733f4a2713aSLionel Sambuc unsigned RHSNumProtocols = RHS->getNumProtocols();
6734f4a2713aSLionel Sambuc if (RHSNumProtocols > 0) {
6735f4a2713aSLionel Sambuc ObjCProtocolDecl **RHSProtocols =
6736f4a2713aSLionel Sambuc const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
6737f4a2713aSLionel Sambuc for (unsigned i = 0; i < RHSNumProtocols; ++i)
6738f4a2713aSLionel Sambuc if (InheritedProtocolSet.count(RHSProtocols[i]))
6739f4a2713aSLionel Sambuc IntersectionOfProtocols.push_back(RHSProtocols[i]);
6740f4a2713aSLionel Sambuc } else {
6741f4a2713aSLionel Sambuc llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
6742f4a2713aSLionel Sambuc Context.CollectInheritedProtocols(RHS->getInterface(),
6743f4a2713aSLionel Sambuc RHSInheritedProtocols);
6744*0a6a1f1dSLionel Sambuc for (ObjCProtocolDecl *ProtDecl : RHSInheritedProtocols)
6745*0a6a1f1dSLionel Sambuc if (InheritedProtocolSet.count(ProtDecl))
6746*0a6a1f1dSLionel Sambuc IntersectionOfProtocols.push_back(ProtDecl);
6747f4a2713aSLionel Sambuc }
6748f4a2713aSLionel Sambuc }
6749f4a2713aSLionel Sambuc
6750f4a2713aSLionel Sambuc /// areCommonBaseCompatible - Returns common base class of the two classes if
6751f4a2713aSLionel Sambuc /// one found. Note that this is O'2 algorithm. But it will be called as the
6752f4a2713aSLionel Sambuc /// last type comparison in a ?-exp of ObjC pointer types before a
6753f4a2713aSLionel Sambuc /// warning is issued. So, its invokation is extremely rare.
areCommonBaseCompatible(const ObjCObjectPointerType * Lptr,const ObjCObjectPointerType * Rptr)6754f4a2713aSLionel Sambuc QualType ASTContext::areCommonBaseCompatible(
6755f4a2713aSLionel Sambuc const ObjCObjectPointerType *Lptr,
6756f4a2713aSLionel Sambuc const ObjCObjectPointerType *Rptr) {
6757f4a2713aSLionel Sambuc const ObjCObjectType *LHS = Lptr->getObjectType();
6758f4a2713aSLionel Sambuc const ObjCObjectType *RHS = Rptr->getObjectType();
6759f4a2713aSLionel Sambuc const ObjCInterfaceDecl* LDecl = LHS->getInterface();
6760f4a2713aSLionel Sambuc const ObjCInterfaceDecl* RDecl = RHS->getInterface();
6761f4a2713aSLionel Sambuc if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl)))
6762f4a2713aSLionel Sambuc return QualType();
6763f4a2713aSLionel Sambuc
6764f4a2713aSLionel Sambuc do {
6765f4a2713aSLionel Sambuc LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
6766f4a2713aSLionel Sambuc if (canAssignObjCInterfaces(LHS, RHS)) {
6767f4a2713aSLionel Sambuc SmallVector<ObjCProtocolDecl *, 8> Protocols;
6768f4a2713aSLionel Sambuc getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
6769f4a2713aSLionel Sambuc
6770f4a2713aSLionel Sambuc QualType Result = QualType(LHS, 0);
6771f4a2713aSLionel Sambuc if (!Protocols.empty())
6772f4a2713aSLionel Sambuc Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
6773f4a2713aSLionel Sambuc Result = getObjCObjectPointerType(Result);
6774f4a2713aSLionel Sambuc return Result;
6775f4a2713aSLionel Sambuc }
6776f4a2713aSLionel Sambuc } while ((LDecl = LDecl->getSuperClass()));
6777f4a2713aSLionel Sambuc
6778f4a2713aSLionel Sambuc return QualType();
6779f4a2713aSLionel Sambuc }
6780f4a2713aSLionel Sambuc
canAssignObjCInterfaces(const ObjCObjectType * LHS,const ObjCObjectType * RHS)6781f4a2713aSLionel Sambuc bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
6782f4a2713aSLionel Sambuc const ObjCObjectType *RHS) {
6783f4a2713aSLionel Sambuc assert(LHS->getInterface() && "LHS is not an interface type");
6784f4a2713aSLionel Sambuc assert(RHS->getInterface() && "RHS is not an interface type");
6785f4a2713aSLionel Sambuc
6786f4a2713aSLionel Sambuc // Verify that the base decls are compatible: the RHS must be a subclass of
6787f4a2713aSLionel Sambuc // the LHS.
6788f4a2713aSLionel Sambuc if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
6789f4a2713aSLionel Sambuc return false;
6790f4a2713aSLionel Sambuc
6791f4a2713aSLionel Sambuc // RHS must have a superset of the protocols in the LHS. If the LHS is not
6792f4a2713aSLionel Sambuc // protocol qualified at all, then we are good.
6793f4a2713aSLionel Sambuc if (LHS->getNumProtocols() == 0)
6794f4a2713aSLionel Sambuc return true;
6795f4a2713aSLionel Sambuc
6796*0a6a1f1dSLionel Sambuc // Okay, we know the LHS has protocol qualifiers. But RHS may or may not.
6797*0a6a1f1dSLionel Sambuc // More detailed analysis is required.
6798*0a6a1f1dSLionel Sambuc // OK, if LHS is same or a superclass of RHS *and*
6799*0a6a1f1dSLionel Sambuc // this LHS, or as RHS's super class is assignment compatible with LHS.
6800f4a2713aSLionel Sambuc bool IsSuperClass =
6801f4a2713aSLionel Sambuc LHS->getInterface()->isSuperClassOf(RHS->getInterface());
6802f4a2713aSLionel Sambuc if (IsSuperClass) {
6803f4a2713aSLionel Sambuc // OK if conversion of LHS to SuperClass results in narrowing of types
6804f4a2713aSLionel Sambuc // ; i.e., SuperClass may implement at least one of the protocols
6805f4a2713aSLionel Sambuc // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
6806f4a2713aSLionel Sambuc // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
6807f4a2713aSLionel Sambuc llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
6808f4a2713aSLionel Sambuc CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
6809*0a6a1f1dSLionel Sambuc // Also, if RHS has explicit quelifiers, include them for comparing with LHS's
6810*0a6a1f1dSLionel Sambuc // qualifiers.
6811*0a6a1f1dSLionel Sambuc for (auto *RHSPI : RHS->quals())
6812*0a6a1f1dSLionel Sambuc SuperClassInheritedProtocols.insert(RHSPI->getCanonicalDecl());
6813*0a6a1f1dSLionel Sambuc // If there is no protocols associated with RHS, it is not a match.
6814f4a2713aSLionel Sambuc if (SuperClassInheritedProtocols.empty())
6815f4a2713aSLionel Sambuc return false;
6816f4a2713aSLionel Sambuc
6817*0a6a1f1dSLionel Sambuc for (const auto *LHSProto : LHS->quals()) {
6818f4a2713aSLionel Sambuc bool SuperImplementsProtocol = false;
6819*0a6a1f1dSLionel Sambuc for (auto *SuperClassProto : SuperClassInheritedProtocols)
6820f4a2713aSLionel Sambuc if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
6821f4a2713aSLionel Sambuc SuperImplementsProtocol = true;
6822f4a2713aSLionel Sambuc break;
6823f4a2713aSLionel Sambuc }
6824f4a2713aSLionel Sambuc if (!SuperImplementsProtocol)
6825f4a2713aSLionel Sambuc return false;
6826f4a2713aSLionel Sambuc }
6827f4a2713aSLionel Sambuc return true;
6828f4a2713aSLionel Sambuc }
6829f4a2713aSLionel Sambuc return false;
6830f4a2713aSLionel Sambuc }
6831f4a2713aSLionel Sambuc
areComparableObjCPointerTypes(QualType LHS,QualType RHS)6832f4a2713aSLionel Sambuc bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
6833f4a2713aSLionel Sambuc // get the "pointed to" types
6834f4a2713aSLionel Sambuc const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
6835f4a2713aSLionel Sambuc const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
6836f4a2713aSLionel Sambuc
6837f4a2713aSLionel Sambuc if (!LHSOPT || !RHSOPT)
6838f4a2713aSLionel Sambuc return false;
6839f4a2713aSLionel Sambuc
6840f4a2713aSLionel Sambuc return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
6841f4a2713aSLionel Sambuc canAssignObjCInterfaces(RHSOPT, LHSOPT);
6842f4a2713aSLionel Sambuc }
6843f4a2713aSLionel Sambuc
canBindObjCObjectType(QualType To,QualType From)6844f4a2713aSLionel Sambuc bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
6845f4a2713aSLionel Sambuc return canAssignObjCInterfaces(
6846f4a2713aSLionel Sambuc getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
6847f4a2713aSLionel Sambuc getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
6848f4a2713aSLionel Sambuc }
6849f4a2713aSLionel Sambuc
6850f4a2713aSLionel Sambuc /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
6851f4a2713aSLionel Sambuc /// both shall have the identically qualified version of a compatible type.
6852f4a2713aSLionel Sambuc /// C99 6.2.7p1: Two types have compatible types if their types are the
6853f4a2713aSLionel Sambuc /// same. See 6.7.[2,3,5] for additional rules.
typesAreCompatible(QualType LHS,QualType RHS,bool CompareUnqualified)6854f4a2713aSLionel Sambuc bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
6855f4a2713aSLionel Sambuc bool CompareUnqualified) {
6856f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus)
6857f4a2713aSLionel Sambuc return hasSameType(LHS, RHS);
6858f4a2713aSLionel Sambuc
6859f4a2713aSLionel Sambuc return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
6860f4a2713aSLionel Sambuc }
6861f4a2713aSLionel Sambuc
propertyTypesAreCompatible(QualType LHS,QualType RHS)6862f4a2713aSLionel Sambuc bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) {
6863f4a2713aSLionel Sambuc return typesAreCompatible(LHS, RHS);
6864f4a2713aSLionel Sambuc }
6865f4a2713aSLionel Sambuc
typesAreBlockPointerCompatible(QualType LHS,QualType RHS)6866f4a2713aSLionel Sambuc bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
6867f4a2713aSLionel Sambuc return !mergeTypes(LHS, RHS, true).isNull();
6868f4a2713aSLionel Sambuc }
6869f4a2713aSLionel Sambuc
6870f4a2713aSLionel Sambuc /// mergeTransparentUnionType - if T is a transparent union type and a member
6871f4a2713aSLionel Sambuc /// of T is compatible with SubType, return the merged type, else return
6872f4a2713aSLionel Sambuc /// QualType()
mergeTransparentUnionType(QualType T,QualType SubType,bool OfBlockPointer,bool Unqualified)6873f4a2713aSLionel Sambuc QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
6874f4a2713aSLionel Sambuc bool OfBlockPointer,
6875f4a2713aSLionel Sambuc bool Unqualified) {
6876f4a2713aSLionel Sambuc if (const RecordType *UT = T->getAsUnionType()) {
6877f4a2713aSLionel Sambuc RecordDecl *UD = UT->getDecl();
6878f4a2713aSLionel Sambuc if (UD->hasAttr<TransparentUnionAttr>()) {
6879*0a6a1f1dSLionel Sambuc for (const auto *I : UD->fields()) {
6880*0a6a1f1dSLionel Sambuc QualType ET = I->getType().getUnqualifiedType();
6881f4a2713aSLionel Sambuc QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
6882f4a2713aSLionel Sambuc if (!MT.isNull())
6883f4a2713aSLionel Sambuc return MT;
6884f4a2713aSLionel Sambuc }
6885f4a2713aSLionel Sambuc }
6886f4a2713aSLionel Sambuc }
6887f4a2713aSLionel Sambuc
6888f4a2713aSLionel Sambuc return QualType();
6889f4a2713aSLionel Sambuc }
6890f4a2713aSLionel Sambuc
6891*0a6a1f1dSLionel Sambuc /// mergeFunctionParameterTypes - merge two types which appear as function
6892*0a6a1f1dSLionel Sambuc /// parameter types
mergeFunctionParameterTypes(QualType lhs,QualType rhs,bool OfBlockPointer,bool Unqualified)6893*0a6a1f1dSLionel Sambuc QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs,
6894f4a2713aSLionel Sambuc bool OfBlockPointer,
6895f4a2713aSLionel Sambuc bool Unqualified) {
6896f4a2713aSLionel Sambuc // GNU extension: two types are compatible if they appear as a function
6897f4a2713aSLionel Sambuc // argument, one of the types is a transparent union type and the other
6898f4a2713aSLionel Sambuc // type is compatible with a union member
6899f4a2713aSLionel Sambuc QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
6900f4a2713aSLionel Sambuc Unqualified);
6901f4a2713aSLionel Sambuc if (!lmerge.isNull())
6902f4a2713aSLionel Sambuc return lmerge;
6903f4a2713aSLionel Sambuc
6904f4a2713aSLionel Sambuc QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
6905f4a2713aSLionel Sambuc Unqualified);
6906f4a2713aSLionel Sambuc if (!rmerge.isNull())
6907f4a2713aSLionel Sambuc return rmerge;
6908f4a2713aSLionel Sambuc
6909f4a2713aSLionel Sambuc return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
6910f4a2713aSLionel Sambuc }
6911f4a2713aSLionel Sambuc
mergeFunctionTypes(QualType lhs,QualType rhs,bool OfBlockPointer,bool Unqualified)6912f4a2713aSLionel Sambuc QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
6913f4a2713aSLionel Sambuc bool OfBlockPointer,
6914f4a2713aSLionel Sambuc bool Unqualified) {
6915f4a2713aSLionel Sambuc const FunctionType *lbase = lhs->getAs<FunctionType>();
6916f4a2713aSLionel Sambuc const FunctionType *rbase = rhs->getAs<FunctionType>();
6917f4a2713aSLionel Sambuc const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
6918f4a2713aSLionel Sambuc const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
6919f4a2713aSLionel Sambuc bool allLTypes = true;
6920f4a2713aSLionel Sambuc bool allRTypes = true;
6921f4a2713aSLionel Sambuc
6922f4a2713aSLionel Sambuc // Check return type
6923f4a2713aSLionel Sambuc QualType retType;
6924f4a2713aSLionel Sambuc if (OfBlockPointer) {
6925*0a6a1f1dSLionel Sambuc QualType RHS = rbase->getReturnType();
6926*0a6a1f1dSLionel Sambuc QualType LHS = lbase->getReturnType();
6927f4a2713aSLionel Sambuc bool UnqualifiedResult = Unqualified;
6928f4a2713aSLionel Sambuc if (!UnqualifiedResult)
6929f4a2713aSLionel Sambuc UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
6930f4a2713aSLionel Sambuc retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
6931f4a2713aSLionel Sambuc }
6932f4a2713aSLionel Sambuc else
6933*0a6a1f1dSLionel Sambuc retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), false,
6934f4a2713aSLionel Sambuc Unqualified);
6935f4a2713aSLionel Sambuc if (retType.isNull()) return QualType();
6936f4a2713aSLionel Sambuc
6937f4a2713aSLionel Sambuc if (Unqualified)
6938f4a2713aSLionel Sambuc retType = retType.getUnqualifiedType();
6939f4a2713aSLionel Sambuc
6940*0a6a1f1dSLionel Sambuc CanQualType LRetType = getCanonicalType(lbase->getReturnType());
6941*0a6a1f1dSLionel Sambuc CanQualType RRetType = getCanonicalType(rbase->getReturnType());
6942f4a2713aSLionel Sambuc if (Unqualified) {
6943f4a2713aSLionel Sambuc LRetType = LRetType.getUnqualifiedType();
6944f4a2713aSLionel Sambuc RRetType = RRetType.getUnqualifiedType();
6945f4a2713aSLionel Sambuc }
6946f4a2713aSLionel Sambuc
6947f4a2713aSLionel Sambuc if (getCanonicalType(retType) != LRetType)
6948f4a2713aSLionel Sambuc allLTypes = false;
6949f4a2713aSLionel Sambuc if (getCanonicalType(retType) != RRetType)
6950f4a2713aSLionel Sambuc allRTypes = false;
6951f4a2713aSLionel Sambuc
6952f4a2713aSLionel Sambuc // FIXME: double check this
6953f4a2713aSLionel Sambuc // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
6954f4a2713aSLionel Sambuc // rbase->getRegParmAttr() != 0 &&
6955f4a2713aSLionel Sambuc // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
6956f4a2713aSLionel Sambuc FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
6957f4a2713aSLionel Sambuc FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
6958f4a2713aSLionel Sambuc
6959f4a2713aSLionel Sambuc // Compatible functions must have compatible calling conventions
6960f4a2713aSLionel Sambuc if (lbaseInfo.getCC() != rbaseInfo.getCC())
6961f4a2713aSLionel Sambuc return QualType();
6962f4a2713aSLionel Sambuc
6963f4a2713aSLionel Sambuc // Regparm is part of the calling convention.
6964f4a2713aSLionel Sambuc if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
6965f4a2713aSLionel Sambuc return QualType();
6966f4a2713aSLionel Sambuc if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
6967f4a2713aSLionel Sambuc return QualType();
6968f4a2713aSLionel Sambuc
6969f4a2713aSLionel Sambuc if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult())
6970f4a2713aSLionel Sambuc return QualType();
6971f4a2713aSLionel Sambuc
6972f4a2713aSLionel Sambuc // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
6973f4a2713aSLionel Sambuc bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
6974f4a2713aSLionel Sambuc
6975f4a2713aSLionel Sambuc if (lbaseInfo.getNoReturn() != NoReturn)
6976f4a2713aSLionel Sambuc allLTypes = false;
6977f4a2713aSLionel Sambuc if (rbaseInfo.getNoReturn() != NoReturn)
6978f4a2713aSLionel Sambuc allRTypes = false;
6979f4a2713aSLionel Sambuc
6980f4a2713aSLionel Sambuc FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn);
6981f4a2713aSLionel Sambuc
6982f4a2713aSLionel Sambuc if (lproto && rproto) { // two C99 style function prototypes
6983f4a2713aSLionel Sambuc assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
6984f4a2713aSLionel Sambuc "C++ shouldn't be here");
6985*0a6a1f1dSLionel Sambuc // Compatible functions must have the same number of parameters
6986*0a6a1f1dSLionel Sambuc if (lproto->getNumParams() != rproto->getNumParams())
6987f4a2713aSLionel Sambuc return QualType();
6988f4a2713aSLionel Sambuc
6989f4a2713aSLionel Sambuc // Variadic and non-variadic functions aren't compatible
6990f4a2713aSLionel Sambuc if (lproto->isVariadic() != rproto->isVariadic())
6991f4a2713aSLionel Sambuc return QualType();
6992f4a2713aSLionel Sambuc
6993f4a2713aSLionel Sambuc if (lproto->getTypeQuals() != rproto->getTypeQuals())
6994f4a2713aSLionel Sambuc return QualType();
6995f4a2713aSLionel Sambuc
6996f4a2713aSLionel Sambuc if (LangOpts.ObjCAutoRefCount &&
6997f4a2713aSLionel Sambuc !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto))
6998f4a2713aSLionel Sambuc return QualType();
6999f4a2713aSLionel Sambuc
7000*0a6a1f1dSLionel Sambuc // Check parameter type compatibility
7001f4a2713aSLionel Sambuc SmallVector<QualType, 10> types;
7002*0a6a1f1dSLionel Sambuc for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) {
7003*0a6a1f1dSLionel Sambuc QualType lParamType = lproto->getParamType(i).getUnqualifiedType();
7004*0a6a1f1dSLionel Sambuc QualType rParamType = rproto->getParamType(i).getUnqualifiedType();
7005*0a6a1f1dSLionel Sambuc QualType paramType = mergeFunctionParameterTypes(
7006*0a6a1f1dSLionel Sambuc lParamType, rParamType, OfBlockPointer, Unqualified);
7007*0a6a1f1dSLionel Sambuc if (paramType.isNull())
7008*0a6a1f1dSLionel Sambuc return QualType();
7009f4a2713aSLionel Sambuc
7010f4a2713aSLionel Sambuc if (Unqualified)
7011*0a6a1f1dSLionel Sambuc paramType = paramType.getUnqualifiedType();
7012f4a2713aSLionel Sambuc
7013*0a6a1f1dSLionel Sambuc types.push_back(paramType);
7014f4a2713aSLionel Sambuc if (Unqualified) {
7015*0a6a1f1dSLionel Sambuc lParamType = lParamType.getUnqualifiedType();
7016*0a6a1f1dSLionel Sambuc rParamType = rParamType.getUnqualifiedType();
7017f4a2713aSLionel Sambuc }
7018f4a2713aSLionel Sambuc
7019*0a6a1f1dSLionel Sambuc if (getCanonicalType(paramType) != getCanonicalType(lParamType))
7020f4a2713aSLionel Sambuc allLTypes = false;
7021*0a6a1f1dSLionel Sambuc if (getCanonicalType(paramType) != getCanonicalType(rParamType))
7022f4a2713aSLionel Sambuc allRTypes = false;
7023f4a2713aSLionel Sambuc }
7024f4a2713aSLionel Sambuc
7025f4a2713aSLionel Sambuc if (allLTypes) return lhs;
7026f4a2713aSLionel Sambuc if (allRTypes) return rhs;
7027f4a2713aSLionel Sambuc
7028f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
7029f4a2713aSLionel Sambuc EPI.ExtInfo = einfo;
7030f4a2713aSLionel Sambuc return getFunctionType(retType, types, EPI);
7031f4a2713aSLionel Sambuc }
7032f4a2713aSLionel Sambuc
7033f4a2713aSLionel Sambuc if (lproto) allRTypes = false;
7034f4a2713aSLionel Sambuc if (rproto) allLTypes = false;
7035f4a2713aSLionel Sambuc
7036f4a2713aSLionel Sambuc const FunctionProtoType *proto = lproto ? lproto : rproto;
7037f4a2713aSLionel Sambuc if (proto) {
7038f4a2713aSLionel Sambuc assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
7039f4a2713aSLionel Sambuc if (proto->isVariadic()) return QualType();
7040f4a2713aSLionel Sambuc // Check that the types are compatible with the types that
7041f4a2713aSLionel Sambuc // would result from default argument promotions (C99 6.7.5.3p15).
7042f4a2713aSLionel Sambuc // The only types actually affected are promotable integer
7043f4a2713aSLionel Sambuc // types and floats, which would be passed as a different
7044f4a2713aSLionel Sambuc // type depending on whether the prototype is visible.
7045*0a6a1f1dSLionel Sambuc for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) {
7046*0a6a1f1dSLionel Sambuc QualType paramTy = proto->getParamType(i);
7047f4a2713aSLionel Sambuc
7048f4a2713aSLionel Sambuc // Look at the converted type of enum types, since that is the type used
7049f4a2713aSLionel Sambuc // to pass enum values.
7050*0a6a1f1dSLionel Sambuc if (const EnumType *Enum = paramTy->getAs<EnumType>()) {
7051*0a6a1f1dSLionel Sambuc paramTy = Enum->getDecl()->getIntegerType();
7052*0a6a1f1dSLionel Sambuc if (paramTy.isNull())
7053f4a2713aSLionel Sambuc return QualType();
7054f4a2713aSLionel Sambuc }
7055f4a2713aSLionel Sambuc
7056*0a6a1f1dSLionel Sambuc if (paramTy->isPromotableIntegerType() ||
7057*0a6a1f1dSLionel Sambuc getCanonicalType(paramTy).getUnqualifiedType() == FloatTy)
7058f4a2713aSLionel Sambuc return QualType();
7059f4a2713aSLionel Sambuc }
7060f4a2713aSLionel Sambuc
7061f4a2713aSLionel Sambuc if (allLTypes) return lhs;
7062f4a2713aSLionel Sambuc if (allRTypes) return rhs;
7063f4a2713aSLionel Sambuc
7064f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
7065f4a2713aSLionel Sambuc EPI.ExtInfo = einfo;
7066*0a6a1f1dSLionel Sambuc return getFunctionType(retType, proto->getParamTypes(), EPI);
7067f4a2713aSLionel Sambuc }
7068f4a2713aSLionel Sambuc
7069f4a2713aSLionel Sambuc if (allLTypes) return lhs;
7070f4a2713aSLionel Sambuc if (allRTypes) return rhs;
7071f4a2713aSLionel Sambuc return getFunctionNoProtoType(retType, einfo);
7072f4a2713aSLionel Sambuc }
7073f4a2713aSLionel Sambuc
7074f4a2713aSLionel Sambuc /// Given that we have an enum type and a non-enum type, try to merge them.
mergeEnumWithInteger(ASTContext & Context,const EnumType * ET,QualType other,bool isBlockReturnType)7075f4a2713aSLionel Sambuc static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET,
7076f4a2713aSLionel Sambuc QualType other, bool isBlockReturnType) {
7077f4a2713aSLionel Sambuc // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
7078f4a2713aSLionel Sambuc // a signed integer type, or an unsigned integer type.
7079f4a2713aSLionel Sambuc // Compatibility is based on the underlying type, not the promotion
7080f4a2713aSLionel Sambuc // type.
7081f4a2713aSLionel Sambuc QualType underlyingType = ET->getDecl()->getIntegerType();
7082f4a2713aSLionel Sambuc if (underlyingType.isNull()) return QualType();
7083f4a2713aSLionel Sambuc if (Context.hasSameType(underlyingType, other))
7084f4a2713aSLionel Sambuc return other;
7085f4a2713aSLionel Sambuc
7086f4a2713aSLionel Sambuc // In block return types, we're more permissive and accept any
7087f4a2713aSLionel Sambuc // integral type of the same size.
7088f4a2713aSLionel Sambuc if (isBlockReturnType && other->isIntegerType() &&
7089f4a2713aSLionel Sambuc Context.getTypeSize(underlyingType) == Context.getTypeSize(other))
7090f4a2713aSLionel Sambuc return other;
7091f4a2713aSLionel Sambuc
7092f4a2713aSLionel Sambuc return QualType();
7093f4a2713aSLionel Sambuc }
7094f4a2713aSLionel Sambuc
mergeTypes(QualType LHS,QualType RHS,bool OfBlockPointer,bool Unqualified,bool BlockReturnType)7095f4a2713aSLionel Sambuc QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
7096f4a2713aSLionel Sambuc bool OfBlockPointer,
7097f4a2713aSLionel Sambuc bool Unqualified, bool BlockReturnType) {
7098f4a2713aSLionel Sambuc // C++ [expr]: If an expression initially has the type "reference to T", the
7099f4a2713aSLionel Sambuc // type is adjusted to "T" prior to any further analysis, the expression
7100f4a2713aSLionel Sambuc // designates the object or function denoted by the reference, and the
7101f4a2713aSLionel Sambuc // expression is an lvalue unless the reference is an rvalue reference and
7102f4a2713aSLionel Sambuc // the expression is a function call (possibly inside parentheses).
7103f4a2713aSLionel Sambuc assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
7104f4a2713aSLionel Sambuc assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
7105f4a2713aSLionel Sambuc
7106f4a2713aSLionel Sambuc if (Unqualified) {
7107f4a2713aSLionel Sambuc LHS = LHS.getUnqualifiedType();
7108f4a2713aSLionel Sambuc RHS = RHS.getUnqualifiedType();
7109f4a2713aSLionel Sambuc }
7110f4a2713aSLionel Sambuc
7111f4a2713aSLionel Sambuc QualType LHSCan = getCanonicalType(LHS),
7112f4a2713aSLionel Sambuc RHSCan = getCanonicalType(RHS);
7113f4a2713aSLionel Sambuc
7114f4a2713aSLionel Sambuc // If two types are identical, they are compatible.
7115f4a2713aSLionel Sambuc if (LHSCan == RHSCan)
7116f4a2713aSLionel Sambuc return LHS;
7117f4a2713aSLionel Sambuc
7118f4a2713aSLionel Sambuc // If the qualifiers are different, the types aren't compatible... mostly.
7119f4a2713aSLionel Sambuc Qualifiers LQuals = LHSCan.getLocalQualifiers();
7120f4a2713aSLionel Sambuc Qualifiers RQuals = RHSCan.getLocalQualifiers();
7121f4a2713aSLionel Sambuc if (LQuals != RQuals) {
7122f4a2713aSLionel Sambuc // If any of these qualifiers are different, we have a type
7123f4a2713aSLionel Sambuc // mismatch.
7124f4a2713aSLionel Sambuc if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
7125f4a2713aSLionel Sambuc LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
7126f4a2713aSLionel Sambuc LQuals.getObjCLifetime() != RQuals.getObjCLifetime())
7127f4a2713aSLionel Sambuc return QualType();
7128f4a2713aSLionel Sambuc
7129f4a2713aSLionel Sambuc // Exactly one GC qualifier difference is allowed: __strong is
7130f4a2713aSLionel Sambuc // okay if the other type has no GC qualifier but is an Objective
7131f4a2713aSLionel Sambuc // C object pointer (i.e. implicitly strong by default). We fix
7132f4a2713aSLionel Sambuc // this by pretending that the unqualified type was actually
7133f4a2713aSLionel Sambuc // qualified __strong.
7134f4a2713aSLionel Sambuc Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
7135f4a2713aSLionel Sambuc Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
7136f4a2713aSLionel Sambuc assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
7137f4a2713aSLionel Sambuc
7138f4a2713aSLionel Sambuc if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
7139f4a2713aSLionel Sambuc return QualType();
7140f4a2713aSLionel Sambuc
7141f4a2713aSLionel Sambuc if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
7142f4a2713aSLionel Sambuc return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
7143f4a2713aSLionel Sambuc }
7144f4a2713aSLionel Sambuc if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
7145f4a2713aSLionel Sambuc return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
7146f4a2713aSLionel Sambuc }
7147f4a2713aSLionel Sambuc return QualType();
7148f4a2713aSLionel Sambuc }
7149f4a2713aSLionel Sambuc
7150f4a2713aSLionel Sambuc // Okay, qualifiers are equal.
7151f4a2713aSLionel Sambuc
7152f4a2713aSLionel Sambuc Type::TypeClass LHSClass = LHSCan->getTypeClass();
7153f4a2713aSLionel Sambuc Type::TypeClass RHSClass = RHSCan->getTypeClass();
7154f4a2713aSLionel Sambuc
7155f4a2713aSLionel Sambuc // We want to consider the two function types to be the same for these
7156f4a2713aSLionel Sambuc // comparisons, just force one to the other.
7157f4a2713aSLionel Sambuc if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
7158f4a2713aSLionel Sambuc if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
7159f4a2713aSLionel Sambuc
7160f4a2713aSLionel Sambuc // Same as above for arrays
7161f4a2713aSLionel Sambuc if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
7162f4a2713aSLionel Sambuc LHSClass = Type::ConstantArray;
7163f4a2713aSLionel Sambuc if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
7164f4a2713aSLionel Sambuc RHSClass = Type::ConstantArray;
7165f4a2713aSLionel Sambuc
7166f4a2713aSLionel Sambuc // ObjCInterfaces are just specialized ObjCObjects.
7167f4a2713aSLionel Sambuc if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
7168f4a2713aSLionel Sambuc if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
7169f4a2713aSLionel Sambuc
7170f4a2713aSLionel Sambuc // Canonicalize ExtVector -> Vector.
7171f4a2713aSLionel Sambuc if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
7172f4a2713aSLionel Sambuc if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
7173f4a2713aSLionel Sambuc
7174f4a2713aSLionel Sambuc // If the canonical type classes don't match.
7175f4a2713aSLionel Sambuc if (LHSClass != RHSClass) {
7176f4a2713aSLionel Sambuc // Note that we only have special rules for turning block enum
7177f4a2713aSLionel Sambuc // returns into block int returns, not vice-versa.
7178f4a2713aSLionel Sambuc if (const EnumType* ETy = LHS->getAs<EnumType>()) {
7179f4a2713aSLionel Sambuc return mergeEnumWithInteger(*this, ETy, RHS, false);
7180f4a2713aSLionel Sambuc }
7181f4a2713aSLionel Sambuc if (const EnumType* ETy = RHS->getAs<EnumType>()) {
7182f4a2713aSLionel Sambuc return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType);
7183f4a2713aSLionel Sambuc }
7184f4a2713aSLionel Sambuc // allow block pointer type to match an 'id' type.
7185f4a2713aSLionel Sambuc if (OfBlockPointer && !BlockReturnType) {
7186f4a2713aSLionel Sambuc if (LHS->isObjCIdType() && RHS->isBlockPointerType())
7187f4a2713aSLionel Sambuc return LHS;
7188f4a2713aSLionel Sambuc if (RHS->isObjCIdType() && LHS->isBlockPointerType())
7189f4a2713aSLionel Sambuc return RHS;
7190f4a2713aSLionel Sambuc }
7191f4a2713aSLionel Sambuc
7192f4a2713aSLionel Sambuc return QualType();
7193f4a2713aSLionel Sambuc }
7194f4a2713aSLionel Sambuc
7195f4a2713aSLionel Sambuc // The canonical type classes match.
7196f4a2713aSLionel Sambuc switch (LHSClass) {
7197f4a2713aSLionel Sambuc #define TYPE(Class, Base)
7198f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Class, Base)
7199f4a2713aSLionel Sambuc #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
7200f4a2713aSLionel Sambuc #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
7201f4a2713aSLionel Sambuc #define DEPENDENT_TYPE(Class, Base) case Type::Class:
7202f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
7203f4a2713aSLionel Sambuc llvm_unreachable("Non-canonical and dependent types shouldn't get here");
7204f4a2713aSLionel Sambuc
7205f4a2713aSLionel Sambuc case Type::Auto:
7206f4a2713aSLionel Sambuc case Type::LValueReference:
7207f4a2713aSLionel Sambuc case Type::RValueReference:
7208f4a2713aSLionel Sambuc case Type::MemberPointer:
7209f4a2713aSLionel Sambuc llvm_unreachable("C++ should never be in mergeTypes");
7210f4a2713aSLionel Sambuc
7211f4a2713aSLionel Sambuc case Type::ObjCInterface:
7212f4a2713aSLionel Sambuc case Type::IncompleteArray:
7213f4a2713aSLionel Sambuc case Type::VariableArray:
7214f4a2713aSLionel Sambuc case Type::FunctionProto:
7215f4a2713aSLionel Sambuc case Type::ExtVector:
7216f4a2713aSLionel Sambuc llvm_unreachable("Types are eliminated above");
7217f4a2713aSLionel Sambuc
7218f4a2713aSLionel Sambuc case Type::Pointer:
7219f4a2713aSLionel Sambuc {
7220f4a2713aSLionel Sambuc // Merge two pointer types, while trying to preserve typedef info
7221f4a2713aSLionel Sambuc QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
7222f4a2713aSLionel Sambuc QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
7223f4a2713aSLionel Sambuc if (Unqualified) {
7224f4a2713aSLionel Sambuc LHSPointee = LHSPointee.getUnqualifiedType();
7225f4a2713aSLionel Sambuc RHSPointee = RHSPointee.getUnqualifiedType();
7226f4a2713aSLionel Sambuc }
7227f4a2713aSLionel Sambuc QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
7228f4a2713aSLionel Sambuc Unqualified);
7229f4a2713aSLionel Sambuc if (ResultType.isNull()) return QualType();
7230f4a2713aSLionel Sambuc if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
7231f4a2713aSLionel Sambuc return LHS;
7232f4a2713aSLionel Sambuc if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
7233f4a2713aSLionel Sambuc return RHS;
7234f4a2713aSLionel Sambuc return getPointerType(ResultType);
7235f4a2713aSLionel Sambuc }
7236f4a2713aSLionel Sambuc case Type::BlockPointer:
7237f4a2713aSLionel Sambuc {
7238f4a2713aSLionel Sambuc // Merge two block pointer types, while trying to preserve typedef info
7239f4a2713aSLionel Sambuc QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
7240f4a2713aSLionel Sambuc QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
7241f4a2713aSLionel Sambuc if (Unqualified) {
7242f4a2713aSLionel Sambuc LHSPointee = LHSPointee.getUnqualifiedType();
7243f4a2713aSLionel Sambuc RHSPointee = RHSPointee.getUnqualifiedType();
7244f4a2713aSLionel Sambuc }
7245f4a2713aSLionel Sambuc QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
7246f4a2713aSLionel Sambuc Unqualified);
7247f4a2713aSLionel Sambuc if (ResultType.isNull()) return QualType();
7248f4a2713aSLionel Sambuc if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
7249f4a2713aSLionel Sambuc return LHS;
7250f4a2713aSLionel Sambuc if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
7251f4a2713aSLionel Sambuc return RHS;
7252f4a2713aSLionel Sambuc return getBlockPointerType(ResultType);
7253f4a2713aSLionel Sambuc }
7254f4a2713aSLionel Sambuc case Type::Atomic:
7255f4a2713aSLionel Sambuc {
7256f4a2713aSLionel Sambuc // Merge two pointer types, while trying to preserve typedef info
7257f4a2713aSLionel Sambuc QualType LHSValue = LHS->getAs<AtomicType>()->getValueType();
7258f4a2713aSLionel Sambuc QualType RHSValue = RHS->getAs<AtomicType>()->getValueType();
7259f4a2713aSLionel Sambuc if (Unqualified) {
7260f4a2713aSLionel Sambuc LHSValue = LHSValue.getUnqualifiedType();
7261f4a2713aSLionel Sambuc RHSValue = RHSValue.getUnqualifiedType();
7262f4a2713aSLionel Sambuc }
7263f4a2713aSLionel Sambuc QualType ResultType = mergeTypes(LHSValue, RHSValue, false,
7264f4a2713aSLionel Sambuc Unqualified);
7265f4a2713aSLionel Sambuc if (ResultType.isNull()) return QualType();
7266f4a2713aSLionel Sambuc if (getCanonicalType(LHSValue) == getCanonicalType(ResultType))
7267f4a2713aSLionel Sambuc return LHS;
7268f4a2713aSLionel Sambuc if (getCanonicalType(RHSValue) == getCanonicalType(ResultType))
7269f4a2713aSLionel Sambuc return RHS;
7270f4a2713aSLionel Sambuc return getAtomicType(ResultType);
7271f4a2713aSLionel Sambuc }
7272f4a2713aSLionel Sambuc case Type::ConstantArray:
7273f4a2713aSLionel Sambuc {
7274f4a2713aSLionel Sambuc const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
7275f4a2713aSLionel Sambuc const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
7276f4a2713aSLionel Sambuc if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
7277f4a2713aSLionel Sambuc return QualType();
7278f4a2713aSLionel Sambuc
7279f4a2713aSLionel Sambuc QualType LHSElem = getAsArrayType(LHS)->getElementType();
7280f4a2713aSLionel Sambuc QualType RHSElem = getAsArrayType(RHS)->getElementType();
7281f4a2713aSLionel Sambuc if (Unqualified) {
7282f4a2713aSLionel Sambuc LHSElem = LHSElem.getUnqualifiedType();
7283f4a2713aSLionel Sambuc RHSElem = RHSElem.getUnqualifiedType();
7284f4a2713aSLionel Sambuc }
7285f4a2713aSLionel Sambuc
7286f4a2713aSLionel Sambuc QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
7287f4a2713aSLionel Sambuc if (ResultType.isNull()) return QualType();
7288f4a2713aSLionel Sambuc if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
7289f4a2713aSLionel Sambuc return LHS;
7290f4a2713aSLionel Sambuc if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
7291f4a2713aSLionel Sambuc return RHS;
7292f4a2713aSLionel Sambuc if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
7293f4a2713aSLionel Sambuc ArrayType::ArraySizeModifier(), 0);
7294f4a2713aSLionel Sambuc if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
7295f4a2713aSLionel Sambuc ArrayType::ArraySizeModifier(), 0);
7296f4a2713aSLionel Sambuc const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
7297f4a2713aSLionel Sambuc const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
7298f4a2713aSLionel Sambuc if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
7299f4a2713aSLionel Sambuc return LHS;
7300f4a2713aSLionel Sambuc if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
7301f4a2713aSLionel Sambuc return RHS;
7302f4a2713aSLionel Sambuc if (LVAT) {
7303f4a2713aSLionel Sambuc // FIXME: This isn't correct! But tricky to implement because
7304f4a2713aSLionel Sambuc // the array's size has to be the size of LHS, but the type
7305f4a2713aSLionel Sambuc // has to be different.
7306f4a2713aSLionel Sambuc return LHS;
7307f4a2713aSLionel Sambuc }
7308f4a2713aSLionel Sambuc if (RVAT) {
7309f4a2713aSLionel Sambuc // FIXME: This isn't correct! But tricky to implement because
7310f4a2713aSLionel Sambuc // the array's size has to be the size of RHS, but the type
7311f4a2713aSLionel Sambuc // has to be different.
7312f4a2713aSLionel Sambuc return RHS;
7313f4a2713aSLionel Sambuc }
7314f4a2713aSLionel Sambuc if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
7315f4a2713aSLionel Sambuc if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
7316f4a2713aSLionel Sambuc return getIncompleteArrayType(ResultType,
7317f4a2713aSLionel Sambuc ArrayType::ArraySizeModifier(), 0);
7318f4a2713aSLionel Sambuc }
7319f4a2713aSLionel Sambuc case Type::FunctionNoProto:
7320f4a2713aSLionel Sambuc return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
7321f4a2713aSLionel Sambuc case Type::Record:
7322f4a2713aSLionel Sambuc case Type::Enum:
7323f4a2713aSLionel Sambuc return QualType();
7324f4a2713aSLionel Sambuc case Type::Builtin:
7325f4a2713aSLionel Sambuc // Only exactly equal builtin types are compatible, which is tested above.
7326f4a2713aSLionel Sambuc return QualType();
7327f4a2713aSLionel Sambuc case Type::Complex:
7328f4a2713aSLionel Sambuc // Distinct complex types are incompatible.
7329f4a2713aSLionel Sambuc return QualType();
7330f4a2713aSLionel Sambuc case Type::Vector:
7331f4a2713aSLionel Sambuc // FIXME: The merged type should be an ExtVector!
7332f4a2713aSLionel Sambuc if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
7333f4a2713aSLionel Sambuc RHSCan->getAs<VectorType>()))
7334f4a2713aSLionel Sambuc return LHS;
7335f4a2713aSLionel Sambuc return QualType();
7336f4a2713aSLionel Sambuc case Type::ObjCObject: {
7337f4a2713aSLionel Sambuc // Check if the types are assignment compatible.
7338f4a2713aSLionel Sambuc // FIXME: This should be type compatibility, e.g. whether
7339f4a2713aSLionel Sambuc // "LHS x; RHS x;" at global scope is legal.
7340f4a2713aSLionel Sambuc const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
7341f4a2713aSLionel Sambuc const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
7342f4a2713aSLionel Sambuc if (canAssignObjCInterfaces(LHSIface, RHSIface))
7343f4a2713aSLionel Sambuc return LHS;
7344f4a2713aSLionel Sambuc
7345f4a2713aSLionel Sambuc return QualType();
7346f4a2713aSLionel Sambuc }
7347f4a2713aSLionel Sambuc case Type::ObjCObjectPointer: {
7348f4a2713aSLionel Sambuc if (OfBlockPointer) {
7349f4a2713aSLionel Sambuc if (canAssignObjCInterfacesInBlockPointer(
7350f4a2713aSLionel Sambuc LHS->getAs<ObjCObjectPointerType>(),
7351f4a2713aSLionel Sambuc RHS->getAs<ObjCObjectPointerType>(),
7352f4a2713aSLionel Sambuc BlockReturnType))
7353f4a2713aSLionel Sambuc return LHS;
7354f4a2713aSLionel Sambuc return QualType();
7355f4a2713aSLionel Sambuc }
7356f4a2713aSLionel Sambuc if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
7357f4a2713aSLionel Sambuc RHS->getAs<ObjCObjectPointerType>()))
7358f4a2713aSLionel Sambuc return LHS;
7359f4a2713aSLionel Sambuc
7360f4a2713aSLionel Sambuc return QualType();
7361f4a2713aSLionel Sambuc }
7362f4a2713aSLionel Sambuc }
7363f4a2713aSLionel Sambuc
7364f4a2713aSLionel Sambuc llvm_unreachable("Invalid Type::Class!");
7365f4a2713aSLionel Sambuc }
7366f4a2713aSLionel Sambuc
FunctionTypesMatchOnNSConsumedAttrs(const FunctionProtoType * FromFunctionType,const FunctionProtoType * ToFunctionType)7367f4a2713aSLionel Sambuc bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs(
7368f4a2713aSLionel Sambuc const FunctionProtoType *FromFunctionType,
7369f4a2713aSLionel Sambuc const FunctionProtoType *ToFunctionType) {
7370*0a6a1f1dSLionel Sambuc if (FromFunctionType->hasAnyConsumedParams() !=
7371*0a6a1f1dSLionel Sambuc ToFunctionType->hasAnyConsumedParams())
7372f4a2713aSLionel Sambuc return false;
7373f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo FromEPI =
7374f4a2713aSLionel Sambuc FromFunctionType->getExtProtoInfo();
7375f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo ToEPI =
7376f4a2713aSLionel Sambuc ToFunctionType->getExtProtoInfo();
7377*0a6a1f1dSLionel Sambuc if (FromEPI.ConsumedParameters && ToEPI.ConsumedParameters)
7378*0a6a1f1dSLionel Sambuc for (unsigned i = 0, n = FromFunctionType->getNumParams(); i != n; ++i) {
7379*0a6a1f1dSLionel Sambuc if (FromEPI.ConsumedParameters[i] != ToEPI.ConsumedParameters[i])
7380f4a2713aSLionel Sambuc return false;
7381f4a2713aSLionel Sambuc }
7382f4a2713aSLionel Sambuc return true;
7383f4a2713aSLionel Sambuc }
7384f4a2713aSLionel Sambuc
7385f4a2713aSLionel Sambuc /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
7386f4a2713aSLionel Sambuc /// 'RHS' attributes and returns the merged version; including for function
7387f4a2713aSLionel Sambuc /// return types.
mergeObjCGCQualifiers(QualType LHS,QualType RHS)7388f4a2713aSLionel Sambuc QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
7389f4a2713aSLionel Sambuc QualType LHSCan = getCanonicalType(LHS),
7390f4a2713aSLionel Sambuc RHSCan = getCanonicalType(RHS);
7391f4a2713aSLionel Sambuc // If two types are identical, they are compatible.
7392f4a2713aSLionel Sambuc if (LHSCan == RHSCan)
7393f4a2713aSLionel Sambuc return LHS;
7394f4a2713aSLionel Sambuc if (RHSCan->isFunctionType()) {
7395f4a2713aSLionel Sambuc if (!LHSCan->isFunctionType())
7396f4a2713aSLionel Sambuc return QualType();
7397f4a2713aSLionel Sambuc QualType OldReturnType =
7398*0a6a1f1dSLionel Sambuc cast<FunctionType>(RHSCan.getTypePtr())->getReturnType();
7399f4a2713aSLionel Sambuc QualType NewReturnType =
7400*0a6a1f1dSLionel Sambuc cast<FunctionType>(LHSCan.getTypePtr())->getReturnType();
7401f4a2713aSLionel Sambuc QualType ResReturnType =
7402f4a2713aSLionel Sambuc mergeObjCGCQualifiers(NewReturnType, OldReturnType);
7403f4a2713aSLionel Sambuc if (ResReturnType.isNull())
7404f4a2713aSLionel Sambuc return QualType();
7405f4a2713aSLionel Sambuc if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
7406f4a2713aSLionel Sambuc // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
7407f4a2713aSLionel Sambuc // In either case, use OldReturnType to build the new function type.
7408f4a2713aSLionel Sambuc const FunctionType *F = LHS->getAs<FunctionType>();
7409f4a2713aSLionel Sambuc if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
7410f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
7411f4a2713aSLionel Sambuc EPI.ExtInfo = getFunctionExtInfo(LHS);
7412f4a2713aSLionel Sambuc QualType ResultType =
7413*0a6a1f1dSLionel Sambuc getFunctionType(OldReturnType, FPT->getParamTypes(), EPI);
7414f4a2713aSLionel Sambuc return ResultType;
7415f4a2713aSLionel Sambuc }
7416f4a2713aSLionel Sambuc }
7417f4a2713aSLionel Sambuc return QualType();
7418f4a2713aSLionel Sambuc }
7419f4a2713aSLionel Sambuc
7420f4a2713aSLionel Sambuc // If the qualifiers are different, the types can still be merged.
7421f4a2713aSLionel Sambuc Qualifiers LQuals = LHSCan.getLocalQualifiers();
7422f4a2713aSLionel Sambuc Qualifiers RQuals = RHSCan.getLocalQualifiers();
7423f4a2713aSLionel Sambuc if (LQuals != RQuals) {
7424f4a2713aSLionel Sambuc // If any of these qualifiers are different, we have a type mismatch.
7425f4a2713aSLionel Sambuc if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
7426f4a2713aSLionel Sambuc LQuals.getAddressSpace() != RQuals.getAddressSpace())
7427f4a2713aSLionel Sambuc return QualType();
7428f4a2713aSLionel Sambuc
7429f4a2713aSLionel Sambuc // Exactly one GC qualifier difference is allowed: __strong is
7430f4a2713aSLionel Sambuc // okay if the other type has no GC qualifier but is an Objective
7431f4a2713aSLionel Sambuc // C object pointer (i.e. implicitly strong by default). We fix
7432f4a2713aSLionel Sambuc // this by pretending that the unqualified type was actually
7433f4a2713aSLionel Sambuc // qualified __strong.
7434f4a2713aSLionel Sambuc Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
7435f4a2713aSLionel Sambuc Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
7436f4a2713aSLionel Sambuc assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
7437f4a2713aSLionel Sambuc
7438f4a2713aSLionel Sambuc if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
7439f4a2713aSLionel Sambuc return QualType();
7440f4a2713aSLionel Sambuc
7441f4a2713aSLionel Sambuc if (GC_L == Qualifiers::Strong)
7442f4a2713aSLionel Sambuc return LHS;
7443f4a2713aSLionel Sambuc if (GC_R == Qualifiers::Strong)
7444f4a2713aSLionel Sambuc return RHS;
7445f4a2713aSLionel Sambuc return QualType();
7446f4a2713aSLionel Sambuc }
7447f4a2713aSLionel Sambuc
7448f4a2713aSLionel Sambuc if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
7449f4a2713aSLionel Sambuc QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
7450f4a2713aSLionel Sambuc QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
7451f4a2713aSLionel Sambuc QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
7452f4a2713aSLionel Sambuc if (ResQT == LHSBaseQT)
7453f4a2713aSLionel Sambuc return LHS;
7454f4a2713aSLionel Sambuc if (ResQT == RHSBaseQT)
7455f4a2713aSLionel Sambuc return RHS;
7456f4a2713aSLionel Sambuc }
7457f4a2713aSLionel Sambuc return QualType();
7458f4a2713aSLionel Sambuc }
7459f4a2713aSLionel Sambuc
7460f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
7461f4a2713aSLionel Sambuc // Integer Predicates
7462f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
7463f4a2713aSLionel Sambuc
getIntWidth(QualType T) const7464f4a2713aSLionel Sambuc unsigned ASTContext::getIntWidth(QualType T) const {
7465f4a2713aSLionel Sambuc if (const EnumType *ET = T->getAs<EnumType>())
7466f4a2713aSLionel Sambuc T = ET->getDecl()->getIntegerType();
7467f4a2713aSLionel Sambuc if (T->isBooleanType())
7468f4a2713aSLionel Sambuc return 1;
7469f4a2713aSLionel Sambuc // For builtin types, just use the standard type sizing method
7470f4a2713aSLionel Sambuc return (unsigned)getTypeSize(T);
7471f4a2713aSLionel Sambuc }
7472f4a2713aSLionel Sambuc
getCorrespondingUnsignedType(QualType T) const7473f4a2713aSLionel Sambuc QualType ASTContext::getCorrespondingUnsignedType(QualType T) const {
7474f4a2713aSLionel Sambuc assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
7475f4a2713aSLionel Sambuc
7476f4a2713aSLionel Sambuc // Turn <4 x signed int> -> <4 x unsigned int>
7477f4a2713aSLionel Sambuc if (const VectorType *VTy = T->getAs<VectorType>())
7478f4a2713aSLionel Sambuc return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
7479f4a2713aSLionel Sambuc VTy->getNumElements(), VTy->getVectorKind());
7480f4a2713aSLionel Sambuc
7481f4a2713aSLionel Sambuc // For enums, we return the unsigned version of the base type.
7482f4a2713aSLionel Sambuc if (const EnumType *ETy = T->getAs<EnumType>())
7483f4a2713aSLionel Sambuc T = ETy->getDecl()->getIntegerType();
7484f4a2713aSLionel Sambuc
7485f4a2713aSLionel Sambuc const BuiltinType *BTy = T->getAs<BuiltinType>();
7486f4a2713aSLionel Sambuc assert(BTy && "Unexpected signed integer type");
7487f4a2713aSLionel Sambuc switch (BTy->getKind()) {
7488f4a2713aSLionel Sambuc case BuiltinType::Char_S:
7489f4a2713aSLionel Sambuc case BuiltinType::SChar:
7490f4a2713aSLionel Sambuc return UnsignedCharTy;
7491f4a2713aSLionel Sambuc case BuiltinType::Short:
7492f4a2713aSLionel Sambuc return UnsignedShortTy;
7493f4a2713aSLionel Sambuc case BuiltinType::Int:
7494f4a2713aSLionel Sambuc return UnsignedIntTy;
7495f4a2713aSLionel Sambuc case BuiltinType::Long:
7496f4a2713aSLionel Sambuc return UnsignedLongTy;
7497f4a2713aSLionel Sambuc case BuiltinType::LongLong:
7498f4a2713aSLionel Sambuc return UnsignedLongLongTy;
7499f4a2713aSLionel Sambuc case BuiltinType::Int128:
7500f4a2713aSLionel Sambuc return UnsignedInt128Ty;
7501f4a2713aSLionel Sambuc default:
7502f4a2713aSLionel Sambuc llvm_unreachable("Unexpected signed integer type");
7503f4a2713aSLionel Sambuc }
7504f4a2713aSLionel Sambuc }
7505f4a2713aSLionel Sambuc
~ASTMutationListener()7506f4a2713aSLionel Sambuc ASTMutationListener::~ASTMutationListener() { }
7507f4a2713aSLionel Sambuc
DeducedReturnType(const FunctionDecl * FD,QualType ReturnType)7508f4a2713aSLionel Sambuc void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD,
7509f4a2713aSLionel Sambuc QualType ReturnType) {}
7510f4a2713aSLionel Sambuc
7511f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
7512f4a2713aSLionel Sambuc // Builtin Type Computation
7513f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
7514f4a2713aSLionel Sambuc
7515f4a2713aSLionel Sambuc /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
7516f4a2713aSLionel Sambuc /// pointer over the consumed characters. This returns the resultant type. If
7517f4a2713aSLionel Sambuc /// AllowTypeModifiers is false then modifier like * are not parsed, just basic
7518f4a2713aSLionel Sambuc /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
7519f4a2713aSLionel Sambuc /// a vector of "i*".
7520f4a2713aSLionel Sambuc ///
7521f4a2713aSLionel Sambuc /// RequiresICE is filled in on return to indicate whether the value is required
7522f4a2713aSLionel Sambuc /// to be an Integer Constant Expression.
DecodeTypeFromStr(const char * & Str,const ASTContext & Context,ASTContext::GetBuiltinTypeError & Error,bool & RequiresICE,bool AllowTypeModifiers)7523f4a2713aSLionel Sambuc static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
7524f4a2713aSLionel Sambuc ASTContext::GetBuiltinTypeError &Error,
7525f4a2713aSLionel Sambuc bool &RequiresICE,
7526f4a2713aSLionel Sambuc bool AllowTypeModifiers) {
7527f4a2713aSLionel Sambuc // Modifiers.
7528f4a2713aSLionel Sambuc int HowLong = 0;
7529f4a2713aSLionel Sambuc bool Signed = false, Unsigned = false;
7530f4a2713aSLionel Sambuc RequiresICE = false;
7531f4a2713aSLionel Sambuc
7532f4a2713aSLionel Sambuc // Read the prefixed modifiers first.
7533f4a2713aSLionel Sambuc bool Done = false;
7534f4a2713aSLionel Sambuc while (!Done) {
7535f4a2713aSLionel Sambuc switch (*Str++) {
7536f4a2713aSLionel Sambuc default: Done = true; --Str; break;
7537f4a2713aSLionel Sambuc case 'I':
7538f4a2713aSLionel Sambuc RequiresICE = true;
7539f4a2713aSLionel Sambuc break;
7540f4a2713aSLionel Sambuc case 'S':
7541f4a2713aSLionel Sambuc assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
7542f4a2713aSLionel Sambuc assert(!Signed && "Can't use 'S' modifier multiple times!");
7543f4a2713aSLionel Sambuc Signed = true;
7544f4a2713aSLionel Sambuc break;
7545f4a2713aSLionel Sambuc case 'U':
7546f4a2713aSLionel Sambuc assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
7547f4a2713aSLionel Sambuc assert(!Unsigned && "Can't use 'S' modifier multiple times!");
7548f4a2713aSLionel Sambuc Unsigned = true;
7549f4a2713aSLionel Sambuc break;
7550f4a2713aSLionel Sambuc case 'L':
7551f4a2713aSLionel Sambuc assert(HowLong <= 2 && "Can't have LLLL modifier");
7552f4a2713aSLionel Sambuc ++HowLong;
7553f4a2713aSLionel Sambuc break;
7554*0a6a1f1dSLionel Sambuc case 'W':
7555*0a6a1f1dSLionel Sambuc // This modifier represents int64 type.
7556*0a6a1f1dSLionel Sambuc assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!");
7557*0a6a1f1dSLionel Sambuc switch (Context.getTargetInfo().getInt64Type()) {
7558*0a6a1f1dSLionel Sambuc default:
7559*0a6a1f1dSLionel Sambuc llvm_unreachable("Unexpected integer type");
7560*0a6a1f1dSLionel Sambuc case TargetInfo::SignedLong:
7561*0a6a1f1dSLionel Sambuc HowLong = 1;
7562*0a6a1f1dSLionel Sambuc break;
7563*0a6a1f1dSLionel Sambuc case TargetInfo::SignedLongLong:
7564*0a6a1f1dSLionel Sambuc HowLong = 2;
7565*0a6a1f1dSLionel Sambuc break;
7566*0a6a1f1dSLionel Sambuc }
7567f4a2713aSLionel Sambuc }
7568f4a2713aSLionel Sambuc }
7569f4a2713aSLionel Sambuc
7570f4a2713aSLionel Sambuc QualType Type;
7571f4a2713aSLionel Sambuc
7572f4a2713aSLionel Sambuc // Read the base type.
7573f4a2713aSLionel Sambuc switch (*Str++) {
7574f4a2713aSLionel Sambuc default: llvm_unreachable("Unknown builtin type letter!");
7575f4a2713aSLionel Sambuc case 'v':
7576f4a2713aSLionel Sambuc assert(HowLong == 0 && !Signed && !Unsigned &&
7577f4a2713aSLionel Sambuc "Bad modifiers used with 'v'!");
7578f4a2713aSLionel Sambuc Type = Context.VoidTy;
7579f4a2713aSLionel Sambuc break;
7580f4a2713aSLionel Sambuc case 'h':
7581f4a2713aSLionel Sambuc assert(HowLong == 0 && !Signed && !Unsigned &&
7582f4a2713aSLionel Sambuc "Bad modifiers used with 'f'!");
7583f4a2713aSLionel Sambuc Type = Context.HalfTy;
7584f4a2713aSLionel Sambuc break;
7585f4a2713aSLionel Sambuc case 'f':
7586f4a2713aSLionel Sambuc assert(HowLong == 0 && !Signed && !Unsigned &&
7587f4a2713aSLionel Sambuc "Bad modifiers used with 'f'!");
7588f4a2713aSLionel Sambuc Type = Context.FloatTy;
7589f4a2713aSLionel Sambuc break;
7590f4a2713aSLionel Sambuc case 'd':
7591f4a2713aSLionel Sambuc assert(HowLong < 2 && !Signed && !Unsigned &&
7592f4a2713aSLionel Sambuc "Bad modifiers used with 'd'!");
7593f4a2713aSLionel Sambuc if (HowLong)
7594f4a2713aSLionel Sambuc Type = Context.LongDoubleTy;
7595f4a2713aSLionel Sambuc else
7596f4a2713aSLionel Sambuc Type = Context.DoubleTy;
7597f4a2713aSLionel Sambuc break;
7598f4a2713aSLionel Sambuc case 's':
7599f4a2713aSLionel Sambuc assert(HowLong == 0 && "Bad modifiers used with 's'!");
7600f4a2713aSLionel Sambuc if (Unsigned)
7601f4a2713aSLionel Sambuc Type = Context.UnsignedShortTy;
7602f4a2713aSLionel Sambuc else
7603f4a2713aSLionel Sambuc Type = Context.ShortTy;
7604f4a2713aSLionel Sambuc break;
7605f4a2713aSLionel Sambuc case 'i':
7606f4a2713aSLionel Sambuc if (HowLong == 3)
7607f4a2713aSLionel Sambuc Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
7608f4a2713aSLionel Sambuc else if (HowLong == 2)
7609f4a2713aSLionel Sambuc Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
7610f4a2713aSLionel Sambuc else if (HowLong == 1)
7611f4a2713aSLionel Sambuc Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
7612f4a2713aSLionel Sambuc else
7613f4a2713aSLionel Sambuc Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
7614f4a2713aSLionel Sambuc break;
7615f4a2713aSLionel Sambuc case 'c':
7616f4a2713aSLionel Sambuc assert(HowLong == 0 && "Bad modifiers used with 'c'!");
7617f4a2713aSLionel Sambuc if (Signed)
7618f4a2713aSLionel Sambuc Type = Context.SignedCharTy;
7619f4a2713aSLionel Sambuc else if (Unsigned)
7620f4a2713aSLionel Sambuc Type = Context.UnsignedCharTy;
7621f4a2713aSLionel Sambuc else
7622f4a2713aSLionel Sambuc Type = Context.CharTy;
7623f4a2713aSLionel Sambuc break;
7624f4a2713aSLionel Sambuc case 'b': // boolean
7625f4a2713aSLionel Sambuc assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
7626f4a2713aSLionel Sambuc Type = Context.BoolTy;
7627f4a2713aSLionel Sambuc break;
7628f4a2713aSLionel Sambuc case 'z': // size_t.
7629f4a2713aSLionel Sambuc assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
7630f4a2713aSLionel Sambuc Type = Context.getSizeType();
7631f4a2713aSLionel Sambuc break;
7632f4a2713aSLionel Sambuc case 'F':
7633f4a2713aSLionel Sambuc Type = Context.getCFConstantStringType();
7634f4a2713aSLionel Sambuc break;
7635f4a2713aSLionel Sambuc case 'G':
7636f4a2713aSLionel Sambuc Type = Context.getObjCIdType();
7637f4a2713aSLionel Sambuc break;
7638f4a2713aSLionel Sambuc case 'H':
7639f4a2713aSLionel Sambuc Type = Context.getObjCSelType();
7640f4a2713aSLionel Sambuc break;
7641f4a2713aSLionel Sambuc case 'M':
7642f4a2713aSLionel Sambuc Type = Context.getObjCSuperType();
7643f4a2713aSLionel Sambuc break;
7644f4a2713aSLionel Sambuc case 'a':
7645f4a2713aSLionel Sambuc Type = Context.getBuiltinVaListType();
7646f4a2713aSLionel Sambuc assert(!Type.isNull() && "builtin va list type not initialized!");
7647f4a2713aSLionel Sambuc break;
7648f4a2713aSLionel Sambuc case 'A':
7649f4a2713aSLionel Sambuc // This is a "reference" to a va_list; however, what exactly
7650f4a2713aSLionel Sambuc // this means depends on how va_list is defined. There are two
7651f4a2713aSLionel Sambuc // different kinds of va_list: ones passed by value, and ones
7652f4a2713aSLionel Sambuc // passed by reference. An example of a by-value va_list is
7653f4a2713aSLionel Sambuc // x86, where va_list is a char*. An example of by-ref va_list
7654f4a2713aSLionel Sambuc // is x86-64, where va_list is a __va_list_tag[1]. For x86,
7655f4a2713aSLionel Sambuc // we want this argument to be a char*&; for x86-64, we want
7656f4a2713aSLionel Sambuc // it to be a __va_list_tag*.
7657f4a2713aSLionel Sambuc Type = Context.getBuiltinVaListType();
7658f4a2713aSLionel Sambuc assert(!Type.isNull() && "builtin va list type not initialized!");
7659f4a2713aSLionel Sambuc if (Type->isArrayType())
7660f4a2713aSLionel Sambuc Type = Context.getArrayDecayedType(Type);
7661f4a2713aSLionel Sambuc else
7662f4a2713aSLionel Sambuc Type = Context.getLValueReferenceType(Type);
7663f4a2713aSLionel Sambuc break;
7664f4a2713aSLionel Sambuc case 'V': {
7665f4a2713aSLionel Sambuc char *End;
7666f4a2713aSLionel Sambuc unsigned NumElements = strtoul(Str, &End, 10);
7667f4a2713aSLionel Sambuc assert(End != Str && "Missing vector size");
7668f4a2713aSLionel Sambuc Str = End;
7669f4a2713aSLionel Sambuc
7670f4a2713aSLionel Sambuc QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
7671f4a2713aSLionel Sambuc RequiresICE, false);
7672f4a2713aSLionel Sambuc assert(!RequiresICE && "Can't require vector ICE");
7673f4a2713aSLionel Sambuc
7674f4a2713aSLionel Sambuc // TODO: No way to make AltiVec vectors in builtins yet.
7675f4a2713aSLionel Sambuc Type = Context.getVectorType(ElementType, NumElements,
7676f4a2713aSLionel Sambuc VectorType::GenericVector);
7677f4a2713aSLionel Sambuc break;
7678f4a2713aSLionel Sambuc }
7679f4a2713aSLionel Sambuc case 'E': {
7680f4a2713aSLionel Sambuc char *End;
7681f4a2713aSLionel Sambuc
7682f4a2713aSLionel Sambuc unsigned NumElements = strtoul(Str, &End, 10);
7683f4a2713aSLionel Sambuc assert(End != Str && "Missing vector size");
7684f4a2713aSLionel Sambuc
7685f4a2713aSLionel Sambuc Str = End;
7686f4a2713aSLionel Sambuc
7687f4a2713aSLionel Sambuc QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
7688f4a2713aSLionel Sambuc false);
7689f4a2713aSLionel Sambuc Type = Context.getExtVectorType(ElementType, NumElements);
7690f4a2713aSLionel Sambuc break;
7691f4a2713aSLionel Sambuc }
7692f4a2713aSLionel Sambuc case 'X': {
7693f4a2713aSLionel Sambuc QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
7694f4a2713aSLionel Sambuc false);
7695f4a2713aSLionel Sambuc assert(!RequiresICE && "Can't require complex ICE");
7696f4a2713aSLionel Sambuc Type = Context.getComplexType(ElementType);
7697f4a2713aSLionel Sambuc break;
7698f4a2713aSLionel Sambuc }
7699f4a2713aSLionel Sambuc case 'Y' : {
7700f4a2713aSLionel Sambuc Type = Context.getPointerDiffType();
7701f4a2713aSLionel Sambuc break;
7702f4a2713aSLionel Sambuc }
7703f4a2713aSLionel Sambuc case 'P':
7704f4a2713aSLionel Sambuc Type = Context.getFILEType();
7705f4a2713aSLionel Sambuc if (Type.isNull()) {
7706f4a2713aSLionel Sambuc Error = ASTContext::GE_Missing_stdio;
7707f4a2713aSLionel Sambuc return QualType();
7708f4a2713aSLionel Sambuc }
7709f4a2713aSLionel Sambuc break;
7710f4a2713aSLionel Sambuc case 'J':
7711f4a2713aSLionel Sambuc if (Signed)
7712f4a2713aSLionel Sambuc Type = Context.getsigjmp_bufType();
7713f4a2713aSLionel Sambuc else
7714f4a2713aSLionel Sambuc Type = Context.getjmp_bufType();
7715f4a2713aSLionel Sambuc
7716f4a2713aSLionel Sambuc if (Type.isNull()) {
7717f4a2713aSLionel Sambuc Error = ASTContext::GE_Missing_setjmp;
7718f4a2713aSLionel Sambuc return QualType();
7719f4a2713aSLionel Sambuc }
7720f4a2713aSLionel Sambuc break;
7721f4a2713aSLionel Sambuc case 'K':
7722f4a2713aSLionel Sambuc assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!");
7723f4a2713aSLionel Sambuc Type = Context.getucontext_tType();
7724f4a2713aSLionel Sambuc
7725f4a2713aSLionel Sambuc if (Type.isNull()) {
7726f4a2713aSLionel Sambuc Error = ASTContext::GE_Missing_ucontext;
7727f4a2713aSLionel Sambuc return QualType();
7728f4a2713aSLionel Sambuc }
7729f4a2713aSLionel Sambuc break;
7730f4a2713aSLionel Sambuc case 'p':
7731f4a2713aSLionel Sambuc Type = Context.getProcessIDType();
7732f4a2713aSLionel Sambuc break;
7733f4a2713aSLionel Sambuc }
7734f4a2713aSLionel Sambuc
7735f4a2713aSLionel Sambuc // If there are modifiers and if we're allowed to parse them, go for it.
7736f4a2713aSLionel Sambuc Done = !AllowTypeModifiers;
7737f4a2713aSLionel Sambuc while (!Done) {
7738f4a2713aSLionel Sambuc switch (char c = *Str++) {
7739f4a2713aSLionel Sambuc default: Done = true; --Str; break;
7740f4a2713aSLionel Sambuc case '*':
7741f4a2713aSLionel Sambuc case '&': {
7742f4a2713aSLionel Sambuc // Both pointers and references can have their pointee types
7743f4a2713aSLionel Sambuc // qualified with an address space.
7744f4a2713aSLionel Sambuc char *End;
7745f4a2713aSLionel Sambuc unsigned AddrSpace = strtoul(Str, &End, 10);
7746f4a2713aSLionel Sambuc if (End != Str && AddrSpace != 0) {
7747f4a2713aSLionel Sambuc Type = Context.getAddrSpaceQualType(Type, AddrSpace);
7748f4a2713aSLionel Sambuc Str = End;
7749f4a2713aSLionel Sambuc }
7750f4a2713aSLionel Sambuc if (c == '*')
7751f4a2713aSLionel Sambuc Type = Context.getPointerType(Type);
7752f4a2713aSLionel Sambuc else
7753f4a2713aSLionel Sambuc Type = Context.getLValueReferenceType(Type);
7754f4a2713aSLionel Sambuc break;
7755f4a2713aSLionel Sambuc }
7756f4a2713aSLionel Sambuc // FIXME: There's no way to have a built-in with an rvalue ref arg.
7757f4a2713aSLionel Sambuc case 'C':
7758f4a2713aSLionel Sambuc Type = Type.withConst();
7759f4a2713aSLionel Sambuc break;
7760f4a2713aSLionel Sambuc case 'D':
7761f4a2713aSLionel Sambuc Type = Context.getVolatileType(Type);
7762f4a2713aSLionel Sambuc break;
7763f4a2713aSLionel Sambuc case 'R':
7764f4a2713aSLionel Sambuc Type = Type.withRestrict();
7765f4a2713aSLionel Sambuc break;
7766f4a2713aSLionel Sambuc }
7767f4a2713aSLionel Sambuc }
7768f4a2713aSLionel Sambuc
7769f4a2713aSLionel Sambuc assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
7770f4a2713aSLionel Sambuc "Integer constant 'I' type must be an integer");
7771f4a2713aSLionel Sambuc
7772f4a2713aSLionel Sambuc return Type;
7773f4a2713aSLionel Sambuc }
7774f4a2713aSLionel Sambuc
7775f4a2713aSLionel Sambuc /// GetBuiltinType - Return the type for the specified builtin.
GetBuiltinType(unsigned Id,GetBuiltinTypeError & Error,unsigned * IntegerConstantArgs) const7776f4a2713aSLionel Sambuc QualType ASTContext::GetBuiltinType(unsigned Id,
7777f4a2713aSLionel Sambuc GetBuiltinTypeError &Error,
7778f4a2713aSLionel Sambuc unsigned *IntegerConstantArgs) const {
7779f4a2713aSLionel Sambuc const char *TypeStr = BuiltinInfo.GetTypeString(Id);
7780f4a2713aSLionel Sambuc
7781f4a2713aSLionel Sambuc SmallVector<QualType, 8> ArgTypes;
7782f4a2713aSLionel Sambuc
7783f4a2713aSLionel Sambuc bool RequiresICE = false;
7784f4a2713aSLionel Sambuc Error = GE_None;
7785f4a2713aSLionel Sambuc QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
7786f4a2713aSLionel Sambuc RequiresICE, true);
7787f4a2713aSLionel Sambuc if (Error != GE_None)
7788f4a2713aSLionel Sambuc return QualType();
7789f4a2713aSLionel Sambuc
7790f4a2713aSLionel Sambuc assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
7791f4a2713aSLionel Sambuc
7792f4a2713aSLionel Sambuc while (TypeStr[0] && TypeStr[0] != '.') {
7793f4a2713aSLionel Sambuc QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
7794f4a2713aSLionel Sambuc if (Error != GE_None)
7795f4a2713aSLionel Sambuc return QualType();
7796f4a2713aSLionel Sambuc
7797f4a2713aSLionel Sambuc // If this argument is required to be an IntegerConstantExpression and the
7798f4a2713aSLionel Sambuc // caller cares, fill in the bitmask we return.
7799f4a2713aSLionel Sambuc if (RequiresICE && IntegerConstantArgs)
7800f4a2713aSLionel Sambuc *IntegerConstantArgs |= 1 << ArgTypes.size();
7801f4a2713aSLionel Sambuc
7802f4a2713aSLionel Sambuc // Do array -> pointer decay. The builtin should use the decayed type.
7803f4a2713aSLionel Sambuc if (Ty->isArrayType())
7804f4a2713aSLionel Sambuc Ty = getArrayDecayedType(Ty);
7805f4a2713aSLionel Sambuc
7806f4a2713aSLionel Sambuc ArgTypes.push_back(Ty);
7807f4a2713aSLionel Sambuc }
7808f4a2713aSLionel Sambuc
7809f4a2713aSLionel Sambuc assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
7810f4a2713aSLionel Sambuc "'.' should only occur at end of builtin type list!");
7811f4a2713aSLionel Sambuc
7812f4a2713aSLionel Sambuc FunctionType::ExtInfo EI(CC_C);
7813f4a2713aSLionel Sambuc if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
7814f4a2713aSLionel Sambuc
7815f4a2713aSLionel Sambuc bool Variadic = (TypeStr[0] == '.');
7816f4a2713aSLionel Sambuc
7817f4a2713aSLionel Sambuc // We really shouldn't be making a no-proto type here, especially in C++.
7818f4a2713aSLionel Sambuc if (ArgTypes.empty() && Variadic)
7819f4a2713aSLionel Sambuc return getFunctionNoProtoType(ResType, EI);
7820f4a2713aSLionel Sambuc
7821f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI;
7822f4a2713aSLionel Sambuc EPI.ExtInfo = EI;
7823f4a2713aSLionel Sambuc EPI.Variadic = Variadic;
7824f4a2713aSLionel Sambuc
7825f4a2713aSLionel Sambuc return getFunctionType(ResType, ArgTypes, EPI);
7826f4a2713aSLionel Sambuc }
7827f4a2713aSLionel Sambuc
basicGVALinkageForFunction(const ASTContext & Context,const FunctionDecl * FD)7828*0a6a1f1dSLionel Sambuc static GVALinkage basicGVALinkageForFunction(const ASTContext &Context,
7829*0a6a1f1dSLionel Sambuc const FunctionDecl *FD) {
7830f4a2713aSLionel Sambuc if (!FD->isExternallyVisible())
7831f4a2713aSLionel Sambuc return GVA_Internal;
7832f4a2713aSLionel Sambuc
7833f4a2713aSLionel Sambuc GVALinkage External = GVA_StrongExternal;
7834f4a2713aSLionel Sambuc switch (FD->getTemplateSpecializationKind()) {
7835f4a2713aSLionel Sambuc case TSK_Undeclared:
7836f4a2713aSLionel Sambuc case TSK_ExplicitSpecialization:
7837f4a2713aSLionel Sambuc External = GVA_StrongExternal;
7838f4a2713aSLionel Sambuc break;
7839f4a2713aSLionel Sambuc
7840f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDefinition:
7841*0a6a1f1dSLionel Sambuc return GVA_StrongODR;
7842f4a2713aSLionel Sambuc
7843*0a6a1f1dSLionel Sambuc // C++11 [temp.explicit]p10:
7844*0a6a1f1dSLionel Sambuc // [ Note: The intent is that an inline function that is the subject of
7845*0a6a1f1dSLionel Sambuc // an explicit instantiation declaration will still be implicitly
7846*0a6a1f1dSLionel Sambuc // instantiated when used so that the body can be considered for
7847*0a6a1f1dSLionel Sambuc // inlining, but that no out-of-line copy of the inline function would be
7848*0a6a1f1dSLionel Sambuc // generated in the translation unit. -- end note ]
7849f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDeclaration:
7850*0a6a1f1dSLionel Sambuc return GVA_AvailableExternally;
7851*0a6a1f1dSLionel Sambuc
7852f4a2713aSLionel Sambuc case TSK_ImplicitInstantiation:
7853*0a6a1f1dSLionel Sambuc External = GVA_DiscardableODR;
7854f4a2713aSLionel Sambuc break;
7855f4a2713aSLionel Sambuc }
7856f4a2713aSLionel Sambuc
7857f4a2713aSLionel Sambuc if (!FD->isInlined())
7858f4a2713aSLionel Sambuc return External;
7859f4a2713aSLionel Sambuc
7860*0a6a1f1dSLionel Sambuc if ((!Context.getLangOpts().CPlusPlus && !Context.getLangOpts().MSVCCompat &&
7861*0a6a1f1dSLionel Sambuc !FD->hasAttr<DLLExportAttr>()) ||
7862f4a2713aSLionel Sambuc FD->hasAttr<GNUInlineAttr>()) {
7863*0a6a1f1dSLionel Sambuc // FIXME: This doesn't match gcc's behavior for dllexport inline functions.
7864*0a6a1f1dSLionel Sambuc
7865f4a2713aSLionel Sambuc // GNU or C99 inline semantics. Determine whether this symbol should be
7866f4a2713aSLionel Sambuc // externally visible.
7867f4a2713aSLionel Sambuc if (FD->isInlineDefinitionExternallyVisible())
7868f4a2713aSLionel Sambuc return External;
7869f4a2713aSLionel Sambuc
7870f4a2713aSLionel Sambuc // C99 inline semantics, where the symbol is not externally visible.
7871*0a6a1f1dSLionel Sambuc return GVA_AvailableExternally;
7872f4a2713aSLionel Sambuc }
7873f4a2713aSLionel Sambuc
7874*0a6a1f1dSLionel Sambuc // Functions specified with extern and inline in -fms-compatibility mode
7875*0a6a1f1dSLionel Sambuc // forcibly get emitted. While the body of the function cannot be later
7876*0a6a1f1dSLionel Sambuc // replaced, the function definition cannot be discarded.
7877*0a6a1f1dSLionel Sambuc if (FD->isMSExternInline())
7878*0a6a1f1dSLionel Sambuc return GVA_StrongODR;
7879f4a2713aSLionel Sambuc
7880*0a6a1f1dSLionel Sambuc return GVA_DiscardableODR;
7881f4a2713aSLionel Sambuc }
7882f4a2713aSLionel Sambuc
adjustGVALinkageForDLLAttribute(GVALinkage L,const Decl * D)7883*0a6a1f1dSLionel Sambuc static GVALinkage adjustGVALinkageForDLLAttribute(GVALinkage L, const Decl *D) {
7884*0a6a1f1dSLionel Sambuc // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx
7885*0a6a1f1dSLionel Sambuc // dllexport/dllimport on inline functions.
7886*0a6a1f1dSLionel Sambuc if (D->hasAttr<DLLImportAttr>()) {
7887*0a6a1f1dSLionel Sambuc if (L == GVA_DiscardableODR || L == GVA_StrongODR)
7888*0a6a1f1dSLionel Sambuc return GVA_AvailableExternally;
7889*0a6a1f1dSLionel Sambuc } else if (D->hasAttr<DLLExportAttr>()) {
7890*0a6a1f1dSLionel Sambuc if (L == GVA_DiscardableODR)
7891*0a6a1f1dSLionel Sambuc return GVA_StrongODR;
7892*0a6a1f1dSLionel Sambuc }
7893*0a6a1f1dSLionel Sambuc return L;
7894*0a6a1f1dSLionel Sambuc }
7895*0a6a1f1dSLionel Sambuc
GetGVALinkageForFunction(const FunctionDecl * FD) const7896*0a6a1f1dSLionel Sambuc GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const {
7897*0a6a1f1dSLionel Sambuc return adjustGVALinkageForDLLAttribute(basicGVALinkageForFunction(*this, FD),
7898*0a6a1f1dSLionel Sambuc FD);
7899*0a6a1f1dSLionel Sambuc }
7900*0a6a1f1dSLionel Sambuc
basicGVALinkageForVariable(const ASTContext & Context,const VarDecl * VD)7901*0a6a1f1dSLionel Sambuc static GVALinkage basicGVALinkageForVariable(const ASTContext &Context,
7902*0a6a1f1dSLionel Sambuc const VarDecl *VD) {
7903f4a2713aSLionel Sambuc if (!VD->isExternallyVisible())
7904f4a2713aSLionel Sambuc return GVA_Internal;
7905f4a2713aSLionel Sambuc
7906*0a6a1f1dSLionel Sambuc if (VD->isStaticLocal()) {
7907*0a6a1f1dSLionel Sambuc GVALinkage StaticLocalLinkage = GVA_DiscardableODR;
7908*0a6a1f1dSLionel Sambuc const DeclContext *LexicalContext = VD->getParentFunctionOrMethod();
7909*0a6a1f1dSLionel Sambuc while (LexicalContext && !isa<FunctionDecl>(LexicalContext))
7910*0a6a1f1dSLionel Sambuc LexicalContext = LexicalContext->getLexicalParent();
7911*0a6a1f1dSLionel Sambuc
7912*0a6a1f1dSLionel Sambuc // Let the static local variable inherit it's linkage from the nearest
7913*0a6a1f1dSLionel Sambuc // enclosing function.
7914*0a6a1f1dSLionel Sambuc if (LexicalContext)
7915*0a6a1f1dSLionel Sambuc StaticLocalLinkage =
7916*0a6a1f1dSLionel Sambuc Context.GetGVALinkageForFunction(cast<FunctionDecl>(LexicalContext));
7917*0a6a1f1dSLionel Sambuc
7918*0a6a1f1dSLionel Sambuc // GVA_StrongODR function linkage is stronger than what we need,
7919*0a6a1f1dSLionel Sambuc // downgrade to GVA_DiscardableODR.
7920*0a6a1f1dSLionel Sambuc // This allows us to discard the variable if we never end up needing it.
7921*0a6a1f1dSLionel Sambuc return StaticLocalLinkage == GVA_StrongODR ? GVA_DiscardableODR
7922*0a6a1f1dSLionel Sambuc : StaticLocalLinkage;
7923*0a6a1f1dSLionel Sambuc }
7924*0a6a1f1dSLionel Sambuc
7925*0a6a1f1dSLionel Sambuc // MSVC treats in-class initialized static data members as definitions.
7926*0a6a1f1dSLionel Sambuc // By giving them non-strong linkage, out-of-line definitions won't
7927*0a6a1f1dSLionel Sambuc // cause link errors.
7928*0a6a1f1dSLionel Sambuc if (Context.isMSStaticDataMemberInlineDefinition(VD))
7929*0a6a1f1dSLionel Sambuc return GVA_DiscardableODR;
7930*0a6a1f1dSLionel Sambuc
7931f4a2713aSLionel Sambuc switch (VD->getTemplateSpecializationKind()) {
7932f4a2713aSLionel Sambuc case TSK_Undeclared:
7933f4a2713aSLionel Sambuc case TSK_ExplicitSpecialization:
7934f4a2713aSLionel Sambuc return GVA_StrongExternal;
7935f4a2713aSLionel Sambuc
7936f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDefinition:
7937*0a6a1f1dSLionel Sambuc return GVA_StrongODR;
7938*0a6a1f1dSLionel Sambuc
7939*0a6a1f1dSLionel Sambuc case TSK_ExplicitInstantiationDeclaration:
7940*0a6a1f1dSLionel Sambuc return GVA_AvailableExternally;
7941f4a2713aSLionel Sambuc
7942f4a2713aSLionel Sambuc case TSK_ImplicitInstantiation:
7943*0a6a1f1dSLionel Sambuc return GVA_DiscardableODR;
7944f4a2713aSLionel Sambuc }
7945f4a2713aSLionel Sambuc
7946f4a2713aSLionel Sambuc llvm_unreachable("Invalid Linkage!");
7947f4a2713aSLionel Sambuc }
7948f4a2713aSLionel Sambuc
GetGVALinkageForVariable(const VarDecl * VD)7949*0a6a1f1dSLionel Sambuc GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
7950*0a6a1f1dSLionel Sambuc return adjustGVALinkageForDLLAttribute(basicGVALinkageForVariable(*this, VD),
7951*0a6a1f1dSLionel Sambuc VD);
7952*0a6a1f1dSLionel Sambuc }
7953*0a6a1f1dSLionel Sambuc
DeclMustBeEmitted(const Decl * D)7954f4a2713aSLionel Sambuc bool ASTContext::DeclMustBeEmitted(const Decl *D) {
7955f4a2713aSLionel Sambuc if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
7956f4a2713aSLionel Sambuc if (!VD->isFileVarDecl())
7957f4a2713aSLionel Sambuc return false;
7958*0a6a1f1dSLionel Sambuc // Global named register variables (GNU extension) are never emitted.
7959*0a6a1f1dSLionel Sambuc if (VD->getStorageClass() == SC_Register)
7960*0a6a1f1dSLionel Sambuc return false;
7961f4a2713aSLionel Sambuc } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7962f4a2713aSLionel Sambuc // We never need to emit an uninstantiated function template.
7963f4a2713aSLionel Sambuc if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
7964f4a2713aSLionel Sambuc return false;
7965*0a6a1f1dSLionel Sambuc } else if (isa<OMPThreadPrivateDecl>(D))
7966*0a6a1f1dSLionel Sambuc return true;
7967*0a6a1f1dSLionel Sambuc else
7968f4a2713aSLionel Sambuc return false;
7969f4a2713aSLionel Sambuc
7970f4a2713aSLionel Sambuc // If this is a member of a class template, we do not need to emit it.
7971f4a2713aSLionel Sambuc if (D->getDeclContext()->isDependentContext())
7972f4a2713aSLionel Sambuc return false;
7973f4a2713aSLionel Sambuc
7974f4a2713aSLionel Sambuc // Weak references don't produce any output by themselves.
7975f4a2713aSLionel Sambuc if (D->hasAttr<WeakRefAttr>())
7976f4a2713aSLionel Sambuc return false;
7977f4a2713aSLionel Sambuc
7978f4a2713aSLionel Sambuc // Aliases and used decls are required.
7979f4a2713aSLionel Sambuc if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
7980f4a2713aSLionel Sambuc return true;
7981f4a2713aSLionel Sambuc
7982f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7983f4a2713aSLionel Sambuc // Forward declarations aren't required.
7984f4a2713aSLionel Sambuc if (!FD->doesThisDeclarationHaveABody())
7985f4a2713aSLionel Sambuc return FD->doesDeclarationForceExternallyVisibleDefinition();
7986f4a2713aSLionel Sambuc
7987f4a2713aSLionel Sambuc // Constructors and destructors are required.
7988f4a2713aSLionel Sambuc if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
7989f4a2713aSLionel Sambuc return true;
7990f4a2713aSLionel Sambuc
7991f4a2713aSLionel Sambuc // The key function for a class is required. This rule only comes
7992f4a2713aSLionel Sambuc // into play when inline functions can be key functions, though.
7993f4a2713aSLionel Sambuc if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) {
7994f4a2713aSLionel Sambuc if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
7995f4a2713aSLionel Sambuc const CXXRecordDecl *RD = MD->getParent();
7996f4a2713aSLionel Sambuc if (MD->isOutOfLine() && RD->isDynamicClass()) {
7997f4a2713aSLionel Sambuc const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD);
7998f4a2713aSLionel Sambuc if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
7999f4a2713aSLionel Sambuc return true;
8000f4a2713aSLionel Sambuc }
8001f4a2713aSLionel Sambuc }
8002f4a2713aSLionel Sambuc }
8003f4a2713aSLionel Sambuc
8004f4a2713aSLionel Sambuc GVALinkage Linkage = GetGVALinkageForFunction(FD);
8005f4a2713aSLionel Sambuc
8006f4a2713aSLionel Sambuc // static, static inline, always_inline, and extern inline functions can
8007f4a2713aSLionel Sambuc // always be deferred. Normal inline functions can be deferred in C99/C++.
8008f4a2713aSLionel Sambuc // Implicit template instantiations can also be deferred in C++.
8009*0a6a1f1dSLionel Sambuc if (Linkage == GVA_Internal || Linkage == GVA_AvailableExternally ||
8010*0a6a1f1dSLionel Sambuc Linkage == GVA_DiscardableODR)
8011f4a2713aSLionel Sambuc return false;
8012f4a2713aSLionel Sambuc return true;
8013f4a2713aSLionel Sambuc }
8014f4a2713aSLionel Sambuc
8015f4a2713aSLionel Sambuc const VarDecl *VD = cast<VarDecl>(D);
8016f4a2713aSLionel Sambuc assert(VD->isFileVarDecl() && "Expected file scoped var");
8017f4a2713aSLionel Sambuc
8018*0a6a1f1dSLionel Sambuc if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly &&
8019*0a6a1f1dSLionel Sambuc !isMSStaticDataMemberInlineDefinition(VD))
8020f4a2713aSLionel Sambuc return false;
8021f4a2713aSLionel Sambuc
8022f4a2713aSLionel Sambuc // Variables that can be needed in other TUs are required.
8023f4a2713aSLionel Sambuc GVALinkage L = GetGVALinkageForVariable(VD);
8024*0a6a1f1dSLionel Sambuc if (L != GVA_Internal && L != GVA_AvailableExternally &&
8025*0a6a1f1dSLionel Sambuc L != GVA_DiscardableODR)
8026f4a2713aSLionel Sambuc return true;
8027f4a2713aSLionel Sambuc
8028f4a2713aSLionel Sambuc // Variables that have destruction with side-effects are required.
8029f4a2713aSLionel Sambuc if (VD->getType().isDestructedType())
8030f4a2713aSLionel Sambuc return true;
8031f4a2713aSLionel Sambuc
8032f4a2713aSLionel Sambuc // Variables that have initialization with side-effects are required.
8033f4a2713aSLionel Sambuc if (VD->getInit() && VD->getInit()->HasSideEffects(*this))
8034f4a2713aSLionel Sambuc return true;
8035f4a2713aSLionel Sambuc
8036f4a2713aSLionel Sambuc return false;
8037f4a2713aSLionel Sambuc }
8038f4a2713aSLionel Sambuc
getDefaultCallingConvention(bool IsVariadic,bool IsCXXMethod) const8039f4a2713aSLionel Sambuc CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic,
8040f4a2713aSLionel Sambuc bool IsCXXMethod) const {
8041f4a2713aSLionel Sambuc // Pass through to the C++ ABI object
8042f4a2713aSLionel Sambuc if (IsCXXMethod)
8043f4a2713aSLionel Sambuc return ABI->getDefaultMethodCallConv(IsVariadic);
8044f4a2713aSLionel Sambuc
8045f4a2713aSLionel Sambuc return (LangOpts.MRTD && !IsVariadic) ? CC_X86StdCall : CC_C;
8046f4a2713aSLionel Sambuc }
8047f4a2713aSLionel Sambuc
isNearlyEmpty(const CXXRecordDecl * RD) const8048f4a2713aSLionel Sambuc bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
8049f4a2713aSLionel Sambuc // Pass through to the C++ ABI object
8050f4a2713aSLionel Sambuc return ABI->isNearlyEmpty(RD);
8051f4a2713aSLionel Sambuc }
8052f4a2713aSLionel Sambuc
getVTableContext()8053*0a6a1f1dSLionel Sambuc VTableContextBase *ASTContext::getVTableContext() {
8054*0a6a1f1dSLionel Sambuc if (!VTContext.get()) {
8055*0a6a1f1dSLionel Sambuc if (Target->getCXXABI().isMicrosoft())
8056*0a6a1f1dSLionel Sambuc VTContext.reset(new MicrosoftVTableContext(*this));
8057*0a6a1f1dSLionel Sambuc else
8058*0a6a1f1dSLionel Sambuc VTContext.reset(new ItaniumVTableContext(*this));
8059*0a6a1f1dSLionel Sambuc }
8060*0a6a1f1dSLionel Sambuc return VTContext.get();
8061*0a6a1f1dSLionel Sambuc }
8062*0a6a1f1dSLionel Sambuc
createMangleContext()8063f4a2713aSLionel Sambuc MangleContext *ASTContext::createMangleContext() {
8064f4a2713aSLionel Sambuc switch (Target->getCXXABI().getKind()) {
8065f4a2713aSLionel Sambuc case TargetCXXABI::GenericAArch64:
8066f4a2713aSLionel Sambuc case TargetCXXABI::GenericItanium:
8067f4a2713aSLionel Sambuc case TargetCXXABI::GenericARM:
8068*0a6a1f1dSLionel Sambuc case TargetCXXABI::GenericMIPS:
8069f4a2713aSLionel Sambuc case TargetCXXABI::iOS:
8070*0a6a1f1dSLionel Sambuc case TargetCXXABI::iOS64:
8071f4a2713aSLionel Sambuc return ItaniumMangleContext::create(*this, getDiagnostics());
8072f4a2713aSLionel Sambuc case TargetCXXABI::Microsoft:
8073f4a2713aSLionel Sambuc return MicrosoftMangleContext::create(*this, getDiagnostics());
8074f4a2713aSLionel Sambuc }
8075f4a2713aSLionel Sambuc llvm_unreachable("Unsupported ABI");
8076f4a2713aSLionel Sambuc }
8077f4a2713aSLionel Sambuc
~CXXABI()8078f4a2713aSLionel Sambuc CXXABI::~CXXABI() {}
8079f4a2713aSLionel Sambuc
getSideTableAllocatedMemory() const8080f4a2713aSLionel Sambuc size_t ASTContext::getSideTableAllocatedMemory() const {
8081f4a2713aSLionel Sambuc return ASTRecordLayouts.getMemorySize() +
8082f4a2713aSLionel Sambuc llvm::capacity_in_bytes(ObjCLayouts) +
8083f4a2713aSLionel Sambuc llvm::capacity_in_bytes(KeyFunctions) +
8084f4a2713aSLionel Sambuc llvm::capacity_in_bytes(ObjCImpls) +
8085f4a2713aSLionel Sambuc llvm::capacity_in_bytes(BlockVarCopyInits) +
8086f4a2713aSLionel Sambuc llvm::capacity_in_bytes(DeclAttrs) +
8087f4a2713aSLionel Sambuc llvm::capacity_in_bytes(TemplateOrInstantiation) +
8088f4a2713aSLionel Sambuc llvm::capacity_in_bytes(InstantiatedFromUsingDecl) +
8089f4a2713aSLionel Sambuc llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) +
8090f4a2713aSLionel Sambuc llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) +
8091f4a2713aSLionel Sambuc llvm::capacity_in_bytes(OverriddenMethods) +
8092f4a2713aSLionel Sambuc llvm::capacity_in_bytes(Types) +
8093f4a2713aSLionel Sambuc llvm::capacity_in_bytes(VariableArrayTypes) +
8094f4a2713aSLionel Sambuc llvm::capacity_in_bytes(ClassScopeSpecializationPattern);
8095f4a2713aSLionel Sambuc }
8096f4a2713aSLionel Sambuc
8097f4a2713aSLionel Sambuc /// getIntTypeForBitwidth -
8098f4a2713aSLionel Sambuc /// sets integer QualTy according to specified details:
8099f4a2713aSLionel Sambuc /// bitwidth, signed/unsigned.
8100f4a2713aSLionel Sambuc /// Returns empty type if there is no appropriate target types.
getIntTypeForBitwidth(unsigned DestWidth,unsigned Signed) const8101f4a2713aSLionel Sambuc QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth,
8102f4a2713aSLionel Sambuc unsigned Signed) const {
8103f4a2713aSLionel Sambuc TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(DestWidth, Signed);
8104f4a2713aSLionel Sambuc CanQualType QualTy = getFromTargetType(Ty);
8105f4a2713aSLionel Sambuc if (!QualTy && DestWidth == 128)
8106f4a2713aSLionel Sambuc return Signed ? Int128Ty : UnsignedInt128Ty;
8107f4a2713aSLionel Sambuc return QualTy;
8108f4a2713aSLionel Sambuc }
8109f4a2713aSLionel Sambuc
8110f4a2713aSLionel Sambuc /// getRealTypeForBitwidth -
8111f4a2713aSLionel Sambuc /// sets floating point QualTy according to specified bitwidth.
8112f4a2713aSLionel Sambuc /// Returns empty type if there is no appropriate target types.
getRealTypeForBitwidth(unsigned DestWidth) const8113f4a2713aSLionel Sambuc QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth) const {
8114f4a2713aSLionel Sambuc TargetInfo::RealType Ty = getTargetInfo().getRealTypeByWidth(DestWidth);
8115f4a2713aSLionel Sambuc switch (Ty) {
8116f4a2713aSLionel Sambuc case TargetInfo::Float:
8117f4a2713aSLionel Sambuc return FloatTy;
8118f4a2713aSLionel Sambuc case TargetInfo::Double:
8119f4a2713aSLionel Sambuc return DoubleTy;
8120f4a2713aSLionel Sambuc case TargetInfo::LongDouble:
8121f4a2713aSLionel Sambuc return LongDoubleTy;
8122f4a2713aSLionel Sambuc case TargetInfo::NoFloat:
8123f4a2713aSLionel Sambuc return QualType();
8124f4a2713aSLionel Sambuc }
8125f4a2713aSLionel Sambuc
8126f4a2713aSLionel Sambuc llvm_unreachable("Unhandled TargetInfo::RealType value");
8127f4a2713aSLionel Sambuc }
8128f4a2713aSLionel Sambuc
setManglingNumber(const NamedDecl * ND,unsigned Number)8129f4a2713aSLionel Sambuc void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) {
8130f4a2713aSLionel Sambuc if (Number > 1)
8131f4a2713aSLionel Sambuc MangleNumbers[ND] = Number;
8132f4a2713aSLionel Sambuc }
8133f4a2713aSLionel Sambuc
getManglingNumber(const NamedDecl * ND) const8134f4a2713aSLionel Sambuc unsigned ASTContext::getManglingNumber(const NamedDecl *ND) const {
8135f4a2713aSLionel Sambuc llvm::DenseMap<const NamedDecl *, unsigned>::const_iterator I =
8136f4a2713aSLionel Sambuc MangleNumbers.find(ND);
8137f4a2713aSLionel Sambuc return I != MangleNumbers.end() ? I->second : 1;
8138f4a2713aSLionel Sambuc }
8139f4a2713aSLionel Sambuc
setStaticLocalNumber(const VarDecl * VD,unsigned Number)8140*0a6a1f1dSLionel Sambuc void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) {
8141*0a6a1f1dSLionel Sambuc if (Number > 1)
8142*0a6a1f1dSLionel Sambuc StaticLocalNumbers[VD] = Number;
8143*0a6a1f1dSLionel Sambuc }
8144*0a6a1f1dSLionel Sambuc
getStaticLocalNumber(const VarDecl * VD) const8145*0a6a1f1dSLionel Sambuc unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const {
8146*0a6a1f1dSLionel Sambuc llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I =
8147*0a6a1f1dSLionel Sambuc StaticLocalNumbers.find(VD);
8148*0a6a1f1dSLionel Sambuc return I != StaticLocalNumbers.end() ? I->second : 1;
8149*0a6a1f1dSLionel Sambuc }
8150*0a6a1f1dSLionel Sambuc
8151f4a2713aSLionel Sambuc MangleNumberingContext &
getManglingNumberContext(const DeclContext * DC)8152f4a2713aSLionel Sambuc ASTContext::getManglingNumberContext(const DeclContext *DC) {
8153f4a2713aSLionel Sambuc assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C.
8154f4a2713aSLionel Sambuc MangleNumberingContext *&MCtx = MangleNumberingContexts[DC];
8155f4a2713aSLionel Sambuc if (!MCtx)
8156f4a2713aSLionel Sambuc MCtx = createMangleNumberingContext();
8157f4a2713aSLionel Sambuc return *MCtx;
8158f4a2713aSLionel Sambuc }
8159f4a2713aSLionel Sambuc
createMangleNumberingContext() const8160f4a2713aSLionel Sambuc MangleNumberingContext *ASTContext::createMangleNumberingContext() const {
8161f4a2713aSLionel Sambuc return ABI->createMangleNumberingContext();
8162f4a2713aSLionel Sambuc }
8163f4a2713aSLionel Sambuc
setParameterIndex(const ParmVarDecl * D,unsigned int index)8164f4a2713aSLionel Sambuc void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) {
8165f4a2713aSLionel Sambuc ParamIndices[D] = index;
8166f4a2713aSLionel Sambuc }
8167f4a2713aSLionel Sambuc
getParameterIndex(const ParmVarDecl * D) const8168f4a2713aSLionel Sambuc unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const {
8169f4a2713aSLionel Sambuc ParameterIndexTable::const_iterator I = ParamIndices.find(D);
8170f4a2713aSLionel Sambuc assert(I != ParamIndices.end() &&
8171f4a2713aSLionel Sambuc "ParmIndices lacks entry set by ParmVarDecl");
8172f4a2713aSLionel Sambuc return I->second;
8173f4a2713aSLionel Sambuc }
8174f4a2713aSLionel Sambuc
8175f4a2713aSLionel Sambuc APValue *
getMaterializedTemporaryValue(const MaterializeTemporaryExpr * E,bool MayCreate)8176f4a2713aSLionel Sambuc ASTContext::getMaterializedTemporaryValue(const MaterializeTemporaryExpr *E,
8177f4a2713aSLionel Sambuc bool MayCreate) {
8178f4a2713aSLionel Sambuc assert(E && E->getStorageDuration() == SD_Static &&
8179f4a2713aSLionel Sambuc "don't need to cache the computed value for this temporary");
8180f4a2713aSLionel Sambuc if (MayCreate)
8181f4a2713aSLionel Sambuc return &MaterializedTemporaryValues[E];
8182f4a2713aSLionel Sambuc
8183f4a2713aSLionel Sambuc llvm::DenseMap<const MaterializeTemporaryExpr *, APValue>::iterator I =
8184f4a2713aSLionel Sambuc MaterializedTemporaryValues.find(E);
8185*0a6a1f1dSLionel Sambuc return I == MaterializedTemporaryValues.end() ? nullptr : &I->second;
8186f4a2713aSLionel Sambuc }
8187f4a2713aSLionel Sambuc
AtomicUsesUnsupportedLibcall(const AtomicExpr * E) const8188f4a2713aSLionel Sambuc bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const {
8189f4a2713aSLionel Sambuc const llvm::Triple &T = getTargetInfo().getTriple();
8190f4a2713aSLionel Sambuc if (!T.isOSDarwin())
8191f4a2713aSLionel Sambuc return false;
8192f4a2713aSLionel Sambuc
8193f4a2713aSLionel Sambuc if (!(T.isiOS() && T.isOSVersionLT(7)) &&
8194f4a2713aSLionel Sambuc !(T.isMacOSX() && T.isOSVersionLT(10, 9)))
8195f4a2713aSLionel Sambuc return false;
8196f4a2713aSLionel Sambuc
8197f4a2713aSLionel Sambuc QualType AtomicTy = E->getPtr()->getType()->getPointeeType();
8198f4a2713aSLionel Sambuc CharUnits sizeChars = getTypeSizeInChars(AtomicTy);
8199f4a2713aSLionel Sambuc uint64_t Size = sizeChars.getQuantity();
8200f4a2713aSLionel Sambuc CharUnits alignChars = getTypeAlignInChars(AtomicTy);
8201f4a2713aSLionel Sambuc unsigned Align = alignChars.getQuantity();
8202f4a2713aSLionel Sambuc unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth();
8203f4a2713aSLionel Sambuc return (Size != Align || toBits(sizeChars) > MaxInlineWidthInBits);
8204f4a2713aSLionel Sambuc }
8205f4a2713aSLionel Sambuc
8206f4a2713aSLionel Sambuc namespace {
8207f4a2713aSLionel Sambuc
8208f4a2713aSLionel Sambuc /// \brief A \c RecursiveASTVisitor that builds a map from nodes to their
8209f4a2713aSLionel Sambuc /// parents as defined by the \c RecursiveASTVisitor.
8210f4a2713aSLionel Sambuc ///
8211f4a2713aSLionel Sambuc /// Note that the relationship described here is purely in terms of AST
8212f4a2713aSLionel Sambuc /// traversal - there are other relationships (for example declaration context)
8213f4a2713aSLionel Sambuc /// in the AST that are better modeled by special matchers.
8214f4a2713aSLionel Sambuc ///
8215f4a2713aSLionel Sambuc /// FIXME: Currently only builds up the map using \c Stmt and \c Decl nodes.
8216f4a2713aSLionel Sambuc class ParentMapASTVisitor : public RecursiveASTVisitor<ParentMapASTVisitor> {
8217f4a2713aSLionel Sambuc
8218f4a2713aSLionel Sambuc public:
8219f4a2713aSLionel Sambuc /// \brief Builds and returns the translation unit's parent map.
8220f4a2713aSLionel Sambuc ///
8221f4a2713aSLionel Sambuc /// The caller takes ownership of the returned \c ParentMap.
buildMap(TranslationUnitDecl & TU)8222f4a2713aSLionel Sambuc static ASTContext::ParentMap *buildMap(TranslationUnitDecl &TU) {
8223f4a2713aSLionel Sambuc ParentMapASTVisitor Visitor(new ASTContext::ParentMap);
8224f4a2713aSLionel Sambuc Visitor.TraverseDecl(&TU);
8225f4a2713aSLionel Sambuc return Visitor.Parents;
8226f4a2713aSLionel Sambuc }
8227f4a2713aSLionel Sambuc
8228f4a2713aSLionel Sambuc private:
8229f4a2713aSLionel Sambuc typedef RecursiveASTVisitor<ParentMapASTVisitor> VisitorBase;
8230f4a2713aSLionel Sambuc
ParentMapASTVisitor(ASTContext::ParentMap * Parents)8231f4a2713aSLionel Sambuc ParentMapASTVisitor(ASTContext::ParentMap *Parents) : Parents(Parents) {
8232f4a2713aSLionel Sambuc }
8233f4a2713aSLionel Sambuc
shouldVisitTemplateInstantiations() const8234f4a2713aSLionel Sambuc bool shouldVisitTemplateInstantiations() const {
8235f4a2713aSLionel Sambuc return true;
8236f4a2713aSLionel Sambuc }
shouldVisitImplicitCode() const8237f4a2713aSLionel Sambuc bool shouldVisitImplicitCode() const {
8238f4a2713aSLionel Sambuc return true;
8239f4a2713aSLionel Sambuc }
8240f4a2713aSLionel Sambuc // Disables data recursion. We intercept Traverse* methods in the RAV, which
8241f4a2713aSLionel Sambuc // are not triggered during data recursion.
shouldUseDataRecursionFor(clang::Stmt * S) const8242f4a2713aSLionel Sambuc bool shouldUseDataRecursionFor(clang::Stmt *S) const {
8243f4a2713aSLionel Sambuc return false;
8244f4a2713aSLionel Sambuc }
8245f4a2713aSLionel Sambuc
8246f4a2713aSLionel Sambuc template <typename T>
TraverseNode(T * Node,bool (VisitorBase::* traverse)(T *))8247f4a2713aSLionel Sambuc bool TraverseNode(T *Node, bool(VisitorBase:: *traverse) (T *)) {
8248*0a6a1f1dSLionel Sambuc if (!Node)
8249f4a2713aSLionel Sambuc return true;
8250*0a6a1f1dSLionel Sambuc if (ParentStack.size() > 0) {
8251*0a6a1f1dSLionel Sambuc // FIXME: Currently we add the same parent multiple times, but only
8252*0a6a1f1dSLionel Sambuc // when no memoization data is available for the type.
8253*0a6a1f1dSLionel Sambuc // For example when we visit all subexpressions of template
8254*0a6a1f1dSLionel Sambuc // instantiations; this is suboptimal, but benign: the only way to
8255*0a6a1f1dSLionel Sambuc // visit those is with hasAncestor / hasParent, and those do not create
8256*0a6a1f1dSLionel Sambuc // new matches.
8257f4a2713aSLionel Sambuc // The plan is to enable DynTypedNode to be storable in a map or hash
8258f4a2713aSLionel Sambuc // map. The main problem there is to implement hash functions /
8259f4a2713aSLionel Sambuc // comparison operators for all types that DynTypedNode supports that
8260f4a2713aSLionel Sambuc // do not have pointer identity.
8261*0a6a1f1dSLionel Sambuc auto &NodeOrVector = (*Parents)[Node];
8262*0a6a1f1dSLionel Sambuc if (NodeOrVector.isNull()) {
8263*0a6a1f1dSLionel Sambuc NodeOrVector = new ast_type_traits::DynTypedNode(ParentStack.back());
8264*0a6a1f1dSLionel Sambuc } else {
8265*0a6a1f1dSLionel Sambuc if (NodeOrVector.template is<ast_type_traits::DynTypedNode *>()) {
8266*0a6a1f1dSLionel Sambuc auto *Node =
8267*0a6a1f1dSLionel Sambuc NodeOrVector.template get<ast_type_traits::DynTypedNode *>();
8268*0a6a1f1dSLionel Sambuc auto *Vector = new ASTContext::ParentVector(1, *Node);
8269*0a6a1f1dSLionel Sambuc NodeOrVector = Vector;
8270*0a6a1f1dSLionel Sambuc delete Node;
8271*0a6a1f1dSLionel Sambuc }
8272*0a6a1f1dSLionel Sambuc assert(NodeOrVector.template is<ASTContext::ParentVector *>());
8273*0a6a1f1dSLionel Sambuc
8274*0a6a1f1dSLionel Sambuc auto *Vector =
8275*0a6a1f1dSLionel Sambuc NodeOrVector.template get<ASTContext::ParentVector *>();
8276*0a6a1f1dSLionel Sambuc // Skip duplicates for types that have memoization data.
8277*0a6a1f1dSLionel Sambuc // We must check that the type has memoization data before calling
8278*0a6a1f1dSLionel Sambuc // std::find() because DynTypedNode::operator== can't compare all
8279*0a6a1f1dSLionel Sambuc // types.
8280*0a6a1f1dSLionel Sambuc bool Found = ParentStack.back().getMemoizationData() &&
8281*0a6a1f1dSLionel Sambuc std::find(Vector->begin(), Vector->end(),
8282*0a6a1f1dSLionel Sambuc ParentStack.back()) != Vector->end();
8283*0a6a1f1dSLionel Sambuc if (!Found)
8284*0a6a1f1dSLionel Sambuc Vector->push_back(ParentStack.back());
8285*0a6a1f1dSLionel Sambuc }
8286*0a6a1f1dSLionel Sambuc }
8287f4a2713aSLionel Sambuc ParentStack.push_back(ast_type_traits::DynTypedNode::create(*Node));
8288f4a2713aSLionel Sambuc bool Result = (this ->* traverse) (Node);
8289f4a2713aSLionel Sambuc ParentStack.pop_back();
8290f4a2713aSLionel Sambuc return Result;
8291f4a2713aSLionel Sambuc }
8292f4a2713aSLionel Sambuc
TraverseDecl(Decl * DeclNode)8293f4a2713aSLionel Sambuc bool TraverseDecl(Decl *DeclNode) {
8294f4a2713aSLionel Sambuc return TraverseNode(DeclNode, &VisitorBase::TraverseDecl);
8295f4a2713aSLionel Sambuc }
8296f4a2713aSLionel Sambuc
TraverseStmt(Stmt * StmtNode)8297f4a2713aSLionel Sambuc bool TraverseStmt(Stmt *StmtNode) {
8298f4a2713aSLionel Sambuc return TraverseNode(StmtNode, &VisitorBase::TraverseStmt);
8299f4a2713aSLionel Sambuc }
8300f4a2713aSLionel Sambuc
8301f4a2713aSLionel Sambuc ASTContext::ParentMap *Parents;
8302f4a2713aSLionel Sambuc llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack;
8303f4a2713aSLionel Sambuc
8304f4a2713aSLionel Sambuc friend class RecursiveASTVisitor<ParentMapASTVisitor>;
8305f4a2713aSLionel Sambuc };
8306f4a2713aSLionel Sambuc
8307f4a2713aSLionel Sambuc } // end namespace
8308f4a2713aSLionel Sambuc
8309*0a6a1f1dSLionel Sambuc ArrayRef<ast_type_traits::DynTypedNode>
getParents(const ast_type_traits::DynTypedNode & Node)8310f4a2713aSLionel Sambuc ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) {
8311f4a2713aSLionel Sambuc assert(Node.getMemoizationData() &&
8312f4a2713aSLionel Sambuc "Invariant broken: only nodes that support memoization may be "
8313f4a2713aSLionel Sambuc "used in the parent map.");
8314f4a2713aSLionel Sambuc if (!AllParents) {
8315f4a2713aSLionel Sambuc // We always need to run over the whole translation unit, as
8316f4a2713aSLionel Sambuc // hasAncestor can escape any subtree.
8317f4a2713aSLionel Sambuc AllParents.reset(
8318f4a2713aSLionel Sambuc ParentMapASTVisitor::buildMap(*getTranslationUnitDecl()));
8319f4a2713aSLionel Sambuc }
8320f4a2713aSLionel Sambuc ParentMap::const_iterator I = AllParents->find(Node.getMemoizationData());
8321f4a2713aSLionel Sambuc if (I == AllParents->end()) {
8322*0a6a1f1dSLionel Sambuc return None;
8323f4a2713aSLionel Sambuc }
8324*0a6a1f1dSLionel Sambuc if (auto *N = I->second.dyn_cast<ast_type_traits::DynTypedNode *>()) {
8325*0a6a1f1dSLionel Sambuc return llvm::makeArrayRef(N, 1);
8326*0a6a1f1dSLionel Sambuc }
8327*0a6a1f1dSLionel Sambuc return *I->second.get<ParentVector *>();
8328f4a2713aSLionel Sambuc }
8329f4a2713aSLionel Sambuc
8330f4a2713aSLionel Sambuc bool
ObjCMethodsAreEqual(const ObjCMethodDecl * MethodDecl,const ObjCMethodDecl * MethodImpl)8331f4a2713aSLionel Sambuc ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl,
8332f4a2713aSLionel Sambuc const ObjCMethodDecl *MethodImpl) {
8333f4a2713aSLionel Sambuc // No point trying to match an unavailable/deprecated mothod.
8334f4a2713aSLionel Sambuc if (MethodDecl->hasAttr<UnavailableAttr>()
8335f4a2713aSLionel Sambuc || MethodDecl->hasAttr<DeprecatedAttr>())
8336f4a2713aSLionel Sambuc return false;
8337f4a2713aSLionel Sambuc if (MethodDecl->getObjCDeclQualifier() !=
8338f4a2713aSLionel Sambuc MethodImpl->getObjCDeclQualifier())
8339f4a2713aSLionel Sambuc return false;
8340*0a6a1f1dSLionel Sambuc if (!hasSameType(MethodDecl->getReturnType(), MethodImpl->getReturnType()))
8341f4a2713aSLionel Sambuc return false;
8342f4a2713aSLionel Sambuc
8343f4a2713aSLionel Sambuc if (MethodDecl->param_size() != MethodImpl->param_size())
8344f4a2713aSLionel Sambuc return false;
8345f4a2713aSLionel Sambuc
8346f4a2713aSLionel Sambuc for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(),
8347f4a2713aSLionel Sambuc IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(),
8348f4a2713aSLionel Sambuc EF = MethodDecl->param_end();
8349f4a2713aSLionel Sambuc IM != EM && IF != EF; ++IM, ++IF) {
8350f4a2713aSLionel Sambuc const ParmVarDecl *DeclVar = (*IF);
8351f4a2713aSLionel Sambuc const ParmVarDecl *ImplVar = (*IM);
8352f4a2713aSLionel Sambuc if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier())
8353f4a2713aSLionel Sambuc return false;
8354f4a2713aSLionel Sambuc if (!hasSameType(DeclVar->getType(), ImplVar->getType()))
8355f4a2713aSLionel Sambuc return false;
8356f4a2713aSLionel Sambuc }
8357f4a2713aSLionel Sambuc return (MethodDecl->isVariadic() == MethodImpl->isVariadic());
8358f4a2713aSLionel Sambuc
8359f4a2713aSLionel Sambuc }
8360*0a6a1f1dSLionel Sambuc
8361*0a6a1f1dSLionel Sambuc // Explicitly instantiate this in case a Redeclarable<T> is used from a TU that
8362*0a6a1f1dSLionel Sambuc // doesn't include ASTContext.h
8363*0a6a1f1dSLionel Sambuc template
8364*0a6a1f1dSLionel Sambuc clang::LazyGenerationalUpdatePtr<
8365*0a6a1f1dSLionel Sambuc const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType
8366*0a6a1f1dSLionel Sambuc clang::LazyGenerationalUpdatePtr<
8367*0a6a1f1dSLionel Sambuc const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue(
8368*0a6a1f1dSLionel Sambuc const clang::ASTContext &Ctx, Decl *Value);
8369