blob: e028186d46e515675d1c19c7fec7c2be5db7b675 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000018#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000023#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000025#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000027#include "llvm/Support/MemoryBuffer.h"
Anders Carlsson29445a02009-07-18 21:19:52 +000028#include "RecordLayoutBuilder.h"
29
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
32enum FloatingRank {
33 FloatRank, DoubleRank, LongDoubleRank
34};
35
Chris Lattner61710852008-10-05 17:34:18 +000036ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
37 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000038 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +000039 Builtin::Context &builtins,
Mike Stump1eb44332009-09-09 15:08:12 +000040 bool FreeMem, unsigned size_reserve) :
41 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +000042 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump1eb44332009-09-09 15:08:12 +000043 sigjmp_bufDecl(0), SourceMgr(SM), LangOpts(LOpts),
44 LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +000045 Idents(idents), Selectors(sels),
Mike Stump1eb44332009-09-09 15:08:12 +000046 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
David Chisnall0f436562009-08-17 16:35:33 +000047 ObjCIdRedefinitionType = QualType();
48 ObjCClassRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +000049 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +000050 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +000051 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000052}
53
Reid Spencer5f016e22007-07-11 17:01:13 +000054ASTContext::~ASTContext() {
55 // Deallocate all the types.
56 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000057 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 Types.pop_back();
59 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000060
Nuno Lopesb74668e2008-12-17 22:30:25 +000061 {
John McCall0953e762009-09-24 19:53:00 +000062 llvm::FoldingSet<ExtQuals>::iterator
63 I = ExtQualNodes.begin(), E = ExtQualNodes.end();
64 while (I != E)
65 Deallocate(&*I++);
66 }
67
68 {
Nuno Lopesb74668e2008-12-17 22:30:25 +000069 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
70 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
71 while (I != E) {
72 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
73 delete R;
74 }
75 }
76
77 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000078 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
79 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000080 while (I != E) {
81 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
82 delete R;
83 }
84 }
85
Douglas Gregorab452ba2009-03-26 23:50:42 +000086 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000087 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
88 NNS = NestedNameSpecifiers.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000089 NNSEnd = NestedNameSpecifiers.end();
90 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000091 /* Increment in loop */)
92 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000093
94 if (GlobalNestedNameSpecifier)
95 GlobalNestedNameSpecifier->Destroy(*this);
96
Eli Friedmanb26153c2008-05-27 03:08:09 +000097 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000098}
99
Mike Stump1eb44332009-09-09 15:08:12 +0000100void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
102 ExternalSource.reset(Source.take());
103}
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105void ASTContext::PrintStats() const {
106 fprintf(stderr, "*** AST Context Stats:\n");
107 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000108
Douglas Gregordbe833d2009-05-26 14:40:08 +0000109 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000110#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000111#define ABSTRACT_TYPE(Name, Parent)
112#include "clang/AST/TypeNodes.def"
113 0 // Extra
114 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000115
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
117 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000118 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120
Douglas Gregordbe833d2009-05-26 14:40:08 +0000121 unsigned Idx = 0;
122 unsigned TotalBytes = 0;
123#define TYPE(Name, Parent) \
124 if (counts[Idx]) \
125 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
126 TotalBytes += counts[Idx] * sizeof(Name##Type); \
127 ++Idx;
128#define ABSTRACT_TYPE(Name, Parent)
129#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregordbe833d2009-05-26 14:40:08 +0000131 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132
133 if (ExternalSource.get()) {
134 fprintf(stderr, "\n");
135 ExternalSource->PrintStats();
136 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000137}
138
139
140void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000141 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
142 R = QualType(Ty, 0);
143 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
Reid Spencer5f016e22007-07-11 17:01:13 +0000146void ASTContext::InitBuiltinTypes() {
147 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 // C99 6.2.5p19.
150 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 // C99 6.2.5p2.
153 InitBuiltinType(BoolTy, BuiltinType::Bool);
154 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000155 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 InitBuiltinType(CharTy, BuiltinType::Char_S);
157 else
158 InitBuiltinType(CharTy, BuiltinType::Char_U);
159 // C99 6.2.5p4.
160 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
161 InitBuiltinType(ShortTy, BuiltinType::Short);
162 InitBuiltinType(IntTy, BuiltinType::Int);
163 InitBuiltinType(LongTy, BuiltinType::Long);
164 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 // C99 6.2.5p6.
167 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
168 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
169 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
170 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
171 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 // C99 6.2.5p10.
174 InitBuiltinType(FloatTy, BuiltinType::Float);
175 InitBuiltinType(DoubleTy, BuiltinType::Double);
176 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000177
Chris Lattner2df9ced2009-04-30 02:43:43 +0000178 // GNU extension, 128-bit integers.
179 InitBuiltinType(Int128Ty, BuiltinType::Int128);
180 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
181
Chris Lattner3a250322009-02-26 23:43:47 +0000182 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
183 InitBuiltinType(WCharTy, BuiltinType::WChar);
184 else // C99
185 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000186
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000187 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
188 InitBuiltinType(Char16Ty, BuiltinType::Char16);
189 else // C99
190 Char16Ty = getFromTargetType(Target.getChar16Type());
191
192 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
193 InitBuiltinType(Char32Ty, BuiltinType::Char32);
194 else // C99
195 Char32Ty = getFromTargetType(Target.getChar32Type());
196
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000197 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000198 InitBuiltinType(OverloadTy, BuiltinType::Overload);
199
200 // Placeholder type for type-dependent expressions whose type is
201 // completely unknown. No code should ever check a type against
202 // DependentTy and users should never see it; however, it is here to
203 // help diagnose failures to properly check for type-dependent
204 // expressions.
205 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000206
Mike Stump1eb44332009-09-09 15:08:12 +0000207 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000208 // not yet been deduced.
209 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 // C99 6.2.5p11.
212 FloatComplexTy = getComplexType(FloatTy);
213 DoubleComplexTy = getComplexType(DoubleTy);
214 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000215
Steve Naroff7e219e42007-10-15 14:41:52 +0000216 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Steve Naroffde2e22d2009-07-15 18:40:39 +0000218 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
219 ObjCIdTypedefType = QualType();
220 ObjCClassTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Steve Naroffde2e22d2009-07-15 18:40:39 +0000222 // Builtin types for 'id' and 'Class'.
223 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
224 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Steve Naroff14108da2009-07-10 23:34:53 +0000225
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000226 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000228 // void * type
229 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000230
231 // nullptr type (C++0x 2.14.7)
232 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000233}
234
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000235MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000236ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000237 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000238 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000239 = InstantiatedFromStaticDataMember.find(Var);
240 if (Pos == InstantiatedFromStaticDataMember.end())
241 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Douglas Gregor7caa6822009-07-24 20:34:43 +0000243 return Pos->second;
244}
245
Mike Stump1eb44332009-09-09 15:08:12 +0000246void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000247ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
248 TemplateSpecializationKind TSK) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000249 assert(Inst->isStaticDataMember() && "Not a static data member");
250 assert(Tmpl->isStaticDataMember() && "Not a static data member");
251 assert(!InstantiatedFromStaticDataMember[Inst] &&
252 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000253 InstantiatedFromStaticDataMember[Inst]
254 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000255}
256
Anders Carlsson0d8df782009-08-29 19:37:28 +0000257UnresolvedUsingDecl *
258ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000259 llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
Anders Carlsson0d8df782009-08-29 19:37:28 +0000260 = InstantiatedFromUnresolvedUsingDecl.find(UUD);
261 if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
262 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Anders Carlsson0d8df782009-08-29 19:37:28 +0000264 return Pos->second;
265}
266
267void
268ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
269 UnresolvedUsingDecl *UUD) {
270 assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
271 "Already noted what using decl what instantiated from");
272 InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
273}
274
Anders Carlssond8b285f2009-09-01 04:26:58 +0000275FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
276 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
277 = InstantiatedFromUnnamedFieldDecl.find(Field);
278 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
279 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Anders Carlssond8b285f2009-09-01 04:26:58 +0000281 return Pos->second;
282}
283
284void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
285 FieldDecl *Tmpl) {
286 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
287 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
288 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
289 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Anders Carlssond8b285f2009-09-01 04:26:58 +0000291 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
292}
293
Douglas Gregor2e222532009-07-02 17:08:52 +0000294namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000295 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000296 : std::binary_function<SourceRange, SourceRange, bool> {
297 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Douglas Gregor2e222532009-07-02 17:08:52 +0000299 public:
300 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Douglas Gregor2e222532009-07-02 17:08:52 +0000302 bool operator()(SourceRange X, SourceRange Y) {
303 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
304 }
305 };
306}
307
308/// \brief Determine whether the given comment is a Doxygen-style comment.
309///
310/// \param Start the start of the comment text.
311///
312/// \param End the end of the comment text.
313///
314/// \param Member whether we want to check whether this is a member comment
315/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
316/// we only return true when we find a non-member comment.
Mike Stump1eb44332009-09-09 15:08:12 +0000317static bool
318isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
Douglas Gregor2e222532009-07-02 17:08:52 +0000319 bool Member = false) {
Mike Stump1eb44332009-09-09 15:08:12 +0000320 const char *BufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000321 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
322 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
323 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregor2e222532009-07-02 17:08:52 +0000325 if (End - Start < 4)
326 return false;
327
328 assert(Start[0] == '/' && "Not a comment?");
329 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
330 return false;
331 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
332 return false;
333
334 return (Start[3] == '<') == Member;
335}
336
337/// \brief Retrieve the comment associated with the given declaration, if
Mike Stump1eb44332009-09-09 15:08:12 +0000338/// it has one.
Douglas Gregor2e222532009-07-02 17:08:52 +0000339const char *ASTContext::getCommentForDecl(const Decl *D) {
340 if (!D)
341 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Douglas Gregor2e222532009-07-02 17:08:52 +0000343 // Check whether we have cached a comment string for this declaration
344 // already.
Mike Stump1eb44332009-09-09 15:08:12 +0000345 llvm::DenseMap<const Decl *, std::string>::iterator Pos
Douglas Gregor2e222532009-07-02 17:08:52 +0000346 = DeclComments.find(D);
347 if (Pos != DeclComments.end())
348 return Pos->second.c_str();
349
Mike Stump1eb44332009-09-09 15:08:12 +0000350 // If we have an external AST source and have not yet loaded comments from
Douglas Gregor2e222532009-07-02 17:08:52 +0000351 // that source, do so now.
352 if (ExternalSource && !LoadedExternalComments) {
353 std::vector<SourceRange> LoadedComments;
354 ExternalSource->ReadComments(LoadedComments);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Douglas Gregor2e222532009-07-02 17:08:52 +0000356 if (!LoadedComments.empty())
357 Comments.insert(Comments.begin(), LoadedComments.begin(),
358 LoadedComments.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Douglas Gregor2e222532009-07-02 17:08:52 +0000360 LoadedExternalComments = true;
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
363 // If there are no comments anywhere, we won't find anything.
Douglas Gregor2e222532009-07-02 17:08:52 +0000364 if (Comments.empty())
365 return 0;
366
367 // If the declaration doesn't map directly to a location in a file, we
368 // can't find the comment.
369 SourceLocation DeclStartLoc = D->getLocStart();
370 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
371 return 0;
372
373 // Find the comment that occurs just before this declaration.
374 std::vector<SourceRange>::iterator LastComment
Mike Stump1eb44332009-09-09 15:08:12 +0000375 = std::lower_bound(Comments.begin(), Comments.end(),
Douglas Gregor2e222532009-07-02 17:08:52 +0000376 SourceRange(DeclStartLoc),
377 BeforeInTranslationUnit(&SourceMgr));
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Douglas Gregor2e222532009-07-02 17:08:52 +0000379 // Decompose the location for the start of the declaration and find the
380 // beginning of the file buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000381 std::pair<FileID, unsigned> DeclStartDecomp
Douglas Gregor2e222532009-07-02 17:08:52 +0000382 = SourceMgr.getDecomposedLoc(DeclStartLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000383 const char *FileBufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000384 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Douglas Gregor2e222532009-07-02 17:08:52 +0000386 // First check whether we have a comment for a member.
387 if (LastComment != Comments.end() &&
388 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
389 isDoxygenComment(SourceMgr, *LastComment, true)) {
390 std::pair<FileID, unsigned> LastCommentEndDecomp
391 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
392 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
393 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
Mike Stump1eb44332009-09-09 15:08:12 +0000394 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000395 LastCommentEndDecomp.second)) {
396 // The Doxygen member comment comes after the declaration starts and
397 // is on the same line and in the same file as the declaration. This
398 // is the comment we want.
399 std::string &Result = DeclComments[D];
Mike Stump1eb44332009-09-09 15:08:12 +0000400 Result.append(FileBufferStart +
401 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000402 FileBufferStart + LastCommentEndDecomp.second + 1);
403 return Result.c_str();
404 }
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregor2e222532009-07-02 17:08:52 +0000407 if (LastComment == Comments.begin())
408 return 0;
409 --LastComment;
410
411 // Decompose the end of the comment.
412 std::pair<FileID, unsigned> LastCommentEndDecomp
413 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Douglas Gregor2e222532009-07-02 17:08:52 +0000415 // If the comment and the declaration aren't in the same file, then they
416 // aren't related.
417 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
418 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Douglas Gregor2e222532009-07-02 17:08:52 +0000420 // Check that we actually have a Doxygen comment.
421 if (!isDoxygenComment(SourceMgr, *LastComment))
422 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Douglas Gregor2e222532009-07-02 17:08:52 +0000424 // Compute the starting line for the declaration and for the end of the
425 // comment (this is expensive).
Mike Stump1eb44332009-09-09 15:08:12 +0000426 unsigned DeclStartLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000427 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
428 unsigned CommentEndLine
Mike Stump1eb44332009-09-09 15:08:12 +0000429 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000430 LastCommentEndDecomp.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Douglas Gregor2e222532009-07-02 17:08:52 +0000432 // If the comment does not end on the line prior to the declaration, then
433 // the comment is not associated with the declaration at all.
434 if (CommentEndLine + 1 != DeclStartLine)
435 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregor2e222532009-07-02 17:08:52 +0000437 // We have a comment, but there may be more comments on the previous lines.
438 // Keep looking so long as the comments are still Doxygen comments and are
439 // still adjacent.
Mike Stump1eb44332009-09-09 15:08:12 +0000440 unsigned ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000441 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
442 std::vector<SourceRange>::iterator FirstComment = LastComment;
443 while (FirstComment != Comments.begin()) {
444 // Look at the previous comment
445 --FirstComment;
446 std::pair<FileID, unsigned> Decomp
447 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Douglas Gregor2e222532009-07-02 17:08:52 +0000449 // If this previous comment is in a different file, we're done.
450 if (Decomp.first != DeclStartDecomp.first) {
451 ++FirstComment;
452 break;
453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Douglas Gregor2e222532009-07-02 17:08:52 +0000455 // If this comment is not a Doxygen comment, we're done.
456 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
457 ++FirstComment;
458 break;
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Douglas Gregor2e222532009-07-02 17:08:52 +0000461 // If the line number is not what we expected, we're done.
462 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
463 if (Line != ExpectedLine) {
464 ++FirstComment;
465 break;
466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Douglas Gregor2e222532009-07-02 17:08:52 +0000468 // Set the next expected line number.
Mike Stump1eb44332009-09-09 15:08:12 +0000469 ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000470 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
471 }
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Douglas Gregor2e222532009-07-02 17:08:52 +0000473 // The iterator range [FirstComment, LastComment] contains all of the
474 // BCPL comments that, together, are associated with this declaration.
475 // Form a single comment block string for this declaration that concatenates
476 // all of these comments.
477 std::string &Result = DeclComments[D];
478 while (FirstComment != LastComment) {
479 std::pair<FileID, unsigned> DecompStart
480 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
481 std::pair<FileID, unsigned> DecompEnd
482 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
483 Result.append(FileBufferStart + DecompStart.second,
484 FileBufferStart + DecompEnd.second + 1);
485 ++FirstComment;
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregor2e222532009-07-02 17:08:52 +0000488 // Append the last comment line.
Mike Stump1eb44332009-09-09 15:08:12 +0000489 Result.append(FileBufferStart +
490 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000491 FileBufferStart + LastCommentEndDecomp.second + 1);
492 return Result.c_str();
493}
494
Chris Lattner464175b2007-07-18 17:52:12 +0000495//===----------------------------------------------------------------------===//
496// Type Sizing and Analysis
497//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000498
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000499/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
500/// scalar floating point type.
501const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000502 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000503 assert(BT && "Not a floating point type!");
504 switch (BT->getKind()) {
505 default: assert(0 && "Not a floating point type!");
506 case BuiltinType::Float: return Target.getFloatFormat();
507 case BuiltinType::Double: return Target.getDoubleFormat();
508 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
509 }
510}
511
Mike Stump196efbf2009-09-22 02:43:44 +0000512/// getDeclAlignInBytes - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000513/// specified decl. Note that bitfields do not have a valid alignment, so
514/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000515unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000516 unsigned Align = Target.getCharWidth();
517
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000518 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedmandcdafb62009-02-22 02:56:25 +0000519 Align = std::max(Align, AA->getAlignment());
520
Chris Lattneraf707ab2009-01-24 21:53:27 +0000521 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
522 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000523 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000524 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000525 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000526 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
527 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000528 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
529 T = cast<ArrayType>(T)->getElementType();
530
531 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
532 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000533 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000534
535 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000536}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000537
Chris Lattnera7674d82007-07-13 22:13:22 +0000538/// getTypeSize - Return the size of the specified type, in bits. This method
539/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000540///
541/// FIXME: Pointers into different addr spaces could have different sizes and
542/// alignment requirements: getPointerInfo should take an AddrSpace, this
543/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000544std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000545ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000546 uint64_t Width=0;
547 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000548 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000549#define TYPE(Class, Base)
550#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000551#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000552#define DEPENDENT_TYPE(Class, Base) case Type::Class:
553#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000554 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000555 break;
556
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +0000557 case Type::ObjCProtocolList:
558 assert(false && "Should not see protocol list types");
559 break;
560
Chris Lattner692233e2007-07-13 22:27:08 +0000561 case Type::FunctionNoProto:
562 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000563 // GCC extension: alignof(function) = 32 bits
564 Width = 0;
565 Align = 32;
566 break;
567
Douglas Gregor72564e72009-02-26 23:50:07 +0000568 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000569 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000570 Width = 0;
571 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
572 break;
573
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000574 case Type::ConstantArrayWithExpr:
575 case Type::ConstantArrayWithoutExpr:
Steve Narofffb22d962007-08-30 01:06:46 +0000576 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000577 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Chris Lattner98be4942008-03-05 18:54:05 +0000579 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000580 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000581 Align = EltInfo.second;
582 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000583 }
Nate Begeman213541a2008-04-18 23:10:10 +0000584 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000585 case Type::Vector: {
Mike Stump1eb44332009-09-09 15:08:12 +0000586 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000587 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000588 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000589 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000590 // If the alignment is not a power of 2, round up to the next power of 2.
591 // This happens for non-power-of-2 length vectors.
592 // FIXME: this should probably be a target property.
593 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000594 break;
595 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000596
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000597 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000598 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000599 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000600 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000601 // GCC extension: alignof(void) = 8 bits.
602 Width = 0;
603 Align = 8;
604 break;
605
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000606 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000607 Width = Target.getBoolWidth();
608 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000609 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000610 case BuiltinType::Char_S:
611 case BuiltinType::Char_U:
612 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000613 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000614 Width = Target.getCharWidth();
615 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000616 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000617 case BuiltinType::WChar:
618 Width = Target.getWCharWidth();
619 Align = Target.getWCharAlign();
620 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000621 case BuiltinType::Char16:
622 Width = Target.getChar16Width();
623 Align = Target.getChar16Align();
624 break;
625 case BuiltinType::Char32:
626 Width = Target.getChar32Width();
627 Align = Target.getChar32Align();
628 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000629 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000630 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000631 Width = Target.getShortWidth();
632 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000633 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000634 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000635 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000636 Width = Target.getIntWidth();
637 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000638 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000639 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000640 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000641 Width = Target.getLongWidth();
642 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000643 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000644 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000645 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000646 Width = Target.getLongLongWidth();
647 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000648 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000649 case BuiltinType::Int128:
650 case BuiltinType::UInt128:
651 Width = 128;
652 Align = 128; // int128_t is 128-bit aligned on all targets.
653 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000654 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000655 Width = Target.getFloatWidth();
656 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000657 break;
658 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000659 Width = Target.getDoubleWidth();
660 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000661 break;
662 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000663 Width = Target.getLongDoubleWidth();
664 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000665 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000666 case BuiltinType::NullPtr:
667 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
668 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000669 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000670 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000671 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000672 case Type::FixedWidthInt:
673 // FIXME: This isn't precisely correct; the width/alignment should depend
674 // on the available types for the target
675 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000676 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000677 Align = Width;
678 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000679 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000680 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000681 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000682 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000683 case Type::BlockPointer: {
684 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
685 Width = Target.getPointerWidth(AS);
686 Align = Target.getPointerAlign(AS);
687 break;
688 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000689 case Type::Pointer: {
690 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000691 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000692 Align = Target.getPointerAlign(AS);
693 break;
694 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000695 case Type::LValueReference:
696 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000697 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000698 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000699 // FIXME: This is wrong for struct layout: a reference in a struct has
700 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000701 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000702 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000703 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
704 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
705 // If we ever want to support other ABIs this needs to be abstracted.
706
Sebastian Redlf30208a2009-01-24 21:16:55 +0000707 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000708 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000709 getTypeInfo(getPointerDiffType());
710 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000711 if (Pointee->isFunctionType())
712 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000713 Align = PtrDiffInfo.second;
714 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000715 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000716 case Type::Complex: {
717 // Complex types have the same alignment as their elements, but twice the
718 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000719 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000720 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000721 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000722 Align = EltInfo.second;
723 break;
724 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000725 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000726 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000727 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
728 Width = Layout.getSize();
729 Align = Layout.getAlignment();
730 break;
731 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000732 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000733 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000734 const TagType *TT = cast<TagType>(T);
735
736 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000737 Width = 1;
738 Align = 1;
739 break;
740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Daniel Dunbar1d751182008-11-08 05:48:37 +0000742 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000743 return getTypeInfo(ET->getDecl()->getIntegerType());
744
Daniel Dunbar1d751182008-11-08 05:48:37 +0000745 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000746 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
747 Width = Layout.getSize();
748 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000749 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000750 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000751
John McCall7da24312009-09-05 00:15:47 +0000752 case Type::Elaborated: {
753 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType().getTypePtr());
754 }
755
Douglas Gregor18857642009-04-30 17:32:17 +0000756 case Type::Typedef: {
757 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000758 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregor18857642009-04-30 17:32:17 +0000759 Align = Aligned->getAlignment();
760 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
761 } else
762 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000763 break;
Chris Lattner71763312008-04-06 22:05:18 +0000764 }
Douglas Gregor18857642009-04-30 17:32:17 +0000765
766 case Type::TypeOfExpr:
767 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
768 .getTypePtr());
769
770 case Type::TypeOf:
771 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
772
Anders Carlsson395b4752009-06-24 19:06:50 +0000773 case Type::Decltype:
774 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
775 .getTypePtr());
776
Douglas Gregor18857642009-04-30 17:32:17 +0000777 case Type::QualifiedName:
778 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Douglas Gregor18857642009-04-30 17:32:17 +0000780 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000781 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000782 "Cannot request the size of a dependent type");
783 // FIXME: this is likely to be wrong once we support template
784 // aliases, since a template alias could refer to a typedef that
785 // has an __aligned__ attribute on it.
786 return getTypeInfo(getCanonicalType(T));
787 }
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Chris Lattner464175b2007-07-18 17:52:12 +0000789 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000790 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000791}
792
Chris Lattner34ebde42009-01-27 18:08:34 +0000793/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
794/// type for the current target in bits. This can be different than the ABI
795/// alignment in cases where it is beneficial for performance to overalign
796/// a data type.
797unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
798 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000799
800 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000801 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000802 T = CT->getElementType().getTypePtr();
803 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
804 T->isSpecificBuiltinType(BuiltinType::LongLong))
805 return std::max(ABIAlign, (unsigned)getTypeSize(T));
806
Chris Lattner34ebde42009-01-27 18:08:34 +0000807 return ABIAlign;
808}
809
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000810static void CollectLocalObjCIvars(ASTContext *Ctx,
811 const ObjCInterfaceDecl *OI,
812 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000813 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
814 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000815 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000816 if (!IVDecl->isInvalidDecl())
817 Fields.push_back(cast<FieldDecl>(IVDecl));
818 }
819}
820
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000821void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
822 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
823 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
824 CollectObjCIvars(SuperClass, Fields);
825 CollectLocalObjCIvars(this, OI, Fields);
826}
827
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000828/// ShallowCollectObjCIvars -
829/// Collect all ivars, including those synthesized, in the current class.
830///
831void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
832 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
833 bool CollectSynthesized) {
834 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
835 E = OI->ivar_end(); I != E; ++I) {
836 Ivars.push_back(*I);
837 }
838 if (CollectSynthesized)
839 CollectSynthesizedIvars(OI, Ivars);
840}
841
Fariborz Jahanian98200742009-05-12 18:14:29 +0000842void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
843 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000844 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
845 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian98200742009-05-12 18:14:29 +0000846 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
847 Ivars.push_back(Ivar);
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Fariborz Jahanian98200742009-05-12 18:14:29 +0000849 // Also look into nested protocols.
850 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
851 E = PD->protocol_end(); P != E; ++P)
852 CollectProtocolSynthesizedIvars(*P, Ivars);
853}
854
855/// CollectSynthesizedIvars -
856/// This routine collect synthesized ivars for the designated class.
857///
858void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
859 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000860 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
861 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000862 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
863 Ivars.push_back(Ivar);
864 }
865 // Also look into interface's protocol list for properties declared
866 // in the protocol and whose ivars are synthesized.
867 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
868 PE = OI->protocol_end(); P != PE; ++P) {
869 ObjCProtocolDecl *PD = (*P);
870 CollectProtocolSynthesizedIvars(PD, Ivars);
871 }
872}
873
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000874unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
875 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000876 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
877 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000878 if ((*I)->getPropertyIvarDecl())
879 ++count;
880
881 // Also look into nested protocols.
882 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
883 E = PD->protocol_end(); P != E; ++P)
884 count += CountProtocolSynthesizedIvars(*P);
885 return count;
886}
887
Mike Stump1eb44332009-09-09 15:08:12 +0000888unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000889 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000890 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
891 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000892 if ((*I)->getPropertyIvarDecl())
893 ++count;
894 }
895 // Also look into interface's protocol list for properties declared
896 // in the protocol and whose ivars are synthesized.
897 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
898 PE = OI->protocol_end(); P != PE; ++P) {
899 ObjCProtocolDecl *PD = (*P);
900 count += CountProtocolSynthesizedIvars(PD);
901 }
902 return count;
903}
904
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000905/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
906ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
907 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
908 I = ObjCImpls.find(D);
909 if (I != ObjCImpls.end())
910 return cast<ObjCImplementationDecl>(I->second);
911 return 0;
912}
913/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
914ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
915 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
916 I = ObjCImpls.find(D);
917 if (I != ObjCImpls.end())
918 return cast<ObjCCategoryImplDecl>(I->second);
919 return 0;
920}
921
922/// \brief Set the implementation of ObjCInterfaceDecl.
923void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
924 ObjCImplementationDecl *ImplD) {
925 assert(IFaceD && ImplD && "Passed null params");
926 ObjCImpls[IFaceD] = ImplD;
927}
928/// \brief Set the implementation of ObjCCategoryDecl.
929void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
930 ObjCCategoryImplDecl *ImplD) {
931 assert(CatD && ImplD && "Passed null params");
932 ObjCImpls[CatD] = ImplD;
933}
934
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000935/// \brief Allocate an uninitialized DeclaratorInfo.
936///
937/// The caller should initialize the memory held by DeclaratorInfo using
938/// the TypeLoc wrappers.
939///
940/// \param T the type that will be the basis for type source info. This type
941/// should refer to how the declarator was written in source code, not to
942/// what type semantic analysis resolved the declarator to.
943DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) {
944 unsigned DataSize = TypeLoc::getFullDataSizeForType(T);
945 DeclaratorInfo *DInfo =
946 (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
947 new (DInfo) DeclaratorInfo(T);
948 return DInfo;
949}
950
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000951/// getInterfaceLayoutImpl - Get or compute information about the
952/// layout of the given interface.
953///
954/// \param Impl - If given, also include the layout of the interface's
955/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000956const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000957ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
958 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000959 assert(!D->isForwardDecl() && "Invalid interface decl!");
960
Devang Patel44a3dde2008-06-04 21:54:36 +0000961 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000962 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000963 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
964 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
965 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000966
Daniel Dunbar453addb2009-05-03 11:16:44 +0000967 // Add in synthesized ivar count if laying out an implementation.
968 if (Impl) {
Anders Carlsson29445a02009-07-18 21:19:52 +0000969 unsigned FieldCount = D->ivar_size();
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000970 unsigned SynthCount = CountSynthesizedIvars(D);
971 FieldCount += SynthCount;
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000972 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000973 // entry. Note we can't cache this because we simply free all
974 // entries later; however we shouldn't look up implementations
975 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000976 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000977 return getObjCLayout(D, 0);
978 }
979
Mike Stump1eb44332009-09-09 15:08:12 +0000980 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000981 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
982 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Devang Patel44a3dde2008-06-04 21:54:36 +0000984 return *NewEntry;
985}
986
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000987const ASTRecordLayout &
988ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
989 return getObjCLayout(D, 0);
990}
991
992const ASTRecordLayout &
993ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
994 return getObjCLayout(D->getClassInterface(), D);
995}
996
Devang Patel88a981b2007-11-01 19:11:01 +0000997/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000998/// specified record (struct/union/class), which indicates its size and field
999/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +00001000const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001001 D = D->getDefinition(*this);
1002 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +00001003
Chris Lattner464175b2007-07-18 17:52:12 +00001004 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +00001005 // Note that we can't save a reference to the entry because this function
1006 // is recursive.
1007 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +00001008 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +00001009
Mike Stump1eb44332009-09-09 15:08:12 +00001010 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +00001011 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +00001012 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Chris Lattner5d2a6302007-07-18 18:26:58 +00001014 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001015}
1016
Chris Lattnera7674d82007-07-13 22:13:22 +00001017//===----------------------------------------------------------------------===//
1018// Type creation/memoization methods
1019//===----------------------------------------------------------------------===//
1020
John McCall0953e762009-09-24 19:53:00 +00001021QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1022 unsigned Fast = Quals.getFastQualifiers();
1023 Quals.removeFastQualifiers();
1024
1025 // Check if we've already instantiated this type.
1026 llvm::FoldingSetNodeID ID;
1027 ExtQuals::Profile(ID, TypeNode, Quals);
1028 void *InsertPos = 0;
1029 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1030 assert(EQ->getQualifiers() == Quals);
1031 QualType T = QualType(EQ, Fast);
1032 return T;
1033 }
1034
John McCall6b304a02009-09-24 23:30:46 +00001035 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001036 ExtQualNodes.InsertNode(New, InsertPos);
1037 QualType T = QualType(New, Fast);
1038 return T;
1039}
1040
1041QualType ASTContext::getVolatileType(QualType T) {
1042 QualType CanT = getCanonicalType(T);
1043 if (CanT.isVolatileQualified()) return T;
1044
1045 QualifierCollector Quals;
1046 const Type *TypeNode = Quals.strip(T);
1047 Quals.addVolatile();
1048
1049 return getExtQualType(TypeNode, Quals);
1050}
1051
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001052QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001053 QualType CanT = getCanonicalType(T);
1054 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001055 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001056
John McCall0953e762009-09-24 19:53:00 +00001057 // If we are composing extended qualifiers together, merge together
1058 // into one ExtQuals node.
1059 QualifierCollector Quals;
1060 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
John McCall0953e762009-09-24 19:53:00 +00001062 // If this type already has an address space specified, it cannot get
1063 // another one.
1064 assert(!Quals.hasAddressSpace() &&
1065 "Type cannot be in multiple addr spaces!");
1066 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001067
John McCall0953e762009-09-24 19:53:00 +00001068 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001069}
1070
Chris Lattnerb7d25532009-02-18 22:53:11 +00001071QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001072 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001073 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001074 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001075 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001077 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001078 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001079 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001080 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1081 return getPointerType(ResultType);
1082 }
1083 }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
John McCall0953e762009-09-24 19:53:00 +00001085 // If we are composing extended qualifiers together, merge together
1086 // into one ExtQuals node.
1087 QualifierCollector Quals;
1088 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001089
John McCall0953e762009-09-24 19:53:00 +00001090 // If this type already has an ObjCGC specified, it cannot get
1091 // another one.
1092 assert(!Quals.hasObjCGCAttr() &&
1093 "Type cannot have multiple ObjCGCs!");
1094 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001095
John McCall0953e762009-09-24 19:53:00 +00001096 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001097}
Chris Lattnera7674d82007-07-13 22:13:22 +00001098
Mike Stump24556362009-07-25 21:26:53 +00001099QualType ASTContext::getNoReturnType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00001100 QualType ResultType;
Mike Stump24556362009-07-25 21:26:53 +00001101 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001102 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001103 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001104 ResultType = getPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001105 } else if (T->isBlockPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001106 QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001107 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001108 ResultType = getBlockPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001109 } else {
1110 assert (T->isFunctionType()
1111 && "can't noreturn qualify non-pointer to function or block type");
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Benjamin Kramerbdbeeb52009-09-25 11:47:22 +00001113 if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) {
1114 ResultType = getFunctionNoProtoType(FNPT->getResultType(), true);
John McCall0953e762009-09-24 19:53:00 +00001115 } else {
1116 const FunctionProtoType *F = T->getAs<FunctionProtoType>();
1117 ResultType
1118 = getFunctionType(F->getResultType(), F->arg_type_begin(),
1119 F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
1120 F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
1121 F->getNumExceptions(), F->exception_begin(), true);
1122 }
Mike Stump24556362009-07-25 21:26:53 +00001123 }
John McCall0953e762009-09-24 19:53:00 +00001124
1125 return getQualifiedType(ResultType, T.getQualifiers());
Mike Stump24556362009-07-25 21:26:53 +00001126}
1127
Reid Spencer5f016e22007-07-11 17:01:13 +00001128/// getComplexType - Return the uniqued reference to the type for a complex
1129/// number with the specified element type.
1130QualType ASTContext::getComplexType(QualType T) {
1131 // Unique pointers, to guarantee there is only one pointer of a particular
1132 // structure.
1133 llvm::FoldingSetNodeID ID;
1134 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 void *InsertPos = 0;
1137 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1138 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 // If the pointee type isn't canonical, this won't be a canonical type either,
1141 // so fill in the canonical type field.
1142 QualType Canonical;
1143 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001144 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 // Get the new insert position for the node we care about.
1147 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001148 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 }
John McCall6b304a02009-09-24 23:30:46 +00001150 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 Types.push_back(New);
1152 ComplexTypes.InsertNode(New, InsertPos);
1153 return QualType(New, 0);
1154}
1155
Eli Friedmanf98aba32009-02-13 02:31:07 +00001156QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1157 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1158 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1159 FixedWidthIntType *&Entry = Map[Width];
1160 if (!Entry)
1161 Entry = new FixedWidthIntType(Width, Signed);
1162 return QualType(Entry, 0);
1163}
Reid Spencer5f016e22007-07-11 17:01:13 +00001164
1165/// getPointerType - Return the uniqued reference to the type for a pointer to
1166/// the specified type.
1167QualType ASTContext::getPointerType(QualType T) {
1168 // Unique pointers, to guarantee there is only one pointer of a particular
1169 // structure.
1170 llvm::FoldingSetNodeID ID;
1171 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 void *InsertPos = 0;
1174 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1175 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 // If the pointee type isn't canonical, this won't be a canonical type either,
1178 // so fill in the canonical type field.
1179 QualType Canonical;
1180 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001181 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 // Get the new insert position for the node we care about.
1184 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001185 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 }
John McCall6b304a02009-09-24 23:30:46 +00001187 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 Types.push_back(New);
1189 PointerTypes.InsertNode(New, InsertPos);
1190 return QualType(New, 0);
1191}
1192
Mike Stump1eb44332009-09-09 15:08:12 +00001193/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001194/// a pointer to the specified block.
1195QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001196 assert(T->isFunctionType() && "block of function types only");
1197 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001198 // structure.
1199 llvm::FoldingSetNodeID ID;
1200 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Steve Naroff5618bd42008-08-27 16:04:49 +00001202 void *InsertPos = 0;
1203 if (BlockPointerType *PT =
1204 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1205 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001206
1207 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001208 // type either so fill in the canonical type field.
1209 QualType Canonical;
1210 if (!T->isCanonical()) {
1211 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Steve Naroff5618bd42008-08-27 16:04:49 +00001213 // Get the new insert position for the node we care about.
1214 BlockPointerType *NewIP =
1215 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001216 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001217 }
John McCall6b304a02009-09-24 23:30:46 +00001218 BlockPointerType *New
1219 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001220 Types.push_back(New);
1221 BlockPointerTypes.InsertNode(New, InsertPos);
1222 return QualType(New, 0);
1223}
1224
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001225/// getLValueReferenceType - Return the uniqued reference to the type for an
1226/// lvalue reference to the specified type.
1227QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 // Unique pointers, to guarantee there is only one pointer of a particular
1229 // structure.
1230 llvm::FoldingSetNodeID ID;
1231 ReferenceType::Profile(ID, T);
1232
1233 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001234 if (LValueReferenceType *RT =
1235 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001237
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 // If the referencee type isn't canonical, this won't be a canonical type
1239 // either, so fill in the canonical type field.
1240 QualType Canonical;
1241 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001242 Canonical = getLValueReferenceType(getCanonicalType(T));
1243
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001245 LValueReferenceType *NewIP =
1246 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001247 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 }
1249
John McCall6b304a02009-09-24 23:30:46 +00001250 LValueReferenceType *New
1251 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001253 LValueReferenceTypes.InsertNode(New, InsertPos);
1254 return QualType(New, 0);
1255}
1256
1257/// getRValueReferenceType - Return the uniqued reference to the type for an
1258/// rvalue reference to the specified type.
1259QualType ASTContext::getRValueReferenceType(QualType T) {
1260 // Unique pointers, to guarantee there is only one pointer of a particular
1261 // structure.
1262 llvm::FoldingSetNodeID ID;
1263 ReferenceType::Profile(ID, T);
1264
1265 void *InsertPos = 0;
1266 if (RValueReferenceType *RT =
1267 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1268 return QualType(RT, 0);
1269
1270 // If the referencee type isn't canonical, this won't be a canonical type
1271 // either, so fill in the canonical type field.
1272 QualType Canonical;
1273 if (!T->isCanonical()) {
1274 Canonical = getRValueReferenceType(getCanonicalType(T));
1275
1276 // Get the new insert position for the node we care about.
1277 RValueReferenceType *NewIP =
1278 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1279 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1280 }
1281
John McCall6b304a02009-09-24 23:30:46 +00001282 RValueReferenceType *New
1283 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001284 Types.push_back(New);
1285 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 return QualType(New, 0);
1287}
1288
Sebastian Redlf30208a2009-01-24 21:16:55 +00001289/// getMemberPointerType - Return the uniqued reference to the type for a
1290/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001291QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001292 // Unique pointers, to guarantee there is only one pointer of a particular
1293 // structure.
1294 llvm::FoldingSetNodeID ID;
1295 MemberPointerType::Profile(ID, T, Cls);
1296
1297 void *InsertPos = 0;
1298 if (MemberPointerType *PT =
1299 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1300 return QualType(PT, 0);
1301
1302 // If the pointee or class type isn't canonical, this won't be a canonical
1303 // type either, so fill in the canonical type field.
1304 QualType Canonical;
1305 if (!T->isCanonical()) {
1306 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1307
1308 // Get the new insert position for the node we care about.
1309 MemberPointerType *NewIP =
1310 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1311 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1312 }
John McCall6b304a02009-09-24 23:30:46 +00001313 MemberPointerType *New
1314 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001315 Types.push_back(New);
1316 MemberPointerTypes.InsertNode(New, InsertPos);
1317 return QualType(New, 0);
1318}
1319
Mike Stump1eb44332009-09-09 15:08:12 +00001320/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001321/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001322QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001323 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001324 ArrayType::ArraySizeModifier ASM,
1325 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001326 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1327 "Constant array of VLAs is illegal!");
1328
Chris Lattner38aeec72009-05-13 04:12:56 +00001329 // Convert the array size into a canonical width matching the pointer size for
1330 // the target.
1331 llvm::APInt ArySize(ArySizeIn);
1332 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Reid Spencer5f016e22007-07-11 17:01:13 +00001334 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001335 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001338 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001339 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 // If the element type isn't canonical, this won't be a canonical type either,
1343 // so fill in the canonical type field.
1344 QualType Canonical;
1345 if (!EltTy->isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001346 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001347 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001348 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001349 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001350 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001351 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001352 }
Mike Stump1eb44332009-09-09 15:08:12 +00001353
John McCall6b304a02009-09-24 23:30:46 +00001354 ConstantArrayType *New = new(*this,TypeAlignment)
1355 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001356 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001357 Types.push_back(New);
1358 return QualType(New, 0);
1359}
1360
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001361/// getConstantArrayWithExprType - Return a reference to the type for
1362/// an array of the specified element type.
1363QualType
1364ASTContext::getConstantArrayWithExprType(QualType EltTy,
1365 const llvm::APInt &ArySizeIn,
1366 Expr *ArySizeExpr,
1367 ArrayType::ArraySizeModifier ASM,
1368 unsigned EltTypeQuals,
1369 SourceRange Brackets) {
1370 // Convert the array size into a canonical width matching the pointer
1371 // size for the target.
1372 llvm::APInt ArySize(ArySizeIn);
1373 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1374
1375 // Compute the canonical ConstantArrayType.
1376 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1377 ArySize, ASM, EltTypeQuals);
1378 // Since we don't unique expressions, it isn't possible to unique VLA's
1379 // that have an expression provided for their size.
John McCall6b304a02009-09-24 23:30:46 +00001380 ConstantArrayWithExprType *New = new(*this, TypeAlignment)
1381 ConstantArrayWithExprType(EltTy, Canonical, ArySize, ArySizeExpr,
1382 ASM, EltTypeQuals, Brackets);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001383 Types.push_back(New);
1384 return QualType(New, 0);
1385}
1386
1387/// getConstantArrayWithoutExprType - Return a reference to the type for
1388/// an array of the specified element type.
1389QualType
1390ASTContext::getConstantArrayWithoutExprType(QualType EltTy,
1391 const llvm::APInt &ArySizeIn,
1392 ArrayType::ArraySizeModifier ASM,
1393 unsigned EltTypeQuals) {
1394 // Convert the array size into a canonical width matching the pointer
1395 // size for the target.
1396 llvm::APInt ArySize(ArySizeIn);
1397 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1398
1399 // Compute the canonical ConstantArrayType.
1400 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1401 ArySize, ASM, EltTypeQuals);
John McCall6b304a02009-09-24 23:30:46 +00001402 ConstantArrayWithoutExprType *New = new(*this, TypeAlignment)
1403 ConstantArrayWithoutExprType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001404 Types.push_back(New);
1405 return QualType(New, 0);
1406}
1407
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001408/// getVariableArrayType - Returns a non-unique reference to the type for a
1409/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001410QualType ASTContext::getVariableArrayType(QualType EltTy,
1411 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001412 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001413 unsigned EltTypeQuals,
1414 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001415 // Since we don't unique expressions, it isn't possible to unique VLA's
1416 // that have an expression provided for their size.
1417
John McCall6b304a02009-09-24 23:30:46 +00001418 VariableArrayType *New = new(*this, TypeAlignment)
1419 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001420
1421 VariableArrayTypes.push_back(New);
1422 Types.push_back(New);
1423 return QualType(New, 0);
1424}
1425
Douglas Gregor898574e2008-12-05 23:32:09 +00001426/// getDependentSizedArrayType - Returns a non-unique reference to
1427/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001428/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001429QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1430 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001431 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001432 unsigned EltTypeQuals,
1433 SourceRange Brackets) {
Mike Stump1eb44332009-09-09 15:08:12 +00001434 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001435 "Size must be type- or value-dependent!");
1436
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001437 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001438 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001439 EltTypeQuals, NumElts);
Douglas Gregor898574e2008-12-05 23:32:09 +00001440
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001441 void *InsertPos = 0;
1442 DependentSizedArrayType *Canon
1443 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1444 DependentSizedArrayType *New;
1445 if (Canon) {
1446 // We already have a canonical version of this array type; use it as
1447 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001448 New = new (*this, TypeAlignment)
1449 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1450 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001451 } else {
1452 QualType CanonEltTy = getCanonicalType(EltTy);
1453 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001454 New = new (*this, TypeAlignment)
1455 DependentSizedArrayType(*this, EltTy, QualType(),
1456 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001457 DependentSizedArrayTypes.InsertNode(New, InsertPos);
1458 } else {
1459 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1460 ASM, EltTypeQuals,
1461 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001462 New = new (*this, TypeAlignment)
1463 DependentSizedArrayType(*this, EltTy, Canon,
1464 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001465 }
1466 }
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Douglas Gregor898574e2008-12-05 23:32:09 +00001468 Types.push_back(New);
1469 return QualType(New, 0);
1470}
1471
Eli Friedmanc5773c42008-02-15 18:16:39 +00001472QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1473 ArrayType::ArraySizeModifier ASM,
1474 unsigned EltTypeQuals) {
1475 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001476 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001477
1478 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001479 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001480 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1481 return QualType(ATP, 0);
1482
1483 // If the element type isn't canonical, this won't be a canonical type
1484 // either, so fill in the canonical type field.
1485 QualType Canonical;
1486
1487 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001488 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001489 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001490
1491 // Get the new insert position for the node we care about.
1492 IncompleteArrayType *NewIP =
1493 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001494 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001495 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001496
John McCall6b304a02009-09-24 23:30:46 +00001497 IncompleteArrayType *New = new (*this, TypeAlignment)
1498 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001499
1500 IncompleteArrayTypes.InsertNode(New, InsertPos);
1501 Types.push_back(New);
1502 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001503}
1504
Steve Naroff73322922007-07-18 18:00:27 +00001505/// getVectorType - Return the unique reference to a vector type of
1506/// the specified element type and size. VectorType must be a built-in type.
1507QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Chris Lattnerf52ab252008-04-06 22:59:24 +00001510 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001511 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Reid Spencer5f016e22007-07-11 17:01:13 +00001513 // Check if we've already instantiated a vector of this type.
1514 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001515 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001516 void *InsertPos = 0;
1517 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1518 return QualType(VTP, 0);
1519
1520 // If the element type isn't canonical, this won't be a canonical type either,
1521 // so fill in the canonical type field.
1522 QualType Canonical;
1523 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001524 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Reid Spencer5f016e22007-07-11 17:01:13 +00001526 // Get the new insert position for the node we care about.
1527 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001528 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 }
John McCall6b304a02009-09-24 23:30:46 +00001530 VectorType *New = new (*this, TypeAlignment)
1531 VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001532 VectorTypes.InsertNode(New, InsertPos);
1533 Types.push_back(New);
1534 return QualType(New, 0);
1535}
1536
Nate Begeman213541a2008-04-18 23:10:10 +00001537/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001538/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001539QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001540 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Chris Lattnerf52ab252008-04-06 22:59:24 +00001542 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001543 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Steve Naroff73322922007-07-18 18:00:27 +00001545 // Check if we've already instantiated a vector of this type.
1546 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001547 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001548 void *InsertPos = 0;
1549 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1550 return QualType(VTP, 0);
1551
1552 // If the element type isn't canonical, this won't be a canonical type either,
1553 // so fill in the canonical type field.
1554 QualType Canonical;
1555 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001556 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Steve Naroff73322922007-07-18 18:00:27 +00001558 // Get the new insert position for the node we care about.
1559 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001560 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001561 }
John McCall6b304a02009-09-24 23:30:46 +00001562 ExtVectorType *New = new (*this, TypeAlignment)
1563 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001564 VectorTypes.InsertNode(New, InsertPos);
1565 Types.push_back(New);
1566 return QualType(New, 0);
1567}
1568
Mike Stump1eb44332009-09-09 15:08:12 +00001569QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001570 Expr *SizeExpr,
1571 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001572 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001573 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001574 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001576 void *InsertPos = 0;
1577 DependentSizedExtVectorType *Canon
1578 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1579 DependentSizedExtVectorType *New;
1580 if (Canon) {
1581 // We already have a canonical version of this array type; use it as
1582 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001583 New = new (*this, TypeAlignment)
1584 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1585 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001586 } else {
1587 QualType CanonVecTy = getCanonicalType(vecType);
1588 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001589 New = new (*this, TypeAlignment)
1590 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1591 AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001592 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1593 } else {
1594 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1595 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001596 New = new (*this, TypeAlignment)
1597 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001598 }
1599 }
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001601 Types.push_back(New);
1602 return QualType(New, 0);
1603}
1604
Douglas Gregor72564e72009-02-26 23:50:07 +00001605/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001606///
Mike Stump24556362009-07-25 21:26:53 +00001607QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001608 // Unique functions, to guarantee there is only one function of a particular
1609 // structure.
1610 llvm::FoldingSetNodeID ID;
Mike Stump24556362009-07-25 21:26:53 +00001611 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Reid Spencer5f016e22007-07-11 17:01:13 +00001613 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001614 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001615 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Reid Spencer5f016e22007-07-11 17:01:13 +00001618 QualType Canonical;
1619 if (!ResultTy->isCanonical()) {
Mike Stump24556362009-07-25 21:26:53 +00001620 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001623 FunctionNoProtoType *NewIP =
1624 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001625 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001626 }
Mike Stump1eb44332009-09-09 15:08:12 +00001627
John McCall6b304a02009-09-24 23:30:46 +00001628 FunctionNoProtoType *New = new (*this, TypeAlignment)
1629 FunctionNoProtoType(ResultTy, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001631 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001632 return QualType(New, 0);
1633}
1634
1635/// getFunctionType - Return a normal function type with a typed argument
1636/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001637QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001638 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001639 unsigned TypeQuals, bool hasExceptionSpec,
1640 bool hasAnyExceptionSpec, unsigned NumExs,
Mike Stump24556362009-07-25 21:26:53 +00001641 const QualType *ExArray, bool NoReturn) {
Anders Carlsson83913e32009-09-16 23:47:08 +00001642 if (LangOpts.CPlusPlus) {
1643 for (unsigned i = 0; i != NumArgs; ++i)
John McCall0953e762009-09-24 19:53:00 +00001644 assert(!ArgArray[i].hasQualifiers() &&
1645 "C++ arguments can't have toplevel qualifiers!");
Anders Carlsson83913e32009-09-16 23:47:08 +00001646 }
1647
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 // Unique functions, to guarantee there is only one function of a particular
1649 // structure.
1650 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001651 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001652 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001653 NumExs, ExArray, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001654
1655 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001656 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001657 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001658 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001659
1660 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001661 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001662 if (hasExceptionSpec)
1663 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001664 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1665 if (!ArgArray[i]->isCanonical())
1666 isCanonical = false;
1667
1668 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001669 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 QualType Canonical;
1671 if (!isCanonical) {
1672 llvm::SmallVector<QualType, 16> CanonicalArgs;
1673 CanonicalArgs.reserve(NumArgs);
1674 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001675 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001676
Chris Lattnerf52ab252008-04-06 22:59:24 +00001677 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001678 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001679 isVariadic, TypeQuals, false,
1680 false, 0, 0, NoReturn);
Sebastian Redl465226e2009-05-27 22:11:52 +00001681
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001683 FunctionProtoType *NewIP =
1684 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001685 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001687
Douglas Gregor72564e72009-02-26 23:50:07 +00001688 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001689 // for two variable size arrays (for parameter and exception types) at the
1690 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001691 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001692 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1693 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001694 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001695 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001696 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001697 ExArray, NumExs, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001699 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001700 return QualType(FTP, 0);
1701}
1702
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001703/// getTypeDeclType - Return the unique reference to the type for the
1704/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001705QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001706 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001707 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001709 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001710 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001711 else if (isa<TemplateTypeParmDecl>(Decl)) {
1712 assert(false && "Template type parameter types are always available.");
Mike Stump9fdbab32009-07-31 02:02:20 +00001713 } else if (ObjCInterfaceDecl *ObjCInterface
1714 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001715 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001716
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001717 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001718 if (PrevDecl)
1719 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001720 else
John McCall6b304a02009-09-24 23:30:46 +00001721 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
Mike Stump9fdbab32009-07-31 02:02:20 +00001722 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001723 if (PrevDecl)
1724 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001725 else
John McCall6b304a02009-09-24 23:30:46 +00001726 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
Mike Stump9fdbab32009-07-31 02:02:20 +00001727 } else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001728 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001729
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001730 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001731 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001732}
1733
Reid Spencer5f016e22007-07-11 17:01:13 +00001734/// getTypedefType - Return the unique reference to the type for the
1735/// specified typename decl.
1736QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1737 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Chris Lattnerf52ab252008-04-06 22:59:24 +00001739 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001740 Decl->TypeForDecl = new(*this, TypeAlignment)
1741 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 Types.push_back(Decl->TypeForDecl);
1743 return QualType(Decl->TypeForDecl, 0);
1744}
1745
Douglas Gregorfab9d672009-02-05 23:33:38 +00001746/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001747/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001748/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001749QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001750 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001751 IdentifierInfo *Name) {
1752 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001753 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001754 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001755 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001756 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1757
1758 if (TypeParm)
1759 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001761 if (Name) {
1762 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall6b304a02009-09-24 23:30:46 +00001763 TypeParm = new (*this, TypeAlignment)
1764 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001765 } else
John McCall6b304a02009-09-24 23:30:46 +00001766 TypeParm = new (*this, TypeAlignment)
1767 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001768
1769 Types.push_back(TypeParm);
1770 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1771
1772 return QualType(TypeParm, 0);
1773}
1774
Mike Stump1eb44332009-09-09 15:08:12 +00001775QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001776ASTContext::getTemplateSpecializationType(TemplateName Template,
1777 const TemplateArgument *Args,
1778 unsigned NumArgs,
1779 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001780 if (!Canon.isNull())
1781 Canon = getCanonicalType(Canon);
1782 else {
1783 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001784 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1785 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1786 CanonArgs.reserve(NumArgs);
1787 for (unsigned I = 0; I != NumArgs; ++I)
1788 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1789
1790 // Determine whether this canonical template specialization type already
1791 // exists.
1792 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001793 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001794 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001795
1796 void *InsertPos = 0;
1797 TemplateSpecializationType *Spec
1798 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Douglas Gregor1275ae02009-07-28 23:00:59 +00001800 if (!Spec) {
1801 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001802 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001803 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001804 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001805 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001806 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001807 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001808 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001809 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001810 }
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Douglas Gregorb88e8882009-07-30 17:40:51 +00001812 if (Canon.isNull())
1813 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001814 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001815 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001816 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001817
Douglas Gregor1275ae02009-07-28 23:00:59 +00001818 // Allocate the (non-canonical) template specialization type, but don't
1819 // try to unique it: these types typically have location information that
1820 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001821 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001822 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001823 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001824 TemplateSpecializationType *Spec
1825 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001826 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Douglas Gregor55f6b142009-02-09 18:46:07 +00001828 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001829 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001830}
1831
Mike Stump1eb44332009-09-09 15:08:12 +00001832QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001833ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001834 QualType NamedType) {
1835 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001836 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001837
1838 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001839 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001840 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1841 if (T)
1842 return QualType(T, 0);
1843
Mike Stump1eb44332009-09-09 15:08:12 +00001844 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001845 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001846 Types.push_back(T);
1847 QualifiedNameTypes.InsertNode(T, InsertPos);
1848 return QualType(T, 0);
1849}
1850
Mike Stump1eb44332009-09-09 15:08:12 +00001851QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001852 const IdentifierInfo *Name,
1853 QualType Canon) {
1854 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1855
1856 if (Canon.isNull()) {
1857 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1858 if (CanonNNS != NNS)
1859 Canon = getTypenameType(CanonNNS, Name);
1860 }
1861
1862 llvm::FoldingSetNodeID ID;
1863 TypenameType::Profile(ID, NNS, Name);
1864
1865 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001866 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001867 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1868 if (T)
1869 return QualType(T, 0);
1870
1871 T = new (*this) TypenameType(NNS, Name, Canon);
1872 Types.push_back(T);
1873 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001874 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001875}
1876
Mike Stump1eb44332009-09-09 15:08:12 +00001877QualType
1878ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001879 const TemplateSpecializationType *TemplateId,
1880 QualType Canon) {
1881 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1882
1883 if (Canon.isNull()) {
1884 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1885 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1886 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1887 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00001888 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00001889 assert(CanonTemplateId &&
1890 "Canonical type must also be a template specialization type");
1891 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1892 }
1893 }
1894
1895 llvm::FoldingSetNodeID ID;
1896 TypenameType::Profile(ID, NNS, TemplateId);
1897
1898 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001899 TypenameType *T
Douglas Gregor17343172009-04-01 00:28:59 +00001900 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1901 if (T)
1902 return QualType(T, 0);
1903
1904 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1905 Types.push_back(T);
1906 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001907 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00001908}
1909
John McCall7da24312009-09-05 00:15:47 +00001910QualType
1911ASTContext::getElaboratedType(QualType UnderlyingType,
1912 ElaboratedType::TagKind Tag) {
1913 llvm::FoldingSetNodeID ID;
1914 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001915
John McCall7da24312009-09-05 00:15:47 +00001916 void *InsertPos = 0;
1917 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1918 if (T)
1919 return QualType(T, 0);
1920
1921 QualType Canon = getCanonicalType(UnderlyingType);
1922
1923 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1924 Types.push_back(T);
1925 ElaboratedTypes.InsertNode(T, InsertPos);
1926 return QualType(T, 0);
1927}
1928
Chris Lattner88cb27a2008-04-07 04:56:42 +00001929/// CmpProtocolNames - Comparison predicate for sorting protocols
1930/// alphabetically.
1931static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1932 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001933 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001934}
1935
1936static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1937 unsigned &NumProtocols) {
1938 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Chris Lattner88cb27a2008-04-07 04:56:42 +00001940 // Sort protocols, keyed by name.
1941 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1942
1943 // Remove duplicates.
1944 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1945 NumProtocols = ProtocolsEnd-Protocols;
1946}
1947
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001948/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1949/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00001950QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00001951 ObjCProtocolDecl **Protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001952 unsigned NumProtocols) {
1953 // Sort the protocol list alphabetically to canonicalize it.
1954 if (NumProtocols)
1955 SortAndUniqueProtocols(Protocols, NumProtocols);
1956
1957 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00001958 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001959
1960 void *InsertPos = 0;
1961 if (ObjCObjectPointerType *QT =
1962 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1963 return QualType(QT, 0);
1964
1965 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001966 ObjCObjectPointerType *QType = new (*this, TypeAlignment)
1967 ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001969 Types.push_back(QType);
1970 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1971 return QualType(QType, 0);
1972}
Chris Lattner88cb27a2008-04-07 04:56:42 +00001973
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001974/// getObjCInterfaceType - Return the unique reference to the type for the
1975/// specified ObjC interface decl. The list of protocols is optional.
1976QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001977 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Mike Stump1eb44332009-09-09 15:08:12 +00001978 if (NumProtocols)
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001979 // Sort the protocol list alphabetically to canonicalize it.
1980 SortAndUniqueProtocols(Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001982 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001983 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001985 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001986 if (ObjCInterfaceType *QT =
1987 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001988 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001989
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001990 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001991 ObjCInterfaceType *QType = new (*this, TypeAlignment)
1992 ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1993 Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001994 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001995 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001996 return QualType(QType, 0);
1997}
1998
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00001999QualType ASTContext::getObjCProtocolListType(QualType T,
2000 ObjCProtocolDecl **Protocols,
2001 unsigned NumProtocols) {
2002 llvm::FoldingSetNodeID ID;
2003 ObjCProtocolListType::Profile(ID, T, Protocols, NumProtocols);
2004
2005 void *InsertPos = 0;
2006 if (ObjCProtocolListType *QT =
2007 ObjCProtocolListTypes.FindNodeOrInsertPos(ID, InsertPos))
2008 return QualType(QT, 0);
2009
2010 // No Match;
2011 ObjCProtocolListType *QType = new (*this, TypeAlignment)
2012 ObjCProtocolListType(T, Protocols, NumProtocols);
2013 Types.push_back(QType);
2014 ObjCProtocolListTypes.InsertNode(QType, InsertPos);
2015 return QualType(QType, 0);
2016}
2017
Douglas Gregor72564e72009-02-26 23:50:07 +00002018/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2019/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002020/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002021/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002022/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002023QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002024 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002025 if (tofExpr->isTypeDependent()) {
2026 llvm::FoldingSetNodeID ID;
2027 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002028
Douglas Gregorb1975722009-07-30 23:18:24 +00002029 void *InsertPos = 0;
2030 DependentTypeOfExprType *Canon
2031 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2032 if (Canon) {
2033 // We already have a "canonical" version of an identical, dependent
2034 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002035 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002036 QualType((TypeOfExprType*)Canon, 0));
2037 }
2038 else {
2039 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002040 Canon
2041 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002042 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2043 toe = Canon;
2044 }
2045 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002046 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002047 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002048 }
Steve Naroff9752f252007-08-01 18:02:17 +00002049 Types.push_back(toe);
2050 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002051}
2052
Steve Naroff9752f252007-08-01 18:02:17 +00002053/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2054/// TypeOfType AST's. The only motivation to unique these nodes would be
2055/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002056/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002057/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002058QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002059 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002060 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002061 Types.push_back(tot);
2062 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002063}
2064
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002065/// getDecltypeForExpr - Given an expr, will return the decltype for that
2066/// expression, according to the rules in C++0x [dcl.type.simple]p4
2067static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002068 if (e->isTypeDependent())
2069 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002070
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002071 // If e is an id expression or a class member access, decltype(e) is defined
2072 // as the type of the entity named by e.
2073 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2074 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2075 return VD->getType();
2076 }
2077 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2078 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2079 return FD->getType();
2080 }
2081 // If e is a function call or an invocation of an overloaded operator,
2082 // (parentheses around e are ignored), decltype(e) is defined as the
2083 // return type of that function.
2084 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2085 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002086
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002087 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002088
2089 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002090 // defined as T&, otherwise decltype(e) is defined as T.
2091 if (e->isLvalue(Context) == Expr::LV_Valid)
2092 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002093
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002094 return T;
2095}
2096
Anders Carlsson395b4752009-06-24 19:06:50 +00002097/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2098/// DecltypeType AST's. The only motivation to unique these nodes would be
2099/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002100/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002101/// on canonical type's (which are always unique).
2102QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002103 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002104 if (e->isTypeDependent()) {
2105 llvm::FoldingSetNodeID ID;
2106 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002108 void *InsertPos = 0;
2109 DependentDecltypeType *Canon
2110 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2111 if (Canon) {
2112 // We already have a "canonical" version of an equivalent, dependent
2113 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002114 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002115 QualType((DecltypeType*)Canon, 0));
2116 }
2117 else {
2118 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002119 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002120 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2121 dt = Canon;
2122 }
2123 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002124 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002125 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002126 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002127 Types.push_back(dt);
2128 return QualType(dt, 0);
2129}
2130
Reid Spencer5f016e22007-07-11 17:01:13 +00002131/// getTagDeclType - Return the unique reference to the type for the
2132/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002133QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002134 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002135 // FIXME: What is the design on getTagDeclType when it requires casting
2136 // away const? mutable?
2137 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002138}
2139
Mike Stump1eb44332009-09-09 15:08:12 +00002140/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2141/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2142/// needs to agree with the definition in <stddef.h>.
Reid Spencer5f016e22007-07-11 17:01:13 +00002143QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002144 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002145}
2146
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002147/// getSignedWCharType - Return the type of "signed wchar_t".
2148/// Used when in C++, as a GCC extension.
2149QualType ASTContext::getSignedWCharType() const {
2150 // FIXME: derive from "Target" ?
2151 return WCharTy;
2152}
2153
2154/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2155/// Used when in C++, as a GCC extension.
2156QualType ASTContext::getUnsignedWCharType() const {
2157 // FIXME: derive from "Target" ?
2158 return UnsignedIntTy;
2159}
2160
Chris Lattner8b9023b2007-07-13 03:05:23 +00002161/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2162/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2163QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002164 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002165}
2166
Chris Lattnere6327742008-04-02 05:18:44 +00002167//===----------------------------------------------------------------------===//
2168// Type Operators
2169//===----------------------------------------------------------------------===//
2170
Chris Lattner77c96472008-04-06 22:41:35 +00002171/// getCanonicalType - Return the canonical (structural) type corresponding to
2172/// the specified potentially non-canonical type. The non-canonical version
2173/// of a type may have many "decorated" versions of types. Decorators can
2174/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2175/// to be free of any of these, allowing two canonical types to be compared
2176/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002177CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002178 QualifierCollector Quals;
2179 const Type *Ptr = Quals.strip(T);
2180 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002181
John McCall0953e762009-09-24 19:53:00 +00002182 // The canonical internal type will be the canonical type *except*
2183 // that we push type qualifiers down through array types.
2184
2185 // If there are no new qualifiers to push down, stop here.
2186 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002187 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002188
John McCall0953e762009-09-24 19:53:00 +00002189 // If the type qualifiers are on an array type, get the canonical
2190 // type of the array with the qualifiers applied to the element
2191 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002192 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2193 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002194 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002195
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002196 // Get the canonical version of the element with the extra qualifiers on it.
2197 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002198 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002199 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002201 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002202 return CanQualType::CreateUnsafe(
2203 getConstantArrayType(NewEltTy, CAT->getSize(),
2204 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002205 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002206 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002207 return CanQualType::CreateUnsafe(
2208 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002209 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Douglas Gregor898574e2008-12-05 23:32:09 +00002211 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002212 return CanQualType::CreateUnsafe(
2213 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002214 DSAT->getSizeExpr() ?
2215 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002216 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002217 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002218 DSAT->getBracketsRange()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002219
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002220 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002221 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002222 VAT->getSizeExpr() ?
2223 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002224 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002225 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002226 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002227}
2228
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002229TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2230 // If this template name refers to a template, the canonical
2231 // template name merely stores the template itself.
2232 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002233 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002234
Mike Stump1eb44332009-09-09 15:08:12 +00002235 // If this template name refers to a set of overloaded function templates,
Douglas Gregord99cbe62009-07-29 18:26:50 +00002236 /// the canonical template name merely stores the set of function templates.
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002237 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2238 OverloadedFunctionDecl *CanonOvl = 0;
2239 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2240 FEnd = Ovl->function_end();
2241 F != FEnd; ++F) {
2242 Decl *Canon = F->get()->getCanonicalDecl();
2243 if (CanonOvl || Canon != F->get()) {
2244 if (!CanonOvl)
Mike Stump1eb44332009-09-09 15:08:12 +00002245 CanonOvl = OverloadedFunctionDecl::Create(*this,
2246 Ovl->getDeclContext(),
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002247 Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002249 CanonOvl->addOverload(
2250 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2251 }
2252 }
Mike Stump1eb44332009-09-09 15:08:12 +00002253
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002254 return TemplateName(CanonOvl? CanonOvl : Ovl);
2255 }
Mike Stump1eb44332009-09-09 15:08:12 +00002256
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002257 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2258 assert(DTN && "Non-dependent template names must refer to template decls.");
2259 return DTN->CanonicalTemplateName;
2260}
2261
Mike Stump1eb44332009-09-09 15:08:12 +00002262TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002263ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2264 switch (Arg.getKind()) {
2265 case TemplateArgument::Null:
2266 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Douglas Gregor1275ae02009-07-28 23:00:59 +00002268 case TemplateArgument::Expression:
2269 // FIXME: Build canonical expression?
2270 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Douglas Gregor1275ae02009-07-28 23:00:59 +00002272 case TemplateArgument::Declaration:
2273 return TemplateArgument(SourceLocation(),
2274 Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002275
Douglas Gregor1275ae02009-07-28 23:00:59 +00002276 case TemplateArgument::Integral:
2277 return TemplateArgument(SourceLocation(),
2278 *Arg.getAsIntegral(),
2279 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002280
Douglas Gregor1275ae02009-07-28 23:00:59 +00002281 case TemplateArgument::Type:
2282 return TemplateArgument(SourceLocation(),
2283 getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002284
Douglas Gregor1275ae02009-07-28 23:00:59 +00002285 case TemplateArgument::Pack: {
2286 // FIXME: Allocate in ASTContext
2287 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2288 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002289 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002290 AEnd = Arg.pack_end();
2291 A != AEnd; (void)++A, ++Idx)
2292 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002293
Douglas Gregor1275ae02009-07-28 23:00:59 +00002294 TemplateArgument Result;
2295 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2296 return Result;
2297 }
2298 }
2299
2300 // Silence GCC warning
2301 assert(false && "Unhandled template argument kind");
2302 return TemplateArgument();
2303}
2304
Douglas Gregord57959a2009-03-27 23:10:48 +00002305NestedNameSpecifier *
2306ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002307 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002308 return 0;
2309
2310 switch (NNS->getKind()) {
2311 case NestedNameSpecifier::Identifier:
2312 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002313 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002314 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2315 NNS->getAsIdentifier());
2316
2317 case NestedNameSpecifier::Namespace:
2318 // A namespace is canonical; build a nested-name-specifier with
2319 // this namespace and no prefix.
2320 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2321
2322 case NestedNameSpecifier::TypeSpec:
2323 case NestedNameSpecifier::TypeSpecWithTemplate: {
2324 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002325 return NestedNameSpecifier::Create(*this, 0,
2326 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002327 T.getTypePtr());
2328 }
2329
2330 case NestedNameSpecifier::Global:
2331 // The global specifier is canonical and unique.
2332 return NNS;
2333 }
2334
2335 // Required to silence a GCC warning
2336 return 0;
2337}
2338
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002339
2340const ArrayType *ASTContext::getAsArrayType(QualType T) {
2341 // Handle the non-qualified case efficiently.
John McCall0953e762009-09-24 19:53:00 +00002342 if (!T.hasQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002343 // Handle the common positive case fast.
2344 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2345 return AT;
2346 }
Mike Stump1eb44332009-09-09 15:08:12 +00002347
John McCall0953e762009-09-24 19:53:00 +00002348 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002349 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002350 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002351 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002352
John McCall0953e762009-09-24 19:53:00 +00002353 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002354 // implements C99 6.7.3p8: "If the specification of an array type includes
2355 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002356
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002357 // If we get here, we either have type qualifiers on the type, or we have
2358 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002359 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002360
John McCall0953e762009-09-24 19:53:00 +00002361 QualifierCollector Qs;
2362 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002364 // If we have a simple case, just return now.
2365 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002366 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002367 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002368
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002369 // Otherwise, we have an array and we have qualifiers on it. Push the
2370 // qualifiers into the array element type and return a new array type.
2371 // Get the canonical version of the element with the extra qualifiers on it.
2372 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002373 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002374
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002375 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2376 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2377 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002378 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002379 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2380 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2381 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002382 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002383
Mike Stump1eb44332009-09-09 15:08:12 +00002384 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002385 = dyn_cast<DependentSizedArrayType>(ATy))
2386 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002387 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002388 DSAT->getSizeExpr() ?
2389 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002390 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002391 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002392 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002393
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002394 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002395 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002396 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002397 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002398 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002399 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002400 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002401}
2402
2403
Chris Lattnere6327742008-04-02 05:18:44 +00002404/// getArrayDecayedType - Return the properly qualified result of decaying the
2405/// specified array type to a pointer. This operation is non-trivial when
2406/// handling typedefs etc. The canonical type of "T" must be an array type,
2407/// this returns a pointer to a properly qualified element of the array.
2408///
2409/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2410QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002411 // Get the element type with 'getAsArrayType' so that we don't lose any
2412 // typedefs in the element type of the array. This also handles propagation
2413 // of type qualifiers from the array type into the element type if present
2414 // (C99 6.7.3p8).
2415 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2416 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002417
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002418 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002419
2420 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002421 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002422}
2423
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002424QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002425 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002426 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002427 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002428 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2429 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002430 } else {
John McCall0953e762009-09-24 19:53:00 +00002431 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002432 }
2433 }
2434}
2435
Anders Carlssonfbbce492009-09-25 01:23:32 +00002436QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2437 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002438
Anders Carlssonfbbce492009-09-25 01:23:32 +00002439 if (const ArrayType *AT = getAsArrayType(ElemTy))
2440 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002441
Anders Carlsson6183a992008-12-21 03:44:36 +00002442 return ElemTy;
2443}
2444
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002445/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002446uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002447ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2448 uint64_t ElementCount = 1;
2449 do {
2450 ElementCount *= CA->getSize().getZExtValue();
2451 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2452 } while (CA);
2453 return ElementCount;
2454}
2455
Reid Spencer5f016e22007-07-11 17:01:13 +00002456/// getFloatingRank - Return a relative rank for floating point types.
2457/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002458static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002459 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002460 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002461
John McCall183700f2009-09-21 23:43:11 +00002462 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2463 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002464 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002465 case BuiltinType::Float: return FloatRank;
2466 case BuiltinType::Double: return DoubleRank;
2467 case BuiltinType::LongDouble: return LongDoubleRank;
2468 }
2469}
2470
Mike Stump1eb44332009-09-09 15:08:12 +00002471/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2472/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002473/// 'typeDomain' is a real floating point or complex type.
2474/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002475QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2476 QualType Domain) const {
2477 FloatingRank EltRank = getFloatingRank(Size);
2478 if (Domain->isComplexType()) {
2479 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002480 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002481 case FloatRank: return FloatComplexTy;
2482 case DoubleRank: return DoubleComplexTy;
2483 case LongDoubleRank: return LongDoubleComplexTy;
2484 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002485 }
Chris Lattner1361b112008-04-06 23:58:54 +00002486
2487 assert(Domain->isRealFloatingType() && "Unknown domain!");
2488 switch (EltRank) {
2489 default: assert(0 && "getFloatingRank(): illegal value for rank");
2490 case FloatRank: return FloatTy;
2491 case DoubleRank: return DoubleTy;
2492 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002493 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002494}
2495
Chris Lattner7cfeb082008-04-06 23:55:33 +00002496/// getFloatingTypeOrder - Compare the rank of the two specified floating
2497/// point types, ignoring the domain of the type (i.e. 'double' ==
2498/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002499/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002500int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2501 FloatingRank LHSR = getFloatingRank(LHS);
2502 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002503
Chris Lattnera75cea32008-04-06 23:38:49 +00002504 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002505 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002506 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002507 return 1;
2508 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002509}
2510
Chris Lattnerf52ab252008-04-06 22:59:24 +00002511/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2512/// routine will assert if passed a built-in type that isn't an integer or enum,
2513/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002514unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002515 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002516 if (EnumType* ET = dyn_cast<EnumType>(T))
2517 T = ET->getDecl()->getIntegerType().getTypePtr();
2518
Eli Friedmana3426752009-07-05 23:44:27 +00002519 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2520 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2521
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002522 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2523 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2524
2525 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2526 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2527
Eli Friedmanf98aba32009-02-13 02:31:07 +00002528 // There are two things which impact the integer rank: the width, and
2529 // the ordering of builtins. The builtin ordering is encoded in the
2530 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner1b63e4f2009-06-14 01:54:56 +00002531 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanf98aba32009-02-13 02:31:07 +00002532 return FWIT->getWidth() << 3;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002533
Chris Lattnerf52ab252008-04-06 22:59:24 +00002534 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002535 default: assert(0 && "getIntegerRank(): not a built-in integer");
2536 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002537 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002538 case BuiltinType::Char_S:
2539 case BuiltinType::Char_U:
2540 case BuiltinType::SChar:
2541 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002542 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002543 case BuiltinType::Short:
2544 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002545 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002546 case BuiltinType::Int:
2547 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002548 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002549 case BuiltinType::Long:
2550 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002551 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002552 case BuiltinType::LongLong:
2553 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002554 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002555 case BuiltinType::Int128:
2556 case BuiltinType::UInt128:
2557 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002558 }
2559}
2560
Eli Friedman04e83572009-08-20 04:21:42 +00002561/// \brief Whether this is a promotable bitfield reference according
2562/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2563///
2564/// \returns the type this bit-field will promote to, or NULL if no
2565/// promotion occurs.
2566QualType ASTContext::isPromotableBitField(Expr *E) {
2567 FieldDecl *Field = E->getBitField();
2568 if (!Field)
2569 return QualType();
2570
2571 QualType FT = Field->getType();
2572
2573 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2574 uint64_t BitWidth = BitWidthAP.getZExtValue();
2575 uint64_t IntSize = getTypeSize(IntTy);
2576 // GCC extension compatibility: if the bit-field size is less than or equal
2577 // to the size of int, it gets promoted no matter what its type is.
2578 // For instance, unsigned long bf : 4 gets promoted to signed int.
2579 if (BitWidth < IntSize)
2580 return IntTy;
2581
2582 if (BitWidth == IntSize)
2583 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2584
2585 // Types bigger than int are not subject to promotions, and therefore act
2586 // like the base type.
2587 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2588 // is ridiculous.
2589 return QualType();
2590}
2591
Eli Friedmana95d7572009-08-19 07:44:53 +00002592/// getPromotedIntegerType - Returns the type that Promotable will
2593/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2594/// integer type.
2595QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2596 assert(!Promotable.isNull());
2597 assert(Promotable->isPromotableIntegerType());
2598 if (Promotable->isSignedIntegerType())
2599 return IntTy;
2600 uint64_t PromotableSize = getTypeSize(Promotable);
2601 uint64_t IntSize = getTypeSize(IntTy);
2602 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2603 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2604}
2605
Mike Stump1eb44332009-09-09 15:08:12 +00002606/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002607/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002608/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002609int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002610 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2611 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002612 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002613
Chris Lattnerf52ab252008-04-06 22:59:24 +00002614 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2615 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002616
Chris Lattner7cfeb082008-04-06 23:55:33 +00002617 unsigned LHSRank = getIntegerRank(LHSC);
2618 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Chris Lattner7cfeb082008-04-06 23:55:33 +00002620 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2621 if (LHSRank == RHSRank) return 0;
2622 return LHSRank > RHSRank ? 1 : -1;
2623 }
Mike Stump1eb44332009-09-09 15:08:12 +00002624
Chris Lattner7cfeb082008-04-06 23:55:33 +00002625 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2626 if (LHSUnsigned) {
2627 // If the unsigned [LHS] type is larger, return it.
2628 if (LHSRank >= RHSRank)
2629 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002630
Chris Lattner7cfeb082008-04-06 23:55:33 +00002631 // If the signed type can represent all values of the unsigned type, it
2632 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002633 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002634 return -1;
2635 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002636
Chris Lattner7cfeb082008-04-06 23:55:33 +00002637 // If the unsigned [RHS] type is larger, return it.
2638 if (RHSRank >= LHSRank)
2639 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002640
Chris Lattner7cfeb082008-04-06 23:55:33 +00002641 // If the signed type can represent all values of the unsigned type, it
2642 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002643 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002644 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002645}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002646
Mike Stump1eb44332009-09-09 15:08:12 +00002647// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002648QualType ASTContext::getCFConstantStringType() {
2649 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002650 CFConstantStringTypeDecl =
2651 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002652 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002653 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002654
Anders Carlsson71993dd2007-08-17 05:31:46 +00002655 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002656 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002657 // int flags;
2658 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002659 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002660 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002661 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002662 FieldTypes[3] = LongTy;
2663
Anders Carlsson71993dd2007-08-17 05:31:46 +00002664 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002665 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002666 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002667 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002668 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002669 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002670 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002671 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002672 }
2673
2674 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002675 }
Mike Stump1eb44332009-09-09 15:08:12 +00002676
Anders Carlsson71993dd2007-08-17 05:31:46 +00002677 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002678}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002679
Douglas Gregor319ac892009-04-23 22:29:11 +00002680void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002681 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002682 assert(Rec && "Invalid CFConstantStringType");
2683 CFConstantStringTypeDecl = Rec->getDecl();
2684}
2685
Mike Stump1eb44332009-09-09 15:08:12 +00002686QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002687 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002688 ObjCFastEnumerationStateTypeDecl =
2689 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2690 &Idents.get("__objcFastEnumerationState"));
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002692 QualType FieldTypes[] = {
2693 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002694 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002695 getPointerType(UnsignedLongTy),
2696 getConstantArrayType(UnsignedLongTy,
2697 llvm::APInt(32, 5), ArrayType::Normal, 0)
2698 };
Mike Stump1eb44332009-09-09 15:08:12 +00002699
Douglas Gregor44b43212008-12-11 16:49:14 +00002700 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002701 FieldDecl *Field = FieldDecl::Create(*this,
2702 ObjCFastEnumerationStateTypeDecl,
2703 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002704 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002705 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002706 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002707 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002708 }
Mike Stump1eb44332009-09-09 15:08:12 +00002709
Douglas Gregor44b43212008-12-11 16:49:14 +00002710 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002711 }
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002713 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2714}
2715
Douglas Gregor319ac892009-04-23 22:29:11 +00002716void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002717 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002718 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2719 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2720}
2721
Anders Carlssone8c49532007-10-29 06:33:42 +00002722// This returns true if a type has been typedefed to BOOL:
2723// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002724static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002725 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002726 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2727 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002729 return false;
2730}
2731
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002732/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002733/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002734int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002735 uint64_t sz = getTypeSize(type);
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002737 // Make all integer and enum types at least as large as an int
2738 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002739 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002740 // Treat arrays as pointers, since that's how they're passed in.
2741 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002742 sz = getTypeSize(VoidPtrTy);
2743 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002744}
2745
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002746/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002747/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002748void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002749 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002750 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002751 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002752 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002753 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002754 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002755 // Compute size of all parameters.
2756 // Start with computing size of a pointer in number of bytes.
2757 // FIXME: There might(should) be a better way of doing this computation!
2758 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002759 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002760 // The first two arguments (self and _cmd) are pointers; account for
2761 // their size.
2762 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002763 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2764 E = Decl->param_end(); PI != E; ++PI) {
2765 QualType PType = (*PI)->getType();
2766 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002767 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002768 ParmOffset += sz;
2769 }
2770 S += llvm::utostr(ParmOffset);
2771 S += "@0:";
2772 S += llvm::utostr(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002774 // Argument types.
2775 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002776 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2777 E = Decl->param_end(); PI != E; ++PI) {
2778 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00002779 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002780 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002781 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2782 // Use array's original type only if it has known number of
2783 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002784 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002785 PType = PVDecl->getType();
2786 } else if (PType->isFunctionType())
2787 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002788 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002789 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002790 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002791 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002792 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002793 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002794 }
2795}
2796
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002797/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002798/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002799/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2800/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00002801/// Property attributes are stored as a comma-delimited C string. The simple
2802/// attributes readonly and bycopy are encoded as single characters. The
2803/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2804/// encoded as single characters, followed by an identifier. Property types
2805/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002806/// these attributes are defined by the following enumeration:
2807/// @code
2808/// enum PropertyAttributes {
2809/// kPropertyReadOnly = 'R', // property is read-only.
2810/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2811/// kPropertyByref = '&', // property is a reference to the value last assigned
2812/// kPropertyDynamic = 'D', // property is dynamic
2813/// kPropertyGetter = 'G', // followed by getter selector name
2814/// kPropertySetter = 'S', // followed by setter selector name
2815/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2816/// kPropertyType = 't' // followed by old-style type encoding.
2817/// kPropertyWeak = 'W' // 'weak' property
2818/// kPropertyStrong = 'P' // property GC'able
2819/// kPropertyNonAtomic = 'N' // property non-atomic
2820/// };
2821/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00002822void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002823 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002824 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002825 // Collect information from the property implementation decl(s).
2826 bool Dynamic = false;
2827 ObjCPropertyImplDecl *SynthesizePID = 0;
2828
2829 // FIXME: Duplicated code due to poor abstraction.
2830 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00002831 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002832 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2833 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002834 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002835 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002836 ObjCPropertyImplDecl *PID = *i;
2837 if (PID->getPropertyDecl() == PD) {
2838 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2839 Dynamic = true;
2840 } else {
2841 SynthesizePID = PID;
2842 }
2843 }
2844 }
2845 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002846 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002847 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002848 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002849 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002850 ObjCPropertyImplDecl *PID = *i;
2851 if (PID->getPropertyDecl() == PD) {
2852 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2853 Dynamic = true;
2854 } else {
2855 SynthesizePID = PID;
2856 }
2857 }
Mike Stump1eb44332009-09-09 15:08:12 +00002858 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002859 }
2860 }
2861
2862 // FIXME: This is not very efficient.
2863 S = "T";
2864
2865 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002866 // GCC has some special rules regarding encoding of properties which
2867 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00002868 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002869 true /* outermost type */,
2870 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002871
2872 if (PD->isReadOnly()) {
2873 S += ",R";
2874 } else {
2875 switch (PD->getSetterKind()) {
2876 case ObjCPropertyDecl::Assign: break;
2877 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002878 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002879 }
2880 }
2881
2882 // It really isn't clear at all what this means, since properties
2883 // are "dynamic by default".
2884 if (Dynamic)
2885 S += ",D";
2886
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002887 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2888 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00002889
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002890 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2891 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002892 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002893 }
2894
2895 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2896 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002897 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002898 }
2899
2900 if (SynthesizePID) {
2901 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2902 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002903 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002904 }
2905
2906 // FIXME: OBJCGC: weak & strong
2907}
2908
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002909/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00002910/// Another legacy compatibility encoding: 32-bit longs are encoded as
2911/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002912/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2913///
2914void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00002915 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00002916 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002917 if (BT->getKind() == BuiltinType::ULong &&
2918 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002919 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002920 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002921 if (BT->getKind() == BuiltinType::Long &&
2922 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002923 PointeeTy = IntTy;
2924 }
2925 }
2926}
2927
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002928void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002929 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002930 // We follow the behavior of gcc, expanding structures which are
2931 // directly pointed to, and expanding embedded structures. Note that
2932 // these rules are sufficient to prevent recursive encoding of the
2933 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00002934 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002935 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002936}
2937
Mike Stump1eb44332009-09-09 15:08:12 +00002938static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002939 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002940 const Expr *E = FD->getBitWidth();
2941 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2942 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002943 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002944 S += 'b';
2945 S += llvm::utostr(N);
2946}
2947
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002948void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2949 bool ExpandPointedToStructures,
2950 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002951 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002952 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002953 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00002954 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002955 if (FD && FD->isBitField())
2956 return EncodeBitField(this, S, FD);
2957 char encoding;
2958 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002959 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002960 case BuiltinType::Void: encoding = 'v'; break;
2961 case BuiltinType::Bool: encoding = 'B'; break;
2962 case BuiltinType::Char_U:
2963 case BuiltinType::UChar: encoding = 'C'; break;
2964 case BuiltinType::UShort: encoding = 'S'; break;
2965 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002966 case BuiltinType::ULong:
2967 encoding =
2968 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002969 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002970 case BuiltinType::UInt128: encoding = 'T'; break;
2971 case BuiltinType::ULongLong: encoding = 'Q'; break;
2972 case BuiltinType::Char_S:
2973 case BuiltinType::SChar: encoding = 'c'; break;
2974 case BuiltinType::Short: encoding = 's'; break;
2975 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002976 case BuiltinType::Long:
2977 encoding =
2978 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002979 break;
2980 case BuiltinType::LongLong: encoding = 'q'; break;
2981 case BuiltinType::Int128: encoding = 't'; break;
2982 case BuiltinType::Float: encoding = 'f'; break;
2983 case BuiltinType::Double: encoding = 'd'; break;
2984 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002985 }
Mike Stump1eb44332009-09-09 15:08:12 +00002986
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002987 S += encoding;
2988 return;
2989 }
Mike Stump1eb44332009-09-09 15:08:12 +00002990
John McCall183700f2009-09-21 23:43:11 +00002991 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002992 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00002993 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002994 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002995 return;
2996 }
Mike Stump1eb44332009-09-09 15:08:12 +00002997
Ted Kremenek6217b802009-07-29 21:53:49 +00002998 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002999 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003000 bool isReadOnly = false;
3001 // For historical/compatibility reasons, the read-only qualifier of the
3002 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3003 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003004 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003005 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003006 if (OutermostType && T.isConstQualified()) {
3007 isReadOnly = true;
3008 S += 'r';
3009 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003010 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003011 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003012 while (P->getAs<PointerType>())
3013 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003014 if (P.isConstQualified()) {
3015 isReadOnly = true;
3016 S += 'r';
3017 }
3018 }
3019 if (isReadOnly) {
3020 // Another legacy compatibility encoding. Some ObjC qualifier and type
3021 // combinations need to be rearranged.
3022 // Rewrite "in const" from "nr" to "rn"
3023 const char * s = S.c_str();
3024 int len = S.length();
3025 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3026 std::string replace = "rn";
3027 S.replace(S.end()-2, S.end(), replace);
3028 }
3029 }
Steve Naroff14108da2009-07-10 23:34:53 +00003030 if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00003031 S += ':';
3032 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00003033 }
Mike Stump1eb44332009-09-09 15:08:12 +00003034
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003035 if (PointeeTy->isCharType()) {
3036 // char pointer types should be encoded as '*' unless it is a
3037 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003038 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003039 S += '*';
3040 return;
3041 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003042 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003043 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3044 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3045 S += '#';
3046 return;
3047 }
3048 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3049 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3050 S += '@';
3051 return;
3052 }
3053 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003054 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003055 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003056 getLegacyIntegralTypeEncoding(PointeeTy);
3057
Mike Stump1eb44332009-09-09 15:08:12 +00003058 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003059 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003060 return;
3061 }
Mike Stump1eb44332009-09-09 15:08:12 +00003062
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003063 if (const ArrayType *AT =
3064 // Ignore type qualifiers etc.
3065 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003066 if (isa<IncompleteArrayType>(AT)) {
3067 // Incomplete arrays are encoded as a pointer to the array element.
3068 S += '^';
3069
Mike Stump1eb44332009-09-09 15:08:12 +00003070 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003071 false, ExpandStructures, FD);
3072 } else {
3073 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003074
Anders Carlsson559a8332009-02-22 01:38:57 +00003075 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3076 S += llvm::utostr(CAT->getSize().getZExtValue());
3077 else {
3078 //Variable length arrays are encoded as a regular array with 0 elements.
3079 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3080 S += '0';
3081 }
Mike Stump1eb44332009-09-09 15:08:12 +00003082
3083 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003084 false, ExpandStructures, FD);
3085 S += ']';
3086 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003087 return;
3088 }
Mike Stump1eb44332009-09-09 15:08:12 +00003089
John McCall183700f2009-09-21 23:43:11 +00003090 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003091 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003092 return;
3093 }
Mike Stump1eb44332009-09-09 15:08:12 +00003094
Ted Kremenek6217b802009-07-29 21:53:49 +00003095 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003096 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003097 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003098 // Anonymous structures print as '?'
3099 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3100 S += II->getName();
3101 } else {
3102 S += '?';
3103 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003104 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003105 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003106 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3107 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003108 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003109 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003110 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003111 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003112 S += '"';
3113 }
Mike Stump1eb44332009-09-09 15:08:12 +00003114
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003115 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003116 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003117 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003118 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003119 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003120 QualType qt = Field->getType();
3121 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003122 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003123 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003124 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003125 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003126 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003127 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003128 return;
3129 }
Mike Stump1eb44332009-09-09 15:08:12 +00003130
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003131 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003132 if (FD && FD->isBitField())
3133 EncodeBitField(this, S, FD);
3134 else
3135 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003136 return;
3137 }
Mike Stump1eb44332009-09-09 15:08:12 +00003138
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003139 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003140 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003141 return;
3142 }
Mike Stump1eb44332009-09-09 15:08:12 +00003143
John McCall0953e762009-09-24 19:53:00 +00003144 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003145 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003146 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003147 S += '{';
3148 const IdentifierInfo *II = OI->getIdentifier();
3149 S += II->getName();
3150 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003151 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003152 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003153 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003154 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003155 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003156 RecFields[i]);
3157 else
Mike Stump1eb44332009-09-09 15:08:12 +00003158 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003159 FD);
3160 }
3161 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003162 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003163 }
Mike Stump1eb44332009-09-09 15:08:12 +00003164
John McCall183700f2009-09-21 23:43:11 +00003165 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003166 if (OPT->isObjCIdType()) {
3167 S += '@';
3168 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003169 }
Mike Stump1eb44332009-09-09 15:08:12 +00003170
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003171 if (OPT->isObjCClassType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003172 S += '#';
3173 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003174 }
Mike Stump1eb44332009-09-09 15:08:12 +00003175
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003176 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003177 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003178 ExpandPointedToStructures,
3179 ExpandStructures, FD);
3180 if (FD || EncodingProperty) {
3181 // Note that we do extended encoding of protocol qualifer list
3182 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003183 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003184 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3185 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003186 S += '<';
3187 S += (*I)->getNameAsString();
3188 S += '>';
3189 }
3190 S += '"';
3191 }
3192 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003193 }
Mike Stump1eb44332009-09-09 15:08:12 +00003194
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003195 QualType PointeeTy = OPT->getPointeeType();
3196 if (!EncodingProperty &&
3197 isa<TypedefType>(PointeeTy.getTypePtr())) {
3198 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003199 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003200 // {...};
3201 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003202 getObjCEncodingForTypeImpl(PointeeTy, S,
3203 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003204 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003205 return;
3206 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003207
3208 S += '@';
3209 if (FD || EncodingProperty) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003210 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003211 S += OPT->getInterfaceDecl()->getNameAsCString();
3212 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3213 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003214 S += '<';
3215 S += (*I)->getNameAsString();
3216 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003217 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003218 S += '"';
3219 }
3220 return;
3221 }
Mike Stump1eb44332009-09-09 15:08:12 +00003222
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003223 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003224}
3225
Mike Stump1eb44332009-09-09 15:08:12 +00003226void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003227 std::string& S) const {
3228 if (QT & Decl::OBJC_TQ_In)
3229 S += 'n';
3230 if (QT & Decl::OBJC_TQ_Inout)
3231 S += 'N';
3232 if (QT & Decl::OBJC_TQ_Out)
3233 S += 'o';
3234 if (QT & Decl::OBJC_TQ_Bycopy)
3235 S += 'O';
3236 if (QT & Decl::OBJC_TQ_Byref)
3237 S += 'R';
3238 if (QT & Decl::OBJC_TQ_Oneway)
3239 S += 'V';
3240}
3241
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003242void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003243 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003244
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003245 BuiltinVaListType = T;
3246}
3247
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003248void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003249 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003250}
3251
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003252void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor319ac892009-04-23 22:29:11 +00003253 ObjCSelType = T;
3254
John McCall183700f2009-09-21 23:43:11 +00003255 const TypedefType *TT = T->getAs<TypedefType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003256 if (!TT)
3257 return;
3258 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003259
3260 // typedef struct objc_selector *SEL;
Ted Kremenek6217b802009-07-29 21:53:49 +00003261 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003262 if (!ptr)
3263 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003264 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003265 if (!rec)
3266 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003267 SelStructType = rec;
3268}
3269
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003270void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003271 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003272}
3273
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003274void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003275 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003276}
3277
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003278void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003279 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003280 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003281
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003282 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003283}
3284
Douglas Gregor7532dc62009-03-30 22:58:21 +00003285/// \brief Retrieve the template name that represents a qualified
3286/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003287TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003288 bool TemplateKeyword,
3289 TemplateDecl *Template) {
3290 llvm::FoldingSetNodeID ID;
3291 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3292
3293 void *InsertPos = 0;
3294 QualifiedTemplateName *QTN =
3295 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3296 if (!QTN) {
3297 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3298 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3299 }
3300
3301 return TemplateName(QTN);
3302}
3303
Douglas Gregord99cbe62009-07-29 18:26:50 +00003304/// \brief Retrieve the template name that represents a qualified
3305/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003306TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregord99cbe62009-07-29 18:26:50 +00003307 bool TemplateKeyword,
3308 OverloadedFunctionDecl *Template) {
3309 llvm::FoldingSetNodeID ID;
3310 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump1eb44332009-09-09 15:08:12 +00003311
Douglas Gregord99cbe62009-07-29 18:26:50 +00003312 void *InsertPos = 0;
3313 QualifiedTemplateName *QTN =
3314 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3315 if (!QTN) {
3316 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3317 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3318 }
Mike Stump1eb44332009-09-09 15:08:12 +00003319
Douglas Gregord99cbe62009-07-29 18:26:50 +00003320 return TemplateName(QTN);
3321}
3322
Douglas Gregor7532dc62009-03-30 22:58:21 +00003323/// \brief Retrieve the template name that represents a dependent
3324/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003325TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003326 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003327 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003328 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003329
3330 llvm::FoldingSetNodeID ID;
3331 DependentTemplateName::Profile(ID, NNS, Name);
3332
3333 void *InsertPos = 0;
3334 DependentTemplateName *QTN =
3335 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3336
3337 if (QTN)
3338 return TemplateName(QTN);
3339
3340 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3341 if (CanonNNS == NNS) {
3342 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3343 } else {
3344 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3345 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3346 }
3347
3348 DependentTemplateNames.InsertNode(QTN, InsertPos);
3349 return TemplateName(QTN);
3350}
3351
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003352/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003353/// TargetInfo, produce the corresponding type. The unsigned @p Type
3354/// is actually a value of type @c TargetInfo::IntType.
3355QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003356 switch (Type) {
Mike Stump1eb44332009-09-09 15:08:12 +00003357 case TargetInfo::NoInt: return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003358 case TargetInfo::SignedShort: return ShortTy;
3359 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3360 case TargetInfo::SignedInt: return IntTy;
3361 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3362 case TargetInfo::SignedLong: return LongTy;
3363 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3364 case TargetInfo::SignedLongLong: return LongLongTy;
3365 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3366 }
3367
3368 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00003369 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003370}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003371
3372//===----------------------------------------------------------------------===//
3373// Type Predicates.
3374//===----------------------------------------------------------------------===//
3375
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003376/// isObjCNSObjectType - Return true if this is an NSObject object using
3377/// NSObject attribute on a c-style pointer type.
3378/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003379/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003380///
3381bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3382 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3383 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003384 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003385 return true;
3386 }
Mike Stump1eb44332009-09-09 15:08:12 +00003387 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003388}
3389
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003390/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3391/// garbage collection attribute.
3392///
John McCall0953e762009-09-24 19:53:00 +00003393Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3394 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003395 if (getLangOptions().ObjC1 &&
3396 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003397 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003398 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003399 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003400 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003401 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003402 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003403 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003404 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003405 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003406 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003407 // Non-pointers have none gc'able attribute regardless of the attribute
3408 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003409 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003410 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003411 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003412 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003413}
3414
Chris Lattner6ac46a42008-04-07 06:51:04 +00003415//===----------------------------------------------------------------------===//
3416// Type Compatibility Testing
3417//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003418
Mike Stump1eb44332009-09-09 15:08:12 +00003419/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003420/// compatible.
3421static bool areCompatVectorTypes(const VectorType *LHS,
3422 const VectorType *RHS) {
3423 assert(LHS->isCanonical() && RHS->isCanonical());
3424 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003425 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003426}
3427
Steve Naroff4084c302009-07-23 01:01:38 +00003428//===----------------------------------------------------------------------===//
3429// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3430//===----------------------------------------------------------------------===//
3431
3432/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3433/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003434bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3435 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003436 if (lProto == rProto)
3437 return true;
3438 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3439 E = rProto->protocol_end(); PI != E; ++PI)
3440 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3441 return true;
3442 return false;
3443}
3444
Steve Naroff4084c302009-07-23 01:01:38 +00003445/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3446/// return true if lhs's protocols conform to rhs's protocol; false
3447/// otherwise.
3448bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3449 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3450 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3451 return false;
3452}
3453
3454/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3455/// ObjCQualifiedIDType.
3456bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3457 bool compare) {
3458 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003459 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003460 lhs->isObjCIdType() || lhs->isObjCClassType())
3461 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003462 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003463 rhs->isObjCIdType() || rhs->isObjCClassType())
3464 return true;
3465
3466 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003467 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003468
Steve Naroff4084c302009-07-23 01:01:38 +00003469 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003470
Steve Naroff4084c302009-07-23 01:01:38 +00003471 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003472 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003473 // make sure we check the class hierarchy.
3474 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3475 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3476 E = lhsQID->qual_end(); I != E; ++I) {
3477 // when comparing an id<P> on lhs with a static type on rhs,
3478 // see if static class implements all of id's protocols, directly or
3479 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003480 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003481 return false;
3482 }
3483 }
3484 // If there are no qualifiers and no interface, we have an 'id'.
3485 return true;
3486 }
Mike Stump1eb44332009-09-09 15:08:12 +00003487 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003488 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3489 E = lhsQID->qual_end(); I != E; ++I) {
3490 ObjCProtocolDecl *lhsProto = *I;
3491 bool match = false;
3492
3493 // when comparing an id<P> on lhs with a static type on rhs,
3494 // see if static class implements all of id's protocols, directly or
3495 // through its super class and categories.
3496 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3497 E = rhsOPT->qual_end(); J != E; ++J) {
3498 ObjCProtocolDecl *rhsProto = *J;
3499 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3500 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3501 match = true;
3502 break;
3503 }
3504 }
Mike Stump1eb44332009-09-09 15:08:12 +00003505 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00003506 // make sure we check the class hierarchy.
3507 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3508 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3509 E = lhsQID->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 (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003514 match = true;
3515 break;
3516 }
3517 }
3518 }
3519 if (!match)
3520 return false;
3521 }
Mike Stump1eb44332009-09-09 15:08:12 +00003522
Steve Naroff4084c302009-07-23 01:01:38 +00003523 return true;
3524 }
Mike Stump1eb44332009-09-09 15:08:12 +00003525
Steve Naroff4084c302009-07-23 01:01:38 +00003526 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3527 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3528
Mike Stump1eb44332009-09-09 15:08:12 +00003529 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00003530 lhs->getAsObjCInterfacePointerType()) {
3531 if (lhsOPT->qual_empty()) {
3532 bool match = false;
3533 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3534 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3535 E = rhsQID->qual_end(); I != E; ++I) {
3536 // when comparing an id<P> on lhs with a static type on rhs,
3537 // see if static class implements all of id's protocols, directly or
3538 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003539 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003540 match = true;
3541 break;
3542 }
3543 }
3544 if (!match)
3545 return false;
3546 }
3547 return true;
3548 }
Mike Stump1eb44332009-09-09 15:08:12 +00003549 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003550 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3551 E = lhsOPT->qual_end(); I != E; ++I) {
3552 ObjCProtocolDecl *lhsProto = *I;
3553 bool match = false;
3554
3555 // when comparing an id<P> on lhs with a static type on rhs,
3556 // see if static class implements all of id's protocols, directly or
3557 // through its super class and categories.
3558 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3559 E = rhsQID->qual_end(); J != E; ++J) {
3560 ObjCProtocolDecl *rhsProto = *J;
3561 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3562 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3563 match = true;
3564 break;
3565 }
3566 }
3567 if (!match)
3568 return false;
3569 }
3570 return true;
3571 }
3572 return false;
3573}
3574
Eli Friedman3d815e72008-08-22 00:56:42 +00003575/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003576/// compatible for assignment from RHS to LHS. This handles validation of any
3577/// protocol qualifiers on the LHS or RHS.
3578///
Steve Naroff14108da2009-07-10 23:34:53 +00003579bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3580 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003581 // If either type represents the built-in 'id' or 'Class' types, return true.
3582 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00003583 return true;
3584
Steve Naroff4084c302009-07-23 01:01:38 +00003585 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00003586 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3587 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00003588 false);
3589
Steve Naroff14108da2009-07-10 23:34:53 +00003590 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3591 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00003592 if (LHS && RHS) // We have 2 user-defined types.
3593 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00003594
Steve Naroff4084c302009-07-23 01:01:38 +00003595 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003596}
3597
Eli Friedman3d815e72008-08-22 00:56:42 +00003598bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3599 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00003600 // Verify that the base decls are compatible: the RHS must be a subclass of
3601 // the LHS.
3602 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3603 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003604
Chris Lattner6ac46a42008-04-07 06:51:04 +00003605 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3606 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003607 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003608 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003609
Chris Lattner6ac46a42008-04-07 06:51:04 +00003610 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3611 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003612 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003613 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00003614
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003615 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3616 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003617 LHSPI != LHSPE; LHSPI++) {
3618 bool RHSImplementsProtocol = false;
3619
3620 // If the RHS doesn't implement the protocol on the left, the types
3621 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003622 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00003623 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00003624 RHSPI != RHSPE; RHSPI++) {
3625 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003626 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003627 break;
3628 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003629 }
3630 // FIXME: For better diagnostics, consider passing back the protocol name.
3631 if (!RHSImplementsProtocol)
3632 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003633 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003634 // The RHS implements all protocols listed on the LHS.
3635 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003636}
3637
Steve Naroff389bf462009-02-12 17:52:19 +00003638bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3639 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00003640 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3641 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003642
Steve Naroff14108da2009-07-10 23:34:53 +00003643 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00003644 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003645
3646 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3647 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00003648}
3649
Mike Stump1eb44332009-09-09 15:08:12 +00003650/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00003651/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00003652/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00003653/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00003654bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3655 return !mergeTypes(LHS, RHS).isNull();
3656}
3657
3658QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall183700f2009-09-21 23:43:11 +00003659 const FunctionType *lbase = lhs->getAs<FunctionType>();
3660 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00003661 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3662 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00003663 bool allLTypes = true;
3664 bool allRTypes = true;
3665
3666 // Check return type
3667 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3668 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003669 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3670 allLTypes = false;
3671 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3672 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00003673 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00003674 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3675 if (NoReturn != lbase->getNoReturnAttr())
3676 allLTypes = false;
3677 if (NoReturn != rbase->getNoReturnAttr())
3678 allRTypes = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003679
Eli Friedman3d815e72008-08-22 00:56:42 +00003680 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00003681 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3682 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003683 unsigned lproto_nargs = lproto->getNumArgs();
3684 unsigned rproto_nargs = rproto->getNumArgs();
3685
3686 // Compatible functions must have the same number of arguments
3687 if (lproto_nargs != rproto_nargs)
3688 return QualType();
3689
3690 // Variadic and non-variadic functions aren't compatible
3691 if (lproto->isVariadic() != rproto->isVariadic())
3692 return QualType();
3693
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003694 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3695 return QualType();
3696
Eli Friedman3d815e72008-08-22 00:56:42 +00003697 // Check argument compatibility
3698 llvm::SmallVector<QualType, 10> types;
3699 for (unsigned i = 0; i < lproto_nargs; i++) {
3700 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3701 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3702 QualType argtype = mergeTypes(largtype, rargtype);
3703 if (argtype.isNull()) return QualType();
3704 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003705 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3706 allLTypes = false;
3707 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3708 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003709 }
3710 if (allLTypes) return lhs;
3711 if (allRTypes) return rhs;
3712 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00003713 lproto->isVariadic(), lproto->getTypeQuals(),
3714 NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003715 }
3716
3717 if (lproto) allRTypes = false;
3718 if (rproto) allLTypes = false;
3719
Douglas Gregor72564e72009-02-26 23:50:07 +00003720 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003721 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003722 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003723 if (proto->isVariadic()) return QualType();
3724 // Check that the types are compatible with the types that
3725 // would result from default argument promotions (C99 6.7.5.3p15).
3726 // The only types actually affected are promotable integer
3727 // types and floats, which would be passed as a different
3728 // type depending on whether the prototype is visible.
3729 unsigned proto_nargs = proto->getNumArgs();
3730 for (unsigned i = 0; i < proto_nargs; ++i) {
3731 QualType argTy = proto->getArgType(i);
3732 if (argTy->isPromotableIntegerType() ||
3733 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3734 return QualType();
3735 }
3736
3737 if (allLTypes) return lhs;
3738 if (allRTypes) return rhs;
3739 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00003740 proto->getNumArgs(), proto->isVariadic(),
3741 proto->getTypeQuals(), NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003742 }
3743
3744 if (allLTypes) return lhs;
3745 if (allRTypes) return rhs;
Mike Stump24556362009-07-25 21:26:53 +00003746 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003747}
3748
3749QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003750 // C++ [expr]: If an expression initially has the type "reference to T", the
3751 // type is adjusted to "T" prior to any further analysis, the expression
3752 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003753 // expression is an lvalue unless the reference is an rvalue reference and
3754 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003755 // FIXME: C++ shouldn't be going through here! The rules are different
3756 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003757 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3758 // shouldn't be going through here!
Ted Kremenek6217b802009-07-29 21:53:49 +00003759 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003760 LHS = RT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00003761 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003762 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003763
Eli Friedman3d815e72008-08-22 00:56:42 +00003764 QualType LHSCan = getCanonicalType(LHS),
3765 RHSCan = getCanonicalType(RHS);
3766
3767 // If two types are identical, they are compatible.
3768 if (LHSCan == RHSCan)
3769 return LHS;
3770
John McCall0953e762009-09-24 19:53:00 +00003771 // If the qualifiers are different, the types aren't compatible... mostly.
3772 Qualifiers LQuals = LHSCan.getQualifiers();
3773 Qualifiers RQuals = RHSCan.getQualifiers();
3774 if (LQuals != RQuals) {
3775 // If any of these qualifiers are different, we have a type
3776 // mismatch.
3777 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
3778 LQuals.getAddressSpace() != RQuals.getAddressSpace())
3779 return QualType();
3780
3781 // Exactly one GC qualifier difference is allowed: __strong is
3782 // okay if the other type has no GC qualifier but is an Objective
3783 // C object pointer (i.e. implicitly strong by default). We fix
3784 // this by pretending that the unqualified type was actually
3785 // qualified __strong.
3786 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
3787 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
3788 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
3789
3790 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
3791 return QualType();
3792
3793 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
3794 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
3795 }
3796 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
3797 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
3798 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003799 return QualType();
John McCall0953e762009-09-24 19:53:00 +00003800 }
3801
3802 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00003803
Eli Friedman852d63b2009-06-01 01:22:52 +00003804 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3805 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003806
Chris Lattner1adb8832008-01-14 05:45:46 +00003807 // We want to consider the two function types to be the same for these
3808 // comparisons, just force one to the other.
3809 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3810 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003811
3812 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003813 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3814 LHSClass = Type::ConstantArray;
3815 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3816 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00003817
Nate Begeman213541a2008-04-18 23:10:10 +00003818 // Canonicalize ExtVector -> Vector.
3819 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3820 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00003821
Chris Lattnera36a61f2008-04-07 05:43:21 +00003822 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003823 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00003824 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00003825 // a signed integer type, or an unsigned integer type.
John McCall183700f2009-09-21 23:43:11 +00003826 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003827 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3828 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003829 }
John McCall183700f2009-09-21 23:43:11 +00003830 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003831 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3832 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003833 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003834
Eli Friedman3d815e72008-08-22 00:56:42 +00003835 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003836 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003837
Steve Naroff4a746782008-01-09 22:43:08 +00003838 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003839 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003840#define TYPE(Class, Base)
3841#define ABSTRACT_TYPE(Class, Base)
3842#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3843#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3844#include "clang/AST/TypeNodes.def"
3845 assert(false && "Non-canonical and dependent types shouldn't get here");
3846 return QualType();
3847
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003848 case Type::LValueReference:
3849 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003850 case Type::MemberPointer:
3851 assert(false && "C++ should never be in mergeTypes");
3852 return QualType();
3853
3854 case Type::IncompleteArray:
3855 case Type::VariableArray:
3856 case Type::FunctionProto:
3857 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00003858 assert(false && "Types are eliminated above");
3859 return QualType();
3860
Chris Lattner1adb8832008-01-14 05:45:46 +00003861 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003862 {
3863 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003864 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
3865 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003866 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3867 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00003868 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003869 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00003870 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003871 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003872 return getPointerType(ResultType);
3873 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003874 case Type::BlockPointer:
3875 {
3876 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003877 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
3878 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00003879 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3880 if (ResultType.isNull()) return QualType();
3881 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3882 return LHS;
3883 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3884 return RHS;
3885 return getBlockPointerType(ResultType);
3886 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003887 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003888 {
3889 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3890 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3891 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3892 return QualType();
3893
3894 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3895 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3896 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3897 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003898 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3899 return LHS;
3900 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3901 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003902 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3903 ArrayType::ArraySizeModifier(), 0);
3904 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3905 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003906 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3907 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003908 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3909 return LHS;
3910 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3911 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003912 if (LVAT) {
3913 // FIXME: This isn't correct! But tricky to implement because
3914 // the array's size has to be the size of LHS, but the type
3915 // has to be different.
3916 return LHS;
3917 }
3918 if (RVAT) {
3919 // FIXME: This isn't correct! But tricky to implement because
3920 // the array's size has to be the size of RHS, but the type
3921 // has to be different.
3922 return RHS;
3923 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003924 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3925 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003926 return getIncompleteArrayType(ResultType,
3927 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003928 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003929 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003930 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003931 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003932 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003933 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003934 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003935 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003936 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003937 case Type::Complex:
3938 // Distinct complex types are incompatible.
3939 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003940 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003941 // FIXME: The merged type should be an ExtVector!
John McCall183700f2009-09-21 23:43:11 +00003942 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00003943 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003944 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003945 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003946 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003947 // FIXME: This should be type compatibility, e.g. whether
3948 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00003949 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
3950 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00003951 if (LHSIface && RHSIface &&
3952 canAssignObjCInterfaces(LHSIface, RHSIface))
3953 return LHS;
3954
Eli Friedman3d815e72008-08-22 00:56:42 +00003955 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003956 }
Steve Naroff14108da2009-07-10 23:34:53 +00003957 case Type::ObjCObjectPointer: {
John McCall183700f2009-09-21 23:43:11 +00003958 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
3959 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00003960 return LHS;
3961
Steve Naroffbc76dd02008-12-10 22:14:21 +00003962 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00003963 }
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003964 case Type::FixedWidthInt:
3965 // Distinct fixed-width integers are not compatible.
3966 return QualType();
Douglas Gregor7532dc62009-03-30 22:58:21 +00003967 case Type::TemplateSpecialization:
3968 assert(false && "Dependent types have no size");
3969 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003970 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003971
3972 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003973}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003974
Chris Lattner5426bf62008-04-07 07:01:58 +00003975//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003976// Integer Predicates
3977//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003978
Eli Friedmanad74a752008-06-28 06:23:08 +00003979unsigned ASTContext::getIntWidth(QualType T) {
3980 if (T == BoolTy)
3981 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003982 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3983 return FWIT->getWidth();
3984 }
3985 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003986 return (unsigned)getTypeSize(T);
3987}
3988
3989QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3990 assert(T->isSignedIntegerType() && "Unexpected type");
John McCall183700f2009-09-21 23:43:11 +00003991 if (const EnumType* ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00003992 T = ETy->getDecl()->getIntegerType();
John McCall183700f2009-09-21 23:43:11 +00003993 const BuiltinType* BTy = T->getAs<BuiltinType>();
Eli Friedmanad74a752008-06-28 06:23:08 +00003994 assert (BTy && "Unexpected signed integer type");
3995 switch (BTy->getKind()) {
3996 case BuiltinType::Char_S:
3997 case BuiltinType::SChar:
3998 return UnsignedCharTy;
3999 case BuiltinType::Short:
4000 return UnsignedShortTy;
4001 case BuiltinType::Int:
4002 return UnsignedIntTy;
4003 case BuiltinType::Long:
4004 return UnsignedLongTy;
4005 case BuiltinType::LongLong:
4006 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00004007 case BuiltinType::Int128:
4008 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00004009 default:
4010 assert(0 && "Unexpected signed integer type");
4011 return QualType();
4012 }
4013}
4014
Douglas Gregor2cf26342009-04-09 22:27:44 +00004015ExternalASTSource::~ExternalASTSource() { }
4016
4017void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00004018
4019
4020//===----------------------------------------------------------------------===//
4021// Builtin Type Computation
4022//===----------------------------------------------------------------------===//
4023
4024/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4025/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004026static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004027 ASTContext::GetBuiltinTypeError &Error,
4028 bool AllowTypeModifiers = true) {
4029 // Modifiers.
4030 int HowLong = 0;
4031 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004032
Chris Lattner86df27b2009-06-14 00:45:47 +00004033 // Read the modifiers first.
4034 bool Done = false;
4035 while (!Done) {
4036 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004037 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004038 case 'S':
4039 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4040 assert(!Signed && "Can't use 'S' modifier multiple times!");
4041 Signed = true;
4042 break;
4043 case 'U':
4044 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4045 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4046 Unsigned = true;
4047 break;
4048 case 'L':
4049 assert(HowLong <= 2 && "Can't have LLLL modifier");
4050 ++HowLong;
4051 break;
4052 }
4053 }
4054
4055 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004056
Chris Lattner86df27b2009-06-14 00:45:47 +00004057 // Read the base type.
4058 switch (*Str++) {
4059 default: assert(0 && "Unknown builtin type letter!");
4060 case 'v':
4061 assert(HowLong == 0 && !Signed && !Unsigned &&
4062 "Bad modifiers used with 'v'!");
4063 Type = Context.VoidTy;
4064 break;
4065 case 'f':
4066 assert(HowLong == 0 && !Signed && !Unsigned &&
4067 "Bad modifiers used with 'f'!");
4068 Type = Context.FloatTy;
4069 break;
4070 case 'd':
4071 assert(HowLong < 2 && !Signed && !Unsigned &&
4072 "Bad modifiers used with 'd'!");
4073 if (HowLong)
4074 Type = Context.LongDoubleTy;
4075 else
4076 Type = Context.DoubleTy;
4077 break;
4078 case 's':
4079 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4080 if (Unsigned)
4081 Type = Context.UnsignedShortTy;
4082 else
4083 Type = Context.ShortTy;
4084 break;
4085 case 'i':
4086 if (HowLong == 3)
4087 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4088 else if (HowLong == 2)
4089 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4090 else if (HowLong == 1)
4091 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4092 else
4093 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4094 break;
4095 case 'c':
4096 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4097 if (Signed)
4098 Type = Context.SignedCharTy;
4099 else if (Unsigned)
4100 Type = Context.UnsignedCharTy;
4101 else
4102 Type = Context.CharTy;
4103 break;
4104 case 'b': // boolean
4105 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4106 Type = Context.BoolTy;
4107 break;
4108 case 'z': // size_t.
4109 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4110 Type = Context.getSizeType();
4111 break;
4112 case 'F':
4113 Type = Context.getCFConstantStringType();
4114 break;
4115 case 'a':
4116 Type = Context.getBuiltinVaListType();
4117 assert(!Type.isNull() && "builtin va list type not initialized!");
4118 break;
4119 case 'A':
4120 // This is a "reference" to a va_list; however, what exactly
4121 // this means depends on how va_list is defined. There are two
4122 // different kinds of va_list: ones passed by value, and ones
4123 // passed by reference. An example of a by-value va_list is
4124 // x86, where va_list is a char*. An example of by-ref va_list
4125 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4126 // we want this argument to be a char*&; for x86-64, we want
4127 // it to be a __va_list_tag*.
4128 Type = Context.getBuiltinVaListType();
4129 assert(!Type.isNull() && "builtin va list type not initialized!");
4130 if (Type->isArrayType()) {
4131 Type = Context.getArrayDecayedType(Type);
4132 } else {
4133 Type = Context.getLValueReferenceType(Type);
4134 }
4135 break;
4136 case 'V': {
4137 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004138 unsigned NumElements = strtoul(Str, &End, 10);
4139 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004140
Chris Lattner86df27b2009-06-14 00:45:47 +00004141 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004142
Chris Lattner86df27b2009-06-14 00:45:47 +00004143 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4144 Type = Context.getVectorType(ElementType, NumElements);
4145 break;
4146 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00004147 case 'X': {
4148 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4149 Type = Context.getComplexType(ElementType);
4150 break;
4151 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004152 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004153 Type = Context.getFILEType();
4154 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004155 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004156 return QualType();
4157 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004158 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004159 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004160 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004161 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004162 else
4163 Type = Context.getjmp_bufType();
4164
Mike Stumpfd612db2009-07-28 23:47:15 +00004165 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004166 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004167 return QualType();
4168 }
4169 break;
Mike Stump782fa302009-07-28 02:25:19 +00004170 }
Mike Stump1eb44332009-09-09 15:08:12 +00004171
Chris Lattner86df27b2009-06-14 00:45:47 +00004172 if (!AllowTypeModifiers)
4173 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004174
Chris Lattner86df27b2009-06-14 00:45:47 +00004175 Done = false;
4176 while (!Done) {
4177 switch (*Str++) {
4178 default: Done = true; --Str; break;
4179 case '*':
4180 Type = Context.getPointerType(Type);
4181 break;
4182 case '&':
4183 Type = Context.getLValueReferenceType(Type);
4184 break;
4185 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4186 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004187 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004188 break;
4189 }
4190 }
Mike Stump1eb44332009-09-09 15:08:12 +00004191
Chris Lattner86df27b2009-06-14 00:45:47 +00004192 return Type;
4193}
4194
4195/// GetBuiltinType - Return the type for the specified builtin.
4196QualType ASTContext::GetBuiltinType(unsigned id,
4197 GetBuiltinTypeError &Error) {
4198 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004199
Chris Lattner86df27b2009-06-14 00:45:47 +00004200 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004201
Chris Lattner86df27b2009-06-14 00:45:47 +00004202 Error = GE_None;
4203 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4204 if (Error != GE_None)
4205 return QualType();
4206 while (TypeStr[0] && TypeStr[0] != '.') {
4207 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4208 if (Error != GE_None)
4209 return QualType();
4210
4211 // Do array -> pointer decay. The builtin should use the decayed type.
4212 if (Ty->isArrayType())
4213 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004214
Chris Lattner86df27b2009-06-14 00:45:47 +00004215 ArgTypes.push_back(Ty);
4216 }
4217
4218 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4219 "'.' should only occur at end of builtin type list!");
4220
4221 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4222 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4223 return getFunctionNoProtoType(ResType);
4224 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4225 TypeStr[0] == '.', 0);
4226}
Eli Friedmana95d7572009-08-19 07:44:53 +00004227
4228QualType
4229ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4230 // Perform the usual unary conversions. We do this early so that
4231 // integral promotions to "int" can allow us to exit early, in the
4232 // lhs == rhs check. Also, for conversion purposes, we ignore any
4233 // qualifiers. For example, "const float" and "float" are
4234 // equivalent.
4235 if (lhs->isPromotableIntegerType())
4236 lhs = getPromotedIntegerType(lhs);
4237 else
4238 lhs = lhs.getUnqualifiedType();
4239 if (rhs->isPromotableIntegerType())
4240 rhs = getPromotedIntegerType(rhs);
4241 else
4242 rhs = rhs.getUnqualifiedType();
4243
4244 // If both types are identical, no conversion is needed.
4245 if (lhs == rhs)
4246 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004247
Eli Friedmana95d7572009-08-19 07:44:53 +00004248 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4249 // The caller can deal with this (e.g. pointer + int).
4250 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4251 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004252
4253 // At this point, we have two different arithmetic types.
4254
Eli Friedmana95d7572009-08-19 07:44:53 +00004255 // Handle complex types first (C99 6.3.1.8p1).
4256 if (lhs->isComplexType() || rhs->isComplexType()) {
4257 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004258 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004259 // convert the rhs to the lhs complex type.
4260 return lhs;
4261 }
Mike Stump1eb44332009-09-09 15:08:12 +00004262 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004263 // convert the lhs to the rhs complex type.
4264 return rhs;
4265 }
4266 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004267 // When both operands are complex, the shorter operand is converted to the
4268 // type of the longer, and that is the type of the result. This corresponds
4269 // to what is done when combining two real floating-point operands.
4270 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004271 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004272 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004273 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004274 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004275 // "double _Complex" is promoted to "long double _Complex".
4276 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004277
4278 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004279 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004280 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004281 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004282 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004283 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4284 // domains match. This is a requirement for our implementation, C99
4285 // does not require this promotion.
4286 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4287 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4288 return rhs;
4289 } else { // handle "_Complex double, double".
4290 return lhs;
4291 }
4292 }
4293 return lhs; // The domain/size match exactly.
4294 }
4295 // Now handle "real" floating types (i.e. float, double, long double).
4296 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4297 // if we have an integer operand, the result is the real floating type.
4298 if (rhs->isIntegerType()) {
4299 // convert rhs to the lhs floating point type.
4300 return lhs;
4301 }
4302 if (rhs->isComplexIntegerType()) {
4303 // convert rhs to the complex floating point type.
4304 return getComplexType(lhs);
4305 }
4306 if (lhs->isIntegerType()) {
4307 // convert lhs to the rhs floating point type.
4308 return rhs;
4309 }
Mike Stump1eb44332009-09-09 15:08:12 +00004310 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004311 // convert lhs to the complex floating point type.
4312 return getComplexType(rhs);
4313 }
4314 // We have two real floating types, float/complex combos were handled above.
4315 // Convert the smaller operand to the bigger result.
4316 int result = getFloatingTypeOrder(lhs, rhs);
4317 if (result > 0) // convert the rhs
4318 return lhs;
4319 assert(result < 0 && "illegal float comparison");
4320 return rhs; // convert the lhs
4321 }
4322 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4323 // Handle GCC complex int extension.
4324 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4325 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4326
4327 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004328 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004329 rhsComplexInt->getElementType()) >= 0)
4330 return lhs; // convert the rhs
4331 return rhs;
4332 } else if (lhsComplexInt && rhs->isIntegerType()) {
4333 // convert the rhs to the lhs complex type.
4334 return lhs;
4335 } else if (rhsComplexInt && lhs->isIntegerType()) {
4336 // convert the lhs to the rhs complex type.
4337 return rhs;
4338 }
4339 }
4340 // Finally, we have two differing integer types.
4341 // The rules for this case are in C99 6.3.1.8
4342 int compare = getIntegerTypeOrder(lhs, rhs);
4343 bool lhsSigned = lhs->isSignedIntegerType(),
4344 rhsSigned = rhs->isSignedIntegerType();
4345 QualType destType;
4346 if (lhsSigned == rhsSigned) {
4347 // Same signedness; use the higher-ranked type
4348 destType = compare >= 0 ? lhs : rhs;
4349 } else if (compare != (lhsSigned ? 1 : -1)) {
4350 // The unsigned type has greater than or equal rank to the
4351 // signed type, so use the unsigned type
4352 destType = lhsSigned ? rhs : lhs;
4353 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4354 // The two types are different widths; if we are here, that
4355 // means the signed type is larger than the unsigned type, so
4356 // use the signed type.
4357 destType = lhsSigned ? lhs : rhs;
4358 } else {
4359 // The signed type is higher-ranked than the unsigned type,
4360 // but isn't actually any bigger (like unsigned int and long
4361 // on most 32-bit systems). Use the unsigned type corresponding
4362 // to the signed type.
4363 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4364 }
4365 return destType;
4366}