blob: d96f5fbf1cb6be667ec0287239746b73339bb812 [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 Stump1eb44332009-09-09 15:08:12 +000043 sigjmp_bufDecl(0), SourceMgr(SM), LangOpts(LOpts),
44 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.
946DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) {
947 unsigned DataSize = TypeLoc::getFullDataSizeForType(T);
948 DeclaratorInfo *DInfo =
949 (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
950 new (DInfo) DeclaratorInfo(T);
951 return DInfo;
952}
953
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000954/// getInterfaceLayoutImpl - Get or compute information about the
955/// layout of the given interface.
956///
957/// \param Impl - If given, also include the layout of the interface's
958/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000959const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000960ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
961 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000962 assert(!D->isForwardDecl() && "Invalid interface decl!");
963
Devang Patel44a3dde2008-06-04 21:54:36 +0000964 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000965 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000966 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
967 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
968 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000969
Daniel Dunbar453addb2009-05-03 11:16:44 +0000970 // Add in synthesized ivar count if laying out an implementation.
971 if (Impl) {
Anders Carlsson29445a02009-07-18 21:19:52 +0000972 unsigned FieldCount = D->ivar_size();
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000973 unsigned SynthCount = CountSynthesizedIvars(D);
974 FieldCount += SynthCount;
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000975 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000976 // entry. Note we can't cache this because we simply free all
977 // entries later; however we shouldn't look up implementations
978 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000979 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000980 return getObjCLayout(D, 0);
981 }
982
Mike Stump1eb44332009-09-09 15:08:12 +0000983 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000984 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
985 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Devang Patel44a3dde2008-06-04 21:54:36 +0000987 return *NewEntry;
988}
989
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000990const ASTRecordLayout &
991ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
992 return getObjCLayout(D, 0);
993}
994
995const ASTRecordLayout &
996ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
997 return getObjCLayout(D->getClassInterface(), D);
998}
999
Devang Patel88a981b2007-11-01 19:11:01 +00001000/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +00001001/// specified record (struct/union/class), which indicates its size and field
1002/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +00001003const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001004 D = D->getDefinition(*this);
1005 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +00001006
Chris Lattner464175b2007-07-18 17:52:12 +00001007 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +00001008 // Note that we can't save a reference to the entry because this function
1009 // is recursive.
1010 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +00001011 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +00001012
Mike Stump1eb44332009-09-09 15:08:12 +00001013 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +00001014 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +00001015 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Chris Lattner5d2a6302007-07-18 18:26:58 +00001017 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001018}
1019
Chris Lattnera7674d82007-07-13 22:13:22 +00001020//===----------------------------------------------------------------------===//
1021// Type creation/memoization methods
1022//===----------------------------------------------------------------------===//
1023
John McCall0953e762009-09-24 19:53:00 +00001024QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1025 unsigned Fast = Quals.getFastQualifiers();
1026 Quals.removeFastQualifiers();
1027
1028 // Check if we've already instantiated this type.
1029 llvm::FoldingSetNodeID ID;
1030 ExtQuals::Profile(ID, TypeNode, Quals);
1031 void *InsertPos = 0;
1032 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1033 assert(EQ->getQualifiers() == Quals);
1034 QualType T = QualType(EQ, Fast);
1035 return T;
1036 }
1037
John McCall6b304a02009-09-24 23:30:46 +00001038 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001039 ExtQualNodes.InsertNode(New, InsertPos);
1040 QualType T = QualType(New, Fast);
1041 return T;
1042}
1043
1044QualType ASTContext::getVolatileType(QualType T) {
1045 QualType CanT = getCanonicalType(T);
1046 if (CanT.isVolatileQualified()) return T;
1047
1048 QualifierCollector Quals;
1049 const Type *TypeNode = Quals.strip(T);
1050 Quals.addVolatile();
1051
1052 return getExtQualType(TypeNode, Quals);
1053}
1054
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001055QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001056 QualType CanT = getCanonicalType(T);
1057 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001058 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001059
John McCall0953e762009-09-24 19:53:00 +00001060 // If we are composing extended qualifiers together, merge together
1061 // into one ExtQuals node.
1062 QualifierCollector Quals;
1063 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001064
John McCall0953e762009-09-24 19:53:00 +00001065 // If this type already has an address space specified, it cannot get
1066 // another one.
1067 assert(!Quals.hasAddressSpace() &&
1068 "Type cannot be in multiple addr spaces!");
1069 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001070
John McCall0953e762009-09-24 19:53:00 +00001071 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001072}
1073
Chris Lattnerb7d25532009-02-18 22:53:11 +00001074QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001075 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001076 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001077 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001078 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001080 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001081 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001082 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001083 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1084 return getPointerType(ResultType);
1085 }
1086 }
Mike Stump1eb44332009-09-09 15:08:12 +00001087
John McCall0953e762009-09-24 19:53:00 +00001088 // If we are composing extended qualifiers together, merge together
1089 // into one ExtQuals node.
1090 QualifierCollector Quals;
1091 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
John McCall0953e762009-09-24 19:53:00 +00001093 // If this type already has an ObjCGC specified, it cannot get
1094 // another one.
1095 assert(!Quals.hasObjCGCAttr() &&
1096 "Type cannot have multiple ObjCGCs!");
1097 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001098
John McCall0953e762009-09-24 19:53:00 +00001099 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001100}
Chris Lattnera7674d82007-07-13 22:13:22 +00001101
Mike Stump24556362009-07-25 21:26:53 +00001102QualType ASTContext::getNoReturnType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00001103 QualType ResultType;
Mike Stump24556362009-07-25 21:26:53 +00001104 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001105 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001106 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001107 ResultType = getPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001108 } else if (T->isBlockPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001109 QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001110 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001111 ResultType = getBlockPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001112 } else {
1113 assert (T->isFunctionType()
1114 && "can't noreturn qualify non-pointer to function or block type");
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Benjamin Kramerbdbeeb52009-09-25 11:47:22 +00001116 if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) {
1117 ResultType = getFunctionNoProtoType(FNPT->getResultType(), true);
John McCall0953e762009-09-24 19:53:00 +00001118 } else {
1119 const FunctionProtoType *F = T->getAs<FunctionProtoType>();
1120 ResultType
1121 = getFunctionType(F->getResultType(), F->arg_type_begin(),
1122 F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
1123 F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
1124 F->getNumExceptions(), F->exception_begin(), true);
1125 }
Mike Stump24556362009-07-25 21:26:53 +00001126 }
John McCall0953e762009-09-24 19:53:00 +00001127
1128 return getQualifiedType(ResultType, T.getQualifiers());
Mike Stump24556362009-07-25 21:26:53 +00001129}
1130
Reid Spencer5f016e22007-07-11 17:01:13 +00001131/// getComplexType - Return the uniqued reference to the type for a complex
1132/// number with the specified element type.
1133QualType ASTContext::getComplexType(QualType T) {
1134 // Unique pointers, to guarantee there is only one pointer of a particular
1135 // structure.
1136 llvm::FoldingSetNodeID ID;
1137 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 void *InsertPos = 0;
1140 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1141 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 // If the pointee type isn't canonical, this won't be a canonical type either,
1144 // so fill in the canonical type field.
1145 QualType Canonical;
1146 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001147 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 // Get the new insert position for the node we care about.
1150 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001151 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 }
John McCall6b304a02009-09-24 23:30:46 +00001153 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 Types.push_back(New);
1155 ComplexTypes.InsertNode(New, InsertPos);
1156 return QualType(New, 0);
1157}
1158
Eli Friedmanf98aba32009-02-13 02:31:07 +00001159QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1160 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1161 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1162 FixedWidthIntType *&Entry = Map[Width];
1163 if (!Entry)
1164 Entry = new FixedWidthIntType(Width, Signed);
1165 return QualType(Entry, 0);
1166}
Reid Spencer5f016e22007-07-11 17:01:13 +00001167
1168/// getPointerType - Return the uniqued reference to the type for a pointer to
1169/// the specified type.
1170QualType ASTContext::getPointerType(QualType T) {
1171 // Unique pointers, to guarantee there is only one pointer of a particular
1172 // structure.
1173 llvm::FoldingSetNodeID ID;
1174 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 void *InsertPos = 0;
1177 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1178 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 // If the pointee type isn't canonical, this won't be a canonical type either,
1181 // so fill in the canonical type field.
1182 QualType Canonical;
1183 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001184 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 // Get the new insert position for the node we care about.
1187 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001188 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 }
John McCall6b304a02009-09-24 23:30:46 +00001190 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 Types.push_back(New);
1192 PointerTypes.InsertNode(New, InsertPos);
1193 return QualType(New, 0);
1194}
1195
Mike Stump1eb44332009-09-09 15:08:12 +00001196/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001197/// a pointer to the specified block.
1198QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001199 assert(T->isFunctionType() && "block of function types only");
1200 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001201 // structure.
1202 llvm::FoldingSetNodeID ID;
1203 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Steve Naroff5618bd42008-08-27 16:04:49 +00001205 void *InsertPos = 0;
1206 if (BlockPointerType *PT =
1207 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1208 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
1210 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001211 // type either so fill in the canonical type field.
1212 QualType Canonical;
1213 if (!T->isCanonical()) {
1214 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Steve Naroff5618bd42008-08-27 16:04:49 +00001216 // Get the new insert position for the node we care about.
1217 BlockPointerType *NewIP =
1218 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001219 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001220 }
John McCall6b304a02009-09-24 23:30:46 +00001221 BlockPointerType *New
1222 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001223 Types.push_back(New);
1224 BlockPointerTypes.InsertNode(New, InsertPos);
1225 return QualType(New, 0);
1226}
1227
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001228/// getLValueReferenceType - Return the uniqued reference to the type for an
1229/// lvalue reference to the specified type.
1230QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 // Unique pointers, to guarantee there is only one pointer of a particular
1232 // structure.
1233 llvm::FoldingSetNodeID ID;
1234 ReferenceType::Profile(ID, T);
1235
1236 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001237 if (LValueReferenceType *RT =
1238 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001240
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 // If the referencee type isn't canonical, this won't be a canonical type
1242 // either, so fill in the canonical type field.
1243 QualType Canonical;
1244 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001245 Canonical = getLValueReferenceType(getCanonicalType(T));
1246
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001248 LValueReferenceType *NewIP =
1249 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001250 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 }
1252
John McCall6b304a02009-09-24 23:30:46 +00001253 LValueReferenceType *New
1254 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001256 LValueReferenceTypes.InsertNode(New, InsertPos);
1257 return QualType(New, 0);
1258}
1259
1260/// getRValueReferenceType - Return the uniqued reference to the type for an
1261/// rvalue reference to the specified type.
1262QualType ASTContext::getRValueReferenceType(QualType T) {
1263 // Unique pointers, to guarantee there is only one pointer of a particular
1264 // structure.
1265 llvm::FoldingSetNodeID ID;
1266 ReferenceType::Profile(ID, T);
1267
1268 void *InsertPos = 0;
1269 if (RValueReferenceType *RT =
1270 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1271 return QualType(RT, 0);
1272
1273 // If the referencee type isn't canonical, this won't be a canonical type
1274 // either, so fill in the canonical type field.
1275 QualType Canonical;
1276 if (!T->isCanonical()) {
1277 Canonical = getRValueReferenceType(getCanonicalType(T));
1278
1279 // Get the new insert position for the node we care about.
1280 RValueReferenceType *NewIP =
1281 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1282 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1283 }
1284
John McCall6b304a02009-09-24 23:30:46 +00001285 RValueReferenceType *New
1286 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001287 Types.push_back(New);
1288 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 return QualType(New, 0);
1290}
1291
Sebastian Redlf30208a2009-01-24 21:16:55 +00001292/// getMemberPointerType - Return the uniqued reference to the type for a
1293/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001294QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001295 // Unique pointers, to guarantee there is only one pointer of a particular
1296 // structure.
1297 llvm::FoldingSetNodeID ID;
1298 MemberPointerType::Profile(ID, T, Cls);
1299
1300 void *InsertPos = 0;
1301 if (MemberPointerType *PT =
1302 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1303 return QualType(PT, 0);
1304
1305 // If the pointee or class type isn't canonical, this won't be a canonical
1306 // type either, so fill in the canonical type field.
1307 QualType Canonical;
1308 if (!T->isCanonical()) {
1309 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1310
1311 // Get the new insert position for the node we care about.
1312 MemberPointerType *NewIP =
1313 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1314 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1315 }
John McCall6b304a02009-09-24 23:30:46 +00001316 MemberPointerType *New
1317 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001318 Types.push_back(New);
1319 MemberPointerTypes.InsertNode(New, InsertPos);
1320 return QualType(New, 0);
1321}
1322
Mike Stump1eb44332009-09-09 15:08:12 +00001323/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001324/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001325QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001326 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001327 ArrayType::ArraySizeModifier ASM,
1328 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001329 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1330 "Constant array of VLAs is illegal!");
1331
Chris Lattner38aeec72009-05-13 04:12:56 +00001332 // Convert the array size into a canonical width matching the pointer size for
1333 // the target.
1334 llvm::APInt ArySize(ArySizeIn);
1335 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001338 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001341 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001342 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 // If the element type isn't canonical, this won't be a canonical type either,
1346 // so fill in the canonical type field.
1347 QualType Canonical;
1348 if (!EltTy->isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001349 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001350 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001352 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001353 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001354 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 }
Mike Stump1eb44332009-09-09 15:08:12 +00001356
John McCall6b304a02009-09-24 23:30:46 +00001357 ConstantArrayType *New = new(*this,TypeAlignment)
1358 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001359 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001360 Types.push_back(New);
1361 return QualType(New, 0);
1362}
1363
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001364/// getVariableArrayType - Returns a non-unique reference to the type for a
1365/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001366QualType ASTContext::getVariableArrayType(QualType EltTy,
1367 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001368 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001369 unsigned EltTypeQuals,
1370 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001371 // Since we don't unique expressions, it isn't possible to unique VLA's
1372 // that have an expression provided for their size.
1373
John McCall6b304a02009-09-24 23:30:46 +00001374 VariableArrayType *New = new(*this, TypeAlignment)
1375 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001376
1377 VariableArrayTypes.push_back(New);
1378 Types.push_back(New);
1379 return QualType(New, 0);
1380}
1381
Douglas Gregor898574e2008-12-05 23:32:09 +00001382/// getDependentSizedArrayType - Returns a non-unique reference to
1383/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001384/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001385QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1386 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001387 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001388 unsigned EltTypeQuals,
1389 SourceRange Brackets) {
Mike Stump1eb44332009-09-09 15:08:12 +00001390 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001391 "Size must be type- or value-dependent!");
1392
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001393 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001394 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001395 EltTypeQuals, NumElts);
Douglas Gregor898574e2008-12-05 23:32:09 +00001396
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001397 void *InsertPos = 0;
1398 DependentSizedArrayType *Canon
1399 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1400 DependentSizedArrayType *New;
1401 if (Canon) {
1402 // We already have a canonical version of this array type; use it as
1403 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001404 New = new (*this, TypeAlignment)
1405 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1406 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001407 } else {
1408 QualType CanonEltTy = getCanonicalType(EltTy);
1409 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001410 New = new (*this, TypeAlignment)
1411 DependentSizedArrayType(*this, EltTy, QualType(),
1412 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001413 DependentSizedArrayTypes.InsertNode(New, InsertPos);
1414 } else {
1415 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1416 ASM, EltTypeQuals,
1417 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001418 New = new (*this, TypeAlignment)
1419 DependentSizedArrayType(*this, EltTy, Canon,
1420 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001421 }
1422 }
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Douglas Gregor898574e2008-12-05 23:32:09 +00001424 Types.push_back(New);
1425 return QualType(New, 0);
1426}
1427
Eli Friedmanc5773c42008-02-15 18:16:39 +00001428QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1429 ArrayType::ArraySizeModifier ASM,
1430 unsigned EltTypeQuals) {
1431 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001432 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001433
1434 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001435 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001436 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1437 return QualType(ATP, 0);
1438
1439 // If the element type isn't canonical, this won't be a canonical type
1440 // either, so fill in the canonical type field.
1441 QualType Canonical;
1442
1443 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001444 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001445 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001446
1447 // Get the new insert position for the node we care about.
1448 IncompleteArrayType *NewIP =
1449 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001450 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001451 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001452
John McCall6b304a02009-09-24 23:30:46 +00001453 IncompleteArrayType *New = new (*this, TypeAlignment)
1454 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001455
1456 IncompleteArrayTypes.InsertNode(New, InsertPos);
1457 Types.push_back(New);
1458 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001459}
1460
Steve Naroff73322922007-07-18 18:00:27 +00001461/// getVectorType - Return the unique reference to a vector type of
1462/// the specified element type and size. VectorType must be a built-in type.
1463QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001464 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Chris Lattnerf52ab252008-04-06 22:59:24 +00001466 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001467 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Reid Spencer5f016e22007-07-11 17:01:13 +00001469 // Check if we've already instantiated a vector of this type.
1470 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001471 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001472 void *InsertPos = 0;
1473 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1474 return QualType(VTP, 0);
1475
1476 // If the element type isn't canonical, this won't be a canonical type either,
1477 // so fill in the canonical type field.
1478 QualType Canonical;
1479 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001480 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Reid Spencer5f016e22007-07-11 17:01:13 +00001482 // Get the new insert position for the node we care about.
1483 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001484 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001485 }
John McCall6b304a02009-09-24 23:30:46 +00001486 VectorType *New = new (*this, TypeAlignment)
1487 VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 VectorTypes.InsertNode(New, InsertPos);
1489 Types.push_back(New);
1490 return QualType(New, 0);
1491}
1492
Nate Begeman213541a2008-04-18 23:10:10 +00001493/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001494/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001495QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001496 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Chris Lattnerf52ab252008-04-06 22:59:24 +00001498 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001499 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Steve Naroff73322922007-07-18 18:00:27 +00001501 // Check if we've already instantiated a vector of this type.
1502 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001503 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001504 void *InsertPos = 0;
1505 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1506 return QualType(VTP, 0);
1507
1508 // If the element type isn't canonical, this won't be a canonical type either,
1509 // so fill in the canonical type field.
1510 QualType Canonical;
1511 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001512 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Steve Naroff73322922007-07-18 18:00:27 +00001514 // Get the new insert position for the node we care about.
1515 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001516 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001517 }
John McCall6b304a02009-09-24 23:30:46 +00001518 ExtVectorType *New = new (*this, TypeAlignment)
1519 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001520 VectorTypes.InsertNode(New, InsertPos);
1521 Types.push_back(New);
1522 return QualType(New, 0);
1523}
1524
Mike Stump1eb44332009-09-09 15:08:12 +00001525QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001526 Expr *SizeExpr,
1527 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001528 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001529 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001530 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001532 void *InsertPos = 0;
1533 DependentSizedExtVectorType *Canon
1534 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1535 DependentSizedExtVectorType *New;
1536 if (Canon) {
1537 // We already have a canonical version of this array type; use it as
1538 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001539 New = new (*this, TypeAlignment)
1540 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1541 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001542 } else {
1543 QualType CanonVecTy = getCanonicalType(vecType);
1544 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001545 New = new (*this, TypeAlignment)
1546 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1547 AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001548 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1549 } else {
1550 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1551 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001552 New = new (*this, TypeAlignment)
1553 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001554 }
1555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001557 Types.push_back(New);
1558 return QualType(New, 0);
1559}
1560
Douglas Gregor72564e72009-02-26 23:50:07 +00001561/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001562///
Mike Stump24556362009-07-25 21:26:53 +00001563QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 // Unique functions, to guarantee there is only one function of a particular
1565 // structure.
1566 llvm::FoldingSetNodeID ID;
Mike Stump24556362009-07-25 21:26:53 +00001567 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Reid Spencer5f016e22007-07-11 17:01:13 +00001569 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001570 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001571 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001572 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Reid Spencer5f016e22007-07-11 17:01:13 +00001574 QualType Canonical;
1575 if (!ResultTy->isCanonical()) {
Mike Stump24556362009-07-25 21:26:53 +00001576 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001579 FunctionNoProtoType *NewIP =
1580 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001581 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
John McCall6b304a02009-09-24 23:30:46 +00001584 FunctionNoProtoType *New = new (*this, TypeAlignment)
1585 FunctionNoProtoType(ResultTy, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001586 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001587 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001588 return QualType(New, 0);
1589}
1590
1591/// getFunctionType - Return a normal function type with a typed argument
1592/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001593QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001594 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001595 unsigned TypeQuals, bool hasExceptionSpec,
1596 bool hasAnyExceptionSpec, unsigned NumExs,
Mike Stump24556362009-07-25 21:26:53 +00001597 const QualType *ExArray, bool NoReturn) {
Anders Carlsson83913e32009-09-16 23:47:08 +00001598 if (LangOpts.CPlusPlus) {
1599 for (unsigned i = 0; i != NumArgs; ++i)
John McCall0953e762009-09-24 19:53:00 +00001600 assert(!ArgArray[i].hasQualifiers() &&
1601 "C++ arguments can't have toplevel qualifiers!");
Anders Carlsson83913e32009-09-16 23:47:08 +00001602 }
1603
Reid Spencer5f016e22007-07-11 17:01:13 +00001604 // Unique functions, to guarantee there is only one function of a particular
1605 // structure.
1606 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001607 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001608 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001609 NumExs, ExArray, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001610
1611 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001612 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001613 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001615
1616 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001617 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001618 if (hasExceptionSpec)
1619 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1621 if (!ArgArray[i]->isCanonical())
1622 isCanonical = false;
1623
1624 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001625 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001626 QualType Canonical;
1627 if (!isCanonical) {
1628 llvm::SmallVector<QualType, 16> CanonicalArgs;
1629 CanonicalArgs.reserve(NumArgs);
1630 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001631 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001632
Chris Lattnerf52ab252008-04-06 22:59:24 +00001633 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001634 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001635 isVariadic, TypeQuals, false,
1636 false, 0, 0, NoReturn);
Sebastian Redl465226e2009-05-27 22:11:52 +00001637
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001639 FunctionProtoType *NewIP =
1640 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001641 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001642 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001643
Douglas Gregor72564e72009-02-26 23:50:07 +00001644 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001645 // for two variable size arrays (for parameter and exception types) at the
1646 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001647 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001648 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1649 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001650 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001651 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001652 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001653 ExArray, NumExs, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001654 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001655 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 return QualType(FTP, 0);
1657}
1658
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001659/// getTypeDeclType - Return the unique reference to the type for the
1660/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001661QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001662 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001663 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001665 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001666 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001667 else if (isa<TemplateTypeParmDecl>(Decl)) {
1668 assert(false && "Template type parameter types are always available.");
Mike Stump9fdbab32009-07-31 02:02:20 +00001669 } else if (ObjCInterfaceDecl *ObjCInterface
1670 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001671 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001672
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001673 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001674 if (PrevDecl)
1675 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001676 else
John McCall6b304a02009-09-24 23:30:46 +00001677 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
Mike Stump9fdbab32009-07-31 02:02:20 +00001678 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001679 if (PrevDecl)
1680 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001681 else
John McCall6b304a02009-09-24 23:30:46 +00001682 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
Mike Stump9fdbab32009-07-31 02:02:20 +00001683 } else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001684 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001685
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001686 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001687 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001688}
1689
Reid Spencer5f016e22007-07-11 17:01:13 +00001690/// getTypedefType - Return the unique reference to the type for the
1691/// specified typename decl.
1692QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1693 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Chris Lattnerf52ab252008-04-06 22:59:24 +00001695 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001696 Decl->TypeForDecl = new(*this, TypeAlignment)
1697 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 Types.push_back(Decl->TypeForDecl);
1699 return QualType(Decl->TypeForDecl, 0);
1700}
1701
John McCall49a832b2009-10-18 09:09:24 +00001702/// \brief Retrieve a substitution-result type.
1703QualType
1704ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1705 QualType Replacement) {
1706 assert(Replacement->isCanonical()
1707 && "replacement types must always be canonical");
1708
1709 llvm::FoldingSetNodeID ID;
1710 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1711 void *InsertPos = 0;
1712 SubstTemplateTypeParmType *SubstParm
1713 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1714
1715 if (!SubstParm) {
1716 SubstParm = new (*this, TypeAlignment)
1717 SubstTemplateTypeParmType(Parm, Replacement);
1718 Types.push_back(SubstParm);
1719 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1720 }
1721
1722 return QualType(SubstParm, 0);
1723}
1724
Douglas Gregorfab9d672009-02-05 23:33:38 +00001725/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001726/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001727/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001728QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001729 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001730 IdentifierInfo *Name) {
1731 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001732 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001733 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001734 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001735 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1736
1737 if (TypeParm)
1738 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001740 if (Name) {
1741 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall6b304a02009-09-24 23:30:46 +00001742 TypeParm = new (*this, TypeAlignment)
1743 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001744 } else
John McCall6b304a02009-09-24 23:30:46 +00001745 TypeParm = new (*this, TypeAlignment)
1746 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001747
1748 Types.push_back(TypeParm);
1749 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1750
1751 return QualType(TypeParm, 0);
1752}
1753
Mike Stump1eb44332009-09-09 15:08:12 +00001754QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001755ASTContext::getTemplateSpecializationType(TemplateName Template,
1756 const TemplateArgument *Args,
1757 unsigned NumArgs,
1758 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001759 if (!Canon.isNull())
1760 Canon = getCanonicalType(Canon);
1761 else {
1762 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001763 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1764 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1765 CanonArgs.reserve(NumArgs);
1766 for (unsigned I = 0; I != NumArgs; ++I)
1767 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1768
1769 // Determine whether this canonical template specialization type already
1770 // exists.
1771 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001772 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001773 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001774
1775 void *InsertPos = 0;
1776 TemplateSpecializationType *Spec
1777 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Douglas Gregor1275ae02009-07-28 23:00:59 +00001779 if (!Spec) {
1780 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001781 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001782 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001783 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001784 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001785 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001786 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001787 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001788 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001789 }
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Douglas Gregorb88e8882009-07-30 17:40:51 +00001791 if (Canon.isNull())
1792 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001793 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001794 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001795 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001796
Douglas Gregor1275ae02009-07-28 23:00:59 +00001797 // Allocate the (non-canonical) template specialization type, but don't
1798 // try to unique it: these types typically have location information that
1799 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001800 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001801 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001802 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001803 TemplateSpecializationType *Spec
1804 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001805 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Douglas Gregor55f6b142009-02-09 18:46:07 +00001807 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001808 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001809}
1810
Mike Stump1eb44332009-09-09 15:08:12 +00001811QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001812ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001813 QualType NamedType) {
1814 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001815 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001816
1817 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001818 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001819 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1820 if (T)
1821 return QualType(T, 0);
1822
Mike Stump1eb44332009-09-09 15:08:12 +00001823 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001824 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001825 Types.push_back(T);
1826 QualifiedNameTypes.InsertNode(T, InsertPos);
1827 return QualType(T, 0);
1828}
1829
Mike Stump1eb44332009-09-09 15:08:12 +00001830QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001831 const IdentifierInfo *Name,
1832 QualType Canon) {
1833 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1834
1835 if (Canon.isNull()) {
1836 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1837 if (CanonNNS != NNS)
1838 Canon = getTypenameType(CanonNNS, Name);
1839 }
1840
1841 llvm::FoldingSetNodeID ID;
1842 TypenameType::Profile(ID, NNS, Name);
1843
1844 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001845 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001846 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1847 if (T)
1848 return QualType(T, 0);
1849
1850 T = new (*this) TypenameType(NNS, Name, Canon);
1851 Types.push_back(T);
1852 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001853 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001854}
1855
Mike Stump1eb44332009-09-09 15:08:12 +00001856QualType
1857ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001858 const TemplateSpecializationType *TemplateId,
1859 QualType Canon) {
1860 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1861
1862 if (Canon.isNull()) {
1863 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1864 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1865 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1866 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00001867 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00001868 assert(CanonTemplateId &&
1869 "Canonical type must also be a template specialization type");
1870 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1871 }
1872 }
1873
1874 llvm::FoldingSetNodeID ID;
1875 TypenameType::Profile(ID, NNS, TemplateId);
1876
1877 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001878 TypenameType *T
Douglas Gregor17343172009-04-01 00:28:59 +00001879 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1880 if (T)
1881 return QualType(T, 0);
1882
1883 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1884 Types.push_back(T);
1885 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001886 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00001887}
1888
John McCall7da24312009-09-05 00:15:47 +00001889QualType
1890ASTContext::getElaboratedType(QualType UnderlyingType,
1891 ElaboratedType::TagKind Tag) {
1892 llvm::FoldingSetNodeID ID;
1893 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001894
John McCall7da24312009-09-05 00:15:47 +00001895 void *InsertPos = 0;
1896 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1897 if (T)
1898 return QualType(T, 0);
1899
1900 QualType Canon = getCanonicalType(UnderlyingType);
1901
1902 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1903 Types.push_back(T);
1904 ElaboratedTypes.InsertNode(T, InsertPos);
1905 return QualType(T, 0);
1906}
1907
Chris Lattner88cb27a2008-04-07 04:56:42 +00001908/// CmpProtocolNames - Comparison predicate for sorting protocols
1909/// alphabetically.
1910static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1911 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001912 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001913}
1914
1915static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1916 unsigned &NumProtocols) {
1917 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Chris Lattner88cb27a2008-04-07 04:56:42 +00001919 // Sort protocols, keyed by name.
1920 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1921
1922 // Remove duplicates.
1923 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1924 NumProtocols = ProtocolsEnd-Protocols;
1925}
1926
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001927/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1928/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00001929QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00001930 ObjCProtocolDecl **Protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001931 unsigned NumProtocols) {
1932 // Sort the protocol list alphabetically to canonicalize it.
1933 if (NumProtocols)
1934 SortAndUniqueProtocols(Protocols, NumProtocols);
1935
1936 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00001937 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001938
1939 void *InsertPos = 0;
1940 if (ObjCObjectPointerType *QT =
1941 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1942 return QualType(QT, 0);
1943
1944 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001945 ObjCObjectPointerType *QType = new (*this, TypeAlignment)
1946 ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001948 Types.push_back(QType);
1949 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1950 return QualType(QType, 0);
1951}
Chris Lattner88cb27a2008-04-07 04:56:42 +00001952
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001953/// getObjCInterfaceType - Return the unique reference to the type for the
1954/// specified ObjC interface decl. The list of protocols is optional.
1955QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001956 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Mike Stump1eb44332009-09-09 15:08:12 +00001957 if (NumProtocols)
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001958 // Sort the protocol list alphabetically to canonicalize it.
1959 SortAndUniqueProtocols(Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001960
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001961 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001962 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001964 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001965 if (ObjCInterfaceType *QT =
1966 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001967 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001969 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001970 ObjCInterfaceType *QType = new (*this, TypeAlignment)
1971 ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1972 Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001973 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001974 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001975 return QualType(QType, 0);
1976}
1977
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00001978QualType ASTContext::getObjCProtocolListType(QualType T,
1979 ObjCProtocolDecl **Protocols,
1980 unsigned NumProtocols) {
1981 llvm::FoldingSetNodeID ID;
1982 ObjCProtocolListType::Profile(ID, T, Protocols, NumProtocols);
1983
1984 void *InsertPos = 0;
1985 if (ObjCProtocolListType *QT =
1986 ObjCProtocolListTypes.FindNodeOrInsertPos(ID, InsertPos))
1987 return QualType(QT, 0);
1988
1989 // No Match;
1990 ObjCProtocolListType *QType = new (*this, TypeAlignment)
1991 ObjCProtocolListType(T, Protocols, NumProtocols);
1992 Types.push_back(QType);
1993 ObjCProtocolListTypes.InsertNode(QType, InsertPos);
1994 return QualType(QType, 0);
1995}
1996
Douglas Gregor72564e72009-02-26 23:50:07 +00001997/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1998/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001999/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002000/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002001/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002002QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002003 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002004 if (tofExpr->isTypeDependent()) {
2005 llvm::FoldingSetNodeID ID;
2006 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Douglas Gregorb1975722009-07-30 23:18:24 +00002008 void *InsertPos = 0;
2009 DependentTypeOfExprType *Canon
2010 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2011 if (Canon) {
2012 // We already have a "canonical" version of an identical, dependent
2013 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002014 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002015 QualType((TypeOfExprType*)Canon, 0));
2016 }
2017 else {
2018 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002019 Canon
2020 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002021 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2022 toe = Canon;
2023 }
2024 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002025 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002026 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002027 }
Steve Naroff9752f252007-08-01 18:02:17 +00002028 Types.push_back(toe);
2029 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002030}
2031
Steve Naroff9752f252007-08-01 18:02:17 +00002032/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2033/// TypeOfType AST's. The only motivation to unique these nodes would be
2034/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002035/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002036/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002037QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002038 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002039 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002040 Types.push_back(tot);
2041 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002042}
2043
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002044/// getDecltypeForExpr - Given an expr, will return the decltype for that
2045/// expression, according to the rules in C++0x [dcl.type.simple]p4
2046static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002047 if (e->isTypeDependent())
2048 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002050 // If e is an id expression or a class member access, decltype(e) is defined
2051 // as the type of the entity named by e.
2052 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2053 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2054 return VD->getType();
2055 }
2056 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2057 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2058 return FD->getType();
2059 }
2060 // If e is a function call or an invocation of an overloaded operator,
2061 // (parentheses around e are ignored), decltype(e) is defined as the
2062 // return type of that function.
2063 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2064 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002066 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002067
2068 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002069 // defined as T&, otherwise decltype(e) is defined as T.
2070 if (e->isLvalue(Context) == Expr::LV_Valid)
2071 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002073 return T;
2074}
2075
Anders Carlsson395b4752009-06-24 19:06:50 +00002076/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2077/// DecltypeType AST's. The only motivation to unique these nodes would be
2078/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002079/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002080/// on canonical type's (which are always unique).
2081QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002082 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002083 if (e->isTypeDependent()) {
2084 llvm::FoldingSetNodeID ID;
2085 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002086
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002087 void *InsertPos = 0;
2088 DependentDecltypeType *Canon
2089 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2090 if (Canon) {
2091 // We already have a "canonical" version of an equivalent, dependent
2092 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002093 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002094 QualType((DecltypeType*)Canon, 0));
2095 }
2096 else {
2097 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002098 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002099 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2100 dt = Canon;
2101 }
2102 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002103 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002104 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002105 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002106 Types.push_back(dt);
2107 return QualType(dt, 0);
2108}
2109
Reid Spencer5f016e22007-07-11 17:01:13 +00002110/// getTagDeclType - Return the unique reference to the type for the
2111/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002112QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002113 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002114 // FIXME: What is the design on getTagDeclType when it requires casting
2115 // away const? mutable?
2116 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002117}
2118
Mike Stump1eb44332009-09-09 15:08:12 +00002119/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2120/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2121/// needs to agree with the definition in <stddef.h>.
Reid Spencer5f016e22007-07-11 17:01:13 +00002122QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002123 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002124}
2125
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002126/// getSignedWCharType - Return the type of "signed wchar_t".
2127/// Used when in C++, as a GCC extension.
2128QualType ASTContext::getSignedWCharType() const {
2129 // FIXME: derive from "Target" ?
2130 return WCharTy;
2131}
2132
2133/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2134/// Used when in C++, as a GCC extension.
2135QualType ASTContext::getUnsignedWCharType() const {
2136 // FIXME: derive from "Target" ?
2137 return UnsignedIntTy;
2138}
2139
Chris Lattner8b9023b2007-07-13 03:05:23 +00002140/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2141/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2142QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002143 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002144}
2145
Chris Lattnere6327742008-04-02 05:18:44 +00002146//===----------------------------------------------------------------------===//
2147// Type Operators
2148//===----------------------------------------------------------------------===//
2149
Chris Lattner77c96472008-04-06 22:41:35 +00002150/// getCanonicalType - Return the canonical (structural) type corresponding to
2151/// the specified potentially non-canonical type. The non-canonical version
2152/// of a type may have many "decorated" versions of types. Decorators can
2153/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2154/// to be free of any of these, allowing two canonical types to be compared
2155/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002156CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002157 QualifierCollector Quals;
2158 const Type *Ptr = Quals.strip(T);
2159 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002160
John McCall0953e762009-09-24 19:53:00 +00002161 // The canonical internal type will be the canonical type *except*
2162 // that we push type qualifiers down through array types.
2163
2164 // If there are no new qualifiers to push down, stop here.
2165 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002166 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002167
John McCall0953e762009-09-24 19:53:00 +00002168 // If the type qualifiers are on an array type, get the canonical
2169 // type of the array with the qualifiers applied to the element
2170 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002171 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2172 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002173 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002174
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002175 // Get the canonical version of the element with the extra qualifiers on it.
2176 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002177 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002178 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002179
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002180 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002181 return CanQualType::CreateUnsafe(
2182 getConstantArrayType(NewEltTy, CAT->getSize(),
2183 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002184 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002185 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002186 return CanQualType::CreateUnsafe(
2187 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002188 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002189
Douglas Gregor898574e2008-12-05 23:32:09 +00002190 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002191 return CanQualType::CreateUnsafe(
2192 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002193 DSAT->getSizeExpr() ?
2194 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002195 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002196 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002197 DSAT->getBracketsRange()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002198
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002199 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002200 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002201 VAT->getSizeExpr() ?
2202 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002203 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002204 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002205 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002206}
2207
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002208TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2209 // If this template name refers to a template, the canonical
2210 // template name merely stores the template itself.
2211 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002212 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002213
Mike Stump1eb44332009-09-09 15:08:12 +00002214 // If this template name refers to a set of overloaded function templates,
Douglas Gregord99cbe62009-07-29 18:26:50 +00002215 /// the canonical template name merely stores the set of function templates.
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002216 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2217 OverloadedFunctionDecl *CanonOvl = 0;
2218 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2219 FEnd = Ovl->function_end();
2220 F != FEnd; ++F) {
2221 Decl *Canon = F->get()->getCanonicalDecl();
2222 if (CanonOvl || Canon != F->get()) {
2223 if (!CanonOvl)
Mike Stump1eb44332009-09-09 15:08:12 +00002224 CanonOvl = OverloadedFunctionDecl::Create(*this,
2225 Ovl->getDeclContext(),
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002226 Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00002227
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002228 CanonOvl->addOverload(
2229 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2230 }
2231 }
Mike Stump1eb44332009-09-09 15:08:12 +00002232
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002233 return TemplateName(CanonOvl? CanonOvl : Ovl);
2234 }
Mike Stump1eb44332009-09-09 15:08:12 +00002235
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002236 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2237 assert(DTN && "Non-dependent template names must refer to template decls.");
2238 return DTN->CanonicalTemplateName;
2239}
2240
Mike Stump1eb44332009-09-09 15:08:12 +00002241TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002242ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2243 switch (Arg.getKind()) {
2244 case TemplateArgument::Null:
2245 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Douglas Gregor1275ae02009-07-28 23:00:59 +00002247 case TemplateArgument::Expression:
2248 // FIXME: Build canonical expression?
2249 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002250
Douglas Gregor1275ae02009-07-28 23:00:59 +00002251 case TemplateArgument::Declaration:
2252 return TemplateArgument(SourceLocation(),
2253 Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002254
Douglas Gregor1275ae02009-07-28 23:00:59 +00002255 case TemplateArgument::Integral:
2256 return TemplateArgument(SourceLocation(),
2257 *Arg.getAsIntegral(),
2258 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002259
Douglas Gregor1275ae02009-07-28 23:00:59 +00002260 case TemplateArgument::Type:
2261 return TemplateArgument(SourceLocation(),
2262 getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002263
Douglas Gregor1275ae02009-07-28 23:00:59 +00002264 case TemplateArgument::Pack: {
2265 // FIXME: Allocate in ASTContext
2266 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2267 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002268 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002269 AEnd = Arg.pack_end();
2270 A != AEnd; (void)++A, ++Idx)
2271 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002272
Douglas Gregor1275ae02009-07-28 23:00:59 +00002273 TemplateArgument Result;
2274 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2275 return Result;
2276 }
2277 }
2278
2279 // Silence GCC warning
2280 assert(false && "Unhandled template argument kind");
2281 return TemplateArgument();
2282}
2283
Douglas Gregord57959a2009-03-27 23:10:48 +00002284NestedNameSpecifier *
2285ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002286 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002287 return 0;
2288
2289 switch (NNS->getKind()) {
2290 case NestedNameSpecifier::Identifier:
2291 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002292 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002293 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2294 NNS->getAsIdentifier());
2295
2296 case NestedNameSpecifier::Namespace:
2297 // A namespace is canonical; build a nested-name-specifier with
2298 // this namespace and no prefix.
2299 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2300
2301 case NestedNameSpecifier::TypeSpec:
2302 case NestedNameSpecifier::TypeSpecWithTemplate: {
2303 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002304 return NestedNameSpecifier::Create(*this, 0,
2305 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002306 T.getTypePtr());
2307 }
2308
2309 case NestedNameSpecifier::Global:
2310 // The global specifier is canonical and unique.
2311 return NNS;
2312 }
2313
2314 // Required to silence a GCC warning
2315 return 0;
2316}
2317
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002318
2319const ArrayType *ASTContext::getAsArrayType(QualType T) {
2320 // Handle the non-qualified case efficiently.
John McCall0953e762009-09-24 19:53:00 +00002321 if (!T.hasQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002322 // Handle the common positive case fast.
2323 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2324 return AT;
2325 }
Mike Stump1eb44332009-09-09 15:08:12 +00002326
John McCall0953e762009-09-24 19:53:00 +00002327 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002328 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002329 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002330 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002331
John McCall0953e762009-09-24 19:53:00 +00002332 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002333 // implements C99 6.7.3p8: "If the specification of an array type includes
2334 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002335
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002336 // If we get here, we either have type qualifiers on the type, or we have
2337 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002338 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002339
John McCall0953e762009-09-24 19:53:00 +00002340 QualifierCollector Qs;
2341 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002343 // If we have a simple case, just return now.
2344 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002345 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002346 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002347
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002348 // Otherwise, we have an array and we have qualifiers on it. Push the
2349 // qualifiers into the array element type and return a new array type.
2350 // Get the canonical version of the element with the extra qualifiers on it.
2351 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002352 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002354 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2355 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2356 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002357 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002358 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2359 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2360 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002361 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002362
Mike Stump1eb44332009-09-09 15:08:12 +00002363 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002364 = dyn_cast<DependentSizedArrayType>(ATy))
2365 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002366 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002367 DSAT->getSizeExpr() ?
2368 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002369 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002370 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002371 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002373 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002374 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002375 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002376 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002377 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002378 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002379 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002380}
2381
2382
Chris Lattnere6327742008-04-02 05:18:44 +00002383/// getArrayDecayedType - Return the properly qualified result of decaying the
2384/// specified array type to a pointer. This operation is non-trivial when
2385/// handling typedefs etc. The canonical type of "T" must be an array type,
2386/// this returns a pointer to a properly qualified element of the array.
2387///
2388/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2389QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002390 // Get the element type with 'getAsArrayType' so that we don't lose any
2391 // typedefs in the element type of the array. This also handles propagation
2392 // of type qualifiers from the array type into the element type if present
2393 // (C99 6.7.3p8).
2394 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2395 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002397 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002398
2399 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002400 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002401}
2402
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002403QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002404 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002405 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002406 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002407 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2408 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002409 } else {
John McCall0953e762009-09-24 19:53:00 +00002410 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002411 }
2412 }
2413}
2414
Anders Carlssonfbbce492009-09-25 01:23:32 +00002415QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2416 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002417
Anders Carlssonfbbce492009-09-25 01:23:32 +00002418 if (const ArrayType *AT = getAsArrayType(ElemTy))
2419 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002420
Anders Carlsson6183a992008-12-21 03:44:36 +00002421 return ElemTy;
2422}
2423
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002424/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002425uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002426ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2427 uint64_t ElementCount = 1;
2428 do {
2429 ElementCount *= CA->getSize().getZExtValue();
2430 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2431 } while (CA);
2432 return ElementCount;
2433}
2434
Reid Spencer5f016e22007-07-11 17:01:13 +00002435/// getFloatingRank - Return a relative rank for floating point types.
2436/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002437static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002438 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002439 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002440
John McCall183700f2009-09-21 23:43:11 +00002441 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2442 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002443 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002444 case BuiltinType::Float: return FloatRank;
2445 case BuiltinType::Double: return DoubleRank;
2446 case BuiltinType::LongDouble: return LongDoubleRank;
2447 }
2448}
2449
Mike Stump1eb44332009-09-09 15:08:12 +00002450/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2451/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002452/// 'typeDomain' is a real floating point or complex type.
2453/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002454QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2455 QualType Domain) const {
2456 FloatingRank EltRank = getFloatingRank(Size);
2457 if (Domain->isComplexType()) {
2458 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002459 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002460 case FloatRank: return FloatComplexTy;
2461 case DoubleRank: return DoubleComplexTy;
2462 case LongDoubleRank: return LongDoubleComplexTy;
2463 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002464 }
Chris Lattner1361b112008-04-06 23:58:54 +00002465
2466 assert(Domain->isRealFloatingType() && "Unknown domain!");
2467 switch (EltRank) {
2468 default: assert(0 && "getFloatingRank(): illegal value for rank");
2469 case FloatRank: return FloatTy;
2470 case DoubleRank: return DoubleTy;
2471 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002472 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002473}
2474
Chris Lattner7cfeb082008-04-06 23:55:33 +00002475/// getFloatingTypeOrder - Compare the rank of the two specified floating
2476/// point types, ignoring the domain of the type (i.e. 'double' ==
2477/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002478/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002479int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2480 FloatingRank LHSR = getFloatingRank(LHS);
2481 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002482
Chris Lattnera75cea32008-04-06 23:38:49 +00002483 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002484 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002485 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002486 return 1;
2487 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002488}
2489
Chris Lattnerf52ab252008-04-06 22:59:24 +00002490/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2491/// routine will assert if passed a built-in type that isn't an integer or enum,
2492/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002493unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002494 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002495 if (EnumType* ET = dyn_cast<EnumType>(T))
2496 T = ET->getDecl()->getIntegerType().getTypePtr();
2497
Eli Friedmana3426752009-07-05 23:44:27 +00002498 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2499 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2500
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002501 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2502 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2503
2504 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2505 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2506
Eli Friedmanf98aba32009-02-13 02:31:07 +00002507 // There are two things which impact the integer rank: the width, and
2508 // the ordering of builtins. The builtin ordering is encoded in the
2509 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner1b63e4f2009-06-14 01:54:56 +00002510 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanf98aba32009-02-13 02:31:07 +00002511 return FWIT->getWidth() << 3;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002512
Chris Lattnerf52ab252008-04-06 22:59:24 +00002513 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002514 default: assert(0 && "getIntegerRank(): not a built-in integer");
2515 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002516 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002517 case BuiltinType::Char_S:
2518 case BuiltinType::Char_U:
2519 case BuiltinType::SChar:
2520 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002521 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002522 case BuiltinType::Short:
2523 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002524 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002525 case BuiltinType::Int:
2526 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002527 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002528 case BuiltinType::Long:
2529 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002530 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002531 case BuiltinType::LongLong:
2532 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002533 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002534 case BuiltinType::Int128:
2535 case BuiltinType::UInt128:
2536 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002537 }
2538}
2539
Eli Friedman04e83572009-08-20 04:21:42 +00002540/// \brief Whether this is a promotable bitfield reference according
2541/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2542///
2543/// \returns the type this bit-field will promote to, or NULL if no
2544/// promotion occurs.
2545QualType ASTContext::isPromotableBitField(Expr *E) {
2546 FieldDecl *Field = E->getBitField();
2547 if (!Field)
2548 return QualType();
2549
2550 QualType FT = Field->getType();
2551
2552 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2553 uint64_t BitWidth = BitWidthAP.getZExtValue();
2554 uint64_t IntSize = getTypeSize(IntTy);
2555 // GCC extension compatibility: if the bit-field size is less than or equal
2556 // to the size of int, it gets promoted no matter what its type is.
2557 // For instance, unsigned long bf : 4 gets promoted to signed int.
2558 if (BitWidth < IntSize)
2559 return IntTy;
2560
2561 if (BitWidth == IntSize)
2562 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2563
2564 // Types bigger than int are not subject to promotions, and therefore act
2565 // like the base type.
2566 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2567 // is ridiculous.
2568 return QualType();
2569}
2570
Eli Friedmana95d7572009-08-19 07:44:53 +00002571/// getPromotedIntegerType - Returns the type that Promotable will
2572/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2573/// integer type.
2574QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2575 assert(!Promotable.isNull());
2576 assert(Promotable->isPromotableIntegerType());
2577 if (Promotable->isSignedIntegerType())
2578 return IntTy;
2579 uint64_t PromotableSize = getTypeSize(Promotable);
2580 uint64_t IntSize = getTypeSize(IntTy);
2581 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2582 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2583}
2584
Mike Stump1eb44332009-09-09 15:08:12 +00002585/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002586/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002587/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002588int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002589 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2590 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002591 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002592
Chris Lattnerf52ab252008-04-06 22:59:24 +00002593 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2594 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002595
Chris Lattner7cfeb082008-04-06 23:55:33 +00002596 unsigned LHSRank = getIntegerRank(LHSC);
2597 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Chris Lattner7cfeb082008-04-06 23:55:33 +00002599 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2600 if (LHSRank == RHSRank) return 0;
2601 return LHSRank > RHSRank ? 1 : -1;
2602 }
Mike Stump1eb44332009-09-09 15:08:12 +00002603
Chris Lattner7cfeb082008-04-06 23:55:33 +00002604 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2605 if (LHSUnsigned) {
2606 // If the unsigned [LHS] type is larger, return it.
2607 if (LHSRank >= RHSRank)
2608 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Chris Lattner7cfeb082008-04-06 23:55:33 +00002610 // If the signed type can represent all values of the unsigned type, it
2611 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002612 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002613 return -1;
2614 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002615
Chris Lattner7cfeb082008-04-06 23:55:33 +00002616 // If the unsigned [RHS] type is larger, return it.
2617 if (RHSRank >= LHSRank)
2618 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Chris Lattner7cfeb082008-04-06 23:55:33 +00002620 // If the signed type can represent all values of the unsigned type, it
2621 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002622 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002623 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002624}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002625
Mike Stump1eb44332009-09-09 15:08:12 +00002626// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002627QualType ASTContext::getCFConstantStringType() {
2628 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002629 CFConstantStringTypeDecl =
2630 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002631 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002632 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002633
Anders Carlsson71993dd2007-08-17 05:31:46 +00002634 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002635 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002636 // int flags;
2637 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002638 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002639 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002640 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002641 FieldTypes[3] = LongTy;
2642
Anders Carlsson71993dd2007-08-17 05:31:46 +00002643 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002644 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002645 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002646 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002647 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002648 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002649 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002650 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002651 }
2652
2653 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002654 }
Mike Stump1eb44332009-09-09 15:08:12 +00002655
Anders Carlsson71993dd2007-08-17 05:31:46 +00002656 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002657}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002658
Douglas Gregor319ac892009-04-23 22:29:11 +00002659void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002660 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002661 assert(Rec && "Invalid CFConstantStringType");
2662 CFConstantStringTypeDecl = Rec->getDecl();
2663}
2664
Mike Stump1eb44332009-09-09 15:08:12 +00002665QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002666 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002667 ObjCFastEnumerationStateTypeDecl =
2668 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2669 &Idents.get("__objcFastEnumerationState"));
Mike Stump1eb44332009-09-09 15:08:12 +00002670
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002671 QualType FieldTypes[] = {
2672 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002673 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002674 getPointerType(UnsignedLongTy),
2675 getConstantArrayType(UnsignedLongTy,
2676 llvm::APInt(32, 5), ArrayType::Normal, 0)
2677 };
Mike Stump1eb44332009-09-09 15:08:12 +00002678
Douglas Gregor44b43212008-12-11 16:49:14 +00002679 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002680 FieldDecl *Field = FieldDecl::Create(*this,
2681 ObjCFastEnumerationStateTypeDecl,
2682 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002683 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002684 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002685 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002686 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002687 }
Mike Stump1eb44332009-09-09 15:08:12 +00002688
Douglas Gregor44b43212008-12-11 16:49:14 +00002689 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002690 }
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002692 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2693}
2694
Douglas Gregor319ac892009-04-23 22:29:11 +00002695void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002696 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002697 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2698 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2699}
2700
Anders Carlssone8c49532007-10-29 06:33:42 +00002701// This returns true if a type has been typedefed to BOOL:
2702// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002703static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002704 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002705 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2706 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00002707
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002708 return false;
2709}
2710
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002711/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002712/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002713int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002714 uint64_t sz = getTypeSize(type);
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002716 // Make all integer and enum types at least as large as an int
2717 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002718 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002719 // Treat arrays as pointers, since that's how they're passed in.
2720 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002721 sz = getTypeSize(VoidPtrTy);
2722 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002723}
2724
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002725/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002726/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002727void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002728 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002729 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002730 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002731 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002732 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002733 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002734 // Compute size of all parameters.
2735 // Start with computing size of a pointer in number of bytes.
2736 // FIXME: There might(should) be a better way of doing this computation!
2737 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002738 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002739 // The first two arguments (self and _cmd) are pointers; account for
2740 // their size.
2741 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002742 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2743 E = Decl->param_end(); PI != E; ++PI) {
2744 QualType PType = (*PI)->getType();
2745 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002746 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002747 ParmOffset += sz;
2748 }
2749 S += llvm::utostr(ParmOffset);
2750 S += "@0:";
2751 S += llvm::utostr(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00002752
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002753 // Argument types.
2754 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002755 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2756 E = Decl->param_end(); PI != E; ++PI) {
2757 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00002758 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002759 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002760 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2761 // Use array's original type only if it has known number of
2762 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002763 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002764 PType = PVDecl->getType();
2765 } else if (PType->isFunctionType())
2766 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002767 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002768 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002769 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002770 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002771 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002772 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002773 }
2774}
2775
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002776/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002777/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002778/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2779/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00002780/// Property attributes are stored as a comma-delimited C string. The simple
2781/// attributes readonly and bycopy are encoded as single characters. The
2782/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2783/// encoded as single characters, followed by an identifier. Property types
2784/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002785/// these attributes are defined by the following enumeration:
2786/// @code
2787/// enum PropertyAttributes {
2788/// kPropertyReadOnly = 'R', // property is read-only.
2789/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2790/// kPropertyByref = '&', // property is a reference to the value last assigned
2791/// kPropertyDynamic = 'D', // property is dynamic
2792/// kPropertyGetter = 'G', // followed by getter selector name
2793/// kPropertySetter = 'S', // followed by setter selector name
2794/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2795/// kPropertyType = 't' // followed by old-style type encoding.
2796/// kPropertyWeak = 'W' // 'weak' property
2797/// kPropertyStrong = 'P' // property GC'able
2798/// kPropertyNonAtomic = 'N' // property non-atomic
2799/// };
2800/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00002801void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002802 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002803 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002804 // Collect information from the property implementation decl(s).
2805 bool Dynamic = false;
2806 ObjCPropertyImplDecl *SynthesizePID = 0;
2807
2808 // FIXME: Duplicated code due to poor abstraction.
2809 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00002810 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002811 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2812 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002813 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002814 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002815 ObjCPropertyImplDecl *PID = *i;
2816 if (PID->getPropertyDecl() == PD) {
2817 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2818 Dynamic = true;
2819 } else {
2820 SynthesizePID = PID;
2821 }
2822 }
2823 }
2824 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002825 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002826 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002827 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002828 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002829 ObjCPropertyImplDecl *PID = *i;
2830 if (PID->getPropertyDecl() == PD) {
2831 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2832 Dynamic = true;
2833 } else {
2834 SynthesizePID = PID;
2835 }
2836 }
Mike Stump1eb44332009-09-09 15:08:12 +00002837 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002838 }
2839 }
2840
2841 // FIXME: This is not very efficient.
2842 S = "T";
2843
2844 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002845 // GCC has some special rules regarding encoding of properties which
2846 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00002847 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002848 true /* outermost type */,
2849 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002850
2851 if (PD->isReadOnly()) {
2852 S += ",R";
2853 } else {
2854 switch (PD->getSetterKind()) {
2855 case ObjCPropertyDecl::Assign: break;
2856 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002857 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002858 }
2859 }
2860
2861 // It really isn't clear at all what this means, since properties
2862 // are "dynamic by default".
2863 if (Dynamic)
2864 S += ",D";
2865
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002866 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2867 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002869 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2870 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002871 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002872 }
2873
2874 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2875 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002876 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002877 }
2878
2879 if (SynthesizePID) {
2880 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2881 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002882 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002883 }
2884
2885 // FIXME: OBJCGC: weak & strong
2886}
2887
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002888/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00002889/// Another legacy compatibility encoding: 32-bit longs are encoded as
2890/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002891/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2892///
2893void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00002894 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00002895 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002896 if (BT->getKind() == BuiltinType::ULong &&
2897 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002898 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002899 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002900 if (BT->getKind() == BuiltinType::Long &&
2901 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002902 PointeeTy = IntTy;
2903 }
2904 }
2905}
2906
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002907void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002908 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002909 // We follow the behavior of gcc, expanding structures which are
2910 // directly pointed to, and expanding embedded structures. Note that
2911 // these rules are sufficient to prevent recursive encoding of the
2912 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00002913 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002914 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002915}
2916
Mike Stump1eb44332009-09-09 15:08:12 +00002917static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002918 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002919 const Expr *E = FD->getBitWidth();
2920 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2921 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002922 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002923 S += 'b';
2924 S += llvm::utostr(N);
2925}
2926
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002927// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002928void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2929 bool ExpandPointedToStructures,
2930 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002931 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002932 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002933 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00002934 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002935 if (FD && FD->isBitField())
2936 return EncodeBitField(this, S, FD);
2937 char encoding;
2938 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002939 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002940 case BuiltinType::Void: encoding = 'v'; break;
2941 case BuiltinType::Bool: encoding = 'B'; break;
2942 case BuiltinType::Char_U:
2943 case BuiltinType::UChar: encoding = 'C'; break;
2944 case BuiltinType::UShort: encoding = 'S'; break;
2945 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002946 case BuiltinType::ULong:
2947 encoding =
2948 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002949 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002950 case BuiltinType::UInt128: encoding = 'T'; break;
2951 case BuiltinType::ULongLong: encoding = 'Q'; break;
2952 case BuiltinType::Char_S:
2953 case BuiltinType::SChar: encoding = 'c'; break;
2954 case BuiltinType::Short: encoding = 's'; break;
2955 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002956 case BuiltinType::Long:
2957 encoding =
2958 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002959 break;
2960 case BuiltinType::LongLong: encoding = 'q'; break;
2961 case BuiltinType::Int128: encoding = 't'; break;
2962 case BuiltinType::Float: encoding = 'f'; break;
2963 case BuiltinType::Double: encoding = 'd'; break;
2964 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002965 }
Mike Stump1eb44332009-09-09 15:08:12 +00002966
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002967 S += encoding;
2968 return;
2969 }
Mike Stump1eb44332009-09-09 15:08:12 +00002970
John McCall183700f2009-09-21 23:43:11 +00002971 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002972 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00002973 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002974 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002975 return;
2976 }
Mike Stump1eb44332009-09-09 15:08:12 +00002977
Ted Kremenek6217b802009-07-29 21:53:49 +00002978 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002979 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002980 bool isReadOnly = false;
2981 // For historical/compatibility reasons, the read-only qualifier of the
2982 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2983 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00002984 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00002985 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002986 if (OutermostType && T.isConstQualified()) {
2987 isReadOnly = true;
2988 S += 'r';
2989 }
Mike Stump9fdbab32009-07-31 02:02:20 +00002990 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002991 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00002992 while (P->getAs<PointerType>())
2993 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002994 if (P.isConstQualified()) {
2995 isReadOnly = true;
2996 S += 'r';
2997 }
2998 }
2999 if (isReadOnly) {
3000 // Another legacy compatibility encoding. Some ObjC qualifier and type
3001 // combinations need to be rearranged.
3002 // Rewrite "in const" from "nr" to "rn"
3003 const char * s = S.c_str();
3004 int len = S.length();
3005 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3006 std::string replace = "rn";
3007 S.replace(S.end()-2, S.end(), replace);
3008 }
3009 }
Steve Naroff14108da2009-07-10 23:34:53 +00003010 if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00003011 S += ':';
3012 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00003013 }
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003015 if (PointeeTy->isCharType()) {
3016 // char pointer types should be encoded as '*' unless it is a
3017 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003018 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003019 S += '*';
3020 return;
3021 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003022 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003023 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3024 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3025 S += '#';
3026 return;
3027 }
3028 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3029 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3030 S += '@';
3031 return;
3032 }
3033 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003034 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003035 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003036 getLegacyIntegralTypeEncoding(PointeeTy);
3037
Mike Stump1eb44332009-09-09 15:08:12 +00003038 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003039 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003040 return;
3041 }
Mike Stump1eb44332009-09-09 15:08:12 +00003042
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003043 if (const ArrayType *AT =
3044 // Ignore type qualifiers etc.
3045 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003046 if (isa<IncompleteArrayType>(AT)) {
3047 // Incomplete arrays are encoded as a pointer to the array element.
3048 S += '^';
3049
Mike Stump1eb44332009-09-09 15:08:12 +00003050 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003051 false, ExpandStructures, FD);
3052 } else {
3053 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003054
Anders Carlsson559a8332009-02-22 01:38:57 +00003055 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3056 S += llvm::utostr(CAT->getSize().getZExtValue());
3057 else {
3058 //Variable length arrays are encoded as a regular array with 0 elements.
3059 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3060 S += '0';
3061 }
Mike Stump1eb44332009-09-09 15:08:12 +00003062
3063 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003064 false, ExpandStructures, FD);
3065 S += ']';
3066 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003067 return;
3068 }
Mike Stump1eb44332009-09-09 15:08:12 +00003069
John McCall183700f2009-09-21 23:43:11 +00003070 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003071 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003072 return;
3073 }
Mike Stump1eb44332009-09-09 15:08:12 +00003074
Ted Kremenek6217b802009-07-29 21:53:49 +00003075 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003076 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003077 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003078 // Anonymous structures print as '?'
3079 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3080 S += II->getName();
3081 } else {
3082 S += '?';
3083 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003084 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003085 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003086 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3087 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003088 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003089 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003090 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003091 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003092 S += '"';
3093 }
Mike Stump1eb44332009-09-09 15:08:12 +00003094
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003095 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003096 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003097 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003098 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003099 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003100 QualType qt = Field->getType();
3101 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003102 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003103 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003104 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003105 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003106 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003107 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003108 return;
3109 }
Mike Stump1eb44332009-09-09 15:08:12 +00003110
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003111 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003112 if (FD && FD->isBitField())
3113 EncodeBitField(this, S, FD);
3114 else
3115 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003116 return;
3117 }
Mike Stump1eb44332009-09-09 15:08:12 +00003118
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003119 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003120 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003121 return;
3122 }
Mike Stump1eb44332009-09-09 15:08:12 +00003123
John McCall0953e762009-09-24 19:53:00 +00003124 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003125 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003126 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003127 S += '{';
3128 const IdentifierInfo *II = OI->getIdentifier();
3129 S += II->getName();
3130 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003131 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003132 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003133 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003134 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003135 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003136 RecFields[i]);
3137 else
Mike Stump1eb44332009-09-09 15:08:12 +00003138 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003139 FD);
3140 }
3141 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003142 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003143 }
Mike Stump1eb44332009-09-09 15:08:12 +00003144
John McCall183700f2009-09-21 23:43:11 +00003145 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003146 if (OPT->isObjCIdType()) {
3147 S += '@';
3148 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003149 }
Mike Stump1eb44332009-09-09 15:08:12 +00003150
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003151 if (OPT->isObjCClassType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003152 S += '#';
3153 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003154 }
Mike Stump1eb44332009-09-09 15:08:12 +00003155
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003156 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003157 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003158 ExpandPointedToStructures,
3159 ExpandStructures, FD);
3160 if (FD || EncodingProperty) {
3161 // Note that we do extended encoding of protocol qualifer list
3162 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003163 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003164 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3165 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003166 S += '<';
3167 S += (*I)->getNameAsString();
3168 S += '>';
3169 }
3170 S += '"';
3171 }
3172 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003173 }
Mike Stump1eb44332009-09-09 15:08:12 +00003174
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003175 QualType PointeeTy = OPT->getPointeeType();
3176 if (!EncodingProperty &&
3177 isa<TypedefType>(PointeeTy.getTypePtr())) {
3178 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003179 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003180 // {...};
3181 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003182 getObjCEncodingForTypeImpl(PointeeTy, S,
3183 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003184 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003185 return;
3186 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003187
3188 S += '@';
3189 if (FD || EncodingProperty) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003190 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003191 S += OPT->getInterfaceDecl()->getNameAsCString();
3192 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3193 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003194 S += '<';
3195 S += (*I)->getNameAsString();
3196 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003197 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003198 S += '"';
3199 }
3200 return;
3201 }
Mike Stump1eb44332009-09-09 15:08:12 +00003202
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003203 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003204}
3205
Mike Stump1eb44332009-09-09 15:08:12 +00003206void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003207 std::string& S) const {
3208 if (QT & Decl::OBJC_TQ_In)
3209 S += 'n';
3210 if (QT & Decl::OBJC_TQ_Inout)
3211 S += 'N';
3212 if (QT & Decl::OBJC_TQ_Out)
3213 S += 'o';
3214 if (QT & Decl::OBJC_TQ_Bycopy)
3215 S += 'O';
3216 if (QT & Decl::OBJC_TQ_Byref)
3217 S += 'R';
3218 if (QT & Decl::OBJC_TQ_Oneway)
3219 S += 'V';
3220}
3221
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003222void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003223 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003224
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003225 BuiltinVaListType = T;
3226}
3227
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003228void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003229 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003230}
3231
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003232void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor319ac892009-04-23 22:29:11 +00003233 ObjCSelType = T;
3234
John McCall183700f2009-09-21 23:43:11 +00003235 const TypedefType *TT = T->getAs<TypedefType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003236 if (!TT)
3237 return;
3238 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003239
3240 // typedef struct objc_selector *SEL;
Ted Kremenek6217b802009-07-29 21:53:49 +00003241 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003242 if (!ptr)
3243 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003244 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003245 if (!rec)
3246 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003247 SelStructType = rec;
3248}
3249
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003250void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003251 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003252}
3253
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003254void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003255 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003256}
3257
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003258void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003259 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003260 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003261
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003262 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003263}
3264
Douglas Gregor7532dc62009-03-30 22:58:21 +00003265/// \brief Retrieve the template name that represents a qualified
3266/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003267TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003268 bool TemplateKeyword,
3269 TemplateDecl *Template) {
3270 llvm::FoldingSetNodeID ID;
3271 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3272
3273 void *InsertPos = 0;
3274 QualifiedTemplateName *QTN =
3275 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3276 if (!QTN) {
3277 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3278 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3279 }
3280
3281 return TemplateName(QTN);
3282}
3283
Douglas Gregord99cbe62009-07-29 18:26:50 +00003284/// \brief Retrieve the template name that represents a qualified
3285/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003286TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregord99cbe62009-07-29 18:26:50 +00003287 bool TemplateKeyword,
3288 OverloadedFunctionDecl *Template) {
3289 llvm::FoldingSetNodeID ID;
3290 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump1eb44332009-09-09 15:08:12 +00003291
Douglas Gregord99cbe62009-07-29 18:26:50 +00003292 void *InsertPos = 0;
3293 QualifiedTemplateName *QTN =
3294 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3295 if (!QTN) {
3296 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3297 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3298 }
Mike Stump1eb44332009-09-09 15:08:12 +00003299
Douglas Gregord99cbe62009-07-29 18:26:50 +00003300 return TemplateName(QTN);
3301}
3302
Douglas Gregor7532dc62009-03-30 22:58:21 +00003303/// \brief Retrieve the template name that represents a dependent
3304/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003305TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003306 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003307 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003308 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003309
3310 llvm::FoldingSetNodeID ID;
3311 DependentTemplateName::Profile(ID, NNS, Name);
3312
3313 void *InsertPos = 0;
3314 DependentTemplateName *QTN =
3315 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3316
3317 if (QTN)
3318 return TemplateName(QTN);
3319
3320 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3321 if (CanonNNS == NNS) {
3322 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3323 } else {
3324 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3325 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3326 }
3327
3328 DependentTemplateNames.InsertNode(QTN, InsertPos);
3329 return TemplateName(QTN);
3330}
3331
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003332/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003333/// TargetInfo, produce the corresponding type. The unsigned @p Type
3334/// is actually a value of type @c TargetInfo::IntType.
3335QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003336 switch (Type) {
Mike Stump1eb44332009-09-09 15:08:12 +00003337 case TargetInfo::NoInt: return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003338 case TargetInfo::SignedShort: return ShortTy;
3339 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3340 case TargetInfo::SignedInt: return IntTy;
3341 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3342 case TargetInfo::SignedLong: return LongTy;
3343 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3344 case TargetInfo::SignedLongLong: return LongLongTy;
3345 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3346 }
3347
3348 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00003349 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003350}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003351
3352//===----------------------------------------------------------------------===//
3353// Type Predicates.
3354//===----------------------------------------------------------------------===//
3355
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003356/// isObjCNSObjectType - Return true if this is an NSObject object using
3357/// NSObject attribute on a c-style pointer type.
3358/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003359/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003360///
3361bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3362 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3363 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003364 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003365 return true;
3366 }
Mike Stump1eb44332009-09-09 15:08:12 +00003367 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003368}
3369
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003370/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3371/// garbage collection attribute.
3372///
John McCall0953e762009-09-24 19:53:00 +00003373Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3374 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003375 if (getLangOptions().ObjC1 &&
3376 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003377 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003378 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003379 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003380 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003381 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003382 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003383 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003384 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003385 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003386 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003387 // Non-pointers have none gc'able attribute regardless of the attribute
3388 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003389 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003390 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003391 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003392 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003393}
3394
Chris Lattner6ac46a42008-04-07 06:51:04 +00003395//===----------------------------------------------------------------------===//
3396// Type Compatibility Testing
3397//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003398
Mike Stump1eb44332009-09-09 15:08:12 +00003399/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003400/// compatible.
3401static bool areCompatVectorTypes(const VectorType *LHS,
3402 const VectorType *RHS) {
3403 assert(LHS->isCanonical() && RHS->isCanonical());
3404 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003405 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003406}
3407
Steve Naroff4084c302009-07-23 01:01:38 +00003408//===----------------------------------------------------------------------===//
3409// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3410//===----------------------------------------------------------------------===//
3411
3412/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3413/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003414bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3415 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003416 if (lProto == rProto)
3417 return true;
3418 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3419 E = rProto->protocol_end(); PI != E; ++PI)
3420 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3421 return true;
3422 return false;
3423}
3424
Steve Naroff4084c302009-07-23 01:01:38 +00003425/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3426/// return true if lhs's protocols conform to rhs's protocol; false
3427/// otherwise.
3428bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3429 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3430 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3431 return false;
3432}
3433
3434/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3435/// ObjCQualifiedIDType.
3436bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3437 bool compare) {
3438 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003439 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003440 lhs->isObjCIdType() || lhs->isObjCClassType())
3441 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003442 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003443 rhs->isObjCIdType() || rhs->isObjCClassType())
3444 return true;
3445
3446 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003447 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003448
Steve Naroff4084c302009-07-23 01:01:38 +00003449 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003450
Steve Naroff4084c302009-07-23 01:01:38 +00003451 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003452 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003453 // make sure we check the class hierarchy.
3454 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3455 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3456 E = lhsQID->qual_end(); I != E; ++I) {
3457 // when comparing an id<P> on lhs with a static type on rhs,
3458 // see if static class implements all of id's protocols, directly or
3459 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003460 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003461 return false;
3462 }
3463 }
3464 // If there are no qualifiers and no interface, we have an 'id'.
3465 return true;
3466 }
Mike Stump1eb44332009-09-09 15:08:12 +00003467 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003468 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3469 E = lhsQID->qual_end(); I != E; ++I) {
3470 ObjCProtocolDecl *lhsProto = *I;
3471 bool match = false;
3472
3473 // when comparing an id<P> on lhs with a static type on rhs,
3474 // see if static class implements all of id's protocols, directly or
3475 // through its super class and categories.
3476 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3477 E = rhsOPT->qual_end(); J != E; ++J) {
3478 ObjCProtocolDecl *rhsProto = *J;
3479 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3480 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3481 match = true;
3482 break;
3483 }
3484 }
Mike Stump1eb44332009-09-09 15:08:12 +00003485 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00003486 // make sure we check the class hierarchy.
3487 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3488 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3489 E = lhsQID->qual_end(); I != E; ++I) {
3490 // when comparing an id<P> on lhs with a static type on rhs,
3491 // see if static class implements all of id's protocols, directly or
3492 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003493 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003494 match = true;
3495 break;
3496 }
3497 }
3498 }
3499 if (!match)
3500 return false;
3501 }
Mike Stump1eb44332009-09-09 15:08:12 +00003502
Steve Naroff4084c302009-07-23 01:01:38 +00003503 return true;
3504 }
Mike Stump1eb44332009-09-09 15:08:12 +00003505
Steve Naroff4084c302009-07-23 01:01:38 +00003506 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3507 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3508
Mike Stump1eb44332009-09-09 15:08:12 +00003509 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00003510 lhs->getAsObjCInterfacePointerType()) {
3511 if (lhsOPT->qual_empty()) {
3512 bool match = false;
3513 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3514 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3515 E = rhsQID->qual_end(); I != E; ++I) {
3516 // when comparing an id<P> on lhs with a static type on rhs,
3517 // see if static class implements all of id's protocols, directly or
3518 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003519 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003520 match = true;
3521 break;
3522 }
3523 }
3524 if (!match)
3525 return false;
3526 }
3527 return true;
3528 }
Mike Stump1eb44332009-09-09 15:08:12 +00003529 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003530 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3531 E = lhsOPT->qual_end(); I != E; ++I) {
3532 ObjCProtocolDecl *lhsProto = *I;
3533 bool match = false;
3534
3535 // when comparing an id<P> on lhs with a static type on rhs,
3536 // see if static class implements all of id's protocols, directly or
3537 // through its super class and categories.
3538 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3539 E = rhsQID->qual_end(); J != E; ++J) {
3540 ObjCProtocolDecl *rhsProto = *J;
3541 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3542 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3543 match = true;
3544 break;
3545 }
3546 }
3547 if (!match)
3548 return false;
3549 }
3550 return true;
3551 }
3552 return false;
3553}
3554
Eli Friedman3d815e72008-08-22 00:56:42 +00003555/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003556/// compatible for assignment from RHS to LHS. This handles validation of any
3557/// protocol qualifiers on the LHS or RHS.
3558///
Steve Naroff14108da2009-07-10 23:34:53 +00003559bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3560 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003561 // If either type represents the built-in 'id' or 'Class' types, return true.
3562 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00003563 return true;
3564
Steve Naroff4084c302009-07-23 01:01:38 +00003565 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00003566 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3567 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00003568 false);
3569
Steve Naroff14108da2009-07-10 23:34:53 +00003570 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3571 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00003572 if (LHS && RHS) // We have 2 user-defined types.
3573 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00003574
Steve Naroff4084c302009-07-23 01:01:38 +00003575 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003576}
3577
Eli Friedman3d815e72008-08-22 00:56:42 +00003578bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3579 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00003580 // Verify that the base decls are compatible: the RHS must be a subclass of
3581 // the LHS.
3582 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3583 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003584
Chris Lattner6ac46a42008-04-07 06:51:04 +00003585 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3586 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003587 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003588 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003589
Chris Lattner6ac46a42008-04-07 06:51:04 +00003590 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3591 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003592 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003593 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00003594
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003595 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3596 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003597 LHSPI != LHSPE; LHSPI++) {
3598 bool RHSImplementsProtocol = false;
3599
3600 // If the RHS doesn't implement the protocol on the left, the types
3601 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003602 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00003603 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00003604 RHSPI != RHSPE; RHSPI++) {
3605 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003606 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003607 break;
3608 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003609 }
3610 // FIXME: For better diagnostics, consider passing back the protocol name.
3611 if (!RHSImplementsProtocol)
3612 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003613 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003614 // The RHS implements all protocols listed on the LHS.
3615 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003616}
3617
Steve Naroff389bf462009-02-12 17:52:19 +00003618bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3619 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00003620 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3621 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003622
Steve Naroff14108da2009-07-10 23:34:53 +00003623 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00003624 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003625
3626 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3627 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00003628}
3629
Mike Stump1eb44332009-09-09 15:08:12 +00003630/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00003631/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00003632/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00003633/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00003634bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3635 return !mergeTypes(LHS, RHS).isNull();
3636}
3637
3638QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall183700f2009-09-21 23:43:11 +00003639 const FunctionType *lbase = lhs->getAs<FunctionType>();
3640 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00003641 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3642 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00003643 bool allLTypes = true;
3644 bool allRTypes = true;
3645
3646 // Check return type
3647 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3648 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003649 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3650 allLTypes = false;
3651 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3652 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00003653 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00003654 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3655 if (NoReturn != lbase->getNoReturnAttr())
3656 allLTypes = false;
3657 if (NoReturn != rbase->getNoReturnAttr())
3658 allRTypes = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003659
Eli Friedman3d815e72008-08-22 00:56:42 +00003660 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00003661 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3662 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003663 unsigned lproto_nargs = lproto->getNumArgs();
3664 unsigned rproto_nargs = rproto->getNumArgs();
3665
3666 // Compatible functions must have the same number of arguments
3667 if (lproto_nargs != rproto_nargs)
3668 return QualType();
3669
3670 // Variadic and non-variadic functions aren't compatible
3671 if (lproto->isVariadic() != rproto->isVariadic())
3672 return QualType();
3673
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003674 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3675 return QualType();
3676
Eli Friedman3d815e72008-08-22 00:56:42 +00003677 // Check argument compatibility
3678 llvm::SmallVector<QualType, 10> types;
3679 for (unsigned i = 0; i < lproto_nargs; i++) {
3680 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3681 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3682 QualType argtype = mergeTypes(largtype, rargtype);
3683 if (argtype.isNull()) return QualType();
3684 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003685 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3686 allLTypes = false;
3687 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3688 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003689 }
3690 if (allLTypes) return lhs;
3691 if (allRTypes) return rhs;
3692 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00003693 lproto->isVariadic(), lproto->getTypeQuals(),
3694 NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003695 }
3696
3697 if (lproto) allRTypes = false;
3698 if (rproto) allLTypes = false;
3699
Douglas Gregor72564e72009-02-26 23:50:07 +00003700 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003701 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003702 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003703 if (proto->isVariadic()) return QualType();
3704 // Check that the types are compatible with the types that
3705 // would result from default argument promotions (C99 6.7.5.3p15).
3706 // The only types actually affected are promotable integer
3707 // types and floats, which would be passed as a different
3708 // type depending on whether the prototype is visible.
3709 unsigned proto_nargs = proto->getNumArgs();
3710 for (unsigned i = 0; i < proto_nargs; ++i) {
3711 QualType argTy = proto->getArgType(i);
3712 if (argTy->isPromotableIntegerType() ||
3713 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3714 return QualType();
3715 }
3716
3717 if (allLTypes) return lhs;
3718 if (allRTypes) return rhs;
3719 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00003720 proto->getNumArgs(), proto->isVariadic(),
3721 proto->getTypeQuals(), NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003722 }
3723
3724 if (allLTypes) return lhs;
3725 if (allRTypes) return rhs;
Mike Stump24556362009-07-25 21:26:53 +00003726 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003727}
3728
3729QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003730 // C++ [expr]: If an expression initially has the type "reference to T", the
3731 // type is adjusted to "T" prior to any further analysis, the expression
3732 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003733 // expression is an lvalue unless the reference is an rvalue reference and
3734 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003735 // FIXME: C++ shouldn't be going through here! The rules are different
3736 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003737 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3738 // shouldn't be going through here!
Ted Kremenek6217b802009-07-29 21:53:49 +00003739 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003740 LHS = RT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00003741 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003742 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003743
Eli Friedman3d815e72008-08-22 00:56:42 +00003744 QualType LHSCan = getCanonicalType(LHS),
3745 RHSCan = getCanonicalType(RHS);
3746
3747 // If two types are identical, they are compatible.
3748 if (LHSCan == RHSCan)
3749 return LHS;
3750
John McCall0953e762009-09-24 19:53:00 +00003751 // If the qualifiers are different, the types aren't compatible... mostly.
3752 Qualifiers LQuals = LHSCan.getQualifiers();
3753 Qualifiers RQuals = RHSCan.getQualifiers();
3754 if (LQuals != RQuals) {
3755 // If any of these qualifiers are different, we have a type
3756 // mismatch.
3757 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
3758 LQuals.getAddressSpace() != RQuals.getAddressSpace())
3759 return QualType();
3760
3761 // Exactly one GC qualifier difference is allowed: __strong is
3762 // okay if the other type has no GC qualifier but is an Objective
3763 // C object pointer (i.e. implicitly strong by default). We fix
3764 // this by pretending that the unqualified type was actually
3765 // qualified __strong.
3766 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
3767 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
3768 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
3769
3770 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
3771 return QualType();
3772
3773 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
3774 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
3775 }
3776 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
3777 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
3778 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003779 return QualType();
John McCall0953e762009-09-24 19:53:00 +00003780 }
3781
3782 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00003783
Eli Friedman852d63b2009-06-01 01:22:52 +00003784 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3785 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003786
Chris Lattner1adb8832008-01-14 05:45:46 +00003787 // We want to consider the two function types to be the same for these
3788 // comparisons, just force one to the other.
3789 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3790 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003791
3792 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003793 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3794 LHSClass = Type::ConstantArray;
3795 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3796 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00003797
Nate Begeman213541a2008-04-18 23:10:10 +00003798 // Canonicalize ExtVector -> Vector.
3799 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3800 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00003801
Chris Lattnera36a61f2008-04-07 05:43:21 +00003802 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003803 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00003804 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00003805 // a signed integer type, or an unsigned integer type.
John McCall183700f2009-09-21 23:43:11 +00003806 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003807 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3808 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003809 }
John McCall183700f2009-09-21 23:43:11 +00003810 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003811 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3812 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003813 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003814
Eli Friedman3d815e72008-08-22 00:56:42 +00003815 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003816 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003817
Steve Naroff4a746782008-01-09 22:43:08 +00003818 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003819 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003820#define TYPE(Class, Base)
3821#define ABSTRACT_TYPE(Class, Base)
3822#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3823#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3824#include "clang/AST/TypeNodes.def"
3825 assert(false && "Non-canonical and dependent types shouldn't get here");
3826 return QualType();
3827
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003828 case Type::LValueReference:
3829 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003830 case Type::MemberPointer:
3831 assert(false && "C++ should never be in mergeTypes");
3832 return QualType();
3833
3834 case Type::IncompleteArray:
3835 case Type::VariableArray:
3836 case Type::FunctionProto:
3837 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00003838 assert(false && "Types are eliminated above");
3839 return QualType();
3840
Chris Lattner1adb8832008-01-14 05:45:46 +00003841 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003842 {
3843 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003844 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
3845 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003846 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3847 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00003848 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003849 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00003850 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003851 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003852 return getPointerType(ResultType);
3853 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003854 case Type::BlockPointer:
3855 {
3856 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003857 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
3858 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00003859 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3860 if (ResultType.isNull()) return QualType();
3861 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3862 return LHS;
3863 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3864 return RHS;
3865 return getBlockPointerType(ResultType);
3866 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003867 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003868 {
3869 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3870 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3871 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3872 return QualType();
3873
3874 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3875 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3876 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3877 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003878 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3879 return LHS;
3880 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3881 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003882 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3883 ArrayType::ArraySizeModifier(), 0);
3884 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3885 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003886 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3887 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003888 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3889 return LHS;
3890 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3891 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003892 if (LVAT) {
3893 // FIXME: This isn't correct! But tricky to implement because
3894 // the array's size has to be the size of LHS, but the type
3895 // has to be different.
3896 return LHS;
3897 }
3898 if (RVAT) {
3899 // FIXME: This isn't correct! But tricky to implement because
3900 // the array's size has to be the size of RHS, but the type
3901 // has to be different.
3902 return RHS;
3903 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003904 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3905 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003906 return getIncompleteArrayType(ResultType,
3907 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003908 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003909 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003910 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003911 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003912 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003913 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003914 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003915 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003916 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003917 case Type::Complex:
3918 // Distinct complex types are incompatible.
3919 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003920 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003921 // FIXME: The merged type should be an ExtVector!
John McCall183700f2009-09-21 23:43:11 +00003922 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00003923 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003924 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003925 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003926 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003927 // FIXME: This should be type compatibility, e.g. whether
3928 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00003929 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
3930 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00003931 if (LHSIface && RHSIface &&
3932 canAssignObjCInterfaces(LHSIface, RHSIface))
3933 return LHS;
3934
Eli Friedman3d815e72008-08-22 00:56:42 +00003935 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003936 }
Steve Naroff14108da2009-07-10 23:34:53 +00003937 case Type::ObjCObjectPointer: {
John McCall183700f2009-09-21 23:43:11 +00003938 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
3939 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00003940 return LHS;
3941
Steve Naroffbc76dd02008-12-10 22:14:21 +00003942 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00003943 }
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003944 case Type::FixedWidthInt:
3945 // Distinct fixed-width integers are not compatible.
3946 return QualType();
Douglas Gregor7532dc62009-03-30 22:58:21 +00003947 case Type::TemplateSpecialization:
3948 assert(false && "Dependent types have no size");
3949 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003950 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003951
3952 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003953}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003954
Chris Lattner5426bf62008-04-07 07:01:58 +00003955//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003956// Integer Predicates
3957//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003958
Eli Friedmanad74a752008-06-28 06:23:08 +00003959unsigned ASTContext::getIntWidth(QualType T) {
3960 if (T == BoolTy)
3961 return 1;
Chris Lattner6a2b9262009-10-17 20:33:28 +00003962 if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003963 return FWIT->getWidth();
3964 }
3965 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003966 return (unsigned)getTypeSize(T);
3967}
3968
3969QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3970 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00003971
3972 // Turn <4 x signed int> -> <4 x unsigned int>
3973 if (const VectorType *VTy = T->getAs<VectorType>())
3974 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
3975 VTy->getNumElements());
3976
3977 // For enums, we return the unsigned version of the base type.
3978 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00003979 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00003980
3981 const BuiltinType *BTy = T->getAs<BuiltinType>();
3982 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00003983 switch (BTy->getKind()) {
3984 case BuiltinType::Char_S:
3985 case BuiltinType::SChar:
3986 return UnsignedCharTy;
3987 case BuiltinType::Short:
3988 return UnsignedShortTy;
3989 case BuiltinType::Int:
3990 return UnsignedIntTy;
3991 case BuiltinType::Long:
3992 return UnsignedLongTy;
3993 case BuiltinType::LongLong:
3994 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003995 case BuiltinType::Int128:
3996 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003997 default:
3998 assert(0 && "Unexpected signed integer type");
3999 return QualType();
4000 }
4001}
4002
Douglas Gregor2cf26342009-04-09 22:27:44 +00004003ExternalASTSource::~ExternalASTSource() { }
4004
4005void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00004006
4007
4008//===----------------------------------------------------------------------===//
4009// Builtin Type Computation
4010//===----------------------------------------------------------------------===//
4011
4012/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4013/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004014static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004015 ASTContext::GetBuiltinTypeError &Error,
4016 bool AllowTypeModifiers = true) {
4017 // Modifiers.
4018 int HowLong = 0;
4019 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004020
Chris Lattner86df27b2009-06-14 00:45:47 +00004021 // Read the modifiers first.
4022 bool Done = false;
4023 while (!Done) {
4024 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004025 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004026 case 'S':
4027 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4028 assert(!Signed && "Can't use 'S' modifier multiple times!");
4029 Signed = true;
4030 break;
4031 case 'U':
4032 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4033 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4034 Unsigned = true;
4035 break;
4036 case 'L':
4037 assert(HowLong <= 2 && "Can't have LLLL modifier");
4038 ++HowLong;
4039 break;
4040 }
4041 }
4042
4043 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004044
Chris Lattner86df27b2009-06-14 00:45:47 +00004045 // Read the base type.
4046 switch (*Str++) {
4047 default: assert(0 && "Unknown builtin type letter!");
4048 case 'v':
4049 assert(HowLong == 0 && !Signed && !Unsigned &&
4050 "Bad modifiers used with 'v'!");
4051 Type = Context.VoidTy;
4052 break;
4053 case 'f':
4054 assert(HowLong == 0 && !Signed && !Unsigned &&
4055 "Bad modifiers used with 'f'!");
4056 Type = Context.FloatTy;
4057 break;
4058 case 'd':
4059 assert(HowLong < 2 && !Signed && !Unsigned &&
4060 "Bad modifiers used with 'd'!");
4061 if (HowLong)
4062 Type = Context.LongDoubleTy;
4063 else
4064 Type = Context.DoubleTy;
4065 break;
4066 case 's':
4067 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4068 if (Unsigned)
4069 Type = Context.UnsignedShortTy;
4070 else
4071 Type = Context.ShortTy;
4072 break;
4073 case 'i':
4074 if (HowLong == 3)
4075 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4076 else if (HowLong == 2)
4077 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4078 else if (HowLong == 1)
4079 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4080 else
4081 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4082 break;
4083 case 'c':
4084 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4085 if (Signed)
4086 Type = Context.SignedCharTy;
4087 else if (Unsigned)
4088 Type = Context.UnsignedCharTy;
4089 else
4090 Type = Context.CharTy;
4091 break;
4092 case 'b': // boolean
4093 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4094 Type = Context.BoolTy;
4095 break;
4096 case 'z': // size_t.
4097 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4098 Type = Context.getSizeType();
4099 break;
4100 case 'F':
4101 Type = Context.getCFConstantStringType();
4102 break;
4103 case 'a':
4104 Type = Context.getBuiltinVaListType();
4105 assert(!Type.isNull() && "builtin va list type not initialized!");
4106 break;
4107 case 'A':
4108 // This is a "reference" to a va_list; however, what exactly
4109 // this means depends on how va_list is defined. There are two
4110 // different kinds of va_list: ones passed by value, and ones
4111 // passed by reference. An example of a by-value va_list is
4112 // x86, where va_list is a char*. An example of by-ref va_list
4113 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4114 // we want this argument to be a char*&; for x86-64, we want
4115 // it to be a __va_list_tag*.
4116 Type = Context.getBuiltinVaListType();
4117 assert(!Type.isNull() && "builtin va list type not initialized!");
4118 if (Type->isArrayType()) {
4119 Type = Context.getArrayDecayedType(Type);
4120 } else {
4121 Type = Context.getLValueReferenceType(Type);
4122 }
4123 break;
4124 case 'V': {
4125 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004126 unsigned NumElements = strtoul(Str, &End, 10);
4127 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004128
Chris Lattner86df27b2009-06-14 00:45:47 +00004129 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004130
Chris Lattner86df27b2009-06-14 00:45:47 +00004131 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4132 Type = Context.getVectorType(ElementType, NumElements);
4133 break;
4134 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00004135 case 'X': {
4136 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4137 Type = Context.getComplexType(ElementType);
4138 break;
4139 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004140 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004141 Type = Context.getFILEType();
4142 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004143 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004144 return QualType();
4145 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004146 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004147 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004148 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004149 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004150 else
4151 Type = Context.getjmp_bufType();
4152
Mike Stumpfd612db2009-07-28 23:47:15 +00004153 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004154 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004155 return QualType();
4156 }
4157 break;
Mike Stump782fa302009-07-28 02:25:19 +00004158 }
Mike Stump1eb44332009-09-09 15:08:12 +00004159
Chris Lattner86df27b2009-06-14 00:45:47 +00004160 if (!AllowTypeModifiers)
4161 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004162
Chris Lattner86df27b2009-06-14 00:45:47 +00004163 Done = false;
4164 while (!Done) {
4165 switch (*Str++) {
4166 default: Done = true; --Str; break;
4167 case '*':
4168 Type = Context.getPointerType(Type);
4169 break;
4170 case '&':
4171 Type = Context.getLValueReferenceType(Type);
4172 break;
4173 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4174 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004175 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004176 break;
4177 }
4178 }
Mike Stump1eb44332009-09-09 15:08:12 +00004179
Chris Lattner86df27b2009-06-14 00:45:47 +00004180 return Type;
4181}
4182
4183/// GetBuiltinType - Return the type for the specified builtin.
4184QualType ASTContext::GetBuiltinType(unsigned id,
4185 GetBuiltinTypeError &Error) {
4186 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004187
Chris Lattner86df27b2009-06-14 00:45:47 +00004188 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004189
Chris Lattner86df27b2009-06-14 00:45:47 +00004190 Error = GE_None;
4191 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4192 if (Error != GE_None)
4193 return QualType();
4194 while (TypeStr[0] && TypeStr[0] != '.') {
4195 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4196 if (Error != GE_None)
4197 return QualType();
4198
4199 // Do array -> pointer decay. The builtin should use the decayed type.
4200 if (Ty->isArrayType())
4201 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004202
Chris Lattner86df27b2009-06-14 00:45:47 +00004203 ArgTypes.push_back(Ty);
4204 }
4205
4206 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4207 "'.' should only occur at end of builtin type list!");
4208
4209 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4210 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4211 return getFunctionNoProtoType(ResType);
4212 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4213 TypeStr[0] == '.', 0);
4214}
Eli Friedmana95d7572009-08-19 07:44:53 +00004215
4216QualType
4217ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4218 // Perform the usual unary conversions. We do this early so that
4219 // integral promotions to "int" can allow us to exit early, in the
4220 // lhs == rhs check. Also, for conversion purposes, we ignore any
4221 // qualifiers. For example, "const float" and "float" are
4222 // equivalent.
4223 if (lhs->isPromotableIntegerType())
4224 lhs = getPromotedIntegerType(lhs);
4225 else
4226 lhs = lhs.getUnqualifiedType();
4227 if (rhs->isPromotableIntegerType())
4228 rhs = getPromotedIntegerType(rhs);
4229 else
4230 rhs = rhs.getUnqualifiedType();
4231
4232 // If both types are identical, no conversion is needed.
4233 if (lhs == rhs)
4234 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004235
Eli Friedmana95d7572009-08-19 07:44:53 +00004236 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4237 // The caller can deal with this (e.g. pointer + int).
4238 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4239 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004240
4241 // At this point, we have two different arithmetic types.
4242
Eli Friedmana95d7572009-08-19 07:44:53 +00004243 // Handle complex types first (C99 6.3.1.8p1).
4244 if (lhs->isComplexType() || rhs->isComplexType()) {
4245 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004246 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004247 // convert the rhs to the lhs complex type.
4248 return lhs;
4249 }
Mike Stump1eb44332009-09-09 15:08:12 +00004250 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004251 // convert the lhs to the rhs complex type.
4252 return rhs;
4253 }
4254 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004255 // When both operands are complex, the shorter operand is converted to the
4256 // type of the longer, and that is the type of the result. This corresponds
4257 // to what is done when combining two real floating-point operands.
4258 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004259 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004260 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004261 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004262 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004263 // "double _Complex" is promoted to "long double _Complex".
4264 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004265
4266 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004267 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004268 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004269 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004270 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004271 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4272 // domains match. This is a requirement for our implementation, C99
4273 // does not require this promotion.
4274 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4275 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4276 return rhs;
4277 } else { // handle "_Complex double, double".
4278 return lhs;
4279 }
4280 }
4281 return lhs; // The domain/size match exactly.
4282 }
4283 // Now handle "real" floating types (i.e. float, double, long double).
4284 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4285 // if we have an integer operand, the result is the real floating type.
4286 if (rhs->isIntegerType()) {
4287 // convert rhs to the lhs floating point type.
4288 return lhs;
4289 }
4290 if (rhs->isComplexIntegerType()) {
4291 // convert rhs to the complex floating point type.
4292 return getComplexType(lhs);
4293 }
4294 if (lhs->isIntegerType()) {
4295 // convert lhs to the rhs floating point type.
4296 return rhs;
4297 }
Mike Stump1eb44332009-09-09 15:08:12 +00004298 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004299 // convert lhs to the complex floating point type.
4300 return getComplexType(rhs);
4301 }
4302 // We have two real floating types, float/complex combos were handled above.
4303 // Convert the smaller operand to the bigger result.
4304 int result = getFloatingTypeOrder(lhs, rhs);
4305 if (result > 0) // convert the rhs
4306 return lhs;
4307 assert(result < 0 && "illegal float comparison");
4308 return rhs; // convert the lhs
4309 }
4310 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4311 // Handle GCC complex int extension.
4312 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4313 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4314
4315 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004316 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004317 rhsComplexInt->getElementType()) >= 0)
4318 return lhs; // convert the rhs
4319 return rhs;
4320 } else if (lhsComplexInt && rhs->isIntegerType()) {
4321 // convert the rhs to the lhs complex type.
4322 return lhs;
4323 } else if (rhsComplexInt && lhs->isIntegerType()) {
4324 // convert the lhs to the rhs complex type.
4325 return rhs;
4326 }
4327 }
4328 // Finally, we have two differing integer types.
4329 // The rules for this case are in C99 6.3.1.8
4330 int compare = getIntegerTypeOrder(lhs, rhs);
4331 bool lhsSigned = lhs->isSignedIntegerType(),
4332 rhsSigned = rhs->isSignedIntegerType();
4333 QualType destType;
4334 if (lhsSigned == rhsSigned) {
4335 // Same signedness; use the higher-ranked type
4336 destType = compare >= 0 ? lhs : rhs;
4337 } else if (compare != (lhsSigned ? 1 : -1)) {
4338 // The unsigned type has greater than or equal rank to the
4339 // signed type, so use the unsigned type
4340 destType = lhsSigned ? rhs : lhs;
4341 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4342 // The two types are different widths; if we are here, that
4343 // means the signed type is larger than the unsigned type, so
4344 // use the signed type.
4345 destType = lhsSigned ? lhs : rhs;
4346 } else {
4347 // The signed type is higher-ranked than the unsigned type,
4348 // but isn't actually any bigger (like unsigned int and long
4349 // on most 32-bit systems). Use the unsigned type corresponding
4350 // to the signed type.
4351 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4352 }
4353 return destType;
4354}