blob: 42a6704d8aab73f44361022c7377f3df6cd5827e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000018#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000023#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000025#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000027#include "llvm/Support/MemoryBuffer.h"
Anders Carlsson29445a02009-07-18 21:19:52 +000028#include "RecordLayoutBuilder.h"
29
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
32enum FloatingRank {
33 FloatRank, DoubleRank, LongDoubleRank
34};
35
Chris Lattner61710852008-10-05 17:34:18 +000036ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
37 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000038 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +000039 Builtin::Context &builtins,
Mike Stump1eb44332009-09-09 15:08:12 +000040 bool FreeMem, unsigned size_reserve) :
41 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +000042 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stumpadaaad32009-10-20 02:12:22 +000043 sigjmp_bufDecl(0), BlockDescriptorType(0), SourceMgr(SM), LangOpts(LOpts),
Mike Stump1eb44332009-09-09 15:08:12 +000044 LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +000045 Idents(idents), Selectors(sels),
Mike Stump1eb44332009-09-09 15:08:12 +000046 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
David Chisnall0f436562009-08-17 16:35:33 +000047 ObjCIdRedefinitionType = QualType();
48 ObjCClassRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +000049 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +000050 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +000051 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000052}
53
Reid Spencer5f016e22007-07-11 17:01:13 +000054ASTContext::~ASTContext() {
55 // Deallocate all the types.
56 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000057 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 Types.pop_back();
59 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000060
Nuno Lopesb74668e2008-12-17 22:30:25 +000061 {
John McCall0953e762009-09-24 19:53:00 +000062 llvm::FoldingSet<ExtQuals>::iterator
63 I = ExtQualNodes.begin(), E = ExtQualNodes.end();
64 while (I != E)
65 Deallocate(&*I++);
66 }
67
68 {
Nuno Lopesb74668e2008-12-17 22:30:25 +000069 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
70 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
71 while (I != E) {
72 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
73 delete R;
74 }
75 }
76
77 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000078 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
79 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000080 while (I != E) {
81 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
82 delete R;
83 }
84 }
85
Douglas Gregorab452ba2009-03-26 23:50:42 +000086 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000087 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
88 NNS = NestedNameSpecifiers.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000089 NNSEnd = NestedNameSpecifiers.end();
90 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000091 /* Increment in loop */)
92 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000093
94 if (GlobalNestedNameSpecifier)
95 GlobalNestedNameSpecifier->Destroy(*this);
96
Eli Friedmanb26153c2008-05-27 03:08:09 +000097 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000098}
99
Mike Stump1eb44332009-09-09 15:08:12 +0000100void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
102 ExternalSource.reset(Source.take());
103}
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105void ASTContext::PrintStats() const {
106 fprintf(stderr, "*** AST Context Stats:\n");
107 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000108
Douglas Gregordbe833d2009-05-26 14:40:08 +0000109 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000110#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000111#define ABSTRACT_TYPE(Name, Parent)
112#include "clang/AST/TypeNodes.def"
113 0 // Extra
114 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000115
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
117 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000118 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120
Douglas Gregordbe833d2009-05-26 14:40:08 +0000121 unsigned Idx = 0;
122 unsigned TotalBytes = 0;
123#define TYPE(Name, Parent) \
124 if (counts[Idx]) \
125 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
126 TotalBytes += counts[Idx] * sizeof(Name##Type); \
127 ++Idx;
128#define ABSTRACT_TYPE(Name, Parent)
129#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregordbe833d2009-05-26 14:40:08 +0000131 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132
133 if (ExternalSource.get()) {
134 fprintf(stderr, "\n");
135 ExternalSource->PrintStats();
136 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000137}
138
139
140void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000141 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
142 R = QualType(Ty, 0);
143 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
Reid Spencer5f016e22007-07-11 17:01:13 +0000146void ASTContext::InitBuiltinTypes() {
147 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 // C99 6.2.5p19.
150 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 // C99 6.2.5p2.
153 InitBuiltinType(BoolTy, BuiltinType::Bool);
154 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000155 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 InitBuiltinType(CharTy, BuiltinType::Char_S);
157 else
158 InitBuiltinType(CharTy, BuiltinType::Char_U);
159 // C99 6.2.5p4.
160 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
161 InitBuiltinType(ShortTy, BuiltinType::Short);
162 InitBuiltinType(IntTy, BuiltinType::Int);
163 InitBuiltinType(LongTy, BuiltinType::Long);
164 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 // C99 6.2.5p6.
167 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
168 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
169 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
170 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
171 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 // C99 6.2.5p10.
174 InitBuiltinType(FloatTy, BuiltinType::Float);
175 InitBuiltinType(DoubleTy, BuiltinType::Double);
176 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000177
Chris Lattner2df9ced2009-04-30 02:43:43 +0000178 // GNU extension, 128-bit integers.
179 InitBuiltinType(Int128Ty, BuiltinType::Int128);
180 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
181
Chris Lattner3a250322009-02-26 23:43:47 +0000182 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
183 InitBuiltinType(WCharTy, BuiltinType::WChar);
184 else // C99
185 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000186
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000187 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
188 InitBuiltinType(Char16Ty, BuiltinType::Char16);
189 else // C99
190 Char16Ty = getFromTargetType(Target.getChar16Type());
191
192 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
193 InitBuiltinType(Char32Ty, BuiltinType::Char32);
194 else // C99
195 Char32Ty = getFromTargetType(Target.getChar32Type());
196
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000197 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000198 InitBuiltinType(OverloadTy, BuiltinType::Overload);
199
200 // Placeholder type for type-dependent expressions whose type is
201 // completely unknown. No code should ever check a type against
202 // DependentTy and users should never see it; however, it is here to
203 // help diagnose failures to properly check for type-dependent
204 // expressions.
205 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000206
Mike Stump1eb44332009-09-09 15:08:12 +0000207 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000208 // not yet been deduced.
209 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 // C99 6.2.5p11.
212 FloatComplexTy = getComplexType(FloatTy);
213 DoubleComplexTy = getComplexType(DoubleTy);
214 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000215
Steve Naroff7e219e42007-10-15 14:41:52 +0000216 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Steve Naroffde2e22d2009-07-15 18:40:39 +0000218 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
219 ObjCIdTypedefType = QualType();
220 ObjCClassTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Steve Naroffde2e22d2009-07-15 18:40:39 +0000222 // Builtin types for 'id' and 'Class'.
223 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
224 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Steve Naroff14108da2009-07-10 23:34:53 +0000225
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000226 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000228 // void * type
229 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000230
231 // nullptr type (C++0x 2.14.7)
232 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000233}
234
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000235MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000236ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000237 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000238 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000239 = InstantiatedFromStaticDataMember.find(Var);
240 if (Pos == InstantiatedFromStaticDataMember.end())
241 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Douglas Gregor7caa6822009-07-24 20:34:43 +0000243 return Pos->second;
244}
245
Mike Stump1eb44332009-09-09 15:08:12 +0000246void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000247ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
248 TemplateSpecializationKind TSK) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000249 assert(Inst->isStaticDataMember() && "Not a static data member");
250 assert(Tmpl->isStaticDataMember() && "Not a static data member");
251 assert(!InstantiatedFromStaticDataMember[Inst] &&
252 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000253 InstantiatedFromStaticDataMember[Inst]
254 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000255}
256
Anders Carlsson0d8df782009-08-29 19:37:28 +0000257UnresolvedUsingDecl *
258ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000259 llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
Anders Carlsson0d8df782009-08-29 19:37:28 +0000260 = InstantiatedFromUnresolvedUsingDecl.find(UUD);
261 if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
262 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Anders Carlsson0d8df782009-08-29 19:37:28 +0000264 return Pos->second;
265}
266
267void
268ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
269 UnresolvedUsingDecl *UUD) {
270 assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
271 "Already noted what using decl what instantiated from");
272 InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
273}
274
Anders Carlssond8b285f2009-09-01 04:26:58 +0000275FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
276 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
277 = InstantiatedFromUnnamedFieldDecl.find(Field);
278 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
279 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Anders Carlssond8b285f2009-09-01 04:26:58 +0000281 return Pos->second;
282}
283
284void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
285 FieldDecl *Tmpl) {
286 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
287 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
288 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
289 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Anders Carlssond8b285f2009-09-01 04:26:58 +0000291 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
292}
293
Douglas Gregor2e222532009-07-02 17:08:52 +0000294namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000295 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000296 : std::binary_function<SourceRange, SourceRange, bool> {
297 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Douglas Gregor2e222532009-07-02 17:08:52 +0000299 public:
300 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Douglas Gregor2e222532009-07-02 17:08:52 +0000302 bool operator()(SourceRange X, SourceRange Y) {
303 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
304 }
305 };
306}
307
308/// \brief Determine whether the given comment is a Doxygen-style comment.
309///
310/// \param Start the start of the comment text.
311///
312/// \param End the end of the comment text.
313///
314/// \param Member whether we want to check whether this is a member comment
315/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
316/// we only return true when we find a non-member comment.
Mike Stump1eb44332009-09-09 15:08:12 +0000317static bool
318isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
Douglas Gregor2e222532009-07-02 17:08:52 +0000319 bool Member = false) {
Mike Stump1eb44332009-09-09 15:08:12 +0000320 const char *BufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000321 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
322 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
323 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregor2e222532009-07-02 17:08:52 +0000325 if (End - Start < 4)
326 return false;
327
328 assert(Start[0] == '/' && "Not a comment?");
329 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
330 return false;
331 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
332 return false;
333
334 return (Start[3] == '<') == Member;
335}
336
337/// \brief Retrieve the comment associated with the given declaration, if
Mike Stump1eb44332009-09-09 15:08:12 +0000338/// it has one.
Douglas Gregor2e222532009-07-02 17:08:52 +0000339const char *ASTContext::getCommentForDecl(const Decl *D) {
340 if (!D)
341 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Douglas Gregor2e222532009-07-02 17:08:52 +0000343 // Check whether we have cached a comment string for this declaration
344 // already.
Mike Stump1eb44332009-09-09 15:08:12 +0000345 llvm::DenseMap<const Decl *, std::string>::iterator Pos
Douglas Gregor2e222532009-07-02 17:08:52 +0000346 = DeclComments.find(D);
347 if (Pos != DeclComments.end())
348 return Pos->second.c_str();
349
Mike Stump1eb44332009-09-09 15:08:12 +0000350 // If we have an external AST source and have not yet loaded comments from
Douglas Gregor2e222532009-07-02 17:08:52 +0000351 // that source, do so now.
352 if (ExternalSource && !LoadedExternalComments) {
353 std::vector<SourceRange> LoadedComments;
354 ExternalSource->ReadComments(LoadedComments);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Douglas Gregor2e222532009-07-02 17:08:52 +0000356 if (!LoadedComments.empty())
357 Comments.insert(Comments.begin(), LoadedComments.begin(),
358 LoadedComments.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Douglas Gregor2e222532009-07-02 17:08:52 +0000360 LoadedExternalComments = true;
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
363 // If there are no comments anywhere, we won't find anything.
Douglas Gregor2e222532009-07-02 17:08:52 +0000364 if (Comments.empty())
365 return 0;
366
367 // If the declaration doesn't map directly to a location in a file, we
368 // can't find the comment.
369 SourceLocation DeclStartLoc = D->getLocStart();
370 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
371 return 0;
372
373 // Find the comment that occurs just before this declaration.
374 std::vector<SourceRange>::iterator LastComment
Mike Stump1eb44332009-09-09 15:08:12 +0000375 = std::lower_bound(Comments.begin(), Comments.end(),
Douglas Gregor2e222532009-07-02 17:08:52 +0000376 SourceRange(DeclStartLoc),
377 BeforeInTranslationUnit(&SourceMgr));
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Douglas Gregor2e222532009-07-02 17:08:52 +0000379 // Decompose the location for the start of the declaration and find the
380 // beginning of the file buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000381 std::pair<FileID, unsigned> DeclStartDecomp
Douglas Gregor2e222532009-07-02 17:08:52 +0000382 = SourceMgr.getDecomposedLoc(DeclStartLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000383 const char *FileBufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000384 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Douglas Gregor2e222532009-07-02 17:08:52 +0000386 // First check whether we have a comment for a member.
387 if (LastComment != Comments.end() &&
388 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
389 isDoxygenComment(SourceMgr, *LastComment, true)) {
390 std::pair<FileID, unsigned> LastCommentEndDecomp
391 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
392 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
393 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
Mike Stump1eb44332009-09-09 15:08:12 +0000394 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000395 LastCommentEndDecomp.second)) {
396 // The Doxygen member comment comes after the declaration starts and
397 // is on the same line and in the same file as the declaration. This
398 // is the comment we want.
399 std::string &Result = DeclComments[D];
Mike Stump1eb44332009-09-09 15:08:12 +0000400 Result.append(FileBufferStart +
401 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000402 FileBufferStart + LastCommentEndDecomp.second + 1);
403 return Result.c_str();
404 }
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregor2e222532009-07-02 17:08:52 +0000407 if (LastComment == Comments.begin())
408 return 0;
409 --LastComment;
410
411 // Decompose the end of the comment.
412 std::pair<FileID, unsigned> LastCommentEndDecomp
413 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Douglas Gregor2e222532009-07-02 17:08:52 +0000415 // If the comment and the declaration aren't in the same file, then they
416 // aren't related.
417 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
418 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Douglas Gregor2e222532009-07-02 17:08:52 +0000420 // Check that we actually have a Doxygen comment.
421 if (!isDoxygenComment(SourceMgr, *LastComment))
422 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Douglas Gregor2e222532009-07-02 17:08:52 +0000424 // Compute the starting line for the declaration and for the end of the
425 // comment (this is expensive).
Mike Stump1eb44332009-09-09 15:08:12 +0000426 unsigned DeclStartLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000427 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
428 unsigned CommentEndLine
Mike Stump1eb44332009-09-09 15:08:12 +0000429 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000430 LastCommentEndDecomp.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Douglas Gregor2e222532009-07-02 17:08:52 +0000432 // If the comment does not end on the line prior to the declaration, then
433 // the comment is not associated with the declaration at all.
434 if (CommentEndLine + 1 != DeclStartLine)
435 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregor2e222532009-07-02 17:08:52 +0000437 // We have a comment, but there may be more comments on the previous lines.
438 // Keep looking so long as the comments are still Doxygen comments and are
439 // still adjacent.
Mike Stump1eb44332009-09-09 15:08:12 +0000440 unsigned ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000441 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
442 std::vector<SourceRange>::iterator FirstComment = LastComment;
443 while (FirstComment != Comments.begin()) {
444 // Look at the previous comment
445 --FirstComment;
446 std::pair<FileID, unsigned> Decomp
447 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Douglas Gregor2e222532009-07-02 17:08:52 +0000449 // If this previous comment is in a different file, we're done.
450 if (Decomp.first != DeclStartDecomp.first) {
451 ++FirstComment;
452 break;
453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Douglas Gregor2e222532009-07-02 17:08:52 +0000455 // If this comment is not a Doxygen comment, we're done.
456 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
457 ++FirstComment;
458 break;
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Douglas Gregor2e222532009-07-02 17:08:52 +0000461 // If the line number is not what we expected, we're done.
462 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
463 if (Line != ExpectedLine) {
464 ++FirstComment;
465 break;
466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Douglas Gregor2e222532009-07-02 17:08:52 +0000468 // Set the next expected line number.
Mike Stump1eb44332009-09-09 15:08:12 +0000469 ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000470 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
471 }
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Douglas Gregor2e222532009-07-02 17:08:52 +0000473 // The iterator range [FirstComment, LastComment] contains all of the
474 // BCPL comments that, together, are associated with this declaration.
475 // Form a single comment block string for this declaration that concatenates
476 // all of these comments.
477 std::string &Result = DeclComments[D];
478 while (FirstComment != LastComment) {
479 std::pair<FileID, unsigned> DecompStart
480 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
481 std::pair<FileID, unsigned> DecompEnd
482 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
483 Result.append(FileBufferStart + DecompStart.second,
484 FileBufferStart + DecompEnd.second + 1);
485 ++FirstComment;
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregor2e222532009-07-02 17:08:52 +0000488 // Append the last comment line.
Mike Stump1eb44332009-09-09 15:08:12 +0000489 Result.append(FileBufferStart +
490 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000491 FileBufferStart + LastCommentEndDecomp.second + 1);
492 return Result.c_str();
493}
494
Chris Lattner464175b2007-07-18 17:52:12 +0000495//===----------------------------------------------------------------------===//
496// Type Sizing and Analysis
497//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000498
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000499/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
500/// scalar floating point type.
501const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000502 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000503 assert(BT && "Not a floating point type!");
504 switch (BT->getKind()) {
505 default: assert(0 && "Not a floating point type!");
506 case BuiltinType::Float: return Target.getFloatFormat();
507 case BuiltinType::Double: return Target.getDoubleFormat();
508 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
509 }
510}
511
Mike Stump196efbf2009-09-22 02:43:44 +0000512/// getDeclAlignInBytes - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000513/// specified decl. Note that bitfields do not have a valid alignment, so
514/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000515unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000516 unsigned Align = Target.getCharWidth();
517
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000518 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedmandcdafb62009-02-22 02:56:25 +0000519 Align = std::max(Align, AA->getAlignment());
520
Chris Lattneraf707ab2009-01-24 21:53:27 +0000521 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
522 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000523 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000524 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000525 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000526 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
527 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000528 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
529 T = cast<ArrayType>(T)->getElementType();
530
531 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
532 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000533 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000534
535 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000536}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000537
Chris Lattnera7674d82007-07-13 22:13:22 +0000538/// getTypeSize - Return the size of the specified type, in bits. This method
539/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000540///
541/// FIXME: Pointers into different addr spaces could have different sizes and
542/// alignment requirements: getPointerInfo should take an AddrSpace, this
543/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000544std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000545ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000546 uint64_t Width=0;
547 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000548 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000549#define TYPE(Class, Base)
550#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000551#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000552#define DEPENDENT_TYPE(Class, Base) case Type::Class:
553#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000554 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000555 break;
556
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +0000557 case Type::ObjCProtocolList:
558 assert(false && "Should not see protocol list types");
559 break;
560
Chris Lattner692233e2007-07-13 22:27:08 +0000561 case Type::FunctionNoProto:
562 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000563 // GCC extension: alignof(function) = 32 bits
564 Width = 0;
565 Align = 32;
566 break;
567
Douglas Gregor72564e72009-02-26 23:50:07 +0000568 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000569 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000570 Width = 0;
571 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
572 break;
573
Steve Narofffb22d962007-08-30 01:06:46 +0000574 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000575 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattner98be4942008-03-05 18:54:05 +0000577 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000578 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000579 Align = EltInfo.second;
580 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000581 }
Nate Begeman213541a2008-04-18 23:10:10 +0000582 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000583 case Type::Vector: {
Mike Stump1eb44332009-09-09 15:08:12 +0000584 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000585 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000586 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000587 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000588 // If the alignment is not a power of 2, round up to the next power of 2.
589 // This happens for non-power-of-2 length vectors.
590 // FIXME: this should probably be a target property.
591 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000592 break;
593 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000594
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000595 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000596 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000597 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000598 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000599 // GCC extension: alignof(void) = 8 bits.
600 Width = 0;
601 Align = 8;
602 break;
603
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000604 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000605 Width = Target.getBoolWidth();
606 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000607 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000608 case BuiltinType::Char_S:
609 case BuiltinType::Char_U:
610 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000611 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000612 Width = Target.getCharWidth();
613 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000614 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000615 case BuiltinType::WChar:
616 Width = Target.getWCharWidth();
617 Align = Target.getWCharAlign();
618 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000619 case BuiltinType::Char16:
620 Width = Target.getChar16Width();
621 Align = Target.getChar16Align();
622 break;
623 case BuiltinType::Char32:
624 Width = Target.getChar32Width();
625 Align = Target.getChar32Align();
626 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000627 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000628 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000629 Width = Target.getShortWidth();
630 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000631 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000632 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000633 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000634 Width = Target.getIntWidth();
635 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000636 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000637 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000638 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000639 Width = Target.getLongWidth();
640 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000641 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000642 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000643 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000644 Width = Target.getLongLongWidth();
645 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000646 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000647 case BuiltinType::Int128:
648 case BuiltinType::UInt128:
649 Width = 128;
650 Align = 128; // int128_t is 128-bit aligned on all targets.
651 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000652 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000653 Width = Target.getFloatWidth();
654 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000655 break;
656 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000657 Width = Target.getDoubleWidth();
658 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000659 break;
660 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000661 Width = Target.getLongDoubleWidth();
662 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000663 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000664 case BuiltinType::NullPtr:
665 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
666 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000667 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000668 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000669 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000670 case Type::FixedWidthInt:
671 // FIXME: This isn't precisely correct; the width/alignment should depend
672 // on the available types for the target
673 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000674 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000675 Align = Width;
676 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000677 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000678 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000679 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000680 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000681 case Type::BlockPointer: {
682 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
683 Width = Target.getPointerWidth(AS);
684 Align = Target.getPointerAlign(AS);
685 break;
686 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000687 case Type::Pointer: {
688 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000689 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000690 Align = Target.getPointerAlign(AS);
691 break;
692 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000693 case Type::LValueReference:
694 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000695 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000696 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000697 // FIXME: This is wrong for struct layout: a reference in a struct has
698 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000699 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000700 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000701 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
702 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
703 // If we ever want to support other ABIs this needs to be abstracted.
704
Sebastian Redlf30208a2009-01-24 21:16:55 +0000705 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000706 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000707 getTypeInfo(getPointerDiffType());
708 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000709 if (Pointee->isFunctionType())
710 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000711 Align = PtrDiffInfo.second;
712 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000713 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000714 case Type::Complex: {
715 // Complex types have the same alignment as their elements, but twice the
716 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000717 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000718 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000719 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000720 Align = EltInfo.second;
721 break;
722 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000723 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000724 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000725 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
726 Width = Layout.getSize();
727 Align = Layout.getAlignment();
728 break;
729 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000730 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000731 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000732 const TagType *TT = cast<TagType>(T);
733
734 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000735 Width = 1;
736 Align = 1;
737 break;
738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Daniel Dunbar1d751182008-11-08 05:48:37 +0000740 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000741 return getTypeInfo(ET->getDecl()->getIntegerType());
742
Daniel Dunbar1d751182008-11-08 05:48:37 +0000743 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000744 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
745 Width = Layout.getSize();
746 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000747 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000748 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000749
John McCall49a832b2009-10-18 09:09:24 +0000750 case Type::SubstTemplateTypeParm: {
751 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
752 getReplacementType().getTypePtr());
753 }
754
John McCall7da24312009-09-05 00:15:47 +0000755 case Type::Elaborated: {
756 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType().getTypePtr());
757 }
758
Douglas Gregor18857642009-04-30 17:32:17 +0000759 case Type::Typedef: {
760 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000761 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregor18857642009-04-30 17:32:17 +0000762 Align = Aligned->getAlignment();
763 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
764 } else
765 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000766 break;
Chris Lattner71763312008-04-06 22:05:18 +0000767 }
Douglas Gregor18857642009-04-30 17:32:17 +0000768
769 case Type::TypeOfExpr:
770 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
771 .getTypePtr());
772
773 case Type::TypeOf:
774 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
775
Anders Carlsson395b4752009-06-24 19:06:50 +0000776 case Type::Decltype:
777 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
778 .getTypePtr());
779
Douglas Gregor18857642009-04-30 17:32:17 +0000780 case Type::QualifiedName:
781 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Douglas Gregor18857642009-04-30 17:32:17 +0000783 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000784 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000785 "Cannot request the size of a dependent type");
786 // FIXME: this is likely to be wrong once we support template
787 // aliases, since a template alias could refer to a typedef that
788 // has an __aligned__ attribute on it.
789 return getTypeInfo(getCanonicalType(T));
790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattner464175b2007-07-18 17:52:12 +0000792 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000793 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000794}
795
Chris Lattner34ebde42009-01-27 18:08:34 +0000796/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
797/// type for the current target in bits. This can be different than the ABI
798/// alignment in cases where it is beneficial for performance to overalign
799/// a data type.
800unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
801 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000802
803 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000804 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000805 T = CT->getElementType().getTypePtr();
806 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
807 T->isSpecificBuiltinType(BuiltinType::LongLong))
808 return std::max(ABIAlign, (unsigned)getTypeSize(T));
809
Chris Lattner34ebde42009-01-27 18:08:34 +0000810 return ABIAlign;
811}
812
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000813static void CollectLocalObjCIvars(ASTContext *Ctx,
814 const ObjCInterfaceDecl *OI,
815 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000816 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
817 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000818 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000819 if (!IVDecl->isInvalidDecl())
820 Fields.push_back(cast<FieldDecl>(IVDecl));
821 }
822}
823
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000824void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
825 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
826 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
827 CollectObjCIvars(SuperClass, Fields);
828 CollectLocalObjCIvars(this, OI, Fields);
829}
830
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000831/// ShallowCollectObjCIvars -
832/// Collect all ivars, including those synthesized, in the current class.
833///
834void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
835 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
836 bool CollectSynthesized) {
837 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
838 E = OI->ivar_end(); I != E; ++I) {
839 Ivars.push_back(*I);
840 }
841 if (CollectSynthesized)
842 CollectSynthesizedIvars(OI, Ivars);
843}
844
Fariborz Jahanian98200742009-05-12 18:14:29 +0000845void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
846 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000847 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
848 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian98200742009-05-12 18:14:29 +0000849 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
850 Ivars.push_back(Ivar);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Fariborz Jahanian98200742009-05-12 18:14:29 +0000852 // Also look into nested protocols.
853 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
854 E = PD->protocol_end(); P != E; ++P)
855 CollectProtocolSynthesizedIvars(*P, Ivars);
856}
857
858/// CollectSynthesizedIvars -
859/// This routine collect synthesized ivars for the designated class.
860///
861void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
862 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000863 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
864 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000865 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
866 Ivars.push_back(Ivar);
867 }
868 // Also look into interface's protocol list for properties declared
869 // in the protocol and whose ivars are synthesized.
870 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
871 PE = OI->protocol_end(); P != PE; ++P) {
872 ObjCProtocolDecl *PD = (*P);
873 CollectProtocolSynthesizedIvars(PD, Ivars);
874 }
875}
876
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000877unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
878 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000879 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
880 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000881 if ((*I)->getPropertyIvarDecl())
882 ++count;
883
884 // Also look into nested protocols.
885 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
886 E = PD->protocol_end(); P != E; ++P)
887 count += CountProtocolSynthesizedIvars(*P);
888 return count;
889}
890
Mike Stump1eb44332009-09-09 15:08:12 +0000891unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000892 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000893 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
894 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000895 if ((*I)->getPropertyIvarDecl())
896 ++count;
897 }
898 // Also look into interface's protocol list for properties declared
899 // in the protocol and whose ivars are synthesized.
900 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
901 PE = OI->protocol_end(); P != PE; ++P) {
902 ObjCProtocolDecl *PD = (*P);
903 count += CountProtocolSynthesizedIvars(PD);
904 }
905 return count;
906}
907
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000908/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
909ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
910 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
911 I = ObjCImpls.find(D);
912 if (I != ObjCImpls.end())
913 return cast<ObjCImplementationDecl>(I->second);
914 return 0;
915}
916/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
917ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
918 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
919 I = ObjCImpls.find(D);
920 if (I != ObjCImpls.end())
921 return cast<ObjCCategoryImplDecl>(I->second);
922 return 0;
923}
924
925/// \brief Set the implementation of ObjCInterfaceDecl.
926void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
927 ObjCImplementationDecl *ImplD) {
928 assert(IFaceD && ImplD && "Passed null params");
929 ObjCImpls[IFaceD] = ImplD;
930}
931/// \brief Set the implementation of ObjCCategoryDecl.
932void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
933 ObjCCategoryImplDecl *ImplD) {
934 assert(CatD && ImplD && "Passed null params");
935 ObjCImpls[CatD] = ImplD;
936}
937
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000938/// \brief Allocate an uninitialized DeclaratorInfo.
939///
940/// The caller should initialize the memory held by DeclaratorInfo using
941/// the TypeLoc wrappers.
942///
943/// \param T the type that will be the basis for type source info. This type
944/// should refer to how the declarator was written in source code, not to
945/// what type semantic analysis resolved the declarator to.
John McCall109de5e2009-10-21 00:23:54 +0000946DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T,
947 unsigned DataSize) {
948 if (!DataSize)
949 DataSize = TypeLoc::getFullDataSizeForType(T);
950 else
951 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
952 "incorrect data size provided to CreateDeclaratorInfo!");
953
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000954 DeclaratorInfo *DInfo =
955 (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
956 new (DInfo) DeclaratorInfo(T);
957 return DInfo;
958}
959
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000960/// getInterfaceLayoutImpl - Get or compute information about the
961/// layout of the given interface.
962///
963/// \param Impl - If given, also include the layout of the interface's
964/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000965const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000966ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
967 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000968 assert(!D->isForwardDecl() && "Invalid interface decl!");
969
Devang Patel44a3dde2008-06-04 21:54:36 +0000970 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000971 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000972 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
973 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
974 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000975
Daniel Dunbar453addb2009-05-03 11:16:44 +0000976 // Add in synthesized ivar count if laying out an implementation.
977 if (Impl) {
Anders Carlsson29445a02009-07-18 21:19:52 +0000978 unsigned FieldCount = D->ivar_size();
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000979 unsigned SynthCount = CountSynthesizedIvars(D);
980 FieldCount += SynthCount;
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000981 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000982 // entry. Note we can't cache this because we simply free all
983 // entries later; however we shouldn't look up implementations
984 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000985 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000986 return getObjCLayout(D, 0);
987 }
988
Mike Stump1eb44332009-09-09 15:08:12 +0000989 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000990 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
991 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Devang Patel44a3dde2008-06-04 21:54:36 +0000993 return *NewEntry;
994}
995
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000996const ASTRecordLayout &
997ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
998 return getObjCLayout(D, 0);
999}
1000
1001const ASTRecordLayout &
1002ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1003 return getObjCLayout(D->getClassInterface(), D);
1004}
1005
Devang Patel88a981b2007-11-01 19:11:01 +00001006/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +00001007/// specified record (struct/union/class), which indicates its size and field
1008/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +00001009const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001010 D = D->getDefinition(*this);
1011 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +00001012
Chris Lattner464175b2007-07-18 17:52:12 +00001013 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +00001014 // Note that we can't save a reference to the entry because this function
1015 // is recursive.
1016 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +00001017 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +00001018
Mike Stump1eb44332009-09-09 15:08:12 +00001019 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +00001020 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +00001021 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Chris Lattner5d2a6302007-07-18 18:26:58 +00001023 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001024}
1025
Chris Lattnera7674d82007-07-13 22:13:22 +00001026//===----------------------------------------------------------------------===//
1027// Type creation/memoization methods
1028//===----------------------------------------------------------------------===//
1029
John McCall0953e762009-09-24 19:53:00 +00001030QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1031 unsigned Fast = Quals.getFastQualifiers();
1032 Quals.removeFastQualifiers();
1033
1034 // Check if we've already instantiated this type.
1035 llvm::FoldingSetNodeID ID;
1036 ExtQuals::Profile(ID, TypeNode, Quals);
1037 void *InsertPos = 0;
1038 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1039 assert(EQ->getQualifiers() == Quals);
1040 QualType T = QualType(EQ, Fast);
1041 return T;
1042 }
1043
John McCall6b304a02009-09-24 23:30:46 +00001044 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001045 ExtQualNodes.InsertNode(New, InsertPos);
1046 QualType T = QualType(New, Fast);
1047 return T;
1048}
1049
1050QualType ASTContext::getVolatileType(QualType T) {
1051 QualType CanT = getCanonicalType(T);
1052 if (CanT.isVolatileQualified()) return T;
1053
1054 QualifierCollector Quals;
1055 const Type *TypeNode = Quals.strip(T);
1056 Quals.addVolatile();
1057
1058 return getExtQualType(TypeNode, Quals);
1059}
1060
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001061QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001062 QualType CanT = getCanonicalType(T);
1063 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001064 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001065
John McCall0953e762009-09-24 19:53:00 +00001066 // If we are composing extended qualifiers together, merge together
1067 // into one ExtQuals node.
1068 QualifierCollector Quals;
1069 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001070
John McCall0953e762009-09-24 19:53:00 +00001071 // If this type already has an address space specified, it cannot get
1072 // another one.
1073 assert(!Quals.hasAddressSpace() &&
1074 "Type cannot be in multiple addr spaces!");
1075 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001076
John McCall0953e762009-09-24 19:53:00 +00001077 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001078}
1079
Chris Lattnerb7d25532009-02-18 22:53:11 +00001080QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001081 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001082 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001083 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001084 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001086 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001087 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001088 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001089 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1090 return getPointerType(ResultType);
1091 }
1092 }
Mike Stump1eb44332009-09-09 15:08:12 +00001093
John McCall0953e762009-09-24 19:53:00 +00001094 // If we are composing extended qualifiers together, merge together
1095 // into one ExtQuals node.
1096 QualifierCollector Quals;
1097 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001098
John McCall0953e762009-09-24 19:53:00 +00001099 // If this type already has an ObjCGC specified, it cannot get
1100 // another one.
1101 assert(!Quals.hasObjCGCAttr() &&
1102 "Type cannot have multiple ObjCGCs!");
1103 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001104
John McCall0953e762009-09-24 19:53:00 +00001105 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001106}
Chris Lattnera7674d82007-07-13 22:13:22 +00001107
Mike Stump24556362009-07-25 21:26:53 +00001108QualType ASTContext::getNoReturnType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00001109 QualType ResultType;
Mike Stump24556362009-07-25 21:26:53 +00001110 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001111 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001112 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001113 ResultType = getPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001114 } else if (T->isBlockPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001115 QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001116 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001117 ResultType = getBlockPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001118 } else {
1119 assert (T->isFunctionType()
1120 && "can't noreturn qualify non-pointer to function or block type");
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Benjamin Kramerbdbeeb52009-09-25 11:47:22 +00001122 if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) {
1123 ResultType = getFunctionNoProtoType(FNPT->getResultType(), true);
John McCall0953e762009-09-24 19:53:00 +00001124 } else {
1125 const FunctionProtoType *F = T->getAs<FunctionProtoType>();
1126 ResultType
1127 = getFunctionType(F->getResultType(), F->arg_type_begin(),
1128 F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
1129 F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
1130 F->getNumExceptions(), F->exception_begin(), true);
1131 }
Mike Stump24556362009-07-25 21:26:53 +00001132 }
John McCall0953e762009-09-24 19:53:00 +00001133
1134 return getQualifiedType(ResultType, T.getQualifiers());
Mike Stump24556362009-07-25 21:26:53 +00001135}
1136
Reid Spencer5f016e22007-07-11 17:01:13 +00001137/// getComplexType - Return the uniqued reference to the type for a complex
1138/// number with the specified element type.
1139QualType ASTContext::getComplexType(QualType T) {
1140 // Unique pointers, to guarantee there is only one pointer of a particular
1141 // structure.
1142 llvm::FoldingSetNodeID ID;
1143 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 void *InsertPos = 0;
1146 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1147 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 // If the pointee type isn't canonical, this won't be a canonical type either,
1150 // so fill in the canonical type field.
1151 QualType Canonical;
1152 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001153 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 // Get the new insert position for the node we care about.
1156 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001157 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 }
John McCall6b304a02009-09-24 23:30:46 +00001159 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 Types.push_back(New);
1161 ComplexTypes.InsertNode(New, InsertPos);
1162 return QualType(New, 0);
1163}
1164
Eli Friedmanf98aba32009-02-13 02:31:07 +00001165QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1166 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1167 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1168 FixedWidthIntType *&Entry = Map[Width];
1169 if (!Entry)
1170 Entry = new FixedWidthIntType(Width, Signed);
1171 return QualType(Entry, 0);
1172}
Reid Spencer5f016e22007-07-11 17:01:13 +00001173
1174/// getPointerType - Return the uniqued reference to the type for a pointer to
1175/// the specified type.
1176QualType ASTContext::getPointerType(QualType T) {
1177 // Unique pointers, to guarantee there is only one pointer of a particular
1178 // structure.
1179 llvm::FoldingSetNodeID ID;
1180 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 void *InsertPos = 0;
1183 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1184 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 // If the pointee type isn't canonical, this won't be a canonical type either,
1187 // so fill in the canonical type field.
1188 QualType Canonical;
1189 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001190 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 // Get the new insert position for the node we care about.
1193 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001194 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 }
John McCall6b304a02009-09-24 23:30:46 +00001196 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 Types.push_back(New);
1198 PointerTypes.InsertNode(New, InsertPos);
1199 return QualType(New, 0);
1200}
1201
Mike Stump1eb44332009-09-09 15:08:12 +00001202/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001203/// a pointer to the specified block.
1204QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001205 assert(T->isFunctionType() && "block of function types only");
1206 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001207 // structure.
1208 llvm::FoldingSetNodeID ID;
1209 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Steve Naroff5618bd42008-08-27 16:04:49 +00001211 void *InsertPos = 0;
1212 if (BlockPointerType *PT =
1213 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1214 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001215
1216 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001217 // type either so fill in the canonical type field.
1218 QualType Canonical;
1219 if (!T->isCanonical()) {
1220 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Steve Naroff5618bd42008-08-27 16:04:49 +00001222 // Get the new insert position for the node we care about.
1223 BlockPointerType *NewIP =
1224 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001225 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001226 }
John McCall6b304a02009-09-24 23:30:46 +00001227 BlockPointerType *New
1228 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001229 Types.push_back(New);
1230 BlockPointerTypes.InsertNode(New, InsertPos);
1231 return QualType(New, 0);
1232}
1233
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001234/// getLValueReferenceType - Return the uniqued reference to the type for an
1235/// lvalue reference to the specified type.
1236QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 // Unique pointers, to guarantee there is only one pointer of a particular
1238 // structure.
1239 llvm::FoldingSetNodeID ID;
1240 ReferenceType::Profile(ID, T);
1241
1242 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001243 if (LValueReferenceType *RT =
1244 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001246
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 // If the referencee type isn't canonical, this won't be a canonical type
1248 // either, so fill in the canonical type field.
1249 QualType Canonical;
1250 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001251 Canonical = getLValueReferenceType(getCanonicalType(T));
1252
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001254 LValueReferenceType *NewIP =
1255 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001256 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 }
1258
John McCall6b304a02009-09-24 23:30:46 +00001259 LValueReferenceType *New
1260 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001262 LValueReferenceTypes.InsertNode(New, InsertPos);
1263 return QualType(New, 0);
1264}
1265
1266/// getRValueReferenceType - Return the uniqued reference to the type for an
1267/// rvalue reference to the specified type.
1268QualType ASTContext::getRValueReferenceType(QualType T) {
1269 // Unique pointers, to guarantee there is only one pointer of a particular
1270 // structure.
1271 llvm::FoldingSetNodeID ID;
1272 ReferenceType::Profile(ID, T);
1273
1274 void *InsertPos = 0;
1275 if (RValueReferenceType *RT =
1276 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1277 return QualType(RT, 0);
1278
1279 // If the referencee type isn't canonical, this won't be a canonical type
1280 // either, so fill in the canonical type field.
1281 QualType Canonical;
1282 if (!T->isCanonical()) {
1283 Canonical = getRValueReferenceType(getCanonicalType(T));
1284
1285 // Get the new insert position for the node we care about.
1286 RValueReferenceType *NewIP =
1287 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1288 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1289 }
1290
John McCall6b304a02009-09-24 23:30:46 +00001291 RValueReferenceType *New
1292 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001293 Types.push_back(New);
1294 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 return QualType(New, 0);
1296}
1297
Sebastian Redlf30208a2009-01-24 21:16:55 +00001298/// getMemberPointerType - Return the uniqued reference to the type for a
1299/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001300QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001301 // Unique pointers, to guarantee there is only one pointer of a particular
1302 // structure.
1303 llvm::FoldingSetNodeID ID;
1304 MemberPointerType::Profile(ID, T, Cls);
1305
1306 void *InsertPos = 0;
1307 if (MemberPointerType *PT =
1308 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1309 return QualType(PT, 0);
1310
1311 // If the pointee or class type isn't canonical, this won't be a canonical
1312 // type either, so fill in the canonical type field.
1313 QualType Canonical;
1314 if (!T->isCanonical()) {
1315 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1316
1317 // Get the new insert position for the node we care about.
1318 MemberPointerType *NewIP =
1319 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1320 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1321 }
John McCall6b304a02009-09-24 23:30:46 +00001322 MemberPointerType *New
1323 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001324 Types.push_back(New);
1325 MemberPointerTypes.InsertNode(New, InsertPos);
1326 return QualType(New, 0);
1327}
1328
Mike Stump1eb44332009-09-09 15:08:12 +00001329/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001330/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001331QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001332 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001333 ArrayType::ArraySizeModifier ASM,
1334 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001335 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1336 "Constant array of VLAs is illegal!");
1337
Chris Lattner38aeec72009-05-13 04:12:56 +00001338 // Convert the array size into a canonical width matching the pointer size for
1339 // the target.
1340 llvm::APInt ArySize(ArySizeIn);
1341 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001344 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Reid Spencer5f016e22007-07-11 17:01:13 +00001346 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001347 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001348 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 // If the element type isn't canonical, this won't be a canonical type either,
1352 // so fill in the canonical type field.
1353 QualType Canonical;
1354 if (!EltTy->isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001355 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001356 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001357 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001358 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001359 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001360 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 }
Mike Stump1eb44332009-09-09 15:08:12 +00001362
John McCall6b304a02009-09-24 23:30:46 +00001363 ConstantArrayType *New = new(*this,TypeAlignment)
1364 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001365 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001366 Types.push_back(New);
1367 return QualType(New, 0);
1368}
1369
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001370/// getVariableArrayType - Returns a non-unique reference to the type for a
1371/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001372QualType ASTContext::getVariableArrayType(QualType EltTy,
1373 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001374 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001375 unsigned EltTypeQuals,
1376 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001377 // Since we don't unique expressions, it isn't possible to unique VLA's
1378 // that have an expression provided for their size.
1379
John McCall6b304a02009-09-24 23:30:46 +00001380 VariableArrayType *New = new(*this, TypeAlignment)
1381 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001382
1383 VariableArrayTypes.push_back(New);
1384 Types.push_back(New);
1385 return QualType(New, 0);
1386}
1387
Douglas Gregor898574e2008-12-05 23:32:09 +00001388/// getDependentSizedArrayType - Returns a non-unique reference to
1389/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001390/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001391QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1392 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001393 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001394 unsigned EltTypeQuals,
1395 SourceRange Brackets) {
Mike Stump1eb44332009-09-09 15:08:12 +00001396 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001397 "Size must be type- or value-dependent!");
1398
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001399 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001400 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001401 EltTypeQuals, NumElts);
Douglas Gregor898574e2008-12-05 23:32:09 +00001402
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001403 void *InsertPos = 0;
1404 DependentSizedArrayType *Canon
1405 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1406 DependentSizedArrayType *New;
1407 if (Canon) {
1408 // We already have a canonical version of this array type; use it as
1409 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001410 New = new (*this, TypeAlignment)
1411 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1412 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001413 } else {
1414 QualType CanonEltTy = getCanonicalType(EltTy);
1415 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001416 New = new (*this, TypeAlignment)
1417 DependentSizedArrayType(*this, EltTy, QualType(),
1418 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001419 DependentSizedArrayTypes.InsertNode(New, InsertPos);
1420 } else {
1421 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1422 ASM, EltTypeQuals,
1423 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001424 New = new (*this, TypeAlignment)
1425 DependentSizedArrayType(*this, EltTy, Canon,
1426 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001427 }
1428 }
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Douglas Gregor898574e2008-12-05 23:32:09 +00001430 Types.push_back(New);
1431 return QualType(New, 0);
1432}
1433
Eli Friedmanc5773c42008-02-15 18:16:39 +00001434QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1435 ArrayType::ArraySizeModifier ASM,
1436 unsigned EltTypeQuals) {
1437 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001438 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001439
1440 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001441 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001442 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1443 return QualType(ATP, 0);
1444
1445 // If the element type isn't canonical, this won't be a canonical type
1446 // either, so fill in the canonical type field.
1447 QualType Canonical;
1448
1449 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001450 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001451 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001452
1453 // Get the new insert position for the node we care about.
1454 IncompleteArrayType *NewIP =
1455 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001456 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001457 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001458
John McCall6b304a02009-09-24 23:30:46 +00001459 IncompleteArrayType *New = new (*this, TypeAlignment)
1460 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001461
1462 IncompleteArrayTypes.InsertNode(New, InsertPos);
1463 Types.push_back(New);
1464 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001465}
1466
Steve Naroff73322922007-07-18 18:00:27 +00001467/// getVectorType - Return the unique reference to a vector type of
1468/// the specified element type and size. VectorType must be a built-in type.
1469QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001470 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Chris Lattnerf52ab252008-04-06 22:59:24 +00001472 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001473 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 // Check if we've already instantiated a vector of this type.
1476 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001477 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001478 void *InsertPos = 0;
1479 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1480 return QualType(VTP, 0);
1481
1482 // If the element type isn't canonical, this won't be a canonical type either,
1483 // so fill in the canonical type field.
1484 QualType Canonical;
1485 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001486 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 // Get the new insert position for the node we care about.
1489 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001490 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 }
John McCall6b304a02009-09-24 23:30:46 +00001492 VectorType *New = new (*this, TypeAlignment)
1493 VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001494 VectorTypes.InsertNode(New, InsertPos);
1495 Types.push_back(New);
1496 return QualType(New, 0);
1497}
1498
Nate Begeman213541a2008-04-18 23:10:10 +00001499/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001500/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001501QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001502 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Chris Lattnerf52ab252008-04-06 22:59:24 +00001504 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001505 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Steve Naroff73322922007-07-18 18:00:27 +00001507 // Check if we've already instantiated a vector of this type.
1508 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001509 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001510 void *InsertPos = 0;
1511 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1512 return QualType(VTP, 0);
1513
1514 // If the element type isn't canonical, this won't be a canonical type either,
1515 // so fill in the canonical type field.
1516 QualType Canonical;
1517 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001518 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Steve Naroff73322922007-07-18 18:00:27 +00001520 // Get the new insert position for the node we care about.
1521 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001522 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001523 }
John McCall6b304a02009-09-24 23:30:46 +00001524 ExtVectorType *New = new (*this, TypeAlignment)
1525 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001526 VectorTypes.InsertNode(New, InsertPos);
1527 Types.push_back(New);
1528 return QualType(New, 0);
1529}
1530
Mike Stump1eb44332009-09-09 15:08:12 +00001531QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001532 Expr *SizeExpr,
1533 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001534 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001535 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001536 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001538 void *InsertPos = 0;
1539 DependentSizedExtVectorType *Canon
1540 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1541 DependentSizedExtVectorType *New;
1542 if (Canon) {
1543 // We already have a canonical version of this array type; use it as
1544 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001545 New = new (*this, TypeAlignment)
1546 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1547 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001548 } else {
1549 QualType CanonVecTy = getCanonicalType(vecType);
1550 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001551 New = new (*this, TypeAlignment)
1552 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1553 AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001554 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1555 } else {
1556 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1557 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001558 New = new (*this, TypeAlignment)
1559 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001560 }
1561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001563 Types.push_back(New);
1564 return QualType(New, 0);
1565}
1566
Douglas Gregor72564e72009-02-26 23:50:07 +00001567/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001568///
Mike Stump24556362009-07-25 21:26:53 +00001569QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001570 // Unique functions, to guarantee there is only one function of a particular
1571 // structure.
1572 llvm::FoldingSetNodeID ID;
Mike Stump24556362009-07-25 21:26:53 +00001573 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001574
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001576 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001577 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Reid Spencer5f016e22007-07-11 17:01:13 +00001580 QualType Canonical;
1581 if (!ResultTy->isCanonical()) {
Mike Stump24556362009-07-25 21:26:53 +00001582 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Reid Spencer5f016e22007-07-11 17:01:13 +00001584 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001585 FunctionNoProtoType *NewIP =
1586 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001587 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001588 }
Mike Stump1eb44332009-09-09 15:08:12 +00001589
John McCall6b304a02009-09-24 23:30:46 +00001590 FunctionNoProtoType *New = new (*this, TypeAlignment)
1591 FunctionNoProtoType(ResultTy, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001592 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001593 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001594 return QualType(New, 0);
1595}
1596
1597/// getFunctionType - Return a normal function type with a typed argument
1598/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001599QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001600 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001601 unsigned TypeQuals, bool hasExceptionSpec,
1602 bool hasAnyExceptionSpec, unsigned NumExs,
Mike Stump24556362009-07-25 21:26:53 +00001603 const QualType *ExArray, bool NoReturn) {
Anders Carlsson83913e32009-09-16 23:47:08 +00001604 if (LangOpts.CPlusPlus) {
1605 for (unsigned i = 0; i != NumArgs; ++i)
John McCall0953e762009-09-24 19:53:00 +00001606 assert(!ArgArray[i].hasQualifiers() &&
1607 "C++ arguments can't have toplevel qualifiers!");
Anders Carlsson83913e32009-09-16 23:47:08 +00001608 }
1609
Reid Spencer5f016e22007-07-11 17:01:13 +00001610 // Unique functions, to guarantee there is only one function of a particular
1611 // structure.
1612 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001613 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001614 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001615 NumExs, ExArray, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001616
1617 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001618 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001619 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001621
1622 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001623 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001624 if (hasExceptionSpec)
1625 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001626 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1627 if (!ArgArray[i]->isCanonical())
1628 isCanonical = false;
1629
1630 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001631 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001632 QualType Canonical;
1633 if (!isCanonical) {
1634 llvm::SmallVector<QualType, 16> CanonicalArgs;
1635 CanonicalArgs.reserve(NumArgs);
1636 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001637 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001638
Chris Lattnerf52ab252008-04-06 22:59:24 +00001639 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001640 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001641 isVariadic, TypeQuals, false,
1642 false, 0, 0, NoReturn);
Sebastian Redl465226e2009-05-27 22:11:52 +00001643
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001645 FunctionProtoType *NewIP =
1646 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001647 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001649
Douglas Gregor72564e72009-02-26 23:50:07 +00001650 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001651 // for two variable size arrays (for parameter and exception types) at the
1652 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001653 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001654 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1655 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001656 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001657 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001658 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001659 ExArray, NumExs, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001661 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 return QualType(FTP, 0);
1663}
1664
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001665/// getTypeDeclType - Return the unique reference to the type for the
1666/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001667QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001668 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001669 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001671 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001672 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001673 else if (isa<TemplateTypeParmDecl>(Decl)) {
1674 assert(false && "Template type parameter types are always available.");
Mike Stump9fdbab32009-07-31 02:02:20 +00001675 } else if (ObjCInterfaceDecl *ObjCInterface
1676 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001677 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001678
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001679 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001680 if (PrevDecl)
1681 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001682 else
John McCall6b304a02009-09-24 23:30:46 +00001683 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
Mike Stump9fdbab32009-07-31 02:02:20 +00001684 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001685 if (PrevDecl)
1686 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001687 else
John McCall6b304a02009-09-24 23:30:46 +00001688 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
Mike Stump9fdbab32009-07-31 02:02:20 +00001689 } else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001690 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001691
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001692 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001693 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001694}
1695
Reid Spencer5f016e22007-07-11 17:01:13 +00001696/// getTypedefType - Return the unique reference to the type for the
1697/// specified typename decl.
1698QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1699 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Chris Lattnerf52ab252008-04-06 22:59:24 +00001701 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001702 Decl->TypeForDecl = new(*this, TypeAlignment)
1703 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 Types.push_back(Decl->TypeForDecl);
1705 return QualType(Decl->TypeForDecl, 0);
1706}
1707
John McCall49a832b2009-10-18 09:09:24 +00001708/// \brief Retrieve a substitution-result type.
1709QualType
1710ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1711 QualType Replacement) {
1712 assert(Replacement->isCanonical()
1713 && "replacement types must always be canonical");
1714
1715 llvm::FoldingSetNodeID ID;
1716 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1717 void *InsertPos = 0;
1718 SubstTemplateTypeParmType *SubstParm
1719 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1720
1721 if (!SubstParm) {
1722 SubstParm = new (*this, TypeAlignment)
1723 SubstTemplateTypeParmType(Parm, Replacement);
1724 Types.push_back(SubstParm);
1725 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1726 }
1727
1728 return QualType(SubstParm, 0);
1729}
1730
Douglas Gregorfab9d672009-02-05 23:33:38 +00001731/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001732/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001733/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001734QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001735 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001736 IdentifierInfo *Name) {
1737 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001738 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001739 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001740 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001741 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1742
1743 if (TypeParm)
1744 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001746 if (Name) {
1747 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall6b304a02009-09-24 23:30:46 +00001748 TypeParm = new (*this, TypeAlignment)
1749 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001750 } else
John McCall6b304a02009-09-24 23:30:46 +00001751 TypeParm = new (*this, TypeAlignment)
1752 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001753
1754 Types.push_back(TypeParm);
1755 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1756
1757 return QualType(TypeParm, 0);
1758}
1759
Mike Stump1eb44332009-09-09 15:08:12 +00001760QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001761ASTContext::getTemplateSpecializationType(TemplateName Template,
1762 const TemplateArgument *Args,
1763 unsigned NumArgs,
1764 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001765 if (!Canon.isNull())
1766 Canon = getCanonicalType(Canon);
1767 else {
1768 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001769 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1770 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1771 CanonArgs.reserve(NumArgs);
1772 for (unsigned I = 0; I != NumArgs; ++I)
1773 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1774
1775 // Determine whether this canonical template specialization type already
1776 // exists.
1777 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001778 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001779 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001780
1781 void *InsertPos = 0;
1782 TemplateSpecializationType *Spec
1783 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Douglas Gregor1275ae02009-07-28 23:00:59 +00001785 if (!Spec) {
1786 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001787 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001788 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001789 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001790 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001791 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001792 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001793 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001794 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001795 }
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Douglas Gregorb88e8882009-07-30 17:40:51 +00001797 if (Canon.isNull())
1798 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001799 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001800 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001801 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001802
Douglas Gregor1275ae02009-07-28 23:00:59 +00001803 // Allocate the (non-canonical) template specialization type, but don't
1804 // try to unique it: these types typically have location information that
1805 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001806 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001807 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001808 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001809 TemplateSpecializationType *Spec
1810 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001811 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Douglas Gregor55f6b142009-02-09 18:46:07 +00001813 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001814 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001815}
1816
Mike Stump1eb44332009-09-09 15:08:12 +00001817QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001818ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001819 QualType NamedType) {
1820 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001821 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001822
1823 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001824 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001825 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1826 if (T)
1827 return QualType(T, 0);
1828
Mike Stump1eb44332009-09-09 15:08:12 +00001829 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001830 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001831 Types.push_back(T);
1832 QualifiedNameTypes.InsertNode(T, InsertPos);
1833 return QualType(T, 0);
1834}
1835
Mike Stump1eb44332009-09-09 15:08:12 +00001836QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001837 const IdentifierInfo *Name,
1838 QualType Canon) {
1839 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1840
1841 if (Canon.isNull()) {
1842 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1843 if (CanonNNS != NNS)
1844 Canon = getTypenameType(CanonNNS, Name);
1845 }
1846
1847 llvm::FoldingSetNodeID ID;
1848 TypenameType::Profile(ID, NNS, Name);
1849
1850 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001851 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001852 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1853 if (T)
1854 return QualType(T, 0);
1855
1856 T = new (*this) TypenameType(NNS, Name, Canon);
1857 Types.push_back(T);
1858 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001859 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001860}
1861
Mike Stump1eb44332009-09-09 15:08:12 +00001862QualType
1863ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001864 const TemplateSpecializationType *TemplateId,
1865 QualType Canon) {
1866 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1867
1868 if (Canon.isNull()) {
1869 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1870 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1871 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1872 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00001873 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00001874 assert(CanonTemplateId &&
1875 "Canonical type must also be a template specialization type");
1876 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1877 }
1878 }
1879
1880 llvm::FoldingSetNodeID ID;
1881 TypenameType::Profile(ID, NNS, TemplateId);
1882
1883 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001884 TypenameType *T
Douglas Gregor17343172009-04-01 00:28:59 +00001885 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1886 if (T)
1887 return QualType(T, 0);
1888
1889 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1890 Types.push_back(T);
1891 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001892 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00001893}
1894
John McCall7da24312009-09-05 00:15:47 +00001895QualType
1896ASTContext::getElaboratedType(QualType UnderlyingType,
1897 ElaboratedType::TagKind Tag) {
1898 llvm::FoldingSetNodeID ID;
1899 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001900
John McCall7da24312009-09-05 00:15:47 +00001901 void *InsertPos = 0;
1902 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1903 if (T)
1904 return QualType(T, 0);
1905
1906 QualType Canon = getCanonicalType(UnderlyingType);
1907
1908 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1909 Types.push_back(T);
1910 ElaboratedTypes.InsertNode(T, InsertPos);
1911 return QualType(T, 0);
1912}
1913
Chris Lattner88cb27a2008-04-07 04:56:42 +00001914/// CmpProtocolNames - Comparison predicate for sorting protocols
1915/// alphabetically.
1916static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1917 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001918 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001919}
1920
1921static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1922 unsigned &NumProtocols) {
1923 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Chris Lattner88cb27a2008-04-07 04:56:42 +00001925 // Sort protocols, keyed by name.
1926 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1927
1928 // Remove duplicates.
1929 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1930 NumProtocols = ProtocolsEnd-Protocols;
1931}
1932
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001933/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1934/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00001935QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00001936 ObjCProtocolDecl **Protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001937 unsigned NumProtocols) {
1938 // Sort the protocol list alphabetically to canonicalize it.
1939 if (NumProtocols)
1940 SortAndUniqueProtocols(Protocols, NumProtocols);
1941
1942 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00001943 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001944
1945 void *InsertPos = 0;
1946 if (ObjCObjectPointerType *QT =
1947 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1948 return QualType(QT, 0);
1949
1950 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001951 ObjCObjectPointerType *QType = new (*this, TypeAlignment)
1952 ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001953
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001954 Types.push_back(QType);
1955 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1956 return QualType(QType, 0);
1957}
Chris Lattner88cb27a2008-04-07 04:56:42 +00001958
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001959/// getObjCInterfaceType - Return the unique reference to the type for the
1960/// specified ObjC interface decl. The list of protocols is optional.
1961QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001962 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Mike Stump1eb44332009-09-09 15:08:12 +00001963 if (NumProtocols)
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001964 // Sort the protocol list alphabetically to canonicalize it.
1965 SortAndUniqueProtocols(Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001967 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001968 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001970 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001971 if (ObjCInterfaceType *QT =
1972 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001973 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001975 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001976 ObjCInterfaceType *QType = new (*this, TypeAlignment)
1977 ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1978 Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001979 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001980 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001981 return QualType(QType, 0);
1982}
1983
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00001984QualType ASTContext::getObjCProtocolListType(QualType T,
1985 ObjCProtocolDecl **Protocols,
1986 unsigned NumProtocols) {
1987 llvm::FoldingSetNodeID ID;
1988 ObjCProtocolListType::Profile(ID, T, Protocols, NumProtocols);
1989
1990 void *InsertPos = 0;
1991 if (ObjCProtocolListType *QT =
1992 ObjCProtocolListTypes.FindNodeOrInsertPos(ID, InsertPos))
1993 return QualType(QT, 0);
1994
1995 // No Match;
1996 ObjCProtocolListType *QType = new (*this, TypeAlignment)
1997 ObjCProtocolListType(T, Protocols, NumProtocols);
1998 Types.push_back(QType);
1999 ObjCProtocolListTypes.InsertNode(QType, InsertPos);
2000 return QualType(QType, 0);
2001}
2002
Douglas Gregor72564e72009-02-26 23:50:07 +00002003/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2004/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002005/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002006/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002007/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002008QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002009 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002010 if (tofExpr->isTypeDependent()) {
2011 llvm::FoldingSetNodeID ID;
2012 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002013
Douglas Gregorb1975722009-07-30 23:18:24 +00002014 void *InsertPos = 0;
2015 DependentTypeOfExprType *Canon
2016 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2017 if (Canon) {
2018 // We already have a "canonical" version of an identical, dependent
2019 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002020 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002021 QualType((TypeOfExprType*)Canon, 0));
2022 }
2023 else {
2024 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002025 Canon
2026 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002027 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2028 toe = Canon;
2029 }
2030 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002031 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002032 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002033 }
Steve Naroff9752f252007-08-01 18:02:17 +00002034 Types.push_back(toe);
2035 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002036}
2037
Steve Naroff9752f252007-08-01 18:02:17 +00002038/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2039/// TypeOfType AST's. The only motivation to unique these nodes would be
2040/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002041/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002042/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002043QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002044 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002045 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002046 Types.push_back(tot);
2047 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002048}
2049
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002050/// getDecltypeForExpr - Given an expr, will return the decltype for that
2051/// expression, according to the rules in C++0x [dcl.type.simple]p4
2052static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002053 if (e->isTypeDependent())
2054 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002055
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002056 // If e is an id expression or a class member access, decltype(e) is defined
2057 // as the type of the entity named by e.
2058 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2059 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2060 return VD->getType();
2061 }
2062 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2063 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2064 return FD->getType();
2065 }
2066 // If e is a function call or an invocation of an overloaded operator,
2067 // (parentheses around e are ignored), decltype(e) is defined as the
2068 // return type of that function.
2069 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2070 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002072 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002073
2074 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002075 // defined as T&, otherwise decltype(e) is defined as T.
2076 if (e->isLvalue(Context) == Expr::LV_Valid)
2077 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002079 return T;
2080}
2081
Anders Carlsson395b4752009-06-24 19:06:50 +00002082/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2083/// DecltypeType AST's. The only motivation to unique these nodes would be
2084/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002085/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002086/// on canonical type's (which are always unique).
2087QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002088 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002089 if (e->isTypeDependent()) {
2090 llvm::FoldingSetNodeID ID;
2091 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002092
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002093 void *InsertPos = 0;
2094 DependentDecltypeType *Canon
2095 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2096 if (Canon) {
2097 // We already have a "canonical" version of an equivalent, dependent
2098 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002099 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002100 QualType((DecltypeType*)Canon, 0));
2101 }
2102 else {
2103 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002104 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002105 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2106 dt = Canon;
2107 }
2108 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002109 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002110 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002111 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002112 Types.push_back(dt);
2113 return QualType(dt, 0);
2114}
2115
Reid Spencer5f016e22007-07-11 17:01:13 +00002116/// getTagDeclType - Return the unique reference to the type for the
2117/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002118QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002119 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002120 // FIXME: What is the design on getTagDeclType when it requires casting
2121 // away const? mutable?
2122 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002123}
2124
Mike Stump1eb44332009-09-09 15:08:12 +00002125/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2126/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2127/// needs to agree with the definition in <stddef.h>.
Reid Spencer5f016e22007-07-11 17:01:13 +00002128QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002129 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002130}
2131
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002132/// getSignedWCharType - Return the type of "signed wchar_t".
2133/// Used when in C++, as a GCC extension.
2134QualType ASTContext::getSignedWCharType() const {
2135 // FIXME: derive from "Target" ?
2136 return WCharTy;
2137}
2138
2139/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2140/// Used when in C++, as a GCC extension.
2141QualType ASTContext::getUnsignedWCharType() const {
2142 // FIXME: derive from "Target" ?
2143 return UnsignedIntTy;
2144}
2145
Chris Lattner8b9023b2007-07-13 03:05:23 +00002146/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2147/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2148QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002149 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002150}
2151
Chris Lattnere6327742008-04-02 05:18:44 +00002152//===----------------------------------------------------------------------===//
2153// Type Operators
2154//===----------------------------------------------------------------------===//
2155
Chris Lattner77c96472008-04-06 22:41:35 +00002156/// getCanonicalType - Return the canonical (structural) type corresponding to
2157/// the specified potentially non-canonical type. The non-canonical version
2158/// of a type may have many "decorated" versions of types. Decorators can
2159/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2160/// to be free of any of these, allowing two canonical types to be compared
2161/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002162CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002163 QualifierCollector Quals;
2164 const Type *Ptr = Quals.strip(T);
2165 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002166
John McCall0953e762009-09-24 19:53:00 +00002167 // The canonical internal type will be the canonical type *except*
2168 // that we push type qualifiers down through array types.
2169
2170 // If there are no new qualifiers to push down, stop here.
2171 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002172 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002173
John McCall0953e762009-09-24 19:53:00 +00002174 // If the type qualifiers are on an array type, get the canonical
2175 // type of the array with the qualifiers applied to the element
2176 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002177 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2178 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002179 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002180
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002181 // Get the canonical version of the element with the extra qualifiers on it.
2182 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002183 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002184 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002186 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002187 return CanQualType::CreateUnsafe(
2188 getConstantArrayType(NewEltTy, CAT->getSize(),
2189 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002190 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002191 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002192 return CanQualType::CreateUnsafe(
2193 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002194 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002195
Douglas Gregor898574e2008-12-05 23:32:09 +00002196 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002197 return CanQualType::CreateUnsafe(
2198 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002199 DSAT->getSizeExpr() ?
2200 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002201 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002202 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002203 DSAT->getBracketsRange()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002204
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002205 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002206 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002207 VAT->getSizeExpr() ?
2208 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002209 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002210 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002211 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002212}
2213
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002214TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2215 // If this template name refers to a template, the canonical
2216 // template name merely stores the template itself.
2217 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002218 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002219
Mike Stump1eb44332009-09-09 15:08:12 +00002220 // If this template name refers to a set of overloaded function templates,
Douglas Gregord99cbe62009-07-29 18:26:50 +00002221 /// the canonical template name merely stores the set of function templates.
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002222 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2223 OverloadedFunctionDecl *CanonOvl = 0;
2224 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2225 FEnd = Ovl->function_end();
2226 F != FEnd; ++F) {
2227 Decl *Canon = F->get()->getCanonicalDecl();
2228 if (CanonOvl || Canon != F->get()) {
2229 if (!CanonOvl)
Mike Stump1eb44332009-09-09 15:08:12 +00002230 CanonOvl = OverloadedFunctionDecl::Create(*this,
2231 Ovl->getDeclContext(),
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002232 Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00002233
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002234 CanonOvl->addOverload(
2235 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2236 }
2237 }
Mike Stump1eb44332009-09-09 15:08:12 +00002238
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002239 return TemplateName(CanonOvl? CanonOvl : Ovl);
2240 }
Mike Stump1eb44332009-09-09 15:08:12 +00002241
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002242 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2243 assert(DTN && "Non-dependent template names must refer to template decls.");
2244 return DTN->CanonicalTemplateName;
2245}
2246
Mike Stump1eb44332009-09-09 15:08:12 +00002247TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002248ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2249 switch (Arg.getKind()) {
2250 case TemplateArgument::Null:
2251 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Douglas Gregor1275ae02009-07-28 23:00:59 +00002253 case TemplateArgument::Expression:
2254 // FIXME: Build canonical expression?
2255 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002256
Douglas Gregor1275ae02009-07-28 23:00:59 +00002257 case TemplateArgument::Declaration:
2258 return TemplateArgument(SourceLocation(),
2259 Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002260
Douglas Gregor1275ae02009-07-28 23:00:59 +00002261 case TemplateArgument::Integral:
2262 return TemplateArgument(SourceLocation(),
2263 *Arg.getAsIntegral(),
2264 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Douglas Gregor1275ae02009-07-28 23:00:59 +00002266 case TemplateArgument::Type:
2267 return TemplateArgument(SourceLocation(),
2268 getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002269
Douglas Gregor1275ae02009-07-28 23:00:59 +00002270 case TemplateArgument::Pack: {
2271 // FIXME: Allocate in ASTContext
2272 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2273 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002274 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002275 AEnd = Arg.pack_end();
2276 A != AEnd; (void)++A, ++Idx)
2277 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Douglas Gregor1275ae02009-07-28 23:00:59 +00002279 TemplateArgument Result;
2280 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2281 return Result;
2282 }
2283 }
2284
2285 // Silence GCC warning
2286 assert(false && "Unhandled template argument kind");
2287 return TemplateArgument();
2288}
2289
Douglas Gregord57959a2009-03-27 23:10:48 +00002290NestedNameSpecifier *
2291ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002292 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002293 return 0;
2294
2295 switch (NNS->getKind()) {
2296 case NestedNameSpecifier::Identifier:
2297 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002298 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002299 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2300 NNS->getAsIdentifier());
2301
2302 case NestedNameSpecifier::Namespace:
2303 // A namespace is canonical; build a nested-name-specifier with
2304 // this namespace and no prefix.
2305 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2306
2307 case NestedNameSpecifier::TypeSpec:
2308 case NestedNameSpecifier::TypeSpecWithTemplate: {
2309 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002310 return NestedNameSpecifier::Create(*this, 0,
2311 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002312 T.getTypePtr());
2313 }
2314
2315 case NestedNameSpecifier::Global:
2316 // The global specifier is canonical and unique.
2317 return NNS;
2318 }
2319
2320 // Required to silence a GCC warning
2321 return 0;
2322}
2323
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002324
2325const ArrayType *ASTContext::getAsArrayType(QualType T) {
2326 // Handle the non-qualified case efficiently.
John McCall0953e762009-09-24 19:53:00 +00002327 if (!T.hasQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002328 // Handle the common positive case fast.
2329 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2330 return AT;
2331 }
Mike Stump1eb44332009-09-09 15:08:12 +00002332
John McCall0953e762009-09-24 19:53:00 +00002333 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002334 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002335 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002336 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002337
John McCall0953e762009-09-24 19:53:00 +00002338 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002339 // implements C99 6.7.3p8: "If the specification of an array type includes
2340 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002341
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002342 // If we get here, we either have type qualifiers on the type, or we have
2343 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002344 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002345
John McCall0953e762009-09-24 19:53:00 +00002346 QualifierCollector Qs;
2347 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002348
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002349 // If we have a simple case, just return now.
2350 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002351 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002352 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002354 // Otherwise, we have an array and we have qualifiers on it. Push the
2355 // qualifiers into the array element type and return a new array type.
2356 // Get the canonical version of the element with the extra qualifiers on it.
2357 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002358 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002359
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002360 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2361 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2362 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002363 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002364 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2365 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2366 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002367 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002368
Mike Stump1eb44332009-09-09 15:08:12 +00002369 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002370 = dyn_cast<DependentSizedArrayType>(ATy))
2371 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002372 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002373 DSAT->getSizeExpr() ?
2374 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002375 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002376 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002377 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002378
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002379 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002380 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002381 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002382 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002383 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002384 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002385 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002386}
2387
2388
Chris Lattnere6327742008-04-02 05:18:44 +00002389/// getArrayDecayedType - Return the properly qualified result of decaying the
2390/// specified array type to a pointer. This operation is non-trivial when
2391/// handling typedefs etc. The canonical type of "T" must be an array type,
2392/// this returns a pointer to a properly qualified element of the array.
2393///
2394/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2395QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002396 // Get the element type with 'getAsArrayType' so that we don't lose any
2397 // typedefs in the element type of the array. This also handles propagation
2398 // of type qualifiers from the array type into the element type if present
2399 // (C99 6.7.3p8).
2400 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2401 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002402
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002403 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002404
2405 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002406 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002407}
2408
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002409QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002410 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002411 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002412 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002413 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2414 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002415 } else {
John McCall0953e762009-09-24 19:53:00 +00002416 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002417 }
2418 }
2419}
2420
Anders Carlssonfbbce492009-09-25 01:23:32 +00002421QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2422 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002423
Anders Carlssonfbbce492009-09-25 01:23:32 +00002424 if (const ArrayType *AT = getAsArrayType(ElemTy))
2425 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002426
Anders Carlsson6183a992008-12-21 03:44:36 +00002427 return ElemTy;
2428}
2429
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002430/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002431uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002432ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2433 uint64_t ElementCount = 1;
2434 do {
2435 ElementCount *= CA->getSize().getZExtValue();
2436 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2437 } while (CA);
2438 return ElementCount;
2439}
2440
Reid Spencer5f016e22007-07-11 17:01:13 +00002441/// getFloatingRank - Return a relative rank for floating point types.
2442/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002443static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002444 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002445 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002446
John McCall183700f2009-09-21 23:43:11 +00002447 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2448 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002449 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002450 case BuiltinType::Float: return FloatRank;
2451 case BuiltinType::Double: return DoubleRank;
2452 case BuiltinType::LongDouble: return LongDoubleRank;
2453 }
2454}
2455
Mike Stump1eb44332009-09-09 15:08:12 +00002456/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2457/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002458/// 'typeDomain' is a real floating point or complex type.
2459/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002460QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2461 QualType Domain) const {
2462 FloatingRank EltRank = getFloatingRank(Size);
2463 if (Domain->isComplexType()) {
2464 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002465 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002466 case FloatRank: return FloatComplexTy;
2467 case DoubleRank: return DoubleComplexTy;
2468 case LongDoubleRank: return LongDoubleComplexTy;
2469 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002470 }
Chris Lattner1361b112008-04-06 23:58:54 +00002471
2472 assert(Domain->isRealFloatingType() && "Unknown domain!");
2473 switch (EltRank) {
2474 default: assert(0 && "getFloatingRank(): illegal value for rank");
2475 case FloatRank: return FloatTy;
2476 case DoubleRank: return DoubleTy;
2477 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002478 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002479}
2480
Chris Lattner7cfeb082008-04-06 23:55:33 +00002481/// getFloatingTypeOrder - Compare the rank of the two specified floating
2482/// point types, ignoring the domain of the type (i.e. 'double' ==
2483/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002484/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002485int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2486 FloatingRank LHSR = getFloatingRank(LHS);
2487 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002488
Chris Lattnera75cea32008-04-06 23:38:49 +00002489 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002490 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002491 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002492 return 1;
2493 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002494}
2495
Chris Lattnerf52ab252008-04-06 22:59:24 +00002496/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2497/// routine will assert if passed a built-in type that isn't an integer or enum,
2498/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002499unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002500 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002501 if (EnumType* ET = dyn_cast<EnumType>(T))
2502 T = ET->getDecl()->getIntegerType().getTypePtr();
2503
Eli Friedmana3426752009-07-05 23:44:27 +00002504 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2505 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2506
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002507 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2508 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2509
2510 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2511 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2512
Eli Friedmanf98aba32009-02-13 02:31:07 +00002513 // There are two things which impact the integer rank: the width, and
2514 // the ordering of builtins. The builtin ordering is encoded in the
2515 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner1b63e4f2009-06-14 01:54:56 +00002516 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanf98aba32009-02-13 02:31:07 +00002517 return FWIT->getWidth() << 3;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002518
Chris Lattnerf52ab252008-04-06 22:59:24 +00002519 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002520 default: assert(0 && "getIntegerRank(): not a built-in integer");
2521 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002522 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002523 case BuiltinType::Char_S:
2524 case BuiltinType::Char_U:
2525 case BuiltinType::SChar:
2526 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002527 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002528 case BuiltinType::Short:
2529 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002530 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002531 case BuiltinType::Int:
2532 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002533 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002534 case BuiltinType::Long:
2535 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002536 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002537 case BuiltinType::LongLong:
2538 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002539 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002540 case BuiltinType::Int128:
2541 case BuiltinType::UInt128:
2542 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002543 }
2544}
2545
Eli Friedman04e83572009-08-20 04:21:42 +00002546/// \brief Whether this is a promotable bitfield reference according
2547/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2548///
2549/// \returns the type this bit-field will promote to, or NULL if no
2550/// promotion occurs.
2551QualType ASTContext::isPromotableBitField(Expr *E) {
2552 FieldDecl *Field = E->getBitField();
2553 if (!Field)
2554 return QualType();
2555
2556 QualType FT = Field->getType();
2557
2558 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2559 uint64_t BitWidth = BitWidthAP.getZExtValue();
2560 uint64_t IntSize = getTypeSize(IntTy);
2561 // GCC extension compatibility: if the bit-field size is less than or equal
2562 // to the size of int, it gets promoted no matter what its type is.
2563 // For instance, unsigned long bf : 4 gets promoted to signed int.
2564 if (BitWidth < IntSize)
2565 return IntTy;
2566
2567 if (BitWidth == IntSize)
2568 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2569
2570 // Types bigger than int are not subject to promotions, and therefore act
2571 // like the base type.
2572 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2573 // is ridiculous.
2574 return QualType();
2575}
2576
Eli Friedmana95d7572009-08-19 07:44:53 +00002577/// getPromotedIntegerType - Returns the type that Promotable will
2578/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2579/// integer type.
2580QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2581 assert(!Promotable.isNull());
2582 assert(Promotable->isPromotableIntegerType());
2583 if (Promotable->isSignedIntegerType())
2584 return IntTy;
2585 uint64_t PromotableSize = getTypeSize(Promotable);
2586 uint64_t IntSize = getTypeSize(IntTy);
2587 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2588 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2589}
2590
Mike Stump1eb44332009-09-09 15:08:12 +00002591/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002592/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002593/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002594int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002595 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2596 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002597 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Chris Lattnerf52ab252008-04-06 22:59:24 +00002599 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2600 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002601
Chris Lattner7cfeb082008-04-06 23:55:33 +00002602 unsigned LHSRank = getIntegerRank(LHSC);
2603 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002604
Chris Lattner7cfeb082008-04-06 23:55:33 +00002605 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2606 if (LHSRank == RHSRank) return 0;
2607 return LHSRank > RHSRank ? 1 : -1;
2608 }
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Chris Lattner7cfeb082008-04-06 23:55:33 +00002610 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2611 if (LHSUnsigned) {
2612 // If the unsigned [LHS] type is larger, return it.
2613 if (LHSRank >= RHSRank)
2614 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002615
Chris Lattner7cfeb082008-04-06 23:55:33 +00002616 // If the signed type can represent all values of the unsigned type, it
2617 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002618 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002619 return -1;
2620 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002621
Chris Lattner7cfeb082008-04-06 23:55:33 +00002622 // If the unsigned [RHS] type is larger, return it.
2623 if (RHSRank >= LHSRank)
2624 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002625
Chris Lattner7cfeb082008-04-06 23:55:33 +00002626 // If the signed type can represent all values of the unsigned type, it
2627 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002628 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002629 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002630}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002631
Mike Stump1eb44332009-09-09 15:08:12 +00002632// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002633QualType ASTContext::getCFConstantStringType() {
2634 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002635 CFConstantStringTypeDecl =
2636 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002637 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002638 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002639
Anders Carlsson71993dd2007-08-17 05:31:46 +00002640 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002641 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002642 // int flags;
2643 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002644 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002645 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002646 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002647 FieldTypes[3] = LongTy;
2648
Anders Carlsson71993dd2007-08-17 05:31:46 +00002649 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002650 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002651 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002652 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002653 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002654 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002655 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002656 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002657 }
2658
2659 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002660 }
Mike Stump1eb44332009-09-09 15:08:12 +00002661
Anders Carlsson71993dd2007-08-17 05:31:46 +00002662 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002663}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002664
Douglas Gregor319ac892009-04-23 22:29:11 +00002665void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002666 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002667 assert(Rec && "Invalid CFConstantStringType");
2668 CFConstantStringTypeDecl = Rec->getDecl();
2669}
2670
Mike Stump1eb44332009-09-09 15:08:12 +00002671QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002672 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002673 ObjCFastEnumerationStateTypeDecl =
2674 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2675 &Idents.get("__objcFastEnumerationState"));
Mike Stump1eb44332009-09-09 15:08:12 +00002676
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002677 QualType FieldTypes[] = {
2678 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002679 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002680 getPointerType(UnsignedLongTy),
2681 getConstantArrayType(UnsignedLongTy,
2682 llvm::APInt(32, 5), ArrayType::Normal, 0)
2683 };
Mike Stump1eb44332009-09-09 15:08:12 +00002684
Douglas Gregor44b43212008-12-11 16:49:14 +00002685 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002686 FieldDecl *Field = FieldDecl::Create(*this,
2687 ObjCFastEnumerationStateTypeDecl,
2688 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002689 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002690 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002691 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002692 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002693 }
Mike Stump1eb44332009-09-09 15:08:12 +00002694
Douglas Gregor44b43212008-12-11 16:49:14 +00002695 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002696 }
Mike Stump1eb44332009-09-09 15:08:12 +00002697
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002698 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2699}
2700
Mike Stumpadaaad32009-10-20 02:12:22 +00002701QualType ASTContext::getBlockDescriptorType() {
2702 if (BlockDescriptorType)
2703 return getTagDeclType(BlockDescriptorType);
2704
2705 RecordDecl *T;
2706 // FIXME: Needs the FlagAppleBlock bit.
2707 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2708 &Idents.get("__block_descriptor"));
2709
2710 QualType FieldTypes[] = {
2711 UnsignedLongTy,
2712 UnsignedLongTy,
2713 };
2714
2715 const char *FieldNames[] = {
2716 "reserved",
2717 "Size",
2718 };
2719
2720 for (size_t i = 0; i < 2; ++i) {
2721 FieldDecl *Field = FieldDecl::Create(*this,
2722 T,
2723 SourceLocation(),
2724 &Idents.get(FieldNames[i]),
2725 FieldTypes[i], /*DInfo=*/0,
2726 /*BitWidth=*/0,
2727 /*Mutable=*/false);
2728 T->addDecl(Field);
2729 }
2730
2731 T->completeDefinition(*this);
2732
2733 BlockDescriptorType = T;
2734
2735 return getTagDeclType(BlockDescriptorType);
2736}
2737
2738void ASTContext::setBlockDescriptorType(QualType T) {
2739 const RecordType *Rec = T->getAs<RecordType>();
2740 assert(Rec && "Invalid BlockDescriptorType");
2741 BlockDescriptorType = Rec->getDecl();
2742}
2743
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002744bool ASTContext::BlockRequiresCopying(QualType Ty) {
2745 if (Ty->isBlockPointerType())
2746 return true;
2747 if (isObjCNSObjectType(Ty))
2748 return true;
2749 if (Ty->isObjCObjectPointerType())
2750 return true;
2751 return false;
2752}
2753
2754QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
2755 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00002756 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002757 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00002758 // unsigned int __flags;
2759 // unsigned int __size;
Mike Stump38e16272009-10-21 22:01:24 +00002760 // void *__copy_helper; // as needed
2761 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002762 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00002763 // } *
2764
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002765 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
2766
2767 // FIXME: Move up
2768 static int UniqueBlockByRefTypeID = 0;
2769 char Name[36];
2770 sprintf(Name, "__Block_byref_%d_%s", ++UniqueBlockByRefTypeID, DeclName);
2771 RecordDecl *T;
2772 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2773 &Idents.get(Name));
2774 T->startDefinition();
2775 QualType Int32Ty = IntTy;
2776 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
2777 QualType FieldTypes[] = {
2778 getPointerType(VoidPtrTy),
2779 getPointerType(getTagDeclType(T)),
2780 Int32Ty,
2781 Int32Ty,
2782 getPointerType(VoidPtrTy),
2783 getPointerType(VoidPtrTy),
2784 Ty
2785 };
2786
2787 const char *FieldNames[] = {
2788 "__isa",
2789 "__forwarding",
2790 "__flags",
2791 "__size",
2792 "__copy_helper",
2793 "__destroy_helper",
2794 DeclName,
2795 };
2796
2797 for (size_t i = 0; i < 7; ++i) {
2798 if (!HasCopyAndDispose && i >=4 && i <= 5)
2799 continue;
2800 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
2801 &Idents.get(FieldNames[i]),
2802 FieldTypes[i], /*DInfo=*/0,
2803 /*BitWidth=*/0, /*Mutable=*/false);
2804 T->addDecl(Field);
2805 }
2806
2807 T->completeDefinition(*this);
2808
2809 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00002810}
2811
2812
2813QualType ASTContext::getBlockParmType(
2814 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpadaaad32009-10-20 02:12:22 +00002815 // FIXME: Move up
2816 static int UniqueBlockParmTypeID = 0;
2817 char Name[36];
2818 sprintf(Name, "__block_literal_%u", ++UniqueBlockParmTypeID);
2819 RecordDecl *T;
2820 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2821 &Idents.get(Name));
Mike Stumpadaaad32009-10-20 02:12:22 +00002822 QualType FieldTypes[] = {
2823 getPointerType(VoidPtrTy),
2824 IntTy,
2825 IntTy,
2826 getPointerType(VoidPtrTy),
2827 getPointerType(getBlockDescriptorType()),
2828 };
2829
2830 const char *FieldNames[] = {
2831 "__isa",
2832 "__flags",
2833 "__reserved",
2834 "__FuncPtr",
2835 "__descriptor"
2836 };
2837
2838 for (size_t i = 0; i < 5; ++i) {
Mike Stumpea26cb52009-10-21 03:49:08 +00002839 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00002840 &Idents.get(FieldNames[i]),
2841 FieldTypes[i], /*DInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00002842 /*BitWidth=*/0, /*Mutable=*/false);
2843 T->addDecl(Field);
2844 }
2845
2846 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
2847 const Expr *E = BlockDeclRefDecls[i];
2848 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
2849 clang::IdentifierInfo *Name = 0;
2850 if (BDRE) {
2851 const ValueDecl *D = BDRE->getDecl();
2852 Name = &Idents.get(D->getName());
2853 }
2854 QualType FieldType = E->getType();
2855
2856 if (BDRE && BDRE->isByRef())
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002857 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
2858 FieldType);
Mike Stumpea26cb52009-10-21 03:49:08 +00002859
2860 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
2861 Name, FieldType, /*DInfo=*/0,
2862 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpadaaad32009-10-20 02:12:22 +00002863 T->addDecl(Field);
2864 }
2865
2866 T->completeDefinition(*this);
Mike Stumpea26cb52009-10-21 03:49:08 +00002867
2868 return getPointerType(getTagDeclType(T));
Mike Stumpadaaad32009-10-20 02:12:22 +00002869}
2870
Douglas Gregor319ac892009-04-23 22:29:11 +00002871void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002872 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002873 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2874 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2875}
2876
Anders Carlssone8c49532007-10-29 06:33:42 +00002877// This returns true if a type has been typedefed to BOOL:
2878// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002879static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002880 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002881 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2882 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00002883
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002884 return false;
2885}
2886
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002887/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002888/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002889int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002890 uint64_t sz = getTypeSize(type);
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002892 // Make all integer and enum types at least as large as an int
2893 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002894 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002895 // Treat arrays as pointers, since that's how they're passed in.
2896 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002897 sz = getTypeSize(VoidPtrTy);
2898 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002899}
2900
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002901/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002902/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002903void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002904 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002905 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002906 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002907 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002908 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002909 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002910 // Compute size of all parameters.
2911 // Start with computing size of a pointer in number of bytes.
2912 // FIXME: There might(should) be a better way of doing this computation!
2913 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002914 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002915 // The first two arguments (self and _cmd) are pointers; account for
2916 // their size.
2917 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002918 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2919 E = Decl->param_end(); PI != E; ++PI) {
2920 QualType PType = (*PI)->getType();
2921 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002922 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002923 ParmOffset += sz;
2924 }
2925 S += llvm::utostr(ParmOffset);
2926 S += "@0:";
2927 S += llvm::utostr(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00002928
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002929 // Argument types.
2930 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002931 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2932 E = Decl->param_end(); PI != E; ++PI) {
2933 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00002934 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002935 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002936 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2937 // Use array's original type only if it has known number of
2938 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002939 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002940 PType = PVDecl->getType();
2941 } else if (PType->isFunctionType())
2942 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002943 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002944 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002945 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002946 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002947 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002948 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002949 }
2950}
2951
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002952/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002953/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002954/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2955/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00002956/// Property attributes are stored as a comma-delimited C string. The simple
2957/// attributes readonly and bycopy are encoded as single characters. The
2958/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2959/// encoded as single characters, followed by an identifier. Property types
2960/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002961/// these attributes are defined by the following enumeration:
2962/// @code
2963/// enum PropertyAttributes {
2964/// kPropertyReadOnly = 'R', // property is read-only.
2965/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2966/// kPropertyByref = '&', // property is a reference to the value last assigned
2967/// kPropertyDynamic = 'D', // property is dynamic
2968/// kPropertyGetter = 'G', // followed by getter selector name
2969/// kPropertySetter = 'S', // followed by setter selector name
2970/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2971/// kPropertyType = 't' // followed by old-style type encoding.
2972/// kPropertyWeak = 'W' // 'weak' property
2973/// kPropertyStrong = 'P' // property GC'able
2974/// kPropertyNonAtomic = 'N' // property non-atomic
2975/// };
2976/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00002977void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002978 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002979 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002980 // Collect information from the property implementation decl(s).
2981 bool Dynamic = false;
2982 ObjCPropertyImplDecl *SynthesizePID = 0;
2983
2984 // FIXME: Duplicated code due to poor abstraction.
2985 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00002986 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002987 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2988 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002989 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002990 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002991 ObjCPropertyImplDecl *PID = *i;
2992 if (PID->getPropertyDecl() == PD) {
2993 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2994 Dynamic = true;
2995 } else {
2996 SynthesizePID = PID;
2997 }
2998 }
2999 }
3000 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003001 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003002 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003003 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003004 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003005 ObjCPropertyImplDecl *PID = *i;
3006 if (PID->getPropertyDecl() == PD) {
3007 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3008 Dynamic = true;
3009 } else {
3010 SynthesizePID = PID;
3011 }
3012 }
Mike Stump1eb44332009-09-09 15:08:12 +00003013 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003014 }
3015 }
3016
3017 // FIXME: This is not very efficient.
3018 S = "T";
3019
3020 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003021 // GCC has some special rules regarding encoding of properties which
3022 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003023 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003024 true /* outermost type */,
3025 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003026
3027 if (PD->isReadOnly()) {
3028 S += ",R";
3029 } else {
3030 switch (PD->getSetterKind()) {
3031 case ObjCPropertyDecl::Assign: break;
3032 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003033 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003034 }
3035 }
3036
3037 // It really isn't clear at all what this means, since properties
3038 // are "dynamic by default".
3039 if (Dynamic)
3040 S += ",D";
3041
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003042 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3043 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003044
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003045 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3046 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003047 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003048 }
3049
3050 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3051 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003052 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003053 }
3054
3055 if (SynthesizePID) {
3056 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3057 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00003058 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003059 }
3060
3061 // FIXME: OBJCGC: weak & strong
3062}
3063
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003064/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00003065/// Another legacy compatibility encoding: 32-bit longs are encoded as
3066/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003067/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3068///
3069void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00003070 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00003071 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003072 if (BT->getKind() == BuiltinType::ULong &&
3073 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003074 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00003075 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003076 if (BT->getKind() == BuiltinType::Long &&
3077 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003078 PointeeTy = IntTy;
3079 }
3080 }
3081}
3082
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003083void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003084 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003085 // We follow the behavior of gcc, expanding structures which are
3086 // directly pointed to, and expanding embedded structures. Note that
3087 // these rules are sufficient to prevent recursive encoding of the
3088 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00003089 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00003090 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003091}
3092
Mike Stump1eb44332009-09-09 15:08:12 +00003093static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003094 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003095 const Expr *E = FD->getBitWidth();
3096 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3097 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00003098 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003099 S += 'b';
3100 S += llvm::utostr(N);
3101}
3102
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003103// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003104void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3105 bool ExpandPointedToStructures,
3106 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003107 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003108 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003109 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00003110 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003111 if (FD && FD->isBitField())
3112 return EncodeBitField(this, S, FD);
3113 char encoding;
3114 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003115 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003116 case BuiltinType::Void: encoding = 'v'; break;
3117 case BuiltinType::Bool: encoding = 'B'; break;
3118 case BuiltinType::Char_U:
3119 case BuiltinType::UChar: encoding = 'C'; break;
3120 case BuiltinType::UShort: encoding = 'S'; break;
3121 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003122 case BuiltinType::ULong:
3123 encoding =
3124 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00003125 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003126 case BuiltinType::UInt128: encoding = 'T'; break;
3127 case BuiltinType::ULongLong: encoding = 'Q'; break;
3128 case BuiltinType::Char_S:
3129 case BuiltinType::SChar: encoding = 'c'; break;
3130 case BuiltinType::Short: encoding = 's'; break;
3131 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003132 case BuiltinType::Long:
3133 encoding =
3134 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003135 break;
3136 case BuiltinType::LongLong: encoding = 'q'; break;
3137 case BuiltinType::Int128: encoding = 't'; break;
3138 case BuiltinType::Float: encoding = 'f'; break;
3139 case BuiltinType::Double: encoding = 'd'; break;
3140 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003141 }
Mike Stump1eb44332009-09-09 15:08:12 +00003142
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003143 S += encoding;
3144 return;
3145 }
Mike Stump1eb44332009-09-09 15:08:12 +00003146
John McCall183700f2009-09-21 23:43:11 +00003147 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003148 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00003149 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003150 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003151 return;
3152 }
Mike Stump1eb44332009-09-09 15:08:12 +00003153
Ted Kremenek6217b802009-07-29 21:53:49 +00003154 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003155 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003156 bool isReadOnly = false;
3157 // For historical/compatibility reasons, the read-only qualifier of the
3158 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3159 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003160 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003161 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003162 if (OutermostType && T.isConstQualified()) {
3163 isReadOnly = true;
3164 S += 'r';
3165 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003166 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003167 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003168 while (P->getAs<PointerType>())
3169 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003170 if (P.isConstQualified()) {
3171 isReadOnly = true;
3172 S += 'r';
3173 }
3174 }
3175 if (isReadOnly) {
3176 // Another legacy compatibility encoding. Some ObjC qualifier and type
3177 // combinations need to be rearranged.
3178 // Rewrite "in const" from "nr" to "rn"
3179 const char * s = S.c_str();
3180 int len = S.length();
3181 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3182 std::string replace = "rn";
3183 S.replace(S.end()-2, S.end(), replace);
3184 }
3185 }
Steve Naroff14108da2009-07-10 23:34:53 +00003186 if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00003187 S += ':';
3188 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00003189 }
Mike Stump1eb44332009-09-09 15:08:12 +00003190
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003191 if (PointeeTy->isCharType()) {
3192 // char pointer types should be encoded as '*' unless it is a
3193 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003194 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003195 S += '*';
3196 return;
3197 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003198 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003199 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3200 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3201 S += '#';
3202 return;
3203 }
3204 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3205 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3206 S += '@';
3207 return;
3208 }
3209 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003210 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003211 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003212 getLegacyIntegralTypeEncoding(PointeeTy);
3213
Mike Stump1eb44332009-09-09 15:08:12 +00003214 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003215 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003216 return;
3217 }
Mike Stump1eb44332009-09-09 15:08:12 +00003218
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003219 if (const ArrayType *AT =
3220 // Ignore type qualifiers etc.
3221 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003222 if (isa<IncompleteArrayType>(AT)) {
3223 // Incomplete arrays are encoded as a pointer to the array element.
3224 S += '^';
3225
Mike Stump1eb44332009-09-09 15:08:12 +00003226 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003227 false, ExpandStructures, FD);
3228 } else {
3229 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003230
Anders Carlsson559a8332009-02-22 01:38:57 +00003231 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3232 S += llvm::utostr(CAT->getSize().getZExtValue());
3233 else {
3234 //Variable length arrays are encoded as a regular array with 0 elements.
3235 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3236 S += '0';
3237 }
Mike Stump1eb44332009-09-09 15:08:12 +00003238
3239 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003240 false, ExpandStructures, FD);
3241 S += ']';
3242 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003243 return;
3244 }
Mike Stump1eb44332009-09-09 15:08:12 +00003245
John McCall183700f2009-09-21 23:43:11 +00003246 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003247 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003248 return;
3249 }
Mike Stump1eb44332009-09-09 15:08:12 +00003250
Ted Kremenek6217b802009-07-29 21:53:49 +00003251 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003252 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003253 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003254 // Anonymous structures print as '?'
3255 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3256 S += II->getName();
3257 } else {
3258 S += '?';
3259 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003260 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003261 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003262 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3263 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003264 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003265 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003266 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003267 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003268 S += '"';
3269 }
Mike Stump1eb44332009-09-09 15:08:12 +00003270
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003271 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003272 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003273 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003274 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003275 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003276 QualType qt = Field->getType();
3277 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003278 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003279 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003280 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003281 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003282 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003283 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003284 return;
3285 }
Mike Stump1eb44332009-09-09 15:08:12 +00003286
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003287 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003288 if (FD && FD->isBitField())
3289 EncodeBitField(this, S, FD);
3290 else
3291 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003292 return;
3293 }
Mike Stump1eb44332009-09-09 15:08:12 +00003294
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003295 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003296 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003297 return;
3298 }
Mike Stump1eb44332009-09-09 15:08:12 +00003299
John McCall0953e762009-09-24 19:53:00 +00003300 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003301 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003302 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003303 S += '{';
3304 const IdentifierInfo *II = OI->getIdentifier();
3305 S += II->getName();
3306 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003307 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003308 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003309 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003310 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003311 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003312 RecFields[i]);
3313 else
Mike Stump1eb44332009-09-09 15:08:12 +00003314 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003315 FD);
3316 }
3317 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003318 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003319 }
Mike Stump1eb44332009-09-09 15:08:12 +00003320
John McCall183700f2009-09-21 23:43:11 +00003321 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003322 if (OPT->isObjCIdType()) {
3323 S += '@';
3324 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003325 }
Mike Stump1eb44332009-09-09 15:08:12 +00003326
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003327 if (OPT->isObjCClassType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003328 S += '#';
3329 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003330 }
Mike Stump1eb44332009-09-09 15:08:12 +00003331
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003332 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003333 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003334 ExpandPointedToStructures,
3335 ExpandStructures, FD);
3336 if (FD || EncodingProperty) {
3337 // Note that we do extended encoding of protocol qualifer list
3338 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003339 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003340 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3341 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003342 S += '<';
3343 S += (*I)->getNameAsString();
3344 S += '>';
3345 }
3346 S += '"';
3347 }
3348 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003349 }
Mike Stump1eb44332009-09-09 15:08:12 +00003350
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003351 QualType PointeeTy = OPT->getPointeeType();
3352 if (!EncodingProperty &&
3353 isa<TypedefType>(PointeeTy.getTypePtr())) {
3354 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003355 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003356 // {...};
3357 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003358 getObjCEncodingForTypeImpl(PointeeTy, S,
3359 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003360 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003361 return;
3362 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003363
3364 S += '@';
3365 if (FD || EncodingProperty) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003366 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003367 S += OPT->getInterfaceDecl()->getNameAsCString();
3368 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3369 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003370 S += '<';
3371 S += (*I)->getNameAsString();
3372 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003373 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003374 S += '"';
3375 }
3376 return;
3377 }
Mike Stump1eb44332009-09-09 15:08:12 +00003378
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003379 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003380}
3381
Mike Stump1eb44332009-09-09 15:08:12 +00003382void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003383 std::string& S) const {
3384 if (QT & Decl::OBJC_TQ_In)
3385 S += 'n';
3386 if (QT & Decl::OBJC_TQ_Inout)
3387 S += 'N';
3388 if (QT & Decl::OBJC_TQ_Out)
3389 S += 'o';
3390 if (QT & Decl::OBJC_TQ_Bycopy)
3391 S += 'O';
3392 if (QT & Decl::OBJC_TQ_Byref)
3393 S += 'R';
3394 if (QT & Decl::OBJC_TQ_Oneway)
3395 S += 'V';
3396}
3397
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003398void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003399 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003400
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003401 BuiltinVaListType = T;
3402}
3403
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003404void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003405 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003406}
3407
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003408void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor319ac892009-04-23 22:29:11 +00003409 ObjCSelType = T;
3410
John McCall183700f2009-09-21 23:43:11 +00003411 const TypedefType *TT = T->getAs<TypedefType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003412 if (!TT)
3413 return;
3414 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003415
3416 // typedef struct objc_selector *SEL;
Ted Kremenek6217b802009-07-29 21:53:49 +00003417 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003418 if (!ptr)
3419 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003420 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003421 if (!rec)
3422 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003423 SelStructType = rec;
3424}
3425
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003426void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003427 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003428}
3429
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003430void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003431 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003432}
3433
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003434void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003435 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003436 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003437
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003438 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003439}
3440
Douglas Gregor7532dc62009-03-30 22:58:21 +00003441/// \brief Retrieve the template name that represents a qualified
3442/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003443TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003444 bool TemplateKeyword,
3445 TemplateDecl *Template) {
3446 llvm::FoldingSetNodeID ID;
3447 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3448
3449 void *InsertPos = 0;
3450 QualifiedTemplateName *QTN =
3451 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3452 if (!QTN) {
3453 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3454 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3455 }
3456
3457 return TemplateName(QTN);
3458}
3459
Douglas Gregord99cbe62009-07-29 18:26:50 +00003460/// \brief Retrieve the template name that represents a qualified
3461/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003462TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregord99cbe62009-07-29 18:26:50 +00003463 bool TemplateKeyword,
3464 OverloadedFunctionDecl *Template) {
3465 llvm::FoldingSetNodeID ID;
3466 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump1eb44332009-09-09 15:08:12 +00003467
Douglas Gregord99cbe62009-07-29 18:26:50 +00003468 void *InsertPos = 0;
3469 QualifiedTemplateName *QTN =
3470 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3471 if (!QTN) {
3472 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3473 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3474 }
Mike Stump1eb44332009-09-09 15:08:12 +00003475
Douglas Gregord99cbe62009-07-29 18:26:50 +00003476 return TemplateName(QTN);
3477}
3478
Douglas Gregor7532dc62009-03-30 22:58:21 +00003479/// \brief Retrieve the template name that represents a dependent
3480/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003481TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003482 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003483 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003484 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003485
3486 llvm::FoldingSetNodeID ID;
3487 DependentTemplateName::Profile(ID, NNS, Name);
3488
3489 void *InsertPos = 0;
3490 DependentTemplateName *QTN =
3491 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3492
3493 if (QTN)
3494 return TemplateName(QTN);
3495
3496 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3497 if (CanonNNS == NNS) {
3498 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3499 } else {
3500 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3501 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3502 }
3503
3504 DependentTemplateNames.InsertNode(QTN, InsertPos);
3505 return TemplateName(QTN);
3506}
3507
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003508/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003509/// TargetInfo, produce the corresponding type. The unsigned @p Type
3510/// is actually a value of type @c TargetInfo::IntType.
3511QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003512 switch (Type) {
Mike Stump1eb44332009-09-09 15:08:12 +00003513 case TargetInfo::NoInt: return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003514 case TargetInfo::SignedShort: return ShortTy;
3515 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3516 case TargetInfo::SignedInt: return IntTy;
3517 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3518 case TargetInfo::SignedLong: return LongTy;
3519 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3520 case TargetInfo::SignedLongLong: return LongLongTy;
3521 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3522 }
3523
3524 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00003525 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003526}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003527
3528//===----------------------------------------------------------------------===//
3529// Type Predicates.
3530//===----------------------------------------------------------------------===//
3531
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003532/// isObjCNSObjectType - Return true if this is an NSObject object using
3533/// NSObject attribute on a c-style pointer type.
3534/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003535/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003536///
3537bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3538 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3539 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003540 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003541 return true;
3542 }
Mike Stump1eb44332009-09-09 15:08:12 +00003543 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003544}
3545
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003546/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3547/// garbage collection attribute.
3548///
John McCall0953e762009-09-24 19:53:00 +00003549Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3550 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003551 if (getLangOptions().ObjC1 &&
3552 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003553 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003554 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003555 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003556 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003557 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003558 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003559 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003560 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003561 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003562 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003563 // Non-pointers have none gc'able attribute regardless of the attribute
3564 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003565 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003566 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003567 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003568 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003569}
3570
Chris Lattner6ac46a42008-04-07 06:51:04 +00003571//===----------------------------------------------------------------------===//
3572// Type Compatibility Testing
3573//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003574
Mike Stump1eb44332009-09-09 15:08:12 +00003575/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003576/// compatible.
3577static bool areCompatVectorTypes(const VectorType *LHS,
3578 const VectorType *RHS) {
3579 assert(LHS->isCanonical() && RHS->isCanonical());
3580 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003581 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003582}
3583
Steve Naroff4084c302009-07-23 01:01:38 +00003584//===----------------------------------------------------------------------===//
3585// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3586//===----------------------------------------------------------------------===//
3587
3588/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3589/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003590bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3591 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003592 if (lProto == rProto)
3593 return true;
3594 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3595 E = rProto->protocol_end(); PI != E; ++PI)
3596 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3597 return true;
3598 return false;
3599}
3600
Steve Naroff4084c302009-07-23 01:01:38 +00003601/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3602/// return true if lhs's protocols conform to rhs's protocol; false
3603/// otherwise.
3604bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3605 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3606 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3607 return false;
3608}
3609
3610/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3611/// ObjCQualifiedIDType.
3612bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3613 bool compare) {
3614 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003615 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003616 lhs->isObjCIdType() || lhs->isObjCClassType())
3617 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003618 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003619 rhs->isObjCIdType() || rhs->isObjCClassType())
3620 return true;
3621
3622 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003623 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003624
Steve Naroff4084c302009-07-23 01:01:38 +00003625 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003626
Steve Naroff4084c302009-07-23 01:01:38 +00003627 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003628 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003629 // make sure we check the class hierarchy.
3630 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3631 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3632 E = lhsQID->qual_end(); I != E; ++I) {
3633 // when comparing an id<P> on lhs with a static type on rhs,
3634 // see if static class implements all of id's protocols, directly or
3635 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003636 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003637 return false;
3638 }
3639 }
3640 // If there are no qualifiers and no interface, we have an 'id'.
3641 return true;
3642 }
Mike Stump1eb44332009-09-09 15:08:12 +00003643 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003644 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3645 E = lhsQID->qual_end(); I != E; ++I) {
3646 ObjCProtocolDecl *lhsProto = *I;
3647 bool match = false;
3648
3649 // when comparing an id<P> on lhs with a static type on rhs,
3650 // see if static class implements all of id's protocols, directly or
3651 // through its super class and categories.
3652 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3653 E = rhsOPT->qual_end(); J != E; ++J) {
3654 ObjCProtocolDecl *rhsProto = *J;
3655 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3656 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3657 match = true;
3658 break;
3659 }
3660 }
Mike Stump1eb44332009-09-09 15:08:12 +00003661 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00003662 // make sure we check the class hierarchy.
3663 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3664 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3665 E = lhsQID->qual_end(); I != E; ++I) {
3666 // when comparing an id<P> on lhs with a static type on rhs,
3667 // see if static class implements all of id's protocols, directly or
3668 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003669 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003670 match = true;
3671 break;
3672 }
3673 }
3674 }
3675 if (!match)
3676 return false;
3677 }
Mike Stump1eb44332009-09-09 15:08:12 +00003678
Steve Naroff4084c302009-07-23 01:01:38 +00003679 return true;
3680 }
Mike Stump1eb44332009-09-09 15:08:12 +00003681
Steve Naroff4084c302009-07-23 01:01:38 +00003682 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3683 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3684
Mike Stump1eb44332009-09-09 15:08:12 +00003685 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00003686 lhs->getAsObjCInterfacePointerType()) {
3687 if (lhsOPT->qual_empty()) {
3688 bool match = false;
3689 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3690 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3691 E = rhsQID->qual_end(); I != E; ++I) {
3692 // when comparing an id<P> on lhs with a static type on rhs,
3693 // see if static class implements all of id's protocols, directly or
3694 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003695 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003696 match = true;
3697 break;
3698 }
3699 }
3700 if (!match)
3701 return false;
3702 }
3703 return true;
3704 }
Mike Stump1eb44332009-09-09 15:08:12 +00003705 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003706 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3707 E = lhsOPT->qual_end(); I != E; ++I) {
3708 ObjCProtocolDecl *lhsProto = *I;
3709 bool match = false;
3710
3711 // when comparing an id<P> on lhs with a static type on rhs,
3712 // see if static class implements all of id's protocols, directly or
3713 // through its super class and categories.
3714 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3715 E = rhsQID->qual_end(); J != E; ++J) {
3716 ObjCProtocolDecl *rhsProto = *J;
3717 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3718 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3719 match = true;
3720 break;
3721 }
3722 }
3723 if (!match)
3724 return false;
3725 }
3726 return true;
3727 }
3728 return false;
3729}
3730
Eli Friedman3d815e72008-08-22 00:56:42 +00003731/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003732/// compatible for assignment from RHS to LHS. This handles validation of any
3733/// protocol qualifiers on the LHS or RHS.
3734///
Steve Naroff14108da2009-07-10 23:34:53 +00003735bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3736 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003737 // If either type represents the built-in 'id' or 'Class' types, return true.
3738 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00003739 return true;
3740
Steve Naroff4084c302009-07-23 01:01:38 +00003741 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00003742 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3743 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00003744 false);
3745
Steve Naroff14108da2009-07-10 23:34:53 +00003746 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3747 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00003748 if (LHS && RHS) // We have 2 user-defined types.
3749 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00003750
Steve Naroff4084c302009-07-23 01:01:38 +00003751 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003752}
3753
Eli Friedman3d815e72008-08-22 00:56:42 +00003754bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3755 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00003756 // Verify that the base decls are compatible: the RHS must be a subclass of
3757 // the LHS.
3758 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3759 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003760
Chris Lattner6ac46a42008-04-07 06:51:04 +00003761 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3762 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003763 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003764 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003765
Chris Lattner6ac46a42008-04-07 06:51:04 +00003766 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3767 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003768 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003769 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00003770
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003771 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3772 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003773 LHSPI != LHSPE; LHSPI++) {
3774 bool RHSImplementsProtocol = false;
3775
3776 // If the RHS doesn't implement the protocol on the left, the types
3777 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003778 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00003779 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00003780 RHSPI != RHSPE; RHSPI++) {
3781 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003782 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003783 break;
3784 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003785 }
3786 // FIXME: For better diagnostics, consider passing back the protocol name.
3787 if (!RHSImplementsProtocol)
3788 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003789 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003790 // The RHS implements all protocols listed on the LHS.
3791 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003792}
3793
Steve Naroff389bf462009-02-12 17:52:19 +00003794bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3795 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00003796 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3797 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003798
Steve Naroff14108da2009-07-10 23:34:53 +00003799 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00003800 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003801
3802 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3803 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00003804}
3805
Mike Stump1eb44332009-09-09 15:08:12 +00003806/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00003807/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00003808/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00003809/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00003810bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3811 return !mergeTypes(LHS, RHS).isNull();
3812}
3813
3814QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall183700f2009-09-21 23:43:11 +00003815 const FunctionType *lbase = lhs->getAs<FunctionType>();
3816 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00003817 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3818 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00003819 bool allLTypes = true;
3820 bool allRTypes = true;
3821
3822 // Check return type
3823 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3824 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003825 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3826 allLTypes = false;
3827 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3828 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00003829 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00003830 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3831 if (NoReturn != lbase->getNoReturnAttr())
3832 allLTypes = false;
3833 if (NoReturn != rbase->getNoReturnAttr())
3834 allRTypes = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003835
Eli Friedman3d815e72008-08-22 00:56:42 +00003836 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00003837 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3838 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003839 unsigned lproto_nargs = lproto->getNumArgs();
3840 unsigned rproto_nargs = rproto->getNumArgs();
3841
3842 // Compatible functions must have the same number of arguments
3843 if (lproto_nargs != rproto_nargs)
3844 return QualType();
3845
3846 // Variadic and non-variadic functions aren't compatible
3847 if (lproto->isVariadic() != rproto->isVariadic())
3848 return QualType();
3849
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003850 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3851 return QualType();
3852
Eli Friedman3d815e72008-08-22 00:56:42 +00003853 // Check argument compatibility
3854 llvm::SmallVector<QualType, 10> types;
3855 for (unsigned i = 0; i < lproto_nargs; i++) {
3856 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3857 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3858 QualType argtype = mergeTypes(largtype, rargtype);
3859 if (argtype.isNull()) return QualType();
3860 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003861 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3862 allLTypes = false;
3863 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3864 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003865 }
3866 if (allLTypes) return lhs;
3867 if (allRTypes) return rhs;
3868 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00003869 lproto->isVariadic(), lproto->getTypeQuals(),
3870 NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003871 }
3872
3873 if (lproto) allRTypes = false;
3874 if (rproto) allLTypes = false;
3875
Douglas Gregor72564e72009-02-26 23:50:07 +00003876 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003877 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003878 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003879 if (proto->isVariadic()) return QualType();
3880 // Check that the types are compatible with the types that
3881 // would result from default argument promotions (C99 6.7.5.3p15).
3882 // The only types actually affected are promotable integer
3883 // types and floats, which would be passed as a different
3884 // type depending on whether the prototype is visible.
3885 unsigned proto_nargs = proto->getNumArgs();
3886 for (unsigned i = 0; i < proto_nargs; ++i) {
3887 QualType argTy = proto->getArgType(i);
3888 if (argTy->isPromotableIntegerType() ||
3889 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3890 return QualType();
3891 }
3892
3893 if (allLTypes) return lhs;
3894 if (allRTypes) return rhs;
3895 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00003896 proto->getNumArgs(), proto->isVariadic(),
3897 proto->getTypeQuals(), NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003898 }
3899
3900 if (allLTypes) return lhs;
3901 if (allRTypes) return rhs;
Mike Stump24556362009-07-25 21:26:53 +00003902 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003903}
3904
3905QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003906 // C++ [expr]: If an expression initially has the type "reference to T", the
3907 // type is adjusted to "T" prior to any further analysis, the expression
3908 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003909 // expression is an lvalue unless the reference is an rvalue reference and
3910 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003911 // FIXME: C++ shouldn't be going through here! The rules are different
3912 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003913 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3914 // shouldn't be going through here!
Ted Kremenek6217b802009-07-29 21:53:49 +00003915 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003916 LHS = RT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00003917 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003918 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003919
Eli Friedman3d815e72008-08-22 00:56:42 +00003920 QualType LHSCan = getCanonicalType(LHS),
3921 RHSCan = getCanonicalType(RHS);
3922
3923 // If two types are identical, they are compatible.
3924 if (LHSCan == RHSCan)
3925 return LHS;
3926
John McCall0953e762009-09-24 19:53:00 +00003927 // If the qualifiers are different, the types aren't compatible... mostly.
3928 Qualifiers LQuals = LHSCan.getQualifiers();
3929 Qualifiers RQuals = RHSCan.getQualifiers();
3930 if (LQuals != RQuals) {
3931 // If any of these qualifiers are different, we have a type
3932 // mismatch.
3933 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
3934 LQuals.getAddressSpace() != RQuals.getAddressSpace())
3935 return QualType();
3936
3937 // Exactly one GC qualifier difference is allowed: __strong is
3938 // okay if the other type has no GC qualifier but is an Objective
3939 // C object pointer (i.e. implicitly strong by default). We fix
3940 // this by pretending that the unqualified type was actually
3941 // qualified __strong.
3942 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
3943 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
3944 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
3945
3946 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
3947 return QualType();
3948
3949 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
3950 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
3951 }
3952 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
3953 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
3954 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003955 return QualType();
John McCall0953e762009-09-24 19:53:00 +00003956 }
3957
3958 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00003959
Eli Friedman852d63b2009-06-01 01:22:52 +00003960 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3961 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003962
Chris Lattner1adb8832008-01-14 05:45:46 +00003963 // We want to consider the two function types to be the same for these
3964 // comparisons, just force one to the other.
3965 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3966 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003967
3968 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003969 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3970 LHSClass = Type::ConstantArray;
3971 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3972 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00003973
Nate Begeman213541a2008-04-18 23:10:10 +00003974 // Canonicalize ExtVector -> Vector.
3975 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3976 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00003977
Chris Lattnera36a61f2008-04-07 05:43:21 +00003978 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003979 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00003980 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00003981 // a signed integer type, or an unsigned integer type.
John McCall183700f2009-09-21 23:43:11 +00003982 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003983 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3984 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003985 }
John McCall183700f2009-09-21 23:43:11 +00003986 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003987 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3988 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003989 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003990
Eli Friedman3d815e72008-08-22 00:56:42 +00003991 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003992 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003993
Steve Naroff4a746782008-01-09 22:43:08 +00003994 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003995 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003996#define TYPE(Class, Base)
3997#define ABSTRACT_TYPE(Class, Base)
3998#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3999#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4000#include "clang/AST/TypeNodes.def"
4001 assert(false && "Non-canonical and dependent types shouldn't get here");
4002 return QualType();
4003
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004004 case Type::LValueReference:
4005 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00004006 case Type::MemberPointer:
4007 assert(false && "C++ should never be in mergeTypes");
4008 return QualType();
4009
4010 case Type::IncompleteArray:
4011 case Type::VariableArray:
4012 case Type::FunctionProto:
4013 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00004014 assert(false && "Types are eliminated above");
4015 return QualType();
4016
Chris Lattner1adb8832008-01-14 05:45:46 +00004017 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00004018 {
4019 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004020 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4021 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00004022 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4023 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00004024 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004025 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00004026 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004027 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004028 return getPointerType(ResultType);
4029 }
Steve Naroffc0febd52008-12-10 17:49:55 +00004030 case Type::BlockPointer:
4031 {
4032 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004033 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4034 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00004035 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4036 if (ResultType.isNull()) return QualType();
4037 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4038 return LHS;
4039 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4040 return RHS;
4041 return getBlockPointerType(ResultType);
4042 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004043 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00004044 {
4045 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4046 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4047 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4048 return QualType();
4049
4050 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4051 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4052 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4053 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00004054 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4055 return LHS;
4056 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4057 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00004058 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4059 ArrayType::ArraySizeModifier(), 0);
4060 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4061 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004062 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4063 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00004064 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4065 return LHS;
4066 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4067 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004068 if (LVAT) {
4069 // FIXME: This isn't correct! But tricky to implement because
4070 // the array's size has to be the size of LHS, but the type
4071 // has to be different.
4072 return LHS;
4073 }
4074 if (RVAT) {
4075 // FIXME: This isn't correct! But tricky to implement because
4076 // the array's size has to be the size of RHS, but the type
4077 // has to be different.
4078 return RHS;
4079 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00004080 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4081 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004082 return getIncompleteArrayType(ResultType,
4083 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004084 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004085 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00004086 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00004087 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00004088 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00004089 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00004090 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004091 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00004092 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00004093 case Type::Complex:
4094 // Distinct complex types are incompatible.
4095 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004096 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004097 // FIXME: The merged type should be an ExtVector!
John McCall183700f2009-09-21 23:43:11 +00004098 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00004099 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00004100 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004101 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00004102 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004103 // FIXME: This should be type compatibility, e.g. whether
4104 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00004105 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4106 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00004107 if (LHSIface && RHSIface &&
4108 canAssignObjCInterfaces(LHSIface, RHSIface))
4109 return LHS;
4110
Eli Friedman3d815e72008-08-22 00:56:42 +00004111 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004112 }
Steve Naroff14108da2009-07-10 23:34:53 +00004113 case Type::ObjCObjectPointer: {
John McCall183700f2009-09-21 23:43:11 +00004114 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4115 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00004116 return LHS;
4117
Steve Naroffbc76dd02008-12-10 22:14:21 +00004118 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00004119 }
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004120 case Type::FixedWidthInt:
4121 // Distinct fixed-width integers are not compatible.
4122 return QualType();
Douglas Gregor7532dc62009-03-30 22:58:21 +00004123 case Type::TemplateSpecialization:
4124 assert(false && "Dependent types have no size");
4125 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00004126 }
Douglas Gregor72564e72009-02-26 23:50:07 +00004127
4128 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004129}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00004130
Chris Lattner5426bf62008-04-07 07:01:58 +00004131//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00004132// Integer Predicates
4133//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00004134
Eli Friedmanad74a752008-06-28 06:23:08 +00004135unsigned ASTContext::getIntWidth(QualType T) {
4136 if (T == BoolTy)
4137 return 1;
Chris Lattner6a2b9262009-10-17 20:33:28 +00004138 if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00004139 return FWIT->getWidth();
4140 }
4141 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00004142 return (unsigned)getTypeSize(T);
4143}
4144
4145QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4146 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00004147
4148 // Turn <4 x signed int> -> <4 x unsigned int>
4149 if (const VectorType *VTy = T->getAs<VectorType>())
4150 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
4151 VTy->getNumElements());
4152
4153 // For enums, we return the unsigned version of the base type.
4154 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00004155 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00004156
4157 const BuiltinType *BTy = T->getAs<BuiltinType>();
4158 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00004159 switch (BTy->getKind()) {
4160 case BuiltinType::Char_S:
4161 case BuiltinType::SChar:
4162 return UnsignedCharTy;
4163 case BuiltinType::Short:
4164 return UnsignedShortTy;
4165 case BuiltinType::Int:
4166 return UnsignedIntTy;
4167 case BuiltinType::Long:
4168 return UnsignedLongTy;
4169 case BuiltinType::LongLong:
4170 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00004171 case BuiltinType::Int128:
4172 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00004173 default:
4174 assert(0 && "Unexpected signed integer type");
4175 return QualType();
4176 }
4177}
4178
Douglas Gregor2cf26342009-04-09 22:27:44 +00004179ExternalASTSource::~ExternalASTSource() { }
4180
4181void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00004182
4183
4184//===----------------------------------------------------------------------===//
4185// Builtin Type Computation
4186//===----------------------------------------------------------------------===//
4187
4188/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4189/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004190static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004191 ASTContext::GetBuiltinTypeError &Error,
4192 bool AllowTypeModifiers = true) {
4193 // Modifiers.
4194 int HowLong = 0;
4195 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004196
Chris Lattner86df27b2009-06-14 00:45:47 +00004197 // Read the modifiers first.
4198 bool Done = false;
4199 while (!Done) {
4200 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004201 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004202 case 'S':
4203 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4204 assert(!Signed && "Can't use 'S' modifier multiple times!");
4205 Signed = true;
4206 break;
4207 case 'U':
4208 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4209 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4210 Unsigned = true;
4211 break;
4212 case 'L':
4213 assert(HowLong <= 2 && "Can't have LLLL modifier");
4214 ++HowLong;
4215 break;
4216 }
4217 }
4218
4219 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004220
Chris Lattner86df27b2009-06-14 00:45:47 +00004221 // Read the base type.
4222 switch (*Str++) {
4223 default: assert(0 && "Unknown builtin type letter!");
4224 case 'v':
4225 assert(HowLong == 0 && !Signed && !Unsigned &&
4226 "Bad modifiers used with 'v'!");
4227 Type = Context.VoidTy;
4228 break;
4229 case 'f':
4230 assert(HowLong == 0 && !Signed && !Unsigned &&
4231 "Bad modifiers used with 'f'!");
4232 Type = Context.FloatTy;
4233 break;
4234 case 'd':
4235 assert(HowLong < 2 && !Signed && !Unsigned &&
4236 "Bad modifiers used with 'd'!");
4237 if (HowLong)
4238 Type = Context.LongDoubleTy;
4239 else
4240 Type = Context.DoubleTy;
4241 break;
4242 case 's':
4243 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4244 if (Unsigned)
4245 Type = Context.UnsignedShortTy;
4246 else
4247 Type = Context.ShortTy;
4248 break;
4249 case 'i':
4250 if (HowLong == 3)
4251 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4252 else if (HowLong == 2)
4253 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4254 else if (HowLong == 1)
4255 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4256 else
4257 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4258 break;
4259 case 'c':
4260 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4261 if (Signed)
4262 Type = Context.SignedCharTy;
4263 else if (Unsigned)
4264 Type = Context.UnsignedCharTy;
4265 else
4266 Type = Context.CharTy;
4267 break;
4268 case 'b': // boolean
4269 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4270 Type = Context.BoolTy;
4271 break;
4272 case 'z': // size_t.
4273 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4274 Type = Context.getSizeType();
4275 break;
4276 case 'F':
4277 Type = Context.getCFConstantStringType();
4278 break;
4279 case 'a':
4280 Type = Context.getBuiltinVaListType();
4281 assert(!Type.isNull() && "builtin va list type not initialized!");
4282 break;
4283 case 'A':
4284 // This is a "reference" to a va_list; however, what exactly
4285 // this means depends on how va_list is defined. There are two
4286 // different kinds of va_list: ones passed by value, and ones
4287 // passed by reference. An example of a by-value va_list is
4288 // x86, where va_list is a char*. An example of by-ref va_list
4289 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4290 // we want this argument to be a char*&; for x86-64, we want
4291 // it to be a __va_list_tag*.
4292 Type = Context.getBuiltinVaListType();
4293 assert(!Type.isNull() && "builtin va list type not initialized!");
4294 if (Type->isArrayType()) {
4295 Type = Context.getArrayDecayedType(Type);
4296 } else {
4297 Type = Context.getLValueReferenceType(Type);
4298 }
4299 break;
4300 case 'V': {
4301 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004302 unsigned NumElements = strtoul(Str, &End, 10);
4303 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004304
Chris Lattner86df27b2009-06-14 00:45:47 +00004305 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004306
Chris Lattner86df27b2009-06-14 00:45:47 +00004307 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4308 Type = Context.getVectorType(ElementType, NumElements);
4309 break;
4310 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00004311 case 'X': {
4312 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4313 Type = Context.getComplexType(ElementType);
4314 break;
4315 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004316 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004317 Type = Context.getFILEType();
4318 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004319 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004320 return QualType();
4321 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004322 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004323 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004324 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004325 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004326 else
4327 Type = Context.getjmp_bufType();
4328
Mike Stumpfd612db2009-07-28 23:47:15 +00004329 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004330 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004331 return QualType();
4332 }
4333 break;
Mike Stump782fa302009-07-28 02:25:19 +00004334 }
Mike Stump1eb44332009-09-09 15:08:12 +00004335
Chris Lattner86df27b2009-06-14 00:45:47 +00004336 if (!AllowTypeModifiers)
4337 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004338
Chris Lattner86df27b2009-06-14 00:45:47 +00004339 Done = false;
4340 while (!Done) {
4341 switch (*Str++) {
4342 default: Done = true; --Str; break;
4343 case '*':
4344 Type = Context.getPointerType(Type);
4345 break;
4346 case '&':
4347 Type = Context.getLValueReferenceType(Type);
4348 break;
4349 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4350 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004351 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004352 break;
4353 }
4354 }
Mike Stump1eb44332009-09-09 15:08:12 +00004355
Chris Lattner86df27b2009-06-14 00:45:47 +00004356 return Type;
4357}
4358
4359/// GetBuiltinType - Return the type for the specified builtin.
4360QualType ASTContext::GetBuiltinType(unsigned id,
4361 GetBuiltinTypeError &Error) {
4362 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004363
Chris Lattner86df27b2009-06-14 00:45:47 +00004364 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004365
Chris Lattner86df27b2009-06-14 00:45:47 +00004366 Error = GE_None;
4367 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4368 if (Error != GE_None)
4369 return QualType();
4370 while (TypeStr[0] && TypeStr[0] != '.') {
4371 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4372 if (Error != GE_None)
4373 return QualType();
4374
4375 // Do array -> pointer decay. The builtin should use the decayed type.
4376 if (Ty->isArrayType())
4377 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004378
Chris Lattner86df27b2009-06-14 00:45:47 +00004379 ArgTypes.push_back(Ty);
4380 }
4381
4382 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4383 "'.' should only occur at end of builtin type list!");
4384
4385 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4386 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4387 return getFunctionNoProtoType(ResType);
4388 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4389 TypeStr[0] == '.', 0);
4390}
Eli Friedmana95d7572009-08-19 07:44:53 +00004391
4392QualType
4393ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4394 // Perform the usual unary conversions. We do this early so that
4395 // integral promotions to "int" can allow us to exit early, in the
4396 // lhs == rhs check. Also, for conversion purposes, we ignore any
4397 // qualifiers. For example, "const float" and "float" are
4398 // equivalent.
4399 if (lhs->isPromotableIntegerType())
4400 lhs = getPromotedIntegerType(lhs);
4401 else
4402 lhs = lhs.getUnqualifiedType();
4403 if (rhs->isPromotableIntegerType())
4404 rhs = getPromotedIntegerType(rhs);
4405 else
4406 rhs = rhs.getUnqualifiedType();
4407
4408 // If both types are identical, no conversion is needed.
4409 if (lhs == rhs)
4410 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004411
Eli Friedmana95d7572009-08-19 07:44:53 +00004412 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4413 // The caller can deal with this (e.g. pointer + int).
4414 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4415 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004416
4417 // At this point, we have two different arithmetic types.
4418
Eli Friedmana95d7572009-08-19 07:44:53 +00004419 // Handle complex types first (C99 6.3.1.8p1).
4420 if (lhs->isComplexType() || rhs->isComplexType()) {
4421 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004422 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004423 // convert the rhs to the lhs complex type.
4424 return lhs;
4425 }
Mike Stump1eb44332009-09-09 15:08:12 +00004426 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004427 // convert the lhs to the rhs complex type.
4428 return rhs;
4429 }
4430 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004431 // When both operands are complex, the shorter operand is converted to the
4432 // type of the longer, and that is the type of the result. This corresponds
4433 // to what is done when combining two real floating-point operands.
4434 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004435 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004436 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004437 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004438 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004439 // "double _Complex" is promoted to "long double _Complex".
4440 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004441
4442 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004443 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004444 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004445 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004446 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004447 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4448 // domains match. This is a requirement for our implementation, C99
4449 // does not require this promotion.
4450 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4451 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4452 return rhs;
4453 } else { // handle "_Complex double, double".
4454 return lhs;
4455 }
4456 }
4457 return lhs; // The domain/size match exactly.
4458 }
4459 // Now handle "real" floating types (i.e. float, double, long double).
4460 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4461 // if we have an integer operand, the result is the real floating type.
4462 if (rhs->isIntegerType()) {
4463 // convert rhs to the lhs floating point type.
4464 return lhs;
4465 }
4466 if (rhs->isComplexIntegerType()) {
4467 // convert rhs to the complex floating point type.
4468 return getComplexType(lhs);
4469 }
4470 if (lhs->isIntegerType()) {
4471 // convert lhs to the rhs floating point type.
4472 return rhs;
4473 }
Mike Stump1eb44332009-09-09 15:08:12 +00004474 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004475 // convert lhs to the complex floating point type.
4476 return getComplexType(rhs);
4477 }
4478 // We have two real floating types, float/complex combos were handled above.
4479 // Convert the smaller operand to the bigger result.
4480 int result = getFloatingTypeOrder(lhs, rhs);
4481 if (result > 0) // convert the rhs
4482 return lhs;
4483 assert(result < 0 && "illegal float comparison");
4484 return rhs; // convert the lhs
4485 }
4486 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4487 // Handle GCC complex int extension.
4488 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4489 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4490
4491 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004492 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004493 rhsComplexInt->getElementType()) >= 0)
4494 return lhs; // convert the rhs
4495 return rhs;
4496 } else if (lhsComplexInt && rhs->isIntegerType()) {
4497 // convert the rhs to the lhs complex type.
4498 return lhs;
4499 } else if (rhsComplexInt && lhs->isIntegerType()) {
4500 // convert the lhs to the rhs complex type.
4501 return rhs;
4502 }
4503 }
4504 // Finally, we have two differing integer types.
4505 // The rules for this case are in C99 6.3.1.8
4506 int compare = getIntegerTypeOrder(lhs, rhs);
4507 bool lhsSigned = lhs->isSignedIntegerType(),
4508 rhsSigned = rhs->isSignedIntegerType();
4509 QualType destType;
4510 if (lhsSigned == rhsSigned) {
4511 // Same signedness; use the higher-ranked type
4512 destType = compare >= 0 ? lhs : rhs;
4513 } else if (compare != (lhsSigned ? 1 : -1)) {
4514 // The unsigned type has greater than or equal rank to the
4515 // signed type, so use the unsigned type
4516 destType = lhsSigned ? rhs : lhs;
4517 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4518 // The two types are different widths; if we are here, that
4519 // means the signed type is larger than the unsigned type, so
4520 // use the signed type.
4521 destType = lhsSigned ? lhs : rhs;
4522 } else {
4523 // The signed type is higher-ranked than the unsigned type,
4524 // but isn't actually any bigger (like unsigned int and long
4525 // on most 32-bit systems). Use the unsigned type corresponding
4526 // to the signed type.
4527 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4528 }
4529 return destType;
4530}