blob: 1b77bbe1dbd8cc1f9c71d91e56781b25e8dfb2b5 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000018#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000023#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000025#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000027#include "llvm/Support/MemoryBuffer.h"
Anders Carlsson29445a02009-07-18 21:19:52 +000028#include "RecordLayoutBuilder.h"
29
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
32enum FloatingRank {
33 FloatRank, DoubleRank, LongDoubleRank
34};
35
Chris Lattner61710852008-10-05 17:34:18 +000036ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
37 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000038 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +000039 Builtin::Context &builtins,
Mike Stump1eb44332009-09-09 15:08:12 +000040 bool FreeMem, unsigned size_reserve) :
41 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +000042 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump1eb44332009-09-09 15:08:12 +000043 sigjmp_bufDecl(0), SourceMgr(SM), LangOpts(LOpts),
44 LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +000045 Idents(idents), Selectors(sels),
Mike Stump1eb44332009-09-09 15:08:12 +000046 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
David Chisnall0f436562009-08-17 16:35:33 +000047 ObjCIdRedefinitionType = QualType();
48 ObjCClassRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +000049 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +000050 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +000051 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000052}
53
Reid Spencer5f016e22007-07-11 17:01:13 +000054ASTContext::~ASTContext() {
55 // Deallocate all the types.
56 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000057 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 Types.pop_back();
59 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000060
Nuno Lopesb74668e2008-12-17 22:30:25 +000061 {
John McCall0953e762009-09-24 19:53:00 +000062 llvm::FoldingSet<ExtQuals>::iterator
63 I = ExtQualNodes.begin(), E = ExtQualNodes.end();
64 while (I != E)
65 Deallocate(&*I++);
66 }
67
68 {
Nuno Lopesb74668e2008-12-17 22:30:25 +000069 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
70 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
71 while (I != E) {
72 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
73 delete R;
74 }
75 }
76
77 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000078 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
79 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000080 while (I != E) {
81 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
82 delete R;
83 }
84 }
85
Douglas Gregorab452ba2009-03-26 23:50:42 +000086 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000087 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
88 NNS = NestedNameSpecifiers.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000089 NNSEnd = NestedNameSpecifiers.end();
90 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000091 /* Increment in loop */)
92 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000093
94 if (GlobalNestedNameSpecifier)
95 GlobalNestedNameSpecifier->Destroy(*this);
96
Eli Friedmanb26153c2008-05-27 03:08:09 +000097 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000098}
99
Mike Stump1eb44332009-09-09 15:08:12 +0000100void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
102 ExternalSource.reset(Source.take());
103}
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105void ASTContext::PrintStats() const {
106 fprintf(stderr, "*** AST Context Stats:\n");
107 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000108
Douglas Gregordbe833d2009-05-26 14:40:08 +0000109 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000110#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000111#define ABSTRACT_TYPE(Name, Parent)
112#include "clang/AST/TypeNodes.def"
113 0 // Extra
114 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000115
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
117 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000118 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120
Douglas Gregordbe833d2009-05-26 14:40:08 +0000121 unsigned Idx = 0;
122 unsigned TotalBytes = 0;
123#define TYPE(Name, Parent) \
124 if (counts[Idx]) \
125 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
126 TotalBytes += counts[Idx] * sizeof(Name##Type); \
127 ++Idx;
128#define ABSTRACT_TYPE(Name, Parent)
129#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregordbe833d2009-05-26 14:40:08 +0000131 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132
133 if (ExternalSource.get()) {
134 fprintf(stderr, "\n");
135 ExternalSource->PrintStats();
136 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000137}
138
139
140void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000141 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
142 R = QualType(Ty, 0);
143 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
Reid Spencer5f016e22007-07-11 17:01:13 +0000146void ASTContext::InitBuiltinTypes() {
147 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 // C99 6.2.5p19.
150 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 // C99 6.2.5p2.
153 InitBuiltinType(BoolTy, BuiltinType::Bool);
154 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000155 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 InitBuiltinType(CharTy, BuiltinType::Char_S);
157 else
158 InitBuiltinType(CharTy, BuiltinType::Char_U);
159 // C99 6.2.5p4.
160 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
161 InitBuiltinType(ShortTy, BuiltinType::Short);
162 InitBuiltinType(IntTy, BuiltinType::Int);
163 InitBuiltinType(LongTy, BuiltinType::Long);
164 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 // C99 6.2.5p6.
167 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
168 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
169 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
170 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
171 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 // C99 6.2.5p10.
174 InitBuiltinType(FloatTy, BuiltinType::Float);
175 InitBuiltinType(DoubleTy, BuiltinType::Double);
176 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000177
Chris Lattner2df9ced2009-04-30 02:43:43 +0000178 // GNU extension, 128-bit integers.
179 InitBuiltinType(Int128Ty, BuiltinType::Int128);
180 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
181
Chris Lattner3a250322009-02-26 23:43:47 +0000182 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
183 InitBuiltinType(WCharTy, BuiltinType::WChar);
184 else // C99
185 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000186
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000187 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
188 InitBuiltinType(Char16Ty, BuiltinType::Char16);
189 else // C99
190 Char16Ty = getFromTargetType(Target.getChar16Type());
191
192 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
193 InitBuiltinType(Char32Ty, BuiltinType::Char32);
194 else // C99
195 Char32Ty = getFromTargetType(Target.getChar32Type());
196
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000197 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000198 InitBuiltinType(OverloadTy, BuiltinType::Overload);
199
200 // Placeholder type for type-dependent expressions whose type is
201 // completely unknown. No code should ever check a type against
202 // DependentTy and users should never see it; however, it is here to
203 // help diagnose failures to properly check for type-dependent
204 // expressions.
205 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000206
Mike Stump1eb44332009-09-09 15:08:12 +0000207 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000208 // not yet been deduced.
209 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 // C99 6.2.5p11.
212 FloatComplexTy = getComplexType(FloatTy);
213 DoubleComplexTy = getComplexType(DoubleTy);
214 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000215
Steve Naroff7e219e42007-10-15 14:41:52 +0000216 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Steve Naroffde2e22d2009-07-15 18:40:39 +0000218 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
219 ObjCIdTypedefType = QualType();
220 ObjCClassTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Steve Naroffde2e22d2009-07-15 18:40:39 +0000222 // Builtin types for 'id' and 'Class'.
223 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
224 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Steve Naroff14108da2009-07-10 23:34:53 +0000225
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000226 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000228 // void * type
229 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000230
231 // nullptr type (C++0x 2.14.7)
232 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000233}
234
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000235MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000236ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000237 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000238 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000239 = InstantiatedFromStaticDataMember.find(Var);
240 if (Pos == InstantiatedFromStaticDataMember.end())
241 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Douglas Gregor7caa6822009-07-24 20:34:43 +0000243 return Pos->second;
244}
245
Mike Stump1eb44332009-09-09 15:08:12 +0000246void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000247ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
248 TemplateSpecializationKind TSK) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000249 assert(Inst->isStaticDataMember() && "Not a static data member");
250 assert(Tmpl->isStaticDataMember() && "Not a static data member");
251 assert(!InstantiatedFromStaticDataMember[Inst] &&
252 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000253 InstantiatedFromStaticDataMember[Inst]
254 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000255}
256
Anders Carlsson0d8df782009-08-29 19:37:28 +0000257UnresolvedUsingDecl *
258ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000259 llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
Anders Carlsson0d8df782009-08-29 19:37:28 +0000260 = InstantiatedFromUnresolvedUsingDecl.find(UUD);
261 if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
262 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Anders Carlsson0d8df782009-08-29 19:37:28 +0000264 return Pos->second;
265}
266
267void
268ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
269 UnresolvedUsingDecl *UUD) {
270 assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
271 "Already noted what using decl what instantiated from");
272 InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
273}
274
Anders Carlssond8b285f2009-09-01 04:26:58 +0000275FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
276 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
277 = InstantiatedFromUnnamedFieldDecl.find(Field);
278 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
279 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Anders Carlssond8b285f2009-09-01 04:26:58 +0000281 return Pos->second;
282}
283
284void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
285 FieldDecl *Tmpl) {
286 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
287 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
288 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
289 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Anders Carlssond8b285f2009-09-01 04:26:58 +0000291 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
292}
293
Douglas Gregor2e222532009-07-02 17:08:52 +0000294namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000295 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000296 : std::binary_function<SourceRange, SourceRange, bool> {
297 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Douglas Gregor2e222532009-07-02 17:08:52 +0000299 public:
300 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Douglas Gregor2e222532009-07-02 17:08:52 +0000302 bool operator()(SourceRange X, SourceRange Y) {
303 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
304 }
305 };
306}
307
308/// \brief Determine whether the given comment is a Doxygen-style comment.
309///
310/// \param Start the start of the comment text.
311///
312/// \param End the end of the comment text.
313///
314/// \param Member whether we want to check whether this is a member comment
315/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
316/// we only return true when we find a non-member comment.
Mike Stump1eb44332009-09-09 15:08:12 +0000317static bool
318isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
Douglas Gregor2e222532009-07-02 17:08:52 +0000319 bool Member = false) {
Mike Stump1eb44332009-09-09 15:08:12 +0000320 const char *BufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000321 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
322 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
323 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregor2e222532009-07-02 17:08:52 +0000325 if (End - Start < 4)
326 return false;
327
328 assert(Start[0] == '/' && "Not a comment?");
329 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
330 return false;
331 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
332 return false;
333
334 return (Start[3] == '<') == Member;
335}
336
337/// \brief Retrieve the comment associated with the given declaration, if
Mike Stump1eb44332009-09-09 15:08:12 +0000338/// it has one.
Douglas Gregor2e222532009-07-02 17:08:52 +0000339const char *ASTContext::getCommentForDecl(const Decl *D) {
340 if (!D)
341 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Douglas Gregor2e222532009-07-02 17:08:52 +0000343 // Check whether we have cached a comment string for this declaration
344 // already.
Mike Stump1eb44332009-09-09 15:08:12 +0000345 llvm::DenseMap<const Decl *, std::string>::iterator Pos
Douglas Gregor2e222532009-07-02 17:08:52 +0000346 = DeclComments.find(D);
347 if (Pos != DeclComments.end())
348 return Pos->second.c_str();
349
Mike Stump1eb44332009-09-09 15:08:12 +0000350 // If we have an external AST source and have not yet loaded comments from
Douglas Gregor2e222532009-07-02 17:08:52 +0000351 // that source, do so now.
352 if (ExternalSource && !LoadedExternalComments) {
353 std::vector<SourceRange> LoadedComments;
354 ExternalSource->ReadComments(LoadedComments);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Douglas Gregor2e222532009-07-02 17:08:52 +0000356 if (!LoadedComments.empty())
357 Comments.insert(Comments.begin(), LoadedComments.begin(),
358 LoadedComments.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Douglas Gregor2e222532009-07-02 17:08:52 +0000360 LoadedExternalComments = true;
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
363 // If there are no comments anywhere, we won't find anything.
Douglas Gregor2e222532009-07-02 17:08:52 +0000364 if (Comments.empty())
365 return 0;
366
367 // If the declaration doesn't map directly to a location in a file, we
368 // can't find the comment.
369 SourceLocation DeclStartLoc = D->getLocStart();
370 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
371 return 0;
372
373 // Find the comment that occurs just before this declaration.
374 std::vector<SourceRange>::iterator LastComment
Mike Stump1eb44332009-09-09 15:08:12 +0000375 = std::lower_bound(Comments.begin(), Comments.end(),
Douglas Gregor2e222532009-07-02 17:08:52 +0000376 SourceRange(DeclStartLoc),
377 BeforeInTranslationUnit(&SourceMgr));
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Douglas Gregor2e222532009-07-02 17:08:52 +0000379 // Decompose the location for the start of the declaration and find the
380 // beginning of the file buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000381 std::pair<FileID, unsigned> DeclStartDecomp
Douglas Gregor2e222532009-07-02 17:08:52 +0000382 = SourceMgr.getDecomposedLoc(DeclStartLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000383 const char *FileBufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000384 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Douglas Gregor2e222532009-07-02 17:08:52 +0000386 // First check whether we have a comment for a member.
387 if (LastComment != Comments.end() &&
388 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
389 isDoxygenComment(SourceMgr, *LastComment, true)) {
390 std::pair<FileID, unsigned> LastCommentEndDecomp
391 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
392 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
393 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
Mike Stump1eb44332009-09-09 15:08:12 +0000394 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000395 LastCommentEndDecomp.second)) {
396 // The Doxygen member comment comes after the declaration starts and
397 // is on the same line and in the same file as the declaration. This
398 // is the comment we want.
399 std::string &Result = DeclComments[D];
Mike Stump1eb44332009-09-09 15:08:12 +0000400 Result.append(FileBufferStart +
401 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000402 FileBufferStart + LastCommentEndDecomp.second + 1);
403 return Result.c_str();
404 }
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregor2e222532009-07-02 17:08:52 +0000407 if (LastComment == Comments.begin())
408 return 0;
409 --LastComment;
410
411 // Decompose the end of the comment.
412 std::pair<FileID, unsigned> LastCommentEndDecomp
413 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Douglas Gregor2e222532009-07-02 17:08:52 +0000415 // If the comment and the declaration aren't in the same file, then they
416 // aren't related.
417 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
418 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Douglas Gregor2e222532009-07-02 17:08:52 +0000420 // Check that we actually have a Doxygen comment.
421 if (!isDoxygenComment(SourceMgr, *LastComment))
422 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Douglas Gregor2e222532009-07-02 17:08:52 +0000424 // Compute the starting line for the declaration and for the end of the
425 // comment (this is expensive).
Mike Stump1eb44332009-09-09 15:08:12 +0000426 unsigned DeclStartLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000427 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
428 unsigned CommentEndLine
Mike Stump1eb44332009-09-09 15:08:12 +0000429 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000430 LastCommentEndDecomp.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Douglas Gregor2e222532009-07-02 17:08:52 +0000432 // If the comment does not end on the line prior to the declaration, then
433 // the comment is not associated with the declaration at all.
434 if (CommentEndLine + 1 != DeclStartLine)
435 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregor2e222532009-07-02 17:08:52 +0000437 // We have a comment, but there may be more comments on the previous lines.
438 // Keep looking so long as the comments are still Doxygen comments and are
439 // still adjacent.
Mike Stump1eb44332009-09-09 15:08:12 +0000440 unsigned ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000441 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
442 std::vector<SourceRange>::iterator FirstComment = LastComment;
443 while (FirstComment != Comments.begin()) {
444 // Look at the previous comment
445 --FirstComment;
446 std::pair<FileID, unsigned> Decomp
447 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Douglas Gregor2e222532009-07-02 17:08:52 +0000449 // If this previous comment is in a different file, we're done.
450 if (Decomp.first != DeclStartDecomp.first) {
451 ++FirstComment;
452 break;
453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Douglas Gregor2e222532009-07-02 17:08:52 +0000455 // If this comment is not a Doxygen comment, we're done.
456 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
457 ++FirstComment;
458 break;
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Douglas Gregor2e222532009-07-02 17:08:52 +0000461 // If the line number is not what we expected, we're done.
462 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
463 if (Line != ExpectedLine) {
464 ++FirstComment;
465 break;
466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Douglas Gregor2e222532009-07-02 17:08:52 +0000468 // Set the next expected line number.
Mike Stump1eb44332009-09-09 15:08:12 +0000469 ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000470 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
471 }
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Douglas Gregor2e222532009-07-02 17:08:52 +0000473 // The iterator range [FirstComment, LastComment] contains all of the
474 // BCPL comments that, together, are associated with this declaration.
475 // Form a single comment block string for this declaration that concatenates
476 // all of these comments.
477 std::string &Result = DeclComments[D];
478 while (FirstComment != LastComment) {
479 std::pair<FileID, unsigned> DecompStart
480 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
481 std::pair<FileID, unsigned> DecompEnd
482 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
483 Result.append(FileBufferStart + DecompStart.second,
484 FileBufferStart + DecompEnd.second + 1);
485 ++FirstComment;
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregor2e222532009-07-02 17:08:52 +0000488 // Append the last comment line.
Mike Stump1eb44332009-09-09 15:08:12 +0000489 Result.append(FileBufferStart +
490 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000491 FileBufferStart + LastCommentEndDecomp.second + 1);
492 return Result.c_str();
493}
494
Chris Lattner464175b2007-07-18 17:52:12 +0000495//===----------------------------------------------------------------------===//
496// Type Sizing and Analysis
497//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000498
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000499/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
500/// scalar floating point type.
501const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000502 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000503 assert(BT && "Not a floating point type!");
504 switch (BT->getKind()) {
505 default: assert(0 && "Not a floating point type!");
506 case BuiltinType::Float: return Target.getFloatFormat();
507 case BuiltinType::Double: return Target.getDoubleFormat();
508 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
509 }
510}
511
Mike Stump196efbf2009-09-22 02:43:44 +0000512/// getDeclAlignInBytes - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000513/// specified decl. Note that bitfields do not have a valid alignment, so
514/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000515unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000516 unsigned Align = Target.getCharWidth();
517
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000518 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedmandcdafb62009-02-22 02:56:25 +0000519 Align = std::max(Align, AA->getAlignment());
520
Chris Lattneraf707ab2009-01-24 21:53:27 +0000521 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
522 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000523 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000524 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000525 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000526 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
527 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000528 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
529 T = cast<ArrayType>(T)->getElementType();
530
531 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
532 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000533 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000534
535 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000536}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000537
Chris Lattnera7674d82007-07-13 22:13:22 +0000538/// getTypeSize - Return the size of the specified type, in bits. This method
539/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000540///
541/// FIXME: Pointers into different addr spaces could have different sizes and
542/// alignment requirements: getPointerInfo should take an AddrSpace, this
543/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000544std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000545ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000546 uint64_t Width=0;
547 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000548 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000549#define TYPE(Class, Base)
550#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000551#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000552#define DEPENDENT_TYPE(Class, Base) case Type::Class:
553#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000554 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000555 break;
556
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +0000557 case Type::ObjCProtocolList:
558 assert(false && "Should not see protocol list types");
559 break;
560
Chris Lattner692233e2007-07-13 22:27:08 +0000561 case Type::FunctionNoProto:
562 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000563 // GCC extension: alignof(function) = 32 bits
564 Width = 0;
565 Align = 32;
566 break;
567
Douglas Gregor72564e72009-02-26 23:50:07 +0000568 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000569 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000570 Width = 0;
571 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
572 break;
573
Steve Narofffb22d962007-08-30 01:06:46 +0000574 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000575 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattner98be4942008-03-05 18:54:05 +0000577 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000578 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000579 Align = EltInfo.second;
580 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000581 }
Nate Begeman213541a2008-04-18 23:10:10 +0000582 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000583 case Type::Vector: {
Mike Stump1eb44332009-09-09 15:08:12 +0000584 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000585 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000586 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000587 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000588 // If the alignment is not a power of 2, round up to the next power of 2.
589 // This happens for non-power-of-2 length vectors.
590 // FIXME: this should probably be a target property.
591 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000592 break;
593 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000594
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000595 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000596 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000597 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000598 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000599 // GCC extension: alignof(void) = 8 bits.
600 Width = 0;
601 Align = 8;
602 break;
603
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000604 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000605 Width = Target.getBoolWidth();
606 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000607 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000608 case BuiltinType::Char_S:
609 case BuiltinType::Char_U:
610 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000611 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000612 Width = Target.getCharWidth();
613 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000614 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000615 case BuiltinType::WChar:
616 Width = Target.getWCharWidth();
617 Align = Target.getWCharAlign();
618 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000619 case BuiltinType::Char16:
620 Width = Target.getChar16Width();
621 Align = Target.getChar16Align();
622 break;
623 case BuiltinType::Char32:
624 Width = Target.getChar32Width();
625 Align = Target.getChar32Align();
626 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000627 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000628 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000629 Width = Target.getShortWidth();
630 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000631 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000632 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000633 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000634 Width = Target.getIntWidth();
635 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000636 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000637 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000638 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000639 Width = Target.getLongWidth();
640 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000641 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000642 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000643 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000644 Width = Target.getLongLongWidth();
645 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000646 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000647 case BuiltinType::Int128:
648 case BuiltinType::UInt128:
649 Width = 128;
650 Align = 128; // int128_t is 128-bit aligned on all targets.
651 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000652 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000653 Width = Target.getFloatWidth();
654 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000655 break;
656 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000657 Width = Target.getDoubleWidth();
658 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000659 break;
660 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000661 Width = Target.getLongDoubleWidth();
662 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000663 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000664 case BuiltinType::NullPtr:
665 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
666 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000667 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000668 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000669 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000670 case Type::FixedWidthInt:
671 // FIXME: This isn't precisely correct; the width/alignment should depend
672 // on the available types for the target
673 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000674 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000675 Align = Width;
676 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000677 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000678 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000679 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000680 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000681 case Type::BlockPointer: {
682 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
683 Width = Target.getPointerWidth(AS);
684 Align = Target.getPointerAlign(AS);
685 break;
686 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000687 case Type::Pointer: {
688 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000689 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000690 Align = Target.getPointerAlign(AS);
691 break;
692 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000693 case Type::LValueReference:
694 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000695 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000696 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000697 // FIXME: This is wrong for struct layout: a reference in a struct has
698 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000699 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000700 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000701 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
702 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
703 // If we ever want to support other ABIs this needs to be abstracted.
704
Sebastian Redlf30208a2009-01-24 21:16:55 +0000705 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000706 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000707 getTypeInfo(getPointerDiffType());
708 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000709 if (Pointee->isFunctionType())
710 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000711 Align = PtrDiffInfo.second;
712 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000713 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000714 case Type::Complex: {
715 // Complex types have the same alignment as their elements, but twice the
716 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000717 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000718 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000719 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000720 Align = EltInfo.second;
721 break;
722 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000723 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000724 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000725 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
726 Width = Layout.getSize();
727 Align = Layout.getAlignment();
728 break;
729 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000730 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000731 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000732 const TagType *TT = cast<TagType>(T);
733
734 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000735 Width = 1;
736 Align = 1;
737 break;
738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Daniel Dunbar1d751182008-11-08 05:48:37 +0000740 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000741 return getTypeInfo(ET->getDecl()->getIntegerType());
742
Daniel Dunbar1d751182008-11-08 05:48:37 +0000743 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000744 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
745 Width = Layout.getSize();
746 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000747 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000748 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000749
John McCall7da24312009-09-05 00:15:47 +0000750 case Type::Elaborated: {
751 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType().getTypePtr());
752 }
753
Douglas Gregor18857642009-04-30 17:32:17 +0000754 case Type::Typedef: {
755 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000756 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregor18857642009-04-30 17:32:17 +0000757 Align = Aligned->getAlignment();
758 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
759 } else
760 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000761 break;
Chris Lattner71763312008-04-06 22:05:18 +0000762 }
Douglas Gregor18857642009-04-30 17:32:17 +0000763
764 case Type::TypeOfExpr:
765 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
766 .getTypePtr());
767
768 case Type::TypeOf:
769 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
770
Anders Carlsson395b4752009-06-24 19:06:50 +0000771 case Type::Decltype:
772 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
773 .getTypePtr());
774
Douglas Gregor18857642009-04-30 17:32:17 +0000775 case Type::QualifiedName:
776 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Douglas Gregor18857642009-04-30 17:32:17 +0000778 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000779 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000780 "Cannot request the size of a dependent type");
781 // FIXME: this is likely to be wrong once we support template
782 // aliases, since a template alias could refer to a typedef that
783 // has an __aligned__ attribute on it.
784 return getTypeInfo(getCanonicalType(T));
785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Chris Lattner464175b2007-07-18 17:52:12 +0000787 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000788 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000789}
790
Chris Lattner34ebde42009-01-27 18:08:34 +0000791/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
792/// type for the current target in bits. This can be different than the ABI
793/// alignment in cases where it is beneficial for performance to overalign
794/// a data type.
795unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
796 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000797
798 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000799 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000800 T = CT->getElementType().getTypePtr();
801 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
802 T->isSpecificBuiltinType(BuiltinType::LongLong))
803 return std::max(ABIAlign, (unsigned)getTypeSize(T));
804
Chris Lattner34ebde42009-01-27 18:08:34 +0000805 return ABIAlign;
806}
807
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000808static void CollectLocalObjCIvars(ASTContext *Ctx,
809 const ObjCInterfaceDecl *OI,
810 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000811 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
812 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000813 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000814 if (!IVDecl->isInvalidDecl())
815 Fields.push_back(cast<FieldDecl>(IVDecl));
816 }
817}
818
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000819void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
820 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
821 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
822 CollectObjCIvars(SuperClass, Fields);
823 CollectLocalObjCIvars(this, OI, Fields);
824}
825
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000826/// ShallowCollectObjCIvars -
827/// Collect all ivars, including those synthesized, in the current class.
828///
829void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
830 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
831 bool CollectSynthesized) {
832 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
833 E = OI->ivar_end(); I != E; ++I) {
834 Ivars.push_back(*I);
835 }
836 if (CollectSynthesized)
837 CollectSynthesizedIvars(OI, Ivars);
838}
839
Fariborz Jahanian98200742009-05-12 18:14:29 +0000840void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
841 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000842 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
843 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian98200742009-05-12 18:14:29 +0000844 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
845 Ivars.push_back(Ivar);
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Fariborz Jahanian98200742009-05-12 18:14:29 +0000847 // Also look into nested protocols.
848 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
849 E = PD->protocol_end(); P != E; ++P)
850 CollectProtocolSynthesizedIvars(*P, Ivars);
851}
852
853/// CollectSynthesizedIvars -
854/// This routine collect synthesized ivars for the designated class.
855///
856void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
857 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000858 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
859 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000860 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
861 Ivars.push_back(Ivar);
862 }
863 // Also look into interface's protocol list for properties declared
864 // in the protocol and whose ivars are synthesized.
865 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
866 PE = OI->protocol_end(); P != PE; ++P) {
867 ObjCProtocolDecl *PD = (*P);
868 CollectProtocolSynthesizedIvars(PD, Ivars);
869 }
870}
871
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000872unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
873 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000874 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
875 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000876 if ((*I)->getPropertyIvarDecl())
877 ++count;
878
879 // Also look into nested protocols.
880 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
881 E = PD->protocol_end(); P != E; ++P)
882 count += CountProtocolSynthesizedIvars(*P);
883 return count;
884}
885
Mike Stump1eb44332009-09-09 15:08:12 +0000886unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000887 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000888 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
889 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000890 if ((*I)->getPropertyIvarDecl())
891 ++count;
892 }
893 // Also look into interface's protocol list for properties declared
894 // in the protocol and whose ivars are synthesized.
895 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
896 PE = OI->protocol_end(); P != PE; ++P) {
897 ObjCProtocolDecl *PD = (*P);
898 count += CountProtocolSynthesizedIvars(PD);
899 }
900 return count;
901}
902
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000903/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
904ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
905 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
906 I = ObjCImpls.find(D);
907 if (I != ObjCImpls.end())
908 return cast<ObjCImplementationDecl>(I->second);
909 return 0;
910}
911/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
912ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
913 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
914 I = ObjCImpls.find(D);
915 if (I != ObjCImpls.end())
916 return cast<ObjCCategoryImplDecl>(I->second);
917 return 0;
918}
919
920/// \brief Set the implementation of ObjCInterfaceDecl.
921void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
922 ObjCImplementationDecl *ImplD) {
923 assert(IFaceD && ImplD && "Passed null params");
924 ObjCImpls[IFaceD] = ImplD;
925}
926/// \brief Set the implementation of ObjCCategoryDecl.
927void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
928 ObjCCategoryImplDecl *ImplD) {
929 assert(CatD && ImplD && "Passed null params");
930 ObjCImpls[CatD] = ImplD;
931}
932
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000933/// \brief Allocate an uninitialized DeclaratorInfo.
934///
935/// The caller should initialize the memory held by DeclaratorInfo using
936/// the TypeLoc wrappers.
937///
938/// \param T the type that will be the basis for type source info. This type
939/// should refer to how the declarator was written in source code, not to
940/// what type semantic analysis resolved the declarator to.
941DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) {
942 unsigned DataSize = TypeLoc::getFullDataSizeForType(T);
943 DeclaratorInfo *DInfo =
944 (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
945 new (DInfo) DeclaratorInfo(T);
946 return DInfo;
947}
948
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000949/// getInterfaceLayoutImpl - Get or compute information about the
950/// layout of the given interface.
951///
952/// \param Impl - If given, also include the layout of the interface's
953/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000954const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000955ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
956 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000957 assert(!D->isForwardDecl() && "Invalid interface decl!");
958
Devang Patel44a3dde2008-06-04 21:54:36 +0000959 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000960 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000961 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
962 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
963 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000964
Daniel Dunbar453addb2009-05-03 11:16:44 +0000965 // Add in synthesized ivar count if laying out an implementation.
966 if (Impl) {
Anders Carlsson29445a02009-07-18 21:19:52 +0000967 unsigned FieldCount = D->ivar_size();
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000968 unsigned SynthCount = CountSynthesizedIvars(D);
969 FieldCount += SynthCount;
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000970 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000971 // entry. Note we can't cache this because we simply free all
972 // entries later; however we shouldn't look up implementations
973 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000974 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000975 return getObjCLayout(D, 0);
976 }
977
Mike Stump1eb44332009-09-09 15:08:12 +0000978 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000979 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
980 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Devang Patel44a3dde2008-06-04 21:54:36 +0000982 return *NewEntry;
983}
984
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000985const ASTRecordLayout &
986ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
987 return getObjCLayout(D, 0);
988}
989
990const ASTRecordLayout &
991ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
992 return getObjCLayout(D->getClassInterface(), D);
993}
994
Devang Patel88a981b2007-11-01 19:11:01 +0000995/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000996/// specified record (struct/union/class), which indicates its size and field
997/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000998const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000999 D = D->getDefinition(*this);
1000 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +00001001
Chris Lattner464175b2007-07-18 17:52:12 +00001002 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +00001003 // Note that we can't save a reference to the entry because this function
1004 // is recursive.
1005 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +00001006 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +00001007
Mike Stump1eb44332009-09-09 15:08:12 +00001008 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +00001009 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +00001010 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattner5d2a6302007-07-18 18:26:58 +00001012 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001013}
1014
Chris Lattnera7674d82007-07-13 22:13:22 +00001015//===----------------------------------------------------------------------===//
1016// Type creation/memoization methods
1017//===----------------------------------------------------------------------===//
1018
John McCall0953e762009-09-24 19:53:00 +00001019QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1020 unsigned Fast = Quals.getFastQualifiers();
1021 Quals.removeFastQualifiers();
1022
1023 // Check if we've already instantiated this type.
1024 llvm::FoldingSetNodeID ID;
1025 ExtQuals::Profile(ID, TypeNode, Quals);
1026 void *InsertPos = 0;
1027 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1028 assert(EQ->getQualifiers() == Quals);
1029 QualType T = QualType(EQ, Fast);
1030 return T;
1031 }
1032
John McCall6b304a02009-09-24 23:30:46 +00001033 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001034 ExtQualNodes.InsertNode(New, InsertPos);
1035 QualType T = QualType(New, Fast);
1036 return T;
1037}
1038
1039QualType ASTContext::getVolatileType(QualType T) {
1040 QualType CanT = getCanonicalType(T);
1041 if (CanT.isVolatileQualified()) return T;
1042
1043 QualifierCollector Quals;
1044 const Type *TypeNode = Quals.strip(T);
1045 Quals.addVolatile();
1046
1047 return getExtQualType(TypeNode, Quals);
1048}
1049
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001050QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001051 QualType CanT = getCanonicalType(T);
1052 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001053 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001054
John McCall0953e762009-09-24 19:53:00 +00001055 // If we are composing extended qualifiers together, merge together
1056 // into one ExtQuals node.
1057 QualifierCollector Quals;
1058 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001059
John McCall0953e762009-09-24 19:53:00 +00001060 // If this type already has an address space specified, it cannot get
1061 // another one.
1062 assert(!Quals.hasAddressSpace() &&
1063 "Type cannot be in multiple addr spaces!");
1064 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001065
John McCall0953e762009-09-24 19:53:00 +00001066 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001067}
1068
Chris Lattnerb7d25532009-02-18 22:53:11 +00001069QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001070 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001071 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001072 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001073 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001075 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001076 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001077 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001078 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1079 return getPointerType(ResultType);
1080 }
1081 }
Mike Stump1eb44332009-09-09 15:08:12 +00001082
John McCall0953e762009-09-24 19:53:00 +00001083 // If we are composing extended qualifiers together, merge together
1084 // into one ExtQuals node.
1085 QualifierCollector Quals;
1086 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001087
John McCall0953e762009-09-24 19:53:00 +00001088 // If this type already has an ObjCGC specified, it cannot get
1089 // another one.
1090 assert(!Quals.hasObjCGCAttr() &&
1091 "Type cannot have multiple ObjCGCs!");
1092 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001093
John McCall0953e762009-09-24 19:53:00 +00001094 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001095}
Chris Lattnera7674d82007-07-13 22:13:22 +00001096
Mike Stump24556362009-07-25 21:26:53 +00001097QualType ASTContext::getNoReturnType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00001098 QualType ResultType;
Mike Stump24556362009-07-25 21:26:53 +00001099 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001100 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001101 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001102 ResultType = getPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001103 } else if (T->isBlockPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001104 QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001105 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001106 ResultType = getBlockPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001107 } else {
1108 assert (T->isFunctionType()
1109 && "can't noreturn qualify non-pointer to function or block type");
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Benjamin Kramerbdbeeb52009-09-25 11:47:22 +00001111 if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) {
1112 ResultType = getFunctionNoProtoType(FNPT->getResultType(), true);
John McCall0953e762009-09-24 19:53:00 +00001113 } else {
1114 const FunctionProtoType *F = T->getAs<FunctionProtoType>();
1115 ResultType
1116 = getFunctionType(F->getResultType(), F->arg_type_begin(),
1117 F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
1118 F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
1119 F->getNumExceptions(), F->exception_begin(), true);
1120 }
Mike Stump24556362009-07-25 21:26:53 +00001121 }
John McCall0953e762009-09-24 19:53:00 +00001122
1123 return getQualifiedType(ResultType, T.getQualifiers());
Mike Stump24556362009-07-25 21:26:53 +00001124}
1125
Reid Spencer5f016e22007-07-11 17:01:13 +00001126/// getComplexType - Return the uniqued reference to the type for a complex
1127/// number with the specified element type.
1128QualType ASTContext::getComplexType(QualType T) {
1129 // Unique pointers, to guarantee there is only one pointer of a particular
1130 // structure.
1131 llvm::FoldingSetNodeID ID;
1132 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 void *InsertPos = 0;
1135 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1136 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 // If the pointee type isn't canonical, this won't be a canonical type either,
1139 // so fill in the canonical type field.
1140 QualType Canonical;
1141 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001142 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 // Get the new insert position for the node we care about.
1145 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001146 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 }
John McCall6b304a02009-09-24 23:30:46 +00001148 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 Types.push_back(New);
1150 ComplexTypes.InsertNode(New, InsertPos);
1151 return QualType(New, 0);
1152}
1153
Eli Friedmanf98aba32009-02-13 02:31:07 +00001154QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1155 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1156 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1157 FixedWidthIntType *&Entry = Map[Width];
1158 if (!Entry)
1159 Entry = new FixedWidthIntType(Width, Signed);
1160 return QualType(Entry, 0);
1161}
Reid Spencer5f016e22007-07-11 17:01:13 +00001162
1163/// getPointerType - Return the uniqued reference to the type for a pointer to
1164/// the specified type.
1165QualType ASTContext::getPointerType(QualType T) {
1166 // Unique pointers, to guarantee there is only one pointer of a particular
1167 // structure.
1168 llvm::FoldingSetNodeID ID;
1169 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 void *InsertPos = 0;
1172 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1173 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 // If the pointee type isn't canonical, this won't be a canonical type either,
1176 // so fill in the canonical type field.
1177 QualType Canonical;
1178 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001179 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 // Get the new insert position for the node we care about.
1182 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001183 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 }
John McCall6b304a02009-09-24 23:30:46 +00001185 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 Types.push_back(New);
1187 PointerTypes.InsertNode(New, InsertPos);
1188 return QualType(New, 0);
1189}
1190
Mike Stump1eb44332009-09-09 15:08:12 +00001191/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001192/// a pointer to the specified block.
1193QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001194 assert(T->isFunctionType() && "block of function types only");
1195 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001196 // structure.
1197 llvm::FoldingSetNodeID ID;
1198 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Steve Naroff5618bd42008-08-27 16:04:49 +00001200 void *InsertPos = 0;
1201 if (BlockPointerType *PT =
1202 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1203 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001204
1205 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001206 // type either so fill in the canonical type field.
1207 QualType Canonical;
1208 if (!T->isCanonical()) {
1209 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Steve Naroff5618bd42008-08-27 16:04:49 +00001211 // Get the new insert position for the node we care about.
1212 BlockPointerType *NewIP =
1213 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001214 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001215 }
John McCall6b304a02009-09-24 23:30:46 +00001216 BlockPointerType *New
1217 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001218 Types.push_back(New);
1219 BlockPointerTypes.InsertNode(New, InsertPos);
1220 return QualType(New, 0);
1221}
1222
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001223/// getLValueReferenceType - Return the uniqued reference to the type for an
1224/// lvalue reference to the specified type.
1225QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 // Unique pointers, to guarantee there is only one pointer of a particular
1227 // structure.
1228 llvm::FoldingSetNodeID ID;
1229 ReferenceType::Profile(ID, T);
1230
1231 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001232 if (LValueReferenceType *RT =
1233 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001235
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 // If the referencee type isn't canonical, this won't be a canonical type
1237 // either, so fill in the canonical type field.
1238 QualType Canonical;
1239 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001240 Canonical = getLValueReferenceType(getCanonicalType(T));
1241
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001243 LValueReferenceType *NewIP =
1244 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001245 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 }
1247
John McCall6b304a02009-09-24 23:30:46 +00001248 LValueReferenceType *New
1249 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001251 LValueReferenceTypes.InsertNode(New, InsertPos);
1252 return QualType(New, 0);
1253}
1254
1255/// getRValueReferenceType - Return the uniqued reference to the type for an
1256/// rvalue reference to the specified type.
1257QualType ASTContext::getRValueReferenceType(QualType T) {
1258 // Unique pointers, to guarantee there is only one pointer of a particular
1259 // structure.
1260 llvm::FoldingSetNodeID ID;
1261 ReferenceType::Profile(ID, T);
1262
1263 void *InsertPos = 0;
1264 if (RValueReferenceType *RT =
1265 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1266 return QualType(RT, 0);
1267
1268 // If the referencee type isn't canonical, this won't be a canonical type
1269 // either, so fill in the canonical type field.
1270 QualType Canonical;
1271 if (!T->isCanonical()) {
1272 Canonical = getRValueReferenceType(getCanonicalType(T));
1273
1274 // Get the new insert position for the node we care about.
1275 RValueReferenceType *NewIP =
1276 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1277 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1278 }
1279
John McCall6b304a02009-09-24 23:30:46 +00001280 RValueReferenceType *New
1281 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001282 Types.push_back(New);
1283 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001284 return QualType(New, 0);
1285}
1286
Sebastian Redlf30208a2009-01-24 21:16:55 +00001287/// getMemberPointerType - Return the uniqued reference to the type for a
1288/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001289QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001290 // Unique pointers, to guarantee there is only one pointer of a particular
1291 // structure.
1292 llvm::FoldingSetNodeID ID;
1293 MemberPointerType::Profile(ID, T, Cls);
1294
1295 void *InsertPos = 0;
1296 if (MemberPointerType *PT =
1297 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1298 return QualType(PT, 0);
1299
1300 // If the pointee or class type isn't canonical, this won't be a canonical
1301 // type either, so fill in the canonical type field.
1302 QualType Canonical;
1303 if (!T->isCanonical()) {
1304 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1305
1306 // Get the new insert position for the node we care about.
1307 MemberPointerType *NewIP =
1308 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1309 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1310 }
John McCall6b304a02009-09-24 23:30:46 +00001311 MemberPointerType *New
1312 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001313 Types.push_back(New);
1314 MemberPointerTypes.InsertNode(New, InsertPos);
1315 return QualType(New, 0);
1316}
1317
Mike Stump1eb44332009-09-09 15:08:12 +00001318/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001319/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001320QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001321 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001322 ArrayType::ArraySizeModifier ASM,
1323 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001324 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1325 "Constant array of VLAs is illegal!");
1326
Chris Lattner38aeec72009-05-13 04:12:56 +00001327 // Convert the array size into a canonical width matching the pointer size for
1328 // the target.
1329 llvm::APInt ArySize(ArySizeIn);
1330 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Reid Spencer5f016e22007-07-11 17:01:13 +00001332 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001333 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001336 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001337 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 // If the element type isn't canonical, this won't be a canonical type either,
1341 // so fill in the canonical type field.
1342 QualType Canonical;
1343 if (!EltTy->isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001344 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001345 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001346 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001347 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001348 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001349 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 }
Mike Stump1eb44332009-09-09 15:08:12 +00001351
John McCall6b304a02009-09-24 23:30:46 +00001352 ConstantArrayType *New = new(*this,TypeAlignment)
1353 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001354 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 Types.push_back(New);
1356 return QualType(New, 0);
1357}
1358
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001359/// getVariableArrayType - Returns a non-unique reference to the type for a
1360/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001361QualType ASTContext::getVariableArrayType(QualType EltTy,
1362 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001363 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001364 unsigned EltTypeQuals,
1365 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001366 // Since we don't unique expressions, it isn't possible to unique VLA's
1367 // that have an expression provided for their size.
1368
John McCall6b304a02009-09-24 23:30:46 +00001369 VariableArrayType *New = new(*this, TypeAlignment)
1370 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001371
1372 VariableArrayTypes.push_back(New);
1373 Types.push_back(New);
1374 return QualType(New, 0);
1375}
1376
Douglas Gregor898574e2008-12-05 23:32:09 +00001377/// getDependentSizedArrayType - Returns a non-unique reference to
1378/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001379/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001380QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1381 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001382 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001383 unsigned EltTypeQuals,
1384 SourceRange Brackets) {
Mike Stump1eb44332009-09-09 15:08:12 +00001385 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001386 "Size must be type- or value-dependent!");
1387
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001388 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001389 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001390 EltTypeQuals, NumElts);
Douglas Gregor898574e2008-12-05 23:32:09 +00001391
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001392 void *InsertPos = 0;
1393 DependentSizedArrayType *Canon
1394 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1395 DependentSizedArrayType *New;
1396 if (Canon) {
1397 // We already have a canonical version of this array type; use it as
1398 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001399 New = new (*this, TypeAlignment)
1400 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1401 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001402 } else {
1403 QualType CanonEltTy = getCanonicalType(EltTy);
1404 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001405 New = new (*this, TypeAlignment)
1406 DependentSizedArrayType(*this, EltTy, QualType(),
1407 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001408 DependentSizedArrayTypes.InsertNode(New, InsertPos);
1409 } else {
1410 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1411 ASM, EltTypeQuals,
1412 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001413 New = new (*this, TypeAlignment)
1414 DependentSizedArrayType(*this, EltTy, Canon,
1415 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001416 }
1417 }
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Douglas Gregor898574e2008-12-05 23:32:09 +00001419 Types.push_back(New);
1420 return QualType(New, 0);
1421}
1422
Eli Friedmanc5773c42008-02-15 18:16:39 +00001423QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1424 ArrayType::ArraySizeModifier ASM,
1425 unsigned EltTypeQuals) {
1426 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001427 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001428
1429 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001430 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001431 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1432 return QualType(ATP, 0);
1433
1434 // If the element type isn't canonical, this won't be a canonical type
1435 // either, so fill in the canonical type field.
1436 QualType Canonical;
1437
1438 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001439 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001440 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001441
1442 // Get the new insert position for the node we care about.
1443 IncompleteArrayType *NewIP =
1444 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001445 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001446 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001447
John McCall6b304a02009-09-24 23:30:46 +00001448 IncompleteArrayType *New = new (*this, TypeAlignment)
1449 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001450
1451 IncompleteArrayTypes.InsertNode(New, InsertPos);
1452 Types.push_back(New);
1453 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001454}
1455
Steve Naroff73322922007-07-18 18:00:27 +00001456/// getVectorType - Return the unique reference to a vector type of
1457/// the specified element type and size. VectorType must be a built-in type.
1458QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001459 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Chris Lattnerf52ab252008-04-06 22:59:24 +00001461 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001462 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Reid Spencer5f016e22007-07-11 17:01:13 +00001464 // Check if we've already instantiated a vector of this type.
1465 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001466 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 void *InsertPos = 0;
1468 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1469 return QualType(VTP, 0);
1470
1471 // If the element type isn't canonical, this won't be a canonical type either,
1472 // so fill in the canonical type field.
1473 QualType Canonical;
1474 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001475 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 // Get the new insert position for the node we care about.
1478 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001479 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001480 }
John McCall6b304a02009-09-24 23:30:46 +00001481 VectorType *New = new (*this, TypeAlignment)
1482 VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001483 VectorTypes.InsertNode(New, InsertPos);
1484 Types.push_back(New);
1485 return QualType(New, 0);
1486}
1487
Nate Begeman213541a2008-04-18 23:10:10 +00001488/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001489/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001490QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001491 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Chris Lattnerf52ab252008-04-06 22:59:24 +00001493 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001494 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Steve Naroff73322922007-07-18 18:00:27 +00001496 // Check if we've already instantiated a vector of this type.
1497 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001498 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001499 void *InsertPos = 0;
1500 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1501 return QualType(VTP, 0);
1502
1503 // If the element type isn't canonical, this won't be a canonical type either,
1504 // so fill in the canonical type field.
1505 QualType Canonical;
1506 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001507 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Steve Naroff73322922007-07-18 18:00:27 +00001509 // Get the new insert position for the node we care about.
1510 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001511 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001512 }
John McCall6b304a02009-09-24 23:30:46 +00001513 ExtVectorType *New = new (*this, TypeAlignment)
1514 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001515 VectorTypes.InsertNode(New, InsertPos);
1516 Types.push_back(New);
1517 return QualType(New, 0);
1518}
1519
Mike Stump1eb44332009-09-09 15:08:12 +00001520QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001521 Expr *SizeExpr,
1522 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001523 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001524 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001525 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001527 void *InsertPos = 0;
1528 DependentSizedExtVectorType *Canon
1529 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1530 DependentSizedExtVectorType *New;
1531 if (Canon) {
1532 // We already have a canonical version of this array type; use it as
1533 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001534 New = new (*this, TypeAlignment)
1535 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1536 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001537 } else {
1538 QualType CanonVecTy = getCanonicalType(vecType);
1539 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001540 New = new (*this, TypeAlignment)
1541 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1542 AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001543 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1544 } else {
1545 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1546 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001547 New = new (*this, TypeAlignment)
1548 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001549 }
1550 }
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001552 Types.push_back(New);
1553 return QualType(New, 0);
1554}
1555
Douglas Gregor72564e72009-02-26 23:50:07 +00001556/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001557///
Mike Stump24556362009-07-25 21:26:53 +00001558QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001559 // Unique functions, to guarantee there is only one function of a particular
1560 // structure.
1561 llvm::FoldingSetNodeID ID;
Mike Stump24556362009-07-25 21:26:53 +00001562 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001565 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001566 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Reid Spencer5f016e22007-07-11 17:01:13 +00001569 QualType Canonical;
1570 if (!ResultTy->isCanonical()) {
Mike Stump24556362009-07-25 21:26:53 +00001571 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001574 FunctionNoProtoType *NewIP =
1575 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001576 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001577 }
Mike Stump1eb44332009-09-09 15:08:12 +00001578
John McCall6b304a02009-09-24 23:30:46 +00001579 FunctionNoProtoType *New = new (*this, TypeAlignment)
1580 FunctionNoProtoType(ResultTy, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001581 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001582 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001583 return QualType(New, 0);
1584}
1585
1586/// getFunctionType - Return a normal function type with a typed argument
1587/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001588QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001589 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001590 unsigned TypeQuals, bool hasExceptionSpec,
1591 bool hasAnyExceptionSpec, unsigned NumExs,
Mike Stump24556362009-07-25 21:26:53 +00001592 const QualType *ExArray, bool NoReturn) {
Anders Carlsson83913e32009-09-16 23:47:08 +00001593 if (LangOpts.CPlusPlus) {
1594 for (unsigned i = 0; i != NumArgs; ++i)
John McCall0953e762009-09-24 19:53:00 +00001595 assert(!ArgArray[i].hasQualifiers() &&
1596 "C++ arguments can't have toplevel qualifiers!");
Anders Carlsson83913e32009-09-16 23:47:08 +00001597 }
1598
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 // Unique functions, to guarantee there is only one function of a particular
1600 // structure.
1601 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001602 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001603 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001604 NumExs, ExArray, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001605
1606 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001607 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001608 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001609 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001610
1611 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001613 if (hasExceptionSpec)
1614 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001615 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1616 if (!ArgArray[i]->isCanonical())
1617 isCanonical = false;
1618
1619 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001620 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001621 QualType Canonical;
1622 if (!isCanonical) {
1623 llvm::SmallVector<QualType, 16> CanonicalArgs;
1624 CanonicalArgs.reserve(NumArgs);
1625 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001626 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001627
Chris Lattnerf52ab252008-04-06 22:59:24 +00001628 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001629 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001630 isVariadic, TypeQuals, false,
1631 false, 0, 0, NoReturn);
Sebastian Redl465226e2009-05-27 22:11:52 +00001632
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001634 FunctionProtoType *NewIP =
1635 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001636 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001638
Douglas Gregor72564e72009-02-26 23:50:07 +00001639 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001640 // for two variable size arrays (for parameter and exception types) at the
1641 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001642 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001643 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1644 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001645 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001646 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001647 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001648 ExArray, NumExs, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001649 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001650 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 return QualType(FTP, 0);
1652}
1653
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001654/// getTypeDeclType - Return the unique reference to the type for the
1655/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001656QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001657 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001658 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001660 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001661 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001662 else if (isa<TemplateTypeParmDecl>(Decl)) {
1663 assert(false && "Template type parameter types are always available.");
Mike Stump9fdbab32009-07-31 02:02:20 +00001664 } else if (ObjCInterfaceDecl *ObjCInterface
1665 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001666 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001667
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001668 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001669 if (PrevDecl)
1670 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001671 else
John McCall6b304a02009-09-24 23:30:46 +00001672 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
Mike Stump9fdbab32009-07-31 02:02:20 +00001673 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001674 if (PrevDecl)
1675 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001676 else
John McCall6b304a02009-09-24 23:30:46 +00001677 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
Mike Stump9fdbab32009-07-31 02:02:20 +00001678 } else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001679 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001680
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001681 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001682 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001683}
1684
Reid Spencer5f016e22007-07-11 17:01:13 +00001685/// getTypedefType - Return the unique reference to the type for the
1686/// specified typename decl.
1687QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1688 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001689
Chris Lattnerf52ab252008-04-06 22:59:24 +00001690 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001691 Decl->TypeForDecl = new(*this, TypeAlignment)
1692 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001693 Types.push_back(Decl->TypeForDecl);
1694 return QualType(Decl->TypeForDecl, 0);
1695}
1696
Douglas Gregorfab9d672009-02-05 23:33:38 +00001697/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001698/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001699/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001700QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001701 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001702 IdentifierInfo *Name) {
1703 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001704 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001705 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001706 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001707 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1708
1709 if (TypeParm)
1710 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001712 if (Name) {
1713 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall6b304a02009-09-24 23:30:46 +00001714 TypeParm = new (*this, TypeAlignment)
1715 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001716 } else
John McCall6b304a02009-09-24 23:30:46 +00001717 TypeParm = new (*this, TypeAlignment)
1718 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001719
1720 Types.push_back(TypeParm);
1721 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1722
1723 return QualType(TypeParm, 0);
1724}
1725
Mike Stump1eb44332009-09-09 15:08:12 +00001726QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001727ASTContext::getTemplateSpecializationType(TemplateName Template,
1728 const TemplateArgument *Args,
1729 unsigned NumArgs,
1730 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001731 if (!Canon.isNull())
1732 Canon = getCanonicalType(Canon);
1733 else {
1734 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001735 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1736 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1737 CanonArgs.reserve(NumArgs);
1738 for (unsigned I = 0; I != NumArgs; ++I)
1739 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1740
1741 // Determine whether this canonical template specialization type already
1742 // exists.
1743 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001744 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001745 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001746
1747 void *InsertPos = 0;
1748 TemplateSpecializationType *Spec
1749 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Douglas Gregor1275ae02009-07-28 23:00:59 +00001751 if (!Spec) {
1752 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001753 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001754 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001755 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001756 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001757 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001758 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001759 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001760 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001761 }
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Douglas Gregorb88e8882009-07-30 17:40:51 +00001763 if (Canon.isNull())
1764 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001765 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001766 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001767 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001768
Douglas Gregor1275ae02009-07-28 23:00:59 +00001769 // Allocate the (non-canonical) template specialization type, but don't
1770 // try to unique it: these types typically have location information that
1771 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001772 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001773 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001774 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001775 TemplateSpecializationType *Spec
1776 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001777 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Douglas Gregor55f6b142009-02-09 18:46:07 +00001779 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001780 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001781}
1782
Mike Stump1eb44332009-09-09 15:08:12 +00001783QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001784ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001785 QualType NamedType) {
1786 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001787 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001788
1789 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001790 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001791 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1792 if (T)
1793 return QualType(T, 0);
1794
Mike Stump1eb44332009-09-09 15:08:12 +00001795 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001796 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001797 Types.push_back(T);
1798 QualifiedNameTypes.InsertNode(T, InsertPos);
1799 return QualType(T, 0);
1800}
1801
Mike Stump1eb44332009-09-09 15:08:12 +00001802QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001803 const IdentifierInfo *Name,
1804 QualType Canon) {
1805 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1806
1807 if (Canon.isNull()) {
1808 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1809 if (CanonNNS != NNS)
1810 Canon = getTypenameType(CanonNNS, Name);
1811 }
1812
1813 llvm::FoldingSetNodeID ID;
1814 TypenameType::Profile(ID, NNS, Name);
1815
1816 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001817 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001818 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1819 if (T)
1820 return QualType(T, 0);
1821
1822 T = new (*this) TypenameType(NNS, Name, Canon);
1823 Types.push_back(T);
1824 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001825 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001826}
1827
Mike Stump1eb44332009-09-09 15:08:12 +00001828QualType
1829ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001830 const TemplateSpecializationType *TemplateId,
1831 QualType Canon) {
1832 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1833
1834 if (Canon.isNull()) {
1835 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1836 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1837 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1838 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00001839 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00001840 assert(CanonTemplateId &&
1841 "Canonical type must also be a template specialization type");
1842 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1843 }
1844 }
1845
1846 llvm::FoldingSetNodeID ID;
1847 TypenameType::Profile(ID, NNS, TemplateId);
1848
1849 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001850 TypenameType *T
Douglas Gregor17343172009-04-01 00:28:59 +00001851 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1852 if (T)
1853 return QualType(T, 0);
1854
1855 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1856 Types.push_back(T);
1857 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001858 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00001859}
1860
John McCall7da24312009-09-05 00:15:47 +00001861QualType
1862ASTContext::getElaboratedType(QualType UnderlyingType,
1863 ElaboratedType::TagKind Tag) {
1864 llvm::FoldingSetNodeID ID;
1865 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001866
John McCall7da24312009-09-05 00:15:47 +00001867 void *InsertPos = 0;
1868 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1869 if (T)
1870 return QualType(T, 0);
1871
1872 QualType Canon = getCanonicalType(UnderlyingType);
1873
1874 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1875 Types.push_back(T);
1876 ElaboratedTypes.InsertNode(T, InsertPos);
1877 return QualType(T, 0);
1878}
1879
Chris Lattner88cb27a2008-04-07 04:56:42 +00001880/// CmpProtocolNames - Comparison predicate for sorting protocols
1881/// alphabetically.
1882static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1883 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001884 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001885}
1886
1887static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1888 unsigned &NumProtocols) {
1889 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Chris Lattner88cb27a2008-04-07 04:56:42 +00001891 // Sort protocols, keyed by name.
1892 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1893
1894 // Remove duplicates.
1895 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1896 NumProtocols = ProtocolsEnd-Protocols;
1897}
1898
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001899/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1900/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00001901QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00001902 ObjCProtocolDecl **Protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001903 unsigned NumProtocols) {
1904 // Sort the protocol list alphabetically to canonicalize it.
1905 if (NumProtocols)
1906 SortAndUniqueProtocols(Protocols, NumProtocols);
1907
1908 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00001909 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001910
1911 void *InsertPos = 0;
1912 if (ObjCObjectPointerType *QT =
1913 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1914 return QualType(QT, 0);
1915
1916 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001917 ObjCObjectPointerType *QType = new (*this, TypeAlignment)
1918 ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001920 Types.push_back(QType);
1921 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1922 return QualType(QType, 0);
1923}
Chris Lattner88cb27a2008-04-07 04:56:42 +00001924
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001925/// getObjCInterfaceType - Return the unique reference to the type for the
1926/// specified ObjC interface decl. The list of protocols is optional.
1927QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001928 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Mike Stump1eb44332009-09-09 15:08:12 +00001929 if (NumProtocols)
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001930 // Sort the protocol list alphabetically to canonicalize it.
1931 SortAndUniqueProtocols(Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001933 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001934 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001936 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001937 if (ObjCInterfaceType *QT =
1938 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001939 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001941 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001942 ObjCInterfaceType *QType = new (*this, TypeAlignment)
1943 ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1944 Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001945 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001946 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001947 return QualType(QType, 0);
1948}
1949
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00001950QualType ASTContext::getObjCProtocolListType(QualType T,
1951 ObjCProtocolDecl **Protocols,
1952 unsigned NumProtocols) {
1953 llvm::FoldingSetNodeID ID;
1954 ObjCProtocolListType::Profile(ID, T, Protocols, NumProtocols);
1955
1956 void *InsertPos = 0;
1957 if (ObjCProtocolListType *QT =
1958 ObjCProtocolListTypes.FindNodeOrInsertPos(ID, InsertPos))
1959 return QualType(QT, 0);
1960
1961 // No Match;
1962 ObjCProtocolListType *QType = new (*this, TypeAlignment)
1963 ObjCProtocolListType(T, Protocols, NumProtocols);
1964 Types.push_back(QType);
1965 ObjCProtocolListTypes.InsertNode(QType, InsertPos);
1966 return QualType(QType, 0);
1967}
1968
Douglas Gregor72564e72009-02-26 23:50:07 +00001969/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1970/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001971/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00001972/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00001973/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001974QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00001975 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00001976 if (tofExpr->isTypeDependent()) {
1977 llvm::FoldingSetNodeID ID;
1978 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001979
Douglas Gregorb1975722009-07-30 23:18:24 +00001980 void *InsertPos = 0;
1981 DependentTypeOfExprType *Canon
1982 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
1983 if (Canon) {
1984 // We already have a "canonical" version of an identical, dependent
1985 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00001986 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00001987 QualType((TypeOfExprType*)Canon, 0));
1988 }
1989 else {
1990 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00001991 Canon
1992 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00001993 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
1994 toe = Canon;
1995 }
1996 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00001997 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00001998 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00001999 }
Steve Naroff9752f252007-08-01 18:02:17 +00002000 Types.push_back(toe);
2001 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002002}
2003
Steve Naroff9752f252007-08-01 18:02:17 +00002004/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2005/// TypeOfType AST's. The only motivation to unique these nodes would be
2006/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002007/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002008/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002009QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002010 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002011 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002012 Types.push_back(tot);
2013 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002014}
2015
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002016/// getDecltypeForExpr - Given an expr, will return the decltype for that
2017/// expression, according to the rules in C++0x [dcl.type.simple]p4
2018static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002019 if (e->isTypeDependent())
2020 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002022 // If e is an id expression or a class member access, decltype(e) is defined
2023 // as the type of the entity named by e.
2024 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2025 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2026 return VD->getType();
2027 }
2028 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2029 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2030 return FD->getType();
2031 }
2032 // If e is a function call or an invocation of an overloaded operator,
2033 // (parentheses around e are ignored), decltype(e) is defined as the
2034 // return type of that function.
2035 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2036 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002038 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002039
2040 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002041 // defined as T&, otherwise decltype(e) is defined as T.
2042 if (e->isLvalue(Context) == Expr::LV_Valid)
2043 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002045 return T;
2046}
2047
Anders Carlsson395b4752009-06-24 19:06:50 +00002048/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2049/// DecltypeType AST's. The only motivation to unique these nodes would be
2050/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002051/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002052/// on canonical type's (which are always unique).
2053QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002054 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002055 if (e->isTypeDependent()) {
2056 llvm::FoldingSetNodeID ID;
2057 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002058
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002059 void *InsertPos = 0;
2060 DependentDecltypeType *Canon
2061 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2062 if (Canon) {
2063 // We already have a "canonical" version of an equivalent, dependent
2064 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002065 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002066 QualType((DecltypeType*)Canon, 0));
2067 }
2068 else {
2069 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002070 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002071 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2072 dt = Canon;
2073 }
2074 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002075 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002076 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002077 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002078 Types.push_back(dt);
2079 return QualType(dt, 0);
2080}
2081
Reid Spencer5f016e22007-07-11 17:01:13 +00002082/// getTagDeclType - Return the unique reference to the type for the
2083/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002084QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002085 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002086 // FIXME: What is the design on getTagDeclType when it requires casting
2087 // away const? mutable?
2088 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002089}
2090
Mike Stump1eb44332009-09-09 15:08:12 +00002091/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2092/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2093/// needs to agree with the definition in <stddef.h>.
Reid Spencer5f016e22007-07-11 17:01:13 +00002094QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002095 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002096}
2097
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002098/// getSignedWCharType - Return the type of "signed wchar_t".
2099/// Used when in C++, as a GCC extension.
2100QualType ASTContext::getSignedWCharType() const {
2101 // FIXME: derive from "Target" ?
2102 return WCharTy;
2103}
2104
2105/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2106/// Used when in C++, as a GCC extension.
2107QualType ASTContext::getUnsignedWCharType() const {
2108 // FIXME: derive from "Target" ?
2109 return UnsignedIntTy;
2110}
2111
Chris Lattner8b9023b2007-07-13 03:05:23 +00002112/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2113/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2114QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002115 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002116}
2117
Chris Lattnere6327742008-04-02 05:18:44 +00002118//===----------------------------------------------------------------------===//
2119// Type Operators
2120//===----------------------------------------------------------------------===//
2121
Chris Lattner77c96472008-04-06 22:41:35 +00002122/// getCanonicalType - Return the canonical (structural) type corresponding to
2123/// the specified potentially non-canonical type. The non-canonical version
2124/// of a type may have many "decorated" versions of types. Decorators can
2125/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2126/// to be free of any of these, allowing two canonical types to be compared
2127/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002128CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002129 QualifierCollector Quals;
2130 const Type *Ptr = Quals.strip(T);
2131 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002132
John McCall0953e762009-09-24 19:53:00 +00002133 // The canonical internal type will be the canonical type *except*
2134 // that we push type qualifiers down through array types.
2135
2136 // If there are no new qualifiers to push down, stop here.
2137 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002138 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002139
John McCall0953e762009-09-24 19:53:00 +00002140 // If the type qualifiers are on an array type, get the canonical
2141 // type of the array with the qualifiers applied to the element
2142 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002143 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2144 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002145 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002146
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002147 // Get the canonical version of the element with the extra qualifiers on it.
2148 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002149 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002150 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002151
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002152 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002153 return CanQualType::CreateUnsafe(
2154 getConstantArrayType(NewEltTy, CAT->getSize(),
2155 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002156 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002157 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002158 return CanQualType::CreateUnsafe(
2159 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002160 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Douglas Gregor898574e2008-12-05 23:32:09 +00002162 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002163 return CanQualType::CreateUnsafe(
2164 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002165 DSAT->getSizeExpr() ?
2166 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002167 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002168 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002169 DSAT->getBracketsRange()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002170
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002171 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002172 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002173 VAT->getSizeExpr() ?
2174 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002175 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002176 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002177 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002178}
2179
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002180TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2181 // If this template name refers to a template, the canonical
2182 // template name merely stores the template itself.
2183 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002184 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002185
Mike Stump1eb44332009-09-09 15:08:12 +00002186 // If this template name refers to a set of overloaded function templates,
Douglas Gregord99cbe62009-07-29 18:26:50 +00002187 /// the canonical template name merely stores the set of function templates.
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002188 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2189 OverloadedFunctionDecl *CanonOvl = 0;
2190 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2191 FEnd = Ovl->function_end();
2192 F != FEnd; ++F) {
2193 Decl *Canon = F->get()->getCanonicalDecl();
2194 if (CanonOvl || Canon != F->get()) {
2195 if (!CanonOvl)
Mike Stump1eb44332009-09-09 15:08:12 +00002196 CanonOvl = OverloadedFunctionDecl::Create(*this,
2197 Ovl->getDeclContext(),
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002198 Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00002199
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002200 CanonOvl->addOverload(
2201 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2202 }
2203 }
Mike Stump1eb44332009-09-09 15:08:12 +00002204
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002205 return TemplateName(CanonOvl? CanonOvl : Ovl);
2206 }
Mike Stump1eb44332009-09-09 15:08:12 +00002207
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002208 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2209 assert(DTN && "Non-dependent template names must refer to template decls.");
2210 return DTN->CanonicalTemplateName;
2211}
2212
Mike Stump1eb44332009-09-09 15:08:12 +00002213TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002214ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2215 switch (Arg.getKind()) {
2216 case TemplateArgument::Null:
2217 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002218
Douglas Gregor1275ae02009-07-28 23:00:59 +00002219 case TemplateArgument::Expression:
2220 // FIXME: Build canonical expression?
2221 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002222
Douglas Gregor1275ae02009-07-28 23:00:59 +00002223 case TemplateArgument::Declaration:
2224 return TemplateArgument(SourceLocation(),
2225 Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002226
Douglas Gregor1275ae02009-07-28 23:00:59 +00002227 case TemplateArgument::Integral:
2228 return TemplateArgument(SourceLocation(),
2229 *Arg.getAsIntegral(),
2230 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002231
Douglas Gregor1275ae02009-07-28 23:00:59 +00002232 case TemplateArgument::Type:
2233 return TemplateArgument(SourceLocation(),
2234 getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002235
Douglas Gregor1275ae02009-07-28 23:00:59 +00002236 case TemplateArgument::Pack: {
2237 // FIXME: Allocate in ASTContext
2238 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2239 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002240 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002241 AEnd = Arg.pack_end();
2242 A != AEnd; (void)++A, ++Idx)
2243 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Douglas Gregor1275ae02009-07-28 23:00:59 +00002245 TemplateArgument Result;
2246 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2247 return Result;
2248 }
2249 }
2250
2251 // Silence GCC warning
2252 assert(false && "Unhandled template argument kind");
2253 return TemplateArgument();
2254}
2255
Douglas Gregord57959a2009-03-27 23:10:48 +00002256NestedNameSpecifier *
2257ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002258 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002259 return 0;
2260
2261 switch (NNS->getKind()) {
2262 case NestedNameSpecifier::Identifier:
2263 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002264 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002265 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2266 NNS->getAsIdentifier());
2267
2268 case NestedNameSpecifier::Namespace:
2269 // A namespace is canonical; build a nested-name-specifier with
2270 // this namespace and no prefix.
2271 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2272
2273 case NestedNameSpecifier::TypeSpec:
2274 case NestedNameSpecifier::TypeSpecWithTemplate: {
2275 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002276 return NestedNameSpecifier::Create(*this, 0,
2277 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002278 T.getTypePtr());
2279 }
2280
2281 case NestedNameSpecifier::Global:
2282 // The global specifier is canonical and unique.
2283 return NNS;
2284 }
2285
2286 // Required to silence a GCC warning
2287 return 0;
2288}
2289
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002290
2291const ArrayType *ASTContext::getAsArrayType(QualType T) {
2292 // Handle the non-qualified case efficiently.
John McCall0953e762009-09-24 19:53:00 +00002293 if (!T.hasQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002294 // Handle the common positive case fast.
2295 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2296 return AT;
2297 }
Mike Stump1eb44332009-09-09 15:08:12 +00002298
John McCall0953e762009-09-24 19:53:00 +00002299 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002300 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002301 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002302 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002303
John McCall0953e762009-09-24 19:53:00 +00002304 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002305 // implements C99 6.7.3p8: "If the specification of an array type includes
2306 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002308 // If we get here, we either have type qualifiers on the type, or we have
2309 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002310 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002311
John McCall0953e762009-09-24 19:53:00 +00002312 QualifierCollector Qs;
2313 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002315 // If we have a simple case, just return now.
2316 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002317 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002318 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002319
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002320 // Otherwise, we have an array and we have qualifiers on it. Push the
2321 // qualifiers into the array element type and return a new array type.
2322 // Get the canonical version of the element with the extra qualifiers on it.
2323 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002324 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002325
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002326 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2327 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2328 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002329 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002330 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2331 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2332 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002333 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002334
Mike Stump1eb44332009-09-09 15:08:12 +00002335 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002336 = dyn_cast<DependentSizedArrayType>(ATy))
2337 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002338 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002339 DSAT->getSizeExpr() ?
2340 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002341 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002342 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002343 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002344
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002345 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002346 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002347 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002348 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002349 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002350 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002351 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002352}
2353
2354
Chris Lattnere6327742008-04-02 05:18:44 +00002355/// getArrayDecayedType - Return the properly qualified result of decaying the
2356/// specified array type to a pointer. This operation is non-trivial when
2357/// handling typedefs etc. The canonical type of "T" must be an array type,
2358/// this returns a pointer to a properly qualified element of the array.
2359///
2360/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2361QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002362 // Get the element type with 'getAsArrayType' so that we don't lose any
2363 // typedefs in the element type of the array. This also handles propagation
2364 // of type qualifiers from the array type into the element type if present
2365 // (C99 6.7.3p8).
2366 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2367 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002368
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002369 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002370
2371 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002372 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002373}
2374
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002375QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002376 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002377 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002378 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002379 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2380 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002381 } else {
John McCall0953e762009-09-24 19:53:00 +00002382 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002383 }
2384 }
2385}
2386
Anders Carlssonfbbce492009-09-25 01:23:32 +00002387QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2388 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002389
Anders Carlssonfbbce492009-09-25 01:23:32 +00002390 if (const ArrayType *AT = getAsArrayType(ElemTy))
2391 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002392
Anders Carlsson6183a992008-12-21 03:44:36 +00002393 return ElemTy;
2394}
2395
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002396/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002397uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002398ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2399 uint64_t ElementCount = 1;
2400 do {
2401 ElementCount *= CA->getSize().getZExtValue();
2402 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2403 } while (CA);
2404 return ElementCount;
2405}
2406
Reid Spencer5f016e22007-07-11 17:01:13 +00002407/// getFloatingRank - Return a relative rank for floating point types.
2408/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002409static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002410 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002411 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002412
John McCall183700f2009-09-21 23:43:11 +00002413 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2414 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002415 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002416 case BuiltinType::Float: return FloatRank;
2417 case BuiltinType::Double: return DoubleRank;
2418 case BuiltinType::LongDouble: return LongDoubleRank;
2419 }
2420}
2421
Mike Stump1eb44332009-09-09 15:08:12 +00002422/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2423/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002424/// 'typeDomain' is a real floating point or complex type.
2425/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002426QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2427 QualType Domain) const {
2428 FloatingRank EltRank = getFloatingRank(Size);
2429 if (Domain->isComplexType()) {
2430 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002431 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002432 case FloatRank: return FloatComplexTy;
2433 case DoubleRank: return DoubleComplexTy;
2434 case LongDoubleRank: return LongDoubleComplexTy;
2435 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002436 }
Chris Lattner1361b112008-04-06 23:58:54 +00002437
2438 assert(Domain->isRealFloatingType() && "Unknown domain!");
2439 switch (EltRank) {
2440 default: assert(0 && "getFloatingRank(): illegal value for rank");
2441 case FloatRank: return FloatTy;
2442 case DoubleRank: return DoubleTy;
2443 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002444 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002445}
2446
Chris Lattner7cfeb082008-04-06 23:55:33 +00002447/// getFloatingTypeOrder - Compare the rank of the two specified floating
2448/// point types, ignoring the domain of the type (i.e. 'double' ==
2449/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002450/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002451int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2452 FloatingRank LHSR = getFloatingRank(LHS);
2453 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002454
Chris Lattnera75cea32008-04-06 23:38:49 +00002455 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002456 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002457 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002458 return 1;
2459 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002460}
2461
Chris Lattnerf52ab252008-04-06 22:59:24 +00002462/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2463/// routine will assert if passed a built-in type that isn't an integer or enum,
2464/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002465unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002466 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002467 if (EnumType* ET = dyn_cast<EnumType>(T))
2468 T = ET->getDecl()->getIntegerType().getTypePtr();
2469
Eli Friedmana3426752009-07-05 23:44:27 +00002470 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2471 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2472
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002473 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2474 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2475
2476 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2477 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2478
Eli Friedmanf98aba32009-02-13 02:31:07 +00002479 // There are two things which impact the integer rank: the width, and
2480 // the ordering of builtins. The builtin ordering is encoded in the
2481 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner1b63e4f2009-06-14 01:54:56 +00002482 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanf98aba32009-02-13 02:31:07 +00002483 return FWIT->getWidth() << 3;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002484
Chris Lattnerf52ab252008-04-06 22:59:24 +00002485 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002486 default: assert(0 && "getIntegerRank(): not a built-in integer");
2487 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002488 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002489 case BuiltinType::Char_S:
2490 case BuiltinType::Char_U:
2491 case BuiltinType::SChar:
2492 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002493 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002494 case BuiltinType::Short:
2495 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002496 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002497 case BuiltinType::Int:
2498 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002499 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002500 case BuiltinType::Long:
2501 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002502 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002503 case BuiltinType::LongLong:
2504 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002505 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002506 case BuiltinType::Int128:
2507 case BuiltinType::UInt128:
2508 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002509 }
2510}
2511
Eli Friedman04e83572009-08-20 04:21:42 +00002512/// \brief Whether this is a promotable bitfield reference according
2513/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2514///
2515/// \returns the type this bit-field will promote to, or NULL if no
2516/// promotion occurs.
2517QualType ASTContext::isPromotableBitField(Expr *E) {
2518 FieldDecl *Field = E->getBitField();
2519 if (!Field)
2520 return QualType();
2521
2522 QualType FT = Field->getType();
2523
2524 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2525 uint64_t BitWidth = BitWidthAP.getZExtValue();
2526 uint64_t IntSize = getTypeSize(IntTy);
2527 // GCC extension compatibility: if the bit-field size is less than or equal
2528 // to the size of int, it gets promoted no matter what its type is.
2529 // For instance, unsigned long bf : 4 gets promoted to signed int.
2530 if (BitWidth < IntSize)
2531 return IntTy;
2532
2533 if (BitWidth == IntSize)
2534 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2535
2536 // Types bigger than int are not subject to promotions, and therefore act
2537 // like the base type.
2538 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2539 // is ridiculous.
2540 return QualType();
2541}
2542
Eli Friedmana95d7572009-08-19 07:44:53 +00002543/// getPromotedIntegerType - Returns the type that Promotable will
2544/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2545/// integer type.
2546QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2547 assert(!Promotable.isNull());
2548 assert(Promotable->isPromotableIntegerType());
2549 if (Promotable->isSignedIntegerType())
2550 return IntTy;
2551 uint64_t PromotableSize = getTypeSize(Promotable);
2552 uint64_t IntSize = getTypeSize(IntTy);
2553 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2554 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2555}
2556
Mike Stump1eb44332009-09-09 15:08:12 +00002557/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002558/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002559/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002560int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002561 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2562 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002563 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002564
Chris Lattnerf52ab252008-04-06 22:59:24 +00002565 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2566 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002567
Chris Lattner7cfeb082008-04-06 23:55:33 +00002568 unsigned LHSRank = getIntegerRank(LHSC);
2569 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002570
Chris Lattner7cfeb082008-04-06 23:55:33 +00002571 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2572 if (LHSRank == RHSRank) return 0;
2573 return LHSRank > RHSRank ? 1 : -1;
2574 }
Mike Stump1eb44332009-09-09 15:08:12 +00002575
Chris Lattner7cfeb082008-04-06 23:55:33 +00002576 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2577 if (LHSUnsigned) {
2578 // If the unsigned [LHS] type is larger, return it.
2579 if (LHSRank >= RHSRank)
2580 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Chris Lattner7cfeb082008-04-06 23:55:33 +00002582 // If the signed type can represent all values of the unsigned type, it
2583 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002584 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002585 return -1;
2586 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002587
Chris Lattner7cfeb082008-04-06 23:55:33 +00002588 // If the unsigned [RHS] type is larger, return it.
2589 if (RHSRank >= LHSRank)
2590 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002591
Chris Lattner7cfeb082008-04-06 23:55:33 +00002592 // If the signed type can represent all values of the unsigned type, it
2593 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002594 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002595 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002596}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002597
Mike Stump1eb44332009-09-09 15:08:12 +00002598// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002599QualType ASTContext::getCFConstantStringType() {
2600 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002601 CFConstantStringTypeDecl =
2602 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002603 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002604 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002605
Anders Carlsson71993dd2007-08-17 05:31:46 +00002606 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002607 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002608 // int flags;
2609 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002610 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002611 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002612 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002613 FieldTypes[3] = LongTy;
2614
Anders Carlsson71993dd2007-08-17 05:31:46 +00002615 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002616 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002617 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002618 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002619 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002620 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002621 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002622 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002623 }
2624
2625 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002626 }
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Anders Carlsson71993dd2007-08-17 05:31:46 +00002628 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002629}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002630
Douglas Gregor319ac892009-04-23 22:29:11 +00002631void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002632 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002633 assert(Rec && "Invalid CFConstantStringType");
2634 CFConstantStringTypeDecl = Rec->getDecl();
2635}
2636
Mike Stump1eb44332009-09-09 15:08:12 +00002637QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002638 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002639 ObjCFastEnumerationStateTypeDecl =
2640 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2641 &Idents.get("__objcFastEnumerationState"));
Mike Stump1eb44332009-09-09 15:08:12 +00002642
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002643 QualType FieldTypes[] = {
2644 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002645 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002646 getPointerType(UnsignedLongTy),
2647 getConstantArrayType(UnsignedLongTy,
2648 llvm::APInt(32, 5), ArrayType::Normal, 0)
2649 };
Mike Stump1eb44332009-09-09 15:08:12 +00002650
Douglas Gregor44b43212008-12-11 16:49:14 +00002651 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002652 FieldDecl *Field = FieldDecl::Create(*this,
2653 ObjCFastEnumerationStateTypeDecl,
2654 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002655 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002656 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002657 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002658 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002659 }
Mike Stump1eb44332009-09-09 15:08:12 +00002660
Douglas Gregor44b43212008-12-11 16:49:14 +00002661 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002662 }
Mike Stump1eb44332009-09-09 15:08:12 +00002663
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002664 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2665}
2666
Douglas Gregor319ac892009-04-23 22:29:11 +00002667void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002668 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002669 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2670 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2671}
2672
Anders Carlssone8c49532007-10-29 06:33:42 +00002673// This returns true if a type has been typedefed to BOOL:
2674// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002675static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002676 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002677 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2678 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00002679
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002680 return false;
2681}
2682
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002683/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002684/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002685int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002686 uint64_t sz = getTypeSize(type);
Mike Stump1eb44332009-09-09 15:08:12 +00002687
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002688 // Make all integer and enum types at least as large as an int
2689 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002690 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002691 // Treat arrays as pointers, since that's how they're passed in.
2692 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002693 sz = getTypeSize(VoidPtrTy);
2694 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002695}
2696
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002697/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002698/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002699void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002700 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002701 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002702 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002703 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002704 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002705 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002706 // Compute size of all parameters.
2707 // Start with computing size of a pointer in number of bytes.
2708 // FIXME: There might(should) be a better way of doing this computation!
2709 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002710 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002711 // The first two arguments (self and _cmd) are pointers; account for
2712 // their size.
2713 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002714 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2715 E = Decl->param_end(); PI != E; ++PI) {
2716 QualType PType = (*PI)->getType();
2717 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002718 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002719 ParmOffset += sz;
2720 }
2721 S += llvm::utostr(ParmOffset);
2722 S += "@0:";
2723 S += llvm::utostr(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002725 // Argument types.
2726 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002727 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2728 E = Decl->param_end(); PI != E; ++PI) {
2729 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00002730 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002731 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002732 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2733 // Use array's original type only if it has known number of
2734 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002735 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002736 PType = PVDecl->getType();
2737 } else if (PType->isFunctionType())
2738 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002739 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002740 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002741 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002742 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002743 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002744 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002745 }
2746}
2747
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002748/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002749/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002750/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2751/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00002752/// Property attributes are stored as a comma-delimited C string. The simple
2753/// attributes readonly and bycopy are encoded as single characters. The
2754/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2755/// encoded as single characters, followed by an identifier. Property types
2756/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002757/// these attributes are defined by the following enumeration:
2758/// @code
2759/// enum PropertyAttributes {
2760/// kPropertyReadOnly = 'R', // property is read-only.
2761/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2762/// kPropertyByref = '&', // property is a reference to the value last assigned
2763/// kPropertyDynamic = 'D', // property is dynamic
2764/// kPropertyGetter = 'G', // followed by getter selector name
2765/// kPropertySetter = 'S', // followed by setter selector name
2766/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2767/// kPropertyType = 't' // followed by old-style type encoding.
2768/// kPropertyWeak = 'W' // 'weak' property
2769/// kPropertyStrong = 'P' // property GC'able
2770/// kPropertyNonAtomic = 'N' // property non-atomic
2771/// };
2772/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00002773void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002774 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002775 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002776 // Collect information from the property implementation decl(s).
2777 bool Dynamic = false;
2778 ObjCPropertyImplDecl *SynthesizePID = 0;
2779
2780 // FIXME: Duplicated code due to poor abstraction.
2781 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00002782 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002783 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2784 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002785 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002786 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002787 ObjCPropertyImplDecl *PID = *i;
2788 if (PID->getPropertyDecl() == PD) {
2789 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2790 Dynamic = true;
2791 } else {
2792 SynthesizePID = PID;
2793 }
2794 }
2795 }
2796 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002797 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002798 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002799 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002800 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002801 ObjCPropertyImplDecl *PID = *i;
2802 if (PID->getPropertyDecl() == PD) {
2803 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2804 Dynamic = true;
2805 } else {
2806 SynthesizePID = PID;
2807 }
2808 }
Mike Stump1eb44332009-09-09 15:08:12 +00002809 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002810 }
2811 }
2812
2813 // FIXME: This is not very efficient.
2814 S = "T";
2815
2816 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002817 // GCC has some special rules regarding encoding of properties which
2818 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00002819 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002820 true /* outermost type */,
2821 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002822
2823 if (PD->isReadOnly()) {
2824 S += ",R";
2825 } else {
2826 switch (PD->getSetterKind()) {
2827 case ObjCPropertyDecl::Assign: break;
2828 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002829 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002830 }
2831 }
2832
2833 // It really isn't clear at all what this means, since properties
2834 // are "dynamic by default".
2835 if (Dynamic)
2836 S += ",D";
2837
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002838 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2839 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002841 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2842 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002843 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002844 }
2845
2846 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2847 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002848 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002849 }
2850
2851 if (SynthesizePID) {
2852 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2853 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002854 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002855 }
2856
2857 // FIXME: OBJCGC: weak & strong
2858}
2859
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002860/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00002861/// Another legacy compatibility encoding: 32-bit longs are encoded as
2862/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002863/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2864///
2865void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00002866 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00002867 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002868 if (BT->getKind() == BuiltinType::ULong &&
2869 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002870 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002871 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002872 if (BT->getKind() == BuiltinType::Long &&
2873 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002874 PointeeTy = IntTy;
2875 }
2876 }
2877}
2878
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002879void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002880 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002881 // We follow the behavior of gcc, expanding structures which are
2882 // directly pointed to, and expanding embedded structures. Note that
2883 // these rules are sufficient to prevent recursive encoding of the
2884 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00002885 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002886 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002887}
2888
Mike Stump1eb44332009-09-09 15:08:12 +00002889static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002890 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002891 const Expr *E = FD->getBitWidth();
2892 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2893 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002894 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002895 S += 'b';
2896 S += llvm::utostr(N);
2897}
2898
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002899void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2900 bool ExpandPointedToStructures,
2901 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002902 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002903 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002904 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00002905 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002906 if (FD && FD->isBitField())
2907 return EncodeBitField(this, S, FD);
2908 char encoding;
2909 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002910 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002911 case BuiltinType::Void: encoding = 'v'; break;
2912 case BuiltinType::Bool: encoding = 'B'; break;
2913 case BuiltinType::Char_U:
2914 case BuiltinType::UChar: encoding = 'C'; break;
2915 case BuiltinType::UShort: encoding = 'S'; break;
2916 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002917 case BuiltinType::ULong:
2918 encoding =
2919 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002920 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002921 case BuiltinType::UInt128: encoding = 'T'; break;
2922 case BuiltinType::ULongLong: encoding = 'Q'; break;
2923 case BuiltinType::Char_S:
2924 case BuiltinType::SChar: encoding = 'c'; break;
2925 case BuiltinType::Short: encoding = 's'; break;
2926 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002927 case BuiltinType::Long:
2928 encoding =
2929 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002930 break;
2931 case BuiltinType::LongLong: encoding = 'q'; break;
2932 case BuiltinType::Int128: encoding = 't'; break;
2933 case BuiltinType::Float: encoding = 'f'; break;
2934 case BuiltinType::Double: encoding = 'd'; break;
2935 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002936 }
Mike Stump1eb44332009-09-09 15:08:12 +00002937
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002938 S += encoding;
2939 return;
2940 }
Mike Stump1eb44332009-09-09 15:08:12 +00002941
John McCall183700f2009-09-21 23:43:11 +00002942 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002943 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00002944 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002945 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002946 return;
2947 }
Mike Stump1eb44332009-09-09 15:08:12 +00002948
Ted Kremenek6217b802009-07-29 21:53:49 +00002949 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002950 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002951 bool isReadOnly = false;
2952 // For historical/compatibility reasons, the read-only qualifier of the
2953 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2954 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00002955 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00002956 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002957 if (OutermostType && T.isConstQualified()) {
2958 isReadOnly = true;
2959 S += 'r';
2960 }
Mike Stump9fdbab32009-07-31 02:02:20 +00002961 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002962 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00002963 while (P->getAs<PointerType>())
2964 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002965 if (P.isConstQualified()) {
2966 isReadOnly = true;
2967 S += 'r';
2968 }
2969 }
2970 if (isReadOnly) {
2971 // Another legacy compatibility encoding. Some ObjC qualifier and type
2972 // combinations need to be rearranged.
2973 // Rewrite "in const" from "nr" to "rn"
2974 const char * s = S.c_str();
2975 int len = S.length();
2976 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2977 std::string replace = "rn";
2978 S.replace(S.end()-2, S.end(), replace);
2979 }
2980 }
Steve Naroff14108da2009-07-10 23:34:53 +00002981 if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002982 S += ':';
2983 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002984 }
Mike Stump1eb44332009-09-09 15:08:12 +00002985
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002986 if (PointeeTy->isCharType()) {
2987 // char pointer types should be encoded as '*' unless it is a
2988 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002989 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002990 S += '*';
2991 return;
2992 }
Ted Kremenek6217b802009-07-29 21:53:49 +00002993 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00002994 // GCC binary compat: Need to convert "struct objc_class *" to "#".
2995 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
2996 S += '#';
2997 return;
2998 }
2999 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3000 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3001 S += '@';
3002 return;
3003 }
3004 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003005 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003006 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003007 getLegacyIntegralTypeEncoding(PointeeTy);
3008
Mike Stump1eb44332009-09-09 15:08:12 +00003009 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003010 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003011 return;
3012 }
Mike Stump1eb44332009-09-09 15:08:12 +00003013
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003014 if (const ArrayType *AT =
3015 // Ignore type qualifiers etc.
3016 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003017 if (isa<IncompleteArrayType>(AT)) {
3018 // Incomplete arrays are encoded as a pointer to the array element.
3019 S += '^';
3020
Mike Stump1eb44332009-09-09 15:08:12 +00003021 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003022 false, ExpandStructures, FD);
3023 } else {
3024 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003025
Anders Carlsson559a8332009-02-22 01:38:57 +00003026 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3027 S += llvm::utostr(CAT->getSize().getZExtValue());
3028 else {
3029 //Variable length arrays are encoded as a regular array with 0 elements.
3030 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3031 S += '0';
3032 }
Mike Stump1eb44332009-09-09 15:08:12 +00003033
3034 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003035 false, ExpandStructures, FD);
3036 S += ']';
3037 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003038 return;
3039 }
Mike Stump1eb44332009-09-09 15:08:12 +00003040
John McCall183700f2009-09-21 23:43:11 +00003041 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003042 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003043 return;
3044 }
Mike Stump1eb44332009-09-09 15:08:12 +00003045
Ted Kremenek6217b802009-07-29 21:53:49 +00003046 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003047 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003048 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003049 // Anonymous structures print as '?'
3050 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3051 S += II->getName();
3052 } else {
3053 S += '?';
3054 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003055 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003056 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003057 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3058 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003059 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003060 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003061 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003062 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003063 S += '"';
3064 }
Mike Stump1eb44332009-09-09 15:08:12 +00003065
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003066 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003067 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003068 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003069 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003070 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003071 QualType qt = Field->getType();
3072 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003073 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003074 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003075 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003076 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003077 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003078 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003079 return;
3080 }
Mike Stump1eb44332009-09-09 15:08:12 +00003081
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003082 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003083 if (FD && FD->isBitField())
3084 EncodeBitField(this, S, FD);
3085 else
3086 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003087 return;
3088 }
Mike Stump1eb44332009-09-09 15:08:12 +00003089
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003090 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003091 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003092 return;
3093 }
Mike Stump1eb44332009-09-09 15:08:12 +00003094
John McCall0953e762009-09-24 19:53:00 +00003095 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003096 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003097 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003098 S += '{';
3099 const IdentifierInfo *II = OI->getIdentifier();
3100 S += II->getName();
3101 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003102 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003103 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003104 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003105 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003106 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003107 RecFields[i]);
3108 else
Mike Stump1eb44332009-09-09 15:08:12 +00003109 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003110 FD);
3111 }
3112 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003113 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003114 }
Mike Stump1eb44332009-09-09 15:08:12 +00003115
John McCall183700f2009-09-21 23:43:11 +00003116 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003117 if (OPT->isObjCIdType()) {
3118 S += '@';
3119 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003120 }
Mike Stump1eb44332009-09-09 15:08:12 +00003121
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003122 if (OPT->isObjCClassType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003123 S += '#';
3124 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003125 }
Mike Stump1eb44332009-09-09 15:08:12 +00003126
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003127 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003128 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003129 ExpandPointedToStructures,
3130 ExpandStructures, FD);
3131 if (FD || EncodingProperty) {
3132 // Note that we do extended encoding of protocol qualifer list
3133 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003134 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003135 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3136 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003137 S += '<';
3138 S += (*I)->getNameAsString();
3139 S += '>';
3140 }
3141 S += '"';
3142 }
3143 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003144 }
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003146 QualType PointeeTy = OPT->getPointeeType();
3147 if (!EncodingProperty &&
3148 isa<TypedefType>(PointeeTy.getTypePtr())) {
3149 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003150 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003151 // {...};
3152 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003153 getObjCEncodingForTypeImpl(PointeeTy, S,
3154 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003155 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003156 return;
3157 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003158
3159 S += '@';
3160 if (FD || EncodingProperty) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003161 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003162 S += OPT->getInterfaceDecl()->getNameAsCString();
3163 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3164 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003165 S += '<';
3166 S += (*I)->getNameAsString();
3167 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003168 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003169 S += '"';
3170 }
3171 return;
3172 }
Mike Stump1eb44332009-09-09 15:08:12 +00003173
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003174 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003175}
3176
Mike Stump1eb44332009-09-09 15:08:12 +00003177void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003178 std::string& S) const {
3179 if (QT & Decl::OBJC_TQ_In)
3180 S += 'n';
3181 if (QT & Decl::OBJC_TQ_Inout)
3182 S += 'N';
3183 if (QT & Decl::OBJC_TQ_Out)
3184 S += 'o';
3185 if (QT & Decl::OBJC_TQ_Bycopy)
3186 S += 'O';
3187 if (QT & Decl::OBJC_TQ_Byref)
3188 S += 'R';
3189 if (QT & Decl::OBJC_TQ_Oneway)
3190 S += 'V';
3191}
3192
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003193void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003194 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003195
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003196 BuiltinVaListType = T;
3197}
3198
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003199void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003200 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003201}
3202
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003203void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor319ac892009-04-23 22:29:11 +00003204 ObjCSelType = T;
3205
John McCall183700f2009-09-21 23:43:11 +00003206 const TypedefType *TT = T->getAs<TypedefType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003207 if (!TT)
3208 return;
3209 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003210
3211 // typedef struct objc_selector *SEL;
Ted Kremenek6217b802009-07-29 21:53:49 +00003212 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003213 if (!ptr)
3214 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003215 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003216 if (!rec)
3217 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003218 SelStructType = rec;
3219}
3220
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003221void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003222 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003223}
3224
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003225void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003226 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003227}
3228
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003229void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003230 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003231 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003232
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003233 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003234}
3235
Douglas Gregor7532dc62009-03-30 22:58:21 +00003236/// \brief Retrieve the template name that represents a qualified
3237/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003238TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003239 bool TemplateKeyword,
3240 TemplateDecl *Template) {
3241 llvm::FoldingSetNodeID ID;
3242 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3243
3244 void *InsertPos = 0;
3245 QualifiedTemplateName *QTN =
3246 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3247 if (!QTN) {
3248 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3249 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3250 }
3251
3252 return TemplateName(QTN);
3253}
3254
Douglas Gregord99cbe62009-07-29 18:26:50 +00003255/// \brief Retrieve the template name that represents a qualified
3256/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003257TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregord99cbe62009-07-29 18:26:50 +00003258 bool TemplateKeyword,
3259 OverloadedFunctionDecl *Template) {
3260 llvm::FoldingSetNodeID ID;
3261 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump1eb44332009-09-09 15:08:12 +00003262
Douglas Gregord99cbe62009-07-29 18:26:50 +00003263 void *InsertPos = 0;
3264 QualifiedTemplateName *QTN =
3265 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3266 if (!QTN) {
3267 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3268 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3269 }
Mike Stump1eb44332009-09-09 15:08:12 +00003270
Douglas Gregord99cbe62009-07-29 18:26:50 +00003271 return TemplateName(QTN);
3272}
3273
Douglas Gregor7532dc62009-03-30 22:58:21 +00003274/// \brief Retrieve the template name that represents a dependent
3275/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003276TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003277 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003278 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003279 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003280
3281 llvm::FoldingSetNodeID ID;
3282 DependentTemplateName::Profile(ID, NNS, Name);
3283
3284 void *InsertPos = 0;
3285 DependentTemplateName *QTN =
3286 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3287
3288 if (QTN)
3289 return TemplateName(QTN);
3290
3291 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3292 if (CanonNNS == NNS) {
3293 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3294 } else {
3295 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3296 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3297 }
3298
3299 DependentTemplateNames.InsertNode(QTN, InsertPos);
3300 return TemplateName(QTN);
3301}
3302
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003303/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003304/// TargetInfo, produce the corresponding type. The unsigned @p Type
3305/// is actually a value of type @c TargetInfo::IntType.
3306QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003307 switch (Type) {
Mike Stump1eb44332009-09-09 15:08:12 +00003308 case TargetInfo::NoInt: return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003309 case TargetInfo::SignedShort: return ShortTy;
3310 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3311 case TargetInfo::SignedInt: return IntTy;
3312 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3313 case TargetInfo::SignedLong: return LongTy;
3314 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3315 case TargetInfo::SignedLongLong: return LongLongTy;
3316 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3317 }
3318
3319 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00003320 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003321}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003322
3323//===----------------------------------------------------------------------===//
3324// Type Predicates.
3325//===----------------------------------------------------------------------===//
3326
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003327/// isObjCNSObjectType - Return true if this is an NSObject object using
3328/// NSObject attribute on a c-style pointer type.
3329/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003330/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003331///
3332bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3333 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3334 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003335 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003336 return true;
3337 }
Mike Stump1eb44332009-09-09 15:08:12 +00003338 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003339}
3340
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003341/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3342/// garbage collection attribute.
3343///
John McCall0953e762009-09-24 19:53:00 +00003344Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3345 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003346 if (getLangOptions().ObjC1 &&
3347 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003348 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003349 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003350 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003351 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003352 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003353 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003354 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003355 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003356 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003357 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003358 // Non-pointers have none gc'able attribute regardless of the attribute
3359 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003360 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003361 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003362 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003363 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003364}
3365
Chris Lattner6ac46a42008-04-07 06:51:04 +00003366//===----------------------------------------------------------------------===//
3367// Type Compatibility Testing
3368//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003369
Mike Stump1eb44332009-09-09 15:08:12 +00003370/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003371/// compatible.
3372static bool areCompatVectorTypes(const VectorType *LHS,
3373 const VectorType *RHS) {
3374 assert(LHS->isCanonical() && RHS->isCanonical());
3375 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003376 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003377}
3378
Steve Naroff4084c302009-07-23 01:01:38 +00003379//===----------------------------------------------------------------------===//
3380// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3381//===----------------------------------------------------------------------===//
3382
3383/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3384/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003385bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3386 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003387 if (lProto == rProto)
3388 return true;
3389 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3390 E = rProto->protocol_end(); PI != E; ++PI)
3391 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3392 return true;
3393 return false;
3394}
3395
Steve Naroff4084c302009-07-23 01:01:38 +00003396/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3397/// return true if lhs's protocols conform to rhs's protocol; false
3398/// otherwise.
3399bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3400 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3401 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3402 return false;
3403}
3404
3405/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3406/// ObjCQualifiedIDType.
3407bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3408 bool compare) {
3409 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003410 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003411 lhs->isObjCIdType() || lhs->isObjCClassType())
3412 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003413 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003414 rhs->isObjCIdType() || rhs->isObjCClassType())
3415 return true;
3416
3417 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003418 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003419
Steve Naroff4084c302009-07-23 01:01:38 +00003420 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003421
Steve Naroff4084c302009-07-23 01:01:38 +00003422 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003423 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003424 // make sure we check the class hierarchy.
3425 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3426 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3427 E = lhsQID->qual_end(); I != E; ++I) {
3428 // when comparing an id<P> on lhs with a static type on rhs,
3429 // see if static class implements all of id's protocols, directly or
3430 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003431 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003432 return false;
3433 }
3434 }
3435 // If there are no qualifiers and no interface, we have an 'id'.
3436 return true;
3437 }
Mike Stump1eb44332009-09-09 15:08:12 +00003438 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003439 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3440 E = lhsQID->qual_end(); I != E; ++I) {
3441 ObjCProtocolDecl *lhsProto = *I;
3442 bool match = false;
3443
3444 // when comparing an id<P> on lhs with a static type on rhs,
3445 // see if static class implements all of id's protocols, directly or
3446 // through its super class and categories.
3447 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3448 E = rhsOPT->qual_end(); J != E; ++J) {
3449 ObjCProtocolDecl *rhsProto = *J;
3450 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3451 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3452 match = true;
3453 break;
3454 }
3455 }
Mike Stump1eb44332009-09-09 15:08:12 +00003456 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00003457 // make sure we check the class hierarchy.
3458 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3459 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3460 E = lhsQID->qual_end(); I != E; ++I) {
3461 // when comparing an id<P> on lhs with a static type on rhs,
3462 // see if static class implements all of id's protocols, directly or
3463 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003464 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003465 match = true;
3466 break;
3467 }
3468 }
3469 }
3470 if (!match)
3471 return false;
3472 }
Mike Stump1eb44332009-09-09 15:08:12 +00003473
Steve Naroff4084c302009-07-23 01:01:38 +00003474 return true;
3475 }
Mike Stump1eb44332009-09-09 15:08:12 +00003476
Steve Naroff4084c302009-07-23 01:01:38 +00003477 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3478 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3479
Mike Stump1eb44332009-09-09 15:08:12 +00003480 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00003481 lhs->getAsObjCInterfacePointerType()) {
3482 if (lhsOPT->qual_empty()) {
3483 bool match = false;
3484 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3485 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3486 E = rhsQID->qual_end(); I != E; ++I) {
3487 // when comparing an id<P> on lhs with a static type on rhs,
3488 // see if static class implements all of id's protocols, directly or
3489 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003490 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003491 match = true;
3492 break;
3493 }
3494 }
3495 if (!match)
3496 return false;
3497 }
3498 return true;
3499 }
Mike Stump1eb44332009-09-09 15:08:12 +00003500 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003501 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3502 E = lhsOPT->qual_end(); I != E; ++I) {
3503 ObjCProtocolDecl *lhsProto = *I;
3504 bool match = false;
3505
3506 // when comparing an id<P> on lhs with a static type on rhs,
3507 // see if static class implements all of id's protocols, directly or
3508 // through its super class and categories.
3509 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3510 E = rhsQID->qual_end(); J != E; ++J) {
3511 ObjCProtocolDecl *rhsProto = *J;
3512 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3513 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3514 match = true;
3515 break;
3516 }
3517 }
3518 if (!match)
3519 return false;
3520 }
3521 return true;
3522 }
3523 return false;
3524}
3525
Eli Friedman3d815e72008-08-22 00:56:42 +00003526/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003527/// compatible for assignment from RHS to LHS. This handles validation of any
3528/// protocol qualifiers on the LHS or RHS.
3529///
Steve Naroff14108da2009-07-10 23:34:53 +00003530bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3531 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003532 // If either type represents the built-in 'id' or 'Class' types, return true.
3533 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00003534 return true;
3535
Steve Naroff4084c302009-07-23 01:01:38 +00003536 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00003537 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3538 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00003539 false);
3540
Steve Naroff14108da2009-07-10 23:34:53 +00003541 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3542 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00003543 if (LHS && RHS) // We have 2 user-defined types.
3544 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00003545
Steve Naroff4084c302009-07-23 01:01:38 +00003546 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003547}
3548
Eli Friedman3d815e72008-08-22 00:56:42 +00003549bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3550 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00003551 // Verify that the base decls are compatible: the RHS must be a subclass of
3552 // the LHS.
3553 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3554 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003555
Chris Lattner6ac46a42008-04-07 06:51:04 +00003556 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3557 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003558 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003559 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003560
Chris Lattner6ac46a42008-04-07 06:51:04 +00003561 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3562 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003563 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003564 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00003565
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003566 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3567 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003568 LHSPI != LHSPE; LHSPI++) {
3569 bool RHSImplementsProtocol = false;
3570
3571 // If the RHS doesn't implement the protocol on the left, the types
3572 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003573 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00003574 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00003575 RHSPI != RHSPE; RHSPI++) {
3576 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003577 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003578 break;
3579 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003580 }
3581 // FIXME: For better diagnostics, consider passing back the protocol name.
3582 if (!RHSImplementsProtocol)
3583 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003584 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003585 // The RHS implements all protocols listed on the LHS.
3586 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003587}
3588
Steve Naroff389bf462009-02-12 17:52:19 +00003589bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3590 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00003591 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3592 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003593
Steve Naroff14108da2009-07-10 23:34:53 +00003594 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00003595 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003596
3597 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3598 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00003599}
3600
Mike Stump1eb44332009-09-09 15:08:12 +00003601/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00003602/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00003603/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00003604/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00003605bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3606 return !mergeTypes(LHS, RHS).isNull();
3607}
3608
3609QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall183700f2009-09-21 23:43:11 +00003610 const FunctionType *lbase = lhs->getAs<FunctionType>();
3611 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00003612 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3613 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00003614 bool allLTypes = true;
3615 bool allRTypes = true;
3616
3617 // Check return type
3618 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3619 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003620 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3621 allLTypes = false;
3622 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3623 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00003624 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00003625 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3626 if (NoReturn != lbase->getNoReturnAttr())
3627 allLTypes = false;
3628 if (NoReturn != rbase->getNoReturnAttr())
3629 allRTypes = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003630
Eli Friedman3d815e72008-08-22 00:56:42 +00003631 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00003632 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3633 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003634 unsigned lproto_nargs = lproto->getNumArgs();
3635 unsigned rproto_nargs = rproto->getNumArgs();
3636
3637 // Compatible functions must have the same number of arguments
3638 if (lproto_nargs != rproto_nargs)
3639 return QualType();
3640
3641 // Variadic and non-variadic functions aren't compatible
3642 if (lproto->isVariadic() != rproto->isVariadic())
3643 return QualType();
3644
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003645 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3646 return QualType();
3647
Eli Friedman3d815e72008-08-22 00:56:42 +00003648 // Check argument compatibility
3649 llvm::SmallVector<QualType, 10> types;
3650 for (unsigned i = 0; i < lproto_nargs; i++) {
3651 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3652 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3653 QualType argtype = mergeTypes(largtype, rargtype);
3654 if (argtype.isNull()) return QualType();
3655 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003656 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3657 allLTypes = false;
3658 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3659 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003660 }
3661 if (allLTypes) return lhs;
3662 if (allRTypes) return rhs;
3663 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00003664 lproto->isVariadic(), lproto->getTypeQuals(),
3665 NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003666 }
3667
3668 if (lproto) allRTypes = false;
3669 if (rproto) allLTypes = false;
3670
Douglas Gregor72564e72009-02-26 23:50:07 +00003671 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003672 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003673 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003674 if (proto->isVariadic()) return QualType();
3675 // Check that the types are compatible with the types that
3676 // would result from default argument promotions (C99 6.7.5.3p15).
3677 // The only types actually affected are promotable integer
3678 // types and floats, which would be passed as a different
3679 // type depending on whether the prototype is visible.
3680 unsigned proto_nargs = proto->getNumArgs();
3681 for (unsigned i = 0; i < proto_nargs; ++i) {
3682 QualType argTy = proto->getArgType(i);
3683 if (argTy->isPromotableIntegerType() ||
3684 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3685 return QualType();
3686 }
3687
3688 if (allLTypes) return lhs;
3689 if (allRTypes) return rhs;
3690 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00003691 proto->getNumArgs(), proto->isVariadic(),
3692 proto->getTypeQuals(), NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003693 }
3694
3695 if (allLTypes) return lhs;
3696 if (allRTypes) return rhs;
Mike Stump24556362009-07-25 21:26:53 +00003697 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003698}
3699
3700QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003701 // C++ [expr]: If an expression initially has the type "reference to T", the
3702 // type is adjusted to "T" prior to any further analysis, the expression
3703 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003704 // expression is an lvalue unless the reference is an rvalue reference and
3705 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003706 // FIXME: C++ shouldn't be going through here! The rules are different
3707 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003708 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3709 // shouldn't be going through here!
Ted Kremenek6217b802009-07-29 21:53:49 +00003710 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003711 LHS = RT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00003712 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003713 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003714
Eli Friedman3d815e72008-08-22 00:56:42 +00003715 QualType LHSCan = getCanonicalType(LHS),
3716 RHSCan = getCanonicalType(RHS);
3717
3718 // If two types are identical, they are compatible.
3719 if (LHSCan == RHSCan)
3720 return LHS;
3721
John McCall0953e762009-09-24 19:53:00 +00003722 // If the qualifiers are different, the types aren't compatible... mostly.
3723 Qualifiers LQuals = LHSCan.getQualifiers();
3724 Qualifiers RQuals = RHSCan.getQualifiers();
3725 if (LQuals != RQuals) {
3726 // If any of these qualifiers are different, we have a type
3727 // mismatch.
3728 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
3729 LQuals.getAddressSpace() != RQuals.getAddressSpace())
3730 return QualType();
3731
3732 // Exactly one GC qualifier difference is allowed: __strong is
3733 // okay if the other type has no GC qualifier but is an Objective
3734 // C object pointer (i.e. implicitly strong by default). We fix
3735 // this by pretending that the unqualified type was actually
3736 // qualified __strong.
3737 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
3738 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
3739 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
3740
3741 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
3742 return QualType();
3743
3744 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
3745 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
3746 }
3747 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
3748 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
3749 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003750 return QualType();
John McCall0953e762009-09-24 19:53:00 +00003751 }
3752
3753 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00003754
Eli Friedman852d63b2009-06-01 01:22:52 +00003755 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3756 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003757
Chris Lattner1adb8832008-01-14 05:45:46 +00003758 // We want to consider the two function types to be the same for these
3759 // comparisons, just force one to the other.
3760 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3761 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003762
3763 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003764 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3765 LHSClass = Type::ConstantArray;
3766 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3767 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00003768
Nate Begeman213541a2008-04-18 23:10:10 +00003769 // Canonicalize ExtVector -> Vector.
3770 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3771 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00003772
Chris Lattnera36a61f2008-04-07 05:43:21 +00003773 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003774 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00003775 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00003776 // a signed integer type, or an unsigned integer type.
John McCall183700f2009-09-21 23:43:11 +00003777 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003778 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3779 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003780 }
John McCall183700f2009-09-21 23:43:11 +00003781 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003782 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3783 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003784 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003785
Eli Friedman3d815e72008-08-22 00:56:42 +00003786 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003787 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003788
Steve Naroff4a746782008-01-09 22:43:08 +00003789 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003790 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003791#define TYPE(Class, Base)
3792#define ABSTRACT_TYPE(Class, Base)
3793#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3794#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3795#include "clang/AST/TypeNodes.def"
3796 assert(false && "Non-canonical and dependent types shouldn't get here");
3797 return QualType();
3798
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003799 case Type::LValueReference:
3800 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003801 case Type::MemberPointer:
3802 assert(false && "C++ should never be in mergeTypes");
3803 return QualType();
3804
3805 case Type::IncompleteArray:
3806 case Type::VariableArray:
3807 case Type::FunctionProto:
3808 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00003809 assert(false && "Types are eliminated above");
3810 return QualType();
3811
Chris Lattner1adb8832008-01-14 05:45:46 +00003812 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003813 {
3814 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003815 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
3816 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003817 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3818 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00003819 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003820 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00003821 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003822 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003823 return getPointerType(ResultType);
3824 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003825 case Type::BlockPointer:
3826 {
3827 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003828 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
3829 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00003830 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3831 if (ResultType.isNull()) return QualType();
3832 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3833 return LHS;
3834 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3835 return RHS;
3836 return getBlockPointerType(ResultType);
3837 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003838 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003839 {
3840 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3841 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3842 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3843 return QualType();
3844
3845 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3846 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3847 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3848 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003849 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3850 return LHS;
3851 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3852 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003853 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3854 ArrayType::ArraySizeModifier(), 0);
3855 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3856 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003857 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3858 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003859 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3860 return LHS;
3861 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3862 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003863 if (LVAT) {
3864 // FIXME: This isn't correct! But tricky to implement because
3865 // the array's size has to be the size of LHS, but the type
3866 // has to be different.
3867 return LHS;
3868 }
3869 if (RVAT) {
3870 // FIXME: This isn't correct! But tricky to implement because
3871 // the array's size has to be the size of RHS, but the type
3872 // has to be different.
3873 return RHS;
3874 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003875 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3876 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003877 return getIncompleteArrayType(ResultType,
3878 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003879 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003880 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003881 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003882 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003883 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003884 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003885 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003886 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003887 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003888 case Type::Complex:
3889 // Distinct complex types are incompatible.
3890 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003891 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003892 // FIXME: The merged type should be an ExtVector!
John McCall183700f2009-09-21 23:43:11 +00003893 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00003894 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003895 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003896 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003897 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003898 // FIXME: This should be type compatibility, e.g. whether
3899 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00003900 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
3901 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00003902 if (LHSIface && RHSIface &&
3903 canAssignObjCInterfaces(LHSIface, RHSIface))
3904 return LHS;
3905
Eli Friedman3d815e72008-08-22 00:56:42 +00003906 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003907 }
Steve Naroff14108da2009-07-10 23:34:53 +00003908 case Type::ObjCObjectPointer: {
John McCall183700f2009-09-21 23:43:11 +00003909 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
3910 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00003911 return LHS;
3912
Steve Naroffbc76dd02008-12-10 22:14:21 +00003913 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00003914 }
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003915 case Type::FixedWidthInt:
3916 // Distinct fixed-width integers are not compatible.
3917 return QualType();
Douglas Gregor7532dc62009-03-30 22:58:21 +00003918 case Type::TemplateSpecialization:
3919 assert(false && "Dependent types have no size");
3920 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003921 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003922
3923 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003924}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003925
Chris Lattner5426bf62008-04-07 07:01:58 +00003926//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003927// Integer Predicates
3928//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003929
Eli Friedmanad74a752008-06-28 06:23:08 +00003930unsigned ASTContext::getIntWidth(QualType T) {
3931 if (T == BoolTy)
3932 return 1;
Chris Lattner6a2b9262009-10-17 20:33:28 +00003933 if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003934 return FWIT->getWidth();
3935 }
3936 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003937 return (unsigned)getTypeSize(T);
3938}
3939
3940QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3941 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00003942
3943 // Turn <4 x signed int> -> <4 x unsigned int>
3944 if (const VectorType *VTy = T->getAs<VectorType>())
3945 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
3946 VTy->getNumElements());
3947
3948 // For enums, we return the unsigned version of the base type.
3949 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00003950 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00003951
3952 const BuiltinType *BTy = T->getAs<BuiltinType>();
3953 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00003954 switch (BTy->getKind()) {
3955 case BuiltinType::Char_S:
3956 case BuiltinType::SChar:
3957 return UnsignedCharTy;
3958 case BuiltinType::Short:
3959 return UnsignedShortTy;
3960 case BuiltinType::Int:
3961 return UnsignedIntTy;
3962 case BuiltinType::Long:
3963 return UnsignedLongTy;
3964 case BuiltinType::LongLong:
3965 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003966 case BuiltinType::Int128:
3967 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003968 default:
3969 assert(0 && "Unexpected signed integer type");
3970 return QualType();
3971 }
3972}
3973
Douglas Gregor2cf26342009-04-09 22:27:44 +00003974ExternalASTSource::~ExternalASTSource() { }
3975
3976void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00003977
3978
3979//===----------------------------------------------------------------------===//
3980// Builtin Type Computation
3981//===----------------------------------------------------------------------===//
3982
3983/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
3984/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00003985static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00003986 ASTContext::GetBuiltinTypeError &Error,
3987 bool AllowTypeModifiers = true) {
3988 // Modifiers.
3989 int HowLong = 0;
3990 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003991
Chris Lattner86df27b2009-06-14 00:45:47 +00003992 // Read the modifiers first.
3993 bool Done = false;
3994 while (!Done) {
3995 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00003996 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00003997 case 'S':
3998 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
3999 assert(!Signed && "Can't use 'S' modifier multiple times!");
4000 Signed = true;
4001 break;
4002 case 'U':
4003 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4004 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4005 Unsigned = true;
4006 break;
4007 case 'L':
4008 assert(HowLong <= 2 && "Can't have LLLL modifier");
4009 ++HowLong;
4010 break;
4011 }
4012 }
4013
4014 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004015
Chris Lattner86df27b2009-06-14 00:45:47 +00004016 // Read the base type.
4017 switch (*Str++) {
4018 default: assert(0 && "Unknown builtin type letter!");
4019 case 'v':
4020 assert(HowLong == 0 && !Signed && !Unsigned &&
4021 "Bad modifiers used with 'v'!");
4022 Type = Context.VoidTy;
4023 break;
4024 case 'f':
4025 assert(HowLong == 0 && !Signed && !Unsigned &&
4026 "Bad modifiers used with 'f'!");
4027 Type = Context.FloatTy;
4028 break;
4029 case 'd':
4030 assert(HowLong < 2 && !Signed && !Unsigned &&
4031 "Bad modifiers used with 'd'!");
4032 if (HowLong)
4033 Type = Context.LongDoubleTy;
4034 else
4035 Type = Context.DoubleTy;
4036 break;
4037 case 's':
4038 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4039 if (Unsigned)
4040 Type = Context.UnsignedShortTy;
4041 else
4042 Type = Context.ShortTy;
4043 break;
4044 case 'i':
4045 if (HowLong == 3)
4046 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4047 else if (HowLong == 2)
4048 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4049 else if (HowLong == 1)
4050 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4051 else
4052 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4053 break;
4054 case 'c':
4055 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4056 if (Signed)
4057 Type = Context.SignedCharTy;
4058 else if (Unsigned)
4059 Type = Context.UnsignedCharTy;
4060 else
4061 Type = Context.CharTy;
4062 break;
4063 case 'b': // boolean
4064 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4065 Type = Context.BoolTy;
4066 break;
4067 case 'z': // size_t.
4068 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4069 Type = Context.getSizeType();
4070 break;
4071 case 'F':
4072 Type = Context.getCFConstantStringType();
4073 break;
4074 case 'a':
4075 Type = Context.getBuiltinVaListType();
4076 assert(!Type.isNull() && "builtin va list type not initialized!");
4077 break;
4078 case 'A':
4079 // This is a "reference" to a va_list; however, what exactly
4080 // this means depends on how va_list is defined. There are two
4081 // different kinds of va_list: ones passed by value, and ones
4082 // passed by reference. An example of a by-value va_list is
4083 // x86, where va_list is a char*. An example of by-ref va_list
4084 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4085 // we want this argument to be a char*&; for x86-64, we want
4086 // it to be a __va_list_tag*.
4087 Type = Context.getBuiltinVaListType();
4088 assert(!Type.isNull() && "builtin va list type not initialized!");
4089 if (Type->isArrayType()) {
4090 Type = Context.getArrayDecayedType(Type);
4091 } else {
4092 Type = Context.getLValueReferenceType(Type);
4093 }
4094 break;
4095 case 'V': {
4096 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004097 unsigned NumElements = strtoul(Str, &End, 10);
4098 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004099
Chris Lattner86df27b2009-06-14 00:45:47 +00004100 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004101
Chris Lattner86df27b2009-06-14 00:45:47 +00004102 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4103 Type = Context.getVectorType(ElementType, NumElements);
4104 break;
4105 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00004106 case 'X': {
4107 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4108 Type = Context.getComplexType(ElementType);
4109 break;
4110 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004111 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004112 Type = Context.getFILEType();
4113 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004114 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004115 return QualType();
4116 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004117 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004118 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004119 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004120 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004121 else
4122 Type = Context.getjmp_bufType();
4123
Mike Stumpfd612db2009-07-28 23:47:15 +00004124 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004125 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004126 return QualType();
4127 }
4128 break;
Mike Stump782fa302009-07-28 02:25:19 +00004129 }
Mike Stump1eb44332009-09-09 15:08:12 +00004130
Chris Lattner86df27b2009-06-14 00:45:47 +00004131 if (!AllowTypeModifiers)
4132 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004133
Chris Lattner86df27b2009-06-14 00:45:47 +00004134 Done = false;
4135 while (!Done) {
4136 switch (*Str++) {
4137 default: Done = true; --Str; break;
4138 case '*':
4139 Type = Context.getPointerType(Type);
4140 break;
4141 case '&':
4142 Type = Context.getLValueReferenceType(Type);
4143 break;
4144 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4145 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004146 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004147 break;
4148 }
4149 }
Mike Stump1eb44332009-09-09 15:08:12 +00004150
Chris Lattner86df27b2009-06-14 00:45:47 +00004151 return Type;
4152}
4153
4154/// GetBuiltinType - Return the type for the specified builtin.
4155QualType ASTContext::GetBuiltinType(unsigned id,
4156 GetBuiltinTypeError &Error) {
4157 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004158
Chris Lattner86df27b2009-06-14 00:45:47 +00004159 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004160
Chris Lattner86df27b2009-06-14 00:45:47 +00004161 Error = GE_None;
4162 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4163 if (Error != GE_None)
4164 return QualType();
4165 while (TypeStr[0] && TypeStr[0] != '.') {
4166 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4167 if (Error != GE_None)
4168 return QualType();
4169
4170 // Do array -> pointer decay. The builtin should use the decayed type.
4171 if (Ty->isArrayType())
4172 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004173
Chris Lattner86df27b2009-06-14 00:45:47 +00004174 ArgTypes.push_back(Ty);
4175 }
4176
4177 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4178 "'.' should only occur at end of builtin type list!");
4179
4180 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4181 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4182 return getFunctionNoProtoType(ResType);
4183 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4184 TypeStr[0] == '.', 0);
4185}
Eli Friedmana95d7572009-08-19 07:44:53 +00004186
4187QualType
4188ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4189 // Perform the usual unary conversions. We do this early so that
4190 // integral promotions to "int" can allow us to exit early, in the
4191 // lhs == rhs check. Also, for conversion purposes, we ignore any
4192 // qualifiers. For example, "const float" and "float" are
4193 // equivalent.
4194 if (lhs->isPromotableIntegerType())
4195 lhs = getPromotedIntegerType(lhs);
4196 else
4197 lhs = lhs.getUnqualifiedType();
4198 if (rhs->isPromotableIntegerType())
4199 rhs = getPromotedIntegerType(rhs);
4200 else
4201 rhs = rhs.getUnqualifiedType();
4202
4203 // If both types are identical, no conversion is needed.
4204 if (lhs == rhs)
4205 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004206
Eli Friedmana95d7572009-08-19 07:44:53 +00004207 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4208 // The caller can deal with this (e.g. pointer + int).
4209 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4210 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004211
4212 // At this point, we have two different arithmetic types.
4213
Eli Friedmana95d7572009-08-19 07:44:53 +00004214 // Handle complex types first (C99 6.3.1.8p1).
4215 if (lhs->isComplexType() || rhs->isComplexType()) {
4216 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004217 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004218 // convert the rhs to the lhs complex type.
4219 return lhs;
4220 }
Mike Stump1eb44332009-09-09 15:08:12 +00004221 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004222 // convert the lhs to the rhs complex type.
4223 return rhs;
4224 }
4225 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004226 // When both operands are complex, the shorter operand is converted to the
4227 // type of the longer, and that is the type of the result. This corresponds
4228 // to what is done when combining two real floating-point operands.
4229 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004230 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004231 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004232 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004233 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004234 // "double _Complex" is promoted to "long double _Complex".
4235 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004236
4237 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004238 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004239 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004240 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004241 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004242 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4243 // domains match. This is a requirement for our implementation, C99
4244 // does not require this promotion.
4245 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4246 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4247 return rhs;
4248 } else { // handle "_Complex double, double".
4249 return lhs;
4250 }
4251 }
4252 return lhs; // The domain/size match exactly.
4253 }
4254 // Now handle "real" floating types (i.e. float, double, long double).
4255 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4256 // if we have an integer operand, the result is the real floating type.
4257 if (rhs->isIntegerType()) {
4258 // convert rhs to the lhs floating point type.
4259 return lhs;
4260 }
4261 if (rhs->isComplexIntegerType()) {
4262 // convert rhs to the complex floating point type.
4263 return getComplexType(lhs);
4264 }
4265 if (lhs->isIntegerType()) {
4266 // convert lhs to the rhs floating point type.
4267 return rhs;
4268 }
Mike Stump1eb44332009-09-09 15:08:12 +00004269 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004270 // convert lhs to the complex floating point type.
4271 return getComplexType(rhs);
4272 }
4273 // We have two real floating types, float/complex combos were handled above.
4274 // Convert the smaller operand to the bigger result.
4275 int result = getFloatingTypeOrder(lhs, rhs);
4276 if (result > 0) // convert the rhs
4277 return lhs;
4278 assert(result < 0 && "illegal float comparison");
4279 return rhs; // convert the lhs
4280 }
4281 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4282 // Handle GCC complex int extension.
4283 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4284 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4285
4286 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004287 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004288 rhsComplexInt->getElementType()) >= 0)
4289 return lhs; // convert the rhs
4290 return rhs;
4291 } else if (lhsComplexInt && rhs->isIntegerType()) {
4292 // convert the rhs to the lhs complex type.
4293 return lhs;
4294 } else if (rhsComplexInt && lhs->isIntegerType()) {
4295 // convert the lhs to the rhs complex type.
4296 return rhs;
4297 }
4298 }
4299 // Finally, we have two differing integer types.
4300 // The rules for this case are in C99 6.3.1.8
4301 int compare = getIntegerTypeOrder(lhs, rhs);
4302 bool lhsSigned = lhs->isSignedIntegerType(),
4303 rhsSigned = rhs->isSignedIntegerType();
4304 QualType destType;
4305 if (lhsSigned == rhsSigned) {
4306 // Same signedness; use the higher-ranked type
4307 destType = compare >= 0 ? lhs : rhs;
4308 } else if (compare != (lhsSigned ? 1 : -1)) {
4309 // The unsigned type has greater than or equal rank to the
4310 // signed type, so use the unsigned type
4311 destType = lhsSigned ? rhs : lhs;
4312 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4313 // The two types are different widths; if we are here, that
4314 // means the signed type is larger than the unsigned type, so
4315 // use the signed type.
4316 destType = lhsSigned ? lhs : rhs;
4317 } else {
4318 // The signed type is higher-ranked than the unsigned type,
4319 // but isn't actually any bigger (like unsigned int and long
4320 // on most 32-bit systems). Use the unsigned type corresponding
4321 // to the signed type.
4322 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4323 }
4324 return destType;
4325}