blob: e0223b009f6f57cae466309a4b2e88bf9a094179 [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 Gregor7caa6822009-07-24 20:34:43 +0000235VarDecl *ASTContext::getInstantiatedFromStaticDataMember(VarDecl *Var) {
236 assert(Var->isStaticDataMember() && "Not a static data member");
237 llvm::DenseMap<VarDecl *, VarDecl *>::iterator Pos
238 = InstantiatedFromStaticDataMember.find(Var);
239 if (Pos == InstantiatedFromStaticDataMember.end())
240 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Douglas Gregor7caa6822009-07-24 20:34:43 +0000242 return Pos->second;
243}
244
Mike Stump1eb44332009-09-09 15:08:12 +0000245void
Douglas Gregor7caa6822009-07-24 20:34:43 +0000246ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl) {
247 assert(Inst->isStaticDataMember() && "Not a static data member");
248 assert(Tmpl->isStaticDataMember() && "Not a static data member");
249 assert(!InstantiatedFromStaticDataMember[Inst] &&
250 "Already noted what static data member was instantiated from");
251 InstantiatedFromStaticDataMember[Inst] = Tmpl;
252}
253
Anders Carlsson0d8df782009-08-29 19:37:28 +0000254UnresolvedUsingDecl *
255ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000256 llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
Anders Carlsson0d8df782009-08-29 19:37:28 +0000257 = InstantiatedFromUnresolvedUsingDecl.find(UUD);
258 if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
259 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Anders Carlsson0d8df782009-08-29 19:37:28 +0000261 return Pos->second;
262}
263
264void
265ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
266 UnresolvedUsingDecl *UUD) {
267 assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
268 "Already noted what using decl what instantiated from");
269 InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
270}
271
Anders Carlssond8b285f2009-09-01 04:26:58 +0000272FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
273 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
274 = InstantiatedFromUnnamedFieldDecl.find(Field);
275 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
276 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Anders Carlssond8b285f2009-09-01 04:26:58 +0000278 return Pos->second;
279}
280
281void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
282 FieldDecl *Tmpl) {
283 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
284 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
285 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
286 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Anders Carlssond8b285f2009-09-01 04:26:58 +0000288 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
289}
290
Douglas Gregor2e222532009-07-02 17:08:52 +0000291namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000292 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000293 : std::binary_function<SourceRange, SourceRange, bool> {
294 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Douglas Gregor2e222532009-07-02 17:08:52 +0000296 public:
297 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Douglas Gregor2e222532009-07-02 17:08:52 +0000299 bool operator()(SourceRange X, SourceRange Y) {
300 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
301 }
302 };
303}
304
305/// \brief Determine whether the given comment is a Doxygen-style comment.
306///
307/// \param Start the start of the comment text.
308///
309/// \param End the end of the comment text.
310///
311/// \param Member whether we want to check whether this is a member comment
312/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
313/// we only return true when we find a non-member comment.
Mike Stump1eb44332009-09-09 15:08:12 +0000314static bool
315isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
Douglas Gregor2e222532009-07-02 17:08:52 +0000316 bool Member = false) {
Mike Stump1eb44332009-09-09 15:08:12 +0000317 const char *BufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000318 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
319 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
320 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Douglas Gregor2e222532009-07-02 17:08:52 +0000322 if (End - Start < 4)
323 return false;
324
325 assert(Start[0] == '/' && "Not a comment?");
326 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
327 return false;
328 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
329 return false;
330
331 return (Start[3] == '<') == Member;
332}
333
334/// \brief Retrieve the comment associated with the given declaration, if
Mike Stump1eb44332009-09-09 15:08:12 +0000335/// it has one.
Douglas Gregor2e222532009-07-02 17:08:52 +0000336const char *ASTContext::getCommentForDecl(const Decl *D) {
337 if (!D)
338 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Douglas Gregor2e222532009-07-02 17:08:52 +0000340 // Check whether we have cached a comment string for this declaration
341 // already.
Mike Stump1eb44332009-09-09 15:08:12 +0000342 llvm::DenseMap<const Decl *, std::string>::iterator Pos
Douglas Gregor2e222532009-07-02 17:08:52 +0000343 = DeclComments.find(D);
344 if (Pos != DeclComments.end())
345 return Pos->second.c_str();
346
Mike Stump1eb44332009-09-09 15:08:12 +0000347 // If we have an external AST source and have not yet loaded comments from
Douglas Gregor2e222532009-07-02 17:08:52 +0000348 // that source, do so now.
349 if (ExternalSource && !LoadedExternalComments) {
350 std::vector<SourceRange> LoadedComments;
351 ExternalSource->ReadComments(LoadedComments);
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Douglas Gregor2e222532009-07-02 17:08:52 +0000353 if (!LoadedComments.empty())
354 Comments.insert(Comments.begin(), LoadedComments.begin(),
355 LoadedComments.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Douglas Gregor2e222532009-07-02 17:08:52 +0000357 LoadedExternalComments = true;
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
360 // If there are no comments anywhere, we won't find anything.
Douglas Gregor2e222532009-07-02 17:08:52 +0000361 if (Comments.empty())
362 return 0;
363
364 // If the declaration doesn't map directly to a location in a file, we
365 // can't find the comment.
366 SourceLocation DeclStartLoc = D->getLocStart();
367 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
368 return 0;
369
370 // Find the comment that occurs just before this declaration.
371 std::vector<SourceRange>::iterator LastComment
Mike Stump1eb44332009-09-09 15:08:12 +0000372 = std::lower_bound(Comments.begin(), Comments.end(),
Douglas Gregor2e222532009-07-02 17:08:52 +0000373 SourceRange(DeclStartLoc),
374 BeforeInTranslationUnit(&SourceMgr));
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Douglas Gregor2e222532009-07-02 17:08:52 +0000376 // Decompose the location for the start of the declaration and find the
377 // beginning of the file buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000378 std::pair<FileID, unsigned> DeclStartDecomp
Douglas Gregor2e222532009-07-02 17:08:52 +0000379 = SourceMgr.getDecomposedLoc(DeclStartLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000380 const char *FileBufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000381 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Douglas Gregor2e222532009-07-02 17:08:52 +0000383 // First check whether we have a comment for a member.
384 if (LastComment != Comments.end() &&
385 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
386 isDoxygenComment(SourceMgr, *LastComment, true)) {
387 std::pair<FileID, unsigned> LastCommentEndDecomp
388 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
389 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
390 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
Mike Stump1eb44332009-09-09 15:08:12 +0000391 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000392 LastCommentEndDecomp.second)) {
393 // The Doxygen member comment comes after the declaration starts and
394 // is on the same line and in the same file as the declaration. This
395 // is the comment we want.
396 std::string &Result = DeclComments[D];
Mike Stump1eb44332009-09-09 15:08:12 +0000397 Result.append(FileBufferStart +
398 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000399 FileBufferStart + LastCommentEndDecomp.second + 1);
400 return Result.c_str();
401 }
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Douglas Gregor2e222532009-07-02 17:08:52 +0000404 if (LastComment == Comments.begin())
405 return 0;
406 --LastComment;
407
408 // Decompose the end of the comment.
409 std::pair<FileID, unsigned> LastCommentEndDecomp
410 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Douglas Gregor2e222532009-07-02 17:08:52 +0000412 // If the comment and the declaration aren't in the same file, then they
413 // aren't related.
414 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
415 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Douglas Gregor2e222532009-07-02 17:08:52 +0000417 // Check that we actually have a Doxygen comment.
418 if (!isDoxygenComment(SourceMgr, *LastComment))
419 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Douglas Gregor2e222532009-07-02 17:08:52 +0000421 // Compute the starting line for the declaration and for the end of the
422 // comment (this is expensive).
Mike Stump1eb44332009-09-09 15:08:12 +0000423 unsigned DeclStartLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000424 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
425 unsigned CommentEndLine
Mike Stump1eb44332009-09-09 15:08:12 +0000426 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000427 LastCommentEndDecomp.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Douglas Gregor2e222532009-07-02 17:08:52 +0000429 // If the comment does not end on the line prior to the declaration, then
430 // the comment is not associated with the declaration at all.
431 if (CommentEndLine + 1 != DeclStartLine)
432 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Douglas Gregor2e222532009-07-02 17:08:52 +0000434 // We have a comment, but there may be more comments on the previous lines.
435 // Keep looking so long as the comments are still Doxygen comments and are
436 // still adjacent.
Mike Stump1eb44332009-09-09 15:08:12 +0000437 unsigned ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000438 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
439 std::vector<SourceRange>::iterator FirstComment = LastComment;
440 while (FirstComment != Comments.begin()) {
441 // Look at the previous comment
442 --FirstComment;
443 std::pair<FileID, unsigned> Decomp
444 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Douglas Gregor2e222532009-07-02 17:08:52 +0000446 // If this previous comment is in a different file, we're done.
447 if (Decomp.first != DeclStartDecomp.first) {
448 ++FirstComment;
449 break;
450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Douglas Gregor2e222532009-07-02 17:08:52 +0000452 // If this comment is not a Doxygen comment, we're done.
453 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
454 ++FirstComment;
455 break;
456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Douglas Gregor2e222532009-07-02 17:08:52 +0000458 // If the line number is not what we expected, we're done.
459 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
460 if (Line != ExpectedLine) {
461 ++FirstComment;
462 break;
463 }
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Douglas Gregor2e222532009-07-02 17:08:52 +0000465 // Set the next expected line number.
Mike Stump1eb44332009-09-09 15:08:12 +0000466 ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000467 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
468 }
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Douglas Gregor2e222532009-07-02 17:08:52 +0000470 // The iterator range [FirstComment, LastComment] contains all of the
471 // BCPL comments that, together, are associated with this declaration.
472 // Form a single comment block string for this declaration that concatenates
473 // all of these comments.
474 std::string &Result = DeclComments[D];
475 while (FirstComment != LastComment) {
476 std::pair<FileID, unsigned> DecompStart
477 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
478 std::pair<FileID, unsigned> DecompEnd
479 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
480 Result.append(FileBufferStart + DecompStart.second,
481 FileBufferStart + DecompEnd.second + 1);
482 ++FirstComment;
483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Douglas Gregor2e222532009-07-02 17:08:52 +0000485 // Append the last comment line.
Mike Stump1eb44332009-09-09 15:08:12 +0000486 Result.append(FileBufferStart +
487 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000488 FileBufferStart + LastCommentEndDecomp.second + 1);
489 return Result.c_str();
490}
491
Chris Lattner464175b2007-07-18 17:52:12 +0000492//===----------------------------------------------------------------------===//
493// Type Sizing and Analysis
494//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000495
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000496/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
497/// scalar floating point type.
498const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000499 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000500 assert(BT && "Not a floating point type!");
501 switch (BT->getKind()) {
502 default: assert(0 && "Not a floating point type!");
503 case BuiltinType::Float: return Target.getFloatFormat();
504 case BuiltinType::Double: return Target.getDoubleFormat();
505 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
506 }
507}
508
Mike Stump196efbf2009-09-22 02:43:44 +0000509/// getDeclAlignInBytes - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000510/// specified decl. Note that bitfields do not have a valid alignment, so
511/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000512unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000513 unsigned Align = Target.getCharWidth();
514
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000515 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedmandcdafb62009-02-22 02:56:25 +0000516 Align = std::max(Align, AA->getAlignment());
517
Chris Lattneraf707ab2009-01-24 21:53:27 +0000518 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
519 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000520 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000521 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000522 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000523 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
524 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000525 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
526 T = cast<ArrayType>(T)->getElementType();
527
528 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
529 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000530 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000531
532 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000533}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000534
Chris Lattnera7674d82007-07-13 22:13:22 +0000535/// getTypeSize - Return the size of the specified type, in bits. This method
536/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000537///
538/// FIXME: Pointers into different addr spaces could have different sizes and
539/// alignment requirements: getPointerInfo should take an AddrSpace, this
540/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000541std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000542ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000543 uint64_t Width=0;
544 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000545 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000546#define TYPE(Class, Base)
547#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000548#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000549#define DEPENDENT_TYPE(Class, Base) case Type::Class:
550#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000551 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000552 break;
553
Chris Lattner692233e2007-07-13 22:27:08 +0000554 case Type::FunctionNoProto:
555 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000556 // GCC extension: alignof(function) = 32 bits
557 Width = 0;
558 Align = 32;
559 break;
560
Douglas Gregor72564e72009-02-26 23:50:07 +0000561 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000562 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000563 Width = 0;
564 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
565 break;
566
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000567 case Type::ConstantArrayWithExpr:
568 case Type::ConstantArrayWithoutExpr:
Steve Narofffb22d962007-08-30 01:06:46 +0000569 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000570 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattner98be4942008-03-05 18:54:05 +0000572 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000573 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000574 Align = EltInfo.second;
575 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000576 }
Nate Begeman213541a2008-04-18 23:10:10 +0000577 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000578 case Type::Vector: {
Mike Stump1eb44332009-09-09 15:08:12 +0000579 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000580 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000581 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000582 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000583 // If the alignment is not a power of 2, round up to the next power of 2.
584 // This happens for non-power-of-2 length vectors.
585 // FIXME: this should probably be a target property.
586 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000587 break;
588 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000589
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000590 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000591 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000592 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000593 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000594 // GCC extension: alignof(void) = 8 bits.
595 Width = 0;
596 Align = 8;
597 break;
598
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000599 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000600 Width = Target.getBoolWidth();
601 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000602 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000603 case BuiltinType::Char_S:
604 case BuiltinType::Char_U:
605 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000606 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000607 Width = Target.getCharWidth();
608 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000609 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000610 case BuiltinType::WChar:
611 Width = Target.getWCharWidth();
612 Align = Target.getWCharAlign();
613 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000614 case BuiltinType::Char16:
615 Width = Target.getChar16Width();
616 Align = Target.getChar16Align();
617 break;
618 case BuiltinType::Char32:
619 Width = Target.getChar32Width();
620 Align = Target.getChar32Align();
621 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000622 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000623 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000624 Width = Target.getShortWidth();
625 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000626 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000627 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000628 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000629 Width = Target.getIntWidth();
630 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000631 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000632 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000633 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000634 Width = Target.getLongWidth();
635 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000636 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000637 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000638 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000639 Width = Target.getLongLongWidth();
640 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000641 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000642 case BuiltinType::Int128:
643 case BuiltinType::UInt128:
644 Width = 128;
645 Align = 128; // int128_t is 128-bit aligned on all targets.
646 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000647 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000648 Width = Target.getFloatWidth();
649 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000650 break;
651 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000652 Width = Target.getDoubleWidth();
653 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000654 break;
655 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000656 Width = Target.getLongDoubleWidth();
657 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000658 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000659 case BuiltinType::NullPtr:
660 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
661 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000662 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000663 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000664 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000665 case Type::FixedWidthInt:
666 // FIXME: This isn't precisely correct; the width/alignment should depend
667 // on the available types for the target
668 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000669 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000670 Align = Width;
671 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000672 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000673 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000674 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000675 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000676 case Type::BlockPointer: {
677 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
678 Width = Target.getPointerWidth(AS);
679 Align = Target.getPointerAlign(AS);
680 break;
681 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000682 case Type::Pointer: {
683 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000684 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000685 Align = Target.getPointerAlign(AS);
686 break;
687 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000688 case Type::LValueReference:
689 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000690 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000691 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000692 // FIXME: This is wrong for struct layout: a reference in a struct has
693 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000694 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000695 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000696 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
697 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
698 // If we ever want to support other ABIs this needs to be abstracted.
699
Sebastian Redlf30208a2009-01-24 21:16:55 +0000700 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000701 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000702 getTypeInfo(getPointerDiffType());
703 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000704 if (Pointee->isFunctionType())
705 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000706 Align = PtrDiffInfo.second;
707 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000708 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000709 case Type::Complex: {
710 // Complex types have the same alignment as their elements, but twice the
711 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000712 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000713 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000714 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000715 Align = EltInfo.second;
716 break;
717 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000718 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000719 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000720 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
721 Width = Layout.getSize();
722 Align = Layout.getAlignment();
723 break;
724 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000725 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000726 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000727 const TagType *TT = cast<TagType>(T);
728
729 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000730 Width = 1;
731 Align = 1;
732 break;
733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Daniel Dunbar1d751182008-11-08 05:48:37 +0000735 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000736 return getTypeInfo(ET->getDecl()->getIntegerType());
737
Daniel Dunbar1d751182008-11-08 05:48:37 +0000738 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000739 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
740 Width = Layout.getSize();
741 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000742 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000743 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000744
John McCall7da24312009-09-05 00:15:47 +0000745 case Type::Elaborated: {
746 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType().getTypePtr());
747 }
748
Douglas Gregor18857642009-04-30 17:32:17 +0000749 case Type::Typedef: {
750 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000751 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregor18857642009-04-30 17:32:17 +0000752 Align = Aligned->getAlignment();
753 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
754 } else
755 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000756 break;
Chris Lattner71763312008-04-06 22:05:18 +0000757 }
Douglas Gregor18857642009-04-30 17:32:17 +0000758
759 case Type::TypeOfExpr:
760 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
761 .getTypePtr());
762
763 case Type::TypeOf:
764 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
765
Anders Carlsson395b4752009-06-24 19:06:50 +0000766 case Type::Decltype:
767 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
768 .getTypePtr());
769
Douglas Gregor18857642009-04-30 17:32:17 +0000770 case Type::QualifiedName:
771 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Douglas Gregor18857642009-04-30 17:32:17 +0000773 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000774 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000775 "Cannot request the size of a dependent type");
776 // FIXME: this is likely to be wrong once we support template
777 // aliases, since a template alias could refer to a typedef that
778 // has an __aligned__ attribute on it.
779 return getTypeInfo(getCanonicalType(T));
780 }
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Chris Lattner464175b2007-07-18 17:52:12 +0000782 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000783 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000784}
785
Chris Lattner34ebde42009-01-27 18:08:34 +0000786/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
787/// type for the current target in bits. This can be different than the ABI
788/// alignment in cases where it is beneficial for performance to overalign
789/// a data type.
790unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
791 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000792
793 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000794 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000795 T = CT->getElementType().getTypePtr();
796 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
797 T->isSpecificBuiltinType(BuiltinType::LongLong))
798 return std::max(ABIAlign, (unsigned)getTypeSize(T));
799
Chris Lattner34ebde42009-01-27 18:08:34 +0000800 return ABIAlign;
801}
802
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000803static void CollectLocalObjCIvars(ASTContext *Ctx,
804 const ObjCInterfaceDecl *OI,
805 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000806 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
807 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000808 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000809 if (!IVDecl->isInvalidDecl())
810 Fields.push_back(cast<FieldDecl>(IVDecl));
811 }
812}
813
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000814void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
815 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
816 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
817 CollectObjCIvars(SuperClass, Fields);
818 CollectLocalObjCIvars(this, OI, Fields);
819}
820
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000821/// ShallowCollectObjCIvars -
822/// Collect all ivars, including those synthesized, in the current class.
823///
824void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
825 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
826 bool CollectSynthesized) {
827 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
828 E = OI->ivar_end(); I != E; ++I) {
829 Ivars.push_back(*I);
830 }
831 if (CollectSynthesized)
832 CollectSynthesizedIvars(OI, Ivars);
833}
834
Fariborz Jahanian98200742009-05-12 18:14:29 +0000835void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
836 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000837 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
838 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian98200742009-05-12 18:14:29 +0000839 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
840 Ivars.push_back(Ivar);
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Fariborz Jahanian98200742009-05-12 18:14:29 +0000842 // Also look into nested protocols.
843 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
844 E = PD->protocol_end(); P != E; ++P)
845 CollectProtocolSynthesizedIvars(*P, Ivars);
846}
847
848/// CollectSynthesizedIvars -
849/// This routine collect synthesized ivars for the designated class.
850///
851void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
852 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000853 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
854 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000855 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
856 Ivars.push_back(Ivar);
857 }
858 // Also look into interface's protocol list for properties declared
859 // in the protocol and whose ivars are synthesized.
860 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
861 PE = OI->protocol_end(); P != PE; ++P) {
862 ObjCProtocolDecl *PD = (*P);
863 CollectProtocolSynthesizedIvars(PD, Ivars);
864 }
865}
866
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000867unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
868 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000869 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
870 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000871 if ((*I)->getPropertyIvarDecl())
872 ++count;
873
874 // Also look into nested protocols.
875 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
876 E = PD->protocol_end(); P != E; ++P)
877 count += CountProtocolSynthesizedIvars(*P);
878 return count;
879}
880
Mike Stump1eb44332009-09-09 15:08:12 +0000881unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000882 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000883 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
884 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000885 if ((*I)->getPropertyIvarDecl())
886 ++count;
887 }
888 // Also look into interface's protocol list for properties declared
889 // in the protocol and whose ivars are synthesized.
890 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
891 PE = OI->protocol_end(); P != PE; ++P) {
892 ObjCProtocolDecl *PD = (*P);
893 count += CountProtocolSynthesizedIvars(PD);
894 }
895 return count;
896}
897
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000898/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
899ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
900 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
901 I = ObjCImpls.find(D);
902 if (I != ObjCImpls.end())
903 return cast<ObjCImplementationDecl>(I->second);
904 return 0;
905}
906/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
907ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
908 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
909 I = ObjCImpls.find(D);
910 if (I != ObjCImpls.end())
911 return cast<ObjCCategoryImplDecl>(I->second);
912 return 0;
913}
914
915/// \brief Set the implementation of ObjCInterfaceDecl.
916void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
917 ObjCImplementationDecl *ImplD) {
918 assert(IFaceD && ImplD && "Passed null params");
919 ObjCImpls[IFaceD] = ImplD;
920}
921/// \brief Set the implementation of ObjCCategoryDecl.
922void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
923 ObjCCategoryImplDecl *ImplD) {
924 assert(CatD && ImplD && "Passed null params");
925 ObjCImpls[CatD] = ImplD;
926}
927
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000928/// \brief Allocate an uninitialized DeclaratorInfo.
929///
930/// The caller should initialize the memory held by DeclaratorInfo using
931/// the TypeLoc wrappers.
932///
933/// \param T the type that will be the basis for type source info. This type
934/// should refer to how the declarator was written in source code, not to
935/// what type semantic analysis resolved the declarator to.
936DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) {
937 unsigned DataSize = TypeLoc::getFullDataSizeForType(T);
938 DeclaratorInfo *DInfo =
939 (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
940 new (DInfo) DeclaratorInfo(T);
941 return DInfo;
942}
943
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000944/// getInterfaceLayoutImpl - Get or compute information about the
945/// layout of the given interface.
946///
947/// \param Impl - If given, also include the layout of the interface's
948/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000949const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000950ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
951 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000952 assert(!D->isForwardDecl() && "Invalid interface decl!");
953
Devang Patel44a3dde2008-06-04 21:54:36 +0000954 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000955 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000956 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
957 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
958 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000959
Daniel Dunbar453addb2009-05-03 11:16:44 +0000960 // Add in synthesized ivar count if laying out an implementation.
961 if (Impl) {
Anders Carlsson29445a02009-07-18 21:19:52 +0000962 unsigned FieldCount = D->ivar_size();
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000963 unsigned SynthCount = CountSynthesizedIvars(D);
964 FieldCount += SynthCount;
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000965 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000966 // entry. Note we can't cache this because we simply free all
967 // entries later; however we shouldn't look up implementations
968 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000969 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000970 return getObjCLayout(D, 0);
971 }
972
Mike Stump1eb44332009-09-09 15:08:12 +0000973 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000974 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
975 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Devang Patel44a3dde2008-06-04 21:54:36 +0000977 return *NewEntry;
978}
979
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000980const ASTRecordLayout &
981ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
982 return getObjCLayout(D, 0);
983}
984
985const ASTRecordLayout &
986ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
987 return getObjCLayout(D->getClassInterface(), D);
988}
989
Devang Patel88a981b2007-11-01 19:11:01 +0000990/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000991/// specified record (struct/union/class), which indicates its size and field
992/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000993const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000994 D = D->getDefinition(*this);
995 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000996
Chris Lattner464175b2007-07-18 17:52:12 +0000997 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +0000998 // Note that we can't save a reference to the entry because this function
999 // is recursive.
1000 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +00001001 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +00001002
Mike Stump1eb44332009-09-09 15:08:12 +00001003 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +00001004 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +00001005 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Chris Lattner5d2a6302007-07-18 18:26:58 +00001007 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001008}
1009
Chris Lattnera7674d82007-07-13 22:13:22 +00001010//===----------------------------------------------------------------------===//
1011// Type creation/memoization methods
1012//===----------------------------------------------------------------------===//
1013
John McCall0953e762009-09-24 19:53:00 +00001014QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1015 unsigned Fast = Quals.getFastQualifiers();
1016 Quals.removeFastQualifiers();
1017
1018 // Check if we've already instantiated this type.
1019 llvm::FoldingSetNodeID ID;
1020 ExtQuals::Profile(ID, TypeNode, Quals);
1021 void *InsertPos = 0;
1022 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1023 assert(EQ->getQualifiers() == Quals);
1024 QualType T = QualType(EQ, Fast);
1025 return T;
1026 }
1027
John McCall6b304a02009-09-24 23:30:46 +00001028 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001029 ExtQualNodes.InsertNode(New, InsertPos);
1030 QualType T = QualType(New, Fast);
1031 return T;
1032}
1033
1034QualType ASTContext::getVolatileType(QualType T) {
1035 QualType CanT = getCanonicalType(T);
1036 if (CanT.isVolatileQualified()) return T;
1037
1038 QualifierCollector Quals;
1039 const Type *TypeNode = Quals.strip(T);
1040 Quals.addVolatile();
1041
1042 return getExtQualType(TypeNode, Quals);
1043}
1044
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001045QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001046 QualType CanT = getCanonicalType(T);
1047 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001048 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001049
John McCall0953e762009-09-24 19:53:00 +00001050 // If we are composing extended qualifiers together, merge together
1051 // into one ExtQuals node.
1052 QualifierCollector Quals;
1053 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001054
John McCall0953e762009-09-24 19:53:00 +00001055 // If this type already has an address space specified, it cannot get
1056 // another one.
1057 assert(!Quals.hasAddressSpace() &&
1058 "Type cannot be in multiple addr spaces!");
1059 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001060
John McCall0953e762009-09-24 19:53:00 +00001061 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001062}
1063
Chris Lattnerb7d25532009-02-18 22:53:11 +00001064QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001065 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001066 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001067 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001068 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001070 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001071 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001072 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001073 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1074 return getPointerType(ResultType);
1075 }
1076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
John McCall0953e762009-09-24 19:53:00 +00001078 // If we are composing extended qualifiers together, merge together
1079 // into one ExtQuals node.
1080 QualifierCollector Quals;
1081 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001082
John McCall0953e762009-09-24 19:53:00 +00001083 // If this type already has an ObjCGC specified, it cannot get
1084 // another one.
1085 assert(!Quals.hasObjCGCAttr() &&
1086 "Type cannot have multiple ObjCGCs!");
1087 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001088
John McCall0953e762009-09-24 19:53:00 +00001089 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001090}
Chris Lattnera7674d82007-07-13 22:13:22 +00001091
Mike Stump24556362009-07-25 21:26:53 +00001092QualType ASTContext::getNoReturnType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00001093 QualType ResultType;
Mike Stump24556362009-07-25 21:26:53 +00001094 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001095 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001096 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001097 ResultType = getPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001098 } else if (T->isBlockPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001099 QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001100 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001101 ResultType = getBlockPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001102 } else {
1103 assert (T->isFunctionType()
1104 && "can't noreturn qualify non-pointer to function or block type");
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Benjamin Kramerbdbeeb52009-09-25 11:47:22 +00001106 if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) {
1107 ResultType = getFunctionNoProtoType(FNPT->getResultType(), true);
John McCall0953e762009-09-24 19:53:00 +00001108 } else {
1109 const FunctionProtoType *F = T->getAs<FunctionProtoType>();
1110 ResultType
1111 = getFunctionType(F->getResultType(), F->arg_type_begin(),
1112 F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
1113 F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
1114 F->getNumExceptions(), F->exception_begin(), true);
1115 }
Mike Stump24556362009-07-25 21:26:53 +00001116 }
John McCall0953e762009-09-24 19:53:00 +00001117
1118 return getQualifiedType(ResultType, T.getQualifiers());
Mike Stump24556362009-07-25 21:26:53 +00001119}
1120
Reid Spencer5f016e22007-07-11 17:01:13 +00001121/// getComplexType - Return the uniqued reference to the type for a complex
1122/// number with the specified element type.
1123QualType ASTContext::getComplexType(QualType T) {
1124 // Unique pointers, to guarantee there is only one pointer of a particular
1125 // structure.
1126 llvm::FoldingSetNodeID ID;
1127 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 void *InsertPos = 0;
1130 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1131 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Reid Spencer5f016e22007-07-11 17:01:13 +00001133 // If the pointee type isn't canonical, this won't be a canonical type either,
1134 // so fill in the canonical type field.
1135 QualType Canonical;
1136 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001137 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 // Get the new insert position for the node we care about.
1140 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001141 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 }
John McCall6b304a02009-09-24 23:30:46 +00001143 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 Types.push_back(New);
1145 ComplexTypes.InsertNode(New, InsertPos);
1146 return QualType(New, 0);
1147}
1148
Eli Friedmanf98aba32009-02-13 02:31:07 +00001149QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1150 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1151 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1152 FixedWidthIntType *&Entry = Map[Width];
1153 if (!Entry)
1154 Entry = new FixedWidthIntType(Width, Signed);
1155 return QualType(Entry, 0);
1156}
Reid Spencer5f016e22007-07-11 17:01:13 +00001157
1158/// getPointerType - Return the uniqued reference to the type for a pointer to
1159/// the specified type.
1160QualType ASTContext::getPointerType(QualType T) {
1161 // Unique pointers, to guarantee there is only one pointer of a particular
1162 // structure.
1163 llvm::FoldingSetNodeID ID;
1164 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 void *InsertPos = 0;
1167 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1168 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 // If the pointee type isn't canonical, this won't be a canonical type either,
1171 // so fill in the canonical type field.
1172 QualType Canonical;
1173 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001174 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 // Get the new insert position for the node we care about.
1177 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001178 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 }
John McCall6b304a02009-09-24 23:30:46 +00001180 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 Types.push_back(New);
1182 PointerTypes.InsertNode(New, InsertPos);
1183 return QualType(New, 0);
1184}
1185
Mike Stump1eb44332009-09-09 15:08:12 +00001186/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001187/// a pointer to the specified block.
1188QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001189 assert(T->isFunctionType() && "block of function types only");
1190 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001191 // structure.
1192 llvm::FoldingSetNodeID ID;
1193 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Steve Naroff5618bd42008-08-27 16:04:49 +00001195 void *InsertPos = 0;
1196 if (BlockPointerType *PT =
1197 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1198 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001199
1200 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001201 // type either so fill in the canonical type field.
1202 QualType Canonical;
1203 if (!T->isCanonical()) {
1204 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Steve Naroff5618bd42008-08-27 16:04:49 +00001206 // Get the new insert position for the node we care about.
1207 BlockPointerType *NewIP =
1208 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001209 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001210 }
John McCall6b304a02009-09-24 23:30:46 +00001211 BlockPointerType *New
1212 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001213 Types.push_back(New);
1214 BlockPointerTypes.InsertNode(New, InsertPos);
1215 return QualType(New, 0);
1216}
1217
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001218/// getLValueReferenceType - Return the uniqued reference to the type for an
1219/// lvalue reference to the specified type.
1220QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 // Unique pointers, to guarantee there is only one pointer of a particular
1222 // structure.
1223 llvm::FoldingSetNodeID ID;
1224 ReferenceType::Profile(ID, T);
1225
1226 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001227 if (LValueReferenceType *RT =
1228 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001230
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 // If the referencee type isn't canonical, this won't be a canonical type
1232 // either, so fill in the canonical type field.
1233 QualType Canonical;
1234 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001235 Canonical = getLValueReferenceType(getCanonicalType(T));
1236
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001238 LValueReferenceType *NewIP =
1239 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001240 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 }
1242
John McCall6b304a02009-09-24 23:30:46 +00001243 LValueReferenceType *New
1244 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001246 LValueReferenceTypes.InsertNode(New, InsertPos);
1247 return QualType(New, 0);
1248}
1249
1250/// getRValueReferenceType - Return the uniqued reference to the type for an
1251/// rvalue reference to the specified type.
1252QualType ASTContext::getRValueReferenceType(QualType T) {
1253 // Unique pointers, to guarantee there is only one pointer of a particular
1254 // structure.
1255 llvm::FoldingSetNodeID ID;
1256 ReferenceType::Profile(ID, T);
1257
1258 void *InsertPos = 0;
1259 if (RValueReferenceType *RT =
1260 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1261 return QualType(RT, 0);
1262
1263 // If the referencee type isn't canonical, this won't be a canonical type
1264 // either, so fill in the canonical type field.
1265 QualType Canonical;
1266 if (!T->isCanonical()) {
1267 Canonical = getRValueReferenceType(getCanonicalType(T));
1268
1269 // Get the new insert position for the node we care about.
1270 RValueReferenceType *NewIP =
1271 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1272 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1273 }
1274
John McCall6b304a02009-09-24 23:30:46 +00001275 RValueReferenceType *New
1276 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001277 Types.push_back(New);
1278 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 return QualType(New, 0);
1280}
1281
Sebastian Redlf30208a2009-01-24 21:16:55 +00001282/// getMemberPointerType - Return the uniqued reference to the type for a
1283/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001284QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001285 // Unique pointers, to guarantee there is only one pointer of a particular
1286 // structure.
1287 llvm::FoldingSetNodeID ID;
1288 MemberPointerType::Profile(ID, T, Cls);
1289
1290 void *InsertPos = 0;
1291 if (MemberPointerType *PT =
1292 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1293 return QualType(PT, 0);
1294
1295 // If the pointee or class type isn't canonical, this won't be a canonical
1296 // type either, so fill in the canonical type field.
1297 QualType Canonical;
1298 if (!T->isCanonical()) {
1299 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1300
1301 // Get the new insert position for the node we care about.
1302 MemberPointerType *NewIP =
1303 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1304 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1305 }
John McCall6b304a02009-09-24 23:30:46 +00001306 MemberPointerType *New
1307 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001308 Types.push_back(New);
1309 MemberPointerTypes.InsertNode(New, InsertPos);
1310 return QualType(New, 0);
1311}
1312
Mike Stump1eb44332009-09-09 15:08:12 +00001313/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001314/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001315QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001316 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001317 ArrayType::ArraySizeModifier ASM,
1318 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001319 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1320 "Constant array of VLAs is illegal!");
1321
Chris Lattner38aeec72009-05-13 04:12:56 +00001322 // Convert the array size into a canonical width matching the pointer size for
1323 // the target.
1324 llvm::APInt ArySize(ArySizeIn);
1325 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001328 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001331 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001332 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 // If the element type isn't canonical, this won't be a canonical type either,
1336 // so fill in the canonical type field.
1337 QualType Canonical;
1338 if (!EltTy->isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001339 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001340 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001342 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001343 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001344 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 }
Mike Stump1eb44332009-09-09 15:08:12 +00001346
John McCall6b304a02009-09-24 23:30:46 +00001347 ConstantArrayType *New = new(*this,TypeAlignment)
1348 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001349 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 Types.push_back(New);
1351 return QualType(New, 0);
1352}
1353
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001354/// getConstantArrayWithExprType - Return a reference to the type for
1355/// an array of the specified element type.
1356QualType
1357ASTContext::getConstantArrayWithExprType(QualType EltTy,
1358 const llvm::APInt &ArySizeIn,
1359 Expr *ArySizeExpr,
1360 ArrayType::ArraySizeModifier ASM,
1361 unsigned EltTypeQuals,
1362 SourceRange Brackets) {
1363 // Convert the array size into a canonical width matching the pointer
1364 // size for the target.
1365 llvm::APInt ArySize(ArySizeIn);
1366 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1367
1368 // Compute the canonical ConstantArrayType.
1369 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1370 ArySize, ASM, EltTypeQuals);
1371 // Since we don't unique expressions, it isn't possible to unique VLA's
1372 // that have an expression provided for their size.
John McCall6b304a02009-09-24 23:30:46 +00001373 ConstantArrayWithExprType *New = new(*this, TypeAlignment)
1374 ConstantArrayWithExprType(EltTy, Canonical, ArySize, ArySizeExpr,
1375 ASM, EltTypeQuals, Brackets);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001376 Types.push_back(New);
1377 return QualType(New, 0);
1378}
1379
1380/// getConstantArrayWithoutExprType - Return a reference to the type for
1381/// an array of the specified element type.
1382QualType
1383ASTContext::getConstantArrayWithoutExprType(QualType EltTy,
1384 const llvm::APInt &ArySizeIn,
1385 ArrayType::ArraySizeModifier ASM,
1386 unsigned EltTypeQuals) {
1387 // Convert the array size into a canonical width matching the pointer
1388 // size for the target.
1389 llvm::APInt ArySize(ArySizeIn);
1390 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1391
1392 // Compute the canonical ConstantArrayType.
1393 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1394 ArySize, ASM, EltTypeQuals);
John McCall6b304a02009-09-24 23:30:46 +00001395 ConstantArrayWithoutExprType *New = new(*this, TypeAlignment)
1396 ConstantArrayWithoutExprType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001397 Types.push_back(New);
1398 return QualType(New, 0);
1399}
1400
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001401/// getVariableArrayType - Returns a non-unique reference to the type for a
1402/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001403QualType ASTContext::getVariableArrayType(QualType EltTy,
1404 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001405 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001406 unsigned EltTypeQuals,
1407 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001408 // Since we don't unique expressions, it isn't possible to unique VLA's
1409 // that have an expression provided for their size.
1410
John McCall6b304a02009-09-24 23:30:46 +00001411 VariableArrayType *New = new(*this, TypeAlignment)
1412 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001413
1414 VariableArrayTypes.push_back(New);
1415 Types.push_back(New);
1416 return QualType(New, 0);
1417}
1418
Douglas Gregor898574e2008-12-05 23:32:09 +00001419/// getDependentSizedArrayType - Returns a non-unique reference to
1420/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001421/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001422QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1423 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001424 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001425 unsigned EltTypeQuals,
1426 SourceRange Brackets) {
Mike Stump1eb44332009-09-09 15:08:12 +00001427 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001428 "Size must be type- or value-dependent!");
1429
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001430 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001431 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001432 EltTypeQuals, NumElts);
Douglas Gregor898574e2008-12-05 23:32:09 +00001433
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001434 void *InsertPos = 0;
1435 DependentSizedArrayType *Canon
1436 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1437 DependentSizedArrayType *New;
1438 if (Canon) {
1439 // We already have a canonical version of this array type; use it as
1440 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001441 New = new (*this, TypeAlignment)
1442 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1443 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001444 } else {
1445 QualType CanonEltTy = getCanonicalType(EltTy);
1446 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001447 New = new (*this, TypeAlignment)
1448 DependentSizedArrayType(*this, EltTy, QualType(),
1449 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001450 DependentSizedArrayTypes.InsertNode(New, InsertPos);
1451 } else {
1452 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1453 ASM, EltTypeQuals,
1454 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001455 New = new (*this, TypeAlignment)
1456 DependentSizedArrayType(*this, EltTy, Canon,
1457 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001458 }
1459 }
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Douglas Gregor898574e2008-12-05 23:32:09 +00001461 Types.push_back(New);
1462 return QualType(New, 0);
1463}
1464
Eli Friedmanc5773c42008-02-15 18:16:39 +00001465QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1466 ArrayType::ArraySizeModifier ASM,
1467 unsigned EltTypeQuals) {
1468 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001469 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001470
1471 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001472 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001473 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1474 return QualType(ATP, 0);
1475
1476 // If the element type isn't canonical, this won't be a canonical type
1477 // either, so fill in the canonical type field.
1478 QualType Canonical;
1479
1480 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001481 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001482 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001483
1484 // Get the new insert position for the node we care about.
1485 IncompleteArrayType *NewIP =
1486 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001487 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001488 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001489
John McCall6b304a02009-09-24 23:30:46 +00001490 IncompleteArrayType *New = new (*this, TypeAlignment)
1491 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001492
1493 IncompleteArrayTypes.InsertNode(New, InsertPos);
1494 Types.push_back(New);
1495 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001496}
1497
Steve Naroff73322922007-07-18 18:00:27 +00001498/// getVectorType - Return the unique reference to a vector type of
1499/// the specified element type and size. VectorType must be a built-in type.
1500QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001501 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Chris Lattnerf52ab252008-04-06 22:59:24 +00001503 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001504 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Reid Spencer5f016e22007-07-11 17:01:13 +00001506 // Check if we've already instantiated a vector of this type.
1507 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001508 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001509 void *InsertPos = 0;
1510 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1511 return QualType(VTP, 0);
1512
1513 // If the element type isn't canonical, this won't be a canonical type either,
1514 // so fill in the canonical type field.
1515 QualType Canonical;
1516 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001517 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 // Get the new insert position for the node we care about.
1520 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001521 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001522 }
John McCall6b304a02009-09-24 23:30:46 +00001523 VectorType *New = new (*this, TypeAlignment)
1524 VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001525 VectorTypes.InsertNode(New, InsertPos);
1526 Types.push_back(New);
1527 return QualType(New, 0);
1528}
1529
Nate Begeman213541a2008-04-18 23:10:10 +00001530/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001531/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001532QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001533 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Chris Lattnerf52ab252008-04-06 22:59:24 +00001535 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001536 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Steve Naroff73322922007-07-18 18:00:27 +00001538 // Check if we've already instantiated a vector of this type.
1539 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001540 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001541 void *InsertPos = 0;
1542 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1543 return QualType(VTP, 0);
1544
1545 // If the element type isn't canonical, this won't be a canonical type either,
1546 // so fill in the canonical type field.
1547 QualType Canonical;
1548 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001549 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Steve Naroff73322922007-07-18 18:00:27 +00001551 // Get the new insert position for the node we care about.
1552 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001553 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001554 }
John McCall6b304a02009-09-24 23:30:46 +00001555 ExtVectorType *New = new (*this, TypeAlignment)
1556 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001557 VectorTypes.InsertNode(New, InsertPos);
1558 Types.push_back(New);
1559 return QualType(New, 0);
1560}
1561
Mike Stump1eb44332009-09-09 15:08:12 +00001562QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001563 Expr *SizeExpr,
1564 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001565 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001566 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001567 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001569 void *InsertPos = 0;
1570 DependentSizedExtVectorType *Canon
1571 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1572 DependentSizedExtVectorType *New;
1573 if (Canon) {
1574 // We already have a canonical version of this array type; use it as
1575 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001576 New = new (*this, TypeAlignment)
1577 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1578 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001579 } else {
1580 QualType CanonVecTy = getCanonicalType(vecType);
1581 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001582 New = new (*this, TypeAlignment)
1583 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1584 AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001585 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1586 } else {
1587 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1588 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001589 New = new (*this, TypeAlignment)
1590 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001591 }
1592 }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001594 Types.push_back(New);
1595 return QualType(New, 0);
1596}
1597
Douglas Gregor72564e72009-02-26 23:50:07 +00001598/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001599///
Mike Stump24556362009-07-25 21:26:53 +00001600QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001601 // Unique functions, to guarantee there is only one function of a particular
1602 // structure.
1603 llvm::FoldingSetNodeID ID;
Mike Stump24556362009-07-25 21:26:53 +00001604 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001607 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001608 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001609 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 QualType Canonical;
1612 if (!ResultTy->isCanonical()) {
Mike Stump24556362009-07-25 21:26:53 +00001613 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Reid Spencer5f016e22007-07-11 17:01:13 +00001615 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001616 FunctionNoProtoType *NewIP =
1617 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001618 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 }
Mike Stump1eb44332009-09-09 15:08:12 +00001620
John McCall6b304a02009-09-24 23:30:46 +00001621 FunctionNoProtoType *New = new (*this, TypeAlignment)
1622 FunctionNoProtoType(ResultTy, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001623 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001624 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001625 return QualType(New, 0);
1626}
1627
1628/// getFunctionType - Return a normal function type with a typed argument
1629/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001630QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001631 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001632 unsigned TypeQuals, bool hasExceptionSpec,
1633 bool hasAnyExceptionSpec, unsigned NumExs,
Mike Stump24556362009-07-25 21:26:53 +00001634 const QualType *ExArray, bool NoReturn) {
Anders Carlsson83913e32009-09-16 23:47:08 +00001635 if (LangOpts.CPlusPlus) {
1636 for (unsigned i = 0; i != NumArgs; ++i)
John McCall0953e762009-09-24 19:53:00 +00001637 assert(!ArgArray[i].hasQualifiers() &&
1638 "C++ arguments can't have toplevel qualifiers!");
Anders Carlsson83913e32009-09-16 23:47:08 +00001639 }
1640
Reid Spencer5f016e22007-07-11 17:01:13 +00001641 // Unique functions, to guarantee there is only one function of a particular
1642 // structure.
1643 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001644 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001645 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001646 NumExs, ExArray, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001647
1648 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001649 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001650 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001652
1653 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001654 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001655 if (hasExceptionSpec)
1656 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1658 if (!ArgArray[i]->isCanonical())
1659 isCanonical = false;
1660
1661 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001662 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001663 QualType Canonical;
1664 if (!isCanonical) {
1665 llvm::SmallVector<QualType, 16> CanonicalArgs;
1666 CanonicalArgs.reserve(NumArgs);
1667 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001668 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001669
Chris Lattnerf52ab252008-04-06 22:59:24 +00001670 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001671 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001672 isVariadic, TypeQuals, false,
1673 false, 0, 0, NoReturn);
Sebastian Redl465226e2009-05-27 22:11:52 +00001674
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001676 FunctionProtoType *NewIP =
1677 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001678 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001680
Douglas Gregor72564e72009-02-26 23:50:07 +00001681 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001682 // for two variable size arrays (for parameter and exception types) at the
1683 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001684 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001685 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1686 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001687 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001688 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001689 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001690 ExArray, NumExs, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001691 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001692 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001693 return QualType(FTP, 0);
1694}
1695
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001696/// getTypeDeclType - Return the unique reference to the type for the
1697/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001698QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001699 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001700 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001702 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001703 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001704 else if (isa<TemplateTypeParmDecl>(Decl)) {
1705 assert(false && "Template type parameter types are always available.");
Mike Stump9fdbab32009-07-31 02:02:20 +00001706 } else if (ObjCInterfaceDecl *ObjCInterface
1707 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001708 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001709
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001710 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001711 if (PrevDecl)
1712 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001713 else
John McCall6b304a02009-09-24 23:30:46 +00001714 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
Mike Stump9fdbab32009-07-31 02:02:20 +00001715 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001716 if (PrevDecl)
1717 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001718 else
John McCall6b304a02009-09-24 23:30:46 +00001719 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
Mike Stump9fdbab32009-07-31 02:02:20 +00001720 } else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001721 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001722
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001723 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001724 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001725}
1726
Reid Spencer5f016e22007-07-11 17:01:13 +00001727/// getTypedefType - Return the unique reference to the type for the
1728/// specified typename decl.
1729QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1730 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001731
Chris Lattnerf52ab252008-04-06 22:59:24 +00001732 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001733 Decl->TypeForDecl = new(*this, TypeAlignment)
1734 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001735 Types.push_back(Decl->TypeForDecl);
1736 return QualType(Decl->TypeForDecl, 0);
1737}
1738
Douglas Gregorfab9d672009-02-05 23:33:38 +00001739/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001740/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001741/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001742QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001743 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001744 IdentifierInfo *Name) {
1745 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001746 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001747 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001748 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001749 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1750
1751 if (TypeParm)
1752 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001754 if (Name) {
1755 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall6b304a02009-09-24 23:30:46 +00001756 TypeParm = new (*this, TypeAlignment)
1757 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001758 } else
John McCall6b304a02009-09-24 23:30:46 +00001759 TypeParm = new (*this, TypeAlignment)
1760 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001761
1762 Types.push_back(TypeParm);
1763 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1764
1765 return QualType(TypeParm, 0);
1766}
1767
Mike Stump1eb44332009-09-09 15:08:12 +00001768QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001769ASTContext::getTemplateSpecializationType(TemplateName Template,
1770 const TemplateArgument *Args,
1771 unsigned NumArgs,
1772 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001773 if (!Canon.isNull())
1774 Canon = getCanonicalType(Canon);
1775 else {
1776 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001777 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1778 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1779 CanonArgs.reserve(NumArgs);
1780 for (unsigned I = 0; I != NumArgs; ++I)
1781 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1782
1783 // Determine whether this canonical template specialization type already
1784 // exists.
1785 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001786 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001787 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001788
1789 void *InsertPos = 0;
1790 TemplateSpecializationType *Spec
1791 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Douglas Gregor1275ae02009-07-28 23:00:59 +00001793 if (!Spec) {
1794 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001795 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001796 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001797 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001798 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001799 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001800 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001801 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001802 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001803 }
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Douglas Gregorb88e8882009-07-30 17:40:51 +00001805 if (Canon.isNull())
1806 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001807 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001808 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001809 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001810
Douglas Gregor1275ae02009-07-28 23:00:59 +00001811 // Allocate the (non-canonical) template specialization type, but don't
1812 // try to unique it: these types typically have location information that
1813 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001814 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001815 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001816 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001817 TemplateSpecializationType *Spec
1818 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001819 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Douglas Gregor55f6b142009-02-09 18:46:07 +00001821 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001822 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001823}
1824
Mike Stump1eb44332009-09-09 15:08:12 +00001825QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001826ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001827 QualType NamedType) {
1828 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001829 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001830
1831 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001832 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001833 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1834 if (T)
1835 return QualType(T, 0);
1836
Mike Stump1eb44332009-09-09 15:08:12 +00001837 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001838 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001839 Types.push_back(T);
1840 QualifiedNameTypes.InsertNode(T, InsertPos);
1841 return QualType(T, 0);
1842}
1843
Mike Stump1eb44332009-09-09 15:08:12 +00001844QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001845 const IdentifierInfo *Name,
1846 QualType Canon) {
1847 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1848
1849 if (Canon.isNull()) {
1850 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1851 if (CanonNNS != NNS)
1852 Canon = getTypenameType(CanonNNS, Name);
1853 }
1854
1855 llvm::FoldingSetNodeID ID;
1856 TypenameType::Profile(ID, NNS, Name);
1857
1858 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001859 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001860 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1861 if (T)
1862 return QualType(T, 0);
1863
1864 T = new (*this) TypenameType(NNS, Name, Canon);
1865 Types.push_back(T);
1866 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001867 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001868}
1869
Mike Stump1eb44332009-09-09 15:08:12 +00001870QualType
1871ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001872 const TemplateSpecializationType *TemplateId,
1873 QualType Canon) {
1874 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1875
1876 if (Canon.isNull()) {
1877 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1878 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1879 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1880 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00001881 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00001882 assert(CanonTemplateId &&
1883 "Canonical type must also be a template specialization type");
1884 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1885 }
1886 }
1887
1888 llvm::FoldingSetNodeID ID;
1889 TypenameType::Profile(ID, NNS, TemplateId);
1890
1891 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001892 TypenameType *T
Douglas Gregor17343172009-04-01 00:28:59 +00001893 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1894 if (T)
1895 return QualType(T, 0);
1896
1897 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1898 Types.push_back(T);
1899 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001900 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00001901}
1902
John McCall7da24312009-09-05 00:15:47 +00001903QualType
1904ASTContext::getElaboratedType(QualType UnderlyingType,
1905 ElaboratedType::TagKind Tag) {
1906 llvm::FoldingSetNodeID ID;
1907 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001908
John McCall7da24312009-09-05 00:15:47 +00001909 void *InsertPos = 0;
1910 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1911 if (T)
1912 return QualType(T, 0);
1913
1914 QualType Canon = getCanonicalType(UnderlyingType);
1915
1916 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1917 Types.push_back(T);
1918 ElaboratedTypes.InsertNode(T, InsertPos);
1919 return QualType(T, 0);
1920}
1921
Chris Lattner88cb27a2008-04-07 04:56:42 +00001922/// CmpProtocolNames - Comparison predicate for sorting protocols
1923/// alphabetically.
1924static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1925 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001926 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001927}
1928
1929static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1930 unsigned &NumProtocols) {
1931 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Chris Lattner88cb27a2008-04-07 04:56:42 +00001933 // Sort protocols, keyed by name.
1934 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1935
1936 // Remove duplicates.
1937 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1938 NumProtocols = ProtocolsEnd-Protocols;
1939}
1940
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001941/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1942/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00001943QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00001944 ObjCProtocolDecl **Protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001945 unsigned NumProtocols) {
1946 // Sort the protocol list alphabetically to canonicalize it.
1947 if (NumProtocols)
1948 SortAndUniqueProtocols(Protocols, NumProtocols);
1949
1950 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00001951 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001952
1953 void *InsertPos = 0;
1954 if (ObjCObjectPointerType *QT =
1955 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1956 return QualType(QT, 0);
1957
1958 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001959 ObjCObjectPointerType *QType = new (*this, TypeAlignment)
1960 ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001962 Types.push_back(QType);
1963 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1964 return QualType(QType, 0);
1965}
Chris Lattner88cb27a2008-04-07 04:56:42 +00001966
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001967/// getObjCInterfaceType - Return the unique reference to the type for the
1968/// specified ObjC interface decl. The list of protocols is optional.
1969QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001970 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Mike Stump1eb44332009-09-09 15:08:12 +00001971 if (NumProtocols)
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001972 // Sort the protocol list alphabetically to canonicalize it.
1973 SortAndUniqueProtocols(Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001975 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001976 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001978 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001979 if (ObjCInterfaceType *QT =
1980 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001981 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001982
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001983 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001984 ObjCInterfaceType *QType = new (*this, TypeAlignment)
1985 ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1986 Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001987 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001988 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001989 return QualType(QType, 0);
1990}
1991
Douglas Gregor72564e72009-02-26 23:50:07 +00001992/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1993/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001994/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00001995/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00001996/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001997QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00001998 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00001999 if (tofExpr->isTypeDependent()) {
2000 llvm::FoldingSetNodeID ID;
2001 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002002
Douglas Gregorb1975722009-07-30 23:18:24 +00002003 void *InsertPos = 0;
2004 DependentTypeOfExprType *Canon
2005 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2006 if (Canon) {
2007 // We already have a "canonical" version of an identical, dependent
2008 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002009 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002010 QualType((TypeOfExprType*)Canon, 0));
2011 }
2012 else {
2013 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002014 Canon
2015 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002016 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2017 toe = Canon;
2018 }
2019 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002020 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002021 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002022 }
Steve Naroff9752f252007-08-01 18:02:17 +00002023 Types.push_back(toe);
2024 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002025}
2026
Steve Naroff9752f252007-08-01 18:02:17 +00002027/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2028/// TypeOfType AST's. The only motivation to unique these nodes would be
2029/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002030/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002031/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002032QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002033 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002034 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002035 Types.push_back(tot);
2036 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002037}
2038
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002039/// getDecltypeForExpr - Given an expr, will return the decltype for that
2040/// expression, according to the rules in C++0x [dcl.type.simple]p4
2041static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002042 if (e->isTypeDependent())
2043 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002045 // If e is an id expression or a class member access, decltype(e) is defined
2046 // as the type of the entity named by e.
2047 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2048 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2049 return VD->getType();
2050 }
2051 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2052 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2053 return FD->getType();
2054 }
2055 // If e is a function call or an invocation of an overloaded operator,
2056 // (parentheses around e are ignored), decltype(e) is defined as the
2057 // return type of that function.
2058 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2059 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002060
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002061 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002062
2063 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002064 // defined as T&, otherwise decltype(e) is defined as T.
2065 if (e->isLvalue(Context) == Expr::LV_Valid)
2066 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002068 return T;
2069}
2070
Anders Carlsson395b4752009-06-24 19:06:50 +00002071/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2072/// DecltypeType AST's. The only motivation to unique these nodes would be
2073/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002074/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002075/// on canonical type's (which are always unique).
2076QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002077 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002078 if (e->isTypeDependent()) {
2079 llvm::FoldingSetNodeID ID;
2080 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002082 void *InsertPos = 0;
2083 DependentDecltypeType *Canon
2084 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2085 if (Canon) {
2086 // We already have a "canonical" version of an equivalent, dependent
2087 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002088 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002089 QualType((DecltypeType*)Canon, 0));
2090 }
2091 else {
2092 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002093 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002094 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2095 dt = Canon;
2096 }
2097 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002098 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002099 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002100 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002101 Types.push_back(dt);
2102 return QualType(dt, 0);
2103}
2104
Reid Spencer5f016e22007-07-11 17:01:13 +00002105/// getTagDeclType - Return the unique reference to the type for the
2106/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002107QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002108 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002109 // FIXME: What is the design on getTagDeclType when it requires casting
2110 // away const? mutable?
2111 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002112}
2113
Mike Stump1eb44332009-09-09 15:08:12 +00002114/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2115/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2116/// needs to agree with the definition in <stddef.h>.
Reid Spencer5f016e22007-07-11 17:01:13 +00002117QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002118 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002119}
2120
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002121/// getSignedWCharType - Return the type of "signed wchar_t".
2122/// Used when in C++, as a GCC extension.
2123QualType ASTContext::getSignedWCharType() const {
2124 // FIXME: derive from "Target" ?
2125 return WCharTy;
2126}
2127
2128/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2129/// Used when in C++, as a GCC extension.
2130QualType ASTContext::getUnsignedWCharType() const {
2131 // FIXME: derive from "Target" ?
2132 return UnsignedIntTy;
2133}
2134
Chris Lattner8b9023b2007-07-13 03:05:23 +00002135/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2136/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2137QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002138 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002139}
2140
Chris Lattnere6327742008-04-02 05:18:44 +00002141//===----------------------------------------------------------------------===//
2142// Type Operators
2143//===----------------------------------------------------------------------===//
2144
Chris Lattner77c96472008-04-06 22:41:35 +00002145/// getCanonicalType - Return the canonical (structural) type corresponding to
2146/// the specified potentially non-canonical type. The non-canonical version
2147/// of a type may have many "decorated" versions of types. Decorators can
2148/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2149/// to be free of any of these, allowing two canonical types to be compared
2150/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002151CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002152 QualifierCollector Quals;
2153 const Type *Ptr = Quals.strip(T);
2154 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002155
John McCall0953e762009-09-24 19:53:00 +00002156 // The canonical internal type will be the canonical type *except*
2157 // that we push type qualifiers down through array types.
2158
2159 // If there are no new qualifiers to push down, stop here.
2160 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002161 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002162
John McCall0953e762009-09-24 19:53:00 +00002163 // If the type qualifiers are on an array type, get the canonical
2164 // type of the array with the qualifiers applied to the element
2165 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002166 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2167 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002168 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002169
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002170 // Get the canonical version of the element with the extra qualifiers on it.
2171 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002172 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002173 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002174
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002175 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002176 return CanQualType::CreateUnsafe(
2177 getConstantArrayType(NewEltTy, CAT->getSize(),
2178 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002179 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002180 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002181 return CanQualType::CreateUnsafe(
2182 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002183 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002184
Douglas Gregor898574e2008-12-05 23:32:09 +00002185 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002186 return CanQualType::CreateUnsafe(
2187 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002188 DSAT->getSizeExpr() ?
2189 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002190 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002191 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002192 DSAT->getBracketsRange()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002193
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002194 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002195 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002196 VAT->getSizeExpr() ?
2197 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002198 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002199 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002200 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002201}
2202
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002203TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2204 // If this template name refers to a template, the canonical
2205 // template name merely stores the template itself.
2206 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002207 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002208
Mike Stump1eb44332009-09-09 15:08:12 +00002209 // If this template name refers to a set of overloaded function templates,
Douglas Gregord99cbe62009-07-29 18:26:50 +00002210 /// the canonical template name merely stores the set of function templates.
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002211 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2212 OverloadedFunctionDecl *CanonOvl = 0;
2213 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2214 FEnd = Ovl->function_end();
2215 F != FEnd; ++F) {
2216 Decl *Canon = F->get()->getCanonicalDecl();
2217 if (CanonOvl || Canon != F->get()) {
2218 if (!CanonOvl)
Mike Stump1eb44332009-09-09 15:08:12 +00002219 CanonOvl = OverloadedFunctionDecl::Create(*this,
2220 Ovl->getDeclContext(),
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002221 Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00002222
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002223 CanonOvl->addOverload(
2224 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2225 }
2226 }
Mike Stump1eb44332009-09-09 15:08:12 +00002227
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002228 return TemplateName(CanonOvl? CanonOvl : Ovl);
2229 }
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002231 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2232 assert(DTN && "Non-dependent template names must refer to template decls.");
2233 return DTN->CanonicalTemplateName;
2234}
2235
Mike Stump1eb44332009-09-09 15:08:12 +00002236TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002237ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2238 switch (Arg.getKind()) {
2239 case TemplateArgument::Null:
2240 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002241
Douglas Gregor1275ae02009-07-28 23:00:59 +00002242 case TemplateArgument::Expression:
2243 // FIXME: Build canonical expression?
2244 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Douglas Gregor1275ae02009-07-28 23:00:59 +00002246 case TemplateArgument::Declaration:
2247 return TemplateArgument(SourceLocation(),
2248 Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002249
Douglas Gregor1275ae02009-07-28 23:00:59 +00002250 case TemplateArgument::Integral:
2251 return TemplateArgument(SourceLocation(),
2252 *Arg.getAsIntegral(),
2253 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002254
Douglas Gregor1275ae02009-07-28 23:00:59 +00002255 case TemplateArgument::Type:
2256 return TemplateArgument(SourceLocation(),
2257 getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002258
Douglas Gregor1275ae02009-07-28 23:00:59 +00002259 case TemplateArgument::Pack: {
2260 // FIXME: Allocate in ASTContext
2261 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2262 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002263 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002264 AEnd = Arg.pack_end();
2265 A != AEnd; (void)++A, ++Idx)
2266 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Douglas Gregor1275ae02009-07-28 23:00:59 +00002268 TemplateArgument Result;
2269 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2270 return Result;
2271 }
2272 }
2273
2274 // Silence GCC warning
2275 assert(false && "Unhandled template argument kind");
2276 return TemplateArgument();
2277}
2278
Douglas Gregord57959a2009-03-27 23:10:48 +00002279NestedNameSpecifier *
2280ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002281 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002282 return 0;
2283
2284 switch (NNS->getKind()) {
2285 case NestedNameSpecifier::Identifier:
2286 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002287 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002288 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2289 NNS->getAsIdentifier());
2290
2291 case NestedNameSpecifier::Namespace:
2292 // A namespace is canonical; build a nested-name-specifier with
2293 // this namespace and no prefix.
2294 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2295
2296 case NestedNameSpecifier::TypeSpec:
2297 case NestedNameSpecifier::TypeSpecWithTemplate: {
2298 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002299 return NestedNameSpecifier::Create(*this, 0,
2300 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002301 T.getTypePtr());
2302 }
2303
2304 case NestedNameSpecifier::Global:
2305 // The global specifier is canonical and unique.
2306 return NNS;
2307 }
2308
2309 // Required to silence a GCC warning
2310 return 0;
2311}
2312
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002313
2314const ArrayType *ASTContext::getAsArrayType(QualType T) {
2315 // Handle the non-qualified case efficiently.
John McCall0953e762009-09-24 19:53:00 +00002316 if (!T.hasQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002317 // Handle the common positive case fast.
2318 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2319 return AT;
2320 }
Mike Stump1eb44332009-09-09 15:08:12 +00002321
John McCall0953e762009-09-24 19:53:00 +00002322 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002323 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002324 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002325 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002326
John McCall0953e762009-09-24 19:53:00 +00002327 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002328 // implements C99 6.7.3p8: "If the specification of an array type includes
2329 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002330
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002331 // If we get here, we either have type qualifiers on the type, or we have
2332 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002333 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002334
John McCall0953e762009-09-24 19:53:00 +00002335 QualifierCollector Qs;
2336 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002337
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002338 // If we have a simple case, just return now.
2339 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002340 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002341 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002343 // Otherwise, we have an array and we have qualifiers on it. Push the
2344 // qualifiers into the array element type and return a new array type.
2345 // Get the canonical version of the element with the extra qualifiers on it.
2346 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002347 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002348
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002349 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2350 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2351 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002352 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002353 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2354 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2355 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002356 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002357
Mike Stump1eb44332009-09-09 15:08:12 +00002358 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002359 = dyn_cast<DependentSizedArrayType>(ATy))
2360 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002361 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002362 DSAT->getSizeExpr() ?
2363 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002364 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002365 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002366 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002367
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002368 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002369 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002370 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002371 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002372 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002373 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002374 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002375}
2376
2377
Chris Lattnere6327742008-04-02 05:18:44 +00002378/// getArrayDecayedType - Return the properly qualified result of decaying the
2379/// specified array type to a pointer. This operation is non-trivial when
2380/// handling typedefs etc. The canonical type of "T" must be an array type,
2381/// this returns a pointer to a properly qualified element of the array.
2382///
2383/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2384QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002385 // Get the element type with 'getAsArrayType' so that we don't lose any
2386 // typedefs in the element type of the array. This also handles propagation
2387 // of type qualifiers from the array type into the element type if present
2388 // (C99 6.7.3p8).
2389 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2390 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002391
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002392 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002393
2394 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002395 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002396}
2397
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002398QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002399 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002400 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002401 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002402 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2403 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002404 } else {
John McCall0953e762009-09-24 19:53:00 +00002405 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002406 }
2407 }
2408}
2409
Anders Carlssonfbbce492009-09-25 01:23:32 +00002410QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2411 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002412
Anders Carlssonfbbce492009-09-25 01:23:32 +00002413 if (const ArrayType *AT = getAsArrayType(ElemTy))
2414 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Anders Carlsson6183a992008-12-21 03:44:36 +00002416 return ElemTy;
2417}
2418
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002419/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002420uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002421ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2422 uint64_t ElementCount = 1;
2423 do {
2424 ElementCount *= CA->getSize().getZExtValue();
2425 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2426 } while (CA);
2427 return ElementCount;
2428}
2429
Reid Spencer5f016e22007-07-11 17:01:13 +00002430/// getFloatingRank - Return a relative rank for floating point types.
2431/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002432static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002433 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002434 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002435
John McCall183700f2009-09-21 23:43:11 +00002436 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2437 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002438 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002439 case BuiltinType::Float: return FloatRank;
2440 case BuiltinType::Double: return DoubleRank;
2441 case BuiltinType::LongDouble: return LongDoubleRank;
2442 }
2443}
2444
Mike Stump1eb44332009-09-09 15:08:12 +00002445/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2446/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002447/// 'typeDomain' is a real floating point or complex type.
2448/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002449QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2450 QualType Domain) const {
2451 FloatingRank EltRank = getFloatingRank(Size);
2452 if (Domain->isComplexType()) {
2453 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002454 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002455 case FloatRank: return FloatComplexTy;
2456 case DoubleRank: return DoubleComplexTy;
2457 case LongDoubleRank: return LongDoubleComplexTy;
2458 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002459 }
Chris Lattner1361b112008-04-06 23:58:54 +00002460
2461 assert(Domain->isRealFloatingType() && "Unknown domain!");
2462 switch (EltRank) {
2463 default: assert(0 && "getFloatingRank(): illegal value for rank");
2464 case FloatRank: return FloatTy;
2465 case DoubleRank: return DoubleTy;
2466 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002467 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002468}
2469
Chris Lattner7cfeb082008-04-06 23:55:33 +00002470/// getFloatingTypeOrder - Compare the rank of the two specified floating
2471/// point types, ignoring the domain of the type (i.e. 'double' ==
2472/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002473/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002474int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2475 FloatingRank LHSR = getFloatingRank(LHS);
2476 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002477
Chris Lattnera75cea32008-04-06 23:38:49 +00002478 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002479 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002480 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002481 return 1;
2482 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002483}
2484
Chris Lattnerf52ab252008-04-06 22:59:24 +00002485/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2486/// routine will assert if passed a built-in type that isn't an integer or enum,
2487/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002488unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002489 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002490 if (EnumType* ET = dyn_cast<EnumType>(T))
2491 T = ET->getDecl()->getIntegerType().getTypePtr();
2492
Eli Friedmana3426752009-07-05 23:44:27 +00002493 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2494 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2495
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002496 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2497 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2498
2499 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2500 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2501
Eli Friedmanf98aba32009-02-13 02:31:07 +00002502 // There are two things which impact the integer rank: the width, and
2503 // the ordering of builtins. The builtin ordering is encoded in the
2504 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner1b63e4f2009-06-14 01:54:56 +00002505 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanf98aba32009-02-13 02:31:07 +00002506 return FWIT->getWidth() << 3;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002507
Chris Lattnerf52ab252008-04-06 22:59:24 +00002508 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002509 default: assert(0 && "getIntegerRank(): not a built-in integer");
2510 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002511 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002512 case BuiltinType::Char_S:
2513 case BuiltinType::Char_U:
2514 case BuiltinType::SChar:
2515 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002516 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002517 case BuiltinType::Short:
2518 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002519 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002520 case BuiltinType::Int:
2521 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002522 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002523 case BuiltinType::Long:
2524 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002525 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002526 case BuiltinType::LongLong:
2527 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002528 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002529 case BuiltinType::Int128:
2530 case BuiltinType::UInt128:
2531 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002532 }
2533}
2534
Eli Friedman04e83572009-08-20 04:21:42 +00002535/// \brief Whether this is a promotable bitfield reference according
2536/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2537///
2538/// \returns the type this bit-field will promote to, or NULL if no
2539/// promotion occurs.
2540QualType ASTContext::isPromotableBitField(Expr *E) {
2541 FieldDecl *Field = E->getBitField();
2542 if (!Field)
2543 return QualType();
2544
2545 QualType FT = Field->getType();
2546
2547 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2548 uint64_t BitWidth = BitWidthAP.getZExtValue();
2549 uint64_t IntSize = getTypeSize(IntTy);
2550 // GCC extension compatibility: if the bit-field size is less than or equal
2551 // to the size of int, it gets promoted no matter what its type is.
2552 // For instance, unsigned long bf : 4 gets promoted to signed int.
2553 if (BitWidth < IntSize)
2554 return IntTy;
2555
2556 if (BitWidth == IntSize)
2557 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2558
2559 // Types bigger than int are not subject to promotions, and therefore act
2560 // like the base type.
2561 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2562 // is ridiculous.
2563 return QualType();
2564}
2565
Eli Friedmana95d7572009-08-19 07:44:53 +00002566/// getPromotedIntegerType - Returns the type that Promotable will
2567/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2568/// integer type.
2569QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2570 assert(!Promotable.isNull());
2571 assert(Promotable->isPromotableIntegerType());
2572 if (Promotable->isSignedIntegerType())
2573 return IntTy;
2574 uint64_t PromotableSize = getTypeSize(Promotable);
2575 uint64_t IntSize = getTypeSize(IntTy);
2576 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2577 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2578}
2579
Mike Stump1eb44332009-09-09 15:08:12 +00002580/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002581/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002582/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002583int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002584 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2585 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002586 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Chris Lattnerf52ab252008-04-06 22:59:24 +00002588 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2589 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002590
Chris Lattner7cfeb082008-04-06 23:55:33 +00002591 unsigned LHSRank = getIntegerRank(LHSC);
2592 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002593
Chris Lattner7cfeb082008-04-06 23:55:33 +00002594 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2595 if (LHSRank == RHSRank) return 0;
2596 return LHSRank > RHSRank ? 1 : -1;
2597 }
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Chris Lattner7cfeb082008-04-06 23:55:33 +00002599 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2600 if (LHSUnsigned) {
2601 // If the unsigned [LHS] type is larger, return it.
2602 if (LHSRank >= RHSRank)
2603 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002604
Chris Lattner7cfeb082008-04-06 23:55:33 +00002605 // If the signed type can represent all values of the unsigned type, it
2606 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002607 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002608 return -1;
2609 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002610
Chris Lattner7cfeb082008-04-06 23:55:33 +00002611 // If the unsigned [RHS] type is larger, return it.
2612 if (RHSRank >= LHSRank)
2613 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002614
Chris Lattner7cfeb082008-04-06 23:55:33 +00002615 // If the signed type can represent all values of the unsigned type, it
2616 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002617 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002618 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002619}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002620
Mike Stump1eb44332009-09-09 15:08:12 +00002621// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002622QualType ASTContext::getCFConstantStringType() {
2623 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002624 CFConstantStringTypeDecl =
2625 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002626 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002627 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Anders Carlsson71993dd2007-08-17 05:31:46 +00002629 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002630 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002631 // int flags;
2632 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002633 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002634 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002635 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002636 FieldTypes[3] = LongTy;
2637
Anders Carlsson71993dd2007-08-17 05:31:46 +00002638 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002639 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002640 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002641 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002642 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002643 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002644 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002645 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002646 }
2647
2648 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002649 }
Mike Stump1eb44332009-09-09 15:08:12 +00002650
Anders Carlsson71993dd2007-08-17 05:31:46 +00002651 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002652}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002653
Douglas Gregor319ac892009-04-23 22:29:11 +00002654void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002655 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002656 assert(Rec && "Invalid CFConstantStringType");
2657 CFConstantStringTypeDecl = Rec->getDecl();
2658}
2659
Mike Stump1eb44332009-09-09 15:08:12 +00002660QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002661 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002662 ObjCFastEnumerationStateTypeDecl =
2663 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2664 &Idents.get("__objcFastEnumerationState"));
Mike Stump1eb44332009-09-09 15:08:12 +00002665
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002666 QualType FieldTypes[] = {
2667 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002668 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002669 getPointerType(UnsignedLongTy),
2670 getConstantArrayType(UnsignedLongTy,
2671 llvm::APInt(32, 5), ArrayType::Normal, 0)
2672 };
Mike Stump1eb44332009-09-09 15:08:12 +00002673
Douglas Gregor44b43212008-12-11 16:49:14 +00002674 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002675 FieldDecl *Field = FieldDecl::Create(*this,
2676 ObjCFastEnumerationStateTypeDecl,
2677 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002678 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002679 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002680 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002681 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002682 }
Mike Stump1eb44332009-09-09 15:08:12 +00002683
Douglas Gregor44b43212008-12-11 16:49:14 +00002684 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002685 }
Mike Stump1eb44332009-09-09 15:08:12 +00002686
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002687 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2688}
2689
Douglas Gregor319ac892009-04-23 22:29:11 +00002690void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002691 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002692 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2693 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2694}
2695
Anders Carlssone8c49532007-10-29 06:33:42 +00002696// This returns true if a type has been typedefed to BOOL:
2697// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002698static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002699 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002700 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2701 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00002702
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002703 return false;
2704}
2705
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002706/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002707/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002708int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002709 uint64_t sz = getTypeSize(type);
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002711 // Make all integer and enum types at least as large as an int
2712 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002713 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002714 // Treat arrays as pointers, since that's how they're passed in.
2715 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002716 sz = getTypeSize(VoidPtrTy);
2717 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002718}
2719
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002720/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002721/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002722void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002723 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002724 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002725 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002726 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002727 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002728 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002729 // Compute size of all parameters.
2730 // Start with computing size of a pointer in number of bytes.
2731 // FIXME: There might(should) be a better way of doing this computation!
2732 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002733 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002734 // The first two arguments (self and _cmd) are pointers; account for
2735 // their size.
2736 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002737 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2738 E = Decl->param_end(); PI != E; ++PI) {
2739 QualType PType = (*PI)->getType();
2740 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002741 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002742 ParmOffset += sz;
2743 }
2744 S += llvm::utostr(ParmOffset);
2745 S += "@0:";
2746 S += llvm::utostr(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00002747
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002748 // Argument types.
2749 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002750 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2751 E = Decl->param_end(); PI != E; ++PI) {
2752 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00002753 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002754 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002755 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2756 // Use array's original type only if it has known number of
2757 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002758 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002759 PType = PVDecl->getType();
2760 } else if (PType->isFunctionType())
2761 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002762 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002763 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002764 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002765 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002766 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002767 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002768 }
2769}
2770
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002771/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002772/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002773/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2774/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00002775/// Property attributes are stored as a comma-delimited C string. The simple
2776/// attributes readonly and bycopy are encoded as single characters. The
2777/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2778/// encoded as single characters, followed by an identifier. Property types
2779/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002780/// these attributes are defined by the following enumeration:
2781/// @code
2782/// enum PropertyAttributes {
2783/// kPropertyReadOnly = 'R', // property is read-only.
2784/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2785/// kPropertyByref = '&', // property is a reference to the value last assigned
2786/// kPropertyDynamic = 'D', // property is dynamic
2787/// kPropertyGetter = 'G', // followed by getter selector name
2788/// kPropertySetter = 'S', // followed by setter selector name
2789/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2790/// kPropertyType = 't' // followed by old-style type encoding.
2791/// kPropertyWeak = 'W' // 'weak' property
2792/// kPropertyStrong = 'P' // property GC'able
2793/// kPropertyNonAtomic = 'N' // property non-atomic
2794/// };
2795/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00002796void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002797 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002798 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002799 // Collect information from the property implementation decl(s).
2800 bool Dynamic = false;
2801 ObjCPropertyImplDecl *SynthesizePID = 0;
2802
2803 // FIXME: Duplicated code due to poor abstraction.
2804 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00002805 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002806 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2807 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002808 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002809 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002810 ObjCPropertyImplDecl *PID = *i;
2811 if (PID->getPropertyDecl() == PD) {
2812 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2813 Dynamic = true;
2814 } else {
2815 SynthesizePID = PID;
2816 }
2817 }
2818 }
2819 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002820 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002821 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002822 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002823 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002824 ObjCPropertyImplDecl *PID = *i;
2825 if (PID->getPropertyDecl() == PD) {
2826 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2827 Dynamic = true;
2828 } else {
2829 SynthesizePID = PID;
2830 }
2831 }
Mike Stump1eb44332009-09-09 15:08:12 +00002832 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002833 }
2834 }
2835
2836 // FIXME: This is not very efficient.
2837 S = "T";
2838
2839 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002840 // GCC has some special rules regarding encoding of properties which
2841 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00002842 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002843 true /* outermost type */,
2844 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002845
2846 if (PD->isReadOnly()) {
2847 S += ",R";
2848 } else {
2849 switch (PD->getSetterKind()) {
2850 case ObjCPropertyDecl::Assign: break;
2851 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002852 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002853 }
2854 }
2855
2856 // It really isn't clear at all what this means, since properties
2857 // are "dynamic by default".
2858 if (Dynamic)
2859 S += ",D";
2860
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002861 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2862 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00002863
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002864 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2865 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002866 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002867 }
2868
2869 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2870 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002871 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002872 }
2873
2874 if (SynthesizePID) {
2875 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2876 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002877 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002878 }
2879
2880 // FIXME: OBJCGC: weak & strong
2881}
2882
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002883/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00002884/// Another legacy compatibility encoding: 32-bit longs are encoded as
2885/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002886/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2887///
2888void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00002889 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00002890 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002891 if (BT->getKind() == BuiltinType::ULong &&
2892 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002893 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002894 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002895 if (BT->getKind() == BuiltinType::Long &&
2896 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002897 PointeeTy = IntTy;
2898 }
2899 }
2900}
2901
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002902void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002903 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002904 // We follow the behavior of gcc, expanding structures which are
2905 // directly pointed to, and expanding embedded structures. Note that
2906 // these rules are sufficient to prevent recursive encoding of the
2907 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00002908 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002909 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002910}
2911
Mike Stump1eb44332009-09-09 15:08:12 +00002912static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002913 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002914 const Expr *E = FD->getBitWidth();
2915 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2916 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002917 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002918 S += 'b';
2919 S += llvm::utostr(N);
2920}
2921
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002922void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2923 bool ExpandPointedToStructures,
2924 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002925 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002926 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002927 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00002928 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002929 if (FD && FD->isBitField())
2930 return EncodeBitField(this, S, FD);
2931 char encoding;
2932 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002933 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002934 case BuiltinType::Void: encoding = 'v'; break;
2935 case BuiltinType::Bool: encoding = 'B'; break;
2936 case BuiltinType::Char_U:
2937 case BuiltinType::UChar: encoding = 'C'; break;
2938 case BuiltinType::UShort: encoding = 'S'; break;
2939 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002940 case BuiltinType::ULong:
2941 encoding =
2942 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002943 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002944 case BuiltinType::UInt128: encoding = 'T'; break;
2945 case BuiltinType::ULongLong: encoding = 'Q'; break;
2946 case BuiltinType::Char_S:
2947 case BuiltinType::SChar: encoding = 'c'; break;
2948 case BuiltinType::Short: encoding = 's'; break;
2949 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002950 case BuiltinType::Long:
2951 encoding =
2952 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002953 break;
2954 case BuiltinType::LongLong: encoding = 'q'; break;
2955 case BuiltinType::Int128: encoding = 't'; break;
2956 case BuiltinType::Float: encoding = 'f'; break;
2957 case BuiltinType::Double: encoding = 'd'; break;
2958 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002959 }
Mike Stump1eb44332009-09-09 15:08:12 +00002960
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002961 S += encoding;
2962 return;
2963 }
Mike Stump1eb44332009-09-09 15:08:12 +00002964
John McCall183700f2009-09-21 23:43:11 +00002965 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002966 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00002967 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002968 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002969 return;
2970 }
Mike Stump1eb44332009-09-09 15:08:12 +00002971
Ted Kremenek6217b802009-07-29 21:53:49 +00002972 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002973 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002974 bool isReadOnly = false;
2975 // For historical/compatibility reasons, the read-only qualifier of the
2976 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2977 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00002978 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00002979 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002980 if (OutermostType && T.isConstQualified()) {
2981 isReadOnly = true;
2982 S += 'r';
2983 }
Mike Stump9fdbab32009-07-31 02:02:20 +00002984 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002985 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00002986 while (P->getAs<PointerType>())
2987 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002988 if (P.isConstQualified()) {
2989 isReadOnly = true;
2990 S += 'r';
2991 }
2992 }
2993 if (isReadOnly) {
2994 // Another legacy compatibility encoding. Some ObjC qualifier and type
2995 // combinations need to be rearranged.
2996 // Rewrite "in const" from "nr" to "rn"
2997 const char * s = S.c_str();
2998 int len = S.length();
2999 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3000 std::string replace = "rn";
3001 S.replace(S.end()-2, S.end(), replace);
3002 }
3003 }
Steve Naroff14108da2009-07-10 23:34:53 +00003004 if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00003005 S += ':';
3006 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00003007 }
Mike Stump1eb44332009-09-09 15:08:12 +00003008
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003009 if (PointeeTy->isCharType()) {
3010 // char pointer types should be encoded as '*' unless it is a
3011 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003012 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003013 S += '*';
3014 return;
3015 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003016 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003017 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3018 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3019 S += '#';
3020 return;
3021 }
3022 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3023 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3024 S += '@';
3025 return;
3026 }
3027 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003028 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003029 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003030 getLegacyIntegralTypeEncoding(PointeeTy);
3031
Mike Stump1eb44332009-09-09 15:08:12 +00003032 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003033 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003034 return;
3035 }
Mike Stump1eb44332009-09-09 15:08:12 +00003036
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003037 if (const ArrayType *AT =
3038 // Ignore type qualifiers etc.
3039 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003040 if (isa<IncompleteArrayType>(AT)) {
3041 // Incomplete arrays are encoded as a pointer to the array element.
3042 S += '^';
3043
Mike Stump1eb44332009-09-09 15:08:12 +00003044 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003045 false, ExpandStructures, FD);
3046 } else {
3047 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003048
Anders Carlsson559a8332009-02-22 01:38:57 +00003049 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3050 S += llvm::utostr(CAT->getSize().getZExtValue());
3051 else {
3052 //Variable length arrays are encoded as a regular array with 0 elements.
3053 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3054 S += '0';
3055 }
Mike Stump1eb44332009-09-09 15:08:12 +00003056
3057 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003058 false, ExpandStructures, FD);
3059 S += ']';
3060 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003061 return;
3062 }
Mike Stump1eb44332009-09-09 15:08:12 +00003063
John McCall183700f2009-09-21 23:43:11 +00003064 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003065 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003066 return;
3067 }
Mike Stump1eb44332009-09-09 15:08:12 +00003068
Ted Kremenek6217b802009-07-29 21:53:49 +00003069 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003070 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003071 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003072 // Anonymous structures print as '?'
3073 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3074 S += II->getName();
3075 } else {
3076 S += '?';
3077 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003078 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003079 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003080 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3081 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003082 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003083 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003084 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003085 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003086 S += '"';
3087 }
Mike Stump1eb44332009-09-09 15:08:12 +00003088
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003089 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003090 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003091 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003092 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003093 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003094 QualType qt = Field->getType();
3095 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003096 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003097 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003098 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003099 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003100 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003101 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003102 return;
3103 }
Mike Stump1eb44332009-09-09 15:08:12 +00003104
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003105 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003106 if (FD && FD->isBitField())
3107 EncodeBitField(this, S, FD);
3108 else
3109 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003110 return;
3111 }
Mike Stump1eb44332009-09-09 15:08:12 +00003112
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003113 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003114 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003115 return;
3116 }
Mike Stump1eb44332009-09-09 15:08:12 +00003117
John McCall0953e762009-09-24 19:53:00 +00003118 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003119 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003120 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003121 S += '{';
3122 const IdentifierInfo *II = OI->getIdentifier();
3123 S += II->getName();
3124 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003125 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003126 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003127 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003128 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003129 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003130 RecFields[i]);
3131 else
Mike Stump1eb44332009-09-09 15:08:12 +00003132 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003133 FD);
3134 }
3135 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003136 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003137 }
Mike Stump1eb44332009-09-09 15:08:12 +00003138
John McCall183700f2009-09-21 23:43:11 +00003139 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003140 if (OPT->isObjCIdType()) {
3141 S += '@';
3142 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003143 }
Mike Stump1eb44332009-09-09 15:08:12 +00003144
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003145 if (OPT->isObjCClassType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003146 S += '#';
3147 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003148 }
Mike Stump1eb44332009-09-09 15:08:12 +00003149
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003150 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003151 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003152 ExpandPointedToStructures,
3153 ExpandStructures, FD);
3154 if (FD || EncodingProperty) {
3155 // Note that we do extended encoding of protocol qualifer list
3156 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003157 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003158 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3159 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003160 S += '<';
3161 S += (*I)->getNameAsString();
3162 S += '>';
3163 }
3164 S += '"';
3165 }
3166 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003167 }
Mike Stump1eb44332009-09-09 15:08:12 +00003168
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003169 QualType PointeeTy = OPT->getPointeeType();
3170 if (!EncodingProperty &&
3171 isa<TypedefType>(PointeeTy.getTypePtr())) {
3172 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003173 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003174 // {...};
3175 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003176 getObjCEncodingForTypeImpl(PointeeTy, S,
3177 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003178 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003179 return;
3180 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003181
3182 S += '@';
3183 if (FD || EncodingProperty) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003184 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003185 S += OPT->getInterfaceDecl()->getNameAsCString();
3186 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3187 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003188 S += '<';
3189 S += (*I)->getNameAsString();
3190 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003191 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003192 S += '"';
3193 }
3194 return;
3195 }
Mike Stump1eb44332009-09-09 15:08:12 +00003196
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003197 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003198}
3199
Mike Stump1eb44332009-09-09 15:08:12 +00003200void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003201 std::string& S) const {
3202 if (QT & Decl::OBJC_TQ_In)
3203 S += 'n';
3204 if (QT & Decl::OBJC_TQ_Inout)
3205 S += 'N';
3206 if (QT & Decl::OBJC_TQ_Out)
3207 S += 'o';
3208 if (QT & Decl::OBJC_TQ_Bycopy)
3209 S += 'O';
3210 if (QT & Decl::OBJC_TQ_Byref)
3211 S += 'R';
3212 if (QT & Decl::OBJC_TQ_Oneway)
3213 S += 'V';
3214}
3215
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003216void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003217 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003218
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003219 BuiltinVaListType = T;
3220}
3221
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003222void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003223 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003224}
3225
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003226void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor319ac892009-04-23 22:29:11 +00003227 ObjCSelType = T;
3228
John McCall183700f2009-09-21 23:43:11 +00003229 const TypedefType *TT = T->getAs<TypedefType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003230 if (!TT)
3231 return;
3232 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003233
3234 // typedef struct objc_selector *SEL;
Ted Kremenek6217b802009-07-29 21:53:49 +00003235 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003236 if (!ptr)
3237 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003238 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003239 if (!rec)
3240 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003241 SelStructType = rec;
3242}
3243
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003244void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003245 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003246}
3247
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003248void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003249 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003250}
3251
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003252void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003253 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003254 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003255
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003256 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003257}
3258
Douglas Gregor7532dc62009-03-30 22:58:21 +00003259/// \brief Retrieve the template name that represents a qualified
3260/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003261TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003262 bool TemplateKeyword,
3263 TemplateDecl *Template) {
3264 llvm::FoldingSetNodeID ID;
3265 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3266
3267 void *InsertPos = 0;
3268 QualifiedTemplateName *QTN =
3269 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3270 if (!QTN) {
3271 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3272 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3273 }
3274
3275 return TemplateName(QTN);
3276}
3277
Douglas Gregord99cbe62009-07-29 18:26:50 +00003278/// \brief Retrieve the template name that represents a qualified
3279/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003280TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregord99cbe62009-07-29 18:26:50 +00003281 bool TemplateKeyword,
3282 OverloadedFunctionDecl *Template) {
3283 llvm::FoldingSetNodeID ID;
3284 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump1eb44332009-09-09 15:08:12 +00003285
Douglas Gregord99cbe62009-07-29 18:26:50 +00003286 void *InsertPos = 0;
3287 QualifiedTemplateName *QTN =
3288 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3289 if (!QTN) {
3290 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3291 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3292 }
Mike Stump1eb44332009-09-09 15:08:12 +00003293
Douglas Gregord99cbe62009-07-29 18:26:50 +00003294 return TemplateName(QTN);
3295}
3296
Douglas Gregor7532dc62009-03-30 22:58:21 +00003297/// \brief Retrieve the template name that represents a dependent
3298/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003299TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003300 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003301 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003302 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003303
3304 llvm::FoldingSetNodeID ID;
3305 DependentTemplateName::Profile(ID, NNS, Name);
3306
3307 void *InsertPos = 0;
3308 DependentTemplateName *QTN =
3309 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3310
3311 if (QTN)
3312 return TemplateName(QTN);
3313
3314 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3315 if (CanonNNS == NNS) {
3316 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3317 } else {
3318 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3319 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3320 }
3321
3322 DependentTemplateNames.InsertNode(QTN, InsertPos);
3323 return TemplateName(QTN);
3324}
3325
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003326/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003327/// TargetInfo, produce the corresponding type. The unsigned @p Type
3328/// is actually a value of type @c TargetInfo::IntType.
3329QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003330 switch (Type) {
Mike Stump1eb44332009-09-09 15:08:12 +00003331 case TargetInfo::NoInt: return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003332 case TargetInfo::SignedShort: return ShortTy;
3333 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3334 case TargetInfo::SignedInt: return IntTy;
3335 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3336 case TargetInfo::SignedLong: return LongTy;
3337 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3338 case TargetInfo::SignedLongLong: return LongLongTy;
3339 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3340 }
3341
3342 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00003343 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003344}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003345
3346//===----------------------------------------------------------------------===//
3347// Type Predicates.
3348//===----------------------------------------------------------------------===//
3349
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003350/// isObjCNSObjectType - Return true if this is an NSObject object using
3351/// NSObject attribute on a c-style pointer type.
3352/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003353/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003354///
3355bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3356 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3357 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003358 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003359 return true;
3360 }
Mike Stump1eb44332009-09-09 15:08:12 +00003361 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003362}
3363
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003364/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3365/// garbage collection attribute.
3366///
John McCall0953e762009-09-24 19:53:00 +00003367Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3368 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003369 if (getLangOptions().ObjC1 &&
3370 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003371 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003372 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003373 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003374 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003375 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003376 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003377 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003378 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003379 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003380 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003381 // Non-pointers have none gc'able attribute regardless of the attribute
3382 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003383 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003384 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003385 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003386 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003387}
3388
Chris Lattner6ac46a42008-04-07 06:51:04 +00003389//===----------------------------------------------------------------------===//
3390// Type Compatibility Testing
3391//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003392
Mike Stump1eb44332009-09-09 15:08:12 +00003393/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003394/// compatible.
3395static bool areCompatVectorTypes(const VectorType *LHS,
3396 const VectorType *RHS) {
3397 assert(LHS->isCanonical() && RHS->isCanonical());
3398 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003399 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003400}
3401
Steve Naroff4084c302009-07-23 01:01:38 +00003402//===----------------------------------------------------------------------===//
3403// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3404//===----------------------------------------------------------------------===//
3405
3406/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3407/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003408bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3409 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003410 if (lProto == rProto)
3411 return true;
3412 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3413 E = rProto->protocol_end(); PI != E; ++PI)
3414 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3415 return true;
3416 return false;
3417}
3418
Steve Naroff4084c302009-07-23 01:01:38 +00003419/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3420/// return true if lhs's protocols conform to rhs's protocol; false
3421/// otherwise.
3422bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3423 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3424 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3425 return false;
3426}
3427
3428/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3429/// ObjCQualifiedIDType.
3430bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3431 bool compare) {
3432 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003433 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003434 lhs->isObjCIdType() || lhs->isObjCClassType())
3435 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003436 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003437 rhs->isObjCIdType() || rhs->isObjCClassType())
3438 return true;
3439
3440 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003441 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003442
Steve Naroff4084c302009-07-23 01:01:38 +00003443 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003444
Steve Naroff4084c302009-07-23 01:01:38 +00003445 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003446 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003447 // make sure we check the class hierarchy.
3448 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3449 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3450 E = lhsQID->qual_end(); I != E; ++I) {
3451 // when comparing an id<P> on lhs with a static type on rhs,
3452 // see if static class implements all of id's protocols, directly or
3453 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003454 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003455 return false;
3456 }
3457 }
3458 // If there are no qualifiers and no interface, we have an 'id'.
3459 return true;
3460 }
Mike Stump1eb44332009-09-09 15:08:12 +00003461 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003462 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3463 E = lhsQID->qual_end(); I != E; ++I) {
3464 ObjCProtocolDecl *lhsProto = *I;
3465 bool match = false;
3466
3467 // when comparing an id<P> on lhs with a static type on rhs,
3468 // see if static class implements all of id's protocols, directly or
3469 // through its super class and categories.
3470 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3471 E = rhsOPT->qual_end(); J != E; ++J) {
3472 ObjCProtocolDecl *rhsProto = *J;
3473 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3474 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3475 match = true;
3476 break;
3477 }
3478 }
Mike Stump1eb44332009-09-09 15:08:12 +00003479 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00003480 // make sure we check the class hierarchy.
3481 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3482 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3483 E = lhsQID->qual_end(); I != E; ++I) {
3484 // when comparing an id<P> on lhs with a static type on rhs,
3485 // see if static class implements all of id's protocols, directly or
3486 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003487 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003488 match = true;
3489 break;
3490 }
3491 }
3492 }
3493 if (!match)
3494 return false;
3495 }
Mike Stump1eb44332009-09-09 15:08:12 +00003496
Steve Naroff4084c302009-07-23 01:01:38 +00003497 return true;
3498 }
Mike Stump1eb44332009-09-09 15:08:12 +00003499
Steve Naroff4084c302009-07-23 01:01:38 +00003500 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3501 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3502
Mike Stump1eb44332009-09-09 15:08:12 +00003503 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00003504 lhs->getAsObjCInterfacePointerType()) {
3505 if (lhsOPT->qual_empty()) {
3506 bool match = false;
3507 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3508 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3509 E = rhsQID->qual_end(); I != E; ++I) {
3510 // when comparing an id<P> on lhs with a static type on rhs,
3511 // see if static class implements all of id's protocols, directly or
3512 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003513 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003514 match = true;
3515 break;
3516 }
3517 }
3518 if (!match)
3519 return false;
3520 }
3521 return true;
3522 }
Mike Stump1eb44332009-09-09 15:08:12 +00003523 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003524 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3525 E = lhsOPT->qual_end(); I != E; ++I) {
3526 ObjCProtocolDecl *lhsProto = *I;
3527 bool match = false;
3528
3529 // when comparing an id<P> on lhs with a static type on rhs,
3530 // see if static class implements all of id's protocols, directly or
3531 // through its super class and categories.
3532 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3533 E = rhsQID->qual_end(); J != E; ++J) {
3534 ObjCProtocolDecl *rhsProto = *J;
3535 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3536 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3537 match = true;
3538 break;
3539 }
3540 }
3541 if (!match)
3542 return false;
3543 }
3544 return true;
3545 }
3546 return false;
3547}
3548
Eli Friedman3d815e72008-08-22 00:56:42 +00003549/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003550/// compatible for assignment from RHS to LHS. This handles validation of any
3551/// protocol qualifiers on the LHS or RHS.
3552///
Steve Naroff14108da2009-07-10 23:34:53 +00003553bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3554 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003555 // If either type represents the built-in 'id' or 'Class' types, return true.
3556 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00003557 return true;
3558
Steve Naroff4084c302009-07-23 01:01:38 +00003559 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00003560 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3561 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00003562 false);
3563
Steve Naroff14108da2009-07-10 23:34:53 +00003564 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3565 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00003566 if (LHS && RHS) // We have 2 user-defined types.
3567 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00003568
Steve Naroff4084c302009-07-23 01:01:38 +00003569 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003570}
3571
Eli Friedman3d815e72008-08-22 00:56:42 +00003572bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3573 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00003574 // Verify that the base decls are compatible: the RHS must be a subclass of
3575 // the LHS.
3576 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3577 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003578
Chris Lattner6ac46a42008-04-07 06:51:04 +00003579 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3580 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003581 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003582 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003583
Chris Lattner6ac46a42008-04-07 06:51:04 +00003584 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3585 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003586 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003587 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00003588
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003589 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3590 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003591 LHSPI != LHSPE; LHSPI++) {
3592 bool RHSImplementsProtocol = false;
3593
3594 // If the RHS doesn't implement the protocol on the left, the types
3595 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003596 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00003597 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00003598 RHSPI != RHSPE; RHSPI++) {
3599 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003600 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003601 break;
3602 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003603 }
3604 // FIXME: For better diagnostics, consider passing back the protocol name.
3605 if (!RHSImplementsProtocol)
3606 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003607 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003608 // The RHS implements all protocols listed on the LHS.
3609 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003610}
3611
Steve Naroff389bf462009-02-12 17:52:19 +00003612bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3613 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00003614 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3615 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003616
Steve Naroff14108da2009-07-10 23:34:53 +00003617 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00003618 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003619
3620 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3621 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00003622}
3623
Mike Stump1eb44332009-09-09 15:08:12 +00003624/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00003625/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00003626/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00003627/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00003628bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3629 return !mergeTypes(LHS, RHS).isNull();
3630}
3631
3632QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall183700f2009-09-21 23:43:11 +00003633 const FunctionType *lbase = lhs->getAs<FunctionType>();
3634 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00003635 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3636 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00003637 bool allLTypes = true;
3638 bool allRTypes = true;
3639
3640 // Check return type
3641 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3642 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003643 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3644 allLTypes = false;
3645 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3646 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00003647 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00003648 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3649 if (NoReturn != lbase->getNoReturnAttr())
3650 allLTypes = false;
3651 if (NoReturn != rbase->getNoReturnAttr())
3652 allRTypes = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003653
Eli Friedman3d815e72008-08-22 00:56:42 +00003654 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00003655 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3656 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003657 unsigned lproto_nargs = lproto->getNumArgs();
3658 unsigned rproto_nargs = rproto->getNumArgs();
3659
3660 // Compatible functions must have the same number of arguments
3661 if (lproto_nargs != rproto_nargs)
3662 return QualType();
3663
3664 // Variadic and non-variadic functions aren't compatible
3665 if (lproto->isVariadic() != rproto->isVariadic())
3666 return QualType();
3667
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003668 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3669 return QualType();
3670
Eli Friedman3d815e72008-08-22 00:56:42 +00003671 // Check argument compatibility
3672 llvm::SmallVector<QualType, 10> types;
3673 for (unsigned i = 0; i < lproto_nargs; i++) {
3674 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3675 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3676 QualType argtype = mergeTypes(largtype, rargtype);
3677 if (argtype.isNull()) return QualType();
3678 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003679 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3680 allLTypes = false;
3681 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3682 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003683 }
3684 if (allLTypes) return lhs;
3685 if (allRTypes) return rhs;
3686 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00003687 lproto->isVariadic(), lproto->getTypeQuals(),
3688 NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003689 }
3690
3691 if (lproto) allRTypes = false;
3692 if (rproto) allLTypes = false;
3693
Douglas Gregor72564e72009-02-26 23:50:07 +00003694 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003695 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003696 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003697 if (proto->isVariadic()) return QualType();
3698 // Check that the types are compatible with the types that
3699 // would result from default argument promotions (C99 6.7.5.3p15).
3700 // The only types actually affected are promotable integer
3701 // types and floats, which would be passed as a different
3702 // type depending on whether the prototype is visible.
3703 unsigned proto_nargs = proto->getNumArgs();
3704 for (unsigned i = 0; i < proto_nargs; ++i) {
3705 QualType argTy = proto->getArgType(i);
3706 if (argTy->isPromotableIntegerType() ||
3707 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3708 return QualType();
3709 }
3710
3711 if (allLTypes) return lhs;
3712 if (allRTypes) return rhs;
3713 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00003714 proto->getNumArgs(), proto->isVariadic(),
3715 proto->getTypeQuals(), NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003716 }
3717
3718 if (allLTypes) return lhs;
3719 if (allRTypes) return rhs;
Mike Stump24556362009-07-25 21:26:53 +00003720 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003721}
3722
3723QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003724 // C++ [expr]: If an expression initially has the type "reference to T", the
3725 // type is adjusted to "T" prior to any further analysis, the expression
3726 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003727 // expression is an lvalue unless the reference is an rvalue reference and
3728 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003729 // FIXME: C++ shouldn't be going through here! The rules are different
3730 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003731 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3732 // shouldn't be going through here!
Ted Kremenek6217b802009-07-29 21:53:49 +00003733 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003734 LHS = RT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00003735 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003736 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003737
Eli Friedman3d815e72008-08-22 00:56:42 +00003738 QualType LHSCan = getCanonicalType(LHS),
3739 RHSCan = getCanonicalType(RHS);
3740
3741 // If two types are identical, they are compatible.
3742 if (LHSCan == RHSCan)
3743 return LHS;
3744
John McCall0953e762009-09-24 19:53:00 +00003745 // If the qualifiers are different, the types aren't compatible... mostly.
3746 Qualifiers LQuals = LHSCan.getQualifiers();
3747 Qualifiers RQuals = RHSCan.getQualifiers();
3748 if (LQuals != RQuals) {
3749 // If any of these qualifiers are different, we have a type
3750 // mismatch.
3751 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
3752 LQuals.getAddressSpace() != RQuals.getAddressSpace())
3753 return QualType();
3754
3755 // Exactly one GC qualifier difference is allowed: __strong is
3756 // okay if the other type has no GC qualifier but is an Objective
3757 // C object pointer (i.e. implicitly strong by default). We fix
3758 // this by pretending that the unqualified type was actually
3759 // qualified __strong.
3760 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
3761 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
3762 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
3763
3764 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
3765 return QualType();
3766
3767 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
3768 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
3769 }
3770 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
3771 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
3772 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003773 return QualType();
John McCall0953e762009-09-24 19:53:00 +00003774 }
3775
3776 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00003777
Eli Friedman852d63b2009-06-01 01:22:52 +00003778 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3779 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003780
Chris Lattner1adb8832008-01-14 05:45:46 +00003781 // We want to consider the two function types to be the same for these
3782 // comparisons, just force one to the other.
3783 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3784 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003785
3786 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003787 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3788 LHSClass = Type::ConstantArray;
3789 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3790 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00003791
Nate Begeman213541a2008-04-18 23:10:10 +00003792 // Canonicalize ExtVector -> Vector.
3793 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3794 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00003795
Chris Lattnera36a61f2008-04-07 05:43:21 +00003796 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003797 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00003798 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00003799 // a signed integer type, or an unsigned integer type.
John McCall183700f2009-09-21 23:43:11 +00003800 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003801 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3802 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003803 }
John McCall183700f2009-09-21 23:43:11 +00003804 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003805 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3806 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003807 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003808
Eli Friedman3d815e72008-08-22 00:56:42 +00003809 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003810 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003811
Steve Naroff4a746782008-01-09 22:43:08 +00003812 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003813 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003814#define TYPE(Class, Base)
3815#define ABSTRACT_TYPE(Class, Base)
3816#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3817#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3818#include "clang/AST/TypeNodes.def"
3819 assert(false && "Non-canonical and dependent types shouldn't get here");
3820 return QualType();
3821
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003822 case Type::LValueReference:
3823 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003824 case Type::MemberPointer:
3825 assert(false && "C++ should never be in mergeTypes");
3826 return QualType();
3827
3828 case Type::IncompleteArray:
3829 case Type::VariableArray:
3830 case Type::FunctionProto:
3831 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00003832 assert(false && "Types are eliminated above");
3833 return QualType();
3834
Chris Lattner1adb8832008-01-14 05:45:46 +00003835 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003836 {
3837 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003838 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
3839 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003840 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3841 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00003842 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003843 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00003844 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003845 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003846 return getPointerType(ResultType);
3847 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003848 case Type::BlockPointer:
3849 {
3850 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003851 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
3852 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00003853 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3854 if (ResultType.isNull()) return QualType();
3855 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3856 return LHS;
3857 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3858 return RHS;
3859 return getBlockPointerType(ResultType);
3860 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003861 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003862 {
3863 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3864 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3865 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3866 return QualType();
3867
3868 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3869 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3870 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3871 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003872 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3873 return LHS;
3874 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3875 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003876 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3877 ArrayType::ArraySizeModifier(), 0);
3878 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3879 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003880 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3881 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003882 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3883 return LHS;
3884 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3885 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003886 if (LVAT) {
3887 // FIXME: This isn't correct! But tricky to implement because
3888 // the array's size has to be the size of LHS, but the type
3889 // has to be different.
3890 return LHS;
3891 }
3892 if (RVAT) {
3893 // FIXME: This isn't correct! But tricky to implement because
3894 // the array's size has to be the size of RHS, but the type
3895 // has to be different.
3896 return RHS;
3897 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003898 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3899 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003900 return getIncompleteArrayType(ResultType,
3901 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003902 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003903 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003904 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003905 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003906 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003907 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003908 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003909 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003910 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003911 case Type::Complex:
3912 // Distinct complex types are incompatible.
3913 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003914 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003915 // FIXME: The merged type should be an ExtVector!
John McCall183700f2009-09-21 23:43:11 +00003916 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00003917 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003918 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003919 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003920 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003921 // FIXME: This should be type compatibility, e.g. whether
3922 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00003923 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
3924 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00003925 if (LHSIface && RHSIface &&
3926 canAssignObjCInterfaces(LHSIface, RHSIface))
3927 return LHS;
3928
Eli Friedman3d815e72008-08-22 00:56:42 +00003929 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003930 }
Steve Naroff14108da2009-07-10 23:34:53 +00003931 case Type::ObjCObjectPointer: {
John McCall183700f2009-09-21 23:43:11 +00003932 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
3933 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00003934 return LHS;
3935
Steve Naroffbc76dd02008-12-10 22:14:21 +00003936 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00003937 }
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003938 case Type::FixedWidthInt:
3939 // Distinct fixed-width integers are not compatible.
3940 return QualType();
Douglas Gregor7532dc62009-03-30 22:58:21 +00003941 case Type::TemplateSpecialization:
3942 assert(false && "Dependent types have no size");
3943 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003944 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003945
3946 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003947}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003948
Chris Lattner5426bf62008-04-07 07:01:58 +00003949//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003950// Integer Predicates
3951//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003952
Eli Friedmanad74a752008-06-28 06:23:08 +00003953unsigned ASTContext::getIntWidth(QualType T) {
3954 if (T == BoolTy)
3955 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003956 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3957 return FWIT->getWidth();
3958 }
3959 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003960 return (unsigned)getTypeSize(T);
3961}
3962
3963QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3964 assert(T->isSignedIntegerType() && "Unexpected type");
John McCall183700f2009-09-21 23:43:11 +00003965 if (const EnumType* ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00003966 T = ETy->getDecl()->getIntegerType();
John McCall183700f2009-09-21 23:43:11 +00003967 const BuiltinType* BTy = T->getAs<BuiltinType>();
Eli Friedmanad74a752008-06-28 06:23:08 +00003968 assert (BTy && "Unexpected signed integer type");
3969 switch (BTy->getKind()) {
3970 case BuiltinType::Char_S:
3971 case BuiltinType::SChar:
3972 return UnsignedCharTy;
3973 case BuiltinType::Short:
3974 return UnsignedShortTy;
3975 case BuiltinType::Int:
3976 return UnsignedIntTy;
3977 case BuiltinType::Long:
3978 return UnsignedLongTy;
3979 case BuiltinType::LongLong:
3980 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003981 case BuiltinType::Int128:
3982 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003983 default:
3984 assert(0 && "Unexpected signed integer type");
3985 return QualType();
3986 }
3987}
3988
Douglas Gregor2cf26342009-04-09 22:27:44 +00003989ExternalASTSource::~ExternalASTSource() { }
3990
3991void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00003992
3993
3994//===----------------------------------------------------------------------===//
3995// Builtin Type Computation
3996//===----------------------------------------------------------------------===//
3997
3998/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
3999/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004000static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004001 ASTContext::GetBuiltinTypeError &Error,
4002 bool AllowTypeModifiers = true) {
4003 // Modifiers.
4004 int HowLong = 0;
4005 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004006
Chris Lattner86df27b2009-06-14 00:45:47 +00004007 // Read the modifiers first.
4008 bool Done = false;
4009 while (!Done) {
4010 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004011 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004012 case 'S':
4013 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4014 assert(!Signed && "Can't use 'S' modifier multiple times!");
4015 Signed = true;
4016 break;
4017 case 'U':
4018 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4019 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4020 Unsigned = true;
4021 break;
4022 case 'L':
4023 assert(HowLong <= 2 && "Can't have LLLL modifier");
4024 ++HowLong;
4025 break;
4026 }
4027 }
4028
4029 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004030
Chris Lattner86df27b2009-06-14 00:45:47 +00004031 // Read the base type.
4032 switch (*Str++) {
4033 default: assert(0 && "Unknown builtin type letter!");
4034 case 'v':
4035 assert(HowLong == 0 && !Signed && !Unsigned &&
4036 "Bad modifiers used with 'v'!");
4037 Type = Context.VoidTy;
4038 break;
4039 case 'f':
4040 assert(HowLong == 0 && !Signed && !Unsigned &&
4041 "Bad modifiers used with 'f'!");
4042 Type = Context.FloatTy;
4043 break;
4044 case 'd':
4045 assert(HowLong < 2 && !Signed && !Unsigned &&
4046 "Bad modifiers used with 'd'!");
4047 if (HowLong)
4048 Type = Context.LongDoubleTy;
4049 else
4050 Type = Context.DoubleTy;
4051 break;
4052 case 's':
4053 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4054 if (Unsigned)
4055 Type = Context.UnsignedShortTy;
4056 else
4057 Type = Context.ShortTy;
4058 break;
4059 case 'i':
4060 if (HowLong == 3)
4061 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4062 else if (HowLong == 2)
4063 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4064 else if (HowLong == 1)
4065 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4066 else
4067 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4068 break;
4069 case 'c':
4070 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4071 if (Signed)
4072 Type = Context.SignedCharTy;
4073 else if (Unsigned)
4074 Type = Context.UnsignedCharTy;
4075 else
4076 Type = Context.CharTy;
4077 break;
4078 case 'b': // boolean
4079 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4080 Type = Context.BoolTy;
4081 break;
4082 case 'z': // size_t.
4083 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4084 Type = Context.getSizeType();
4085 break;
4086 case 'F':
4087 Type = Context.getCFConstantStringType();
4088 break;
4089 case 'a':
4090 Type = Context.getBuiltinVaListType();
4091 assert(!Type.isNull() && "builtin va list type not initialized!");
4092 break;
4093 case 'A':
4094 // This is a "reference" to a va_list; however, what exactly
4095 // this means depends on how va_list is defined. There are two
4096 // different kinds of va_list: ones passed by value, and ones
4097 // passed by reference. An example of a by-value va_list is
4098 // x86, where va_list is a char*. An example of by-ref va_list
4099 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4100 // we want this argument to be a char*&; for x86-64, we want
4101 // it to be a __va_list_tag*.
4102 Type = Context.getBuiltinVaListType();
4103 assert(!Type.isNull() && "builtin va list type not initialized!");
4104 if (Type->isArrayType()) {
4105 Type = Context.getArrayDecayedType(Type);
4106 } else {
4107 Type = Context.getLValueReferenceType(Type);
4108 }
4109 break;
4110 case 'V': {
4111 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004112 unsigned NumElements = strtoul(Str, &End, 10);
4113 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004114
Chris Lattner86df27b2009-06-14 00:45:47 +00004115 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004116
Chris Lattner86df27b2009-06-14 00:45:47 +00004117 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4118 Type = Context.getVectorType(ElementType, NumElements);
4119 break;
4120 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00004121 case 'X': {
4122 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4123 Type = Context.getComplexType(ElementType);
4124 break;
4125 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004126 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004127 Type = Context.getFILEType();
4128 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004129 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004130 return QualType();
4131 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004132 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004133 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004134 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004135 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004136 else
4137 Type = Context.getjmp_bufType();
4138
Mike Stumpfd612db2009-07-28 23:47:15 +00004139 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004140 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004141 return QualType();
4142 }
4143 break;
Mike Stump782fa302009-07-28 02:25:19 +00004144 }
Mike Stump1eb44332009-09-09 15:08:12 +00004145
Chris Lattner86df27b2009-06-14 00:45:47 +00004146 if (!AllowTypeModifiers)
4147 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004148
Chris Lattner86df27b2009-06-14 00:45:47 +00004149 Done = false;
4150 while (!Done) {
4151 switch (*Str++) {
4152 default: Done = true; --Str; break;
4153 case '*':
4154 Type = Context.getPointerType(Type);
4155 break;
4156 case '&':
4157 Type = Context.getLValueReferenceType(Type);
4158 break;
4159 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4160 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004161 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004162 break;
4163 }
4164 }
Mike Stump1eb44332009-09-09 15:08:12 +00004165
Chris Lattner86df27b2009-06-14 00:45:47 +00004166 return Type;
4167}
4168
4169/// GetBuiltinType - Return the type for the specified builtin.
4170QualType ASTContext::GetBuiltinType(unsigned id,
4171 GetBuiltinTypeError &Error) {
4172 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004173
Chris Lattner86df27b2009-06-14 00:45:47 +00004174 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004175
Chris Lattner86df27b2009-06-14 00:45:47 +00004176 Error = GE_None;
4177 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4178 if (Error != GE_None)
4179 return QualType();
4180 while (TypeStr[0] && TypeStr[0] != '.') {
4181 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4182 if (Error != GE_None)
4183 return QualType();
4184
4185 // Do array -> pointer decay. The builtin should use the decayed type.
4186 if (Ty->isArrayType())
4187 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004188
Chris Lattner86df27b2009-06-14 00:45:47 +00004189 ArgTypes.push_back(Ty);
4190 }
4191
4192 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4193 "'.' should only occur at end of builtin type list!");
4194
4195 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4196 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4197 return getFunctionNoProtoType(ResType);
4198 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4199 TypeStr[0] == '.', 0);
4200}
Eli Friedmana95d7572009-08-19 07:44:53 +00004201
4202QualType
4203ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4204 // Perform the usual unary conversions. We do this early so that
4205 // integral promotions to "int" can allow us to exit early, in the
4206 // lhs == rhs check. Also, for conversion purposes, we ignore any
4207 // qualifiers. For example, "const float" and "float" are
4208 // equivalent.
4209 if (lhs->isPromotableIntegerType())
4210 lhs = getPromotedIntegerType(lhs);
4211 else
4212 lhs = lhs.getUnqualifiedType();
4213 if (rhs->isPromotableIntegerType())
4214 rhs = getPromotedIntegerType(rhs);
4215 else
4216 rhs = rhs.getUnqualifiedType();
4217
4218 // If both types are identical, no conversion is needed.
4219 if (lhs == rhs)
4220 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004221
Eli Friedmana95d7572009-08-19 07:44:53 +00004222 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4223 // The caller can deal with this (e.g. pointer + int).
4224 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4225 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004226
4227 // At this point, we have two different arithmetic types.
4228
Eli Friedmana95d7572009-08-19 07:44:53 +00004229 // Handle complex types first (C99 6.3.1.8p1).
4230 if (lhs->isComplexType() || rhs->isComplexType()) {
4231 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004232 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004233 // convert the rhs to the lhs complex type.
4234 return lhs;
4235 }
Mike Stump1eb44332009-09-09 15:08:12 +00004236 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004237 // convert the lhs to the rhs complex type.
4238 return rhs;
4239 }
4240 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004241 // When both operands are complex, the shorter operand is converted to the
4242 // type of the longer, and that is the type of the result. This corresponds
4243 // to what is done when combining two real floating-point operands.
4244 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004245 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004246 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004247 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004248 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004249 // "double _Complex" is promoted to "long double _Complex".
4250 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004251
4252 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004253 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004254 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004255 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004256 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004257 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4258 // domains match. This is a requirement for our implementation, C99
4259 // does not require this promotion.
4260 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4261 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4262 return rhs;
4263 } else { // handle "_Complex double, double".
4264 return lhs;
4265 }
4266 }
4267 return lhs; // The domain/size match exactly.
4268 }
4269 // Now handle "real" floating types (i.e. float, double, long double).
4270 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4271 // if we have an integer operand, the result is the real floating type.
4272 if (rhs->isIntegerType()) {
4273 // convert rhs to the lhs floating point type.
4274 return lhs;
4275 }
4276 if (rhs->isComplexIntegerType()) {
4277 // convert rhs to the complex floating point type.
4278 return getComplexType(lhs);
4279 }
4280 if (lhs->isIntegerType()) {
4281 // convert lhs to the rhs floating point type.
4282 return rhs;
4283 }
Mike Stump1eb44332009-09-09 15:08:12 +00004284 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004285 // convert lhs to the complex floating point type.
4286 return getComplexType(rhs);
4287 }
4288 // We have two real floating types, float/complex combos were handled above.
4289 // Convert the smaller operand to the bigger result.
4290 int result = getFloatingTypeOrder(lhs, rhs);
4291 if (result > 0) // convert the rhs
4292 return lhs;
4293 assert(result < 0 && "illegal float comparison");
4294 return rhs; // convert the lhs
4295 }
4296 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4297 // Handle GCC complex int extension.
4298 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4299 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4300
4301 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004302 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004303 rhsComplexInt->getElementType()) >= 0)
4304 return lhs; // convert the rhs
4305 return rhs;
4306 } else if (lhsComplexInt && rhs->isIntegerType()) {
4307 // convert the rhs to the lhs complex type.
4308 return lhs;
4309 } else if (rhsComplexInt && lhs->isIntegerType()) {
4310 // convert the lhs to the rhs complex type.
4311 return rhs;
4312 }
4313 }
4314 // Finally, we have two differing integer types.
4315 // The rules for this case are in C99 6.3.1.8
4316 int compare = getIntegerTypeOrder(lhs, rhs);
4317 bool lhsSigned = lhs->isSignedIntegerType(),
4318 rhsSigned = rhs->isSignedIntegerType();
4319 QualType destType;
4320 if (lhsSigned == rhsSigned) {
4321 // Same signedness; use the higher-ranked type
4322 destType = compare >= 0 ? lhs : rhs;
4323 } else if (compare != (lhsSigned ? 1 : -1)) {
4324 // The unsigned type has greater than or equal rank to the
4325 // signed type, so use the unsigned type
4326 destType = lhsSigned ? rhs : lhs;
4327 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4328 // The two types are different widths; if we are here, that
4329 // means the signed type is larger than the unsigned type, so
4330 // use the signed type.
4331 destType = lhsSigned ? lhs : rhs;
4332 } else {
4333 // The signed type is higher-ranked than the unsigned type,
4334 // but isn't actually any bigger (like unsigned int and long
4335 // on most 32-bit systems). Use the unsigned type corresponding
4336 // to the signed type.
4337 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4338 }
4339 return destType;
4340}