blob: 7f5c9b1ec1e6b947f230eff111bc33681153aee1 [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"
Ken Dyckbdc601b2009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/ExternalASTSource.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000024#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000026#include "llvm/ADT/SmallString.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000027#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000028#include "llvm/Support/MathExtras.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000029#include "llvm/Support/raw_ostream.h"
Anders Carlsson29445a02009-07-18 21:19:52 +000030#include "RecordLayoutBuilder.h"
31
Reid Spencer5f016e22007-07-11 17:01:13 +000032using namespace clang;
33
34enum FloatingRank {
35 FloatRank, DoubleRank, LongDoubleRank
36};
37
Chris Lattner61710852008-10-05 17:34:18 +000038ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar444be732009-11-13 05:51:54 +000039 const TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000040 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +000041 Builtin::Context &builtins,
Mike Stump1eb44332009-09-09 15:08:12 +000042 bool FreeMem, unsigned size_reserve) :
43 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +000044 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump083c25e2009-10-22 00:49:09 +000045 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
Douglas Gregorc6fbbed2010-03-19 22:13:20 +000046 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +000047 Idents(idents), Selectors(sels),
Mike Stump1eb44332009-09-09 15:08:12 +000048 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
David Chisnall0f436562009-08-17 16:35:33 +000049 ObjCIdRedefinitionType = QualType();
50 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +000051 ObjCSelRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +000052 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +000053 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +000054 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000055}
56
Reid Spencer5f016e22007-07-11 17:01:13 +000057ASTContext::~ASTContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +000058 // Release the DenseMaps associated with DeclContext objects.
59 // FIXME: Is this the ideal solution?
60 ReleaseDeclContextMaps();
Douglas Gregor7d10b7e2010-03-02 23:58:15 +000061
62 // Release all of the memory associated with overridden C++ methods.
63 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
64 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
65 OM != OMEnd; ++OM)
66 OM->second.Destroy();
Ted Kremenek3478eb62010-02-11 07:12:28 +000067
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000068 if (FreeMemory) {
69 // Deallocate all the types.
70 while (!Types.empty()) {
71 Types.back()->Destroy(*this);
72 Types.pop_back();
73 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000074
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000075 for (llvm::FoldingSet<ExtQuals>::iterator
76 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
77 // Increment in loop to prevent using deallocated memory.
John McCall0953e762009-09-24 19:53:00 +000078 Deallocate(&*I++);
Nuno Lopesb74668e2008-12-17 22:30:25 +000079 }
Nuno Lopesb74668e2008-12-17 22:30:25 +000080
Ted Kremenek503524a2010-03-08 20:56:29 +000081 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
82 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
83 // Increment in loop to prevent using deallocated memory.
84 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
85 R->Destroy(*this);
86 }
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000087
Ted Kremenek503524a2010-03-08 20:56:29 +000088 for (llvm::DenseMap<const ObjCContainerDecl*,
89 const ASTRecordLayout*>::iterator
90 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
91 // Increment in loop to prevent using deallocated memory.
92 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
93 R->Destroy(*this);
94 }
Nuno Lopesb74668e2008-12-17 22:30:25 +000095 }
96
Douglas Gregorab452ba2009-03-26 23:50:42 +000097 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000098 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
99 NNS = NestedNameSpecifiers.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000100 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenekbbfd68d2009-12-23 21:13:52 +0000101 NNS != NNSEnd; ) {
102 // Increment in loop to prevent using deallocated memory.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +0000103 (*NNS++).Destroy(*this);
Ted Kremenekbbfd68d2009-12-23 21:13:52 +0000104 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000105
106 if (GlobalNestedNameSpecifier)
107 GlobalNestedNameSpecifier->Destroy(*this);
108
Eli Friedmanb26153c2008-05-27 03:08:09 +0000109 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000110}
111
Mike Stump1eb44332009-09-09 15:08:12 +0000112void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000113ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
114 ExternalSource.reset(Source.take());
115}
116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117void ASTContext::PrintStats() const {
118 fprintf(stderr, "*** AST Context Stats:\n");
119 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000120
Douglas Gregordbe833d2009-05-26 14:40:08 +0000121 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000122#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000123#define ABSTRACT_TYPE(Name, Parent)
124#include "clang/AST/TypeNodes.def"
125 0 // Extra
126 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000127
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
129 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000130 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 }
132
Douglas Gregordbe833d2009-05-26 14:40:08 +0000133 unsigned Idx = 0;
134 unsigned TotalBytes = 0;
135#define TYPE(Name, Parent) \
136 if (counts[Idx]) \
137 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
138 TotalBytes += counts[Idx] * sizeof(Name##Type); \
139 ++Idx;
140#define ABSTRACT_TYPE(Name, Parent)
141#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Douglas Gregordbe833d2009-05-26 14:40:08 +0000143 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144
145 if (ExternalSource.get()) {
146 fprintf(stderr, "\n");
147 ExternalSource->PrintStats();
148 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000149}
150
151
John McCalle27ec8a2009-10-23 23:03:21 +0000152void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000153 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCalle27ec8a2009-10-23 23:03:21 +0000154 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall6b304a02009-09-24 23:30:46 +0000155 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156}
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158void ASTContext::InitBuiltinTypes() {
159 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 // C99 6.2.5p19.
162 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 // C99 6.2.5p2.
165 InitBuiltinType(BoolTy, BuiltinType::Bool);
166 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000167 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 InitBuiltinType(CharTy, BuiltinType::Char_S);
169 else
170 InitBuiltinType(CharTy, BuiltinType::Char_U);
171 // C99 6.2.5p4.
172 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
173 InitBuiltinType(ShortTy, BuiltinType::Short);
174 InitBuiltinType(IntTy, BuiltinType::Int);
175 InitBuiltinType(LongTy, BuiltinType::Long);
176 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 // C99 6.2.5p6.
179 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
180 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
181 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
182 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
183 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 // C99 6.2.5p10.
186 InitBuiltinType(FloatTy, BuiltinType::Float);
187 InitBuiltinType(DoubleTy, BuiltinType::Double);
188 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000189
Chris Lattner2df9ced2009-04-30 02:43:43 +0000190 // GNU extension, 128-bit integers.
191 InitBuiltinType(Int128Ty, BuiltinType::Int128);
192 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
193
Chris Lattner3a250322009-02-26 23:43:47 +0000194 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
195 InitBuiltinType(WCharTy, BuiltinType::WChar);
196 else // C99
197 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000198
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000199 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
200 InitBuiltinType(Char16Ty, BuiltinType::Char16);
201 else // C99
202 Char16Ty = getFromTargetType(Target.getChar16Type());
203
204 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
205 InitBuiltinType(Char32Ty, BuiltinType::Char32);
206 else // C99
207 Char32Ty = getFromTargetType(Target.getChar32Type());
208
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000209 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000210 InitBuiltinType(OverloadTy, BuiltinType::Overload);
211
212 // Placeholder type for type-dependent expressions whose type is
213 // completely unknown. No code should ever check a type against
214 // DependentTy and users should never see it; however, it is here to
215 // help diagnose failures to properly check for type-dependent
216 // expressions.
217 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000218
Mike Stump1eb44332009-09-09 15:08:12 +0000219 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000220 // not yet been deduced.
221 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 // C99 6.2.5p11.
224 FloatComplexTy = getComplexType(FloatTy);
225 DoubleComplexTy = getComplexType(DoubleTy);
226 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000227
Steve Naroff7e219e42007-10-15 14:41:52 +0000228 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Steve Naroffde2e22d2009-07-15 18:40:39 +0000230 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
231 ObjCIdTypedefType = QualType();
232 ObjCClassTypedefType = QualType();
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000233 ObjCSelTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000235 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroffde2e22d2009-07-15 18:40:39 +0000236 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
237 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000238 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff14108da2009-07-10 23:34:53 +0000239
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000240 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000242 // void * type
243 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000244
245 // nullptr type (C++0x 2.14.7)
246 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000247}
248
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000249MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000250ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000251 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000252 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000253 = InstantiatedFromStaticDataMember.find(Var);
254 if (Pos == InstantiatedFromStaticDataMember.end())
255 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Douglas Gregor7caa6822009-07-24 20:34:43 +0000257 return Pos->second;
258}
259
Mike Stump1eb44332009-09-09 15:08:12 +0000260void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000261ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
262 TemplateSpecializationKind TSK) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000263 assert(Inst->isStaticDataMember() && "Not a static data member");
264 assert(Tmpl->isStaticDataMember() && "Not a static data member");
265 assert(!InstantiatedFromStaticDataMember[Inst] &&
266 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000267 InstantiatedFromStaticDataMember[Inst]
268 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000269}
270
John McCall7ba107a2009-11-18 02:36:19 +0000271NamedDecl *
John McCalled976492009-12-04 22:46:56 +0000272ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCall7ba107a2009-11-18 02:36:19 +0000273 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCalled976492009-12-04 22:46:56 +0000274 = InstantiatedFromUsingDecl.find(UUD);
275 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson0d8df782009-08-29 19:37:28 +0000276 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Anders Carlsson0d8df782009-08-29 19:37:28 +0000278 return Pos->second;
279}
280
281void
John McCalled976492009-12-04 22:46:56 +0000282ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
283 assert((isa<UsingDecl>(Pattern) ||
284 isa<UnresolvedUsingValueDecl>(Pattern) ||
285 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
286 "pattern decl is not a using decl");
287 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
288 InstantiatedFromUsingDecl[Inst] = Pattern;
289}
290
291UsingShadowDecl *
292ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
293 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
294 = InstantiatedFromUsingShadowDecl.find(Inst);
295 if (Pos == InstantiatedFromUsingShadowDecl.end())
296 return 0;
297
298 return Pos->second;
299}
300
301void
302ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
303 UsingShadowDecl *Pattern) {
304 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
305 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +0000306}
307
Anders Carlssond8b285f2009-09-01 04:26:58 +0000308FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
309 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
310 = InstantiatedFromUnnamedFieldDecl.find(Field);
311 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
312 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Anders Carlssond8b285f2009-09-01 04:26:58 +0000314 return Pos->second;
315}
316
317void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
318 FieldDecl *Tmpl) {
319 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
320 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
321 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
322 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Anders Carlssond8b285f2009-09-01 04:26:58 +0000324 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
325}
326
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000327CXXMethodVector::iterator CXXMethodVector::begin() const {
328 if ((Storage & 0x01) == 0)
329 return reinterpret_cast<iterator>(&Storage);
330
331 vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
332 return &Vec->front();
333}
334
335CXXMethodVector::iterator CXXMethodVector::end() const {
336 if ((Storage & 0x01) == 0) {
337 if (Storage == 0)
338 return reinterpret_cast<iterator>(&Storage);
339
340 return reinterpret_cast<iterator>(&Storage) + 1;
341 }
342
343 vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
344 return &Vec->front() + Vec->size();
345}
346
347void CXXMethodVector::push_back(const CXXMethodDecl *Method) {
348 if (Storage == 0) {
349 // 0 -> 1 element.
350 Storage = reinterpret_cast<uintptr_t>(Method);
351 return;
352 }
353
354 vector_type *Vec;
355 if ((Storage & 0x01) == 0) {
356 // 1 -> 2 elements. Allocate a new vector and push the element into that
357 // vector.
358 Vec = new vector_type;
359 Vec->push_back(reinterpret_cast<const CXXMethodDecl *>(Storage));
360 Storage = reinterpret_cast<uintptr_t>(Vec) | 0x01;
361 } else
362 Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
363
364 // Add the new method to the vector.
365 Vec->push_back(Method);
366}
367
368void CXXMethodVector::Destroy() {
369 if (Storage & 0x01)
370 delete reinterpret_cast<vector_type *>(Storage & ~0x01);
371
372 Storage = 0;
373}
374
375
376ASTContext::overridden_cxx_method_iterator
377ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
378 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
379 = OverriddenMethods.find(Method);
380 if (Pos == OverriddenMethods.end())
381 return 0;
382
383 return Pos->second.begin();
384}
385
386ASTContext::overridden_cxx_method_iterator
387ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
388 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
389 = OverriddenMethods.find(Method);
390 if (Pos == OverriddenMethods.end())
391 return 0;
392
393 return Pos->second.end();
394}
395
396void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
397 const CXXMethodDecl *Overridden) {
398 OverriddenMethods[Method].push_back(Overridden);
399}
400
Douglas Gregor2e222532009-07-02 17:08:52 +0000401namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000402 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000403 : std::binary_function<SourceRange, SourceRange, bool> {
404 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Douglas Gregor2e222532009-07-02 17:08:52 +0000406 public:
407 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Douglas Gregor2e222532009-07-02 17:08:52 +0000409 bool operator()(SourceRange X, SourceRange Y) {
410 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
411 }
412 };
413}
414
Chris Lattner464175b2007-07-18 17:52:12 +0000415//===----------------------------------------------------------------------===//
416// Type Sizing and Analysis
417//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000418
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000419/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
420/// scalar floating point type.
421const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000422 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000423 assert(BT && "Not a floating point type!");
424 switch (BT->getKind()) {
425 default: assert(0 && "Not a floating point type!");
426 case BuiltinType::Float: return Target.getFloatFormat();
427 case BuiltinType::Double: return Target.getDoubleFormat();
428 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
429 }
430}
431
Ken Dyck8b752f12010-01-27 17:10:57 +0000432/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000433/// specified decl. Note that bitfields do not have a valid alignment, so
434/// this method will assert on them.
Sebastian Redl5d484e82009-11-23 17:18:46 +0000435/// If @p RefAsPointee, references are treated like their underlying type
436/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck8b752f12010-01-27 17:10:57 +0000437CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000438 unsigned Align = Target.getCharWidth();
439
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000440 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Sean Huntbbd37c62009-11-21 08:43:09 +0000441 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedmandcdafb62009-02-22 02:56:25 +0000442
Chris Lattneraf707ab2009-01-24 21:53:27 +0000443 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
444 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000445 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl5d484e82009-11-23 17:18:46 +0000446 if (RefAsPointee)
447 T = RT->getPointeeType();
448 else
449 T = getPointerType(RT->getPointeeType());
450 }
451 if (!T->isIncompleteType() && !T->isFunctionType()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000452 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000453 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
454 T = cast<ArrayType>(T)->getElementType();
455
456 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
457 }
Charles Davis05f62472010-02-23 04:52:00 +0000458 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
459 // In the case of a field in a packed struct, we want the minimum
460 // of the alignment of the field and the alignment of the struct.
461 Align = std::min(Align,
462 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
463 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000464 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000465
Ken Dyck8b752f12010-01-27 17:10:57 +0000466 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattneraf707ab2009-01-24 21:53:27 +0000467}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000468
Chris Lattnera7674d82007-07-13 22:13:22 +0000469/// getTypeSize - Return the size of the specified type, in bits. This method
470/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000471///
472/// FIXME: Pointers into different addr spaces could have different sizes and
473/// alignment requirements: getPointerInfo should take an AddrSpace, this
474/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000475std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000476ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000477 uint64_t Width=0;
478 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000479 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000480#define TYPE(Class, Base)
481#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000482#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000483#define DEPENDENT_TYPE(Class, Base) case Type::Class:
484#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000485 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000486 break;
487
Chris Lattner692233e2007-07-13 22:27:08 +0000488 case Type::FunctionNoProto:
489 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000490 // GCC extension: alignof(function) = 32 bits
491 Width = 0;
492 Align = 32;
493 break;
494
Douglas Gregor72564e72009-02-26 23:50:07 +0000495 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000496 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000497 Width = 0;
498 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
499 break;
500
Steve Narofffb22d962007-08-30 01:06:46 +0000501 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000502 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattner98be4942008-03-05 18:54:05 +0000504 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000505 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000506 Align = EltInfo.second;
507 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000508 }
Nate Begeman213541a2008-04-18 23:10:10 +0000509 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000510 case Type::Vector: {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000511 const VectorType *VT = cast<VectorType>(T);
512 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
513 Width = EltInfo.first*VT->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000514 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000515 // If the alignment is not a power of 2, round up to the next power of 2.
516 // This happens for non-power-of-2 length vectors.
Chris Lattner9fcfe922009-10-22 05:17:15 +0000517 if (VT->getNumElements() & (VT->getNumElements()-1)) {
518 Align = llvm::NextPowerOf2(Align);
519 Width = llvm::RoundUpToAlignment(Width, Align);
520 }
Chris Lattner030d8842007-07-19 22:06:24 +0000521 break;
522 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000523
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000524 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000525 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000526 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000527 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000528 // GCC extension: alignof(void) = 8 bits.
529 Width = 0;
530 Align = 8;
531 break;
532
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000533 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000534 Width = Target.getBoolWidth();
535 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000536 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000537 case BuiltinType::Char_S:
538 case BuiltinType::Char_U:
539 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000540 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000541 Width = Target.getCharWidth();
542 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000543 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000544 case BuiltinType::WChar:
545 Width = Target.getWCharWidth();
546 Align = Target.getWCharAlign();
547 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000548 case BuiltinType::Char16:
549 Width = Target.getChar16Width();
550 Align = Target.getChar16Align();
551 break;
552 case BuiltinType::Char32:
553 Width = Target.getChar32Width();
554 Align = Target.getChar32Align();
555 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000556 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000557 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000558 Width = Target.getShortWidth();
559 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000560 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000561 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000562 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000563 Width = Target.getIntWidth();
564 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000565 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000566 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000567 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000568 Width = Target.getLongWidth();
569 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000570 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000571 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000572 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000573 Width = Target.getLongLongWidth();
574 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000575 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000576 case BuiltinType::Int128:
577 case BuiltinType::UInt128:
578 Width = 128;
579 Align = 128; // int128_t is 128-bit aligned on all targets.
580 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000581 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000582 Width = Target.getFloatWidth();
583 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000584 break;
585 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000586 Width = Target.getDoubleWidth();
587 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000588 break;
589 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000590 Width = Target.getLongDoubleWidth();
591 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000592 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000593 case BuiltinType::NullPtr:
594 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
595 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000596 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000597 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000598 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000599 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000600 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000601 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000602 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000603 case Type::BlockPointer: {
604 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
605 Width = Target.getPointerWidth(AS);
606 Align = Target.getPointerAlign(AS);
607 break;
608 }
Sebastian Redl5d484e82009-11-23 17:18:46 +0000609 case Type::LValueReference:
610 case Type::RValueReference: {
611 // alignof and sizeof should never enter this code path here, so we go
612 // the pointer route.
613 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
614 Width = Target.getPointerWidth(AS);
615 Align = Target.getPointerAlign(AS);
616 break;
617 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000618 case Type::Pointer: {
619 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000620 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000621 Align = Target.getPointerAlign(AS);
622 break;
623 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000624 case Type::MemberPointer: {
Sebastian Redlf30208a2009-01-24 21:16:55 +0000625 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000626 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000627 getTypeInfo(getPointerDiffType());
628 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000629 if (Pointee->isFunctionType())
630 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000631 Align = PtrDiffInfo.second;
632 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000633 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000634 case Type::Complex: {
635 // Complex types have the same alignment as their elements, but twice the
636 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000637 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000638 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000639 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000640 Align = EltInfo.second;
641 break;
642 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000643 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000644 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000645 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
646 Width = Layout.getSize();
647 Align = Layout.getAlignment();
648 break;
649 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000650 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000651 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000652 const TagType *TT = cast<TagType>(T);
653
654 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000655 Width = 1;
656 Align = 1;
657 break;
658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Daniel Dunbar1d751182008-11-08 05:48:37 +0000660 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000661 return getTypeInfo(ET->getDecl()->getIntegerType());
662
Daniel Dunbar1d751182008-11-08 05:48:37 +0000663 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000664 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
665 Width = Layout.getSize();
666 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000667 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000668 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000669
Chris Lattner9fcfe922009-10-22 05:17:15 +0000670 case Type::SubstTemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000671 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
672 getReplacementType().getTypePtr());
John McCall49a832b2009-10-18 09:09:24 +0000673
Chris Lattner9fcfe922009-10-22 05:17:15 +0000674 case Type::Elaborated:
675 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType()
676 .getTypePtr());
John McCall7da24312009-09-05 00:15:47 +0000677
Douglas Gregor18857642009-04-30 17:32:17 +0000678 case Type::Typedef: {
679 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000680 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000681 Align = std::max(Aligned->getMaxAlignment(),
682 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregor18857642009-04-30 17:32:17 +0000683 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
684 } else
685 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000686 break;
Chris Lattner71763312008-04-06 22:05:18 +0000687 }
Douglas Gregor18857642009-04-30 17:32:17 +0000688
689 case Type::TypeOfExpr:
690 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
691 .getTypePtr());
692
693 case Type::TypeOf:
694 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
695
Anders Carlsson395b4752009-06-24 19:06:50 +0000696 case Type::Decltype:
697 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
698 .getTypePtr());
699
Douglas Gregor18857642009-04-30 17:32:17 +0000700 case Type::QualifiedName:
701 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000702
John McCall3cb0ebd2010-03-10 03:28:59 +0000703 case Type::InjectedClassName:
704 return getTypeInfo(cast<InjectedClassNameType>(T)
705 ->getUnderlyingType().getTypePtr());
706
Douglas Gregor18857642009-04-30 17:32:17 +0000707 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000708 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000709 "Cannot request the size of a dependent type");
710 // FIXME: this is likely to be wrong once we support template
711 // aliases, since a template alias could refer to a typedef that
712 // has an __aligned__ attribute on it.
713 return getTypeInfo(getCanonicalType(T));
714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattner464175b2007-07-18 17:52:12 +0000716 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000717 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000718}
719
Ken Dyckbdc601b2009-12-22 14:23:30 +0000720/// getTypeSizeInChars - Return the size of the specified type, in characters.
721/// This method does not work on incomplete types.
722CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000723 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000724}
725CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000726 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000727}
728
Ken Dyck16e20cc2010-01-26 17:25:18 +0000729/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck86fa4312010-01-26 17:22:55 +0000730/// characters. This method does not work on incomplete types.
731CharUnits ASTContext::getTypeAlignInChars(QualType T) {
732 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
733}
734CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
735 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
736}
737
Chris Lattner34ebde42009-01-27 18:08:34 +0000738/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
739/// type for the current target in bits. This can be different than the ABI
740/// alignment in cases where it is beneficial for performance to overalign
741/// a data type.
742unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
743 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000744
745 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000746 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000747 T = CT->getElementType().getTypePtr();
748 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
749 T->isSpecificBuiltinType(BuiltinType::LongLong))
750 return std::max(ABIAlign, (unsigned)getTypeSize(T));
751
Chris Lattner34ebde42009-01-27 18:08:34 +0000752 return ABIAlign;
753}
754
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000755static void CollectLocalObjCIvars(ASTContext *Ctx,
756 const ObjCInterfaceDecl *OI,
757 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000758 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
759 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000760 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000761 if (!IVDecl->isInvalidDecl())
762 Fields.push_back(cast<FieldDecl>(IVDecl));
763 }
764}
765
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000766void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
767 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
768 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
769 CollectObjCIvars(SuperClass, Fields);
770 CollectLocalObjCIvars(this, OI, Fields);
771}
772
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000773/// ShallowCollectObjCIvars -
774/// Collect all ivars, including those synthesized, in the current class.
775///
776void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000777 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000778 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
779 E = OI->ivar_end(); I != E; ++I) {
780 Ivars.push_back(*I);
781 }
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000782
783 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000784}
785
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000786/// CollectNonClassIvars -
787/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek7d2aa112010-03-11 19:44:54 +0000788/// This includes synthesized ivars (via @synthesize) and those in
789// class's @implementation.
Fariborz Jahanian98200742009-05-12 18:14:29 +0000790///
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000791void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian98200742009-05-12 18:14:29 +0000792 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000793 // Find ivars declared in class extension.
794 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
795 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
796 E = CDecl->ivar_end(); I != E; ++I) {
797 Ivars.push_back(*I);
798 }
799 }
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000800
Ted Kremenek7d2aa112010-03-11 19:44:54 +0000801 // Also add any ivar defined in this class's implementation. This
802 // includes synthesized ivars.
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000803 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
804 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
805 E = ImplDecl->ivar_end(); I != E; ++I)
806 Ivars.push_back(*I);
807 }
Fariborz Jahanian98200742009-05-12 18:14:29 +0000808}
809
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000810/// CollectInheritedProtocols - Collect all protocols in current class and
811/// those inherited by it.
812void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000813 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000814 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
815 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
816 PE = OI->protocol_end(); P != PE; ++P) {
817 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000818 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000819 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000820 PE = Proto->protocol_end(); P != PE; ++P) {
821 Protocols.insert(*P);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000822 CollectInheritedProtocols(*P, Protocols);
823 }
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000824 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000825
826 // Categories of this Interface.
827 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
828 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
829 CollectInheritedProtocols(CDeclChain, Protocols);
830 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
831 while (SD) {
832 CollectInheritedProtocols(SD, Protocols);
833 SD = SD->getSuperClass();
834 }
835 return;
836 }
837 if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
838 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
839 PE = OC->protocol_end(); P != PE; ++P) {
840 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000841 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000842 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
843 PE = Proto->protocol_end(); P != PE; ++P)
844 CollectInheritedProtocols(*P, Protocols);
845 }
846 return;
847 }
848 if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
849 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
850 PE = OP->protocol_end(); P != PE; ++P) {
851 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000852 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000853 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
854 PE = Proto->protocol_end(); P != PE; ++P)
855 CollectInheritedProtocols(*P, Protocols);
856 }
857 return;
858 }
859}
860
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000861unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
862 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000863 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
864 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000865 if ((*I)->getPropertyIvarDecl())
866 ++count;
867
868 // Also look into nested protocols.
869 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
870 E = PD->protocol_end(); P != E; ++P)
871 count += CountProtocolSynthesizedIvars(*P);
872 return count;
873}
874
Mike Stump1eb44332009-09-09 15:08:12 +0000875unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000876 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000877 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
878 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000879 if ((*I)->getPropertyIvarDecl())
880 ++count;
881 }
882 // Also look into interface's protocol list for properties declared
883 // in the protocol and whose ivars are synthesized.
884 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
885 PE = OI->protocol_end(); P != PE; ++P) {
886 ObjCProtocolDecl *PD = (*P);
887 count += CountProtocolSynthesizedIvars(PD);
888 }
889 return count;
890}
891
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000892/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
893ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
894 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
895 I = ObjCImpls.find(D);
896 if (I != ObjCImpls.end())
897 return cast<ObjCImplementationDecl>(I->second);
898 return 0;
899}
900/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
901ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
902 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
903 I = ObjCImpls.find(D);
904 if (I != ObjCImpls.end())
905 return cast<ObjCCategoryImplDecl>(I->second);
906 return 0;
907}
908
909/// \brief Set the implementation of ObjCInterfaceDecl.
910void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
911 ObjCImplementationDecl *ImplD) {
912 assert(IFaceD && ImplD && "Passed null params");
913 ObjCImpls[IFaceD] = ImplD;
914}
915/// \brief Set the implementation of ObjCCategoryDecl.
916void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
917 ObjCCategoryImplDecl *ImplD) {
918 assert(CatD && ImplD && "Passed null params");
919 ObjCImpls[CatD] = ImplD;
920}
921
John McCalla93c9342009-12-07 02:54:59 +0000922/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000923///
John McCalla93c9342009-12-07 02:54:59 +0000924/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000925/// the TypeLoc wrappers.
926///
927/// \param T the type that will be the basis for type source info. This type
928/// should refer to how the declarator was written in source code, not to
929/// what type semantic analysis resolved the declarator to.
John McCalla93c9342009-12-07 02:54:59 +0000930TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall109de5e2009-10-21 00:23:54 +0000931 unsigned DataSize) {
932 if (!DataSize)
933 DataSize = TypeLoc::getFullDataSizeForType(T);
934 else
935 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCalla93c9342009-12-07 02:54:59 +0000936 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall109de5e2009-10-21 00:23:54 +0000937
John McCalla93c9342009-12-07 02:54:59 +0000938 TypeSourceInfo *TInfo =
939 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
940 new (TInfo) TypeSourceInfo(T);
941 return TInfo;
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000942}
943
John McCalla93c9342009-12-07 02:54:59 +0000944TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCalla4eb74d2009-10-23 21:14:09 +0000945 SourceLocation L) {
John McCalla93c9342009-12-07 02:54:59 +0000946 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCalla4eb74d2009-10-23 21:14:09 +0000947 DI->getTypeLoc().initialize(L);
948 return DI;
949}
950
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000951/// getInterfaceLayoutImpl - Get or compute information about the
952/// layout of the given interface.
953///
954/// \param Impl - If given, also include the layout of the interface's
955/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000956const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000957ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
958 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000959 assert(!D->isForwardDecl() && "Invalid interface decl!");
960
Devang Patel44a3dde2008-06-04 21:54:36 +0000961 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000962 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000963 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
964 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
965 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000966
Daniel Dunbar453addb2009-05-03 11:16:44 +0000967 // Add in synthesized ivar count if laying out an implementation.
968 if (Impl) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000969 unsigned SynthCount = CountSynthesizedIvars(D);
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) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000999 D = D->getDefinition();
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001000 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
Anders Carlssonf53df232009-12-07 04:35:11 +00001015const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor952b0172010-02-11 01:04:33 +00001016 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlssonf53df232009-12-07 04:35:11 +00001017 assert(RD && "Cannot get key function for forward declarations!");
1018
1019 const CXXMethodDecl *&Entry = KeyFunctions[RD];
1020 if (!Entry)
1021 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
1022 else
1023 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
1024 "Key function changed!");
1025
1026 return Entry;
1027}
1028
Chris Lattnera7674d82007-07-13 22:13:22 +00001029//===----------------------------------------------------------------------===//
1030// Type creation/memoization methods
1031//===----------------------------------------------------------------------===//
1032
John McCall0953e762009-09-24 19:53:00 +00001033QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1034 unsigned Fast = Quals.getFastQualifiers();
1035 Quals.removeFastQualifiers();
1036
1037 // Check if we've already instantiated this type.
1038 llvm::FoldingSetNodeID ID;
1039 ExtQuals::Profile(ID, TypeNode, Quals);
1040 void *InsertPos = 0;
1041 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1042 assert(EQ->getQualifiers() == Quals);
1043 QualType T = QualType(EQ, Fast);
1044 return T;
1045 }
1046
John McCall6b304a02009-09-24 23:30:46 +00001047 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001048 ExtQualNodes.InsertNode(New, InsertPos);
1049 QualType T = QualType(New, Fast);
1050 return T;
1051}
1052
1053QualType ASTContext::getVolatileType(QualType T) {
1054 QualType CanT = getCanonicalType(T);
1055 if (CanT.isVolatileQualified()) return T;
1056
1057 QualifierCollector Quals;
1058 const Type *TypeNode = Quals.strip(T);
1059 Quals.addVolatile();
1060
1061 return getExtQualType(TypeNode, Quals);
1062}
1063
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001064QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001065 QualType CanT = getCanonicalType(T);
1066 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001067 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001068
John McCall0953e762009-09-24 19:53:00 +00001069 // If we are composing extended qualifiers together, merge together
1070 // into one ExtQuals node.
1071 QualifierCollector Quals;
1072 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001073
John McCall0953e762009-09-24 19:53:00 +00001074 // If this type already has an address space specified, it cannot get
1075 // another one.
1076 assert(!Quals.hasAddressSpace() &&
1077 "Type cannot be in multiple addr spaces!");
1078 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
John McCall0953e762009-09-24 19:53:00 +00001080 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001081}
1082
Chris Lattnerb7d25532009-02-18 22:53:11 +00001083QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001084 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001085 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001086 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001087 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001089 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001090 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001091 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001092 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1093 return getPointerType(ResultType);
1094 }
1095 }
Mike Stump1eb44332009-09-09 15:08:12 +00001096
John McCall0953e762009-09-24 19:53:00 +00001097 // If we are composing extended qualifiers together, merge together
1098 // into one ExtQuals node.
1099 QualifierCollector Quals;
1100 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001101
John McCall0953e762009-09-24 19:53:00 +00001102 // If this type already has an ObjCGC specified, it cannot get
1103 // another one.
1104 assert(!Quals.hasObjCGCAttr() &&
1105 "Type cannot have multiple ObjCGCs!");
1106 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001107
John McCall0953e762009-09-24 19:53:00 +00001108 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001109}
Chris Lattnera7674d82007-07-13 22:13:22 +00001110
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001111static QualType getNoReturnCallConvType(ASTContext& Context, QualType T,
1112 bool AddNoReturn,
1113 CallingConv CallConv) {
John McCall0953e762009-09-24 19:53:00 +00001114 QualType ResultType;
Douglas Gregor43c79c22009-12-09 00:47:37 +00001115 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1116 QualType Pointee = Pointer->getPointeeType();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001117 ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn,
1118 CallConv);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001119 if (ResultType == Pointee)
1120 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001121
1122 ResultType = Context.getPointerType(ResultType);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001123 } else if (const BlockPointerType *BlockPointer
1124 = T->getAs<BlockPointerType>()) {
1125 QualType Pointee = BlockPointer->getPointeeType();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001126 ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn,
1127 CallConv);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001128 if (ResultType == Pointee)
1129 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001130
1131 ResultType = Context.getBlockPointerType(ResultType);
1132 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
1133 if (F->getNoReturnAttr() == AddNoReturn && F->getCallConv() == CallConv)
Douglas Gregor43c79c22009-12-09 00:47:37 +00001134 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001135
Douglas Gregor43c79c22009-12-09 00:47:37 +00001136 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001137 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
1138 AddNoReturn, CallConv);
John McCall0953e762009-09-24 19:53:00 +00001139 } else {
Douglas Gregor43c79c22009-12-09 00:47:37 +00001140 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall0953e762009-09-24 19:53:00 +00001141 ResultType
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001142 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1143 FPT->getNumArgs(), FPT->isVariadic(),
1144 FPT->getTypeQuals(),
1145 FPT->hasExceptionSpec(),
1146 FPT->hasAnyExceptionSpec(),
1147 FPT->getNumExceptions(),
1148 FPT->exception_begin(),
1149 AddNoReturn, CallConv);
John McCall0953e762009-09-24 19:53:00 +00001150 }
Douglas Gregor43c79c22009-12-09 00:47:37 +00001151 } else
1152 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001153
1154 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1155}
1156
1157QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
1158 return getNoReturnCallConvType(*this, T, AddNoReturn, T.getCallConv());
1159}
1160
1161QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
1162 return getNoReturnCallConvType(*this, T, T.getNoReturnAttr(), CallConv);
Mike Stump24556362009-07-25 21:26:53 +00001163}
1164
Reid Spencer5f016e22007-07-11 17:01:13 +00001165/// getComplexType - Return the uniqued reference to the type for a complex
1166/// number with the specified element type.
1167QualType ASTContext::getComplexType(QualType T) {
1168 // Unique pointers, to guarantee there is only one pointer of a particular
1169 // structure.
1170 llvm::FoldingSetNodeID ID;
1171 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 void *InsertPos = 0;
1174 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1175 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 // If the pointee type isn't canonical, this won't be a canonical type either,
1178 // so fill in the canonical type field.
1179 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001180 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001181 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 // Get the new insert position for the node we care about.
1184 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001185 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 }
John McCall6b304a02009-09-24 23:30:46 +00001187 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 Types.push_back(New);
1189 ComplexTypes.InsertNode(New, InsertPos);
1190 return QualType(New, 0);
1191}
1192
Reid Spencer5f016e22007-07-11 17:01:13 +00001193/// getPointerType - Return the uniqued reference to the type for a pointer to
1194/// the specified type.
1195QualType ASTContext::getPointerType(QualType T) {
1196 // Unique pointers, to guarantee there is only one pointer of a particular
1197 // structure.
1198 llvm::FoldingSetNodeID ID;
1199 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 void *InsertPos = 0;
1202 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1203 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 // If the pointee type isn't canonical, this won't be a canonical type either,
1206 // so fill in the canonical type field.
1207 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001208 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001209 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 // Get the new insert position for the node we care about.
1212 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001213 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 }
John McCall6b304a02009-09-24 23:30:46 +00001215 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 Types.push_back(New);
1217 PointerTypes.InsertNode(New, InsertPos);
1218 return QualType(New, 0);
1219}
1220
Mike Stump1eb44332009-09-09 15:08:12 +00001221/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001222/// a pointer to the specified block.
1223QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001224 assert(T->isFunctionType() && "block of function types only");
1225 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001226 // structure.
1227 llvm::FoldingSetNodeID ID;
1228 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Steve Naroff5618bd42008-08-27 16:04:49 +00001230 void *InsertPos = 0;
1231 if (BlockPointerType *PT =
1232 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1233 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001234
1235 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001236 // type either so fill in the canonical type field.
1237 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001238 if (!T.isCanonical()) {
Steve Naroff5618bd42008-08-27 16:04:49 +00001239 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Steve Naroff5618bd42008-08-27 16:04:49 +00001241 // Get the new insert position for the node we care about.
1242 BlockPointerType *NewIP =
1243 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001244 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001245 }
John McCall6b304a02009-09-24 23:30:46 +00001246 BlockPointerType *New
1247 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001248 Types.push_back(New);
1249 BlockPointerTypes.InsertNode(New, InsertPos);
1250 return QualType(New, 0);
1251}
1252
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001253/// getLValueReferenceType - Return the uniqued reference to the type for an
1254/// lvalue reference to the specified type.
John McCall54e14c42009-10-22 22:37:11 +00001255QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 // Unique pointers, to guarantee there is only one pointer of a particular
1257 // structure.
1258 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001259 ReferenceType::Profile(ID, T, SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001260
1261 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001262 if (LValueReferenceType *RT =
1263 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001265
John McCall54e14c42009-10-22 22:37:11 +00001266 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1267
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 // 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;
John McCall54e14c42009-10-22 22:37:11 +00001271 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1272 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1273 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001274
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001276 LValueReferenceType *NewIP =
1277 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001278 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 }
1280
John McCall6b304a02009-09-24 23:30:46 +00001281 LValueReferenceType *New
John McCall54e14c42009-10-22 22:37:11 +00001282 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1283 SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001284 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001285 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCall54e14c42009-10-22 22:37:11 +00001286
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001287 return QualType(New, 0);
1288}
1289
1290/// getRValueReferenceType - Return the uniqued reference to the type for an
1291/// rvalue reference to the specified type.
1292QualType ASTContext::getRValueReferenceType(QualType T) {
1293 // Unique pointers, to guarantee there is only one pointer of a particular
1294 // structure.
1295 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001296 ReferenceType::Profile(ID, T, false);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001297
1298 void *InsertPos = 0;
1299 if (RValueReferenceType *RT =
1300 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1301 return QualType(RT, 0);
1302
John McCall54e14c42009-10-22 22:37:11 +00001303 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1304
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001305 // If the referencee type isn't canonical, this won't be a canonical type
1306 // either, so fill in the canonical type field.
1307 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001308 if (InnerRef || !T.isCanonical()) {
1309 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1310 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001311
1312 // Get the new insert position for the node we care about.
1313 RValueReferenceType *NewIP =
1314 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1315 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1316 }
1317
John McCall6b304a02009-09-24 23:30:46 +00001318 RValueReferenceType *New
1319 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001320 Types.push_back(New);
1321 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 return QualType(New, 0);
1323}
1324
Sebastian Redlf30208a2009-01-24 21:16:55 +00001325/// getMemberPointerType - Return the uniqued reference to the type for a
1326/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001327QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001328 // Unique pointers, to guarantee there is only one pointer of a particular
1329 // structure.
1330 llvm::FoldingSetNodeID ID;
1331 MemberPointerType::Profile(ID, T, Cls);
1332
1333 void *InsertPos = 0;
1334 if (MemberPointerType *PT =
1335 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1336 return QualType(PT, 0);
1337
1338 // If the pointee or class type isn't canonical, this won't be a canonical
1339 // type either, so fill in the canonical type field.
1340 QualType Canonical;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001341 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001342 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1343
1344 // Get the new insert position for the node we care about.
1345 MemberPointerType *NewIP =
1346 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1347 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1348 }
John McCall6b304a02009-09-24 23:30:46 +00001349 MemberPointerType *New
1350 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001351 Types.push_back(New);
1352 MemberPointerTypes.InsertNode(New, InsertPos);
1353 return QualType(New, 0);
1354}
1355
Mike Stump1eb44332009-09-09 15:08:12 +00001356/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001357/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001358QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001359 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001360 ArrayType::ArraySizeModifier ASM,
1361 unsigned EltTypeQuals) {
Sebastian Redl923d56d2009-11-05 15:52:31 +00001362 assert((EltTy->isDependentType() ||
1363 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedman587cbdf2009-05-29 20:17:55 +00001364 "Constant array of VLAs is illegal!");
1365
Chris Lattner38aeec72009-05-13 04:12:56 +00001366 // Convert the array size into a canonical width matching the pointer size for
1367 // the target.
1368 llvm::APInt ArySize(ArySizeIn);
1369 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001372 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001375 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001376 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 // If the element type isn't canonical, this won't be a canonical type either,
1380 // so fill in the canonical type field.
1381 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001382 if (!EltTy.isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001383 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001384 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001386 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001387 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001388 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 }
Mike Stump1eb44332009-09-09 15:08:12 +00001390
John McCall6b304a02009-09-24 23:30:46 +00001391 ConstantArrayType *New = new(*this,TypeAlignment)
1392 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001393 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 Types.push_back(New);
1395 return QualType(New, 0);
1396}
1397
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001398/// getVariableArrayType - Returns a non-unique reference to the type for a
1399/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001400QualType ASTContext::getVariableArrayType(QualType EltTy,
1401 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001402 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001403 unsigned EltTypeQuals,
1404 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001405 // Since we don't unique expressions, it isn't possible to unique VLA's
1406 // that have an expression provided for their size.
1407
John McCall6b304a02009-09-24 23:30:46 +00001408 VariableArrayType *New = new(*this, TypeAlignment)
1409 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001410
1411 VariableArrayTypes.push_back(New);
1412 Types.push_back(New);
1413 return QualType(New, 0);
1414}
1415
Douglas Gregor898574e2008-12-05 23:32:09 +00001416/// getDependentSizedArrayType - Returns a non-unique reference to
1417/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001418/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001419QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1420 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001421 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001422 unsigned EltTypeQuals,
1423 SourceRange Brackets) {
Douglas Gregorcb78d882009-11-19 18:03:26 +00001424 assert((!NumElts || NumElts->isTypeDependent() ||
1425 NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001426 "Size must be type- or value-dependent!");
1427
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001428 void *InsertPos = 0;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001429 DependentSizedArrayType *Canon = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00001430 llvm::FoldingSetNodeID ID;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001431
1432 if (NumElts) {
1433 // Dependently-sized array types that do not have a specified
1434 // number of elements will have their sizes deduced from an
1435 // initializer.
Douglas Gregorcb78d882009-11-19 18:03:26 +00001436 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1437 EltTypeQuals, NumElts);
1438
1439 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1440 }
1441
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001442 DependentSizedArrayType *New;
1443 if (Canon) {
1444 // We already have a canonical version of this array type; use it as
1445 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001446 New = new (*this, TypeAlignment)
1447 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1448 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001449 } else {
1450 QualType CanonEltTy = getCanonicalType(EltTy);
1451 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001452 New = new (*this, TypeAlignment)
1453 DependentSizedArrayType(*this, EltTy, QualType(),
1454 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001455
Douglas Gregor789b1f62010-02-04 18:10:26 +00001456 if (NumElts) {
1457 DependentSizedArrayType *CanonCheck
1458 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1459 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1460 (void)CanonCheck;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001461 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001462 }
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001463 } else {
1464 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1465 ASM, EltTypeQuals,
1466 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001467 New = new (*this, TypeAlignment)
1468 DependentSizedArrayType(*this, EltTy, Canon,
1469 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001470 }
1471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Douglas Gregor898574e2008-12-05 23:32:09 +00001473 Types.push_back(New);
1474 return QualType(New, 0);
1475}
1476
Eli Friedmanc5773c42008-02-15 18:16:39 +00001477QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1478 ArrayType::ArraySizeModifier ASM,
1479 unsigned EltTypeQuals) {
1480 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001481 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001482
1483 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001484 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001485 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1486 return QualType(ATP, 0);
1487
1488 // If the element type isn't canonical, this won't be a canonical type
1489 // either, so fill in the canonical type field.
1490 QualType Canonical;
1491
John McCall467b27b2009-10-22 20:10:53 +00001492 if (!EltTy.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001493 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001494 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001495
1496 // Get the new insert position for the node we care about.
1497 IncompleteArrayType *NewIP =
1498 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001499 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001500 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001501
John McCall6b304a02009-09-24 23:30:46 +00001502 IncompleteArrayType *New = new (*this, TypeAlignment)
1503 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001504
1505 IncompleteArrayTypes.InsertNode(New, InsertPos);
1506 Types.push_back(New);
1507 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001508}
1509
Steve Naroff73322922007-07-18 18:00:27 +00001510/// getVectorType - Return the unique reference to a vector type of
1511/// the specified element type and size. VectorType must be a built-in type.
John Thompson82287d12010-02-05 00:12:22 +00001512QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1513 bool IsAltiVec, bool IsPixel) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001514 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Chris Lattnerf52ab252008-04-06 22:59:24 +00001516 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001517 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 // Check if we've already instantiated a vector of this type.
1520 llvm::FoldingSetNodeID ID;
John Thompson82287d12010-02-05 00:12:22 +00001521 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1522 IsAltiVec, IsPixel);
Reid Spencer5f016e22007-07-11 17:01:13 +00001523 void *InsertPos = 0;
1524 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1525 return QualType(VTP, 0);
1526
1527 // If the element type isn't canonical, this won't be a canonical type either,
1528 // so fill in the canonical type field.
1529 QualType Canonical;
John Thompson82287d12010-02-05 00:12:22 +00001530 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1531 Canonical = getVectorType(getCanonicalType(vecType),
1532 NumElts, false, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 // Get the new insert position for the node we care about.
1535 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001536 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001537 }
John McCall6b304a02009-09-24 23:30:46 +00001538 VectorType *New = new (*this, TypeAlignment)
John Thompson82287d12010-02-05 00:12:22 +00001539 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 VectorTypes.InsertNode(New, InsertPos);
1541 Types.push_back(New);
1542 return QualType(New, 0);
1543}
1544
Nate Begeman213541a2008-04-18 23:10:10 +00001545/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001546/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001547QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001548 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Chris Lattnerf52ab252008-04-06 22:59:24 +00001550 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001551 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Steve Naroff73322922007-07-18 18:00:27 +00001553 // Check if we've already instantiated a vector of this type.
1554 llvm::FoldingSetNodeID ID;
John Thompson82287d12010-02-05 00:12:22 +00001555 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff73322922007-07-18 18:00:27 +00001556 void *InsertPos = 0;
1557 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1558 return QualType(VTP, 0);
1559
1560 // If the element type isn't canonical, this won't be a canonical type either,
1561 // so fill in the canonical type field.
1562 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001563 if (!vecType.isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001564 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Steve Naroff73322922007-07-18 18:00:27 +00001566 // Get the new insert position for the node we care about.
1567 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001568 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001569 }
John McCall6b304a02009-09-24 23:30:46 +00001570 ExtVectorType *New = new (*this, TypeAlignment)
1571 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001572 VectorTypes.InsertNode(New, InsertPos);
1573 Types.push_back(New);
1574 return QualType(New, 0);
1575}
1576
Mike Stump1eb44332009-09-09 15:08:12 +00001577QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001578 Expr *SizeExpr,
1579 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001580 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001581 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001582 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001584 void *InsertPos = 0;
1585 DependentSizedExtVectorType *Canon
1586 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1587 DependentSizedExtVectorType *New;
1588 if (Canon) {
1589 // We already have a canonical version of this array type; use it as
1590 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001591 New = new (*this, TypeAlignment)
1592 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1593 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001594 } else {
1595 QualType CanonVecTy = getCanonicalType(vecType);
1596 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001597 New = new (*this, TypeAlignment)
1598 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1599 AttrLoc);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001600
1601 DependentSizedExtVectorType *CanonCheck
1602 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1603 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1604 (void)CanonCheck;
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001605 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1606 } else {
1607 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1608 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001609 New = new (*this, TypeAlignment)
1610 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001611 }
1612 }
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001614 Types.push_back(New);
1615 return QualType(New, 0);
1616}
1617
Douglas Gregor72564e72009-02-26 23:50:07 +00001618/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001619///
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001620QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn,
1621 CallingConv CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 // Unique functions, to guarantee there is only one function of a particular
1623 // structure.
1624 llvm::FoldingSetNodeID ID;
John McCallf82b4e82010-02-04 05:44:44 +00001625 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn, CallConv);
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Reid Spencer5f016e22007-07-11 17:01:13 +00001627 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001628 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001629 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Reid Spencer5f016e22007-07-11 17:01:13 +00001632 QualType Canonical;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001633 if (!ResultTy.isCanonical() ||
John McCall04a67a62010-02-05 21:31:56 +00001634 getCanonicalCallConv(CallConv) != CallConv) {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001635 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn,
John McCall04a67a62010-02-05 21:31:56 +00001636 getCanonicalCallConv(CallConv));
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001639 FunctionNoProtoType *NewIP =
1640 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001641 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001642 }
Mike Stump1eb44332009-09-09 15:08:12 +00001643
John McCall6b304a02009-09-24 23:30:46 +00001644 FunctionNoProtoType *New = new (*this, TypeAlignment)
John McCallf82b4e82010-02-04 05:44:44 +00001645 FunctionNoProtoType(ResultTy, Canonical, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001646 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001647 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 return QualType(New, 0);
1649}
1650
1651/// getFunctionType - Return a normal function type with a typed argument
1652/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001653QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001654 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001655 unsigned TypeQuals, bool hasExceptionSpec,
1656 bool hasAnyExceptionSpec, unsigned NumExs,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001657 const QualType *ExArray, bool NoReturn,
1658 CallingConv CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001659 // Unique functions, to guarantee there is only one function of a particular
1660 // structure.
1661 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001662 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001663 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
John McCallf82b4e82010-02-04 05:44:44 +00001664 NumExs, ExArray, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001665
1666 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001667 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001668 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001670
1671 // Determine whether the type being created is already canonical or not.
John McCall54e14c42009-10-22 22:37:11 +00001672 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001674 if (!ArgArray[i].isCanonicalAsParam())
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 isCanonical = false;
1676
1677 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001678 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 QualType Canonical;
John McCall04a67a62010-02-05 21:31:56 +00001680 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 llvm::SmallVector<QualType, 16> CanonicalArgs;
1682 CanonicalArgs.reserve(NumArgs);
1683 for (unsigned i = 0; i != NumArgs; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001684 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001685
Chris Lattnerf52ab252008-04-06 22:59:24 +00001686 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001687 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001688 isVariadic, TypeQuals, false,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001689 false, 0, 0, NoReturn,
John McCall04a67a62010-02-05 21:31:56 +00001690 getCanonicalCallConv(CallConv));
Sebastian Redl465226e2009-05-27 22:11:52 +00001691
Reid Spencer5f016e22007-07-11 17:01:13 +00001692 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001693 FunctionProtoType *NewIP =
1694 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001695 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001697
Douglas Gregor72564e72009-02-26 23:50:07 +00001698 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001699 // for two variable size arrays (for parameter and exception types) at the
1700 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001701 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001702 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1703 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001704 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001705 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001706 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001707 ExArray, NumExs, Canonical, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001709 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 return QualType(FTP, 0);
1711}
1712
John McCall3cb0ebd2010-03-10 03:28:59 +00001713#ifndef NDEBUG
1714static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1715 if (!isa<CXXRecordDecl>(D)) return false;
1716 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1717 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1718 return true;
1719 if (RD->getDescribedClassTemplate() &&
1720 !isa<ClassTemplateSpecializationDecl>(RD))
1721 return true;
1722 return false;
1723}
1724#endif
1725
1726/// getInjectedClassNameType - Return the unique reference to the
1727/// injected class name type for the specified templated declaration.
1728QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1729 QualType TST) {
1730 assert(NeedsInjectedClassNameType(Decl));
1731 if (Decl->TypeForDecl) {
1732 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1733 } else if (CXXRecordDecl *PrevDecl
1734 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1735 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1736 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1737 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1738 } else {
1739 Decl->TypeForDecl = new (*this, TypeAlignment)
1740 InjectedClassNameType(Decl, TST, TST->getCanonicalTypeInternal());
1741 Types.push_back(Decl->TypeForDecl);
1742 }
1743 return QualType(Decl->TypeForDecl, 0);
1744}
1745
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001746/// getTypeDeclType - Return the unique reference to the type for the
1747/// specified type declaration.
John McCallbecb8d52010-03-10 06:48:02 +00001748QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001749 assert(Decl && "Passed null for Decl param");
John McCallbecb8d52010-03-10 06:48:02 +00001750 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump1eb44332009-09-09 15:08:12 +00001751
John McCall19c85762010-02-16 03:57:14 +00001752 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001753 return getTypedefType(Typedef);
John McCallbecb8d52010-03-10 06:48:02 +00001754
1755 if (const ObjCInterfaceDecl *ObjCInterface
Mike Stump9fdbab32009-07-31 02:02:20 +00001756 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001757 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001758
John McCallbecb8d52010-03-10 06:48:02 +00001759 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1760 "Template type parameter types are always available.");
1761
John McCall19c85762010-02-16 03:57:14 +00001762 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001763 assert(!Record->getPreviousDeclaration() &&
1764 "struct/union has previous declaration");
1765 assert(!NeedsInjectedClassNameType(Record));
1766 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall19c85762010-02-16 03:57:14 +00001767 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001768 assert(!Enum->getPreviousDeclaration() &&
1769 "enum has previous declaration");
1770 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall19c85762010-02-16 03:57:14 +00001771 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCalled976492009-12-04 22:46:56 +00001772 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1773 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stump9fdbab32009-07-31 02:02:20 +00001774 } else
John McCallbecb8d52010-03-10 06:48:02 +00001775 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001776
John McCallbecb8d52010-03-10 06:48:02 +00001777 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001778 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001779}
1780
Reid Spencer5f016e22007-07-11 17:01:13 +00001781/// getTypedefType - Return the unique reference to the type for the
1782/// specified typename decl.
John McCall19c85762010-02-16 03:57:14 +00001783QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Chris Lattnerf52ab252008-04-06 22:59:24 +00001786 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001787 Decl->TypeForDecl = new(*this, TypeAlignment)
1788 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 Types.push_back(Decl->TypeForDecl);
1790 return QualType(Decl->TypeForDecl, 0);
1791}
1792
John McCall49a832b2009-10-18 09:09:24 +00001793/// \brief Retrieve a substitution-result type.
1794QualType
1795ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1796 QualType Replacement) {
John McCall467b27b2009-10-22 20:10:53 +00001797 assert(Replacement.isCanonical()
John McCall49a832b2009-10-18 09:09:24 +00001798 && "replacement types must always be canonical");
1799
1800 llvm::FoldingSetNodeID ID;
1801 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1802 void *InsertPos = 0;
1803 SubstTemplateTypeParmType *SubstParm
1804 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1805
1806 if (!SubstParm) {
1807 SubstParm = new (*this, TypeAlignment)
1808 SubstTemplateTypeParmType(Parm, Replacement);
1809 Types.push_back(SubstParm);
1810 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1811 }
1812
1813 return QualType(SubstParm, 0);
1814}
1815
Douglas Gregorfab9d672009-02-05 23:33:38 +00001816/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001817/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001818/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001819QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001820 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001821 IdentifierInfo *Name) {
1822 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001823 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001824 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001825 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001826 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1827
1828 if (TypeParm)
1829 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001831 if (Name) {
1832 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall6b304a02009-09-24 23:30:46 +00001833 TypeParm = new (*this, TypeAlignment)
1834 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001835
1836 TemplateTypeParmType *TypeCheck
1837 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1838 assert(!TypeCheck && "Template type parameter canonical type broken");
1839 (void)TypeCheck;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001840 } else
John McCall6b304a02009-09-24 23:30:46 +00001841 TypeParm = new (*this, TypeAlignment)
1842 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001843
1844 Types.push_back(TypeParm);
1845 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1846
1847 return QualType(TypeParm, 0);
1848}
1849
John McCall3cb0ebd2010-03-10 03:28:59 +00001850TypeSourceInfo *
1851ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1852 SourceLocation NameLoc,
1853 const TemplateArgumentListInfo &Args,
1854 QualType CanonType) {
1855 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1856
1857 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1858 TemplateSpecializationTypeLoc TL
1859 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1860 TL.setTemplateNameLoc(NameLoc);
1861 TL.setLAngleLoc(Args.getLAngleLoc());
1862 TL.setRAngleLoc(Args.getRAngleLoc());
1863 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1864 TL.setArgLocInfo(i, Args[i].getLocInfo());
1865 return DI;
1866}
1867
Mike Stump1eb44332009-09-09 15:08:12 +00001868QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001869ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCalld5532b62009-11-23 01:53:49 +00001870 const TemplateArgumentListInfo &Args,
John McCall833ca992009-10-29 08:12:44 +00001871 QualType Canon) {
John McCalld5532b62009-11-23 01:53:49 +00001872 unsigned NumArgs = Args.size();
1873
John McCall833ca992009-10-29 08:12:44 +00001874 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1875 ArgVec.reserve(NumArgs);
1876 for (unsigned i = 0; i != NumArgs; ++i)
1877 ArgVec.push_back(Args[i].getArgument());
1878
1879 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon);
1880}
1881
1882QualType
1883ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001884 const TemplateArgument *Args,
1885 unsigned NumArgs,
1886 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001887 if (!Canon.isNull())
1888 Canon = getCanonicalType(Canon);
1889 else {
1890 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001891 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1892 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1893 CanonArgs.reserve(NumArgs);
1894 for (unsigned I = 0; I != NumArgs; ++I)
1895 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1896
1897 // Determine whether this canonical template specialization type already
1898 // exists.
1899 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001900 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001901 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001902
1903 void *InsertPos = 0;
1904 TemplateSpecializationType *Spec
1905 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Douglas Gregor1275ae02009-07-28 23:00:59 +00001907 if (!Spec) {
1908 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001909 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001910 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001911 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001912 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001913 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001914 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001915 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001916 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001917 }
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Douglas Gregorb88e8882009-07-30 17:40:51 +00001919 if (Canon.isNull())
1920 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001921 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001922 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001923 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001924
Douglas Gregor1275ae02009-07-28 23:00:59 +00001925 // Allocate the (non-canonical) template specialization type, but don't
1926 // try to unique it: these types typically have location information that
1927 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001928 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001929 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001930 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001931 TemplateSpecializationType *Spec
1932 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001933 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001934
Douglas Gregor55f6b142009-02-09 18:46:07 +00001935 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001936 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001937}
1938
Mike Stump1eb44332009-09-09 15:08:12 +00001939QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001940ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001941 QualType NamedType) {
1942 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001943 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001944
1945 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001946 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001947 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1948 if (T)
1949 return QualType(T, 0);
1950
Douglas Gregor789b1f62010-02-04 18:10:26 +00001951 QualType Canon = NamedType;
1952 if (!Canon.isCanonical()) {
1953 Canon = getCanonicalType(NamedType);
1954 QualifiedNameType *CheckT
1955 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1956 assert(!CheckT && "Qualified name canonical type broken");
1957 (void)CheckT;
1958 }
1959
1960 T = new (*this) QualifiedNameType(NNS, NamedType, Canon);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001961 Types.push_back(T);
1962 QualifiedNameTypes.InsertNode(T, InsertPos);
1963 return QualType(T, 0);
1964}
1965
Mike Stump1eb44332009-09-09 15:08:12 +00001966QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001967 const IdentifierInfo *Name,
1968 QualType Canon) {
1969 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1970
1971 if (Canon.isNull()) {
1972 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1973 if (CanonNNS != NNS)
1974 Canon = getTypenameType(CanonNNS, Name);
1975 }
1976
1977 llvm::FoldingSetNodeID ID;
1978 TypenameType::Profile(ID, NNS, Name);
1979
1980 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001981 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001982 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1983 if (T)
1984 return QualType(T, 0);
1985
1986 T = new (*this) TypenameType(NNS, Name, Canon);
1987 Types.push_back(T);
1988 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001989 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001990}
1991
Mike Stump1eb44332009-09-09 15:08:12 +00001992QualType
1993ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001994 const TemplateSpecializationType *TemplateId,
1995 QualType Canon) {
1996 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1997
Douglas Gregor789b1f62010-02-04 18:10:26 +00001998 llvm::FoldingSetNodeID ID;
1999 TypenameType::Profile(ID, NNS, TemplateId);
2000
2001 void *InsertPos = 0;
2002 TypenameType *T
2003 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
2004 if (T)
2005 return QualType(T, 0);
2006
Douglas Gregor17343172009-04-01 00:28:59 +00002007 if (Canon.isNull()) {
2008 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2009 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
2010 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
2011 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00002012 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00002013 assert(CanonTemplateId &&
2014 "Canonical type must also be a template specialization type");
2015 Canon = getTypenameType(CanonNNS, CanonTemplateId);
2016 }
Douglas Gregor789b1f62010-02-04 18:10:26 +00002017
2018 TypenameType *CheckT
2019 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
2020 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregor17343172009-04-01 00:28:59 +00002021 }
2022
Douglas Gregor17343172009-04-01 00:28:59 +00002023 T = new (*this) TypenameType(NNS, TemplateId, Canon);
2024 Types.push_back(T);
2025 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002026 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00002027}
2028
John McCall7da24312009-09-05 00:15:47 +00002029QualType
2030ASTContext::getElaboratedType(QualType UnderlyingType,
2031 ElaboratedType::TagKind Tag) {
2032 llvm::FoldingSetNodeID ID;
2033 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00002034
John McCall7da24312009-09-05 00:15:47 +00002035 void *InsertPos = 0;
2036 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2037 if (T)
2038 return QualType(T, 0);
2039
Douglas Gregor789b1f62010-02-04 18:10:26 +00002040 QualType Canon = UnderlyingType;
2041 if (!Canon.isCanonical()) {
2042 Canon = getCanonicalType(Canon);
2043 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2044 assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT;
2045 }
John McCall7da24312009-09-05 00:15:47 +00002046
2047 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
2048 Types.push_back(T);
2049 ElaboratedTypes.InsertNode(T, InsertPos);
2050 return QualType(T, 0);
2051}
2052
Chris Lattner88cb27a2008-04-07 04:56:42 +00002053/// CmpProtocolNames - Comparison predicate for sorting protocols
2054/// alphabetically.
2055static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2056 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002057 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00002058}
2059
John McCall54e14c42009-10-22 22:37:11 +00002060static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
2061 unsigned NumProtocols) {
2062 if (NumProtocols == 0) return true;
2063
2064 for (unsigned i = 1; i != NumProtocols; ++i)
2065 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2066 return false;
2067 return true;
2068}
2069
2070static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattner88cb27a2008-04-07 04:56:42 +00002071 unsigned &NumProtocols) {
2072 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Chris Lattner88cb27a2008-04-07 04:56:42 +00002074 // Sort protocols, keyed by name.
2075 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2076
2077 // Remove duplicates.
2078 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2079 NumProtocols = ProtocolsEnd-Protocols;
2080}
2081
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002082/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2083/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00002084QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00002085 ObjCProtocolDecl **Protocols,
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002086 unsigned NumProtocols,
2087 unsigned Quals) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002088 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00002089 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002090 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002091
2092 void *InsertPos = 0;
2093 if (ObjCObjectPointerType *QT =
2094 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002095 return getQualifiedType(QualType(QT, 0), Qs);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002096
John McCall54e14c42009-10-22 22:37:11 +00002097 // Sort the protocol list alphabetically to canonicalize it.
2098 QualType Canonical;
2099 if (!InterfaceT.isCanonical() ||
2100 !areSortedAndUniqued(Protocols, NumProtocols)) {
2101 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
2102 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2103 unsigned UniqueCount = NumProtocols;
2104
2105 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2106 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2107
2108 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2109 &Sorted[0], UniqueCount);
2110 } else {
2111 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2112 Protocols, NumProtocols);
2113 }
2114
2115 // Regenerate InsertPos.
2116 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2117 }
2118
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002119 // No match.
2120 unsigned Size = sizeof(ObjCObjectPointerType)
2121 + NumProtocols * sizeof(ObjCProtocolDecl *);
2122 void *Mem = Allocate(Size, TypeAlignment);
2123 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2124 InterfaceT,
2125 Protocols,
2126 NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002128 Types.push_back(QType);
2129 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002130 return getQualifiedType(QualType(QType, 0), Qs);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002131}
Chris Lattner88cb27a2008-04-07 04:56:42 +00002132
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002133/// getObjCInterfaceType - Return the unique reference to the type for the
2134/// specified ObjC interface decl. The list of protocols is optional.
2135QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002136 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002137 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002138 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00002139
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002140 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002141 if (ObjCInterfaceType *QT =
2142 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002143 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002144
John McCall54e14c42009-10-22 22:37:11 +00002145 // Sort the protocol list alphabetically to canonicalize it.
2146 QualType Canonical;
2147 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
2148 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2149 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2150
2151 unsigned UniqueCount = NumProtocols;
2152 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2153
2154 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2155
2156 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2157 }
2158
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002159 unsigned Size = sizeof(ObjCInterfaceType)
2160 + NumProtocols * sizeof(ObjCProtocolDecl *);
2161 void *Mem = Allocate(Size, TypeAlignment);
2162 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2163 const_cast<ObjCInterfaceDecl*>(Decl),
2164 Protocols,
2165 NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002166
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002167 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002168 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002169 return QualType(QType, 0);
2170}
2171
Douglas Gregor72564e72009-02-26 23:50:07 +00002172/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2173/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002174/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002175/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002176/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002177QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002178 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002179 if (tofExpr->isTypeDependent()) {
2180 llvm::FoldingSetNodeID ID;
2181 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002182
Douglas Gregorb1975722009-07-30 23:18:24 +00002183 void *InsertPos = 0;
2184 DependentTypeOfExprType *Canon
2185 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2186 if (Canon) {
2187 // We already have a "canonical" version of an identical, dependent
2188 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002189 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002190 QualType((TypeOfExprType*)Canon, 0));
2191 }
2192 else {
2193 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002194 Canon
2195 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002196 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2197 toe = Canon;
2198 }
2199 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002200 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002201 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002202 }
Steve Naroff9752f252007-08-01 18:02:17 +00002203 Types.push_back(toe);
2204 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002205}
2206
Steve Naroff9752f252007-08-01 18:02:17 +00002207/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2208/// TypeOfType AST's. The only motivation to unique these nodes would be
2209/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002210/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002211/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002212QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002213 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002214 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002215 Types.push_back(tot);
2216 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002217}
2218
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002219/// getDecltypeForExpr - Given an expr, will return the decltype for that
2220/// expression, according to the rules in C++0x [dcl.type.simple]p4
2221static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002222 if (e->isTypeDependent())
2223 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002224
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002225 // If e is an id expression or a class member access, decltype(e) is defined
2226 // as the type of the entity named by e.
2227 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2228 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2229 return VD->getType();
2230 }
2231 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2232 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2233 return FD->getType();
2234 }
2235 // If e is a function call or an invocation of an overloaded operator,
2236 // (parentheses around e are ignored), decltype(e) is defined as the
2237 // return type of that function.
2238 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2239 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002240
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002241 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002242
2243 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002244 // defined as T&, otherwise decltype(e) is defined as T.
2245 if (e->isLvalue(Context) == Expr::LV_Valid)
2246 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002247
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002248 return T;
2249}
2250
Anders Carlsson395b4752009-06-24 19:06:50 +00002251/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2252/// DecltypeType AST's. The only motivation to unique these nodes would be
2253/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002254/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002255/// on canonical type's (which are always unique).
2256QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002257 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002258 if (e->isTypeDependent()) {
2259 llvm::FoldingSetNodeID ID;
2260 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002262 void *InsertPos = 0;
2263 DependentDecltypeType *Canon
2264 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2265 if (Canon) {
2266 // We already have a "canonical" version of an equivalent, dependent
2267 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002268 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002269 QualType((DecltypeType*)Canon, 0));
2270 }
2271 else {
2272 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002273 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002274 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2275 dt = Canon;
2276 }
2277 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002278 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002279 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002280 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002281 Types.push_back(dt);
2282 return QualType(dt, 0);
2283}
2284
Reid Spencer5f016e22007-07-11 17:01:13 +00002285/// getTagDeclType - Return the unique reference to the type for the
2286/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002287QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002288 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002289 // FIXME: What is the design on getTagDeclType when it requires casting
2290 // away const? mutable?
2291 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002292}
2293
Mike Stump1eb44332009-09-09 15:08:12 +00002294/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2295/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2296/// needs to agree with the definition in <stddef.h>.
Anders Carlssona3ccda52009-12-12 00:26:23 +00002297CanQualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002298 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002299}
2300
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002301/// getSignedWCharType - Return the type of "signed wchar_t".
2302/// Used when in C++, as a GCC extension.
2303QualType ASTContext::getSignedWCharType() const {
2304 // FIXME: derive from "Target" ?
2305 return WCharTy;
2306}
2307
2308/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2309/// Used when in C++, as a GCC extension.
2310QualType ASTContext::getUnsignedWCharType() const {
2311 // FIXME: derive from "Target" ?
2312 return UnsignedIntTy;
2313}
2314
Chris Lattner8b9023b2007-07-13 03:05:23 +00002315/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2316/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2317QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002318 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002319}
2320
Chris Lattnere6327742008-04-02 05:18:44 +00002321//===----------------------------------------------------------------------===//
2322// Type Operators
2323//===----------------------------------------------------------------------===//
2324
John McCall54e14c42009-10-22 22:37:11 +00002325CanQualType ASTContext::getCanonicalParamType(QualType T) {
2326 // Push qualifiers into arrays, and then discard any remaining
2327 // qualifiers.
2328 T = getCanonicalType(T);
2329 const Type *Ty = T.getTypePtr();
2330
2331 QualType Result;
2332 if (isa<ArrayType>(Ty)) {
2333 Result = getArrayDecayedType(QualType(Ty,0));
2334 } else if (isa<FunctionType>(Ty)) {
2335 Result = getPointerType(QualType(Ty, 0));
2336 } else {
2337 Result = QualType(Ty, 0);
2338 }
2339
2340 return CanQualType::CreateUnsafe(Result);
2341}
2342
Chris Lattner77c96472008-04-06 22:41:35 +00002343/// getCanonicalType - Return the canonical (structural) type corresponding to
2344/// the specified potentially non-canonical type. The non-canonical version
2345/// of a type may have many "decorated" versions of types. Decorators can
2346/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2347/// to be free of any of these, allowing two canonical types to be compared
2348/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002349CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002350 QualifierCollector Quals;
2351 const Type *Ptr = Quals.strip(T);
2352 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002353
John McCall0953e762009-09-24 19:53:00 +00002354 // The canonical internal type will be the canonical type *except*
2355 // that we push type qualifiers down through array types.
2356
2357 // If there are no new qualifiers to push down, stop here.
2358 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002359 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002360
John McCall0953e762009-09-24 19:53:00 +00002361 // If the type qualifiers are on an array type, get the canonical
2362 // type of the array with the qualifiers applied to the element
2363 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002364 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2365 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002366 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002367
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002368 // Get the canonical version of the element with the extra qualifiers on it.
2369 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002370 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002371 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002373 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002374 return CanQualType::CreateUnsafe(
2375 getConstantArrayType(NewEltTy, CAT->getSize(),
2376 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002377 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002378 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002379 return CanQualType::CreateUnsafe(
2380 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002381 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002382
Douglas Gregor898574e2008-12-05 23:32:09 +00002383 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002384 return CanQualType::CreateUnsafe(
2385 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002386 DSAT->getSizeExpr() ?
2387 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002388 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002389 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor87a924e2009-10-30 22:56:57 +00002390 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor898574e2008-12-05 23:32:09 +00002391
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002392 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002393 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002394 VAT->getSizeExpr() ?
2395 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002396 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002397 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002398 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002399}
2400
Chandler Carruth28e318c2009-12-29 07:16:59 +00002401QualType ASTContext::getUnqualifiedArrayType(QualType T,
2402 Qualifiers &Quals) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002403 Quals = T.getQualifiers();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002404 if (!isa<ArrayType>(T)) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002405 return T.getUnqualifiedType();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002406 }
2407
Chandler Carruth28e318c2009-12-29 07:16:59 +00002408 const ArrayType *AT = cast<ArrayType>(T);
2409 QualType Elt = AT->getElementType();
Zhongxing Xuc1ae0a82010-01-05 08:15:06 +00002410 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002411 if (Elt == UnqualElt)
2412 return T;
2413
2414 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2415 return getConstantArrayType(UnqualElt, CAT->getSize(),
2416 CAT->getSizeModifier(), 0);
2417 }
2418
2419 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2420 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2421 }
2422
2423 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2424 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2425 DSAT->getSizeModifier(), 0,
2426 SourceRange());
2427}
2428
John McCall80ad16f2009-11-24 18:42:40 +00002429DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2430 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2431 return TD->getDeclName();
2432
2433 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2434 if (DTN->isIdentifier()) {
2435 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2436 } else {
2437 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2438 }
2439 }
2440
John McCall0bd6feb2009-12-02 08:04:21 +00002441 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2442 assert(Storage);
2443 return (*Storage->begin())->getDeclName();
John McCall80ad16f2009-11-24 18:42:40 +00002444}
2445
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002446TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2447 // If this template name refers to a template, the canonical
2448 // template name merely stores the template itself.
2449 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002450 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002451
John McCall0bd6feb2009-12-02 08:04:21 +00002452 assert(!Name.getAsOverloadedTemplate());
Mike Stump1eb44332009-09-09 15:08:12 +00002453
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002454 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2455 assert(DTN && "Non-dependent template names must refer to template decls.");
2456 return DTN->CanonicalTemplateName;
2457}
2458
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002459bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2460 X = getCanonicalTemplateName(X);
2461 Y = getCanonicalTemplateName(Y);
2462 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2463}
2464
Mike Stump1eb44332009-09-09 15:08:12 +00002465TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002466ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2467 switch (Arg.getKind()) {
2468 case TemplateArgument::Null:
2469 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002470
Douglas Gregor1275ae02009-07-28 23:00:59 +00002471 case TemplateArgument::Expression:
Douglas Gregor1275ae02009-07-28 23:00:59 +00002472 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002473
Douglas Gregor1275ae02009-07-28 23:00:59 +00002474 case TemplateArgument::Declaration:
John McCall833ca992009-10-29 08:12:44 +00002475 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002476
Douglas Gregor788cd062009-11-11 01:00:40 +00002477 case TemplateArgument::Template:
2478 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2479
Douglas Gregor1275ae02009-07-28 23:00:59 +00002480 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00002481 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002482 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Douglas Gregor1275ae02009-07-28 23:00:59 +00002484 case TemplateArgument::Type:
John McCall833ca992009-10-29 08:12:44 +00002485 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002486
Douglas Gregor1275ae02009-07-28 23:00:59 +00002487 case TemplateArgument::Pack: {
2488 // FIXME: Allocate in ASTContext
2489 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2490 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002491 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002492 AEnd = Arg.pack_end();
2493 A != AEnd; (void)++A, ++Idx)
2494 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002495
Douglas Gregor1275ae02009-07-28 23:00:59 +00002496 TemplateArgument Result;
2497 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2498 return Result;
2499 }
2500 }
2501
2502 // Silence GCC warning
2503 assert(false && "Unhandled template argument kind");
2504 return TemplateArgument();
2505}
2506
Douglas Gregord57959a2009-03-27 23:10:48 +00002507NestedNameSpecifier *
2508ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002509 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002510 return 0;
2511
2512 switch (NNS->getKind()) {
2513 case NestedNameSpecifier::Identifier:
2514 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002515 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002516 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2517 NNS->getAsIdentifier());
2518
2519 case NestedNameSpecifier::Namespace:
2520 // A namespace is canonical; build a nested-name-specifier with
2521 // this namespace and no prefix.
2522 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2523
2524 case NestedNameSpecifier::TypeSpec:
2525 case NestedNameSpecifier::TypeSpecWithTemplate: {
2526 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002527 return NestedNameSpecifier::Create(*this, 0,
2528 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002529 T.getTypePtr());
2530 }
2531
2532 case NestedNameSpecifier::Global:
2533 // The global specifier is canonical and unique.
2534 return NNS;
2535 }
2536
2537 // Required to silence a GCC warning
2538 return 0;
2539}
2540
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002541
2542const ArrayType *ASTContext::getAsArrayType(QualType T) {
2543 // Handle the non-qualified case efficiently.
Douglas Gregora4923eb2009-11-16 21:35:15 +00002544 if (!T.hasLocalQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002545 // Handle the common positive case fast.
2546 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2547 return AT;
2548 }
Mike Stump1eb44332009-09-09 15:08:12 +00002549
John McCall0953e762009-09-24 19:53:00 +00002550 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002551 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002552 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002553 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002554
John McCall0953e762009-09-24 19:53:00 +00002555 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002556 // implements C99 6.7.3p8: "If the specification of an array type includes
2557 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002558
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002559 // If we get here, we either have type qualifiers on the type, or we have
2560 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002561 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002562
John McCall0953e762009-09-24 19:53:00 +00002563 QualifierCollector Qs;
2564 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002565
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002566 // If we have a simple case, just return now.
2567 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002568 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002569 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002570
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002571 // Otherwise, we have an array and we have qualifiers on it. Push the
2572 // qualifiers into the array element type and return a new array type.
2573 // Get the canonical version of the element with the extra qualifiers on it.
2574 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002575 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002576
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002577 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2578 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2579 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002580 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002581 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2582 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2583 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002584 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002585
Mike Stump1eb44332009-09-09 15:08:12 +00002586 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002587 = dyn_cast<DependentSizedArrayType>(ATy))
2588 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002589 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002590 DSAT->getSizeExpr() ?
2591 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002592 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002593 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002594 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002595
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002596 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002597 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002598 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002599 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002600 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002601 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002602 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002603}
2604
2605
Chris Lattnere6327742008-04-02 05:18:44 +00002606/// getArrayDecayedType - Return the properly qualified result of decaying the
2607/// specified array type to a pointer. This operation is non-trivial when
2608/// handling typedefs etc. The canonical type of "T" must be an array type,
2609/// this returns a pointer to a properly qualified element of the array.
2610///
2611/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2612QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002613 // Get the element type with 'getAsArrayType' so that we don't lose any
2614 // typedefs in the element type of the array. This also handles propagation
2615 // of type qualifiers from the array type into the element type if present
2616 // (C99 6.7.3p8).
2617 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2618 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002620 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002621
2622 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002623 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002624}
2625
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002626QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002627 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002628 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002629 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002630 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2631 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002632 } else {
John McCall0953e762009-09-24 19:53:00 +00002633 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002634 }
2635 }
2636}
2637
Anders Carlssonfbbce492009-09-25 01:23:32 +00002638QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2639 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002640
Anders Carlssonfbbce492009-09-25 01:23:32 +00002641 if (const ArrayType *AT = getAsArrayType(ElemTy))
2642 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002643
Anders Carlsson6183a992008-12-21 03:44:36 +00002644 return ElemTy;
2645}
2646
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002647/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002648uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002649ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2650 uint64_t ElementCount = 1;
2651 do {
2652 ElementCount *= CA->getSize().getZExtValue();
2653 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2654 } while (CA);
2655 return ElementCount;
2656}
2657
Reid Spencer5f016e22007-07-11 17:01:13 +00002658/// getFloatingRank - Return a relative rank for floating point types.
2659/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002660static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002661 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002662 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002663
John McCall183700f2009-09-21 23:43:11 +00002664 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2665 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002666 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002667 case BuiltinType::Float: return FloatRank;
2668 case BuiltinType::Double: return DoubleRank;
2669 case BuiltinType::LongDouble: return LongDoubleRank;
2670 }
2671}
2672
Mike Stump1eb44332009-09-09 15:08:12 +00002673/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2674/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002675/// 'typeDomain' is a real floating point or complex type.
2676/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002677QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2678 QualType Domain) const {
2679 FloatingRank EltRank = getFloatingRank(Size);
2680 if (Domain->isComplexType()) {
2681 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002682 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002683 case FloatRank: return FloatComplexTy;
2684 case DoubleRank: return DoubleComplexTy;
2685 case LongDoubleRank: return LongDoubleComplexTy;
2686 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002687 }
Chris Lattner1361b112008-04-06 23:58:54 +00002688
2689 assert(Domain->isRealFloatingType() && "Unknown domain!");
2690 switch (EltRank) {
2691 default: assert(0 && "getFloatingRank(): illegal value for rank");
2692 case FloatRank: return FloatTy;
2693 case DoubleRank: return DoubleTy;
2694 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002695 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002696}
2697
Chris Lattner7cfeb082008-04-06 23:55:33 +00002698/// getFloatingTypeOrder - Compare the rank of the two specified floating
2699/// point types, ignoring the domain of the type (i.e. 'double' ==
2700/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002701/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002702int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2703 FloatingRank LHSR = getFloatingRank(LHS);
2704 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Chris Lattnera75cea32008-04-06 23:38:49 +00002706 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002707 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002708 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002709 return 1;
2710 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002711}
2712
Chris Lattnerf52ab252008-04-06 22:59:24 +00002713/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2714/// routine will assert if passed a built-in type that isn't an integer or enum,
2715/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002716unsigned ASTContext::getIntegerRank(Type *T) {
John McCall467b27b2009-10-22 20:10:53 +00002717 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002718 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall842aef82009-12-09 09:09:27 +00002719 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedmanf98aba32009-02-13 02:31:07 +00002720
Eli Friedmana3426752009-07-05 23:44:27 +00002721 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2722 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2723
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002724 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2725 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2726
2727 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2728 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2729
Chris Lattnerf52ab252008-04-06 22:59:24 +00002730 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002731 default: assert(0 && "getIntegerRank(): not a built-in integer");
2732 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002733 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002734 case BuiltinType::Char_S:
2735 case BuiltinType::Char_U:
2736 case BuiltinType::SChar:
2737 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002738 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002739 case BuiltinType::Short:
2740 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002741 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002742 case BuiltinType::Int:
2743 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002744 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002745 case BuiltinType::Long:
2746 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002747 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002748 case BuiltinType::LongLong:
2749 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002750 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002751 case BuiltinType::Int128:
2752 case BuiltinType::UInt128:
2753 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002754 }
2755}
2756
Eli Friedman04e83572009-08-20 04:21:42 +00002757/// \brief Whether this is a promotable bitfield reference according
2758/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2759///
2760/// \returns the type this bit-field will promote to, or NULL if no
2761/// promotion occurs.
2762QualType ASTContext::isPromotableBitField(Expr *E) {
2763 FieldDecl *Field = E->getBitField();
2764 if (!Field)
2765 return QualType();
2766
2767 QualType FT = Field->getType();
2768
2769 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2770 uint64_t BitWidth = BitWidthAP.getZExtValue();
2771 uint64_t IntSize = getTypeSize(IntTy);
2772 // GCC extension compatibility: if the bit-field size is less than or equal
2773 // to the size of int, it gets promoted no matter what its type is.
2774 // For instance, unsigned long bf : 4 gets promoted to signed int.
2775 if (BitWidth < IntSize)
2776 return IntTy;
2777
2778 if (BitWidth == IntSize)
2779 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2780
2781 // Types bigger than int are not subject to promotions, and therefore act
2782 // like the base type.
2783 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2784 // is ridiculous.
2785 return QualType();
2786}
2787
Eli Friedmana95d7572009-08-19 07:44:53 +00002788/// getPromotedIntegerType - Returns the type that Promotable will
2789/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2790/// integer type.
2791QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2792 assert(!Promotable.isNull());
2793 assert(Promotable->isPromotableIntegerType());
John McCall842aef82009-12-09 09:09:27 +00002794 if (const EnumType *ET = Promotable->getAs<EnumType>())
2795 return ET->getDecl()->getPromotionType();
Eli Friedmana95d7572009-08-19 07:44:53 +00002796 if (Promotable->isSignedIntegerType())
2797 return IntTy;
2798 uint64_t PromotableSize = getTypeSize(Promotable);
2799 uint64_t IntSize = getTypeSize(IntTy);
2800 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2801 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2802}
2803
Mike Stump1eb44332009-09-09 15:08:12 +00002804/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002805/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002806/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002807int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002808 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2809 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002810 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002811
Chris Lattnerf52ab252008-04-06 22:59:24 +00002812 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2813 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002814
Chris Lattner7cfeb082008-04-06 23:55:33 +00002815 unsigned LHSRank = getIntegerRank(LHSC);
2816 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002817
Chris Lattner7cfeb082008-04-06 23:55:33 +00002818 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2819 if (LHSRank == RHSRank) return 0;
2820 return LHSRank > RHSRank ? 1 : -1;
2821 }
Mike Stump1eb44332009-09-09 15:08:12 +00002822
Chris Lattner7cfeb082008-04-06 23:55:33 +00002823 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2824 if (LHSUnsigned) {
2825 // If the unsigned [LHS] type is larger, return it.
2826 if (LHSRank >= RHSRank)
2827 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002828
Chris Lattner7cfeb082008-04-06 23:55:33 +00002829 // If the signed type can represent all values of the unsigned type, it
2830 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002831 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002832 return -1;
2833 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002834
Chris Lattner7cfeb082008-04-06 23:55:33 +00002835 // If the unsigned [RHS] type is larger, return it.
2836 if (RHSRank >= LHSRank)
2837 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002838
Chris Lattner7cfeb082008-04-06 23:55:33 +00002839 // If the signed type can represent all values of the unsigned type, it
2840 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002841 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002842 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002843}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002844
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002845static RecordDecl *
2846CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2847 SourceLocation L, IdentifierInfo *Id) {
2848 if (Ctx.getLangOptions().CPlusPlus)
2849 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2850 else
2851 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2852}
2853
Mike Stump1eb44332009-09-09 15:08:12 +00002854// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002855QualType ASTContext::getCFConstantStringType() {
2856 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002857 CFConstantStringTypeDecl =
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002858 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2859 &Idents.get("NSConstantString"));
John McCall5cfa0112010-02-05 01:33:36 +00002860 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002861
Anders Carlssonf06273f2007-11-19 00:25:30 +00002862 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002863
Anders Carlsson71993dd2007-08-17 05:31:46 +00002864 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002865 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002866 // int flags;
2867 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002868 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002869 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002870 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002871 FieldTypes[3] = LongTy;
2872
Anders Carlsson71993dd2007-08-17 05:31:46 +00002873 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002874 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002875 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002876 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00002877 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002878 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002879 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002880 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002881 }
2882
Douglas Gregor838db382010-02-11 01:19:42 +00002883 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson71993dd2007-08-17 05:31:46 +00002884 }
Mike Stump1eb44332009-09-09 15:08:12 +00002885
Anders Carlsson71993dd2007-08-17 05:31:46 +00002886 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002887}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002888
Douglas Gregor319ac892009-04-23 22:29:11 +00002889void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002890 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002891 assert(Rec && "Invalid CFConstantStringType");
2892 CFConstantStringTypeDecl = Rec->getDecl();
2893}
2894
Mike Stump1eb44332009-09-09 15:08:12 +00002895QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002896 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002897 ObjCFastEnumerationStateTypeDecl =
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002898 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2899 &Idents.get("__objcFastEnumerationState"));
John McCall5cfa0112010-02-05 01:33:36 +00002900 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002901
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002902 QualType FieldTypes[] = {
2903 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002904 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002905 getPointerType(UnsignedLongTy),
2906 getConstantArrayType(UnsignedLongTy,
2907 llvm::APInt(32, 5), ArrayType::Normal, 0)
2908 };
Mike Stump1eb44332009-09-09 15:08:12 +00002909
Douglas Gregor44b43212008-12-11 16:49:14 +00002910 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002911 FieldDecl *Field = FieldDecl::Create(*this,
2912 ObjCFastEnumerationStateTypeDecl,
2913 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00002914 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002915 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002916 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002917 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002918 }
Mike Stump1eb44332009-09-09 15:08:12 +00002919
Douglas Gregor838db382010-02-11 01:19:42 +00002920 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002921 }
Mike Stump1eb44332009-09-09 15:08:12 +00002922
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002923 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2924}
2925
Mike Stumpadaaad32009-10-20 02:12:22 +00002926QualType ASTContext::getBlockDescriptorType() {
2927 if (BlockDescriptorType)
2928 return getTagDeclType(BlockDescriptorType);
2929
2930 RecordDecl *T;
2931 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002932 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2933 &Idents.get("__block_descriptor"));
John McCall5cfa0112010-02-05 01:33:36 +00002934 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00002935
2936 QualType FieldTypes[] = {
2937 UnsignedLongTy,
2938 UnsignedLongTy,
2939 };
2940
2941 const char *FieldNames[] = {
2942 "reserved",
Mike Stump083c25e2009-10-22 00:49:09 +00002943 "Size"
Mike Stumpadaaad32009-10-20 02:12:22 +00002944 };
2945
2946 for (size_t i = 0; i < 2; ++i) {
2947 FieldDecl *Field = FieldDecl::Create(*this,
2948 T,
2949 SourceLocation(),
2950 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00002951 FieldTypes[i], /*TInfo=*/0,
Mike Stumpadaaad32009-10-20 02:12:22 +00002952 /*BitWidth=*/0,
2953 /*Mutable=*/false);
2954 T->addDecl(Field);
2955 }
2956
Douglas Gregor838db382010-02-11 01:19:42 +00002957 T->completeDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00002958
2959 BlockDescriptorType = T;
2960
2961 return getTagDeclType(BlockDescriptorType);
2962}
2963
2964void ASTContext::setBlockDescriptorType(QualType T) {
2965 const RecordType *Rec = T->getAs<RecordType>();
2966 assert(Rec && "Invalid BlockDescriptorType");
2967 BlockDescriptorType = Rec->getDecl();
2968}
2969
Mike Stump083c25e2009-10-22 00:49:09 +00002970QualType ASTContext::getBlockDescriptorExtendedType() {
2971 if (BlockDescriptorExtendedType)
2972 return getTagDeclType(BlockDescriptorExtendedType);
2973
2974 RecordDecl *T;
2975 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002976 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2977 &Idents.get("__block_descriptor_withcopydispose"));
John McCall5cfa0112010-02-05 01:33:36 +00002978 T->startDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00002979
2980 QualType FieldTypes[] = {
2981 UnsignedLongTy,
2982 UnsignedLongTy,
2983 getPointerType(VoidPtrTy),
2984 getPointerType(VoidPtrTy)
2985 };
2986
2987 const char *FieldNames[] = {
2988 "reserved",
2989 "Size",
2990 "CopyFuncPtr",
2991 "DestroyFuncPtr"
2992 };
2993
2994 for (size_t i = 0; i < 4; ++i) {
2995 FieldDecl *Field = FieldDecl::Create(*this,
2996 T,
2997 SourceLocation(),
2998 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00002999 FieldTypes[i], /*TInfo=*/0,
Mike Stump083c25e2009-10-22 00:49:09 +00003000 /*BitWidth=*/0,
3001 /*Mutable=*/false);
3002 T->addDecl(Field);
3003 }
3004
Douglas Gregor838db382010-02-11 01:19:42 +00003005 T->completeDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003006
3007 BlockDescriptorExtendedType = T;
3008
3009 return getTagDeclType(BlockDescriptorExtendedType);
3010}
3011
3012void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3013 const RecordType *Rec = T->getAs<RecordType>();
3014 assert(Rec && "Invalid BlockDescriptorType");
3015 BlockDescriptorExtendedType = Rec->getDecl();
3016}
3017
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003018bool ASTContext::BlockRequiresCopying(QualType Ty) {
3019 if (Ty->isBlockPointerType())
3020 return true;
3021 if (isObjCNSObjectType(Ty))
3022 return true;
3023 if (Ty->isObjCObjectPointerType())
3024 return true;
3025 return false;
3026}
3027
3028QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3029 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00003030 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003031 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00003032 // unsigned int __flags;
3033 // unsigned int __size;
Mike Stump38e16272009-10-21 22:01:24 +00003034 // void *__copy_helper; // as needed
3035 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003036 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00003037 // } *
3038
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003039 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3040
3041 // FIXME: Move up
Fariborz Jahanian4d0d85c2009-10-24 00:16:42 +00003042 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003043 llvm::SmallString<36> Name;
3044 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3045 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003046 RecordDecl *T;
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003047 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3048 &Idents.get(Name.str()));
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003049 T->startDefinition();
3050 QualType Int32Ty = IntTy;
3051 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3052 QualType FieldTypes[] = {
3053 getPointerType(VoidPtrTy),
3054 getPointerType(getTagDeclType(T)),
3055 Int32Ty,
3056 Int32Ty,
3057 getPointerType(VoidPtrTy),
3058 getPointerType(VoidPtrTy),
3059 Ty
3060 };
3061
3062 const char *FieldNames[] = {
3063 "__isa",
3064 "__forwarding",
3065 "__flags",
3066 "__size",
3067 "__copy_helper",
3068 "__destroy_helper",
3069 DeclName,
3070 };
3071
3072 for (size_t i = 0; i < 7; ++i) {
3073 if (!HasCopyAndDispose && i >=4 && i <= 5)
3074 continue;
3075 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3076 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003077 FieldTypes[i], /*TInfo=*/0,
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003078 /*BitWidth=*/0, /*Mutable=*/false);
3079 T->addDecl(Field);
3080 }
3081
Douglas Gregor838db382010-02-11 01:19:42 +00003082 T->completeDefinition();
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003083
3084 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00003085}
3086
3087
3088QualType ASTContext::getBlockParmType(
Mike Stump083c25e2009-10-22 00:49:09 +00003089 bool BlockHasCopyDispose,
Mike Stumpea26cb52009-10-21 03:49:08 +00003090 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpadaaad32009-10-20 02:12:22 +00003091 // FIXME: Move up
Fariborz Jahanian4d0d85c2009-10-24 00:16:42 +00003092 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003093 llvm::SmallString<36> Name;
3094 llvm::raw_svector_ostream(Name) << "__block_literal_"
3095 << ++UniqueBlockParmTypeID;
Mike Stumpadaaad32009-10-20 02:12:22 +00003096 RecordDecl *T;
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003097 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3098 &Idents.get(Name.str()));
John McCall5cfa0112010-02-05 01:33:36 +00003099 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003100 QualType FieldTypes[] = {
3101 getPointerType(VoidPtrTy),
3102 IntTy,
3103 IntTy,
3104 getPointerType(VoidPtrTy),
Mike Stump083c25e2009-10-22 00:49:09 +00003105 (BlockHasCopyDispose ?
3106 getPointerType(getBlockDescriptorExtendedType()) :
3107 getPointerType(getBlockDescriptorType()))
Mike Stumpadaaad32009-10-20 02:12:22 +00003108 };
3109
3110 const char *FieldNames[] = {
3111 "__isa",
3112 "__flags",
3113 "__reserved",
3114 "__FuncPtr",
3115 "__descriptor"
3116 };
3117
3118 for (size_t i = 0; i < 5; ++i) {
Mike Stumpea26cb52009-10-21 03:49:08 +00003119 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00003120 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003121 FieldTypes[i], /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003122 /*BitWidth=*/0, /*Mutable=*/false);
3123 T->addDecl(Field);
3124 }
3125
3126 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3127 const Expr *E = BlockDeclRefDecls[i];
3128 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3129 clang::IdentifierInfo *Name = 0;
3130 if (BDRE) {
3131 const ValueDecl *D = BDRE->getDecl();
3132 Name = &Idents.get(D->getName());
3133 }
3134 QualType FieldType = E->getType();
3135
3136 if (BDRE && BDRE->isByRef())
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003137 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3138 FieldType);
Mike Stumpea26cb52009-10-21 03:49:08 +00003139
3140 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCalla93c9342009-12-07 02:54:59 +00003141 Name, FieldType, /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003142 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpadaaad32009-10-20 02:12:22 +00003143 T->addDecl(Field);
3144 }
3145
Douglas Gregor838db382010-02-11 01:19:42 +00003146 T->completeDefinition();
Mike Stumpea26cb52009-10-21 03:49:08 +00003147
3148 return getPointerType(getTagDeclType(T));
Mike Stumpadaaad32009-10-20 02:12:22 +00003149}
3150
Douglas Gregor319ac892009-04-23 22:29:11 +00003151void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003152 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003153 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3154 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3155}
3156
Anders Carlssone8c49532007-10-29 06:33:42 +00003157// This returns true if a type has been typedefed to BOOL:
3158// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00003159static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00003160 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00003161 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3162 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00003163
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003164 return false;
3165}
3166
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003167/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003168/// purpose.
Ken Dyckaa8741a2010-01-11 19:19:56 +00003169CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck199c3d62010-01-11 17:06:35 +00003170 CharUnits sz = getTypeSizeInChars(type);
Mike Stump1eb44332009-09-09 15:08:12 +00003171
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003172 // Make all integer and enum types at least as large as an int
Ken Dyck199c3d62010-01-11 17:06:35 +00003173 if (sz.isPositive() && type->isIntegralType())
3174 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003175 // Treat arrays as pointers, since that's how they're passed in.
3176 else if (type->isArrayType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003177 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003178 return sz;
Ken Dyck199c3d62010-01-11 17:06:35 +00003179}
3180
3181static inline
3182std::string charUnitsToString(const CharUnits &CU) {
3183 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003184}
3185
David Chisnall5e530af2009-11-17 19:33:30 +00003186/// getObjCEncodingForBlockDecl - Return the encoded type for this method
3187/// declaration.
3188void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3189 std::string& S) {
3190 const BlockDecl *Decl = Expr->getBlockDecl();
3191 QualType BlockTy =
3192 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3193 // Encode result type.
3194 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3195 // Compute size of all parameters.
3196 // Start with computing size of a pointer in number of bytes.
3197 // FIXME: There might(should) be a better way of doing this computation!
3198 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003199 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3200 CharUnits ParmOffset = PtrSize;
David Chisnall5e530af2009-11-17 19:33:30 +00003201 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3202 E = Decl->param_end(); PI != E; ++PI) {
3203 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003204 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003205 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall5e530af2009-11-17 19:33:30 +00003206 ParmOffset += sz;
3207 }
3208 // Size of the argument frame
Ken Dyck199c3d62010-01-11 17:06:35 +00003209 S += charUnitsToString(ParmOffset);
David Chisnall5e530af2009-11-17 19:33:30 +00003210 // Block pointer and offset.
3211 S += "@?0";
3212 ParmOffset = PtrSize;
3213
3214 // Argument types.
3215 ParmOffset = PtrSize;
3216 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3217 Decl->param_end(); PI != E; ++PI) {
3218 ParmVarDecl *PVDecl = *PI;
3219 QualType PType = PVDecl->getOriginalType();
3220 if (const ArrayType *AT =
3221 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3222 // Use array's original type only if it has known number of
3223 // elements.
3224 if (!isa<ConstantArrayType>(AT))
3225 PType = PVDecl->getType();
3226 } else if (PType->isFunctionType())
3227 PType = PVDecl->getType();
3228 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003229 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003230 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall5e530af2009-11-17 19:33:30 +00003231 }
3232}
3233
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003234/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003235/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003236void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003237 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003238 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003239 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003240 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003241 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003242 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003243 // Compute size of all parameters.
3244 // Start with computing size of a pointer in number of bytes.
3245 // FIXME: There might(should) be a better way of doing this computation!
3246 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003247 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003248 // The first two arguments (self and _cmd) are pointers; account for
3249 // their size.
Ken Dyck199c3d62010-01-11 17:06:35 +00003250 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003251 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3252 E = Decl->param_end(); PI != E; ++PI) {
3253 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003254 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003255 assert (sz.isPositive() &&
3256 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003257 ParmOffset += sz;
3258 }
Ken Dyck199c3d62010-01-11 17:06:35 +00003259 S += charUnitsToString(ParmOffset);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003260 S += "@0:";
Ken Dyck199c3d62010-01-11 17:06:35 +00003261 S += charUnitsToString(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00003262
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003263 // Argument types.
3264 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003265 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3266 E = Decl->param_end(); PI != E; ++PI) {
3267 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00003268 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003269 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00003270 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3271 // Use array's original type only if it has known number of
3272 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00003273 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00003274 PType = PVDecl->getType();
3275 } else if (PType->isFunctionType())
3276 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003277 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003278 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003279 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003280 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003281 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003282 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003283 }
3284}
3285
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003286/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003287/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003288/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3289/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00003290/// Property attributes are stored as a comma-delimited C string. The simple
3291/// attributes readonly and bycopy are encoded as single characters. The
3292/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3293/// encoded as single characters, followed by an identifier. Property types
3294/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003295/// these attributes are defined by the following enumeration:
3296/// @code
3297/// enum PropertyAttributes {
3298/// kPropertyReadOnly = 'R', // property is read-only.
3299/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3300/// kPropertyByref = '&', // property is a reference to the value last assigned
3301/// kPropertyDynamic = 'D', // property is dynamic
3302/// kPropertyGetter = 'G', // followed by getter selector name
3303/// kPropertySetter = 'S', // followed by setter selector name
3304/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3305/// kPropertyType = 't' // followed by old-style type encoding.
3306/// kPropertyWeak = 'W' // 'weak' property
3307/// kPropertyStrong = 'P' // property GC'able
3308/// kPropertyNonAtomic = 'N' // property non-atomic
3309/// };
3310/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00003311void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003312 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003313 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003314 // Collect information from the property implementation decl(s).
3315 bool Dynamic = false;
3316 ObjCPropertyImplDecl *SynthesizePID = 0;
3317
3318 // FIXME: Duplicated code due to poor abstraction.
3319 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00003320 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003321 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3322 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003323 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003324 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003325 ObjCPropertyImplDecl *PID = *i;
3326 if (PID->getPropertyDecl() == PD) {
3327 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3328 Dynamic = true;
3329 } else {
3330 SynthesizePID = PID;
3331 }
3332 }
3333 }
3334 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003335 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003336 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003337 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003338 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003339 ObjCPropertyImplDecl *PID = *i;
3340 if (PID->getPropertyDecl() == PD) {
3341 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3342 Dynamic = true;
3343 } else {
3344 SynthesizePID = PID;
3345 }
3346 }
Mike Stump1eb44332009-09-09 15:08:12 +00003347 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003348 }
3349 }
3350
3351 // FIXME: This is not very efficient.
3352 S = "T";
3353
3354 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003355 // GCC has some special rules regarding encoding of properties which
3356 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003357 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003358 true /* outermost type */,
3359 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003360
3361 if (PD->isReadOnly()) {
3362 S += ",R";
3363 } else {
3364 switch (PD->getSetterKind()) {
3365 case ObjCPropertyDecl::Assign: break;
3366 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003367 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003368 }
3369 }
3370
3371 // It really isn't clear at all what this means, since properties
3372 // are "dynamic by default".
3373 if (Dynamic)
3374 S += ",D";
3375
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003376 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3377 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003378
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003379 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3380 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003381 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003382 }
3383
3384 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3385 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003386 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003387 }
3388
3389 if (SynthesizePID) {
3390 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3391 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00003392 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003393 }
3394
3395 // FIXME: OBJCGC: weak & strong
3396}
3397
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003398/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00003399/// Another legacy compatibility encoding: 32-bit longs are encoded as
3400/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003401/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3402///
3403void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00003404 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00003405 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003406 if (BT->getKind() == BuiltinType::ULong &&
3407 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003408 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00003409 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003410 if (BT->getKind() == BuiltinType::Long &&
3411 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003412 PointeeTy = IntTy;
3413 }
3414 }
3415}
3416
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003417void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003418 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003419 // We follow the behavior of gcc, expanding structures which are
3420 // directly pointed to, and expanding embedded structures. Note that
3421 // these rules are sufficient to prevent recursive encoding of the
3422 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00003423 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00003424 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003425}
3426
Mike Stump1eb44332009-09-09 15:08:12 +00003427static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003428 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003429 const Expr *E = FD->getBitWidth();
3430 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3431 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00003432 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003433 S += 'b';
3434 S += llvm::utostr(N);
3435}
3436
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003437// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003438void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3439 bool ExpandPointedToStructures,
3440 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003441 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003442 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003443 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00003444 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003445 if (FD && FD->isBitField())
3446 return EncodeBitField(this, S, FD);
3447 char encoding;
3448 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003449 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003450 case BuiltinType::Void: encoding = 'v'; break;
3451 case BuiltinType::Bool: encoding = 'B'; break;
3452 case BuiltinType::Char_U:
3453 case BuiltinType::UChar: encoding = 'C'; break;
3454 case BuiltinType::UShort: encoding = 'S'; break;
3455 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003456 case BuiltinType::ULong:
3457 encoding =
3458 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00003459 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003460 case BuiltinType::UInt128: encoding = 'T'; break;
3461 case BuiltinType::ULongLong: encoding = 'Q'; break;
3462 case BuiltinType::Char_S:
3463 case BuiltinType::SChar: encoding = 'c'; break;
3464 case BuiltinType::Short: encoding = 's'; break;
3465 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003466 case BuiltinType::Long:
3467 encoding =
3468 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003469 break;
3470 case BuiltinType::LongLong: encoding = 'q'; break;
3471 case BuiltinType::Int128: encoding = 't'; break;
3472 case BuiltinType::Float: encoding = 'f'; break;
3473 case BuiltinType::Double: encoding = 'd'; break;
3474 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003475 }
Mike Stump1eb44332009-09-09 15:08:12 +00003476
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003477 S += encoding;
3478 return;
3479 }
Mike Stump1eb44332009-09-09 15:08:12 +00003480
John McCall183700f2009-09-21 23:43:11 +00003481 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003482 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00003483 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003484 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003485 return;
3486 }
Fariborz Jahanian60bce3e2009-11-23 20:40:50 +00003487
Ted Kremenek6217b802009-07-29 21:53:49 +00003488 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003489 if (PT->isObjCSelType()) {
3490 S += ':';
3491 return;
3492 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003493 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003494
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003495 bool isReadOnly = false;
3496 // For historical/compatibility reasons, the read-only qualifier of the
3497 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3498 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003499 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003500 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003501 if (OutermostType && T.isConstQualified()) {
3502 isReadOnly = true;
3503 S += 'r';
3504 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003505 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003506 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003507 while (P->getAs<PointerType>())
3508 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003509 if (P.isConstQualified()) {
3510 isReadOnly = true;
3511 S += 'r';
3512 }
3513 }
3514 if (isReadOnly) {
3515 // Another legacy compatibility encoding. Some ObjC qualifier and type
3516 // combinations need to be rearranged.
3517 // Rewrite "in const" from "nr" to "rn"
3518 const char * s = S.c_str();
3519 int len = S.length();
3520 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3521 std::string replace = "rn";
3522 S.replace(S.end()-2, S.end(), replace);
3523 }
3524 }
Mike Stump1eb44332009-09-09 15:08:12 +00003525
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003526 if (PointeeTy->isCharType()) {
3527 // char pointer types should be encoded as '*' unless it is a
3528 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003529 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003530 S += '*';
3531 return;
3532 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003533 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003534 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3535 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3536 S += '#';
3537 return;
3538 }
3539 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3540 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3541 S += '@';
3542 return;
3543 }
3544 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003545 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003546 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003547 getLegacyIntegralTypeEncoding(PointeeTy);
3548
Mike Stump1eb44332009-09-09 15:08:12 +00003549 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003550 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003551 return;
3552 }
Mike Stump1eb44332009-09-09 15:08:12 +00003553
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003554 if (const ArrayType *AT =
3555 // Ignore type qualifiers etc.
3556 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003557 if (isa<IncompleteArrayType>(AT)) {
3558 // Incomplete arrays are encoded as a pointer to the array element.
3559 S += '^';
3560
Mike Stump1eb44332009-09-09 15:08:12 +00003561 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003562 false, ExpandStructures, FD);
3563 } else {
3564 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003565
Anders Carlsson559a8332009-02-22 01:38:57 +00003566 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3567 S += llvm::utostr(CAT->getSize().getZExtValue());
3568 else {
3569 //Variable length arrays are encoded as a regular array with 0 elements.
3570 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3571 S += '0';
3572 }
Mike Stump1eb44332009-09-09 15:08:12 +00003573
3574 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003575 false, ExpandStructures, FD);
3576 S += ']';
3577 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003578 return;
3579 }
Mike Stump1eb44332009-09-09 15:08:12 +00003580
John McCall183700f2009-09-21 23:43:11 +00003581 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003582 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003583 return;
3584 }
Mike Stump1eb44332009-09-09 15:08:12 +00003585
Ted Kremenek6217b802009-07-29 21:53:49 +00003586 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003587 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003588 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003589 // Anonymous structures print as '?'
3590 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3591 S += II->getName();
3592 } else {
3593 S += '?';
3594 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003595 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003596 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003597 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3598 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003599 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003600 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003601 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003602 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003603 S += '"';
3604 }
Mike Stump1eb44332009-09-09 15:08:12 +00003605
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003606 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003607 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003608 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003609 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003610 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003611 QualType qt = Field->getType();
3612 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003613 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003614 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003615 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003616 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003617 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003618 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003619 return;
3620 }
Mike Stump1eb44332009-09-09 15:08:12 +00003621
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003622 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003623 if (FD && FD->isBitField())
3624 EncodeBitField(this, S, FD);
3625 else
3626 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003627 return;
3628 }
Mike Stump1eb44332009-09-09 15:08:12 +00003629
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003630 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003631 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003632 return;
3633 }
Mike Stump1eb44332009-09-09 15:08:12 +00003634
John McCall0953e762009-09-24 19:53:00 +00003635 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003636 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003637 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003638 S += '{';
3639 const IdentifierInfo *II = OI->getIdentifier();
3640 S += II->getName();
3641 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003642 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003643 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003644 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003645 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003646 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003647 RecFields[i]);
3648 else
Mike Stump1eb44332009-09-09 15:08:12 +00003649 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003650 FD);
3651 }
3652 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003653 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003654 }
Mike Stump1eb44332009-09-09 15:08:12 +00003655
John McCall183700f2009-09-21 23:43:11 +00003656 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003657 if (OPT->isObjCIdType()) {
3658 S += '@';
3659 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003660 }
Mike Stump1eb44332009-09-09 15:08:12 +00003661
Steve Naroff27d20a22009-10-28 22:03:49 +00003662 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3663 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3664 // Since this is a binary compatibility issue, need to consult with runtime
3665 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff14108da2009-07-10 23:34:53 +00003666 S += '#';
3667 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003668 }
Mike Stump1eb44332009-09-09 15:08:12 +00003669
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003670 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003671 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003672 ExpandPointedToStructures,
3673 ExpandStructures, FD);
3674 if (FD || EncodingProperty) {
3675 // Note that we do extended encoding of protocol qualifer list
3676 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003677 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003678 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3679 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003680 S += '<';
3681 S += (*I)->getNameAsString();
3682 S += '>';
3683 }
3684 S += '"';
3685 }
3686 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003687 }
Mike Stump1eb44332009-09-09 15:08:12 +00003688
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003689 QualType PointeeTy = OPT->getPointeeType();
3690 if (!EncodingProperty &&
3691 isa<TypedefType>(PointeeTy.getTypePtr())) {
3692 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003693 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003694 // {...};
3695 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003696 getObjCEncodingForTypeImpl(PointeeTy, S,
3697 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003698 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003699 return;
3700 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003701
3702 S += '@';
Steve Naroff27d20a22009-10-28 22:03:49 +00003703 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003704 S += '"';
Steve Naroff27d20a22009-10-28 22:03:49 +00003705 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003706 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3707 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003708 S += '<';
3709 S += (*I)->getNameAsString();
3710 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003711 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003712 S += '"';
3713 }
3714 return;
3715 }
Mike Stump1eb44332009-09-09 15:08:12 +00003716
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003717 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003718}
3719
Mike Stump1eb44332009-09-09 15:08:12 +00003720void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003721 std::string& S) const {
3722 if (QT & Decl::OBJC_TQ_In)
3723 S += 'n';
3724 if (QT & Decl::OBJC_TQ_Inout)
3725 S += 'N';
3726 if (QT & Decl::OBJC_TQ_Out)
3727 S += 'o';
3728 if (QT & Decl::OBJC_TQ_Bycopy)
3729 S += 'O';
3730 if (QT & Decl::OBJC_TQ_Byref)
3731 S += 'R';
3732 if (QT & Decl::OBJC_TQ_Oneway)
3733 S += 'V';
3734}
3735
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003736void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003737 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003738
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003739 BuiltinVaListType = T;
3740}
3741
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003742void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003743 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003744}
3745
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003746void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00003747 ObjCSelTypedefType = T;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003748}
3749
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003750void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003751 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003752}
3753
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003754void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003755 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003756}
3757
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003758void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003759 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003760 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003761
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003762 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003763}
3764
John McCall0bd6feb2009-12-02 08:04:21 +00003765/// \brief Retrieve the template name that corresponds to a non-empty
3766/// lookup.
John McCalleec51cf2010-01-20 00:46:10 +00003767TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3768 UnresolvedSetIterator End) {
John McCall0bd6feb2009-12-02 08:04:21 +00003769 unsigned size = End - Begin;
3770 assert(size > 1 && "set is not overloaded!");
3771
3772 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3773 size * sizeof(FunctionTemplateDecl*));
3774 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3775
3776 NamedDecl **Storage = OT->getStorage();
John McCalleec51cf2010-01-20 00:46:10 +00003777 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCall0bd6feb2009-12-02 08:04:21 +00003778 NamedDecl *D = *I;
3779 assert(isa<FunctionTemplateDecl>(D) ||
3780 (isa<UsingShadowDecl>(D) &&
3781 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3782 *Storage++ = D;
3783 }
3784
3785 return TemplateName(OT);
3786}
3787
Douglas Gregor7532dc62009-03-30 22:58:21 +00003788/// \brief Retrieve the template name that represents a qualified
3789/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003790TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003791 bool TemplateKeyword,
3792 TemplateDecl *Template) {
Douglas Gregor789b1f62010-02-04 18:10:26 +00003793 // FIXME: Canonicalization?
Douglas Gregor7532dc62009-03-30 22:58:21 +00003794 llvm::FoldingSetNodeID ID;
3795 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3796
3797 void *InsertPos = 0;
3798 QualifiedTemplateName *QTN =
3799 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3800 if (!QTN) {
3801 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3802 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3803 }
3804
3805 return TemplateName(QTN);
3806}
3807
3808/// \brief Retrieve the template name that represents a dependent
3809/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003810TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003811 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003812 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003813 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003814
3815 llvm::FoldingSetNodeID ID;
3816 DependentTemplateName::Profile(ID, NNS, Name);
3817
3818 void *InsertPos = 0;
3819 DependentTemplateName *QTN =
3820 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3821
3822 if (QTN)
3823 return TemplateName(QTN);
3824
3825 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3826 if (CanonNNS == NNS) {
3827 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3828 } else {
3829 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3830 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00003831 DependentTemplateName *CheckQTN =
3832 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3833 assert(!CheckQTN && "Dependent type name canonicalization broken");
3834 (void)CheckQTN;
Douglas Gregor7532dc62009-03-30 22:58:21 +00003835 }
3836
3837 DependentTemplateNames.InsertNode(QTN, InsertPos);
3838 return TemplateName(QTN);
3839}
3840
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003841/// \brief Retrieve the template name that represents a dependent
3842/// template name such as \c MetaFun::template operator+.
3843TemplateName
3844ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3845 OverloadedOperatorKind Operator) {
3846 assert((!NNS || NNS->isDependent()) &&
3847 "Nested name specifier must be dependent");
3848
3849 llvm::FoldingSetNodeID ID;
3850 DependentTemplateName::Profile(ID, NNS, Operator);
3851
3852 void *InsertPos = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00003853 DependentTemplateName *QTN
3854 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003855
3856 if (QTN)
3857 return TemplateName(QTN);
3858
3859 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3860 if (CanonNNS == NNS) {
3861 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3862 } else {
3863 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3864 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00003865
3866 DependentTemplateName *CheckQTN
3867 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3868 assert(!CheckQTN && "Dependent template name canonicalization broken");
3869 (void)CheckQTN;
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003870 }
3871
3872 DependentTemplateNames.InsertNode(QTN, InsertPos);
3873 return TemplateName(QTN);
3874}
3875
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003876/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003877/// TargetInfo, produce the corresponding type. The unsigned @p Type
3878/// is actually a value of type @c TargetInfo::IntType.
John McCalle27ec8a2009-10-23 23:03:21 +00003879CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003880 switch (Type) {
John McCalle27ec8a2009-10-23 23:03:21 +00003881 case TargetInfo::NoInt: return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003882 case TargetInfo::SignedShort: return ShortTy;
3883 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3884 case TargetInfo::SignedInt: return IntTy;
3885 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3886 case TargetInfo::SignedLong: return LongTy;
3887 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3888 case TargetInfo::SignedLongLong: return LongLongTy;
3889 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3890 }
3891
3892 assert(false && "Unhandled TargetInfo::IntType value");
John McCalle27ec8a2009-10-23 23:03:21 +00003893 return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003894}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003895
3896//===----------------------------------------------------------------------===//
3897// Type Predicates.
3898//===----------------------------------------------------------------------===//
3899
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003900/// isObjCNSObjectType - Return true if this is an NSObject object using
3901/// NSObject attribute on a c-style pointer type.
3902/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003903/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003904///
3905bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3906 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3907 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003908 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003909 return true;
3910 }
Mike Stump1eb44332009-09-09 15:08:12 +00003911 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003912}
3913
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003914/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3915/// garbage collection attribute.
3916///
John McCall0953e762009-09-24 19:53:00 +00003917Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3918 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003919 if (getLangOptions().ObjC1 &&
3920 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003921 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003922 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003923 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003924 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003925 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003926 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003927 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003928 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003929 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003930 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003931 // Non-pointers have none gc'able attribute regardless of the attribute
3932 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003933 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003934 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003935 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003936 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003937}
3938
Chris Lattner6ac46a42008-04-07 06:51:04 +00003939//===----------------------------------------------------------------------===//
3940// Type Compatibility Testing
3941//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003942
Mike Stump1eb44332009-09-09 15:08:12 +00003943/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003944/// compatible.
3945static bool areCompatVectorTypes(const VectorType *LHS,
3946 const VectorType *RHS) {
John McCall467b27b2009-10-22 20:10:53 +00003947 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner6ac46a42008-04-07 06:51:04 +00003948 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003949 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003950}
3951
Steve Naroff4084c302009-07-23 01:01:38 +00003952//===----------------------------------------------------------------------===//
3953// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3954//===----------------------------------------------------------------------===//
3955
3956/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3957/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003958bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3959 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003960 if (lProto == rProto)
3961 return true;
3962 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3963 E = rProto->protocol_end(); PI != E; ++PI)
3964 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3965 return true;
3966 return false;
3967}
3968
Steve Naroff4084c302009-07-23 01:01:38 +00003969/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3970/// return true if lhs's protocols conform to rhs's protocol; false
3971/// otherwise.
3972bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3973 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3974 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3975 return false;
3976}
3977
3978/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3979/// ObjCQualifiedIDType.
3980bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3981 bool compare) {
3982 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003983 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003984 lhs->isObjCIdType() || lhs->isObjCClassType())
3985 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003986 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003987 rhs->isObjCIdType() || rhs->isObjCClassType())
3988 return true;
3989
3990 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003991 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003992
Steve Naroff4084c302009-07-23 01:01:38 +00003993 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003994
Steve Naroff4084c302009-07-23 01:01:38 +00003995 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003996 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003997 // make sure we check the class hierarchy.
3998 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3999 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4000 E = lhsQID->qual_end(); I != E; ++I) {
4001 // when comparing an id<P> on lhs with a static type on rhs,
4002 // see if static class implements all of id's protocols, directly or
4003 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004004 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00004005 return false;
4006 }
4007 }
4008 // If there are no qualifiers and no interface, we have an 'id'.
4009 return true;
4010 }
Mike Stump1eb44332009-09-09 15:08:12 +00004011 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004012 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4013 E = lhsQID->qual_end(); I != E; ++I) {
4014 ObjCProtocolDecl *lhsProto = *I;
4015 bool match = false;
4016
4017 // when comparing an id<P> on lhs with a static type on rhs,
4018 // see if static class implements all of id's protocols, directly or
4019 // through its super class and categories.
4020 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4021 E = rhsOPT->qual_end(); J != E; ++J) {
4022 ObjCProtocolDecl *rhsProto = *J;
4023 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4024 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4025 match = true;
4026 break;
4027 }
4028 }
Mike Stump1eb44332009-09-09 15:08:12 +00004029 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00004030 // make sure we check the class hierarchy.
4031 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4032 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4033 E = lhsQID->qual_end(); I != E; ++I) {
4034 // when comparing an id<P> on lhs with a static type on rhs,
4035 // see if static class implements all of id's protocols, directly or
4036 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004037 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004038 match = true;
4039 break;
4040 }
4041 }
4042 }
4043 if (!match)
4044 return false;
4045 }
Mike Stump1eb44332009-09-09 15:08:12 +00004046
Steve Naroff4084c302009-07-23 01:01:38 +00004047 return true;
4048 }
Mike Stump1eb44332009-09-09 15:08:12 +00004049
Steve Naroff4084c302009-07-23 01:01:38 +00004050 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4051 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4052
Mike Stump1eb44332009-09-09 15:08:12 +00004053 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00004054 lhs->getAsObjCInterfacePointerType()) {
4055 if (lhsOPT->qual_empty()) {
4056 bool match = false;
4057 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4058 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4059 E = rhsQID->qual_end(); I != E; ++I) {
4060 // when comparing an id<P> on lhs with a static type on rhs,
4061 // see if static class implements all of id's protocols, directly or
4062 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004063 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004064 match = true;
4065 break;
4066 }
4067 }
4068 if (!match)
4069 return false;
4070 }
4071 return true;
4072 }
Mike Stump1eb44332009-09-09 15:08:12 +00004073 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004074 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4075 E = lhsOPT->qual_end(); I != E; ++I) {
4076 ObjCProtocolDecl *lhsProto = *I;
4077 bool match = false;
4078
4079 // when comparing an id<P> on lhs with a static type on rhs,
4080 // see if static class implements all of id's protocols, directly or
4081 // through its super class and categories.
4082 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4083 E = rhsQID->qual_end(); J != E; ++J) {
4084 ObjCProtocolDecl *rhsProto = *J;
4085 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4086 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4087 match = true;
4088 break;
4089 }
4090 }
4091 if (!match)
4092 return false;
4093 }
4094 return true;
4095 }
4096 return false;
4097}
4098
Eli Friedman3d815e72008-08-22 00:56:42 +00004099/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004100/// compatible for assignment from RHS to LHS. This handles validation of any
4101/// protocol qualifiers on the LHS or RHS.
4102///
Steve Naroff14108da2009-07-10 23:34:53 +00004103bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4104 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004105 // If either type represents the built-in 'id' or 'Class' types, return true.
4106 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00004107 return true;
4108
Steve Naroff4084c302009-07-23 01:01:38 +00004109 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00004110 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4111 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00004112 false);
4113
Steve Naroff14108da2009-07-10 23:34:53 +00004114 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4115 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00004116 if (LHS && RHS) // We have 2 user-defined types.
4117 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00004118
Steve Naroff4084c302009-07-23 01:01:38 +00004119 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004120}
4121
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004122/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4123/// for providing type-safty for objective-c pointers used to pass/return
4124/// arguments in block literals. When passed as arguments, passing 'A*' where
4125/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4126/// not OK. For the return type, the opposite is not OK.
4127bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4128 const ObjCObjectPointerType *LHSOPT,
4129 const ObjCObjectPointerType *RHSOPT) {
4130 if (RHSOPT->isObjCBuiltinType())
4131 return true;
4132
4133 if (LHSOPT->isObjCBuiltinType()) {
4134 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4135 }
4136
4137 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
4138 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4139 QualType(RHSOPT,0),
4140 false);
4141
4142 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4143 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4144 if (LHS && RHS) { // We have 2 user-defined types.
4145 if (LHS != RHS) {
4146 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4147 return false;
4148 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4149 return true;
4150 }
4151 else
4152 return true;
4153 }
4154 return false;
4155}
4156
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004157/// getIntersectionOfProtocols - This routine finds the intersection of set
4158/// of protocols inherited from two distinct objective-c pointer objects.
4159/// It is used to build composite qualifier list of the composite type of
4160/// the conditional expression involving two objective-c pointer objects.
4161static
4162void getIntersectionOfProtocols(ASTContext &Context,
4163 const ObjCObjectPointerType *LHSOPT,
4164 const ObjCObjectPointerType *RHSOPT,
4165 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4166
4167 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4168 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4169
4170 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4171 unsigned LHSNumProtocols = LHS->getNumProtocols();
4172 if (LHSNumProtocols > 0)
4173 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4174 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004175 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4176 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004177 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4178 LHSInheritedProtocols.end());
4179 }
4180
4181 unsigned RHSNumProtocols = RHS->getNumProtocols();
4182 if (RHSNumProtocols > 0) {
4183 ObjCProtocolDecl **RHSProtocols = (ObjCProtocolDecl **)RHS->qual_begin();
4184 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4185 if (InheritedProtocolSet.count(RHSProtocols[i]))
4186 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4187 }
4188 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004189 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004190 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004191 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4192 RHSInheritedProtocols.begin(),
4193 E = RHSInheritedProtocols.end(); I != E; ++I)
4194 if (InheritedProtocolSet.count((*I)))
4195 IntersectionOfProtocols.push_back((*I));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004196 }
4197}
4198
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004199/// areCommonBaseCompatible - Returns common base class of the two classes if
4200/// one found. Note that this is O'2 algorithm. But it will be called as the
4201/// last type comparison in a ?-exp of ObjC pointer types before a
4202/// warning is issued. So, its invokation is extremely rare.
4203QualType ASTContext::areCommonBaseCompatible(
4204 const ObjCObjectPointerType *LHSOPT,
4205 const ObjCObjectPointerType *RHSOPT) {
4206 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4207 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4208 if (!LHS || !RHS)
4209 return QualType();
4210
4211 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4212 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4213 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004214 if (canAssignObjCInterfaces(LHS, RHS)) {
4215 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4216 getIntersectionOfProtocols(*this,
4217 LHSOPT, RHSOPT, IntersectionOfProtocols);
4218 if (IntersectionOfProtocols.empty())
4219 LHSTy = getObjCObjectPointerType(LHSTy);
4220 else
4221 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4222 IntersectionOfProtocols.size());
4223 return LHSTy;
4224 }
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004225 }
4226
4227 return QualType();
4228}
4229
Eli Friedman3d815e72008-08-22 00:56:42 +00004230bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4231 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00004232 // Verify that the base decls are compatible: the RHS must be a subclass of
4233 // the LHS.
4234 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4235 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004236
Chris Lattner6ac46a42008-04-07 06:51:04 +00004237 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4238 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004239 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004240 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004241
Chris Lattner6ac46a42008-04-07 06:51:04 +00004242 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4243 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004244 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004245 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00004246
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004247 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4248 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004249 LHSPI != LHSPE; LHSPI++) {
4250 bool RHSImplementsProtocol = false;
4251
4252 // If the RHS doesn't implement the protocol on the left, the types
4253 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004254 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00004255 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00004256 RHSPI != RHSPE; RHSPI++) {
4257 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004258 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00004259 break;
4260 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004261 }
4262 // FIXME: For better diagnostics, consider passing back the protocol name.
4263 if (!RHSImplementsProtocol)
4264 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004265 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004266 // The RHS implements all protocols listed on the LHS.
4267 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004268}
4269
Steve Naroff389bf462009-02-12 17:52:19 +00004270bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4271 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00004272 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4273 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004274
Steve Naroff14108da2009-07-10 23:34:53 +00004275 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00004276 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004277
4278 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4279 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00004280}
4281
Mike Stump1eb44332009-09-09 15:08:12 +00004282/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00004283/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00004284/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00004285/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00004286bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004287 if (getLangOptions().CPlusPlus)
4288 return hasSameType(LHS, RHS);
4289
Eli Friedman3d815e72008-08-22 00:56:42 +00004290 return !mergeTypes(LHS, RHS).isNull();
4291}
4292
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004293bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4294 return !mergeTypes(LHS, RHS, true).isNull();
4295}
4296
4297QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4298 bool OfBlockPointer) {
John McCall183700f2009-09-21 23:43:11 +00004299 const FunctionType *lbase = lhs->getAs<FunctionType>();
4300 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00004301 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4302 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00004303 bool allLTypes = true;
4304 bool allRTypes = true;
4305
4306 // Check return type
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004307 QualType retType;
4308 if (OfBlockPointer)
4309 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4310 else
4311 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman3d815e72008-08-22 00:56:42 +00004312 if (retType.isNull()) return QualType();
Fariborz Jahanian6de8b622010-02-10 00:32:12 +00004313 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner61710852008-10-05 17:34:18 +00004314 allLTypes = false;
Fariborz Jahanian6de8b622010-02-10 00:32:12 +00004315 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner61710852008-10-05 17:34:18 +00004316 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00004317 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00004318 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
4319 if (NoReturn != lbase->getNoReturnAttr())
4320 allLTypes = false;
4321 if (NoReturn != rbase->getNoReturnAttr())
4322 allRTypes = false;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004323 CallingConv lcc = lbase->getCallConv();
4324 CallingConv rcc = rbase->getCallConv();
4325 // Compatible functions must have compatible calling conventions
John McCall04a67a62010-02-05 21:31:56 +00004326 if (!isSameCallConv(lcc, rcc))
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004327 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004328
Eli Friedman3d815e72008-08-22 00:56:42 +00004329 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00004330 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4331 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004332 unsigned lproto_nargs = lproto->getNumArgs();
4333 unsigned rproto_nargs = rproto->getNumArgs();
4334
4335 // Compatible functions must have the same number of arguments
4336 if (lproto_nargs != rproto_nargs)
4337 return QualType();
4338
4339 // Variadic and non-variadic functions aren't compatible
4340 if (lproto->isVariadic() != rproto->isVariadic())
4341 return QualType();
4342
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00004343 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4344 return QualType();
4345
Eli Friedman3d815e72008-08-22 00:56:42 +00004346 // Check argument compatibility
4347 llvm::SmallVector<QualType, 10> types;
4348 for (unsigned i = 0; i < lproto_nargs; i++) {
4349 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4350 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004351 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman3d815e72008-08-22 00:56:42 +00004352 if (argtype.isNull()) return QualType();
4353 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00004354 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4355 allLTypes = false;
4356 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4357 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00004358 }
4359 if (allLTypes) return lhs;
4360 if (allRTypes) return rhs;
4361 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00004362 lproto->isVariadic(), lproto->getTypeQuals(),
Douglas Gregor1bf40242010-02-12 17:17:28 +00004363 false, false, 0, 0, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004364 }
4365
4366 if (lproto) allRTypes = false;
4367 if (rproto) allLTypes = false;
4368
Douglas Gregor72564e72009-02-26 23:50:07 +00004369 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00004370 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00004371 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004372 if (proto->isVariadic()) return QualType();
4373 // Check that the types are compatible with the types that
4374 // would result from default argument promotions (C99 6.7.5.3p15).
4375 // The only types actually affected are promotable integer
4376 // types and floats, which would be passed as a different
4377 // type depending on whether the prototype is visible.
4378 unsigned proto_nargs = proto->getNumArgs();
4379 for (unsigned i = 0; i < proto_nargs; ++i) {
4380 QualType argTy = proto->getArgType(i);
Douglas Gregorb0f8eac2010-02-03 19:27:29 +00004381
4382 // Look at the promotion type of enum types, since that is the type used
4383 // to pass enum values.
4384 if (const EnumType *Enum = argTy->getAs<EnumType>())
4385 argTy = Enum->getDecl()->getPromotionType();
4386
Eli Friedman3d815e72008-08-22 00:56:42 +00004387 if (argTy->isPromotableIntegerType() ||
4388 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4389 return QualType();
4390 }
4391
4392 if (allLTypes) return lhs;
4393 if (allRTypes) return rhs;
4394 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00004395 proto->getNumArgs(), proto->isVariadic(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00004396 proto->getTypeQuals(),
4397 false, false, 0, 0, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004398 }
4399
4400 if (allLTypes) return lhs;
4401 if (allRTypes) return rhs;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004402 return getFunctionNoProtoType(retType, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004403}
4404
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004405QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4406 bool OfBlockPointer) {
Bill Wendling43d69752007-12-03 07:33:35 +00004407 // C++ [expr]: If an expression initially has the type "reference to T", the
4408 // type is adjusted to "T" prior to any further analysis, the expression
4409 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004410 // expression is an lvalue unless the reference is an rvalue reference and
4411 // the expression is a function call (possibly inside parentheses).
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004412 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4413 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4414
Eli Friedman3d815e72008-08-22 00:56:42 +00004415 QualType LHSCan = getCanonicalType(LHS),
4416 RHSCan = getCanonicalType(RHS);
4417
4418 // If two types are identical, they are compatible.
4419 if (LHSCan == RHSCan)
4420 return LHS;
4421
John McCall0953e762009-09-24 19:53:00 +00004422 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregora4923eb2009-11-16 21:35:15 +00004423 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4424 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall0953e762009-09-24 19:53:00 +00004425 if (LQuals != RQuals) {
4426 // If any of these qualifiers are different, we have a type
4427 // mismatch.
4428 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4429 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4430 return QualType();
4431
4432 // Exactly one GC qualifier difference is allowed: __strong is
4433 // okay if the other type has no GC qualifier but is an Objective
4434 // C object pointer (i.e. implicitly strong by default). We fix
4435 // this by pretending that the unqualified type was actually
4436 // qualified __strong.
4437 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4438 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4439 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4440
4441 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4442 return QualType();
4443
4444 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4445 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4446 }
4447 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4448 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4449 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004450 return QualType();
John McCall0953e762009-09-24 19:53:00 +00004451 }
4452
4453 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00004454
Eli Friedman852d63b2009-06-01 01:22:52 +00004455 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4456 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00004457
Chris Lattner1adb8832008-01-14 05:45:46 +00004458 // We want to consider the two function types to be the same for these
4459 // comparisons, just force one to the other.
4460 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4461 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00004462
4463 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00004464 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4465 LHSClass = Type::ConstantArray;
4466 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4467 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00004468
Nate Begeman213541a2008-04-18 23:10:10 +00004469 // Canonicalize ExtVector -> Vector.
4470 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4471 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00004472
Chris Lattnera36a61f2008-04-07 05:43:21 +00004473 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004474 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00004475 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00004476 // a signed integer type, or an unsigned integer type.
John McCall842aef82009-12-09 09:09:27 +00004477 // Compatibility is based on the underlying type, not the promotion
4478 // type.
John McCall183700f2009-09-21 23:43:11 +00004479 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004480 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4481 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004482 }
John McCall183700f2009-09-21 23:43:11 +00004483 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004484 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4485 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004486 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004487
Eli Friedman3d815e72008-08-22 00:56:42 +00004488 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004489 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004490
Steve Naroff4a746782008-01-09 22:43:08 +00004491 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004492 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00004493#define TYPE(Class, Base)
4494#define ABSTRACT_TYPE(Class, Base)
John McCallad5e7382010-03-01 23:49:17 +00004495#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +00004496#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4497#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4498#include "clang/AST/TypeNodes.def"
4499 assert(false && "Non-canonical and dependent types shouldn't get here");
4500 return QualType();
4501
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004502 case Type::LValueReference:
4503 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00004504 case Type::MemberPointer:
4505 assert(false && "C++ should never be in mergeTypes");
4506 return QualType();
4507
4508 case Type::IncompleteArray:
4509 case Type::VariableArray:
4510 case Type::FunctionProto:
4511 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00004512 assert(false && "Types are eliminated above");
4513 return QualType();
4514
Chris Lattner1adb8832008-01-14 05:45:46 +00004515 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00004516 {
4517 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004518 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4519 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00004520 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4521 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00004522 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004523 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00004524 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004525 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004526 return getPointerType(ResultType);
4527 }
Steve Naroffc0febd52008-12-10 17:49:55 +00004528 case Type::BlockPointer:
4529 {
4530 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004531 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4532 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004533 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroffc0febd52008-12-10 17:49:55 +00004534 if (ResultType.isNull()) return QualType();
4535 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4536 return LHS;
4537 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4538 return RHS;
4539 return getBlockPointerType(ResultType);
4540 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004541 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00004542 {
4543 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4544 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4545 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4546 return QualType();
4547
4548 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4549 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4550 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4551 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00004552 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4553 return LHS;
4554 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4555 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00004556 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4557 ArrayType::ArraySizeModifier(), 0);
4558 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4559 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004560 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4561 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00004562 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4563 return LHS;
4564 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4565 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004566 if (LVAT) {
4567 // FIXME: This isn't correct! But tricky to implement because
4568 // the array's size has to be the size of LHS, but the type
4569 // has to be different.
4570 return LHS;
4571 }
4572 if (RVAT) {
4573 // FIXME: This isn't correct! But tricky to implement because
4574 // the array's size has to be the size of RHS, but the type
4575 // has to be different.
4576 return RHS;
4577 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00004578 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4579 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004580 return getIncompleteArrayType(ResultType,
4581 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004582 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004583 case Type::FunctionNoProto:
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004584 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregor72564e72009-02-26 23:50:07 +00004585 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00004586 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00004587 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00004588 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004589 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00004590 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00004591 case Type::Complex:
4592 // Distinct complex types are incompatible.
4593 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004594 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004595 // FIXME: The merged type should be an ExtVector!
John McCall1c471f32010-03-12 23:14:13 +00004596 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4597 RHSCan->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00004598 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00004599 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004600 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00004601 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004602 // FIXME: This should be type compatibility, e.g. whether
4603 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00004604 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4605 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00004606 if (LHSIface && RHSIface &&
4607 canAssignObjCInterfaces(LHSIface, RHSIface))
4608 return LHS;
4609
Eli Friedman3d815e72008-08-22 00:56:42 +00004610 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004611 }
Steve Naroff14108da2009-07-10 23:34:53 +00004612 case Type::ObjCObjectPointer: {
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004613 if (OfBlockPointer) {
4614 if (canAssignObjCInterfacesInBlockPointer(
4615 LHS->getAs<ObjCObjectPointerType>(),
4616 RHS->getAs<ObjCObjectPointerType>()))
4617 return LHS;
4618 return QualType();
4619 }
John McCall183700f2009-09-21 23:43:11 +00004620 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4621 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00004622 return LHS;
4623
Steve Naroffbc76dd02008-12-10 22:14:21 +00004624 return QualType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004625 }
Steve Naroffec0550f2007-10-15 20:41:53 +00004626 }
Douglas Gregor72564e72009-02-26 23:50:07 +00004627
4628 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004629}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00004630
Chris Lattner5426bf62008-04-07 07:01:58 +00004631//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00004632// Integer Predicates
4633//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00004634
Eli Friedmanad74a752008-06-28 06:23:08 +00004635unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl632d7722009-11-05 21:10:57 +00004636 if (T->isBooleanType())
Eli Friedmanad74a752008-06-28 06:23:08 +00004637 return 1;
John McCall842aef82009-12-09 09:09:27 +00004638 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedman29a7f332009-12-10 22:29:29 +00004639 T = ET->getDecl()->getIntegerType();
Eli Friedmanf98aba32009-02-13 02:31:07 +00004640 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00004641 return (unsigned)getTypeSize(T);
4642}
4643
4644QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4645 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00004646
4647 // Turn <4 x signed int> -> <4 x unsigned int>
4648 if (const VectorType *VTy = T->getAs<VectorType>())
4649 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson82287d12010-02-05 00:12:22 +00004650 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattner6a2b9262009-10-17 20:33:28 +00004651
4652 // For enums, we return the unsigned version of the base type.
4653 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00004654 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00004655
4656 const BuiltinType *BTy = T->getAs<BuiltinType>();
4657 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00004658 switch (BTy->getKind()) {
4659 case BuiltinType::Char_S:
4660 case BuiltinType::SChar:
4661 return UnsignedCharTy;
4662 case BuiltinType::Short:
4663 return UnsignedShortTy;
4664 case BuiltinType::Int:
4665 return UnsignedIntTy;
4666 case BuiltinType::Long:
4667 return UnsignedLongTy;
4668 case BuiltinType::LongLong:
4669 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00004670 case BuiltinType::Int128:
4671 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00004672 default:
4673 assert(0 && "Unexpected signed integer type");
4674 return QualType();
4675 }
4676}
4677
Douglas Gregor2cf26342009-04-09 22:27:44 +00004678ExternalASTSource::~ExternalASTSource() { }
4679
4680void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00004681
4682
4683//===----------------------------------------------------------------------===//
4684// Builtin Type Computation
4685//===----------------------------------------------------------------------===//
4686
4687/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4688/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004689static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004690 ASTContext::GetBuiltinTypeError &Error,
4691 bool AllowTypeModifiers = true) {
4692 // Modifiers.
4693 int HowLong = 0;
4694 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004695
Chris Lattner86df27b2009-06-14 00:45:47 +00004696 // Read the modifiers first.
4697 bool Done = false;
4698 while (!Done) {
4699 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004700 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004701 case 'S':
4702 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4703 assert(!Signed && "Can't use 'S' modifier multiple times!");
4704 Signed = true;
4705 break;
4706 case 'U':
4707 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4708 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4709 Unsigned = true;
4710 break;
4711 case 'L':
4712 assert(HowLong <= 2 && "Can't have LLLL modifier");
4713 ++HowLong;
4714 break;
4715 }
4716 }
4717
4718 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004719
Chris Lattner86df27b2009-06-14 00:45:47 +00004720 // Read the base type.
4721 switch (*Str++) {
4722 default: assert(0 && "Unknown builtin type letter!");
4723 case 'v':
4724 assert(HowLong == 0 && !Signed && !Unsigned &&
4725 "Bad modifiers used with 'v'!");
4726 Type = Context.VoidTy;
4727 break;
4728 case 'f':
4729 assert(HowLong == 0 && !Signed && !Unsigned &&
4730 "Bad modifiers used with 'f'!");
4731 Type = Context.FloatTy;
4732 break;
4733 case 'd':
4734 assert(HowLong < 2 && !Signed && !Unsigned &&
4735 "Bad modifiers used with 'd'!");
4736 if (HowLong)
4737 Type = Context.LongDoubleTy;
4738 else
4739 Type = Context.DoubleTy;
4740 break;
4741 case 's':
4742 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4743 if (Unsigned)
4744 Type = Context.UnsignedShortTy;
4745 else
4746 Type = Context.ShortTy;
4747 break;
4748 case 'i':
4749 if (HowLong == 3)
4750 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4751 else if (HowLong == 2)
4752 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4753 else if (HowLong == 1)
4754 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4755 else
4756 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4757 break;
4758 case 'c':
4759 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4760 if (Signed)
4761 Type = Context.SignedCharTy;
4762 else if (Unsigned)
4763 Type = Context.UnsignedCharTy;
4764 else
4765 Type = Context.CharTy;
4766 break;
4767 case 'b': // boolean
4768 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4769 Type = Context.BoolTy;
4770 break;
4771 case 'z': // size_t.
4772 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4773 Type = Context.getSizeType();
4774 break;
4775 case 'F':
4776 Type = Context.getCFConstantStringType();
4777 break;
4778 case 'a':
4779 Type = Context.getBuiltinVaListType();
4780 assert(!Type.isNull() && "builtin va list type not initialized!");
4781 break;
4782 case 'A':
4783 // This is a "reference" to a va_list; however, what exactly
4784 // this means depends on how va_list is defined. There are two
4785 // different kinds of va_list: ones passed by value, and ones
4786 // passed by reference. An example of a by-value va_list is
4787 // x86, where va_list is a char*. An example of by-ref va_list
4788 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4789 // we want this argument to be a char*&; for x86-64, we want
4790 // it to be a __va_list_tag*.
4791 Type = Context.getBuiltinVaListType();
4792 assert(!Type.isNull() && "builtin va list type not initialized!");
4793 if (Type->isArrayType()) {
4794 Type = Context.getArrayDecayedType(Type);
4795 } else {
4796 Type = Context.getLValueReferenceType(Type);
4797 }
4798 break;
4799 case 'V': {
4800 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004801 unsigned NumElements = strtoul(Str, &End, 10);
4802 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004803
Chris Lattner86df27b2009-06-14 00:45:47 +00004804 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004805
Chris Lattner86df27b2009-06-14 00:45:47 +00004806 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson82287d12010-02-05 00:12:22 +00004807 // FIXME: Don't know what to do about AltiVec.
4808 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattner86df27b2009-06-14 00:45:47 +00004809 break;
4810 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00004811 case 'X': {
4812 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4813 Type = Context.getComplexType(ElementType);
4814 break;
4815 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004816 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004817 Type = Context.getFILEType();
4818 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004819 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004820 return QualType();
4821 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004822 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004823 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004824 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004825 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004826 else
4827 Type = Context.getjmp_bufType();
4828
Mike Stumpfd612db2009-07-28 23:47:15 +00004829 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004830 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004831 return QualType();
4832 }
4833 break;
Mike Stump782fa302009-07-28 02:25:19 +00004834 }
Mike Stump1eb44332009-09-09 15:08:12 +00004835
Chris Lattner86df27b2009-06-14 00:45:47 +00004836 if (!AllowTypeModifiers)
4837 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004838
Chris Lattner86df27b2009-06-14 00:45:47 +00004839 Done = false;
4840 while (!Done) {
John McCall187ab372010-03-12 04:21:28 +00004841 switch (char c = *Str++) {
Chris Lattner86df27b2009-06-14 00:45:47 +00004842 default: Done = true; --Str; break;
4843 case '*':
Chris Lattner86df27b2009-06-14 00:45:47 +00004844 case '&':
John McCall187ab372010-03-12 04:21:28 +00004845 {
4846 // Both pointers and references can have their pointee types
4847 // qualified with an address space.
4848 char *End;
4849 unsigned AddrSpace = strtoul(Str, &End, 10);
4850 if (End != Str && AddrSpace != 0) {
4851 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4852 Str = End;
4853 }
4854 }
4855 if (c == '*')
4856 Type = Context.getPointerType(Type);
4857 else
4858 Type = Context.getLValueReferenceType(Type);
Chris Lattner86df27b2009-06-14 00:45:47 +00004859 break;
4860 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4861 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004862 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004863 break;
Fariborz Jahanian013af392010-01-26 22:48:42 +00004864 case 'D':
4865 Type = Context.getVolatileType(Type);
4866 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004867 }
4868 }
Mike Stump1eb44332009-09-09 15:08:12 +00004869
Chris Lattner86df27b2009-06-14 00:45:47 +00004870 return Type;
4871}
4872
4873/// GetBuiltinType - Return the type for the specified builtin.
4874QualType ASTContext::GetBuiltinType(unsigned id,
4875 GetBuiltinTypeError &Error) {
4876 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004877
Chris Lattner86df27b2009-06-14 00:45:47 +00004878 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004879
Chris Lattner86df27b2009-06-14 00:45:47 +00004880 Error = GE_None;
4881 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4882 if (Error != GE_None)
4883 return QualType();
4884 while (TypeStr[0] && TypeStr[0] != '.') {
4885 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4886 if (Error != GE_None)
4887 return QualType();
4888
4889 // Do array -> pointer decay. The builtin should use the decayed type.
4890 if (Ty->isArrayType())
4891 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004892
Chris Lattner86df27b2009-06-14 00:45:47 +00004893 ArgTypes.push_back(Ty);
4894 }
4895
4896 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4897 "'.' should only occur at end of builtin type list!");
4898
4899 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4900 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4901 return getFunctionNoProtoType(ResType);
Douglas Gregorce056bc2010-02-21 22:15:06 +00004902
4903 // FIXME: Should we create noreturn types?
Chris Lattner86df27b2009-06-14 00:45:47 +00004904 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00004905 TypeStr[0] == '.', 0, false, false, 0, 0,
4906 false, CC_Default);
Chris Lattner86df27b2009-06-14 00:45:47 +00004907}
Eli Friedmana95d7572009-08-19 07:44:53 +00004908
4909QualType
4910ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4911 // Perform the usual unary conversions. We do this early so that
4912 // integral promotions to "int" can allow us to exit early, in the
4913 // lhs == rhs check. Also, for conversion purposes, we ignore any
4914 // qualifiers. For example, "const float" and "float" are
4915 // equivalent.
4916 if (lhs->isPromotableIntegerType())
4917 lhs = getPromotedIntegerType(lhs);
4918 else
4919 lhs = lhs.getUnqualifiedType();
4920 if (rhs->isPromotableIntegerType())
4921 rhs = getPromotedIntegerType(rhs);
4922 else
4923 rhs = rhs.getUnqualifiedType();
4924
4925 // If both types are identical, no conversion is needed.
4926 if (lhs == rhs)
4927 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004928
Eli Friedmana95d7572009-08-19 07:44:53 +00004929 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4930 // The caller can deal with this (e.g. pointer + int).
4931 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4932 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004933
4934 // At this point, we have two different arithmetic types.
4935
Eli Friedmana95d7572009-08-19 07:44:53 +00004936 // Handle complex types first (C99 6.3.1.8p1).
4937 if (lhs->isComplexType() || rhs->isComplexType()) {
4938 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004939 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004940 // convert the rhs to the lhs complex type.
4941 return lhs;
4942 }
Mike Stump1eb44332009-09-09 15:08:12 +00004943 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004944 // convert the lhs to the rhs complex type.
4945 return rhs;
4946 }
4947 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004948 // When both operands are complex, the shorter operand is converted to the
4949 // type of the longer, and that is the type of the result. This corresponds
4950 // to what is done when combining two real floating-point operands.
4951 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004952 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004953 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004954 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004955 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004956 // "double _Complex" is promoted to "long double _Complex".
4957 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004958
4959 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004960 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004961 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004962 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004963 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004964 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4965 // domains match. This is a requirement for our implementation, C99
4966 // does not require this promotion.
4967 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4968 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4969 return rhs;
4970 } else { // handle "_Complex double, double".
4971 return lhs;
4972 }
4973 }
4974 return lhs; // The domain/size match exactly.
4975 }
4976 // Now handle "real" floating types (i.e. float, double, long double).
4977 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4978 // if we have an integer operand, the result is the real floating type.
4979 if (rhs->isIntegerType()) {
4980 // convert rhs to the lhs floating point type.
4981 return lhs;
4982 }
4983 if (rhs->isComplexIntegerType()) {
4984 // convert rhs to the complex floating point type.
4985 return getComplexType(lhs);
4986 }
4987 if (lhs->isIntegerType()) {
4988 // convert lhs to the rhs floating point type.
4989 return rhs;
4990 }
Mike Stump1eb44332009-09-09 15:08:12 +00004991 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004992 // convert lhs to the complex floating point type.
4993 return getComplexType(rhs);
4994 }
4995 // We have two real floating types, float/complex combos were handled above.
4996 // Convert the smaller operand to the bigger result.
4997 int result = getFloatingTypeOrder(lhs, rhs);
4998 if (result > 0) // convert the rhs
4999 return lhs;
5000 assert(result < 0 && "illegal float comparison");
5001 return rhs; // convert the lhs
5002 }
5003 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5004 // Handle GCC complex int extension.
5005 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5006 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5007
5008 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00005009 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00005010 rhsComplexInt->getElementType()) >= 0)
5011 return lhs; // convert the rhs
5012 return rhs;
5013 } else if (lhsComplexInt && rhs->isIntegerType()) {
5014 // convert the rhs to the lhs complex type.
5015 return lhs;
5016 } else if (rhsComplexInt && lhs->isIntegerType()) {
5017 // convert the lhs to the rhs complex type.
5018 return rhs;
5019 }
5020 }
5021 // Finally, we have two differing integer types.
5022 // The rules for this case are in C99 6.3.1.8
5023 int compare = getIntegerTypeOrder(lhs, rhs);
5024 bool lhsSigned = lhs->isSignedIntegerType(),
5025 rhsSigned = rhs->isSignedIntegerType();
5026 QualType destType;
5027 if (lhsSigned == rhsSigned) {
5028 // Same signedness; use the higher-ranked type
5029 destType = compare >= 0 ? lhs : rhs;
5030 } else if (compare != (lhsSigned ? 1 : -1)) {
5031 // The unsigned type has greater than or equal rank to the
5032 // signed type, so use the unsigned type
5033 destType = lhsSigned ? rhs : lhs;
5034 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5035 // The two types are different widths; if we are here, that
5036 // means the signed type is larger than the unsigned type, so
5037 // use the signed type.
5038 destType = lhsSigned ? lhs : rhs;
5039 } else {
5040 // The signed type is higher-ranked than the unsigned type,
5041 // but isn't actually any bigger (like unsigned int and long
5042 // on most 32-bit systems). Use the unsigned type corresponding
5043 // to the signed type.
5044 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5045 }
5046 return destType;
5047}